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