blob: 64ce7e00e98a0c9c3d6d8d0b70dbe64dc1443c79 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
David Brownella7707ad2008-06-19 17:52:07 -07002 * serial.c -- USB gadget serial driver
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
David Brownella7707ad2008-06-19 17:52:07 -07004 * Copyright (C) 2003 Al Borchers (alborchers@steinerpoint.com)
5 * Copyright (C) 2008 by David Brownell
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * This software is distributed under the terms of the GNU General
8 * Public License ("GPL") as published by the Free Software Foundation,
9 * either version 2 of that License or (at your option) any later version.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
11
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/utsname.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/device.h>
15#include <linux/tty.h>
16#include <linux/tty_flip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
David Brownella7707ad2008-06-19 17:52:07 -070018#include "u_serial.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include "gadget_chips.h"
20
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022/* Defines */
23
David Brownella7707ad2008-06-19 17:52:07 -070024#define GS_VERSION_STR "v2.3"
25#define GS_VERSION_NUM 0x2300
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27#define GS_LONG_NAME "Gadget Serial"
28#define GS_SHORT_NAME "g_serial"
29
David Brownella7707ad2008-06-19 17:52:07 -070030#define GS_VERSION_NAME GS_LONG_NAME " " GS_VERSION_STR
31
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
David Brownell9079e912008-05-07 16:00:36 -070033/* REVISIT only one port is supported for now;
34 * see gs_{send,recv}_packet() ... no multiplexing,
35 * and no support for multiple ACM devices.
36 */
37#define GS_NUM_PORTS 1
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39#define GS_NUM_CONFIGS 1
40#define GS_NO_CONFIG_ID 0
41#define GS_BULK_CONFIG_ID 1
42#define GS_ACM_CONFIG_ID 2
43
44#define GS_MAX_NUM_INTERFACES 2
45#define GS_BULK_INTERFACE_ID 0
46#define GS_CONTROL_INTERFACE_ID 0
47#define GS_DATA_INTERFACE_ID 1
48
49#define GS_MAX_DESC_LEN 256
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#define GS_DEFAULT_USE_ACM 0
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
David Brownell51a0e852007-08-02 00:02:47 -070054/* maxpacket and other transfer characteristics vary by speed. */
55static inline struct usb_endpoint_descriptor *
56choose_ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs,
57 struct usb_endpoint_descriptor *fs)
58{
59 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
60 return hs;
61 return fs;
62}
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Linus Torvalds1da177e2005-04-16 15:20:36 -070065/* Thanks to NetChip Technologies for donating this product ID.
66 *
67 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
68 * Instead: allocate your own, using normal USB-IF procedures.
69 */
70#define GS_VENDOR_ID 0x0525 /* NetChip */
71#define GS_PRODUCT_ID 0xa4a6 /* Linux-USB Serial Gadget */
72#define GS_CDC_PRODUCT_ID 0xa4a7 /* ... as CDC-ACM */
73
74#define GS_LOG2_NOTIFY_INTERVAL 5 /* 1 << 5 == 32 msec */
75#define GS_NOTIFY_MAXPACKET 8
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077/* the device structure holds info for the USB device */
78struct gs_dev {
79 struct usb_gadget *dev_gadget; /* gadget device pointer */
80 spinlock_t dev_lock; /* lock for set/reset config */
81 int dev_config; /* configuration number */
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 struct usb_request *dev_ctrl_req; /* control request */
David Brownella7707ad2008-06-19 17:52:07 -070083
84 struct gserial gser; /* serial/tty port */
Linus Torvalds1da177e2005-04-16 15:20:36 -070085};
86
87
88/* Functions */
89
David Brownell9079e912008-05-07 16:00:36 -070090/* gadget driver internals */
Linus Torvalds1da177e2005-04-16 15:20:36 -070091static int gs_set_config(struct gs_dev *dev, unsigned config);
92static void gs_reset_config(struct gs_dev *dev);
David Brownell51a0e852007-08-02 00:02:47 -070093static int gs_build_config_buf(u8 *buf, struct usb_gadget *g,
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 u8 type, unsigned int index, int is_otg);
95
96static struct usb_request *gs_alloc_req(struct usb_ep *ep, unsigned int len,
Al Viro55016f12005-10-21 03:21:58 -040097 gfp_t kmalloc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098static void gs_free_req(struct usb_ep *ep, struct usb_request *req);
99
David Brownell9079e912008-05-07 16:00:36 -0700100/*-------------------------------------------------------------------------*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102/* USB descriptors */
103
104#define GS_MANUFACTURER_STR_ID 1
105#define GS_PRODUCT_STR_ID 2
106#define GS_SERIAL_STR_ID 3
107#define GS_BULK_CONFIG_STR_ID 4
108#define GS_ACM_CONFIG_STR_ID 5
109#define GS_CONTROL_STR_ID 6
110#define GS_DATA_STR_ID 7
111
112/* static strings, in UTF-8 */
113static char manufacturer[50];
114static struct usb_string gs_strings[] = {
115 { GS_MANUFACTURER_STR_ID, manufacturer },
David Brownella7707ad2008-06-19 17:52:07 -0700116 { GS_PRODUCT_STR_ID, GS_VERSION_NAME },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 { GS_BULK_CONFIG_STR_ID, "Gadget Serial Bulk" },
118 { GS_ACM_CONFIG_STR_ID, "Gadget Serial CDC ACM" },
119 { GS_CONTROL_STR_ID, "Gadget Serial Control" },
120 { GS_DATA_STR_ID, "Gadget Serial Data" },
121 { } /* end of list */
122};
123
124static struct usb_gadget_strings gs_string_table = {
125 .language = 0x0409, /* en-us */
126 .strings = gs_strings,
127};
128
129static struct usb_device_descriptor gs_device_desc = {
130 .bLength = USB_DT_DEVICE_SIZE,
131 .bDescriptorType = USB_DT_DEVICE,
132 .bcdUSB = __constant_cpu_to_le16(0x0200),
133 .bDeviceSubClass = 0,
134 .bDeviceProtocol = 0,
135 .idVendor = __constant_cpu_to_le16(GS_VENDOR_ID),
136 .idProduct = __constant_cpu_to_le16(GS_PRODUCT_ID),
137 .iManufacturer = GS_MANUFACTURER_STR_ID,
138 .iProduct = GS_PRODUCT_STR_ID,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 .bNumConfigurations = GS_NUM_CONFIGS,
140};
141
142static struct usb_otg_descriptor gs_otg_descriptor = {
143 .bLength = sizeof(gs_otg_descriptor),
144 .bDescriptorType = USB_DT_OTG,
145 .bmAttributes = USB_OTG_SRP,
146};
147
148static struct usb_config_descriptor gs_bulk_config_desc = {
149 .bLength = USB_DT_CONFIG_SIZE,
150 .bDescriptorType = USB_DT_CONFIG,
151 /* .wTotalLength computed dynamically */
152 .bNumInterfaces = 1,
153 .bConfigurationValue = GS_BULK_CONFIG_ID,
154 .iConfiguration = GS_BULK_CONFIG_STR_ID,
155 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
156 .bMaxPower = 1,
157};
158
159static struct usb_config_descriptor gs_acm_config_desc = {
160 .bLength = USB_DT_CONFIG_SIZE,
161 .bDescriptorType = USB_DT_CONFIG,
162 /* .wTotalLength computed dynamically */
163 .bNumInterfaces = 2,
164 .bConfigurationValue = GS_ACM_CONFIG_ID,
165 .iConfiguration = GS_ACM_CONFIG_STR_ID,
166 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
167 .bMaxPower = 1,
168};
169
170static const struct usb_interface_descriptor gs_bulk_interface_desc = {
171 .bLength = USB_DT_INTERFACE_SIZE,
172 .bDescriptorType = USB_DT_INTERFACE,
173 .bInterfaceNumber = GS_BULK_INTERFACE_ID,
174 .bNumEndpoints = 2,
David Brownellb9370332008-05-07 14:27:37 -0700175 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 .bInterfaceSubClass = 0,
177 .bInterfaceProtocol = 0,
178 .iInterface = GS_DATA_STR_ID,
179};
180
181static const struct usb_interface_descriptor gs_control_interface_desc = {
182 .bLength = USB_DT_INTERFACE_SIZE,
183 .bDescriptorType = USB_DT_INTERFACE,
184 .bInterfaceNumber = GS_CONTROL_INTERFACE_ID,
185 .bNumEndpoints = 1,
186 .bInterfaceClass = USB_CLASS_COMM,
187 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
188 .bInterfaceProtocol = USB_CDC_ACM_PROTO_AT_V25TER,
189 .iInterface = GS_CONTROL_STR_ID,
190};
191
192static const struct usb_interface_descriptor gs_data_interface_desc = {
193 .bLength = USB_DT_INTERFACE_SIZE,
194 .bDescriptorType = USB_DT_INTERFACE,
195 .bInterfaceNumber = GS_DATA_INTERFACE_ID,
196 .bNumEndpoints = 2,
197 .bInterfaceClass = USB_CLASS_CDC_DATA,
198 .bInterfaceSubClass = 0,
199 .bInterfaceProtocol = 0,
200 .iInterface = GS_DATA_STR_ID,
201};
202
203static const struct usb_cdc_header_desc gs_header_desc = {
204 .bLength = sizeof(gs_header_desc),
205 .bDescriptorType = USB_DT_CS_INTERFACE,
206 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
207 .bcdCDC = __constant_cpu_to_le16(0x0110),
208};
209
210static const struct usb_cdc_call_mgmt_descriptor gs_call_mgmt_descriptor = {
David Brownell51a0e852007-08-02 00:02:47 -0700211 .bLength = sizeof(gs_call_mgmt_descriptor),
212 .bDescriptorType = USB_DT_CS_INTERFACE,
213 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
214 .bmCapabilities = 0,
215 .bDataInterface = 1, /* index of data interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216};
217
218static struct usb_cdc_acm_descriptor gs_acm_descriptor = {
David Brownell51a0e852007-08-02 00:02:47 -0700219 .bLength = sizeof(gs_acm_descriptor),
220 .bDescriptorType = USB_DT_CS_INTERFACE,
221 .bDescriptorSubType = USB_CDC_ACM_TYPE,
David Brownellf371e752008-04-18 17:37:49 -0700222 .bmCapabilities = (1 << 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223};
224
225static const struct usb_cdc_union_desc gs_union_desc = {
226 .bLength = sizeof(gs_union_desc),
227 .bDescriptorType = USB_DT_CS_INTERFACE,
228 .bDescriptorSubType = USB_CDC_UNION_TYPE,
229 .bMasterInterface0 = 0, /* index of control interface */
230 .bSlaveInterface0 = 1, /* index of data interface */
231};
David Brownell51a0e852007-08-02 00:02:47 -0700232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233static struct usb_endpoint_descriptor gs_fullspeed_notify_desc = {
234 .bLength = USB_DT_ENDPOINT_SIZE,
235 .bDescriptorType = USB_DT_ENDPOINT,
236 .bEndpointAddress = USB_DIR_IN,
237 .bmAttributes = USB_ENDPOINT_XFER_INT,
238 .wMaxPacketSize = __constant_cpu_to_le16(GS_NOTIFY_MAXPACKET),
239 .bInterval = 1 << GS_LOG2_NOTIFY_INTERVAL,
240};
241
242static struct usb_endpoint_descriptor gs_fullspeed_in_desc = {
243 .bLength = USB_DT_ENDPOINT_SIZE,
244 .bDescriptorType = USB_DT_ENDPOINT,
245 .bEndpointAddress = USB_DIR_IN,
246 .bmAttributes = USB_ENDPOINT_XFER_BULK,
247};
248
249static struct usb_endpoint_descriptor gs_fullspeed_out_desc = {
250 .bLength = USB_DT_ENDPOINT_SIZE,
251 .bDescriptorType = USB_DT_ENDPOINT,
252 .bEndpointAddress = USB_DIR_OUT,
253 .bmAttributes = USB_ENDPOINT_XFER_BULK,
254};
255
256static const struct usb_descriptor_header *gs_bulk_fullspeed_function[] = {
257 (struct usb_descriptor_header *) &gs_otg_descriptor,
258 (struct usb_descriptor_header *) &gs_bulk_interface_desc,
259 (struct usb_descriptor_header *) &gs_fullspeed_in_desc,
260 (struct usb_descriptor_header *) &gs_fullspeed_out_desc,
261 NULL,
262};
263
264static const struct usb_descriptor_header *gs_acm_fullspeed_function[] = {
265 (struct usb_descriptor_header *) &gs_otg_descriptor,
266 (struct usb_descriptor_header *) &gs_control_interface_desc,
267 (struct usb_descriptor_header *) &gs_header_desc,
268 (struct usb_descriptor_header *) &gs_call_mgmt_descriptor,
269 (struct usb_descriptor_header *) &gs_acm_descriptor,
270 (struct usb_descriptor_header *) &gs_union_desc,
271 (struct usb_descriptor_header *) &gs_fullspeed_notify_desc,
272 (struct usb_descriptor_header *) &gs_data_interface_desc,
273 (struct usb_descriptor_header *) &gs_fullspeed_in_desc,
274 (struct usb_descriptor_header *) &gs_fullspeed_out_desc,
275 NULL,
276};
277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278static struct usb_endpoint_descriptor gs_highspeed_notify_desc = {
279 .bLength = USB_DT_ENDPOINT_SIZE,
280 .bDescriptorType = USB_DT_ENDPOINT,
281 .bEndpointAddress = USB_DIR_IN,
282 .bmAttributes = USB_ENDPOINT_XFER_INT,
283 .wMaxPacketSize = __constant_cpu_to_le16(GS_NOTIFY_MAXPACKET),
284 .bInterval = GS_LOG2_NOTIFY_INTERVAL+4,
285};
286
287static struct usb_endpoint_descriptor gs_highspeed_in_desc = {
288 .bLength = USB_DT_ENDPOINT_SIZE,
289 .bDescriptorType = USB_DT_ENDPOINT,
290 .bmAttributes = USB_ENDPOINT_XFER_BULK,
291 .wMaxPacketSize = __constant_cpu_to_le16(512),
292};
293
294static struct usb_endpoint_descriptor gs_highspeed_out_desc = {
295 .bLength = USB_DT_ENDPOINT_SIZE,
296 .bDescriptorType = USB_DT_ENDPOINT,
297 .bmAttributes = USB_ENDPOINT_XFER_BULK,
298 .wMaxPacketSize = __constant_cpu_to_le16(512),
299};
300
301static struct usb_qualifier_descriptor gs_qualifier_desc = {
302 .bLength = sizeof(struct usb_qualifier_descriptor),
303 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
304 .bcdUSB = __constant_cpu_to_le16 (0x0200),
305 /* assumes ep0 uses the same value for both speeds ... */
306 .bNumConfigurations = GS_NUM_CONFIGS,
307};
308
309static const struct usb_descriptor_header *gs_bulk_highspeed_function[] = {
310 (struct usb_descriptor_header *) &gs_otg_descriptor,
311 (struct usb_descriptor_header *) &gs_bulk_interface_desc,
312 (struct usb_descriptor_header *) &gs_highspeed_in_desc,
313 (struct usb_descriptor_header *) &gs_highspeed_out_desc,
314 NULL,
315};
316
317static const struct usb_descriptor_header *gs_acm_highspeed_function[] = {
318 (struct usb_descriptor_header *) &gs_otg_descriptor,
319 (struct usb_descriptor_header *) &gs_control_interface_desc,
320 (struct usb_descriptor_header *) &gs_header_desc,
321 (struct usb_descriptor_header *) &gs_call_mgmt_descriptor,
322 (struct usb_descriptor_header *) &gs_acm_descriptor,
323 (struct usb_descriptor_header *) &gs_union_desc,
324 (struct usb_descriptor_header *) &gs_highspeed_notify_desc,
325 (struct usb_descriptor_header *) &gs_data_interface_desc,
326 (struct usb_descriptor_header *) &gs_highspeed_in_desc,
327 (struct usb_descriptor_header *) &gs_highspeed_out_desc,
328 NULL,
329};
330
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
David Brownell9079e912008-05-07 16:00:36 -0700332/*-------------------------------------------------------------------------*/
333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334/* Module */
David Brownella7707ad2008-06-19 17:52:07 -0700335MODULE_DESCRIPTION(GS_VERSION_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336MODULE_AUTHOR("Al Borchers");
David Brownella7707ad2008-06-19 17:52:07 -0700337MODULE_AUTHOR("David Brownell");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338MODULE_LICENSE("GPL");
339
David Brownell9079e912008-05-07 16:00:36 -0700340static unsigned int use_acm = GS_DEFAULT_USE_ACM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341module_param(use_acm, uint, S_IRUGO);
342MODULE_PARM_DESC(use_acm, "Use CDC ACM, 0=no, 1=yes, default=no");
343
David Brownell9079e912008-05-07 16:00:36 -0700344/*-------------------------------------------------------------------------*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346/* Gadget Driver */
347
348/*
David Brownell9079e912008-05-07 16:00:36 -0700349 * gs_unbind
350 *
351 * Called on module unload. Frees the control request and device
352 * structure.
353 */
David Brownella7707ad2008-06-19 17:52:07 -0700354static void __exit gs_unbind(struct usb_gadget *gadget)
David Brownell9079e912008-05-07 16:00:36 -0700355{
356 struct gs_dev *dev = get_gadget_data(gadget);
357
David Brownell9079e912008-05-07 16:00:36 -0700358 /* read/write requests already freed, only control request remains */
359 if (dev != NULL) {
360 if (dev->dev_ctrl_req != NULL) {
361 gs_free_req(gadget->ep0, dev->dev_ctrl_req);
362 dev->dev_ctrl_req = NULL;
363 }
David Brownell734d37c2008-05-07 14:25:24 -0700364 gs_reset_config(dev);
David Brownell9079e912008-05-07 16:00:36 -0700365 kfree(dev);
366 set_gadget_data(gadget, NULL);
367 }
368
David Brownella7707ad2008-06-19 17:52:07 -0700369 pr_info("gs_unbind: %s unbound\n", GS_VERSION_NAME);
370
371 gserial_cleanup();
David Brownell9079e912008-05-07 16:00:36 -0700372}
373
374/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 * gs_bind
376 *
377 * Called on module load. Allocates and initializes the device
378 * structure and a control request.
379 */
David Brownell329af282006-02-18 12:31:05 -0800380static int __init gs_bind(struct usb_gadget *gadget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
382 int ret;
383 struct usb_ep *ep;
384 struct gs_dev *dev;
David Brownell91e79c92005-07-13 15:18:30 -0700385 int gcnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
David Brownella7707ad2008-06-19 17:52:07 -0700387 ret = gserial_setup(gadget, GS_NUM_PORTS);
388 if (ret < 0)
389 return ret;
390
David Brownell91e79c92005-07-13 15:18:30 -0700391 /* Some controllers can't support CDC ACM:
392 * - sh doesn't support multiple interfaces or configs;
393 * - sa1100 doesn't have a third interrupt endpoint
394 */
395 if (gadget_is_sh(gadget) || gadget_is_sa1100(gadget))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 use_acm = 0;
David Brownell91e79c92005-07-13 15:18:30 -0700397
398 gcnum = usb_gadget_controller_number(gadget);
399 if (gcnum >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 gs_device_desc.bcdDevice =
David Brownell91e79c92005-07-13 15:18:30 -0700401 cpu_to_le16(GS_VERSION_NUM | gcnum);
402 else {
David Brownell00274922007-11-19 12:58:36 -0800403 pr_warning("gs_bind: controller '%s' not recognized\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 gadget->name);
405 /* unrecognized, but safe unless bulk is REALLY quirky */
406 gs_device_desc.bcdDevice =
407 __constant_cpu_to_le16(GS_VERSION_NUM|0x0099);
408 }
409
David Brownell734d37c2008-05-07 14:25:24 -0700410 dev = kzalloc(sizeof(struct gs_dev), GFP_KERNEL);
David Brownella7707ad2008-06-19 17:52:07 -0700411 if (dev == NULL) {
412 ret = -ENOMEM;
413 goto autoconf_fail;
414 }
David Brownell734d37c2008-05-07 14:25:24 -0700415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 usb_ep_autoconfig_reset(gadget);
David Brownella7707ad2008-06-19 17:52:07 -0700417 ret = -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
419 ep = usb_ep_autoconfig(gadget, &gs_fullspeed_in_desc);
420 if (!ep)
421 goto autoconf_fail;
David Brownella7707ad2008-06-19 17:52:07 -0700422 dev->gser.in = ep;
David Brownell734d37c2008-05-07 14:25:24 -0700423 ep->driver_data = dev; /* claim the endpoint */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
425 ep = usb_ep_autoconfig(gadget, &gs_fullspeed_out_desc);
426 if (!ep)
427 goto autoconf_fail;
David Brownella7707ad2008-06-19 17:52:07 -0700428 dev->gser.out = ep;
David Brownell734d37c2008-05-07 14:25:24 -0700429 ep->driver_data = dev; /* claim the endpoint */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431 if (use_acm) {
432 ep = usb_ep_autoconfig(gadget, &gs_fullspeed_notify_desc);
433 if (!ep) {
David Brownell00274922007-11-19 12:58:36 -0800434 pr_err("gs_bind: cannot run ACM on %s\n", gadget->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 goto autoconf_fail;
436 }
437 gs_device_desc.idProduct = __constant_cpu_to_le16(
438 GS_CDC_PRODUCT_ID),
David Brownella7707ad2008-06-19 17:52:07 -0700439 dev->gser.notify = ep;
David Brownell734d37c2008-05-07 14:25:24 -0700440 ep->driver_data = dev; /* claim the endpoint */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 }
442
443 gs_device_desc.bDeviceClass = use_acm
444 ? USB_CLASS_COMM : USB_CLASS_VENDOR_SPEC;
445 gs_device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
446
David Brownell51a0e852007-08-02 00:02:47 -0700447 if (gadget_is_dualspeed(gadget)) {
448 gs_qualifier_desc.bDeviceClass = use_acm
449 ? USB_CLASS_COMM : USB_CLASS_VENDOR_SPEC;
450 /* assume ep0 uses the same packet size for both speeds */
451 gs_qualifier_desc.bMaxPacketSize0 =
452 gs_device_desc.bMaxPacketSize0;
453 /* assume endpoints are dual-speed */
454 gs_highspeed_notify_desc.bEndpointAddress =
455 gs_fullspeed_notify_desc.bEndpointAddress;
456 gs_highspeed_in_desc.bEndpointAddress =
457 gs_fullspeed_in_desc.bEndpointAddress;
458 gs_highspeed_out_desc.bEndpointAddress =
459 gs_fullspeed_out_desc.bEndpointAddress;
460 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
462 usb_gadget_set_selfpowered(gadget);
463
David Brownell51a0e852007-08-02 00:02:47 -0700464 if (gadget_is_otg(gadget)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 gs_otg_descriptor.bmAttributes |= USB_OTG_HNP,
466 gs_bulk_config_desc.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
467 gs_acm_config_desc.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
468 }
469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 snprintf(manufacturer, sizeof(manufacturer), "%s %s with %s",
Serge E. Hallyn96b644b2006-10-02 02:18:13 -0700471 init_utsname()->sysname, init_utsname()->release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 gadget->name);
473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 dev->dev_gadget = gadget;
475 spin_lock_init(&dev->dev_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 set_gadget_data(gadget, dev);
477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 /* preallocate control response and buffer */
479 dev->dev_ctrl_req = gs_alloc_req(gadget->ep0, GS_MAX_DESC_LEN,
480 GFP_KERNEL);
481 if (dev->dev_ctrl_req == NULL) {
David Brownella7707ad2008-06-19 17:52:07 -0700482 ret = -ENOMEM;
483 goto autoconf_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 gadget->ep0->driver_data = dev;
486
David Brownella7707ad2008-06-19 17:52:07 -0700487 pr_info("gs_bind: %s bound\n", GS_VERSION_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
489 return 0;
490
491autoconf_fail:
David Brownell734d37c2008-05-07 14:25:24 -0700492 kfree(dev);
David Brownella7707ad2008-06-19 17:52:07 -0700493 gserial_cleanup();
494 pr_err("gs_bind: to %s, err %d\n", gadget->name, ret);
495 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496}
497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498static int gs_setup_standard(struct usb_gadget *gadget,
499 const struct usb_ctrlrequest *ctrl)
500{
501 int ret = -EOPNOTSUPP;
502 struct gs_dev *dev = get_gadget_data(gadget);
503 struct usb_request *req = dev->dev_ctrl_req;
David Brownell1bbc1692005-05-07 13:05:13 -0700504 u16 wIndex = le16_to_cpu(ctrl->wIndex);
505 u16 wValue = le16_to_cpu(ctrl->wValue);
506 u16 wLength = le16_to_cpu(ctrl->wLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
508 switch (ctrl->bRequest) {
509 case USB_REQ_GET_DESCRIPTOR:
510 if (ctrl->bRequestType != USB_DIR_IN)
511 break;
512
513 switch (wValue >> 8) {
514 case USB_DT_DEVICE:
515 ret = min(wLength,
516 (u16)sizeof(struct usb_device_descriptor));
517 memcpy(req->buf, &gs_device_desc, ret);
518 break;
519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 case USB_DT_DEVICE_QUALIFIER:
David Brownell51a0e852007-08-02 00:02:47 -0700521 if (!gadget_is_dualspeed(gadget))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 break;
523 ret = min(wLength,
524 (u16)sizeof(struct usb_qualifier_descriptor));
525 memcpy(req->buf, &gs_qualifier_desc, ret);
526 break;
527
528 case USB_DT_OTHER_SPEED_CONFIG:
David Brownell51a0e852007-08-02 00:02:47 -0700529 if (!gadget_is_dualspeed(gadget))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 break;
531 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 case USB_DT_CONFIG:
David Brownell51a0e852007-08-02 00:02:47 -0700533 ret = gs_build_config_buf(req->buf, gadget,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 wValue >> 8, wValue & 0xff,
David Brownell51a0e852007-08-02 00:02:47 -0700535 gadget_is_otg(gadget));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 if (ret >= 0)
537 ret = min(wLength, (u16)ret);
538 break;
539
540 case USB_DT_STRING:
541 /* wIndex == language code. */
542 ret = usb_gadget_get_string(&gs_string_table,
543 wValue & 0xff, req->buf);
544 if (ret >= 0)
545 ret = min(wLength, (u16)ret);
546 break;
547 }
548 break;
549
550 case USB_REQ_SET_CONFIGURATION:
551 if (ctrl->bRequestType != 0)
552 break;
553 spin_lock(&dev->dev_lock);
554 ret = gs_set_config(dev, wValue);
555 spin_unlock(&dev->dev_lock);
556 break;
557
558 case USB_REQ_GET_CONFIGURATION:
559 if (ctrl->bRequestType != USB_DIR_IN)
560 break;
561 *(u8 *)req->buf = dev->dev_config;
562 ret = min(wLength, (u16)1);
563 break;
564
565 case USB_REQ_SET_INTERFACE:
566 if (ctrl->bRequestType != USB_RECIP_INTERFACE
567 || !dev->dev_config
568 || wIndex >= GS_MAX_NUM_INTERFACES)
569 break;
570 if (dev->dev_config == GS_BULK_CONFIG_ID
571 && wIndex != GS_BULK_INTERFACE_ID)
572 break;
573 /* no alternate interface settings */
574 if (wValue != 0)
575 break;
576 spin_lock(&dev->dev_lock);
577 /* PXA hardware partially handles SET_INTERFACE;
578 * we need to kluge around that interference. */
579 if (gadget_is_pxa(gadget)) {
580 ret = gs_set_config(dev, use_acm ?
581 GS_ACM_CONFIG_ID : GS_BULK_CONFIG_ID);
582 goto set_interface_done;
583 }
584 if (dev->dev_config != GS_BULK_CONFIG_ID
585 && wIndex == GS_CONTROL_INTERFACE_ID) {
David Brownella7707ad2008-06-19 17:52:07 -0700586 if (dev->gser.notify) {
587 usb_ep_disable(dev->gser.notify);
588 usb_ep_enable(dev->gser.notify,
589 dev->gser.notify_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 }
591 } else {
David Brownella7707ad2008-06-19 17:52:07 -0700592 gserial_connect(&dev->gser, 0);
593 gserial_disconnect(&dev->gser);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 }
595 ret = 0;
596set_interface_done:
597 spin_unlock(&dev->dev_lock);
598 break;
599
600 case USB_REQ_GET_INTERFACE:
601 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)
602 || dev->dev_config == GS_NO_CONFIG_ID)
603 break;
604 if (wIndex >= GS_MAX_NUM_INTERFACES
605 || (dev->dev_config == GS_BULK_CONFIG_ID
606 && wIndex != GS_BULK_INTERFACE_ID)) {
607 ret = -EDOM;
608 break;
609 }
610 /* no alternate interface settings */
611 *(u8 *)req->buf = 0;
612 ret = min(wLength, (u16)1);
613 break;
614
615 default:
David Brownell00274922007-11-19 12:58:36 -0800616 pr_err("gs_setup: unknown standard request, type=%02x, "
617 "request=%02x, value=%04x, index=%04x, length=%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 ctrl->bRequestType, ctrl->bRequest,
619 wValue, wIndex, wLength);
620 break;
621 }
622
623 return ret;
624}
625
David Brownell9079e912008-05-07 16:00:36 -0700626static void gs_setup_complete_set_line_coding(struct usb_ep *ep,
627 struct usb_request *req)
628{
629 struct gs_dev *dev = ep->driver_data;
David Brownell9079e912008-05-07 16:00:36 -0700630
631 switch (req->status) {
632 case 0:
633 /* normal completion */
David Brownella7707ad2008-06-19 17:52:07 -0700634 if (req->actual != sizeof(dev->gser.port_line_coding))
David Brownell9079e912008-05-07 16:00:36 -0700635 usb_ep_set_halt(ep);
David Brownella7707ad2008-06-19 17:52:07 -0700636 else {
David Brownell9079e912008-05-07 16:00:36 -0700637 struct usb_cdc_line_coding *value = req->buf;
638
639 /* REVISIT: we currently just remember this data.
640 * If we change that, (a) validate it first, then
641 * (b) update whatever hardware needs updating.
642 */
David Brownella7707ad2008-06-19 17:52:07 -0700643 spin_lock(&dev->dev_lock);
644 dev->gser.port_line_coding = *value;
645 spin_unlock(&dev->dev_lock);
David Brownell9079e912008-05-07 16:00:36 -0700646 }
647 break;
648
649 case -ESHUTDOWN:
650 /* disconnect */
651 gs_free_req(ep, req);
652 break;
653
654 default:
655 /* unexpected */
656 break;
657 }
658 return;
659}
660
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661static int gs_setup_class(struct usb_gadget *gadget,
662 const struct usb_ctrlrequest *ctrl)
663{
664 int ret = -EOPNOTSUPP;
665 struct gs_dev *dev = get_gadget_data(gadget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 struct usb_request *req = dev->dev_ctrl_req;
David Brownell1bbc1692005-05-07 13:05:13 -0700667 u16 wIndex = le16_to_cpu(ctrl->wIndex);
668 u16 wValue = le16_to_cpu(ctrl->wValue);
669 u16 wLength = le16_to_cpu(ctrl->wLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
671 switch (ctrl->bRequest) {
672 case USB_CDC_REQ_SET_LINE_CODING:
David Brownellf371e752008-04-18 17:37:49 -0700673 if (wLength != sizeof(struct usb_cdc_line_coding))
674 break;
675 ret = wLength;
676 req->complete = gs_setup_complete_set_line_coding;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 break;
678
679 case USB_CDC_REQ_GET_LINE_CODING:
David Brownellf371e752008-04-18 17:37:49 -0700680 ret = min_t(int, wLength, sizeof(struct usb_cdc_line_coding));
David Brownella7707ad2008-06-19 17:52:07 -0700681 spin_lock(&dev->dev_lock);
682 memcpy(req->buf, &dev->gser.port_line_coding, ret);
683 spin_unlock(&dev->dev_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 break;
685
686 case USB_CDC_REQ_SET_CONTROL_LINE_STATE:
David Brownellf371e752008-04-18 17:37:49 -0700687 if (wLength != 0)
688 break;
689 ret = 0;
David Brownella7707ad2008-06-19 17:52:07 -0700690 /* REVISIT: we currently just remember this data.
691 * If we change that, update whatever hardware needs
692 * updating.
693 */
694 spin_lock(&dev->dev_lock);
695 dev->gser.port_handshake_bits = wValue;
696 spin_unlock(&dev->dev_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 break;
698
699 default:
David Brownellf371e752008-04-18 17:37:49 -0700700 /* NOTE: strictly speaking, we should accept AT-commands
701 * using SEND_ENCPSULATED_COMMAND/GET_ENCAPSULATED_RESPONSE.
702 * But our call management descriptor says we don't handle
703 * call management, so we should be able to get by without
704 * handling those "required" commands (except by stalling).
705 */
David Brownell00274922007-11-19 12:58:36 -0800706 pr_err("gs_setup: unknown class request, "
David Brownell49b4f902007-08-26 12:44:24 -0700707 "type=%02x, request=%02x, value=%04x, "
708 "index=%04x, length=%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 ctrl->bRequestType, ctrl->bRequest,
710 wValue, wIndex, wLength);
711 break;
712 }
713
714 return ret;
715}
716
717/*
718 * gs_setup_complete
719 */
720static void gs_setup_complete(struct usb_ep *ep, struct usb_request *req)
721{
722 if (req->status || req->actual != req->length) {
David Brownell00274922007-11-19 12:58:36 -0800723 pr_err("gs_setup_complete: status error, status=%d, "
724 "actual=%d, length=%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 req->status, req->actual, req->length);
726 }
727}
728
729/*
David Brownell9079e912008-05-07 16:00:36 -0700730 * gs_setup
731 *
732 * Implements all the control endpoint functionality that's not
733 * handled in hardware or the hardware driver.
734 *
735 * Returns the size of the data sent to the host, or a negative
736 * error number.
737 */
738static int gs_setup(struct usb_gadget *gadget,
739 const struct usb_ctrlrequest *ctrl)
740{
741 int ret = -EOPNOTSUPP;
742 struct gs_dev *dev = get_gadget_data(gadget);
743 struct usb_request *req = dev->dev_ctrl_req;
744 u16 wIndex = le16_to_cpu(ctrl->wIndex);
745 u16 wValue = le16_to_cpu(ctrl->wValue);
746 u16 wLength = le16_to_cpu(ctrl->wLength);
747
748 req->complete = gs_setup_complete;
749
750 switch (ctrl->bRequestType & USB_TYPE_MASK) {
751 case USB_TYPE_STANDARD:
752 ret = gs_setup_standard(gadget, ctrl);
753 break;
754
755 case USB_TYPE_CLASS:
756 ret = gs_setup_class(gadget, ctrl);
757 break;
758
759 default:
760 pr_err("gs_setup: unknown request, type=%02x, request=%02x, "
761 "value=%04x, index=%04x, length=%d\n",
762 ctrl->bRequestType, ctrl->bRequest,
763 wValue, wIndex, wLength);
764 break;
765 }
766
767 /* respond with data transfer before status phase? */
768 if (ret >= 0) {
769 req->length = ret;
770 req->zero = ret < wLength
771 && (ret % gadget->ep0->maxpacket) == 0;
772 ret = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
773 if (ret < 0) {
774 pr_err("gs_setup: cannot queue response, ret=%d\n",
775 ret);
776 req->status = 0;
777 gs_setup_complete(gadget->ep0, req);
778 }
779 }
780
781 /* device either stalls (ret < 0) or reports success */
782 return ret;
783}
784
785/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 * gs_disconnect
787 *
788 * Called when the device is disconnected. Frees the closed
789 * ports and disconnects open ports. Open ports will be freed
790 * on close. Then reallocates the ports for the next connection.
791 */
792static void gs_disconnect(struct usb_gadget *gadget)
793{
794 unsigned long flags;
795 struct gs_dev *dev = get_gadget_data(gadget);
796
797 spin_lock_irqsave(&dev->dev_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 gs_reset_config(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 spin_unlock_irqrestore(&dev->dev_lock, flags);
800
David Brownell00274922007-11-19 12:58:36 -0800801 pr_info("gs_disconnect: %s disconnected\n", GS_LONG_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802}
803
David Brownell9079e912008-05-07 16:00:36 -0700804static struct usb_gadget_driver gs_gadget_driver = {
805#ifdef CONFIG_USB_GADGET_DUALSPEED
806 .speed = USB_SPEED_HIGH,
807#else
808 .speed = USB_SPEED_FULL,
809#endif /* CONFIG_USB_GADGET_DUALSPEED */
810 .function = GS_LONG_NAME,
811 .bind = gs_bind,
812 .unbind = gs_unbind,
813 .setup = gs_setup,
814 .disconnect = gs_disconnect,
815 .driver = {
816 .name = GS_SHORT_NAME,
817 .owner = THIS_MODULE,
818 },
819};
820
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821/*
822 * gs_set_config
823 *
824 * Configures the device by enabling device specific
825 * optimizations, setting up the endpoints, allocating
826 * read and write requests and queuing read requests.
827 *
828 * The device lock must be held when calling this function.
829 */
830static int gs_set_config(struct gs_dev *dev, unsigned config)
831{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 int ret = 0;
833 struct usb_gadget *gadget = dev->dev_gadget;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
835 if (config == dev->dev_config)
836 return 0;
837
838 gs_reset_config(dev);
839
840 switch (config) {
841 case GS_NO_CONFIG_ID:
842 return 0;
843 case GS_BULK_CONFIG_ID:
844 if (use_acm)
845 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 break;
847 case GS_ACM_CONFIG_ID:
848 if (!use_acm)
849 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 break;
851 default:
852 return -EINVAL;
853 }
854
David Brownella7707ad2008-06-19 17:52:07 -0700855 dev->gser.in_desc = choose_ep_desc(gadget,
David Brownell734d37c2008-05-07 14:25:24 -0700856 &gs_highspeed_in_desc,
857 &gs_fullspeed_in_desc);
David Brownella7707ad2008-06-19 17:52:07 -0700858 dev->gser.out_desc = choose_ep_desc(gadget,
David Brownell734d37c2008-05-07 14:25:24 -0700859 &gs_highspeed_out_desc,
860 &gs_fullspeed_out_desc);
David Brownella7707ad2008-06-19 17:52:07 -0700861 dev->gser.notify_desc = dev->gser.notify
David Brownell734d37c2008-05-07 14:25:24 -0700862 ? choose_ep_desc(gadget,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 &gs_highspeed_notify_desc,
David Brownell734d37c2008-05-07 14:25:24 -0700864 &gs_fullspeed_notify_desc)
865 : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
David Brownella7707ad2008-06-19 17:52:07 -0700867 /* only support one "serial" port for now */
868 if (dev->gser.notify) {
869 ret = usb_ep_enable(dev->gser.notify, dev->gser.notify_desc);
870 if (ret < 0)
871 return ret;
872 dev->gser.notify->driver_data = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 }
874
David Brownella7707ad2008-06-19 17:52:07 -0700875 ret = gserial_connect(&dev->gser, 0);
876 if (ret < 0) {
877 if (dev->gser.notify) {
878 usb_ep_disable(dev->gser.notify);
879 dev->gser.notify->driver_data = NULL;
David Brownell734d37c2008-05-07 14:25:24 -0700880 }
David Brownella7707ad2008-06-19 17:52:07 -0700881 return ret;
David Brownell734d37c2008-05-07 14:25:24 -0700882 }
883
884 dev->dev_config = config;
885
David Brownellf371e752008-04-18 17:37:49 -0700886 /* REVISIT the ACM mode should be able to actually *issue* some
887 * notifications, for at least serial state change events if
888 * not also for network connection; say so in bmCapabilities.
889 */
890
David Brownell00274922007-11-19 12:58:36 -0800891 pr_info("gs_set_config: %s configured, %s speed %s config\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 GS_LONG_NAME,
893 gadget->speed == USB_SPEED_HIGH ? "high" : "full",
894 config == GS_BULK_CONFIG_ID ? "BULK" : "CDC-ACM");
895
896 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897}
898
899/*
900 * gs_reset_config
901 *
902 * Mark the device as not configured, disable all endpoints,
903 * which forces completion of pending I/O and frees queued
904 * requests, and free the remaining write requests on the
905 * free list.
906 *
907 * The device lock must be held when calling this function.
908 */
909static void gs_reset_config(struct gs_dev *dev)
910{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 if (dev->dev_config == GS_NO_CONFIG_ID)
912 return;
913
914 dev->dev_config = GS_NO_CONFIG_ID;
915
David Brownella7707ad2008-06-19 17:52:07 -0700916 gserial_disconnect(&dev->gser);
917 if (dev->gser.notify) {
918 usb_ep_disable(dev->gser.notify);
919 dev->gser.notify->driver_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921}
922
923/*
924 * gs_build_config_buf
925 *
926 * Builds the config descriptors in the given buffer and returns the
927 * length, or a negative error number.
928 */
David Brownell51a0e852007-08-02 00:02:47 -0700929static int gs_build_config_buf(u8 *buf, struct usb_gadget *g,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 u8 type, unsigned int index, int is_otg)
931{
932 int len;
David Brownell51a0e852007-08-02 00:02:47 -0700933 int high_speed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 const struct usb_config_descriptor *config_desc;
935 const struct usb_descriptor_header **function;
936
937 if (index >= gs_device_desc.bNumConfigurations)
938 return -EINVAL;
939
940 /* other speed switches high and full speed */
David Brownell51a0e852007-08-02 00:02:47 -0700941 if (gadget_is_dualspeed(g)) {
942 high_speed = (g->speed == USB_SPEED_HIGH);
943 if (type == USB_DT_OTHER_SPEED_CONFIG)
944 high_speed = !high_speed;
945 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
947 if (use_acm) {
948 config_desc = &gs_acm_config_desc;
David Brownell51a0e852007-08-02 00:02:47 -0700949 function = high_speed
950 ? gs_acm_highspeed_function
951 : gs_acm_fullspeed_function;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 } else {
953 config_desc = &gs_bulk_config_desc;
David Brownell51a0e852007-08-02 00:02:47 -0700954 function = high_speed
955 ? gs_bulk_highspeed_function
956 : gs_bulk_fullspeed_function;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 }
958
959 /* for now, don't advertise srp-only devices */
960 if (!is_otg)
961 function++;
962
963 len = usb_gadget_config_buf(config_desc, buf, GS_MAX_DESC_LEN, function);
964 if (len < 0)
965 return len;
966
967 ((struct usb_config_descriptor *)buf)->bDescriptorType = type;
968
969 return len;
970}
971
972/*
973 * gs_alloc_req
974 *
975 * Allocate a usb_request and its buffer. Returns a pointer to the
976 * usb_request or NULL if there is an error.
977 */
David Brownell1bbc1692005-05-07 13:05:13 -0700978static struct usb_request *
Al Viro55016f12005-10-21 03:21:58 -0400979gs_alloc_req(struct usb_ep *ep, unsigned int len, gfp_t kmalloc_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980{
981 struct usb_request *req;
982
983 if (ep == NULL)
984 return NULL;
985
986 req = usb_ep_alloc_request(ep, kmalloc_flags);
987
988 if (req != NULL) {
989 req->length = len;
990 req->buf = kmalloc(len, kmalloc_flags);
991 if (req->buf == NULL) {
992 usb_ep_free_request(ep, req);
993 return NULL;
994 }
995 }
996
997 return req;
998}
999
1000/*
1001 * gs_free_req
1002 *
1003 * Free a usb_request and its buffer.
1004 */
1005static void gs_free_req(struct usb_ep *ep, struct usb_request *req)
1006{
1007 if (ep != NULL && req != NULL) {
1008 kfree(req->buf);
1009 usb_ep_free_request(ep, req);
1010 }
1011}
1012
David Brownell9079e912008-05-07 16:00:36 -07001013/*-------------------------------------------------------------------------*/
1014
David Brownell9079e912008-05-07 16:00:36 -07001015/*
1016 * gs_module_init
1017 *
1018 * Register as a USB gadget driver and a tty driver.
1019 */
1020static int __init gs_module_init(void)
1021{
David Brownella7707ad2008-06-19 17:52:07 -07001022 return usb_gadget_register_driver(&gs_gadget_driver);
David Brownell9079e912008-05-07 16:00:36 -07001023}
1024module_init(gs_module_init);
1025
1026/*
1027 * gs_module_exit
1028 *
1029 * Unregister as a tty driver and a USB gadget driver.
1030 */
1031static void __exit gs_module_exit(void)
1032{
David Brownell9079e912008-05-07 16:00:36 -07001033 usb_gadget_unregister_driver(&gs_gadget_driver);
David Brownell9079e912008-05-07 16:00:36 -07001034}
1035module_exit(gs_module_exit);