2 * HSI character device driver, implements the character device
5 * Copyright (C) 2010 Nokia Corporation. All rights reserved.
7 * Contact: Andras Domokos <andras.domokos@nokia.com>
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * version 2 as published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
24 #include <linux/errno.h>
25 #include <linux/types.h>
26 #include <linux/atomic.h>
27 #include <linux/kernel.h>
28 #include <linux/init.h>
29 #include <linux/module.h>
30 #include <linux/mutex.h>
31 #include <linux/list.h>
32 #include <linux/slab.h>
33 #include <linux/kmemleak.h>
34 #include <linux/ioctl.h>
35 #include <linux/wait.h>
37 #include <linux/sched.h>
38 #include <linux/device.h>
39 #include <linux/cdev.h>
40 #include <linux/uaccess.h>
41 #include <linux/scatterlist.h>
42 #include <linux/stat.h>
43 #include <linux/hsi/hsi.h>
44 #include <linux/hsi/hsi_char.h>
46 #define HSC_DEVS 16 /* Num of channels */
52 #define HSC_PORT_ID_BITS 4
54 #define HSC_PORT_ID_MASK 3
55 #define HSC_CH_MASK 0xf
58 * We support up to 4 controllers that can have up to 4
59 * ports, which should currently be more than enough.
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))
77 struct hsc_client_data
;
79 * struct hsc_channel - hsi_char internal channel data
81 * @flags: Keeps state of the channel (open/close, reading, writing)
82 * @free_msgs_list: List of free HSI messages/requests
83 * @rx_msgs_queue: List of pending RX requests
84 * @tx_msgs_queue: List of pending TX requests
85 * @lock: Serialize access to the lists
86 * @cl: reference to the associated hsi_client
87 * @cl_data: reference to the client data that this channels belongs to
88 * @rx_wait: RX requests wait queue
89 * @tx_wait: TX requests wait queue
94 struct list_head free_msgs_list
;
95 struct list_head rx_msgs_queue
;
96 struct list_head tx_msgs_queue
;
98 struct hsi_client
*cl
;
99 struct hsc_client_data
*cl_data
;
100 wait_queue_head_t rx_wait
;
101 wait_queue_head_t tx_wait
;
105 * struct hsc_client_data - hsi_char internal client data
106 * @cdev: Characther device associated to the hsi_client
107 * @lock: Lock to serialize open/close access
108 * @flags: Keeps track of port state (rx hwbreak armed)
109 * @usecnt: Use count for claiming the HSI port (mutex protected)
110 * @cl: Referece to the HSI client
111 * @channels: Array of channels accessible by the client
113 struct hsc_client_data
{
118 struct hsi_client
*cl
;
119 struct hsc_channel channels
[HSC_DEVS
];
122 /* Stores the major number dynamically allocated for hsi_char */
123 static unsigned int hsc_major
;
124 /* Maximum buffer size that hsi_char will accept from userspace */
125 static unsigned int max_data_size
= 0x1000;
126 module_param(max_data_size
, uint
, 0);
127 MODULE_PARM_DESC(max_data_size
, "max read/write data size [4,8..65536] (^2)");
129 static void hsc_add_tail(struct hsc_channel
*channel
, struct hsi_msg
*msg
,
130 struct list_head
*queue
)
134 spin_lock_irqsave(&channel
->lock
, flags
);
135 list_add_tail(&msg
->link
, queue
);
136 spin_unlock_irqrestore(&channel
->lock
, flags
);
139 static struct hsi_msg
*hsc_get_first_msg(struct hsc_channel
*channel
,
140 struct list_head
*queue
)
142 struct hsi_msg
*msg
= NULL
;
145 spin_lock_irqsave(&channel
->lock
, flags
);
147 if (list_empty(queue
))
150 msg
= list_first_entry(queue
, struct hsi_msg
, link
);
151 list_del(&msg
->link
);
153 spin_unlock_irqrestore(&channel
->lock
, flags
);
158 static inline void hsc_msg_free(struct hsi_msg
*msg
)
160 kfree(sg_virt(msg
->sgt
.sgl
));
164 static void hsc_free_list(struct list_head
*list
)
166 struct hsi_msg
*msg
, *tmp
;
168 list_for_each_entry_safe(msg
, tmp
, list
, link
) {
169 list_del(&msg
->link
);
174 static void hsc_reset_list(struct hsc_channel
*channel
, struct list_head
*l
)
179 spin_lock_irqsave(&channel
->lock
, flags
);
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
)
191 msg
= hsi_alloc_msg(1, GFP_KERNEL
);
194 buf
= kmalloc(alloc_size
, GFP_KERNEL
);
199 sg_init_one(msg
->sgt
.sgl
, buf
, alloc_size
);
200 /* Ignore false positive, due to sg pointer handling */
201 kmemleak_ignore(buf
);
208 static inline int hsc_msgs_alloc(struct hsc_channel
*channel
)
213 for (i
= 0; i
< HSC_MSGS
; i
++) {
214 msg
= hsc_msg_alloc(max_data_size
);
217 msg
->channel
= channel
->ch
;
218 list_add_tail(&msg
->link
, &channel
->free_msgs_list
);
223 hsc_free_list(&channel
->free_msgs_list
);
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
)
240 struct hsc_client_data
*cl_data
= hsi_client_drvdata(msg
->cl
);
241 struct hsc_channel
*channel
= cl_data
->channels
+ msg
->channel
;
243 if (test_bit(HSC_CH_READ
, &channel
->flags
)) {
244 hsc_add_tail(channel
, msg
, &channel
->rx_msgs_queue
);
245 wake_up(&channel
->rx_wait
);
247 hsc_add_tail(channel
, msg
, &channel
->free_msgs_list
);
251 static void hsc_rx_msg_destructor(struct hsi_msg
*msg
)
253 msg
->status
= HSI_STATUS_ERROR
;
254 hsc_msg_len_set(msg
, 0);
255 hsc_rx_completed(msg
);
258 static void hsc_tx_completed(struct hsi_msg
*msg
)
260 struct hsc_client_data
*cl_data
= hsi_client_drvdata(msg
->cl
);
261 struct hsc_channel
*channel
= cl_data
->channels
+ msg
->channel
;
263 if (test_bit(HSC_CH_WRITE
, &channel
->flags
)) {
264 hsc_add_tail(channel
, msg
, &channel
->tx_msgs_queue
);
265 wake_up(&channel
->tx_wait
);
267 hsc_add_tail(channel
, msg
, &channel
->free_msgs_list
);
271 static void hsc_tx_msg_destructor(struct hsi_msg
*msg
)
273 msg
->status
= HSI_STATUS_ERROR
;
274 hsc_msg_len_set(msg
, 0);
275 hsc_tx_completed(msg
);
278 static void hsc_break_req_destructor(struct hsi_msg
*msg
)
280 struct hsc_client_data
*cl_data
= hsi_client_drvdata(msg
->cl
);
283 clear_bit(HSC_RXBREAK
, &cl_data
->flags
);
286 static void hsc_break_received(struct hsi_msg
*msg
)
288 struct hsc_client_data
*cl_data
= hsi_client_drvdata(msg
->cl
);
289 struct hsc_channel
*channel
= cl_data
->channels
;
292 /* Broadcast HWBREAK on all channels */
293 for (i
= 0; i
< HSC_DEVS
; i
++, channel
++) {
294 struct hsi_msg
*msg2
;
296 if (!test_bit(HSC_CH_READ
, &channel
->flags
))
298 msg2
= hsc_get_first_msg(channel
, &channel
->free_msgs_list
);
301 clear_bit(HSC_CH_READ
, &channel
->flags
);
302 hsc_msg_len_set(msg2
, 0);
303 msg2
->status
= HSI_STATUS_COMPLETED
;
304 hsc_add_tail(channel
, msg2
, &channel
->rx_msgs_queue
);
305 wake_up(&channel
->rx_wait
);
308 ret
= hsi_async_read(msg
->cl
, msg
);
310 hsc_break_req_destructor(msg
);
313 static int hsc_break_request(struct hsi_client
*cl
)
315 struct hsc_client_data
*cl_data
= hsi_client_drvdata(cl
);
319 if (test_and_set_bit(HSC_RXBREAK
, &cl_data
->flags
))
322 msg
= hsi_alloc_msg(0, GFP_KERNEL
);
324 clear_bit(HSC_RXBREAK
, &cl_data
->flags
);
327 msg
->break_frame
= 1;
328 msg
->complete
= hsc_break_received
;
329 msg
->destructor
= hsc_break_req_destructor
;
330 ret
= hsi_async_read(cl
, msg
);
332 hsc_break_req_destructor(msg
);
337 static int hsc_break_send(struct hsi_client
*cl
)
342 msg
= hsi_alloc_msg(0, GFP_ATOMIC
);
345 msg
->break_frame
= 1;
346 msg
->complete
= hsi_free_msg
;
347 msg
->destructor
= hsi_free_msg
;
348 ret
= hsi_async_write(cl
, msg
);
355 static int hsc_rx_set(struct hsi_client
*cl
, struct hsc_rx_config
*rxc
)
357 struct hsi_config tmp
;
360 if ((rxc
->mode
!= HSI_MODE_STREAM
) && (rxc
->mode
!= HSI_MODE_FRAME
))
362 if ((rxc
->channels
== 0) || (rxc
->channels
> HSC_DEVS
))
364 if (rxc
->channels
& (rxc
->channels
- 1))
366 if ((rxc
->flow
!= HSI_FLOW_SYNC
) && (rxc
->flow
!= HSI_FLOW_PIPE
))
369 cl
->rx_cfg
.mode
= rxc
->mode
;
370 cl
->rx_cfg
.num_hw_channels
= rxc
->channels
;
371 cl
->rx_cfg
.flow
= rxc
->flow
;
377 if (rxc
->mode
== HSI_MODE_FRAME
)
378 hsc_break_request(cl
);
383 static inline void hsc_rx_get(struct hsi_client
*cl
, struct hsc_rx_config
*rxc
)
385 rxc
->mode
= cl
->rx_cfg
.mode
;
386 rxc
->channels
= cl
->rx_cfg
.num_hw_channels
;
387 rxc
->flow
= cl
->rx_cfg
.flow
;
390 static int hsc_tx_set(struct hsi_client
*cl
, struct hsc_tx_config
*txc
)
392 struct hsi_config tmp
;
395 if ((txc
->mode
!= HSI_MODE_STREAM
) && (txc
->mode
!= HSI_MODE_FRAME
))
397 if ((txc
->channels
== 0) || (txc
->channels
> HSC_DEVS
))
399 if (txc
->channels
& (txc
->channels
- 1))
401 if ((txc
->arb_mode
!= HSI_ARB_RR
) && (txc
->arb_mode
!= HSI_ARB_PRIO
))
404 cl
->tx_cfg
.mode
= txc
->mode
;
405 cl
->tx_cfg
.num_hw_channels
= txc
->channels
;
406 cl
->tx_cfg
.speed
= txc
->speed
;
407 cl
->tx_cfg
.arb_mode
= txc
->arb_mode
;
417 static inline void hsc_tx_get(struct hsi_client
*cl
, struct hsc_tx_config
*txc
)
419 txc
->mode
= cl
->tx_cfg
.mode
;
420 txc
->channels
= cl
->tx_cfg
.num_hw_channels
;
421 txc
->speed
= cl
->tx_cfg
.speed
;
422 txc
->arb_mode
= cl
->tx_cfg
.arb_mode
;
425 static ssize_t
hsc_read(struct file
*file
, char __user
*buf
, size_t len
,
426 loff_t
*ppos __maybe_unused
)
428 struct hsc_channel
*channel
= file
->private_data
;
434 if (!IS_ALIGNED(len
, sizeof(u32
)))
436 if (len
> max_data_size
)
438 if (channel
->ch
>= channel
->cl
->rx_cfg
.num_hw_channels
)
440 if (test_and_set_bit(HSC_CH_READ
, &channel
->flags
))
442 msg
= hsc_get_first_msg(channel
, &channel
->free_msgs_list
);
447 hsc_msg_len_set(msg
, len
);
448 msg
->complete
= hsc_rx_completed
;
449 msg
->destructor
= hsc_rx_msg_destructor
;
450 ret
= hsi_async_read(channel
->cl
, msg
);
452 hsc_add_tail(channel
, msg
, &channel
->free_msgs_list
);
456 ret
= wait_event_interruptible(channel
->rx_wait
,
457 !list_empty(&channel
->rx_msgs_queue
));
459 clear_bit(HSC_CH_READ
, &channel
->flags
);
460 hsi_flush(channel
->cl
);
464 msg
= hsc_get_first_msg(channel
, &channel
->rx_msgs_queue
);
466 if (msg
->status
!= HSI_STATUS_ERROR
) {
467 ret
= copy_to_user((void __user
*)buf
,
468 sg_virt(msg
->sgt
.sgl
), hsc_msg_len_get(msg
));
472 ret
= hsc_msg_len_get(msg
);
476 hsc_add_tail(channel
, msg
, &channel
->free_msgs_list
);
479 clear_bit(HSC_CH_READ
, &channel
->flags
);
484 static ssize_t
hsc_write(struct file
*file
, const char __user
*buf
, size_t len
,
485 loff_t
*ppos __maybe_unused
)
487 struct hsc_channel
*channel
= file
->private_data
;
491 if ((len
== 0) || !IS_ALIGNED(len
, sizeof(u32
)))
493 if (len
> max_data_size
)
495 if (channel
->ch
>= channel
->cl
->tx_cfg
.num_hw_channels
)
497 if (test_and_set_bit(HSC_CH_WRITE
, &channel
->flags
))
499 msg
= hsc_get_first_msg(channel
, &channel
->free_msgs_list
);
501 clear_bit(HSC_CH_WRITE
, &channel
->flags
);
504 if (copy_from_user(sg_virt(msg
->sgt
.sgl
), (void __user
*)buf
, len
)) {
508 hsc_msg_len_set(msg
, len
);
509 msg
->complete
= hsc_tx_completed
;
510 msg
->destructor
= hsc_tx_msg_destructor
;
511 ret
= hsi_async_write(channel
->cl
, msg
);
515 ret
= wait_event_interruptible(channel
->tx_wait
,
516 !list_empty(&channel
->tx_msgs_queue
));
518 clear_bit(HSC_CH_WRITE
, &channel
->flags
);
519 hsi_flush(channel
->cl
);
523 msg
= hsc_get_first_msg(channel
, &channel
->tx_msgs_queue
);
525 if (msg
->status
== HSI_STATUS_ERROR
)
528 ret
= hsc_msg_len_get(msg
);
530 hsc_add_tail(channel
, msg
, &channel
->free_msgs_list
);
533 clear_bit(HSC_CH_WRITE
, &channel
->flags
);
538 static long hsc_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
540 struct hsc_channel
*channel
= file
->private_data
;
542 struct hsc_rx_config rxc
;
543 struct hsc_tx_config txc
;
548 hsi_flush(channel
->cl
);
551 if (copy_from_user(&state
, (void __user
*)arg
, sizeof(state
)))
553 if (state
== HSC_PM_DISABLE
) {
554 if (test_and_set_bit(HSC_CH_WLINE
, &channel
->flags
))
556 ret
= hsi_start_tx(channel
->cl
);
557 } else if (state
== HSC_PM_ENABLE
) {
558 if (!test_and_clear_bit(HSC_CH_WLINE
, &channel
->flags
))
560 ret
= hsi_stop_tx(channel
->cl
);
566 return hsc_break_send(channel
->cl
);
568 if (copy_from_user(&rxc
, (void __user
*)arg
, sizeof(rxc
)))
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
)))
577 if (copy_from_user(&txc
, (void __user
*)arg
, sizeof(txc
)))
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
)))
592 static inline void __hsc_port_release(struct hsc_client_data
*cl_data
)
594 BUG_ON(cl_data
->usecnt
== 0);
596 if (--cl_data
->usecnt
== 0) {
597 hsi_flush(cl_data
->cl
);
598 hsi_release_port(cl_data
->cl
);
602 static int hsc_open(struct inode
*inode
, struct file
*file
)
604 struct hsc_client_data
*cl_data
;
605 struct hsc_channel
*channel
;
608 pr_debug("open, minor = %d\n", iminor(inode
));
610 cl_data
= container_of(inode
->i_cdev
, struct hsc_client_data
, cdev
);
611 mutex_lock(&cl_data
->lock
);
612 channel
= cl_data
->channels
+ (iminor(inode
) & HSC_CH_MASK
);
614 if (test_and_set_bit(HSC_CH_OPEN
, &channel
->flags
)) {
619 * Check if we have already claimed the port associated to the HSI
620 * client. If not then try to claim it, else increase its refcount
622 if (cl_data
->usecnt
== 0) {
623 ret
= hsi_claim_port(cl_data
->cl
, 0);
626 hsi_setup(cl_data
->cl
);
630 ret
= hsc_msgs_alloc(channel
);
632 __hsc_port_release(cl_data
);
636 file
->private_data
= channel
;
637 mutex_unlock(&cl_data
->lock
);
641 mutex_unlock(&cl_data
->lock
);
646 static int hsc_release(struct inode
*inode __maybe_unused
, struct file
*file
)
648 struct hsc_channel
*channel
= file
->private_data
;
649 struct hsc_client_data
*cl_data
= channel
->cl_data
;
651 mutex_lock(&cl_data
->lock
);
652 file
->private_data
= NULL
;
653 if (test_and_clear_bit(HSC_CH_WLINE
, &channel
->flags
))
654 hsi_stop_tx(channel
->cl
);
655 __hsc_port_release(cl_data
);
656 hsc_reset_list(channel
, &channel
->rx_msgs_queue
);
657 hsc_reset_list(channel
, &channel
->tx_msgs_queue
);
658 hsc_reset_list(channel
, &channel
->free_msgs_list
);
659 clear_bit(HSC_CH_READ
, &channel
->flags
);
660 clear_bit(HSC_CH_WRITE
, &channel
->flags
);
661 clear_bit(HSC_CH_OPEN
, &channel
->flags
);
662 wake_up(&channel
->rx_wait
);
663 wake_up(&channel
->tx_wait
);
664 mutex_unlock(&cl_data
->lock
);
669 static const struct file_operations hsc_fops
= {
670 .owner
= THIS_MODULE
,
673 .unlocked_ioctl
= hsc_ioctl
,
675 .release
= hsc_release
,
678 static void hsc_channel_init(struct hsc_channel
*channel
)
680 init_waitqueue_head(&channel
->rx_wait
);
681 init_waitqueue_head(&channel
->tx_wait
);
682 spin_lock_init(&channel
->lock
);
683 INIT_LIST_HEAD(&channel
->free_msgs_list
);
684 INIT_LIST_HEAD(&channel
->rx_msgs_queue
);
685 INIT_LIST_HEAD(&channel
->tx_msgs_queue
);
688 static int hsc_probe(struct device
*dev
)
690 const char devname
[] = "hsi_char";
691 struct hsc_client_data
*cl_data
;
692 struct hsc_channel
*channel
;
693 struct hsi_client
*cl
= to_hsi_client(dev
);
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");
704 hsc_baseminor
= HSC_BASEMINOR(hsi_id(cl
), hsi_port_id(cl
));
706 ret
= alloc_chrdev_region(&hsc_dev
, hsc_baseminor
,
709 hsc_major
= MAJOR(hsc_dev
);
711 hsc_dev
= MKDEV(hsc_major
, hsc_baseminor
);
712 ret
= register_chrdev_region(hsc_dev
, HSC_DEVS
, devname
);
715 dev_err(dev
, "Device %s allocation failed %d\n",
716 hsc_major
? "minor" : "major", ret
);
719 mutex_init(&cl_data
->lock
);
720 hsi_client_set_drvdata(cl
, cl_data
);
721 cdev_init(&cl_data
->cdev
, &hsc_fops
);
722 cl_data
->cdev
.owner
= THIS_MODULE
;
724 for (i
= 0, channel
= cl_data
->channels
; i
< HSC_DEVS
; i
++, channel
++) {
725 hsc_channel_init(channel
);
728 channel
->cl_data
= cl_data
;
731 /* 1 hsi client -> N char devices (one for each channel) */
732 ret
= cdev_add(&cl_data
->cdev
, hsc_dev
, HSC_DEVS
);
734 dev_err(dev
, "Could not add char device %d\n", ret
);
740 unregister_chrdev_region(hsc_dev
, HSC_DEVS
);
747 static int hsc_remove(struct device
*dev
)
749 struct hsi_client
*cl
= to_hsi_client(dev
);
750 struct hsc_client_data
*cl_data
= hsi_client_drvdata(cl
);
751 dev_t hsc_dev
= cl_data
->cdev
.dev
;
753 cdev_del(&cl_data
->cdev
);
754 unregister_chrdev_region(hsc_dev
, HSC_DEVS
);
755 hsi_client_set_drvdata(cl
, NULL
);
761 static struct hsi_client_driver hsc_driver
= {
764 .owner
= THIS_MODULE
,
766 .remove
= hsc_remove
,
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");
780 ret
= hsi_register_client_driver(&hsc_driver
);
782 pr_err("Error while registering HSI/SSI driver %d", ret
);
786 pr_info("HSI/SSI char device loaded\n");
790 module_init(hsc_init
);
792 static void __exit
hsc_exit(void)
794 hsi_unregister_client_driver(&hsc_driver
);
795 pr_info("HSI char device removed\n");
797 module_exit(hsc_exit
);
799 MODULE_AUTHOR("Andras Domokos <andras.domokos@nokia.com>");
800 MODULE_ALIAS("hsi:hsi_char");
801 MODULE_DESCRIPTION("HSI character device");
802 MODULE_LICENSE("GPL v2");