blob: 90f130126c10d605db2115ee6c16afe8329266cb [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>
Michael Hund2824bd22005-06-27 22:44:22 +020036
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 */
Michael Hundba3e66e2006-02-02 09:36:43 +010044#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 */
Michael Hund2824bd22005-06-27 22:44:22 +020058
59#define USB_VENDOR_ID_VERNIER 0x08f7
Michael Hund2824bd22005-06-27 22:44:22 +020060#define USB_DEVICE_ID_VERNIER_GOTEMP 0x0002
61#define USB_DEVICE_ID_VERNIER_SKIP 0x0003
62#define USB_DEVICE_ID_VERNIER_CYCLOPS 0x0004
Stephen Ware5b0a4d62008-02-17 11:01:58 -080063#define USB_DEVICE_ID_VERNIER_LCSPEC 0x0006
Michael Hund2824bd22005-06-27 22:44:22 +020064
Michael Hund2824bd22005-06-27 22:44:22 +020065#ifdef CONFIG_USB_DYNAMIC_MINORS
66#define USB_LD_MINOR_BASE 0
67#else
68#define USB_LD_MINOR_BASE 176
69#endif
70
71/* table of devices that work with this driver */
72static struct usb_device_id ld_usb_table [] = {
Michael Hundba3e66e2006-02-02 09:36:43 +010073 { 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) },
Michael Hund2824bd22005-06-27 22:44:22 +020087 { 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) },
Stephen Ware5b0a4d62008-02-17 11:01:58 -080090 { USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_LCSPEC) },
Michael Hund2824bd22005-06-27 22:44:22 +020091 { } /* Terminating entry */
92};
93MODULE_DEVICE_TABLE(usb, ld_usb_table);
Michael Hundba3e66e2006-02-02 09:36:43 +010094MODULE_VERSION("V0.13");
Michael Hund2824bd22005-06-27 22:44:22 +020095MODULE_AUTHOR("Michael Hund <mhund@ld-didactic.de>");
96MODULE_DESCRIPTION("LD USB Driver");
97MODULE_LICENSE("GPL");
98MODULE_SUPPORTED_DEVICE("LD USB Devices");
99
100#ifdef CONFIG_USB_DEBUG
101 static int debug = 1;
102#else
103 static int debug = 0;
104#endif
105
106/* Use our own dbg macro */
107#define dbg_info(dev, format, arg...) do { if (debug) dev_info(dev , format , ## arg); } while (0)
108
109/* Module parameters */
110module_param(debug, int, S_IRUGO | S_IWUSR);
111MODULE_PARM_DESC(debug, "Debug enabled or not");
112
113/* All interrupt in transfers are collected in a ring buffer to
114 * avoid racing conditions and get better performance of the driver.
115 */
116static int ring_buffer_size = 128;
117module_param(ring_buffer_size, int, 0);
118MODULE_PARM_DESC(ring_buffer_size, "Read ring buffer size in reports");
119
120/* The write_buffer can contain more than one interrupt out transfer.
121 */
122static int write_buffer_size = 10;
123module_param(write_buffer_size, int, 0);
124MODULE_PARM_DESC(write_buffer_size, "Write buffer size in reports");
125
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.
134 */
135static int min_interrupt_in_interval = 2;
136module_param(min_interrupt_in_interval, int, 0);
137MODULE_PARM_DESC(min_interrupt_in_interval, "Minimum interrupt in interval in ms");
138
139static int min_interrupt_out_interval = 2;
140module_param(min_interrupt_out_interval, int, 0);
141MODULE_PARM_DESC(min_interrupt_out_interval, "Minimum interrupt out interval in ms");
142
143/* Structure to hold all of our device specific stuff */
144struct ld_usb {
Daniel Walkerce0d7d32008-04-28 10:34:56 -0700145 struct mutex mutex; /* locks this structure */
Michael Hund2824bd22005-06-27 22:44:22 +0200146 struct usb_interface* intf; /* save off the usb interface pointer */
147
148 int open_count; /* number of times this port has been opened */
149
150 char* ring_buffer;
151 unsigned int ring_head;
152 unsigned int ring_tail;
153
154 wait_queue_head_t read_wait;
155 wait_queue_head_t write_wait;
156
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;
Oliver Neukum9d33efd2007-05-04 09:23:40 +0200164 int buffer_overflow;
165 spinlock_t rbsl;
Michael Hund2824bd22005-06-27 22:44:22 +0200166
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;
173};
174
Michael Hund2824bd22005-06-27 22:44:22 +0200175static struct usb_driver ld_usb_driver;
176
177/**
178 * ld_usb_abort_transfers
179 * aborts transfers and frees associated data structures
180 */
181static void ld_usb_abort_transfers(struct ld_usb *dev)
182{
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);
188 }
189 if (dev->interrupt_out_busy)
190 if (dev->intf)
191 usb_kill_urb(dev->interrupt_out_urb);
192}
193
194/**
195 * ld_usb_delete
196 */
197static void ld_usb_delete(struct ld_usb *dev)
198{
199 ld_usb_abort_transfers(dev);
200
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);
208}
209
210/**
211 * ld_usb_interrupt_in_callback
212 */
David Howells7d12e782006-10-05 14:55:46 +0100213static void ld_usb_interrupt_in_callback(struct urb *urb)
Michael Hund2824bd22005-06-27 22:44:22 +0200214{
215 struct ld_usb *dev = urb->context;
216 size_t *actual_buffer;
217 unsigned int next_ring_head;
Greg Kroah-Hartman491c0212007-07-18 10:58:02 -0700218 int status = urb->status;
Michael Hund2824bd22005-06-27 22:44:22 +0200219 int retval;
220
Greg Kroah-Hartman491c0212007-07-18 10:58:02 -0700221 if (status) {
222 if (status == -ENOENT ||
223 status == -ECONNRESET ||
224 status == -ESHUTDOWN) {
Michael Hund2824bd22005-06-27 22:44:22 +0200225 goto exit;
226 } else {
227 dbg_info(&dev->intf->dev, "%s: nonzero status received: %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800228 __func__, status);
Oliver Neukum9d33efd2007-05-04 09:23:40 +0200229 spin_lock(&dev->rbsl);
Michael Hund2824bd22005-06-27 22:44:22 +0200230 goto resubmit; /* maybe we can recover */
231 }
232 }
233
Oliver Neukum9d33efd2007-05-04 09:23:40 +0200234 spin_lock(&dev->rbsl);
Michael Hund2824bd22005-06-27 22:44:22 +0200235 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",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800244 __func__, urb->actual_length);
Oliver Neukum9d33efd2007-05-04 09:23:40 +0200245 } else {
Michael Hund2824bd22005-06-27 22:44:22 +0200246 dev_warn(&dev->intf->dev,
247 "Ring buffer overflow, %d bytes dropped\n",
248 urb->actual_length);
Oliver Neukum9d33efd2007-05-04 09:23:40 +0200249 dev->buffer_overflow = 1;
250 }
Michael Hund2824bd22005-06-27 22:44:22 +0200251 }
252
253resubmit:
254 /* resubmit if we're still running */
Oliver Neukum9d33efd2007-05-04 09:23:40 +0200255 if (dev->interrupt_in_running && !dev->buffer_overflow && dev->intf) {
Michael Hund2824bd22005-06-27 22:44:22 +0200256 retval = usb_submit_urb(dev->interrupt_in_urb, GFP_ATOMIC);
Oliver Neukum9d33efd2007-05-04 09:23:40 +0200257 if (retval) {
Michael Hund2824bd22005-06-27 22:44:22 +0200258 dev_err(&dev->intf->dev,
259 "usb_submit_urb failed (%d)\n", retval);
Oliver Neukum9d33efd2007-05-04 09:23:40 +0200260 dev->buffer_overflow = 1;
261 }
Michael Hund2824bd22005-06-27 22:44:22 +0200262 }
Oliver Neukum9d33efd2007-05-04 09:23:40 +0200263 spin_unlock(&dev->rbsl);
Michael Hund2824bd22005-06-27 22:44:22 +0200264exit:
265 dev->interrupt_in_done = 1;
266 wake_up_interruptible(&dev->read_wait);
267}
268
269/**
270 * ld_usb_interrupt_out_callback
271 */
David Howells7d12e782006-10-05 14:55:46 +0100272static void ld_usb_interrupt_out_callback(struct urb *urb)
Michael Hund2824bd22005-06-27 22:44:22 +0200273{
274 struct ld_usb *dev = urb->context;
Greg Kroah-Hartman491c0212007-07-18 10:58:02 -0700275 int status = urb->status;
Michael Hund2824bd22005-06-27 22:44:22 +0200276
277 /* sync/async unlink faults aren't errors */
Greg Kroah-Hartman491c0212007-07-18 10:58:02 -0700278 if (status && !(status == -ENOENT ||
279 status == -ECONNRESET ||
280 status == -ESHUTDOWN))
Michael Hund2824bd22005-06-27 22:44:22 +0200281 dbg_info(&dev->intf->dev,
282 "%s - nonzero write interrupt status received: %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800283 __func__, status);
Michael Hund2824bd22005-06-27 22:44:22 +0200284
285 dev->interrupt_out_busy = 0;
286 wake_up_interruptible(&dev->write_wait);
287}
288
289/**
290 * ld_usb_open
291 */
292static int ld_usb_open(struct inode *inode, struct file *file)
293{
294 struct ld_usb *dev;
295 int subminor;
Alan Sternd4ead162007-05-22 11:46:41 -0400296 int retval;
Michael Hund2824bd22005-06-27 22:44:22 +0200297 struct usb_interface *interface;
298
299 nonseekable_open(inode, file);
300 subminor = iminor(inode);
301
Michael Hund2824bd22005-06-27 22:44:22 +0200302 interface = usb_find_interface(&ld_usb_driver, subminor);
303
304 if (!interface) {
305 err("%s - error, can't find device for minor %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800306 __func__, subminor);
Alan Sternd4ead162007-05-22 11:46:41 -0400307 return -ENODEV;
Michael Hund2824bd22005-06-27 22:44:22 +0200308 }
309
310 dev = usb_get_intfdata(interface);
311
Alan Sternd4ead162007-05-22 11:46:41 -0400312 if (!dev)
313 return -ENODEV;
Michael Hund2824bd22005-06-27 22:44:22 +0200314
315 /* lock this device */
Daniel Walkerce0d7d32008-04-28 10:34:56 -0700316 if (mutex_lock_interruptible(&dev->mutex))
Alan Sternd4ead162007-05-22 11:46:41 -0400317 return -ERESTARTSYS;
Michael Hund2824bd22005-06-27 22:44:22 +0200318
319 /* allow opening only once */
320 if (dev->open_count) {
321 retval = -EBUSY;
322 goto unlock_exit;
323 }
324 dev->open_count = 1;
325
326 /* initialize in direction */
327 dev->ring_head = 0;
328 dev->ring_tail = 0;
Oliver Neukum9d33efd2007-05-04 09:23:40 +0200329 dev->buffer_overflow = 0;
Michael Hund2824bd22005-06-27 22:44:22 +0200330 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);
339
340 dev->interrupt_in_running = 1;
341 dev->interrupt_in_done = 0;
342
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;
349 }
350
351 /* save device in the file's private structure */
352 file->private_data = dev;
353
354unlock_exit:
Daniel Walkerce0d7d32008-04-28 10:34:56 -0700355 mutex_unlock(&dev->mutex);
Michael Hund2824bd22005-06-27 22:44:22 +0200356
Michael Hund2824bd22005-06-27 22:44:22 +0200357 return retval;
358}
359
360/**
361 * ld_usb_release
362 */
363static int ld_usb_release(struct inode *inode, struct file *file)
364{
365 struct ld_usb *dev;
366 int retval = 0;
367
368 dev = file->private_data;
369
370 if (dev == NULL) {
371 retval = -ENODEV;
372 goto exit;
373 }
374
Daniel Walkerce0d7d32008-04-28 10:34:56 -0700375 if (mutex_lock_interruptible(&dev->mutex)) {
Michael Hund2824bd22005-06-27 22:44:22 +0200376 retval = -ERESTARTSYS;
377 goto exit;
378 }
379
380 if (dev->open_count != 1) {
381 retval = -ENODEV;
382 goto unlock_exit;
383 }
384 if (dev->intf == NULL) {
385 /* the device was unplugged before the file was released */
Daniel Walkerce0d7d32008-04-28 10:34:56 -0700386 mutex_unlock(&dev->mutex);
Michael Hund2824bd22005-06-27 22:44:22 +0200387 /* unlock here as ld_usb_delete frees dev */
388 ld_usb_delete(dev);
389 goto exit;
390 }
391
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;
397
398unlock_exit:
Daniel Walkerce0d7d32008-04-28 10:34:56 -0700399 mutex_unlock(&dev->mutex);
Michael Hund2824bd22005-06-27 22:44:22 +0200400
401exit:
402 return retval;
403}
404
405/**
406 * ld_usb_poll
407 */
408static unsigned int ld_usb_poll(struct file *file, poll_table *wait)
409{
410 struct ld_usb *dev;
411 unsigned int mask = 0;
412
413 dev = file->private_data;
414
Oliver Neukumd12b85e2009-07-02 17:01:06 +0200415 if (!dev->intf)
416 return POLLERR | POLLHUP;
417
Michael Hund2824bd22005-06-27 22:44:22 +0200418 poll_wait(file, &dev->read_wait, wait);
419 poll_wait(file, &dev->write_wait, wait);
420
421 if (dev->ring_head != dev->ring_tail)
422 mask |= POLLIN | POLLRDNORM;
423 if (!dev->interrupt_out_busy)
424 mask |= POLLOUT | POLLWRNORM;
425
426 return mask;
427}
428
429/**
430 * ld_usb_read
431 */
432static ssize_t ld_usb_read(struct file *file, char __user *buffer, size_t count,
433 loff_t *ppos)
434{
435 struct ld_usb *dev;
436 size_t *actual_buffer;
437 size_t bytes_to_read;
438 int retval = 0;
Oliver Neukum9d33efd2007-05-04 09:23:40 +0200439 int rv;
Michael Hund2824bd22005-06-27 22:44:22 +0200440
441 dev = file->private_data;
442
443 /* verify that we actually have some data to read */
444 if (count == 0)
445 goto exit;
446
447 /* lock this object */
Daniel Walkerce0d7d32008-04-28 10:34:56 -0700448 if (mutex_lock_interruptible(&dev->mutex)) {
Michael Hund2824bd22005-06-27 22:44:22 +0200449 retval = -ERESTARTSYS;
450 goto exit;
451 }
452
453 /* verify that the device wasn't unplugged */
454 if (dev->intf == NULL) {
455 retval = -ENODEV;
456 err("No device or device unplugged %d\n", retval);
457 goto unlock_exit;
458 }
459
460 /* wait for data */
Oliver Neukum9d33efd2007-05-04 09:23:40 +0200461 spin_lock_irq(&dev->rbsl);
Michael Hund2824bd22005-06-27 22:44:22 +0200462 if (dev->ring_head == dev->ring_tail) {
Oliver Neukum9d33efd2007-05-04 09:23:40 +0200463 dev->interrupt_in_done = 0;
464 spin_unlock_irq(&dev->rbsl);
Michael Hund2824bd22005-06-27 22:44:22 +0200465 if (file->f_flags & O_NONBLOCK) {
466 retval = -EAGAIN;
467 goto unlock_exit;
468 }
469 retval = wait_event_interruptible(dev->read_wait, dev->interrupt_in_done);
470 if (retval < 0)
471 goto unlock_exit;
Oliver Neukum9d33efd2007-05-04 09:23:40 +0200472 } else {
473 spin_unlock_irq(&dev->rbsl);
Michael Hund2824bd22005-06-27 22:44:22 +0200474 }
475
476 /* actual_buffer contains actual_length + interrupt_in_buffer */
477 actual_buffer = (size_t*)(dev->ring_buffer + dev->ring_tail*(sizeof(size_t)+dev->interrupt_in_endpoint_size));
478 bytes_to_read = min(count, *actual_buffer);
479 if (bytes_to_read < *actual_buffer)
Alexey Dobriyandd7d5002005-08-14 17:24:26 +0400480 dev_warn(&dev->intf->dev, "Read buffer overflow, %zd bytes dropped\n",
Michael Hund2824bd22005-06-27 22:44:22 +0200481 *actual_buffer-bytes_to_read);
482
483 /* copy one interrupt_in_buffer from ring_buffer into userspace */
484 if (copy_to_user(buffer, actual_buffer+1, bytes_to_read)) {
485 retval = -EFAULT;
486 goto unlock_exit;
487 }
488 dev->ring_tail = (dev->ring_tail+1) % ring_buffer_size;
489
490 retval = bytes_to_read;
491
Oliver Neukum9d33efd2007-05-04 09:23:40 +0200492 spin_lock_irq(&dev->rbsl);
493 if (dev->buffer_overflow) {
494 dev->buffer_overflow = 0;
495 spin_unlock_irq(&dev->rbsl);
496 rv = usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL);
497 if (rv < 0)
498 dev->buffer_overflow = 1;
499 } else {
500 spin_unlock_irq(&dev->rbsl);
501 }
502
Michael Hund2824bd22005-06-27 22:44:22 +0200503unlock_exit:
504 /* unlock the device */
Daniel Walkerce0d7d32008-04-28 10:34:56 -0700505 mutex_unlock(&dev->mutex);
Michael Hund2824bd22005-06-27 22:44:22 +0200506
507exit:
508 return retval;
509}
510
511/**
512 * ld_usb_write
513 */
514static ssize_t ld_usb_write(struct file *file, const char __user *buffer,
515 size_t count, loff_t *ppos)
516{
517 struct ld_usb *dev;
518 size_t bytes_to_write;
519 int retval = 0;
520
521 dev = file->private_data;
522
523 /* verify that we actually have some data to write */
524 if (count == 0)
525 goto exit;
526
527 /* lock this object */
Daniel Walkerce0d7d32008-04-28 10:34:56 -0700528 if (mutex_lock_interruptible(&dev->mutex)) {
Michael Hund2824bd22005-06-27 22:44:22 +0200529 retval = -ERESTARTSYS;
530 goto exit;
531 }
532
533 /* verify that the device wasn't unplugged */
534 if (dev->intf == NULL) {
535 retval = -ENODEV;
536 err("No device or device unplugged %d\n", retval);
537 goto unlock_exit;
538 }
539
540 /* wait until previous transfer is finished */
541 if (dev->interrupt_out_busy) {
542 if (file->f_flags & O_NONBLOCK) {
543 retval = -EAGAIN;
544 goto unlock_exit;
545 }
546 retval = wait_event_interruptible(dev->write_wait, !dev->interrupt_out_busy);
547 if (retval < 0) {
548 goto unlock_exit;
549 }
550 }
551
552 /* write the data into interrupt_out_buffer from userspace */
553 bytes_to_write = min(count, write_buffer_size*dev->interrupt_out_endpoint_size);
554 if (bytes_to_write < count)
Alexey Dobriyandd7d5002005-08-14 17:24:26 +0400555 dev_warn(&dev->intf->dev, "Write buffer overflow, %zd bytes dropped\n",count-bytes_to_write);
Harvey Harrison441b62c2008-03-03 16:08:34 -0800556 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 +0200557
558 if (copy_from_user(dev->interrupt_out_buffer, buffer, bytes_to_write)) {
559 retval = -EFAULT;
560 goto unlock_exit;
561 }
562
563 if (dev->interrupt_out_endpoint == NULL) {
564 /* try HID_REQ_SET_REPORT=9 on control_endpoint instead of interrupt_out_endpoint */
565 retval = usb_control_msg(interface_to_usbdev(dev->intf),
566 usb_sndctrlpipe(interface_to_usbdev(dev->intf), 0),
567 9,
568 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
569 1 << 8, 0,
570 dev->interrupt_out_buffer,
571 bytes_to_write,
572 USB_CTRL_SET_TIMEOUT * HZ);
573 if (retval < 0)
574 err("Couldn't submit HID_REQ_SET_REPORT %d\n", retval);
575 goto unlock_exit;
576 }
577
578 /* send off the urb */
579 usb_fill_int_urb(dev->interrupt_out_urb,
580 interface_to_usbdev(dev->intf),
581 usb_sndintpipe(interface_to_usbdev(dev->intf),
582 dev->interrupt_out_endpoint->bEndpointAddress),
583 dev->interrupt_out_buffer,
584 bytes_to_write,
585 ld_usb_interrupt_out_callback,
586 dev,
587 dev->interrupt_out_interval);
588
589 dev->interrupt_out_busy = 1;
590 wmb();
591
592 retval = usb_submit_urb(dev->interrupt_out_urb, GFP_KERNEL);
593 if (retval) {
594 dev->interrupt_out_busy = 0;
595 err("Couldn't submit interrupt_out_urb %d\n", retval);
596 goto unlock_exit;
597 }
598 retval = bytes_to_write;
599
600unlock_exit:
601 /* unlock the device */
Daniel Walkerce0d7d32008-04-28 10:34:56 -0700602 mutex_unlock(&dev->mutex);
Michael Hund2824bd22005-06-27 22:44:22 +0200603
604exit:
605 return retval;
606}
607
608/* file operations needed when we register this driver */
Luiz Fernando N. Capitulino066202d2006-08-05 20:37:11 -0300609static const struct file_operations ld_usb_fops = {
Michael Hund2824bd22005-06-27 22:44:22 +0200610 .owner = THIS_MODULE,
611 .read = ld_usb_read,
612 .write = ld_usb_write,
613 .open = ld_usb_open,
614 .release = ld_usb_release,
615 .poll = ld_usb_poll,
616};
617
618/*
619 * usb class driver info in order to get a minor number from the usb core,
Greg Kroah-Hartman595b14c2006-01-18 17:36:58 -0500620 * and to have the device registered with the driver core
Michael Hund2824bd22005-06-27 22:44:22 +0200621 */
622static struct usb_class_driver ld_usb_class = {
623 .name = "ldusb%d",
624 .fops = &ld_usb_fops,
625 .minor_base = USB_LD_MINOR_BASE,
626};
627
628/**
629 * ld_usb_probe
630 *
631 * Called by the usb core when a new device is connected that it thinks
632 * this driver might be interested in.
633 */
634static int ld_usb_probe(struct usb_interface *intf, const struct usb_device_id *id)
635{
636 struct usb_device *udev = interface_to_usbdev(intf);
637 struct ld_usb *dev = NULL;
638 struct usb_host_interface *iface_desc;
639 struct usb_endpoint_descriptor *endpoint;
640 char *buffer;
641 int i;
642 int retval = -ENOMEM;
643
644 /* allocate memory for our device state and intialize it */
645
Oliver Neukum1144cf72006-01-06 22:40:02 +0100646 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Michael Hund2824bd22005-06-27 22:44:22 +0200647 if (dev == NULL) {
648 dev_err(&intf->dev, "Out of memory\n");
649 goto exit;
650 }
Daniel Walkerce0d7d32008-04-28 10:34:56 -0700651 mutex_init(&dev->mutex);
Oliver Neukum9d33efd2007-05-04 09:23:40 +0200652 spin_lock_init(&dev->rbsl);
Michael Hund2824bd22005-06-27 22:44:22 +0200653 dev->intf = intf;
654 init_waitqueue_head(&dev->read_wait);
655 init_waitqueue_head(&dev->write_wait);
656
657 /* workaround for early firmware versions on fast computers */
658 if ((le16_to_cpu(udev->descriptor.idVendor) == USB_VENDOR_ID_LD) &&
Michael Hundba3e66e2006-02-02 09:36:43 +0100659 ((le16_to_cpu(udev->descriptor.idProduct) == USB_DEVICE_ID_LD_CASSY) ||
660 (le16_to_cpu(udev->descriptor.idProduct) == USB_DEVICE_ID_LD_COM3LAB)) &&
Michael Hund2824bd22005-06-27 22:44:22 +0200661 (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x103)) {
662 buffer = kmalloc(256, GFP_KERNEL);
Michael Hunda6db5922005-07-29 12:17:20 -0700663 if (buffer == NULL) {
664 dev_err(&intf->dev, "Couldn't allocate string buffer\n");
665 goto error;
666 }
Michael Hund2824bd22005-06-27 22:44:22 +0200667 /* usb_string makes SETUP+STALL to leave always ControlReadLoop */
668 usb_string(udev, 255, buffer, 256);
669 kfree(buffer);
670 }
671
672 iface_desc = intf->cur_altsetting;
673
674 /* set up the endpoint information */
675 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
676 endpoint = &iface_desc->endpoint[i].desc;
677
Luiz Fernando N. Capitulino54826872006-09-27 11:58:54 -0700678 if (usb_endpoint_is_int_in(endpoint))
Michael Hund2824bd22005-06-27 22:44:22 +0200679 dev->interrupt_in_endpoint = endpoint;
Michael Hund2824bd22005-06-27 22:44:22 +0200680
Luiz Fernando N. Capitulino54826872006-09-27 11:58:54 -0700681 if (usb_endpoint_is_int_out(endpoint))
Michael Hund2824bd22005-06-27 22:44:22 +0200682 dev->interrupt_out_endpoint = endpoint;
Michael Hund2824bd22005-06-27 22:44:22 +0200683 }
684 if (dev->interrupt_in_endpoint == NULL) {
685 dev_err(&intf->dev, "Interrupt in endpoint not found\n");
686 goto error;
687 }
688 if (dev->interrupt_out_endpoint == NULL)
689 dev_warn(&intf->dev, "Interrupt out endpoint not found (using control endpoint instead)\n");
690
691 dev->interrupt_in_endpoint_size = le16_to_cpu(dev->interrupt_in_endpoint->wMaxPacketSize);
692 dev->ring_buffer = kmalloc(ring_buffer_size*(sizeof(size_t)+dev->interrupt_in_endpoint_size), GFP_KERNEL);
693 if (!dev->ring_buffer) {
694 dev_err(&intf->dev, "Couldn't allocate ring_buffer\n");
695 goto error;
696 }
697 dev->interrupt_in_buffer = kmalloc(dev->interrupt_in_endpoint_size, GFP_KERNEL);
698 if (!dev->interrupt_in_buffer) {
699 dev_err(&intf->dev, "Couldn't allocate interrupt_in_buffer\n");
700 goto error;
701 }
702 dev->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
703 if (!dev->interrupt_in_urb) {
704 dev_err(&intf->dev, "Couldn't allocate interrupt_in_urb\n");
705 goto error;
706 }
707 dev->interrupt_out_endpoint_size = dev->interrupt_out_endpoint ? le16_to_cpu(dev->interrupt_out_endpoint->wMaxPacketSize) :
708 udev->descriptor.bMaxPacketSize0;
709 dev->interrupt_out_buffer = kmalloc(write_buffer_size*dev->interrupt_out_endpoint_size, GFP_KERNEL);
710 if (!dev->interrupt_out_buffer) {
711 dev_err(&intf->dev, "Couldn't allocate interrupt_out_buffer\n");
712 goto error;
713 }
714 dev->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL);
715 if (!dev->interrupt_out_urb) {
716 dev_err(&intf->dev, "Couldn't allocate interrupt_out_urb\n");
717 goto error;
718 }
719 dev->interrupt_in_interval = min_interrupt_in_interval > dev->interrupt_in_endpoint->bInterval ? min_interrupt_in_interval : dev->interrupt_in_endpoint->bInterval;
720 if (dev->interrupt_out_endpoint)
721 dev->interrupt_out_interval = min_interrupt_out_interval > dev->interrupt_out_endpoint->bInterval ? min_interrupt_out_interval : dev->interrupt_out_endpoint->bInterval;
722
723 /* we can register the device now, as it is ready */
724 usb_set_intfdata(intf, dev);
725
726 retval = usb_register_dev(intf, &ld_usb_class);
727 if (retval) {
728 /* something prevented us from registering this driver */
729 dev_err(&intf->dev, "Not able to get a minor for this device.\n");
730 usb_set_intfdata(intf, NULL);
731 goto error;
732 }
733
734 /* let the user know what node this device is now attached to */
735 dev_info(&intf->dev, "LD USB Device #%d now attached to major %d minor %d\n",
736 (intf->minor - USB_LD_MINOR_BASE), USB_MAJOR, intf->minor);
737
738exit:
739 return retval;
740
741error:
742 ld_usb_delete(dev);
743
744 return retval;
745}
746
747/**
748 * ld_usb_disconnect
749 *
750 * Called by the usb core when the device is removed from the system.
751 */
752static void ld_usb_disconnect(struct usb_interface *intf)
753{
754 struct ld_usb *dev;
755 int minor;
756
Michael Hund2824bd22005-06-27 22:44:22 +0200757 dev = usb_get_intfdata(intf);
758 usb_set_intfdata(intf, NULL);
759
Michael Hund2824bd22005-06-27 22:44:22 +0200760 minor = intf->minor;
761
762 /* give back our minor */
763 usb_deregister_dev(intf, &ld_usb_class);
764
Daniel Walkerce0d7d32008-04-28 10:34:56 -0700765 mutex_lock(&dev->mutex);
Alan Sternd4ead162007-05-22 11:46:41 -0400766
Michael Hund2824bd22005-06-27 22:44:22 +0200767 /* if the device is not opened, then we clean up right now */
768 if (!dev->open_count) {
Daniel Walkerce0d7d32008-04-28 10:34:56 -0700769 mutex_unlock(&dev->mutex);
Michael Hund2824bd22005-06-27 22:44:22 +0200770 ld_usb_delete(dev);
771 } else {
772 dev->intf = NULL;
Oliver Neukumd12b85e2009-07-02 17:01:06 +0200773 /* wake up pollers */
774 wake_up_interruptible_all(&dev->read_wait);
775 wake_up_interruptible_all(&dev->write_wait);
Daniel Walkerce0d7d32008-04-28 10:34:56 -0700776 mutex_unlock(&dev->mutex);
Michael Hund2824bd22005-06-27 22:44:22 +0200777 }
778
Michael Hund2824bd22005-06-27 22:44:22 +0200779 dev_info(&intf->dev, "LD USB Device #%d now disconnected\n",
780 (minor - USB_LD_MINOR_BASE));
781}
782
783/* usb specific object needed to register this driver with the usb subsystem */
784static struct usb_driver ld_usb_driver = {
Michael Hund2824bd22005-06-27 22:44:22 +0200785 .name = "ldusb",
786 .probe = ld_usb_probe,
787 .disconnect = ld_usb_disconnect,
788 .id_table = ld_usb_table,
789};
790
791/**
792 * ld_usb_init
793 */
794static int __init ld_usb_init(void)
795{
796 int retval;
797
798 /* register this driver with the USB subsystem */
799 retval = usb_register(&ld_usb_driver);
800 if (retval)
801 err("usb_register failed for the "__FILE__" driver. Error number %d\n", retval);
802
803 return retval;
804}
805
806/**
807 * ld_usb_exit
808 */
809static void __exit ld_usb_exit(void)
810{
811 /* deregister this driver with the USB subsystem */
812 usb_deregister(&ld_usb_driver);
813}
814
815module_init(ld_usb_init);
816module_exit(ld_usb_exit);
817