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;
93 struct list_head rx_reqs; /* List of free RX structs */
94 struct list_head rx_reqs_active; /* List of Active RX xfers */
95 struct list_head rx_buffers; /* List of completed xfers */
96 /* wait until there is data to be read. */
97 wait_queue_head_t rx_wait;
98 struct list_head tx_reqs; /* List of free TX structs */
99 struct list_head tx_reqs_active; /* List of Active TX xfers */
100 /* Wait until there are write buffers available to use. */
101 wait_queue_head_t tx_wait;
102 /* Wait until all write buffers have been sent. */
103 wait_queue_head_t tx_flush_wait;
104 struct usb_request *current_rx_req;
105 size_t current_rx_bytes;
109 struct cdev printer_cdev;
111 u8 printer_cdev_open;
112 wait_queue_head_t wait;
115 static struct printer_dev usb_printer_gadget;
117 /*-------------------------------------------------------------------------*/
119 /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
120 * Instead: allocate your own, using normal USB-IF procedures.
123 /* Thanks to NetChip Technologies for donating this product ID.
125 #define PRINTER_VENDOR_NUM 0x0525 /* NetChip */
126 #define PRINTER_PRODUCT_NUM 0xa4a8 /* Linux-USB Printer Gadget */
128 /* Some systems will want different product identifiers published in the
129 * device descriptor, either numbers or strings or both. These string
130 * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
133 static ushort idVendor;
134 module_param(idVendor, ushort, S_IRUGO);
135 MODULE_PARM_DESC(idVendor, "USB Vendor ID");
137 static ushort idProduct;
138 module_param(idProduct, ushort, S_IRUGO);
139 MODULE_PARM_DESC(idProduct, "USB Product ID");
141 static ushort bcdDevice;
142 module_param(bcdDevice, ushort, S_IRUGO);
143 MODULE_PARM_DESC(bcdDevice, "USB Device version (BCD)");
145 static char *iManufacturer;
146 module_param(iManufacturer, charp, S_IRUGO);
147 MODULE_PARM_DESC(iManufacturer, "USB Manufacturer string");
149 static char *iProduct;
150 module_param(iProduct, charp, S_IRUGO);
151 MODULE_PARM_DESC(iProduct, "USB Product string");
153 static char *iSerialNum;
154 module_param(iSerialNum, charp, S_IRUGO);
155 MODULE_PARM_DESC(iSerialNum, "1");
157 static char *iPNPstring;
158 module_param(iPNPstring, charp, S_IRUGO);
159 MODULE_PARM_DESC(iPNPstring, "MFG:linux;MDL:g_printer;CLS:PRINTER;SN:1;");
161 /* Number of requests to allocate per endpoint, not used for ep0. */
162 static unsigned qlen = 10;
163 module_param(qlen, uint, S_IRUGO|S_IWUSR);
167 #ifdef CONFIG_USB_GADGET_DUALSPEED
168 #define DEVSPEED USB_SPEED_HIGH
169 #else /* full speed (low speed doesn't do bulk) */
170 #define DEVSPEED USB_SPEED_FULL
173 /*-------------------------------------------------------------------------*/
175 #define xprintk(d, level, fmt, args...) \
176 printk(level "%s: " fmt, DRIVER_DESC, ## args)
179 #define DBG(dev, fmt, args...) \
180 xprintk(dev, KERN_DEBUG, fmt, ## args)
182 #define DBG(dev, fmt, args...) \
187 #define VDBG(dev, fmt, args...) \
188 xprintk(dev, KERN_DEBUG, fmt, ## args)
190 #define VDBG(dev, fmt, args...) \
194 #define ERROR(dev, fmt, args...) \
195 xprintk(dev, KERN_ERR, fmt, ## args)
196 #define WARNING(dev, fmt, args...) \
197 xprintk(dev, KERN_WARNING, fmt, ## args)
198 #define INFO(dev, fmt, args...) \
199 xprintk(dev, KERN_INFO, fmt, ## args)
201 /*-------------------------------------------------------------------------*/
203 /* USB DRIVER HOOKUP (to the hardware driver, below us), mostly
204 * ep0 implementation: descriptors, config management, setup().
205 * also optional class-specific notification interrupt transfer.
209 * DESCRIPTORS ... most are static, but strings and (full) configuration
210 * descriptors are built on demand.
213 #define STRING_MANUFACTURER 1
214 #define STRING_PRODUCT 2
215 #define STRING_SERIALNUM 3
217 /* holds our biggest descriptor */
218 #define USB_DESC_BUFSIZE 256
219 #define USB_BUFSIZE 8192
221 /* This device advertises one configuration. */
222 #define DEV_CONFIG_VALUE 1
223 #define PRINTER_INTERFACE 0
225 static struct usb_device_descriptor device_desc = {
226 .bLength = sizeof device_desc,
227 .bDescriptorType = USB_DT_DEVICE,
228 .bcdUSB = cpu_to_le16(0x0200),
229 .bDeviceClass = USB_CLASS_PER_INTERFACE,
230 .bDeviceSubClass = 0,
231 .bDeviceProtocol = 0,
232 .idVendor = cpu_to_le16(PRINTER_VENDOR_NUM),
233 .idProduct = cpu_to_le16(PRINTER_PRODUCT_NUM),
234 .iManufacturer = STRING_MANUFACTURER,
235 .iProduct = STRING_PRODUCT,
236 .iSerialNumber = STRING_SERIALNUM,
237 .bNumConfigurations = 1
240 static struct usb_otg_descriptor otg_desc = {
241 .bLength = sizeof otg_desc,
242 .bDescriptorType = USB_DT_OTG,
243 .bmAttributes = USB_OTG_SRP
246 static struct usb_config_descriptor config_desc = {
247 .bLength = sizeof config_desc,
248 .bDescriptorType = USB_DT_CONFIG,
250 /* compute wTotalLength on the fly */
252 .bConfigurationValue = DEV_CONFIG_VALUE,
254 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
255 .bMaxPower = CONFIG_USB_GADGET_VBUS_DRAW / 2,
258 static struct usb_interface_descriptor intf_desc = {
259 .bLength = sizeof intf_desc,
260 .bDescriptorType = USB_DT_INTERFACE,
261 .bInterfaceNumber = PRINTER_INTERFACE,
263 .bInterfaceClass = USB_CLASS_PRINTER,
264 .bInterfaceSubClass = 1, /* Printer Sub-Class */
265 .bInterfaceProtocol = 2, /* Bi-Directional */
269 static struct usb_endpoint_descriptor fs_ep_in_desc = {
270 .bLength = USB_DT_ENDPOINT_SIZE,
271 .bDescriptorType = USB_DT_ENDPOINT,
272 .bEndpointAddress = USB_DIR_IN,
273 .bmAttributes = USB_ENDPOINT_XFER_BULK
276 static struct usb_endpoint_descriptor fs_ep_out_desc = {
277 .bLength = USB_DT_ENDPOINT_SIZE,
278 .bDescriptorType = USB_DT_ENDPOINT,
279 .bEndpointAddress = USB_DIR_OUT,
280 .bmAttributes = USB_ENDPOINT_XFER_BULK
283 static const struct usb_descriptor_header *fs_printer_function [11] = {
284 (struct usb_descriptor_header *) &otg_desc,
285 (struct usb_descriptor_header *) &intf_desc,
286 (struct usb_descriptor_header *) &fs_ep_in_desc,
287 (struct usb_descriptor_header *) &fs_ep_out_desc,
291 #ifdef CONFIG_USB_GADGET_DUALSPEED
294 * usb 2.0 devices need to expose both high speed and full speed
295 * descriptors, unless they only run at full speed.
298 static struct usb_endpoint_descriptor hs_ep_in_desc = {
299 .bLength = USB_DT_ENDPOINT_SIZE,
300 .bDescriptorType = USB_DT_ENDPOINT,
301 .bmAttributes = USB_ENDPOINT_XFER_BULK,
302 .wMaxPacketSize = cpu_to_le16(512)
305 static struct usb_endpoint_descriptor hs_ep_out_desc = {
306 .bLength = USB_DT_ENDPOINT_SIZE,
307 .bDescriptorType = USB_DT_ENDPOINT,
308 .bmAttributes = USB_ENDPOINT_XFER_BULK,
309 .wMaxPacketSize = cpu_to_le16(512)
312 static struct usb_qualifier_descriptor dev_qualifier = {
313 .bLength = sizeof dev_qualifier,
314 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
315 .bcdUSB = cpu_to_le16(0x0200),
316 .bDeviceClass = USB_CLASS_PRINTER,
317 .bNumConfigurations = 1
320 static const struct usb_descriptor_header *hs_printer_function [11] = {
321 (struct usb_descriptor_header *) &otg_desc,
322 (struct usb_descriptor_header *) &intf_desc,
323 (struct usb_descriptor_header *) &hs_ep_in_desc,
324 (struct usb_descriptor_header *) &hs_ep_out_desc,
328 /* maxpacket and other transfer characteristics vary by speed. */
329 #define ep_desc(g, hs, fs) (((g)->speed == USB_SPEED_HIGH)?(hs):(fs))
333 /* if there's no high speed support, maxpacket doesn't change. */
334 #define ep_desc(g, hs, fs) (((void)(g)), (fs))
336 #endif /* !CONFIG_USB_GADGET_DUALSPEED */
338 /*-------------------------------------------------------------------------*/
340 /* descriptors that are built on-demand */
342 static char manufacturer [50];
343 static char product_desc [40] = DRIVER_DESC;
344 static char serial_num [40] = "1";
345 static char pnp_string [1024] =
346 "XXMFG:linux;MDL:g_printer;CLS:PRINTER;SN:1;";
348 /* static strings, in UTF-8 */
349 static struct usb_string strings [] = {
350 { STRING_MANUFACTURER, manufacturer, },
351 { STRING_PRODUCT, product_desc, },
352 { STRING_SERIALNUM, serial_num, },
353 { } /* end of list */
356 static struct usb_gadget_strings stringtab = {
357 .language = 0x0409, /* en-us */
361 /*-------------------------------------------------------------------------*/
363 static struct usb_request *
364 printer_req_alloc(struct usb_ep *ep, unsigned len, gfp_t gfp_flags)
366 struct usb_request *req;
368 req = usb_ep_alloc_request(ep, gfp_flags);
372 req->buf = kmalloc(len, gfp_flags);
373 if (req->buf == NULL) {
374 usb_ep_free_request(ep, req);
383 printer_req_free(struct usb_ep *ep, struct usb_request *req)
385 if (ep != NULL && req != NULL) {
387 usb_ep_free_request(ep, req);
391 /*-------------------------------------------------------------------------*/
393 static void rx_complete(struct usb_ep *ep, struct usb_request *req)
395 struct printer_dev *dev = ep->driver_data;
396 int status = req->status;
399 spin_lock_irqsave(&dev->lock, flags);
401 list_del_init(&req->list); /* Remode from Active List */
405 /* normal completion */
407 if (req->actual > 0) {
408 list_add_tail(&req->list, &dev->rx_buffers);
409 DBG(dev, "G_Printer : rx length %d\n", req->actual);
411 list_add(&req->list, &dev->rx_reqs);
415 /* software-driven interface shutdown */
416 case -ECONNRESET: /* unlink */
417 case -ESHUTDOWN: /* disconnect etc */
418 VDBG(dev, "rx shutdown, code %d\n", status);
419 list_add(&req->list, &dev->rx_reqs);
422 /* for hardware automagic (such as pxa) */
423 case -ECONNABORTED: /* endpoint reset */
424 DBG(dev, "rx %s reset\n", ep->name);
425 list_add(&req->list, &dev->rx_reqs);
433 DBG(dev, "rx status %d\n", status);
434 list_add(&req->list, &dev->rx_reqs);
438 wake_up_interruptible(&dev->rx_wait);
439 spin_unlock_irqrestore(&dev->lock, flags);
442 static void tx_complete(struct usb_ep *ep, struct usb_request *req)
444 struct printer_dev *dev = ep->driver_data;
446 switch (req->status) {
448 VDBG(dev, "tx err %d\n", req->status);
450 case -ECONNRESET: /* unlink */
451 case -ESHUTDOWN: /* disconnect etc */
457 spin_lock(&dev->lock);
458 /* Take the request struct off the active list and put it on the
461 list_del_init(&req->list);
462 list_add(&req->list, &dev->tx_reqs);
463 wake_up_interruptible(&dev->tx_wait);
464 if (likely(list_empty(&dev->tx_reqs_active)))
465 wake_up_interruptible(&dev->tx_flush_wait);
467 spin_unlock(&dev->lock);
470 /*-------------------------------------------------------------------------*/
473 printer_open(struct inode *inode, struct file *fd)
475 struct printer_dev *dev;
479 mutex_lock(&printer_mutex);
480 dev = container_of(inode->i_cdev, struct printer_dev, printer_cdev);
482 spin_lock_irqsave(&dev->lock, flags);
484 if (!dev->printer_cdev_open) {
485 dev->printer_cdev_open = 1;
486 fd->private_data = dev;
488 /* Change the printer status to show that it's on-line. */
489 dev->printer_status |= PRINTER_SELECTED;
492 spin_unlock_irqrestore(&dev->lock, flags);
494 DBG(dev, "printer_open returned %x\n", ret);
495 mutex_unlock(&printer_mutex);
500 printer_close(struct inode *inode, struct file *fd)
502 struct printer_dev *dev = fd->private_data;
505 spin_lock_irqsave(&dev->lock, flags);
506 dev->printer_cdev_open = 0;
507 fd->private_data = NULL;
508 /* Change printer status to show that the printer is off-line. */
509 dev->printer_status &= ~PRINTER_SELECTED;
510 spin_unlock_irqrestore(&dev->lock, flags);
512 DBG(dev, "printer_close\n");
517 /* This function must be called with interrupts turned off. */
519 setup_rx_reqs(struct printer_dev *dev)
521 struct usb_request *req;
523 while (likely(!list_empty(&dev->rx_reqs))) {
526 req = container_of(dev->rx_reqs.next,
527 struct usb_request, list);
528 list_del_init(&req->list);
530 /* The USB Host sends us whatever amount of data it wants to
531 * so we always set the length field to the full USB_BUFSIZE.
532 * If the amount of data is more than the read() caller asked
533 * for it will be stored in the request buffer until it is
534 * asked for by read().
536 req->length = USB_BUFSIZE;
537 req->complete = rx_complete;
539 error = usb_ep_queue(dev->out_ep, req, GFP_ATOMIC);
541 DBG(dev, "rx submit --> %d\n", error);
542 list_add(&req->list, &dev->rx_reqs);
545 list_add(&req->list, &dev->rx_reqs_active);
551 printer_read(struct file *fd, char __user *buf, size_t len, loff_t *ptr)
553 struct printer_dev *dev = fd->private_data;
557 struct usb_request *req;
558 /* This is a pointer to the current USB rx request. */
559 struct usb_request *current_rx_req;
560 /* This is the number of bytes in the current rx buffer. */
561 size_t current_rx_bytes;
562 /* This is a pointer to the current rx buffer. */
568 DBG(dev, "printer_read trying to read %d bytes\n", (int)len);
570 mutex_lock(&dev->lock_printer_io);
571 spin_lock_irqsave(&dev->lock, flags);
573 /* We will use this flag later to check if a printer reset happened
574 * after we turn interrupts back on.
576 dev->reset_printer = 0;
581 current_rx_req = dev->current_rx_req;
582 current_rx_bytes = dev->current_rx_bytes;
583 current_rx_buf = dev->current_rx_buf;
584 dev->current_rx_req = NULL;
585 dev->current_rx_bytes = 0;
586 dev->current_rx_buf = NULL;
588 /* Check if there is any data in the read buffers. Please note that
589 * current_rx_bytes is the number of bytes in the current rx buffer.
590 * If it is zero then check if there are any other rx_buffers that
591 * are on the completed list. We are only out of data if all rx
594 if ((current_rx_bytes == 0) &&
595 (likely(list_empty(&dev->rx_buffers)))) {
596 /* Turn interrupts back on before sleeping. */
597 spin_unlock_irqrestore(&dev->lock, flags);
600 * If no data is available check if this is a NON-Blocking
603 if (fd->f_flags & (O_NONBLOCK|O_NDELAY)) {
604 mutex_unlock(&dev->lock_printer_io);
608 /* Sleep until data is available */
609 wait_event_interruptible(dev->rx_wait,
610 (likely(!list_empty(&dev->rx_buffers))));
611 spin_lock_irqsave(&dev->lock, flags);
614 /* We have data to return then copy it to the caller's buffer.*/
615 while ((current_rx_bytes || likely(!list_empty(&dev->rx_buffers)))
617 if (current_rx_bytes == 0) {
618 req = container_of(dev->rx_buffers.next,
619 struct usb_request, list);
620 list_del_init(&req->list);
622 if (req->actual && req->buf) {
623 current_rx_req = req;
624 current_rx_bytes = req->actual;
625 current_rx_buf = req->buf;
627 list_add(&req->list, &dev->rx_reqs);
632 /* Don't leave irqs off while doing memory copies */
633 spin_unlock_irqrestore(&dev->lock, flags);
635 if (len > current_rx_bytes)
636 size = current_rx_bytes;
640 size -= copy_to_user(buf, current_rx_buf, size);
641 bytes_copied += size;
645 spin_lock_irqsave(&dev->lock, flags);
647 /* We've disconnected or reset so return. */
648 if (dev->reset_printer) {
649 list_add(¤t_rx_req->list, &dev->rx_reqs);
650 spin_unlock_irqrestore(&dev->lock, flags);
651 mutex_unlock(&dev->lock_printer_io);
655 /* If we not returning all the data left in this RX request
656 * buffer then adjust the amount of data left in the buffer.
657 * Othewise if we are done with this RX request buffer then
658 * requeue it to get any incoming data from the USB host.
660 if (size < current_rx_bytes) {
661 current_rx_bytes -= size;
662 current_rx_buf += size;
664 list_add(¤t_rx_req->list, &dev->rx_reqs);
665 current_rx_bytes = 0;
666 current_rx_buf = NULL;
667 current_rx_req = NULL;
671 dev->current_rx_req = current_rx_req;
672 dev->current_rx_bytes = current_rx_bytes;
673 dev->current_rx_buf = current_rx_buf;
675 spin_unlock_irqrestore(&dev->lock, flags);
676 mutex_unlock(&dev->lock_printer_io);
678 DBG(dev, "printer_read returned %d bytes\n", (int)bytes_copied);
687 printer_write(struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
689 struct printer_dev *dev = fd->private_data;
691 size_t size; /* Amount of data in a TX request. */
692 size_t bytes_copied = 0;
693 struct usb_request *req;
695 DBG(dev, "printer_write trying to send %d bytes\n", (int)len);
700 mutex_lock(&dev->lock_printer_io);
701 spin_lock_irqsave(&dev->lock, flags);
703 /* Check if a printer reset happens while we have interrupts on */
704 dev->reset_printer = 0;
706 /* Check if there is any available write buffers */
707 if (likely(list_empty(&dev->tx_reqs))) {
708 /* Turn interrupts back on before sleeping. */
709 spin_unlock_irqrestore(&dev->lock, flags);
712 * If write buffers are available check if this is
713 * a NON-Blocking call or not.
715 if (fd->f_flags & (O_NONBLOCK|O_NDELAY)) {
716 mutex_unlock(&dev->lock_printer_io);
720 /* Sleep until a write buffer is available */
721 wait_event_interruptible(dev->tx_wait,
722 (likely(!list_empty(&dev->tx_reqs))));
723 spin_lock_irqsave(&dev->lock, flags);
726 while (likely(!list_empty(&dev->tx_reqs)) && len) {
728 if (len > USB_BUFSIZE)
733 req = container_of(dev->tx_reqs.next, struct usb_request,
735 list_del_init(&req->list);
737 req->complete = tx_complete;
740 /* Check if we need to send a zero length packet. */
742 /* They will be more TX requests so no yet. */
745 /* If the data amount is not a multple of the
746 * maxpacket size then send a zero length packet.
748 req->zero = ((len % dev->in_ep->maxpacket) == 0);
750 /* Don't leave irqs off while doing memory copies */
751 spin_unlock_irqrestore(&dev->lock, flags);
753 if (copy_from_user(req->buf, buf, size)) {
754 list_add(&req->list, &dev->tx_reqs);
755 mutex_unlock(&dev->lock_printer_io);
759 bytes_copied += size;
763 spin_lock_irqsave(&dev->lock, flags);
765 /* We've disconnected or reset so free the req and buffer */
766 if (dev->reset_printer) {
767 list_add(&req->list, &dev->tx_reqs);
768 spin_unlock_irqrestore(&dev->lock, flags);
769 mutex_unlock(&dev->lock_printer_io);
773 if (usb_ep_queue(dev->in_ep, req, GFP_ATOMIC)) {
774 list_add(&req->list, &dev->tx_reqs);
775 spin_unlock_irqrestore(&dev->lock, flags);
776 mutex_unlock(&dev->lock_printer_io);
780 list_add(&req->list, &dev->tx_reqs_active);
784 spin_unlock_irqrestore(&dev->lock, flags);
785 mutex_unlock(&dev->lock_printer_io);
787 DBG(dev, "printer_write sent %d bytes\n", (int)bytes_copied);
797 printer_fsync(struct file *fd, loff_t start, loff_t end, int datasync)
799 struct printer_dev *dev = fd->private_data;
800 struct inode *inode = fd->f_path.dentry->d_inode;
804 mutex_lock(&inode->i_mutex);
805 spin_lock_irqsave(&dev->lock, flags);
806 tx_list_empty = (likely(list_empty(&dev->tx_reqs)));
807 spin_unlock_irqrestore(&dev->lock, flags);
809 if (!tx_list_empty) {
810 /* Sleep until all data has been sent */
811 wait_event_interruptible(dev->tx_flush_wait,
812 (likely(list_empty(&dev->tx_reqs_active))));
814 mutex_unlock(&inode->i_mutex);
820 printer_poll(struct file *fd, poll_table *wait)
822 struct printer_dev *dev = fd->private_data;
826 mutex_lock(&dev->lock_printer_io);
827 spin_lock_irqsave(&dev->lock, flags);
829 spin_unlock_irqrestore(&dev->lock, flags);
830 mutex_unlock(&dev->lock_printer_io);
832 poll_wait(fd, &dev->rx_wait, wait);
833 poll_wait(fd, &dev->tx_wait, wait);
835 spin_lock_irqsave(&dev->lock, flags);
836 if (likely(!list_empty(&dev->tx_reqs)))
837 status |= POLLOUT | POLLWRNORM;
839 if (likely(dev->current_rx_bytes) ||
840 likely(!list_empty(&dev->rx_buffers)))
841 status |= POLLIN | POLLRDNORM;
843 spin_unlock_irqrestore(&dev->lock, flags);
849 printer_ioctl(struct file *fd, unsigned int code, unsigned long arg)
851 struct printer_dev *dev = fd->private_data;
855 DBG(dev, "printer_ioctl: cmd=0x%4.4x, arg=%lu\n", code, arg);
859 spin_lock_irqsave(&dev->lock, flags);
862 case GADGET_GET_PRINTER_STATUS:
863 status = (int)dev->printer_status;
865 case GADGET_SET_PRINTER_STATUS:
866 dev->printer_status = (u8)arg;
869 /* could not handle ioctl */
870 DBG(dev, "printer_ioctl: ERROR cmd=0x%4.4xis not supported\n",
875 spin_unlock_irqrestore(&dev->lock, flags);
880 /* used after endpoint configuration */
881 static const struct file_operations printer_io_operations = {
882 .owner = THIS_MODULE,
883 .open = printer_open,
884 .read = printer_read,
885 .write = printer_write,
886 .fsync = printer_fsync,
887 .poll = printer_poll,
888 .unlocked_ioctl = printer_ioctl,
889 .release = printer_close,
890 .llseek = noop_llseek,
893 /*-------------------------------------------------------------------------*/
896 set_printer_interface(struct printer_dev *dev)
900 dev->in_ep->desc = ep_desc(dev->gadget, &hs_ep_in_desc, &fs_ep_in_desc);
901 dev->in_ep->driver_data = dev;
903 dev->out_ep->desc = ep_desc(dev->gadget, &hs_ep_out_desc,
905 dev->out_ep->driver_data = dev;
907 result = usb_ep_enable(dev->in_ep);
909 DBG(dev, "enable %s --> %d\n", dev->in_ep->name, result);
913 result = usb_ep_enable(dev->out_ep);
915 DBG(dev, "enable %s --> %d\n", dev->in_ep->name, result);
920 /* on error, disable any endpoints */
922 (void) usb_ep_disable(dev->in_ep);
923 (void) usb_ep_disable(dev->out_ep);
924 dev->in_ep->desc = NULL;
925 dev->out_ep->desc = NULL;
928 /* caller is responsible for cleanup on error */
932 static void printer_reset_interface(struct printer_dev *dev)
934 if (dev->interface < 0)
937 DBG(dev, "%s\n", __func__);
939 if (dev->in_ep->desc)
940 usb_ep_disable(dev->in_ep);
942 if (dev->out_ep->desc)
943 usb_ep_disable(dev->out_ep);
945 dev->in_ep->desc = NULL;
946 dev->out_ep->desc = NULL;
950 /* change our operational config. must agree with the code
951 * that returns config descriptors, and altsetting code.
954 printer_set_config(struct printer_dev *dev, unsigned number)
957 struct usb_gadget *gadget = dev->gadget;
960 case DEV_CONFIG_VALUE:
971 usb_gadget_vbus_draw(dev->gadget,
972 dev->gadget->is_otg ? 8 : 100);
977 power = 2 * config_desc.bMaxPower;
978 usb_gadget_vbus_draw(dev->gadget, power);
980 switch (gadget->speed) {
981 case USB_SPEED_FULL: speed = "full"; break;
982 #ifdef CONFIG_USB_GADGET_DUALSPEED
983 case USB_SPEED_HIGH: speed = "high"; break;
985 default: speed = "?"; break;
988 dev->config = number;
989 INFO(dev, "%s speed config #%d: %d mA, %s\n",
990 speed, number, power, driver_desc);
996 config_buf(enum usb_device_speed speed, u8 *buf, u8 type, unsigned index,
1000 const struct usb_descriptor_header **function;
1001 #ifdef CONFIG_USB_GADGET_DUALSPEED
1002 int hs = (speed == USB_SPEED_HIGH);
1004 if (type == USB_DT_OTHER_SPEED_CONFIG)
1008 function = hs_printer_function;
1010 function = fs_printer_function;
1013 function = fs_printer_function;
1016 if (index >= device_desc.bNumConfigurations)
1019 /* for now, don't advertise srp-only devices */
1023 len = usb_gadget_config_buf(&config_desc, buf, USB_DESC_BUFSIZE,
1027 ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
1031 /* Change our operational Interface. */
1033 set_interface(struct printer_dev *dev, unsigned number)
1037 /* Free the current interface */
1038 switch (dev->interface) {
1039 case PRINTER_INTERFACE:
1040 printer_reset_interface(dev);
1045 case PRINTER_INTERFACE:
1046 result = set_printer_interface(dev);
1048 printer_reset_interface(dev);
1050 dev->interface = PRINTER_INTERFACE;
1059 INFO(dev, "Using interface %x\n", number);
1064 static void printer_setup_complete(struct usb_ep *ep, struct usb_request *req)
1066 if (req->status || req->actual != req->length)
1067 DBG((struct printer_dev *) ep->driver_data,
1068 "setup complete --> %d, %d/%d\n",
1069 req->status, req->actual, req->length);
1072 static void printer_soft_reset(struct printer_dev *dev)
1074 struct usb_request *req;
1076 INFO(dev, "Received Printer Reset Request\n");
1078 if (usb_ep_disable(dev->in_ep))
1079 DBG(dev, "Failed to disable USB in_ep\n");
1080 if (usb_ep_disable(dev->out_ep))
1081 DBG(dev, "Failed to disable USB out_ep\n");
1083 if (dev->current_rx_req != NULL) {
1084 list_add(&dev->current_rx_req->list, &dev->rx_reqs);
1085 dev->current_rx_req = NULL;
1087 dev->current_rx_bytes = 0;
1088 dev->current_rx_buf = NULL;
1089 dev->reset_printer = 1;
1091 while (likely(!(list_empty(&dev->rx_buffers)))) {
1092 req = container_of(dev->rx_buffers.next, struct usb_request,
1094 list_del_init(&req->list);
1095 list_add(&req->list, &dev->rx_reqs);
1098 while (likely(!(list_empty(&dev->rx_reqs_active)))) {
1099 req = container_of(dev->rx_buffers.next, struct usb_request,
1101 list_del_init(&req->list);
1102 list_add(&req->list, &dev->rx_reqs);
1105 while (likely(!(list_empty(&dev->tx_reqs_active)))) {
1106 req = container_of(dev->tx_reqs_active.next,
1107 struct usb_request, list);
1108 list_del_init(&req->list);
1109 list_add(&req->list, &dev->tx_reqs);
1112 if (usb_ep_enable(dev->in_ep))
1113 DBG(dev, "Failed to enable USB in_ep\n");
1114 if (usb_ep_enable(dev->out_ep))
1115 DBG(dev, "Failed to enable USB out_ep\n");
1117 wake_up_interruptible(&dev->rx_wait);
1118 wake_up_interruptible(&dev->tx_wait);
1119 wake_up_interruptible(&dev->tx_flush_wait);
1122 /*-------------------------------------------------------------------------*/
1125 * The setup() callback implements all the ep0 functionality that's not
1126 * handled lower down.
1129 printer_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1131 struct printer_dev *dev = get_gadget_data(gadget);
1132 struct usb_request *req = dev->req;
1133 int value = -EOPNOTSUPP;
1134 u16 wIndex = le16_to_cpu(ctrl->wIndex);
1135 u16 wValue = le16_to_cpu(ctrl->wValue);
1136 u16 wLength = le16_to_cpu(ctrl->wLength);
1138 DBG(dev, "ctrl req%02x.%02x v%04x i%04x l%d\n",
1139 ctrl->bRequestType, ctrl->bRequest, wValue, wIndex, wLength);
1141 req->complete = printer_setup_complete;
1143 switch (ctrl->bRequestType&USB_TYPE_MASK) {
1145 case USB_TYPE_STANDARD:
1146 switch (ctrl->bRequest) {
1148 case USB_REQ_GET_DESCRIPTOR:
1149 if (ctrl->bRequestType != USB_DIR_IN)
1151 switch (wValue >> 8) {
1154 device_desc.bMaxPacketSize0 =
1155 gadget->ep0->maxpacket;
1156 value = min(wLength, (u16) sizeof device_desc);
1157 memcpy(req->buf, &device_desc, value);
1159 #ifdef CONFIG_USB_GADGET_DUALSPEED
1160 case USB_DT_DEVICE_QUALIFIER:
1161 if (!gadget->is_dualspeed)
1164 * assumes ep0 uses the same value for both
1167 dev_qualifier.bMaxPacketSize0 =
1168 gadget->ep0->maxpacket;
1169 value = min(wLength,
1170 (u16) sizeof dev_qualifier);
1171 memcpy(req->buf, &dev_qualifier, value);
1174 case USB_DT_OTHER_SPEED_CONFIG:
1175 if (!gadget->is_dualspeed)
1178 #endif /* CONFIG_USB_GADGET_DUALSPEED */
1180 value = config_buf(gadget->speed, req->buf,
1185 value = min(wLength, (u16) value);
1189 value = usb_gadget_get_string(&stringtab,
1190 wValue & 0xff, req->buf);
1192 value = min(wLength, (u16) value);
1197 case USB_REQ_SET_CONFIGURATION:
1198 if (ctrl->bRequestType != 0)
1200 if (gadget->a_hnp_support)
1201 DBG(dev, "HNP available\n");
1202 else if (gadget->a_alt_hnp_support)
1203 DBG(dev, "HNP needs a different root port\n");
1204 value = printer_set_config(dev, wValue);
1206 value = set_interface(dev, PRINTER_INTERFACE);
1208 case USB_REQ_GET_CONFIGURATION:
1209 if (ctrl->bRequestType != USB_DIR_IN)
1211 *(u8 *)req->buf = dev->config;
1212 value = min(wLength, (u16) 1);
1215 case USB_REQ_SET_INTERFACE:
1216 if (ctrl->bRequestType != USB_RECIP_INTERFACE ||
1220 value = set_interface(dev, PRINTER_INTERFACE);
1222 case USB_REQ_GET_INTERFACE:
1223 if (ctrl->bRequestType !=
1224 (USB_DIR_IN|USB_RECIP_INTERFACE)
1228 *(u8 *)req->buf = dev->interface;
1229 value = min(wLength, (u16) 1);
1237 case USB_TYPE_CLASS:
1238 switch (ctrl->bRequest) {
1239 case 0: /* Get the IEEE-1284 PNP String */
1240 /* Only one printer interface is supported. */
1241 if ((wIndex>>8) != PRINTER_INTERFACE)
1244 value = (pnp_string[0]<<8)|pnp_string[1];
1245 memcpy(req->buf, pnp_string, value);
1246 DBG(dev, "1284 PNP String: %x %s\n", value,
1250 case 1: /* Get Port Status */
1251 /* Only one printer interface is supported. */
1252 if (wIndex != PRINTER_INTERFACE)
1255 *(u8 *)req->buf = dev->printer_status;
1256 value = min(wLength, (u16) 1);
1259 case 2: /* Soft Reset */
1260 /* Only one printer interface is supported. */
1261 if (wIndex != PRINTER_INTERFACE)
1264 printer_soft_reset(dev);
1277 "unknown ctrl req%02x.%02x v%04x i%04x l%d\n",
1278 ctrl->bRequestType, ctrl->bRequest,
1279 wValue, wIndex, wLength);
1283 /* respond with data transfer before status phase? */
1285 req->length = value;
1286 req->zero = value < wLength;
1287 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
1289 DBG(dev, "ep_queue --> %d\n", value);
1291 printer_setup_complete(gadget->ep0, req);
1295 /* host either stalls (value < 0) or reports success */
1300 printer_disconnect(struct usb_gadget *gadget)
1302 struct printer_dev *dev = get_gadget_data(gadget);
1303 unsigned long flags;
1305 DBG(dev, "%s\n", __func__);
1307 spin_lock_irqsave(&dev->lock, flags);
1309 printer_reset_interface(dev);
1311 spin_unlock_irqrestore(&dev->lock, flags);
1315 printer_unbind(struct usb_gadget *gadget)
1317 struct printer_dev *dev = get_gadget_data(gadget);
1318 struct usb_request *req;
1321 DBG(dev, "%s\n", __func__);
1323 /* Remove sysfs files */
1324 device_destroy(usb_gadget_class, g_printer_devno);
1326 /* Remove Character Device */
1327 cdev_del(&dev->printer_cdev);
1329 /* we must already have been disconnected ... no i/o may be active */
1330 WARN_ON(!list_empty(&dev->tx_reqs_active));
1331 WARN_ON(!list_empty(&dev->rx_reqs_active));
1333 /* Free all memory for this driver. */
1334 while (!list_empty(&dev->tx_reqs)) {
1335 req = container_of(dev->tx_reqs.next, struct usb_request,
1337 list_del(&req->list);
1338 printer_req_free(dev->in_ep, req);
1341 if (dev->current_rx_req != NULL)
1342 printer_req_free(dev->out_ep, dev->current_rx_req);
1344 while (!list_empty(&dev->rx_reqs)) {
1345 req = container_of(dev->rx_reqs.next,
1346 struct usb_request, list);
1347 list_del(&req->list);
1348 printer_req_free(dev->out_ep, req);
1351 while (!list_empty(&dev->rx_buffers)) {
1352 req = container_of(dev->rx_buffers.next,
1353 struct usb_request, list);
1354 list_del(&req->list);
1355 printer_req_free(dev->out_ep, req);
1359 printer_req_free(gadget->ep0, dev->req);
1363 set_gadget_data(gadget, NULL);
1367 printer_bind(struct usb_gadget *gadget)
1369 struct printer_dev *dev;
1370 struct usb_ep *in_ep, *out_ep;
1371 int status = -ENOMEM;
1375 struct usb_request *req;
1377 dev = &usb_printer_gadget;
1380 /* Setup the sysfs files for the printer gadget. */
1381 dev->pdev = device_create(usb_gadget_class, NULL, g_printer_devno,
1383 if (IS_ERR(dev->pdev)) {
1384 ERROR(dev, "Failed to create device: g_printer\n");
1389 * Register a character device as an interface to a user mode
1390 * program that handles the printer specific functionality.
1392 cdev_init(&dev->printer_cdev, &printer_io_operations);
1393 dev->printer_cdev.owner = THIS_MODULE;
1394 status = cdev_add(&dev->printer_cdev, g_printer_devno, 1);
1396 ERROR(dev, "Failed to open char device\n");
1400 gcnum = usb_gadget_controller_number(gadget);
1402 device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
1404 dev_warn(&gadget->dev, "controller '%s' not recognized\n",
1406 /* unrecognized, but safe unless bulk is REALLY quirky */
1407 device_desc.bcdDevice =
1408 cpu_to_le16(0xFFFF);
1410 snprintf(manufacturer, sizeof(manufacturer), "%s %s with %s",
1411 init_utsname()->sysname, init_utsname()->release,
1414 device_desc.idVendor =
1415 cpu_to_le16(PRINTER_VENDOR_NUM);
1416 device_desc.idProduct =
1417 cpu_to_le16(PRINTER_PRODUCT_NUM);
1419 /* support optional vendor/distro customization */
1422 dev_err(&gadget->dev, "idVendor needs idProduct!\n");
1425 device_desc.idVendor = cpu_to_le16(idVendor);
1426 device_desc.idProduct = cpu_to_le16(idProduct);
1428 device_desc.bcdDevice = cpu_to_le16(bcdDevice);
1432 strlcpy(manufacturer, iManufacturer, sizeof manufacturer);
1435 strlcpy(product_desc, iProduct, sizeof product_desc);
1438 strlcpy(serial_num, iSerialNum, sizeof serial_num);
1441 strlcpy(&pnp_string[2], iPNPstring, (sizeof pnp_string)-2);
1443 len = strlen(pnp_string);
1444 pnp_string[0] = (len >> 8) & 0xFF;
1445 pnp_string[1] = len & 0xFF;
1447 /* all we really need is bulk IN/OUT */
1448 usb_ep_autoconfig_reset(gadget);
1449 in_ep = usb_ep_autoconfig(gadget, &fs_ep_in_desc);
1452 dev_err(&gadget->dev, "can't autoconfigure on %s\n",
1456 in_ep->driver_data = in_ep; /* claim */
1458 out_ep = usb_ep_autoconfig(gadget, &fs_ep_out_desc);
1461 out_ep->driver_data = out_ep; /* claim */
1463 #ifdef CONFIG_USB_GADGET_DUALSPEED
1464 /* assumes that all endpoints are dual-speed */
1465 hs_ep_in_desc.bEndpointAddress = fs_ep_in_desc.bEndpointAddress;
1466 hs_ep_out_desc.bEndpointAddress = fs_ep_out_desc.bEndpointAddress;
1467 #endif /* DUALSPEED */
1469 usb_gadget_set_selfpowered(gadget);
1471 if (gadget->is_otg) {
1472 otg_desc.bmAttributes |= USB_OTG_HNP,
1473 config_desc.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1476 spin_lock_init(&dev->lock);
1477 mutex_init(&dev->lock_printer_io);
1478 INIT_LIST_HEAD(&dev->tx_reqs);
1479 INIT_LIST_HEAD(&dev->tx_reqs_active);
1480 INIT_LIST_HEAD(&dev->rx_reqs);
1481 INIT_LIST_HEAD(&dev->rx_reqs_active);
1482 INIT_LIST_HEAD(&dev->rx_buffers);
1483 init_waitqueue_head(&dev->rx_wait);
1484 init_waitqueue_head(&dev->tx_wait);
1485 init_waitqueue_head(&dev->tx_flush_wait);
1488 dev->interface = -1;
1489 dev->printer_cdev_open = 0;
1490 dev->printer_status = PRINTER_NOT_ERROR;
1491 dev->current_rx_req = NULL;
1492 dev->current_rx_bytes = 0;
1493 dev->current_rx_buf = NULL;
1496 dev->out_ep = out_ep;
1498 /* preallocate control message data and buffer */
1499 dev->req = printer_req_alloc(gadget->ep0, USB_DESC_BUFSIZE,
1506 for (i = 0; i < QLEN; i++) {
1507 req = printer_req_alloc(dev->in_ep, USB_BUFSIZE, GFP_KERNEL);
1509 while (!list_empty(&dev->tx_reqs)) {
1510 req = container_of(dev->tx_reqs.next,
1511 struct usb_request, list);
1512 list_del(&req->list);
1513 printer_req_free(dev->in_ep, req);
1517 list_add(&req->list, &dev->tx_reqs);
1520 for (i = 0; i < QLEN; i++) {
1521 req = printer_req_alloc(dev->out_ep, USB_BUFSIZE, GFP_KERNEL);
1523 while (!list_empty(&dev->rx_reqs)) {
1524 req = container_of(dev->rx_reqs.next,
1525 struct usb_request, list);
1526 list_del(&req->list);
1527 printer_req_free(dev->out_ep, req);
1531 list_add(&req->list, &dev->rx_reqs);
1534 dev->req->complete = printer_setup_complete;
1536 /* finish hookup to lower layer ... */
1537 dev->gadget = gadget;
1538 set_gadget_data(gadget, dev);
1539 gadget->ep0->driver_data = dev;
1541 INFO(dev, "%s, version: " DRIVER_VERSION "\n", driver_desc);
1542 INFO(dev, "using %s, OUT %s IN %s\n", gadget->name, out_ep->name,
1548 printer_unbind(gadget);
1552 /*-------------------------------------------------------------------------*/
1554 static struct usb_gadget_driver printer_driver = {
1557 .function = (char *) driver_desc,
1558 .unbind = printer_unbind,
1560 .setup = printer_setup,
1561 .disconnect = printer_disconnect,
1564 .name = (char *) shortname,
1565 .owner = THIS_MODULE,
1569 MODULE_DESCRIPTION(DRIVER_DESC);
1570 MODULE_AUTHOR("Craig Nadler");
1571 MODULE_LICENSE("GPL");
1578 usb_gadget_class = class_create(THIS_MODULE, "usb_printer_gadget");
1579 if (IS_ERR(usb_gadget_class)) {
1580 status = PTR_ERR(usb_gadget_class);
1581 ERROR(dev, "unable to create usb_gadget class %d\n", status);
1585 status = alloc_chrdev_region(&g_printer_devno, 0, 1,
1586 "USB printer gadget");
1588 ERROR(dev, "alloc_chrdev_region %d\n", status);
1589 class_destroy(usb_gadget_class);
1593 status = usb_gadget_probe_driver(&printer_driver, printer_bind);
1595 class_destroy(usb_gadget_class);
1596 unregister_chrdev_region(g_printer_devno, 1);
1597 DBG(dev, "usb_gadget_probe_driver %x\n", status);
1609 mutex_lock(&usb_printer_gadget.lock_printer_io);
1610 status = usb_gadget_unregister_driver(&printer_driver);
1612 ERROR(dev, "usb_gadget_unregister_driver %x\n", status);
1614 unregister_chrdev_region(g_printer_devno, 2);
1615 class_destroy(usb_gadget_class);
1616 mutex_unlock(&usb_printer_gadget.lock_printer_io);
1618 module_exit(cleanup);