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