]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/usb/misc/ldusb.c
189a9db035090fb35cf4c320d6dffd5a0d63c8db
[linux-2.6-omap-h63xx.git] / drivers / usb / misc / ldusb.c
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 #define USB_DEVICE_ID_VERNIER_LCSPEC    0x0006
65
66 #ifdef CONFIG_USB_DYNAMIC_MINORS
67 #define USB_LD_MINOR_BASE       0
68 #else
69 #define USB_LD_MINOR_BASE       176
70 #endif
71
72 /* table of devices that work with this driver */
73 static struct usb_device_id ld_usb_table [] = {
74         { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_CASSY) },
75         { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_POCKETCASSY) },
76         { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MOBILECASSY) },
77         { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_JWM) },
78         { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_DMMP) },
79         { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_UMIP) },
80         { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_XRAY1) },
81         { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_XRAY2) },
82         { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_VIDEOCOM) },
83         { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_COM3LAB) },
84         { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_TELEPORT) },
85         { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_NETWORKANALYSER) },
86         { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_POWERCONTROL) },
87         { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MACHINETEST) },
88         { USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_LABPRO) },
89         { USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_GOTEMP) },
90         { USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_SKIP) },
91         { USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_CYCLOPS) },
92         { USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_LCSPEC) },
93         { }                                     /* Terminating entry */
94 };
95 MODULE_DEVICE_TABLE(usb, ld_usb_table);
96 MODULE_VERSION("V0.13");
97 MODULE_AUTHOR("Michael Hund <mhund@ld-didactic.de>");
98 MODULE_DESCRIPTION("LD USB Driver");
99 MODULE_LICENSE("GPL");
100 MODULE_SUPPORTED_DEVICE("LD USB Devices");
101
102 #ifdef CONFIG_USB_DEBUG
103         static int debug = 1;
104 #else
105         static int debug = 0;
106 #endif
107
108 /* Use our own dbg macro */
109 #define dbg_info(dev, format, arg...) do { if (debug) dev_info(dev , format , ## arg); } while (0)
110
111 /* Module parameters */
112 module_param(debug, int, S_IRUGO | S_IWUSR);
113 MODULE_PARM_DESC(debug, "Debug enabled or not");
114
115 /* All interrupt in transfers are collected in a ring buffer to
116  * avoid racing conditions and get better performance of the driver.
117  */
118 static int ring_buffer_size = 128;
119 module_param(ring_buffer_size, int, 0);
120 MODULE_PARM_DESC(ring_buffer_size, "Read ring buffer size in reports");
121
122 /* The write_buffer can contain more than one interrupt out transfer.
123  */
124 static int write_buffer_size = 10;
125 module_param(write_buffer_size, int, 0);
126 MODULE_PARM_DESC(write_buffer_size, "Write buffer size in reports");
127
128 /* As of kernel version 2.6.4 ehci-hcd uses an
129  * "only one interrupt transfer per frame" shortcut
130  * to simplify the scheduling of periodic transfers.
131  * This conflicts with our standard 1ms intervals for in and out URBs.
132  * We use default intervals of 2ms for in and 2ms for out transfers,
133  * which should be fast enough.
134  * Increase the interval to allow more devices that do interrupt transfers,
135  * or set to 1 to use the standard interval from the endpoint descriptors.
136  */
137 static int min_interrupt_in_interval = 2;
138 module_param(min_interrupt_in_interval, int, 0);
139 MODULE_PARM_DESC(min_interrupt_in_interval, "Minimum interrupt in interval in ms");
140
141 static int min_interrupt_out_interval = 2;
142 module_param(min_interrupt_out_interval, int, 0);
143 MODULE_PARM_DESC(min_interrupt_out_interval, "Minimum interrupt out interval in ms");
144
145 /* Structure to hold all of our device specific stuff */
146 struct ld_usb {
147         struct mutex            mutex;          /* locks this structure */
148         struct usb_interface*   intf;           /* save off the usb interface pointer */
149
150         int                     open_count;     /* number of times this port has been opened */
151
152         char*                   ring_buffer;
153         unsigned int            ring_head;
154         unsigned int            ring_tail;
155
156         wait_queue_head_t       read_wait;
157         wait_queue_head_t       write_wait;
158
159         char*                   interrupt_in_buffer;
160         struct usb_endpoint_descriptor* interrupt_in_endpoint;
161         struct urb*             interrupt_in_urb;
162         int                     interrupt_in_interval;
163         size_t                  interrupt_in_endpoint_size;
164         int                     interrupt_in_running;
165         int                     interrupt_in_done;
166         int                     buffer_overflow;
167         spinlock_t              rbsl;
168
169         char*                   interrupt_out_buffer;
170         struct usb_endpoint_descriptor* interrupt_out_endpoint;
171         struct urb*             interrupt_out_urb;
172         int                     interrupt_out_interval;
173         size_t                  interrupt_out_endpoint_size;
174         int                     interrupt_out_busy;
175 };
176
177 static struct usb_driver ld_usb_driver;
178
179 /**
180  *      ld_usb_abort_transfers
181  *      aborts transfers and frees associated data structures
182  */
183 static void ld_usb_abort_transfers(struct ld_usb *dev)
184 {
185         /* shutdown transfer */
186         if (dev->interrupt_in_running) {
187                 dev->interrupt_in_running = 0;
188                 if (dev->intf)
189                         usb_kill_urb(dev->interrupt_in_urb);
190         }
191         if (dev->interrupt_out_busy)
192                 if (dev->intf)
193                         usb_kill_urb(dev->interrupt_out_urb);
194 }
195
196 /**
197  *      ld_usb_delete
198  */
199 static void ld_usb_delete(struct ld_usb *dev)
200 {
201         ld_usb_abort_transfers(dev);
202
203         /* free data structures */
204         usb_free_urb(dev->interrupt_in_urb);
205         usb_free_urb(dev->interrupt_out_urb);
206         kfree(dev->ring_buffer);
207         kfree(dev->interrupt_in_buffer);
208         kfree(dev->interrupt_out_buffer);
209         kfree(dev);
210 }
211
212 /**
213  *      ld_usb_interrupt_in_callback
214  */
215 static void ld_usb_interrupt_in_callback(struct urb *urb)
216 {
217         struct ld_usb *dev = urb->context;
218         size_t *actual_buffer;
219         unsigned int next_ring_head;
220         int status = urb->status;
221         int retval;
222
223         if (status) {
224                 if (status == -ENOENT ||
225                     status == -ECONNRESET ||
226                     status == -ESHUTDOWN) {
227                         goto exit;
228                 } else {
229                         dbg_info(&dev->intf->dev, "%s: nonzero status received: %d\n",
230                                  __func__, status);
231                         spin_lock(&dev->rbsl);
232                         goto resubmit; /* maybe we can recover */
233                 }
234         }
235
236         spin_lock(&dev->rbsl);
237         if (urb->actual_length > 0) {
238                 next_ring_head = (dev->ring_head+1) % ring_buffer_size;
239                 if (next_ring_head != dev->ring_tail) {
240                         actual_buffer = (size_t*)(dev->ring_buffer + dev->ring_head*(sizeof(size_t)+dev->interrupt_in_endpoint_size));
241                         /* actual_buffer gets urb->actual_length + interrupt_in_buffer */
242                         *actual_buffer = urb->actual_length;
243                         memcpy(actual_buffer+1, dev->interrupt_in_buffer, urb->actual_length);
244                         dev->ring_head = next_ring_head;
245                         dbg_info(&dev->intf->dev, "%s: received %d bytes\n",
246                                  __func__, urb->actual_length);
247                 } else {
248                         dev_warn(&dev->intf->dev,
249                                  "Ring buffer overflow, %d bytes dropped\n",
250                                  urb->actual_length);
251                         dev->buffer_overflow = 1;
252                 }
253         }
254
255 resubmit:
256         /* resubmit if we're still running */
257         if (dev->interrupt_in_running && !dev->buffer_overflow && dev->intf) {
258                 retval = usb_submit_urb(dev->interrupt_in_urb, GFP_ATOMIC);
259                 if (retval) {
260                         dev_err(&dev->intf->dev,
261                                 "usb_submit_urb failed (%d)\n", retval);
262                         dev->buffer_overflow = 1;
263                 }
264         }
265         spin_unlock(&dev->rbsl);
266 exit:
267         dev->interrupt_in_done = 1;
268         wake_up_interruptible(&dev->read_wait);
269 }
270
271 /**
272  *      ld_usb_interrupt_out_callback
273  */
274 static void ld_usb_interrupt_out_callback(struct urb *urb)
275 {
276         struct ld_usb *dev = urb->context;
277         int status = urb->status;
278
279         /* sync/async unlink faults aren't errors */
280         if (status && !(status == -ENOENT ||
281                         status == -ECONNRESET ||
282                         status == -ESHUTDOWN))
283                 dbg_info(&dev->intf->dev,
284                          "%s - nonzero write interrupt status received: %d\n",
285                          __func__, status);
286
287         dev->interrupt_out_busy = 0;
288         wake_up_interruptible(&dev->write_wait);
289 }
290
291 /**
292  *      ld_usb_open
293  */
294 static int ld_usb_open(struct inode *inode, struct file *file)
295 {
296         struct ld_usb *dev;
297         int subminor;
298         int retval;
299         struct usb_interface *interface;
300
301         nonseekable_open(inode, file);
302         subminor = iminor(inode);
303
304         interface = usb_find_interface(&ld_usb_driver, subminor);
305
306         if (!interface) {
307                 err("%s - error, can't find device for minor %d\n",
308                      __func__, subminor);
309                 return -ENODEV;
310         }
311
312         dev = usb_get_intfdata(interface);
313
314         if (!dev)
315                 return -ENODEV;
316
317         /* lock this device */
318         if (mutex_lock_interruptible(&dev->mutex))
319                 return -ERESTARTSYS;
320
321         /* allow opening only once */
322         if (dev->open_count) {
323                 retval = -EBUSY;
324                 goto unlock_exit;
325         }
326         dev->open_count = 1;
327
328         /* initialize in direction */
329         dev->ring_head = 0;
330         dev->ring_tail = 0;
331         dev->buffer_overflow = 0;
332         usb_fill_int_urb(dev->interrupt_in_urb,
333                          interface_to_usbdev(interface),
334                          usb_rcvintpipe(interface_to_usbdev(interface),
335                                         dev->interrupt_in_endpoint->bEndpointAddress),
336                          dev->interrupt_in_buffer,
337                          dev->interrupt_in_endpoint_size,
338                          ld_usb_interrupt_in_callback,
339                          dev,
340                          dev->interrupt_in_interval);
341
342         dev->interrupt_in_running = 1;
343         dev->interrupt_in_done = 0;
344
345         retval = usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL);
346         if (retval) {
347                 dev_err(&interface->dev, "Couldn't submit interrupt_in_urb %d\n", retval);
348                 dev->interrupt_in_running = 0;
349                 dev->open_count = 0;
350                 goto unlock_exit;
351         }
352
353         /* save device in the file's private structure */
354         file->private_data = dev;
355
356 unlock_exit:
357         mutex_unlock(&dev->mutex);
358
359         return retval;
360 }
361
362 /**
363  *      ld_usb_release
364  */
365 static int ld_usb_release(struct inode *inode, struct file *file)
366 {
367         struct ld_usb *dev;
368         int retval = 0;
369
370         dev = file->private_data;
371
372         if (dev == NULL) {
373                 retval = -ENODEV;
374                 goto exit;
375         }
376
377         if (mutex_lock_interruptible(&dev->mutex)) {
378                 retval = -ERESTARTSYS;
379                 goto exit;
380         }
381
382         if (dev->open_count != 1) {
383                 retval = -ENODEV;
384                 goto unlock_exit;
385         }
386         if (dev->intf == NULL) {
387                 /* the device was unplugged before the file was released */
388                 mutex_unlock(&dev->mutex);
389                 /* unlock here as ld_usb_delete frees dev */
390                 ld_usb_delete(dev);
391                 goto exit;
392         }
393
394         /* wait until write transfer is finished */
395         if (dev->interrupt_out_busy)
396                 wait_event_interruptible_timeout(dev->write_wait, !dev->interrupt_out_busy, 2 * HZ);
397         ld_usb_abort_transfers(dev);
398         dev->open_count = 0;
399
400 unlock_exit:
401         mutex_unlock(&dev->mutex);
402
403 exit:
404         return retval;
405 }
406
407 /**
408  *      ld_usb_poll
409  */
410 static unsigned int ld_usb_poll(struct file *file, poll_table *wait)
411 {
412         struct ld_usb *dev;
413         unsigned int mask = 0;
414
415         dev = file->private_data;
416
417         poll_wait(file, &dev->read_wait, wait);
418         poll_wait(file, &dev->write_wait, wait);
419
420         if (dev->ring_head != dev->ring_tail)
421                 mask |= POLLIN | POLLRDNORM;
422         if (!dev->interrupt_out_busy)
423                 mask |= POLLOUT | POLLWRNORM;
424
425         return mask;
426 }
427
428 /**
429  *      ld_usb_read
430  */
431 static ssize_t ld_usb_read(struct file *file, char __user *buffer, size_t count,
432                            loff_t *ppos)
433 {
434         struct ld_usb *dev;
435         size_t *actual_buffer;
436         size_t bytes_to_read;
437         int retval = 0;
438         int rv;
439
440         dev = file->private_data;
441
442         /* verify that we actually have some data to read */
443         if (count == 0)
444                 goto exit;
445
446         /* lock this object */
447         if (mutex_lock_interruptible(&dev->mutex)) {
448                 retval = -ERESTARTSYS;
449                 goto exit;
450         }
451
452         /* verify that the device wasn't unplugged */
453         if (dev->intf == NULL) {
454                 retval = -ENODEV;
455                 err("No device or device unplugged %d\n", retval);
456                 goto unlock_exit;
457         }
458
459         /* wait for data */
460         spin_lock_irq(&dev->rbsl);
461         if (dev->ring_head == dev->ring_tail) {
462                 dev->interrupt_in_done = 0;
463                 spin_unlock_irq(&dev->rbsl);
464                 if (file->f_flags & O_NONBLOCK) {
465                         retval = -EAGAIN;
466                         goto unlock_exit;
467                 }
468                 retval = wait_event_interruptible(dev->read_wait, dev->interrupt_in_done);
469                 if (retval < 0)
470                         goto unlock_exit;
471         } else {
472                 spin_unlock_irq(&dev->rbsl);
473         }
474
475         /* actual_buffer contains actual_length + interrupt_in_buffer */
476         actual_buffer = (size_t*)(dev->ring_buffer + dev->ring_tail*(sizeof(size_t)+dev->interrupt_in_endpoint_size));
477         bytes_to_read = min(count, *actual_buffer);
478         if (bytes_to_read < *actual_buffer)
479                 dev_warn(&dev->intf->dev, "Read buffer overflow, %zd bytes dropped\n",
480                          *actual_buffer-bytes_to_read);
481
482         /* copy one interrupt_in_buffer from ring_buffer into userspace */
483         if (copy_to_user(buffer, actual_buffer+1, bytes_to_read)) {
484                 retval = -EFAULT;
485                 goto unlock_exit;
486         }
487         dev->ring_tail = (dev->ring_tail+1) % ring_buffer_size;
488
489         retval = bytes_to_read;
490
491         spin_lock_irq(&dev->rbsl);
492         if (dev->buffer_overflow) {
493                 dev->buffer_overflow = 0;
494                 spin_unlock_irq(&dev->rbsl);
495                 rv = usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL);
496                 if (rv < 0)
497                         dev->buffer_overflow = 1;
498         } else {
499                 spin_unlock_irq(&dev->rbsl);
500         }
501
502 unlock_exit:
503         /* unlock the device */
504         mutex_unlock(&dev->mutex);
505
506 exit:
507         return retval;
508 }
509
510 /**
511  *      ld_usb_write
512  */
513 static ssize_t ld_usb_write(struct file *file, const char __user *buffer,
514                             size_t count, loff_t *ppos)
515 {
516         struct ld_usb *dev;
517         size_t bytes_to_write;
518         int retval = 0;
519
520         dev = file->private_data;
521
522         /* verify that we actually have some data to write */
523         if (count == 0)
524                 goto exit;
525
526         /* lock this object */
527         if (mutex_lock_interruptible(&dev->mutex)) {
528                 retval = -ERESTARTSYS;
529                 goto exit;
530         }
531
532         /* verify that the device wasn't unplugged */
533         if (dev->intf == NULL) {
534                 retval = -ENODEV;
535                 err("No device or device unplugged %d\n", retval);
536                 goto unlock_exit;
537         }
538
539         /* wait until previous transfer is finished */
540         if (dev->interrupt_out_busy) {
541                 if (file->f_flags & O_NONBLOCK) {
542                         retval = -EAGAIN;
543                         goto unlock_exit;
544                 }
545                 retval = wait_event_interruptible(dev->write_wait, !dev->interrupt_out_busy);
546                 if (retval < 0) {
547                         goto unlock_exit;
548                 }
549         }
550
551         /* write the data into interrupt_out_buffer from userspace */
552         bytes_to_write = min(count, write_buffer_size*dev->interrupt_out_endpoint_size);
553         if (bytes_to_write < count)
554                 dev_warn(&dev->intf->dev, "Write buffer overflow, %zd bytes dropped\n",count-bytes_to_write);
555         dbg_info(&dev->intf->dev, "%s: count = %zd, bytes_to_write = %zd\n", __func__, count, bytes_to_write);
556
557         if (copy_from_user(dev->interrupt_out_buffer, buffer, bytes_to_write)) {
558                 retval = -EFAULT;
559                 goto unlock_exit;
560         }
561
562         if (dev->interrupt_out_endpoint == NULL) {
563                 /* try HID_REQ_SET_REPORT=9 on control_endpoint instead of interrupt_out_endpoint */
564                 retval = usb_control_msg(interface_to_usbdev(dev->intf),
565                                          usb_sndctrlpipe(interface_to_usbdev(dev->intf), 0),
566                                          9,
567                                          USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
568                                          1 << 8, 0,
569                                          dev->interrupt_out_buffer,
570                                          bytes_to_write,
571                                          USB_CTRL_SET_TIMEOUT * HZ);
572                 if (retval < 0)
573                         err("Couldn't submit HID_REQ_SET_REPORT %d\n", retval);
574                 goto unlock_exit;
575         }
576
577         /* send off the urb */
578         usb_fill_int_urb(dev->interrupt_out_urb,
579                          interface_to_usbdev(dev->intf),
580                          usb_sndintpipe(interface_to_usbdev(dev->intf),
581                                         dev->interrupt_out_endpoint->bEndpointAddress),
582                          dev->interrupt_out_buffer,
583                          bytes_to_write,
584                          ld_usb_interrupt_out_callback,
585                          dev,
586                          dev->interrupt_out_interval);
587
588         dev->interrupt_out_busy = 1;
589         wmb();
590
591         retval = usb_submit_urb(dev->interrupt_out_urb, GFP_KERNEL);
592         if (retval) {
593                 dev->interrupt_out_busy = 0;
594                 err("Couldn't submit interrupt_out_urb %d\n", retval);
595                 goto unlock_exit;
596         }
597         retval = bytes_to_write;
598
599 unlock_exit:
600         /* unlock the device */
601         mutex_unlock(&dev->mutex);
602
603 exit:
604         return retval;
605 }
606
607 /* file operations needed when we register this driver */
608 static const struct file_operations ld_usb_fops = {
609         .owner =        THIS_MODULE,
610         .read  =        ld_usb_read,
611         .write =        ld_usb_write,
612         .open =         ld_usb_open,
613         .release =      ld_usb_release,
614         .poll =         ld_usb_poll,
615 };
616
617 /*
618  * usb class driver info in order to get a minor number from the usb core,
619  * and to have the device registered with the driver core
620  */
621 static struct usb_class_driver ld_usb_class = {
622         .name =         "ldusb%d",
623         .fops =         &ld_usb_fops,
624         .minor_base =   USB_LD_MINOR_BASE,
625 };
626
627 /**
628  *      ld_usb_probe
629  *
630  *      Called by the usb core when a new device is connected that it thinks
631  *      this driver might be interested in.
632  */
633 static int ld_usb_probe(struct usb_interface *intf, const struct usb_device_id *id)
634 {
635         struct usb_device *udev = interface_to_usbdev(intf);
636         struct ld_usb *dev = NULL;
637         struct usb_host_interface *iface_desc;
638         struct usb_endpoint_descriptor *endpoint;
639         char *buffer;
640         int i;
641         int retval = -ENOMEM;
642
643         /* allocate memory for our device state and intialize it */
644
645         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
646         if (dev == NULL) {
647                 dev_err(&intf->dev, "Out of memory\n");
648                 goto exit;
649         }
650         mutex_init(&dev->mutex);
651         spin_lock_init(&dev->rbsl);
652         dev->intf = intf;
653         init_waitqueue_head(&dev->read_wait);
654         init_waitqueue_head(&dev->write_wait);
655
656         /* workaround for early firmware versions on fast computers */
657         if ((le16_to_cpu(udev->descriptor.idVendor) == USB_VENDOR_ID_LD) &&
658             ((le16_to_cpu(udev->descriptor.idProduct) == USB_DEVICE_ID_LD_CASSY) ||
659              (le16_to_cpu(udev->descriptor.idProduct) == USB_DEVICE_ID_LD_COM3LAB)) &&
660             (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x103)) {
661                 buffer = kmalloc(256, GFP_KERNEL);
662                 if (buffer == NULL) {
663                         dev_err(&intf->dev, "Couldn't allocate string buffer\n");
664                         goto error;
665                 }
666                 /* usb_string makes SETUP+STALL to leave always ControlReadLoop */
667                 usb_string(udev, 255, buffer, 256);
668                 kfree(buffer);
669         }
670
671         iface_desc = intf->cur_altsetting;
672
673         /* set up the endpoint information */
674         for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
675                 endpoint = &iface_desc->endpoint[i].desc;
676
677                 if (usb_endpoint_is_int_in(endpoint))
678                         dev->interrupt_in_endpoint = endpoint;
679
680                 if (usb_endpoint_is_int_out(endpoint))
681                         dev->interrupt_out_endpoint = endpoint;
682         }
683         if (dev->interrupt_in_endpoint == NULL) {
684                 dev_err(&intf->dev, "Interrupt in endpoint not found\n");
685                 goto error;
686         }
687         if (dev->interrupt_out_endpoint == NULL)
688                 dev_warn(&intf->dev, "Interrupt out endpoint not found (using control endpoint instead)\n");
689
690         dev->interrupt_in_endpoint_size = le16_to_cpu(dev->interrupt_in_endpoint->wMaxPacketSize);
691         dev->ring_buffer = kmalloc(ring_buffer_size*(sizeof(size_t)+dev->interrupt_in_endpoint_size), GFP_KERNEL);
692         if (!dev->ring_buffer) {
693                 dev_err(&intf->dev, "Couldn't allocate ring_buffer\n");
694                 goto error;
695         }
696         dev->interrupt_in_buffer = kmalloc(dev->interrupt_in_endpoint_size, GFP_KERNEL);
697         if (!dev->interrupt_in_buffer) {
698                 dev_err(&intf->dev, "Couldn't allocate interrupt_in_buffer\n");
699                 goto error;
700         }
701         dev->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
702         if (!dev->interrupt_in_urb) {
703                 dev_err(&intf->dev, "Couldn't allocate interrupt_in_urb\n");
704                 goto error;
705         }
706         dev->interrupt_out_endpoint_size = dev->interrupt_out_endpoint ? le16_to_cpu(dev->interrupt_out_endpoint->wMaxPacketSize) :
707                                                                          udev->descriptor.bMaxPacketSize0;
708         dev->interrupt_out_buffer = kmalloc(write_buffer_size*dev->interrupt_out_endpoint_size, GFP_KERNEL);
709         if (!dev->interrupt_out_buffer) {
710                 dev_err(&intf->dev, "Couldn't allocate interrupt_out_buffer\n");
711                 goto error;
712         }
713         dev->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL);
714         if (!dev->interrupt_out_urb) {
715                 dev_err(&intf->dev, "Couldn't allocate interrupt_out_urb\n");
716                 goto error;
717         }
718         dev->interrupt_in_interval = min_interrupt_in_interval > dev->interrupt_in_endpoint->bInterval ? min_interrupt_in_interval : dev->interrupt_in_endpoint->bInterval;
719         if (dev->interrupt_out_endpoint)
720                 dev->interrupt_out_interval = min_interrupt_out_interval > dev->interrupt_out_endpoint->bInterval ? min_interrupt_out_interval : 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         ld_usb_delete(dev);
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         mutex_lock(&dev->mutex);
765
766         /* if the device is not opened, then we clean up right now */
767         if (!dev->open_count) {
768                 mutex_unlock(&dev->mutex);
769                 ld_usb_delete(dev);
770         } else {
771                 dev->intf = NULL;
772                 mutex_unlock(&dev->mutex);
773         }
774
775         dev_info(&intf->dev, "LD USB Device #%d now disconnected\n",
776                  (minor - USB_LD_MINOR_BASE));
777 }
778
779 /* usb specific object needed to register this driver with the usb subsystem */
780 static struct usb_driver ld_usb_driver = {
781         .name =         "ldusb",
782         .probe =        ld_usb_probe,
783         .disconnect =   ld_usb_disconnect,
784         .id_table =     ld_usb_table,
785 };
786
787 /**
788  *      ld_usb_init
789  */
790 static int __init ld_usb_init(void)
791 {
792         int retval;
793
794         /* register this driver with the USB subsystem */
795         retval = usb_register(&ld_usb_driver);
796         if (retval)
797                 err("usb_register failed for the "__FILE__" driver. Error number %d\n", retval);
798
799         return retval;
800 }
801
802 /**
803  *      ld_usb_exit
804  */
805 static void __exit ld_usb_exit(void)
806 {
807         /* deregister this driver with the USB subsystem */
808         usb_deregister(&ld_usb_driver);
809 }
810
811 module_init(ld_usb_init);
812 module_exit(ld_usb_exit);
813