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