xref: /linux/drivers/usb/misc/ldusb.c (revision f4cdf7ca9a1fdcca413157df19753f388a5a224e)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Generic USB driver for report based interrupt in/out devices
4  * like LD Didactic's USB devices. LD Didactic's USB devices are
5  * HID devices which do not use HID report definitons (they use
6  * raw interrupt in and our reports only for communication).
7  *
8  * This driver uses a ring buffer for time critical reading of
9  * interrupt in reports and provides read and write methods for
10  * raw interrupt reports (similar to the Windows HID driver).
11  * Devices based on the book USB COMPLETE by Jan Axelson may need
12  * such a compatibility to the Windows HID driver.
13  *
14  * Copyright (C) 2005 Michael Hund <mhund@ld-didactic.de>
15  *
16  * Derived from Lego USB Tower driver
17  * Copyright (C) 2003 David Glance <advidgsf@sourceforge.net>
18  *		 2001-2004 Juergen Stuber <starblue@users.sourceforge.net>
19  */
20 
21 #include <linux/kernel.h>
22 #include <linux/errno.h>
23 #include <linux/slab.h>
24 #include <linux/module.h>
25 #include <linux/mutex.h>
26 
27 #include <linux/uaccess.h>
28 #include <linux/input.h>
29 #include <linux/usb.h>
30 #include <linux/poll.h>
31 
32 /* Define these values to match your devices */
33 #define USB_VENDOR_ID_LD		0x0f11	/* USB Vendor ID of LD Didactic GmbH */
34 #define USB_DEVICE_ID_LD_CASSY		0x1000	/* USB Product ID of CASSY-S modules with 8 bytes endpoint size */
35 #define USB_DEVICE_ID_LD_CASSY2		0x1001	/* USB Product ID of CASSY-S modules with 64 bytes endpoint size */
36 #define USB_DEVICE_ID_LD_POCKETCASSY	0x1010	/* USB Product ID of Pocket-CASSY */
37 #define USB_DEVICE_ID_LD_POCKETCASSY2	0x1011	/* USB Product ID of Pocket-CASSY 2 (reserved) */
38 #define USB_DEVICE_ID_LD_MOBILECASSY	0x1020	/* USB Product ID of Mobile-CASSY */
39 #define USB_DEVICE_ID_LD_MOBILECASSY2	0x1021	/* USB Product ID of Mobile-CASSY 2 (reserved) */
40 #define USB_DEVICE_ID_LD_MICROCASSYVOLTAGE	0x1031	/* USB Product ID of Micro-CASSY Voltage */
41 #define USB_DEVICE_ID_LD_MICROCASSYCURRENT	0x1032	/* USB Product ID of Micro-CASSY Current */
42 #define USB_DEVICE_ID_LD_MICROCASSYTIME		0x1033	/* USB Product ID of Micro-CASSY Time (reserved) */
43 #define USB_DEVICE_ID_LD_MICROCASSYTEMPERATURE	0x1035	/* USB Product ID of Micro-CASSY Temperature */
44 #define USB_DEVICE_ID_LD_MICROCASSYPH		0x1038	/* USB Product ID of Micro-CASSY pH */
45 #define USB_DEVICE_ID_LD_POWERANALYSERCASSY	0x1040	/* USB Product ID of Power Analyser CASSY */
46 #define USB_DEVICE_ID_LD_CONVERTERCONTROLLERCASSY	0x1042	/* USB Product ID of Converter Controller CASSY */
47 #define USB_DEVICE_ID_LD_MACHINETESTCASSY	0x1043	/* USB Product ID of Machine Test CASSY */
48 #define USB_DEVICE_ID_LD_JWM		0x1080	/* USB Product ID of Joule and Wattmeter */
49 #define USB_DEVICE_ID_LD_DMMP		0x1081	/* USB Product ID of Digital Multimeter P (reserved) */
50 #define USB_DEVICE_ID_LD_UMIP		0x1090	/* USB Product ID of UMI P */
51 #define USB_DEVICE_ID_LD_UMIC		0x10A0	/* USB Product ID of UMI C */
52 #define USB_DEVICE_ID_LD_UMIB		0x10B0	/* USB Product ID of UMI B */
53 #define USB_DEVICE_ID_LD_XRAY		0x1100	/* USB Product ID of X-Ray Apparatus 55481 */
54 #define USB_DEVICE_ID_LD_XRAY2		0x1101	/* USB Product ID of X-Ray Apparatus 554800 */
55 #define USB_DEVICE_ID_LD_XRAYCT		0x1110	/* USB Product ID of X-Ray Apparatus CT 554821*/
56 #define USB_DEVICE_ID_LD_VIDEOCOM	0x1200	/* USB Product ID of VideoCom */
57 #define USB_DEVICE_ID_LD_MOTOR		0x1210	/* USB Product ID of Motor (reserved) */
58 #define USB_DEVICE_ID_LD_COM3LAB	0x2000	/* USB Product ID of COM3LAB */
59 #define USB_DEVICE_ID_LD_TELEPORT	0x2010	/* USB Product ID of Terminal Adapter */
60 #define USB_DEVICE_ID_LD_NETWORKANALYSER 0x2020	/* USB Product ID of Network Analyser */
61 #define USB_DEVICE_ID_LD_POWERCONTROL	0x2030	/* USB Product ID of Converter Control Unit */
62 #define USB_DEVICE_ID_LD_MACHINETEST	0x2040	/* USB Product ID of Machine Test System */
63 #define USB_DEVICE_ID_LD_MOSTANALYSER	0x2050	/* USB Product ID of MOST Protocol Analyser */
64 #define USB_DEVICE_ID_LD_MOSTANALYSER2	0x2051	/* USB Product ID of MOST Protocol Analyser 2 */
65 #define USB_DEVICE_ID_LD_ABSESP		0x2060	/* USB Product ID of ABS ESP */
66 #define USB_DEVICE_ID_LD_AUTODATABUS	0x2070	/* USB Product ID of Automotive Data Buses */
67 #define USB_DEVICE_ID_LD_MCT		0x2080	/* USB Product ID of Microcontroller technique */
68 #define USB_DEVICE_ID_LD_HYBRID		0x2090	/* USB Product ID of Automotive Hybrid */
69 #define USB_DEVICE_ID_LD_HEATCONTROL	0x20A0	/* USB Product ID of Heat control */
70 
71 #ifdef CONFIG_USB_DYNAMIC_MINORS
72 #define USB_LD_MINOR_BASE	0
73 #else
74 #define USB_LD_MINOR_BASE	176
75 #endif
76 
77 /* table of devices that work with this driver */
78 static const struct usb_device_id ld_usb_table[] = {
79 	{ USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_CASSY) },
80 	{ USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_CASSY2) },
81 	{ USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_POCKETCASSY) },
82 	{ USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_POCKETCASSY2) },
83 	{ USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MOBILECASSY) },
84 	{ USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MOBILECASSY2) },
85 	{ USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MICROCASSYVOLTAGE) },
86 	{ USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MICROCASSYCURRENT) },
87 	{ USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MICROCASSYTIME) },
88 	{ USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MICROCASSYTEMPERATURE) },
89 	{ USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MICROCASSYPH) },
90 	{ USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_POWERANALYSERCASSY) },
91 	{ USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_CONVERTERCONTROLLERCASSY) },
92 	{ USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MACHINETESTCASSY) },
93 	{ USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_JWM) },
94 	{ USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_DMMP) },
95 	{ USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_UMIP) },
96 	{ USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_UMIC) },
97 	{ USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_UMIB) },
98 	{ USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_XRAY) },
99 	{ USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_XRAY2) },
100 	{ USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_VIDEOCOM) },
101 	{ USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MOTOR) },
102 	{ USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_COM3LAB) },
103 	{ USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_TELEPORT) },
104 	{ USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_NETWORKANALYSER) },
105 	{ USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_POWERCONTROL) },
106 	{ USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MACHINETEST) },
107 	{ USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MOSTANALYSER) },
108 	{ USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MOSTANALYSER2) },
109 	{ USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_ABSESP) },
110 	{ USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_AUTODATABUS) },
111 	{ USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MCT) },
112 	{ USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_HYBRID) },
113 	{ USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_HEATCONTROL) },
114 	{ }					/* Terminating entry */
115 };
116 MODULE_DEVICE_TABLE(usb, ld_usb_table);
117 MODULE_AUTHOR("Michael Hund <mhund@ld-didactic.de>");
118 MODULE_DESCRIPTION("LD USB Driver");
119 MODULE_LICENSE("GPL");
120 
121 /* All interrupt in transfers are collected in a ring buffer to
122  * avoid racing conditions and get better performance of the driver.
123  */
124 static int ring_buffer_size = 128;
125 module_param(ring_buffer_size, int, 0000);
126 MODULE_PARM_DESC(ring_buffer_size, "Read ring buffer size in reports");
127 
128 /* The write_buffer can contain more than one interrupt out transfer.
129  */
130 static int write_buffer_size = 10;
131 module_param(write_buffer_size, int, 0000);
132 MODULE_PARM_DESC(write_buffer_size, "Write buffer size in reports");
133 
134 /* As of kernel version 2.6.4 ehci-hcd uses an
135  * "only one interrupt transfer per frame" shortcut
136  * to simplify the scheduling of periodic transfers.
137  * This conflicts with our standard 1ms intervals for in and out URBs.
138  * We use default intervals of 2ms for in and 2ms for out transfers,
139  * which should be fast enough.
140  * Increase the interval to allow more devices that do interrupt transfers,
141  * or set to 1 to use the standard interval from the endpoint descriptors.
142  */
143 static int min_interrupt_in_interval = 2;
144 module_param(min_interrupt_in_interval, int, 0000);
145 MODULE_PARM_DESC(min_interrupt_in_interval, "Minimum interrupt in interval in ms");
146 
147 static int min_interrupt_out_interval = 2;
148 module_param(min_interrupt_out_interval, int, 0000);
149 MODULE_PARM_DESC(min_interrupt_out_interval, "Minimum interrupt out interval in ms");
150 
151 /* Structure to hold all of our device specific stuff */
152 struct ld_usb {
153 	struct kref		kref;
154 	struct mutex		mutex;		/* locks this structure */
155 	struct usb_interface	*intf;		/* save off the usb interface pointer */
156 	unsigned long		disconnected:1;
157 
158 	int			open_count;	/* number of times this port has been opened */
159 
160 	char			*ring_buffer;
161 	unsigned int		ring_head;
162 	unsigned int		ring_tail;
163 
164 	wait_queue_head_t	read_wait;
165 	wait_queue_head_t	write_wait;
166 
167 	char			*interrupt_in_buffer;
168 	struct usb_endpoint_descriptor *interrupt_in_endpoint;
169 	struct urb		*interrupt_in_urb;
170 	int			interrupt_in_interval;
171 	size_t			interrupt_in_endpoint_size;
172 	int			interrupt_in_running;
173 	int			interrupt_in_done;
174 	int			buffer_overflow;
175 	spinlock_t		rbsl;
176 
177 	char			*interrupt_out_buffer;
178 	struct usb_endpoint_descriptor *interrupt_out_endpoint;
179 	struct urb		*interrupt_out_urb;
180 	int			interrupt_out_interval;
181 	size_t			interrupt_out_endpoint_size;
182 	int			interrupt_out_busy;
183 };
184 
185 static struct usb_driver ld_usb_driver;
186 
187 /*
188  *	ld_usb_abort_transfers
189  *      aborts transfers and frees associated data structures
190  */
191 static void ld_usb_abort_transfers(struct ld_usb *dev)
192 {
193 	/* shutdown transfer */
194 	if (dev->interrupt_in_running) {
195 		dev->interrupt_in_running = 0;
196 		usb_kill_urb(dev->interrupt_in_urb);
197 	}
198 	if (dev->interrupt_out_busy)
199 		usb_kill_urb(dev->interrupt_out_urb);
200 }
201 
202 /*
203  *	ld_usb_delete
204  */
205 static void ld_usb_delete(struct kref *kref)
206 {
207 	struct ld_usb *dev = container_of(kref, struct ld_usb, kref);
208 
209 	/* free data structures */
210 	usb_free_urb(dev->interrupt_in_urb);
211 	usb_free_urb(dev->interrupt_out_urb);
212 	kfree(dev->ring_buffer);
213 	kfree(dev->interrupt_in_buffer);
214 	kfree(dev->interrupt_out_buffer);
215 	kfree(dev);
216 }
217 
218 /*
219  *	ld_usb_interrupt_in_callback
220  */
221 static void ld_usb_interrupt_in_callback(struct urb *urb)
222 {
223 	struct ld_usb *dev = urb->context;
224 	size_t *actual_buffer;
225 	unsigned int next_ring_head;
226 	int status = urb->status;
227 	unsigned long flags;
228 	int retval;
229 
230 	if (status) {
231 		if (status == -ENOENT ||
232 		    status == -ECONNRESET ||
233 		    status == -ESHUTDOWN) {
234 			goto exit;
235 		} else {
236 			dev_dbg(&dev->intf->dev,
237 				"%s: nonzero status received: %d\n", __func__,
238 				status);
239 			spin_lock_irqsave(&dev->rbsl, flags);
240 			goto resubmit; /* maybe we can recover */
241 		}
242 	}
243 
244 	spin_lock_irqsave(&dev->rbsl, flags);
245 	if (urb->actual_length > 0) {
246 		next_ring_head = (dev->ring_head+1) % ring_buffer_size;
247 		if (next_ring_head != dev->ring_tail) {
248 			actual_buffer = (size_t *)(dev->ring_buffer + dev->ring_head * (sizeof(size_t)+dev->interrupt_in_endpoint_size));
249 			/* actual_buffer gets urb->actual_length + interrupt_in_buffer */
250 			*actual_buffer = urb->actual_length;
251 			memcpy(actual_buffer+1, dev->interrupt_in_buffer, urb->actual_length);
252 			dev->ring_head = next_ring_head;
253 			dev_dbg(&dev->intf->dev, "%s: received %d bytes\n",
254 				__func__, urb->actual_length);
255 		} else {
256 			dev_warn(&dev->intf->dev,
257 				 "Ring buffer overflow, %d bytes dropped\n",
258 				 urb->actual_length);
259 			dev->buffer_overflow = 1;
260 		}
261 	}
262 
263 resubmit:
264 	/* resubmit if we're still running */
265 	if (dev->interrupt_in_running && !dev->buffer_overflow) {
266 		retval = usb_submit_urb(dev->interrupt_in_urb, GFP_ATOMIC);
267 		if (retval) {
268 			dev_err(&dev->intf->dev,
269 				"usb_submit_urb failed (%d)\n", retval);
270 			dev->buffer_overflow = 1;
271 		}
272 	}
273 	spin_unlock_irqrestore(&dev->rbsl, flags);
274 exit:
275 	dev->interrupt_in_done = 1;
276 	wake_up_interruptible(&dev->read_wait);
277 }
278 
279 /*
280  *	ld_usb_interrupt_out_callback
281  */
282 static void ld_usb_interrupt_out_callback(struct urb *urb)
283 {
284 	struct ld_usb *dev = urb->context;
285 	int status = urb->status;
286 
287 	/* sync/async unlink faults aren't errors */
288 	if (status && !(status == -ENOENT ||
289 			status == -ECONNRESET ||
290 			status == -ESHUTDOWN))
291 		dev_dbg(&dev->intf->dev,
292 			"%s - nonzero write interrupt status received: %d\n",
293 			__func__, status);
294 
295 	dev->interrupt_out_busy = 0;
296 	wake_up_interruptible(&dev->write_wait);
297 }
298 
299 /*
300  *	ld_usb_open
301  */
302 static int ld_usb_open(struct inode *inode, struct file *file)
303 {
304 	struct ld_usb *dev;
305 	int subminor;
306 	int retval;
307 	struct usb_interface *interface;
308 
309 	stream_open(inode, file);
310 	subminor = iminor(inode);
311 
312 	interface = usb_find_interface(&ld_usb_driver, subminor);
313 
314 	if (!interface) {
315 		printk(KERN_ERR "%s - error, can't find device for minor %d\n",
316 		       __func__, subminor);
317 		return -ENODEV;
318 	}
319 
320 	dev = usb_get_intfdata(interface);
321 
322 	if (!dev)
323 		return -ENODEV;
324 
325 	/* lock this device */
326 	if (mutex_lock_interruptible(&dev->mutex))
327 		return -ERESTARTSYS;
328 
329 	/* allow opening only once */
330 	if (dev->open_count) {
331 		retval = -EBUSY;
332 		goto unlock_exit;
333 	}
334 	dev->open_count = 1;
335 
336 	/* initialize in direction */
337 	dev->ring_head = 0;
338 	dev->ring_tail = 0;
339 	dev->buffer_overflow = 0;
340 	usb_fill_int_urb(dev->interrupt_in_urb,
341 			 interface_to_usbdev(interface),
342 			 usb_rcvintpipe(interface_to_usbdev(interface),
343 					dev->interrupt_in_endpoint->bEndpointAddress),
344 			 dev->interrupt_in_buffer,
345 			 dev->interrupt_in_endpoint_size,
346 			 ld_usb_interrupt_in_callback,
347 			 dev,
348 			 dev->interrupt_in_interval);
349 
350 	dev->interrupt_in_running = 1;
351 	dev->interrupt_in_done = 0;
352 
353 	retval = usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL);
354 	if (retval) {
355 		dev_err(&interface->dev, "Couldn't submit interrupt_in_urb %d\n", retval);
356 		dev->interrupt_in_running = 0;
357 		dev->open_count = 0;
358 		goto unlock_exit;
359 	}
360 
361 	kref_get(&dev->kref);
362 
363 	/* save device in the file's private structure */
364 	file->private_data = dev;
365 
366 unlock_exit:
367 	mutex_unlock(&dev->mutex);
368 
369 	return retval;
370 }
371 
372 /*
373  *	ld_usb_release
374  */
375 static int ld_usb_release(struct inode *inode, struct file *file)
376 {
377 	struct ld_usb *dev;
378 	int retval = 0;
379 
380 	dev = file->private_data;
381 
382 	if (dev == NULL) {
383 		retval = -ENODEV;
384 		goto exit;
385 	}
386 
387 	mutex_lock(&dev->mutex);
388 
389 	if (dev->disconnected)
390 		goto unlock_exit;
391 
392 	/* wait until write transfer is finished */
393 	if (dev->interrupt_out_busy)
394 		wait_event_interruptible_timeout(dev->write_wait, !dev->interrupt_out_busy, 2 * HZ);
395 	ld_usb_abort_transfers(dev);
396 	dev->open_count = 0;
397 
398 unlock_exit:
399 	mutex_unlock(&dev->mutex);
400 	kref_put(&dev->kref, ld_usb_delete);
401 exit:
402 	return retval;
403 }
404 
405 /*
406  *	ld_usb_poll
407  */
408 static __poll_t ld_usb_poll(struct file *file, poll_table *wait)
409 {
410 	struct ld_usb *dev;
411 	__poll_t mask = 0;
412 
413 	dev = file->private_data;
414 
415 	if (dev->disconnected)
416 		return EPOLLERR | EPOLLHUP;
417 
418 	poll_wait(file, &dev->read_wait, wait);
419 	poll_wait(file, &dev->write_wait, wait);
420 
421 	if (dev->ring_head != dev->ring_tail)
422 		mask |= EPOLLIN | EPOLLRDNORM;
423 	if (!dev->interrupt_out_busy)
424 		mask |= EPOLLOUT | EPOLLWRNORM;
425 
426 	return mask;
427 }
428 
429 /*
430  *	ld_usb_read
431  */
432 static ssize_t ld_usb_read(struct file *file, char __user *buffer, size_t count,
433 			   loff_t *ppos)
434 {
435 	struct ld_usb *dev;
436 	size_t *actual_buffer;
437 	size_t bytes_to_read;
438 	int retval = 0;
439 	int rv;
440 
441 	dev = file->private_data;
442 
443 	/* verify that we actually have some data to read */
444 	if (count == 0)
445 		goto exit;
446 
447 	/* lock this object */
448 	if (mutex_lock_interruptible(&dev->mutex)) {
449 		retval = -ERESTARTSYS;
450 		goto exit;
451 	}
452 
453 	/* verify that the device wasn't unplugged */
454 	if (dev->disconnected) {
455 		retval = -ENODEV;
456 		printk(KERN_ERR "ldusb: No device or device unplugged %d\n", retval);
457 		goto unlock_exit;
458 	}
459 
460 	/* wait for data */
461 	spin_lock_irq(&dev->rbsl);
462 	while (dev->ring_head == dev->ring_tail) {
463 		dev->interrupt_in_done = 0;
464 		spin_unlock_irq(&dev->rbsl);
465 		if (file->f_flags & O_NONBLOCK) {
466 			retval = -EAGAIN;
467 			goto unlock_exit;
468 		}
469 		retval = wait_event_interruptible(dev->read_wait, dev->interrupt_in_done);
470 		if (retval < 0)
471 			goto unlock_exit;
472 
473 		spin_lock_irq(&dev->rbsl);
474 	}
475 	spin_unlock_irq(&dev->rbsl);
476 
477 	/* actual_buffer contains actual_length + interrupt_in_buffer */
478 	actual_buffer = (size_t *)(dev->ring_buffer + dev->ring_tail * (sizeof(size_t)+dev->interrupt_in_endpoint_size));
479 	if (*actual_buffer > dev->interrupt_in_endpoint_size) {
480 		retval = -EIO;
481 		goto unlock_exit;
482 	}
483 	bytes_to_read = min(count, *actual_buffer);
484 	if (bytes_to_read < *actual_buffer)
485 		dev_warn(&dev->intf->dev, "Read buffer overflow, %zu bytes dropped\n",
486 			 *actual_buffer-bytes_to_read);
487 
488 	/* copy one interrupt_in_buffer from ring_buffer into userspace */
489 	if (copy_to_user(buffer, actual_buffer+1, bytes_to_read)) {
490 		retval = -EFAULT;
491 		goto unlock_exit;
492 	}
493 	retval = bytes_to_read;
494 
495 	spin_lock_irq(&dev->rbsl);
496 	dev->ring_tail = (dev->ring_tail + 1) % ring_buffer_size;
497 
498 	if (dev->buffer_overflow) {
499 		dev->buffer_overflow = 0;
500 		spin_unlock_irq(&dev->rbsl);
501 		rv = usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL);
502 		if (rv < 0)
503 			dev->buffer_overflow = 1;
504 	} else {
505 		spin_unlock_irq(&dev->rbsl);
506 	}
507 
508 unlock_exit:
509 	/* unlock the device */
510 	mutex_unlock(&dev->mutex);
511 
512 exit:
513 	return retval;
514 }
515 
516 /*
517  *	ld_usb_write
518  */
519 static ssize_t ld_usb_write(struct file *file, const char __user *buffer,
520 			    size_t count, loff_t *ppos)
521 {
522 	struct ld_usb *dev;
523 	size_t bytes_to_write;
524 	int retval = 0;
525 
526 	dev = file->private_data;
527 
528 	/* verify that we actually have some data to write */
529 	if (count == 0)
530 		goto exit;
531 
532 	/* lock this object */
533 	if (mutex_lock_interruptible(&dev->mutex)) {
534 		retval = -ERESTARTSYS;
535 		goto exit;
536 	}
537 
538 	/* verify that the device wasn't unplugged */
539 	if (dev->disconnected) {
540 		retval = -ENODEV;
541 		printk(KERN_ERR "ldusb: No device or device unplugged %d\n", retval);
542 		goto unlock_exit;
543 	}
544 
545 	/* wait until previous transfer is finished */
546 	if (dev->interrupt_out_busy) {
547 		if (file->f_flags & O_NONBLOCK) {
548 			retval = -EAGAIN;
549 			goto unlock_exit;
550 		}
551 		retval = wait_event_interruptible(dev->write_wait, !dev->interrupt_out_busy);
552 		if (retval < 0) {
553 			goto unlock_exit;
554 		}
555 	}
556 
557 	/* write the data into interrupt_out_buffer from userspace */
558 	bytes_to_write = min(count, write_buffer_size*dev->interrupt_out_endpoint_size);
559 	if (bytes_to_write < count)
560 		dev_warn(&dev->intf->dev, "Write buffer overflow, %zu bytes dropped\n",
561 			count - bytes_to_write);
562 	dev_dbg(&dev->intf->dev, "%s: count = %zu, bytes_to_write = %zu\n",
563 		__func__, count, bytes_to_write);
564 
565 	if (copy_from_user(dev->interrupt_out_buffer, buffer, bytes_to_write)) {
566 		retval = -EFAULT;
567 		goto unlock_exit;
568 	}
569 
570 	if (dev->interrupt_out_endpoint == NULL) {
571 		/* try HID_REQ_SET_REPORT=9 on control_endpoint instead of interrupt_out_endpoint */
572 		retval = usb_control_msg(interface_to_usbdev(dev->intf),
573 					 usb_sndctrlpipe(interface_to_usbdev(dev->intf), 0),
574 					 9,
575 					 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
576 					 1 << 8, 0,
577 					 dev->interrupt_out_buffer,
578 					 bytes_to_write,
579 					 USB_CTRL_SET_TIMEOUT);
580 		if (retval < 0)
581 			dev_err(&dev->intf->dev,
582 				"Couldn't submit HID_REQ_SET_REPORT %d\n",
583 				retval);
584 		goto unlock_exit;
585 	}
586 
587 	/* send off the urb */
588 	usb_fill_int_urb(dev->interrupt_out_urb,
589 			 interface_to_usbdev(dev->intf),
590 			 usb_sndintpipe(interface_to_usbdev(dev->intf),
591 					dev->interrupt_out_endpoint->bEndpointAddress),
592 			 dev->interrupt_out_buffer,
593 			 bytes_to_write,
594 			 ld_usb_interrupt_out_callback,
595 			 dev,
596 			 dev->interrupt_out_interval);
597 
598 	dev->interrupt_out_busy = 1;
599 	wmb();
600 
601 	retval = usb_submit_urb(dev->interrupt_out_urb, GFP_KERNEL);
602 	if (retval) {
603 		dev->interrupt_out_busy = 0;
604 		dev_err(&dev->intf->dev,
605 			"Couldn't submit interrupt_out_urb %d\n", retval);
606 		goto unlock_exit;
607 	}
608 	retval = bytes_to_write;
609 
610 unlock_exit:
611 	/* unlock the device */
612 	mutex_unlock(&dev->mutex);
613 
614 exit:
615 	return retval;
616 }
617 
618 /* file operations needed when we register this driver */
619 static const struct file_operations ld_usb_fops = {
620 	.owner =	THIS_MODULE,
621 	.read  =	ld_usb_read,
622 	.write =	ld_usb_write,
623 	.open =		ld_usb_open,
624 	.release =	ld_usb_release,
625 	.poll =		ld_usb_poll,
626 };
627 
628 /*
629  * usb class driver info in order to get a minor number from the usb core,
630  * and to have the device registered with the driver core
631  */
632 static struct usb_class_driver ld_usb_class = {
633 	.name =		"ldusb%d",
634 	.fops =		&ld_usb_fops,
635 	.minor_base =	USB_LD_MINOR_BASE,
636 };
637 
638 /*
639  *	ld_usb_probe
640  *
641  *	Called by the usb core when a new device is connected that it thinks
642  *	this driver might be interested in.
643  */
644 static int ld_usb_probe(struct usb_interface *intf, const struct usb_device_id *id)
645 {
646 	struct usb_device *udev = interface_to_usbdev(intf);
647 	struct ld_usb *dev = NULL;
648 	struct usb_host_interface *iface_desc;
649 	char *buffer;
650 	int retval = -ENOMEM;
651 	int res;
652 
653 	/* allocate memory for our device state and initialize it */
654 
655 	dev = kzalloc_obj(*dev);
656 	if (!dev)
657 		goto exit;
658 
659 	kref_init(&dev->kref);
660 	mutex_init(&dev->mutex);
661 	spin_lock_init(&dev->rbsl);
662 	dev->intf = intf;
663 	init_waitqueue_head(&dev->read_wait);
664 	init_waitqueue_head(&dev->write_wait);
665 
666 	/* workaround for early firmware versions on fast computers */
667 	if ((le16_to_cpu(udev->descriptor.idVendor) == USB_VENDOR_ID_LD) &&
668 	    ((le16_to_cpu(udev->descriptor.idProduct) == USB_DEVICE_ID_LD_CASSY) ||
669 	     (le16_to_cpu(udev->descriptor.idProduct) == USB_DEVICE_ID_LD_COM3LAB)) &&
670 	    (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x103)) {
671 		buffer = kmalloc(256, GFP_KERNEL);
672 		if (!buffer)
673 			goto error;
674 		/* usb_string makes SETUP+STALL to leave always ControlReadLoop */
675 		usb_string(udev, 255, buffer, 256);
676 		kfree(buffer);
677 	}
678 
679 	iface_desc = intf->cur_altsetting;
680 
681 	res = usb_find_last_int_in_endpoint(iface_desc,
682 			&dev->interrupt_in_endpoint);
683 	if (res) {
684 		dev_err(&intf->dev, "Interrupt in endpoint not found\n");
685 		retval = res;
686 		goto error;
687 	}
688 
689 	res = usb_find_last_int_out_endpoint(iface_desc,
690 			&dev->interrupt_out_endpoint);
691 	if (res)
692 		dev_warn(&intf->dev, "Interrupt out endpoint not found (using control endpoint instead)\n");
693 
694 	dev->interrupt_in_endpoint_size = usb_endpoint_maxp(dev->interrupt_in_endpoint);
695 	dev->ring_buffer = kcalloc(ring_buffer_size,
696 			sizeof(size_t) + dev->interrupt_in_endpoint_size,
697 			GFP_KERNEL);
698 	if (!dev->ring_buffer)
699 		goto error;
700 	dev->interrupt_in_buffer = kmalloc(dev->interrupt_in_endpoint_size, GFP_KERNEL);
701 	if (!dev->interrupt_in_buffer)
702 		goto error;
703 	dev->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
704 	if (!dev->interrupt_in_urb)
705 		goto error;
706 	dev->interrupt_out_endpoint_size = dev->interrupt_out_endpoint ? usb_endpoint_maxp(dev->interrupt_out_endpoint) :
707 									 udev->descriptor.bMaxPacketSize0;
708 	dev->interrupt_out_buffer =
709 		kmalloc_array(write_buffer_size,
710 			      dev->interrupt_out_endpoint_size, GFP_KERNEL);
711 	if (!dev->interrupt_out_buffer)
712 		goto error;
713 	dev->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL);
714 	if (!dev->interrupt_out_urb)
715 		goto error;
716 	dev->interrupt_in_interval = max_t(int, min_interrupt_in_interval,
717 					   dev->interrupt_in_endpoint->bInterval);
718 	if (dev->interrupt_out_endpoint)
719 		dev->interrupt_out_interval = max_t(int, min_interrupt_out_interval,
720 						    dev->interrupt_out_endpoint->bInterval);
721 
722 	/* we can register the device now, as it is ready */
723 	usb_set_intfdata(intf, dev);
724 
725 	retval = usb_register_dev(intf, &ld_usb_class);
726 	if (retval) {
727 		/* something prevented us from registering this driver */
728 		dev_err(&intf->dev, "Not able to get a minor for this device.\n");
729 		usb_set_intfdata(intf, NULL);
730 		goto error;
731 	}
732 
733 	/* let the user know what node this device is now attached to */
734 	dev_info(&intf->dev, "LD USB Device #%d now attached to major %d minor %d\n",
735 		(intf->minor - USB_LD_MINOR_BASE), USB_MAJOR, intf->minor);
736 
737 exit:
738 	return retval;
739 
740 error:
741 	kref_put(&dev->kref, ld_usb_delete);
742 
743 	return retval;
744 }
745 
746 /*
747  *	ld_usb_disconnect
748  *
749  *	Called by the usb core when the device is removed from the system.
750  */
751 static void ld_usb_disconnect(struct usb_interface *intf)
752 {
753 	struct ld_usb *dev;
754 	int minor;
755 
756 	dev = usb_get_intfdata(intf);
757 	usb_set_intfdata(intf, NULL);
758 
759 	minor = intf->minor;
760 
761 	/* give back our minor */
762 	usb_deregister_dev(intf, &ld_usb_class);
763 
764 	usb_poison_urb(dev->interrupt_in_urb);
765 	usb_poison_urb(dev->interrupt_out_urb);
766 
767 	mutex_lock(&dev->mutex);
768 
769 	dev->disconnected = 1;
770 
771 	if (dev->open_count) {
772 		/* wake up pollers */
773 		wake_up_interruptible_all(&dev->read_wait);
774 		wake_up_interruptible_all(&dev->write_wait);
775 	}
776 
777 	mutex_unlock(&dev->mutex);
778 
779 	kref_put(&dev->kref, ld_usb_delete);
780 
781 	dev_info(&intf->dev, "LD USB Device #%d now disconnected\n",
782 		 (minor - USB_LD_MINOR_BASE));
783 }
784 
785 /* usb specific object needed to register this driver with the usb subsystem */
786 static struct usb_driver ld_usb_driver = {
787 	.name =		"ldusb",
788 	.probe =	ld_usb_probe,
789 	.disconnect =	ld_usb_disconnect,
790 	.id_table =	ld_usb_table,
791 };
792 
793 module_usb_driver(ld_usb_driver);
794 
795