]>
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> | |
2824bd25 MH |
23 | */ |
24 | ||
2824bd25 MH |
25 | #include <linux/kernel.h> |
26 | #include <linux/errno.h> | |
27 | #include <linux/init.h> | |
28 | #include <linux/slab.h> | |
29 | #include <linux/module.h> | |
4186ecf8 | 30 | #include <linux/mutex.h> |
2824bd25 MH |
31 | |
32 | #include <asm/uaccess.h> | |
33 | #include <linux/input.h> | |
34 | #include <linux/usb.h> | |
35 | #include <linux/poll.h> | |
36 | ||
37 | /* Define these values to match your devices */ | |
38 | #define USB_VENDOR_ID_LD 0x0f11 /* USB Vendor ID of LD Didactic GmbH */ | |
ce97cac8 MH |
39 | #define USB_DEVICE_ID_LD_CASSY 0x1000 /* USB Product ID of CASSY-S modules with 8 bytes endpoint size */ |
40 | #define USB_DEVICE_ID_LD_CASSY2 0x1001 /* USB Product ID of CASSY-S modules with 64 bytes endpoint size */ | |
ba3e66e9 | 41 | #define USB_DEVICE_ID_LD_POCKETCASSY 0x1010 /* USB Product ID of Pocket-CASSY */ |
ce97cac8 | 42 | #define USB_DEVICE_ID_LD_POCKETCASSY2 0x1011 /* USB Product ID of Pocket-CASSY 2 (reserved) */ |
ba3e66e9 | 43 | #define USB_DEVICE_ID_LD_MOBILECASSY 0x1020 /* USB Product ID of Mobile-CASSY */ |
ce97cac8 MH |
44 | #define USB_DEVICE_ID_LD_MOBILECASSY2 0x1021 /* USB Product ID of Mobile-CASSY 2 (reserved) */ |
45 | #define USB_DEVICE_ID_LD_MICROCASSYVOLTAGE 0x1031 /* USB Product ID of Micro-CASSY Voltage */ | |
46 | #define USB_DEVICE_ID_LD_MICROCASSYCURRENT 0x1032 /* USB Product ID of Micro-CASSY Current */ | |
47 | #define USB_DEVICE_ID_LD_MICROCASSYTIME 0x1033 /* USB Product ID of Micro-CASSY Time (reserved) */ | |
48 | #define USB_DEVICE_ID_LD_MICROCASSYTEMPERATURE 0x1035 /* USB Product ID of Micro-CASSY Temperature */ | |
49 | #define USB_DEVICE_ID_LD_MICROCASSYPH 0x1038 /* USB Product ID of Micro-CASSY pH */ | |
ba3e66e9 MH |
50 | #define USB_DEVICE_ID_LD_JWM 0x1080 /* USB Product ID of Joule and Wattmeter */ |
51 | #define USB_DEVICE_ID_LD_DMMP 0x1081 /* USB Product ID of Digital Multimeter P (reserved) */ | |
52 | #define USB_DEVICE_ID_LD_UMIP 0x1090 /* USB Product ID of UMI P */ | |
ce97cac8 MH |
53 | #define USB_DEVICE_ID_LD_UMIC 0x10A0 /* USB Product ID of UMI C */ |
54 | #define USB_DEVICE_ID_LD_UMIB 0x10B0 /* USB Product ID of UMI B */ | |
55 | #define USB_DEVICE_ID_LD_XRAY 0x1100 /* USB Product ID of X-Ray Apparatus 55481 */ | |
56 | #define USB_DEVICE_ID_LD_XRAY2 0x1101 /* USB Product ID of X-Ray Apparatus 554800 */ | |
57 | #define USB_DEVICE_ID_LD_XRAYCT 0x1110 /* USB Product ID of X-Ray Apparatus CT 554821*/ | |
ba3e66e9 | 58 | #define USB_DEVICE_ID_LD_VIDEOCOM 0x1200 /* USB Product ID of VideoCom */ |
ce97cac8 | 59 | #define USB_DEVICE_ID_LD_MOTOR 0x1210 /* USB Product ID of Motor (reserved) */ |
ba3e66e9 MH |
60 | #define USB_DEVICE_ID_LD_COM3LAB 0x2000 /* USB Product ID of COM3LAB */ |
61 | #define USB_DEVICE_ID_LD_TELEPORT 0x2010 /* USB Product ID of Terminal Adapter */ | |
62 | #define USB_DEVICE_ID_LD_NETWORKANALYSER 0x2020 /* USB Product ID of Network Analyser */ | |
63 | #define USB_DEVICE_ID_LD_POWERCONTROL 0x2030 /* USB Product ID of Converter Control Unit */ | |
64 | #define USB_DEVICE_ID_LD_MACHINETEST 0x2040 /* USB Product ID of Machine Test System */ | |
ce97cac8 MH |
65 | #define USB_DEVICE_ID_LD_MOSTANALYSER 0x2050 /* USB Product ID of MOST Protocol Analyser */ |
66 | #define USB_DEVICE_ID_LD_MOSTANALYSER2 0x2051 /* USB Product ID of MOST Protocol Analyser 2 */ | |
67 | #define USB_DEVICE_ID_LD_ABSESP 0x2060 /* USB Product ID of ABS ESP */ | |
68 | #define USB_DEVICE_ID_LD_AUTODATABUS 0x2070 /* USB Product ID of Automotive Data Buses */ | |
69 | #define USB_DEVICE_ID_LD_MCT 0x2080 /* USB Product ID of Microcontroller technique */ | |
70 | #define USB_DEVICE_ID_LD_HYBRID 0x2090 /* USB Product ID of Automotive Hybrid */ | |
71 | #define USB_DEVICE_ID_LD_HEATCONTROL 0x20A0 /* USB Product ID of Heat control */ | |
2824bd25 MH |
72 | |
73 | #define USB_VENDOR_ID_VERNIER 0x08f7 | |
2824bd25 MH |
74 | #define USB_DEVICE_ID_VERNIER_GOTEMP 0x0002 |
75 | #define USB_DEVICE_ID_VERNIER_SKIP 0x0003 | |
76 | #define USB_DEVICE_ID_VERNIER_CYCLOPS 0x0004 | |
5b0a4d66 | 77 | #define USB_DEVICE_ID_VERNIER_LCSPEC 0x0006 |
2824bd25 | 78 | |
2824bd25 MH |
79 | #ifdef CONFIG_USB_DYNAMIC_MINORS |
80 | #define USB_LD_MINOR_BASE 0 | |
81 | #else | |
82 | #define USB_LD_MINOR_BASE 176 | |
83 | #endif | |
84 | ||
85 | /* table of devices that work with this driver */ | |
33b9e162 | 86 | static const struct usb_device_id ld_usb_table[] = { |
ba3e66e9 | 87 | { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_CASSY) }, |
ce97cac8 | 88 | { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_CASSY2) }, |
ba3e66e9 | 89 | { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_POCKETCASSY) }, |
ce97cac8 | 90 | { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_POCKETCASSY2) }, |
ba3e66e9 | 91 | { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MOBILECASSY) }, |
ce97cac8 MH |
92 | { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MOBILECASSY2) }, |
93 | { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MICROCASSYVOLTAGE) }, | |
94 | { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MICROCASSYCURRENT) }, | |
95 | { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MICROCASSYTIME) }, | |
96 | { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MICROCASSYTEMPERATURE) }, | |
97 | { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MICROCASSYPH) }, | |
ba3e66e9 MH |
98 | { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_JWM) }, |
99 | { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_DMMP) }, | |
100 | { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_UMIP) }, | |
ce97cac8 MH |
101 | { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_UMIC) }, |
102 | { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_UMIB) }, | |
103 | { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_XRAY) }, | |
ba3e66e9 MH |
104 | { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_XRAY2) }, |
105 | { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_VIDEOCOM) }, | |
ce97cac8 | 106 | { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MOTOR) }, |
ba3e66e9 MH |
107 | { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_COM3LAB) }, |
108 | { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_TELEPORT) }, | |
109 | { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_NETWORKANALYSER) }, | |
110 | { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_POWERCONTROL) }, | |
111 | { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MACHINETEST) }, | |
ce97cac8 MH |
112 | { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MOSTANALYSER) }, |
113 | { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MOSTANALYSER2) }, | |
114 | { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_ABSESP) }, | |
115 | { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_AUTODATABUS) }, | |
116 | { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MCT) }, | |
117 | { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_HYBRID) }, | |
118 | { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_HEATCONTROL) }, | |
2824bd25 MH |
119 | { USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_GOTEMP) }, |
120 | { USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_SKIP) }, | |
121 | { USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_CYCLOPS) }, | |
5b0a4d66 | 122 | { USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_LCSPEC) }, |
2824bd25 MH |
123 | { } /* Terminating entry */ |
124 | }; | |
125 | MODULE_DEVICE_TABLE(usb, ld_usb_table); | |
ce97cac8 | 126 | MODULE_VERSION("V0.14"); |
2824bd25 MH |
127 | MODULE_AUTHOR("Michael Hund <mhund@ld-didactic.de>"); |
128 | MODULE_DESCRIPTION("LD USB Driver"); | |
129 | MODULE_LICENSE("GPL"); | |
130 | MODULE_SUPPORTED_DEVICE("LD USB Devices"); | |
131 | ||
2824bd25 MH |
132 | /* All interrupt in transfers are collected in a ring buffer to |
133 | * avoid racing conditions and get better performance of the driver. | |
134 | */ | |
135 | static int ring_buffer_size = 128; | |
136 | module_param(ring_buffer_size, int, 0); | |
137 | MODULE_PARM_DESC(ring_buffer_size, "Read ring buffer size in reports"); | |
138 | ||
139 | /* The write_buffer can contain more than one interrupt out transfer. | |
140 | */ | |
141 | static int write_buffer_size = 10; | |
142 | module_param(write_buffer_size, int, 0); | |
143 | MODULE_PARM_DESC(write_buffer_size, "Write buffer size in reports"); | |
144 | ||
145 | /* As of kernel version 2.6.4 ehci-hcd uses an | |
146 | * "only one interrupt transfer per frame" shortcut | |
147 | * to simplify the scheduling of periodic transfers. | |
148 | * This conflicts with our standard 1ms intervals for in and out URBs. | |
149 | * We use default intervals of 2ms for in and 2ms for out transfers, | |
150 | * which should be fast enough. | |
151 | * Increase the interval to allow more devices that do interrupt transfers, | |
152 | * or set to 1 to use the standard interval from the endpoint descriptors. | |
153 | */ | |
154 | static int min_interrupt_in_interval = 2; | |
155 | module_param(min_interrupt_in_interval, int, 0); | |
156 | MODULE_PARM_DESC(min_interrupt_in_interval, "Minimum interrupt in interval in ms"); | |
157 | ||
158 | static int min_interrupt_out_interval = 2; | |
159 | module_param(min_interrupt_out_interval, int, 0); | |
160 | MODULE_PARM_DESC(min_interrupt_out_interval, "Minimum interrupt out interval in ms"); | |
161 | ||
162 | /* Structure to hold all of our device specific stuff */ | |
163 | struct ld_usb { | |
ce0d7d3f | 164 | struct mutex mutex; /* locks this structure */ |
2824bd25 MH |
165 | struct usb_interface* intf; /* save off the usb interface pointer */ |
166 | ||
167 | int open_count; /* number of times this port has been opened */ | |
168 | ||
169 | char* ring_buffer; | |
170 | unsigned int ring_head; | |
171 | unsigned int ring_tail; | |
172 | ||
173 | wait_queue_head_t read_wait; | |
174 | wait_queue_head_t write_wait; | |
175 | ||
176 | char* interrupt_in_buffer; | |
177 | struct usb_endpoint_descriptor* interrupt_in_endpoint; | |
178 | struct urb* interrupt_in_urb; | |
179 | int interrupt_in_interval; | |
180 | size_t interrupt_in_endpoint_size; | |
181 | int interrupt_in_running; | |
182 | int interrupt_in_done; | |
9d33efd9 ON |
183 | int buffer_overflow; |
184 | spinlock_t rbsl; | |
2824bd25 MH |
185 | |
186 | char* interrupt_out_buffer; | |
187 | struct usb_endpoint_descriptor* interrupt_out_endpoint; | |
188 | struct urb* interrupt_out_urb; | |
189 | int interrupt_out_interval; | |
190 | size_t interrupt_out_endpoint_size; | |
191 | int interrupt_out_busy; | |
192 | }; | |
193 | ||
2824bd25 MH |
194 | static struct usb_driver ld_usb_driver; |
195 | ||
196 | /** | |
197 | * ld_usb_abort_transfers | |
198 | * aborts transfers and frees associated data structures | |
199 | */ | |
200 | static void ld_usb_abort_transfers(struct ld_usb *dev) | |
201 | { | |
202 | /* shutdown transfer */ | |
203 | if (dev->interrupt_in_running) { | |
204 | dev->interrupt_in_running = 0; | |
205 | if (dev->intf) | |
206 | usb_kill_urb(dev->interrupt_in_urb); | |
207 | } | |
208 | if (dev->interrupt_out_busy) | |
209 | if (dev->intf) | |
210 | usb_kill_urb(dev->interrupt_out_urb); | |
211 | } | |
212 | ||
213 | /** | |
214 | * ld_usb_delete | |
215 | */ | |
216 | static void ld_usb_delete(struct ld_usb *dev) | |
217 | { | |
218 | ld_usb_abort_transfers(dev); | |
219 | ||
220 | /* free data structures */ | |
221 | usb_free_urb(dev->interrupt_in_urb); | |
222 | usb_free_urb(dev->interrupt_out_urb); | |
223 | kfree(dev->ring_buffer); | |
224 | kfree(dev->interrupt_in_buffer); | |
225 | kfree(dev->interrupt_out_buffer); | |
226 | kfree(dev); | |
227 | } | |
228 | ||
229 | /** | |
230 | * ld_usb_interrupt_in_callback | |
231 | */ | |
7d12e780 | 232 | static void ld_usb_interrupt_in_callback(struct urb *urb) |
2824bd25 MH |
233 | { |
234 | struct ld_usb *dev = urb->context; | |
235 | size_t *actual_buffer; | |
236 | unsigned int next_ring_head; | |
491c021e | 237 | int status = urb->status; |
2824bd25 MH |
238 | int retval; |
239 | ||
491c021e GKH |
240 | if (status) { |
241 | if (status == -ENOENT || | |
242 | status == -ECONNRESET || | |
243 | status == -ESHUTDOWN) { | |
2824bd25 MH |
244 | goto exit; |
245 | } else { | |
457163c4 GKH |
246 | dev_dbg(&dev->intf->dev, |
247 | "%s: nonzero status received: %d\n", __func__, | |
248 | status); | |
9d33efd9 | 249 | spin_lock(&dev->rbsl); |
2824bd25 MH |
250 | goto resubmit; /* maybe we can recover */ |
251 | } | |
252 | } | |
253 | ||
9d33efd9 | 254 | spin_lock(&dev->rbsl); |
2824bd25 MH |
255 | if (urb->actual_length > 0) { |
256 | next_ring_head = (dev->ring_head+1) % ring_buffer_size; | |
257 | if (next_ring_head != dev->ring_tail) { | |
258 | actual_buffer = (size_t*)(dev->ring_buffer + dev->ring_head*(sizeof(size_t)+dev->interrupt_in_endpoint_size)); | |
259 | /* actual_buffer gets urb->actual_length + interrupt_in_buffer */ | |
260 | *actual_buffer = urb->actual_length; | |
261 | memcpy(actual_buffer+1, dev->interrupt_in_buffer, urb->actual_length); | |
262 | dev->ring_head = next_ring_head; | |
457163c4 GKH |
263 | dev_dbg(&dev->intf->dev, "%s: received %d bytes\n", |
264 | __func__, urb->actual_length); | |
9d33efd9 | 265 | } else { |
2824bd25 MH |
266 | dev_warn(&dev->intf->dev, |
267 | "Ring buffer overflow, %d bytes dropped\n", | |
268 | urb->actual_length); | |
9d33efd9 ON |
269 | dev->buffer_overflow = 1; |
270 | } | |
2824bd25 MH |
271 | } |
272 | ||
273 | resubmit: | |
274 | /* resubmit if we're still running */ | |
9d33efd9 | 275 | if (dev->interrupt_in_running && !dev->buffer_overflow && dev->intf) { |
2824bd25 | 276 | retval = usb_submit_urb(dev->interrupt_in_urb, GFP_ATOMIC); |
9d33efd9 | 277 | if (retval) { |
2824bd25 MH |
278 | dev_err(&dev->intf->dev, |
279 | "usb_submit_urb failed (%d)\n", retval); | |
9d33efd9 ON |
280 | dev->buffer_overflow = 1; |
281 | } | |
2824bd25 | 282 | } |
9d33efd9 | 283 | spin_unlock(&dev->rbsl); |
2824bd25 MH |
284 | exit: |
285 | dev->interrupt_in_done = 1; | |
286 | wake_up_interruptible(&dev->read_wait); | |
287 | } | |
288 | ||
289 | /** | |
290 | * ld_usb_interrupt_out_callback | |
291 | */ | |
7d12e780 | 292 | static void ld_usb_interrupt_out_callback(struct urb *urb) |
2824bd25 MH |
293 | { |
294 | struct ld_usb *dev = urb->context; | |
491c021e | 295 | int status = urb->status; |
2824bd25 MH |
296 | |
297 | /* sync/async unlink faults aren't errors */ | |
491c021e GKH |
298 | if (status && !(status == -ENOENT || |
299 | status == -ECONNRESET || | |
300 | status == -ESHUTDOWN)) | |
457163c4 GKH |
301 | dev_dbg(&dev->intf->dev, |
302 | "%s - nonzero write interrupt status received: %d\n", | |
303 | __func__, status); | |
2824bd25 MH |
304 | |
305 | dev->interrupt_out_busy = 0; | |
306 | wake_up_interruptible(&dev->write_wait); | |
307 | } | |
308 | ||
309 | /** | |
310 | * ld_usb_open | |
311 | */ | |
312 | static int ld_usb_open(struct inode *inode, struct file *file) | |
313 | { | |
314 | struct ld_usb *dev; | |
315 | int subminor; | |
d4ead16f | 316 | int retval; |
2824bd25 MH |
317 | struct usb_interface *interface; |
318 | ||
319 | nonseekable_open(inode, file); | |
320 | subminor = iminor(inode); | |
321 | ||
2824bd25 MH |
322 | interface = usb_find_interface(&ld_usb_driver, subminor); |
323 | ||
324 | if (!interface) { | |
b22862e5 GKH |
325 | printk(KERN_ERR "%s - error, can't find device for minor %d\n", |
326 | __func__, subminor); | |
d4ead16f | 327 | return -ENODEV; |
2824bd25 MH |
328 | } |
329 | ||
330 | dev = usb_get_intfdata(interface); | |
331 | ||
6248c52f | 332 | if (!dev) |
d4ead16f | 333 | return -ENODEV; |
2824bd25 MH |
334 | |
335 | /* lock this device */ | |
6248c52f | 336 | if (mutex_lock_interruptible(&dev->mutex)) |
d4ead16f | 337 | return -ERESTARTSYS; |
2824bd25 MH |
338 | |
339 | /* allow opening only once */ | |
340 | if (dev->open_count) { | |
341 | retval = -EBUSY; | |
342 | goto unlock_exit; | |
343 | } | |
344 | dev->open_count = 1; | |
345 | ||
346 | /* initialize in direction */ | |
347 | dev->ring_head = 0; | |
348 | dev->ring_tail = 0; | |
9d33efd9 | 349 | dev->buffer_overflow = 0; |
2824bd25 MH |
350 | usb_fill_int_urb(dev->interrupt_in_urb, |
351 | interface_to_usbdev(interface), | |
352 | usb_rcvintpipe(interface_to_usbdev(interface), | |
353 | dev->interrupt_in_endpoint->bEndpointAddress), | |
354 | dev->interrupt_in_buffer, | |
355 | dev->interrupt_in_endpoint_size, | |
356 | ld_usb_interrupt_in_callback, | |
357 | dev, | |
358 | dev->interrupt_in_interval); | |
359 | ||
360 | dev->interrupt_in_running = 1; | |
361 | dev->interrupt_in_done = 0; | |
362 | ||
363 | retval = usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL); | |
364 | if (retval) { | |
365 | dev_err(&interface->dev, "Couldn't submit interrupt_in_urb %d\n", retval); | |
366 | dev->interrupt_in_running = 0; | |
367 | dev->open_count = 0; | |
368 | goto unlock_exit; | |
369 | } | |
370 | ||
371 | /* save device in the file's private structure */ | |
372 | file->private_data = dev; | |
373 | ||
374 | unlock_exit: | |
ce0d7d3f | 375 | mutex_unlock(&dev->mutex); |
2824bd25 | 376 | |
2824bd25 MH |
377 | return retval; |
378 | } | |
379 | ||
380 | /** | |
381 | * ld_usb_release | |
382 | */ | |
383 | static int ld_usb_release(struct inode *inode, struct file *file) | |
384 | { | |
385 | struct ld_usb *dev; | |
386 | int retval = 0; | |
387 | ||
388 | dev = file->private_data; | |
389 | ||
390 | if (dev == NULL) { | |
391 | retval = -ENODEV; | |
392 | goto exit; | |
393 | } | |
394 | ||
ce0d7d3f | 395 | if (mutex_lock_interruptible(&dev->mutex)) { |
2824bd25 MH |
396 | retval = -ERESTARTSYS; |
397 | goto exit; | |
398 | } | |
399 | ||
400 | if (dev->open_count != 1) { | |
401 | retval = -ENODEV; | |
402 | goto unlock_exit; | |
403 | } | |
404 | if (dev->intf == NULL) { | |
405 | /* the device was unplugged before the file was released */ | |
ce0d7d3f | 406 | mutex_unlock(&dev->mutex); |
2824bd25 MH |
407 | /* unlock here as ld_usb_delete frees dev */ |
408 | ld_usb_delete(dev); | |
409 | goto exit; | |
410 | } | |
411 | ||
412 | /* wait until write transfer is finished */ | |
413 | if (dev->interrupt_out_busy) | |
414 | wait_event_interruptible_timeout(dev->write_wait, !dev->interrupt_out_busy, 2 * HZ); | |
415 | ld_usb_abort_transfers(dev); | |
416 | dev->open_count = 0; | |
417 | ||
418 | unlock_exit: | |
ce0d7d3f | 419 | mutex_unlock(&dev->mutex); |
2824bd25 MH |
420 | |
421 | exit: | |
422 | return retval; | |
423 | } | |
424 | ||
425 | /** | |
426 | * ld_usb_poll | |
427 | */ | |
428 | static unsigned int ld_usb_poll(struct file *file, poll_table *wait) | |
429 | { | |
430 | struct ld_usb *dev; | |
431 | unsigned int mask = 0; | |
432 | ||
433 | dev = file->private_data; | |
434 | ||
d12b85e7 ON |
435 | if (!dev->intf) |
436 | return POLLERR | POLLHUP; | |
437 | ||
2824bd25 MH |
438 | poll_wait(file, &dev->read_wait, wait); |
439 | poll_wait(file, &dev->write_wait, wait); | |
440 | ||
441 | if (dev->ring_head != dev->ring_tail) | |
442 | mask |= POLLIN | POLLRDNORM; | |
443 | if (!dev->interrupt_out_busy) | |
444 | mask |= POLLOUT | POLLWRNORM; | |
445 | ||
446 | return mask; | |
447 | } | |
448 | ||
449 | /** | |
450 | * ld_usb_read | |
451 | */ | |
452 | static ssize_t ld_usb_read(struct file *file, char __user *buffer, size_t count, | |
453 | loff_t *ppos) | |
454 | { | |
455 | struct ld_usb *dev; | |
456 | size_t *actual_buffer; | |
457 | size_t bytes_to_read; | |
458 | int retval = 0; | |
9d33efd9 | 459 | int rv; |
2824bd25 MH |
460 | |
461 | dev = file->private_data; | |
462 | ||
463 | /* verify that we actually have some data to read */ | |
464 | if (count == 0) | |
465 | goto exit; | |
466 | ||
467 | /* lock this object */ | |
ce0d7d3f | 468 | if (mutex_lock_interruptible(&dev->mutex)) { |
2824bd25 MH |
469 | retval = -ERESTARTSYS; |
470 | goto exit; | |
471 | } | |
472 | ||
473 | /* verify that the device wasn't unplugged */ | |
474 | if (dev->intf == NULL) { | |
475 | retval = -ENODEV; | |
b22862e5 | 476 | printk(KERN_ERR "ldusb: No device or device unplugged %d\n", retval); |
2824bd25 MH |
477 | goto unlock_exit; |
478 | } | |
479 | ||
480 | /* wait for data */ | |
9d33efd9 | 481 | spin_lock_irq(&dev->rbsl); |
2824bd25 | 482 | if (dev->ring_head == dev->ring_tail) { |
9d33efd9 ON |
483 | dev->interrupt_in_done = 0; |
484 | spin_unlock_irq(&dev->rbsl); | |
2824bd25 MH |
485 | if (file->f_flags & O_NONBLOCK) { |
486 | retval = -EAGAIN; | |
487 | goto unlock_exit; | |
488 | } | |
489 | retval = wait_event_interruptible(dev->read_wait, dev->interrupt_in_done); | |
490 | if (retval < 0) | |
491 | goto unlock_exit; | |
9d33efd9 ON |
492 | } else { |
493 | spin_unlock_irq(&dev->rbsl); | |
2824bd25 MH |
494 | } |
495 | ||
496 | /* actual_buffer contains actual_length + interrupt_in_buffer */ | |
497 | actual_buffer = (size_t*)(dev->ring_buffer + dev->ring_tail*(sizeof(size_t)+dev->interrupt_in_endpoint_size)); | |
498 | bytes_to_read = min(count, *actual_buffer); | |
499 | if (bytes_to_read < *actual_buffer) | |
dd7d5008 | 500 | dev_warn(&dev->intf->dev, "Read buffer overflow, %zd bytes dropped\n", |
2824bd25 MH |
501 | *actual_buffer-bytes_to_read); |
502 | ||
503 | /* copy one interrupt_in_buffer from ring_buffer into userspace */ | |
504 | if (copy_to_user(buffer, actual_buffer+1, bytes_to_read)) { | |
505 | retval = -EFAULT; | |
506 | goto unlock_exit; | |
507 | } | |
508 | dev->ring_tail = (dev->ring_tail+1) % ring_buffer_size; | |
509 | ||
510 | retval = bytes_to_read; | |
511 | ||
9d33efd9 ON |
512 | spin_lock_irq(&dev->rbsl); |
513 | if (dev->buffer_overflow) { | |
514 | dev->buffer_overflow = 0; | |
515 | spin_unlock_irq(&dev->rbsl); | |
516 | rv = usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL); | |
517 | if (rv < 0) | |
518 | dev->buffer_overflow = 1; | |
519 | } else { | |
520 | spin_unlock_irq(&dev->rbsl); | |
521 | } | |
522 | ||
2824bd25 MH |
523 | unlock_exit: |
524 | /* unlock the device */ | |
ce0d7d3f | 525 | mutex_unlock(&dev->mutex); |
2824bd25 MH |
526 | |
527 | exit: | |
528 | return retval; | |
529 | } | |
530 | ||
531 | /** | |
532 | * ld_usb_write | |
533 | */ | |
534 | static ssize_t ld_usb_write(struct file *file, const char __user *buffer, | |
535 | size_t count, loff_t *ppos) | |
536 | { | |
537 | struct ld_usb *dev; | |
538 | size_t bytes_to_write; | |
539 | int retval = 0; | |
540 | ||
541 | dev = file->private_data; | |
542 | ||
543 | /* verify that we actually have some data to write */ | |
544 | if (count == 0) | |
545 | goto exit; | |
546 | ||
547 | /* lock this object */ | |
ce0d7d3f | 548 | if (mutex_lock_interruptible(&dev->mutex)) { |
2824bd25 MH |
549 | retval = -ERESTARTSYS; |
550 | goto exit; | |
551 | } | |
552 | ||
553 | /* verify that the device wasn't unplugged */ | |
554 | if (dev->intf == NULL) { | |
555 | retval = -ENODEV; | |
b22862e5 | 556 | printk(KERN_ERR "ldusb: No device or device unplugged %d\n", retval); |
2824bd25 MH |
557 | goto unlock_exit; |
558 | } | |
559 | ||
560 | /* wait until previous transfer is finished */ | |
561 | if (dev->interrupt_out_busy) { | |
562 | if (file->f_flags & O_NONBLOCK) { | |
563 | retval = -EAGAIN; | |
564 | goto unlock_exit; | |
565 | } | |
566 | retval = wait_event_interruptible(dev->write_wait, !dev->interrupt_out_busy); | |
567 | if (retval < 0) { | |
568 | goto unlock_exit; | |
569 | } | |
570 | } | |
571 | ||
572 | /* write the data into interrupt_out_buffer from userspace */ | |
573 | bytes_to_write = min(count, write_buffer_size*dev->interrupt_out_endpoint_size); | |
574 | if (bytes_to_write < count) | |
dd7d5008 | 575 | dev_warn(&dev->intf->dev, "Write buffer overflow, %zd bytes dropped\n",count-bytes_to_write); |
457163c4 GKH |
576 | dev_dbg(&dev->intf->dev, "%s: count = %zd, bytes_to_write = %zd\n", |
577 | __func__, count, bytes_to_write); | |
2824bd25 MH |
578 | |
579 | if (copy_from_user(dev->interrupt_out_buffer, buffer, bytes_to_write)) { | |
580 | retval = -EFAULT; | |
581 | goto unlock_exit; | |
582 | } | |
583 | ||
584 | if (dev->interrupt_out_endpoint == NULL) { | |
585 | /* try HID_REQ_SET_REPORT=9 on control_endpoint instead of interrupt_out_endpoint */ | |
586 | retval = usb_control_msg(interface_to_usbdev(dev->intf), | |
587 | usb_sndctrlpipe(interface_to_usbdev(dev->intf), 0), | |
588 | 9, | |
589 | USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT, | |
590 | 1 << 8, 0, | |
591 | dev->interrupt_out_buffer, | |
592 | bytes_to_write, | |
593 | USB_CTRL_SET_TIMEOUT * HZ); | |
594 | if (retval < 0) | |
b22862e5 GKH |
595 | dev_err(&dev->intf->dev, |
596 | "Couldn't submit HID_REQ_SET_REPORT %d\n", | |
597 | retval); | |
2824bd25 MH |
598 | goto unlock_exit; |
599 | } | |
600 | ||
601 | /* send off the urb */ | |
602 | usb_fill_int_urb(dev->interrupt_out_urb, | |
603 | interface_to_usbdev(dev->intf), | |
604 | usb_sndintpipe(interface_to_usbdev(dev->intf), | |
605 | dev->interrupt_out_endpoint->bEndpointAddress), | |
606 | dev->interrupt_out_buffer, | |
607 | bytes_to_write, | |
608 | ld_usb_interrupt_out_callback, | |
609 | dev, | |
610 | dev->interrupt_out_interval); | |
611 | ||
612 | dev->interrupt_out_busy = 1; | |
613 | wmb(); | |
614 | ||
615 | retval = usb_submit_urb(dev->interrupt_out_urb, GFP_KERNEL); | |
616 | if (retval) { | |
617 | dev->interrupt_out_busy = 0; | |
b22862e5 GKH |
618 | dev_err(&dev->intf->dev, |
619 | "Couldn't submit interrupt_out_urb %d\n", retval); | |
2824bd25 MH |
620 | goto unlock_exit; |
621 | } | |
622 | retval = bytes_to_write; | |
623 | ||
624 | unlock_exit: | |
625 | /* unlock the device */ | |
ce0d7d3f | 626 | mutex_unlock(&dev->mutex); |
2824bd25 MH |
627 | |
628 | exit: | |
629 | return retval; | |
630 | } | |
631 | ||
632 | /* file operations needed when we register this driver */ | |
066202dd | 633 | static const struct file_operations ld_usb_fops = { |
2824bd25 MH |
634 | .owner = THIS_MODULE, |
635 | .read = ld_usb_read, | |
636 | .write = ld_usb_write, | |
637 | .open = ld_usb_open, | |
638 | .release = ld_usb_release, | |
639 | .poll = ld_usb_poll, | |
6038f373 | 640 | .llseek = no_llseek, |
2824bd25 MH |
641 | }; |
642 | ||
643 | /* | |
644 | * usb class driver info in order to get a minor number from the usb core, | |
595b14cb | 645 | * and to have the device registered with the driver core |
2824bd25 MH |
646 | */ |
647 | static struct usb_class_driver ld_usb_class = { | |
648 | .name = "ldusb%d", | |
649 | .fops = &ld_usb_fops, | |
650 | .minor_base = USB_LD_MINOR_BASE, | |
651 | }; | |
652 | ||
653 | /** | |
654 | * ld_usb_probe | |
655 | * | |
656 | * Called by the usb core when a new device is connected that it thinks | |
657 | * this driver might be interested in. | |
658 | */ | |
659 | static int ld_usb_probe(struct usb_interface *intf, const struct usb_device_id *id) | |
660 | { | |
661 | struct usb_device *udev = interface_to_usbdev(intf); | |
662 | struct ld_usb *dev = NULL; | |
663 | struct usb_host_interface *iface_desc; | |
664 | struct usb_endpoint_descriptor *endpoint; | |
665 | char *buffer; | |
666 | int i; | |
667 | int retval = -ENOMEM; | |
668 | ||
b595076a | 669 | /* allocate memory for our device state and initialize it */ |
2824bd25 | 670 | |
1144cf7a | 671 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
2824bd25 MH |
672 | if (dev == NULL) { |
673 | dev_err(&intf->dev, "Out of memory\n"); | |
674 | goto exit; | |
675 | } | |
ce0d7d3f | 676 | mutex_init(&dev->mutex); |
9d33efd9 | 677 | spin_lock_init(&dev->rbsl); |
2824bd25 MH |
678 | dev->intf = intf; |
679 | init_waitqueue_head(&dev->read_wait); | |
680 | init_waitqueue_head(&dev->write_wait); | |
681 | ||
682 | /* workaround for early firmware versions on fast computers */ | |
683 | if ((le16_to_cpu(udev->descriptor.idVendor) == USB_VENDOR_ID_LD) && | |
ba3e66e9 MH |
684 | ((le16_to_cpu(udev->descriptor.idProduct) == USB_DEVICE_ID_LD_CASSY) || |
685 | (le16_to_cpu(udev->descriptor.idProduct) == USB_DEVICE_ID_LD_COM3LAB)) && | |
2824bd25 MH |
686 | (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x103)) { |
687 | buffer = kmalloc(256, GFP_KERNEL); | |
a6db592e MH |
688 | if (buffer == NULL) { |
689 | dev_err(&intf->dev, "Couldn't allocate string buffer\n"); | |
690 | goto error; | |
691 | } | |
2824bd25 MH |
692 | /* usb_string makes SETUP+STALL to leave always ControlReadLoop */ |
693 | usb_string(udev, 255, buffer, 256); | |
694 | kfree(buffer); | |
695 | } | |
696 | ||
697 | iface_desc = intf->cur_altsetting; | |
698 | ||
699 | /* set up the endpoint information */ | |
700 | for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) { | |
701 | endpoint = &iface_desc->endpoint[i].desc; | |
702 | ||
5482687b | 703 | if (usb_endpoint_is_int_in(endpoint)) |
2824bd25 | 704 | dev->interrupt_in_endpoint = endpoint; |
2824bd25 | 705 | |
5482687b | 706 | if (usb_endpoint_is_int_out(endpoint)) |
2824bd25 | 707 | dev->interrupt_out_endpoint = endpoint; |
2824bd25 MH |
708 | } |
709 | if (dev->interrupt_in_endpoint == NULL) { | |
710 | dev_err(&intf->dev, "Interrupt in endpoint not found\n"); | |
711 | goto error; | |
712 | } | |
713 | if (dev->interrupt_out_endpoint == NULL) | |
714 | dev_warn(&intf->dev, "Interrupt out endpoint not found (using control endpoint instead)\n"); | |
715 | ||
29cc8897 | 716 | dev->interrupt_in_endpoint_size = usb_endpoint_maxp(dev->interrupt_in_endpoint); |
2824bd25 MH |
717 | dev->ring_buffer = kmalloc(ring_buffer_size*(sizeof(size_t)+dev->interrupt_in_endpoint_size), GFP_KERNEL); |
718 | if (!dev->ring_buffer) { | |
719 | dev_err(&intf->dev, "Couldn't allocate ring_buffer\n"); | |
720 | goto error; | |
721 | } | |
722 | dev->interrupt_in_buffer = kmalloc(dev->interrupt_in_endpoint_size, GFP_KERNEL); | |
723 | if (!dev->interrupt_in_buffer) { | |
724 | dev_err(&intf->dev, "Couldn't allocate interrupt_in_buffer\n"); | |
725 | goto error; | |
726 | } | |
727 | dev->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL); | |
728 | if (!dev->interrupt_in_urb) { | |
729 | dev_err(&intf->dev, "Couldn't allocate interrupt_in_urb\n"); | |
730 | goto error; | |
731 | } | |
29cc8897 | 732 | dev->interrupt_out_endpoint_size = dev->interrupt_out_endpoint ? usb_endpoint_maxp(dev->interrupt_out_endpoint) : |
2824bd25 MH |
733 | udev->descriptor.bMaxPacketSize0; |
734 | dev->interrupt_out_buffer = kmalloc(write_buffer_size*dev->interrupt_out_endpoint_size, GFP_KERNEL); | |
735 | if (!dev->interrupt_out_buffer) { | |
736 | dev_err(&intf->dev, "Couldn't allocate interrupt_out_buffer\n"); | |
737 | goto error; | |
738 | } | |
739 | dev->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL); | |
740 | if (!dev->interrupt_out_urb) { | |
741 | dev_err(&intf->dev, "Couldn't allocate interrupt_out_urb\n"); | |
742 | goto error; | |
743 | } | |
744 | dev->interrupt_in_interval = min_interrupt_in_interval > dev->interrupt_in_endpoint->bInterval ? min_interrupt_in_interval : dev->interrupt_in_endpoint->bInterval; | |
745 | if (dev->interrupt_out_endpoint) | |
746 | dev->interrupt_out_interval = min_interrupt_out_interval > dev->interrupt_out_endpoint->bInterval ? min_interrupt_out_interval : dev->interrupt_out_endpoint->bInterval; | |
747 | ||
748 | /* we can register the device now, as it is ready */ | |
749 | usb_set_intfdata(intf, dev); | |
750 | ||
751 | retval = usb_register_dev(intf, &ld_usb_class); | |
752 | if (retval) { | |
753 | /* something prevented us from registering this driver */ | |
754 | dev_err(&intf->dev, "Not able to get a minor for this device.\n"); | |
755 | usb_set_intfdata(intf, NULL); | |
756 | goto error; | |
757 | } | |
758 | ||
759 | /* let the user know what node this device is now attached to */ | |
760 | dev_info(&intf->dev, "LD USB Device #%d now attached to major %d minor %d\n", | |
761 | (intf->minor - USB_LD_MINOR_BASE), USB_MAJOR, intf->minor); | |
762 | ||
763 | exit: | |
764 | return retval; | |
765 | ||
766 | error: | |
767 | ld_usb_delete(dev); | |
768 | ||
769 | return retval; | |
770 | } | |
771 | ||
772 | /** | |
773 | * ld_usb_disconnect | |
774 | * | |
775 | * Called by the usb core when the device is removed from the system. | |
776 | */ | |
777 | static void ld_usb_disconnect(struct usb_interface *intf) | |
778 | { | |
779 | struct ld_usb *dev; | |
780 | int minor; | |
781 | ||
2824bd25 MH |
782 | dev = usb_get_intfdata(intf); |
783 | usb_set_intfdata(intf, NULL); | |
784 | ||
2824bd25 MH |
785 | minor = intf->minor; |
786 | ||
787 | /* give back our minor */ | |
788 | usb_deregister_dev(intf, &ld_usb_class); | |
789 | ||
ce0d7d3f | 790 | mutex_lock(&dev->mutex); |
d4ead16f | 791 | |
2824bd25 MH |
792 | /* if the device is not opened, then we clean up right now */ |
793 | if (!dev->open_count) { | |
ce0d7d3f | 794 | mutex_unlock(&dev->mutex); |
2824bd25 MH |
795 | ld_usb_delete(dev); |
796 | } else { | |
797 | dev->intf = NULL; | |
d12b85e7 ON |
798 | /* wake up pollers */ |
799 | wake_up_interruptible_all(&dev->read_wait); | |
800 | wake_up_interruptible_all(&dev->write_wait); | |
ce0d7d3f | 801 | mutex_unlock(&dev->mutex); |
2824bd25 MH |
802 | } |
803 | ||
2824bd25 MH |
804 | dev_info(&intf->dev, "LD USB Device #%d now disconnected\n", |
805 | (minor - USB_LD_MINOR_BASE)); | |
806 | } | |
807 | ||
808 | /* usb specific object needed to register this driver with the usb subsystem */ | |
809 | static struct usb_driver ld_usb_driver = { | |
2824bd25 MH |
810 | .name = "ldusb", |
811 | .probe = ld_usb_probe, | |
812 | .disconnect = ld_usb_disconnect, | |
813 | .id_table = ld_usb_table, | |
814 | }; | |
815 | ||
65db4305 | 816 | module_usb_driver(ld_usb_driver); |
2824bd25 | 817 |