1 /*
2 * g_serial.c -- USB gadget serial driver
3 *
4 * Copyright 2003 (C) Al Borchers (alborchers@steinerpoint.com)
5 *
6 * This code is based in part on the Gadget Zero driver, which
7 * is Copyright (C) 2003 by David Brownell, all rights reserved.
8 *
9 * This code also borrows from usbserial.c, which is
10 * Copyright (C) 1999 - 2002 Greg Kroah-Hartman (greg@kroah.com)
11 * Copyright (C) 2000 Peter Berger (pberger@brimson.com)
12 * Copyright (C) 2000 Al Borchers (alborchers@steinerpoint.com)
13 *
14 * This software is distributed under the terms of the GNU General
15 * Public License ("GPL") as published by the Free Software Foundation,
16 * either version 2 of that License or (at your option) any later version.
17 *
18 */
19
20 #include <linux/config.h>
21 #include <linux/module.h>
22 #include <linux/kernel.h>
23 #include <linux/delay.h>
24 #include <linux/ioport.h>
25 #include <linux/sched.h>
26 #include <linux/slab.h>
27 #include <linux/smp_lock.h>
28 #include <linux/errno.h>
29 #include <linux/init.h>
30 #include <linux/timer.h>
31 #include <linux/list.h>
32 #include <linux/interrupt.h>
33 #include <linux/utsname.h>
34 #include <linux/wait.h>
35 #include <linux/proc_fs.h>
36 #include <linux/device.h>
37 #include <linux/tty.h>
38 #include <linux/tty_flip.h>
39
40 #include <asm/byteorder.h>
41 #include <asm/io.h>
42 #include <asm/irq.h>
43 #include <asm/system.h>
44 #include <asm/unaligned.h>
45 #include <asm/uaccess.h>
46
47 #include <linux/usb_ch9.h>
48 #include <linux/usb_gadget.h>
49
50 #include "gadget_chips.h"
51
52
53 /* Wait Cond */
54
55 #define __wait_cond_interruptible(wq, condition, lock, flags, ret) \
56 do { \
57 wait_queue_t __wait; \
58 init_waitqueue_entry(&__wait, current); \
59 \
60 add_wait_queue(&wq, &__wait); \
61 for (;;) { \
62 set_current_state(TASK_INTERRUPTIBLE); \
63 if (condition) \
64 break; \
65 if (!signal_pending(current)) { \
66 spin_unlock_irqrestore(lock, flags); \
67 schedule(); \
68 spin_lock_irqsave(lock, flags); \
69 continue; \
70 } \
71 ret = -ERESTARTSYS; \
72 break; \
73 } \
74 current->state = TASK_RUNNING; \
75 remove_wait_queue(&wq, &__wait); \
76 } while (0)
77
78 #define wait_cond_interruptible(wq, condition, lock, flags) \
79 ({ \
80 int __ret = 0; \
81 if (!(condition)) \
82 __wait_cond_interruptible(wq, condition, lock, flags, \
83 __ret); \
84 __ret; \
85 })
86
87 #define __wait_cond_interruptible_timeout(wq, condition, lock, flags, \
88 timeout, ret) \
89 do { \
90 signed long __timeout = timeout; \
91 wait_queue_t __wait; \
92 init_waitqueue_entry(&__wait, current); \
93 \
94 add_wait_queue(&wq, &__wait); \
95 for (;;) { \
96 set_current_state(TASK_INTERRUPTIBLE); \
97 if (__timeout == 0) \
98 break; \
99 if (condition) \
100 break; \
101 if (!signal_pending(current)) { \
102 spin_unlock_irqrestore(lock, flags); \
103 __timeout = schedule_timeout(__timeout); \
104 spin_lock_irqsave(lock, flags); \
105 continue; \
106 } \
107 ret = -ERESTARTSYS; \
108 break; \
109 } \
110 current->state = TASK_RUNNING; \
111 remove_wait_queue(&wq, &__wait); \
112 } while (0)
113
114 #define wait_cond_interruptible_timeout(wq, condition, lock, flags, \
115 timeout) \
116 ({ \
117 int __ret = 0; \
118 if (!(condition)) \
119 __wait_cond_interruptible_timeout(wq, condition, lock, \
120 flags, timeout, __ret); \
121 __ret; \
122 })
123
124
125 /* CDC-ACM Defines and Structures */
126
127 #define USB_CDC_SUBCLASS_ACM 2
128
129 #define USB_CDC_CTRL_PROTO_NONE 0
130 #define USB_CDC_CTRL_PROTO_AT 1
131 #define USB_CDC_CTRL_PROTO_VENDOR 0xff
132
133 #define USB_CDC_SUBTYPE_HEADER 0
134 #define USB_CDC_SUBTYPE_CALL_MGMT 1
135 #define USB_CDC_SUBTYPE_ACM 2
136 #define USB_CDC_SUBTYPE_UNION 6
137
138 #define USB_CDC_CALL_MGMT_CAP_CALL_MGMT 0x01
139 #define USB_CDC_CALL_MGMT_CAP_DATA_INTF 0x02
140
141 #define USB_CDC_REQ_SET_LINE_CODING 0x20
142 #define USB_CDC_REQ_GET_LINE_CODING 0x21
143 #define USB_CDC_REQ_SET_CONTROL_LINE_STATE 0x22
144
145 #define USB_CDC_1_STOP_BITS 0
146 #define USB_CDC_1_5_STOP_BITS 1
147 #define USB_CDC_2_STOP_BITS 2
148
149 #define USB_CDC_NO_PARITY 0
150 #define USB_CDC_ODD_PARITY 1
151 #define USB_CDC_EVEN_PARITY 2
152 #define USB_CDC_MARK_PARITY 3
153 #define USB_CDC_SPACE_PARITY 4
154
155 /* Header Functional Descriptor from CDC spec 5.2.3.1 */
156 struct usb_cdc_header_desc {
157 u8 bLength;
158 u8 bDescriptorType;
159 u8 bDescriptorSubType;
160 u16 bcdCDC;
161 } __attribute__ ((packed));
162
163 /* Call Management Descriptor from CDC spec 5.2.3.3 */
164 struct usb_cdc_call_mgmt_desc {
165 u8 bLength;
166 u8 bDescriptorType;
167 u8 bDescriptorSubType;
168 u8 bmCapabilities;
169 u8 bDataInterface;
170 } __attribute__ ((packed));
171
172 /* Abstract Control Management Descriptor from CDC spec 5.2.3.4 */
173 struct usb_cdc_acm_desc {
174 u8 bLength;
175 u8 bDescriptorType;
176 u8 bDescriptorSubType;
177 u8 bmCapabilities;
178 } __attribute__ ((packed));
179
180 /* Union Functional Descriptor from CDC spec 5.2.3.8 */
181 struct usb_cdc_union_desc {
182 u8 bLength;
183 u8 bDescriptorType;
184 u8 bDescriptorSubType;
185 u8 bMasterInterface0;
186 u8 bSlaveInterface0;
187 /* ... and there could be other slave interfaces */
188 } __attribute__ ((packed));
189
190 /* Line Coding Structure from CDC spec 6.2.13 */
191 struct usb_cdc_line_coding {
192 u32 dwDTERate;
193 u8 bCharFormat;
194 u8 bParityType;
195 u8 bDataBits;
196 } __attribute__ ((packed));
197
198
199 /* Defines */
200
201 #define GS_VERSION_STR "v2.0"
202 #define GS_VERSION_NUM 0x0200
203
204 #define GS_LONG_NAME "Gadget Serial"
205 #define GS_SHORT_NAME "g_serial"
206
207 #define GS_MAJOR 127
208 #define GS_MINOR_START 0
209
210 #define GS_NUM_PORTS 16
211
212 #define GS_NUM_CONFIGS 1
213 #define GS_NO_CONFIG_ID 0
214 #define GS_BULK_CONFIG_ID 1
215 #define GS_ACM_CONFIG_ID 2
216
217 #define GS_MAX_NUM_INTERFACES 2
218 #define GS_BULK_INTERFACE_ID 0
219 #define GS_CONTROL_INTERFACE_ID 0
220 #define GS_DATA_INTERFACE_ID 1
221
222 #define GS_MAX_DESC_LEN 256
223
224 #define GS_DEFAULT_READ_Q_SIZE 32
225 #define GS_DEFAULT_WRITE_Q_SIZE 32
226
227 #define GS_DEFAULT_WRITE_BUF_SIZE 8192
228 #define GS_TMP_BUF_SIZE 8192
229
230 #define GS_CLOSE_TIMEOUT 15
231
232 #define GS_DEFAULT_USE_ACM 0
233
234 #define GS_DEFAULT_DTE_RATE 9600
235 #define GS_DEFAULT_DATA_BITS 8
236 #define GS_DEFAULT_PARITY USB_CDC_NO_PARITY
237 #define GS_DEFAULT_CHAR_FORMAT USB_CDC_1_STOP_BITS
238
239 /* select highspeed/fullspeed, hiding highspeed if not configured */
240 #ifdef CONFIG_USB_GADGET_DUALSPEED
241 #define GS_SPEED_SELECT(is_hs,hs,fs) ((is_hs) ? (hs) : (fs))
242 #else
243 #define GS_SPEED_SELECT(is_hs,hs,fs) (fs)
244 #endif /* CONFIG_USB_GADGET_DUALSPEED */
245
246 /* debug settings */
247 #ifdef GS_DEBUG
248 static int debug = 1;
249
250 #define gs_debug(format, arg...) \
251 do { if (debug) printk(KERN_DEBUG format, ## arg); } while(0)
252 #define gs_debug_level(level, format, arg...) \
253 do { if (debug>=level) printk(KERN_DEBUG format, ## arg); } while(0)
254
255 #else
256
257 #define gs_debug(format, arg...) \
258 do { } while(0)
259 #define gs_debug_level(level, format, arg...) \
260 do { } while(0)
261
262 #endif /* GS_DEBUG */
263
264 /* Thanks to NetChip Technologies for donating this product ID.
265 *
266 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
267 * Instead: allocate your own, using normal USB-IF procedures.
268 */
269 #define GS_VENDOR_ID 0x0525 /* NetChip */
270 #define GS_PRODUCT_ID 0xa4a6 /* Linux-USB Serial Gadget */
271 #define GS_CDC_PRODUCT_ID 0xa4a7 /* ... as CDC-ACM */
272
273 #define GS_LOG2_NOTIFY_INTERVAL 5 /* 1 << 5 == 32 msec */
274 #define GS_NOTIFY_MAXPACKET 8
275
276
277 /* Structures */
278
279 struct gs_dev;
280
281 /* circular buffer */
282 struct gs_buf {
283 unsigned int buf_size;
284 char *buf_buf;
285 char *buf_get;
286 char *buf_put;
287 };
288
289 /* list of requests */
290 struct gs_req_entry {
291 struct list_head re_entry;
292 struct usb_request *re_req;
293 };
294
295 /* the port structure holds info for each port, one for each minor number */
296 struct gs_port {
297 struct gs_dev *port_dev; /* pointer to device struct */
298 struct tty_struct *port_tty; /* pointer to tty struct */
299 spinlock_t port_lock;
300 int port_num;
301 int port_open_count;
302 int port_in_use; /* open/close in progress */
303 wait_queue_head_t port_write_wait;/* waiting to write */
304 struct gs_buf *port_write_buf;
305 struct usb_cdc_line_coding port_line_coding;
306 };
307
308 /* the device structure holds info for the USB device */
309 struct gs_dev {
310 struct usb_gadget *dev_gadget; /* gadget device pointer */
311 spinlock_t dev_lock; /* lock for set/reset config */
312 int dev_config; /* configuration number */
313 struct usb_ep *dev_notify_ep; /* address of notify endpoint */
314 struct usb_ep *dev_in_ep; /* address of in endpoint */
315 struct usb_ep *dev_out_ep; /* address of out endpoint */
316 struct usb_endpoint_descriptor /* desciptor of notify ep */
317 *dev_notify_ep_desc;
318 struct usb_endpoint_descriptor /* descriptor of in endpoint */
319 *dev_in_ep_desc;
320 struct usb_endpoint_descriptor /* descriptor of out endpoint */
321 *dev_out_ep_desc;
322 struct usb_request *dev_ctrl_req; /* control request */
323 struct list_head dev_req_list; /* list of write requests */
324 int dev_sched_port; /* round robin port scheduled */
325 struct gs_port *dev_port[GS_NUM_PORTS]; /* the ports */
326 };
327
328
329 /* Functions */
330
331 /* module */
332 static int __init gs_module_init(void);
333 static void __exit gs_module_exit(void);
334
335 /* tty driver */
336 static int gs_open(struct tty_struct *tty, struct file *file);
337 static void gs_close(struct tty_struct *tty, struct file *file);
338 static int gs_write(struct tty_struct *tty,
339 const unsigned char *buf, int count);
340 static void gs_put_char(struct tty_struct *tty, unsigned char ch);
341 static void gs_flush_chars(struct tty_struct *tty);
342 static int gs_write_room(struct tty_struct *tty);
343 static int gs_chars_in_buffer(struct tty_struct *tty);
344 static void gs_throttle(struct tty_struct * tty);
345 static void gs_unthrottle(struct tty_struct * tty);
346 static void gs_break(struct tty_struct *tty, int break_state);
347 static int gs_ioctl(struct tty_struct *tty, struct file *file,
348 unsigned int cmd, unsigned long arg);
349 static void gs_set_termios(struct tty_struct *tty, struct termios *old);
350
351 static int gs_send(struct gs_dev *dev);
352 static int gs_send_packet(struct gs_dev *dev, char *packet,
353 unsigned int size);
354 static int gs_recv_packet(struct gs_dev *dev, char *packet,
355 unsigned int size);
356 static void gs_read_complete(struct usb_ep *ep, struct usb_request *req);
357 static void gs_write_complete(struct usb_ep *ep, struct usb_request *req);
358
359 /* gadget driver */
360 static int gs_bind(struct usb_gadget *gadget);
361 static void gs_unbind(struct usb_gadget *gadget);
362 static int gs_setup(struct usb_gadget *gadget,
363 const struct usb_ctrlrequest *ctrl);
364 static int gs_setup_standard(struct usb_gadget *gadget,
365 const struct usb_ctrlrequest *ctrl);
366 static int gs_setup_class(struct usb_gadget *gadget,
367 const struct usb_ctrlrequest *ctrl);
368 static void gs_setup_complete(struct usb_ep *ep, struct usb_request *req);
369 static void gs_disconnect(struct usb_gadget *gadget);
370 static int gs_set_config(struct gs_dev *dev, unsigned config);
371 static void gs_reset_config(struct gs_dev *dev);
372 static int gs_build_config_buf(u8 *buf, enum usb_device_speed speed,
373 u8 type, unsigned int index, int is_otg);
374
375 static struct usb_request *gs_alloc_req(struct usb_ep *ep, unsigned int len,
376 int kmalloc_flags);
377 static void gs_free_req(struct usb_ep *ep, struct usb_request *req);
378
379 static struct gs_req_entry *gs_alloc_req_entry(struct usb_ep *ep, unsigned len,
380 int kmalloc_flags);
381 static void gs_free_req_entry(struct usb_ep *ep, struct gs_req_entry *req);
382
383 static int gs_alloc_ports(struct gs_dev *dev, int kmalloc_flags);
384 static void gs_free_ports(struct gs_dev *dev);
385
386 /* circular buffer */
387 static struct gs_buf *gs_buf_alloc(unsigned int size, int kmalloc_flags);
388 static void gs_buf_free(struct gs_buf *gb);
389 static void gs_buf_clear(struct gs_buf *gb);
390 static unsigned int gs_buf_data_avail(struct gs_buf *gb);
391 static unsigned int gs_buf_space_avail(struct gs_buf *gb);
392 static unsigned int gs_buf_put(struct gs_buf *gb, const char *buf,
393 unsigned int count);
394 static unsigned int gs_buf_get(struct gs_buf *gb, char *buf,
395 unsigned int count);
396
397 /* external functions */
398 extern int net2280_set_fifo_mode(struct usb_gadget *gadget, int mode);
399
400
401 /* Globals */
402
403 static struct gs_dev *gs_device;
404
405 static const char *EP_IN_NAME;
406 static const char *EP_OUT_NAME;
407 static const char *EP_NOTIFY_NAME;
408
409 static struct semaphore gs_open_close_sem[GS_NUM_PORTS];
410
411 static unsigned int read_q_size = GS_DEFAULT_READ_Q_SIZE;
412 static unsigned int write_q_size = GS_DEFAULT_WRITE_Q_SIZE;
413
414 static unsigned int write_buf_size = GS_DEFAULT_WRITE_BUF_SIZE;
415
416 static unsigned int use_acm = GS_DEFAULT_USE_ACM;
417
418
419 /* tty driver struct */
420 static struct tty_operations gs_tty_ops = {
421 .open = gs_open,
422 .close = gs_close,
423 .write = gs_write,
424 .put_char = gs_put_char,
425 .flush_chars = gs_flush_chars,
426 .write_room = gs_write_room,
427 .ioctl = gs_ioctl,
428 .set_termios = gs_set_termios,
429 .throttle = gs_throttle,
430 .unthrottle = gs_unthrottle,
431 .break_ctl = gs_break,
432 .chars_in_buffer = gs_chars_in_buffer,
433 };
434 static struct tty_driver *gs_tty_driver;
435
436 /* gadget driver struct */
437 static struct usb_gadget_driver gs_gadget_driver = {
438 #ifdef CONFIG_USB_GADGET_DUALSPEED
439 .speed = USB_SPEED_HIGH,
440 #else
441 .speed = USB_SPEED_FULL,
442 #endif /* CONFIG_USB_GADGET_DUALSPEED */
443 .function = GS_LONG_NAME,
444 .bind = gs_bind,
445 .unbind = gs_unbind,
446 .setup = gs_setup,
447 .disconnect = gs_disconnect,
448 .driver = {
449 .name = GS_SHORT_NAME,
450 /* .shutdown = ... */
451 /* .suspend = ... */
452 /* .resume = ... */
453 },
454 };
455
456
457 /* USB descriptors */
458
459 #define GS_MANUFACTURER_STR_ID 1
460 #define GS_PRODUCT_STR_ID 2
461 #define GS_SERIAL_STR_ID 3
462 #define GS_BULK_CONFIG_STR_ID 4
463 #define GS_ACM_CONFIG_STR_ID 5
464 #define GS_CONTROL_STR_ID 6
465 #define GS_DATA_STR_ID 7
466
467 /* static strings, in UTF-8 */
468 static char manufacturer[50];
469 static struct usb_string gs_strings[] = {
470 { GS_MANUFACTURER_STR_ID, manufacturer },
471 { GS_PRODUCT_STR_ID, GS_LONG_NAME },
472 { GS_SERIAL_STR_ID, "" },
473 { GS_BULK_CONFIG_STR_ID, "Gadget Serial Bulk" },
474 { GS_ACM_CONFIG_STR_ID, "Gadget Serial CDC ACM" },
475 { GS_CONTROL_STR_ID, "Gadget Serial Control" },
476 { GS_DATA_STR_ID, "Gadget Serial Data" },
477 { } /* end of list */
478 };
479
480 static struct usb_gadget_strings gs_string_table = {
481 .language = 0x0409, /* en-us */
482 .strings = gs_strings,
483 };
484
485 static struct usb_device_descriptor gs_device_desc = {
486 .bLength = USB_DT_DEVICE_SIZE,
487 .bDescriptorType = USB_DT_DEVICE,
488 .bcdUSB = __constant_cpu_to_le16(0x0200),
489 .bDeviceSubClass = 0,
490 .bDeviceProtocol = 0,
491 .idVendor = __constant_cpu_to_le16(GS_VENDOR_ID),
492 .idProduct = __constant_cpu_to_le16(GS_PRODUCT_ID),
493 .iManufacturer = GS_MANUFACTURER_STR_ID,
494 .iProduct = GS_PRODUCT_STR_ID,
495 .iSerialNumber = GS_SERIAL_STR_ID,
496 .bNumConfigurations = GS_NUM_CONFIGS,
497 };
498
499 static struct usb_otg_descriptor gs_otg_descriptor = {
500 .bLength = sizeof(gs_otg_descriptor),
501 .bDescriptorType = USB_DT_OTG,
502 .bmAttributes = USB_OTG_SRP,
503 };
504
505 static struct usb_config_descriptor gs_bulk_config_desc = {
506 .bLength = USB_DT_CONFIG_SIZE,
507 .bDescriptorType = USB_DT_CONFIG,
508 /* .wTotalLength computed dynamically */
509 .bNumInterfaces = 1,
510 .bConfigurationValue = GS_BULK_CONFIG_ID,
511 .iConfiguration = GS_BULK_CONFIG_STR_ID,
512 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
513 .bMaxPower = 1,
514 };
515
516 static struct usb_config_descriptor gs_acm_config_desc = {
517 .bLength = USB_DT_CONFIG_SIZE,
518 .bDescriptorType = USB_DT_CONFIG,
519 /* .wTotalLength computed dynamically */
520 .bNumInterfaces = 2,
521 .bConfigurationValue = GS_ACM_CONFIG_ID,
522 .iConfiguration = GS_ACM_CONFIG_STR_ID,
523 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
524 .bMaxPower = 1,
525 };
526
527 static const struct usb_interface_descriptor gs_bulk_interface_desc = {
528 .bLength = USB_DT_INTERFACE_SIZE,
529 .bDescriptorType = USB_DT_INTERFACE,
530 .bInterfaceNumber = GS_BULK_INTERFACE_ID,
531 .bNumEndpoints = 2,
532 .bInterfaceClass = USB_CLASS_CDC_DATA,
533 .bInterfaceSubClass = 0,
534 .bInterfaceProtocol = 0,
535 .iInterface = GS_DATA_STR_ID,
536 };
537
538 static const struct usb_interface_descriptor gs_control_interface_desc = {
539 .bLength = USB_DT_INTERFACE_SIZE,
540 .bDescriptorType = USB_DT_INTERFACE,
541 .bInterfaceNumber = GS_CONTROL_INTERFACE_ID,
542 .bNumEndpoints = 1,
543 .bInterfaceClass = USB_CLASS_COMM,
544 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
545 .bInterfaceProtocol = USB_CDC_CTRL_PROTO_AT,
546 .iInterface = GS_CONTROL_STR_ID,
547 };
548
549 static const struct usb_interface_descriptor gs_data_interface_desc = {
550 .bLength = USB_DT_INTERFACE_SIZE,
551 .bDescriptorType = USB_DT_INTERFACE,
552 .bInterfaceNumber = GS_DATA_INTERFACE_ID,
553 .bNumEndpoints = 2,
554 .bInterfaceClass = USB_CLASS_CDC_DATA,
555 .bInterfaceSubClass = 0,
556 .bInterfaceProtocol = 0,
557 .iInterface = GS_DATA_STR_ID,
558 };
559
560 static const struct usb_cdc_header_desc gs_header_desc = {
561 .bLength = sizeof(gs_header_desc),
562 .bDescriptorType = USB_DT_CS_INTERFACE,
563 .bDescriptorSubType = USB_CDC_SUBTYPE_HEADER,
564 .bcdCDC = __constant_cpu_to_le16(0x0110),
565 };
566
567 static const struct usb_cdc_call_mgmt_desc gs_call_mgmt_descriptor = {
568 .bLength = sizeof(gs_call_mgmt_descriptor),
569 .bDescriptorType = USB_DT_CS_INTERFACE,
570 .bDescriptorSubType = USB_CDC_SUBTYPE_CALL_MGMT,
571 .bmCapabilities = 0,
572 .bDataInterface = 1, /* index of data interface */
573 };
574
575 static struct usb_cdc_acm_desc gs_acm_descriptor = {
576 .bLength = sizeof(gs_acm_descriptor),
577 .bDescriptorType = USB_DT_CS_INTERFACE,
578 .bDescriptorSubType = USB_CDC_SUBTYPE_ACM,
579 .bmCapabilities = 0,
580 };
581
582 static const struct usb_cdc_union_desc gs_union_desc = {
583 .bLength = sizeof(gs_union_desc),
584 .bDescriptorType = USB_DT_CS_INTERFACE,
585 .bDescriptorSubType = USB_CDC_SUBTYPE_UNION,
586 .bMasterInterface0 = 0, /* index of control interface */
587 .bSlaveInterface0 = 1, /* index of data interface */
588 };
589
590 static struct usb_endpoint_descriptor gs_fullspeed_notify_desc = {
591 .bLength = USB_DT_ENDPOINT_SIZE,
592 .bDescriptorType = USB_DT_ENDPOINT,
593 .bEndpointAddress = USB_DIR_IN,
594 .bmAttributes = USB_ENDPOINT_XFER_INT,
595 .wMaxPacketSize = __constant_cpu_to_le16(GS_NOTIFY_MAXPACKET),
596 .bInterval = 1 << GS_LOG2_NOTIFY_INTERVAL,
597 };
598
599 static struct usb_endpoint_descriptor gs_fullspeed_in_desc = {
600 .bLength = USB_DT_ENDPOINT_SIZE,
601 .bDescriptorType = USB_DT_ENDPOINT,
602 .bEndpointAddress = USB_DIR_IN,
603 .bmAttributes = USB_ENDPOINT_XFER_BULK,
604 };
605
606 static struct usb_endpoint_descriptor gs_fullspeed_out_desc = {
607 .bLength = USB_DT_ENDPOINT_SIZE,
608 .bDescriptorType = USB_DT_ENDPOINT,
609 .bEndpointAddress = USB_DIR_OUT,
610 .bmAttributes = USB_ENDPOINT_XFER_BULK,
611 };
612
613 static const struct usb_descriptor_header *gs_bulk_fullspeed_function[] = {
614 (struct usb_descriptor_header *) &gs_otg_descriptor,
615 (struct usb_descriptor_header *) &gs_bulk_interface_desc,
616 (struct usb_descriptor_header *) &gs_fullspeed_in_desc,
617 (struct usb_descriptor_header *) &gs_fullspeed_out_desc,
618 NULL,
619 };
620
621 static const struct usb_descriptor_header *gs_acm_fullspeed_function[] = {
622 (struct usb_descriptor_header *) &gs_otg_descriptor,
623 (struct usb_descriptor_header *) &gs_control_interface_desc,
624 (struct usb_descriptor_header *) &gs_header_desc,
625 (struct usb_descriptor_header *) &gs_call_mgmt_descriptor,
626 (struct usb_descriptor_header *) &gs_acm_descriptor,
627 (struct usb_descriptor_header *) &gs_union_desc,
628 (struct usb_descriptor_header *) &gs_fullspeed_notify_desc,
629 (struct usb_descriptor_header *) &gs_data_interface_desc,
630 (struct usb_descriptor_header *) &gs_fullspeed_in_desc,
631 (struct usb_descriptor_header *) &gs_fullspeed_out_desc,
632 NULL,
633 };
634
635 #ifdef CONFIG_USB_GADGET_DUALSPEED
636 static struct usb_endpoint_descriptor gs_highspeed_notify_desc = {
637 .bLength = USB_DT_ENDPOINT_SIZE,
638 .bDescriptorType = USB_DT_ENDPOINT,
639 .bEndpointAddress = USB_DIR_IN,
640 .bmAttributes = USB_ENDPOINT_XFER_INT,
641 .wMaxPacketSize = __constant_cpu_to_le16(GS_NOTIFY_MAXPACKET),
642 .bInterval = GS_LOG2_NOTIFY_INTERVAL+4,
643 };
644
645 static struct usb_endpoint_descriptor gs_highspeed_in_desc = {
646 .bLength = USB_DT_ENDPOINT_SIZE,
647 .bDescriptorType = USB_DT_ENDPOINT,
648 .bmAttributes = USB_ENDPOINT_XFER_BULK,
649 .wMaxPacketSize = __constant_cpu_to_le16(512),
650 };
651
652 static struct usb_endpoint_descriptor gs_highspeed_out_desc = {
653 .bLength = USB_DT_ENDPOINT_SIZE,
654 .bDescriptorType = USB_DT_ENDPOINT,
655 .bmAttributes = USB_ENDPOINT_XFER_BULK,
656 .wMaxPacketSize = __constant_cpu_to_le16(512),
657 };
658
659 static struct usb_qualifier_descriptor gs_qualifier_desc = {
660 .bLength = sizeof(struct usb_qualifier_descriptor),
661 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
662 .bcdUSB = __constant_cpu_to_le16 (0x0200),
663 /* assumes ep0 uses the same value for both speeds ... */
664 .bNumConfigurations = GS_NUM_CONFIGS,
665 };
666
667 static const struct usb_descriptor_header *gs_bulk_highspeed_function[] = {
668 (struct usb_descriptor_header *) &gs_otg_descriptor,
669 (struct usb_descriptor_header *) &gs_bulk_interface_desc,
670 (struct usb_descriptor_header *) &gs_highspeed_in_desc,
671 (struct usb_descriptor_header *) &gs_highspeed_out_desc,
672 NULL,
673 };
674
675 static const struct usb_descriptor_header *gs_acm_highspeed_function[] = {
676 (struct usb_descriptor_header *) &gs_otg_descriptor,
677 (struct usb_descriptor_header *) &gs_control_interface_desc,
678 (struct usb_descriptor_header *) &gs_header_desc,
679 (struct usb_descriptor_header *) &gs_call_mgmt_descriptor,
680 (struct usb_descriptor_header *) &gs_acm_descriptor,
681 (struct usb_descriptor_header *) &gs_union_desc,
682 (struct usb_descriptor_header *) &gs_highspeed_notify_desc,
683 (struct usb_descriptor_header *) &gs_data_interface_desc,
684 (struct usb_descriptor_header *) &gs_highspeed_in_desc,
685 (struct usb_descriptor_header *) &gs_highspeed_out_desc,
686 NULL,
687 };
688
689 #endif /* CONFIG_USB_GADGET_DUALSPEED */
690
691
692 /* Module */
693 MODULE_DESCRIPTION(GS_LONG_NAME);
694 MODULE_AUTHOR("Al Borchers");
695 MODULE_LICENSE("GPL");
696
697 #ifdef GS_DEBUG
698 module_param(debug, int, S_IRUGO|S_IWUSR);
699 MODULE_PARM_DESC(debug, "Enable debugging, 0=off, 1=on");
700 #endif
701
702 module_param(read_q_size, uint, S_IRUGO);
703 MODULE_PARM_DESC(read_q_size, "Read request queue size, default=32");
704
705 module_param(write_q_size, uint, S_IRUGO);
706 MODULE_PARM_DESC(write_q_size, "Write request queue size, default=32");
707
708 module_param(write_buf_size, uint, S_IRUGO);
709 MODULE_PARM_DESC(write_buf_size, "Write buffer size, default=8192");
710
711 module_param(use_acm, uint, S_IRUGO);
712 MODULE_PARM_DESC(use_acm, "Use CDC ACM, 0=no, 1=yes, default=no");
713
714 module_init(gs_module_init);
715 module_exit(gs_module_exit);
716
717 /*
718 * gs_module_init
719 *
720 * Register as a USB gadget driver and a tty driver.
721 */
722 static int __init gs_module_init(void)
723 {
724 int i;
725 int retval;
726
727 retval = usb_gadget_register_driver(&gs_gadget_driver);
728 if (retval) {
729 printk(KERN_ERR "gs_module_init: cannot register gadget driver, ret=%d\n", retval);
730 return retval;
731 }
732
733 gs_tty_driver = alloc_tty_driver(GS_NUM_PORTS);
734 if (!gs_tty_driver)
735 return -ENOMEM;
736 gs_tty_driver->owner = THIS_MODULE;
737 gs_tty_driver->driver_name = GS_SHORT_NAME;
738 gs_tty_driver->name = "ttygs";
739 gs_tty_driver->devfs_name = "usb/ttygs/";
740 gs_tty_driver->major = GS_MAJOR;
741 gs_tty_driver->minor_start = GS_MINOR_START;
742 gs_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
743 gs_tty_driver->subtype = SERIAL_TYPE_NORMAL;
744 gs_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS;
745 gs_tty_driver->init_termios = tty_std_termios;
746 gs_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
747 tty_set_operations(gs_tty_driver, &gs_tty_ops);
748
749 for (i=0; i < GS_NUM_PORTS; i++)
750 sema_init(&gs_open_close_sem[i], 1);
751
752 retval = tty_register_driver(gs_tty_driver);
753 if (retval) {
754 usb_gadget_unregister_driver(&gs_gadget_driver);
755 put_tty_driver(gs_tty_driver);
756 printk(KERN_ERR "gs_module_init: cannot register tty driver, ret=%d\n", retval);
757 return retval;
758 }
759
760 printk(KERN_INFO "gs_module_init: %s %s loaded\n", GS_LONG_NAME, GS_VERSION_STR);
761 return 0;
762 }
763
764 /*
765 * gs_module_exit
766 *
767 * Unregister as a tty driver and a USB gadget driver.
768 */
769 static void __exit gs_module_exit(void)
770 {
771 tty_unregister_driver(gs_tty_driver);
772 put_tty_driver(gs_tty_driver);
773 usb_gadget_unregister_driver(&gs_gadget_driver);
774
775 printk(KERN_INFO "gs_module_exit: %s %s unloaded\n", GS_LONG_NAME, GS_VERSION_STR);
776 }
777
778 /* TTY Driver */
779
780 /*
781 * gs_open
782 */
783 static int gs_open(struct tty_struct *tty, struct file *file)
784 {
785 int port_num;
786 unsigned long flags;
787 struct gs_port *port;
788 struct gs_dev *dev;
789 struct gs_buf *buf;
790 struct semaphore *sem;
791 int ret;
792
793 port_num = tty->index;
794
795 gs_debug("gs_open: (%d,%p,%p)\n", port_num, tty, file);
796
797 if (port_num < 0 || port_num >= GS_NUM_PORTS) {
798 printk(KERN_ERR "gs_open: (%d,%p,%p) invalid port number\n",
799 port_num, tty, file);
800 return -ENODEV;
801 }
802
803 dev = gs_device;
804
805 if (dev == NULL) {
806 printk(KERN_ERR "gs_open: (%d,%p,%p) NULL device pointer\n",
807 port_num, tty, file);
808 return -ENODEV;
809 }
810
811 sem = &gs_open_close_sem[port_num];
812 if (down_interruptible(sem)) {
813 printk(KERN_ERR
814 "gs_open: (%d,%p,%p) interrupted waiting for semaphore\n",
815 port_num, tty, file);
816 return -ERESTARTSYS;
817 }
818
819 spin_lock_irqsave(&dev->dev_lock, flags);
820
821 if (dev->dev_config == GS_NO_CONFIG_ID) {
822 printk(KERN_ERR
823 "gs_open: (%d,%p,%p) device is not connected\n",
824 port_num, tty, file);
825 ret = -ENODEV;
826 goto exit_unlock_dev;
827 }
828
829 port = dev->dev_port[port_num];
830
831 if (port == NULL) {
832 printk(KERN_ERR "gs_open: (%d,%p,%p) NULL port pointer\n",
833 port_num, tty, file);
834 ret = -ENODEV;
835 goto exit_unlock_dev;
836 }
837
838 spin_lock(&port->port_lock);
839 spin_unlock(&dev->dev_lock);
840
841 if (port->port_dev == NULL) {
842 printk(KERN_ERR "gs_open: (%d,%p,%p) port disconnected (1)\n",
843 port_num, tty, file);
844 ret = -EIO;
845 goto exit_unlock_port;
846 }
847
848 if (port->port_open_count > 0) {
849 ++port->port_open_count;
850 gs_debug("gs_open: (%d,%p,%p) already open\n",
851 port_num, tty, file);
852 ret = 0;
853 goto exit_unlock_port;
854 }
855
856 tty->driver_data = NULL;
857
858 /* mark port as in use, we can drop port lock and sleep if necessary */
859 port->port_in_use = 1;
860
861 /* allocate write buffer on first open */
862 if (port->port_write_buf == NULL) {
863 spin_unlock_irqrestore(&port->port_lock, flags);
864 buf = gs_buf_alloc(write_buf_size, GFP_KERNEL);
865 spin_lock_irqsave(&port->port_lock, flags);
866
867 /* might have been disconnected while asleep, check */
868 if (port->port_dev == NULL) {
869 printk(KERN_ERR
870 "gs_open: (%d,%p,%p) port disconnected (2)\n",
871 port_num, tty, file);
872 port->port_in_use = 0;
873 ret = -EIO;
874 goto exit_unlock_port;
875 }
876
877 if ((port->port_write_buf=buf) == NULL) {
878 printk(KERN_ERR "gs_open: (%d,%p,%p) cannot allocate port write buffer\n",
879 port_num, tty, file);
880 port->port_in_use = 0;
881 ret = -ENOMEM;
882 goto exit_unlock_port;
883 }
884
885 }
886
887 /* wait for carrier detect (not implemented) */
888
889 /* might have been disconnected while asleep, check */
890 if (port->port_dev == NULL) {
891 printk(KERN_ERR "gs_open: (%d,%p,%p) port disconnected (3)\n",
892 port_num, tty, file);
893 port->port_in_use = 0;
894 ret = -EIO;
895 goto exit_unlock_port;
896 }
897
898 tty->driver_data = port;
899 port->port_tty = tty;
900 port->port_open_count = 1;
901 port->port_in_use = 0;
902
903 gs_debug("gs_open: (%d,%p,%p) completed\n", port_num, tty, file);
904
905 ret = 0;
906
907 exit_unlock_port:
908 spin_unlock_irqrestore(&port->port_lock, flags);
909 up(sem);
910 return ret;
911
912 exit_unlock_dev:
913 spin_unlock_irqrestore(&dev->dev_lock, flags);
914 up(sem);
915 return ret;
916
917 }
918
919 /*
920 * gs_close
921 */
922 static void gs_close(struct tty_struct *tty, struct file *file)
923 {
924 unsigned long flags;
925 struct gs_port *port = tty->driver_data;
926 struct semaphore *sem;
927
928 if (port == NULL) {
929 printk(KERN_ERR "gs_close: NULL port pointer\n");
930 return;
931 }
932
933 gs_debug("gs_close: (%d,%p,%p)\n", port->port_num, tty, file);
934
935 sem = &gs_open_close_sem[port->port_num];
936 down(sem);
937
938 spin_lock_irqsave(&port->port_lock, flags);
939
940 if (port->port_open_count == 0) {
941 printk(KERN_ERR
942 "gs_close: (%d,%p,%p) port is already closed\n",
943 port->port_num, tty, file);
944 goto exit;
945 }
946
947 if (port->port_open_count > 1) {
948 --port->port_open_count;
949 goto exit;
950 }
951
952 /* free disconnected port on final close */
953 if (port->port_dev == NULL) {
954 kfree(port);
955 goto exit;
956 }
957
958 /* mark port as closed but in use, we can drop port lock */
959 /* and sleep if necessary */
960 port->port_in_use = 1;
961 port->port_open_count = 0;
962
963 /* wait for write buffer to drain, or */
964 /* at most GS_CLOSE_TIMEOUT seconds */
965 if (gs_buf_data_avail(port->port_write_buf) > 0) {
966 wait_cond_interruptible_timeout(port->port_write_wait,
967 port->port_dev == NULL
968 || gs_buf_data_avail(port->port_write_buf) == 0,
969 &port->port_lock, flags, GS_CLOSE_TIMEOUT * HZ);
970 }
971
972 /* free disconnected port on final close */
973 /* (might have happened during the above sleep) */
974 if (port->port_dev == NULL) {
975 kfree(port);
976 goto exit;
977 }
978
979 gs_buf_clear(port->port_write_buf);
980
981 tty->driver_data = NULL;
982 port->port_tty = NULL;
983 port->port_in_use = 0;
984
985 gs_debug("gs_close: (%d,%p,%p) completed\n",
986 port->port_num, tty, file);
987
988 exit:
989 spin_unlock_irqrestore(&port->port_lock, flags);
990 up(sem);
991 }
992
993 /*
994 * gs_write
995 */
996 static int gs_write(struct tty_struct *tty, const unsigned char *buf, int count)
997 {
998 unsigned long flags;
999 struct gs_port *port = tty->driver_data;
1000 int ret;
1001
1002 if (port == NULL) {
1003 printk(KERN_ERR "gs_write: NULL port pointer\n");
1004 return -EIO;
1005 }
1006
1007 gs_debug("gs_write: (%d,%p) writing %d bytes\n", port->port_num, tty,
1008 count);
1009
1010 if (count == 0)
1011 return 0;
1012
1013 spin_lock_irqsave(&port->port_lock, flags);
1014
1015 if (port->port_dev == NULL) {
1016 printk(KERN_ERR "gs_write: (%d,%p) port is not connected\n",
1017 port->port_num, tty);
1018 ret = -EIO;
1019 goto exit;
1020 }
1021
1022 if (port->port_open_count == 0) {
1023 printk(KERN_ERR "gs_write: (%d,%p) port is closed\n",
1024 port->port_num, tty);
1025 ret = -EBADF;
1026 goto exit;
1027 }
1028
1029 count = gs_buf_put(port->port_write_buf, buf, count);
1030
1031 spin_unlock_irqrestore(&port->port_lock, flags);
1032
1033 gs_send(gs_device);
1034
1035 gs_debug("gs_write: (%d,%p) wrote %d bytes\n", port->port_num, tty,
1036 count);
1037
1038 return count;
1039
1040 exit:
1041 spin_unlock_irqrestore(&port->port_lock, flags);
1042 return ret;
1043 }
1044
1045 /*
1046 * gs_put_char
1047 */
1048 static void gs_put_char(struct tty_struct *tty, unsigned char ch)
1049 {
1050 unsigned long flags;
1051 struct gs_port *port = tty->driver_data;
1052
1053 if (port == NULL) {
1054 printk(KERN_ERR "gs_put_char: NULL port pointer\n");
1055 return;
1056 }
1057
1058 gs_debug("gs_put_char: (%d,%p) char=0x%x, called from %p, %p, %p\n", port->port_num, tty, ch, __builtin_return_address(0), __builtin_return_address(1), __builtin_return_address(2));
1059
1060 spin_lock_irqsave(&port->port_lock, flags);
1061
1062 if (port->port_dev == NULL) {
1063 printk(KERN_ERR "gs_put_char: (%d,%p) port is not connected\n",
1064 port->port_num, tty);
1065 goto exit;
1066 }
1067
1068 if (port->port_open_count == 0) {
1069 printk(KERN_ERR "gs_put_char: (%d,%p) port is closed\n",
1070 port->port_num, tty);
1071 goto exit;
1072 }
1073
1074 gs_buf_put(port->port_write_buf, &ch, 1);
1075
1076 exit:
1077 spin_unlock_irqrestore(&port->port_lock, flags);
1078 }
1079
1080 /*
1081 * gs_flush_chars
1082 */
1083 static void gs_flush_chars(struct tty_struct *tty)
1084 {
1085 unsigned long flags;
1086 struct gs_port *port = tty->driver_data;
1087
1088 if (port == NULL) {
1089 printk(KERN_ERR "gs_flush_chars: NULL port pointer\n");
1090 return;
1091 }
1092
1093 gs_debug("gs_flush_chars: (%d,%p)\n", port->port_num, tty);
1094
1095 spin_lock_irqsave(&port->port_lock, flags);
1096
1097 if (port->port_dev == NULL) {
1098 printk(KERN_ERR
1099 "gs_flush_chars: (%d,%p) port is not connected\n",
1100 port->port_num, tty);
1101 goto exit;
1102 }
1103
1104 if (port->port_open_count == 0) {
1105 printk(KERN_ERR "gs_flush_chars: (%d,%p) port is closed\n",
1106 port->port_num, tty);
1107 goto exit;
1108 }
1109
1110 spin_unlock_irqrestore(&port->port_lock, flags);
1111
1112 gs_send(gs_device);
1113
1114 return;
1115
1116 exit:
1117 spin_unlock_irqrestore(&port->port_lock, flags);
1118 }
1119
1120 /*
1121 * gs_write_room
1122 */
1123 static int gs_write_room(struct tty_struct *tty)
1124 {
1125
1126 int room = 0;
1127 unsigned long flags;
1128 struct gs_port *port = tty->driver_data;
1129
1130
1131 if (port == NULL)
1132 return 0;
1133
1134 spin_lock_irqsave(&port->port_lock, flags);
1135
1136 if (port->port_dev != NULL && port->port_open_count > 0
1137 && port->port_write_buf != NULL)
1138 room = gs_buf_space_avail(port->port_write_buf);
1139
1140 spin_unlock_irqrestore(&port->port_lock, flags);
1141
1142 gs_debug("gs_write_room: (%d,%p) room=%d\n",
1143 port->port_num, tty, room);
1144
1145 return room;
1146 }
1147
1148 /*
1149 * gs_chars_in_buffer
1150 */
1151 static int gs_chars_in_buffer(struct tty_struct *tty)
1152 {
1153 int chars = 0;
1154 unsigned long flags;
1155 struct gs_port *port = tty->driver_data;
1156
1157 if (port == NULL)
1158 return 0;
1159
1160 spin_lock_irqsave(&port->port_lock, flags);
1161
1162 if (port->port_dev != NULL && port->port_open_count > 0
1163 && port->port_write_buf != NULL)
1164 chars = gs_buf_data_avail(port->port_write_buf);
1165
1166 spin_unlock_irqrestore(&port->port_lock, flags);
1167
1168 gs_debug("gs_chars_in_buffer: (%d,%p) chars=%d\n",
1169 port->port_num, tty, chars);
1170
1171 return chars;
1172 }
1173
1174 /*
1175 * gs_throttle
1176 */
1177 static void gs_throttle(struct tty_struct *tty)
1178 {
1179 }
1180
1181 /*
1182 * gs_unthrottle
1183 */
1184 static void gs_unthrottle(struct tty_struct *tty)
1185 {
1186 }
1187
1188 /*
1189 * gs_break
1190 */
1191 static void gs_break(struct tty_struct *tty, int break_state)
1192 {
1193 }
1194
1195 /*
1196 * gs_ioctl
1197 */
1198 static int gs_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
1199 {
1200 struct gs_port *port = tty->driver_data;
1201
1202 if (port == NULL) {
1203 printk(KERN_ERR "gs_ioctl: NULL port pointer\n");
1204 return -EIO;
1205 }
1206
1207 gs_debug("gs_ioctl: (%d,%p,%p) cmd=0x%4.4x, arg=%lu\n",
1208 port->port_num, tty, file, cmd, arg);
1209
1210 /* handle ioctls */
1211
1212 /* could not handle ioctl */
1213 return -ENOIOCTLCMD;
1214 }
1215
1216 /*
1217 * gs_set_termios
1218 */
1219 static void gs_set_termios(struct tty_struct *tty, struct termios *old)
1220 {
1221 }
1222
1223 /*
1224 * gs_send
1225 *
1226 * This function finds available write requests, calls
1227 * gs_send_packet to fill these packets with data, and
1228 * continues until either there are no more write requests
1229 * available or no more data to send. This function is
1230 * run whenever data arrives or write requests are available.
1231 */
1232 static int gs_send(struct gs_dev *dev)
1233 {
1234 int ret,len;
1235 unsigned long flags;
1236 struct usb_ep *ep;
1237 struct usb_request *req;
1238 struct gs_req_entry *req_entry;
1239
1240 if (dev == NULL) {
1241 printk(KERN_ERR "gs_send: NULL device pointer\n");
1242 return -ENODEV;
1243 }
1244
1245 spin_lock_irqsave(&dev->dev_lock, flags);
1246
1247 ep = dev->dev_in_ep;
1248
1249 while(!list_empty(&dev->dev_req_list)) {
1250
1251 req_entry = list_entry(dev->dev_req_list.next,
1252 struct gs_req_entry, re_entry);
1253
1254 req = req_entry->re_req;
1255
1256 len = gs_send_packet(dev, req->buf, ep->maxpacket);
1257
1258 if (len > 0) {
1259 gs_debug_level(3, "gs_send: len=%d, 0x%2.2x 0x%2.2x 0x%2.2x ...\n", len, *((unsigned char *)req->buf), *((unsigned char *)req->buf+1), *((unsigned char *)req->buf+2));
1260 list_del(&req_entry->re_entry);
1261 req->length = len;
1262 if ((ret=usb_ep_queue(ep, req, GFP_ATOMIC))) {
1263 printk(KERN_ERR
1264 "gs_send: cannot queue read request, ret=%d\n",
1265 ret);
1266 break;
1267 }
1268 } else {
1269 break;
1270 }
1271
1272 }
1273
1274 spin_unlock_irqrestore(&dev->dev_lock, flags);
1275
1276 return 0;
1277 }
1278
1279 /*
1280 * gs_send_packet
1281 *
1282 * If there is data to send, a packet is built in the given
1283 * buffer and the size is returned. If there is no data to
1284 * send, 0 is returned. If there is any error a negative
1285 * error number is returned.
1286 *
1287 * Called during USB completion routine, on interrupt time.
1288 *
1289 * We assume that disconnect will not happen until all completion
1290 * routines have completed, so we can assume that the dev_port
1291 * array does not change during the lifetime of this function.
1292 */
1293 static int gs_send_packet(struct gs_dev *dev, char *packet, unsigned int size)
1294 {
1295 unsigned int len;
1296 struct gs_port *port;
1297
1298 /* TEMPORARY -- only port 0 is supported right now */
1299 port = dev->dev_port[0];
1300
1301 if (port == NULL) {
1302 printk(KERN_ERR
1303 "gs_send_packet: port=%d, NULL port pointer\n",
1304 0);
1305 return -EIO;
1306 }
1307
1308 spin_lock(&port->port_lock);
1309
1310 len = gs_buf_data_avail(port->port_write_buf);
1311 if (len < size)
1312 size = len;
1313
1314 if (size == 0)
1315 goto exit;
1316
1317 size = gs_buf_get(port->port_write_buf, packet, size);
1318
1319 if (port->port_tty)
1320 wake_up_interruptible(&port->port_tty->write_wait);
1321
1322 exit:
1323 spin_unlock(&port->port_lock);
1324 return size;
1325 }
1326
1327 /*
1328 * gs_recv_packet
1329 *
1330 * Called for each USB packet received. Reads the packet
1331 * header and stuffs the data in the appropriate tty buffer.
1332 * Returns 0 if successful, or a negative error number.
1333 *
1334 * Called during USB completion routine, on interrupt time.
1335 *
1336 * We assume that disconnect will not happen until all completion
1337 * routines have completed, so we can assume that the dev_port
1338 * array does not change during the lifetime of this function.
1339 */
1340 static int gs_recv_packet(struct gs_dev *dev, char *packet, unsigned int size)
1341 {
1342 unsigned int len;
1343 struct gs_port *port;
1344 int ret;
1345
1346 /* TEMPORARY -- only port 0 is supported right now */
1347 port = dev->dev_port[0];
1348
1349 if (port == NULL) {
1350 printk(KERN_ERR "gs_recv_packet: port=%d, NULL port pointer\n",
1351 port->port_num);
1352 return -EIO;
1353 }
1354
1355 spin_lock(&port->port_lock);
1356
1357 if (port->port_open_count == 0) {
1358 printk(KERN_ERR "gs_recv_packet: port=%d, port is closed\n",
1359 port->port_num);
1360 ret = -EIO;
1361 goto exit;
1362 }
1363
1364 if (port->port_tty == NULL) {
1365 printk(KERN_ERR "gs_recv_packet: port=%d, NULL tty pointer\n",
1366 port->port_num);
1367 ret = -EIO;
1368 goto exit;
1369 }
1370
1371 if (port->port_tty->magic != TTY_MAGIC) {
1372 printk(KERN_ERR "gs_recv_packet: port=%d, bad tty magic\n",
1373 port->port_num);
1374 ret = -EIO;
1375 goto exit;
1376 }
1377
1378 len = (unsigned int)(TTY_FLIPBUF_SIZE - port->port_tty->flip.count);
1379 if (len < size)
1380 size = len;
1381
1382 if (size > 0) {
1383 memcpy(port->port_tty->flip.char_buf_ptr, packet, size);
1384 port->port_tty->flip.char_buf_ptr += size;
1385 port->port_tty->flip.count += size;
1386 tty_flip_buffer_push(port->port_tty);
1387 wake_up_interruptible(&port->port_tty->read_wait);
1388 }
1389
1390 ret = 0;
1391
1392 exit:
1393 spin_unlock(&port->port_lock);
1394 return ret;
1395 }
1396
1397 /*
1398 * gs_read_complete
1399 */
1400 static void gs_read_complete(struct usb_ep *ep, struct usb_request *req)
1401 {
1402 int ret;
1403 struct gs_dev *dev = ep->driver_data;
1404
1405 if (dev == NULL) {
1406 printk(KERN_ERR "gs_read_complete: NULL device pointer\n");
1407 return;
1408 }
1409
1410 switch(req->status) {
1411 case 0:
1412 /* normal completion */
1413 gs_recv_packet(dev, req->buf, req->actual);
1414 requeue:
1415 req->length = ep->maxpacket;
1416 if ((ret=usb_ep_queue(ep, req, GFP_ATOMIC))) {
1417 printk(KERN_ERR
1418 "gs_read_complete: cannot queue read request, ret=%d\n",
1419 ret);
1420 }
1421 break;
1422
1423 case -ESHUTDOWN:
1424 /* disconnect */
1425 gs_debug("gs_read_complete: shutdown\n");
1426 gs_free_req(ep, req);
1427 break;
1428
1429 default:
1430 /* unexpected */
1431 printk(KERN_ERR
1432 "gs_read_complete: unexpected status error, status=%d\n",
1433 req->status);
1434 goto requeue;
1435 break;
1436 }
1437 }
1438
1439 /*
1440 * gs_write_complete
1441 */
1442 static void gs_write_complete(struct usb_ep *ep, struct usb_request *req)
1443 {
1444 struct gs_dev *dev = ep->driver_data;
1445 struct gs_req_entry *gs_req = req->context;
1446
1447 if (dev == NULL) {
1448 printk(KERN_ERR "gs_write_complete: NULL device pointer\n");
1449 return;
1450 }
1451
1452 switch(req->status) {
1453 case 0:
1454 /* normal completion */
1455 requeue:
1456 if (gs_req == NULL) {
1457 printk(KERN_ERR
1458 "gs_write_complete: NULL request pointer\n");
1459 return;
1460 }
1461
1462 spin_lock(&dev->dev_lock);
1463 list_add(&gs_req->re_entry, &dev->dev_req_list);
1464 spin_unlock(&dev->dev_lock);
1465
1466 gs_send(dev);
1467
1468 break;
1469
1470 case -ESHUTDOWN:
1471 /* disconnect */
1472 gs_debug("gs_write_complete: shutdown\n");
1473 gs_free_req(ep, req);
1474 break;
1475
1476 default:
1477 printk(KERN_ERR
1478 "gs_write_complete: unexpected status error, status=%d\n",
1479 req->status);
1480 goto requeue;
1481 break;
1482 }
1483 }
1484
1485 /* Gadget Driver */
1486
1487 /*
1488 * gs_bind
1489 *
1490 * Called on module load. Allocates and initializes the device
1491 * structure and a control request.
1492 */
1493 static int gs_bind(struct usb_gadget *gadget)
1494 {
1495 int ret;
1496 struct usb_ep *ep;
1497 struct gs_dev *dev;
1498
1499 /* device specific */
1500 if (gadget_is_net2280(gadget)) {
1501 gs_device_desc.bcdDevice =
1502 __constant_cpu_to_le16(GS_VERSION_NUM|0x0001);
1503 } else if (gadget_is_pxa(gadget)) {
1504 gs_device_desc.bcdDevice =
1505 __constant_cpu_to_le16(GS_VERSION_NUM|0x0002);
1506 } else if (gadget_is_sh(gadget)) {
1507 gs_device_desc.bcdDevice =
1508 __constant_cpu_to_le16(GS_VERSION_NUM|0x0003);
1509 /* sh doesn't support multiple interfaces or configs */
1510 use_acm = 0;
1511 } else if (gadget_is_sa1100(gadget)) {
1512 gs_device_desc.bcdDevice =
1513 __constant_cpu_to_le16(GS_VERSION_NUM|0x0004);
1514 /* sa1100 doesn't support necessary endpoints */
1515 use_acm = 0;
1516 } else if (gadget_is_goku(gadget)) {
1517 gs_device_desc.bcdDevice =
1518 __constant_cpu_to_le16(GS_VERSION_NUM|0x0005);
1519 } else if (gadget_is_mq11xx(gadget)) {
1520 gs_device_desc.bcdDevice =
1521 __constant_cpu_to_le16(GS_VERSION_NUM|0x0006);
1522 } else if (gadget_is_omap(gadget)) {
1523 gs_device_desc.bcdDevice =
1524 __constant_cpu_to_le16(GS_VERSION_NUM|0x0007);
1525 } else if (gadget_is_lh7a40x(gadget)) {
1526 gs_device_desc.bcdDevice =
1527 __constant_cpu_to_le16(GS_VERSION_NUM|0x0008);
1528 } else if (gadget_is_n9604(gadget)) {
1529 gs_device_desc.bcdDevice =
1530 __constant_cpu_to_le16(GS_VERSION_NUM|0x0009);
1531 } else if (gadget_is_pxa27x(gadget)) {
1532 gs_device_desc.bcdDevice =
1533 __constant_cpu_to_le16(GS_VERSION_NUM|0x0011);
1534 } else {
1535 printk(KERN_WARNING "gs_bind: controller '%s' not recognized\n",
1536 gadget->name);
1537 /* unrecognized, but safe unless bulk is REALLY quirky */
1538 gs_device_desc.bcdDevice =
1539 __constant_cpu_to_le16(GS_VERSION_NUM|0x0099);
1540 }
1541
1542 usb_ep_autoconfig_reset(gadget);
1543
1544 ep = usb_ep_autoconfig(gadget, &gs_fullspeed_in_desc);
1545 if (!ep)
1546 goto autoconf_fail;
1547 EP_IN_NAME = ep->name;
1548 ep->driver_data = ep; /* claim the endpoint */
1549
1550 ep = usb_ep_autoconfig(gadget, &gs_fullspeed_out_desc);
1551 if (!ep)
1552 goto autoconf_fail;
1553 EP_OUT_NAME = ep->name;
1554 ep->driver_data = ep; /* claim the endpoint */
1555
1556 if (use_acm) {
1557 ep = usb_ep_autoconfig(gadget, &gs_fullspeed_notify_desc);
1558 if (!ep) {
1559 printk(KERN_ERR "gs_bind: cannot run ACM on %s\n", gadget->name);
1560 goto autoconf_fail;
1561 }
1562 gs_device_desc.idProduct = __constant_cpu_to_le16(
1563 GS_CDC_PRODUCT_ID),
1564 EP_NOTIFY_NAME = ep->name;
1565 ep->driver_data = ep; /* claim the endpoint */
1566 }
1567
1568 gs_device_desc.bDeviceClass = use_acm
1569 ? USB_CLASS_COMM : USB_CLASS_VENDOR_SPEC;
1570 gs_device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
1571
1572 #ifdef CONFIG_USB_GADGET_DUALSPEED
1573 gs_qualifier_desc.bDeviceClass = use_acm
1574 ? USB_CLASS_COMM : USB_CLASS_VENDOR_SPEC;
1575 /* assume ep0 uses the same packet size for both speeds */
1576 gs_qualifier_desc.bMaxPacketSize0 = gs_device_desc.bMaxPacketSize0;
1577 /* assume endpoints are dual-speed */
1578 gs_highspeed_notify_desc.bEndpointAddress =
1579 gs_fullspeed_notify_desc.bEndpointAddress;
1580 gs_highspeed_in_desc.bEndpointAddress =
1581 gs_fullspeed_in_desc.bEndpointAddress;
1582 gs_highspeed_out_desc.bEndpointAddress =
1583 gs_fullspeed_out_desc.bEndpointAddress;
1584 #endif /* CONFIG_USB_GADGET_DUALSPEED */
1585
1586 usb_gadget_set_selfpowered(gadget);
1587
1588 if (gadget->is_otg) {
1589 gs_otg_descriptor.bmAttributes |= USB_OTG_HNP,
1590 gs_bulk_config_desc.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1591 gs_acm_config_desc.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1592 }
1593
1594 gs_device = dev = kmalloc(sizeof(struct gs_dev), GFP_KERNEL);
1595 if (dev == NULL)
1596 return -ENOMEM;
1597
1598 snprintf(manufacturer, sizeof(manufacturer), "%s %s with %s",
1599 system_utsname.sysname, system_utsname.release,
1600 gadget->name);
1601
1602 memset(dev, 0, sizeof(struct gs_dev));
1603 dev->dev_gadget = gadget;
1604 spin_lock_init(&dev->dev_lock);
1605 INIT_LIST_HEAD(&dev->dev_req_list);
1606 set_gadget_data(gadget, dev);
1607
1608 if ((ret=gs_alloc_ports(dev, GFP_KERNEL)) != 0) {
1609 printk(KERN_ERR "gs_bind: cannot allocate ports\n");
1610 gs_unbind(gadget);
1611 return ret;
1612 }
1613
1614 /* preallocate control response and buffer */
1615 dev->dev_ctrl_req = gs_alloc_req(gadget->ep0, GS_MAX_DESC_LEN,
1616 GFP_KERNEL);
1617 if (dev->dev_ctrl_req == NULL) {
1618 gs_unbind(gadget);
1619 return -ENOMEM;
1620 }
1621 dev->dev_ctrl_req->complete = gs_setup_complete;
1622
1623 gadget->ep0->driver_data = dev;
1624
1625 printk(KERN_INFO "gs_bind: %s %s bound\n",
1626 GS_LONG_NAME, GS_VERSION_STR);
1627
1628 return 0;
1629
1630 autoconf_fail:
1631 printk(KERN_ERR "gs_bind: cannot autoconfigure on %s\n", gadget->name);
1632 return -ENODEV;
1633 }
1634
1635 /*
1636 * gs_unbind
1637 *
1638 * Called on module unload. Frees the control request and device
1639 * structure.
1640 */
1641 static void gs_unbind(struct usb_gadget *gadget)
1642 {
1643 struct gs_dev *dev = get_gadget_data(gadget);
1644
1645 gs_device = NULL;
1646
1647 /* read/write requests already freed, only control request remains */
1648 if (dev != NULL) {
1649 if (dev->dev_ctrl_req != NULL) {
1650 gs_free_req(gadget->ep0, dev->dev_ctrl_req);
1651 dev->dev_ctrl_req = NULL;
1652 }
1653 gs_free_ports(dev);
1654 kfree(dev);
1655 set_gadget_data(gadget, NULL);
1656 }
1657
1658 printk(KERN_INFO "gs_unbind: %s %s unbound\n", GS_LONG_NAME,
1659 GS_VERSION_STR);
1660 }
1661
1662 /*
1663 * gs_setup
1664 *
1665 * Implements all the control endpoint functionality that's not
1666 * handled in hardware or the hardware driver.
1667 *
1668 * Returns the size of the data sent to the host, or a negative
1669 * error number.
1670 */
1671 static int gs_setup(struct usb_gadget *gadget,
1672 const struct usb_ctrlrequest *ctrl)
1673 {
1674 int ret = -EOPNOTSUPP;
1675 struct gs_dev *dev = get_gadget_data(gadget);
1676 struct usb_request *req = dev->dev_ctrl_req;
1677
1678 switch (ctrl->bRequestType & USB_TYPE_MASK) {
1679 case USB_TYPE_STANDARD:
1680 ret = gs_setup_standard(gadget,ctrl);
1681 break;
1682
1683 case USB_TYPE_CLASS:
1684 ret = gs_setup_class(gadget,ctrl);
1685 break;
1686
1687 default:
1688 printk(KERN_ERR "gs_setup: unknown request, type=%02x, request=%02x, value=%04x, index=%04x, length=%d\n",
1689 ctrl->bRequestType, ctrl->bRequest, ctrl->wValue,
1690 ctrl->wIndex, ctrl->wLength);
1691 break;
1692 }
1693
1694 /* respond with data transfer before status phase? */
1695 if (ret >= 0) {
1696 req->length = ret;
1697 req->zero = ret < ctrl->wLength
1698 && (ret % gadget->ep0->maxpacket) == 0;
1699 ret = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
1700 if (ret < 0) {
1701 printk(KERN_ERR "gs_setup: cannot queue response, ret=%d\n",
1702 ret);
1703 req->status = 0;
1704 gs_setup_complete(gadget->ep0, req);
1705 }
1706 }
1707
1708 /* device either stalls (ret < 0) or reports success */
1709 return ret;
1710 }
1711
1712 static int gs_setup_standard(struct usb_gadget *gadget,
1713 const struct usb_ctrlrequest *ctrl)
1714 {
1715 int ret = -EOPNOTSUPP;
1716 struct gs_dev *dev = get_gadget_data(gadget);
1717 struct usb_request *req = dev->dev_ctrl_req;
1718
1719 switch (ctrl->bRequest) {
1720 case USB_REQ_GET_DESCRIPTOR:
1721 if (ctrl->bRequestType != USB_DIR_IN)
1722 break;
1723
1724 switch (ctrl->wValue >> 8) {
1725 case USB_DT_DEVICE:
1726 ret = min(ctrl->wLength,
1727 (u16)sizeof(struct usb_device_descriptor));
1728 memcpy(req->buf, &gs_device_desc, ret);
1729 break;
1730
1731 #ifdef CONFIG_USB_GADGET_DUALSPEED
1732 case USB_DT_DEVICE_QUALIFIER:
1733 if (!gadget->is_dualspeed)
1734 break;
1735 ret = min(ctrl->wLength,
1736 (u16)sizeof(struct usb_qualifier_descriptor));
1737 memcpy(req->buf, &gs_qualifier_desc, ret);
1738 break;
1739
1740 case USB_DT_OTHER_SPEED_CONFIG:
1741 if (!gadget->is_dualspeed)
1742 break;
1743 /* fall through */
1744 #endif /* CONFIG_USB_GADGET_DUALSPEED */
1745 case USB_DT_CONFIG:
1746 ret = gs_build_config_buf(req->buf, gadget->speed,
1747 ctrl->wValue >> 8, ctrl->wValue & 0xff,
1748 gadget->is_otg);
1749 if (ret >= 0)
1750 ret = min(ctrl->wLength, (u16)ret);
1751 break;
1752
1753 case USB_DT_STRING:
1754 /* wIndex == language code. */
1755 ret = usb_gadget_get_string(&gs_string_table,
1756 ctrl->wValue & 0xff, req->buf);
1757 if (ret >= 0)
1758 ret = min(ctrl->wLength, (u16)ret);
1759 break;
1760 }
1761 break;
1762
1763 case USB_REQ_SET_CONFIGURATION:
1764 if (ctrl->bRequestType != 0)
1765 break;
1766 spin_lock(&dev->dev_lock);
1767 ret = gs_set_config(dev, ctrl->wValue);
1768 spin_unlock(&dev->dev_lock);
1769 break;
1770
1771 case USB_REQ_GET_CONFIGURATION:
1772 if (ctrl->bRequestType != USB_DIR_IN)
1773 break;
1774 *(u8 *)req->buf = dev->dev_config;
1775 ret = min(ctrl->wLength, (u16)1);
1776 break;
1777
1778 case USB_REQ_SET_INTERFACE:
1779 if (ctrl->bRequestType != USB_RECIP_INTERFACE
1780 || !dev->dev_config || ctrl->wIndex >= GS_MAX_NUM_INTERFACES)
1781 break;
1782 if (dev->dev_config == GS_BULK_CONFIG_ID
1783 && ctrl->wIndex != GS_BULK_INTERFACE_ID)
1784 break;
1785 /* no alternate interface settings */
1786 if (ctrl->wValue != 0)
1787 break;
1788 spin_lock(&dev->dev_lock);
1789 /* PXA hardware partially handles SET_INTERFACE;
1790 * we need to kluge around that interference. */
1791 if (gadget_is_pxa(gadget)) {
1792 ret = gs_set_config(dev, use_acm ?
1793 GS_ACM_CONFIG_ID : GS_BULK_CONFIG_ID);
1794 goto set_interface_done;
1795 }
1796 if (dev->dev_config != GS_BULK_CONFIG_ID
1797 && ctrl->wIndex == GS_CONTROL_INTERFACE_ID) {
1798 if (dev->dev_notify_ep) {
1799 usb_ep_disable(dev->dev_notify_ep);
1800 usb_ep_enable(dev->dev_notify_ep, dev->dev_notify_ep_desc);
1801 }
1802 } else {
1803 usb_ep_disable(dev->dev_in_ep);
1804 usb_ep_disable(dev->dev_out_ep);
1805 usb_ep_enable(dev->dev_in_ep, dev->dev_in_ep_desc);
1806 usb_ep_enable(dev->dev_out_ep, dev->dev_out_ep_desc);
1807 }
1808 ret = 0;
1809 set_interface_done:
1810 spin_unlock(&dev->dev_lock);
1811 break;
1812
1813 case USB_REQ_GET_INTERFACE:
1814 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)
1815 || dev->dev_config == GS_NO_CONFIG_ID)
1816 break;
1817 if (ctrl->wIndex >= GS_MAX_NUM_INTERFACES
1818 || (dev->dev_config == GS_BULK_CONFIG_ID
1819 && ctrl->wIndex != GS_BULK_INTERFACE_ID)) {
1820 ret = -EDOM;
1821 break;
1822 }
1823 /* no alternate interface settings */
1824 *(u8 *)req->buf = 0;
1825 ret = min(ctrl->wLength, (u16)1);
1826 break;
1827
1828 default:
1829 printk(KERN_ERR "gs_setup: unknown standard request, type=%02x, request=%02x, value=%04x, index=%04x, length=%d\n",
1830 ctrl->bRequestType, ctrl->bRequest, ctrl->wValue,
1831 ctrl->wIndex, ctrl->wLength);
1832 break;
1833 }
1834
1835 return ret;
1836 }
1837
1838 static int gs_setup_class(struct usb_gadget *gadget,
1839 const struct usb_ctrlrequest *ctrl)
1840 {
1841 int ret = -EOPNOTSUPP;
1842 struct gs_dev *dev = get_gadget_data(gadget);
1843 struct gs_port *port = dev->dev_port[0]; /* ACM only has one port */
1844 struct usb_request *req = dev->dev_ctrl_req;
1845
1846 switch (ctrl->bRequest) {
1847 case USB_CDC_REQ_SET_LINE_CODING:
1848 ret = min(ctrl->wLength,
1849 (u16)sizeof(struct usb_cdc_line_coding));
1850 if (port) {
1851 spin_lock(&port->port_lock);
1852 memcpy(&port->port_line_coding, req->buf, ret);
1853 spin_unlock(&port->port_lock);
1854 }
1855 break;
1856
1857 case USB_CDC_REQ_GET_LINE_CODING:
1858 port = dev->dev_port[0]; /* ACM only has one port */
1859 ret = min(ctrl->wLength,
1860 (u16)sizeof(struct usb_cdc_line_coding));
1861 if (port) {
1862 spin_lock(&port->port_lock);
1863 memcpy(req->buf, &port->port_line_coding, ret);
1864 spin_unlock(&port->port_lock);
1865 }
1866 break;
1867
1868 case USB_CDC_REQ_SET_CONTROL_LINE_STATE:
1869 ret = 0;
1870 break;
1871
1872 default:
1873 printk(KERN_ERR "gs_setup: unknown class request, type=%02x, request=%02x, value=%04x, index=%04x, length=%d\n",
1874 ctrl->bRequestType, ctrl->bRequest, ctrl->wValue,
1875 ctrl->wIndex, ctrl->wLength);
1876 break;
1877 }
1878
1879 return ret;
1880 }
1881
1882 /*
1883 * gs_setup_complete
1884 */
1885 static void gs_setup_complete(struct usb_ep *ep, struct usb_request *req)
1886 {
1887 if (req->status || req->actual != req->length) {
1888 printk(KERN_ERR "gs_setup_complete: status error, status=%d, actual=%d, length=%d\n",
1889 req->status, req->actual, req->length);
1890 }
1891 }
1892
1893 /*
1894 * gs_disconnect
1895 *
1896 * Called when the device is disconnected. Frees the closed
1897 * ports and disconnects open ports. Open ports will be freed
1898 * on close. Then reallocates the ports for the next connection.
1899 */
1900 static void gs_disconnect(struct usb_gadget *gadget)
1901 {
1902 unsigned long flags;
1903 struct gs_dev *dev = get_gadget_data(gadget);
1904
1905 spin_lock_irqsave(&dev->dev_lock, flags);
1906
1907 gs_reset_config(dev);
1908
1909 /* free closed ports and disconnect open ports */
1910 /* (open ports will be freed when closed) */
1911 gs_free_ports(dev);
1912
1913 /* re-allocate ports for the next connection */
1914 if (gs_alloc_ports(dev, GFP_ATOMIC) != 0)
1915 printk(KERN_ERR "gs_disconnect: cannot re-allocate ports\n");
1916
1917 spin_unlock_irqrestore(&dev->dev_lock, flags);
1918
1919 printk(KERN_INFO "gs_disconnect: %s disconnected\n", GS_LONG_NAME);
1920 }
1921
1922 /*
1923 * gs_set_config
1924 *
1925 * Configures the device by enabling device specific
1926 * optimizations, setting up the endpoints, allocating
1927 * read and write requests and queuing read requests.
1928 *
1929 * The device lock must be held when calling this function.
1930 */
1931 static int gs_set_config(struct gs_dev *dev, unsigned config)
1932 {
1933 int i;
1934 int ret = 0;
1935 struct usb_gadget *gadget = dev->dev_gadget;
1936 struct usb_ep *ep;
1937 struct usb_endpoint_descriptor *ep_desc;
1938 struct usb_request *req;
1939 struct gs_req_entry *req_entry;
1940
1941 if (dev == NULL) {
1942 printk(KERN_ERR "gs_set_config: NULL device pointer\n");
1943 return 0;
1944 }
1945
1946 if (config == dev->dev_config)
1947 return 0;
1948
1949 gs_reset_config(dev);
1950
1951 switch (config) {
1952 case GS_NO_CONFIG_ID:
1953 return 0;
1954 case GS_BULK_CONFIG_ID:
1955 if (use_acm)
1956 return -EINVAL;
1957 /* device specific optimizations */
1958 if (gadget_is_net2280(gadget))
1959 net2280_set_fifo_mode(gadget, 1);
1960 break;
1961 case GS_ACM_CONFIG_ID:
1962 if (!use_acm)
1963 return -EINVAL;
1964 /* device specific optimizations */
1965 if (gadget_is_net2280(gadget))
1966 net2280_set_fifo_mode(gadget, 1);
1967 break;
1968 default:
1969 return -EINVAL;
1970 }
1971
1972 dev->dev_config = config;
1973
1974 gadget_for_each_ep(ep, gadget) {
1975
1976 if (EP_NOTIFY_NAME
1977 && strcmp(ep->name, EP_NOTIFY_NAME) == 0) {
1978 ep_desc = GS_SPEED_SELECT(
1979 gadget->speed == USB_SPEED_HIGH,
1980 &gs_highspeed_notify_desc,
1981 &gs_fullspeed_notify_desc);
1982 ret = usb_ep_enable(ep,ep_desc);
1983 if (ret == 0) {
1984 ep->driver_data = dev;
1985 dev->dev_notify_ep = ep;
1986 dev->dev_notify_ep_desc = ep_desc;
1987 } else {
1988 printk(KERN_ERR "gs_set_config: cannot enable notify endpoint %s, ret=%d\n",
1989 ep->name, ret);
1990 goto exit_reset_config;
1991 }
1992 }
1993
1994 else if (strcmp(ep->name, EP_IN_NAME) == 0) {
1995 ep_desc = GS_SPEED_SELECT(
1996 gadget->speed == USB_SPEED_HIGH,
1997 &gs_highspeed_in_desc,
1998 &gs_fullspeed_in_desc);
1999 ret = usb_ep_enable(ep,ep_desc);
2000 if (ret == 0) {
2001 ep->driver_data = dev;
2002 dev->dev_in_ep = ep;
2003 dev->dev_in_ep_desc = ep_desc;
2004 } else {
2005 printk(KERN_ERR "gs_set_config: cannot enable in endpoint %s, ret=%d\n",
2006 ep->name, ret);
2007 goto exit_reset_config;
2008 }
2009 }
2010
2011 else if (strcmp(ep->name, EP_OUT_NAME) == 0) {
2012 ep_desc = GS_SPEED_SELECT(
2013 gadget->speed == USB_SPEED_HIGH,
2014 &gs_highspeed_out_desc,
2015 &gs_fullspeed_out_desc);
2016 ret = usb_ep_enable(ep,ep_desc);
2017 if (ret == 0) {
2018 ep->driver_data = dev;
2019 dev->dev_out_ep = ep;
2020 dev->dev_out_ep_desc = ep_desc;
2021 } else {
2022 printk(KERN_ERR "gs_set_config: cannot enable out endpoint %s, ret=%d\n",
2023 ep->name, ret);
2024 goto exit_reset_config;
2025 }
2026 }
2027
2028 }
2029
2030 if (dev->dev_in_ep == NULL || dev->dev_out_ep == NULL
2031 || (config != GS_BULK_CONFIG_ID && dev->dev_notify_ep == NULL)) {
2032 printk(KERN_ERR "gs_set_config: cannot find endpoints\n");
2033 ret = -ENODEV;
2034 goto exit_reset_config;
2035 }
2036
2037 /* allocate and queue read requests */
2038 ep = dev->dev_out_ep;
2039 for (i=0; i<read_q_size && ret == 0; i++) {
2040 if ((req=gs_alloc_req(ep, ep->maxpacket, GFP_ATOMIC))) {
2041 req->complete = gs_read_complete;
2042 if ((ret=usb_ep_queue(ep, req, GFP_ATOMIC))) {
2043 printk(KERN_ERR "gs_set_config: cannot queue read request, ret=%d\n",
2044 ret);
2045 }
2046 } else {
2047 printk(KERN_ERR "gs_set_config: cannot allocate read requests\n");
2048 ret = -ENOMEM;
2049 goto exit_reset_config;
2050 }
2051 }
2052
2053 /* allocate write requests, and put on free list */
2054 ep = dev->dev_in_ep;
2055 for (i=0; i<write_q_size; i++) {
2056 if ((req_entry=gs_alloc_req_entry(ep, ep->maxpacket, GFP_ATOMIC))) {
2057 req_entry->re_req->complete = gs_write_complete;
2058 list_add(&req_entry->re_entry, &dev->dev_req_list);
2059 } else {
2060 printk(KERN_ERR "gs_set_config: cannot allocate write requests\n");
2061 ret = -ENOMEM;
2062 goto exit_reset_config;
2063 }
2064 }
2065
2066 printk(KERN_INFO "gs_set_config: %s configured, %s speed %s config\n",
2067 GS_LONG_NAME,
2068 gadget->speed == USB_SPEED_HIGH ? "high" : "full",
2069 config == GS_BULK_CONFIG_ID ? "BULK" : "CDC-ACM");
2070
2071 return 0;
2072
2073 exit_reset_config:
2074 gs_reset_config(dev);
2075 return ret;
2076 }
2077
2078 /*
2079 * gs_reset_config
2080 *
2081 * Mark the device as not configured, disable all endpoints,
2082 * which forces completion of pending I/O and frees queued
2083 * requests, and free the remaining write requests on the
2084 * free list.
2085 *
2086 * The device lock must be held when calling this function.
2087 */
2088 static void gs_reset_config(struct gs_dev *dev)
2089 {
2090 struct gs_req_entry *req_entry;
2091
2092 if (dev == NULL) {
2093 printk(KERN_ERR "gs_reset_config: NULL device pointer\n");
2094 return;
2095 }
2096
2097 if (dev->dev_config == GS_NO_CONFIG_ID)
2098 return;
2099
2100 dev->dev_config = GS_NO_CONFIG_ID;
2101
2102 /* free write requests on the free list */
2103 while(!list_empty(&dev->dev_req_list)) {
2104 req_entry = list_entry(dev->dev_req_list.next,
2105 struct gs_req_entry, re_entry);
2106 list_del(&req_entry->re_entry);
2107 gs_free_req_entry(dev->dev_in_ep, req_entry);
2108 }
2109
2110 /* disable endpoints, forcing completion of pending i/o; */
2111 /* completion handlers free their requests in this case */
2112 if (dev->dev_notify_ep) {
2113 usb_ep_disable(dev->dev_notify_ep);
2114 dev->dev_notify_ep = NULL;
2115 }
2116 if (dev->dev_in_ep) {
2117 usb_ep_disable(dev->dev_in_ep);
2118 dev->dev_in_ep = NULL;
2119 }
2120 if (dev->dev_out_ep) {
2121 usb_ep_disable(dev->dev_out_ep);
2122 dev->dev_out_ep = NULL;
2123 }
2124 }
2125
2126 /*
2127 * gs_build_config_buf
2128 *
2129 * Builds the config descriptors in the given buffer and returns the
2130 * length, or a negative error number.
2131 */
2132 static int gs_build_config_buf(u8 *buf, enum usb_device_speed speed,
2133 u8 type, unsigned int index, int is_otg)
2134 {
2135 int len;
2136 int high_speed;
2137 const struct usb_config_descriptor *config_desc;
2138 const struct usb_descriptor_header **function;
2139
2140 if (index >= gs_device_desc.bNumConfigurations)
2141 return -EINVAL;
2142
2143 /* other speed switches high and full speed */
2144 high_speed = (speed == USB_SPEED_HIGH);
2145 if (type == USB_DT_OTHER_SPEED_CONFIG)
2146 high_speed = !high_speed;
2147
2148 if (use_acm) {
2149 config_desc = &gs_acm_config_desc;
2150 function = GS_SPEED_SELECT(high_speed,
2151 gs_acm_highspeed_function,
2152 gs_acm_fullspeed_function);
2153 } else {
2154 config_desc = &gs_bulk_config_desc;
2155 function = GS_SPEED_SELECT(high_speed,
2156 gs_bulk_highspeed_function,
2157 gs_bulk_fullspeed_function);
2158 }
2159
2160 /* for now, don't advertise srp-only devices */
2161 if (!is_otg)
2162 function++;
2163
2164 len = usb_gadget_config_buf(config_desc, buf, GS_MAX_DESC_LEN, function);
2165 if (len < 0)
2166 return len;
2167
2168 ((struct usb_config_descriptor *)buf)->bDescriptorType = type;
2169
2170 return len;
2171 }
2172
2173 /*
2174 * gs_alloc_req
2175 *
2176 * Allocate a usb_request and its buffer. Returns a pointer to the
2177 * usb_request or NULL if there is an error.
2178 */
2179 static struct usb_request *gs_alloc_req(struct usb_ep *ep, unsigned int len, int kmalloc_flags)
2180 {
2181 struct usb_request *req;
2182
2183 if (ep == NULL)
2184 return NULL;
2185
2186 req = usb_ep_alloc_request(ep, kmalloc_flags);
2187
2188 if (req != NULL) {
2189 req->length = len;
2190 req->buf = kmalloc(len, kmalloc_flags);
2191 if (req->buf == NULL) {
2192 usb_ep_free_request(ep, req);
2193 return NULL;
2194 }
2195 }
2196
2197 return req;
2198 }
2199
2200 /*
2201 * gs_free_req
2202 *
2203 * Free a usb_request and its buffer.
2204 */
2205 static void gs_free_req(struct usb_ep *ep, struct usb_request *req)
2206 {
2207 if (ep != NULL && req != NULL) {
2208 kfree(req->buf);
2209 usb_ep_free_request(ep, req);
2210 }
2211 }
2212
2213 /*
2214 * gs_alloc_req_entry
2215 *
2216 * Allocates a request and its buffer, using the given
2217 * endpoint, buffer len, and kmalloc flags.
2218 */
2219 static struct gs_req_entry *gs_alloc_req_entry(struct usb_ep *ep, unsigned len, int kmalloc_flags)
2220 {
2221 struct gs_req_entry *req;
2222
2223 req = kmalloc(sizeof(struct gs_req_entry), kmalloc_flags);
2224 if (req == NULL)
2225 return NULL;
2226
2227 req->re_req = gs_alloc_req(ep, len, kmalloc_flags);
2228 if (req->re_req == NULL) {
2229 kfree(req);
2230 return NULL;
2231 }
2232
2233 req->re_req->context = req;
2234
2235 return req;
2236 }
2237
2238 /*
2239 * gs_free_req_entry
2240 *
2241 * Frees a request and its buffer.
2242 */
2243 static void gs_free_req_entry(struct usb_ep *ep, struct gs_req_entry *req)
2244 {
2245 if (ep != NULL && req != NULL) {
2246 if (req->re_req != NULL)
2247 gs_free_req(ep, req->re_req);
2248 kfree(req);
2249 }
2250 }
2251
2252 /*
2253 * gs_alloc_ports
2254 *
2255 * Allocate all ports and set the gs_dev struct to point to them.
2256 * Return 0 if successful, or a negative error number.
2257 *
2258 * The device lock is normally held when calling this function.
2259 */
2260 static int gs_alloc_ports(struct gs_dev *dev, int kmalloc_flags)
2261 {
2262 int i;
2263 struct gs_port *port;
2264
2265 if (dev == NULL)
2266 return -EIO;
2267
2268 for (i=0; i<GS_NUM_PORTS; i++) {
2269 if ((port=(struct gs_port *)kmalloc(sizeof(struct gs_port), kmalloc_flags)) == NULL)
2270 return -ENOMEM;
2271
2272 memset(port, 0, sizeof(struct gs_port));
2273 port->port_dev = dev;
2274 port->port_num = i;
2275 port->port_line_coding.dwDTERate = GS_DEFAULT_DTE_RATE;
2276 port->port_line_coding.bCharFormat = GS_DEFAULT_CHAR_FORMAT;
2277 port->port_line_coding.bParityType = GS_DEFAULT_PARITY;
2278 port->port_line_coding.bDataBits = GS_DEFAULT_DATA_BITS;
2279 spin_lock_init(&port->port_lock);
2280 init_waitqueue_head(&port->port_write_wait);
2281
2282 dev->dev_port[i] = port;
2283 }
2284
2285 return 0;
2286 }
2287
2288 /*
2289 * gs_free_ports
2290 *
2291 * Free all closed ports. Open ports are disconnected by
2292 * freeing their write buffers, setting their device pointers
2293 * and the pointers to them in the device to NULL. These
2294 * ports will be freed when closed.
2295 *
2296 * The device lock is normally held when calling this function.
2297 */
2298 static void gs_free_ports(struct gs_dev *dev)
2299 {
2300 int i;
2301 unsigned long flags;
2302 struct gs_port *port;
2303
2304 if (dev == NULL)
2305 return;
2306
2307 for (i=0; i<GS_NUM_PORTS; i++) {
2308 if ((port=dev->dev_port[i]) != NULL) {
2309 dev->dev_port[i] = NULL;
2310
2311 spin_lock_irqsave(&port->port_lock, flags);
2312
2313 if (port->port_write_buf != NULL) {
2314 gs_buf_free(port->port_write_buf);
2315 port->port_write_buf = NULL;
2316 }
2317
2318 if (port->port_open_count > 0 || port->port_in_use) {
2319 port->port_dev = NULL;
2320 wake_up_interruptible(&port->port_write_wait);
2321 if (port->port_tty) {
2322 wake_up_interruptible(&port->port_tty->read_wait);
2323 wake_up_interruptible(&port->port_tty->write_wait);
2324 }
2325 spin_unlock_irqrestore(&port->port_lock, flags);
2326 } else {
2327 kfree(port);
2328 }
2329
2330 }
2331 }
2332 }
2333
2334 /* Circular Buffer */
2335
2336 /*
2337 * gs_buf_alloc
2338 *
2339 * Allocate a circular buffer and all associated memory.
2340 */
2341 static struct gs_buf *gs_buf_alloc(unsigned int size, int kmalloc_flags)
2342 {
2343 struct gs_buf *gb;
2344
2345 if (size == 0)
2346 return NULL;
2347
2348 gb = (struct gs_buf *)kmalloc(sizeof(struct gs_buf), kmalloc_flags);
2349 if (gb == NULL)
2350 return NULL;
2351
2352 gb->buf_buf = kmalloc(size, kmalloc_flags);
2353 if (gb->buf_buf == NULL) {
2354 kfree(gb);
2355 return NULL;
2356 }
2357
2358 gb->buf_size = size;
2359 gb->buf_get = gb->buf_put = gb->buf_buf;
2360
2361 return gb;
2362 }
2363
2364 /*
2365 * gs_buf_free
2366 *
2367 * Free the buffer and all associated memory.
2368 */
2369 void gs_buf_free(struct gs_buf *gb)
2370 {
2371 if (gb != NULL) {
2372 if (gb->buf_buf != NULL)
2373 kfree(gb->buf_buf);
2374 kfree(gb);
2375 }
2376 }
2377
2378 /*
2379 * gs_buf_clear
2380 *
2381 * Clear out all data in the circular buffer.
2382 */
2383 void gs_buf_clear(struct gs_buf *gb)
2384 {
2385 if (gb != NULL)
2386 gb->buf_get = gb->buf_put;
2387 /* equivalent to a get of all data available */
2388 }
2389
2390 /*
2391 * gs_buf_data_avail
2392 *
2393 * Return the number of bytes of data available in the circular
2394 * buffer.
2395 */
2396 unsigned int gs_buf_data_avail(struct gs_buf *gb)
2397 {
2398 if (gb != NULL)
2399 return (gb->buf_size + gb->buf_put - gb->buf_get) % gb->buf_size;
2400 else
2401 return 0;
2402 }
2403
2404 /*
2405 * gs_buf_space_avail
2406 *
2407 * Return the number of bytes of space available in the circular
2408 * buffer.
2409 */
2410 unsigned int gs_buf_space_avail(struct gs_buf *gb)
2411 {
2412 if (gb != NULL)
2413 return (gb->buf_size + gb->buf_get - gb->buf_put - 1) % gb->buf_size;
2414 else
2415 return 0;
2416 }
2417
2418 /*
2419 * gs_buf_put
2420 *
2421 * Copy data data from a user buffer and put it into the circular buffer.
2422 * Restrict to the amount of space available.
2423 *
2424 * Return the number of bytes copied.
2425 */
2426 unsigned int gs_buf_put(struct gs_buf *gb, const char *buf, unsigned int count)
2427 {
2428 unsigned int len;
2429
2430 if (gb == NULL)
2431 return 0;
2432
2433 len = gs_buf_space_avail(gb);
2434 if (count > len)
2435 count = len;
2436
2437 if (count == 0)
2438 return 0;
2439
2440 len = gb->buf_buf + gb->buf_size - gb->buf_put;
2441 if (count > len) {
2442 memcpy(gb->buf_put, buf, len);
2443 memcpy(gb->buf_buf, buf+len, count - len);
2444 gb->buf_put = gb->buf_buf + count - len;
2445 } else {
2446 memcpy(gb->buf_put, buf, count);
2447 if (count < len)
2448 gb->buf_put += count;
2449 else /* count == len */
2450 gb->buf_put = gb->buf_buf;
2451 }
2452
2453 return count;
2454 }
2455
2456 /*
2457 * gs_buf_get
2458 *
2459 * Get data from the circular buffer and copy to the given buffer.
2460 * Restrict to the amount of data available.
2461 *
2462 * Return the number of bytes copied.
2463 */
2464 unsigned int gs_buf_get(struct gs_buf *gb, char *buf, unsigned int count)
2465 {
2466 unsigned int len;
2467
2468 if (gb == NULL)
2469 return 0;
2470
2471 len = gs_buf_data_avail(gb);
2472 if (count > len)
2473 count = len;
2474
2475 if (count == 0)
2476 return 0;
2477
2478 len = gb->buf_buf + gb->buf_size - gb->buf_get;
2479 if (count > len) {
2480 memcpy(buf, gb->buf_get, len);
2481 memcpy(buf+len, gb->buf_buf, count - len);
2482 gb->buf_get = gb->buf_buf + count - len;
2483 } else {
2484 memcpy(buf, gb->buf_get, count);
2485 if (count < len)
2486 gb->buf_get += count;
2487 else /* count == len */
2488 gb->buf_get = gb->buf_buf;
2489 }
2490
2491 return count;
2492 }
2493
|
This page was automatically generated by the
LXR engine.
|