Commit | Line | Data |
---|---|---|
5fd54ace | 1 | // SPDX-License-Identifier: GPL-2.0 |
946b960d GKH |
2 | /* |
3 | * Native support for the I/O-Warrior USB devices | |
4 | * | |
5 | * Copyright (c) 2003-2005 Code Mercenaries GmbH | |
6 | * written by Christian Lucht <lucht@codemercs.com> | |
7 | * | |
8 | * based on | |
9 | ||
10 | * usb-skeleton.c by Greg Kroah-Hartman <greg@kroah.com> | |
11 | * brlvger.c by Stephane Dalton <sdalton@videotron.ca> | |
2ba277a0 | 12 | * and Stephane Doyon <s.doyon@videotron.ca> |
946b960d GKH |
13 | * |
14 | * Released under the GPLv2. | |
15 | */ | |
16 | ||
17 | #include <linux/module.h> | |
18 | #include <linux/usb.h> | |
946b960d GKH |
19 | #include <linux/slab.h> |
20 | #include <linux/sched.h> | |
925ce689 | 21 | #include <linux/mutex.h> |
946b960d | 22 | #include <linux/poll.h> |
946b960d GKH |
23 | #include <linux/usb/iowarrior.h> |
24 | ||
946b960d | 25 | #define DRIVER_AUTHOR "Christian Lucht <lucht@codemercs.com>" |
c35c376f | 26 | #define DRIVER_DESC "USB IO-Warrior driver" |
946b960d GKH |
27 | |
28 | #define USB_VENDOR_ID_CODEMERCS 1984 | |
29 | /* low speed iowarrior */ | |
30 | #define USB_DEVICE_ID_CODEMERCS_IOW40 0x1500 | |
31 | #define USB_DEVICE_ID_CODEMERCS_IOW24 0x1501 | |
32 | #define USB_DEVICE_ID_CODEMERCS_IOWPV1 0x1511 | |
33 | #define USB_DEVICE_ID_CODEMERCS_IOWPV2 0x1512 | |
34 | /* full speed iowarrior */ | |
35 | #define USB_DEVICE_ID_CODEMERCS_IOW56 0x1503 | |
5f6f8da2 GKH |
36 | /* fuller speed iowarrior */ |
37 | #define USB_DEVICE_ID_CODEMERCS_IOW28 0x1504 | |
38 | #define USB_DEVICE_ID_CODEMERCS_IOW28L 0x1505 | |
bab5417f | 39 | #define USB_DEVICE_ID_CODEMERCS_IOW100 0x1506 |
946b960d | 40 | |
461d8deb GKH |
41 | /* OEMed devices */ |
42 | #define USB_DEVICE_ID_CODEMERCS_IOW24SAG 0x158a | |
43 | #define USB_DEVICE_ID_CODEMERCS_IOW56AM 0x158b | |
44 | ||
946b960d GKH |
45 | /* Get a minor range for your devices from the usb maintainer */ |
46 | #ifdef CONFIG_USB_DYNAMIC_MINORS | |
47 | #define IOWARRIOR_MINOR_BASE 0 | |
48 | #else | |
25985edc | 49 | #define IOWARRIOR_MINOR_BASE 208 // SKELETON_MINOR_BASE 192 + 16, not official yet |
946b960d GKH |
50 | #endif |
51 | ||
52 | /* interrupt input queue size */ | |
53 | #define MAX_INTERRUPT_BUFFER 16 | |
54 | /* | |
55 | maximum number of urbs that are submitted for writes at the same time, | |
56 | this applies to the IOWarrior56 only! | |
57 | IOWarrior24 and IOWarrior40 use synchronous usb_control_msg calls. | |
58 | */ | |
59 | #define MAX_WRITES_IN_FLIGHT 4 | |
60 | ||
946b960d GKH |
61 | MODULE_AUTHOR(DRIVER_AUTHOR); |
62 | MODULE_DESCRIPTION(DRIVER_DESC); | |
63 | MODULE_LICENSE("GPL"); | |
64 | ||
946b960d GKH |
65 | static struct usb_driver iowarrior_driver; |
66 | ||
67 | /*--------------*/ | |
68 | /* data */ | |
69 | /*--------------*/ | |
70 | ||
71 | /* Structure to hold all of our device specific stuff */ | |
72 | struct iowarrior { | |
73 | struct mutex mutex; /* locks this structure */ | |
74 | struct usb_device *udev; /* save off the usb device pointer */ | |
75 | struct usb_interface *interface; /* the interface for this device */ | |
76 | unsigned char minor; /* the starting minor number for this device */ | |
77 | struct usb_endpoint_descriptor *int_out_endpoint; /* endpoint for reading (needed for IOW56 only) */ | |
78 | struct usb_endpoint_descriptor *int_in_endpoint; /* endpoint for reading */ | |
79 | struct urb *int_in_urb; /* the urb for reading data */ | |
80 | unsigned char *int_in_buffer; /* buffer for data to be read */ | |
81 | unsigned char serial_number; /* to detect lost packages */ | |
82 | unsigned char *read_queue; /* size is MAX_INTERRUPT_BUFFER * packet size */ | |
83 | wait_queue_head_t read_wait; | |
84 | wait_queue_head_t write_wait; /* wait-queue for writing to the device */ | |
85 | atomic_t write_busy; /* number of write-urbs submitted */ | |
86 | atomic_t read_idx; | |
87 | atomic_t intr_idx; | |
946b960d GKH |
88 | atomic_t overflow_flag; /* signals an index 'rollover' */ |
89 | int present; /* this is 1 as long as the device is connected */ | |
90 | int opened; /* this is 1 if the device is currently open */ | |
91 | char chip_serial[9]; /* the serial number string of the chip connected */ | |
92 | int report_size; /* number of bytes in a report */ | |
93 | u16 product_id; | |
b5f8d468 | 94 | struct usb_anchor submitted; |
946b960d GKH |
95 | }; |
96 | ||
97 | /*--------------*/ | |
98 | /* globals */ | |
99 | /*--------------*/ | |
946b960d GKH |
100 | |
101 | /* | |
102 | * USB spec identifies 5 second timeouts. | |
103 | */ | |
104 | #define GET_TIMEOUT 5 | |
105 | #define USB_REQ_GET_REPORT 0x01 | |
106 | //#if 0 | |
107 | static int usb_get_report(struct usb_device *dev, | |
108 | struct usb_host_interface *inter, unsigned char type, | |
109 | unsigned char id, void *buf, int size) | |
110 | { | |
111 | return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), | |
112 | USB_REQ_GET_REPORT, | |
113 | USB_DIR_IN | USB_TYPE_CLASS | | |
114 | USB_RECIP_INTERFACE, (type << 8) + id, | |
115 | inter->desc.bInterfaceNumber, buf, size, | |
147c5a17 | 116 | GET_TIMEOUT*HZ); |
946b960d GKH |
117 | } |
118 | //#endif | |
119 | ||
120 | #define USB_REQ_SET_REPORT 0x09 | |
121 | ||
122 | static int usb_set_report(struct usb_interface *intf, unsigned char type, | |
123 | unsigned char id, void *buf, int size) | |
124 | { | |
125 | return usb_control_msg(interface_to_usbdev(intf), | |
126 | usb_sndctrlpipe(interface_to_usbdev(intf), 0), | |
127 | USB_REQ_SET_REPORT, | |
128 | USB_TYPE_CLASS | USB_RECIP_INTERFACE, | |
129 | (type << 8) + id, | |
130 | intf->cur_altsetting->desc.bInterfaceNumber, buf, | |
147c5a17 | 131 | size, HZ); |
946b960d GKH |
132 | } |
133 | ||
134 | /*---------------------*/ | |
135 | /* driver registration */ | |
136 | /*---------------------*/ | |
137 | /* table of devices that work with this driver */ | |
33b9e162 | 138 | static const struct usb_device_id iowarrior_ids[] = { |
946b960d GKH |
139 | {USB_DEVICE(USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOW40)}, |
140 | {USB_DEVICE(USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOW24)}, | |
141 | {USB_DEVICE(USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOWPV1)}, | |
142 | {USB_DEVICE(USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOWPV2)}, | |
143 | {USB_DEVICE(USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOW56)}, | |
461d8deb GKH |
144 | {USB_DEVICE(USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOW24SAG)}, |
145 | {USB_DEVICE(USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOW56AM)}, | |
5f6f8da2 GKH |
146 | {USB_DEVICE(USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOW28)}, |
147 | {USB_DEVICE(USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOW28L)}, | |
bab5417f | 148 | {USB_DEVICE(USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOW100)}, |
946b960d GKH |
149 | {} /* Terminating entry */ |
150 | }; | |
151 | MODULE_DEVICE_TABLE(usb, iowarrior_ids); | |
152 | ||
153 | /* | |
154 | * USB callback handler for reading data | |
155 | */ | |
156 | static void iowarrior_callback(struct urb *urb) | |
157 | { | |
cdc97792 | 158 | struct iowarrior *dev = urb->context; |
946b960d GKH |
159 | int intr_idx; |
160 | int read_idx; | |
161 | int aux_idx; | |
162 | int offset; | |
fb3abee6 GKH |
163 | int status = urb->status; |
164 | int retval; | |
946b960d | 165 | |
fb3abee6 | 166 | switch (status) { |
946b960d GKH |
167 | case 0: |
168 | /* success */ | |
169 | break; | |
170 | case -ECONNRESET: | |
171 | case -ENOENT: | |
172 | case -ESHUTDOWN: | |
173 | return; | |
174 | default: | |
175 | goto exit; | |
176 | } | |
177 | ||
946b960d GKH |
178 | intr_idx = atomic_read(&dev->intr_idx); |
179 | /* aux_idx become previous intr_idx */ | |
180 | aux_idx = (intr_idx == 0) ? (MAX_INTERRUPT_BUFFER - 1) : (intr_idx - 1); | |
181 | read_idx = atomic_read(&dev->read_idx); | |
182 | ||
183 | /* queue is not empty and it's interface 0 */ | |
184 | if ((intr_idx != read_idx) | |
185 | && (dev->interface->cur_altsetting->desc.bInterfaceNumber == 0)) { | |
186 | /* + 1 for serial number */ | |
187 | offset = aux_idx * (dev->report_size + 1); | |
188 | if (!memcmp | |
189 | (dev->read_queue + offset, urb->transfer_buffer, | |
190 | dev->report_size)) { | |
191 | /* equal values on interface 0 will be ignored */ | |
946b960d GKH |
192 | goto exit; |
193 | } | |
194 | } | |
195 | ||
196 | /* aux_idx become next intr_idx */ | |
197 | aux_idx = (intr_idx == (MAX_INTERRUPT_BUFFER - 1)) ? 0 : (intr_idx + 1); | |
198 | if (read_idx == aux_idx) { | |
199 | /* queue full, dropping oldest input */ | |
200 | read_idx = (++read_idx == MAX_INTERRUPT_BUFFER) ? 0 : read_idx; | |
201 | atomic_set(&dev->read_idx, read_idx); | |
202 | atomic_set(&dev->overflow_flag, 1); | |
203 | } | |
204 | ||
205 | /* +1 for serial number */ | |
206 | offset = intr_idx * (dev->report_size + 1); | |
207 | memcpy(dev->read_queue + offset, urb->transfer_buffer, | |
208 | dev->report_size); | |
209 | *(dev->read_queue + offset + (dev->report_size)) = dev->serial_number++; | |
210 | ||
211 | atomic_set(&dev->intr_idx, aux_idx); | |
946b960d GKH |
212 | /* tell the blocking read about the new data */ |
213 | wake_up_interruptible(&dev->read_wait); | |
214 | ||
215 | exit: | |
fb3abee6 GKH |
216 | retval = usb_submit_urb(urb, GFP_ATOMIC); |
217 | if (retval) | |
898eb71c | 218 | dev_err(&dev->interface->dev, "%s - usb_submit_urb failed with result %d\n", |
441b62c1 | 219 | __func__, retval); |
946b960d GKH |
220 | |
221 | } | |
222 | ||
223 | /* | |
224 | * USB Callback handler for write-ops | |
225 | */ | |
226 | static void iowarrior_write_callback(struct urb *urb) | |
227 | { | |
228 | struct iowarrior *dev; | |
fb3abee6 GKH |
229 | int status = urb->status; |
230 | ||
cdc97792 | 231 | dev = urb->context; |
946b960d | 232 | /* sync/async unlink faults aren't errors */ |
fb3abee6 GKH |
233 | if (status && |
234 | !(status == -ENOENT || | |
235 | status == -ECONNRESET || status == -ESHUTDOWN)) { | |
549e8350 JP |
236 | dev_dbg(&dev->interface->dev, |
237 | "nonzero write bulk status received: %d\n", status); | |
946b960d GKH |
238 | } |
239 | /* free up our allocated buffer */ | |
997ea58e DM |
240 | usb_free_coherent(urb->dev, urb->transfer_buffer_length, |
241 | urb->transfer_buffer, urb->transfer_dma); | |
946b960d GKH |
242 | /* tell a waiting writer the interrupt-out-pipe is available again */ |
243 | atomic_dec(&dev->write_busy); | |
244 | wake_up_interruptible(&dev->write_wait); | |
245 | } | |
246 | ||
247 | /** | |
248 | * iowarrior_delete | |
249 | */ | |
250 | static inline void iowarrior_delete(struct iowarrior *dev) | |
251 | { | |
549e8350 | 252 | dev_dbg(&dev->interface->dev, "minor %d\n", dev->minor); |
946b960d GKH |
253 | kfree(dev->int_in_buffer); |
254 | usb_free_urb(dev->int_in_urb); | |
255 | kfree(dev->read_queue); | |
80cd5479 | 256 | usb_put_intf(dev->interface); |
946b960d GKH |
257 | kfree(dev); |
258 | } | |
259 | ||
260 | /*---------------------*/ | |
261 | /* fops implementation */ | |
262 | /*---------------------*/ | |
263 | ||
264 | static int read_index(struct iowarrior *dev) | |
265 | { | |
266 | int intr_idx, read_idx; | |
267 | ||
268 | read_idx = atomic_read(&dev->read_idx); | |
269 | intr_idx = atomic_read(&dev->intr_idx); | |
270 | ||
271 | return (read_idx == intr_idx ? -1 : read_idx); | |
272 | } | |
273 | ||
274 | /** | |
275 | * iowarrior_read | |
276 | */ | |
277 | static ssize_t iowarrior_read(struct file *file, char __user *buffer, | |
278 | size_t count, loff_t *ppos) | |
279 | { | |
280 | struct iowarrior *dev; | |
281 | int read_idx; | |
282 | int offset; | |
283 | ||
5bd6e8b3 | 284 | dev = file->private_data; |
946b960d GKH |
285 | |
286 | /* verify that the device wasn't unplugged */ | |
3cfb4842 | 287 | if (!dev || !dev->present) |
946b960d GKH |
288 | return -ENODEV; |
289 | ||
549e8350 JP |
290 | dev_dbg(&dev->interface->dev, "minor %d, count = %zd\n", |
291 | dev->minor, count); | |
946b960d GKH |
292 | |
293 | /* read count must be packet size (+ time stamp) */ | |
294 | if ((count != dev->report_size) | |
295 | && (count != (dev->report_size + 1))) | |
296 | return -EINVAL; | |
297 | ||
298 | /* repeat until no buffer overrun in callback handler occur */ | |
299 | do { | |
300 | atomic_set(&dev->overflow_flag, 0); | |
301 | if ((read_idx = read_index(dev)) == -1) { | |
f38f1418 | 302 | /* queue empty */ |
946b960d GKH |
303 | if (file->f_flags & O_NONBLOCK) |
304 | return -EAGAIN; | |
305 | else { | |
306 | //next line will return when there is either new data, or the device is unplugged | |
307 | int r = wait_event_interruptible(dev->read_wait, | |
308 | (!dev->present | |
309 | || (read_idx = | |
310 | read_index | |
311 | (dev)) != | |
312 | -1)); | |
313 | if (r) { | |
314 | //we were interrupted by a signal | |
315 | return -ERESTART; | |
316 | } | |
317 | if (!dev->present) { | |
318 | //The device was unplugged | |
319 | return -ENODEV; | |
320 | } | |
321 | if (read_idx == -1) { | |
322 | // Can this happen ??? | |
323 | return 0; | |
324 | } | |
325 | } | |
326 | } | |
327 | ||
328 | offset = read_idx * (dev->report_size + 1); | |
329 | if (copy_to_user(buffer, dev->read_queue + offset, count)) { | |
330 | return -EFAULT; | |
331 | } | |
332 | } while (atomic_read(&dev->overflow_flag)); | |
333 | ||
334 | read_idx = ++read_idx == MAX_INTERRUPT_BUFFER ? 0 : read_idx; | |
335 | atomic_set(&dev->read_idx, read_idx); | |
336 | return count; | |
337 | } | |
338 | ||
339 | /* | |
340 | * iowarrior_write | |
341 | */ | |
342 | static ssize_t iowarrior_write(struct file *file, | |
343 | const char __user *user_buffer, | |
344 | size_t count, loff_t *ppos) | |
345 | { | |
346 | struct iowarrior *dev; | |
347 | int retval = 0; | |
348 | char *buf = NULL; /* for IOW24 and IOW56 we need a buffer */ | |
349 | struct urb *int_out_urb = NULL; | |
350 | ||
5bd6e8b3 | 351 | dev = file->private_data; |
946b960d GKH |
352 | |
353 | mutex_lock(&dev->mutex); | |
354 | /* verify that the device wasn't unplugged */ | |
e28c6a77 | 355 | if (!dev->present) { |
946b960d GKH |
356 | retval = -ENODEV; |
357 | goto exit; | |
358 | } | |
549e8350 JP |
359 | dev_dbg(&dev->interface->dev, "minor %d, count = %zd\n", |
360 | dev->minor, count); | |
946b960d GKH |
361 | /* if count is 0 we're already done */ |
362 | if (count == 0) { | |
363 | retval = 0; | |
364 | goto exit; | |
365 | } | |
366 | /* We only accept full reports */ | |
367 | if (count != dev->report_size) { | |
368 | retval = -EINVAL; | |
369 | goto exit; | |
370 | } | |
371 | switch (dev->product_id) { | |
372 | case USB_DEVICE_ID_CODEMERCS_IOW24: | |
461d8deb | 373 | case USB_DEVICE_ID_CODEMERCS_IOW24SAG: |
946b960d GKH |
374 | case USB_DEVICE_ID_CODEMERCS_IOWPV1: |
375 | case USB_DEVICE_ID_CODEMERCS_IOWPV2: | |
376 | case USB_DEVICE_ID_CODEMERCS_IOW40: | |
377 | /* IOW24 and IOW40 use a synchronous call */ | |
ca2ef0d5 GT |
378 | buf = memdup_user(user_buffer, count); |
379 | if (IS_ERR(buf)) { | |
380 | retval = PTR_ERR(buf); | |
946b960d GKH |
381 | goto exit; |
382 | } | |
383 | retval = usb_set_report(dev->interface, 2, 0, buf, count); | |
384 | kfree(buf); | |
385 | goto exit; | |
386 | break; | |
387 | case USB_DEVICE_ID_CODEMERCS_IOW56: | |
461d8deb | 388 | case USB_DEVICE_ID_CODEMERCS_IOW56AM: |
5f6f8da2 GKH |
389 | case USB_DEVICE_ID_CODEMERCS_IOW28: |
390 | case USB_DEVICE_ID_CODEMERCS_IOW28L: | |
bab5417f | 391 | case USB_DEVICE_ID_CODEMERCS_IOW100: |
946b960d GKH |
392 | /* The IOW56 uses asynchronous IO and more urbs */ |
393 | if (atomic_read(&dev->write_busy) == MAX_WRITES_IN_FLIGHT) { | |
394 | /* Wait until we are below the limit for submitted urbs */ | |
395 | if (file->f_flags & O_NONBLOCK) { | |
396 | retval = -EAGAIN; | |
397 | goto exit; | |
398 | } else { | |
399 | retval = wait_event_interruptible(dev->write_wait, | |
400 | (!dev->present || (atomic_read (&dev-> write_busy) < MAX_WRITES_IN_FLIGHT))); | |
401 | if (retval) { | |
402 | /* we were interrupted by a signal */ | |
403 | retval = -ERESTART; | |
404 | goto exit; | |
405 | } | |
406 | if (!dev->present) { | |
407 | /* The device was unplugged */ | |
408 | retval = -ENODEV; | |
409 | goto exit; | |
410 | } | |
411 | if (!dev->opened) { | |
412 | /* We were closed while waiting for an URB */ | |
413 | retval = -ENODEV; | |
414 | goto exit; | |
415 | } | |
416 | } | |
417 | } | |
418 | atomic_inc(&dev->write_busy); | |
419 | int_out_urb = usb_alloc_urb(0, GFP_KERNEL); | |
420 | if (!int_out_urb) { | |
421 | retval = -ENOMEM; | |
f81ee4d5 | 422 | goto error_no_urb; |
946b960d | 423 | } |
997ea58e DM |
424 | buf = usb_alloc_coherent(dev->udev, dev->report_size, |
425 | GFP_KERNEL, &int_out_urb->transfer_dma); | |
946b960d GKH |
426 | if (!buf) { |
427 | retval = -ENOMEM; | |
549e8350 JP |
428 | dev_dbg(&dev->interface->dev, |
429 | "Unable to allocate buffer\n"); | |
f81ee4d5 | 430 | goto error_no_buffer; |
946b960d GKH |
431 | } |
432 | usb_fill_int_urb(int_out_urb, dev->udev, | |
433 | usb_sndintpipe(dev->udev, | |
434 | dev->int_out_endpoint->bEndpointAddress), | |
435 | buf, dev->report_size, | |
436 | iowarrior_write_callback, dev, | |
437 | dev->int_out_endpoint->bInterval); | |
438 | int_out_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; | |
439 | if (copy_from_user(buf, user_buffer, count)) { | |
440 | retval = -EFAULT; | |
441 | goto error; | |
442 | } | |
b5f8d468 | 443 | usb_anchor_urb(int_out_urb, &dev->submitted); |
946b960d GKH |
444 | retval = usb_submit_urb(int_out_urb, GFP_KERNEL); |
445 | if (retval) { | |
549e8350 JP |
446 | dev_dbg(&dev->interface->dev, |
447 | "submit error %d for urb nr.%d\n", | |
448 | retval, atomic_read(&dev->write_busy)); | |
b5f8d468 | 449 | usb_unanchor_urb(int_out_urb); |
946b960d GKH |
450 | goto error; |
451 | } | |
452 | /* submit was ok */ | |
453 | retval = count; | |
454 | usb_free_urb(int_out_urb); | |
455 | goto exit; | |
456 | break; | |
457 | default: | |
458 | /* what do we have here ? An unsupported Product-ID ? */ | |
898eb71c | 459 | dev_err(&dev->interface->dev, "%s - not supported for product=0x%x\n", |
441b62c1 | 460 | __func__, dev->product_id); |
946b960d GKH |
461 | retval = -EFAULT; |
462 | goto exit; | |
463 | break; | |
464 | } | |
465 | error: | |
997ea58e DM |
466 | usb_free_coherent(dev->udev, dev->report_size, buf, |
467 | int_out_urb->transfer_dma); | |
f81ee4d5 | 468 | error_no_buffer: |
946b960d | 469 | usb_free_urb(int_out_urb); |
f81ee4d5 | 470 | error_no_urb: |
946b960d GKH |
471 | atomic_dec(&dev->write_busy); |
472 | wake_up_interruptible(&dev->write_wait); | |
473 | exit: | |
474 | mutex_unlock(&dev->mutex); | |
475 | return retval; | |
476 | } | |
477 | ||
478 | /** | |
479 | * iowarrior_ioctl | |
480 | */ | |
824f16fd AC |
481 | static long iowarrior_ioctl(struct file *file, unsigned int cmd, |
482 | unsigned long arg) | |
946b960d GKH |
483 | { |
484 | struct iowarrior *dev = NULL; | |
485 | __u8 *buffer; | |
486 | __u8 __user *user_buffer; | |
487 | int retval; | |
488 | int io_res; /* checks for bytes read/written and copy_to/from_user results */ | |
489 | ||
5bd6e8b3 | 490 | dev = file->private_data; |
3cfb4842 | 491 | if (!dev) |
946b960d | 492 | return -ENODEV; |
946b960d GKH |
493 | |
494 | buffer = kzalloc(dev->report_size, GFP_KERNEL); | |
495 | if (!buffer) | |
496 | return -ENOMEM; | |
497 | ||
946b960d GKH |
498 | mutex_lock(&dev->mutex); |
499 | ||
500 | /* verify that the device wasn't unplugged */ | |
501 | if (!dev->present) { | |
fc0f8fc9 ON |
502 | retval = -ENODEV; |
503 | goto error_out; | |
946b960d GKH |
504 | } |
505 | ||
549e8350 JP |
506 | dev_dbg(&dev->interface->dev, "minor %d, cmd 0x%.4x, arg %ld\n", |
507 | dev->minor, cmd, arg); | |
946b960d GKH |
508 | |
509 | retval = 0; | |
510 | io_res = 0; | |
511 | switch (cmd) { | |
512 | case IOW_WRITE: | |
513 | if (dev->product_id == USB_DEVICE_ID_CODEMERCS_IOW24 || | |
461d8deb | 514 | dev->product_id == USB_DEVICE_ID_CODEMERCS_IOW24SAG || |
946b960d GKH |
515 | dev->product_id == USB_DEVICE_ID_CODEMERCS_IOWPV1 || |
516 | dev->product_id == USB_DEVICE_ID_CODEMERCS_IOWPV2 || | |
517 | dev->product_id == USB_DEVICE_ID_CODEMERCS_IOW40) { | |
518 | user_buffer = (__u8 __user *)arg; | |
519 | io_res = copy_from_user(buffer, user_buffer, | |
520 | dev->report_size); | |
521 | if (io_res) { | |
522 | retval = -EFAULT; | |
523 | } else { | |
524 | io_res = usb_set_report(dev->interface, 2, 0, | |
525 | buffer, | |
526 | dev->report_size); | |
527 | if (io_res < 0) | |
528 | retval = io_res; | |
529 | } | |
530 | } else { | |
531 | retval = -EINVAL; | |
532 | dev_err(&dev->interface->dev, | |
898eb71c | 533 | "ioctl 'IOW_WRITE' is not supported for product=0x%x.\n", |
946b960d GKH |
534 | dev->product_id); |
535 | } | |
536 | break; | |
537 | case IOW_READ: | |
538 | user_buffer = (__u8 __user *)arg; | |
539 | io_res = usb_get_report(dev->udev, | |
540 | dev->interface->cur_altsetting, 1, 0, | |
541 | buffer, dev->report_size); | |
542 | if (io_res < 0) | |
543 | retval = io_res; | |
544 | else { | |
545 | io_res = copy_to_user(user_buffer, buffer, dev->report_size); | |
6d4d4554 | 546 | if (io_res) |
946b960d GKH |
547 | retval = -EFAULT; |
548 | } | |
549 | break; | |
550 | case IOW_GETINFO: | |
551 | { | |
552 | /* Report available information for the device */ | |
553 | struct iowarrior_info info; | |
554 | /* needed for power consumption */ | |
555 | struct usb_config_descriptor *cfg_descriptor = &dev->udev->actconfig->desc; | |
556 | ||
eca67aae | 557 | memset(&info, 0, sizeof(info)); |
946b960d GKH |
558 | /* directly from the descriptor */ |
559 | info.vendor = le16_to_cpu(dev->udev->descriptor.idVendor); | |
560 | info.product = dev->product_id; | |
561 | info.revision = le16_to_cpu(dev->udev->descriptor.bcdDevice); | |
562 | ||
563 | /* 0==UNKNOWN, 1==LOW(usb1.1) ,2=FULL(usb1.1), 3=HIGH(usb2.0) */ | |
dd5ca753 | 564 | info.speed = dev->udev->speed; |
946b960d GKH |
565 | info.if_num = dev->interface->cur_altsetting->desc.bInterfaceNumber; |
566 | info.report_size = dev->report_size; | |
567 | ||
568 | /* serial number string has been read earlier 8 chars or empty string */ | |
569 | memcpy(info.serial, dev->chip_serial, | |
570 | sizeof(dev->chip_serial)); | |
571 | if (cfg_descriptor == NULL) { | |
572 | info.power = -1; /* no information available */ | |
573 | } else { | |
574 | /* the MaxPower is stored in units of 2mA to make it fit into a byte-value */ | |
575 | info.power = cfg_descriptor->bMaxPower * 2; | |
576 | } | |
577 | io_res = copy_to_user((struct iowarrior_info __user *)arg, &info, | |
578 | sizeof(struct iowarrior_info)); | |
6d4d4554 | 579 | if (io_res) |
946b960d GKH |
580 | retval = -EFAULT; |
581 | break; | |
582 | } | |
583 | default: | |
584 | /* return that we did not understand this ioctl call */ | |
585 | retval = -ENOTTY; | |
586 | break; | |
587 | } | |
fc0f8fc9 | 588 | error_out: |
946b960d GKH |
589 | /* unlock the device */ |
590 | mutex_unlock(&dev->mutex); | |
fc0f8fc9 | 591 | kfree(buffer); |
946b960d GKH |
592 | return retval; |
593 | } | |
594 | ||
595 | /** | |
596 | * iowarrior_open | |
597 | */ | |
598 | static int iowarrior_open(struct inode *inode, struct file *file) | |
599 | { | |
600 | struct iowarrior *dev = NULL; | |
601 | struct usb_interface *interface; | |
602 | int subminor; | |
603 | int retval = 0; | |
604 | ||
946b960d GKH |
605 | subminor = iminor(inode); |
606 | ||
946b960d GKH |
607 | interface = usb_find_interface(&iowarrior_driver, subminor); |
608 | if (!interface) { | |
ebb2fe57 | 609 | pr_err("%s - error, can't find device for minor %d\n", |
1c2eef03 | 610 | __func__, subminor); |
d4ead16f | 611 | return -ENODEV; |
946b960d GKH |
612 | } |
613 | ||
614 | dev = usb_get_intfdata(interface); | |
8d33e828 | 615 | if (!dev) |
d4ead16f AS |
616 | return -ENODEV; |
617 | ||
618 | mutex_lock(&dev->mutex); | |
946b960d GKH |
619 | |
620 | /* Only one process can open each device, no sharing. */ | |
621 | if (dev->opened) { | |
622 | retval = -EBUSY; | |
623 | goto out; | |
624 | } | |
625 | ||
626 | /* setup interrupt handler for receiving values */ | |
627 | if ((retval = usb_submit_urb(dev->int_in_urb, GFP_KERNEL)) < 0) { | |
628 | dev_err(&interface->dev, "Error %d while submitting URB\n", retval); | |
629 | retval = -EFAULT; | |
630 | goto out; | |
631 | } | |
632 | /* increment our usage count for the driver */ | |
633 | ++dev->opened; | |
634 | /* save our object in the file's private structure */ | |
635 | file->private_data = dev; | |
636 | retval = 0; | |
637 | ||
638 | out: | |
d4ead16f | 639 | mutex_unlock(&dev->mutex); |
946b960d GKH |
640 | return retval; |
641 | } | |
642 | ||
643 | /** | |
644 | * iowarrior_release | |
645 | */ | |
646 | static int iowarrior_release(struct inode *inode, struct file *file) | |
647 | { | |
648 | struct iowarrior *dev; | |
649 | int retval = 0; | |
650 | ||
5bd6e8b3 | 651 | dev = file->private_data; |
3cfb4842 | 652 | if (!dev) |
946b960d | 653 | return -ENODEV; |
946b960d | 654 | |
549e8350 | 655 | dev_dbg(&dev->interface->dev, "minor %d\n", dev->minor); |
946b960d GKH |
656 | |
657 | /* lock our device */ | |
658 | mutex_lock(&dev->mutex); | |
659 | ||
660 | if (dev->opened <= 0) { | |
661 | retval = -ENODEV; /* close called more than once */ | |
662 | mutex_unlock(&dev->mutex); | |
663 | } else { | |
a895d57d | 664 | dev->opened = 0; /* we're closing now */ |
946b960d GKH |
665 | retval = 0; |
666 | if (dev->present) { | |
667 | /* | |
668 | The device is still connected so we only shutdown | |
669 | pending read-/write-ops. | |
670 | */ | |
671 | usb_kill_urb(dev->int_in_urb); | |
672 | wake_up_interruptible(&dev->read_wait); | |
673 | wake_up_interruptible(&dev->write_wait); | |
674 | mutex_unlock(&dev->mutex); | |
675 | } else { | |
676 | /* The device was unplugged, cleanup resources */ | |
677 | mutex_unlock(&dev->mutex); | |
678 | iowarrior_delete(dev); | |
679 | } | |
680 | } | |
681 | return retval; | |
682 | } | |
683 | ||
afc9a42b | 684 | static __poll_t iowarrior_poll(struct file *file, poll_table * wait) |
946b960d GKH |
685 | { |
686 | struct iowarrior *dev = file->private_data; | |
afc9a42b | 687 | __poll_t mask = 0; |
946b960d GKH |
688 | |
689 | if (!dev->present) | |
a9a08845 | 690 | return EPOLLERR | EPOLLHUP; |
946b960d GKH |
691 | |
692 | poll_wait(file, &dev->read_wait, wait); | |
693 | poll_wait(file, &dev->write_wait, wait); | |
694 | ||
695 | if (!dev->present) | |
a9a08845 | 696 | return EPOLLERR | EPOLLHUP; |
946b960d GKH |
697 | |
698 | if (read_index(dev) != -1) | |
a9a08845 | 699 | mask |= EPOLLIN | EPOLLRDNORM; |
946b960d GKH |
700 | |
701 | if (atomic_read(&dev->write_busy) < MAX_WRITES_IN_FLIGHT) | |
a9a08845 | 702 | mask |= EPOLLOUT | EPOLLWRNORM; |
946b960d GKH |
703 | return mask; |
704 | } | |
705 | ||
706 | /* | |
707 | * File operations needed when we register this driver. | |
708 | * This assumes that this driver NEEDS file operations, | |
709 | * of course, which means that the driver is expected | |
710 | * to have a node in the /dev directory. If the USB | |
711 | * device were for a network interface then the driver | |
712 | * would use "struct net_driver" instead, and a serial | |
713 | * device would use "struct tty_driver". | |
714 | */ | |
0b3f5fe6 | 715 | static const struct file_operations iowarrior_fops = { |
946b960d GKH |
716 | .owner = THIS_MODULE, |
717 | .write = iowarrior_write, | |
718 | .read = iowarrior_read, | |
824f16fd | 719 | .unlocked_ioctl = iowarrior_ioctl, |
946b960d GKH |
720 | .open = iowarrior_open, |
721 | .release = iowarrior_release, | |
722 | .poll = iowarrior_poll, | |
6038f373 | 723 | .llseek = noop_llseek, |
946b960d GKH |
724 | }; |
725 | ||
2c9ede55 | 726 | static char *iowarrior_devnode(struct device *dev, umode_t *mode) |
f7a386c5 KS |
727 | { |
728 | return kasprintf(GFP_KERNEL, "usb/%s", dev_name(dev)); | |
729 | } | |
730 | ||
946b960d GKH |
731 | /* |
732 | * usb class driver info in order to get a minor number from the usb core, | |
733 | * and to have the device registered with devfs and the driver core | |
734 | */ | |
735 | static struct usb_class_driver iowarrior_class = { | |
736 | .name = "iowarrior%d", | |
e454cea2 | 737 | .devnode = iowarrior_devnode, |
946b960d GKH |
738 | .fops = &iowarrior_fops, |
739 | .minor_base = IOWARRIOR_MINOR_BASE, | |
740 | }; | |
741 | ||
742 | /*---------------------------------*/ | |
743 | /* probe and disconnect functions */ | |
744 | /*---------------------------------*/ | |
745 | /** | |
746 | * iowarrior_probe | |
747 | * | |
748 | * Called by the usb core when a new device is connected that it thinks | |
749 | * this driver might be interested in. | |
750 | */ | |
751 | static int iowarrior_probe(struct usb_interface *interface, | |
752 | const struct usb_device_id *id) | |
753 | { | |
754 | struct usb_device *udev = interface_to_usbdev(interface); | |
755 | struct iowarrior *dev = NULL; | |
756 | struct usb_host_interface *iface_desc; | |
946b960d | 757 | int retval = -ENOMEM; |
920df8d7 | 758 | int res; |
946b960d | 759 | |
b595076a | 760 | /* allocate memory for our device state and initialize it */ |
946b960d | 761 | dev = kzalloc(sizeof(struct iowarrior), GFP_KERNEL); |
3cfb4842 | 762 | if (!dev) |
946b960d | 763 | return retval; |
946b960d GKH |
764 | |
765 | mutex_init(&dev->mutex); | |
766 | ||
767 | atomic_set(&dev->intr_idx, 0); | |
768 | atomic_set(&dev->read_idx, 0); | |
946b960d GKH |
769 | atomic_set(&dev->overflow_flag, 0); |
770 | init_waitqueue_head(&dev->read_wait); | |
771 | atomic_set(&dev->write_busy, 0); | |
772 | init_waitqueue_head(&dev->write_wait); | |
773 | ||
774 | dev->udev = udev; | |
80cd5479 | 775 | dev->interface = usb_get_intf(interface); |
946b960d GKH |
776 | |
777 | iface_desc = interface->cur_altsetting; | |
778 | dev->product_id = le16_to_cpu(udev->descriptor.idProduct); | |
779 | ||
b5f8d468 JH |
780 | init_usb_anchor(&dev->submitted); |
781 | ||
920df8d7 JH |
782 | res = usb_find_last_int_in_endpoint(iface_desc, &dev->int_in_endpoint); |
783 | if (res) { | |
b7321e81 | 784 | dev_err(&interface->dev, "no interrupt-in endpoint found\n"); |
920df8d7 | 785 | retval = res; |
b7321e81 JH |
786 | goto error; |
787 | } | |
788 | ||
461d8deb | 789 | if ((dev->product_id == USB_DEVICE_ID_CODEMERCS_IOW56) || |
5f6f8da2 GKH |
790 | (dev->product_id == USB_DEVICE_ID_CODEMERCS_IOW56AM) || |
791 | (dev->product_id == USB_DEVICE_ID_CODEMERCS_IOW28) || | |
bab5417f GKH |
792 | (dev->product_id == USB_DEVICE_ID_CODEMERCS_IOW28L) || |
793 | (dev->product_id == USB_DEVICE_ID_CODEMERCS_IOW100)) { | |
920df8d7 JH |
794 | res = usb_find_last_int_out_endpoint(iface_desc, |
795 | &dev->int_out_endpoint); | |
796 | if (res) { | |
de46e566 | 797 | dev_err(&interface->dev, "no interrupt-out endpoint found\n"); |
920df8d7 | 798 | retval = res; |
de46e566 JH |
799 | goto error; |
800 | } | |
801 | } | |
802 | ||
a895d57d | 803 | /* we have to check the report_size often, so remember it in the endianness suitable for our machine */ |
29cc8897 | 804 | dev->report_size = usb_endpoint_maxp(dev->int_in_endpoint); |
946b960d | 805 | if ((dev->interface->cur_altsetting->desc.bInterfaceNumber == 0) && |
461d8deb | 806 | ((dev->product_id == USB_DEVICE_ID_CODEMERCS_IOW56) || |
5f6f8da2 GKH |
807 | (dev->product_id == USB_DEVICE_ID_CODEMERCS_IOW56AM) || |
808 | (dev->product_id == USB_DEVICE_ID_CODEMERCS_IOW28) || | |
bab5417f GKH |
809 | (dev->product_id == USB_DEVICE_ID_CODEMERCS_IOW28L) || |
810 | (dev->product_id == USB_DEVICE_ID_CODEMERCS_IOW100))) | |
946b960d GKH |
811 | /* IOWarrior56 has wMaxPacketSize different from report size */ |
812 | dev->report_size = 7; | |
813 | ||
814 | /* create the urb and buffer for reading */ | |
815 | dev->int_in_urb = usb_alloc_urb(0, GFP_KERNEL); | |
5656bbb7 | 816 | if (!dev->int_in_urb) |
946b960d | 817 | goto error; |
946b960d | 818 | dev->int_in_buffer = kmalloc(dev->report_size, GFP_KERNEL); |
3cfb4842 | 819 | if (!dev->int_in_buffer) |
946b960d | 820 | goto error; |
946b960d GKH |
821 | usb_fill_int_urb(dev->int_in_urb, dev->udev, |
822 | usb_rcvintpipe(dev->udev, | |
823 | dev->int_in_endpoint->bEndpointAddress), | |
824 | dev->int_in_buffer, dev->report_size, | |
825 | iowarrior_callback, dev, | |
826 | dev->int_in_endpoint->bInterval); | |
827 | /* create an internal buffer for interrupt data from the device */ | |
828 | dev->read_queue = | |
23feefda GS |
829 | kmalloc_array(dev->report_size + 1, MAX_INTERRUPT_BUFFER, |
830 | GFP_KERNEL); | |
3cfb4842 | 831 | if (!dev->read_queue) |
946b960d | 832 | goto error; |
946b960d GKH |
833 | /* Get the serial-number of the chip */ |
834 | memset(dev->chip_serial, 0x00, sizeof(dev->chip_serial)); | |
835 | usb_string(udev, udev->descriptor.iSerialNumber, dev->chip_serial, | |
836 | sizeof(dev->chip_serial)); | |
837 | if (strlen(dev->chip_serial) != 8) | |
838 | memset(dev->chip_serial, 0x00, sizeof(dev->chip_serial)); | |
839 | ||
840 | /* Set the idle timeout to 0, if this is interface 0 */ | |
841 | if (dev->interface->cur_altsetting->desc.bInterfaceNumber == 0) { | |
147c5a17 EF |
842 | usb_control_msg(udev, usb_sndctrlpipe(udev, 0), |
843 | 0x0A, | |
844 | USB_TYPE_CLASS | USB_RECIP_INTERFACE, 0, | |
845 | 0, NULL, 0, USB_CTRL_SET_TIMEOUT); | |
946b960d GKH |
846 | } |
847 | /* allow device read and ioctl */ | |
848 | dev->present = 1; | |
849 | ||
850 | /* we can register the device now, as it is ready */ | |
851 | usb_set_intfdata(interface, dev); | |
852 | ||
853 | retval = usb_register_dev(interface, &iowarrior_class); | |
854 | if (retval) { | |
855 | /* something prevented us from registering this driver */ | |
856 | dev_err(&interface->dev, "Not able to get a minor for this device.\n"); | |
946b960d GKH |
857 | goto error; |
858 | } | |
859 | ||
860 | dev->minor = interface->minor; | |
861 | ||
862 | /* let the user know what node this device is now attached to */ | |
863 | dev_info(&interface->dev, "IOWarrior product=0x%x, serial=%s interface=%d " | |
864 | "now attached to iowarrior%d\n", dev->product_id, dev->chip_serial, | |
865 | iface_desc->desc.bInterfaceNumber, dev->minor - IOWARRIOR_MINOR_BASE); | |
866 | return retval; | |
867 | ||
868 | error: | |
869 | iowarrior_delete(dev); | |
870 | return retval; | |
871 | } | |
872 | ||
873 | /** | |
874 | * iowarrior_disconnect | |
875 | * | |
876 | * Called by the usb core when the device is removed from the system. | |
877 | */ | |
878 | static void iowarrior_disconnect(struct usb_interface *interface) | |
879 | { | |
7c5b971d JH |
880 | struct iowarrior *dev = usb_get_intfdata(interface); |
881 | int minor = dev->minor; | |
946b960d | 882 | |
946b960d GKH |
883 | usb_deregister_dev(interface, &iowarrior_class); |
884 | ||
d4ead16f AS |
885 | mutex_lock(&dev->mutex); |
886 | ||
946b960d | 887 | /* prevent device read, write and ioctl */ |
edc4746f | 888 | dev->present = 0; |
946b960d GKH |
889 | |
890 | if (dev->opened) { | |
891 | /* There is a process that holds a filedescriptor to the device , | |
892 | so we only shutdown read-/write-ops going on. | |
893 | Deleting the device is postponed until close() was called. | |
894 | */ | |
895 | usb_kill_urb(dev->int_in_urb); | |
b5f8d468 | 896 | usb_kill_anchored_urbs(&dev->submitted); |
946b960d GKH |
897 | wake_up_interruptible(&dev->read_wait); |
898 | wake_up_interruptible(&dev->write_wait); | |
edc4746f | 899 | mutex_unlock(&dev->mutex); |
946b960d GKH |
900 | } else { |
901 | /* no process is using the device, cleanup now */ | |
edc4746f | 902 | mutex_unlock(&dev->mutex); |
946b960d GKH |
903 | iowarrior_delete(dev); |
904 | } | |
946b960d GKH |
905 | |
906 | dev_info(&interface->dev, "I/O-Warror #%d now disconnected\n", | |
907 | minor - IOWARRIOR_MINOR_BASE); | |
908 | } | |
909 | ||
910 | /* usb specific object needed to register this driver with the usb subsystem */ | |
911 | static struct usb_driver iowarrior_driver = { | |
912 | .name = "iowarrior", | |
913 | .probe = iowarrior_probe, | |
914 | .disconnect = iowarrior_disconnect, | |
915 | .id_table = iowarrior_ids, | |
916 | }; | |
917 | ||
65db4305 | 918 | module_usb_driver(iowarrior_driver); |