aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/usb/gadget
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/usb/gadget')
-rw-r--r--drivers/usb/gadget/Kconfig17
-rw-r--r--drivers/usb/gadget/composite.c17
-rw-r--r--drivers/usb/gadget/function/Makefile2
-rw-r--r--drivers/usb/gadget/function/f_hid.c1
-rw-r--r--drivers/usb/gadget/function/f_mass_storage.c2
-rw-r--r--drivers/usb/gadget/function/f_printer.c1471
-rw-r--r--drivers/usb/gadget/function/u_printer.h37
-rw-r--r--drivers/usb/gadget/function/u_serial.c2
-rw-r--r--drivers/usb/gadget/legacy/Kconfig1
-rw-r--r--drivers/usb/gadget/legacy/printer.c1239
-rw-r--r--drivers/usb/gadget/udc/atmel_usba_udc.c212
-rw-r--r--drivers/usb/gadget/udc/atmel_usba_udc.h26
-rw-r--r--drivers/usb/gadget/udc/dummy_hcd.c2
-rw-r--r--drivers/usb/gadget/udc/goku_udc.c233
-rw-r--r--drivers/usb/gadget/udc/lpc32xx_udc.c15
-rw-r--r--drivers/usb/gadget/udc/net2280.c182
-rw-r--r--drivers/usb/gadget/udc/net2280.h2
-rw-r--r--drivers/usb/gadget/udc/pxa27x_udc.c132
-rw-r--r--drivers/usb/gadget/udc/udc-core.c68
19 files changed, 2126 insertions, 1535 deletions
diff --git a/drivers/usb/gadget/Kconfig b/drivers/usb/gadget/Kconfig
index b454d05be583..bcf83c0a6e62 100644
--- a/drivers/usb/gadget/Kconfig
+++ b/drivers/usb/gadget/Kconfig
@@ -196,6 +196,9 @@ config USB_F_MIDI
196config USB_F_HID 196config USB_F_HID
197 tristate 197 tristate
198 198
199config USB_F_PRINTER
200 tristate
201
199choice 202choice
200 tristate "USB Gadget Drivers" 203 tristate "USB Gadget Drivers"
201 default USB_ETH 204 default USB_ETH
@@ -434,6 +437,20 @@ config USB_CONFIGFS_F_UVC
434 device. It provides a userspace API to process UVC control requests 437 device. It provides a userspace API to process UVC control requests
435 and stream video data to the host. 438 and stream video data to the host.
436 439
440config USB_CONFIGFS_F_PRINTER
441 bool "Printer function"
442 select USB_F_PRINTER
443 depends on USB_CONFIGFS
444 help
445 The Printer function channels data between the USB host and a
446 userspace program driving the print engine. The user space
447 program reads and writes the device file /dev/g_printer<X> to
448 receive or send printer data. It can use ioctl calls to
449 the device file to get or set printer status.
450
451 For more information, see Documentation/usb/gadget_printer.txt
452 which includes sample code for accessing the device file.
453
437source "drivers/usb/gadget/legacy/Kconfig" 454source "drivers/usb/gadget/legacy/Kconfig"
438 455
439endchoice 456endchoice
diff --git a/drivers/usb/gadget/composite.c b/drivers/usb/gadget/composite.c
index 13adfd1a3f54..4e3447bbd097 100644
--- a/drivers/usb/gadget/composite.c
+++ b/drivers/usb/gadget/composite.c
@@ -1161,11 +1161,11 @@ static struct usb_gadget_string_container *copy_gadget_strings(
1161 * This function will create a deep copy of usb_gadget_strings and usb_string 1161 * This function will create a deep copy of usb_gadget_strings and usb_string
1162 * and attach it to the cdev. The actual string (usb_string.s) will not be 1162 * and attach it to the cdev. The actual string (usb_string.s) will not be
1163 * copied but only a referenced will be made. The struct usb_gadget_strings 1163 * copied but only a referenced will be made. The struct usb_gadget_strings
1164 * array may contain multiple languges and should be NULL terminated. 1164 * array may contain multiple languages and should be NULL terminated.
1165 * The ->language pointer of each struct usb_gadget_strings has to contain the 1165 * The ->language pointer of each struct usb_gadget_strings has to contain the
1166 * same amount of entries. 1166 * same amount of entries.
1167 * For instance: sp[0] is en-US, sp[1] is es-ES. It is expected that the first 1167 * For instance: sp[0] is en-US, sp[1] is es-ES. It is expected that the first
1168 * usb_string entry of es-ES containts the translation of the first usb_string 1168 * usb_string entry of es-ES contains the translation of the first usb_string
1169 * entry of en-US. Therefore both entries become the same id assign. 1169 * entry of en-US. Therefore both entries become the same id assign.
1170 */ 1170 */
1171struct usb_string *usb_gstrings_attach(struct usb_composite_dev *cdev, 1171struct usb_string *usb_gstrings_attach(struct usb_composite_dev *cdev,
@@ -1472,6 +1472,13 @@ composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1472 req->length = 0; 1472 req->length = 0;
1473 gadget->ep0->driver_data = cdev; 1473 gadget->ep0->driver_data = cdev;
1474 1474
1475 /*
1476 * Don't let non-standard requests match any of the cases below
1477 * by accident.
1478 */
1479 if ((ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_STANDARD)
1480 goto unknown;
1481
1475 switch (ctrl->bRequest) { 1482 switch (ctrl->bRequest) {
1476 1483
1477 /* we handle all standard USB descriptors */ 1484 /* we handle all standard USB descriptors */
@@ -1751,6 +1758,10 @@ unknown:
1751 * take such requests too, if that's ever needed: to work 1758 * take such requests too, if that's ever needed: to work
1752 * in config 0, etc. 1759 * in config 0, etc.
1753 */ 1760 */
1761 list_for_each_entry(f, &cdev->config->functions, list)
1762 if (f->req_match && f->req_match(f, ctrl))
1763 goto try_fun_setup;
1764 f = NULL;
1754 switch (ctrl->bRequestType & USB_RECIP_MASK) { 1765 switch (ctrl->bRequestType & USB_RECIP_MASK) {
1755 case USB_RECIP_INTERFACE: 1766 case USB_RECIP_INTERFACE:
1756 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES) 1767 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
@@ -1768,7 +1779,7 @@ unknown:
1768 f = NULL; 1779 f = NULL;
1769 break; 1780 break;
1770 } 1781 }
1771 1782try_fun_setup:
1772 if (f && f->setup) 1783 if (f && f->setup)
1773 value = f->setup(f, ctrl); 1784 value = f->setup(f, ctrl);
1774 else { 1785 else {
diff --git a/drivers/usb/gadget/function/Makefile b/drivers/usb/gadget/function/Makefile
index f71b1aaa0edf..bd7def576955 100644
--- a/drivers/usb/gadget/function/Makefile
+++ b/drivers/usb/gadget/function/Makefile
@@ -42,3 +42,5 @@ usb_f_midi-y := f_midi.o
42obj-$(CONFIG_USB_F_MIDI) += usb_f_midi.o 42obj-$(CONFIG_USB_F_MIDI) += usb_f_midi.o
43usb_f_hid-y := f_hid.o 43usb_f_hid-y := f_hid.o
44obj-$(CONFIG_USB_F_HID) += usb_f_hid.o 44obj-$(CONFIG_USB_F_HID) += usb_f_hid.o
45usb_f_printer-y := f_printer.o
46obj-$(CONFIG_USB_F_PRINTER) += usb_f_printer.o
diff --git a/drivers/usb/gadget/function/f_hid.c b/drivers/usb/gadget/function/f_hid.c
index a2612fb79eff..13dfc9915b1d 100644
--- a/drivers/usb/gadget/function/f_hid.c
+++ b/drivers/usb/gadget/function/f_hid.c
@@ -908,7 +908,6 @@ static void hidg_unbind(struct usb_configuration *c, struct usb_function *f)
908 908
909 /* disable/free request and end point */ 909 /* disable/free request and end point */
910 usb_ep_disable(hidg->in_ep); 910 usb_ep_disable(hidg->in_ep);
911 usb_ep_dequeue(hidg->in_ep, hidg->req);
912 kfree(hidg->req->buf); 911 kfree(hidg->req->buf);
913 usb_ep_free_request(hidg->in_ep, hidg->req); 912 usb_ep_free_request(hidg->in_ep, hidg->req);
914 913
diff --git a/drivers/usb/gadget/function/f_mass_storage.c b/drivers/usb/gadget/function/f_mass_storage.c
index 49a2f19c2b87..3cc109f3c9c8 100644
--- a/drivers/usb/gadget/function/f_mass_storage.c
+++ b/drivers/usb/gadget/function/f_mass_storage.c
@@ -1085,7 +1085,7 @@ static int do_inquiry(struct fsg_common *common, struct fsg_buffhd *bh)
1085 if (!curlun) { /* Unsupported LUNs are okay */ 1085 if (!curlun) { /* Unsupported LUNs are okay */
1086 common->bad_lun_okay = 1; 1086 common->bad_lun_okay = 1;
1087 memset(buf, 0, 36); 1087 memset(buf, 0, 36);
1088 buf[0] = 0x7f; /* Unsupported, no device-type */ 1088 buf[0] = TYPE_NO_LUN; /* Unsupported, no device-type */
1089 buf[4] = 31; /* Additional length */ 1089 buf[4] = 31; /* Additional length */
1090 return 36; 1090 return 36;
1091 } 1091 }
diff --git a/drivers/usb/gadget/function/f_printer.c b/drivers/usb/gadget/function/f_printer.c
new file mode 100644
index 000000000000..44173df27273
--- /dev/null
+++ b/drivers/usb/gadget/function/f_printer.c
@@ -0,0 +1,1471 @@
1/*
2 * f_printer.c - USB printer function driver
3 *
4 * Copied from drivers/usb/gadget/legacy/printer.c,
5 * which was:
6 *
7 * printer.c -- Printer gadget driver
8 *
9 * Copyright (C) 2003-2005 David Brownell
10 * Copyright (C) 2006 Craig W. Nadler
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 */
17
18#include <linux/module.h>
19#include <linux/kernel.h>
20#include <linux/delay.h>
21#include <linux/ioport.h>
22#include <linux/sched.h>
23#include <linux/slab.h>
24#include <linux/mutex.h>
25#include <linux/errno.h>
26#include <linux/init.h>
27#include <linux/idr.h>
28#include <linux/timer.h>
29#include <linux/list.h>
30#include <linux/interrupt.h>
31#include <linux/device.h>
32#include <linux/moduleparam.h>
33#include <linux/fs.h>
34#include <linux/poll.h>
35#include <linux/types.h>
36#include <linux/ctype.h>
37#include <linux/cdev.h>
38
39#include <asm/byteorder.h>
40#include <linux/io.h>
41#include <linux/irq.h>
42#include <linux/uaccess.h>
43#include <asm/unaligned.h>
44
45#include <linux/usb/ch9.h>
46#include <linux/usb/composite.h>
47#include <linux/usb/gadget.h>
48#include <linux/usb/g_printer.h>
49
50#include "u_printer.h"
51
52#define PNP_STRING_LEN 1024
53#define PRINTER_MINORS 4
54#define GET_DEVICE_ID 0
55#define GET_PORT_STATUS 1
56#define SOFT_RESET 2
57
58static int major, minors;
59static struct class *usb_gadget_class;
60static DEFINE_IDA(printer_ida);
61static DEFINE_MUTEX(printer_ida_lock); /* protects access do printer_ida */
62
63/*-------------------------------------------------------------------------*/
64
65struct printer_dev {
66 spinlock_t lock; /* lock this structure */
67 /* lock buffer lists during read/write calls */
68 struct mutex lock_printer_io;
69 struct usb_gadget *gadget;
70 s8 interface;
71 struct usb_ep *in_ep, *out_ep;
72
73 struct list_head rx_reqs; /* List of free RX structs */
74 struct list_head rx_reqs_active; /* List of Active RX xfers */
75 struct list_head rx_buffers; /* List of completed xfers */
76 /* wait until there is data to be read. */
77 wait_queue_head_t rx_wait;
78 struct list_head tx_reqs; /* List of free TX structs */
79 struct list_head tx_reqs_active; /* List of Active TX xfers */
80 /* Wait until there are write buffers available to use. */
81 wait_queue_head_t tx_wait;
82 /* Wait until all write buffers have been sent. */
83 wait_queue_head_t tx_flush_wait;
84 struct usb_request *current_rx_req;
85 size_t current_rx_bytes;
86 u8 *current_rx_buf;
87 u8 printer_status;
88 u8 reset_printer;
89 int minor;
90 struct cdev printer_cdev;
91 u8 printer_cdev_open;
92 wait_queue_head_t wait;
93 unsigned q_len;
94 char *pnp_string; /* We don't own memory! */
95 struct usb_function function;
96};
97
98static inline struct printer_dev *func_to_printer(struct usb_function *f)
99{
100 return container_of(f, struct printer_dev, function);
101}
102
103/*-------------------------------------------------------------------------*/
104
105/*
106 * DESCRIPTORS ... most are static, but strings and (full) configuration
107 * descriptors are built on demand.
108 */
109
110/* holds our biggest descriptor */
111#define USB_DESC_BUFSIZE 256
112#define USB_BUFSIZE 8192
113
114static struct usb_interface_descriptor intf_desc = {
115 .bLength = sizeof(intf_desc),
116 .bDescriptorType = USB_DT_INTERFACE,
117 .bNumEndpoints = 2,
118 .bInterfaceClass = USB_CLASS_PRINTER,
119 .bInterfaceSubClass = 1, /* Printer Sub-Class */
120 .bInterfaceProtocol = 2, /* Bi-Directional */
121 .iInterface = 0
122};
123
124static struct usb_endpoint_descriptor fs_ep_in_desc = {
125 .bLength = USB_DT_ENDPOINT_SIZE,
126 .bDescriptorType = USB_DT_ENDPOINT,
127 .bEndpointAddress = USB_DIR_IN,
128 .bmAttributes = USB_ENDPOINT_XFER_BULK
129};
130
131static struct usb_endpoint_descriptor fs_ep_out_desc = {
132 .bLength = USB_DT_ENDPOINT_SIZE,
133 .bDescriptorType = USB_DT_ENDPOINT,
134 .bEndpointAddress = USB_DIR_OUT,
135 .bmAttributes = USB_ENDPOINT_XFER_BULK
136};
137
138static struct usb_descriptor_header *fs_printer_function[] = {
139 (struct usb_descriptor_header *) &intf_desc,
140 (struct usb_descriptor_header *) &fs_ep_in_desc,
141 (struct usb_descriptor_header *) &fs_ep_out_desc,
142 NULL
143};
144
145/*
146 * usb 2.0 devices need to expose both high speed and full speed
147 * descriptors, unless they only run at full speed.
148 */
149
150static struct usb_endpoint_descriptor hs_ep_in_desc = {
151 .bLength = USB_DT_ENDPOINT_SIZE,
152 .bDescriptorType = USB_DT_ENDPOINT,
153 .bmAttributes = USB_ENDPOINT_XFER_BULK,
154 .wMaxPacketSize = cpu_to_le16(512)
155};
156
157static struct usb_endpoint_descriptor hs_ep_out_desc = {
158 .bLength = USB_DT_ENDPOINT_SIZE,
159 .bDescriptorType = USB_DT_ENDPOINT,
160 .bmAttributes = USB_ENDPOINT_XFER_BULK,
161 .wMaxPacketSize = cpu_to_le16(512)
162};
163
164static struct usb_qualifier_descriptor dev_qualifier = {
165 .bLength = sizeof(dev_qualifier),
166 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
167 .bcdUSB = cpu_to_le16(0x0200),
168 .bDeviceClass = USB_CLASS_PRINTER,
169 .bNumConfigurations = 1
170};
171
172static struct usb_descriptor_header *hs_printer_function[] = {
173 (struct usb_descriptor_header *) &intf_desc,
174 (struct usb_descriptor_header *) &hs_ep_in_desc,
175 (struct usb_descriptor_header *) &hs_ep_out_desc,
176 NULL
177};
178
179/*
180 * Added endpoint descriptors for 3.0 devices
181 */
182
183static struct usb_endpoint_descriptor ss_ep_in_desc = {
184 .bLength = USB_DT_ENDPOINT_SIZE,
185 .bDescriptorType = USB_DT_ENDPOINT,
186 .bmAttributes = USB_ENDPOINT_XFER_BULK,
187 .wMaxPacketSize = cpu_to_le16(1024),
188};
189
190static struct usb_ss_ep_comp_descriptor ss_ep_in_comp_desc = {
191 .bLength = sizeof(ss_ep_in_comp_desc),
192 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
193};
194
195static struct usb_endpoint_descriptor ss_ep_out_desc = {
196 .bLength = USB_DT_ENDPOINT_SIZE,
197 .bDescriptorType = USB_DT_ENDPOINT,
198 .bmAttributes = USB_ENDPOINT_XFER_BULK,
199 .wMaxPacketSize = cpu_to_le16(1024),
200};
201
202static struct usb_ss_ep_comp_descriptor ss_ep_out_comp_desc = {
203 .bLength = sizeof(ss_ep_out_comp_desc),
204 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
205};
206
207static struct usb_descriptor_header *ss_printer_function[] = {
208 (struct usb_descriptor_header *) &intf_desc,
209 (struct usb_descriptor_header *) &ss_ep_in_desc,
210 (struct usb_descriptor_header *) &ss_ep_in_comp_desc,
211 (struct usb_descriptor_header *) &ss_ep_out_desc,
212 (struct usb_descriptor_header *) &ss_ep_out_comp_desc,
213 NULL
214};
215
216/* maxpacket and other transfer characteristics vary by speed. */
217static inline struct usb_endpoint_descriptor *ep_desc(struct usb_gadget *gadget,
218 struct usb_endpoint_descriptor *fs,
219 struct usb_endpoint_descriptor *hs,
220 struct usb_endpoint_descriptor *ss)
221{
222 switch (gadget->speed) {
223 case USB_SPEED_SUPER:
224 return ss;
225 case USB_SPEED_HIGH:
226 return hs;
227 default:
228 return fs;
229 }
230}
231
232/*-------------------------------------------------------------------------*/
233
234static struct usb_request *
235printer_req_alloc(struct usb_ep *ep, unsigned len, gfp_t gfp_flags)
236{
237 struct usb_request *req;
238
239 req = usb_ep_alloc_request(ep, gfp_flags);
240
241 if (req != NULL) {
242 req->length = len;
243 req->buf = kmalloc(len, gfp_flags);
244 if (req->buf == NULL) {
245 usb_ep_free_request(ep, req);
246 return NULL;
247 }
248 }
249
250 return req;
251}
252
253static void
254printer_req_free(struct usb_ep *ep, struct usb_request *req)
255{
256 if (ep != NULL && req != NULL) {
257 kfree(req->buf);
258 usb_ep_free_request(ep, req);
259 }
260}
261
262/*-------------------------------------------------------------------------*/
263
264static void rx_complete(struct usb_ep *ep, struct usb_request *req)
265{
266 struct printer_dev *dev = ep->driver_data;
267 int status = req->status;
268 unsigned long flags;
269
270 spin_lock_irqsave(&dev->lock, flags);
271
272 list_del_init(&req->list); /* Remode from Active List */
273
274 switch (status) {
275
276 /* normal completion */
277 case 0:
278 if (req->actual > 0) {
279 list_add_tail(&req->list, &dev->rx_buffers);
280 DBG(dev, "G_Printer : rx length %d\n", req->actual);
281 } else {
282 list_add(&req->list, &dev->rx_reqs);
283 }
284 break;
285
286 /* software-driven interface shutdown */
287 case -ECONNRESET: /* unlink */
288 case -ESHUTDOWN: /* disconnect etc */
289 VDBG(dev, "rx shutdown, code %d\n", status);
290 list_add(&req->list, &dev->rx_reqs);
291 break;
292
293 /* for hardware automagic (such as pxa) */
294 case -ECONNABORTED: /* endpoint reset */
295 DBG(dev, "rx %s reset\n", ep->name);
296 list_add(&req->list, &dev->rx_reqs);
297 break;
298
299 /* data overrun */
300 case -EOVERFLOW:
301 /* FALLTHROUGH */
302
303 default:
304 DBG(dev, "rx status %d\n", status);
305 list_add(&req->list, &dev->rx_reqs);
306 break;
307 }
308
309 wake_up_interruptible(&dev->rx_wait);
310 spin_unlock_irqrestore(&dev->lock, flags);
311}
312
313static void tx_complete(struct usb_ep *ep, struct usb_request *req)
314{
315 struct printer_dev *dev = ep->driver_data;
316
317 switch (req->status) {
318 default:
319 VDBG(dev, "tx err %d\n", req->status);
320 /* FALLTHROUGH */
321 case -ECONNRESET: /* unlink */
322 case -ESHUTDOWN: /* disconnect etc */
323 break;
324 case 0:
325 break;
326 }
327
328 spin_lock(&dev->lock);
329 /* Take the request struct off the active list and put it on the
330 * free list.
331 */
332 list_del_init(&req->list);
333 list_add(&req->list, &dev->tx_reqs);
334 wake_up_interruptible(&dev->tx_wait);
335 if (likely(list_empty(&dev->tx_reqs_active)))
336 wake_up_interruptible(&dev->tx_flush_wait);
337
338 spin_unlock(&dev->lock);
339}
340
341/*-------------------------------------------------------------------------*/
342
343static int
344printer_open(struct inode *inode, struct file *fd)
345{
346 struct printer_dev *dev;
347 unsigned long flags;
348 int ret = -EBUSY;
349
350 dev = container_of(inode->i_cdev, struct printer_dev, printer_cdev);
351
352 spin_lock_irqsave(&dev->lock, flags);
353
354 if (!dev->printer_cdev_open) {
355 dev->printer_cdev_open = 1;
356 fd->private_data = dev;
357 ret = 0;
358 /* Change the printer status to show that it's on-line. */
359 dev->printer_status |= PRINTER_SELECTED;
360 }
361
362 spin_unlock_irqrestore(&dev->lock, flags);
363
364 DBG(dev, "printer_open returned %x\n", ret);
365 return ret;
366}
367
368static int
369printer_close(struct inode *inode, struct file *fd)
370{
371 struct printer_dev *dev = fd->private_data;
372 unsigned long flags;
373
374 spin_lock_irqsave(&dev->lock, flags);
375 dev->printer_cdev_open = 0;
376 fd->private_data = NULL;
377 /* Change printer status to show that the printer is off-line. */
378 dev->printer_status &= ~PRINTER_SELECTED;
379 spin_unlock_irqrestore(&dev->lock, flags);
380
381 DBG(dev, "printer_close\n");
382
383 return 0;
384}
385
386/* This function must be called with interrupts turned off. */
387static void
388setup_rx_reqs(struct printer_dev *dev)
389{
390 struct usb_request *req;
391
392 while (likely(!list_empty(&dev->rx_reqs))) {
393 int error;
394
395 req = container_of(dev->rx_reqs.next,
396 struct usb_request, list);
397 list_del_init(&req->list);
398
399 /* The USB Host sends us whatever amount of data it wants to
400 * so we always set the length field to the full USB_BUFSIZE.
401 * If the amount of data is more than the read() caller asked
402 * for it will be stored in the request buffer until it is
403 * asked for by read().
404 */
405 req->length = USB_BUFSIZE;
406 req->complete = rx_complete;
407
408 /* here, we unlock, and only unlock, to avoid deadlock. */
409 spin_unlock(&dev->lock);
410 error = usb_ep_queue(dev->out_ep, req, GFP_ATOMIC);
411 spin_lock(&dev->lock);
412 if (error) {
413 DBG(dev, "rx submit --> %d\n", error);
414 list_add(&req->list, &dev->rx_reqs);
415 break;
416 }
417 /* if the req is empty, then add it into dev->rx_reqs_active. */
418 else if (list_empty(&req->list))
419 list_add(&req->list, &dev->rx_reqs_active);
420 }
421}
422
423static ssize_t
424printer_read(struct file *fd, char __user *buf, size_t len, loff_t *ptr)
425{
426 struct printer_dev *dev = fd->private_data;
427 unsigned long flags;
428 size_t size;
429 size_t bytes_copied;
430 struct usb_request *req;
431 /* This is a pointer to the current USB rx request. */
432 struct usb_request *current_rx_req;
433 /* This is the number of bytes in the current rx buffer. */
434 size_t current_rx_bytes;
435 /* This is a pointer to the current rx buffer. */
436 u8 *current_rx_buf;
437
438 if (len == 0)
439 return -EINVAL;
440
441 DBG(dev, "printer_read trying to read %d bytes\n", (int)len);
442
443 mutex_lock(&dev->lock_printer_io);
444 spin_lock_irqsave(&dev->lock, flags);
445
446 /* We will use this flag later to check if a printer reset happened
447 * after we turn interrupts back on.
448 */
449 dev->reset_printer = 0;
450
451 setup_rx_reqs(dev);
452
453 bytes_copied = 0;
454 current_rx_req = dev->current_rx_req;
455 current_rx_bytes = dev->current_rx_bytes;
456 current_rx_buf = dev->current_rx_buf;
457 dev->current_rx_req = NULL;
458 dev->current_rx_bytes = 0;
459 dev->current_rx_buf = NULL;
460
461 /* Check if there is any data in the read buffers. Please note that
462 * current_rx_bytes is the number of bytes in the current rx buffer.
463 * If it is zero then check if there are any other rx_buffers that
464 * are on the completed list. We are only out of data if all rx
465 * buffers are empty.
466 */
467 if ((current_rx_bytes == 0) &&
468 (likely(list_empty(&dev->rx_buffers)))) {
469 /* Turn interrupts back on before sleeping. */
470 spin_unlock_irqrestore(&dev->lock, flags);
471
472 /*
473 * If no data is available check if this is a NON-Blocking
474 * call or not.
475 */
476 if (fd->f_flags & (O_NONBLOCK|O_NDELAY)) {
477 mutex_unlock(&dev->lock_printer_io);
478 return -EAGAIN;
479 }
480
481 /* Sleep until data is available */
482 wait_event_interruptible(dev->rx_wait,
483 (likely(!list_empty(&dev->rx_buffers))));
484 spin_lock_irqsave(&dev->lock, flags);
485 }
486
487 /* We have data to return then copy it to the caller's buffer.*/
488 while ((current_rx_bytes || likely(!list_empty(&dev->rx_buffers)))
489 && len) {
490 if (current_rx_bytes == 0) {
491 req = container_of(dev->rx_buffers.next,
492 struct usb_request, list);
493 list_del_init(&req->list);
494
495 if (req->actual && req->buf) {
496 current_rx_req = req;
497 current_rx_bytes = req->actual;
498 current_rx_buf = req->buf;
499 } else {
500 list_add(&req->list, &dev->rx_reqs);
501 continue;
502 }
503 }
504
505 /* Don't leave irqs off while doing memory copies */
506 spin_unlock_irqrestore(&dev->lock, flags);
507
508 if (len > current_rx_bytes)
509 size = current_rx_bytes;
510 else
511 size = len;
512
513 size -= copy_to_user(buf, current_rx_buf, size);
514 bytes_copied += size;
515 len -= size;
516 buf += size;
517
518 spin_lock_irqsave(&dev->lock, flags);
519
520 /* We've disconnected or reset so return. */
521 if (dev->reset_printer) {
522 list_add(&current_rx_req->list, &dev->rx_reqs);
523 spin_unlock_irqrestore(&dev->lock, flags);
524 mutex_unlock(&dev->lock_printer_io);
525 return -EAGAIN;
526 }
527
528 /* If we not returning all the data left in this RX request
529 * buffer then adjust the amount of data left in the buffer.
530 * Othewise if we are done with this RX request buffer then
531 * requeue it to get any incoming data from the USB host.
532 */
533 if (size < current_rx_bytes) {
534 current_rx_bytes -= size;
535 current_rx_buf += size;
536 } else {
537 list_add(&current_rx_req->list, &dev->rx_reqs);
538 current_rx_bytes = 0;
539 current_rx_buf = NULL;
540 current_rx_req = NULL;
541 }
542 }
543
544 dev->current_rx_req = current_rx_req;
545 dev->current_rx_bytes = current_rx_bytes;
546 dev->current_rx_buf = current_rx_buf;
547
548 spin_unlock_irqrestore(&dev->lock, flags);
549 mutex_unlock(&dev->lock_printer_io);
550
551 DBG(dev, "printer_read returned %d bytes\n", (int)bytes_copied);
552
553 if (bytes_copied)
554 return bytes_copied;
555 else
556 return -EAGAIN;
557}
558
559static ssize_t
560printer_write(struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
561{
562 struct printer_dev *dev = fd->private_data;
563 unsigned long flags;
564 size_t size; /* Amount of data in a TX request. */
565 size_t bytes_copied = 0;
566 struct usb_request *req;
567
568 DBG(dev, "printer_write trying to send %d bytes\n", (int)len);
569
570 if (len == 0)
571 return -EINVAL;
572
573 mutex_lock(&dev->lock_printer_io);
574 spin_lock_irqsave(&dev->lock, flags);
575
576 /* Check if a printer reset happens while we have interrupts on */
577 dev->reset_printer = 0;
578
579 /* Check if there is any available write buffers */
580 if (likely(list_empty(&dev->tx_reqs))) {
581 /* Turn interrupts back on before sleeping. */
582 spin_unlock_irqrestore(&dev->lock, flags);
583
584 /*
585 * If write buffers are available check if this is
586 * a NON-Blocking call or not.
587 */
588 if (fd->f_flags & (O_NONBLOCK|O_NDELAY)) {
589 mutex_unlock(&dev->lock_printer_io);
590 return -EAGAIN;
591 }
592
593 /* Sleep until a write buffer is available */
594 wait_event_interruptible(dev->tx_wait,
595 (likely(!list_empty(&dev->tx_reqs))));
596 spin_lock_irqsave(&dev->lock, flags);
597 }
598
599 while (likely(!list_empty(&dev->tx_reqs)) && len) {
600
601 if (len > USB_BUFSIZE)
602 size = USB_BUFSIZE;
603 else
604 size = len;
605
606 req = container_of(dev->tx_reqs.next, struct usb_request,
607 list);
608 list_del_init(&req->list);
609
610 req->complete = tx_complete;
611 req->length = size;
612
613 /* Check if we need to send a zero length packet. */
614 if (len > size)
615 /* They will be more TX requests so no yet. */
616 req->zero = 0;
617 else
618 /* If the data amount is not a multiple of the
619 * maxpacket size then send a zero length packet.
620 */
621 req->zero = ((len % dev->in_ep->maxpacket) == 0);
622
623 /* Don't leave irqs off while doing memory copies */
624 spin_unlock_irqrestore(&dev->lock, flags);
625
626 if (copy_from_user(req->buf, buf, size)) {
627 list_add(&req->list, &dev->tx_reqs);
628 mutex_unlock(&dev->lock_printer_io);
629 return bytes_copied;
630 }
631
632 bytes_copied += size;
633 len -= size;
634 buf += size;
635
636 spin_lock_irqsave(&dev->lock, flags);
637
638 /* We've disconnected or reset so free the req and buffer */
639 if (dev->reset_printer) {
640 list_add(&req->list, &dev->tx_reqs);
641 spin_unlock_irqrestore(&dev->lock, flags);
642 mutex_unlock(&dev->lock_printer_io);
643 return -EAGAIN;
644 }
645
646 if (usb_ep_queue(dev->in_ep, req, GFP_ATOMIC)) {
647 list_add(&req->list, &dev->tx_reqs);
648 spin_unlock_irqrestore(&dev->lock, flags);
649 mutex_unlock(&dev->lock_printer_io);
650 return -EAGAIN;
651 }
652
653 list_add(&req->list, &dev->tx_reqs_active);
654
655 }
656
657 spin_unlock_irqrestore(&dev->lock, flags);
658 mutex_unlock(&dev->lock_printer_io);
659
660 DBG(dev, "printer_write sent %d bytes\n", (int)bytes_copied);
661
662 if (bytes_copied)
663 return bytes_copied;
664 else
665 return -EAGAIN;
666}
667
668static int
669printer_fsync(struct file *fd, loff_t start, loff_t end, int datasync)
670{
671 struct printer_dev *dev = fd->private_data;
672 struct inode *inode = file_inode(fd);
673 unsigned long flags;
674 int tx_list_empty;
675
676 mutex_lock(&inode->i_mutex);
677 spin_lock_irqsave(&dev->lock, flags);
678 tx_list_empty = (likely(list_empty(&dev->tx_reqs)));
679 spin_unlock_irqrestore(&dev->lock, flags);
680
681 if (!tx_list_empty) {
682 /* Sleep until all data has been sent */
683 wait_event_interruptible(dev->tx_flush_wait,
684 (likely(list_empty(&dev->tx_reqs_active))));
685 }
686 mutex_unlock(&inode->i_mutex);
687
688 return 0;
689}
690
691static unsigned int
692printer_poll(struct file *fd, poll_table *wait)
693{
694 struct printer_dev *dev = fd->private_data;
695 unsigned long flags;
696 int status = 0;
697
698 mutex_lock(&dev->lock_printer_io);
699 spin_lock_irqsave(&dev->lock, flags);
700 setup_rx_reqs(dev);
701 spin_unlock_irqrestore(&dev->lock, flags);
702 mutex_unlock(&dev->lock_printer_io);
703
704 poll_wait(fd, &dev->rx_wait, wait);
705 poll_wait(fd, &dev->tx_wait, wait);
706
707 spin_lock_irqsave(&dev->lock, flags);
708 if (likely(!list_empty(&dev->tx_reqs)))
709 status |= POLLOUT | POLLWRNORM;
710
711 if (likely(dev->current_rx_bytes) ||
712 likely(!list_empty(&dev->rx_buffers)))
713 status |= POLLIN | POLLRDNORM;
714
715 spin_unlock_irqrestore(&dev->lock, flags);
716
717 return status;
718}
719
720static long
721printer_ioctl(struct file *fd, unsigned int code, unsigned long arg)
722{
723 struct printer_dev *dev = fd->private_data;
724 unsigned long flags;
725 int status = 0;
726
727 DBG(dev, "printer_ioctl: cmd=0x%4.4x, arg=%lu\n", code, arg);
728
729 /* handle ioctls */
730
731 spin_lock_irqsave(&dev->lock, flags);
732
733 switch (code) {
734 case GADGET_GET_PRINTER_STATUS:
735 status = (int)dev->printer_status;
736 break;
737 case GADGET_SET_PRINTER_STATUS:
738 dev->printer_status = (u8)arg;
739 break;
740 default:
741 /* could not handle ioctl */
742 DBG(dev, "printer_ioctl: ERROR cmd=0x%4.4xis not supported\n",
743 code);
744 status = -ENOTTY;
745 }
746
747 spin_unlock_irqrestore(&dev->lock, flags);
748
749 return status;
750}
751
752/* used after endpoint configuration */
753static const struct file_operations printer_io_operations = {
754 .owner = THIS_MODULE,
755 .open = printer_open,
756 .read = printer_read,
757 .write = printer_write,
758 .fsync = printer_fsync,
759 .poll = printer_poll,
760 .unlocked_ioctl = printer_ioctl,
761 .release = printer_close,
762 .llseek = noop_llseek,
763};
764
765/*-------------------------------------------------------------------------*/
766
767static int
768set_printer_interface(struct printer_dev *dev)
769{
770 int result = 0;
771
772 dev->in_ep->desc = ep_desc(dev->gadget, &fs_ep_in_desc, &hs_ep_in_desc,
773 &ss_ep_in_desc);
774 dev->in_ep->driver_data = dev;
775
776 dev->out_ep->desc = ep_desc(dev->gadget, &fs_ep_out_desc,
777 &hs_ep_out_desc, &ss_ep_out_desc);
778 dev->out_ep->driver_data = dev;
779
780 result = usb_ep_enable(dev->in_ep);
781 if (result != 0) {
782 DBG(dev, "enable %s --> %d\n", dev->in_ep->name, result);
783 goto done;
784 }
785
786 result = usb_ep_enable(dev->out_ep);
787 if (result != 0) {
788 DBG(dev, "enable %s --> %d\n", dev->in_ep->name, result);
789 goto done;
790 }
791
792done:
793 /* on error, disable any endpoints */
794 if (result != 0) {
795 (void) usb_ep_disable(dev->in_ep);
796 (void) usb_ep_disable(dev->out_ep);
797 dev->in_ep->desc = NULL;
798 dev->out_ep->desc = NULL;
799 }
800
801 /* caller is responsible for cleanup on error */
802 return result;
803}
804
805static void printer_reset_interface(struct printer_dev *dev)
806{
807 if (dev->interface < 0)
808 return;
809
810 DBG(dev, "%s\n", __func__);
811
812 if (dev->in_ep->desc)
813 usb_ep_disable(dev->in_ep);
814
815 if (dev->out_ep->desc)
816 usb_ep_disable(dev->out_ep);
817
818 dev->in_ep->desc = NULL;
819 dev->out_ep->desc = NULL;
820 dev->interface = -1;
821}
822
823/* Change our operational Interface. */
824static int set_interface(struct printer_dev *dev, unsigned number)
825{
826 int result = 0;
827
828 /* Free the current interface */
829 printer_reset_interface(dev);
830
831 result = set_printer_interface(dev);
832 if (result)
833 printer_reset_interface(dev);
834 else
835 dev->interface = number;
836
837 if (!result)
838 INFO(dev, "Using interface %x\n", number);
839
840 return result;
841}
842
843static void printer_soft_reset(struct printer_dev *dev)
844{
845 struct usb_request *req;
846
847 INFO(dev, "Received Printer Reset Request\n");
848
849 if (usb_ep_disable(dev->in_ep))
850 DBG(dev, "Failed to disable USB in_ep\n");
851 if (usb_ep_disable(dev->out_ep))
852 DBG(dev, "Failed to disable USB out_ep\n");
853
854 if (dev->current_rx_req != NULL) {
855 list_add(&dev->current_rx_req->list, &dev->rx_reqs);
856 dev->current_rx_req = NULL;
857 }
858 dev->current_rx_bytes = 0;
859 dev->current_rx_buf = NULL;
860 dev->reset_printer = 1;
861
862 while (likely(!(list_empty(&dev->rx_buffers)))) {
863 req = container_of(dev->rx_buffers.next, struct usb_request,
864 list);
865 list_del_init(&req->list);
866 list_add(&req->list, &dev->rx_reqs);
867 }
868
869 while (likely(!(list_empty(&dev->rx_reqs_active)))) {
870 req = container_of(dev->rx_buffers.next, struct usb_request,
871 list);
872 list_del_init(&req->list);
873 list_add(&req->list, &dev->rx_reqs);
874 }
875
876 while (likely(!(list_empty(&dev->tx_reqs_active)))) {
877 req = container_of(dev->tx_reqs_active.next,
878 struct usb_request, list);
879 list_del_init(&req->list);
880 list_add(&req->list, &dev->tx_reqs);
881 }
882
883 if (usb_ep_enable(dev->in_ep))
884 DBG(dev, "Failed to enable USB in_ep\n");
885 if (usb_ep_enable(dev->out_ep))
886 DBG(dev, "Failed to enable USB out_ep\n");
887
888 wake_up_interruptible(&dev->rx_wait);
889 wake_up_interruptible(&dev->tx_wait);
890 wake_up_interruptible(&dev->tx_flush_wait);
891}
892
893/*-------------------------------------------------------------------------*/
894
895static bool gprinter_req_match(struct usb_function *f,
896 const struct usb_ctrlrequest *ctrl)
897{
898 struct printer_dev *dev = func_to_printer(f);
899 u16 w_index = le16_to_cpu(ctrl->wIndex);
900 u16 w_value = le16_to_cpu(ctrl->wValue);
901 u16 w_length = le16_to_cpu(ctrl->wLength);
902
903 if ((ctrl->bRequestType & USB_RECIP_MASK) != USB_RECIP_INTERFACE ||
904 (ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS)
905 return false;
906
907 switch (ctrl->bRequest) {
908 case GET_DEVICE_ID:
909 w_index >>= 8;
910 if (w_length <= PNP_STRING_LEN &&
911 (USB_DIR_IN & ctrl->bRequestType))
912 break;
913 return false;
914 case GET_PORT_STATUS:
915 if (!w_value && w_length == 1 &&
916 (USB_DIR_IN & ctrl->bRequestType))
917 break;
918 return false;
919 case SOFT_RESET:
920 if (!w_value && !w_length &&
921 !(USB_DIR_IN & ctrl->bRequestType))
922 break;
923 /* fall through */
924 default:
925 return false;
926 }
927 return w_index == dev->interface;
928}
929
930/*
931 * The setup() callback implements all the ep0 functionality that's not
932 * handled lower down.
933 */
934static int printer_func_setup(struct usb_function *f,
935 const struct usb_ctrlrequest *ctrl)
936{
937 struct printer_dev *dev = func_to_printer(f);
938 struct usb_composite_dev *cdev = f->config->cdev;
939 struct usb_request *req = cdev->req;
940 int value = -EOPNOTSUPP;
941 u16 wIndex = le16_to_cpu(ctrl->wIndex);
942 u16 wValue = le16_to_cpu(ctrl->wValue);
943 u16 wLength = le16_to_cpu(ctrl->wLength);
944
945 DBG(dev, "ctrl req%02x.%02x v%04x i%04x l%d\n",
946 ctrl->bRequestType, ctrl->bRequest, wValue, wIndex, wLength);
947
948 switch (ctrl->bRequestType&USB_TYPE_MASK) {
949 case USB_TYPE_CLASS:
950 switch (ctrl->bRequest) {
951 case GET_DEVICE_ID: /* Get the IEEE-1284 PNP String */
952 /* Only one printer interface is supported. */
953 if ((wIndex>>8) != dev->interface)
954 break;
955
956 value = (dev->pnp_string[0] << 8) | dev->pnp_string[1];
957 memcpy(req->buf, dev->pnp_string, value);
958 DBG(dev, "1284 PNP String: %x %s\n", value,
959 &dev->pnp_string[2]);
960 break;
961
962 case GET_PORT_STATUS: /* Get Port Status */
963 /* Only one printer interface is supported. */
964 if (wIndex != dev->interface)
965 break;
966
967 *(u8 *)req->buf = dev->printer_status;
968 value = min_t(u16, wLength, 1);
969 break;
970
971 case SOFT_RESET: /* Soft Reset */
972 /* Only one printer interface is supported. */
973 if (wIndex != dev->interface)
974 break;
975
976 printer_soft_reset(dev);
977
978 value = 0;
979 break;
980
981 default:
982 goto unknown;
983 }
984 break;
985
986 default:
987unknown:
988 VDBG(dev,
989 "unknown ctrl req%02x.%02x v%04x i%04x l%d\n",
990 ctrl->bRequestType, ctrl->bRequest,
991 wValue, wIndex, wLength);
992 break;
993 }
994 /* host either stalls (value < 0) or reports success */
995 if (value >= 0) {
996 req->length = value;
997 req->zero = value < wLength;
998 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
999 if (value < 0) {
1000 ERROR(dev, "%s:%d Error!\n", __func__, __LINE__);
1001 req->status = 0;
1002 }
1003 }
1004 return value;
1005}
1006
1007static int printer_func_bind(struct usb_configuration *c,
1008 struct usb_function *f)
1009{
1010 struct usb_gadget *gadget = c->cdev->gadget;
1011 struct printer_dev *dev = func_to_printer(f);
1012 struct device *pdev;
1013 struct usb_composite_dev *cdev = c->cdev;
1014 struct usb_ep *in_ep;
1015 struct usb_ep *out_ep = NULL;
1016 struct usb_request *req;
1017 dev_t devt;
1018 int id;
1019 int ret;
1020 u32 i;
1021
1022 id = usb_interface_id(c, f);
1023 if (id < 0)
1024 return id;
1025 intf_desc.bInterfaceNumber = id;
1026
1027 /* finish hookup to lower layer ... */
1028 dev->gadget = gadget;
1029
1030 /* all we really need is bulk IN/OUT */
1031 in_ep = usb_ep_autoconfig(cdev->gadget, &fs_ep_in_desc);
1032 if (!in_ep) {
1033autoconf_fail:
1034 dev_err(&cdev->gadget->dev, "can't autoconfigure on %s\n",
1035 cdev->gadget->name);
1036 return -ENODEV;
1037 }
1038 in_ep->driver_data = in_ep; /* claim */
1039
1040 out_ep = usb_ep_autoconfig(cdev->gadget, &fs_ep_out_desc);
1041 if (!out_ep)
1042 goto autoconf_fail;
1043 out_ep->driver_data = out_ep; /* claim */
1044
1045 /* assumes that all endpoints are dual-speed */
1046 hs_ep_in_desc.bEndpointAddress = fs_ep_in_desc.bEndpointAddress;
1047 hs_ep_out_desc.bEndpointAddress = fs_ep_out_desc.bEndpointAddress;
1048 ss_ep_in_desc.bEndpointAddress = fs_ep_in_desc.bEndpointAddress;
1049 ss_ep_out_desc.bEndpointAddress = fs_ep_out_desc.bEndpointAddress;
1050
1051 ret = usb_assign_descriptors(f, fs_printer_function,
1052 hs_printer_function, ss_printer_function);
1053 if (ret)
1054 return ret;
1055
1056 dev->in_ep = in_ep;
1057 dev->out_ep = out_ep;
1058
1059 ret = -ENOMEM;
1060 for (i = 0; i < dev->q_len; i++) {
1061 req = printer_req_alloc(dev->in_ep, USB_BUFSIZE, GFP_KERNEL);
1062 if (!req)
1063 goto fail_tx_reqs;
1064 list_add(&req->list, &dev->tx_reqs);
1065 }
1066
1067 for (i = 0; i < dev->q_len; i++) {
1068 req = printer_req_alloc(dev->out_ep, USB_BUFSIZE, GFP_KERNEL);
1069 if (!req)
1070 goto fail_rx_reqs;
1071 list_add(&req->list, &dev->rx_reqs);
1072 }
1073
1074 /* Setup the sysfs files for the printer gadget. */
1075 devt = MKDEV(major, dev->minor);
1076 pdev = device_create(usb_gadget_class, NULL, devt,
1077 NULL, "g_printer%d", dev->minor);
1078 if (IS_ERR(pdev)) {
1079 ERROR(dev, "Failed to create device: g_printer\n");
1080 ret = PTR_ERR(pdev);
1081 goto fail_rx_reqs;
1082 }
1083
1084 /*
1085 * Register a character device as an interface to a user mode
1086 * program that handles the printer specific functionality.
1087 */
1088 cdev_init(&dev->printer_cdev, &printer_io_operations);
1089 dev->printer_cdev.owner = THIS_MODULE;
1090 ret = cdev_add(&dev->printer_cdev, devt, 1);
1091 if (ret) {
1092 ERROR(dev, "Failed to open char device\n");
1093 goto fail_cdev_add;
1094 }
1095
1096 return 0;
1097
1098fail_cdev_add:
1099 device_destroy(usb_gadget_class, devt);
1100
1101fail_rx_reqs:
1102 while (!list_empty(&dev->rx_reqs)) {
1103 req = container_of(dev->rx_reqs.next, struct usb_request, list);
1104 list_del(&req->list);
1105 printer_req_free(dev->out_ep, req);
1106 }
1107
1108fail_tx_reqs:
1109 while (!list_empty(&dev->tx_reqs)) {
1110 req = container_of(dev->tx_reqs.next, struct usb_request, list);
1111 list_del(&req->list);
1112 printer_req_free(dev->in_ep, req);
1113 }
1114
1115 return ret;
1116
1117}
1118
1119static int printer_func_set_alt(struct usb_function *f,
1120 unsigned intf, unsigned alt)
1121{
1122 struct printer_dev *dev = func_to_printer(f);
1123 int ret = -ENOTSUPP;
1124
1125 if (!alt)
1126 ret = set_interface(dev, intf);
1127
1128 return ret;
1129}
1130
1131static void printer_func_disable(struct usb_function *f)
1132{
1133 struct printer_dev *dev = func_to_printer(f);
1134 unsigned long flags;
1135
1136 DBG(dev, "%s\n", __func__);
1137
1138 spin_lock_irqsave(&dev->lock, flags);
1139 printer_reset_interface(dev);
1140 spin_unlock_irqrestore(&dev->lock, flags);
1141}
1142
1143static inline struct f_printer_opts
1144*to_f_printer_opts(struct config_item *item)
1145{
1146 return container_of(to_config_group(item), struct f_printer_opts,
1147 func_inst.group);
1148}
1149
1150CONFIGFS_ATTR_STRUCT(f_printer_opts);
1151CONFIGFS_ATTR_OPS(f_printer_opts);
1152
1153static void printer_attr_release(struct config_item *item)
1154{
1155 struct f_printer_opts *opts = to_f_printer_opts(item);
1156
1157 usb_put_function_instance(&opts->func_inst);
1158}
1159
1160static struct configfs_item_operations printer_item_ops = {
1161 .release = printer_attr_release,
1162 .show_attribute = f_printer_opts_attr_show,
1163 .store_attribute = f_printer_opts_attr_store,
1164};
1165
1166static ssize_t f_printer_opts_pnp_string_show(struct f_printer_opts *opts,
1167 char *page)
1168{
1169 int result;
1170
1171 mutex_lock(&opts->lock);
1172 result = strlcpy(page, opts->pnp_string + 2, PNP_STRING_LEN - 2);
1173 mutex_unlock(&opts->lock);
1174
1175 return result;
1176}
1177
1178static ssize_t f_printer_opts_pnp_string_store(struct f_printer_opts *opts,
1179 const char *page, size_t len)
1180{
1181 int result, l;
1182
1183 mutex_lock(&opts->lock);
1184 result = strlcpy(opts->pnp_string + 2, page, PNP_STRING_LEN - 2);
1185 l = strlen(opts->pnp_string + 2) + 2;
1186 opts->pnp_string[0] = (l >> 8) & 0xFF;
1187 opts->pnp_string[1] = l & 0xFF;
1188 mutex_unlock(&opts->lock);
1189
1190 return result;
1191}
1192
1193static struct f_printer_opts_attribute f_printer_opts_pnp_string =
1194 __CONFIGFS_ATTR(pnp_string, S_IRUGO | S_IWUSR,
1195 f_printer_opts_pnp_string_show,
1196 f_printer_opts_pnp_string_store);
1197
1198static ssize_t f_printer_opts_q_len_show(struct f_printer_opts *opts,
1199 char *page)
1200{
1201 int result;
1202
1203 mutex_lock(&opts->lock);
1204 result = sprintf(page, "%d\n", opts->q_len);
1205 mutex_unlock(&opts->lock);
1206
1207 return result;
1208}
1209
1210static ssize_t f_printer_opts_q_len_store(struct f_printer_opts *opts,
1211 const char *page, size_t len)
1212{
1213 int ret;
1214 u16 num;
1215
1216 mutex_lock(&opts->lock);
1217 if (opts->refcnt) {
1218 ret = -EBUSY;
1219 goto end;
1220 }
1221
1222 ret = kstrtou16(page, 0, &num);
1223 if (ret)
1224 goto end;
1225
1226 opts->q_len = (unsigned)num;
1227 ret = len;
1228end:
1229 mutex_unlock(&opts->lock);
1230 return ret;
1231}
1232
1233static struct f_printer_opts_attribute f_printer_opts_q_len =
1234 __CONFIGFS_ATTR(q_len, S_IRUGO | S_IWUSR, f_printer_opts_q_len_show,
1235 f_printer_opts_q_len_store);
1236
1237static struct configfs_attribute *printer_attrs[] = {
1238 &f_printer_opts_pnp_string.attr,
1239 &f_printer_opts_q_len.attr,
1240 NULL,
1241};
1242
1243static struct config_item_type printer_func_type = {
1244 .ct_item_ops = &printer_item_ops,
1245 .ct_attrs = printer_attrs,
1246 .ct_owner = THIS_MODULE,
1247};
1248
1249static inline int gprinter_get_minor(void)
1250{
1251 return ida_simple_get(&printer_ida, 0, 0, GFP_KERNEL);
1252}
1253
1254static inline void gprinter_put_minor(int minor)
1255{
1256 ida_simple_remove(&printer_ida, minor);
1257}
1258
1259static int gprinter_setup(int);
1260static void gprinter_cleanup(void);
1261
1262static void gprinter_free_inst(struct usb_function_instance *f)
1263{
1264 struct f_printer_opts *opts;
1265
1266 opts = container_of(f, struct f_printer_opts, func_inst);
1267
1268 mutex_lock(&printer_ida_lock);
1269
1270 gprinter_put_minor(opts->minor);
1271 if (idr_is_empty(&printer_ida.idr))
1272 gprinter_cleanup();
1273
1274 mutex_unlock(&printer_ida_lock);
1275
1276 kfree(opts);
1277}
1278
1279static struct usb_function_instance *gprinter_alloc_inst(void)
1280{
1281 struct f_printer_opts *opts;
1282 struct usb_function_instance *ret;
1283 int status = 0;
1284
1285 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
1286 if (!opts)
1287 return ERR_PTR(-ENOMEM);
1288
1289 mutex_init(&opts->lock);
1290 opts->func_inst.free_func_inst = gprinter_free_inst;
1291 ret = &opts->func_inst;
1292
1293 mutex_lock(&printer_ida_lock);
1294
1295 if (idr_is_empty(&printer_ida.idr)) {
1296 status = gprinter_setup(PRINTER_MINORS);
1297 if (status) {
1298 ret = ERR_PTR(status);
1299 kfree(opts);
1300 goto unlock;
1301 }
1302 }
1303
1304 opts->minor = gprinter_get_minor();
1305 if (opts->minor < 0) {
1306 ret = ERR_PTR(opts->minor);
1307 kfree(opts);
1308 if (idr_is_empty(&printer_ida.idr))
1309 gprinter_cleanup();
1310 goto unlock;
1311 }
1312 config_group_init_type_name(&opts->func_inst.group, "",
1313 &printer_func_type);
1314
1315unlock:
1316 mutex_unlock(&printer_ida_lock);
1317 return ret;
1318}
1319
1320static void gprinter_free(struct usb_function *f)
1321{
1322 struct printer_dev *dev = func_to_printer(f);
1323 struct f_printer_opts *opts;
1324
1325 opts = container_of(f->fi, struct f_printer_opts, func_inst);
1326 kfree(dev);
1327 mutex_lock(&opts->lock);
1328 --opts->refcnt;
1329 mutex_unlock(&opts->lock);
1330}
1331
1332static void printer_func_unbind(struct usb_configuration *c,
1333 struct usb_function *f)
1334{
1335 struct printer_dev *dev;
1336 struct usb_request *req;
1337
1338 dev = func_to_printer(f);
1339
1340 device_destroy(usb_gadget_class, MKDEV(major, dev->minor));
1341
1342 /* Remove Character Device */
1343 cdev_del(&dev->printer_cdev);
1344
1345 /* we must already have been disconnected ... no i/o may be active */
1346 WARN_ON(!list_empty(&dev->tx_reqs_active));
1347 WARN_ON(!list_empty(&dev->rx_reqs_active));
1348
1349 /* Free all memory for this driver. */
1350 while (!list_empty(&dev->tx_reqs)) {
1351 req = container_of(dev->tx_reqs.next, struct usb_request,
1352 list);
1353 list_del(&req->list);
1354 printer_req_free(dev->in_ep, req);
1355 }
1356
1357 if (dev->current_rx_req != NULL)
1358 printer_req_free(dev->out_ep, dev->current_rx_req);
1359
1360 while (!list_empty(&dev->rx_reqs)) {
1361 req = container_of(dev->rx_reqs.next,
1362 struct usb_request, list);
1363 list_del(&req->list);
1364 printer_req_free(dev->out_ep, req);
1365 }
1366
1367 while (!list_empty(&dev->rx_buffers)) {
1368 req = container_of(dev->rx_buffers.next,
1369 struct usb_request, list);
1370 list_del(&req->list);
1371 printer_req_free(dev->out_ep, req);
1372 }
1373 usb_free_all_descriptors(f);
1374}
1375
1376static struct usb_function *gprinter_alloc(struct usb_function_instance *fi)
1377{
1378 struct printer_dev *dev;
1379 struct f_printer_opts *opts;
1380
1381 opts = container_of(fi, struct f_printer_opts, func_inst);
1382
1383 mutex_lock(&opts->lock);
1384 if (opts->minor >= minors) {
1385 mutex_unlock(&opts->lock);
1386 return ERR_PTR(-ENOENT);
1387 }
1388
1389 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1390 if (!dev) {
1391 mutex_unlock(&opts->lock);
1392 return ERR_PTR(-ENOMEM);
1393 }
1394
1395 ++opts->refcnt;
1396 dev->minor = opts->minor;
1397 dev->pnp_string = opts->pnp_string;
1398 dev->q_len = opts->q_len;
1399 mutex_unlock(&opts->lock);
1400
1401 dev->function.name = "printer";
1402 dev->function.bind = printer_func_bind;
1403 dev->function.setup = printer_func_setup;
1404 dev->function.unbind = printer_func_unbind;
1405 dev->function.set_alt = printer_func_set_alt;
1406 dev->function.disable = printer_func_disable;
1407 dev->function.req_match = gprinter_req_match;
1408 dev->function.free_func = gprinter_free;
1409
1410 INIT_LIST_HEAD(&dev->tx_reqs);
1411 INIT_LIST_HEAD(&dev->rx_reqs);
1412 INIT_LIST_HEAD(&dev->rx_buffers);
1413 INIT_LIST_HEAD(&dev->tx_reqs_active);
1414 INIT_LIST_HEAD(&dev->rx_reqs_active);
1415
1416 spin_lock_init(&dev->lock);
1417 mutex_init(&dev->lock_printer_io);
1418 init_waitqueue_head(&dev->rx_wait);
1419 init_waitqueue_head(&dev->tx_wait);
1420 init_waitqueue_head(&dev->tx_flush_wait);
1421
1422 dev->interface = -1;
1423 dev->printer_cdev_open = 0;
1424 dev->printer_status = PRINTER_NOT_ERROR;
1425 dev->current_rx_req = NULL;
1426 dev->current_rx_bytes = 0;
1427 dev->current_rx_buf = NULL;
1428
1429 return &dev->function;
1430}
1431
1432DECLARE_USB_FUNCTION_INIT(printer, gprinter_alloc_inst, gprinter_alloc);
1433MODULE_LICENSE("GPL");
1434MODULE_AUTHOR("Craig Nadler");
1435
1436static int gprinter_setup(int count)
1437{
1438 int status;
1439 dev_t devt;
1440
1441 usb_gadget_class = class_create(THIS_MODULE, "usb_printer_gadget");
1442 if (IS_ERR(usb_gadget_class)) {
1443 status = PTR_ERR(usb_gadget_class);
1444 usb_gadget_class = NULL;
1445 pr_err("unable to create usb_gadget class %d\n", status);
1446 return status;
1447 }
1448
1449 status = alloc_chrdev_region(&devt, 0, count, "USB printer gadget");
1450 if (status) {
1451 pr_err("alloc_chrdev_region %d\n", status);
1452 class_destroy(usb_gadget_class);
1453 usb_gadget_class = NULL;
1454 return status;
1455 }
1456
1457 major = MAJOR(devt);
1458 minors = count;
1459
1460 return status;
1461}
1462
1463static void gprinter_cleanup(void)
1464{
1465 if (major) {
1466 unregister_chrdev_region(MKDEV(major, 0), minors);
1467 major = minors = 0;
1468 }
1469 class_destroy(usb_gadget_class);
1470 usb_gadget_class = NULL;
1471}
diff --git a/drivers/usb/gadget/function/u_printer.h b/drivers/usb/gadget/function/u_printer.h
new file mode 100644
index 000000000000..0e2c49d4274e
--- /dev/null
+++ b/drivers/usb/gadget/function/u_printer.h
@@ -0,0 +1,37 @@
1/*
2 * u_printer.h
3 *
4 * Utility definitions for the printer function
5 *
6 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
7 * http://www.samsung.com
8 *
9 * Author: Andrzej Pietrasiewicz <andrzej.p@samsung.com>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
14 */
15
16#ifndef U_PRINTER_H
17#define U_PRINTER_H
18
19#include <linux/usb/composite.h>
20
21#define PNP_STRING_LEN 1024
22
23struct f_printer_opts {
24 struct usb_function_instance func_inst;
25 int minor;
26 char pnp_string[PNP_STRING_LEN];
27 unsigned q_len;
28
29 /*
30 * Protect the data from concurrent access by read/write
31 * and create symlink/remove symlink
32 */
33 struct mutex lock;
34 int refcnt;
35};
36
37#endif /* U_PRINTER_H */
diff --git a/drivers/usb/gadget/function/u_serial.c b/drivers/usb/gadget/function/u_serial.c
index 491082aaf103..89179ab20c10 100644
--- a/drivers/usb/gadget/function/u_serial.c
+++ b/drivers/usb/gadget/function/u_serial.c
@@ -912,7 +912,7 @@ static int gs_put_char(struct tty_struct *tty, unsigned char ch)
912 unsigned long flags; 912 unsigned long flags;
913 int status; 913 int status;
914 914
915 pr_vdebug("gs_put_char: (%d,%p) char=0x%x, called from %pf\n", 915 pr_vdebug("gs_put_char: (%d,%p) char=0x%x, called from %ps\n",
916 port->port_num, tty, ch, __builtin_return_address(0)); 916 port->port_num, tty, ch, __builtin_return_address(0));
917 917
918 spin_lock_irqsave(&port->port_lock, flags); 918 spin_lock_irqsave(&port->port_lock, flags);
diff --git a/drivers/usb/gadget/legacy/Kconfig b/drivers/usb/gadget/legacy/Kconfig
index 113c87e22117..d5a7102de696 100644
--- a/drivers/usb/gadget/legacy/Kconfig
+++ b/drivers/usb/gadget/legacy/Kconfig
@@ -301,6 +301,7 @@ config USB_MIDI_GADGET
301config USB_G_PRINTER 301config USB_G_PRINTER
302 tristate "Printer Gadget" 302 tristate "Printer Gadget"
303 select USB_LIBCOMPOSITE 303 select USB_LIBCOMPOSITE
304 select USB_F_PRINTER
304 help 305 help
305 The Printer Gadget channels data between the USB host and a 306 The Printer Gadget channels data between the USB host and a
306 userspace program driving the print engine. The user space 307 userspace program driving the print engine. The user space
diff --git a/drivers/usb/gadget/legacy/printer.c b/drivers/usb/gadget/legacy/printer.c
index 90545980542f..d5b6ee725a2a 100644
--- a/drivers/usb/gadget/legacy/printer.c
+++ b/drivers/usb/gadget/legacy/printer.c
@@ -12,29 +12,7 @@
12 12
13#include <linux/module.h> 13#include <linux/module.h>
14#include <linux/kernel.h> 14#include <linux/kernel.h>
15#include <linux/delay.h>
16#include <linux/ioport.h>
17#include <linux/sched.h>
18#include <linux/slab.h>
19#include <linux/mutex.h>
20#include <linux/errno.h>
21#include <linux/init.h>
22#include <linux/timer.h>
23#include <linux/list.h>
24#include <linux/interrupt.h>
25#include <linux/device.h>
26#include <linux/moduleparam.h>
27#include <linux/fs.h>
28#include <linux/poll.h>
29#include <linux/types.h>
30#include <linux/ctype.h>
31#include <linux/cdev.h>
32
33#include <asm/byteorder.h> 15#include <asm/byteorder.h>
34#include <linux/io.h>
35#include <linux/irq.h>
36#include <linux/uaccess.h>
37#include <asm/unaligned.h>
38 16
39#include <linux/usb/ch9.h> 17#include <linux/usb/ch9.h>
40#include <linux/usb/composite.h> 18#include <linux/usb/composite.h>
@@ -46,50 +24,12 @@
46USB_GADGET_COMPOSITE_OPTIONS(); 24USB_GADGET_COMPOSITE_OPTIONS();
47 25
48#define DRIVER_DESC "Printer Gadget" 26#define DRIVER_DESC "Printer Gadget"
49#define DRIVER_VERSION "2007 OCT 06" 27#define DRIVER_VERSION "2015 FEB 17"
50 28
51static DEFINE_MUTEX(printer_mutex);
52static const char shortname [] = "printer"; 29static const char shortname [] = "printer";
53static const char driver_desc [] = DRIVER_DESC; 30static const char driver_desc [] = DRIVER_DESC;
54 31
55static dev_t g_printer_devno; 32#include "u_printer.h"
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 */
64 struct mutex lock_printer_io;
65 struct usb_gadget *gadget;
66 s8 interface;
67 struct usb_ep *in_ep, *out_ep;
68
69 struct list_head rx_reqs; /* List of free RX structs */
70 struct list_head rx_reqs_active; /* List of Active RX xfers */
71 struct list_head rx_buffers; /* List of completed xfers */
72 /* wait until there is data to be read. */
73 wait_queue_head_t rx_wait;
74 struct list_head tx_reqs; /* List of free TX structs */
75 struct list_head tx_reqs_active; /* List of Active TX xfers */
76 /* Wait until there are write buffers available to use. */
77 wait_queue_head_t tx_wait;
78 /* Wait until all write buffers have been sent. */
79 wait_queue_head_t tx_flush_wait;
80 struct usb_request *current_rx_req;
81 size_t current_rx_bytes;
82 u8 *current_rx_buf;
83 u8 printer_status;
84 u8 reset_printer;
85 struct cdev printer_cdev;
86 struct device *pdev;
87 u8 printer_cdev_open;
88 wait_queue_head_t wait;
89 struct usb_function function;
90};
91
92static struct printer_dev usb_printer_gadget;
93 33
94/*-------------------------------------------------------------------------*/ 34/*-------------------------------------------------------------------------*/
95 35
@@ -120,6 +60,9 @@ module_param(qlen, uint, S_IRUGO|S_IWUSR);
120 60
121#define QLEN qlen 61#define QLEN qlen
122 62
63static struct usb_function_instance *fi_printer;
64static struct usb_function *f_printer;
65
123/*-------------------------------------------------------------------------*/ 66/*-------------------------------------------------------------------------*/
124 67
125/* 68/*
@@ -127,10 +70,6 @@ module_param(qlen, uint, S_IRUGO|S_IWUSR);
127 * descriptors are built on demand. 70 * descriptors are built on demand.
128 */ 71 */
129 72
130/* holds our biggest descriptor */
131#define USB_DESC_BUFSIZE 256
132#define USB_BUFSIZE 8192
133
134static struct usb_device_descriptor device_desc = { 73static struct usb_device_descriptor device_desc = {
135 .bLength = sizeof device_desc, 74 .bLength = sizeof device_desc,
136 .bDescriptorType = USB_DT_DEVICE, 75 .bDescriptorType = USB_DT_DEVICE,
@@ -143,108 +82,6 @@ static struct usb_device_descriptor device_desc = {
143 .bNumConfigurations = 1 82 .bNumConfigurations = 1
144}; 83};
145 84
146static struct usb_interface_descriptor intf_desc = {
147 .bLength = sizeof intf_desc,
148 .bDescriptorType = USB_DT_INTERFACE,
149 .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
170static struct usb_descriptor_header *fs_printer_function[] = {
171 (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
177/*
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,
186 .wMaxPacketSize = cpu_to_le16(512)
187};
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,
193 .wMaxPacketSize = cpu_to_le16(512)
194};
195
196static struct usb_qualifier_descriptor dev_qualifier = {
197 .bLength = sizeof dev_qualifier,
198 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
199 .bcdUSB = cpu_to_le16(0x0200),
200 .bDeviceClass = USB_CLASS_PRINTER,
201 .bNumConfigurations = 1
202};
203
204static struct usb_descriptor_header *hs_printer_function[] = {
205 (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
211/*
212 * Added endpoint descriptors for 3.0 devices
213 */
214
215static struct usb_endpoint_descriptor ss_ep_in_desc = {
216 .bLength = USB_DT_ENDPOINT_SIZE,
217 .bDescriptorType = USB_DT_ENDPOINT,
218 .bmAttributes = USB_ENDPOINT_XFER_BULK,
219 .wMaxPacketSize = cpu_to_le16(1024),
220};
221
222static struct usb_ss_ep_comp_descriptor ss_ep_in_comp_desc = {
223 .bLength = sizeof(ss_ep_in_comp_desc),
224 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
225};
226
227static struct usb_endpoint_descriptor ss_ep_out_desc = {
228 .bLength = USB_DT_ENDPOINT_SIZE,
229 .bDescriptorType = USB_DT_ENDPOINT,
230 .bmAttributes = USB_ENDPOINT_XFER_BULK,
231 .wMaxPacketSize = cpu_to_le16(1024),
232};
233
234static struct usb_ss_ep_comp_descriptor ss_ep_out_comp_desc = {
235 .bLength = sizeof(ss_ep_out_comp_desc),
236 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
237};
238
239static struct usb_descriptor_header *ss_printer_function[] = {
240 (struct usb_descriptor_header *) &intf_desc,
241 (struct usb_descriptor_header *) &ss_ep_in_desc,
242 (struct usb_descriptor_header *) &ss_ep_in_comp_desc,
243 (struct usb_descriptor_header *) &ss_ep_out_desc,
244 (struct usb_descriptor_header *) &ss_ep_out_comp_desc,
245 NULL
246};
247
248static struct usb_otg_descriptor otg_descriptor = { 85static struct usb_otg_descriptor otg_descriptor = {
249 .bLength = sizeof otg_descriptor, 86 .bLength = sizeof otg_descriptor,
250 .bDescriptorType = USB_DT_OTG, 87 .bDescriptorType = USB_DT_OTG,
@@ -256,29 +93,13 @@ static const struct usb_descriptor_header *otg_desc[] = {
256 NULL, 93 NULL,
257}; 94};
258 95
259/* maxpacket and other transfer characteristics vary by speed. */
260static inline struct usb_endpoint_descriptor *ep_desc(struct usb_gadget *gadget,
261 struct usb_endpoint_descriptor *fs,
262 struct usb_endpoint_descriptor *hs,
263 struct usb_endpoint_descriptor *ss)
264{
265 switch (gadget->speed) {
266 case USB_SPEED_SUPER:
267 return ss;
268 case USB_SPEED_HIGH:
269 return hs;
270 default:
271 return fs;
272 }
273}
274
275/*-------------------------------------------------------------------------*/ 96/*-------------------------------------------------------------------------*/
276 97
277/* descriptors that are built on-demand */ 98/* descriptors that are built on-demand */
278 99
279static char product_desc [40] = DRIVER_DESC; 100static char product_desc [40] = DRIVER_DESC;
280static char serial_num [40] = "1"; 101static char serial_num [40] = "1";
281static char pnp_string [1024] = 102static char pnp_string[PNP_STRING_LEN] =
282 "XXMFG:linux;MDL:g_printer;CLS:PRINTER;SN:1;"; 103 "XXMFG:linux;MDL:g_printer;CLS:PRINTER;SN:1;";
283 104
284/* static strings, in UTF-8 */ 105/* static strings, in UTF-8 */
@@ -299,921 +120,19 @@ static struct usb_gadget_strings *dev_strings[] = {
299 NULL, 120 NULL,
300}; 121};
301 122
302/*-------------------------------------------------------------------------*/
303
304static struct usb_request *
305printer_req_alloc(struct usb_ep *ep, unsigned len, gfp_t gfp_flags)
306{
307 struct usb_request *req;
308
309 req = usb_ep_alloc_request(ep, gfp_flags);
310
311 if (req != NULL) {
312 req->length = len;
313 req->buf = kmalloc(len, gfp_flags);
314 if (req->buf == NULL) {
315 usb_ep_free_request(ep, req);
316 return NULL;
317 }
318 }
319
320 return req;
321}
322
323static void
324printer_req_free(struct usb_ep *ep, struct usb_request *req)
325{
326 if (ep != NULL && req != NULL) {
327 kfree(req->buf);
328 usb_ep_free_request(ep, req);
329 }
330}
331
332/*-------------------------------------------------------------------------*/
333
334static void rx_complete(struct usb_ep *ep, struct usb_request *req)
335{
336 struct printer_dev *dev = ep->driver_data;
337 int status = req->status;
338 unsigned long flags;
339
340 spin_lock_irqsave(&dev->lock, flags);
341
342 list_del_init(&req->list); /* Remode from Active List */
343
344 switch (status) {
345
346 /* normal completion */
347 case 0:
348 if (req->actual > 0) {
349 list_add_tail(&req->list, &dev->rx_buffers);
350 DBG(dev, "G_Printer : rx length %d\n", req->actual);
351 } else {
352 list_add(&req->list, &dev->rx_reqs);
353 }
354 break;
355
356 /* software-driven interface shutdown */
357 case -ECONNRESET: /* unlink */
358 case -ESHUTDOWN: /* disconnect etc */
359 VDBG(dev, "rx shutdown, code %d\n", status);
360 list_add(&req->list, &dev->rx_reqs);
361 break;
362
363 /* for hardware automagic (such as pxa) */
364 case -ECONNABORTED: /* endpoint reset */
365 DBG(dev, "rx %s reset\n", ep->name);
366 list_add(&req->list, &dev->rx_reqs);
367 break;
368
369 /* data overrun */
370 case -EOVERFLOW:
371 /* FALLTHROUGH */
372
373 default:
374 DBG(dev, "rx status %d\n", status);
375 list_add(&req->list, &dev->rx_reqs);
376 break;
377 }
378
379 wake_up_interruptible(&dev->rx_wait);
380 spin_unlock_irqrestore(&dev->lock, flags);
381}
382
383static void tx_complete(struct usb_ep *ep, struct usb_request *req)
384{
385 struct printer_dev *dev = ep->driver_data;
386
387 switch (req->status) {
388 default:
389 VDBG(dev, "tx err %d\n", req->status);
390 /* FALLTHROUGH */
391 case -ECONNRESET: /* unlink */
392 case -ESHUTDOWN: /* disconnect etc */
393 break;
394 case 0:
395 break;
396 }
397
398 spin_lock(&dev->lock);
399 /* Take the request struct off the active list and put it on the
400 * free list.
401 */
402 list_del_init(&req->list);
403 list_add(&req->list, &dev->tx_reqs);
404 wake_up_interruptible(&dev->tx_wait);
405 if (likely(list_empty(&dev->tx_reqs_active)))
406 wake_up_interruptible(&dev->tx_flush_wait);
407
408 spin_unlock(&dev->lock);
409}
410
411/*-------------------------------------------------------------------------*/
412
413static int
414printer_open(struct inode *inode, struct file *fd)
415{
416 struct printer_dev *dev;
417 unsigned long flags;
418 int ret = -EBUSY;
419
420 mutex_lock(&printer_mutex);
421 dev = container_of(inode->i_cdev, struct printer_dev, printer_cdev);
422
423 spin_lock_irqsave(&dev->lock, flags);
424
425 if (!dev->printer_cdev_open) {
426 dev->printer_cdev_open = 1;
427 fd->private_data = dev;
428 ret = 0;
429 /* Change the printer status to show that it's on-line. */
430 dev->printer_status |= PRINTER_SELECTED;
431 }
432
433 spin_unlock_irqrestore(&dev->lock, flags);
434
435 DBG(dev, "printer_open returned %x\n", ret);
436 mutex_unlock(&printer_mutex);
437 return ret;
438}
439
440static int
441printer_close(struct inode *inode, struct file *fd)
442{
443 struct printer_dev *dev = fd->private_data;
444 unsigned long flags;
445
446 spin_lock_irqsave(&dev->lock, flags);
447 dev->printer_cdev_open = 0;
448 fd->private_data = NULL;
449 /* Change printer status to show that the printer is off-line. */
450 dev->printer_status &= ~PRINTER_SELECTED;
451 spin_unlock_irqrestore(&dev->lock, flags);
452
453 DBG(dev, "printer_close\n");
454
455 return 0;
456}
457
458/* This function must be called with interrupts turned off. */
459static void
460setup_rx_reqs(struct printer_dev *dev)
461{
462 struct usb_request *req;
463
464 while (likely(!list_empty(&dev->rx_reqs))) {
465 int error;
466
467 req = container_of(dev->rx_reqs.next,
468 struct usb_request, list);
469 list_del_init(&req->list);
470
471 /* The USB Host sends us whatever amount of data it wants to
472 * so we always set the length field to the full USB_BUFSIZE.
473 * If the amount of data is more than the read() caller asked
474 * for it will be stored in the request buffer until it is
475 * asked for by read().
476 */
477 req->length = USB_BUFSIZE;
478 req->complete = rx_complete;
479
480 /* here, we unlock, and only unlock, to avoid deadlock. */
481 spin_unlock(&dev->lock);
482 error = usb_ep_queue(dev->out_ep, req, GFP_ATOMIC);
483 spin_lock(&dev->lock);
484 if (error) {
485 DBG(dev, "rx submit --> %d\n", error);
486 list_add(&req->list, &dev->rx_reqs);
487 break;
488 }
489 /* if the req is empty, then add it into dev->rx_reqs_active. */
490 else if (list_empty(&req->list)) {
491 list_add(&req->list, &dev->rx_reqs_active);
492 }
493 }
494}
495
496static ssize_t
497printer_read(struct file *fd, char __user *buf, size_t len, loff_t *ptr)
498{
499 struct printer_dev *dev = fd->private_data;
500 unsigned long flags;
501 size_t size;
502 size_t bytes_copied;
503 struct usb_request *req;
504 /* This is a pointer to the current USB rx request. */
505 struct usb_request *current_rx_req;
506 /* This is the number of bytes in the current rx buffer. */
507 size_t current_rx_bytes;
508 /* This is a pointer to the current rx buffer. */
509 u8 *current_rx_buf;
510
511 if (len == 0)
512 return -EINVAL;
513
514 DBG(dev, "printer_read trying to read %d bytes\n", (int)len);
515
516 mutex_lock(&dev->lock_printer_io);
517 spin_lock_irqsave(&dev->lock, flags);
518
519 /* We will use this flag later to check if a printer reset happened
520 * after we turn interrupts back on.
521 */
522 dev->reset_printer = 0;
523
524 setup_rx_reqs(dev);
525
526 bytes_copied = 0;
527 current_rx_req = dev->current_rx_req;
528 current_rx_bytes = dev->current_rx_bytes;
529 current_rx_buf = dev->current_rx_buf;
530 dev->current_rx_req = NULL;
531 dev->current_rx_bytes = 0;
532 dev->current_rx_buf = NULL;
533
534 /* Check if there is any data in the read buffers. Please note that
535 * current_rx_bytes is the number of bytes in the current rx buffer.
536 * If it is zero then check if there are any other rx_buffers that
537 * are on the completed list. We are only out of data if all rx
538 * buffers are empty.
539 */
540 if ((current_rx_bytes == 0) &&
541 (likely(list_empty(&dev->rx_buffers)))) {
542 /* Turn interrupts back on before sleeping. */
543 spin_unlock_irqrestore(&dev->lock, flags);
544
545 /*
546 * If no data is available check if this is a NON-Blocking
547 * call or not.
548 */
549 if (fd->f_flags & (O_NONBLOCK|O_NDELAY)) {
550 mutex_unlock(&dev->lock_printer_io);
551 return -EAGAIN;
552 }
553
554 /* Sleep until data is available */
555 wait_event_interruptible(dev->rx_wait,
556 (likely(!list_empty(&dev->rx_buffers))));
557 spin_lock_irqsave(&dev->lock, flags);
558 }
559
560 /* We have data to return then copy it to the caller's buffer.*/
561 while ((current_rx_bytes || likely(!list_empty(&dev->rx_buffers)))
562 && len) {
563 if (current_rx_bytes == 0) {
564 req = container_of(dev->rx_buffers.next,
565 struct usb_request, list);
566 list_del_init(&req->list);
567
568 if (req->actual && req->buf) {
569 current_rx_req = req;
570 current_rx_bytes = req->actual;
571 current_rx_buf = req->buf;
572 } else {
573 list_add(&req->list, &dev->rx_reqs);
574 continue;
575 }
576 }
577
578 /* Don't leave irqs off while doing memory copies */
579 spin_unlock_irqrestore(&dev->lock, flags);
580
581 if (len > current_rx_bytes)
582 size = current_rx_bytes;
583 else
584 size = len;
585
586 size -= copy_to_user(buf, current_rx_buf, size);
587 bytes_copied += size;
588 len -= size;
589 buf += size;
590
591 spin_lock_irqsave(&dev->lock, flags);
592
593 /* We've disconnected or reset so return. */
594 if (dev->reset_printer) {
595 list_add(&current_rx_req->list, &dev->rx_reqs);
596 spin_unlock_irqrestore(&dev->lock, flags);
597 mutex_unlock(&dev->lock_printer_io);
598 return -EAGAIN;
599 }
600
601 /* If we not returning all the data left in this RX request
602 * buffer then adjust the amount of data left in the buffer.
603 * Othewise if we are done with this RX request buffer then
604 * requeue it to get any incoming data from the USB host.
605 */
606 if (size < current_rx_bytes) {
607 current_rx_bytes -= size;
608 current_rx_buf += size;
609 } else {
610 list_add(&current_rx_req->list, &dev->rx_reqs);
611 current_rx_bytes = 0;
612 current_rx_buf = NULL;
613 current_rx_req = NULL;
614 }
615 }
616
617 dev->current_rx_req = current_rx_req;
618 dev->current_rx_bytes = current_rx_bytes;
619 dev->current_rx_buf = current_rx_buf;
620
621 spin_unlock_irqrestore(&dev->lock, flags);
622 mutex_unlock(&dev->lock_printer_io);
623
624 DBG(dev, "printer_read returned %d bytes\n", (int)bytes_copied);
625
626 if (bytes_copied)
627 return bytes_copied;
628 else
629 return -EAGAIN;
630}
631
632static ssize_t
633printer_write(struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
634{
635 struct printer_dev *dev = fd->private_data;
636 unsigned long flags;
637 size_t size; /* Amount of data in a TX request. */
638 size_t bytes_copied = 0;
639 struct usb_request *req;
640
641 DBG(dev, "printer_write trying to send %d bytes\n", (int)len);
642
643 if (len == 0)
644 return -EINVAL;
645
646 mutex_lock(&dev->lock_printer_io);
647 spin_lock_irqsave(&dev->lock, flags);
648
649 /* Check if a printer reset happens while we have interrupts on */
650 dev->reset_printer = 0;
651
652 /* Check if there is any available write buffers */
653 if (likely(list_empty(&dev->tx_reqs))) {
654 /* Turn interrupts back on before sleeping. */
655 spin_unlock_irqrestore(&dev->lock, flags);
656
657 /*
658 * If write buffers are available check if this is
659 * a NON-Blocking call or not.
660 */
661 if (fd->f_flags & (O_NONBLOCK|O_NDELAY)) {
662 mutex_unlock(&dev->lock_printer_io);
663 return -EAGAIN;
664 }
665
666 /* Sleep until a write buffer is available */
667 wait_event_interruptible(dev->tx_wait,
668 (likely(!list_empty(&dev->tx_reqs))));
669 spin_lock_irqsave(&dev->lock, flags);
670 }
671
672 while (likely(!list_empty(&dev->tx_reqs)) && len) {
673
674 if (len > USB_BUFSIZE)
675 size = USB_BUFSIZE;
676 else
677 size = len;
678
679 req = container_of(dev->tx_reqs.next, struct usb_request,
680 list);
681 list_del_init(&req->list);
682
683 req->complete = tx_complete;
684 req->length = size;
685
686 /* Check if we need to send a zero length packet. */
687 if (len > size)
688 /* They will be more TX requests so no yet. */
689 req->zero = 0;
690 else
691 /* If the data amount is not a multple of the
692 * maxpacket size then send a zero length packet.
693 */
694 req->zero = ((len % dev->in_ep->maxpacket) == 0);
695
696 /* Don't leave irqs off while doing memory copies */
697 spin_unlock_irqrestore(&dev->lock, flags);
698
699 if (copy_from_user(req->buf, buf, size)) {
700 list_add(&req->list, &dev->tx_reqs);
701 mutex_unlock(&dev->lock_printer_io);
702 return bytes_copied;
703 }
704
705 bytes_copied += size;
706 len -= size;
707 buf += size;
708
709 spin_lock_irqsave(&dev->lock, flags);
710
711 /* We've disconnected or reset so free the req and buffer */
712 if (dev->reset_printer) {
713 list_add(&req->list, &dev->tx_reqs);
714 spin_unlock_irqrestore(&dev->lock, flags);
715 mutex_unlock(&dev->lock_printer_io);
716 return -EAGAIN;
717 }
718
719 if (usb_ep_queue(dev->in_ep, req, GFP_ATOMIC)) {
720 list_add(&req->list, &dev->tx_reqs);
721 spin_unlock_irqrestore(&dev->lock, flags);
722 mutex_unlock(&dev->lock_printer_io);
723 return -EAGAIN;
724 }
725
726 list_add(&req->list, &dev->tx_reqs_active);
727
728 }
729
730 spin_unlock_irqrestore(&dev->lock, flags);
731 mutex_unlock(&dev->lock_printer_io);
732
733 DBG(dev, "printer_write sent %d bytes\n", (int)bytes_copied);
734
735 if (bytes_copied) {
736 return bytes_copied;
737 } else {
738 return -EAGAIN;
739 }
740}
741
742static int
743printer_fsync(struct file *fd, loff_t start, loff_t end, int datasync)
744{
745 struct printer_dev *dev = fd->private_data;
746 struct inode *inode = file_inode(fd);
747 unsigned long flags;
748 int tx_list_empty;
749
750 mutex_lock(&inode->i_mutex);
751 spin_lock_irqsave(&dev->lock, flags);
752 tx_list_empty = (likely(list_empty(&dev->tx_reqs)));
753 spin_unlock_irqrestore(&dev->lock, flags);
754
755 if (!tx_list_empty) {
756 /* Sleep until all data has been sent */
757 wait_event_interruptible(dev->tx_flush_wait,
758 (likely(list_empty(&dev->tx_reqs_active))));
759 }
760 mutex_unlock(&inode->i_mutex);
761
762 return 0;
763}
764
765static unsigned int
766printer_poll(struct file *fd, poll_table *wait)
767{
768 struct printer_dev *dev = fd->private_data;
769 unsigned long flags;
770 int status = 0;
771
772 mutex_lock(&dev->lock_printer_io);
773 spin_lock_irqsave(&dev->lock, flags);
774 setup_rx_reqs(dev);
775 spin_unlock_irqrestore(&dev->lock, flags);
776 mutex_unlock(&dev->lock_printer_io);
777
778 poll_wait(fd, &dev->rx_wait, wait);
779 poll_wait(fd, &dev->tx_wait, wait);
780
781 spin_lock_irqsave(&dev->lock, flags);
782 if (likely(!list_empty(&dev->tx_reqs)))
783 status |= POLLOUT | POLLWRNORM;
784
785 if (likely(dev->current_rx_bytes) ||
786 likely(!list_empty(&dev->rx_buffers)))
787 status |= POLLIN | POLLRDNORM;
788
789 spin_unlock_irqrestore(&dev->lock, flags);
790
791 return status;
792}
793
794static long
795printer_ioctl(struct file *fd, unsigned int code, unsigned long arg)
796{
797 struct printer_dev *dev = fd->private_data;
798 unsigned long flags;
799 int status = 0;
800
801 DBG(dev, "printer_ioctl: cmd=0x%4.4x, arg=%lu\n", code, arg);
802
803 /* handle ioctls */
804
805 spin_lock_irqsave(&dev->lock, flags);
806
807 switch (code) {
808 case GADGET_GET_PRINTER_STATUS:
809 status = (int)dev->printer_status;
810 break;
811 case GADGET_SET_PRINTER_STATUS:
812 dev->printer_status = (u8)arg;
813 break;
814 default:
815 /* could not handle ioctl */
816 DBG(dev, "printer_ioctl: ERROR cmd=0x%4.4xis not supported\n",
817 code);
818 status = -ENOTTY;
819 }
820
821 spin_unlock_irqrestore(&dev->lock, flags);
822
823 return status;
824}
825
826/* used after endpoint configuration */
827static const struct file_operations printer_io_operations = {
828 .owner = THIS_MODULE,
829 .open = printer_open,
830 .read = printer_read,
831 .write = printer_write,
832 .fsync = printer_fsync,
833 .poll = printer_poll,
834 .unlocked_ioctl = printer_ioctl,
835 .release = printer_close,
836 .llseek = noop_llseek,
837};
838
839/*-------------------------------------------------------------------------*/
840
841static int
842set_printer_interface(struct printer_dev *dev)
843{
844 int result = 0;
845
846 dev->in_ep->desc = ep_desc(dev->gadget, &fs_ep_in_desc, &hs_ep_in_desc,
847 &ss_ep_in_desc);
848 dev->in_ep->driver_data = dev;
849
850 dev->out_ep->desc = ep_desc(dev->gadget, &fs_ep_out_desc,
851 &hs_ep_out_desc, &ss_ep_out_desc);
852 dev->out_ep->driver_data = dev;
853
854 result = usb_ep_enable(dev->in_ep);
855 if (result != 0) {
856 DBG(dev, "enable %s --> %d\n", dev->in_ep->name, result);
857 goto done;
858 }
859
860 result = usb_ep_enable(dev->out_ep);
861 if (result != 0) {
862 DBG(dev, "enable %s --> %d\n", dev->in_ep->name, result);
863 goto done;
864 }
865
866done:
867 /* on error, disable any endpoints */
868 if (result != 0) {
869 (void) usb_ep_disable(dev->in_ep);
870 (void) usb_ep_disable(dev->out_ep);
871 dev->in_ep->desc = NULL;
872 dev->out_ep->desc = NULL;
873 }
874
875 /* caller is responsible for cleanup on error */
876 return result;
877}
878
879static void printer_reset_interface(struct printer_dev *dev)
880{
881 if (dev->interface < 0)
882 return;
883
884 DBG(dev, "%s\n", __func__);
885
886 if (dev->in_ep->desc)
887 usb_ep_disable(dev->in_ep);
888
889 if (dev->out_ep->desc)
890 usb_ep_disable(dev->out_ep);
891
892 dev->in_ep->desc = NULL;
893 dev->out_ep->desc = NULL;
894 dev->interface = -1;
895}
896
897/* Change our operational Interface. */
898static int set_interface(struct printer_dev *dev, unsigned number)
899{
900 int result = 0;
901
902 /* Free the current interface */
903 printer_reset_interface(dev);
904
905 result = set_printer_interface(dev);
906 if (result)
907 printer_reset_interface(dev);
908 else
909 dev->interface = number;
910
911 if (!result)
912 INFO(dev, "Using interface %x\n", number);
913
914 return result;
915}
916
917static void printer_soft_reset(struct printer_dev *dev)
918{
919 struct usb_request *req;
920
921 INFO(dev, "Received Printer Reset Request\n");
922
923 if (usb_ep_disable(dev->in_ep))
924 DBG(dev, "Failed to disable USB in_ep\n");
925 if (usb_ep_disable(dev->out_ep))
926 DBG(dev, "Failed to disable USB out_ep\n");
927
928 if (dev->current_rx_req != NULL) {
929 list_add(&dev->current_rx_req->list, &dev->rx_reqs);
930 dev->current_rx_req = NULL;
931 }
932 dev->current_rx_bytes = 0;
933 dev->current_rx_buf = NULL;
934 dev->reset_printer = 1;
935
936 while (likely(!(list_empty(&dev->rx_buffers)))) {
937 req = container_of(dev->rx_buffers.next, struct usb_request,
938 list);
939 list_del_init(&req->list);
940 list_add(&req->list, &dev->rx_reqs);
941 }
942
943 while (likely(!(list_empty(&dev->rx_reqs_active)))) {
944 req = container_of(dev->rx_buffers.next, struct usb_request,
945 list);
946 list_del_init(&req->list);
947 list_add(&req->list, &dev->rx_reqs);
948 }
949
950 while (likely(!(list_empty(&dev->tx_reqs_active)))) {
951 req = container_of(dev->tx_reqs_active.next,
952 struct usb_request, list);
953 list_del_init(&req->list);
954 list_add(&req->list, &dev->tx_reqs);
955 }
956
957 if (usb_ep_enable(dev->in_ep))
958 DBG(dev, "Failed to enable USB in_ep\n");
959 if (usb_ep_enable(dev->out_ep))
960 DBG(dev, "Failed to enable USB out_ep\n");
961
962 wake_up_interruptible(&dev->rx_wait);
963 wake_up_interruptible(&dev->tx_wait);
964 wake_up_interruptible(&dev->tx_flush_wait);
965}
966
967/*-------------------------------------------------------------------------*/
968
969/*
970 * The setup() callback implements all the ep0 functionality that's not
971 * handled lower down.
972 */
973static int printer_func_setup(struct usb_function *f,
974 const struct usb_ctrlrequest *ctrl)
975{
976 struct printer_dev *dev = container_of(f, struct printer_dev, function);
977 struct usb_composite_dev *cdev = f->config->cdev;
978 struct usb_request *req = cdev->req;
979 int value = -EOPNOTSUPP;
980 u16 wIndex = le16_to_cpu(ctrl->wIndex);
981 u16 wValue = le16_to_cpu(ctrl->wValue);
982 u16 wLength = le16_to_cpu(ctrl->wLength);
983
984 DBG(dev, "ctrl req%02x.%02x v%04x i%04x l%d\n",
985 ctrl->bRequestType, ctrl->bRequest, wValue, wIndex, wLength);
986
987 switch (ctrl->bRequestType&USB_TYPE_MASK) {
988 case USB_TYPE_CLASS:
989 switch (ctrl->bRequest) {
990 case 0: /* Get the IEEE-1284 PNP String */
991 /* Only one printer interface is supported. */
992 if ((wIndex>>8) != dev->interface)
993 break;
994
995 value = (pnp_string[0]<<8)|pnp_string[1];
996 memcpy(req->buf, pnp_string, value);
997 DBG(dev, "1284 PNP String: %x %s\n", value,
998 &pnp_string[2]);
999 break;
1000
1001 case 1: /* Get Port Status */
1002 /* Only one printer interface is supported. */
1003 if (wIndex != dev->interface)
1004 break;
1005
1006 *(u8 *)req->buf = dev->printer_status;
1007 value = min(wLength, (u16) 1);
1008 break;
1009
1010 case 2: /* Soft Reset */
1011 /* Only one printer interface is supported. */
1012 if (wIndex != dev->interface)
1013 break;
1014
1015 printer_soft_reset(dev);
1016
1017 value = 0;
1018 break;
1019
1020 default:
1021 goto unknown;
1022 }
1023 break;
1024
1025 default:
1026unknown:
1027 VDBG(dev,
1028 "unknown ctrl req%02x.%02x v%04x i%04x l%d\n",
1029 ctrl->bRequestType, ctrl->bRequest,
1030 wValue, wIndex, wLength);
1031 break;
1032 }
1033 /* host either stalls (value < 0) or reports success */
1034 return value;
1035}
1036
1037static int __init printer_func_bind(struct usb_configuration *c,
1038 struct usb_function *f)
1039{
1040 struct printer_dev *dev = container_of(f, struct printer_dev, function);
1041 struct usb_composite_dev *cdev = c->cdev;
1042 struct usb_ep *in_ep;
1043 struct usb_ep *out_ep = NULL;
1044 int id;
1045 int ret;
1046
1047 id = usb_interface_id(c, f);
1048 if (id < 0)
1049 return id;
1050 intf_desc.bInterfaceNumber = id;
1051
1052 /* all we really need is bulk IN/OUT */
1053 in_ep = usb_ep_autoconfig(cdev->gadget, &fs_ep_in_desc);
1054 if (!in_ep) {
1055autoconf_fail:
1056 dev_err(&cdev->gadget->dev, "can't autoconfigure on %s\n",
1057 cdev->gadget->name);
1058 return -ENODEV;
1059 }
1060 in_ep->driver_data = in_ep; /* claim */
1061
1062 out_ep = usb_ep_autoconfig(cdev->gadget, &fs_ep_out_desc);
1063 if (!out_ep)
1064 goto autoconf_fail;
1065 out_ep->driver_data = out_ep; /* claim */
1066
1067 /* assumes that all endpoints are dual-speed */
1068 hs_ep_in_desc.bEndpointAddress = fs_ep_in_desc.bEndpointAddress;
1069 hs_ep_out_desc.bEndpointAddress = fs_ep_out_desc.bEndpointAddress;
1070 ss_ep_in_desc.bEndpointAddress = fs_ep_in_desc.bEndpointAddress;
1071 ss_ep_out_desc.bEndpointAddress = fs_ep_out_desc.bEndpointAddress;
1072
1073 ret = usb_assign_descriptors(f, fs_printer_function,
1074 hs_printer_function, ss_printer_function);
1075 if (ret)
1076 return ret;
1077
1078 dev->in_ep = in_ep;
1079 dev->out_ep = out_ep;
1080 return 0;
1081}
1082
1083static void printer_func_unbind(struct usb_configuration *c,
1084 struct usb_function *f)
1085{
1086 usb_free_all_descriptors(f);
1087}
1088
1089static int printer_func_set_alt(struct usb_function *f,
1090 unsigned intf, unsigned alt)
1091{
1092 struct printer_dev *dev = container_of(f, struct printer_dev, function);
1093 int ret = -ENOTSUPP;
1094
1095 if (!alt)
1096 ret = set_interface(dev, intf);
1097
1098 return ret;
1099}
1100
1101static void printer_func_disable(struct usb_function *f)
1102{
1103 struct printer_dev *dev = container_of(f, struct printer_dev, function);
1104 unsigned long flags;
1105
1106 DBG(dev, "%s\n", __func__);
1107
1108 spin_lock_irqsave(&dev->lock, flags);
1109 printer_reset_interface(dev);
1110 spin_unlock_irqrestore(&dev->lock, flags);
1111}
1112
1113static void printer_cfg_unbind(struct usb_configuration *c)
1114{
1115 struct printer_dev *dev;
1116 struct usb_request *req;
1117
1118 dev = &usb_printer_gadget;
1119
1120 DBG(dev, "%s\n", __func__);
1121
1122 /* Remove sysfs files */
1123 device_destroy(usb_gadget_class, g_printer_devno);
1124
1125 /* Remove Character Device */
1126 cdev_del(&dev->printer_cdev);
1127
1128 /* we must already have been disconnected ... no i/o may be active */
1129 WARN_ON(!list_empty(&dev->tx_reqs_active));
1130 WARN_ON(!list_empty(&dev->rx_reqs_active));
1131
1132 /* Free all memory for this driver. */
1133 while (!list_empty(&dev->tx_reqs)) {
1134 req = container_of(dev->tx_reqs.next, struct usb_request,
1135 list);
1136 list_del(&req->list);
1137 printer_req_free(dev->in_ep, req);
1138 }
1139
1140 if (dev->current_rx_req != NULL)
1141 printer_req_free(dev->out_ep, dev->current_rx_req);
1142
1143 while (!list_empty(&dev->rx_reqs)) {
1144 req = container_of(dev->rx_reqs.next,
1145 struct usb_request, list);
1146 list_del(&req->list);
1147 printer_req_free(dev->out_ep, req);
1148 }
1149
1150 while (!list_empty(&dev->rx_buffers)) {
1151 req = container_of(dev->rx_buffers.next,
1152 struct usb_request, list);
1153 list_del(&req->list);
1154 printer_req_free(dev->out_ep, req);
1155 }
1156}
1157
1158static struct usb_configuration printer_cfg_driver = { 123static struct usb_configuration printer_cfg_driver = {
1159 .label = "printer", 124 .label = "printer",
1160 .unbind = printer_cfg_unbind,
1161 .bConfigurationValue = 1, 125 .bConfigurationValue = 1,
1162 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER, 126 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
1163}; 127};
1164 128
1165static int __init printer_bind_config(struct usb_configuration *c) 129static int __init printer_do_config(struct usb_configuration *c)
1166{ 130{
1167 struct usb_gadget *gadget = c->cdev->gadget; 131 struct usb_gadget *gadget = c->cdev->gadget;
1168 struct printer_dev *dev; 132 int status = 0;
1169 int status = -ENOMEM;
1170 size_t len;
1171 u32 i;
1172 struct usb_request *req;
1173 133
1174 usb_ep_autoconfig_reset(gadget); 134 usb_ep_autoconfig_reset(gadget);
1175 135
1176 dev = &usb_printer_gadget;
1177
1178 dev->function.name = shortname;
1179 dev->function.bind = printer_func_bind;
1180 dev->function.setup = printer_func_setup;
1181 dev->function.unbind = printer_func_unbind;
1182 dev->function.set_alt = printer_func_set_alt;
1183 dev->function.disable = printer_func_disable;
1184
1185 status = usb_add_function(c, &dev->function);
1186 if (status)
1187 return status;
1188
1189 /* Setup the sysfs files for the printer gadget. */
1190 dev->pdev = device_create(usb_gadget_class, NULL, g_printer_devno,
1191 NULL, "g_printer");
1192 if (IS_ERR(dev->pdev)) {
1193 ERROR(dev, "Failed to create device: g_printer\n");
1194 status = PTR_ERR(dev->pdev);
1195 goto fail;
1196 }
1197
1198 /*
1199 * Register a character device as an interface to a user mode
1200 * program that handles the printer specific functionality.
1201 */
1202 cdev_init(&dev->printer_cdev, &printer_io_operations);
1203 dev->printer_cdev.owner = THIS_MODULE;
1204 status = cdev_add(&dev->printer_cdev, g_printer_devno, 1);
1205 if (status) {
1206 ERROR(dev, "Failed to open char device\n");
1207 goto fail;
1208 }
1209
1210 if (iPNPstring)
1211 strlcpy(&pnp_string[2], iPNPstring, (sizeof pnp_string)-2);
1212
1213 len = strlen(pnp_string);
1214 pnp_string[0] = (len >> 8) & 0xFF;
1215 pnp_string[1] = len & 0xFF;
1216
1217 usb_gadget_set_selfpowered(gadget); 136 usb_gadget_set_selfpowered(gadget);
1218 137
1219 if (gadget_is_otg(gadget)) { 138 if (gadget_is_otg(gadget)) {
@@ -1222,86 +141,64 @@ static int __init printer_bind_config(struct usb_configuration *c)
1222 printer_cfg_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP; 141 printer_cfg_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1223 } 142 }
1224 143
1225 spin_lock_init(&dev->lock); 144 f_printer = usb_get_function(fi_printer);
1226 mutex_init(&dev->lock_printer_io); 145 if (IS_ERR(f_printer))
1227 INIT_LIST_HEAD(&dev->tx_reqs); 146 return PTR_ERR(f_printer);
1228 INIT_LIST_HEAD(&dev->tx_reqs_active);
1229 INIT_LIST_HEAD(&dev->rx_reqs);
1230 INIT_LIST_HEAD(&dev->rx_reqs_active);
1231 INIT_LIST_HEAD(&dev->rx_buffers);
1232 init_waitqueue_head(&dev->rx_wait);
1233 init_waitqueue_head(&dev->tx_wait);
1234 init_waitqueue_head(&dev->tx_flush_wait);
1235
1236 dev->interface = -1;
1237 dev->printer_cdev_open = 0;
1238 dev->printer_status = PRINTER_NOT_ERROR;
1239 dev->current_rx_req = NULL;
1240 dev->current_rx_bytes = 0;
1241 dev->current_rx_buf = NULL;
1242
1243 for (i = 0; i < QLEN; i++) {
1244 req = printer_req_alloc(dev->in_ep, USB_BUFSIZE, GFP_KERNEL);
1245 if (!req) {
1246 while (!list_empty(&dev->tx_reqs)) {
1247 req = container_of(dev->tx_reqs.next,
1248 struct usb_request, list);
1249 list_del(&req->list);
1250 printer_req_free(dev->in_ep, req);
1251 }
1252 return -ENOMEM;
1253 }
1254 list_add(&req->list, &dev->tx_reqs);
1255 }
1256
1257 for (i = 0; i < QLEN; i++) {
1258 req = printer_req_alloc(dev->out_ep, USB_BUFSIZE, GFP_KERNEL);
1259 if (!req) {
1260 while (!list_empty(&dev->rx_reqs)) {
1261 req = container_of(dev->rx_reqs.next,
1262 struct usb_request, list);
1263 list_del(&req->list);
1264 printer_req_free(dev->out_ep, req);
1265 }
1266 return -ENOMEM;
1267 }
1268 list_add(&req->list, &dev->rx_reqs);
1269 }
1270
1271 /* finish hookup to lower layer ... */
1272 dev->gadget = gadget;
1273 147
1274 INFO(dev, "%s, version: " DRIVER_VERSION "\n", driver_desc); 148 status = usb_add_function(c, f_printer);
1275 return 0; 149 if (status < 0)
150 usb_put_function(f_printer);
1276 151
1277fail:
1278 printer_cfg_unbind(c);
1279 return status; 152 return status;
1280} 153}
1281 154
1282static int printer_unbind(struct usb_composite_dev *cdev)
1283{
1284 return 0;
1285}
1286
1287static int __init printer_bind(struct usb_composite_dev *cdev) 155static int __init printer_bind(struct usb_composite_dev *cdev)
1288{ 156{
1289 int ret; 157 struct f_printer_opts *opts;
158 int ret, len;
159
160 fi_printer = usb_get_function_instance("printer");
161 if (IS_ERR(fi_printer))
162 return PTR_ERR(fi_printer);
163
164 if (iPNPstring)
165 strlcpy(&pnp_string[2], iPNPstring, PNP_STRING_LEN - 2);
166
167 len = strlen(pnp_string);
168 pnp_string[0] = (len >> 8) & 0xFF;
169 pnp_string[1] = len & 0xFF;
170
171 opts = container_of(fi_printer, struct f_printer_opts, func_inst);
172 opts->minor = 0;
173 memcpy(opts->pnp_string, pnp_string, PNP_STRING_LEN);
174 opts->q_len = QLEN;
1290 175
1291 ret = usb_string_ids_tab(cdev, strings); 176 ret = usb_string_ids_tab(cdev, strings);
1292 if (ret < 0) 177 if (ret < 0) {
178 usb_put_function_instance(fi_printer);
1293 return ret; 179 return ret;
180 }
1294 device_desc.iManufacturer = strings[USB_GADGET_MANUFACTURER_IDX].id; 181 device_desc.iManufacturer = strings[USB_GADGET_MANUFACTURER_IDX].id;
1295 device_desc.iProduct = strings[USB_GADGET_PRODUCT_IDX].id; 182 device_desc.iProduct = strings[USB_GADGET_PRODUCT_IDX].id;
1296 device_desc.iSerialNumber = strings[USB_GADGET_SERIAL_IDX].id; 183 device_desc.iSerialNumber = strings[USB_GADGET_SERIAL_IDX].id;
1297 184
1298 ret = usb_add_config(cdev, &printer_cfg_driver, printer_bind_config); 185 ret = usb_add_config(cdev, &printer_cfg_driver, printer_do_config);
1299 if (ret) 186 if (ret) {
187 usb_put_function_instance(fi_printer);
1300 return ret; 188 return ret;
189 }
1301 usb_composite_overwrite_options(cdev, &coverwrite); 190 usb_composite_overwrite_options(cdev, &coverwrite);
1302 return ret; 191 return ret;
1303} 192}
1304 193
194static int __exit printer_unbind(struct usb_composite_dev *cdev)
195{
196 usb_put_function(f_printer);
197 usb_put_function_instance(fi_printer);
198
199 return 0;
200}
201
1305static __refdata struct usb_composite_driver printer_driver = { 202static __refdata struct usb_composite_driver printer_driver = {
1306 .name = shortname, 203 .name = shortname,
1307 .dev = &device_desc, 204 .dev = &device_desc,
@@ -1311,47 +208,7 @@ static __refdata struct usb_composite_driver printer_driver = {
1311 .unbind = printer_unbind, 208 .unbind = printer_unbind,
1312}; 209};
1313 210
1314static int __init 211module_usb_composite_driver(printer_driver);
1315init(void)
1316{
1317 int status;
1318
1319 usb_gadget_class = class_create(THIS_MODULE, "usb_printer_gadget");
1320 if (IS_ERR(usb_gadget_class)) {
1321 status = PTR_ERR(usb_gadget_class);
1322 pr_err("unable to create usb_gadget class %d\n", status);
1323 return status;
1324 }
1325
1326 status = alloc_chrdev_region(&g_printer_devno, 0, 1,
1327 "USB printer gadget");
1328 if (status) {
1329 pr_err("alloc_chrdev_region %d\n", status);
1330 class_destroy(usb_gadget_class);
1331 return status;
1332 }
1333
1334 status = usb_composite_probe(&printer_driver);
1335 if (status) {
1336 class_destroy(usb_gadget_class);
1337 unregister_chrdev_region(g_printer_devno, 1);
1338 pr_err("usb_gadget_probe_driver %x\n", status);
1339 }
1340
1341 return status;
1342}
1343module_init(init);
1344
1345static void __exit
1346cleanup(void)
1347{
1348 mutex_lock(&usb_printer_gadget.lock_printer_io);
1349 usb_composite_unregister(&printer_driver);
1350 unregister_chrdev_region(g_printer_devno, 1);
1351 class_destroy(usb_gadget_class);
1352 mutex_unlock(&usb_printer_gadget.lock_printer_io);
1353}
1354module_exit(cleanup);
1355 212
1356MODULE_DESCRIPTION(DRIVER_DESC); 213MODULE_DESCRIPTION(DRIVER_DESC);
1357MODULE_AUTHOR("Craig Nadler"); 214MODULE_AUTHOR("Craig Nadler");
diff --git a/drivers/usb/gadget/udc/atmel_usba_udc.c b/drivers/usb/gadget/udc/atmel_usba_udc.c
index d79cb35dbf8a..4c01953a0869 100644
--- a/drivers/usb/gadget/udc/atmel_usba_udc.c
+++ b/drivers/usb/gadget/udc/atmel_usba_udc.c
@@ -152,7 +152,7 @@ static int regs_dbg_open(struct inode *inode, struct file *file)
152 152
153 spin_lock_irq(&udc->lock); 153 spin_lock_irq(&udc->lock);
154 for (i = 0; i < inode->i_size / 4; i++) 154 for (i = 0; i < inode->i_size / 4; i++)
155 data[i] = __raw_readl(udc->regs + i * 4); 155 data[i] = usba_io_readl(udc->regs + i * 4);
156 spin_unlock_irq(&udc->lock); 156 spin_unlock_irq(&udc->lock);
157 157
158 file->private_data = data; 158 file->private_data = data;
@@ -1249,7 +1249,7 @@ static int handle_ep0_setup(struct usba_udc *udc, struct usba_ep *ep,
1249 if (crq->wLength != cpu_to_le16(sizeof(status))) 1249 if (crq->wLength != cpu_to_le16(sizeof(status)))
1250 goto stall; 1250 goto stall;
1251 ep->state = DATA_STAGE_IN; 1251 ep->state = DATA_STAGE_IN;
1252 __raw_writew(status, ep->fifo); 1252 usba_io_writew(status, ep->fifo);
1253 usba_ep_writel(ep, SET_STA, USBA_TX_PK_RDY); 1253 usba_ep_writel(ep, SET_STA, USBA_TX_PK_RDY);
1254 break; 1254 break;
1255 } 1255 }
@@ -1739,7 +1739,72 @@ static irqreturn_t usba_udc_irq(int irq, void *devid)
1739 return IRQ_HANDLED; 1739 return IRQ_HANDLED;
1740} 1740}
1741 1741
1742static irqreturn_t usba_vbus_irq(int irq, void *devid) 1742static int start_clock(struct usba_udc *udc)
1743{
1744 int ret;
1745
1746 if (udc->clocked)
1747 return 0;
1748
1749 ret = clk_prepare_enable(udc->pclk);
1750 if (ret)
1751 return ret;
1752 ret = clk_prepare_enable(udc->hclk);
1753 if (ret) {
1754 clk_disable_unprepare(udc->pclk);
1755 return ret;
1756 }
1757
1758 udc->clocked = true;
1759 return 0;
1760}
1761
1762static void stop_clock(struct usba_udc *udc)
1763{
1764 if (!udc->clocked)
1765 return;
1766
1767 clk_disable_unprepare(udc->hclk);
1768 clk_disable_unprepare(udc->pclk);
1769
1770 udc->clocked = false;
1771}
1772
1773static int usba_start(struct usba_udc *udc)
1774{
1775 unsigned long flags;
1776 int ret;
1777
1778 ret = start_clock(udc);
1779 if (ret)
1780 return ret;
1781
1782 spin_lock_irqsave(&udc->lock, flags);
1783 toggle_bias(udc, 1);
1784 usba_writel(udc, CTRL, USBA_ENABLE_MASK);
1785 usba_int_enb_set(udc, USBA_END_OF_RESET);
1786 spin_unlock_irqrestore(&udc->lock, flags);
1787
1788 return 0;
1789}
1790
1791static void usba_stop(struct usba_udc *udc)
1792{
1793 unsigned long flags;
1794
1795 spin_lock_irqsave(&udc->lock, flags);
1796 udc->gadget.speed = USB_SPEED_UNKNOWN;
1797 reset_all_endpoints(udc);
1798
1799 /* This will also disable the DP pullup */
1800 toggle_bias(udc, 0);
1801 usba_writel(udc, CTRL, USBA_DISABLE_MASK);
1802 spin_unlock_irqrestore(&udc->lock, flags);
1803
1804 stop_clock(udc);
1805}
1806
1807static irqreturn_t usba_vbus_irq_thread(int irq, void *devid)
1743{ 1808{
1744 struct usba_udc *udc = devid; 1809 struct usba_udc *udc = devid;
1745 int vbus; 1810 int vbus;
@@ -1747,35 +1812,22 @@ static irqreturn_t usba_vbus_irq(int irq, void *devid)
1747 /* debounce */ 1812 /* debounce */
1748 udelay(10); 1813 udelay(10);
1749 1814
1750 spin_lock(&udc->lock); 1815 mutex_lock(&udc->vbus_mutex);
1751
1752 /* May happen if Vbus pin toggles during probe() */
1753 if (!udc->driver)
1754 goto out;
1755 1816
1756 vbus = vbus_is_present(udc); 1817 vbus = vbus_is_present(udc);
1757 if (vbus != udc->vbus_prev) { 1818 if (vbus != udc->vbus_prev) {
1758 if (vbus) { 1819 if (vbus) {
1759 toggle_bias(udc, 1); 1820 usba_start(udc);
1760 usba_writel(udc, CTRL, USBA_ENABLE_MASK);
1761 usba_int_enb_set(udc, USBA_END_OF_RESET);
1762 } else { 1821 } else {
1763 udc->gadget.speed = USB_SPEED_UNKNOWN; 1822 usba_stop(udc);
1764 reset_all_endpoints(udc); 1823
1765 toggle_bias(udc, 0); 1824 if (udc->driver->disconnect)
1766 usba_writel(udc, CTRL, USBA_DISABLE_MASK);
1767 if (udc->driver->disconnect) {
1768 spin_unlock(&udc->lock);
1769 udc->driver->disconnect(&udc->gadget); 1825 udc->driver->disconnect(&udc->gadget);
1770 spin_lock(&udc->lock);
1771 }
1772 } 1826 }
1773 udc->vbus_prev = vbus; 1827 udc->vbus_prev = vbus;
1774 } 1828 }
1775 1829
1776out: 1830 mutex_unlock(&udc->vbus_mutex);
1777 spin_unlock(&udc->lock);
1778
1779 return IRQ_HANDLED; 1831 return IRQ_HANDLED;
1780} 1832}
1781 1833
@@ -1787,55 +1839,47 @@ static int atmel_usba_start(struct usb_gadget *gadget,
1787 unsigned long flags; 1839 unsigned long flags;
1788 1840
1789 spin_lock_irqsave(&udc->lock, flags); 1841 spin_lock_irqsave(&udc->lock, flags);
1790
1791 udc->devstatus = 1 << USB_DEVICE_SELF_POWERED; 1842 udc->devstatus = 1 << USB_DEVICE_SELF_POWERED;
1792 udc->driver = driver; 1843 udc->driver = driver;
1793 spin_unlock_irqrestore(&udc->lock, flags); 1844 spin_unlock_irqrestore(&udc->lock, flags);
1794 1845
1795 ret = clk_prepare_enable(udc->pclk); 1846 mutex_lock(&udc->vbus_mutex);
1796 if (ret)
1797 return ret;
1798 ret = clk_prepare_enable(udc->hclk);
1799 if (ret) {
1800 clk_disable_unprepare(udc->pclk);
1801 return ret;
1802 }
1803 1847
1804 udc->vbus_prev = 0;
1805 if (gpio_is_valid(udc->vbus_pin)) 1848 if (gpio_is_valid(udc->vbus_pin))
1806 enable_irq(gpio_to_irq(udc->vbus_pin)); 1849 enable_irq(gpio_to_irq(udc->vbus_pin));
1807 1850
1808 /* If Vbus is present, enable the controller and wait for reset */ 1851 /* If Vbus is present, enable the controller and wait for reset */
1809 spin_lock_irqsave(&udc->lock, flags); 1852 udc->vbus_prev = vbus_is_present(udc);
1810 if (vbus_is_present(udc) && udc->vbus_prev == 0) { 1853 if (udc->vbus_prev) {
1811 toggle_bias(udc, 1); 1854 ret = usba_start(udc);
1812 usba_writel(udc, CTRL, USBA_ENABLE_MASK); 1855 if (ret)
1813 usba_int_enb_set(udc, USBA_END_OF_RESET); 1856 goto err;
1814 } 1857 }
1815 spin_unlock_irqrestore(&udc->lock, flags);
1816 1858
1859 mutex_unlock(&udc->vbus_mutex);
1817 return 0; 1860 return 0;
1861
1862err:
1863 if (gpio_is_valid(udc->vbus_pin))
1864 disable_irq(gpio_to_irq(udc->vbus_pin));
1865
1866 mutex_unlock(&udc->vbus_mutex);
1867
1868 spin_lock_irqsave(&udc->lock, flags);
1869 udc->devstatus &= ~(1 << USB_DEVICE_SELF_POWERED);
1870 udc->driver = NULL;
1871 spin_unlock_irqrestore(&udc->lock, flags);
1872 return ret;
1818} 1873}
1819 1874
1820static int atmel_usba_stop(struct usb_gadget *gadget) 1875static int atmel_usba_stop(struct usb_gadget *gadget)
1821{ 1876{
1822 struct usba_udc *udc = container_of(gadget, struct usba_udc, gadget); 1877 struct usba_udc *udc = container_of(gadget, struct usba_udc, gadget);
1823 unsigned long flags;
1824 1878
1825 if (gpio_is_valid(udc->vbus_pin)) 1879 if (gpio_is_valid(udc->vbus_pin))
1826 disable_irq(gpio_to_irq(udc->vbus_pin)); 1880 disable_irq(gpio_to_irq(udc->vbus_pin));
1827 1881
1828 spin_lock_irqsave(&udc->lock, flags); 1882 usba_stop(udc);
1829 udc->gadget.speed = USB_SPEED_UNKNOWN;
1830 reset_all_endpoints(udc);
1831 spin_unlock_irqrestore(&udc->lock, flags);
1832
1833 /* This will also disable the DP pullup */
1834 toggle_bias(udc, 0);
1835 usba_writel(udc, CTRL, USBA_DISABLE_MASK);
1836
1837 clk_disable_unprepare(udc->hclk);
1838 clk_disable_unprepare(udc->pclk);
1839 1883
1840 udc->driver = NULL; 1884 udc->driver = NULL;
1841 1885
@@ -2057,6 +2101,7 @@ static int usba_udc_probe(struct platform_device *pdev)
2057 return PTR_ERR(hclk); 2101 return PTR_ERR(hclk);
2058 2102
2059 spin_lock_init(&udc->lock); 2103 spin_lock_init(&udc->lock);
2104 mutex_init(&udc->vbus_mutex);
2060 udc->pdev = pdev; 2105 udc->pdev = pdev;
2061 udc->pclk = pclk; 2106 udc->pclk = pclk;
2062 udc->hclk = hclk; 2107 udc->hclk = hclk;
@@ -2111,17 +2156,17 @@ static int usba_udc_probe(struct platform_device *pdev)
2111 2156
2112 if (gpio_is_valid(udc->vbus_pin)) { 2157 if (gpio_is_valid(udc->vbus_pin)) {
2113 if (!devm_gpio_request(&pdev->dev, udc->vbus_pin, "atmel_usba_udc")) { 2158 if (!devm_gpio_request(&pdev->dev, udc->vbus_pin, "atmel_usba_udc")) {
2114 ret = devm_request_irq(&pdev->dev, 2159 irq_set_status_flags(gpio_to_irq(udc->vbus_pin),
2115 gpio_to_irq(udc->vbus_pin), 2160 IRQ_NOAUTOEN);
2116 usba_vbus_irq, 0, 2161 ret = devm_request_threaded_irq(&pdev->dev,
2162 gpio_to_irq(udc->vbus_pin), NULL,
2163 usba_vbus_irq_thread, IRQF_ONESHOT,
2117 "atmel_usba_udc", udc); 2164 "atmel_usba_udc", udc);
2118 if (ret) { 2165 if (ret) {
2119 udc->vbus_pin = -ENODEV; 2166 udc->vbus_pin = -ENODEV;
2120 dev_warn(&udc->pdev->dev, 2167 dev_warn(&udc->pdev->dev,
2121 "failed to request vbus irq; " 2168 "failed to request vbus irq; "
2122 "assuming always on\n"); 2169 "assuming always on\n");
2123 } else {
2124 disable_irq(gpio_to_irq(udc->vbus_pin));
2125 } 2170 }
2126 } else { 2171 } else {
2127 /* gpio_request fail so use -EINVAL for gpio_is_valid */ 2172 /* gpio_request fail so use -EINVAL for gpio_is_valid */
@@ -2132,6 +2177,7 @@ static int usba_udc_probe(struct platform_device *pdev)
2132 ret = usb_add_gadget_udc(&pdev->dev, &udc->gadget); 2177 ret = usb_add_gadget_udc(&pdev->dev, &udc->gadget);
2133 if (ret) 2178 if (ret)
2134 return ret; 2179 return ret;
2180 device_init_wakeup(&pdev->dev, 1);
2135 2181
2136 usba_init_debugfs(udc); 2182 usba_init_debugfs(udc);
2137 for (i = 1; i < udc->num_ep; i++) 2183 for (i = 1; i < udc->num_ep; i++)
@@ -2147,6 +2193,7 @@ static int __exit usba_udc_remove(struct platform_device *pdev)
2147 2193
2148 udc = platform_get_drvdata(pdev); 2194 udc = platform_get_drvdata(pdev);
2149 2195
2196 device_init_wakeup(&pdev->dev, 0);
2150 usb_del_gadget_udc(&udc->gadget); 2197 usb_del_gadget_udc(&udc->gadget);
2151 2198
2152 for (i = 1; i < udc->num_ep; i++) 2199 for (i = 1; i < udc->num_ep; i++)
@@ -2156,10 +2203,65 @@ static int __exit usba_udc_remove(struct platform_device *pdev)
2156 return 0; 2203 return 0;
2157} 2204}
2158 2205
2206#ifdef CONFIG_PM
2207static int usba_udc_suspend(struct device *dev)
2208{
2209 struct usba_udc *udc = dev_get_drvdata(dev);
2210
2211 /* Not started */
2212 if (!udc->driver)
2213 return 0;
2214
2215 mutex_lock(&udc->vbus_mutex);
2216
2217 if (!device_may_wakeup(dev)) {
2218 usba_stop(udc);
2219 goto out;
2220 }
2221
2222 /*
2223 * Device may wake up. We stay clocked if we failed
2224 * to request vbus irq, assuming always on.
2225 */
2226 if (gpio_is_valid(udc->vbus_pin)) {
2227 usba_stop(udc);
2228 enable_irq_wake(gpio_to_irq(udc->vbus_pin));
2229 }
2230
2231out:
2232 mutex_unlock(&udc->vbus_mutex);
2233 return 0;
2234}
2235
2236static int usba_udc_resume(struct device *dev)
2237{
2238 struct usba_udc *udc = dev_get_drvdata(dev);
2239
2240 /* Not started */
2241 if (!udc->driver)
2242 return 0;
2243
2244 if (device_may_wakeup(dev) && gpio_is_valid(udc->vbus_pin))
2245 disable_irq_wake(gpio_to_irq(udc->vbus_pin));
2246
2247 /* If Vbus is present, enable the controller and wait for reset */
2248 mutex_lock(&udc->vbus_mutex);
2249 udc->vbus_prev = vbus_is_present(udc);
2250 if (udc->vbus_prev)
2251 usba_start(udc);
2252 mutex_unlock(&udc->vbus_mutex);
2253
2254 return 0;
2255}
2256#endif
2257
2258static SIMPLE_DEV_PM_OPS(usba_udc_pm_ops, usba_udc_suspend, usba_udc_resume);
2259
2159static struct platform_driver udc_driver = { 2260static struct platform_driver udc_driver = {
2160 .remove = __exit_p(usba_udc_remove), 2261 .remove = __exit_p(usba_udc_remove),
2161 .driver = { 2262 .driver = {
2162 .name = "atmel_usba_udc", 2263 .name = "atmel_usba_udc",
2264 .pm = &usba_udc_pm_ops,
2163 .of_match_table = of_match_ptr(atmel_udc_dt_ids), 2265 .of_match_table = of_match_ptr(atmel_udc_dt_ids),
2164 }, 2266 },
2165}; 2267};
diff --git a/drivers/usb/gadget/udc/atmel_usba_udc.h b/drivers/usb/gadget/udc/atmel_usba_udc.h
index 497cd18836f3..ea448a344767 100644
--- a/drivers/usb/gadget/udc/atmel_usba_udc.h
+++ b/drivers/usb/gadget/udc/atmel_usba_udc.h
@@ -191,18 +191,28 @@
191 | USBA_BF(name, value)) 191 | USBA_BF(name, value))
192 192
193/* Register access macros */ 193/* Register access macros */
194#ifdef CONFIG_AVR32
195#define usba_io_readl __raw_readl
196#define usba_io_writel __raw_writel
197#define usba_io_writew __raw_writew
198#else
199#define usba_io_readl readl_relaxed
200#define usba_io_writel writel_relaxed
201#define usba_io_writew writew_relaxed
202#endif
203
194#define usba_readl(udc, reg) \ 204#define usba_readl(udc, reg) \
195 __raw_readl((udc)->regs + USBA_##reg) 205 usba_io_readl((udc)->regs + USBA_##reg)
196#define usba_writel(udc, reg, value) \ 206#define usba_writel(udc, reg, value) \
197 __raw_writel((value), (udc)->regs + USBA_##reg) 207 usba_io_writel((value), (udc)->regs + USBA_##reg)
198#define usba_ep_readl(ep, reg) \ 208#define usba_ep_readl(ep, reg) \
199 __raw_readl((ep)->ep_regs + USBA_EPT_##reg) 209 usba_io_readl((ep)->ep_regs + USBA_EPT_##reg)
200#define usba_ep_writel(ep, reg, value) \ 210#define usba_ep_writel(ep, reg, value) \
201 __raw_writel((value), (ep)->ep_regs + USBA_EPT_##reg) 211 usba_io_writel((value), (ep)->ep_regs + USBA_EPT_##reg)
202#define usba_dma_readl(ep, reg) \ 212#define usba_dma_readl(ep, reg) \
203 __raw_readl((ep)->dma_regs + USBA_DMA_##reg) 213 usba_io_readl((ep)->dma_regs + USBA_DMA_##reg)
204#define usba_dma_writel(ep, reg, value) \ 214#define usba_dma_writel(ep, reg, value) \
205 __raw_writel((value), (ep)->dma_regs + USBA_DMA_##reg) 215 usba_io_writel((value), (ep)->dma_regs + USBA_DMA_##reg)
206 216
207/* Calculate base address for a given endpoint or DMA controller */ 217/* Calculate base address for a given endpoint or DMA controller */
208#define USBA_EPT_BASE(x) (0x100 + (x) * 0x20) 218#define USBA_EPT_BASE(x) (0x100 + (x) * 0x20)
@@ -313,6 +323,9 @@ struct usba_udc {
313 /* Protect hw registers from concurrent modifications */ 323 /* Protect hw registers from concurrent modifications */
314 spinlock_t lock; 324 spinlock_t lock;
315 325
326 /* Mutex to prevent concurrent start or stop */
327 struct mutex vbus_mutex;
328
316 void __iomem *regs; 329 void __iomem *regs;
317 void __iomem *fifo; 330 void __iomem *fifo;
318 331
@@ -328,6 +341,7 @@ struct usba_udc {
328 struct clk *hclk; 341 struct clk *hclk;
329 struct usba_ep *usba_ep; 342 struct usba_ep *usba_ep;
330 bool bias_pulse_needed; 343 bool bias_pulse_needed;
344 bool clocked;
331 345
332 u16 devstatus; 346 u16 devstatus;
333 347
diff --git a/drivers/usb/gadget/udc/dummy_hcd.c b/drivers/usb/gadget/udc/dummy_hcd.c
index 8dda48445f6f..7592db7824c6 100644
--- a/drivers/usb/gadget/udc/dummy_hcd.c
+++ b/drivers/usb/gadget/udc/dummy_hcd.c
@@ -2631,7 +2631,7 @@ static int __init init(void)
2631 return -EINVAL; 2631 return -EINVAL;
2632 2632
2633 if (mod_data.num < 1 || mod_data.num > MAX_NUM_UDC) { 2633 if (mod_data.num < 1 || mod_data.num > MAX_NUM_UDC) {
2634 pr_err("Number of emulated UDC must be in range of 1…%d\n", 2634 pr_err("Number of emulated UDC must be in range of 1...%d\n",
2635 MAX_NUM_UDC); 2635 MAX_NUM_UDC);
2636 return -EINVAL; 2636 return -EINVAL;
2637 } 2637 }
diff --git a/drivers/usb/gadget/udc/goku_udc.c b/drivers/usb/gadget/udc/goku_udc.c
index 5b9176e7202a..9e8d842e8c08 100644
--- a/drivers/usb/gadget/udc/goku_udc.c
+++ b/drivers/usb/gadget/udc/goku_udc.c
@@ -1024,35 +1024,79 @@ static const char proc_node_name [] = "driver/udc";
1024static void dump_intmask(struct seq_file *m, const char *label, u32 mask) 1024static void dump_intmask(struct seq_file *m, const char *label, u32 mask)
1025{ 1025{
1026 /* int_status is the same format ... */ 1026 /* int_status is the same format ... */
1027 seq_printf(m, 1027 seq_printf(m, "%s %05X =" FOURBITS EIGHTBITS EIGHTBITS "\n",
1028 "%s %05X =" FOURBITS EIGHTBITS EIGHTBITS "\n", 1028 label, mask,
1029 label, mask, 1029 (mask & INT_PWRDETECT) ? " power" : "",
1030 (mask & INT_PWRDETECT) ? " power" : "", 1030 (mask & INT_SYSERROR) ? " sys" : "",
1031 (mask & INT_SYSERROR) ? " sys" : "", 1031 (mask & INT_MSTRDEND) ? " in-dma" : "",
1032 (mask & INT_MSTRDEND) ? " in-dma" : "", 1032 (mask & INT_MSTWRTMOUT) ? " wrtmo" : "",
1033 (mask & INT_MSTWRTMOUT) ? " wrtmo" : "", 1033
1034 1034 (mask & INT_MSTWREND) ? " out-dma" : "",
1035 (mask & INT_MSTWREND) ? " out-dma" : "", 1035 (mask & INT_MSTWRSET) ? " wrset" : "",
1036 (mask & INT_MSTWRSET) ? " wrset" : "", 1036 (mask & INT_ERR) ? " err" : "",
1037 (mask & INT_ERR) ? " err" : "", 1037 (mask & INT_SOF) ? " sof" : "",
1038 (mask & INT_SOF) ? " sof" : "", 1038
1039 1039 (mask & INT_EP3NAK) ? " ep3nak" : "",
1040 (mask & INT_EP3NAK) ? " ep3nak" : "", 1040 (mask & INT_EP2NAK) ? " ep2nak" : "",
1041 (mask & INT_EP2NAK) ? " ep2nak" : "", 1041 (mask & INT_EP1NAK) ? " ep1nak" : "",
1042 (mask & INT_EP1NAK) ? " ep1nak" : "", 1042 (mask & INT_EP3DATASET) ? " ep3" : "",
1043 (mask & INT_EP3DATASET) ? " ep3" : "", 1043
1044 1044 (mask & INT_EP2DATASET) ? " ep2" : "",
1045 (mask & INT_EP2DATASET) ? " ep2" : "", 1045 (mask & INT_EP1DATASET) ? " ep1" : "",
1046 (mask & INT_EP1DATASET) ? " ep1" : "", 1046 (mask & INT_STATUSNAK) ? " ep0snak" : "",
1047 (mask & INT_STATUSNAK) ? " ep0snak" : "", 1047 (mask & INT_STATUS) ? " ep0status" : "",
1048 (mask & INT_STATUS) ? " ep0status" : "", 1048
1049 1049 (mask & INT_SETUP) ? " setup" : "",
1050 (mask & INT_SETUP) ? " setup" : "", 1050 (mask & INT_ENDPOINT0) ? " ep0" : "",
1051 (mask & INT_ENDPOINT0) ? " ep0" : "", 1051 (mask & INT_USBRESET) ? " reset" : "",
1052 (mask & INT_USBRESET) ? " reset" : "", 1052 (mask & INT_SUSPEND) ? " suspend" : "");
1053 (mask & INT_SUSPEND) ? " suspend" : ""); 1053}
1054
1055static const char *udc_ep_state(enum ep0state state)
1056{
1057 switch (state) {
1058 case EP0_DISCONNECT:
1059 return "ep0_disconnect";
1060 case EP0_IDLE:
1061 return "ep0_idle";
1062 case EP0_IN:
1063 return "ep0_in";
1064 case EP0_OUT:
1065 return "ep0_out";
1066 case EP0_STATUS:
1067 return "ep0_status";
1068 case EP0_STALL:
1069 return "ep0_stall";
1070 case EP0_SUSPEND:
1071 return "ep0_suspend";
1072 }
1073
1074 return "ep0_?";
1054} 1075}
1055 1076
1077static const char *udc_ep_status(u32 status)
1078{
1079 switch (status & EPxSTATUS_EP_MASK) {
1080 case EPxSTATUS_EP_READY:
1081 return "ready";
1082 case EPxSTATUS_EP_DATAIN:
1083 return "packet";
1084 case EPxSTATUS_EP_FULL:
1085 return "full";
1086 case EPxSTATUS_EP_TX_ERR: /* host will retry */
1087 return "tx_err";
1088 case EPxSTATUS_EP_RX_ERR:
1089 return "rx_err";
1090 case EPxSTATUS_EP_BUSY: /* ep0 only */
1091 return "busy";
1092 case EPxSTATUS_EP_STALL:
1093 return "stall";
1094 case EPxSTATUS_EP_INVALID: /* these "can't happen" */
1095 return "invalid";
1096 }
1097
1098 return "?";
1099}
1056 1100
1057static int udc_proc_read(struct seq_file *m, void *v) 1101static int udc_proc_read(struct seq_file *m, void *v)
1058{ 1102{
@@ -1068,29 +1112,18 @@ static int udc_proc_read(struct seq_file *m, void *v)
1068 tmp = readl(&regs->power_detect); 1112 tmp = readl(&regs->power_detect);
1069 is_usb_connected = tmp & PW_DETECT; 1113 is_usb_connected = tmp & PW_DETECT;
1070 seq_printf(m, 1114 seq_printf(m,
1071 "%s - %s\n" 1115 "%s - %s\n"
1072 "%s version: %s %s\n" 1116 "%s version: %s %s\n"
1073 "Gadget driver: %s\n" 1117 "Gadget driver: %s\n"
1074 "Host %s, %s\n" 1118 "Host %s, %s\n"
1075 "\n", 1119 "\n",
1076 pci_name(dev->pdev), driver_desc, 1120 pci_name(dev->pdev), driver_desc,
1077 driver_name, DRIVER_VERSION, dmastr(), 1121 driver_name, DRIVER_VERSION, dmastr(),
1078 dev->driver ? dev->driver->driver.name : "(none)", 1122 dev->driver ? dev->driver->driver.name : "(none)",
1079 is_usb_connected 1123 is_usb_connected
1080 ? ((tmp & PW_PULLUP) ? "full speed" : "powered") 1124 ? ((tmp & PW_PULLUP) ? "full speed" : "powered")
1081 : "disconnected", 1125 : "disconnected",
1082 ({const char *state; 1126 udc_ep_state(dev->ep0state));
1083 switch(dev->ep0state){
1084 case EP0_DISCONNECT: state = "ep0_disconnect"; break;
1085 case EP0_IDLE: state = "ep0_idle"; break;
1086 case EP0_IN: state = "ep0_in"; break;
1087 case EP0_OUT: state = "ep0_out"; break;
1088 case EP0_STATUS: state = "ep0_status"; break;
1089 case EP0_STALL: state = "ep0_stall"; break;
1090 case EP0_SUSPEND: state = "ep0_suspend"; break;
1091 default: state = "ep0_?"; break;
1092 } state; })
1093 );
1094 1127
1095 dump_intmask(m, "int_status", readl(&regs->int_status)); 1128 dump_intmask(m, "int_status", readl(&regs->int_status));
1096 dump_intmask(m, "int_enable", readl(&regs->int_enable)); 1129 dump_intmask(m, "int_enable", readl(&regs->int_enable));
@@ -1099,31 +1132,30 @@ static int udc_proc_read(struct seq_file *m, void *v)
1099 goto done; 1132 goto done;
1100 1133
1101 /* registers for (active) device and ep0 */ 1134 /* registers for (active) device and ep0 */
1102 if (seq_printf(m, "\nirqs %lu\ndataset %02x " 1135 seq_printf(m, "\nirqs %lu\ndataset %02x single.bcs %02x.%02x state %x addr %u\n",
1103 "single.bcs %02x.%02x state %x addr %u\n", 1136 dev->irqs, readl(&regs->DataSet),
1104 dev->irqs, readl(&regs->DataSet), 1137 readl(&regs->EPxSingle), readl(&regs->EPxBCS),
1105 readl(&regs->EPxSingle), readl(&regs->EPxBCS), 1138 readl(&regs->UsbState),
1106 readl(&regs->UsbState), 1139 readl(&regs->address));
1107 readl(&regs->address)) < 0) 1140 if (seq_has_overflowed(m))
1108 goto done; 1141 goto done;
1109 1142
1110 tmp = readl(&regs->dma_master); 1143 tmp = readl(&regs->dma_master);
1111 if (seq_printf(m, 1144 seq_printf(m, "dma %03X =" EIGHTBITS "%s %s\n",
1112 "dma %03X =" EIGHTBITS "%s %s\n", tmp, 1145 tmp,
1113 (tmp & MST_EOPB_DIS) ? " eopb-" : "", 1146 (tmp & MST_EOPB_DIS) ? " eopb-" : "",
1114 (tmp & MST_EOPB_ENA) ? " eopb+" : "", 1147 (tmp & MST_EOPB_ENA) ? " eopb+" : "",
1115 (tmp & MST_TIMEOUT_DIS) ? " tmo-" : "", 1148 (tmp & MST_TIMEOUT_DIS) ? " tmo-" : "",
1116 (tmp & MST_TIMEOUT_ENA) ? " tmo+" : "", 1149 (tmp & MST_TIMEOUT_ENA) ? " tmo+" : "",
1117 1150
1118 (tmp & MST_RD_EOPB) ? " eopb" : "", 1151 (tmp & MST_RD_EOPB) ? " eopb" : "",
1119 (tmp & MST_RD_RESET) ? " in_reset" : "", 1152 (tmp & MST_RD_RESET) ? " in_reset" : "",
1120 (tmp & MST_WR_RESET) ? " out_reset" : "", 1153 (tmp & MST_WR_RESET) ? " out_reset" : "",
1121 (tmp & MST_RD_ENA) ? " IN" : "", 1154 (tmp & MST_RD_ENA) ? " IN" : "",
1122 1155
1123 (tmp & MST_WR_ENA) ? " OUT" : "", 1156 (tmp & MST_WR_ENA) ? " OUT" : "",
1124 (tmp & MST_CONNECTION) 1157 (tmp & MST_CONNECTION) ? "ep1in/ep2out" : "ep1out/ep2in");
1125 ? "ep1in/ep2out" 1158 if (seq_has_overflowed(m))
1126 : "ep1out/ep2in") < 0)
1127 goto done; 1159 goto done;
1128 1160
1129 /* dump endpoint queues */ 1161 /* dump endpoint queues */
@@ -1135,44 +1167,23 @@ static int udc_proc_read(struct seq_file *m, void *v)
1135 continue; 1167 continue;
1136 1168
1137 tmp = readl(ep->reg_status); 1169 tmp = readl(ep->reg_status);
1138 if (seq_printf(m, 1170 seq_printf(m, "%s %s max %u %s, irqs %lu, status %02x (%s) " FOURBITS "\n",
1139 "%s %s max %u %s, irqs %lu, " 1171 ep->ep.name,
1140 "status %02x (%s) " FOURBITS "\n", 1172 ep->is_in ? "in" : "out",
1141 ep->ep.name, 1173 ep->ep.maxpacket,
1142 ep->is_in ? "in" : "out", 1174 ep->dma ? "dma" : "pio",
1143 ep->ep.maxpacket, 1175 ep->irqs,
1144 ep->dma ? "dma" : "pio", 1176 tmp, udc_ep_status(tmp),
1145 ep->irqs, 1177 (tmp & EPxSTATUS_TOGGLE) ? "data1" : "data0",
1146 tmp, ({ char *s; 1178 (tmp & EPxSTATUS_SUSPEND) ? " suspend" : "",
1147 switch (tmp & EPxSTATUS_EP_MASK) { 1179 (tmp & EPxSTATUS_FIFO_DISABLE) ? " disable" : "",
1148 case EPxSTATUS_EP_READY: 1180 (tmp & EPxSTATUS_STAGE_ERROR) ? " ep0stat" : "");
1149 s = "ready"; break; 1181 if (seq_has_overflowed(m))
1150 case EPxSTATUS_EP_DATAIN:
1151 s = "packet"; break;
1152 case EPxSTATUS_EP_FULL:
1153 s = "full"; break;
1154 case EPxSTATUS_EP_TX_ERR: // host will retry
1155 s = "tx_err"; break;
1156 case EPxSTATUS_EP_RX_ERR:
1157 s = "rx_err"; break;
1158 case EPxSTATUS_EP_BUSY: /* ep0 only */
1159 s = "busy"; break;
1160 case EPxSTATUS_EP_STALL:
1161 s = "stall"; break;
1162 case EPxSTATUS_EP_INVALID: // these "can't happen"
1163 s = "invalid"; break;
1164 default:
1165 s = "?"; break;
1166 } s; }),
1167 (tmp & EPxSTATUS_TOGGLE) ? "data1" : "data0",
1168 (tmp & EPxSTATUS_SUSPEND) ? " suspend" : "",
1169 (tmp & EPxSTATUS_FIFO_DISABLE) ? " disable" : "",
1170 (tmp & EPxSTATUS_STAGE_ERROR) ? " ep0stat" : ""
1171 ) < 0)
1172 goto done; 1182 goto done;
1173 1183
1174 if (list_empty(&ep->queue)) { 1184 if (list_empty(&ep->queue)) {
1175 if (seq_puts(m, "\t(nothing queued)\n") < 0) 1185 seq_puts(m, "\t(nothing queued)\n");
1186 if (seq_has_overflowed(m))
1176 goto done; 1187 goto done;
1177 continue; 1188 continue;
1178 } 1189 }
@@ -1187,10 +1198,10 @@ static int udc_proc_read(struct seq_file *m, void *v)
1187 } else 1198 } else
1188 tmp = req->req.actual; 1199 tmp = req->req.actual;
1189 1200
1190 if (seq_printf(m, 1201 seq_printf(m, "\treq %p len %u/%u buf %p\n",
1191 "\treq %p len %u/%u buf %p\n", 1202 &req->req, tmp, req->req.length,
1192 &req->req, tmp, req->req.length, 1203 req->req.buf);
1193 req->req.buf) < 0) 1204 if (seq_has_overflowed(m))
1194 goto done; 1205 goto done;
1195 } 1206 }
1196 } 1207 }
diff --git a/drivers/usb/gadget/udc/lpc32xx_udc.c b/drivers/usb/gadget/udc/lpc32xx_udc.c
index 27fd41333f71..3b6a7852822d 100644
--- a/drivers/usb/gadget/udc/lpc32xx_udc.c
+++ b/drivers/usb/gadget/udc/lpc32xx_udc.c
@@ -1803,23 +1803,14 @@ static int lpc32xx_ep_queue(struct usb_ep *_ep,
1803 req = container_of(_req, struct lpc32xx_request, req); 1803 req = container_of(_req, struct lpc32xx_request, req);
1804 ep = container_of(_ep, struct lpc32xx_ep, ep); 1804 ep = container_of(_ep, struct lpc32xx_ep, ep);
1805 1805
1806 if (!_req || !_req->complete || !_req->buf || 1806 if (!_ep || !_req || !_req->complete || !_req->buf ||
1807 !list_empty(&req->queue)) 1807 !list_empty(&req->queue))
1808 return -EINVAL; 1808 return -EINVAL;
1809 1809
1810 udc = ep->udc; 1810 udc = ep->udc;
1811 1811
1812 if (!_ep) { 1812 if (udc->gadget.speed == USB_SPEED_UNKNOWN)
1813 dev_dbg(udc->dev, "invalid ep\n"); 1813 return -EPIPE;
1814 return -EINVAL;
1815 }
1816
1817
1818 if ((!udc) || (!udc->driver) ||
1819 (udc->gadget.speed == USB_SPEED_UNKNOWN)) {
1820 dev_dbg(udc->dev, "invalid device\n");
1821 return -EINVAL;
1822 }
1823 1814
1824 if (ep->lep) { 1815 if (ep->lep) {
1825 struct lpc32xx_usbd_dd_gad *dd; 1816 struct lpc32xx_usbd_dd_gad *dd;
diff --git a/drivers/usb/gadget/udc/net2280.c b/drivers/usb/gadget/udc/net2280.c
index d2c0bf65e345..9871b90195ad 100644
--- a/drivers/usb/gadget/udc/net2280.c
+++ b/drivers/usb/gadget/udc/net2280.c
@@ -80,6 +80,13 @@ static const char *const ep_name[] = {
80 "ep-e", "ep-f", "ep-g", "ep-h", 80 "ep-e", "ep-f", "ep-g", "ep-h",
81}; 81};
82 82
83/* Endpoint names for usb3380 advance mode */
84static const char *const ep_name_adv[] = {
85 ep0name,
86 "ep1in", "ep2out", "ep3in", "ep4out",
87 "ep1out", "ep2in", "ep3out", "ep4in",
88};
89
83/* mode 0 == ep-{a,b,c,d} 1K fifo each 90/* mode 0 == ep-{a,b,c,d} 1K fifo each
84 * mode 1 == ep-{a,b} 2K fifo each, ep-{c,d} unavailable 91 * mode 1 == ep-{a,b} 2K fifo each, ep-{c,d} unavailable
85 * mode 2 == ep-a 2K fifo, ep-{b,c} 1K each, ep-d unavailable 92 * mode 2 == ep-a 2K fifo, ep-{b,c} 1K each, ep-d unavailable
@@ -138,31 +145,44 @@ net2280_enable(struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
138 u32 max, tmp; 145 u32 max, tmp;
139 unsigned long flags; 146 unsigned long flags;
140 static const u32 ep_key[9] = { 1, 0, 1, 0, 1, 1, 0, 1, 0 }; 147 static const u32 ep_key[9] = { 1, 0, 1, 0, 1, 1, 0, 1, 0 };
148 int ret = 0;
141 149
142 ep = container_of(_ep, struct net2280_ep, ep); 150 ep = container_of(_ep, struct net2280_ep, ep);
143 if (!_ep || !desc || ep->desc || _ep->name == ep0name || 151 if (!_ep || !desc || ep->desc || _ep->name == ep0name ||
144 desc->bDescriptorType != USB_DT_ENDPOINT) 152 desc->bDescriptorType != USB_DT_ENDPOINT) {
153 pr_err("%s: failed at line=%d\n", __func__, __LINE__);
145 return -EINVAL; 154 return -EINVAL;
155 }
146 dev = ep->dev; 156 dev = ep->dev;
147 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) 157 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
148 return -ESHUTDOWN; 158 ret = -ESHUTDOWN;
159 goto print_err;
160 }
149 161
150 /* erratum 0119 workaround ties up an endpoint number */ 162 /* erratum 0119 workaround ties up an endpoint number */
151 if ((desc->bEndpointAddress & 0x0f) == EP_DONTUSE) 163 if ((desc->bEndpointAddress & 0x0f) == EP_DONTUSE) {
152 return -EDOM; 164 ret = -EDOM;
165 goto print_err;
166 }
153 167
154 if (dev->quirks & PLX_SUPERSPEED) { 168 if (dev->quirks & PLX_SUPERSPEED) {
155 if ((desc->bEndpointAddress & 0x0f) >= 0x0c) 169 if ((desc->bEndpointAddress & 0x0f) >= 0x0c) {
156 return -EDOM; 170 ret = -EDOM;
171 goto print_err;
172 }
157 ep->is_in = !!usb_endpoint_dir_in(desc); 173 ep->is_in = !!usb_endpoint_dir_in(desc);
158 if (dev->enhanced_mode && ep->is_in && ep_key[ep->num]) 174 if (dev->enhanced_mode && ep->is_in && ep_key[ep->num]) {
159 return -EINVAL; 175 ret = -EINVAL;
176 goto print_err;
177 }
160 } 178 }
161 179
162 /* sanity check ep-e/ep-f since their fifos are small */ 180 /* sanity check ep-e/ep-f since their fifos are small */
163 max = usb_endpoint_maxp(desc) & 0x1fff; 181 max = usb_endpoint_maxp(desc) & 0x1fff;
164 if (ep->num > 4 && max > 64 && (dev->quirks & PLX_LEGACY)) 182 if (ep->num > 4 && max > 64 && (dev->quirks & PLX_LEGACY)) {
165 return -ERANGE; 183 ret = -ERANGE;
184 goto print_err;
185 }
166 186
167 spin_lock_irqsave(&dev->lock, flags); 187 spin_lock_irqsave(&dev->lock, flags);
168 _ep->maxpacket = max & 0x7ff; 188 _ep->maxpacket = max & 0x7ff;
@@ -192,7 +212,8 @@ net2280_enable(struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
192 (dev->gadget.speed == USB_SPEED_HIGH && max != 512) || 212 (dev->gadget.speed == USB_SPEED_HIGH && max != 512) ||
193 (dev->gadget.speed == USB_SPEED_FULL && max > 64)) { 213 (dev->gadget.speed == USB_SPEED_FULL && max > 64)) {
194 spin_unlock_irqrestore(&dev->lock, flags); 214 spin_unlock_irqrestore(&dev->lock, flags);
195 return -ERANGE; 215 ret = -ERANGE;
216 goto print_err;
196 } 217 }
197 } 218 }
198 ep->is_iso = (tmp == USB_ENDPOINT_XFER_ISOC); 219 ep->is_iso = (tmp == USB_ENDPOINT_XFER_ISOC);
@@ -271,7 +292,11 @@ net2280_enable(struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
271 292
272 /* pci writes may still be posted */ 293 /* pci writes may still be posted */
273 spin_unlock_irqrestore(&dev->lock, flags); 294 spin_unlock_irqrestore(&dev->lock, flags);
274 return 0; 295 return ret;
296
297print_err:
298 dev_err(&ep->dev->pdev->dev, "%s: error=%d\n", __func__, ret);
299 return ret;
275} 300}
276 301
277static int handshake(u32 __iomem *ptr, u32 mask, u32 done, int usec) 302static int handshake(u32 __iomem *ptr, u32 mask, u32 done, int usec)
@@ -426,9 +451,10 @@ static int net2280_disable(struct usb_ep *_ep)
426 unsigned long flags; 451 unsigned long flags;
427 452
428 ep = container_of(_ep, struct net2280_ep, ep); 453 ep = container_of(_ep, struct net2280_ep, ep);
429 if (!_ep || !ep->desc || _ep->name == ep0name) 454 if (!_ep || !ep->desc || _ep->name == ep0name) {
455 pr_err("%s: Invalid ep=%p or ep->desc\n", __func__, _ep);
430 return -EINVAL; 456 return -EINVAL;
431 457 }
432 spin_lock_irqsave(&ep->dev->lock, flags); 458 spin_lock_irqsave(&ep->dev->lock, flags);
433 nuke(ep); 459 nuke(ep);
434 460
@@ -458,8 +484,10 @@ static struct usb_request
458 struct net2280_ep *ep; 484 struct net2280_ep *ep;
459 struct net2280_request *req; 485 struct net2280_request *req;
460 486
461 if (!_ep) 487 if (!_ep) {
488 pr_err("%s: Invalid ep\n", __func__);
462 return NULL; 489 return NULL;
490 }
463 ep = container_of(_ep, struct net2280_ep, ep); 491 ep = container_of(_ep, struct net2280_ep, ep);
464 492
465 req = kzalloc(sizeof(*req), gfp_flags); 493 req = kzalloc(sizeof(*req), gfp_flags);
@@ -491,8 +519,11 @@ static void net2280_free_request(struct usb_ep *_ep, struct usb_request *_req)
491 struct net2280_request *req; 519 struct net2280_request *req;
492 520
493 ep = container_of(_ep, struct net2280_ep, ep); 521 ep = container_of(_ep, struct net2280_ep, ep);
494 if (!_ep || !_req) 522 if (!_ep || !_req) {
523 dev_err(&ep->dev->pdev->dev, "%s: Inavlid ep=%p or req=%p\n",
524 __func__, _ep, _req);
495 return; 525 return;
526 }
496 527
497 req = container_of(_req, struct net2280_request, req); 528 req = container_of(_req, struct net2280_request, req);
498 WARN_ON(!list_empty(&req->queue)); 529 WARN_ON(!list_empty(&req->queue));
@@ -896,35 +927,44 @@ net2280_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
896 struct net2280_ep *ep; 927 struct net2280_ep *ep;
897 struct net2280 *dev; 928 struct net2280 *dev;
898 unsigned long flags; 929 unsigned long flags;
930 int ret = 0;
899 931
900 /* we always require a cpu-view buffer, so that we can 932 /* we always require a cpu-view buffer, so that we can
901 * always use pio (as fallback or whatever). 933 * always use pio (as fallback or whatever).
902 */ 934 */
903 req = container_of(_req, struct net2280_request, req);
904 if (!_req || !_req->complete || !_req->buf ||
905 !list_empty(&req->queue))
906 return -EINVAL;
907 if (_req->length > (~0 & DMA_BYTE_COUNT_MASK))
908 return -EDOM;
909 ep = container_of(_ep, struct net2280_ep, ep); 935 ep = container_of(_ep, struct net2280_ep, ep);
910 if (!_ep || (!ep->desc && ep->num != 0)) 936 if (!_ep || (!ep->desc && ep->num != 0)) {
937 pr_err("%s: Invalid ep=%p or ep->desc\n", __func__, _ep);
911 return -EINVAL; 938 return -EINVAL;
939 }
940 req = container_of(_req, struct net2280_request, req);
941 if (!_req || !_req->complete || !_req->buf ||
942 !list_empty(&req->queue)) {
943 ret = -EINVAL;
944 goto print_err;
945 }
946 if (_req->length > (~0 & DMA_BYTE_COUNT_MASK)) {
947 ret = -EDOM;
948 goto print_err;
949 }
912 dev = ep->dev; 950 dev = ep->dev;
913 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) 951 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
914 return -ESHUTDOWN; 952 ret = -ESHUTDOWN;
953 goto print_err;
954 }
915 955
916 /* FIXME implement PIO fallback for ZLPs with DMA */ 956 /* FIXME implement PIO fallback for ZLPs with DMA */
917 if (ep->dma && _req->length == 0) 957 if (ep->dma && _req->length == 0) {
918 return -EOPNOTSUPP; 958 ret = -EOPNOTSUPP;
959 goto print_err;
960 }
919 961
920 /* set up dma mapping in case the caller didn't */ 962 /* set up dma mapping in case the caller didn't */
921 if (ep->dma) { 963 if (ep->dma) {
922 int ret;
923
924 ret = usb_gadget_map_request(&dev->gadget, _req, 964 ret = usb_gadget_map_request(&dev->gadget, _req,
925 ep->is_in); 965 ep->is_in);
926 if (ret) 966 if (ret)
927 return ret; 967 goto print_err;
928 } 968 }
929 969
930 ep_vdbg(dev, "%s queue req %p, len %d buf %p\n", 970 ep_vdbg(dev, "%s queue req %p, len %d buf %p\n",
@@ -1013,7 +1053,11 @@ done:
1013 spin_unlock_irqrestore(&dev->lock, flags); 1053 spin_unlock_irqrestore(&dev->lock, flags);
1014 1054
1015 /* pci writes may still be posted */ 1055 /* pci writes may still be posted */
1016 return 0; 1056 return ret;
1057
1058print_err:
1059 dev_err(&ep->dev->pdev->dev, "%s: error=%d\n", __func__, ret);
1060 return ret;
1017} 1061}
1018 1062
1019static inline void 1063static inline void
@@ -1134,8 +1178,11 @@ static int net2280_dequeue(struct usb_ep *_ep, struct usb_request *_req)
1134 int stopped; 1178 int stopped;
1135 1179
1136 ep = container_of(_ep, struct net2280_ep, ep); 1180 ep = container_of(_ep, struct net2280_ep, ep);
1137 if (!_ep || (!ep->desc && ep->num != 0) || !_req) 1181 if (!_ep || (!ep->desc && ep->num != 0) || !_req) {
1182 pr_err("%s: Invalid ep=%p or ep->desc or req=%p\n",
1183 __func__, _ep, _req);
1138 return -EINVAL; 1184 return -EINVAL;
1185 }
1139 1186
1140 spin_lock_irqsave(&ep->dev->lock, flags); 1187 spin_lock_irqsave(&ep->dev->lock, flags);
1141 stopped = ep->stopped; 1188 stopped = ep->stopped;
@@ -1157,6 +1204,8 @@ static int net2280_dequeue(struct usb_ep *_ep, struct usb_request *_req)
1157 } 1204 }
1158 if (&req->req != _req) { 1205 if (&req->req != _req) {
1159 spin_unlock_irqrestore(&ep->dev->lock, flags); 1206 spin_unlock_irqrestore(&ep->dev->lock, flags);
1207 dev_err(&ep->dev->pdev->dev, "%s: Request mismatch\n",
1208 __func__);
1160 return -EINVAL; 1209 return -EINVAL;
1161 } 1210 }
1162 1211
@@ -1214,20 +1263,28 @@ net2280_set_halt_and_wedge(struct usb_ep *_ep, int value, int wedged)
1214 int retval = 0; 1263 int retval = 0;
1215 1264
1216 ep = container_of(_ep, struct net2280_ep, ep); 1265 ep = container_of(_ep, struct net2280_ep, ep);
1217 if (!_ep || (!ep->desc && ep->num != 0)) 1266 if (!_ep || (!ep->desc && ep->num != 0)) {
1267 pr_err("%s: Invalid ep=%p or ep->desc\n", __func__, _ep);
1218 return -EINVAL; 1268 return -EINVAL;
1219 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN) 1269 }
1220 return -ESHUTDOWN; 1270 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN) {
1271 retval = -ESHUTDOWN;
1272 goto print_err;
1273 }
1221 if (ep->desc /* not ep0 */ && (ep->desc->bmAttributes & 0x03) 1274 if (ep->desc /* not ep0 */ && (ep->desc->bmAttributes & 0x03)
1222 == USB_ENDPOINT_XFER_ISOC) 1275 == USB_ENDPOINT_XFER_ISOC) {
1223 return -EINVAL; 1276 retval = -EINVAL;
1277 goto print_err;
1278 }
1224 1279
1225 spin_lock_irqsave(&ep->dev->lock, flags); 1280 spin_lock_irqsave(&ep->dev->lock, flags);
1226 if (!list_empty(&ep->queue)) 1281 if (!list_empty(&ep->queue)) {
1227 retval = -EAGAIN; 1282 retval = -EAGAIN;
1228 else if (ep->is_in && value && net2280_fifo_status(_ep) != 0) 1283 goto print_unlock;
1284 } else if (ep->is_in && value && net2280_fifo_status(_ep) != 0) {
1229 retval = -EAGAIN; 1285 retval = -EAGAIN;
1230 else { 1286 goto print_unlock;
1287 } else {
1231 ep_vdbg(ep->dev, "%s %s %s\n", _ep->name, 1288 ep_vdbg(ep->dev, "%s %s %s\n", _ep->name,
1232 value ? "set" : "clear", 1289 value ? "set" : "clear",
1233 wedged ? "wedge" : "halt"); 1290 wedged ? "wedge" : "halt");
@@ -1251,6 +1308,12 @@ net2280_set_halt_and_wedge(struct usb_ep *_ep, int value, int wedged)
1251 spin_unlock_irqrestore(&ep->dev->lock, flags); 1308 spin_unlock_irqrestore(&ep->dev->lock, flags);
1252 1309
1253 return retval; 1310 return retval;
1311
1312print_unlock:
1313 spin_unlock_irqrestore(&ep->dev->lock, flags);
1314print_err:
1315 dev_err(&ep->dev->pdev->dev, "%s: error=%d\n", __func__, retval);
1316 return retval;
1254} 1317}
1255 1318
1256static int net2280_set_halt(struct usb_ep *_ep, int value) 1319static int net2280_set_halt(struct usb_ep *_ep, int value)
@@ -1260,8 +1323,10 @@ static int net2280_set_halt(struct usb_ep *_ep, int value)
1260 1323
1261static int net2280_set_wedge(struct usb_ep *_ep) 1324static int net2280_set_wedge(struct usb_ep *_ep)
1262{ 1325{
1263 if (!_ep || _ep->name == ep0name) 1326 if (!_ep || _ep->name == ep0name) {
1327 pr_err("%s: Invalid ep=%p or ep0\n", __func__, _ep);
1264 return -EINVAL; 1328 return -EINVAL;
1329 }
1265 return net2280_set_halt_and_wedge(_ep, 1, 1); 1330 return net2280_set_halt_and_wedge(_ep, 1, 1);
1266} 1331}
1267 1332
@@ -1271,14 +1336,22 @@ static int net2280_fifo_status(struct usb_ep *_ep)
1271 u32 avail; 1336 u32 avail;
1272 1337
1273 ep = container_of(_ep, struct net2280_ep, ep); 1338 ep = container_of(_ep, struct net2280_ep, ep);
1274 if (!_ep || (!ep->desc && ep->num != 0)) 1339 if (!_ep || (!ep->desc && ep->num != 0)) {
1340 pr_err("%s: Invalid ep=%p or ep->desc\n", __func__, _ep);
1275 return -ENODEV; 1341 return -ENODEV;
1276 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN) 1342 }
1343 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN) {
1344 dev_err(&ep->dev->pdev->dev,
1345 "%s: Invalid driver=%p or speed=%d\n",
1346 __func__, ep->dev->driver, ep->dev->gadget.speed);
1277 return -ESHUTDOWN; 1347 return -ESHUTDOWN;
1348 }
1278 1349
1279 avail = readl(&ep->regs->ep_avail) & (BIT(12) - 1); 1350 avail = readl(&ep->regs->ep_avail) & (BIT(12) - 1);
1280 if (avail > ep->fifo_size) 1351 if (avail > ep->fifo_size) {
1352 dev_err(&ep->dev->pdev->dev, "%s: Fifo overflow\n", __func__);
1281 return -EOVERFLOW; 1353 return -EOVERFLOW;
1354 }
1282 if (ep->is_in) 1355 if (ep->is_in)
1283 avail = ep->fifo_size - avail; 1356 avail = ep->fifo_size - avail;
1284 return avail; 1357 return avail;
@@ -1289,10 +1362,16 @@ static void net2280_fifo_flush(struct usb_ep *_ep)
1289 struct net2280_ep *ep; 1362 struct net2280_ep *ep;
1290 1363
1291 ep = container_of(_ep, struct net2280_ep, ep); 1364 ep = container_of(_ep, struct net2280_ep, ep);
1292 if (!_ep || (!ep->desc && ep->num != 0)) 1365 if (!_ep || (!ep->desc && ep->num != 0)) {
1366 pr_err("%s: Invalid ep=%p or ep->desc\n", __func__, _ep);
1293 return; 1367 return;
1294 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN) 1368 }
1369 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN) {
1370 dev_err(&ep->dev->pdev->dev,
1371 "%s: Invalid driver=%p or speed=%d\n",
1372 __func__, ep->dev->driver, ep->dev->gadget.speed);
1295 return; 1373 return;
1374 }
1296 1375
1297 writel(BIT(FIFO_FLUSH), &ep->regs->ep_stat); 1376 writel(BIT(FIFO_FLUSH), &ep->regs->ep_stat);
1298 (void) readl(&ep->regs->ep_rsp); 1377 (void) readl(&ep->regs->ep_rsp);
@@ -1977,7 +2056,7 @@ static void usb_reinit_338x(struct net2280 *dev)
1977 for (i = 0; i < dev->n_ep; i++) { 2056 for (i = 0; i < dev->n_ep; i++) {
1978 struct net2280_ep *ep = &dev->ep[i]; 2057 struct net2280_ep *ep = &dev->ep[i];
1979 2058
1980 ep->ep.name = ep_name[i]; 2059 ep->ep.name = dev->enhanced_mode ? ep_name_adv[i] : ep_name[i];
1981 ep->dev = dev; 2060 ep->dev = dev;
1982 ep->num = i; 2061 ep->num = i;
1983 2062
@@ -1989,11 +2068,9 @@ static void usb_reinit_338x(struct net2280 *dev)
1989 ep->regs = (struct net2280_ep_regs __iomem *) 2068 ep->regs = (struct net2280_ep_regs __iomem *)
1990 (((void __iomem *)&dev->epregs[ne[i]]) + 2069 (((void __iomem *)&dev->epregs[ne[i]]) +
1991 ep_reg_addr[i]); 2070 ep_reg_addr[i]);
1992 ep->fiforegs = &dev->fiforegs[i];
1993 } else { 2071 } else {
1994 ep->cfg = &dev->epregs[i]; 2072 ep->cfg = &dev->epregs[i];
1995 ep->regs = &dev->epregs[i]; 2073 ep->regs = &dev->epregs[i];
1996 ep->fiforegs = &dev->fiforegs[i];
1997 } 2074 }
1998 2075
1999 ep->fifo_size = (i != 0) ? 2048 : 512; 2076 ep->fifo_size = (i != 0) ? 2048 : 512;
@@ -2186,7 +2263,6 @@ static int net2280_start(struct usb_gadget *_gadget,
2186 dev->ep[i].irqs = 0; 2263 dev->ep[i].irqs = 0;
2187 2264
2188 /* hook up the driver ... */ 2265 /* hook up the driver ... */
2189 dev->softconnect = 1;
2190 driver->driver.bus = NULL; 2266 driver->driver.bus = NULL;
2191 dev->driver = driver; 2267 dev->driver = driver;
2192 2268
@@ -3052,6 +3128,8 @@ next_endpoints:
3052 BIT(PCI_RETRY_ABORT_INTERRUPT)) 3128 BIT(PCI_RETRY_ABORT_INTERRUPT))
3053 3129
3054static void handle_stat1_irqs(struct net2280 *dev, u32 stat) 3130static void handle_stat1_irqs(struct net2280 *dev, u32 stat)
3131__releases(dev->lock)
3132__acquires(dev->lock)
3055{ 3133{
3056 struct net2280_ep *ep; 3134 struct net2280_ep *ep;
3057 u32 tmp, num, mask, scratch; 3135 u32 tmp, num, mask, scratch;
@@ -3373,8 +3451,6 @@ static int net2280_probe(struct pci_dev *pdev, const struct pci_device_id *id)
3373 u32 usbstat; 3451 u32 usbstat;
3374 dev->usb_ext = (struct usb338x_usb_ext_regs __iomem *) 3452 dev->usb_ext = (struct usb338x_usb_ext_regs __iomem *)
3375 (base + 0x00b4); 3453 (base + 0x00b4);
3376 dev->fiforegs = (struct usb338x_fifo_regs __iomem *)
3377 (base + 0x0500);
3378 dev->llregs = (struct usb338x_ll_regs __iomem *) 3454 dev->llregs = (struct usb338x_ll_regs __iomem *)
3379 (base + 0x0700); 3455 (base + 0x0700);
3380 dev->ll_lfps_regs = (struct usb338x_ll_lfps_regs __iomem *) 3456 dev->ll_lfps_regs = (struct usb338x_ll_lfps_regs __iomem *)
diff --git a/drivers/usb/gadget/udc/net2280.h b/drivers/usb/gadget/udc/net2280.h
index ac8d5a20a378..4dff60d34f73 100644
--- a/drivers/usb/gadget/udc/net2280.h
+++ b/drivers/usb/gadget/udc/net2280.h
@@ -96,7 +96,6 @@ struct net2280_ep {
96 struct net2280_ep_regs __iomem *regs; 96 struct net2280_ep_regs __iomem *regs;
97 struct net2280_dma_regs __iomem *dma; 97 struct net2280_dma_regs __iomem *dma;
98 struct net2280_dma *dummy; 98 struct net2280_dma *dummy;
99 struct usb338x_fifo_regs __iomem *fiforegs;
100 dma_addr_t td_dma; /* of dummy */ 99 dma_addr_t td_dma; /* of dummy */
101 struct net2280 *dev; 100 struct net2280 *dev;
102 unsigned long irqs; 101 unsigned long irqs;
@@ -181,7 +180,6 @@ struct net2280 {
181 struct net2280_dma_regs __iomem *dma; 180 struct net2280_dma_regs __iomem *dma;
182 struct net2280_dep_regs __iomem *dep; 181 struct net2280_dep_regs __iomem *dep;
183 struct net2280_ep_regs __iomem *epregs; 182 struct net2280_ep_regs __iomem *epregs;
184 struct usb338x_fifo_regs __iomem *fiforegs;
185 struct usb338x_ll_regs __iomem *llregs; 183 struct usb338x_ll_regs __iomem *llregs;
186 struct usb338x_ll_lfps_regs __iomem *ll_lfps_regs; 184 struct usb338x_ll_lfps_regs __iomem *ll_lfps_regs;
187 struct usb338x_ll_tsn_regs __iomem *ll_tsn_regs; 185 struct usb338x_ll_tsn_regs __iomem *ll_tsn_regs;
diff --git a/drivers/usb/gadget/udc/pxa27x_udc.c b/drivers/usb/gadget/udc/pxa27x_udc.c
index f0ae143dab6d..b51226abade6 100644
--- a/drivers/usb/gadget/udc/pxa27x_udc.c
+++ b/drivers/usb/gadget/udc/pxa27x_udc.c
@@ -93,50 +93,46 @@ static void handle_ep(struct pxa_ep *ep);
93static int state_dbg_show(struct seq_file *s, void *p) 93static int state_dbg_show(struct seq_file *s, void *p)
94{ 94{
95 struct pxa_udc *udc = s->private; 95 struct pxa_udc *udc = s->private;
96 int pos = 0, ret;
97 u32 tmp; 96 u32 tmp;
98 97
99 ret = -ENODEV;
100 if (!udc->driver) 98 if (!udc->driver)
101 goto out; 99 return -ENODEV;
102 100
103 /* basic device status */ 101 /* basic device status */
104 pos += seq_printf(s, DRIVER_DESC "\n" 102 seq_printf(s, DRIVER_DESC "\n"
105 "%s version: %s\nGadget driver: %s\n", 103 "%s version: %s\n"
106 driver_name, DRIVER_VERSION, 104 "Gadget driver: %s\n",
107 udc->driver ? udc->driver->driver.name : "(none)"); 105 driver_name, DRIVER_VERSION,
106 udc->driver ? udc->driver->driver.name : "(none)");
108 107
109 tmp = udc_readl(udc, UDCCR); 108 tmp = udc_readl(udc, UDCCR);
110 pos += seq_printf(s, 109 seq_printf(s,
111 "udccr=0x%0x(%s%s%s%s%s%s%s%s%s%s), " 110 "udccr=0x%0x(%s%s%s%s%s%s%s%s%s%s), con=%d,inter=%d,altinter=%d\n",
112 "con=%d,inter=%d,altinter=%d\n", tmp, 111 tmp,
113 (tmp & UDCCR_OEN) ? " oen":"", 112 (tmp & UDCCR_OEN) ? " oen":"",
114 (tmp & UDCCR_AALTHNP) ? " aalthnp":"", 113 (tmp & UDCCR_AALTHNP) ? " aalthnp":"",
115 (tmp & UDCCR_AHNP) ? " rem" : "", 114 (tmp & UDCCR_AHNP) ? " rem" : "",
116 (tmp & UDCCR_BHNP) ? " rstir" : "", 115 (tmp & UDCCR_BHNP) ? " rstir" : "",
117 (tmp & UDCCR_DWRE) ? " dwre" : "", 116 (tmp & UDCCR_DWRE) ? " dwre" : "",
118 (tmp & UDCCR_SMAC) ? " smac" : "", 117 (tmp & UDCCR_SMAC) ? " smac" : "",
119 (tmp & UDCCR_EMCE) ? " emce" : "", 118 (tmp & UDCCR_EMCE) ? " emce" : "",
120 (tmp & UDCCR_UDR) ? " udr" : "", 119 (tmp & UDCCR_UDR) ? " udr" : "",
121 (tmp & UDCCR_UDA) ? " uda" : "", 120 (tmp & UDCCR_UDA) ? " uda" : "",
122 (tmp & UDCCR_UDE) ? " ude" : "", 121 (tmp & UDCCR_UDE) ? " ude" : "",
123 (tmp & UDCCR_ACN) >> UDCCR_ACN_S, 122 (tmp & UDCCR_ACN) >> UDCCR_ACN_S,
124 (tmp & UDCCR_AIN) >> UDCCR_AIN_S, 123 (tmp & UDCCR_AIN) >> UDCCR_AIN_S,
125 (tmp & UDCCR_AAISN) >> UDCCR_AAISN_S); 124 (tmp & UDCCR_AAISN) >> UDCCR_AAISN_S);
126 /* registers for device and ep0 */ 125 /* registers for device and ep0 */
127 pos += seq_printf(s, "udcicr0=0x%08x udcicr1=0x%08x\n", 126 seq_printf(s, "udcicr0=0x%08x udcicr1=0x%08x\n",
128 udc_readl(udc, UDCICR0), udc_readl(udc, UDCICR1)); 127 udc_readl(udc, UDCICR0), udc_readl(udc, UDCICR1));
129 pos += seq_printf(s, "udcisr0=0x%08x udcisr1=0x%08x\n", 128 seq_printf(s, "udcisr0=0x%08x udcisr1=0x%08x\n",
130 udc_readl(udc, UDCISR0), udc_readl(udc, UDCISR1)); 129 udc_readl(udc, UDCISR0), udc_readl(udc, UDCISR1));
131 pos += seq_printf(s, "udcfnr=%d\n", udc_readl(udc, UDCFNR)); 130 seq_printf(s, "udcfnr=%d\n", udc_readl(udc, UDCFNR));
132 pos += seq_printf(s, "irqs: reset=%lu, suspend=%lu, resume=%lu, " 131 seq_printf(s, "irqs: reset=%lu, suspend=%lu, resume=%lu, reconfig=%lu\n",
133 "reconfig=%lu\n", 132 udc->stats.irqs_reset, udc->stats.irqs_suspend,
134 udc->stats.irqs_reset, udc->stats.irqs_suspend, 133 udc->stats.irqs_resume, udc->stats.irqs_reconfig);
135 udc->stats.irqs_resume, udc->stats.irqs_reconfig); 134
136 135 return 0;
137 ret = 0;
138out:
139 return ret;
140} 136}
141 137
142static int queues_dbg_show(struct seq_file *s, void *p) 138static int queues_dbg_show(struct seq_file *s, void *p)
@@ -144,75 +140,67 @@ static int queues_dbg_show(struct seq_file *s, void *p)
144 struct pxa_udc *udc = s->private; 140 struct pxa_udc *udc = s->private;
145 struct pxa_ep *ep; 141 struct pxa_ep *ep;
146 struct pxa27x_request *req; 142 struct pxa27x_request *req;
147 int pos = 0, i, maxpkt, ret; 143 int i, maxpkt;
148 144
149 ret = -ENODEV;
150 if (!udc->driver) 145 if (!udc->driver)
151 goto out; 146 return -ENODEV;
152 147
153 /* dump endpoint queues */ 148 /* dump endpoint queues */
154 for (i = 0; i < NR_PXA_ENDPOINTS; i++) { 149 for (i = 0; i < NR_PXA_ENDPOINTS; i++) {
155 ep = &udc->pxa_ep[i]; 150 ep = &udc->pxa_ep[i];
156 maxpkt = ep->fifo_size; 151 maxpkt = ep->fifo_size;
157 pos += seq_printf(s, "%-12s max_pkt=%d %s\n", 152 seq_printf(s, "%-12s max_pkt=%d %s\n",
158 EPNAME(ep), maxpkt, "pio"); 153 EPNAME(ep), maxpkt, "pio");
159 154
160 if (list_empty(&ep->queue)) { 155 if (list_empty(&ep->queue)) {
161 pos += seq_printf(s, "\t(nothing queued)\n"); 156 seq_puts(s, "\t(nothing queued)\n");
162 continue; 157 continue;
163 } 158 }
164 159
165 list_for_each_entry(req, &ep->queue, queue) { 160 list_for_each_entry(req, &ep->queue, queue) {
166 pos += seq_printf(s, "\treq %p len %d/%d buf %p\n", 161 seq_printf(s, "\treq %p len %d/%d buf %p\n",
167 &req->req, req->req.actual, 162 &req->req, req->req.actual,
168 req->req.length, req->req.buf); 163 req->req.length, req->req.buf);
169 } 164 }
170 } 165 }
171 166
172 ret = 0; 167 return 0;
173out:
174 return ret;
175} 168}
176 169
177static int eps_dbg_show(struct seq_file *s, void *p) 170static int eps_dbg_show(struct seq_file *s, void *p)
178{ 171{
179 struct pxa_udc *udc = s->private; 172 struct pxa_udc *udc = s->private;
180 struct pxa_ep *ep; 173 struct pxa_ep *ep;
181 int pos = 0, i, ret; 174 int i;
182 u32 tmp; 175 u32 tmp;
183 176
184 ret = -ENODEV;
185 if (!udc->driver) 177 if (!udc->driver)
186 goto out; 178 return -ENODEV;
187 179
188 ep = &udc->pxa_ep[0]; 180 ep = &udc->pxa_ep[0];
189 tmp = udc_ep_readl(ep, UDCCSR); 181 tmp = udc_ep_readl(ep, UDCCSR);
190 pos += seq_printf(s, "udccsr0=0x%03x(%s%s%s%s%s%s%s)\n", tmp, 182 seq_printf(s, "udccsr0=0x%03x(%s%s%s%s%s%s%s)\n",
191 (tmp & UDCCSR0_SA) ? " sa" : "", 183 tmp,
192 (tmp & UDCCSR0_RNE) ? " rne" : "", 184 (tmp & UDCCSR0_SA) ? " sa" : "",
193 (tmp & UDCCSR0_FST) ? " fst" : "", 185 (tmp & UDCCSR0_RNE) ? " rne" : "",
194 (tmp & UDCCSR0_SST) ? " sst" : "", 186 (tmp & UDCCSR0_FST) ? " fst" : "",
195 (tmp & UDCCSR0_DME) ? " dme" : "", 187 (tmp & UDCCSR0_SST) ? " sst" : "",
196 (tmp & UDCCSR0_IPR) ? " ipr" : "", 188 (tmp & UDCCSR0_DME) ? " dme" : "",
197 (tmp & UDCCSR0_OPC) ? " opc" : ""); 189 (tmp & UDCCSR0_IPR) ? " ipr" : "",
190 (tmp & UDCCSR0_OPC) ? " opc" : "");
198 for (i = 0; i < NR_PXA_ENDPOINTS; i++) { 191 for (i = 0; i < NR_PXA_ENDPOINTS; i++) {
199 ep = &udc->pxa_ep[i]; 192 ep = &udc->pxa_ep[i];
200 tmp = i? udc_ep_readl(ep, UDCCR) : udc_readl(udc, UDCCR); 193 tmp = i? udc_ep_readl(ep, UDCCR) : udc_readl(udc, UDCCR);
201 pos += seq_printf(s, "%-12s: " 194 seq_printf(s, "%-12s: IN %lu(%lu reqs), OUT %lu(%lu reqs), irqs=%lu, udccr=0x%08x, udccsr=0x%03x, udcbcr=%d\n",
202 "IN %lu(%lu reqs), OUT %lu(%lu reqs), " 195 EPNAME(ep),
203 "irqs=%lu, udccr=0x%08x, udccsr=0x%03x, " 196 ep->stats.in_bytes, ep->stats.in_ops,
204 "udcbcr=%d\n", 197 ep->stats.out_bytes, ep->stats.out_ops,
205 EPNAME(ep), 198 ep->stats.irqs,
206 ep->stats.in_bytes, ep->stats.in_ops, 199 tmp, udc_ep_readl(ep, UDCCSR),
207 ep->stats.out_bytes, ep->stats.out_ops, 200 udc_ep_readl(ep, UDCBCR));
208 ep->stats.irqs,
209 tmp, udc_ep_readl(ep, UDCCSR),
210 udc_ep_readl(ep, UDCBCR));
211 } 201 }
212 202
213 ret = 0; 203 return 0;
214out:
215 return ret;
216} 204}
217 205
218static int eps_dbg_open(struct inode *inode, struct file *file) 206static int eps_dbg_open(struct inode *inode, struct file *file)
diff --git a/drivers/usb/gadget/udc/udc-core.c b/drivers/usb/gadget/udc/udc-core.c
index 5a81cb086b99..d69c35558f68 100644
--- a/drivers/usb/gadget/udc/udc-core.c
+++ b/drivers/usb/gadget/udc/udc-core.c
@@ -35,6 +35,8 @@
35 * @dev - the child device to the actual controller 35 * @dev - the child device to the actual controller
36 * @gadget - the gadget. For use by the class code 36 * @gadget - the gadget. For use by the class code
37 * @list - for use by the udc class driver 37 * @list - for use by the udc class driver
38 * @vbus - for udcs who care about vbus status, this value is real vbus status;
39 * for udcs who do not care about vbus status, this value is always true
38 * 40 *
39 * This represents the internal data structure which is used by the UDC-class 41 * This represents the internal data structure which is used by the UDC-class
40 * to hold information about udc driver and gadget together. 42 * to hold information about udc driver and gadget together.
@@ -44,6 +46,7 @@ struct usb_udc {
44 struct usb_gadget *gadget; 46 struct usb_gadget *gadget;
45 struct device dev; 47 struct device dev;
46 struct list_head list; 48 struct list_head list;
49 bool vbus;
47}; 50};
48 51
49static struct class *udc_class; 52static struct class *udc_class;
@@ -128,21 +131,11 @@ EXPORT_SYMBOL_GPL(usb_gadget_giveback_request);
128 131
129static void usb_gadget_state_work(struct work_struct *work) 132static void usb_gadget_state_work(struct work_struct *work)
130{ 133{
131 struct usb_gadget *gadget = work_to_gadget(work); 134 struct usb_gadget *gadget = work_to_gadget(work);
132 struct usb_udc *udc = NULL; 135 struct usb_udc *udc = gadget->udc;
133
134 mutex_lock(&udc_lock);
135 list_for_each_entry(udc, &udc_list, list)
136 if (udc->gadget == gadget)
137 goto found;
138 mutex_unlock(&udc_lock);
139
140 return;
141
142found:
143 mutex_unlock(&udc_lock);
144 136
145 sysfs_notify(&udc->dev.kobj, NULL, "state"); 137 if (udc)
138 sysfs_notify(&udc->dev.kobj, NULL, "state");
146} 139}
147 140
148void usb_gadget_set_state(struct usb_gadget *gadget, 141void usb_gadget_set_state(struct usb_gadget *gadget,
@@ -155,6 +148,34 @@ EXPORT_SYMBOL_GPL(usb_gadget_set_state);
155 148
156/* ------------------------------------------------------------------------- */ 149/* ------------------------------------------------------------------------- */
157 150
151static void usb_udc_connect_control(struct usb_udc *udc)
152{
153 if (udc->vbus)
154 usb_gadget_connect(udc->gadget);
155 else
156 usb_gadget_disconnect(udc->gadget);
157}
158
159/**
160 * usb_udc_vbus_handler - updates the udc core vbus status, and try to
161 * connect or disconnect gadget
162 * @gadget: The gadget which vbus change occurs
163 * @status: The vbus status
164 *
165 * The udc driver calls it when it wants to connect or disconnect gadget
166 * according to vbus status.
167 */
168void usb_udc_vbus_handler(struct usb_gadget *gadget, bool status)
169{
170 struct usb_udc *udc = gadget->udc;
171
172 if (udc) {
173 udc->vbus = status;
174 usb_udc_connect_control(udc);
175 }
176}
177EXPORT_SYMBOL_GPL(usb_udc_vbus_handler);
178
158/** 179/**
159 * usb_gadget_udc_reset - notifies the udc core that bus reset occurs 180 * usb_gadget_udc_reset - notifies the udc core that bus reset occurs
160 * @gadget: The gadget which bus reset occurs 181 * @gadget: The gadget which bus reset occurs
@@ -278,6 +299,7 @@ int usb_add_gadget_udc_release(struct device *parent, struct usb_gadget *gadget,
278 goto err3; 299 goto err3;
279 300
280 udc->gadget = gadget; 301 udc->gadget = gadget;
302 gadget->udc = udc;
281 303
282 mutex_lock(&udc_lock); 304 mutex_lock(&udc_lock);
283 list_add_tail(&udc->list, &udc_list); 305 list_add_tail(&udc->list, &udc_list);
@@ -287,6 +309,7 @@ int usb_add_gadget_udc_release(struct device *parent, struct usb_gadget *gadget,
287 goto err4; 309 goto err4;
288 310
289 usb_gadget_set_state(gadget, USB_STATE_NOTATTACHED); 311 usb_gadget_set_state(gadget, USB_STATE_NOTATTACHED);
312 udc->vbus = true;
290 313
291 mutex_unlock(&udc_lock); 314 mutex_unlock(&udc_lock);
292 315
@@ -348,21 +371,14 @@ static void usb_gadget_remove_driver(struct usb_udc *udc)
348 */ 371 */
349void usb_del_gadget_udc(struct usb_gadget *gadget) 372void usb_del_gadget_udc(struct usb_gadget *gadget)
350{ 373{
351 struct usb_udc *udc = NULL; 374 struct usb_udc *udc = gadget->udc;
352 375
353 mutex_lock(&udc_lock); 376 if (!udc)
354 list_for_each_entry(udc, &udc_list, list) 377 return;
355 if (udc->gadget == gadget)
356 goto found;
357
358 dev_err(gadget->dev.parent, "gadget not registered.\n");
359 mutex_unlock(&udc_lock);
360
361 return;
362 378
363found:
364 dev_vdbg(gadget->dev.parent, "unregistering gadget\n"); 379 dev_vdbg(gadget->dev.parent, "unregistering gadget\n");
365 380
381 mutex_lock(&udc_lock);
366 list_del(&udc->list); 382 list_del(&udc->list);
367 mutex_unlock(&udc_lock); 383 mutex_unlock(&udc_lock);
368 384
@@ -397,7 +413,7 @@ static int udc_bind_to_driver(struct usb_udc *udc, struct usb_gadget_driver *dri
397 driver->unbind(udc->gadget); 413 driver->unbind(udc->gadget);
398 goto err1; 414 goto err1;
399 } 415 }
400 usb_gadget_connect(udc->gadget); 416 usb_udc_connect_control(udc);
401 417
402 kobject_uevent(&udc->dev.kobj, KOBJ_CHANGE); 418 kobject_uevent(&udc->dev.kobj, KOBJ_CHANGE);
403 return 0; 419 return 0;