]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/usb/gadget/printer.c
USB: remove incorrect struct class_device from the printer gadget
[mirror_ubuntu-bionic-kernel.git] / drivers / usb / gadget / printer.c
1 /*
2 * printer.c -- Printer gadget driver
3 *
4 * Copyright (C) 2003-2005 David Brownell
5 * Copyright (C) 2006 Craig W. Nadler
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * 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
59 static const char shortname [] = "printer";
60 static const char driver_desc [] = DRIVER_DESC;
61
62 static dev_t g_printer_devno;
63
64 static struct class *usb_gadget_class;
65
66 /*-------------------------------------------------------------------------*/
67
68 struct 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 cdev printer_cdev;
96 struct device *pdev;
97 u8 printer_cdev_open;
98 wait_queue_head_t wait;
99 };
100
101 static 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
119 static ushort __initdata idVendor;
120 module_param(idVendor, ushort, S_IRUGO);
121 MODULE_PARM_DESC(idVendor, "USB Vendor ID");
122
123 static ushort __initdata idProduct;
124 module_param(idProduct, ushort, S_IRUGO);
125 MODULE_PARM_DESC(idProduct, "USB Product ID");
126
127 static ushort __initdata bcdDevice;
128 module_param(bcdDevice, ushort, S_IRUGO);
129 MODULE_PARM_DESC(bcdDevice, "USB Device version (BCD)");
130
131 static char *__initdata iManufacturer;
132 module_param(iManufacturer, charp, S_IRUGO);
133 MODULE_PARM_DESC(iManufacturer, "USB Manufacturer string");
134
135 static char *__initdata iProduct;
136 module_param(iProduct, charp, S_IRUGO);
137 MODULE_PARM_DESC(iProduct, "USB Product string");
138
139 static char *__initdata iSerialNum;
140 module_param(iSerialNum, charp, S_IRUGO);
141 MODULE_PARM_DESC(iSerialNum, "1");
142
143 static char *__initdata iPNPstring;
144 module_param(iPNPstring, charp, S_IRUGO);
145 MODULE_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. */
148 static unsigned qlen = 10;
149 module_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
211 static 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
226 static struct usb_otg_descriptor otg_desc = {
227 .bLength = sizeof otg_desc,
228 .bDescriptorType = USB_DT_OTG,
229 .bmAttributes = USB_OTG_SRP
230 };
231
232 static 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
244 static 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
255 static 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
262 static 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
269 static 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
284 static 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
291 static 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
298 static 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
306 static 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
328 static char manufacturer [50];
329 static char product_desc [40] = DRIVER_DESC;
330 static char serial_num [40] = "1";
331 static char pnp_string [1024] =
332 "XXMFG:linux;MDL:g_printer;CLS:PRINTER;SN:1;";
333
334 /* static strings, in UTF-8 */
335 static struct usb_string strings [] = {
336 { STRING_MANUFACTURER, manufacturer, },
337 { STRING_PRODUCT, product_desc, },
338 { STRING_SERIALNUM, serial_num, },
339 { } /* end of list */
340 };
341
342 static struct usb_gadget_strings stringtab = {
343 .language = 0x0409, /* en-us */
344 .strings = strings,
345 };
346
347 /*-------------------------------------------------------------------------*/
348
349 static struct usb_request *
350 printer_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
368 static void
369 printer_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
379 static 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:
393 list_add_tail(&req->list, &dev->rx_buffers);
394 wake_up_interruptible(&dev->rx_wait);
395 DBG(dev, "G_Printer : rx length %d\n", req->actual);
396 break;
397
398 /* software-driven interface shutdown */
399 case -ECONNRESET: /* unlink */
400 case -ESHUTDOWN: /* disconnect etc */
401 VDBG(dev, "rx shutdown, code %d\n", status);
402 list_add(&req->list, &dev->rx_reqs);
403 break;
404
405 /* for hardware automagic (such as pxa) */
406 case -ECONNABORTED: /* endpoint reset */
407 DBG(dev, "rx %s reset\n", ep->name);
408 list_add(&req->list, &dev->rx_reqs);
409 break;
410
411 /* data overrun */
412 case -EOVERFLOW:
413 /* FALLTHROUGH */
414
415 default:
416 DBG(dev, "rx status %d\n", status);
417 list_add(&req->list, &dev->rx_reqs);
418 break;
419 }
420 spin_unlock_irqrestore(&dev->lock, flags);
421 }
422
423 static void tx_complete(struct usb_ep *ep, struct usb_request *req)
424 {
425 struct printer_dev *dev = ep->driver_data;
426
427 switch (req->status) {
428 default:
429 VDBG(dev, "tx err %d\n", req->status);
430 /* FALLTHROUGH */
431 case -ECONNRESET: /* unlink */
432 case -ESHUTDOWN: /* disconnect etc */
433 break;
434 case 0:
435 break;
436 }
437
438 spin_lock(&dev->lock);
439 /* Take the request struct off the active list and put it on the
440 * free list.
441 */
442 list_del_init(&req->list);
443 list_add(&req->list, &dev->tx_reqs);
444 wake_up_interruptible(&dev->tx_wait);
445 if (likely(list_empty(&dev->tx_reqs_active)))
446 wake_up_interruptible(&dev->tx_flush_wait);
447
448 spin_unlock(&dev->lock);
449 }
450
451 /*-------------------------------------------------------------------------*/
452
453 static int
454 printer_open(struct inode *inode, struct file *fd)
455 {
456 struct printer_dev *dev;
457 unsigned long flags;
458 int ret = -EBUSY;
459
460 dev = container_of(inode->i_cdev, struct printer_dev, printer_cdev);
461
462 spin_lock_irqsave(&dev->lock, flags);
463
464 if (!dev->printer_cdev_open) {
465 dev->printer_cdev_open = 1;
466 fd->private_data = dev;
467 ret = 0;
468 /* Change the printer status to show that it's on-line. */
469 dev->printer_status |= PRINTER_SELECTED;
470 }
471
472 spin_unlock_irqrestore(&dev->lock, flags);
473
474 DBG(dev, "printer_open returned %x\n", ret);
475
476 return ret;
477 }
478
479 static int
480 printer_close(struct inode *inode, struct file *fd)
481 {
482 struct printer_dev *dev = fd->private_data;
483 unsigned long flags;
484
485 spin_lock_irqsave(&dev->lock, flags);
486 dev->printer_cdev_open = 0;
487 fd->private_data = NULL;
488 /* Change printer status to show that the printer is off-line. */
489 dev->printer_status &= ~PRINTER_SELECTED;
490 spin_unlock_irqrestore(&dev->lock, flags);
491
492 DBG(dev, "printer_close\n");
493
494 return 0;
495 }
496
497 static ssize_t
498 printer_read(struct file *fd, char __user *buf, size_t len, loff_t *ptr)
499 {
500 struct printer_dev *dev = fd->private_data;
501 unsigned long flags;
502 size_t size;
503 size_t bytes_copied;
504 struct usb_request *req;
505 /* This is a pointer to the current USB rx request. */
506 struct usb_request *current_rx_req;
507 /* This is the number of bytes in the current rx buffer. */
508 size_t current_rx_bytes;
509 /* This is a pointer to the current rx buffer. */
510 u8 *current_rx_buf;
511
512 if (len == 0)
513 return -EINVAL;
514
515 DBG(dev, "printer_read trying to read %d bytes\n", (int)len);
516
517 spin_lock(&dev->lock_printer_io);
518 spin_lock_irqsave(&dev->lock, flags);
519
520 /* We will use this flag later to check if a printer reset happened
521 * after we turn interrupts back on.
522 */
523 dev->reset_printer = 0;
524
525 while (likely(!list_empty(&dev->rx_reqs))) {
526 int error;
527
528 req = container_of(dev->rx_reqs.next,
529 struct usb_request, list);
530 list_del_init(&req->list);
531
532 /* The USB Host sends us whatever amount of data it wants to
533 * so we always set the length field to the full USB_BUFSIZE.
534 * If the amount of data is more than the read() caller asked
535 * for it will be stored in the request buffer until it is
536 * asked for by read().
537 */
538 req->length = USB_BUFSIZE;
539 req->complete = rx_complete;
540
541 error = usb_ep_queue(dev->out_ep, req, GFP_ATOMIC);
542 if (error) {
543 DBG(dev, "rx submit --> %d\n", error);
544 list_add(&req->list, &dev->rx_reqs);
545 break;
546 } else {
547 list_add(&req->list, &dev->rx_reqs_active);
548 }
549 }
550
551 bytes_copied = 0;
552 current_rx_req = dev->current_rx_req;
553 current_rx_bytes = dev->current_rx_bytes;
554 current_rx_buf = dev->current_rx_buf;
555 dev->current_rx_req = NULL;
556 dev->current_rx_bytes = 0;
557 dev->current_rx_buf = NULL;
558
559 /* Check if there is any data in the read buffers. Please note that
560 * current_rx_bytes is the number of bytes in the current rx buffer.
561 * If it is zero then check if there are any other rx_buffers that
562 * are on the completed list. We are only out of data if all rx
563 * buffers are empty.
564 */
565 if ((current_rx_bytes == 0) &&
566 (likely(list_empty(&dev->rx_buffers)))) {
567 /* Turn interrupts back on before sleeping. */
568 spin_unlock_irqrestore(&dev->lock, flags);
569
570 /*
571 * If no data is available check if this is a NON-Blocking
572 * call or not.
573 */
574 if (fd->f_flags & (O_NONBLOCK|O_NDELAY)) {
575 spin_unlock(&dev->lock_printer_io);
576 return -EAGAIN;
577 }
578
579 /* Sleep until data is available */
580 wait_event_interruptible(dev->rx_wait,
581 (likely(!list_empty(&dev->rx_buffers))));
582 spin_lock_irqsave(&dev->lock, flags);
583 }
584
585 /* We have data to return then copy it to the caller's buffer.*/
586 while ((current_rx_bytes || likely(!list_empty(&dev->rx_buffers)))
587 && len) {
588 if (current_rx_bytes == 0) {
589 req = container_of(dev->rx_buffers.next,
590 struct usb_request, list);
591 list_del_init(&req->list);
592
593 if (req->actual && req->buf) {
594 current_rx_req = req;
595 current_rx_bytes = req->actual;
596 current_rx_buf = req->buf;
597 } else {
598 list_add(&req->list, &dev->rx_reqs);
599 continue;
600 }
601 }
602
603 /* Don't leave irqs off while doing memory copies */
604 spin_unlock_irqrestore(&dev->lock, flags);
605
606 if (len > current_rx_bytes)
607 size = current_rx_bytes;
608 else
609 size = len;
610
611 size -= copy_to_user(buf, current_rx_buf, size);
612 bytes_copied += size;
613 len -= size;
614 buf += size;
615
616 spin_lock_irqsave(&dev->lock, flags);
617
618 /* We've disconnected or reset free the req and buffer */
619 if (dev->reset_printer) {
620 printer_req_free(dev->out_ep, current_rx_req);
621 spin_unlock_irqrestore(&dev->lock, flags);
622 spin_unlock(&dev->lock_printer_io);
623 return -EAGAIN;
624 }
625
626 /* If we not returning all the data left in this RX request
627 * buffer then adjust the amount of data left in the buffer.
628 * Othewise if we are done with this RX request buffer then
629 * requeue it to get any incoming data from the USB host.
630 */
631 if (size < current_rx_bytes) {
632 current_rx_bytes -= size;
633 current_rx_buf += size;
634 } else {
635 list_add(&current_rx_req->list, &dev->rx_reqs);
636 current_rx_bytes = 0;
637 current_rx_buf = NULL;
638 current_rx_req = NULL;
639 }
640 }
641
642 dev->current_rx_req = current_rx_req;
643 dev->current_rx_bytes = current_rx_bytes;
644 dev->current_rx_buf = current_rx_buf;
645
646 spin_unlock_irqrestore(&dev->lock, flags);
647 spin_unlock(&dev->lock_printer_io);
648
649 DBG(dev, "printer_read returned %d bytes\n", (int)bytes_copied);
650
651 if (bytes_copied)
652 return bytes_copied;
653 else
654 return -EAGAIN;
655 }
656
657 static ssize_t
658 printer_write(struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
659 {
660 struct printer_dev *dev = fd->private_data;
661 unsigned long flags;
662 size_t size; /* Amount of data in a TX request. */
663 size_t bytes_copied = 0;
664 struct usb_request *req;
665
666 DBG(dev, "printer_write trying to send %d bytes\n", (int)len);
667
668 if (len == 0)
669 return -EINVAL;
670
671 spin_lock(&dev->lock_printer_io);
672 spin_lock_irqsave(&dev->lock, flags);
673
674 /* Check if a printer reset happens while we have interrupts on */
675 dev->reset_printer = 0;
676
677 /* Check if there is any available write buffers */
678 if (likely(list_empty(&dev->tx_reqs))) {
679 /* Turn interrupts back on before sleeping. */
680 spin_unlock_irqrestore(&dev->lock, flags);
681
682 /*
683 * If write buffers are available check if this is
684 * a NON-Blocking call or not.
685 */
686 if (fd->f_flags & (O_NONBLOCK|O_NDELAY)) {
687 spin_unlock(&dev->lock_printer_io);
688 return -EAGAIN;
689 }
690
691 /* Sleep until a write buffer is available */
692 wait_event_interruptible(dev->tx_wait,
693 (likely(!list_empty(&dev->tx_reqs))));
694 spin_lock_irqsave(&dev->lock, flags);
695 }
696
697 while (likely(!list_empty(&dev->tx_reqs)) && len) {
698
699 if (len > USB_BUFSIZE)
700 size = USB_BUFSIZE;
701 else
702 size = len;
703
704 req = container_of(dev->tx_reqs.next, struct usb_request,
705 list);
706 list_del_init(&req->list);
707
708 req->complete = tx_complete;
709 req->length = size;
710
711 /* Check if we need to send a zero length packet. */
712 if (len > size)
713 /* They will be more TX requests so no yet. */
714 req->zero = 0;
715 else
716 /* If the data amount is not a multple of the
717 * maxpacket size then send a zero length packet.
718 */
719 req->zero = ((len % dev->in_ep->maxpacket) == 0);
720
721 /* Don't leave irqs off while doing memory copies */
722 spin_unlock_irqrestore(&dev->lock, flags);
723
724 if (copy_from_user(req->buf, buf, size)) {
725 list_add(&req->list, &dev->tx_reqs);
726 spin_unlock(&dev->lock_printer_io);
727 return bytes_copied;
728 }
729
730 bytes_copied += size;
731 len -= size;
732 buf += size;
733
734 spin_lock_irqsave(&dev->lock, flags);
735
736 /* We've disconnected or reset so free the req and buffer */
737 if (dev->reset_printer) {
738 printer_req_free(dev->in_ep, req);
739 spin_unlock_irqrestore(&dev->lock, flags);
740 spin_unlock(&dev->lock_printer_io);
741 return -EAGAIN;
742 }
743
744 if (usb_ep_queue(dev->in_ep, req, GFP_ATOMIC)) {
745 list_add(&req->list, &dev->tx_reqs);
746 spin_unlock_irqrestore(&dev->lock, flags);
747 spin_unlock(&dev->lock_printer_io);
748 return -EAGAIN;
749 }
750
751 list_add(&req->list, &dev->tx_reqs_active);
752
753 }
754
755 spin_unlock_irqrestore(&dev->lock, flags);
756 spin_unlock(&dev->lock_printer_io);
757
758 DBG(dev, "printer_write sent %d bytes\n", (int)bytes_copied);
759
760 if (bytes_copied) {
761 return bytes_copied;
762 } else {
763 return -EAGAIN;
764 }
765 }
766
767 static int
768 printer_fsync(struct file *fd, struct dentry *dentry, int datasync)
769 {
770 struct printer_dev *dev = fd->private_data;
771 unsigned long flags;
772 int tx_list_empty;
773
774 spin_lock_irqsave(&dev->lock, flags);
775 tx_list_empty = (likely(list_empty(&dev->tx_reqs)));
776 spin_unlock_irqrestore(&dev->lock, flags);
777
778 if (!tx_list_empty) {
779 /* Sleep until all data has been sent */
780 wait_event_interruptible(dev->tx_flush_wait,
781 (likely(list_empty(&dev->tx_reqs_active))));
782 }
783
784 return 0;
785 }
786
787 static unsigned int
788 printer_poll(struct file *fd, poll_table *wait)
789 {
790 struct printer_dev *dev = fd->private_data;
791 unsigned long flags;
792 int status = 0;
793
794 poll_wait(fd, &dev->rx_wait, wait);
795 poll_wait(fd, &dev->tx_wait, wait);
796
797 spin_lock_irqsave(&dev->lock, flags);
798 if (likely(!list_empty(&dev->tx_reqs)))
799 status |= POLLOUT | POLLWRNORM;
800
801 if (likely(!list_empty(&dev->rx_buffers)))
802 status |= POLLIN | POLLRDNORM;
803
804 spin_unlock_irqrestore(&dev->lock, flags);
805
806 return status;
807 }
808
809 static int
810 printer_ioctl(struct inode *inode, struct file *fd, unsigned int code,
811 unsigned long arg)
812 {
813 struct printer_dev *dev = fd->private_data;
814 unsigned long flags;
815 int status = 0;
816
817 DBG(dev, "printer_ioctl: cmd=0x%4.4x, arg=%lu\n", code, arg);
818
819 /* handle ioctls */
820
821 spin_lock_irqsave(&dev->lock, flags);
822
823 switch (code) {
824 case GADGET_GET_PRINTER_STATUS:
825 status = (int)dev->printer_status;
826 break;
827 case GADGET_SET_PRINTER_STATUS:
828 dev->printer_status = (u8)arg;
829 break;
830 default:
831 /* could not handle ioctl */
832 DBG(dev, "printer_ioctl: ERROR cmd=0x%4.4xis not supported\n",
833 code);
834 status = -ENOTTY;
835 }
836
837 spin_unlock_irqrestore(&dev->lock, flags);
838
839 return status;
840 }
841
842 /* used after endpoint configuration */
843 static struct file_operations printer_io_operations = {
844 .owner = THIS_MODULE,
845 .open = printer_open,
846 .read = printer_read,
847 .write = printer_write,
848 .fsync = printer_fsync,
849 .poll = printer_poll,
850 .ioctl = printer_ioctl,
851 .release = printer_close
852 };
853
854 /*-------------------------------------------------------------------------*/
855
856 static int
857 set_printer_interface(struct printer_dev *dev)
858 {
859 int result = 0;
860
861 dev->in = ep_desc(dev->gadget, &hs_ep_in_desc, &fs_ep_in_desc);
862 dev->in_ep->driver_data = dev;
863
864 dev->out = ep_desc(dev->gadget, &hs_ep_out_desc, &fs_ep_out_desc);
865 dev->out_ep->driver_data = dev;
866
867 result = usb_ep_enable(dev->in_ep, dev->in);
868 if (result != 0) {
869 DBG(dev, "enable %s --> %d\n", dev->in_ep->name, result);
870 goto done;
871 }
872
873 result = usb_ep_enable(dev->out_ep, dev->out);
874 if (result != 0) {
875 DBG(dev, "enable %s --> %d\n", dev->in_ep->name, result);
876 goto done;
877 }
878
879 done:
880 /* on error, disable any endpoints */
881 if (result != 0) {
882 (void) usb_ep_disable(dev->in_ep);
883 (void) usb_ep_disable(dev->out_ep);
884 dev->in = NULL;
885 dev->out = NULL;
886 }
887
888 /* caller is responsible for cleanup on error */
889 return result;
890 }
891
892 static void printer_reset_interface(struct printer_dev *dev)
893 {
894 if (dev->interface < 0)
895 return;
896
897 DBG(dev, "%s\n", __FUNCTION__);
898
899 if (dev->in)
900 usb_ep_disable(dev->in_ep);
901
902 if (dev->out)
903 usb_ep_disable(dev->out_ep);
904
905 dev->interface = -1;
906 }
907
908 /* change our operational config. must agree with the code
909 * that returns config descriptors, and altsetting code.
910 */
911 static int
912 printer_set_config(struct printer_dev *dev, unsigned number)
913 {
914 int result = 0;
915 struct usb_gadget *gadget = dev->gadget;
916
917 if (gadget_is_sa1100(gadget) && dev->config) {
918 /* tx fifo is full, but we can't clear it...*/
919 INFO(dev, "can't change configurations\n");
920 return -ESPIPE;
921 }
922
923 switch (number) {
924 case DEV_CONFIG_VALUE:
925 result = 0;
926 break;
927 default:
928 result = -EINVAL;
929 /* FALL THROUGH */
930 case 0:
931 break;
932 }
933
934 if (result) {
935 usb_gadget_vbus_draw(dev->gadget,
936 dev->gadget->is_otg ? 8 : 100);
937 } else {
938 char *speed;
939 unsigned power;
940
941 power = 2 * config_desc.bMaxPower;
942 usb_gadget_vbus_draw(dev->gadget, power);
943
944 switch (gadget->speed) {
945 case USB_SPEED_FULL: speed = "full"; break;
946 #ifdef CONFIG_USB_GADGET_DUALSPEED
947 case USB_SPEED_HIGH: speed = "high"; break;
948 #endif
949 default: speed = "?"; break;
950 }
951
952 dev->config = number;
953 INFO(dev, "%s speed config #%d: %d mA, %s\n",
954 speed, number, power, driver_desc);
955 }
956 return result;
957 }
958
959 static int
960 config_buf(enum usb_device_speed speed, u8 *buf, u8 type, unsigned index,
961 int is_otg)
962 {
963 int len;
964 const struct usb_descriptor_header **function;
965 #ifdef CONFIG_USB_GADGET_DUALSPEED
966 int hs = (speed == USB_SPEED_HIGH);
967
968 if (type == USB_DT_OTHER_SPEED_CONFIG)
969 hs = !hs;
970
971 if (hs) {
972 function = hs_printer_function;
973 } else {
974 function = fs_printer_function;
975 }
976 #else
977 function = fs_printer_function;
978 #endif
979
980 if (index >= device_desc.bNumConfigurations)
981 return -EINVAL;
982
983 /* for now, don't advertise srp-only devices */
984 if (!is_otg)
985 function++;
986
987 len = usb_gadget_config_buf(&config_desc, buf, USB_DESC_BUFSIZE,
988 function);
989 if (len < 0)
990 return len;
991 ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
992 return len;
993 }
994
995 /* Change our operational Interface. */
996 static int
997 set_interface(struct printer_dev *dev, unsigned number)
998 {
999 int result = 0;
1000
1001 if (gadget_is_sa1100(dev->gadget) && dev->interface < 0) {
1002 /* tx fifo is full, but we can't clear it...*/
1003 INFO(dev, "can't change interfaces\n");
1004 return -ESPIPE;
1005 }
1006
1007 /* Free the current interface */
1008 switch (dev->interface) {
1009 case PRINTER_INTERFACE:
1010 printer_reset_interface(dev);
1011 break;
1012 }
1013
1014 switch (number) {
1015 case PRINTER_INTERFACE:
1016 result = set_printer_interface(dev);
1017 if (result) {
1018 printer_reset_interface(dev);
1019 } else {
1020 dev->interface = PRINTER_INTERFACE;
1021 }
1022 break;
1023 default:
1024 result = -EINVAL;
1025 /* FALL THROUGH */
1026 }
1027
1028 if (!result)
1029 INFO(dev, "Using interface %x\n", number);
1030
1031 return result;
1032 }
1033
1034 static void printer_setup_complete(struct usb_ep *ep, struct usb_request *req)
1035 {
1036 if (req->status || req->actual != req->length)
1037 DBG((struct printer_dev *) ep->driver_data,
1038 "setup complete --> %d, %d/%d\n",
1039 req->status, req->actual, req->length);
1040 }
1041
1042 static void printer_soft_reset(struct printer_dev *dev)
1043 {
1044 struct usb_request *req;
1045
1046 INFO(dev, "Received Printer Reset Request\n");
1047
1048 if (usb_ep_disable(dev->in_ep))
1049 DBG(dev, "Failed to disable USB in_ep\n");
1050 if (usb_ep_disable(dev->out_ep))
1051 DBG(dev, "Failed to disable USB out_ep\n");
1052
1053 if (dev->current_rx_req != NULL) {
1054 list_add(&dev->current_rx_req->list, &dev->rx_reqs);
1055 dev->current_rx_req = NULL;
1056 }
1057 dev->current_rx_bytes = 0;
1058 dev->current_rx_buf = NULL;
1059 dev->reset_printer = 1;
1060
1061 while (likely(!(list_empty(&dev->rx_buffers)))) {
1062 req = container_of(dev->rx_buffers.next, struct usb_request,
1063 list);
1064 list_del_init(&req->list);
1065 list_add(&req->list, &dev->rx_reqs);
1066 }
1067
1068 while (likely(!(list_empty(&dev->rx_reqs_active)))) {
1069 req = container_of(dev->rx_buffers.next, struct usb_request,
1070 list);
1071 list_del_init(&req->list);
1072 list_add(&req->list, &dev->rx_reqs);
1073 }
1074
1075 while (likely(!(list_empty(&dev->tx_reqs_active)))) {
1076 req = container_of(dev->tx_reqs_active.next,
1077 struct usb_request, list);
1078 list_del_init(&req->list);
1079 list_add(&req->list, &dev->tx_reqs);
1080 }
1081
1082 if (usb_ep_enable(dev->in_ep, dev->in))
1083 DBG(dev, "Failed to enable USB in_ep\n");
1084 if (usb_ep_enable(dev->out_ep, dev->out))
1085 DBG(dev, "Failed to enable USB out_ep\n");
1086
1087 wake_up_interruptible(&dev->tx_wait);
1088 wake_up_interruptible(&dev->tx_flush_wait);
1089 }
1090
1091 /*-------------------------------------------------------------------------*/
1092
1093 /*
1094 * The setup() callback implements all the ep0 functionality that's not
1095 * handled lower down.
1096 */
1097 static int
1098 printer_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1099 {
1100 struct printer_dev *dev = get_gadget_data(gadget);
1101 struct usb_request *req = dev->req;
1102 int value = -EOPNOTSUPP;
1103 u16 wIndex = le16_to_cpu(ctrl->wIndex);
1104 u16 wValue = le16_to_cpu(ctrl->wValue);
1105 u16 wLength = le16_to_cpu(ctrl->wLength);
1106
1107 DBG(dev, "ctrl req%02x.%02x v%04x i%04x l%d\n",
1108 ctrl->bRequestType, ctrl->bRequest, wValue, wIndex, wLength);
1109
1110 req->complete = printer_setup_complete;
1111
1112 switch (ctrl->bRequestType&USB_TYPE_MASK) {
1113
1114 case USB_TYPE_STANDARD:
1115 switch (ctrl->bRequest) {
1116
1117 case USB_REQ_GET_DESCRIPTOR:
1118 if (ctrl->bRequestType != USB_DIR_IN)
1119 break;
1120 switch (wValue >> 8) {
1121
1122 case USB_DT_DEVICE:
1123 value = min(wLength, (u16) sizeof device_desc);
1124 memcpy(req->buf, &device_desc, value);
1125 break;
1126 #ifdef CONFIG_USB_GADGET_DUALSPEED
1127 case USB_DT_DEVICE_QUALIFIER:
1128 if (!gadget->is_dualspeed)
1129 break;
1130 value = min(wLength,
1131 (u16) sizeof dev_qualifier);
1132 memcpy(req->buf, &dev_qualifier, value);
1133 break;
1134
1135 case USB_DT_OTHER_SPEED_CONFIG:
1136 if (!gadget->is_dualspeed)
1137 break;
1138 /* FALLTHROUGH */
1139 #endif /* CONFIG_USB_GADGET_DUALSPEED */
1140 case USB_DT_CONFIG:
1141 value = config_buf(gadget->speed, req->buf,
1142 wValue >> 8,
1143 wValue & 0xff,
1144 gadget->is_otg);
1145 if (value >= 0)
1146 value = min(wLength, (u16) value);
1147 break;
1148
1149 case USB_DT_STRING:
1150 value = usb_gadget_get_string(&stringtab,
1151 wValue & 0xff, req->buf);
1152 if (value >= 0)
1153 value = min(wLength, (u16) value);
1154 break;
1155 }
1156 break;
1157
1158 case USB_REQ_SET_CONFIGURATION:
1159 if (ctrl->bRequestType != 0)
1160 break;
1161 if (gadget->a_hnp_support)
1162 DBG(dev, "HNP available\n");
1163 else if (gadget->a_alt_hnp_support)
1164 DBG(dev, "HNP needs a different root port\n");
1165 value = printer_set_config(dev, wValue);
1166 break;
1167 case USB_REQ_GET_CONFIGURATION:
1168 if (ctrl->bRequestType != USB_DIR_IN)
1169 break;
1170 *(u8 *)req->buf = dev->config;
1171 value = min(wLength, (u16) 1);
1172 break;
1173
1174 case USB_REQ_SET_INTERFACE:
1175 if (ctrl->bRequestType != USB_RECIP_INTERFACE ||
1176 !dev->config)
1177 break;
1178
1179 value = set_interface(dev, PRINTER_INTERFACE);
1180 break;
1181 case USB_REQ_GET_INTERFACE:
1182 if (ctrl->bRequestType !=
1183 (USB_DIR_IN|USB_RECIP_INTERFACE)
1184 || !dev->config)
1185 break;
1186
1187 *(u8 *)req->buf = dev->interface;
1188 value = min(wLength, (u16) 1);
1189 break;
1190
1191 default:
1192 goto unknown;
1193 }
1194 break;
1195
1196 case USB_TYPE_CLASS:
1197 switch (ctrl->bRequest) {
1198 case 0: /* Get the IEEE-1284 PNP String */
1199 /* Only one printer interface is supported. */
1200 if ((wIndex>>8) != PRINTER_INTERFACE)
1201 break;
1202
1203 value = (pnp_string[0]<<8)|pnp_string[1];
1204 memcpy(req->buf, pnp_string, value);
1205 DBG(dev, "1284 PNP String: %x %s\n", value,
1206 &pnp_string[2]);
1207 break;
1208
1209 case 1: /* Get Port Status */
1210 /* Only one printer interface is supported. */
1211 if (wIndex != PRINTER_INTERFACE)
1212 break;
1213
1214 *(u8 *)req->buf = dev->printer_status;
1215 value = min(wLength, (u16) 1);
1216 break;
1217
1218 case 2: /* Soft Reset */
1219 /* Only one printer interface is supported. */
1220 if (wIndex != PRINTER_INTERFACE)
1221 break;
1222
1223 printer_soft_reset(dev);
1224
1225 value = 0;
1226 break;
1227
1228 default:
1229 goto unknown;
1230 }
1231 break;
1232
1233 default:
1234 unknown:
1235 VDBG(dev,
1236 "unknown ctrl req%02x.%02x v%04x i%04x l%d\n",
1237 ctrl->bRequestType, ctrl->bRequest,
1238 wValue, wIndex, wLength);
1239 break;
1240 }
1241
1242 /* respond with data transfer before status phase? */
1243 if (value >= 0) {
1244 req->length = value;
1245 req->zero = value < wLength
1246 && (value % gadget->ep0->maxpacket) == 0;
1247 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
1248 if (value < 0) {
1249 DBG(dev, "ep_queue --> %d\n", value);
1250 req->status = 0;
1251 printer_setup_complete(gadget->ep0, req);
1252 }
1253 }
1254
1255 /* host either stalls (value < 0) or reports success */
1256 return value;
1257 }
1258
1259 static void
1260 printer_disconnect(struct usb_gadget *gadget)
1261 {
1262 struct printer_dev *dev = get_gadget_data(gadget);
1263 unsigned long flags;
1264
1265 DBG(dev, "%s\n", __FUNCTION__);
1266
1267 spin_lock_irqsave(&dev->lock, flags);
1268
1269 printer_reset_interface(dev);
1270
1271 spin_unlock_irqrestore(&dev->lock, flags);
1272 }
1273
1274 static void
1275 printer_unbind(struct usb_gadget *gadget)
1276 {
1277 struct printer_dev *dev = get_gadget_data(gadget);
1278 struct usb_request *req;
1279
1280
1281 DBG(dev, "%s\n", __FUNCTION__);
1282
1283 /* Remove sysfs files */
1284 device_destroy(usb_gadget_class, g_printer_devno);
1285
1286 /* Remove Character Device */
1287 cdev_del(&dev->printer_cdev);
1288
1289 /* we must already have been disconnected ... no i/o may be active */
1290 WARN_ON(!list_empty(&dev->tx_reqs_active));
1291 WARN_ON(!list_empty(&dev->rx_reqs_active));
1292
1293 /* Free all memory for this driver. */
1294 while (!list_empty(&dev->tx_reqs)) {
1295 req = container_of(dev->tx_reqs.next, struct usb_request,
1296 list);
1297 list_del(&req->list);
1298 printer_req_free(dev->in_ep, req);
1299 }
1300
1301 if (dev->current_rx_req != NULL)
1302 printer_req_free(dev->out_ep, dev->current_rx_req);
1303
1304 while (!list_empty(&dev->rx_reqs)) {
1305 req = container_of(dev->rx_reqs.next,
1306 struct usb_request, list);
1307 list_del(&req->list);
1308 printer_req_free(dev->out_ep, req);
1309 }
1310
1311 while (!list_empty(&dev->rx_buffers)) {
1312 req = container_of(dev->rx_buffers.next,
1313 struct usb_request, list);
1314 list_del(&req->list);
1315 printer_req_free(dev->out_ep, req);
1316 }
1317
1318 if (dev->req) {
1319 printer_req_free(gadget->ep0, dev->req);
1320 dev->req = NULL;
1321 }
1322
1323 set_gadget_data(gadget, NULL);
1324 }
1325
1326 static int __init
1327 printer_bind(struct usb_gadget *gadget)
1328 {
1329 struct printer_dev *dev;
1330 struct usb_ep *in_ep, *out_ep;
1331 int status = -ENOMEM;
1332 int gcnum;
1333 size_t len;
1334 u32 i;
1335 struct usb_request *req;
1336
1337 dev = &usb_printer_gadget;
1338
1339
1340 /* Setup the sysfs files for the printer gadget. */
1341 dev->pdev = device_create(usb_gadget_class, NULL, g_printer_devno,
1342 "g_printer");
1343 if (IS_ERR(dev->pdev)) {
1344 ERROR(dev, "Failed to create device: g_printer\n");
1345 goto fail;
1346 }
1347
1348 /*
1349 * Register a character device as an interface to a user mode
1350 * program that handles the printer specific functionality.
1351 */
1352 cdev_init(&dev->printer_cdev, &printer_io_operations);
1353 dev->printer_cdev.owner = THIS_MODULE;
1354 status = cdev_add(&dev->printer_cdev, g_printer_devno, 1);
1355 if (status) {
1356 ERROR(dev, "Failed to open char device\n");
1357 goto fail;
1358 }
1359
1360 if (gadget_is_sa1100(gadget)) {
1361 /* hardware can't write zero length packets. */
1362 ERROR(dev, "SA1100 controller is unsupport by this driver\n");
1363 goto fail;
1364 }
1365
1366 gcnum = usb_gadget_controller_number(gadget);
1367 if (gcnum >= 0) {
1368 device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
1369 } else {
1370 dev_warn(&gadget->dev, "controller '%s' not recognized\n",
1371 gadget->name);
1372 /* unrecognized, but safe unless bulk is REALLY quirky */
1373 device_desc.bcdDevice =
1374 __constant_cpu_to_le16(0xFFFF);
1375 }
1376 snprintf(manufacturer, sizeof(manufacturer), "%s %s with %s",
1377 init_utsname()->sysname, init_utsname()->release,
1378 gadget->name);
1379
1380 device_desc.idVendor =
1381 __constant_cpu_to_le16(PRINTER_VENDOR_NUM);
1382 device_desc.idProduct =
1383 __constant_cpu_to_le16(PRINTER_PRODUCT_NUM);
1384
1385 /* support optional vendor/distro customization */
1386 if (idVendor) {
1387 if (!idProduct) {
1388 dev_err(&gadget->dev, "idVendor needs idProduct!\n");
1389 return -ENODEV;
1390 }
1391 device_desc.idVendor = cpu_to_le16(idVendor);
1392 device_desc.idProduct = cpu_to_le16(idProduct);
1393 if (bcdDevice)
1394 device_desc.bcdDevice = cpu_to_le16(bcdDevice);
1395 }
1396
1397 if (iManufacturer)
1398 strlcpy(manufacturer, iManufacturer, sizeof manufacturer);
1399
1400 if (iProduct)
1401 strlcpy(product_desc, iProduct, sizeof product_desc);
1402
1403 if (iSerialNum)
1404 strlcpy(serial_num, iSerialNum, sizeof serial_num);
1405
1406 if (iPNPstring)
1407 strlcpy(&pnp_string[2], iPNPstring, (sizeof pnp_string)-2);
1408
1409 len = strlen(pnp_string);
1410 pnp_string[0] = (len >> 8) & 0xFF;
1411 pnp_string[1] = len & 0xFF;
1412
1413 /* all we really need is bulk IN/OUT */
1414 usb_ep_autoconfig_reset(gadget);
1415 in_ep = usb_ep_autoconfig(gadget, &fs_ep_in_desc);
1416 if (!in_ep) {
1417 autoconf_fail:
1418 dev_err(&gadget->dev, "can't autoconfigure on %s\n",
1419 gadget->name);
1420 return -ENODEV;
1421 }
1422 in_ep->driver_data = in_ep; /* claim */
1423
1424 out_ep = usb_ep_autoconfig(gadget, &fs_ep_out_desc);
1425 if (!out_ep)
1426 goto autoconf_fail;
1427 out_ep->driver_data = out_ep; /* claim */
1428
1429 #ifdef CONFIG_USB_GADGET_DUALSPEED
1430 /* assumes ep0 uses the same value for both speeds ... */
1431 dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
1432
1433 /* and that all endpoints are dual-speed */
1434 hs_ep_in_desc.bEndpointAddress = fs_ep_in_desc.bEndpointAddress;
1435 hs_ep_out_desc.bEndpointAddress = fs_ep_out_desc.bEndpointAddress;
1436 #endif /* DUALSPEED */
1437
1438 device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
1439 usb_gadget_set_selfpowered(gadget);
1440
1441 if (gadget->is_otg) {
1442 otg_desc.bmAttributes |= USB_OTG_HNP,
1443 config_desc.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1444 config_desc.bMaxPower = 4;
1445 }
1446
1447 spin_lock_init(&dev->lock);
1448 spin_lock_init(&dev->lock_printer_io);
1449 INIT_LIST_HEAD(&dev->tx_reqs);
1450 INIT_LIST_HEAD(&dev->tx_reqs_active);
1451 INIT_LIST_HEAD(&dev->rx_reqs);
1452 INIT_LIST_HEAD(&dev->rx_reqs_active);
1453 INIT_LIST_HEAD(&dev->rx_buffers);
1454 init_waitqueue_head(&dev->rx_wait);
1455 init_waitqueue_head(&dev->tx_wait);
1456 init_waitqueue_head(&dev->tx_flush_wait);
1457
1458 dev->config = 0;
1459 dev->interface = -1;
1460 dev->printer_cdev_open = 0;
1461 dev->printer_status = PRINTER_NOT_ERROR;
1462 dev->current_rx_req = NULL;
1463 dev->current_rx_bytes = 0;
1464 dev->current_rx_buf = NULL;
1465
1466 dev->in_ep = in_ep;
1467 dev->out_ep = out_ep;
1468
1469 /* preallocate control message data and buffer */
1470 dev->req = printer_req_alloc(gadget->ep0, USB_DESC_BUFSIZE,
1471 GFP_KERNEL);
1472 if (!dev->req) {
1473 status = -ENOMEM;
1474 goto fail;
1475 }
1476
1477 for (i = 0; i < QLEN; i++) {
1478 req = printer_req_alloc(dev->in_ep, USB_BUFSIZE, GFP_KERNEL);
1479 if (!req) {
1480 while (!list_empty(&dev->tx_reqs)) {
1481 req = container_of(dev->tx_reqs.next,
1482 struct usb_request, list);
1483 list_del(&req->list);
1484 printer_req_free(dev->in_ep, req);
1485 }
1486 return -ENOMEM;
1487 }
1488 list_add(&req->list, &dev->tx_reqs);
1489 }
1490
1491 for (i = 0; i < QLEN; i++) {
1492 req = printer_req_alloc(dev->out_ep, USB_BUFSIZE, GFP_KERNEL);
1493 if (!req) {
1494 while (!list_empty(&dev->rx_reqs)) {
1495 req = container_of(dev->rx_reqs.next,
1496 struct usb_request, list);
1497 list_del(&req->list);
1498 printer_req_free(dev->out_ep, req);
1499 }
1500 return -ENOMEM;
1501 }
1502 list_add(&req->list, &dev->rx_reqs);
1503 }
1504
1505 dev->req->complete = printer_setup_complete;
1506
1507 /* finish hookup to lower layer ... */
1508 dev->gadget = gadget;
1509 set_gadget_data(gadget, dev);
1510 gadget->ep0->driver_data = dev;
1511
1512 INFO(dev, "%s, version: " DRIVER_VERSION "\n", driver_desc);
1513 INFO(dev, "using %s, OUT %s IN %s\n", gadget->name, out_ep->name,
1514 in_ep->name);
1515
1516 return 0;
1517
1518 fail:
1519 printer_unbind(gadget);
1520 return status;
1521 }
1522
1523 /*-------------------------------------------------------------------------*/
1524
1525 static struct usb_gadget_driver printer_driver = {
1526 .speed = DEVSPEED,
1527
1528 .function = (char *) driver_desc,
1529 .bind = printer_bind,
1530 .unbind = printer_unbind,
1531
1532 .setup = printer_setup,
1533 .disconnect = printer_disconnect,
1534
1535 .driver = {
1536 .name = (char *) shortname,
1537 .owner = THIS_MODULE,
1538 },
1539 };
1540
1541 MODULE_DESCRIPTION(DRIVER_DESC);
1542 MODULE_AUTHOR("Craig Nadler");
1543 MODULE_LICENSE("GPL");
1544
1545 static int __init
1546 init(void)
1547 {
1548 int status;
1549
1550 usb_gadget_class = class_create(THIS_MODULE, "usb_printer_gadget");
1551 if (IS_ERR(usb_gadget_class)) {
1552 status = PTR_ERR(usb_gadget_class);
1553 ERROR(dev, "unable to create usb_gadget class %d\n", status);
1554 return status;
1555 }
1556
1557 status = alloc_chrdev_region(&g_printer_devno, 0, 1,
1558 "USB printer gadget");
1559 if (status) {
1560 ERROR(dev, "alloc_chrdev_region %d\n", status);
1561 class_destroy(usb_gadget_class);
1562 return status;
1563 }
1564
1565 status = usb_gadget_register_driver(&printer_driver);
1566 if (status) {
1567 class_destroy(usb_gadget_class);
1568 unregister_chrdev_region(g_printer_devno, 1);
1569 DBG(dev, "usb_gadget_register_driver %x\n", status);
1570 }
1571
1572 return status;
1573 }
1574 module_init(init);
1575
1576 static void __exit
1577 cleanup(void)
1578 {
1579 int status;
1580
1581 spin_lock(&usb_printer_gadget.lock_printer_io);
1582 class_destroy(usb_gadget_class);
1583 unregister_chrdev_region(g_printer_devno, 2);
1584
1585 status = usb_gadget_unregister_driver(&printer_driver);
1586 if (status)
1587 ERROR(dev, "usb_gadget_unregister_driver %x\n", status);
1588
1589 spin_unlock(&usb_printer_gadget.lock_printer_io);
1590 }
1591 module_exit(cleanup);