1/* 2 * printer.c -- Printer gadget driver 3 * 4 * Copyright (C) 2003-2005 David Brownell 5 * Copyright (C) 2006 Craig W. Nadler 6 * 7 * This program is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation; either version 2 of the License, or 10 * (at your option) any later version. 11 * 12 * This program is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with this program; if not, write to the Free Software 19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 20 */ 21 22#include <linux/module.h> 23#include <linux/kernel.h> 24#include <linux/delay.h> 25#include <linux/ioport.h> 26#include <linux/sched.h> 27#include <linux/slab.h> 28#include <linux/mutex.h> 29#include <linux/errno.h> 30#include <linux/init.h> 31#include <linux/timer.h> 32#include <linux/list.h> 33#include <linux/interrupt.h> 34#include <linux/utsname.h> 35#include <linux/device.h> 36#include <linux/moduleparam.h> 37#include <linux/fs.h> 38#include <linux/poll.h> 39#include <linux/types.h> 40#include <linux/ctype.h> 41#include <linux/cdev.h> 42 43#include <asm/byteorder.h> 44#include <linux/io.h> 45#include <linux/irq.h> 46#include <asm/system.h> 47#include <linux/uaccess.h> 48#include <asm/unaligned.h> 49 50#include <linux/usb/ch9.h> 51#include <linux/usb/gadget.h> 52#include <linux/usb/g_printer.h> 53 54#include "gadget_chips.h" 55 56 57/* 58 * Kbuild is not very cooperative with respect to linking separately 59 * compiled library objects into one module. So for now we won't use 60 * separate compilation ... ensuring init/exit sections work to shrink 61 * the runtime footprint, and giving us at least some parts of what 62 * a "gcc --combine ... part1.c part2.c part3.c ... " build would. 63 */ 64#include "usbstring.c" 65#include "config.c" 66#include "epautoconf.c" 67 68/*-------------------------------------------------------------------------*/ 69 70#define DRIVER_DESC "Printer Gadget" 71#define DRIVER_VERSION "2007 OCT 06" 72 73static DEFINE_MUTEX(printer_mutex); 74static const char shortname [] = "printer"; 75static const char driver_desc [] = DRIVER_DESC; 76 77static dev_t g_printer_devno; 78 79static struct class *usb_gadget_class; 80 81/*-------------------------------------------------------------------------*/ 82 83struct printer_dev { 84 spinlock_t lock; /* lock this structure */ 85 /* lock buffer lists during read/write calls */ 86 struct mutex lock_printer_io; 87 struct usb_gadget *gadget; 88 struct usb_request *req; /* for control responses */ 89 u8 config; 90 s8 interface; 91 struct usb_ep *in_ep, *out_ep; 92 const struct usb_endpoint_descriptor 93 *in, *out; 94 struct list_head rx_reqs; /* List of free RX structs */ 95 struct list_head rx_reqs_active; /* List of Active RX xfers */ 96 struct list_head rx_buffers; /* List of completed xfers */ 97 /* wait until there is data to be read. */ 98 wait_queue_head_t rx_wait; 99 struct list_head tx_reqs; /* List of free TX structs */ 100 struct list_head tx_reqs_active; /* List of Active TX xfers */ 101 /* Wait until there are write buffers available to use. */ 102 wait_queue_head_t tx_wait; 103 /* Wait until all write buffers have been sent. */ 104 wait_queue_head_t tx_flush_wait; 105 struct usb_request *current_rx_req; 106 size_t current_rx_bytes; 107 u8 *current_rx_buf; 108 u8 printer_status; 109 u8 reset_printer; 110 struct cdev printer_cdev; 111 struct device *pdev; 112 u8 printer_cdev_open; 113 wait_queue_head_t wait; 114}; 115 116static struct printer_dev usb_printer_gadget; 117 118/*-------------------------------------------------------------------------*/ 119 120/* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!! 121 * Instead: allocate your own, using normal USB-IF procedures. 122 */ 123 124/* Thanks to NetChip Technologies for donating this product ID. 125 */ 126#define PRINTER_VENDOR_NUM 0x0525 /* NetChip */ 127#define PRINTER_PRODUCT_NUM 0xa4a8 /* Linux-USB Printer Gadget */ 128 129/* Some systems will want different product identifers published in the 130 * device descriptor, either numbers or strings or both. These string 131 * parameters are in UTF-8 (superset of ASCII's 7 bit characters). 132 */ 133 134static ushort idVendor; 135module_param(idVendor, ushort, S_IRUGO); 136MODULE_PARM_DESC(idVendor, "USB Vendor ID"); 137 138static ushort idProduct; 139module_param(idProduct, ushort, S_IRUGO); 140MODULE_PARM_DESC(idProduct, "USB Product ID"); 141 142static ushort bcdDevice; 143module_param(bcdDevice, ushort, S_IRUGO); 144MODULE_PARM_DESC(bcdDevice, "USB Device version (BCD)"); 145 146static char *iManufacturer; 147module_param(iManufacturer, charp, S_IRUGO); 148MODULE_PARM_DESC(iManufacturer, "USB Manufacturer string"); 149 150static char *iProduct; 151module_param(iProduct, charp, S_IRUGO); 152MODULE_PARM_DESC(iProduct, "USB Product string"); 153 154static char *iSerialNum; 155module_param(iSerialNum, charp, S_IRUGO); 156MODULE_PARM_DESC(iSerialNum, "1"); 157 158static char *iPNPstring; 159module_param(iPNPstring, charp, S_IRUGO); 160MODULE_PARM_DESC(iPNPstring, "MFG:linux;MDL:g_printer;CLS:PRINTER;SN:1;"); 161 162/* Number of requests to allocate per endpoint, not used for ep0. */ 163static unsigned qlen = 10; 164module_param(qlen, uint, S_IRUGO|S_IWUSR); 165 166#define QLEN qlen 167 168#ifdef CONFIG_USB_GADGET_DUALSPEED 169#define DEVSPEED USB_SPEED_HIGH 170#else /* full speed (low speed doesn't do bulk) */ 171#define DEVSPEED USB_SPEED_FULL 172#endif 173 174/*-------------------------------------------------------------------------*/ 175 176#define xprintk(d, level, fmt, args...) \ 177 printk(level "%s: " fmt, DRIVER_DESC, ## args) 178 179#ifdef DEBUG 180#define DBG(dev, fmt, args...) \ 181 xprintk(dev, KERN_DEBUG, fmt, ## args) 182#else 183#define DBG(dev, fmt, args...) \ 184 do { } while (0) 185#endif /* DEBUG */ 186 187#ifdef VERBOSE 188#define VDBG(dev, fmt, args...) \ 189 xprintk(dev, KERN_DEBUG, fmt, ## args) 190#else 191#define VDBG(dev, fmt, args...) \ 192 do { } while (0) 193#endif /* VERBOSE */ 194 195#define ERROR(dev, fmt, args...) \ 196 xprintk(dev, KERN_ERR, fmt, ## args) 197#define WARNING(dev, fmt, args...) \ 198 xprintk(dev, KERN_WARNING, fmt, ## args) 199#define INFO(dev, fmt, args...) \ 200 xprintk(dev, KERN_INFO, fmt, ## args) 201 202/*-------------------------------------------------------------------------*/ 203 204/* USB DRIVER HOOKUP (to the hardware driver, below us), mostly 205 * ep0 implementation: descriptors, config management, setup(). 206 * also optional class-specific notification interrupt transfer. 207 */ 208 209/* 210 * DESCRIPTORS ... most are static, but strings and (full) configuration 211 * descriptors are built on demand. 212 */ 213 214#define STRING_MANUFACTURER 1 215#define STRING_PRODUCT 2 216#define STRING_SERIALNUM 3 217 218/* holds our biggest descriptor */ 219#define USB_DESC_BUFSIZE 256 220#define USB_BUFSIZE 8192 221 222/* This device advertises one configuration. */ 223#define DEV_CONFIG_VALUE 1 224#define PRINTER_INTERFACE 0 225 226static struct usb_device_descriptor device_desc = { 227 .bLength = sizeof device_desc, 228 .bDescriptorType = USB_DT_DEVICE, 229 .bcdUSB = cpu_to_le16(0x0200), 230 .bDeviceClass = USB_CLASS_PER_INTERFACE, 231 .bDeviceSubClass = 0, 232 .bDeviceProtocol = 0, 233 .idVendor = cpu_to_le16(PRINTER_VENDOR_NUM), 234 .idProduct = cpu_to_le16(PRINTER_PRODUCT_NUM), 235 .iManufacturer = STRING_MANUFACTURER, 236 .iProduct = STRING_PRODUCT, 237 .iSerialNumber = STRING_SERIALNUM, 238 .bNumConfigurations = 1 239}; 240 241static struct usb_otg_descriptor otg_desc = { 242 .bLength = sizeof otg_desc, 243 .bDescriptorType = USB_DT_OTG, 244 .bmAttributes = USB_OTG_SRP 245}; 246 247static struct usb_config_descriptor config_desc = { 248 .bLength = sizeof config_desc, 249 .bDescriptorType = USB_DT_CONFIG, 250 251 /* compute wTotalLength on the fly */ 252 .bNumInterfaces = 1, 253 .bConfigurationValue = DEV_CONFIG_VALUE, 254 .iConfiguration = 0, 255 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER, 256 .bMaxPower = CONFIG_USB_GADGET_VBUS_DRAW / 2, 257}; 258 259static struct usb_interface_descriptor intf_desc = { 260 .bLength = sizeof intf_desc, 261 .bDescriptorType = USB_DT_INTERFACE, 262 .bInterfaceNumber = PRINTER_INTERFACE, 263 .bNumEndpoints = 2, 264 .bInterfaceClass = USB_CLASS_PRINTER, 265 .bInterfaceSubClass = 1, /* Printer Sub-Class */ 266 .bInterfaceProtocol = 2, /* Bi-Directional */ 267 .iInterface = 0 268}; 269 270static struct usb_endpoint_descriptor fs_ep_in_desc = { 271 .bLength = USB_DT_ENDPOINT_SIZE, 272 .bDescriptorType = USB_DT_ENDPOINT, 273 .bEndpointAddress = USB_DIR_IN, 274 .bmAttributes = USB_ENDPOINT_XFER_BULK 275}; 276 277static struct usb_endpoint_descriptor fs_ep_out_desc = { 278 .bLength = USB_DT_ENDPOINT_SIZE, 279 .bDescriptorType = USB_DT_ENDPOINT, 280 .bEndpointAddress = USB_DIR_OUT, 281 .bmAttributes = USB_ENDPOINT_XFER_BULK 282}; 283 284static const struct usb_descriptor_header *fs_printer_function [11] = { 285 (struct usb_descriptor_header *) &otg_desc, 286 (struct usb_descriptor_header *) &intf_desc, 287 (struct usb_descriptor_header *) &fs_ep_in_desc, 288 (struct usb_descriptor_header *) &fs_ep_out_desc, 289 NULL 290}; 291 292#ifdef CONFIG_USB_GADGET_DUALSPEED 293 294/* 295 * usb 2.0 devices need to expose both high speed and full speed 296 * descriptors, unless they only run at full speed. 297 */ 298 299static struct usb_endpoint_descriptor hs_ep_in_desc = { 300 .bLength = USB_DT_ENDPOINT_SIZE, 301 .bDescriptorType = USB_DT_ENDPOINT, 302 .bmAttributes = USB_ENDPOINT_XFER_BULK, 303 .wMaxPacketSize = cpu_to_le16(512) 304}; 305 306static struct usb_endpoint_descriptor hs_ep_out_desc = { 307 .bLength = USB_DT_ENDPOINT_SIZE, 308 .bDescriptorType = USB_DT_ENDPOINT, 309 .bmAttributes = USB_ENDPOINT_XFER_BULK, 310 .wMaxPacketSize = cpu_to_le16(512) 311}; 312 313static struct usb_qualifier_descriptor dev_qualifier = { 314 .bLength = sizeof dev_qualifier, 315 .bDescriptorType = USB_DT_DEVICE_QUALIFIER, 316 .bcdUSB = cpu_to_le16(0x0200), 317 .bDeviceClass = USB_CLASS_PRINTER, 318 .bNumConfigurations = 1 319}; 320 321static const struct usb_descriptor_header *hs_printer_function [11] = { 322 (struct usb_descriptor_header *) &otg_desc, 323 (struct usb_descriptor_header *) &intf_desc, 324 (struct usb_descriptor_header *) &hs_ep_in_desc, 325 (struct usb_descriptor_header *) &hs_ep_out_desc, 326 NULL 327}; 328 329/* maxpacket and other transfer characteristics vary by speed. */ 330#define ep_desc(g, hs, fs) (((g)->speed == USB_SPEED_HIGH)?(hs):(fs)) 331 332#else 333 334/* if there's no high speed support, maxpacket doesn't change. */ 335#define ep_desc(g, hs, fs) (((void)(g)), (fs)) 336 337#endif /* !CONFIG_USB_GADGET_DUALSPEED */ 338 339/*-------------------------------------------------------------------------*/ 340 341/* descriptors that are built on-demand */ 342 343static char manufacturer [50]; 344static char product_desc [40] = DRIVER_DESC; 345static char serial_num [40] = "1"; 346static char pnp_string [1024] = 347 "XXMFG:linux;MDL:g_printer;CLS:PRINTER;SN:1;"; 348 349/* static strings, in UTF-8 */ 350static struct usb_string strings [] = { 351 { STRING_MANUFACTURER, manufacturer, }, 352 { STRING_PRODUCT, product_desc, }, 353 { STRING_SERIALNUM, serial_num, }, 354 { } /* end of list */ 355}; 356 357static struct usb_gadget_strings stringtab = { 358 .language = 0x0409, /* en-us */ 359 .strings = strings, 360}; 361 362/*-------------------------------------------------------------------------*/ 363 364static struct usb_request * 365printer_req_alloc(struct usb_ep *ep, unsigned len, gfp_t gfp_flags) 366{ 367 struct usb_request *req; 368 369 req = usb_ep_alloc_request(ep, gfp_flags); 370 371 if (req != NULL) { 372 req->length = len; 373 req->buf = kmalloc(len, gfp_flags); 374 if (req->buf == NULL) { 375 usb_ep_free_request(ep, req); 376 return NULL; 377 } 378 } 379 380 return req; 381} 382 383static void 384printer_req_free(struct usb_ep *ep, struct usb_request *req) 385{ 386 if (ep != NULL && req != NULL) { 387 kfree(req->buf); 388 usb_ep_free_request(ep, req); 389 } 390} 391 392/*-------------------------------------------------------------------------*/ 393 394static void rx_complete(struct usb_ep *ep, struct usb_request *req) 395{ 396 struct printer_dev *dev = ep->driver_data; 397 int status = req->status; 398 unsigned long flags; 399 400 spin_lock_irqsave(&dev->lock, flags); 401 402 list_del_init(&req->list); /* Remode from Active List */ 403 404 switch (status) { 405 406 /* normal completion */ 407 case 0: 408 if (req->actual > 0) { 409 list_add_tail(&req->list, &dev->rx_buffers); 410 DBG(dev, "G_Printer : rx length %d\n", req->actual); 411 } else { 412 list_add(&req->list, &dev->rx_reqs); 413 } 414 break; 415 416 /* software-driven interface shutdown */ 417 case -ECONNRESET: /* unlink */ 418 case -ESHUTDOWN: /* disconnect etc */ 419 VDBG(dev, "rx shutdown, code %d\n", status); 420 list_add(&req->list, &dev->rx_reqs); 421 break; 422 423 /* for hardware automagic (such as pxa) */ 424 case -ECONNABORTED: /* endpoint reset */ 425 DBG(dev, "rx %s reset\n", ep->name); 426 list_add(&req->list, &dev->rx_reqs); 427 break; 428 429 /* data overrun */ 430 case -EOVERFLOW: 431 /* FALLTHROUGH */ 432 433 default: 434 DBG(dev, "rx status %d\n", status); 435 list_add(&req->list, &dev->rx_reqs); 436 break; 437 } 438 439 wake_up_interruptible(&dev->rx_wait); 440 spin_unlock_irqrestore(&dev->lock, flags); 441} 442 443static void tx_complete(struct usb_ep *ep, struct usb_request *req) 444{ 445 struct printer_dev *dev = ep->driver_data; 446 447 switch (req->status) { 448 default: 449 VDBG(dev, "tx err %d\n", req->status); 450 /* FALLTHROUGH */ 451 case -ECONNRESET: /* unlink */ 452 case -ESHUTDOWN: /* disconnect etc */ 453 break; 454 case 0: 455 break; 456 } 457 458 spin_lock(&dev->lock); 459 /* Take the request struct off the active list and put it on the 460 * free list. 461 */ 462 list_del_init(&req->list); 463 list_add(&req->list, &dev->tx_reqs); 464 wake_up_interruptible(&dev->tx_wait); 465 if (likely(list_empty(&dev->tx_reqs_active))) 466 wake_up_interruptible(&dev->tx_flush_wait); 467 468 spin_unlock(&dev->lock); 469} 470 471/*-------------------------------------------------------------------------*/ 472 473static int 474printer_open(struct inode *inode, struct file *fd) 475{ 476 struct printer_dev *dev; 477 unsigned long flags; 478 int ret = -EBUSY; 479 480 mutex_lock(&printer_mutex); 481 dev = container_of(inode->i_cdev, struct printer_dev, printer_cdev); 482 483 spin_lock_irqsave(&dev->lock, flags); 484 485 if (!dev->printer_cdev_open) { 486 dev->printer_cdev_open = 1; 487 fd->private_data = dev; 488 ret = 0; 489 /* Change the printer status to show that it's on-line. */ 490 dev->printer_status |= PRINTER_SELECTED; 491 } 492 493 spin_unlock_irqrestore(&dev->lock, flags); 494 495 DBG(dev, "printer_open returned %x\n", ret); 496 mutex_unlock(&printer_mutex); 497 return ret; 498} 499 500static int 501printer_close(struct inode *inode, struct file *fd) 502{ 503 struct printer_dev *dev = fd->private_data; 504 unsigned long flags; 505 506 spin_lock_irqsave(&dev->lock, flags); 507 dev->printer_cdev_open = 0; 508 fd->private_data = NULL; 509 /* Change printer status to show that the printer is off-line. */ 510 dev->printer_status &= ~PRINTER_SELECTED; 511 spin_unlock_irqrestore(&dev->lock, flags); 512 513 DBG(dev, "printer_close\n"); 514 515 return 0; 516} 517 518/* This function must be called with interrupts turned off. */ 519static void 520setup_rx_reqs(struct printer_dev *dev) 521{ 522 struct usb_request *req; 523 524 while (likely(!list_empty(&dev->rx_reqs))) { 525 int error; 526 527 req = container_of(dev->rx_reqs.next, 528 struct usb_request, list); 529 list_del_init(&req->list); 530 531 /* The USB Host sends us whatever amount of data it wants to 532 * so we always set the length field to the full USB_BUFSIZE. 533 * If the amount of data is more than the read() caller asked 534 * for it will be stored in the request buffer until it is 535 * asked for by read(). 536 */ 537 req->length = USB_BUFSIZE; 538 req->complete = rx_complete; 539 540 error = usb_ep_queue(dev->out_ep, req, GFP_ATOMIC); 541 if (error) { 542 DBG(dev, "rx submit --> %d\n", error); 543 list_add(&req->list, &dev->rx_reqs); 544 break; 545 } else { 546 list_add(&req->list, &dev->rx_reqs_active); 547 } 548 } 549} 550 551static ssize_t 552printer_read(struct file *fd, 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; 557 size_t bytes_copied; 558 struct usb_request *req; 559 /* This is a pointer to the current USB rx request. */ 560 struct usb_request *current_rx_req; 561 /* This is the number of bytes in the current rx buffer. */ 562 size_t current_rx_bytes; 563 /* This is a pointer to the current rx buffer. */ 564 u8 *current_rx_buf; 565 566 if (len == 0) 567 return -EINVAL; 568 569 DBG(dev, "printer_read trying to read %d bytes\n", (int)len); 570 571 mutex_lock(&dev->lock_printer_io); 572 spin_lock_irqsave(&dev->lock, flags); 573 574 /* We will use this flag later to check if a printer reset happened 575 * after we turn interrupts back on. 576 */ 577 dev->reset_printer = 0; 578 579 setup_rx_reqs(dev); 580 581 bytes_copied = 0; 582 current_rx_req = dev->current_rx_req; 583 current_rx_bytes = dev->current_rx_bytes; 584 current_rx_buf = dev->current_rx_buf; 585 dev->current_rx_req = NULL; 586 dev->current_rx_bytes = 0; 587 dev->current_rx_buf = NULL; 588 589 /* Check if there is any data in the read buffers. Please note that 590 * current_rx_bytes is the number of bytes in the current rx buffer. 591 * If it is zero then check if there are any other rx_buffers that 592 * are on the completed list. We are only out of data if all rx 593 * buffers are empty. 594 */ 595 if ((current_rx_bytes == 0) && 596 (likely(list_empty(&dev->rx_buffers)))) { 597 /* Turn interrupts back on before sleeping. */ 598 spin_unlock_irqrestore(&dev->lock, flags); 599 600 /* 601 * If no data is available check if this is a NON-Blocking 602 * call or not. 603 */ 604 if (fd->f_flags & (O_NONBLOCK|O_NDELAY)) { 605 mutex_unlock(&dev->lock_printer_io); 606 return -EAGAIN; 607 } 608 609 /* Sleep until data is available */ 610 wait_event_interruptible(dev->rx_wait, 611 (likely(!list_empty(&dev->rx_buffers)))); 612 spin_lock_irqsave(&dev->lock, flags); 613 } 614 615 /* We have data to return then copy it to the caller's buffer.*/ 616 while ((current_rx_bytes || likely(!list_empty(&dev->rx_buffers))) 617 && len) { 618 if (current_rx_bytes == 0) { 619 req = container_of(dev->rx_buffers.next, 620 struct usb_request, list); 621 list_del_init(&req->list); 622 623 if (req->actual && req->buf) { 624 current_rx_req = req; 625 current_rx_bytes = req->actual; 626 current_rx_buf = req->buf; 627 } else { 628 list_add(&req->list, &dev->rx_reqs); 629 continue; 630 } 631 } 632 633 /* Don't leave irqs off while doing memory copies */ 634 spin_unlock_irqrestore(&dev->lock, flags); 635 636 if (len > current_rx_bytes) 637 size = current_rx_bytes; 638 else 639 size = len; 640 641 size -= copy_to_user(buf, current_rx_buf, size); 642 bytes_copied += size; 643 len -= size; 644 buf += size; 645 646 spin_lock_irqsave(&dev->lock, flags); 647 648 /* We've disconnected or reset so return. */ 649 if (dev->reset_printer) { 650 list_add(¤t_rx_req->list, &dev->rx_reqs); 651 spin_unlock_irqrestore(&dev->lock, flags); 652 mutex_unlock(&dev->lock_printer_io); 653 return -EAGAIN; 654 } 655 656 /* If we not returning all the data left in this RX request 657 * buffer then adjust the amount of data left in the buffer. 658 * Othewise if we are done with this RX request buffer then 659 * requeue it to get any incoming data from the USB host. 660 */ 661 if (size < current_rx_bytes) { 662 current_rx_bytes -= size; 663 current_rx_buf += size; 664 } else { 665 list_add(¤t_rx_req->list, &dev->rx_reqs); 666 current_rx_bytes = 0; 667 current_rx_buf = NULL; 668 current_rx_req = NULL; 669 } 670 } 671 672 dev->current_rx_req = current_rx_req; 673 dev->current_rx_bytes = current_rx_bytes; 674 dev->current_rx_buf = current_rx_buf; 675 676 spin_unlock_irqrestore(&dev->lock, flags); 677 mutex_unlock(&dev->lock_printer_io); 678 679 DBG(dev, "printer_read returned %d bytes\n", (int)bytes_copied); 680 681 if (bytes_copied) 682 return bytes_copied; 683 else 684 return -EAGAIN; 685} 686 687static ssize_t 688printer_write(struct file *fd, const char __user *buf, size_t len, loff_t *ptr) 689{ 690 struct printer_dev *dev = fd->private_data; 691 unsigned long flags; 692 size_t size; /* Amount of data in a TX request. */ 693 size_t bytes_copied = 0; 694 struct usb_request *req; 695 696 DBG(dev, "printer_write trying to send %d bytes\n", (int)len); 697 698 if (len == 0) 699 return -EINVAL; 700 701 mutex_lock(&dev->lock_printer_io); 702 spin_lock_irqsave(&dev->lock, flags); 703 704 /* Check if a printer reset happens while we have interrupts on */ 705 dev->reset_printer = 0; 706 707 /* Check if there is any available write buffers */ 708 if (likely(list_empty(&dev->tx_reqs))) { 709 /* Turn interrupts back on before sleeping. */ 710 spin_unlock_irqrestore(&dev->lock, flags); 711 712 /* 713 * If write buffers are available check if this is 714 * a NON-Blocking call or not. 715 */ 716 if (fd->f_flags & (O_NONBLOCK|O_NDELAY)) { 717 mutex_unlock(&dev->lock_printer_io); 718 return -EAGAIN; 719 } 720 721 /* Sleep until a write buffer is available */ 722 wait_event_interruptible(dev->tx_wait, 723 (likely(!list_empty(&dev->tx_reqs)))); 724 spin_lock_irqsave(&dev->lock, flags); 725 } 726 727 while (likely(!list_empty(&dev->tx_reqs)) && len) { 728 729 if (len > USB_BUFSIZE) 730 size = USB_BUFSIZE; 731 else 732 size = len; 733 734 req = container_of(dev->tx_reqs.next, struct usb_request, 735 list); 736 list_del_init(&req->list); 737 738 req->complete = tx_complete; 739 req->length = size; 740 741 /* Check if we need to send a zero length packet. */ 742 if (len > size) 743 /* They will be more TX requests so no yet. */ 744 req->zero = 0; 745 else 746 /* If the data amount is not a multple of the 747 * maxpacket size then send a zero length packet. 748 */ 749 req->zero = ((len % dev->in_ep->maxpacket) == 0); 750 751 /* Don't leave irqs off while doing memory copies */ 752 spin_unlock_irqrestore(&dev->lock, flags); 753 754 if (copy_from_user(req->buf, buf, size)) { 755 list_add(&req->list, &dev->tx_reqs); 756 mutex_unlock(&dev->lock_printer_io); 757 return bytes_copied; 758 } 759 760 bytes_copied += size; 761 len -= size; 762 buf += size; 763 764 spin_lock_irqsave(&dev->lock, flags); 765 766 /* We've disconnected or reset so free the req and buffer */ 767 if (dev->reset_printer) { 768 list_add(&req->list, &dev->tx_reqs); 769 spin_unlock_irqrestore(&dev->lock, flags); 770 mutex_unlock(&dev->lock_printer_io); 771 return -EAGAIN; 772 } 773 774 if (usb_ep_queue(dev->in_ep, req, GFP_ATOMIC)) { 775 list_add(&req->list, &dev->tx_reqs); 776 spin_unlock_irqrestore(&dev->lock, flags); 777 mutex_unlock(&dev->lock_printer_io); 778 return -EAGAIN; 779 } 780 781 list_add(&req->list, &dev->tx_reqs_active); 782 783 } 784 785 spin_unlock_irqrestore(&dev->lock, flags); 786 mutex_unlock(&dev->lock_printer_io); 787 788 DBG(dev, "printer_write sent %d bytes\n", (int)bytes_copied); 789 790 if (bytes_copied) { 791 return bytes_copied; 792 } else { 793 return -EAGAIN; 794 } 795} 796 797static int 798printer_fsync(struct file *fd, int datasync) 799{ 800 struct printer_dev *dev = fd->private_data; 801 unsigned long flags; 802 int tx_list_empty; 803 804 spin_lock_irqsave(&dev->lock, flags); 805 tx_list_empty = (likely(list_empty(&dev->tx_reqs))); 806 spin_unlock_irqrestore(&dev->lock, flags); 807 808 if (!tx_list_empty) { 809 /* Sleep until all data has been sent */ 810 wait_event_interruptible(dev->tx_flush_wait, 811 (likely(list_empty(&dev->tx_reqs_active)))); 812 } 813 814 return 0; 815} 816 817static unsigned int 818printer_poll(struct file *fd, poll_table *wait) 819{ 820 struct printer_dev *dev = fd->private_data; 821 unsigned long flags; 822 int status = 0; 823 824 mutex_lock(&dev->lock_printer_io); 825 spin_lock_irqsave(&dev->lock, flags); 826 setup_rx_reqs(dev); 827 spin_unlock_irqrestore(&dev->lock, flags); 828 mutex_unlock(&dev->lock_printer_io); 829 830 poll_wait(fd, &dev->rx_wait, wait); 831 poll_wait(fd, &dev->tx_wait, wait); 832 833 spin_lock_irqsave(&dev->lock, flags); 834 if (likely(!list_empty(&dev->tx_reqs))) 835 status |= POLLOUT | POLLWRNORM; 836 837 if (likely(dev->current_rx_bytes) || 838 likely(!list_empty(&dev->rx_buffers))) 839 status |= POLLIN | POLLRDNORM; 840 841 spin_unlock_irqrestore(&dev->lock, flags); 842 843 return status; 844} 845 846static long 847printer_ioctl(struct file *fd, unsigned int code, unsigned long arg) 848{ 849 struct printer_dev *dev = fd->private_data; 850 unsigned long flags; 851 int status = 0; 852 853 DBG(dev, "printer_ioctl: cmd=0x%4.4x, arg=%lu\n", code, arg); 854 855 /* handle ioctls */ 856 857 spin_lock_irqsave(&dev->lock, flags); 858 859 switch (code) { 860 case GADGET_GET_PRINTER_STATUS: 861 status = (int)dev->printer_status; 862 break; 863 case GADGET_SET_PRINTER_STATUS: 864 dev->printer_status = (u8)arg; 865 break; 866 default: 867 /* could not handle ioctl */ 868 DBG(dev, "printer_ioctl: ERROR cmd=0x%4.4xis not supported\n", 869 code); 870 status = -ENOTTY; 871 } 872 873 spin_unlock_irqrestore(&dev->lock, flags); 874 875 return status; 876} 877 878/* used after endpoint configuration */ 879static const struct file_operations printer_io_operations = { 880 .owner = THIS_MODULE, 881 .open = printer_open, 882 .read = printer_read, 883 .write = printer_write, 884 .fsync = printer_fsync, 885 .poll = printer_poll, 886 .unlocked_ioctl = printer_ioctl, 887 .release = printer_close 888}; 889 890/*-------------------------------------------------------------------------*/ 891 892static int 893set_printer_interface(struct printer_dev *dev) 894{ 895 int result = 0; 896 897 dev->in = ep_desc(dev->gadget, &hs_ep_in_desc, &fs_ep_in_desc); 898 dev->in_ep->driver_data = dev; 899 900 dev->out = ep_desc(dev->gadget, &hs_ep_out_desc, &fs_ep_out_desc); 901 dev->out_ep->driver_data = dev; 902 903 result = usb_ep_enable(dev->in_ep, dev->in); 904 if (result != 0) { 905 DBG(dev, "enable %s --> %d\n", dev->in_ep->name, result); 906 goto done; 907 } 908 909 result = usb_ep_enable(dev->out_ep, dev->out); 910 if (result != 0) { 911 DBG(dev, "enable %s --> %d\n", dev->in_ep->name, result); 912 goto done; 913 } 914 915done: 916 /* on error, disable any endpoints */ 917 if (result != 0) { 918 (void) usb_ep_disable(dev->in_ep); 919 (void) usb_ep_disable(dev->out_ep); 920 dev->in = NULL; 921 dev->out = NULL; 922 } 923 924 /* caller is responsible for cleanup on error */ 925 return result; 926} 927 928static void printer_reset_interface(struct printer_dev *dev) 929{ 930 if (dev->interface < 0) 931 return; 932 933 DBG(dev, "%s\n", __func__); 934 935 if (dev->in) 936 usb_ep_disable(dev->in_ep); 937 938 if (dev->out) 939 usb_ep_disable(dev->out_ep); 940 941 dev->interface = -1; 942} 943 944/* change our operational config. must agree with the code 945 * that returns config descriptors, and altsetting code. 946 */ 947static int 948printer_set_config(struct printer_dev *dev, unsigned number) 949{ 950 int result = 0; 951 struct usb_gadget *gadget = dev->gadget; 952 953 switch (number) { 954 case DEV_CONFIG_VALUE: 955 result = 0; 956 break; 957 default: 958 result = -EINVAL; 959 /* FALL THROUGH */ 960 case 0: 961 break; 962 } 963 964 if (result) { 965 usb_gadget_vbus_draw(dev->gadget, 966 dev->gadget->is_otg ? 8 : 100); 967 } else { 968 char *speed; 969 unsigned power; 970 971 power = 2 * config_desc.bMaxPower; 972 usb_gadget_vbus_draw(dev->gadget, power); 973 974 switch (gadget->speed) { 975 case USB_SPEED_FULL: speed = "full"; break; 976#ifdef CONFIG_USB_GADGET_DUALSPEED 977 case USB_SPEED_HIGH: speed = "high"; break; 978#endif 979 default: speed = "?"; break; 980 } 981 982 dev->config = number; 983 INFO(dev, "%s speed config #%d: %d mA, %s\n", 984 speed, number, power, driver_desc); 985 } 986 return result; 987} 988 989static int 990config_buf(enum usb_device_speed speed, u8 *buf, u8 type, unsigned index, 991 int is_otg) 992{ 993 int len; 994 const struct usb_descriptor_header **function; 995#ifdef CONFIG_USB_GADGET_DUALSPEED 996 int hs = (speed == USB_SPEED_HIGH); 997 998 if (type == USB_DT_OTHER_SPEED_CONFIG) 999 hs = !hs; 1000 1001 if (hs) { 1002 function = hs_printer_function; 1003 } else { 1004 function = fs_printer_function; 1005 } 1006#else 1007 function = fs_printer_function; 1008#endif 1009 1010 if (index >= device_desc.bNumConfigurations) 1011 return -EINVAL; 1012 1013 /* for now, don't advertise srp-only devices */ 1014 if (!is_otg) 1015 function++; 1016 1017 len = usb_gadget_config_buf(&config_desc, buf, USB_DESC_BUFSIZE, 1018 function); 1019 if (len < 0) 1020 return len; 1021 ((struct usb_config_descriptor *) buf)->bDescriptorType = type; 1022 return len; 1023} 1024 1025/* Change our operational Interface. */ 1026static int 1027set_interface(struct printer_dev *dev, unsigned number) 1028{ 1029 int result = 0; 1030 1031 /* Free the current interface */ 1032 switch (dev->interface) { 1033 case PRINTER_INTERFACE: 1034 printer_reset_interface(dev); 1035 break; 1036 } 1037 1038 switch (number) { 1039 case PRINTER_INTERFACE: 1040 result = set_printer_interface(dev); 1041 if (result) { 1042 printer_reset_interface(dev); 1043 } else { 1044 dev->interface = PRINTER_INTERFACE; 1045 } 1046 break; 1047 default: 1048 result = -EINVAL; 1049 /* FALL THROUGH */ 1050 } 1051 1052 if (!result) 1053 INFO(dev, "Using interface %x\n", number); 1054 1055 return result; 1056} 1057 1058static void printer_setup_complete(struct usb_ep *ep, struct usb_request *req) 1059{ 1060 if (req->status || req->actual != req->length) 1061 DBG((struct printer_dev *) ep->driver_data, 1062 "setup complete --> %d, %d/%d\n", 1063 req->status, req->actual, req->length); 1064} 1065 1066static void printer_soft_reset(struct printer_dev *dev) 1067{ 1068 struct usb_request *req; 1069 1070 INFO(dev, "Received Printer Reset Request\n"); 1071 1072 if (usb_ep_disable(dev->in_ep)) 1073 DBG(dev, "Failed to disable USB in_ep\n"); 1074 if (usb_ep_disable(dev->out_ep)) 1075 DBG(dev, "Failed to disable USB out_ep\n"); 1076 1077 if (dev->current_rx_req != NULL) { 1078 list_add(&dev->current_rx_req->list, &dev->rx_reqs); 1079 dev->current_rx_req = NULL; 1080 } 1081 dev->current_rx_bytes = 0; 1082 dev->current_rx_buf = NULL; 1083 dev->reset_printer = 1; 1084 1085 while (likely(!(list_empty(&dev->rx_buffers)))) { 1086 req = container_of(dev->rx_buffers.next, struct usb_request, 1087 list); 1088 list_del_init(&req->list); 1089 list_add(&req->list, &dev->rx_reqs); 1090 } 1091 1092 while (likely(!(list_empty(&dev->rx_reqs_active)))) { 1093 req = container_of(dev->rx_buffers.next, struct usb_request, 1094 list); 1095 list_del_init(&req->list); 1096 list_add(&req->list, &dev->rx_reqs); 1097 } 1098 1099 while (likely(!(list_empty(&dev->tx_reqs_active)))) { 1100 req = container_of(dev->tx_reqs_active.next, 1101 struct usb_request, list); 1102 list_del_init(&req->list); 1103 list_add(&req->list, &dev->tx_reqs); 1104 } 1105 1106 if (usb_ep_enable(dev->in_ep, dev->in)) 1107 DBG(dev, "Failed to enable USB in_ep\n"); 1108 if (usb_ep_enable(dev->out_ep, dev->out)) 1109 DBG(dev, "Failed to enable USB out_ep\n"); 1110 1111 wake_up_interruptible(&dev->rx_wait); 1112 wake_up_interruptible(&dev->tx_wait); 1113 wake_up_interruptible(&dev->tx_flush_wait); 1114} 1115 1116/*-------------------------------------------------------------------------*/ 1117 1118/* 1119 * The setup() callback implements all the ep0 functionality that's not 1120 * handled lower down. 1121 */ 1122static int 1123printer_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl) 1124{ 1125 struct printer_dev *dev = get_gadget_data(gadget); 1126 struct usb_request *req = dev->req; 1127 int value = -EOPNOTSUPP; 1128 u16 wIndex = le16_to_cpu(ctrl->wIndex); 1129 u16 wValue = le16_to_cpu(ctrl->wValue); 1130 u16 wLength = le16_to_cpu(ctrl->wLength); 1131 1132 DBG(dev, "ctrl req%02x.%02x v%04x i%04x l%d\n", 1133 ctrl->bRequestType, ctrl->bRequest, wValue, wIndex, wLength); 1134 1135 req->complete = printer_setup_complete; 1136 1137 switch (ctrl->bRequestType&USB_TYPE_MASK) { 1138 1139 case USB_TYPE_STANDARD: 1140 switch (ctrl->bRequest) { 1141 1142 case USB_REQ_GET_DESCRIPTOR: 1143 if (ctrl->bRequestType != USB_DIR_IN) 1144 break; 1145 switch (wValue >> 8) { 1146 1147 case USB_DT_DEVICE: 1148 value = min(wLength, (u16) sizeof device_desc); 1149 memcpy(req->buf, &device_desc, value); 1150 break; 1151#ifdef CONFIG_USB_GADGET_DUALSPEED 1152 case USB_DT_DEVICE_QUALIFIER: 1153 if (!gadget->is_dualspeed) 1154 break; 1155 value = min(wLength, 1156 (u16) sizeof dev_qualifier); 1157 memcpy(req->buf, &dev_qualifier, value); 1158 break; 1159 1160 case USB_DT_OTHER_SPEED_CONFIG: 1161 if (!gadget->is_dualspeed) 1162 break; 1163 /* FALLTHROUGH */ 1164#endif /* CONFIG_USB_GADGET_DUALSPEED */ 1165 case USB_DT_CONFIG: 1166 value = config_buf(gadget->speed, req->buf, 1167 wValue >> 8, 1168 wValue & 0xff, 1169 gadget->is_otg); 1170 if (value >= 0) 1171 value = min(wLength, (u16) value); 1172 break; 1173 1174 case USB_DT_STRING: 1175 value = usb_gadget_get_string(&stringtab, 1176 wValue & 0xff, req->buf); 1177 if (value >= 0) 1178 value = min(wLength, (u16) value); 1179 break; 1180 } 1181 break; 1182 1183 case USB_REQ_SET_CONFIGURATION: 1184 if (ctrl->bRequestType != 0) 1185 break; 1186 if (gadget->a_hnp_support) 1187 DBG(dev, "HNP available\n"); 1188 else if (gadget->a_alt_hnp_support) 1189 DBG(dev, "HNP needs a different root port\n"); 1190 value = printer_set_config(dev, wValue); 1191 break; 1192 case USB_REQ_GET_CONFIGURATION: 1193 if (ctrl->bRequestType != USB_DIR_IN) 1194 break; 1195 *(u8 *)req->buf = dev->config; 1196 value = min(wLength, (u16) 1); 1197 break; 1198 1199 case USB_REQ_SET_INTERFACE: 1200 if (ctrl->bRequestType != USB_RECIP_INTERFACE || 1201 !dev->config) 1202 break; 1203 1204 value = set_interface(dev, PRINTER_INTERFACE); 1205 break; 1206 case USB_REQ_GET_INTERFACE: 1207 if (ctrl->bRequestType != 1208 (USB_DIR_IN|USB_RECIP_INTERFACE) 1209 || !dev->config) 1210 break; 1211 1212 *(u8 *)req->buf = dev->interface; 1213 value = min(wLength, (u16) 1); 1214 break; 1215 1216 default: 1217 goto unknown; 1218 } 1219 break; 1220 1221 case USB_TYPE_CLASS: 1222 switch (ctrl->bRequest) { 1223 case 0: /* Get the IEEE-1284 PNP String */ 1224 /* Only one printer interface is supported. */ 1225 if ((wIndex>>8) != PRINTER_INTERFACE) 1226 break; 1227 1228 value = (pnp_string[0]<<8)|pnp_string[1]; 1229 memcpy(req->buf, pnp_string, value); 1230 DBG(dev, "1284 PNP String: %x %s\n", value, 1231 &pnp_string[2]); 1232 break; 1233 1234 case 1: /* Get Port Status */ 1235 /* Only one printer interface is supported. */ 1236 if (wIndex != PRINTER_INTERFACE) 1237 break; 1238 1239 *(u8 *)req->buf = dev->printer_status; 1240 value = min(wLength, (u16) 1); 1241 break; 1242 1243 case 2: /* Soft Reset */ 1244 /* Only one printer interface is supported. */ 1245 if (wIndex != PRINTER_INTERFACE) 1246 break; 1247 1248 printer_soft_reset(dev); 1249 1250 value = 0; 1251 break; 1252 1253 default: 1254 goto unknown; 1255 } 1256 break; 1257 1258 default: 1259unknown: 1260 VDBG(dev, 1261 "unknown ctrl req%02x.%02x v%04x i%04x l%d\n", 1262 ctrl->bRequestType, ctrl->bRequest, 1263 wValue, wIndex, wLength); 1264 break; 1265 } 1266 1267 /* respond with data transfer before status phase? */ 1268 if (value >= 0) { 1269 req->length = value; 1270 req->zero = value < wLength; 1271 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC); 1272 if (value < 0) { 1273 DBG(dev, "ep_queue --> %d\n", value); 1274 req->status = 0; 1275 printer_setup_complete(gadget->ep0, req); 1276 } 1277 } 1278 1279 /* host either stalls (value < 0) or reports success */ 1280 return value; 1281} 1282 1283static void 1284printer_disconnect(struct usb_gadget *gadget) 1285{ 1286 struct printer_dev *dev = get_gadget_data(gadget); 1287 unsigned long flags; 1288 1289 DBG(dev, "%s\n", __func__); 1290 1291 spin_lock_irqsave(&dev->lock, flags); 1292 1293 printer_reset_interface(dev); 1294 1295 spin_unlock_irqrestore(&dev->lock, flags); 1296} 1297 1298static void 1299printer_unbind(struct usb_gadget *gadget) 1300{ 1301 struct printer_dev *dev = get_gadget_data(gadget); 1302 struct usb_request *req; 1303 1304 1305 DBG(dev, "%s\n", __func__); 1306 1307 /* Remove sysfs files */ 1308 device_destroy(usb_gadget_class, g_printer_devno); 1309 1310 /* Remove Character Device */ 1311 cdev_del(&dev->printer_cdev); 1312 1313 /* we must already have been disconnected ... no i/o may be active */ 1314 WARN_ON(!list_empty(&dev->tx_reqs_active)); 1315 WARN_ON(!list_empty(&dev->rx_reqs_active)); 1316 1317 /* Free all memory for this driver. */ 1318 while (!list_empty(&dev->tx_reqs)) { 1319 req = container_of(dev->tx_reqs.next, struct usb_request, 1320 list); 1321 list_del(&req->list); 1322 printer_req_free(dev->in_ep, req); 1323 } 1324 1325 if (dev->current_rx_req != NULL) 1326 printer_req_free(dev->out_ep, dev->current_rx_req); 1327 1328 while (!list_empty(&dev->rx_reqs)) { 1329 req = container_of(dev->rx_reqs.next, 1330 struct usb_request, list); 1331 list_del(&req->list); 1332 printer_req_free(dev->out_ep, req); 1333 } 1334 1335 while (!list_empty(&dev->rx_buffers)) { 1336 req = container_of(dev->rx_buffers.next, 1337 struct usb_request, list); 1338 list_del(&req->list); 1339 printer_req_free(dev->out_ep, req); 1340 } 1341 1342 if (dev->req) { 1343 printer_req_free(gadget->ep0, dev->req); 1344 dev->req = NULL; 1345 } 1346 1347 set_gadget_data(gadget, NULL); 1348} 1349 1350static int __ref 1351printer_bind(struct usb_gadget *gadget) 1352{ 1353 struct printer_dev *dev; 1354 struct usb_ep *in_ep, *out_ep; 1355 int status = -ENOMEM; 1356 int gcnum; 1357 size_t len; 1358 u32 i; 1359 struct usb_request *req; 1360 1361 dev = &usb_printer_gadget; 1362 1363 1364 /* Setup the sysfs files for the printer gadget. */ 1365 dev->pdev = device_create(usb_gadget_class, NULL, g_printer_devno, 1366 NULL, "g_printer"); 1367 if (IS_ERR(dev->pdev)) { 1368 ERROR(dev, "Failed to create device: g_printer\n"); 1369 goto fail; 1370 } 1371 1372 /* 1373 * Register a character device as an interface to a user mode 1374 * program that handles the printer specific functionality. 1375 */ 1376 cdev_init(&dev->printer_cdev, &printer_io_operations); 1377 dev->printer_cdev.owner = THIS_MODULE; 1378 status = cdev_add(&dev->printer_cdev, g_printer_devno, 1); 1379 if (status) { 1380 ERROR(dev, "Failed to open char device\n"); 1381 goto fail; 1382 } 1383 1384 gcnum = usb_gadget_controller_number(gadget); 1385 if (gcnum >= 0) { 1386 device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum); 1387 } else { 1388 dev_warn(&gadget->dev, "controller '%s' not recognized\n", 1389 gadget->name); 1390 /* unrecognized, but safe unless bulk is REALLY quirky */ 1391 device_desc.bcdDevice = 1392 cpu_to_le16(0xFFFF); 1393 } 1394 snprintf(manufacturer, sizeof(manufacturer), "%s %s with %s", 1395 init_utsname()->sysname, init_utsname()->release, 1396 gadget->name); 1397 1398 device_desc.idVendor = 1399 cpu_to_le16(PRINTER_VENDOR_NUM); 1400 device_desc.idProduct = 1401 cpu_to_le16(PRINTER_PRODUCT_NUM); 1402 1403 /* support optional vendor/distro customization */ 1404 if (idVendor) { 1405 if (!idProduct) { 1406 dev_err(&gadget->dev, "idVendor needs idProduct!\n"); 1407 return -ENODEV; 1408 } 1409 device_desc.idVendor = cpu_to_le16(idVendor); 1410 device_desc.idProduct = cpu_to_le16(idProduct); 1411 if (bcdDevice) 1412 device_desc.bcdDevice = cpu_to_le16(bcdDevice); 1413 } 1414 1415 if (iManufacturer) 1416 strlcpy(manufacturer, iManufacturer, sizeof manufacturer); 1417 1418 if (iProduct) 1419 strlcpy(product_desc, iProduct, sizeof product_desc); 1420 1421 if (iSerialNum) 1422 strlcpy(serial_num, iSerialNum, sizeof serial_num); 1423 1424 if (iPNPstring) 1425 strlcpy(&pnp_string[2], iPNPstring, (sizeof pnp_string)-2); 1426 1427 len = strlen(pnp_string); 1428 pnp_string[0] = (len >> 8) & 0xFF; 1429 pnp_string[1] = len & 0xFF; 1430 1431 /* all we really need is bulk IN/OUT */ 1432 usb_ep_autoconfig_reset(gadget); 1433 in_ep = usb_ep_autoconfig(gadget, &fs_ep_in_desc); 1434 if (!in_ep) { 1435autoconf_fail: 1436 dev_err(&gadget->dev, "can't autoconfigure on %s\n", 1437 gadget->name); 1438 return -ENODEV; 1439 } 1440 in_ep->driver_data = in_ep; /* claim */ 1441 1442 out_ep = usb_ep_autoconfig(gadget, &fs_ep_out_desc); 1443 if (!out_ep) 1444 goto autoconf_fail; 1445 out_ep->driver_data = out_ep; /* claim */ 1446 1447#ifdef CONFIG_USB_GADGET_DUALSPEED 1448 /* assumes ep0 uses the same value for both speeds ... */ 1449 dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0; 1450 1451 /* and that all endpoints are dual-speed */ 1452 hs_ep_in_desc.bEndpointAddress = fs_ep_in_desc.bEndpointAddress; 1453 hs_ep_out_desc.bEndpointAddress = fs_ep_out_desc.bEndpointAddress; 1454#endif /* DUALSPEED */ 1455 1456 device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket; 1457 usb_gadget_set_selfpowered(gadget); 1458 1459 if (gadget->is_otg) { 1460 otg_desc.bmAttributes |= USB_OTG_HNP, 1461 config_desc.bmAttributes |= USB_CONFIG_ATT_WAKEUP; 1462 } 1463 1464 spin_lock_init(&dev->lock); 1465 mutex_init(&dev->lock_printer_io); 1466 INIT_LIST_HEAD(&dev->tx_reqs); 1467 INIT_LIST_HEAD(&dev->tx_reqs_active); 1468 INIT_LIST_HEAD(&dev->rx_reqs); 1469 INIT_LIST_HEAD(&dev->rx_reqs_active); 1470 INIT_LIST_HEAD(&dev->rx_buffers); 1471 init_waitqueue_head(&dev->rx_wait); 1472 init_waitqueue_head(&dev->tx_wait); 1473 init_waitqueue_head(&dev->tx_flush_wait); 1474 1475 dev->config = 0; 1476 dev->interface = -1; 1477 dev->printer_cdev_open = 0; 1478 dev->printer_status = PRINTER_NOT_ERROR; 1479 dev->current_rx_req = NULL; 1480 dev->current_rx_bytes = 0; 1481 dev->current_rx_buf = NULL; 1482 1483 dev->in_ep = in_ep; 1484 dev->out_ep = out_ep; 1485 1486 /* preallocate control message data and buffer */ 1487 dev->req = printer_req_alloc(gadget->ep0, USB_DESC_BUFSIZE, 1488 GFP_KERNEL); 1489 if (!dev->req) { 1490 status = -ENOMEM; 1491 goto fail; 1492 } 1493 1494 for (i = 0; i < QLEN; i++) { 1495 req = printer_req_alloc(dev->in_ep, USB_BUFSIZE, GFP_KERNEL); 1496 if (!req) { 1497 while (!list_empty(&dev->tx_reqs)) { 1498 req = container_of(dev->tx_reqs.next, 1499 struct usb_request, list); 1500 list_del(&req->list); 1501 printer_req_free(dev->in_ep, req); 1502 } 1503 return -ENOMEM; 1504 } 1505 list_add(&req->list, &dev->tx_reqs); 1506 } 1507 1508 for (i = 0; i < QLEN; i++) { 1509 req = printer_req_alloc(dev->out_ep, USB_BUFSIZE, GFP_KERNEL); 1510 if (!req) { 1511 while (!list_empty(&dev->rx_reqs)) { 1512 req = container_of(dev->rx_reqs.next, 1513 struct usb_request, list); 1514 list_del(&req->list); 1515 printer_req_free(dev->out_ep, req); 1516 } 1517 return -ENOMEM; 1518 } 1519 list_add(&req->list, &dev->rx_reqs); 1520 } 1521 1522 dev->req->complete = printer_setup_complete; 1523 1524 /* finish hookup to lower layer ... */ 1525 dev->gadget = gadget; 1526 set_gadget_data(gadget, dev); 1527 gadget->ep0->driver_data = dev; 1528 1529 INFO(dev, "%s, version: " DRIVER_VERSION "\n", driver_desc); 1530 INFO(dev, "using %s, OUT %s IN %s\n", gadget->name, out_ep->name, 1531 in_ep->name); 1532 1533 return 0; 1534 1535fail: 1536 printer_unbind(gadget); 1537 return status; 1538} 1539 1540/*-------------------------------------------------------------------------*/ 1541 1542static struct usb_gadget_driver printer_driver = { 1543 .speed = DEVSPEED, 1544 1545 .function = (char *) driver_desc, 1546 .bind = printer_bind, 1547 .unbind = printer_unbind, 1548 1549 .setup = printer_setup, 1550 .disconnect = printer_disconnect, 1551 1552 .driver = { 1553 .name = (char *) shortname, 1554 .owner = THIS_MODULE, 1555 }, 1556}; 1557 1558MODULE_DESCRIPTION(DRIVER_DESC); 1559MODULE_AUTHOR("Craig Nadler"); 1560MODULE_LICENSE("GPL"); 1561 1562static int __init 1563init(void) 1564{ 1565 int status; 1566 1567 usb_gadget_class = class_create(THIS_MODULE, "usb_printer_gadget"); 1568 if (IS_ERR(usb_gadget_class)) { 1569 status = PTR_ERR(usb_gadget_class); 1570 ERROR(dev, "unable to create usb_gadget class %d\n", status); 1571 return status; 1572 } 1573 1574 status = alloc_chrdev_region(&g_printer_devno, 0, 1, 1575 "USB printer gadget"); 1576 if (status) { 1577 ERROR(dev, "alloc_chrdev_region %d\n", status); 1578 class_destroy(usb_gadget_class); 1579 return status; 1580 } 1581 1582 status = usb_gadget_register_driver(&printer_driver); 1583 if (status) { 1584 class_destroy(usb_gadget_class); 1585 unregister_chrdev_region(g_printer_devno, 1); 1586 DBG(dev, "usb_gadget_register_driver %x\n", status); 1587 } 1588 1589 return status; 1590} 1591module_init(init); 1592 1593static void __exit 1594cleanup(void) 1595{ 1596 int status; 1597 1598 mutex_lock(&usb_printer_gadget.lock_printer_io); 1599 status = usb_gadget_unregister_driver(&printer_driver); 1600 if (status) 1601 ERROR(dev, "usb_gadget_unregister_driver %x\n", status); 1602 1603 unregister_chrdev_region(g_printer_devno, 2); 1604 class_destroy(usb_gadget_class); 1605 mutex_unlock(&usb_printer_gadget.lock_printer_io); 1606} 1607module_exit(cleanup); 1608