]>
Commit | Line | Data |
---|---|---|
2824bd25 MH |
1 | /** |
2 | * Generic USB driver for report based interrupt in/out devices | |
3 | * like LD Didactic's USB devices. LD Didactic's USB devices are | |
4 | * HID devices which do not use HID report definitons (they use | |
5 | * raw interrupt in and our reports only for communication). | |
6 | * | |
7 | * This driver uses a ring buffer for time critical reading of | |
8 | * interrupt in reports and provides read and write methods for | |
9 | * raw interrupt reports (similar to the Windows HID driver). | |
10 | * Devices based on the book USB COMPLETE by Jan Axelson may need | |
11 | * such a compatibility to the Windows HID driver. | |
12 | * | |
13 | * Copyright (C) 2005 Michael Hund <mhund@ld-didactic.de> | |
14 | * | |
15 | * This program is free software; you can redistribute it and/or | |
16 | * modify it under the terms of the GNU General Public License as | |
17 | * published by the Free Software Foundation; either version 2 of | |
18 | * the License, or (at your option) any later version. | |
19 | * | |
20 | * Derived from Lego USB Tower driver | |
21 | * Copyright (C) 2003 David Glance <advidgsf@sourceforge.net> | |
22 | * 2001-2004 Juergen Stuber <starblue@users.sourceforge.net> | |
23 | * | |
24 | * V0.1 (mh) Initial version | |
25 | * V0.11 (mh) Added raw support for HID 1.0 devices (no interrupt out endpoint) | |
a6db592e | 26 | * V0.12 (mh) Added kmalloc check for string buffer |
ba3e66e9 | 27 | * V0.13 (mh) Added support for LD X-Ray and Machine Test System |
2824bd25 MH |
28 | */ |
29 | ||
2824bd25 MH |
30 | #include <linux/kernel.h> |
31 | #include <linux/errno.h> | |
32 | #include <linux/init.h> | |
33 | #include <linux/slab.h> | |
34 | #include <linux/module.h> | |
4186ecf8 | 35 | #include <linux/mutex.h> |
2824bd25 MH |
36 | |
37 | #include <asm/uaccess.h> | |
38 | #include <linux/input.h> | |
39 | #include <linux/usb.h> | |
40 | #include <linux/poll.h> | |
41 | ||
42 | /* Define these values to match your devices */ | |
43 | #define USB_VENDOR_ID_LD 0x0f11 /* USB Vendor ID of LD Didactic GmbH */ | |
ba3e66e9 MH |
44 | #define USB_DEVICE_ID_LD_CASSY 0x1000 /* USB Product ID of CASSY-S */ |
45 | #define USB_DEVICE_ID_LD_POCKETCASSY 0x1010 /* USB Product ID of Pocket-CASSY */ | |
46 | #define USB_DEVICE_ID_LD_MOBILECASSY 0x1020 /* USB Product ID of Mobile-CASSY */ | |
47 | #define USB_DEVICE_ID_LD_JWM 0x1080 /* USB Product ID of Joule and Wattmeter */ | |
48 | #define USB_DEVICE_ID_LD_DMMP 0x1081 /* USB Product ID of Digital Multimeter P (reserved) */ | |
49 | #define USB_DEVICE_ID_LD_UMIP 0x1090 /* USB Product ID of UMI P */ | |
50 | #define USB_DEVICE_ID_LD_XRAY1 0x1100 /* USB Product ID of X-Ray Apparatus */ | |
51 | #define USB_DEVICE_ID_LD_XRAY2 0x1101 /* USB Product ID of X-Ray Apparatus */ | |
52 | #define USB_DEVICE_ID_LD_VIDEOCOM 0x1200 /* USB Product ID of VideoCom */ | |
53 | #define USB_DEVICE_ID_LD_COM3LAB 0x2000 /* USB Product ID of COM3LAB */ | |
54 | #define USB_DEVICE_ID_LD_TELEPORT 0x2010 /* USB Product ID of Terminal Adapter */ | |
55 | #define USB_DEVICE_ID_LD_NETWORKANALYSER 0x2020 /* USB Product ID of Network Analyser */ | |
56 | #define USB_DEVICE_ID_LD_POWERCONTROL 0x2030 /* USB Product ID of Converter Control Unit */ | |
57 | #define USB_DEVICE_ID_LD_MACHINETEST 0x2040 /* USB Product ID of Machine Test System */ | |
2824bd25 MH |
58 | |
59 | #define USB_VENDOR_ID_VERNIER 0x08f7 | |
60 | #define USB_DEVICE_ID_VERNIER_LABPRO 0x0001 | |
61 | #define USB_DEVICE_ID_VERNIER_GOTEMP 0x0002 | |
62 | #define USB_DEVICE_ID_VERNIER_SKIP 0x0003 | |
63 | #define USB_DEVICE_ID_VERNIER_CYCLOPS 0x0004 | |
5b0a4d66 | 64 | #define USB_DEVICE_ID_VERNIER_LCSPEC 0x0006 |
2824bd25 | 65 | |
79dcdbf6 JG |
66 | #define USB_VENDOR_ID_MICROCHIP 0x04d8 |
67 | #define USB_DEVICE_ID_PICDEM 0x000c | |
2824bd25 MH |
68 | |
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 */ | |
76 | static struct usb_device_id ld_usb_table [] = { | |
ba3e66e9 MH |
77 | { 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) }, | |
2824bd25 MH |
91 | { 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) }, | |
79dcdbf6 | 95 | { USB_DEVICE(USB_VENDOR_ID_MICROCHIP, USB_DEVICE_ID_PICDEM) }, |
5b0a4d66 | 96 | { USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_LCSPEC) }, |
2824bd25 MH |
97 | { } /* Terminating entry */ |
98 | }; | |
99 | MODULE_DEVICE_TABLE(usb, ld_usb_table); | |
ba3e66e9 | 100 | MODULE_VERSION("V0.13"); |
2824bd25 MH |
101 | MODULE_AUTHOR("Michael Hund <mhund@ld-didactic.de>"); |
102 | MODULE_DESCRIPTION("LD USB Driver"); | |
103 | MODULE_LICENSE("GPL"); | |
104 | MODULE_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 */ | |
116 | module_param(debug, int, S_IRUGO | S_IWUSR); | |
117 | MODULE_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 | */ | |
122 | static int ring_buffer_size = 128; | |
123 | module_param(ring_buffer_size, int, 0); | |
124 | MODULE_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 | */ | |
128 | static int write_buffer_size = 10; | |
129 | module_param(write_buffer_size, int, 0); | |
130 | MODULE_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 | */ | |
141 | static int min_interrupt_in_interval = 2; | |
142 | module_param(min_interrupt_in_interval, int, 0); | |
143 | MODULE_PARM_DESC(min_interrupt_in_interval, "Minimum interrupt in interval in ms"); | |
144 | ||
145 | static int min_interrupt_out_interval = 2; | |
146 | module_param(min_interrupt_out_interval, int, 0); | |
147 | MODULE_PARM_DESC(min_interrupt_out_interval, "Minimum interrupt out interval in ms"); | |
148 | ||
149 | /* Structure to hold all of our device specific stuff */ | |
150 | struct ld_usb { | |
ce0d7d3f | 151 | struct mutex mutex; /* locks this structure */ |
2824bd25 MH |
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; | |
9d33efd9 ON |
170 | int buffer_overflow; |
171 | spinlock_t rbsl; | |
2824bd25 MH |
172 | |
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 | ||
2824bd25 MH |
181 | static struct usb_driver ld_usb_driver; |
182 | ||
183 | /** | |
184 | * ld_usb_abort_transfers | |
185 | * aborts transfers and frees associated data structures | |
186 | */ | |
187 | static 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 | */ | |
203 | static 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 | */ | |
7d12e780 | 219 | static void ld_usb_interrupt_in_callback(struct urb *urb) |
2824bd25 MH |
220 | { |
221 | struct ld_usb *dev = urb->context; | |
222 | size_t *actual_buffer; | |
223 | unsigned int next_ring_head; | |
491c021e | 224 | int status = urb->status; |
2824bd25 MH |
225 | int retval; |
226 | ||
491c021e GKH |
227 | if (status) { |
228 | if (status == -ENOENT || | |
229 | status == -ECONNRESET || | |
230 | status == -ESHUTDOWN) { | |
2824bd25 MH |
231 | goto exit; |
232 | } else { | |
233 | dbg_info(&dev->intf->dev, "%s: nonzero status received: %d\n", | |
441b62c1 | 234 | __func__, status); |
9d33efd9 | 235 | spin_lock(&dev->rbsl); |
2824bd25 MH |
236 | goto resubmit; /* maybe we can recover */ |
237 | } | |
238 | } | |
239 | ||
9d33efd9 | 240 | spin_lock(&dev->rbsl); |
2824bd25 MH |
241 | 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", | |
441b62c1 | 250 | __func__, urb->actual_length); |
9d33efd9 | 251 | } else { |
2824bd25 MH |
252 | dev_warn(&dev->intf->dev, |
253 | "Ring buffer overflow, %d bytes dropped\n", | |
254 | urb->actual_length); | |
9d33efd9 ON |
255 | dev->buffer_overflow = 1; |
256 | } | |
2824bd25 MH |
257 | } |
258 | ||
259 | resubmit: | |
260 | /* resubmit if we're still running */ | |
9d33efd9 | 261 | if (dev->interrupt_in_running && !dev->buffer_overflow && dev->intf) { |
2824bd25 | 262 | retval = usb_submit_urb(dev->interrupt_in_urb, GFP_ATOMIC); |
9d33efd9 | 263 | if (retval) { |
2824bd25 MH |
264 | dev_err(&dev->intf->dev, |
265 | "usb_submit_urb failed (%d)\n", retval); | |
9d33efd9 ON |
266 | dev->buffer_overflow = 1; |
267 | } | |
2824bd25 | 268 | } |
9d33efd9 | 269 | spin_unlock(&dev->rbsl); |
2824bd25 MH |
270 | exit: |
271 | dev->interrupt_in_done = 1; | |
272 | wake_up_interruptible(&dev->read_wait); | |
273 | } | |
274 | ||
275 | /** | |
276 | * ld_usb_interrupt_out_callback | |
277 | */ | |
7d12e780 | 278 | static void ld_usb_interrupt_out_callback(struct urb *urb) |
2824bd25 MH |
279 | { |
280 | struct ld_usb *dev = urb->context; | |
491c021e | 281 | int status = urb->status; |
2824bd25 MH |
282 | |
283 | /* sync/async unlink faults aren't errors */ | |
491c021e GKH |
284 | if (status && !(status == -ENOENT || |
285 | status == -ECONNRESET || | |
286 | status == -ESHUTDOWN)) | |
2824bd25 MH |
287 | dbg_info(&dev->intf->dev, |
288 | "%s - nonzero write interrupt status received: %d\n", | |
441b62c1 | 289 | __func__, status); |
2824bd25 MH |
290 | |
291 | dev->interrupt_out_busy = 0; | |
292 | wake_up_interruptible(&dev->write_wait); | |
293 | } | |
294 | ||
295 | /** | |
296 | * ld_usb_open | |
297 | */ | |
298 | static int ld_usb_open(struct inode *inode, struct file *file) | |
299 | { | |
300 | struct ld_usb *dev; | |
301 | int subminor; | |
d4ead16f | 302 | int retval; |
2824bd25 MH |
303 | struct usb_interface *interface; |
304 | ||
305 | nonseekable_open(inode, file); | |
306 | subminor = iminor(inode); | |
307 | ||
2824bd25 MH |
308 | interface = usb_find_interface(&ld_usb_driver, subminor); |
309 | ||
310 | if (!interface) { | |
311 | err("%s - error, can't find device for minor %d\n", | |
441b62c1 | 312 | __func__, subminor); |
d4ead16f | 313 | return -ENODEV; |
2824bd25 MH |
314 | } |
315 | ||
316 | dev = usb_get_intfdata(interface); | |
317 | ||
d4ead16f AS |
318 | if (!dev) |
319 | return -ENODEV; | |
2824bd25 MH |
320 | |
321 | /* lock this device */ | |
ce0d7d3f | 322 | if (mutex_lock_interruptible(&dev->mutex)) |
d4ead16f | 323 | return -ERESTARTSYS; |
2824bd25 MH |
324 | |
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; | |
9d33efd9 | 335 | dev->buffer_overflow = 0; |
2824bd25 MH |
336 | 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 | ||
360 | unlock_exit: | |
ce0d7d3f | 361 | mutex_unlock(&dev->mutex); |
2824bd25 | 362 | |
2824bd25 MH |
363 | return retval; |
364 | } | |
365 | ||
366 | /** | |
367 | * ld_usb_release | |
368 | */ | |
369 | static 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 | ||
ce0d7d3f | 381 | if (mutex_lock_interruptible(&dev->mutex)) { |
2824bd25 MH |
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 */ | |
ce0d7d3f | 392 | mutex_unlock(&dev->mutex); |
2824bd25 MH |
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 | ||
404 | unlock_exit: | |
ce0d7d3f | 405 | mutex_unlock(&dev->mutex); |
2824bd25 MH |
406 | |
407 | exit: | |
408 | return retval; | |
409 | } | |
410 | ||
411 | /** | |
412 | * ld_usb_poll | |
413 | */ | |
414 | static 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 | */ | |
435 | static 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; | |
9d33efd9 | 442 | int rv; |
2824bd25 MH |
443 | |
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 */ | |
ce0d7d3f | 451 | if (mutex_lock_interruptible(&dev->mutex)) { |
2824bd25 MH |
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 */ | |
9d33efd9 | 464 | spin_lock_irq(&dev->rbsl); |
2824bd25 | 465 | if (dev->ring_head == dev->ring_tail) { |
9d33efd9 ON |
466 | dev->interrupt_in_done = 0; |
467 | spin_unlock_irq(&dev->rbsl); | |
2824bd25 MH |
468 | 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; | |
9d33efd9 ON |
475 | } else { |
476 | spin_unlock_irq(&dev->rbsl); | |
2824bd25 MH |
477 | } |
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) | |
dd7d5008 | 483 | dev_warn(&dev->intf->dev, "Read buffer overflow, %zd bytes dropped\n", |
2824bd25 MH |
484 | *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 | ||
9d33efd9 ON |
495 | 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 | ||
2824bd25 MH |
506 | unlock_exit: |
507 | /* unlock the device */ | |
ce0d7d3f | 508 | mutex_unlock(&dev->mutex); |
2824bd25 MH |
509 | |
510 | exit: | |
511 | return retval; | |
512 | } | |
513 | ||
514 | /** | |
515 | * ld_usb_write | |
516 | */ | |
517 | static 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 */ | |
ce0d7d3f | 531 | if (mutex_lock_interruptible(&dev->mutex)) { |
2824bd25 MH |
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) | |
dd7d5008 | 558 | dev_warn(&dev->intf->dev, "Write buffer overflow, %zd bytes dropped\n",count-bytes_to_write); |
441b62c1 | 559 | dbg_info(&dev->intf->dev, "%s: count = %zd, bytes_to_write = %zd\n", __func__, count, bytes_to_write); |
2824bd25 MH |
560 | |
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 | ||
603 | unlock_exit: | |
604 | /* unlock the device */ | |
ce0d7d3f | 605 | mutex_unlock(&dev->mutex); |
2824bd25 MH |
606 | |
607 | exit: | |
608 | return retval; | |
609 | } | |
610 | ||
611 | /* file operations needed when we register this driver */ | |
066202dd | 612 | static const struct file_operations ld_usb_fops = { |
2824bd25 MH |
613 | .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, | |
595b14cb | 623 | * and to have the device registered with the driver core |
2824bd25 MH |
624 | */ |
625 | static 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 | */ | |
637 | static 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 | ||
1144cf7a | 649 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
2824bd25 MH |
650 | if (dev == NULL) { |
651 | dev_err(&intf->dev, "Out of memory\n"); | |
652 | goto exit; | |
653 | } | |
ce0d7d3f | 654 | mutex_init(&dev->mutex); |
9d33efd9 | 655 | spin_lock_init(&dev->rbsl); |
2824bd25 MH |
656 | 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) && | |
ba3e66e9 MH |
662 | ((le16_to_cpu(udev->descriptor.idProduct) == USB_DEVICE_ID_LD_CASSY) || |
663 | (le16_to_cpu(udev->descriptor.idProduct) == USB_DEVICE_ID_LD_COM3LAB)) && | |
2824bd25 MH |
664 | (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x103)) { |
665 | buffer = kmalloc(256, GFP_KERNEL); | |
a6db592e MH |
666 | if (buffer == NULL) { |
667 | dev_err(&intf->dev, "Couldn't allocate string buffer\n"); | |
668 | goto error; | |
669 | } | |
2824bd25 MH |
670 | /* 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 | ||
5482687b | 681 | if (usb_endpoint_is_int_in(endpoint)) |
2824bd25 | 682 | dev->interrupt_in_endpoint = endpoint; |
2824bd25 | 683 | |
5482687b | 684 | if (usb_endpoint_is_int_out(endpoint)) |
2824bd25 | 685 | dev->interrupt_out_endpoint = endpoint; |
2824bd25 MH |
686 | } |
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 | ||
741 | exit: | |
742 | return retval; | |
743 | ||
744 | error: | |
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 | */ | |
755 | static void ld_usb_disconnect(struct usb_interface *intf) | |
756 | { | |
757 | struct ld_usb *dev; | |
758 | int minor; | |
759 | ||
2824bd25 MH |
760 | dev = usb_get_intfdata(intf); |
761 | usb_set_intfdata(intf, NULL); | |
762 | ||
2824bd25 MH |
763 | minor = intf->minor; |
764 | ||
765 | /* give back our minor */ | |
766 | usb_deregister_dev(intf, &ld_usb_class); | |
767 | ||
ce0d7d3f | 768 | mutex_lock(&dev->mutex); |
d4ead16f | 769 | |
2824bd25 MH |
770 | /* if the device is not opened, then we clean up right now */ |
771 | if (!dev->open_count) { | |
ce0d7d3f | 772 | mutex_unlock(&dev->mutex); |
2824bd25 MH |
773 | ld_usb_delete(dev); |
774 | } else { | |
775 | dev->intf = NULL; | |
ce0d7d3f | 776 | mutex_unlock(&dev->mutex); |
2824bd25 MH |
777 | } |
778 | ||
2824bd25 MH |
779 | 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 */ | |
784 | static struct usb_driver ld_usb_driver = { | |
2824bd25 MH |
785 | .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 | */ | |
794 | static 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 | */ | |
809 | static void __exit ld_usb_exit(void) | |
810 | { | |
811 | /* deregister this driver with the USB subsystem */ | |
812 | usb_deregister(&ld_usb_driver); | |
813 | } | |
814 | ||
815 | module_init(ld_usb_init); | |
816 | module_exit(ld_usb_exit); | |
817 |