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 */
97 struct usb_device
*usb_dev
;
98 struct usb_interface
*interface
;
102 struct urb
*rx_urb
[DLN2_MAX_URBS
];
103 void *rx_buf
[DLN2_MAX_URBS
];
105 struct dln2_mod_rx_slots mod_rx_slots
[DLN2_HANDLES
];
107 struct list_head event_cb_list
;
108 spinlock_t event_cb_lock
;
111 int active_transfers
;
112 wait_queue_head_t disconnect_wq
;
113 spinlock_t disconnect_lock
;
116 struct dln2_event_cb_entry
{
117 struct list_head list
;
119 struct platform_device
*pdev
;
120 dln2_event_cb_t callback
;
123 int dln2_register_event_cb(struct platform_device
*pdev
, u16 id
,
124 dln2_event_cb_t event_cb
)
126 struct dln2_dev
*dln2
= dev_get_drvdata(pdev
->dev
.parent
);
127 struct dln2_event_cb_entry
*i
, *entry
;
131 entry
= kzalloc(sizeof(*entry
), GFP_KERNEL
);
136 entry
->callback
= event_cb
;
139 spin_lock_irqsave(&dln2
->event_cb_lock
, flags
);
141 list_for_each_entry(i
, &dln2
->event_cb_list
, list
) {
149 list_add_rcu(&entry
->list
, &dln2
->event_cb_list
);
151 spin_unlock_irqrestore(&dln2
->event_cb_lock
, flags
);
158 EXPORT_SYMBOL(dln2_register_event_cb
);
160 void dln2_unregister_event_cb(struct platform_device
*pdev
, u16 id
)
162 struct dln2_dev
*dln2
= dev_get_drvdata(pdev
->dev
.parent
);
163 struct dln2_event_cb_entry
*i
;
167 spin_lock_irqsave(&dln2
->event_cb_lock
, flags
);
169 list_for_each_entry(i
, &dln2
->event_cb_list
, list
) {
171 list_del_rcu(&i
->list
);
177 spin_unlock_irqrestore(&dln2
->event_cb_lock
, flags
);
184 EXPORT_SYMBOL(dln2_unregister_event_cb
);
187 * Returns true if a valid transfer slot is found. In this case the URB must not
188 * be resubmitted immediately in dln2_rx as we need the data when dln2_transfer
189 * is woke up. It will be resubmitted there.
191 static bool dln2_transfer_complete(struct dln2_dev
*dln2
, struct urb
*urb
,
192 u16 handle
, u16 rx_slot
)
194 struct device
*dev
= &dln2
->interface
->dev
;
195 struct dln2_mod_rx_slots
*rxs
= &dln2
->mod_rx_slots
[handle
];
196 struct dln2_rx_context
*rxc
;
197 bool valid_slot
= false;
199 if (rx_slot
>= DLN2_MAX_RX_SLOTS
)
202 rxc
= &rxs
->slots
[rx_slot
];
205 * No need to disable interrupts as this lock is not taken in interrupt
206 * context elsewhere in this driver. This function (or its callers) are
207 * also not exported to other modules.
209 spin_lock(&rxs
->lock
);
210 if (rxc
->in_use
&& !rxc
->urb
) {
212 complete(&rxc
->done
);
215 spin_unlock(&rxs
->lock
);
219 dev_warn(dev
, "bad/late response %d/%d\n", handle
, rx_slot
);
224 static void dln2_run_event_callbacks(struct dln2_dev
*dln2
, u16 id
, u16 echo
,
227 struct dln2_event_cb_entry
*i
;
231 list_for_each_entry_rcu(i
, &dln2
->event_cb_list
, list
) {
233 i
->callback(i
->pdev
, echo
, data
, len
);
241 static void dln2_rx(struct urb
*urb
)
243 struct dln2_dev
*dln2
= urb
->context
;
244 struct dln2_header
*hdr
= urb
->transfer_buffer
;
245 struct device
*dev
= &dln2
->interface
->dev
;
246 u16 id
, echo
, handle
, size
;
251 switch (urb
->status
) {
259 /* this urb is terminated, clean up */
260 dev_dbg(dev
, "urb shutting down with status %d\n", urb
->status
);
263 dev_dbg(dev
, "nonzero urb status received %d\n", urb
->status
);
267 if (urb
->actual_length
< sizeof(struct dln2_header
)) {
268 dev_err(dev
, "short response: %d\n", urb
->actual_length
);
272 handle
= le16_to_cpu(hdr
->handle
);
273 id
= le16_to_cpu(hdr
->id
);
274 echo
= le16_to_cpu(hdr
->echo
);
275 size
= le16_to_cpu(hdr
->size
);
277 if (size
!= urb
->actual_length
) {
278 dev_err(dev
, "size mismatch: handle %x cmd %x echo %x size %d actual %d\n",
279 handle
, id
, echo
, size
, urb
->actual_length
);
283 if (handle
>= DLN2_HANDLES
) {
284 dev_warn(dev
, "invalid handle %d\n", handle
);
288 data
= urb
->transfer_buffer
+ sizeof(struct dln2_header
);
289 len
= urb
->actual_length
- sizeof(struct dln2_header
);
291 if (handle
== DLN2_HANDLE_EVENT
) {
292 dln2_run_event_callbacks(dln2
, id
, echo
, data
, len
);
294 /* URB will be re-submitted in _dln2_transfer (free_rx_slot) */
295 if (dln2_transfer_complete(dln2
, urb
, handle
, echo
))
300 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
302 dev_err(dev
, "failed to resubmit RX URB: %d\n", err
);
305 static void *dln2_prep_buf(u16 handle
, u16 cmd
, u16 echo
, const void *obuf
,
306 int *obuf_len
, gfp_t gfp
)
310 struct dln2_header
*hdr
;
312 len
= *obuf_len
+ sizeof(*hdr
);
313 buf
= kmalloc(len
, gfp
);
317 hdr
= (struct dln2_header
*)buf
;
318 hdr
->id
= cpu_to_le16(cmd
);
319 hdr
->size
= cpu_to_le16(len
);
320 hdr
->echo
= cpu_to_le16(echo
);
321 hdr
->handle
= cpu_to_le16(handle
);
323 memcpy(buf
+ sizeof(*hdr
), obuf
, *obuf_len
);
330 static int dln2_send_wait(struct dln2_dev
*dln2
, u16 handle
, u16 cmd
, u16 echo
,
331 const void *obuf
, int obuf_len
)
338 buf
= dln2_prep_buf(handle
, cmd
, echo
, obuf
, &len
, GFP_KERNEL
);
342 ret
= usb_bulk_msg(dln2
->usb_dev
,
343 usb_sndbulkpipe(dln2
->usb_dev
, dln2
->ep_out
),
344 buf
, len
, &actual
, DLN2_USB_TIMEOUT
);
351 static bool find_free_slot(struct dln2_dev
*dln2
, u16 handle
, int *slot
)
353 struct dln2_mod_rx_slots
*rxs
;
356 if (dln2
->disconnect
) {
361 rxs
= &dln2
->mod_rx_slots
[handle
];
363 spin_lock_irqsave(&rxs
->lock
, flags
);
365 *slot
= find_first_zero_bit(rxs
->bmap
, DLN2_MAX_RX_SLOTS
);
367 if (*slot
< DLN2_MAX_RX_SLOTS
) {
368 struct dln2_rx_context
*rxc
= &rxs
->slots
[*slot
];
370 set_bit(*slot
, rxs
->bmap
);
374 spin_unlock_irqrestore(&rxs
->lock
, flags
);
376 return *slot
< DLN2_MAX_RX_SLOTS
;
379 static int alloc_rx_slot(struct dln2_dev
*dln2
, u16 handle
)
385 * No need to timeout here, the wait is bounded by the timeout in
388 ret
= wait_event_interruptible(dln2
->mod_rx_slots
[handle
].wq
,
389 find_free_slot(dln2
, handle
, &slot
));
396 static void free_rx_slot(struct dln2_dev
*dln2
, u16 handle
, int slot
)
398 struct dln2_mod_rx_slots
*rxs
;
399 struct urb
*urb
= NULL
;
401 struct dln2_rx_context
*rxc
;
403 rxs
= &dln2
->mod_rx_slots
[handle
];
405 spin_lock_irqsave(&rxs
->lock
, flags
);
407 clear_bit(slot
, rxs
->bmap
);
409 rxc
= &rxs
->slots
[slot
];
413 reinit_completion(&rxc
->done
);
415 spin_unlock_irqrestore(&rxs
->lock
, flags
);
419 struct device
*dev
= &dln2
->interface
->dev
;
421 err
= usb_submit_urb(urb
, GFP_KERNEL
);
423 dev_err(dev
, "failed to resubmit RX URB: %d\n", err
);
426 wake_up_interruptible(&rxs
->wq
);
429 static int _dln2_transfer(struct dln2_dev
*dln2
, u16 handle
, u16 cmd
,
430 const void *obuf
, unsigned obuf_len
,
431 void *ibuf
, unsigned *ibuf_len
)
435 struct dln2_response
*rsp
;
436 struct dln2_rx_context
*rxc
;
437 struct device
*dev
= &dln2
->interface
->dev
;
438 const unsigned long timeout
= msecs_to_jiffies(DLN2_USB_TIMEOUT
);
439 struct dln2_mod_rx_slots
*rxs
= &dln2
->mod_rx_slots
[handle
];
442 spin_lock(&dln2
->disconnect_lock
);
443 if (!dln2
->disconnect
)
444 dln2
->active_transfers
++;
447 spin_unlock(&dln2
->disconnect_lock
);
452 rx_slot
= alloc_rx_slot(dln2
, handle
);
458 ret
= dln2_send_wait(dln2
, handle
, cmd
, rx_slot
, obuf
, obuf_len
);
460 dev_err(dev
, "USB write failed: %d\n", ret
);
461 goto out_free_rx_slot
;
464 rxc
= &rxs
->slots
[rx_slot
];
466 ret
= wait_for_completion_interruptible_timeout(&rxc
->done
, timeout
);
470 goto out_free_rx_slot
;
475 if (dln2
->disconnect
) {
477 goto out_free_rx_slot
;
480 /* if we got here we know that the response header has been checked */
481 rsp
= rxc
->urb
->transfer_buffer
;
482 size
= le16_to_cpu(rsp
->hdr
.size
);
484 if (size
< sizeof(*rsp
)) {
486 goto out_free_rx_slot
;
489 if (le16_to_cpu(rsp
->result
) > 0x80) {
490 dev_dbg(dev
, "%d received response with error %d\n",
491 handle
, le16_to_cpu(rsp
->result
));
493 goto out_free_rx_slot
;
497 goto out_free_rx_slot
;
499 if (*ibuf_len
> size
- sizeof(*rsp
))
500 *ibuf_len
= size
- sizeof(*rsp
);
502 memcpy(ibuf
, rsp
+ 1, *ibuf_len
);
505 free_rx_slot(dln2
, handle
, rx_slot
);
507 spin_lock(&dln2
->disconnect_lock
);
508 dln2
->active_transfers
--;
509 spin_unlock(&dln2
->disconnect_lock
);
510 if (dln2
->disconnect
)
511 wake_up(&dln2
->disconnect_wq
);
516 int dln2_transfer(struct platform_device
*pdev
, u16 cmd
,
517 const void *obuf
, unsigned obuf_len
,
518 void *ibuf
, unsigned *ibuf_len
)
520 struct dln2_platform_data
*dln2_pdata
;
521 struct dln2_dev
*dln2
;
524 dln2
= dev_get_drvdata(pdev
->dev
.parent
);
525 dln2_pdata
= dev_get_platdata(&pdev
->dev
);
526 handle
= dln2_pdata
->handle
;
528 return _dln2_transfer(dln2
, handle
, cmd
, obuf
, obuf_len
, ibuf
,
531 EXPORT_SYMBOL(dln2_transfer
);
533 static int dln2_check_hw(struct dln2_dev
*dln2
)
537 int len
= sizeof(hw_type
);
539 ret
= _dln2_transfer(dln2
, DLN2_HANDLE_CTRL
, CMD_GET_DEVICE_VER
,
540 NULL
, 0, &hw_type
, &len
);
543 if (len
< sizeof(hw_type
))
546 if (le32_to_cpu(hw_type
) != DLN2_HW_ID
) {
547 dev_err(&dln2
->interface
->dev
, "Device ID 0x%x not supported\n",
548 le32_to_cpu(hw_type
));
555 static int dln2_print_serialno(struct dln2_dev
*dln2
)
559 int len
= sizeof(serial_no
);
560 struct device
*dev
= &dln2
->interface
->dev
;
562 ret
= _dln2_transfer(dln2
, DLN2_HANDLE_CTRL
, CMD_GET_DEVICE_SN
, NULL
, 0,
566 if (len
< sizeof(serial_no
))
569 dev_info(dev
, "Diolan DLN2 serial %u\n", le32_to_cpu(serial_no
));
574 static int dln2_hw_init(struct dln2_dev
*dln2
)
578 ret
= dln2_check_hw(dln2
);
582 return dln2_print_serialno(dln2
);
585 static void dln2_free_rx_urbs(struct dln2_dev
*dln2
)
589 for (i
= 0; i
< DLN2_MAX_URBS
; i
++) {
590 usb_free_urb(dln2
->rx_urb
[i
]);
591 kfree(dln2
->rx_buf
[i
]);
595 static void dln2_stop_rx_urbs(struct dln2_dev
*dln2
)
599 for (i
= 0; i
< DLN2_MAX_URBS
; i
++)
600 usb_kill_urb(dln2
->rx_urb
[i
]);
603 static void dln2_free(struct dln2_dev
*dln2
)
605 dln2_free_rx_urbs(dln2
);
606 usb_put_dev(dln2
->usb_dev
);
610 static int dln2_setup_rx_urbs(struct dln2_dev
*dln2
,
611 struct usb_host_interface
*hostif
)
614 const int rx_max_size
= DLN2_RX_BUF_SIZE
;
616 for (i
= 0; i
< DLN2_MAX_URBS
; i
++) {
617 dln2
->rx_buf
[i
] = kmalloc(rx_max_size
, GFP_KERNEL
);
618 if (!dln2
->rx_buf
[i
])
621 dln2
->rx_urb
[i
] = usb_alloc_urb(0, GFP_KERNEL
);
622 if (!dln2
->rx_urb
[i
])
625 usb_fill_bulk_urb(dln2
->rx_urb
[i
], dln2
->usb_dev
,
626 usb_rcvbulkpipe(dln2
->usb_dev
, dln2
->ep_in
),
627 dln2
->rx_buf
[i
], rx_max_size
, dln2_rx
, dln2
);
633 static int dln2_start_rx_urbs(struct dln2_dev
*dln2
, gfp_t gfp
)
635 struct device
*dev
= &dln2
->interface
->dev
;
639 for (i
= 0; i
< DLN2_MAX_URBS
; i
++) {
640 ret
= usb_submit_urb(dln2
->rx_urb
[i
], gfp
);
642 dev_err(dev
, "failed to submit RX URB: %d\n", ret
);
650 static struct dln2_platform_data dln2_pdata_gpio
= {
651 .handle
= DLN2_HANDLE_GPIO
,
654 /* Only one I2C port seems to be supported on current hardware */
655 static struct dln2_platform_data dln2_pdata_i2c
= {
656 .handle
= DLN2_HANDLE_I2C
,
660 /* Only one SPI port supported */
661 static struct dln2_platform_data dln2_pdata_spi
= {
662 .handle
= DLN2_HANDLE_SPI
,
666 static const struct mfd_cell dln2_devs
[] = {
669 .platform_data
= &dln2_pdata_gpio
,
670 .pdata_size
= sizeof(struct dln2_platform_data
),
674 .platform_data
= &dln2_pdata_i2c
,
675 .pdata_size
= sizeof(struct dln2_platform_data
),
679 .platform_data
= &dln2_pdata_spi
,
680 .pdata_size
= sizeof(struct dln2_platform_data
),
684 static void dln2_stop(struct dln2_dev
*dln2
)
688 /* don't allow starting new transfers */
689 spin_lock(&dln2
->disconnect_lock
);
690 dln2
->disconnect
= true;
691 spin_unlock(&dln2
->disconnect_lock
);
693 /* cancel in progress transfers */
694 for (i
= 0; i
< DLN2_HANDLES
; i
++) {
695 struct dln2_mod_rx_slots
*rxs
= &dln2
->mod_rx_slots
[i
];
698 spin_lock_irqsave(&rxs
->lock
, flags
);
700 /* cancel all response waiters */
701 for (j
= 0; j
< DLN2_MAX_RX_SLOTS
; j
++) {
702 struct dln2_rx_context
*rxc
= &rxs
->slots
[j
];
705 complete(&rxc
->done
);
708 spin_unlock_irqrestore(&rxs
->lock
, flags
);
711 /* wait for transfers to end */
712 wait_event(dln2
->disconnect_wq
, !dln2
->active_transfers
);
714 dln2_stop_rx_urbs(dln2
);
717 static void dln2_disconnect(struct usb_interface
*interface
)
719 struct dln2_dev
*dln2
= usb_get_intfdata(interface
);
723 mfd_remove_devices(&interface
->dev
);
728 static int dln2_probe(struct usb_interface
*interface
,
729 const struct usb_device_id
*usb_id
)
731 struct usb_host_interface
*hostif
= interface
->cur_altsetting
;
732 struct device
*dev
= &interface
->dev
;
733 struct dln2_dev
*dln2
;
737 if (hostif
->desc
.bInterfaceNumber
!= 0 ||
738 hostif
->desc
.bNumEndpoints
< 2)
741 dln2
= kzalloc(sizeof(*dln2
), GFP_KERNEL
);
745 dln2
->ep_out
= hostif
->endpoint
[0].desc
.bEndpointAddress
;
746 dln2
->ep_in
= hostif
->endpoint
[1].desc
.bEndpointAddress
;
747 dln2
->usb_dev
= usb_get_dev(interface_to_usbdev(interface
));
748 dln2
->interface
= interface
;
749 usb_set_intfdata(interface
, dln2
);
750 init_waitqueue_head(&dln2
->disconnect_wq
);
752 for (i
= 0; i
< DLN2_HANDLES
; i
++) {
753 init_waitqueue_head(&dln2
->mod_rx_slots
[i
].wq
);
754 spin_lock_init(&dln2
->mod_rx_slots
[i
].lock
);
755 for (j
= 0; j
< DLN2_MAX_RX_SLOTS
; j
++)
756 init_completion(&dln2
->mod_rx_slots
[i
].slots
[j
].done
);
759 spin_lock_init(&dln2
->event_cb_lock
);
760 spin_lock_init(&dln2
->disconnect_lock
);
761 INIT_LIST_HEAD(&dln2
->event_cb_list
);
763 ret
= dln2_setup_rx_urbs(dln2
, hostif
);
767 ret
= dln2_start_rx_urbs(dln2
, GFP_KERNEL
);
771 ret
= dln2_hw_init(dln2
);
773 dev_err(dev
, "failed to initialize hardware\n");
777 ret
= mfd_add_hotplug_devices(dev
, dln2_devs
, ARRAY_SIZE(dln2_devs
));
779 dev_err(dev
, "failed to add mfd devices to core\n");
786 dln2_stop_rx_urbs(dln2
);
794 static int dln2_suspend(struct usb_interface
*iface
, pm_message_t message
)
796 struct dln2_dev
*dln2
= usb_get_intfdata(iface
);
803 static int dln2_resume(struct usb_interface
*iface
)
805 struct dln2_dev
*dln2
= usb_get_intfdata(iface
);
807 dln2
->disconnect
= false;
809 return dln2_start_rx_urbs(dln2
, GFP_NOIO
);
812 static const struct usb_device_id dln2_table
[] = {
813 { USB_DEVICE(0xa257, 0x2013) },
817 MODULE_DEVICE_TABLE(usb
, dln2_table
);
819 static struct usb_driver dln2_driver
= {
822 .disconnect
= dln2_disconnect
,
823 .id_table
= dln2_table
,
824 .suspend
= dln2_suspend
,
825 .resume
= dln2_resume
,
828 module_usb_driver(dln2_driver
);
830 MODULE_AUTHOR("Octavian Purdila <octavian.purdila@intel.com>");
831 MODULE_DESCRIPTION("Core driver for the Diolan DLN2 interface adapter");
832 MODULE_LICENSE("GPL v2");