Commit | Line | Data |
---|---|---|
25a010c8 CN |
1 | /* |
2 | * printer.c -- Printer gadget driver | |
3 | * | |
4 | * Copyright (C) 2003-2005 David Brownell | |
5 | * Copyright (C) 2006 Craig W. Nadler | |
6 | * | |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation; either version 2 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program; if not, write to the Free Software | |
19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
20 | */ | |
21 | ||
22 | #include <linux/module.h> | |
23 | #include <linux/kernel.h> | |
24 | #include <linux/delay.h> | |
25 | #include <linux/ioport.h> | |
26 | #include <linux/sched.h> | |
27 | #include <linux/slab.h> | |
28 | #include <linux/smp_lock.h> | |
29 | #include <linux/errno.h> | |
30 | #include <linux/init.h> | |
31 | #include <linux/timer.h> | |
32 | #include <linux/list.h> | |
33 | #include <linux/interrupt.h> | |
34 | #include <linux/utsname.h> | |
35 | #include <linux/device.h> | |
36 | #include <linux/moduleparam.h> | |
37 | #include <linux/fs.h> | |
38 | #include <linux/poll.h> | |
39 | #include <linux/types.h> | |
40 | #include <linux/ctype.h> | |
41 | #include <linux/cdev.h> | |
42 | ||
43 | #include <asm/byteorder.h> | |
44 | #include <linux/io.h> | |
45 | #include <linux/irq.h> | |
46 | #include <asm/system.h> | |
47 | #include <linux/uaccess.h> | |
48 | #include <asm/unaligned.h> | |
49 | ||
50 | #include <linux/usb/ch9.h> | |
51 | #include <linux/usb/gadget.h> | |
52 | #include <linux/usb/g_printer.h> | |
53 | ||
54 | #include "gadget_chips.h" | |
55 | ||
0a56b03f DB |
56 | |
57 | /* | |
58 | * Kbuild is not very cooperative with respect to linking separately | |
59 | * compiled library objects into one module. So for now we won't use | |
60 | * separate compilation ... ensuring init/exit sections work to shrink | |
61 | * the runtime footprint, and giving us at least some parts of what | |
62 | * a "gcc --combine ... part1.c part2.c part3.c ... " build would. | |
63 | */ | |
64 | #include "usbstring.c" | |
65 | #include "config.c" | |
66 | #include "epautoconf.c" | |
67 | ||
68 | /*-------------------------------------------------------------------------*/ | |
69 | ||
25a010c8 CN |
70 | #define DRIVER_DESC "Printer Gadget" |
71 | #define DRIVER_VERSION "2007 OCT 06" | |
72 | ||
73 | static const char shortname [] = "printer"; | |
74 | static const char driver_desc [] = DRIVER_DESC; | |
75 | ||
76 | static dev_t g_printer_devno; | |
77 | ||
78 | static struct class *usb_gadget_class; | |
79 | ||
80 | /*-------------------------------------------------------------------------*/ | |
81 | ||
82 | struct printer_dev { | |
83 | spinlock_t lock; /* lock this structure */ | |
84 | /* lock buffer lists during read/write calls */ | |
85 | spinlock_t lock_printer_io; | |
86 | struct usb_gadget *gadget; | |
87 | struct usb_request *req; /* for control responses */ | |
88 | u8 config; | |
89 | s8 interface; | |
90 | struct usb_ep *in_ep, *out_ep; | |
91 | const struct usb_endpoint_descriptor | |
92 | *in, *out; | |
93 | struct list_head rx_reqs; /* List of free RX structs */ | |
94 | struct list_head rx_reqs_active; /* List of Active RX xfers */ | |
95 | struct list_head rx_buffers; /* List of completed xfers */ | |
96 | /* wait until there is data to be read. */ | |
97 | wait_queue_head_t rx_wait; | |
98 | struct list_head tx_reqs; /* List of free TX structs */ | |
99 | struct list_head tx_reqs_active; /* List of Active TX xfers */ | |
100 | /* Wait until there are write buffers available to use. */ | |
101 | wait_queue_head_t tx_wait; | |
102 | /* Wait until all write buffers have been sent. */ | |
103 | wait_queue_head_t tx_flush_wait; | |
104 | struct usb_request *current_rx_req; | |
105 | size_t current_rx_bytes; | |
106 | u8 *current_rx_buf; | |
107 | u8 printer_status; | |
108 | u8 reset_printer; | |
25a010c8 CN |
109 | struct cdev printer_cdev; |
110 | struct device *pdev; | |
111 | u8 printer_cdev_open; | |
112 | wait_queue_head_t wait; | |
113 | }; | |
114 | ||
115 | static struct printer_dev usb_printer_gadget; | |
116 | ||
117 | /*-------------------------------------------------------------------------*/ | |
118 | ||
119 | /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!! | |
120 | * Instead: allocate your own, using normal USB-IF procedures. | |
121 | */ | |
122 | ||
123 | /* Thanks to NetChip Technologies for donating this product ID. | |
124 | */ | |
125 | #define PRINTER_VENDOR_NUM 0x0525 /* NetChip */ | |
126 | #define PRINTER_PRODUCT_NUM 0xa4a8 /* Linux-USB Printer Gadget */ | |
127 | ||
128 | /* Some systems will want different product identifers published in the | |
129 | * device descriptor, either numbers or strings or both. These string | |
130 | * parameters are in UTF-8 (superset of ASCII's 7 bit characters). | |
131 | */ | |
132 | ||
133 | static ushort __initdata idVendor; | |
134 | module_param(idVendor, ushort, S_IRUGO); | |
135 | MODULE_PARM_DESC(idVendor, "USB Vendor ID"); | |
136 | ||
137 | static ushort __initdata idProduct; | |
138 | module_param(idProduct, ushort, S_IRUGO); | |
139 | MODULE_PARM_DESC(idProduct, "USB Product ID"); | |
140 | ||
141 | static ushort __initdata bcdDevice; | |
142 | module_param(bcdDevice, ushort, S_IRUGO); | |
143 | MODULE_PARM_DESC(bcdDevice, "USB Device version (BCD)"); | |
144 | ||
145 | static char *__initdata iManufacturer; | |
146 | module_param(iManufacturer, charp, S_IRUGO); | |
147 | MODULE_PARM_DESC(iManufacturer, "USB Manufacturer string"); | |
148 | ||
149 | static char *__initdata iProduct; | |
150 | module_param(iProduct, charp, S_IRUGO); | |
151 | MODULE_PARM_DESC(iProduct, "USB Product string"); | |
152 | ||
153 | static char *__initdata iSerialNum; | |
154 | module_param(iSerialNum, charp, S_IRUGO); | |
155 | MODULE_PARM_DESC(iSerialNum, "1"); | |
156 | ||
157 | static char *__initdata iPNPstring; | |
158 | module_param(iPNPstring, charp, S_IRUGO); | |
159 | MODULE_PARM_DESC(iPNPstring, "MFG:linux;MDL:g_printer;CLS:PRINTER;SN:1;"); | |
160 | ||
161 | /* Number of requests to allocate per endpoint, not used for ep0. */ | |
162 | static unsigned qlen = 10; | |
163 | module_param(qlen, uint, S_IRUGO|S_IWUSR); | |
164 | ||
165 | #define QLEN qlen | |
166 | ||
167 | #ifdef CONFIG_USB_GADGET_DUALSPEED | |
168 | #define DEVSPEED USB_SPEED_HIGH | |
169 | #else /* full speed (low speed doesn't do bulk) */ | |
170 | #define DEVSPEED USB_SPEED_FULL | |
171 | #endif | |
172 | ||
173 | /*-------------------------------------------------------------------------*/ | |
174 | ||
175 | #define xprintk(d, level, fmt, args...) \ | |
176 | printk(level "%s: " fmt, DRIVER_DESC, ## args) | |
177 | ||
178 | #ifdef DEBUG | |
179 | #define DBG(dev, fmt, args...) \ | |
180 | xprintk(dev, KERN_DEBUG, fmt, ## args) | |
181 | #else | |
182 | #define DBG(dev, fmt, args...) \ | |
183 | do { } while (0) | |
184 | #endif /* DEBUG */ | |
185 | ||
186 | #ifdef VERBOSE | |
187 | #define VDBG(dev, fmt, args...) \ | |
188 | xprintk(dev, KERN_DEBUG, fmt, ## args) | |
189 | #else | |
190 | #define VDBG(dev, fmt, args...) \ | |
191 | do { } while (0) | |
192 | #endif /* VERBOSE */ | |
193 | ||
194 | #define ERROR(dev, fmt, args...) \ | |
195 | xprintk(dev, KERN_ERR, fmt, ## args) | |
b6c63937 | 196 | #define WARNING(dev, fmt, args...) \ |
25a010c8 CN |
197 | xprintk(dev, KERN_WARNING, fmt, ## args) |
198 | #define INFO(dev, fmt, args...) \ | |
199 | xprintk(dev, KERN_INFO, fmt, ## args) | |
200 | ||
201 | /*-------------------------------------------------------------------------*/ | |
202 | ||
203 | /* USB DRIVER HOOKUP (to the hardware driver, below us), mostly | |
204 | * ep0 implementation: descriptors, config management, setup(). | |
205 | * also optional class-specific notification interrupt transfer. | |
206 | */ | |
207 | ||
208 | /* | |
209 | * DESCRIPTORS ... most are static, but strings and (full) configuration | |
210 | * descriptors are built on demand. | |
211 | */ | |
212 | ||
213 | #define STRING_MANUFACTURER 1 | |
214 | #define STRING_PRODUCT 2 | |
215 | #define STRING_SERIALNUM 3 | |
216 | ||
217 | /* holds our biggest descriptor */ | |
218 | #define USB_DESC_BUFSIZE 256 | |
219 | #define USB_BUFSIZE 8192 | |
220 | ||
221 | /* This device advertises one configuration. */ | |
222 | #define DEV_CONFIG_VALUE 1 | |
223 | #define PRINTER_INTERFACE 0 | |
224 | ||
225 | static struct usb_device_descriptor device_desc = { | |
226 | .bLength = sizeof device_desc, | |
227 | .bDescriptorType = USB_DT_DEVICE, | |
228 | .bcdUSB = __constant_cpu_to_le16(0x0200), | |
229 | .bDeviceClass = USB_CLASS_PER_INTERFACE, | |
230 | .bDeviceSubClass = 0, | |
231 | .bDeviceProtocol = 0, | |
232 | .idVendor = __constant_cpu_to_le16(PRINTER_VENDOR_NUM), | |
233 | .idProduct = __constant_cpu_to_le16(PRINTER_PRODUCT_NUM), | |
234 | .iManufacturer = STRING_MANUFACTURER, | |
235 | .iProduct = STRING_PRODUCT, | |
236 | .iSerialNumber = STRING_SERIALNUM, | |
237 | .bNumConfigurations = 1 | |
238 | }; | |
239 | ||
240 | static struct usb_otg_descriptor otg_desc = { | |
241 | .bLength = sizeof otg_desc, | |
242 | .bDescriptorType = USB_DT_OTG, | |
243 | .bmAttributes = USB_OTG_SRP | |
244 | }; | |
245 | ||
246 | static struct usb_config_descriptor config_desc = { | |
247 | .bLength = sizeof config_desc, | |
248 | .bDescriptorType = USB_DT_CONFIG, | |
249 | ||
250 | /* compute wTotalLength on the fly */ | |
251 | .bNumInterfaces = 1, | |
252 | .bConfigurationValue = DEV_CONFIG_VALUE, | |
253 | .iConfiguration = 0, | |
254 | .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER, | |
255 | .bMaxPower = 1 /* Self-Powered */ | |
256 | }; | |
257 | ||
258 | static struct usb_interface_descriptor intf_desc = { | |
259 | .bLength = sizeof intf_desc, | |
260 | .bDescriptorType = USB_DT_INTERFACE, | |
261 | .bInterfaceNumber = PRINTER_INTERFACE, | |
262 | .bNumEndpoints = 2, | |
263 | .bInterfaceClass = USB_CLASS_PRINTER, | |
264 | .bInterfaceSubClass = 1, /* Printer Sub-Class */ | |
265 | .bInterfaceProtocol = 2, /* Bi-Directional */ | |
266 | .iInterface = 0 | |
267 | }; | |
268 | ||
269 | static struct usb_endpoint_descriptor fs_ep_in_desc = { | |
270 | .bLength = USB_DT_ENDPOINT_SIZE, | |
271 | .bDescriptorType = USB_DT_ENDPOINT, | |
272 | .bEndpointAddress = USB_DIR_IN, | |
273 | .bmAttributes = USB_ENDPOINT_XFER_BULK | |
274 | }; | |
275 | ||
276 | static struct usb_endpoint_descriptor fs_ep_out_desc = { | |
277 | .bLength = USB_DT_ENDPOINT_SIZE, | |
278 | .bDescriptorType = USB_DT_ENDPOINT, | |
279 | .bEndpointAddress = USB_DIR_OUT, | |
280 | .bmAttributes = USB_ENDPOINT_XFER_BULK | |
281 | }; | |
282 | ||
283 | static const struct usb_descriptor_header *fs_printer_function [11] = { | |
284 | (struct usb_descriptor_header *) &otg_desc, | |
285 | (struct usb_descriptor_header *) &intf_desc, | |
286 | (struct usb_descriptor_header *) &fs_ep_in_desc, | |
287 | (struct usb_descriptor_header *) &fs_ep_out_desc, | |
288 | NULL | |
289 | }; | |
290 | ||
291 | #ifdef CONFIG_USB_GADGET_DUALSPEED | |
292 | ||
293 | /* | |
294 | * usb 2.0 devices need to expose both high speed and full speed | |
295 | * descriptors, unless they only run at full speed. | |
296 | */ | |
297 | ||
298 | static struct usb_endpoint_descriptor hs_ep_in_desc = { | |
299 | .bLength = USB_DT_ENDPOINT_SIZE, | |
300 | .bDescriptorType = USB_DT_ENDPOINT, | |
301 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | |
302 | .wMaxPacketSize = __constant_cpu_to_le16(512) | |
303 | }; | |
304 | ||
305 | static struct usb_endpoint_descriptor hs_ep_out_desc = { | |
306 | .bLength = USB_DT_ENDPOINT_SIZE, | |
307 | .bDescriptorType = USB_DT_ENDPOINT, | |
308 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | |
309 | .wMaxPacketSize = __constant_cpu_to_le16(512) | |
310 | }; | |
311 | ||
312 | static struct usb_qualifier_descriptor dev_qualifier = { | |
313 | .bLength = sizeof dev_qualifier, | |
314 | .bDescriptorType = USB_DT_DEVICE_QUALIFIER, | |
315 | .bcdUSB = __constant_cpu_to_le16(0x0200), | |
316 | .bDeviceClass = USB_CLASS_PRINTER, | |
317 | .bNumConfigurations = 1 | |
318 | }; | |
319 | ||
320 | static const struct usb_descriptor_header *hs_printer_function [11] = { | |
321 | (struct usb_descriptor_header *) &otg_desc, | |
322 | (struct usb_descriptor_header *) &intf_desc, | |
323 | (struct usb_descriptor_header *) &hs_ep_in_desc, | |
324 | (struct usb_descriptor_header *) &hs_ep_out_desc, | |
325 | NULL | |
326 | }; | |
327 | ||
328 | /* maxpacket and other transfer characteristics vary by speed. */ | |
329 | #define ep_desc(g, hs, fs) (((g)->speed == USB_SPEED_HIGH)?(hs):(fs)) | |
330 | ||
331 | #else | |
332 | ||
333 | /* if there's no high speed support, maxpacket doesn't change. */ | |
334 | #define ep_desc(g, hs, fs) (((void)(g)), (fs)) | |
335 | ||
336 | #endif /* !CONFIG_USB_GADGET_DUALSPEED */ | |
337 | ||
338 | /*-------------------------------------------------------------------------*/ | |
339 | ||
340 | /* descriptors that are built on-demand */ | |
341 | ||
342 | static char manufacturer [50]; | |
343 | static char product_desc [40] = DRIVER_DESC; | |
344 | static char serial_num [40] = "1"; | |
345 | static char pnp_string [1024] = | |
346 | "XXMFG:linux;MDL:g_printer;CLS:PRINTER;SN:1;"; | |
347 | ||
348 | /* static strings, in UTF-8 */ | |
349 | static struct usb_string strings [] = { | |
350 | { STRING_MANUFACTURER, manufacturer, }, | |
351 | { STRING_PRODUCT, product_desc, }, | |
352 | { STRING_SERIALNUM, serial_num, }, | |
353 | { } /* end of list */ | |
354 | }; | |
355 | ||
356 | static struct usb_gadget_strings stringtab = { | |
357 | .language = 0x0409, /* en-us */ | |
358 | .strings = strings, | |
359 | }; | |
360 | ||
361 | /*-------------------------------------------------------------------------*/ | |
362 | ||
363 | static struct usb_request * | |
364 | printer_req_alloc(struct usb_ep *ep, unsigned len, gfp_t gfp_flags) | |
365 | { | |
366 | struct usb_request *req; | |
367 | ||
368 | req = usb_ep_alloc_request(ep, gfp_flags); | |
369 | ||
370 | if (req != NULL) { | |
371 | req->length = len; | |
372 | req->buf = kmalloc(len, gfp_flags); | |
373 | if (req->buf == NULL) { | |
374 | usb_ep_free_request(ep, req); | |
375 | return NULL; | |
376 | } | |
377 | } | |
378 | ||
379 | return req; | |
380 | } | |
381 | ||
382 | static void | |
383 | printer_req_free(struct usb_ep *ep, struct usb_request *req) | |
384 | { | |
385 | if (ep != NULL && req != NULL) { | |
386 | kfree(req->buf); | |
387 | usb_ep_free_request(ep, req); | |
388 | } | |
389 | } | |
390 | ||
391 | /*-------------------------------------------------------------------------*/ | |
392 | ||
393 | static void rx_complete(struct usb_ep *ep, struct usb_request *req) | |
394 | { | |
395 | struct printer_dev *dev = ep->driver_data; | |
396 | int status = req->status; | |
397 | unsigned long flags; | |
398 | ||
399 | spin_lock_irqsave(&dev->lock, flags); | |
400 | ||
401 | list_del_init(&req->list); /* Remode from Active List */ | |
402 | ||
403 | switch (status) { | |
404 | ||
405 | /* normal completion */ | |
406 | case 0: | |
cc901bbb CN |
407 | if (req->actual > 0) { |
408 | list_add_tail(&req->list, &dev->rx_buffers); | |
409 | DBG(dev, "G_Printer : rx length %d\n", req->actual); | |
410 | } else { | |
411 | list_add(&req->list, &dev->rx_reqs); | |
412 | } | |
25a010c8 CN |
413 | break; |
414 | ||
415 | /* software-driven interface shutdown */ | |
416 | case -ECONNRESET: /* unlink */ | |
417 | case -ESHUTDOWN: /* disconnect etc */ | |
418 | VDBG(dev, "rx shutdown, code %d\n", status); | |
419 | list_add(&req->list, &dev->rx_reqs); | |
420 | break; | |
421 | ||
422 | /* for hardware automagic (such as pxa) */ | |
423 | case -ECONNABORTED: /* endpoint reset */ | |
424 | DBG(dev, "rx %s reset\n", ep->name); | |
425 | list_add(&req->list, &dev->rx_reqs); | |
426 | break; | |
427 | ||
428 | /* data overrun */ | |
429 | case -EOVERFLOW: | |
430 | /* FALLTHROUGH */ | |
431 | ||
432 | default: | |
433 | DBG(dev, "rx status %d\n", status); | |
434 | list_add(&req->list, &dev->rx_reqs); | |
435 | break; | |
436 | } | |
cc901bbb CN |
437 | |
438 | wake_up_interruptible(&dev->rx_wait); | |
25a010c8 CN |
439 | spin_unlock_irqrestore(&dev->lock, flags); |
440 | } | |
441 | ||
442 | static void tx_complete(struct usb_ep *ep, struct usb_request *req) | |
443 | { | |
444 | struct printer_dev *dev = ep->driver_data; | |
445 | ||
446 | switch (req->status) { | |
447 | default: | |
448 | VDBG(dev, "tx err %d\n", req->status); | |
449 | /* FALLTHROUGH */ | |
450 | case -ECONNRESET: /* unlink */ | |
451 | case -ESHUTDOWN: /* disconnect etc */ | |
452 | break; | |
453 | case 0: | |
454 | break; | |
455 | } | |
456 | ||
457 | spin_lock(&dev->lock); | |
458 | /* Take the request struct off the active list and put it on the | |
459 | * free list. | |
460 | */ | |
461 | list_del_init(&req->list); | |
462 | list_add(&req->list, &dev->tx_reqs); | |
463 | wake_up_interruptible(&dev->tx_wait); | |
464 | if (likely(list_empty(&dev->tx_reqs_active))) | |
465 | wake_up_interruptible(&dev->tx_flush_wait); | |
466 | ||
467 | spin_unlock(&dev->lock); | |
468 | } | |
469 | ||
470 | /*-------------------------------------------------------------------------*/ | |
471 | ||
472 | static int | |
473 | printer_open(struct inode *inode, struct file *fd) | |
474 | { | |
475 | struct printer_dev *dev; | |
476 | unsigned long flags; | |
477 | int ret = -EBUSY; | |
478 | ||
b2f2ba01 | 479 | lock_kernel(); |
25a010c8 CN |
480 | dev = container_of(inode->i_cdev, struct printer_dev, printer_cdev); |
481 | ||
482 | spin_lock_irqsave(&dev->lock, flags); | |
483 | ||
484 | if (!dev->printer_cdev_open) { | |
485 | dev->printer_cdev_open = 1; | |
486 | fd->private_data = dev; | |
487 | ret = 0; | |
488 | /* Change the printer status to show that it's on-line. */ | |
489 | dev->printer_status |= PRINTER_SELECTED; | |
490 | } | |
491 | ||
492 | spin_unlock_irqrestore(&dev->lock, flags); | |
493 | ||
494 | DBG(dev, "printer_open returned %x\n", ret); | |
b2f2ba01 | 495 | unlock_kernel(); |
25a010c8 CN |
496 | return ret; |
497 | } | |
498 | ||
499 | static int | |
500 | printer_close(struct inode *inode, struct file *fd) | |
501 | { | |
502 | struct printer_dev *dev = fd->private_data; | |
503 | unsigned long flags; | |
504 | ||
505 | spin_lock_irqsave(&dev->lock, flags); | |
506 | dev->printer_cdev_open = 0; | |
507 | fd->private_data = NULL; | |
508 | /* Change printer status to show that the printer is off-line. */ | |
509 | dev->printer_status &= ~PRINTER_SELECTED; | |
510 | spin_unlock_irqrestore(&dev->lock, flags); | |
511 | ||
512 | DBG(dev, "printer_close\n"); | |
513 | ||
514 | return 0; | |
515 | } | |
516 | ||
cc901bbb CN |
517 | /* This function must be called with interrupts turned off. */ |
518 | static void | |
519 | setup_rx_reqs(struct printer_dev *dev) | |
520 | { | |
521 | struct usb_request *req; | |
522 | ||
523 | while (likely(!list_empty(&dev->rx_reqs))) { | |
524 | int error; | |
525 | ||
526 | req = container_of(dev->rx_reqs.next, | |
527 | struct usb_request, list); | |
528 | list_del_init(&req->list); | |
529 | ||
530 | /* The USB Host sends us whatever amount of data it wants to | |
531 | * so we always set the length field to the full USB_BUFSIZE. | |
532 | * If the amount of data is more than the read() caller asked | |
533 | * for it will be stored in the request buffer until it is | |
534 | * asked for by read(). | |
535 | */ | |
536 | req->length = USB_BUFSIZE; | |
537 | req->complete = rx_complete; | |
538 | ||
539 | error = usb_ep_queue(dev->out_ep, req, GFP_ATOMIC); | |
540 | if (error) { | |
541 | DBG(dev, "rx submit --> %d\n", error); | |
542 | list_add(&req->list, &dev->rx_reqs); | |
543 | break; | |
544 | } else { | |
545 | list_add(&req->list, &dev->rx_reqs_active); | |
546 | } | |
547 | } | |
548 | } | |
549 | ||
25a010c8 CN |
550 | static ssize_t |
551 | printer_read(struct file *fd, char __user *buf, size_t len, loff_t *ptr) | |
552 | { | |
553 | struct printer_dev *dev = fd->private_data; | |
554 | unsigned long flags; | |
555 | size_t size; | |
556 | size_t bytes_copied; | |
557 | struct usb_request *req; | |
558 | /* This is a pointer to the current USB rx request. */ | |
559 | struct usb_request *current_rx_req; | |
560 | /* This is the number of bytes in the current rx buffer. */ | |
561 | size_t current_rx_bytes; | |
562 | /* This is a pointer to the current rx buffer. */ | |
563 | u8 *current_rx_buf; | |
564 | ||
565 | if (len == 0) | |
566 | return -EINVAL; | |
567 | ||
568 | DBG(dev, "printer_read trying to read %d bytes\n", (int)len); | |
569 | ||
570 | spin_lock(&dev->lock_printer_io); | |
571 | spin_lock_irqsave(&dev->lock, flags); | |
572 | ||
573 | /* We will use this flag later to check if a printer reset happened | |
574 | * after we turn interrupts back on. | |
575 | */ | |
576 | dev->reset_printer = 0; | |
577 | ||
cc901bbb | 578 | setup_rx_reqs(dev); |
25a010c8 CN |
579 | |
580 | bytes_copied = 0; | |
581 | current_rx_req = dev->current_rx_req; | |
582 | current_rx_bytes = dev->current_rx_bytes; | |
583 | current_rx_buf = dev->current_rx_buf; | |
584 | dev->current_rx_req = NULL; | |
585 | dev->current_rx_bytes = 0; | |
586 | dev->current_rx_buf = NULL; | |
587 | ||
588 | /* Check if there is any data in the read buffers. Please note that | |
589 | * current_rx_bytes is the number of bytes in the current rx buffer. | |
590 | * If it is zero then check if there are any other rx_buffers that | |
591 | * are on the completed list. We are only out of data if all rx | |
592 | * buffers are empty. | |
593 | */ | |
594 | if ((current_rx_bytes == 0) && | |
595 | (likely(list_empty(&dev->rx_buffers)))) { | |
596 | /* Turn interrupts back on before sleeping. */ | |
597 | spin_unlock_irqrestore(&dev->lock, flags); | |
598 | ||
599 | /* | |
600 | * If no data is available check if this is a NON-Blocking | |
601 | * call or not. | |
602 | */ | |
603 | if (fd->f_flags & (O_NONBLOCK|O_NDELAY)) { | |
604 | spin_unlock(&dev->lock_printer_io); | |
605 | return -EAGAIN; | |
606 | } | |
607 | ||
608 | /* Sleep until data is available */ | |
609 | wait_event_interruptible(dev->rx_wait, | |
610 | (likely(!list_empty(&dev->rx_buffers)))); | |
611 | spin_lock_irqsave(&dev->lock, flags); | |
612 | } | |
613 | ||
614 | /* We have data to return then copy it to the caller's buffer.*/ | |
615 | while ((current_rx_bytes || likely(!list_empty(&dev->rx_buffers))) | |
616 | && len) { | |
617 | if (current_rx_bytes == 0) { | |
618 | req = container_of(dev->rx_buffers.next, | |
619 | struct usb_request, list); | |
620 | list_del_init(&req->list); | |
621 | ||
622 | if (req->actual && req->buf) { | |
623 | current_rx_req = req; | |
624 | current_rx_bytes = req->actual; | |
625 | current_rx_buf = req->buf; | |
626 | } else { | |
627 | list_add(&req->list, &dev->rx_reqs); | |
628 | continue; | |
629 | } | |
630 | } | |
631 | ||
632 | /* Don't leave irqs off while doing memory copies */ | |
633 | spin_unlock_irqrestore(&dev->lock, flags); | |
634 | ||
635 | if (len > current_rx_bytes) | |
636 | size = current_rx_bytes; | |
637 | else | |
638 | size = len; | |
639 | ||
640 | size -= copy_to_user(buf, current_rx_buf, size); | |
641 | bytes_copied += size; | |
642 | len -= size; | |
643 | buf += size; | |
644 | ||
645 | spin_lock_irqsave(&dev->lock, flags); | |
646 | ||
cc901bbb | 647 | /* We've disconnected or reset so return. */ |
25a010c8 | 648 | if (dev->reset_printer) { |
cc901bbb | 649 | list_add(¤t_rx_req->list, &dev->rx_reqs); |
25a010c8 CN |
650 | spin_unlock_irqrestore(&dev->lock, flags); |
651 | spin_unlock(&dev->lock_printer_io); | |
652 | return -EAGAIN; | |
653 | } | |
654 | ||
655 | /* If we not returning all the data left in this RX request | |
656 | * buffer then adjust the amount of data left in the buffer. | |
657 | * Othewise if we are done with this RX request buffer then | |
658 | * requeue it to get any incoming data from the USB host. | |
659 | */ | |
660 | if (size < current_rx_bytes) { | |
661 | current_rx_bytes -= size; | |
662 | current_rx_buf += size; | |
663 | } else { | |
664 | list_add(¤t_rx_req->list, &dev->rx_reqs); | |
665 | current_rx_bytes = 0; | |
666 | current_rx_buf = NULL; | |
667 | current_rx_req = NULL; | |
668 | } | |
669 | } | |
670 | ||
671 | dev->current_rx_req = current_rx_req; | |
672 | dev->current_rx_bytes = current_rx_bytes; | |
673 | dev->current_rx_buf = current_rx_buf; | |
674 | ||
675 | spin_unlock_irqrestore(&dev->lock, flags); | |
676 | spin_unlock(&dev->lock_printer_io); | |
677 | ||
678 | DBG(dev, "printer_read returned %d bytes\n", (int)bytes_copied); | |
679 | ||
680 | if (bytes_copied) | |
681 | return bytes_copied; | |
682 | else | |
683 | return -EAGAIN; | |
684 | } | |
685 | ||
686 | static ssize_t | |
687 | printer_write(struct file *fd, const char __user *buf, size_t len, loff_t *ptr) | |
688 | { | |
689 | struct printer_dev *dev = fd->private_data; | |
690 | unsigned long flags; | |
691 | size_t size; /* Amount of data in a TX request. */ | |
692 | size_t bytes_copied = 0; | |
693 | struct usb_request *req; | |
694 | ||
695 | DBG(dev, "printer_write trying to send %d bytes\n", (int)len); | |
696 | ||
697 | if (len == 0) | |
698 | return -EINVAL; | |
699 | ||
700 | spin_lock(&dev->lock_printer_io); | |
701 | spin_lock_irqsave(&dev->lock, flags); | |
702 | ||
703 | /* Check if a printer reset happens while we have interrupts on */ | |
704 | dev->reset_printer = 0; | |
705 | ||
706 | /* Check if there is any available write buffers */ | |
707 | if (likely(list_empty(&dev->tx_reqs))) { | |
708 | /* Turn interrupts back on before sleeping. */ | |
709 | spin_unlock_irqrestore(&dev->lock, flags); | |
710 | ||
711 | /* | |
712 | * If write buffers are available check if this is | |
713 | * a NON-Blocking call or not. | |
714 | */ | |
715 | if (fd->f_flags & (O_NONBLOCK|O_NDELAY)) { | |
716 | spin_unlock(&dev->lock_printer_io); | |
717 | return -EAGAIN; | |
718 | } | |
719 | ||
720 | /* Sleep until a write buffer is available */ | |
721 | wait_event_interruptible(dev->tx_wait, | |
722 | (likely(!list_empty(&dev->tx_reqs)))); | |
723 | spin_lock_irqsave(&dev->lock, flags); | |
724 | } | |
725 | ||
726 | while (likely(!list_empty(&dev->tx_reqs)) && len) { | |
727 | ||
728 | if (len > USB_BUFSIZE) | |
729 | size = USB_BUFSIZE; | |
730 | else | |
731 | size = len; | |
732 | ||
733 | req = container_of(dev->tx_reqs.next, struct usb_request, | |
734 | list); | |
735 | list_del_init(&req->list); | |
736 | ||
737 | req->complete = tx_complete; | |
738 | req->length = size; | |
739 | ||
740 | /* Check if we need to send a zero length packet. */ | |
741 | if (len > size) | |
742 | /* They will be more TX requests so no yet. */ | |
743 | req->zero = 0; | |
744 | else | |
745 | /* If the data amount is not a multple of the | |
746 | * maxpacket size then send a zero length packet. | |
747 | */ | |
748 | req->zero = ((len % dev->in_ep->maxpacket) == 0); | |
749 | ||
750 | /* Don't leave irqs off while doing memory copies */ | |
751 | spin_unlock_irqrestore(&dev->lock, flags); | |
752 | ||
753 | if (copy_from_user(req->buf, buf, size)) { | |
754 | list_add(&req->list, &dev->tx_reqs); | |
755 | spin_unlock(&dev->lock_printer_io); | |
756 | return bytes_copied; | |
757 | } | |
758 | ||
759 | bytes_copied += size; | |
760 | len -= size; | |
761 | buf += size; | |
762 | ||
763 | spin_lock_irqsave(&dev->lock, flags); | |
764 | ||
765 | /* We've disconnected or reset so free the req and buffer */ | |
766 | if (dev->reset_printer) { | |
cc901bbb | 767 | list_add(&req->list, &dev->tx_reqs); |
25a010c8 CN |
768 | spin_unlock_irqrestore(&dev->lock, flags); |
769 | spin_unlock(&dev->lock_printer_io); | |
770 | return -EAGAIN; | |
771 | } | |
772 | ||
773 | if (usb_ep_queue(dev->in_ep, req, GFP_ATOMIC)) { | |
774 | list_add(&req->list, &dev->tx_reqs); | |
775 | spin_unlock_irqrestore(&dev->lock, flags); | |
776 | spin_unlock(&dev->lock_printer_io); | |
777 | return -EAGAIN; | |
778 | } | |
779 | ||
780 | list_add(&req->list, &dev->tx_reqs_active); | |
781 | ||
782 | } | |
783 | ||
784 | spin_unlock_irqrestore(&dev->lock, flags); | |
785 | spin_unlock(&dev->lock_printer_io); | |
786 | ||
787 | DBG(dev, "printer_write sent %d bytes\n", (int)bytes_copied); | |
788 | ||
789 | if (bytes_copied) { | |
790 | return bytes_copied; | |
791 | } else { | |
792 | return -EAGAIN; | |
793 | } | |
794 | } | |
795 | ||
796 | static int | |
797 | printer_fsync(struct file *fd, struct dentry *dentry, int datasync) | |
798 | { | |
799 | struct printer_dev *dev = fd->private_data; | |
800 | unsigned long flags; | |
801 | int tx_list_empty; | |
802 | ||
803 | spin_lock_irqsave(&dev->lock, flags); | |
804 | tx_list_empty = (likely(list_empty(&dev->tx_reqs))); | |
805 | spin_unlock_irqrestore(&dev->lock, flags); | |
806 | ||
807 | if (!tx_list_empty) { | |
808 | /* Sleep until all data has been sent */ | |
809 | wait_event_interruptible(dev->tx_flush_wait, | |
810 | (likely(list_empty(&dev->tx_reqs_active)))); | |
811 | } | |
812 | ||
813 | return 0; | |
814 | } | |
815 | ||
816 | static unsigned int | |
817 | printer_poll(struct file *fd, poll_table *wait) | |
818 | { | |
819 | struct printer_dev *dev = fd->private_data; | |
820 | unsigned long flags; | |
821 | int status = 0; | |
822 | ||
cc901bbb CN |
823 | spin_lock(&dev->lock_printer_io); |
824 | spin_lock_irqsave(&dev->lock, flags); | |
825 | setup_rx_reqs(dev); | |
826 | spin_unlock_irqrestore(&dev->lock, flags); | |
827 | spin_unlock(&dev->lock_printer_io); | |
828 | ||
25a010c8 CN |
829 | poll_wait(fd, &dev->rx_wait, wait); |
830 | poll_wait(fd, &dev->tx_wait, wait); | |
831 | ||
832 | spin_lock_irqsave(&dev->lock, flags); | |
833 | if (likely(!list_empty(&dev->tx_reqs))) | |
834 | status |= POLLOUT | POLLWRNORM; | |
835 | ||
cc901bbb CN |
836 | if (likely(dev->current_rx_bytes) || |
837 | likely(!list_empty(&dev->rx_buffers))) | |
25a010c8 CN |
838 | status |= POLLIN | POLLRDNORM; |
839 | ||
840 | spin_unlock_irqrestore(&dev->lock, flags); | |
841 | ||
842 | return status; | |
843 | } | |
844 | ||
44c389a0 AC |
845 | static long |
846 | printer_ioctl(struct file *fd, unsigned int code, unsigned long arg) | |
25a010c8 CN |
847 | { |
848 | struct printer_dev *dev = fd->private_data; | |
849 | unsigned long flags; | |
850 | int status = 0; | |
851 | ||
852 | DBG(dev, "printer_ioctl: cmd=0x%4.4x, arg=%lu\n", code, arg); | |
853 | ||
854 | /* handle ioctls */ | |
855 | ||
856 | spin_lock_irqsave(&dev->lock, flags); | |
857 | ||
858 | switch (code) { | |
859 | case GADGET_GET_PRINTER_STATUS: | |
860 | status = (int)dev->printer_status; | |
861 | break; | |
862 | case GADGET_SET_PRINTER_STATUS: | |
863 | dev->printer_status = (u8)arg; | |
864 | break; | |
865 | default: | |
866 | /* could not handle ioctl */ | |
867 | DBG(dev, "printer_ioctl: ERROR cmd=0x%4.4xis not supported\n", | |
868 | code); | |
869 | status = -ENOTTY; | |
870 | } | |
871 | ||
872 | spin_unlock_irqrestore(&dev->lock, flags); | |
873 | ||
874 | return status; | |
875 | } | |
876 | ||
877 | /* used after endpoint configuration */ | |
878 | static struct file_operations printer_io_operations = { | |
879 | .owner = THIS_MODULE, | |
880 | .open = printer_open, | |
881 | .read = printer_read, | |
882 | .write = printer_write, | |
883 | .fsync = printer_fsync, | |
884 | .poll = printer_poll, | |
44c389a0 | 885 | .unlocked_ioctl = printer_ioctl, |
25a010c8 CN |
886 | .release = printer_close |
887 | }; | |
888 | ||
889 | /*-------------------------------------------------------------------------*/ | |
890 | ||
891 | static int | |
892 | set_printer_interface(struct printer_dev *dev) | |
893 | { | |
894 | int result = 0; | |
895 | ||
896 | dev->in = ep_desc(dev->gadget, &hs_ep_in_desc, &fs_ep_in_desc); | |
897 | dev->in_ep->driver_data = dev; | |
898 | ||
899 | dev->out = ep_desc(dev->gadget, &hs_ep_out_desc, &fs_ep_out_desc); | |
900 | dev->out_ep->driver_data = dev; | |
901 | ||
902 | result = usb_ep_enable(dev->in_ep, dev->in); | |
903 | if (result != 0) { | |
904 | DBG(dev, "enable %s --> %d\n", dev->in_ep->name, result); | |
905 | goto done; | |
906 | } | |
907 | ||
908 | result = usb_ep_enable(dev->out_ep, dev->out); | |
909 | if (result != 0) { | |
910 | DBG(dev, "enable %s --> %d\n", dev->in_ep->name, result); | |
911 | goto done; | |
912 | } | |
913 | ||
914 | done: | |
915 | /* on error, disable any endpoints */ | |
916 | if (result != 0) { | |
917 | (void) usb_ep_disable(dev->in_ep); | |
918 | (void) usb_ep_disable(dev->out_ep); | |
919 | dev->in = NULL; | |
920 | dev->out = NULL; | |
921 | } | |
922 | ||
923 | /* caller is responsible for cleanup on error */ | |
924 | return result; | |
925 | } | |
926 | ||
927 | static void printer_reset_interface(struct printer_dev *dev) | |
928 | { | |
929 | if (dev->interface < 0) | |
930 | return; | |
931 | ||
441b62c1 | 932 | DBG(dev, "%s\n", __func__); |
25a010c8 CN |
933 | |
934 | if (dev->in) | |
935 | usb_ep_disable(dev->in_ep); | |
936 | ||
937 | if (dev->out) | |
938 | usb_ep_disable(dev->out_ep); | |
939 | ||
940 | dev->interface = -1; | |
941 | } | |
942 | ||
943 | /* change our operational config. must agree with the code | |
944 | * that returns config descriptors, and altsetting code. | |
945 | */ | |
946 | static int | |
947 | printer_set_config(struct printer_dev *dev, unsigned number) | |
948 | { | |
949 | int result = 0; | |
950 | struct usb_gadget *gadget = dev->gadget; | |
951 | ||
952 | if (gadget_is_sa1100(gadget) && dev->config) { | |
953 | /* tx fifo is full, but we can't clear it...*/ | |
954 | INFO(dev, "can't change configurations\n"); | |
955 | return -ESPIPE; | |
956 | } | |
957 | ||
958 | switch (number) { | |
959 | case DEV_CONFIG_VALUE: | |
960 | result = 0; | |
961 | break; | |
962 | default: | |
963 | result = -EINVAL; | |
964 | /* FALL THROUGH */ | |
965 | case 0: | |
966 | break; | |
967 | } | |
968 | ||
969 | if (result) { | |
970 | usb_gadget_vbus_draw(dev->gadget, | |
971 | dev->gadget->is_otg ? 8 : 100); | |
972 | } else { | |
973 | char *speed; | |
974 | unsigned power; | |
975 | ||
976 | power = 2 * config_desc.bMaxPower; | |
977 | usb_gadget_vbus_draw(dev->gadget, power); | |
978 | ||
979 | switch (gadget->speed) { | |
980 | case USB_SPEED_FULL: speed = "full"; break; | |
981 | #ifdef CONFIG_USB_GADGET_DUALSPEED | |
982 | case USB_SPEED_HIGH: speed = "high"; break; | |
983 | #endif | |
984 | default: speed = "?"; break; | |
985 | } | |
986 | ||
987 | dev->config = number; | |
988 | INFO(dev, "%s speed config #%d: %d mA, %s\n", | |
989 | speed, number, power, driver_desc); | |
990 | } | |
991 | return result; | |
992 | } | |
993 | ||
994 | static int | |
995 | config_buf(enum usb_device_speed speed, u8 *buf, u8 type, unsigned index, | |
996 | int is_otg) | |
997 | { | |
998 | int len; | |
999 | const struct usb_descriptor_header **function; | |
1000 | #ifdef CONFIG_USB_GADGET_DUALSPEED | |
1001 | int hs = (speed == USB_SPEED_HIGH); | |
1002 | ||
1003 | if (type == USB_DT_OTHER_SPEED_CONFIG) | |
1004 | hs = !hs; | |
1005 | ||
1006 | if (hs) { | |
1007 | function = hs_printer_function; | |
1008 | } else { | |
1009 | function = fs_printer_function; | |
1010 | } | |
1011 | #else | |
1012 | function = fs_printer_function; | |
1013 | #endif | |
1014 | ||
1015 | if (index >= device_desc.bNumConfigurations) | |
1016 | return -EINVAL; | |
1017 | ||
1018 | /* for now, don't advertise srp-only devices */ | |
1019 | if (!is_otg) | |
1020 | function++; | |
1021 | ||
1022 | len = usb_gadget_config_buf(&config_desc, buf, USB_DESC_BUFSIZE, | |
1023 | function); | |
1024 | if (len < 0) | |
1025 | return len; | |
1026 | ((struct usb_config_descriptor *) buf)->bDescriptorType = type; | |
1027 | return len; | |
1028 | } | |
1029 | ||
1030 | /* Change our operational Interface. */ | |
1031 | static int | |
1032 | set_interface(struct printer_dev *dev, unsigned number) | |
1033 | { | |
1034 | int result = 0; | |
1035 | ||
1036 | if (gadget_is_sa1100(dev->gadget) && dev->interface < 0) { | |
1037 | /* tx fifo is full, but we can't clear it...*/ | |
1038 | INFO(dev, "can't change interfaces\n"); | |
1039 | return -ESPIPE; | |
1040 | } | |
1041 | ||
1042 | /* Free the current interface */ | |
1043 | switch (dev->interface) { | |
1044 | case PRINTER_INTERFACE: | |
1045 | printer_reset_interface(dev); | |
1046 | break; | |
1047 | } | |
1048 | ||
1049 | switch (number) { | |
1050 | case PRINTER_INTERFACE: | |
1051 | result = set_printer_interface(dev); | |
1052 | if (result) { | |
1053 | printer_reset_interface(dev); | |
1054 | } else { | |
1055 | dev->interface = PRINTER_INTERFACE; | |
1056 | } | |
1057 | break; | |
1058 | default: | |
1059 | result = -EINVAL; | |
1060 | /* FALL THROUGH */ | |
1061 | } | |
1062 | ||
1063 | if (!result) | |
1064 | INFO(dev, "Using interface %x\n", number); | |
1065 | ||
1066 | return result; | |
1067 | } | |
1068 | ||
1069 | static void printer_setup_complete(struct usb_ep *ep, struct usb_request *req) | |
1070 | { | |
1071 | if (req->status || req->actual != req->length) | |
1072 | DBG((struct printer_dev *) ep->driver_data, | |
1073 | "setup complete --> %d, %d/%d\n", | |
1074 | req->status, req->actual, req->length); | |
1075 | } | |
1076 | ||
1077 | static void printer_soft_reset(struct printer_dev *dev) | |
1078 | { | |
1079 | struct usb_request *req; | |
1080 | ||
1081 | INFO(dev, "Received Printer Reset Request\n"); | |
1082 | ||
1083 | if (usb_ep_disable(dev->in_ep)) | |
1084 | DBG(dev, "Failed to disable USB in_ep\n"); | |
1085 | if (usb_ep_disable(dev->out_ep)) | |
1086 | DBG(dev, "Failed to disable USB out_ep\n"); | |
1087 | ||
1088 | if (dev->current_rx_req != NULL) { | |
1089 | list_add(&dev->current_rx_req->list, &dev->rx_reqs); | |
1090 | dev->current_rx_req = NULL; | |
1091 | } | |
1092 | dev->current_rx_bytes = 0; | |
1093 | dev->current_rx_buf = NULL; | |
1094 | dev->reset_printer = 1; | |
1095 | ||
1096 | while (likely(!(list_empty(&dev->rx_buffers)))) { | |
1097 | req = container_of(dev->rx_buffers.next, struct usb_request, | |
1098 | list); | |
1099 | list_del_init(&req->list); | |
1100 | list_add(&req->list, &dev->rx_reqs); | |
1101 | } | |
1102 | ||
1103 | while (likely(!(list_empty(&dev->rx_reqs_active)))) { | |
1104 | req = container_of(dev->rx_buffers.next, struct usb_request, | |
1105 | list); | |
1106 | list_del_init(&req->list); | |
1107 | list_add(&req->list, &dev->rx_reqs); | |
1108 | } | |
1109 | ||
1110 | while (likely(!(list_empty(&dev->tx_reqs_active)))) { | |
1111 | req = container_of(dev->tx_reqs_active.next, | |
1112 | struct usb_request, list); | |
1113 | list_del_init(&req->list); | |
1114 | list_add(&req->list, &dev->tx_reqs); | |
1115 | } | |
1116 | ||
1117 | if (usb_ep_enable(dev->in_ep, dev->in)) | |
1118 | DBG(dev, "Failed to enable USB in_ep\n"); | |
1119 | if (usb_ep_enable(dev->out_ep, dev->out)) | |
1120 | DBG(dev, "Failed to enable USB out_ep\n"); | |
1121 | ||
cc901bbb | 1122 | wake_up_interruptible(&dev->rx_wait); |
25a010c8 CN |
1123 | wake_up_interruptible(&dev->tx_wait); |
1124 | wake_up_interruptible(&dev->tx_flush_wait); | |
1125 | } | |
1126 | ||
1127 | /*-------------------------------------------------------------------------*/ | |
1128 | ||
1129 | /* | |
1130 | * The setup() callback implements all the ep0 functionality that's not | |
1131 | * handled lower down. | |
1132 | */ | |
1133 | static int | |
1134 | printer_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl) | |
1135 | { | |
1136 | struct printer_dev *dev = get_gadget_data(gadget); | |
1137 | struct usb_request *req = dev->req; | |
1138 | int value = -EOPNOTSUPP; | |
1139 | u16 wIndex = le16_to_cpu(ctrl->wIndex); | |
1140 | u16 wValue = le16_to_cpu(ctrl->wValue); | |
1141 | u16 wLength = le16_to_cpu(ctrl->wLength); | |
1142 | ||
1143 | DBG(dev, "ctrl req%02x.%02x v%04x i%04x l%d\n", | |
1144 | ctrl->bRequestType, ctrl->bRequest, wValue, wIndex, wLength); | |
1145 | ||
1146 | req->complete = printer_setup_complete; | |
1147 | ||
1148 | switch (ctrl->bRequestType&USB_TYPE_MASK) { | |
1149 | ||
1150 | case USB_TYPE_STANDARD: | |
1151 | switch (ctrl->bRequest) { | |
1152 | ||
1153 | case USB_REQ_GET_DESCRIPTOR: | |
1154 | if (ctrl->bRequestType != USB_DIR_IN) | |
1155 | break; | |
1156 | switch (wValue >> 8) { | |
1157 | ||
1158 | case USB_DT_DEVICE: | |
1159 | value = min(wLength, (u16) sizeof device_desc); | |
1160 | memcpy(req->buf, &device_desc, value); | |
1161 | break; | |
1162 | #ifdef CONFIG_USB_GADGET_DUALSPEED | |
1163 | case USB_DT_DEVICE_QUALIFIER: | |
1164 | if (!gadget->is_dualspeed) | |
1165 | break; | |
1166 | value = min(wLength, | |
1167 | (u16) sizeof dev_qualifier); | |
1168 | memcpy(req->buf, &dev_qualifier, value); | |
1169 | break; | |
1170 | ||
1171 | case USB_DT_OTHER_SPEED_CONFIG: | |
1172 | if (!gadget->is_dualspeed) | |
1173 | break; | |
1174 | /* FALLTHROUGH */ | |
1175 | #endif /* CONFIG_USB_GADGET_DUALSPEED */ | |
1176 | case USB_DT_CONFIG: | |
1177 | value = config_buf(gadget->speed, req->buf, | |
1178 | wValue >> 8, | |
1179 | wValue & 0xff, | |
1180 | gadget->is_otg); | |
1181 | if (value >= 0) | |
1182 | value = min(wLength, (u16) value); | |
1183 | break; | |
1184 | ||
1185 | case USB_DT_STRING: | |
1186 | value = usb_gadget_get_string(&stringtab, | |
1187 | wValue & 0xff, req->buf); | |
1188 | if (value >= 0) | |
1189 | value = min(wLength, (u16) value); | |
1190 | break; | |
1191 | } | |
1192 | break; | |
1193 | ||
1194 | case USB_REQ_SET_CONFIGURATION: | |
1195 | if (ctrl->bRequestType != 0) | |
1196 | break; | |
1197 | if (gadget->a_hnp_support) | |
1198 | DBG(dev, "HNP available\n"); | |
1199 | else if (gadget->a_alt_hnp_support) | |
1200 | DBG(dev, "HNP needs a different root port\n"); | |
1201 | value = printer_set_config(dev, wValue); | |
1202 | break; | |
1203 | case USB_REQ_GET_CONFIGURATION: | |
1204 | if (ctrl->bRequestType != USB_DIR_IN) | |
1205 | break; | |
1206 | *(u8 *)req->buf = dev->config; | |
1207 | value = min(wLength, (u16) 1); | |
1208 | break; | |
1209 | ||
1210 | case USB_REQ_SET_INTERFACE: | |
1211 | if (ctrl->bRequestType != USB_RECIP_INTERFACE || | |
1212 | !dev->config) | |
1213 | break; | |
1214 | ||
1215 | value = set_interface(dev, PRINTER_INTERFACE); | |
1216 | break; | |
1217 | case USB_REQ_GET_INTERFACE: | |
1218 | if (ctrl->bRequestType != | |
1219 | (USB_DIR_IN|USB_RECIP_INTERFACE) | |
1220 | || !dev->config) | |
1221 | break; | |
1222 | ||
1223 | *(u8 *)req->buf = dev->interface; | |
1224 | value = min(wLength, (u16) 1); | |
1225 | break; | |
1226 | ||
1227 | default: | |
1228 | goto unknown; | |
1229 | } | |
1230 | break; | |
1231 | ||
1232 | case USB_TYPE_CLASS: | |
1233 | switch (ctrl->bRequest) { | |
1234 | case 0: /* Get the IEEE-1284 PNP String */ | |
1235 | /* Only one printer interface is supported. */ | |
1236 | if ((wIndex>>8) != PRINTER_INTERFACE) | |
1237 | break; | |
1238 | ||
1239 | value = (pnp_string[0]<<8)|pnp_string[1]; | |
1240 | memcpy(req->buf, pnp_string, value); | |
1241 | DBG(dev, "1284 PNP String: %x %s\n", value, | |
1242 | &pnp_string[2]); | |
1243 | break; | |
1244 | ||
1245 | case 1: /* Get Port Status */ | |
1246 | /* Only one printer interface is supported. */ | |
1247 | if (wIndex != PRINTER_INTERFACE) | |
1248 | break; | |
1249 | ||
1250 | *(u8 *)req->buf = dev->printer_status; | |
1251 | value = min(wLength, (u16) 1); | |
1252 | break; | |
1253 | ||
1254 | case 2: /* Soft Reset */ | |
1255 | /* Only one printer interface is supported. */ | |
1256 | if (wIndex != PRINTER_INTERFACE) | |
1257 | break; | |
1258 | ||
1259 | printer_soft_reset(dev); | |
1260 | ||
1261 | value = 0; | |
1262 | break; | |
1263 | ||
1264 | default: | |
1265 | goto unknown; | |
1266 | } | |
1267 | break; | |
1268 | ||
1269 | default: | |
1270 | unknown: | |
1271 | VDBG(dev, | |
1272 | "unknown ctrl req%02x.%02x v%04x i%04x l%d\n", | |
1273 | ctrl->bRequestType, ctrl->bRequest, | |
1274 | wValue, wIndex, wLength); | |
1275 | break; | |
1276 | } | |
1277 | ||
1278 | /* respond with data transfer before status phase? */ | |
1279 | if (value >= 0) { | |
1280 | req->length = value; | |
1281 | req->zero = value < wLength | |
1282 | && (value % gadget->ep0->maxpacket) == 0; | |
1283 | value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC); | |
1284 | if (value < 0) { | |
1285 | DBG(dev, "ep_queue --> %d\n", value); | |
1286 | req->status = 0; | |
1287 | printer_setup_complete(gadget->ep0, req); | |
1288 | } | |
1289 | } | |
1290 | ||
1291 | /* host either stalls (value < 0) or reports success */ | |
1292 | return value; | |
1293 | } | |
1294 | ||
1295 | static void | |
1296 | printer_disconnect(struct usb_gadget *gadget) | |
1297 | { | |
1298 | struct printer_dev *dev = get_gadget_data(gadget); | |
1299 | unsigned long flags; | |
1300 | ||
441b62c1 | 1301 | DBG(dev, "%s\n", __func__); |
25a010c8 CN |
1302 | |
1303 | spin_lock_irqsave(&dev->lock, flags); | |
1304 | ||
1305 | printer_reset_interface(dev); | |
1306 | ||
1307 | spin_unlock_irqrestore(&dev->lock, flags); | |
1308 | } | |
1309 | ||
1310 | static void | |
1311 | printer_unbind(struct usb_gadget *gadget) | |
1312 | { | |
1313 | struct printer_dev *dev = get_gadget_data(gadget); | |
1314 | struct usb_request *req; | |
1315 | ||
1316 | ||
441b62c1 | 1317 | DBG(dev, "%s\n", __func__); |
25a010c8 CN |
1318 | |
1319 | /* Remove sysfs files */ | |
1320 | device_destroy(usb_gadget_class, g_printer_devno); | |
1321 | ||
1322 | /* Remove Character Device */ | |
1323 | cdev_del(&dev->printer_cdev); | |
1324 | ||
1325 | /* we must already have been disconnected ... no i/o may be active */ | |
1326 | WARN_ON(!list_empty(&dev->tx_reqs_active)); | |
1327 | WARN_ON(!list_empty(&dev->rx_reqs_active)); | |
1328 | ||
1329 | /* Free all memory for this driver. */ | |
1330 | while (!list_empty(&dev->tx_reqs)) { | |
1331 | req = container_of(dev->tx_reqs.next, struct usb_request, | |
1332 | list); | |
1333 | list_del(&req->list); | |
1334 | printer_req_free(dev->in_ep, req); | |
1335 | } | |
1336 | ||
efa66f14 | 1337 | if (dev->current_rx_req != NULL) |
25a010c8 CN |
1338 | printer_req_free(dev->out_ep, dev->current_rx_req); |
1339 | ||
1340 | while (!list_empty(&dev->rx_reqs)) { | |
1341 | req = container_of(dev->rx_reqs.next, | |
1342 | struct usb_request, list); | |
1343 | list_del(&req->list); | |
1344 | printer_req_free(dev->out_ep, req); | |
1345 | } | |
1346 | ||
1347 | while (!list_empty(&dev->rx_buffers)) { | |
1348 | req = container_of(dev->rx_buffers.next, | |
1349 | struct usb_request, list); | |
1350 | list_del(&req->list); | |
1351 | printer_req_free(dev->out_ep, req); | |
1352 | } | |
1353 | ||
1354 | if (dev->req) { | |
1355 | printer_req_free(gadget->ep0, dev->req); | |
1356 | dev->req = NULL; | |
1357 | } | |
1358 | ||
1359 | set_gadget_data(gadget, NULL); | |
1360 | } | |
1361 | ||
1362 | static int __init | |
1363 | printer_bind(struct usb_gadget *gadget) | |
1364 | { | |
1365 | struct printer_dev *dev; | |
1366 | struct usb_ep *in_ep, *out_ep; | |
1367 | int status = -ENOMEM; | |
1368 | int gcnum; | |
1369 | size_t len; | |
1370 | u32 i; | |
1371 | struct usb_request *req; | |
1372 | ||
1373 | dev = &usb_printer_gadget; | |
1374 | ||
1375 | ||
1376 | /* Setup the sysfs files for the printer gadget. */ | |
b0b090e5 GKH |
1377 | dev->pdev = device_create(usb_gadget_class, NULL, g_printer_devno, |
1378 | NULL, "g_printer"); | |
25a010c8 CN |
1379 | if (IS_ERR(dev->pdev)) { |
1380 | ERROR(dev, "Failed to create device: g_printer\n"); | |
1381 | goto fail; | |
1382 | } | |
1383 | ||
1384 | /* | |
1385 | * Register a character device as an interface to a user mode | |
1386 | * program that handles the printer specific functionality. | |
1387 | */ | |
1388 | cdev_init(&dev->printer_cdev, &printer_io_operations); | |
1389 | dev->printer_cdev.owner = THIS_MODULE; | |
1390 | status = cdev_add(&dev->printer_cdev, g_printer_devno, 1); | |
1391 | if (status) { | |
1392 | ERROR(dev, "Failed to open char device\n"); | |
1393 | goto fail; | |
1394 | } | |
1395 | ||
1396 | if (gadget_is_sa1100(gadget)) { | |
1397 | /* hardware can't write zero length packets. */ | |
1398 | ERROR(dev, "SA1100 controller is unsupport by this driver\n"); | |
1399 | goto fail; | |
1400 | } | |
1401 | ||
1402 | gcnum = usb_gadget_controller_number(gadget); | |
1403 | if (gcnum >= 0) { | |
1404 | device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum); | |
1405 | } else { | |
1406 | dev_warn(&gadget->dev, "controller '%s' not recognized\n", | |
1407 | gadget->name); | |
1408 | /* unrecognized, but safe unless bulk is REALLY quirky */ | |
1409 | device_desc.bcdDevice = | |
1410 | __constant_cpu_to_le16(0xFFFF); | |
1411 | } | |
1412 | snprintf(manufacturer, sizeof(manufacturer), "%s %s with %s", | |
1413 | init_utsname()->sysname, init_utsname()->release, | |
1414 | gadget->name); | |
1415 | ||
1416 | device_desc.idVendor = | |
1417 | __constant_cpu_to_le16(PRINTER_VENDOR_NUM); | |
1418 | device_desc.idProduct = | |
1419 | __constant_cpu_to_le16(PRINTER_PRODUCT_NUM); | |
1420 | ||
1421 | /* support optional vendor/distro customization */ | |
1422 | if (idVendor) { | |
1423 | if (!idProduct) { | |
1424 | dev_err(&gadget->dev, "idVendor needs idProduct!\n"); | |
1425 | return -ENODEV; | |
1426 | } | |
1427 | device_desc.idVendor = cpu_to_le16(idVendor); | |
1428 | device_desc.idProduct = cpu_to_le16(idProduct); | |
1429 | if (bcdDevice) | |
1430 | device_desc.bcdDevice = cpu_to_le16(bcdDevice); | |
1431 | } | |
1432 | ||
1433 | if (iManufacturer) | |
1434 | strlcpy(manufacturer, iManufacturer, sizeof manufacturer); | |
1435 | ||
1436 | if (iProduct) | |
1437 | strlcpy(product_desc, iProduct, sizeof product_desc); | |
1438 | ||
1439 | if (iSerialNum) | |
1440 | strlcpy(serial_num, iSerialNum, sizeof serial_num); | |
1441 | ||
1442 | if (iPNPstring) | |
1443 | strlcpy(&pnp_string[2], iPNPstring, (sizeof pnp_string)-2); | |
1444 | ||
1445 | len = strlen(pnp_string); | |
1446 | pnp_string[0] = (len >> 8) & 0xFF; | |
1447 | pnp_string[1] = len & 0xFF; | |
1448 | ||
1449 | /* all we really need is bulk IN/OUT */ | |
1450 | usb_ep_autoconfig_reset(gadget); | |
1451 | in_ep = usb_ep_autoconfig(gadget, &fs_ep_in_desc); | |
1452 | if (!in_ep) { | |
1453 | autoconf_fail: | |
1454 | dev_err(&gadget->dev, "can't autoconfigure on %s\n", | |
1455 | gadget->name); | |
1456 | return -ENODEV; | |
1457 | } | |
1458 | in_ep->driver_data = in_ep; /* claim */ | |
1459 | ||
1460 | out_ep = usb_ep_autoconfig(gadget, &fs_ep_out_desc); | |
1461 | if (!out_ep) | |
1462 | goto autoconf_fail; | |
1463 | out_ep->driver_data = out_ep; /* claim */ | |
1464 | ||
1465 | #ifdef CONFIG_USB_GADGET_DUALSPEED | |
1466 | /* assumes ep0 uses the same value for both speeds ... */ | |
1467 | dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0; | |
1468 | ||
1469 | /* and that all endpoints are dual-speed */ | |
1470 | hs_ep_in_desc.bEndpointAddress = fs_ep_in_desc.bEndpointAddress; | |
1471 | hs_ep_out_desc.bEndpointAddress = fs_ep_out_desc.bEndpointAddress; | |
1472 | #endif /* DUALSPEED */ | |
1473 | ||
1474 | device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket; | |
1475 | usb_gadget_set_selfpowered(gadget); | |
1476 | ||
1477 | if (gadget->is_otg) { | |
1478 | otg_desc.bmAttributes |= USB_OTG_HNP, | |
1479 | config_desc.bmAttributes |= USB_CONFIG_ATT_WAKEUP; | |
1480 | config_desc.bMaxPower = 4; | |
1481 | } | |
1482 | ||
1483 | spin_lock_init(&dev->lock); | |
1484 | spin_lock_init(&dev->lock_printer_io); | |
1485 | INIT_LIST_HEAD(&dev->tx_reqs); | |
1486 | INIT_LIST_HEAD(&dev->tx_reqs_active); | |
1487 | INIT_LIST_HEAD(&dev->rx_reqs); | |
1488 | INIT_LIST_HEAD(&dev->rx_reqs_active); | |
1489 | INIT_LIST_HEAD(&dev->rx_buffers); | |
1490 | init_waitqueue_head(&dev->rx_wait); | |
1491 | init_waitqueue_head(&dev->tx_wait); | |
1492 | init_waitqueue_head(&dev->tx_flush_wait); | |
1493 | ||
1494 | dev->config = 0; | |
1495 | dev->interface = -1; | |
1496 | dev->printer_cdev_open = 0; | |
1497 | dev->printer_status = PRINTER_NOT_ERROR; | |
1498 | dev->current_rx_req = NULL; | |
1499 | dev->current_rx_bytes = 0; | |
1500 | dev->current_rx_buf = NULL; | |
1501 | ||
1502 | dev->in_ep = in_ep; | |
1503 | dev->out_ep = out_ep; | |
1504 | ||
1505 | /* preallocate control message data and buffer */ | |
1506 | dev->req = printer_req_alloc(gadget->ep0, USB_DESC_BUFSIZE, | |
1507 | GFP_KERNEL); | |
1508 | if (!dev->req) { | |
1509 | status = -ENOMEM; | |
1510 | goto fail; | |
1511 | } | |
1512 | ||
1513 | for (i = 0; i < QLEN; i++) { | |
1514 | req = printer_req_alloc(dev->in_ep, USB_BUFSIZE, GFP_KERNEL); | |
1515 | if (!req) { | |
1516 | while (!list_empty(&dev->tx_reqs)) { | |
1517 | req = container_of(dev->tx_reqs.next, | |
1518 | struct usb_request, list); | |
1519 | list_del(&req->list); | |
1520 | printer_req_free(dev->in_ep, req); | |
1521 | } | |
1522 | return -ENOMEM; | |
1523 | } | |
1524 | list_add(&req->list, &dev->tx_reqs); | |
1525 | } | |
1526 | ||
1527 | for (i = 0; i < QLEN; i++) { | |
1528 | req = printer_req_alloc(dev->out_ep, USB_BUFSIZE, GFP_KERNEL); | |
1529 | if (!req) { | |
1530 | while (!list_empty(&dev->rx_reqs)) { | |
1531 | req = container_of(dev->rx_reqs.next, | |
1532 | struct usb_request, list); | |
1533 | list_del(&req->list); | |
1534 | printer_req_free(dev->out_ep, req); | |
1535 | } | |
1536 | return -ENOMEM; | |
1537 | } | |
1538 | list_add(&req->list, &dev->rx_reqs); | |
1539 | } | |
1540 | ||
1541 | dev->req->complete = printer_setup_complete; | |
1542 | ||
1543 | /* finish hookup to lower layer ... */ | |
1544 | dev->gadget = gadget; | |
1545 | set_gadget_data(gadget, dev); | |
1546 | gadget->ep0->driver_data = dev; | |
1547 | ||
1548 | INFO(dev, "%s, version: " DRIVER_VERSION "\n", driver_desc); | |
1549 | INFO(dev, "using %s, OUT %s IN %s\n", gadget->name, out_ep->name, | |
1550 | in_ep->name); | |
1551 | ||
1552 | return 0; | |
1553 | ||
1554 | fail: | |
1555 | printer_unbind(gadget); | |
1556 | return status; | |
1557 | } | |
1558 | ||
1559 | /*-------------------------------------------------------------------------*/ | |
1560 | ||
1561 | static struct usb_gadget_driver printer_driver = { | |
1562 | .speed = DEVSPEED, | |
1563 | ||
1564 | .function = (char *) driver_desc, | |
1565 | .bind = printer_bind, | |
1566 | .unbind = printer_unbind, | |
1567 | ||
1568 | .setup = printer_setup, | |
1569 | .disconnect = printer_disconnect, | |
1570 | ||
1571 | .driver = { | |
1572 | .name = (char *) shortname, | |
1573 | .owner = THIS_MODULE, | |
1574 | }, | |
1575 | }; | |
1576 | ||
1577 | MODULE_DESCRIPTION(DRIVER_DESC); | |
1578 | MODULE_AUTHOR("Craig Nadler"); | |
1579 | MODULE_LICENSE("GPL"); | |
1580 | ||
1581 | static int __init | |
1582 | init(void) | |
1583 | { | |
1584 | int status; | |
1585 | ||
1586 | usb_gadget_class = class_create(THIS_MODULE, "usb_printer_gadget"); | |
1587 | if (IS_ERR(usb_gadget_class)) { | |
1588 | status = PTR_ERR(usb_gadget_class); | |
1589 | ERROR(dev, "unable to create usb_gadget class %d\n", status); | |
1590 | return status; | |
1591 | } | |
1592 | ||
1593 | status = alloc_chrdev_region(&g_printer_devno, 0, 1, | |
1594 | "USB printer gadget"); | |
1595 | if (status) { | |
1596 | ERROR(dev, "alloc_chrdev_region %d\n", status); | |
1597 | class_destroy(usb_gadget_class); | |
1598 | return status; | |
1599 | } | |
1600 | ||
1601 | status = usb_gadget_register_driver(&printer_driver); | |
1602 | if (status) { | |
1603 | class_destroy(usb_gadget_class); | |
1604 | unregister_chrdev_region(g_printer_devno, 1); | |
1605 | DBG(dev, "usb_gadget_register_driver %x\n", status); | |
1606 | } | |
1607 | ||
1608 | return status; | |
1609 | } | |
1610 | module_init(init); | |
1611 | ||
1612 | static void __exit | |
1613 | cleanup(void) | |
1614 | { | |
1615 | int status; | |
1616 | ||
1617 | spin_lock(&usb_printer_gadget.lock_printer_io); | |
1618 | class_destroy(usb_gadget_class); | |
1619 | unregister_chrdev_region(g_printer_devno, 2); | |
1620 | ||
1621 | status = usb_gadget_unregister_driver(&printer_driver); | |
1622 | if (status) | |
1623 | ERROR(dev, "usb_gadget_unregister_driver %x\n", status); | |
1624 | ||
1625 | spin_unlock(&usb_printer_gadget.lock_printer_io); | |
1626 | } | |
1627 | module_exit(cleanup); |