24 #include <linux/errno.h>
25 #include <linux/types.h>
27 #include <linux/kernel.h>
29 #include <linux/module.h>
31 #include <linux/list.h>
32 #include <linux/slab.h>
35 #include <linux/wait.h>
37 #include <linux/sched.h>
38 #include <linux/device.h>
42 #include <linux/stat.h>
52 #define HSC_PORT_ID_BITS 4
54 #define HSC_PORT_ID_MASK 3
55 #define HSC_CH_MASK 0xf
61 #define HSC_BASEMINOR(id, port_id) \
62 ((((id) & HSC_ID_MASK) << HSC_ID_BITS) | \
63 (((port_id) & HSC_PORT_ID_MASK) << HSC_PORT_ID_BITS))
123 static unsigned int hsc_major;
125 static unsigned int max_data_size = 0x1000;
127 MODULE_PARM_DESC(max_data_size,
"max read/write data size [4,8..65536] (^2)");
136 spin_unlock_irqrestore(&channel->
lock, flags);
147 if (list_empty(queue))
153 spin_unlock_irqrestore(&channel->
lock, flags);
158 static inline void hsc_msg_free(
struct hsi_msg *msg)
180 list_splice_init(l, &
list);
181 spin_unlock_irqrestore(&channel->
lock, flags);
183 hsc_free_list(&
list);
186 static inline struct hsi_msg *hsc_msg_alloc(
unsigned int alloc_size)
208 static inline int hsc_msgs_alloc(
struct hsc_channel *channel)
214 msg = hsc_msg_alloc(max_data_size);
228 static inline unsigned int hsc_msg_len_get(
struct hsi_msg *msg)
230 return msg->
sgt.sgl->length;
233 static inline void hsc_msg_len_set(
struct hsi_msg *msg,
unsigned int len)
235 msg->
sgt.sgl->length = len;
238 static void hsc_rx_completed(
struct hsi_msg *msg)
251 static void hsc_rx_msg_destructor(
struct hsi_msg *msg)
254 hsc_msg_len_set(msg, 0);
255 hsc_rx_completed(msg);
258 static void hsc_tx_completed(
struct hsi_msg *msg)
271 static void hsc_tx_msg_destructor(
struct hsi_msg *msg)
274 hsc_msg_len_set(msg, 0);
275 hsc_tx_completed(msg);
278 static void hsc_break_req_destructor(
struct hsi_msg *msg)
286 static void hsc_break_received(
struct hsi_msg *msg)
293 for (i = 0; i <
HSC_DEVS; i++, channel++) {
302 hsc_msg_len_set(msg2, 0);
308 ret = hsi_async_read(msg->
cl, msg);
310 hsc_break_req_destructor(msg);
330 ret = hsi_async_read(cl, msg);
332 hsc_break_req_destructor(msg);
337 static int hsc_break_send(
struct hsi_client *cl)
348 ret = hsi_async_write(cl, msg);
378 hsc_break_request(cl);
425 static ssize_t hsc_read(
struct file *
file,
char __user *buf,
size_t len,
436 if (len > max_data_size)
438 if (channel->
ch >= channel->
cl->rx_cfg.channels)
447 hsc_msg_len_set(msg, len);
450 ret = hsi_async_read(channel->
cl, msg);
460 hsi_flush(channel->
cl);
468 sg_virt(msg->
sgt.sgl), hsc_msg_len_get(msg));
472 ret = hsc_msg_len_get(msg);
484 static ssize_t hsc_write(
struct file *file,
const char __user *buf,
size_t len,
485 loff_t *ppos __maybe_unused)
493 if (len > max_data_size)
495 if (channel->
ch >= channel->
cl->tx_cfg.channels)
508 hsc_msg_len_set(msg, len);
511 ret = hsi_async_write(channel->
cl, msg);
519 hsi_flush(channel->
cl);
528 ret = hsc_msg_len_get(msg);
538 static long hsc_ioctl(
struct file *file,
unsigned int cmd,
unsigned long arg)
548 hsi_flush(channel->
cl);
556 ret = hsi_start_tx(channel->
cl);
560 ret = hsi_stop_tx(channel->
cl);
566 return hsc_break_send(channel->
cl);
570 return hsc_rx_set(channel->
cl, &rxc);
572 hsc_rx_get(channel->
cl, &rxc);
573 if (
copy_to_user((
void __user *)arg, &rxc,
sizeof(rxc)))
579 return hsc_tx_set(channel->
cl, &txc);
581 hsc_tx_get(channel->
cl, &txc);
582 if (
copy_to_user((
void __user *)arg, &txc,
sizeof(txc)))
596 if (--cl_data->
usecnt == 0) {
597 hsi_flush(cl_data->
cl);
602 static int hsc_open(
struct inode *
inode,
struct file *file)
608 pr_debug(
"open, minor = %d\n", iminor(inode));
622 if (cl_data->
usecnt == 0) {
626 hsi_setup(cl_data->
cl);
630 ret = hsc_msgs_alloc(channel);
632 __hsc_port_release(cl_data);
646 static int hsc_release(
struct inode *inode __maybe_unused,
struct file *file)
654 hsi_stop_tx(channel->
cl);
655 __hsc_port_release(cl_data);
673 .unlocked_ioctl = hsc_ioctl,
675 .release = hsc_release,
690 const char devname[] =
"hsi_char";
694 unsigned int hsc_baseminor;
699 cl_data = kzalloc(
sizeof(*cl_data),
GFP_KERNEL);
701 dev_err(dev,
"Could not allocate hsc_client_data\n");
709 hsc_major =
MAJOR(hsc_dev);
711 hsc_dev =
MKDEV(hsc_major, hsc_baseminor);
715 dev_err(dev,
"Device %s allocation failed %d\n",
716 hsc_major ?
"minor" :
"major", ret);
720 hsi_client_set_drvdata(cl, cl_data);
724 for (i = 0, channel = cl_data->
channels; i < HSC_DEVS; i++, channel++) {
725 hsc_channel_init(channel);
734 dev_err(dev,
"Could not add char device %d\n", ret);
755 hsi_client_set_drvdata(cl,
NULL);
770 static int __init hsc_init(
void)
774 if ((max_data_size < 4) || (max_data_size > 0x10000) ||
775 (max_data_size & (max_data_size - 1))) {
776 pr_err(
"Invalid max read/write data size");
782 pr_err(
"Error while registering HSI/SSI driver %d", ret);
786 pr_info(
"HSI/SSI char device loaded\n");
792 static void __exit hsc_exit(
void)
794 hsi_unregister_client_driver(&hsc_driver);
795 pr_info(
"HSI char device removed\n");