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