Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / drivers / usb / gadget / printer.c
blob376cc9b1e3f5243893b9c96f42ea77db981a5d36
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 <<<<<<< HEAD:drivers/usb/gadget/printer.c
96 struct class_device *printer_class_dev;
97 =======
98 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/usb/gadget/printer.c
99 struct cdev printer_cdev;
100 struct device *pdev;
101 u8 printer_cdev_open;
102 wait_queue_head_t wait;
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 identifers 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 ushort __initdata idVendor;
124 module_param(idVendor, ushort, S_IRUGO);
125 MODULE_PARM_DESC(idVendor, "USB Vendor ID");
127 static ushort __initdata idProduct;
128 module_param(idProduct, ushort, S_IRUGO);
129 MODULE_PARM_DESC(idProduct, "USB Product ID");
131 static ushort __initdata bcdDevice;
132 module_param(bcdDevice, ushort, S_IRUGO);
133 MODULE_PARM_DESC(bcdDevice, "USB Device version (BCD)");
135 static char *__initdata iManufacturer;
136 module_param(iManufacturer, charp, S_IRUGO);
137 MODULE_PARM_DESC(iManufacturer, "USB Manufacturer string");
139 static char *__initdata iProduct;
140 module_param(iProduct, charp, S_IRUGO);
141 MODULE_PARM_DESC(iProduct, "USB Product string");
143 static char *__initdata iSerialNum;
144 module_param(iSerialNum, charp, S_IRUGO);
145 MODULE_PARM_DESC(iSerialNum, "1");
147 static char *__initdata iPNPstring;
148 module_param(iPNPstring, charp, S_IRUGO);
149 MODULE_PARM_DESC(iPNPstring, "MFG:linux;MDL:g_printer;CLS:PRINTER;SN:1;");
151 /* Number of requests to allocate per endpoint, not used for ep0. */
152 static unsigned qlen = 10;
153 module_param(qlen, uint, S_IRUGO|S_IWUSR);
155 #define QLEN qlen
157 #ifdef CONFIG_USB_GADGET_DUALSPEED
158 #define DEVSPEED USB_SPEED_HIGH
159 #else /* full speed (low speed doesn't do bulk) */
160 #define DEVSPEED USB_SPEED_FULL
161 #endif
163 /*-------------------------------------------------------------------------*/
165 #define xprintk(d, level, fmt, args...) \
166 printk(level "%s: " fmt, DRIVER_DESC, ## args)
168 #ifdef DEBUG
169 #define DBG(dev, fmt, args...) \
170 xprintk(dev, KERN_DEBUG, fmt, ## args)
171 #else
172 #define DBG(dev, fmt, args...) \
173 do { } while (0)
174 #endif /* DEBUG */
176 #ifdef VERBOSE
177 #define VDBG(dev, fmt, args...) \
178 xprintk(dev, KERN_DEBUG, fmt, ## args)
179 #else
180 #define VDBG(dev, fmt, args...) \
181 do { } while (0)
182 #endif /* VERBOSE */
184 #define ERROR(dev, fmt, args...) \
185 xprintk(dev, KERN_ERR, fmt, ## args)
186 #define WARN(dev, fmt, args...) \
187 xprintk(dev, KERN_WARNING, fmt, ## args)
188 #define INFO(dev, fmt, args...) \
189 xprintk(dev, KERN_INFO, fmt, ## args)
191 /*-------------------------------------------------------------------------*/
193 /* USB DRIVER HOOKUP (to the hardware driver, below us), mostly
194 * ep0 implementation: descriptors, config management, setup().
195 * also optional class-specific notification interrupt transfer.
199 * DESCRIPTORS ... most are static, but strings and (full) configuration
200 * descriptors are built on demand.
203 #define STRING_MANUFACTURER 1
204 #define STRING_PRODUCT 2
205 #define STRING_SERIALNUM 3
207 /* holds our biggest descriptor */
208 #define USB_DESC_BUFSIZE 256
209 #define USB_BUFSIZE 8192
211 /* This device advertises one configuration. */
212 #define DEV_CONFIG_VALUE 1
213 #define PRINTER_INTERFACE 0
215 static struct usb_device_descriptor device_desc = {
216 .bLength = sizeof device_desc,
217 .bDescriptorType = USB_DT_DEVICE,
218 .bcdUSB = __constant_cpu_to_le16(0x0200),
219 .bDeviceClass = USB_CLASS_PER_INTERFACE,
220 .bDeviceSubClass = 0,
221 .bDeviceProtocol = 0,
222 .idVendor = __constant_cpu_to_le16(PRINTER_VENDOR_NUM),
223 .idProduct = __constant_cpu_to_le16(PRINTER_PRODUCT_NUM),
224 .iManufacturer = STRING_MANUFACTURER,
225 .iProduct = STRING_PRODUCT,
226 .iSerialNumber = STRING_SERIALNUM,
227 .bNumConfigurations = 1
230 static struct usb_otg_descriptor otg_desc = {
231 .bLength = sizeof otg_desc,
232 .bDescriptorType = USB_DT_OTG,
233 .bmAttributes = USB_OTG_SRP
236 static struct usb_config_descriptor config_desc = {
237 .bLength = sizeof config_desc,
238 .bDescriptorType = USB_DT_CONFIG,
240 /* compute wTotalLength on the fly */
241 .bNumInterfaces = 1,
242 .bConfigurationValue = DEV_CONFIG_VALUE,
243 .iConfiguration = 0,
244 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
245 .bMaxPower = 1 /* Self-Powered */
248 static struct usb_interface_descriptor intf_desc = {
249 .bLength = sizeof intf_desc,
250 .bDescriptorType = USB_DT_INTERFACE,
251 .bInterfaceNumber = PRINTER_INTERFACE,
252 .bNumEndpoints = 2,
253 .bInterfaceClass = USB_CLASS_PRINTER,
254 .bInterfaceSubClass = 1, /* Printer Sub-Class */
255 .bInterfaceProtocol = 2, /* Bi-Directional */
256 .iInterface = 0
259 static struct usb_endpoint_descriptor fs_ep_in_desc = {
260 .bLength = USB_DT_ENDPOINT_SIZE,
261 .bDescriptorType = USB_DT_ENDPOINT,
262 .bEndpointAddress = USB_DIR_IN,
263 .bmAttributes = USB_ENDPOINT_XFER_BULK
266 static struct usb_endpoint_descriptor fs_ep_out_desc = {
267 .bLength = USB_DT_ENDPOINT_SIZE,
268 .bDescriptorType = USB_DT_ENDPOINT,
269 .bEndpointAddress = USB_DIR_OUT,
270 .bmAttributes = USB_ENDPOINT_XFER_BULK
273 static const struct usb_descriptor_header *fs_printer_function [11] = {
274 (struct usb_descriptor_header *) &otg_desc,
275 (struct usb_descriptor_header *) &intf_desc,
276 (struct usb_descriptor_header *) &fs_ep_in_desc,
277 (struct usb_descriptor_header *) &fs_ep_out_desc,
278 NULL
281 #ifdef CONFIG_USB_GADGET_DUALSPEED
284 * usb 2.0 devices need to expose both high speed and full speed
285 * descriptors, unless they only run at full speed.
288 static struct usb_endpoint_descriptor hs_ep_in_desc = {
289 .bLength = USB_DT_ENDPOINT_SIZE,
290 .bDescriptorType = USB_DT_ENDPOINT,
291 .bmAttributes = USB_ENDPOINT_XFER_BULK,
292 .wMaxPacketSize = __constant_cpu_to_le16(512)
295 static struct usb_endpoint_descriptor hs_ep_out_desc = {
296 .bLength = USB_DT_ENDPOINT_SIZE,
297 .bDescriptorType = USB_DT_ENDPOINT,
298 .bmAttributes = USB_ENDPOINT_XFER_BULK,
299 .wMaxPacketSize = __constant_cpu_to_le16(512)
302 static struct usb_qualifier_descriptor dev_qualifier = {
303 .bLength = sizeof dev_qualifier,
304 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
305 .bcdUSB = __constant_cpu_to_le16(0x0200),
306 .bDeviceClass = USB_CLASS_PRINTER,
307 .bNumConfigurations = 1
310 static const struct usb_descriptor_header *hs_printer_function [11] = {
311 (struct usb_descriptor_header *) &otg_desc,
312 (struct usb_descriptor_header *) &intf_desc,
313 (struct usb_descriptor_header *) &hs_ep_in_desc,
314 (struct usb_descriptor_header *) &hs_ep_out_desc,
315 NULL
318 /* maxpacket and other transfer characteristics vary by speed. */
319 #define ep_desc(g, hs, fs) (((g)->speed == USB_SPEED_HIGH)?(hs):(fs))
321 #else
323 /* if there's no high speed support, maxpacket doesn't change. */
324 #define ep_desc(g, hs, fs) (((void)(g)), (fs))
326 #endif /* !CONFIG_USB_GADGET_DUALSPEED */
328 /*-------------------------------------------------------------------------*/
330 /* descriptors that are built on-demand */
332 static char manufacturer [50];
333 static char product_desc [40] = DRIVER_DESC;
334 static char serial_num [40] = "1";
335 static char pnp_string [1024] =
336 "XXMFG:linux;MDL:g_printer;CLS:PRINTER;SN:1;";
338 /* static strings, in UTF-8 */
339 static struct usb_string strings [] = {
340 { STRING_MANUFACTURER, manufacturer, },
341 { STRING_PRODUCT, product_desc, },
342 { STRING_SERIALNUM, serial_num, },
343 { } /* end of list */
346 static struct usb_gadget_strings stringtab = {
347 .language = 0x0409, /* en-us */
348 .strings = strings,
351 /*-------------------------------------------------------------------------*/
353 static struct usb_request *
354 printer_req_alloc(struct usb_ep *ep, unsigned len, gfp_t gfp_flags)
356 struct usb_request *req;
358 req = usb_ep_alloc_request(ep, gfp_flags);
360 if (req != NULL) {
361 req->length = len;
362 req->buf = kmalloc(len, gfp_flags);
363 if (req->buf == NULL) {
364 usb_ep_free_request(ep, req);
365 return NULL;
369 return req;
372 static void
373 printer_req_free(struct usb_ep *ep, struct usb_request *req)
375 if (ep != NULL && req != NULL) {
376 kfree(req->buf);
377 usb_ep_free_request(ep, req);
381 /*-------------------------------------------------------------------------*/
383 static void rx_complete(struct usb_ep *ep, struct usb_request *req)
385 struct printer_dev *dev = ep->driver_data;
386 int status = req->status;
387 unsigned long flags;
389 spin_lock_irqsave(&dev->lock, flags);
391 list_del_init(&req->list); /* Remode from Active List */
393 switch (status) {
395 /* normal completion */
396 case 0:
397 list_add_tail(&req->list, &dev->rx_buffers);
398 wake_up_interruptible(&dev->rx_wait);
399 DBG(dev, "G_Printer : rx length %d\n", req->actual);
400 break;
402 /* software-driven interface shutdown */
403 case -ECONNRESET: /* unlink */
404 case -ESHUTDOWN: /* disconnect etc */
405 VDBG(dev, "rx shutdown, code %d\n", status);
406 list_add(&req->list, &dev->rx_reqs);
407 break;
409 /* for hardware automagic (such as pxa) */
410 case -ECONNABORTED: /* endpoint reset */
411 DBG(dev, "rx %s reset\n", ep->name);
412 list_add(&req->list, &dev->rx_reqs);
413 break;
415 /* data overrun */
416 case -EOVERFLOW:
417 /* FALLTHROUGH */
419 default:
420 DBG(dev, "rx status %d\n", status);
421 list_add(&req->list, &dev->rx_reqs);
422 break;
424 spin_unlock_irqrestore(&dev->lock, flags);
427 static void tx_complete(struct usb_ep *ep, struct usb_request *req)
429 struct printer_dev *dev = ep->driver_data;
431 switch (req->status) {
432 default:
433 VDBG(dev, "tx err %d\n", req->status);
434 /* FALLTHROUGH */
435 case -ECONNRESET: /* unlink */
436 case -ESHUTDOWN: /* disconnect etc */
437 break;
438 case 0:
439 break;
442 spin_lock(&dev->lock);
443 /* Take the request struct off the active list and put it on the
444 * free list.
446 list_del_init(&req->list);
447 list_add(&req->list, &dev->tx_reqs);
448 wake_up_interruptible(&dev->tx_wait);
449 if (likely(list_empty(&dev->tx_reqs_active)))
450 wake_up_interruptible(&dev->tx_flush_wait);
452 spin_unlock(&dev->lock);
455 /*-------------------------------------------------------------------------*/
457 static int
458 printer_open(struct inode *inode, struct file *fd)
460 struct printer_dev *dev;
461 unsigned long flags;
462 int ret = -EBUSY;
464 dev = container_of(inode->i_cdev, struct printer_dev, printer_cdev);
466 spin_lock_irqsave(&dev->lock, flags);
468 if (!dev->printer_cdev_open) {
469 dev->printer_cdev_open = 1;
470 fd->private_data = dev;
471 ret = 0;
472 /* Change the printer status to show that it's on-line. */
473 dev->printer_status |= PRINTER_SELECTED;
476 spin_unlock_irqrestore(&dev->lock, flags);
478 DBG(dev, "printer_open returned %x\n", ret);
480 return ret;
483 static int
484 printer_close(struct inode *inode, struct file *fd)
486 struct printer_dev *dev = fd->private_data;
487 unsigned long flags;
489 spin_lock_irqsave(&dev->lock, flags);
490 dev->printer_cdev_open = 0;
491 fd->private_data = NULL;
492 /* Change printer status to show that the printer is off-line. */
493 dev->printer_status &= ~PRINTER_SELECTED;
494 spin_unlock_irqrestore(&dev->lock, flags);
496 DBG(dev, "printer_close\n");
498 return 0;
501 static ssize_t
502 printer_read(struct file *fd, char __user *buf, size_t len, loff_t *ptr)
504 struct printer_dev *dev = fd->private_data;
505 unsigned long flags;
506 size_t size;
507 size_t bytes_copied;
508 struct usb_request *req;
509 /* This is a pointer to the current USB rx request. */
510 struct usb_request *current_rx_req;
511 /* This is the number of bytes in the current rx buffer. */
512 size_t current_rx_bytes;
513 /* This is a pointer to the current rx buffer. */
514 u8 *current_rx_buf;
516 if (len == 0)
517 return -EINVAL;
519 DBG(dev, "printer_read trying to read %d bytes\n", (int)len);
521 spin_lock(&dev->lock_printer_io);
522 spin_lock_irqsave(&dev->lock, flags);
524 /* We will use this flag later to check if a printer reset happened
525 * after we turn interrupts back on.
527 dev->reset_printer = 0;
529 while (likely(!list_empty(&dev->rx_reqs))) {
530 int error;
532 req = container_of(dev->rx_reqs.next,
533 struct usb_request, list);
534 list_del_init(&req->list);
536 /* The USB Host sends us whatever amount of data it wants to
537 * so we always set the length field to the full USB_BUFSIZE.
538 * If the amount of data is more than the read() caller asked
539 * for it will be stored in the request buffer until it is
540 * asked for by read().
542 req->length = USB_BUFSIZE;
543 req->complete = rx_complete;
545 error = usb_ep_queue(dev->out_ep, req, GFP_ATOMIC);
546 if (error) {
547 DBG(dev, "rx submit --> %d\n", error);
548 list_add(&req->list, &dev->rx_reqs);
549 break;
550 } else {
551 list_add(&req->list, &dev->rx_reqs_active);
555 bytes_copied = 0;
556 current_rx_req = dev->current_rx_req;
557 current_rx_bytes = dev->current_rx_bytes;
558 current_rx_buf = dev->current_rx_buf;
559 dev->current_rx_req = NULL;
560 dev->current_rx_bytes = 0;
561 dev->current_rx_buf = NULL;
563 /* Check if there is any data in the read buffers. Please note that
564 * current_rx_bytes is the number of bytes in the current rx buffer.
565 * If it is zero then check if there are any other rx_buffers that
566 * are on the completed list. We are only out of data if all rx
567 * buffers are empty.
569 if ((current_rx_bytes == 0) &&
570 (likely(list_empty(&dev->rx_buffers)))) {
571 /* Turn interrupts back on before sleeping. */
572 spin_unlock_irqrestore(&dev->lock, flags);
575 * If no data is available check if this is a NON-Blocking
576 * call or not.
578 if (fd->f_flags & (O_NONBLOCK|O_NDELAY)) {
579 spin_unlock(&dev->lock_printer_io);
580 return -EAGAIN;
583 /* Sleep until data is available */
584 wait_event_interruptible(dev->rx_wait,
585 (likely(!list_empty(&dev->rx_buffers))));
586 spin_lock_irqsave(&dev->lock, flags);
589 /* We have data to return then copy it to the caller's buffer.*/
590 while ((current_rx_bytes || likely(!list_empty(&dev->rx_buffers)))
591 && len) {
592 if (current_rx_bytes == 0) {
593 req = container_of(dev->rx_buffers.next,
594 struct usb_request, list);
595 list_del_init(&req->list);
597 if (req->actual && req->buf) {
598 current_rx_req = req;
599 current_rx_bytes = req->actual;
600 current_rx_buf = req->buf;
601 } else {
602 list_add(&req->list, &dev->rx_reqs);
603 continue;
607 /* Don't leave irqs off while doing memory copies */
608 spin_unlock_irqrestore(&dev->lock, flags);
610 if (len > current_rx_bytes)
611 size = current_rx_bytes;
612 else
613 size = len;
615 size -= copy_to_user(buf, current_rx_buf, size);
616 bytes_copied += size;
617 len -= size;
618 buf += size;
620 spin_lock_irqsave(&dev->lock, flags);
622 /* We've disconnected or reset free the req and buffer */
623 if (dev->reset_printer) {
624 printer_req_free(dev->out_ep, current_rx_req);
625 spin_unlock_irqrestore(&dev->lock, flags);
626 spin_unlock(&dev->lock_printer_io);
627 return -EAGAIN;
630 /* If we not returning all the data left in this RX request
631 * buffer then adjust the amount of data left in the buffer.
632 * Othewise if we are done with this RX request buffer then
633 * requeue it to get any incoming data from the USB host.
635 if (size < current_rx_bytes) {
636 current_rx_bytes -= size;
637 current_rx_buf += size;
638 } else {
639 list_add(&current_rx_req->list, &dev->rx_reqs);
640 current_rx_bytes = 0;
641 current_rx_buf = NULL;
642 current_rx_req = NULL;
646 dev->current_rx_req = current_rx_req;
647 dev->current_rx_bytes = current_rx_bytes;
648 dev->current_rx_buf = current_rx_buf;
650 spin_unlock_irqrestore(&dev->lock, flags);
651 spin_unlock(&dev->lock_printer_io);
653 DBG(dev, "printer_read returned %d bytes\n", (int)bytes_copied);
655 if (bytes_copied)
656 return bytes_copied;
657 else
658 return -EAGAIN;
661 static ssize_t
662 printer_write(struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
664 struct printer_dev *dev = fd->private_data;
665 unsigned long flags;
666 size_t size; /* Amount of data in a TX request. */
667 size_t bytes_copied = 0;
668 struct usb_request *req;
670 DBG(dev, "printer_write trying to send %d bytes\n", (int)len);
672 if (len == 0)
673 return -EINVAL;
675 spin_lock(&dev->lock_printer_io);
676 spin_lock_irqsave(&dev->lock, flags);
678 /* Check if a printer reset happens while we have interrupts on */
679 dev->reset_printer = 0;
681 /* Check if there is any available write buffers */
682 if (likely(list_empty(&dev->tx_reqs))) {
683 /* Turn interrupts back on before sleeping. */
684 spin_unlock_irqrestore(&dev->lock, flags);
687 * If write buffers are available check if this is
688 * a NON-Blocking call or not.
690 if (fd->f_flags & (O_NONBLOCK|O_NDELAY)) {
691 spin_unlock(&dev->lock_printer_io);
692 return -EAGAIN;
695 /* Sleep until a write buffer is available */
696 wait_event_interruptible(dev->tx_wait,
697 (likely(!list_empty(&dev->tx_reqs))));
698 spin_lock_irqsave(&dev->lock, flags);
701 while (likely(!list_empty(&dev->tx_reqs)) && len) {
703 if (len > USB_BUFSIZE)
704 size = USB_BUFSIZE;
705 else
706 size = len;
708 req = container_of(dev->tx_reqs.next, struct usb_request,
709 list);
710 list_del_init(&req->list);
712 req->complete = tx_complete;
713 req->length = size;
715 /* Check if we need to send a zero length packet. */
716 if (len > size)
717 /* They will be more TX requests so no yet. */
718 req->zero = 0;
719 else
720 /* If the data amount is not a multple of the
721 * maxpacket size then send a zero length packet.
723 req->zero = ((len % dev->in_ep->maxpacket) == 0);
725 /* Don't leave irqs off while doing memory copies */
726 spin_unlock_irqrestore(&dev->lock, flags);
728 if (copy_from_user(req->buf, buf, size)) {
729 list_add(&req->list, &dev->tx_reqs);
730 spin_unlock(&dev->lock_printer_io);
731 return bytes_copied;
734 bytes_copied += size;
735 len -= size;
736 buf += size;
738 spin_lock_irqsave(&dev->lock, flags);
740 /* We've disconnected or reset so free the req and buffer */
741 if (dev->reset_printer) {
742 printer_req_free(dev->in_ep, req);
743 spin_unlock_irqrestore(&dev->lock, flags);
744 spin_unlock(&dev->lock_printer_io);
745 return -EAGAIN;
748 if (usb_ep_queue(dev->in_ep, req, GFP_ATOMIC)) {
749 list_add(&req->list, &dev->tx_reqs);
750 spin_unlock_irqrestore(&dev->lock, flags);
751 spin_unlock(&dev->lock_printer_io);
752 return -EAGAIN;
755 list_add(&req->list, &dev->tx_reqs_active);
759 spin_unlock_irqrestore(&dev->lock, flags);
760 spin_unlock(&dev->lock_printer_io);
762 DBG(dev, "printer_write sent %d bytes\n", (int)bytes_copied);
764 if (bytes_copied) {
765 return bytes_copied;
766 } else {
767 return -EAGAIN;
771 static int
772 printer_fsync(struct file *fd, struct dentry *dentry, int datasync)
774 struct printer_dev *dev = fd->private_data;
775 unsigned long flags;
776 int tx_list_empty;
778 spin_lock_irqsave(&dev->lock, flags);
779 tx_list_empty = (likely(list_empty(&dev->tx_reqs)));
780 spin_unlock_irqrestore(&dev->lock, flags);
782 if (!tx_list_empty) {
783 /* Sleep until all data has been sent */
784 wait_event_interruptible(dev->tx_flush_wait,
785 (likely(list_empty(&dev->tx_reqs_active))));
788 return 0;
791 static unsigned int
792 printer_poll(struct file *fd, poll_table *wait)
794 struct printer_dev *dev = fd->private_data;
795 unsigned long flags;
796 int status = 0;
798 poll_wait(fd, &dev->rx_wait, wait);
799 poll_wait(fd, &dev->tx_wait, wait);
801 spin_lock_irqsave(&dev->lock, flags);
802 if (likely(!list_empty(&dev->tx_reqs)))
803 status |= POLLOUT | POLLWRNORM;
805 if (likely(!list_empty(&dev->rx_buffers)))
806 status |= POLLIN | POLLRDNORM;
808 spin_unlock_irqrestore(&dev->lock, flags);
810 return status;
813 static int
814 printer_ioctl(struct inode *inode, struct file *fd, unsigned int code,
815 unsigned long arg)
817 struct printer_dev *dev = fd->private_data;
818 unsigned long flags;
819 int status = 0;
821 DBG(dev, "printer_ioctl: cmd=0x%4.4x, arg=%lu\n", code, arg);
823 /* handle ioctls */
825 spin_lock_irqsave(&dev->lock, flags);
827 switch (code) {
828 case GADGET_GET_PRINTER_STATUS:
829 status = (int)dev->printer_status;
830 break;
831 case GADGET_SET_PRINTER_STATUS:
832 dev->printer_status = (u8)arg;
833 break;
834 default:
835 /* could not handle ioctl */
836 DBG(dev, "printer_ioctl: ERROR cmd=0x%4.4xis not supported\n",
837 code);
838 status = -ENOTTY;
841 spin_unlock_irqrestore(&dev->lock, flags);
843 return status;
846 /* used after endpoint configuration */
847 static struct file_operations printer_io_operations = {
848 .owner = THIS_MODULE,
849 .open = printer_open,
850 .read = printer_read,
851 .write = printer_write,
852 .fsync = printer_fsync,
853 .poll = printer_poll,
854 .ioctl = printer_ioctl,
855 .release = printer_close
858 /*-------------------------------------------------------------------------*/
860 static int
861 set_printer_interface(struct printer_dev *dev)
863 int result = 0;
865 dev->in = ep_desc(dev->gadget, &hs_ep_in_desc, &fs_ep_in_desc);
866 dev->in_ep->driver_data = dev;
868 dev->out = ep_desc(dev->gadget, &hs_ep_out_desc, &fs_ep_out_desc);
869 dev->out_ep->driver_data = dev;
871 result = usb_ep_enable(dev->in_ep, dev->in);
872 if (result != 0) {
873 DBG(dev, "enable %s --> %d\n", dev->in_ep->name, result);
874 goto done;
877 result = usb_ep_enable(dev->out_ep, dev->out);
878 if (result != 0) {
879 DBG(dev, "enable %s --> %d\n", dev->in_ep->name, result);
880 goto done;
883 done:
884 /* on error, disable any endpoints */
885 if (result != 0) {
886 (void) usb_ep_disable(dev->in_ep);
887 (void) usb_ep_disable(dev->out_ep);
888 dev->in = NULL;
889 dev->out = NULL;
892 /* caller is responsible for cleanup on error */
893 return result;
896 static void printer_reset_interface(struct printer_dev *dev)
898 if (dev->interface < 0)
899 return;
901 DBG(dev, "%s\n", __FUNCTION__);
903 if (dev->in)
904 usb_ep_disable(dev->in_ep);
906 if (dev->out)
907 usb_ep_disable(dev->out_ep);
909 dev->interface = -1;
912 /* change our operational config. must agree with the code
913 * that returns config descriptors, and altsetting code.
915 static int
916 printer_set_config(struct printer_dev *dev, unsigned number)
918 int result = 0;
919 struct usb_gadget *gadget = dev->gadget;
921 if (gadget_is_sa1100(gadget) && dev->config) {
922 /* tx fifo is full, but we can't clear it...*/
923 INFO(dev, "can't change configurations\n");
924 return -ESPIPE;
927 switch (number) {
928 case DEV_CONFIG_VALUE:
929 result = 0;
930 break;
931 default:
932 result = -EINVAL;
933 /* FALL THROUGH */
934 case 0:
935 break;
938 if (result) {
939 usb_gadget_vbus_draw(dev->gadget,
940 dev->gadget->is_otg ? 8 : 100);
941 } else {
942 char *speed;
943 unsigned power;
945 power = 2 * config_desc.bMaxPower;
946 usb_gadget_vbus_draw(dev->gadget, power);
948 switch (gadget->speed) {
949 case USB_SPEED_FULL: speed = "full"; break;
950 #ifdef CONFIG_USB_GADGET_DUALSPEED
951 case USB_SPEED_HIGH: speed = "high"; break;
952 #endif
953 default: speed = "?"; break;
956 dev->config = number;
957 INFO(dev, "%s speed config #%d: %d mA, %s\n",
958 speed, number, power, driver_desc);
960 return result;
963 static int
964 config_buf(enum usb_device_speed speed, u8 *buf, u8 type, unsigned index,
965 int is_otg)
967 int len;
968 const struct usb_descriptor_header **function;
969 #ifdef CONFIG_USB_GADGET_DUALSPEED
970 int hs = (speed == USB_SPEED_HIGH);
972 if (type == USB_DT_OTHER_SPEED_CONFIG)
973 hs = !hs;
975 if (hs) {
976 function = hs_printer_function;
977 } else {
978 function = fs_printer_function;
980 #else
981 function = fs_printer_function;
982 #endif
984 if (index >= device_desc.bNumConfigurations)
985 return -EINVAL;
987 /* for now, don't advertise srp-only devices */
988 if (!is_otg)
989 function++;
991 len = usb_gadget_config_buf(&config_desc, buf, USB_DESC_BUFSIZE,
992 function);
993 if (len < 0)
994 return len;
995 ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
996 return len;
999 /* Change our operational Interface. */
1000 static int
1001 set_interface(struct printer_dev *dev, unsigned number)
1003 int result = 0;
1005 if (gadget_is_sa1100(dev->gadget) && dev->interface < 0) {
1006 /* tx fifo is full, but we can't clear it...*/
1007 INFO(dev, "can't change interfaces\n");
1008 return -ESPIPE;
1011 /* Free the current interface */
1012 switch (dev->interface) {
1013 case PRINTER_INTERFACE:
1014 printer_reset_interface(dev);
1015 break;
1018 switch (number) {
1019 case PRINTER_INTERFACE:
1020 result = set_printer_interface(dev);
1021 if (result) {
1022 printer_reset_interface(dev);
1023 } else {
1024 dev->interface = PRINTER_INTERFACE;
1026 break;
1027 default:
1028 result = -EINVAL;
1029 /* FALL THROUGH */
1032 if (!result)
1033 INFO(dev, "Using interface %x\n", number);
1035 return result;
1038 static void printer_setup_complete(struct usb_ep *ep, struct usb_request *req)
1040 if (req->status || req->actual != req->length)
1041 DBG((struct printer_dev *) ep->driver_data,
1042 "setup complete --> %d, %d/%d\n",
1043 req->status, req->actual, req->length);
1046 static void printer_soft_reset(struct printer_dev *dev)
1048 struct usb_request *req;
1050 INFO(dev, "Received Printer Reset Request\n");
1052 if (usb_ep_disable(dev->in_ep))
1053 DBG(dev, "Failed to disable USB in_ep\n");
1054 if (usb_ep_disable(dev->out_ep))
1055 DBG(dev, "Failed to disable USB out_ep\n");
1057 if (dev->current_rx_req != NULL) {
1058 list_add(&dev->current_rx_req->list, &dev->rx_reqs);
1059 dev->current_rx_req = NULL;
1061 dev->current_rx_bytes = 0;
1062 dev->current_rx_buf = NULL;
1063 dev->reset_printer = 1;
1065 while (likely(!(list_empty(&dev->rx_buffers)))) {
1066 req = container_of(dev->rx_buffers.next, struct usb_request,
1067 list);
1068 list_del_init(&req->list);
1069 list_add(&req->list, &dev->rx_reqs);
1072 while (likely(!(list_empty(&dev->rx_reqs_active)))) {
1073 req = container_of(dev->rx_buffers.next, struct usb_request,
1074 list);
1075 list_del_init(&req->list);
1076 list_add(&req->list, &dev->rx_reqs);
1079 while (likely(!(list_empty(&dev->tx_reqs_active)))) {
1080 req = container_of(dev->tx_reqs_active.next,
1081 struct usb_request, list);
1082 list_del_init(&req->list);
1083 list_add(&req->list, &dev->tx_reqs);
1086 if (usb_ep_enable(dev->in_ep, dev->in))
1087 DBG(dev, "Failed to enable USB in_ep\n");
1088 if (usb_ep_enable(dev->out_ep, dev->out))
1089 DBG(dev, "Failed to enable USB out_ep\n");
1091 wake_up_interruptible(&dev->tx_wait);
1092 wake_up_interruptible(&dev->tx_flush_wait);
1095 /*-------------------------------------------------------------------------*/
1098 * The setup() callback implements all the ep0 functionality that's not
1099 * handled lower down.
1101 static int
1102 printer_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1104 struct printer_dev *dev = get_gadget_data(gadget);
1105 struct usb_request *req = dev->req;
1106 int value = -EOPNOTSUPP;
1107 u16 wIndex = le16_to_cpu(ctrl->wIndex);
1108 u16 wValue = le16_to_cpu(ctrl->wValue);
1109 u16 wLength = le16_to_cpu(ctrl->wLength);
1111 DBG(dev, "ctrl req%02x.%02x v%04x i%04x l%d\n",
1112 ctrl->bRequestType, ctrl->bRequest, wValue, wIndex, wLength);
1114 req->complete = printer_setup_complete;
1116 switch (ctrl->bRequestType&USB_TYPE_MASK) {
1118 case USB_TYPE_STANDARD:
1119 switch (ctrl->bRequest) {
1121 case USB_REQ_GET_DESCRIPTOR:
1122 if (ctrl->bRequestType != USB_DIR_IN)
1123 break;
1124 switch (wValue >> 8) {
1126 case USB_DT_DEVICE:
1127 value = min(wLength, (u16) sizeof device_desc);
1128 memcpy(req->buf, &device_desc, value);
1129 break;
1130 #ifdef CONFIG_USB_GADGET_DUALSPEED
1131 case USB_DT_DEVICE_QUALIFIER:
1132 if (!gadget->is_dualspeed)
1133 break;
1134 value = min(wLength,
1135 (u16) sizeof dev_qualifier);
1136 memcpy(req->buf, &dev_qualifier, value);
1137 break;
1139 case USB_DT_OTHER_SPEED_CONFIG:
1140 if (!gadget->is_dualspeed)
1141 break;
1142 /* FALLTHROUGH */
1143 #endif /* CONFIG_USB_GADGET_DUALSPEED */
1144 case USB_DT_CONFIG:
1145 value = config_buf(gadget->speed, req->buf,
1146 wValue >> 8,
1147 wValue & 0xff,
1148 gadget->is_otg);
1149 if (value >= 0)
1150 value = min(wLength, (u16) value);
1151 break;
1153 case USB_DT_STRING:
1154 value = usb_gadget_get_string(&stringtab,
1155 wValue & 0xff, req->buf);
1156 if (value >= 0)
1157 value = min(wLength, (u16) value);
1158 break;
1160 break;
1162 case USB_REQ_SET_CONFIGURATION:
1163 if (ctrl->bRequestType != 0)
1164 break;
1165 if (gadget->a_hnp_support)
1166 DBG(dev, "HNP available\n");
1167 else if (gadget->a_alt_hnp_support)
1168 DBG(dev, "HNP needs a different root port\n");
1169 value = printer_set_config(dev, wValue);
1170 break;
1171 case USB_REQ_GET_CONFIGURATION:
1172 if (ctrl->bRequestType != USB_DIR_IN)
1173 break;
1174 *(u8 *)req->buf = dev->config;
1175 value = min(wLength, (u16) 1);
1176 break;
1178 case USB_REQ_SET_INTERFACE:
1179 if (ctrl->bRequestType != USB_RECIP_INTERFACE ||
1180 !dev->config)
1181 break;
1183 value = set_interface(dev, PRINTER_INTERFACE);
1184 break;
1185 case USB_REQ_GET_INTERFACE:
1186 if (ctrl->bRequestType !=
1187 (USB_DIR_IN|USB_RECIP_INTERFACE)
1188 || !dev->config)
1189 break;
1191 *(u8 *)req->buf = dev->interface;
1192 value = min(wLength, (u16) 1);
1193 break;
1195 default:
1196 goto unknown;
1198 break;
1200 case USB_TYPE_CLASS:
1201 switch (ctrl->bRequest) {
1202 case 0: /* Get the IEEE-1284 PNP String */
1203 /* Only one printer interface is supported. */
1204 if ((wIndex>>8) != PRINTER_INTERFACE)
1205 break;
1207 value = (pnp_string[0]<<8)|pnp_string[1];
1208 memcpy(req->buf, pnp_string, value);
1209 DBG(dev, "1284 PNP String: %x %s\n", value,
1210 &pnp_string[2]);
1211 break;
1213 case 1: /* Get Port Status */
1214 /* Only one printer interface is supported. */
1215 if (wIndex != PRINTER_INTERFACE)
1216 break;
1218 *(u8 *)req->buf = dev->printer_status;
1219 value = min(wLength, (u16) 1);
1220 break;
1222 case 2: /* Soft Reset */
1223 /* Only one printer interface is supported. */
1224 if (wIndex != PRINTER_INTERFACE)
1225 break;
1227 printer_soft_reset(dev);
1229 value = 0;
1230 break;
1232 default:
1233 goto unknown;
1235 break;
1237 default:
1238 unknown:
1239 VDBG(dev,
1240 "unknown ctrl req%02x.%02x v%04x i%04x l%d\n",
1241 ctrl->bRequestType, ctrl->bRequest,
1242 wValue, wIndex, wLength);
1243 break;
1246 /* respond with data transfer before status phase? */
1247 if (value >= 0) {
1248 req->length = value;
1249 req->zero = value < wLength
1250 && (value % gadget->ep0->maxpacket) == 0;
1251 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
1252 if (value < 0) {
1253 DBG(dev, "ep_queue --> %d\n", value);
1254 req->status = 0;
1255 printer_setup_complete(gadget->ep0, req);
1259 /* host either stalls (value < 0) or reports success */
1260 return value;
1263 static void
1264 printer_disconnect(struct usb_gadget *gadget)
1266 struct printer_dev *dev = get_gadget_data(gadget);
1267 unsigned long flags;
1269 DBG(dev, "%s\n", __FUNCTION__);
1271 spin_lock_irqsave(&dev->lock, flags);
1273 printer_reset_interface(dev);
1275 spin_unlock_irqrestore(&dev->lock, flags);
1278 static void
1279 printer_unbind(struct usb_gadget *gadget)
1281 struct printer_dev *dev = get_gadget_data(gadget);
1282 struct usb_request *req;
1285 DBG(dev, "%s\n", __FUNCTION__);
1287 /* Remove sysfs files */
1288 device_destroy(usb_gadget_class, g_printer_devno);
1290 /* Remove Character Device */
1291 cdev_del(&dev->printer_cdev);
1293 /* we must already have been disconnected ... no i/o may be active */
1294 WARN_ON(!list_empty(&dev->tx_reqs_active));
1295 WARN_ON(!list_empty(&dev->rx_reqs_active));
1297 /* Free all memory for this driver. */
1298 while (!list_empty(&dev->tx_reqs)) {
1299 req = container_of(dev->tx_reqs.next, struct usb_request,
1300 list);
1301 list_del(&req->list);
1302 printer_req_free(dev->in_ep, req);
1305 <<<<<<< HEAD:drivers/usb/gadget/printer.c
1306 if (dev->current_rx_req != NULL);
1307 =======
1308 if (dev->current_rx_req != NULL)
1309 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/usb/gadget/printer.c
1310 printer_req_free(dev->out_ep, dev->current_rx_req);
1312 while (!list_empty(&dev->rx_reqs)) {
1313 req = container_of(dev->rx_reqs.next,
1314 struct usb_request, list);
1315 list_del(&req->list);
1316 printer_req_free(dev->out_ep, req);
1319 while (!list_empty(&dev->rx_buffers)) {
1320 req = container_of(dev->rx_buffers.next,
1321 struct usb_request, list);
1322 list_del(&req->list);
1323 printer_req_free(dev->out_ep, req);
1326 if (dev->req) {
1327 printer_req_free(gadget->ep0, dev->req);
1328 dev->req = NULL;
1331 set_gadget_data(gadget, NULL);
1334 static int __init
1335 printer_bind(struct usb_gadget *gadget)
1337 struct printer_dev *dev;
1338 struct usb_ep *in_ep, *out_ep;
1339 int status = -ENOMEM;
1340 int gcnum;
1341 size_t len;
1342 u32 i;
1343 struct usb_request *req;
1345 dev = &usb_printer_gadget;
1348 /* Setup the sysfs files for the printer gadget. */
1349 dev->pdev = device_create(usb_gadget_class, NULL, g_printer_devno,
1350 "g_printer");
1351 if (IS_ERR(dev->pdev)) {
1352 ERROR(dev, "Failed to create device: g_printer\n");
1353 goto fail;
1357 * Register a character device as an interface to a user mode
1358 * program that handles the printer specific functionality.
1360 cdev_init(&dev->printer_cdev, &printer_io_operations);
1361 dev->printer_cdev.owner = THIS_MODULE;
1362 status = cdev_add(&dev->printer_cdev, g_printer_devno, 1);
1363 if (status) {
1364 ERROR(dev, "Failed to open char device\n");
1365 goto fail;
1368 if (gadget_is_sa1100(gadget)) {
1369 /* hardware can't write zero length packets. */
1370 ERROR(dev, "SA1100 controller is unsupport by this driver\n");
1371 goto fail;
1374 gcnum = usb_gadget_controller_number(gadget);
1375 if (gcnum >= 0) {
1376 device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
1377 } else {
1378 dev_warn(&gadget->dev, "controller '%s' not recognized\n",
1379 gadget->name);
1380 /* unrecognized, but safe unless bulk is REALLY quirky */
1381 device_desc.bcdDevice =
1382 __constant_cpu_to_le16(0xFFFF);
1384 snprintf(manufacturer, sizeof(manufacturer), "%s %s with %s",
1385 init_utsname()->sysname, init_utsname()->release,
1386 gadget->name);
1388 device_desc.idVendor =
1389 __constant_cpu_to_le16(PRINTER_VENDOR_NUM);
1390 device_desc.idProduct =
1391 __constant_cpu_to_le16(PRINTER_PRODUCT_NUM);
1393 /* support optional vendor/distro customization */
1394 if (idVendor) {
1395 if (!idProduct) {
1396 dev_err(&gadget->dev, "idVendor needs idProduct!\n");
1397 return -ENODEV;
1399 device_desc.idVendor = cpu_to_le16(idVendor);
1400 device_desc.idProduct = cpu_to_le16(idProduct);
1401 if (bcdDevice)
1402 device_desc.bcdDevice = cpu_to_le16(bcdDevice);
1405 if (iManufacturer)
1406 strlcpy(manufacturer, iManufacturer, sizeof manufacturer);
1408 if (iProduct)
1409 strlcpy(product_desc, iProduct, sizeof product_desc);
1411 if (iSerialNum)
1412 strlcpy(serial_num, iSerialNum, sizeof serial_num);
1414 if (iPNPstring)
1415 strlcpy(&pnp_string[2], iPNPstring, (sizeof pnp_string)-2);
1417 len = strlen(pnp_string);
1418 pnp_string[0] = (len >> 8) & 0xFF;
1419 pnp_string[1] = len & 0xFF;
1421 /* all we really need is bulk IN/OUT */
1422 usb_ep_autoconfig_reset(gadget);
1423 in_ep = usb_ep_autoconfig(gadget, &fs_ep_in_desc);
1424 if (!in_ep) {
1425 autoconf_fail:
1426 dev_err(&gadget->dev, "can't autoconfigure on %s\n",
1427 gadget->name);
1428 return -ENODEV;
1430 in_ep->driver_data = in_ep; /* claim */
1432 out_ep = usb_ep_autoconfig(gadget, &fs_ep_out_desc);
1433 if (!out_ep)
1434 goto autoconf_fail;
1435 out_ep->driver_data = out_ep; /* claim */
1437 #ifdef CONFIG_USB_GADGET_DUALSPEED
1438 /* assumes ep0 uses the same value for both speeds ... */
1439 dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
1441 /* and that all endpoints are dual-speed */
1442 hs_ep_in_desc.bEndpointAddress = fs_ep_in_desc.bEndpointAddress;
1443 hs_ep_out_desc.bEndpointAddress = fs_ep_out_desc.bEndpointAddress;
1444 #endif /* DUALSPEED */
1446 device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
1447 usb_gadget_set_selfpowered(gadget);
1449 if (gadget->is_otg) {
1450 otg_desc.bmAttributes |= USB_OTG_HNP,
1451 config_desc.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1452 config_desc.bMaxPower = 4;
1455 spin_lock_init(&dev->lock);
1456 spin_lock_init(&dev->lock_printer_io);
1457 INIT_LIST_HEAD(&dev->tx_reqs);
1458 INIT_LIST_HEAD(&dev->tx_reqs_active);
1459 INIT_LIST_HEAD(&dev->rx_reqs);
1460 INIT_LIST_HEAD(&dev->rx_reqs_active);
1461 INIT_LIST_HEAD(&dev->rx_buffers);
1462 init_waitqueue_head(&dev->rx_wait);
1463 init_waitqueue_head(&dev->tx_wait);
1464 init_waitqueue_head(&dev->tx_flush_wait);
1466 dev->config = 0;
1467 dev->interface = -1;
1468 dev->printer_cdev_open = 0;
1469 dev->printer_status = PRINTER_NOT_ERROR;
1470 dev->current_rx_req = NULL;
1471 dev->current_rx_bytes = 0;
1472 dev->current_rx_buf = NULL;
1474 dev->in_ep = in_ep;
1475 dev->out_ep = out_ep;
1477 /* preallocate control message data and buffer */
1478 dev->req = printer_req_alloc(gadget->ep0, USB_DESC_BUFSIZE,
1479 GFP_KERNEL);
1480 if (!dev->req) {
1481 status = -ENOMEM;
1482 goto fail;
1485 for (i = 0; i < QLEN; i++) {
1486 req = printer_req_alloc(dev->in_ep, USB_BUFSIZE, GFP_KERNEL);
1487 if (!req) {
1488 while (!list_empty(&dev->tx_reqs)) {
1489 req = container_of(dev->tx_reqs.next,
1490 struct usb_request, list);
1491 list_del(&req->list);
1492 printer_req_free(dev->in_ep, req);
1494 return -ENOMEM;
1496 list_add(&req->list, &dev->tx_reqs);
1499 for (i = 0; i < QLEN; i++) {
1500 req = printer_req_alloc(dev->out_ep, USB_BUFSIZE, GFP_KERNEL);
1501 if (!req) {
1502 while (!list_empty(&dev->rx_reqs)) {
1503 req = container_of(dev->rx_reqs.next,
1504 struct usb_request, list);
1505 list_del(&req->list);
1506 printer_req_free(dev->out_ep, req);
1508 return -ENOMEM;
1510 list_add(&req->list, &dev->rx_reqs);
1513 dev->req->complete = printer_setup_complete;
1515 /* finish hookup to lower layer ... */
1516 dev->gadget = gadget;
1517 set_gadget_data(gadget, dev);
1518 gadget->ep0->driver_data = dev;
1520 INFO(dev, "%s, version: " DRIVER_VERSION "\n", driver_desc);
1521 INFO(dev, "using %s, OUT %s IN %s\n", gadget->name, out_ep->name,
1522 in_ep->name);
1524 return 0;
1526 fail:
1527 printer_unbind(gadget);
1528 return status;
1531 /*-------------------------------------------------------------------------*/
1533 static struct usb_gadget_driver printer_driver = {
1534 .speed = DEVSPEED,
1536 .function = (char *) driver_desc,
1537 .bind = printer_bind,
1538 .unbind = printer_unbind,
1540 .setup = printer_setup,
1541 .disconnect = printer_disconnect,
1543 .driver = {
1544 .name = (char *) shortname,
1545 .owner = THIS_MODULE,
1549 MODULE_DESCRIPTION(DRIVER_DESC);
1550 MODULE_AUTHOR("Craig Nadler");
1551 MODULE_LICENSE("GPL");
1553 static int __init
1554 init(void)
1556 int status;
1558 usb_gadget_class = class_create(THIS_MODULE, "usb_printer_gadget");
1559 if (IS_ERR(usb_gadget_class)) {
1560 status = PTR_ERR(usb_gadget_class);
1561 ERROR(dev, "unable to create usb_gadget class %d\n", status);
1562 return status;
1565 status = alloc_chrdev_region(&g_printer_devno, 0, 1,
1566 "USB printer gadget");
1567 if (status) {
1568 ERROR(dev, "alloc_chrdev_region %d\n", status);
1569 class_destroy(usb_gadget_class);
1570 return status;
1573 status = usb_gadget_register_driver(&printer_driver);
1574 if (status) {
1575 class_destroy(usb_gadget_class);
1576 unregister_chrdev_region(g_printer_devno, 1);
1577 DBG(dev, "usb_gadget_register_driver %x\n", status);
1580 return status;
1582 module_init(init);
1584 static void __exit
1585 cleanup(void)
1587 int status;
1589 spin_lock(&usb_printer_gadget.lock_printer_io);
1590 class_destroy(usb_gadget_class);
1591 unregister_chrdev_region(g_printer_devno, 2);
1593 status = usb_gadget_unregister_driver(&printer_driver);
1594 if (status)
1595 ERROR(dev, "usb_gadget_unregister_driver %x\n", status);
1597 spin_unlock(&usb_printer_gadget.lock_printer_io);
1599 module_exit(cleanup);