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