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.
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.
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
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>
38 #include <linux/poll.h>
39 #include <linux/types.h>
40 #include <linux/ctype.h>
41 #include <linux/cdev.h>
43 #include <asm/byteorder.h>
45 #include <linux/irq.h>
46 #include <asm/system.h>
47 #include <linux/uaccess.h>
48 #include <asm/unaligned.h>
50 #include <linux/usb/ch9.h>
51 #include <linux/usb/gadget.h>
52 #include <linux/usb/g_printer.h>
54 #include "gadget_chips.h"
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.
64 #include "usbstring.c"
66 #include "epautoconf.c"
68 /*-------------------------------------------------------------------------*/
70 #define DRIVER_DESC "Printer Gadget"
71 #define DRIVER_VERSION "2007 OCT 06"
73 static DEFINE_MUTEX(printer_mutex);
74 static const char shortname [] = "printer";
75 static const char driver_desc [] = DRIVER_DESC;
77 static dev_t g_printer_devno;
79 static struct class *usb_gadget_class;
81 /*-------------------------------------------------------------------------*/
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 */
91 struct usb_ep *in_ep, *out_ep;
92 const struct usb_endpoint_descriptor
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;
110 struct cdev printer_cdev;
112 u8 printer_cdev_open;
113 wait_queue_head_t wait;
116 static struct printer_dev usb_printer_gadget;
118 /*-------------------------------------------------------------------------*/
120 /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
121 * Instead: allocate your own, using normal USB-IF procedures.
124 /* Thanks to NetChip Technologies for donating this product ID.
126 #define PRINTER_VENDOR_NUM 0x0525 /* NetChip */
127 #define PRINTER_PRODUCT_NUM 0xa4a8 /* Linux-USB Printer Gadget */
129 /* Some systems will want different product identifiers 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).
134 static ushort idVendor;
135 module_param(idVendor, ushort, S_IRUGO);
136 MODULE_PARM_DESC(idVendor, "USB Vendor ID");
138 static ushort idProduct;
139 module_param(idProduct, ushort, S_IRUGO);
140 MODULE_PARM_DESC(idProduct, "USB Product ID");
142 static ushort bcdDevice;
143 module_param(bcdDevice, ushort, S_IRUGO);
144 MODULE_PARM_DESC(bcdDevice, "USB Device version (BCD)");
146 static char *iManufacturer;
147 module_param(iManufacturer, charp, S_IRUGO);
148 MODULE_PARM_DESC(iManufacturer, "USB Manufacturer string");
150 static char *iProduct;
151 module_param(iProduct, charp, S_IRUGO);
152 MODULE_PARM_DESC(iProduct, "USB Product string");
154 static char *iSerialNum;
155 module_param(iSerialNum, charp, S_IRUGO);
156 MODULE_PARM_DESC(iSerialNum, "1");
158 static char *iPNPstring;
159 module_param(iPNPstring, charp, S_IRUGO);
160 MODULE_PARM_DESC(iPNPstring, "MFG:linux;MDL:g_printer;CLS:PRINTER;SN:1;");
162 /* Number of requests to allocate per endpoint, not used for ep0. */
163 static unsigned qlen = 10;
164 module_param(qlen, uint, S_IRUGO|S_IWUSR);
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
174 /*-------------------------------------------------------------------------*/
176 #define xprintk(d, level, fmt, args...) \
177 printk(level "%s: " fmt, DRIVER_DESC, ## args)
180 #define DBG(dev, fmt, args...) \
181 xprintk(dev, KERN_DEBUG, fmt, ## args)
183 #define DBG(dev, fmt, args...) \
188 #define VDBG(dev, fmt, args...) \
189 xprintk(dev, KERN_DEBUG, fmt, ## args)
191 #define VDBG(dev, fmt, args...) \
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)
202 /*-------------------------------------------------------------------------*/
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.
210 * DESCRIPTORS ... most are static, but strings and (full) configuration
211 * descriptors are built on demand.
214 #define STRING_MANUFACTURER 1
215 #define STRING_PRODUCT 2
216 #define STRING_SERIALNUM 3
218 /* holds our biggest descriptor */
219 #define USB_DESC_BUFSIZE 256
220 #define USB_BUFSIZE 8192
222 /* This device advertises one configuration. */
223 #define DEV_CONFIG_VALUE 1
224 #define PRINTER_INTERFACE 0
226 static 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
241 static struct usb_otg_descriptor otg_desc = {
242 .bLength = sizeof otg_desc,
243 .bDescriptorType = USB_DT_OTG,
244 .bmAttributes = USB_OTG_SRP
247 static struct usb_config_descriptor config_desc = {
248 .bLength = sizeof config_desc,
249 .bDescriptorType = USB_DT_CONFIG,
251 /* compute wTotalLength on the fly */
253 .bConfigurationValue = DEV_CONFIG_VALUE,
255 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
256 .bMaxPower = CONFIG_USB_GADGET_VBUS_DRAW / 2,
259 static struct usb_interface_descriptor intf_desc = {
260 .bLength = sizeof intf_desc,
261 .bDescriptorType = USB_DT_INTERFACE,
262 .bInterfaceNumber = PRINTER_INTERFACE,
264 .bInterfaceClass = USB_CLASS_PRINTER,
265 .bInterfaceSubClass = 1, /* Printer Sub-Class */
266 .bInterfaceProtocol = 2, /* Bi-Directional */
270 static 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
277 static 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
284 static 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,
292 #ifdef CONFIG_USB_GADGET_DUALSPEED
295 * usb 2.0 devices need to expose both high speed and full speed
296 * descriptors, unless they only run at full speed.
299 static 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)
306 static 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)
313 static 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
321 static 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,
329 /* maxpacket and other transfer characteristics vary by speed. */
330 #define ep_desc(g, hs, fs) (((g)->speed == USB_SPEED_HIGH)?(hs):(fs))
334 /* if there's no high speed support, maxpacket doesn't change. */
335 #define ep_desc(g, hs, fs) (((void)(g)), (fs))
337 #endif /* !CONFIG_USB_GADGET_DUALSPEED */
339 /*-------------------------------------------------------------------------*/
341 /* descriptors that are built on-demand */
343 static char manufacturer [50];
344 static char product_desc [40] = DRIVER_DESC;
345 static char serial_num [40] = "1";
346 static char pnp_string [1024] =
347 "XXMFG:linux;MDL:g_printer;CLS:PRINTER;SN:1;";
349 /* static strings, in UTF-8 */
350 static struct usb_string strings [] = {
351 { STRING_MANUFACTURER, manufacturer, },
352 { STRING_PRODUCT, product_desc, },
353 { STRING_SERIALNUM, serial_num, },
354 { } /* end of list */
357 static struct usb_gadget_strings stringtab = {
358 .language = 0x0409, /* en-us */
362 /*-------------------------------------------------------------------------*/
364 static struct usb_request *
365 printer_req_alloc(struct usb_ep *ep, unsigned len, gfp_t gfp_flags)
367 struct usb_request *req;
369 req = usb_ep_alloc_request(ep, gfp_flags);
373 req->buf = kmalloc(len, gfp_flags);
374 if (req->buf == NULL) {
375 usb_ep_free_request(ep, req);
384 printer_req_free(struct usb_ep *ep, struct usb_request *req)
386 if (ep != NULL && req != NULL) {
388 usb_ep_free_request(ep, req);
392 /*-------------------------------------------------------------------------*/
394 static void rx_complete(struct usb_ep *ep, struct usb_request *req)
396 struct printer_dev *dev = ep->driver_data;
397 int status = req->status;
400 spin_lock_irqsave(&dev->lock, flags);
402 list_del_init(&req->list); /* Remode from Active List */
406 /* normal completion */
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);
412 list_add(&req->list, &dev->rx_reqs);
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);
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);
434 DBG(dev, "rx status %d\n", status);
435 list_add(&req->list, &dev->rx_reqs);
439 wake_up_interruptible(&dev->rx_wait);
440 spin_unlock_irqrestore(&dev->lock, flags);
443 static void tx_complete(struct usb_ep *ep, struct usb_request *req)
445 struct printer_dev *dev = ep->driver_data;
447 switch (req->status) {
449 VDBG(dev, "tx err %d\n", req->status);
451 case -ECONNRESET: /* unlink */
452 case -ESHUTDOWN: /* disconnect etc */
458 spin_lock(&dev->lock);
459 /* Take the request struct off the active list and put it on the
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);
468 spin_unlock(&dev->lock);
471 /*-------------------------------------------------------------------------*/
474 printer_open(struct inode *inode, struct file *fd)
476 struct printer_dev *dev;
480 mutex_lock(&printer_mutex);
481 dev = container_of(inode->i_cdev, struct printer_dev, printer_cdev);
483 spin_lock_irqsave(&dev->lock, flags);
485 if (!dev->printer_cdev_open) {
486 dev->printer_cdev_open = 1;
487 fd->private_data = dev;
489 /* Change the printer status to show that it's on-line. */
490 dev->printer_status |= PRINTER_SELECTED;
493 spin_unlock_irqrestore(&dev->lock, flags);
495 DBG(dev, "printer_open returned %x\n", ret);
496 mutex_unlock(&printer_mutex);
501 printer_close(struct inode *inode, struct file *fd)
503 struct printer_dev *dev = fd->private_data;
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);
513 DBG(dev, "printer_close\n");
518 /* This function must be called with interrupts turned off. */
520 setup_rx_reqs(struct printer_dev *dev)
522 struct usb_request *req;
524 while (likely(!list_empty(&dev->rx_reqs))) {
527 req = container_of(dev->rx_reqs.next,
528 struct usb_request, list);
529 list_del_init(&req->list);
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().
537 req->length = USB_BUFSIZE;
538 req->complete = rx_complete;
540 error = usb_ep_queue(dev->out_ep, req, GFP_ATOMIC);
542 DBG(dev, "rx submit --> %d\n", error);
543 list_add(&req->list, &dev->rx_reqs);
546 list_add(&req->list, &dev->rx_reqs_active);
552 printer_read(struct file *fd, char __user *buf, size_t len, loff_t *ptr)
554 struct printer_dev *dev = fd->private_data;
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. */
569 DBG(dev, "printer_read trying to read %d bytes\n", (int)len);
571 mutex_lock(&dev->lock_printer_io);
572 spin_lock_irqsave(&dev->lock, flags);
574 /* We will use this flag later to check if a printer reset happened
575 * after we turn interrupts back on.
577 dev->reset_printer = 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;
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
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);
601 * If no data is available check if this is a NON-Blocking
604 if (fd->f_flags & (O_NONBLOCK|O_NDELAY)) {
605 mutex_unlock(&dev->lock_printer_io);
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);
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)))
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);
623 if (req->actual && req->buf) {
624 current_rx_req = req;
625 current_rx_bytes = req->actual;
626 current_rx_buf = req->buf;
628 list_add(&req->list, &dev->rx_reqs);
633 /* Don't leave irqs off while doing memory copies */
634 spin_unlock_irqrestore(&dev->lock, flags);
636 if (len > current_rx_bytes)
637 size = current_rx_bytes;
641 size -= copy_to_user(buf, current_rx_buf, size);
642 bytes_copied += size;
646 spin_lock_irqsave(&dev->lock, flags);
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);
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.
661 if (size < current_rx_bytes) {
662 current_rx_bytes -= size;
663 current_rx_buf += size;
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;
672 dev->current_rx_req = current_rx_req;
673 dev->current_rx_bytes = current_rx_bytes;
674 dev->current_rx_buf = current_rx_buf;
676 spin_unlock_irqrestore(&dev->lock, flags);
677 mutex_unlock(&dev->lock_printer_io);
679 DBG(dev, "printer_read returned %d bytes\n", (int)bytes_copied);
688 printer_write(struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
690 struct printer_dev *dev = fd->private_data;
692 size_t size; /* Amount of data in a TX request. */
693 size_t bytes_copied = 0;
694 struct usb_request *req;
696 DBG(dev, "printer_write trying to send %d bytes\n", (int)len);
701 mutex_lock(&dev->lock_printer_io);
702 spin_lock_irqsave(&dev->lock, flags);
704 /* Check if a printer reset happens while we have interrupts on */
705 dev->reset_printer = 0;
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);
713 * If write buffers are available check if this is
714 * a NON-Blocking call or not.
716 if (fd->f_flags & (O_NONBLOCK|O_NDELAY)) {
717 mutex_unlock(&dev->lock_printer_io);
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);
727 while (likely(!list_empty(&dev->tx_reqs)) && len) {
729 if (len > USB_BUFSIZE)
734 req = container_of(dev->tx_reqs.next, struct usb_request,
736 list_del_init(&req->list);
738 req->complete = tx_complete;
741 /* Check if we need to send a zero length packet. */
743 /* They will be more TX requests so no yet. */
746 /* If the data amount is not a multple of the
747 * maxpacket size then send a zero length packet.
749 req->zero = ((len % dev->in_ep->maxpacket) == 0);
751 /* Don't leave irqs off while doing memory copies */
752 spin_unlock_irqrestore(&dev->lock, flags);
754 if (copy_from_user(req->buf, buf, size)) {
755 list_add(&req->list, &dev->tx_reqs);
756 mutex_unlock(&dev->lock_printer_io);
760 bytes_copied += size;
764 spin_lock_irqsave(&dev->lock, flags);
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);
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);
781 list_add(&req->list, &dev->tx_reqs_active);
785 spin_unlock_irqrestore(&dev->lock, flags);
786 mutex_unlock(&dev->lock_printer_io);
788 DBG(dev, "printer_write sent %d bytes\n", (int)bytes_copied);
798 printer_fsync(struct file *fd, int datasync)
800 struct printer_dev *dev = fd->private_data;
804 spin_lock_irqsave(&dev->lock, flags);
805 tx_list_empty = (likely(list_empty(&dev->tx_reqs)));
806 spin_unlock_irqrestore(&dev->lock, flags);
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))));
818 printer_poll(struct file *fd, poll_table *wait)
820 struct printer_dev *dev = fd->private_data;
824 mutex_lock(&dev->lock_printer_io);
825 spin_lock_irqsave(&dev->lock, flags);
827 spin_unlock_irqrestore(&dev->lock, flags);
828 mutex_unlock(&dev->lock_printer_io);
830 poll_wait(fd, &dev->rx_wait, wait);
831 poll_wait(fd, &dev->tx_wait, wait);
833 spin_lock_irqsave(&dev->lock, flags);
834 if (likely(!list_empty(&dev->tx_reqs)))
835 status |= POLLOUT | POLLWRNORM;
837 if (likely(dev->current_rx_bytes) ||
838 likely(!list_empty(&dev->rx_buffers)))
839 status |= POLLIN | POLLRDNORM;
841 spin_unlock_irqrestore(&dev->lock, flags);
847 printer_ioctl(struct file *fd, unsigned int code, unsigned long arg)
849 struct printer_dev *dev = fd->private_data;
853 DBG(dev, "printer_ioctl: cmd=0x%4.4x, arg=%lu\n", code, arg);
857 spin_lock_irqsave(&dev->lock, flags);
860 case GADGET_GET_PRINTER_STATUS:
861 status = (int)dev->printer_status;
863 case GADGET_SET_PRINTER_STATUS:
864 dev->printer_status = (u8)arg;
867 /* could not handle ioctl */
868 DBG(dev, "printer_ioctl: ERROR cmd=0x%4.4xis not supported\n",
873 spin_unlock_irqrestore(&dev->lock, flags);
878 /* used after endpoint configuration */
879 static 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 .llseek = noop_llseek,
891 /*-------------------------------------------------------------------------*/
894 set_printer_interface(struct printer_dev *dev)
898 dev->in = ep_desc(dev->gadget, &hs_ep_in_desc, &fs_ep_in_desc);
899 dev->in_ep->driver_data = dev;
901 dev->out = ep_desc(dev->gadget, &hs_ep_out_desc, &fs_ep_out_desc);
902 dev->out_ep->driver_data = dev;
904 result = usb_ep_enable(dev->in_ep, dev->in);
906 DBG(dev, "enable %s --> %d\n", dev->in_ep->name, result);
910 result = usb_ep_enable(dev->out_ep, dev->out);
912 DBG(dev, "enable %s --> %d\n", dev->in_ep->name, result);
917 /* on error, disable any endpoints */
919 (void) usb_ep_disable(dev->in_ep);
920 (void) usb_ep_disable(dev->out_ep);
925 /* caller is responsible for cleanup on error */
929 static void printer_reset_interface(struct printer_dev *dev)
931 if (dev->interface < 0)
934 DBG(dev, "%s\n", __func__);
937 usb_ep_disable(dev->in_ep);
940 usb_ep_disable(dev->out_ep);
945 /* change our operational config. must agree with the code
946 * that returns config descriptors, and altsetting code.
949 printer_set_config(struct printer_dev *dev, unsigned number)
952 struct usb_gadget *gadget = dev->gadget;
955 case DEV_CONFIG_VALUE:
966 usb_gadget_vbus_draw(dev->gadget,
967 dev->gadget->is_otg ? 8 : 100);
972 power = 2 * config_desc.bMaxPower;
973 usb_gadget_vbus_draw(dev->gadget, power);
975 switch (gadget->speed) {
976 case USB_SPEED_FULL: speed = "full"; break;
977 #ifdef CONFIG_USB_GADGET_DUALSPEED
978 case USB_SPEED_HIGH: speed = "high"; break;
980 default: speed = "?"; break;
983 dev->config = number;
984 INFO(dev, "%s speed config #%d: %d mA, %s\n",
985 speed, number, power, driver_desc);
991 config_buf(enum usb_device_speed speed, u8 *buf, u8 type, unsigned index,
995 const struct usb_descriptor_header **function;
996 #ifdef CONFIG_USB_GADGET_DUALSPEED
997 int hs = (speed == USB_SPEED_HIGH);
999 if (type == USB_DT_OTHER_SPEED_CONFIG)
1003 function = hs_printer_function;
1005 function = fs_printer_function;
1008 function = fs_printer_function;
1011 if (index >= device_desc.bNumConfigurations)
1014 /* for now, don't advertise srp-only devices */
1018 len = usb_gadget_config_buf(&config_desc, buf, USB_DESC_BUFSIZE,
1022 ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
1026 /* Change our operational Interface. */
1028 set_interface(struct printer_dev *dev, unsigned number)
1032 /* Free the current interface */
1033 switch (dev->interface) {
1034 case PRINTER_INTERFACE:
1035 printer_reset_interface(dev);
1040 case PRINTER_INTERFACE:
1041 result = set_printer_interface(dev);
1043 printer_reset_interface(dev);
1045 dev->interface = PRINTER_INTERFACE;
1054 INFO(dev, "Using interface %x\n", number);
1059 static void printer_setup_complete(struct usb_ep *ep, struct usb_request *req)
1061 if (req->status || req->actual != req->length)
1062 DBG((struct printer_dev *) ep->driver_data,
1063 "setup complete --> %d, %d/%d\n",
1064 req->status, req->actual, req->length);
1067 static void printer_soft_reset(struct printer_dev *dev)
1069 struct usb_request *req;
1071 INFO(dev, "Received Printer Reset Request\n");
1073 if (usb_ep_disable(dev->in_ep))
1074 DBG(dev, "Failed to disable USB in_ep\n");
1075 if (usb_ep_disable(dev->out_ep))
1076 DBG(dev, "Failed to disable USB out_ep\n");
1078 if (dev->current_rx_req != NULL) {
1079 list_add(&dev->current_rx_req->list, &dev->rx_reqs);
1080 dev->current_rx_req = NULL;
1082 dev->current_rx_bytes = 0;
1083 dev->current_rx_buf = NULL;
1084 dev->reset_printer = 1;
1086 while (likely(!(list_empty(&dev->rx_buffers)))) {
1087 req = container_of(dev->rx_buffers.next, struct usb_request,
1089 list_del_init(&req->list);
1090 list_add(&req->list, &dev->rx_reqs);
1093 while (likely(!(list_empty(&dev->rx_reqs_active)))) {
1094 req = container_of(dev->rx_buffers.next, struct usb_request,
1096 list_del_init(&req->list);
1097 list_add(&req->list, &dev->rx_reqs);
1100 while (likely(!(list_empty(&dev->tx_reqs_active)))) {
1101 req = container_of(dev->tx_reqs_active.next,
1102 struct usb_request, list);
1103 list_del_init(&req->list);
1104 list_add(&req->list, &dev->tx_reqs);
1107 if (usb_ep_enable(dev->in_ep, dev->in))
1108 DBG(dev, "Failed to enable USB in_ep\n");
1109 if (usb_ep_enable(dev->out_ep, dev->out))
1110 DBG(dev, "Failed to enable USB out_ep\n");
1112 wake_up_interruptible(&dev->rx_wait);
1113 wake_up_interruptible(&dev->tx_wait);
1114 wake_up_interruptible(&dev->tx_flush_wait);
1117 /*-------------------------------------------------------------------------*/
1120 * The setup() callback implements all the ep0 functionality that's not
1121 * handled lower down.
1124 printer_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1126 struct printer_dev *dev = get_gadget_data(gadget);
1127 struct usb_request *req = dev->req;
1128 int value = -EOPNOTSUPP;
1129 u16 wIndex = le16_to_cpu(ctrl->wIndex);
1130 u16 wValue = le16_to_cpu(ctrl->wValue);
1131 u16 wLength = le16_to_cpu(ctrl->wLength);
1133 DBG(dev, "ctrl req%02x.%02x v%04x i%04x l%d\n",
1134 ctrl->bRequestType, ctrl->bRequest, wValue, wIndex, wLength);
1136 req->complete = printer_setup_complete;
1138 switch (ctrl->bRequestType&USB_TYPE_MASK) {
1140 case USB_TYPE_STANDARD:
1141 switch (ctrl->bRequest) {
1143 case USB_REQ_GET_DESCRIPTOR:
1144 if (ctrl->bRequestType != USB_DIR_IN)
1146 switch (wValue >> 8) {
1149 value = min(wLength, (u16) sizeof device_desc);
1150 memcpy(req->buf, &device_desc, value);
1152 #ifdef CONFIG_USB_GADGET_DUALSPEED
1153 case USB_DT_DEVICE_QUALIFIER:
1154 if (!gadget->is_dualspeed)
1156 value = min(wLength,
1157 (u16) sizeof dev_qualifier);
1158 memcpy(req->buf, &dev_qualifier, value);
1161 case USB_DT_OTHER_SPEED_CONFIG:
1162 if (!gadget->is_dualspeed)
1165 #endif /* CONFIG_USB_GADGET_DUALSPEED */
1167 value = config_buf(gadget->speed, req->buf,
1172 value = min(wLength, (u16) value);
1176 value = usb_gadget_get_string(&stringtab,
1177 wValue & 0xff, req->buf);
1179 value = min(wLength, (u16) value);
1184 case USB_REQ_SET_CONFIGURATION:
1185 if (ctrl->bRequestType != 0)
1187 if (gadget->a_hnp_support)
1188 DBG(dev, "HNP available\n");
1189 else if (gadget->a_alt_hnp_support)
1190 DBG(dev, "HNP needs a different root port\n");
1191 value = printer_set_config(dev, wValue);
1193 value = set_interface(dev, PRINTER_INTERFACE);
1195 case USB_REQ_GET_CONFIGURATION:
1196 if (ctrl->bRequestType != USB_DIR_IN)
1198 *(u8 *)req->buf = dev->config;
1199 value = min(wLength, (u16) 1);
1202 case USB_REQ_SET_INTERFACE:
1203 if (ctrl->bRequestType != USB_RECIP_INTERFACE ||
1207 value = set_interface(dev, PRINTER_INTERFACE);
1209 case USB_REQ_GET_INTERFACE:
1210 if (ctrl->bRequestType !=
1211 (USB_DIR_IN|USB_RECIP_INTERFACE)
1215 *(u8 *)req->buf = dev->interface;
1216 value = min(wLength, (u16) 1);
1224 case USB_TYPE_CLASS:
1225 switch (ctrl->bRequest) {
1226 case 0: /* Get the IEEE-1284 PNP String */
1227 /* Only one printer interface is supported. */
1228 if ((wIndex>>8) != PRINTER_INTERFACE)
1231 value = (pnp_string[0]<<8)|pnp_string[1];
1232 memcpy(req->buf, pnp_string, value);
1233 DBG(dev, "1284 PNP String: %x %s\n", value,
1237 case 1: /* Get Port Status */
1238 /* Only one printer interface is supported. */
1239 if (wIndex != PRINTER_INTERFACE)
1242 *(u8 *)req->buf = dev->printer_status;
1243 value = min(wLength, (u16) 1);
1246 case 2: /* Soft Reset */
1247 /* Only one printer interface is supported. */
1248 if (wIndex != PRINTER_INTERFACE)
1251 printer_soft_reset(dev);
1264 "unknown ctrl req%02x.%02x v%04x i%04x l%d\n",
1265 ctrl->bRequestType, ctrl->bRequest,
1266 wValue, wIndex, wLength);
1270 /* respond with data transfer before status phase? */
1272 req->length = value;
1273 req->zero = value < wLength;
1274 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
1276 DBG(dev, "ep_queue --> %d\n", value);
1278 printer_setup_complete(gadget->ep0, req);
1282 /* host either stalls (value < 0) or reports success */
1287 printer_disconnect(struct usb_gadget *gadget)
1289 struct printer_dev *dev = get_gadget_data(gadget);
1290 unsigned long flags;
1292 DBG(dev, "%s\n", __func__);
1294 spin_lock_irqsave(&dev->lock, flags);
1296 printer_reset_interface(dev);
1298 spin_unlock_irqrestore(&dev->lock, flags);
1302 printer_unbind(struct usb_gadget *gadget)
1304 struct printer_dev *dev = get_gadget_data(gadget);
1305 struct usb_request *req;
1308 DBG(dev, "%s\n", __func__);
1310 /* Remove sysfs files */
1311 device_destroy(usb_gadget_class, g_printer_devno);
1313 /* Remove Character Device */
1314 cdev_del(&dev->printer_cdev);
1316 /* we must already have been disconnected ... no i/o may be active */
1317 WARN_ON(!list_empty(&dev->tx_reqs_active));
1318 WARN_ON(!list_empty(&dev->rx_reqs_active));
1320 /* Free all memory for this driver. */
1321 while (!list_empty(&dev->tx_reqs)) {
1322 req = container_of(dev->tx_reqs.next, struct usb_request,
1324 list_del(&req->list);
1325 printer_req_free(dev->in_ep, req);
1328 if (dev->current_rx_req != NULL)
1329 printer_req_free(dev->out_ep, dev->current_rx_req);
1331 while (!list_empty(&dev->rx_reqs)) {
1332 req = container_of(dev->rx_reqs.next,
1333 struct usb_request, list);
1334 list_del(&req->list);
1335 printer_req_free(dev->out_ep, req);
1338 while (!list_empty(&dev->rx_buffers)) {
1339 req = container_of(dev->rx_buffers.next,
1340 struct usb_request, list);
1341 list_del(&req->list);
1342 printer_req_free(dev->out_ep, req);
1346 printer_req_free(gadget->ep0, dev->req);
1350 set_gadget_data(gadget, NULL);
1354 printer_bind(struct usb_gadget *gadget)
1356 struct printer_dev *dev;
1357 struct usb_ep *in_ep, *out_ep;
1358 int status = -ENOMEM;
1362 struct usb_request *req;
1364 dev = &usb_printer_gadget;
1367 /* Setup the sysfs files for the printer gadget. */
1368 dev->pdev = device_create(usb_gadget_class, NULL, g_printer_devno,
1370 if (IS_ERR(dev->pdev)) {
1371 ERROR(dev, "Failed to create device: g_printer\n");
1376 * Register a character device as an interface to a user mode
1377 * program that handles the printer specific functionality.
1379 cdev_init(&dev->printer_cdev, &printer_io_operations);
1380 dev->printer_cdev.owner = THIS_MODULE;
1381 status = cdev_add(&dev->printer_cdev, g_printer_devno, 1);
1383 ERROR(dev, "Failed to open char device\n");
1387 gcnum = usb_gadget_controller_number(gadget);
1389 device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
1391 dev_warn(&gadget->dev, "controller '%s' not recognized\n",
1393 /* unrecognized, but safe unless bulk is REALLY quirky */
1394 device_desc.bcdDevice =
1395 cpu_to_le16(0xFFFF);
1397 snprintf(manufacturer, sizeof(manufacturer), "%s %s with %s",
1398 init_utsname()->sysname, init_utsname()->release,
1401 device_desc.idVendor =
1402 cpu_to_le16(PRINTER_VENDOR_NUM);
1403 device_desc.idProduct =
1404 cpu_to_le16(PRINTER_PRODUCT_NUM);
1406 /* support optional vendor/distro customization */
1409 dev_err(&gadget->dev, "idVendor needs idProduct!\n");
1412 device_desc.idVendor = cpu_to_le16(idVendor);
1413 device_desc.idProduct = cpu_to_le16(idProduct);
1415 device_desc.bcdDevice = cpu_to_le16(bcdDevice);
1419 strlcpy(manufacturer, iManufacturer, sizeof manufacturer);
1422 strlcpy(product_desc, iProduct, sizeof product_desc);
1425 strlcpy(serial_num, iSerialNum, sizeof serial_num);
1428 strlcpy(&pnp_string[2], iPNPstring, (sizeof pnp_string)-2);
1430 len = strlen(pnp_string);
1431 pnp_string[0] = (len >> 8) & 0xFF;
1432 pnp_string[1] = len & 0xFF;
1434 /* all we really need is bulk IN/OUT */
1435 usb_ep_autoconfig_reset(gadget);
1436 in_ep = usb_ep_autoconfig(gadget, &fs_ep_in_desc);
1439 dev_err(&gadget->dev, "can't autoconfigure on %s\n",
1443 in_ep->driver_data = in_ep; /* claim */
1445 out_ep = usb_ep_autoconfig(gadget, &fs_ep_out_desc);
1448 out_ep->driver_data = out_ep; /* claim */
1450 #ifdef CONFIG_USB_GADGET_DUALSPEED
1451 /* assumes ep0 uses the same value for both speeds ... */
1452 dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
1454 /* and that all endpoints are dual-speed */
1455 hs_ep_in_desc.bEndpointAddress = fs_ep_in_desc.bEndpointAddress;
1456 hs_ep_out_desc.bEndpointAddress = fs_ep_out_desc.bEndpointAddress;
1457 #endif /* DUALSPEED */
1459 device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
1460 usb_gadget_set_selfpowered(gadget);
1462 if (gadget->is_otg) {
1463 otg_desc.bmAttributes |= USB_OTG_HNP,
1464 config_desc.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1467 spin_lock_init(&dev->lock);
1468 mutex_init(&dev->lock_printer_io);
1469 INIT_LIST_HEAD(&dev->tx_reqs);
1470 INIT_LIST_HEAD(&dev->tx_reqs_active);
1471 INIT_LIST_HEAD(&dev->rx_reqs);
1472 INIT_LIST_HEAD(&dev->rx_reqs_active);
1473 INIT_LIST_HEAD(&dev->rx_buffers);
1474 init_waitqueue_head(&dev->rx_wait);
1475 init_waitqueue_head(&dev->tx_wait);
1476 init_waitqueue_head(&dev->tx_flush_wait);
1479 dev->interface = -1;
1480 dev->printer_cdev_open = 0;
1481 dev->printer_status = PRINTER_NOT_ERROR;
1482 dev->current_rx_req = NULL;
1483 dev->current_rx_bytes = 0;
1484 dev->current_rx_buf = NULL;
1487 dev->out_ep = out_ep;
1489 /* preallocate control message data and buffer */
1490 dev->req = printer_req_alloc(gadget->ep0, USB_DESC_BUFSIZE,
1497 for (i = 0; i < QLEN; i++) {
1498 req = printer_req_alloc(dev->in_ep, USB_BUFSIZE, GFP_KERNEL);
1500 while (!list_empty(&dev->tx_reqs)) {
1501 req = container_of(dev->tx_reqs.next,
1502 struct usb_request, list);
1503 list_del(&req->list);
1504 printer_req_free(dev->in_ep, req);
1508 list_add(&req->list, &dev->tx_reqs);
1511 for (i = 0; i < QLEN; i++) {
1512 req = printer_req_alloc(dev->out_ep, USB_BUFSIZE, GFP_KERNEL);
1514 while (!list_empty(&dev->rx_reqs)) {
1515 req = container_of(dev->rx_reqs.next,
1516 struct usb_request, list);
1517 list_del(&req->list);
1518 printer_req_free(dev->out_ep, req);
1522 list_add(&req->list, &dev->rx_reqs);
1525 dev->req->complete = printer_setup_complete;
1527 /* finish hookup to lower layer ... */
1528 dev->gadget = gadget;
1529 set_gadget_data(gadget, dev);
1530 gadget->ep0->driver_data = dev;
1532 INFO(dev, "%s, version: " DRIVER_VERSION "\n", driver_desc);
1533 INFO(dev, "using %s, OUT %s IN %s\n", gadget->name, out_ep->name,
1539 printer_unbind(gadget);
1543 /*-------------------------------------------------------------------------*/
1545 static struct usb_gadget_driver printer_driver = {
1548 .function = (char *) driver_desc,
1549 .unbind = printer_unbind,
1551 .setup = printer_setup,
1552 .disconnect = printer_disconnect,
1555 .name = (char *) shortname,
1556 .owner = THIS_MODULE,
1560 MODULE_DESCRIPTION(DRIVER_DESC);
1561 MODULE_AUTHOR("Craig Nadler");
1562 MODULE_LICENSE("GPL");
1569 usb_gadget_class = class_create(THIS_MODULE, "usb_printer_gadget");
1570 if (IS_ERR(usb_gadget_class)) {
1571 status = PTR_ERR(usb_gadget_class);
1572 ERROR(dev, "unable to create usb_gadget class %d\n", status);
1576 status = alloc_chrdev_region(&g_printer_devno, 0, 1,
1577 "USB printer gadget");
1579 ERROR(dev, "alloc_chrdev_region %d\n", status);
1580 class_destroy(usb_gadget_class);
1584 status = usb_gadget_probe_driver(&printer_driver, printer_bind);
1586 class_destroy(usb_gadget_class);
1587 unregister_chrdev_region(g_printer_devno, 1);
1588 DBG(dev, "usb_gadget_probe_driver %x\n", status);
1600 mutex_lock(&usb_printer_gadget.lock_printer_io);
1601 status = usb_gadget_unregister_driver(&printer_driver);
1603 ERROR(dev, "usb_gadget_unregister_driver %x\n", status);
1605 unregister_chrdev_region(g_printer_devno, 2);
1606 class_destroy(usb_gadget_class);
1607 mutex_unlock(&usb_printer_gadget.lock_printer_io);
1609 module_exit(cleanup);