2 * f_printer.c - USB printer function driver
4 * Copied from drivers/usb/gadget/legacy/printer.c,
7 * printer.c -- Printer gadget driver
9 * Copyright (C) 2003-2005 David Brownell
10 * Copyright (C) 2006 Craig W. Nadler
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
18 #include <linux/module.h>
19 #include <linux/kernel.h>
20 #include <linux/delay.h>
21 #include <linux/ioport.h>
22 #include <linux/sched.h>
23 #include <linux/slab.h>
24 #include <linux/mutex.h>
25 #include <linux/errno.h>
26 #include <linux/init.h>
27 #include <linux/idr.h>
28 #include <linux/timer.h>
29 #include <linux/list.h>
30 #include <linux/interrupt.h>
31 #include <linux/device.h>
32 #include <linux/moduleparam.h>
34 #include <linux/poll.h>
35 #include <linux/types.h>
36 #include <linux/ctype.h>
37 #include <linux/cdev.h>
39 #include <asm/byteorder.h>
41 #include <linux/irq.h>
42 #include <linux/uaccess.h>
43 #include <asm/unaligned.h>
45 #include <linux/usb/ch9.h>
46 #include <linux/usb/composite.h>
47 #include <linux/usb/gadget.h>
48 #include <linux/usb/g_printer.h>
50 #include "u_printer.h"
52 #define PNP_STRING_LEN 1024
53 #define PRINTER_MINORS 4
54 #define GET_DEVICE_ID 0
55 #define GET_PORT_STATUS 1
58 static int major
, minors
;
59 static struct class *usb_gadget_class
;
60 static DEFINE_IDA(printer_ida
);
61 static DEFINE_MUTEX(printer_ida_lock
); /* protects access do printer_ida */
63 /*-------------------------------------------------------------------------*/
66 spinlock_t lock
; /* lock this structure */
67 /* lock buffer lists during read/write calls */
68 struct mutex lock_printer_io
;
69 struct usb_gadget
*gadget
;
71 struct usb_ep
*in_ep
, *out_ep
;
73 struct list_head rx_reqs
; /* List of free RX structs */
74 struct list_head rx_reqs_active
; /* List of Active RX xfers */
75 struct list_head rx_buffers
; /* List of completed xfers */
76 /* wait until there is data to be read. */
77 wait_queue_head_t rx_wait
;
78 struct list_head tx_reqs
; /* List of free TX structs */
79 struct list_head tx_reqs_active
; /* List of Active TX xfers */
80 /* Wait until there are write buffers available to use. */
81 wait_queue_head_t tx_wait
;
82 /* Wait until all write buffers have been sent. */
83 wait_queue_head_t tx_flush_wait
;
84 struct usb_request
*current_rx_req
;
85 size_t current_rx_bytes
;
90 struct cdev printer_cdev
;
92 wait_queue_head_t wait
;
94 char *pnp_string
; /* We don't own memory! */
95 struct usb_function function
;
98 static inline struct printer_dev
*func_to_printer(struct usb_function
*f
)
100 return container_of(f
, struct printer_dev
, function
);
103 /*-------------------------------------------------------------------------*/
106 * DESCRIPTORS ... most are static, but strings and (full) configuration
107 * descriptors are built on demand.
110 /* holds our biggest descriptor */
111 #define USB_DESC_BUFSIZE 256
112 #define USB_BUFSIZE 8192
114 static struct usb_interface_descriptor intf_desc
= {
115 .bLength
= sizeof(intf_desc
),
116 .bDescriptorType
= USB_DT_INTERFACE
,
118 .bInterfaceClass
= USB_CLASS_PRINTER
,
119 .bInterfaceSubClass
= 1, /* Printer Sub-Class */
120 .bInterfaceProtocol
= 2, /* Bi-Directional */
124 static struct usb_endpoint_descriptor fs_ep_in_desc
= {
125 .bLength
= USB_DT_ENDPOINT_SIZE
,
126 .bDescriptorType
= USB_DT_ENDPOINT
,
127 .bEndpointAddress
= USB_DIR_IN
,
128 .bmAttributes
= USB_ENDPOINT_XFER_BULK
131 static struct usb_endpoint_descriptor fs_ep_out_desc
= {
132 .bLength
= USB_DT_ENDPOINT_SIZE
,
133 .bDescriptorType
= USB_DT_ENDPOINT
,
134 .bEndpointAddress
= USB_DIR_OUT
,
135 .bmAttributes
= USB_ENDPOINT_XFER_BULK
138 static struct usb_descriptor_header
*fs_printer_function
[] = {
139 (struct usb_descriptor_header
*) &intf_desc
,
140 (struct usb_descriptor_header
*) &fs_ep_in_desc
,
141 (struct usb_descriptor_header
*) &fs_ep_out_desc
,
146 * usb 2.0 devices need to expose both high speed and full speed
147 * descriptors, unless they only run at full speed.
150 static struct usb_endpoint_descriptor hs_ep_in_desc
= {
151 .bLength
= USB_DT_ENDPOINT_SIZE
,
152 .bDescriptorType
= USB_DT_ENDPOINT
,
153 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
154 .wMaxPacketSize
= cpu_to_le16(512)
157 static struct usb_endpoint_descriptor hs_ep_out_desc
= {
158 .bLength
= USB_DT_ENDPOINT_SIZE
,
159 .bDescriptorType
= USB_DT_ENDPOINT
,
160 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
161 .wMaxPacketSize
= cpu_to_le16(512)
164 static struct usb_descriptor_header
*hs_printer_function
[] = {
165 (struct usb_descriptor_header
*) &intf_desc
,
166 (struct usb_descriptor_header
*) &hs_ep_in_desc
,
167 (struct usb_descriptor_header
*) &hs_ep_out_desc
,
172 * Added endpoint descriptors for 3.0 devices
175 static struct usb_endpoint_descriptor ss_ep_in_desc
= {
176 .bLength
= USB_DT_ENDPOINT_SIZE
,
177 .bDescriptorType
= USB_DT_ENDPOINT
,
178 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
179 .wMaxPacketSize
= cpu_to_le16(1024),
182 static struct usb_ss_ep_comp_descriptor ss_ep_in_comp_desc
= {
183 .bLength
= sizeof(ss_ep_in_comp_desc
),
184 .bDescriptorType
= USB_DT_SS_ENDPOINT_COMP
,
187 static struct usb_endpoint_descriptor ss_ep_out_desc
= {
188 .bLength
= USB_DT_ENDPOINT_SIZE
,
189 .bDescriptorType
= USB_DT_ENDPOINT
,
190 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
191 .wMaxPacketSize
= cpu_to_le16(1024),
194 static struct usb_ss_ep_comp_descriptor ss_ep_out_comp_desc
= {
195 .bLength
= sizeof(ss_ep_out_comp_desc
),
196 .bDescriptorType
= USB_DT_SS_ENDPOINT_COMP
,
199 static struct usb_descriptor_header
*ss_printer_function
[] = {
200 (struct usb_descriptor_header
*) &intf_desc
,
201 (struct usb_descriptor_header
*) &ss_ep_in_desc
,
202 (struct usb_descriptor_header
*) &ss_ep_in_comp_desc
,
203 (struct usb_descriptor_header
*) &ss_ep_out_desc
,
204 (struct usb_descriptor_header
*) &ss_ep_out_comp_desc
,
208 /* maxpacket and other transfer characteristics vary by speed. */
209 static inline struct usb_endpoint_descriptor
*ep_desc(struct usb_gadget
*gadget
,
210 struct usb_endpoint_descriptor
*fs
,
211 struct usb_endpoint_descriptor
*hs
,
212 struct usb_endpoint_descriptor
*ss
)
214 switch (gadget
->speed
) {
215 case USB_SPEED_SUPER
:
224 /*-------------------------------------------------------------------------*/
226 static struct usb_request
*
227 printer_req_alloc(struct usb_ep
*ep
, unsigned len
, gfp_t gfp_flags
)
229 struct usb_request
*req
;
231 req
= usb_ep_alloc_request(ep
, gfp_flags
);
235 req
->buf
= kmalloc(len
, gfp_flags
);
236 if (req
->buf
== NULL
) {
237 usb_ep_free_request(ep
, req
);
246 printer_req_free(struct usb_ep
*ep
, struct usb_request
*req
)
248 if (ep
!= NULL
&& req
!= NULL
) {
250 usb_ep_free_request(ep
, req
);
254 /*-------------------------------------------------------------------------*/
256 static void rx_complete(struct usb_ep
*ep
, struct usb_request
*req
)
258 struct printer_dev
*dev
= ep
->driver_data
;
259 int status
= req
->status
;
262 spin_lock_irqsave(&dev
->lock
, flags
);
264 list_del_init(&req
->list
); /* Remode from Active List */
268 /* normal completion */
270 if (req
->actual
> 0) {
271 list_add_tail(&req
->list
, &dev
->rx_buffers
);
272 DBG(dev
, "G_Printer : rx length %d\n", req
->actual
);
274 list_add(&req
->list
, &dev
->rx_reqs
);
278 /* software-driven interface shutdown */
279 case -ECONNRESET
: /* unlink */
280 case -ESHUTDOWN
: /* disconnect etc */
281 VDBG(dev
, "rx shutdown, code %d\n", status
);
282 list_add(&req
->list
, &dev
->rx_reqs
);
285 /* for hardware automagic (such as pxa) */
286 case -ECONNABORTED
: /* endpoint reset */
287 DBG(dev
, "rx %s reset\n", ep
->name
);
288 list_add(&req
->list
, &dev
->rx_reqs
);
296 DBG(dev
, "rx status %d\n", status
);
297 list_add(&req
->list
, &dev
->rx_reqs
);
301 wake_up_interruptible(&dev
->rx_wait
);
302 spin_unlock_irqrestore(&dev
->lock
, flags
);
305 static void tx_complete(struct usb_ep
*ep
, struct usb_request
*req
)
307 struct printer_dev
*dev
= ep
->driver_data
;
309 switch (req
->status
) {
311 VDBG(dev
, "tx err %d\n", req
->status
);
313 case -ECONNRESET
: /* unlink */
314 case -ESHUTDOWN
: /* disconnect etc */
320 spin_lock(&dev
->lock
);
321 /* Take the request struct off the active list and put it on the
324 list_del_init(&req
->list
);
325 list_add(&req
->list
, &dev
->tx_reqs
);
326 wake_up_interruptible(&dev
->tx_wait
);
327 if (likely(list_empty(&dev
->tx_reqs_active
)))
328 wake_up_interruptible(&dev
->tx_flush_wait
);
330 spin_unlock(&dev
->lock
);
333 /*-------------------------------------------------------------------------*/
336 printer_open(struct inode
*inode
, struct file
*fd
)
338 struct printer_dev
*dev
;
342 dev
= container_of(inode
->i_cdev
, struct printer_dev
, printer_cdev
);
344 spin_lock_irqsave(&dev
->lock
, flags
);
346 if (!dev
->printer_cdev_open
) {
347 dev
->printer_cdev_open
= 1;
348 fd
->private_data
= dev
;
350 /* Change the printer status to show that it's on-line. */
351 dev
->printer_status
|= PRINTER_SELECTED
;
354 spin_unlock_irqrestore(&dev
->lock
, flags
);
356 DBG(dev
, "printer_open returned %x\n", ret
);
361 printer_close(struct inode
*inode
, struct file
*fd
)
363 struct printer_dev
*dev
= fd
->private_data
;
366 spin_lock_irqsave(&dev
->lock
, flags
);
367 dev
->printer_cdev_open
= 0;
368 fd
->private_data
= NULL
;
369 /* Change printer status to show that the printer is off-line. */
370 dev
->printer_status
&= ~PRINTER_SELECTED
;
371 spin_unlock_irqrestore(&dev
->lock
, flags
);
373 DBG(dev
, "printer_close\n");
378 /* This function must be called with interrupts turned off. */
380 setup_rx_reqs(struct printer_dev
*dev
)
382 struct usb_request
*req
;
384 while (likely(!list_empty(&dev
->rx_reqs
))) {
387 req
= container_of(dev
->rx_reqs
.next
,
388 struct usb_request
, list
);
389 list_del_init(&req
->list
);
391 /* The USB Host sends us whatever amount of data it wants to
392 * so we always set the length field to the full USB_BUFSIZE.
393 * If the amount of data is more than the read() caller asked
394 * for it will be stored in the request buffer until it is
395 * asked for by read().
397 req
->length
= USB_BUFSIZE
;
398 req
->complete
= rx_complete
;
400 /* here, we unlock, and only unlock, to avoid deadlock. */
401 spin_unlock(&dev
->lock
);
402 error
= usb_ep_queue(dev
->out_ep
, req
, GFP_ATOMIC
);
403 spin_lock(&dev
->lock
);
405 DBG(dev
, "rx submit --> %d\n", error
);
406 list_add(&req
->list
, &dev
->rx_reqs
);
409 /* if the req is empty, then add it into dev->rx_reqs_active. */
410 else if (list_empty(&req
->list
))
411 list_add(&req
->list
, &dev
->rx_reqs_active
);
416 printer_read(struct file
*fd
, char __user
*buf
, size_t len
, loff_t
*ptr
)
418 struct printer_dev
*dev
= fd
->private_data
;
422 struct usb_request
*req
;
423 /* This is a pointer to the current USB rx request. */
424 struct usb_request
*current_rx_req
;
425 /* This is the number of bytes in the current rx buffer. */
426 size_t current_rx_bytes
;
427 /* This is a pointer to the current rx buffer. */
433 DBG(dev
, "printer_read trying to read %d bytes\n", (int)len
);
435 mutex_lock(&dev
->lock_printer_io
);
436 spin_lock_irqsave(&dev
->lock
, flags
);
438 /* We will use this flag later to check if a printer reset happened
439 * after we turn interrupts back on.
441 dev
->reset_printer
= 0;
446 current_rx_req
= dev
->current_rx_req
;
447 current_rx_bytes
= dev
->current_rx_bytes
;
448 current_rx_buf
= dev
->current_rx_buf
;
449 dev
->current_rx_req
= NULL
;
450 dev
->current_rx_bytes
= 0;
451 dev
->current_rx_buf
= NULL
;
453 /* Check if there is any data in the read buffers. Please note that
454 * current_rx_bytes is the number of bytes in the current rx buffer.
455 * If it is zero then check if there are any other rx_buffers that
456 * are on the completed list. We are only out of data if all rx
459 if ((current_rx_bytes
== 0) &&
460 (likely(list_empty(&dev
->rx_buffers
)))) {
461 /* Turn interrupts back on before sleeping. */
462 spin_unlock_irqrestore(&dev
->lock
, flags
);
465 * If no data is available check if this is a NON-Blocking
468 if (fd
->f_flags
& (O_NONBLOCK
|O_NDELAY
)) {
469 mutex_unlock(&dev
->lock_printer_io
);
473 /* Sleep until data is available */
474 wait_event_interruptible(dev
->rx_wait
,
475 (likely(!list_empty(&dev
->rx_buffers
))));
476 spin_lock_irqsave(&dev
->lock
, flags
);
479 /* We have data to return then copy it to the caller's buffer.*/
480 while ((current_rx_bytes
|| likely(!list_empty(&dev
->rx_buffers
)))
482 if (current_rx_bytes
== 0) {
483 req
= container_of(dev
->rx_buffers
.next
,
484 struct usb_request
, list
);
485 list_del_init(&req
->list
);
487 if (req
->actual
&& req
->buf
) {
488 current_rx_req
= req
;
489 current_rx_bytes
= req
->actual
;
490 current_rx_buf
= req
->buf
;
492 list_add(&req
->list
, &dev
->rx_reqs
);
497 /* Don't leave irqs off while doing memory copies */
498 spin_unlock_irqrestore(&dev
->lock
, flags
);
500 if (len
> current_rx_bytes
)
501 size
= current_rx_bytes
;
505 size
-= copy_to_user(buf
, current_rx_buf
, size
);
506 bytes_copied
+= size
;
510 spin_lock_irqsave(&dev
->lock
, flags
);
512 /* We've disconnected or reset so return. */
513 if (dev
->reset_printer
) {
514 list_add(¤t_rx_req
->list
, &dev
->rx_reqs
);
515 spin_unlock_irqrestore(&dev
->lock
, flags
);
516 mutex_unlock(&dev
->lock_printer_io
);
520 /* If we not returning all the data left in this RX request
521 * buffer then adjust the amount of data left in the buffer.
522 * Othewise if we are done with this RX request buffer then
523 * requeue it to get any incoming data from the USB host.
525 if (size
< current_rx_bytes
) {
526 current_rx_bytes
-= size
;
527 current_rx_buf
+= size
;
529 list_add(¤t_rx_req
->list
, &dev
->rx_reqs
);
530 current_rx_bytes
= 0;
531 current_rx_buf
= NULL
;
532 current_rx_req
= NULL
;
536 dev
->current_rx_req
= current_rx_req
;
537 dev
->current_rx_bytes
= current_rx_bytes
;
538 dev
->current_rx_buf
= current_rx_buf
;
540 spin_unlock_irqrestore(&dev
->lock
, flags
);
541 mutex_unlock(&dev
->lock_printer_io
);
543 DBG(dev
, "printer_read returned %d bytes\n", (int)bytes_copied
);
552 printer_write(struct file
*fd
, const char __user
*buf
, size_t len
, loff_t
*ptr
)
554 struct printer_dev
*dev
= fd
->private_data
;
556 size_t size
; /* Amount of data in a TX request. */
557 size_t bytes_copied
= 0;
558 struct usb_request
*req
;
560 DBG(dev
, "printer_write trying to send %d bytes\n", (int)len
);
565 mutex_lock(&dev
->lock_printer_io
);
566 spin_lock_irqsave(&dev
->lock
, flags
);
568 /* Check if a printer reset happens while we have interrupts on */
569 dev
->reset_printer
= 0;
571 /* Check if there is any available write buffers */
572 if (likely(list_empty(&dev
->tx_reqs
))) {
573 /* Turn interrupts back on before sleeping. */
574 spin_unlock_irqrestore(&dev
->lock
, flags
);
577 * If write buffers are available check if this is
578 * a NON-Blocking call or not.
580 if (fd
->f_flags
& (O_NONBLOCK
|O_NDELAY
)) {
581 mutex_unlock(&dev
->lock_printer_io
);
585 /* Sleep until a write buffer is available */
586 wait_event_interruptible(dev
->tx_wait
,
587 (likely(!list_empty(&dev
->tx_reqs
))));
588 spin_lock_irqsave(&dev
->lock
, flags
);
591 while (likely(!list_empty(&dev
->tx_reqs
)) && len
) {
593 if (len
> USB_BUFSIZE
)
598 req
= container_of(dev
->tx_reqs
.next
, struct usb_request
,
600 list_del_init(&req
->list
);
602 req
->complete
= tx_complete
;
605 /* Check if we need to send a zero length packet. */
607 /* They will be more TX requests so no yet. */
610 /* If the data amount is not a multiple of the
611 * maxpacket size then send a zero length packet.
613 req
->zero
= ((len
% dev
->in_ep
->maxpacket
) == 0);
615 /* Don't leave irqs off while doing memory copies */
616 spin_unlock_irqrestore(&dev
->lock
, flags
);
618 if (copy_from_user(req
->buf
, buf
, size
)) {
619 list_add(&req
->list
, &dev
->tx_reqs
);
620 mutex_unlock(&dev
->lock_printer_io
);
624 bytes_copied
+= size
;
628 spin_lock_irqsave(&dev
->lock
, flags
);
630 /* We've disconnected or reset so free the req and buffer */
631 if (dev
->reset_printer
) {
632 list_add(&req
->list
, &dev
->tx_reqs
);
633 spin_unlock_irqrestore(&dev
->lock
, flags
);
634 mutex_unlock(&dev
->lock_printer_io
);
638 if (usb_ep_queue(dev
->in_ep
, req
, GFP_ATOMIC
)) {
639 list_add(&req
->list
, &dev
->tx_reqs
);
640 spin_unlock_irqrestore(&dev
->lock
, flags
);
641 mutex_unlock(&dev
->lock_printer_io
);
645 list_add(&req
->list
, &dev
->tx_reqs_active
);
649 spin_unlock_irqrestore(&dev
->lock
, flags
);
650 mutex_unlock(&dev
->lock_printer_io
);
652 DBG(dev
, "printer_write sent %d bytes\n", (int)bytes_copied
);
661 printer_fsync(struct file
*fd
, loff_t start
, loff_t end
, int datasync
)
663 struct printer_dev
*dev
= fd
->private_data
;
664 struct inode
*inode
= file_inode(fd
);
669 spin_lock_irqsave(&dev
->lock
, flags
);
670 tx_list_empty
= (likely(list_empty(&dev
->tx_reqs
)));
671 spin_unlock_irqrestore(&dev
->lock
, flags
);
673 if (!tx_list_empty
) {
674 /* Sleep until all data has been sent */
675 wait_event_interruptible(dev
->tx_flush_wait
,
676 (likely(list_empty(&dev
->tx_reqs_active
))));
684 printer_poll(struct file
*fd
, poll_table
*wait
)
686 struct printer_dev
*dev
= fd
->private_data
;
690 mutex_lock(&dev
->lock_printer_io
);
691 spin_lock_irqsave(&dev
->lock
, flags
);
693 spin_unlock_irqrestore(&dev
->lock
, flags
);
694 mutex_unlock(&dev
->lock_printer_io
);
696 poll_wait(fd
, &dev
->rx_wait
, wait
);
697 poll_wait(fd
, &dev
->tx_wait
, wait
);
699 spin_lock_irqsave(&dev
->lock
, flags
);
700 if (likely(!list_empty(&dev
->tx_reqs
)))
701 status
|= POLLOUT
| POLLWRNORM
;
703 if (likely(dev
->current_rx_bytes
) ||
704 likely(!list_empty(&dev
->rx_buffers
)))
705 status
|= POLLIN
| POLLRDNORM
;
707 spin_unlock_irqrestore(&dev
->lock
, flags
);
713 printer_ioctl(struct file
*fd
, unsigned int code
, unsigned long arg
)
715 struct printer_dev
*dev
= fd
->private_data
;
719 DBG(dev
, "printer_ioctl: cmd=0x%4.4x, arg=%lu\n", code
, arg
);
723 spin_lock_irqsave(&dev
->lock
, flags
);
726 case GADGET_GET_PRINTER_STATUS
:
727 status
= (int)dev
->printer_status
;
729 case GADGET_SET_PRINTER_STATUS
:
730 dev
->printer_status
= (u8
)arg
;
733 /* could not handle ioctl */
734 DBG(dev
, "printer_ioctl: ERROR cmd=0x%4.4xis not supported\n",
739 spin_unlock_irqrestore(&dev
->lock
, flags
);
744 /* used after endpoint configuration */
745 static const struct file_operations printer_io_operations
= {
746 .owner
= THIS_MODULE
,
747 .open
= printer_open
,
748 .read
= printer_read
,
749 .write
= printer_write
,
750 .fsync
= printer_fsync
,
751 .poll
= printer_poll
,
752 .unlocked_ioctl
= printer_ioctl
,
753 .release
= printer_close
,
754 .llseek
= noop_llseek
,
757 /*-------------------------------------------------------------------------*/
760 set_printer_interface(struct printer_dev
*dev
)
764 dev
->in_ep
->desc
= ep_desc(dev
->gadget
, &fs_ep_in_desc
, &hs_ep_in_desc
,
766 dev
->in_ep
->driver_data
= dev
;
768 dev
->out_ep
->desc
= ep_desc(dev
->gadget
, &fs_ep_out_desc
,
769 &hs_ep_out_desc
, &ss_ep_out_desc
);
770 dev
->out_ep
->driver_data
= dev
;
772 result
= usb_ep_enable(dev
->in_ep
);
774 DBG(dev
, "enable %s --> %d\n", dev
->in_ep
->name
, result
);
778 result
= usb_ep_enable(dev
->out_ep
);
780 DBG(dev
, "enable %s --> %d\n", dev
->in_ep
->name
, result
);
785 /* on error, disable any endpoints */
787 (void) usb_ep_disable(dev
->in_ep
);
788 (void) usb_ep_disable(dev
->out_ep
);
789 dev
->in_ep
->desc
= NULL
;
790 dev
->out_ep
->desc
= NULL
;
793 /* caller is responsible for cleanup on error */
797 static void printer_reset_interface(struct printer_dev
*dev
)
801 if (dev
->interface
< 0)
804 DBG(dev
, "%s\n", __func__
);
806 if (dev
->in_ep
->desc
)
807 usb_ep_disable(dev
->in_ep
);
809 if (dev
->out_ep
->desc
)
810 usb_ep_disable(dev
->out_ep
);
812 spin_lock_irqsave(&dev
->lock
, flags
);
813 dev
->in_ep
->desc
= NULL
;
814 dev
->out_ep
->desc
= NULL
;
816 spin_unlock_irqrestore(&dev
->lock
, flags
);
819 /* Change our operational Interface. */
820 static int set_interface(struct printer_dev
*dev
, unsigned number
)
824 /* Free the current interface */
825 printer_reset_interface(dev
);
827 result
= set_printer_interface(dev
);
829 printer_reset_interface(dev
);
831 dev
->interface
= number
;
834 INFO(dev
, "Using interface %x\n", number
);
839 static void printer_soft_reset(struct printer_dev
*dev
)
841 struct usb_request
*req
;
843 INFO(dev
, "Received Printer Reset Request\n");
845 if (usb_ep_disable(dev
->in_ep
))
846 DBG(dev
, "Failed to disable USB in_ep\n");
847 if (usb_ep_disable(dev
->out_ep
))
848 DBG(dev
, "Failed to disable USB out_ep\n");
850 if (dev
->current_rx_req
!= NULL
) {
851 list_add(&dev
->current_rx_req
->list
, &dev
->rx_reqs
);
852 dev
->current_rx_req
= NULL
;
854 dev
->current_rx_bytes
= 0;
855 dev
->current_rx_buf
= NULL
;
856 dev
->reset_printer
= 1;
858 while (likely(!(list_empty(&dev
->rx_buffers
)))) {
859 req
= container_of(dev
->rx_buffers
.next
, struct usb_request
,
861 list_del_init(&req
->list
);
862 list_add(&req
->list
, &dev
->rx_reqs
);
865 while (likely(!(list_empty(&dev
->rx_reqs_active
)))) {
866 req
= container_of(dev
->rx_buffers
.next
, struct usb_request
,
868 list_del_init(&req
->list
);
869 list_add(&req
->list
, &dev
->rx_reqs
);
872 while (likely(!(list_empty(&dev
->tx_reqs_active
)))) {
873 req
= container_of(dev
->tx_reqs_active
.next
,
874 struct usb_request
, list
);
875 list_del_init(&req
->list
);
876 list_add(&req
->list
, &dev
->tx_reqs
);
879 if (usb_ep_enable(dev
->in_ep
))
880 DBG(dev
, "Failed to enable USB in_ep\n");
881 if (usb_ep_enable(dev
->out_ep
))
882 DBG(dev
, "Failed to enable USB out_ep\n");
884 wake_up_interruptible(&dev
->rx_wait
);
885 wake_up_interruptible(&dev
->tx_wait
);
886 wake_up_interruptible(&dev
->tx_flush_wait
);
889 /*-------------------------------------------------------------------------*/
891 static bool gprinter_req_match(struct usb_function
*f
,
892 const struct usb_ctrlrequest
*ctrl
)
894 struct printer_dev
*dev
= func_to_printer(f
);
895 u16 w_index
= le16_to_cpu(ctrl
->wIndex
);
896 u16 w_value
= le16_to_cpu(ctrl
->wValue
);
897 u16 w_length
= le16_to_cpu(ctrl
->wLength
);
899 if ((ctrl
->bRequestType
& USB_RECIP_MASK
) != USB_RECIP_INTERFACE
||
900 (ctrl
->bRequestType
& USB_TYPE_MASK
) != USB_TYPE_CLASS
)
903 switch (ctrl
->bRequest
) {
906 if (w_length
<= PNP_STRING_LEN
&&
907 (USB_DIR_IN
& ctrl
->bRequestType
))
910 case GET_PORT_STATUS
:
911 if (!w_value
&& w_length
== 1 &&
912 (USB_DIR_IN
& ctrl
->bRequestType
))
916 if (!w_value
&& !w_length
&&
917 !(USB_DIR_IN
& ctrl
->bRequestType
))
923 return w_index
== dev
->interface
;
927 * The setup() callback implements all the ep0 functionality that's not
928 * handled lower down.
930 static int printer_func_setup(struct usb_function
*f
,
931 const struct usb_ctrlrequest
*ctrl
)
933 struct printer_dev
*dev
= func_to_printer(f
);
934 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
935 struct usb_request
*req
= cdev
->req
;
936 int value
= -EOPNOTSUPP
;
937 u16 wIndex
= le16_to_cpu(ctrl
->wIndex
);
938 u16 wValue
= le16_to_cpu(ctrl
->wValue
);
939 u16 wLength
= le16_to_cpu(ctrl
->wLength
);
941 DBG(dev
, "ctrl req%02x.%02x v%04x i%04x l%d\n",
942 ctrl
->bRequestType
, ctrl
->bRequest
, wValue
, wIndex
, wLength
);
944 switch (ctrl
->bRequestType
&USB_TYPE_MASK
) {
946 switch (ctrl
->bRequest
) {
947 case GET_DEVICE_ID
: /* Get the IEEE-1284 PNP String */
948 /* Only one printer interface is supported. */
949 if ((wIndex
>>8) != dev
->interface
)
952 value
= (dev
->pnp_string
[0] << 8) | dev
->pnp_string
[1];
953 memcpy(req
->buf
, dev
->pnp_string
, value
);
954 DBG(dev
, "1284 PNP String: %x %s\n", value
,
955 &dev
->pnp_string
[2]);
958 case GET_PORT_STATUS
: /* Get Port Status */
959 /* Only one printer interface is supported. */
960 if (wIndex
!= dev
->interface
)
963 *(u8
*)req
->buf
= dev
->printer_status
;
964 value
= min_t(u16
, wLength
, 1);
967 case SOFT_RESET
: /* Soft Reset */
968 /* Only one printer interface is supported. */
969 if (wIndex
!= dev
->interface
)
972 printer_soft_reset(dev
);
985 "unknown ctrl req%02x.%02x v%04x i%04x l%d\n",
986 ctrl
->bRequestType
, ctrl
->bRequest
,
987 wValue
, wIndex
, wLength
);
990 /* host either stalls (value < 0) or reports success */
993 req
->zero
= value
< wLength
;
994 value
= usb_ep_queue(cdev
->gadget
->ep0
, req
, GFP_ATOMIC
);
996 ERROR(dev
, "%s:%d Error!\n", __func__
, __LINE__
);
1003 static int printer_func_bind(struct usb_configuration
*c
,
1004 struct usb_function
*f
)
1006 struct usb_gadget
*gadget
= c
->cdev
->gadget
;
1007 struct printer_dev
*dev
= func_to_printer(f
);
1008 struct device
*pdev
;
1009 struct usb_composite_dev
*cdev
= c
->cdev
;
1010 struct usb_ep
*in_ep
;
1011 struct usb_ep
*out_ep
= NULL
;
1012 struct usb_request
*req
;
1018 id
= usb_interface_id(c
, f
);
1021 intf_desc
.bInterfaceNumber
= id
;
1023 /* finish hookup to lower layer ... */
1024 dev
->gadget
= gadget
;
1026 /* all we really need is bulk IN/OUT */
1027 in_ep
= usb_ep_autoconfig(cdev
->gadget
, &fs_ep_in_desc
);
1030 dev_err(&cdev
->gadget
->dev
, "can't autoconfigure on %s\n",
1031 cdev
->gadget
->name
);
1035 out_ep
= usb_ep_autoconfig(cdev
->gadget
, &fs_ep_out_desc
);
1039 /* assumes that all endpoints are dual-speed */
1040 hs_ep_in_desc
.bEndpointAddress
= fs_ep_in_desc
.bEndpointAddress
;
1041 hs_ep_out_desc
.bEndpointAddress
= fs_ep_out_desc
.bEndpointAddress
;
1042 ss_ep_in_desc
.bEndpointAddress
= fs_ep_in_desc
.bEndpointAddress
;
1043 ss_ep_out_desc
.bEndpointAddress
= fs_ep_out_desc
.bEndpointAddress
;
1045 ret
= usb_assign_descriptors(f
, fs_printer_function
,
1046 hs_printer_function
, ss_printer_function
, NULL
);
1051 dev
->out_ep
= out_ep
;
1054 for (i
= 0; i
< dev
->q_len
; i
++) {
1055 req
= printer_req_alloc(dev
->in_ep
, USB_BUFSIZE
, GFP_KERNEL
);
1058 list_add(&req
->list
, &dev
->tx_reqs
);
1061 for (i
= 0; i
< dev
->q_len
; i
++) {
1062 req
= printer_req_alloc(dev
->out_ep
, USB_BUFSIZE
, GFP_KERNEL
);
1065 list_add(&req
->list
, &dev
->rx_reqs
);
1068 /* Setup the sysfs files for the printer gadget. */
1069 devt
= MKDEV(major
, dev
->minor
);
1070 pdev
= device_create(usb_gadget_class
, NULL
, devt
,
1071 NULL
, "g_printer%d", dev
->minor
);
1073 ERROR(dev
, "Failed to create device: g_printer\n");
1074 ret
= PTR_ERR(pdev
);
1079 * Register a character device as an interface to a user mode
1080 * program that handles the printer specific functionality.
1082 cdev_init(&dev
->printer_cdev
, &printer_io_operations
);
1083 dev
->printer_cdev
.owner
= THIS_MODULE
;
1084 ret
= cdev_add(&dev
->printer_cdev
, devt
, 1);
1086 ERROR(dev
, "Failed to open char device\n");
1093 device_destroy(usb_gadget_class
, devt
);
1096 while (!list_empty(&dev
->rx_reqs
)) {
1097 req
= container_of(dev
->rx_reqs
.next
, struct usb_request
, list
);
1098 list_del(&req
->list
);
1099 printer_req_free(dev
->out_ep
, req
);
1103 while (!list_empty(&dev
->tx_reqs
)) {
1104 req
= container_of(dev
->tx_reqs
.next
, struct usb_request
, list
);
1105 list_del(&req
->list
);
1106 printer_req_free(dev
->in_ep
, req
);
1113 static int printer_func_set_alt(struct usb_function
*f
,
1114 unsigned intf
, unsigned alt
)
1116 struct printer_dev
*dev
= func_to_printer(f
);
1117 int ret
= -ENOTSUPP
;
1120 ret
= set_interface(dev
, intf
);
1125 static void printer_func_disable(struct usb_function
*f
)
1127 struct printer_dev
*dev
= func_to_printer(f
);
1129 DBG(dev
, "%s\n", __func__
);
1131 printer_reset_interface(dev
);
1134 static inline struct f_printer_opts
1135 *to_f_printer_opts(struct config_item
*item
)
1137 return container_of(to_config_group(item
), struct f_printer_opts
,
1141 static void printer_attr_release(struct config_item
*item
)
1143 struct f_printer_opts
*opts
= to_f_printer_opts(item
);
1145 usb_put_function_instance(&opts
->func_inst
);
1148 static struct configfs_item_operations printer_item_ops
= {
1149 .release
= printer_attr_release
,
1152 static ssize_t
f_printer_opts_pnp_string_show(struct config_item
*item
,
1155 struct f_printer_opts
*opts
= to_f_printer_opts(item
);
1158 mutex_lock(&opts
->lock
);
1159 result
= strlcpy(page
, opts
->pnp_string
+ 2, PNP_STRING_LEN
- 2);
1160 mutex_unlock(&opts
->lock
);
1165 static ssize_t
f_printer_opts_pnp_string_store(struct config_item
*item
,
1166 const char *page
, size_t len
)
1168 struct f_printer_opts
*opts
= to_f_printer_opts(item
);
1171 mutex_lock(&opts
->lock
);
1172 result
= strlcpy(opts
->pnp_string
+ 2, page
, PNP_STRING_LEN
- 2);
1173 l
= strlen(opts
->pnp_string
+ 2) + 2;
1174 opts
->pnp_string
[0] = (l
>> 8) & 0xFF;
1175 opts
->pnp_string
[1] = l
& 0xFF;
1176 mutex_unlock(&opts
->lock
);
1181 CONFIGFS_ATTR(f_printer_opts_
, pnp_string
);
1183 static ssize_t
f_printer_opts_q_len_show(struct config_item
*item
,
1186 struct f_printer_opts
*opts
= to_f_printer_opts(item
);
1189 mutex_lock(&opts
->lock
);
1190 result
= sprintf(page
, "%d\n", opts
->q_len
);
1191 mutex_unlock(&opts
->lock
);
1196 static ssize_t
f_printer_opts_q_len_store(struct config_item
*item
,
1197 const char *page
, size_t len
)
1199 struct f_printer_opts
*opts
= to_f_printer_opts(item
);
1203 mutex_lock(&opts
->lock
);
1209 ret
= kstrtou16(page
, 0, &num
);
1213 opts
->q_len
= (unsigned)num
;
1216 mutex_unlock(&opts
->lock
);
1220 CONFIGFS_ATTR(f_printer_opts_
, q_len
);
1222 static struct configfs_attribute
*printer_attrs
[] = {
1223 &f_printer_opts_attr_pnp_string
,
1224 &f_printer_opts_attr_q_len
,
1228 static struct config_item_type printer_func_type
= {
1229 .ct_item_ops
= &printer_item_ops
,
1230 .ct_attrs
= printer_attrs
,
1231 .ct_owner
= THIS_MODULE
,
1234 static inline int gprinter_get_minor(void)
1238 ret
= ida_simple_get(&printer_ida
, 0, 0, GFP_KERNEL
);
1239 if (ret
>= PRINTER_MINORS
) {
1240 ida_simple_remove(&printer_ida
, ret
);
1247 static inline void gprinter_put_minor(int minor
)
1249 ida_simple_remove(&printer_ida
, minor
);
1252 static int gprinter_setup(int);
1253 static void gprinter_cleanup(void);
1255 static void gprinter_free_inst(struct usb_function_instance
*f
)
1257 struct f_printer_opts
*opts
;
1259 opts
= container_of(f
, struct f_printer_opts
, func_inst
);
1261 mutex_lock(&printer_ida_lock
);
1263 gprinter_put_minor(opts
->minor
);
1264 if (idr_is_empty(&printer_ida
.idr
))
1267 mutex_unlock(&printer_ida_lock
);
1272 static struct usb_function_instance
*gprinter_alloc_inst(void)
1274 struct f_printer_opts
*opts
;
1275 struct usb_function_instance
*ret
;
1278 opts
= kzalloc(sizeof(*opts
), GFP_KERNEL
);
1280 return ERR_PTR(-ENOMEM
);
1282 mutex_init(&opts
->lock
);
1283 opts
->func_inst
.free_func_inst
= gprinter_free_inst
;
1284 ret
= &opts
->func_inst
;
1286 mutex_lock(&printer_ida_lock
);
1288 if (idr_is_empty(&printer_ida
.idr
)) {
1289 status
= gprinter_setup(PRINTER_MINORS
);
1291 ret
= ERR_PTR(status
);
1297 opts
->minor
= gprinter_get_minor();
1298 if (opts
->minor
< 0) {
1299 ret
= ERR_PTR(opts
->minor
);
1301 if (idr_is_empty(&printer_ida
.idr
))
1305 config_group_init_type_name(&opts
->func_inst
.group
, "",
1306 &printer_func_type
);
1309 mutex_unlock(&printer_ida_lock
);
1313 static void gprinter_free(struct usb_function
*f
)
1315 struct printer_dev
*dev
= func_to_printer(f
);
1316 struct f_printer_opts
*opts
;
1318 opts
= container_of(f
->fi
, struct f_printer_opts
, func_inst
);
1320 mutex_lock(&opts
->lock
);
1322 mutex_unlock(&opts
->lock
);
1325 static void printer_func_unbind(struct usb_configuration
*c
,
1326 struct usb_function
*f
)
1328 struct printer_dev
*dev
;
1329 struct usb_request
*req
;
1331 dev
= func_to_printer(f
);
1333 device_destroy(usb_gadget_class
, MKDEV(major
, dev
->minor
));
1335 /* Remove Character Device */
1336 cdev_del(&dev
->printer_cdev
);
1338 /* we must already have been disconnected ... no i/o may be active */
1339 WARN_ON(!list_empty(&dev
->tx_reqs_active
));
1340 WARN_ON(!list_empty(&dev
->rx_reqs_active
));
1342 /* Free all memory for this driver. */
1343 while (!list_empty(&dev
->tx_reqs
)) {
1344 req
= container_of(dev
->tx_reqs
.next
, struct usb_request
,
1346 list_del(&req
->list
);
1347 printer_req_free(dev
->in_ep
, req
);
1350 if (dev
->current_rx_req
!= NULL
)
1351 printer_req_free(dev
->out_ep
, dev
->current_rx_req
);
1353 while (!list_empty(&dev
->rx_reqs
)) {
1354 req
= container_of(dev
->rx_reqs
.next
,
1355 struct usb_request
, list
);
1356 list_del(&req
->list
);
1357 printer_req_free(dev
->out_ep
, req
);
1360 while (!list_empty(&dev
->rx_buffers
)) {
1361 req
= container_of(dev
->rx_buffers
.next
,
1362 struct usb_request
, list
);
1363 list_del(&req
->list
);
1364 printer_req_free(dev
->out_ep
, req
);
1366 usb_free_all_descriptors(f
);
1369 static struct usb_function
*gprinter_alloc(struct usb_function_instance
*fi
)
1371 struct printer_dev
*dev
;
1372 struct f_printer_opts
*opts
;
1374 opts
= container_of(fi
, struct f_printer_opts
, func_inst
);
1376 mutex_lock(&opts
->lock
);
1377 if (opts
->minor
>= minors
) {
1378 mutex_unlock(&opts
->lock
);
1379 return ERR_PTR(-ENOENT
);
1382 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
1384 mutex_unlock(&opts
->lock
);
1385 return ERR_PTR(-ENOMEM
);
1389 dev
->minor
= opts
->minor
;
1390 dev
->pnp_string
= opts
->pnp_string
;
1391 dev
->q_len
= opts
->q_len
;
1392 mutex_unlock(&opts
->lock
);
1394 dev
->function
.name
= "printer";
1395 dev
->function
.bind
= printer_func_bind
;
1396 dev
->function
.setup
= printer_func_setup
;
1397 dev
->function
.unbind
= printer_func_unbind
;
1398 dev
->function
.set_alt
= printer_func_set_alt
;
1399 dev
->function
.disable
= printer_func_disable
;
1400 dev
->function
.req_match
= gprinter_req_match
;
1401 dev
->function
.free_func
= gprinter_free
;
1403 INIT_LIST_HEAD(&dev
->tx_reqs
);
1404 INIT_LIST_HEAD(&dev
->rx_reqs
);
1405 INIT_LIST_HEAD(&dev
->rx_buffers
);
1406 INIT_LIST_HEAD(&dev
->tx_reqs_active
);
1407 INIT_LIST_HEAD(&dev
->rx_reqs_active
);
1409 spin_lock_init(&dev
->lock
);
1410 mutex_init(&dev
->lock_printer_io
);
1411 init_waitqueue_head(&dev
->rx_wait
);
1412 init_waitqueue_head(&dev
->tx_wait
);
1413 init_waitqueue_head(&dev
->tx_flush_wait
);
1415 dev
->interface
= -1;
1416 dev
->printer_cdev_open
= 0;
1417 dev
->printer_status
= PRINTER_NOT_ERROR
;
1418 dev
->current_rx_req
= NULL
;
1419 dev
->current_rx_bytes
= 0;
1420 dev
->current_rx_buf
= NULL
;
1422 return &dev
->function
;
1425 DECLARE_USB_FUNCTION_INIT(printer
, gprinter_alloc_inst
, gprinter_alloc
);
1426 MODULE_LICENSE("GPL");
1427 MODULE_AUTHOR("Craig Nadler");
1429 static int gprinter_setup(int count
)
1434 usb_gadget_class
= class_create(THIS_MODULE
, "usb_printer_gadget");
1435 if (IS_ERR(usb_gadget_class
)) {
1436 status
= PTR_ERR(usb_gadget_class
);
1437 usb_gadget_class
= NULL
;
1438 pr_err("unable to create usb_gadget class %d\n", status
);
1442 status
= alloc_chrdev_region(&devt
, 0, count
, "USB printer gadget");
1444 pr_err("alloc_chrdev_region %d\n", status
);
1445 class_destroy(usb_gadget_class
);
1446 usb_gadget_class
= NULL
;
1450 major
= MAJOR(devt
);
1456 static void gprinter_cleanup(void)
1459 unregister_chrdev_region(MKDEV(major
, 0), minors
);
1462 class_destroy(usb_gadget_class
);
1463 usb_gadget_class
= NULL
;