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/kernel.h>
27 //#include <linux/device.h>
29 #include <ubi_uboot.h>
30 #include <linux/mtd/compat.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 obex_ep_descs {
44 struct usb_endpoint_descriptor *obex_in;
45 struct usb_endpoint_descriptor *obex_out;
55 struct obex_ep_descs fs;
56 struct obex_ep_descs hs;
59 static inline struct f_obex *func_to_obex(struct usb_function *f)
61 return container_of(f, struct f_obex, port.func);
64 static inline struct f_obex *port_to_obex(struct gserial *p)
66 return container_of(p, struct f_obex, port);
69 /*-------------------------------------------------------------------------*/
71 #define OBEX_CTRL_IDX 0
72 #define OBEX_DATA_IDX 1
74 static struct usb_string obex_string_defs[] = {
75 [OBEX_CTRL_IDX].s = "CDC Object Exchange (OBEX)",
76 [OBEX_DATA_IDX].s = "CDC OBEX Data",
77 { }, /* end of list */
80 static struct usb_gadget_strings obex_string_table = {
81 .language = 0x0409, /* en-US */
82 .strings = obex_string_defs,
85 static struct usb_gadget_strings *obex_strings[] = {
90 /*-------------------------------------------------------------------------*/
92 static struct usb_interface_descriptor obex_control_intf __initdata = {
93 .bLength = sizeof(obex_control_intf),
94 .bDescriptorType = USB_DT_INTERFACE,
95 .bInterfaceNumber = 0,
97 .bAlternateSetting = 0,
99 .bInterfaceClass = USB_CLASS_COMM,
100 .bInterfaceSubClass = USB_CDC_SUBCLASS_OBEX,
103 static struct usb_interface_descriptor obex_data_nop_intf __initdata = {
104 .bLength = sizeof(obex_data_nop_intf),
105 .bDescriptorType = USB_DT_INTERFACE,
106 .bInterfaceNumber = 1,
108 .bAlternateSetting = 0,
110 .bInterfaceClass = USB_CLASS_CDC_DATA,
113 static struct usb_interface_descriptor obex_data_intf __initdata = {
114 .bLength = sizeof(obex_data_intf),
115 .bDescriptorType = USB_DT_INTERFACE,
116 .bInterfaceNumber = 2,
118 .bAlternateSetting = 1,
120 .bInterfaceClass = USB_CLASS_CDC_DATA,
123 static struct usb_cdc_header_desc obex_cdc_header_desc __initdata = {
124 .bLength = sizeof(obex_cdc_header_desc),
125 .bDescriptorType = USB_DT_CS_INTERFACE,
126 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
127 .bcdCDC = cpu_to_le16(0x0120),
130 static struct usb_cdc_union_desc obex_cdc_union_desc __initdata = {
131 .bLength = sizeof(obex_cdc_union_desc),
132 .bDescriptorType = USB_DT_CS_INTERFACE,
133 .bDescriptorSubType = USB_CDC_UNION_TYPE,
134 .bMasterInterface0 = 1,
135 .bSlaveInterface0 = 2,
138 static struct usb_cdc_obex_desc obex_desc __initdata = {
139 .bLength = sizeof(obex_desc),
140 .bDescriptorType = USB_DT_CS_INTERFACE,
141 .bDescriptorSubType = USB_CDC_OBEX_TYPE,
142 .bcdVersion = cpu_to_le16(0x0100),
145 /* High-Speed Support */
147 static struct usb_endpoint_descriptor obex_hs_ep_out_desc __initdata = {
148 .bLength = USB_DT_ENDPOINT_SIZE,
149 .bDescriptorType = USB_DT_ENDPOINT,
151 .bEndpointAddress = USB_DIR_OUT,
152 .bmAttributes = USB_ENDPOINT_XFER_BULK,
153 .wMaxPacketSize = cpu_to_le16(512),
156 static struct usb_endpoint_descriptor obex_hs_ep_in_desc __initdata = {
157 .bLength = USB_DT_ENDPOINT_SIZE,
158 .bDescriptorType = USB_DT_ENDPOINT,
160 .bEndpointAddress = USB_DIR_IN,
161 .bmAttributes = USB_ENDPOINT_XFER_BULK,
162 .wMaxPacketSize = cpu_to_le16(512),
165 static struct usb_descriptor_header *hs_function[] __initdata = {
166 (struct usb_descriptor_header *) &obex_control_intf,
167 (struct usb_descriptor_header *) &obex_cdc_header_desc,
168 (struct usb_descriptor_header *) &obex_desc,
169 (struct usb_descriptor_header *) &obex_cdc_union_desc,
171 (struct usb_descriptor_header *) &obex_data_nop_intf,
172 (struct usb_descriptor_header *) &obex_data_intf,
173 (struct usb_descriptor_header *) &obex_hs_ep_in_desc,
174 (struct usb_descriptor_header *) &obex_hs_ep_out_desc,
178 /* Full-Speed Support */
180 static struct usb_endpoint_descriptor obex_fs_ep_in_desc __initdata = {
181 .bLength = USB_DT_ENDPOINT_SIZE,
182 .bDescriptorType = USB_DT_ENDPOINT,
184 .bEndpointAddress = USB_DIR_IN,
185 .bmAttributes = USB_ENDPOINT_XFER_BULK,
188 static struct usb_endpoint_descriptor obex_fs_ep_out_desc __initdata = {
189 .bLength = USB_DT_ENDPOINT_SIZE,
190 .bDescriptorType = USB_DT_ENDPOINT,
192 .bEndpointAddress = USB_DIR_OUT,
193 .bmAttributes = USB_ENDPOINT_XFER_BULK,
196 static struct usb_descriptor_header *fs_function[] __initdata = {
197 (struct usb_descriptor_header *) &obex_control_intf,
198 (struct usb_descriptor_header *) &obex_cdc_header_desc,
199 (struct usb_descriptor_header *) &obex_desc,
200 (struct usb_descriptor_header *) &obex_cdc_union_desc,
202 (struct usb_descriptor_header *) &obex_data_nop_intf,
203 (struct usb_descriptor_header *) &obex_data_intf,
204 (struct usb_descriptor_header *) &obex_fs_ep_in_desc,
205 (struct usb_descriptor_header *) &obex_fs_ep_out_desc,
209 /*-------------------------------------------------------------------------*/
211 static int obex_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
213 struct f_obex *obex = func_to_obex(f);
214 struct usb_composite_dev *cdev = f->config->cdev;
216 if (intf == obex->ctrl_id) {
220 DBG(cdev, "reset obex ttyGS%d control\n", obex->port_num);
222 } else if (intf == obex->data_id) {
226 if (obex->port.in->driver_data) {
227 DBG(cdev, "reset obex ttyGS%d\n", obex->port_num);
228 gserial_disconnect(&obex->port);
231 if (!obex->port.in_desc) {
232 DBG(cdev, "init obex ttyGS%d\n", obex->port_num);
233 obex->port.in_desc = ep_choose(cdev->gadget,
234 obex->hs.obex_in, obex->fs.obex_in);
235 obex->port.out_desc = ep_choose(cdev->gadget,
236 obex->hs.obex_out, obex->fs.obex_out);
240 DBG(cdev, "activate obex ttyGS%d\n", obex->port_num);
241 gserial_connect(&obex->port, obex->port_num);
253 static int obex_get_alt(struct usb_function *f, unsigned intf)
255 struct f_obex *obex = func_to_obex(f);
257 if (intf == obex->ctrl_id)
260 return obex->port.in->driver_data ? 1 : 0;
263 static void obex_disable(struct usb_function *f)
265 struct f_obex *obex = func_to_obex(f);
267 DBG(f->config->cdev, "obex ttyGS%d disable\n", obex->port_num);
268 gserial_disconnect(&obex->port);
271 /*-------------------------------------------------------------------------*/
273 static void obex_connect(struct gserial *g)
275 struct f_obex *obex = port_to_obex(g);
278 if (!obex->can_activate)
281 status = usb_function_activate(&g->func);
283 DBG(g->func.config->cdev,
284 "obex ttyGS%d function activate --> %d\n",
285 obex->port_num, status);
288 static void obex_disconnect(struct gserial *g)
290 struct f_obex *obex = port_to_obex(g);
293 if (!obex->can_activate)
296 status = usb_function_deactivate(&g->func);
298 DBG(g->func.config->cdev,
299 "obex ttyGS%d function deactivate --> %d\n",
300 obex->port_num, status);
303 /*-------------------------------------------------------------------------*/
306 obex_bind(struct usb_configuration *c, struct usb_function *f)
308 struct usb_composite_dev *cdev = c->cdev;
309 struct f_obex *obex = func_to_obex(f);
313 /* allocate instance-specific interface IDs, and patch descriptors */
315 status = usb_interface_id(c, f);
318 obex->ctrl_id = status;
320 obex_control_intf.bInterfaceNumber = status;
321 obex_cdc_union_desc.bMasterInterface0 = status;
323 status = usb_interface_id(c, f);
326 obex->data_id = status;
328 obex_data_nop_intf.bInterfaceNumber = status;
329 obex_data_intf.bInterfaceNumber = status;
330 obex_cdc_union_desc.bSlaveInterface0 = status;
332 /* allocate instance-specific endpoints */
334 ep = usb_ep_autoconfig(cdev->gadget, &obex_fs_ep_in_desc);
338 ep->driver_data = cdev; /* claim */
340 ep = usb_ep_autoconfig(cdev->gadget, &obex_fs_ep_out_desc);
344 ep->driver_data = cdev; /* claim */
346 /* copy descriptors, and track endpoint copies */
347 f->descriptors = usb_copy_descriptors(fs_function);
349 obex->fs.obex_in = usb_find_endpoint(fs_function,
350 f->descriptors, &obex_fs_ep_in_desc);
351 obex->fs.obex_out = usb_find_endpoint(fs_function,
352 f->descriptors, &obex_fs_ep_out_desc);
354 /* support all relevant hardware speeds... we expect that when
355 * hardware is dual speed, all bulk-capable endpoints work at
358 if (gadget_is_dualspeed(c->cdev->gadget)) {
360 obex_hs_ep_in_desc.bEndpointAddress =
361 obex_fs_ep_in_desc.bEndpointAddress;
362 obex_hs_ep_out_desc.bEndpointAddress =
363 obex_fs_ep_out_desc.bEndpointAddress;
365 /* copy descriptors, and track endpoint copies */
366 f->hs_descriptors = usb_copy_descriptors(hs_function);
368 obex->hs.obex_in = usb_find_endpoint(hs_function,
369 f->hs_descriptors, &obex_hs_ep_in_desc);
370 obex->hs.obex_out = usb_find_endpoint(hs_function,
371 f->hs_descriptors, &obex_hs_ep_out_desc);
374 /* Avoid letting this gadget enumerate until the userspace
375 * OBEX server is active.
377 status = usb_function_deactivate(f);
379 WARNING(cdev, "obex ttyGS%d: can't prevent enumeration, %d\n",
380 obex->port_num, status);
382 obex->can_activate = true;
385 DBG(cdev, "obex ttyGS%d: %s speed IN/%s OUT/%s\n",
387 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
388 obex->port.in->name, obex->port.out->name);
393 /* we might as well release our claims on endpoints */
395 obex->port.out->driver_data = NULL;
397 obex->port.in->driver_data = NULL;
399 ERROR(cdev, "%s/%p: can't bind, err %d\n", f->name, f, status);
405 obex_unbind(struct usb_configuration *c, struct usb_function *f)
407 if (gadget_is_dualspeed(c->cdev->gadget))
408 usb_free_descriptors(f->hs_descriptors);
409 usb_free_descriptors(f->descriptors);
410 kfree(func_to_obex(f));
413 /* Some controllers can't support CDC OBEX ... */
414 static inline bool can_support_obex(struct usb_configuration *c)
416 /* Since the first interface is a NOP, we can ignore the
417 * issue of multi-interface support on most controllers.
419 * Altsettings are mandatory, however...
421 if (!gadget_supports_altsettings(c->cdev->gadget))
424 /* everything else is *probably* fine ... */
429 * obex_bind_config - add a CDC OBEX function to a configuration
430 * @c: the configuration to support the CDC OBEX instance
431 * @port_num: /dev/ttyGS* port this interface will use
432 * Context: single threaded during gadget setup
434 * Returns zero on success, else negative errno.
436 * Caller must have called @gserial_setup() with enough ports to
437 * handle all the ones it binds. Caller is also responsible
438 * for calling @gserial_cleanup() before module unload.
440 int __init obex_bind_config(struct usb_configuration *c, u8 port_num)
445 if (!can_support_obex(c))
448 /* maybe allocate device-global string IDs, and patch descriptors */
449 if (obex_string_defs[OBEX_CTRL_IDX].id == 0) {
450 status = usb_string_id(c->cdev);
453 obex_string_defs[OBEX_CTRL_IDX].id = status;
455 obex_control_intf.iInterface = status;
457 status = usb_string_id(c->cdev);
460 obex_string_defs[OBEX_DATA_IDX].id = status;
462 obex_data_nop_intf.iInterface =
463 obex_data_intf.iInterface = status;
466 /* allocate and initialize one new instance */
467 obex = kzalloc(sizeof *obex, GFP_KERNEL);
471 obex->port_num = port_num;
473 obex->port.connect = obex_connect;
474 obex->port.disconnect = obex_disconnect;
476 obex->port.func.name = "obex";
477 obex->port.func.strings = obex_strings;
478 /* descriptors are per-instance copies */
479 obex->port.func.bind = obex_bind;
480 obex->port.func.unbind = obex_unbind;
481 obex->port.func.set_alt = obex_set_alt;
482 obex->port.func.get_alt = obex_get_alt;
483 obex->port.func.disable = obex_disable;
485 status = usb_add_function(c, &obex->port.func);
492 MODULE_AUTHOR("Felipe Balbi");
493 MODULE_LICENSE("GPL");