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 /* 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 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 */
98 struct usb_anchor submitted
;
106 * USB spec identifies 5 second timeouts.
108 #define GET_TIMEOUT 5
109 #define USB_REQ_GET_REPORT 0x01
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),
117 USB_DIR_IN
| USB_TYPE_CLASS
|
118 USB_RECIP_INTERFACE
, (type
<< 8) + id
,
119 inter
->desc
.bInterfaceNumber
, buf
, size
,
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),
132 USB_TYPE_CLASS
| USB_RECIP_INTERFACE
,
134 intf
->cur_altsetting
->desc
.bInterfaceNumber
, buf
,
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
;
167 int status
= urb
->status
;
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);
193 (dev
->read_queue
+ offset
, urb
->transfer_buffer
,
195 /* equal values on interface 0 will be ignored */
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
,
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
);
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 __poll_t
iowarrior_poll(struct file
*file
, poll_table
* wait
)
701 struct iowarrior
*dev
= file
->private_data
;
705 return EPOLLERR
| EPOLLHUP
;
707 poll_wait(file
, &dev
->read_wait
, wait
);
708 poll_wait(file
, &dev
->write_wait
, wait
);
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
;
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 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
);
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
);
799 dev_err(&interface
->dev
, "no interrupt-in endpoint found\n");
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
);
812 dev_err(&interface
->dev
, "no interrupt-out endpoint found\n");
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
)
833 dev
->int_in_buffer
= kmalloc(dev
->report_size
, GFP_KERNEL
);
834 if (!dev
->int_in_buffer
)
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 */
844 kmalloc(((dev
->report_size
+ 1) * MAX_INTERRUPT_BUFFER
),
846 if (!dev
->read_queue
)
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),
859 USB_TYPE_CLASS
| USB_RECIP_INTERFACE
, 0,
860 0, NULL
, 0, USB_CTRL_SET_TIMEOUT
);
862 /* allow device read and ioctl */
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
);
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
);
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
);
885 iowarrior_delete(dev
);
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
;
899 dev
= usb_get_intfdata(interface
);
900 mutex_lock(&iowarrior_open_disc_lock
);
901 usb_set_intfdata(interface
, NULL
);
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 */
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
);
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
= {
937 .probe
= iowarrior_probe
,
938 .disconnect
= iowarrior_disconnect
,
939 .id_table
= iowarrior_ids
,
942 module_usb_driver(iowarrior_driver
);