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