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>
30 #include "gadget_chips.h"
34 * This CDC OBEX function support just packages a TTY-ish byte stream.
35 * A user mode server will put it into "raw" mode and handle all the
36 * relevant protocol details ... this is just a kernel passthrough.
37 * When possible, we prevent gadget enumeration until that server is
38 * ready to handle the commands.
41 struct obex_ep_descs
{
42 struct usb_endpoint_descriptor
*obex_in
;
43 struct usb_endpoint_descriptor
*obex_out
;
53 struct obex_ep_descs fs
;
54 struct obex_ep_descs hs
;
57 static inline struct f_obex
*func_to_obex(struct usb_function
*f
)
59 return container_of(f
, struct f_obex
, port
.func
);
62 static inline struct f_obex
*port_to_obex(struct gserial
*p
)
64 return container_of(p
, struct f_obex
, port
);
67 /*-------------------------------------------------------------------------*/
69 #define OBEX_CTRL_IDX 0
70 #define OBEX_DATA_IDX 1
72 static struct usb_string obex_string_defs
[] = {
73 [OBEX_CTRL_IDX
].s
= "CDC Object Exchange (OBEX)",
74 [OBEX_DATA_IDX
].s
= "CDC OBEX Data",
75 { }, /* end of list */
78 static struct usb_gadget_strings obex_string_table
= {
79 .language
= 0x0409, /* en-US */
80 .strings
= obex_string_defs
,
83 static struct usb_gadget_strings
*obex_strings
[] = {
88 /*-------------------------------------------------------------------------*/
90 static struct usb_interface_descriptor obex_control_intf __initdata
= {
91 .bLength
= sizeof(obex_control_intf
),
92 .bDescriptorType
= USB_DT_INTERFACE
,
93 .bInterfaceNumber
= 0,
95 .bAlternateSetting
= 0,
97 .bInterfaceClass
= USB_CLASS_COMM
,
98 .bInterfaceSubClass
= USB_CDC_SUBCLASS_OBEX
,
101 static struct usb_interface_descriptor obex_data_nop_intf __initdata
= {
102 .bLength
= sizeof(obex_data_nop_intf
),
103 .bDescriptorType
= USB_DT_INTERFACE
,
104 .bInterfaceNumber
= 1,
106 .bAlternateSetting
= 0,
108 .bInterfaceClass
= USB_CLASS_CDC_DATA
,
111 static struct usb_interface_descriptor obex_data_intf __initdata
= {
112 .bLength
= sizeof(obex_data_intf
),
113 .bDescriptorType
= USB_DT_INTERFACE
,
114 .bInterfaceNumber
= 2,
116 .bAlternateSetting
= 1,
118 .bInterfaceClass
= USB_CLASS_CDC_DATA
,
121 static struct usb_cdc_header_desc obex_cdc_header_desc __initdata
= {
122 .bLength
= sizeof(obex_cdc_header_desc
),
123 .bDescriptorType
= USB_DT_CS_INTERFACE
,
124 .bDescriptorSubType
= USB_CDC_HEADER_TYPE
,
125 .bcdCDC
= cpu_to_le16(0x0120),
128 static struct usb_cdc_union_desc obex_cdc_union_desc __initdata
= {
129 .bLength
= sizeof(obex_cdc_union_desc
),
130 .bDescriptorType
= USB_DT_CS_INTERFACE
,
131 .bDescriptorSubType
= USB_CDC_UNION_TYPE
,
132 .bMasterInterface0
= 1,
133 .bSlaveInterface0
= 2,
136 static struct usb_cdc_obex_desc obex_desc __initdata
= {
137 .bLength
= sizeof(obex_desc
),
138 .bDescriptorType
= USB_DT_CS_INTERFACE
,
139 .bDescriptorSubType
= USB_CDC_OBEX_TYPE
,
140 .bcdVersion
= cpu_to_le16(0x0100),
143 /* High-Speed Support */
145 static struct usb_endpoint_descriptor obex_hs_ep_out_desc __initdata
= {
146 .bLength
= USB_DT_ENDPOINT_SIZE
,
147 .bDescriptorType
= USB_DT_ENDPOINT
,
149 .bEndpointAddress
= USB_DIR_OUT
,
150 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
151 .wMaxPacketSize
= cpu_to_le16(512),
154 static struct usb_endpoint_descriptor obex_hs_ep_in_desc __initdata
= {
155 .bLength
= USB_DT_ENDPOINT_SIZE
,
156 .bDescriptorType
= USB_DT_ENDPOINT
,
158 .bEndpointAddress
= USB_DIR_IN
,
159 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
160 .wMaxPacketSize
= cpu_to_le16(512),
163 static struct usb_descriptor_header
*hs_function
[] __initdata
= {
164 (struct usb_descriptor_header
*) &obex_control_intf
,
165 (struct usb_descriptor_header
*) &obex_cdc_header_desc
,
166 (struct usb_descriptor_header
*) &obex_desc
,
167 (struct usb_descriptor_header
*) &obex_cdc_union_desc
,
169 (struct usb_descriptor_header
*) &obex_data_nop_intf
,
170 (struct usb_descriptor_header
*) &obex_data_intf
,
171 (struct usb_descriptor_header
*) &obex_hs_ep_in_desc
,
172 (struct usb_descriptor_header
*) &obex_hs_ep_out_desc
,
176 /* Full-Speed Support */
178 static struct usb_endpoint_descriptor obex_fs_ep_in_desc __initdata
= {
179 .bLength
= USB_DT_ENDPOINT_SIZE
,
180 .bDescriptorType
= USB_DT_ENDPOINT
,
182 .bEndpointAddress
= USB_DIR_IN
,
183 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
186 static struct usb_endpoint_descriptor obex_fs_ep_out_desc __initdata
= {
187 .bLength
= USB_DT_ENDPOINT_SIZE
,
188 .bDescriptorType
= USB_DT_ENDPOINT
,
190 .bEndpointAddress
= USB_DIR_OUT
,
191 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
194 static struct usb_descriptor_header
*fs_function
[] __initdata
= {
195 (struct usb_descriptor_header
*) &obex_control_intf
,
196 (struct usb_descriptor_header
*) &obex_cdc_header_desc
,
197 (struct usb_descriptor_header
*) &obex_desc
,
198 (struct usb_descriptor_header
*) &obex_cdc_union_desc
,
200 (struct usb_descriptor_header
*) &obex_data_nop_intf
,
201 (struct usb_descriptor_header
*) &obex_data_intf
,
202 (struct usb_descriptor_header
*) &obex_fs_ep_in_desc
,
203 (struct usb_descriptor_header
*) &obex_fs_ep_out_desc
,
207 /*-------------------------------------------------------------------------*/
209 static int obex_set_alt(struct usb_function
*f
, unsigned intf
, unsigned alt
)
211 struct f_obex
*obex
= func_to_obex(f
);
212 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
214 if (intf
== obex
->ctrl_id
) {
218 DBG(cdev
, "reset obex ttyGS%d control\n", obex
->port_num
);
220 } else if (intf
== obex
->data_id
) {
224 if (obex
->port
.in
->driver_data
) {
225 DBG(cdev
, "reset obex ttyGS%d\n", obex
->port_num
);
226 gserial_disconnect(&obex
->port
);
229 if (!obex
->port
.in_desc
) {
230 DBG(cdev
, "init obex ttyGS%d\n", obex
->port_num
);
231 obex
->port
.in_desc
= ep_choose(cdev
->gadget
,
232 obex
->hs
.obex_in
, obex
->fs
.obex_in
);
233 obex
->port
.out_desc
= ep_choose(cdev
->gadget
,
234 obex
->hs
.obex_out
, obex
->fs
.obex_out
);
238 DBG(cdev
, "activate obex ttyGS%d\n", obex
->port_num
);
239 gserial_connect(&obex
->port
, obex
->port_num
);
251 static int obex_get_alt(struct usb_function
*f
, unsigned intf
)
253 struct f_obex
*obex
= func_to_obex(f
);
255 if (intf
== obex
->ctrl_id
)
258 return obex
->port
.in
->driver_data
? 1 : 0;
261 static void obex_disable(struct usb_function
*f
)
263 struct f_obex
*obex
= func_to_obex(f
);
264 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
266 DBG(cdev
, "obex ttyGS%d disable\n", obex
->port_num
);
267 gserial_disconnect(&obex
->port
);
270 /*-------------------------------------------------------------------------*/
272 static void obex_connect(struct gserial
*g
)
274 struct f_obex
*obex
= port_to_obex(g
);
275 struct usb_composite_dev
*cdev
= g
->func
.config
->cdev
;
278 if (!obex
->can_activate
)
281 status
= usb_function_activate(&g
->func
);
283 DBG(cdev
, "obex ttyGS%d function activate --> %d\n",
284 obex
->port_num
, status
);
287 static void obex_disconnect(struct gserial
*g
)
289 struct f_obex
*obex
= port_to_obex(g
);
290 struct usb_composite_dev
*cdev
= g
->func
.config
->cdev
;
293 if (!obex
->can_activate
)
296 status
= usb_function_deactivate(&g
->func
);
298 DBG(cdev
, "obex ttyGS%d function deactivate --> %d\n",
299 obex
->port_num
, status
);
302 /*-------------------------------------------------------------------------*/
305 obex_bind(struct usb_configuration
*c
, struct usb_function
*f
)
307 struct usb_composite_dev
*cdev
= c
->cdev
;
308 struct f_obex
*obex
= func_to_obex(f
);
312 /* allocate instance-specific interface IDs, and patch descriptors */
314 status
= usb_interface_id(c
, f
);
317 obex
->ctrl_id
= status
;
319 obex_control_intf
.bInterfaceNumber
= status
;
320 obex_cdc_union_desc
.bMasterInterface0
= status
;
322 status
= usb_interface_id(c
, f
);
325 obex
->data_id
= status
;
327 obex_data_nop_intf
.bInterfaceNumber
= status
;
328 obex_data_intf
.bInterfaceNumber
= status
;
329 obex_cdc_union_desc
.bSlaveInterface0
= status
;
331 /* allocate instance-specific endpoints */
333 ep
= usb_ep_autoconfig(cdev
->gadget
, &obex_fs_ep_in_desc
);
337 ep
->driver_data
= cdev
; /* claim */
339 ep
= usb_ep_autoconfig(cdev
->gadget
, &obex_fs_ep_out_desc
);
343 ep
->driver_data
= cdev
; /* claim */
345 /* copy descriptors, and track endpoint copies */
346 f
->descriptors
= usb_copy_descriptors(fs_function
);
348 obex
->fs
.obex_in
= usb_find_endpoint(fs_function
,
349 f
->descriptors
, &obex_fs_ep_in_desc
);
350 obex
->fs
.obex_out
= usb_find_endpoint(fs_function
,
351 f
->descriptors
, &obex_fs_ep_out_desc
);
353 /* support all relevant hardware speeds... we expect that when
354 * hardware is dual speed, all bulk-capable endpoints work at
357 if (gadget_is_dualspeed(c
->cdev
->gadget
)) {
359 obex_hs_ep_in_desc
.bEndpointAddress
=
360 obex_fs_ep_in_desc
.bEndpointAddress
;
361 obex_hs_ep_out_desc
.bEndpointAddress
=
362 obex_fs_ep_out_desc
.bEndpointAddress
;
364 /* copy descriptors, and track endpoint copies */
365 f
->hs_descriptors
= usb_copy_descriptors(hs_function
);
367 obex
->hs
.obex_in
= usb_find_endpoint(hs_function
,
368 f
->hs_descriptors
, &obex_hs_ep_in_desc
);
369 obex
->hs
.obex_out
= usb_find_endpoint(hs_function
,
370 f
->hs_descriptors
, &obex_hs_ep_out_desc
);
373 /* Avoid letting this gadget enumerate until the userspace
374 * OBEX server is active.
376 status
= usb_function_deactivate(f
);
378 WARNING(cdev
, "obex ttyGS%d: can't prevent enumeration, %d\n",
379 obex
->port_num
, status
);
381 obex
->can_activate
= true;
384 DBG(cdev
, "obex ttyGS%d: %s speed IN/%s OUT/%s\n",
386 gadget_is_dualspeed(c
->cdev
->gadget
) ? "dual" : "full",
387 obex
->port
.in
->name
, obex
->port
.out
->name
);
392 /* we might as well release our claims on endpoints */
394 obex
->port
.out
->driver_data
= NULL
;
396 obex
->port
.in
->driver_data
= NULL
;
398 ERROR(cdev
, "%s/%p: can't bind, err %d\n", f
->name
, f
, status
);
404 obex_unbind(struct usb_configuration
*c
, struct usb_function
*f
)
406 if (gadget_is_dualspeed(c
->cdev
->gadget
))
407 usb_free_descriptors(f
->hs_descriptors
);
408 usb_free_descriptors(f
->descriptors
);
409 kfree(func_to_obex(f
));
412 /* Some controllers can't support CDC OBEX ... */
413 static inline bool can_support_obex(struct usb_configuration
*c
)
415 /* Since the first interface is a NOP, we can ignore the
416 * issue of multi-interface support on most controllers.
418 * Altsettings are mandatory, however...
420 if (!gadget_supports_altsettings(c
->cdev
->gadget
))
423 /* everything else is *probably* fine ... */
428 * obex_bind_config - add a CDC OBEX function to a configuration
429 * @c: the configuration to support the CDC OBEX instance
430 * @port_num: /dev/ttyGS* port this interface will use
431 * Context: single threaded during gadget setup
433 * Returns zero on success, else negative errno.
435 * Caller must have called @gserial_setup() with enough ports to
436 * handle all the ones it binds. Caller is also responsible
437 * for calling @gserial_cleanup() before module unload.
439 int __init
obex_bind_config(struct usb_configuration
*c
, u8 port_num
)
444 if (!can_support_obex(c
))
447 /* maybe allocate device-global string IDs, and patch descriptors */
448 if (obex_string_defs
[OBEX_CTRL_IDX
].id
== 0) {
449 status
= usb_string_id(c
->cdev
);
452 obex_string_defs
[OBEX_CTRL_IDX
].id
= status
;
454 obex_control_intf
.iInterface
= status
;
456 status
= usb_string_id(c
->cdev
);
459 obex_string_defs
[OBEX_DATA_IDX
].id
= status
;
461 obex_data_nop_intf
.iInterface
=
462 obex_data_intf
.iInterface
= status
;
465 /* allocate and initialize one new instance */
466 obex
= kzalloc(sizeof *obex
, GFP_KERNEL
);
470 obex
->port_num
= port_num
;
472 obex
->port
.connect
= obex_connect
;
473 obex
->port
.disconnect
= obex_disconnect
;
475 obex
->port
.func
.name
= "obex";
476 obex
->port
.func
.strings
= obex_strings
;
477 /* descriptors are per-instance copies */
478 obex
->port
.func
.bind
= obex_bind
;
479 obex
->port
.func
.unbind
= obex_unbind
;
480 obex
->port
.func
.set_alt
= obex_set_alt
;
481 obex
->port
.func
.get_alt
= obex_get_alt
;
482 obex
->port
.func
.disable
= obex_disable
;
484 status
= usb_add_function(c
, &obex
->port
.func
);
491 MODULE_AUTHOR("Felipe Balbi");
492 MODULE_LICENSE("GPL");