blob: 394502abeb964a330130f981f0e9afd80d726930 [file] [log] [blame]
Felipe Balbi30867752008-08-18 17:39:30 -07001/*
2 * f_obex.c -- USB CDC OBEX function driver
3 *
4 * Copyright (C) 2008 Nokia Corporation
5 * Contact: Felipe Balbi <felipe.balbi@nokia.com>
6 *
7 * Based on f_acm.c by Al Borchers and David Brownell.
8 *
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.
13 *
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.
18 *
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
22 */
23
24/* #define VERBOSE_DEBUG */
25
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Felipe Balbi30867752008-08-18 17:39:30 -070027#include <linux/kernel.h>
Felipe Balbi30867752008-08-18 17:39:30 -070028#include <linux/device.h>
29
30#include "u_serial.h"
31#include "gadget_chips.h"
32
33
34/*
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.
David Brownell21214272008-08-18 17:45:25 -070038 * When possible, we prevent gadget enumeration until that server is
39 * ready to handle the commands.
Felipe Balbi30867752008-08-18 17:39:30 -070040 */
41
Felipe Balbi30867752008-08-18 17:39:30 -070042struct f_obex {
43 struct gserial port;
44 u8 ctrl_id;
45 u8 data_id;
46 u8 port_num;
David Brownell21214272008-08-18 17:45:25 -070047 u8 can_activate;
Felipe Balbi30867752008-08-18 17:39:30 -070048};
49
50static inline struct f_obex *func_to_obex(struct usb_function *f)
51{
52 return container_of(f, struct f_obex, port.func);
53}
54
David Brownell21214272008-08-18 17:45:25 -070055static inline struct f_obex *port_to_obex(struct gserial *p)
56{
57 return container_of(p, struct f_obex, port);
58}
59
Felipe Balbi30867752008-08-18 17:39:30 -070060/*-------------------------------------------------------------------------*/
61
62#define OBEX_CTRL_IDX 0
63#define OBEX_DATA_IDX 1
64
65static 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 */
69};
70
71static struct usb_gadget_strings obex_string_table = {
72 .language = 0x0409, /* en-US */
73 .strings = obex_string_defs,
74};
75
76static struct usb_gadget_strings *obex_strings[] = {
77 &obex_string_table,
78 NULL,
79};
80
81/*-------------------------------------------------------------------------*/
82
83static struct usb_interface_descriptor obex_control_intf __initdata = {
84 .bLength = sizeof(obex_control_intf),
85 .bDescriptorType = USB_DT_INTERFACE,
86 .bInterfaceNumber = 0,
87
88 .bAlternateSetting = 0,
89 .bNumEndpoints = 0,
90 .bInterfaceClass = USB_CLASS_COMM,
91 .bInterfaceSubClass = USB_CDC_SUBCLASS_OBEX,
92};
93
94static struct usb_interface_descriptor obex_data_nop_intf __initdata = {
95 .bLength = sizeof(obex_data_nop_intf),
96 .bDescriptorType = USB_DT_INTERFACE,
97 .bInterfaceNumber = 1,
98
99 .bAlternateSetting = 0,
100 .bNumEndpoints = 0,
101 .bInterfaceClass = USB_CLASS_CDC_DATA,
102};
103
104static struct usb_interface_descriptor obex_data_intf __initdata = {
105 .bLength = sizeof(obex_data_intf),
106 .bDescriptorType = USB_DT_INTERFACE,
107 .bInterfaceNumber = 2,
108
109 .bAlternateSetting = 1,
110 .bNumEndpoints = 2,
111 .bInterfaceClass = USB_CLASS_CDC_DATA,
112};
113
114static 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,
Harvey Harrison551509d2009-02-11 14:11:36 -0800118 .bcdCDC = cpu_to_le16(0x0120),
Felipe Balbi30867752008-08-18 17:39:30 -0700119};
120
121static 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,
127};
128
129static 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,
Harvey Harrison551509d2009-02-11 14:11:36 -0800133 .bcdVersion = cpu_to_le16(0x0100),
Felipe Balbi30867752008-08-18 17:39:30 -0700134};
135
136/* High-Speed Support */
137
138static struct usb_endpoint_descriptor obex_hs_ep_out_desc __initdata = {
139 .bLength = USB_DT_ENDPOINT_SIZE,
140 .bDescriptorType = USB_DT_ENDPOINT,
141
142 .bEndpointAddress = USB_DIR_OUT,
143 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Harvey Harrison551509d2009-02-11 14:11:36 -0800144 .wMaxPacketSize = cpu_to_le16(512),
Felipe Balbi30867752008-08-18 17:39:30 -0700145};
146
147static struct usb_endpoint_descriptor obex_hs_ep_in_desc __initdata = {
148 .bLength = USB_DT_ENDPOINT_SIZE,
149 .bDescriptorType = USB_DT_ENDPOINT,
150
151 .bEndpointAddress = USB_DIR_IN,
152 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Harvey Harrison551509d2009-02-11 14:11:36 -0800153 .wMaxPacketSize = cpu_to_le16(512),
Felipe Balbi30867752008-08-18 17:39:30 -0700154};
155
156static 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,
161
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,
167};
168
169/* Full-Speed Support */
170
171static struct usb_endpoint_descriptor obex_fs_ep_in_desc __initdata = {
172 .bLength = USB_DT_ENDPOINT_SIZE,
173 .bDescriptorType = USB_DT_ENDPOINT,
174
175 .bEndpointAddress = USB_DIR_IN,
176 .bmAttributes = USB_ENDPOINT_XFER_BULK,
177};
178
179static struct usb_endpoint_descriptor obex_fs_ep_out_desc __initdata = {
180 .bLength = USB_DT_ENDPOINT_SIZE,
181 .bDescriptorType = USB_DT_ENDPOINT,
182
183 .bEndpointAddress = USB_DIR_OUT,
184 .bmAttributes = USB_ENDPOINT_XFER_BULK,
185};
186
187static 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,
192
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,
198};
199
200/*-------------------------------------------------------------------------*/
201
202static int obex_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
203{
204 struct f_obex *obex = func_to_obex(f);
205 struct usb_composite_dev *cdev = f->config->cdev;
206
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);
212
213 } else if (intf == obex->data_id) {
214 if (alt > 1)
215 goto fail;
216
217 if (obex->port.in->driver_data) {
218 DBG(cdev, "reset obex ttyGS%d\n", obex->port_num);
219 gserial_disconnect(&obex->port);
220 }
221
Tatyana Brokhmanea2a1df72011-06-28 16:33:50 +0300222 if (!obex->port.in->desc || !obex->port.out->desc) {
Felipe Balbi30867752008-08-18 17:39:30 -0700223 DBG(cdev, "init obex ttyGS%d\n", obex->port_num);
Tatyana Brokhmanea2a1df72011-06-28 16:33:50 +0300224 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;
231 }
Felipe Balbi30867752008-08-18 17:39:30 -0700232 }
233
234 if (alt == 1) {
235 DBG(cdev, "activate obex ttyGS%d\n", obex->port_num);
236 gserial_connect(&obex->port, obex->port_num);
237 }
238
239 } else
240 goto fail;
241
242 return 0;
243
244fail:
245 return -EINVAL;
246}
247
248static int obex_get_alt(struct usb_function *f, unsigned intf)
249{
250 struct f_obex *obex = func_to_obex(f);
251
252 if (intf == obex->ctrl_id)
253 return 0;
254
255 return obex->port.in->driver_data ? 1 : 0;
256}
257
258static void obex_disable(struct usb_function *f)
259{
260 struct f_obex *obex = func_to_obex(f);
261 struct usb_composite_dev *cdev = f->config->cdev;
262
263 DBG(cdev, "obex ttyGS%d disable\n", obex->port_num);
264 gserial_disconnect(&obex->port);
265}
266
267/*-------------------------------------------------------------------------*/
268
David Brownell21214272008-08-18 17:45:25 -0700269static void obex_connect(struct gserial *g)
270{
271 struct f_obex *obex = port_to_obex(g);
272 struct usb_composite_dev *cdev = g->func.config->cdev;
273 int status;
274
275 if (!obex->can_activate)
276 return;
277
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);
282}
283
284static void obex_disconnect(struct gserial *g)
285{
286 struct f_obex *obex = port_to_obex(g);
287 struct usb_composite_dev *cdev = g->func.config->cdev;
288 int status;
289
290 if (!obex->can_activate)
291 return;
292
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);
297}
298
299/*-------------------------------------------------------------------------*/
300
Felipe Balbi30867752008-08-18 17:39:30 -0700301static int __init
302obex_bind(struct usb_configuration *c, struct usb_function *f)
303{
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;
308
309 /* allocate instance-specific interface IDs, and patch descriptors */
310
311 status = usb_interface_id(c, f);
312 if (status < 0)
313 goto fail;
314 obex->ctrl_id = status;
315
316 obex_control_intf.bInterfaceNumber = status;
317 obex_cdc_union_desc.bMasterInterface0 = status;
318
319 status = usb_interface_id(c, f);
320 if (status < 0)
321 goto fail;
322 obex->data_id = status;
323
324 obex_data_nop_intf.bInterfaceNumber = status;
325 obex_data_intf.bInterfaceNumber = status;
326 obex_cdc_union_desc.bSlaveInterface0 = status;
327
328 /* allocate instance-specific endpoints */
329
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 */
335
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 */
341
342 /* copy descriptors, and track endpoint copies */
343 f->descriptors = usb_copy_descriptors(fs_function);
344
Felipe Balbi30867752008-08-18 17:39:30 -0700345 /* support all relevant hardware speeds... we expect that when
346 * hardware is dual speed, all bulk-capable endpoints work at
347 * both speeds
348 */
349 if (gadget_is_dualspeed(c->cdev->gadget)) {
350
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;
355
356 /* copy descriptors, and track endpoint copies */
357 f->hs_descriptors = usb_copy_descriptors(hs_function);
Felipe Balbi30867752008-08-18 17:39:30 -0700358 }
359
David Brownell21214272008-08-18 17:45:25 -0700360 /* Avoid letting this gadget enumerate until the userspace
361 * OBEX server is active.
362 */
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;
369
370
Felipe Balbi30867752008-08-18 17:39:30 -0700371 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);
375
376 return 0;
377
378fail:
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;
384
385 ERROR(cdev, "%s/%p: can't bind, err %d\n", f->name, f, status);
386
387 return status;
388}
389
390static void
391obex_unbind(struct usb_configuration *c, struct usb_function *f)
392{
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));
397}
398
399/* Some controllers can't support CDC OBEX ... */
400static inline bool can_support_obex(struct usb_configuration *c)
401{
402 /* Since the first interface is a NOP, we can ignore the
403 * issue of multi-interface support on most controllers.
404 *
405 * Altsettings are mandatory, however...
406 */
407 if (!gadget_supports_altsettings(c->cdev->gadget))
408 return false;
409
410 /* everything else is *probably* fine ... */
411 return true;
412}
413
414/**
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
419 *
420 * Returns zero on success, else negative errno.
421 *
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.
425 */
426int __init obex_bind_config(struct usb_configuration *c, u8 port_num)
427{
428 struct f_obex *obex;
429 int status;
430
431 if (!can_support_obex(c))
432 return -EINVAL;
433
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;
440
441 obex_control_intf.iInterface = status;
442
443 status = usb_string_id(c->cdev);
444 if (status < 0)
445 return status;
446 obex_string_defs[OBEX_DATA_IDX].id = status;
447
448 obex_data_nop_intf.iInterface =
449 obex_data_intf.iInterface = status;
450 }
451
452 /* allocate and initialize one new instance */
453 obex = kzalloc(sizeof *obex, GFP_KERNEL);
454 if (!obex)
455 return -ENOMEM;
456
457 obex->port_num = port_num;
458
David Brownell21214272008-08-18 17:45:25 -0700459 obex->port.connect = obex_connect;
460 obex->port.disconnect = obex_disconnect;
461
Felipe Balbi30867752008-08-18 17:39:30 -0700462 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;
470
471 status = usb_add_function(c, &obex->port.func);
472 if (status)
473 kfree(obex);
474
475 return status;
476}
477
478MODULE_AUTHOR("Felipe Balbi");
479MODULE_LICENSE("GPL");