1*2b27bdccSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
24e69fc22SAndras Domokos /*
34e69fc22SAndras Domokos * HSI character device driver, implements the character device
44e69fc22SAndras Domokos * interface.
54e69fc22SAndras Domokos *
64e69fc22SAndras Domokos * Copyright (C) 2010 Nokia Corporation. All rights reserved.
74e69fc22SAndras Domokos *
84e69fc22SAndras Domokos * Contact: Andras Domokos <andras.domokos@nokia.com>
94e69fc22SAndras Domokos */
104e69fc22SAndras Domokos
114e69fc22SAndras Domokos #include <linux/errno.h>
124e69fc22SAndras Domokos #include <linux/types.h>
134e69fc22SAndras Domokos #include <linux/atomic.h>
144e69fc22SAndras Domokos #include <linux/kernel.h>
154e69fc22SAndras Domokos #include <linux/init.h>
164e69fc22SAndras Domokos #include <linux/module.h>
174e69fc22SAndras Domokos #include <linux/mutex.h>
184e69fc22SAndras Domokos #include <linux/list.h>
194e69fc22SAndras Domokos #include <linux/slab.h>
204e69fc22SAndras Domokos #include <linux/kmemleak.h>
214e69fc22SAndras Domokos #include <linux/ioctl.h>
224e69fc22SAndras Domokos #include <linux/wait.h>
234e69fc22SAndras Domokos #include <linux/fs.h>
244e69fc22SAndras Domokos #include <linux/sched.h>
254e69fc22SAndras Domokos #include <linux/device.h>
264e69fc22SAndras Domokos #include <linux/cdev.h>
274e69fc22SAndras Domokos #include <linux/uaccess.h>
284e69fc22SAndras Domokos #include <linux/scatterlist.h>
294e69fc22SAndras Domokos #include <linux/stat.h>
304e69fc22SAndras Domokos #include <linux/hsi/hsi.h>
314e69fc22SAndras Domokos #include <linux/hsi/hsi_char.h>
324e69fc22SAndras Domokos
334e69fc22SAndras Domokos #define HSC_DEVS 16 /* Num of channels */
344e69fc22SAndras Domokos #define HSC_MSGS 4
354e69fc22SAndras Domokos
364e69fc22SAndras Domokos #define HSC_RXBREAK 0
374e69fc22SAndras Domokos
384e69fc22SAndras Domokos #define HSC_ID_BITS 6
394e69fc22SAndras Domokos #define HSC_PORT_ID_BITS 4
404e69fc22SAndras Domokos #define HSC_ID_MASK 3
414e69fc22SAndras Domokos #define HSC_PORT_ID_MASK 3
424e69fc22SAndras Domokos #define HSC_CH_MASK 0xf
434e69fc22SAndras Domokos
444e69fc22SAndras Domokos /*
454e69fc22SAndras Domokos * We support up to 4 controllers that can have up to 4
464e69fc22SAndras Domokos * ports, which should currently be more than enough.
474e69fc22SAndras Domokos */
484e69fc22SAndras Domokos #define HSC_BASEMINOR(id, port_id) \
494e69fc22SAndras Domokos ((((id) & HSC_ID_MASK) << HSC_ID_BITS) | \
504e69fc22SAndras Domokos (((port_id) & HSC_PORT_ID_MASK) << HSC_PORT_ID_BITS))
514e69fc22SAndras Domokos
524e69fc22SAndras Domokos enum {
534e69fc22SAndras Domokos HSC_CH_OPEN,
544e69fc22SAndras Domokos HSC_CH_READ,
554e69fc22SAndras Domokos HSC_CH_WRITE,
564e69fc22SAndras Domokos HSC_CH_WLINE,
574e69fc22SAndras Domokos };
584e69fc22SAndras Domokos
594e69fc22SAndras Domokos enum {
604e69fc22SAndras Domokos HSC_RX,
614e69fc22SAndras Domokos HSC_TX,
624e69fc22SAndras Domokos };
634e69fc22SAndras Domokos
644e69fc22SAndras Domokos struct hsc_client_data;
654e69fc22SAndras Domokos /**
664e69fc22SAndras Domokos * struct hsc_channel - hsi_char internal channel data
674e69fc22SAndras Domokos * @ch: channel number
684e69fc22SAndras Domokos * @flags: Keeps state of the channel (open/close, reading, writing)
694e69fc22SAndras Domokos * @free_msgs_list: List of free HSI messages/requests
704e69fc22SAndras Domokos * @rx_msgs_queue: List of pending RX requests
714e69fc22SAndras Domokos * @tx_msgs_queue: List of pending TX requests
724e69fc22SAndras Domokos * @lock: Serialize access to the lists
734e69fc22SAndras Domokos * @cl: reference to the associated hsi_client
744e69fc22SAndras Domokos * @cl_data: reference to the client data that this channels belongs to
754e69fc22SAndras Domokos * @rx_wait: RX requests wait queue
764e69fc22SAndras Domokos * @tx_wait: TX requests wait queue
774e69fc22SAndras Domokos */
784e69fc22SAndras Domokos struct hsc_channel {
794e69fc22SAndras Domokos unsigned int ch;
804e69fc22SAndras Domokos unsigned long flags;
814e69fc22SAndras Domokos struct list_head free_msgs_list;
824e69fc22SAndras Domokos struct list_head rx_msgs_queue;
834e69fc22SAndras Domokos struct list_head tx_msgs_queue;
844e69fc22SAndras Domokos spinlock_t lock;
854e69fc22SAndras Domokos struct hsi_client *cl;
864e69fc22SAndras Domokos struct hsc_client_data *cl_data;
874e69fc22SAndras Domokos wait_queue_head_t rx_wait;
884e69fc22SAndras Domokos wait_queue_head_t tx_wait;
894e69fc22SAndras Domokos };
904e69fc22SAndras Domokos
914e69fc22SAndras Domokos /**
924e69fc22SAndras Domokos * struct hsc_client_data - hsi_char internal client data
934e69fc22SAndras Domokos * @cdev: Characther device associated to the hsi_client
944e69fc22SAndras Domokos * @lock: Lock to serialize open/close access
954e69fc22SAndras Domokos * @flags: Keeps track of port state (rx hwbreak armed)
964e69fc22SAndras Domokos * @usecnt: Use count for claiming the HSI port (mutex protected)
974e69fc22SAndras Domokos * @cl: Referece to the HSI client
984e69fc22SAndras Domokos * @channels: Array of channels accessible by the client
994e69fc22SAndras Domokos */
1004e69fc22SAndras Domokos struct hsc_client_data {
1014e69fc22SAndras Domokos struct cdev cdev;
1024e69fc22SAndras Domokos struct mutex lock;
1034e69fc22SAndras Domokos unsigned long flags;
1044e69fc22SAndras Domokos unsigned int usecnt;
1054e69fc22SAndras Domokos struct hsi_client *cl;
1064e69fc22SAndras Domokos struct hsc_channel channels[HSC_DEVS];
1074e69fc22SAndras Domokos };
1084e69fc22SAndras Domokos
1094e69fc22SAndras Domokos /* Stores the major number dynamically allocated for hsi_char */
1104e69fc22SAndras Domokos static unsigned int hsc_major;
1114e69fc22SAndras Domokos /* Maximum buffer size that hsi_char will accept from userspace */
1124e69fc22SAndras Domokos static unsigned int max_data_size = 0x1000;
113fdadb6e9SCarlos Chinea module_param(max_data_size, uint, 0);
1144e69fc22SAndras Domokos MODULE_PARM_DESC(max_data_size, "max read/write data size [4,8..65536] (^2)");
1154e69fc22SAndras Domokos
hsc_add_tail(struct hsc_channel * channel,struct hsi_msg * msg,struct list_head * queue)1164e69fc22SAndras Domokos static void hsc_add_tail(struct hsc_channel *channel, struct hsi_msg *msg,
1174e69fc22SAndras Domokos struct list_head *queue)
1184e69fc22SAndras Domokos {
1194e69fc22SAndras Domokos unsigned long flags;
1204e69fc22SAndras Domokos
1214e69fc22SAndras Domokos spin_lock_irqsave(&channel->lock, flags);
1224e69fc22SAndras Domokos list_add_tail(&msg->link, queue);
1234e69fc22SAndras Domokos spin_unlock_irqrestore(&channel->lock, flags);
1244e69fc22SAndras Domokos }
1254e69fc22SAndras Domokos
hsc_get_first_msg(struct hsc_channel * channel,struct list_head * queue)1264e69fc22SAndras Domokos static struct hsi_msg *hsc_get_first_msg(struct hsc_channel *channel,
1274e69fc22SAndras Domokos struct list_head *queue)
1284e69fc22SAndras Domokos {
1294e69fc22SAndras Domokos struct hsi_msg *msg = NULL;
1304e69fc22SAndras Domokos unsigned long flags;
1314e69fc22SAndras Domokos
1324e69fc22SAndras Domokos spin_lock_irqsave(&channel->lock, flags);
1334e69fc22SAndras Domokos
1344e69fc22SAndras Domokos if (list_empty(queue))
1354e69fc22SAndras Domokos goto out;
1364e69fc22SAndras Domokos
1374e69fc22SAndras Domokos msg = list_first_entry(queue, struct hsi_msg, link);
1384e69fc22SAndras Domokos list_del(&msg->link);
1394e69fc22SAndras Domokos out:
1404e69fc22SAndras Domokos spin_unlock_irqrestore(&channel->lock, flags);
1414e69fc22SAndras Domokos
1424e69fc22SAndras Domokos return msg;
1434e69fc22SAndras Domokos }
1444e69fc22SAndras Domokos
hsc_msg_free(struct hsi_msg * msg)1454e69fc22SAndras Domokos static inline void hsc_msg_free(struct hsi_msg *msg)
1464e69fc22SAndras Domokos {
1474e69fc22SAndras Domokos kfree(sg_virt(msg->sgt.sgl));
1484e69fc22SAndras Domokos hsi_free_msg(msg);
1494e69fc22SAndras Domokos }
1504e69fc22SAndras Domokos
hsc_free_list(struct list_head * list)1514e69fc22SAndras Domokos static void hsc_free_list(struct list_head *list)
1524e69fc22SAndras Domokos {
1534e69fc22SAndras Domokos struct hsi_msg *msg, *tmp;
1544e69fc22SAndras Domokos
1554e69fc22SAndras Domokos list_for_each_entry_safe(msg, tmp, list, link) {
1564e69fc22SAndras Domokos list_del(&msg->link);
1574e69fc22SAndras Domokos hsc_msg_free(msg);
1584e69fc22SAndras Domokos }
1594e69fc22SAndras Domokos }
1604e69fc22SAndras Domokos
hsc_reset_list(struct hsc_channel * channel,struct list_head * l)1614e69fc22SAndras Domokos static void hsc_reset_list(struct hsc_channel *channel, struct list_head *l)
1624e69fc22SAndras Domokos {
1634e69fc22SAndras Domokos unsigned long flags;
1644e69fc22SAndras Domokos LIST_HEAD(list);
1654e69fc22SAndras Domokos
1664e69fc22SAndras Domokos spin_lock_irqsave(&channel->lock, flags);
1674e69fc22SAndras Domokos list_splice_init(l, &list);
1684e69fc22SAndras Domokos spin_unlock_irqrestore(&channel->lock, flags);
1694e69fc22SAndras Domokos
1704e69fc22SAndras Domokos hsc_free_list(&list);
1714e69fc22SAndras Domokos }
1724e69fc22SAndras Domokos
hsc_msg_alloc(unsigned int alloc_size)1734e69fc22SAndras Domokos static inline struct hsi_msg *hsc_msg_alloc(unsigned int alloc_size)
1744e69fc22SAndras Domokos {
1754e69fc22SAndras Domokos struct hsi_msg *msg;
1764e69fc22SAndras Domokos void *buf;
1774e69fc22SAndras Domokos
1784e69fc22SAndras Domokos msg = hsi_alloc_msg(1, GFP_KERNEL);
1794e69fc22SAndras Domokos if (!msg)
1804e69fc22SAndras Domokos goto out;
1814e69fc22SAndras Domokos buf = kmalloc(alloc_size, GFP_KERNEL);
1824e69fc22SAndras Domokos if (!buf) {
1834e69fc22SAndras Domokos hsi_free_msg(msg);
1844e69fc22SAndras Domokos goto out;
1854e69fc22SAndras Domokos }
1864e69fc22SAndras Domokos sg_init_one(msg->sgt.sgl, buf, alloc_size);
1874e69fc22SAndras Domokos /* Ignore false positive, due to sg pointer handling */
1884e69fc22SAndras Domokos kmemleak_ignore(buf);
1894e69fc22SAndras Domokos
1904e69fc22SAndras Domokos return msg;
1914e69fc22SAndras Domokos out:
1924e69fc22SAndras Domokos return NULL;
1934e69fc22SAndras Domokos }
1944e69fc22SAndras Domokos
hsc_msgs_alloc(struct hsc_channel * channel)1954e69fc22SAndras Domokos static inline int hsc_msgs_alloc(struct hsc_channel *channel)
1964e69fc22SAndras Domokos {
1974e69fc22SAndras Domokos struct hsi_msg *msg;
1984e69fc22SAndras Domokos int i;
1994e69fc22SAndras Domokos
2004e69fc22SAndras Domokos for (i = 0; i < HSC_MSGS; i++) {
2014e69fc22SAndras Domokos msg = hsc_msg_alloc(max_data_size);
2024e69fc22SAndras Domokos if (!msg)
2034e69fc22SAndras Domokos goto out;
2044e69fc22SAndras Domokos msg->channel = channel->ch;
2054e69fc22SAndras Domokos list_add_tail(&msg->link, &channel->free_msgs_list);
2064e69fc22SAndras Domokos }
2074e69fc22SAndras Domokos
2084e69fc22SAndras Domokos return 0;
2094e69fc22SAndras Domokos out:
2104e69fc22SAndras Domokos hsc_free_list(&channel->free_msgs_list);
2114e69fc22SAndras Domokos
2124e69fc22SAndras Domokos return -ENOMEM;
2134e69fc22SAndras Domokos }
2144e69fc22SAndras Domokos
hsc_msg_len_get(struct hsi_msg * msg)2154e69fc22SAndras Domokos static inline unsigned int hsc_msg_len_get(struct hsi_msg *msg)
2164e69fc22SAndras Domokos {
2174e69fc22SAndras Domokos return msg->sgt.sgl->length;
2184e69fc22SAndras Domokos }
2194e69fc22SAndras Domokos
hsc_msg_len_set(struct hsi_msg * msg,unsigned int len)2204e69fc22SAndras Domokos static inline void hsc_msg_len_set(struct hsi_msg *msg, unsigned int len)
2214e69fc22SAndras Domokos {
2224e69fc22SAndras Domokos msg->sgt.sgl->length = len;
2234e69fc22SAndras Domokos }
2244e69fc22SAndras Domokos
hsc_rx_completed(struct hsi_msg * msg)2254e69fc22SAndras Domokos static void hsc_rx_completed(struct hsi_msg *msg)
2264e69fc22SAndras Domokos {
2274e69fc22SAndras Domokos struct hsc_client_data *cl_data = hsi_client_drvdata(msg->cl);
2284e69fc22SAndras Domokos struct hsc_channel *channel = cl_data->channels + msg->channel;
2294e69fc22SAndras Domokos
2304e69fc22SAndras Domokos if (test_bit(HSC_CH_READ, &channel->flags)) {
2314e69fc22SAndras Domokos hsc_add_tail(channel, msg, &channel->rx_msgs_queue);
2324e69fc22SAndras Domokos wake_up(&channel->rx_wait);
2334e69fc22SAndras Domokos } else {
2344e69fc22SAndras Domokos hsc_add_tail(channel, msg, &channel->free_msgs_list);
2354e69fc22SAndras Domokos }
2364e69fc22SAndras Domokos }
2374e69fc22SAndras Domokos
hsc_rx_msg_destructor(struct hsi_msg * msg)2384e69fc22SAndras Domokos static void hsc_rx_msg_destructor(struct hsi_msg *msg)
2394e69fc22SAndras Domokos {
2404e69fc22SAndras Domokos msg->status = HSI_STATUS_ERROR;
2414e69fc22SAndras Domokos hsc_msg_len_set(msg, 0);
2424e69fc22SAndras Domokos hsc_rx_completed(msg);
2434e69fc22SAndras Domokos }
2444e69fc22SAndras Domokos
hsc_tx_completed(struct hsi_msg * msg)2454e69fc22SAndras Domokos static void hsc_tx_completed(struct hsi_msg *msg)
2464e69fc22SAndras Domokos {
2474e69fc22SAndras Domokos struct hsc_client_data *cl_data = hsi_client_drvdata(msg->cl);
2484e69fc22SAndras Domokos struct hsc_channel *channel = cl_data->channels + msg->channel;
2494e69fc22SAndras Domokos
2504e69fc22SAndras Domokos if (test_bit(HSC_CH_WRITE, &channel->flags)) {
2514e69fc22SAndras Domokos hsc_add_tail(channel, msg, &channel->tx_msgs_queue);
2524e69fc22SAndras Domokos wake_up(&channel->tx_wait);
2534e69fc22SAndras Domokos } else {
2544e69fc22SAndras Domokos hsc_add_tail(channel, msg, &channel->free_msgs_list);
2554e69fc22SAndras Domokos }
2564e69fc22SAndras Domokos }
2574e69fc22SAndras Domokos
hsc_tx_msg_destructor(struct hsi_msg * msg)2584e69fc22SAndras Domokos static void hsc_tx_msg_destructor(struct hsi_msg *msg)
2594e69fc22SAndras Domokos {
2604e69fc22SAndras Domokos msg->status = HSI_STATUS_ERROR;
2614e69fc22SAndras Domokos hsc_msg_len_set(msg, 0);
2624e69fc22SAndras Domokos hsc_tx_completed(msg);
2634e69fc22SAndras Domokos }
2644e69fc22SAndras Domokos
hsc_break_req_destructor(struct hsi_msg * msg)2654e69fc22SAndras Domokos static void hsc_break_req_destructor(struct hsi_msg *msg)
2664e69fc22SAndras Domokos {
2674e69fc22SAndras Domokos struct hsc_client_data *cl_data = hsi_client_drvdata(msg->cl);
2684e69fc22SAndras Domokos
2694e69fc22SAndras Domokos hsi_free_msg(msg);
2704e69fc22SAndras Domokos clear_bit(HSC_RXBREAK, &cl_data->flags);
2714e69fc22SAndras Domokos }
2724e69fc22SAndras Domokos
hsc_break_received(struct hsi_msg * msg)2734e69fc22SAndras Domokos static void hsc_break_received(struct hsi_msg *msg)
2744e69fc22SAndras Domokos {
2754e69fc22SAndras Domokos struct hsc_client_data *cl_data = hsi_client_drvdata(msg->cl);
2764e69fc22SAndras Domokos struct hsc_channel *channel = cl_data->channels;
2774e69fc22SAndras Domokos int i, ret;
2784e69fc22SAndras Domokos
2794e69fc22SAndras Domokos /* Broadcast HWBREAK on all channels */
2804e69fc22SAndras Domokos for (i = 0; i < HSC_DEVS; i++, channel++) {
2814e69fc22SAndras Domokos struct hsi_msg *msg2;
2824e69fc22SAndras Domokos
2834e69fc22SAndras Domokos if (!test_bit(HSC_CH_READ, &channel->flags))
2844e69fc22SAndras Domokos continue;
2854e69fc22SAndras Domokos msg2 = hsc_get_first_msg(channel, &channel->free_msgs_list);
2864e69fc22SAndras Domokos if (!msg2)
2874e69fc22SAndras Domokos continue;
2884e69fc22SAndras Domokos clear_bit(HSC_CH_READ, &channel->flags);
2894e69fc22SAndras Domokos hsc_msg_len_set(msg2, 0);
2904e69fc22SAndras Domokos msg2->status = HSI_STATUS_COMPLETED;
2914e69fc22SAndras Domokos hsc_add_tail(channel, msg2, &channel->rx_msgs_queue);
2924e69fc22SAndras Domokos wake_up(&channel->rx_wait);
2934e69fc22SAndras Domokos }
2944e69fc22SAndras Domokos hsi_flush(msg->cl);
2954e69fc22SAndras Domokos ret = hsi_async_read(msg->cl, msg);
2964e69fc22SAndras Domokos if (ret < 0)
2974e69fc22SAndras Domokos hsc_break_req_destructor(msg);
2984e69fc22SAndras Domokos }
2994e69fc22SAndras Domokos
hsc_break_request(struct hsi_client * cl)3004e69fc22SAndras Domokos static int hsc_break_request(struct hsi_client *cl)
3014e69fc22SAndras Domokos {
3024e69fc22SAndras Domokos struct hsc_client_data *cl_data = hsi_client_drvdata(cl);
3034e69fc22SAndras Domokos struct hsi_msg *msg;
3044e69fc22SAndras Domokos int ret;
3054e69fc22SAndras Domokos
3064e69fc22SAndras Domokos if (test_and_set_bit(HSC_RXBREAK, &cl_data->flags))
3074e69fc22SAndras Domokos return -EBUSY;
3084e69fc22SAndras Domokos
3094e69fc22SAndras Domokos msg = hsi_alloc_msg(0, GFP_KERNEL);
3104e69fc22SAndras Domokos if (!msg) {
3114e69fc22SAndras Domokos clear_bit(HSC_RXBREAK, &cl_data->flags);
3124e69fc22SAndras Domokos return -ENOMEM;
3134e69fc22SAndras Domokos }
3144e69fc22SAndras Domokos msg->break_frame = 1;
3154e69fc22SAndras Domokos msg->complete = hsc_break_received;
3164e69fc22SAndras Domokos msg->destructor = hsc_break_req_destructor;
3174e69fc22SAndras Domokos ret = hsi_async_read(cl, msg);
3184e69fc22SAndras Domokos if (ret < 0)
3194e69fc22SAndras Domokos hsc_break_req_destructor(msg);
3204e69fc22SAndras Domokos
3214e69fc22SAndras Domokos return ret;
3224e69fc22SAndras Domokos }
3234e69fc22SAndras Domokos
hsc_break_send(struct hsi_client * cl)3244e69fc22SAndras Domokos static int hsc_break_send(struct hsi_client *cl)
3254e69fc22SAndras Domokos {
3264e69fc22SAndras Domokos struct hsi_msg *msg;
3274e69fc22SAndras Domokos int ret;
3284e69fc22SAndras Domokos
3294e69fc22SAndras Domokos msg = hsi_alloc_msg(0, GFP_ATOMIC);
3304e69fc22SAndras Domokos if (!msg)
3314e69fc22SAndras Domokos return -ENOMEM;
3324e69fc22SAndras Domokos msg->break_frame = 1;
3334e69fc22SAndras Domokos msg->complete = hsi_free_msg;
3344e69fc22SAndras Domokos msg->destructor = hsi_free_msg;
3354e69fc22SAndras Domokos ret = hsi_async_write(cl, msg);
3364e69fc22SAndras Domokos if (ret < 0)
3374e69fc22SAndras Domokos hsi_free_msg(msg);
3384e69fc22SAndras Domokos
3394e69fc22SAndras Domokos return ret;
3404e69fc22SAndras Domokos }
3414e69fc22SAndras Domokos
hsc_rx_set(struct hsi_client * cl,struct hsc_rx_config * rxc)3424e69fc22SAndras Domokos static int hsc_rx_set(struct hsi_client *cl, struct hsc_rx_config *rxc)
3434e69fc22SAndras Domokos {
3444e69fc22SAndras Domokos struct hsi_config tmp;
3454e69fc22SAndras Domokos int ret;
3464e69fc22SAndras Domokos
3474e69fc22SAndras Domokos if ((rxc->mode != HSI_MODE_STREAM) && (rxc->mode != HSI_MODE_FRAME))
3484e69fc22SAndras Domokos return -EINVAL;
3494e69fc22SAndras Domokos if ((rxc->channels == 0) || (rxc->channels > HSC_DEVS))
3504e69fc22SAndras Domokos return -EINVAL;
3514e69fc22SAndras Domokos if (rxc->channels & (rxc->channels - 1))
3524e69fc22SAndras Domokos return -EINVAL;
3534e69fc22SAndras Domokos if ((rxc->flow != HSI_FLOW_SYNC) && (rxc->flow != HSI_FLOW_PIPE))
3544e69fc22SAndras Domokos return -EINVAL;
3554e69fc22SAndras Domokos tmp = cl->rx_cfg;
3564e69fc22SAndras Domokos cl->rx_cfg.mode = rxc->mode;
357a088cf16SSebastian Reichel cl->rx_cfg.num_hw_channels = rxc->channels;
3584e69fc22SAndras Domokos cl->rx_cfg.flow = rxc->flow;
3594e69fc22SAndras Domokos ret = hsi_setup(cl);
3604e69fc22SAndras Domokos if (ret < 0) {
3614e69fc22SAndras Domokos cl->rx_cfg = tmp;
3624e69fc22SAndras Domokos return ret;
3634e69fc22SAndras Domokos }
3644e69fc22SAndras Domokos if (rxc->mode == HSI_MODE_FRAME)
3654e69fc22SAndras Domokos hsc_break_request(cl);
3664e69fc22SAndras Domokos
3674e69fc22SAndras Domokos return ret;
3684e69fc22SAndras Domokos }
3694e69fc22SAndras Domokos
hsc_rx_get(struct hsi_client * cl,struct hsc_rx_config * rxc)3704e69fc22SAndras Domokos static inline void hsc_rx_get(struct hsi_client *cl, struct hsc_rx_config *rxc)
3714e69fc22SAndras Domokos {
3724e69fc22SAndras Domokos rxc->mode = cl->rx_cfg.mode;
373a088cf16SSebastian Reichel rxc->channels = cl->rx_cfg.num_hw_channels;
3744e69fc22SAndras Domokos rxc->flow = cl->rx_cfg.flow;
3754e69fc22SAndras Domokos }
3764e69fc22SAndras Domokos
hsc_tx_set(struct hsi_client * cl,struct hsc_tx_config * txc)3774e69fc22SAndras Domokos static int hsc_tx_set(struct hsi_client *cl, struct hsc_tx_config *txc)
3784e69fc22SAndras Domokos {
3794e69fc22SAndras Domokos struct hsi_config tmp;
3804e69fc22SAndras Domokos int ret;
3814e69fc22SAndras Domokos
3824e69fc22SAndras Domokos if ((txc->mode != HSI_MODE_STREAM) && (txc->mode != HSI_MODE_FRAME))
3834e69fc22SAndras Domokos return -EINVAL;
3844e69fc22SAndras Domokos if ((txc->channels == 0) || (txc->channels > HSC_DEVS))
3854e69fc22SAndras Domokos return -EINVAL;
3864e69fc22SAndras Domokos if (txc->channels & (txc->channels - 1))
3874e69fc22SAndras Domokos return -EINVAL;
3884e69fc22SAndras Domokos if ((txc->arb_mode != HSI_ARB_RR) && (txc->arb_mode != HSI_ARB_PRIO))
3894e69fc22SAndras Domokos return -EINVAL;
3904e69fc22SAndras Domokos tmp = cl->tx_cfg;
3914e69fc22SAndras Domokos cl->tx_cfg.mode = txc->mode;
392a088cf16SSebastian Reichel cl->tx_cfg.num_hw_channels = txc->channels;
3934e69fc22SAndras Domokos cl->tx_cfg.speed = txc->speed;
3944e69fc22SAndras Domokos cl->tx_cfg.arb_mode = txc->arb_mode;
3954e69fc22SAndras Domokos ret = hsi_setup(cl);
3964e69fc22SAndras Domokos if (ret < 0) {
3974e69fc22SAndras Domokos cl->tx_cfg = tmp;
3984e69fc22SAndras Domokos return ret;
3994e69fc22SAndras Domokos }
4004e69fc22SAndras Domokos
4014e69fc22SAndras Domokos return ret;
4024e69fc22SAndras Domokos }
4034e69fc22SAndras Domokos
hsc_tx_get(struct hsi_client * cl,struct hsc_tx_config * txc)4044e69fc22SAndras Domokos static inline void hsc_tx_get(struct hsi_client *cl, struct hsc_tx_config *txc)
4054e69fc22SAndras Domokos {
4064e69fc22SAndras Domokos txc->mode = cl->tx_cfg.mode;
407a088cf16SSebastian Reichel txc->channels = cl->tx_cfg.num_hw_channels;
4084e69fc22SAndras Domokos txc->speed = cl->tx_cfg.speed;
4094e69fc22SAndras Domokos txc->arb_mode = cl->tx_cfg.arb_mode;
4104e69fc22SAndras Domokos }
4114e69fc22SAndras Domokos
hsc_read(struct file * file,char __user * buf,size_t len,loff_t * ppos __maybe_unused)4124e69fc22SAndras Domokos static ssize_t hsc_read(struct file *file, char __user *buf, size_t len,
4134e69fc22SAndras Domokos loff_t *ppos __maybe_unused)
4144e69fc22SAndras Domokos {
4154e69fc22SAndras Domokos struct hsc_channel *channel = file->private_data;
4164e69fc22SAndras Domokos struct hsi_msg *msg;
4174e69fc22SAndras Domokos ssize_t ret;
4184e69fc22SAndras Domokos
4194e69fc22SAndras Domokos if (len == 0)
4204e69fc22SAndras Domokos return 0;
4214e69fc22SAndras Domokos if (!IS_ALIGNED(len, sizeof(u32)))
4224e69fc22SAndras Domokos return -EINVAL;
4234e69fc22SAndras Domokos if (len > max_data_size)
4244e69fc22SAndras Domokos len = max_data_size;
425a088cf16SSebastian Reichel if (channel->ch >= channel->cl->rx_cfg.num_hw_channels)
4264e69fc22SAndras Domokos return -ECHRNG;
4274e69fc22SAndras Domokos if (test_and_set_bit(HSC_CH_READ, &channel->flags))
4284e69fc22SAndras Domokos return -EBUSY;
4294e69fc22SAndras Domokos msg = hsc_get_first_msg(channel, &channel->free_msgs_list);
4304e69fc22SAndras Domokos if (!msg) {
4314e69fc22SAndras Domokos ret = -ENOSPC;
4324e69fc22SAndras Domokos goto out;
4334e69fc22SAndras Domokos }
4344e69fc22SAndras Domokos hsc_msg_len_set(msg, len);
4354e69fc22SAndras Domokos msg->complete = hsc_rx_completed;
4364e69fc22SAndras Domokos msg->destructor = hsc_rx_msg_destructor;
4374e69fc22SAndras Domokos ret = hsi_async_read(channel->cl, msg);
4384e69fc22SAndras Domokos if (ret < 0) {
4394e69fc22SAndras Domokos hsc_add_tail(channel, msg, &channel->free_msgs_list);
4404e69fc22SAndras Domokos goto out;
4414e69fc22SAndras Domokos }
4424e69fc22SAndras Domokos
4434e69fc22SAndras Domokos ret = wait_event_interruptible(channel->rx_wait,
4444e69fc22SAndras Domokos !list_empty(&channel->rx_msgs_queue));
4454e69fc22SAndras Domokos if (ret < 0) {
4464e69fc22SAndras Domokos clear_bit(HSC_CH_READ, &channel->flags);
4474e69fc22SAndras Domokos hsi_flush(channel->cl);
4484e69fc22SAndras Domokos return -EINTR;
4494e69fc22SAndras Domokos }
4504e69fc22SAndras Domokos
4514e69fc22SAndras Domokos msg = hsc_get_first_msg(channel, &channel->rx_msgs_queue);
4524e69fc22SAndras Domokos if (msg) {
4534e69fc22SAndras Domokos if (msg->status != HSI_STATUS_ERROR) {
4544e69fc22SAndras Domokos ret = copy_to_user((void __user *)buf,
4554e69fc22SAndras Domokos sg_virt(msg->sgt.sgl), hsc_msg_len_get(msg));
4564e69fc22SAndras Domokos if (ret)
4574e69fc22SAndras Domokos ret = -EFAULT;
4584e69fc22SAndras Domokos else
4594e69fc22SAndras Domokos ret = hsc_msg_len_get(msg);
4604e69fc22SAndras Domokos } else {
4614e69fc22SAndras Domokos ret = -EIO;
4624e69fc22SAndras Domokos }
4634e69fc22SAndras Domokos hsc_add_tail(channel, msg, &channel->free_msgs_list);
4644e69fc22SAndras Domokos }
4654e69fc22SAndras Domokos out:
4664e69fc22SAndras Domokos clear_bit(HSC_CH_READ, &channel->flags);
4674e69fc22SAndras Domokos
4684e69fc22SAndras Domokos return ret;
4694e69fc22SAndras Domokos }
4704e69fc22SAndras Domokos
hsc_write(struct file * file,const char __user * buf,size_t len,loff_t * ppos __maybe_unused)4714e69fc22SAndras Domokos static ssize_t hsc_write(struct file *file, const char __user *buf, size_t len,
4724e69fc22SAndras Domokos loff_t *ppos __maybe_unused)
4734e69fc22SAndras Domokos {
4744e69fc22SAndras Domokos struct hsc_channel *channel = file->private_data;
4754e69fc22SAndras Domokos struct hsi_msg *msg;
4764e69fc22SAndras Domokos ssize_t ret;
4774e69fc22SAndras Domokos
4784e69fc22SAndras Domokos if ((len == 0) || !IS_ALIGNED(len, sizeof(u32)))
4794e69fc22SAndras Domokos return -EINVAL;
4804e69fc22SAndras Domokos if (len > max_data_size)
4814e69fc22SAndras Domokos len = max_data_size;
482a088cf16SSebastian Reichel if (channel->ch >= channel->cl->tx_cfg.num_hw_channels)
4834e69fc22SAndras Domokos return -ECHRNG;
4844e69fc22SAndras Domokos if (test_and_set_bit(HSC_CH_WRITE, &channel->flags))
4854e69fc22SAndras Domokos return -EBUSY;
4864e69fc22SAndras Domokos msg = hsc_get_first_msg(channel, &channel->free_msgs_list);
4874e69fc22SAndras Domokos if (!msg) {
4884e69fc22SAndras Domokos clear_bit(HSC_CH_WRITE, &channel->flags);
4894e69fc22SAndras Domokos return -ENOSPC;
4904e69fc22SAndras Domokos }
4914e69fc22SAndras Domokos if (copy_from_user(sg_virt(msg->sgt.sgl), (void __user *)buf, len)) {
4924e69fc22SAndras Domokos ret = -EFAULT;
4934e69fc22SAndras Domokos goto out;
4944e69fc22SAndras Domokos }
4954e69fc22SAndras Domokos hsc_msg_len_set(msg, len);
4964e69fc22SAndras Domokos msg->complete = hsc_tx_completed;
4974e69fc22SAndras Domokos msg->destructor = hsc_tx_msg_destructor;
4984e69fc22SAndras Domokos ret = hsi_async_write(channel->cl, msg);
4994e69fc22SAndras Domokos if (ret < 0)
5004e69fc22SAndras Domokos goto out;
5014e69fc22SAndras Domokos
5024e69fc22SAndras Domokos ret = wait_event_interruptible(channel->tx_wait,
5034e69fc22SAndras Domokos !list_empty(&channel->tx_msgs_queue));
5044e69fc22SAndras Domokos if (ret < 0) {
5054e69fc22SAndras Domokos clear_bit(HSC_CH_WRITE, &channel->flags);
5064e69fc22SAndras Domokos hsi_flush(channel->cl);
5074e69fc22SAndras Domokos return -EINTR;
5084e69fc22SAndras Domokos }
5094e69fc22SAndras Domokos
5104e69fc22SAndras Domokos msg = hsc_get_first_msg(channel, &channel->tx_msgs_queue);
5114e69fc22SAndras Domokos if (msg) {
5124e69fc22SAndras Domokos if (msg->status == HSI_STATUS_ERROR)
5134e69fc22SAndras Domokos ret = -EIO;
5144e69fc22SAndras Domokos else
5154e69fc22SAndras Domokos ret = hsc_msg_len_get(msg);
5164e69fc22SAndras Domokos
5174e69fc22SAndras Domokos hsc_add_tail(channel, msg, &channel->free_msgs_list);
5184e69fc22SAndras Domokos }
5194e69fc22SAndras Domokos out:
5204e69fc22SAndras Domokos clear_bit(HSC_CH_WRITE, &channel->flags);
5214e69fc22SAndras Domokos
5224e69fc22SAndras Domokos return ret;
5234e69fc22SAndras Domokos }
5244e69fc22SAndras Domokos
hsc_ioctl(struct file * file,unsigned int cmd,unsigned long arg)5254e69fc22SAndras Domokos static long hsc_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
5264e69fc22SAndras Domokos {
5274e69fc22SAndras Domokos struct hsc_channel *channel = file->private_data;
5284e69fc22SAndras Domokos unsigned int state;
5294e69fc22SAndras Domokos struct hsc_rx_config rxc;
5304e69fc22SAndras Domokos struct hsc_tx_config txc;
5314e69fc22SAndras Domokos long ret = 0;
5324e69fc22SAndras Domokos
5334e69fc22SAndras Domokos switch (cmd) {
5344e69fc22SAndras Domokos case HSC_RESET:
5354e69fc22SAndras Domokos hsi_flush(channel->cl);
5364e69fc22SAndras Domokos break;
5374e69fc22SAndras Domokos case HSC_SET_PM:
5384e69fc22SAndras Domokos if (copy_from_user(&state, (void __user *)arg, sizeof(state)))
5394e69fc22SAndras Domokos return -EFAULT;
5404e69fc22SAndras Domokos if (state == HSC_PM_DISABLE) {
5414e69fc22SAndras Domokos if (test_and_set_bit(HSC_CH_WLINE, &channel->flags))
5424e69fc22SAndras Domokos return -EINVAL;
5434e69fc22SAndras Domokos ret = hsi_start_tx(channel->cl);
5444e69fc22SAndras Domokos } else if (state == HSC_PM_ENABLE) {
5454e69fc22SAndras Domokos if (!test_and_clear_bit(HSC_CH_WLINE, &channel->flags))
5464e69fc22SAndras Domokos return -EINVAL;
5474e69fc22SAndras Domokos ret = hsi_stop_tx(channel->cl);
5484e69fc22SAndras Domokos } else {
5494e69fc22SAndras Domokos ret = -EINVAL;
5504e69fc22SAndras Domokos }
5514e69fc22SAndras Domokos break;
5524e69fc22SAndras Domokos case HSC_SEND_BREAK:
5534e69fc22SAndras Domokos return hsc_break_send(channel->cl);
5544e69fc22SAndras Domokos case HSC_SET_RX:
5554e69fc22SAndras Domokos if (copy_from_user(&rxc, (void __user *)arg, sizeof(rxc)))
5564e69fc22SAndras Domokos return -EFAULT;
5574e69fc22SAndras Domokos return hsc_rx_set(channel->cl, &rxc);
5584e69fc22SAndras Domokos case HSC_GET_RX:
5594e69fc22SAndras Domokos hsc_rx_get(channel->cl, &rxc);
5604e69fc22SAndras Domokos if (copy_to_user((void __user *)arg, &rxc, sizeof(rxc)))
5614e69fc22SAndras Domokos return -EFAULT;
5624e69fc22SAndras Domokos break;
5634e69fc22SAndras Domokos case HSC_SET_TX:
5644e69fc22SAndras Domokos if (copy_from_user(&txc, (void __user *)arg, sizeof(txc)))
5654e69fc22SAndras Domokos return -EFAULT;
5664e69fc22SAndras Domokos return hsc_tx_set(channel->cl, &txc);
5674e69fc22SAndras Domokos case HSC_GET_TX:
5684e69fc22SAndras Domokos hsc_tx_get(channel->cl, &txc);
5694e69fc22SAndras Domokos if (copy_to_user((void __user *)arg, &txc, sizeof(txc)))
5704e69fc22SAndras Domokos return -EFAULT;
5714e69fc22SAndras Domokos break;
5724e69fc22SAndras Domokos default:
5734e69fc22SAndras Domokos return -ENOIOCTLCMD;
5744e69fc22SAndras Domokos }
5754e69fc22SAndras Domokos
5764e69fc22SAndras Domokos return ret;
5774e69fc22SAndras Domokos }
5784e69fc22SAndras Domokos
__hsc_port_release(struct hsc_client_data * cl_data)5794e69fc22SAndras Domokos static inline void __hsc_port_release(struct hsc_client_data *cl_data)
5804e69fc22SAndras Domokos {
5814e69fc22SAndras Domokos BUG_ON(cl_data->usecnt == 0);
5824e69fc22SAndras Domokos
5834e69fc22SAndras Domokos if (--cl_data->usecnt == 0) {
5844e69fc22SAndras Domokos hsi_flush(cl_data->cl);
5854e69fc22SAndras Domokos hsi_release_port(cl_data->cl);
5864e69fc22SAndras Domokos }
5874e69fc22SAndras Domokos }
5884e69fc22SAndras Domokos
hsc_open(struct inode * inode,struct file * file)5894e69fc22SAndras Domokos static int hsc_open(struct inode *inode, struct file *file)
5904e69fc22SAndras Domokos {
5914e69fc22SAndras Domokos struct hsc_client_data *cl_data;
5924e69fc22SAndras Domokos struct hsc_channel *channel;
5934e69fc22SAndras Domokos int ret = 0;
5944e69fc22SAndras Domokos
5954e69fc22SAndras Domokos pr_debug("open, minor = %d\n", iminor(inode));
5964e69fc22SAndras Domokos
5974e69fc22SAndras Domokos cl_data = container_of(inode->i_cdev, struct hsc_client_data, cdev);
5984e69fc22SAndras Domokos mutex_lock(&cl_data->lock);
5994e69fc22SAndras Domokos channel = cl_data->channels + (iminor(inode) & HSC_CH_MASK);
6004e69fc22SAndras Domokos
6014e69fc22SAndras Domokos if (test_and_set_bit(HSC_CH_OPEN, &channel->flags)) {
6024e69fc22SAndras Domokos ret = -EBUSY;
6034e69fc22SAndras Domokos goto out;
6044e69fc22SAndras Domokos }
6054e69fc22SAndras Domokos /*
6064e69fc22SAndras Domokos * Check if we have already claimed the port associated to the HSI
6074e69fc22SAndras Domokos * client. If not then try to claim it, else increase its refcount
6084e69fc22SAndras Domokos */
6094e69fc22SAndras Domokos if (cl_data->usecnt == 0) {
6104e69fc22SAndras Domokos ret = hsi_claim_port(cl_data->cl, 0);
6114e69fc22SAndras Domokos if (ret < 0)
6124e69fc22SAndras Domokos goto out;
6134e69fc22SAndras Domokos hsi_setup(cl_data->cl);
6144e69fc22SAndras Domokos }
6154e69fc22SAndras Domokos cl_data->usecnt++;
6164e69fc22SAndras Domokos
6174e69fc22SAndras Domokos ret = hsc_msgs_alloc(channel);
6184e69fc22SAndras Domokos if (ret < 0) {
6194e69fc22SAndras Domokos __hsc_port_release(cl_data);
6204e69fc22SAndras Domokos goto out;
6214e69fc22SAndras Domokos }
6224e69fc22SAndras Domokos
6234e69fc22SAndras Domokos file->private_data = channel;
6244e69fc22SAndras Domokos mutex_unlock(&cl_data->lock);
6254e69fc22SAndras Domokos
6264e69fc22SAndras Domokos return ret;
6274e69fc22SAndras Domokos out:
6284e69fc22SAndras Domokos mutex_unlock(&cl_data->lock);
6294e69fc22SAndras Domokos
6304e69fc22SAndras Domokos return ret;
6314e69fc22SAndras Domokos }
6324e69fc22SAndras Domokos
hsc_release(struct inode * inode __maybe_unused,struct file * file)6334e69fc22SAndras Domokos static int hsc_release(struct inode *inode __maybe_unused, struct file *file)
6344e69fc22SAndras Domokos {
6354e69fc22SAndras Domokos struct hsc_channel *channel = file->private_data;
6364e69fc22SAndras Domokos struct hsc_client_data *cl_data = channel->cl_data;
6374e69fc22SAndras Domokos
6384e69fc22SAndras Domokos mutex_lock(&cl_data->lock);
6394e69fc22SAndras Domokos file->private_data = NULL;
6404e69fc22SAndras Domokos if (test_and_clear_bit(HSC_CH_WLINE, &channel->flags))
6414e69fc22SAndras Domokos hsi_stop_tx(channel->cl);
6424e69fc22SAndras Domokos __hsc_port_release(cl_data);
6434e69fc22SAndras Domokos hsc_reset_list(channel, &channel->rx_msgs_queue);
6444e69fc22SAndras Domokos hsc_reset_list(channel, &channel->tx_msgs_queue);
6454e69fc22SAndras Domokos hsc_reset_list(channel, &channel->free_msgs_list);
6464e69fc22SAndras Domokos clear_bit(HSC_CH_READ, &channel->flags);
6474e69fc22SAndras Domokos clear_bit(HSC_CH_WRITE, &channel->flags);
6484e69fc22SAndras Domokos clear_bit(HSC_CH_OPEN, &channel->flags);
6494e69fc22SAndras Domokos wake_up(&channel->rx_wait);
6504e69fc22SAndras Domokos wake_up(&channel->tx_wait);
6514e69fc22SAndras Domokos mutex_unlock(&cl_data->lock);
6524e69fc22SAndras Domokos
6534e69fc22SAndras Domokos return 0;
6544e69fc22SAndras Domokos }
6554e69fc22SAndras Domokos
6564e69fc22SAndras Domokos static const struct file_operations hsc_fops = {
6574e69fc22SAndras Domokos .owner = THIS_MODULE,
6584e69fc22SAndras Domokos .read = hsc_read,
6594e69fc22SAndras Domokos .write = hsc_write,
6604e69fc22SAndras Domokos .unlocked_ioctl = hsc_ioctl,
6614e69fc22SAndras Domokos .open = hsc_open,
6624e69fc22SAndras Domokos .release = hsc_release,
6634e69fc22SAndras Domokos };
6644e69fc22SAndras Domokos
hsc_channel_init(struct hsc_channel * channel)6650fe763c5SGreg Kroah-Hartman static void hsc_channel_init(struct hsc_channel *channel)
6664e69fc22SAndras Domokos {
6674e69fc22SAndras Domokos init_waitqueue_head(&channel->rx_wait);
6684e69fc22SAndras Domokos init_waitqueue_head(&channel->tx_wait);
6694e69fc22SAndras Domokos spin_lock_init(&channel->lock);
6704e69fc22SAndras Domokos INIT_LIST_HEAD(&channel->free_msgs_list);
6714e69fc22SAndras Domokos INIT_LIST_HEAD(&channel->rx_msgs_queue);
6724e69fc22SAndras Domokos INIT_LIST_HEAD(&channel->tx_msgs_queue);
6734e69fc22SAndras Domokos }
6744e69fc22SAndras Domokos
hsc_probe(struct device * dev)6750fe763c5SGreg Kroah-Hartman static int hsc_probe(struct device *dev)
6764e69fc22SAndras Domokos {
6774e69fc22SAndras Domokos const char devname[] = "hsi_char";
6784e69fc22SAndras Domokos struct hsc_client_data *cl_data;
6794e69fc22SAndras Domokos struct hsc_channel *channel;
6804e69fc22SAndras Domokos struct hsi_client *cl = to_hsi_client(dev);
6814e69fc22SAndras Domokos unsigned int hsc_baseminor;
6824e69fc22SAndras Domokos dev_t hsc_dev;
6834e69fc22SAndras Domokos int ret;
6844e69fc22SAndras Domokos int i;
6854e69fc22SAndras Domokos
6864e69fc22SAndras Domokos cl_data = kzalloc(sizeof(*cl_data), GFP_KERNEL);
687e3d7fbabSMarkus Elfring if (!cl_data)
6884e69fc22SAndras Domokos return -ENOMEM;
689e3d7fbabSMarkus Elfring
6904e69fc22SAndras Domokos hsc_baseminor = HSC_BASEMINOR(hsi_id(cl), hsi_port_id(cl));
6914e69fc22SAndras Domokos if (!hsc_major) {
6924e69fc22SAndras Domokos ret = alloc_chrdev_region(&hsc_dev, hsc_baseminor,
6934e69fc22SAndras Domokos HSC_DEVS, devname);
69484d93b5eSSebastian Reichel if (ret == 0)
6954e69fc22SAndras Domokos hsc_major = MAJOR(hsc_dev);
6964e69fc22SAndras Domokos } else {
6974e69fc22SAndras Domokos hsc_dev = MKDEV(hsc_major, hsc_baseminor);
6984e69fc22SAndras Domokos ret = register_chrdev_region(hsc_dev, HSC_DEVS, devname);
6994e69fc22SAndras Domokos }
7004e69fc22SAndras Domokos if (ret < 0) {
7014e69fc22SAndras Domokos dev_err(dev, "Device %s allocation failed %d\n",
7024e69fc22SAndras Domokos hsc_major ? "minor" : "major", ret);
7034e69fc22SAndras Domokos goto out1;
7044e69fc22SAndras Domokos }
7054e69fc22SAndras Domokos mutex_init(&cl_data->lock);
7064e69fc22SAndras Domokos hsi_client_set_drvdata(cl, cl_data);
7074e69fc22SAndras Domokos cdev_init(&cl_data->cdev, &hsc_fops);
7084e69fc22SAndras Domokos cl_data->cdev.owner = THIS_MODULE;
7094e69fc22SAndras Domokos cl_data->cl = cl;
7104e69fc22SAndras Domokos for (i = 0, channel = cl_data->channels; i < HSC_DEVS; i++, channel++) {
7114e69fc22SAndras Domokos hsc_channel_init(channel);
7124e69fc22SAndras Domokos channel->ch = i;
7134e69fc22SAndras Domokos channel->cl = cl;
7144e69fc22SAndras Domokos channel->cl_data = cl_data;
7154e69fc22SAndras Domokos }
7164e69fc22SAndras Domokos
7174e69fc22SAndras Domokos /* 1 hsi client -> N char devices (one for each channel) */
7184e69fc22SAndras Domokos ret = cdev_add(&cl_data->cdev, hsc_dev, HSC_DEVS);
7194e69fc22SAndras Domokos if (ret) {
7204e69fc22SAndras Domokos dev_err(dev, "Could not add char device %d\n", ret);
7214e69fc22SAndras Domokos goto out2;
7224e69fc22SAndras Domokos }
7234e69fc22SAndras Domokos
7244e69fc22SAndras Domokos return 0;
7254e69fc22SAndras Domokos out2:
7264e69fc22SAndras Domokos unregister_chrdev_region(hsc_dev, HSC_DEVS);
7274e69fc22SAndras Domokos out1:
7284e69fc22SAndras Domokos kfree(cl_data);
7294e69fc22SAndras Domokos
7304e69fc22SAndras Domokos return ret;
7314e69fc22SAndras Domokos }
7324e69fc22SAndras Domokos
hsc_remove(struct device * dev)7330fe763c5SGreg Kroah-Hartman static int hsc_remove(struct device *dev)
7344e69fc22SAndras Domokos {
7354e69fc22SAndras Domokos struct hsi_client *cl = to_hsi_client(dev);
7364e69fc22SAndras Domokos struct hsc_client_data *cl_data = hsi_client_drvdata(cl);
7374e69fc22SAndras Domokos dev_t hsc_dev = cl_data->cdev.dev;
7384e69fc22SAndras Domokos
7394e69fc22SAndras Domokos cdev_del(&cl_data->cdev);
7404e69fc22SAndras Domokos unregister_chrdev_region(hsc_dev, HSC_DEVS);
7414e69fc22SAndras Domokos hsi_client_set_drvdata(cl, NULL);
7424e69fc22SAndras Domokos kfree(cl_data);
7434e69fc22SAndras Domokos
7444e69fc22SAndras Domokos return 0;
7454e69fc22SAndras Domokos }
7464e69fc22SAndras Domokos
7474e69fc22SAndras Domokos static struct hsi_client_driver hsc_driver = {
7484e69fc22SAndras Domokos .driver = {
7494e69fc22SAndras Domokos .name = "hsi_char",
7504e69fc22SAndras Domokos .owner = THIS_MODULE,
7514e69fc22SAndras Domokos .probe = hsc_probe,
7520fe763c5SGreg Kroah-Hartman .remove = hsc_remove,
7534e69fc22SAndras Domokos },
7544e69fc22SAndras Domokos };
7554e69fc22SAndras Domokos
hsc_init(void)7564e69fc22SAndras Domokos static int __init hsc_init(void)
7574e69fc22SAndras Domokos {
7584e69fc22SAndras Domokos int ret;
7594e69fc22SAndras Domokos
7604e69fc22SAndras Domokos if ((max_data_size < 4) || (max_data_size > 0x10000) ||
7614e69fc22SAndras Domokos (max_data_size & (max_data_size - 1))) {
76236edec05SArvind Yadav pr_err("Invalid max read/write data size\n");
7634e69fc22SAndras Domokos return -EINVAL;
7644e69fc22SAndras Domokos }
7654e69fc22SAndras Domokos
7664e69fc22SAndras Domokos ret = hsi_register_client_driver(&hsc_driver);
7674e69fc22SAndras Domokos if (ret) {
76836edec05SArvind Yadav pr_err("Error while registering HSI/SSI driver %d\n", ret);
7694e69fc22SAndras Domokos return ret;
7704e69fc22SAndras Domokos }
7714e69fc22SAndras Domokos
7724e69fc22SAndras Domokos pr_info("HSI/SSI char device loaded\n");
7734e69fc22SAndras Domokos
7744e69fc22SAndras Domokos return 0;
7754e69fc22SAndras Domokos }
7764e69fc22SAndras Domokos module_init(hsc_init);
7774e69fc22SAndras Domokos
hsc_exit(void)7784e69fc22SAndras Domokos static void __exit hsc_exit(void)
7794e69fc22SAndras Domokos {
7804e69fc22SAndras Domokos hsi_unregister_client_driver(&hsc_driver);
7814e69fc22SAndras Domokos pr_info("HSI char device removed\n");
7824e69fc22SAndras Domokos }
7834e69fc22SAndras Domokos module_exit(hsc_exit);
7844e69fc22SAndras Domokos
7854e69fc22SAndras Domokos MODULE_AUTHOR("Andras Domokos <andras.domokos@nokia.com>");
7864e69fc22SAndras Domokos MODULE_ALIAS("hsi:hsi_char");
7874e69fc22SAndras Domokos MODULE_DESCRIPTION("HSI character device");
7884e69fc22SAndras Domokos MODULE_LICENSE("GPL v2");
789