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. | |
25a010c8 CN |
11 | */ |
12 | ||
13 | #include <linux/module.h> | |
14 | #include <linux/kernel.h> | |
15 | #include <linux/delay.h> | |
16 | #include <linux/ioport.h> | |
17 | #include <linux/sched.h> | |
18 | #include <linux/slab.h> | |
925ce689 | 19 | #include <linux/mutex.h> |
25a010c8 CN |
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/utsname.h> | |
26 | #include <linux/device.h> | |
27 | #include <linux/moduleparam.h> | |
28 | #include <linux/fs.h> | |
29 | #include <linux/poll.h> | |
30 | #include <linux/types.h> | |
31 | #include <linux/ctype.h> | |
32 | #include <linux/cdev.h> | |
33 | ||
34 | #include <asm/byteorder.h> | |
35 | #include <linux/io.h> | |
36 | #include <linux/irq.h> | |
25a010c8 CN |
37 | #include <linux/uaccess.h> |
38 | #include <asm/unaligned.h> | |
39 | ||
40 | #include <linux/usb/ch9.h> | |
41 | #include <linux/usb/gadget.h> | |
42 | #include <linux/usb/g_printer.h> | |
43 | ||
44 | #include "gadget_chips.h" | |
45 | ||
0a56b03f DB |
46 | |
47 | /* | |
48 | * Kbuild is not very cooperative with respect to linking separately | |
49 | * compiled library objects into one module. So for now we won't use | |
50 | * separate compilation ... ensuring init/exit sections work to shrink | |
51 | * the runtime footprint, and giving us at least some parts of what | |
52 | * a "gcc --combine ... part1.c part2.c part3.c ... " build would. | |
53 | */ | |
2e87edf4 | 54 | #include "composite.c" |
0a56b03f DB |
55 | |
56 | /*-------------------------------------------------------------------------*/ | |
7d16e8d3 | 57 | USB_GADGET_COMPOSITE_OPTIONS(); |
0a56b03f | 58 | |
25a010c8 CN |
59 | #define DRIVER_DESC "Printer Gadget" |
60 | #define DRIVER_VERSION "2007 OCT 06" | |
61 | ||
925ce689 | 62 | static DEFINE_MUTEX(printer_mutex); |
25a010c8 CN |
63 | static const char shortname [] = "printer"; |
64 | static const char driver_desc [] = DRIVER_DESC; | |
65 | ||
66 | static dev_t g_printer_devno; | |
67 | ||
68 | static struct class *usb_gadget_class; | |
69 | ||
70 | /*-------------------------------------------------------------------------*/ | |
71 | ||
72 | struct printer_dev { | |
73 | spinlock_t lock; /* lock this structure */ | |
74 | /* lock buffer lists during read/write calls */ | |
10ca4425 | 75 | struct mutex lock_printer_io; |
25a010c8 | 76 | struct usb_gadget *gadget; |
25a010c8 CN |
77 | s8 interface; |
78 | struct usb_ep *in_ep, *out_ep; | |
72c973dd | 79 | |
25a010c8 CN |
80 | struct list_head rx_reqs; /* List of free RX structs */ |
81 | struct list_head rx_reqs_active; /* List of Active RX xfers */ | |
82 | struct list_head rx_buffers; /* List of completed xfers */ | |
83 | /* wait until there is data to be read. */ | |
84 | wait_queue_head_t rx_wait; | |
85 | struct list_head tx_reqs; /* List of free TX structs */ | |
86 | struct list_head tx_reqs_active; /* List of Active TX xfers */ | |
87 | /* Wait until there are write buffers available to use. */ | |
88 | wait_queue_head_t tx_wait; | |
89 | /* Wait until all write buffers have been sent. */ | |
90 | wait_queue_head_t tx_flush_wait; | |
91 | struct usb_request *current_rx_req; | |
92 | size_t current_rx_bytes; | |
93 | u8 *current_rx_buf; | |
94 | u8 printer_status; | |
95 | u8 reset_printer; | |
25a010c8 CN |
96 | struct cdev printer_cdev; |
97 | struct device *pdev; | |
98 | u8 printer_cdev_open; | |
99 | wait_queue_head_t wait; | |
2e87edf4 | 100 | struct usb_function function; |
25a010c8 CN |
101 | }; |
102 | ||
103 | static struct printer_dev usb_printer_gadget; | |
104 | ||
105 | /*-------------------------------------------------------------------------*/ | |
106 | ||
107 | /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!! | |
108 | * Instead: allocate your own, using normal USB-IF procedures. | |
109 | */ | |
110 | ||
111 | /* Thanks to NetChip Technologies for donating this product ID. | |
112 | */ | |
113 | #define PRINTER_VENDOR_NUM 0x0525 /* NetChip */ | |
114 | #define PRINTER_PRODUCT_NUM 0xa4a8 /* Linux-USB Printer Gadget */ | |
115 | ||
25985edc | 116 | /* Some systems will want different product identifiers published in the |
25a010c8 CN |
117 | * device descriptor, either numbers or strings or both. These string |
118 | * parameters are in UTF-8 (superset of ASCII's 7 bit characters). | |
119 | */ | |
120 | ||
ad84e4a9 | 121 | static char *iSerialNum; |
25a010c8 CN |
122 | module_param(iSerialNum, charp, S_IRUGO); |
123 | MODULE_PARM_DESC(iSerialNum, "1"); | |
124 | ||
ad84e4a9 | 125 | static char *iPNPstring; |
25a010c8 CN |
126 | module_param(iPNPstring, charp, S_IRUGO); |
127 | MODULE_PARM_DESC(iPNPstring, "MFG:linux;MDL:g_printer;CLS:PRINTER;SN:1;"); | |
128 | ||
129 | /* Number of requests to allocate per endpoint, not used for ep0. */ | |
130 | static unsigned qlen = 10; | |
131 | module_param(qlen, uint, S_IRUGO|S_IWUSR); | |
132 | ||
133 | #define QLEN qlen | |
134 | ||
25a010c8 CN |
135 | /*-------------------------------------------------------------------------*/ |
136 | ||
25a010c8 CN |
137 | /* |
138 | * DESCRIPTORS ... most are static, but strings and (full) configuration | |
139 | * descriptors are built on demand. | |
140 | */ | |
141 | ||
25a010c8 CN |
142 | /* holds our biggest descriptor */ |
143 | #define USB_DESC_BUFSIZE 256 | |
144 | #define USB_BUFSIZE 8192 | |
145 | ||
25a010c8 CN |
146 | static struct usb_device_descriptor device_desc = { |
147 | .bLength = sizeof device_desc, | |
148 | .bDescriptorType = USB_DT_DEVICE, | |
551509d2 | 149 | .bcdUSB = cpu_to_le16(0x0200), |
25a010c8 CN |
150 | .bDeviceClass = USB_CLASS_PER_INTERFACE, |
151 | .bDeviceSubClass = 0, | |
152 | .bDeviceProtocol = 0, | |
551509d2 HH |
153 | .idVendor = cpu_to_le16(PRINTER_VENDOR_NUM), |
154 | .idProduct = cpu_to_le16(PRINTER_PRODUCT_NUM), | |
25a010c8 CN |
155 | .bNumConfigurations = 1 |
156 | }; | |
157 | ||
25a010c8 CN |
158 | static struct usb_interface_descriptor intf_desc = { |
159 | .bLength = sizeof intf_desc, | |
160 | .bDescriptorType = USB_DT_INTERFACE, | |
25a010c8 CN |
161 | .bNumEndpoints = 2, |
162 | .bInterfaceClass = USB_CLASS_PRINTER, | |
163 | .bInterfaceSubClass = 1, /* Printer Sub-Class */ | |
164 | .bInterfaceProtocol = 2, /* Bi-Directional */ | |
165 | .iInterface = 0 | |
166 | }; | |
167 | ||
168 | static struct usb_endpoint_descriptor fs_ep_in_desc = { | |
169 | .bLength = USB_DT_ENDPOINT_SIZE, | |
170 | .bDescriptorType = USB_DT_ENDPOINT, | |
171 | .bEndpointAddress = USB_DIR_IN, | |
172 | .bmAttributes = USB_ENDPOINT_XFER_BULK | |
173 | }; | |
174 | ||
175 | static struct usb_endpoint_descriptor fs_ep_out_desc = { | |
176 | .bLength = USB_DT_ENDPOINT_SIZE, | |
177 | .bDescriptorType = USB_DT_ENDPOINT, | |
178 | .bEndpointAddress = USB_DIR_OUT, | |
179 | .bmAttributes = USB_ENDPOINT_XFER_BULK | |
180 | }; | |
181 | ||
2e87edf4 | 182 | static struct usb_descriptor_header *fs_printer_function[] = { |
25a010c8 CN |
183 | (struct usb_descriptor_header *) &intf_desc, |
184 | (struct usb_descriptor_header *) &fs_ep_in_desc, | |
185 | (struct usb_descriptor_header *) &fs_ep_out_desc, | |
186 | NULL | |
187 | }; | |
188 | ||
25a010c8 CN |
189 | /* |
190 | * usb 2.0 devices need to expose both high speed and full speed | |
191 | * descriptors, unless they only run at full speed. | |
192 | */ | |
193 | ||
194 | static struct usb_endpoint_descriptor hs_ep_in_desc = { | |
195 | .bLength = USB_DT_ENDPOINT_SIZE, | |
196 | .bDescriptorType = USB_DT_ENDPOINT, | |
197 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | |
551509d2 | 198 | .wMaxPacketSize = cpu_to_le16(512) |
25a010c8 CN |
199 | }; |
200 | ||
201 | static struct usb_endpoint_descriptor hs_ep_out_desc = { | |
202 | .bLength = USB_DT_ENDPOINT_SIZE, | |
203 | .bDescriptorType = USB_DT_ENDPOINT, | |
204 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | |
551509d2 | 205 | .wMaxPacketSize = cpu_to_le16(512) |
25a010c8 CN |
206 | }; |
207 | ||
208 | static struct usb_qualifier_descriptor dev_qualifier = { | |
209 | .bLength = sizeof dev_qualifier, | |
210 | .bDescriptorType = USB_DT_DEVICE_QUALIFIER, | |
551509d2 | 211 | .bcdUSB = cpu_to_le16(0x0200), |
25a010c8 CN |
212 | .bDeviceClass = USB_CLASS_PRINTER, |
213 | .bNumConfigurations = 1 | |
214 | }; | |
215 | ||
2e87edf4 | 216 | static struct usb_descriptor_header *hs_printer_function[] = { |
25a010c8 CN |
217 | (struct usb_descriptor_header *) &intf_desc, |
218 | (struct usb_descriptor_header *) &hs_ep_in_desc, | |
219 | (struct usb_descriptor_header *) &hs_ep_out_desc, | |
220 | NULL | |
221 | }; | |
222 | ||
2e87edf4 SAS |
223 | static struct usb_otg_descriptor otg_descriptor = { |
224 | .bLength = sizeof otg_descriptor, | |
225 | .bDescriptorType = USB_DT_OTG, | |
226 | .bmAttributes = USB_OTG_SRP, | |
227 | }; | |
228 | ||
229 | static const struct usb_descriptor_header *otg_desc[] = { | |
230 | (struct usb_descriptor_header *) &otg_descriptor, | |
231 | NULL, | |
232 | }; | |
233 | ||
25a010c8 CN |
234 | /* maxpacket and other transfer characteristics vary by speed. */ |
235 | #define ep_desc(g, hs, fs) (((g)->speed == USB_SPEED_HIGH)?(hs):(fs)) | |
236 | ||
25a010c8 CN |
237 | /*-------------------------------------------------------------------------*/ |
238 | ||
239 | /* descriptors that are built on-demand */ | |
240 | ||
241 | static char manufacturer [50]; | |
242 | static char product_desc [40] = DRIVER_DESC; | |
243 | static char serial_num [40] = "1"; | |
244 | static char pnp_string [1024] = | |
245 | "XXMFG:linux;MDL:g_printer;CLS:PRINTER;SN:1;"; | |
246 | ||
247 | /* static strings, in UTF-8 */ | |
248 | static struct usb_string strings [] = { | |
276e2e4f SAS |
249 | [USB_GADGET_MANUFACTURER_IDX].s = manufacturer, |
250 | [USB_GADGET_PRODUCT_IDX].s = product_desc, | |
251 | [USB_GADGET_SERIAL_IDX].s = serial_num, | |
25a010c8 CN |
252 | { } /* end of list */ |
253 | }; | |
254 | ||
2e87edf4 | 255 | static struct usb_gadget_strings stringtab_dev = { |
25a010c8 CN |
256 | .language = 0x0409, /* en-us */ |
257 | .strings = strings, | |
258 | }; | |
259 | ||
2e87edf4 SAS |
260 | static struct usb_gadget_strings *dev_strings[] = { |
261 | &stringtab_dev, | |
262 | NULL, | |
263 | }; | |
264 | ||
25a010c8 CN |
265 | /*-------------------------------------------------------------------------*/ |
266 | ||
267 | static struct usb_request * | |
268 | printer_req_alloc(struct usb_ep *ep, unsigned len, gfp_t gfp_flags) | |
269 | { | |
270 | struct usb_request *req; | |
271 | ||
272 | req = usb_ep_alloc_request(ep, gfp_flags); | |
273 | ||
274 | if (req != NULL) { | |
275 | req->length = len; | |
276 | req->buf = kmalloc(len, gfp_flags); | |
277 | if (req->buf == NULL) { | |
278 | usb_ep_free_request(ep, req); | |
279 | return NULL; | |
280 | } | |
281 | } | |
282 | ||
283 | return req; | |
284 | } | |
285 | ||
286 | static void | |
287 | printer_req_free(struct usb_ep *ep, struct usb_request *req) | |
288 | { | |
289 | if (ep != NULL && req != NULL) { | |
290 | kfree(req->buf); | |
291 | usb_ep_free_request(ep, req); | |
292 | } | |
293 | } | |
294 | ||
295 | /*-------------------------------------------------------------------------*/ | |
296 | ||
297 | static void rx_complete(struct usb_ep *ep, struct usb_request *req) | |
298 | { | |
299 | struct printer_dev *dev = ep->driver_data; | |
300 | int status = req->status; | |
301 | unsigned long flags; | |
302 | ||
303 | spin_lock_irqsave(&dev->lock, flags); | |
304 | ||
305 | list_del_init(&req->list); /* Remode from Active List */ | |
306 | ||
307 | switch (status) { | |
308 | ||
309 | /* normal completion */ | |
310 | case 0: | |
cc901bbb CN |
311 | if (req->actual > 0) { |
312 | list_add_tail(&req->list, &dev->rx_buffers); | |
313 | DBG(dev, "G_Printer : rx length %d\n", req->actual); | |
314 | } else { | |
315 | list_add(&req->list, &dev->rx_reqs); | |
316 | } | |
25a010c8 CN |
317 | break; |
318 | ||
319 | /* software-driven interface shutdown */ | |
320 | case -ECONNRESET: /* unlink */ | |
321 | case -ESHUTDOWN: /* disconnect etc */ | |
322 | VDBG(dev, "rx shutdown, code %d\n", status); | |
323 | list_add(&req->list, &dev->rx_reqs); | |
324 | break; | |
325 | ||
326 | /* for hardware automagic (such as pxa) */ | |
327 | case -ECONNABORTED: /* endpoint reset */ | |
328 | DBG(dev, "rx %s reset\n", ep->name); | |
329 | list_add(&req->list, &dev->rx_reqs); | |
330 | break; | |
331 | ||
332 | /* data overrun */ | |
333 | case -EOVERFLOW: | |
334 | /* FALLTHROUGH */ | |
335 | ||
336 | default: | |
337 | DBG(dev, "rx status %d\n", status); | |
338 | list_add(&req->list, &dev->rx_reqs); | |
339 | break; | |
340 | } | |
cc901bbb CN |
341 | |
342 | wake_up_interruptible(&dev->rx_wait); | |
25a010c8 CN |
343 | spin_unlock_irqrestore(&dev->lock, flags); |
344 | } | |
345 | ||
346 | static void tx_complete(struct usb_ep *ep, struct usb_request *req) | |
347 | { | |
348 | struct printer_dev *dev = ep->driver_data; | |
349 | ||
350 | switch (req->status) { | |
351 | default: | |
352 | VDBG(dev, "tx err %d\n", req->status); | |
353 | /* FALLTHROUGH */ | |
354 | case -ECONNRESET: /* unlink */ | |
355 | case -ESHUTDOWN: /* disconnect etc */ | |
356 | break; | |
357 | case 0: | |
358 | break; | |
359 | } | |
360 | ||
361 | spin_lock(&dev->lock); | |
362 | /* Take the request struct off the active list and put it on the | |
363 | * free list. | |
364 | */ | |
365 | list_del_init(&req->list); | |
366 | list_add(&req->list, &dev->tx_reqs); | |
367 | wake_up_interruptible(&dev->tx_wait); | |
368 | if (likely(list_empty(&dev->tx_reqs_active))) | |
369 | wake_up_interruptible(&dev->tx_flush_wait); | |
370 | ||
371 | spin_unlock(&dev->lock); | |
372 | } | |
373 | ||
374 | /*-------------------------------------------------------------------------*/ | |
375 | ||
376 | static int | |
377 | printer_open(struct inode *inode, struct file *fd) | |
378 | { | |
379 | struct printer_dev *dev; | |
380 | unsigned long flags; | |
381 | int ret = -EBUSY; | |
382 | ||
925ce689 | 383 | mutex_lock(&printer_mutex); |
25a010c8 CN |
384 | dev = container_of(inode->i_cdev, struct printer_dev, printer_cdev); |
385 | ||
386 | spin_lock_irqsave(&dev->lock, flags); | |
387 | ||
388 | if (!dev->printer_cdev_open) { | |
389 | dev->printer_cdev_open = 1; | |
390 | fd->private_data = dev; | |
391 | ret = 0; | |
392 | /* Change the printer status to show that it's on-line. */ | |
393 | dev->printer_status |= PRINTER_SELECTED; | |
394 | } | |
395 | ||
396 | spin_unlock_irqrestore(&dev->lock, flags); | |
397 | ||
398 | DBG(dev, "printer_open returned %x\n", ret); | |
925ce689 | 399 | mutex_unlock(&printer_mutex); |
25a010c8 CN |
400 | return ret; |
401 | } | |
402 | ||
403 | static int | |
404 | printer_close(struct inode *inode, struct file *fd) | |
405 | { | |
406 | struct printer_dev *dev = fd->private_data; | |
407 | unsigned long flags; | |
408 | ||
409 | spin_lock_irqsave(&dev->lock, flags); | |
410 | dev->printer_cdev_open = 0; | |
411 | fd->private_data = NULL; | |
412 | /* Change printer status to show that the printer is off-line. */ | |
413 | dev->printer_status &= ~PRINTER_SELECTED; | |
414 | spin_unlock_irqrestore(&dev->lock, flags); | |
415 | ||
416 | DBG(dev, "printer_close\n"); | |
417 | ||
418 | return 0; | |
419 | } | |
420 | ||
cc901bbb CN |
421 | /* This function must be called with interrupts turned off. */ |
422 | static void | |
423 | setup_rx_reqs(struct printer_dev *dev) | |
424 | { | |
425 | struct usb_request *req; | |
426 | ||
427 | while (likely(!list_empty(&dev->rx_reqs))) { | |
428 | int error; | |
429 | ||
430 | req = container_of(dev->rx_reqs.next, | |
431 | struct usb_request, list); | |
432 | list_del_init(&req->list); | |
433 | ||
434 | /* The USB Host sends us whatever amount of data it wants to | |
435 | * so we always set the length field to the full USB_BUFSIZE. | |
436 | * If the amount of data is more than the read() caller asked | |
437 | * for it will be stored in the request buffer until it is | |
438 | * asked for by read(). | |
439 | */ | |
440 | req->length = USB_BUFSIZE; | |
441 | req->complete = rx_complete; | |
442 | ||
443 | error = usb_ep_queue(dev->out_ep, req, GFP_ATOMIC); | |
444 | if (error) { | |
445 | DBG(dev, "rx submit --> %d\n", error); | |
446 | list_add(&req->list, &dev->rx_reqs); | |
447 | break; | |
448 | } else { | |
449 | list_add(&req->list, &dev->rx_reqs_active); | |
450 | } | |
451 | } | |
452 | } | |
453 | ||
25a010c8 CN |
454 | static ssize_t |
455 | printer_read(struct file *fd, char __user *buf, size_t len, loff_t *ptr) | |
456 | { | |
457 | struct printer_dev *dev = fd->private_data; | |
458 | unsigned long flags; | |
459 | size_t size; | |
460 | size_t bytes_copied; | |
461 | struct usb_request *req; | |
462 | /* This is a pointer to the current USB rx request. */ | |
463 | struct usb_request *current_rx_req; | |
464 | /* This is the number of bytes in the current rx buffer. */ | |
465 | size_t current_rx_bytes; | |
466 | /* This is a pointer to the current rx buffer. */ | |
467 | u8 *current_rx_buf; | |
468 | ||
469 | if (len == 0) | |
470 | return -EINVAL; | |
471 | ||
472 | DBG(dev, "printer_read trying to read %d bytes\n", (int)len); | |
473 | ||
10ca4425 | 474 | mutex_lock(&dev->lock_printer_io); |
25a010c8 CN |
475 | spin_lock_irqsave(&dev->lock, flags); |
476 | ||
477 | /* We will use this flag later to check if a printer reset happened | |
478 | * after we turn interrupts back on. | |
479 | */ | |
480 | dev->reset_printer = 0; | |
481 | ||
cc901bbb | 482 | setup_rx_reqs(dev); |
25a010c8 CN |
483 | |
484 | bytes_copied = 0; | |
485 | current_rx_req = dev->current_rx_req; | |
486 | current_rx_bytes = dev->current_rx_bytes; | |
487 | current_rx_buf = dev->current_rx_buf; | |
488 | dev->current_rx_req = NULL; | |
489 | dev->current_rx_bytes = 0; | |
490 | dev->current_rx_buf = NULL; | |
491 | ||
492 | /* Check if there is any data in the read buffers. Please note that | |
493 | * current_rx_bytes is the number of bytes in the current rx buffer. | |
494 | * If it is zero then check if there are any other rx_buffers that | |
495 | * are on the completed list. We are only out of data if all rx | |
496 | * buffers are empty. | |
497 | */ | |
498 | if ((current_rx_bytes == 0) && | |
499 | (likely(list_empty(&dev->rx_buffers)))) { | |
500 | /* Turn interrupts back on before sleeping. */ | |
501 | spin_unlock_irqrestore(&dev->lock, flags); | |
502 | ||
503 | /* | |
504 | * If no data is available check if this is a NON-Blocking | |
505 | * call or not. | |
506 | */ | |
507 | if (fd->f_flags & (O_NONBLOCK|O_NDELAY)) { | |
10ca4425 | 508 | mutex_unlock(&dev->lock_printer_io); |
25a010c8 CN |
509 | return -EAGAIN; |
510 | } | |
511 | ||
512 | /* Sleep until data is available */ | |
513 | wait_event_interruptible(dev->rx_wait, | |
514 | (likely(!list_empty(&dev->rx_buffers)))); | |
515 | spin_lock_irqsave(&dev->lock, flags); | |
516 | } | |
517 | ||
518 | /* We have data to return then copy it to the caller's buffer.*/ | |
519 | while ((current_rx_bytes || likely(!list_empty(&dev->rx_buffers))) | |
520 | && len) { | |
521 | if (current_rx_bytes == 0) { | |
522 | req = container_of(dev->rx_buffers.next, | |
523 | struct usb_request, list); | |
524 | list_del_init(&req->list); | |
525 | ||
526 | if (req->actual && req->buf) { | |
527 | current_rx_req = req; | |
528 | current_rx_bytes = req->actual; | |
529 | current_rx_buf = req->buf; | |
530 | } else { | |
531 | list_add(&req->list, &dev->rx_reqs); | |
532 | continue; | |
533 | } | |
534 | } | |
535 | ||
536 | /* Don't leave irqs off while doing memory copies */ | |
537 | spin_unlock_irqrestore(&dev->lock, flags); | |
538 | ||
539 | if (len > current_rx_bytes) | |
540 | size = current_rx_bytes; | |
541 | else | |
542 | size = len; | |
543 | ||
544 | size -= copy_to_user(buf, current_rx_buf, size); | |
545 | bytes_copied += size; | |
546 | len -= size; | |
547 | buf += size; | |
548 | ||
549 | spin_lock_irqsave(&dev->lock, flags); | |
550 | ||
cc901bbb | 551 | /* We've disconnected or reset so return. */ |
25a010c8 | 552 | if (dev->reset_printer) { |
cc901bbb | 553 | list_add(¤t_rx_req->list, &dev->rx_reqs); |
25a010c8 | 554 | spin_unlock_irqrestore(&dev->lock, flags); |
10ca4425 | 555 | mutex_unlock(&dev->lock_printer_io); |
25a010c8 CN |
556 | return -EAGAIN; |
557 | } | |
558 | ||
559 | /* If we not returning all the data left in this RX request | |
560 | * buffer then adjust the amount of data left in the buffer. | |
561 | * Othewise if we are done with this RX request buffer then | |
562 | * requeue it to get any incoming data from the USB host. | |
563 | */ | |
564 | if (size < current_rx_bytes) { | |
565 | current_rx_bytes -= size; | |
566 | current_rx_buf += size; | |
567 | } else { | |
568 | list_add(¤t_rx_req->list, &dev->rx_reqs); | |
569 | current_rx_bytes = 0; | |
570 | current_rx_buf = NULL; | |
571 | current_rx_req = NULL; | |
572 | } | |
573 | } | |
574 | ||
575 | dev->current_rx_req = current_rx_req; | |
576 | dev->current_rx_bytes = current_rx_bytes; | |
577 | dev->current_rx_buf = current_rx_buf; | |
578 | ||
579 | spin_unlock_irqrestore(&dev->lock, flags); | |
10ca4425 | 580 | mutex_unlock(&dev->lock_printer_io); |
25a010c8 CN |
581 | |
582 | DBG(dev, "printer_read returned %d bytes\n", (int)bytes_copied); | |
583 | ||
584 | if (bytes_copied) | |
585 | return bytes_copied; | |
586 | else | |
587 | return -EAGAIN; | |
588 | } | |
589 | ||
590 | static ssize_t | |
591 | printer_write(struct file *fd, const char __user *buf, size_t len, loff_t *ptr) | |
592 | { | |
593 | struct printer_dev *dev = fd->private_data; | |
594 | unsigned long flags; | |
595 | size_t size; /* Amount of data in a TX request. */ | |
596 | size_t bytes_copied = 0; | |
597 | struct usb_request *req; | |
598 | ||
599 | DBG(dev, "printer_write trying to send %d bytes\n", (int)len); | |
600 | ||
601 | if (len == 0) | |
602 | return -EINVAL; | |
603 | ||
10ca4425 | 604 | mutex_lock(&dev->lock_printer_io); |
25a010c8 CN |
605 | spin_lock_irqsave(&dev->lock, flags); |
606 | ||
607 | /* Check if a printer reset happens while we have interrupts on */ | |
608 | dev->reset_printer = 0; | |
609 | ||
610 | /* Check if there is any available write buffers */ | |
611 | if (likely(list_empty(&dev->tx_reqs))) { | |
612 | /* Turn interrupts back on before sleeping. */ | |
613 | spin_unlock_irqrestore(&dev->lock, flags); | |
614 | ||
615 | /* | |
616 | * If write buffers are available check if this is | |
617 | * a NON-Blocking call or not. | |
618 | */ | |
619 | if (fd->f_flags & (O_NONBLOCK|O_NDELAY)) { | |
10ca4425 | 620 | mutex_unlock(&dev->lock_printer_io); |
25a010c8 CN |
621 | return -EAGAIN; |
622 | } | |
623 | ||
624 | /* Sleep until a write buffer is available */ | |
625 | wait_event_interruptible(dev->tx_wait, | |
626 | (likely(!list_empty(&dev->tx_reqs)))); | |
627 | spin_lock_irqsave(&dev->lock, flags); | |
628 | } | |
629 | ||
630 | while (likely(!list_empty(&dev->tx_reqs)) && len) { | |
631 | ||
632 | if (len > USB_BUFSIZE) | |
633 | size = USB_BUFSIZE; | |
634 | else | |
635 | size = len; | |
636 | ||
637 | req = container_of(dev->tx_reqs.next, struct usb_request, | |
638 | list); | |
639 | list_del_init(&req->list); | |
640 | ||
641 | req->complete = tx_complete; | |
642 | req->length = size; | |
643 | ||
644 | /* Check if we need to send a zero length packet. */ | |
645 | if (len > size) | |
646 | /* They will be more TX requests so no yet. */ | |
647 | req->zero = 0; | |
648 | else | |
649 | /* If the data amount is not a multple of the | |
650 | * maxpacket size then send a zero length packet. | |
651 | */ | |
652 | req->zero = ((len % dev->in_ep->maxpacket) == 0); | |
653 | ||
654 | /* Don't leave irqs off while doing memory copies */ | |
655 | spin_unlock_irqrestore(&dev->lock, flags); | |
656 | ||
657 | if (copy_from_user(req->buf, buf, size)) { | |
658 | list_add(&req->list, &dev->tx_reqs); | |
10ca4425 | 659 | mutex_unlock(&dev->lock_printer_io); |
25a010c8 CN |
660 | return bytes_copied; |
661 | } | |
662 | ||
663 | bytes_copied += size; | |
664 | len -= size; | |
665 | buf += size; | |
666 | ||
667 | spin_lock_irqsave(&dev->lock, flags); | |
668 | ||
669 | /* We've disconnected or reset so free the req and buffer */ | |
670 | if (dev->reset_printer) { | |
cc901bbb | 671 | list_add(&req->list, &dev->tx_reqs); |
25a010c8 | 672 | spin_unlock_irqrestore(&dev->lock, flags); |
10ca4425 | 673 | mutex_unlock(&dev->lock_printer_io); |
25a010c8 CN |
674 | return -EAGAIN; |
675 | } | |
676 | ||
677 | if (usb_ep_queue(dev->in_ep, req, GFP_ATOMIC)) { | |
678 | list_add(&req->list, &dev->tx_reqs); | |
679 | spin_unlock_irqrestore(&dev->lock, flags); | |
10ca4425 | 680 | mutex_unlock(&dev->lock_printer_io); |
25a010c8 CN |
681 | return -EAGAIN; |
682 | } | |
683 | ||
684 | list_add(&req->list, &dev->tx_reqs_active); | |
685 | ||
686 | } | |
687 | ||
688 | spin_unlock_irqrestore(&dev->lock, flags); | |
10ca4425 | 689 | mutex_unlock(&dev->lock_printer_io); |
25a010c8 CN |
690 | |
691 | DBG(dev, "printer_write sent %d bytes\n", (int)bytes_copied); | |
692 | ||
693 | if (bytes_copied) { | |
694 | return bytes_copied; | |
695 | } else { | |
696 | return -EAGAIN; | |
697 | } | |
698 | } | |
699 | ||
700 | static int | |
02c24a82 | 701 | printer_fsync(struct file *fd, loff_t start, loff_t end, int datasync) |
25a010c8 CN |
702 | { |
703 | struct printer_dev *dev = fd->private_data; | |
02c24a82 | 704 | struct inode *inode = fd->f_path.dentry->d_inode; |
25a010c8 CN |
705 | unsigned long flags; |
706 | int tx_list_empty; | |
707 | ||
02c24a82 | 708 | mutex_lock(&inode->i_mutex); |
25a010c8 CN |
709 | spin_lock_irqsave(&dev->lock, flags); |
710 | tx_list_empty = (likely(list_empty(&dev->tx_reqs))); | |
711 | spin_unlock_irqrestore(&dev->lock, flags); | |
712 | ||
713 | if (!tx_list_empty) { | |
714 | /* Sleep until all data has been sent */ | |
715 | wait_event_interruptible(dev->tx_flush_wait, | |
716 | (likely(list_empty(&dev->tx_reqs_active)))); | |
717 | } | |
02c24a82 | 718 | mutex_unlock(&inode->i_mutex); |
25a010c8 CN |
719 | |
720 | return 0; | |
721 | } | |
722 | ||
723 | static unsigned int | |
724 | printer_poll(struct file *fd, poll_table *wait) | |
725 | { | |
726 | struct printer_dev *dev = fd->private_data; | |
727 | unsigned long flags; | |
728 | int status = 0; | |
729 | ||
10ca4425 | 730 | mutex_lock(&dev->lock_printer_io); |
cc901bbb CN |
731 | spin_lock_irqsave(&dev->lock, flags); |
732 | setup_rx_reqs(dev); | |
733 | spin_unlock_irqrestore(&dev->lock, flags); | |
10ca4425 | 734 | mutex_unlock(&dev->lock_printer_io); |
cc901bbb | 735 | |
25a010c8 CN |
736 | poll_wait(fd, &dev->rx_wait, wait); |
737 | poll_wait(fd, &dev->tx_wait, wait); | |
738 | ||
739 | spin_lock_irqsave(&dev->lock, flags); | |
740 | if (likely(!list_empty(&dev->tx_reqs))) | |
741 | status |= POLLOUT | POLLWRNORM; | |
742 | ||
cc901bbb CN |
743 | if (likely(dev->current_rx_bytes) || |
744 | likely(!list_empty(&dev->rx_buffers))) | |
25a010c8 CN |
745 | status |= POLLIN | POLLRDNORM; |
746 | ||
747 | spin_unlock_irqrestore(&dev->lock, flags); | |
748 | ||
749 | return status; | |
750 | } | |
751 | ||
44c389a0 AC |
752 | static long |
753 | printer_ioctl(struct file *fd, unsigned int code, unsigned long arg) | |
25a010c8 CN |
754 | { |
755 | struct printer_dev *dev = fd->private_data; | |
756 | unsigned long flags; | |
757 | int status = 0; | |
758 | ||
759 | DBG(dev, "printer_ioctl: cmd=0x%4.4x, arg=%lu\n", code, arg); | |
760 | ||
761 | /* handle ioctls */ | |
762 | ||
763 | spin_lock_irqsave(&dev->lock, flags); | |
764 | ||
765 | switch (code) { | |
766 | case GADGET_GET_PRINTER_STATUS: | |
767 | status = (int)dev->printer_status; | |
768 | break; | |
769 | case GADGET_SET_PRINTER_STATUS: | |
770 | dev->printer_status = (u8)arg; | |
771 | break; | |
772 | default: | |
773 | /* could not handle ioctl */ | |
774 | DBG(dev, "printer_ioctl: ERROR cmd=0x%4.4xis not supported\n", | |
775 | code); | |
776 | status = -ENOTTY; | |
777 | } | |
778 | ||
779 | spin_unlock_irqrestore(&dev->lock, flags); | |
780 | ||
781 | return status; | |
782 | } | |
783 | ||
784 | /* used after endpoint configuration */ | |
828c0950 | 785 | static const struct file_operations printer_io_operations = { |
25a010c8 CN |
786 | .owner = THIS_MODULE, |
787 | .open = printer_open, | |
788 | .read = printer_read, | |
789 | .write = printer_write, | |
790 | .fsync = printer_fsync, | |
791 | .poll = printer_poll, | |
44c389a0 | 792 | .unlocked_ioctl = printer_ioctl, |
6038f373 AB |
793 | .release = printer_close, |
794 | .llseek = noop_llseek, | |
25a010c8 CN |
795 | }; |
796 | ||
797 | /*-------------------------------------------------------------------------*/ | |
798 | ||
799 | static int | |
800 | set_printer_interface(struct printer_dev *dev) | |
801 | { | |
802 | int result = 0; | |
803 | ||
72c973dd | 804 | dev->in_ep->desc = ep_desc(dev->gadget, &hs_ep_in_desc, &fs_ep_in_desc); |
25a010c8 CN |
805 | dev->in_ep->driver_data = dev; |
806 | ||
72c973dd TB |
807 | dev->out_ep->desc = ep_desc(dev->gadget, &hs_ep_out_desc, |
808 | &fs_ep_out_desc); | |
25a010c8 CN |
809 | dev->out_ep->driver_data = dev; |
810 | ||
72c973dd | 811 | result = usb_ep_enable(dev->in_ep); |
25a010c8 CN |
812 | if (result != 0) { |
813 | DBG(dev, "enable %s --> %d\n", dev->in_ep->name, result); | |
814 | goto done; | |
815 | } | |
816 | ||
72c973dd | 817 | result = usb_ep_enable(dev->out_ep); |
25a010c8 CN |
818 | if (result != 0) { |
819 | DBG(dev, "enable %s --> %d\n", dev->in_ep->name, result); | |
820 | goto done; | |
821 | } | |
822 | ||
823 | done: | |
824 | /* on error, disable any endpoints */ | |
825 | if (result != 0) { | |
826 | (void) usb_ep_disable(dev->in_ep); | |
827 | (void) usb_ep_disable(dev->out_ep); | |
72c973dd TB |
828 | dev->in_ep->desc = NULL; |
829 | dev->out_ep->desc = NULL; | |
25a010c8 CN |
830 | } |
831 | ||
832 | /* caller is responsible for cleanup on error */ | |
833 | return result; | |
834 | } | |
835 | ||
836 | static void printer_reset_interface(struct printer_dev *dev) | |
837 | { | |
838 | if (dev->interface < 0) | |
839 | return; | |
840 | ||
441b62c1 | 841 | DBG(dev, "%s\n", __func__); |
25a010c8 | 842 | |
72c973dd | 843 | if (dev->in_ep->desc) |
25a010c8 CN |
844 | usb_ep_disable(dev->in_ep); |
845 | ||
72c973dd | 846 | if (dev->out_ep->desc) |
25a010c8 CN |
847 | usb_ep_disable(dev->out_ep); |
848 | ||
72c973dd TB |
849 | dev->in_ep->desc = NULL; |
850 | dev->out_ep->desc = NULL; | |
25a010c8 CN |
851 | dev->interface = -1; |
852 | } | |
853 | ||
25a010c8 | 854 | /* Change our operational Interface. */ |
2e87edf4 | 855 | static int set_interface(struct printer_dev *dev, unsigned number) |
25a010c8 CN |
856 | { |
857 | int result = 0; | |
858 | ||
25a010c8 | 859 | /* Free the current interface */ |
5a175bb8 | 860 | printer_reset_interface(dev); |
25a010c8 | 861 | |
5a175bb8 SAS |
862 | result = set_printer_interface(dev); |
863 | if (result) | |
864 | printer_reset_interface(dev); | |
865 | else | |
866 | dev->interface = number; | |
25a010c8 CN |
867 | |
868 | if (!result) | |
869 | INFO(dev, "Using interface %x\n", number); | |
870 | ||
871 | return result; | |
872 | } | |
873 | ||
25a010c8 CN |
874 | static void printer_soft_reset(struct printer_dev *dev) |
875 | { | |
876 | struct usb_request *req; | |
877 | ||
878 | INFO(dev, "Received Printer Reset Request\n"); | |
879 | ||
880 | if (usb_ep_disable(dev->in_ep)) | |
881 | DBG(dev, "Failed to disable USB in_ep\n"); | |
882 | if (usb_ep_disable(dev->out_ep)) | |
883 | DBG(dev, "Failed to disable USB out_ep\n"); | |
884 | ||
885 | if (dev->current_rx_req != NULL) { | |
886 | list_add(&dev->current_rx_req->list, &dev->rx_reqs); | |
887 | dev->current_rx_req = NULL; | |
888 | } | |
889 | dev->current_rx_bytes = 0; | |
890 | dev->current_rx_buf = NULL; | |
891 | dev->reset_printer = 1; | |
892 | ||
893 | while (likely(!(list_empty(&dev->rx_buffers)))) { | |
894 | req = container_of(dev->rx_buffers.next, struct usb_request, | |
895 | list); | |
896 | list_del_init(&req->list); | |
897 | list_add(&req->list, &dev->rx_reqs); | |
898 | } | |
899 | ||
900 | while (likely(!(list_empty(&dev->rx_reqs_active)))) { | |
901 | req = container_of(dev->rx_buffers.next, struct usb_request, | |
902 | list); | |
903 | list_del_init(&req->list); | |
904 | list_add(&req->list, &dev->rx_reqs); | |
905 | } | |
906 | ||
907 | while (likely(!(list_empty(&dev->tx_reqs_active)))) { | |
908 | req = container_of(dev->tx_reqs_active.next, | |
909 | struct usb_request, list); | |
910 | list_del_init(&req->list); | |
911 | list_add(&req->list, &dev->tx_reqs); | |
912 | } | |
913 | ||
72c973dd | 914 | if (usb_ep_enable(dev->in_ep)) |
25a010c8 | 915 | DBG(dev, "Failed to enable USB in_ep\n"); |
72c973dd | 916 | if (usb_ep_enable(dev->out_ep)) |
25a010c8 CN |
917 | DBG(dev, "Failed to enable USB out_ep\n"); |
918 | ||
cc901bbb | 919 | wake_up_interruptible(&dev->rx_wait); |
25a010c8 CN |
920 | wake_up_interruptible(&dev->tx_wait); |
921 | wake_up_interruptible(&dev->tx_flush_wait); | |
922 | } | |
923 | ||
924 | /*-------------------------------------------------------------------------*/ | |
925 | ||
926 | /* | |
927 | * The setup() callback implements all the ep0 functionality that's not | |
928 | * handled lower down. | |
929 | */ | |
2e87edf4 SAS |
930 | static int printer_func_setup(struct usb_function *f, |
931 | const struct usb_ctrlrequest *ctrl) | |
25a010c8 | 932 | { |
2e87edf4 SAS |
933 | struct printer_dev *dev = container_of(f, struct printer_dev, function); |
934 | struct usb_composite_dev *cdev = f->config->cdev; | |
935 | struct usb_request *req = cdev->req; | |
25a010c8 CN |
936 | int value = -EOPNOTSUPP; |
937 | u16 wIndex = le16_to_cpu(ctrl->wIndex); | |
938 | u16 wValue = le16_to_cpu(ctrl->wValue); | |
939 | u16 wLength = le16_to_cpu(ctrl->wLength); | |
940 | ||
941 | DBG(dev, "ctrl req%02x.%02x v%04x i%04x l%d\n", | |
942 | ctrl->bRequestType, ctrl->bRequest, wValue, wIndex, wLength); | |
943 | ||
25a010c8 | 944 | switch (ctrl->bRequestType&USB_TYPE_MASK) { |
25a010c8 CN |
945 | case USB_TYPE_CLASS: |
946 | switch (ctrl->bRequest) { | |
947 | case 0: /* Get the IEEE-1284 PNP String */ | |
948 | /* Only one printer interface is supported. */ | |
5a175bb8 | 949 | if ((wIndex>>8) != dev->interface) |
25a010c8 CN |
950 | break; |
951 | ||
952 | value = (pnp_string[0]<<8)|pnp_string[1]; | |
953 | memcpy(req->buf, pnp_string, value); | |
954 | DBG(dev, "1284 PNP String: %x %s\n", value, | |
955 | &pnp_string[2]); | |
956 | break; | |
957 | ||
958 | case 1: /* Get Port Status */ | |
959 | /* Only one printer interface is supported. */ | |
5a175bb8 | 960 | if (wIndex != dev->interface) |
25a010c8 CN |
961 | break; |
962 | ||
963 | *(u8 *)req->buf = dev->printer_status; | |
964 | value = min(wLength, (u16) 1); | |
965 | break; | |
966 | ||
967 | case 2: /* Soft Reset */ | |
968 | /* Only one printer interface is supported. */ | |
5a175bb8 | 969 | if (wIndex != dev->interface) |
25a010c8 CN |
970 | break; |
971 | ||
972 | printer_soft_reset(dev); | |
973 | ||
974 | value = 0; | |
975 | break; | |
976 | ||
977 | default: | |
978 | goto unknown; | |
979 | } | |
980 | break; | |
981 | ||
982 | default: | |
983 | unknown: | |
984 | VDBG(dev, | |
985 | "unknown ctrl req%02x.%02x v%04x i%04x l%d\n", | |
986 | ctrl->bRequestType, ctrl->bRequest, | |
987 | wValue, wIndex, wLength); | |
988 | break; | |
989 | } | |
25a010c8 CN |
990 | /* host either stalls (value < 0) or reports success */ |
991 | return value; | |
992 | } | |
993 | ||
2e87edf4 SAS |
994 | static int __init printer_func_bind(struct usb_configuration *c, |
995 | struct usb_function *f) | |
996 | { | |
5a175bb8 SAS |
997 | struct printer_dev *dev = container_of(f, struct printer_dev, function); |
998 | struct usb_composite_dev *cdev = c->cdev; | |
999 | struct usb_ep *in_ep, *out_ep; | |
1000 | int id; | |
1001 | ||
1002 | id = usb_interface_id(c, f); | |
1003 | if (id < 0) | |
1004 | return id; | |
1005 | intf_desc.bInterfaceNumber = id; | |
1006 | ||
1007 | /* all we really need is bulk IN/OUT */ | |
1008 | in_ep = usb_ep_autoconfig(cdev->gadget, &fs_ep_in_desc); | |
1009 | if (!in_ep) { | |
1010 | autoconf_fail: | |
1011 | dev_err(&cdev->gadget->dev, "can't autoconfigure on %s\n", | |
1012 | cdev->gadget->name); | |
1013 | return -ENODEV; | |
1014 | } | |
1015 | in_ep->driver_data = in_ep; /* claim */ | |
1016 | ||
1017 | out_ep = usb_ep_autoconfig(cdev->gadget, &fs_ep_out_desc); | |
1018 | if (!out_ep) | |
1019 | goto autoconf_fail; | |
1020 | out_ep->driver_data = out_ep; /* claim */ | |
1021 | ||
1022 | /* assumes that all endpoints are dual-speed */ | |
1023 | hs_ep_in_desc.bEndpointAddress = fs_ep_in_desc.bEndpointAddress; | |
1024 | hs_ep_out_desc.bEndpointAddress = fs_ep_out_desc.bEndpointAddress; | |
1025 | ||
1026 | dev->in_ep = in_ep; | |
1027 | dev->out_ep = out_ep; | |
2e87edf4 SAS |
1028 | return 0; |
1029 | } | |
1030 | ||
1031 | static void printer_func_unbind(struct usb_configuration *c, | |
1032 | struct usb_function *f) | |
1033 | { | |
1034 | } | |
1035 | ||
1036 | static int printer_func_set_alt(struct usb_function *f, | |
1037 | unsigned intf, unsigned alt) | |
25a010c8 | 1038 | { |
2e87edf4 SAS |
1039 | struct printer_dev *dev = container_of(f, struct printer_dev, function); |
1040 | int ret = -ENOTSUPP; | |
1041 | ||
1042 | if (!alt) | |
5a175bb8 SAS |
1043 | ret = set_interface(dev, intf); |
1044 | ||
2e87edf4 SAS |
1045 | return ret; |
1046 | } | |
1047 | ||
1048 | static void printer_func_disable(struct usb_function *f) | |
1049 | { | |
1050 | struct printer_dev *dev = container_of(f, struct printer_dev, function); | |
25a010c8 CN |
1051 | unsigned long flags; |
1052 | ||
441b62c1 | 1053 | DBG(dev, "%s\n", __func__); |
25a010c8 CN |
1054 | |
1055 | spin_lock_irqsave(&dev->lock, flags); | |
25a010c8 | 1056 | printer_reset_interface(dev); |
25a010c8 CN |
1057 | spin_unlock_irqrestore(&dev->lock, flags); |
1058 | } | |
1059 | ||
2e87edf4 | 1060 | static void printer_cfg_unbind(struct usb_configuration *c) |
25a010c8 | 1061 | { |
2e87edf4 | 1062 | struct printer_dev *dev; |
25a010c8 CN |
1063 | struct usb_request *req; |
1064 | ||
2e87edf4 | 1065 | dev = &usb_printer_gadget; |
25a010c8 | 1066 | |
441b62c1 | 1067 | DBG(dev, "%s\n", __func__); |
25a010c8 CN |
1068 | |
1069 | /* Remove sysfs files */ | |
1070 | device_destroy(usb_gadget_class, g_printer_devno); | |
1071 | ||
1072 | /* Remove Character Device */ | |
1073 | cdev_del(&dev->printer_cdev); | |
1074 | ||
1075 | /* we must already have been disconnected ... no i/o may be active */ | |
1076 | WARN_ON(!list_empty(&dev->tx_reqs_active)); | |
1077 | WARN_ON(!list_empty(&dev->rx_reqs_active)); | |
1078 | ||
1079 | /* Free all memory for this driver. */ | |
1080 | while (!list_empty(&dev->tx_reqs)) { | |
1081 | req = container_of(dev->tx_reqs.next, struct usb_request, | |
1082 | list); | |
1083 | list_del(&req->list); | |
1084 | printer_req_free(dev->in_ep, req); | |
1085 | } | |
1086 | ||
efa66f14 | 1087 | if (dev->current_rx_req != NULL) |
25a010c8 CN |
1088 | printer_req_free(dev->out_ep, dev->current_rx_req); |
1089 | ||
1090 | while (!list_empty(&dev->rx_reqs)) { | |
1091 | req = container_of(dev->rx_reqs.next, | |
1092 | struct usb_request, list); | |
1093 | list_del(&req->list); | |
1094 | printer_req_free(dev->out_ep, req); | |
1095 | } | |
1096 | ||
1097 | while (!list_empty(&dev->rx_buffers)) { | |
1098 | req = container_of(dev->rx_buffers.next, | |
1099 | struct usb_request, list); | |
1100 | list_del(&req->list); | |
1101 | printer_req_free(dev->out_ep, req); | |
1102 | } | |
25a010c8 CN |
1103 | } |
1104 | ||
2e87edf4 SAS |
1105 | static struct usb_configuration printer_cfg_driver = { |
1106 | .label = "printer", | |
1107 | .unbind = printer_cfg_unbind, | |
1108 | .bConfigurationValue = 1, | |
1109 | .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER, | |
1110 | }; | |
1111 | ||
1112 | static int __init printer_bind_config(struct usb_configuration *c) | |
25a010c8 | 1113 | { |
2e87edf4 | 1114 | struct usb_gadget *gadget = c->cdev->gadget; |
25a010c8 | 1115 | struct printer_dev *dev; |
25a010c8 CN |
1116 | int status = -ENOMEM; |
1117 | int gcnum; | |
1118 | size_t len; | |
1119 | u32 i; | |
1120 | struct usb_request *req; | |
1121 | ||
5a175bb8 SAS |
1122 | usb_ep_autoconfig_reset(gadget); |
1123 | ||
25a010c8 CN |
1124 | dev = &usb_printer_gadget; |
1125 | ||
2e87edf4 SAS |
1126 | dev->function.name = shortname; |
1127 | dev->function.descriptors = fs_printer_function; | |
1128 | dev->function.hs_descriptors = hs_printer_function; | |
1129 | dev->function.bind = printer_func_bind; | |
1130 | dev->function.setup = printer_func_setup; | |
1131 | dev->function.unbind = printer_func_unbind; | |
1132 | dev->function.set_alt = printer_func_set_alt; | |
1133 | dev->function.disable = printer_func_disable; | |
25a010c8 | 1134 | |
5a175bb8 SAS |
1135 | status = usb_add_function(c, &dev->function); |
1136 | if (status) | |
1137 | return status; | |
1138 | ||
25a010c8 | 1139 | /* Setup the sysfs files for the printer gadget. */ |
b0b090e5 GKH |
1140 | dev->pdev = device_create(usb_gadget_class, NULL, g_printer_devno, |
1141 | NULL, "g_printer"); | |
25a010c8 CN |
1142 | if (IS_ERR(dev->pdev)) { |
1143 | ERROR(dev, "Failed to create device: g_printer\n"); | |
1144 | goto fail; | |
1145 | } | |
1146 | ||
1147 | /* | |
1148 | * Register a character device as an interface to a user mode | |
1149 | * program that handles the printer specific functionality. | |
1150 | */ | |
1151 | cdev_init(&dev->printer_cdev, &printer_io_operations); | |
1152 | dev->printer_cdev.owner = THIS_MODULE; | |
1153 | status = cdev_add(&dev->printer_cdev, g_printer_devno, 1); | |
1154 | if (status) { | |
1155 | ERROR(dev, "Failed to open char device\n"); | |
1156 | goto fail; | |
1157 | } | |
1158 | ||
25a010c8 CN |
1159 | gcnum = usb_gadget_controller_number(gadget); |
1160 | if (gcnum >= 0) { | |
1161 | device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum); | |
1162 | } else { | |
1163 | dev_warn(&gadget->dev, "controller '%s' not recognized\n", | |
1164 | gadget->name); | |
1165 | /* unrecognized, but safe unless bulk is REALLY quirky */ | |
1166 | device_desc.bcdDevice = | |
551509d2 | 1167 | cpu_to_le16(0xFFFF); |
25a010c8 CN |
1168 | } |
1169 | snprintf(manufacturer, sizeof(manufacturer), "%s %s with %s", | |
1170 | init_utsname()->sysname, init_utsname()->release, | |
1171 | gadget->name); | |
1172 | ||
25a010c8 CN |
1173 | if (iSerialNum) |
1174 | strlcpy(serial_num, iSerialNum, sizeof serial_num); | |
1175 | ||
1176 | if (iPNPstring) | |
1177 | strlcpy(&pnp_string[2], iPNPstring, (sizeof pnp_string)-2); | |
1178 | ||
1179 | len = strlen(pnp_string); | |
1180 | pnp_string[0] = (len >> 8) & 0xFF; | |
1181 | pnp_string[1] = len & 0xFF; | |
1182 | ||
25a010c8 CN |
1183 | usb_gadget_set_selfpowered(gadget); |
1184 | ||
1185 | if (gadget->is_otg) { | |
2e87edf4 SAS |
1186 | otg_descriptor.bmAttributes |= USB_OTG_HNP; |
1187 | printer_cfg_driver.descriptors = otg_desc; | |
1188 | printer_cfg_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP; | |
25a010c8 CN |
1189 | } |
1190 | ||
1191 | spin_lock_init(&dev->lock); | |
10ca4425 | 1192 | mutex_init(&dev->lock_printer_io); |
25a010c8 CN |
1193 | INIT_LIST_HEAD(&dev->tx_reqs); |
1194 | INIT_LIST_HEAD(&dev->tx_reqs_active); | |
1195 | INIT_LIST_HEAD(&dev->rx_reqs); | |
1196 | INIT_LIST_HEAD(&dev->rx_reqs_active); | |
1197 | INIT_LIST_HEAD(&dev->rx_buffers); | |
1198 | init_waitqueue_head(&dev->rx_wait); | |
1199 | init_waitqueue_head(&dev->tx_wait); | |
1200 | init_waitqueue_head(&dev->tx_flush_wait); | |
1201 | ||
25a010c8 CN |
1202 | dev->interface = -1; |
1203 | dev->printer_cdev_open = 0; | |
1204 | dev->printer_status = PRINTER_NOT_ERROR; | |
1205 | dev->current_rx_req = NULL; | |
1206 | dev->current_rx_bytes = 0; | |
1207 | dev->current_rx_buf = NULL; | |
1208 | ||
25a010c8 CN |
1209 | for (i = 0; i < QLEN; i++) { |
1210 | req = printer_req_alloc(dev->in_ep, USB_BUFSIZE, GFP_KERNEL); | |
1211 | if (!req) { | |
1212 | while (!list_empty(&dev->tx_reqs)) { | |
1213 | req = container_of(dev->tx_reqs.next, | |
1214 | struct usb_request, list); | |
1215 | list_del(&req->list); | |
1216 | printer_req_free(dev->in_ep, req); | |
1217 | } | |
1218 | return -ENOMEM; | |
1219 | } | |
1220 | list_add(&req->list, &dev->tx_reqs); | |
1221 | } | |
1222 | ||
1223 | for (i = 0; i < QLEN; i++) { | |
1224 | req = printer_req_alloc(dev->out_ep, USB_BUFSIZE, GFP_KERNEL); | |
1225 | if (!req) { | |
1226 | while (!list_empty(&dev->rx_reqs)) { | |
1227 | req = container_of(dev->rx_reqs.next, | |
1228 | struct usb_request, list); | |
1229 | list_del(&req->list); | |
1230 | printer_req_free(dev->out_ep, req); | |
1231 | } | |
1232 | return -ENOMEM; | |
1233 | } | |
1234 | list_add(&req->list, &dev->rx_reqs); | |
1235 | } | |
1236 | ||
25a010c8 CN |
1237 | /* finish hookup to lower layer ... */ |
1238 | dev->gadget = gadget; | |
25a010c8 CN |
1239 | |
1240 | INFO(dev, "%s, version: " DRIVER_VERSION "\n", driver_desc); | |
25a010c8 CN |
1241 | return 0; |
1242 | ||
1243 | fail: | |
2e87edf4 | 1244 | printer_cfg_unbind(c); |
25a010c8 CN |
1245 | return status; |
1246 | } | |
1247 | ||
2e87edf4 SAS |
1248 | static int printer_unbind(struct usb_composite_dev *cdev) |
1249 | { | |
1250 | return 0; | |
1251 | } | |
25a010c8 | 1252 | |
2e87edf4 SAS |
1253 | static int __init printer_bind(struct usb_composite_dev *cdev) |
1254 | { | |
5a175bb8 SAS |
1255 | int ret; |
1256 | ||
1257 | ret = usb_string_ids_tab(cdev, strings); | |
1258 | if (ret < 0) | |
1259 | return ret; | |
276e2e4f SAS |
1260 | device_desc.iManufacturer = strings[USB_GADGET_MANUFACTURER_IDX].id; |
1261 | device_desc.iProduct = strings[USB_GADGET_PRODUCT_IDX].id; | |
1262 | device_desc.iSerialNumber = strings[USB_GADGET_SERIAL_IDX].id; | |
5a175bb8 SAS |
1263 | |
1264 | ret = usb_add_config(cdev, &printer_cfg_driver, printer_bind_config); | |
7d16e8d3 SAS |
1265 | if (ret) |
1266 | return ret; | |
1267 | usb_composite_overwrite_options(cdev, &coverwrite); | |
5a175bb8 | 1268 | return ret; |
2e87edf4 | 1269 | } |
25a010c8 | 1270 | |
c2ec75c2 | 1271 | static __refdata struct usb_composite_driver printer_driver = { |
2e87edf4 SAS |
1272 | .name = shortname, |
1273 | .dev = &device_desc, | |
1274 | .strings = dev_strings, | |
1275 | .max_speed = USB_SPEED_HIGH, | |
03e42bd5 | 1276 | .bind = printer_bind, |
25a010c8 | 1277 | .unbind = printer_unbind, |
25a010c8 CN |
1278 | }; |
1279 | ||
25a010c8 CN |
1280 | static int __init |
1281 | init(void) | |
1282 | { | |
1283 | int status; | |
1284 | ||
1285 | usb_gadget_class = class_create(THIS_MODULE, "usb_printer_gadget"); | |
1286 | if (IS_ERR(usb_gadget_class)) { | |
1287 | status = PTR_ERR(usb_gadget_class); | |
2e87edf4 | 1288 | pr_err("unable to create usb_gadget class %d\n", status); |
25a010c8 CN |
1289 | return status; |
1290 | } | |
1291 | ||
1292 | status = alloc_chrdev_region(&g_printer_devno, 0, 1, | |
1293 | "USB printer gadget"); | |
1294 | if (status) { | |
2e87edf4 | 1295 | pr_err("alloc_chrdev_region %d\n", status); |
25a010c8 CN |
1296 | class_destroy(usb_gadget_class); |
1297 | return status; | |
1298 | } | |
1299 | ||
03e42bd5 | 1300 | status = usb_composite_probe(&printer_driver); |
25a010c8 CN |
1301 | if (status) { |
1302 | class_destroy(usb_gadget_class); | |
1303 | unregister_chrdev_region(g_printer_devno, 1); | |
2e87edf4 | 1304 | pr_err("usb_gadget_probe_driver %x\n", status); |
25a010c8 CN |
1305 | } |
1306 | ||
1307 | return status; | |
1308 | } | |
1309 | module_init(init); | |
1310 | ||
1311 | static void __exit | |
1312 | cleanup(void) | |
1313 | { | |
10ca4425 | 1314 | mutex_lock(&usb_printer_gadget.lock_printer_io); |
2e87edf4 | 1315 | usb_composite_unregister(&printer_driver); |
8582d861 | 1316 | unregister_chrdev_region(g_printer_devno, 1); |
d5aa4751 | 1317 | class_destroy(usb_gadget_class); |
10ca4425 | 1318 | mutex_unlock(&usb_printer_gadget.lock_printer_io); |
25a010c8 CN |
1319 | } |
1320 | module_exit(cleanup); | |
2e87edf4 SAS |
1321 | |
1322 | MODULE_DESCRIPTION(DRIVER_DESC); | |
1323 | MODULE_AUTHOR("Craig Nadler"); | |
1324 | MODULE_LICENSE("GPL"); |