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_qualifier_descriptor dev_qualifier
= {
165 .bLength
= sizeof(dev_qualifier
),
166 .bDescriptorType
= USB_DT_DEVICE_QUALIFIER
,
167 .bcdUSB
= cpu_to_le16(0x0200),
168 .bDeviceClass
= USB_CLASS_PRINTER
,
169 .bNumConfigurations
= 1
172 static struct usb_descriptor_header
*hs_printer_function
[] = {
173 (struct usb_descriptor_header
*) &intf_desc
,
174 (struct usb_descriptor_header
*) &hs_ep_in_desc
,
175 (struct usb_descriptor_header
*) &hs_ep_out_desc
,
180 * Added endpoint descriptors for 3.0 devices
183 static struct usb_endpoint_descriptor ss_ep_in_desc
= {
184 .bLength
= USB_DT_ENDPOINT_SIZE
,
185 .bDescriptorType
= USB_DT_ENDPOINT
,
186 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
187 .wMaxPacketSize
= cpu_to_le16(1024),
190 static struct usb_ss_ep_comp_descriptor ss_ep_in_comp_desc
= {
191 .bLength
= sizeof(ss_ep_in_comp_desc
),
192 .bDescriptorType
= USB_DT_SS_ENDPOINT_COMP
,
195 static struct usb_endpoint_descriptor ss_ep_out_desc
= {
196 .bLength
= USB_DT_ENDPOINT_SIZE
,
197 .bDescriptorType
= USB_DT_ENDPOINT
,
198 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
199 .wMaxPacketSize
= cpu_to_le16(1024),
202 static struct usb_ss_ep_comp_descriptor ss_ep_out_comp_desc
= {
203 .bLength
= sizeof(ss_ep_out_comp_desc
),
204 .bDescriptorType
= USB_DT_SS_ENDPOINT_COMP
,
207 static struct usb_descriptor_header
*ss_printer_function
[] = {
208 (struct usb_descriptor_header
*) &intf_desc
,
209 (struct usb_descriptor_header
*) &ss_ep_in_desc
,
210 (struct usb_descriptor_header
*) &ss_ep_in_comp_desc
,
211 (struct usb_descriptor_header
*) &ss_ep_out_desc
,
212 (struct usb_descriptor_header
*) &ss_ep_out_comp_desc
,
216 /* maxpacket and other transfer characteristics vary by speed. */
217 static inline struct usb_endpoint_descriptor
*ep_desc(struct usb_gadget
*gadget
,
218 struct usb_endpoint_descriptor
*fs
,
219 struct usb_endpoint_descriptor
*hs
,
220 struct usb_endpoint_descriptor
*ss
)
222 switch (gadget
->speed
) {
223 case USB_SPEED_SUPER
:
232 /*-------------------------------------------------------------------------*/
234 static struct usb_request
*
235 printer_req_alloc(struct usb_ep
*ep
, unsigned len
, gfp_t gfp_flags
)
237 struct usb_request
*req
;
239 req
= usb_ep_alloc_request(ep
, gfp_flags
);
243 req
->buf
= kmalloc(len
, gfp_flags
);
244 if (req
->buf
== NULL
) {
245 usb_ep_free_request(ep
, req
);
254 printer_req_free(struct usb_ep
*ep
, struct usb_request
*req
)
256 if (ep
!= NULL
&& req
!= NULL
) {
258 usb_ep_free_request(ep
, req
);
262 /*-------------------------------------------------------------------------*/
264 static void rx_complete(struct usb_ep
*ep
, struct usb_request
*req
)
266 struct printer_dev
*dev
= ep
->driver_data
;
267 int status
= req
->status
;
270 spin_lock_irqsave(&dev
->lock
, flags
);
272 list_del_init(&req
->list
); /* Remode from Active List */
276 /* normal completion */
278 if (req
->actual
> 0) {
279 list_add_tail(&req
->list
, &dev
->rx_buffers
);
280 DBG(dev
, "G_Printer : rx length %d\n", req
->actual
);
282 list_add(&req
->list
, &dev
->rx_reqs
);
286 /* software-driven interface shutdown */
287 case -ECONNRESET
: /* unlink */
288 case -ESHUTDOWN
: /* disconnect etc */
289 VDBG(dev
, "rx shutdown, code %d\n", status
);
290 list_add(&req
->list
, &dev
->rx_reqs
);
293 /* for hardware automagic (such as pxa) */
294 case -ECONNABORTED
: /* endpoint reset */
295 DBG(dev
, "rx %s reset\n", ep
->name
);
296 list_add(&req
->list
, &dev
->rx_reqs
);
304 DBG(dev
, "rx status %d\n", status
);
305 list_add(&req
->list
, &dev
->rx_reqs
);
309 wake_up_interruptible(&dev
->rx_wait
);
310 spin_unlock_irqrestore(&dev
->lock
, flags
);
313 static void tx_complete(struct usb_ep
*ep
, struct usb_request
*req
)
315 struct printer_dev
*dev
= ep
->driver_data
;
317 switch (req
->status
) {
319 VDBG(dev
, "tx err %d\n", req
->status
);
321 case -ECONNRESET
: /* unlink */
322 case -ESHUTDOWN
: /* disconnect etc */
328 spin_lock(&dev
->lock
);
329 /* Take the request struct off the active list and put it on the
332 list_del_init(&req
->list
);
333 list_add(&req
->list
, &dev
->tx_reqs
);
334 wake_up_interruptible(&dev
->tx_wait
);
335 if (likely(list_empty(&dev
->tx_reqs_active
)))
336 wake_up_interruptible(&dev
->tx_flush_wait
);
338 spin_unlock(&dev
->lock
);
341 /*-------------------------------------------------------------------------*/
344 printer_open(struct inode
*inode
, struct file
*fd
)
346 struct printer_dev
*dev
;
350 dev
= container_of(inode
->i_cdev
, struct printer_dev
, printer_cdev
);
352 spin_lock_irqsave(&dev
->lock
, flags
);
354 if (!dev
->printer_cdev_open
) {
355 dev
->printer_cdev_open
= 1;
356 fd
->private_data
= dev
;
358 /* Change the printer status to show that it's on-line. */
359 dev
->printer_status
|= PRINTER_SELECTED
;
362 spin_unlock_irqrestore(&dev
->lock
, flags
);
364 DBG(dev
, "printer_open returned %x\n", ret
);
369 printer_close(struct inode
*inode
, struct file
*fd
)
371 struct printer_dev
*dev
= fd
->private_data
;
374 spin_lock_irqsave(&dev
->lock
, flags
);
375 dev
->printer_cdev_open
= 0;
376 fd
->private_data
= NULL
;
377 /* Change printer status to show that the printer is off-line. */
378 dev
->printer_status
&= ~PRINTER_SELECTED
;
379 spin_unlock_irqrestore(&dev
->lock
, flags
);
381 DBG(dev
, "printer_close\n");
386 /* This function must be called with interrupts turned off. */
388 setup_rx_reqs(struct printer_dev
*dev
)
390 struct usb_request
*req
;
392 while (likely(!list_empty(&dev
->rx_reqs
))) {
395 req
= container_of(dev
->rx_reqs
.next
,
396 struct usb_request
, list
);
397 list_del_init(&req
->list
);
399 /* The USB Host sends us whatever amount of data it wants to
400 * so we always set the length field to the full USB_BUFSIZE.
401 * If the amount of data is more than the read() caller asked
402 * for it will be stored in the request buffer until it is
403 * asked for by read().
405 req
->length
= USB_BUFSIZE
;
406 req
->complete
= rx_complete
;
408 /* here, we unlock, and only unlock, to avoid deadlock. */
409 spin_unlock(&dev
->lock
);
410 error
= usb_ep_queue(dev
->out_ep
, req
, GFP_ATOMIC
);
411 spin_lock(&dev
->lock
);
413 DBG(dev
, "rx submit --> %d\n", error
);
414 list_add(&req
->list
, &dev
->rx_reqs
);
417 /* if the req is empty, then add it into dev->rx_reqs_active. */
418 else if (list_empty(&req
->list
))
419 list_add(&req
->list
, &dev
->rx_reqs_active
);
424 printer_read(struct file
*fd
, char __user
*buf
, size_t len
, loff_t
*ptr
)
426 struct printer_dev
*dev
= fd
->private_data
;
430 struct usb_request
*req
;
431 /* This is a pointer to the current USB rx request. */
432 struct usb_request
*current_rx_req
;
433 /* This is the number of bytes in the current rx buffer. */
434 size_t current_rx_bytes
;
435 /* This is a pointer to the current rx buffer. */
441 DBG(dev
, "printer_read trying to read %d bytes\n", (int)len
);
443 mutex_lock(&dev
->lock_printer_io
);
444 spin_lock_irqsave(&dev
->lock
, flags
);
446 /* We will use this flag later to check if a printer reset happened
447 * after we turn interrupts back on.
449 dev
->reset_printer
= 0;
454 current_rx_req
= dev
->current_rx_req
;
455 current_rx_bytes
= dev
->current_rx_bytes
;
456 current_rx_buf
= dev
->current_rx_buf
;
457 dev
->current_rx_req
= NULL
;
458 dev
->current_rx_bytes
= 0;
459 dev
->current_rx_buf
= NULL
;
461 /* Check if there is any data in the read buffers. Please note that
462 * current_rx_bytes is the number of bytes in the current rx buffer.
463 * If it is zero then check if there are any other rx_buffers that
464 * are on the completed list. We are only out of data if all rx
467 if ((current_rx_bytes
== 0) &&
468 (likely(list_empty(&dev
->rx_buffers
)))) {
469 /* Turn interrupts back on before sleeping. */
470 spin_unlock_irqrestore(&dev
->lock
, flags
);
473 * If no data is available check if this is a NON-Blocking
476 if (fd
->f_flags
& (O_NONBLOCK
|O_NDELAY
)) {
477 mutex_unlock(&dev
->lock_printer_io
);
481 /* Sleep until data is available */
482 wait_event_interruptible(dev
->rx_wait
,
483 (likely(!list_empty(&dev
->rx_buffers
))));
484 spin_lock_irqsave(&dev
->lock
, flags
);
487 /* We have data to return then copy it to the caller's buffer.*/
488 while ((current_rx_bytes
|| likely(!list_empty(&dev
->rx_buffers
)))
490 if (current_rx_bytes
== 0) {
491 req
= container_of(dev
->rx_buffers
.next
,
492 struct usb_request
, list
);
493 list_del_init(&req
->list
);
495 if (req
->actual
&& req
->buf
) {
496 current_rx_req
= req
;
497 current_rx_bytes
= req
->actual
;
498 current_rx_buf
= req
->buf
;
500 list_add(&req
->list
, &dev
->rx_reqs
);
505 /* Don't leave irqs off while doing memory copies */
506 spin_unlock_irqrestore(&dev
->lock
, flags
);
508 if (len
> current_rx_bytes
)
509 size
= current_rx_bytes
;
513 size
-= copy_to_user(buf
, current_rx_buf
, size
);
514 bytes_copied
+= size
;
518 spin_lock_irqsave(&dev
->lock
, flags
);
520 /* We've disconnected or reset so return. */
521 if (dev
->reset_printer
) {
522 list_add(¤t_rx_req
->list
, &dev
->rx_reqs
);
523 spin_unlock_irqrestore(&dev
->lock
, flags
);
524 mutex_unlock(&dev
->lock_printer_io
);
528 /* If we not returning all the data left in this RX request
529 * buffer then adjust the amount of data left in the buffer.
530 * Othewise if we are done with this RX request buffer then
531 * requeue it to get any incoming data from the USB host.
533 if (size
< current_rx_bytes
) {
534 current_rx_bytes
-= size
;
535 current_rx_buf
+= size
;
537 list_add(¤t_rx_req
->list
, &dev
->rx_reqs
);
538 current_rx_bytes
= 0;
539 current_rx_buf
= NULL
;
540 current_rx_req
= NULL
;
544 dev
->current_rx_req
= current_rx_req
;
545 dev
->current_rx_bytes
= current_rx_bytes
;
546 dev
->current_rx_buf
= current_rx_buf
;
548 spin_unlock_irqrestore(&dev
->lock
, flags
);
549 mutex_unlock(&dev
->lock_printer_io
);
551 DBG(dev
, "printer_read returned %d bytes\n", (int)bytes_copied
);
560 printer_write(struct file
*fd
, const char __user
*buf
, size_t len
, loff_t
*ptr
)
562 struct printer_dev
*dev
= fd
->private_data
;
564 size_t size
; /* Amount of data in a TX request. */
565 size_t bytes_copied
= 0;
566 struct usb_request
*req
;
568 DBG(dev
, "printer_write trying to send %d bytes\n", (int)len
);
573 mutex_lock(&dev
->lock_printer_io
);
574 spin_lock_irqsave(&dev
->lock
, flags
);
576 /* Check if a printer reset happens while we have interrupts on */
577 dev
->reset_printer
= 0;
579 /* Check if there is any available write buffers */
580 if (likely(list_empty(&dev
->tx_reqs
))) {
581 /* Turn interrupts back on before sleeping. */
582 spin_unlock_irqrestore(&dev
->lock
, flags
);
585 * If write buffers are available check if this is
586 * a NON-Blocking call or not.
588 if (fd
->f_flags
& (O_NONBLOCK
|O_NDELAY
)) {
589 mutex_unlock(&dev
->lock_printer_io
);
593 /* Sleep until a write buffer is available */
594 wait_event_interruptible(dev
->tx_wait
,
595 (likely(!list_empty(&dev
->tx_reqs
))));
596 spin_lock_irqsave(&dev
->lock
, flags
);
599 while (likely(!list_empty(&dev
->tx_reqs
)) && len
) {
601 if (len
> USB_BUFSIZE
)
606 req
= container_of(dev
->tx_reqs
.next
, struct usb_request
,
608 list_del_init(&req
->list
);
610 req
->complete
= tx_complete
;
613 /* Check if we need to send a zero length packet. */
615 /* They will be more TX requests so no yet. */
618 /* If the data amount is not a multiple of the
619 * maxpacket size then send a zero length packet.
621 req
->zero
= ((len
% dev
->in_ep
->maxpacket
) == 0);
623 /* Don't leave irqs off while doing memory copies */
624 spin_unlock_irqrestore(&dev
->lock
, flags
);
626 if (copy_from_user(req
->buf
, buf
, size
)) {
627 list_add(&req
->list
, &dev
->tx_reqs
);
628 mutex_unlock(&dev
->lock_printer_io
);
632 bytes_copied
+= size
;
636 spin_lock_irqsave(&dev
->lock
, flags
);
638 /* We've disconnected or reset so free the req and buffer */
639 if (dev
->reset_printer
) {
640 list_add(&req
->list
, &dev
->tx_reqs
);
641 spin_unlock_irqrestore(&dev
->lock
, flags
);
642 mutex_unlock(&dev
->lock_printer_io
);
646 if (usb_ep_queue(dev
->in_ep
, req
, GFP_ATOMIC
)) {
647 list_add(&req
->list
, &dev
->tx_reqs
);
648 spin_unlock_irqrestore(&dev
->lock
, flags
);
649 mutex_unlock(&dev
->lock_printer_io
);
653 list_add(&req
->list
, &dev
->tx_reqs_active
);
657 spin_unlock_irqrestore(&dev
->lock
, flags
);
658 mutex_unlock(&dev
->lock_printer_io
);
660 DBG(dev
, "printer_write sent %d bytes\n", (int)bytes_copied
);
669 printer_fsync(struct file
*fd
, loff_t start
, loff_t end
, int datasync
)
671 struct printer_dev
*dev
= fd
->private_data
;
672 struct inode
*inode
= file_inode(fd
);
676 mutex_lock(&inode
->i_mutex
);
677 spin_lock_irqsave(&dev
->lock
, flags
);
678 tx_list_empty
= (likely(list_empty(&dev
->tx_reqs
)));
679 spin_unlock_irqrestore(&dev
->lock
, flags
);
681 if (!tx_list_empty
) {
682 /* Sleep until all data has been sent */
683 wait_event_interruptible(dev
->tx_flush_wait
,
684 (likely(list_empty(&dev
->tx_reqs_active
))));
686 mutex_unlock(&inode
->i_mutex
);
692 printer_poll(struct file
*fd
, poll_table
*wait
)
694 struct printer_dev
*dev
= fd
->private_data
;
698 mutex_lock(&dev
->lock_printer_io
);
699 spin_lock_irqsave(&dev
->lock
, flags
);
701 spin_unlock_irqrestore(&dev
->lock
, flags
);
702 mutex_unlock(&dev
->lock_printer_io
);
704 poll_wait(fd
, &dev
->rx_wait
, wait
);
705 poll_wait(fd
, &dev
->tx_wait
, wait
);
707 spin_lock_irqsave(&dev
->lock
, flags
);
708 if (likely(!list_empty(&dev
->tx_reqs
)))
709 status
|= POLLOUT
| POLLWRNORM
;
711 if (likely(dev
->current_rx_bytes
) ||
712 likely(!list_empty(&dev
->rx_buffers
)))
713 status
|= POLLIN
| POLLRDNORM
;
715 spin_unlock_irqrestore(&dev
->lock
, flags
);
721 printer_ioctl(struct file
*fd
, unsigned int code
, unsigned long arg
)
723 struct printer_dev
*dev
= fd
->private_data
;
727 DBG(dev
, "printer_ioctl: cmd=0x%4.4x, arg=%lu\n", code
, arg
);
731 spin_lock_irqsave(&dev
->lock
, flags
);
734 case GADGET_GET_PRINTER_STATUS
:
735 status
= (int)dev
->printer_status
;
737 case GADGET_SET_PRINTER_STATUS
:
738 dev
->printer_status
= (u8
)arg
;
741 /* could not handle ioctl */
742 DBG(dev
, "printer_ioctl: ERROR cmd=0x%4.4xis not supported\n",
747 spin_unlock_irqrestore(&dev
->lock
, flags
);
752 /* used after endpoint configuration */
753 static const struct file_operations printer_io_operations
= {
754 .owner
= THIS_MODULE
,
755 .open
= printer_open
,
756 .read
= printer_read
,
757 .write
= printer_write
,
758 .fsync
= printer_fsync
,
759 .poll
= printer_poll
,
760 .unlocked_ioctl
= printer_ioctl
,
761 .release
= printer_close
,
762 .llseek
= noop_llseek
,
765 /*-------------------------------------------------------------------------*/
768 set_printer_interface(struct printer_dev
*dev
)
772 dev
->in_ep
->desc
= ep_desc(dev
->gadget
, &fs_ep_in_desc
, &hs_ep_in_desc
,
774 dev
->in_ep
->driver_data
= dev
;
776 dev
->out_ep
->desc
= ep_desc(dev
->gadget
, &fs_ep_out_desc
,
777 &hs_ep_out_desc
, &ss_ep_out_desc
);
778 dev
->out_ep
->driver_data
= dev
;
780 result
= usb_ep_enable(dev
->in_ep
);
782 DBG(dev
, "enable %s --> %d\n", dev
->in_ep
->name
, result
);
786 result
= usb_ep_enable(dev
->out_ep
);
788 DBG(dev
, "enable %s --> %d\n", dev
->in_ep
->name
, result
);
793 /* on error, disable any endpoints */
795 (void) usb_ep_disable(dev
->in_ep
);
796 (void) usb_ep_disable(dev
->out_ep
);
797 dev
->in_ep
->desc
= NULL
;
798 dev
->out_ep
->desc
= NULL
;
801 /* caller is responsible for cleanup on error */
805 static void printer_reset_interface(struct printer_dev
*dev
)
809 if (dev
->interface
< 0)
812 DBG(dev
, "%s\n", __func__
);
814 if (dev
->in_ep
->desc
)
815 usb_ep_disable(dev
->in_ep
);
817 if (dev
->out_ep
->desc
)
818 usb_ep_disable(dev
->out_ep
);
820 spin_lock_irqsave(&dev
->lock
, flags
);
821 dev
->in_ep
->desc
= NULL
;
822 dev
->out_ep
->desc
= NULL
;
824 spin_unlock_irqrestore(&dev
->lock
, flags
);
827 /* Change our operational Interface. */
828 static int set_interface(struct printer_dev
*dev
, unsigned number
)
832 /* Free the current interface */
833 printer_reset_interface(dev
);
835 result
= set_printer_interface(dev
);
837 printer_reset_interface(dev
);
839 dev
->interface
= number
;
842 INFO(dev
, "Using interface %x\n", number
);
847 static void printer_soft_reset(struct printer_dev
*dev
)
849 struct usb_request
*req
;
851 INFO(dev
, "Received Printer Reset Request\n");
853 if (usb_ep_disable(dev
->in_ep
))
854 DBG(dev
, "Failed to disable USB in_ep\n");
855 if (usb_ep_disable(dev
->out_ep
))
856 DBG(dev
, "Failed to disable USB out_ep\n");
858 if (dev
->current_rx_req
!= NULL
) {
859 list_add(&dev
->current_rx_req
->list
, &dev
->rx_reqs
);
860 dev
->current_rx_req
= NULL
;
862 dev
->current_rx_bytes
= 0;
863 dev
->current_rx_buf
= NULL
;
864 dev
->reset_printer
= 1;
866 while (likely(!(list_empty(&dev
->rx_buffers
)))) {
867 req
= container_of(dev
->rx_buffers
.next
, struct usb_request
,
869 list_del_init(&req
->list
);
870 list_add(&req
->list
, &dev
->rx_reqs
);
873 while (likely(!(list_empty(&dev
->rx_reqs_active
)))) {
874 req
= container_of(dev
->rx_buffers
.next
, struct usb_request
,
876 list_del_init(&req
->list
);
877 list_add(&req
->list
, &dev
->rx_reqs
);
880 while (likely(!(list_empty(&dev
->tx_reqs_active
)))) {
881 req
= container_of(dev
->tx_reqs_active
.next
,
882 struct usb_request
, list
);
883 list_del_init(&req
->list
);
884 list_add(&req
->list
, &dev
->tx_reqs
);
887 if (usb_ep_enable(dev
->in_ep
))
888 DBG(dev
, "Failed to enable USB in_ep\n");
889 if (usb_ep_enable(dev
->out_ep
))
890 DBG(dev
, "Failed to enable USB out_ep\n");
892 wake_up_interruptible(&dev
->rx_wait
);
893 wake_up_interruptible(&dev
->tx_wait
);
894 wake_up_interruptible(&dev
->tx_flush_wait
);
897 /*-------------------------------------------------------------------------*/
899 static bool gprinter_req_match(struct usb_function
*f
,
900 const struct usb_ctrlrequest
*ctrl
)
902 struct printer_dev
*dev
= func_to_printer(f
);
903 u16 w_index
= le16_to_cpu(ctrl
->wIndex
);
904 u16 w_value
= le16_to_cpu(ctrl
->wValue
);
905 u16 w_length
= le16_to_cpu(ctrl
->wLength
);
907 if ((ctrl
->bRequestType
& USB_RECIP_MASK
) != USB_RECIP_INTERFACE
||
908 (ctrl
->bRequestType
& USB_TYPE_MASK
) != USB_TYPE_CLASS
)
911 switch (ctrl
->bRequest
) {
914 if (w_length
<= PNP_STRING_LEN
&&
915 (USB_DIR_IN
& ctrl
->bRequestType
))
918 case GET_PORT_STATUS
:
919 if (!w_value
&& w_length
== 1 &&
920 (USB_DIR_IN
& ctrl
->bRequestType
))
924 if (!w_value
&& !w_length
&&
925 !(USB_DIR_IN
& ctrl
->bRequestType
))
931 return w_index
== dev
->interface
;
935 * The setup() callback implements all the ep0 functionality that's not
936 * handled lower down.
938 static int printer_func_setup(struct usb_function
*f
,
939 const struct usb_ctrlrequest
*ctrl
)
941 struct printer_dev
*dev
= func_to_printer(f
);
942 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
943 struct usb_request
*req
= cdev
->req
;
944 int value
= -EOPNOTSUPP
;
945 u16 wIndex
= le16_to_cpu(ctrl
->wIndex
);
946 u16 wValue
= le16_to_cpu(ctrl
->wValue
);
947 u16 wLength
= le16_to_cpu(ctrl
->wLength
);
949 DBG(dev
, "ctrl req%02x.%02x v%04x i%04x l%d\n",
950 ctrl
->bRequestType
, ctrl
->bRequest
, wValue
, wIndex
, wLength
);
952 switch (ctrl
->bRequestType
&USB_TYPE_MASK
) {
954 switch (ctrl
->bRequest
) {
955 case GET_DEVICE_ID
: /* Get the IEEE-1284 PNP String */
956 /* Only one printer interface is supported. */
957 if ((wIndex
>>8) != dev
->interface
)
960 value
= (dev
->pnp_string
[0] << 8) | dev
->pnp_string
[1];
961 memcpy(req
->buf
, dev
->pnp_string
, value
);
962 DBG(dev
, "1284 PNP String: %x %s\n", value
,
963 &dev
->pnp_string
[2]);
966 case GET_PORT_STATUS
: /* Get Port Status */
967 /* Only one printer interface is supported. */
968 if (wIndex
!= dev
->interface
)
971 *(u8
*)req
->buf
= dev
->printer_status
;
972 value
= min_t(u16
, wLength
, 1);
975 case SOFT_RESET
: /* Soft Reset */
976 /* Only one printer interface is supported. */
977 if (wIndex
!= dev
->interface
)
980 printer_soft_reset(dev
);
993 "unknown ctrl req%02x.%02x v%04x i%04x l%d\n",
994 ctrl
->bRequestType
, ctrl
->bRequest
,
995 wValue
, wIndex
, wLength
);
998 /* host either stalls (value < 0) or reports success */
1000 req
->length
= value
;
1001 req
->zero
= value
< wLength
;
1002 value
= usb_ep_queue(cdev
->gadget
->ep0
, req
, GFP_ATOMIC
);
1004 ERROR(dev
, "%s:%d Error!\n", __func__
, __LINE__
);
1011 static int printer_func_bind(struct usb_configuration
*c
,
1012 struct usb_function
*f
)
1014 struct usb_gadget
*gadget
= c
->cdev
->gadget
;
1015 struct printer_dev
*dev
= func_to_printer(f
);
1016 struct device
*pdev
;
1017 struct usb_composite_dev
*cdev
= c
->cdev
;
1018 struct usb_ep
*in_ep
;
1019 struct usb_ep
*out_ep
= NULL
;
1020 struct usb_request
*req
;
1026 id
= usb_interface_id(c
, f
);
1029 intf_desc
.bInterfaceNumber
= id
;
1031 /* finish hookup to lower layer ... */
1032 dev
->gadget
= gadget
;
1034 /* all we really need is bulk IN/OUT */
1035 in_ep
= usb_ep_autoconfig(cdev
->gadget
, &fs_ep_in_desc
);
1038 dev_err(&cdev
->gadget
->dev
, "can't autoconfigure on %s\n",
1039 cdev
->gadget
->name
);
1043 out_ep
= usb_ep_autoconfig(cdev
->gadget
, &fs_ep_out_desc
);
1047 /* assumes that all endpoints are dual-speed */
1048 hs_ep_in_desc
.bEndpointAddress
= fs_ep_in_desc
.bEndpointAddress
;
1049 hs_ep_out_desc
.bEndpointAddress
= fs_ep_out_desc
.bEndpointAddress
;
1050 ss_ep_in_desc
.bEndpointAddress
= fs_ep_in_desc
.bEndpointAddress
;
1051 ss_ep_out_desc
.bEndpointAddress
= fs_ep_out_desc
.bEndpointAddress
;
1053 ret
= usb_assign_descriptors(f
, fs_printer_function
,
1054 hs_printer_function
, ss_printer_function
);
1059 dev
->out_ep
= out_ep
;
1062 for (i
= 0; i
< dev
->q_len
; i
++) {
1063 req
= printer_req_alloc(dev
->in_ep
, USB_BUFSIZE
, GFP_KERNEL
);
1066 list_add(&req
->list
, &dev
->tx_reqs
);
1069 for (i
= 0; i
< dev
->q_len
; i
++) {
1070 req
= printer_req_alloc(dev
->out_ep
, USB_BUFSIZE
, GFP_KERNEL
);
1073 list_add(&req
->list
, &dev
->rx_reqs
);
1076 /* Setup the sysfs files for the printer gadget. */
1077 devt
= MKDEV(major
, dev
->minor
);
1078 pdev
= device_create(usb_gadget_class
, NULL
, devt
,
1079 NULL
, "g_printer%d", dev
->minor
);
1081 ERROR(dev
, "Failed to create device: g_printer\n");
1082 ret
= PTR_ERR(pdev
);
1087 * Register a character device as an interface to a user mode
1088 * program that handles the printer specific functionality.
1090 cdev_init(&dev
->printer_cdev
, &printer_io_operations
);
1091 dev
->printer_cdev
.owner
= THIS_MODULE
;
1092 ret
= cdev_add(&dev
->printer_cdev
, devt
, 1);
1094 ERROR(dev
, "Failed to open char device\n");
1101 device_destroy(usb_gadget_class
, devt
);
1104 while (!list_empty(&dev
->rx_reqs
)) {
1105 req
= container_of(dev
->rx_reqs
.next
, struct usb_request
, list
);
1106 list_del(&req
->list
);
1107 printer_req_free(dev
->out_ep
, req
);
1111 while (!list_empty(&dev
->tx_reqs
)) {
1112 req
= container_of(dev
->tx_reqs
.next
, struct usb_request
, list
);
1113 list_del(&req
->list
);
1114 printer_req_free(dev
->in_ep
, req
);
1121 static int printer_func_set_alt(struct usb_function
*f
,
1122 unsigned intf
, unsigned alt
)
1124 struct printer_dev
*dev
= func_to_printer(f
);
1125 int ret
= -ENOTSUPP
;
1128 ret
= set_interface(dev
, intf
);
1133 static void printer_func_disable(struct usb_function
*f
)
1135 struct printer_dev
*dev
= func_to_printer(f
);
1137 DBG(dev
, "%s\n", __func__
);
1139 printer_reset_interface(dev
);
1142 static inline struct f_printer_opts
1143 *to_f_printer_opts(struct config_item
*item
)
1145 return container_of(to_config_group(item
), struct f_printer_opts
,
1149 static void printer_attr_release(struct config_item
*item
)
1151 struct f_printer_opts
*opts
= to_f_printer_opts(item
);
1153 usb_put_function_instance(&opts
->func_inst
);
1156 static struct configfs_item_operations printer_item_ops
= {
1157 .release
= printer_attr_release
,
1160 static ssize_t
f_printer_opts_pnp_string_show(struct config_item
*item
,
1163 struct f_printer_opts
*opts
= to_f_printer_opts(item
);
1166 mutex_lock(&opts
->lock
);
1167 result
= strlcpy(page
, opts
->pnp_string
+ 2, PNP_STRING_LEN
- 2);
1168 mutex_unlock(&opts
->lock
);
1173 static ssize_t
f_printer_opts_pnp_string_store(struct config_item
*item
,
1174 const char *page
, size_t len
)
1176 struct f_printer_opts
*opts
= to_f_printer_opts(item
);
1179 mutex_lock(&opts
->lock
);
1180 result
= strlcpy(opts
->pnp_string
+ 2, page
, PNP_STRING_LEN
- 2);
1181 l
= strlen(opts
->pnp_string
+ 2) + 2;
1182 opts
->pnp_string
[0] = (l
>> 8) & 0xFF;
1183 opts
->pnp_string
[1] = l
& 0xFF;
1184 mutex_unlock(&opts
->lock
);
1189 CONFIGFS_ATTR(f_printer_opts_
, pnp_string
);
1191 static ssize_t
f_printer_opts_q_len_show(struct config_item
*item
,
1194 struct f_printer_opts
*opts
= to_f_printer_opts(item
);
1197 mutex_lock(&opts
->lock
);
1198 result
= sprintf(page
, "%d\n", opts
->q_len
);
1199 mutex_unlock(&opts
->lock
);
1204 static ssize_t
f_printer_opts_q_len_store(struct config_item
*item
,
1205 const char *page
, size_t len
)
1207 struct f_printer_opts
*opts
= to_f_printer_opts(item
);
1211 mutex_lock(&opts
->lock
);
1217 ret
= kstrtou16(page
, 0, &num
);
1221 opts
->q_len
= (unsigned)num
;
1224 mutex_unlock(&opts
->lock
);
1228 CONFIGFS_ATTR(f_printer_opts_
, q_len
);
1230 static struct configfs_attribute
*printer_attrs
[] = {
1231 &f_printer_opts_attr_pnp_string
,
1232 &f_printer_opts_attr_q_len
,
1236 static struct config_item_type printer_func_type
= {
1237 .ct_item_ops
= &printer_item_ops
,
1238 .ct_attrs
= printer_attrs
,
1239 .ct_owner
= THIS_MODULE
,
1242 static inline int gprinter_get_minor(void)
1246 ret
= ida_simple_get(&printer_ida
, 0, 0, GFP_KERNEL
);
1247 if (ret
>= PRINTER_MINORS
) {
1248 ida_simple_remove(&printer_ida
, ret
);
1255 static inline void gprinter_put_minor(int minor
)
1257 ida_simple_remove(&printer_ida
, minor
);
1260 static int gprinter_setup(int);
1261 static void gprinter_cleanup(void);
1263 static void gprinter_free_inst(struct usb_function_instance
*f
)
1265 struct f_printer_opts
*opts
;
1267 opts
= container_of(f
, struct f_printer_opts
, func_inst
);
1269 mutex_lock(&printer_ida_lock
);
1271 gprinter_put_minor(opts
->minor
);
1272 if (idr_is_empty(&printer_ida
.idr
))
1275 mutex_unlock(&printer_ida_lock
);
1280 static struct usb_function_instance
*gprinter_alloc_inst(void)
1282 struct f_printer_opts
*opts
;
1283 struct usb_function_instance
*ret
;
1286 opts
= kzalloc(sizeof(*opts
), GFP_KERNEL
);
1288 return ERR_PTR(-ENOMEM
);
1290 mutex_init(&opts
->lock
);
1291 opts
->func_inst
.free_func_inst
= gprinter_free_inst
;
1292 ret
= &opts
->func_inst
;
1294 mutex_lock(&printer_ida_lock
);
1296 if (idr_is_empty(&printer_ida
.idr
)) {
1297 status
= gprinter_setup(PRINTER_MINORS
);
1299 ret
= ERR_PTR(status
);
1305 opts
->minor
= gprinter_get_minor();
1306 if (opts
->minor
< 0) {
1307 ret
= ERR_PTR(opts
->minor
);
1309 if (idr_is_empty(&printer_ida
.idr
))
1313 config_group_init_type_name(&opts
->func_inst
.group
, "",
1314 &printer_func_type
);
1317 mutex_unlock(&printer_ida_lock
);
1321 static void gprinter_free(struct usb_function
*f
)
1323 struct printer_dev
*dev
= func_to_printer(f
);
1324 struct f_printer_opts
*opts
;
1326 opts
= container_of(f
->fi
, struct f_printer_opts
, func_inst
);
1328 mutex_lock(&opts
->lock
);
1330 mutex_unlock(&opts
->lock
);
1333 static void printer_func_unbind(struct usb_configuration
*c
,
1334 struct usb_function
*f
)
1336 struct printer_dev
*dev
;
1337 struct usb_request
*req
;
1339 dev
= func_to_printer(f
);
1341 device_destroy(usb_gadget_class
, MKDEV(major
, dev
->minor
));
1343 /* Remove Character Device */
1344 cdev_del(&dev
->printer_cdev
);
1346 /* we must already have been disconnected ... no i/o may be active */
1347 WARN_ON(!list_empty(&dev
->tx_reqs_active
));
1348 WARN_ON(!list_empty(&dev
->rx_reqs_active
));
1350 /* Free all memory for this driver. */
1351 while (!list_empty(&dev
->tx_reqs
)) {
1352 req
= container_of(dev
->tx_reqs
.next
, struct usb_request
,
1354 list_del(&req
->list
);
1355 printer_req_free(dev
->in_ep
, req
);
1358 if (dev
->current_rx_req
!= NULL
)
1359 printer_req_free(dev
->out_ep
, dev
->current_rx_req
);
1361 while (!list_empty(&dev
->rx_reqs
)) {
1362 req
= container_of(dev
->rx_reqs
.next
,
1363 struct usb_request
, list
);
1364 list_del(&req
->list
);
1365 printer_req_free(dev
->out_ep
, req
);
1368 while (!list_empty(&dev
->rx_buffers
)) {
1369 req
= container_of(dev
->rx_buffers
.next
,
1370 struct usb_request
, list
);
1371 list_del(&req
->list
);
1372 printer_req_free(dev
->out_ep
, req
);
1374 usb_free_all_descriptors(f
);
1377 static struct usb_function
*gprinter_alloc(struct usb_function_instance
*fi
)
1379 struct printer_dev
*dev
;
1380 struct f_printer_opts
*opts
;
1382 opts
= container_of(fi
, struct f_printer_opts
, func_inst
);
1384 mutex_lock(&opts
->lock
);
1385 if (opts
->minor
>= minors
) {
1386 mutex_unlock(&opts
->lock
);
1387 return ERR_PTR(-ENOENT
);
1390 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
1392 mutex_unlock(&opts
->lock
);
1393 return ERR_PTR(-ENOMEM
);
1397 dev
->minor
= opts
->minor
;
1398 dev
->pnp_string
= opts
->pnp_string
;
1399 dev
->q_len
= opts
->q_len
;
1400 mutex_unlock(&opts
->lock
);
1402 dev
->function
.name
= "printer";
1403 dev
->function
.bind
= printer_func_bind
;
1404 dev
->function
.setup
= printer_func_setup
;
1405 dev
->function
.unbind
= printer_func_unbind
;
1406 dev
->function
.set_alt
= printer_func_set_alt
;
1407 dev
->function
.disable
= printer_func_disable
;
1408 dev
->function
.req_match
= gprinter_req_match
;
1409 dev
->function
.free_func
= gprinter_free
;
1411 INIT_LIST_HEAD(&dev
->tx_reqs
);
1412 INIT_LIST_HEAD(&dev
->rx_reqs
);
1413 INIT_LIST_HEAD(&dev
->rx_buffers
);
1414 INIT_LIST_HEAD(&dev
->tx_reqs_active
);
1415 INIT_LIST_HEAD(&dev
->rx_reqs_active
);
1417 spin_lock_init(&dev
->lock
);
1418 mutex_init(&dev
->lock_printer_io
);
1419 init_waitqueue_head(&dev
->rx_wait
);
1420 init_waitqueue_head(&dev
->tx_wait
);
1421 init_waitqueue_head(&dev
->tx_flush_wait
);
1423 dev
->interface
= -1;
1424 dev
->printer_cdev_open
= 0;
1425 dev
->printer_status
= PRINTER_NOT_ERROR
;
1426 dev
->current_rx_req
= NULL
;
1427 dev
->current_rx_bytes
= 0;
1428 dev
->current_rx_buf
= NULL
;
1430 return &dev
->function
;
1433 DECLARE_USB_FUNCTION_INIT(printer
, gprinter_alloc_inst
, gprinter_alloc
);
1434 MODULE_LICENSE("GPL");
1435 MODULE_AUTHOR("Craig Nadler");
1437 static int gprinter_setup(int count
)
1442 usb_gadget_class
= class_create(THIS_MODULE
, "usb_printer_gadget");
1443 if (IS_ERR(usb_gadget_class
)) {
1444 status
= PTR_ERR(usb_gadget_class
);
1445 usb_gadget_class
= NULL
;
1446 pr_err("unable to create usb_gadget class %d\n", status
);
1450 status
= alloc_chrdev_region(&devt
, 0, count
, "USB printer gadget");
1452 pr_err("alloc_chrdev_region %d\n", status
);
1453 class_destroy(usb_gadget_class
);
1454 usb_gadget_class
= NULL
;
1458 major
= MAJOR(devt
);
1464 static void gprinter_cleanup(void)
1467 unregister_chrdev_region(MKDEV(major
, 0), minors
);
1470 class_destroy(usb_gadget_class
);
1471 usb_gadget_class
= NULL
;