iwlwifi: introduce host commands callbacks
[linux/fpc-iii.git] / drivers / usb / gadget / printer.c
blob2c32bd08ee7db2c6051df00de37f709de9041a10
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.
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>
37 #include <linux/fs.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>
44 #include <linux/io.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 /*-------------------------------------------------------------------------*/
68 struct printer_dev {
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 */
74 u8 config;
75 s8 interface;
76 struct usb_ep *in_ep, *out_ep;
77 const struct usb_endpoint_descriptor
78 *in, *out;
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;
92 u8 *current_rx_buf;
93 u8 printer_status;
94 u8 reset_printer;
95 struct cdev printer_cdev;
96 struct device *pdev;
97 u8 printer_cdev_open;
98 wait_queue_head_t wait;
101 static struct printer_dev usb_printer_gadget;
103 /*-------------------------------------------------------------------------*/
105 /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
106 * Instead: allocate your own, using normal USB-IF procedures.
109 /* Thanks to NetChip Technologies for donating this product ID.
111 #define PRINTER_VENDOR_NUM 0x0525 /* NetChip */
112 #define PRINTER_PRODUCT_NUM 0xa4a8 /* Linux-USB Printer Gadget */
114 /* Some systems will want different product identifers published in the
115 * device descriptor, either numbers or strings or both. These string
116 * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
119 static ushort __initdata idVendor;
120 module_param(idVendor, ushort, S_IRUGO);
121 MODULE_PARM_DESC(idVendor, "USB Vendor ID");
123 static ushort __initdata idProduct;
124 module_param(idProduct, ushort, S_IRUGO);
125 MODULE_PARM_DESC(idProduct, "USB Product ID");
127 static ushort __initdata bcdDevice;
128 module_param(bcdDevice, ushort, S_IRUGO);
129 MODULE_PARM_DESC(bcdDevice, "USB Device version (BCD)");
131 static char *__initdata iManufacturer;
132 module_param(iManufacturer, charp, S_IRUGO);
133 MODULE_PARM_DESC(iManufacturer, "USB Manufacturer string");
135 static char *__initdata iProduct;
136 module_param(iProduct, charp, S_IRUGO);
137 MODULE_PARM_DESC(iProduct, "USB Product string");
139 static char *__initdata iSerialNum;
140 module_param(iSerialNum, charp, S_IRUGO);
141 MODULE_PARM_DESC(iSerialNum, "1");
143 static char *__initdata iPNPstring;
144 module_param(iPNPstring, charp, S_IRUGO);
145 MODULE_PARM_DESC(iPNPstring, "MFG:linux;MDL:g_printer;CLS:PRINTER;SN:1;");
147 /* Number of requests to allocate per endpoint, not used for ep0. */
148 static unsigned qlen = 10;
149 module_param(qlen, uint, S_IRUGO|S_IWUSR);
151 #define QLEN qlen
153 #ifdef CONFIG_USB_GADGET_DUALSPEED
154 #define DEVSPEED USB_SPEED_HIGH
155 #else /* full speed (low speed doesn't do bulk) */
156 #define DEVSPEED USB_SPEED_FULL
157 #endif
159 /*-------------------------------------------------------------------------*/
161 #define xprintk(d, level, fmt, args...) \
162 printk(level "%s: " fmt, DRIVER_DESC, ## args)
164 #ifdef DEBUG
165 #define DBG(dev, fmt, args...) \
166 xprintk(dev, KERN_DEBUG, fmt, ## args)
167 #else
168 #define DBG(dev, fmt, args...) \
169 do { } while (0)
170 #endif /* DEBUG */
172 #ifdef VERBOSE
173 #define VDBG(dev, fmt, args...) \
174 xprintk(dev, KERN_DEBUG, fmt, ## args)
175 #else
176 #define VDBG(dev, fmt, args...) \
177 do { } while (0)
178 #endif /* VERBOSE */
180 #define ERROR(dev, fmt, args...) \
181 xprintk(dev, KERN_ERR, fmt, ## args)
182 #define WARN(dev, fmt, args...) \
183 xprintk(dev, KERN_WARNING, fmt, ## args)
184 #define INFO(dev, fmt, args...) \
185 xprintk(dev, KERN_INFO, fmt, ## args)
187 /*-------------------------------------------------------------------------*/
189 /* USB DRIVER HOOKUP (to the hardware driver, below us), mostly
190 * ep0 implementation: descriptors, config management, setup().
191 * also optional class-specific notification interrupt transfer.
195 * DESCRIPTORS ... most are static, but strings and (full) configuration
196 * descriptors are built on demand.
199 #define STRING_MANUFACTURER 1
200 #define STRING_PRODUCT 2
201 #define STRING_SERIALNUM 3
203 /* holds our biggest descriptor */
204 #define USB_DESC_BUFSIZE 256
205 #define USB_BUFSIZE 8192
207 /* This device advertises one configuration. */
208 #define DEV_CONFIG_VALUE 1
209 #define PRINTER_INTERFACE 0
211 static struct usb_device_descriptor device_desc = {
212 .bLength = sizeof device_desc,
213 .bDescriptorType = USB_DT_DEVICE,
214 .bcdUSB = __constant_cpu_to_le16(0x0200),
215 .bDeviceClass = USB_CLASS_PER_INTERFACE,
216 .bDeviceSubClass = 0,
217 .bDeviceProtocol = 0,
218 .idVendor = __constant_cpu_to_le16(PRINTER_VENDOR_NUM),
219 .idProduct = __constant_cpu_to_le16(PRINTER_PRODUCT_NUM),
220 .iManufacturer = STRING_MANUFACTURER,
221 .iProduct = STRING_PRODUCT,
222 .iSerialNumber = STRING_SERIALNUM,
223 .bNumConfigurations = 1
226 static struct usb_otg_descriptor otg_desc = {
227 .bLength = sizeof otg_desc,
228 .bDescriptorType = USB_DT_OTG,
229 .bmAttributes = USB_OTG_SRP
232 static struct usb_config_descriptor config_desc = {
233 .bLength = sizeof config_desc,
234 .bDescriptorType = USB_DT_CONFIG,
236 /* compute wTotalLength on the fly */
237 .bNumInterfaces = 1,
238 .bConfigurationValue = DEV_CONFIG_VALUE,
239 .iConfiguration = 0,
240 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
241 .bMaxPower = 1 /* Self-Powered */
244 static struct usb_interface_descriptor intf_desc = {
245 .bLength = sizeof intf_desc,
246 .bDescriptorType = USB_DT_INTERFACE,
247 .bInterfaceNumber = PRINTER_INTERFACE,
248 .bNumEndpoints = 2,
249 .bInterfaceClass = USB_CLASS_PRINTER,
250 .bInterfaceSubClass = 1, /* Printer Sub-Class */
251 .bInterfaceProtocol = 2, /* Bi-Directional */
252 .iInterface = 0
255 static struct usb_endpoint_descriptor fs_ep_in_desc = {
256 .bLength = USB_DT_ENDPOINT_SIZE,
257 .bDescriptorType = USB_DT_ENDPOINT,
258 .bEndpointAddress = USB_DIR_IN,
259 .bmAttributes = USB_ENDPOINT_XFER_BULK
262 static struct usb_endpoint_descriptor fs_ep_out_desc = {
263 .bLength = USB_DT_ENDPOINT_SIZE,
264 .bDescriptorType = USB_DT_ENDPOINT,
265 .bEndpointAddress = USB_DIR_OUT,
266 .bmAttributes = USB_ENDPOINT_XFER_BULK
269 static const struct usb_descriptor_header *fs_printer_function [11] = {
270 (struct usb_descriptor_header *) &otg_desc,
271 (struct usb_descriptor_header *) &intf_desc,
272 (struct usb_descriptor_header *) &fs_ep_in_desc,
273 (struct usb_descriptor_header *) &fs_ep_out_desc,
274 NULL
277 #ifdef CONFIG_USB_GADGET_DUALSPEED
280 * usb 2.0 devices need to expose both high speed and full speed
281 * descriptors, unless they only run at full speed.
284 static struct usb_endpoint_descriptor hs_ep_in_desc = {
285 .bLength = USB_DT_ENDPOINT_SIZE,
286 .bDescriptorType = USB_DT_ENDPOINT,
287 .bmAttributes = USB_ENDPOINT_XFER_BULK,
288 .wMaxPacketSize = __constant_cpu_to_le16(512)
291 static struct usb_endpoint_descriptor hs_ep_out_desc = {
292 .bLength = USB_DT_ENDPOINT_SIZE,
293 .bDescriptorType = USB_DT_ENDPOINT,
294 .bmAttributes = USB_ENDPOINT_XFER_BULK,
295 .wMaxPacketSize = __constant_cpu_to_le16(512)
298 static struct usb_qualifier_descriptor dev_qualifier = {
299 .bLength = sizeof dev_qualifier,
300 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
301 .bcdUSB = __constant_cpu_to_le16(0x0200),
302 .bDeviceClass = USB_CLASS_PRINTER,
303 .bNumConfigurations = 1
306 static const struct usb_descriptor_header *hs_printer_function [11] = {
307 (struct usb_descriptor_header *) &otg_desc,
308 (struct usb_descriptor_header *) &intf_desc,
309 (struct usb_descriptor_header *) &hs_ep_in_desc,
310 (struct usb_descriptor_header *) &hs_ep_out_desc,
311 NULL
314 /* maxpacket and other transfer characteristics vary by speed. */
315 #define ep_desc(g, hs, fs) (((g)->speed == USB_SPEED_HIGH)?(hs):(fs))
317 #else
319 /* if there's no high speed support, maxpacket doesn't change. */
320 #define ep_desc(g, hs, fs) (((void)(g)), (fs))
322 #endif /* !CONFIG_USB_GADGET_DUALSPEED */
324 /*-------------------------------------------------------------------------*/
326 /* descriptors that are built on-demand */
328 static char manufacturer [50];
329 static char product_desc [40] = DRIVER_DESC;
330 static char serial_num [40] = "1";
331 static char pnp_string [1024] =
332 "XXMFG:linux;MDL:g_printer;CLS:PRINTER;SN:1;";
334 /* static strings, in UTF-8 */
335 static struct usb_string strings [] = {
336 { STRING_MANUFACTURER, manufacturer, },
337 { STRING_PRODUCT, product_desc, },
338 { STRING_SERIALNUM, serial_num, },
339 { } /* end of list */
342 static struct usb_gadget_strings stringtab = {
343 .language = 0x0409, /* en-us */
344 .strings = strings,
347 /*-------------------------------------------------------------------------*/
349 static struct usb_request *
350 printer_req_alloc(struct usb_ep *ep, unsigned len, gfp_t gfp_flags)
352 struct usb_request *req;
354 req = usb_ep_alloc_request(ep, gfp_flags);
356 if (req != NULL) {
357 req->length = len;
358 req->buf = kmalloc(len, gfp_flags);
359 if (req->buf == NULL) {
360 usb_ep_free_request(ep, req);
361 return NULL;
365 return req;
368 static void
369 printer_req_free(struct usb_ep *ep, struct usb_request *req)
371 if (ep != NULL && req != NULL) {
372 kfree(req->buf);
373 usb_ep_free_request(ep, req);
377 /*-------------------------------------------------------------------------*/
379 static void rx_complete(struct usb_ep *ep, struct usb_request *req)
381 struct printer_dev *dev = ep->driver_data;
382 int status = req->status;
383 unsigned long flags;
385 spin_lock_irqsave(&dev->lock, flags);
387 list_del_init(&req->list); /* Remode from Active List */
389 switch (status) {
391 /* normal completion */
392 case 0:
393 list_add_tail(&req->list, &dev->rx_buffers);
394 wake_up_interruptible(&dev->rx_wait);
395 DBG(dev, "G_Printer : rx length %d\n", req->actual);
396 break;
398 /* software-driven interface shutdown */
399 case -ECONNRESET: /* unlink */
400 case -ESHUTDOWN: /* disconnect etc */
401 VDBG(dev, "rx shutdown, code %d\n", status);
402 list_add(&req->list, &dev->rx_reqs);
403 break;
405 /* for hardware automagic (such as pxa) */
406 case -ECONNABORTED: /* endpoint reset */
407 DBG(dev, "rx %s reset\n", ep->name);
408 list_add(&req->list, &dev->rx_reqs);
409 break;
411 /* data overrun */
412 case -EOVERFLOW:
413 /* FALLTHROUGH */
415 default:
416 DBG(dev, "rx status %d\n", status);
417 list_add(&req->list, &dev->rx_reqs);
418 break;
420 spin_unlock_irqrestore(&dev->lock, flags);
423 static void tx_complete(struct usb_ep *ep, struct usb_request *req)
425 struct printer_dev *dev = ep->driver_data;
427 switch (req->status) {
428 default:
429 VDBG(dev, "tx err %d\n", req->status);
430 /* FALLTHROUGH */
431 case -ECONNRESET: /* unlink */
432 case -ESHUTDOWN: /* disconnect etc */
433 break;
434 case 0:
435 break;
438 spin_lock(&dev->lock);
439 /* Take the request struct off the active list and put it on the
440 * free list.
442 list_del_init(&req->list);
443 list_add(&req->list, &dev->tx_reqs);
444 wake_up_interruptible(&dev->tx_wait);
445 if (likely(list_empty(&dev->tx_reqs_active)))
446 wake_up_interruptible(&dev->tx_flush_wait);
448 spin_unlock(&dev->lock);
451 /*-------------------------------------------------------------------------*/
453 static int
454 printer_open(struct inode *inode, struct file *fd)
456 struct printer_dev *dev;
457 unsigned long flags;
458 int ret = -EBUSY;
460 dev = container_of(inode->i_cdev, struct printer_dev, printer_cdev);
462 spin_lock_irqsave(&dev->lock, flags);
464 if (!dev->printer_cdev_open) {
465 dev->printer_cdev_open = 1;
466 fd->private_data = dev;
467 ret = 0;
468 /* Change the printer status to show that it's on-line. */
469 dev->printer_status |= PRINTER_SELECTED;
472 spin_unlock_irqrestore(&dev->lock, flags);
474 DBG(dev, "printer_open returned %x\n", ret);
476 return ret;
479 static int
480 printer_close(struct inode *inode, struct file *fd)
482 struct printer_dev *dev = fd->private_data;
483 unsigned long flags;
485 spin_lock_irqsave(&dev->lock, flags);
486 dev->printer_cdev_open = 0;
487 fd->private_data = NULL;
488 /* Change printer status to show that the printer is off-line. */
489 dev->printer_status &= ~PRINTER_SELECTED;
490 spin_unlock_irqrestore(&dev->lock, flags);
492 DBG(dev, "printer_close\n");
494 return 0;
497 static ssize_t
498 printer_read(struct file *fd, char __user *buf, size_t len, loff_t *ptr)
500 struct printer_dev *dev = fd->private_data;
501 unsigned long flags;
502 size_t size;
503 size_t bytes_copied;
504 struct usb_request *req;
505 /* This is a pointer to the current USB rx request. */
506 struct usb_request *current_rx_req;
507 /* This is the number of bytes in the current rx buffer. */
508 size_t current_rx_bytes;
509 /* This is a pointer to the current rx buffer. */
510 u8 *current_rx_buf;
512 if (len == 0)
513 return -EINVAL;
515 DBG(dev, "printer_read trying to read %d bytes\n", (int)len);
517 spin_lock(&dev->lock_printer_io);
518 spin_lock_irqsave(&dev->lock, flags);
520 /* We will use this flag later to check if a printer reset happened
521 * after we turn interrupts back on.
523 dev->reset_printer = 0;
525 while (likely(!list_empty(&dev->rx_reqs))) {
526 int error;
528 req = container_of(dev->rx_reqs.next,
529 struct usb_request, list);
530 list_del_init(&req->list);
532 /* The USB Host sends us whatever amount of data it wants to
533 * so we always set the length field to the full USB_BUFSIZE.
534 * If the amount of data is more than the read() caller asked
535 * for it will be stored in the request buffer until it is
536 * asked for by read().
538 req->length = USB_BUFSIZE;
539 req->complete = rx_complete;
541 error = usb_ep_queue(dev->out_ep, req, GFP_ATOMIC);
542 if (error) {
543 DBG(dev, "rx submit --> %d\n", error);
544 list_add(&req->list, &dev->rx_reqs);
545 break;
546 } else {
547 list_add(&req->list, &dev->rx_reqs_active);
551 bytes_copied = 0;
552 current_rx_req = dev->current_rx_req;
553 current_rx_bytes = dev->current_rx_bytes;
554 current_rx_buf = dev->current_rx_buf;
555 dev->current_rx_req = NULL;
556 dev->current_rx_bytes = 0;
557 dev->current_rx_buf = NULL;
559 /* Check if there is any data in the read buffers. Please note that
560 * current_rx_bytes is the number of bytes in the current rx buffer.
561 * If it is zero then check if there are any other rx_buffers that
562 * are on the completed list. We are only out of data if all rx
563 * buffers are empty.
565 if ((current_rx_bytes == 0) &&
566 (likely(list_empty(&dev->rx_buffers)))) {
567 /* Turn interrupts back on before sleeping. */
568 spin_unlock_irqrestore(&dev->lock, flags);
571 * If no data is available check if this is a NON-Blocking
572 * call or not.
574 if (fd->f_flags & (O_NONBLOCK|O_NDELAY)) {
575 spin_unlock(&dev->lock_printer_io);
576 return -EAGAIN;
579 /* Sleep until data is available */
580 wait_event_interruptible(dev->rx_wait,
581 (likely(!list_empty(&dev->rx_buffers))));
582 spin_lock_irqsave(&dev->lock, flags);
585 /* We have data to return then copy it to the caller's buffer.*/
586 while ((current_rx_bytes || likely(!list_empty(&dev->rx_buffers)))
587 && len) {
588 if (current_rx_bytes == 0) {
589 req = container_of(dev->rx_buffers.next,
590 struct usb_request, list);
591 list_del_init(&req->list);
593 if (req->actual && req->buf) {
594 current_rx_req = req;
595 current_rx_bytes = req->actual;
596 current_rx_buf = req->buf;
597 } else {
598 list_add(&req->list, &dev->rx_reqs);
599 continue;
603 /* Don't leave irqs off while doing memory copies */
604 spin_unlock_irqrestore(&dev->lock, flags);
606 if (len > current_rx_bytes)
607 size = current_rx_bytes;
608 else
609 size = len;
611 size -= copy_to_user(buf, current_rx_buf, size);
612 bytes_copied += size;
613 len -= size;
614 buf += size;
616 spin_lock_irqsave(&dev->lock, flags);
618 /* We've disconnected or reset free the req and buffer */
619 if (dev->reset_printer) {
620 printer_req_free(dev->out_ep, current_rx_req);
621 spin_unlock_irqrestore(&dev->lock, flags);
622 spin_unlock(&dev->lock_printer_io);
623 return -EAGAIN;
626 /* If we not returning all the data left in this RX request
627 * buffer then adjust the amount of data left in the buffer.
628 * Othewise if we are done with this RX request buffer then
629 * requeue it to get any incoming data from the USB host.
631 if (size < current_rx_bytes) {
632 current_rx_bytes -= size;
633 current_rx_buf += size;
634 } else {
635 list_add(&current_rx_req->list, &dev->rx_reqs);
636 current_rx_bytes = 0;
637 current_rx_buf = NULL;
638 current_rx_req = NULL;
642 dev->current_rx_req = current_rx_req;
643 dev->current_rx_bytes = current_rx_bytes;
644 dev->current_rx_buf = current_rx_buf;
646 spin_unlock_irqrestore(&dev->lock, flags);
647 spin_unlock(&dev->lock_printer_io);
649 DBG(dev, "printer_read returned %d bytes\n", (int)bytes_copied);
651 if (bytes_copied)
652 return bytes_copied;
653 else
654 return -EAGAIN;
657 static ssize_t
658 printer_write(struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
660 struct printer_dev *dev = fd->private_data;
661 unsigned long flags;
662 size_t size; /* Amount of data in a TX request. */
663 size_t bytes_copied = 0;
664 struct usb_request *req;
666 DBG(dev, "printer_write trying to send %d bytes\n", (int)len);
668 if (len == 0)
669 return -EINVAL;
671 spin_lock(&dev->lock_printer_io);
672 spin_lock_irqsave(&dev->lock, flags);
674 /* Check if a printer reset happens while we have interrupts on */
675 dev->reset_printer = 0;
677 /* Check if there is any available write buffers */
678 if (likely(list_empty(&dev->tx_reqs))) {
679 /* Turn interrupts back on before sleeping. */
680 spin_unlock_irqrestore(&dev->lock, flags);
683 * If write buffers are available check if this is
684 * a NON-Blocking call or not.
686 if (fd->f_flags & (O_NONBLOCK|O_NDELAY)) {
687 spin_unlock(&dev->lock_printer_io);
688 return -EAGAIN;
691 /* Sleep until a write buffer is available */
692 wait_event_interruptible(dev->tx_wait,
693 (likely(!list_empty(&dev->tx_reqs))));
694 spin_lock_irqsave(&dev->lock, flags);
697 while (likely(!list_empty(&dev->tx_reqs)) && len) {
699 if (len > USB_BUFSIZE)
700 size = USB_BUFSIZE;
701 else
702 size = len;
704 req = container_of(dev->tx_reqs.next, struct usb_request,
705 list);
706 list_del_init(&req->list);
708 req->complete = tx_complete;
709 req->length = size;
711 /* Check if we need to send a zero length packet. */
712 if (len > size)
713 /* They will be more TX requests so no yet. */
714 req->zero = 0;
715 else
716 /* If the data amount is not a multple of the
717 * maxpacket size then send a zero length packet.
719 req->zero = ((len % dev->in_ep->maxpacket) == 0);
721 /* Don't leave irqs off while doing memory copies */
722 spin_unlock_irqrestore(&dev->lock, flags);
724 if (copy_from_user(req->buf, buf, size)) {
725 list_add(&req->list, &dev->tx_reqs);
726 spin_unlock(&dev->lock_printer_io);
727 return bytes_copied;
730 bytes_copied += size;
731 len -= size;
732 buf += size;
734 spin_lock_irqsave(&dev->lock, flags);
736 /* We've disconnected or reset so free the req and buffer */
737 if (dev->reset_printer) {
738 printer_req_free(dev->in_ep, req);
739 spin_unlock_irqrestore(&dev->lock, flags);
740 spin_unlock(&dev->lock_printer_io);
741 return -EAGAIN;
744 if (usb_ep_queue(dev->in_ep, req, GFP_ATOMIC)) {
745 list_add(&req->list, &dev->tx_reqs);
746 spin_unlock_irqrestore(&dev->lock, flags);
747 spin_unlock(&dev->lock_printer_io);
748 return -EAGAIN;
751 list_add(&req->list, &dev->tx_reqs_active);
755 spin_unlock_irqrestore(&dev->lock, flags);
756 spin_unlock(&dev->lock_printer_io);
758 DBG(dev, "printer_write sent %d bytes\n", (int)bytes_copied);
760 if (bytes_copied) {
761 return bytes_copied;
762 } else {
763 return -EAGAIN;
767 static int
768 printer_fsync(struct file *fd, struct dentry *dentry, int datasync)
770 struct printer_dev *dev = fd->private_data;
771 unsigned long flags;
772 int tx_list_empty;
774 spin_lock_irqsave(&dev->lock, flags);
775 tx_list_empty = (likely(list_empty(&dev->tx_reqs)));
776 spin_unlock_irqrestore(&dev->lock, flags);
778 if (!tx_list_empty) {
779 /* Sleep until all data has been sent */
780 wait_event_interruptible(dev->tx_flush_wait,
781 (likely(list_empty(&dev->tx_reqs_active))));
784 return 0;
787 static unsigned int
788 printer_poll(struct file *fd, poll_table *wait)
790 struct printer_dev *dev = fd->private_data;
791 unsigned long flags;
792 int status = 0;
794 poll_wait(fd, &dev->rx_wait, wait);
795 poll_wait(fd, &dev->tx_wait, wait);
797 spin_lock_irqsave(&dev->lock, flags);
798 if (likely(!list_empty(&dev->tx_reqs)))
799 status |= POLLOUT | POLLWRNORM;
801 if (likely(!list_empty(&dev->rx_buffers)))
802 status |= POLLIN | POLLRDNORM;
804 spin_unlock_irqrestore(&dev->lock, flags);
806 return status;
809 static int
810 printer_ioctl(struct inode *inode, struct file *fd, unsigned int code,
811 unsigned long arg)
813 struct printer_dev *dev = fd->private_data;
814 unsigned long flags;
815 int status = 0;
817 DBG(dev, "printer_ioctl: cmd=0x%4.4x, arg=%lu\n", code, arg);
819 /* handle ioctls */
821 spin_lock_irqsave(&dev->lock, flags);
823 switch (code) {
824 case GADGET_GET_PRINTER_STATUS:
825 status = (int)dev->printer_status;
826 break;
827 case GADGET_SET_PRINTER_STATUS:
828 dev->printer_status = (u8)arg;
829 break;
830 default:
831 /* could not handle ioctl */
832 DBG(dev, "printer_ioctl: ERROR cmd=0x%4.4xis not supported\n",
833 code);
834 status = -ENOTTY;
837 spin_unlock_irqrestore(&dev->lock, flags);
839 return status;
842 /* used after endpoint configuration */
843 static struct file_operations printer_io_operations = {
844 .owner = THIS_MODULE,
845 .open = printer_open,
846 .read = printer_read,
847 .write = printer_write,
848 .fsync = printer_fsync,
849 .poll = printer_poll,
850 .ioctl = printer_ioctl,
851 .release = printer_close
854 /*-------------------------------------------------------------------------*/
856 static int
857 set_printer_interface(struct printer_dev *dev)
859 int result = 0;
861 dev->in = ep_desc(dev->gadget, &hs_ep_in_desc, &fs_ep_in_desc);
862 dev->in_ep->driver_data = dev;
864 dev->out = ep_desc(dev->gadget, &hs_ep_out_desc, &fs_ep_out_desc);
865 dev->out_ep->driver_data = dev;
867 result = usb_ep_enable(dev->in_ep, dev->in);
868 if (result != 0) {
869 DBG(dev, "enable %s --> %d\n", dev->in_ep->name, result);
870 goto done;
873 result = usb_ep_enable(dev->out_ep, dev->out);
874 if (result != 0) {
875 DBG(dev, "enable %s --> %d\n", dev->in_ep->name, result);
876 goto done;
879 done:
880 /* on error, disable any endpoints */
881 if (result != 0) {
882 (void) usb_ep_disable(dev->in_ep);
883 (void) usb_ep_disable(dev->out_ep);
884 dev->in = NULL;
885 dev->out = NULL;
888 /* caller is responsible for cleanup on error */
889 return result;
892 static void printer_reset_interface(struct printer_dev *dev)
894 if (dev->interface < 0)
895 return;
897 DBG(dev, "%s\n", __FUNCTION__);
899 if (dev->in)
900 usb_ep_disable(dev->in_ep);
902 if (dev->out)
903 usb_ep_disable(dev->out_ep);
905 dev->interface = -1;
908 /* change our operational config. must agree with the code
909 * that returns config descriptors, and altsetting code.
911 static int
912 printer_set_config(struct printer_dev *dev, unsigned number)
914 int result = 0;
915 struct usb_gadget *gadget = dev->gadget;
917 if (gadget_is_sa1100(gadget) && dev->config) {
918 /* tx fifo is full, but we can't clear it...*/
919 INFO(dev, "can't change configurations\n");
920 return -ESPIPE;
923 switch (number) {
924 case DEV_CONFIG_VALUE:
925 result = 0;
926 break;
927 default:
928 result = -EINVAL;
929 /* FALL THROUGH */
930 case 0:
931 break;
934 if (result) {
935 usb_gadget_vbus_draw(dev->gadget,
936 dev->gadget->is_otg ? 8 : 100);
937 } else {
938 char *speed;
939 unsigned power;
941 power = 2 * config_desc.bMaxPower;
942 usb_gadget_vbus_draw(dev->gadget, power);
944 switch (gadget->speed) {
945 case USB_SPEED_FULL: speed = "full"; break;
946 #ifdef CONFIG_USB_GADGET_DUALSPEED
947 case USB_SPEED_HIGH: speed = "high"; break;
948 #endif
949 default: speed = "?"; break;
952 dev->config = number;
953 INFO(dev, "%s speed config #%d: %d mA, %s\n",
954 speed, number, power, driver_desc);
956 return result;
959 static int
960 config_buf(enum usb_device_speed speed, u8 *buf, u8 type, unsigned index,
961 int is_otg)
963 int len;
964 const struct usb_descriptor_header **function;
965 #ifdef CONFIG_USB_GADGET_DUALSPEED
966 int hs = (speed == USB_SPEED_HIGH);
968 if (type == USB_DT_OTHER_SPEED_CONFIG)
969 hs = !hs;
971 if (hs) {
972 function = hs_printer_function;
973 } else {
974 function = fs_printer_function;
976 #else
977 function = fs_printer_function;
978 #endif
980 if (index >= device_desc.bNumConfigurations)
981 return -EINVAL;
983 /* for now, don't advertise srp-only devices */
984 if (!is_otg)
985 function++;
987 len = usb_gadget_config_buf(&config_desc, buf, USB_DESC_BUFSIZE,
988 function);
989 if (len < 0)
990 return len;
991 ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
992 return len;
995 /* Change our operational Interface. */
996 static int
997 set_interface(struct printer_dev *dev, unsigned number)
999 int result = 0;
1001 if (gadget_is_sa1100(dev->gadget) && dev->interface < 0) {
1002 /* tx fifo is full, but we can't clear it...*/
1003 INFO(dev, "can't change interfaces\n");
1004 return -ESPIPE;
1007 /* Free the current interface */
1008 switch (dev->interface) {
1009 case PRINTER_INTERFACE:
1010 printer_reset_interface(dev);
1011 break;
1014 switch (number) {
1015 case PRINTER_INTERFACE:
1016 result = set_printer_interface(dev);
1017 if (result) {
1018 printer_reset_interface(dev);
1019 } else {
1020 dev->interface = PRINTER_INTERFACE;
1022 break;
1023 default:
1024 result = -EINVAL;
1025 /* FALL THROUGH */
1028 if (!result)
1029 INFO(dev, "Using interface %x\n", number);
1031 return result;
1034 static void printer_setup_complete(struct usb_ep *ep, struct usb_request *req)
1036 if (req->status || req->actual != req->length)
1037 DBG((struct printer_dev *) ep->driver_data,
1038 "setup complete --> %d, %d/%d\n",
1039 req->status, req->actual, req->length);
1042 static void printer_soft_reset(struct printer_dev *dev)
1044 struct usb_request *req;
1046 INFO(dev, "Received Printer Reset Request\n");
1048 if (usb_ep_disable(dev->in_ep))
1049 DBG(dev, "Failed to disable USB in_ep\n");
1050 if (usb_ep_disable(dev->out_ep))
1051 DBG(dev, "Failed to disable USB out_ep\n");
1053 if (dev->current_rx_req != NULL) {
1054 list_add(&dev->current_rx_req->list, &dev->rx_reqs);
1055 dev->current_rx_req = NULL;
1057 dev->current_rx_bytes = 0;
1058 dev->current_rx_buf = NULL;
1059 dev->reset_printer = 1;
1061 while (likely(!(list_empty(&dev->rx_buffers)))) {
1062 req = container_of(dev->rx_buffers.next, struct usb_request,
1063 list);
1064 list_del_init(&req->list);
1065 list_add(&req->list, &dev->rx_reqs);
1068 while (likely(!(list_empty(&dev->rx_reqs_active)))) {
1069 req = container_of(dev->rx_buffers.next, struct usb_request,
1070 list);
1071 list_del_init(&req->list);
1072 list_add(&req->list, &dev->rx_reqs);
1075 while (likely(!(list_empty(&dev->tx_reqs_active)))) {
1076 req = container_of(dev->tx_reqs_active.next,
1077 struct usb_request, list);
1078 list_del_init(&req->list);
1079 list_add(&req->list, &dev->tx_reqs);
1082 if (usb_ep_enable(dev->in_ep, dev->in))
1083 DBG(dev, "Failed to enable USB in_ep\n");
1084 if (usb_ep_enable(dev->out_ep, dev->out))
1085 DBG(dev, "Failed to enable USB out_ep\n");
1087 wake_up_interruptible(&dev->tx_wait);
1088 wake_up_interruptible(&dev->tx_flush_wait);
1091 /*-------------------------------------------------------------------------*/
1094 * The setup() callback implements all the ep0 functionality that's not
1095 * handled lower down.
1097 static int
1098 printer_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1100 struct printer_dev *dev = get_gadget_data(gadget);
1101 struct usb_request *req = dev->req;
1102 int value = -EOPNOTSUPP;
1103 u16 wIndex = le16_to_cpu(ctrl->wIndex);
1104 u16 wValue = le16_to_cpu(ctrl->wValue);
1105 u16 wLength = le16_to_cpu(ctrl->wLength);
1107 DBG(dev, "ctrl req%02x.%02x v%04x i%04x l%d\n",
1108 ctrl->bRequestType, ctrl->bRequest, wValue, wIndex, wLength);
1110 req->complete = printer_setup_complete;
1112 switch (ctrl->bRequestType&USB_TYPE_MASK) {
1114 case USB_TYPE_STANDARD:
1115 switch (ctrl->bRequest) {
1117 case USB_REQ_GET_DESCRIPTOR:
1118 if (ctrl->bRequestType != USB_DIR_IN)
1119 break;
1120 switch (wValue >> 8) {
1122 case USB_DT_DEVICE:
1123 value = min(wLength, (u16) sizeof device_desc);
1124 memcpy(req->buf, &device_desc, value);
1125 break;
1126 #ifdef CONFIG_USB_GADGET_DUALSPEED
1127 case USB_DT_DEVICE_QUALIFIER:
1128 if (!gadget->is_dualspeed)
1129 break;
1130 value = min(wLength,
1131 (u16) sizeof dev_qualifier);
1132 memcpy(req->buf, &dev_qualifier, value);
1133 break;
1135 case USB_DT_OTHER_SPEED_CONFIG:
1136 if (!gadget->is_dualspeed)
1137 break;
1138 /* FALLTHROUGH */
1139 #endif /* CONFIG_USB_GADGET_DUALSPEED */
1140 case USB_DT_CONFIG:
1141 value = config_buf(gadget->speed, req->buf,
1142 wValue >> 8,
1143 wValue & 0xff,
1144 gadget->is_otg);
1145 if (value >= 0)
1146 value = min(wLength, (u16) value);
1147 break;
1149 case USB_DT_STRING:
1150 value = usb_gadget_get_string(&stringtab,
1151 wValue & 0xff, req->buf);
1152 if (value >= 0)
1153 value = min(wLength, (u16) value);
1154 break;
1156 break;
1158 case USB_REQ_SET_CONFIGURATION:
1159 if (ctrl->bRequestType != 0)
1160 break;
1161 if (gadget->a_hnp_support)
1162 DBG(dev, "HNP available\n");
1163 else if (gadget->a_alt_hnp_support)
1164 DBG(dev, "HNP needs a different root port\n");
1165 value = printer_set_config(dev, wValue);
1166 break;
1167 case USB_REQ_GET_CONFIGURATION:
1168 if (ctrl->bRequestType != USB_DIR_IN)
1169 break;
1170 *(u8 *)req->buf = dev->config;
1171 value = min(wLength, (u16) 1);
1172 break;
1174 case USB_REQ_SET_INTERFACE:
1175 if (ctrl->bRequestType != USB_RECIP_INTERFACE ||
1176 !dev->config)
1177 break;
1179 value = set_interface(dev, PRINTER_INTERFACE);
1180 break;
1181 case USB_REQ_GET_INTERFACE:
1182 if (ctrl->bRequestType !=
1183 (USB_DIR_IN|USB_RECIP_INTERFACE)
1184 || !dev->config)
1185 break;
1187 *(u8 *)req->buf = dev->interface;
1188 value = min(wLength, (u16) 1);
1189 break;
1191 default:
1192 goto unknown;
1194 break;
1196 case USB_TYPE_CLASS:
1197 switch (ctrl->bRequest) {
1198 case 0: /* Get the IEEE-1284 PNP String */
1199 /* Only one printer interface is supported. */
1200 if ((wIndex>>8) != PRINTER_INTERFACE)
1201 break;
1203 value = (pnp_string[0]<<8)|pnp_string[1];
1204 memcpy(req->buf, pnp_string, value);
1205 DBG(dev, "1284 PNP String: %x %s\n", value,
1206 &pnp_string[2]);
1207 break;
1209 case 1: /* Get Port Status */
1210 /* Only one printer interface is supported. */
1211 if (wIndex != PRINTER_INTERFACE)
1212 break;
1214 *(u8 *)req->buf = dev->printer_status;
1215 value = min(wLength, (u16) 1);
1216 break;
1218 case 2: /* Soft Reset */
1219 /* Only one printer interface is supported. */
1220 if (wIndex != PRINTER_INTERFACE)
1221 break;
1223 printer_soft_reset(dev);
1225 value = 0;
1226 break;
1228 default:
1229 goto unknown;
1231 break;
1233 default:
1234 unknown:
1235 VDBG(dev,
1236 "unknown ctrl req%02x.%02x v%04x i%04x l%d\n",
1237 ctrl->bRequestType, ctrl->bRequest,
1238 wValue, wIndex, wLength);
1239 break;
1242 /* respond with data transfer before status phase? */
1243 if (value >= 0) {
1244 req->length = value;
1245 req->zero = value < wLength
1246 && (value % gadget->ep0->maxpacket) == 0;
1247 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
1248 if (value < 0) {
1249 DBG(dev, "ep_queue --> %d\n", value);
1250 req->status = 0;
1251 printer_setup_complete(gadget->ep0, req);
1255 /* host either stalls (value < 0) or reports success */
1256 return value;
1259 static void
1260 printer_disconnect(struct usb_gadget *gadget)
1262 struct printer_dev *dev = get_gadget_data(gadget);
1263 unsigned long flags;
1265 DBG(dev, "%s\n", __FUNCTION__);
1267 spin_lock_irqsave(&dev->lock, flags);
1269 printer_reset_interface(dev);
1271 spin_unlock_irqrestore(&dev->lock, flags);
1274 static void
1275 printer_unbind(struct usb_gadget *gadget)
1277 struct printer_dev *dev = get_gadget_data(gadget);
1278 struct usb_request *req;
1281 DBG(dev, "%s\n", __FUNCTION__);
1283 /* Remove sysfs files */
1284 device_destroy(usb_gadget_class, g_printer_devno);
1286 /* Remove Character Device */
1287 cdev_del(&dev->printer_cdev);
1289 /* we must already have been disconnected ... no i/o may be active */
1290 WARN_ON(!list_empty(&dev->tx_reqs_active));
1291 WARN_ON(!list_empty(&dev->rx_reqs_active));
1293 /* Free all memory for this driver. */
1294 while (!list_empty(&dev->tx_reqs)) {
1295 req = container_of(dev->tx_reqs.next, struct usb_request,
1296 list);
1297 list_del(&req->list);
1298 printer_req_free(dev->in_ep, req);
1301 if (dev->current_rx_req != NULL)
1302 printer_req_free(dev->out_ep, dev->current_rx_req);
1304 while (!list_empty(&dev->rx_reqs)) {
1305 req = container_of(dev->rx_reqs.next,
1306 struct usb_request, list);
1307 list_del(&req->list);
1308 printer_req_free(dev->out_ep, req);
1311 while (!list_empty(&dev->rx_buffers)) {
1312 req = container_of(dev->rx_buffers.next,
1313 struct usb_request, list);
1314 list_del(&req->list);
1315 printer_req_free(dev->out_ep, req);
1318 if (dev->req) {
1319 printer_req_free(gadget->ep0, dev->req);
1320 dev->req = NULL;
1323 set_gadget_data(gadget, NULL);
1326 static int __init
1327 printer_bind(struct usb_gadget *gadget)
1329 struct printer_dev *dev;
1330 struct usb_ep *in_ep, *out_ep;
1331 int status = -ENOMEM;
1332 int gcnum;
1333 size_t len;
1334 u32 i;
1335 struct usb_request *req;
1337 dev = &usb_printer_gadget;
1340 /* Setup the sysfs files for the printer gadget. */
1341 dev->pdev = device_create(usb_gadget_class, NULL, g_printer_devno,
1342 "g_printer");
1343 if (IS_ERR(dev->pdev)) {
1344 ERROR(dev, "Failed to create device: g_printer\n");
1345 goto fail;
1349 * Register a character device as an interface to a user mode
1350 * program that handles the printer specific functionality.
1352 cdev_init(&dev->printer_cdev, &printer_io_operations);
1353 dev->printer_cdev.owner = THIS_MODULE;
1354 status = cdev_add(&dev->printer_cdev, g_printer_devno, 1);
1355 if (status) {
1356 ERROR(dev, "Failed to open char device\n");
1357 goto fail;
1360 if (gadget_is_sa1100(gadget)) {
1361 /* hardware can't write zero length packets. */
1362 ERROR(dev, "SA1100 controller is unsupport by this driver\n");
1363 goto fail;
1366 gcnum = usb_gadget_controller_number(gadget);
1367 if (gcnum >= 0) {
1368 device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
1369 } else {
1370 dev_warn(&gadget->dev, "controller '%s' not recognized\n",
1371 gadget->name);
1372 /* unrecognized, but safe unless bulk is REALLY quirky */
1373 device_desc.bcdDevice =
1374 __constant_cpu_to_le16(0xFFFF);
1376 snprintf(manufacturer, sizeof(manufacturer), "%s %s with %s",
1377 init_utsname()->sysname, init_utsname()->release,
1378 gadget->name);
1380 device_desc.idVendor =
1381 __constant_cpu_to_le16(PRINTER_VENDOR_NUM);
1382 device_desc.idProduct =
1383 __constant_cpu_to_le16(PRINTER_PRODUCT_NUM);
1385 /* support optional vendor/distro customization */
1386 if (idVendor) {
1387 if (!idProduct) {
1388 dev_err(&gadget->dev, "idVendor needs idProduct!\n");
1389 return -ENODEV;
1391 device_desc.idVendor = cpu_to_le16(idVendor);
1392 device_desc.idProduct = cpu_to_le16(idProduct);
1393 if (bcdDevice)
1394 device_desc.bcdDevice = cpu_to_le16(bcdDevice);
1397 if (iManufacturer)
1398 strlcpy(manufacturer, iManufacturer, sizeof manufacturer);
1400 if (iProduct)
1401 strlcpy(product_desc, iProduct, sizeof product_desc);
1403 if (iSerialNum)
1404 strlcpy(serial_num, iSerialNum, sizeof serial_num);
1406 if (iPNPstring)
1407 strlcpy(&pnp_string[2], iPNPstring, (sizeof pnp_string)-2);
1409 len = strlen(pnp_string);
1410 pnp_string[0] = (len >> 8) & 0xFF;
1411 pnp_string[1] = len & 0xFF;
1413 /* all we really need is bulk IN/OUT */
1414 usb_ep_autoconfig_reset(gadget);
1415 in_ep = usb_ep_autoconfig(gadget, &fs_ep_in_desc);
1416 if (!in_ep) {
1417 autoconf_fail:
1418 dev_err(&gadget->dev, "can't autoconfigure on %s\n",
1419 gadget->name);
1420 return -ENODEV;
1422 in_ep->driver_data = in_ep; /* claim */
1424 out_ep = usb_ep_autoconfig(gadget, &fs_ep_out_desc);
1425 if (!out_ep)
1426 goto autoconf_fail;
1427 out_ep->driver_data = out_ep; /* claim */
1429 #ifdef CONFIG_USB_GADGET_DUALSPEED
1430 /* assumes ep0 uses the same value for both speeds ... */
1431 dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
1433 /* and that all endpoints are dual-speed */
1434 hs_ep_in_desc.bEndpointAddress = fs_ep_in_desc.bEndpointAddress;
1435 hs_ep_out_desc.bEndpointAddress = fs_ep_out_desc.bEndpointAddress;
1436 #endif /* DUALSPEED */
1438 device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
1439 usb_gadget_set_selfpowered(gadget);
1441 if (gadget->is_otg) {
1442 otg_desc.bmAttributes |= USB_OTG_HNP,
1443 config_desc.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1444 config_desc.bMaxPower = 4;
1447 spin_lock_init(&dev->lock);
1448 spin_lock_init(&dev->lock_printer_io);
1449 INIT_LIST_HEAD(&dev->tx_reqs);
1450 INIT_LIST_HEAD(&dev->tx_reqs_active);
1451 INIT_LIST_HEAD(&dev->rx_reqs);
1452 INIT_LIST_HEAD(&dev->rx_reqs_active);
1453 INIT_LIST_HEAD(&dev->rx_buffers);
1454 init_waitqueue_head(&dev->rx_wait);
1455 init_waitqueue_head(&dev->tx_wait);
1456 init_waitqueue_head(&dev->tx_flush_wait);
1458 dev->config = 0;
1459 dev->interface = -1;
1460 dev->printer_cdev_open = 0;
1461 dev->printer_status = PRINTER_NOT_ERROR;
1462 dev->current_rx_req = NULL;
1463 dev->current_rx_bytes = 0;
1464 dev->current_rx_buf = NULL;
1466 dev->in_ep = in_ep;
1467 dev->out_ep = out_ep;
1469 /* preallocate control message data and buffer */
1470 dev->req = printer_req_alloc(gadget->ep0, USB_DESC_BUFSIZE,
1471 GFP_KERNEL);
1472 if (!dev->req) {
1473 status = -ENOMEM;
1474 goto fail;
1477 for (i = 0; i < QLEN; i++) {
1478 req = printer_req_alloc(dev->in_ep, USB_BUFSIZE, GFP_KERNEL);
1479 if (!req) {
1480 while (!list_empty(&dev->tx_reqs)) {
1481 req = container_of(dev->tx_reqs.next,
1482 struct usb_request, list);
1483 list_del(&req->list);
1484 printer_req_free(dev->in_ep, req);
1486 return -ENOMEM;
1488 list_add(&req->list, &dev->tx_reqs);
1491 for (i = 0; i < QLEN; i++) {
1492 req = printer_req_alloc(dev->out_ep, USB_BUFSIZE, GFP_KERNEL);
1493 if (!req) {
1494 while (!list_empty(&dev->rx_reqs)) {
1495 req = container_of(dev->rx_reqs.next,
1496 struct usb_request, list);
1497 list_del(&req->list);
1498 printer_req_free(dev->out_ep, req);
1500 return -ENOMEM;
1502 list_add(&req->list, &dev->rx_reqs);
1505 dev->req->complete = printer_setup_complete;
1507 /* finish hookup to lower layer ... */
1508 dev->gadget = gadget;
1509 set_gadget_data(gadget, dev);
1510 gadget->ep0->driver_data = dev;
1512 INFO(dev, "%s, version: " DRIVER_VERSION "\n", driver_desc);
1513 INFO(dev, "using %s, OUT %s IN %s\n", gadget->name, out_ep->name,
1514 in_ep->name);
1516 return 0;
1518 fail:
1519 printer_unbind(gadget);
1520 return status;
1523 /*-------------------------------------------------------------------------*/
1525 static struct usb_gadget_driver printer_driver = {
1526 .speed = DEVSPEED,
1528 .function = (char *) driver_desc,
1529 .bind = printer_bind,
1530 .unbind = printer_unbind,
1532 .setup = printer_setup,
1533 .disconnect = printer_disconnect,
1535 .driver = {
1536 .name = (char *) shortname,
1537 .owner = THIS_MODULE,
1541 MODULE_DESCRIPTION(DRIVER_DESC);
1542 MODULE_AUTHOR("Craig Nadler");
1543 MODULE_LICENSE("GPL");
1545 static int __init
1546 init(void)
1548 int status;
1550 usb_gadget_class = class_create(THIS_MODULE, "usb_printer_gadget");
1551 if (IS_ERR(usb_gadget_class)) {
1552 status = PTR_ERR(usb_gadget_class);
1553 ERROR(dev, "unable to create usb_gadget class %d\n", status);
1554 return status;
1557 status = alloc_chrdev_region(&g_printer_devno, 0, 1,
1558 "USB printer gadget");
1559 if (status) {
1560 ERROR(dev, "alloc_chrdev_region %d\n", status);
1561 class_destroy(usb_gadget_class);
1562 return status;
1565 status = usb_gadget_register_driver(&printer_driver);
1566 if (status) {
1567 class_destroy(usb_gadget_class);
1568 unregister_chrdev_region(g_printer_devno, 1);
1569 DBG(dev, "usb_gadget_register_driver %x\n", status);
1572 return status;
1574 module_init(init);
1576 static void __exit
1577 cleanup(void)
1579 int status;
1581 spin_lock(&usb_printer_gadget.lock_printer_io);
1582 class_destroy(usb_gadget_class);
1583 unregister_chrdev_region(g_printer_devno, 2);
1585 status = usb_gadget_unregister_driver(&printer_driver);
1586 if (status)
1587 ERROR(dev, "usb_gadget_unregister_driver %x\n", status);
1589 spin_unlock(&usb_printer_gadget.lock_printer_io);
1591 module_exit(cleanup);