This source file includes following definitions.
- usb_get_report
 
- usb_set_report
 
- iowarrior_callback
 
- iowarrior_write_callback
 
- iowarrior_delete
 
- read_index
 
- iowarrior_read
 
- iowarrior_write
 
- iowarrior_ioctl
 
- iowarrior_open
 
- iowarrior_release
 
- iowarrior_poll
 
- iowarrior_devnode
 
- iowarrior_probe
 
- iowarrior_disconnect
 
   1 
   2 
   3 
   4 
   5 
   6 
   7 
   8 
   9 
  10 
  11 
  12 
  13 
  14 
  15 
  16 
  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>
  24 
  25 #define DRIVER_AUTHOR "Christian Lucht <lucht@codemercs.com>"
  26 #define DRIVER_DESC "USB IO-Warrior driver"
  27 
  28 #define USB_VENDOR_ID_CODEMERCS         1984
  29 
  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 
  35 #define USB_DEVICE_ID_CODEMERCS_IOW56   0x1503
  36 
  37 #define USB_DEVICE_ID_CODEMERCS_IOW28   0x1504
  38 #define USB_DEVICE_ID_CODEMERCS_IOW28L  0x1505
  39 #define USB_DEVICE_ID_CODEMERCS_IOW100  0x1506
  40 
  41 
  42 #define USB_DEVICE_ID_CODEMERCS_IOW24SAG        0x158a
  43 #define USB_DEVICE_ID_CODEMERCS_IOW56AM         0x158b
  44 
  45 
  46 #ifdef CONFIG_USB_DYNAMIC_MINORS
  47 #define IOWARRIOR_MINOR_BASE    0
  48 #else
  49 #define IOWARRIOR_MINOR_BASE    208     
  50 #endif
  51 
  52 
  53 #define MAX_INTERRUPT_BUFFER 16
  54 
  55 
  56 
  57 
  58 
  59 #define MAX_WRITES_IN_FLIGHT 4
  60 
  61 MODULE_AUTHOR(DRIVER_AUTHOR);
  62 MODULE_DESCRIPTION(DRIVER_DESC);
  63 MODULE_LICENSE("GPL");
  64 
  65 static struct usb_driver iowarrior_driver;
  66 
  67 
  68 
  69 
  70 
  71 
  72 struct iowarrior {
  73         struct mutex mutex;                     
  74         struct usb_device *udev;                
  75         struct usb_interface *interface;        
  76         unsigned char minor;                    
  77         struct usb_endpoint_descriptor *int_out_endpoint;       
  78         struct usb_endpoint_descriptor *int_in_endpoint;        
  79         struct urb *int_in_urb;         
  80         unsigned char *int_in_buffer;   
  81         unsigned char serial_number;    
  82         unsigned char *read_queue;      
  83         wait_queue_head_t read_wait;
  84         wait_queue_head_t write_wait;   
  85         atomic_t write_busy;            
  86         atomic_t read_idx;
  87         atomic_t intr_idx;
  88         atomic_t overflow_flag;         
  89         int present;                    
  90         int opened;                     
  91         char chip_serial[9];            
  92         int report_size;                
  93         u16 product_id;
  94         struct usb_anchor submitted;
  95 };
  96 
  97 
  98 
  99 
 100 
 101 
 102 
 103 
 104 #define GET_TIMEOUT 5
 105 #define USB_REQ_GET_REPORT  0x01
 106 
 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)
 110 {
 111         return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
 112                                USB_REQ_GET_REPORT,
 113                                USB_DIR_IN | USB_TYPE_CLASS |
 114                                USB_RECIP_INTERFACE, (type << 8) + id,
 115                                inter->desc.bInterfaceNumber, buf, size,
 116                                GET_TIMEOUT*HZ);
 117 }
 118 
 119 
 120 #define USB_REQ_SET_REPORT 0x09
 121 
 122 static int usb_set_report(struct usb_interface *intf, unsigned char type,
 123                           unsigned char id, void *buf, int size)
 124 {
 125         return usb_control_msg(interface_to_usbdev(intf),
 126                                usb_sndctrlpipe(interface_to_usbdev(intf), 0),
 127                                USB_REQ_SET_REPORT,
 128                                USB_TYPE_CLASS | USB_RECIP_INTERFACE,
 129                                (type << 8) + id,
 130                                intf->cur_altsetting->desc.bInterfaceNumber, buf,
 131                                size, HZ);
 132 }
 133 
 134 
 135 
 136 
 137 
 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         {}                      
 150 };
 151 MODULE_DEVICE_TABLE(usb, iowarrior_ids);
 152 
 153 
 154 
 155 
 156 static void iowarrior_callback(struct urb *urb)
 157 {
 158         struct iowarrior *dev = urb->context;
 159         int intr_idx;
 160         int read_idx;
 161         int aux_idx;
 162         int offset;
 163         int status = urb->status;
 164         int retval;
 165 
 166         switch (status) {
 167         case 0:
 168                 
 169                 break;
 170         case -ECONNRESET:
 171         case -ENOENT:
 172         case -ESHUTDOWN:
 173                 return;
 174         default:
 175                 goto exit;
 176         }
 177 
 178         intr_idx = atomic_read(&dev->intr_idx);
 179         
 180         aux_idx = (intr_idx == 0) ? (MAX_INTERRUPT_BUFFER - 1) : (intr_idx - 1);
 181         read_idx = atomic_read(&dev->read_idx);
 182 
 183         
 184         if ((intr_idx != read_idx)
 185             && (dev->interface->cur_altsetting->desc.bInterfaceNumber == 0)) {
 186                 
 187                 offset = aux_idx * (dev->report_size + 1);
 188                 if (!memcmp
 189                     (dev->read_queue + offset, urb->transfer_buffer,
 190                      dev->report_size)) {
 191                         
 192                         goto exit;
 193                 }
 194         }
 195 
 196         
 197         aux_idx = (intr_idx == (MAX_INTERRUPT_BUFFER - 1)) ? 0 : (intr_idx + 1);
 198         if (read_idx == aux_idx) {
 199                 
 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);
 203         }
 204 
 205         
 206         offset = intr_idx * (dev->report_size + 1);
 207         memcpy(dev->read_queue + offset, urb->transfer_buffer,
 208                dev->report_size);
 209         *(dev->read_queue + offset + (dev->report_size)) = dev->serial_number++;
 210 
 211         atomic_set(&dev->intr_idx, aux_idx);
 212         
 213         wake_up_interruptible(&dev->read_wait);
 214 
 215 exit:
 216         retval = usb_submit_urb(urb, GFP_ATOMIC);
 217         if (retval)
 218                 dev_err(&dev->interface->dev, "%s - usb_submit_urb failed with result %d\n",
 219                         __func__, retval);
 220 
 221 }
 222 
 223 
 224 
 225 
 226 static void iowarrior_write_callback(struct urb *urb)
 227 {
 228         struct iowarrior *dev;
 229         int status = urb->status;
 230 
 231         dev = urb->context;
 232         
 233         if (status &&
 234             !(status == -ENOENT ||
 235               status == -ECONNRESET || status == -ESHUTDOWN)) {
 236                 dev_dbg(&dev->interface->dev,
 237                         "nonzero write bulk status received: %d\n", status);
 238         }
 239         
 240         usb_free_coherent(urb->dev, urb->transfer_buffer_length,
 241                           urb->transfer_buffer, urb->transfer_dma);
 242         
 243         atomic_dec(&dev->write_busy);
 244         wake_up_interruptible(&dev->write_wait);
 245 }
 246 
 247 
 248 
 249 
 250 static inline void iowarrior_delete(struct iowarrior *dev)
 251 {
 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);
 257         kfree(dev);
 258 }
 259 
 260 
 261 
 262 
 263 
 264 static int read_index(struct iowarrior *dev)
 265 {
 266         int intr_idx, read_idx;
 267 
 268         read_idx = atomic_read(&dev->read_idx);
 269         intr_idx = atomic_read(&dev->intr_idx);
 270 
 271         return (read_idx == intr_idx ? -1 : read_idx);
 272 }
 273 
 274 
 275 
 276 
 277 static ssize_t iowarrior_read(struct file *file, char __user *buffer,
 278                               size_t count, loff_t *ppos)
 279 {
 280         struct iowarrior *dev;
 281         int read_idx;
 282         int offset;
 283 
 284         dev = file->private_data;
 285 
 286         
 287         if (!dev || !dev->present)
 288                 return -ENODEV;
 289 
 290         dev_dbg(&dev->interface->dev, "minor %d, count = %zd\n",
 291                 dev->minor, count);
 292 
 293         
 294         if ((count != dev->report_size)
 295             && (count != (dev->report_size + 1)))
 296                 return -EINVAL;
 297 
 298         
 299         do {
 300                 atomic_set(&dev->overflow_flag, 0);
 301                 if ((read_idx = read_index(dev)) == -1) {
 302                         
 303                         if (file->f_flags & O_NONBLOCK)
 304                                 return -EAGAIN;
 305                         else {
 306                                 
 307                                 int r = wait_event_interruptible(dev->read_wait,
 308                                                                  (!dev->present
 309                                                                   || (read_idx =
 310                                                                       read_index
 311                                                                       (dev)) !=
 312                                                                   -1));
 313                                 if (r) {
 314                                         
 315                                         return -ERESTART;
 316                                 }
 317                                 if (!dev->present) {
 318                                         
 319                                         return -ENODEV;
 320                                 }
 321                                 if (read_idx == -1) {
 322                                         
 323                                         return 0;
 324                                 }
 325                         }
 326                 }
 327 
 328                 offset = read_idx * (dev->report_size + 1);
 329                 if (copy_to_user(buffer, dev->read_queue + offset, count)) {
 330                         return -EFAULT;
 331                 }
 332         } while (atomic_read(&dev->overflow_flag));
 333 
 334         read_idx = ++read_idx == MAX_INTERRUPT_BUFFER ? 0 : read_idx;
 335         atomic_set(&dev->read_idx, read_idx);
 336         return count;
 337 }
 338 
 339 
 340 
 341 
 342 static ssize_t iowarrior_write(struct file *file,
 343                                const char __user *user_buffer,
 344                                size_t count, loff_t *ppos)
 345 {
 346         struct iowarrior *dev;
 347         int retval = 0;
 348         char *buf = NULL;       
 349         struct urb *int_out_urb = NULL;
 350 
 351         dev = file->private_data;
 352 
 353         mutex_lock(&dev->mutex);
 354         
 355         if (!dev->present) {
 356                 retval = -ENODEV;
 357                 goto exit;
 358         }
 359         dev_dbg(&dev->interface->dev, "minor %d, count = %zd\n",
 360                 dev->minor, count);
 361         
 362         if (count == 0) {
 363                 retval = 0;
 364                 goto exit;
 365         }
 366         
 367         if (count != dev->report_size) {
 368                 retval = -EINVAL;
 369                 goto exit;
 370         }
 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                 
 378                 buf = memdup_user(user_buffer, count);
 379                 if (IS_ERR(buf)) {
 380                         retval = PTR_ERR(buf);
 381                         goto exit;
 382                 }
 383                 retval = usb_set_report(dev->interface, 2, 0, buf, count);
 384                 kfree(buf);
 385                 goto exit;
 386                 break;
 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                 
 393                 if (atomic_read(&dev->write_busy) == MAX_WRITES_IN_FLIGHT) {
 394                         
 395                         if (file->f_flags & O_NONBLOCK) {
 396                                 retval = -EAGAIN;
 397                                 goto exit;
 398                         } else {
 399                                 retval = wait_event_interruptible(dev->write_wait,
 400                                                                   (!dev->present || (atomic_read (&dev-> write_busy) < MAX_WRITES_IN_FLIGHT)));
 401                                 if (retval) {
 402                                         
 403                                         retval = -ERESTART;
 404                                         goto exit;
 405                                 }
 406                                 if (!dev->present) {
 407                                         
 408                                         retval = -ENODEV;
 409                                         goto exit;
 410                                 }
 411                                 if (!dev->opened) {
 412                                         
 413                                         retval = -ENODEV;
 414                                         goto exit;
 415                                 }
 416                         }
 417                 }
 418                 atomic_inc(&dev->write_busy);
 419                 int_out_urb = usb_alloc_urb(0, GFP_KERNEL);
 420                 if (!int_out_urb) {
 421                         retval = -ENOMEM;
 422                         goto error_no_urb;
 423                 }
 424                 buf = usb_alloc_coherent(dev->udev, dev->report_size,
 425                                          GFP_KERNEL, &int_out_urb->transfer_dma);
 426                 if (!buf) {
 427                         retval = -ENOMEM;
 428                         dev_dbg(&dev->interface->dev,
 429                                 "Unable to allocate buffer\n");
 430                         goto error_no_buffer;
 431                 }
 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)) {
 440                         retval = -EFAULT;
 441                         goto error;
 442                 }
 443                 usb_anchor_urb(int_out_urb, &dev->submitted);
 444                 retval = usb_submit_urb(int_out_urb, GFP_KERNEL);
 445                 if (retval) {
 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);
 450                         goto error;
 451                 }
 452                 
 453                 retval = count;
 454                 usb_free_urb(int_out_urb);
 455                 goto exit;
 456                 break;
 457         default:
 458                 
 459                 dev_err(&dev->interface->dev, "%s - not supported for product=0x%x\n",
 460                         __func__, dev->product_id);
 461                 retval = -EFAULT;
 462                 goto exit;
 463                 break;
 464         }
 465 error:
 466         usb_free_coherent(dev->udev, dev->report_size, buf,
 467                           int_out_urb->transfer_dma);
 468 error_no_buffer:
 469         usb_free_urb(int_out_urb);
 470 error_no_urb:
 471         atomic_dec(&dev->write_busy);
 472         wake_up_interruptible(&dev->write_wait);
 473 exit:
 474         mutex_unlock(&dev->mutex);
 475         return retval;
 476 }
 477 
 478 
 479 
 480 
 481 static long iowarrior_ioctl(struct file *file, unsigned int cmd,
 482                                                         unsigned long arg)
 483 {
 484         struct iowarrior *dev = NULL;
 485         __u8 *buffer;
 486         __u8 __user *user_buffer;
 487         int retval;
 488         int io_res;             
 489 
 490         dev = file->private_data;
 491         if (!dev)
 492                 return -ENODEV;
 493 
 494         buffer = kzalloc(dev->report_size, GFP_KERNEL);
 495         if (!buffer)
 496                 return -ENOMEM;
 497 
 498         mutex_lock(&dev->mutex);
 499 
 500         
 501         if (!dev->present) {
 502                 retval = -ENODEV;
 503                 goto error_out;
 504         }
 505 
 506         dev_dbg(&dev->interface->dev, "minor %d, cmd 0x%.4x, arg %ld\n",
 507                 dev->minor, cmd, arg);
 508 
 509         retval = 0;
 510         io_res = 0;
 511         switch (cmd) {
 512         case IOW_WRITE:
 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,
 520                                                 dev->report_size);
 521                         if (io_res) {
 522                                 retval = -EFAULT;
 523                         } else {
 524                                 io_res = usb_set_report(dev->interface, 2, 0,
 525                                                         buffer,
 526                                                         dev->report_size);
 527                                 if (io_res < 0)
 528                                         retval = io_res;
 529                         }
 530                 } else {
 531                         retval = -EINVAL;
 532                         dev_err(&dev->interface->dev,
 533                                 "ioctl 'IOW_WRITE' is not supported for product=0x%x.\n",
 534                                 dev->product_id);
 535                 }
 536                 break;
 537         case IOW_READ:
 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);
 542                 if (io_res < 0)
 543                         retval = io_res;
 544                 else {
 545                         io_res = copy_to_user(user_buffer, buffer, dev->report_size);
 546                         if (io_res)
 547                                 retval = -EFAULT;
 548                 }
 549                 break;
 550         case IOW_GETINFO:
 551                 {
 552                         
 553                         struct iowarrior_info info;
 554                         
 555                         struct usb_config_descriptor *cfg_descriptor = &dev->udev->actconfig->desc;
 556 
 557                         memset(&info, 0, sizeof(info));
 558                         
 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);
 562 
 563                         
 564                         info.speed = dev->udev->speed;
 565                         info.if_num = dev->interface->cur_altsetting->desc.bInterfaceNumber;
 566                         info.report_size = dev->report_size;
 567 
 568                         
 569                         memcpy(info.serial, dev->chip_serial,
 570                                sizeof(dev->chip_serial));
 571                         if (cfg_descriptor == NULL) {
 572                                 info.power = -1;        
 573                         } else {
 574                                 
 575                                 info.power = cfg_descriptor->bMaxPower * 2;
 576                         }
 577                         io_res = copy_to_user((struct iowarrior_info __user *)arg, &info,
 578                                          sizeof(struct iowarrior_info));
 579                         if (io_res)
 580                                 retval = -EFAULT;
 581                         break;
 582                 }
 583         default:
 584                 
 585                 retval = -ENOTTY;
 586                 break;
 587         }
 588 error_out:
 589         
 590         mutex_unlock(&dev->mutex);
 591         kfree(buffer);
 592         return retval;
 593 }
 594 
 595 
 596 
 597 
 598 static int iowarrior_open(struct inode *inode, struct file *file)
 599 {
 600         struct iowarrior *dev = NULL;
 601         struct usb_interface *interface;
 602         int subminor;
 603         int retval = 0;
 604 
 605         subminor = iminor(inode);
 606 
 607         interface = usb_find_interface(&iowarrior_driver, subminor);
 608         if (!interface) {
 609                 pr_err("%s - error, can't find device for minor %d\n",
 610                        __func__, subminor);
 611                 return -ENODEV;
 612         }
 613 
 614         dev = usb_get_intfdata(interface);
 615         if (!dev)
 616                 return -ENODEV;
 617 
 618         mutex_lock(&dev->mutex);
 619 
 620         
 621         if (dev->opened) {
 622                 retval = -EBUSY;
 623                 goto out;
 624         }
 625 
 626         
 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);
 629                 retval = -EFAULT;
 630                 goto out;
 631         }
 632         
 633         ++dev->opened;
 634         
 635         file->private_data = dev;
 636         retval = 0;
 637 
 638 out:
 639         mutex_unlock(&dev->mutex);
 640         return retval;
 641 }
 642 
 643 
 644 
 645 
 646 static int iowarrior_release(struct inode *inode, struct file *file)
 647 {
 648         struct iowarrior *dev;
 649         int retval = 0;
 650 
 651         dev = file->private_data;
 652         if (!dev)
 653                 return -ENODEV;
 654 
 655         dev_dbg(&dev->interface->dev, "minor %d\n", dev->minor);
 656 
 657         
 658         mutex_lock(&dev->mutex);
 659 
 660         if (dev->opened <= 0) {
 661                 retval = -ENODEV;       
 662                 mutex_unlock(&dev->mutex);
 663         } else {
 664                 dev->opened = 0;        
 665                 retval = 0;
 666                 if (dev->present) {
 667                         
 668 
 669 
 670 
 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);
 675                 } else {
 676                         
 677                         mutex_unlock(&dev->mutex);
 678                         iowarrior_delete(dev);
 679                 }
 680         }
 681         return retval;
 682 }
 683 
 684 static __poll_t iowarrior_poll(struct file *file, poll_table * wait)
 685 {
 686         struct iowarrior *dev = file->private_data;
 687         __poll_t mask = 0;
 688 
 689         if (!dev->present)
 690                 return EPOLLERR | EPOLLHUP;
 691 
 692         poll_wait(file, &dev->read_wait, wait);
 693         poll_wait(file, &dev->write_wait, wait);
 694 
 695         if (!dev->present)
 696                 return EPOLLERR | EPOLLHUP;
 697 
 698         if (read_index(dev) != -1)
 699                 mask |= EPOLLIN | EPOLLRDNORM;
 700 
 701         if (atomic_read(&dev->write_busy) < MAX_WRITES_IN_FLIGHT)
 702                 mask |= EPOLLOUT | EPOLLWRNORM;
 703         return mask;
 704 }
 705 
 706 
 707 
 708 
 709 
 710 
 711 
 712 
 713 
 714 
 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,
 724 };
 725 
 726 static char *iowarrior_devnode(struct device *dev, umode_t *mode)
 727 {
 728         return kasprintf(GFP_KERNEL, "usb/%s", dev_name(dev));
 729 }
 730 
 731 
 732 
 733 
 734 
 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,
 740 };
 741 
 742 
 743 
 744 
 745 
 746 
 747 
 748 
 749 
 750 
 751 static int iowarrior_probe(struct usb_interface *interface,
 752                            const struct usb_device_id *id)
 753 {
 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;
 758         int res;
 759 
 760         
 761         dev = kzalloc(sizeof(struct iowarrior), GFP_KERNEL);
 762         if (!dev)
 763                 return retval;
 764 
 765         mutex_init(&dev->mutex);
 766 
 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);
 773 
 774         dev->udev = udev;
 775         dev->interface = usb_get_intf(interface);
 776 
 777         iface_desc = interface->cur_altsetting;
 778         dev->product_id = le16_to_cpu(udev->descriptor.idProduct);
 779 
 780         init_usb_anchor(&dev->submitted);
 781 
 782         res = usb_find_last_int_in_endpoint(iface_desc, &dev->int_in_endpoint);
 783         if (res) {
 784                 dev_err(&interface->dev, "no interrupt-in endpoint found\n");
 785                 retval = res;
 786                 goto error;
 787         }
 788 
 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);
 796                 if (res) {
 797                         dev_err(&interface->dev, "no interrupt-out endpoint found\n");
 798                         retval = res;
 799                         goto error;
 800                 }
 801         }
 802 
 803         
 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                 
 812                 dev->report_size = 7;
 813 
 814         
 815         dev->int_in_urb = usb_alloc_urb(0, GFP_KERNEL);
 816         if (!dev->int_in_urb)
 817                 goto error;
 818         dev->int_in_buffer = kmalloc(dev->report_size, GFP_KERNEL);
 819         if (!dev->int_in_buffer)
 820                 goto error;
 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         
 828         dev->read_queue =
 829             kmalloc_array(dev->report_size + 1, MAX_INTERRUPT_BUFFER,
 830                           GFP_KERNEL);
 831         if (!dev->read_queue)
 832                 goto error;
 833         
 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));
 839 
 840         
 841         if (dev->interface->cur_altsetting->desc.bInterfaceNumber == 0) {
 842             usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
 843                             0x0A,
 844                             USB_TYPE_CLASS | USB_RECIP_INTERFACE, 0,
 845                             0, NULL, 0, USB_CTRL_SET_TIMEOUT);
 846         }
 847         
 848         dev->present = 1;
 849 
 850         
 851         usb_set_intfdata(interface, dev);
 852 
 853         retval = usb_register_dev(interface, &iowarrior_class);
 854         if (retval) {
 855                 
 856                 dev_err(&interface->dev, "Not able to get a minor for this device.\n");
 857                 goto error;
 858         }
 859 
 860         dev->minor = interface->minor;
 861 
 862         
 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);
 866         return retval;
 867 
 868 error:
 869         iowarrior_delete(dev);
 870         return retval;
 871 }
 872 
 873 
 874 
 875 
 876 
 877 
 878 static void iowarrior_disconnect(struct usb_interface *interface)
 879 {
 880         struct iowarrior *dev = usb_get_intfdata(interface);
 881         int minor = dev->minor;
 882 
 883         usb_deregister_dev(interface, &iowarrior_class);
 884 
 885         mutex_lock(&dev->mutex);
 886 
 887         
 888         dev->present = 0;
 889 
 890         if (dev->opened) {
 891                 
 892 
 893 
 894 
 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);
 900         } else {
 901                 
 902                 mutex_unlock(&dev->mutex);
 903                 iowarrior_delete(dev);
 904         }
 905 
 906         dev_info(&interface->dev, "I/O-Warror #%d now disconnected\n",
 907                  minor - IOWARRIOR_MINOR_BASE);
 908 }
 909 
 910 
 911 static struct usb_driver iowarrior_driver = {
 912         .name = "iowarrior",
 913         .probe = iowarrior_probe,
 914         .disconnect = iowarrior_disconnect,
 915         .id_table = iowarrior_ids,
 916 };
 917 
 918 module_usb_driver(iowarrior_driver);