Add linux-next specific files for 20110831
[linux-2.6/next.git] / drivers / usb / gadget / f_obex.c
blobd13dbaa86ed35f7694da05dbd157f5587ea0aecd
1 /*
2 * f_obex.c -- USB CDC OBEX function driver
4 * Copyright (C) 2008 Nokia Corporation
5 * Contact: Felipe Balbi <felipe.balbi@nokia.com>
7 * Based on f_acm.c by Al Borchers and David Brownell.
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.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 /* #define VERBOSE_DEBUG */
26 #include <linux/slab.h>
27 #include <linux/kernel.h>
28 #include <linux/device.h>
29 #include <linux/module.h>
31 #include "u_serial.h"
32 #include "gadget_chips.h"
36 * This CDC OBEX function support just packages a TTY-ish byte stream.
37 * A user mode server will put it into "raw" mode and handle all the
38 * relevant protocol details ... this is just a kernel passthrough.
39 * When possible, we prevent gadget enumeration until that server is
40 * ready to handle the commands.
43 struct f_obex {
44 struct gserial port;
45 u8 ctrl_id;
46 u8 data_id;
47 u8 port_num;
48 u8 can_activate;
51 static inline struct f_obex *func_to_obex(struct usb_function *f)
53 return container_of(f, struct f_obex, port.func);
56 static inline struct f_obex *port_to_obex(struct gserial *p)
58 return container_of(p, struct f_obex, port);
61 /*-------------------------------------------------------------------------*/
63 #define OBEX_CTRL_IDX 0
64 #define OBEX_DATA_IDX 1
66 static struct usb_string obex_string_defs[] = {
67 [OBEX_CTRL_IDX].s = "CDC Object Exchange (OBEX)",
68 [OBEX_DATA_IDX].s = "CDC OBEX Data",
69 { }, /* end of list */
72 static struct usb_gadget_strings obex_string_table = {
73 .language = 0x0409, /* en-US */
74 .strings = obex_string_defs,
77 static struct usb_gadget_strings *obex_strings[] = {
78 &obex_string_table,
79 NULL,
82 /*-------------------------------------------------------------------------*/
84 static struct usb_interface_descriptor obex_control_intf __initdata = {
85 .bLength = sizeof(obex_control_intf),
86 .bDescriptorType = USB_DT_INTERFACE,
87 .bInterfaceNumber = 0,
89 .bAlternateSetting = 0,
90 .bNumEndpoints = 0,
91 .bInterfaceClass = USB_CLASS_COMM,
92 .bInterfaceSubClass = USB_CDC_SUBCLASS_OBEX,
95 static struct usb_interface_descriptor obex_data_nop_intf __initdata = {
96 .bLength = sizeof(obex_data_nop_intf),
97 .bDescriptorType = USB_DT_INTERFACE,
98 .bInterfaceNumber = 1,
100 .bAlternateSetting = 0,
101 .bNumEndpoints = 0,
102 .bInterfaceClass = USB_CLASS_CDC_DATA,
105 static struct usb_interface_descriptor obex_data_intf __initdata = {
106 .bLength = sizeof(obex_data_intf),
107 .bDescriptorType = USB_DT_INTERFACE,
108 .bInterfaceNumber = 2,
110 .bAlternateSetting = 1,
111 .bNumEndpoints = 2,
112 .bInterfaceClass = USB_CLASS_CDC_DATA,
115 static struct usb_cdc_header_desc obex_cdc_header_desc __initdata = {
116 .bLength = sizeof(obex_cdc_header_desc),
117 .bDescriptorType = USB_DT_CS_INTERFACE,
118 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
119 .bcdCDC = cpu_to_le16(0x0120),
122 static struct usb_cdc_union_desc obex_cdc_union_desc __initdata = {
123 .bLength = sizeof(obex_cdc_union_desc),
124 .bDescriptorType = USB_DT_CS_INTERFACE,
125 .bDescriptorSubType = USB_CDC_UNION_TYPE,
126 .bMasterInterface0 = 1,
127 .bSlaveInterface0 = 2,
130 static struct usb_cdc_obex_desc obex_desc __initdata = {
131 .bLength = sizeof(obex_desc),
132 .bDescriptorType = USB_DT_CS_INTERFACE,
133 .bDescriptorSubType = USB_CDC_OBEX_TYPE,
134 .bcdVersion = cpu_to_le16(0x0100),
137 /* High-Speed Support */
139 static struct usb_endpoint_descriptor obex_hs_ep_out_desc __initdata = {
140 .bLength = USB_DT_ENDPOINT_SIZE,
141 .bDescriptorType = USB_DT_ENDPOINT,
143 .bEndpointAddress = USB_DIR_OUT,
144 .bmAttributes = USB_ENDPOINT_XFER_BULK,
145 .wMaxPacketSize = cpu_to_le16(512),
148 static struct usb_endpoint_descriptor obex_hs_ep_in_desc __initdata = {
149 .bLength = USB_DT_ENDPOINT_SIZE,
150 .bDescriptorType = USB_DT_ENDPOINT,
152 .bEndpointAddress = USB_DIR_IN,
153 .bmAttributes = USB_ENDPOINT_XFER_BULK,
154 .wMaxPacketSize = cpu_to_le16(512),
157 static struct usb_descriptor_header *hs_function[] __initdata = {
158 (struct usb_descriptor_header *) &obex_control_intf,
159 (struct usb_descriptor_header *) &obex_cdc_header_desc,
160 (struct usb_descriptor_header *) &obex_desc,
161 (struct usb_descriptor_header *) &obex_cdc_union_desc,
163 (struct usb_descriptor_header *) &obex_data_nop_intf,
164 (struct usb_descriptor_header *) &obex_data_intf,
165 (struct usb_descriptor_header *) &obex_hs_ep_in_desc,
166 (struct usb_descriptor_header *) &obex_hs_ep_out_desc,
167 NULL,
170 /* Full-Speed Support */
172 static struct usb_endpoint_descriptor obex_fs_ep_in_desc __initdata = {
173 .bLength = USB_DT_ENDPOINT_SIZE,
174 .bDescriptorType = USB_DT_ENDPOINT,
176 .bEndpointAddress = USB_DIR_IN,
177 .bmAttributes = USB_ENDPOINT_XFER_BULK,
180 static struct usb_endpoint_descriptor obex_fs_ep_out_desc __initdata = {
181 .bLength = USB_DT_ENDPOINT_SIZE,
182 .bDescriptorType = USB_DT_ENDPOINT,
184 .bEndpointAddress = USB_DIR_OUT,
185 .bmAttributes = USB_ENDPOINT_XFER_BULK,
188 static struct usb_descriptor_header *fs_function[] __initdata = {
189 (struct usb_descriptor_header *) &obex_control_intf,
190 (struct usb_descriptor_header *) &obex_cdc_header_desc,
191 (struct usb_descriptor_header *) &obex_desc,
192 (struct usb_descriptor_header *) &obex_cdc_union_desc,
194 (struct usb_descriptor_header *) &obex_data_nop_intf,
195 (struct usb_descriptor_header *) &obex_data_intf,
196 (struct usb_descriptor_header *) &obex_fs_ep_in_desc,
197 (struct usb_descriptor_header *) &obex_fs_ep_out_desc,
198 NULL,
201 /*-------------------------------------------------------------------------*/
203 static int obex_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
205 struct f_obex *obex = func_to_obex(f);
206 struct usb_composite_dev *cdev = f->config->cdev;
208 if (intf == obex->ctrl_id) {
209 if (alt != 0)
210 goto fail;
211 /* NOP */
212 DBG(cdev, "reset obex ttyGS%d control\n", obex->port_num);
214 } else if (intf == obex->data_id) {
215 if (alt > 1)
216 goto fail;
218 if (obex->port.in->driver_data) {
219 DBG(cdev, "reset obex ttyGS%d\n", obex->port_num);
220 gserial_disconnect(&obex->port);
223 if (!obex->port.in->desc || !obex->port.out->desc) {
224 DBG(cdev, "init obex ttyGS%d\n", obex->port_num);
225 if (config_ep_by_speed(cdev->gadget, f,
226 obex->port.in) ||
227 config_ep_by_speed(cdev->gadget, f,
228 obex->port.out)) {
229 obex->port.out->desc = NULL;
230 obex->port.in->desc = NULL;
231 goto fail;
235 if (alt == 1) {
236 DBG(cdev, "activate obex ttyGS%d\n", obex->port_num);
237 gserial_connect(&obex->port, obex->port_num);
240 } else
241 goto fail;
243 return 0;
245 fail:
246 return -EINVAL;
249 static int obex_get_alt(struct usb_function *f, unsigned intf)
251 struct f_obex *obex = func_to_obex(f);
253 if (intf == obex->ctrl_id)
254 return 0;
256 return obex->port.in->driver_data ? 1 : 0;
259 static void obex_disable(struct usb_function *f)
261 struct f_obex *obex = func_to_obex(f);
262 struct usb_composite_dev *cdev = f->config->cdev;
264 DBG(cdev, "obex ttyGS%d disable\n", obex->port_num);
265 gserial_disconnect(&obex->port);
268 /*-------------------------------------------------------------------------*/
270 static void obex_connect(struct gserial *g)
272 struct f_obex *obex = port_to_obex(g);
273 struct usb_composite_dev *cdev = g->func.config->cdev;
274 int status;
276 if (!obex->can_activate)
277 return;
279 status = usb_function_activate(&g->func);
280 if (status)
281 DBG(cdev, "obex ttyGS%d function activate --> %d\n",
282 obex->port_num, status);
285 static void obex_disconnect(struct gserial *g)
287 struct f_obex *obex = port_to_obex(g);
288 struct usb_composite_dev *cdev = g->func.config->cdev;
289 int status;
291 if (!obex->can_activate)
292 return;
294 status = usb_function_deactivate(&g->func);
295 if (status)
296 DBG(cdev, "obex ttyGS%d function deactivate --> %d\n",
297 obex->port_num, status);
300 /*-------------------------------------------------------------------------*/
302 static int __init
303 obex_bind(struct usb_configuration *c, struct usb_function *f)
305 struct usb_composite_dev *cdev = c->cdev;
306 struct f_obex *obex = func_to_obex(f);
307 int status;
308 struct usb_ep *ep;
310 /* allocate instance-specific interface IDs, and patch descriptors */
312 status = usb_interface_id(c, f);
313 if (status < 0)
314 goto fail;
315 obex->ctrl_id = status;
317 obex_control_intf.bInterfaceNumber = status;
318 obex_cdc_union_desc.bMasterInterface0 = status;
320 status = usb_interface_id(c, f);
321 if (status < 0)
322 goto fail;
323 obex->data_id = status;
325 obex_data_nop_intf.bInterfaceNumber = status;
326 obex_data_intf.bInterfaceNumber = status;
327 obex_cdc_union_desc.bSlaveInterface0 = status;
329 /* allocate instance-specific endpoints */
331 ep = usb_ep_autoconfig(cdev->gadget, &obex_fs_ep_in_desc);
332 if (!ep)
333 goto fail;
334 obex->port.in = ep;
335 ep->driver_data = cdev; /* claim */
337 ep = usb_ep_autoconfig(cdev->gadget, &obex_fs_ep_out_desc);
338 if (!ep)
339 goto fail;
340 obex->port.out = ep;
341 ep->driver_data = cdev; /* claim */
343 /* copy descriptors, and track endpoint copies */
344 f->descriptors = usb_copy_descriptors(fs_function);
346 /* support all relevant hardware speeds... we expect that when
347 * hardware is dual speed, all bulk-capable endpoints work at
348 * both speeds
350 if (gadget_is_dualspeed(c->cdev->gadget)) {
352 obex_hs_ep_in_desc.bEndpointAddress =
353 obex_fs_ep_in_desc.bEndpointAddress;
354 obex_hs_ep_out_desc.bEndpointAddress =
355 obex_fs_ep_out_desc.bEndpointAddress;
357 /* copy descriptors, and track endpoint copies */
358 f->hs_descriptors = usb_copy_descriptors(hs_function);
361 /* Avoid letting this gadget enumerate until the userspace
362 * OBEX server is active.
364 status = usb_function_deactivate(f);
365 if (status < 0)
366 WARNING(cdev, "obex ttyGS%d: can't prevent enumeration, %d\n",
367 obex->port_num, status);
368 else
369 obex->can_activate = true;
372 DBG(cdev, "obex ttyGS%d: %s speed IN/%s OUT/%s\n",
373 obex->port_num,
374 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
375 obex->port.in->name, obex->port.out->name);
377 return 0;
379 fail:
380 /* we might as well release our claims on endpoints */
381 if (obex->port.out)
382 obex->port.out->driver_data = NULL;
383 if (obex->port.in)
384 obex->port.in->driver_data = NULL;
386 ERROR(cdev, "%s/%p: can't bind, err %d\n", f->name, f, status);
388 return status;
391 static void
392 obex_unbind(struct usb_configuration *c, struct usb_function *f)
394 if (gadget_is_dualspeed(c->cdev->gadget))
395 usb_free_descriptors(f->hs_descriptors);
396 usb_free_descriptors(f->descriptors);
397 kfree(func_to_obex(f));
400 /* Some controllers can't support CDC OBEX ... */
401 static inline bool can_support_obex(struct usb_configuration *c)
403 /* Since the first interface is a NOP, we can ignore the
404 * issue of multi-interface support on most controllers.
406 * Altsettings are mandatory, however...
408 if (!gadget_supports_altsettings(c->cdev->gadget))
409 return false;
411 /* everything else is *probably* fine ... */
412 return true;
416 * obex_bind_config - add a CDC OBEX function to a configuration
417 * @c: the configuration to support the CDC OBEX instance
418 * @port_num: /dev/ttyGS* port this interface will use
419 * Context: single threaded during gadget setup
421 * Returns zero on success, else negative errno.
423 * Caller must have called @gserial_setup() with enough ports to
424 * handle all the ones it binds. Caller is also responsible
425 * for calling @gserial_cleanup() before module unload.
427 int __init obex_bind_config(struct usb_configuration *c, u8 port_num)
429 struct f_obex *obex;
430 int status;
432 if (!can_support_obex(c))
433 return -EINVAL;
435 /* maybe allocate device-global string IDs, and patch descriptors */
436 if (obex_string_defs[OBEX_CTRL_IDX].id == 0) {
437 status = usb_string_id(c->cdev);
438 if (status < 0)
439 return status;
440 obex_string_defs[OBEX_CTRL_IDX].id = status;
442 obex_control_intf.iInterface = status;
444 status = usb_string_id(c->cdev);
445 if (status < 0)
446 return status;
447 obex_string_defs[OBEX_DATA_IDX].id = status;
449 obex_data_nop_intf.iInterface =
450 obex_data_intf.iInterface = status;
453 /* allocate and initialize one new instance */
454 obex = kzalloc(sizeof *obex, GFP_KERNEL);
455 if (!obex)
456 return -ENOMEM;
458 obex->port_num = port_num;
460 obex->port.connect = obex_connect;
461 obex->port.disconnect = obex_disconnect;
463 obex->port.func.name = "obex";
464 obex->port.func.strings = obex_strings;
465 /* descriptors are per-instance copies */
466 obex->port.func.bind = obex_bind;
467 obex->port.func.unbind = obex_unbind;
468 obex->port.func.set_alt = obex_set_alt;
469 obex->port.func.get_alt = obex_get_alt;
470 obex->port.func.disable = obex_disable;
472 status = usb_add_function(c, &obex->port.func);
473 if (status)
474 kfree(obex);
476 return status;
479 MODULE_AUTHOR("Felipe Balbi");
480 MODULE_LICENSE("GPL");