blob: bf7a56b6d48ac2c3ebb3acf8920c70d79900d307 [file] [log] [blame]
Craig W. Nadler25a010c2007-11-11 15:00:15 -08001/*
2 * printer.c -- Printer gadget driver
3 *
4 * Copyright (C) 2003-2005 David Brownell
5 * Copyright (C) 2006 Craig W. Nadler
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
Craig W. Nadler25a010c2007-11-11 15:00:15 -080011 */
12
13#include <linux/module.h>
14#include <linux/kernel.h>
15#include <linux/delay.h>
16#include <linux/ioport.h>
17#include <linux/sched.h>
18#include <linux/slab.h>
Arnd Bergmann925ce682010-07-11 23:18:56 +020019#include <linux/mutex.h>
Craig W. Nadler25a010c2007-11-11 15:00:15 -080020#include <linux/errno.h>
21#include <linux/init.h>
22#include <linux/timer.h>
23#include <linux/list.h>
24#include <linux/interrupt.h>
Craig W. Nadler25a010c2007-11-11 15:00:15 -080025#include <linux/device.h>
26#include <linux/moduleparam.h>
27#include <linux/fs.h>
28#include <linux/poll.h>
29#include <linux/types.h>
30#include <linux/ctype.h>
31#include <linux/cdev.h>
32
33#include <asm/byteorder.h>
34#include <linux/io.h>
35#include <linux/irq.h>
Craig W. Nadler25a010c2007-11-11 15:00:15 -080036#include <linux/uaccess.h>
37#include <asm/unaligned.h>
38
39#include <linux/usb/ch9.h>
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +020040#include <linux/usb/composite.h>
Craig W. Nadler25a010c2007-11-11 15:00:15 -080041#include <linux/usb/gadget.h>
42#include <linux/usb/g_printer.h>
43
44#include "gadget_chips.h"
45
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +020046USB_GADGET_COMPOSITE_OPTIONS();
David Brownell0a56b032008-08-18 17:42:49 -070047
Craig W. Nadler25a010c2007-11-11 15:00:15 -080048#define DRIVER_DESC "Printer Gadget"
49#define DRIVER_VERSION "2007 OCT 06"
50
Arnd Bergmann925ce682010-07-11 23:18:56 +020051static DEFINE_MUTEX(printer_mutex);
Craig W. Nadler25a010c2007-11-11 15:00:15 -080052static const char shortname [] = "printer";
53static const char driver_desc [] = DRIVER_DESC;
54
55static dev_t g_printer_devno;
56
57static struct class *usb_gadget_class;
58
59/*-------------------------------------------------------------------------*/
60
61struct printer_dev {
62 spinlock_t lock; /* lock this structure */
63 /* lock buffer lists during read/write calls */
Jiri Slaby10ca4422010-06-21 17:02:40 +020064 struct mutex lock_printer_io;
Craig W. Nadler25a010c2007-11-11 15:00:15 -080065 struct usb_gadget *gadget;
Craig W. Nadler25a010c2007-11-11 15:00:15 -080066 s8 interface;
67 struct usb_ep *in_ep, *out_ep;
Tatyana Brokhman72c973d2011-06-28 16:33:48 +030068
Craig W. Nadler25a010c2007-11-11 15:00:15 -080069 struct list_head rx_reqs; /* List of free RX structs */
70 struct list_head rx_reqs_active; /* List of Active RX xfers */
71 struct list_head rx_buffers; /* List of completed xfers */
72 /* wait until there is data to be read. */
73 wait_queue_head_t rx_wait;
74 struct list_head tx_reqs; /* List of free TX structs */
75 struct list_head tx_reqs_active; /* List of Active TX xfers */
76 /* Wait until there are write buffers available to use. */
77 wait_queue_head_t tx_wait;
78 /* Wait until all write buffers have been sent. */
79 wait_queue_head_t tx_flush_wait;
80 struct usb_request *current_rx_req;
81 size_t current_rx_bytes;
82 u8 *current_rx_buf;
83 u8 printer_status;
84 u8 reset_printer;
Craig W. Nadler25a010c2007-11-11 15:00:15 -080085 struct cdev printer_cdev;
86 struct device *pdev;
87 u8 printer_cdev_open;
88 wait_queue_head_t wait;
Sebastian Andrzej Siewior2e87edf2012-02-04 18:55:20 +010089 struct usb_function function;
Craig W. Nadler25a010c2007-11-11 15:00:15 -080090};
91
92static struct printer_dev usb_printer_gadget;
93
94/*-------------------------------------------------------------------------*/
95
96/* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
97 * Instead: allocate your own, using normal USB-IF procedures.
98 */
99
100/* Thanks to NetChip Technologies for donating this product ID.
101 */
102#define PRINTER_VENDOR_NUM 0x0525 /* NetChip */
103#define PRINTER_PRODUCT_NUM 0xa4a8 /* Linux-USB Printer Gadget */
104
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300105/* Some systems will want different product identifiers published in the
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800106 * device descriptor, either numbers or strings or both. These string
107 * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
108 */
109
Sebastian Andrzej Siewior1cf0d262012-09-10 15:01:54 +0200110module_param_named(iSerialNum, coverwrite.serial_number, charp, S_IRUGO);
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800111MODULE_PARM_DESC(iSerialNum, "1");
112
Alan Sternad84e4a2011-01-10 11:24:14 -0500113static char *iPNPstring;
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800114module_param(iPNPstring, charp, S_IRUGO);
115MODULE_PARM_DESC(iPNPstring, "MFG:linux;MDL:g_printer;CLS:PRINTER;SN:1;");
116
117/* Number of requests to allocate per endpoint, not used for ep0. */
118static unsigned qlen = 10;
119module_param(qlen, uint, S_IRUGO|S_IWUSR);
120
121#define QLEN qlen
122
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800123/*-------------------------------------------------------------------------*/
124
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800125/*
126 * DESCRIPTORS ... most are static, but strings and (full) configuration
127 * descriptors are built on demand.
128 */
129
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800130/* holds our biggest descriptor */
131#define USB_DESC_BUFSIZE 256
132#define USB_BUFSIZE 8192
133
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800134static struct usb_device_descriptor device_desc = {
135 .bLength = sizeof device_desc,
136 .bDescriptorType = USB_DT_DEVICE,
Harvey Harrison551509d2009-02-11 14:11:36 -0800137 .bcdUSB = cpu_to_le16(0x0200),
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800138 .bDeviceClass = USB_CLASS_PER_INTERFACE,
139 .bDeviceSubClass = 0,
140 .bDeviceProtocol = 0,
Harvey Harrison551509d2009-02-11 14:11:36 -0800141 .idVendor = cpu_to_le16(PRINTER_VENDOR_NUM),
142 .idProduct = cpu_to_le16(PRINTER_PRODUCT_NUM),
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800143 .bNumConfigurations = 1
144};
145
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800146static struct usb_interface_descriptor intf_desc = {
147 .bLength = sizeof intf_desc,
148 .bDescriptorType = USB_DT_INTERFACE,
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800149 .bNumEndpoints = 2,
150 .bInterfaceClass = USB_CLASS_PRINTER,
151 .bInterfaceSubClass = 1, /* Printer Sub-Class */
152 .bInterfaceProtocol = 2, /* Bi-Directional */
153 .iInterface = 0
154};
155
156static struct usb_endpoint_descriptor fs_ep_in_desc = {
157 .bLength = USB_DT_ENDPOINT_SIZE,
158 .bDescriptorType = USB_DT_ENDPOINT,
159 .bEndpointAddress = USB_DIR_IN,
160 .bmAttributes = USB_ENDPOINT_XFER_BULK
161};
162
163static struct usb_endpoint_descriptor fs_ep_out_desc = {
164 .bLength = USB_DT_ENDPOINT_SIZE,
165 .bDescriptorType = USB_DT_ENDPOINT,
166 .bEndpointAddress = USB_DIR_OUT,
167 .bmAttributes = USB_ENDPOINT_XFER_BULK
168};
169
Sebastian Andrzej Siewior2e87edf2012-02-04 18:55:20 +0100170static struct usb_descriptor_header *fs_printer_function[] = {
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800171 (struct usb_descriptor_header *) &intf_desc,
172 (struct usb_descriptor_header *) &fs_ep_in_desc,
173 (struct usb_descriptor_header *) &fs_ep_out_desc,
174 NULL
175};
176
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800177/*
178 * usb 2.0 devices need to expose both high speed and full speed
179 * descriptors, unless they only run at full speed.
180 */
181
182static struct usb_endpoint_descriptor hs_ep_in_desc = {
183 .bLength = USB_DT_ENDPOINT_SIZE,
184 .bDescriptorType = USB_DT_ENDPOINT,
185 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Harvey Harrison551509d2009-02-11 14:11:36 -0800186 .wMaxPacketSize = cpu_to_le16(512)
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800187};
188
189static struct usb_endpoint_descriptor hs_ep_out_desc = {
190 .bLength = USB_DT_ENDPOINT_SIZE,
191 .bDescriptorType = USB_DT_ENDPOINT,
192 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Harvey Harrison551509d2009-02-11 14:11:36 -0800193 .wMaxPacketSize = cpu_to_le16(512)
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800194};
195
196static struct usb_qualifier_descriptor dev_qualifier = {
197 .bLength = sizeof dev_qualifier,
198 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
Harvey Harrison551509d2009-02-11 14:11:36 -0800199 .bcdUSB = cpu_to_le16(0x0200),
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800200 .bDeviceClass = USB_CLASS_PRINTER,
201 .bNumConfigurations = 1
202};
203
Sebastian Andrzej Siewior2e87edf2012-02-04 18:55:20 +0100204static struct usb_descriptor_header *hs_printer_function[] = {
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800205 (struct usb_descriptor_header *) &intf_desc,
206 (struct usb_descriptor_header *) &hs_ep_in_desc,
207 (struct usb_descriptor_header *) &hs_ep_out_desc,
208 NULL
209};
210
Sebastian Andrzej Siewior2e87edf2012-02-04 18:55:20 +0100211static struct usb_otg_descriptor otg_descriptor = {
212 .bLength = sizeof otg_descriptor,
213 .bDescriptorType = USB_DT_OTG,
214 .bmAttributes = USB_OTG_SRP,
215};
216
217static const struct usb_descriptor_header *otg_desc[] = {
218 (struct usb_descriptor_header *) &otg_descriptor,
219 NULL,
220};
221
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800222/* maxpacket and other transfer characteristics vary by speed. */
223#define ep_desc(g, hs, fs) (((g)->speed == USB_SPEED_HIGH)?(hs):(fs))
224
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800225/*-------------------------------------------------------------------------*/
226
227/* descriptors that are built on-demand */
228
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800229static char product_desc [40] = DRIVER_DESC;
230static char serial_num [40] = "1";
231static char pnp_string [1024] =
232 "XXMFG:linux;MDL:g_printer;CLS:PRINTER;SN:1;";
233
234/* static strings, in UTF-8 */
235static struct usb_string strings [] = {
Sebastian Andrzej Siewiorcc2683c2012-09-10 15:01:58 +0200236 [USB_GADGET_MANUFACTURER_IDX].s = "",
Sebastian Andrzej Siewior276e2e42012-09-06 20:11:21 +0200237 [USB_GADGET_PRODUCT_IDX].s = product_desc,
238 [USB_GADGET_SERIAL_IDX].s = serial_num,
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800239 { } /* end of list */
240};
241
Sebastian Andrzej Siewior2e87edf2012-02-04 18:55:20 +0100242static struct usb_gadget_strings stringtab_dev = {
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800243 .language = 0x0409, /* en-us */
244 .strings = strings,
245};
246
Sebastian Andrzej Siewior2e87edf2012-02-04 18:55:20 +0100247static struct usb_gadget_strings *dev_strings[] = {
248 &stringtab_dev,
249 NULL,
250};
251
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800252/*-------------------------------------------------------------------------*/
253
254static struct usb_request *
255printer_req_alloc(struct usb_ep *ep, unsigned len, gfp_t gfp_flags)
256{
257 struct usb_request *req;
258
259 req = usb_ep_alloc_request(ep, gfp_flags);
260
261 if (req != NULL) {
262 req->length = len;
263 req->buf = kmalloc(len, gfp_flags);
264 if (req->buf == NULL) {
265 usb_ep_free_request(ep, req);
266 return NULL;
267 }
268 }
269
270 return req;
271}
272
273static void
274printer_req_free(struct usb_ep *ep, struct usb_request *req)
275{
276 if (ep != NULL && req != NULL) {
277 kfree(req->buf);
278 usb_ep_free_request(ep, req);
279 }
280}
281
282/*-------------------------------------------------------------------------*/
283
284static void rx_complete(struct usb_ep *ep, struct usb_request *req)
285{
286 struct printer_dev *dev = ep->driver_data;
287 int status = req->status;
288 unsigned long flags;
289
290 spin_lock_irqsave(&dev->lock, flags);
291
292 list_del_init(&req->list); /* Remode from Active List */
293
294 switch (status) {
295
296 /* normal completion */
297 case 0:
Craig W. Nadlercc901bb2008-03-20 14:46:26 -0700298 if (req->actual > 0) {
299 list_add_tail(&req->list, &dev->rx_buffers);
300 DBG(dev, "G_Printer : rx length %d\n", req->actual);
301 } else {
302 list_add(&req->list, &dev->rx_reqs);
303 }
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800304 break;
305
306 /* software-driven interface shutdown */
307 case -ECONNRESET: /* unlink */
308 case -ESHUTDOWN: /* disconnect etc */
309 VDBG(dev, "rx shutdown, code %d\n", status);
310 list_add(&req->list, &dev->rx_reqs);
311 break;
312
313 /* for hardware automagic (such as pxa) */
314 case -ECONNABORTED: /* endpoint reset */
315 DBG(dev, "rx %s reset\n", ep->name);
316 list_add(&req->list, &dev->rx_reqs);
317 break;
318
319 /* data overrun */
320 case -EOVERFLOW:
321 /* FALLTHROUGH */
322
323 default:
324 DBG(dev, "rx status %d\n", status);
325 list_add(&req->list, &dev->rx_reqs);
326 break;
327 }
Craig W. Nadlercc901bb2008-03-20 14:46:26 -0700328
329 wake_up_interruptible(&dev->rx_wait);
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800330 spin_unlock_irqrestore(&dev->lock, flags);
331}
332
333static void tx_complete(struct usb_ep *ep, struct usb_request *req)
334{
335 struct printer_dev *dev = ep->driver_data;
336
337 switch (req->status) {
338 default:
339 VDBG(dev, "tx err %d\n", req->status);
340 /* FALLTHROUGH */
341 case -ECONNRESET: /* unlink */
342 case -ESHUTDOWN: /* disconnect etc */
343 break;
344 case 0:
345 break;
346 }
347
348 spin_lock(&dev->lock);
349 /* Take the request struct off the active list and put it on the
350 * free list.
351 */
352 list_del_init(&req->list);
353 list_add(&req->list, &dev->tx_reqs);
354 wake_up_interruptible(&dev->tx_wait);
355 if (likely(list_empty(&dev->tx_reqs_active)))
356 wake_up_interruptible(&dev->tx_flush_wait);
357
358 spin_unlock(&dev->lock);
359}
360
361/*-------------------------------------------------------------------------*/
362
363static int
364printer_open(struct inode *inode, struct file *fd)
365{
366 struct printer_dev *dev;
367 unsigned long flags;
368 int ret = -EBUSY;
369
Arnd Bergmann925ce682010-07-11 23:18:56 +0200370 mutex_lock(&printer_mutex);
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800371 dev = container_of(inode->i_cdev, struct printer_dev, printer_cdev);
372
373 spin_lock_irqsave(&dev->lock, flags);
374
375 if (!dev->printer_cdev_open) {
376 dev->printer_cdev_open = 1;
377 fd->private_data = dev;
378 ret = 0;
379 /* Change the printer status to show that it's on-line. */
380 dev->printer_status |= PRINTER_SELECTED;
381 }
382
383 spin_unlock_irqrestore(&dev->lock, flags);
384
385 DBG(dev, "printer_open returned %x\n", ret);
Arnd Bergmann925ce682010-07-11 23:18:56 +0200386 mutex_unlock(&printer_mutex);
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800387 return ret;
388}
389
390static int
391printer_close(struct inode *inode, struct file *fd)
392{
393 struct printer_dev *dev = fd->private_data;
394 unsigned long flags;
395
396 spin_lock_irqsave(&dev->lock, flags);
397 dev->printer_cdev_open = 0;
398 fd->private_data = NULL;
399 /* Change printer status to show that the printer is off-line. */
400 dev->printer_status &= ~PRINTER_SELECTED;
401 spin_unlock_irqrestore(&dev->lock, flags);
402
403 DBG(dev, "printer_close\n");
404
405 return 0;
406}
407
Craig W. Nadlercc901bb2008-03-20 14:46:26 -0700408/* This function must be called with interrupts turned off. */
409static void
410setup_rx_reqs(struct printer_dev *dev)
411{
412 struct usb_request *req;
413
414 while (likely(!list_empty(&dev->rx_reqs))) {
415 int error;
416
417 req = container_of(dev->rx_reqs.next,
418 struct usb_request, list);
419 list_del_init(&req->list);
420
421 /* The USB Host sends us whatever amount of data it wants to
422 * so we always set the length field to the full USB_BUFSIZE.
423 * If the amount of data is more than the read() caller asked
424 * for it will be stored in the request buffer until it is
425 * asked for by read().
426 */
427 req->length = USB_BUFSIZE;
428 req->complete = rx_complete;
429
430 error = usb_ep_queue(dev->out_ep, req, GFP_ATOMIC);
431 if (error) {
432 DBG(dev, "rx submit --> %d\n", error);
433 list_add(&req->list, &dev->rx_reqs);
434 break;
435 } else {
436 list_add(&req->list, &dev->rx_reqs_active);
437 }
438 }
439}
440
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800441static ssize_t
442printer_read(struct file *fd, char __user *buf, size_t len, loff_t *ptr)
443{
444 struct printer_dev *dev = fd->private_data;
445 unsigned long flags;
446 size_t size;
447 size_t bytes_copied;
448 struct usb_request *req;
449 /* This is a pointer to the current USB rx request. */
450 struct usb_request *current_rx_req;
451 /* This is the number of bytes in the current rx buffer. */
452 size_t current_rx_bytes;
453 /* This is a pointer to the current rx buffer. */
454 u8 *current_rx_buf;
455
456 if (len == 0)
457 return -EINVAL;
458
459 DBG(dev, "printer_read trying to read %d bytes\n", (int)len);
460
Jiri Slaby10ca4422010-06-21 17:02:40 +0200461 mutex_lock(&dev->lock_printer_io);
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800462 spin_lock_irqsave(&dev->lock, flags);
463
464 /* We will use this flag later to check if a printer reset happened
465 * after we turn interrupts back on.
466 */
467 dev->reset_printer = 0;
468
Craig W. Nadlercc901bb2008-03-20 14:46:26 -0700469 setup_rx_reqs(dev);
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800470
471 bytes_copied = 0;
472 current_rx_req = dev->current_rx_req;
473 current_rx_bytes = dev->current_rx_bytes;
474 current_rx_buf = dev->current_rx_buf;
475 dev->current_rx_req = NULL;
476 dev->current_rx_bytes = 0;
477 dev->current_rx_buf = NULL;
478
479 /* Check if there is any data in the read buffers. Please note that
480 * current_rx_bytes is the number of bytes in the current rx buffer.
481 * If it is zero then check if there are any other rx_buffers that
482 * are on the completed list. We are only out of data if all rx
483 * buffers are empty.
484 */
485 if ((current_rx_bytes == 0) &&
486 (likely(list_empty(&dev->rx_buffers)))) {
487 /* Turn interrupts back on before sleeping. */
488 spin_unlock_irqrestore(&dev->lock, flags);
489
490 /*
491 * If no data is available check if this is a NON-Blocking
492 * call or not.
493 */
494 if (fd->f_flags & (O_NONBLOCK|O_NDELAY)) {
Jiri Slaby10ca4422010-06-21 17:02:40 +0200495 mutex_unlock(&dev->lock_printer_io);
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800496 return -EAGAIN;
497 }
498
499 /* Sleep until data is available */
500 wait_event_interruptible(dev->rx_wait,
501 (likely(!list_empty(&dev->rx_buffers))));
502 spin_lock_irqsave(&dev->lock, flags);
503 }
504
505 /* We have data to return then copy it to the caller's buffer.*/
506 while ((current_rx_bytes || likely(!list_empty(&dev->rx_buffers)))
507 && len) {
508 if (current_rx_bytes == 0) {
509 req = container_of(dev->rx_buffers.next,
510 struct usb_request, list);
511 list_del_init(&req->list);
512
513 if (req->actual && req->buf) {
514 current_rx_req = req;
515 current_rx_bytes = req->actual;
516 current_rx_buf = req->buf;
517 } else {
518 list_add(&req->list, &dev->rx_reqs);
519 continue;
520 }
521 }
522
523 /* Don't leave irqs off while doing memory copies */
524 spin_unlock_irqrestore(&dev->lock, flags);
525
526 if (len > current_rx_bytes)
527 size = current_rx_bytes;
528 else
529 size = len;
530
531 size -= copy_to_user(buf, current_rx_buf, size);
532 bytes_copied += size;
533 len -= size;
534 buf += size;
535
536 spin_lock_irqsave(&dev->lock, flags);
537
Craig W. Nadlercc901bb2008-03-20 14:46:26 -0700538 /* We've disconnected or reset so return. */
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800539 if (dev->reset_printer) {
Craig W. Nadlercc901bb2008-03-20 14:46:26 -0700540 list_add(&current_rx_req->list, &dev->rx_reqs);
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800541 spin_unlock_irqrestore(&dev->lock, flags);
Jiri Slaby10ca4422010-06-21 17:02:40 +0200542 mutex_unlock(&dev->lock_printer_io);
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800543 return -EAGAIN;
544 }
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(&current_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);
Jiri Slaby10ca4422010-06-21 17:02:40 +0200567 mutex_unlock(&dev->lock_printer_io);
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800568
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
577static ssize_t
578printer_write(struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
579{
580 struct printer_dev *dev = fd->private_data;
581 unsigned long flags;
582 size_t size; /* Amount of data in a TX request. */
583 size_t bytes_copied = 0;
584 struct usb_request *req;
585
586 DBG(dev, "printer_write trying to send %d bytes\n", (int)len);
587
588 if (len == 0)
589 return -EINVAL;
590
Jiri Slaby10ca4422010-06-21 17:02:40 +0200591 mutex_lock(&dev->lock_printer_io);
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800592 spin_lock_irqsave(&dev->lock, flags);
593
594 /* Check if a printer reset happens while we have interrupts on */
595 dev->reset_printer = 0;
596
597 /* Check if there is any available write buffers */
598 if (likely(list_empty(&dev->tx_reqs))) {
599 /* Turn interrupts back on before sleeping. */
600 spin_unlock_irqrestore(&dev->lock, flags);
601
602 /*
603 * If write buffers are available check if this is
604 * a NON-Blocking call or not.
605 */
606 if (fd->f_flags & (O_NONBLOCK|O_NDELAY)) {
Jiri Slaby10ca4422010-06-21 17:02:40 +0200607 mutex_unlock(&dev->lock_printer_io);
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800608 return -EAGAIN;
609 }
610
611 /* Sleep until a write buffer is available */
612 wait_event_interruptible(dev->tx_wait,
613 (likely(!list_empty(&dev->tx_reqs))));
614 spin_lock_irqsave(&dev->lock, flags);
615 }
616
617 while (likely(!list_empty(&dev->tx_reqs)) && len) {
618
619 if (len > USB_BUFSIZE)
620 size = USB_BUFSIZE;
621 else
622 size = len;
623
624 req = container_of(dev->tx_reqs.next, struct usb_request,
625 list);
626 list_del_init(&req->list);
627
628 req->complete = tx_complete;
629 req->length = size;
630
631 /* Check if we need to send a zero length packet. */
632 if (len > size)
633 /* They will be more TX requests so no yet. */
634 req->zero = 0;
635 else
636 /* If the data amount is not a multple of the
637 * maxpacket size then send a zero length packet.
638 */
639 req->zero = ((len % dev->in_ep->maxpacket) == 0);
640
641 /* Don't leave irqs off while doing memory copies */
642 spin_unlock_irqrestore(&dev->lock, flags);
643
644 if (copy_from_user(req->buf, buf, size)) {
645 list_add(&req->list, &dev->tx_reqs);
Jiri Slaby10ca4422010-06-21 17:02:40 +0200646 mutex_unlock(&dev->lock_printer_io);
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800647 return bytes_copied;
648 }
649
650 bytes_copied += size;
651 len -= size;
652 buf += size;
653
654 spin_lock_irqsave(&dev->lock, flags);
655
656 /* We've disconnected or reset so free the req and buffer */
657 if (dev->reset_printer) {
Craig W. Nadlercc901bb2008-03-20 14:46:26 -0700658 list_add(&req->list, &dev->tx_reqs);
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800659 spin_unlock_irqrestore(&dev->lock, flags);
Jiri Slaby10ca4422010-06-21 17:02:40 +0200660 mutex_unlock(&dev->lock_printer_io);
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800661 return -EAGAIN;
662 }
663
664 if (usb_ep_queue(dev->in_ep, req, GFP_ATOMIC)) {
665 list_add(&req->list, &dev->tx_reqs);
666 spin_unlock_irqrestore(&dev->lock, flags);
Jiri Slaby10ca4422010-06-21 17:02:40 +0200667 mutex_unlock(&dev->lock_printer_io);
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800668 return -EAGAIN;
669 }
670
671 list_add(&req->list, &dev->tx_reqs_active);
672
673 }
674
675 spin_unlock_irqrestore(&dev->lock, flags);
Jiri Slaby10ca4422010-06-21 17:02:40 +0200676 mutex_unlock(&dev->lock_printer_io);
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800677
678 DBG(dev, "printer_write sent %d bytes\n", (int)bytes_copied);
679
680 if (bytes_copied) {
681 return bytes_copied;
682 } else {
683 return -EAGAIN;
684 }
685}
686
687static int
Josef Bacik02c24a82011-07-16 20:44:56 -0400688printer_fsync(struct file *fd, loff_t start, loff_t end, int datasync)
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800689{
690 struct printer_dev *dev = fd->private_data;
Al Viro496ad9a2013-01-23 17:07:38 -0500691 struct inode *inode = file_inode(fd);
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800692 unsigned long flags;
693 int tx_list_empty;
694
Josef Bacik02c24a82011-07-16 20:44:56 -0400695 mutex_lock(&inode->i_mutex);
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800696 spin_lock_irqsave(&dev->lock, flags);
697 tx_list_empty = (likely(list_empty(&dev->tx_reqs)));
698 spin_unlock_irqrestore(&dev->lock, flags);
699
700 if (!tx_list_empty) {
701 /* Sleep until all data has been sent */
702 wait_event_interruptible(dev->tx_flush_wait,
703 (likely(list_empty(&dev->tx_reqs_active))));
704 }
Josef Bacik02c24a82011-07-16 20:44:56 -0400705 mutex_unlock(&inode->i_mutex);
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800706
707 return 0;
708}
709
710static unsigned int
711printer_poll(struct file *fd, poll_table *wait)
712{
713 struct printer_dev *dev = fd->private_data;
714 unsigned long flags;
715 int status = 0;
716
Jiri Slaby10ca4422010-06-21 17:02:40 +0200717 mutex_lock(&dev->lock_printer_io);
Craig W. Nadlercc901bb2008-03-20 14:46:26 -0700718 spin_lock_irqsave(&dev->lock, flags);
719 setup_rx_reqs(dev);
720 spin_unlock_irqrestore(&dev->lock, flags);
Jiri Slaby10ca4422010-06-21 17:02:40 +0200721 mutex_unlock(&dev->lock_printer_io);
Craig W. Nadlercc901bb2008-03-20 14:46:26 -0700722
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800723 poll_wait(fd, &dev->rx_wait, wait);
724 poll_wait(fd, &dev->tx_wait, wait);
725
726 spin_lock_irqsave(&dev->lock, flags);
727 if (likely(!list_empty(&dev->tx_reqs)))
728 status |= POLLOUT | POLLWRNORM;
729
Craig W. Nadlercc901bb2008-03-20 14:46:26 -0700730 if (likely(dev->current_rx_bytes) ||
731 likely(!list_empty(&dev->rx_buffers)))
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800732 status |= POLLIN | POLLRDNORM;
733
734 spin_unlock_irqrestore(&dev->lock, flags);
735
736 return status;
737}
738
Alan Cox44c389a2008-05-22 22:03:27 +0100739static long
740printer_ioctl(struct file *fd, unsigned int code, unsigned long arg)
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800741{
742 struct printer_dev *dev = fd->private_data;
743 unsigned long flags;
744 int status = 0;
745
746 DBG(dev, "printer_ioctl: cmd=0x%4.4x, arg=%lu\n", code, arg);
747
748 /* handle ioctls */
749
750 spin_lock_irqsave(&dev->lock, flags);
751
752 switch (code) {
753 case GADGET_GET_PRINTER_STATUS:
754 status = (int)dev->printer_status;
755 break;
756 case GADGET_SET_PRINTER_STATUS:
757 dev->printer_status = (u8)arg;
758 break;
759 default:
760 /* could not handle ioctl */
761 DBG(dev, "printer_ioctl: ERROR cmd=0x%4.4xis not supported\n",
762 code);
763 status = -ENOTTY;
764 }
765
766 spin_unlock_irqrestore(&dev->lock, flags);
767
768 return status;
769}
770
771/* used after endpoint configuration */
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700772static const struct file_operations printer_io_operations = {
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800773 .owner = THIS_MODULE,
774 .open = printer_open,
775 .read = printer_read,
776 .write = printer_write,
777 .fsync = printer_fsync,
778 .poll = printer_poll,
Alan Cox44c389a2008-05-22 22:03:27 +0100779 .unlocked_ioctl = printer_ioctl,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200780 .release = printer_close,
781 .llseek = noop_llseek,
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800782};
783
784/*-------------------------------------------------------------------------*/
785
786static int
787set_printer_interface(struct printer_dev *dev)
788{
789 int result = 0;
790
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300791 dev->in_ep->desc = ep_desc(dev->gadget, &hs_ep_in_desc, &fs_ep_in_desc);
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800792 dev->in_ep->driver_data = dev;
793
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300794 dev->out_ep->desc = ep_desc(dev->gadget, &hs_ep_out_desc,
795 &fs_ep_out_desc);
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800796 dev->out_ep->driver_data = dev;
797
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300798 result = usb_ep_enable(dev->in_ep);
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800799 if (result != 0) {
800 DBG(dev, "enable %s --> %d\n", dev->in_ep->name, result);
801 goto done;
802 }
803
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300804 result = usb_ep_enable(dev->out_ep);
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800805 if (result != 0) {
806 DBG(dev, "enable %s --> %d\n", dev->in_ep->name, result);
807 goto done;
808 }
809
810done:
811 /* on error, disable any endpoints */
812 if (result != 0) {
813 (void) usb_ep_disable(dev->in_ep);
814 (void) usb_ep_disable(dev->out_ep);
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300815 dev->in_ep->desc = NULL;
816 dev->out_ep->desc = NULL;
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800817 }
818
819 /* caller is responsible for cleanup on error */
820 return result;
821}
822
823static void printer_reset_interface(struct printer_dev *dev)
824{
825 if (dev->interface < 0)
826 return;
827
Harvey Harrison441b62c2008-03-03 16:08:34 -0800828 DBG(dev, "%s\n", __func__);
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800829
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300830 if (dev->in_ep->desc)
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800831 usb_ep_disable(dev->in_ep);
832
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300833 if (dev->out_ep->desc)
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800834 usb_ep_disable(dev->out_ep);
835
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300836 dev->in_ep->desc = NULL;
837 dev->out_ep->desc = NULL;
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800838 dev->interface = -1;
839}
840
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800841/* Change our operational Interface. */
Sebastian Andrzej Siewior2e87edf2012-02-04 18:55:20 +0100842static int set_interface(struct printer_dev *dev, unsigned number)
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800843{
844 int result = 0;
845
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800846 /* Free the current interface */
Sebastian Andrzej Siewior5a175bb2012-09-06 20:11:17 +0200847 printer_reset_interface(dev);
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800848
Sebastian Andrzej Siewior5a175bb2012-09-06 20:11:17 +0200849 result = set_printer_interface(dev);
850 if (result)
851 printer_reset_interface(dev);
852 else
853 dev->interface = number;
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800854
855 if (!result)
856 INFO(dev, "Using interface %x\n", number);
857
858 return result;
859}
860
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800861static void printer_soft_reset(struct printer_dev *dev)
862{
863 struct usb_request *req;
864
865 INFO(dev, "Received Printer Reset Request\n");
866
867 if (usb_ep_disable(dev->in_ep))
868 DBG(dev, "Failed to disable USB in_ep\n");
869 if (usb_ep_disable(dev->out_ep))
870 DBG(dev, "Failed to disable USB out_ep\n");
871
872 if (dev->current_rx_req != NULL) {
873 list_add(&dev->current_rx_req->list, &dev->rx_reqs);
874 dev->current_rx_req = NULL;
875 }
876 dev->current_rx_bytes = 0;
877 dev->current_rx_buf = NULL;
878 dev->reset_printer = 1;
879
880 while (likely(!(list_empty(&dev->rx_buffers)))) {
881 req = container_of(dev->rx_buffers.next, struct usb_request,
882 list);
883 list_del_init(&req->list);
884 list_add(&req->list, &dev->rx_reqs);
885 }
886
887 while (likely(!(list_empty(&dev->rx_reqs_active)))) {
888 req = container_of(dev->rx_buffers.next, struct usb_request,
889 list);
890 list_del_init(&req->list);
891 list_add(&req->list, &dev->rx_reqs);
892 }
893
894 while (likely(!(list_empty(&dev->tx_reqs_active)))) {
895 req = container_of(dev->tx_reqs_active.next,
896 struct usb_request, list);
897 list_del_init(&req->list);
898 list_add(&req->list, &dev->tx_reqs);
899 }
900
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300901 if (usb_ep_enable(dev->in_ep))
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800902 DBG(dev, "Failed to enable USB in_ep\n");
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300903 if (usb_ep_enable(dev->out_ep))
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800904 DBG(dev, "Failed to enable USB out_ep\n");
905
Craig W. Nadlercc901bb2008-03-20 14:46:26 -0700906 wake_up_interruptible(&dev->rx_wait);
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800907 wake_up_interruptible(&dev->tx_wait);
908 wake_up_interruptible(&dev->tx_flush_wait);
909}
910
911/*-------------------------------------------------------------------------*/
912
913/*
914 * The setup() callback implements all the ep0 functionality that's not
915 * handled lower down.
916 */
Sebastian Andrzej Siewior2e87edf2012-02-04 18:55:20 +0100917static int printer_func_setup(struct usb_function *f,
918 const struct usb_ctrlrequest *ctrl)
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800919{
Sebastian Andrzej Siewior2e87edf2012-02-04 18:55:20 +0100920 struct printer_dev *dev = container_of(f, struct printer_dev, function);
921 struct usb_composite_dev *cdev = f->config->cdev;
922 struct usb_request *req = cdev->req;
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800923 int value = -EOPNOTSUPP;
924 u16 wIndex = le16_to_cpu(ctrl->wIndex);
925 u16 wValue = le16_to_cpu(ctrl->wValue);
926 u16 wLength = le16_to_cpu(ctrl->wLength);
927
928 DBG(dev, "ctrl req%02x.%02x v%04x i%04x l%d\n",
929 ctrl->bRequestType, ctrl->bRequest, wValue, wIndex, wLength);
930
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800931 switch (ctrl->bRequestType&USB_TYPE_MASK) {
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800932 case USB_TYPE_CLASS:
933 switch (ctrl->bRequest) {
934 case 0: /* Get the IEEE-1284 PNP String */
935 /* Only one printer interface is supported. */
Sebastian Andrzej Siewior5a175bb2012-09-06 20:11:17 +0200936 if ((wIndex>>8) != dev->interface)
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800937 break;
938
939 value = (pnp_string[0]<<8)|pnp_string[1];
940 memcpy(req->buf, pnp_string, value);
941 DBG(dev, "1284 PNP String: %x %s\n", value,
942 &pnp_string[2]);
943 break;
944
945 case 1: /* Get Port Status */
946 /* Only one printer interface is supported. */
Sebastian Andrzej Siewior5a175bb2012-09-06 20:11:17 +0200947 if (wIndex != dev->interface)
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800948 break;
949
950 *(u8 *)req->buf = dev->printer_status;
951 value = min(wLength, (u16) 1);
952 break;
953
954 case 2: /* Soft Reset */
955 /* Only one printer interface is supported. */
Sebastian Andrzej Siewior5a175bb2012-09-06 20:11:17 +0200956 if (wIndex != dev->interface)
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800957 break;
958
959 printer_soft_reset(dev);
960
961 value = 0;
962 break;
963
964 default:
965 goto unknown;
966 }
967 break;
968
969 default:
970unknown:
971 VDBG(dev,
972 "unknown ctrl req%02x.%02x v%04x i%04x l%d\n",
973 ctrl->bRequestType, ctrl->bRequest,
974 wValue, wIndex, wLength);
975 break;
976 }
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800977 /* host either stalls (value < 0) or reports success */
978 return value;
979}
980
Sebastian Andrzej Siewior2e87edf2012-02-04 18:55:20 +0100981static int __init printer_func_bind(struct usb_configuration *c,
982 struct usb_function *f)
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800983{
Sebastian Andrzej Siewior5a175bb2012-09-06 20:11:17 +0200984 struct printer_dev *dev = container_of(f, struct printer_dev, function);
985 struct usb_composite_dev *cdev = c->cdev;
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200986 struct usb_ep *in_ep;
987 struct usb_ep *out_ep = NULL;
Sebastian Andrzej Siewior5a175bb2012-09-06 20:11:17 +0200988 int id;
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200989 int ret;
Sebastian Andrzej Siewior5a175bb2012-09-06 20:11:17 +0200990
991 id = usb_interface_id(c, f);
992 if (id < 0)
993 return id;
994 intf_desc.bInterfaceNumber = id;
995
996 /* all we really need is bulk IN/OUT */
997 in_ep = usb_ep_autoconfig(cdev->gadget, &fs_ep_in_desc);
998 if (!in_ep) {
999autoconf_fail:
1000 dev_err(&cdev->gadget->dev, "can't autoconfigure on %s\n",
1001 cdev->gadget->name);
1002 return -ENODEV;
1003 }
1004 in_ep->driver_data = in_ep; /* claim */
1005
1006 out_ep = usb_ep_autoconfig(cdev->gadget, &fs_ep_out_desc);
1007 if (!out_ep)
1008 goto autoconf_fail;
1009 out_ep->driver_data = out_ep; /* claim */
1010
1011 /* assumes that all endpoints are dual-speed */
1012 hs_ep_in_desc.bEndpointAddress = fs_ep_in_desc.bEndpointAddress;
1013 hs_ep_out_desc.bEndpointAddress = fs_ep_out_desc.bEndpointAddress;
1014
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +02001015 ret = usb_assign_descriptors(f, fs_printer_function,
1016 hs_printer_function, NULL);
1017 if (ret)
1018 return ret;
1019
Sebastian Andrzej Siewior5a175bb2012-09-06 20:11:17 +02001020 dev->in_ep = in_ep;
1021 dev->out_ep = out_ep;
Sebastian Andrzej Siewior2e87edf2012-02-04 18:55:20 +01001022 return 0;
1023}
1024
1025static void printer_func_unbind(struct usb_configuration *c,
1026 struct usb_function *f)
1027{
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +02001028 usb_free_all_descriptors(f);
Sebastian Andrzej Siewior2e87edf2012-02-04 18:55:20 +01001029}
1030
1031static int printer_func_set_alt(struct usb_function *f,
1032 unsigned intf, unsigned alt)
1033{
1034 struct printer_dev *dev = container_of(f, struct printer_dev, function);
1035 int ret = -ENOTSUPP;
1036
1037 if (!alt)
Sebastian Andrzej Siewior5a175bb2012-09-06 20:11:17 +02001038 ret = set_interface(dev, intf);
1039
Sebastian Andrzej Siewior2e87edf2012-02-04 18:55:20 +01001040 return ret;
1041}
1042
1043static void printer_func_disable(struct usb_function *f)
1044{
1045 struct printer_dev *dev = container_of(f, struct printer_dev, function);
Craig W. Nadler25a010c2007-11-11 15:00:15 -08001046 unsigned long flags;
1047
Harvey Harrison441b62c2008-03-03 16:08:34 -08001048 DBG(dev, "%s\n", __func__);
Craig W. Nadler25a010c2007-11-11 15:00:15 -08001049
1050 spin_lock_irqsave(&dev->lock, flags);
Craig W. Nadler25a010c2007-11-11 15:00:15 -08001051 printer_reset_interface(dev);
Craig W. Nadler25a010c2007-11-11 15:00:15 -08001052 spin_unlock_irqrestore(&dev->lock, flags);
1053}
1054
Sebastian Andrzej Siewior2e87edf2012-02-04 18:55:20 +01001055static void printer_cfg_unbind(struct usb_configuration *c)
Craig W. Nadler25a010c2007-11-11 15:00:15 -08001056{
Sebastian Andrzej Siewior2e87edf2012-02-04 18:55:20 +01001057 struct printer_dev *dev;
Craig W. Nadler25a010c2007-11-11 15:00:15 -08001058 struct usb_request *req;
1059
Sebastian Andrzej Siewior2e87edf2012-02-04 18:55:20 +01001060 dev = &usb_printer_gadget;
Craig W. Nadler25a010c2007-11-11 15:00:15 -08001061
Harvey Harrison441b62c2008-03-03 16:08:34 -08001062 DBG(dev, "%s\n", __func__);
Craig W. Nadler25a010c2007-11-11 15:00:15 -08001063
1064 /* Remove sysfs files */
1065 device_destroy(usb_gadget_class, g_printer_devno);
1066
1067 /* Remove Character Device */
1068 cdev_del(&dev->printer_cdev);
1069
1070 /* we must already have been disconnected ... no i/o may be active */
1071 WARN_ON(!list_empty(&dev->tx_reqs_active));
1072 WARN_ON(!list_empty(&dev->rx_reqs_active));
1073
1074 /* Free all memory for this driver. */
1075 while (!list_empty(&dev->tx_reqs)) {
1076 req = container_of(dev->tx_reqs.next, struct usb_request,
1077 list);
1078 list_del(&req->list);
1079 printer_req_free(dev->in_ep, req);
1080 }
1081
Adrian Bunkefa66f12008-02-09 03:16:03 -08001082 if (dev->current_rx_req != NULL)
Craig W. Nadler25a010c2007-11-11 15:00:15 -08001083 printer_req_free(dev->out_ep, dev->current_rx_req);
1084
1085 while (!list_empty(&dev->rx_reqs)) {
1086 req = container_of(dev->rx_reqs.next,
1087 struct usb_request, list);
1088 list_del(&req->list);
1089 printer_req_free(dev->out_ep, req);
1090 }
1091
1092 while (!list_empty(&dev->rx_buffers)) {
1093 req = container_of(dev->rx_buffers.next,
1094 struct usb_request, list);
1095 list_del(&req->list);
1096 printer_req_free(dev->out_ep, req);
1097 }
Craig W. Nadler25a010c2007-11-11 15:00:15 -08001098}
1099
Sebastian Andrzej Siewior2e87edf2012-02-04 18:55:20 +01001100static struct usb_configuration printer_cfg_driver = {
1101 .label = "printer",
1102 .unbind = printer_cfg_unbind,
1103 .bConfigurationValue = 1,
1104 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
1105};
1106
1107static int __init printer_bind_config(struct usb_configuration *c)
Craig W. Nadler25a010c2007-11-11 15:00:15 -08001108{
Sebastian Andrzej Siewior2e87edf2012-02-04 18:55:20 +01001109 struct usb_gadget *gadget = c->cdev->gadget;
Craig W. Nadler25a010c2007-11-11 15:00:15 -08001110 struct printer_dev *dev;
Craig W. Nadler25a010c2007-11-11 15:00:15 -08001111 int status = -ENOMEM;
Craig W. Nadler25a010c2007-11-11 15:00:15 -08001112 size_t len;
1113 u32 i;
1114 struct usb_request *req;
1115
Sebastian Andrzej Siewior5a175bb2012-09-06 20:11:17 +02001116 usb_ep_autoconfig_reset(gadget);
1117
Craig W. Nadler25a010c2007-11-11 15:00:15 -08001118 dev = &usb_printer_gadget;
1119
Sebastian Andrzej Siewior2e87edf2012-02-04 18:55:20 +01001120 dev->function.name = shortname;
Sebastian Andrzej Siewior2e87edf2012-02-04 18:55:20 +01001121 dev->function.bind = printer_func_bind;
1122 dev->function.setup = printer_func_setup;
1123 dev->function.unbind = printer_func_unbind;
1124 dev->function.set_alt = printer_func_set_alt;
1125 dev->function.disable = printer_func_disable;
Craig W. Nadler25a010c2007-11-11 15:00:15 -08001126
Sebastian Andrzej Siewior5a175bb2012-09-06 20:11:17 +02001127 status = usb_add_function(c, &dev->function);
1128 if (status)
1129 return status;
1130
Craig W. Nadler25a010c2007-11-11 15:00:15 -08001131 /* Setup the sysfs files for the printer gadget. */
Greg Kroah-Hartmanb0b090e2008-07-21 20:03:34 -07001132 dev->pdev = device_create(usb_gadget_class, NULL, g_printer_devno,
1133 NULL, "g_printer");
Craig W. Nadler25a010c2007-11-11 15:00:15 -08001134 if (IS_ERR(dev->pdev)) {
1135 ERROR(dev, "Failed to create device: g_printer\n");
1136 goto fail;
1137 }
1138
1139 /*
1140 * Register a character device as an interface to a user mode
1141 * program that handles the printer specific functionality.
1142 */
1143 cdev_init(&dev->printer_cdev, &printer_io_operations);
1144 dev->printer_cdev.owner = THIS_MODULE;
1145 status = cdev_add(&dev->printer_cdev, g_printer_devno, 1);
1146 if (status) {
1147 ERROR(dev, "Failed to open char device\n");
1148 goto fail;
1149 }
1150
Craig W. Nadler25a010c2007-11-11 15:00:15 -08001151 if (iPNPstring)
1152 strlcpy(&pnp_string[2], iPNPstring, (sizeof pnp_string)-2);
1153
1154 len = strlen(pnp_string);
1155 pnp_string[0] = (len >> 8) & 0xFF;
1156 pnp_string[1] = len & 0xFF;
1157
Craig W. Nadler25a010c2007-11-11 15:00:15 -08001158 usb_gadget_set_selfpowered(gadget);
1159
1160 if (gadget->is_otg) {
Sebastian Andrzej Siewior2e87edf2012-02-04 18:55:20 +01001161 otg_descriptor.bmAttributes |= USB_OTG_HNP;
1162 printer_cfg_driver.descriptors = otg_desc;
1163 printer_cfg_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
Craig W. Nadler25a010c2007-11-11 15:00:15 -08001164 }
1165
1166 spin_lock_init(&dev->lock);
Jiri Slaby10ca4422010-06-21 17:02:40 +02001167 mutex_init(&dev->lock_printer_io);
Craig W. Nadler25a010c2007-11-11 15:00:15 -08001168 INIT_LIST_HEAD(&dev->tx_reqs);
1169 INIT_LIST_HEAD(&dev->tx_reqs_active);
1170 INIT_LIST_HEAD(&dev->rx_reqs);
1171 INIT_LIST_HEAD(&dev->rx_reqs_active);
1172 INIT_LIST_HEAD(&dev->rx_buffers);
1173 init_waitqueue_head(&dev->rx_wait);
1174 init_waitqueue_head(&dev->tx_wait);
1175 init_waitqueue_head(&dev->tx_flush_wait);
1176
Craig W. Nadler25a010c2007-11-11 15:00:15 -08001177 dev->interface = -1;
1178 dev->printer_cdev_open = 0;
1179 dev->printer_status = PRINTER_NOT_ERROR;
1180 dev->current_rx_req = NULL;
1181 dev->current_rx_bytes = 0;
1182 dev->current_rx_buf = NULL;
1183
Craig W. Nadler25a010c2007-11-11 15:00:15 -08001184 for (i = 0; i < QLEN; i++) {
1185 req = printer_req_alloc(dev->in_ep, USB_BUFSIZE, GFP_KERNEL);
1186 if (!req) {
1187 while (!list_empty(&dev->tx_reqs)) {
1188 req = container_of(dev->tx_reqs.next,
1189 struct usb_request, list);
1190 list_del(&req->list);
1191 printer_req_free(dev->in_ep, req);
1192 }
1193 return -ENOMEM;
1194 }
1195 list_add(&req->list, &dev->tx_reqs);
1196 }
1197
1198 for (i = 0; i < QLEN; i++) {
1199 req = printer_req_alloc(dev->out_ep, USB_BUFSIZE, GFP_KERNEL);
1200 if (!req) {
1201 while (!list_empty(&dev->rx_reqs)) {
1202 req = container_of(dev->rx_reqs.next,
1203 struct usb_request, list);
1204 list_del(&req->list);
1205 printer_req_free(dev->out_ep, req);
1206 }
1207 return -ENOMEM;
1208 }
1209 list_add(&req->list, &dev->rx_reqs);
1210 }
1211
Craig W. Nadler25a010c2007-11-11 15:00:15 -08001212 /* finish hookup to lower layer ... */
1213 dev->gadget = gadget;
Craig W. Nadler25a010c2007-11-11 15:00:15 -08001214
1215 INFO(dev, "%s, version: " DRIVER_VERSION "\n", driver_desc);
Craig W. Nadler25a010c2007-11-11 15:00:15 -08001216 return 0;
1217
1218fail:
Sebastian Andrzej Siewior2e87edf2012-02-04 18:55:20 +01001219 printer_cfg_unbind(c);
Craig W. Nadler25a010c2007-11-11 15:00:15 -08001220 return status;
1221}
1222
Sebastian Andrzej Siewior2e87edf2012-02-04 18:55:20 +01001223static int printer_unbind(struct usb_composite_dev *cdev)
1224{
1225 return 0;
1226}
Craig W. Nadler25a010c2007-11-11 15:00:15 -08001227
Sebastian Andrzej Siewior2e87edf2012-02-04 18:55:20 +01001228static int __init printer_bind(struct usb_composite_dev *cdev)
1229{
Sebastian Andrzej Siewior5a175bb2012-09-06 20:11:17 +02001230 int ret;
1231
1232 ret = usb_string_ids_tab(cdev, strings);
1233 if (ret < 0)
1234 return ret;
Sebastian Andrzej Siewior276e2e42012-09-06 20:11:21 +02001235 device_desc.iManufacturer = strings[USB_GADGET_MANUFACTURER_IDX].id;
1236 device_desc.iProduct = strings[USB_GADGET_PRODUCT_IDX].id;
1237 device_desc.iSerialNumber = strings[USB_GADGET_SERIAL_IDX].id;
Sebastian Andrzej Siewior5a175bb2012-09-06 20:11:17 +02001238
1239 ret = usb_add_config(cdev, &printer_cfg_driver, printer_bind_config);
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02001240 if (ret)
1241 return ret;
1242 usb_composite_overwrite_options(cdev, &coverwrite);
Sebastian Andrzej Siewior5a175bb2012-09-06 20:11:17 +02001243 return ret;
Sebastian Andrzej Siewior2e87edf2012-02-04 18:55:20 +01001244}
Craig W. Nadler25a010c2007-11-11 15:00:15 -08001245
Sebastian Andrzej Siewiorc2ec75c2012-09-06 20:11:03 +02001246static __refdata struct usb_composite_driver printer_driver = {
Sebastian Andrzej Siewior2e87edf2012-02-04 18:55:20 +01001247 .name = shortname,
1248 .dev = &device_desc,
1249 .strings = dev_strings,
1250 .max_speed = USB_SPEED_HIGH,
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +02001251 .bind = printer_bind,
Craig W. Nadler25a010c2007-11-11 15:00:15 -08001252 .unbind = printer_unbind,
Craig W. Nadler25a010c2007-11-11 15:00:15 -08001253};
1254
Craig W. Nadler25a010c2007-11-11 15:00:15 -08001255static int __init
1256init(void)
1257{
1258 int status;
1259
1260 usb_gadget_class = class_create(THIS_MODULE, "usb_printer_gadget");
1261 if (IS_ERR(usb_gadget_class)) {
1262 status = PTR_ERR(usb_gadget_class);
Sebastian Andrzej Siewior2e87edf2012-02-04 18:55:20 +01001263 pr_err("unable to create usb_gadget class %d\n", status);
Craig W. Nadler25a010c2007-11-11 15:00:15 -08001264 return status;
1265 }
1266
1267 status = alloc_chrdev_region(&g_printer_devno, 0, 1,
1268 "USB printer gadget");
1269 if (status) {
Sebastian Andrzej Siewior2e87edf2012-02-04 18:55:20 +01001270 pr_err("alloc_chrdev_region %d\n", status);
Craig W. Nadler25a010c2007-11-11 15:00:15 -08001271 class_destroy(usb_gadget_class);
1272 return status;
1273 }
1274
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +02001275 status = usb_composite_probe(&printer_driver);
Craig W. Nadler25a010c2007-11-11 15:00:15 -08001276 if (status) {
1277 class_destroy(usb_gadget_class);
1278 unregister_chrdev_region(g_printer_devno, 1);
Sebastian Andrzej Siewior2e87edf2012-02-04 18:55:20 +01001279 pr_err("usb_gadget_probe_driver %x\n", status);
Craig W. Nadler25a010c2007-11-11 15:00:15 -08001280 }
1281
1282 return status;
1283}
1284module_init(init);
1285
1286static void __exit
1287cleanup(void)
1288{
Jiri Slaby10ca4422010-06-21 17:02:40 +02001289 mutex_lock(&usb_printer_gadget.lock_printer_io);
Sebastian Andrzej Siewior2e87edf2012-02-04 18:55:20 +01001290 usb_composite_unregister(&printer_driver);
Fabian Godehardt8582d862011-09-01 14:15:46 +02001291 unregister_chrdev_region(g_printer_devno, 1);
Alan Sternd5aa4752011-01-10 11:23:05 -05001292 class_destroy(usb_gadget_class);
Jiri Slaby10ca4422010-06-21 17:02:40 +02001293 mutex_unlock(&usb_printer_gadget.lock_printer_io);
Craig W. Nadler25a010c2007-11-11 15:00:15 -08001294}
1295module_exit(cleanup);
Sebastian Andrzej Siewior2e87edf2012-02-04 18:55:20 +01001296
1297MODULE_DESCRIPTION(DRIVER_DESC);
1298MODULE_AUTHOR("Craig Nadler");
1299MODULE_LICENSE("GPL");