mtd: nand_base: use __func__ instead of typing names
[linux/fpc-iii.git] / drivers / usb / misc / ldusb.c
blobad4fb15b5dcbcaa6a12b26037880e4121900d9a2
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).
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.
13 * Copyright (C) 2005 Michael Hund <mhund@ld-didactic.de>
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.
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>
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
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>
37 #include <asm/uaccess.h>
38 #include <linux/input.h>
39 #include <linux/usb.h>
40 #include <linux/poll.h>
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 */
59 #define USB_VENDOR_ID_VERNIER 0x08f7
60 #define USB_DEVICE_ID_VERNIER_GOTEMP 0x0002
61 #define USB_DEVICE_ID_VERNIER_SKIP 0x0003
62 #define USB_DEVICE_ID_VERNIER_CYCLOPS 0x0004
63 #define USB_DEVICE_ID_VERNIER_LCSPEC 0x0006
65 #ifdef CONFIG_USB_DYNAMIC_MINORS
66 #define USB_LD_MINOR_BASE 0
67 #else
68 #define USB_LD_MINOR_BASE 176
69 #endif
71 /* table of devices that work with this driver */
72 static struct usb_device_id ld_usb_table [] = {
73 { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_CASSY) },
74 { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_POCKETCASSY) },
75 { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MOBILECASSY) },
76 { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_JWM) },
77 { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_DMMP) },
78 { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_UMIP) },
79 { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_XRAY1) },
80 { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_XRAY2) },
81 { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_VIDEOCOM) },
82 { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_COM3LAB) },
83 { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_TELEPORT) },
84 { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_NETWORKANALYSER) },
85 { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_POWERCONTROL) },
86 { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MACHINETEST) },
87 { USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_GOTEMP) },
88 { USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_SKIP) },
89 { USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_CYCLOPS) },
90 { USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_LCSPEC) },
91 { } /* Terminating entry */
93 MODULE_DEVICE_TABLE(usb, ld_usb_table);
94 MODULE_VERSION("V0.13");
95 MODULE_AUTHOR("Michael Hund <mhund@ld-didactic.de>");
96 MODULE_DESCRIPTION("LD USB Driver");
97 MODULE_LICENSE("GPL");
98 MODULE_SUPPORTED_DEVICE("LD USB Devices");
100 #ifdef CONFIG_USB_DEBUG
101 static int debug = 1;
102 #else
103 static int debug = 0;
104 #endif
106 /* Use our own dbg macro */
107 #define dbg_info(dev, format, arg...) do { if (debug) dev_info(dev , format , ## arg); } while (0)
109 /* Module parameters */
110 module_param(debug, int, S_IRUGO | S_IWUSR);
111 MODULE_PARM_DESC(debug, "Debug enabled or not");
113 /* All interrupt in transfers are collected in a ring buffer to
114 * avoid racing conditions and get better performance of the driver.
116 static int ring_buffer_size = 128;
117 module_param(ring_buffer_size, int, 0);
118 MODULE_PARM_DESC(ring_buffer_size, "Read ring buffer size in reports");
120 /* The write_buffer can contain more than one interrupt out transfer.
122 static int write_buffer_size = 10;
123 module_param(write_buffer_size, int, 0);
124 MODULE_PARM_DESC(write_buffer_size, "Write buffer size in reports");
126 /* As of kernel version 2.6.4 ehci-hcd uses an
127 * "only one interrupt transfer per frame" shortcut
128 * to simplify the scheduling of periodic transfers.
129 * This conflicts with our standard 1ms intervals for in and out URBs.
130 * We use default intervals of 2ms for in and 2ms for out transfers,
131 * which should be fast enough.
132 * Increase the interval to allow more devices that do interrupt transfers,
133 * or set to 1 to use the standard interval from the endpoint descriptors.
135 static int min_interrupt_in_interval = 2;
136 module_param(min_interrupt_in_interval, int, 0);
137 MODULE_PARM_DESC(min_interrupt_in_interval, "Minimum interrupt in interval in ms");
139 static int min_interrupt_out_interval = 2;
140 module_param(min_interrupt_out_interval, int, 0);
141 MODULE_PARM_DESC(min_interrupt_out_interval, "Minimum interrupt out interval in ms");
143 /* Structure to hold all of our device specific stuff */
144 struct ld_usb {
145 struct mutex mutex; /* locks this structure */
146 struct usb_interface* intf; /* save off the usb interface pointer */
148 int open_count; /* number of times this port has been opened */
150 char* ring_buffer;
151 unsigned int ring_head;
152 unsigned int ring_tail;
154 wait_queue_head_t read_wait;
155 wait_queue_head_t write_wait;
157 char* interrupt_in_buffer;
158 struct usb_endpoint_descriptor* interrupt_in_endpoint;
159 struct urb* interrupt_in_urb;
160 int interrupt_in_interval;
161 size_t interrupt_in_endpoint_size;
162 int interrupt_in_running;
163 int interrupt_in_done;
164 int buffer_overflow;
165 spinlock_t rbsl;
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;
175 static struct usb_driver ld_usb_driver;
178 * ld_usb_abort_transfers
179 * aborts transfers and frees associated data structures
181 static void ld_usb_abort_transfers(struct ld_usb *dev)
183 /* shutdown transfer */
184 if (dev->interrupt_in_running) {
185 dev->interrupt_in_running = 0;
186 if (dev->intf)
187 usb_kill_urb(dev->interrupt_in_urb);
189 if (dev->interrupt_out_busy)
190 if (dev->intf)
191 usb_kill_urb(dev->interrupt_out_urb);
195 * ld_usb_delete
197 static void ld_usb_delete(struct ld_usb *dev)
199 ld_usb_abort_transfers(dev);
201 /* free data structures */
202 usb_free_urb(dev->interrupt_in_urb);
203 usb_free_urb(dev->interrupt_out_urb);
204 kfree(dev->ring_buffer);
205 kfree(dev->interrupt_in_buffer);
206 kfree(dev->interrupt_out_buffer);
207 kfree(dev);
211 * ld_usb_interrupt_in_callback
213 static void ld_usb_interrupt_in_callback(struct urb *urb)
215 struct ld_usb *dev = urb->context;
216 size_t *actual_buffer;
217 unsigned int next_ring_head;
218 int status = urb->status;
219 int retval;
221 if (status) {
222 if (status == -ENOENT ||
223 status == -ECONNRESET ||
224 status == -ESHUTDOWN) {
225 goto exit;
226 } else {
227 dbg_info(&dev->intf->dev, "%s: nonzero status received: %d\n",
228 __func__, status);
229 spin_lock(&dev->rbsl);
230 goto resubmit; /* maybe we can recover */
234 spin_lock(&dev->rbsl);
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 __func__, urb->actual_length);
245 } else {
246 dev_warn(&dev->intf->dev,
247 "Ring buffer overflow, %d bytes dropped\n",
248 urb->actual_length);
249 dev->buffer_overflow = 1;
253 resubmit:
254 /* resubmit if we're still running */
255 if (dev->interrupt_in_running && !dev->buffer_overflow && 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 dev->buffer_overflow = 1;
263 spin_unlock(&dev->rbsl);
264 exit:
265 dev->interrupt_in_done = 1;
266 wake_up_interruptible(&dev->read_wait);
270 * ld_usb_interrupt_out_callback
272 static void ld_usb_interrupt_out_callback(struct urb *urb)
274 struct ld_usb *dev = urb->context;
275 int status = urb->status;
277 /* sync/async unlink faults aren't errors */
278 if (status && !(status == -ENOENT ||
279 status == -ECONNRESET ||
280 status == -ESHUTDOWN))
281 dbg_info(&dev->intf->dev,
282 "%s - nonzero write interrupt status received: %d\n",
283 __func__, status);
285 dev->interrupt_out_busy = 0;
286 wake_up_interruptible(&dev->write_wait);
290 * ld_usb_open
292 static int ld_usb_open(struct inode *inode, struct file *file)
294 struct ld_usb *dev;
295 int subminor;
296 int retval;
297 struct usb_interface *interface;
299 nonseekable_open(inode, file);
300 subminor = iminor(inode);
302 interface = usb_find_interface(&ld_usb_driver, subminor);
304 if (!interface) {
305 err("%s - error, can't find device for minor %d\n",
306 __func__, subminor);
307 return -ENODEV;
310 dev = usb_get_intfdata(interface);
312 if (!dev)
313 return -ENODEV;
315 /* lock this device */
316 if (mutex_lock_interruptible(&dev->mutex))
317 return -ERESTARTSYS;
319 /* allow opening only once */
320 if (dev->open_count) {
321 retval = -EBUSY;
322 goto unlock_exit;
324 dev->open_count = 1;
326 /* initialize in direction */
327 dev->ring_head = 0;
328 dev->ring_tail = 0;
329 dev->buffer_overflow = 0;
330 usb_fill_int_urb(dev->interrupt_in_urb,
331 interface_to_usbdev(interface),
332 usb_rcvintpipe(interface_to_usbdev(interface),
333 dev->interrupt_in_endpoint->bEndpointAddress),
334 dev->interrupt_in_buffer,
335 dev->interrupt_in_endpoint_size,
336 ld_usb_interrupt_in_callback,
337 dev,
338 dev->interrupt_in_interval);
340 dev->interrupt_in_running = 1;
341 dev->interrupt_in_done = 0;
343 retval = usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL);
344 if (retval) {
345 dev_err(&interface->dev, "Couldn't submit interrupt_in_urb %d\n", retval);
346 dev->interrupt_in_running = 0;
347 dev->open_count = 0;
348 goto unlock_exit;
351 /* save device in the file's private structure */
352 file->private_data = dev;
354 unlock_exit:
355 mutex_unlock(&dev->mutex);
357 return retval;
361 * ld_usb_release
363 static int ld_usb_release(struct inode *inode, struct file *file)
365 struct ld_usb *dev;
366 int retval = 0;
368 dev = file->private_data;
370 if (dev == NULL) {
371 retval = -ENODEV;
372 goto exit;
375 if (mutex_lock_interruptible(&dev->mutex)) {
376 retval = -ERESTARTSYS;
377 goto exit;
380 if (dev->open_count != 1) {
381 retval = -ENODEV;
382 goto unlock_exit;
384 if (dev->intf == NULL) {
385 /* the device was unplugged before the file was released */
386 mutex_unlock(&dev->mutex);
387 /* unlock here as ld_usb_delete frees dev */
388 ld_usb_delete(dev);
389 goto exit;
392 /* wait until write transfer is finished */
393 if (dev->interrupt_out_busy)
394 wait_event_interruptible_timeout(dev->write_wait, !dev->interrupt_out_busy, 2 * HZ);
395 ld_usb_abort_transfers(dev);
396 dev->open_count = 0;
398 unlock_exit:
399 mutex_unlock(&dev->mutex);
401 exit:
402 return retval;
406 * ld_usb_poll
408 static unsigned int ld_usb_poll(struct file *file, poll_table *wait)
410 struct ld_usb *dev;
411 unsigned int mask = 0;
413 dev = file->private_data;
415 poll_wait(file, &dev->read_wait, wait);
416 poll_wait(file, &dev->write_wait, wait);
418 if (dev->ring_head != dev->ring_tail)
419 mask |= POLLIN | POLLRDNORM;
420 if (!dev->interrupt_out_busy)
421 mask |= POLLOUT | POLLWRNORM;
423 return mask;
427 * ld_usb_read
429 static ssize_t ld_usb_read(struct file *file, char __user *buffer, size_t count,
430 loff_t *ppos)
432 struct ld_usb *dev;
433 size_t *actual_buffer;
434 size_t bytes_to_read;
435 int retval = 0;
436 int rv;
438 dev = file->private_data;
440 /* verify that we actually have some data to read */
441 if (count == 0)
442 goto exit;
444 /* lock this object */
445 if (mutex_lock_interruptible(&dev->mutex)) {
446 retval = -ERESTARTSYS;
447 goto exit;
450 /* verify that the device wasn't unplugged */
451 if (dev->intf == NULL) {
452 retval = -ENODEV;
453 err("No device or device unplugged %d\n", retval);
454 goto unlock_exit;
457 /* wait for data */
458 spin_lock_irq(&dev->rbsl);
459 if (dev->ring_head == dev->ring_tail) {
460 dev->interrupt_in_done = 0;
461 spin_unlock_irq(&dev->rbsl);
462 if (file->f_flags & O_NONBLOCK) {
463 retval = -EAGAIN;
464 goto unlock_exit;
466 retval = wait_event_interruptible(dev->read_wait, dev->interrupt_in_done);
467 if (retval < 0)
468 goto unlock_exit;
469 } else {
470 spin_unlock_irq(&dev->rbsl);
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);
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;
485 dev->ring_tail = (dev->ring_tail+1) % ring_buffer_size;
487 retval = bytes_to_read;
489 spin_lock_irq(&dev->rbsl);
490 if (dev->buffer_overflow) {
491 dev->buffer_overflow = 0;
492 spin_unlock_irq(&dev->rbsl);
493 rv = usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL);
494 if (rv < 0)
495 dev->buffer_overflow = 1;
496 } else {
497 spin_unlock_irq(&dev->rbsl);
500 unlock_exit:
501 /* unlock the device */
502 mutex_unlock(&dev->mutex);
504 exit:
505 return retval;
509 * ld_usb_write
511 static ssize_t ld_usb_write(struct file *file, const char __user *buffer,
512 size_t count, loff_t *ppos)
514 struct ld_usb *dev;
515 size_t bytes_to_write;
516 int retval = 0;
518 dev = file->private_data;
520 /* verify that we actually have some data to write */
521 if (count == 0)
522 goto exit;
524 /* lock this object */
525 if (mutex_lock_interruptible(&dev->mutex)) {
526 retval = -ERESTARTSYS;
527 goto exit;
530 /* verify that the device wasn't unplugged */
531 if (dev->intf == NULL) {
532 retval = -ENODEV;
533 err("No device or device unplugged %d\n", retval);
534 goto unlock_exit;
537 /* wait until previous transfer is finished */
538 if (dev->interrupt_out_busy) {
539 if (file->f_flags & O_NONBLOCK) {
540 retval = -EAGAIN;
541 goto unlock_exit;
543 retval = wait_event_interruptible(dev->write_wait, !dev->interrupt_out_busy);
544 if (retval < 0) {
545 goto unlock_exit;
549 /* write the data into interrupt_out_buffer from userspace */
550 bytes_to_write = min(count, write_buffer_size*dev->interrupt_out_endpoint_size);
551 if (bytes_to_write < count)
552 dev_warn(&dev->intf->dev, "Write buffer overflow, %zd bytes dropped\n",count-bytes_to_write);
553 dbg_info(&dev->intf->dev, "%s: count = %zd, bytes_to_write = %zd\n", __func__, count, bytes_to_write);
555 if (copy_from_user(dev->interrupt_out_buffer, buffer, bytes_to_write)) {
556 retval = -EFAULT;
557 goto unlock_exit;
560 if (dev->interrupt_out_endpoint == NULL) {
561 /* try HID_REQ_SET_REPORT=9 on control_endpoint instead of interrupt_out_endpoint */
562 retval = usb_control_msg(interface_to_usbdev(dev->intf),
563 usb_sndctrlpipe(interface_to_usbdev(dev->intf), 0),
565 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
566 1 << 8, 0,
567 dev->interrupt_out_buffer,
568 bytes_to_write,
569 USB_CTRL_SET_TIMEOUT * HZ);
570 if (retval < 0)
571 err("Couldn't submit HID_REQ_SET_REPORT %d\n", retval);
572 goto unlock_exit;
575 /* send off the urb */
576 usb_fill_int_urb(dev->interrupt_out_urb,
577 interface_to_usbdev(dev->intf),
578 usb_sndintpipe(interface_to_usbdev(dev->intf),
579 dev->interrupt_out_endpoint->bEndpointAddress),
580 dev->interrupt_out_buffer,
581 bytes_to_write,
582 ld_usb_interrupt_out_callback,
583 dev,
584 dev->interrupt_out_interval);
586 dev->interrupt_out_busy = 1;
587 wmb();
589 retval = usb_submit_urb(dev->interrupt_out_urb, GFP_KERNEL);
590 if (retval) {
591 dev->interrupt_out_busy = 0;
592 err("Couldn't submit interrupt_out_urb %d\n", retval);
593 goto unlock_exit;
595 retval = bytes_to_write;
597 unlock_exit:
598 /* unlock the device */
599 mutex_unlock(&dev->mutex);
601 exit:
602 return retval;
605 /* file operations needed when we register this driver */
606 static const struct file_operations ld_usb_fops = {
607 .owner = THIS_MODULE,
608 .read = ld_usb_read,
609 .write = ld_usb_write,
610 .open = ld_usb_open,
611 .release = ld_usb_release,
612 .poll = ld_usb_poll,
616 * usb class driver info in order to get a minor number from the usb core,
617 * and to have the device registered with the driver core
619 static struct usb_class_driver ld_usb_class = {
620 .name = "ldusb%d",
621 .fops = &ld_usb_fops,
622 .minor_base = USB_LD_MINOR_BASE,
626 * ld_usb_probe
628 * Called by the usb core when a new device is connected that it thinks
629 * this driver might be interested in.
631 static int ld_usb_probe(struct usb_interface *intf, const struct usb_device_id *id)
633 struct usb_device *udev = interface_to_usbdev(intf);
634 struct ld_usb *dev = NULL;
635 struct usb_host_interface *iface_desc;
636 struct usb_endpoint_descriptor *endpoint;
637 char *buffer;
638 int i;
639 int retval = -ENOMEM;
641 /* allocate memory for our device state and intialize it */
643 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
644 if (dev == NULL) {
645 dev_err(&intf->dev, "Out of memory\n");
646 goto exit;
648 mutex_init(&dev->mutex);
649 spin_lock_init(&dev->rbsl);
650 dev->intf = intf;
651 init_waitqueue_head(&dev->read_wait);
652 init_waitqueue_head(&dev->write_wait);
654 /* workaround for early firmware versions on fast computers */
655 if ((le16_to_cpu(udev->descriptor.idVendor) == USB_VENDOR_ID_LD) &&
656 ((le16_to_cpu(udev->descriptor.idProduct) == USB_DEVICE_ID_LD_CASSY) ||
657 (le16_to_cpu(udev->descriptor.idProduct) == USB_DEVICE_ID_LD_COM3LAB)) &&
658 (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x103)) {
659 buffer = kmalloc(256, GFP_KERNEL);
660 if (buffer == NULL) {
661 dev_err(&intf->dev, "Couldn't allocate string buffer\n");
662 goto error;
664 /* usb_string makes SETUP+STALL to leave always ControlReadLoop */
665 usb_string(udev, 255, buffer, 256);
666 kfree(buffer);
669 iface_desc = intf->cur_altsetting;
671 /* set up the endpoint information */
672 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
673 endpoint = &iface_desc->endpoint[i].desc;
675 if (usb_endpoint_is_int_in(endpoint))
676 dev->interrupt_in_endpoint = endpoint;
678 if (usb_endpoint_is_int_out(endpoint))
679 dev->interrupt_out_endpoint = endpoint;
681 if (dev->interrupt_in_endpoint == NULL) {
682 dev_err(&intf->dev, "Interrupt in endpoint not found\n");
683 goto error;
685 if (dev->interrupt_out_endpoint == NULL)
686 dev_warn(&intf->dev, "Interrupt out endpoint not found (using control endpoint instead)\n");
688 dev->interrupt_in_endpoint_size = le16_to_cpu(dev->interrupt_in_endpoint->wMaxPacketSize);
689 dev->ring_buffer = kmalloc(ring_buffer_size*(sizeof(size_t)+dev->interrupt_in_endpoint_size), GFP_KERNEL);
690 if (!dev->ring_buffer) {
691 dev_err(&intf->dev, "Couldn't allocate ring_buffer\n");
692 goto error;
694 dev->interrupt_in_buffer = kmalloc(dev->interrupt_in_endpoint_size, GFP_KERNEL);
695 if (!dev->interrupt_in_buffer) {
696 dev_err(&intf->dev, "Couldn't allocate interrupt_in_buffer\n");
697 goto error;
699 dev->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
700 if (!dev->interrupt_in_urb) {
701 dev_err(&intf->dev, "Couldn't allocate interrupt_in_urb\n");
702 goto error;
704 dev->interrupt_out_endpoint_size = dev->interrupt_out_endpoint ? le16_to_cpu(dev->interrupt_out_endpoint->wMaxPacketSize) :
705 udev->descriptor.bMaxPacketSize0;
706 dev->interrupt_out_buffer = kmalloc(write_buffer_size*dev->interrupt_out_endpoint_size, GFP_KERNEL);
707 if (!dev->interrupt_out_buffer) {
708 dev_err(&intf->dev, "Couldn't allocate interrupt_out_buffer\n");
709 goto error;
711 dev->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL);
712 if (!dev->interrupt_out_urb) {
713 dev_err(&intf->dev, "Couldn't allocate interrupt_out_urb\n");
714 goto error;
716 dev->interrupt_in_interval = min_interrupt_in_interval > dev->interrupt_in_endpoint->bInterval ? min_interrupt_in_interval : dev->interrupt_in_endpoint->bInterval;
717 if (dev->interrupt_out_endpoint)
718 dev->interrupt_out_interval = min_interrupt_out_interval > dev->interrupt_out_endpoint->bInterval ? min_interrupt_out_interval : dev->interrupt_out_endpoint->bInterval;
720 /* we can register the device now, as it is ready */
721 usb_set_intfdata(intf, dev);
723 retval = usb_register_dev(intf, &ld_usb_class);
724 if (retval) {
725 /* something prevented us from registering this driver */
726 dev_err(&intf->dev, "Not able to get a minor for this device.\n");
727 usb_set_intfdata(intf, NULL);
728 goto error;
731 /* let the user know what node this device is now attached to */
732 dev_info(&intf->dev, "LD USB Device #%d now attached to major %d minor %d\n",
733 (intf->minor - USB_LD_MINOR_BASE), USB_MAJOR, intf->minor);
735 exit:
736 return retval;
738 error:
739 ld_usb_delete(dev);
741 return retval;
745 * ld_usb_disconnect
747 * Called by the usb core when the device is removed from the system.
749 static void ld_usb_disconnect(struct usb_interface *intf)
751 struct ld_usb *dev;
752 int minor;
754 dev = usb_get_intfdata(intf);
755 usb_set_intfdata(intf, NULL);
757 minor = intf->minor;
759 /* give back our minor */
760 usb_deregister_dev(intf, &ld_usb_class);
762 mutex_lock(&dev->mutex);
764 /* if the device is not opened, then we clean up right now */
765 if (!dev->open_count) {
766 mutex_unlock(&dev->mutex);
767 ld_usb_delete(dev);
768 } else {
769 dev->intf = NULL;
770 mutex_unlock(&dev->mutex);
773 dev_info(&intf->dev, "LD USB Device #%d now disconnected\n",
774 (minor - USB_LD_MINOR_BASE));
777 /* usb specific object needed to register this driver with the usb subsystem */
778 static struct usb_driver ld_usb_driver = {
779 .name = "ldusb",
780 .probe = ld_usb_probe,
781 .disconnect = ld_usb_disconnect,
782 .id_table = ld_usb_table,
786 * ld_usb_init
788 static int __init ld_usb_init(void)
790 int retval;
792 /* register this driver with the USB subsystem */
793 retval = usb_register(&ld_usb_driver);
794 if (retval)
795 err("usb_register failed for the "__FILE__" driver. Error number %d\n", retval);
797 return retval;
801 * ld_usb_exit
803 static void __exit ld_usb_exit(void)
805 /* deregister this driver with the USB subsystem */
806 usb_deregister(&ld_usb_driver);
809 module_init(ld_usb_init);
810 module_exit(ld_usb_exit);