2 * Driver for the Diolan DLN-2 USB adapter
4 * Copyright (c) 2014 Intel Corporation
8 * Copyright (c) 2010-2011 Ericsson AB
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation, version 2.
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/types.h>
18 #include <linux/slab.h>
19 #include <linux/usb.h>
20 #include <linux/i2c.h>
21 #include <linux/mutex.h>
22 #include <linux/platform_device.h>
23 #include <linux/mfd/core.h>
24 #include <linux/mfd/dln2.h>
25 #include <linux/rculist.h>
34 struct dln2_response
{
35 struct dln2_header hdr
;
39 #define DLN2_GENERIC_MODULE_ID 0x00
40 #define DLN2_GENERIC_CMD(cmd) DLN2_CMD(cmd, DLN2_GENERIC_MODULE_ID)
41 #define CMD_GET_DEVICE_VER DLN2_GENERIC_CMD(0x30)
42 #define CMD_GET_DEVICE_SN DLN2_GENERIC_CMD(0x31)
44 #define DLN2_HW_ID 0x200
45 #define DLN2_USB_TIMEOUT 200 /* in ms */
46 #define DLN2_MAX_RX_SLOTS 16
47 #define DLN2_MAX_URBS 16
48 #define DLN2_RX_BUF_SIZE 512
51 DLN2_HANDLE_EVENT
= 0, /* don't change, hardware defined */
60 * Receive context used between the receive demultiplexer and the transfer
61 * routine. While sending a request the transfer routine will look for a free
62 * receive context and use it to wait for a response and to receive the URB and
63 * thus the response data.
65 struct dln2_rx_context
{
66 /* completion used to wait for a response */
67 struct completion done
;
69 /* if non-NULL the URB contains the response */
72 /* if true then this context is used to wait for a response */
77 * Receive contexts for a particular DLN2 module (i2c, gpio, etc.). We use the
78 * handle header field to identify the module in dln2_dev.mod_rx_slots and then
79 * the echo header field to index the slots field and find the receive context
80 * for a particular request.
82 struct dln2_mod_rx_slots
{
84 DECLARE_BITMAP(bmap
, DLN2_MAX_RX_SLOTS
);
86 /* used to wait for a free RX slot */
89 /* used to wait for an RX operation to complete */
90 struct dln2_rx_context slots
[DLN2_MAX_RX_SLOTS
];
92 /* avoid races between alloc/free_rx_slot and dln2_rx_transfer */
102 struct usb_device
*usb_dev
;
103 struct usb_interface
*interface
;
107 struct urb
*rx_urb
[DLN2_MAX_URBS
];
108 void *rx_buf
[DLN2_MAX_URBS
];
110 struct dln2_mod_rx_slots mod_rx_slots
[DLN2_HANDLES
];
112 struct list_head event_cb_list
;
113 spinlock_t event_cb_lock
;
116 int active_transfers
;
117 wait_queue_head_t disconnect_wq
;
118 spinlock_t disconnect_lock
;
121 struct dln2_event_cb_entry
{
122 struct list_head list
;
124 struct platform_device
*pdev
;
125 dln2_event_cb_t callback
;
128 int dln2_register_event_cb(struct platform_device
*pdev
, u16 id
,
129 dln2_event_cb_t event_cb
)
131 struct dln2_dev
*dln2
= dev_get_drvdata(pdev
->dev
.parent
);
132 struct dln2_event_cb_entry
*i
, *entry
;
136 entry
= kzalloc(sizeof(*entry
), GFP_KERNEL
);
141 entry
->callback
= event_cb
;
144 spin_lock_irqsave(&dln2
->event_cb_lock
, flags
);
146 list_for_each_entry(i
, &dln2
->event_cb_list
, list
) {
154 list_add_rcu(&entry
->list
, &dln2
->event_cb_list
);
156 spin_unlock_irqrestore(&dln2
->event_cb_lock
, flags
);
163 EXPORT_SYMBOL(dln2_register_event_cb
);
165 void dln2_unregister_event_cb(struct platform_device
*pdev
, u16 id
)
167 struct dln2_dev
*dln2
= dev_get_drvdata(pdev
->dev
.parent
);
168 struct dln2_event_cb_entry
*i
;
172 spin_lock_irqsave(&dln2
->event_cb_lock
, flags
);
174 list_for_each_entry(i
, &dln2
->event_cb_list
, list
) {
176 list_del_rcu(&i
->list
);
182 spin_unlock_irqrestore(&dln2
->event_cb_lock
, flags
);
189 EXPORT_SYMBOL(dln2_unregister_event_cb
);
192 * Returns true if a valid transfer slot is found. In this case the URB must not
193 * be resubmitted immediately in dln2_rx as we need the data when dln2_transfer
194 * is woke up. It will be resubmitted there.
196 static bool dln2_transfer_complete(struct dln2_dev
*dln2
, struct urb
*urb
,
197 u16 handle
, u16 rx_slot
)
199 struct device
*dev
= &dln2
->interface
->dev
;
200 struct dln2_mod_rx_slots
*rxs
= &dln2
->mod_rx_slots
[handle
];
201 struct dln2_rx_context
*rxc
;
203 bool valid_slot
= false;
205 if (rx_slot
>= DLN2_MAX_RX_SLOTS
)
208 rxc
= &rxs
->slots
[rx_slot
];
210 spin_lock_irqsave(&rxs
->lock
, flags
);
211 if (rxc
->in_use
&& !rxc
->urb
) {
213 complete(&rxc
->done
);
216 spin_unlock_irqrestore(&rxs
->lock
, flags
);
220 dev_warn(dev
, "bad/late response %d/%d\n", handle
, rx_slot
);
225 static void dln2_run_event_callbacks(struct dln2_dev
*dln2
, u16 id
, u16 echo
,
228 struct dln2_event_cb_entry
*i
;
232 list_for_each_entry_rcu(i
, &dln2
->event_cb_list
, list
) {
234 i
->callback(i
->pdev
, echo
, data
, len
);
242 static void dln2_rx(struct urb
*urb
)
244 struct dln2_dev
*dln2
= urb
->context
;
245 struct dln2_header
*hdr
= urb
->transfer_buffer
;
246 struct device
*dev
= &dln2
->interface
->dev
;
247 u16 id
, echo
, handle
, size
;
252 switch (urb
->status
) {
260 /* this urb is terminated, clean up */
261 dev_dbg(dev
, "urb shutting down with status %d\n", urb
->status
);
264 dev_dbg(dev
, "nonzero urb status received %d\n", urb
->status
);
268 if (urb
->actual_length
< sizeof(struct dln2_header
)) {
269 dev_err(dev
, "short response: %d\n", urb
->actual_length
);
273 handle
= le16_to_cpu(hdr
->handle
);
274 id
= le16_to_cpu(hdr
->id
);
275 echo
= le16_to_cpu(hdr
->echo
);
276 size
= le16_to_cpu(hdr
->size
);
278 if (size
!= urb
->actual_length
) {
279 dev_err(dev
, "size mismatch: handle %x cmd %x echo %x size %d actual %d\n",
280 handle
, id
, echo
, size
, urb
->actual_length
);
284 if (handle
>= DLN2_HANDLES
) {
285 dev_warn(dev
, "invalid handle %d\n", handle
);
289 data
= urb
->transfer_buffer
+ sizeof(struct dln2_header
);
290 len
= urb
->actual_length
- sizeof(struct dln2_header
);
292 if (handle
== DLN2_HANDLE_EVENT
) {
293 dln2_run_event_callbacks(dln2
, id
, echo
, data
, len
);
295 /* URB will be re-submitted in _dln2_transfer (free_rx_slot) */
296 if (dln2_transfer_complete(dln2
, urb
, handle
, echo
))
301 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
303 dev_err(dev
, "failed to resubmit RX URB: %d\n", err
);
306 static void *dln2_prep_buf(u16 handle
, u16 cmd
, u16 echo
, const void *obuf
,
307 int *obuf_len
, gfp_t gfp
)
311 struct dln2_header
*hdr
;
313 len
= *obuf_len
+ sizeof(*hdr
);
314 buf
= kmalloc(len
, gfp
);
318 hdr
= (struct dln2_header
*)buf
;
319 hdr
->id
= cpu_to_le16(cmd
);
320 hdr
->size
= cpu_to_le16(len
);
321 hdr
->echo
= cpu_to_le16(echo
);
322 hdr
->handle
= cpu_to_le16(handle
);
324 memcpy(buf
+ sizeof(*hdr
), obuf
, *obuf_len
);
331 static int dln2_send_wait(struct dln2_dev
*dln2
, u16 handle
, u16 cmd
, u16 echo
,
332 const void *obuf
, int obuf_len
)
339 buf
= dln2_prep_buf(handle
, cmd
, echo
, obuf
, &len
, GFP_KERNEL
);
343 ret
= usb_bulk_msg(dln2
->usb_dev
,
344 usb_sndbulkpipe(dln2
->usb_dev
, dln2
->ep_out
),
345 buf
, len
, &actual
, DLN2_USB_TIMEOUT
);
352 static bool find_free_slot(struct dln2_dev
*dln2
, u16 handle
, int *slot
)
354 struct dln2_mod_rx_slots
*rxs
;
357 if (dln2
->disconnect
) {
362 rxs
= &dln2
->mod_rx_slots
[handle
];
364 spin_lock_irqsave(&rxs
->lock
, flags
);
366 *slot
= find_first_zero_bit(rxs
->bmap
, DLN2_MAX_RX_SLOTS
);
368 if (*slot
< DLN2_MAX_RX_SLOTS
) {
369 struct dln2_rx_context
*rxc
= &rxs
->slots
[*slot
];
371 set_bit(*slot
, rxs
->bmap
);
375 spin_unlock_irqrestore(&rxs
->lock
, flags
);
377 return *slot
< DLN2_MAX_RX_SLOTS
;
380 static int alloc_rx_slot(struct dln2_dev
*dln2
, u16 handle
)
386 * No need to timeout here, the wait is bounded by the timeout in
389 ret
= wait_event_interruptible(dln2
->mod_rx_slots
[handle
].wq
,
390 find_free_slot(dln2
, handle
, &slot
));
397 static void free_rx_slot(struct dln2_dev
*dln2
, u16 handle
, int slot
)
399 struct dln2_mod_rx_slots
*rxs
;
400 struct urb
*urb
= NULL
;
402 struct dln2_rx_context
*rxc
;
404 rxs
= &dln2
->mod_rx_slots
[handle
];
406 spin_lock_irqsave(&rxs
->lock
, flags
);
408 clear_bit(slot
, rxs
->bmap
);
410 rxc
= &rxs
->slots
[slot
];
414 reinit_completion(&rxc
->done
);
416 spin_unlock_irqrestore(&rxs
->lock
, flags
);
420 struct device
*dev
= &dln2
->interface
->dev
;
422 err
= usb_submit_urb(urb
, GFP_KERNEL
);
424 dev_err(dev
, "failed to resubmit RX URB: %d\n", err
);
427 wake_up_interruptible(&rxs
->wq
);
430 static int _dln2_transfer(struct dln2_dev
*dln2
, u16 handle
, u16 cmd
,
431 const void *obuf
, unsigned obuf_len
,
432 void *ibuf
, unsigned *ibuf_len
)
436 struct dln2_response
*rsp
;
437 struct dln2_rx_context
*rxc
;
438 struct device
*dev
= &dln2
->interface
->dev
;
439 const unsigned long timeout
= msecs_to_jiffies(DLN2_USB_TIMEOUT
);
440 struct dln2_mod_rx_slots
*rxs
= &dln2
->mod_rx_slots
[handle
];
443 spin_lock(&dln2
->disconnect_lock
);
444 if (!dln2
->disconnect
)
445 dln2
->active_transfers
++;
448 spin_unlock(&dln2
->disconnect_lock
);
453 rx_slot
= alloc_rx_slot(dln2
, handle
);
459 ret
= dln2_send_wait(dln2
, handle
, cmd
, rx_slot
, obuf
, obuf_len
);
461 dev_err(dev
, "USB write failed: %d\n", ret
);
462 goto out_free_rx_slot
;
465 rxc
= &rxs
->slots
[rx_slot
];
467 ret
= wait_for_completion_interruptible_timeout(&rxc
->done
, timeout
);
471 goto out_free_rx_slot
;
476 if (dln2
->disconnect
) {
478 goto out_free_rx_slot
;
481 /* if we got here we know that the response header has been checked */
482 rsp
= rxc
->urb
->transfer_buffer
;
483 size
= le16_to_cpu(rsp
->hdr
.size
);
485 if (size
< sizeof(*rsp
)) {
487 goto out_free_rx_slot
;
490 if (le16_to_cpu(rsp
->result
) > 0x80) {
491 dev_dbg(dev
, "%d received response with error %d\n",
492 handle
, le16_to_cpu(rsp
->result
));
494 goto out_free_rx_slot
;
498 goto out_free_rx_slot
;
500 if (*ibuf_len
> size
- sizeof(*rsp
))
501 *ibuf_len
= size
- sizeof(*rsp
);
503 memcpy(ibuf
, rsp
+ 1, *ibuf_len
);
506 free_rx_slot(dln2
, handle
, rx_slot
);
508 spin_lock(&dln2
->disconnect_lock
);
509 dln2
->active_transfers
--;
510 spin_unlock(&dln2
->disconnect_lock
);
511 if (dln2
->disconnect
)
512 wake_up(&dln2
->disconnect_wq
);
517 int dln2_transfer(struct platform_device
*pdev
, u16 cmd
,
518 const void *obuf
, unsigned obuf_len
,
519 void *ibuf
, unsigned *ibuf_len
)
521 struct dln2_platform_data
*dln2_pdata
;
522 struct dln2_dev
*dln2
;
525 dln2
= dev_get_drvdata(pdev
->dev
.parent
);
526 dln2_pdata
= dev_get_platdata(&pdev
->dev
);
527 handle
= dln2_pdata
->handle
;
529 return _dln2_transfer(dln2
, handle
, cmd
, obuf
, obuf_len
, ibuf
,
532 EXPORT_SYMBOL(dln2_transfer
);
534 static int dln2_check_hw(struct dln2_dev
*dln2
)
538 int len
= sizeof(hw_type
);
540 ret
= _dln2_transfer(dln2
, DLN2_HANDLE_CTRL
, CMD_GET_DEVICE_VER
,
541 NULL
, 0, &hw_type
, &len
);
544 if (len
< sizeof(hw_type
))
547 if (le32_to_cpu(hw_type
) != DLN2_HW_ID
) {
548 dev_err(&dln2
->interface
->dev
, "Device ID 0x%x not supported\n",
549 le32_to_cpu(hw_type
));
556 static int dln2_print_serialno(struct dln2_dev
*dln2
)
560 int len
= sizeof(serial_no
);
561 struct device
*dev
= &dln2
->interface
->dev
;
563 ret
= _dln2_transfer(dln2
, DLN2_HANDLE_CTRL
, CMD_GET_DEVICE_SN
, NULL
, 0,
567 if (len
< sizeof(serial_no
))
570 dev_info(dev
, "Diolan DLN2 serial %u\n", le32_to_cpu(serial_no
));
575 static int dln2_hw_init(struct dln2_dev
*dln2
)
579 ret
= dln2_check_hw(dln2
);
583 return dln2_print_serialno(dln2
);
586 static void dln2_free_rx_urbs(struct dln2_dev
*dln2
)
590 for (i
= 0; i
< DLN2_MAX_URBS
; i
++) {
591 usb_free_urb(dln2
->rx_urb
[i
]);
592 kfree(dln2
->rx_buf
[i
]);
596 static void dln2_stop_rx_urbs(struct dln2_dev
*dln2
)
600 for (i
= 0; i
< DLN2_MAX_URBS
; i
++)
601 usb_kill_urb(dln2
->rx_urb
[i
]);
604 static void dln2_free(struct dln2_dev
*dln2
)
606 dln2_free_rx_urbs(dln2
);
607 usb_put_dev(dln2
->usb_dev
);
611 static int dln2_setup_rx_urbs(struct dln2_dev
*dln2
,
612 struct usb_host_interface
*hostif
)
615 const int rx_max_size
= DLN2_RX_BUF_SIZE
;
617 for (i
= 0; i
< DLN2_MAX_URBS
; i
++) {
618 dln2
->rx_buf
[i
] = kmalloc(rx_max_size
, GFP_KERNEL
);
619 if (!dln2
->rx_buf
[i
])
622 dln2
->rx_urb
[i
] = usb_alloc_urb(0, GFP_KERNEL
);
623 if (!dln2
->rx_urb
[i
])
626 usb_fill_bulk_urb(dln2
->rx_urb
[i
], dln2
->usb_dev
,
627 usb_rcvbulkpipe(dln2
->usb_dev
, dln2
->ep_in
),
628 dln2
->rx_buf
[i
], rx_max_size
, dln2_rx
, dln2
);
634 static int dln2_start_rx_urbs(struct dln2_dev
*dln2
, gfp_t gfp
)
636 struct device
*dev
= &dln2
->interface
->dev
;
640 for (i
= 0; i
< DLN2_MAX_URBS
; i
++) {
641 ret
= usb_submit_urb(dln2
->rx_urb
[i
], gfp
);
643 dev_err(dev
, "failed to submit RX URB: %d\n", ret
);
651 static struct dln2_platform_data dln2_pdata_gpio
= {
652 .handle
= DLN2_HANDLE_GPIO
,
655 /* Only one I2C port seems to be supported on current hardware */
656 static struct dln2_platform_data dln2_pdata_i2c
= {
657 .handle
= DLN2_HANDLE_I2C
,
661 /* Only one SPI port supported */
662 static struct dln2_platform_data dln2_pdata_spi
= {
663 .handle
= DLN2_HANDLE_SPI
,
667 static const struct mfd_cell dln2_devs
[] = {
670 .platform_data
= &dln2_pdata_gpio
,
671 .pdata_size
= sizeof(struct dln2_platform_data
),
675 .platform_data
= &dln2_pdata_i2c
,
676 .pdata_size
= sizeof(struct dln2_platform_data
),
680 .platform_data
= &dln2_pdata_spi
,
681 .pdata_size
= sizeof(struct dln2_platform_data
),
685 static void dln2_stop(struct dln2_dev
*dln2
)
689 /* don't allow starting new transfers */
690 spin_lock(&dln2
->disconnect_lock
);
691 dln2
->disconnect
= true;
692 spin_unlock(&dln2
->disconnect_lock
);
694 /* cancel in progress transfers */
695 for (i
= 0; i
< DLN2_HANDLES
; i
++) {
696 struct dln2_mod_rx_slots
*rxs
= &dln2
->mod_rx_slots
[i
];
699 spin_lock_irqsave(&rxs
->lock
, flags
);
701 /* cancel all response waiters */
702 for (j
= 0; j
< DLN2_MAX_RX_SLOTS
; j
++) {
703 struct dln2_rx_context
*rxc
= &rxs
->slots
[j
];
706 complete(&rxc
->done
);
709 spin_unlock_irqrestore(&rxs
->lock
, flags
);
712 /* wait for transfers to end */
713 wait_event(dln2
->disconnect_wq
, !dln2
->active_transfers
);
715 dln2_stop_rx_urbs(dln2
);
718 static void dln2_disconnect(struct usb_interface
*interface
)
720 struct dln2_dev
*dln2
= usb_get_intfdata(interface
);
724 mfd_remove_devices(&interface
->dev
);
729 static int dln2_probe(struct usb_interface
*interface
,
730 const struct usb_device_id
*usb_id
)
732 struct usb_host_interface
*hostif
= interface
->cur_altsetting
;
733 struct usb_endpoint_descriptor
*epin
;
734 struct usb_endpoint_descriptor
*epout
;
735 struct device
*dev
= &interface
->dev
;
736 struct dln2_dev
*dln2
;
740 if (hostif
->desc
.bInterfaceNumber
!= 0 ||
741 hostif
->desc
.bNumEndpoints
< 2)
744 epout
= &hostif
->endpoint
[DLN2_EP_OUT
].desc
;
745 if (!usb_endpoint_is_bulk_out(epout
))
747 epin
= &hostif
->endpoint
[DLN2_EP_IN
].desc
;
748 if (!usb_endpoint_is_bulk_in(epin
))
751 dln2
= kzalloc(sizeof(*dln2
), GFP_KERNEL
);
755 dln2
->ep_out
= epout
->bEndpointAddress
;
756 dln2
->ep_in
= epin
->bEndpointAddress
;
757 dln2
->usb_dev
= usb_get_dev(interface_to_usbdev(interface
));
758 dln2
->interface
= interface
;
759 usb_set_intfdata(interface
, dln2
);
760 init_waitqueue_head(&dln2
->disconnect_wq
);
762 for (i
= 0; i
< DLN2_HANDLES
; i
++) {
763 init_waitqueue_head(&dln2
->mod_rx_slots
[i
].wq
);
764 spin_lock_init(&dln2
->mod_rx_slots
[i
].lock
);
765 for (j
= 0; j
< DLN2_MAX_RX_SLOTS
; j
++)
766 init_completion(&dln2
->mod_rx_slots
[i
].slots
[j
].done
);
769 spin_lock_init(&dln2
->event_cb_lock
);
770 spin_lock_init(&dln2
->disconnect_lock
);
771 INIT_LIST_HEAD(&dln2
->event_cb_list
);
773 ret
= dln2_setup_rx_urbs(dln2
, hostif
);
777 ret
= dln2_start_rx_urbs(dln2
, GFP_KERNEL
);
781 ret
= dln2_hw_init(dln2
);
783 dev_err(dev
, "failed to initialize hardware\n");
787 ret
= mfd_add_hotplug_devices(dev
, dln2_devs
, ARRAY_SIZE(dln2_devs
));
789 dev_err(dev
, "failed to add mfd devices to core\n");
796 dln2_stop_rx_urbs(dln2
);
804 static int dln2_suspend(struct usb_interface
*iface
, pm_message_t message
)
806 struct dln2_dev
*dln2
= usb_get_intfdata(iface
);
813 static int dln2_resume(struct usb_interface
*iface
)
815 struct dln2_dev
*dln2
= usb_get_intfdata(iface
);
817 dln2
->disconnect
= false;
819 return dln2_start_rx_urbs(dln2
, GFP_NOIO
);
822 static const struct usb_device_id dln2_table
[] = {
823 { USB_DEVICE(0xa257, 0x2013) },
827 MODULE_DEVICE_TABLE(usb
, dln2_table
);
829 static struct usb_driver dln2_driver
= {
832 .disconnect
= dln2_disconnect
,
833 .id_table
= dln2_table
,
834 .suspend
= dln2_suspend
,
835 .resume
= dln2_resume
,
838 module_usb_driver(dln2_driver
);
840 MODULE_AUTHOR("Octavian Purdila <octavian.purdila@intel.com>");
841 MODULE_DESCRIPTION("Core driver for the Diolan DLN2 interface adapter");
842 MODULE_LICENSE("GPL v2");