blob: 11580e81e2c633b0163214680342334105b64dc8 [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
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
Stephen Ware5b0a4d62008-02-17 11:01:58 -080064#define USB_DEVICE_ID_VERNIER_LCSPEC 0x0006
Michael Hund2824bd22005-06-27 22:44:22 +020065
Joey Goncalves79dcdbf2007-04-20 11:05:54 -070066#define USB_VENDOR_ID_MICROCHIP 0x04d8
67#define USB_DEVICE_ID_PICDEM 0x000c
Michael Hund2824bd22005-06-27 22:44:22 +020068
69#ifdef CONFIG_USB_DYNAMIC_MINORS
70#define USB_LD_MINOR_BASE 0
71#else
72#define USB_LD_MINOR_BASE 176
73#endif
74
75/* table of devices that work with this driver */
76static struct usb_device_id ld_usb_table [] = {
Michael Hundba3e66e2006-02-02 09:36:43 +010077 { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_CASSY) },
78 { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_POCKETCASSY) },
79 { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MOBILECASSY) },
80 { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_JWM) },
81 { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_DMMP) },
82 { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_UMIP) },
83 { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_XRAY1) },
84 { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_XRAY2) },
85 { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_VIDEOCOM) },
86 { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_COM3LAB) },
87 { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_TELEPORT) },
88 { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_NETWORKANALYSER) },
89 { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_POWERCONTROL) },
90 { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MACHINETEST) },
Michael Hund2824bd22005-06-27 22:44:22 +020091 { USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_LABPRO) },
92 { USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_GOTEMP) },
93 { USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_SKIP) },
94 { USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_CYCLOPS) },
Joey Goncalves79dcdbf2007-04-20 11:05:54 -070095 { USB_DEVICE(USB_VENDOR_ID_MICROCHIP, USB_DEVICE_ID_PICDEM) },
Stephen Ware5b0a4d62008-02-17 11:01:58 -080096 { USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_LCSPEC) },
Michael Hund2824bd22005-06-27 22:44:22 +020097 { } /* Terminating entry */
98};
99MODULE_DEVICE_TABLE(usb, ld_usb_table);
Michael Hundba3e66e2006-02-02 09:36:43 +0100100MODULE_VERSION("V0.13");
Michael Hund2824bd22005-06-27 22:44:22 +0200101MODULE_AUTHOR("Michael Hund <mhund@ld-didactic.de>");
102MODULE_DESCRIPTION("LD USB Driver");
103MODULE_LICENSE("GPL");
104MODULE_SUPPORTED_DEVICE("LD USB Devices");
105
106#ifdef CONFIG_USB_DEBUG
107 static int debug = 1;
108#else
109 static int debug = 0;
110#endif
111
112/* Use our own dbg macro */
113#define dbg_info(dev, format, arg...) do { if (debug) dev_info(dev , format , ## arg); } while (0)
114
115/* Module parameters */
116module_param(debug, int, S_IRUGO | S_IWUSR);
117MODULE_PARM_DESC(debug, "Debug enabled or not");
118
119/* All interrupt in transfers are collected in a ring buffer to
120 * avoid racing conditions and get better performance of the driver.
121 */
122static int ring_buffer_size = 128;
123module_param(ring_buffer_size, int, 0);
124MODULE_PARM_DESC(ring_buffer_size, "Read ring buffer size in reports");
125
126/* The write_buffer can contain more than one interrupt out transfer.
127 */
128static int write_buffer_size = 10;
129module_param(write_buffer_size, int, 0);
130MODULE_PARM_DESC(write_buffer_size, "Write buffer size in reports");
131
132/* As of kernel version 2.6.4 ehci-hcd uses an
133 * "only one interrupt transfer per frame" shortcut
134 * to simplify the scheduling of periodic transfers.
135 * This conflicts with our standard 1ms intervals for in and out URBs.
136 * We use default intervals of 2ms for in and 2ms for out transfers,
137 * which should be fast enough.
138 * Increase the interval to allow more devices that do interrupt transfers,
139 * or set to 1 to use the standard interval from the endpoint descriptors.
140 */
141static int min_interrupt_in_interval = 2;
142module_param(min_interrupt_in_interval, int, 0);
143MODULE_PARM_DESC(min_interrupt_in_interval, "Minimum interrupt in interval in ms");
144
145static int min_interrupt_out_interval = 2;
146module_param(min_interrupt_out_interval, int, 0);
147MODULE_PARM_DESC(min_interrupt_out_interval, "Minimum interrupt out interval in ms");
148
149/* Structure to hold all of our device specific stuff */
150struct ld_usb {
151 struct semaphore sem; /* locks this structure */
152 struct usb_interface* intf; /* save off the usb interface pointer */
153
154 int open_count; /* number of times this port has been opened */
155
156 char* ring_buffer;
157 unsigned int ring_head;
158 unsigned int ring_tail;
159
160 wait_queue_head_t read_wait;
161 wait_queue_head_t write_wait;
162
163 char* interrupt_in_buffer;
164 struct usb_endpoint_descriptor* interrupt_in_endpoint;
165 struct urb* interrupt_in_urb;
166 int interrupt_in_interval;
167 size_t interrupt_in_endpoint_size;
168 int interrupt_in_running;
169 int interrupt_in_done;
Oliver Neukum9d33efd2007-05-04 09:23:40 +0200170 int buffer_overflow;
171 spinlock_t rbsl;
Michael Hund2824bd22005-06-27 22:44:22 +0200172
173 char* interrupt_out_buffer;
174 struct usb_endpoint_descriptor* interrupt_out_endpoint;
175 struct urb* interrupt_out_urb;
176 int interrupt_out_interval;
177 size_t interrupt_out_endpoint_size;
178 int interrupt_out_busy;
179};
180
Michael Hund2824bd22005-06-27 22:44:22 +0200181static struct usb_driver ld_usb_driver;
182
183/**
184 * ld_usb_abort_transfers
185 * aborts transfers and frees associated data structures
186 */
187static void ld_usb_abort_transfers(struct ld_usb *dev)
188{
189 /* shutdown transfer */
190 if (dev->interrupt_in_running) {
191 dev->interrupt_in_running = 0;
192 if (dev->intf)
193 usb_kill_urb(dev->interrupt_in_urb);
194 }
195 if (dev->interrupt_out_busy)
196 if (dev->intf)
197 usb_kill_urb(dev->interrupt_out_urb);
198}
199
200/**
201 * ld_usb_delete
202 */
203static void ld_usb_delete(struct ld_usb *dev)
204{
205 ld_usb_abort_transfers(dev);
206
207 /* free data structures */
208 usb_free_urb(dev->interrupt_in_urb);
209 usb_free_urb(dev->interrupt_out_urb);
210 kfree(dev->ring_buffer);
211 kfree(dev->interrupt_in_buffer);
212 kfree(dev->interrupt_out_buffer);
213 kfree(dev);
214}
215
216/**
217 * ld_usb_interrupt_in_callback
218 */
David Howells7d12e782006-10-05 14:55:46 +0100219static void ld_usb_interrupt_in_callback(struct urb *urb)
Michael Hund2824bd22005-06-27 22:44:22 +0200220{
221 struct ld_usb *dev = urb->context;
222 size_t *actual_buffer;
223 unsigned int next_ring_head;
Greg Kroah-Hartman491c0212007-07-18 10:58:02 -0700224 int status = urb->status;
Michael Hund2824bd22005-06-27 22:44:22 +0200225 int retval;
226
Greg Kroah-Hartman491c0212007-07-18 10:58:02 -0700227 if (status) {
228 if (status == -ENOENT ||
229 status == -ECONNRESET ||
230 status == -ESHUTDOWN) {
Michael Hund2824bd22005-06-27 22:44:22 +0200231 goto exit;
232 } else {
233 dbg_info(&dev->intf->dev, "%s: nonzero status received: %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800234 __func__, status);
Oliver Neukum9d33efd2007-05-04 09:23:40 +0200235 spin_lock(&dev->rbsl);
Michael Hund2824bd22005-06-27 22:44:22 +0200236 goto resubmit; /* maybe we can recover */
237 }
238 }
239
Oliver Neukum9d33efd2007-05-04 09:23:40 +0200240 spin_lock(&dev->rbsl);
Michael Hund2824bd22005-06-27 22:44:22 +0200241 if (urb->actual_length > 0) {
242 next_ring_head = (dev->ring_head+1) % ring_buffer_size;
243 if (next_ring_head != dev->ring_tail) {
244 actual_buffer = (size_t*)(dev->ring_buffer + dev->ring_head*(sizeof(size_t)+dev->interrupt_in_endpoint_size));
245 /* actual_buffer gets urb->actual_length + interrupt_in_buffer */
246 *actual_buffer = urb->actual_length;
247 memcpy(actual_buffer+1, dev->interrupt_in_buffer, urb->actual_length);
248 dev->ring_head = next_ring_head;
249 dbg_info(&dev->intf->dev, "%s: received %d bytes\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800250 __func__, urb->actual_length);
Oliver Neukum9d33efd2007-05-04 09:23:40 +0200251 } else {
Michael Hund2824bd22005-06-27 22:44:22 +0200252 dev_warn(&dev->intf->dev,
253 "Ring buffer overflow, %d bytes dropped\n",
254 urb->actual_length);
Oliver Neukum9d33efd2007-05-04 09:23:40 +0200255 dev->buffer_overflow = 1;
256 }
Michael Hund2824bd22005-06-27 22:44:22 +0200257 }
258
259resubmit:
260 /* resubmit if we're still running */
Oliver Neukum9d33efd2007-05-04 09:23:40 +0200261 if (dev->interrupt_in_running && !dev->buffer_overflow && dev->intf) {
Michael Hund2824bd22005-06-27 22:44:22 +0200262 retval = usb_submit_urb(dev->interrupt_in_urb, GFP_ATOMIC);
Oliver Neukum9d33efd2007-05-04 09:23:40 +0200263 if (retval) {
Michael Hund2824bd22005-06-27 22:44:22 +0200264 dev_err(&dev->intf->dev,
265 "usb_submit_urb failed (%d)\n", retval);
Oliver Neukum9d33efd2007-05-04 09:23:40 +0200266 dev->buffer_overflow = 1;
267 }
Michael Hund2824bd22005-06-27 22:44:22 +0200268 }
Oliver Neukum9d33efd2007-05-04 09:23:40 +0200269 spin_unlock(&dev->rbsl);
Michael Hund2824bd22005-06-27 22:44:22 +0200270exit:
271 dev->interrupt_in_done = 1;
272 wake_up_interruptible(&dev->read_wait);
273}
274
275/**
276 * ld_usb_interrupt_out_callback
277 */
David Howells7d12e782006-10-05 14:55:46 +0100278static void ld_usb_interrupt_out_callback(struct urb *urb)
Michael Hund2824bd22005-06-27 22:44:22 +0200279{
280 struct ld_usb *dev = urb->context;
Greg Kroah-Hartman491c0212007-07-18 10:58:02 -0700281 int status = urb->status;
Michael Hund2824bd22005-06-27 22:44:22 +0200282
283 /* sync/async unlink faults aren't errors */
Greg Kroah-Hartman491c0212007-07-18 10:58:02 -0700284 if (status && !(status == -ENOENT ||
285 status == -ECONNRESET ||
286 status == -ESHUTDOWN))
Michael Hund2824bd22005-06-27 22:44:22 +0200287 dbg_info(&dev->intf->dev,
288 "%s - nonzero write interrupt status received: %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800289 __func__, status);
Michael Hund2824bd22005-06-27 22:44:22 +0200290
291 dev->interrupt_out_busy = 0;
292 wake_up_interruptible(&dev->write_wait);
293}
294
295/**
296 * ld_usb_open
297 */
298static int ld_usb_open(struct inode *inode, struct file *file)
299{
300 struct ld_usb *dev;
301 int subminor;
Alan Sternd4ead162007-05-22 11:46:41 -0400302 int retval;
Michael Hund2824bd22005-06-27 22:44:22 +0200303 struct usb_interface *interface;
304
305 nonseekable_open(inode, file);
306 subminor = iminor(inode);
307
Michael Hund2824bd22005-06-27 22:44:22 +0200308 interface = usb_find_interface(&ld_usb_driver, subminor);
309
310 if (!interface) {
311 err("%s - error, can't find device for minor %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800312 __func__, subminor);
Alan Sternd4ead162007-05-22 11:46:41 -0400313 return -ENODEV;
Michael Hund2824bd22005-06-27 22:44:22 +0200314 }
315
316 dev = usb_get_intfdata(interface);
317
Alan Sternd4ead162007-05-22 11:46:41 -0400318 if (!dev)
319 return -ENODEV;
Michael Hund2824bd22005-06-27 22:44:22 +0200320
321 /* lock this device */
Alan Sternd4ead162007-05-22 11:46:41 -0400322 if (down_interruptible(&dev->sem))
323 return -ERESTARTSYS;
Michael Hund2824bd22005-06-27 22:44:22 +0200324
325 /* allow opening only once */
326 if (dev->open_count) {
327 retval = -EBUSY;
328 goto unlock_exit;
329 }
330 dev->open_count = 1;
331
332 /* initialize in direction */
333 dev->ring_head = 0;
334 dev->ring_tail = 0;
Oliver Neukum9d33efd2007-05-04 09:23:40 +0200335 dev->buffer_overflow = 0;
Michael Hund2824bd22005-06-27 22:44:22 +0200336 usb_fill_int_urb(dev->interrupt_in_urb,
337 interface_to_usbdev(interface),
338 usb_rcvintpipe(interface_to_usbdev(interface),
339 dev->interrupt_in_endpoint->bEndpointAddress),
340 dev->interrupt_in_buffer,
341 dev->interrupt_in_endpoint_size,
342 ld_usb_interrupt_in_callback,
343 dev,
344 dev->interrupt_in_interval);
345
346 dev->interrupt_in_running = 1;
347 dev->interrupt_in_done = 0;
348
349 retval = usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL);
350 if (retval) {
351 dev_err(&interface->dev, "Couldn't submit interrupt_in_urb %d\n", retval);
352 dev->interrupt_in_running = 0;
353 dev->open_count = 0;
354 goto unlock_exit;
355 }
356
357 /* save device in the file's private structure */
358 file->private_data = dev;
359
360unlock_exit:
361 up(&dev->sem);
362
Michael Hund2824bd22005-06-27 22:44:22 +0200363 return retval;
364}
365
366/**
367 * ld_usb_release
368 */
369static int ld_usb_release(struct inode *inode, struct file *file)
370{
371 struct ld_usb *dev;
372 int retval = 0;
373
374 dev = file->private_data;
375
376 if (dev == NULL) {
377 retval = -ENODEV;
378 goto exit;
379 }
380
381 if (down_interruptible(&dev->sem)) {
382 retval = -ERESTARTSYS;
383 goto exit;
384 }
385
386 if (dev->open_count != 1) {
387 retval = -ENODEV;
388 goto unlock_exit;
389 }
390 if (dev->intf == NULL) {
391 /* the device was unplugged before the file was released */
392 up(&dev->sem);
393 /* unlock here as ld_usb_delete frees dev */
394 ld_usb_delete(dev);
395 goto exit;
396 }
397
398 /* wait until write transfer is finished */
399 if (dev->interrupt_out_busy)
400 wait_event_interruptible_timeout(dev->write_wait, !dev->interrupt_out_busy, 2 * HZ);
401 ld_usb_abort_transfers(dev);
402 dev->open_count = 0;
403
404unlock_exit:
405 up(&dev->sem);
406
407exit:
408 return retval;
409}
410
411/**
412 * ld_usb_poll
413 */
414static unsigned int ld_usb_poll(struct file *file, poll_table *wait)
415{
416 struct ld_usb *dev;
417 unsigned int mask = 0;
418
419 dev = file->private_data;
420
421 poll_wait(file, &dev->read_wait, wait);
422 poll_wait(file, &dev->write_wait, wait);
423
424 if (dev->ring_head != dev->ring_tail)
425 mask |= POLLIN | POLLRDNORM;
426 if (!dev->interrupt_out_busy)
427 mask |= POLLOUT | POLLWRNORM;
428
429 return mask;
430}
431
432/**
433 * ld_usb_read
434 */
435static ssize_t ld_usb_read(struct file *file, char __user *buffer, size_t count,
436 loff_t *ppos)
437{
438 struct ld_usb *dev;
439 size_t *actual_buffer;
440 size_t bytes_to_read;
441 int retval = 0;
Oliver Neukum9d33efd2007-05-04 09:23:40 +0200442 int rv;
Michael Hund2824bd22005-06-27 22:44:22 +0200443
444 dev = file->private_data;
445
446 /* verify that we actually have some data to read */
447 if (count == 0)
448 goto exit;
449
450 /* lock this object */
451 if (down_interruptible(&dev->sem)) {
452 retval = -ERESTARTSYS;
453 goto exit;
454 }
455
456 /* verify that the device wasn't unplugged */
457 if (dev->intf == NULL) {
458 retval = -ENODEV;
459 err("No device or device unplugged %d\n", retval);
460 goto unlock_exit;
461 }
462
463 /* wait for data */
Oliver Neukum9d33efd2007-05-04 09:23:40 +0200464 spin_lock_irq(&dev->rbsl);
Michael Hund2824bd22005-06-27 22:44:22 +0200465 if (dev->ring_head == dev->ring_tail) {
Oliver Neukum9d33efd2007-05-04 09:23:40 +0200466 dev->interrupt_in_done = 0;
467 spin_unlock_irq(&dev->rbsl);
Michael Hund2824bd22005-06-27 22:44:22 +0200468 if (file->f_flags & O_NONBLOCK) {
469 retval = -EAGAIN;
470 goto unlock_exit;
471 }
472 retval = wait_event_interruptible(dev->read_wait, dev->interrupt_in_done);
473 if (retval < 0)
474 goto unlock_exit;
Oliver Neukum9d33efd2007-05-04 09:23:40 +0200475 } else {
476 spin_unlock_irq(&dev->rbsl);
Michael Hund2824bd22005-06-27 22:44:22 +0200477 }
478
479 /* actual_buffer contains actual_length + interrupt_in_buffer */
480 actual_buffer = (size_t*)(dev->ring_buffer + dev->ring_tail*(sizeof(size_t)+dev->interrupt_in_endpoint_size));
481 bytes_to_read = min(count, *actual_buffer);
482 if (bytes_to_read < *actual_buffer)
Alexey Dobriyandd7d5002005-08-14 17:24:26 +0400483 dev_warn(&dev->intf->dev, "Read buffer overflow, %zd bytes dropped\n",
Michael Hund2824bd22005-06-27 22:44:22 +0200484 *actual_buffer-bytes_to_read);
485
486 /* copy one interrupt_in_buffer from ring_buffer into userspace */
487 if (copy_to_user(buffer, actual_buffer+1, bytes_to_read)) {
488 retval = -EFAULT;
489 goto unlock_exit;
490 }
491 dev->ring_tail = (dev->ring_tail+1) % ring_buffer_size;
492
493 retval = bytes_to_read;
494
Oliver Neukum9d33efd2007-05-04 09:23:40 +0200495 spin_lock_irq(&dev->rbsl);
496 if (dev->buffer_overflow) {
497 dev->buffer_overflow = 0;
498 spin_unlock_irq(&dev->rbsl);
499 rv = usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL);
500 if (rv < 0)
501 dev->buffer_overflow = 1;
502 } else {
503 spin_unlock_irq(&dev->rbsl);
504 }
505
Michael Hund2824bd22005-06-27 22:44:22 +0200506unlock_exit:
507 /* unlock the device */
508 up(&dev->sem);
509
510exit:
511 return retval;
512}
513
514/**
515 * ld_usb_write
516 */
517static ssize_t ld_usb_write(struct file *file, const char __user *buffer,
518 size_t count, loff_t *ppos)
519{
520 struct ld_usb *dev;
521 size_t bytes_to_write;
522 int retval = 0;
523
524 dev = file->private_data;
525
526 /* verify that we actually have some data to write */
527 if (count == 0)
528 goto exit;
529
530 /* lock this object */
531 if (down_interruptible(&dev->sem)) {
532 retval = -ERESTARTSYS;
533 goto exit;
534 }
535
536 /* verify that the device wasn't unplugged */
537 if (dev->intf == NULL) {
538 retval = -ENODEV;
539 err("No device or device unplugged %d\n", retval);
540 goto unlock_exit;
541 }
542
543 /* wait until previous transfer is finished */
544 if (dev->interrupt_out_busy) {
545 if (file->f_flags & O_NONBLOCK) {
546 retval = -EAGAIN;
547 goto unlock_exit;
548 }
549 retval = wait_event_interruptible(dev->write_wait, !dev->interrupt_out_busy);
550 if (retval < 0) {
551 goto unlock_exit;
552 }
553 }
554
555 /* write the data into interrupt_out_buffer from userspace */
556 bytes_to_write = min(count, write_buffer_size*dev->interrupt_out_endpoint_size);
557 if (bytes_to_write < count)
Alexey Dobriyandd7d5002005-08-14 17:24:26 +0400558 dev_warn(&dev->intf->dev, "Write buffer overflow, %zd bytes dropped\n",count-bytes_to_write);
Harvey Harrison441b62c2008-03-03 16:08:34 -0800559 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 +0200560
561 if (copy_from_user(dev->interrupt_out_buffer, buffer, bytes_to_write)) {
562 retval = -EFAULT;
563 goto unlock_exit;
564 }
565
566 if (dev->interrupt_out_endpoint == NULL) {
567 /* try HID_REQ_SET_REPORT=9 on control_endpoint instead of interrupt_out_endpoint */
568 retval = usb_control_msg(interface_to_usbdev(dev->intf),
569 usb_sndctrlpipe(interface_to_usbdev(dev->intf), 0),
570 9,
571 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
572 1 << 8, 0,
573 dev->interrupt_out_buffer,
574 bytes_to_write,
575 USB_CTRL_SET_TIMEOUT * HZ);
576 if (retval < 0)
577 err("Couldn't submit HID_REQ_SET_REPORT %d\n", retval);
578 goto unlock_exit;
579 }
580
581 /* send off the urb */
582 usb_fill_int_urb(dev->interrupt_out_urb,
583 interface_to_usbdev(dev->intf),
584 usb_sndintpipe(interface_to_usbdev(dev->intf),
585 dev->interrupt_out_endpoint->bEndpointAddress),
586 dev->interrupt_out_buffer,
587 bytes_to_write,
588 ld_usb_interrupt_out_callback,
589 dev,
590 dev->interrupt_out_interval);
591
592 dev->interrupt_out_busy = 1;
593 wmb();
594
595 retval = usb_submit_urb(dev->interrupt_out_urb, GFP_KERNEL);
596 if (retval) {
597 dev->interrupt_out_busy = 0;
598 err("Couldn't submit interrupt_out_urb %d\n", retval);
599 goto unlock_exit;
600 }
601 retval = bytes_to_write;
602
603unlock_exit:
604 /* unlock the device */
605 up(&dev->sem);
606
607exit:
608 return retval;
609}
610
611/* file operations needed when we register this driver */
Luiz Fernando N. Capitulino066202d2006-08-05 20:37:11 -0300612static const struct file_operations ld_usb_fops = {
Michael Hund2824bd22005-06-27 22:44:22 +0200613 .owner = THIS_MODULE,
614 .read = ld_usb_read,
615 .write = ld_usb_write,
616 .open = ld_usb_open,
617 .release = ld_usb_release,
618 .poll = ld_usb_poll,
619};
620
621/*
622 * usb class driver info in order to get a minor number from the usb core,
Greg Kroah-Hartman595b14c2006-01-18 17:36:58 -0500623 * and to have the device registered with the driver core
Michael Hund2824bd22005-06-27 22:44:22 +0200624 */
625static struct usb_class_driver ld_usb_class = {
626 .name = "ldusb%d",
627 .fops = &ld_usb_fops,
628 .minor_base = USB_LD_MINOR_BASE,
629};
630
631/**
632 * ld_usb_probe
633 *
634 * Called by the usb core when a new device is connected that it thinks
635 * this driver might be interested in.
636 */
637static int ld_usb_probe(struct usb_interface *intf, const struct usb_device_id *id)
638{
639 struct usb_device *udev = interface_to_usbdev(intf);
640 struct ld_usb *dev = NULL;
641 struct usb_host_interface *iface_desc;
642 struct usb_endpoint_descriptor *endpoint;
643 char *buffer;
644 int i;
645 int retval = -ENOMEM;
646
647 /* allocate memory for our device state and intialize it */
648
Oliver Neukum1144cf72006-01-06 22:40:02 +0100649 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Michael Hund2824bd22005-06-27 22:44:22 +0200650 if (dev == NULL) {
651 dev_err(&intf->dev, "Out of memory\n");
652 goto exit;
653 }
Michael Hund2824bd22005-06-27 22:44:22 +0200654 init_MUTEX(&dev->sem);
Oliver Neukum9d33efd2007-05-04 09:23:40 +0200655 spin_lock_init(&dev->rbsl);
Michael Hund2824bd22005-06-27 22:44:22 +0200656 dev->intf = intf;
657 init_waitqueue_head(&dev->read_wait);
658 init_waitqueue_head(&dev->write_wait);
659
660 /* workaround for early firmware versions on fast computers */
661 if ((le16_to_cpu(udev->descriptor.idVendor) == USB_VENDOR_ID_LD) &&
Michael Hundba3e66e2006-02-02 09:36:43 +0100662 ((le16_to_cpu(udev->descriptor.idProduct) == USB_DEVICE_ID_LD_CASSY) ||
663 (le16_to_cpu(udev->descriptor.idProduct) == USB_DEVICE_ID_LD_COM3LAB)) &&
Michael Hund2824bd22005-06-27 22:44:22 +0200664 (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x103)) {
665 buffer = kmalloc(256, GFP_KERNEL);
Michael Hunda6db5922005-07-29 12:17:20 -0700666 if (buffer == NULL) {
667 dev_err(&intf->dev, "Couldn't allocate string buffer\n");
668 goto error;
669 }
Michael Hund2824bd22005-06-27 22:44:22 +0200670 /* usb_string makes SETUP+STALL to leave always ControlReadLoop */
671 usb_string(udev, 255, buffer, 256);
672 kfree(buffer);
673 }
674
675 iface_desc = intf->cur_altsetting;
676
677 /* set up the endpoint information */
678 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
679 endpoint = &iface_desc->endpoint[i].desc;
680
Luiz Fernando N. Capitulino54826872006-09-27 11:58:54 -0700681 if (usb_endpoint_is_int_in(endpoint))
Michael Hund2824bd22005-06-27 22:44:22 +0200682 dev->interrupt_in_endpoint = endpoint;
Michael Hund2824bd22005-06-27 22:44:22 +0200683
Luiz Fernando N. Capitulino54826872006-09-27 11:58:54 -0700684 if (usb_endpoint_is_int_out(endpoint))
Michael Hund2824bd22005-06-27 22:44:22 +0200685 dev->interrupt_out_endpoint = endpoint;
Michael Hund2824bd22005-06-27 22:44:22 +0200686 }
687 if (dev->interrupt_in_endpoint == NULL) {
688 dev_err(&intf->dev, "Interrupt in endpoint not found\n");
689 goto error;
690 }
691 if (dev->interrupt_out_endpoint == NULL)
692 dev_warn(&intf->dev, "Interrupt out endpoint not found (using control endpoint instead)\n");
693
694 dev->interrupt_in_endpoint_size = le16_to_cpu(dev->interrupt_in_endpoint->wMaxPacketSize);
695 dev->ring_buffer = kmalloc(ring_buffer_size*(sizeof(size_t)+dev->interrupt_in_endpoint_size), GFP_KERNEL);
696 if (!dev->ring_buffer) {
697 dev_err(&intf->dev, "Couldn't allocate ring_buffer\n");
698 goto error;
699 }
700 dev->interrupt_in_buffer = kmalloc(dev->interrupt_in_endpoint_size, GFP_KERNEL);
701 if (!dev->interrupt_in_buffer) {
702 dev_err(&intf->dev, "Couldn't allocate interrupt_in_buffer\n");
703 goto error;
704 }
705 dev->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
706 if (!dev->interrupt_in_urb) {
707 dev_err(&intf->dev, "Couldn't allocate interrupt_in_urb\n");
708 goto error;
709 }
710 dev->interrupt_out_endpoint_size = dev->interrupt_out_endpoint ? le16_to_cpu(dev->interrupt_out_endpoint->wMaxPacketSize) :
711 udev->descriptor.bMaxPacketSize0;
712 dev->interrupt_out_buffer = kmalloc(write_buffer_size*dev->interrupt_out_endpoint_size, GFP_KERNEL);
713 if (!dev->interrupt_out_buffer) {
714 dev_err(&intf->dev, "Couldn't allocate interrupt_out_buffer\n");
715 goto error;
716 }
717 dev->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL);
718 if (!dev->interrupt_out_urb) {
719 dev_err(&intf->dev, "Couldn't allocate interrupt_out_urb\n");
720 goto error;
721 }
722 dev->interrupt_in_interval = min_interrupt_in_interval > dev->interrupt_in_endpoint->bInterval ? min_interrupt_in_interval : dev->interrupt_in_endpoint->bInterval;
723 if (dev->interrupt_out_endpoint)
724 dev->interrupt_out_interval = min_interrupt_out_interval > dev->interrupt_out_endpoint->bInterval ? min_interrupt_out_interval : dev->interrupt_out_endpoint->bInterval;
725
726 /* we can register the device now, as it is ready */
727 usb_set_intfdata(intf, dev);
728
729 retval = usb_register_dev(intf, &ld_usb_class);
730 if (retval) {
731 /* something prevented us from registering this driver */
732 dev_err(&intf->dev, "Not able to get a minor for this device.\n");
733 usb_set_intfdata(intf, NULL);
734 goto error;
735 }
736
737 /* let the user know what node this device is now attached to */
738 dev_info(&intf->dev, "LD USB Device #%d now attached to major %d minor %d\n",
739 (intf->minor - USB_LD_MINOR_BASE), USB_MAJOR, intf->minor);
740
741exit:
742 return retval;
743
744error:
745 ld_usb_delete(dev);
746
747 return retval;
748}
749
750/**
751 * ld_usb_disconnect
752 *
753 * Called by the usb core when the device is removed from the system.
754 */
755static void ld_usb_disconnect(struct usb_interface *intf)
756{
757 struct ld_usb *dev;
758 int minor;
759
Michael Hund2824bd22005-06-27 22:44:22 +0200760 dev = usb_get_intfdata(intf);
761 usb_set_intfdata(intf, NULL);
762
Michael Hund2824bd22005-06-27 22:44:22 +0200763 minor = intf->minor;
764
765 /* give back our minor */
766 usb_deregister_dev(intf, &ld_usb_class);
767
Alan Sternd4ead162007-05-22 11:46:41 -0400768 down(&dev->sem);
769
Michael Hund2824bd22005-06-27 22:44:22 +0200770 /* if the device is not opened, then we clean up right now */
771 if (!dev->open_count) {
772 up(&dev->sem);
773 ld_usb_delete(dev);
774 } else {
775 dev->intf = NULL;
776 up(&dev->sem);
777 }
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