[PATCH] pcmcia: fix zeroing of cm4000_cs.c data
[linux-2.6] / 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/config.h>
31 #include <linux/kernel.h>
32 #include <linux/errno.h>
33 #include <linux/init.h>
34 #include <linux/slab.h>
35 #include <linux/module.h>
36 #include <linux/mutex.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_LABPRO    0x0001
62 #define USB_DEVICE_ID_VERNIER_GOTEMP    0x0002
63 #define USB_DEVICE_ID_VERNIER_SKIP      0x0003
64 #define USB_DEVICE_ID_VERNIER_CYCLOPS   0x0004
65
66
67 #ifdef CONFIG_USB_DYNAMIC_MINORS
68 #define USB_LD_MINOR_BASE       0
69 #else
70 #define USB_LD_MINOR_BASE       176
71 #endif
72
73 /* table of devices that work with this driver */
74 static struct usb_device_id ld_usb_table [] = {
75         { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_CASSY) },
76         { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_POCKETCASSY) },
77         { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MOBILECASSY) },
78         { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_JWM) },
79         { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_DMMP) },
80         { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_UMIP) },
81         { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_XRAY1) },
82         { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_XRAY2) },
83         { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_VIDEOCOM) },
84         { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_COM3LAB) },
85         { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_TELEPORT) },
86         { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_NETWORKANALYSER) },
87         { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_POWERCONTROL) },
88         { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MACHINETEST) },
89         { USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_LABPRO) },
90         { USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_GOTEMP) },
91         { USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_SKIP) },
92         { USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_CYCLOPS) },
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 semaphore        sem;            /* 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
167         char*                   interrupt_out_buffer;
168         struct usb_endpoint_descriptor* interrupt_out_endpoint;
169         struct urb*             interrupt_out_urb;
170         int                     interrupt_out_interval;
171         size_t                  interrupt_out_endpoint_size;
172         int                     interrupt_out_busy;
173 };
174
175 /* prevent races between open() and disconnect() */
176 static DEFINE_MUTEX(disconnect_mutex);
177
178 static struct usb_driver ld_usb_driver;
179
180 /**
181  *      ld_usb_abort_transfers
182  *      aborts transfers and frees associated data structures
183  */
184 static void ld_usb_abort_transfers(struct ld_usb *dev)
185 {
186         /* shutdown transfer */
187         if (dev->interrupt_in_running) {
188                 dev->interrupt_in_running = 0;
189                 if (dev->intf)
190                         usb_kill_urb(dev->interrupt_in_urb);
191         }
192         if (dev->interrupt_out_busy)
193                 if (dev->intf)
194                         usb_kill_urb(dev->interrupt_out_urb);
195 }
196
197 /**
198  *      ld_usb_delete
199  */
200 static void ld_usb_delete(struct ld_usb *dev)
201 {
202         ld_usb_abort_transfers(dev);
203
204         /* free data structures */
205         usb_free_urb(dev->interrupt_in_urb);
206         usb_free_urb(dev->interrupt_out_urb);
207         kfree(dev->ring_buffer);
208         kfree(dev->interrupt_in_buffer);
209         kfree(dev->interrupt_out_buffer);
210         kfree(dev);
211 }
212
213 /**
214  *      ld_usb_interrupt_in_callback
215  */
216 static void ld_usb_interrupt_in_callback(struct urb *urb, struct pt_regs *regs)
217 {
218         struct ld_usb *dev = urb->context;
219         size_t *actual_buffer;
220         unsigned int next_ring_head;
221         int retval;
222
223         if (urb->status) {
224                 if (urb->status == -ENOENT ||
225                     urb->status == -ECONNRESET ||
226                     urb->status == -ESHUTDOWN) {
227                         goto exit;
228                 } else {
229                         dbg_info(&dev->intf->dev, "%s: nonzero status received: %d\n",
230                                  __FUNCTION__, urb->status);
231                         goto resubmit; /* maybe we can recover */
232                 }
233         }
234
235         if (urb->actual_length > 0) {
236                 next_ring_head = (dev->ring_head+1) % ring_buffer_size;
237                 if (next_ring_head != dev->ring_tail) {
238                         actual_buffer = (size_t*)(dev->ring_buffer + dev->ring_head*(sizeof(size_t)+dev->interrupt_in_endpoint_size));
239                         /* actual_buffer gets urb->actual_length + interrupt_in_buffer */
240                         *actual_buffer = urb->actual_length;
241                         memcpy(actual_buffer+1, dev->interrupt_in_buffer, urb->actual_length);
242                         dev->ring_head = next_ring_head;
243                         dbg_info(&dev->intf->dev, "%s: received %d bytes\n",
244                                  __FUNCTION__, urb->actual_length);
245                 } else
246                         dev_warn(&dev->intf->dev,
247                                  "Ring buffer overflow, %d bytes dropped\n",
248                                  urb->actual_length);
249         }
250
251 resubmit:
252         /* resubmit if we're still running */
253         if (dev->interrupt_in_running && dev->intf) {
254                 retval = usb_submit_urb(dev->interrupt_in_urb, GFP_ATOMIC);
255                 if (retval)
256                         dev_err(&dev->intf->dev,
257                                 "usb_submit_urb failed (%d)\n", retval);
258         }
259
260 exit:
261         dev->interrupt_in_done = 1;
262         wake_up_interruptible(&dev->read_wait);
263 }
264
265 /**
266  *      ld_usb_interrupt_out_callback
267  */
268 static void ld_usb_interrupt_out_callback(struct urb *urb, struct pt_regs *regs)
269 {
270         struct ld_usb *dev = urb->context;
271
272         /* sync/async unlink faults aren't errors */
273         if (urb->status && !(urb->status == -ENOENT ||
274                              urb->status == -ECONNRESET ||
275                              urb->status == -ESHUTDOWN))
276                 dbg_info(&dev->intf->dev,
277                          "%s - nonzero write interrupt status received: %d\n",
278                          __FUNCTION__, urb->status);
279
280         dev->interrupt_out_busy = 0;
281         wake_up_interruptible(&dev->write_wait);
282 }
283
284 /**
285  *      ld_usb_open
286  */
287 static int ld_usb_open(struct inode *inode, struct file *file)
288 {
289         struct ld_usb *dev;
290         int subminor;
291         int retval = 0;
292         struct usb_interface *interface;
293
294         nonseekable_open(inode, file);
295         subminor = iminor(inode);
296
297         mutex_lock(&disconnect_mutex);
298
299         interface = usb_find_interface(&ld_usb_driver, subminor);
300
301         if (!interface) {
302                 err("%s - error, can't find device for minor %d\n",
303                      __FUNCTION__, subminor);
304                 retval = -ENODEV;
305                 goto unlock_disconnect_exit;
306         }
307
308         dev = usb_get_intfdata(interface);
309
310         if (!dev) {
311                 retval = -ENODEV;
312                 goto unlock_disconnect_exit;
313         }
314
315         /* lock this device */
316         if (down_interruptible(&dev->sem)) {
317                 retval = -ERESTARTSYS;
318                 goto unlock_disconnect_exit;
319         }
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         usb_fill_int_urb(dev->interrupt_in_urb,
332                          interface_to_usbdev(interface),
333                          usb_rcvintpipe(interface_to_usbdev(interface),
334                                         dev->interrupt_in_endpoint->bEndpointAddress),
335                          dev->interrupt_in_buffer,
336                          dev->interrupt_in_endpoint_size,
337                          ld_usb_interrupt_in_callback,
338                          dev,
339                          dev->interrupt_in_interval);
340
341         dev->interrupt_in_running = 1;
342         dev->interrupt_in_done = 0;
343
344         retval = usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL);
345         if (retval) {
346                 dev_err(&interface->dev, "Couldn't submit interrupt_in_urb %d\n", retval);
347                 dev->interrupt_in_running = 0;
348                 dev->open_count = 0;
349                 goto unlock_exit;
350         }
351
352         /* save device in the file's private structure */
353         file->private_data = dev;
354
355 unlock_exit:
356         up(&dev->sem);
357
358 unlock_disconnect_exit:
359         mutex_unlock(&disconnect_mutex);
360
361         return retval;
362 }
363
364 /**
365  *      ld_usb_release
366  */
367 static int ld_usb_release(struct inode *inode, struct file *file)
368 {
369         struct ld_usb *dev;
370         int retval = 0;
371
372         dev = file->private_data;
373
374         if (dev == NULL) {
375                 retval = -ENODEV;
376                 goto exit;
377         }
378
379         if (down_interruptible(&dev->sem)) {
380                 retval = -ERESTARTSYS;
381                 goto exit;
382         }
383
384         if (dev->open_count != 1) {
385                 retval = -ENODEV;
386                 goto unlock_exit;
387         }
388         if (dev->intf == NULL) {
389                 /* the device was unplugged before the file was released */
390                 up(&dev->sem);
391                 /* unlock here as ld_usb_delete frees dev */
392                 ld_usb_delete(dev);
393                 goto exit;
394         }
395
396         /* wait until write transfer is finished */
397         if (dev->interrupt_out_busy)
398                 wait_event_interruptible_timeout(dev->write_wait, !dev->interrupt_out_busy, 2 * HZ);
399         ld_usb_abort_transfers(dev);
400         dev->open_count = 0;
401
402 unlock_exit:
403         up(&dev->sem);
404
405 exit:
406         return retval;
407 }
408
409 /**
410  *      ld_usb_poll
411  */
412 static unsigned int ld_usb_poll(struct file *file, poll_table *wait)
413 {
414         struct ld_usb *dev;
415         unsigned int mask = 0;
416
417         dev = file->private_data;
418
419         poll_wait(file, &dev->read_wait, wait);
420         poll_wait(file, &dev->write_wait, wait);
421
422         if (dev->ring_head != dev->ring_tail)
423                 mask |= POLLIN | POLLRDNORM;
424         if (!dev->interrupt_out_busy)
425                 mask |= POLLOUT | POLLWRNORM;
426
427         return mask;
428 }
429
430 /**
431  *      ld_usb_read
432  */
433 static ssize_t ld_usb_read(struct file *file, char __user *buffer, size_t count,
434                            loff_t *ppos)
435 {
436         struct ld_usb *dev;
437         size_t *actual_buffer;
438         size_t bytes_to_read;
439         int retval = 0;
440
441         dev = file->private_data;
442
443         /* verify that we actually have some data to read */
444         if (count == 0)
445                 goto exit;
446
447         /* lock this object */
448         if (down_interruptible(&dev->sem)) {
449                 retval = -ERESTARTSYS;
450                 goto exit;
451         }
452
453         /* verify that the device wasn't unplugged */
454         if (dev->intf == NULL) {
455                 retval = -ENODEV;
456                 err("No device or device unplugged %d\n", retval);
457                 goto unlock_exit;
458         }
459
460         /* wait for data */
461         if (dev->ring_head == dev->ring_tail) {
462                 if (file->f_flags & O_NONBLOCK) {
463                         retval = -EAGAIN;
464                         goto unlock_exit;
465                 }
466                 retval = wait_event_interruptible(dev->read_wait, dev->interrupt_in_done);
467                 if (retval < 0)
468                         goto unlock_exit;
469         }
470
471         /* actual_buffer contains actual_length + interrupt_in_buffer */
472         actual_buffer = (size_t*)(dev->ring_buffer + dev->ring_tail*(sizeof(size_t)+dev->interrupt_in_endpoint_size));
473         bytes_to_read = min(count, *actual_buffer);
474         if (bytes_to_read < *actual_buffer)
475                 dev_warn(&dev->intf->dev, "Read buffer overflow, %zd bytes dropped\n",
476                          *actual_buffer-bytes_to_read);
477
478         /* copy one interrupt_in_buffer from ring_buffer into userspace */
479         if (copy_to_user(buffer, actual_buffer+1, bytes_to_read)) {
480                 retval = -EFAULT;
481                 goto unlock_exit;
482         }
483         dev->ring_tail = (dev->ring_tail+1) % ring_buffer_size;
484
485         retval = bytes_to_read;
486
487 unlock_exit:
488         /* unlock the device */
489         up(&dev->sem);
490
491 exit:
492         return retval;
493 }
494
495 /**
496  *      ld_usb_write
497  */
498 static ssize_t ld_usb_write(struct file *file, const char __user *buffer,
499                             size_t count, loff_t *ppos)
500 {
501         struct ld_usb *dev;
502         size_t bytes_to_write;
503         int retval = 0;
504
505         dev = file->private_data;
506
507         /* verify that we actually have some data to write */
508         if (count == 0)
509                 goto exit;
510
511         /* lock this object */
512         if (down_interruptible(&dev->sem)) {
513                 retval = -ERESTARTSYS;
514                 goto exit;
515         }
516
517         /* verify that the device wasn't unplugged */
518         if (dev->intf == NULL) {
519                 retval = -ENODEV;
520                 err("No device or device unplugged %d\n", retval);
521                 goto unlock_exit;
522         }
523
524         /* wait until previous transfer is finished */
525         if (dev->interrupt_out_busy) {
526                 if (file->f_flags & O_NONBLOCK) {
527                         retval = -EAGAIN;
528                         goto unlock_exit;
529                 }
530                 retval = wait_event_interruptible(dev->write_wait, !dev->interrupt_out_busy);
531                 if (retval < 0) {
532                         goto unlock_exit;
533                 }
534         }
535
536         /* write the data into interrupt_out_buffer from userspace */
537         bytes_to_write = min(count, write_buffer_size*dev->interrupt_out_endpoint_size);
538         if (bytes_to_write < count)
539                 dev_warn(&dev->intf->dev, "Write buffer overflow, %zd bytes dropped\n",count-bytes_to_write);
540         dbg_info(&dev->intf->dev, "%s: count = %zd, bytes_to_write = %zd\n", __FUNCTION__, count, bytes_to_write);
541
542         if (copy_from_user(dev->interrupt_out_buffer, buffer, bytes_to_write)) {
543                 retval = -EFAULT;
544                 goto unlock_exit;
545         }
546
547         if (dev->interrupt_out_endpoint == NULL) {
548                 /* try HID_REQ_SET_REPORT=9 on control_endpoint instead of interrupt_out_endpoint */
549                 retval = usb_control_msg(interface_to_usbdev(dev->intf),
550                                          usb_sndctrlpipe(interface_to_usbdev(dev->intf), 0),
551                                          9,
552                                          USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
553                                          1 << 8, 0,
554                                          dev->interrupt_out_buffer,
555                                          bytes_to_write,
556                                          USB_CTRL_SET_TIMEOUT * HZ);
557                 if (retval < 0)
558                         err("Couldn't submit HID_REQ_SET_REPORT %d\n", retval);
559                 goto unlock_exit;
560         }
561
562         /* send off the urb */
563         usb_fill_int_urb(dev->interrupt_out_urb,
564                          interface_to_usbdev(dev->intf),
565                          usb_sndintpipe(interface_to_usbdev(dev->intf),
566                                         dev->interrupt_out_endpoint->bEndpointAddress),
567                          dev->interrupt_out_buffer,
568                          bytes_to_write,
569                          ld_usb_interrupt_out_callback,
570                          dev,
571                          dev->interrupt_out_interval);
572
573         dev->interrupt_out_busy = 1;
574         wmb();
575
576         retval = usb_submit_urb(dev->interrupt_out_urb, GFP_KERNEL);
577         if (retval) {
578                 dev->interrupt_out_busy = 0;
579                 err("Couldn't submit interrupt_out_urb %d\n", retval);
580                 goto unlock_exit;
581         }
582         retval = bytes_to_write;
583
584 unlock_exit:
585         /* unlock the device */
586         up(&dev->sem);
587
588 exit:
589         return retval;
590 }
591
592 /* file operations needed when we register this driver */
593 static struct file_operations ld_usb_fops = {
594         .owner =        THIS_MODULE,
595         .read  =        ld_usb_read,
596         .write =        ld_usb_write,
597         .open =         ld_usb_open,
598         .release =      ld_usb_release,
599         .poll =         ld_usb_poll,
600 };
601
602 /*
603  * usb class driver info in order to get a minor number from the usb core,
604  * and to have the device registered with the driver core
605  */
606 static struct usb_class_driver ld_usb_class = {
607         .name =         "ldusb%d",
608         .fops =         &ld_usb_fops,
609         .minor_base =   USB_LD_MINOR_BASE,
610 };
611
612 /**
613  *      ld_usb_probe
614  *
615  *      Called by the usb core when a new device is connected that it thinks
616  *      this driver might be interested in.
617  */
618 static int ld_usb_probe(struct usb_interface *intf, const struct usb_device_id *id)
619 {
620         struct usb_device *udev = interface_to_usbdev(intf);
621         struct ld_usb *dev = NULL;
622         struct usb_host_interface *iface_desc;
623         struct usb_endpoint_descriptor *endpoint;
624         char *buffer;
625         int i;
626         int retval = -ENOMEM;
627
628         /* allocate memory for our device state and intialize it */
629
630         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
631         if (dev == NULL) {
632                 dev_err(&intf->dev, "Out of memory\n");
633                 goto exit;
634         }
635         init_MUTEX(&dev->sem);
636         dev->intf = intf;
637         init_waitqueue_head(&dev->read_wait);
638         init_waitqueue_head(&dev->write_wait);
639
640         /* workaround for early firmware versions on fast computers */
641         if ((le16_to_cpu(udev->descriptor.idVendor) == USB_VENDOR_ID_LD) &&
642             ((le16_to_cpu(udev->descriptor.idProduct) == USB_DEVICE_ID_LD_CASSY) ||
643              (le16_to_cpu(udev->descriptor.idProduct) == USB_DEVICE_ID_LD_COM3LAB)) &&
644             (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x103)) {
645                 buffer = kmalloc(256, GFP_KERNEL);
646                 if (buffer == NULL) {
647                         dev_err(&intf->dev, "Couldn't allocate string buffer\n");
648                         goto error;
649                 }
650                 /* usb_string makes SETUP+STALL to leave always ControlReadLoop */
651                 usb_string(udev, 255, buffer, 256);
652                 kfree(buffer);
653         }
654
655         iface_desc = intf->cur_altsetting;
656
657         /* set up the endpoint information */
658         for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
659                 endpoint = &iface_desc->endpoint[i].desc;
660
661                 if (((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) &&
662                     ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)) {
663                         dev->interrupt_in_endpoint = endpoint;
664                 }
665
666                 if (((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) &&
667                     ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)) {
668                         dev->interrupt_out_endpoint = endpoint;
669                 }
670         }
671         if (dev->interrupt_in_endpoint == NULL) {
672                 dev_err(&intf->dev, "Interrupt in endpoint not found\n");
673                 goto error;
674         }
675         if (dev->interrupt_out_endpoint == NULL)
676                 dev_warn(&intf->dev, "Interrupt out endpoint not found (using control endpoint instead)\n");
677
678         dev->interrupt_in_endpoint_size = le16_to_cpu(dev->interrupt_in_endpoint->wMaxPacketSize);
679         dev->ring_buffer = kmalloc(ring_buffer_size*(sizeof(size_t)+dev->interrupt_in_endpoint_size), GFP_KERNEL);
680         if (!dev->ring_buffer) {
681                 dev_err(&intf->dev, "Couldn't allocate ring_buffer\n");
682                 goto error;
683         }
684         dev->interrupt_in_buffer = kmalloc(dev->interrupt_in_endpoint_size, GFP_KERNEL);
685         if (!dev->interrupt_in_buffer) {
686                 dev_err(&intf->dev, "Couldn't allocate interrupt_in_buffer\n");
687                 goto error;
688         }
689         dev->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
690         if (!dev->interrupt_in_urb) {
691                 dev_err(&intf->dev, "Couldn't allocate interrupt_in_urb\n");
692                 goto error;
693         }
694         dev->interrupt_out_endpoint_size = dev->interrupt_out_endpoint ? le16_to_cpu(dev->interrupt_out_endpoint->wMaxPacketSize) :
695                                                                          udev->descriptor.bMaxPacketSize0;
696         dev->interrupt_out_buffer = kmalloc(write_buffer_size*dev->interrupt_out_endpoint_size, GFP_KERNEL);
697         if (!dev->interrupt_out_buffer) {
698                 dev_err(&intf->dev, "Couldn't allocate interrupt_out_buffer\n");
699                 goto error;
700         }
701         dev->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL);
702         if (!dev->interrupt_out_urb) {
703                 dev_err(&intf->dev, "Couldn't allocate interrupt_out_urb\n");
704                 goto error;
705         }
706         dev->interrupt_in_interval = min_interrupt_in_interval > dev->interrupt_in_endpoint->bInterval ? min_interrupt_in_interval : dev->interrupt_in_endpoint->bInterval;
707         if (dev->interrupt_out_endpoint)
708                 dev->interrupt_out_interval = min_interrupt_out_interval > dev->interrupt_out_endpoint->bInterval ? min_interrupt_out_interval : dev->interrupt_out_endpoint->bInterval;
709
710         /* we can register the device now, as it is ready */
711         usb_set_intfdata(intf, dev);
712
713         retval = usb_register_dev(intf, &ld_usb_class);
714         if (retval) {
715                 /* something prevented us from registering this driver */
716                 dev_err(&intf->dev, "Not able to get a minor for this device.\n");
717                 usb_set_intfdata(intf, NULL);
718                 goto error;
719         }
720
721         /* let the user know what node this device is now attached to */
722         dev_info(&intf->dev, "LD USB Device #%d now attached to major %d minor %d\n",
723                 (intf->minor - USB_LD_MINOR_BASE), USB_MAJOR, intf->minor);
724
725 exit:
726         return retval;
727
728 error:
729         ld_usb_delete(dev);
730
731         return retval;
732 }
733
734 /**
735  *      ld_usb_disconnect
736  *
737  *      Called by the usb core when the device is removed from the system.
738  */
739 static void ld_usb_disconnect(struct usb_interface *intf)
740 {
741         struct ld_usb *dev;
742         int minor;
743
744         mutex_lock(&disconnect_mutex);
745
746         dev = usb_get_intfdata(intf);
747         usb_set_intfdata(intf, NULL);
748
749         down(&dev->sem);
750
751         minor = intf->minor;
752
753         /* give back our minor */
754         usb_deregister_dev(intf, &ld_usb_class);
755
756         /* if the device is not opened, then we clean up right now */
757         if (!dev->open_count) {
758                 up(&dev->sem);
759                 ld_usb_delete(dev);
760         } else {
761                 dev->intf = NULL;
762                 up(&dev->sem);
763         }
764
765         mutex_unlock(&disconnect_mutex);
766
767         dev_info(&intf->dev, "LD USB Device #%d now disconnected\n",
768                  (minor - USB_LD_MINOR_BASE));
769 }
770
771 /* usb specific object needed to register this driver with the usb subsystem */
772 static struct usb_driver ld_usb_driver = {
773         .name =         "ldusb",
774         .probe =        ld_usb_probe,
775         .disconnect =   ld_usb_disconnect,
776         .id_table =     ld_usb_table,
777 };
778
779 /**
780  *      ld_usb_init
781  */
782 static int __init ld_usb_init(void)
783 {
784         int retval;
785
786         /* register this driver with the USB subsystem */
787         retval = usb_register(&ld_usb_driver);
788         if (retval)
789                 err("usb_register failed for the "__FILE__" driver. Error number %d\n", retval);
790
791         return retval;
792 }
793
794 /**
795  *      ld_usb_exit
796  */
797 static void __exit ld_usb_exit(void)
798 {
799         /* deregister this driver with the USB subsystem */
800         usb_deregister(&ld_usb_driver);
801 }
802
803 module_init(ld_usb_init);
804 module_exit(ld_usb_exit);
805