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 PNP_STRING_LEN 1024
54 #define PRINTER_MINORS 4
55 #define GET_DEVICE_ID 0
56 #define GET_PORT_STATUS 1
59 static int major, minors;
60 static struct class *usb_gadget_class;
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->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 kref_get(&dev->kref);
365 DBG(dev, "printer_open returned %x\n", ret);
370 printer_close(struct inode *inode, struct file *fd)
372 struct printer_dev *dev = fd->private_data;
375 spin_lock_irqsave(&dev->lock, flags);
376 dev->printer_cdev_open = 0;
377 fd->private_data = NULL;
378 /* Change printer status to show that the printer is off-line. */
379 dev->printer_status &= ~PRINTER_SELECTED;
380 spin_unlock_irqrestore(&dev->lock, flags);
382 kref_put(&dev->kref, printer_dev_free);
383 DBG(dev, "printer_close\n");
388 /* This function must be called with interrupts turned off. */
390 setup_rx_reqs(struct printer_dev *dev)
392 struct usb_request *req;
394 while (likely(!list_empty(&dev->rx_reqs))) {
397 req = container_of(dev->rx_reqs.next,
398 struct usb_request, list);
399 list_del_init(&req->list);
401 /* The USB Host sends us whatever amount of data it wants to
402 * so we always set the length field to the full USB_BUFSIZE.
403 * If the amount of data is more than the read() caller asked
404 * for it will be stored in the request buffer until it is
405 * asked for by read().
407 req->length = USB_BUFSIZE;
408 req->complete = rx_complete;
410 /* here, we unlock, and only unlock, to avoid deadlock. */
411 spin_unlock(&dev->lock);
412 error = usb_ep_queue(dev->out_ep, req, GFP_ATOMIC);
413 spin_lock(&dev->lock);
415 DBG(dev, "rx submit --> %d\n", error);
416 list_add(&req->list, &dev->rx_reqs);
419 /* if the req is empty, then add it into dev->rx_reqs_active. */
420 else if (list_empty(&req->list))
421 list_add(&req->list, &dev->rx_reqs_active);
426 printer_read(struct file *fd, char __user *buf, size_t len, loff_t *ptr)
428 struct printer_dev *dev = fd->private_data;
432 struct usb_request *req;
433 /* This is a pointer to the current USB rx request. */
434 struct usb_request *current_rx_req;
435 /* This is the number of bytes in the current rx buffer. */
436 size_t current_rx_bytes;
437 /* This is a pointer to the current rx buffer. */
443 DBG(dev, "printer_read trying to read %d bytes\n", (int)len);
445 mutex_lock(&dev->lock_printer_io);
446 spin_lock_irqsave(&dev->lock, flags);
448 /* We will use this flag later to check if a printer reset happened
449 * after we turn interrupts back on.
451 dev->reset_printer = 0;
456 current_rx_req = dev->current_rx_req;
457 current_rx_bytes = dev->current_rx_bytes;
458 current_rx_buf = dev->current_rx_buf;
459 dev->current_rx_req = NULL;
460 dev->current_rx_bytes = 0;
461 dev->current_rx_buf = NULL;
463 /* Check if there is any data in the read buffers. Please note that
464 * current_rx_bytes is the number of bytes in the current rx buffer.
465 * If it is zero then check if there are any other rx_buffers that
466 * are on the completed list. We are only out of data if all rx
469 if ((current_rx_bytes == 0) &&
470 (likely(list_empty(&dev->rx_buffers)))) {
471 /* Turn interrupts back on before sleeping. */
472 spin_unlock_irqrestore(&dev->lock, flags);
475 * If no data is available check if this is a NON-Blocking
478 if (fd->f_flags & (O_NONBLOCK|O_NDELAY)) {
479 mutex_unlock(&dev->lock_printer_io);
483 /* Sleep until data is available */
484 wait_event_interruptible(dev->rx_wait,
485 (likely(!list_empty(&dev->rx_buffers))));
486 spin_lock_irqsave(&dev->lock, flags);
489 /* We have data to return then copy it to the caller's buffer.*/
490 while ((current_rx_bytes || likely(!list_empty(&dev->rx_buffers)))
492 if (current_rx_bytes == 0) {
493 req = container_of(dev->rx_buffers.next,
494 struct usb_request, list);
495 list_del_init(&req->list);
497 if (req->actual && req->buf) {
498 current_rx_req = req;
499 current_rx_bytes = req->actual;
500 current_rx_buf = req->buf;
502 list_add(&req->list, &dev->rx_reqs);
507 /* Don't leave irqs off while doing memory copies */
508 spin_unlock_irqrestore(&dev->lock, flags);
510 if (len > current_rx_bytes)
511 size = current_rx_bytes;
515 size -= copy_to_user(buf, current_rx_buf, size);
516 bytes_copied += size;
520 spin_lock_irqsave(&dev->lock, flags);
522 /* We've disconnected or reset so return. */
523 if (dev->reset_printer) {
524 list_add(¤t_rx_req->list, &dev->rx_reqs);
525 spin_unlock_irqrestore(&dev->lock, flags);
526 mutex_unlock(&dev->lock_printer_io);
530 /* If we not returning all the data left in this RX request
531 * buffer then adjust the amount of data left in the buffer.
532 * Othewise if we are done with this RX request buffer then
533 * requeue it to get any incoming data from the USB host.
535 if (size < current_rx_bytes) {
536 current_rx_bytes -= size;
537 current_rx_buf += size;
539 list_add(¤t_rx_req->list, &dev->rx_reqs);
540 current_rx_bytes = 0;
541 current_rx_buf = NULL;
542 current_rx_req = NULL;
546 dev->current_rx_req = current_rx_req;
547 dev->current_rx_bytes = current_rx_bytes;
548 dev->current_rx_buf = current_rx_buf;
550 spin_unlock_irqrestore(&dev->lock, flags);
551 mutex_unlock(&dev->lock_printer_io);
553 DBG(dev, "printer_read returned %d bytes\n", (int)bytes_copied);
562 printer_write(struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
564 struct printer_dev *dev = fd->private_data;
566 size_t size; /* Amount of data in a TX request. */
567 size_t bytes_copied = 0;
568 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 if (usb_ep_queue(dev->in_ep, req, GFP_ATOMIC)) {
649 list_add(&req->list, &dev->tx_reqs);
650 spin_unlock_irqrestore(&dev->lock, flags);
651 mutex_unlock(&dev->lock_printer_io);
655 list_add(&req->list, &dev->tx_reqs_active);
659 spin_unlock_irqrestore(&dev->lock, flags);
660 mutex_unlock(&dev->lock_printer_io);
662 DBG(dev, "printer_write sent %d bytes\n", (int)bytes_copied);
671 printer_fsync(struct file *fd, loff_t start, loff_t end, int datasync)
673 struct printer_dev *dev = fd->private_data;
674 struct inode *inode = file_inode(fd);
678 mutex_lock(&inode->i_mutex);
679 spin_lock_irqsave(&dev->lock, flags);
680 tx_list_empty = (likely(list_empty(&dev->tx_reqs)));
681 spin_unlock_irqrestore(&dev->lock, flags);
683 if (!tx_list_empty) {
684 /* Sleep until all data has been sent */
685 wait_event_interruptible(dev->tx_flush_wait,
686 (likely(list_empty(&dev->tx_reqs_active))));
688 mutex_unlock(&inode->i_mutex);
694 printer_poll(struct file *fd, poll_table *wait)
696 struct printer_dev *dev = fd->private_data;
700 mutex_lock(&dev->lock_printer_io);
701 spin_lock_irqsave(&dev->lock, flags);
703 spin_unlock_irqrestore(&dev->lock, flags);
704 mutex_unlock(&dev->lock_printer_io);
706 poll_wait(fd, &dev->rx_wait, wait);
707 poll_wait(fd, &dev->tx_wait, wait);
709 spin_lock_irqsave(&dev->lock, flags);
710 if (likely(!list_empty(&dev->tx_reqs)))
711 status |= POLLOUT | POLLWRNORM;
713 if (likely(dev->current_rx_bytes) ||
714 likely(!list_empty(&dev->rx_buffers)))
715 status |= POLLIN | POLLRDNORM;
717 spin_unlock_irqrestore(&dev->lock, flags);
723 printer_ioctl(struct file *fd, unsigned int code, unsigned long arg)
725 struct printer_dev *dev = fd->private_data;
729 DBG(dev, "printer_ioctl: cmd=0x%4.4x, arg=%lu\n", code, arg);
733 spin_lock_irqsave(&dev->lock, flags);
736 case GADGET_GET_PRINTER_STATUS:
737 status = (int)dev->printer_status;
739 case GADGET_SET_PRINTER_STATUS:
740 dev->printer_status = (u8)arg;
743 /* could not handle ioctl */
744 DBG(dev, "printer_ioctl: ERROR cmd=0x%4.4xis not supported\n",
749 spin_unlock_irqrestore(&dev->lock, flags);
754 /* used after endpoint configuration */
755 static const struct file_operations printer_io_operations = {
756 .owner = THIS_MODULE,
757 .open = printer_open,
758 .read = printer_read,
759 .write = printer_write,
760 .fsync = printer_fsync,
761 .poll = printer_poll,
762 .unlocked_ioctl = printer_ioctl,
763 .release = printer_close,
764 .llseek = noop_llseek,
767 /*-------------------------------------------------------------------------*/
770 set_printer_interface(struct printer_dev *dev)
774 dev->in_ep->desc = ep_desc(dev->gadget, &fs_ep_in_desc, &hs_ep_in_desc,
776 dev->in_ep->driver_data = dev;
778 dev->out_ep->desc = ep_desc(dev->gadget, &fs_ep_out_desc,
779 &hs_ep_out_desc, &ss_ep_out_desc);
780 dev->out_ep->driver_data = dev;
782 result = usb_ep_enable(dev->in_ep);
784 DBG(dev, "enable %s --> %d\n", dev->in_ep->name, result);
788 result = usb_ep_enable(dev->out_ep);
790 DBG(dev, "enable %s --> %d\n", dev->in_ep->name, result);
795 /* on error, disable any endpoints */
797 (void) usb_ep_disable(dev->in_ep);
798 (void) usb_ep_disable(dev->out_ep);
799 dev->in_ep->desc = NULL;
800 dev->out_ep->desc = NULL;
803 /* caller is responsible for cleanup on error */
807 static void printer_reset_interface(struct printer_dev *dev)
811 if (dev->interface < 0)
814 DBG(dev, "%s\n", __func__);
816 if (dev->in_ep->desc)
817 usb_ep_disable(dev->in_ep);
819 if (dev->out_ep->desc)
820 usb_ep_disable(dev->out_ep);
822 spin_lock_irqsave(&dev->lock, flags);
823 dev->in_ep->desc = NULL;
824 dev->out_ep->desc = NULL;
826 spin_unlock_irqrestore(&dev->lock, flags);
829 /* Change our operational Interface. */
830 static int set_interface(struct printer_dev *dev, unsigned number)
834 /* Free the current interface */
835 printer_reset_interface(dev);
837 result = set_printer_interface(dev);
839 printer_reset_interface(dev);
841 dev->interface = number;
844 INFO(dev, "Using interface %x\n", number);
849 static void printer_soft_reset(struct printer_dev *dev)
851 struct usb_request *req;
853 INFO(dev, "Received Printer Reset Request\n");
855 if (usb_ep_disable(dev->in_ep))
856 DBG(dev, "Failed to disable USB in_ep\n");
857 if (usb_ep_disable(dev->out_ep))
858 DBG(dev, "Failed to disable USB out_ep\n");
860 if (dev->current_rx_req != NULL) {
861 list_add(&dev->current_rx_req->list, &dev->rx_reqs);
862 dev->current_rx_req = NULL;
864 dev->current_rx_bytes = 0;
865 dev->current_rx_buf = NULL;
866 dev->reset_printer = 1;
868 while (likely(!(list_empty(&dev->rx_buffers)))) {
869 req = container_of(dev->rx_buffers.next, struct usb_request,
871 list_del_init(&req->list);
872 list_add(&req->list, &dev->rx_reqs);
875 while (likely(!(list_empty(&dev->rx_reqs_active)))) {
876 req = container_of(dev->rx_buffers.next, struct usb_request,
878 list_del_init(&req->list);
879 list_add(&req->list, &dev->rx_reqs);
882 while (likely(!(list_empty(&dev->tx_reqs_active)))) {
883 req = container_of(dev->tx_reqs_active.next,
884 struct usb_request, list);
885 list_del_init(&req->list);
886 list_add(&req->list, &dev->tx_reqs);
889 if (usb_ep_enable(dev->in_ep))
890 DBG(dev, "Failed to enable USB in_ep\n");
891 if (usb_ep_enable(dev->out_ep))
892 DBG(dev, "Failed to enable USB out_ep\n");
894 wake_up_interruptible(&dev->rx_wait);
895 wake_up_interruptible(&dev->tx_wait);
896 wake_up_interruptible(&dev->tx_flush_wait);
899 /*-------------------------------------------------------------------------*/
901 static bool gprinter_req_match(struct usb_function *f,
902 const struct usb_ctrlrequest *ctrl)
904 struct printer_dev *dev = func_to_printer(f);
905 u16 w_index = le16_to_cpu(ctrl->wIndex);
906 u16 w_value = le16_to_cpu(ctrl->wValue);
907 u16 w_length = le16_to_cpu(ctrl->wLength);
909 if ((ctrl->bRequestType & USB_RECIP_MASK) != USB_RECIP_INTERFACE ||
910 (ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS)
913 switch (ctrl->bRequest) {
916 if (w_length <= PNP_STRING_LEN &&
917 (USB_DIR_IN & ctrl->bRequestType))
920 case GET_PORT_STATUS:
921 if (!w_value && w_length == 1 &&
922 (USB_DIR_IN & ctrl->bRequestType))
926 if (!w_value && !w_length &&
927 !(USB_DIR_IN & ctrl->bRequestType))
933 return w_index == dev->interface;
937 * The setup() callback implements all the ep0 functionality that's not
938 * handled lower down.
940 static int printer_func_setup(struct usb_function *f,
941 const struct usb_ctrlrequest *ctrl)
943 struct printer_dev *dev = func_to_printer(f);
944 struct usb_composite_dev *cdev = f->config->cdev;
945 struct usb_request *req = cdev->req;
946 int value = -EOPNOTSUPP;
947 u16 wIndex = le16_to_cpu(ctrl->wIndex);
948 u16 wValue = le16_to_cpu(ctrl->wValue);
949 u16 wLength = le16_to_cpu(ctrl->wLength);
951 DBG(dev, "ctrl req%02x.%02x v%04x i%04x l%d\n",
952 ctrl->bRequestType, ctrl->bRequest, wValue, wIndex, wLength);
954 switch (ctrl->bRequestType&USB_TYPE_MASK) {
956 switch (ctrl->bRequest) {
957 case GET_DEVICE_ID: /* Get the IEEE-1284 PNP String */
958 /* Only one printer interface is supported. */
959 if ((wIndex>>8) != dev->interface)
962 value = (dev->pnp_string[0] << 8) | dev->pnp_string[1];
963 memcpy(req->buf, dev->pnp_string, value);
964 DBG(dev, "1284 PNP String: %x %s\n", value,
965 &dev->pnp_string[2]);
968 case GET_PORT_STATUS: /* Get Port Status */
969 /* Only one printer interface is supported. */
970 if (wIndex != dev->interface)
973 *(u8 *)req->buf = dev->printer_status;
974 value = min_t(u16, wLength, 1);
977 case SOFT_RESET: /* Soft Reset */
978 /* Only one printer interface is supported. */
979 if (wIndex != dev->interface)
982 printer_soft_reset(dev);
995 "unknown ctrl req%02x.%02x v%04x i%04x l%d\n",
996 ctrl->bRequestType, ctrl->bRequest,
997 wValue, wIndex, wLength);
1000 /* host either stalls (value < 0) or reports success */
1002 req->length = value;
1003 req->zero = value < wLength;
1004 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
1006 ERROR(dev, "%s:%d Error!\n", __func__, __LINE__);
1013 static int printer_func_bind(struct usb_configuration *c,
1014 struct usb_function *f)
1016 struct usb_gadget *gadget = c->cdev->gadget;
1017 struct printer_dev *dev = func_to_printer(f);
1018 struct device *pdev;
1019 struct usb_composite_dev *cdev = c->cdev;
1020 struct usb_ep *in_ep;
1021 struct usb_ep *out_ep = NULL;
1022 struct usb_request *req;
1028 id = usb_interface_id(c, f);
1031 intf_desc.bInterfaceNumber = id;
1033 /* finish hookup to lower layer ... */
1034 dev->gadget = gadget;
1036 /* all we really need is bulk IN/OUT */
1037 in_ep = usb_ep_autoconfig(cdev->gadget, &fs_ep_in_desc);
1040 dev_err(&cdev->gadget->dev, "can't autoconfigure on %s\n",
1041 cdev->gadget->name);
1045 out_ep = usb_ep_autoconfig(cdev->gadget, &fs_ep_out_desc);
1049 /* assumes that all endpoints are dual-speed */
1050 hs_ep_in_desc.bEndpointAddress = fs_ep_in_desc.bEndpointAddress;
1051 hs_ep_out_desc.bEndpointAddress = fs_ep_out_desc.bEndpointAddress;
1052 ss_ep_in_desc.bEndpointAddress = fs_ep_in_desc.bEndpointAddress;
1053 ss_ep_out_desc.bEndpointAddress = fs_ep_out_desc.bEndpointAddress;
1055 ret = usb_assign_descriptors(f, fs_printer_function,
1056 hs_printer_function, ss_printer_function);
1061 dev->out_ep = out_ep;
1064 for (i = 0; i < dev->q_len; i++) {
1065 req = printer_req_alloc(dev->in_ep, USB_BUFSIZE, GFP_KERNEL);
1068 list_add(&req->list, &dev->tx_reqs);
1071 for (i = 0; i < dev->q_len; i++) {
1072 req = printer_req_alloc(dev->out_ep, USB_BUFSIZE, GFP_KERNEL);
1075 list_add(&req->list, &dev->rx_reqs);
1078 /* Setup the sysfs files for the printer gadget. */
1079 devt = MKDEV(major, dev->minor);
1080 pdev = device_create(usb_gadget_class, NULL, devt,
1081 NULL, "g_printer%d", dev->minor);
1083 ERROR(dev, "Failed to create device: g_printer\n");
1084 ret = PTR_ERR(pdev);
1089 * Register a character device as an interface to a user mode
1090 * program that handles the printer specific functionality.
1092 cdev_init(&dev->printer_cdev, &printer_io_operations);
1093 dev->printer_cdev.owner = THIS_MODULE;
1094 ret = cdev_add(&dev->printer_cdev, devt, 1);
1096 ERROR(dev, "Failed to open char device\n");
1103 device_destroy(usb_gadget_class, devt);
1106 while (!list_empty(&dev->rx_reqs)) {
1107 req = container_of(dev->rx_reqs.next, struct usb_request, list);
1108 list_del(&req->list);
1109 printer_req_free(dev->out_ep, req);
1113 while (!list_empty(&dev->tx_reqs)) {
1114 req = container_of(dev->tx_reqs.next, struct usb_request, list);
1115 list_del(&req->list);
1116 printer_req_free(dev->in_ep, req);
1119 usb_free_all_descriptors(f);
1124 static int printer_func_set_alt(struct usb_function *f,
1125 unsigned intf, unsigned alt)
1127 struct printer_dev *dev = func_to_printer(f);
1128 int ret = -ENOTSUPP;
1131 ret = set_interface(dev, intf);
1136 static void printer_func_disable(struct usb_function *f)
1138 struct printer_dev *dev = func_to_printer(f);
1140 DBG(dev, "%s\n", __func__);
1142 printer_reset_interface(dev);
1145 static inline struct f_printer_opts
1146 *to_f_printer_opts(struct config_item *item)
1148 return container_of(to_config_group(item), struct f_printer_opts,
1152 static void printer_attr_release(struct config_item *item)
1154 struct f_printer_opts *opts = to_f_printer_opts(item);
1156 usb_put_function_instance(&opts->func_inst);
1159 static struct configfs_item_operations printer_item_ops = {
1160 .release = printer_attr_release,
1163 static ssize_t f_printer_opts_pnp_string_show(struct config_item *item,
1166 struct f_printer_opts *opts = to_f_printer_opts(item);
1169 mutex_lock(&opts->lock);
1170 result = strlcpy(page, opts->pnp_string + 2, PNP_STRING_LEN - 2);
1171 mutex_unlock(&opts->lock);
1176 static ssize_t f_printer_opts_pnp_string_store(struct config_item *item,
1177 const char *page, size_t len)
1179 struct f_printer_opts *opts = to_f_printer_opts(item);
1182 mutex_lock(&opts->lock);
1183 result = strlcpy(opts->pnp_string + 2, page, PNP_STRING_LEN - 2);
1184 l = strlen(opts->pnp_string + 2) + 2;
1185 opts->pnp_string[0] = (l >> 8) & 0xFF;
1186 opts->pnp_string[1] = l & 0xFF;
1187 mutex_unlock(&opts->lock);
1192 CONFIGFS_ATTR(f_printer_opts_, pnp_string);
1194 static ssize_t f_printer_opts_q_len_show(struct config_item *item,
1197 struct f_printer_opts *opts = to_f_printer_opts(item);
1200 mutex_lock(&opts->lock);
1201 result = sprintf(page, "%d\n", opts->q_len);
1202 mutex_unlock(&opts->lock);
1207 static ssize_t f_printer_opts_q_len_store(struct config_item *item,
1208 const char *page, size_t len)
1210 struct f_printer_opts *opts = to_f_printer_opts(item);
1214 mutex_lock(&opts->lock);
1220 ret = kstrtou16(page, 0, &num);
1224 opts->q_len = (unsigned)num;
1227 mutex_unlock(&opts->lock);
1231 CONFIGFS_ATTR(f_printer_opts_, q_len);
1233 static struct configfs_attribute *printer_attrs[] = {
1234 &f_printer_opts_attr_pnp_string,
1235 &f_printer_opts_attr_q_len,
1239 static struct config_item_type printer_func_type = {
1240 .ct_item_ops = &printer_item_ops,
1241 .ct_attrs = printer_attrs,
1242 .ct_owner = THIS_MODULE,
1245 static inline int gprinter_get_minor(void)
1249 ret = ida_simple_get(&printer_ida, 0, 0, GFP_KERNEL);
1250 if (ret >= PRINTER_MINORS) {
1251 ida_simple_remove(&printer_ida, ret);
1258 static inline void gprinter_put_minor(int minor)
1260 ida_simple_remove(&printer_ida, minor);
1263 static int gprinter_setup(int);
1264 static void gprinter_cleanup(void);
1266 static void gprinter_free_inst(struct usb_function_instance *f)
1268 struct f_printer_opts *opts;
1270 opts = container_of(f, struct f_printer_opts, func_inst);
1272 mutex_lock(&printer_ida_lock);
1274 gprinter_put_minor(opts->minor);
1275 if (idr_is_empty(&printer_ida.idr))
1278 mutex_unlock(&printer_ida_lock);
1283 static struct usb_function_instance *gprinter_alloc_inst(void)
1285 struct f_printer_opts *opts;
1286 struct usb_function_instance *ret;
1289 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
1291 return ERR_PTR(-ENOMEM);
1293 mutex_init(&opts->lock);
1294 opts->func_inst.free_func_inst = gprinter_free_inst;
1295 ret = &opts->func_inst;
1297 mutex_lock(&printer_ida_lock);
1299 if (idr_is_empty(&printer_ida.idr)) {
1300 status = gprinter_setup(PRINTER_MINORS);
1302 ret = ERR_PTR(status);
1308 opts->minor = gprinter_get_minor();
1309 if (opts->minor < 0) {
1310 ret = ERR_PTR(opts->minor);
1312 if (idr_is_empty(&printer_ida.idr))
1316 config_group_init_type_name(&opts->func_inst.group, "",
1317 &printer_func_type);
1320 mutex_unlock(&printer_ida_lock);
1324 static void gprinter_free(struct usb_function *f)
1326 struct printer_dev *dev = func_to_printer(f);
1327 struct f_printer_opts *opts;
1329 opts = container_of(f->fi, struct f_printer_opts, func_inst);
1331 kref_put(&dev->kref, printer_dev_free);
1332 mutex_lock(&opts->lock);
1334 mutex_unlock(&opts->lock);
1337 static void printer_func_unbind(struct usb_configuration *c,
1338 struct usb_function *f)
1340 struct printer_dev *dev;
1341 struct usb_request *req;
1343 dev = func_to_printer(f);
1345 device_destroy(usb_gadget_class, MKDEV(major, dev->minor));
1347 /* Remove Character Device */
1348 cdev_del(&dev->printer_cdev);
1350 /* we must already have been disconnected ... no i/o may be active */
1351 WARN_ON(!list_empty(&dev->tx_reqs_active));
1352 WARN_ON(!list_empty(&dev->rx_reqs_active));
1354 /* Free all memory for this driver. */
1355 while (!list_empty(&dev->tx_reqs)) {
1356 req = container_of(dev->tx_reqs.next, struct usb_request,
1358 list_del(&req->list);
1359 printer_req_free(dev->in_ep, req);
1362 if (dev->current_rx_req != NULL)
1363 printer_req_free(dev->out_ep, dev->current_rx_req);
1365 while (!list_empty(&dev->rx_reqs)) {
1366 req = container_of(dev->rx_reqs.next,
1367 struct usb_request, list);
1368 list_del(&req->list);
1369 printer_req_free(dev->out_ep, req);
1372 while (!list_empty(&dev->rx_buffers)) {
1373 req = container_of(dev->rx_buffers.next,
1374 struct usb_request, list);
1375 list_del(&req->list);
1376 printer_req_free(dev->out_ep, req);
1378 usb_free_all_descriptors(f);
1381 static struct usb_function *gprinter_alloc(struct usb_function_instance *fi)
1383 struct printer_dev *dev;
1384 struct f_printer_opts *opts;
1386 opts = container_of(fi, struct f_printer_opts, func_inst);
1388 mutex_lock(&opts->lock);
1389 if (opts->minor >= minors) {
1390 mutex_unlock(&opts->lock);
1391 return ERR_PTR(-ENOENT);
1394 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1396 mutex_unlock(&opts->lock);
1397 return ERR_PTR(-ENOMEM);
1400 kref_init(&dev->kref);
1402 dev->minor = opts->minor;
1403 dev->pnp_string = opts->pnp_string;
1404 dev->q_len = opts->q_len;
1405 mutex_unlock(&opts->lock);
1407 dev->function.name = "printer";
1408 dev->function.bind = printer_func_bind;
1409 dev->function.setup = printer_func_setup;
1410 dev->function.unbind = printer_func_unbind;
1411 dev->function.set_alt = printer_func_set_alt;
1412 dev->function.disable = printer_func_disable;
1413 dev->function.req_match = gprinter_req_match;
1414 dev->function.free_func = gprinter_free;
1416 INIT_LIST_HEAD(&dev->tx_reqs);
1417 INIT_LIST_HEAD(&dev->rx_reqs);
1418 INIT_LIST_HEAD(&dev->rx_buffers);
1419 INIT_LIST_HEAD(&dev->tx_reqs_active);
1420 INIT_LIST_HEAD(&dev->rx_reqs_active);
1422 spin_lock_init(&dev->lock);
1423 mutex_init(&dev->lock_printer_io);
1424 init_waitqueue_head(&dev->rx_wait);
1425 init_waitqueue_head(&dev->tx_wait);
1426 init_waitqueue_head(&dev->tx_flush_wait);
1428 dev->interface = -1;
1429 dev->printer_cdev_open = 0;
1430 dev->printer_status = PRINTER_NOT_ERROR;
1431 dev->current_rx_req = NULL;
1432 dev->current_rx_bytes = 0;
1433 dev->current_rx_buf = NULL;
1435 return &dev->function;
1438 DECLARE_USB_FUNCTION_INIT(printer, gprinter_alloc_inst, gprinter_alloc);
1439 MODULE_LICENSE("GPL");
1440 MODULE_AUTHOR("Craig Nadler");
1442 static int gprinter_setup(int count)
1447 usb_gadget_class = class_create(THIS_MODULE, "usb_printer_gadget");
1448 if (IS_ERR(usb_gadget_class)) {
1449 status = PTR_ERR(usb_gadget_class);
1450 usb_gadget_class = NULL;
1451 pr_err("unable to create usb_gadget class %d\n", status);
1455 status = alloc_chrdev_region(&devt, 0, count, "USB printer gadget");
1457 pr_err("alloc_chrdev_region %d\n", status);
1458 class_destroy(usb_gadget_class);
1459 usb_gadget_class = NULL;
1463 major = MAJOR(devt);
1469 static void gprinter_cleanup(void)
1472 unregister_chrdev_region(MKDEV(major, 0), minors);
1475 class_destroy(usb_gadget_class);
1476 usb_gadget_class = NULL;