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