Loading...
1/*
2 * printer.c -- Printer gadget driver
3 *
4 * Copyright (C) 2003-2005 David Brownell
5 * Copyright (C) 2006 Craig W. Nadler
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 */
12
13#include <linux/module.h>
14#include <linux/kernel.h>
15#include <linux/delay.h>
16#include <linux/ioport.h>
17#include <linux/sched.h>
18#include <linux/slab.h>
19#include <linux/mutex.h>
20#include <linux/errno.h>
21#include <linux/init.h>
22#include <linux/timer.h>
23#include <linux/list.h>
24#include <linux/interrupt.h>
25#include <linux/utsname.h>
26#include <linux/device.h>
27#include <linux/moduleparam.h>
28#include <linux/fs.h>
29#include <linux/poll.h>
30#include <linux/types.h>
31#include <linux/ctype.h>
32#include <linux/cdev.h>
33
34#include <asm/byteorder.h>
35#include <linux/io.h>
36#include <linux/irq.h>
37#include <linux/uaccess.h>
38#include <asm/unaligned.h>
39
40#include <linux/usb/ch9.h>
41#include <linux/usb/gadget.h>
42#include <linux/usb/g_printer.h>
43
44#include "gadget_chips.h"
45
46
47/*
48 * Kbuild is not very cooperative with respect to linking separately
49 * compiled library objects into one module. So for now we won't use
50 * separate compilation ... ensuring init/exit sections work to shrink
51 * the runtime footprint, and giving us at least some parts of what
52 * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
53 */
54#include "composite.c"
55#include "usbstring.c"
56#include "config.c"
57#include "epautoconf.c"
58
59/*-------------------------------------------------------------------------*/
60
61#define DRIVER_DESC "Printer Gadget"
62#define DRIVER_VERSION "2007 OCT 06"
63
64static DEFINE_MUTEX(printer_mutex);
65static const char shortname [] = "printer";
66static const char driver_desc [] = DRIVER_DESC;
67
68static dev_t g_printer_devno;
69
70static struct class *usb_gadget_class;
71
72/*-------------------------------------------------------------------------*/
73
74struct printer_dev {
75 spinlock_t lock; /* lock this structure */
76 /* lock buffer lists during read/write calls */
77 struct mutex lock_printer_io;
78 struct usb_gadget *gadget;
79 s8 interface;
80 struct usb_ep *in_ep, *out_ep;
81
82 struct list_head rx_reqs; /* List of free RX structs */
83 struct list_head rx_reqs_active; /* List of Active RX xfers */
84 struct list_head rx_buffers; /* List of completed xfers */
85 /* wait until there is data to be read. */
86 wait_queue_head_t rx_wait;
87 struct list_head tx_reqs; /* List of free TX structs */
88 struct list_head tx_reqs_active; /* List of Active TX xfers */
89 /* Wait until there are write buffers available to use. */
90 wait_queue_head_t tx_wait;
91 /* Wait until all write buffers have been sent. */
92 wait_queue_head_t tx_flush_wait;
93 struct usb_request *current_rx_req;
94 size_t current_rx_bytes;
95 u8 *current_rx_buf;
96 u8 printer_status;
97 u8 reset_printer;
98 struct cdev printer_cdev;
99 struct device *pdev;
100 u8 printer_cdev_open;
101 wait_queue_head_t wait;
102 struct usb_function function;
103};
104
105static struct printer_dev usb_printer_gadget;
106
107/*-------------------------------------------------------------------------*/
108
109/* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
110 * Instead: allocate your own, using normal USB-IF procedures.
111 */
112
113/* Thanks to NetChip Technologies for donating this product ID.
114 */
115#define PRINTER_VENDOR_NUM 0x0525 /* NetChip */
116#define PRINTER_PRODUCT_NUM 0xa4a8 /* Linux-USB Printer Gadget */
117
118/* Some systems will want different product identifiers published in the
119 * device descriptor, either numbers or strings or both. These string
120 * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
121 */
122
123static char *iSerialNum;
124module_param(iSerialNum, charp, S_IRUGO);
125MODULE_PARM_DESC(iSerialNum, "1");
126
127static char *iPNPstring;
128module_param(iPNPstring, charp, S_IRUGO);
129MODULE_PARM_DESC(iPNPstring, "MFG:linux;MDL:g_printer;CLS:PRINTER;SN:1;");
130
131/* Number of requests to allocate per endpoint, not used for ep0. */
132static unsigned qlen = 10;
133module_param(qlen, uint, S_IRUGO|S_IWUSR);
134
135#define QLEN qlen
136
137/*-------------------------------------------------------------------------*/
138
139/*
140 * DESCRIPTORS ... most are static, but strings and (full) configuration
141 * descriptors are built on demand.
142 */
143
144#define STRING_MANUFACTURER 0
145#define STRING_PRODUCT 1
146#define STRING_SERIALNUM 2
147
148/* holds our biggest descriptor */
149#define USB_DESC_BUFSIZE 256
150#define USB_BUFSIZE 8192
151
152static struct usb_device_descriptor device_desc = {
153 .bLength = sizeof device_desc,
154 .bDescriptorType = USB_DT_DEVICE,
155 .bcdUSB = cpu_to_le16(0x0200),
156 .bDeviceClass = USB_CLASS_PER_INTERFACE,
157 .bDeviceSubClass = 0,
158 .bDeviceProtocol = 0,
159 .idVendor = cpu_to_le16(PRINTER_VENDOR_NUM),
160 .idProduct = cpu_to_le16(PRINTER_PRODUCT_NUM),
161 .bNumConfigurations = 1
162};
163
164static struct usb_interface_descriptor intf_desc = {
165 .bLength = sizeof intf_desc,
166 .bDescriptorType = USB_DT_INTERFACE,
167 .bNumEndpoints = 2,
168 .bInterfaceClass = USB_CLASS_PRINTER,
169 .bInterfaceSubClass = 1, /* Printer Sub-Class */
170 .bInterfaceProtocol = 2, /* Bi-Directional */
171 .iInterface = 0
172};
173
174static struct usb_endpoint_descriptor fs_ep_in_desc = {
175 .bLength = USB_DT_ENDPOINT_SIZE,
176 .bDescriptorType = USB_DT_ENDPOINT,
177 .bEndpointAddress = USB_DIR_IN,
178 .bmAttributes = USB_ENDPOINT_XFER_BULK
179};
180
181static struct usb_endpoint_descriptor fs_ep_out_desc = {
182 .bLength = USB_DT_ENDPOINT_SIZE,
183 .bDescriptorType = USB_DT_ENDPOINT,
184 .bEndpointAddress = USB_DIR_OUT,
185 .bmAttributes = USB_ENDPOINT_XFER_BULK
186};
187
188static struct usb_descriptor_header *fs_printer_function[] = {
189 (struct usb_descriptor_header *) &intf_desc,
190 (struct usb_descriptor_header *) &fs_ep_in_desc,
191 (struct usb_descriptor_header *) &fs_ep_out_desc,
192 NULL
193};
194
195/*
196 * usb 2.0 devices need to expose both high speed and full speed
197 * descriptors, unless they only run at full speed.
198 */
199
200static struct usb_endpoint_descriptor hs_ep_in_desc = {
201 .bLength = USB_DT_ENDPOINT_SIZE,
202 .bDescriptorType = USB_DT_ENDPOINT,
203 .bmAttributes = USB_ENDPOINT_XFER_BULK,
204 .wMaxPacketSize = cpu_to_le16(512)
205};
206
207static struct usb_endpoint_descriptor hs_ep_out_desc = {
208 .bLength = USB_DT_ENDPOINT_SIZE,
209 .bDescriptorType = USB_DT_ENDPOINT,
210 .bmAttributes = USB_ENDPOINT_XFER_BULK,
211 .wMaxPacketSize = cpu_to_le16(512)
212};
213
214static struct usb_qualifier_descriptor dev_qualifier = {
215 .bLength = sizeof dev_qualifier,
216 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
217 .bcdUSB = cpu_to_le16(0x0200),
218 .bDeviceClass = USB_CLASS_PRINTER,
219 .bNumConfigurations = 1
220};
221
222static struct usb_descriptor_header *hs_printer_function[] = {
223 (struct usb_descriptor_header *) &intf_desc,
224 (struct usb_descriptor_header *) &hs_ep_in_desc,
225 (struct usb_descriptor_header *) &hs_ep_out_desc,
226 NULL
227};
228
229static struct usb_otg_descriptor otg_descriptor = {
230 .bLength = sizeof otg_descriptor,
231 .bDescriptorType = USB_DT_OTG,
232 .bmAttributes = USB_OTG_SRP,
233};
234
235static const struct usb_descriptor_header *otg_desc[] = {
236 (struct usb_descriptor_header *) &otg_descriptor,
237 NULL,
238};
239
240/* maxpacket and other transfer characteristics vary by speed. */
241#define ep_desc(g, hs, fs) (((g)->speed == USB_SPEED_HIGH)?(hs):(fs))
242
243/*-------------------------------------------------------------------------*/
244
245/* descriptors that are built on-demand */
246
247static char manufacturer [50];
248static char product_desc [40] = DRIVER_DESC;
249static char serial_num [40] = "1";
250static char pnp_string [1024] =
251 "XXMFG:linux;MDL:g_printer;CLS:PRINTER;SN:1;";
252
253/* static strings, in UTF-8 */
254static struct usb_string strings [] = {
255 [STRING_MANUFACTURER].s = manufacturer,
256 [STRING_PRODUCT].s = product_desc,
257 [STRING_SERIALNUM].s = serial_num,
258 { } /* end of list */
259};
260
261static struct usb_gadget_strings stringtab_dev = {
262 .language = 0x0409, /* en-us */
263 .strings = strings,
264};
265
266static struct usb_gadget_strings *dev_strings[] = {
267 &stringtab_dev,
268 NULL,
269};
270
271/*-------------------------------------------------------------------------*/
272
273static struct usb_request *
274printer_req_alloc(struct usb_ep *ep, unsigned len, gfp_t gfp_flags)
275{
276 struct usb_request *req;
277
278 req = usb_ep_alloc_request(ep, gfp_flags);
279
280 if (req != NULL) {
281 req->length = len;
282 req->buf = kmalloc(len, gfp_flags);
283 if (req->buf == NULL) {
284 usb_ep_free_request(ep, req);
285 return NULL;
286 }
287 }
288
289 return req;
290}
291
292static void
293printer_req_free(struct usb_ep *ep, struct usb_request *req)
294{
295 if (ep != NULL && req != NULL) {
296 kfree(req->buf);
297 usb_ep_free_request(ep, req);
298 }
299}
300
301/*-------------------------------------------------------------------------*/
302
303static void rx_complete(struct usb_ep *ep, struct usb_request *req)
304{
305 struct printer_dev *dev = ep->driver_data;
306 int status = req->status;
307 unsigned long flags;
308
309 spin_lock_irqsave(&dev->lock, flags);
310
311 list_del_init(&req->list); /* Remode from Active List */
312
313 switch (status) {
314
315 /* normal completion */
316 case 0:
317 if (req->actual > 0) {
318 list_add_tail(&req->list, &dev->rx_buffers);
319 DBG(dev, "G_Printer : rx length %d\n", req->actual);
320 } else {
321 list_add(&req->list, &dev->rx_reqs);
322 }
323 break;
324
325 /* software-driven interface shutdown */
326 case -ECONNRESET: /* unlink */
327 case -ESHUTDOWN: /* disconnect etc */
328 VDBG(dev, "rx shutdown, code %d\n", status);
329 list_add(&req->list, &dev->rx_reqs);
330 break;
331
332 /* for hardware automagic (such as pxa) */
333 case -ECONNABORTED: /* endpoint reset */
334 DBG(dev, "rx %s reset\n", ep->name);
335 list_add(&req->list, &dev->rx_reqs);
336 break;
337
338 /* data overrun */
339 case -EOVERFLOW:
340 /* FALLTHROUGH */
341
342 default:
343 DBG(dev, "rx status %d\n", status);
344 list_add(&req->list, &dev->rx_reqs);
345 break;
346 }
347
348 wake_up_interruptible(&dev->rx_wait);
349 spin_unlock_irqrestore(&dev->lock, flags);
350}
351
352static void tx_complete(struct usb_ep *ep, struct usb_request *req)
353{
354 struct printer_dev *dev = ep->driver_data;
355
356 switch (req->status) {
357 default:
358 VDBG(dev, "tx err %d\n", req->status);
359 /* FALLTHROUGH */
360 case -ECONNRESET: /* unlink */
361 case -ESHUTDOWN: /* disconnect etc */
362 break;
363 case 0:
364 break;
365 }
366
367 spin_lock(&dev->lock);
368 /* Take the request struct off the active list and put it on the
369 * free list.
370 */
371 list_del_init(&req->list);
372 list_add(&req->list, &dev->tx_reqs);
373 wake_up_interruptible(&dev->tx_wait);
374 if (likely(list_empty(&dev->tx_reqs_active)))
375 wake_up_interruptible(&dev->tx_flush_wait);
376
377 spin_unlock(&dev->lock);
378}
379
380/*-------------------------------------------------------------------------*/
381
382static int
383printer_open(struct inode *inode, struct file *fd)
384{
385 struct printer_dev *dev;
386 unsigned long flags;
387 int ret = -EBUSY;
388
389 mutex_lock(&printer_mutex);
390 dev = container_of(inode->i_cdev, struct printer_dev, printer_cdev);
391
392 spin_lock_irqsave(&dev->lock, flags);
393
394 if (!dev->printer_cdev_open) {
395 dev->printer_cdev_open = 1;
396 fd->private_data = dev;
397 ret = 0;
398 /* Change the printer status to show that it's on-line. */
399 dev->printer_status |= PRINTER_SELECTED;
400 }
401
402 spin_unlock_irqrestore(&dev->lock, flags);
403
404 DBG(dev, "printer_open returned %x\n", ret);
405 mutex_unlock(&printer_mutex);
406 return ret;
407}
408
409static int
410printer_close(struct inode *inode, struct file *fd)
411{
412 struct printer_dev *dev = fd->private_data;
413 unsigned long flags;
414
415 spin_lock_irqsave(&dev->lock, flags);
416 dev->printer_cdev_open = 0;
417 fd->private_data = NULL;
418 /* Change printer status to show that the printer is off-line. */
419 dev->printer_status &= ~PRINTER_SELECTED;
420 spin_unlock_irqrestore(&dev->lock, flags);
421
422 DBG(dev, "printer_close\n");
423
424 return 0;
425}
426
427/* This function must be called with interrupts turned off. */
428static void
429setup_rx_reqs(struct printer_dev *dev)
430{
431 struct usb_request *req;
432
433 while (likely(!list_empty(&dev->rx_reqs))) {
434 int error;
435
436 req = container_of(dev->rx_reqs.next,
437 struct usb_request, list);
438 list_del_init(&req->list);
439
440 /* The USB Host sends us whatever amount of data it wants to
441 * so we always set the length field to the full USB_BUFSIZE.
442 * If the amount of data is more than the read() caller asked
443 * for it will be stored in the request buffer until it is
444 * asked for by read().
445 */
446 req->length = USB_BUFSIZE;
447 req->complete = rx_complete;
448
449 error = usb_ep_queue(dev->out_ep, req, GFP_ATOMIC);
450 if (error) {
451 DBG(dev, "rx submit --> %d\n", error);
452 list_add(&req->list, &dev->rx_reqs);
453 break;
454 } else {
455 list_add(&req->list, &dev->rx_reqs_active);
456 }
457 }
458}
459
460static ssize_t
461printer_read(struct file *fd, char __user *buf, size_t len, loff_t *ptr)
462{
463 struct printer_dev *dev = fd->private_data;
464 unsigned long flags;
465 size_t size;
466 size_t bytes_copied;
467 struct usb_request *req;
468 /* This is a pointer to the current USB rx request. */
469 struct usb_request *current_rx_req;
470 /* This is the number of bytes in the current rx buffer. */
471 size_t current_rx_bytes;
472 /* This is a pointer to the current rx buffer. */
473 u8 *current_rx_buf;
474
475 if (len == 0)
476 return -EINVAL;
477
478 DBG(dev, "printer_read trying to read %d bytes\n", (int)len);
479
480 mutex_lock(&dev->lock_printer_io);
481 spin_lock_irqsave(&dev->lock, flags);
482
483 /* We will use this flag later to check if a printer reset happened
484 * after we turn interrupts back on.
485 */
486 dev->reset_printer = 0;
487
488 setup_rx_reqs(dev);
489
490 bytes_copied = 0;
491 current_rx_req = dev->current_rx_req;
492 current_rx_bytes = dev->current_rx_bytes;
493 current_rx_buf = dev->current_rx_buf;
494 dev->current_rx_req = NULL;
495 dev->current_rx_bytes = 0;
496 dev->current_rx_buf = NULL;
497
498 /* Check if there is any data in the read buffers. Please note that
499 * current_rx_bytes is the number of bytes in the current rx buffer.
500 * If it is zero then check if there are any other rx_buffers that
501 * are on the completed list. We are only out of data if all rx
502 * buffers are empty.
503 */
504 if ((current_rx_bytes == 0) &&
505 (likely(list_empty(&dev->rx_buffers)))) {
506 /* Turn interrupts back on before sleeping. */
507 spin_unlock_irqrestore(&dev->lock, flags);
508
509 /*
510 * If no data is available check if this is a NON-Blocking
511 * call or not.
512 */
513 if (fd->f_flags & (O_NONBLOCK|O_NDELAY)) {
514 mutex_unlock(&dev->lock_printer_io);
515 return -EAGAIN;
516 }
517
518 /* Sleep until data is available */
519 wait_event_interruptible(dev->rx_wait,
520 (likely(!list_empty(&dev->rx_buffers))));
521 spin_lock_irqsave(&dev->lock, flags);
522 }
523
524 /* We have data to return then copy it to the caller's buffer.*/
525 while ((current_rx_bytes || likely(!list_empty(&dev->rx_buffers)))
526 && len) {
527 if (current_rx_bytes == 0) {
528 req = container_of(dev->rx_buffers.next,
529 struct usb_request, list);
530 list_del_init(&req->list);
531
532 if (req->actual && req->buf) {
533 current_rx_req = req;
534 current_rx_bytes = req->actual;
535 current_rx_buf = req->buf;
536 } else {
537 list_add(&req->list, &dev->rx_reqs);
538 continue;
539 }
540 }
541
542 /* Don't leave irqs off while doing memory copies */
543 spin_unlock_irqrestore(&dev->lock, flags);
544
545 if (len > current_rx_bytes)
546 size = current_rx_bytes;
547 else
548 size = len;
549
550 size -= copy_to_user(buf, current_rx_buf, size);
551 bytes_copied += size;
552 len -= size;
553 buf += size;
554
555 spin_lock_irqsave(&dev->lock, flags);
556
557 /* We've disconnected or reset so return. */
558 if (dev->reset_printer) {
559 list_add(¤t_rx_req->list, &dev->rx_reqs);
560 spin_unlock_irqrestore(&dev->lock, flags);
561 mutex_unlock(&dev->lock_printer_io);
562 return -EAGAIN;
563 }
564
565 /* If we not returning all the data left in this RX request
566 * buffer then adjust the amount of data left in the buffer.
567 * Othewise if we are done with this RX request buffer then
568 * requeue it to get any incoming data from the USB host.
569 */
570 if (size < current_rx_bytes) {
571 current_rx_bytes -= size;
572 current_rx_buf += size;
573 } else {
574 list_add(¤t_rx_req->list, &dev->rx_reqs);
575 current_rx_bytes = 0;
576 current_rx_buf = NULL;
577 current_rx_req = NULL;
578 }
579 }
580
581 dev->current_rx_req = current_rx_req;
582 dev->current_rx_bytes = current_rx_bytes;
583 dev->current_rx_buf = current_rx_buf;
584
585 spin_unlock_irqrestore(&dev->lock, flags);
586 mutex_unlock(&dev->lock_printer_io);
587
588 DBG(dev, "printer_read returned %d bytes\n", (int)bytes_copied);
589
590 if (bytes_copied)
591 return bytes_copied;
592 else
593 return -EAGAIN;
594}
595
596static ssize_t
597printer_write(struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
598{
599 struct printer_dev *dev = fd->private_data;
600 unsigned long flags;
601 size_t size; /* Amount of data in a TX request. */
602 size_t bytes_copied = 0;
603 struct usb_request *req;
604
605 DBG(dev, "printer_write trying to send %d bytes\n", (int)len);
606
607 if (len == 0)
608 return -EINVAL;
609
610 mutex_lock(&dev->lock_printer_io);
611 spin_lock_irqsave(&dev->lock, flags);
612
613 /* Check if a printer reset happens while we have interrupts on */
614 dev->reset_printer = 0;
615
616 /* Check if there is any available write buffers */
617 if (likely(list_empty(&dev->tx_reqs))) {
618 /* Turn interrupts back on before sleeping. */
619 spin_unlock_irqrestore(&dev->lock, flags);
620
621 /*
622 * If write buffers are available check if this is
623 * a NON-Blocking call or not.
624 */
625 if (fd->f_flags & (O_NONBLOCK|O_NDELAY)) {
626 mutex_unlock(&dev->lock_printer_io);
627 return -EAGAIN;
628 }
629
630 /* Sleep until a write buffer is available */
631 wait_event_interruptible(dev->tx_wait,
632 (likely(!list_empty(&dev->tx_reqs))));
633 spin_lock_irqsave(&dev->lock, flags);
634 }
635
636 while (likely(!list_empty(&dev->tx_reqs)) && len) {
637
638 if (len > USB_BUFSIZE)
639 size = USB_BUFSIZE;
640 else
641 size = len;
642
643 req = container_of(dev->tx_reqs.next, struct usb_request,
644 list);
645 list_del_init(&req->list);
646
647 req->complete = tx_complete;
648 req->length = size;
649
650 /* Check if we need to send a zero length packet. */
651 if (len > size)
652 /* They will be more TX requests so no yet. */
653 req->zero = 0;
654 else
655 /* If the data amount is not a multple of the
656 * maxpacket size then send a zero length packet.
657 */
658 req->zero = ((len % dev->in_ep->maxpacket) == 0);
659
660 /* Don't leave irqs off while doing memory copies */
661 spin_unlock_irqrestore(&dev->lock, flags);
662
663 if (copy_from_user(req->buf, buf, size)) {
664 list_add(&req->list, &dev->tx_reqs);
665 mutex_unlock(&dev->lock_printer_io);
666 return bytes_copied;
667 }
668
669 bytes_copied += size;
670 len -= size;
671 buf += size;
672
673 spin_lock_irqsave(&dev->lock, flags);
674
675 /* We've disconnected or reset so free the req and buffer */
676 if (dev->reset_printer) {
677 list_add(&req->list, &dev->tx_reqs);
678 spin_unlock_irqrestore(&dev->lock, flags);
679 mutex_unlock(&dev->lock_printer_io);
680 return -EAGAIN;
681 }
682
683 if (usb_ep_queue(dev->in_ep, req, GFP_ATOMIC)) {
684 list_add(&req->list, &dev->tx_reqs);
685 spin_unlock_irqrestore(&dev->lock, flags);
686 mutex_unlock(&dev->lock_printer_io);
687 return -EAGAIN;
688 }
689
690 list_add(&req->list, &dev->tx_reqs_active);
691
692 }
693
694 spin_unlock_irqrestore(&dev->lock, flags);
695 mutex_unlock(&dev->lock_printer_io);
696
697 DBG(dev, "printer_write sent %d bytes\n", (int)bytes_copied);
698
699 if (bytes_copied) {
700 return bytes_copied;
701 } else {
702 return -EAGAIN;
703 }
704}
705
706static int
707printer_fsync(struct file *fd, loff_t start, loff_t end, int datasync)
708{
709 struct printer_dev *dev = fd->private_data;
710 struct inode *inode = fd->f_path.dentry->d_inode;
711 unsigned long flags;
712 int tx_list_empty;
713
714 mutex_lock(&inode->i_mutex);
715 spin_lock_irqsave(&dev->lock, flags);
716 tx_list_empty = (likely(list_empty(&dev->tx_reqs)));
717 spin_unlock_irqrestore(&dev->lock, flags);
718
719 if (!tx_list_empty) {
720 /* Sleep until all data has been sent */
721 wait_event_interruptible(dev->tx_flush_wait,
722 (likely(list_empty(&dev->tx_reqs_active))));
723 }
724 mutex_unlock(&inode->i_mutex);
725
726 return 0;
727}
728
729static unsigned int
730printer_poll(struct file *fd, poll_table *wait)
731{
732 struct printer_dev *dev = fd->private_data;
733 unsigned long flags;
734 int status = 0;
735
736 mutex_lock(&dev->lock_printer_io);
737 spin_lock_irqsave(&dev->lock, flags);
738 setup_rx_reqs(dev);
739 spin_unlock_irqrestore(&dev->lock, flags);
740 mutex_unlock(&dev->lock_printer_io);
741
742 poll_wait(fd, &dev->rx_wait, wait);
743 poll_wait(fd, &dev->tx_wait, wait);
744
745 spin_lock_irqsave(&dev->lock, flags);
746 if (likely(!list_empty(&dev->tx_reqs)))
747 status |= POLLOUT | POLLWRNORM;
748
749 if (likely(dev->current_rx_bytes) ||
750 likely(!list_empty(&dev->rx_buffers)))
751 status |= POLLIN | POLLRDNORM;
752
753 spin_unlock_irqrestore(&dev->lock, flags);
754
755 return status;
756}
757
758static long
759printer_ioctl(struct file *fd, unsigned int code, unsigned long arg)
760{
761 struct printer_dev *dev = fd->private_data;
762 unsigned long flags;
763 int status = 0;
764
765 DBG(dev, "printer_ioctl: cmd=0x%4.4x, arg=%lu\n", code, arg);
766
767 /* handle ioctls */
768
769 spin_lock_irqsave(&dev->lock, flags);
770
771 switch (code) {
772 case GADGET_GET_PRINTER_STATUS:
773 status = (int)dev->printer_status;
774 break;
775 case GADGET_SET_PRINTER_STATUS:
776 dev->printer_status = (u8)arg;
777 break;
778 default:
779 /* could not handle ioctl */
780 DBG(dev, "printer_ioctl: ERROR cmd=0x%4.4xis not supported\n",
781 code);
782 status = -ENOTTY;
783 }
784
785 spin_unlock_irqrestore(&dev->lock, flags);
786
787 return status;
788}
789
790/* used after endpoint configuration */
791static const struct file_operations printer_io_operations = {
792 .owner = THIS_MODULE,
793 .open = printer_open,
794 .read = printer_read,
795 .write = printer_write,
796 .fsync = printer_fsync,
797 .poll = printer_poll,
798 .unlocked_ioctl = printer_ioctl,
799 .release = printer_close,
800 .llseek = noop_llseek,
801};
802
803/*-------------------------------------------------------------------------*/
804
805static int
806set_printer_interface(struct printer_dev *dev)
807{
808 int result = 0;
809
810 dev->in_ep->desc = ep_desc(dev->gadget, &hs_ep_in_desc, &fs_ep_in_desc);
811 dev->in_ep->driver_data = dev;
812
813 dev->out_ep->desc = ep_desc(dev->gadget, &hs_ep_out_desc,
814 &fs_ep_out_desc);
815 dev->out_ep->driver_data = dev;
816
817 result = usb_ep_enable(dev->in_ep);
818 if (result != 0) {
819 DBG(dev, "enable %s --> %d\n", dev->in_ep->name, result);
820 goto done;
821 }
822
823 result = usb_ep_enable(dev->out_ep);
824 if (result != 0) {
825 DBG(dev, "enable %s --> %d\n", dev->in_ep->name, result);
826 goto done;
827 }
828
829done:
830 /* on error, disable any endpoints */
831 if (result != 0) {
832 (void) usb_ep_disable(dev->in_ep);
833 (void) usb_ep_disable(dev->out_ep);
834 dev->in_ep->desc = NULL;
835 dev->out_ep->desc = NULL;
836 }
837
838 /* caller is responsible for cleanup on error */
839 return result;
840}
841
842static void printer_reset_interface(struct printer_dev *dev)
843{
844 if (dev->interface < 0)
845 return;
846
847 DBG(dev, "%s\n", __func__);
848
849 if (dev->in_ep->desc)
850 usb_ep_disable(dev->in_ep);
851
852 if (dev->out_ep->desc)
853 usb_ep_disable(dev->out_ep);
854
855 dev->in_ep->desc = NULL;
856 dev->out_ep->desc = NULL;
857 dev->interface = -1;
858}
859
860/* Change our operational Interface. */
861static int set_interface(struct printer_dev *dev, unsigned number)
862{
863 int result = 0;
864
865 /* Free the current interface */
866 printer_reset_interface(dev);
867
868 result = set_printer_interface(dev);
869 if (result)
870 printer_reset_interface(dev);
871 else
872 dev->interface = number;
873
874 if (!result)
875 INFO(dev, "Using interface %x\n", number);
876
877 return result;
878}
879
880static void printer_soft_reset(struct printer_dev *dev)
881{
882 struct usb_request *req;
883
884 INFO(dev, "Received Printer Reset Request\n");
885
886 if (usb_ep_disable(dev->in_ep))
887 DBG(dev, "Failed to disable USB in_ep\n");
888 if (usb_ep_disable(dev->out_ep))
889 DBG(dev, "Failed to disable USB out_ep\n");
890
891 if (dev->current_rx_req != NULL) {
892 list_add(&dev->current_rx_req->list, &dev->rx_reqs);
893 dev->current_rx_req = NULL;
894 }
895 dev->current_rx_bytes = 0;
896 dev->current_rx_buf = NULL;
897 dev->reset_printer = 1;
898
899 while (likely(!(list_empty(&dev->rx_buffers)))) {
900 req = container_of(dev->rx_buffers.next, struct usb_request,
901 list);
902 list_del_init(&req->list);
903 list_add(&req->list, &dev->rx_reqs);
904 }
905
906 while (likely(!(list_empty(&dev->rx_reqs_active)))) {
907 req = container_of(dev->rx_buffers.next, struct usb_request,
908 list);
909 list_del_init(&req->list);
910 list_add(&req->list, &dev->rx_reqs);
911 }
912
913 while (likely(!(list_empty(&dev->tx_reqs_active)))) {
914 req = container_of(dev->tx_reqs_active.next,
915 struct usb_request, list);
916 list_del_init(&req->list);
917 list_add(&req->list, &dev->tx_reqs);
918 }
919
920 if (usb_ep_enable(dev->in_ep))
921 DBG(dev, "Failed to enable USB in_ep\n");
922 if (usb_ep_enable(dev->out_ep))
923 DBG(dev, "Failed to enable USB out_ep\n");
924
925 wake_up_interruptible(&dev->rx_wait);
926 wake_up_interruptible(&dev->tx_wait);
927 wake_up_interruptible(&dev->tx_flush_wait);
928}
929
930/*-------------------------------------------------------------------------*/
931
932/*
933 * The setup() callback implements all the ep0 functionality that's not
934 * handled lower down.
935 */
936static int printer_func_setup(struct usb_function *f,
937 const struct usb_ctrlrequest *ctrl)
938{
939 struct printer_dev *dev = container_of(f, struct printer_dev, function);
940 struct usb_composite_dev *cdev = f->config->cdev;
941 struct usb_request *req = cdev->req;
942 int value = -EOPNOTSUPP;
943 u16 wIndex = le16_to_cpu(ctrl->wIndex);
944 u16 wValue = le16_to_cpu(ctrl->wValue);
945 u16 wLength = le16_to_cpu(ctrl->wLength);
946
947 DBG(dev, "ctrl req%02x.%02x v%04x i%04x l%d\n",
948 ctrl->bRequestType, ctrl->bRequest, wValue, wIndex, wLength);
949
950 switch (ctrl->bRequestType&USB_TYPE_MASK) {
951 case USB_TYPE_CLASS:
952 switch (ctrl->bRequest) {
953 case 0: /* Get the IEEE-1284 PNP String */
954 /* Only one printer interface is supported. */
955 if ((wIndex>>8) != dev->interface)
956 break;
957
958 value = (pnp_string[0]<<8)|pnp_string[1];
959 memcpy(req->buf, pnp_string, value);
960 DBG(dev, "1284 PNP String: %x %s\n", value,
961 &pnp_string[2]);
962 break;
963
964 case 1: /* Get Port Status */
965 /* Only one printer interface is supported. */
966 if (wIndex != dev->interface)
967 break;
968
969 *(u8 *)req->buf = dev->printer_status;
970 value = min(wLength, (u16) 1);
971 break;
972
973 case 2: /* Soft Reset */
974 /* Only one printer interface is supported. */
975 if (wIndex != dev->interface)
976 break;
977
978 printer_soft_reset(dev);
979
980 value = 0;
981 break;
982
983 default:
984 goto unknown;
985 }
986 break;
987
988 default:
989unknown:
990 VDBG(dev,
991 "unknown ctrl req%02x.%02x v%04x i%04x l%d\n",
992 ctrl->bRequestType, ctrl->bRequest,
993 wValue, wIndex, wLength);
994 break;
995 }
996 /* host either stalls (value < 0) or reports success */
997 return value;
998}
999
1000static int __init printer_func_bind(struct usb_configuration *c,
1001 struct usb_function *f)
1002{
1003 struct printer_dev *dev = container_of(f, struct printer_dev, function);
1004 struct usb_composite_dev *cdev = c->cdev;
1005 struct usb_ep *in_ep, *out_ep;
1006 int id;
1007
1008 id = usb_interface_id(c, f);
1009 if (id < 0)
1010 return id;
1011 intf_desc.bInterfaceNumber = id;
1012
1013 /* all we really need is bulk IN/OUT */
1014 in_ep = usb_ep_autoconfig(cdev->gadget, &fs_ep_in_desc);
1015 if (!in_ep) {
1016autoconf_fail:
1017 dev_err(&cdev->gadget->dev, "can't autoconfigure on %s\n",
1018 cdev->gadget->name);
1019 return -ENODEV;
1020 }
1021 in_ep->driver_data = in_ep; /* claim */
1022
1023 out_ep = usb_ep_autoconfig(cdev->gadget, &fs_ep_out_desc);
1024 if (!out_ep)
1025 goto autoconf_fail;
1026 out_ep->driver_data = out_ep; /* claim */
1027
1028 /* assumes that all endpoints are dual-speed */
1029 hs_ep_in_desc.bEndpointAddress = fs_ep_in_desc.bEndpointAddress;
1030 hs_ep_out_desc.bEndpointAddress = fs_ep_out_desc.bEndpointAddress;
1031
1032 dev->in_ep = in_ep;
1033 dev->out_ep = out_ep;
1034 return 0;
1035}
1036
1037static void printer_func_unbind(struct usb_configuration *c,
1038 struct usb_function *f)
1039{
1040}
1041
1042static int printer_func_set_alt(struct usb_function *f,
1043 unsigned intf, unsigned alt)
1044{
1045 struct printer_dev *dev = container_of(f, struct printer_dev, function);
1046 int ret = -ENOTSUPP;
1047
1048 if (!alt)
1049 ret = set_interface(dev, intf);
1050
1051 return ret;
1052}
1053
1054static void printer_func_disable(struct usb_function *f)
1055{
1056 struct printer_dev *dev = container_of(f, struct printer_dev, function);
1057 unsigned long flags;
1058
1059 DBG(dev, "%s\n", __func__);
1060
1061 spin_lock_irqsave(&dev->lock, flags);
1062 printer_reset_interface(dev);
1063 spin_unlock_irqrestore(&dev->lock, flags);
1064}
1065
1066static void printer_cfg_unbind(struct usb_configuration *c)
1067{
1068 struct printer_dev *dev;
1069 struct usb_request *req;
1070
1071 dev = &usb_printer_gadget;
1072
1073 DBG(dev, "%s\n", __func__);
1074
1075 /* Remove sysfs files */
1076 device_destroy(usb_gadget_class, g_printer_devno);
1077
1078 /* Remove Character Device */
1079 cdev_del(&dev->printer_cdev);
1080
1081 /* we must already have been disconnected ... no i/o may be active */
1082 WARN_ON(!list_empty(&dev->tx_reqs_active));
1083 WARN_ON(!list_empty(&dev->rx_reqs_active));
1084
1085 /* Free all memory for this driver. */
1086 while (!list_empty(&dev->tx_reqs)) {
1087 req = container_of(dev->tx_reqs.next, struct usb_request,
1088 list);
1089 list_del(&req->list);
1090 printer_req_free(dev->in_ep, req);
1091 }
1092
1093 if (dev->current_rx_req != NULL)
1094 printer_req_free(dev->out_ep, dev->current_rx_req);
1095
1096 while (!list_empty(&dev->rx_reqs)) {
1097 req = container_of(dev->rx_reqs.next,
1098 struct usb_request, list);
1099 list_del(&req->list);
1100 printer_req_free(dev->out_ep, req);
1101 }
1102
1103 while (!list_empty(&dev->rx_buffers)) {
1104 req = container_of(dev->rx_buffers.next,
1105 struct usb_request, list);
1106 list_del(&req->list);
1107 printer_req_free(dev->out_ep, req);
1108 }
1109}
1110
1111static struct usb_configuration printer_cfg_driver = {
1112 .label = "printer",
1113 .unbind = printer_cfg_unbind,
1114 .bConfigurationValue = 1,
1115 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
1116};
1117
1118static int __init printer_bind_config(struct usb_configuration *c)
1119{
1120 struct usb_gadget *gadget = c->cdev->gadget;
1121 struct printer_dev *dev;
1122 int status = -ENOMEM;
1123 int gcnum;
1124 size_t len;
1125 u32 i;
1126 struct usb_request *req;
1127
1128 usb_ep_autoconfig_reset(gadget);
1129
1130 dev = &usb_printer_gadget;
1131
1132 dev->function.name = shortname;
1133 dev->function.descriptors = fs_printer_function;
1134 dev->function.hs_descriptors = hs_printer_function;
1135 dev->function.bind = printer_func_bind;
1136 dev->function.setup = printer_func_setup;
1137 dev->function.unbind = printer_func_unbind;
1138 dev->function.set_alt = printer_func_set_alt;
1139 dev->function.disable = printer_func_disable;
1140
1141 status = usb_add_function(c, &dev->function);
1142 if (status)
1143 return status;
1144
1145 /* Setup the sysfs files for the printer gadget. */
1146 dev->pdev = device_create(usb_gadget_class, NULL, g_printer_devno,
1147 NULL, "g_printer");
1148 if (IS_ERR(dev->pdev)) {
1149 ERROR(dev, "Failed to create device: g_printer\n");
1150 goto fail;
1151 }
1152
1153 /*
1154 * Register a character device as an interface to a user mode
1155 * program that handles the printer specific functionality.
1156 */
1157 cdev_init(&dev->printer_cdev, &printer_io_operations);
1158 dev->printer_cdev.owner = THIS_MODULE;
1159 status = cdev_add(&dev->printer_cdev, g_printer_devno, 1);
1160 if (status) {
1161 ERROR(dev, "Failed to open char device\n");
1162 goto fail;
1163 }
1164
1165 gcnum = usb_gadget_controller_number(gadget);
1166 if (gcnum >= 0) {
1167 device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
1168 } else {
1169 dev_warn(&gadget->dev, "controller '%s' not recognized\n",
1170 gadget->name);
1171 /* unrecognized, but safe unless bulk is REALLY quirky */
1172 device_desc.bcdDevice =
1173 cpu_to_le16(0xFFFF);
1174 }
1175 snprintf(manufacturer, sizeof(manufacturer), "%s %s with %s",
1176 init_utsname()->sysname, init_utsname()->release,
1177 gadget->name);
1178
1179 if (iSerialNum)
1180 strlcpy(serial_num, iSerialNum, sizeof serial_num);
1181
1182 if (iPNPstring)
1183 strlcpy(&pnp_string[2], iPNPstring, (sizeof pnp_string)-2);
1184
1185 len = strlen(pnp_string);
1186 pnp_string[0] = (len >> 8) & 0xFF;
1187 pnp_string[1] = len & 0xFF;
1188
1189 usb_gadget_set_selfpowered(gadget);
1190
1191 if (gadget->is_otg) {
1192 otg_descriptor.bmAttributes |= USB_OTG_HNP;
1193 printer_cfg_driver.descriptors = otg_desc;
1194 printer_cfg_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1195 }
1196
1197 spin_lock_init(&dev->lock);
1198 mutex_init(&dev->lock_printer_io);
1199 INIT_LIST_HEAD(&dev->tx_reqs);
1200 INIT_LIST_HEAD(&dev->tx_reqs_active);
1201 INIT_LIST_HEAD(&dev->rx_reqs);
1202 INIT_LIST_HEAD(&dev->rx_reqs_active);
1203 INIT_LIST_HEAD(&dev->rx_buffers);
1204 init_waitqueue_head(&dev->rx_wait);
1205 init_waitqueue_head(&dev->tx_wait);
1206 init_waitqueue_head(&dev->tx_flush_wait);
1207
1208 dev->interface = -1;
1209 dev->printer_cdev_open = 0;
1210 dev->printer_status = PRINTER_NOT_ERROR;
1211 dev->current_rx_req = NULL;
1212 dev->current_rx_bytes = 0;
1213 dev->current_rx_buf = NULL;
1214
1215 for (i = 0; i < QLEN; i++) {
1216 req = printer_req_alloc(dev->in_ep, USB_BUFSIZE, GFP_KERNEL);
1217 if (!req) {
1218 while (!list_empty(&dev->tx_reqs)) {
1219 req = container_of(dev->tx_reqs.next,
1220 struct usb_request, list);
1221 list_del(&req->list);
1222 printer_req_free(dev->in_ep, req);
1223 }
1224 return -ENOMEM;
1225 }
1226 list_add(&req->list, &dev->tx_reqs);
1227 }
1228
1229 for (i = 0; i < QLEN; i++) {
1230 req = printer_req_alloc(dev->out_ep, USB_BUFSIZE, GFP_KERNEL);
1231 if (!req) {
1232 while (!list_empty(&dev->rx_reqs)) {
1233 req = container_of(dev->rx_reqs.next,
1234 struct usb_request, list);
1235 list_del(&req->list);
1236 printer_req_free(dev->out_ep, req);
1237 }
1238 return -ENOMEM;
1239 }
1240 list_add(&req->list, &dev->rx_reqs);
1241 }
1242
1243 /* finish hookup to lower layer ... */
1244 dev->gadget = gadget;
1245
1246 INFO(dev, "%s, version: " DRIVER_VERSION "\n", driver_desc);
1247 return 0;
1248
1249fail:
1250 printer_cfg_unbind(c);
1251 return status;
1252}
1253
1254static int printer_unbind(struct usb_composite_dev *cdev)
1255{
1256 return 0;
1257}
1258
1259static int __init printer_bind(struct usb_composite_dev *cdev)
1260{
1261 int ret;
1262
1263 ret = usb_string_ids_tab(cdev, strings);
1264 if (ret < 0)
1265 return ret;
1266 device_desc.iManufacturer = strings[STRING_MANUFACTURER].id;
1267 device_desc.iProduct = strings[STRING_PRODUCT].id;
1268 device_desc.iSerialNumber = strings[STRING_SERIALNUM].id;
1269
1270 ret = usb_add_config(cdev, &printer_cfg_driver, printer_bind_config);
1271 return ret;
1272}
1273
1274static struct usb_composite_driver printer_driver = {
1275 .name = shortname,
1276 .dev = &device_desc,
1277 .strings = dev_strings,
1278 .max_speed = USB_SPEED_HIGH,
1279 .unbind = printer_unbind,
1280};
1281
1282static int __init
1283init(void)
1284{
1285 int status;
1286
1287 usb_gadget_class = class_create(THIS_MODULE, "usb_printer_gadget");
1288 if (IS_ERR(usb_gadget_class)) {
1289 status = PTR_ERR(usb_gadget_class);
1290 pr_err("unable to create usb_gadget class %d\n", status);
1291 return status;
1292 }
1293
1294 status = alloc_chrdev_region(&g_printer_devno, 0, 1,
1295 "USB printer gadget");
1296 if (status) {
1297 pr_err("alloc_chrdev_region %d\n", status);
1298 class_destroy(usb_gadget_class);
1299 return status;
1300 }
1301
1302 status = usb_composite_probe(&printer_driver, printer_bind);
1303 if (status) {
1304 class_destroy(usb_gadget_class);
1305 unregister_chrdev_region(g_printer_devno, 1);
1306 pr_err("usb_gadget_probe_driver %x\n", status);
1307 }
1308
1309 return status;
1310}
1311module_init(init);
1312
1313static void __exit
1314cleanup(void)
1315{
1316 mutex_lock(&usb_printer_gadget.lock_printer_io);
1317 usb_composite_unregister(&printer_driver);
1318 unregister_chrdev_region(g_printer_devno, 1);
1319 class_destroy(usb_gadget_class);
1320 mutex_unlock(&usb_printer_gadget.lock_printer_io);
1321}
1322module_exit(cleanup);
1323
1324MODULE_DESCRIPTION(DRIVER_DESC);
1325MODULE_AUTHOR("Craig Nadler");
1326MODULE_LICENSE("GPL");