16 #include <linux/module.h>
19 #include <linux/slab.h>
20 #include <linux/sched.h>
22 #include <linux/poll.h>
26 #define DRIVER_VERSION "v0.4.0"
28 #define DRIVER_DESC "USB IO-Warrior driver (Linux 2.6.x)"
30 #define USB_VENDOR_ID_CODEMERCS 1984
32 #define USB_DEVICE_ID_CODEMERCS_IOW40 0x1500
33 #define USB_DEVICE_ID_CODEMERCS_IOW24 0x1501
34 #define USB_DEVICE_ID_CODEMERCS_IOWPV1 0x1511
35 #define USB_DEVICE_ID_CODEMERCS_IOWPV2 0x1512
37 #define USB_DEVICE_ID_CODEMERCS_IOW56 0x1503
40 #ifdef CONFIG_USB_DYNAMIC_MINORS
41 #define IOWARRIOR_MINOR_BASE 0
43 #define IOWARRIOR_MINOR_BASE 208 // SKELETON_MINOR_BASE 192 + 16, not official yet
47 #define MAX_INTERRUPT_BUFFER 16
53 #define MAX_WRITES_IN_FLIGHT 4
57 #define dbg( format, arg... ) do { if( debug ) printk( KERN_DEBUG __FILE__ ": " format "\n" , ## arg ); } while ( 0 )
65 static bool debug = 0;
69 static struct usb_driver iowarrior_driver;
109 #define GET_TIMEOUT 5
110 #define USB_REQ_GET_REPORT 0x01
112 static int usb_get_report(
struct usb_device *
dev,
113 struct usb_host_interface *
inter,
unsigned char type,
114 unsigned char id,
void *
buf,
int size)
120 inter->desc.bInterfaceNumber, buf, size,
125 #define USB_REQ_SET_REPORT 0x09
128 unsigned char id,
void *
buf,
int size)
131 usb_sndctrlpipe(interface_to_usbdev(intf), 0),
135 intf->cur_altsetting->desc.bInterfaceNumber, buf,
156 static void iowarrior_callback(
struct urb *
urb)
185 if ((intr_idx != read_idx)
186 && (dev->
interface->cur_altsetting->desc.bInterfaceNumber == 0)) {
190 (dev->
read_queue + offset, urb->transfer_buffer,
200 if (read_idx == aux_idx) {
221 dev_err(&dev->
interface->dev,
"%s - usb_submit_urb failed with result %d\n",
229 static void iowarrior_write_callback(
struct urb *urb)
232 int status = urb->status;
239 dbg(
"%s - nonzero write bulk status received: %d",
244 urb->transfer_buffer, urb->transfer_dma);
253 static inline void iowarrior_delete(
struct iowarrior *dev)
255 dbg(
"%s - minor %d", __func__, dev->
minor);
266 static int read_index(
struct iowarrior *dev)
273 return (read_idx == intr_idx ? -1 : read_idx);
280 size_t count, loff_t *ppos)
292 dbg(
"%s - minor %d, count = %zd", __func__, dev->
minor, count);
302 if ((read_idx = read_index(dev)) == -1) {
322 if (read_idx == -1) {
343 static ssize_t iowarrior_write(
struct file *file,
344 const char __user *user_buffer,
345 size_t count, loff_t *ppos)
350 struct urb *int_out_urb =
NULL;
360 dbg(
"%s - minor %d, count = %zd", __func__, dev->
minor, count);
387 retval = usb_set_report(dev->
interface, 2, 0, buf, count);
422 dbg(
"%s Unable to allocate urb ", __func__);
429 dbg(
"%s Unable to allocate buffer ", __func__);
430 goto error_no_buffer;
432 usb_fill_int_urb(int_out_urb, dev->
udev,
433 usb_sndintpipe(dev->
udev,
436 iowarrior_write_callback, dev,
438 int_out_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
445 dbg(
"%s submit error %d for urb nr.%d", __func__,
464 int_out_urb->transfer_dma);
478 static long iowarrior_ioctl(
struct file *file,
unsigned int cmd,
506 dbg(
"%s - minor %d, cmd 0x%.4x, arg %ld", __func__, dev->
minor, cmd,
523 io_res = usb_set_report(dev->
interface, 2, 0,
532 "ioctl 'IOW_WRITE' is not supported for product=0x%x.\n",
538 io_res = usb_get_report(dev->
udev,
564 info.if_num = dev->
interface->cur_altsetting->desc.bInterfaceNumber;
570 if (cfg_descriptor ==
NULL) {
598 static int iowarrior_open(
struct inode *
inode,
struct file *file)
608 subminor = iminor(inode);
619 dev = usb_get_intfdata(interface);
637 dev_err(&interface->dev,
"Error %d while submitting URB\n", retval);
656 static int iowarrior_release(
struct inode *inode,
struct file *file)
666 dbg(
"%s - minor %d", __func__, dev->
minor);
689 iowarrior_delete(dev);
695 static unsigned iowarrior_poll(
struct file *file,
poll_table *
wait)
698 unsigned int mask = 0;
709 if (read_index(dev) != -1)
728 .write = iowarrior_write,
729 .read = iowarrior_read,
730 .unlocked_ioctl = iowarrior_ioctl,
731 .open = iowarrior_open,
732 .release = iowarrior_release,
733 .poll = iowarrior_poll,
746 static struct usb_class_driver iowarrior_class = {
747 .name =
"iowarrior%d",
748 .devnode = iowarrior_devnode,
749 .fops = &iowarrior_fops,
765 struct usb_device *
udev = interface_to_usbdev(interface);
767 struct usb_host_interface *iface_desc;
775 dev_err(&interface->dev,
"Out of memory\n");
792 iface_desc = interface->cur_altsetting;
796 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++
i) {
797 endpoint = &iface_desc->endpoint[
i].desc;
799 if (usb_endpoint_is_int_in(endpoint))
801 if (usb_endpoint_is_int_out(endpoint))
807 if ((dev->
interface->cur_altsetting->desc.bInterfaceNumber == 0) &&
815 dev_err(&interface->dev,
"Couldn't allocate interrupt_in_urb\n");
820 dev_err(&interface->dev,
"Couldn't allocate int_in_buffer\n");
824 usb_rcvintpipe(dev->
udev,
827 iowarrior_callback, dev,
834 dev_err(&interface->dev,
"Couldn't allocate read_queue\n");
845 if (dev->
interface->cur_altsetting->desc.bInterfaceNumber == 0) {
849 0,
NULL, 0, USB_CTRL_SET_TIMEOUT);
855 usb_set_intfdata(interface, dev);
860 dev_err(&interface->dev,
"Not able to get a minor for this device.\n");
861 usb_set_intfdata(interface,
NULL);
865 dev->
minor = interface->minor;
868 dev_info(&interface->dev,
"IOWarrior product=0x%x, serial=%s interface=%d "
874 iowarrior_delete(dev);
883 static void iowarrior_disconnect(
struct usb_interface *interface)
888 dev = usb_get_intfdata(interface);
890 usb_set_intfdata(interface,
NULL);
915 iowarrior_delete(dev);
918 dev_info(&interface->dev,
"I/O-Warror #%d now disconnected\n",
923 static struct usb_driver iowarrior_driver = {
925 .probe = iowarrior_probe,
926 .disconnect = iowarrior_disconnect,
927 .id_table = iowarrior_ids,