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