Merge branch 'upstream-linus' of master.kernel.org:/pub/scm/linux/kernel/git/jgarzik...
[GitHub/MotorolaMobilityLLC/kernel-slsi.git] / drivers / usb / misc / ldusb.c
CommitLineData
2824bd25
MH
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)
a6db592e 26 * V0.12 (mh) Added kmalloc check for string buffer
ba3e66e9 27 * V0.13 (mh) Added support for LD X-Ray and Machine Test System
2824bd25
MH
28 */
29
2824bd25
MH
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>
4186ecf8 35#include <linux/mutex.h>
2824bd25
MH
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 */
ba3e66e9
MH
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 */
2824bd25
MH
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
79dcdbf6
JG
65#define USB_VENDOR_ID_MICROCHIP 0x04d8
66#define USB_DEVICE_ID_PICDEM 0x000c
2824bd25
MH
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 */
75static struct usb_device_id ld_usb_table [] = {
ba3e66e9
MH
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) },
2824bd25
MH
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) },
79dcdbf6 94 { USB_DEVICE(USB_VENDOR_ID_MICROCHIP, USB_DEVICE_ID_PICDEM) },
2824bd25
MH
95 { } /* Terminating entry */
96};
97MODULE_DEVICE_TABLE(usb, ld_usb_table);
ba3e66e9 98MODULE_VERSION("V0.13");
2824bd25
MH
99MODULE_AUTHOR("Michael Hund <mhund@ld-didactic.de>");
100MODULE_DESCRIPTION("LD USB Driver");
101MODULE_LICENSE("GPL");
102MODULE_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 */
114module_param(debug, int, S_IRUGO | S_IWUSR);
115MODULE_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 */
120static int ring_buffer_size = 128;
121module_param(ring_buffer_size, int, 0);
122MODULE_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 */
126static int write_buffer_size = 10;
127module_param(write_buffer_size, int, 0);
128MODULE_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 */
139static int min_interrupt_in_interval = 2;
140module_param(min_interrupt_in_interval, int, 0);
141MODULE_PARM_DESC(min_interrupt_in_interval, "Minimum interrupt in interval in ms");
142
143static int min_interrupt_out_interval = 2;
144module_param(min_interrupt_out_interval, int, 0);
145MODULE_PARM_DESC(min_interrupt_out_interval, "Minimum interrupt out interval in ms");
146
147/* Structure to hold all of our device specific stuff */
148struct 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;
9d33efd9
ON
168 int buffer_overflow;
169 spinlock_t rbsl;
2824bd25
MH
170
171 char* interrupt_out_buffer;
172 struct usb_endpoint_descriptor* interrupt_out_endpoint;
173 struct urb* interrupt_out_urb;
174 int interrupt_out_interval;
175 size_t interrupt_out_endpoint_size;
176 int interrupt_out_busy;
177};
178
2824bd25
MH
179static struct usb_driver ld_usb_driver;
180
181/**
182 * ld_usb_abort_transfers
183 * aborts transfers and frees associated data structures
184 */
185static void ld_usb_abort_transfers(struct ld_usb *dev)
186{
187 /* shutdown transfer */
188 if (dev->interrupt_in_running) {
189 dev->interrupt_in_running = 0;
190 if (dev->intf)
191 usb_kill_urb(dev->interrupt_in_urb);
192 }
193 if (dev->interrupt_out_busy)
194 if (dev->intf)
195 usb_kill_urb(dev->interrupt_out_urb);
196}
197
198/**
199 * ld_usb_delete
200 */
201static void ld_usb_delete(struct ld_usb *dev)
202{
203 ld_usb_abort_transfers(dev);
204
205 /* free data structures */
206 usb_free_urb(dev->interrupt_in_urb);
207 usb_free_urb(dev->interrupt_out_urb);
208 kfree(dev->ring_buffer);
209 kfree(dev->interrupt_in_buffer);
210 kfree(dev->interrupt_out_buffer);
211 kfree(dev);
212}
213
214/**
215 * ld_usb_interrupt_in_callback
216 */
7d12e780 217static void ld_usb_interrupt_in_callback(struct urb *urb)
2824bd25
MH
218{
219 struct ld_usb *dev = urb->context;
220 size_t *actual_buffer;
221 unsigned int next_ring_head;
491c021e 222 int status = urb->status;
2824bd25
MH
223 int retval;
224
491c021e
GKH
225 if (status) {
226 if (status == -ENOENT ||
227 status == -ECONNRESET ||
228 status == -ESHUTDOWN) {
2824bd25
MH
229 goto exit;
230 } else {
231 dbg_info(&dev->intf->dev, "%s: nonzero status received: %d\n",
491c021e 232 __FUNCTION__, status);
9d33efd9 233 spin_lock(&dev->rbsl);
2824bd25
MH
234 goto resubmit; /* maybe we can recover */
235 }
236 }
237
9d33efd9 238 spin_lock(&dev->rbsl);
2824bd25
MH
239 if (urb->actual_length > 0) {
240 next_ring_head = (dev->ring_head+1) % ring_buffer_size;
241 if (next_ring_head != dev->ring_tail) {
242 actual_buffer = (size_t*)(dev->ring_buffer + dev->ring_head*(sizeof(size_t)+dev->interrupt_in_endpoint_size));
243 /* actual_buffer gets urb->actual_length + interrupt_in_buffer */
244 *actual_buffer = urb->actual_length;
245 memcpy(actual_buffer+1, dev->interrupt_in_buffer, urb->actual_length);
246 dev->ring_head = next_ring_head;
247 dbg_info(&dev->intf->dev, "%s: received %d bytes\n",
248 __FUNCTION__, urb->actual_length);
9d33efd9 249 } else {
2824bd25
MH
250 dev_warn(&dev->intf->dev,
251 "Ring buffer overflow, %d bytes dropped\n",
252 urb->actual_length);
9d33efd9
ON
253 dev->buffer_overflow = 1;
254 }
2824bd25
MH
255 }
256
257resubmit:
258 /* resubmit if we're still running */
9d33efd9 259 if (dev->interrupt_in_running && !dev->buffer_overflow && dev->intf) {
2824bd25 260 retval = usb_submit_urb(dev->interrupt_in_urb, GFP_ATOMIC);
9d33efd9 261 if (retval) {
2824bd25
MH
262 dev_err(&dev->intf->dev,
263 "usb_submit_urb failed (%d)\n", retval);
9d33efd9
ON
264 dev->buffer_overflow = 1;
265 }
2824bd25 266 }
9d33efd9 267 spin_unlock(&dev->rbsl);
2824bd25
MH
268exit:
269 dev->interrupt_in_done = 1;
270 wake_up_interruptible(&dev->read_wait);
271}
272
273/**
274 * ld_usb_interrupt_out_callback
275 */
7d12e780 276static void ld_usb_interrupt_out_callback(struct urb *urb)
2824bd25
MH
277{
278 struct ld_usb *dev = urb->context;
491c021e 279 int status = urb->status;
2824bd25
MH
280
281 /* sync/async unlink faults aren't errors */
491c021e
GKH
282 if (status && !(status == -ENOENT ||
283 status == -ECONNRESET ||
284 status == -ESHUTDOWN))
2824bd25
MH
285 dbg_info(&dev->intf->dev,
286 "%s - nonzero write interrupt status received: %d\n",
491c021e 287 __FUNCTION__, status);
2824bd25
MH
288
289 dev->interrupt_out_busy = 0;
290 wake_up_interruptible(&dev->write_wait);
291}
292
293/**
294 * ld_usb_open
295 */
296static int ld_usb_open(struct inode *inode, struct file *file)
297{
298 struct ld_usb *dev;
299 int subminor;
d4ead16f 300 int retval;
2824bd25
MH
301 struct usb_interface *interface;
302
303 nonseekable_open(inode, file);
304 subminor = iminor(inode);
305
2824bd25
MH
306 interface = usb_find_interface(&ld_usb_driver, subminor);
307
308 if (!interface) {
309 err("%s - error, can't find device for minor %d\n",
310 __FUNCTION__, subminor);
d4ead16f 311 return -ENODEV;
2824bd25
MH
312 }
313
314 dev = usb_get_intfdata(interface);
315
d4ead16f
AS
316 if (!dev)
317 return -ENODEV;
2824bd25
MH
318
319 /* lock this device */
d4ead16f
AS
320 if (down_interruptible(&dev->sem))
321 return -ERESTARTSYS;
2824bd25
MH
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;
9d33efd9 333 dev->buffer_overflow = 0;
2824bd25
MH
334 usb_fill_int_urb(dev->interrupt_in_urb,
335 interface_to_usbdev(interface),
336 usb_rcvintpipe(interface_to_usbdev(interface),
337 dev->interrupt_in_endpoint->bEndpointAddress),
338 dev->interrupt_in_buffer,
339 dev->interrupt_in_endpoint_size,
340 ld_usb_interrupt_in_callback,
341 dev,
342 dev->interrupt_in_interval);
343
344 dev->interrupt_in_running = 1;
345 dev->interrupt_in_done = 0;
346
347 retval = usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL);
348 if (retval) {
349 dev_err(&interface->dev, "Couldn't submit interrupt_in_urb %d\n", retval);
350 dev->interrupt_in_running = 0;
351 dev->open_count = 0;
352 goto unlock_exit;
353 }
354
355 /* save device in the file's private structure */
356 file->private_data = dev;
357
358unlock_exit:
359 up(&dev->sem);
360
2824bd25
MH
361 return retval;
362}
363
364/**
365 * ld_usb_release
366 */
367static 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
402unlock_exit:
403 up(&dev->sem);
404
405exit:
406 return retval;
407}
408
409/**
410 * ld_usb_poll
411 */
412static 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 */
433static 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;
9d33efd9 440 int rv;
2824bd25
MH
441
442 dev = file->private_data;
443
444 /* verify that we actually have some data to read */
445 if (count == 0)
446 goto exit;
447
448 /* lock this object */
449 if (down_interruptible(&dev->sem)) {
450 retval = -ERESTARTSYS;
451 goto exit;
452 }
453
454 /* verify that the device wasn't unplugged */
455 if (dev->intf == NULL) {
456 retval = -ENODEV;
457 err("No device or device unplugged %d\n", retval);
458 goto unlock_exit;
459 }
460
461 /* wait for data */
9d33efd9 462 spin_lock_irq(&dev->rbsl);
2824bd25 463 if (dev->ring_head == dev->ring_tail) {
9d33efd9
ON
464 dev->interrupt_in_done = 0;
465 spin_unlock_irq(&dev->rbsl);
2824bd25
MH
466 if (file->f_flags & O_NONBLOCK) {
467 retval = -EAGAIN;
468 goto unlock_exit;
469 }
470 retval = wait_event_interruptible(dev->read_wait, dev->interrupt_in_done);
471 if (retval < 0)
472 goto unlock_exit;
9d33efd9
ON
473 } else {
474 spin_unlock_irq(&dev->rbsl);
2824bd25
MH
475 }
476
477 /* actual_buffer contains actual_length + interrupt_in_buffer */
478 actual_buffer = (size_t*)(dev->ring_buffer + dev->ring_tail*(sizeof(size_t)+dev->interrupt_in_endpoint_size));
479 bytes_to_read = min(count, *actual_buffer);
480 if (bytes_to_read < *actual_buffer)
dd7d5008 481 dev_warn(&dev->intf->dev, "Read buffer overflow, %zd bytes dropped\n",
2824bd25
MH
482 *actual_buffer-bytes_to_read);
483
484 /* copy one interrupt_in_buffer from ring_buffer into userspace */
485 if (copy_to_user(buffer, actual_buffer+1, bytes_to_read)) {
486 retval = -EFAULT;
487 goto unlock_exit;
488 }
489 dev->ring_tail = (dev->ring_tail+1) % ring_buffer_size;
490
491 retval = bytes_to_read;
492
9d33efd9
ON
493 spin_lock_irq(&dev->rbsl);
494 if (dev->buffer_overflow) {
495 dev->buffer_overflow = 0;
496 spin_unlock_irq(&dev->rbsl);
497 rv = usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL);
498 if (rv < 0)
499 dev->buffer_overflow = 1;
500 } else {
501 spin_unlock_irq(&dev->rbsl);
502 }
503
2824bd25
MH
504unlock_exit:
505 /* unlock the device */
506 up(&dev->sem);
507
508exit:
509 return retval;
510}
511
512/**
513 * ld_usb_write
514 */
515static ssize_t ld_usb_write(struct file *file, const char __user *buffer,
516 size_t count, loff_t *ppos)
517{
518 struct ld_usb *dev;
519 size_t bytes_to_write;
520 int retval = 0;
521
522 dev = file->private_data;
523
524 /* verify that we actually have some data to write */
525 if (count == 0)
526 goto exit;
527
528 /* lock this object */
529 if (down_interruptible(&dev->sem)) {
530 retval = -ERESTARTSYS;
531 goto exit;
532 }
533
534 /* verify that the device wasn't unplugged */
535 if (dev->intf == NULL) {
536 retval = -ENODEV;
537 err("No device or device unplugged %d\n", retval);
538 goto unlock_exit;
539 }
540
541 /* wait until previous transfer is finished */
542 if (dev->interrupt_out_busy) {
543 if (file->f_flags & O_NONBLOCK) {
544 retval = -EAGAIN;
545 goto unlock_exit;
546 }
547 retval = wait_event_interruptible(dev->write_wait, !dev->interrupt_out_busy);
548 if (retval < 0) {
549 goto unlock_exit;
550 }
551 }
552
553 /* write the data into interrupt_out_buffer from userspace */
554 bytes_to_write = min(count, write_buffer_size*dev->interrupt_out_endpoint_size);
555 if (bytes_to_write < count)
dd7d5008
AD
556 dev_warn(&dev->intf->dev, "Write buffer overflow, %zd bytes dropped\n",count-bytes_to_write);
557 dbg_info(&dev->intf->dev, "%s: count = %zd, bytes_to_write = %zd\n", __FUNCTION__, count, bytes_to_write);
2824bd25
MH
558
559 if (copy_from_user(dev->interrupt_out_buffer, buffer, bytes_to_write)) {
560 retval = -EFAULT;
561 goto unlock_exit;
562 }
563
564 if (dev->interrupt_out_endpoint == NULL) {
565 /* try HID_REQ_SET_REPORT=9 on control_endpoint instead of interrupt_out_endpoint */
566 retval = usb_control_msg(interface_to_usbdev(dev->intf),
567 usb_sndctrlpipe(interface_to_usbdev(dev->intf), 0),
568 9,
569 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
570 1 << 8, 0,
571 dev->interrupt_out_buffer,
572 bytes_to_write,
573 USB_CTRL_SET_TIMEOUT * HZ);
574 if (retval < 0)
575 err("Couldn't submit HID_REQ_SET_REPORT %d\n", retval);
576 goto unlock_exit;
577 }
578
579 /* send off the urb */
580 usb_fill_int_urb(dev->interrupt_out_urb,
581 interface_to_usbdev(dev->intf),
582 usb_sndintpipe(interface_to_usbdev(dev->intf),
583 dev->interrupt_out_endpoint->bEndpointAddress),
584 dev->interrupt_out_buffer,
585 bytes_to_write,
586 ld_usb_interrupt_out_callback,
587 dev,
588 dev->interrupt_out_interval);
589
590 dev->interrupt_out_busy = 1;
591 wmb();
592
593 retval = usb_submit_urb(dev->interrupt_out_urb, GFP_KERNEL);
594 if (retval) {
595 dev->interrupt_out_busy = 0;
596 err("Couldn't submit interrupt_out_urb %d\n", retval);
597 goto unlock_exit;
598 }
599 retval = bytes_to_write;
600
601unlock_exit:
602 /* unlock the device */
603 up(&dev->sem);
604
605exit:
606 return retval;
607}
608
609/* file operations needed when we register this driver */
066202dd 610static const struct file_operations ld_usb_fops = {
2824bd25
MH
611 .owner = THIS_MODULE,
612 .read = ld_usb_read,
613 .write = ld_usb_write,
614 .open = ld_usb_open,
615 .release = ld_usb_release,
616 .poll = ld_usb_poll,
617};
618
619/*
620 * usb class driver info in order to get a minor number from the usb core,
595b14cb 621 * and to have the device registered with the driver core
2824bd25
MH
622 */
623static struct usb_class_driver ld_usb_class = {
624 .name = "ldusb%d",
625 .fops = &ld_usb_fops,
626 .minor_base = USB_LD_MINOR_BASE,
627};
628
629/**
630 * ld_usb_probe
631 *
632 * Called by the usb core when a new device is connected that it thinks
633 * this driver might be interested in.
634 */
635static int ld_usb_probe(struct usb_interface *intf, const struct usb_device_id *id)
636{
637 struct usb_device *udev = interface_to_usbdev(intf);
638 struct ld_usb *dev = NULL;
639 struct usb_host_interface *iface_desc;
640 struct usb_endpoint_descriptor *endpoint;
641 char *buffer;
642 int i;
643 int retval = -ENOMEM;
644
645 /* allocate memory for our device state and intialize it */
646
1144cf7a 647 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2824bd25
MH
648 if (dev == NULL) {
649 dev_err(&intf->dev, "Out of memory\n");
650 goto exit;
651 }
2824bd25 652 init_MUTEX(&dev->sem);
9d33efd9 653 spin_lock_init(&dev->rbsl);
2824bd25
MH
654 dev->intf = intf;
655 init_waitqueue_head(&dev->read_wait);
656 init_waitqueue_head(&dev->write_wait);
657
658 /* workaround for early firmware versions on fast computers */
659 if ((le16_to_cpu(udev->descriptor.idVendor) == USB_VENDOR_ID_LD) &&
ba3e66e9
MH
660 ((le16_to_cpu(udev->descriptor.idProduct) == USB_DEVICE_ID_LD_CASSY) ||
661 (le16_to_cpu(udev->descriptor.idProduct) == USB_DEVICE_ID_LD_COM3LAB)) &&
2824bd25
MH
662 (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x103)) {
663 buffer = kmalloc(256, GFP_KERNEL);
a6db592e
MH
664 if (buffer == NULL) {
665 dev_err(&intf->dev, "Couldn't allocate string buffer\n");
666 goto error;
667 }
2824bd25
MH
668 /* usb_string makes SETUP+STALL to leave always ControlReadLoop */
669 usb_string(udev, 255, buffer, 256);
670 kfree(buffer);
671 }
672
673 iface_desc = intf->cur_altsetting;
674
675 /* set up the endpoint information */
676 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
677 endpoint = &iface_desc->endpoint[i].desc;
678
5482687b 679 if (usb_endpoint_is_int_in(endpoint))
2824bd25 680 dev->interrupt_in_endpoint = endpoint;
2824bd25 681
5482687b 682 if (usb_endpoint_is_int_out(endpoint))
2824bd25 683 dev->interrupt_out_endpoint = endpoint;
2824bd25
MH
684 }
685 if (dev->interrupt_in_endpoint == NULL) {
686 dev_err(&intf->dev, "Interrupt in endpoint not found\n");
687 goto error;
688 }
689 if (dev->interrupt_out_endpoint == NULL)
690 dev_warn(&intf->dev, "Interrupt out endpoint not found (using control endpoint instead)\n");
691
692 dev->interrupt_in_endpoint_size = le16_to_cpu(dev->interrupt_in_endpoint->wMaxPacketSize);
693 dev->ring_buffer = kmalloc(ring_buffer_size*(sizeof(size_t)+dev->interrupt_in_endpoint_size), GFP_KERNEL);
694 if (!dev->ring_buffer) {
695 dev_err(&intf->dev, "Couldn't allocate ring_buffer\n");
696 goto error;
697 }
698 dev->interrupt_in_buffer = kmalloc(dev->interrupt_in_endpoint_size, GFP_KERNEL);
699 if (!dev->interrupt_in_buffer) {
700 dev_err(&intf->dev, "Couldn't allocate interrupt_in_buffer\n");
701 goto error;
702 }
703 dev->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
704 if (!dev->interrupt_in_urb) {
705 dev_err(&intf->dev, "Couldn't allocate interrupt_in_urb\n");
706 goto error;
707 }
708 dev->interrupt_out_endpoint_size = dev->interrupt_out_endpoint ? le16_to_cpu(dev->interrupt_out_endpoint->wMaxPacketSize) :
709 udev->descriptor.bMaxPacketSize0;
710 dev->interrupt_out_buffer = kmalloc(write_buffer_size*dev->interrupt_out_endpoint_size, GFP_KERNEL);
711 if (!dev->interrupt_out_buffer) {
712 dev_err(&intf->dev, "Couldn't allocate interrupt_out_buffer\n");
713 goto error;
714 }
715 dev->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL);
716 if (!dev->interrupt_out_urb) {
717 dev_err(&intf->dev, "Couldn't allocate interrupt_out_urb\n");
718 goto error;
719 }
720 dev->interrupt_in_interval = min_interrupt_in_interval > dev->interrupt_in_endpoint->bInterval ? min_interrupt_in_interval : dev->interrupt_in_endpoint->bInterval;
721 if (dev->interrupt_out_endpoint)
722 dev->interrupt_out_interval = min_interrupt_out_interval > dev->interrupt_out_endpoint->bInterval ? min_interrupt_out_interval : dev->interrupt_out_endpoint->bInterval;
723
724 /* we can register the device now, as it is ready */
725 usb_set_intfdata(intf, dev);
726
727 retval = usb_register_dev(intf, &ld_usb_class);
728 if (retval) {
729 /* something prevented us from registering this driver */
730 dev_err(&intf->dev, "Not able to get a minor for this device.\n");
731 usb_set_intfdata(intf, NULL);
732 goto error;
733 }
734
735 /* let the user know what node this device is now attached to */
736 dev_info(&intf->dev, "LD USB Device #%d now attached to major %d minor %d\n",
737 (intf->minor - USB_LD_MINOR_BASE), USB_MAJOR, intf->minor);
738
739exit:
740 return retval;
741
742error:
743 ld_usb_delete(dev);
744
745 return retval;
746}
747
748/**
749 * ld_usb_disconnect
750 *
751 * Called by the usb core when the device is removed from the system.
752 */
753static void ld_usb_disconnect(struct usb_interface *intf)
754{
755 struct ld_usb *dev;
756 int minor;
757
2824bd25
MH
758 dev = usb_get_intfdata(intf);
759 usb_set_intfdata(intf, NULL);
760
2824bd25
MH
761 minor = intf->minor;
762
763 /* give back our minor */
764 usb_deregister_dev(intf, &ld_usb_class);
765
d4ead16f
AS
766 down(&dev->sem);
767
2824bd25
MH
768 /* if the device is not opened, then we clean up right now */
769 if (!dev->open_count) {
770 up(&dev->sem);
771 ld_usb_delete(dev);
772 } else {
773 dev->intf = NULL;
774 up(&dev->sem);
775 }
776
2824bd25
MH
777 dev_info(&intf->dev, "LD USB Device #%d now disconnected\n",
778 (minor - USB_LD_MINOR_BASE));
779}
780
781/* usb specific object needed to register this driver with the usb subsystem */
782static struct usb_driver ld_usb_driver = {
2824bd25
MH
783 .name = "ldusb",
784 .probe = ld_usb_probe,
785 .disconnect = ld_usb_disconnect,
786 .id_table = ld_usb_table,
787};
788
789/**
790 * ld_usb_init
791 */
792static int __init ld_usb_init(void)
793{
794 int retval;
795
796 /* register this driver with the USB subsystem */
797 retval = usb_register(&ld_usb_driver);
798 if (retval)
799 err("usb_register failed for the "__FILE__" driver. Error number %d\n", retval);
800
801 return retval;
802}
803
804/**
805 * ld_usb_exit
806 */
807static void __exit ld_usb_exit(void)
808{
809 /* deregister this driver with the USB subsystem */
810 usb_deregister(&ld_usb_driver);
811}
812
813module_init(ld_usb_init);
814module_exit(ld_usb_exit);
815