blob: 8de32df5978a0756f7b63135e4b67f793259f4b4 [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 Hundba3e66e2006-02-02 09:36:43 +010027 * V0.13 (mh) Added support for LD X-Ray and Machine Test System
Michael Hund2824bd22005-06-27 22:44:22 +020028 */
29
Michael Hund2824bd22005-06-27 22:44:22 +020030#include <linux/kernel.h>
31#include <linux/errno.h>
32#include <linux/init.h>
33#include <linux/slab.h>
34#include <linux/module.h>
Arjan van de Ven4186ecf2006-01-11 15:55:29 +010035#include <linux/mutex.h>
Oliver Neukum86266452010-01-13 15:33:15 +010036#include <linux/smp_lock.h>
Michael Hund2824bd22005-06-27 22:44:22 +020037
38#include <asm/uaccess.h>
39#include <linux/input.h>
40#include <linux/usb.h>
41#include <linux/poll.h>
42
43/* Define these values to match your devices */
44#define USB_VENDOR_ID_LD 0x0f11 /* USB Vendor ID of LD Didactic GmbH */
Michael Hundba3e66e2006-02-02 09:36:43 +010045#define USB_DEVICE_ID_LD_CASSY 0x1000 /* USB Product ID of CASSY-S */
46#define USB_DEVICE_ID_LD_POCKETCASSY 0x1010 /* USB Product ID of Pocket-CASSY */
47#define USB_DEVICE_ID_LD_MOBILECASSY 0x1020 /* USB Product ID of Mobile-CASSY */
48#define USB_DEVICE_ID_LD_JWM 0x1080 /* USB Product ID of Joule and Wattmeter */
49#define USB_DEVICE_ID_LD_DMMP 0x1081 /* USB Product ID of Digital Multimeter P (reserved) */
50#define USB_DEVICE_ID_LD_UMIP 0x1090 /* USB Product ID of UMI P */
51#define USB_DEVICE_ID_LD_XRAY1 0x1100 /* USB Product ID of X-Ray Apparatus */
52#define USB_DEVICE_ID_LD_XRAY2 0x1101 /* USB Product ID of X-Ray Apparatus */
53#define USB_DEVICE_ID_LD_VIDEOCOM 0x1200 /* USB Product ID of VideoCom */
54#define USB_DEVICE_ID_LD_COM3LAB 0x2000 /* USB Product ID of COM3LAB */
55#define USB_DEVICE_ID_LD_TELEPORT 0x2010 /* USB Product ID of Terminal Adapter */
56#define USB_DEVICE_ID_LD_NETWORKANALYSER 0x2020 /* USB Product ID of Network Analyser */
57#define USB_DEVICE_ID_LD_POWERCONTROL 0x2030 /* USB Product ID of Converter Control Unit */
58#define USB_DEVICE_ID_LD_MACHINETEST 0x2040 /* USB Product ID of Machine Test System */
Michael Hund2824bd22005-06-27 22:44:22 +020059
60#define USB_VENDOR_ID_VERNIER 0x08f7
Michael Hund2824bd22005-06-27 22:44:22 +020061#define USB_DEVICE_ID_VERNIER_GOTEMP 0x0002
62#define USB_DEVICE_ID_VERNIER_SKIP 0x0003
63#define USB_DEVICE_ID_VERNIER_CYCLOPS 0x0004
Stephen Ware5b0a4d62008-02-17 11:01:58 -080064#define USB_DEVICE_ID_VERNIER_LCSPEC 0x0006
Michael Hund2824bd22005-06-27 22:44:22 +020065
Michael Hund2824bd22005-06-27 22:44:22 +020066#ifdef CONFIG_USB_DYNAMIC_MINORS
67#define USB_LD_MINOR_BASE 0
68#else
69#define USB_LD_MINOR_BASE 176
70#endif
71
72/* table of devices that work with this driver */
Németh Márton33b9e162010-01-10 15:34:45 +010073static const struct usb_device_id ld_usb_table[] = {
Michael Hundba3e66e2006-02-02 09:36:43 +010074 { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_CASSY) },
75 { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_POCKETCASSY) },
76 { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MOBILECASSY) },
77 { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_JWM) },
78 { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_DMMP) },
79 { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_UMIP) },
80 { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_XRAY1) },
81 { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_XRAY2) },
82 { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_VIDEOCOM) },
83 { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_COM3LAB) },
84 { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_TELEPORT) },
85 { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_NETWORKANALYSER) },
86 { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_POWERCONTROL) },
87 { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MACHINETEST) },
Michael Hund2824bd22005-06-27 22:44:22 +020088 { USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_GOTEMP) },
89 { USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_SKIP) },
90 { USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_CYCLOPS) },
Stephen Ware5b0a4d62008-02-17 11:01:58 -080091 { USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_LCSPEC) },
Michael Hund2824bd22005-06-27 22:44:22 +020092 { } /* Terminating entry */
93};
94MODULE_DEVICE_TABLE(usb, ld_usb_table);
Michael Hundba3e66e2006-02-02 09:36:43 +010095MODULE_VERSION("V0.13");
Michael Hund2824bd22005-06-27 22:44:22 +020096MODULE_AUTHOR("Michael Hund <mhund@ld-didactic.de>");
97MODULE_DESCRIPTION("LD USB Driver");
98MODULE_LICENSE("GPL");
99MODULE_SUPPORTED_DEVICE("LD USB Devices");
100
101#ifdef CONFIG_USB_DEBUG
102 static int debug = 1;
103#else
104 static int debug = 0;
105#endif
106
107/* Use our own dbg macro */
108#define dbg_info(dev, format, arg...) do { if (debug) dev_info(dev , format , ## arg); } while (0)
109
110/* Module parameters */
111module_param(debug, int, S_IRUGO | S_IWUSR);
112MODULE_PARM_DESC(debug, "Debug enabled or not");
113
114/* All interrupt in transfers are collected in a ring buffer to
115 * avoid racing conditions and get better performance of the driver.
116 */
117static int ring_buffer_size = 128;
118module_param(ring_buffer_size, int, 0);
119MODULE_PARM_DESC(ring_buffer_size, "Read ring buffer size in reports");
120
121/* The write_buffer can contain more than one interrupt out transfer.
122 */
123static int write_buffer_size = 10;
124module_param(write_buffer_size, int, 0);
125MODULE_PARM_DESC(write_buffer_size, "Write buffer size in reports");
126
127/* As of kernel version 2.6.4 ehci-hcd uses an
128 * "only one interrupt transfer per frame" shortcut
129 * to simplify the scheduling of periodic transfers.
130 * This conflicts with our standard 1ms intervals for in and out URBs.
131 * We use default intervals of 2ms for in and 2ms for out transfers,
132 * which should be fast enough.
133 * Increase the interval to allow more devices that do interrupt transfers,
134 * or set to 1 to use the standard interval from the endpoint descriptors.
135 */
136static int min_interrupt_in_interval = 2;
137module_param(min_interrupt_in_interval, int, 0);
138MODULE_PARM_DESC(min_interrupt_in_interval, "Minimum interrupt in interval in ms");
139
140static int min_interrupt_out_interval = 2;
141module_param(min_interrupt_out_interval, int, 0);
142MODULE_PARM_DESC(min_interrupt_out_interval, "Minimum interrupt out interval in ms");
143
144/* Structure to hold all of our device specific stuff */
145struct ld_usb {
Daniel Walkerce0d7d32008-04-28 10:34:56 -0700146 struct mutex mutex; /* locks this structure */
Michael Hund2824bd22005-06-27 22:44:22 +0200147 struct usb_interface* intf; /* save off the usb interface pointer */
148
149 int open_count; /* number of times this port has been opened */
150
151 char* ring_buffer;
152 unsigned int ring_head;
153 unsigned int ring_tail;
154
155 wait_queue_head_t read_wait;
156 wait_queue_head_t write_wait;
157
158 char* interrupt_in_buffer;
159 struct usb_endpoint_descriptor* interrupt_in_endpoint;
160 struct urb* interrupt_in_urb;
161 int interrupt_in_interval;
162 size_t interrupt_in_endpoint_size;
163 int interrupt_in_running;
164 int interrupt_in_done;
Oliver Neukum9d33efd2007-05-04 09:23:40 +0200165 int buffer_overflow;
166 spinlock_t rbsl;
Michael Hund2824bd22005-06-27 22:44:22 +0200167
168 char* interrupt_out_buffer;
169 struct usb_endpoint_descriptor* interrupt_out_endpoint;
170 struct urb* interrupt_out_urb;
171 int interrupt_out_interval;
172 size_t interrupt_out_endpoint_size;
173 int interrupt_out_busy;
174};
175
Michael Hund2824bd22005-06-27 22:44:22 +0200176static struct usb_driver ld_usb_driver;
177
178/**
179 * ld_usb_abort_transfers
180 * aborts transfers and frees associated data structures
181 */
182static void ld_usb_abort_transfers(struct ld_usb *dev)
183{
184 /* shutdown transfer */
185 if (dev->interrupt_in_running) {
186 dev->interrupt_in_running = 0;
187 if (dev->intf)
188 usb_kill_urb(dev->interrupt_in_urb);
189 }
190 if (dev->interrupt_out_busy)
191 if (dev->intf)
192 usb_kill_urb(dev->interrupt_out_urb);
193}
194
195/**
196 * ld_usb_delete
197 */
198static void ld_usb_delete(struct ld_usb *dev)
199{
200 ld_usb_abort_transfers(dev);
201
202 /* free data structures */
203 usb_free_urb(dev->interrupt_in_urb);
204 usb_free_urb(dev->interrupt_out_urb);
205 kfree(dev->ring_buffer);
206 kfree(dev->interrupt_in_buffer);
207 kfree(dev->interrupt_out_buffer);
208 kfree(dev);
209}
210
211/**
212 * ld_usb_interrupt_in_callback
213 */
David Howells7d12e782006-10-05 14:55:46 +0100214static void ld_usb_interrupt_in_callback(struct urb *urb)
Michael Hund2824bd22005-06-27 22:44:22 +0200215{
216 struct ld_usb *dev = urb->context;
217 size_t *actual_buffer;
218 unsigned int next_ring_head;
Greg Kroah-Hartman491c0212007-07-18 10:58:02 -0700219 int status = urb->status;
Michael Hund2824bd22005-06-27 22:44:22 +0200220 int retval;
221
Greg Kroah-Hartman491c0212007-07-18 10:58:02 -0700222 if (status) {
223 if (status == -ENOENT ||
224 status == -ECONNRESET ||
225 status == -ESHUTDOWN) {
Michael Hund2824bd22005-06-27 22:44:22 +0200226 goto exit;
227 } else {
228 dbg_info(&dev->intf->dev, "%s: nonzero status received: %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800229 __func__, status);
Oliver Neukum9d33efd2007-05-04 09:23:40 +0200230 spin_lock(&dev->rbsl);
Michael Hund2824bd22005-06-27 22:44:22 +0200231 goto resubmit; /* maybe we can recover */
232 }
233 }
234
Oliver Neukum9d33efd2007-05-04 09:23:40 +0200235 spin_lock(&dev->rbsl);
Michael Hund2824bd22005-06-27 22:44:22 +0200236 if (urb->actual_length > 0) {
237 next_ring_head = (dev->ring_head+1) % ring_buffer_size;
238 if (next_ring_head != dev->ring_tail) {
239 actual_buffer = (size_t*)(dev->ring_buffer + dev->ring_head*(sizeof(size_t)+dev->interrupt_in_endpoint_size));
240 /* actual_buffer gets urb->actual_length + interrupt_in_buffer */
241 *actual_buffer = urb->actual_length;
242 memcpy(actual_buffer+1, dev->interrupt_in_buffer, urb->actual_length);
243 dev->ring_head = next_ring_head;
244 dbg_info(&dev->intf->dev, "%s: received %d bytes\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800245 __func__, urb->actual_length);
Oliver Neukum9d33efd2007-05-04 09:23:40 +0200246 } else {
Michael Hund2824bd22005-06-27 22:44:22 +0200247 dev_warn(&dev->intf->dev,
248 "Ring buffer overflow, %d bytes dropped\n",
249 urb->actual_length);
Oliver Neukum9d33efd2007-05-04 09:23:40 +0200250 dev->buffer_overflow = 1;
251 }
Michael Hund2824bd22005-06-27 22:44:22 +0200252 }
253
254resubmit:
255 /* resubmit if we're still running */
Oliver Neukum9d33efd2007-05-04 09:23:40 +0200256 if (dev->interrupt_in_running && !dev->buffer_overflow && dev->intf) {
Michael Hund2824bd22005-06-27 22:44:22 +0200257 retval = usb_submit_urb(dev->interrupt_in_urb, GFP_ATOMIC);
Oliver Neukum9d33efd2007-05-04 09:23:40 +0200258 if (retval) {
Michael Hund2824bd22005-06-27 22:44:22 +0200259 dev_err(&dev->intf->dev,
260 "usb_submit_urb failed (%d)\n", retval);
Oliver Neukum9d33efd2007-05-04 09:23:40 +0200261 dev->buffer_overflow = 1;
262 }
Michael Hund2824bd22005-06-27 22:44:22 +0200263 }
Oliver Neukum9d33efd2007-05-04 09:23:40 +0200264 spin_unlock(&dev->rbsl);
Michael Hund2824bd22005-06-27 22:44:22 +0200265exit:
266 dev->interrupt_in_done = 1;
267 wake_up_interruptible(&dev->read_wait);
268}
269
270/**
271 * ld_usb_interrupt_out_callback
272 */
David Howells7d12e782006-10-05 14:55:46 +0100273static void ld_usb_interrupt_out_callback(struct urb *urb)
Michael Hund2824bd22005-06-27 22:44:22 +0200274{
275 struct ld_usb *dev = urb->context;
Greg Kroah-Hartman491c0212007-07-18 10:58:02 -0700276 int status = urb->status;
Michael Hund2824bd22005-06-27 22:44:22 +0200277
278 /* sync/async unlink faults aren't errors */
Greg Kroah-Hartman491c0212007-07-18 10:58:02 -0700279 if (status && !(status == -ENOENT ||
280 status == -ECONNRESET ||
281 status == -ESHUTDOWN))
Michael Hund2824bd22005-06-27 22:44:22 +0200282 dbg_info(&dev->intf->dev,
283 "%s - nonzero write interrupt status received: %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800284 __func__, status);
Michael Hund2824bd22005-06-27 22:44:22 +0200285
286 dev->interrupt_out_busy = 0;
287 wake_up_interruptible(&dev->write_wait);
288}
289
290/**
291 * ld_usb_open
292 */
293static int ld_usb_open(struct inode *inode, struct file *file)
294{
295 struct ld_usb *dev;
296 int subminor;
Alan Sternd4ead162007-05-22 11:46:41 -0400297 int retval;
Michael Hund2824bd22005-06-27 22:44:22 +0200298 struct usb_interface *interface;
299
Oliver Neukum86266452010-01-13 15:33:15 +0100300 lock_kernel();
Michael Hund2824bd22005-06-27 22:44:22 +0200301 nonseekable_open(inode, file);
302 subminor = iminor(inode);
303
Michael Hund2824bd22005-06-27 22:44:22 +0200304 interface = usb_find_interface(&ld_usb_driver, subminor);
305
306 if (!interface) {
Oliver Neukum86266452010-01-13 15:33:15 +0100307 unlock_kernel();
Michael Hund2824bd22005-06-27 22:44:22 +0200308 err("%s - error, can't find device for minor %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800309 __func__, subminor);
Alan Sternd4ead162007-05-22 11:46:41 -0400310 return -ENODEV;
Michael Hund2824bd22005-06-27 22:44:22 +0200311 }
312
313 dev = usb_get_intfdata(interface);
314
Oliver Neukum86266452010-01-13 15:33:15 +0100315 if (!dev) {
316 unlock_kernel();
Alan Sternd4ead162007-05-22 11:46:41 -0400317 return -ENODEV;
Oliver Neukum86266452010-01-13 15:33:15 +0100318 }
Michael Hund2824bd22005-06-27 22:44:22 +0200319
320 /* lock this device */
Oliver Neukum86266452010-01-13 15:33:15 +0100321 if (mutex_lock_interruptible(&dev->mutex)) {
322 unlock_kernel();
Alan Sternd4ead162007-05-22 11:46:41 -0400323 return -ERESTARTSYS;
Oliver Neukum86266452010-01-13 15:33:15 +0100324 }
Michael Hund2824bd22005-06-27 22:44:22 +0200325
326 /* allow opening only once */
327 if (dev->open_count) {
328 retval = -EBUSY;
329 goto unlock_exit;
330 }
331 dev->open_count = 1;
332
333 /* initialize in direction */
334 dev->ring_head = 0;
335 dev->ring_tail = 0;
Oliver Neukum9d33efd2007-05-04 09:23:40 +0200336 dev->buffer_overflow = 0;
Michael Hund2824bd22005-06-27 22:44:22 +0200337 usb_fill_int_urb(dev->interrupt_in_urb,
338 interface_to_usbdev(interface),
339 usb_rcvintpipe(interface_to_usbdev(interface),
340 dev->interrupt_in_endpoint->bEndpointAddress),
341 dev->interrupt_in_buffer,
342 dev->interrupt_in_endpoint_size,
343 ld_usb_interrupt_in_callback,
344 dev,
345 dev->interrupt_in_interval);
346
347 dev->interrupt_in_running = 1;
348 dev->interrupt_in_done = 0;
349
350 retval = usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL);
351 if (retval) {
352 dev_err(&interface->dev, "Couldn't submit interrupt_in_urb %d\n", retval);
353 dev->interrupt_in_running = 0;
354 dev->open_count = 0;
355 goto unlock_exit;
356 }
357
358 /* save device in the file's private structure */
359 file->private_data = dev;
360
361unlock_exit:
Daniel Walkerce0d7d32008-04-28 10:34:56 -0700362 mutex_unlock(&dev->mutex);
Oliver Neukum86266452010-01-13 15:33:15 +0100363 unlock_kernel();
Michael Hund2824bd22005-06-27 22:44:22 +0200364
Michael Hund2824bd22005-06-27 22:44:22 +0200365 return retval;
366}
367
368/**
369 * ld_usb_release
370 */
371static int ld_usb_release(struct inode *inode, struct file *file)
372{
373 struct ld_usb *dev;
374 int retval = 0;
375
376 dev = file->private_data;
377
378 if (dev == NULL) {
379 retval = -ENODEV;
380 goto exit;
381 }
382
Daniel Walkerce0d7d32008-04-28 10:34:56 -0700383 if (mutex_lock_interruptible(&dev->mutex)) {
Michael Hund2824bd22005-06-27 22:44:22 +0200384 retval = -ERESTARTSYS;
385 goto exit;
386 }
387
388 if (dev->open_count != 1) {
389 retval = -ENODEV;
390 goto unlock_exit;
391 }
392 if (dev->intf == NULL) {
393 /* the device was unplugged before the file was released */
Daniel Walkerce0d7d32008-04-28 10:34:56 -0700394 mutex_unlock(&dev->mutex);
Michael Hund2824bd22005-06-27 22:44:22 +0200395 /* unlock here as ld_usb_delete frees dev */
396 ld_usb_delete(dev);
397 goto exit;
398 }
399
400 /* wait until write transfer is finished */
401 if (dev->interrupt_out_busy)
402 wait_event_interruptible_timeout(dev->write_wait, !dev->interrupt_out_busy, 2 * HZ);
403 ld_usb_abort_transfers(dev);
404 dev->open_count = 0;
405
406unlock_exit:
Daniel Walkerce0d7d32008-04-28 10:34:56 -0700407 mutex_unlock(&dev->mutex);
Michael Hund2824bd22005-06-27 22:44:22 +0200408
409exit:
410 return retval;
411}
412
413/**
414 * ld_usb_poll
415 */
416static unsigned int ld_usb_poll(struct file *file, poll_table *wait)
417{
418 struct ld_usb *dev;
419 unsigned int mask = 0;
420
421 dev = file->private_data;
422
Oliver Neukumd12b85e2009-07-02 17:01:06 +0200423 if (!dev->intf)
424 return POLLERR | POLLHUP;
425
Michael Hund2824bd22005-06-27 22:44:22 +0200426 poll_wait(file, &dev->read_wait, wait);
427 poll_wait(file, &dev->write_wait, wait);
428
429 if (dev->ring_head != dev->ring_tail)
430 mask |= POLLIN | POLLRDNORM;
431 if (!dev->interrupt_out_busy)
432 mask |= POLLOUT | POLLWRNORM;
433
434 return mask;
435}
436
437/**
438 * ld_usb_read
439 */
440static ssize_t ld_usb_read(struct file *file, char __user *buffer, size_t count,
441 loff_t *ppos)
442{
443 struct ld_usb *dev;
444 size_t *actual_buffer;
445 size_t bytes_to_read;
446 int retval = 0;
Oliver Neukum9d33efd2007-05-04 09:23:40 +0200447 int rv;
Michael Hund2824bd22005-06-27 22:44:22 +0200448
449 dev = file->private_data;
450
451 /* verify that we actually have some data to read */
452 if (count == 0)
453 goto exit;
454
455 /* lock this object */
Daniel Walkerce0d7d32008-04-28 10:34:56 -0700456 if (mutex_lock_interruptible(&dev->mutex)) {
Michael Hund2824bd22005-06-27 22:44:22 +0200457 retval = -ERESTARTSYS;
458 goto exit;
459 }
460
461 /* verify that the device wasn't unplugged */
462 if (dev->intf == NULL) {
463 retval = -ENODEV;
464 err("No device or device unplugged %d\n", retval);
465 goto unlock_exit;
466 }
467
468 /* wait for data */
Oliver Neukum9d33efd2007-05-04 09:23:40 +0200469 spin_lock_irq(&dev->rbsl);
Michael Hund2824bd22005-06-27 22:44:22 +0200470 if (dev->ring_head == dev->ring_tail) {
Oliver Neukum9d33efd2007-05-04 09:23:40 +0200471 dev->interrupt_in_done = 0;
472 spin_unlock_irq(&dev->rbsl);
Michael Hund2824bd22005-06-27 22:44:22 +0200473 if (file->f_flags & O_NONBLOCK) {
474 retval = -EAGAIN;
475 goto unlock_exit;
476 }
477 retval = wait_event_interruptible(dev->read_wait, dev->interrupt_in_done);
478 if (retval < 0)
479 goto unlock_exit;
Oliver Neukum9d33efd2007-05-04 09:23:40 +0200480 } else {
481 spin_unlock_irq(&dev->rbsl);
Michael Hund2824bd22005-06-27 22:44:22 +0200482 }
483
484 /* actual_buffer contains actual_length + interrupt_in_buffer */
485 actual_buffer = (size_t*)(dev->ring_buffer + dev->ring_tail*(sizeof(size_t)+dev->interrupt_in_endpoint_size));
486 bytes_to_read = min(count, *actual_buffer);
487 if (bytes_to_read < *actual_buffer)
Alexey Dobriyandd7d5002005-08-14 17:24:26 +0400488 dev_warn(&dev->intf->dev, "Read buffer overflow, %zd bytes dropped\n",
Michael Hund2824bd22005-06-27 22:44:22 +0200489 *actual_buffer-bytes_to_read);
490
491 /* copy one interrupt_in_buffer from ring_buffer into userspace */
492 if (copy_to_user(buffer, actual_buffer+1, bytes_to_read)) {
493 retval = -EFAULT;
494 goto unlock_exit;
495 }
496 dev->ring_tail = (dev->ring_tail+1) % ring_buffer_size;
497
498 retval = bytes_to_read;
499
Oliver Neukum9d33efd2007-05-04 09:23:40 +0200500 spin_lock_irq(&dev->rbsl);
501 if (dev->buffer_overflow) {
502 dev->buffer_overflow = 0;
503 spin_unlock_irq(&dev->rbsl);
504 rv = usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL);
505 if (rv < 0)
506 dev->buffer_overflow = 1;
507 } else {
508 spin_unlock_irq(&dev->rbsl);
509 }
510
Michael Hund2824bd22005-06-27 22:44:22 +0200511unlock_exit:
512 /* unlock the device */
Daniel Walkerce0d7d32008-04-28 10:34:56 -0700513 mutex_unlock(&dev->mutex);
Michael Hund2824bd22005-06-27 22:44:22 +0200514
515exit:
516 return retval;
517}
518
519/**
520 * ld_usb_write
521 */
522static ssize_t ld_usb_write(struct file *file, const char __user *buffer,
523 size_t count, loff_t *ppos)
524{
525 struct ld_usb *dev;
526 size_t bytes_to_write;
527 int retval = 0;
528
529 dev = file->private_data;
530
531 /* verify that we actually have some data to write */
532 if (count == 0)
533 goto exit;
534
535 /* lock this object */
Daniel Walkerce0d7d32008-04-28 10:34:56 -0700536 if (mutex_lock_interruptible(&dev->mutex)) {
Michael Hund2824bd22005-06-27 22:44:22 +0200537 retval = -ERESTARTSYS;
538 goto exit;
539 }
540
541 /* verify that the device wasn't unplugged */
542 if (dev->intf == NULL) {
543 retval = -ENODEV;
544 err("No device or device unplugged %d\n", retval);
545 goto unlock_exit;
546 }
547
548 /* wait until previous transfer is finished */
549 if (dev->interrupt_out_busy) {
550 if (file->f_flags & O_NONBLOCK) {
551 retval = -EAGAIN;
552 goto unlock_exit;
553 }
554 retval = wait_event_interruptible(dev->write_wait, !dev->interrupt_out_busy);
555 if (retval < 0) {
556 goto unlock_exit;
557 }
558 }
559
560 /* write the data into interrupt_out_buffer from userspace */
561 bytes_to_write = min(count, write_buffer_size*dev->interrupt_out_endpoint_size);
562 if (bytes_to_write < count)
Alexey Dobriyandd7d5002005-08-14 17:24:26 +0400563 dev_warn(&dev->intf->dev, "Write buffer overflow, %zd bytes dropped\n",count-bytes_to_write);
Harvey Harrison441b62c2008-03-03 16:08:34 -0800564 dbg_info(&dev->intf->dev, "%s: count = %zd, bytes_to_write = %zd\n", __func__, count, bytes_to_write);
Michael Hund2824bd22005-06-27 22:44:22 +0200565
566 if (copy_from_user(dev->interrupt_out_buffer, buffer, bytes_to_write)) {
567 retval = -EFAULT;
568 goto unlock_exit;
569 }
570
571 if (dev->interrupt_out_endpoint == NULL) {
572 /* try HID_REQ_SET_REPORT=9 on control_endpoint instead of interrupt_out_endpoint */
573 retval = usb_control_msg(interface_to_usbdev(dev->intf),
574 usb_sndctrlpipe(interface_to_usbdev(dev->intf), 0),
575 9,
576 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
577 1 << 8, 0,
578 dev->interrupt_out_buffer,
579 bytes_to_write,
580 USB_CTRL_SET_TIMEOUT * HZ);
581 if (retval < 0)
582 err("Couldn't submit HID_REQ_SET_REPORT %d\n", retval);
583 goto unlock_exit;
584 }
585
586 /* send off the urb */
587 usb_fill_int_urb(dev->interrupt_out_urb,
588 interface_to_usbdev(dev->intf),
589 usb_sndintpipe(interface_to_usbdev(dev->intf),
590 dev->interrupt_out_endpoint->bEndpointAddress),
591 dev->interrupt_out_buffer,
592 bytes_to_write,
593 ld_usb_interrupt_out_callback,
594 dev,
595 dev->interrupt_out_interval);
596
597 dev->interrupt_out_busy = 1;
598 wmb();
599
600 retval = usb_submit_urb(dev->interrupt_out_urb, GFP_KERNEL);
601 if (retval) {
602 dev->interrupt_out_busy = 0;
603 err("Couldn't submit interrupt_out_urb %d\n", retval);
604 goto unlock_exit;
605 }
606 retval = bytes_to_write;
607
608unlock_exit:
609 /* unlock the device */
Daniel Walkerce0d7d32008-04-28 10:34:56 -0700610 mutex_unlock(&dev->mutex);
Michael Hund2824bd22005-06-27 22:44:22 +0200611
612exit:
613 return retval;
614}
615
616/* file operations needed when we register this driver */
Luiz Fernando N. Capitulino066202d2006-08-05 20:37:11 -0300617static const struct file_operations ld_usb_fops = {
Michael Hund2824bd22005-06-27 22:44:22 +0200618 .owner = THIS_MODULE,
619 .read = ld_usb_read,
620 .write = ld_usb_write,
621 .open = ld_usb_open,
622 .release = ld_usb_release,
623 .poll = ld_usb_poll,
624};
625
626/*
627 * usb class driver info in order to get a minor number from the usb core,
Greg Kroah-Hartman595b14c2006-01-18 17:36:58 -0500628 * and to have the device registered with the driver core
Michael Hund2824bd22005-06-27 22:44:22 +0200629 */
630static struct usb_class_driver ld_usb_class = {
631 .name = "ldusb%d",
632 .fops = &ld_usb_fops,
633 .minor_base = USB_LD_MINOR_BASE,
634};
635
636/**
637 * ld_usb_probe
638 *
639 * Called by the usb core when a new device is connected that it thinks
640 * this driver might be interested in.
641 */
642static int ld_usb_probe(struct usb_interface *intf, const struct usb_device_id *id)
643{
644 struct usb_device *udev = interface_to_usbdev(intf);
645 struct ld_usb *dev = NULL;
646 struct usb_host_interface *iface_desc;
647 struct usb_endpoint_descriptor *endpoint;
648 char *buffer;
649 int i;
650 int retval = -ENOMEM;
651
652 /* allocate memory for our device state and intialize it */
653
Oliver Neukum1144cf72006-01-06 22:40:02 +0100654 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Michael Hund2824bd22005-06-27 22:44:22 +0200655 if (dev == NULL) {
656 dev_err(&intf->dev, "Out of memory\n");
657 goto exit;
658 }
Daniel Walkerce0d7d32008-04-28 10:34:56 -0700659 mutex_init(&dev->mutex);
Oliver Neukum9d33efd2007-05-04 09:23:40 +0200660 spin_lock_init(&dev->rbsl);
Michael Hund2824bd22005-06-27 22:44:22 +0200661 dev->intf = intf;
662 init_waitqueue_head(&dev->read_wait);
663 init_waitqueue_head(&dev->write_wait);
664
665 /* workaround for early firmware versions on fast computers */
666 if ((le16_to_cpu(udev->descriptor.idVendor) == USB_VENDOR_ID_LD) &&
Michael Hundba3e66e2006-02-02 09:36:43 +0100667 ((le16_to_cpu(udev->descriptor.idProduct) == USB_DEVICE_ID_LD_CASSY) ||
668 (le16_to_cpu(udev->descriptor.idProduct) == USB_DEVICE_ID_LD_COM3LAB)) &&
Michael Hund2824bd22005-06-27 22:44:22 +0200669 (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x103)) {
670 buffer = kmalloc(256, GFP_KERNEL);
Michael Hunda6db5922005-07-29 12:17:20 -0700671 if (buffer == NULL) {
672 dev_err(&intf->dev, "Couldn't allocate string buffer\n");
673 goto error;
674 }
Michael Hund2824bd22005-06-27 22:44:22 +0200675 /* usb_string makes SETUP+STALL to leave always ControlReadLoop */
676 usb_string(udev, 255, buffer, 256);
677 kfree(buffer);
678 }
679
680 iface_desc = intf->cur_altsetting;
681
682 /* set up the endpoint information */
683 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
684 endpoint = &iface_desc->endpoint[i].desc;
685
Luiz Fernando N. Capitulino54826872006-09-27 11:58:54 -0700686 if (usb_endpoint_is_int_in(endpoint))
Michael Hund2824bd22005-06-27 22:44:22 +0200687 dev->interrupt_in_endpoint = endpoint;
Michael Hund2824bd22005-06-27 22:44:22 +0200688
Luiz Fernando N. Capitulino54826872006-09-27 11:58:54 -0700689 if (usb_endpoint_is_int_out(endpoint))
Michael Hund2824bd22005-06-27 22:44:22 +0200690 dev->interrupt_out_endpoint = endpoint;
Michael Hund2824bd22005-06-27 22:44:22 +0200691 }
692 if (dev->interrupt_in_endpoint == NULL) {
693 dev_err(&intf->dev, "Interrupt in endpoint not found\n");
694 goto error;
695 }
696 if (dev->interrupt_out_endpoint == NULL)
697 dev_warn(&intf->dev, "Interrupt out endpoint not found (using control endpoint instead)\n");
698
699 dev->interrupt_in_endpoint_size = le16_to_cpu(dev->interrupt_in_endpoint->wMaxPacketSize);
700 dev->ring_buffer = kmalloc(ring_buffer_size*(sizeof(size_t)+dev->interrupt_in_endpoint_size), GFP_KERNEL);
701 if (!dev->ring_buffer) {
702 dev_err(&intf->dev, "Couldn't allocate ring_buffer\n");
703 goto error;
704 }
705 dev->interrupt_in_buffer = kmalloc(dev->interrupt_in_endpoint_size, GFP_KERNEL);
706 if (!dev->interrupt_in_buffer) {
707 dev_err(&intf->dev, "Couldn't allocate interrupt_in_buffer\n");
708 goto error;
709 }
710 dev->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
711 if (!dev->interrupt_in_urb) {
712 dev_err(&intf->dev, "Couldn't allocate interrupt_in_urb\n");
713 goto error;
714 }
715 dev->interrupt_out_endpoint_size = dev->interrupt_out_endpoint ? le16_to_cpu(dev->interrupt_out_endpoint->wMaxPacketSize) :
716 udev->descriptor.bMaxPacketSize0;
717 dev->interrupt_out_buffer = kmalloc(write_buffer_size*dev->interrupt_out_endpoint_size, GFP_KERNEL);
718 if (!dev->interrupt_out_buffer) {
719 dev_err(&intf->dev, "Couldn't allocate interrupt_out_buffer\n");
720 goto error;
721 }
722 dev->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL);
723 if (!dev->interrupt_out_urb) {
724 dev_err(&intf->dev, "Couldn't allocate interrupt_out_urb\n");
725 goto error;
726 }
727 dev->interrupt_in_interval = min_interrupt_in_interval > dev->interrupt_in_endpoint->bInterval ? min_interrupt_in_interval : dev->interrupt_in_endpoint->bInterval;
728 if (dev->interrupt_out_endpoint)
729 dev->interrupt_out_interval = min_interrupt_out_interval > dev->interrupt_out_endpoint->bInterval ? min_interrupt_out_interval : dev->interrupt_out_endpoint->bInterval;
730
731 /* we can register the device now, as it is ready */
732 usb_set_intfdata(intf, dev);
733
734 retval = usb_register_dev(intf, &ld_usb_class);
735 if (retval) {
736 /* something prevented us from registering this driver */
737 dev_err(&intf->dev, "Not able to get a minor for this device.\n");
738 usb_set_intfdata(intf, NULL);
739 goto error;
740 }
741
742 /* let the user know what node this device is now attached to */
743 dev_info(&intf->dev, "LD USB Device #%d now attached to major %d minor %d\n",
744 (intf->minor - USB_LD_MINOR_BASE), USB_MAJOR, intf->minor);
745
746exit:
747 return retval;
748
749error:
750 ld_usb_delete(dev);
751
752 return retval;
753}
754
755/**
756 * ld_usb_disconnect
757 *
758 * Called by the usb core when the device is removed from the system.
759 */
760static void ld_usb_disconnect(struct usb_interface *intf)
761{
762 struct ld_usb *dev;
763 int minor;
764
Michael Hund2824bd22005-06-27 22:44:22 +0200765 dev = usb_get_intfdata(intf);
766 usb_set_intfdata(intf, NULL);
767
Michael Hund2824bd22005-06-27 22:44:22 +0200768 minor = intf->minor;
769
770 /* give back our minor */
771 usb_deregister_dev(intf, &ld_usb_class);
772
Daniel Walkerce0d7d32008-04-28 10:34:56 -0700773 mutex_lock(&dev->mutex);
Alan Sternd4ead162007-05-22 11:46:41 -0400774
Michael Hund2824bd22005-06-27 22:44:22 +0200775 /* if the device is not opened, then we clean up right now */
776 if (!dev->open_count) {
Daniel Walkerce0d7d32008-04-28 10:34:56 -0700777 mutex_unlock(&dev->mutex);
Michael Hund2824bd22005-06-27 22:44:22 +0200778 ld_usb_delete(dev);
779 } else {
780 dev->intf = NULL;
Oliver Neukumd12b85e2009-07-02 17:01:06 +0200781 /* wake up pollers */
782 wake_up_interruptible_all(&dev->read_wait);
783 wake_up_interruptible_all(&dev->write_wait);
Daniel Walkerce0d7d32008-04-28 10:34:56 -0700784 mutex_unlock(&dev->mutex);
Michael Hund2824bd22005-06-27 22:44:22 +0200785 }
786
Michael Hund2824bd22005-06-27 22:44:22 +0200787 dev_info(&intf->dev, "LD USB Device #%d now disconnected\n",
788 (minor - USB_LD_MINOR_BASE));
789}
790
791/* usb specific object needed to register this driver with the usb subsystem */
792static struct usb_driver ld_usb_driver = {
Michael Hund2824bd22005-06-27 22:44:22 +0200793 .name = "ldusb",
794 .probe = ld_usb_probe,
795 .disconnect = ld_usb_disconnect,
796 .id_table = ld_usb_table,
797};
798
799/**
800 * ld_usb_init
801 */
802static int __init ld_usb_init(void)
803{
804 int retval;
805
806 /* register this driver with the USB subsystem */
807 retval = usb_register(&ld_usb_driver);
808 if (retval)
809 err("usb_register failed for the "__FILE__" driver. Error number %d\n", retval);
810
811 return retval;
812}
813
814/**
815 * ld_usb_exit
816 */
817static void __exit ld_usb_exit(void)
818{
819 /* deregister this driver with the USB subsystem */
820 usb_deregister(&ld_usb_driver);
821}
822
823module_init(ld_usb_init);
824module_exit(ld_usb_exit);
825