2 * printer.c -- Printer gadget driver
4 * Copyright (C) 2003-2005 David Brownell
5 * Copyright (C) 2006 Craig W. Nadler
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <linux/module.h>
23 #include <linux/kernel.h>
24 #include <linux/delay.h>
25 #include <linux/ioport.h>
26 #include <linux/sched.h>
27 #include <linux/slab.h>
28 #include <linux/smp_lock.h>
29 #include <linux/errno.h>
30 #include <linux/init.h>
31 #include <linux/timer.h>
32 #include <linux/list.h>
33 #include <linux/interrupt.h>
34 #include <linux/utsname.h>
35 #include <linux/device.h>
36 #include <linux/moduleparam.h>
38 #include <linux/poll.h>
39 #include <linux/types.h>
40 #include <linux/ctype.h>
41 #include <linux/cdev.h>
43 #include <asm/byteorder.h>
45 #include <linux/irq.h>
46 #include <asm/system.h>
47 #include <linux/uaccess.h>
48 #include <asm/unaligned.h>
50 #include <linux/usb/ch9.h>
51 #include <linux/usb/gadget.h>
52 #include <linux/usb/g_printer.h>
54 #include "gadget_chips.h"
56 #define DRIVER_DESC "Printer Gadget"
57 #define DRIVER_VERSION "2007 OCT 06"
59 static const char shortname
[] = "printer";
60 static const char driver_desc
[] = DRIVER_DESC
;
62 static dev_t g_printer_devno
;
64 static struct class *usb_gadget_class
;
66 /*-------------------------------------------------------------------------*/
69 spinlock_t lock
; /* lock this structure */
70 /* lock buffer lists during read/write calls */
71 spinlock_t lock_printer_io
;
72 struct usb_gadget
*gadget
;
73 struct usb_request
*req
; /* for control responses */
76 struct usb_ep
*in_ep
, *out_ep
;
77 const struct usb_endpoint_descriptor
79 struct list_head rx_reqs
; /* List of free RX structs */
80 struct list_head rx_reqs_active
; /* List of Active RX xfers */
81 struct list_head rx_buffers
; /* List of completed xfers */
82 /* wait until there is data to be read. */
83 wait_queue_head_t rx_wait
;
84 struct list_head tx_reqs
; /* List of free TX structs */
85 struct list_head tx_reqs_active
; /* List of Active TX xfers */
86 /* Wait until there are write buffers available to use. */
87 wait_queue_head_t tx_wait
;
88 /* Wait until all write buffers have been sent. */
89 wait_queue_head_t tx_flush_wait
;
90 struct usb_request
*current_rx_req
;
91 size_t current_rx_bytes
;
95 <<<<<<< HEAD
:drivers
/usb
/gadget
/printer
.c
96 struct class_device
*printer_class_dev
;
98 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/usb
/gadget
/printer
.c
99 struct cdev printer_cdev
;
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
);
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
163 /*-------------------------------------------------------------------------*/
165 #define xprintk(d, level, fmt, args...) \
166 printk(level "%s: " fmt, DRIVER_DESC, ## args)
169 #define DBG(dev, fmt, args...) \
170 xprintk(dev, KERN_DEBUG, fmt, ## args)
172 #define DBG(dev, fmt, args...) \
177 #define VDBG(dev, fmt, args...) \
178 xprintk(dev, KERN_DEBUG, fmt, ## args)
180 #define VDBG(dev, fmt, args...) \
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 */
242 .bConfigurationValue
= DEV_CONFIG_VALUE
,
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
,
253 .bInterfaceClass
= USB_CLASS_PRINTER
,
254 .bInterfaceSubClass
= 1, /* Printer Sub-Class */
255 .bInterfaceProtocol
= 2, /* Bi-Directional */
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
,
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
,
318 /* maxpacket and other transfer characteristics vary by speed. */
319 #define ep_desc(g, hs, fs) (((g)->speed == USB_SPEED_HIGH)?(hs):(fs))
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 */
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
);
362 req
->buf
= kmalloc(len
, gfp_flags
);
363 if (req
->buf
== NULL
) {
364 usb_ep_free_request(ep
, req
);
373 printer_req_free(struct usb_ep
*ep
, struct usb_request
*req
)
375 if (ep
!= NULL
&& req
!= NULL
) {
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
;
389 spin_lock_irqsave(&dev
->lock
, flags
);
391 list_del_init(&req
->list
); /* Remode from Active List */
395 /* normal completion */
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
);
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
);
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
);
420 DBG(dev
, "rx status %d\n", status
);
421 list_add(&req
->list
, &dev
->rx_reqs
);
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
) {
433 VDBG(dev
, "tx err %d\n", req
->status
);
435 case -ECONNRESET
: /* unlink */
436 case -ESHUTDOWN
: /* disconnect etc */
442 spin_lock(&dev
->lock
);
443 /* Take the request struct off the active list and put it on the
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 /*-------------------------------------------------------------------------*/
458 printer_open(struct inode
*inode
, struct file
*fd
)
460 struct printer_dev
*dev
;
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
;
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
);
484 printer_close(struct inode
*inode
, struct file
*fd
)
486 struct printer_dev
*dev
= fd
->private_data
;
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");
502 printer_read(struct file
*fd
, char __user
*buf
, size_t len
, loff_t
*ptr
)
504 struct printer_dev
*dev
= fd
->private_data
;
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. */
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
))) {
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
);
547 DBG(dev
, "rx submit --> %d\n", error
);
548 list_add(&req
->list
, &dev
->rx_reqs
);
551 list_add(&req
->list
, &dev
->rx_reqs_active
);
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
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
578 if (fd
->f_flags
& (O_NONBLOCK
|O_NDELAY
)) {
579 spin_unlock(&dev
->lock_printer_io
);
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
)))
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
;
602 list_add(&req
->list
, &dev
->rx_reqs
);
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
;
615 size
-= copy_to_user(buf
, current_rx_buf
, size
);
616 bytes_copied
+= 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
);
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
;
639 list_add(¤t_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
);
662 printer_write(struct file
*fd
, const char __user
*buf
, size_t len
, loff_t
*ptr
)
664 struct printer_dev
*dev
= fd
->private_data
;
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
);
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
);
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
)
708 req
= container_of(dev
->tx_reqs
.next
, struct usb_request
,
710 list_del_init(&req
->list
);
712 req
->complete
= tx_complete
;
715 /* Check if we need to send a zero length packet. */
717 /* They will be more TX requests so no yet. */
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
);
734 bytes_copied
+= 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
);
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
);
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
);
772 printer_fsync(struct file
*fd
, struct dentry
*dentry
, int datasync
)
774 struct printer_dev
*dev
= fd
->private_data
;
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
))));
792 printer_poll(struct file
*fd
, poll_table
*wait
)
794 struct printer_dev
*dev
= fd
->private_data
;
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
);
814 printer_ioctl(struct inode
*inode
, struct file
*fd
, unsigned int code
,
817 struct printer_dev
*dev
= fd
->private_data
;
821 DBG(dev
, "printer_ioctl: cmd=0x%4.4x, arg=%lu\n", code
, arg
);
825 spin_lock_irqsave(&dev
->lock
, flags
);
828 case GADGET_GET_PRINTER_STATUS
:
829 status
= (int)dev
->printer_status
;
831 case GADGET_SET_PRINTER_STATUS
:
832 dev
->printer_status
= (u8
)arg
;
835 /* could not handle ioctl */
836 DBG(dev
, "printer_ioctl: ERROR cmd=0x%4.4xis not supported\n",
841 spin_unlock_irqrestore(&dev
->lock
, flags
);
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 /*-------------------------------------------------------------------------*/
861 set_printer_interface(struct printer_dev
*dev
)
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
);
873 DBG(dev
, "enable %s --> %d\n", dev
->in_ep
->name
, result
);
877 result
= usb_ep_enable(dev
->out_ep
, dev
->out
);
879 DBG(dev
, "enable %s --> %d\n", dev
->in_ep
->name
, result
);
884 /* on error, disable any endpoints */
886 (void) usb_ep_disable(dev
->in_ep
);
887 (void) usb_ep_disable(dev
->out_ep
);
892 /* caller is responsible for cleanup on error */
896 static void printer_reset_interface(struct printer_dev
*dev
)
898 if (dev
->interface
< 0)
901 DBG(dev
, "%s\n", __FUNCTION__
);
904 usb_ep_disable(dev
->in_ep
);
907 usb_ep_disable(dev
->out_ep
);
912 /* change our operational config. must agree with the code
913 * that returns config descriptors, and altsetting code.
916 printer_set_config(struct printer_dev
*dev
, unsigned number
)
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");
928 case DEV_CONFIG_VALUE
:
939 usb_gadget_vbus_draw(dev
->gadget
,
940 dev
->gadget
->is_otg
? 8 : 100);
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;
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
);
964 config_buf(enum usb_device_speed speed
, u8
*buf
, u8 type
, unsigned index
,
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
)
976 function
= hs_printer_function
;
978 function
= fs_printer_function
;
981 function
= fs_printer_function
;
984 if (index
>= device_desc
.bNumConfigurations
)
987 /* for now, don't advertise srp-only devices */
991 len
= usb_gadget_config_buf(&config_desc
, buf
, USB_DESC_BUFSIZE
,
995 ((struct usb_config_descriptor
*) buf
)->bDescriptorType
= type
;
999 /* Change our operational Interface. */
1001 set_interface(struct printer_dev
*dev
, unsigned number
)
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");
1011 /* Free the current interface */
1012 switch (dev
->interface
) {
1013 case PRINTER_INTERFACE
:
1014 printer_reset_interface(dev
);
1019 case PRINTER_INTERFACE
:
1020 result
= set_printer_interface(dev
);
1022 printer_reset_interface(dev
);
1024 dev
->interface
= PRINTER_INTERFACE
;
1033 INFO(dev
, "Using interface %x\n", number
);
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
,
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
,
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.
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
)
1124 switch (wValue
>> 8) {
1127 value
= min(wLength
, (u16
) sizeof device_desc
);
1128 memcpy(req
->buf
, &device_desc
, value
);
1130 #ifdef CONFIG_USB_GADGET_DUALSPEED
1131 case USB_DT_DEVICE_QUALIFIER
:
1132 if (!gadget
->is_dualspeed
)
1134 value
= min(wLength
,
1135 (u16
) sizeof dev_qualifier
);
1136 memcpy(req
->buf
, &dev_qualifier
, value
);
1139 case USB_DT_OTHER_SPEED_CONFIG
:
1140 if (!gadget
->is_dualspeed
)
1143 #endif /* CONFIG_USB_GADGET_DUALSPEED */
1145 value
= config_buf(gadget
->speed
, req
->buf
,
1150 value
= min(wLength
, (u16
) value
);
1154 value
= usb_gadget_get_string(&stringtab
,
1155 wValue
& 0xff, req
->buf
);
1157 value
= min(wLength
, (u16
) value
);
1162 case USB_REQ_SET_CONFIGURATION
:
1163 if (ctrl
->bRequestType
!= 0)
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
);
1171 case USB_REQ_GET_CONFIGURATION
:
1172 if (ctrl
->bRequestType
!= USB_DIR_IN
)
1174 *(u8
*)req
->buf
= dev
->config
;
1175 value
= min(wLength
, (u16
) 1);
1178 case USB_REQ_SET_INTERFACE
:
1179 if (ctrl
->bRequestType
!= USB_RECIP_INTERFACE
||
1183 value
= set_interface(dev
, PRINTER_INTERFACE
);
1185 case USB_REQ_GET_INTERFACE
:
1186 if (ctrl
->bRequestType
!=
1187 (USB_DIR_IN
|USB_RECIP_INTERFACE
)
1191 *(u8
*)req
->buf
= dev
->interface
;
1192 value
= min(wLength
, (u16
) 1);
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
)
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
,
1213 case 1: /* Get Port Status */
1214 /* Only one printer interface is supported. */
1215 if (wIndex
!= PRINTER_INTERFACE
)
1218 *(u8
*)req
->buf
= dev
->printer_status
;
1219 value
= min(wLength
, (u16
) 1);
1222 case 2: /* Soft Reset */
1223 /* Only one printer interface is supported. */
1224 if (wIndex
!= PRINTER_INTERFACE
)
1227 printer_soft_reset(dev
);
1240 "unknown ctrl req%02x.%02x v%04x i%04x l%d\n",
1241 ctrl
->bRequestType
, ctrl
->bRequest
,
1242 wValue
, wIndex
, wLength
);
1246 /* respond with data transfer before status phase? */
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
);
1253 DBG(dev
, "ep_queue --> %d\n", value
);
1255 printer_setup_complete(gadget
->ep0
, req
);
1259 /* host either stalls (value < 0) or reports success */
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
);
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
,
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
);
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
);
1327 printer_req_free(gadget
->ep0
, dev
->req
);
1331 set_gadget_data(gadget
, NULL
);
1335 printer_bind(struct usb_gadget
*gadget
)
1337 struct printer_dev
*dev
;
1338 struct usb_ep
*in_ep
, *out_ep
;
1339 int status
= -ENOMEM
;
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
,
1351 if (IS_ERR(dev
->pdev
)) {
1352 ERROR(dev
, "Failed to create device: g_printer\n");
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);
1364 ERROR(dev
, "Failed to open char device\n");
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");
1374 gcnum
= usb_gadget_controller_number(gadget
);
1376 device_desc
.bcdDevice
= cpu_to_le16(0x0200 + gcnum
);
1378 dev_warn(&gadget
->dev
, "controller '%s' not recognized\n",
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
,
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 */
1396 dev_err(&gadget
->dev
, "idVendor needs idProduct!\n");
1399 device_desc
.idVendor
= cpu_to_le16(idVendor
);
1400 device_desc
.idProduct
= cpu_to_le16(idProduct
);
1402 device_desc
.bcdDevice
= cpu_to_le16(bcdDevice
);
1406 strlcpy(manufacturer
, iManufacturer
, sizeof manufacturer
);
1409 strlcpy(product_desc
, iProduct
, sizeof product_desc
);
1412 strlcpy(serial_num
, iSerialNum
, sizeof serial_num
);
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
);
1426 dev_err(&gadget
->dev
, "can't autoconfigure on %s\n",
1430 in_ep
->driver_data
= in_ep
; /* claim */
1432 out_ep
= usb_ep_autoconfig(gadget
, &fs_ep_out_desc
);
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
);
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
;
1475 dev
->out_ep
= out_ep
;
1477 /* preallocate control message data and buffer */
1478 dev
->req
= printer_req_alloc(gadget
->ep0
, USB_DESC_BUFSIZE
,
1485 for (i
= 0; i
< QLEN
; i
++) {
1486 req
= printer_req_alloc(dev
->in_ep
, USB_BUFSIZE
, GFP_KERNEL
);
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
);
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
);
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
);
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
,
1527 printer_unbind(gadget
);
1531 /*-------------------------------------------------------------------------*/
1533 static struct usb_gadget_driver printer_driver
= {
1536 .function
= (char *) driver_desc
,
1537 .bind
= printer_bind
,
1538 .unbind
= printer_unbind
,
1540 .setup
= printer_setup
,
1541 .disconnect
= printer_disconnect
,
1544 .name
= (char *) shortname
,
1545 .owner
= THIS_MODULE
,
1549 MODULE_DESCRIPTION(DRIVER_DESC
);
1550 MODULE_AUTHOR("Craig Nadler");
1551 MODULE_LICENSE("GPL");
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
);
1565 status
= alloc_chrdev_region(&g_printer_devno
, 0, 1,
1566 "USB printer gadget");
1568 ERROR(dev
, "alloc_chrdev_region %d\n", status
);
1569 class_destroy(usb_gadget_class
);
1573 status
= usb_gadget_register_driver(&printer_driver
);
1575 class_destroy(usb_gadget_class
);
1576 unregister_chrdev_region(g_printer_devno
, 1);
1577 DBG(dev
, "usb_gadget_register_driver %x\n", 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
);
1595 ERROR(dev
, "usb_gadget_unregister_driver %x\n", status
);
1597 spin_unlock(&usb_printer_gadget
.lock_printer_io
);
1599 module_exit(cleanup
);