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