Linux 4.19.133
[linux/fpc-iii.git] / drivers / usb / misc / iowarrior.c
blob92875a264b14a4885897d9f585708b0c9cd99650
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Native support for the I/O-Warrior USB devices
5 * Copyright (c) 2003-2005 Code Mercenaries GmbH
6 * written by Christian Lucht <lucht@codemercs.com>
8 * based on
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>
14 * Released under the GPLv2.
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>
25 #define DRIVER_AUTHOR "Christian Lucht <lucht@codemercs.com>"
26 #define DRIVER_DESC "USB IO-Warrior driver"
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
41 /* OEMed devices */
42 #define USB_DEVICE_ID_CODEMERCS_IOW24SAG 0x158a
43 #define USB_DEVICE_ID_CODEMERCS_IOW56AM 0x158b
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
52 /* interrupt input queue size */
53 #define MAX_INTERRUPT_BUFFER 16
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.
59 #define MAX_WRITES_IN_FLIGHT 4
61 MODULE_AUTHOR(DRIVER_AUTHOR);
62 MODULE_DESCRIPTION(DRIVER_DESC);
63 MODULE_LICENSE("GPL");
65 /* Module parameters */
66 static DEFINE_MUTEX(iowarrior_mutex);
68 static struct usb_driver iowarrior_driver;
69 static DEFINE_MUTEX(iowarrior_open_disc_lock);
71 /*--------------*/
72 /* data */
73 /*--------------*/
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 atomic_t overflow_flag; /* signals an index 'rollover' */
93 int present; /* this is 1 as long as the device is connected */
94 int opened; /* this is 1 if the device is currently open */
95 char chip_serial[9]; /* the serial number string of the chip connected */
96 int report_size; /* number of bytes in a report */
97 u16 product_id;
98 struct usb_anchor submitted;
101 /*--------------*/
102 /* globals */
103 /*--------------*/
106 * USB spec identifies 5 second timeouts.
108 #define GET_TIMEOUT 5
109 #define USB_REQ_GET_REPORT 0x01
110 //#if 0
111 static int usb_get_report(struct usb_device *dev,
112 struct usb_host_interface *inter, unsigned char type,
113 unsigned char id, void *buf, int size)
115 return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
116 USB_REQ_GET_REPORT,
117 USB_DIR_IN | USB_TYPE_CLASS |
118 USB_RECIP_INTERFACE, (type << 8) + id,
119 inter->desc.bInterfaceNumber, buf, size,
120 GET_TIMEOUT*HZ);
122 //#endif
124 #define USB_REQ_SET_REPORT 0x09
126 static int usb_set_report(struct usb_interface *intf, unsigned char type,
127 unsigned char id, void *buf, int size)
129 return usb_control_msg(interface_to_usbdev(intf),
130 usb_sndctrlpipe(interface_to_usbdev(intf), 0),
131 USB_REQ_SET_REPORT,
132 USB_TYPE_CLASS | USB_RECIP_INTERFACE,
133 (type << 8) + id,
134 intf->cur_altsetting->desc.bInterfaceNumber, buf,
135 size, HZ);
138 /*---------------------*/
139 /* driver registration */
140 /*---------------------*/
141 /* table of devices that work with this driver */
142 static const struct usb_device_id iowarrior_ids[] = {
143 {USB_DEVICE(USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOW40)},
144 {USB_DEVICE(USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOW24)},
145 {USB_DEVICE(USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOWPV1)},
146 {USB_DEVICE(USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOWPV2)},
147 {USB_DEVICE(USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOW56)},
148 {USB_DEVICE(USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOW24SAG)},
149 {USB_DEVICE(USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOW56AM)},
150 {USB_DEVICE(USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOW28)},
151 {USB_DEVICE(USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOW28L)},
152 {USB_DEVICE(USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOW100)},
153 {} /* Terminating entry */
155 MODULE_DEVICE_TABLE(usb, iowarrior_ids);
158 * USB callback handler for reading data
160 static void iowarrior_callback(struct urb *urb)
162 struct iowarrior *dev = urb->context;
163 int intr_idx;
164 int read_idx;
165 int aux_idx;
166 int offset;
167 int status = urb->status;
168 int retval;
170 switch (status) {
171 case 0:
172 /* success */
173 break;
174 case -ECONNRESET:
175 case -ENOENT:
176 case -ESHUTDOWN:
177 return;
178 default:
179 goto exit;
182 intr_idx = atomic_read(&dev->intr_idx);
183 /* aux_idx become previous intr_idx */
184 aux_idx = (intr_idx == 0) ? (MAX_INTERRUPT_BUFFER - 1) : (intr_idx - 1);
185 read_idx = atomic_read(&dev->read_idx);
187 /* queue is not empty and it's interface 0 */
188 if ((intr_idx != read_idx)
189 && (dev->interface->cur_altsetting->desc.bInterfaceNumber == 0)) {
190 /* + 1 for serial number */
191 offset = aux_idx * (dev->report_size + 1);
192 if (!memcmp
193 (dev->read_queue + offset, urb->transfer_buffer,
194 dev->report_size)) {
195 /* equal values on interface 0 will be ignored */
196 goto exit;
200 /* aux_idx become next intr_idx */
201 aux_idx = (intr_idx == (MAX_INTERRUPT_BUFFER - 1)) ? 0 : (intr_idx + 1);
202 if (read_idx == aux_idx) {
203 /* queue full, dropping oldest input */
204 read_idx = (++read_idx == MAX_INTERRUPT_BUFFER) ? 0 : read_idx;
205 atomic_set(&dev->read_idx, read_idx);
206 atomic_set(&dev->overflow_flag, 1);
209 /* +1 for serial number */
210 offset = intr_idx * (dev->report_size + 1);
211 memcpy(dev->read_queue + offset, urb->transfer_buffer,
212 dev->report_size);
213 *(dev->read_queue + offset + (dev->report_size)) = dev->serial_number++;
215 atomic_set(&dev->intr_idx, aux_idx);
216 /* tell the blocking read about the new data */
217 wake_up_interruptible(&dev->read_wait);
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);
228 * USB Callback handler for write-ops
230 static void iowarrior_write_callback(struct urb *urb)
232 struct iowarrior *dev;
233 int status = urb->status;
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);
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);
252 * iowarrior_delete
254 static inline void iowarrior_delete(struct iowarrior *dev)
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);
264 /*---------------------*/
265 /* fops implementation */
266 /*---------------------*/
268 static int read_index(struct iowarrior *dev)
270 int intr_idx, read_idx;
272 read_idx = atomic_read(&dev->read_idx);
273 intr_idx = atomic_read(&dev->intr_idx);
275 return (read_idx == intr_idx ? -1 : read_idx);
279 * iowarrior_read
281 static ssize_t iowarrior_read(struct file *file, char __user *buffer,
282 size_t count, loff_t *ppos)
284 struct iowarrior *dev;
285 int read_idx;
286 int offset;
288 dev = file->private_data;
290 /* verify that the device wasn't unplugged */
291 if (!dev || !dev->present)
292 return -ENODEV;
294 dev_dbg(&dev->interface->dev, "minor %d, count = %zd\n",
295 dev->minor, count);
297 /* read count must be packet size (+ time stamp) */
298 if ((count != dev->report_size)
299 && (count != (dev->report_size + 1)))
300 return -EINVAL;
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;
321 if (!dev->present) {
322 //The device was unplugged
323 return -ENODEV;
325 if (read_idx == -1) {
326 // Can this happen ???
327 return 0;
332 offset = read_idx * (dev->report_size + 1);
333 if (copy_to_user(buffer, dev->read_queue + offset, count)) {
334 return -EFAULT;
336 } while (atomic_read(&dev->overflow_flag));
338 read_idx = ++read_idx == MAX_INTERRUPT_BUFFER ? 0 : read_idx;
339 atomic_set(&dev->read_idx, read_idx);
340 return count;
344 * iowarrior_write
346 static ssize_t iowarrior_write(struct file *file,
347 const char __user *user_buffer,
348 size_t count, loff_t *ppos)
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;
355 dev = file->private_data;
357 mutex_lock(&dev->mutex);
358 /* verify that the device wasn't unplugged */
359 if (!dev->present) {
360 retval = -ENODEV;
361 goto exit;
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;
370 /* We only accept full reports */
371 if (count != dev->report_size) {
372 retval = -EINVAL;
373 goto exit;
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;
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;
410 if (!dev->present) {
411 /* The device was unplugged */
412 retval = -ENODEV;
413 goto exit;
415 if (!dev->opened) {
416 /* We were closed while waiting for an URB */
417 retval = -ENODEV;
418 goto exit;
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;
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;
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;
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;
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;
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;
483 * iowarrior_ioctl
485 static long iowarrior_ioctl(struct file *file, unsigned int cmd,
486 unsigned long arg)
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 */
494 dev = file->private_data;
495 if (!dev)
496 return -ENODEV;
498 buffer = kzalloc(dev->report_size, GFP_KERNEL);
499 if (!buffer)
500 return -ENOMEM;
502 /* lock this object */
503 mutex_lock(&iowarrior_mutex);
504 mutex_lock(&dev->mutex);
506 /* verify that the device wasn't unplugged */
507 if (!dev->present) {
508 retval = -ENODEV;
509 goto error_out;
512 dev_dbg(&dev->interface->dev, "minor %d, cmd 0x%.4x, arg %ld\n",
513 dev->minor, cmd, arg);
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;
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);
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;
555 break;
556 case IOW_GETINFO:
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;
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);
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;
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;
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;
589 default:
590 /* return that we did not understand this ioctl call */
591 retval = -ENOTTY;
592 break;
594 error_out:
595 /* unlock the device */
596 mutex_unlock(&dev->mutex);
597 mutex_unlock(&iowarrior_mutex);
598 kfree(buffer);
599 return retval;
603 * iowarrior_open
605 static int iowarrior_open(struct inode *inode, struct file *file)
607 struct iowarrior *dev = NULL;
608 struct usb_interface *interface;
609 int subminor;
610 int retval = 0;
612 mutex_lock(&iowarrior_mutex);
613 subminor = iminor(inode);
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;
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;
631 mutex_lock(&dev->mutex);
632 mutex_unlock(&iowarrior_open_disc_lock);
634 /* Only one process can open each device, no sharing. */
635 if (dev->opened) {
636 retval = -EBUSY;
637 goto out;
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;
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;
652 out:
653 mutex_unlock(&dev->mutex);
654 mutex_unlock(&iowarrior_mutex);
655 return retval;
659 * iowarrior_release
661 static int iowarrior_release(struct inode *inode, struct file *file)
663 struct iowarrior *dev;
664 int retval = 0;
666 dev = file->private_data;
667 if (!dev)
668 return -ENODEV;
670 dev_dbg(&dev->interface->dev, "minor %d\n", dev->minor);
672 /* lock our device */
673 mutex_lock(&dev->mutex);
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) {
683 The device is still connected so we only shutdown
684 pending read-/write-ops.
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);
696 return retval;
699 static __poll_t iowarrior_poll(struct file *file, poll_table * wait)
701 struct iowarrior *dev = file->private_data;
702 __poll_t mask = 0;
704 if (!dev->present)
705 return EPOLLERR | EPOLLHUP;
707 poll_wait(file, &dev->read_wait, wait);
708 poll_wait(file, &dev->write_wait, wait);
710 if (!dev->present)
711 return EPOLLERR | EPOLLHUP;
713 if (read_index(dev) != -1)
714 mask |= EPOLLIN | EPOLLRDNORM;
716 if (atomic_read(&dev->write_busy) < MAX_WRITES_IN_FLIGHT)
717 mask |= EPOLLOUT | EPOLLWRNORM;
718 return mask;
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".
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,
741 static char *iowarrior_devnode(struct device *dev, umode_t *mode)
743 return kasprintf(GFP_KERNEL, "usb/%s", dev_name(dev));
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
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,
757 /*---------------------------------*/
758 /* probe and disconnect functions */
759 /*---------------------------------*/
761 * iowarrior_probe
763 * Called by the usb core when a new device is connected that it thinks
764 * this driver might be interested in.
766 static int iowarrior_probe(struct usb_interface *interface,
767 const struct usb_device_id *id)
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;
775 /* allocate memory for our device state and initialize it */
776 dev = kzalloc(sizeof(struct iowarrior), GFP_KERNEL);
777 if (!dev)
778 return retval;
780 mutex_init(&dev->mutex);
782 atomic_set(&dev->intr_idx, 0);
783 atomic_set(&dev->read_idx, 0);
784 atomic_set(&dev->overflow_flag, 0);
785 init_waitqueue_head(&dev->read_wait);
786 atomic_set(&dev->write_busy, 0);
787 init_waitqueue_head(&dev->write_wait);
789 dev->udev = udev;
790 dev->interface = usb_get_intf(interface);
792 iface_desc = interface->cur_altsetting;
793 dev->product_id = le16_to_cpu(udev->descriptor.idProduct);
795 init_usb_anchor(&dev->submitted);
797 res = usb_find_last_int_in_endpoint(iface_desc, &dev->int_in_endpoint);
798 if (res) {
799 dev_err(&interface->dev, "no interrupt-in endpoint found\n");
800 retval = res;
801 goto error;
804 if ((dev->product_id == USB_DEVICE_ID_CODEMERCS_IOW56) ||
805 (dev->product_id == USB_DEVICE_ID_CODEMERCS_IOW56AM) ||
806 (dev->product_id == USB_DEVICE_ID_CODEMERCS_IOW28) ||
807 (dev->product_id == USB_DEVICE_ID_CODEMERCS_IOW28L) ||
808 (dev->product_id == USB_DEVICE_ID_CODEMERCS_IOW100)) {
809 res = usb_find_last_int_out_endpoint(iface_desc,
810 &dev->int_out_endpoint);
811 if (res) {
812 dev_err(&interface->dev, "no interrupt-out endpoint found\n");
813 retval = res;
814 goto error;
818 /* we have to check the report_size often, so remember it in the endianness suitable for our machine */
819 dev->report_size = usb_endpoint_maxp(dev->int_in_endpoint);
820 if ((dev->interface->cur_altsetting->desc.bInterfaceNumber == 0) &&
821 ((dev->product_id == USB_DEVICE_ID_CODEMERCS_IOW56) ||
822 (dev->product_id == USB_DEVICE_ID_CODEMERCS_IOW56AM) ||
823 (dev->product_id == USB_DEVICE_ID_CODEMERCS_IOW28) ||
824 (dev->product_id == USB_DEVICE_ID_CODEMERCS_IOW28L) ||
825 (dev->product_id == USB_DEVICE_ID_CODEMERCS_IOW100)))
826 /* IOWarrior56 has wMaxPacketSize different from report size */
827 dev->report_size = 7;
829 /* create the urb and buffer for reading */
830 dev->int_in_urb = usb_alloc_urb(0, GFP_KERNEL);
831 if (!dev->int_in_urb)
832 goto error;
833 dev->int_in_buffer = kmalloc(dev->report_size, GFP_KERNEL);
834 if (!dev->int_in_buffer)
835 goto error;
836 usb_fill_int_urb(dev->int_in_urb, dev->udev,
837 usb_rcvintpipe(dev->udev,
838 dev->int_in_endpoint->bEndpointAddress),
839 dev->int_in_buffer, dev->report_size,
840 iowarrior_callback, dev,
841 dev->int_in_endpoint->bInterval);
842 /* create an internal buffer for interrupt data from the device */
843 dev->read_queue =
844 kmalloc(((dev->report_size + 1) * MAX_INTERRUPT_BUFFER),
845 GFP_KERNEL);
846 if (!dev->read_queue)
847 goto error;
848 /* Get the serial-number of the chip */
849 memset(dev->chip_serial, 0x00, sizeof(dev->chip_serial));
850 usb_string(udev, udev->descriptor.iSerialNumber, dev->chip_serial,
851 sizeof(dev->chip_serial));
852 if (strlen(dev->chip_serial) != 8)
853 memset(dev->chip_serial, 0x00, sizeof(dev->chip_serial));
855 /* Set the idle timeout to 0, if this is interface 0 */
856 if (dev->interface->cur_altsetting->desc.bInterfaceNumber == 0) {
857 usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
858 0x0A,
859 USB_TYPE_CLASS | USB_RECIP_INTERFACE, 0,
860 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
862 /* allow device read and ioctl */
863 dev->present = 1;
865 /* we can register the device now, as it is ready */
866 usb_set_intfdata(interface, dev);
868 retval = usb_register_dev(interface, &iowarrior_class);
869 if (retval) {
870 /* something prevented us from registering this driver */
871 dev_err(&interface->dev, "Not able to get a minor for this device.\n");
872 usb_set_intfdata(interface, NULL);
873 goto error;
876 dev->minor = interface->minor;
878 /* let the user know what node this device is now attached to */
879 dev_info(&interface->dev, "IOWarrior product=0x%x, serial=%s interface=%d "
880 "now attached to iowarrior%d\n", dev->product_id, dev->chip_serial,
881 iface_desc->desc.bInterfaceNumber, dev->minor - IOWARRIOR_MINOR_BASE);
882 return retval;
884 error:
885 iowarrior_delete(dev);
886 return retval;
890 * iowarrior_disconnect
892 * Called by the usb core when the device is removed from the system.
894 static void iowarrior_disconnect(struct usb_interface *interface)
896 struct iowarrior *dev;
897 int minor;
899 dev = usb_get_intfdata(interface);
900 mutex_lock(&iowarrior_open_disc_lock);
901 usb_set_intfdata(interface, NULL);
903 minor = dev->minor;
904 mutex_unlock(&iowarrior_open_disc_lock);
905 /* give back our minor - this will call close() locks need to be dropped at this point*/
907 usb_deregister_dev(interface, &iowarrior_class);
909 mutex_lock(&dev->mutex);
911 /* prevent device read, write and ioctl */
912 dev->present = 0;
914 if (dev->opened) {
915 /* There is a process that holds a filedescriptor to the device ,
916 so we only shutdown read-/write-ops going on.
917 Deleting the device is postponed until close() was called.
919 usb_kill_urb(dev->int_in_urb);
920 usb_kill_anchored_urbs(&dev->submitted);
921 wake_up_interruptible(&dev->read_wait);
922 wake_up_interruptible(&dev->write_wait);
923 mutex_unlock(&dev->mutex);
924 } else {
925 /* no process is using the device, cleanup now */
926 mutex_unlock(&dev->mutex);
927 iowarrior_delete(dev);
930 dev_info(&interface->dev, "I/O-Warror #%d now disconnected\n",
931 minor - IOWARRIOR_MINOR_BASE);
934 /* usb specific object needed to register this driver with the usb subsystem */
935 static struct usb_driver iowarrior_driver = {
936 .name = "iowarrior",
937 .probe = iowarrior_probe,
938 .disconnect = iowarrior_disconnect,
939 .id_table = iowarrior_ids,
942 module_usb_driver(iowarrior_driver);