1 // SPDX-License-Identifier: GPL-2.0-only
3 * Driver for the Diolan DLN-2 USB adapter
5 * Copyright (c) 2014 Intel Corporation
9 * Copyright (c) 2010-2011 Ericsson AB
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/types.h>
15 #include <linux/slab.h>
16 #include <linux/usb.h>
17 #include <linux/i2c.h>
18 #include <linux/mutex.h>
19 #include <linux/platform_device.h>
20 #include <linux/mfd/core.h>
21 #include <linux/mfd/dln2.h>
22 #include <linux/rculist.h>
31 struct dln2_response {
32 struct dln2_header hdr;
36 #define DLN2_GENERIC_MODULE_ID 0x00
37 #define DLN2_GENERIC_CMD(cmd) DLN2_CMD(cmd, DLN2_GENERIC_MODULE_ID)
38 #define CMD_GET_DEVICE_VER DLN2_GENERIC_CMD(0x30)
39 #define CMD_GET_DEVICE_SN DLN2_GENERIC_CMD(0x31)
41 #define DLN2_HW_ID 0x200
42 #define DLN2_USB_TIMEOUT 200 /* in ms */
43 #define DLN2_MAX_RX_SLOTS 16
44 #define DLN2_MAX_URBS 16
45 #define DLN2_RX_BUF_SIZE 512
48 DLN2_HANDLE_EVENT = 0, /* don't change, hardware defined */
57 * Receive context used between the receive demultiplexer and the transfer
58 * routine. While sending a request the transfer routine will look for a free
59 * receive context and use it to wait for a response and to receive the URB and
60 * thus the response data.
62 struct dln2_rx_context {
63 /* completion used to wait for a response */
64 struct completion done;
66 /* if non-NULL the URB contains the response */
69 /* if true then this context is used to wait for a response */
74 * Receive contexts for a particular DLN2 module (i2c, gpio, etc.). We use the
75 * handle header field to identify the module in dln2_dev.mod_rx_slots and then
76 * the echo header field to index the slots field and find the receive context
77 * for a particular request.
79 struct dln2_mod_rx_slots {
81 DECLARE_BITMAP(bmap, DLN2_MAX_RX_SLOTS);
83 /* used to wait for a free RX slot */
86 /* used to wait for an RX operation to complete */
87 struct dln2_rx_context slots[DLN2_MAX_RX_SLOTS];
89 /* avoid races between alloc/free_rx_slot and dln2_rx_transfer */
99 struct usb_device *usb_dev;
100 struct usb_interface *interface;
104 struct urb *rx_urb[DLN2_MAX_URBS];
105 void *rx_buf[DLN2_MAX_URBS];
107 struct dln2_mod_rx_slots mod_rx_slots[DLN2_HANDLES];
109 struct list_head event_cb_list;
110 spinlock_t event_cb_lock;
113 int active_transfers;
114 wait_queue_head_t disconnect_wq;
115 spinlock_t disconnect_lock;
118 struct dln2_event_cb_entry {
119 struct list_head list;
121 struct platform_device *pdev;
122 dln2_event_cb_t callback;
125 int dln2_register_event_cb(struct platform_device *pdev, u16 id,
126 dln2_event_cb_t event_cb)
128 struct dln2_dev *dln2 = dev_get_drvdata(pdev->dev.parent);
129 struct dln2_event_cb_entry *i, *entry;
133 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
138 entry->callback = event_cb;
141 spin_lock_irqsave(&dln2->event_cb_lock, flags);
143 list_for_each_entry(i, &dln2->event_cb_list, list) {
151 list_add_rcu(&entry->list, &dln2->event_cb_list);
153 spin_unlock_irqrestore(&dln2->event_cb_lock, flags);
160 EXPORT_SYMBOL(dln2_register_event_cb);
162 void dln2_unregister_event_cb(struct platform_device *pdev, u16 id)
164 struct dln2_dev *dln2 = dev_get_drvdata(pdev->dev.parent);
165 struct dln2_event_cb_entry *i;
169 spin_lock_irqsave(&dln2->event_cb_lock, flags);
171 list_for_each_entry(i, &dln2->event_cb_list, list) {
173 list_del_rcu(&i->list);
179 spin_unlock_irqrestore(&dln2->event_cb_lock, flags);
186 EXPORT_SYMBOL(dln2_unregister_event_cb);
189 * Returns true if a valid transfer slot is found. In this case the URB must not
190 * be resubmitted immediately in dln2_rx as we need the data when dln2_transfer
191 * is woke up. It will be resubmitted there.
193 static bool dln2_transfer_complete(struct dln2_dev *dln2, struct urb *urb,
194 u16 handle, u16 rx_slot)
196 struct device *dev = &dln2->interface->dev;
197 struct dln2_mod_rx_slots *rxs = &dln2->mod_rx_slots[handle];
198 struct dln2_rx_context *rxc;
200 bool valid_slot = false;
202 if (rx_slot >= DLN2_MAX_RX_SLOTS)
205 rxc = &rxs->slots[rx_slot];
207 spin_lock_irqsave(&rxs->lock, flags);
208 if (rxc->in_use && !rxc->urb) {
210 complete(&rxc->done);
213 spin_unlock_irqrestore(&rxs->lock, flags);
217 dev_warn(dev, "bad/late response %d/%d\n", handle, rx_slot);
222 static void dln2_run_event_callbacks(struct dln2_dev *dln2, u16 id, u16 echo,
225 struct dln2_event_cb_entry *i;
229 list_for_each_entry_rcu(i, &dln2->event_cb_list, list) {
231 i->callback(i->pdev, echo, data, len);
239 static void dln2_rx(struct urb *urb)
241 struct dln2_dev *dln2 = urb->context;
242 struct dln2_header *hdr = urb->transfer_buffer;
243 struct device *dev = &dln2->interface->dev;
244 u16 id, echo, handle, size;
249 switch (urb->status) {
257 /* this urb is terminated, clean up */
258 dev_dbg(dev, "urb shutting down with status %d\n", urb->status);
261 dev_dbg(dev, "nonzero urb status received %d\n", urb->status);
265 if (urb->actual_length < sizeof(struct dln2_header)) {
266 dev_err(dev, "short response: %d\n", urb->actual_length);
270 handle = le16_to_cpu(hdr->handle);
271 id = le16_to_cpu(hdr->id);
272 echo = le16_to_cpu(hdr->echo);
273 size = le16_to_cpu(hdr->size);
275 if (size != urb->actual_length) {
276 dev_err(dev, "size mismatch: handle %x cmd %x echo %x size %d actual %d\n",
277 handle, id, echo, size, urb->actual_length);
281 if (handle >= DLN2_HANDLES) {
282 dev_warn(dev, "invalid handle %d\n", handle);
286 data = urb->transfer_buffer + sizeof(struct dln2_header);
287 len = urb->actual_length - sizeof(struct dln2_header);
289 if (handle == DLN2_HANDLE_EVENT) {
292 spin_lock_irqsave(&dln2->event_cb_lock, flags);
293 dln2_run_event_callbacks(dln2, id, echo, data, len);
294 spin_unlock_irqrestore(&dln2->event_cb_lock, flags);
296 /* URB will be re-submitted in _dln2_transfer (free_rx_slot) */
297 if (dln2_transfer_complete(dln2, urb, handle, echo))
302 err = usb_submit_urb(urb, GFP_ATOMIC);
304 dev_err(dev, "failed to resubmit RX URB: %d\n", err);
307 static void *dln2_prep_buf(u16 handle, u16 cmd, u16 echo, const void *obuf,
308 int *obuf_len, gfp_t gfp)
312 struct dln2_header *hdr;
314 len = *obuf_len + sizeof(*hdr);
315 buf = kmalloc(len, gfp);
319 hdr = (struct dln2_header *)buf;
320 hdr->id = cpu_to_le16(cmd);
321 hdr->size = cpu_to_le16(len);
322 hdr->echo = cpu_to_le16(echo);
323 hdr->handle = cpu_to_le16(handle);
325 memcpy(buf + sizeof(*hdr), obuf, *obuf_len);
332 static int dln2_send_wait(struct dln2_dev *dln2, u16 handle, u16 cmd, u16 echo,
333 const void *obuf, int obuf_len)
340 buf = dln2_prep_buf(handle, cmd, echo, obuf, &len, GFP_KERNEL);
344 ret = usb_bulk_msg(dln2->usb_dev,
345 usb_sndbulkpipe(dln2->usb_dev, dln2->ep_out),
346 buf, len, &actual, DLN2_USB_TIMEOUT);
353 static bool find_free_slot(struct dln2_dev *dln2, u16 handle, int *slot)
355 struct dln2_mod_rx_slots *rxs;
358 if (dln2->disconnect) {
363 rxs = &dln2->mod_rx_slots[handle];
365 spin_lock_irqsave(&rxs->lock, flags);
367 *slot = find_first_zero_bit(rxs->bmap, DLN2_MAX_RX_SLOTS);
369 if (*slot < DLN2_MAX_RX_SLOTS) {
370 struct dln2_rx_context *rxc = &rxs->slots[*slot];
372 set_bit(*slot, rxs->bmap);
376 spin_unlock_irqrestore(&rxs->lock, flags);
378 return *slot < DLN2_MAX_RX_SLOTS;
381 static int alloc_rx_slot(struct dln2_dev *dln2, u16 handle)
387 * No need to timeout here, the wait is bounded by the timeout in
390 ret = wait_event_interruptible(dln2->mod_rx_slots[handle].wq,
391 find_free_slot(dln2, handle, &slot));
398 static void free_rx_slot(struct dln2_dev *dln2, u16 handle, int slot)
400 struct dln2_mod_rx_slots *rxs;
401 struct urb *urb = NULL;
403 struct dln2_rx_context *rxc;
405 rxs = &dln2->mod_rx_slots[handle];
407 spin_lock_irqsave(&rxs->lock, flags);
409 clear_bit(slot, rxs->bmap);
411 rxc = &rxs->slots[slot];
415 reinit_completion(&rxc->done);
417 spin_unlock_irqrestore(&rxs->lock, flags);
421 struct device *dev = &dln2->interface->dev;
423 err = usb_submit_urb(urb, GFP_KERNEL);
425 dev_err(dev, "failed to resubmit RX URB: %d\n", err);
428 wake_up_interruptible(&rxs->wq);
431 static int _dln2_transfer(struct dln2_dev *dln2, u16 handle, u16 cmd,
432 const void *obuf, unsigned obuf_len,
433 void *ibuf, unsigned *ibuf_len)
437 struct dln2_response *rsp;
438 struct dln2_rx_context *rxc;
439 struct device *dev = &dln2->interface->dev;
440 const unsigned long timeout = msecs_to_jiffies(DLN2_USB_TIMEOUT);
441 struct dln2_mod_rx_slots *rxs = &dln2->mod_rx_slots[handle];
444 spin_lock(&dln2->disconnect_lock);
445 if (!dln2->disconnect)
446 dln2->active_transfers++;
449 spin_unlock(&dln2->disconnect_lock);
454 rx_slot = alloc_rx_slot(dln2, handle);
460 ret = dln2_send_wait(dln2, handle, cmd, rx_slot, obuf, obuf_len);
462 dev_err(dev, "USB write failed: %d\n", ret);
463 goto out_free_rx_slot;
466 rxc = &rxs->slots[rx_slot];
468 ret = wait_for_completion_interruptible_timeout(&rxc->done, timeout);
472 goto out_free_rx_slot;
477 if (dln2->disconnect) {
479 goto out_free_rx_slot;
482 /* if we got here we know that the response header has been checked */
483 rsp = rxc->urb->transfer_buffer;
484 size = le16_to_cpu(rsp->hdr.size);
486 if (size < sizeof(*rsp)) {
488 goto out_free_rx_slot;
491 if (le16_to_cpu(rsp->result) > 0x80) {
492 dev_dbg(dev, "%d received response with error %d\n",
493 handle, le16_to_cpu(rsp->result));
495 goto out_free_rx_slot;
499 goto out_free_rx_slot;
501 if (*ibuf_len > size - sizeof(*rsp))
502 *ibuf_len = size - sizeof(*rsp);
504 memcpy(ibuf, rsp + 1, *ibuf_len);
507 free_rx_slot(dln2, handle, rx_slot);
509 spin_lock(&dln2->disconnect_lock);
510 dln2->active_transfers--;
511 spin_unlock(&dln2->disconnect_lock);
512 if (dln2->disconnect)
513 wake_up(&dln2->disconnect_wq);
518 int dln2_transfer(struct platform_device *pdev, u16 cmd,
519 const void *obuf, unsigned obuf_len,
520 void *ibuf, unsigned *ibuf_len)
522 struct dln2_platform_data *dln2_pdata;
523 struct dln2_dev *dln2;
526 dln2 = dev_get_drvdata(pdev->dev.parent);
527 dln2_pdata = dev_get_platdata(&pdev->dev);
528 handle = dln2_pdata->handle;
530 return _dln2_transfer(dln2, handle, cmd, obuf, obuf_len, ibuf,
533 EXPORT_SYMBOL(dln2_transfer);
535 static int dln2_check_hw(struct dln2_dev *dln2)
539 int len = sizeof(hw_type);
541 ret = _dln2_transfer(dln2, DLN2_HANDLE_CTRL, CMD_GET_DEVICE_VER,
542 NULL, 0, &hw_type, &len);
545 if (len < sizeof(hw_type))
548 if (le32_to_cpu(hw_type) != DLN2_HW_ID) {
549 dev_err(&dln2->interface->dev, "Device ID 0x%x not supported\n",
550 le32_to_cpu(hw_type));
557 static int dln2_print_serialno(struct dln2_dev *dln2)
561 int len = sizeof(serial_no);
562 struct device *dev = &dln2->interface->dev;
564 ret = _dln2_transfer(dln2, DLN2_HANDLE_CTRL, CMD_GET_DEVICE_SN, NULL, 0,
568 if (len < sizeof(serial_no))
571 dev_info(dev, "Diolan DLN2 serial %u\n", le32_to_cpu(serial_no));
576 static int dln2_hw_init(struct dln2_dev *dln2)
580 ret = dln2_check_hw(dln2);
584 return dln2_print_serialno(dln2);
587 static void dln2_free_rx_urbs(struct dln2_dev *dln2)
591 for (i = 0; i < DLN2_MAX_URBS; i++) {
592 usb_free_urb(dln2->rx_urb[i]);
593 kfree(dln2->rx_buf[i]);
597 static void dln2_stop_rx_urbs(struct dln2_dev *dln2)
601 for (i = 0; i < DLN2_MAX_URBS; i++)
602 usb_kill_urb(dln2->rx_urb[i]);
605 static void dln2_free(struct dln2_dev *dln2)
607 dln2_free_rx_urbs(dln2);
608 usb_put_dev(dln2->usb_dev);
612 static int dln2_setup_rx_urbs(struct dln2_dev *dln2,
613 struct usb_host_interface *hostif)
616 const int rx_max_size = DLN2_RX_BUF_SIZE;
618 for (i = 0; i < DLN2_MAX_URBS; i++) {
619 dln2->rx_buf[i] = kmalloc(rx_max_size, GFP_KERNEL);
620 if (!dln2->rx_buf[i])
623 dln2->rx_urb[i] = usb_alloc_urb(0, GFP_KERNEL);
624 if (!dln2->rx_urb[i])
627 usb_fill_bulk_urb(dln2->rx_urb[i], dln2->usb_dev,
628 usb_rcvbulkpipe(dln2->usb_dev, dln2->ep_in),
629 dln2->rx_buf[i], rx_max_size, dln2_rx, dln2);
635 static int dln2_start_rx_urbs(struct dln2_dev *dln2, gfp_t gfp)
637 struct device *dev = &dln2->interface->dev;
641 for (i = 0; i < DLN2_MAX_URBS; i++) {
642 ret = usb_submit_urb(dln2->rx_urb[i], gfp);
644 dev_err(dev, "failed to submit RX URB: %d\n", ret);
653 DLN2_ACPI_MATCH_GPIO = 0,
654 DLN2_ACPI_MATCH_I2C = 1,
655 DLN2_ACPI_MATCH_SPI = 2,
658 static struct dln2_platform_data dln2_pdata_gpio = {
659 .handle = DLN2_HANDLE_GPIO,
662 static struct mfd_cell_acpi_match dln2_acpi_match_gpio = {
663 .adr = DLN2_ACPI_MATCH_GPIO,
666 /* Only one I2C port seems to be supported on current hardware */
667 static struct dln2_platform_data dln2_pdata_i2c = {
668 .handle = DLN2_HANDLE_I2C,
672 static struct mfd_cell_acpi_match dln2_acpi_match_i2c = {
673 .adr = DLN2_ACPI_MATCH_I2C,
676 /* Only one SPI port supported */
677 static struct dln2_platform_data dln2_pdata_spi = {
678 .handle = DLN2_HANDLE_SPI,
682 static struct mfd_cell_acpi_match dln2_acpi_match_spi = {
683 .adr = DLN2_ACPI_MATCH_SPI,
686 static const struct mfd_cell dln2_devs[] = {
689 .acpi_match = &dln2_acpi_match_gpio,
690 .platform_data = &dln2_pdata_gpio,
691 .pdata_size = sizeof(struct dln2_platform_data),
695 .acpi_match = &dln2_acpi_match_i2c,
696 .platform_data = &dln2_pdata_i2c,
697 .pdata_size = sizeof(struct dln2_platform_data),
701 .acpi_match = &dln2_acpi_match_spi,
702 .platform_data = &dln2_pdata_spi,
703 .pdata_size = sizeof(struct dln2_platform_data),
707 static void dln2_stop(struct dln2_dev *dln2)
711 /* don't allow starting new transfers */
712 spin_lock(&dln2->disconnect_lock);
713 dln2->disconnect = true;
714 spin_unlock(&dln2->disconnect_lock);
716 /* cancel in progress transfers */
717 for (i = 0; i < DLN2_HANDLES; i++) {
718 struct dln2_mod_rx_slots *rxs = &dln2->mod_rx_slots[i];
721 spin_lock_irqsave(&rxs->lock, flags);
723 /* cancel all response waiters */
724 for (j = 0; j < DLN2_MAX_RX_SLOTS; j++) {
725 struct dln2_rx_context *rxc = &rxs->slots[j];
728 complete(&rxc->done);
731 spin_unlock_irqrestore(&rxs->lock, flags);
734 /* wait for transfers to end */
735 wait_event(dln2->disconnect_wq, !dln2->active_transfers);
737 dln2_stop_rx_urbs(dln2);
740 static void dln2_disconnect(struct usb_interface *interface)
742 struct dln2_dev *dln2 = usb_get_intfdata(interface);
746 mfd_remove_devices(&interface->dev);
751 static int dln2_probe(struct usb_interface *interface,
752 const struct usb_device_id *usb_id)
754 struct usb_host_interface *hostif = interface->cur_altsetting;
755 struct usb_endpoint_descriptor *epin;
756 struct usb_endpoint_descriptor *epout;
757 struct device *dev = &interface->dev;
758 struct dln2_dev *dln2;
762 if (hostif->desc.bInterfaceNumber != 0 ||
763 hostif->desc.bNumEndpoints < 2)
766 epout = &hostif->endpoint[DLN2_EP_OUT].desc;
767 if (!usb_endpoint_is_bulk_out(epout))
769 epin = &hostif->endpoint[DLN2_EP_IN].desc;
770 if (!usb_endpoint_is_bulk_in(epin))
773 dln2 = kzalloc(sizeof(*dln2), GFP_KERNEL);
777 dln2->ep_out = epout->bEndpointAddress;
778 dln2->ep_in = epin->bEndpointAddress;
779 dln2->usb_dev = usb_get_dev(interface_to_usbdev(interface));
780 dln2->interface = interface;
781 usb_set_intfdata(interface, dln2);
782 init_waitqueue_head(&dln2->disconnect_wq);
784 for (i = 0; i < DLN2_HANDLES; i++) {
785 init_waitqueue_head(&dln2->mod_rx_slots[i].wq);
786 spin_lock_init(&dln2->mod_rx_slots[i].lock);
787 for (j = 0; j < DLN2_MAX_RX_SLOTS; j++)
788 init_completion(&dln2->mod_rx_slots[i].slots[j].done);
791 spin_lock_init(&dln2->event_cb_lock);
792 spin_lock_init(&dln2->disconnect_lock);
793 INIT_LIST_HEAD(&dln2->event_cb_list);
795 ret = dln2_setup_rx_urbs(dln2, hostif);
799 ret = dln2_start_rx_urbs(dln2, GFP_KERNEL);
803 ret = dln2_hw_init(dln2);
805 dev_err(dev, "failed to initialize hardware\n");
809 ret = mfd_add_hotplug_devices(dev, dln2_devs, ARRAY_SIZE(dln2_devs));
811 dev_err(dev, "failed to add mfd devices to core\n");
818 dln2_stop_rx_urbs(dln2);
826 static int dln2_suspend(struct usb_interface *iface, pm_message_t message)
828 struct dln2_dev *dln2 = usb_get_intfdata(iface);
835 static int dln2_resume(struct usb_interface *iface)
837 struct dln2_dev *dln2 = usb_get_intfdata(iface);
839 dln2->disconnect = false;
841 return dln2_start_rx_urbs(dln2, GFP_NOIO);
844 static const struct usb_device_id dln2_table[] = {
845 { USB_DEVICE(0xa257, 0x2013) },
849 MODULE_DEVICE_TABLE(usb, dln2_table);
851 static struct usb_driver dln2_driver = {
854 .disconnect = dln2_disconnect,
855 .id_table = dln2_table,
856 .suspend = dln2_suspend,
857 .resume = dln2_resume,
860 module_usb_driver(dln2_driver);
862 MODULE_AUTHOR("Octavian Purdila <octavian.purdila@intel.com>");
863 MODULE_DESCRIPTION("Core driver for the Diolan DLN2 interface adapter");
864 MODULE_LICENSE("GPL v2");