123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803 |
- /*
- * HSI character device driver, implements the character device
- * interface.
- *
- * Copyright (C) 2010 Nokia Corporation. All rights reserved.
- *
- * Contact: Andras Domokos <andras.domokos@nokia.com>
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * version 2 as published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA
- */
- #include <linux/errno.h>
- #include <linux/types.h>
- #include <linux/atomic.h>
- #include <linux/kernel.h>
- #include <linux/init.h>
- #include <linux/module.h>
- #include <linux/mutex.h>
- #include <linux/list.h>
- #include <linux/slab.h>
- #include <linux/kmemleak.h>
- #include <linux/ioctl.h>
- #include <linux/wait.h>
- #include <linux/fs.h>
- #include <linux/sched.h>
- #include <linux/device.h>
- #include <linux/cdev.h>
- #include <linux/uaccess.h>
- #include <linux/scatterlist.h>
- #include <linux/stat.h>
- #include <linux/hsi/hsi.h>
- #include <linux/hsi/hsi_char.h>
- #define HSC_DEVS 16 /* Num of channels */
- #define HSC_MSGS 4
- #define HSC_RXBREAK 0
- #define HSC_ID_BITS 6
- #define HSC_PORT_ID_BITS 4
- #define HSC_ID_MASK 3
- #define HSC_PORT_ID_MASK 3
- #define HSC_CH_MASK 0xf
- /*
- * We support up to 4 controllers that can have up to 4
- * ports, which should currently be more than enough.
- */
- #define HSC_BASEMINOR(id, port_id) \
- ((((id) & HSC_ID_MASK) << HSC_ID_BITS) | \
- (((port_id) & HSC_PORT_ID_MASK) << HSC_PORT_ID_BITS))
- enum {
- HSC_CH_OPEN,
- HSC_CH_READ,
- HSC_CH_WRITE,
- HSC_CH_WLINE,
- };
- enum {
- HSC_RX,
- HSC_TX,
- };
- struct hsc_client_data;
- /**
- * struct hsc_channel - hsi_char internal channel data
- * @ch: channel number
- * @flags: Keeps state of the channel (open/close, reading, writing)
- * @free_msgs_list: List of free HSI messages/requests
- * @rx_msgs_queue: List of pending RX requests
- * @tx_msgs_queue: List of pending TX requests
- * @lock: Serialize access to the lists
- * @cl: reference to the associated hsi_client
- * @cl_data: reference to the client data that this channels belongs to
- * @rx_wait: RX requests wait queue
- * @tx_wait: TX requests wait queue
- */
- struct hsc_channel {
- unsigned int ch;
- unsigned long flags;
- struct list_head free_msgs_list;
- struct list_head rx_msgs_queue;
- struct list_head tx_msgs_queue;
- spinlock_t lock;
- struct hsi_client *cl;
- struct hsc_client_data *cl_data;
- wait_queue_head_t rx_wait;
- wait_queue_head_t tx_wait;
- };
- /**
- * struct hsc_client_data - hsi_char internal client data
- * @cdev: Characther device associated to the hsi_client
- * @lock: Lock to serialize open/close access
- * @flags: Keeps track of port state (rx hwbreak armed)
- * @usecnt: Use count for claiming the HSI port (mutex protected)
- * @cl: Referece to the HSI client
- * @channels: Array of channels accessible by the client
- */
- struct hsc_client_data {
- struct cdev cdev;
- struct mutex lock;
- unsigned long flags;
- unsigned int usecnt;
- struct hsi_client *cl;
- struct hsc_channel channels[HSC_DEVS];
- };
- /* Stores the major number dynamically allocated for hsi_char */
- static unsigned int hsc_major;
- /* Maximum buffer size that hsi_char will accept from userspace */
- static unsigned int max_data_size = 0x1000;
- module_param(max_data_size, uint, 0);
- MODULE_PARM_DESC(max_data_size, "max read/write data size [4,8..65536] (^2)");
- static void hsc_add_tail(struct hsc_channel *channel, struct hsi_msg *msg,
- struct list_head *queue)
- {
- unsigned long flags;
- spin_lock_irqsave(&channel->lock, flags);
- list_add_tail(&msg->link, queue);
- spin_unlock_irqrestore(&channel->lock, flags);
- }
- static struct hsi_msg *hsc_get_first_msg(struct hsc_channel *channel,
- struct list_head *queue)
- {
- struct hsi_msg *msg = NULL;
- unsigned long flags;
- spin_lock_irqsave(&channel->lock, flags);
- if (list_empty(queue))
- goto out;
- msg = list_first_entry(queue, struct hsi_msg, link);
- list_del(&msg->link);
- out:
- spin_unlock_irqrestore(&channel->lock, flags);
- return msg;
- }
- static inline void hsc_msg_free(struct hsi_msg *msg)
- {
- kfree(sg_virt(msg->sgt.sgl));
- hsi_free_msg(msg);
- }
- static void hsc_free_list(struct list_head *list)
- {
- struct hsi_msg *msg, *tmp;
- list_for_each_entry_safe(msg, tmp, list, link) {
- list_del(&msg->link);
- hsc_msg_free(msg);
- }
- }
- static void hsc_reset_list(struct hsc_channel *channel, struct list_head *l)
- {
- unsigned long flags;
- LIST_HEAD(list);
- spin_lock_irqsave(&channel->lock, flags);
- list_splice_init(l, &list);
- spin_unlock_irqrestore(&channel->lock, flags);
- hsc_free_list(&list);
- }
- static inline struct hsi_msg *hsc_msg_alloc(unsigned int alloc_size)
- {
- struct hsi_msg *msg;
- void *buf;
- msg = hsi_alloc_msg(1, GFP_KERNEL);
- if (!msg)
- goto out;
- buf = kmalloc(alloc_size, GFP_KERNEL);
- if (!buf) {
- hsi_free_msg(msg);
- goto out;
- }
- sg_init_one(msg->sgt.sgl, buf, alloc_size);
- /* Ignore false positive, due to sg pointer handling */
- kmemleak_ignore(buf);
- return msg;
- out:
- return NULL;
- }
- static inline int hsc_msgs_alloc(struct hsc_channel *channel)
- {
- struct hsi_msg *msg;
- int i;
- for (i = 0; i < HSC_MSGS; i++) {
- msg = hsc_msg_alloc(max_data_size);
- if (!msg)
- goto out;
- msg->channel = channel->ch;
- list_add_tail(&msg->link, &channel->free_msgs_list);
- }
- return 0;
- out:
- hsc_free_list(&channel->free_msgs_list);
- return -ENOMEM;
- }
- static inline unsigned int hsc_msg_len_get(struct hsi_msg *msg)
- {
- return msg->sgt.sgl->length;
- }
- static inline void hsc_msg_len_set(struct hsi_msg *msg, unsigned int len)
- {
- msg->sgt.sgl->length = len;
- }
- static void hsc_rx_completed(struct hsi_msg *msg)
- {
- struct hsc_client_data *cl_data = hsi_client_drvdata(msg->cl);
- struct hsc_channel *channel = cl_data->channels + msg->channel;
- if (test_bit(HSC_CH_READ, &channel->flags)) {
- hsc_add_tail(channel, msg, &channel->rx_msgs_queue);
- wake_up(&channel->rx_wait);
- } else {
- hsc_add_tail(channel, msg, &channel->free_msgs_list);
- }
- }
- static void hsc_rx_msg_destructor(struct hsi_msg *msg)
- {
- msg->status = HSI_STATUS_ERROR;
- hsc_msg_len_set(msg, 0);
- hsc_rx_completed(msg);
- }
- static void hsc_tx_completed(struct hsi_msg *msg)
- {
- struct hsc_client_data *cl_data = hsi_client_drvdata(msg->cl);
- struct hsc_channel *channel = cl_data->channels + msg->channel;
- if (test_bit(HSC_CH_WRITE, &channel->flags)) {
- hsc_add_tail(channel, msg, &channel->tx_msgs_queue);
- wake_up(&channel->tx_wait);
- } else {
- hsc_add_tail(channel, msg, &channel->free_msgs_list);
- }
- }
- static void hsc_tx_msg_destructor(struct hsi_msg *msg)
- {
- msg->status = HSI_STATUS_ERROR;
- hsc_msg_len_set(msg, 0);
- hsc_tx_completed(msg);
- }
- static void hsc_break_req_destructor(struct hsi_msg *msg)
- {
- struct hsc_client_data *cl_data = hsi_client_drvdata(msg->cl);
- hsi_free_msg(msg);
- clear_bit(HSC_RXBREAK, &cl_data->flags);
- }
- static void hsc_break_received(struct hsi_msg *msg)
- {
- struct hsc_client_data *cl_data = hsi_client_drvdata(msg->cl);
- struct hsc_channel *channel = cl_data->channels;
- int i, ret;
- /* Broadcast HWBREAK on all channels */
- for (i = 0; i < HSC_DEVS; i++, channel++) {
- struct hsi_msg *msg2;
- if (!test_bit(HSC_CH_READ, &channel->flags))
- continue;
- msg2 = hsc_get_first_msg(channel, &channel->free_msgs_list);
- if (!msg2)
- continue;
- clear_bit(HSC_CH_READ, &channel->flags);
- hsc_msg_len_set(msg2, 0);
- msg2->status = HSI_STATUS_COMPLETED;
- hsc_add_tail(channel, msg2, &channel->rx_msgs_queue);
- wake_up(&channel->rx_wait);
- }
- hsi_flush(msg->cl);
- ret = hsi_async_read(msg->cl, msg);
- if (ret < 0)
- hsc_break_req_destructor(msg);
- }
- static int hsc_break_request(struct hsi_client *cl)
- {
- struct hsc_client_data *cl_data = hsi_client_drvdata(cl);
- struct hsi_msg *msg;
- int ret;
- if (test_and_set_bit(HSC_RXBREAK, &cl_data->flags))
- return -EBUSY;
- msg = hsi_alloc_msg(0, GFP_KERNEL);
- if (!msg) {
- clear_bit(HSC_RXBREAK, &cl_data->flags);
- return -ENOMEM;
- }
- msg->break_frame = 1;
- msg->complete = hsc_break_received;
- msg->destructor = hsc_break_req_destructor;
- ret = hsi_async_read(cl, msg);
- if (ret < 0)
- hsc_break_req_destructor(msg);
- return ret;
- }
- static int hsc_break_send(struct hsi_client *cl)
- {
- struct hsi_msg *msg;
- int ret;
- msg = hsi_alloc_msg(0, GFP_ATOMIC);
- if (!msg)
- return -ENOMEM;
- msg->break_frame = 1;
- msg->complete = hsi_free_msg;
- msg->destructor = hsi_free_msg;
- ret = hsi_async_write(cl, msg);
- if (ret < 0)
- hsi_free_msg(msg);
- return ret;
- }
- static int hsc_rx_set(struct hsi_client *cl, struct hsc_rx_config *rxc)
- {
- struct hsi_config tmp;
- int ret;
- if ((rxc->mode != HSI_MODE_STREAM) && (rxc->mode != HSI_MODE_FRAME))
- return -EINVAL;
- if ((rxc->channels == 0) || (rxc->channels > HSC_DEVS))
- return -EINVAL;
- if (rxc->channels & (rxc->channels - 1))
- return -EINVAL;
- if ((rxc->flow != HSI_FLOW_SYNC) && (rxc->flow != HSI_FLOW_PIPE))
- return -EINVAL;
- tmp = cl->rx_cfg;
- cl->rx_cfg.mode = rxc->mode;
- cl->rx_cfg.num_hw_channels = rxc->channels;
- cl->rx_cfg.flow = rxc->flow;
- ret = hsi_setup(cl);
- if (ret < 0) {
- cl->rx_cfg = tmp;
- return ret;
- }
- if (rxc->mode == HSI_MODE_FRAME)
- hsc_break_request(cl);
- return ret;
- }
- static inline void hsc_rx_get(struct hsi_client *cl, struct hsc_rx_config *rxc)
- {
- rxc->mode = cl->rx_cfg.mode;
- rxc->channels = cl->rx_cfg.num_hw_channels;
- rxc->flow = cl->rx_cfg.flow;
- }
- static int hsc_tx_set(struct hsi_client *cl, struct hsc_tx_config *txc)
- {
- struct hsi_config tmp;
- int ret;
- if ((txc->mode != HSI_MODE_STREAM) && (txc->mode != HSI_MODE_FRAME))
- return -EINVAL;
- if ((txc->channels == 0) || (txc->channels > HSC_DEVS))
- return -EINVAL;
- if (txc->channels & (txc->channels - 1))
- return -EINVAL;
- if ((txc->arb_mode != HSI_ARB_RR) && (txc->arb_mode != HSI_ARB_PRIO))
- return -EINVAL;
- tmp = cl->tx_cfg;
- cl->tx_cfg.mode = txc->mode;
- cl->tx_cfg.num_hw_channels = txc->channels;
- cl->tx_cfg.speed = txc->speed;
- cl->tx_cfg.arb_mode = txc->arb_mode;
- ret = hsi_setup(cl);
- if (ret < 0) {
- cl->tx_cfg = tmp;
- return ret;
- }
- return ret;
- }
- static inline void hsc_tx_get(struct hsi_client *cl, struct hsc_tx_config *txc)
- {
- txc->mode = cl->tx_cfg.mode;
- txc->channels = cl->tx_cfg.num_hw_channels;
- txc->speed = cl->tx_cfg.speed;
- txc->arb_mode = cl->tx_cfg.arb_mode;
- }
- static ssize_t hsc_read(struct file *file, char __user *buf, size_t len,
- loff_t *ppos __maybe_unused)
- {
- struct hsc_channel *channel = file->private_data;
- struct hsi_msg *msg;
- ssize_t ret;
- if (len == 0)
- return 0;
- if (!IS_ALIGNED(len, sizeof(u32)))
- return -EINVAL;
- if (len > max_data_size)
- len = max_data_size;
- if (channel->ch >= channel->cl->rx_cfg.num_hw_channels)
- return -ECHRNG;
- if (test_and_set_bit(HSC_CH_READ, &channel->flags))
- return -EBUSY;
- msg = hsc_get_first_msg(channel, &channel->free_msgs_list);
- if (!msg) {
- ret = -ENOSPC;
- goto out;
- }
- hsc_msg_len_set(msg, len);
- msg->complete = hsc_rx_completed;
- msg->destructor = hsc_rx_msg_destructor;
- ret = hsi_async_read(channel->cl, msg);
- if (ret < 0) {
- hsc_add_tail(channel, msg, &channel->free_msgs_list);
- goto out;
- }
- ret = wait_event_interruptible(channel->rx_wait,
- !list_empty(&channel->rx_msgs_queue));
- if (ret < 0) {
- clear_bit(HSC_CH_READ, &channel->flags);
- hsi_flush(channel->cl);
- return -EINTR;
- }
- msg = hsc_get_first_msg(channel, &channel->rx_msgs_queue);
- if (msg) {
- if (msg->status != HSI_STATUS_ERROR) {
- ret = copy_to_user((void __user *)buf,
- sg_virt(msg->sgt.sgl), hsc_msg_len_get(msg));
- if (ret)
- ret = -EFAULT;
- else
- ret = hsc_msg_len_get(msg);
- } else {
- ret = -EIO;
- }
- hsc_add_tail(channel, msg, &channel->free_msgs_list);
- }
- out:
- clear_bit(HSC_CH_READ, &channel->flags);
- return ret;
- }
- static ssize_t hsc_write(struct file *file, const char __user *buf, size_t len,
- loff_t *ppos __maybe_unused)
- {
- struct hsc_channel *channel = file->private_data;
- struct hsi_msg *msg;
- ssize_t ret;
- if ((len == 0) || !IS_ALIGNED(len, sizeof(u32)))
- return -EINVAL;
- if (len > max_data_size)
- len = max_data_size;
- if (channel->ch >= channel->cl->tx_cfg.num_hw_channels)
- return -ECHRNG;
- if (test_and_set_bit(HSC_CH_WRITE, &channel->flags))
- return -EBUSY;
- msg = hsc_get_first_msg(channel, &channel->free_msgs_list);
- if (!msg) {
- clear_bit(HSC_CH_WRITE, &channel->flags);
- return -ENOSPC;
- }
- if (copy_from_user(sg_virt(msg->sgt.sgl), (void __user *)buf, len)) {
- ret = -EFAULT;
- goto out;
- }
- hsc_msg_len_set(msg, len);
- msg->complete = hsc_tx_completed;
- msg->destructor = hsc_tx_msg_destructor;
- ret = hsi_async_write(channel->cl, msg);
- if (ret < 0)
- goto out;
- ret = wait_event_interruptible(channel->tx_wait,
- !list_empty(&channel->tx_msgs_queue));
- if (ret < 0) {
- clear_bit(HSC_CH_WRITE, &channel->flags);
- hsi_flush(channel->cl);
- return -EINTR;
- }
- msg = hsc_get_first_msg(channel, &channel->tx_msgs_queue);
- if (msg) {
- if (msg->status == HSI_STATUS_ERROR)
- ret = -EIO;
- else
- ret = hsc_msg_len_get(msg);
- hsc_add_tail(channel, msg, &channel->free_msgs_list);
- }
- out:
- clear_bit(HSC_CH_WRITE, &channel->flags);
- return ret;
- }
- static long hsc_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
- {
- struct hsc_channel *channel = file->private_data;
- unsigned int state;
- struct hsc_rx_config rxc;
- struct hsc_tx_config txc;
- long ret = 0;
- switch (cmd) {
- case HSC_RESET:
- hsi_flush(channel->cl);
- break;
- case HSC_SET_PM:
- if (copy_from_user(&state, (void __user *)arg, sizeof(state)))
- return -EFAULT;
- if (state == HSC_PM_DISABLE) {
- if (test_and_set_bit(HSC_CH_WLINE, &channel->flags))
- return -EINVAL;
- ret = hsi_start_tx(channel->cl);
- } else if (state == HSC_PM_ENABLE) {
- if (!test_and_clear_bit(HSC_CH_WLINE, &channel->flags))
- return -EINVAL;
- ret = hsi_stop_tx(channel->cl);
- } else {
- ret = -EINVAL;
- }
- break;
- case HSC_SEND_BREAK:
- return hsc_break_send(channel->cl);
- case HSC_SET_RX:
- if (copy_from_user(&rxc, (void __user *)arg, sizeof(rxc)))
- return -EFAULT;
- return hsc_rx_set(channel->cl, &rxc);
- case HSC_GET_RX:
- hsc_rx_get(channel->cl, &rxc);
- if (copy_to_user((void __user *)arg, &rxc, sizeof(rxc)))
- return -EFAULT;
- break;
- case HSC_SET_TX:
- if (copy_from_user(&txc, (void __user *)arg, sizeof(txc)))
- return -EFAULT;
- return hsc_tx_set(channel->cl, &txc);
- case HSC_GET_TX:
- hsc_tx_get(channel->cl, &txc);
- if (copy_to_user((void __user *)arg, &txc, sizeof(txc)))
- return -EFAULT;
- break;
- default:
- return -ENOIOCTLCMD;
- }
- return ret;
- }
- static inline void __hsc_port_release(struct hsc_client_data *cl_data)
- {
- BUG_ON(cl_data->usecnt == 0);
- if (--cl_data->usecnt == 0) {
- hsi_flush(cl_data->cl);
- hsi_release_port(cl_data->cl);
- }
- }
- static int hsc_open(struct inode *inode, struct file *file)
- {
- struct hsc_client_data *cl_data;
- struct hsc_channel *channel;
- int ret = 0;
- pr_debug("open, minor = %d\n", iminor(inode));
- cl_data = container_of(inode->i_cdev, struct hsc_client_data, cdev);
- mutex_lock(&cl_data->lock);
- channel = cl_data->channels + (iminor(inode) & HSC_CH_MASK);
- if (test_and_set_bit(HSC_CH_OPEN, &channel->flags)) {
- ret = -EBUSY;
- goto out;
- }
- /*
- * Check if we have already claimed the port associated to the HSI
- * client. If not then try to claim it, else increase its refcount
- */
- if (cl_data->usecnt == 0) {
- ret = hsi_claim_port(cl_data->cl, 0);
- if (ret < 0)
- goto out;
- hsi_setup(cl_data->cl);
- }
- cl_data->usecnt++;
- ret = hsc_msgs_alloc(channel);
- if (ret < 0) {
- __hsc_port_release(cl_data);
- goto out;
- }
- file->private_data = channel;
- mutex_unlock(&cl_data->lock);
- return ret;
- out:
- mutex_unlock(&cl_data->lock);
- return ret;
- }
- static int hsc_release(struct inode *inode __maybe_unused, struct file *file)
- {
- struct hsc_channel *channel = file->private_data;
- struct hsc_client_data *cl_data = channel->cl_data;
- mutex_lock(&cl_data->lock);
- file->private_data = NULL;
- if (test_and_clear_bit(HSC_CH_WLINE, &channel->flags))
- hsi_stop_tx(channel->cl);
- __hsc_port_release(cl_data);
- hsc_reset_list(channel, &channel->rx_msgs_queue);
- hsc_reset_list(channel, &channel->tx_msgs_queue);
- hsc_reset_list(channel, &channel->free_msgs_list);
- clear_bit(HSC_CH_READ, &channel->flags);
- clear_bit(HSC_CH_WRITE, &channel->flags);
- clear_bit(HSC_CH_OPEN, &channel->flags);
- wake_up(&channel->rx_wait);
- wake_up(&channel->tx_wait);
- mutex_unlock(&cl_data->lock);
- return 0;
- }
- static const struct file_operations hsc_fops = {
- .owner = THIS_MODULE,
- .read = hsc_read,
- .write = hsc_write,
- .unlocked_ioctl = hsc_ioctl,
- .open = hsc_open,
- .release = hsc_release,
- };
- static void hsc_channel_init(struct hsc_channel *channel)
- {
- init_waitqueue_head(&channel->rx_wait);
- init_waitqueue_head(&channel->tx_wait);
- spin_lock_init(&channel->lock);
- INIT_LIST_HEAD(&channel->free_msgs_list);
- INIT_LIST_HEAD(&channel->rx_msgs_queue);
- INIT_LIST_HEAD(&channel->tx_msgs_queue);
- }
- static int hsc_probe(struct device *dev)
- {
- const char devname[] = "hsi_char";
- struct hsc_client_data *cl_data;
- struct hsc_channel *channel;
- struct hsi_client *cl = to_hsi_client(dev);
- unsigned int hsc_baseminor;
- dev_t hsc_dev;
- int ret;
- int i;
- cl_data = kzalloc(sizeof(*cl_data), GFP_KERNEL);
- if (!cl_data) {
- dev_err(dev, "Could not allocate hsc_client_data\n");
- return -ENOMEM;
- }
- hsc_baseminor = HSC_BASEMINOR(hsi_id(cl), hsi_port_id(cl));
- if (!hsc_major) {
- ret = alloc_chrdev_region(&hsc_dev, hsc_baseminor,
- HSC_DEVS, devname);
- if (ret == 0)
- hsc_major = MAJOR(hsc_dev);
- } else {
- hsc_dev = MKDEV(hsc_major, hsc_baseminor);
- ret = register_chrdev_region(hsc_dev, HSC_DEVS, devname);
- }
- if (ret < 0) {
- dev_err(dev, "Device %s allocation failed %d\n",
- hsc_major ? "minor" : "major", ret);
- goto out1;
- }
- mutex_init(&cl_data->lock);
- hsi_client_set_drvdata(cl, cl_data);
- cdev_init(&cl_data->cdev, &hsc_fops);
- cl_data->cdev.owner = THIS_MODULE;
- cl_data->cl = cl;
- for (i = 0, channel = cl_data->channels; i < HSC_DEVS; i++, channel++) {
- hsc_channel_init(channel);
- channel->ch = i;
- channel->cl = cl;
- channel->cl_data = cl_data;
- }
- /* 1 hsi client -> N char devices (one for each channel) */
- ret = cdev_add(&cl_data->cdev, hsc_dev, HSC_DEVS);
- if (ret) {
- dev_err(dev, "Could not add char device %d\n", ret);
- goto out2;
- }
- return 0;
- out2:
- unregister_chrdev_region(hsc_dev, HSC_DEVS);
- out1:
- kfree(cl_data);
- return ret;
- }
- static int hsc_remove(struct device *dev)
- {
- struct hsi_client *cl = to_hsi_client(dev);
- struct hsc_client_data *cl_data = hsi_client_drvdata(cl);
- dev_t hsc_dev = cl_data->cdev.dev;
- cdev_del(&cl_data->cdev);
- unregister_chrdev_region(hsc_dev, HSC_DEVS);
- hsi_client_set_drvdata(cl, NULL);
- kfree(cl_data);
- return 0;
- }
- static struct hsi_client_driver hsc_driver = {
- .driver = {
- .name = "hsi_char",
- .owner = THIS_MODULE,
- .probe = hsc_probe,
- .remove = hsc_remove,
- },
- };
- static int __init hsc_init(void)
- {
- int ret;
- if ((max_data_size < 4) || (max_data_size > 0x10000) ||
- (max_data_size & (max_data_size - 1))) {
- pr_err("Invalid max read/write data size");
- return -EINVAL;
- }
- ret = hsi_register_client_driver(&hsc_driver);
- if (ret) {
- pr_err("Error while registering HSI/SSI driver %d", ret);
- return ret;
- }
- pr_info("HSI/SSI char device loaded\n");
- return 0;
- }
- module_init(hsc_init);
- static void __exit hsc_exit(void)
- {
- hsi_unregister_client_driver(&hsc_driver);
- pr_info("HSI char device removed\n");
- }
- module_exit(hsc_exit);
- MODULE_AUTHOR("Andras Domokos <andras.domokos@nokia.com>");
- MODULE_ALIAS("hsi:hsi_char");
- MODULE_DESCRIPTION("HSI character device");
- MODULE_LICENSE("GPL v2");
|