Greg Kroah-Hartman | 946b960 | 2007-02-14 13:40:14 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Native support for the I/O-Warrior USB devices |
| 3 | * |
| 4 | * Copyright (c) 2003-2005 Code Mercenaries GmbH |
| 5 | * written by Christian Lucht <lucht@codemercs.com> |
| 6 | * |
| 7 | * based on |
| 8 | |
| 9 | * usb-skeleton.c by Greg Kroah-Hartman <greg@kroah.com> |
| 10 | * brlvger.c by Stephane Dalton <sdalton@videotron.ca> |
| 11 | * and St�hane Doyon <s.doyon@videotron.ca> |
| 12 | * |
| 13 | * Released under the GPLv2. |
| 14 | */ |
| 15 | |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/usb.h> |
| 18 | #include <linux/init.h> |
| 19 | #include <linux/slab.h> |
| 20 | #include <linux/sched.h> |
| 21 | #include <linux/poll.h> |
| 22 | #include <linux/version.h> |
| 23 | #include <linux/usb/iowarrior.h> |
| 24 | |
| 25 | /* Version Information */ |
| 26 | #define DRIVER_VERSION "v0.4.0" |
| 27 | #define DRIVER_AUTHOR "Christian Lucht <lucht@codemercs.com>" |
| 28 | #define DRIVER_DESC "USB IO-Warrior driver (Linux 2.6.x)" |
| 29 | |
| 30 | #define USB_VENDOR_ID_CODEMERCS 1984 |
| 31 | /* low speed iowarrior */ |
| 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 |
| 36 | /* full speed iowarrior */ |
| 37 | #define USB_DEVICE_ID_CODEMERCS_IOW56 0x1503 |
| 38 | |
| 39 | /* Get a minor range for your devices from the usb maintainer */ |
| 40 | #ifdef CONFIG_USB_DYNAMIC_MINORS |
| 41 | #define IOWARRIOR_MINOR_BASE 0 |
| 42 | #else |
| 43 | #define IOWARRIOR_MINOR_BASE 208 // SKELETON_MINOR_BASE 192 + 16, not offical yet |
| 44 | #endif |
| 45 | |
| 46 | /* interrupt input queue size */ |
| 47 | #define MAX_INTERRUPT_BUFFER 16 |
| 48 | /* |
| 49 | maximum number of urbs that are submitted for writes at the same time, |
| 50 | this applies to the IOWarrior56 only! |
| 51 | IOWarrior24 and IOWarrior40 use synchronous usb_control_msg calls. |
| 52 | */ |
| 53 | #define MAX_WRITES_IN_FLIGHT 4 |
| 54 | |
| 55 | /* Use our own dbg macro */ |
| 56 | #undef dbg |
| 57 | #define dbg( format, arg... ) do { if( debug ) printk( KERN_DEBUG __FILE__ ": " format "\n" , ## arg ); } while ( 0 ) |
| 58 | |
| 59 | MODULE_AUTHOR(DRIVER_AUTHOR); |
| 60 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 61 | MODULE_LICENSE("GPL"); |
| 62 | |
| 63 | /* Module parameters */ |
| 64 | static int debug = 0; |
| 65 | module_param(debug, bool, 0644); |
| 66 | MODULE_PARM_DESC(debug, "debug=1 enables debugging messages"); |
| 67 | |
| 68 | static struct usb_driver iowarrior_driver; |
| 69 | |
| 70 | /*--------------*/ |
| 71 | /* data */ |
| 72 | /*--------------*/ |
| 73 | |
| 74 | /* Structure to hold all of our device specific stuff */ |
| 75 | struct iowarrior { |
| 76 | struct mutex mutex; /* locks this structure */ |
| 77 | struct usb_device *udev; /* save off the usb device pointer */ |
| 78 | struct usb_interface *interface; /* the interface for this device */ |
| 79 | unsigned char minor; /* the starting minor number for this device */ |
| 80 | struct usb_endpoint_descriptor *int_out_endpoint; /* endpoint for reading (needed for IOW56 only) */ |
| 81 | struct usb_endpoint_descriptor *int_in_endpoint; /* endpoint for reading */ |
| 82 | struct urb *int_in_urb; /* the urb for reading data */ |
| 83 | unsigned char *int_in_buffer; /* buffer for data to be read */ |
| 84 | unsigned char serial_number; /* to detect lost packages */ |
| 85 | unsigned char *read_queue; /* size is MAX_INTERRUPT_BUFFER * packet size */ |
| 86 | wait_queue_head_t read_wait; |
| 87 | wait_queue_head_t write_wait; /* wait-queue for writing to the device */ |
| 88 | atomic_t write_busy; /* number of write-urbs submitted */ |
| 89 | atomic_t read_idx; |
| 90 | atomic_t intr_idx; |
| 91 | spinlock_t intr_idx_lock; /* protects 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 | }; |
| 99 | |
| 100 | /*--------------*/ |
| 101 | /* globals */ |
| 102 | /*--------------*/ |
Greg Kroah-Hartman | 946b960 | 2007-02-14 13:40:14 -0800 | [diff] [blame] | 103 | |
| 104 | /* |
| 105 | * USB spec identifies 5 second timeouts. |
| 106 | */ |
| 107 | #define GET_TIMEOUT 5 |
| 108 | #define USB_REQ_GET_REPORT 0x01 |
| 109 | //#if 0 |
| 110 | static int usb_get_report(struct usb_device *dev, |
| 111 | struct usb_host_interface *inter, unsigned char type, |
| 112 | unsigned char id, void *buf, int size) |
| 113 | { |
| 114 | return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), |
| 115 | USB_REQ_GET_REPORT, |
| 116 | USB_DIR_IN | USB_TYPE_CLASS | |
| 117 | USB_RECIP_INTERFACE, (type << 8) + id, |
| 118 | inter->desc.bInterfaceNumber, buf, size, |
Eberhard Fahle | 147c5a1 | 2007-04-05 11:13:21 +0200 | [diff] [blame] | 119 | GET_TIMEOUT*HZ); |
Greg Kroah-Hartman | 946b960 | 2007-02-14 13:40:14 -0800 | [diff] [blame] | 120 | } |
| 121 | //#endif |
| 122 | |
| 123 | #define USB_REQ_SET_REPORT 0x09 |
| 124 | |
| 125 | static int usb_set_report(struct usb_interface *intf, unsigned char type, |
| 126 | unsigned char id, void *buf, int size) |
| 127 | { |
| 128 | return usb_control_msg(interface_to_usbdev(intf), |
| 129 | usb_sndctrlpipe(interface_to_usbdev(intf), 0), |
| 130 | USB_REQ_SET_REPORT, |
| 131 | USB_TYPE_CLASS | USB_RECIP_INTERFACE, |
| 132 | (type << 8) + id, |
| 133 | intf->cur_altsetting->desc.bInterfaceNumber, buf, |
Eberhard Fahle | 147c5a1 | 2007-04-05 11:13:21 +0200 | [diff] [blame] | 134 | size, HZ); |
Greg Kroah-Hartman | 946b960 | 2007-02-14 13:40:14 -0800 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | /*---------------------*/ |
| 138 | /* driver registration */ |
| 139 | /*---------------------*/ |
| 140 | /* table of devices that work with this driver */ |
| 141 | static struct usb_device_id iowarrior_ids[] = { |
| 142 | {USB_DEVICE(USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOW40)}, |
| 143 | {USB_DEVICE(USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOW24)}, |
| 144 | {USB_DEVICE(USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOWPV1)}, |
| 145 | {USB_DEVICE(USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOWPV2)}, |
| 146 | {USB_DEVICE(USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOW56)}, |
| 147 | {} /* Terminating entry */ |
| 148 | }; |
| 149 | MODULE_DEVICE_TABLE(usb, iowarrior_ids); |
| 150 | |
| 151 | /* |
| 152 | * USB callback handler for reading data |
| 153 | */ |
| 154 | static void iowarrior_callback(struct urb *urb) |
| 155 | { |
| 156 | struct iowarrior *dev = (struct iowarrior *)urb->context; |
| 157 | int intr_idx; |
| 158 | int read_idx; |
| 159 | int aux_idx; |
| 160 | int offset; |
| 161 | int status; |
| 162 | |
| 163 | switch (urb->status) { |
| 164 | case 0: |
| 165 | /* success */ |
| 166 | break; |
| 167 | case -ECONNRESET: |
| 168 | case -ENOENT: |
| 169 | case -ESHUTDOWN: |
| 170 | return; |
| 171 | default: |
| 172 | goto exit; |
| 173 | } |
| 174 | |
| 175 | spin_lock(&dev->intr_idx_lock); |
| 176 | intr_idx = atomic_read(&dev->intr_idx); |
| 177 | /* aux_idx become previous intr_idx */ |
| 178 | aux_idx = (intr_idx == 0) ? (MAX_INTERRUPT_BUFFER - 1) : (intr_idx - 1); |
| 179 | read_idx = atomic_read(&dev->read_idx); |
| 180 | |
| 181 | /* queue is not empty and it's interface 0 */ |
| 182 | if ((intr_idx != read_idx) |
| 183 | && (dev->interface->cur_altsetting->desc.bInterfaceNumber == 0)) { |
| 184 | /* + 1 for serial number */ |
| 185 | offset = aux_idx * (dev->report_size + 1); |
| 186 | if (!memcmp |
| 187 | (dev->read_queue + offset, urb->transfer_buffer, |
| 188 | dev->report_size)) { |
| 189 | /* equal values on interface 0 will be ignored */ |
| 190 | spin_unlock(&dev->intr_idx_lock); |
| 191 | goto exit; |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | /* aux_idx become next intr_idx */ |
| 196 | aux_idx = (intr_idx == (MAX_INTERRUPT_BUFFER - 1)) ? 0 : (intr_idx + 1); |
| 197 | if (read_idx == aux_idx) { |
| 198 | /* queue full, dropping oldest input */ |
| 199 | read_idx = (++read_idx == MAX_INTERRUPT_BUFFER) ? 0 : read_idx; |
| 200 | atomic_set(&dev->read_idx, read_idx); |
| 201 | atomic_set(&dev->overflow_flag, 1); |
| 202 | } |
| 203 | |
| 204 | /* +1 for serial number */ |
| 205 | offset = intr_idx * (dev->report_size + 1); |
| 206 | memcpy(dev->read_queue + offset, urb->transfer_buffer, |
| 207 | dev->report_size); |
| 208 | *(dev->read_queue + offset + (dev->report_size)) = dev->serial_number++; |
| 209 | |
| 210 | atomic_set(&dev->intr_idx, aux_idx); |
| 211 | spin_unlock(&dev->intr_idx_lock); |
| 212 | /* tell the blocking read about the new data */ |
| 213 | wake_up_interruptible(&dev->read_wait); |
| 214 | |
| 215 | exit: |
| 216 | status = usb_submit_urb(urb, GFP_ATOMIC); |
| 217 | if (status) |
| 218 | dev_err(&dev->interface->dev, "%s - usb_submit_urb failed with result %d", |
| 219 | __FUNCTION__, status); |
| 220 | |
| 221 | } |
| 222 | |
| 223 | /* |
| 224 | * USB Callback handler for write-ops |
| 225 | */ |
| 226 | static void iowarrior_write_callback(struct urb *urb) |
| 227 | { |
| 228 | struct iowarrior *dev; |
| 229 | dev = (struct iowarrior *)urb->context; |
| 230 | /* sync/async unlink faults aren't errors */ |
| 231 | if (urb->status && |
| 232 | !(urb->status == -ENOENT || |
| 233 | urb->status == -ECONNRESET || urb->status == -ESHUTDOWN)) { |
| 234 | dbg("%s - nonzero write bulk status received: %d", |
| 235 | __func__, urb->status); |
| 236 | } |
| 237 | /* free up our allocated buffer */ |
| 238 | usb_buffer_free(urb->dev, urb->transfer_buffer_length, |
| 239 | urb->transfer_buffer, urb->transfer_dma); |
| 240 | /* tell a waiting writer the interrupt-out-pipe is available again */ |
| 241 | atomic_dec(&dev->write_busy); |
| 242 | wake_up_interruptible(&dev->write_wait); |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * iowarrior_delete |
| 247 | */ |
| 248 | static inline void iowarrior_delete(struct iowarrior *dev) |
| 249 | { |
| 250 | dbg("%s - minor %d", __func__, dev->minor); |
| 251 | kfree(dev->int_in_buffer); |
| 252 | usb_free_urb(dev->int_in_urb); |
| 253 | kfree(dev->read_queue); |
| 254 | kfree(dev); |
| 255 | } |
| 256 | |
| 257 | /*---------------------*/ |
| 258 | /* fops implementation */ |
| 259 | /*---------------------*/ |
| 260 | |
| 261 | static int read_index(struct iowarrior *dev) |
| 262 | { |
| 263 | int intr_idx, read_idx; |
| 264 | |
| 265 | read_idx = atomic_read(&dev->read_idx); |
| 266 | intr_idx = atomic_read(&dev->intr_idx); |
| 267 | |
| 268 | return (read_idx == intr_idx ? -1 : read_idx); |
| 269 | } |
| 270 | |
| 271 | /** |
| 272 | * iowarrior_read |
| 273 | */ |
| 274 | static ssize_t iowarrior_read(struct file *file, char __user *buffer, |
| 275 | size_t count, loff_t *ppos) |
| 276 | { |
| 277 | struct iowarrior *dev; |
| 278 | int read_idx; |
| 279 | int offset; |
| 280 | |
| 281 | dev = (struct iowarrior *)file->private_data; |
| 282 | |
| 283 | /* verify that the device wasn't unplugged */ |
| 284 | if (dev == NULL || !dev->present) |
| 285 | return -ENODEV; |
| 286 | |
| 287 | dbg("%s - minor %d, count = %zd", __func__, dev->minor, count); |
| 288 | |
| 289 | /* read count must be packet size (+ time stamp) */ |
| 290 | if ((count != dev->report_size) |
| 291 | && (count != (dev->report_size + 1))) |
| 292 | return -EINVAL; |
| 293 | |
| 294 | /* repeat until no buffer overrun in callback handler occur */ |
| 295 | do { |
| 296 | atomic_set(&dev->overflow_flag, 0); |
| 297 | if ((read_idx = read_index(dev)) == -1) { |
| 298 | /* queue emty */ |
| 299 | if (file->f_flags & O_NONBLOCK) |
| 300 | return -EAGAIN; |
| 301 | else { |
| 302 | //next line will return when there is either new data, or the device is unplugged |
| 303 | int r = wait_event_interruptible(dev->read_wait, |
| 304 | (!dev->present |
| 305 | || (read_idx = |
| 306 | read_index |
| 307 | (dev)) != |
| 308 | -1)); |
| 309 | if (r) { |
| 310 | //we were interrupted by a signal |
| 311 | return -ERESTART; |
| 312 | } |
| 313 | if (!dev->present) { |
| 314 | //The device was unplugged |
| 315 | return -ENODEV; |
| 316 | } |
| 317 | if (read_idx == -1) { |
| 318 | // Can this happen ??? |
| 319 | return 0; |
| 320 | } |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | offset = read_idx * (dev->report_size + 1); |
| 325 | if (copy_to_user(buffer, dev->read_queue + offset, count)) { |
| 326 | return -EFAULT; |
| 327 | } |
| 328 | } while (atomic_read(&dev->overflow_flag)); |
| 329 | |
| 330 | read_idx = ++read_idx == MAX_INTERRUPT_BUFFER ? 0 : read_idx; |
| 331 | atomic_set(&dev->read_idx, read_idx); |
| 332 | return count; |
| 333 | } |
| 334 | |
| 335 | /* |
| 336 | * iowarrior_write |
| 337 | */ |
| 338 | static ssize_t iowarrior_write(struct file *file, |
| 339 | const char __user *user_buffer, |
| 340 | size_t count, loff_t *ppos) |
| 341 | { |
| 342 | struct iowarrior *dev; |
| 343 | int retval = 0; |
| 344 | char *buf = NULL; /* for IOW24 and IOW56 we need a buffer */ |
| 345 | struct urb *int_out_urb = NULL; |
| 346 | |
| 347 | dev = (struct iowarrior *)file->private_data; |
| 348 | |
| 349 | mutex_lock(&dev->mutex); |
| 350 | /* verify that the device wasn't unplugged */ |
| 351 | if (dev == NULL || !dev->present) { |
| 352 | retval = -ENODEV; |
| 353 | goto exit; |
| 354 | } |
| 355 | dbg("%s - minor %d, count = %zd", __func__, dev->minor, count); |
| 356 | /* if count is 0 we're already done */ |
| 357 | if (count == 0) { |
| 358 | retval = 0; |
| 359 | goto exit; |
| 360 | } |
| 361 | /* We only accept full reports */ |
| 362 | if (count != dev->report_size) { |
| 363 | retval = -EINVAL; |
| 364 | goto exit; |
| 365 | } |
| 366 | switch (dev->product_id) { |
| 367 | case USB_DEVICE_ID_CODEMERCS_IOW24: |
| 368 | case USB_DEVICE_ID_CODEMERCS_IOWPV1: |
| 369 | case USB_DEVICE_ID_CODEMERCS_IOWPV2: |
| 370 | case USB_DEVICE_ID_CODEMERCS_IOW40: |
| 371 | /* IOW24 and IOW40 use a synchronous call */ |
| 372 | buf = kmalloc(8, GFP_KERNEL); /* 8 bytes are enough for both products */ |
| 373 | if (!buf) { |
| 374 | retval = -ENOMEM; |
| 375 | goto exit; |
| 376 | } |
| 377 | if (copy_from_user(buf, user_buffer, count)) { |
| 378 | retval = -EFAULT; |
| 379 | kfree(buf); |
| 380 | goto exit; |
| 381 | } |
| 382 | retval = usb_set_report(dev->interface, 2, 0, buf, count); |
| 383 | kfree(buf); |
| 384 | goto exit; |
| 385 | break; |
| 386 | case USB_DEVICE_ID_CODEMERCS_IOW56: |
| 387 | /* The IOW56 uses asynchronous IO and more urbs */ |
| 388 | if (atomic_read(&dev->write_busy) == MAX_WRITES_IN_FLIGHT) { |
| 389 | /* Wait until we are below the limit for submitted urbs */ |
| 390 | if (file->f_flags & O_NONBLOCK) { |
| 391 | retval = -EAGAIN; |
| 392 | goto exit; |
| 393 | } else { |
| 394 | retval = wait_event_interruptible(dev->write_wait, |
| 395 | (!dev->present || (atomic_read (&dev-> write_busy) < MAX_WRITES_IN_FLIGHT))); |
| 396 | if (retval) { |
| 397 | /* we were interrupted by a signal */ |
| 398 | retval = -ERESTART; |
| 399 | goto exit; |
| 400 | } |
| 401 | if (!dev->present) { |
| 402 | /* The device was unplugged */ |
| 403 | retval = -ENODEV; |
| 404 | goto exit; |
| 405 | } |
| 406 | if (!dev->opened) { |
| 407 | /* We were closed while waiting for an URB */ |
| 408 | retval = -ENODEV; |
| 409 | goto exit; |
| 410 | } |
| 411 | } |
| 412 | } |
| 413 | atomic_inc(&dev->write_busy); |
| 414 | int_out_urb = usb_alloc_urb(0, GFP_KERNEL); |
| 415 | if (!int_out_urb) { |
| 416 | retval = -ENOMEM; |
| 417 | dbg("%s Unable to allocate urb ", __func__); |
Oliver Neukum | f81ee4d | 2007-03-19 11:39:13 +0100 | [diff] [blame] | 418 | goto error_no_urb; |
Greg Kroah-Hartman | 946b960 | 2007-02-14 13:40:14 -0800 | [diff] [blame] | 419 | } |
| 420 | buf = usb_buffer_alloc(dev->udev, dev->report_size, |
| 421 | GFP_KERNEL, &int_out_urb->transfer_dma); |
| 422 | if (!buf) { |
| 423 | retval = -ENOMEM; |
| 424 | dbg("%s Unable to allocate buffer ", __func__); |
Oliver Neukum | f81ee4d | 2007-03-19 11:39:13 +0100 | [diff] [blame] | 425 | goto error_no_buffer; |
Greg Kroah-Hartman | 946b960 | 2007-02-14 13:40:14 -0800 | [diff] [blame] | 426 | } |
| 427 | usb_fill_int_urb(int_out_urb, dev->udev, |
| 428 | usb_sndintpipe(dev->udev, |
| 429 | dev->int_out_endpoint->bEndpointAddress), |
| 430 | buf, dev->report_size, |
| 431 | iowarrior_write_callback, dev, |
| 432 | dev->int_out_endpoint->bInterval); |
| 433 | int_out_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; |
| 434 | if (copy_from_user(buf, user_buffer, count)) { |
| 435 | retval = -EFAULT; |
| 436 | goto error; |
| 437 | } |
| 438 | retval = usb_submit_urb(int_out_urb, GFP_KERNEL); |
| 439 | if (retval) { |
| 440 | dbg("%s submit error %d for urb nr.%d", __func__, |
| 441 | retval, atomic_read(&dev->write_busy)); |
| 442 | goto error; |
| 443 | } |
| 444 | /* submit was ok */ |
| 445 | retval = count; |
| 446 | usb_free_urb(int_out_urb); |
| 447 | goto exit; |
| 448 | break; |
| 449 | default: |
| 450 | /* what do we have here ? An unsupported Product-ID ? */ |
| 451 | dev_err(&dev->interface->dev, "%s - not supported for product=0x%x", |
| 452 | __FUNCTION__, dev->product_id); |
| 453 | retval = -EFAULT; |
| 454 | goto exit; |
| 455 | break; |
| 456 | } |
| 457 | error: |
| 458 | usb_buffer_free(dev->udev, dev->report_size, buf, |
| 459 | int_out_urb->transfer_dma); |
Oliver Neukum | f81ee4d | 2007-03-19 11:39:13 +0100 | [diff] [blame] | 460 | error_no_buffer: |
Greg Kroah-Hartman | 946b960 | 2007-02-14 13:40:14 -0800 | [diff] [blame] | 461 | usb_free_urb(int_out_urb); |
Oliver Neukum | f81ee4d | 2007-03-19 11:39:13 +0100 | [diff] [blame] | 462 | error_no_urb: |
Greg Kroah-Hartman | 946b960 | 2007-02-14 13:40:14 -0800 | [diff] [blame] | 463 | atomic_dec(&dev->write_busy); |
| 464 | wake_up_interruptible(&dev->write_wait); |
| 465 | exit: |
| 466 | mutex_unlock(&dev->mutex); |
| 467 | return retval; |
| 468 | } |
| 469 | |
| 470 | /** |
| 471 | * iowarrior_ioctl |
| 472 | */ |
| 473 | static int iowarrior_ioctl(struct inode *inode, struct file *file, |
| 474 | unsigned int cmd, unsigned long arg) |
| 475 | { |
| 476 | struct iowarrior *dev = NULL; |
| 477 | __u8 *buffer; |
| 478 | __u8 __user *user_buffer; |
| 479 | int retval; |
| 480 | int io_res; /* checks for bytes read/written and copy_to/from_user results */ |
| 481 | |
| 482 | dev = (struct iowarrior *)file->private_data; |
| 483 | if (dev == NULL) { |
| 484 | return -ENODEV; |
| 485 | } |
| 486 | |
| 487 | buffer = kzalloc(dev->report_size, GFP_KERNEL); |
| 488 | if (!buffer) |
| 489 | return -ENOMEM; |
| 490 | |
| 491 | /* lock this object */ |
| 492 | mutex_lock(&dev->mutex); |
| 493 | |
| 494 | /* verify that the device wasn't unplugged */ |
| 495 | if (!dev->present) { |
Oliver Neukum | fc0f8fc | 2007-06-12 15:36:07 +0200 | [diff] [blame] | 496 | retval = -ENODEV; |
| 497 | goto error_out; |
Greg Kroah-Hartman | 946b960 | 2007-02-14 13:40:14 -0800 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | dbg("%s - minor %d, cmd 0x%.4x, arg %ld", __func__, dev->minor, cmd, |
| 501 | arg); |
| 502 | |
| 503 | retval = 0; |
| 504 | io_res = 0; |
| 505 | switch (cmd) { |
| 506 | case IOW_WRITE: |
| 507 | if (dev->product_id == USB_DEVICE_ID_CODEMERCS_IOW24 || |
| 508 | dev->product_id == USB_DEVICE_ID_CODEMERCS_IOWPV1 || |
| 509 | dev->product_id == USB_DEVICE_ID_CODEMERCS_IOWPV2 || |
| 510 | dev->product_id == USB_DEVICE_ID_CODEMERCS_IOW40) { |
| 511 | user_buffer = (__u8 __user *)arg; |
| 512 | io_res = copy_from_user(buffer, user_buffer, |
| 513 | dev->report_size); |
| 514 | if (io_res) { |
| 515 | retval = -EFAULT; |
| 516 | } else { |
| 517 | io_res = usb_set_report(dev->interface, 2, 0, |
| 518 | buffer, |
| 519 | dev->report_size); |
| 520 | if (io_res < 0) |
| 521 | retval = io_res; |
| 522 | } |
| 523 | } else { |
| 524 | retval = -EINVAL; |
| 525 | dev_err(&dev->interface->dev, |
| 526 | "ioctl 'IOW_WRITE' is not supported for product=0x%x.", |
| 527 | dev->product_id); |
| 528 | } |
| 529 | break; |
| 530 | case IOW_READ: |
| 531 | user_buffer = (__u8 __user *)arg; |
| 532 | io_res = usb_get_report(dev->udev, |
| 533 | dev->interface->cur_altsetting, 1, 0, |
| 534 | buffer, dev->report_size); |
| 535 | if (io_res < 0) |
| 536 | retval = io_res; |
| 537 | else { |
| 538 | io_res = copy_to_user(user_buffer, buffer, dev->report_size); |
| 539 | if (io_res < 0) |
| 540 | retval = -EFAULT; |
| 541 | } |
| 542 | break; |
| 543 | case IOW_GETINFO: |
| 544 | { |
| 545 | /* Report available information for the device */ |
| 546 | struct iowarrior_info info; |
| 547 | /* needed for power consumption */ |
| 548 | struct usb_config_descriptor *cfg_descriptor = &dev->udev->actconfig->desc; |
| 549 | |
| 550 | /* directly from the descriptor */ |
| 551 | info.vendor = le16_to_cpu(dev->udev->descriptor.idVendor); |
| 552 | info.product = dev->product_id; |
| 553 | info.revision = le16_to_cpu(dev->udev->descriptor.bcdDevice); |
| 554 | |
| 555 | /* 0==UNKNOWN, 1==LOW(usb1.1) ,2=FULL(usb1.1), 3=HIGH(usb2.0) */ |
| 556 | info.speed = le16_to_cpu(dev->udev->speed); |
| 557 | info.if_num = dev->interface->cur_altsetting->desc.bInterfaceNumber; |
| 558 | info.report_size = dev->report_size; |
| 559 | |
| 560 | /* serial number string has been read earlier 8 chars or empty string */ |
| 561 | memcpy(info.serial, dev->chip_serial, |
| 562 | sizeof(dev->chip_serial)); |
| 563 | if (cfg_descriptor == NULL) { |
| 564 | info.power = -1; /* no information available */ |
| 565 | } else { |
| 566 | /* the MaxPower is stored in units of 2mA to make it fit into a byte-value */ |
| 567 | info.power = cfg_descriptor->bMaxPower * 2; |
| 568 | } |
| 569 | io_res = copy_to_user((struct iowarrior_info __user *)arg, &info, |
| 570 | sizeof(struct iowarrior_info)); |
| 571 | if (io_res < 0) |
| 572 | retval = -EFAULT; |
| 573 | break; |
| 574 | } |
| 575 | default: |
| 576 | /* return that we did not understand this ioctl call */ |
| 577 | retval = -ENOTTY; |
| 578 | break; |
| 579 | } |
Oliver Neukum | fc0f8fc | 2007-06-12 15:36:07 +0200 | [diff] [blame] | 580 | error_out: |
Greg Kroah-Hartman | 946b960 | 2007-02-14 13:40:14 -0800 | [diff] [blame] | 581 | /* unlock the device */ |
| 582 | mutex_unlock(&dev->mutex); |
Oliver Neukum | fc0f8fc | 2007-06-12 15:36:07 +0200 | [diff] [blame] | 583 | kfree(buffer); |
Greg Kroah-Hartman | 946b960 | 2007-02-14 13:40:14 -0800 | [diff] [blame] | 584 | return retval; |
| 585 | } |
| 586 | |
| 587 | /** |
| 588 | * iowarrior_open |
| 589 | */ |
| 590 | static int iowarrior_open(struct inode *inode, struct file *file) |
| 591 | { |
| 592 | struct iowarrior *dev = NULL; |
| 593 | struct usb_interface *interface; |
| 594 | int subminor; |
| 595 | int retval = 0; |
| 596 | |
| 597 | dbg("%s", __func__); |
| 598 | |
| 599 | subminor = iminor(inode); |
| 600 | |
Greg Kroah-Hartman | 946b960 | 2007-02-14 13:40:14 -0800 | [diff] [blame] | 601 | interface = usb_find_interface(&iowarrior_driver, subminor); |
| 602 | if (!interface) { |
| 603 | err("%s - error, can't find device for minor %d", __FUNCTION__, |
| 604 | subminor); |
Alan Stern | d4ead16 | 2007-05-22 11:46:41 -0400 | [diff] [blame^] | 605 | return -ENODEV; |
Greg Kroah-Hartman | 946b960 | 2007-02-14 13:40:14 -0800 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | dev = usb_get_intfdata(interface); |
Alan Stern | d4ead16 | 2007-05-22 11:46:41 -0400 | [diff] [blame^] | 609 | if (!dev) |
| 610 | return -ENODEV; |
| 611 | |
| 612 | mutex_lock(&dev->mutex); |
Greg Kroah-Hartman | 946b960 | 2007-02-14 13:40:14 -0800 | [diff] [blame] | 613 | |
| 614 | /* Only one process can open each device, no sharing. */ |
| 615 | if (dev->opened) { |
| 616 | retval = -EBUSY; |
| 617 | goto out; |
| 618 | } |
| 619 | |
| 620 | /* setup interrupt handler for receiving values */ |
| 621 | if ((retval = usb_submit_urb(dev->int_in_urb, GFP_KERNEL)) < 0) { |
| 622 | dev_err(&interface->dev, "Error %d while submitting URB\n", retval); |
| 623 | retval = -EFAULT; |
| 624 | goto out; |
| 625 | } |
| 626 | /* increment our usage count for the driver */ |
| 627 | ++dev->opened; |
| 628 | /* save our object in the file's private structure */ |
| 629 | file->private_data = dev; |
| 630 | retval = 0; |
| 631 | |
| 632 | out: |
Alan Stern | d4ead16 | 2007-05-22 11:46:41 -0400 | [diff] [blame^] | 633 | mutex_unlock(&dev->mutex); |
Greg Kroah-Hartman | 946b960 | 2007-02-14 13:40:14 -0800 | [diff] [blame] | 634 | return retval; |
| 635 | } |
| 636 | |
| 637 | /** |
| 638 | * iowarrior_release |
| 639 | */ |
| 640 | static int iowarrior_release(struct inode *inode, struct file *file) |
| 641 | { |
| 642 | struct iowarrior *dev; |
| 643 | int retval = 0; |
| 644 | |
| 645 | dev = (struct iowarrior *)file->private_data; |
| 646 | if (dev == NULL) { |
| 647 | return -ENODEV; |
| 648 | } |
| 649 | |
| 650 | dbg("%s - minor %d", __func__, dev->minor); |
| 651 | |
| 652 | /* lock our device */ |
| 653 | mutex_lock(&dev->mutex); |
| 654 | |
| 655 | if (dev->opened <= 0) { |
| 656 | retval = -ENODEV; /* close called more than once */ |
| 657 | mutex_unlock(&dev->mutex); |
| 658 | } else { |
| 659 | dev->opened = 0; /* we're closeing now */ |
| 660 | retval = 0; |
| 661 | if (dev->present) { |
| 662 | /* |
| 663 | The device is still connected so we only shutdown |
| 664 | pending read-/write-ops. |
| 665 | */ |
| 666 | usb_kill_urb(dev->int_in_urb); |
| 667 | wake_up_interruptible(&dev->read_wait); |
| 668 | wake_up_interruptible(&dev->write_wait); |
| 669 | mutex_unlock(&dev->mutex); |
| 670 | } else { |
| 671 | /* The device was unplugged, cleanup resources */ |
| 672 | mutex_unlock(&dev->mutex); |
| 673 | iowarrior_delete(dev); |
| 674 | } |
| 675 | } |
| 676 | return retval; |
| 677 | } |
| 678 | |
| 679 | static unsigned iowarrior_poll(struct file *file, poll_table * wait) |
| 680 | { |
| 681 | struct iowarrior *dev = file->private_data; |
| 682 | unsigned int mask = 0; |
| 683 | |
| 684 | if (!dev->present) |
| 685 | return POLLERR | POLLHUP; |
| 686 | |
| 687 | poll_wait(file, &dev->read_wait, wait); |
| 688 | poll_wait(file, &dev->write_wait, wait); |
| 689 | |
| 690 | if (!dev->present) |
| 691 | return POLLERR | POLLHUP; |
| 692 | |
| 693 | if (read_index(dev) != -1) |
| 694 | mask |= POLLIN | POLLRDNORM; |
| 695 | |
| 696 | if (atomic_read(&dev->write_busy) < MAX_WRITES_IN_FLIGHT) |
| 697 | mask |= POLLOUT | POLLWRNORM; |
| 698 | return mask; |
| 699 | } |
| 700 | |
| 701 | /* |
| 702 | * File operations needed when we register this driver. |
| 703 | * This assumes that this driver NEEDS file operations, |
| 704 | * of course, which means that the driver is expected |
| 705 | * to have a node in the /dev directory. If the USB |
| 706 | * device were for a network interface then the driver |
| 707 | * would use "struct net_driver" instead, and a serial |
| 708 | * device would use "struct tty_driver". |
| 709 | */ |
| 710 | static struct file_operations iowarrior_fops = { |
| 711 | .owner = THIS_MODULE, |
| 712 | .write = iowarrior_write, |
| 713 | .read = iowarrior_read, |
| 714 | .ioctl = iowarrior_ioctl, |
| 715 | .open = iowarrior_open, |
| 716 | .release = iowarrior_release, |
| 717 | .poll = iowarrior_poll, |
| 718 | }; |
| 719 | |
| 720 | /* |
| 721 | * usb class driver info in order to get a minor number from the usb core, |
| 722 | * and to have the device registered with devfs and the driver core |
| 723 | */ |
| 724 | static struct usb_class_driver iowarrior_class = { |
| 725 | .name = "iowarrior%d", |
| 726 | .fops = &iowarrior_fops, |
| 727 | .minor_base = IOWARRIOR_MINOR_BASE, |
| 728 | }; |
| 729 | |
| 730 | /*---------------------------------*/ |
| 731 | /* probe and disconnect functions */ |
| 732 | /*---------------------------------*/ |
| 733 | /** |
| 734 | * iowarrior_probe |
| 735 | * |
| 736 | * Called by the usb core when a new device is connected that it thinks |
| 737 | * this driver might be interested in. |
| 738 | */ |
| 739 | static int iowarrior_probe(struct usb_interface *interface, |
| 740 | const struct usb_device_id *id) |
| 741 | { |
| 742 | struct usb_device *udev = interface_to_usbdev(interface); |
| 743 | struct iowarrior *dev = NULL; |
| 744 | struct usb_host_interface *iface_desc; |
| 745 | struct usb_endpoint_descriptor *endpoint; |
| 746 | int i; |
| 747 | int retval = -ENOMEM; |
Greg Kroah-Hartman | 946b960 | 2007-02-14 13:40:14 -0800 | [diff] [blame] | 748 | |
| 749 | /* allocate memory for our device state and intialize it */ |
| 750 | dev = kzalloc(sizeof(struct iowarrior), GFP_KERNEL); |
| 751 | if (dev == NULL) { |
| 752 | dev_err(&interface->dev, "Out of memory"); |
| 753 | return retval; |
| 754 | } |
| 755 | |
| 756 | mutex_init(&dev->mutex); |
| 757 | |
| 758 | atomic_set(&dev->intr_idx, 0); |
| 759 | atomic_set(&dev->read_idx, 0); |
| 760 | spin_lock_init(&dev->intr_idx_lock); |
| 761 | atomic_set(&dev->overflow_flag, 0); |
| 762 | init_waitqueue_head(&dev->read_wait); |
| 763 | atomic_set(&dev->write_busy, 0); |
| 764 | init_waitqueue_head(&dev->write_wait); |
| 765 | |
| 766 | dev->udev = udev; |
| 767 | dev->interface = interface; |
| 768 | |
| 769 | iface_desc = interface->cur_altsetting; |
| 770 | dev->product_id = le16_to_cpu(udev->descriptor.idProduct); |
| 771 | |
| 772 | /* set up the endpoint information */ |
| 773 | for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) { |
| 774 | endpoint = &iface_desc->endpoint[i].desc; |
| 775 | |
| 776 | if (usb_endpoint_is_int_in(endpoint)) |
| 777 | dev->int_in_endpoint = endpoint; |
| 778 | if (usb_endpoint_is_int_out(endpoint)) |
| 779 | /* this one will match for the IOWarrior56 only */ |
| 780 | dev->int_out_endpoint = endpoint; |
| 781 | } |
| 782 | /* we have to check the report_size often, so remember it in the endianess suitable for our machine */ |
| 783 | dev->report_size = le16_to_cpu(dev->int_in_endpoint->wMaxPacketSize); |
| 784 | if ((dev->interface->cur_altsetting->desc.bInterfaceNumber == 0) && |
| 785 | (dev->product_id == USB_DEVICE_ID_CODEMERCS_IOW56)) |
| 786 | /* IOWarrior56 has wMaxPacketSize different from report size */ |
| 787 | dev->report_size = 7; |
| 788 | |
| 789 | /* create the urb and buffer for reading */ |
| 790 | dev->int_in_urb = usb_alloc_urb(0, GFP_KERNEL); |
| 791 | if (!dev->int_in_urb) { |
| 792 | dev_err(&interface->dev, "Couldn't allocate interrupt_in_urb\n"); |
| 793 | goto error; |
| 794 | } |
| 795 | dev->int_in_buffer = kmalloc(dev->report_size, GFP_KERNEL); |
| 796 | if (!dev->int_in_buffer) { |
| 797 | dev_err(&interface->dev, "Couldn't allocate int_in_buffer\n"); |
| 798 | goto error; |
| 799 | } |
| 800 | usb_fill_int_urb(dev->int_in_urb, dev->udev, |
| 801 | usb_rcvintpipe(dev->udev, |
| 802 | dev->int_in_endpoint->bEndpointAddress), |
| 803 | dev->int_in_buffer, dev->report_size, |
| 804 | iowarrior_callback, dev, |
| 805 | dev->int_in_endpoint->bInterval); |
| 806 | /* create an internal buffer for interrupt data from the device */ |
| 807 | dev->read_queue = |
| 808 | kmalloc(((dev->report_size + 1) * MAX_INTERRUPT_BUFFER), |
| 809 | GFP_KERNEL); |
| 810 | if (!dev->read_queue) { |
| 811 | dev_err(&interface->dev, "Couldn't allocate read_queue\n"); |
| 812 | goto error; |
| 813 | } |
| 814 | /* Get the serial-number of the chip */ |
| 815 | memset(dev->chip_serial, 0x00, sizeof(dev->chip_serial)); |
| 816 | usb_string(udev, udev->descriptor.iSerialNumber, dev->chip_serial, |
| 817 | sizeof(dev->chip_serial)); |
| 818 | if (strlen(dev->chip_serial) != 8) |
| 819 | memset(dev->chip_serial, 0x00, sizeof(dev->chip_serial)); |
| 820 | |
| 821 | /* Set the idle timeout to 0, if this is interface 0 */ |
| 822 | if (dev->interface->cur_altsetting->desc.bInterfaceNumber == 0) { |
Eberhard Fahle | 147c5a1 | 2007-04-05 11:13:21 +0200 | [diff] [blame] | 823 | usb_control_msg(udev, usb_sndctrlpipe(udev, 0), |
| 824 | 0x0A, |
| 825 | USB_TYPE_CLASS | USB_RECIP_INTERFACE, 0, |
| 826 | 0, NULL, 0, USB_CTRL_SET_TIMEOUT); |
Greg Kroah-Hartman | 946b960 | 2007-02-14 13:40:14 -0800 | [diff] [blame] | 827 | } |
| 828 | /* allow device read and ioctl */ |
| 829 | dev->present = 1; |
| 830 | |
| 831 | /* we can register the device now, as it is ready */ |
| 832 | usb_set_intfdata(interface, dev); |
| 833 | |
| 834 | retval = usb_register_dev(interface, &iowarrior_class); |
| 835 | if (retval) { |
| 836 | /* something prevented us from registering this driver */ |
| 837 | dev_err(&interface->dev, "Not able to get a minor for this device.\n"); |
| 838 | usb_set_intfdata(interface, NULL); |
| 839 | goto error; |
| 840 | } |
| 841 | |
| 842 | dev->minor = interface->minor; |
| 843 | |
| 844 | /* let the user know what node this device is now attached to */ |
| 845 | dev_info(&interface->dev, "IOWarrior product=0x%x, serial=%s interface=%d " |
| 846 | "now attached to iowarrior%d\n", dev->product_id, dev->chip_serial, |
| 847 | iface_desc->desc.bInterfaceNumber, dev->minor - IOWARRIOR_MINOR_BASE); |
| 848 | return retval; |
| 849 | |
| 850 | error: |
| 851 | iowarrior_delete(dev); |
| 852 | return retval; |
| 853 | } |
| 854 | |
| 855 | /** |
| 856 | * iowarrior_disconnect |
| 857 | * |
| 858 | * Called by the usb core when the device is removed from the system. |
| 859 | */ |
| 860 | static void iowarrior_disconnect(struct usb_interface *interface) |
| 861 | { |
| 862 | struct iowarrior *dev; |
| 863 | int minor; |
| 864 | |
Greg Kroah-Hartman | 946b960 | 2007-02-14 13:40:14 -0800 | [diff] [blame] | 865 | dev = usb_get_intfdata(interface); |
| 866 | usb_set_intfdata(interface, NULL); |
| 867 | |
Greg Kroah-Hartman | 946b960 | 2007-02-14 13:40:14 -0800 | [diff] [blame] | 868 | minor = dev->minor; |
| 869 | |
| 870 | /* give back our minor */ |
| 871 | usb_deregister_dev(interface, &iowarrior_class); |
| 872 | |
Alan Stern | d4ead16 | 2007-05-22 11:46:41 -0400 | [diff] [blame^] | 873 | mutex_lock(&dev->mutex); |
| 874 | |
Greg Kroah-Hartman | 946b960 | 2007-02-14 13:40:14 -0800 | [diff] [blame] | 875 | /* prevent device read, write and ioctl */ |
| 876 | dev->present = 0; |
| 877 | |
| 878 | mutex_unlock(&dev->mutex); |
| 879 | |
| 880 | if (dev->opened) { |
| 881 | /* There is a process that holds a filedescriptor to the device , |
| 882 | so we only shutdown read-/write-ops going on. |
| 883 | Deleting the device is postponed until close() was called. |
| 884 | */ |
| 885 | usb_kill_urb(dev->int_in_urb); |
| 886 | wake_up_interruptible(&dev->read_wait); |
| 887 | wake_up_interruptible(&dev->write_wait); |
| 888 | } else { |
| 889 | /* no process is using the device, cleanup now */ |
| 890 | iowarrior_delete(dev); |
| 891 | } |
Greg Kroah-Hartman | 946b960 | 2007-02-14 13:40:14 -0800 | [diff] [blame] | 892 | |
| 893 | dev_info(&interface->dev, "I/O-Warror #%d now disconnected\n", |
| 894 | minor - IOWARRIOR_MINOR_BASE); |
| 895 | } |
| 896 | |
| 897 | /* usb specific object needed to register this driver with the usb subsystem */ |
| 898 | static struct usb_driver iowarrior_driver = { |
| 899 | .name = "iowarrior", |
| 900 | .probe = iowarrior_probe, |
| 901 | .disconnect = iowarrior_disconnect, |
| 902 | .id_table = iowarrior_ids, |
| 903 | }; |
| 904 | |
| 905 | static int __init iowarrior_init(void) |
| 906 | { |
| 907 | return usb_register(&iowarrior_driver); |
| 908 | } |
| 909 | |
| 910 | static void __exit iowarrior_exit(void) |
| 911 | { |
| 912 | usb_deregister(&iowarrior_driver); |
| 913 | } |
| 914 | |
| 915 | module_init(iowarrior_init); |
| 916 | module_exit(iowarrior_exit); |