1 // SPDX-License-Identifier: GPL-2.0+
3 * f_printer.c - USB printer function driver
5 * Copied from drivers/usb/gadget/legacy/printer.c,
8 * printer.c -- Printer gadget driver
10 * Copyright (C) 2003-2005 David Brownell
11 * Copyright (C) 2006 Craig W. Nadler
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/delay.h>
17 #include <linux/ioport.h>
18 #include <linux/sched.h>
19 #include <linux/slab.h>
20 #include <linux/mutex.h>
21 #include <linux/errno.h>
22 #include <linux/init.h>
23 #include <linux/idr.h>
24 #include <linux/timer.h>
25 #include <linux/list.h>
26 #include <linux/interrupt.h>
27 #include <linux/device.h>
28 #include <linux/moduleparam.h>
30 #include <linux/poll.h>
31 #include <linux/types.h>
32 #include <linux/ctype.h>
33 #include <linux/cdev.h>
34 #include <linux/kref.h>
36 #include <asm/byteorder.h>
38 #include <linux/irq.h>
39 #include <linux/uaccess.h>
40 #include <asm/unaligned.h>
42 #include <linux/usb/ch9.h>
43 #include <linux/usb/composite.h>
44 #include <linux/usb/gadget.h>
45 #include <linux/usb/g_printer.h>
47 #include "u_printer.h"
49 #define PRINTER_MINORS 4
50 #define GET_DEVICE_ID 0
51 #define GET_PORT_STATUS 1
54 #define DEFAULT_Q_LEN 10 /* same as legacy g_printer gadget */
56 static int major, minors;
57 static const struct class usb_gadget_class = {
58 .name = "usb_printer_gadget",
61 static DEFINE_IDA(printer_ida);
62 static DEFINE_MUTEX(printer_ida_lock); /* protects access do printer_ida */
64 /*-------------------------------------------------------------------------*/
67 spinlock_t lock; /* lock this structure */
68 /* lock buffer lists during read/write calls */
69 struct mutex lock_printer_io;
70 struct usb_gadget *gadget;
72 struct usb_ep *in_ep, *out_ep;
74 struct list_head rx_reqs; /* List of free RX structs */
75 struct list_head rx_reqs_active; /* List of Active RX xfers */
76 struct list_head rx_buffers; /* List of completed xfers */
77 /* wait until there is data to be read. */
78 wait_queue_head_t rx_wait;
79 struct list_head tx_reqs; /* List of free TX structs */
80 struct list_head tx_reqs_active; /* List of Active TX xfers */
81 /* Wait until there are write buffers available to use. */
82 wait_queue_head_t tx_wait;
83 /* Wait until all write buffers have been sent. */
84 wait_queue_head_t tx_flush_wait;
85 struct usb_request *current_rx_req;
86 size_t current_rx_bytes;
91 struct cdev printer_cdev;
93 wait_queue_head_t wait;
95 char **pnp_string; /* We don't own memory! */
96 struct usb_function function;
99 static inline struct printer_dev *func_to_printer(struct usb_function *f)
101 return container_of(f, struct printer_dev, function);
104 /*-------------------------------------------------------------------------*/
107 * DESCRIPTORS ... most are static, but strings and (full) configuration
108 * descriptors are built on demand.
111 /* holds our biggest descriptor */
112 #define USB_DESC_BUFSIZE 256
113 #define USB_BUFSIZE 8192
115 static struct usb_interface_descriptor intf_desc = {
116 .bLength = sizeof(intf_desc),
117 .bDescriptorType = USB_DT_INTERFACE,
119 .bInterfaceClass = USB_CLASS_PRINTER,
120 .bInterfaceSubClass = 1, /* Printer Sub-Class */
121 .bInterfaceProtocol = 2, /* Bi-Directional */
125 static struct usb_endpoint_descriptor fs_ep_in_desc = {
126 .bLength = USB_DT_ENDPOINT_SIZE,
127 .bDescriptorType = USB_DT_ENDPOINT,
128 .bEndpointAddress = USB_DIR_IN,
129 .bmAttributes = USB_ENDPOINT_XFER_BULK
132 static struct usb_endpoint_descriptor fs_ep_out_desc = {
133 .bLength = USB_DT_ENDPOINT_SIZE,
134 .bDescriptorType = USB_DT_ENDPOINT,
135 .bEndpointAddress = USB_DIR_OUT,
136 .bmAttributes = USB_ENDPOINT_XFER_BULK
139 static struct usb_descriptor_header *fs_printer_function[] = {
140 (struct usb_descriptor_header *) &intf_desc,
141 (struct usb_descriptor_header *) &fs_ep_in_desc,
142 (struct usb_descriptor_header *) &fs_ep_out_desc,
147 * usb 2.0 devices need to expose both high speed and full speed
148 * descriptors, unless they only run at full speed.
151 static struct usb_endpoint_descriptor hs_ep_in_desc = {
152 .bLength = USB_DT_ENDPOINT_SIZE,
153 .bDescriptorType = USB_DT_ENDPOINT,
154 .bmAttributes = USB_ENDPOINT_XFER_BULK,
155 .wMaxPacketSize = cpu_to_le16(512)
158 static struct usb_endpoint_descriptor hs_ep_out_desc = {
159 .bLength = USB_DT_ENDPOINT_SIZE,
160 .bDescriptorType = USB_DT_ENDPOINT,
161 .bmAttributes = USB_ENDPOINT_XFER_BULK,
162 .wMaxPacketSize = cpu_to_le16(512)
165 static struct usb_descriptor_header *hs_printer_function[] = {
166 (struct usb_descriptor_header *) &intf_desc,
167 (struct usb_descriptor_header *) &hs_ep_in_desc,
168 (struct usb_descriptor_header *) &hs_ep_out_desc,
173 * Added endpoint descriptors for 3.0 devices
176 static struct usb_endpoint_descriptor ss_ep_in_desc = {
177 .bLength = USB_DT_ENDPOINT_SIZE,
178 .bDescriptorType = USB_DT_ENDPOINT,
179 .bmAttributes = USB_ENDPOINT_XFER_BULK,
180 .wMaxPacketSize = cpu_to_le16(1024),
183 static struct usb_ss_ep_comp_descriptor ss_ep_in_comp_desc = {
184 .bLength = sizeof(ss_ep_in_comp_desc),
185 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
188 static struct usb_endpoint_descriptor ss_ep_out_desc = {
189 .bLength = USB_DT_ENDPOINT_SIZE,
190 .bDescriptorType = USB_DT_ENDPOINT,
191 .bmAttributes = USB_ENDPOINT_XFER_BULK,
192 .wMaxPacketSize = cpu_to_le16(1024),
195 static struct usb_ss_ep_comp_descriptor ss_ep_out_comp_desc = {
196 .bLength = sizeof(ss_ep_out_comp_desc),
197 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
200 static struct usb_descriptor_header *ss_printer_function[] = {
201 (struct usb_descriptor_header *) &intf_desc,
202 (struct usb_descriptor_header *) &ss_ep_in_desc,
203 (struct usb_descriptor_header *) &ss_ep_in_comp_desc,
204 (struct usb_descriptor_header *) &ss_ep_out_desc,
205 (struct usb_descriptor_header *) &ss_ep_out_comp_desc,
209 /* maxpacket and other transfer characteristics vary by speed. */
210 static inline struct usb_endpoint_descriptor *ep_desc(struct usb_gadget *gadget,
211 struct usb_endpoint_descriptor *fs,
212 struct usb_endpoint_descriptor *hs,
213 struct usb_endpoint_descriptor *ss)
215 switch (gadget->speed) {
216 case USB_SPEED_SUPER:
225 /*-------------------------------------------------------------------------*/
227 static void printer_dev_free(struct kref *kref)
229 struct printer_dev *dev = container_of(kref, struct printer_dev, kref);
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->interface < 0) {
355 spin_unlock_irqrestore(&dev->lock, flags);
359 if (!dev->printer_cdev_open) {
360 dev->printer_cdev_open = 1;
361 fd->private_data = dev;
363 /* Change the printer status to show that it's on-line. */
364 dev->printer_status |= PRINTER_SELECTED;
367 spin_unlock_irqrestore(&dev->lock, flags);
369 kref_get(&dev->kref);
375 printer_close(struct inode *inode, struct file *fd)
377 struct printer_dev *dev = fd->private_data;
380 spin_lock_irqsave(&dev->lock, flags);
381 dev->printer_cdev_open = 0;
382 fd->private_data = NULL;
383 /* Change printer status to show that the printer is off-line. */
384 dev->printer_status &= ~PRINTER_SELECTED;
385 spin_unlock_irqrestore(&dev->lock, flags);
387 kref_put(&dev->kref, printer_dev_free);
392 /* This function must be called with interrupts turned off. */
394 setup_rx_reqs(struct printer_dev *dev)
396 struct usb_request *req;
398 while (likely(!list_empty(&dev->rx_reqs))) {
401 req = container_of(dev->rx_reqs.next,
402 struct usb_request, list);
403 list_del_init(&req->list);
405 /* The USB Host sends us whatever amount of data it wants to
406 * so we always set the length field to the full USB_BUFSIZE.
407 * If the amount of data is more than the read() caller asked
408 * for it will be stored in the request buffer until it is
409 * asked for by read().
411 req->length = USB_BUFSIZE;
412 req->complete = rx_complete;
414 /* here, we unlock, and only unlock, to avoid deadlock. */
415 spin_unlock(&dev->lock);
416 error = usb_ep_queue(dev->out_ep, req, GFP_ATOMIC);
417 spin_lock(&dev->lock);
419 DBG(dev, "rx submit --> %d\n", error);
420 list_add(&req->list, &dev->rx_reqs);
423 /* if the req is empty, then add it into dev->rx_reqs_active. */
424 else if (list_empty(&req->list))
425 list_add(&req->list, &dev->rx_reqs_active);
430 printer_read(struct file *fd, char __user *buf, size_t len, loff_t *ptr)
432 struct printer_dev *dev = fd->private_data;
436 struct usb_request *req;
437 /* This is a pointer to the current USB rx request. */
438 struct usb_request *current_rx_req;
439 /* This is the number of bytes in the current rx buffer. */
440 size_t current_rx_bytes;
441 /* This is a pointer to the current rx buffer. */
447 DBG(dev, "printer_read trying to read %d bytes\n", (int)len);
449 mutex_lock(&dev->lock_printer_io);
450 spin_lock_irqsave(&dev->lock, flags);
452 if (dev->interface < 0) {
453 spin_unlock_irqrestore(&dev->lock, flags);
454 mutex_unlock(&dev->lock_printer_io);
458 /* We will use this flag later to check if a printer reset happened
459 * after we turn interrupts back on.
461 dev->reset_printer = 0;
466 current_rx_req = dev->current_rx_req;
467 current_rx_bytes = dev->current_rx_bytes;
468 current_rx_buf = dev->current_rx_buf;
469 dev->current_rx_req = NULL;
470 dev->current_rx_bytes = 0;
471 dev->current_rx_buf = NULL;
473 /* Check if there is any data in the read buffers. Please note that
474 * current_rx_bytes is the number of bytes in the current rx buffer.
475 * If it is zero then check if there are any other rx_buffers that
476 * are on the completed list. We are only out of data if all rx
479 if ((current_rx_bytes == 0) &&
480 (likely(list_empty(&dev->rx_buffers)))) {
481 /* Turn interrupts back on before sleeping. */
482 spin_unlock_irqrestore(&dev->lock, flags);
485 * If no data is available check if this is a NON-Blocking
488 if (fd->f_flags & (O_NONBLOCK|O_NDELAY)) {
489 mutex_unlock(&dev->lock_printer_io);
493 /* Sleep until data is available */
494 wait_event_interruptible(dev->rx_wait,
495 (likely(!list_empty(&dev->rx_buffers))));
496 spin_lock_irqsave(&dev->lock, flags);
499 /* We have data to return then copy it to the caller's buffer.*/
500 while ((current_rx_bytes || likely(!list_empty(&dev->rx_buffers)))
502 if (current_rx_bytes == 0) {
503 req = container_of(dev->rx_buffers.next,
504 struct usb_request, list);
505 list_del_init(&req->list);
507 if (req->actual && req->buf) {
508 current_rx_req = req;
509 current_rx_bytes = req->actual;
510 current_rx_buf = req->buf;
512 list_add(&req->list, &dev->rx_reqs);
517 /* Don't leave irqs off while doing memory copies */
518 spin_unlock_irqrestore(&dev->lock, flags);
520 if (len > current_rx_bytes)
521 size = current_rx_bytes;
525 size -= copy_to_user(buf, current_rx_buf, size);
526 bytes_copied += size;
530 spin_lock_irqsave(&dev->lock, flags);
532 /* We've disconnected or reset so return. */
533 if (dev->reset_printer) {
534 list_add(¤t_rx_req->list, &dev->rx_reqs);
535 spin_unlock_irqrestore(&dev->lock, flags);
536 mutex_unlock(&dev->lock_printer_io);
540 /* If we not returning all the data left in this RX request
541 * buffer then adjust the amount of data left in the buffer.
542 * Othewise if we are done with this RX request buffer then
543 * requeue it to get any incoming data from the USB host.
545 if (size < current_rx_bytes) {
546 current_rx_bytes -= size;
547 current_rx_buf += size;
549 list_add(¤t_rx_req->list, &dev->rx_reqs);
550 current_rx_bytes = 0;
551 current_rx_buf = NULL;
552 current_rx_req = NULL;
556 dev->current_rx_req = current_rx_req;
557 dev->current_rx_bytes = current_rx_bytes;
558 dev->current_rx_buf = current_rx_buf;
560 spin_unlock_irqrestore(&dev->lock, flags);
561 mutex_unlock(&dev->lock_printer_io);
563 DBG(dev, "printer_read returned %d bytes\n", (int)bytes_copied);
572 printer_write(struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
574 struct printer_dev *dev = fd->private_data;
576 size_t size; /* Amount of data in a TX request. */
577 size_t bytes_copied = 0;
578 struct usb_request *req;
581 DBG(dev, "printer_write trying to send %d bytes\n", (int)len);
586 mutex_lock(&dev->lock_printer_io);
587 spin_lock_irqsave(&dev->lock, flags);
589 if (dev->interface < 0) {
590 spin_unlock_irqrestore(&dev->lock, flags);
591 mutex_unlock(&dev->lock_printer_io);
595 /* Check if a printer reset happens while we have interrupts on */
596 dev->reset_printer = 0;
598 /* Check if there is any available write buffers */
599 if (likely(list_empty(&dev->tx_reqs))) {
600 /* Turn interrupts back on before sleeping. */
601 spin_unlock_irqrestore(&dev->lock, flags);
604 * If write buffers are available check if this is
605 * a NON-Blocking call or not.
607 if (fd->f_flags & (O_NONBLOCK|O_NDELAY)) {
608 mutex_unlock(&dev->lock_printer_io);
612 /* Sleep until a write buffer is available */
613 wait_event_interruptible(dev->tx_wait,
614 (likely(!list_empty(&dev->tx_reqs))));
615 spin_lock_irqsave(&dev->lock, flags);
618 while (likely(!list_empty(&dev->tx_reqs)) && len) {
620 if (len > USB_BUFSIZE)
625 req = container_of(dev->tx_reqs.next, struct usb_request,
627 list_del_init(&req->list);
629 req->complete = tx_complete;
632 /* Check if we need to send a zero length packet. */
634 /* They will be more TX requests so no yet. */
637 /* If the data amount is not a multiple of the
638 * maxpacket size then send a zero length packet.
640 req->zero = ((len % dev->in_ep->maxpacket) == 0);
642 /* Don't leave irqs off while doing memory copies */
643 spin_unlock_irqrestore(&dev->lock, flags);
645 if (copy_from_user(req->buf, buf, size)) {
646 list_add(&req->list, &dev->tx_reqs);
647 mutex_unlock(&dev->lock_printer_io);
651 bytes_copied += size;
655 spin_lock_irqsave(&dev->lock, flags);
657 /* We've disconnected or reset so free the req and buffer */
658 if (dev->reset_printer) {
659 list_add(&req->list, &dev->tx_reqs);
660 spin_unlock_irqrestore(&dev->lock, flags);
661 mutex_unlock(&dev->lock_printer_io);
665 list_add(&req->list, &dev->tx_reqs_active);
667 /* here, we unlock, and only unlock, to avoid deadlock. */
668 spin_unlock(&dev->lock);
669 value = usb_ep_queue(dev->in_ep, req, GFP_ATOMIC);
670 spin_lock(&dev->lock);
672 list_move(&req->list, &dev->tx_reqs);
673 spin_unlock_irqrestore(&dev->lock, flags);
674 mutex_unlock(&dev->lock_printer_io);
679 spin_unlock_irqrestore(&dev->lock, flags);
680 mutex_unlock(&dev->lock_printer_io);
682 DBG(dev, "printer_write sent %d bytes\n", (int)bytes_copied);
691 printer_fsync(struct file *fd, loff_t start, loff_t end, int datasync)
693 struct printer_dev *dev = fd->private_data;
694 struct inode *inode = file_inode(fd);
699 spin_lock_irqsave(&dev->lock, flags);
701 if (dev->interface < 0) {
702 spin_unlock_irqrestore(&dev->lock, flags);
707 tx_list_empty = (likely(list_empty(&dev->tx_reqs)));
708 spin_unlock_irqrestore(&dev->lock, flags);
710 if (!tx_list_empty) {
711 /* Sleep until all data has been sent */
712 wait_event_interruptible(dev->tx_flush_wait,
713 (likely(list_empty(&dev->tx_reqs_active))));
721 printer_poll(struct file *fd, poll_table *wait)
723 struct printer_dev *dev = fd->private_data;
727 mutex_lock(&dev->lock_printer_io);
728 spin_lock_irqsave(&dev->lock, flags);
730 if (dev->interface < 0) {
731 spin_unlock_irqrestore(&dev->lock, flags);
732 mutex_unlock(&dev->lock_printer_io);
733 return EPOLLERR | EPOLLHUP;
737 spin_unlock_irqrestore(&dev->lock, flags);
738 mutex_unlock(&dev->lock_printer_io);
740 poll_wait(fd, &dev->rx_wait, wait);
741 poll_wait(fd, &dev->tx_wait, wait);
743 spin_lock_irqsave(&dev->lock, flags);
744 if (likely(!list_empty(&dev->tx_reqs)))
745 status |= EPOLLOUT | EPOLLWRNORM;
747 if (likely(dev->current_rx_bytes) ||
748 likely(!list_empty(&dev->rx_buffers)))
749 status |= EPOLLIN | EPOLLRDNORM;
751 spin_unlock_irqrestore(&dev->lock, flags);
757 printer_ioctl(struct file *fd, unsigned int code, unsigned long arg)
759 struct printer_dev *dev = fd->private_data;
763 DBG(dev, "printer_ioctl: cmd=0x%4.4x, arg=%lu\n", code, arg);
767 spin_lock_irqsave(&dev->lock, flags);
769 if (dev->interface < 0) {
770 spin_unlock_irqrestore(&dev->lock, flags);
775 case GADGET_GET_PRINTER_STATUS:
776 status = (int)dev->printer_status;
778 case GADGET_SET_PRINTER_STATUS:
779 dev->printer_status = (u8)arg;
782 /* could not handle ioctl */
783 DBG(dev, "printer_ioctl: ERROR cmd=0x%4.4xis not supported\n",
788 spin_unlock_irqrestore(&dev->lock, flags);
793 /* used after endpoint configuration */
794 static const struct file_operations printer_io_operations = {
795 .owner = THIS_MODULE,
796 .open = printer_open,
797 .read = printer_read,
798 .write = printer_write,
799 .fsync = printer_fsync,
800 .poll = printer_poll,
801 .unlocked_ioctl = printer_ioctl,
802 .release = printer_close,
803 .llseek = noop_llseek,
806 /*-------------------------------------------------------------------------*/
809 set_printer_interface(struct printer_dev *dev)
813 dev->in_ep->desc = ep_desc(dev->gadget, &fs_ep_in_desc, &hs_ep_in_desc,
815 dev->in_ep->driver_data = dev;
817 dev->out_ep->desc = ep_desc(dev->gadget, &fs_ep_out_desc,
818 &hs_ep_out_desc, &ss_ep_out_desc);
819 dev->out_ep->driver_data = dev;
821 result = usb_ep_enable(dev->in_ep);
823 DBG(dev, "enable %s --> %d\n", dev->in_ep->name, result);
827 result = usb_ep_enable(dev->out_ep);
829 DBG(dev, "enable %s --> %d\n", dev->out_ep->name, result);
834 /* on error, disable any endpoints */
836 (void) usb_ep_disable(dev->in_ep);
837 (void) usb_ep_disable(dev->out_ep);
838 dev->in_ep->desc = NULL;
839 dev->out_ep->desc = NULL;
842 /* caller is responsible for cleanup on error */
846 static void printer_reset_interface(struct printer_dev *dev)
850 if (dev->interface < 0)
853 if (dev->in_ep->desc)
854 usb_ep_disable(dev->in_ep);
856 if (dev->out_ep->desc)
857 usb_ep_disable(dev->out_ep);
859 spin_lock_irqsave(&dev->lock, flags);
860 dev->in_ep->desc = NULL;
861 dev->out_ep->desc = NULL;
863 spin_unlock_irqrestore(&dev->lock, flags);
866 /* Change our operational Interface. */
867 static int set_interface(struct printer_dev *dev, unsigned number)
871 /* Free the current interface */
872 printer_reset_interface(dev);
874 result = set_printer_interface(dev);
876 printer_reset_interface(dev);
878 dev->interface = number;
881 INFO(dev, "Using interface %x\n", number);
886 static void printer_soft_reset(struct printer_dev *dev)
888 struct usb_request *req;
890 if (usb_ep_disable(dev->in_ep))
891 DBG(dev, "Failed to disable USB in_ep\n");
892 if (usb_ep_disable(dev->out_ep))
893 DBG(dev, "Failed to disable USB out_ep\n");
895 if (dev->current_rx_req != NULL) {
896 list_add(&dev->current_rx_req->list, &dev->rx_reqs);
897 dev->current_rx_req = NULL;
899 dev->current_rx_bytes = 0;
900 dev->current_rx_buf = NULL;
901 dev->reset_printer = 1;
903 while (likely(!(list_empty(&dev->rx_buffers)))) {
904 req = container_of(dev->rx_buffers.next, struct usb_request,
906 list_del_init(&req->list);
907 list_add(&req->list, &dev->rx_reqs);
910 while (likely(!(list_empty(&dev->rx_reqs_active)))) {
911 req = container_of(dev->rx_buffers.next, struct usb_request,
913 list_del_init(&req->list);
914 list_add(&req->list, &dev->rx_reqs);
917 while (likely(!(list_empty(&dev->tx_reqs_active)))) {
918 req = container_of(dev->tx_reqs_active.next,
919 struct usb_request, list);
920 list_del_init(&req->list);
921 list_add(&req->list, &dev->tx_reqs);
924 if (usb_ep_enable(dev->in_ep))
925 DBG(dev, "Failed to enable USB in_ep\n");
926 if (usb_ep_enable(dev->out_ep))
927 DBG(dev, "Failed to enable USB out_ep\n");
929 wake_up_interruptible(&dev->rx_wait);
930 wake_up_interruptible(&dev->tx_wait);
931 wake_up_interruptible(&dev->tx_flush_wait);
934 /*-------------------------------------------------------------------------*/
936 static bool gprinter_req_match(struct usb_function *f,
937 const struct usb_ctrlrequest *ctrl,
940 struct printer_dev *dev = func_to_printer(f);
941 u16 w_index = le16_to_cpu(ctrl->wIndex);
942 u16 w_value = le16_to_cpu(ctrl->wValue);
943 u16 w_length = le16_to_cpu(ctrl->wLength);
948 if ((ctrl->bRequestType & USB_RECIP_MASK) != USB_RECIP_INTERFACE ||
949 (ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS)
952 switch (ctrl->bRequest) {
955 if (USB_DIR_IN & ctrl->bRequestType)
958 case GET_PORT_STATUS:
959 if (!w_value && w_length == 1 &&
960 (USB_DIR_IN & ctrl->bRequestType))
964 if (!w_value && !w_length &&
965 !(USB_DIR_IN & ctrl->bRequestType))
971 return w_index == dev->interface;
975 * The setup() callback implements all the ep0 functionality that's not
976 * handled lower down.
978 static int printer_func_setup(struct usb_function *f,
979 const struct usb_ctrlrequest *ctrl)
981 struct printer_dev *dev = func_to_printer(f);
982 struct usb_composite_dev *cdev = f->config->cdev;
983 struct usb_request *req = cdev->req;
985 int value = -EOPNOTSUPP;
986 u16 wIndex = le16_to_cpu(ctrl->wIndex);
987 u16 wValue = le16_to_cpu(ctrl->wValue);
988 u16 wLength = le16_to_cpu(ctrl->wLength);
990 DBG(dev, "ctrl req%02x.%02x v%04x i%04x l%d\n",
991 ctrl->bRequestType, ctrl->bRequest, wValue, wIndex, wLength);
993 switch (ctrl->bRequestType&USB_TYPE_MASK) {
995 switch (ctrl->bRequest) {
996 case GET_DEVICE_ID: /* Get the IEEE-1284 PNP String */
997 /* Only one printer interface is supported. */
998 if ((wIndex>>8) != dev->interface)
1001 if (!*dev->pnp_string) {
1005 value = strlen(*dev->pnp_string);
1006 buf[0] = (value >> 8) & 0xFF;
1007 buf[1] = value & 0xFF;
1008 memcpy(buf + 2, *dev->pnp_string, value);
1009 DBG(dev, "1284 PNP String: %x %s\n", value,
1013 case GET_PORT_STATUS: /* Get Port Status */
1014 /* Only one printer interface is supported. */
1015 if (wIndex != dev->interface)
1018 buf[0] = dev->printer_status;
1019 value = min_t(u16, wLength, 1);
1022 case SOFT_RESET: /* Soft Reset */
1023 /* Only one printer interface is supported. */
1024 if (wIndex != dev->interface)
1027 printer_soft_reset(dev);
1040 "unknown ctrl req%02x.%02x v%04x i%04x l%d\n",
1041 ctrl->bRequestType, ctrl->bRequest,
1042 wValue, wIndex, wLength);
1045 /* host either stalls (value < 0) or reports success */
1047 req->length = value;
1048 req->zero = value < wLength;
1049 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
1051 ERROR(dev, "%s:%d Error!\n", __func__, __LINE__);
1058 static int printer_func_bind(struct usb_configuration *c,
1059 struct usb_function *f)
1061 struct usb_gadget *gadget = c->cdev->gadget;
1062 struct printer_dev *dev = func_to_printer(f);
1063 struct device *pdev;
1064 struct usb_composite_dev *cdev = c->cdev;
1065 struct usb_ep *in_ep;
1066 struct usb_ep *out_ep = NULL;
1067 struct usb_request *req;
1073 id = usb_interface_id(c, f);
1076 intf_desc.bInterfaceNumber = id;
1078 /* finish hookup to lower layer ... */
1079 dev->gadget = gadget;
1081 /* all we really need is bulk IN/OUT */
1082 in_ep = usb_ep_autoconfig(cdev->gadget, &fs_ep_in_desc);
1085 dev_err(&cdev->gadget->dev, "can't autoconfigure on %s\n",
1086 cdev->gadget->name);
1090 out_ep = usb_ep_autoconfig(cdev->gadget, &fs_ep_out_desc);
1094 /* assumes that all endpoints are dual-speed */
1095 hs_ep_in_desc.bEndpointAddress = fs_ep_in_desc.bEndpointAddress;
1096 hs_ep_out_desc.bEndpointAddress = fs_ep_out_desc.bEndpointAddress;
1097 ss_ep_in_desc.bEndpointAddress = fs_ep_in_desc.bEndpointAddress;
1098 ss_ep_out_desc.bEndpointAddress = fs_ep_out_desc.bEndpointAddress;
1100 ret = usb_assign_descriptors(f, fs_printer_function,
1101 hs_printer_function, ss_printer_function,
1102 ss_printer_function);
1107 dev->out_ep = out_ep;
1110 for (i = 0; i < dev->q_len; i++) {
1111 req = printer_req_alloc(dev->in_ep, USB_BUFSIZE, GFP_KERNEL);
1114 list_add(&req->list, &dev->tx_reqs);
1117 for (i = 0; i < dev->q_len; i++) {
1118 req = printer_req_alloc(dev->out_ep, USB_BUFSIZE, GFP_KERNEL);
1121 list_add(&req->list, &dev->rx_reqs);
1124 /* Setup the sysfs files for the printer gadget. */
1125 devt = MKDEV(major, dev->minor);
1126 pdev = device_create(&usb_gadget_class, NULL, devt,
1127 NULL, "g_printer%d", dev->minor);
1129 ERROR(dev, "Failed to create device: g_printer\n");
1130 ret = PTR_ERR(pdev);
1135 * Register a character device as an interface to a user mode
1136 * program that handles the printer specific functionality.
1138 cdev_init(&dev->printer_cdev, &printer_io_operations);
1139 dev->printer_cdev.owner = THIS_MODULE;
1140 ret = cdev_add(&dev->printer_cdev, devt, 1);
1142 ERROR(dev, "Failed to open char device\n");
1149 device_destroy(&usb_gadget_class, devt);
1152 while (!list_empty(&dev->rx_reqs)) {
1153 req = container_of(dev->rx_reqs.next, struct usb_request, list);
1154 list_del(&req->list);
1155 printer_req_free(dev->out_ep, req);
1159 while (!list_empty(&dev->tx_reqs)) {
1160 req = container_of(dev->tx_reqs.next, struct usb_request, list);
1161 list_del(&req->list);
1162 printer_req_free(dev->in_ep, req);
1165 usb_free_all_descriptors(f);
1170 static int printer_func_set_alt(struct usb_function *f,
1171 unsigned intf, unsigned alt)
1173 struct printer_dev *dev = func_to_printer(f);
1174 int ret = -ENOTSUPP;
1177 ret = set_interface(dev, intf);
1182 static void printer_func_disable(struct usb_function *f)
1184 struct printer_dev *dev = func_to_printer(f);
1186 printer_reset_interface(dev);
1189 static inline struct f_printer_opts
1190 *to_f_printer_opts(struct config_item *item)
1192 return container_of(to_config_group(item), struct f_printer_opts,
1196 static void printer_attr_release(struct config_item *item)
1198 struct f_printer_opts *opts = to_f_printer_opts(item);
1200 usb_put_function_instance(&opts->func_inst);
1203 static struct configfs_item_operations printer_item_ops = {
1204 .release = printer_attr_release,
1207 static ssize_t f_printer_opts_pnp_string_show(struct config_item *item,
1210 struct f_printer_opts *opts = to_f_printer_opts(item);
1213 mutex_lock(&opts->lock);
1214 if (!opts->pnp_string)
1217 result = strscpy(page, opts->pnp_string, PAGE_SIZE);
1220 } else if (page[result - 1] != '\n' && result + 1 < PAGE_SIZE) {
1221 page[result++] = '\n';
1222 page[result] = '\0';
1226 mutex_unlock(&opts->lock);
1231 static ssize_t f_printer_opts_pnp_string_store(struct config_item *item,
1232 const char *page, size_t len)
1234 struct f_printer_opts *opts = to_f_printer_opts(item);
1238 mutex_lock(&opts->lock);
1240 new_pnp = kstrndup(page, len, GFP_KERNEL);
1246 if (opts->pnp_string_allocated)
1247 kfree(opts->pnp_string);
1249 opts->pnp_string_allocated = true;
1250 opts->pnp_string = new_pnp;
1253 mutex_unlock(&opts->lock);
1258 CONFIGFS_ATTR(f_printer_opts_, pnp_string);
1260 static ssize_t f_printer_opts_q_len_show(struct config_item *item,
1263 struct f_printer_opts *opts = to_f_printer_opts(item);
1266 mutex_lock(&opts->lock);
1267 result = sprintf(page, "%d\n", opts->q_len);
1268 mutex_unlock(&opts->lock);
1273 static ssize_t f_printer_opts_q_len_store(struct config_item *item,
1274 const char *page, size_t len)
1276 struct f_printer_opts *opts = to_f_printer_opts(item);
1280 mutex_lock(&opts->lock);
1286 ret = kstrtou16(page, 0, &num);
1290 opts->q_len = (unsigned)num;
1293 mutex_unlock(&opts->lock);
1297 CONFIGFS_ATTR(f_printer_opts_, q_len);
1299 static struct configfs_attribute *printer_attrs[] = {
1300 &f_printer_opts_attr_pnp_string,
1301 &f_printer_opts_attr_q_len,
1305 static const struct config_item_type printer_func_type = {
1306 .ct_item_ops = &printer_item_ops,
1307 .ct_attrs = printer_attrs,
1308 .ct_owner = THIS_MODULE,
1311 static inline int gprinter_get_minor(void)
1315 ret = ida_simple_get(&printer_ida, 0, 0, GFP_KERNEL);
1316 if (ret >= PRINTER_MINORS) {
1317 ida_simple_remove(&printer_ida, ret);
1324 static inline void gprinter_put_minor(int minor)
1326 ida_simple_remove(&printer_ida, minor);
1329 static int gprinter_setup(int);
1330 static void gprinter_cleanup(void);
1332 static void gprinter_free_inst(struct usb_function_instance *f)
1334 struct f_printer_opts *opts;
1336 opts = container_of(f, struct f_printer_opts, func_inst);
1338 mutex_lock(&printer_ida_lock);
1340 gprinter_put_minor(opts->minor);
1341 if (ida_is_empty(&printer_ida))
1344 mutex_unlock(&printer_ida_lock);
1346 if (opts->pnp_string_allocated)
1347 kfree(opts->pnp_string);
1351 static struct usb_function_instance *gprinter_alloc_inst(void)
1353 struct f_printer_opts *opts;
1354 struct usb_function_instance *ret;
1357 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
1359 return ERR_PTR(-ENOMEM);
1361 mutex_init(&opts->lock);
1362 opts->func_inst.free_func_inst = gprinter_free_inst;
1363 ret = &opts->func_inst;
1365 /* Make sure q_len is initialized, otherwise the bound device can't support read/write! */
1366 opts->q_len = DEFAULT_Q_LEN;
1368 mutex_lock(&printer_ida_lock);
1370 if (ida_is_empty(&printer_ida)) {
1371 status = gprinter_setup(PRINTER_MINORS);
1373 ret = ERR_PTR(status);
1379 opts->minor = gprinter_get_minor();
1380 if (opts->minor < 0) {
1381 ret = ERR_PTR(opts->minor);
1383 if (ida_is_empty(&printer_ida))
1387 config_group_init_type_name(&opts->func_inst.group, "",
1388 &printer_func_type);
1391 mutex_unlock(&printer_ida_lock);
1395 static void gprinter_free(struct usb_function *f)
1397 struct printer_dev *dev = func_to_printer(f);
1398 struct f_printer_opts *opts;
1400 opts = container_of(f->fi, struct f_printer_opts, func_inst);
1402 kref_put(&dev->kref, printer_dev_free);
1403 mutex_lock(&opts->lock);
1405 mutex_unlock(&opts->lock);
1408 static void printer_func_unbind(struct usb_configuration *c,
1409 struct usb_function *f)
1411 struct printer_dev *dev;
1412 struct usb_request *req;
1414 dev = func_to_printer(f);
1416 device_destroy(&usb_gadget_class, MKDEV(major, dev->minor));
1418 /* Remove Character Device */
1419 cdev_del(&dev->printer_cdev);
1421 /* we must already have been disconnected ... no i/o may be active */
1422 WARN_ON(!list_empty(&dev->tx_reqs_active));
1423 WARN_ON(!list_empty(&dev->rx_reqs_active));
1425 /* Free all memory for this driver. */
1426 while (!list_empty(&dev->tx_reqs)) {
1427 req = container_of(dev->tx_reqs.next, struct usb_request,
1429 list_del(&req->list);
1430 printer_req_free(dev->in_ep, req);
1433 if (dev->current_rx_req != NULL)
1434 printer_req_free(dev->out_ep, dev->current_rx_req);
1436 while (!list_empty(&dev->rx_reqs)) {
1437 req = container_of(dev->rx_reqs.next,
1438 struct usb_request, list);
1439 list_del(&req->list);
1440 printer_req_free(dev->out_ep, req);
1443 while (!list_empty(&dev->rx_buffers)) {
1444 req = container_of(dev->rx_buffers.next,
1445 struct usb_request, list);
1446 list_del(&req->list);
1447 printer_req_free(dev->out_ep, req);
1449 usb_free_all_descriptors(f);
1452 static struct usb_function *gprinter_alloc(struct usb_function_instance *fi)
1454 struct printer_dev *dev;
1455 struct f_printer_opts *opts;
1457 opts = container_of(fi, struct f_printer_opts, func_inst);
1459 mutex_lock(&opts->lock);
1460 if (opts->minor >= minors) {
1461 mutex_unlock(&opts->lock);
1462 return ERR_PTR(-ENOENT);
1465 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1467 mutex_unlock(&opts->lock);
1468 return ERR_PTR(-ENOMEM);
1471 kref_init(&dev->kref);
1473 dev->minor = opts->minor;
1474 dev->pnp_string = &opts->pnp_string;
1475 dev->q_len = opts->q_len;
1476 mutex_unlock(&opts->lock);
1478 dev->function.name = "printer";
1479 dev->function.bind = printer_func_bind;
1480 dev->function.setup = printer_func_setup;
1481 dev->function.unbind = printer_func_unbind;
1482 dev->function.set_alt = printer_func_set_alt;
1483 dev->function.disable = printer_func_disable;
1484 dev->function.req_match = gprinter_req_match;
1485 dev->function.free_func = gprinter_free;
1487 INIT_LIST_HEAD(&dev->tx_reqs);
1488 INIT_LIST_HEAD(&dev->rx_reqs);
1489 INIT_LIST_HEAD(&dev->rx_buffers);
1490 INIT_LIST_HEAD(&dev->tx_reqs_active);
1491 INIT_LIST_HEAD(&dev->rx_reqs_active);
1493 spin_lock_init(&dev->lock);
1494 mutex_init(&dev->lock_printer_io);
1495 init_waitqueue_head(&dev->rx_wait);
1496 init_waitqueue_head(&dev->tx_wait);
1497 init_waitqueue_head(&dev->tx_flush_wait);
1499 dev->interface = -1;
1500 dev->printer_cdev_open = 0;
1501 dev->printer_status = PRINTER_NOT_ERROR;
1502 dev->current_rx_req = NULL;
1503 dev->current_rx_bytes = 0;
1504 dev->current_rx_buf = NULL;
1506 return &dev->function;
1509 DECLARE_USB_FUNCTION_INIT(printer, gprinter_alloc_inst, gprinter_alloc);
1510 MODULE_LICENSE("GPL");
1511 MODULE_AUTHOR("Craig Nadler");
1513 static int gprinter_setup(int count)
1518 status = class_register(&usb_gadget_class);
1522 status = alloc_chrdev_region(&devt, 0, count, "USB printer gadget");
1524 pr_err("alloc_chrdev_region %d\n", status);
1525 class_unregister(&usb_gadget_class);
1529 major = MAJOR(devt);
1535 static void gprinter_cleanup(void)
1538 unregister_chrdev_region(MKDEV(major, 0), minors);
1541 class_unregister(&usb_gadget_class);