2 * Native support for the I/O-Warrior USB devices
4 * Copyright (c) 2003-2005, 2020 Code Mercenaries GmbH
5 * written by Christian Lucht <lucht@codemercs.com> and
6 * Christoph Jung <jung@codemercs.com>
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
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
49 #define IOWARRIOR_MINOR_BASE 208 // SKELETON_MINOR_BASE 192 + 16, not official yet
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);
75 /* Structure to hold all of our device specific stuff */
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 */
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 */
99 struct usb_anchor submitted;
106 #define USB_REQ_GET_REPORT 0x01
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)
112 return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
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);
121 #define USB_REQ_SET_REPORT 0x09
123 static int usb_set_report(struct usb_interface *intf, unsigned char type,
124 unsigned char id, void *buf, int size)
126 return usb_control_msg(interface_to_usbdev(intf),
127 usb_sndctrlpipe(interface_to_usbdev(intf), 0),
129 USB_TYPE_CLASS | USB_RECIP_INTERFACE,
131 intf->cur_altsetting->desc.bInterfaceNumber, buf,
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 */
152 MODULE_DEVICE_TABLE(usb, iowarrior_ids);
155 * USB callback handler for reading data
157 static void iowarrior_callback(struct urb *urb)
159 struct iowarrior *dev = urb->context;
164 int status = urb->status;
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);
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);
191 (dev->read_queue + offset, urb->transfer_buffer,
193 /* equal values on interface 0 will be ignored */
194 spin_unlock(&dev->intr_idx_lock);
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);
208 /* +1 for serial number */
209 offset = intr_idx * (dev->report_size + 1);
210 memcpy(dev->read_queue + offset, urb->transfer_buffer,
212 *(dev->read_queue + offset + (dev->report_size)) = dev->serial_number++;
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);
220 retval = usb_submit_urb(urb, GFP_ATOMIC);
222 dev_err(&dev->interface->dev, "%s - usb_submit_urb failed with result %d\n",
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;
236 /* sync/async unlink faults aren't errors */
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);
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);
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);
281 static ssize_t iowarrior_read(struct file *file, char __user *buffer,
282 size_t count, loff_t *ppos)
284 struct iowarrior *dev;
288 dev = file->private_data;
290 /* verify that the device wasn't unplugged */
291 if (!dev || !dev->present)
294 dev_dbg(&dev->interface->dev, "minor %d, count = %zd\n",
297 /* read count must be packet size (+ time stamp) */
298 if ((count != dev->report_size)
299 && (count != (dev->report_size + 1)))
302 /* repeat until no buffer overrun in callback handler occur */
304 atomic_set(&dev->overflow_flag, 0);
305 if ((read_idx = read_index(dev)) == -1) {
307 if (file->f_flags & O_NONBLOCK)
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,
318 //we were interrupted by a signal
322 //The device was unplugged
325 if (read_idx == -1) {
326 // Can this happen ???
332 offset = read_idx * (dev->report_size + 1);
333 if (copy_to_user(buffer, dev->read_queue + offset, count)) {
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);
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;
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 */
363 dev_dbg(&dev->interface->dev, "minor %d, count = %zd\n",
365 /* if count is 0 we're already done */
370 /* We only accept full reports */
371 if (count != dev->report_size) {
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);
384 retval = PTR_ERR(buf);
387 retval = usb_set_report(dev->interface, 2, 0, buf, count);
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) {
403 retval = wait_event_interruptible(dev->write_wait,
404 (!dev->present || (atomic_read (&dev-> write_busy) < MAX_WRITES_IN_FLIGHT)));
406 /* we were interrupted by a signal */
411 /* The device was unplugged */
416 /* We were closed while waiting for an URB */
422 atomic_inc(&dev->write_busy);
423 int_out_urb = usb_alloc_urb(0, GFP_KERNEL);
428 buf = usb_alloc_coherent(dev->udev, dev->report_size,
429 GFP_KERNEL, &int_out_urb->transfer_dma);
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)) {
447 usb_anchor_urb(int_out_urb, &dev->submitted);
448 retval = usb_submit_urb(int_out_urb, GFP_KERNEL);
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);
458 usb_free_urb(int_out_urb);
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);
470 usb_free_coherent(dev->udev, dev->report_size, buf,
471 int_out_urb->transfer_dma);
473 usb_free_urb(int_out_urb);
475 atomic_dec(&dev->write_busy);
476 wake_up_interruptible(&dev->write_wait);
478 mutex_unlock(&dev->mutex);
485 static long iowarrior_ioctl(struct file *file, unsigned int cmd,
488 struct iowarrior *dev = NULL;
490 __u8 __user *user_buffer;
492 int io_res; /* checks for bytes read/written and copy_to/from_user results */
494 dev = file->private_data;
498 buffer = kzalloc(dev->report_size, GFP_KERNEL);
502 /* lock this object */
503 mutex_lock(&iowarrior_mutex);
504 mutex_lock(&dev->mutex);
506 /* verify that the device wasn't unplugged */
512 dev_dbg(&dev->interface->dev, "minor %d, cmd 0x%.4x, arg %ld\n",
513 dev->minor, cmd, arg);
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,
530 io_res = usb_set_report(dev->interface, 2, 0,
538 dev_err(&dev->interface->dev,
539 "ioctl 'IOW_WRITE' is not supported for product=0x%x.\n",
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);
551 io_res = copy_to_user(user_buffer, buffer, dev->report_size);
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 */
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));
590 /* return that we did not understand this ioctl call */
595 /* unlock the device */
596 mutex_unlock(&dev->mutex);
597 mutex_unlock(&iowarrior_mutex);
605 static int iowarrior_open(struct inode *inode, struct file *file)
607 struct iowarrior *dev = NULL;
608 struct usb_interface *interface;
612 mutex_lock(&iowarrior_mutex);
613 subminor = iminor(inode);
615 interface = usb_find_interface(&iowarrior_driver, subminor);
617 mutex_unlock(&iowarrior_mutex);
618 printk(KERN_ERR "%s - error, can't find device for minor %d\n",
623 mutex_lock(&iowarrior_open_disc_lock);
624 dev = usb_get_intfdata(interface);
626 mutex_unlock(&iowarrior_open_disc_lock);
627 mutex_unlock(&iowarrior_mutex);
631 mutex_lock(&dev->mutex);
632 mutex_unlock(&iowarrior_open_disc_lock);
634 /* Only one process can open each device, no sharing. */
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);
646 /* increment our usage count for the driver */
648 /* save our object in the file's private structure */
649 file->private_data = dev;
653 mutex_unlock(&dev->mutex);
654 mutex_unlock(&iowarrior_mutex);
661 static int iowarrior_release(struct inode *inode, struct file *file)
663 struct iowarrior *dev;
666 dev = file->private_data;
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);
679 dev->opened = 0; /* we're closing now */
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);
691 /* The device was unplugged, cleanup resources */
692 mutex_unlock(&dev->mutex);
693 iowarrior_delete(dev);
699 static unsigned iowarrior_poll(struct file *file, poll_table * wait)
701 struct iowarrior *dev = file->private_data;
702 unsigned int mask = 0;
705 return POLLERR | POLLHUP;
707 poll_wait(file, &dev->read_wait, wait);
708 poll_wait(file, &dev->write_wait, wait);
711 return POLLERR | POLLHUP;
713 if (read_index(dev) != -1)
714 mask |= POLLIN | POLLRDNORM;
716 if (atomic_read(&dev->write_busy) < MAX_WRITES_IN_FLIGHT)
717 mask |= POLLOUT | POLLWRNORM;
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 /*---------------------------------*/
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;
775 /* allocate memory for our device state and initialize it */
776 dev = kzalloc(sizeof(struct iowarrior), GFP_KERNEL);
780 mutex_init(&dev->mutex);
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);
791 dev->interface = usb_get_intf(interface);
793 iface_desc = interface->cur_altsetting;
794 dev->product_id = le16_to_cpu(udev->descriptor.idProduct);
796 init_usb_anchor(&dev->submitted);
798 res = usb_find_last_int_in_endpoint(iface_desc, &dev->int_in_endpoint);
800 dev_err(&interface->dev, "no interrupt-in endpoint found\n");
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);
813 dev_err(&interface->dev, "no interrupt-out endpoint found\n");
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);
823 * Some devices need the report size to be different than the
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;
833 case USB_DEVICE_ID_CODEMERCS_IOW28:
834 case USB_DEVICE_ID_CODEMERCS_IOW28L:
835 dev->report_size = 4;
838 case USB_DEVICE_ID_CODEMERCS_IOW100:
839 dev->report_size = 13;
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)
848 dev->int_in_buffer = kmalloc(dev->report_size, GFP_KERNEL);
849 if (!dev->int_in_buffer)
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 */
859 kmalloc(((dev->report_size + 1) * MAX_INTERRUPT_BUFFER),
861 if (!dev->read_queue)
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));
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),
874 USB_TYPE_CLASS | USB_RECIP_INTERFACE, 0,
875 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
877 /* allow device read and ioctl */
880 /* we can register the device now, as it is ready */
881 usb_set_intfdata(interface, dev);
883 retval = usb_register_dev(interface, &iowarrior_class);
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);
891 dev->minor = interface->minor;
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);
900 iowarrior_delete(dev);
905 * iowarrior_disconnect
907 * Called by the usb core when the device is removed from the system.
909 static void iowarrior_disconnect(struct usb_interface *interface)
911 struct iowarrior *dev;
914 dev = usb_get_intfdata(interface);
915 mutex_lock(&iowarrior_open_disc_lock);
916 usb_set_intfdata(interface, NULL);
919 mutex_unlock(&iowarrior_open_disc_lock);
920 /* give back our minor - this will call close() locks need to be dropped at this point*/
922 usb_deregister_dev(interface, &iowarrior_class);
924 mutex_lock(&dev->mutex);
926 /* prevent device read, write and ioctl */
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.
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);
940 /* no process is using the device, cleanup now */
941 mutex_unlock(&dev->mutex);
942 iowarrior_delete(dev);
945 dev_info(&interface->dev, "I/O-Warror #%d now disconnected\n",
946 minor - IOWARRIOR_MINOR_BASE);
949 /* usb specific object needed to register this driver with the usb subsystem */
950 static struct usb_driver iowarrior_driver = {
952 .probe = iowarrior_probe,
953 .disconnect = iowarrior_disconnect,
954 .id_table = iowarrior_ids,
957 module_usb_driver(iowarrior_driver);