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