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/smp_lock.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"
56 #define DRIVER_DESC "Printer Gadget"
57 #define DRIVER_VERSION "2007 OCT 06"
59 static const char shortname [] = "printer";
60 static const char driver_desc [] = DRIVER_DESC;
62 static dev_t g_printer_devno;
64 static struct class *usb_gadget_class;
66 /*-------------------------------------------------------------------------*/
69 spinlock_t lock; /* lock this structure */
70 /* lock buffer lists during read/write calls */
71 spinlock_t lock_printer_io;
72 struct usb_gadget *gadget;
73 struct usb_request *req; /* for control responses */
76 struct usb_ep *in_ep, *out_ep;
77 const struct usb_endpoint_descriptor
79 struct list_head rx_reqs; /* List of free RX structs */
80 struct list_head rx_reqs_active; /* List of Active RX xfers */
81 struct list_head rx_buffers; /* List of completed xfers */
82 /* wait until there is data to be read. */
83 wait_queue_head_t rx_wait;
84 struct list_head tx_reqs; /* List of free TX structs */
85 struct list_head tx_reqs_active; /* List of Active TX xfers */
86 /* Wait until there are write buffers available to use. */
87 wait_queue_head_t tx_wait;
88 /* Wait until all write buffers have been sent. */
89 wait_queue_head_t tx_flush_wait;
90 struct usb_request *current_rx_req;
91 size_t current_rx_bytes;
95 struct class_device *printer_class_dev;
96 struct cdev printer_cdev;
99 wait_queue_head_t wait;
102 static struct printer_dev usb_printer_gadget;
104 /*-------------------------------------------------------------------------*/
106 /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
107 * Instead: allocate your own, using normal USB-IF procedures.
110 /* Thanks to NetChip Technologies for donating this product ID.
112 #define PRINTER_VENDOR_NUM 0x0525 /* NetChip */
113 #define PRINTER_PRODUCT_NUM 0xa4a8 /* Linux-USB Printer Gadget */
115 /* Some systems will want different product identifers published in the
116 * device descriptor, either numbers or strings or both. These string
117 * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
120 static ushort __initdata idVendor;
121 module_param(idVendor, ushort, S_IRUGO);
122 MODULE_PARM_DESC(idVendor, "USB Vendor ID");
124 static ushort __initdata idProduct;
125 module_param(idProduct, ushort, S_IRUGO);
126 MODULE_PARM_DESC(idProduct, "USB Product ID");
128 static ushort __initdata bcdDevice;
129 module_param(bcdDevice, ushort, S_IRUGO);
130 MODULE_PARM_DESC(bcdDevice, "USB Device version (BCD)");
132 static char *__initdata iManufacturer;
133 module_param(iManufacturer, charp, S_IRUGO);
134 MODULE_PARM_DESC(iManufacturer, "USB Manufacturer string");
136 static char *__initdata iProduct;
137 module_param(iProduct, charp, S_IRUGO);
138 MODULE_PARM_DESC(iProduct, "USB Product string");
140 static char *__initdata iSerialNum;
141 module_param(iSerialNum, charp, S_IRUGO);
142 MODULE_PARM_DESC(iSerialNum, "1");
144 static char *__initdata iPNPstring;
145 module_param(iPNPstring, charp, S_IRUGO);
146 MODULE_PARM_DESC(iPNPstring, "MFG:linux;MDL:g_printer;CLS:PRINTER;SN:1;");
148 /* Number of requests to allocate per endpoint, not used for ep0. */
149 static unsigned qlen = 10;
150 module_param(qlen, uint, S_IRUGO|S_IWUSR);
154 #ifdef CONFIG_USB_GADGET_DUALSPEED
155 #define DEVSPEED USB_SPEED_HIGH
156 #else /* full speed (low speed doesn't do bulk) */
157 #define DEVSPEED USB_SPEED_FULL
160 /*-------------------------------------------------------------------------*/
162 #define xprintk(d, level, fmt, args...) \
163 printk(level "%s: " fmt, DRIVER_DESC, ## args)
166 #define DBG(dev, fmt, args...) \
167 xprintk(dev, KERN_DEBUG, fmt, ## args)
169 #define DBG(dev, fmt, args...) \
174 #define VDBG(dev, fmt, args...) \
175 xprintk(dev, KERN_DEBUG, fmt, ## args)
177 #define VDBG(dev, fmt, args...) \
181 #define ERROR(dev, fmt, args...) \
182 xprintk(dev, KERN_ERR, fmt, ## args)
183 #define WARN(dev, fmt, args...) \
184 xprintk(dev, KERN_WARNING, fmt, ## args)
185 #define INFO(dev, fmt, args...) \
186 xprintk(dev, KERN_INFO, fmt, ## args)
188 /*-------------------------------------------------------------------------*/
190 /* USB DRIVER HOOKUP (to the hardware driver, below us), mostly
191 * ep0 implementation: descriptors, config management, setup().
192 * also optional class-specific notification interrupt transfer.
196 * DESCRIPTORS ... most are static, but strings and (full) configuration
197 * descriptors are built on demand.
200 #define STRING_MANUFACTURER 1
201 #define STRING_PRODUCT 2
202 #define STRING_SERIALNUM 3
204 /* holds our biggest descriptor */
205 #define USB_DESC_BUFSIZE 256
206 #define USB_BUFSIZE 8192
208 /* This device advertises one configuration. */
209 #define DEV_CONFIG_VALUE 1
210 #define PRINTER_INTERFACE 0
212 static struct usb_device_descriptor device_desc = {
213 .bLength = sizeof device_desc,
214 .bDescriptorType = USB_DT_DEVICE,
215 .bcdUSB = __constant_cpu_to_le16(0x0200),
216 .bDeviceClass = USB_CLASS_PER_INTERFACE,
217 .bDeviceSubClass = 0,
218 .bDeviceProtocol = 0,
219 .idVendor = __constant_cpu_to_le16(PRINTER_VENDOR_NUM),
220 .idProduct = __constant_cpu_to_le16(PRINTER_PRODUCT_NUM),
221 .iManufacturer = STRING_MANUFACTURER,
222 .iProduct = STRING_PRODUCT,
223 .iSerialNumber = STRING_SERIALNUM,
224 .bNumConfigurations = 1
227 static struct usb_otg_descriptor otg_desc = {
228 .bLength = sizeof otg_desc,
229 .bDescriptorType = USB_DT_OTG,
230 .bmAttributes = USB_OTG_SRP
233 static struct usb_config_descriptor config_desc = {
234 .bLength = sizeof config_desc,
235 .bDescriptorType = USB_DT_CONFIG,
237 /* compute wTotalLength on the fly */
239 .bConfigurationValue = DEV_CONFIG_VALUE,
241 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
242 .bMaxPower = 1 /* Self-Powered */
245 static struct usb_interface_descriptor intf_desc = {
246 .bLength = sizeof intf_desc,
247 .bDescriptorType = USB_DT_INTERFACE,
248 .bInterfaceNumber = PRINTER_INTERFACE,
250 .bInterfaceClass = USB_CLASS_PRINTER,
251 .bInterfaceSubClass = 1, /* Printer Sub-Class */
252 .bInterfaceProtocol = 2, /* Bi-Directional */
256 static struct usb_endpoint_descriptor fs_ep_in_desc = {
257 .bLength = USB_DT_ENDPOINT_SIZE,
258 .bDescriptorType = USB_DT_ENDPOINT,
259 .bEndpointAddress = USB_DIR_IN,
260 .bmAttributes = USB_ENDPOINT_XFER_BULK
263 static struct usb_endpoint_descriptor fs_ep_out_desc = {
264 .bLength = USB_DT_ENDPOINT_SIZE,
265 .bDescriptorType = USB_DT_ENDPOINT,
266 .bEndpointAddress = USB_DIR_OUT,
267 .bmAttributes = USB_ENDPOINT_XFER_BULK
270 static const struct usb_descriptor_header *fs_printer_function [11] = {
271 (struct usb_descriptor_header *) &otg_desc,
272 (struct usb_descriptor_header *) &intf_desc,
273 (struct usb_descriptor_header *) &fs_ep_in_desc,
274 (struct usb_descriptor_header *) &fs_ep_out_desc,
278 #ifdef CONFIG_USB_GADGET_DUALSPEED
281 * usb 2.0 devices need to expose both high speed and full speed
282 * descriptors, unless they only run at full speed.
285 static struct usb_endpoint_descriptor hs_ep_in_desc = {
286 .bLength = USB_DT_ENDPOINT_SIZE,
287 .bDescriptorType = USB_DT_ENDPOINT,
288 .bmAttributes = USB_ENDPOINT_XFER_BULK,
289 .wMaxPacketSize = __constant_cpu_to_le16(512)
292 static struct usb_endpoint_descriptor hs_ep_out_desc = {
293 .bLength = USB_DT_ENDPOINT_SIZE,
294 .bDescriptorType = USB_DT_ENDPOINT,
295 .bmAttributes = USB_ENDPOINT_XFER_BULK,
296 .wMaxPacketSize = __constant_cpu_to_le16(512)
299 static struct usb_qualifier_descriptor dev_qualifier = {
300 .bLength = sizeof dev_qualifier,
301 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
302 .bcdUSB = __constant_cpu_to_le16(0x0200),
303 .bDeviceClass = USB_CLASS_PRINTER,
304 .bNumConfigurations = 1
307 static const struct usb_descriptor_header *hs_printer_function [11] = {
308 (struct usb_descriptor_header *) &otg_desc,
309 (struct usb_descriptor_header *) &intf_desc,
310 (struct usb_descriptor_header *) &hs_ep_in_desc,
311 (struct usb_descriptor_header *) &hs_ep_out_desc,
315 /* maxpacket and other transfer characteristics vary by speed. */
316 #define ep_desc(g, hs, fs) (((g)->speed == USB_SPEED_HIGH)?(hs):(fs))
320 /* if there's no high speed support, maxpacket doesn't change. */
321 #define ep_desc(g, hs, fs) (((void)(g)), (fs))
323 #endif /* !CONFIG_USB_GADGET_DUALSPEED */
325 /*-------------------------------------------------------------------------*/
327 /* descriptors that are built on-demand */
329 static char manufacturer [50];
330 static char product_desc [40] = DRIVER_DESC;
331 static char serial_num [40] = "1";
332 static char pnp_string [1024] =
333 "XXMFG:linux;MDL:g_printer;CLS:PRINTER;SN:1;";
335 /* static strings, in UTF-8 */
336 static struct usb_string strings [] = {
337 { STRING_MANUFACTURER, manufacturer, },
338 { STRING_PRODUCT, product_desc, },
339 { STRING_SERIALNUM, serial_num, },
340 { } /* end of list */
343 static struct usb_gadget_strings stringtab = {
344 .language = 0x0409, /* en-us */
348 /*-------------------------------------------------------------------------*/
350 static struct usb_request *
351 printer_req_alloc(struct usb_ep *ep, unsigned len, gfp_t gfp_flags)
353 struct usb_request *req;
355 req = usb_ep_alloc_request(ep, gfp_flags);
359 req->buf = kmalloc(len, gfp_flags);
360 if (req->buf == NULL) {
361 usb_ep_free_request(ep, req);
370 printer_req_free(struct usb_ep *ep, struct usb_request *req)
372 if (ep != NULL && req != NULL) {
374 usb_ep_free_request(ep, req);
378 /*-------------------------------------------------------------------------*/
380 static void rx_complete(struct usb_ep *ep, struct usb_request *req)
382 struct printer_dev *dev = ep->driver_data;
383 int status = req->status;
386 spin_lock_irqsave(&dev->lock, flags);
388 list_del_init(&req->list); /* Remode from Active List */
392 /* normal completion */
394 list_add_tail(&req->list, &dev->rx_buffers);
395 wake_up_interruptible(&dev->rx_wait);
396 DBG(dev, "G_Printer : rx length %d\n", req->actual);
399 /* software-driven interface shutdown */
400 case -ECONNRESET: /* unlink */
401 case -ESHUTDOWN: /* disconnect etc */
402 VDBG(dev, "rx shutdown, code %d\n", status);
403 list_add(&req->list, &dev->rx_reqs);
406 /* for hardware automagic (such as pxa) */
407 case -ECONNABORTED: /* endpoint reset */
408 DBG(dev, "rx %s reset\n", ep->name);
409 list_add(&req->list, &dev->rx_reqs);
417 DBG(dev, "rx status %d\n", status);
418 list_add(&req->list, &dev->rx_reqs);
421 spin_unlock_irqrestore(&dev->lock, flags);
424 static void tx_complete(struct usb_ep *ep, struct usb_request *req)
426 struct printer_dev *dev = ep->driver_data;
428 switch (req->status) {
430 VDBG(dev, "tx err %d\n", req->status);
432 case -ECONNRESET: /* unlink */
433 case -ESHUTDOWN: /* disconnect etc */
439 spin_lock(&dev->lock);
440 /* Take the request struct off the active list and put it on the
443 list_del_init(&req->list);
444 list_add(&req->list, &dev->tx_reqs);
445 wake_up_interruptible(&dev->tx_wait);
446 if (likely(list_empty(&dev->tx_reqs_active)))
447 wake_up_interruptible(&dev->tx_flush_wait);
449 spin_unlock(&dev->lock);
452 /*-------------------------------------------------------------------------*/
455 printer_open(struct inode *inode, struct file *fd)
457 struct printer_dev *dev;
461 dev = container_of(inode->i_cdev, struct printer_dev, printer_cdev);
463 spin_lock_irqsave(&dev->lock, flags);
465 if (!dev->printer_cdev_open) {
466 dev->printer_cdev_open = 1;
467 fd->private_data = dev;
469 /* Change the printer status to show that it's on-line. */
470 dev->printer_status |= PRINTER_SELECTED;
473 spin_unlock_irqrestore(&dev->lock, flags);
475 DBG(dev, "printer_open returned %x\n", ret);
481 printer_close(struct inode *inode, struct file *fd)
483 struct printer_dev *dev = fd->private_data;
486 spin_lock_irqsave(&dev->lock, flags);
487 dev->printer_cdev_open = 0;
488 fd->private_data = NULL;
489 /* Change printer status to show that the printer is off-line. */
490 dev->printer_status &= ~PRINTER_SELECTED;
491 spin_unlock_irqrestore(&dev->lock, flags);
493 DBG(dev, "printer_close\n");
499 printer_read(struct file *fd, char __user *buf, size_t len, loff_t *ptr)
501 struct printer_dev *dev = fd->private_data;
505 struct usb_request *req;
506 /* This is a pointer to the current USB rx request. */
507 struct usb_request *current_rx_req;
508 /* This is the number of bytes in the current rx buffer. */
509 size_t current_rx_bytes;
510 /* This is a pointer to the current rx buffer. */
516 DBG(dev, "printer_read trying to read %d bytes\n", (int)len);
518 spin_lock(&dev->lock_printer_io);
519 spin_lock_irqsave(&dev->lock, flags);
521 /* We will use this flag later to check if a printer reset happened
522 * after we turn interrupts back on.
524 dev->reset_printer = 0;
526 while (likely(!list_empty(&dev->rx_reqs))) {
529 req = container_of(dev->rx_reqs.next,
530 struct usb_request, list);
531 list_del_init(&req->list);
533 /* The USB Host sends us whatever amount of data it wants to
534 * so we always set the length field to the full USB_BUFSIZE.
535 * If the amount of data is more than the read() caller asked
536 * for it will be stored in the request buffer until it is
537 * asked for by read().
539 req->length = USB_BUFSIZE;
540 req->complete = rx_complete;
542 error = usb_ep_queue(dev->out_ep, req, GFP_ATOMIC);
544 DBG(dev, "rx submit --> %d\n", error);
545 list_add(&req->list, &dev->rx_reqs);
548 list_add(&req->list, &dev->rx_reqs_active);
553 current_rx_req = dev->current_rx_req;
554 current_rx_bytes = dev->current_rx_bytes;
555 current_rx_buf = dev->current_rx_buf;
556 dev->current_rx_req = NULL;
557 dev->current_rx_bytes = 0;
558 dev->current_rx_buf = NULL;
560 /* Check if there is any data in the read buffers. Please note that
561 * current_rx_bytes is the number of bytes in the current rx buffer.
562 * If it is zero then check if there are any other rx_buffers that
563 * are on the completed list. We are only out of data if all rx
566 if ((current_rx_bytes == 0) &&
567 (likely(list_empty(&dev->rx_buffers)))) {
568 /* Turn interrupts back on before sleeping. */
569 spin_unlock_irqrestore(&dev->lock, flags);
572 * If no data is available check if this is a NON-Blocking
575 if (fd->f_flags & (O_NONBLOCK|O_NDELAY)) {
576 spin_unlock(&dev->lock_printer_io);
580 /* Sleep until data is available */
581 wait_event_interruptible(dev->rx_wait,
582 (likely(!list_empty(&dev->rx_buffers))));
583 spin_lock_irqsave(&dev->lock, flags);
586 /* We have data to return then copy it to the caller's buffer.*/
587 while ((current_rx_bytes || likely(!list_empty(&dev->rx_buffers)))
589 if (current_rx_bytes == 0) {
590 req = container_of(dev->rx_buffers.next,
591 struct usb_request, list);
592 list_del_init(&req->list);
594 if (req->actual && req->buf) {
595 current_rx_req = req;
596 current_rx_bytes = req->actual;
597 current_rx_buf = req->buf;
599 list_add(&req->list, &dev->rx_reqs);
604 /* Don't leave irqs off while doing memory copies */
605 spin_unlock_irqrestore(&dev->lock, flags);
607 if (len > current_rx_bytes)
608 size = current_rx_bytes;
612 size -= copy_to_user(buf, current_rx_buf, size);
613 bytes_copied += size;
617 spin_lock_irqsave(&dev->lock, flags);
619 /* We've disconnected or reset free the req and buffer */
620 if (dev->reset_printer) {
621 printer_req_free(dev->out_ep, current_rx_req);
622 spin_unlock_irqrestore(&dev->lock, flags);
623 spin_unlock(&dev->lock_printer_io);
627 /* If we not returning all the data left in this RX request
628 * buffer then adjust the amount of data left in the buffer.
629 * Othewise if we are done with this RX request buffer then
630 * requeue it to get any incoming data from the USB host.
632 if (size < current_rx_bytes) {
633 current_rx_bytes -= size;
634 current_rx_buf += size;
636 list_add(¤t_rx_req->list, &dev->rx_reqs);
637 current_rx_bytes = 0;
638 current_rx_buf = NULL;
639 current_rx_req = NULL;
643 dev->current_rx_req = current_rx_req;
644 dev->current_rx_bytes = current_rx_bytes;
645 dev->current_rx_buf = current_rx_buf;
647 spin_unlock_irqrestore(&dev->lock, flags);
648 spin_unlock(&dev->lock_printer_io);
650 DBG(dev, "printer_read returned %d bytes\n", (int)bytes_copied);
659 printer_write(struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
661 struct printer_dev *dev = fd->private_data;
663 size_t size; /* Amount of data in a TX request. */
664 size_t bytes_copied = 0;
665 struct usb_request *req;
667 DBG(dev, "printer_write trying to send %d bytes\n", (int)len);
672 spin_lock(&dev->lock_printer_io);
673 spin_lock_irqsave(&dev->lock, flags);
675 /* Check if a printer reset happens while we have interrupts on */
676 dev->reset_printer = 0;
678 /* Check if there is any available write buffers */
679 if (likely(list_empty(&dev->tx_reqs))) {
680 /* Turn interrupts back on before sleeping. */
681 spin_unlock_irqrestore(&dev->lock, flags);
684 * If write buffers are available check if this is
685 * a NON-Blocking call or not.
687 if (fd->f_flags & (O_NONBLOCK|O_NDELAY)) {
688 spin_unlock(&dev->lock_printer_io);
692 /* Sleep until a write buffer is available */
693 wait_event_interruptible(dev->tx_wait,
694 (likely(!list_empty(&dev->tx_reqs))));
695 spin_lock_irqsave(&dev->lock, flags);
698 while (likely(!list_empty(&dev->tx_reqs)) && len) {
700 if (len > USB_BUFSIZE)
705 req = container_of(dev->tx_reqs.next, struct usb_request,
707 list_del_init(&req->list);
709 req->complete = tx_complete;
712 /* Check if we need to send a zero length packet. */
714 /* They will be more TX requests so no yet. */
717 /* If the data amount is not a multple of the
718 * maxpacket size then send a zero length packet.
720 req->zero = ((len % dev->in_ep->maxpacket) == 0);
722 /* Don't leave irqs off while doing memory copies */
723 spin_unlock_irqrestore(&dev->lock, flags);
725 if (copy_from_user(req->buf, buf, size)) {
726 list_add(&req->list, &dev->tx_reqs);
727 spin_unlock(&dev->lock_printer_io);
731 bytes_copied += size;
735 spin_lock_irqsave(&dev->lock, flags);
737 /* We've disconnected or reset so free the req and buffer */
738 if (dev->reset_printer) {
739 printer_req_free(dev->in_ep, req);
740 spin_unlock_irqrestore(&dev->lock, flags);
741 spin_unlock(&dev->lock_printer_io);
745 if (usb_ep_queue(dev->in_ep, req, GFP_ATOMIC)) {
746 list_add(&req->list, &dev->tx_reqs);
747 spin_unlock_irqrestore(&dev->lock, flags);
748 spin_unlock(&dev->lock_printer_io);
752 list_add(&req->list, &dev->tx_reqs_active);
756 spin_unlock_irqrestore(&dev->lock, flags);
757 spin_unlock(&dev->lock_printer_io);
759 DBG(dev, "printer_write sent %d bytes\n", (int)bytes_copied);
769 printer_fsync(struct file *fd, struct dentry *dentry, int datasync)
771 struct printer_dev *dev = fd->private_data;
775 spin_lock_irqsave(&dev->lock, flags);
776 tx_list_empty = (likely(list_empty(&dev->tx_reqs)));
777 spin_unlock_irqrestore(&dev->lock, flags);
779 if (!tx_list_empty) {
780 /* Sleep until all data has been sent */
781 wait_event_interruptible(dev->tx_flush_wait,
782 (likely(list_empty(&dev->tx_reqs_active))));
789 printer_poll(struct file *fd, poll_table *wait)
791 struct printer_dev *dev = fd->private_data;
795 poll_wait(fd, &dev->rx_wait, wait);
796 poll_wait(fd, &dev->tx_wait, wait);
798 spin_lock_irqsave(&dev->lock, flags);
799 if (likely(!list_empty(&dev->tx_reqs)))
800 status |= POLLOUT | POLLWRNORM;
802 if (likely(!list_empty(&dev->rx_buffers)))
803 status |= POLLIN | POLLRDNORM;
805 spin_unlock_irqrestore(&dev->lock, flags);
811 printer_ioctl(struct inode *inode, struct file *fd, unsigned int code,
814 struct printer_dev *dev = fd->private_data;
818 DBG(dev, "printer_ioctl: cmd=0x%4.4x, arg=%lu\n", code, arg);
822 spin_lock_irqsave(&dev->lock, flags);
825 case GADGET_GET_PRINTER_STATUS:
826 status = (int)dev->printer_status;
828 case GADGET_SET_PRINTER_STATUS:
829 dev->printer_status = (u8)arg;
832 /* could not handle ioctl */
833 DBG(dev, "printer_ioctl: ERROR cmd=0x%4.4xis not supported\n",
838 spin_unlock_irqrestore(&dev->lock, flags);
843 /* used after endpoint configuration */
844 static struct file_operations printer_io_operations = {
845 .owner = THIS_MODULE,
846 .open = printer_open,
847 .read = printer_read,
848 .write = printer_write,
849 .fsync = printer_fsync,
850 .poll = printer_poll,
851 .ioctl = printer_ioctl,
852 .release = printer_close
855 /*-------------------------------------------------------------------------*/
858 set_printer_interface(struct printer_dev *dev)
862 dev->in = ep_desc(dev->gadget, &hs_ep_in_desc, &fs_ep_in_desc);
863 dev->in_ep->driver_data = dev;
865 dev->out = ep_desc(dev->gadget, &hs_ep_out_desc, &fs_ep_out_desc);
866 dev->out_ep->driver_data = dev;
868 result = usb_ep_enable(dev->in_ep, dev->in);
870 DBG(dev, "enable %s --> %d\n", dev->in_ep->name, result);
874 result = usb_ep_enable(dev->out_ep, dev->out);
876 DBG(dev, "enable %s --> %d\n", dev->in_ep->name, result);
881 /* on error, disable any endpoints */
883 (void) usb_ep_disable(dev->in_ep);
884 (void) usb_ep_disable(dev->out_ep);
889 /* caller is responsible for cleanup on error */
893 static void printer_reset_interface(struct printer_dev *dev)
895 if (dev->interface < 0)
898 DBG(dev, "%s\n", __FUNCTION__);
901 usb_ep_disable(dev->in_ep);
904 usb_ep_disable(dev->out_ep);
909 /* change our operational config. must agree with the code
910 * that returns config descriptors, and altsetting code.
913 printer_set_config(struct printer_dev *dev, unsigned number)
916 struct usb_gadget *gadget = dev->gadget;
918 if (gadget_is_sa1100(gadget) && dev->config) {
919 /* tx fifo is full, but we can't clear it...*/
920 INFO(dev, "can't change configurations\n");
925 case DEV_CONFIG_VALUE:
936 usb_gadget_vbus_draw(dev->gadget,
937 dev->gadget->is_otg ? 8 : 100);
942 power = 2 * config_desc.bMaxPower;
943 usb_gadget_vbus_draw(dev->gadget, power);
945 switch (gadget->speed) {
946 case USB_SPEED_FULL: speed = "full"; break;
947 #ifdef CONFIG_USB_GADGET_DUALSPEED
948 case USB_SPEED_HIGH: speed = "high"; break;
950 default: speed = "?"; break;
953 dev->config = number;
954 INFO(dev, "%s speed config #%d: %d mA, %s\n",
955 speed, number, power, driver_desc);
961 config_buf(enum usb_device_speed speed, u8 *buf, u8 type, unsigned index,
965 const struct usb_descriptor_header **function;
966 #ifdef CONFIG_USB_GADGET_DUALSPEED
967 int hs = (speed == USB_SPEED_HIGH);
969 if (type == USB_DT_OTHER_SPEED_CONFIG)
973 function = hs_printer_function;
975 function = fs_printer_function;
978 function = fs_printer_function;
981 if (index >= device_desc.bNumConfigurations)
984 /* for now, don't advertise srp-only devices */
988 len = usb_gadget_config_buf(&config_desc, buf, USB_DESC_BUFSIZE,
992 ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
996 /* Change our operational Interface. */
998 set_interface(struct printer_dev *dev, unsigned number)
1002 if (gadget_is_sa1100(dev->gadget) && dev->interface < 0) {
1003 /* tx fifo is full, but we can't clear it...*/
1004 INFO(dev, "can't change interfaces\n");
1008 /* Free the current interface */
1009 switch (dev->interface) {
1010 case PRINTER_INTERFACE:
1011 printer_reset_interface(dev);
1016 case PRINTER_INTERFACE:
1017 result = set_printer_interface(dev);
1019 printer_reset_interface(dev);
1021 dev->interface = PRINTER_INTERFACE;
1030 INFO(dev, "Using interface %x\n", number);
1035 static void printer_setup_complete(struct usb_ep *ep, struct usb_request *req)
1037 if (req->status || req->actual != req->length)
1038 DBG((struct printer_dev *) ep->driver_data,
1039 "setup complete --> %d, %d/%d\n",
1040 req->status, req->actual, req->length);
1043 static void printer_soft_reset(struct printer_dev *dev)
1045 struct usb_request *req;
1047 INFO(dev, "Received Printer Reset Request\n");
1049 if (usb_ep_disable(dev->in_ep))
1050 DBG(dev, "Failed to disable USB in_ep\n");
1051 if (usb_ep_disable(dev->out_ep))
1052 DBG(dev, "Failed to disable USB out_ep\n");
1054 if (dev->current_rx_req != NULL) {
1055 list_add(&dev->current_rx_req->list, &dev->rx_reqs);
1056 dev->current_rx_req = NULL;
1058 dev->current_rx_bytes = 0;
1059 dev->current_rx_buf = NULL;
1060 dev->reset_printer = 1;
1062 while (likely(!(list_empty(&dev->rx_buffers)))) {
1063 req = container_of(dev->rx_buffers.next, struct usb_request,
1065 list_del_init(&req->list);
1066 list_add(&req->list, &dev->rx_reqs);
1069 while (likely(!(list_empty(&dev->rx_reqs_active)))) {
1070 req = container_of(dev->rx_buffers.next, struct usb_request,
1072 list_del_init(&req->list);
1073 list_add(&req->list, &dev->rx_reqs);
1076 while (likely(!(list_empty(&dev->tx_reqs_active)))) {
1077 req = container_of(dev->tx_reqs_active.next,
1078 struct usb_request, list);
1079 list_del_init(&req->list);
1080 list_add(&req->list, &dev->tx_reqs);
1083 if (usb_ep_enable(dev->in_ep, dev->in))
1084 DBG(dev, "Failed to enable USB in_ep\n");
1085 if (usb_ep_enable(dev->out_ep, dev->out))
1086 DBG(dev, "Failed to enable USB out_ep\n");
1088 wake_up_interruptible(&dev->tx_wait);
1089 wake_up_interruptible(&dev->tx_flush_wait);
1092 /*-------------------------------------------------------------------------*/
1095 * The setup() callback implements all the ep0 functionality that's not
1096 * handled lower down.
1099 printer_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1101 struct printer_dev *dev = get_gadget_data(gadget);
1102 struct usb_request *req = dev->req;
1103 int value = -EOPNOTSUPP;
1104 u16 wIndex = le16_to_cpu(ctrl->wIndex);
1105 u16 wValue = le16_to_cpu(ctrl->wValue);
1106 u16 wLength = le16_to_cpu(ctrl->wLength);
1108 DBG(dev, "ctrl req%02x.%02x v%04x i%04x l%d\n",
1109 ctrl->bRequestType, ctrl->bRequest, wValue, wIndex, wLength);
1111 req->complete = printer_setup_complete;
1113 switch (ctrl->bRequestType&USB_TYPE_MASK) {
1115 case USB_TYPE_STANDARD:
1116 switch (ctrl->bRequest) {
1118 case USB_REQ_GET_DESCRIPTOR:
1119 if (ctrl->bRequestType != USB_DIR_IN)
1121 switch (wValue >> 8) {
1124 value = min(wLength, (u16) sizeof device_desc);
1125 memcpy(req->buf, &device_desc, value);
1127 #ifdef CONFIG_USB_GADGET_DUALSPEED
1128 case USB_DT_DEVICE_QUALIFIER:
1129 if (!gadget->is_dualspeed)
1131 value = min(wLength,
1132 (u16) sizeof dev_qualifier);
1133 memcpy(req->buf, &dev_qualifier, value);
1136 case USB_DT_OTHER_SPEED_CONFIG:
1137 if (!gadget->is_dualspeed)
1140 #endif /* CONFIG_USB_GADGET_DUALSPEED */
1142 value = config_buf(gadget->speed, req->buf,
1147 value = min(wLength, (u16) value);
1151 value = usb_gadget_get_string(&stringtab,
1152 wValue & 0xff, req->buf);
1154 value = min(wLength, (u16) value);
1159 case USB_REQ_SET_CONFIGURATION:
1160 if (ctrl->bRequestType != 0)
1162 if (gadget->a_hnp_support)
1163 DBG(dev, "HNP available\n");
1164 else if (gadget->a_alt_hnp_support)
1165 DBG(dev, "HNP needs a different root port\n");
1166 value = printer_set_config(dev, wValue);
1168 case USB_REQ_GET_CONFIGURATION:
1169 if (ctrl->bRequestType != USB_DIR_IN)
1171 *(u8 *)req->buf = dev->config;
1172 value = min(wLength, (u16) 1);
1175 case USB_REQ_SET_INTERFACE:
1176 if (ctrl->bRequestType != USB_RECIP_INTERFACE ||
1180 value = set_interface(dev, PRINTER_INTERFACE);
1182 case USB_REQ_GET_INTERFACE:
1183 if (ctrl->bRequestType !=
1184 (USB_DIR_IN|USB_RECIP_INTERFACE)
1188 *(u8 *)req->buf = dev->interface;
1189 value = min(wLength, (u16) 1);
1197 case USB_TYPE_CLASS:
1198 switch (ctrl->bRequest) {
1199 case 0: /* Get the IEEE-1284 PNP String */
1200 /* Only one printer interface is supported. */
1201 if ((wIndex>>8) != PRINTER_INTERFACE)
1204 value = (pnp_string[0]<<8)|pnp_string[1];
1205 memcpy(req->buf, pnp_string, value);
1206 DBG(dev, "1284 PNP String: %x %s\n", value,
1210 case 1: /* Get Port Status */
1211 /* Only one printer interface is supported. */
1212 if (wIndex != PRINTER_INTERFACE)
1215 *(u8 *)req->buf = dev->printer_status;
1216 value = min(wLength, (u16) 1);
1219 case 2: /* Soft Reset */
1220 /* Only one printer interface is supported. */
1221 if (wIndex != PRINTER_INTERFACE)
1224 printer_soft_reset(dev);
1237 "unknown ctrl req%02x.%02x v%04x i%04x l%d\n",
1238 ctrl->bRequestType, ctrl->bRequest,
1239 wValue, wIndex, wLength);
1243 /* respond with data transfer before status phase? */
1245 req->length = value;
1246 req->zero = value < wLength
1247 && (value % gadget->ep0->maxpacket) == 0;
1248 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
1250 DBG(dev, "ep_queue --> %d\n", value);
1252 printer_setup_complete(gadget->ep0, req);
1256 /* host either stalls (value < 0) or reports success */
1261 printer_disconnect(struct usb_gadget *gadget)
1263 struct printer_dev *dev = get_gadget_data(gadget);
1264 unsigned long flags;
1266 DBG(dev, "%s\n", __FUNCTION__);
1268 spin_lock_irqsave(&dev->lock, flags);
1270 printer_reset_interface(dev);
1272 spin_unlock_irqrestore(&dev->lock, flags);
1276 printer_unbind(struct usb_gadget *gadget)
1278 struct printer_dev *dev = get_gadget_data(gadget);
1279 struct usb_request *req;
1282 DBG(dev, "%s\n", __FUNCTION__);
1284 /* Remove sysfs files */
1285 device_destroy(usb_gadget_class, g_printer_devno);
1287 /* Remove Character Device */
1288 cdev_del(&dev->printer_cdev);
1290 /* we must already have been disconnected ... no i/o may be active */
1291 WARN_ON(!list_empty(&dev->tx_reqs_active));
1292 WARN_ON(!list_empty(&dev->rx_reqs_active));
1294 /* Free all memory for this driver. */
1295 while (!list_empty(&dev->tx_reqs)) {
1296 req = container_of(dev->tx_reqs.next, struct usb_request,
1298 list_del(&req->list);
1299 printer_req_free(dev->in_ep, req);
1302 if (dev->current_rx_req != NULL)
1303 printer_req_free(dev->out_ep, dev->current_rx_req);
1305 while (!list_empty(&dev->rx_reqs)) {
1306 req = container_of(dev->rx_reqs.next,
1307 struct usb_request, list);
1308 list_del(&req->list);
1309 printer_req_free(dev->out_ep, req);
1312 while (!list_empty(&dev->rx_buffers)) {
1313 req = container_of(dev->rx_buffers.next,
1314 struct usb_request, list);
1315 list_del(&req->list);
1316 printer_req_free(dev->out_ep, req);
1320 printer_req_free(gadget->ep0, dev->req);
1324 set_gadget_data(gadget, NULL);
1328 printer_bind(struct usb_gadget *gadget)
1330 struct printer_dev *dev;
1331 struct usb_ep *in_ep, *out_ep;
1332 int status = -ENOMEM;
1336 struct usb_request *req;
1338 dev = &usb_printer_gadget;
1341 /* Setup the sysfs files for the printer gadget. */
1342 dev->pdev = device_create(usb_gadget_class, NULL, g_printer_devno,
1344 if (IS_ERR(dev->pdev)) {
1345 ERROR(dev, "Failed to create device: g_printer\n");
1350 * Register a character device as an interface to a user mode
1351 * program that handles the printer specific functionality.
1353 cdev_init(&dev->printer_cdev, &printer_io_operations);
1354 dev->printer_cdev.owner = THIS_MODULE;
1355 status = cdev_add(&dev->printer_cdev, g_printer_devno, 1);
1357 ERROR(dev, "Failed to open char device\n");
1361 if (gadget_is_sa1100(gadget)) {
1362 /* hardware can't write zero length packets. */
1363 ERROR(dev, "SA1100 controller is unsupport by this driver\n");
1367 gcnum = usb_gadget_controller_number(gadget);
1369 device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
1371 dev_warn(&gadget->dev, "controller '%s' not recognized\n",
1373 /* unrecognized, but safe unless bulk is REALLY quirky */
1374 device_desc.bcdDevice =
1375 __constant_cpu_to_le16(0xFFFF);
1377 snprintf(manufacturer, sizeof(manufacturer), "%s %s with %s",
1378 init_utsname()->sysname, init_utsname()->release,
1381 device_desc.idVendor =
1382 __constant_cpu_to_le16(PRINTER_VENDOR_NUM);
1383 device_desc.idProduct =
1384 __constant_cpu_to_le16(PRINTER_PRODUCT_NUM);
1386 /* support optional vendor/distro customization */
1389 dev_err(&gadget->dev, "idVendor needs idProduct!\n");
1392 device_desc.idVendor = cpu_to_le16(idVendor);
1393 device_desc.idProduct = cpu_to_le16(idProduct);
1395 device_desc.bcdDevice = cpu_to_le16(bcdDevice);
1399 strlcpy(manufacturer, iManufacturer, sizeof manufacturer);
1402 strlcpy(product_desc, iProduct, sizeof product_desc);
1405 strlcpy(serial_num, iSerialNum, sizeof serial_num);
1408 strlcpy(&pnp_string[2], iPNPstring, (sizeof pnp_string)-2);
1410 len = strlen(pnp_string);
1411 pnp_string[0] = (len >> 8) & 0xFF;
1412 pnp_string[1] = len & 0xFF;
1414 /* all we really need is bulk IN/OUT */
1415 usb_ep_autoconfig_reset(gadget);
1416 in_ep = usb_ep_autoconfig(gadget, &fs_ep_in_desc);
1419 dev_err(&gadget->dev, "can't autoconfigure on %s\n",
1423 in_ep->driver_data = in_ep; /* claim */
1425 out_ep = usb_ep_autoconfig(gadget, &fs_ep_out_desc);
1428 out_ep->driver_data = out_ep; /* claim */
1430 #ifdef CONFIG_USB_GADGET_DUALSPEED
1431 /* assumes ep0 uses the same value for both speeds ... */
1432 dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
1434 /* and that all endpoints are dual-speed */
1435 hs_ep_in_desc.bEndpointAddress = fs_ep_in_desc.bEndpointAddress;
1436 hs_ep_out_desc.bEndpointAddress = fs_ep_out_desc.bEndpointAddress;
1437 #endif /* DUALSPEED */
1439 device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
1440 usb_gadget_set_selfpowered(gadget);
1442 if (gadget->is_otg) {
1443 otg_desc.bmAttributes |= USB_OTG_HNP,
1444 config_desc.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1445 config_desc.bMaxPower = 4;
1448 spin_lock_init(&dev->lock);
1449 spin_lock_init(&dev->lock_printer_io);
1450 INIT_LIST_HEAD(&dev->tx_reqs);
1451 INIT_LIST_HEAD(&dev->tx_reqs_active);
1452 INIT_LIST_HEAD(&dev->rx_reqs);
1453 INIT_LIST_HEAD(&dev->rx_reqs_active);
1454 INIT_LIST_HEAD(&dev->rx_buffers);
1455 init_waitqueue_head(&dev->rx_wait);
1456 init_waitqueue_head(&dev->tx_wait);
1457 init_waitqueue_head(&dev->tx_flush_wait);
1460 dev->interface = -1;
1461 dev->printer_cdev_open = 0;
1462 dev->printer_status = PRINTER_NOT_ERROR;
1463 dev->current_rx_req = NULL;
1464 dev->current_rx_bytes = 0;
1465 dev->current_rx_buf = NULL;
1468 dev->out_ep = out_ep;
1470 /* preallocate control message data and buffer */
1471 dev->req = printer_req_alloc(gadget->ep0, USB_DESC_BUFSIZE,
1478 for (i = 0; i < QLEN; i++) {
1479 req = printer_req_alloc(dev->in_ep, USB_BUFSIZE, GFP_KERNEL);
1481 while (!list_empty(&dev->tx_reqs)) {
1482 req = container_of(dev->tx_reqs.next,
1483 struct usb_request, list);
1484 list_del(&req->list);
1485 printer_req_free(dev->in_ep, req);
1489 list_add(&req->list, &dev->tx_reqs);
1492 for (i = 0; i < QLEN; i++) {
1493 req = printer_req_alloc(dev->out_ep, USB_BUFSIZE, GFP_KERNEL);
1495 while (!list_empty(&dev->rx_reqs)) {
1496 req = container_of(dev->rx_reqs.next,
1497 struct usb_request, list);
1498 list_del(&req->list);
1499 printer_req_free(dev->out_ep, req);
1503 list_add(&req->list, &dev->rx_reqs);
1506 dev->req->complete = printer_setup_complete;
1508 /* finish hookup to lower layer ... */
1509 dev->gadget = gadget;
1510 set_gadget_data(gadget, dev);
1511 gadget->ep0->driver_data = dev;
1513 INFO(dev, "%s, version: " DRIVER_VERSION "\n", driver_desc);
1514 INFO(dev, "using %s, OUT %s IN %s\n", gadget->name, out_ep->name,
1520 printer_unbind(gadget);
1524 /*-------------------------------------------------------------------------*/
1526 static struct usb_gadget_driver printer_driver = {
1529 .function = (char *) driver_desc,
1530 .bind = printer_bind,
1531 .unbind = printer_unbind,
1533 .setup = printer_setup,
1534 .disconnect = printer_disconnect,
1537 .name = (char *) shortname,
1538 .owner = THIS_MODULE,
1542 MODULE_DESCRIPTION(DRIVER_DESC);
1543 MODULE_AUTHOR("Craig Nadler");
1544 MODULE_LICENSE("GPL");
1551 usb_gadget_class = class_create(THIS_MODULE, "usb_printer_gadget");
1552 if (IS_ERR(usb_gadget_class)) {
1553 status = PTR_ERR(usb_gadget_class);
1554 ERROR(dev, "unable to create usb_gadget class %d\n", status);
1558 status = alloc_chrdev_region(&g_printer_devno, 0, 1,
1559 "USB printer gadget");
1561 ERROR(dev, "alloc_chrdev_region %d\n", status);
1562 class_destroy(usb_gadget_class);
1566 status = usb_gadget_register_driver(&printer_driver);
1568 class_destroy(usb_gadget_class);
1569 unregister_chrdev_region(g_printer_devno, 1);
1570 DBG(dev, "usb_gadget_register_driver %x\n", status);
1582 spin_lock(&usb_printer_gadget.lock_printer_io);
1583 class_destroy(usb_gadget_class);
1584 unregister_chrdev_region(g_printer_devno, 2);
1586 status = usb_gadget_unregister_driver(&printer_driver);
1588 ERROR(dev, "usb_gadget_unregister_driver %x\n", status);
1590 spin_unlock(&usb_printer_gadget.lock_printer_io);
1592 module_exit(cleanup);