blob: ecc1410073f42aef504f9cfdc168235685696dd4 [file] [log] [blame]
Craig W. Nadler25a010c2007-11-11 15:00:15 -08001/*
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 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22#include <linux/module.h>
23#include <linux/kernel.h>
24#include <linux/delay.h>
25#include <linux/ioport.h>
26#include <linux/sched.h>
27#include <linux/slab.h>
28#include <linux/smp_lock.h>
29#include <linux/errno.h>
30#include <linux/init.h>
31#include <linux/timer.h>
32#include <linux/list.h>
33#include <linux/interrupt.h>
34#include <linux/utsname.h>
35#include <linux/device.h>
36#include <linux/moduleparam.h>
37#include <linux/fs.h>
38#include <linux/poll.h>
39#include <linux/types.h>
40#include <linux/ctype.h>
41#include <linux/cdev.h>
42
43#include <asm/byteorder.h>
44#include <linux/io.h>
45#include <linux/irq.h>
46#include <asm/system.h>
47#include <linux/uaccess.h>
48#include <asm/unaligned.h>
49
50#include <linux/usb/ch9.h>
51#include <linux/usb/gadget.h>
52#include <linux/usb/g_printer.h>
53
54#include "gadget_chips.h"
55
56#define DRIVER_DESC "Printer Gadget"
57#define DRIVER_VERSION "2007 OCT 06"
58
59static const char shortname [] = "printer";
60static const char driver_desc [] = DRIVER_DESC;
61
62static dev_t g_printer_devno;
63
64static struct class *usb_gadget_class;
65
66/*-------------------------------------------------------------------------*/
67
68struct printer_dev {
69 spinlock_t lock; /* lock this structure */
70 /* lock buffer lists during read/write calls */
71 spinlock_t lock_printer_io;
72 struct usb_gadget *gadget;
73 struct usb_request *req; /* for control responses */
74 u8 config;
75 s8 interface;
76 struct usb_ep *in_ep, *out_ep;
77 const struct usb_endpoint_descriptor
78 *in, *out;
79 struct list_head rx_reqs; /* List of free RX structs */
80 struct list_head rx_reqs_active; /* List of Active RX xfers */
81 struct list_head rx_buffers; /* List of completed xfers */
82 /* wait until there is data to be read. */
83 wait_queue_head_t rx_wait;
84 struct list_head tx_reqs; /* List of free TX structs */
85 struct list_head tx_reqs_active; /* List of Active TX xfers */
86 /* Wait until there are write buffers available to use. */
87 wait_queue_head_t tx_wait;
88 /* Wait until all write buffers have been sent. */
89 wait_queue_head_t tx_flush_wait;
90 struct usb_request *current_rx_req;
91 size_t current_rx_bytes;
92 u8 *current_rx_buf;
93 u8 printer_status;
94 u8 reset_printer;
Craig W. Nadler25a010c2007-11-11 15:00:15 -080095 struct cdev printer_cdev;
96 struct device *pdev;
97 u8 printer_cdev_open;
98 wait_queue_head_t wait;
99};
100
101static struct printer_dev usb_printer_gadget;
102
103/*-------------------------------------------------------------------------*/
104
105/* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
106 * Instead: allocate your own, using normal USB-IF procedures.
107 */
108
109/* Thanks to NetChip Technologies for donating this product ID.
110 */
111#define PRINTER_VENDOR_NUM 0x0525 /* NetChip */
112#define PRINTER_PRODUCT_NUM 0xa4a8 /* Linux-USB Printer Gadget */
113
114/* Some systems will want different product identifers published in the
115 * device descriptor, either numbers or strings or both. These string
116 * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
117 */
118
119static ushort __initdata idVendor;
120module_param(idVendor, ushort, S_IRUGO);
121MODULE_PARM_DESC(idVendor, "USB Vendor ID");
122
123static ushort __initdata idProduct;
124module_param(idProduct, ushort, S_IRUGO);
125MODULE_PARM_DESC(idProduct, "USB Product ID");
126
127static ushort __initdata bcdDevice;
128module_param(bcdDevice, ushort, S_IRUGO);
129MODULE_PARM_DESC(bcdDevice, "USB Device version (BCD)");
130
131static char *__initdata iManufacturer;
132module_param(iManufacturer, charp, S_IRUGO);
133MODULE_PARM_DESC(iManufacturer, "USB Manufacturer string");
134
135static char *__initdata iProduct;
136module_param(iProduct, charp, S_IRUGO);
137MODULE_PARM_DESC(iProduct, "USB Product string");
138
139static char *__initdata iSerialNum;
140module_param(iSerialNum, charp, S_IRUGO);
141MODULE_PARM_DESC(iSerialNum, "1");
142
143static char *__initdata iPNPstring;
144module_param(iPNPstring, charp, S_IRUGO);
145MODULE_PARM_DESC(iPNPstring, "MFG:linux;MDL:g_printer;CLS:PRINTER;SN:1;");
146
147/* Number of requests to allocate per endpoint, not used for ep0. */
148static unsigned qlen = 10;
149module_param(qlen, uint, S_IRUGO|S_IWUSR);
150
151#define QLEN qlen
152
153#ifdef CONFIG_USB_GADGET_DUALSPEED
154#define DEVSPEED USB_SPEED_HIGH
155#else /* full speed (low speed doesn't do bulk) */
156#define DEVSPEED USB_SPEED_FULL
157#endif
158
159/*-------------------------------------------------------------------------*/
160
161#define xprintk(d, level, fmt, args...) \
162 printk(level "%s: " fmt, DRIVER_DESC, ## args)
163
164#ifdef DEBUG
165#define DBG(dev, fmt, args...) \
166 xprintk(dev, KERN_DEBUG, fmt, ## args)
167#else
168#define DBG(dev, fmt, args...) \
169 do { } while (0)
170#endif /* DEBUG */
171
172#ifdef VERBOSE
173#define VDBG(dev, fmt, args...) \
174 xprintk(dev, KERN_DEBUG, fmt, ## args)
175#else
176#define VDBG(dev, fmt, args...) \
177 do { } while (0)
178#endif /* VERBOSE */
179
180#define ERROR(dev, fmt, args...) \
181 xprintk(dev, KERN_ERR, fmt, ## args)
182#define WARN(dev, fmt, args...) \
183 xprintk(dev, KERN_WARNING, fmt, ## args)
184#define INFO(dev, fmt, args...) \
185 xprintk(dev, KERN_INFO, fmt, ## args)
186
187/*-------------------------------------------------------------------------*/
188
189/* USB DRIVER HOOKUP (to the hardware driver, below us), mostly
190 * ep0 implementation: descriptors, config management, setup().
191 * also optional class-specific notification interrupt transfer.
192 */
193
194/*
195 * DESCRIPTORS ... most are static, but strings and (full) configuration
196 * descriptors are built on demand.
197 */
198
199#define STRING_MANUFACTURER 1
200#define STRING_PRODUCT 2
201#define STRING_SERIALNUM 3
202
203/* holds our biggest descriptor */
204#define USB_DESC_BUFSIZE 256
205#define USB_BUFSIZE 8192
206
207/* This device advertises one configuration. */
208#define DEV_CONFIG_VALUE 1
209#define PRINTER_INTERFACE 0
210
211static struct usb_device_descriptor device_desc = {
212 .bLength = sizeof device_desc,
213 .bDescriptorType = USB_DT_DEVICE,
214 .bcdUSB = __constant_cpu_to_le16(0x0200),
215 .bDeviceClass = USB_CLASS_PER_INTERFACE,
216 .bDeviceSubClass = 0,
217 .bDeviceProtocol = 0,
218 .idVendor = __constant_cpu_to_le16(PRINTER_VENDOR_NUM),
219 .idProduct = __constant_cpu_to_le16(PRINTER_PRODUCT_NUM),
220 .iManufacturer = STRING_MANUFACTURER,
221 .iProduct = STRING_PRODUCT,
222 .iSerialNumber = STRING_SERIALNUM,
223 .bNumConfigurations = 1
224};
225
226static struct usb_otg_descriptor otg_desc = {
227 .bLength = sizeof otg_desc,
228 .bDescriptorType = USB_DT_OTG,
229 .bmAttributes = USB_OTG_SRP
230};
231
232static struct usb_config_descriptor config_desc = {
233 .bLength = sizeof config_desc,
234 .bDescriptorType = USB_DT_CONFIG,
235
236 /* compute wTotalLength on the fly */
237 .bNumInterfaces = 1,
238 .bConfigurationValue = DEV_CONFIG_VALUE,
239 .iConfiguration = 0,
240 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
241 .bMaxPower = 1 /* Self-Powered */
242};
243
244static struct usb_interface_descriptor intf_desc = {
245 .bLength = sizeof intf_desc,
246 .bDescriptorType = USB_DT_INTERFACE,
247 .bInterfaceNumber = PRINTER_INTERFACE,
248 .bNumEndpoints = 2,
249 .bInterfaceClass = USB_CLASS_PRINTER,
250 .bInterfaceSubClass = 1, /* Printer Sub-Class */
251 .bInterfaceProtocol = 2, /* Bi-Directional */
252 .iInterface = 0
253};
254
255static struct usb_endpoint_descriptor fs_ep_in_desc = {
256 .bLength = USB_DT_ENDPOINT_SIZE,
257 .bDescriptorType = USB_DT_ENDPOINT,
258 .bEndpointAddress = USB_DIR_IN,
259 .bmAttributes = USB_ENDPOINT_XFER_BULK
260};
261
262static struct usb_endpoint_descriptor fs_ep_out_desc = {
263 .bLength = USB_DT_ENDPOINT_SIZE,
264 .bDescriptorType = USB_DT_ENDPOINT,
265 .bEndpointAddress = USB_DIR_OUT,
266 .bmAttributes = USB_ENDPOINT_XFER_BULK
267};
268
269static const struct usb_descriptor_header *fs_printer_function [11] = {
270 (struct usb_descriptor_header *) &otg_desc,
271 (struct usb_descriptor_header *) &intf_desc,
272 (struct usb_descriptor_header *) &fs_ep_in_desc,
273 (struct usb_descriptor_header *) &fs_ep_out_desc,
274 NULL
275};
276
277#ifdef CONFIG_USB_GADGET_DUALSPEED
278
279/*
280 * usb 2.0 devices need to expose both high speed and full speed
281 * descriptors, unless they only run at full speed.
282 */
283
284static struct usb_endpoint_descriptor hs_ep_in_desc = {
285 .bLength = USB_DT_ENDPOINT_SIZE,
286 .bDescriptorType = USB_DT_ENDPOINT,
287 .bmAttributes = USB_ENDPOINT_XFER_BULK,
288 .wMaxPacketSize = __constant_cpu_to_le16(512)
289};
290
291static struct usb_endpoint_descriptor hs_ep_out_desc = {
292 .bLength = USB_DT_ENDPOINT_SIZE,
293 .bDescriptorType = USB_DT_ENDPOINT,
294 .bmAttributes = USB_ENDPOINT_XFER_BULK,
295 .wMaxPacketSize = __constant_cpu_to_le16(512)
296};
297
298static struct usb_qualifier_descriptor dev_qualifier = {
299 .bLength = sizeof dev_qualifier,
300 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
301 .bcdUSB = __constant_cpu_to_le16(0x0200),
302 .bDeviceClass = USB_CLASS_PRINTER,
303 .bNumConfigurations = 1
304};
305
306static const struct usb_descriptor_header *hs_printer_function [11] = {
307 (struct usb_descriptor_header *) &otg_desc,
308 (struct usb_descriptor_header *) &intf_desc,
309 (struct usb_descriptor_header *) &hs_ep_in_desc,
310 (struct usb_descriptor_header *) &hs_ep_out_desc,
311 NULL
312};
313
314/* maxpacket and other transfer characteristics vary by speed. */
315#define ep_desc(g, hs, fs) (((g)->speed == USB_SPEED_HIGH)?(hs):(fs))
316
317#else
318
319/* if there's no high speed support, maxpacket doesn't change. */
320#define ep_desc(g, hs, fs) (((void)(g)), (fs))
321
322#endif /* !CONFIG_USB_GADGET_DUALSPEED */
323
324/*-------------------------------------------------------------------------*/
325
326/* descriptors that are built on-demand */
327
328static char manufacturer [50];
329static char product_desc [40] = DRIVER_DESC;
330static char serial_num [40] = "1";
331static char pnp_string [1024] =
332 "XXMFG:linux;MDL:g_printer;CLS:PRINTER;SN:1;";
333
334/* static strings, in UTF-8 */
335static struct usb_string strings [] = {
336 { STRING_MANUFACTURER, manufacturer, },
337 { STRING_PRODUCT, product_desc, },
338 { STRING_SERIALNUM, serial_num, },
339 { } /* end of list */
340};
341
342static struct usb_gadget_strings stringtab = {
343 .language = 0x0409, /* en-us */
344 .strings = strings,
345};
346
347/*-------------------------------------------------------------------------*/
348
349static struct usb_request *
350printer_req_alloc(struct usb_ep *ep, unsigned len, gfp_t gfp_flags)
351{
352 struct usb_request *req;
353
354 req = usb_ep_alloc_request(ep, gfp_flags);
355
356 if (req != NULL) {
357 req->length = len;
358 req->buf = kmalloc(len, gfp_flags);
359 if (req->buf == NULL) {
360 usb_ep_free_request(ep, req);
361 return NULL;
362 }
363 }
364
365 return req;
366}
367
368static void
369printer_req_free(struct usb_ep *ep, struct usb_request *req)
370{
371 if (ep != NULL && req != NULL) {
372 kfree(req->buf);
373 usb_ep_free_request(ep, req);
374 }
375}
376
377/*-------------------------------------------------------------------------*/
378
379static void rx_complete(struct usb_ep *ep, struct usb_request *req)
380{
381 struct printer_dev *dev = ep->driver_data;
382 int status = req->status;
383 unsigned long flags;
384
385 spin_lock_irqsave(&dev->lock, flags);
386
387 list_del_init(&req->list); /* Remode from Active List */
388
389 switch (status) {
390
391 /* normal completion */
392 case 0:
Craig W. Nadlercc901bb2008-03-20 14:46:26 -0700393 if (req->actual > 0) {
394 list_add_tail(&req->list, &dev->rx_buffers);
395 DBG(dev, "G_Printer : rx length %d\n", req->actual);
396 } else {
397 list_add(&req->list, &dev->rx_reqs);
398 }
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800399 break;
400
401 /* software-driven interface shutdown */
402 case -ECONNRESET: /* unlink */
403 case -ESHUTDOWN: /* disconnect etc */
404 VDBG(dev, "rx shutdown, code %d\n", status);
405 list_add(&req->list, &dev->rx_reqs);
406 break;
407
408 /* for hardware automagic (such as pxa) */
409 case -ECONNABORTED: /* endpoint reset */
410 DBG(dev, "rx %s reset\n", ep->name);
411 list_add(&req->list, &dev->rx_reqs);
412 break;
413
414 /* data overrun */
415 case -EOVERFLOW:
416 /* FALLTHROUGH */
417
418 default:
419 DBG(dev, "rx status %d\n", status);
420 list_add(&req->list, &dev->rx_reqs);
421 break;
422 }
Craig W. Nadlercc901bb2008-03-20 14:46:26 -0700423
424 wake_up_interruptible(&dev->rx_wait);
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800425 spin_unlock_irqrestore(&dev->lock, flags);
426}
427
428static void tx_complete(struct usb_ep *ep, struct usb_request *req)
429{
430 struct printer_dev *dev = ep->driver_data;
431
432 switch (req->status) {
433 default:
434 VDBG(dev, "tx err %d\n", req->status);
435 /* FALLTHROUGH */
436 case -ECONNRESET: /* unlink */
437 case -ESHUTDOWN: /* disconnect etc */
438 break;
439 case 0:
440 break;
441 }
442
443 spin_lock(&dev->lock);
444 /* Take the request struct off the active list and put it on the
445 * free list.
446 */
447 list_del_init(&req->list);
448 list_add(&req->list, &dev->tx_reqs);
449 wake_up_interruptible(&dev->tx_wait);
450 if (likely(list_empty(&dev->tx_reqs_active)))
451 wake_up_interruptible(&dev->tx_flush_wait);
452
453 spin_unlock(&dev->lock);
454}
455
456/*-------------------------------------------------------------------------*/
457
458static int
459printer_open(struct inode *inode, struct file *fd)
460{
461 struct printer_dev *dev;
462 unsigned long flags;
463 int ret = -EBUSY;
464
465 dev = container_of(inode->i_cdev, struct printer_dev, printer_cdev);
466
467 spin_lock_irqsave(&dev->lock, flags);
468
469 if (!dev->printer_cdev_open) {
470 dev->printer_cdev_open = 1;
471 fd->private_data = dev;
472 ret = 0;
473 /* Change the printer status to show that it's on-line. */
474 dev->printer_status |= PRINTER_SELECTED;
475 }
476
477 spin_unlock_irqrestore(&dev->lock, flags);
478
479 DBG(dev, "printer_open returned %x\n", ret);
480
481 return ret;
482}
483
484static int
485printer_close(struct inode *inode, struct file *fd)
486{
487 struct printer_dev *dev = fd->private_data;
488 unsigned long flags;
489
490 spin_lock_irqsave(&dev->lock, flags);
491 dev->printer_cdev_open = 0;
492 fd->private_data = NULL;
493 /* Change printer status to show that the printer is off-line. */
494 dev->printer_status &= ~PRINTER_SELECTED;
495 spin_unlock_irqrestore(&dev->lock, flags);
496
497 DBG(dev, "printer_close\n");
498
499 return 0;
500}
501
Craig W. Nadlercc901bb2008-03-20 14:46:26 -0700502/* This function must be called with interrupts turned off. */
503static void
504setup_rx_reqs(struct printer_dev *dev)
505{
506 struct usb_request *req;
507
508 while (likely(!list_empty(&dev->rx_reqs))) {
509 int error;
510
511 req = container_of(dev->rx_reqs.next,
512 struct usb_request, list);
513 list_del_init(&req->list);
514
515 /* The USB Host sends us whatever amount of data it wants to
516 * so we always set the length field to the full USB_BUFSIZE.
517 * If the amount of data is more than the read() caller asked
518 * for it will be stored in the request buffer until it is
519 * asked for by read().
520 */
521 req->length = USB_BUFSIZE;
522 req->complete = rx_complete;
523
524 error = usb_ep_queue(dev->out_ep, req, GFP_ATOMIC);
525 if (error) {
526 DBG(dev, "rx submit --> %d\n", error);
527 list_add(&req->list, &dev->rx_reqs);
528 break;
529 } else {
530 list_add(&req->list, &dev->rx_reqs_active);
531 }
532 }
533}
534
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800535static ssize_t
536printer_read(struct file *fd, char __user *buf, size_t len, loff_t *ptr)
537{
538 struct printer_dev *dev = fd->private_data;
539 unsigned long flags;
540 size_t size;
541 size_t bytes_copied;
542 struct usb_request *req;
543 /* This is a pointer to the current USB rx request. */
544 struct usb_request *current_rx_req;
545 /* This is the number of bytes in the current rx buffer. */
546 size_t current_rx_bytes;
547 /* This is a pointer to the current rx buffer. */
548 u8 *current_rx_buf;
549
550 if (len == 0)
551 return -EINVAL;
552
553 DBG(dev, "printer_read trying to read %d bytes\n", (int)len);
554
555 spin_lock(&dev->lock_printer_io);
556 spin_lock_irqsave(&dev->lock, flags);
557
558 /* We will use this flag later to check if a printer reset happened
559 * after we turn interrupts back on.
560 */
561 dev->reset_printer = 0;
562
Craig W. Nadlercc901bb2008-03-20 14:46:26 -0700563 setup_rx_reqs(dev);
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800564
565 bytes_copied = 0;
566 current_rx_req = dev->current_rx_req;
567 current_rx_bytes = dev->current_rx_bytes;
568 current_rx_buf = dev->current_rx_buf;
569 dev->current_rx_req = NULL;
570 dev->current_rx_bytes = 0;
571 dev->current_rx_buf = NULL;
572
573 /* Check if there is any data in the read buffers. Please note that
574 * current_rx_bytes is the number of bytes in the current rx buffer.
575 * If it is zero then check if there are any other rx_buffers that
576 * are on the completed list. We are only out of data if all rx
577 * buffers are empty.
578 */
579 if ((current_rx_bytes == 0) &&
580 (likely(list_empty(&dev->rx_buffers)))) {
581 /* Turn interrupts back on before sleeping. */
582 spin_unlock_irqrestore(&dev->lock, flags);
583
584 /*
585 * If no data is available check if this is a NON-Blocking
586 * call or not.
587 */
588 if (fd->f_flags & (O_NONBLOCK|O_NDELAY)) {
589 spin_unlock(&dev->lock_printer_io);
590 return -EAGAIN;
591 }
592
593 /* Sleep until data is available */
594 wait_event_interruptible(dev->rx_wait,
595 (likely(!list_empty(&dev->rx_buffers))));
596 spin_lock_irqsave(&dev->lock, flags);
597 }
598
599 /* We have data to return then copy it to the caller's buffer.*/
600 while ((current_rx_bytes || likely(!list_empty(&dev->rx_buffers)))
601 && len) {
602 if (current_rx_bytes == 0) {
603 req = container_of(dev->rx_buffers.next,
604 struct usb_request, list);
605 list_del_init(&req->list);
606
607 if (req->actual && req->buf) {
608 current_rx_req = req;
609 current_rx_bytes = req->actual;
610 current_rx_buf = req->buf;
611 } else {
612 list_add(&req->list, &dev->rx_reqs);
613 continue;
614 }
615 }
616
617 /* Don't leave irqs off while doing memory copies */
618 spin_unlock_irqrestore(&dev->lock, flags);
619
620 if (len > current_rx_bytes)
621 size = current_rx_bytes;
622 else
623 size = len;
624
625 size -= copy_to_user(buf, current_rx_buf, size);
626 bytes_copied += size;
627 len -= size;
628 buf += size;
629
630 spin_lock_irqsave(&dev->lock, flags);
631
Craig W. Nadlercc901bb2008-03-20 14:46:26 -0700632 /* We've disconnected or reset so return. */
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800633 if (dev->reset_printer) {
Craig W. Nadlercc901bb2008-03-20 14:46:26 -0700634 list_add(&current_rx_req->list, &dev->rx_reqs);
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800635 spin_unlock_irqrestore(&dev->lock, flags);
636 spin_unlock(&dev->lock_printer_io);
637 return -EAGAIN;
638 }
639
640 /* If we not returning all the data left in this RX request
641 * buffer then adjust the amount of data left in the buffer.
642 * Othewise if we are done with this RX request buffer then
643 * requeue it to get any incoming data from the USB host.
644 */
645 if (size < current_rx_bytes) {
646 current_rx_bytes -= size;
647 current_rx_buf += size;
648 } else {
649 list_add(&current_rx_req->list, &dev->rx_reqs);
650 current_rx_bytes = 0;
651 current_rx_buf = NULL;
652 current_rx_req = NULL;
653 }
654 }
655
656 dev->current_rx_req = current_rx_req;
657 dev->current_rx_bytes = current_rx_bytes;
658 dev->current_rx_buf = current_rx_buf;
659
660 spin_unlock_irqrestore(&dev->lock, flags);
661 spin_unlock(&dev->lock_printer_io);
662
663 DBG(dev, "printer_read returned %d bytes\n", (int)bytes_copied);
664
665 if (bytes_copied)
666 return bytes_copied;
667 else
668 return -EAGAIN;
669}
670
671static ssize_t
672printer_write(struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
673{
674 struct printer_dev *dev = fd->private_data;
675 unsigned long flags;
676 size_t size; /* Amount of data in a TX request. */
677 size_t bytes_copied = 0;
678 struct usb_request *req;
679
680 DBG(dev, "printer_write trying to send %d bytes\n", (int)len);
681
682 if (len == 0)
683 return -EINVAL;
684
685 spin_lock(&dev->lock_printer_io);
686 spin_lock_irqsave(&dev->lock, flags);
687
688 /* Check if a printer reset happens while we have interrupts on */
689 dev->reset_printer = 0;
690
691 /* Check if there is any available write buffers */
692 if (likely(list_empty(&dev->tx_reqs))) {
693 /* Turn interrupts back on before sleeping. */
694 spin_unlock_irqrestore(&dev->lock, flags);
695
696 /*
697 * If write buffers are available check if this is
698 * a NON-Blocking call or not.
699 */
700 if (fd->f_flags & (O_NONBLOCK|O_NDELAY)) {
701 spin_unlock(&dev->lock_printer_io);
702 return -EAGAIN;
703 }
704
705 /* Sleep until a write buffer is available */
706 wait_event_interruptible(dev->tx_wait,
707 (likely(!list_empty(&dev->tx_reqs))));
708 spin_lock_irqsave(&dev->lock, flags);
709 }
710
711 while (likely(!list_empty(&dev->tx_reqs)) && len) {
712
713 if (len > USB_BUFSIZE)
714 size = USB_BUFSIZE;
715 else
716 size = len;
717
718 req = container_of(dev->tx_reqs.next, struct usb_request,
719 list);
720 list_del_init(&req->list);
721
722 req->complete = tx_complete;
723 req->length = size;
724
725 /* Check if we need to send a zero length packet. */
726 if (len > size)
727 /* They will be more TX requests so no yet. */
728 req->zero = 0;
729 else
730 /* If the data amount is not a multple of the
731 * maxpacket size then send a zero length packet.
732 */
733 req->zero = ((len % dev->in_ep->maxpacket) == 0);
734
735 /* Don't leave irqs off while doing memory copies */
736 spin_unlock_irqrestore(&dev->lock, flags);
737
738 if (copy_from_user(req->buf, buf, size)) {
739 list_add(&req->list, &dev->tx_reqs);
740 spin_unlock(&dev->lock_printer_io);
741 return bytes_copied;
742 }
743
744 bytes_copied += size;
745 len -= size;
746 buf += size;
747
748 spin_lock_irqsave(&dev->lock, flags);
749
750 /* We've disconnected or reset so free the req and buffer */
751 if (dev->reset_printer) {
Craig W. Nadlercc901bb2008-03-20 14:46:26 -0700752 list_add(&req->list, &dev->tx_reqs);
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800753 spin_unlock_irqrestore(&dev->lock, flags);
754 spin_unlock(&dev->lock_printer_io);
755 return -EAGAIN;
756 }
757
758 if (usb_ep_queue(dev->in_ep, req, GFP_ATOMIC)) {
759 list_add(&req->list, &dev->tx_reqs);
760 spin_unlock_irqrestore(&dev->lock, flags);
761 spin_unlock(&dev->lock_printer_io);
762 return -EAGAIN;
763 }
764
765 list_add(&req->list, &dev->tx_reqs_active);
766
767 }
768
769 spin_unlock_irqrestore(&dev->lock, flags);
770 spin_unlock(&dev->lock_printer_io);
771
772 DBG(dev, "printer_write sent %d bytes\n", (int)bytes_copied);
773
774 if (bytes_copied) {
775 return bytes_copied;
776 } else {
777 return -EAGAIN;
778 }
779}
780
781static int
782printer_fsync(struct file *fd, struct dentry *dentry, int datasync)
783{
784 struct printer_dev *dev = fd->private_data;
785 unsigned long flags;
786 int tx_list_empty;
787
788 spin_lock_irqsave(&dev->lock, flags);
789 tx_list_empty = (likely(list_empty(&dev->tx_reqs)));
790 spin_unlock_irqrestore(&dev->lock, flags);
791
792 if (!tx_list_empty) {
793 /* Sleep until all data has been sent */
794 wait_event_interruptible(dev->tx_flush_wait,
795 (likely(list_empty(&dev->tx_reqs_active))));
796 }
797
798 return 0;
799}
800
801static unsigned int
802printer_poll(struct file *fd, poll_table *wait)
803{
804 struct printer_dev *dev = fd->private_data;
805 unsigned long flags;
806 int status = 0;
807
Craig W. Nadlercc901bb2008-03-20 14:46:26 -0700808 spin_lock(&dev->lock_printer_io);
809 spin_lock_irqsave(&dev->lock, flags);
810 setup_rx_reqs(dev);
811 spin_unlock_irqrestore(&dev->lock, flags);
812 spin_unlock(&dev->lock_printer_io);
813
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800814 poll_wait(fd, &dev->rx_wait, wait);
815 poll_wait(fd, &dev->tx_wait, wait);
816
817 spin_lock_irqsave(&dev->lock, flags);
818 if (likely(!list_empty(&dev->tx_reqs)))
819 status |= POLLOUT | POLLWRNORM;
820
Craig W. Nadlercc901bb2008-03-20 14:46:26 -0700821 if (likely(dev->current_rx_bytes) ||
822 likely(!list_empty(&dev->rx_buffers)))
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800823 status |= POLLIN | POLLRDNORM;
824
825 spin_unlock_irqrestore(&dev->lock, flags);
826
827 return status;
828}
829
830static int
831printer_ioctl(struct inode *inode, struct file *fd, unsigned int code,
832 unsigned long arg)
833{
834 struct printer_dev *dev = fd->private_data;
835 unsigned long flags;
836 int status = 0;
837
838 DBG(dev, "printer_ioctl: cmd=0x%4.4x, arg=%lu\n", code, arg);
839
840 /* handle ioctls */
841
842 spin_lock_irqsave(&dev->lock, flags);
843
844 switch (code) {
845 case GADGET_GET_PRINTER_STATUS:
846 status = (int)dev->printer_status;
847 break;
848 case GADGET_SET_PRINTER_STATUS:
849 dev->printer_status = (u8)arg;
850 break;
851 default:
852 /* could not handle ioctl */
853 DBG(dev, "printer_ioctl: ERROR cmd=0x%4.4xis not supported\n",
854 code);
855 status = -ENOTTY;
856 }
857
858 spin_unlock_irqrestore(&dev->lock, flags);
859
860 return status;
861}
862
863/* used after endpoint configuration */
864static struct file_operations printer_io_operations = {
865 .owner = THIS_MODULE,
866 .open = printer_open,
867 .read = printer_read,
868 .write = printer_write,
869 .fsync = printer_fsync,
870 .poll = printer_poll,
871 .ioctl = printer_ioctl,
872 .release = printer_close
873};
874
875/*-------------------------------------------------------------------------*/
876
877static int
878set_printer_interface(struct printer_dev *dev)
879{
880 int result = 0;
881
882 dev->in = ep_desc(dev->gadget, &hs_ep_in_desc, &fs_ep_in_desc);
883 dev->in_ep->driver_data = dev;
884
885 dev->out = ep_desc(dev->gadget, &hs_ep_out_desc, &fs_ep_out_desc);
886 dev->out_ep->driver_data = dev;
887
888 result = usb_ep_enable(dev->in_ep, dev->in);
889 if (result != 0) {
890 DBG(dev, "enable %s --> %d\n", dev->in_ep->name, result);
891 goto done;
892 }
893
894 result = usb_ep_enable(dev->out_ep, dev->out);
895 if (result != 0) {
896 DBG(dev, "enable %s --> %d\n", dev->in_ep->name, result);
897 goto done;
898 }
899
900done:
901 /* on error, disable any endpoints */
902 if (result != 0) {
903 (void) usb_ep_disable(dev->in_ep);
904 (void) usb_ep_disable(dev->out_ep);
905 dev->in = NULL;
906 dev->out = NULL;
907 }
908
909 /* caller is responsible for cleanup on error */
910 return result;
911}
912
913static void printer_reset_interface(struct printer_dev *dev)
914{
915 if (dev->interface < 0)
916 return;
917
918 DBG(dev, "%s\n", __FUNCTION__);
919
920 if (dev->in)
921 usb_ep_disable(dev->in_ep);
922
923 if (dev->out)
924 usb_ep_disable(dev->out_ep);
925
926 dev->interface = -1;
927}
928
929/* change our operational config. must agree with the code
930 * that returns config descriptors, and altsetting code.
931 */
932static int
933printer_set_config(struct printer_dev *dev, unsigned number)
934{
935 int result = 0;
936 struct usb_gadget *gadget = dev->gadget;
937
938 if (gadget_is_sa1100(gadget) && dev->config) {
939 /* tx fifo is full, but we can't clear it...*/
940 INFO(dev, "can't change configurations\n");
941 return -ESPIPE;
942 }
943
944 switch (number) {
945 case DEV_CONFIG_VALUE:
946 result = 0;
947 break;
948 default:
949 result = -EINVAL;
950 /* FALL THROUGH */
951 case 0:
952 break;
953 }
954
955 if (result) {
956 usb_gadget_vbus_draw(dev->gadget,
957 dev->gadget->is_otg ? 8 : 100);
958 } else {
959 char *speed;
960 unsigned power;
961
962 power = 2 * config_desc.bMaxPower;
963 usb_gadget_vbus_draw(dev->gadget, power);
964
965 switch (gadget->speed) {
966 case USB_SPEED_FULL: speed = "full"; break;
967#ifdef CONFIG_USB_GADGET_DUALSPEED
968 case USB_SPEED_HIGH: speed = "high"; break;
969#endif
970 default: speed = "?"; break;
971 }
972
973 dev->config = number;
974 INFO(dev, "%s speed config #%d: %d mA, %s\n",
975 speed, number, power, driver_desc);
976 }
977 return result;
978}
979
980static int
981config_buf(enum usb_device_speed speed, u8 *buf, u8 type, unsigned index,
982 int is_otg)
983{
984 int len;
985 const struct usb_descriptor_header **function;
986#ifdef CONFIG_USB_GADGET_DUALSPEED
987 int hs = (speed == USB_SPEED_HIGH);
988
989 if (type == USB_DT_OTHER_SPEED_CONFIG)
990 hs = !hs;
991
992 if (hs) {
993 function = hs_printer_function;
994 } else {
995 function = fs_printer_function;
996 }
997#else
998 function = fs_printer_function;
999#endif
1000
1001 if (index >= device_desc.bNumConfigurations)
1002 return -EINVAL;
1003
1004 /* for now, don't advertise srp-only devices */
1005 if (!is_otg)
1006 function++;
1007
1008 len = usb_gadget_config_buf(&config_desc, buf, USB_DESC_BUFSIZE,
1009 function);
1010 if (len < 0)
1011 return len;
1012 ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
1013 return len;
1014}
1015
1016/* Change our operational Interface. */
1017static int
1018set_interface(struct printer_dev *dev, unsigned number)
1019{
1020 int result = 0;
1021
1022 if (gadget_is_sa1100(dev->gadget) && dev->interface < 0) {
1023 /* tx fifo is full, but we can't clear it...*/
1024 INFO(dev, "can't change interfaces\n");
1025 return -ESPIPE;
1026 }
1027
1028 /* Free the current interface */
1029 switch (dev->interface) {
1030 case PRINTER_INTERFACE:
1031 printer_reset_interface(dev);
1032 break;
1033 }
1034
1035 switch (number) {
1036 case PRINTER_INTERFACE:
1037 result = set_printer_interface(dev);
1038 if (result) {
1039 printer_reset_interface(dev);
1040 } else {
1041 dev->interface = PRINTER_INTERFACE;
1042 }
1043 break;
1044 default:
1045 result = -EINVAL;
1046 /* FALL THROUGH */
1047 }
1048
1049 if (!result)
1050 INFO(dev, "Using interface %x\n", number);
1051
1052 return result;
1053}
1054
1055static void printer_setup_complete(struct usb_ep *ep, struct usb_request *req)
1056{
1057 if (req->status || req->actual != req->length)
1058 DBG((struct printer_dev *) ep->driver_data,
1059 "setup complete --> %d, %d/%d\n",
1060 req->status, req->actual, req->length);
1061}
1062
1063static void printer_soft_reset(struct printer_dev *dev)
1064{
1065 struct usb_request *req;
1066
1067 INFO(dev, "Received Printer Reset Request\n");
1068
1069 if (usb_ep_disable(dev->in_ep))
1070 DBG(dev, "Failed to disable USB in_ep\n");
1071 if (usb_ep_disable(dev->out_ep))
1072 DBG(dev, "Failed to disable USB out_ep\n");
1073
1074 if (dev->current_rx_req != NULL) {
1075 list_add(&dev->current_rx_req->list, &dev->rx_reqs);
1076 dev->current_rx_req = NULL;
1077 }
1078 dev->current_rx_bytes = 0;
1079 dev->current_rx_buf = NULL;
1080 dev->reset_printer = 1;
1081
1082 while (likely(!(list_empty(&dev->rx_buffers)))) {
1083 req = container_of(dev->rx_buffers.next, struct usb_request,
1084 list);
1085 list_del_init(&req->list);
1086 list_add(&req->list, &dev->rx_reqs);
1087 }
1088
1089 while (likely(!(list_empty(&dev->rx_reqs_active)))) {
1090 req = container_of(dev->rx_buffers.next, struct usb_request,
1091 list);
1092 list_del_init(&req->list);
1093 list_add(&req->list, &dev->rx_reqs);
1094 }
1095
1096 while (likely(!(list_empty(&dev->tx_reqs_active)))) {
1097 req = container_of(dev->tx_reqs_active.next,
1098 struct usb_request, list);
1099 list_del_init(&req->list);
1100 list_add(&req->list, &dev->tx_reqs);
1101 }
1102
1103 if (usb_ep_enable(dev->in_ep, dev->in))
1104 DBG(dev, "Failed to enable USB in_ep\n");
1105 if (usb_ep_enable(dev->out_ep, dev->out))
1106 DBG(dev, "Failed to enable USB out_ep\n");
1107
Craig W. Nadlercc901bb2008-03-20 14:46:26 -07001108 wake_up_interruptible(&dev->rx_wait);
Craig W. Nadler25a010c2007-11-11 15:00:15 -08001109 wake_up_interruptible(&dev->tx_wait);
1110 wake_up_interruptible(&dev->tx_flush_wait);
1111}
1112
1113/*-------------------------------------------------------------------------*/
1114
1115/*
1116 * The setup() callback implements all the ep0 functionality that's not
1117 * handled lower down.
1118 */
1119static int
1120printer_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1121{
1122 struct printer_dev *dev = get_gadget_data(gadget);
1123 struct usb_request *req = dev->req;
1124 int value = -EOPNOTSUPP;
1125 u16 wIndex = le16_to_cpu(ctrl->wIndex);
1126 u16 wValue = le16_to_cpu(ctrl->wValue);
1127 u16 wLength = le16_to_cpu(ctrl->wLength);
1128
1129 DBG(dev, "ctrl req%02x.%02x v%04x i%04x l%d\n",
1130 ctrl->bRequestType, ctrl->bRequest, wValue, wIndex, wLength);
1131
1132 req->complete = printer_setup_complete;
1133
1134 switch (ctrl->bRequestType&USB_TYPE_MASK) {
1135
1136 case USB_TYPE_STANDARD:
1137 switch (ctrl->bRequest) {
1138
1139 case USB_REQ_GET_DESCRIPTOR:
1140 if (ctrl->bRequestType != USB_DIR_IN)
1141 break;
1142 switch (wValue >> 8) {
1143
1144 case USB_DT_DEVICE:
1145 value = min(wLength, (u16) sizeof device_desc);
1146 memcpy(req->buf, &device_desc, value);
1147 break;
1148#ifdef CONFIG_USB_GADGET_DUALSPEED
1149 case USB_DT_DEVICE_QUALIFIER:
1150 if (!gadget->is_dualspeed)
1151 break;
1152 value = min(wLength,
1153 (u16) sizeof dev_qualifier);
1154 memcpy(req->buf, &dev_qualifier, value);
1155 break;
1156
1157 case USB_DT_OTHER_SPEED_CONFIG:
1158 if (!gadget->is_dualspeed)
1159 break;
1160 /* FALLTHROUGH */
1161#endif /* CONFIG_USB_GADGET_DUALSPEED */
1162 case USB_DT_CONFIG:
1163 value = config_buf(gadget->speed, req->buf,
1164 wValue >> 8,
1165 wValue & 0xff,
1166 gadget->is_otg);
1167 if (value >= 0)
1168 value = min(wLength, (u16) value);
1169 break;
1170
1171 case USB_DT_STRING:
1172 value = usb_gadget_get_string(&stringtab,
1173 wValue & 0xff, req->buf);
1174 if (value >= 0)
1175 value = min(wLength, (u16) value);
1176 break;
1177 }
1178 break;
1179
1180 case USB_REQ_SET_CONFIGURATION:
1181 if (ctrl->bRequestType != 0)
1182 break;
1183 if (gadget->a_hnp_support)
1184 DBG(dev, "HNP available\n");
1185 else if (gadget->a_alt_hnp_support)
1186 DBG(dev, "HNP needs a different root port\n");
1187 value = printer_set_config(dev, wValue);
1188 break;
1189 case USB_REQ_GET_CONFIGURATION:
1190 if (ctrl->bRequestType != USB_DIR_IN)
1191 break;
1192 *(u8 *)req->buf = dev->config;
1193 value = min(wLength, (u16) 1);
1194 break;
1195
1196 case USB_REQ_SET_INTERFACE:
1197 if (ctrl->bRequestType != USB_RECIP_INTERFACE ||
1198 !dev->config)
1199 break;
1200
1201 value = set_interface(dev, PRINTER_INTERFACE);
1202 break;
1203 case USB_REQ_GET_INTERFACE:
1204 if (ctrl->bRequestType !=
1205 (USB_DIR_IN|USB_RECIP_INTERFACE)
1206 || !dev->config)
1207 break;
1208
1209 *(u8 *)req->buf = dev->interface;
1210 value = min(wLength, (u16) 1);
1211 break;
1212
1213 default:
1214 goto unknown;
1215 }
1216 break;
1217
1218 case USB_TYPE_CLASS:
1219 switch (ctrl->bRequest) {
1220 case 0: /* Get the IEEE-1284 PNP String */
1221 /* Only one printer interface is supported. */
1222 if ((wIndex>>8) != PRINTER_INTERFACE)
1223 break;
1224
1225 value = (pnp_string[0]<<8)|pnp_string[1];
1226 memcpy(req->buf, pnp_string, value);
1227 DBG(dev, "1284 PNP String: %x %s\n", value,
1228 &pnp_string[2]);
1229 break;
1230
1231 case 1: /* Get Port Status */
1232 /* Only one printer interface is supported. */
1233 if (wIndex != PRINTER_INTERFACE)
1234 break;
1235
1236 *(u8 *)req->buf = dev->printer_status;
1237 value = min(wLength, (u16) 1);
1238 break;
1239
1240 case 2: /* Soft Reset */
1241 /* Only one printer interface is supported. */
1242 if (wIndex != PRINTER_INTERFACE)
1243 break;
1244
1245 printer_soft_reset(dev);
1246
1247 value = 0;
1248 break;
1249
1250 default:
1251 goto unknown;
1252 }
1253 break;
1254
1255 default:
1256unknown:
1257 VDBG(dev,
1258 "unknown ctrl req%02x.%02x v%04x i%04x l%d\n",
1259 ctrl->bRequestType, ctrl->bRequest,
1260 wValue, wIndex, wLength);
1261 break;
1262 }
1263
1264 /* respond with data transfer before status phase? */
1265 if (value >= 0) {
1266 req->length = value;
1267 req->zero = value < wLength
1268 && (value % gadget->ep0->maxpacket) == 0;
1269 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
1270 if (value < 0) {
1271 DBG(dev, "ep_queue --> %d\n", value);
1272 req->status = 0;
1273 printer_setup_complete(gadget->ep0, req);
1274 }
1275 }
1276
1277 /* host either stalls (value < 0) or reports success */
1278 return value;
1279}
1280
1281static void
1282printer_disconnect(struct usb_gadget *gadget)
1283{
1284 struct printer_dev *dev = get_gadget_data(gadget);
1285 unsigned long flags;
1286
1287 DBG(dev, "%s\n", __FUNCTION__);
1288
1289 spin_lock_irqsave(&dev->lock, flags);
1290
1291 printer_reset_interface(dev);
1292
1293 spin_unlock_irqrestore(&dev->lock, flags);
1294}
1295
1296static void
1297printer_unbind(struct usb_gadget *gadget)
1298{
1299 struct printer_dev *dev = get_gadget_data(gadget);
1300 struct usb_request *req;
1301
1302
1303 DBG(dev, "%s\n", __FUNCTION__);
1304
1305 /* Remove sysfs files */
1306 device_destroy(usb_gadget_class, g_printer_devno);
1307
1308 /* Remove Character Device */
1309 cdev_del(&dev->printer_cdev);
1310
1311 /* we must already have been disconnected ... no i/o may be active */
1312 WARN_ON(!list_empty(&dev->tx_reqs_active));
1313 WARN_ON(!list_empty(&dev->rx_reqs_active));
1314
1315 /* Free all memory for this driver. */
1316 while (!list_empty(&dev->tx_reqs)) {
1317 req = container_of(dev->tx_reqs.next, struct usb_request,
1318 list);
1319 list_del(&req->list);
1320 printer_req_free(dev->in_ep, req);
1321 }
1322
Adrian Bunkefa66f12008-02-09 03:16:03 -08001323 if (dev->current_rx_req != NULL)
Craig W. Nadler25a010c2007-11-11 15:00:15 -08001324 printer_req_free(dev->out_ep, dev->current_rx_req);
1325
1326 while (!list_empty(&dev->rx_reqs)) {
1327 req = container_of(dev->rx_reqs.next,
1328 struct usb_request, list);
1329 list_del(&req->list);
1330 printer_req_free(dev->out_ep, req);
1331 }
1332
1333 while (!list_empty(&dev->rx_buffers)) {
1334 req = container_of(dev->rx_buffers.next,
1335 struct usb_request, list);
1336 list_del(&req->list);
1337 printer_req_free(dev->out_ep, req);
1338 }
1339
1340 if (dev->req) {
1341 printer_req_free(gadget->ep0, dev->req);
1342 dev->req = NULL;
1343 }
1344
1345 set_gadget_data(gadget, NULL);
1346}
1347
1348static int __init
1349printer_bind(struct usb_gadget *gadget)
1350{
1351 struct printer_dev *dev;
1352 struct usb_ep *in_ep, *out_ep;
1353 int status = -ENOMEM;
1354 int gcnum;
1355 size_t len;
1356 u32 i;
1357 struct usb_request *req;
1358
1359 dev = &usb_printer_gadget;
1360
1361
1362 /* Setup the sysfs files for the printer gadget. */
1363 dev->pdev = device_create(usb_gadget_class, NULL, g_printer_devno,
1364 "g_printer");
1365 if (IS_ERR(dev->pdev)) {
1366 ERROR(dev, "Failed to create device: g_printer\n");
1367 goto fail;
1368 }
1369
1370 /*
1371 * Register a character device as an interface to a user mode
1372 * program that handles the printer specific functionality.
1373 */
1374 cdev_init(&dev->printer_cdev, &printer_io_operations);
1375 dev->printer_cdev.owner = THIS_MODULE;
1376 status = cdev_add(&dev->printer_cdev, g_printer_devno, 1);
1377 if (status) {
1378 ERROR(dev, "Failed to open char device\n");
1379 goto fail;
1380 }
1381
1382 if (gadget_is_sa1100(gadget)) {
1383 /* hardware can't write zero length packets. */
1384 ERROR(dev, "SA1100 controller is unsupport by this driver\n");
1385 goto fail;
1386 }
1387
1388 gcnum = usb_gadget_controller_number(gadget);
1389 if (gcnum >= 0) {
1390 device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
1391 } else {
1392 dev_warn(&gadget->dev, "controller '%s' not recognized\n",
1393 gadget->name);
1394 /* unrecognized, but safe unless bulk is REALLY quirky */
1395 device_desc.bcdDevice =
1396 __constant_cpu_to_le16(0xFFFF);
1397 }
1398 snprintf(manufacturer, sizeof(manufacturer), "%s %s with %s",
1399 init_utsname()->sysname, init_utsname()->release,
1400 gadget->name);
1401
1402 device_desc.idVendor =
1403 __constant_cpu_to_le16(PRINTER_VENDOR_NUM);
1404 device_desc.idProduct =
1405 __constant_cpu_to_le16(PRINTER_PRODUCT_NUM);
1406
1407 /* support optional vendor/distro customization */
1408 if (idVendor) {
1409 if (!idProduct) {
1410 dev_err(&gadget->dev, "idVendor needs idProduct!\n");
1411 return -ENODEV;
1412 }
1413 device_desc.idVendor = cpu_to_le16(idVendor);
1414 device_desc.idProduct = cpu_to_le16(idProduct);
1415 if (bcdDevice)
1416 device_desc.bcdDevice = cpu_to_le16(bcdDevice);
1417 }
1418
1419 if (iManufacturer)
1420 strlcpy(manufacturer, iManufacturer, sizeof manufacturer);
1421
1422 if (iProduct)
1423 strlcpy(product_desc, iProduct, sizeof product_desc);
1424
1425 if (iSerialNum)
1426 strlcpy(serial_num, iSerialNum, sizeof serial_num);
1427
1428 if (iPNPstring)
1429 strlcpy(&pnp_string[2], iPNPstring, (sizeof pnp_string)-2);
1430
1431 len = strlen(pnp_string);
1432 pnp_string[0] = (len >> 8) & 0xFF;
1433 pnp_string[1] = len & 0xFF;
1434
1435 /* all we really need is bulk IN/OUT */
1436 usb_ep_autoconfig_reset(gadget);
1437 in_ep = usb_ep_autoconfig(gadget, &fs_ep_in_desc);
1438 if (!in_ep) {
1439autoconf_fail:
1440 dev_err(&gadget->dev, "can't autoconfigure on %s\n",
1441 gadget->name);
1442 return -ENODEV;
1443 }
1444 in_ep->driver_data = in_ep; /* claim */
1445
1446 out_ep = usb_ep_autoconfig(gadget, &fs_ep_out_desc);
1447 if (!out_ep)
1448 goto autoconf_fail;
1449 out_ep->driver_data = out_ep; /* claim */
1450
1451#ifdef CONFIG_USB_GADGET_DUALSPEED
1452 /* assumes ep0 uses the same value for both speeds ... */
1453 dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
1454
1455 /* and that all endpoints are dual-speed */
1456 hs_ep_in_desc.bEndpointAddress = fs_ep_in_desc.bEndpointAddress;
1457 hs_ep_out_desc.bEndpointAddress = fs_ep_out_desc.bEndpointAddress;
1458#endif /* DUALSPEED */
1459
1460 device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
1461 usb_gadget_set_selfpowered(gadget);
1462
1463 if (gadget->is_otg) {
1464 otg_desc.bmAttributes |= USB_OTG_HNP,
1465 config_desc.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1466 config_desc.bMaxPower = 4;
1467 }
1468
1469 spin_lock_init(&dev->lock);
1470 spin_lock_init(&dev->lock_printer_io);
1471 INIT_LIST_HEAD(&dev->tx_reqs);
1472 INIT_LIST_HEAD(&dev->tx_reqs_active);
1473 INIT_LIST_HEAD(&dev->rx_reqs);
1474 INIT_LIST_HEAD(&dev->rx_reqs_active);
1475 INIT_LIST_HEAD(&dev->rx_buffers);
1476 init_waitqueue_head(&dev->rx_wait);
1477 init_waitqueue_head(&dev->tx_wait);
1478 init_waitqueue_head(&dev->tx_flush_wait);
1479
1480 dev->config = 0;
1481 dev->interface = -1;
1482 dev->printer_cdev_open = 0;
1483 dev->printer_status = PRINTER_NOT_ERROR;
1484 dev->current_rx_req = NULL;
1485 dev->current_rx_bytes = 0;
1486 dev->current_rx_buf = NULL;
1487
1488 dev->in_ep = in_ep;
1489 dev->out_ep = out_ep;
1490
1491 /* preallocate control message data and buffer */
1492 dev->req = printer_req_alloc(gadget->ep0, USB_DESC_BUFSIZE,
1493 GFP_KERNEL);
1494 if (!dev->req) {
1495 status = -ENOMEM;
1496 goto fail;
1497 }
1498
1499 for (i = 0; i < QLEN; i++) {
1500 req = printer_req_alloc(dev->in_ep, USB_BUFSIZE, GFP_KERNEL);
1501 if (!req) {
1502 while (!list_empty(&dev->tx_reqs)) {
1503 req = container_of(dev->tx_reqs.next,
1504 struct usb_request, list);
1505 list_del(&req->list);
1506 printer_req_free(dev->in_ep, req);
1507 }
1508 return -ENOMEM;
1509 }
1510 list_add(&req->list, &dev->tx_reqs);
1511 }
1512
1513 for (i = 0; i < QLEN; i++) {
1514 req = printer_req_alloc(dev->out_ep, USB_BUFSIZE, GFP_KERNEL);
1515 if (!req) {
1516 while (!list_empty(&dev->rx_reqs)) {
1517 req = container_of(dev->rx_reqs.next,
1518 struct usb_request, list);
1519 list_del(&req->list);
1520 printer_req_free(dev->out_ep, req);
1521 }
1522 return -ENOMEM;
1523 }
1524 list_add(&req->list, &dev->rx_reqs);
1525 }
1526
1527 dev->req->complete = printer_setup_complete;
1528
1529 /* finish hookup to lower layer ... */
1530 dev->gadget = gadget;
1531 set_gadget_data(gadget, dev);
1532 gadget->ep0->driver_data = dev;
1533
1534 INFO(dev, "%s, version: " DRIVER_VERSION "\n", driver_desc);
1535 INFO(dev, "using %s, OUT %s IN %s\n", gadget->name, out_ep->name,
1536 in_ep->name);
1537
1538 return 0;
1539
1540fail:
1541 printer_unbind(gadget);
1542 return status;
1543}
1544
1545/*-------------------------------------------------------------------------*/
1546
1547static struct usb_gadget_driver printer_driver = {
1548 .speed = DEVSPEED,
1549
1550 .function = (char *) driver_desc,
1551 .bind = printer_bind,
1552 .unbind = printer_unbind,
1553
1554 .setup = printer_setup,
1555 .disconnect = printer_disconnect,
1556
1557 .driver = {
1558 .name = (char *) shortname,
1559 .owner = THIS_MODULE,
1560 },
1561};
1562
1563MODULE_DESCRIPTION(DRIVER_DESC);
1564MODULE_AUTHOR("Craig Nadler");
1565MODULE_LICENSE("GPL");
1566
1567static int __init
1568init(void)
1569{
1570 int status;
1571
1572 usb_gadget_class = class_create(THIS_MODULE, "usb_printer_gadget");
1573 if (IS_ERR(usb_gadget_class)) {
1574 status = PTR_ERR(usb_gadget_class);
1575 ERROR(dev, "unable to create usb_gadget class %d\n", status);
1576 return status;
1577 }
1578
1579 status = alloc_chrdev_region(&g_printer_devno, 0, 1,
1580 "USB printer gadget");
1581 if (status) {
1582 ERROR(dev, "alloc_chrdev_region %d\n", status);
1583 class_destroy(usb_gadget_class);
1584 return status;
1585 }
1586
1587 status = usb_gadget_register_driver(&printer_driver);
1588 if (status) {
1589 class_destroy(usb_gadget_class);
1590 unregister_chrdev_region(g_printer_devno, 1);
1591 DBG(dev, "usb_gadget_register_driver %x\n", status);
1592 }
1593
1594 return status;
1595}
1596module_init(init);
1597
1598static void __exit
1599cleanup(void)
1600{
1601 int status;
1602
1603 spin_lock(&usb_printer_gadget.lock_printer_io);
1604 class_destroy(usb_gadget_class);
1605 unregister_chrdev_region(g_printer_devno, 2);
1606
1607 status = usb_gadget_unregister_driver(&printer_driver);
1608 if (status)
1609 ERROR(dev, "usb_gadget_unregister_driver %x\n", status);
1610
1611 spin_unlock(&usb_printer_gadget.lock_printer_io);
1612}
1613module_exit(cleanup);