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.
15 /* #define VERBOSE_DEBUG */
17 #include <linux/slab.h>
18 #include <linux/kernel.h>
19 #include <linux/device.h>
20 #include <linux/module.h>
23 #include "gadget_chips.h"
27 * This CDC OBEX function support just packages a TTY-ish byte stream.
28 * A user mode server will put it into "raw" mode and handle all the
29 * relevant protocol details ... this is just a kernel passthrough.
30 * When possible, we prevent gadget enumeration until that server is
31 * ready to handle the commands.
42 static inline struct f_obex
*func_to_obex(struct usb_function
*f
)
44 return container_of(f
, struct f_obex
, port
.func
);
47 static inline struct f_obex
*port_to_obex(struct gserial
*p
)
49 return container_of(p
, struct f_obex
, port
);
52 /*-------------------------------------------------------------------------*/
54 #define OBEX_CTRL_IDX 0
55 #define OBEX_DATA_IDX 1
57 static struct usb_string obex_string_defs
[] = {
58 [OBEX_CTRL_IDX
].s
= "CDC Object Exchange (OBEX)",
59 [OBEX_DATA_IDX
].s
= "CDC OBEX Data",
60 { }, /* end of list */
63 static struct usb_gadget_strings obex_string_table
= {
64 .language
= 0x0409, /* en-US */
65 .strings
= obex_string_defs
,
68 static struct usb_gadget_strings
*obex_strings
[] = {
73 /*-------------------------------------------------------------------------*/
75 static struct usb_interface_descriptor obex_control_intf
= {
76 .bLength
= sizeof(obex_control_intf
),
77 .bDescriptorType
= USB_DT_INTERFACE
,
78 .bInterfaceNumber
= 0,
80 .bAlternateSetting
= 0,
82 .bInterfaceClass
= USB_CLASS_COMM
,
83 .bInterfaceSubClass
= USB_CDC_SUBCLASS_OBEX
,
86 static struct usb_interface_descriptor obex_data_nop_intf
= {
87 .bLength
= sizeof(obex_data_nop_intf
),
88 .bDescriptorType
= USB_DT_INTERFACE
,
89 .bInterfaceNumber
= 1,
91 .bAlternateSetting
= 0,
93 .bInterfaceClass
= USB_CLASS_CDC_DATA
,
96 static struct usb_interface_descriptor obex_data_intf
= {
97 .bLength
= sizeof(obex_data_intf
),
98 .bDescriptorType
= USB_DT_INTERFACE
,
99 .bInterfaceNumber
= 2,
101 .bAlternateSetting
= 1,
103 .bInterfaceClass
= USB_CLASS_CDC_DATA
,
106 static struct usb_cdc_header_desc obex_cdc_header_desc
= {
107 .bLength
= sizeof(obex_cdc_header_desc
),
108 .bDescriptorType
= USB_DT_CS_INTERFACE
,
109 .bDescriptorSubType
= USB_CDC_HEADER_TYPE
,
110 .bcdCDC
= cpu_to_le16(0x0120),
113 static struct usb_cdc_union_desc obex_cdc_union_desc
= {
114 .bLength
= sizeof(obex_cdc_union_desc
),
115 .bDescriptorType
= USB_DT_CS_INTERFACE
,
116 .bDescriptorSubType
= USB_CDC_UNION_TYPE
,
117 .bMasterInterface0
= 1,
118 .bSlaveInterface0
= 2,
121 static struct usb_cdc_obex_desc obex_desc
= {
122 .bLength
= sizeof(obex_desc
),
123 .bDescriptorType
= USB_DT_CS_INTERFACE
,
124 .bDescriptorSubType
= USB_CDC_OBEX_TYPE
,
125 .bcdVersion
= cpu_to_le16(0x0100),
128 /* High-Speed Support */
130 static struct usb_endpoint_descriptor obex_hs_ep_out_desc
= {
131 .bLength
= USB_DT_ENDPOINT_SIZE
,
132 .bDescriptorType
= USB_DT_ENDPOINT
,
134 .bEndpointAddress
= USB_DIR_OUT
,
135 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
136 .wMaxPacketSize
= cpu_to_le16(512),
139 static struct usb_endpoint_descriptor obex_hs_ep_in_desc
= {
140 .bLength
= USB_DT_ENDPOINT_SIZE
,
141 .bDescriptorType
= USB_DT_ENDPOINT
,
143 .bEndpointAddress
= USB_DIR_IN
,
144 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
145 .wMaxPacketSize
= cpu_to_le16(512),
148 static struct usb_descriptor_header
*hs_function
[] = {
149 (struct usb_descriptor_header
*) &obex_control_intf
,
150 (struct usb_descriptor_header
*) &obex_cdc_header_desc
,
151 (struct usb_descriptor_header
*) &obex_desc
,
152 (struct usb_descriptor_header
*) &obex_cdc_union_desc
,
154 (struct usb_descriptor_header
*) &obex_data_nop_intf
,
155 (struct usb_descriptor_header
*) &obex_data_intf
,
156 (struct usb_descriptor_header
*) &obex_hs_ep_in_desc
,
157 (struct usb_descriptor_header
*) &obex_hs_ep_out_desc
,
161 /* Full-Speed Support */
163 static struct usb_endpoint_descriptor obex_fs_ep_in_desc
= {
164 .bLength
= USB_DT_ENDPOINT_SIZE
,
165 .bDescriptorType
= USB_DT_ENDPOINT
,
167 .bEndpointAddress
= USB_DIR_IN
,
168 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
171 static struct usb_endpoint_descriptor obex_fs_ep_out_desc
= {
172 .bLength
= USB_DT_ENDPOINT_SIZE
,
173 .bDescriptorType
= USB_DT_ENDPOINT
,
175 .bEndpointAddress
= USB_DIR_OUT
,
176 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
179 static struct usb_descriptor_header
*fs_function
[] = {
180 (struct usb_descriptor_header
*) &obex_control_intf
,
181 (struct usb_descriptor_header
*) &obex_cdc_header_desc
,
182 (struct usb_descriptor_header
*) &obex_desc
,
183 (struct usb_descriptor_header
*) &obex_cdc_union_desc
,
185 (struct usb_descriptor_header
*) &obex_data_nop_intf
,
186 (struct usb_descriptor_header
*) &obex_data_intf
,
187 (struct usb_descriptor_header
*) &obex_fs_ep_in_desc
,
188 (struct usb_descriptor_header
*) &obex_fs_ep_out_desc
,
192 /*-------------------------------------------------------------------------*/
194 static int obex_set_alt(struct usb_function
*f
, unsigned intf
, unsigned alt
)
196 struct f_obex
*obex
= func_to_obex(f
);
197 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
199 if (intf
== obex
->ctrl_id
) {
203 dev_dbg(&cdev
->gadget
->dev
,
204 "reset obex ttyGS%d control\n", obex
->port_num
);
206 } else if (intf
== obex
->data_id
) {
210 if (obex
->port
.in
->driver_data
) {
211 dev_dbg(&cdev
->gadget
->dev
,
212 "reset obex ttyGS%d\n", obex
->port_num
);
213 gserial_disconnect(&obex
->port
);
216 if (!obex
->port
.in
->desc
|| !obex
->port
.out
->desc
) {
217 dev_dbg(&cdev
->gadget
->dev
,
218 "init obex ttyGS%d\n", obex
->port_num
);
219 if (config_ep_by_speed(cdev
->gadget
, f
,
221 config_ep_by_speed(cdev
->gadget
, f
,
223 obex
->port
.out
->desc
= NULL
;
224 obex
->port
.in
->desc
= NULL
;
230 dev_dbg(&cdev
->gadget
->dev
,
231 "activate obex ttyGS%d\n", obex
->port_num
);
232 gserial_connect(&obex
->port
, obex
->port_num
);
244 static int obex_get_alt(struct usb_function
*f
, unsigned intf
)
246 struct f_obex
*obex
= func_to_obex(f
);
248 if (intf
== obex
->ctrl_id
)
251 return obex
->port
.in
->driver_data
? 1 : 0;
254 static void obex_disable(struct usb_function
*f
)
256 struct f_obex
*obex
= func_to_obex(f
);
257 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
259 dev_dbg(&cdev
->gadget
->dev
, "obex ttyGS%d disable\n", obex
->port_num
);
260 gserial_disconnect(&obex
->port
);
263 /*-------------------------------------------------------------------------*/
265 static void obex_connect(struct gserial
*g
)
267 struct f_obex
*obex
= port_to_obex(g
);
268 struct usb_composite_dev
*cdev
= g
->func
.config
->cdev
;
271 if (!obex
->can_activate
)
274 status
= usb_function_activate(&g
->func
);
276 dev_dbg(&cdev
->gadget
->dev
,
277 "obex ttyGS%d function activate --> %d\n",
278 obex
->port_num
, status
);
281 static void obex_disconnect(struct gserial
*g
)
283 struct f_obex
*obex
= port_to_obex(g
);
284 struct usb_composite_dev
*cdev
= g
->func
.config
->cdev
;
287 if (!obex
->can_activate
)
290 status
= usb_function_deactivate(&g
->func
);
292 dev_dbg(&cdev
->gadget
->dev
,
293 "obex ttyGS%d function deactivate --> %d\n",
294 obex
->port_num
, status
);
297 /*-------------------------------------------------------------------------*/
299 /* Some controllers can't support CDC OBEX ... */
300 static inline bool can_support_obex(struct usb_configuration
*c
)
302 /* Since the first interface is a NOP, we can ignore the
303 * issue of multi-interface support on most controllers.
305 * Altsettings are mandatory, however...
307 if (!gadget_supports_altsettings(c
->cdev
->gadget
))
310 /* everything else is *probably* fine ... */
314 static int obex_bind(struct usb_configuration
*c
, struct usb_function
*f
)
316 struct usb_composite_dev
*cdev
= c
->cdev
;
317 struct f_obex
*obex
= func_to_obex(f
);
318 struct usb_string
*us
;
322 if (!can_support_obex(c
))
325 us
= usb_gstrings_attach(cdev
, obex_strings
,
326 ARRAY_SIZE(obex_string_defs
));
329 obex_control_intf
.iInterface
= us
[OBEX_CTRL_IDX
].id
;
330 obex_data_nop_intf
.iInterface
= us
[OBEX_DATA_IDX
].id
;
331 obex_data_intf
.iInterface
= us
[OBEX_DATA_IDX
].id
;
333 /* allocate instance-specific interface IDs, and patch descriptors */
335 status
= usb_interface_id(c
, f
);
338 obex
->ctrl_id
= status
;
340 obex_control_intf
.bInterfaceNumber
= status
;
341 obex_cdc_union_desc
.bMasterInterface0
= status
;
343 status
= usb_interface_id(c
, f
);
346 obex
->data_id
= status
;
348 obex_data_nop_intf
.bInterfaceNumber
= status
;
349 obex_data_intf
.bInterfaceNumber
= status
;
350 obex_cdc_union_desc
.bSlaveInterface0
= status
;
352 /* allocate instance-specific endpoints */
355 ep
= usb_ep_autoconfig(cdev
->gadget
, &obex_fs_ep_in_desc
);
359 ep
->driver_data
= cdev
; /* claim */
361 ep
= usb_ep_autoconfig(cdev
->gadget
, &obex_fs_ep_out_desc
);
365 ep
->driver_data
= cdev
; /* claim */
367 /* support all relevant hardware speeds... we expect that when
368 * hardware is dual speed, all bulk-capable endpoints work at
372 obex_hs_ep_in_desc
.bEndpointAddress
=
373 obex_fs_ep_in_desc
.bEndpointAddress
;
374 obex_hs_ep_out_desc
.bEndpointAddress
=
375 obex_fs_ep_out_desc
.bEndpointAddress
;
377 status
= usb_assign_descriptors(f
, fs_function
, hs_function
, NULL
);
381 /* Avoid letting this gadget enumerate until the userspace
382 * OBEX server is active.
384 status
= usb_function_deactivate(f
);
386 WARNING(cdev
, "obex ttyGS%d: can't prevent enumeration, %d\n",
387 obex
->port_num
, status
);
389 obex
->can_activate
= true;
392 dev_dbg(&cdev
->gadget
->dev
, "obex ttyGS%d: %s speed IN/%s OUT/%s\n",
394 gadget_is_dualspeed(c
->cdev
->gadget
) ? "dual" : "full",
395 obex
->port
.in
->name
, obex
->port
.out
->name
);
400 usb_free_all_descriptors(f
);
401 /* we might as well release our claims on endpoints */
403 obex
->port
.out
->driver_data
= NULL
;
405 obex
->port
.in
->driver_data
= NULL
;
407 ERROR(cdev
, "%s/%p: can't bind, err %d\n", f
->name
, f
, status
);
412 static inline struct f_serial_opts
*to_f_serial_opts(struct config_item
*item
)
414 return container_of(to_config_group(item
), struct f_serial_opts
,
418 CONFIGFS_ATTR_STRUCT(f_serial_opts
);
419 static ssize_t
f_obex_attr_show(struct config_item
*item
,
420 struct configfs_attribute
*attr
,
423 struct f_serial_opts
*opts
= to_f_serial_opts(item
);
424 struct f_serial_opts_attribute
*f_serial_opts_attr
=
425 container_of(attr
, struct f_serial_opts_attribute
, attr
);
428 if (f_serial_opts_attr
->show
)
429 ret
= f_serial_opts_attr
->show(opts
, page
);
434 static void obex_attr_release(struct config_item
*item
)
436 struct f_serial_opts
*opts
= to_f_serial_opts(item
);
438 usb_put_function_instance(&opts
->func_inst
);
441 static struct configfs_item_operations obex_item_ops
= {
442 .release
= obex_attr_release
,
443 .show_attribute
= f_obex_attr_show
,
446 static ssize_t
f_obex_port_num_show(struct f_serial_opts
*opts
, char *page
)
448 return sprintf(page
, "%u\n", opts
->port_num
);
451 static struct f_serial_opts_attribute f_obex_port_num
=
452 __CONFIGFS_ATTR_RO(port_num
, f_obex_port_num_show
);
454 static struct configfs_attribute
*acm_attrs
[] = {
455 &f_obex_port_num
.attr
,
459 static struct config_item_type obex_func_type
= {
460 .ct_item_ops
= &obex_item_ops
,
461 .ct_attrs
= acm_attrs
,
462 .ct_owner
= THIS_MODULE
,
465 static void obex_free_inst(struct usb_function_instance
*f
)
467 struct f_serial_opts
*opts
;
469 opts
= container_of(f
, struct f_serial_opts
, func_inst
);
470 gserial_free_line(opts
->port_num
);
474 static struct usb_function_instance
*obex_alloc_inst(void)
476 struct f_serial_opts
*opts
;
479 opts
= kzalloc(sizeof(*opts
), GFP_KERNEL
);
481 return ERR_PTR(-ENOMEM
);
483 opts
->func_inst
.free_func_inst
= obex_free_inst
;
484 ret
= gserial_alloc_line(&opts
->port_num
);
489 config_group_init_type_name(&opts
->func_inst
.group
, "",
492 return &opts
->func_inst
;
495 static void obex_free(struct usb_function
*f
)
499 obex
= func_to_obex(f
);
503 static void obex_unbind(struct usb_configuration
*c
, struct usb_function
*f
)
505 usb_free_all_descriptors(f
);
508 static struct usb_function
*obex_alloc(struct usb_function_instance
*fi
)
511 struct f_serial_opts
*opts
;
513 /* allocate and initialize one new instance */
514 obex
= kzalloc(sizeof(*obex
), GFP_KERNEL
);
516 return ERR_PTR(-ENOMEM
);
518 opts
= container_of(fi
, struct f_serial_opts
, func_inst
);
520 obex
->port_num
= opts
->port_num
;
522 obex
->port
.connect
= obex_connect
;
523 obex
->port
.disconnect
= obex_disconnect
;
525 obex
->port
.func
.name
= "obex";
526 /* descriptors are per-instance copies */
527 obex
->port
.func
.bind
= obex_bind
;
528 obex
->port
.func
.unbind
= obex_unbind
;
529 obex
->port
.func
.set_alt
= obex_set_alt
;
530 obex
->port
.func
.get_alt
= obex_get_alt
;
531 obex
->port
.func
.disable
= obex_disable
;
532 obex
->port
.func
.free_func
= obex_free
;
534 return &obex
->port
.func
;
537 DECLARE_USB_FUNCTION_INIT(obex
, obex_alloc_inst
, obex_alloc
);
538 MODULE_AUTHOR("Felipe Balbi");
539 MODULE_LICENSE("GPL");