2 * printer.c -- Printer gadget driver
4 * Copyright (C) 2003-2005 David Brownell
5 * Copyright (C) 2006 Craig W. Nadler
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.
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>
28 #include <linux/poll.h>
29 #include <linux/types.h>
30 #include <linux/ctype.h>
31 #include <linux/cdev.h>
33 #include <asm/byteorder.h>
35 #include <linux/irq.h>
36 #include <linux/uaccess.h>
37 #include <asm/unaligned.h>
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>
44 #include "gadget_chips.h"
46 USB_GADGET_COMPOSITE_OPTIONS();
48 #define DRIVER_DESC "Printer Gadget"
49 #define DRIVER_VERSION "2007 OCT 06"
51 static DEFINE_MUTEX(printer_mutex);
52 static const char shortname [] = "printer";
53 static const char driver_desc [] = DRIVER_DESC;
55 static dev_t g_printer_devno;
57 static struct class *usb_gadget_class;
59 /*-------------------------------------------------------------------------*/
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;
67 struct usb_ep *in_ep, *out_ep;
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;
85 struct cdev printer_cdev;
88 wait_queue_head_t wait;
89 struct usb_function function;
92 static struct printer_dev usb_printer_gadget;
94 /*-------------------------------------------------------------------------*/
96 /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
97 * Instead: allocate your own, using normal USB-IF procedures.
100 /* Thanks to NetChip Technologies for donating this product ID.
102 #define PRINTER_VENDOR_NUM 0x0525 /* NetChip */
103 #define PRINTER_PRODUCT_NUM 0xa4a8 /* Linux-USB Printer Gadget */
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).
110 module_param_named(iSerialNum, coverwrite.serial_number, charp, S_IRUGO);
111 MODULE_PARM_DESC(iSerialNum, "1");
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;");
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);
123 /*-------------------------------------------------------------------------*/
126 * DESCRIPTORS ... most are static, but strings and (full) configuration
127 * descriptors are built on demand.
130 /* holds our biggest descriptor */
131 #define USB_DESC_BUFSIZE 256
132 #define USB_BUFSIZE 8192
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
146 static struct usb_interface_descriptor intf_desc = {
147 .bLength = sizeof intf_desc,
148 .bDescriptorType = USB_DT_INTERFACE,
150 .bInterfaceClass = USB_CLASS_PRINTER,
151 .bInterfaceSubClass = 1, /* Printer Sub-Class */
152 .bInterfaceProtocol = 2, /* Bi-Directional */
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
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
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,
178 * usb 2.0 devices need to expose both high speed and full speed
179 * descriptors, unless they only run at full speed.
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)
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)
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
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,
212 * Added endpoint descriptors for 3.0 devices
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),
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,
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),
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,
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,
248 static struct usb_otg_descriptor otg_descriptor = {
249 .bLength = sizeof otg_descriptor,
250 .bDescriptorType = USB_DT_OTG,
251 .bmAttributes = USB_OTG_SRP,
254 static const struct usb_descriptor_header *otg_desc[] = {
255 (struct usb_descriptor_header *) &otg_descriptor,
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)
265 switch (gadget->speed) {
266 case USB_SPEED_SUPER:
275 /*-------------------------------------------------------------------------*/
277 /* descriptors that are built on-demand */
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;";
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 */
292 static struct usb_gadget_strings stringtab_dev = {
293 .language = 0x0409, /* en-us */
297 static struct usb_gadget_strings *dev_strings[] = {
302 /*-------------------------------------------------------------------------*/
304 static struct usb_request *
305 printer_req_alloc(struct usb_ep *ep, unsigned len, gfp_t gfp_flags)
307 struct usb_request *req;
309 req = usb_ep_alloc_request(ep, gfp_flags);
313 req->buf = kmalloc(len, gfp_flags);
314 if (req->buf == NULL) {
315 usb_ep_free_request(ep, req);
324 printer_req_free(struct usb_ep *ep, struct usb_request *req)
326 if (ep != NULL && req != NULL) {
328 usb_ep_free_request(ep, req);
332 /*-------------------------------------------------------------------------*/
334 static void rx_complete(struct usb_ep *ep, struct usb_request *req)
336 struct printer_dev *dev = ep->driver_data;
337 int status = req->status;
340 spin_lock_irqsave(&dev->lock, flags);
342 list_del_init(&req->list); /* Remode from Active List */
346 /* normal completion */
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);
352 list_add(&req->list, &dev->rx_reqs);
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);
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);
374 DBG(dev, "rx status %d\n", status);
375 list_add(&req->list, &dev->rx_reqs);
379 wake_up_interruptible(&dev->rx_wait);
380 spin_unlock_irqrestore(&dev->lock, flags);
383 static void tx_complete(struct usb_ep *ep, struct usb_request *req)
385 struct printer_dev *dev = ep->driver_data;
387 switch (req->status) {
389 VDBG(dev, "tx err %d\n", req->status);
391 case -ECONNRESET: /* unlink */
392 case -ESHUTDOWN: /* disconnect etc */
398 spin_lock(&dev->lock);
399 /* Take the request struct off the active list and put it on the
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);
408 spin_unlock(&dev->lock);
411 /*-------------------------------------------------------------------------*/
414 printer_open(struct inode *inode, struct file *fd)
416 struct printer_dev *dev;
420 mutex_lock(&printer_mutex);
421 dev = container_of(inode->i_cdev, struct printer_dev, printer_cdev);
423 spin_lock_irqsave(&dev->lock, flags);
425 if (!dev->printer_cdev_open) {
426 dev->printer_cdev_open = 1;
427 fd->private_data = dev;
429 /* Change the printer status to show that it's on-line. */
430 dev->printer_status |= PRINTER_SELECTED;
433 spin_unlock_irqrestore(&dev->lock, flags);
435 DBG(dev, "printer_open returned %x\n", ret);
436 mutex_unlock(&printer_mutex);
441 printer_close(struct inode *inode, struct file *fd)
443 struct printer_dev *dev = fd->private_data;
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);
453 DBG(dev, "printer_close\n");
458 /* This function must be called with interrupts turned off. */
460 setup_rx_reqs(struct printer_dev *dev)
462 struct usb_request *req;
464 while (likely(!list_empty(&dev->rx_reqs))) {
467 req = container_of(dev->rx_reqs.next,
468 struct usb_request, list);
469 list_del_init(&req->list);
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().
477 req->length = USB_BUFSIZE;
478 req->complete = rx_complete;
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);
485 DBG(dev, "rx submit --> %d\n", error);
486 list_add(&req->list, &dev->rx_reqs);
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);
497 printer_read(struct file *fd, char __user *buf, size_t len, loff_t *ptr)
499 struct printer_dev *dev = fd->private_data;
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. */
514 DBG(dev, "printer_read trying to read %d bytes\n", (int)len);
516 mutex_lock(&dev->lock_printer_io);
517 spin_lock_irqsave(&dev->lock, flags);
519 /* We will use this flag later to check if a printer reset happened
520 * after we turn interrupts back on.
522 dev->reset_printer = 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;
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
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);
546 * If no data is available check if this is a NON-Blocking
549 if (fd->f_flags & (O_NONBLOCK|O_NDELAY)) {
550 mutex_unlock(&dev->lock_printer_io);
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);
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)))
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);
568 if (req->actual && req->buf) {
569 current_rx_req = req;
570 current_rx_bytes = req->actual;
571 current_rx_buf = req->buf;
573 list_add(&req->list, &dev->rx_reqs);
578 /* Don't leave irqs off while doing memory copies */
579 spin_unlock_irqrestore(&dev->lock, flags);
581 if (len > current_rx_bytes)
582 size = current_rx_bytes;
586 size -= copy_to_user(buf, current_rx_buf, size);
587 bytes_copied += size;
591 spin_lock_irqsave(&dev->lock, flags);
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);
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.
606 if (size < current_rx_bytes) {
607 current_rx_bytes -= size;
608 current_rx_buf += size;
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;
617 dev->current_rx_req = current_rx_req;
618 dev->current_rx_bytes = current_rx_bytes;
619 dev->current_rx_buf = current_rx_buf;
621 spin_unlock_irqrestore(&dev->lock, flags);
622 mutex_unlock(&dev->lock_printer_io);
624 DBG(dev, "printer_read returned %d bytes\n", (int)bytes_copied);
633 printer_write(struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
635 struct printer_dev *dev = fd->private_data;
637 size_t size; /* Amount of data in a TX request. */
638 size_t bytes_copied = 0;
639 struct usb_request *req;
641 DBG(dev, "printer_write trying to send %d bytes\n", (int)len);
646 mutex_lock(&dev->lock_printer_io);
647 spin_lock_irqsave(&dev->lock, flags);
649 /* Check if a printer reset happens while we have interrupts on */
650 dev->reset_printer = 0;
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);
658 * If write buffers are available check if this is
659 * a NON-Blocking call or not.
661 if (fd->f_flags & (O_NONBLOCK|O_NDELAY)) {
662 mutex_unlock(&dev->lock_printer_io);
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);
672 while (likely(!list_empty(&dev->tx_reqs)) && len) {
674 if (len > USB_BUFSIZE)
679 req = container_of(dev->tx_reqs.next, struct usb_request,
681 list_del_init(&req->list);
683 req->complete = tx_complete;
686 /* Check if we need to send a zero length packet. */
688 /* They will be more TX requests so no yet. */
691 /* If the data amount is not a multple of the
692 * maxpacket size then send a zero length packet.
694 req->zero = ((len % dev->in_ep->maxpacket) == 0);
696 /* Don't leave irqs off while doing memory copies */
697 spin_unlock_irqrestore(&dev->lock, flags);
699 if (copy_from_user(req->buf, buf, size)) {
700 list_add(&req->list, &dev->tx_reqs);
701 mutex_unlock(&dev->lock_printer_io);
705 bytes_copied += size;
709 spin_lock_irqsave(&dev->lock, flags);
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);
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);
726 list_add(&req->list, &dev->tx_reqs_active);
730 spin_unlock_irqrestore(&dev->lock, flags);
731 mutex_unlock(&dev->lock_printer_io);
733 DBG(dev, "printer_write sent %d bytes\n", (int)bytes_copied);
743 printer_fsync(struct file *fd, loff_t start, loff_t end, int datasync)
745 struct printer_dev *dev = fd->private_data;
746 struct inode *inode = file_inode(fd);
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);
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))));
760 mutex_unlock(&inode->i_mutex);
766 printer_poll(struct file *fd, poll_table *wait)
768 struct printer_dev *dev = fd->private_data;
772 mutex_lock(&dev->lock_printer_io);
773 spin_lock_irqsave(&dev->lock, flags);
775 spin_unlock_irqrestore(&dev->lock, flags);
776 mutex_unlock(&dev->lock_printer_io);
778 poll_wait(fd, &dev->rx_wait, wait);
779 poll_wait(fd, &dev->tx_wait, wait);
781 spin_lock_irqsave(&dev->lock, flags);
782 if (likely(!list_empty(&dev->tx_reqs)))
783 status |= POLLOUT | POLLWRNORM;
785 if (likely(dev->current_rx_bytes) ||
786 likely(!list_empty(&dev->rx_buffers)))
787 status |= POLLIN | POLLRDNORM;
789 spin_unlock_irqrestore(&dev->lock, flags);
795 printer_ioctl(struct file *fd, unsigned int code, unsigned long arg)
797 struct printer_dev *dev = fd->private_data;
801 DBG(dev, "printer_ioctl: cmd=0x%4.4x, arg=%lu\n", code, arg);
805 spin_lock_irqsave(&dev->lock, flags);
808 case GADGET_GET_PRINTER_STATUS:
809 status = (int)dev->printer_status;
811 case GADGET_SET_PRINTER_STATUS:
812 dev->printer_status = (u8)arg;
815 /* could not handle ioctl */
816 DBG(dev, "printer_ioctl: ERROR cmd=0x%4.4xis not supported\n",
821 spin_unlock_irqrestore(&dev->lock, flags);
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,
839 /*-------------------------------------------------------------------------*/
842 set_printer_interface(struct printer_dev *dev)
846 dev->in_ep->desc = ep_desc(dev->gadget, &fs_ep_in_desc, &hs_ep_in_desc,
848 dev->in_ep->driver_data = dev;
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;
854 result = usb_ep_enable(dev->in_ep);
856 DBG(dev, "enable %s --> %d\n", dev->in_ep->name, result);
860 result = usb_ep_enable(dev->out_ep);
862 DBG(dev, "enable %s --> %d\n", dev->in_ep->name, result);
867 /* on error, disable any endpoints */
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;
875 /* caller is responsible for cleanup on error */
879 static void printer_reset_interface(struct printer_dev *dev)
881 if (dev->interface < 0)
884 DBG(dev, "%s\n", __func__);
886 if (dev->in_ep->desc)
887 usb_ep_disable(dev->in_ep);
889 if (dev->out_ep->desc)
890 usb_ep_disable(dev->out_ep);
892 dev->in_ep->desc = NULL;
893 dev->out_ep->desc = NULL;
897 /* Change our operational Interface. */
898 static int set_interface(struct printer_dev *dev, unsigned number)
902 /* Free the current interface */
903 printer_reset_interface(dev);
905 result = set_printer_interface(dev);
907 printer_reset_interface(dev);
909 dev->interface = number;
912 INFO(dev, "Using interface %x\n", number);
917 static void printer_soft_reset(struct printer_dev *dev)
919 struct usb_request *req;
921 INFO(dev, "Received Printer Reset Request\n");
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");
928 if (dev->current_rx_req != NULL) {
929 list_add(&dev->current_rx_req->list, &dev->rx_reqs);
930 dev->current_rx_req = NULL;
932 dev->current_rx_bytes = 0;
933 dev->current_rx_buf = NULL;
934 dev->reset_printer = 1;
936 while (likely(!(list_empty(&dev->rx_buffers)))) {
937 req = container_of(dev->rx_buffers.next, struct usb_request,
939 list_del_init(&req->list);
940 list_add(&req->list, &dev->rx_reqs);
943 while (likely(!(list_empty(&dev->rx_reqs_active)))) {
944 req = container_of(dev->rx_buffers.next, struct usb_request,
946 list_del_init(&req->list);
947 list_add(&req->list, &dev->rx_reqs);
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);
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");
962 wake_up_interruptible(&dev->rx_wait);
963 wake_up_interruptible(&dev->tx_wait);
964 wake_up_interruptible(&dev->tx_flush_wait);
967 /*-------------------------------------------------------------------------*/
970 * The setup() callback implements all the ep0 functionality that's not
971 * handled lower down.
973 static int printer_func_setup(struct usb_function *f,
974 const struct usb_ctrlrequest *ctrl)
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);
984 DBG(dev, "ctrl req%02x.%02x v%04x i%04x l%d\n",
985 ctrl->bRequestType, ctrl->bRequest, wValue, wIndex, wLength);
987 switch (ctrl->bRequestType&USB_TYPE_MASK) {
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)
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,
1001 case 1: /* Get Port Status */
1002 /* Only one printer interface is supported. */
1003 if (wIndex != dev->interface)
1006 *(u8 *)req->buf = dev->printer_status;
1007 value = min(wLength, (u16) 1);
1010 case 2: /* Soft Reset */
1011 /* Only one printer interface is supported. */
1012 if (wIndex != dev->interface)
1015 printer_soft_reset(dev);
1028 "unknown ctrl req%02x.%02x v%04x i%04x l%d\n",
1029 ctrl->bRequestType, ctrl->bRequest,
1030 wValue, wIndex, wLength);
1033 /* host either stalls (value < 0) or reports success */
1037 static int __init printer_func_bind(struct usb_configuration *c,
1038 struct usb_function *f)
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;
1047 id = usb_interface_id(c, f);
1050 intf_desc.bInterfaceNumber = id;
1052 /* all we really need is bulk IN/OUT */
1053 in_ep = usb_ep_autoconfig(cdev->gadget, &fs_ep_in_desc);
1056 dev_err(&cdev->gadget->dev, "can't autoconfigure on %s\n",
1057 cdev->gadget->name);
1060 in_ep->driver_data = in_ep; /* claim */
1062 out_ep = usb_ep_autoconfig(cdev->gadget, &fs_ep_out_desc);
1065 out_ep->driver_data = out_ep; /* claim */
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;
1073 ret = usb_assign_descriptors(f, fs_printer_function,
1074 hs_printer_function, ss_printer_function);
1079 dev->out_ep = out_ep;
1083 static void printer_func_unbind(struct usb_configuration *c,
1084 struct usb_function *f)
1086 usb_free_all_descriptors(f);
1089 static int printer_func_set_alt(struct usb_function *f,
1090 unsigned intf, unsigned alt)
1092 struct printer_dev *dev = container_of(f, struct printer_dev, function);
1093 int ret = -ENOTSUPP;
1096 ret = set_interface(dev, intf);
1101 static void printer_func_disable(struct usb_function *f)
1103 struct printer_dev *dev = container_of(f, struct printer_dev, function);
1104 unsigned long flags;
1106 DBG(dev, "%s\n", __func__);
1108 spin_lock_irqsave(&dev->lock, flags);
1109 printer_reset_interface(dev);
1110 spin_unlock_irqrestore(&dev->lock, flags);
1113 static void printer_cfg_unbind(struct usb_configuration *c)
1115 struct printer_dev *dev;
1116 struct usb_request *req;
1118 dev = &usb_printer_gadget;
1120 DBG(dev, "%s\n", __func__);
1122 /* Remove sysfs files */
1123 device_destroy(usb_gadget_class, g_printer_devno);
1125 /* Remove Character Device */
1126 cdev_del(&dev->printer_cdev);
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));
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,
1136 list_del(&req->list);
1137 printer_req_free(dev->in_ep, req);
1140 if (dev->current_rx_req != NULL)
1141 printer_req_free(dev->out_ep, dev->current_rx_req);
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);
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);
1158 static struct usb_configuration printer_cfg_driver = {
1160 .unbind = printer_cfg_unbind,
1161 .bConfigurationValue = 1,
1162 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
1165 static int __init printer_bind_config(struct usb_configuration *c)
1167 struct usb_gadget *gadget = c->cdev->gadget;
1168 struct printer_dev *dev;
1169 int status = -ENOMEM;
1172 struct usb_request *req;
1174 usb_ep_autoconfig_reset(gadget);
1176 dev = &usb_printer_gadget;
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;
1185 status = usb_add_function(c, &dev->function);
1189 /* Setup the sysfs files for the printer gadget. */
1190 dev->pdev = device_create(usb_gadget_class, NULL, g_printer_devno,
1192 if (IS_ERR(dev->pdev)) {
1193 ERROR(dev, "Failed to create device: g_printer\n");
1194 status = PTR_ERR(dev->pdev);
1199 * Register a character device as an interface to a user mode
1200 * program that handles the printer specific functionality.
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);
1206 ERROR(dev, "Failed to open char device\n");
1211 strlcpy(&pnp_string[2], iPNPstring, (sizeof pnp_string)-2);
1213 len = strlen(pnp_string);
1214 pnp_string[0] = (len >> 8) & 0xFF;
1215 pnp_string[1] = len & 0xFF;
1217 usb_gadget_set_selfpowered(gadget);
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;
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);
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;
1243 for (i = 0; i < QLEN; i++) {
1244 req = printer_req_alloc(dev->in_ep, USB_BUFSIZE, GFP_KERNEL);
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);
1254 list_add(&req->list, &dev->tx_reqs);
1257 for (i = 0; i < QLEN; i++) {
1258 req = printer_req_alloc(dev->out_ep, USB_BUFSIZE, GFP_KERNEL);
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);
1268 list_add(&req->list, &dev->rx_reqs);
1271 /* finish hookup to lower layer ... */
1272 dev->gadget = gadget;
1274 INFO(dev, "%s, version: " DRIVER_VERSION "\n", driver_desc);
1278 printer_cfg_unbind(c);
1282 static int printer_unbind(struct usb_composite_dev *cdev)
1287 static int __init printer_bind(struct usb_composite_dev *cdev)
1291 ret = usb_string_ids_tab(cdev, strings);
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;
1298 ret = usb_add_config(cdev, &printer_cfg_driver, printer_bind_config);
1301 usb_composite_overwrite_options(cdev, &coverwrite);
1305 static __refdata struct usb_composite_driver printer_driver = {
1307 .dev = &device_desc,
1308 .strings = dev_strings,
1309 .max_speed = USB_SPEED_SUPER,
1310 .bind = printer_bind,
1311 .unbind = printer_unbind,
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);
1326 status = alloc_chrdev_region(&g_printer_devno, 0, 1,
1327 "USB printer gadget");
1329 pr_err("alloc_chrdev_region %d\n", status);
1330 class_destroy(usb_gadget_class);
1334 status = usb_composite_probe(&printer_driver);
1336 class_destroy(usb_gadget_class);
1337 unregister_chrdev_region(g_printer_devno, 1);
1338 pr_err("usb_gadget_probe_driver %x\n", status);
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);
1354 module_exit(cleanup);
1356 MODULE_DESCRIPTION(DRIVER_DESC);
1357 MODULE_AUTHOR("Craig Nadler");
1358 MODULE_LICENSE("GPL");