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