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;
202 bool valid_slot = false;
204 if (rx_slot >= DLN2_MAX_RX_SLOTS)
207 rxc = &rxs->slots[rx_slot];
210 * No need to disable interrupts as this lock is not taken in interrupt
211 * context elsewhere in this driver. This function (or its callers) are
212 * also not exported to other modules.
214 spin_lock(&rxs->lock);
215 if (rxc->in_use && !rxc->urb) {
217 complete(&rxc->done);
220 spin_unlock(&rxs->lock);
224 dev_warn(dev, "bad/late response %d/%d\n", handle, rx_slot);
229 static void dln2_run_event_callbacks(struct dln2_dev *dln2, u16 id, u16 echo,
232 struct dln2_event_cb_entry *i;
236 list_for_each_entry_rcu(i, &dln2->event_cb_list, list) {
238 i->callback(i->pdev, echo, data, len);
246 static void dln2_rx(struct urb *urb)
248 struct dln2_dev *dln2 = urb->context;
249 struct dln2_header *hdr = urb->transfer_buffer;
250 struct device *dev = &dln2->interface->dev;
251 u16 id, echo, handle, size;
256 switch (urb->status) {
264 /* this urb is terminated, clean up */
265 dev_dbg(dev, "urb shutting down with status %d\n", urb->status);
268 dev_dbg(dev, "nonzero urb status received %d\n", urb->status);
272 if (urb->actual_length < sizeof(struct dln2_header)) {
273 dev_err(dev, "short response: %d\n", urb->actual_length);
277 handle = le16_to_cpu(hdr->handle);
278 id = le16_to_cpu(hdr->id);
279 echo = le16_to_cpu(hdr->echo);
280 size = le16_to_cpu(hdr->size);
282 if (size != urb->actual_length) {
283 dev_err(dev, "size mismatch: handle %x cmd %x echo %x size %d actual %d\n",
284 handle, id, echo, size, urb->actual_length);
288 if (handle >= DLN2_HANDLES) {
289 dev_warn(dev, "invalid handle %d\n", handle);
293 data = urb->transfer_buffer + sizeof(struct dln2_header);
294 len = urb->actual_length - sizeof(struct dln2_header);
296 if (handle == DLN2_HANDLE_EVENT) {
299 spin_lock_irqsave(&dln2->event_cb_lock, flags);
300 dln2_run_event_callbacks(dln2, id, echo, data, len);
301 spin_unlock_irqrestore(&dln2->event_cb_lock, flags);
303 /* URB will be re-submitted in _dln2_transfer (free_rx_slot) */
304 if (dln2_transfer_complete(dln2, urb, handle, echo))
309 err = usb_submit_urb(urb, GFP_ATOMIC);
311 dev_err(dev, "failed to resubmit RX URB: %d\n", err);
314 static void *dln2_prep_buf(u16 handle, u16 cmd, u16 echo, const void *obuf,
315 int *obuf_len, gfp_t gfp)
319 struct dln2_header *hdr;
321 len = *obuf_len + sizeof(*hdr);
322 buf = kmalloc(len, gfp);
326 hdr = (struct dln2_header *)buf;
327 hdr->id = cpu_to_le16(cmd);
328 hdr->size = cpu_to_le16(len);
329 hdr->echo = cpu_to_le16(echo);
330 hdr->handle = cpu_to_le16(handle);
332 memcpy(buf + sizeof(*hdr), obuf, *obuf_len);
339 static int dln2_send_wait(struct dln2_dev *dln2, u16 handle, u16 cmd, u16 echo,
340 const void *obuf, int obuf_len)
347 buf = dln2_prep_buf(handle, cmd, echo, obuf, &len, GFP_KERNEL);
351 ret = usb_bulk_msg(dln2->usb_dev,
352 usb_sndbulkpipe(dln2->usb_dev, dln2->ep_out),
353 buf, len, &actual, DLN2_USB_TIMEOUT);
360 static bool find_free_slot(struct dln2_dev *dln2, u16 handle, int *slot)
362 struct dln2_mod_rx_slots *rxs;
365 if (dln2->disconnect) {
370 rxs = &dln2->mod_rx_slots[handle];
372 spin_lock_irqsave(&rxs->lock, flags);
374 *slot = find_first_zero_bit(rxs->bmap, DLN2_MAX_RX_SLOTS);
376 if (*slot < DLN2_MAX_RX_SLOTS) {
377 struct dln2_rx_context *rxc = &rxs->slots[*slot];
379 set_bit(*slot, rxs->bmap);
383 spin_unlock_irqrestore(&rxs->lock, flags);
385 return *slot < DLN2_MAX_RX_SLOTS;
388 static int alloc_rx_slot(struct dln2_dev *dln2, u16 handle)
394 * No need to timeout here, the wait is bounded by the timeout in
397 ret = wait_event_interruptible(dln2->mod_rx_slots[handle].wq,
398 find_free_slot(dln2, handle, &slot));
405 static void free_rx_slot(struct dln2_dev *dln2, u16 handle, int slot)
407 struct dln2_mod_rx_slots *rxs;
408 struct urb *urb = NULL;
410 struct dln2_rx_context *rxc;
412 rxs = &dln2->mod_rx_slots[handle];
414 spin_lock_irqsave(&rxs->lock, flags);
416 clear_bit(slot, rxs->bmap);
418 rxc = &rxs->slots[slot];
422 reinit_completion(&rxc->done);
424 spin_unlock_irqrestore(&rxs->lock, flags);
428 struct device *dev = &dln2->interface->dev;
430 err = usb_submit_urb(urb, GFP_KERNEL);
432 dev_err(dev, "failed to resubmit RX URB: %d\n", err);
435 wake_up_interruptible(&rxs->wq);
438 static int _dln2_transfer(struct dln2_dev *dln2, u16 handle, u16 cmd,
439 const void *obuf, unsigned obuf_len,
440 void *ibuf, unsigned *ibuf_len)
444 struct dln2_response *rsp;
445 struct dln2_rx_context *rxc;
446 struct device *dev = &dln2->interface->dev;
447 const unsigned long timeout = msecs_to_jiffies(DLN2_USB_TIMEOUT);
448 struct dln2_mod_rx_slots *rxs = &dln2->mod_rx_slots[handle];
451 spin_lock(&dln2->disconnect_lock);
452 if (!dln2->disconnect)
453 dln2->active_transfers++;
456 spin_unlock(&dln2->disconnect_lock);
461 rx_slot = alloc_rx_slot(dln2, handle);
467 ret = dln2_send_wait(dln2, handle, cmd, rx_slot, obuf, obuf_len);
469 dev_err(dev, "USB write failed: %d\n", ret);
470 goto out_free_rx_slot;
473 rxc = &rxs->slots[rx_slot];
475 ret = wait_for_completion_interruptible_timeout(&rxc->done, timeout);
479 goto out_free_rx_slot;
484 if (dln2->disconnect) {
486 goto out_free_rx_slot;
489 /* if we got here we know that the response header has been checked */
490 rsp = rxc->urb->transfer_buffer;
491 size = le16_to_cpu(rsp->hdr.size);
493 if (size < sizeof(*rsp)) {
495 goto out_free_rx_slot;
498 if (le16_to_cpu(rsp->result) > 0x80) {
499 dev_dbg(dev, "%d received response with error %d\n",
500 handle, le16_to_cpu(rsp->result));
502 goto out_free_rx_slot;
506 goto out_free_rx_slot;
508 if (*ibuf_len > size - sizeof(*rsp))
509 *ibuf_len = size - sizeof(*rsp);
511 memcpy(ibuf, rsp + 1, *ibuf_len);
514 free_rx_slot(dln2, handle, rx_slot);
516 spin_lock(&dln2->disconnect_lock);
517 dln2->active_transfers--;
518 spin_unlock(&dln2->disconnect_lock);
519 if (dln2->disconnect)
520 wake_up(&dln2->disconnect_wq);
525 int dln2_transfer(struct platform_device *pdev, u16 cmd,
526 const void *obuf, unsigned obuf_len,
527 void *ibuf, unsigned *ibuf_len)
529 struct dln2_platform_data *dln2_pdata;
530 struct dln2_dev *dln2;
533 dln2 = dev_get_drvdata(pdev->dev.parent);
534 dln2_pdata = dev_get_platdata(&pdev->dev);
535 handle = dln2_pdata->handle;
537 return _dln2_transfer(dln2, handle, cmd, obuf, obuf_len, ibuf,
540 EXPORT_SYMBOL(dln2_transfer);
542 static int dln2_check_hw(struct dln2_dev *dln2)
546 int len = sizeof(hw_type);
548 ret = _dln2_transfer(dln2, DLN2_HANDLE_CTRL, CMD_GET_DEVICE_VER,
549 NULL, 0, &hw_type, &len);
552 if (len < sizeof(hw_type))
555 if (le32_to_cpu(hw_type) != DLN2_HW_ID) {
556 dev_err(&dln2->interface->dev, "Device ID 0x%x not supported\n",
557 le32_to_cpu(hw_type));
564 static int dln2_print_serialno(struct dln2_dev *dln2)
568 int len = sizeof(serial_no);
569 struct device *dev = &dln2->interface->dev;
571 ret = _dln2_transfer(dln2, DLN2_HANDLE_CTRL, CMD_GET_DEVICE_SN, NULL, 0,
575 if (len < sizeof(serial_no))
578 dev_info(dev, "Diolan DLN2 serial %u\n", le32_to_cpu(serial_no));
583 static int dln2_hw_init(struct dln2_dev *dln2)
587 ret = dln2_check_hw(dln2);
591 return dln2_print_serialno(dln2);
594 static void dln2_free_rx_urbs(struct dln2_dev *dln2)
598 for (i = 0; i < DLN2_MAX_URBS; i++) {
599 usb_free_urb(dln2->rx_urb[i]);
600 kfree(dln2->rx_buf[i]);
604 static void dln2_stop_rx_urbs(struct dln2_dev *dln2)
608 for (i = 0; i < DLN2_MAX_URBS; i++)
609 usb_kill_urb(dln2->rx_urb[i]);
612 static void dln2_free(struct dln2_dev *dln2)
614 dln2_free_rx_urbs(dln2);
615 usb_put_dev(dln2->usb_dev);
619 static int dln2_setup_rx_urbs(struct dln2_dev *dln2,
620 struct usb_host_interface *hostif)
623 const int rx_max_size = DLN2_RX_BUF_SIZE;
625 for (i = 0; i < DLN2_MAX_URBS; i++) {
626 dln2->rx_buf[i] = kmalloc(rx_max_size, GFP_KERNEL);
627 if (!dln2->rx_buf[i])
630 dln2->rx_urb[i] = usb_alloc_urb(0, GFP_KERNEL);
631 if (!dln2->rx_urb[i])
634 usb_fill_bulk_urb(dln2->rx_urb[i], dln2->usb_dev,
635 usb_rcvbulkpipe(dln2->usb_dev, dln2->ep_in),
636 dln2->rx_buf[i], rx_max_size, dln2_rx, dln2);
642 static int dln2_start_rx_urbs(struct dln2_dev *dln2, gfp_t gfp)
644 struct device *dev = &dln2->interface->dev;
648 for (i = 0; i < DLN2_MAX_URBS; i++) {
649 ret = usb_submit_urb(dln2->rx_urb[i], gfp);
651 dev_err(dev, "failed to submit RX URB: %d\n", ret);
659 static struct dln2_platform_data dln2_pdata_gpio = {
660 .handle = DLN2_HANDLE_GPIO,
663 /* Only one I2C port seems to be supported on current hardware */
664 static struct dln2_platform_data dln2_pdata_i2c = {
665 .handle = DLN2_HANDLE_I2C,
669 /* Only one SPI port supported */
670 static struct dln2_platform_data dln2_pdata_spi = {
671 .handle = DLN2_HANDLE_SPI,
675 static const struct mfd_cell dln2_devs[] = {
678 .platform_data = &dln2_pdata_gpio,
679 .pdata_size = sizeof(struct dln2_platform_data),
683 .platform_data = &dln2_pdata_i2c,
684 .pdata_size = sizeof(struct dln2_platform_data),
688 .platform_data = &dln2_pdata_spi,
689 .pdata_size = sizeof(struct dln2_platform_data),
693 static void dln2_stop(struct dln2_dev *dln2)
697 /* don't allow starting new transfers */
698 spin_lock(&dln2->disconnect_lock);
699 dln2->disconnect = true;
700 spin_unlock(&dln2->disconnect_lock);
702 /* cancel in progress transfers */
703 for (i = 0; i < DLN2_HANDLES; i++) {
704 struct dln2_mod_rx_slots *rxs = &dln2->mod_rx_slots[i];
707 spin_lock_irqsave(&rxs->lock, flags);
709 /* cancel all response waiters */
710 for (j = 0; j < DLN2_MAX_RX_SLOTS; j++) {
711 struct dln2_rx_context *rxc = &rxs->slots[j];
714 complete(&rxc->done);
717 spin_unlock_irqrestore(&rxs->lock, flags);
720 /* wait for transfers to end */
721 wait_event(dln2->disconnect_wq, !dln2->active_transfers);
723 dln2_stop_rx_urbs(dln2);
726 static void dln2_disconnect(struct usb_interface *interface)
728 struct dln2_dev *dln2 = usb_get_intfdata(interface);
732 mfd_remove_devices(&interface->dev);
737 static int dln2_probe(struct usb_interface *interface,
738 const struct usb_device_id *usb_id)
740 struct usb_host_interface *hostif = interface->cur_altsetting;
741 struct usb_endpoint_descriptor *epin;
742 struct usb_endpoint_descriptor *epout;
743 struct device *dev = &interface->dev;
744 struct dln2_dev *dln2;
748 if (hostif->desc.bInterfaceNumber != 0 ||
749 hostif->desc.bNumEndpoints < 2)
752 epout = &hostif->endpoint[DLN2_EP_OUT].desc;
753 if (!usb_endpoint_is_bulk_out(epout))
755 epin = &hostif->endpoint[DLN2_EP_IN].desc;
756 if (!usb_endpoint_is_bulk_in(epin))
759 dln2 = kzalloc(sizeof(*dln2), GFP_KERNEL);
763 dln2->ep_out = epout->bEndpointAddress;
764 dln2->ep_in = epin->bEndpointAddress;
765 dln2->usb_dev = usb_get_dev(interface_to_usbdev(interface));
766 dln2->interface = interface;
767 usb_set_intfdata(interface, dln2);
768 init_waitqueue_head(&dln2->disconnect_wq);
770 for (i = 0; i < DLN2_HANDLES; i++) {
771 init_waitqueue_head(&dln2->mod_rx_slots[i].wq);
772 spin_lock_init(&dln2->mod_rx_slots[i].lock);
773 for (j = 0; j < DLN2_MAX_RX_SLOTS; j++)
774 init_completion(&dln2->mod_rx_slots[i].slots[j].done);
777 spin_lock_init(&dln2->event_cb_lock);
778 spin_lock_init(&dln2->disconnect_lock);
779 INIT_LIST_HEAD(&dln2->event_cb_list);
781 ret = dln2_setup_rx_urbs(dln2, hostif);
785 ret = dln2_start_rx_urbs(dln2, GFP_KERNEL);
789 ret = dln2_hw_init(dln2);
791 dev_err(dev, "failed to initialize hardware\n");
795 ret = mfd_add_hotplug_devices(dev, dln2_devs, ARRAY_SIZE(dln2_devs));
797 dev_err(dev, "failed to add mfd devices to core\n");
804 dln2_stop_rx_urbs(dln2);
812 static int dln2_suspend(struct usb_interface *iface, pm_message_t message)
814 struct dln2_dev *dln2 = usb_get_intfdata(iface);
821 static int dln2_resume(struct usb_interface *iface)
823 struct dln2_dev *dln2 = usb_get_intfdata(iface);
825 dln2->disconnect = false;
827 return dln2_start_rx_urbs(dln2, GFP_NOIO);
830 static const struct usb_device_id dln2_table[] = {
831 { USB_DEVICE(0xa257, 0x2013) },
835 MODULE_DEVICE_TABLE(usb, dln2_table);
837 static struct usb_driver dln2_driver = {
840 .disconnect = dln2_disconnect,
841 .id_table = dln2_table,
842 .suspend = dln2_suspend,
843 .resume = dln2_resume,
846 module_usb_driver(dln2_driver);
848 MODULE_AUTHOR("Octavian Purdila <octavian.purdila@intel.com>");
849 MODULE_DESCRIPTION("Core driver for the Diolan DLN2 interface adapter");
850 MODULE_LICENSE("GPL v2");