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