Merge remote-tracking branch 'driver-core/driver-core-next'
[linux-2.6/next.git] / drivers / usb / gadget / f_obex.c
blob394502abeb964a330130f981f0e9afd80d726930
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>
30 #include "u_serial.h"
31 #include "gadget_chips.h"
35 * This CDC OBEX function support just packages a TTY-ish byte stream.
36 * A user mode server will put it into "raw" mode and handle all the
37 * relevant protocol details ... this is just a kernel passthrough.
38 * When possible, we prevent gadget enumeration until that server is
39 * ready to handle the commands.
42 struct f_obex {
43 struct gserial port;
44 u8 ctrl_id;
45 u8 data_id;
46 u8 port_num;
47 u8 can_activate;
50 static inline struct f_obex *func_to_obex(struct usb_function *f)
52 return container_of(f, struct f_obex, port.func);
55 static inline struct f_obex *port_to_obex(struct gserial *p)
57 return container_of(p, struct f_obex, port);
60 /*-------------------------------------------------------------------------*/
62 #define OBEX_CTRL_IDX 0
63 #define OBEX_DATA_IDX 1
65 static struct usb_string obex_string_defs[] = {
66 [OBEX_CTRL_IDX].s = "CDC Object Exchange (OBEX)",
67 [OBEX_DATA_IDX].s = "CDC OBEX Data",
68 { }, /* end of list */
71 static struct usb_gadget_strings obex_string_table = {
72 .language = 0x0409, /* en-US */
73 .strings = obex_string_defs,
76 static struct usb_gadget_strings *obex_strings[] = {
77 &obex_string_table,
78 NULL,
81 /*-------------------------------------------------------------------------*/
83 static struct usb_interface_descriptor obex_control_intf __initdata = {
84 .bLength = sizeof(obex_control_intf),
85 .bDescriptorType = USB_DT_INTERFACE,
86 .bInterfaceNumber = 0,
88 .bAlternateSetting = 0,
89 .bNumEndpoints = 0,
90 .bInterfaceClass = USB_CLASS_COMM,
91 .bInterfaceSubClass = USB_CDC_SUBCLASS_OBEX,
94 static struct usb_interface_descriptor obex_data_nop_intf __initdata = {
95 .bLength = sizeof(obex_data_nop_intf),
96 .bDescriptorType = USB_DT_INTERFACE,
97 .bInterfaceNumber = 1,
99 .bAlternateSetting = 0,
100 .bNumEndpoints = 0,
101 .bInterfaceClass = USB_CLASS_CDC_DATA,
104 static struct usb_interface_descriptor obex_data_intf __initdata = {
105 .bLength = sizeof(obex_data_intf),
106 .bDescriptorType = USB_DT_INTERFACE,
107 .bInterfaceNumber = 2,
109 .bAlternateSetting = 1,
110 .bNumEndpoints = 2,
111 .bInterfaceClass = USB_CLASS_CDC_DATA,
114 static struct usb_cdc_header_desc obex_cdc_header_desc __initdata = {
115 .bLength = sizeof(obex_cdc_header_desc),
116 .bDescriptorType = USB_DT_CS_INTERFACE,
117 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
118 .bcdCDC = cpu_to_le16(0x0120),
121 static struct usb_cdc_union_desc obex_cdc_union_desc __initdata = {
122 .bLength = sizeof(obex_cdc_union_desc),
123 .bDescriptorType = USB_DT_CS_INTERFACE,
124 .bDescriptorSubType = USB_CDC_UNION_TYPE,
125 .bMasterInterface0 = 1,
126 .bSlaveInterface0 = 2,
129 static struct usb_cdc_obex_desc obex_desc __initdata = {
130 .bLength = sizeof(obex_desc),
131 .bDescriptorType = USB_DT_CS_INTERFACE,
132 .bDescriptorSubType = USB_CDC_OBEX_TYPE,
133 .bcdVersion = cpu_to_le16(0x0100),
136 /* High-Speed Support */
138 static struct usb_endpoint_descriptor obex_hs_ep_out_desc __initdata = {
139 .bLength = USB_DT_ENDPOINT_SIZE,
140 .bDescriptorType = USB_DT_ENDPOINT,
142 .bEndpointAddress = USB_DIR_OUT,
143 .bmAttributes = USB_ENDPOINT_XFER_BULK,
144 .wMaxPacketSize = cpu_to_le16(512),
147 static struct usb_endpoint_descriptor obex_hs_ep_in_desc __initdata = {
148 .bLength = USB_DT_ENDPOINT_SIZE,
149 .bDescriptorType = USB_DT_ENDPOINT,
151 .bEndpointAddress = USB_DIR_IN,
152 .bmAttributes = USB_ENDPOINT_XFER_BULK,
153 .wMaxPacketSize = cpu_to_le16(512),
156 static struct usb_descriptor_header *hs_function[] __initdata = {
157 (struct usb_descriptor_header *) &obex_control_intf,
158 (struct usb_descriptor_header *) &obex_cdc_header_desc,
159 (struct usb_descriptor_header *) &obex_desc,
160 (struct usb_descriptor_header *) &obex_cdc_union_desc,
162 (struct usb_descriptor_header *) &obex_data_nop_intf,
163 (struct usb_descriptor_header *) &obex_data_intf,
164 (struct usb_descriptor_header *) &obex_hs_ep_in_desc,
165 (struct usb_descriptor_header *) &obex_hs_ep_out_desc,
166 NULL,
169 /* Full-Speed Support */
171 static struct usb_endpoint_descriptor obex_fs_ep_in_desc __initdata = {
172 .bLength = USB_DT_ENDPOINT_SIZE,
173 .bDescriptorType = USB_DT_ENDPOINT,
175 .bEndpointAddress = USB_DIR_IN,
176 .bmAttributes = USB_ENDPOINT_XFER_BULK,
179 static struct usb_endpoint_descriptor obex_fs_ep_out_desc __initdata = {
180 .bLength = USB_DT_ENDPOINT_SIZE,
181 .bDescriptorType = USB_DT_ENDPOINT,
183 .bEndpointAddress = USB_DIR_OUT,
184 .bmAttributes = USB_ENDPOINT_XFER_BULK,
187 static struct usb_descriptor_header *fs_function[] __initdata = {
188 (struct usb_descriptor_header *) &obex_control_intf,
189 (struct usb_descriptor_header *) &obex_cdc_header_desc,
190 (struct usb_descriptor_header *) &obex_desc,
191 (struct usb_descriptor_header *) &obex_cdc_union_desc,
193 (struct usb_descriptor_header *) &obex_data_nop_intf,
194 (struct usb_descriptor_header *) &obex_data_intf,
195 (struct usb_descriptor_header *) &obex_fs_ep_in_desc,
196 (struct usb_descriptor_header *) &obex_fs_ep_out_desc,
197 NULL,
200 /*-------------------------------------------------------------------------*/
202 static int obex_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
204 struct f_obex *obex = func_to_obex(f);
205 struct usb_composite_dev *cdev = f->config->cdev;
207 if (intf == obex->ctrl_id) {
208 if (alt != 0)
209 goto fail;
210 /* NOP */
211 DBG(cdev, "reset obex ttyGS%d control\n", obex->port_num);
213 } else if (intf == obex->data_id) {
214 if (alt > 1)
215 goto fail;
217 if (obex->port.in->driver_data) {
218 DBG(cdev, "reset obex ttyGS%d\n", obex->port_num);
219 gserial_disconnect(&obex->port);
222 if (!obex->port.in->desc || !obex->port.out->desc) {
223 DBG(cdev, "init obex ttyGS%d\n", obex->port_num);
224 if (config_ep_by_speed(cdev->gadget, f,
225 obex->port.in) ||
226 config_ep_by_speed(cdev->gadget, f,
227 obex->port.out)) {
228 obex->port.out->desc = NULL;
229 obex->port.in->desc = NULL;
230 goto fail;
234 if (alt == 1) {
235 DBG(cdev, "activate obex ttyGS%d\n", obex->port_num);
236 gserial_connect(&obex->port, obex->port_num);
239 } else
240 goto fail;
242 return 0;
244 fail:
245 return -EINVAL;
248 static int obex_get_alt(struct usb_function *f, unsigned intf)
250 struct f_obex *obex = func_to_obex(f);
252 if (intf == obex->ctrl_id)
253 return 0;
255 return obex->port.in->driver_data ? 1 : 0;
258 static void obex_disable(struct usb_function *f)
260 struct f_obex *obex = func_to_obex(f);
261 struct usb_composite_dev *cdev = f->config->cdev;
263 DBG(cdev, "obex ttyGS%d disable\n", obex->port_num);
264 gserial_disconnect(&obex->port);
267 /*-------------------------------------------------------------------------*/
269 static void obex_connect(struct gserial *g)
271 struct f_obex *obex = port_to_obex(g);
272 struct usb_composite_dev *cdev = g->func.config->cdev;
273 int status;
275 if (!obex->can_activate)
276 return;
278 status = usb_function_activate(&g->func);
279 if (status)
280 DBG(cdev, "obex ttyGS%d function activate --> %d\n",
281 obex->port_num, status);
284 static void obex_disconnect(struct gserial *g)
286 struct f_obex *obex = port_to_obex(g);
287 struct usb_composite_dev *cdev = g->func.config->cdev;
288 int status;
290 if (!obex->can_activate)
291 return;
293 status = usb_function_deactivate(&g->func);
294 if (status)
295 DBG(cdev, "obex ttyGS%d function deactivate --> %d\n",
296 obex->port_num, status);
299 /*-------------------------------------------------------------------------*/
301 static int __init
302 obex_bind(struct usb_configuration *c, struct usb_function *f)
304 struct usb_composite_dev *cdev = c->cdev;
305 struct f_obex *obex = func_to_obex(f);
306 int status;
307 struct usb_ep *ep;
309 /* allocate instance-specific interface IDs, and patch descriptors */
311 status = usb_interface_id(c, f);
312 if (status < 0)
313 goto fail;
314 obex->ctrl_id = status;
316 obex_control_intf.bInterfaceNumber = status;
317 obex_cdc_union_desc.bMasterInterface0 = status;
319 status = usb_interface_id(c, f);
320 if (status < 0)
321 goto fail;
322 obex->data_id = status;
324 obex_data_nop_intf.bInterfaceNumber = status;
325 obex_data_intf.bInterfaceNumber = status;
326 obex_cdc_union_desc.bSlaveInterface0 = status;
328 /* allocate instance-specific endpoints */
330 ep = usb_ep_autoconfig(cdev->gadget, &obex_fs_ep_in_desc);
331 if (!ep)
332 goto fail;
333 obex->port.in = ep;
334 ep->driver_data = cdev; /* claim */
336 ep = usb_ep_autoconfig(cdev->gadget, &obex_fs_ep_out_desc);
337 if (!ep)
338 goto fail;
339 obex->port.out = ep;
340 ep->driver_data = cdev; /* claim */
342 /* copy descriptors, and track endpoint copies */
343 f->descriptors = usb_copy_descriptors(fs_function);
345 /* support all relevant hardware speeds... we expect that when
346 * hardware is dual speed, all bulk-capable endpoints work at
347 * both speeds
349 if (gadget_is_dualspeed(c->cdev->gadget)) {
351 obex_hs_ep_in_desc.bEndpointAddress =
352 obex_fs_ep_in_desc.bEndpointAddress;
353 obex_hs_ep_out_desc.bEndpointAddress =
354 obex_fs_ep_out_desc.bEndpointAddress;
356 /* copy descriptors, and track endpoint copies */
357 f->hs_descriptors = usb_copy_descriptors(hs_function);
360 /* Avoid letting this gadget enumerate until the userspace
361 * OBEX server is active.
363 status = usb_function_deactivate(f);
364 if (status < 0)
365 WARNING(cdev, "obex ttyGS%d: can't prevent enumeration, %d\n",
366 obex->port_num, status);
367 else
368 obex->can_activate = true;
371 DBG(cdev, "obex ttyGS%d: %s speed IN/%s OUT/%s\n",
372 obex->port_num,
373 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
374 obex->port.in->name, obex->port.out->name);
376 return 0;
378 fail:
379 /* we might as well release our claims on endpoints */
380 if (obex->port.out)
381 obex->port.out->driver_data = NULL;
382 if (obex->port.in)
383 obex->port.in->driver_data = NULL;
385 ERROR(cdev, "%s/%p: can't bind, err %d\n", f->name, f, status);
387 return status;
390 static void
391 obex_unbind(struct usb_configuration *c, struct usb_function *f)
393 if (gadget_is_dualspeed(c->cdev->gadget))
394 usb_free_descriptors(f->hs_descriptors);
395 usb_free_descriptors(f->descriptors);
396 kfree(func_to_obex(f));
399 /* Some controllers can't support CDC OBEX ... */
400 static inline bool can_support_obex(struct usb_configuration *c)
402 /* Since the first interface is a NOP, we can ignore the
403 * issue of multi-interface support on most controllers.
405 * Altsettings are mandatory, however...
407 if (!gadget_supports_altsettings(c->cdev->gadget))
408 return false;
410 /* everything else is *probably* fine ... */
411 return true;
415 * obex_bind_config - add a CDC OBEX function to a configuration
416 * @c: the configuration to support the CDC OBEX instance
417 * @port_num: /dev/ttyGS* port this interface will use
418 * Context: single threaded during gadget setup
420 * Returns zero on success, else negative errno.
422 * Caller must have called @gserial_setup() with enough ports to
423 * handle all the ones it binds. Caller is also responsible
424 * for calling @gserial_cleanup() before module unload.
426 int __init obex_bind_config(struct usb_configuration *c, u8 port_num)
428 struct f_obex *obex;
429 int status;
431 if (!can_support_obex(c))
432 return -EINVAL;
434 /* maybe allocate device-global string IDs, and patch descriptors */
435 if (obex_string_defs[OBEX_CTRL_IDX].id == 0) {
436 status = usb_string_id(c->cdev);
437 if (status < 0)
438 return status;
439 obex_string_defs[OBEX_CTRL_IDX].id = status;
441 obex_control_intf.iInterface = status;
443 status = usb_string_id(c->cdev);
444 if (status < 0)
445 return status;
446 obex_string_defs[OBEX_DATA_IDX].id = status;
448 obex_data_nop_intf.iInterface =
449 obex_data_intf.iInterface = status;
452 /* allocate and initialize one new instance */
453 obex = kzalloc(sizeof *obex, GFP_KERNEL);
454 if (!obex)
455 return -ENOMEM;
457 obex->port_num = port_num;
459 obex->port.connect = obex_connect;
460 obex->port.disconnect = obex_disconnect;
462 obex->port.func.name = "obex";
463 obex->port.func.strings = obex_strings;
464 /* descriptors are per-instance copies */
465 obex->port.func.bind = obex_bind;
466 obex->port.func.unbind = obex_unbind;
467 obex->port.func.set_alt = obex_set_alt;
468 obex->port.func.get_alt = obex_get_alt;
469 obex->port.func.disable = obex_disable;
471 status = usb_add_function(c, &obex->port.func);
472 if (status)
473 kfree(obex);
475 return status;
478 MODULE_AUTHOR("Felipe Balbi");
479 MODULE_LICENSE("GPL");