GNU Linux-libre 4.14.262-gnu1
[releases.git] / drivers / usb / misc / iowarrior.c
1 /*
2  *  Native support for the I/O-Warrior USB devices
3  *
4  *  Copyright (c) 2003-2005, 2020  Code Mercenaries GmbH
5  *  written by Christian Lucht <lucht@codemercs.com> and
6  *  Christoph Jung <jung@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>
12  *           and St�hane Doyon   <s.doyon@videotron.ca>
13  *
14  *  Released under the GPLv2.
15  */
16
17 #include <linux/module.h>
18 #include <linux/usb.h>
19 #include <linux/slab.h>
20 #include <linux/sched.h>
21 #include <linux/mutex.h>
22 #include <linux/poll.h>
23 #include <linux/usb/iowarrior.h>
24
25 #define DRIVER_AUTHOR "Christian Lucht <lucht@codemercs.com>"
26 #define DRIVER_DESC "USB IO-Warrior driver"
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
36 /* fuller speed iowarrior */
37 #define USB_DEVICE_ID_CODEMERCS_IOW28   0x1504
38 #define USB_DEVICE_ID_CODEMERCS_IOW28L  0x1505
39 #define USB_DEVICE_ID_CODEMERCS_IOW100  0x1506
40
41 /* OEMed devices */
42 #define USB_DEVICE_ID_CODEMERCS_IOW24SAG        0x158a
43 #define USB_DEVICE_ID_CODEMERCS_IOW56AM         0x158b
44
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
49 #define IOWARRIOR_MINOR_BASE    208     // SKELETON_MINOR_BASE 192 + 16, not official yet
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
61 MODULE_AUTHOR(DRIVER_AUTHOR);
62 MODULE_DESCRIPTION(DRIVER_DESC);
63 MODULE_LICENSE("GPL");
64
65 /* Module parameters */
66 static DEFINE_MUTEX(iowarrior_mutex);
67
68 static struct usb_driver iowarrior_driver;
69 static DEFINE_MUTEX(iowarrior_open_disc_lock);
70
71 /*--------------*/
72 /*     data     */
73 /*--------------*/
74
75 /* Structure to hold all of our device specific stuff */
76 struct iowarrior {
77         struct mutex mutex;                     /* locks this structure */
78         struct usb_device *udev;                /* save off the usb device pointer */
79         struct usb_interface *interface;        /* the interface for this device */
80         unsigned char minor;                    /* the starting minor number for this device */
81         struct usb_endpoint_descriptor *int_out_endpoint;       /* endpoint for reading (needed for IOW56 only) */
82         struct usb_endpoint_descriptor *int_in_endpoint;        /* endpoint for reading */
83         struct urb *int_in_urb;         /* the urb for reading data */
84         unsigned char *int_in_buffer;   /* buffer for data to be read */
85         unsigned char serial_number;    /* to detect lost packages */
86         unsigned char *read_queue;      /* size is MAX_INTERRUPT_BUFFER * packet size */
87         wait_queue_head_t read_wait;
88         wait_queue_head_t write_wait;   /* wait-queue for writing to the device */
89         atomic_t write_busy;            /* number of write-urbs submitted */
90         atomic_t read_idx;
91         atomic_t intr_idx;
92         spinlock_t intr_idx_lock;       /* protects 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 #define USB_REQ_GET_REPORT  0x01
107 //#if 0
108 static int usb_get_report(struct usb_device *dev,
109                           struct usb_host_interface *inter, unsigned char type,
110                           unsigned char id, void *buf, int size)
111 {
112         return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
113                                USB_REQ_GET_REPORT,
114                                USB_DIR_IN | USB_TYPE_CLASS |
115                                USB_RECIP_INTERFACE, (type << 8) + id,
116                                inter->desc.bInterfaceNumber, buf, size,
117                                USB_CTRL_GET_TIMEOUT);
118 }
119 //#endif
120
121 #define USB_REQ_SET_REPORT 0x09
122
123 static int usb_set_report(struct usb_interface *intf, unsigned char type,
124                           unsigned char id, void *buf, int size)
125 {
126         return usb_control_msg(interface_to_usbdev(intf),
127                                usb_sndctrlpipe(interface_to_usbdev(intf), 0),
128                                USB_REQ_SET_REPORT,
129                                USB_TYPE_CLASS | USB_RECIP_INTERFACE,
130                                (type << 8) + id,
131                                intf->cur_altsetting->desc.bInterfaceNumber, buf,
132                                size, 1000);
133 }
134
135 /*---------------------*/
136 /* driver registration */
137 /*---------------------*/
138 /* table of devices that work with this driver */
139 static const struct usb_device_id iowarrior_ids[] = {
140         {USB_DEVICE(USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOW40)},
141         {USB_DEVICE(USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOW24)},
142         {USB_DEVICE(USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOWPV1)},
143         {USB_DEVICE(USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOWPV2)},
144         {USB_DEVICE(USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOW56)},
145         {USB_DEVICE(USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOW24SAG)},
146         {USB_DEVICE(USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOW56AM)},
147         {USB_DEVICE(USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOW28)},
148         {USB_DEVICE(USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOW28L)},
149         {USB_DEVICE(USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOW100)},
150         {}                      /* Terminating entry */
151 };
152 MODULE_DEVICE_TABLE(usb, iowarrior_ids);
153
154 /*
155  * USB callback handler for reading data
156  */
157 static void iowarrior_callback(struct urb *urb)
158 {
159         struct iowarrior *dev = urb->context;
160         int intr_idx;
161         int read_idx;
162         int aux_idx;
163         int offset;
164         int status = urb->status;
165         int retval;
166
167         switch (status) {
168         case 0:
169                 /* success */
170                 break;
171         case -ECONNRESET:
172         case -ENOENT:
173         case -ESHUTDOWN:
174                 return;
175         default:
176                 goto exit;
177         }
178
179         spin_lock(&dev->intr_idx_lock);
180         intr_idx = atomic_read(&dev->intr_idx);
181         /* aux_idx become previous intr_idx */
182         aux_idx = (intr_idx == 0) ? (MAX_INTERRUPT_BUFFER - 1) : (intr_idx - 1);
183         read_idx = atomic_read(&dev->read_idx);
184
185         /* queue is not empty and it's interface 0 */
186         if ((intr_idx != read_idx)
187             && (dev->interface->cur_altsetting->desc.bInterfaceNumber == 0)) {
188                 /* + 1 for serial number */
189                 offset = aux_idx * (dev->report_size + 1);
190                 if (!memcmp
191                     (dev->read_queue + offset, urb->transfer_buffer,
192                      dev->report_size)) {
193                         /* equal values on interface 0 will be ignored */
194                         spin_unlock(&dev->intr_idx_lock);
195                         goto exit;
196                 }
197         }
198
199         /* aux_idx become next intr_idx */
200         aux_idx = (intr_idx == (MAX_INTERRUPT_BUFFER - 1)) ? 0 : (intr_idx + 1);
201         if (read_idx == aux_idx) {
202                 /* queue full, dropping oldest input */
203                 read_idx = (++read_idx == MAX_INTERRUPT_BUFFER) ? 0 : read_idx;
204                 atomic_set(&dev->read_idx, read_idx);
205                 atomic_set(&dev->overflow_flag, 1);
206         }
207
208         /* +1 for serial number */
209         offset = intr_idx * (dev->report_size + 1);
210         memcpy(dev->read_queue + offset, urb->transfer_buffer,
211                dev->report_size);
212         *(dev->read_queue + offset + (dev->report_size)) = dev->serial_number++;
213
214         atomic_set(&dev->intr_idx, aux_idx);
215         spin_unlock(&dev->intr_idx_lock);
216         /* tell the blocking read about the new data */
217         wake_up_interruptible(&dev->read_wait);
218
219 exit:
220         retval = usb_submit_urb(urb, GFP_ATOMIC);
221         if (retval)
222                 dev_err(&dev->interface->dev, "%s - usb_submit_urb failed with result %d\n",
223                         __func__, retval);
224
225 }
226
227 /*
228  * USB Callback handler for write-ops
229  */
230 static void iowarrior_write_callback(struct urb *urb)
231 {
232         struct iowarrior *dev;
233         int status = urb->status;
234
235         dev = urb->context;
236         /* sync/async unlink faults aren't errors */
237         if (status &&
238             !(status == -ENOENT ||
239               status == -ECONNRESET || status == -ESHUTDOWN)) {
240                 dev_dbg(&dev->interface->dev,
241                         "nonzero write bulk status received: %d\n", status);
242         }
243         /* free up our allocated buffer */
244         usb_free_coherent(urb->dev, urb->transfer_buffer_length,
245                           urb->transfer_buffer, urb->transfer_dma);
246         /* tell a waiting writer the interrupt-out-pipe is available again */
247         atomic_dec(&dev->write_busy);
248         wake_up_interruptible(&dev->write_wait);
249 }
250
251 /**
252  *      iowarrior_delete
253  */
254 static inline void iowarrior_delete(struct iowarrior *dev)
255 {
256         dev_dbg(&dev->interface->dev, "minor %d\n", dev->minor);
257         kfree(dev->int_in_buffer);
258         usb_free_urb(dev->int_in_urb);
259         kfree(dev->read_queue);
260         usb_put_intf(dev->interface);
261         kfree(dev);
262 }
263
264 /*---------------------*/
265 /* fops implementation */
266 /*---------------------*/
267
268 static int read_index(struct iowarrior *dev)
269 {
270         int intr_idx, read_idx;
271
272         read_idx = atomic_read(&dev->read_idx);
273         intr_idx = atomic_read(&dev->intr_idx);
274
275         return (read_idx == intr_idx ? -1 : read_idx);
276 }
277
278 /**
279  *  iowarrior_read
280  */
281 static ssize_t iowarrior_read(struct file *file, char __user *buffer,
282                               size_t count, loff_t *ppos)
283 {
284         struct iowarrior *dev;
285         int read_idx;
286         int offset;
287
288         dev = file->private_data;
289
290         /* verify that the device wasn't unplugged */
291         if (!dev || !dev->present)
292                 return -ENODEV;
293
294         dev_dbg(&dev->interface->dev, "minor %d, count = %zd\n",
295                 dev->minor, count);
296
297         /* read count must be packet size (+ time stamp) */
298         if ((count != dev->report_size)
299             && (count != (dev->report_size + 1)))
300                 return -EINVAL;
301
302         /* repeat until no buffer overrun in callback handler occur */
303         do {
304                 atomic_set(&dev->overflow_flag, 0);
305                 if ((read_idx = read_index(dev)) == -1) {
306                         /* queue empty */
307                         if (file->f_flags & O_NONBLOCK)
308                                 return -EAGAIN;
309                         else {
310                                 //next line will return when there is either new data, or the device is unplugged
311                                 int r = wait_event_interruptible(dev->read_wait,
312                                                                  (!dev->present
313                                                                   || (read_idx =
314                                                                       read_index
315                                                                       (dev)) !=
316                                                                   -1));
317                                 if (r) {
318                                         //we were interrupted by a signal
319                                         return -ERESTART;
320                                 }
321                                 if (!dev->present) {
322                                         //The device was unplugged
323                                         return -ENODEV;
324                                 }
325                                 if (read_idx == -1) {
326                                         // Can this happen ???
327                                         return 0;
328                                 }
329                         }
330                 }
331
332                 offset = read_idx * (dev->report_size + 1);
333                 if (copy_to_user(buffer, dev->read_queue + offset, count)) {
334                         return -EFAULT;
335                 }
336         } while (atomic_read(&dev->overflow_flag));
337
338         read_idx = ++read_idx == MAX_INTERRUPT_BUFFER ? 0 : read_idx;
339         atomic_set(&dev->read_idx, read_idx);
340         return count;
341 }
342
343 /*
344  * iowarrior_write
345  */
346 static ssize_t iowarrior_write(struct file *file,
347                                const char __user *user_buffer,
348                                size_t count, loff_t *ppos)
349 {
350         struct iowarrior *dev;
351         int retval = 0;
352         char *buf = NULL;       /* for IOW24 and IOW56 we need a buffer */
353         struct urb *int_out_urb = NULL;
354
355         dev = file->private_data;
356
357         mutex_lock(&dev->mutex);
358         /* verify that the device wasn't unplugged */
359         if (!dev->present) {
360                 retval = -ENODEV;
361                 goto exit;
362         }
363         dev_dbg(&dev->interface->dev, "minor %d, count = %zd\n",
364                 dev->minor, count);
365         /* if count is 0 we're already done */
366         if (count == 0) {
367                 retval = 0;
368                 goto exit;
369         }
370         /* We only accept full reports */
371         if (count != dev->report_size) {
372                 retval = -EINVAL;
373                 goto exit;
374         }
375         switch (dev->product_id) {
376         case USB_DEVICE_ID_CODEMERCS_IOW24:
377         case USB_DEVICE_ID_CODEMERCS_IOW24SAG:
378         case USB_DEVICE_ID_CODEMERCS_IOWPV1:
379         case USB_DEVICE_ID_CODEMERCS_IOWPV2:
380         case USB_DEVICE_ID_CODEMERCS_IOW40:
381                 /* IOW24 and IOW40 use a synchronous call */
382                 buf = memdup_user(user_buffer, count);
383                 if (IS_ERR(buf)) {
384                         retval = PTR_ERR(buf);
385                         goto exit;
386                 }
387                 retval = usb_set_report(dev->interface, 2, 0, buf, count);
388                 kfree(buf);
389                 goto exit;
390                 break;
391         case USB_DEVICE_ID_CODEMERCS_IOW56:
392         case USB_DEVICE_ID_CODEMERCS_IOW56AM:
393         case USB_DEVICE_ID_CODEMERCS_IOW28:
394         case USB_DEVICE_ID_CODEMERCS_IOW28L:
395         case USB_DEVICE_ID_CODEMERCS_IOW100:
396                 /* The IOW56 uses asynchronous IO and more urbs */
397                 if (atomic_read(&dev->write_busy) == MAX_WRITES_IN_FLIGHT) {
398                         /* Wait until we are below the limit for submitted urbs */
399                         if (file->f_flags & O_NONBLOCK) {
400                                 retval = -EAGAIN;
401                                 goto exit;
402                         } else {
403                                 retval = wait_event_interruptible(dev->write_wait,
404                                                                   (!dev->present || (atomic_read (&dev-> write_busy) < MAX_WRITES_IN_FLIGHT)));
405                                 if (retval) {
406                                         /* we were interrupted by a signal */
407                                         retval = -ERESTART;
408                                         goto exit;
409                                 }
410                                 if (!dev->present) {
411                                         /* The device was unplugged */
412                                         retval = -ENODEV;
413                                         goto exit;
414                                 }
415                                 if (!dev->opened) {
416                                         /* We were closed while waiting for an URB */
417                                         retval = -ENODEV;
418                                         goto exit;
419                                 }
420                         }
421                 }
422                 atomic_inc(&dev->write_busy);
423                 int_out_urb = usb_alloc_urb(0, GFP_KERNEL);
424                 if (!int_out_urb) {
425                         retval = -ENOMEM;
426                         goto error_no_urb;
427                 }
428                 buf = usb_alloc_coherent(dev->udev, dev->report_size,
429                                          GFP_KERNEL, &int_out_urb->transfer_dma);
430                 if (!buf) {
431                         retval = -ENOMEM;
432                         dev_dbg(&dev->interface->dev,
433                                 "Unable to allocate buffer\n");
434                         goto error_no_buffer;
435                 }
436                 usb_fill_int_urb(int_out_urb, dev->udev,
437                                  usb_sndintpipe(dev->udev,
438                                                 dev->int_out_endpoint->bEndpointAddress),
439                                  buf, dev->report_size,
440                                  iowarrior_write_callback, dev,
441                                  dev->int_out_endpoint->bInterval);
442                 int_out_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
443                 if (copy_from_user(buf, user_buffer, count)) {
444                         retval = -EFAULT;
445                         goto error;
446                 }
447                 usb_anchor_urb(int_out_urb, &dev->submitted);
448                 retval = usb_submit_urb(int_out_urb, GFP_KERNEL);
449                 if (retval) {
450                         dev_dbg(&dev->interface->dev,
451                                 "submit error %d for urb nr.%d\n",
452                                 retval, atomic_read(&dev->write_busy));
453                         usb_unanchor_urb(int_out_urb);
454                         goto error;
455                 }
456                 /* submit was ok */
457                 retval = count;
458                 usb_free_urb(int_out_urb);
459                 goto exit;
460                 break;
461         default:
462                 /* what do we have here ? An unsupported Product-ID ? */
463                 dev_err(&dev->interface->dev, "%s - not supported for product=0x%x\n",
464                         __func__, dev->product_id);
465                 retval = -EFAULT;
466                 goto exit;
467                 break;
468         }
469 error:
470         usb_free_coherent(dev->udev, dev->report_size, buf,
471                           int_out_urb->transfer_dma);
472 error_no_buffer:
473         usb_free_urb(int_out_urb);
474 error_no_urb:
475         atomic_dec(&dev->write_busy);
476         wake_up_interruptible(&dev->write_wait);
477 exit:
478         mutex_unlock(&dev->mutex);
479         return retval;
480 }
481
482 /**
483  *      iowarrior_ioctl
484  */
485 static long iowarrior_ioctl(struct file *file, unsigned int cmd,
486                                                         unsigned long arg)
487 {
488         struct iowarrior *dev = NULL;
489         __u8 *buffer;
490         __u8 __user *user_buffer;
491         int retval;
492         int io_res;             /* checks for bytes read/written and copy_to/from_user results */
493
494         dev = file->private_data;
495         if (!dev)
496                 return -ENODEV;
497
498         buffer = kzalloc(dev->report_size, GFP_KERNEL);
499         if (!buffer)
500                 return -ENOMEM;
501
502         /* lock this object */
503         mutex_lock(&iowarrior_mutex);
504         mutex_lock(&dev->mutex);
505
506         /* verify that the device wasn't unplugged */
507         if (!dev->present) {
508                 retval = -ENODEV;
509                 goto error_out;
510         }
511
512         dev_dbg(&dev->interface->dev, "minor %d, cmd 0x%.4x, arg %ld\n",
513                 dev->minor, cmd, arg);
514
515         retval = 0;
516         io_res = 0;
517         switch (cmd) {
518         case IOW_WRITE:
519                 if (dev->product_id == USB_DEVICE_ID_CODEMERCS_IOW24 ||
520                     dev->product_id == USB_DEVICE_ID_CODEMERCS_IOW24SAG ||
521                     dev->product_id == USB_DEVICE_ID_CODEMERCS_IOWPV1 ||
522                     dev->product_id == USB_DEVICE_ID_CODEMERCS_IOWPV2 ||
523                     dev->product_id == USB_DEVICE_ID_CODEMERCS_IOW40) {
524                         user_buffer = (__u8 __user *)arg;
525                         io_res = copy_from_user(buffer, user_buffer,
526                                                 dev->report_size);
527                         if (io_res) {
528                                 retval = -EFAULT;
529                         } else {
530                                 io_res = usb_set_report(dev->interface, 2, 0,
531                                                         buffer,
532                                                         dev->report_size);
533                                 if (io_res < 0)
534                                         retval = io_res;
535                         }
536                 } else {
537                         retval = -EINVAL;
538                         dev_err(&dev->interface->dev,
539                                 "ioctl 'IOW_WRITE' is not supported for product=0x%x.\n",
540                                 dev->product_id);
541                 }
542                 break;
543         case IOW_READ:
544                 user_buffer = (__u8 __user *)arg;
545                 io_res = usb_get_report(dev->udev,
546                                         dev->interface->cur_altsetting, 1, 0,
547                                         buffer, dev->report_size);
548                 if (io_res < 0)
549                         retval = io_res;
550                 else {
551                         io_res = copy_to_user(user_buffer, buffer, dev->report_size);
552                         if (io_res)
553                                 retval = -EFAULT;
554                 }
555                 break;
556         case IOW_GETINFO:
557                 {
558                         /* Report available information for the device */
559                         struct iowarrior_info info;
560                         /* needed for power consumption */
561                         struct usb_config_descriptor *cfg_descriptor = &dev->udev->actconfig->desc;
562
563                         memset(&info, 0, sizeof(info));
564                         /* directly from the descriptor */
565                         info.vendor = le16_to_cpu(dev->udev->descriptor.idVendor);
566                         info.product = dev->product_id;
567                         info.revision = le16_to_cpu(dev->udev->descriptor.bcdDevice);
568
569                         /* 0==UNKNOWN, 1==LOW(usb1.1) ,2=FULL(usb1.1), 3=HIGH(usb2.0) */
570                         info.speed = dev->udev->speed;
571                         info.if_num = dev->interface->cur_altsetting->desc.bInterfaceNumber;
572                         info.report_size = dev->report_size;
573
574                         /* serial number string has been read earlier 8 chars or empty string */
575                         memcpy(info.serial, dev->chip_serial,
576                                sizeof(dev->chip_serial));
577                         if (cfg_descriptor == NULL) {
578                                 info.power = -1;        /* no information available */
579                         } else {
580                                 /* the MaxPower is stored in units of 2mA to make it fit into a byte-value */
581                                 info.power = cfg_descriptor->bMaxPower * 2;
582                         }
583                         io_res = copy_to_user((struct iowarrior_info __user *)arg, &info,
584                                          sizeof(struct iowarrior_info));
585                         if (io_res)
586                                 retval = -EFAULT;
587                         break;
588                 }
589         default:
590                 /* return that we did not understand this ioctl call */
591                 retval = -ENOTTY;
592                 break;
593         }
594 error_out:
595         /* unlock the device */
596         mutex_unlock(&dev->mutex);
597         mutex_unlock(&iowarrior_mutex);
598         kfree(buffer);
599         return retval;
600 }
601
602 /**
603  *      iowarrior_open
604  */
605 static int iowarrior_open(struct inode *inode, struct file *file)
606 {
607         struct iowarrior *dev = NULL;
608         struct usb_interface *interface;
609         int subminor;
610         int retval = 0;
611
612         mutex_lock(&iowarrior_mutex);
613         subminor = iminor(inode);
614
615         interface = usb_find_interface(&iowarrior_driver, subminor);
616         if (!interface) {
617                 mutex_unlock(&iowarrior_mutex);
618                 printk(KERN_ERR "%s - error, can't find device for minor %d\n",
619                        __func__, subminor);
620                 return -ENODEV;
621         }
622
623         mutex_lock(&iowarrior_open_disc_lock);
624         dev = usb_get_intfdata(interface);
625         if (!dev) {
626                 mutex_unlock(&iowarrior_open_disc_lock);
627                 mutex_unlock(&iowarrior_mutex);
628                 return -ENODEV;
629         }
630
631         mutex_lock(&dev->mutex);
632         mutex_unlock(&iowarrior_open_disc_lock);
633
634         /* Only one process can open each device, no sharing. */
635         if (dev->opened) {
636                 retval = -EBUSY;
637                 goto out;
638         }
639
640         /* setup interrupt handler for receiving values */
641         if ((retval = usb_submit_urb(dev->int_in_urb, GFP_KERNEL)) < 0) {
642                 dev_err(&interface->dev, "Error %d while submitting URB\n", retval);
643                 retval = -EFAULT;
644                 goto out;
645         }
646         /* increment our usage count for the driver */
647         ++dev->opened;
648         /* save our object in the file's private structure */
649         file->private_data = dev;
650         retval = 0;
651
652 out:
653         mutex_unlock(&dev->mutex);
654         mutex_unlock(&iowarrior_mutex);
655         return retval;
656 }
657
658 /**
659  *      iowarrior_release
660  */
661 static int iowarrior_release(struct inode *inode, struct file *file)
662 {
663         struct iowarrior *dev;
664         int retval = 0;
665
666         dev = file->private_data;
667         if (!dev)
668                 return -ENODEV;
669
670         dev_dbg(&dev->interface->dev, "minor %d\n", dev->minor);
671
672         /* lock our device */
673         mutex_lock(&dev->mutex);
674
675         if (dev->opened <= 0) {
676                 retval = -ENODEV;       /* close called more than once */
677                 mutex_unlock(&dev->mutex);
678         } else {
679                 dev->opened = 0;        /* we're closing now */
680                 retval = 0;
681                 if (dev->present) {
682                         /*
683                            The device is still connected so we only shutdown
684                            pending read-/write-ops.
685                          */
686                         usb_kill_urb(dev->int_in_urb);
687                         wake_up_interruptible(&dev->read_wait);
688                         wake_up_interruptible(&dev->write_wait);
689                         mutex_unlock(&dev->mutex);
690                 } else {
691                         /* The device was unplugged, cleanup resources */
692                         mutex_unlock(&dev->mutex);
693                         iowarrior_delete(dev);
694                 }
695         }
696         return retval;
697 }
698
699 static unsigned iowarrior_poll(struct file *file, poll_table * wait)
700 {
701         struct iowarrior *dev = file->private_data;
702         unsigned int mask = 0;
703
704         if (!dev->present)
705                 return POLLERR | POLLHUP;
706
707         poll_wait(file, &dev->read_wait, wait);
708         poll_wait(file, &dev->write_wait, wait);
709
710         if (!dev->present)
711                 return POLLERR | POLLHUP;
712
713         if (read_index(dev) != -1)
714                 mask |= POLLIN | POLLRDNORM;
715
716         if (atomic_read(&dev->write_busy) < MAX_WRITES_IN_FLIGHT)
717                 mask |= POLLOUT | POLLWRNORM;
718         return mask;
719 }
720
721 /*
722  * File operations needed when we register this driver.
723  * This assumes that this driver NEEDS file operations,
724  * of course, which means that the driver is expected
725  * to have a node in the /dev directory. If the USB
726  * device were for a network interface then the driver
727  * would use "struct net_driver" instead, and a serial
728  * device would use "struct tty_driver".
729  */
730 static const struct file_operations iowarrior_fops = {
731         .owner = THIS_MODULE,
732         .write = iowarrior_write,
733         .read = iowarrior_read,
734         .unlocked_ioctl = iowarrior_ioctl,
735         .open = iowarrior_open,
736         .release = iowarrior_release,
737         .poll = iowarrior_poll,
738         .llseek = noop_llseek,
739 };
740
741 static char *iowarrior_devnode(struct device *dev, umode_t *mode)
742 {
743         return kasprintf(GFP_KERNEL, "usb/%s", dev_name(dev));
744 }
745
746 /*
747  * usb class driver info in order to get a minor number from the usb core,
748  * and to have the device registered with devfs and the driver core
749  */
750 static struct usb_class_driver iowarrior_class = {
751         .name = "iowarrior%d",
752         .devnode = iowarrior_devnode,
753         .fops = &iowarrior_fops,
754         .minor_base = IOWARRIOR_MINOR_BASE,
755 };
756
757 /*---------------------------------*/
758 /*  probe and disconnect functions */
759 /*---------------------------------*/
760 /**
761  *      iowarrior_probe
762  *
763  *      Called by the usb core when a new device is connected that it thinks
764  *      this driver might be interested in.
765  */
766 static int iowarrior_probe(struct usb_interface *interface,
767                            const struct usb_device_id *id)
768 {
769         struct usb_device *udev = interface_to_usbdev(interface);
770         struct iowarrior *dev = NULL;
771         struct usb_host_interface *iface_desc;
772         int retval = -ENOMEM;
773         int res;
774
775         /* allocate memory for our device state and initialize it */
776         dev = kzalloc(sizeof(struct iowarrior), GFP_KERNEL);
777         if (!dev)
778                 return retval;
779
780         mutex_init(&dev->mutex);
781
782         atomic_set(&dev->intr_idx, 0);
783         atomic_set(&dev->read_idx, 0);
784         spin_lock_init(&dev->intr_idx_lock);
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  */
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);