1 // SPDX-License-Identifier: GPL-2.0
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>
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 static struct usb_driver iowarrior_driver
;
71 /* Structure to hold all of our device specific stuff */
73 struct mutex mutex
; /* locks this structure */
74 struct usb_device
*udev
; /* save off the usb device pointer */
75 struct usb_interface
*interface
; /* the interface for this device */
76 unsigned char minor
; /* the starting minor number for this device */
77 struct usb_endpoint_descriptor
*int_out_endpoint
; /* endpoint for reading (needed for IOW56 only) */
78 struct usb_endpoint_descriptor
*int_in_endpoint
; /* endpoint for reading */
79 struct urb
*int_in_urb
; /* the urb for reading data */
80 unsigned char *int_in_buffer
; /* buffer for data to be read */
81 unsigned char serial_number
; /* to detect lost packages */
82 unsigned char *read_queue
; /* size is MAX_INTERRUPT_BUFFER * packet size */
83 wait_queue_head_t read_wait
;
84 wait_queue_head_t write_wait
; /* wait-queue for writing to the device */
85 atomic_t write_busy
; /* number of write-urbs submitted */
88 atomic_t overflow_flag
; /* signals an index 'rollover' */
89 int present
; /* this is 1 as long as the device is connected */
90 int opened
; /* this is 1 if the device is currently open */
91 char chip_serial
[9]; /* the serial number string of the chip connected */
92 int report_size
; /* number of bytes in a report */
94 struct usb_anchor submitted
;
102 * USB spec identifies 5 second timeouts.
104 #define GET_TIMEOUT 5
105 #define USB_REQ_GET_REPORT 0x01
107 static int usb_get_report(struct usb_device
*dev
,
108 struct usb_host_interface
*inter
, unsigned char type
,
109 unsigned char id
, void *buf
, int size
)
111 return usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0),
113 USB_DIR_IN
| USB_TYPE_CLASS
|
114 USB_RECIP_INTERFACE
, (type
<< 8) + id
,
115 inter
->desc
.bInterfaceNumber
, buf
, size
,
120 #define USB_REQ_SET_REPORT 0x09
122 static int usb_set_report(struct usb_interface
*intf
, unsigned char type
,
123 unsigned char id
, void *buf
, int size
)
125 return usb_control_msg(interface_to_usbdev(intf
),
126 usb_sndctrlpipe(interface_to_usbdev(intf
), 0),
128 USB_TYPE_CLASS
| USB_RECIP_INTERFACE
,
130 intf
->cur_altsetting
->desc
.bInterfaceNumber
, buf
,
134 /*---------------------*/
135 /* driver registration */
136 /*---------------------*/
137 /* table of devices that work with this driver */
138 static const struct usb_device_id iowarrior_ids
[] = {
139 {USB_DEVICE(USB_VENDOR_ID_CODEMERCS
, USB_DEVICE_ID_CODEMERCS_IOW40
)},
140 {USB_DEVICE(USB_VENDOR_ID_CODEMERCS
, USB_DEVICE_ID_CODEMERCS_IOW24
)},
141 {USB_DEVICE(USB_VENDOR_ID_CODEMERCS
, USB_DEVICE_ID_CODEMERCS_IOWPV1
)},
142 {USB_DEVICE(USB_VENDOR_ID_CODEMERCS
, USB_DEVICE_ID_CODEMERCS_IOWPV2
)},
143 {USB_DEVICE(USB_VENDOR_ID_CODEMERCS
, USB_DEVICE_ID_CODEMERCS_IOW56
)},
144 {USB_DEVICE(USB_VENDOR_ID_CODEMERCS
, USB_DEVICE_ID_CODEMERCS_IOW24SAG
)},
145 {USB_DEVICE(USB_VENDOR_ID_CODEMERCS
, USB_DEVICE_ID_CODEMERCS_IOW56AM
)},
146 {USB_DEVICE(USB_VENDOR_ID_CODEMERCS
, USB_DEVICE_ID_CODEMERCS_IOW28
)},
147 {USB_DEVICE(USB_VENDOR_ID_CODEMERCS
, USB_DEVICE_ID_CODEMERCS_IOW28L
)},
148 {USB_DEVICE(USB_VENDOR_ID_CODEMERCS
, USB_DEVICE_ID_CODEMERCS_IOW100
)},
149 {} /* Terminating entry */
151 MODULE_DEVICE_TABLE(usb
, iowarrior_ids
);
154 * USB callback handler for reading data
156 static void iowarrior_callback(struct urb
*urb
)
158 struct iowarrior
*dev
= urb
->context
;
163 int status
= urb
->status
;
178 intr_idx
= atomic_read(&dev
->intr_idx
);
179 /* aux_idx become previous intr_idx */
180 aux_idx
= (intr_idx
== 0) ? (MAX_INTERRUPT_BUFFER
- 1) : (intr_idx
- 1);
181 read_idx
= atomic_read(&dev
->read_idx
);
183 /* queue is not empty and it's interface 0 */
184 if ((intr_idx
!= read_idx
)
185 && (dev
->interface
->cur_altsetting
->desc
.bInterfaceNumber
== 0)) {
186 /* + 1 for serial number */
187 offset
= aux_idx
* (dev
->report_size
+ 1);
189 (dev
->read_queue
+ offset
, urb
->transfer_buffer
,
191 /* equal values on interface 0 will be ignored */
196 /* aux_idx become next intr_idx */
197 aux_idx
= (intr_idx
== (MAX_INTERRUPT_BUFFER
- 1)) ? 0 : (intr_idx
+ 1);
198 if (read_idx
== aux_idx
) {
199 /* queue full, dropping oldest input */
200 read_idx
= (++read_idx
== MAX_INTERRUPT_BUFFER
) ? 0 : read_idx
;
201 atomic_set(&dev
->read_idx
, read_idx
);
202 atomic_set(&dev
->overflow_flag
, 1);
205 /* +1 for serial number */
206 offset
= intr_idx
* (dev
->report_size
+ 1);
207 memcpy(dev
->read_queue
+ offset
, urb
->transfer_buffer
,
209 *(dev
->read_queue
+ offset
+ (dev
->report_size
)) = dev
->serial_number
++;
211 atomic_set(&dev
->intr_idx
, aux_idx
);
212 /* tell the blocking read about the new data */
213 wake_up_interruptible(&dev
->read_wait
);
216 retval
= usb_submit_urb(urb
, GFP_ATOMIC
);
218 dev_err(&dev
->interface
->dev
, "%s - usb_submit_urb failed with result %d\n",
224 * USB Callback handler for write-ops
226 static void iowarrior_write_callback(struct urb
*urb
)
228 struct iowarrior
*dev
;
229 int status
= urb
->status
;
232 /* sync/async unlink faults aren't errors */
234 !(status
== -ENOENT
||
235 status
== -ECONNRESET
|| status
== -ESHUTDOWN
)) {
236 dev_dbg(&dev
->interface
->dev
,
237 "nonzero write bulk status received: %d\n", status
);
239 /* free up our allocated buffer */
240 usb_free_coherent(urb
->dev
, urb
->transfer_buffer_length
,
241 urb
->transfer_buffer
, urb
->transfer_dma
);
242 /* tell a waiting writer the interrupt-out-pipe is available again */
243 atomic_dec(&dev
->write_busy
);
244 wake_up_interruptible(&dev
->write_wait
);
250 static inline void iowarrior_delete(struct iowarrior
*dev
)
252 dev_dbg(&dev
->interface
->dev
, "minor %d\n", dev
->minor
);
253 kfree(dev
->int_in_buffer
);
254 usb_free_urb(dev
->int_in_urb
);
255 kfree(dev
->read_queue
);
256 usb_put_intf(dev
->interface
);
260 /*---------------------*/
261 /* fops implementation */
262 /*---------------------*/
264 static int read_index(struct iowarrior
*dev
)
266 int intr_idx
, read_idx
;
268 read_idx
= atomic_read(&dev
->read_idx
);
269 intr_idx
= atomic_read(&dev
->intr_idx
);
271 return (read_idx
== intr_idx
? -1 : read_idx
);
277 static ssize_t
iowarrior_read(struct file
*file
, char __user
*buffer
,
278 size_t count
, loff_t
*ppos
)
280 struct iowarrior
*dev
;
284 dev
= file
->private_data
;
286 /* verify that the device wasn't unplugged */
287 if (!dev
|| !dev
->present
)
290 dev_dbg(&dev
->interface
->dev
, "minor %d, count = %zd\n",
293 /* read count must be packet size (+ time stamp) */
294 if ((count
!= dev
->report_size
)
295 && (count
!= (dev
->report_size
+ 1)))
298 /* repeat until no buffer overrun in callback handler occur */
300 atomic_set(&dev
->overflow_flag
, 0);
301 if ((read_idx
= read_index(dev
)) == -1) {
303 if (file
->f_flags
& O_NONBLOCK
)
306 //next line will return when there is either new data, or the device is unplugged
307 int r
= wait_event_interruptible(dev
->read_wait
,
314 //we were interrupted by a signal
318 //The device was unplugged
321 if (read_idx
== -1) {
322 // Can this happen ???
328 offset
= read_idx
* (dev
->report_size
+ 1);
329 if (copy_to_user(buffer
, dev
->read_queue
+ offset
, count
)) {
332 } while (atomic_read(&dev
->overflow_flag
));
334 read_idx
= ++read_idx
== MAX_INTERRUPT_BUFFER
? 0 : read_idx
;
335 atomic_set(&dev
->read_idx
, read_idx
);
342 static ssize_t
iowarrior_write(struct file
*file
,
343 const char __user
*user_buffer
,
344 size_t count
, loff_t
*ppos
)
346 struct iowarrior
*dev
;
348 char *buf
= NULL
; /* for IOW24 and IOW56 we need a buffer */
349 struct urb
*int_out_urb
= NULL
;
351 dev
= file
->private_data
;
353 mutex_lock(&dev
->mutex
);
354 /* verify that the device wasn't unplugged */
359 dev_dbg(&dev
->interface
->dev
, "minor %d, count = %zd\n",
361 /* if count is 0 we're already done */
366 /* We only accept full reports */
367 if (count
!= dev
->report_size
) {
371 switch (dev
->product_id
) {
372 case USB_DEVICE_ID_CODEMERCS_IOW24
:
373 case USB_DEVICE_ID_CODEMERCS_IOW24SAG
:
374 case USB_DEVICE_ID_CODEMERCS_IOWPV1
:
375 case USB_DEVICE_ID_CODEMERCS_IOWPV2
:
376 case USB_DEVICE_ID_CODEMERCS_IOW40
:
377 /* IOW24 and IOW40 use a synchronous call */
378 buf
= memdup_user(user_buffer
, count
);
380 retval
= PTR_ERR(buf
);
383 retval
= usb_set_report(dev
->interface
, 2, 0, buf
, count
);
387 case USB_DEVICE_ID_CODEMERCS_IOW56
:
388 case USB_DEVICE_ID_CODEMERCS_IOW56AM
:
389 case USB_DEVICE_ID_CODEMERCS_IOW28
:
390 case USB_DEVICE_ID_CODEMERCS_IOW28L
:
391 case USB_DEVICE_ID_CODEMERCS_IOW100
:
392 /* The IOW56 uses asynchronous IO and more urbs */
393 if (atomic_read(&dev
->write_busy
) == MAX_WRITES_IN_FLIGHT
) {
394 /* Wait until we are below the limit for submitted urbs */
395 if (file
->f_flags
& O_NONBLOCK
) {
399 retval
= wait_event_interruptible(dev
->write_wait
,
400 (!dev
->present
|| (atomic_read (&dev
-> write_busy
) < MAX_WRITES_IN_FLIGHT
)));
402 /* we were interrupted by a signal */
407 /* The device was unplugged */
412 /* We were closed while waiting for an URB */
418 atomic_inc(&dev
->write_busy
);
419 int_out_urb
= usb_alloc_urb(0, GFP_KERNEL
);
424 buf
= usb_alloc_coherent(dev
->udev
, dev
->report_size
,
425 GFP_KERNEL
, &int_out_urb
->transfer_dma
);
428 dev_dbg(&dev
->interface
->dev
,
429 "Unable to allocate buffer\n");
430 goto error_no_buffer
;
432 usb_fill_int_urb(int_out_urb
, dev
->udev
,
433 usb_sndintpipe(dev
->udev
,
434 dev
->int_out_endpoint
->bEndpointAddress
),
435 buf
, dev
->report_size
,
436 iowarrior_write_callback
, dev
,
437 dev
->int_out_endpoint
->bInterval
);
438 int_out_urb
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
439 if (copy_from_user(buf
, user_buffer
, count
)) {
443 usb_anchor_urb(int_out_urb
, &dev
->submitted
);
444 retval
= usb_submit_urb(int_out_urb
, GFP_KERNEL
);
446 dev_dbg(&dev
->interface
->dev
,
447 "submit error %d for urb nr.%d\n",
448 retval
, atomic_read(&dev
->write_busy
));
449 usb_unanchor_urb(int_out_urb
);
454 usb_free_urb(int_out_urb
);
458 /* what do we have here ? An unsupported Product-ID ? */
459 dev_err(&dev
->interface
->dev
, "%s - not supported for product=0x%x\n",
460 __func__
, dev
->product_id
);
466 usb_free_coherent(dev
->udev
, dev
->report_size
, buf
,
467 int_out_urb
->transfer_dma
);
469 usb_free_urb(int_out_urb
);
471 atomic_dec(&dev
->write_busy
);
472 wake_up_interruptible(&dev
->write_wait
);
474 mutex_unlock(&dev
->mutex
);
481 static long iowarrior_ioctl(struct file
*file
, unsigned int cmd
,
484 struct iowarrior
*dev
= NULL
;
486 __u8 __user
*user_buffer
;
488 int io_res
; /* checks for bytes read/written and copy_to/from_user results */
490 dev
= file
->private_data
;
494 buffer
= kzalloc(dev
->report_size
, GFP_KERNEL
);
498 mutex_lock(&dev
->mutex
);
500 /* verify that the device wasn't unplugged */
506 dev_dbg(&dev
->interface
->dev
, "minor %d, cmd 0x%.4x, arg %ld\n",
507 dev
->minor
, cmd
, arg
);
513 if (dev
->product_id
== USB_DEVICE_ID_CODEMERCS_IOW24
||
514 dev
->product_id
== USB_DEVICE_ID_CODEMERCS_IOW24SAG
||
515 dev
->product_id
== USB_DEVICE_ID_CODEMERCS_IOWPV1
||
516 dev
->product_id
== USB_DEVICE_ID_CODEMERCS_IOWPV2
||
517 dev
->product_id
== USB_DEVICE_ID_CODEMERCS_IOW40
) {
518 user_buffer
= (__u8 __user
*)arg
;
519 io_res
= copy_from_user(buffer
, user_buffer
,
524 io_res
= usb_set_report(dev
->interface
, 2, 0,
532 dev_err(&dev
->interface
->dev
,
533 "ioctl 'IOW_WRITE' is not supported for product=0x%x.\n",
538 user_buffer
= (__u8 __user
*)arg
;
539 io_res
= usb_get_report(dev
->udev
,
540 dev
->interface
->cur_altsetting
, 1, 0,
541 buffer
, dev
->report_size
);
545 io_res
= copy_to_user(user_buffer
, buffer
, dev
->report_size
);
552 /* Report available information for the device */
553 struct iowarrior_info info
;
554 /* needed for power consumption */
555 struct usb_config_descriptor
*cfg_descriptor
= &dev
->udev
->actconfig
->desc
;
557 memset(&info
, 0, sizeof(info
));
558 /* directly from the descriptor */
559 info
.vendor
= le16_to_cpu(dev
->udev
->descriptor
.idVendor
);
560 info
.product
= dev
->product_id
;
561 info
.revision
= le16_to_cpu(dev
->udev
->descriptor
.bcdDevice
);
563 /* 0==UNKNOWN, 1==LOW(usb1.1) ,2=FULL(usb1.1), 3=HIGH(usb2.0) */
564 info
.speed
= dev
->udev
->speed
;
565 info
.if_num
= dev
->interface
->cur_altsetting
->desc
.bInterfaceNumber
;
566 info
.report_size
= dev
->report_size
;
568 /* serial number string has been read earlier 8 chars or empty string */
569 memcpy(info
.serial
, dev
->chip_serial
,
570 sizeof(dev
->chip_serial
));
571 if (cfg_descriptor
== NULL
) {
572 info
.power
= -1; /* no information available */
574 /* the MaxPower is stored in units of 2mA to make it fit into a byte-value */
575 info
.power
= cfg_descriptor
->bMaxPower
* 2;
577 io_res
= copy_to_user((struct iowarrior_info __user
*)arg
, &info
,
578 sizeof(struct iowarrior_info
));
584 /* return that we did not understand this ioctl call */
589 /* unlock the device */
590 mutex_unlock(&dev
->mutex
);
598 static int iowarrior_open(struct inode
*inode
, struct file
*file
)
600 struct iowarrior
*dev
= NULL
;
601 struct usb_interface
*interface
;
605 subminor
= iminor(inode
);
607 interface
= usb_find_interface(&iowarrior_driver
, subminor
);
609 pr_err("%s - error, can't find device for minor %d\n",
614 dev
= usb_get_intfdata(interface
);
618 mutex_lock(&dev
->mutex
);
620 /* Only one process can open each device, no sharing. */
626 /* setup interrupt handler for receiving values */
627 if ((retval
= usb_submit_urb(dev
->int_in_urb
, GFP_KERNEL
)) < 0) {
628 dev_err(&interface
->dev
, "Error %d while submitting URB\n", retval
);
632 /* increment our usage count for the driver */
634 /* save our object in the file's private structure */
635 file
->private_data
= dev
;
639 mutex_unlock(&dev
->mutex
);
646 static int iowarrior_release(struct inode
*inode
, struct file
*file
)
648 struct iowarrior
*dev
;
651 dev
= file
->private_data
;
655 dev_dbg(&dev
->interface
->dev
, "minor %d\n", dev
->minor
);
657 /* lock our device */
658 mutex_lock(&dev
->mutex
);
660 if (dev
->opened
<= 0) {
661 retval
= -ENODEV
; /* close called more than once */
662 mutex_unlock(&dev
->mutex
);
664 dev
->opened
= 0; /* we're closing now */
668 The device is still connected so we only shutdown
669 pending read-/write-ops.
671 usb_kill_urb(dev
->int_in_urb
);
672 wake_up_interruptible(&dev
->read_wait
);
673 wake_up_interruptible(&dev
->write_wait
);
674 mutex_unlock(&dev
->mutex
);
676 /* The device was unplugged, cleanup resources */
677 mutex_unlock(&dev
->mutex
);
678 iowarrior_delete(dev
);
684 static __poll_t
iowarrior_poll(struct file
*file
, poll_table
* wait
)
686 struct iowarrior
*dev
= file
->private_data
;
690 return EPOLLERR
| EPOLLHUP
;
692 poll_wait(file
, &dev
->read_wait
, wait
);
693 poll_wait(file
, &dev
->write_wait
, wait
);
696 return EPOLLERR
| EPOLLHUP
;
698 if (read_index(dev
) != -1)
699 mask
|= EPOLLIN
| EPOLLRDNORM
;
701 if (atomic_read(&dev
->write_busy
) < MAX_WRITES_IN_FLIGHT
)
702 mask
|= EPOLLOUT
| EPOLLWRNORM
;
707 * File operations needed when we register this driver.
708 * This assumes that this driver NEEDS file operations,
709 * of course, which means that the driver is expected
710 * to have a node in the /dev directory. If the USB
711 * device were for a network interface then the driver
712 * would use "struct net_driver" instead, and a serial
713 * device would use "struct tty_driver".
715 static const struct file_operations iowarrior_fops
= {
716 .owner
= THIS_MODULE
,
717 .write
= iowarrior_write
,
718 .read
= iowarrior_read
,
719 .unlocked_ioctl
= iowarrior_ioctl
,
720 .open
= iowarrior_open
,
721 .release
= iowarrior_release
,
722 .poll
= iowarrior_poll
,
723 .llseek
= noop_llseek
,
726 static char *iowarrior_devnode(struct device
*dev
, umode_t
*mode
)
728 return kasprintf(GFP_KERNEL
, "usb/%s", dev_name(dev
));
732 * usb class driver info in order to get a minor number from the usb core,
733 * and to have the device registered with devfs and the driver core
735 static struct usb_class_driver iowarrior_class
= {
736 .name
= "iowarrior%d",
737 .devnode
= iowarrior_devnode
,
738 .fops
= &iowarrior_fops
,
739 .minor_base
= IOWARRIOR_MINOR_BASE
,
742 /*---------------------------------*/
743 /* probe and disconnect functions */
744 /*---------------------------------*/
748 * Called by the usb core when a new device is connected that it thinks
749 * this driver might be interested in.
751 static int iowarrior_probe(struct usb_interface
*interface
,
752 const struct usb_device_id
*id
)
754 struct usb_device
*udev
= interface_to_usbdev(interface
);
755 struct iowarrior
*dev
= NULL
;
756 struct usb_host_interface
*iface_desc
;
757 int retval
= -ENOMEM
;
760 /* allocate memory for our device state and initialize it */
761 dev
= kzalloc(sizeof(struct iowarrior
), GFP_KERNEL
);
765 mutex_init(&dev
->mutex
);
767 atomic_set(&dev
->intr_idx
, 0);
768 atomic_set(&dev
->read_idx
, 0);
769 atomic_set(&dev
->overflow_flag
, 0);
770 init_waitqueue_head(&dev
->read_wait
);
771 atomic_set(&dev
->write_busy
, 0);
772 init_waitqueue_head(&dev
->write_wait
);
775 dev
->interface
= usb_get_intf(interface
);
777 iface_desc
= interface
->cur_altsetting
;
778 dev
->product_id
= le16_to_cpu(udev
->descriptor
.idProduct
);
780 init_usb_anchor(&dev
->submitted
);
782 res
= usb_find_last_int_in_endpoint(iface_desc
, &dev
->int_in_endpoint
);
784 dev_err(&interface
->dev
, "no interrupt-in endpoint found\n");
789 if ((dev
->product_id
== USB_DEVICE_ID_CODEMERCS_IOW56
) ||
790 (dev
->product_id
== USB_DEVICE_ID_CODEMERCS_IOW56AM
) ||
791 (dev
->product_id
== USB_DEVICE_ID_CODEMERCS_IOW28
) ||
792 (dev
->product_id
== USB_DEVICE_ID_CODEMERCS_IOW28L
) ||
793 (dev
->product_id
== USB_DEVICE_ID_CODEMERCS_IOW100
)) {
794 res
= usb_find_last_int_out_endpoint(iface_desc
,
795 &dev
->int_out_endpoint
);
797 dev_err(&interface
->dev
, "no interrupt-out endpoint found\n");
803 /* we have to check the report_size often, so remember it in the endianness suitable for our machine */
804 dev
->report_size
= usb_endpoint_maxp(dev
->int_in_endpoint
);
805 if ((dev
->interface
->cur_altsetting
->desc
.bInterfaceNumber
== 0) &&
806 ((dev
->product_id
== USB_DEVICE_ID_CODEMERCS_IOW56
) ||
807 (dev
->product_id
== USB_DEVICE_ID_CODEMERCS_IOW56AM
) ||
808 (dev
->product_id
== USB_DEVICE_ID_CODEMERCS_IOW28
) ||
809 (dev
->product_id
== USB_DEVICE_ID_CODEMERCS_IOW28L
) ||
810 (dev
->product_id
== USB_DEVICE_ID_CODEMERCS_IOW100
)))
811 /* IOWarrior56 has wMaxPacketSize different from report size */
812 dev
->report_size
= 7;
814 /* create the urb and buffer for reading */
815 dev
->int_in_urb
= usb_alloc_urb(0, GFP_KERNEL
);
816 if (!dev
->int_in_urb
)
818 dev
->int_in_buffer
= kmalloc(dev
->report_size
, GFP_KERNEL
);
819 if (!dev
->int_in_buffer
)
821 usb_fill_int_urb(dev
->int_in_urb
, dev
->udev
,
822 usb_rcvintpipe(dev
->udev
,
823 dev
->int_in_endpoint
->bEndpointAddress
),
824 dev
->int_in_buffer
, dev
->report_size
,
825 iowarrior_callback
, dev
,
826 dev
->int_in_endpoint
->bInterval
);
827 /* create an internal buffer for interrupt data from the device */
829 kmalloc_array(dev
->report_size
+ 1, MAX_INTERRUPT_BUFFER
,
831 if (!dev
->read_queue
)
833 /* Get the serial-number of the chip */
834 memset(dev
->chip_serial
, 0x00, sizeof(dev
->chip_serial
));
835 usb_string(udev
, udev
->descriptor
.iSerialNumber
, dev
->chip_serial
,
836 sizeof(dev
->chip_serial
));
837 if (strlen(dev
->chip_serial
) != 8)
838 memset(dev
->chip_serial
, 0x00, sizeof(dev
->chip_serial
));
840 /* Set the idle timeout to 0, if this is interface 0 */
841 if (dev
->interface
->cur_altsetting
->desc
.bInterfaceNumber
== 0) {
842 usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0),
844 USB_TYPE_CLASS
| USB_RECIP_INTERFACE
, 0,
845 0, NULL
, 0, USB_CTRL_SET_TIMEOUT
);
847 /* allow device read and ioctl */
850 /* we can register the device now, as it is ready */
851 usb_set_intfdata(interface
, dev
);
853 retval
= usb_register_dev(interface
, &iowarrior_class
);
855 /* something prevented us from registering this driver */
856 dev_err(&interface
->dev
, "Not able to get a minor for this device.\n");
860 dev
->minor
= interface
->minor
;
862 /* let the user know what node this device is now attached to */
863 dev_info(&interface
->dev
, "IOWarrior product=0x%x, serial=%s interface=%d "
864 "now attached to iowarrior%d\n", dev
->product_id
, dev
->chip_serial
,
865 iface_desc
->desc
.bInterfaceNumber
, dev
->minor
- IOWARRIOR_MINOR_BASE
);
869 iowarrior_delete(dev
);
874 * iowarrior_disconnect
876 * Called by the usb core when the device is removed from the system.
878 static void iowarrior_disconnect(struct usb_interface
*interface
)
880 struct iowarrior
*dev
= usb_get_intfdata(interface
);
881 int minor
= dev
->minor
;
883 usb_deregister_dev(interface
, &iowarrior_class
);
885 mutex_lock(&dev
->mutex
);
887 /* prevent device read, write and ioctl */
891 /* There is a process that holds a filedescriptor to the device ,
892 so we only shutdown read-/write-ops going on.
893 Deleting the device is postponed until close() was called.
895 usb_kill_urb(dev
->int_in_urb
);
896 usb_kill_anchored_urbs(&dev
->submitted
);
897 wake_up_interruptible(&dev
->read_wait
);
898 wake_up_interruptible(&dev
->write_wait
);
899 mutex_unlock(&dev
->mutex
);
901 /* no process is using the device, cleanup now */
902 mutex_unlock(&dev
->mutex
);
903 iowarrior_delete(dev
);
906 dev_info(&interface
->dev
, "I/O-Warror #%d now disconnected\n",
907 minor
- IOWARRIOR_MINOR_BASE
);
910 /* usb specific object needed to register this driver with the usb subsystem */
911 static struct usb_driver iowarrior_driver
= {
913 .probe
= iowarrior_probe
,
914 .disconnect
= iowarrior_disconnect
,
915 .id_table
= iowarrior_ids
,
918 module_usb_driver(iowarrior_driver
);