linux/drivers/usb/gadget/function/f_printer.c
<<
>>
Prefs
   1/*
   2 * f_printer.c - USB printer function driver
   3 *
   4 * Copied from drivers/usb/gadget/legacy/printer.c,
   5 * which was:
   6 *
   7 * printer.c -- Printer gadget driver
   8 *
   9 * Copyright (C) 2003-2005 David Brownell
  10 * Copyright (C) 2006 Craig W. Nadler
  11 *
  12 * This program is free software; you can redistribute it and/or modify
  13 * it under the terms of the GNU General Public License as published by
  14 * the Free Software Foundation; either version 2 of the License, or
  15 * (at your option) any later version.
  16 */
  17
  18#include <linux/module.h>
  19#include <linux/kernel.h>
  20#include <linux/delay.h>
  21#include <linux/ioport.h>
  22#include <linux/sched.h>
  23#include <linux/slab.h>
  24#include <linux/mutex.h>
  25#include <linux/errno.h>
  26#include <linux/init.h>
  27#include <linux/idr.h>
  28#include <linux/timer.h>
  29#include <linux/list.h>
  30#include <linux/interrupt.h>
  31#include <linux/device.h>
  32#include <linux/moduleparam.h>
  33#include <linux/fs.h>
  34#include <linux/poll.h>
  35#include <linux/types.h>
  36#include <linux/ctype.h>
  37#include <linux/cdev.h>
  38
  39#include <asm/byteorder.h>
  40#include <linux/io.h>
  41#include <linux/irq.h>
  42#include <linux/uaccess.h>
  43#include <asm/unaligned.h>
  44
  45#include <linux/usb/ch9.h>
  46#include <linux/usb/composite.h>
  47#include <linux/usb/gadget.h>
  48#include <linux/usb/g_printer.h>
  49
  50#include "u_printer.h"
  51
  52#define PNP_STRING_LEN          1024
  53#define PRINTER_MINORS          4
  54#define GET_DEVICE_ID           0
  55#define GET_PORT_STATUS         1
  56#define SOFT_RESET              2
  57
  58static int major, minors;
  59static struct class *usb_gadget_class;
  60static DEFINE_IDA(printer_ida);
  61static DEFINE_MUTEX(printer_ida_lock); /* protects access do printer_ida */
  62
  63/*-------------------------------------------------------------------------*/
  64
  65struct printer_dev {
  66        spinlock_t              lock;           /* lock this structure */
  67        /* lock buffer lists during read/write calls */
  68        struct mutex            lock_printer_io;
  69        struct usb_gadget       *gadget;
  70        s8                      interface;
  71        struct usb_ep           *in_ep, *out_ep;
  72
  73        struct list_head        rx_reqs;        /* List of free RX structs */
  74        struct list_head        rx_reqs_active; /* List of Active RX xfers */
  75        struct list_head        rx_buffers;     /* List of completed xfers */
  76        /* wait until there is data to be read. */
  77        wait_queue_head_t       rx_wait;
  78        struct list_head        tx_reqs;        /* List of free TX structs */
  79        struct list_head        tx_reqs_active; /* List of Active TX xfers */
  80        /* Wait until there are write buffers available to use. */
  81        wait_queue_head_t       tx_wait;
  82        /* Wait until all write buffers have been sent. */
  83        wait_queue_head_t       tx_flush_wait;
  84        struct usb_request      *current_rx_req;
  85        size_t                  current_rx_bytes;
  86        u8                      *current_rx_buf;
  87        u8                      printer_status;
  88        u8                      reset_printer;
  89        int                     minor;
  90        struct cdev             printer_cdev;
  91        u8                      printer_cdev_open;
  92        wait_queue_head_t       wait;
  93        unsigned                q_len;
  94        char                    *pnp_string;    /* We don't own memory! */
  95        struct usb_function     function;
  96};
  97
  98static inline struct printer_dev *func_to_printer(struct usb_function *f)
  99{
 100        return container_of(f, struct printer_dev, function);
 101}
 102
 103/*-------------------------------------------------------------------------*/
 104
 105/*
 106 * DESCRIPTORS ... most are static, but strings and (full) configuration
 107 * descriptors are built on demand.
 108 */
 109
 110/* holds our biggest descriptor */
 111#define USB_DESC_BUFSIZE                256
 112#define USB_BUFSIZE                     8192
 113
 114static struct usb_interface_descriptor intf_desc = {
 115        .bLength =              sizeof(intf_desc),
 116        .bDescriptorType =      USB_DT_INTERFACE,
 117        .bNumEndpoints =        2,
 118        .bInterfaceClass =      USB_CLASS_PRINTER,
 119        .bInterfaceSubClass =   1,      /* Printer Sub-Class */
 120        .bInterfaceProtocol =   2,      /* Bi-Directional */
 121        .iInterface =           0
 122};
 123
 124static struct usb_endpoint_descriptor fs_ep_in_desc = {
 125        .bLength =              USB_DT_ENDPOINT_SIZE,
 126        .bDescriptorType =      USB_DT_ENDPOINT,
 127        .bEndpointAddress =     USB_DIR_IN,
 128        .bmAttributes =         USB_ENDPOINT_XFER_BULK
 129};
 130
 131static struct usb_endpoint_descriptor fs_ep_out_desc = {
 132        .bLength =              USB_DT_ENDPOINT_SIZE,
 133        .bDescriptorType =      USB_DT_ENDPOINT,
 134        .bEndpointAddress =     USB_DIR_OUT,
 135        .bmAttributes =         USB_ENDPOINT_XFER_BULK
 136};
 137
 138static struct usb_descriptor_header *fs_printer_function[] = {
 139        (struct usb_descriptor_header *) &intf_desc,
 140        (struct usb_descriptor_header *) &fs_ep_in_desc,
 141        (struct usb_descriptor_header *) &fs_ep_out_desc,
 142        NULL
 143};
 144
 145/*
 146 * usb 2.0 devices need to expose both high speed and full speed
 147 * descriptors, unless they only run at full speed.
 148 */
 149
 150static struct usb_endpoint_descriptor hs_ep_in_desc = {
 151        .bLength =              USB_DT_ENDPOINT_SIZE,
 152        .bDescriptorType =      USB_DT_ENDPOINT,
 153        .bmAttributes =         USB_ENDPOINT_XFER_BULK,
 154        .wMaxPacketSize =       cpu_to_le16(512)
 155};
 156
 157static struct usb_endpoint_descriptor hs_ep_out_desc = {
 158        .bLength =              USB_DT_ENDPOINT_SIZE,
 159        .bDescriptorType =      USB_DT_ENDPOINT,
 160        .bmAttributes =         USB_ENDPOINT_XFER_BULK,
 161        .wMaxPacketSize =       cpu_to_le16(512)
 162};
 163
 164static struct usb_descriptor_header *hs_printer_function[] = {
 165        (struct usb_descriptor_header *) &intf_desc,
 166        (struct usb_descriptor_header *) &hs_ep_in_desc,
 167        (struct usb_descriptor_header *) &hs_ep_out_desc,
 168        NULL
 169};
 170
 171/*
 172 * Added endpoint descriptors for 3.0 devices
 173 */
 174
 175static struct usb_endpoint_descriptor ss_ep_in_desc = {
 176        .bLength =              USB_DT_ENDPOINT_SIZE,
 177        .bDescriptorType =      USB_DT_ENDPOINT,
 178        .bmAttributes =         USB_ENDPOINT_XFER_BULK,
 179        .wMaxPacketSize =       cpu_to_le16(1024),
 180};
 181
 182static struct usb_ss_ep_comp_descriptor ss_ep_in_comp_desc = {
 183        .bLength =              sizeof(ss_ep_in_comp_desc),
 184        .bDescriptorType =      USB_DT_SS_ENDPOINT_COMP,
 185};
 186
 187static struct usb_endpoint_descriptor ss_ep_out_desc = {
 188        .bLength =              USB_DT_ENDPOINT_SIZE,
 189        .bDescriptorType =      USB_DT_ENDPOINT,
 190        .bmAttributes =         USB_ENDPOINT_XFER_BULK,
 191        .wMaxPacketSize =       cpu_to_le16(1024),
 192};
 193
 194static struct usb_ss_ep_comp_descriptor ss_ep_out_comp_desc = {
 195        .bLength =              sizeof(ss_ep_out_comp_desc),
 196        .bDescriptorType =      USB_DT_SS_ENDPOINT_COMP,
 197};
 198
 199static struct usb_descriptor_header *ss_printer_function[] = {
 200        (struct usb_descriptor_header *) &intf_desc,
 201        (struct usb_descriptor_header *) &ss_ep_in_desc,
 202        (struct usb_descriptor_header *) &ss_ep_in_comp_desc,
 203        (struct usb_descriptor_header *) &ss_ep_out_desc,
 204        (struct usb_descriptor_header *) &ss_ep_out_comp_desc,
 205        NULL
 206};
 207
 208/* maxpacket and other transfer characteristics vary by speed. */
 209static inline struct usb_endpoint_descriptor *ep_desc(struct usb_gadget *gadget,
 210                                        struct usb_endpoint_descriptor *fs,
 211                                        struct usb_endpoint_descriptor *hs,
 212                                        struct usb_endpoint_descriptor *ss)
 213{
 214        switch (gadget->speed) {
 215        case USB_SPEED_SUPER:
 216                return ss;
 217        case USB_SPEED_HIGH:
 218                return hs;
 219        default:
 220                return fs;
 221        }
 222}
 223
 224/*-------------------------------------------------------------------------*/
 225
 226static struct usb_request *
 227printer_req_alloc(struct usb_ep *ep, unsigned len, gfp_t gfp_flags)
 228{
 229        struct usb_request      *req;
 230
 231        req = usb_ep_alloc_request(ep, gfp_flags);
 232
 233        if (req != NULL) {
 234                req->length = len;
 235                req->buf = kmalloc(len, gfp_flags);
 236                if (req->buf == NULL) {
 237                        usb_ep_free_request(ep, req);
 238                        return NULL;
 239                }
 240        }
 241
 242        return req;
 243}
 244
 245static void
 246printer_req_free(struct usb_ep *ep, struct usb_request *req)
 247{
 248        if (ep != NULL && req != NULL) {
 249                kfree(req->buf);
 250                usb_ep_free_request(ep, req);
 251        }
 252}
 253
 254/*-------------------------------------------------------------------------*/
 255
 256static void rx_complete(struct usb_ep *ep, struct usb_request *req)
 257{
 258        struct printer_dev      *dev = ep->driver_data;
 259        int                     status = req->status;
 260        unsigned long           flags;
 261
 262        spin_lock_irqsave(&dev->lock, flags);
 263
 264        list_del_init(&req->list);      /* Remode from Active List */
 265
 266        switch (status) {
 267
 268        /* normal completion */
 269        case 0:
 270                if (req->actual > 0) {
 271                        list_add_tail(&req->list, &dev->rx_buffers);
 272                        DBG(dev, "G_Printer : rx length %d\n", req->actual);
 273                } else {
 274                        list_add(&req->list, &dev->rx_reqs);
 275                }
 276                break;
 277
 278        /* software-driven interface shutdown */
 279        case -ECONNRESET:               /* unlink */
 280        case -ESHUTDOWN:                /* disconnect etc */
 281                VDBG(dev, "rx shutdown, code %d\n", status);
 282                list_add(&req->list, &dev->rx_reqs);
 283                break;
 284
 285        /* for hardware automagic (such as pxa) */
 286        case -ECONNABORTED:             /* endpoint reset */
 287                DBG(dev, "rx %s reset\n", ep->name);
 288                list_add(&req->list, &dev->rx_reqs);
 289                break;
 290
 291        /* data overrun */
 292        case -EOVERFLOW:
 293                /* FALLTHROUGH */
 294
 295        default:
 296                DBG(dev, "rx status %d\n", status);
 297                list_add(&req->list, &dev->rx_reqs);
 298                break;
 299        }
 300
 301        wake_up_interruptible(&dev->rx_wait);
 302        spin_unlock_irqrestore(&dev->lock, flags);
 303}
 304
 305static void tx_complete(struct usb_ep *ep, struct usb_request *req)
 306{
 307        struct printer_dev      *dev = ep->driver_data;
 308
 309        switch (req->status) {
 310        default:
 311                VDBG(dev, "tx err %d\n", req->status);
 312                /* FALLTHROUGH */
 313        case -ECONNRESET:               /* unlink */
 314        case -ESHUTDOWN:                /* disconnect etc */
 315                break;
 316        case 0:
 317                break;
 318        }
 319
 320        spin_lock(&dev->lock);
 321        /* Take the request struct off the active list and put it on the
 322         * free list.
 323         */
 324        list_del_init(&req->list);
 325        list_add(&req->list, &dev->tx_reqs);
 326        wake_up_interruptible(&dev->tx_wait);
 327        if (likely(list_empty(&dev->tx_reqs_active)))
 328                wake_up_interruptible(&dev->tx_flush_wait);
 329
 330        spin_unlock(&dev->lock);
 331}
 332
 333/*-------------------------------------------------------------------------*/
 334
 335static int
 336printer_open(struct inode *inode, struct file *fd)
 337{
 338        struct printer_dev      *dev;
 339        unsigned long           flags;
 340        int                     ret = -EBUSY;
 341
 342        dev = container_of(inode->i_cdev, struct printer_dev, printer_cdev);
 343
 344        spin_lock_irqsave(&dev->lock, flags);
 345
 346        if (!dev->printer_cdev_open) {
 347                dev->printer_cdev_open = 1;
 348                fd->private_data = dev;
 349                ret = 0;
 350                /* Change the printer status to show that it's on-line. */
 351                dev->printer_status |= PRINTER_SELECTED;
 352        }
 353
 354        spin_unlock_irqrestore(&dev->lock, flags);
 355
 356        DBG(dev, "printer_open returned %x\n", ret);
 357        return ret;
 358}
 359
 360static int
 361printer_close(struct inode *inode, struct file *fd)
 362{
 363        struct printer_dev      *dev = fd->private_data;
 364        unsigned long           flags;
 365
 366        spin_lock_irqsave(&dev->lock, flags);
 367        dev->printer_cdev_open = 0;
 368        fd->private_data = NULL;
 369        /* Change printer status to show that the printer is off-line. */
 370        dev->printer_status &= ~PRINTER_SELECTED;
 371        spin_unlock_irqrestore(&dev->lock, flags);
 372
 373        DBG(dev, "printer_close\n");
 374
 375        return 0;
 376}
 377
 378/* This function must be called with interrupts turned off. */
 379static void
 380setup_rx_reqs(struct printer_dev *dev)
 381{
 382        struct usb_request              *req;
 383
 384        while (likely(!list_empty(&dev->rx_reqs))) {
 385                int error;
 386
 387                req = container_of(dev->rx_reqs.next,
 388                                struct usb_request, list);
 389                list_del_init(&req->list);
 390
 391                /* The USB Host sends us whatever amount of data it wants to
 392                 * so we always set the length field to the full USB_BUFSIZE.
 393                 * If the amount of data is more than the read() caller asked
 394                 * for it will be stored in the request buffer until it is
 395                 * asked for by read().
 396                 */
 397                req->length = USB_BUFSIZE;
 398                req->complete = rx_complete;
 399
 400                /* here, we unlock, and only unlock, to avoid deadlock. */
 401                spin_unlock(&dev->lock);
 402                error = usb_ep_queue(dev->out_ep, req, GFP_ATOMIC);
 403                spin_lock(&dev->lock);
 404                if (error) {
 405                        DBG(dev, "rx submit --> %d\n", error);
 406                        list_add(&req->list, &dev->rx_reqs);
 407                        break;
 408                }
 409                /* if the req is empty, then add it into dev->rx_reqs_active. */
 410                else if (list_empty(&req->list))
 411                        list_add(&req->list, &dev->rx_reqs_active);
 412        }
 413}
 414
 415static ssize_t
 416printer_read(struct file *fd, char __user *buf, size_t len, loff_t *ptr)
 417{
 418        struct printer_dev              *dev = fd->private_data;
 419        unsigned long                   flags;
 420        size_t                          size;
 421        size_t                          bytes_copied;
 422        struct usb_request              *req;
 423        /* This is a pointer to the current USB rx request. */
 424        struct usb_request              *current_rx_req;
 425        /* This is the number of bytes in the current rx buffer. */
 426        size_t                          current_rx_bytes;
 427        /* This is a pointer to the current rx buffer. */
 428        u8                              *current_rx_buf;
 429
 430        if (len == 0)
 431                return -EINVAL;
 432
 433        DBG(dev, "printer_read trying to read %d bytes\n", (int)len);
 434
 435        mutex_lock(&dev->lock_printer_io);
 436        spin_lock_irqsave(&dev->lock, flags);
 437
 438        /* We will use this flag later to check if a printer reset happened
 439         * after we turn interrupts back on.
 440         */
 441        dev->reset_printer = 0;
 442
 443        setup_rx_reqs(dev);
 444
 445        bytes_copied = 0;
 446        current_rx_req = dev->current_rx_req;
 447        current_rx_bytes = dev->current_rx_bytes;
 448        current_rx_buf = dev->current_rx_buf;
 449        dev->current_rx_req = NULL;
 450        dev->current_rx_bytes = 0;
 451        dev->current_rx_buf = NULL;
 452
 453        /* Check if there is any data in the read buffers. Please note that
 454         * current_rx_bytes is the number of bytes in the current rx buffer.
 455         * If it is zero then check if there are any other rx_buffers that
 456         * are on the completed list. We are only out of data if all rx
 457         * buffers are empty.
 458         */
 459        if ((current_rx_bytes == 0) &&
 460                        (likely(list_empty(&dev->rx_buffers)))) {
 461                /* Turn interrupts back on before sleeping. */
 462                spin_unlock_irqrestore(&dev->lock, flags);
 463
 464                /*
 465                 * If no data is available check if this is a NON-Blocking
 466                 * call or not.
 467                 */
 468                if (fd->f_flags & (O_NONBLOCK|O_NDELAY)) {
 469                        mutex_unlock(&dev->lock_printer_io);
 470                        return -EAGAIN;
 471                }
 472
 473                /* Sleep until data is available */
 474                wait_event_interruptible(dev->rx_wait,
 475                                (likely(!list_empty(&dev->rx_buffers))));
 476                spin_lock_irqsave(&dev->lock, flags);
 477        }
 478
 479        /* We have data to return then copy it to the caller's buffer.*/
 480        while ((current_rx_bytes || likely(!list_empty(&dev->rx_buffers)))
 481                        && len) {
 482                if (current_rx_bytes == 0) {
 483                        req = container_of(dev->rx_buffers.next,
 484                                        struct usb_request, list);
 485                        list_del_init(&req->list);
 486
 487                        if (req->actual && req->buf) {
 488                                current_rx_req = req;
 489                                current_rx_bytes = req->actual;
 490                                current_rx_buf = req->buf;
 491                        } else {
 492                                list_add(&req->list, &dev->rx_reqs);
 493                                continue;
 494                        }
 495                }
 496
 497                /* Don't leave irqs off while doing memory copies */
 498                spin_unlock_irqrestore(&dev->lock, flags);
 499
 500                if (len > current_rx_bytes)
 501                        size = current_rx_bytes;
 502                else
 503                        size = len;
 504
 505                size -= copy_to_user(buf, current_rx_buf, size);
 506                bytes_copied += size;
 507                len -= size;
 508                buf += size;
 509
 510                spin_lock_irqsave(&dev->lock, flags);
 511
 512                /* We've disconnected or reset so return. */
 513                if (dev->reset_printer) {
 514                        list_add(&current_rx_req->list, &dev->rx_reqs);
 515                        spin_unlock_irqrestore(&dev->lock, flags);
 516                        mutex_unlock(&dev->lock_printer_io);
 517                        return -EAGAIN;
 518                }
 519
 520                /* If we not returning all the data left in this RX request
 521                 * buffer then adjust the amount of data left in the buffer.
 522                 * Othewise if we are done with this RX request buffer then
 523                 * requeue it to get any incoming data from the USB host.
 524                 */
 525                if (size < current_rx_bytes) {
 526                        current_rx_bytes -= size;
 527                        current_rx_buf += size;
 528                } else {
 529                        list_add(&current_rx_req->list, &dev->rx_reqs);
 530                        current_rx_bytes = 0;
 531                        current_rx_buf = NULL;
 532                        current_rx_req = NULL;
 533                }
 534        }
 535
 536        dev->current_rx_req = current_rx_req;
 537        dev->current_rx_bytes = current_rx_bytes;
 538        dev->current_rx_buf = current_rx_buf;
 539
 540        spin_unlock_irqrestore(&dev->lock, flags);
 541        mutex_unlock(&dev->lock_printer_io);
 542
 543        DBG(dev, "printer_read returned %d bytes\n", (int)bytes_copied);
 544
 545        if (bytes_copied)
 546                return bytes_copied;
 547        else
 548                return -EAGAIN;
 549}
 550
 551static ssize_t
 552printer_write(struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
 553{
 554        struct printer_dev      *dev = fd->private_data;
 555        unsigned long           flags;
 556        size_t                  size;   /* Amount of data in a TX request. */
 557        size_t                  bytes_copied = 0;
 558        struct usb_request      *req;
 559
 560        DBG(dev, "printer_write trying to send %d bytes\n", (int)len);
 561
 562        if (len == 0)
 563                return -EINVAL;
 564
 565        mutex_lock(&dev->lock_printer_io);
 566        spin_lock_irqsave(&dev->lock, flags);
 567
 568        /* Check if a printer reset happens while we have interrupts on */
 569        dev->reset_printer = 0;
 570
 571        /* Check if there is any available write buffers */
 572        if (likely(list_empty(&dev->tx_reqs))) {
 573                /* Turn interrupts back on before sleeping. */
 574                spin_unlock_irqrestore(&dev->lock, flags);
 575
 576                /*
 577                 * If write buffers are available check if this is
 578                 * a NON-Blocking call or not.
 579                 */
 580                if (fd->f_flags & (O_NONBLOCK|O_NDELAY)) {
 581                        mutex_unlock(&dev->lock_printer_io);
 582                        return -EAGAIN;
 583                }
 584
 585                /* Sleep until a write buffer is available */
 586                wait_event_interruptible(dev->tx_wait,
 587                                (likely(!list_empty(&dev->tx_reqs))));
 588                spin_lock_irqsave(&dev->lock, flags);
 589        }
 590
 591        while (likely(!list_empty(&dev->tx_reqs)) && len) {
 592
 593                if (len > USB_BUFSIZE)
 594                        size = USB_BUFSIZE;
 595                else
 596                        size = len;
 597
 598                req = container_of(dev->tx_reqs.next, struct usb_request,
 599                                list);
 600                list_del_init(&req->list);
 601
 602                req->complete = tx_complete;
 603                req->length = size;
 604
 605                /* Check if we need to send a zero length packet. */
 606                if (len > size)
 607                        /* They will be more TX requests so no yet. */
 608                        req->zero = 0;
 609                else
 610                        /* If the data amount is not a multiple of the
 611                         * maxpacket size then send a zero length packet.
 612                         */
 613                        req->zero = ((len % dev->in_ep->maxpacket) == 0);
 614
 615                /* Don't leave irqs off while doing memory copies */
 616                spin_unlock_irqrestore(&dev->lock, flags);
 617
 618                if (copy_from_user(req->buf, buf, size)) {
 619                        list_add(&req->list, &dev->tx_reqs);
 620                        mutex_unlock(&dev->lock_printer_io);
 621                        return bytes_copied;
 622                }
 623
 624                bytes_copied += size;
 625                len -= size;
 626                buf += size;
 627
 628                spin_lock_irqsave(&dev->lock, flags);
 629
 630                /* We've disconnected or reset so free the req and buffer */
 631                if (dev->reset_printer) {
 632                        list_add(&req->list, &dev->tx_reqs);
 633                        spin_unlock_irqrestore(&dev->lock, flags);
 634                        mutex_unlock(&dev->lock_printer_io);
 635                        return -EAGAIN;
 636                }
 637
 638                if (usb_ep_queue(dev->in_ep, req, GFP_ATOMIC)) {
 639                        list_add(&req->list, &dev->tx_reqs);
 640                        spin_unlock_irqrestore(&dev->lock, flags);
 641                        mutex_unlock(&dev->lock_printer_io);
 642                        return -EAGAIN;
 643                }
 644
 645                list_add(&req->list, &dev->tx_reqs_active);
 646
 647        }
 648
 649        spin_unlock_irqrestore(&dev->lock, flags);
 650        mutex_unlock(&dev->lock_printer_io);
 651
 652        DBG(dev, "printer_write sent %d bytes\n", (int)bytes_copied);
 653
 654        if (bytes_copied)
 655                return bytes_copied;
 656        else
 657                return -EAGAIN;
 658}
 659
 660static int
 661printer_fsync(struct file *fd, loff_t start, loff_t end, int datasync)
 662{
 663        struct printer_dev      *dev = fd->private_data;
 664        struct inode *inode = file_inode(fd);
 665        unsigned long           flags;
 666        int                     tx_list_empty;
 667
 668        inode_lock(inode);
 669        spin_lock_irqsave(&dev->lock, flags);
 670        tx_list_empty = (likely(list_empty(&dev->tx_reqs)));
 671        spin_unlock_irqrestore(&dev->lock, flags);
 672
 673        if (!tx_list_empty) {
 674                /* Sleep until all data has been sent */
 675                wait_event_interruptible(dev->tx_flush_wait,
 676                                (likely(list_empty(&dev->tx_reqs_active))));
 677        }
 678        inode_unlock(inode);
 679
 680        return 0;
 681}
 682
 683static unsigned int
 684printer_poll(struct file *fd, poll_table *wait)
 685{
 686        struct printer_dev      *dev = fd->private_data;
 687        unsigned long           flags;
 688        int                     status = 0;
 689
 690        mutex_lock(&dev->lock_printer_io);
 691        spin_lock_irqsave(&dev->lock, flags);
 692        setup_rx_reqs(dev);
 693        spin_unlock_irqrestore(&dev->lock, flags);
 694        mutex_unlock(&dev->lock_printer_io);
 695
 696        poll_wait(fd, &dev->rx_wait, wait);
 697        poll_wait(fd, &dev->tx_wait, wait);
 698
 699        spin_lock_irqsave(&dev->lock, flags);
 700        if (likely(!list_empty(&dev->tx_reqs)))
 701                status |= POLLOUT | POLLWRNORM;
 702
 703        if (likely(dev->current_rx_bytes) ||
 704                        likely(!list_empty(&dev->rx_buffers)))
 705                status |= POLLIN | POLLRDNORM;
 706
 707        spin_unlock_irqrestore(&dev->lock, flags);
 708
 709        return status;
 710}
 711
 712static long
 713printer_ioctl(struct file *fd, unsigned int code, unsigned long arg)
 714{
 715        struct printer_dev      *dev = fd->private_data;
 716        unsigned long           flags;
 717        int                     status = 0;
 718
 719        DBG(dev, "printer_ioctl: cmd=0x%4.4x, arg=%lu\n", code, arg);
 720
 721        /* handle ioctls */
 722
 723        spin_lock_irqsave(&dev->lock, flags);
 724
 725        switch (code) {
 726        case GADGET_GET_PRINTER_STATUS:
 727                status = (int)dev->printer_status;
 728                break;
 729        case GADGET_SET_PRINTER_STATUS:
 730                dev->printer_status = (u8)arg;
 731                break;
 732        default:
 733                /* could not handle ioctl */
 734                DBG(dev, "printer_ioctl: ERROR cmd=0x%4.4xis not supported\n",
 735                                code);
 736                status = -ENOTTY;
 737        }
 738
 739        spin_unlock_irqrestore(&dev->lock, flags);
 740
 741        return status;
 742}
 743
 744/* used after endpoint configuration */
 745static const struct file_operations printer_io_operations = {
 746        .owner =        THIS_MODULE,
 747        .open =         printer_open,
 748        .read =         printer_read,
 749        .write =        printer_write,
 750        .fsync =        printer_fsync,
 751        .poll =         printer_poll,
 752        .unlocked_ioctl = printer_ioctl,
 753        .release =      printer_close,
 754        .llseek =       noop_llseek,
 755};
 756
 757/*-------------------------------------------------------------------------*/
 758
 759static int
 760set_printer_interface(struct printer_dev *dev)
 761{
 762        int                     result = 0;
 763
 764        dev->in_ep->desc = ep_desc(dev->gadget, &fs_ep_in_desc, &hs_ep_in_desc,
 765                                &ss_ep_in_desc);
 766        dev->in_ep->driver_data = dev;
 767
 768        dev->out_ep->desc = ep_desc(dev->gadget, &fs_ep_out_desc,
 769                                    &hs_ep_out_desc, &ss_ep_out_desc);
 770        dev->out_ep->driver_data = dev;
 771
 772        result = usb_ep_enable(dev->in_ep);
 773        if (result != 0) {
 774                DBG(dev, "enable %s --> %d\n", dev->in_ep->name, result);
 775                goto done;
 776        }
 777
 778        result = usb_ep_enable(dev->out_ep);
 779        if (result != 0) {
 780                DBG(dev, "enable %s --> %d\n", dev->in_ep->name, result);
 781                goto done;
 782        }
 783
 784done:
 785        /* on error, disable any endpoints  */
 786        if (result != 0) {
 787                (void) usb_ep_disable(dev->in_ep);
 788                (void) usb_ep_disable(dev->out_ep);
 789                dev->in_ep->desc = NULL;
 790                dev->out_ep->desc = NULL;
 791        }
 792
 793        /* caller is responsible for cleanup on error */
 794        return result;
 795}
 796
 797static void printer_reset_interface(struct printer_dev *dev)
 798{
 799        unsigned long   flags;
 800
 801        if (dev->interface < 0)
 802                return;
 803
 804        DBG(dev, "%s\n", __func__);
 805
 806        if (dev->in_ep->desc)
 807                usb_ep_disable(dev->in_ep);
 808
 809        if (dev->out_ep->desc)
 810                usb_ep_disable(dev->out_ep);
 811
 812        spin_lock_irqsave(&dev->lock, flags);
 813        dev->in_ep->desc = NULL;
 814        dev->out_ep->desc = NULL;
 815        dev->interface = -1;
 816        spin_unlock_irqrestore(&dev->lock, flags);
 817}
 818
 819/* Change our operational Interface. */
 820static int set_interface(struct printer_dev *dev, unsigned number)
 821{
 822        int                     result = 0;
 823
 824        /* Free the current interface */
 825        printer_reset_interface(dev);
 826
 827        result = set_printer_interface(dev);
 828        if (result)
 829                printer_reset_interface(dev);
 830        else
 831                dev->interface = number;
 832
 833        if (!result)
 834                INFO(dev, "Using interface %x\n", number);
 835
 836        return result;
 837}
 838
 839static void printer_soft_reset(struct printer_dev *dev)
 840{
 841        struct usb_request      *req;
 842
 843        INFO(dev, "Received Printer Reset Request\n");
 844
 845        if (usb_ep_disable(dev->in_ep))
 846                DBG(dev, "Failed to disable USB in_ep\n");
 847        if (usb_ep_disable(dev->out_ep))
 848                DBG(dev, "Failed to disable USB out_ep\n");
 849
 850        if (dev->current_rx_req != NULL) {
 851                list_add(&dev->current_rx_req->list, &dev->rx_reqs);
 852                dev->current_rx_req = NULL;
 853        }
 854        dev->current_rx_bytes = 0;
 855        dev->current_rx_buf = NULL;
 856        dev->reset_printer = 1;
 857
 858        while (likely(!(list_empty(&dev->rx_buffers)))) {
 859                req = container_of(dev->rx_buffers.next, struct usb_request,
 860                                list);
 861                list_del_init(&req->list);
 862                list_add(&req->list, &dev->rx_reqs);
 863        }
 864
 865        while (likely(!(list_empty(&dev->rx_reqs_active)))) {
 866                req = container_of(dev->rx_buffers.next, struct usb_request,
 867                                list);
 868                list_del_init(&req->list);
 869                list_add(&req->list, &dev->rx_reqs);
 870        }
 871
 872        while (likely(!(list_empty(&dev->tx_reqs_active)))) {
 873                req = container_of(dev->tx_reqs_active.next,
 874                                struct usb_request, list);
 875                list_del_init(&req->list);
 876                list_add(&req->list, &dev->tx_reqs);
 877        }
 878
 879        if (usb_ep_enable(dev->in_ep))
 880                DBG(dev, "Failed to enable USB in_ep\n");
 881        if (usb_ep_enable(dev->out_ep))
 882                DBG(dev, "Failed to enable USB out_ep\n");
 883
 884        wake_up_interruptible(&dev->rx_wait);
 885        wake_up_interruptible(&dev->tx_wait);
 886        wake_up_interruptible(&dev->tx_flush_wait);
 887}
 888
 889/*-------------------------------------------------------------------------*/
 890
 891static bool gprinter_req_match(struct usb_function *f,
 892                               const struct usb_ctrlrequest *ctrl,
 893                               bool config0)
 894{
 895        struct printer_dev      *dev = func_to_printer(f);
 896        u16                     w_index = le16_to_cpu(ctrl->wIndex);
 897        u16                     w_value = le16_to_cpu(ctrl->wValue);
 898        u16                     w_length = le16_to_cpu(ctrl->wLength);
 899
 900        if (config0)
 901                return false;
 902
 903        if ((ctrl->bRequestType & USB_RECIP_MASK) != USB_RECIP_INTERFACE ||
 904            (ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS)
 905                return false;
 906
 907        switch (ctrl->bRequest) {
 908        case GET_DEVICE_ID:
 909                w_index >>= 8;
 910                if (w_length <= PNP_STRING_LEN &&
 911                    (USB_DIR_IN & ctrl->bRequestType))
 912                        break;
 913                return false;
 914        case GET_PORT_STATUS:
 915                if (!w_value && w_length == 1 &&
 916                    (USB_DIR_IN & ctrl->bRequestType))
 917                        break;
 918                return false;
 919        case SOFT_RESET:
 920                if (!w_value && !w_length &&
 921                   !(USB_DIR_IN & ctrl->bRequestType))
 922                        break;
 923                /* fall through */
 924        default:
 925                return false;
 926        }
 927        return w_index == dev->interface;
 928}
 929
 930/*
 931 * The setup() callback implements all the ep0 functionality that's not
 932 * handled lower down.
 933 */
 934static int printer_func_setup(struct usb_function *f,
 935                const struct usb_ctrlrequest *ctrl)
 936{
 937        struct printer_dev *dev = func_to_printer(f);
 938        struct usb_composite_dev *cdev = f->config->cdev;
 939        struct usb_request      *req = cdev->req;
 940        int                     value = -EOPNOTSUPP;
 941        u16                     wIndex = le16_to_cpu(ctrl->wIndex);
 942        u16                     wValue = le16_to_cpu(ctrl->wValue);
 943        u16                     wLength = le16_to_cpu(ctrl->wLength);
 944
 945        DBG(dev, "ctrl req%02x.%02x v%04x i%04x l%d\n",
 946                ctrl->bRequestType, ctrl->bRequest, wValue, wIndex, wLength);
 947
 948        switch (ctrl->bRequestType&USB_TYPE_MASK) {
 949        case USB_TYPE_CLASS:
 950                switch (ctrl->bRequest) {
 951                case GET_DEVICE_ID: /* Get the IEEE-1284 PNP String */
 952                        /* Only one printer interface is supported. */
 953                        if ((wIndex>>8) != dev->interface)
 954                                break;
 955
 956                        value = (dev->pnp_string[0] << 8) | dev->pnp_string[1];
 957                        memcpy(req->buf, dev->pnp_string, value);
 958                        DBG(dev, "1284 PNP String: %x %s\n", value,
 959                                        &dev->pnp_string[2]);
 960                        break;
 961
 962                case GET_PORT_STATUS: /* Get Port Status */
 963                        /* Only one printer interface is supported. */
 964                        if (wIndex != dev->interface)
 965                                break;
 966
 967                        *(u8 *)req->buf = dev->printer_status;
 968                        value = min_t(u16, wLength, 1);
 969                        break;
 970
 971                case SOFT_RESET: /* Soft Reset */
 972                        /* Only one printer interface is supported. */
 973                        if (wIndex != dev->interface)
 974                                break;
 975
 976                        printer_soft_reset(dev);
 977
 978                        value = 0;
 979                        break;
 980
 981                default:
 982                        goto unknown;
 983                }
 984                break;
 985
 986        default:
 987unknown:
 988                VDBG(dev,
 989                        "unknown ctrl req%02x.%02x v%04x i%04x l%d\n",
 990                        ctrl->bRequestType, ctrl->bRequest,
 991                        wValue, wIndex, wLength);
 992                break;
 993        }
 994        /* host either stalls (value < 0) or reports success */
 995        if (value >= 0) {
 996                req->length = value;
 997                req->zero = value < wLength;
 998                value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
 999                if (value < 0) {
1000                        ERROR(dev, "%s:%d Error!\n", __func__, __LINE__);
1001                        req->status = 0;
1002                }
1003        }
1004        return value;
1005}
1006
1007static int printer_func_bind(struct usb_configuration *c,
1008                struct usb_function *f)
1009{
1010        struct usb_gadget *gadget = c->cdev->gadget;
1011        struct printer_dev *dev = func_to_printer(f);
1012        struct device *pdev;
1013        struct usb_composite_dev *cdev = c->cdev;
1014        struct usb_ep *in_ep;
1015        struct usb_ep *out_ep = NULL;
1016        struct usb_request *req;
1017        dev_t devt;
1018        int id;
1019        int ret;
1020        u32 i;
1021
1022        id = usb_interface_id(c, f);
1023        if (id < 0)
1024                return id;
1025        intf_desc.bInterfaceNumber = id;
1026
1027        /* finish hookup to lower layer ... */
1028        dev->gadget = gadget;
1029
1030        /* all we really need is bulk IN/OUT */
1031        in_ep = usb_ep_autoconfig(cdev->gadget, &fs_ep_in_desc);
1032        if (!in_ep) {
1033autoconf_fail:
1034                dev_err(&cdev->gadget->dev, "can't autoconfigure on %s\n",
1035                        cdev->gadget->name);
1036                return -ENODEV;
1037        }
1038
1039        out_ep = usb_ep_autoconfig(cdev->gadget, &fs_ep_out_desc);
1040        if (!out_ep)
1041                goto autoconf_fail;
1042
1043        /* assumes that all endpoints are dual-speed */
1044        hs_ep_in_desc.bEndpointAddress = fs_ep_in_desc.bEndpointAddress;
1045        hs_ep_out_desc.bEndpointAddress = fs_ep_out_desc.bEndpointAddress;
1046        ss_ep_in_desc.bEndpointAddress = fs_ep_in_desc.bEndpointAddress;
1047        ss_ep_out_desc.bEndpointAddress = fs_ep_out_desc.bEndpointAddress;
1048
1049        ret = usb_assign_descriptors(f, fs_printer_function,
1050                        hs_printer_function, ss_printer_function, NULL);
1051        if (ret)
1052                return ret;
1053
1054        dev->in_ep = in_ep;
1055        dev->out_ep = out_ep;
1056
1057        ret = -ENOMEM;
1058        for (i = 0; i < dev->q_len; i++) {
1059                req = printer_req_alloc(dev->in_ep, USB_BUFSIZE, GFP_KERNEL);
1060                if (!req)
1061                        goto fail_tx_reqs;
1062                list_add(&req->list, &dev->tx_reqs);
1063        }
1064
1065        for (i = 0; i < dev->q_len; i++) {
1066                req = printer_req_alloc(dev->out_ep, USB_BUFSIZE, GFP_KERNEL);
1067                if (!req)
1068                        goto fail_rx_reqs;
1069                list_add(&req->list, &dev->rx_reqs);
1070        }
1071
1072        /* Setup the sysfs files for the printer gadget. */
1073        devt = MKDEV(major, dev->minor);
1074        pdev = device_create(usb_gadget_class, NULL, devt,
1075                                  NULL, "g_printer%d", dev->minor);
1076        if (IS_ERR(pdev)) {
1077                ERROR(dev, "Failed to create device: g_printer\n");
1078                ret = PTR_ERR(pdev);
1079                goto fail_rx_reqs;
1080        }
1081
1082        /*
1083         * Register a character device as an interface to a user mode
1084         * program that handles the printer specific functionality.
1085         */
1086        cdev_init(&dev->printer_cdev, &printer_io_operations);
1087        dev->printer_cdev.owner = THIS_MODULE;
1088        ret = cdev_add(&dev->printer_cdev, devt, 1);
1089        if (ret) {
1090                ERROR(dev, "Failed to open char device\n");
1091                goto fail_cdev_add;
1092        }
1093
1094        return 0;
1095
1096fail_cdev_add:
1097        device_destroy(usb_gadget_class, devt);
1098
1099fail_rx_reqs:
1100        while (!list_empty(&dev->rx_reqs)) {
1101                req = container_of(dev->rx_reqs.next, struct usb_request, list);
1102                list_del(&req->list);
1103                printer_req_free(dev->out_ep, req);
1104        }
1105
1106fail_tx_reqs:
1107        while (!list_empty(&dev->tx_reqs)) {
1108                req = container_of(dev->tx_reqs.next, struct usb_request, list);
1109                list_del(&req->list);
1110                printer_req_free(dev->in_ep, req);
1111        }
1112
1113        return ret;
1114
1115}
1116
1117static int printer_func_set_alt(struct usb_function *f,
1118                unsigned intf, unsigned alt)
1119{
1120        struct printer_dev *dev = func_to_printer(f);
1121        int ret = -ENOTSUPP;
1122
1123        if (!alt)
1124                ret = set_interface(dev, intf);
1125
1126        return ret;
1127}
1128
1129static void printer_func_disable(struct usb_function *f)
1130{
1131        struct printer_dev *dev = func_to_printer(f);
1132
1133        DBG(dev, "%s\n", __func__);
1134
1135        printer_reset_interface(dev);
1136}
1137
1138static inline struct f_printer_opts
1139*to_f_printer_opts(struct config_item *item)
1140{
1141        return container_of(to_config_group(item), struct f_printer_opts,
1142                            func_inst.group);
1143}
1144
1145static void printer_attr_release(struct config_item *item)
1146{
1147        struct f_printer_opts *opts = to_f_printer_opts(item);
1148
1149        usb_put_function_instance(&opts->func_inst);
1150}
1151
1152static struct configfs_item_operations printer_item_ops = {
1153        .release        = printer_attr_release,
1154};
1155
1156static ssize_t f_printer_opts_pnp_string_show(struct config_item *item,
1157                                              char *page)
1158{
1159        struct f_printer_opts *opts = to_f_printer_opts(item);
1160        int result;
1161
1162        mutex_lock(&opts->lock);
1163        result = strlcpy(page, opts->pnp_string + 2, PNP_STRING_LEN - 2);
1164        mutex_unlock(&opts->lock);
1165
1166        return result;
1167}
1168
1169static ssize_t f_printer_opts_pnp_string_store(struct config_item *item,
1170                                               const char *page, size_t len)
1171{
1172        struct f_printer_opts *opts = to_f_printer_opts(item);
1173        int result, l;
1174
1175        mutex_lock(&opts->lock);
1176        result = strlcpy(opts->pnp_string + 2, page, PNP_STRING_LEN - 2);
1177        l = strlen(opts->pnp_string + 2) + 2;
1178        opts->pnp_string[0] = (l >> 8) & 0xFF;
1179        opts->pnp_string[1] = l & 0xFF;
1180        mutex_unlock(&opts->lock);
1181
1182        return result;
1183}
1184
1185CONFIGFS_ATTR(f_printer_opts_, pnp_string);
1186
1187static ssize_t f_printer_opts_q_len_show(struct config_item *item,
1188                                         char *page)
1189{
1190        struct f_printer_opts *opts = to_f_printer_opts(item);
1191        int result;
1192
1193        mutex_lock(&opts->lock);
1194        result = sprintf(page, "%d\n", opts->q_len);
1195        mutex_unlock(&opts->lock);
1196
1197        return result;
1198}
1199
1200static ssize_t f_printer_opts_q_len_store(struct config_item *item,
1201                                          const char *page, size_t len)
1202{
1203        struct f_printer_opts *opts = to_f_printer_opts(item);
1204        int ret;
1205        u16 num;
1206
1207        mutex_lock(&opts->lock);
1208        if (opts->refcnt) {
1209                ret = -EBUSY;
1210                goto end;
1211        }
1212
1213        ret = kstrtou16(page, 0, &num);
1214        if (ret)
1215                goto end;
1216
1217        opts->q_len = (unsigned)num;
1218        ret = len;
1219end:
1220        mutex_unlock(&opts->lock);
1221        return ret;
1222}
1223
1224CONFIGFS_ATTR(f_printer_opts_, q_len);
1225
1226static struct configfs_attribute *printer_attrs[] = {
1227        &f_printer_opts_attr_pnp_string,
1228        &f_printer_opts_attr_q_len,
1229        NULL,
1230};
1231
1232static struct config_item_type printer_func_type = {
1233        .ct_item_ops    = &printer_item_ops,
1234        .ct_attrs       = printer_attrs,
1235        .ct_owner       = THIS_MODULE,
1236};
1237
1238static inline int gprinter_get_minor(void)
1239{
1240        int ret;
1241
1242        ret = ida_simple_get(&printer_ida, 0, 0, GFP_KERNEL);
1243        if (ret >= PRINTER_MINORS) {
1244                ida_simple_remove(&printer_ida, ret);
1245                ret = -ENODEV;
1246        }
1247
1248        return ret;
1249}
1250
1251static inline void gprinter_put_minor(int minor)
1252{
1253        ida_simple_remove(&printer_ida, minor);
1254}
1255
1256static int gprinter_setup(int);
1257static void gprinter_cleanup(void);
1258
1259static void gprinter_free_inst(struct usb_function_instance *f)
1260{
1261        struct f_printer_opts *opts;
1262
1263        opts = container_of(f, struct f_printer_opts, func_inst);
1264
1265        mutex_lock(&printer_ida_lock);
1266
1267        gprinter_put_minor(opts->minor);
1268        if (ida_is_empty(&printer_ida))
1269                gprinter_cleanup();
1270
1271        mutex_unlock(&printer_ida_lock);
1272
1273        kfree(opts);
1274}
1275
1276static struct usb_function_instance *gprinter_alloc_inst(void)
1277{
1278        struct f_printer_opts *opts;
1279        struct usb_function_instance *ret;
1280        int status = 0;
1281
1282        opts = kzalloc(sizeof(*opts), GFP_KERNEL);
1283        if (!opts)
1284                return ERR_PTR(-ENOMEM);
1285
1286        mutex_init(&opts->lock);
1287        opts->func_inst.free_func_inst = gprinter_free_inst;
1288        ret = &opts->func_inst;
1289
1290        mutex_lock(&printer_ida_lock);
1291
1292        if (ida_is_empty(&printer_ida)) {
1293                status = gprinter_setup(PRINTER_MINORS);
1294                if (status) {
1295                        ret = ERR_PTR(status);
1296                        kfree(opts);
1297                        goto unlock;
1298                }
1299        }
1300
1301        opts->minor = gprinter_get_minor();
1302        if (opts->minor < 0) {
1303                ret = ERR_PTR(opts->minor);
1304                kfree(opts);
1305                if (ida_is_empty(&printer_ida))
1306                        gprinter_cleanup();
1307                goto unlock;
1308        }
1309        config_group_init_type_name(&opts->func_inst.group, "",
1310                                    &printer_func_type);
1311
1312unlock:
1313        mutex_unlock(&printer_ida_lock);
1314        return ret;
1315}
1316
1317static void gprinter_free(struct usb_function *f)
1318{
1319        struct printer_dev *dev = func_to_printer(f);
1320        struct f_printer_opts *opts;
1321
1322        opts = container_of(f->fi, struct f_printer_opts, func_inst);
1323        kfree(dev);
1324        mutex_lock(&opts->lock);
1325        --opts->refcnt;
1326        mutex_unlock(&opts->lock);
1327}
1328
1329static void printer_func_unbind(struct usb_configuration *c,
1330                struct usb_function *f)
1331{
1332        struct printer_dev      *dev;
1333        struct usb_request      *req;
1334
1335        dev = func_to_printer(f);
1336
1337        device_destroy(usb_gadget_class, MKDEV(major, dev->minor));
1338
1339        /* Remove Character Device */
1340        cdev_del(&dev->printer_cdev);
1341
1342        /* we must already have been disconnected ... no i/o may be active */
1343        WARN_ON(!list_empty(&dev->tx_reqs_active));
1344        WARN_ON(!list_empty(&dev->rx_reqs_active));
1345
1346        /* Free all memory for this driver. */
1347        while (!list_empty(&dev->tx_reqs)) {
1348                req = container_of(dev->tx_reqs.next, struct usb_request,
1349                                list);
1350                list_del(&req->list);
1351                printer_req_free(dev->in_ep, req);
1352        }
1353
1354        if (dev->current_rx_req != NULL)
1355                printer_req_free(dev->out_ep, dev->current_rx_req);
1356
1357        while (!list_empty(&dev->rx_reqs)) {
1358                req = container_of(dev->rx_reqs.next,
1359                                struct usb_request, list);
1360                list_del(&req->list);
1361                printer_req_free(dev->out_ep, req);
1362        }
1363
1364        while (!list_empty(&dev->rx_buffers)) {
1365                req = container_of(dev->rx_buffers.next,
1366                                struct usb_request, list);
1367                list_del(&req->list);
1368                printer_req_free(dev->out_ep, req);
1369        }
1370        usb_free_all_descriptors(f);
1371}
1372
1373static struct usb_function *gprinter_alloc(struct usb_function_instance *fi)
1374{
1375        struct printer_dev      *dev;
1376        struct f_printer_opts   *opts;
1377
1378        opts = container_of(fi, struct f_printer_opts, func_inst);
1379
1380        mutex_lock(&opts->lock);
1381        if (opts->minor >= minors) {
1382                mutex_unlock(&opts->lock);
1383                return ERR_PTR(-ENOENT);
1384        }
1385
1386        dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1387        if (!dev) {
1388                mutex_unlock(&opts->lock);
1389                return ERR_PTR(-ENOMEM);
1390        }
1391
1392        ++opts->refcnt;
1393        dev->minor = opts->minor;
1394        dev->pnp_string = opts->pnp_string;
1395        dev->q_len = opts->q_len;
1396        mutex_unlock(&opts->lock);
1397
1398        dev->function.name = "printer";
1399        dev->function.bind = printer_func_bind;
1400        dev->function.setup = printer_func_setup;
1401        dev->function.unbind = printer_func_unbind;
1402        dev->function.set_alt = printer_func_set_alt;
1403        dev->function.disable = printer_func_disable;
1404        dev->function.req_match = gprinter_req_match;
1405        dev->function.free_func = gprinter_free;
1406
1407        INIT_LIST_HEAD(&dev->tx_reqs);
1408        INIT_LIST_HEAD(&dev->rx_reqs);
1409        INIT_LIST_HEAD(&dev->rx_buffers);
1410        INIT_LIST_HEAD(&dev->tx_reqs_active);
1411        INIT_LIST_HEAD(&dev->rx_reqs_active);
1412
1413        spin_lock_init(&dev->lock);
1414        mutex_init(&dev->lock_printer_io);
1415        init_waitqueue_head(&dev->rx_wait);
1416        init_waitqueue_head(&dev->tx_wait);
1417        init_waitqueue_head(&dev->tx_flush_wait);
1418
1419        dev->interface = -1;
1420        dev->printer_cdev_open = 0;
1421        dev->printer_status = PRINTER_NOT_ERROR;
1422        dev->current_rx_req = NULL;
1423        dev->current_rx_bytes = 0;
1424        dev->current_rx_buf = NULL;
1425
1426        return &dev->function;
1427}
1428
1429DECLARE_USB_FUNCTION_INIT(printer, gprinter_alloc_inst, gprinter_alloc);
1430MODULE_LICENSE("GPL");
1431MODULE_AUTHOR("Craig Nadler");
1432
1433static int gprinter_setup(int count)
1434{
1435        int status;
1436        dev_t devt;
1437
1438        usb_gadget_class = class_create(THIS_MODULE, "usb_printer_gadget");
1439        if (IS_ERR(usb_gadget_class)) {
1440                status = PTR_ERR(usb_gadget_class);
1441                usb_gadget_class = NULL;
1442                pr_err("unable to create usb_gadget class %d\n", status);
1443                return status;
1444        }
1445
1446        status = alloc_chrdev_region(&devt, 0, count, "USB printer gadget");
1447        if (status) {
1448                pr_err("alloc_chrdev_region %d\n", status);
1449                class_destroy(usb_gadget_class);
1450                usb_gadget_class = NULL;
1451                return status;
1452        }
1453
1454        major = MAJOR(devt);
1455        minors = count;
1456
1457        return status;
1458}
1459
1460static void gprinter_cleanup(void)
1461{
1462        if (major) {
1463                unregister_chrdev_region(MKDEV(major, 0), minors);
1464                major = minors = 0;
1465        }
1466        class_destroy(usb_gadget_class);
1467        usb_gadget_class = NULL;
1468}
1469