scsi: Fix up files implicitly depending on module.h inclusion
[deliverable/linux.git] / drivers / usb / gadget / f_obex.c
CommitLineData
3086775a
FB
1/*
2 * f_obex.c -- USB CDC OBEX function driver
3 *
4 * Copyright (C) 2008 Nokia Corporation
5 * Contact: Felipe Balbi <felipe.balbi@nokia.com>
6 *
7 * Based on f_acm.c by Al Borchers and David Brownell.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
3086775a
FB
13 */
14
15/* #define VERBOSE_DEBUG */
16
5a0e3ad6 17#include <linux/slab.h>
3086775a 18#include <linux/kernel.h>
3086775a
FB
19#include <linux/device.h>
20
21#include "u_serial.h"
22#include "gadget_chips.h"
23
24
25/*
26 * This CDC OBEX function support just packages a TTY-ish byte stream.
27 * A user mode server will put it into "raw" mode and handle all the
28 * relevant protocol details ... this is just a kernel passthrough.
21214278
DB
29 * When possible, we prevent gadget enumeration until that server is
30 * ready to handle the commands.
3086775a
FB
31 */
32
3086775a
FB
33struct f_obex {
34 struct gserial port;
35 u8 ctrl_id;
36 u8 data_id;
37 u8 port_num;
21214278 38 u8 can_activate;
3086775a
FB
39};
40
41static inline struct f_obex *func_to_obex(struct usb_function *f)
42{
43 return container_of(f, struct f_obex, port.func);
44}
45
21214278
DB
46static inline struct f_obex *port_to_obex(struct gserial *p)
47{
48 return container_of(p, struct f_obex, port);
49}
50
3086775a
FB
51/*-------------------------------------------------------------------------*/
52
53#define OBEX_CTRL_IDX 0
54#define OBEX_DATA_IDX 1
55
56static struct usb_string obex_string_defs[] = {
57 [OBEX_CTRL_IDX].s = "CDC Object Exchange (OBEX)",
58 [OBEX_DATA_IDX].s = "CDC OBEX Data",
59 { }, /* end of list */
60};
61
62static struct usb_gadget_strings obex_string_table = {
63 .language = 0x0409, /* en-US */
64 .strings = obex_string_defs,
65};
66
67static struct usb_gadget_strings *obex_strings[] = {
68 &obex_string_table,
69 NULL,
70};
71
72/*-------------------------------------------------------------------------*/
73
74static struct usb_interface_descriptor obex_control_intf __initdata = {
75 .bLength = sizeof(obex_control_intf),
76 .bDescriptorType = USB_DT_INTERFACE,
77 .bInterfaceNumber = 0,
78
79 .bAlternateSetting = 0,
80 .bNumEndpoints = 0,
81 .bInterfaceClass = USB_CLASS_COMM,
82 .bInterfaceSubClass = USB_CDC_SUBCLASS_OBEX,
83};
84
85static struct usb_interface_descriptor obex_data_nop_intf __initdata = {
86 .bLength = sizeof(obex_data_nop_intf),
87 .bDescriptorType = USB_DT_INTERFACE,
88 .bInterfaceNumber = 1,
89
90 .bAlternateSetting = 0,
91 .bNumEndpoints = 0,
92 .bInterfaceClass = USB_CLASS_CDC_DATA,
93};
94
95static struct usb_interface_descriptor obex_data_intf __initdata = {
96 .bLength = sizeof(obex_data_intf),
97 .bDescriptorType = USB_DT_INTERFACE,
98 .bInterfaceNumber = 2,
99
100 .bAlternateSetting = 1,
101 .bNumEndpoints = 2,
102 .bInterfaceClass = USB_CLASS_CDC_DATA,
103};
104
105static struct usb_cdc_header_desc obex_cdc_header_desc __initdata = {
106 .bLength = sizeof(obex_cdc_header_desc),
107 .bDescriptorType = USB_DT_CS_INTERFACE,
108 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
551509d2 109 .bcdCDC = cpu_to_le16(0x0120),
3086775a
FB
110};
111
112static struct usb_cdc_union_desc obex_cdc_union_desc __initdata = {
113 .bLength = sizeof(obex_cdc_union_desc),
114 .bDescriptorType = USB_DT_CS_INTERFACE,
115 .bDescriptorSubType = USB_CDC_UNION_TYPE,
116 .bMasterInterface0 = 1,
117 .bSlaveInterface0 = 2,
118};
119
120static struct usb_cdc_obex_desc obex_desc __initdata = {
121 .bLength = sizeof(obex_desc),
122 .bDescriptorType = USB_DT_CS_INTERFACE,
123 .bDescriptorSubType = USB_CDC_OBEX_TYPE,
551509d2 124 .bcdVersion = cpu_to_le16(0x0100),
3086775a
FB
125};
126
127/* High-Speed Support */
128
129static struct usb_endpoint_descriptor obex_hs_ep_out_desc __initdata = {
130 .bLength = USB_DT_ENDPOINT_SIZE,
131 .bDescriptorType = USB_DT_ENDPOINT,
132
133 .bEndpointAddress = USB_DIR_OUT,
134 .bmAttributes = USB_ENDPOINT_XFER_BULK,
551509d2 135 .wMaxPacketSize = cpu_to_le16(512),
3086775a
FB
136};
137
138static struct usb_endpoint_descriptor obex_hs_ep_in_desc __initdata = {
139 .bLength = USB_DT_ENDPOINT_SIZE,
140 .bDescriptorType = USB_DT_ENDPOINT,
141
142 .bEndpointAddress = USB_DIR_IN,
143 .bmAttributes = USB_ENDPOINT_XFER_BULK,
551509d2 144 .wMaxPacketSize = cpu_to_le16(512),
3086775a
FB
145};
146
147static struct usb_descriptor_header *hs_function[] __initdata = {
148 (struct usb_descriptor_header *) &obex_control_intf,
149 (struct usb_descriptor_header *) &obex_cdc_header_desc,
150 (struct usb_descriptor_header *) &obex_desc,
151 (struct usb_descriptor_header *) &obex_cdc_union_desc,
152
153 (struct usb_descriptor_header *) &obex_data_nop_intf,
154 (struct usb_descriptor_header *) &obex_data_intf,
155 (struct usb_descriptor_header *) &obex_hs_ep_in_desc,
156 (struct usb_descriptor_header *) &obex_hs_ep_out_desc,
157 NULL,
158};
159
160/* Full-Speed Support */
161
162static struct usb_endpoint_descriptor obex_fs_ep_in_desc __initdata = {
163 .bLength = USB_DT_ENDPOINT_SIZE,
164 .bDescriptorType = USB_DT_ENDPOINT,
165
166 .bEndpointAddress = USB_DIR_IN,
167 .bmAttributes = USB_ENDPOINT_XFER_BULK,
168};
169
170static struct usb_endpoint_descriptor obex_fs_ep_out_desc __initdata = {
171 .bLength = USB_DT_ENDPOINT_SIZE,
172 .bDescriptorType = USB_DT_ENDPOINT,
173
174 .bEndpointAddress = USB_DIR_OUT,
175 .bmAttributes = USB_ENDPOINT_XFER_BULK,
176};
177
178static struct usb_descriptor_header *fs_function[] __initdata = {
179 (struct usb_descriptor_header *) &obex_control_intf,
180 (struct usb_descriptor_header *) &obex_cdc_header_desc,
181 (struct usb_descriptor_header *) &obex_desc,
182 (struct usb_descriptor_header *) &obex_cdc_union_desc,
183
184 (struct usb_descriptor_header *) &obex_data_nop_intf,
185 (struct usb_descriptor_header *) &obex_data_intf,
186 (struct usb_descriptor_header *) &obex_fs_ep_in_desc,
187 (struct usb_descriptor_header *) &obex_fs_ep_out_desc,
188 NULL,
189};
190
191/*-------------------------------------------------------------------------*/
192
193static int obex_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
194{
195 struct f_obex *obex = func_to_obex(f);
196 struct usb_composite_dev *cdev = f->config->cdev;
197
198 if (intf == obex->ctrl_id) {
199 if (alt != 0)
200 goto fail;
201 /* NOP */
202 DBG(cdev, "reset obex ttyGS%d control\n", obex->port_num);
203
204 } else if (intf == obex->data_id) {
205 if (alt > 1)
206 goto fail;
207
208 if (obex->port.in->driver_data) {
209 DBG(cdev, "reset obex ttyGS%d\n", obex->port_num);
210 gserial_disconnect(&obex->port);
211 }
212
ea2a1df7 213 if (!obex->port.in->desc || !obex->port.out->desc) {
3086775a 214 DBG(cdev, "init obex ttyGS%d\n", obex->port_num);
ea2a1df7
TB
215 if (config_ep_by_speed(cdev->gadget, f,
216 obex->port.in) ||
217 config_ep_by_speed(cdev->gadget, f,
218 obex->port.out)) {
219 obex->port.out->desc = NULL;
220 obex->port.in->desc = NULL;
221 goto fail;
222 }
3086775a
FB
223 }
224
225 if (alt == 1) {
226 DBG(cdev, "activate obex ttyGS%d\n", obex->port_num);
227 gserial_connect(&obex->port, obex->port_num);
228 }
229
230 } else
231 goto fail;
232
233 return 0;
234
235fail:
236 return -EINVAL;
237}
238
239static int obex_get_alt(struct usb_function *f, unsigned intf)
240{
241 struct f_obex *obex = func_to_obex(f);
242
243 if (intf == obex->ctrl_id)
244 return 0;
245
246 return obex->port.in->driver_data ? 1 : 0;
247}
248
249static void obex_disable(struct usb_function *f)
250{
251 struct f_obex *obex = func_to_obex(f);
252 struct usb_composite_dev *cdev = f->config->cdev;
253
254 DBG(cdev, "obex ttyGS%d disable\n", obex->port_num);
255 gserial_disconnect(&obex->port);
256}
257
258/*-------------------------------------------------------------------------*/
259
21214278
DB
260static void obex_connect(struct gserial *g)
261{
262 struct f_obex *obex = port_to_obex(g);
263 struct usb_composite_dev *cdev = g->func.config->cdev;
264 int status;
265
266 if (!obex->can_activate)
267 return;
268
269 status = usb_function_activate(&g->func);
270 if (status)
271 DBG(cdev, "obex ttyGS%d function activate --> %d\n",
272 obex->port_num, status);
273}
274
275static void obex_disconnect(struct gserial *g)
276{
277 struct f_obex *obex = port_to_obex(g);
278 struct usb_composite_dev *cdev = g->func.config->cdev;
279 int status;
280
281 if (!obex->can_activate)
282 return;
283
284 status = usb_function_deactivate(&g->func);
285 if (status)
286 DBG(cdev, "obex ttyGS%d function deactivate --> %d\n",
287 obex->port_num, status);
288}
289
290/*-------------------------------------------------------------------------*/
291
3086775a
FB
292static int __init
293obex_bind(struct usb_configuration *c, struct usb_function *f)
294{
295 struct usb_composite_dev *cdev = c->cdev;
296 struct f_obex *obex = func_to_obex(f);
297 int status;
298 struct usb_ep *ep;
299
300 /* allocate instance-specific interface IDs, and patch descriptors */
301
302 status = usb_interface_id(c, f);
303 if (status < 0)
304 goto fail;
305 obex->ctrl_id = status;
306
307 obex_control_intf.bInterfaceNumber = status;
308 obex_cdc_union_desc.bMasterInterface0 = status;
309
310 status = usb_interface_id(c, f);
311 if (status < 0)
312 goto fail;
313 obex->data_id = status;
314
315 obex_data_nop_intf.bInterfaceNumber = status;
316 obex_data_intf.bInterfaceNumber = status;
317 obex_cdc_union_desc.bSlaveInterface0 = status;
318
319 /* allocate instance-specific endpoints */
320
321 ep = usb_ep_autoconfig(cdev->gadget, &obex_fs_ep_in_desc);
322 if (!ep)
323 goto fail;
324 obex->port.in = ep;
325 ep->driver_data = cdev; /* claim */
326
327 ep = usb_ep_autoconfig(cdev->gadget, &obex_fs_ep_out_desc);
328 if (!ep)
329 goto fail;
330 obex->port.out = ep;
331 ep->driver_data = cdev; /* claim */
332
333 /* copy descriptors, and track endpoint copies */
334 f->descriptors = usb_copy_descriptors(fs_function);
335
3086775a
FB
336 /* support all relevant hardware speeds... we expect that when
337 * hardware is dual speed, all bulk-capable endpoints work at
338 * both speeds
339 */
340 if (gadget_is_dualspeed(c->cdev->gadget)) {
341
342 obex_hs_ep_in_desc.bEndpointAddress =
343 obex_fs_ep_in_desc.bEndpointAddress;
344 obex_hs_ep_out_desc.bEndpointAddress =
345 obex_fs_ep_out_desc.bEndpointAddress;
346
347 /* copy descriptors, and track endpoint copies */
348 f->hs_descriptors = usb_copy_descriptors(hs_function);
3086775a
FB
349 }
350
21214278
DB
351 /* Avoid letting this gadget enumerate until the userspace
352 * OBEX server is active.
353 */
354 status = usb_function_deactivate(f);
355 if (status < 0)
356 WARNING(cdev, "obex ttyGS%d: can't prevent enumeration, %d\n",
357 obex->port_num, status);
358 else
359 obex->can_activate = true;
360
361
3086775a
FB
362 DBG(cdev, "obex ttyGS%d: %s speed IN/%s OUT/%s\n",
363 obex->port_num,
364 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
365 obex->port.in->name, obex->port.out->name);
366
367 return 0;
368
369fail:
370 /* we might as well release our claims on endpoints */
371 if (obex->port.out)
372 obex->port.out->driver_data = NULL;
373 if (obex->port.in)
374 obex->port.in->driver_data = NULL;
375
376 ERROR(cdev, "%s/%p: can't bind, err %d\n", f->name, f, status);
377
378 return status;
379}
380
381static void
382obex_unbind(struct usb_configuration *c, struct usb_function *f)
383{
384 if (gadget_is_dualspeed(c->cdev->gadget))
385 usb_free_descriptors(f->hs_descriptors);
386 usb_free_descriptors(f->descriptors);
387 kfree(func_to_obex(f));
388}
389
390/* Some controllers can't support CDC OBEX ... */
391static inline bool can_support_obex(struct usb_configuration *c)
392{
393 /* Since the first interface is a NOP, we can ignore the
394 * issue of multi-interface support on most controllers.
395 *
396 * Altsettings are mandatory, however...
397 */
398 if (!gadget_supports_altsettings(c->cdev->gadget))
399 return false;
400
401 /* everything else is *probably* fine ... */
402 return true;
403}
404
405/**
406 * obex_bind_config - add a CDC OBEX function to a configuration
407 * @c: the configuration to support the CDC OBEX instance
408 * @port_num: /dev/ttyGS* port this interface will use
409 * Context: single threaded during gadget setup
410 *
411 * Returns zero on success, else negative errno.
412 *
413 * Caller must have called @gserial_setup() with enough ports to
414 * handle all the ones it binds. Caller is also responsible
415 * for calling @gserial_cleanup() before module unload.
416 */
417int __init obex_bind_config(struct usb_configuration *c, u8 port_num)
418{
419 struct f_obex *obex;
420 int status;
421
422 if (!can_support_obex(c))
423 return -EINVAL;
424
425 /* maybe allocate device-global string IDs, and patch descriptors */
426 if (obex_string_defs[OBEX_CTRL_IDX].id == 0) {
427 status = usb_string_id(c->cdev);
428 if (status < 0)
429 return status;
430 obex_string_defs[OBEX_CTRL_IDX].id = status;
431
432 obex_control_intf.iInterface = status;
433
434 status = usb_string_id(c->cdev);
435 if (status < 0)
436 return status;
437 obex_string_defs[OBEX_DATA_IDX].id = status;
438
439 obex_data_nop_intf.iInterface =
440 obex_data_intf.iInterface = status;
441 }
442
443 /* allocate and initialize one new instance */
444 obex = kzalloc(sizeof *obex, GFP_KERNEL);
445 if (!obex)
446 return -ENOMEM;
447
448 obex->port_num = port_num;
449
21214278
DB
450 obex->port.connect = obex_connect;
451 obex->port.disconnect = obex_disconnect;
452
3086775a
FB
453 obex->port.func.name = "obex";
454 obex->port.func.strings = obex_strings;
455 /* descriptors are per-instance copies */
456 obex->port.func.bind = obex_bind;
457 obex->port.func.unbind = obex_unbind;
458 obex->port.func.set_alt = obex_set_alt;
459 obex->port.func.get_alt = obex_get_alt;
460 obex->port.func.disable = obex_disable;
461
462 status = usb_add_function(c, &obex->port.func);
463 if (status)
464 kfree(obex);
465
466 return status;
467}
468
469MODULE_AUTHOR("Felipe Balbi");
470MODULE_LICENSE("GPL");
This page took 0.268105 seconds and 5 git commands to generate.