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>
26 * This CDC OBEX function support just packages a TTY-ish byte stream.
27 * A user mode server will put it into "raw" mode and handle all the
28 * relevant protocol details ... this is just a kernel passthrough.
29 * When possible, we prevent gadget enumeration until that server is
30 * ready to handle the commands.
41 static inline struct f_obex
*func_to_obex(struct usb_function
*f
)
43 return container_of(f
, struct f_obex
, port
.func
);
46 static inline struct f_obex
*port_to_obex(struct gserial
*p
)
48 return container_of(p
, struct f_obex
, port
);
51 /*-------------------------------------------------------------------------*/
53 #define OBEX_CTRL_IDX 0
54 #define OBEX_DATA_IDX 1
56 static struct usb_string obex_string_defs
[] = {
57 [OBEX_CTRL_IDX
].s
= "CDC Object Exchange (OBEX)",
58 [OBEX_DATA_IDX
].s
= "CDC OBEX Data",
59 { }, /* end of list */
62 static struct usb_gadget_strings obex_string_table
= {
63 .language
= 0x0409, /* en-US */
64 .strings
= obex_string_defs
,
67 static struct usb_gadget_strings
*obex_strings
[] = {
72 /*-------------------------------------------------------------------------*/
74 static struct usb_interface_descriptor obex_control_intf
= {
75 .bLength
= sizeof(obex_control_intf
),
76 .bDescriptorType
= USB_DT_INTERFACE
,
77 .bInterfaceNumber
= 0,
79 .bAlternateSetting
= 0,
81 .bInterfaceClass
= USB_CLASS_COMM
,
82 .bInterfaceSubClass
= USB_CDC_SUBCLASS_OBEX
,
85 static struct usb_interface_descriptor obex_data_nop_intf
= {
86 .bLength
= sizeof(obex_data_nop_intf
),
87 .bDescriptorType
= USB_DT_INTERFACE
,
88 .bInterfaceNumber
= 1,
90 .bAlternateSetting
= 0,
92 .bInterfaceClass
= USB_CLASS_CDC_DATA
,
95 static struct usb_interface_descriptor obex_data_intf
= {
96 .bLength
= sizeof(obex_data_intf
),
97 .bDescriptorType
= USB_DT_INTERFACE
,
98 .bInterfaceNumber
= 2,
100 .bAlternateSetting
= 1,
102 .bInterfaceClass
= USB_CLASS_CDC_DATA
,
105 static struct usb_cdc_header_desc obex_cdc_header_desc
= {
106 .bLength
= sizeof(obex_cdc_header_desc
),
107 .bDescriptorType
= USB_DT_CS_INTERFACE
,
108 .bDescriptorSubType
= USB_CDC_HEADER_TYPE
,
109 .bcdCDC
= cpu_to_le16(0x0120),
112 static struct usb_cdc_union_desc obex_cdc_union_desc
= {
113 .bLength
= sizeof(obex_cdc_union_desc
),
114 .bDescriptorType
= USB_DT_CS_INTERFACE
,
115 .bDescriptorSubType
= USB_CDC_UNION_TYPE
,
116 .bMasterInterface0
= 1,
117 .bSlaveInterface0
= 2,
120 static struct usb_cdc_obex_desc obex_desc
= {
121 .bLength
= sizeof(obex_desc
),
122 .bDescriptorType
= USB_DT_CS_INTERFACE
,
123 .bDescriptorSubType
= USB_CDC_OBEX_TYPE
,
124 .bcdVersion
= cpu_to_le16(0x0100),
127 /* High-Speed Support */
129 static struct usb_endpoint_descriptor obex_hs_ep_out_desc
= {
130 .bLength
= USB_DT_ENDPOINT_SIZE
,
131 .bDescriptorType
= USB_DT_ENDPOINT
,
133 .bEndpointAddress
= USB_DIR_OUT
,
134 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
135 .wMaxPacketSize
= cpu_to_le16(512),
138 static struct usb_endpoint_descriptor obex_hs_ep_in_desc
= {
139 .bLength
= USB_DT_ENDPOINT_SIZE
,
140 .bDescriptorType
= USB_DT_ENDPOINT
,
142 .bEndpointAddress
= USB_DIR_IN
,
143 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
144 .wMaxPacketSize
= cpu_to_le16(512),
147 static struct usb_descriptor_header
*hs_function
[] = {
148 (struct usb_descriptor_header
*) &obex_control_intf
,
149 (struct usb_descriptor_header
*) &obex_cdc_header_desc
,
150 (struct usb_descriptor_header
*) &obex_desc
,
151 (struct usb_descriptor_header
*) &obex_cdc_union_desc
,
153 (struct usb_descriptor_header
*) &obex_data_nop_intf
,
154 (struct usb_descriptor_header
*) &obex_data_intf
,
155 (struct usb_descriptor_header
*) &obex_hs_ep_in_desc
,
156 (struct usb_descriptor_header
*) &obex_hs_ep_out_desc
,
160 /* Full-Speed Support */
162 static struct usb_endpoint_descriptor obex_fs_ep_in_desc
= {
163 .bLength
= USB_DT_ENDPOINT_SIZE
,
164 .bDescriptorType
= USB_DT_ENDPOINT
,
166 .bEndpointAddress
= USB_DIR_IN
,
167 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
170 static struct usb_endpoint_descriptor obex_fs_ep_out_desc
= {
171 .bLength
= USB_DT_ENDPOINT_SIZE
,
172 .bDescriptorType
= USB_DT_ENDPOINT
,
174 .bEndpointAddress
= USB_DIR_OUT
,
175 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
178 static struct usb_descriptor_header
*fs_function
[] = {
179 (struct usb_descriptor_header
*) &obex_control_intf
,
180 (struct usb_descriptor_header
*) &obex_cdc_header_desc
,
181 (struct usb_descriptor_header
*) &obex_desc
,
182 (struct usb_descriptor_header
*) &obex_cdc_union_desc
,
184 (struct usb_descriptor_header
*) &obex_data_nop_intf
,
185 (struct usb_descriptor_header
*) &obex_data_intf
,
186 (struct usb_descriptor_header
*) &obex_fs_ep_in_desc
,
187 (struct usb_descriptor_header
*) &obex_fs_ep_out_desc
,
191 /*-------------------------------------------------------------------------*/
193 static int obex_set_alt(struct usb_function
*f
, unsigned intf
, unsigned alt
)
195 struct f_obex
*obex
= func_to_obex(f
);
196 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
198 if (intf
== obex
->ctrl_id
) {
202 dev_dbg(&cdev
->gadget
->dev
,
203 "reset obex ttyGS%d control\n", obex
->port_num
);
205 } else if (intf
== obex
->data_id
) {
209 if (obex
->port
.in
->enabled
) {
210 dev_dbg(&cdev
->gadget
->dev
,
211 "reset obex ttyGS%d\n", obex
->port_num
);
212 gserial_disconnect(&obex
->port
);
215 if (!obex
->port
.in
->desc
|| !obex
->port
.out
->desc
) {
216 dev_dbg(&cdev
->gadget
->dev
,
217 "init obex ttyGS%d\n", obex
->port_num
);
218 if (config_ep_by_speed(cdev
->gadget
, f
,
220 config_ep_by_speed(cdev
->gadget
, f
,
222 obex
->port
.out
->desc
= NULL
;
223 obex
->port
.in
->desc
= NULL
;
229 dev_dbg(&cdev
->gadget
->dev
,
230 "activate obex ttyGS%d\n", obex
->port_num
);
231 gserial_connect(&obex
->port
, obex
->port_num
);
245 static int obex_get_alt(struct usb_function
*f
, unsigned intf
)
247 struct f_obex
*obex
= func_to_obex(f
);
249 return obex
->cur_alt
;
252 static void obex_disable(struct usb_function
*f
)
254 struct f_obex
*obex
= func_to_obex(f
);
255 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
257 dev_dbg(&cdev
->gadget
->dev
, "obex ttyGS%d disable\n", obex
->port_num
);
258 gserial_disconnect(&obex
->port
);
261 /*-------------------------------------------------------------------------*/
263 static void obex_connect(struct gserial
*g
)
265 struct f_obex
*obex
= port_to_obex(g
);
266 struct usb_composite_dev
*cdev
= g
->func
.config
->cdev
;
269 status
= usb_function_activate(&g
->func
);
271 dev_dbg(&cdev
->gadget
->dev
,
272 "obex ttyGS%d function activate --> %d\n",
273 obex
->port_num
, status
);
276 static void obex_disconnect(struct gserial
*g
)
278 struct f_obex
*obex
= port_to_obex(g
);
279 struct usb_composite_dev
*cdev
= g
->func
.config
->cdev
;
282 status
= usb_function_deactivate(&g
->func
);
284 dev_dbg(&cdev
->gadget
->dev
,
285 "obex ttyGS%d function deactivate --> %d\n",
286 obex
->port_num
, status
);
289 /*-------------------------------------------------------------------------*/
291 /* Some controllers can't support CDC OBEX ... */
292 static inline bool can_support_obex(struct usb_configuration
*c
)
294 /* Since the first interface is a NOP, we can ignore the
295 * issue of multi-interface support on most controllers.
297 * Altsettings are mandatory, however...
299 if (!gadget_is_altset_supported(c
->cdev
->gadget
))
302 /* everything else is *probably* fine ... */
306 static int 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
);
310 struct usb_string
*us
;
314 if (!can_support_obex(c
))
317 us
= usb_gstrings_attach(cdev
, obex_strings
,
318 ARRAY_SIZE(obex_string_defs
));
321 obex_control_intf
.iInterface
= us
[OBEX_CTRL_IDX
].id
;
322 obex_data_nop_intf
.iInterface
= us
[OBEX_DATA_IDX
].id
;
323 obex_data_intf
.iInterface
= us
[OBEX_DATA_IDX
].id
;
325 /* allocate instance-specific interface IDs, and patch descriptors */
327 status
= usb_interface_id(c
, f
);
330 obex
->ctrl_id
= status
;
332 obex_control_intf
.bInterfaceNumber
= status
;
333 obex_cdc_union_desc
.bMasterInterface0
= status
;
335 status
= usb_interface_id(c
, f
);
338 obex
->data_id
= status
;
340 obex_data_nop_intf
.bInterfaceNumber
= status
;
341 obex_data_intf
.bInterfaceNumber
= status
;
342 obex_cdc_union_desc
.bSlaveInterface0
= status
;
344 /* allocate instance-specific endpoints */
347 ep
= usb_ep_autoconfig(cdev
->gadget
, &obex_fs_ep_in_desc
);
352 ep
= usb_ep_autoconfig(cdev
->gadget
, &obex_fs_ep_out_desc
);
357 /* support all relevant hardware speeds... we expect that when
358 * hardware is dual speed, all bulk-capable endpoints work at
362 obex_hs_ep_in_desc
.bEndpointAddress
=
363 obex_fs_ep_in_desc
.bEndpointAddress
;
364 obex_hs_ep_out_desc
.bEndpointAddress
=
365 obex_fs_ep_out_desc
.bEndpointAddress
;
367 status
= usb_assign_descriptors(f
, fs_function
, hs_function
, NULL
,
372 dev_dbg(&cdev
->gadget
->dev
, "obex ttyGS%d: %s speed IN/%s OUT/%s\n",
374 gadget_is_dualspeed(c
->cdev
->gadget
) ? "dual" : "full",
375 obex
->port
.in
->name
, obex
->port
.out
->name
);
380 ERROR(cdev
, "%s/%p: can't bind, err %d\n", f
->name
, f
, status
);
385 static inline struct f_serial_opts
*to_f_serial_opts(struct config_item
*item
)
387 return container_of(to_config_group(item
), struct f_serial_opts
,
391 static void obex_attr_release(struct config_item
*item
)
393 struct f_serial_opts
*opts
= to_f_serial_opts(item
);
395 usb_put_function_instance(&opts
->func_inst
);
398 static struct configfs_item_operations obex_item_ops
= {
399 .release
= obex_attr_release
,
402 static ssize_t
f_obex_port_num_show(struct config_item
*item
, char *page
)
404 return sprintf(page
, "%u\n", to_f_serial_opts(item
)->port_num
);
407 CONFIGFS_ATTR_RO(f_obex_
, port_num
);
409 static struct configfs_attribute
*acm_attrs
[] = {
410 &f_obex_attr_port_num
,
414 static struct config_item_type obex_func_type
= {
415 .ct_item_ops
= &obex_item_ops
,
416 .ct_attrs
= acm_attrs
,
417 .ct_owner
= THIS_MODULE
,
420 static void obex_free_inst(struct usb_function_instance
*f
)
422 struct f_serial_opts
*opts
;
424 opts
= container_of(f
, struct f_serial_opts
, func_inst
);
425 gserial_free_line(opts
->port_num
);
429 static struct usb_function_instance
*obex_alloc_inst(void)
431 struct f_serial_opts
*opts
;
434 opts
= kzalloc(sizeof(*opts
), GFP_KERNEL
);
436 return ERR_PTR(-ENOMEM
);
438 opts
->func_inst
.free_func_inst
= obex_free_inst
;
439 ret
= gserial_alloc_line(&opts
->port_num
);
444 config_group_init_type_name(&opts
->func_inst
.group
, "",
447 return &opts
->func_inst
;
450 static void obex_free(struct usb_function
*f
)
454 obex
= func_to_obex(f
);
458 static void obex_unbind(struct usb_configuration
*c
, struct usb_function
*f
)
460 usb_free_all_descriptors(f
);
463 static struct usb_function
*obex_alloc(struct usb_function_instance
*fi
)
466 struct f_serial_opts
*opts
;
468 /* allocate and initialize one new instance */
469 obex
= kzalloc(sizeof(*obex
), GFP_KERNEL
);
471 return ERR_PTR(-ENOMEM
);
473 opts
= container_of(fi
, struct f_serial_opts
, func_inst
);
475 obex
->port_num
= opts
->port_num
;
477 obex
->port
.connect
= obex_connect
;
478 obex
->port
.disconnect
= obex_disconnect
;
480 obex
->port
.func
.name
= "obex";
481 /* descriptors are per-instance copies */
482 obex
->port
.func
.bind
= obex_bind
;
483 obex
->port
.func
.unbind
= obex_unbind
;
484 obex
->port
.func
.set_alt
= obex_set_alt
;
485 obex
->port
.func
.get_alt
= obex_get_alt
;
486 obex
->port
.func
.disable
= obex_disable
;
487 obex
->port
.func
.free_func
= obex_free
;
488 obex
->port
.func
.bind_deactivated
= true;
490 return &obex
->port
.func
;
493 DECLARE_USB_FUNCTION_INIT(obex
, obex_alloc_inst
, obex_alloc
);
494 MODULE_AUTHOR("Felipe Balbi");
495 MODULE_LICENSE("GPL");