15fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0+
27e8455b1SLee Jones /*
32824bd25SMichael Hund * Generic USB driver for report based interrupt in/out devices
42824bd25SMichael Hund * like LD Didactic's USB devices. LD Didactic's USB devices are
52824bd25SMichael Hund * HID devices which do not use HID report definitons (they use
62824bd25SMichael Hund * raw interrupt in and our reports only for communication).
72824bd25SMichael Hund *
82824bd25SMichael Hund * This driver uses a ring buffer for time critical reading of
92824bd25SMichael Hund * interrupt in reports and provides read and write methods for
102824bd25SMichael Hund * raw interrupt reports (similar to the Windows HID driver).
112824bd25SMichael Hund * Devices based on the book USB COMPLETE by Jan Axelson may need
122824bd25SMichael Hund * such a compatibility to the Windows HID driver.
132824bd25SMichael Hund *
142824bd25SMichael Hund * Copyright (C) 2005 Michael Hund <mhund@ld-didactic.de>
152824bd25SMichael Hund *
162824bd25SMichael Hund * Derived from Lego USB Tower driver
172824bd25SMichael Hund * Copyright (C) 2003 David Glance <advidgsf@sourceforge.net>
182824bd25SMichael Hund * 2001-2004 Juergen Stuber <starblue@users.sourceforge.net>
192824bd25SMichael Hund */
202824bd25SMichael Hund
212824bd25SMichael Hund #include <linux/kernel.h>
222824bd25SMichael Hund #include <linux/errno.h>
232824bd25SMichael Hund #include <linux/slab.h>
242824bd25SMichael Hund #include <linux/module.h>
254186ecf8SArjan van de Ven #include <linux/mutex.h>
262824bd25SMichael Hund
277c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
282824bd25SMichael Hund #include <linux/input.h>
292824bd25SMichael Hund #include <linux/usb.h>
302824bd25SMichael Hund #include <linux/poll.h>
312824bd25SMichael Hund
322824bd25SMichael Hund /* Define these values to match your devices */
332824bd25SMichael Hund #define USB_VENDOR_ID_LD 0x0f11 /* USB Vendor ID of LD Didactic GmbH */
34ce97cac8SMichael Hund #define USB_DEVICE_ID_LD_CASSY 0x1000 /* USB Product ID of CASSY-S modules with 8 bytes endpoint size */
35ce97cac8SMichael Hund #define USB_DEVICE_ID_LD_CASSY2 0x1001 /* USB Product ID of CASSY-S modules with 64 bytes endpoint size */
36ba3e66e9SMichael Hund #define USB_DEVICE_ID_LD_POCKETCASSY 0x1010 /* USB Product ID of Pocket-CASSY */
37ce97cac8SMichael Hund #define USB_DEVICE_ID_LD_POCKETCASSY2 0x1011 /* USB Product ID of Pocket-CASSY 2 (reserved) */
38ba3e66e9SMichael Hund #define USB_DEVICE_ID_LD_MOBILECASSY 0x1020 /* USB Product ID of Mobile-CASSY */
39ce97cac8SMichael Hund #define USB_DEVICE_ID_LD_MOBILECASSY2 0x1021 /* USB Product ID of Mobile-CASSY 2 (reserved) */
40ce97cac8SMichael Hund #define USB_DEVICE_ID_LD_MICROCASSYVOLTAGE 0x1031 /* USB Product ID of Micro-CASSY Voltage */
41ce97cac8SMichael Hund #define USB_DEVICE_ID_LD_MICROCASSYCURRENT 0x1032 /* USB Product ID of Micro-CASSY Current */
42ce97cac8SMichael Hund #define USB_DEVICE_ID_LD_MICROCASSYTIME 0x1033 /* USB Product ID of Micro-CASSY Time (reserved) */
43ce97cac8SMichael Hund #define USB_DEVICE_ID_LD_MICROCASSYTEMPERATURE 0x1035 /* USB Product ID of Micro-CASSY Temperature */
44ce97cac8SMichael Hund #define USB_DEVICE_ID_LD_MICROCASSYPH 0x1038 /* USB Product ID of Micro-CASSY pH */
4552ad2bd8SKarsten Koop #define USB_DEVICE_ID_LD_POWERANALYSERCASSY 0x1040 /* USB Product ID of Power Analyser CASSY */
4652ad2bd8SKarsten Koop #define USB_DEVICE_ID_LD_CONVERTERCONTROLLERCASSY 0x1042 /* USB Product ID of Converter Controller CASSY */
4752ad2bd8SKarsten Koop #define USB_DEVICE_ID_LD_MACHINETESTCASSY 0x1043 /* USB Product ID of Machine Test CASSY */
48ba3e66e9SMichael Hund #define USB_DEVICE_ID_LD_JWM 0x1080 /* USB Product ID of Joule and Wattmeter */
49ba3e66e9SMichael Hund #define USB_DEVICE_ID_LD_DMMP 0x1081 /* USB Product ID of Digital Multimeter P (reserved) */
50ba3e66e9SMichael Hund #define USB_DEVICE_ID_LD_UMIP 0x1090 /* USB Product ID of UMI P */
51ce97cac8SMichael Hund #define USB_DEVICE_ID_LD_UMIC 0x10A0 /* USB Product ID of UMI C */
52ce97cac8SMichael Hund #define USB_DEVICE_ID_LD_UMIB 0x10B0 /* USB Product ID of UMI B */
53ce97cac8SMichael Hund #define USB_DEVICE_ID_LD_XRAY 0x1100 /* USB Product ID of X-Ray Apparatus 55481 */
54ce97cac8SMichael Hund #define USB_DEVICE_ID_LD_XRAY2 0x1101 /* USB Product ID of X-Ray Apparatus 554800 */
55ce97cac8SMichael Hund #define USB_DEVICE_ID_LD_XRAYCT 0x1110 /* USB Product ID of X-Ray Apparatus CT 554821*/
56ba3e66e9SMichael Hund #define USB_DEVICE_ID_LD_VIDEOCOM 0x1200 /* USB Product ID of VideoCom */
57ce97cac8SMichael Hund #define USB_DEVICE_ID_LD_MOTOR 0x1210 /* USB Product ID of Motor (reserved) */
58ba3e66e9SMichael Hund #define USB_DEVICE_ID_LD_COM3LAB 0x2000 /* USB Product ID of COM3LAB */
59ba3e66e9SMichael Hund #define USB_DEVICE_ID_LD_TELEPORT 0x2010 /* USB Product ID of Terminal Adapter */
60ba3e66e9SMichael Hund #define USB_DEVICE_ID_LD_NETWORKANALYSER 0x2020 /* USB Product ID of Network Analyser */
61ba3e66e9SMichael Hund #define USB_DEVICE_ID_LD_POWERCONTROL 0x2030 /* USB Product ID of Converter Control Unit */
62ba3e66e9SMichael Hund #define USB_DEVICE_ID_LD_MACHINETEST 0x2040 /* USB Product ID of Machine Test System */
63ce97cac8SMichael Hund #define USB_DEVICE_ID_LD_MOSTANALYSER 0x2050 /* USB Product ID of MOST Protocol Analyser */
64ce97cac8SMichael Hund #define USB_DEVICE_ID_LD_MOSTANALYSER2 0x2051 /* USB Product ID of MOST Protocol Analyser 2 */
65ce97cac8SMichael Hund #define USB_DEVICE_ID_LD_ABSESP 0x2060 /* USB Product ID of ABS ESP */
66ce97cac8SMichael Hund #define USB_DEVICE_ID_LD_AUTODATABUS 0x2070 /* USB Product ID of Automotive Data Buses */
67ce97cac8SMichael Hund #define USB_DEVICE_ID_LD_MCT 0x2080 /* USB Product ID of Microcontroller technique */
68ce97cac8SMichael Hund #define USB_DEVICE_ID_LD_HYBRID 0x2090 /* USB Product ID of Automotive Hybrid */
69ce97cac8SMichael Hund #define USB_DEVICE_ID_LD_HEATCONTROL 0x20A0 /* USB Product ID of Heat control */
702824bd25SMichael Hund
712824bd25SMichael Hund #ifdef CONFIG_USB_DYNAMIC_MINORS
722824bd25SMichael Hund #define USB_LD_MINOR_BASE 0
732824bd25SMichael Hund #else
742824bd25SMichael Hund #define USB_LD_MINOR_BASE 176
752824bd25SMichael Hund #endif
762824bd25SMichael Hund
772824bd25SMichael Hund /* table of devices that work with this driver */
7833b9e162SNémeth Márton static const struct usb_device_id ld_usb_table[] = {
79ba3e66e9SMichael Hund { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_CASSY) },
80ce97cac8SMichael Hund { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_CASSY2) },
81ba3e66e9SMichael Hund { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_POCKETCASSY) },
82ce97cac8SMichael Hund { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_POCKETCASSY2) },
83ba3e66e9SMichael Hund { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MOBILECASSY) },
84ce97cac8SMichael Hund { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MOBILECASSY2) },
85ce97cac8SMichael Hund { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MICROCASSYVOLTAGE) },
86ce97cac8SMichael Hund { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MICROCASSYCURRENT) },
87ce97cac8SMichael Hund { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MICROCASSYTIME) },
88ce97cac8SMichael Hund { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MICROCASSYTEMPERATURE) },
89ce97cac8SMichael Hund { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MICROCASSYPH) },
9052ad2bd8SKarsten Koop { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_POWERANALYSERCASSY) },
9152ad2bd8SKarsten Koop { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_CONVERTERCONTROLLERCASSY) },
9252ad2bd8SKarsten Koop { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MACHINETESTCASSY) },
93ba3e66e9SMichael Hund { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_JWM) },
94ba3e66e9SMichael Hund { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_DMMP) },
95ba3e66e9SMichael Hund { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_UMIP) },
96ce97cac8SMichael Hund { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_UMIC) },
97ce97cac8SMichael Hund { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_UMIB) },
98ce97cac8SMichael Hund { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_XRAY) },
99ba3e66e9SMichael Hund { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_XRAY2) },
100ba3e66e9SMichael Hund { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_VIDEOCOM) },
101ce97cac8SMichael Hund { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MOTOR) },
102ba3e66e9SMichael Hund { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_COM3LAB) },
103ba3e66e9SMichael Hund { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_TELEPORT) },
104ba3e66e9SMichael Hund { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_NETWORKANALYSER) },
105ba3e66e9SMichael Hund { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_POWERCONTROL) },
106ba3e66e9SMichael Hund { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MACHINETEST) },
107ce97cac8SMichael Hund { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MOSTANALYSER) },
108ce97cac8SMichael Hund { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MOSTANALYSER2) },
109ce97cac8SMichael Hund { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_ABSESP) },
110ce97cac8SMichael Hund { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_AUTODATABUS) },
111ce97cac8SMichael Hund { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MCT) },
112ce97cac8SMichael Hund { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_HYBRID) },
113ce97cac8SMichael Hund { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_HEATCONTROL) },
1142824bd25SMichael Hund { } /* Terminating entry */
1152824bd25SMichael Hund };
1162824bd25SMichael Hund MODULE_DEVICE_TABLE(usb, ld_usb_table);
1172824bd25SMichael Hund MODULE_AUTHOR("Michael Hund <mhund@ld-didactic.de>");
1182824bd25SMichael Hund MODULE_DESCRIPTION("LD USB Driver");
1192824bd25SMichael Hund MODULE_LICENSE("GPL");
1202824bd25SMichael Hund
1212824bd25SMichael Hund /* All interrupt in transfers are collected in a ring buffer to
1222824bd25SMichael Hund * avoid racing conditions and get better performance of the driver.
1232824bd25SMichael Hund */
1242824bd25SMichael Hund static int ring_buffer_size = 128;
1253d1a4673SMilian Reichardt module_param(ring_buffer_size, int, 0000);
1262824bd25SMichael Hund MODULE_PARM_DESC(ring_buffer_size, "Read ring buffer size in reports");
1272824bd25SMichael Hund
1282824bd25SMichael Hund /* The write_buffer can contain more than one interrupt out transfer.
1292824bd25SMichael Hund */
1302824bd25SMichael Hund static int write_buffer_size = 10;
1313d1a4673SMilian Reichardt module_param(write_buffer_size, int, 0000);
1322824bd25SMichael Hund MODULE_PARM_DESC(write_buffer_size, "Write buffer size in reports");
1332824bd25SMichael Hund
1342824bd25SMichael Hund /* As of kernel version 2.6.4 ehci-hcd uses an
1352824bd25SMichael Hund * "only one interrupt transfer per frame" shortcut
1362824bd25SMichael Hund * to simplify the scheduling of periodic transfers.
1372824bd25SMichael Hund * This conflicts with our standard 1ms intervals for in and out URBs.
1382824bd25SMichael Hund * We use default intervals of 2ms for in and 2ms for out transfers,
1392824bd25SMichael Hund * which should be fast enough.
1402824bd25SMichael Hund * Increase the interval to allow more devices that do interrupt transfers,
1412824bd25SMichael Hund * or set to 1 to use the standard interval from the endpoint descriptors.
1422824bd25SMichael Hund */
1432824bd25SMichael Hund static int min_interrupt_in_interval = 2;
1443d1a4673SMilian Reichardt module_param(min_interrupt_in_interval, int, 0000);
1452824bd25SMichael Hund MODULE_PARM_DESC(min_interrupt_in_interval, "Minimum interrupt in interval in ms");
1462824bd25SMichael Hund
1472824bd25SMichael Hund static int min_interrupt_out_interval = 2;
1483d1a4673SMilian Reichardt module_param(min_interrupt_out_interval, int, 0000);
1492824bd25SMichael Hund MODULE_PARM_DESC(min_interrupt_out_interval, "Minimum interrupt out interval in ms");
1502824bd25SMichael Hund
1512824bd25SMichael Hund /* Structure to hold all of our device specific stuff */
1522824bd25SMichael Hund struct ld_usb {
153ce0d7d3fSDaniel Walker struct mutex mutex; /* locks this structure */
1542824bd25SMichael Hund struct usb_interface *intf; /* save off the usb interface pointer */
15558ecf131SJohan Hovold unsigned long disconnected:1;
1562824bd25SMichael Hund
1572824bd25SMichael Hund int open_count; /* number of times this port has been opened */
1582824bd25SMichael Hund
1592824bd25SMichael Hund char *ring_buffer;
1602824bd25SMichael Hund unsigned int ring_head;
1612824bd25SMichael Hund unsigned int ring_tail;
1622824bd25SMichael Hund
1632824bd25SMichael Hund wait_queue_head_t read_wait;
1642824bd25SMichael Hund wait_queue_head_t write_wait;
1652824bd25SMichael Hund
1662824bd25SMichael Hund char *interrupt_in_buffer;
1672824bd25SMichael Hund struct usb_endpoint_descriptor *interrupt_in_endpoint;
1682824bd25SMichael Hund struct urb *interrupt_in_urb;
1692824bd25SMichael Hund int interrupt_in_interval;
1702824bd25SMichael Hund size_t interrupt_in_endpoint_size;
1712824bd25SMichael Hund int interrupt_in_running;
1722824bd25SMichael Hund int interrupt_in_done;
1739d33efd9SOliver Neukum int buffer_overflow;
1749d33efd9SOliver Neukum spinlock_t rbsl;
1752824bd25SMichael Hund
1762824bd25SMichael Hund char *interrupt_out_buffer;
1772824bd25SMichael Hund struct usb_endpoint_descriptor *interrupt_out_endpoint;
1782824bd25SMichael Hund struct urb *interrupt_out_urb;
1792824bd25SMichael Hund int interrupt_out_interval;
1802824bd25SMichael Hund size_t interrupt_out_endpoint_size;
1812824bd25SMichael Hund int interrupt_out_busy;
1822824bd25SMichael Hund };
1832824bd25SMichael Hund
1842824bd25SMichael Hund static struct usb_driver ld_usb_driver;
1852824bd25SMichael Hund
1867e8455b1SLee Jones /*
1872824bd25SMichael Hund * ld_usb_abort_transfers
1882824bd25SMichael Hund * aborts transfers and frees associated data structures
1892824bd25SMichael Hund */
ld_usb_abort_transfers(struct ld_usb * dev)1902824bd25SMichael Hund static void ld_usb_abort_transfers(struct ld_usb *dev)
1912824bd25SMichael Hund {
1922824bd25SMichael Hund /* shutdown transfer */
1932824bd25SMichael Hund if (dev->interrupt_in_running) {
1942824bd25SMichael Hund dev->interrupt_in_running = 0;
1952824bd25SMichael Hund usb_kill_urb(dev->interrupt_in_urb);
1962824bd25SMichael Hund }
1972824bd25SMichael Hund if (dev->interrupt_out_busy)
1982824bd25SMichael Hund usb_kill_urb(dev->interrupt_out_urb);
1992824bd25SMichael Hund }
2002824bd25SMichael Hund
2017e8455b1SLee Jones /*
2022824bd25SMichael Hund * ld_usb_delete
2032824bd25SMichael Hund */
ld_usb_delete(struct ld_usb * dev)2042824bd25SMichael Hund static void ld_usb_delete(struct ld_usb *dev)
2052824bd25SMichael Hund {
2062824bd25SMichael Hund /* free data structures */
2072824bd25SMichael Hund usb_free_urb(dev->interrupt_in_urb);
2082824bd25SMichael Hund usb_free_urb(dev->interrupt_out_urb);
2092824bd25SMichael Hund kfree(dev->ring_buffer);
2102824bd25SMichael Hund kfree(dev->interrupt_in_buffer);
2112824bd25SMichael Hund kfree(dev->interrupt_out_buffer);
2122824bd25SMichael Hund kfree(dev);
2132824bd25SMichael Hund }
2142824bd25SMichael Hund
2157e8455b1SLee Jones /*
2162824bd25SMichael Hund * ld_usb_interrupt_in_callback
2172824bd25SMichael Hund */
ld_usb_interrupt_in_callback(struct urb * urb)2187d12e780SDavid Howells static void ld_usb_interrupt_in_callback(struct urb *urb)
2192824bd25SMichael Hund {
2202824bd25SMichael Hund struct ld_usb *dev = urb->context;
2212824bd25SMichael Hund size_t *actual_buffer;
2222824bd25SMichael Hund unsigned int next_ring_head;
223491c021eSGreg Kroah-Hartman int status = urb->status;
224d7cdbdd0SSebastian Andrzej Siewior unsigned long flags;
2252824bd25SMichael Hund int retval;
2262824bd25SMichael Hund
227491c021eSGreg Kroah-Hartman if (status) {
228491c021eSGreg Kroah-Hartman if (status == -ENOENT ||
229491c021eSGreg Kroah-Hartman status == -ECONNRESET ||
230491c021eSGreg Kroah-Hartman status == -ESHUTDOWN) {
2312824bd25SMichael Hund goto exit;
2322824bd25SMichael Hund } else {
233457163c4SGreg Kroah-Hartman dev_dbg(&dev->intf->dev,
234457163c4SGreg Kroah-Hartman "%s: nonzero status received: %d\n", __func__,
235457163c4SGreg Kroah-Hartman status);
236d7cdbdd0SSebastian Andrzej Siewior spin_lock_irqsave(&dev->rbsl, flags);
2372824bd25SMichael Hund goto resubmit; /* maybe we can recover */
2382824bd25SMichael Hund }
2392824bd25SMichael Hund }
2402824bd25SMichael Hund
241d7cdbdd0SSebastian Andrzej Siewior spin_lock_irqsave(&dev->rbsl, flags);
2422824bd25SMichael Hund if (urb->actual_length > 0) {
2432824bd25SMichael Hund next_ring_head = (dev->ring_head+1) % ring_buffer_size;
2442824bd25SMichael Hund if (next_ring_head != dev->ring_tail) {
2452824bd25SMichael Hund actual_buffer = (size_t *)(dev->ring_buffer + dev->ring_head * (sizeof(size_t)+dev->interrupt_in_endpoint_size));
2462824bd25SMichael Hund /* actual_buffer gets urb->actual_length + interrupt_in_buffer */
2472824bd25SMichael Hund *actual_buffer = urb->actual_length;
2482824bd25SMichael Hund memcpy(actual_buffer+1, dev->interrupt_in_buffer, urb->actual_length);
2492824bd25SMichael Hund dev->ring_head = next_ring_head;
250457163c4SGreg Kroah-Hartman dev_dbg(&dev->intf->dev, "%s: received %d bytes\n",
251441b62c1SHarvey Harrison __func__, urb->actual_length);
2529d33efd9SOliver Neukum } else {
2532824bd25SMichael Hund dev_warn(&dev->intf->dev,
2542824bd25SMichael Hund "Ring buffer overflow, %d bytes dropped\n",
2552824bd25SMichael Hund urb->actual_length);
2569d33efd9SOliver Neukum dev->buffer_overflow = 1;
2579d33efd9SOliver Neukum }
2582824bd25SMichael Hund }
2592824bd25SMichael Hund
2602824bd25SMichael Hund resubmit:
2612824bd25SMichael Hund /* resubmit if we're still running */
26258ecf131SJohan Hovold if (dev->interrupt_in_running && !dev->buffer_overflow) {
2632824bd25SMichael Hund retval = usb_submit_urb(dev->interrupt_in_urb, GFP_ATOMIC);
2649d33efd9SOliver Neukum if (retval) {
2652824bd25SMichael Hund dev_err(&dev->intf->dev,
2662824bd25SMichael Hund "usb_submit_urb failed (%d)\n", retval);
2679d33efd9SOliver Neukum dev->buffer_overflow = 1;
2682824bd25SMichael Hund }
2699d33efd9SOliver Neukum }
270d7cdbdd0SSebastian Andrzej Siewior spin_unlock_irqrestore(&dev->rbsl, flags);
2712824bd25SMichael Hund exit:
2722824bd25SMichael Hund dev->interrupt_in_done = 1;
2732824bd25SMichael Hund wake_up_interruptible(&dev->read_wait);
2742824bd25SMichael Hund }
2752824bd25SMichael Hund
2767e8455b1SLee Jones /*
2772824bd25SMichael Hund * ld_usb_interrupt_out_callback
2782824bd25SMichael Hund */
ld_usb_interrupt_out_callback(struct urb * urb)2797d12e780SDavid Howells static void ld_usb_interrupt_out_callback(struct urb *urb)
2802824bd25SMichael Hund {
2812824bd25SMichael Hund struct ld_usb *dev = urb->context;
282491c021eSGreg Kroah-Hartman int status = urb->status;
2832824bd25SMichael Hund
2842824bd25SMichael Hund /* sync/async unlink faults aren't errors */
285491c021eSGreg Kroah-Hartman if (status && !(status == -ENOENT ||
286491c021eSGreg Kroah-Hartman status == -ECONNRESET ||
287491c021eSGreg Kroah-Hartman status == -ESHUTDOWN))
288457163c4SGreg Kroah-Hartman dev_dbg(&dev->intf->dev,
2892824bd25SMichael Hund "%s - nonzero write interrupt status received: %d\n",
290441b62c1SHarvey Harrison __func__, status);
2912824bd25SMichael Hund
2922824bd25SMichael Hund dev->interrupt_out_busy = 0;
2932824bd25SMichael Hund wake_up_interruptible(&dev->write_wait);
2942824bd25SMichael Hund }
2952824bd25SMichael Hund
2967e8455b1SLee Jones /*
2972824bd25SMichael Hund * ld_usb_open
2982824bd25SMichael Hund */
ld_usb_open(struct inode * inode,struct file * file)2992824bd25SMichael Hund static int ld_usb_open(struct inode *inode, struct file *file)
3002824bd25SMichael Hund {
3012824bd25SMichael Hund struct ld_usb *dev;
3022824bd25SMichael Hund int subminor;
303d4ead16fSAlan Stern int retval;
3042824bd25SMichael Hund struct usb_interface *interface;
3052824bd25SMichael Hund
306c5bf68feSKirill Smelkov stream_open(inode, file);
3072824bd25SMichael Hund subminor = iminor(inode);
3082824bd25SMichael Hund
3092824bd25SMichael Hund interface = usb_find_interface(&ld_usb_driver, subminor);
3102824bd25SMichael Hund
3112824bd25SMichael Hund if (!interface) {
312b22862e5SGreg Kroah-Hartman printk(KERN_ERR "%s - error, can't find device for minor %d\n",
313441b62c1SHarvey Harrison __func__, subminor);
314d4ead16fSAlan Stern return -ENODEV;
3152824bd25SMichael Hund }
3162824bd25SMichael Hund
3172824bd25SMichael Hund dev = usb_get_intfdata(interface);
3182824bd25SMichael Hund
3196248c52fSOliver Neukum if (!dev)
320d4ead16fSAlan Stern return -ENODEV;
3212824bd25SMichael Hund
3222824bd25SMichael Hund /* lock this device */
3236248c52fSOliver Neukum if (mutex_lock_interruptible(&dev->mutex))
324d4ead16fSAlan Stern return -ERESTARTSYS;
3252824bd25SMichael Hund
3262824bd25SMichael Hund /* allow opening only once */
3272824bd25SMichael Hund if (dev->open_count) {
3282824bd25SMichael Hund retval = -EBUSY;
3292824bd25SMichael Hund goto unlock_exit;
3302824bd25SMichael Hund }
3312824bd25SMichael Hund dev->open_count = 1;
3322824bd25SMichael Hund
3332824bd25SMichael Hund /* initialize in direction */
3342824bd25SMichael Hund dev->ring_head = 0;
3352824bd25SMichael Hund dev->ring_tail = 0;
3369d33efd9SOliver Neukum dev->buffer_overflow = 0;
3372824bd25SMichael Hund usb_fill_int_urb(dev->interrupt_in_urb,
3382824bd25SMichael Hund interface_to_usbdev(interface),
3392824bd25SMichael Hund usb_rcvintpipe(interface_to_usbdev(interface),
3402824bd25SMichael Hund dev->interrupt_in_endpoint->bEndpointAddress),
3412824bd25SMichael Hund dev->interrupt_in_buffer,
3422824bd25SMichael Hund dev->interrupt_in_endpoint_size,
3432824bd25SMichael Hund ld_usb_interrupt_in_callback,
3442824bd25SMichael Hund dev,
3452824bd25SMichael Hund dev->interrupt_in_interval);
3462824bd25SMichael Hund
3472824bd25SMichael Hund dev->interrupt_in_running = 1;
3482824bd25SMichael Hund dev->interrupt_in_done = 0;
3492824bd25SMichael Hund
3502824bd25SMichael Hund retval = usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL);
3512824bd25SMichael Hund if (retval) {
3522824bd25SMichael Hund dev_err(&interface->dev, "Couldn't submit interrupt_in_urb %d\n", retval);
3532824bd25SMichael Hund dev->interrupt_in_running = 0;
3542824bd25SMichael Hund dev->open_count = 0;
3552824bd25SMichael Hund goto unlock_exit;
3562824bd25SMichael Hund }
3572824bd25SMichael Hund
3582824bd25SMichael Hund /* save device in the file's private structure */
3592824bd25SMichael Hund file->private_data = dev;
3602824bd25SMichael Hund
3612824bd25SMichael Hund unlock_exit:
362ce0d7d3fSDaniel Walker mutex_unlock(&dev->mutex);
3632824bd25SMichael Hund
3642824bd25SMichael Hund return retval;
3652824bd25SMichael Hund }
3662824bd25SMichael Hund
3677e8455b1SLee Jones /*
3682824bd25SMichael Hund * ld_usb_release
3692824bd25SMichael Hund */
ld_usb_release(struct inode * inode,struct file * file)3702824bd25SMichael Hund static int ld_usb_release(struct inode *inode, struct file *file)
3712824bd25SMichael Hund {
3722824bd25SMichael Hund struct ld_usb *dev;
3732824bd25SMichael Hund int retval = 0;
3742824bd25SMichael Hund
3752824bd25SMichael Hund dev = file->private_data;
3762824bd25SMichael Hund
3772824bd25SMichael Hund if (dev == NULL) {
3782824bd25SMichael Hund retval = -ENODEV;
3792824bd25SMichael Hund goto exit;
3802824bd25SMichael Hund }
3812824bd25SMichael Hund
382b14a3904SJohan Hovold mutex_lock(&dev->mutex);
3832824bd25SMichael Hund
3842824bd25SMichael Hund if (dev->open_count != 1) {
3852824bd25SMichael Hund retval = -ENODEV;
3862824bd25SMichael Hund goto unlock_exit;
3872824bd25SMichael Hund }
38858ecf131SJohan Hovold if (dev->disconnected) {
3892824bd25SMichael Hund /* the device was unplugged before the file was released */
390ce0d7d3fSDaniel Walker mutex_unlock(&dev->mutex);
3912824bd25SMichael Hund /* unlock here as ld_usb_delete frees dev */
3922824bd25SMichael Hund ld_usb_delete(dev);
3932824bd25SMichael Hund goto exit;
3942824bd25SMichael Hund }
3952824bd25SMichael Hund
3962824bd25SMichael Hund /* wait until write transfer is finished */
3972824bd25SMichael Hund if (dev->interrupt_out_busy)
3982824bd25SMichael Hund wait_event_interruptible_timeout(dev->write_wait, !dev->interrupt_out_busy, 2 * HZ);
3992824bd25SMichael Hund ld_usb_abort_transfers(dev);
4002824bd25SMichael Hund dev->open_count = 0;
4012824bd25SMichael Hund
4022824bd25SMichael Hund unlock_exit:
403ce0d7d3fSDaniel Walker mutex_unlock(&dev->mutex);
4042824bd25SMichael Hund
4052824bd25SMichael Hund exit:
4062824bd25SMichael Hund return retval;
4072824bd25SMichael Hund }
4082824bd25SMichael Hund
4097e8455b1SLee Jones /*
4102824bd25SMichael Hund * ld_usb_poll
4112824bd25SMichael Hund */
ld_usb_poll(struct file * file,poll_table * wait)412afc9a42bSAl Viro static __poll_t ld_usb_poll(struct file *file, poll_table *wait)
4132824bd25SMichael Hund {
4142824bd25SMichael Hund struct ld_usb *dev;
415afc9a42bSAl Viro __poll_t mask = 0;
4162824bd25SMichael Hund
4172824bd25SMichael Hund dev = file->private_data;
4182824bd25SMichael Hund
41958ecf131SJohan Hovold if (dev->disconnected)
420a9a08845SLinus Torvalds return EPOLLERR | EPOLLHUP;
421d12b85e7SOliver Neukum
4222824bd25SMichael Hund poll_wait(file, &dev->read_wait, wait);
4232824bd25SMichael Hund poll_wait(file, &dev->write_wait, wait);
4242824bd25SMichael Hund
4252824bd25SMichael Hund if (dev->ring_head != dev->ring_tail)
426a9a08845SLinus Torvalds mask |= EPOLLIN | EPOLLRDNORM;
4272824bd25SMichael Hund if (!dev->interrupt_out_busy)
428a9a08845SLinus Torvalds mask |= EPOLLOUT | EPOLLWRNORM;
4292824bd25SMichael Hund
4302824bd25SMichael Hund return mask;
4312824bd25SMichael Hund }
4322824bd25SMichael Hund
4337e8455b1SLee Jones /*
4342824bd25SMichael Hund * ld_usb_read
4352824bd25SMichael Hund */
ld_usb_read(struct file * file,char __user * buffer,size_t count,loff_t * ppos)4362824bd25SMichael Hund static ssize_t ld_usb_read(struct file *file, char __user *buffer, size_t count,
4372824bd25SMichael Hund loff_t *ppos)
4382824bd25SMichael Hund {
4392824bd25SMichael Hund struct ld_usb *dev;
4402824bd25SMichael Hund size_t *actual_buffer;
4412824bd25SMichael Hund size_t bytes_to_read;
4422824bd25SMichael Hund int retval = 0;
4439d33efd9SOliver Neukum int rv;
4442824bd25SMichael Hund
4452824bd25SMichael Hund dev = file->private_data;
4462824bd25SMichael Hund
4472824bd25SMichael Hund /* verify that we actually have some data to read */
4482824bd25SMichael Hund if (count == 0)
4492824bd25SMichael Hund goto exit;
4502824bd25SMichael Hund
4512824bd25SMichael Hund /* lock this object */
452ce0d7d3fSDaniel Walker if (mutex_lock_interruptible(&dev->mutex)) {
4532824bd25SMichael Hund retval = -ERESTARTSYS;
4542824bd25SMichael Hund goto exit;
4552824bd25SMichael Hund }
4562824bd25SMichael Hund
4572824bd25SMichael Hund /* verify that the device wasn't unplugged */
45858ecf131SJohan Hovold if (dev->disconnected) {
4592824bd25SMichael Hund retval = -ENODEV;
460b22862e5SGreg Kroah-Hartman printk(KERN_ERR "ldusb: No device or device unplugged %d\n", retval);
4612824bd25SMichael Hund goto unlock_exit;
4622824bd25SMichael Hund }
4632824bd25SMichael Hund
4642824bd25SMichael Hund /* wait for data */
4659d33efd9SOliver Neukum spin_lock_irq(&dev->rbsl);
4667a6f22d7SJohan Hovold while (dev->ring_head == dev->ring_tail) {
4679d33efd9SOliver Neukum dev->interrupt_in_done = 0;
4689d33efd9SOliver Neukum spin_unlock_irq(&dev->rbsl);
4692824bd25SMichael Hund if (file->f_flags & O_NONBLOCK) {
4702824bd25SMichael Hund retval = -EAGAIN;
4712824bd25SMichael Hund goto unlock_exit;
4722824bd25SMichael Hund }
4732824bd25SMichael Hund retval = wait_event_interruptible(dev->read_wait, dev->interrupt_in_done);
4742824bd25SMichael Hund if (retval < 0)
4752824bd25SMichael Hund goto unlock_exit;
4767a6f22d7SJohan Hovold
4777a6f22d7SJohan Hovold spin_lock_irq(&dev->rbsl);
4782824bd25SMichael Hund }
4797a6f22d7SJohan Hovold spin_unlock_irq(&dev->rbsl);
4802824bd25SMichael Hund
4812824bd25SMichael Hund /* actual_buffer contains actual_length + interrupt_in_buffer */
4822824bd25SMichael Hund actual_buffer = (size_t *)(dev->ring_buffer + dev->ring_tail * (sizeof(size_t)+dev->interrupt_in_endpoint_size));
4837a6f22d7SJohan Hovold if (*actual_buffer > dev->interrupt_in_endpoint_size) {
4847a6f22d7SJohan Hovold retval = -EIO;
4857a6f22d7SJohan Hovold goto unlock_exit;
4867a6f22d7SJohan Hovold }
4872824bd25SMichael Hund bytes_to_read = min(count, *actual_buffer);
4882824bd25SMichael Hund if (bytes_to_read < *actual_buffer)
48988f6bf38SJohan Hovold dev_warn(&dev->intf->dev, "Read buffer overflow, %zu bytes dropped\n",
4902824bd25SMichael Hund *actual_buffer-bytes_to_read);
4912824bd25SMichael Hund
4922824bd25SMichael Hund /* copy one interrupt_in_buffer from ring_buffer into userspace */
4932824bd25SMichael Hund if (copy_to_user(buffer, actual_buffer+1, bytes_to_read)) {
4942824bd25SMichael Hund retval = -EFAULT;
4952824bd25SMichael Hund goto unlock_exit;
4962824bd25SMichael Hund }
4972824bd25SMichael Hund retval = bytes_to_read;
4982824bd25SMichael Hund
4999d33efd9SOliver Neukum spin_lock_irq(&dev->rbsl);
500d98ee2a1SJohan Hovold dev->ring_tail = (dev->ring_tail + 1) % ring_buffer_size;
501d98ee2a1SJohan Hovold
5029d33efd9SOliver Neukum if (dev->buffer_overflow) {
5039d33efd9SOliver Neukum dev->buffer_overflow = 0;
5049d33efd9SOliver Neukum spin_unlock_irq(&dev->rbsl);
5059d33efd9SOliver Neukum rv = usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL);
5069d33efd9SOliver Neukum if (rv < 0)
5079d33efd9SOliver Neukum dev->buffer_overflow = 1;
5089d33efd9SOliver Neukum } else {
5099d33efd9SOliver Neukum spin_unlock_irq(&dev->rbsl);
5109d33efd9SOliver Neukum }
5119d33efd9SOliver Neukum
5122824bd25SMichael Hund unlock_exit:
5132824bd25SMichael Hund /* unlock the device */
514ce0d7d3fSDaniel Walker mutex_unlock(&dev->mutex);
5152824bd25SMichael Hund
5162824bd25SMichael Hund exit:
5172824bd25SMichael Hund return retval;
5182824bd25SMichael Hund }
5192824bd25SMichael Hund
5207e8455b1SLee Jones /*
5212824bd25SMichael Hund * ld_usb_write
5222824bd25SMichael Hund */
ld_usb_write(struct file * file,const char __user * buffer,size_t count,loff_t * ppos)5232824bd25SMichael Hund static ssize_t ld_usb_write(struct file *file, const char __user *buffer,
5242824bd25SMichael Hund size_t count, loff_t *ppos)
5252824bd25SMichael Hund {
5262824bd25SMichael Hund struct ld_usb *dev;
5272824bd25SMichael Hund size_t bytes_to_write;
5282824bd25SMichael Hund int retval = 0;
5292824bd25SMichael Hund
5302824bd25SMichael Hund dev = file->private_data;
5312824bd25SMichael Hund
5322824bd25SMichael Hund /* verify that we actually have some data to write */
5332824bd25SMichael Hund if (count == 0)
5342824bd25SMichael Hund goto exit;
5352824bd25SMichael Hund
5362824bd25SMichael Hund /* lock this object */
537ce0d7d3fSDaniel Walker if (mutex_lock_interruptible(&dev->mutex)) {
5382824bd25SMichael Hund retval = -ERESTARTSYS;
5392824bd25SMichael Hund goto exit;
5402824bd25SMichael Hund }
5412824bd25SMichael Hund
5422824bd25SMichael Hund /* verify that the device wasn't unplugged */
54358ecf131SJohan Hovold if (dev->disconnected) {
5442824bd25SMichael Hund retval = -ENODEV;
545b22862e5SGreg Kroah-Hartman printk(KERN_ERR "ldusb: No device or device unplugged %d\n", retval);
5462824bd25SMichael Hund goto unlock_exit;
5472824bd25SMichael Hund }
5482824bd25SMichael Hund
5492824bd25SMichael Hund /* wait until previous transfer is finished */
5502824bd25SMichael Hund if (dev->interrupt_out_busy) {
5512824bd25SMichael Hund if (file->f_flags & O_NONBLOCK) {
5522824bd25SMichael Hund retval = -EAGAIN;
5532824bd25SMichael Hund goto unlock_exit;
5542824bd25SMichael Hund }
5552824bd25SMichael Hund retval = wait_event_interruptible(dev->write_wait, !dev->interrupt_out_busy);
5562824bd25SMichael Hund if (retval < 0) {
5572824bd25SMichael Hund goto unlock_exit;
5582824bd25SMichael Hund }
5592824bd25SMichael Hund }
5602824bd25SMichael Hund
5612824bd25SMichael Hund /* write the data into interrupt_out_buffer from userspace */
5622824bd25SMichael Hund bytes_to_write = min(count, write_buffer_size*dev->interrupt_out_endpoint_size);
5632824bd25SMichael Hund if (bytes_to_write < count)
56488f6bf38SJohan Hovold dev_warn(&dev->intf->dev, "Write buffer overflow, %zu bytes dropped\n",
56588f6bf38SJohan Hovold count - bytes_to_write);
56688f6bf38SJohan Hovold dev_dbg(&dev->intf->dev, "%s: count = %zu, bytes_to_write = %zu\n",
567457163c4SGreg Kroah-Hartman __func__, count, bytes_to_write);
5682824bd25SMichael Hund
5692824bd25SMichael Hund if (copy_from_user(dev->interrupt_out_buffer, buffer, bytes_to_write)) {
5702824bd25SMichael Hund retval = -EFAULT;
5712824bd25SMichael Hund goto unlock_exit;
5722824bd25SMichael Hund }
5732824bd25SMichael Hund
5742824bd25SMichael Hund if (dev->interrupt_out_endpoint == NULL) {
5752824bd25SMichael Hund /* try HID_REQ_SET_REPORT=9 on control_endpoint instead of interrupt_out_endpoint */
5762824bd25SMichael Hund retval = usb_control_msg(interface_to_usbdev(dev->intf),
5772824bd25SMichael Hund usb_sndctrlpipe(interface_to_usbdev(dev->intf), 0),
5782824bd25SMichael Hund 9,
5792824bd25SMichael Hund USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
5802824bd25SMichael Hund 1 << 8, 0,
5812824bd25SMichael Hund dev->interrupt_out_buffer,
5822824bd25SMichael Hund bytes_to_write,
58352403cfbSJohan Hovold USB_CTRL_SET_TIMEOUT);
5842824bd25SMichael Hund if (retval < 0)
585b22862e5SGreg Kroah-Hartman dev_err(&dev->intf->dev,
586b22862e5SGreg Kroah-Hartman "Couldn't submit HID_REQ_SET_REPORT %d\n",
587b22862e5SGreg Kroah-Hartman retval);
5882824bd25SMichael Hund goto unlock_exit;
5892824bd25SMichael Hund }
5902824bd25SMichael Hund
5912824bd25SMichael Hund /* send off the urb */
5922824bd25SMichael Hund usb_fill_int_urb(dev->interrupt_out_urb,
5932824bd25SMichael Hund interface_to_usbdev(dev->intf),
5942824bd25SMichael Hund usb_sndintpipe(interface_to_usbdev(dev->intf),
5952824bd25SMichael Hund dev->interrupt_out_endpoint->bEndpointAddress),
5962824bd25SMichael Hund dev->interrupt_out_buffer,
5972824bd25SMichael Hund bytes_to_write,
5982824bd25SMichael Hund ld_usb_interrupt_out_callback,
5992824bd25SMichael Hund dev,
6002824bd25SMichael Hund dev->interrupt_out_interval);
6012824bd25SMichael Hund
6022824bd25SMichael Hund dev->interrupt_out_busy = 1;
6032824bd25SMichael Hund wmb();
6042824bd25SMichael Hund
6052824bd25SMichael Hund retval = usb_submit_urb(dev->interrupt_out_urb, GFP_KERNEL);
6062824bd25SMichael Hund if (retval) {
6072824bd25SMichael Hund dev->interrupt_out_busy = 0;
608b22862e5SGreg Kroah-Hartman dev_err(&dev->intf->dev,
609b22862e5SGreg Kroah-Hartman "Couldn't submit interrupt_out_urb %d\n", retval);
6102824bd25SMichael Hund goto unlock_exit;
6112824bd25SMichael Hund }
6122824bd25SMichael Hund retval = bytes_to_write;
6132824bd25SMichael Hund
6142824bd25SMichael Hund unlock_exit:
6152824bd25SMichael Hund /* unlock the device */
616ce0d7d3fSDaniel Walker mutex_unlock(&dev->mutex);
6172824bd25SMichael Hund
6182824bd25SMichael Hund exit:
6192824bd25SMichael Hund return retval;
6202824bd25SMichael Hund }
6212824bd25SMichael Hund
6222824bd25SMichael Hund /* file operations needed when we register this driver */
623066202ddSLuiz Fernando N. Capitulino static const struct file_operations ld_usb_fops = {
6242824bd25SMichael Hund .owner = THIS_MODULE,
6252824bd25SMichael Hund .read = ld_usb_read,
6262824bd25SMichael Hund .write = ld_usb_write,
6272824bd25SMichael Hund .open = ld_usb_open,
6282824bd25SMichael Hund .release = ld_usb_release,
6292824bd25SMichael Hund .poll = ld_usb_poll,
6306038f373SArnd Bergmann .llseek = no_llseek,
6312824bd25SMichael Hund };
6322824bd25SMichael Hund
6332824bd25SMichael Hund /*
6342824bd25SMichael Hund * usb class driver info in order to get a minor number from the usb core,
635595b14cbSGreg Kroah-Hartman * and to have the device registered with the driver core
6362824bd25SMichael Hund */
6372824bd25SMichael Hund static struct usb_class_driver ld_usb_class = {
6382824bd25SMichael Hund .name = "ldusb%d",
6392824bd25SMichael Hund .fops = &ld_usb_fops,
6402824bd25SMichael Hund .minor_base = USB_LD_MINOR_BASE,
6412824bd25SMichael Hund };
6422824bd25SMichael Hund
6437e8455b1SLee Jones /*
6442824bd25SMichael Hund * ld_usb_probe
6452824bd25SMichael Hund *
6462824bd25SMichael Hund * Called by the usb core when a new device is connected that it thinks
6472824bd25SMichael Hund * this driver might be interested in.
6482824bd25SMichael Hund */
ld_usb_probe(struct usb_interface * intf,const struct usb_device_id * id)6492824bd25SMichael Hund static int ld_usb_probe(struct usb_interface *intf, const struct usb_device_id *id)
6502824bd25SMichael Hund {
6512824bd25SMichael Hund struct usb_device *udev = interface_to_usbdev(intf);
6522824bd25SMichael Hund struct ld_usb *dev = NULL;
6532824bd25SMichael Hund struct usb_host_interface *iface_desc;
6542824bd25SMichael Hund char *buffer;
6552824bd25SMichael Hund int retval = -ENOMEM;
6562707ca16SJohan Hovold int res;
6572824bd25SMichael Hund
658b595076aSUwe Kleine-König /* allocate memory for our device state and initialize it */
6592824bd25SMichael Hund
6601144cf7aSOliver Neukum dev = kzalloc(sizeof(*dev), GFP_KERNEL);
6616714ffaeSWolfram Sang if (!dev)
6622824bd25SMichael Hund goto exit;
663ce0d7d3fSDaniel Walker mutex_init(&dev->mutex);
6649d33efd9SOliver Neukum spin_lock_init(&dev->rbsl);
6652824bd25SMichael Hund dev->intf = intf;
6662824bd25SMichael Hund init_waitqueue_head(&dev->read_wait);
6672824bd25SMichael Hund init_waitqueue_head(&dev->write_wait);
6682824bd25SMichael Hund
6692824bd25SMichael Hund /* workaround for early firmware versions on fast computers */
6702824bd25SMichael Hund if ((le16_to_cpu(udev->descriptor.idVendor) == USB_VENDOR_ID_LD) &&
671ba3e66e9SMichael Hund ((le16_to_cpu(udev->descriptor.idProduct) == USB_DEVICE_ID_LD_CASSY) ||
672ba3e66e9SMichael Hund (le16_to_cpu(udev->descriptor.idProduct) == USB_DEVICE_ID_LD_COM3LAB)) &&
6732824bd25SMichael Hund (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x103)) {
6742824bd25SMichael Hund buffer = kmalloc(256, GFP_KERNEL);
6756714ffaeSWolfram Sang if (!buffer)
676a6db592eSMichael Hund goto error;
6772824bd25SMichael Hund /* usb_string makes SETUP+STALL to leave always ControlReadLoop */
6782824bd25SMichael Hund usb_string(udev, 255, buffer, 256);
6792824bd25SMichael Hund kfree(buffer);
6802824bd25SMichael Hund }
6812824bd25SMichael Hund
6822824bd25SMichael Hund iface_desc = intf->cur_altsetting;
6832824bd25SMichael Hund
6842707ca16SJohan Hovold res = usb_find_last_int_in_endpoint(iface_desc,
6852707ca16SJohan Hovold &dev->interrupt_in_endpoint);
6862707ca16SJohan Hovold if (res) {
6872824bd25SMichael Hund dev_err(&intf->dev, "Interrupt in endpoint not found\n");
6882707ca16SJohan Hovold retval = res;
6892824bd25SMichael Hund goto error;
6902824bd25SMichael Hund }
6912707ca16SJohan Hovold
6922707ca16SJohan Hovold res = usb_find_last_int_out_endpoint(iface_desc,
6932707ca16SJohan Hovold &dev->interrupt_out_endpoint);
6942707ca16SJohan Hovold if (res)
6952824bd25SMichael Hund dev_warn(&intf->dev, "Interrupt out endpoint not found (using control endpoint instead)\n");
6962824bd25SMichael Hund
69729cc8897SKuninori Morimoto dev->interrupt_in_endpoint_size = usb_endpoint_maxp(dev->interrupt_in_endpoint);
6987a6f22d7SJohan Hovold dev->ring_buffer = kcalloc(ring_buffer_size,
6996da2ec56SKees Cook sizeof(size_t) + dev->interrupt_in_endpoint_size,
7006da2ec56SKees Cook GFP_KERNEL);
7016714ffaeSWolfram Sang if (!dev->ring_buffer)
7022824bd25SMichael Hund goto error;
7032824bd25SMichael Hund dev->interrupt_in_buffer = kmalloc(dev->interrupt_in_endpoint_size, GFP_KERNEL);
7046714ffaeSWolfram Sang if (!dev->interrupt_in_buffer)
7052824bd25SMichael Hund goto error;
7062824bd25SMichael Hund dev->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
7072d403903SWolfram Sang if (!dev->interrupt_in_urb)
7082824bd25SMichael Hund goto error;
70929cc8897SKuninori Morimoto dev->interrupt_out_endpoint_size = dev->interrupt_out_endpoint ? usb_endpoint_maxp(dev->interrupt_out_endpoint) :
7102824bd25SMichael Hund udev->descriptor.bMaxPacketSize0;
7116da2ec56SKees Cook dev->interrupt_out_buffer =
7126da2ec56SKees Cook kmalloc_array(write_buffer_size,
7136da2ec56SKees Cook dev->interrupt_out_endpoint_size, GFP_KERNEL);
7146714ffaeSWolfram Sang if (!dev->interrupt_out_buffer)
7152824bd25SMichael Hund goto error;
7162824bd25SMichael Hund dev->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL);
7172d403903SWolfram Sang if (!dev->interrupt_out_urb)
7182824bd25SMichael Hund goto error;
719*efa2bebfSJiangshan Yi dev->interrupt_in_interval = max_t(int, min_interrupt_in_interval,
720*efa2bebfSJiangshan Yi dev->interrupt_in_endpoint->bInterval);
7212824bd25SMichael Hund if (dev->interrupt_out_endpoint)
722*efa2bebfSJiangshan Yi dev->interrupt_out_interval = max_t(int, min_interrupt_out_interval,
723*efa2bebfSJiangshan Yi dev->interrupt_out_endpoint->bInterval);
7242824bd25SMichael Hund
7252824bd25SMichael Hund /* we can register the device now, as it is ready */
7262824bd25SMichael Hund usb_set_intfdata(intf, dev);
7272824bd25SMichael Hund
7282824bd25SMichael Hund retval = usb_register_dev(intf, &ld_usb_class);
7292824bd25SMichael Hund if (retval) {
7302824bd25SMichael Hund /* something prevented us from registering this driver */
7312824bd25SMichael Hund dev_err(&intf->dev, "Not able to get a minor for this device.\n");
7322824bd25SMichael Hund usb_set_intfdata(intf, NULL);
7332824bd25SMichael Hund goto error;
7342824bd25SMichael Hund }
7352824bd25SMichael Hund
7362824bd25SMichael Hund /* let the user know what node this device is now attached to */
7372824bd25SMichael Hund dev_info(&intf->dev, "LD USB Device #%d now attached to major %d minor %d\n",
7382824bd25SMichael Hund (intf->minor - USB_LD_MINOR_BASE), USB_MAJOR, intf->minor);
7392824bd25SMichael Hund
7402824bd25SMichael Hund exit:
7412824bd25SMichael Hund return retval;
7422824bd25SMichael Hund
7432824bd25SMichael Hund error:
7442824bd25SMichael Hund ld_usb_delete(dev);
7452824bd25SMichael Hund
7462824bd25SMichael Hund return retval;
7472824bd25SMichael Hund }
7482824bd25SMichael Hund
7497e8455b1SLee Jones /*
7502824bd25SMichael Hund * ld_usb_disconnect
7512824bd25SMichael Hund *
7522824bd25SMichael Hund * Called by the usb core when the device is removed from the system.
7532824bd25SMichael Hund */
ld_usb_disconnect(struct usb_interface * intf)7542824bd25SMichael Hund static void ld_usb_disconnect(struct usb_interface *intf)
7552824bd25SMichael Hund {
7562824bd25SMichael Hund struct ld_usb *dev;
7572824bd25SMichael Hund int minor;
7582824bd25SMichael Hund
7592824bd25SMichael Hund dev = usb_get_intfdata(intf);
7602824bd25SMichael Hund usb_set_intfdata(intf, NULL);
7612824bd25SMichael Hund
7622824bd25SMichael Hund minor = intf->minor;
7632824bd25SMichael Hund
7642824bd25SMichael Hund /* give back our minor */
7652824bd25SMichael Hund usb_deregister_dev(intf, &ld_usb_class);
7662824bd25SMichael Hund
76758ecf131SJohan Hovold usb_poison_urb(dev->interrupt_in_urb);
76858ecf131SJohan Hovold usb_poison_urb(dev->interrupt_out_urb);
76958ecf131SJohan Hovold
770ce0d7d3fSDaniel Walker mutex_lock(&dev->mutex);
771d4ead16fSAlan Stern
7722824bd25SMichael Hund /* if the device is not opened, then we clean up right now */
7732824bd25SMichael Hund if (!dev->open_count) {
774ce0d7d3fSDaniel Walker mutex_unlock(&dev->mutex);
7752824bd25SMichael Hund ld_usb_delete(dev);
7762824bd25SMichael Hund } else {
77758ecf131SJohan Hovold dev->disconnected = 1;
778d12b85e7SOliver Neukum /* wake up pollers */
779d12b85e7SOliver Neukum wake_up_interruptible_all(&dev->read_wait);
780d12b85e7SOliver Neukum wake_up_interruptible_all(&dev->write_wait);
781ce0d7d3fSDaniel Walker mutex_unlock(&dev->mutex);
7822824bd25SMichael Hund }
7832824bd25SMichael Hund
7842824bd25SMichael Hund dev_info(&intf->dev, "LD USB Device #%d now disconnected\n",
7852824bd25SMichael Hund (minor - USB_LD_MINOR_BASE));
7862824bd25SMichael Hund }
7872824bd25SMichael Hund
7882824bd25SMichael Hund /* usb specific object needed to register this driver with the usb subsystem */
7892824bd25SMichael Hund static struct usb_driver ld_usb_driver = {
7902824bd25SMichael Hund .name = "ldusb",
7912824bd25SMichael Hund .probe = ld_usb_probe,
7922824bd25SMichael Hund .disconnect = ld_usb_disconnect,
7932824bd25SMichael Hund .id_table = ld_usb_table,
7942824bd25SMichael Hund };
7952824bd25SMichael Hund
79665db4305SGreg Kroah-Hartman module_usb_driver(ld_usb_driver);
7972824bd25SMichael Hund
798