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>
38 #include <linux/kref.h>
40 #include <asm/byteorder.h>
42 #include <linux/irq.h>
43 #include <linux/uaccess.h>
44 #include <asm/unaligned.h>
46 #include <linux/usb/ch9.h>
47 #include <linux/usb/composite.h>
48 #include <linux/usb/gadget.h>
49 #include <linux/usb/g_printer.h>
51 #include "u_printer.h"
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 void printer_dev_free(struct kref *kref)
228 struct printer_dev *dev = container_of(kref, struct printer_dev, kref);
233 static struct usb_request *
234 printer_req_alloc(struct usb_ep *ep, unsigned len, gfp_t gfp_flags)
236 struct usb_request *req;
238 req = usb_ep_alloc_request(ep, gfp_flags);
242 req->buf = kmalloc(len, gfp_flags);
243 if (req->buf == NULL) {
244 usb_ep_free_request(ep, req);
253 printer_req_free(struct usb_ep *ep, struct usb_request *req)
255 if (ep != NULL && req != NULL) {
257 usb_ep_free_request(ep, req);
261 /*-------------------------------------------------------------------------*/
263 static void rx_complete(struct usb_ep *ep, struct usb_request *req)
265 struct printer_dev *dev = ep->driver_data;
266 int status = req->status;
269 spin_lock_irqsave(&dev->lock, flags);
271 list_del_init(&req->list); /* Remode from Active List */
275 /* normal completion */
277 if (req->actual > 0) {
278 list_add_tail(&req->list, &dev->rx_buffers);
279 DBG(dev, "G_Printer : rx length %d\n", req->actual);
281 list_add(&req->list, &dev->rx_reqs);
285 /* software-driven interface shutdown */
286 case -ECONNRESET: /* unlink */
287 case -ESHUTDOWN: /* disconnect etc */
288 VDBG(dev, "rx shutdown, code %d\n", status);
289 list_add(&req->list, &dev->rx_reqs);
292 /* for hardware automagic (such as pxa) */
293 case -ECONNABORTED: /* endpoint reset */
294 DBG(dev, "rx %s reset\n", ep->name);
295 list_add(&req->list, &dev->rx_reqs);
303 DBG(dev, "rx status %d\n", status);
304 list_add(&req->list, &dev->rx_reqs);
308 wake_up_interruptible(&dev->rx_wait);
309 spin_unlock_irqrestore(&dev->lock, flags);
312 static void tx_complete(struct usb_ep *ep, struct usb_request *req)
314 struct printer_dev *dev = ep->driver_data;
316 switch (req->status) {
318 VDBG(dev, "tx err %d\n", req->status);
320 case -ECONNRESET: /* unlink */
321 case -ESHUTDOWN: /* disconnect etc */
327 spin_lock(&dev->lock);
328 /* Take the request struct off the active list and put it on the
331 list_del_init(&req->list);
332 list_add(&req->list, &dev->tx_reqs);
333 wake_up_interruptible(&dev->tx_wait);
334 if (likely(list_empty(&dev->tx_reqs_active)))
335 wake_up_interruptible(&dev->tx_flush_wait);
337 spin_unlock(&dev->lock);
340 /*-------------------------------------------------------------------------*/
343 printer_open(struct inode *inode, struct file *fd)
345 struct printer_dev *dev;
349 dev = container_of(inode->i_cdev, struct printer_dev, printer_cdev);
351 spin_lock_irqsave(&dev->lock, flags);
353 if (!dev->printer_cdev_open) {
354 dev->printer_cdev_open = 1;
355 fd->private_data = dev;
357 /* Change the printer status to show that it's on-line. */
358 dev->printer_status |= PRINTER_SELECTED;
361 spin_unlock_irqrestore(&dev->lock, flags);
363 kref_get(&dev->kref);
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 kref_put(&dev->kref, printer_dev_free);
382 DBG(dev, "printer_close\n");
387 /* This function must be called with interrupts turned off. */
389 setup_rx_reqs(struct printer_dev *dev)
391 struct usb_request *req;
393 while (likely(!list_empty(&dev->rx_reqs))) {
396 req = container_of(dev->rx_reqs.next,
397 struct usb_request, list);
398 list_del_init(&req->list);
400 /* The USB Host sends us whatever amount of data it wants to
401 * so we always set the length field to the full USB_BUFSIZE.
402 * If the amount of data is more than the read() caller asked
403 * for it will be stored in the request buffer until it is
404 * asked for by read().
406 req->length = USB_BUFSIZE;
407 req->complete = rx_complete;
409 /* here, we unlock, and only unlock, to avoid deadlock. */
410 spin_unlock(&dev->lock);
411 error = usb_ep_queue(dev->out_ep, req, GFP_ATOMIC);
412 spin_lock(&dev->lock);
414 DBG(dev, "rx submit --> %d\n", error);
415 list_add(&req->list, &dev->rx_reqs);
418 /* if the req is empty, then add it into dev->rx_reqs_active. */
419 else if (list_empty(&req->list))
420 list_add(&req->list, &dev->rx_reqs_active);
425 printer_read(struct file *fd, char __user *buf, size_t len, loff_t *ptr)
427 struct printer_dev *dev = fd->private_data;
431 struct usb_request *req;
432 /* This is a pointer to the current USB rx request. */
433 struct usb_request *current_rx_req;
434 /* This is the number of bytes in the current rx buffer. */
435 size_t current_rx_bytes;
436 /* This is a pointer to the current rx buffer. */
442 DBG(dev, "printer_read trying to read %d bytes\n", (int)len);
444 mutex_lock(&dev->lock_printer_io);
445 spin_lock_irqsave(&dev->lock, flags);
447 /* We will use this flag later to check if a printer reset happened
448 * after we turn interrupts back on.
450 dev->reset_printer = 0;
455 current_rx_req = dev->current_rx_req;
456 current_rx_bytes = dev->current_rx_bytes;
457 current_rx_buf = dev->current_rx_buf;
458 dev->current_rx_req = NULL;
459 dev->current_rx_bytes = 0;
460 dev->current_rx_buf = NULL;
462 /* Check if there is any data in the read buffers. Please note that
463 * current_rx_bytes is the number of bytes in the current rx buffer.
464 * If it is zero then check if there are any other rx_buffers that
465 * are on the completed list. We are only out of data if all rx
468 if ((current_rx_bytes == 0) &&
469 (likely(list_empty(&dev->rx_buffers)))) {
470 /* Turn interrupts back on before sleeping. */
471 spin_unlock_irqrestore(&dev->lock, flags);
474 * If no data is available check if this is a NON-Blocking
477 if (fd->f_flags & (O_NONBLOCK|O_NDELAY)) {
478 mutex_unlock(&dev->lock_printer_io);
482 /* Sleep until data is available */
483 wait_event_interruptible(dev->rx_wait,
484 (likely(!list_empty(&dev->rx_buffers))));
485 spin_lock_irqsave(&dev->lock, flags);
488 /* We have data to return then copy it to the caller's buffer.*/
489 while ((current_rx_bytes || likely(!list_empty(&dev->rx_buffers)))
491 if (current_rx_bytes == 0) {
492 req = container_of(dev->rx_buffers.next,
493 struct usb_request, list);
494 list_del_init(&req->list);
496 if (req->actual && req->buf) {
497 current_rx_req = req;
498 current_rx_bytes = req->actual;
499 current_rx_buf = req->buf;
501 list_add(&req->list, &dev->rx_reqs);
506 /* Don't leave irqs off while doing memory copies */
507 spin_unlock_irqrestore(&dev->lock, flags);
509 if (len > current_rx_bytes)
510 size = current_rx_bytes;
514 size -= copy_to_user(buf, current_rx_buf, size);
515 bytes_copied += size;
519 spin_lock_irqsave(&dev->lock, flags);
521 /* We've disconnected or reset so return. */
522 if (dev->reset_printer) {
523 list_add(¤t_rx_req->list, &dev->rx_reqs);
524 spin_unlock_irqrestore(&dev->lock, flags);
525 mutex_unlock(&dev->lock_printer_io);
529 /* If we not returning all the data left in this RX request
530 * buffer then adjust the amount of data left in the buffer.
531 * Othewise if we are done with this RX request buffer then
532 * requeue it to get any incoming data from the USB host.
534 if (size < current_rx_bytes) {
535 current_rx_bytes -= size;
536 current_rx_buf += size;
538 list_add(¤t_rx_req->list, &dev->rx_reqs);
539 current_rx_bytes = 0;
540 current_rx_buf = NULL;
541 current_rx_req = NULL;
545 dev->current_rx_req = current_rx_req;
546 dev->current_rx_bytes = current_rx_bytes;
547 dev->current_rx_buf = current_rx_buf;
549 spin_unlock_irqrestore(&dev->lock, flags);
550 mutex_unlock(&dev->lock_printer_io);
552 DBG(dev, "printer_read returned %d bytes\n", (int)bytes_copied);
561 printer_write(struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
563 struct printer_dev *dev = fd->private_data;
565 size_t size; /* Amount of data in a TX request. */
566 size_t bytes_copied = 0;
567 struct usb_request *req;
570 DBG(dev, "printer_write trying to send %d bytes\n", (int)len);
575 mutex_lock(&dev->lock_printer_io);
576 spin_lock_irqsave(&dev->lock, flags);
578 /* Check if a printer reset happens while we have interrupts on */
579 dev->reset_printer = 0;
581 /* Check if there is any available write buffers */
582 if (likely(list_empty(&dev->tx_reqs))) {
583 /* Turn interrupts back on before sleeping. */
584 spin_unlock_irqrestore(&dev->lock, flags);
587 * If write buffers are available check if this is
588 * a NON-Blocking call or not.
590 if (fd->f_flags & (O_NONBLOCK|O_NDELAY)) {
591 mutex_unlock(&dev->lock_printer_io);
595 /* Sleep until a write buffer is available */
596 wait_event_interruptible(dev->tx_wait,
597 (likely(!list_empty(&dev->tx_reqs))));
598 spin_lock_irqsave(&dev->lock, flags);
601 while (likely(!list_empty(&dev->tx_reqs)) && len) {
603 if (len > USB_BUFSIZE)
608 req = container_of(dev->tx_reqs.next, struct usb_request,
610 list_del_init(&req->list);
612 req->complete = tx_complete;
615 /* Check if we need to send a zero length packet. */
617 /* They will be more TX requests so no yet. */
620 /* If the data amount is not a multiple of the
621 * maxpacket size then send a zero length packet.
623 req->zero = ((len % dev->in_ep->maxpacket) == 0);
625 /* Don't leave irqs off while doing memory copies */
626 spin_unlock_irqrestore(&dev->lock, flags);
628 if (copy_from_user(req->buf, buf, size)) {
629 list_add(&req->list, &dev->tx_reqs);
630 mutex_unlock(&dev->lock_printer_io);
634 bytes_copied += size;
638 spin_lock_irqsave(&dev->lock, flags);
640 /* We've disconnected or reset so free the req and buffer */
641 if (dev->reset_printer) {
642 list_add(&req->list, &dev->tx_reqs);
643 spin_unlock_irqrestore(&dev->lock, flags);
644 mutex_unlock(&dev->lock_printer_io);
648 list_add(&req->list, &dev->tx_reqs_active);
650 /* here, we unlock, and only unlock, to avoid deadlock. */
651 spin_unlock(&dev->lock);
652 value = usb_ep_queue(dev->in_ep, req, GFP_ATOMIC);
653 spin_lock(&dev->lock);
655 list_del(&req->list);
656 list_add(&req->list, &dev->tx_reqs);
657 spin_unlock_irqrestore(&dev->lock, flags);
658 mutex_unlock(&dev->lock_printer_io);
663 spin_unlock_irqrestore(&dev->lock, flags);
664 mutex_unlock(&dev->lock_printer_io);
666 DBG(dev, "printer_write sent %d bytes\n", (int)bytes_copied);
675 printer_fsync(struct file *fd, loff_t start, loff_t end, int datasync)
677 struct printer_dev *dev = fd->private_data;
678 struct inode *inode = file_inode(fd);
683 spin_lock_irqsave(&dev->lock, flags);
684 tx_list_empty = (likely(list_empty(&dev->tx_reqs)));
685 spin_unlock_irqrestore(&dev->lock, flags);
687 if (!tx_list_empty) {
688 /* Sleep until all data has been sent */
689 wait_event_interruptible(dev->tx_flush_wait,
690 (likely(list_empty(&dev->tx_reqs_active))));
698 printer_poll(struct file *fd, poll_table *wait)
700 struct printer_dev *dev = fd->private_data;
704 mutex_lock(&dev->lock_printer_io);
705 spin_lock_irqsave(&dev->lock, flags);
707 spin_unlock_irqrestore(&dev->lock, flags);
708 mutex_unlock(&dev->lock_printer_io);
710 poll_wait(fd, &dev->rx_wait, wait);
711 poll_wait(fd, &dev->tx_wait, wait);
713 spin_lock_irqsave(&dev->lock, flags);
714 if (likely(!list_empty(&dev->tx_reqs)))
715 status |= POLLOUT | POLLWRNORM;
717 if (likely(dev->current_rx_bytes) ||
718 likely(!list_empty(&dev->rx_buffers)))
719 status |= POLLIN | POLLRDNORM;
721 spin_unlock_irqrestore(&dev->lock, flags);
727 printer_ioctl(struct file *fd, unsigned int code, unsigned long arg)
729 struct printer_dev *dev = fd->private_data;
733 DBG(dev, "printer_ioctl: cmd=0x%4.4x, arg=%lu\n", code, arg);
737 spin_lock_irqsave(&dev->lock, flags);
740 case GADGET_GET_PRINTER_STATUS:
741 status = (int)dev->printer_status;
743 case GADGET_SET_PRINTER_STATUS:
744 dev->printer_status = (u8)arg;
747 /* could not handle ioctl */
748 DBG(dev, "printer_ioctl: ERROR cmd=0x%4.4xis not supported\n",
753 spin_unlock_irqrestore(&dev->lock, flags);
758 /* used after endpoint configuration */
759 static const struct file_operations printer_io_operations = {
760 .owner = THIS_MODULE,
761 .open = printer_open,
762 .read = printer_read,
763 .write = printer_write,
764 .fsync = printer_fsync,
765 .poll = printer_poll,
766 .unlocked_ioctl = printer_ioctl,
767 .release = printer_close,
768 .llseek = noop_llseek,
771 /*-------------------------------------------------------------------------*/
774 set_printer_interface(struct printer_dev *dev)
778 dev->in_ep->desc = ep_desc(dev->gadget, &fs_ep_in_desc, &hs_ep_in_desc,
780 dev->in_ep->driver_data = dev;
782 dev->out_ep->desc = ep_desc(dev->gadget, &fs_ep_out_desc,
783 &hs_ep_out_desc, &ss_ep_out_desc);
784 dev->out_ep->driver_data = dev;
786 result = usb_ep_enable(dev->in_ep);
788 DBG(dev, "enable %s --> %d\n", dev->in_ep->name, result);
792 result = usb_ep_enable(dev->out_ep);
794 DBG(dev, "enable %s --> %d\n", dev->in_ep->name, result);
799 /* on error, disable any endpoints */
801 (void) usb_ep_disable(dev->in_ep);
802 (void) usb_ep_disable(dev->out_ep);
803 dev->in_ep->desc = NULL;
804 dev->out_ep->desc = NULL;
807 /* caller is responsible for cleanup on error */
811 static void printer_reset_interface(struct printer_dev *dev)
815 if (dev->interface < 0)
818 DBG(dev, "%s\n", __func__);
820 if (dev->in_ep->desc)
821 usb_ep_disable(dev->in_ep);
823 if (dev->out_ep->desc)
824 usb_ep_disable(dev->out_ep);
826 spin_lock_irqsave(&dev->lock, flags);
827 dev->in_ep->desc = NULL;
828 dev->out_ep->desc = NULL;
830 spin_unlock_irqrestore(&dev->lock, flags);
833 /* Change our operational Interface. */
834 static int set_interface(struct printer_dev *dev, unsigned number)
838 /* Free the current interface */
839 printer_reset_interface(dev);
841 result = set_printer_interface(dev);
843 printer_reset_interface(dev);
845 dev->interface = number;
848 INFO(dev, "Using interface %x\n", number);
853 static void printer_soft_reset(struct printer_dev *dev)
855 struct usb_request *req;
857 INFO(dev, "Received Printer Reset Request\n");
859 if (usb_ep_disable(dev->in_ep))
860 DBG(dev, "Failed to disable USB in_ep\n");
861 if (usb_ep_disable(dev->out_ep))
862 DBG(dev, "Failed to disable USB out_ep\n");
864 if (dev->current_rx_req != NULL) {
865 list_add(&dev->current_rx_req->list, &dev->rx_reqs);
866 dev->current_rx_req = NULL;
868 dev->current_rx_bytes = 0;
869 dev->current_rx_buf = NULL;
870 dev->reset_printer = 1;
872 while (likely(!(list_empty(&dev->rx_buffers)))) {
873 req = container_of(dev->rx_buffers.next, struct usb_request,
875 list_del_init(&req->list);
876 list_add(&req->list, &dev->rx_reqs);
879 while (likely(!(list_empty(&dev->rx_reqs_active)))) {
880 req = container_of(dev->rx_buffers.next, struct usb_request,
882 list_del_init(&req->list);
883 list_add(&req->list, &dev->rx_reqs);
886 while (likely(!(list_empty(&dev->tx_reqs_active)))) {
887 req = container_of(dev->tx_reqs_active.next,
888 struct usb_request, list);
889 list_del_init(&req->list);
890 list_add(&req->list, &dev->tx_reqs);
893 if (usb_ep_enable(dev->in_ep))
894 DBG(dev, "Failed to enable USB in_ep\n");
895 if (usb_ep_enable(dev->out_ep))
896 DBG(dev, "Failed to enable USB out_ep\n");
898 wake_up_interruptible(&dev->rx_wait);
899 wake_up_interruptible(&dev->tx_wait);
900 wake_up_interruptible(&dev->tx_flush_wait);
903 /*-------------------------------------------------------------------------*/
905 static bool gprinter_req_match(struct usb_function *f,
906 const struct usb_ctrlrequest *ctrl,
909 struct printer_dev *dev = func_to_printer(f);
910 u16 w_index = le16_to_cpu(ctrl->wIndex);
911 u16 w_value = le16_to_cpu(ctrl->wValue);
912 u16 w_length = le16_to_cpu(ctrl->wLength);
917 if ((ctrl->bRequestType & USB_RECIP_MASK) != USB_RECIP_INTERFACE ||
918 (ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS)
921 switch (ctrl->bRequest) {
924 if (USB_DIR_IN & ctrl->bRequestType)
927 case GET_PORT_STATUS:
928 if (!w_value && w_length == 1 &&
929 (USB_DIR_IN & ctrl->bRequestType))
933 if (!w_value && !w_length &&
934 !(USB_DIR_IN & ctrl->bRequestType))
940 return w_index == dev->interface;
944 * The setup() callback implements all the ep0 functionality that's not
945 * handled lower down.
947 static int printer_func_setup(struct usb_function *f,
948 const struct usb_ctrlrequest *ctrl)
950 struct printer_dev *dev = func_to_printer(f);
951 struct usb_composite_dev *cdev = f->config->cdev;
952 struct usb_request *req = cdev->req;
954 int value = -EOPNOTSUPP;
955 u16 wIndex = le16_to_cpu(ctrl->wIndex);
956 u16 wValue = le16_to_cpu(ctrl->wValue);
957 u16 wLength = le16_to_cpu(ctrl->wLength);
959 DBG(dev, "ctrl req%02x.%02x v%04x i%04x l%d\n",
960 ctrl->bRequestType, ctrl->bRequest, wValue, wIndex, wLength);
962 switch (ctrl->bRequestType&USB_TYPE_MASK) {
964 switch (ctrl->bRequest) {
965 case GET_DEVICE_ID: /* Get the IEEE-1284 PNP String */
966 /* Only one printer interface is supported. */
967 if ((wIndex>>8) != dev->interface)
970 if (!dev->pnp_string) {
974 value = strlen(dev->pnp_string);
975 buf[0] = (value >> 8) & 0xFF;
976 buf[1] = value & 0xFF;
977 memcpy(buf + 2, dev->pnp_string, value);
978 DBG(dev, "1284 PNP String: %x %s\n", value,
982 case GET_PORT_STATUS: /* Get Port Status */
983 /* Only one printer interface is supported. */
984 if (wIndex != dev->interface)
987 buf[0] = dev->printer_status;
988 value = min_t(u16, wLength, 1);
991 case SOFT_RESET: /* Soft Reset */
992 /* Only one printer interface is supported. */
993 if (wIndex != dev->interface)
996 printer_soft_reset(dev);
1009 "unknown ctrl req%02x.%02x v%04x i%04x l%d\n",
1010 ctrl->bRequestType, ctrl->bRequest,
1011 wValue, wIndex, wLength);
1014 /* host either stalls (value < 0) or reports success */
1016 req->length = value;
1017 req->zero = value < wLength;
1018 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
1020 ERROR(dev, "%s:%d Error!\n", __func__, __LINE__);
1027 static int printer_func_bind(struct usb_configuration *c,
1028 struct usb_function *f)
1030 struct usb_gadget *gadget = c->cdev->gadget;
1031 struct printer_dev *dev = func_to_printer(f);
1032 struct device *pdev;
1033 struct usb_composite_dev *cdev = c->cdev;
1034 struct usb_ep *in_ep;
1035 struct usb_ep *out_ep = NULL;
1036 struct usb_request *req;
1042 id = usb_interface_id(c, f);
1045 intf_desc.bInterfaceNumber = id;
1047 /* finish hookup to lower layer ... */
1048 dev->gadget = gadget;
1050 /* all we really need is bulk IN/OUT */
1051 in_ep = usb_ep_autoconfig(cdev->gadget, &fs_ep_in_desc);
1054 dev_err(&cdev->gadget->dev, "can't autoconfigure on %s\n",
1055 cdev->gadget->name);
1059 out_ep = usb_ep_autoconfig(cdev->gadget, &fs_ep_out_desc);
1063 /* assumes that all endpoints are dual-speed */
1064 hs_ep_in_desc.bEndpointAddress = fs_ep_in_desc.bEndpointAddress;
1065 hs_ep_out_desc.bEndpointAddress = fs_ep_out_desc.bEndpointAddress;
1066 ss_ep_in_desc.bEndpointAddress = fs_ep_in_desc.bEndpointAddress;
1067 ss_ep_out_desc.bEndpointAddress = fs_ep_out_desc.bEndpointAddress;
1069 ret = usb_assign_descriptors(f, fs_printer_function,
1070 hs_printer_function, ss_printer_function,
1071 ss_printer_function);
1076 dev->out_ep = out_ep;
1079 for (i = 0; i < dev->q_len; i++) {
1080 req = printer_req_alloc(dev->in_ep, USB_BUFSIZE, GFP_KERNEL);
1083 list_add(&req->list, &dev->tx_reqs);
1086 for (i = 0; i < dev->q_len; i++) {
1087 req = printer_req_alloc(dev->out_ep, USB_BUFSIZE, GFP_KERNEL);
1090 list_add(&req->list, &dev->rx_reqs);
1093 /* Setup the sysfs files for the printer gadget. */
1094 devt = MKDEV(major, dev->minor);
1095 pdev = device_create(usb_gadget_class, NULL, devt,
1096 NULL, "g_printer%d", dev->minor);
1098 ERROR(dev, "Failed to create device: g_printer\n");
1099 ret = PTR_ERR(pdev);
1104 * Register a character device as an interface to a user mode
1105 * program that handles the printer specific functionality.
1107 cdev_init(&dev->printer_cdev, &printer_io_operations);
1108 dev->printer_cdev.owner = THIS_MODULE;
1109 ret = cdev_add(&dev->printer_cdev, devt, 1);
1111 ERROR(dev, "Failed to open char device\n");
1118 device_destroy(usb_gadget_class, devt);
1121 while (!list_empty(&dev->rx_reqs)) {
1122 req = container_of(dev->rx_reqs.next, struct usb_request, list);
1123 list_del(&req->list);
1124 printer_req_free(dev->out_ep, req);
1128 while (!list_empty(&dev->tx_reqs)) {
1129 req = container_of(dev->tx_reqs.next, struct usb_request, list);
1130 list_del(&req->list);
1131 printer_req_free(dev->in_ep, req);
1134 usb_free_all_descriptors(f);
1139 static int printer_func_set_alt(struct usb_function *f,
1140 unsigned intf, unsigned alt)
1142 struct printer_dev *dev = func_to_printer(f);
1143 int ret = -ENOTSUPP;
1146 ret = set_interface(dev, intf);
1151 static void printer_func_disable(struct usb_function *f)
1153 struct printer_dev *dev = func_to_printer(f);
1155 DBG(dev, "%s\n", __func__);
1157 printer_reset_interface(dev);
1160 static inline struct f_printer_opts
1161 *to_f_printer_opts(struct config_item *item)
1163 return container_of(to_config_group(item), struct f_printer_opts,
1167 static void printer_attr_release(struct config_item *item)
1169 struct f_printer_opts *opts = to_f_printer_opts(item);
1171 usb_put_function_instance(&opts->func_inst);
1174 static struct configfs_item_operations printer_item_ops = {
1175 .release = printer_attr_release,
1178 static ssize_t f_printer_opts_pnp_string_show(struct config_item *item,
1181 struct f_printer_opts *opts = to_f_printer_opts(item);
1184 mutex_lock(&opts->lock);
1185 if (!opts->pnp_string)
1188 result = strlcpy(page, opts->pnp_string, PAGE_SIZE);
1189 if (result >= PAGE_SIZE) {
1191 } else if (page[result - 1] != '\n' && result + 1 < PAGE_SIZE) {
1192 page[result++] = '\n';
1193 page[result] = '\0';
1197 mutex_unlock(&opts->lock);
1202 static ssize_t f_printer_opts_pnp_string_store(struct config_item *item,
1203 const char *page, size_t len)
1205 struct f_printer_opts *opts = to_f_printer_opts(item);
1209 mutex_lock(&opts->lock);
1211 new_pnp = kstrndup(page, len, GFP_KERNEL);
1217 if (opts->pnp_string_allocated)
1218 kfree(opts->pnp_string);
1220 opts->pnp_string_allocated = true;
1221 opts->pnp_string = new_pnp;
1224 mutex_unlock(&opts->lock);
1229 CONFIGFS_ATTR(f_printer_opts_, pnp_string);
1231 static ssize_t f_printer_opts_q_len_show(struct config_item *item,
1234 struct f_printer_opts *opts = to_f_printer_opts(item);
1237 mutex_lock(&opts->lock);
1238 result = sprintf(page, "%d\n", opts->q_len);
1239 mutex_unlock(&opts->lock);
1244 static ssize_t f_printer_opts_q_len_store(struct config_item *item,
1245 const char *page, size_t len)
1247 struct f_printer_opts *opts = to_f_printer_opts(item);
1251 mutex_lock(&opts->lock);
1257 ret = kstrtou16(page, 0, &num);
1261 opts->q_len = (unsigned)num;
1264 mutex_unlock(&opts->lock);
1268 CONFIGFS_ATTR(f_printer_opts_, q_len);
1270 static struct configfs_attribute *printer_attrs[] = {
1271 &f_printer_opts_attr_pnp_string,
1272 &f_printer_opts_attr_q_len,
1276 static struct config_item_type printer_func_type = {
1277 .ct_item_ops = &printer_item_ops,
1278 .ct_attrs = printer_attrs,
1279 .ct_owner = THIS_MODULE,
1282 static inline int gprinter_get_minor(void)
1286 ret = ida_simple_get(&printer_ida, 0, 0, GFP_KERNEL);
1287 if (ret >= PRINTER_MINORS) {
1288 ida_simple_remove(&printer_ida, ret);
1295 static inline void gprinter_put_minor(int minor)
1297 ida_simple_remove(&printer_ida, minor);
1300 static int gprinter_setup(int);
1301 static void gprinter_cleanup(void);
1303 static void gprinter_free_inst(struct usb_function_instance *f)
1305 struct f_printer_opts *opts;
1307 opts = container_of(f, struct f_printer_opts, func_inst);
1309 mutex_lock(&printer_ida_lock);
1311 gprinter_put_minor(opts->minor);
1312 if (ida_is_empty(&printer_ida))
1315 mutex_unlock(&printer_ida_lock);
1317 if (opts->pnp_string_allocated)
1318 kfree(opts->pnp_string);
1322 static struct usb_function_instance *gprinter_alloc_inst(void)
1324 struct f_printer_opts *opts;
1325 struct usb_function_instance *ret;
1328 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
1330 return ERR_PTR(-ENOMEM);
1332 mutex_init(&opts->lock);
1333 opts->func_inst.free_func_inst = gprinter_free_inst;
1334 ret = &opts->func_inst;
1336 mutex_lock(&printer_ida_lock);
1338 if (ida_is_empty(&printer_ida)) {
1339 status = gprinter_setup(PRINTER_MINORS);
1341 ret = ERR_PTR(status);
1347 opts->minor = gprinter_get_minor();
1348 if (opts->minor < 0) {
1349 ret = ERR_PTR(opts->minor);
1351 if (ida_is_empty(&printer_ida))
1355 config_group_init_type_name(&opts->func_inst.group, "",
1356 &printer_func_type);
1359 mutex_unlock(&printer_ida_lock);
1363 static void gprinter_free(struct usb_function *f)
1365 struct printer_dev *dev = func_to_printer(f);
1366 struct f_printer_opts *opts;
1368 opts = container_of(f->fi, struct f_printer_opts, func_inst);
1370 kref_put(&dev->kref, printer_dev_free);
1371 mutex_lock(&opts->lock);
1373 mutex_unlock(&opts->lock);
1376 static void printer_func_unbind(struct usb_configuration *c,
1377 struct usb_function *f)
1379 struct printer_dev *dev;
1380 struct usb_request *req;
1382 dev = func_to_printer(f);
1384 device_destroy(usb_gadget_class, MKDEV(major, dev->minor));
1386 /* Remove Character Device */
1387 cdev_del(&dev->printer_cdev);
1389 /* we must already have been disconnected ... no i/o may be active */
1390 WARN_ON(!list_empty(&dev->tx_reqs_active));
1391 WARN_ON(!list_empty(&dev->rx_reqs_active));
1393 /* Free all memory for this driver. */
1394 while (!list_empty(&dev->tx_reqs)) {
1395 req = container_of(dev->tx_reqs.next, struct usb_request,
1397 list_del(&req->list);
1398 printer_req_free(dev->in_ep, req);
1401 if (dev->current_rx_req != NULL)
1402 printer_req_free(dev->out_ep, dev->current_rx_req);
1404 while (!list_empty(&dev->rx_reqs)) {
1405 req = container_of(dev->rx_reqs.next,
1406 struct usb_request, list);
1407 list_del(&req->list);
1408 printer_req_free(dev->out_ep, req);
1411 while (!list_empty(&dev->rx_buffers)) {
1412 req = container_of(dev->rx_buffers.next,
1413 struct usb_request, list);
1414 list_del(&req->list);
1415 printer_req_free(dev->out_ep, req);
1417 usb_free_all_descriptors(f);
1420 static struct usb_function *gprinter_alloc(struct usb_function_instance *fi)
1422 struct printer_dev *dev;
1423 struct f_printer_opts *opts;
1425 opts = container_of(fi, struct f_printer_opts, func_inst);
1427 mutex_lock(&opts->lock);
1428 if (opts->minor >= minors) {
1429 mutex_unlock(&opts->lock);
1430 return ERR_PTR(-ENOENT);
1433 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1435 mutex_unlock(&opts->lock);
1436 return ERR_PTR(-ENOMEM);
1439 kref_init(&dev->kref);
1441 dev->minor = opts->minor;
1442 dev->pnp_string = opts->pnp_string;
1443 dev->q_len = opts->q_len;
1444 mutex_unlock(&opts->lock);
1446 dev->function.name = "printer";
1447 dev->function.bind = printer_func_bind;
1448 dev->function.setup = printer_func_setup;
1449 dev->function.unbind = printer_func_unbind;
1450 dev->function.set_alt = printer_func_set_alt;
1451 dev->function.disable = printer_func_disable;
1452 dev->function.req_match = gprinter_req_match;
1453 dev->function.free_func = gprinter_free;
1455 INIT_LIST_HEAD(&dev->tx_reqs);
1456 INIT_LIST_HEAD(&dev->rx_reqs);
1457 INIT_LIST_HEAD(&dev->rx_buffers);
1458 INIT_LIST_HEAD(&dev->tx_reqs_active);
1459 INIT_LIST_HEAD(&dev->rx_reqs_active);
1461 spin_lock_init(&dev->lock);
1462 mutex_init(&dev->lock_printer_io);
1463 init_waitqueue_head(&dev->rx_wait);
1464 init_waitqueue_head(&dev->tx_wait);
1465 init_waitqueue_head(&dev->tx_flush_wait);
1467 dev->interface = -1;
1468 dev->printer_cdev_open = 0;
1469 dev->printer_status = PRINTER_NOT_ERROR;
1470 dev->current_rx_req = NULL;
1471 dev->current_rx_bytes = 0;
1472 dev->current_rx_buf = NULL;
1474 return &dev->function;
1477 DECLARE_USB_FUNCTION_INIT(printer, gprinter_alloc_inst, gprinter_alloc);
1478 MODULE_LICENSE("GPL");
1479 MODULE_AUTHOR("Craig Nadler");
1481 static int gprinter_setup(int count)
1486 usb_gadget_class = class_create(THIS_MODULE, "usb_printer_gadget");
1487 if (IS_ERR(usb_gadget_class)) {
1488 status = PTR_ERR(usb_gadget_class);
1489 usb_gadget_class = NULL;
1490 pr_err("unable to create usb_gadget class %d\n", status);
1494 status = alloc_chrdev_region(&devt, 0, count, "USB printer gadget");
1496 pr_err("alloc_chrdev_region %d\n", status);
1497 class_destroy(usb_gadget_class);
1498 usb_gadget_class = NULL;
1502 major = MAJOR(devt);
1508 static void gprinter_cleanup(void)
1511 unregister_chrdev_region(MKDEV(major, 0), minors);
1514 class_destroy(usb_gadget_class);
1515 usb_gadget_class = NULL;