Adding support for MOXA ART SoC. Testing port of linux-2.6.32.60-moxart.
[linux-3.6.7-moxart.git] / drivers / usb / gadget / printer.c
blob75876155797cb622cf37d5ce50a96ea670d843ca
1 /*
2 * printer.c -- Printer gadget driver
4 * Copyright (C) 2003-2005 David Brownell
5 * Copyright (C) 2006 Craig W. Nadler
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/delay.h>
16 #include <linux/ioport.h>
17 #include <linux/sched.h>
18 #include <linux/slab.h>
19 #include <linux/mutex.h>
20 #include <linux/errno.h>
21 #include <linux/init.h>
22 #include <linux/timer.h>
23 #include <linux/list.h>
24 #include <linux/interrupt.h>
25 #include <linux/utsname.h>
26 #include <linux/device.h>
27 #include <linux/moduleparam.h>
28 #include <linux/fs.h>
29 #include <linux/poll.h>
30 #include <linux/types.h>
31 #include <linux/ctype.h>
32 #include <linux/cdev.h>
34 #include <asm/byteorder.h>
35 #include <linux/io.h>
36 #include <linux/irq.h>
37 #include <linux/uaccess.h>
38 #include <asm/unaligned.h>
40 #include <linux/usb/ch9.h>
41 #include <linux/usb/gadget.h>
42 #include <linux/usb/g_printer.h>
44 #include "gadget_chips.h"
48 * Kbuild is not very cooperative with respect to linking separately
49 * compiled library objects into one module. So for now we won't use
50 * separate compilation ... ensuring init/exit sections work to shrink
51 * the runtime footprint, and giving us at least some parts of what
52 * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
54 #include "composite.c"
55 #include "usbstring.c"
56 #include "config.c"
57 #include "epautoconf.c"
59 /*-------------------------------------------------------------------------*/
61 #define DRIVER_DESC "Printer Gadget"
62 #define DRIVER_VERSION "2007 OCT 06"
64 static DEFINE_MUTEX(printer_mutex);
65 static const char shortname [] = "printer";
66 static const char driver_desc [] = DRIVER_DESC;
68 static dev_t g_printer_devno;
70 static struct class *usb_gadget_class;
72 /*-------------------------------------------------------------------------*/
74 struct printer_dev {
75 spinlock_t lock; /* lock this structure */
76 /* lock buffer lists during read/write calls */
77 struct mutex lock_printer_io;
78 struct usb_gadget *gadget;
79 s8 interface;
80 struct usb_ep *in_ep, *out_ep;
82 struct list_head rx_reqs; /* List of free RX structs */
83 struct list_head rx_reqs_active; /* List of Active RX xfers */
84 struct list_head rx_buffers; /* List of completed xfers */
85 /* wait until there is data to be read. */
86 wait_queue_head_t rx_wait;
87 struct list_head tx_reqs; /* List of free TX structs */
88 struct list_head tx_reqs_active; /* List of Active TX xfers */
89 /* Wait until there are write buffers available to use. */
90 wait_queue_head_t tx_wait;
91 /* Wait until all write buffers have been sent. */
92 wait_queue_head_t tx_flush_wait;
93 struct usb_request *current_rx_req;
94 size_t current_rx_bytes;
95 u8 *current_rx_buf;
96 u8 printer_status;
97 u8 reset_printer;
98 struct cdev printer_cdev;
99 struct device *pdev;
100 u8 printer_cdev_open;
101 wait_queue_head_t wait;
102 struct usb_function function;
105 static struct printer_dev usb_printer_gadget;
107 /*-------------------------------------------------------------------------*/
109 /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
110 * Instead: allocate your own, using normal USB-IF procedures.
113 /* Thanks to NetChip Technologies for donating this product ID.
115 #define PRINTER_VENDOR_NUM 0x0525 /* NetChip */
116 #define PRINTER_PRODUCT_NUM 0xa4a8 /* Linux-USB Printer Gadget */
118 /* Some systems will want different product identifiers published in the
119 * device descriptor, either numbers or strings or both. These string
120 * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
123 static char *iSerialNum;
124 module_param(iSerialNum, charp, S_IRUGO);
125 MODULE_PARM_DESC(iSerialNum, "1");
127 static char *iPNPstring;
128 module_param(iPNPstring, charp, S_IRUGO);
129 MODULE_PARM_DESC(iPNPstring, "MFG:linux;MDL:g_printer;CLS:PRINTER;SN:1;");
131 /* Number of requests to allocate per endpoint, not used for ep0. */
132 static unsigned qlen = 10;
133 module_param(qlen, uint, S_IRUGO|S_IWUSR);
135 #define QLEN qlen
137 /*-------------------------------------------------------------------------*/
140 * DESCRIPTORS ... most are static, but strings and (full) configuration
141 * descriptors are built on demand.
144 #define STRING_MANUFACTURER 0
145 #define STRING_PRODUCT 1
146 #define STRING_SERIALNUM 2
148 /* holds our biggest descriptor */
149 #define USB_DESC_BUFSIZE 256
150 #define USB_BUFSIZE 8192
152 static struct usb_device_descriptor device_desc = {
153 .bLength = sizeof device_desc,
154 .bDescriptorType = USB_DT_DEVICE,
155 .bcdUSB = cpu_to_le16(0x0200),
156 .bDeviceClass = USB_CLASS_PER_INTERFACE,
157 .bDeviceSubClass = 0,
158 .bDeviceProtocol = 0,
159 .idVendor = cpu_to_le16(PRINTER_VENDOR_NUM),
160 .idProduct = cpu_to_le16(PRINTER_PRODUCT_NUM),
161 .bNumConfigurations = 1
164 static struct usb_interface_descriptor intf_desc = {
165 .bLength = sizeof intf_desc,
166 .bDescriptorType = USB_DT_INTERFACE,
167 .bNumEndpoints = 2,
168 .bInterfaceClass = USB_CLASS_PRINTER,
169 .bInterfaceSubClass = 1, /* Printer Sub-Class */
170 .bInterfaceProtocol = 2, /* Bi-Directional */
171 .iInterface = 0
174 static struct usb_endpoint_descriptor fs_ep_in_desc = {
175 .bLength = USB_DT_ENDPOINT_SIZE,
176 .bDescriptorType = USB_DT_ENDPOINT,
177 .bEndpointAddress = USB_DIR_IN,
178 .bmAttributes = USB_ENDPOINT_XFER_BULK
181 static struct usb_endpoint_descriptor fs_ep_out_desc = {
182 .bLength = USB_DT_ENDPOINT_SIZE,
183 .bDescriptorType = USB_DT_ENDPOINT,
184 .bEndpointAddress = USB_DIR_OUT,
185 .bmAttributes = USB_ENDPOINT_XFER_BULK
188 static struct usb_descriptor_header *fs_printer_function[] = {
189 (struct usb_descriptor_header *) &intf_desc,
190 (struct usb_descriptor_header *) &fs_ep_in_desc,
191 (struct usb_descriptor_header *) &fs_ep_out_desc,
192 NULL
196 * usb 2.0 devices need to expose both high speed and full speed
197 * descriptors, unless they only run at full speed.
200 static struct usb_endpoint_descriptor hs_ep_in_desc = {
201 .bLength = USB_DT_ENDPOINT_SIZE,
202 .bDescriptorType = USB_DT_ENDPOINT,
203 .bmAttributes = USB_ENDPOINT_XFER_BULK,
204 .wMaxPacketSize = cpu_to_le16(512)
207 static struct usb_endpoint_descriptor hs_ep_out_desc = {
208 .bLength = USB_DT_ENDPOINT_SIZE,
209 .bDescriptorType = USB_DT_ENDPOINT,
210 .bmAttributes = USB_ENDPOINT_XFER_BULK,
211 .wMaxPacketSize = cpu_to_le16(512)
214 static struct usb_qualifier_descriptor dev_qualifier = {
215 .bLength = sizeof dev_qualifier,
216 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
217 .bcdUSB = cpu_to_le16(0x0200),
218 .bDeviceClass = USB_CLASS_PRINTER,
219 .bNumConfigurations = 1
222 static struct usb_descriptor_header *hs_printer_function[] = {
223 (struct usb_descriptor_header *) &intf_desc,
224 (struct usb_descriptor_header *) &hs_ep_in_desc,
225 (struct usb_descriptor_header *) &hs_ep_out_desc,
226 NULL
229 static struct usb_otg_descriptor otg_descriptor = {
230 .bLength = sizeof otg_descriptor,
231 .bDescriptorType = USB_DT_OTG,
232 .bmAttributes = USB_OTG_SRP,
235 static const struct usb_descriptor_header *otg_desc[] = {
236 (struct usb_descriptor_header *) &otg_descriptor,
237 NULL,
240 /* maxpacket and other transfer characteristics vary by speed. */
241 #define ep_desc(g, hs, fs) (((g)->speed == USB_SPEED_HIGH)?(hs):(fs))
243 /*-------------------------------------------------------------------------*/
245 /* descriptors that are built on-demand */
247 static char manufacturer [50];
248 static char product_desc [40] = DRIVER_DESC;
249 static char serial_num [40] = "1";
250 static char pnp_string [1024] =
251 "XXMFG:linux;MDL:g_printer;CLS:PRINTER;SN:1;";
253 /* static strings, in UTF-8 */
254 static struct usb_string strings [] = {
255 [STRING_MANUFACTURER].s = manufacturer,
256 [STRING_PRODUCT].s = product_desc,
257 [STRING_SERIALNUM].s = serial_num,
258 { } /* end of list */
261 static struct usb_gadget_strings stringtab_dev = {
262 .language = 0x0409, /* en-us */
263 .strings = strings,
266 static struct usb_gadget_strings *dev_strings[] = {
267 &stringtab_dev,
268 NULL,
271 /*-------------------------------------------------------------------------*/
273 static struct usb_request *
274 printer_req_alloc(struct usb_ep *ep, unsigned len, gfp_t gfp_flags)
276 struct usb_request *req;
278 req = usb_ep_alloc_request(ep, gfp_flags);
280 if (req != NULL) {
281 req->length = len;
282 req->buf = kmalloc(len, gfp_flags);
283 if (req->buf == NULL) {
284 usb_ep_free_request(ep, req);
285 return NULL;
289 return req;
292 static void
293 printer_req_free(struct usb_ep *ep, struct usb_request *req)
295 if (ep != NULL && req != NULL) {
296 kfree(req->buf);
297 usb_ep_free_request(ep, req);
301 /*-------------------------------------------------------------------------*/
303 static void rx_complete(struct usb_ep *ep, struct usb_request *req)
305 struct printer_dev *dev = ep->driver_data;
306 int status = req->status;
307 unsigned long flags;
309 spin_lock_irqsave(&dev->lock, flags);
311 list_del_init(&req->list); /* Remode from Active List */
313 switch (status) {
315 /* normal completion */
316 case 0:
317 if (req->actual > 0) {
318 list_add_tail(&req->list, &dev->rx_buffers);
319 DBG(dev, "G_Printer : rx length %d\n", req->actual);
320 } else {
321 list_add(&req->list, &dev->rx_reqs);
323 break;
325 /* software-driven interface shutdown */
326 case -ECONNRESET: /* unlink */
327 case -ESHUTDOWN: /* disconnect etc */
328 VDBG(dev, "rx shutdown, code %d\n", status);
329 list_add(&req->list, &dev->rx_reqs);
330 break;
332 /* for hardware automagic (such as pxa) */
333 case -ECONNABORTED: /* endpoint reset */
334 DBG(dev, "rx %s reset\n", ep->name);
335 list_add(&req->list, &dev->rx_reqs);
336 break;
338 /* data overrun */
339 case -EOVERFLOW:
340 /* FALLTHROUGH */
342 default:
343 DBG(dev, "rx status %d\n", status);
344 list_add(&req->list, &dev->rx_reqs);
345 break;
348 wake_up_interruptible(&dev->rx_wait);
349 spin_unlock_irqrestore(&dev->lock, flags);
352 static void tx_complete(struct usb_ep *ep, struct usb_request *req)
354 struct printer_dev *dev = ep->driver_data;
356 switch (req->status) {
357 default:
358 VDBG(dev, "tx err %d\n", req->status);
359 /* FALLTHROUGH */
360 case -ECONNRESET: /* unlink */
361 case -ESHUTDOWN: /* disconnect etc */
362 break;
363 case 0:
364 break;
367 spin_lock(&dev->lock);
368 /* Take the request struct off the active list and put it on the
369 * free list.
371 list_del_init(&req->list);
372 list_add(&req->list, &dev->tx_reqs);
373 wake_up_interruptible(&dev->tx_wait);
374 if (likely(list_empty(&dev->tx_reqs_active)))
375 wake_up_interruptible(&dev->tx_flush_wait);
377 spin_unlock(&dev->lock);
380 /*-------------------------------------------------------------------------*/
382 static int
383 printer_open(struct inode *inode, struct file *fd)
385 struct printer_dev *dev;
386 unsigned long flags;
387 int ret = -EBUSY;
389 mutex_lock(&printer_mutex);
390 dev = container_of(inode->i_cdev, struct printer_dev, printer_cdev);
392 spin_lock_irqsave(&dev->lock, flags);
394 if (!dev->printer_cdev_open) {
395 dev->printer_cdev_open = 1;
396 fd->private_data = dev;
397 ret = 0;
398 /* Change the printer status to show that it's on-line. */
399 dev->printer_status |= PRINTER_SELECTED;
402 spin_unlock_irqrestore(&dev->lock, flags);
404 DBG(dev, "printer_open returned %x\n", ret);
405 mutex_unlock(&printer_mutex);
406 return ret;
409 static int
410 printer_close(struct inode *inode, struct file *fd)
412 struct printer_dev *dev = fd->private_data;
413 unsigned long flags;
415 spin_lock_irqsave(&dev->lock, flags);
416 dev->printer_cdev_open = 0;
417 fd->private_data = NULL;
418 /* Change printer status to show that the printer is off-line. */
419 dev->printer_status &= ~PRINTER_SELECTED;
420 spin_unlock_irqrestore(&dev->lock, flags);
422 DBG(dev, "printer_close\n");
424 return 0;
427 /* This function must be called with interrupts turned off. */
428 static void
429 setup_rx_reqs(struct printer_dev *dev)
431 struct usb_request *req;
433 while (likely(!list_empty(&dev->rx_reqs))) {
434 int error;
436 req = container_of(dev->rx_reqs.next,
437 struct usb_request, list);
438 list_del_init(&req->list);
440 /* The USB Host sends us whatever amount of data it wants to
441 * so we always set the length field to the full USB_BUFSIZE.
442 * If the amount of data is more than the read() caller asked
443 * for it will be stored in the request buffer until it is
444 * asked for by read().
446 req->length = USB_BUFSIZE;
447 req->complete = rx_complete;
449 error = usb_ep_queue(dev->out_ep, req, GFP_ATOMIC);
450 if (error) {
451 DBG(dev, "rx submit --> %d\n", error);
452 list_add(&req->list, &dev->rx_reqs);
453 break;
454 } else {
455 list_add(&req->list, &dev->rx_reqs_active);
460 static ssize_t
461 printer_read(struct file *fd, char __user *buf, size_t len, loff_t *ptr)
463 struct printer_dev *dev = fd->private_data;
464 unsigned long flags;
465 size_t size;
466 size_t bytes_copied;
467 struct usb_request *req;
468 /* This is a pointer to the current USB rx request. */
469 struct usb_request *current_rx_req;
470 /* This is the number of bytes in the current rx buffer. */
471 size_t current_rx_bytes;
472 /* This is a pointer to the current rx buffer. */
473 u8 *current_rx_buf;
475 if (len == 0)
476 return -EINVAL;
478 DBG(dev, "printer_read trying to read %d bytes\n", (int)len);
480 mutex_lock(&dev->lock_printer_io);
481 spin_lock_irqsave(&dev->lock, flags);
483 /* We will use this flag later to check if a printer reset happened
484 * after we turn interrupts back on.
486 dev->reset_printer = 0;
488 setup_rx_reqs(dev);
490 bytes_copied = 0;
491 current_rx_req = dev->current_rx_req;
492 current_rx_bytes = dev->current_rx_bytes;
493 current_rx_buf = dev->current_rx_buf;
494 dev->current_rx_req = NULL;
495 dev->current_rx_bytes = 0;
496 dev->current_rx_buf = NULL;
498 /* Check if there is any data in the read buffers. Please note that
499 * current_rx_bytes is the number of bytes in the current rx buffer.
500 * If it is zero then check if there are any other rx_buffers that
501 * are on the completed list. We are only out of data if all rx
502 * buffers are empty.
504 if ((current_rx_bytes == 0) &&
505 (likely(list_empty(&dev->rx_buffers)))) {
506 /* Turn interrupts back on before sleeping. */
507 spin_unlock_irqrestore(&dev->lock, flags);
510 * If no data is available check if this is a NON-Blocking
511 * call or not.
513 if (fd->f_flags & (O_NONBLOCK|O_NDELAY)) {
514 mutex_unlock(&dev->lock_printer_io);
515 return -EAGAIN;
518 /* Sleep until data is available */
519 wait_event_interruptible(dev->rx_wait,
520 (likely(!list_empty(&dev->rx_buffers))));
521 spin_lock_irqsave(&dev->lock, flags);
524 /* We have data to return then copy it to the caller's buffer.*/
525 while ((current_rx_bytes || likely(!list_empty(&dev->rx_buffers)))
526 && len) {
527 if (current_rx_bytes == 0) {
528 req = container_of(dev->rx_buffers.next,
529 struct usb_request, list);
530 list_del_init(&req->list);
532 if (req->actual && req->buf) {
533 current_rx_req = req;
534 current_rx_bytes = req->actual;
535 current_rx_buf = req->buf;
536 } else {
537 list_add(&req->list, &dev->rx_reqs);
538 continue;
542 /* Don't leave irqs off while doing memory copies */
543 spin_unlock_irqrestore(&dev->lock, flags);
545 if (len > current_rx_bytes)
546 size = current_rx_bytes;
547 else
548 size = len;
550 size -= copy_to_user(buf, current_rx_buf, size);
551 bytes_copied += size;
552 len -= size;
553 buf += size;
555 spin_lock_irqsave(&dev->lock, flags);
557 /* We've disconnected or reset so return. */
558 if (dev->reset_printer) {
559 list_add(&current_rx_req->list, &dev->rx_reqs);
560 spin_unlock_irqrestore(&dev->lock, flags);
561 mutex_unlock(&dev->lock_printer_io);
562 return -EAGAIN;
565 /* If we not returning all the data left in this RX request
566 * buffer then adjust the amount of data left in the buffer.
567 * Othewise if we are done with this RX request buffer then
568 * requeue it to get any incoming data from the USB host.
570 if (size < current_rx_bytes) {
571 current_rx_bytes -= size;
572 current_rx_buf += size;
573 } else {
574 list_add(&current_rx_req->list, &dev->rx_reqs);
575 current_rx_bytes = 0;
576 current_rx_buf = NULL;
577 current_rx_req = NULL;
581 dev->current_rx_req = current_rx_req;
582 dev->current_rx_bytes = current_rx_bytes;
583 dev->current_rx_buf = current_rx_buf;
585 spin_unlock_irqrestore(&dev->lock, flags);
586 mutex_unlock(&dev->lock_printer_io);
588 DBG(dev, "printer_read returned %d bytes\n", (int)bytes_copied);
590 if (bytes_copied)
591 return bytes_copied;
592 else
593 return -EAGAIN;
596 static ssize_t
597 printer_write(struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
599 struct printer_dev *dev = fd->private_data;
600 unsigned long flags;
601 size_t size; /* Amount of data in a TX request. */
602 size_t bytes_copied = 0;
603 struct usb_request *req;
605 DBG(dev, "printer_write trying to send %d bytes\n", (int)len);
607 if (len == 0)
608 return -EINVAL;
610 mutex_lock(&dev->lock_printer_io);
611 spin_lock_irqsave(&dev->lock, flags);
613 /* Check if a printer reset happens while we have interrupts on */
614 dev->reset_printer = 0;
616 /* Check if there is any available write buffers */
617 if (likely(list_empty(&dev->tx_reqs))) {
618 /* Turn interrupts back on before sleeping. */
619 spin_unlock_irqrestore(&dev->lock, flags);
622 * If write buffers are available check if this is
623 * a NON-Blocking call or not.
625 if (fd->f_flags & (O_NONBLOCK|O_NDELAY)) {
626 mutex_unlock(&dev->lock_printer_io);
627 return -EAGAIN;
630 /* Sleep until a write buffer is available */
631 wait_event_interruptible(dev->tx_wait,
632 (likely(!list_empty(&dev->tx_reqs))));
633 spin_lock_irqsave(&dev->lock, flags);
636 while (likely(!list_empty(&dev->tx_reqs)) && len) {
638 if (len > USB_BUFSIZE)
639 size = USB_BUFSIZE;
640 else
641 size = len;
643 req = container_of(dev->tx_reqs.next, struct usb_request,
644 list);
645 list_del_init(&req->list);
647 req->complete = tx_complete;
648 req->length = size;
650 /* Check if we need to send a zero length packet. */
651 if (len > size)
652 /* They will be more TX requests so no yet. */
653 req->zero = 0;
654 else
655 /* If the data amount is not a multple of the
656 * maxpacket size then send a zero length packet.
658 req->zero = ((len % dev->in_ep->maxpacket) == 0);
660 /* Don't leave irqs off while doing memory copies */
661 spin_unlock_irqrestore(&dev->lock, flags);
663 if (copy_from_user(req->buf, buf, size)) {
664 list_add(&req->list, &dev->tx_reqs);
665 mutex_unlock(&dev->lock_printer_io);
666 return bytes_copied;
669 bytes_copied += size;
670 len -= size;
671 buf += size;
673 spin_lock_irqsave(&dev->lock, flags);
675 /* We've disconnected or reset so free the req and buffer */
676 if (dev->reset_printer) {
677 list_add(&req->list, &dev->tx_reqs);
678 spin_unlock_irqrestore(&dev->lock, flags);
679 mutex_unlock(&dev->lock_printer_io);
680 return -EAGAIN;
683 if (usb_ep_queue(dev->in_ep, req, GFP_ATOMIC)) {
684 list_add(&req->list, &dev->tx_reqs);
685 spin_unlock_irqrestore(&dev->lock, flags);
686 mutex_unlock(&dev->lock_printer_io);
687 return -EAGAIN;
690 list_add(&req->list, &dev->tx_reqs_active);
694 spin_unlock_irqrestore(&dev->lock, flags);
695 mutex_unlock(&dev->lock_printer_io);
697 DBG(dev, "printer_write sent %d bytes\n", (int)bytes_copied);
699 if (bytes_copied) {
700 return bytes_copied;
701 } else {
702 return -EAGAIN;
706 static int
707 printer_fsync(struct file *fd, loff_t start, loff_t end, int datasync)
709 struct printer_dev *dev = fd->private_data;
710 struct inode *inode = fd->f_path.dentry->d_inode;
711 unsigned long flags;
712 int tx_list_empty;
714 mutex_lock(&inode->i_mutex);
715 spin_lock_irqsave(&dev->lock, flags);
716 tx_list_empty = (likely(list_empty(&dev->tx_reqs)));
717 spin_unlock_irqrestore(&dev->lock, flags);
719 if (!tx_list_empty) {
720 /* Sleep until all data has been sent */
721 wait_event_interruptible(dev->tx_flush_wait,
722 (likely(list_empty(&dev->tx_reqs_active))));
724 mutex_unlock(&inode->i_mutex);
726 return 0;
729 static unsigned int
730 printer_poll(struct file *fd, poll_table *wait)
732 struct printer_dev *dev = fd->private_data;
733 unsigned long flags;
734 int status = 0;
736 mutex_lock(&dev->lock_printer_io);
737 spin_lock_irqsave(&dev->lock, flags);
738 setup_rx_reqs(dev);
739 spin_unlock_irqrestore(&dev->lock, flags);
740 mutex_unlock(&dev->lock_printer_io);
742 poll_wait(fd, &dev->rx_wait, wait);
743 poll_wait(fd, &dev->tx_wait, wait);
745 spin_lock_irqsave(&dev->lock, flags);
746 if (likely(!list_empty(&dev->tx_reqs)))
747 status |= POLLOUT | POLLWRNORM;
749 if (likely(dev->current_rx_bytes) ||
750 likely(!list_empty(&dev->rx_buffers)))
751 status |= POLLIN | POLLRDNORM;
753 spin_unlock_irqrestore(&dev->lock, flags);
755 return status;
758 static long
759 printer_ioctl(struct file *fd, unsigned int code, unsigned long arg)
761 struct printer_dev *dev = fd->private_data;
762 unsigned long flags;
763 int status = 0;
765 DBG(dev, "printer_ioctl: cmd=0x%4.4x, arg=%lu\n", code, arg);
767 /* handle ioctls */
769 spin_lock_irqsave(&dev->lock, flags);
771 switch (code) {
772 case GADGET_GET_PRINTER_STATUS:
773 status = (int)dev->printer_status;
774 break;
775 case GADGET_SET_PRINTER_STATUS:
776 dev->printer_status = (u8)arg;
777 break;
778 default:
779 /* could not handle ioctl */
780 DBG(dev, "printer_ioctl: ERROR cmd=0x%4.4xis not supported\n",
781 code);
782 status = -ENOTTY;
785 spin_unlock_irqrestore(&dev->lock, flags);
787 return status;
790 /* used after endpoint configuration */
791 static const struct file_operations printer_io_operations = {
792 .owner = THIS_MODULE,
793 .open = printer_open,
794 .read = printer_read,
795 .write = printer_write,
796 .fsync = printer_fsync,
797 .poll = printer_poll,
798 .unlocked_ioctl = printer_ioctl,
799 .release = printer_close,
800 .llseek = noop_llseek,
803 /*-------------------------------------------------------------------------*/
805 static int
806 set_printer_interface(struct printer_dev *dev)
808 int result = 0;
810 dev->in_ep->desc = ep_desc(dev->gadget, &hs_ep_in_desc, &fs_ep_in_desc);
811 dev->in_ep->driver_data = dev;
813 dev->out_ep->desc = ep_desc(dev->gadget, &hs_ep_out_desc,
814 &fs_ep_out_desc);
815 dev->out_ep->driver_data = dev;
817 result = usb_ep_enable(dev->in_ep);
818 if (result != 0) {
819 DBG(dev, "enable %s --> %d\n", dev->in_ep->name, result);
820 goto done;
823 result = usb_ep_enable(dev->out_ep);
824 if (result != 0) {
825 DBG(dev, "enable %s --> %d\n", dev->in_ep->name, result);
826 goto done;
829 done:
830 /* on error, disable any endpoints */
831 if (result != 0) {
832 (void) usb_ep_disable(dev->in_ep);
833 (void) usb_ep_disable(dev->out_ep);
834 dev->in_ep->desc = NULL;
835 dev->out_ep->desc = NULL;
838 /* caller is responsible for cleanup on error */
839 return result;
842 static void printer_reset_interface(struct printer_dev *dev)
844 if (dev->interface < 0)
845 return;
847 DBG(dev, "%s\n", __func__);
849 if (dev->in_ep->desc)
850 usb_ep_disable(dev->in_ep);
852 if (dev->out_ep->desc)
853 usb_ep_disable(dev->out_ep);
855 dev->in_ep->desc = NULL;
856 dev->out_ep->desc = NULL;
857 dev->interface = -1;
860 /* Change our operational Interface. */
861 static int set_interface(struct printer_dev *dev, unsigned number)
863 int result = 0;
865 /* Free the current interface */
866 printer_reset_interface(dev);
868 result = set_printer_interface(dev);
869 if (result)
870 printer_reset_interface(dev);
871 else
872 dev->interface = number;
874 if (!result)
875 INFO(dev, "Using interface %x\n", number);
877 return result;
880 static void printer_soft_reset(struct printer_dev *dev)
882 struct usb_request *req;
884 INFO(dev, "Received Printer Reset Request\n");
886 if (usb_ep_disable(dev->in_ep))
887 DBG(dev, "Failed to disable USB in_ep\n");
888 if (usb_ep_disable(dev->out_ep))
889 DBG(dev, "Failed to disable USB out_ep\n");
891 if (dev->current_rx_req != NULL) {
892 list_add(&dev->current_rx_req->list, &dev->rx_reqs);
893 dev->current_rx_req = NULL;
895 dev->current_rx_bytes = 0;
896 dev->current_rx_buf = NULL;
897 dev->reset_printer = 1;
899 while (likely(!(list_empty(&dev->rx_buffers)))) {
900 req = container_of(dev->rx_buffers.next, struct usb_request,
901 list);
902 list_del_init(&req->list);
903 list_add(&req->list, &dev->rx_reqs);
906 while (likely(!(list_empty(&dev->rx_reqs_active)))) {
907 req = container_of(dev->rx_buffers.next, struct usb_request,
908 list);
909 list_del_init(&req->list);
910 list_add(&req->list, &dev->rx_reqs);
913 while (likely(!(list_empty(&dev->tx_reqs_active)))) {
914 req = container_of(dev->tx_reqs_active.next,
915 struct usb_request, list);
916 list_del_init(&req->list);
917 list_add(&req->list, &dev->tx_reqs);
920 if (usb_ep_enable(dev->in_ep))
921 DBG(dev, "Failed to enable USB in_ep\n");
922 if (usb_ep_enable(dev->out_ep))
923 DBG(dev, "Failed to enable USB out_ep\n");
925 wake_up_interruptible(&dev->rx_wait);
926 wake_up_interruptible(&dev->tx_wait);
927 wake_up_interruptible(&dev->tx_flush_wait);
930 /*-------------------------------------------------------------------------*/
933 * The setup() callback implements all the ep0 functionality that's not
934 * handled lower down.
936 static int printer_func_setup(struct usb_function *f,
937 const struct usb_ctrlrequest *ctrl)
939 struct printer_dev *dev = container_of(f, struct printer_dev, function);
940 struct usb_composite_dev *cdev = f->config->cdev;
941 struct usb_request *req = cdev->req;
942 int value = -EOPNOTSUPP;
943 u16 wIndex = le16_to_cpu(ctrl->wIndex);
944 u16 wValue = le16_to_cpu(ctrl->wValue);
945 u16 wLength = le16_to_cpu(ctrl->wLength);
947 DBG(dev, "ctrl req%02x.%02x v%04x i%04x l%d\n",
948 ctrl->bRequestType, ctrl->bRequest, wValue, wIndex, wLength);
950 switch (ctrl->bRequestType&USB_TYPE_MASK) {
951 case USB_TYPE_CLASS:
952 switch (ctrl->bRequest) {
953 case 0: /* Get the IEEE-1284 PNP String */
954 /* Only one printer interface is supported. */
955 if ((wIndex>>8) != dev->interface)
956 break;
958 value = (pnp_string[0]<<8)|pnp_string[1];
959 memcpy(req->buf, pnp_string, value);
960 DBG(dev, "1284 PNP String: %x %s\n", value,
961 &pnp_string[2]);
962 break;
964 case 1: /* Get Port Status */
965 /* Only one printer interface is supported. */
966 if (wIndex != dev->interface)
967 break;
969 *(u8 *)req->buf = dev->printer_status;
970 value = min(wLength, (u16) 1);
971 break;
973 case 2: /* Soft Reset */
974 /* Only one printer interface is supported. */
975 if (wIndex != dev->interface)
976 break;
978 printer_soft_reset(dev);
980 value = 0;
981 break;
983 default:
984 goto unknown;
986 break;
988 default:
989 unknown:
990 VDBG(dev,
991 "unknown ctrl req%02x.%02x v%04x i%04x l%d\n",
992 ctrl->bRequestType, ctrl->bRequest,
993 wValue, wIndex, wLength);
994 break;
996 /* host either stalls (value < 0) or reports success */
997 return value;
1000 static int __init printer_func_bind(struct usb_configuration *c,
1001 struct usb_function *f)
1003 struct printer_dev *dev = container_of(f, struct printer_dev, function);
1004 struct usb_composite_dev *cdev = c->cdev;
1005 struct usb_ep *in_ep, *out_ep;
1006 int id;
1008 id = usb_interface_id(c, f);
1009 if (id < 0)
1010 return id;
1011 intf_desc.bInterfaceNumber = id;
1013 /* all we really need is bulk IN/OUT */
1014 in_ep = usb_ep_autoconfig(cdev->gadget, &fs_ep_in_desc);
1015 if (!in_ep) {
1016 autoconf_fail:
1017 dev_err(&cdev->gadget->dev, "can't autoconfigure on %s\n",
1018 cdev->gadget->name);
1019 return -ENODEV;
1021 in_ep->driver_data = in_ep; /* claim */
1023 out_ep = usb_ep_autoconfig(cdev->gadget, &fs_ep_out_desc);
1024 if (!out_ep)
1025 goto autoconf_fail;
1026 out_ep->driver_data = out_ep; /* claim */
1028 /* assumes that all endpoints are dual-speed */
1029 hs_ep_in_desc.bEndpointAddress = fs_ep_in_desc.bEndpointAddress;
1030 hs_ep_out_desc.bEndpointAddress = fs_ep_out_desc.bEndpointAddress;
1032 dev->in_ep = in_ep;
1033 dev->out_ep = out_ep;
1034 return 0;
1037 static void printer_func_unbind(struct usb_configuration *c,
1038 struct usb_function *f)
1042 static int printer_func_set_alt(struct usb_function *f,
1043 unsigned intf, unsigned alt)
1045 struct printer_dev *dev = container_of(f, struct printer_dev, function);
1046 int ret = -ENOTSUPP;
1048 if (!alt)
1049 ret = set_interface(dev, intf);
1051 return ret;
1054 static void printer_func_disable(struct usb_function *f)
1056 struct printer_dev *dev = container_of(f, struct printer_dev, function);
1057 unsigned long flags;
1059 DBG(dev, "%s\n", __func__);
1061 spin_lock_irqsave(&dev->lock, flags);
1062 printer_reset_interface(dev);
1063 spin_unlock_irqrestore(&dev->lock, flags);
1066 static void printer_cfg_unbind(struct usb_configuration *c)
1068 struct printer_dev *dev;
1069 struct usb_request *req;
1071 dev = &usb_printer_gadget;
1073 DBG(dev, "%s\n", __func__);
1075 /* Remove sysfs files */
1076 device_destroy(usb_gadget_class, g_printer_devno);
1078 /* Remove Character Device */
1079 cdev_del(&dev->printer_cdev);
1081 /* we must already have been disconnected ... no i/o may be active */
1082 WARN_ON(!list_empty(&dev->tx_reqs_active));
1083 WARN_ON(!list_empty(&dev->rx_reqs_active));
1085 /* Free all memory for this driver. */
1086 while (!list_empty(&dev->tx_reqs)) {
1087 req = container_of(dev->tx_reqs.next, struct usb_request,
1088 list);
1089 list_del(&req->list);
1090 printer_req_free(dev->in_ep, req);
1093 if (dev->current_rx_req != NULL)
1094 printer_req_free(dev->out_ep, dev->current_rx_req);
1096 while (!list_empty(&dev->rx_reqs)) {
1097 req = container_of(dev->rx_reqs.next,
1098 struct usb_request, list);
1099 list_del(&req->list);
1100 printer_req_free(dev->out_ep, req);
1103 while (!list_empty(&dev->rx_buffers)) {
1104 req = container_of(dev->rx_buffers.next,
1105 struct usb_request, list);
1106 list_del(&req->list);
1107 printer_req_free(dev->out_ep, req);
1111 static struct usb_configuration printer_cfg_driver = {
1112 .label = "printer",
1113 .unbind = printer_cfg_unbind,
1114 .bConfigurationValue = 1,
1115 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
1118 static int __init printer_bind_config(struct usb_configuration *c)
1120 struct usb_gadget *gadget = c->cdev->gadget;
1121 struct printer_dev *dev;
1122 int status = -ENOMEM;
1123 int gcnum;
1124 size_t len;
1125 u32 i;
1126 struct usb_request *req;
1128 usb_ep_autoconfig_reset(gadget);
1130 dev = &usb_printer_gadget;
1132 dev->function.name = shortname;
1133 dev->function.descriptors = fs_printer_function;
1134 dev->function.hs_descriptors = hs_printer_function;
1135 dev->function.bind = printer_func_bind;
1136 dev->function.setup = printer_func_setup;
1137 dev->function.unbind = printer_func_unbind;
1138 dev->function.set_alt = printer_func_set_alt;
1139 dev->function.disable = printer_func_disable;
1141 status = usb_add_function(c, &dev->function);
1142 if (status)
1143 return status;
1145 /* Setup the sysfs files for the printer gadget. */
1146 dev->pdev = device_create(usb_gadget_class, NULL, g_printer_devno,
1147 NULL, "g_printer");
1148 if (IS_ERR(dev->pdev)) {
1149 ERROR(dev, "Failed to create device: g_printer\n");
1150 goto fail;
1154 * Register a character device as an interface to a user mode
1155 * program that handles the printer specific functionality.
1157 cdev_init(&dev->printer_cdev, &printer_io_operations);
1158 dev->printer_cdev.owner = THIS_MODULE;
1159 status = cdev_add(&dev->printer_cdev, g_printer_devno, 1);
1160 if (status) {
1161 ERROR(dev, "Failed to open char device\n");
1162 goto fail;
1165 gcnum = usb_gadget_controller_number(gadget);
1166 if (gcnum >= 0) {
1167 device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
1168 } else {
1169 dev_warn(&gadget->dev, "controller '%s' not recognized\n",
1170 gadget->name);
1171 /* unrecognized, but safe unless bulk is REALLY quirky */
1172 device_desc.bcdDevice =
1173 cpu_to_le16(0xFFFF);
1175 snprintf(manufacturer, sizeof(manufacturer), "%s %s with %s",
1176 init_utsname()->sysname, init_utsname()->release,
1177 gadget->name);
1179 if (iSerialNum)
1180 strlcpy(serial_num, iSerialNum, sizeof serial_num);
1182 if (iPNPstring)
1183 strlcpy(&pnp_string[2], iPNPstring, (sizeof pnp_string)-2);
1185 len = strlen(pnp_string);
1186 pnp_string[0] = (len >> 8) & 0xFF;
1187 pnp_string[1] = len & 0xFF;
1189 usb_gadget_set_selfpowered(gadget);
1191 if (gadget->is_otg) {
1192 otg_descriptor.bmAttributes |= USB_OTG_HNP;
1193 printer_cfg_driver.descriptors = otg_desc;
1194 printer_cfg_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1197 spin_lock_init(&dev->lock);
1198 mutex_init(&dev->lock_printer_io);
1199 INIT_LIST_HEAD(&dev->tx_reqs);
1200 INIT_LIST_HEAD(&dev->tx_reqs_active);
1201 INIT_LIST_HEAD(&dev->rx_reqs);
1202 INIT_LIST_HEAD(&dev->rx_reqs_active);
1203 INIT_LIST_HEAD(&dev->rx_buffers);
1204 init_waitqueue_head(&dev->rx_wait);
1205 init_waitqueue_head(&dev->tx_wait);
1206 init_waitqueue_head(&dev->tx_flush_wait);
1208 dev->interface = -1;
1209 dev->printer_cdev_open = 0;
1210 dev->printer_status = PRINTER_NOT_ERROR;
1211 dev->current_rx_req = NULL;
1212 dev->current_rx_bytes = 0;
1213 dev->current_rx_buf = NULL;
1215 for (i = 0; i < QLEN; i++) {
1216 req = printer_req_alloc(dev->in_ep, USB_BUFSIZE, GFP_KERNEL);
1217 if (!req) {
1218 while (!list_empty(&dev->tx_reqs)) {
1219 req = container_of(dev->tx_reqs.next,
1220 struct usb_request, list);
1221 list_del(&req->list);
1222 printer_req_free(dev->in_ep, req);
1224 return -ENOMEM;
1226 list_add(&req->list, &dev->tx_reqs);
1229 for (i = 0; i < QLEN; i++) {
1230 req = printer_req_alloc(dev->out_ep, USB_BUFSIZE, GFP_KERNEL);
1231 if (!req) {
1232 while (!list_empty(&dev->rx_reqs)) {
1233 req = container_of(dev->rx_reqs.next,
1234 struct usb_request, list);
1235 list_del(&req->list);
1236 printer_req_free(dev->out_ep, req);
1238 return -ENOMEM;
1240 list_add(&req->list, &dev->rx_reqs);
1243 /* finish hookup to lower layer ... */
1244 dev->gadget = gadget;
1246 INFO(dev, "%s, version: " DRIVER_VERSION "\n", driver_desc);
1247 return 0;
1249 fail:
1250 printer_cfg_unbind(c);
1251 return status;
1254 static int printer_unbind(struct usb_composite_dev *cdev)
1256 return 0;
1259 static int __init printer_bind(struct usb_composite_dev *cdev)
1261 int ret;
1263 ret = usb_string_ids_tab(cdev, strings);
1264 if (ret < 0)
1265 return ret;
1266 device_desc.iManufacturer = strings[STRING_MANUFACTURER].id;
1267 device_desc.iProduct = strings[STRING_PRODUCT].id;
1268 device_desc.iSerialNumber = strings[STRING_SERIALNUM].id;
1270 ret = usb_add_config(cdev, &printer_cfg_driver, printer_bind_config);
1271 return ret;
1274 static struct usb_composite_driver printer_driver = {
1275 .name = shortname,
1276 .dev = &device_desc,
1277 .strings = dev_strings,
1278 .max_speed = USB_SPEED_HIGH,
1279 .unbind = printer_unbind,
1282 static int __init
1283 init(void)
1285 int status;
1287 usb_gadget_class = class_create(THIS_MODULE, "usb_printer_gadget");
1288 if (IS_ERR(usb_gadget_class)) {
1289 status = PTR_ERR(usb_gadget_class);
1290 pr_err("unable to create usb_gadget class %d\n", status);
1291 return status;
1294 status = alloc_chrdev_region(&g_printer_devno, 0, 1,
1295 "USB printer gadget");
1296 if (status) {
1297 pr_err("alloc_chrdev_region %d\n", status);
1298 class_destroy(usb_gadget_class);
1299 return status;
1302 status = usb_composite_probe(&printer_driver, printer_bind);
1303 if (status) {
1304 class_destroy(usb_gadget_class);
1305 unregister_chrdev_region(g_printer_devno, 1);
1306 pr_err("usb_gadget_probe_driver %x\n", status);
1309 return status;
1311 module_init(init);
1313 static void __exit
1314 cleanup(void)
1316 mutex_lock(&usb_printer_gadget.lock_printer_io);
1317 usb_composite_unregister(&printer_driver);
1318 unregister_chrdev_region(g_printer_devno, 1);
1319 class_destroy(usb_gadget_class);
1320 mutex_unlock(&usb_printer_gadget.lock_printer_io);
1322 module_exit(cleanup);
1324 MODULE_DESCRIPTION(DRIVER_DESC);
1325 MODULE_AUTHOR("Craig Nadler");
1326 MODULE_LICENSE("GPL");