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