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 */
58 * Receive context used between the receive demultiplexer and the transfer
59 * routine. While sending a request the transfer routine will look for a free
60 * receive context and use it to wait for a response and to receive the URB and
61 * thus the response data.
63 struct dln2_rx_context {
64 /* completion used to wait for a response */
65 struct completion done;
67 /* if non-NULL the URB contains the response */
70 /* if true then this context is used to wait for a response */
75 * Receive contexts for a particular DLN2 module (i2c, gpio, etc.). We use the
76 * handle header field to identify the module in dln2_dev.mod_rx_slots and then
77 * the echo header field to index the slots field and find the receive context
78 * for a particular request.
80 struct dln2_mod_rx_slots {
82 DECLARE_BITMAP(bmap, DLN2_MAX_RX_SLOTS);
84 /* used to wait for a free RX slot */
87 /* used to wait for an RX operation to complete */
88 struct dln2_rx_context slots[DLN2_MAX_RX_SLOTS];
90 /* avoid races between alloc/free_rx_slot and dln2_rx_transfer */
100 struct usb_device *usb_dev;
101 struct usb_interface *interface;
105 struct urb *rx_urb[DLN2_MAX_URBS];
106 void *rx_buf[DLN2_MAX_URBS];
108 struct dln2_mod_rx_slots mod_rx_slots[DLN2_HANDLES];
110 struct list_head event_cb_list;
111 spinlock_t event_cb_lock;
114 int active_transfers;
115 wait_queue_head_t disconnect_wq;
116 spinlock_t disconnect_lock;
119 struct dln2_event_cb_entry {
120 struct list_head list;
122 struct platform_device *pdev;
123 dln2_event_cb_t callback;
126 int dln2_register_event_cb(struct platform_device *pdev, u16 id,
127 dln2_event_cb_t event_cb)
129 struct dln2_dev *dln2 = dev_get_drvdata(pdev->dev.parent);
130 struct dln2_event_cb_entry *i, *entry;
134 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
139 entry->callback = event_cb;
142 spin_lock_irqsave(&dln2->event_cb_lock, flags);
144 list_for_each_entry(i, &dln2->event_cb_list, list) {
152 list_add_rcu(&entry->list, &dln2->event_cb_list);
154 spin_unlock_irqrestore(&dln2->event_cb_lock, flags);
161 EXPORT_SYMBOL(dln2_register_event_cb);
163 void dln2_unregister_event_cb(struct platform_device *pdev, u16 id)
165 struct dln2_dev *dln2 = dev_get_drvdata(pdev->dev.parent);
166 struct dln2_event_cb_entry *i;
170 spin_lock_irqsave(&dln2->event_cb_lock, flags);
172 list_for_each_entry(i, &dln2->event_cb_list, list) {
174 list_del_rcu(&i->list);
180 spin_unlock_irqrestore(&dln2->event_cb_lock, flags);
187 EXPORT_SYMBOL(dln2_unregister_event_cb);
190 * Returns true if a valid transfer slot is found. In this case the URB must not
191 * be resubmitted immediately in dln2_rx as we need the data when dln2_transfer
192 * is woke up. It will be resubmitted there.
194 static bool dln2_transfer_complete(struct dln2_dev *dln2, struct urb *urb,
195 u16 handle, u16 rx_slot)
197 struct device *dev = &dln2->interface->dev;
198 struct dln2_mod_rx_slots *rxs = &dln2->mod_rx_slots[handle];
199 struct dln2_rx_context *rxc;
201 bool valid_slot = false;
203 if (rx_slot >= DLN2_MAX_RX_SLOTS)
206 rxc = &rxs->slots[rx_slot];
208 spin_lock_irqsave(&rxs->lock, flags);
209 if (rxc->in_use && !rxc->urb) {
211 complete(&rxc->done);
214 spin_unlock_irqrestore(&rxs->lock, flags);
218 dev_warn(dev, "bad/late response %d/%d\n", handle, rx_slot);
223 static void dln2_run_event_callbacks(struct dln2_dev *dln2, u16 id, u16 echo,
226 struct dln2_event_cb_entry *i;
230 list_for_each_entry_rcu(i, &dln2->event_cb_list, list) {
232 i->callback(i->pdev, echo, data, len);
240 static void dln2_rx(struct urb *urb)
242 struct dln2_dev *dln2 = urb->context;
243 struct dln2_header *hdr = urb->transfer_buffer;
244 struct device *dev = &dln2->interface->dev;
245 u16 id, echo, handle, size;
250 switch (urb->status) {
258 /* this urb is terminated, clean up */
259 dev_dbg(dev, "urb shutting down with status %d\n", urb->status);
262 dev_dbg(dev, "nonzero urb status received %d\n", urb->status);
266 if (urb->actual_length < sizeof(struct dln2_header)) {
267 dev_err(dev, "short response: %d\n", urb->actual_length);
271 handle = le16_to_cpu(hdr->handle);
272 id = le16_to_cpu(hdr->id);
273 echo = le16_to_cpu(hdr->echo);
274 size = le16_to_cpu(hdr->size);
276 if (size != urb->actual_length) {
277 dev_err(dev, "size mismatch: handle %x cmd %x echo %x size %d actual %d\n",
278 handle, id, echo, size, urb->actual_length);
282 if (handle >= DLN2_HANDLES) {
283 dev_warn(dev, "invalid handle %d\n", handle);
287 data = urb->transfer_buffer + sizeof(struct dln2_header);
288 len = urb->actual_length - sizeof(struct dln2_header);
290 if (handle == DLN2_HANDLE_EVENT) {
293 spin_lock_irqsave(&dln2->event_cb_lock, flags);
294 dln2_run_event_callbacks(dln2, id, echo, data, len);
295 spin_unlock_irqrestore(&dln2->event_cb_lock, flags);
297 /* URB will be re-submitted in _dln2_transfer (free_rx_slot) */
298 if (dln2_transfer_complete(dln2, urb, handle, echo))
303 err = usb_submit_urb(urb, GFP_ATOMIC);
305 dev_err(dev, "failed to resubmit RX URB: %d\n", err);
308 static void *dln2_prep_buf(u16 handle, u16 cmd, u16 echo, const void *obuf,
309 int *obuf_len, gfp_t gfp)
313 struct dln2_header *hdr;
315 len = *obuf_len + sizeof(*hdr);
316 buf = kmalloc(len, gfp);
320 hdr = (struct dln2_header *)buf;
321 hdr->id = cpu_to_le16(cmd);
322 hdr->size = cpu_to_le16(len);
323 hdr->echo = cpu_to_le16(echo);
324 hdr->handle = cpu_to_le16(handle);
326 memcpy(buf + sizeof(*hdr), obuf, *obuf_len);
333 static int dln2_send_wait(struct dln2_dev *dln2, u16 handle, u16 cmd, u16 echo,
334 const void *obuf, int obuf_len)
341 buf = dln2_prep_buf(handle, cmd, echo, obuf, &len, GFP_KERNEL);
345 ret = usb_bulk_msg(dln2->usb_dev,
346 usb_sndbulkpipe(dln2->usb_dev, dln2->ep_out),
347 buf, len, &actual, DLN2_USB_TIMEOUT);
354 static bool find_free_slot(struct dln2_dev *dln2, u16 handle, int *slot)
356 struct dln2_mod_rx_slots *rxs;
359 if (dln2->disconnect) {
364 rxs = &dln2->mod_rx_slots[handle];
366 spin_lock_irqsave(&rxs->lock, flags);
368 *slot = find_first_zero_bit(rxs->bmap, DLN2_MAX_RX_SLOTS);
370 if (*slot < DLN2_MAX_RX_SLOTS) {
371 struct dln2_rx_context *rxc = &rxs->slots[*slot];
373 set_bit(*slot, rxs->bmap);
377 spin_unlock_irqrestore(&rxs->lock, flags);
379 return *slot < DLN2_MAX_RX_SLOTS;
382 static int alloc_rx_slot(struct dln2_dev *dln2, u16 handle)
388 * No need to timeout here, the wait is bounded by the timeout in
391 ret = wait_event_interruptible(dln2->mod_rx_slots[handle].wq,
392 find_free_slot(dln2, handle, &slot));
399 static void free_rx_slot(struct dln2_dev *dln2, u16 handle, int slot)
401 struct dln2_mod_rx_slots *rxs;
402 struct urb *urb = NULL;
404 struct dln2_rx_context *rxc;
406 rxs = &dln2->mod_rx_slots[handle];
408 spin_lock_irqsave(&rxs->lock, flags);
410 clear_bit(slot, rxs->bmap);
412 rxc = &rxs->slots[slot];
416 reinit_completion(&rxc->done);
418 spin_unlock_irqrestore(&rxs->lock, flags);
422 struct device *dev = &dln2->interface->dev;
424 err = usb_submit_urb(urb, GFP_KERNEL);
426 dev_err(dev, "failed to resubmit RX URB: %d\n", err);
429 wake_up_interruptible(&rxs->wq);
432 static int _dln2_transfer(struct dln2_dev *dln2, u16 handle, u16 cmd,
433 const void *obuf, unsigned obuf_len,
434 void *ibuf, unsigned *ibuf_len)
438 struct dln2_response *rsp;
439 struct dln2_rx_context *rxc;
440 struct device *dev = &dln2->interface->dev;
441 const unsigned long timeout = msecs_to_jiffies(DLN2_USB_TIMEOUT);
442 struct dln2_mod_rx_slots *rxs = &dln2->mod_rx_slots[handle];
445 spin_lock(&dln2->disconnect_lock);
446 if (!dln2->disconnect)
447 dln2->active_transfers++;
450 spin_unlock(&dln2->disconnect_lock);
455 rx_slot = alloc_rx_slot(dln2, handle);
461 ret = dln2_send_wait(dln2, handle, cmd, rx_slot, obuf, obuf_len);
463 dev_err(dev, "USB write failed: %d\n", ret);
464 goto out_free_rx_slot;
467 rxc = &rxs->slots[rx_slot];
469 ret = wait_for_completion_interruptible_timeout(&rxc->done, timeout);
473 goto out_free_rx_slot;
478 if (dln2->disconnect) {
480 goto out_free_rx_slot;
483 /* if we got here we know that the response header has been checked */
484 rsp = rxc->urb->transfer_buffer;
485 size = le16_to_cpu(rsp->hdr.size);
487 if (size < sizeof(*rsp)) {
489 goto out_free_rx_slot;
492 if (le16_to_cpu(rsp->result) > 0x80) {
493 dev_dbg(dev, "%d received response with error %d\n",
494 handle, le16_to_cpu(rsp->result));
496 goto out_free_rx_slot;
500 goto out_free_rx_slot;
502 if (*ibuf_len > size - sizeof(*rsp))
503 *ibuf_len = size - sizeof(*rsp);
505 memcpy(ibuf, rsp + 1, *ibuf_len);
508 free_rx_slot(dln2, handle, rx_slot);
510 spin_lock(&dln2->disconnect_lock);
511 dln2->active_transfers--;
512 spin_unlock(&dln2->disconnect_lock);
513 if (dln2->disconnect)
514 wake_up(&dln2->disconnect_wq);
519 int dln2_transfer(struct platform_device *pdev, u16 cmd,
520 const void *obuf, unsigned obuf_len,
521 void *ibuf, unsigned *ibuf_len)
523 struct dln2_platform_data *dln2_pdata;
524 struct dln2_dev *dln2;
527 dln2 = dev_get_drvdata(pdev->dev.parent);
528 dln2_pdata = dev_get_platdata(&pdev->dev);
529 handle = dln2_pdata->handle;
531 return _dln2_transfer(dln2, handle, cmd, obuf, obuf_len, ibuf,
534 EXPORT_SYMBOL(dln2_transfer);
536 static int dln2_check_hw(struct dln2_dev *dln2)
540 int len = sizeof(hw_type);
542 ret = _dln2_transfer(dln2, DLN2_HANDLE_CTRL, CMD_GET_DEVICE_VER,
543 NULL, 0, &hw_type, &len);
546 if (len < sizeof(hw_type))
549 if (le32_to_cpu(hw_type) != DLN2_HW_ID) {
550 dev_err(&dln2->interface->dev, "Device ID 0x%x not supported\n",
551 le32_to_cpu(hw_type));
558 static int dln2_print_serialno(struct dln2_dev *dln2)
562 int len = sizeof(serial_no);
563 struct device *dev = &dln2->interface->dev;
565 ret = _dln2_transfer(dln2, DLN2_HANDLE_CTRL, CMD_GET_DEVICE_SN, NULL, 0,
569 if (len < sizeof(serial_no))
572 dev_info(dev, "Diolan DLN2 serial %u\n", le32_to_cpu(serial_no));
577 static int dln2_hw_init(struct dln2_dev *dln2)
581 ret = dln2_check_hw(dln2);
585 return dln2_print_serialno(dln2);
588 static void dln2_free_rx_urbs(struct dln2_dev *dln2)
592 for (i = 0; i < DLN2_MAX_URBS; i++) {
593 usb_free_urb(dln2->rx_urb[i]);
594 kfree(dln2->rx_buf[i]);
598 static void dln2_stop_rx_urbs(struct dln2_dev *dln2)
602 for (i = 0; i < DLN2_MAX_URBS; i++)
603 usb_kill_urb(dln2->rx_urb[i]);
606 static void dln2_free(struct dln2_dev *dln2)
608 dln2_free_rx_urbs(dln2);
609 usb_put_dev(dln2->usb_dev);
613 static int dln2_setup_rx_urbs(struct dln2_dev *dln2,
614 struct usb_host_interface *hostif)
617 const int rx_max_size = DLN2_RX_BUF_SIZE;
619 for (i = 0; i < DLN2_MAX_URBS; i++) {
620 dln2->rx_buf[i] = kmalloc(rx_max_size, GFP_KERNEL);
621 if (!dln2->rx_buf[i])
624 dln2->rx_urb[i] = usb_alloc_urb(0, GFP_KERNEL);
625 if (!dln2->rx_urb[i])
628 usb_fill_bulk_urb(dln2->rx_urb[i], dln2->usb_dev,
629 usb_rcvbulkpipe(dln2->usb_dev, dln2->ep_in),
630 dln2->rx_buf[i], rx_max_size, dln2_rx, dln2);
636 static int dln2_start_rx_urbs(struct dln2_dev *dln2, gfp_t gfp)
638 struct device *dev = &dln2->interface->dev;
642 for (i = 0; i < DLN2_MAX_URBS; i++) {
643 ret = usb_submit_urb(dln2->rx_urb[i], gfp);
645 dev_err(dev, "failed to submit RX URB: %d\n", ret);
654 DLN2_ACPI_MATCH_GPIO = 0,
655 DLN2_ACPI_MATCH_I2C = 1,
656 DLN2_ACPI_MATCH_SPI = 2,
657 DLN2_ACPI_MATCH_ADC = 3,
660 static struct dln2_platform_data dln2_pdata_gpio = {
661 .handle = DLN2_HANDLE_GPIO,
664 static struct mfd_cell_acpi_match dln2_acpi_match_gpio = {
665 .adr = DLN2_ACPI_MATCH_GPIO,
668 /* Only one I2C port seems to be supported on current hardware */
669 static struct dln2_platform_data dln2_pdata_i2c = {
670 .handle = DLN2_HANDLE_I2C,
674 static struct mfd_cell_acpi_match dln2_acpi_match_i2c = {
675 .adr = DLN2_ACPI_MATCH_I2C,
678 /* Only one SPI port supported */
679 static struct dln2_platform_data dln2_pdata_spi = {
680 .handle = DLN2_HANDLE_SPI,
684 static struct mfd_cell_acpi_match dln2_acpi_match_spi = {
685 .adr = DLN2_ACPI_MATCH_SPI,
688 /* Only one ADC port supported */
689 static struct dln2_platform_data dln2_pdata_adc = {
690 .handle = DLN2_HANDLE_ADC,
694 static struct mfd_cell_acpi_match dln2_acpi_match_adc = {
695 .adr = DLN2_ACPI_MATCH_ADC,
698 static const struct mfd_cell dln2_devs[] = {
701 .acpi_match = &dln2_acpi_match_gpio,
702 .platform_data = &dln2_pdata_gpio,
703 .pdata_size = sizeof(struct dln2_platform_data),
707 .acpi_match = &dln2_acpi_match_i2c,
708 .platform_data = &dln2_pdata_i2c,
709 .pdata_size = sizeof(struct dln2_platform_data),
713 .acpi_match = &dln2_acpi_match_spi,
714 .platform_data = &dln2_pdata_spi,
715 .pdata_size = sizeof(struct dln2_platform_data),
719 .acpi_match = &dln2_acpi_match_adc,
720 .platform_data = &dln2_pdata_adc,
721 .pdata_size = sizeof(struct dln2_platform_data),
725 static void dln2_stop(struct dln2_dev *dln2)
729 /* don't allow starting new transfers */
730 spin_lock(&dln2->disconnect_lock);
731 dln2->disconnect = true;
732 spin_unlock(&dln2->disconnect_lock);
734 /* cancel in progress transfers */
735 for (i = 0; i < DLN2_HANDLES; i++) {
736 struct dln2_mod_rx_slots *rxs = &dln2->mod_rx_slots[i];
739 spin_lock_irqsave(&rxs->lock, flags);
741 /* cancel all response waiters */
742 for (j = 0; j < DLN2_MAX_RX_SLOTS; j++) {
743 struct dln2_rx_context *rxc = &rxs->slots[j];
746 complete(&rxc->done);
749 spin_unlock_irqrestore(&rxs->lock, flags);
752 /* wait for transfers to end */
753 wait_event(dln2->disconnect_wq, !dln2->active_transfers);
755 dln2_stop_rx_urbs(dln2);
758 static void dln2_disconnect(struct usb_interface *interface)
760 struct dln2_dev *dln2 = usb_get_intfdata(interface);
764 mfd_remove_devices(&interface->dev);
769 static int dln2_probe(struct usb_interface *interface,
770 const struct usb_device_id *usb_id)
772 struct usb_host_interface *hostif = interface->cur_altsetting;
773 struct usb_endpoint_descriptor *epin;
774 struct usb_endpoint_descriptor *epout;
775 struct device *dev = &interface->dev;
776 struct dln2_dev *dln2;
780 if (hostif->desc.bInterfaceNumber != 0 ||
781 hostif->desc.bNumEndpoints < 2)
784 epout = &hostif->endpoint[DLN2_EP_OUT].desc;
785 if (!usb_endpoint_is_bulk_out(epout))
787 epin = &hostif->endpoint[DLN2_EP_IN].desc;
788 if (!usb_endpoint_is_bulk_in(epin))
791 dln2 = kzalloc(sizeof(*dln2), GFP_KERNEL);
795 dln2->ep_out = epout->bEndpointAddress;
796 dln2->ep_in = epin->bEndpointAddress;
797 dln2->usb_dev = usb_get_dev(interface_to_usbdev(interface));
798 dln2->interface = interface;
799 usb_set_intfdata(interface, dln2);
800 init_waitqueue_head(&dln2->disconnect_wq);
802 for (i = 0; i < DLN2_HANDLES; i++) {
803 init_waitqueue_head(&dln2->mod_rx_slots[i].wq);
804 spin_lock_init(&dln2->mod_rx_slots[i].lock);
805 for (j = 0; j < DLN2_MAX_RX_SLOTS; j++)
806 init_completion(&dln2->mod_rx_slots[i].slots[j].done);
809 spin_lock_init(&dln2->event_cb_lock);
810 spin_lock_init(&dln2->disconnect_lock);
811 INIT_LIST_HEAD(&dln2->event_cb_list);
813 ret = dln2_setup_rx_urbs(dln2, hostif);
817 ret = dln2_start_rx_urbs(dln2, GFP_KERNEL);
821 ret = dln2_hw_init(dln2);
823 dev_err(dev, "failed to initialize hardware\n");
827 ret = mfd_add_hotplug_devices(dev, dln2_devs, ARRAY_SIZE(dln2_devs));
829 dev_err(dev, "failed to add mfd devices to core\n");
836 dln2_stop_rx_urbs(dln2);
844 static int dln2_suspend(struct usb_interface *iface, pm_message_t message)
846 struct dln2_dev *dln2 = usb_get_intfdata(iface);
853 static int dln2_resume(struct usb_interface *iface)
855 struct dln2_dev *dln2 = usb_get_intfdata(iface);
857 dln2->disconnect = false;
859 return dln2_start_rx_urbs(dln2, GFP_NOIO);
862 static const struct usb_device_id dln2_table[] = {
863 { USB_DEVICE(0xa257, 0x2013) },
867 MODULE_DEVICE_TABLE(usb, dln2_table);
869 static struct usb_driver dln2_driver = {
872 .disconnect = dln2_disconnect,
873 .id_table = dln2_table,
874 .suspend = dln2_suspend,
875 .resume = dln2_resume,
878 module_usb_driver(dln2_driver);
880 MODULE_AUTHOR("Octavian Purdila <octavian.purdila@intel.com>");
881 MODULE_DESCRIPTION("Core driver for the Diolan DLN2 interface adapter");
882 MODULE_LICENSE("GPL v2");