]> Pileus Git - ~andy/linux/blob - drivers/usb/gadget/f_obex.c
Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/padovan/blueto...
[~andy/linux] / drivers / usb / gadget / f_obex.c
1 /*
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
15 /* #define VERBOSE_DEBUG */
16
17 #include <linux/slab.h>
18 #include <linux/kernel.h>
19 #include <linux/device.h>
20
21 #include "u_serial.h"
22 #include "gadget_chips.h"
23
24
25 /*
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.
31  */
32
33 struct f_obex {
34         struct gserial                  port;
35         u8                              ctrl_id;
36         u8                              data_id;
37         u8                              port_num;
38         u8                              can_activate;
39 };
40
41 static inline struct f_obex *func_to_obex(struct usb_function *f)
42 {
43         return container_of(f, struct f_obex, port.func);
44 }
45
46 static inline struct f_obex *port_to_obex(struct gserial *p)
47 {
48         return container_of(p, struct f_obex, port);
49 }
50
51 /*-------------------------------------------------------------------------*/
52
53 #define OBEX_CTRL_IDX   0
54 #define OBEX_DATA_IDX   1
55
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 */
60 };
61
62 static struct usb_gadget_strings obex_string_table = {
63         .language               = 0x0409,       /* en-US */
64         .strings                = obex_string_defs,
65 };
66
67 static struct usb_gadget_strings *obex_strings[] = {
68         &obex_string_table,
69         NULL,
70 };
71
72 /*-------------------------------------------------------------------------*/
73
74 static struct usb_interface_descriptor obex_control_intf __initdata = {
75         .bLength                = sizeof(obex_control_intf),
76         .bDescriptorType        = USB_DT_INTERFACE,
77         .bInterfaceNumber       = 0,
78
79         .bAlternateSetting      = 0,
80         .bNumEndpoints          = 0,
81         .bInterfaceClass        = USB_CLASS_COMM,
82         .bInterfaceSubClass     = USB_CDC_SUBCLASS_OBEX,
83 };
84
85 static struct usb_interface_descriptor obex_data_nop_intf __initdata = {
86         .bLength                = sizeof(obex_data_nop_intf),
87         .bDescriptorType        = USB_DT_INTERFACE,
88         .bInterfaceNumber       = 1,
89
90         .bAlternateSetting      = 0,
91         .bNumEndpoints          = 0,
92         .bInterfaceClass        = USB_CLASS_CDC_DATA,
93 };
94
95 static struct usb_interface_descriptor obex_data_intf __initdata = {
96         .bLength                = sizeof(obex_data_intf),
97         .bDescriptorType        = USB_DT_INTERFACE,
98         .bInterfaceNumber       = 2,
99
100         .bAlternateSetting      = 1,
101         .bNumEndpoints          = 2,
102         .bInterfaceClass        = USB_CLASS_CDC_DATA,
103 };
104
105 static struct usb_cdc_header_desc obex_cdc_header_desc __initdata = {
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),
110 };
111
112 static struct usb_cdc_union_desc obex_cdc_union_desc __initdata = {
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,
118 };
119
120 static struct usb_cdc_obex_desc obex_desc __initdata = {
121         .bLength                = sizeof(obex_desc),
122         .bDescriptorType        = USB_DT_CS_INTERFACE,
123         .bDescriptorSubType     = USB_CDC_OBEX_TYPE,
124         .bcdVersion             = cpu_to_le16(0x0100),
125 };
126
127 /* High-Speed Support */
128
129 static struct usb_endpoint_descriptor obex_hs_ep_out_desc __initdata = {
130         .bLength                = USB_DT_ENDPOINT_SIZE,
131         .bDescriptorType        = USB_DT_ENDPOINT,
132
133         .bEndpointAddress       = USB_DIR_OUT,
134         .bmAttributes           = USB_ENDPOINT_XFER_BULK,
135         .wMaxPacketSize         = cpu_to_le16(512),
136 };
137
138 static struct usb_endpoint_descriptor obex_hs_ep_in_desc __initdata = {
139         .bLength                = USB_DT_ENDPOINT_SIZE,
140         .bDescriptorType        = USB_DT_ENDPOINT,
141
142         .bEndpointAddress       = USB_DIR_IN,
143         .bmAttributes           = USB_ENDPOINT_XFER_BULK,
144         .wMaxPacketSize         = cpu_to_le16(512),
145 };
146
147 static struct usb_descriptor_header *hs_function[] __initdata = {
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,
152
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,
157         NULL,
158 };
159
160 /* Full-Speed Support */
161
162 static struct usb_endpoint_descriptor obex_fs_ep_in_desc __initdata = {
163         .bLength                = USB_DT_ENDPOINT_SIZE,
164         .bDescriptorType        = USB_DT_ENDPOINT,
165
166         .bEndpointAddress       = USB_DIR_IN,
167         .bmAttributes           = USB_ENDPOINT_XFER_BULK,
168 };
169
170 static struct usb_endpoint_descriptor obex_fs_ep_out_desc __initdata = {
171         .bLength                = USB_DT_ENDPOINT_SIZE,
172         .bDescriptorType        = USB_DT_ENDPOINT,
173
174         .bEndpointAddress       = USB_DIR_OUT,
175         .bmAttributes           = USB_ENDPOINT_XFER_BULK,
176 };
177
178 static struct usb_descriptor_header *fs_function[] __initdata = {
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,
183
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,
188         NULL,
189 };
190
191 /*-------------------------------------------------------------------------*/
192
193 static int obex_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
194 {
195         struct f_obex           *obex = func_to_obex(f);
196         struct usb_composite_dev *cdev = f->config->cdev;
197
198         if (intf == obex->ctrl_id) {
199                 if (alt != 0)
200                         goto fail;
201                 /* NOP */
202                 DBG(cdev, "reset obex ttyGS%d control\n", obex->port_num);
203
204         } else if (intf == obex->data_id) {
205                 if (alt > 1)
206                         goto fail;
207
208                 if (obex->port.in->driver_data) {
209                         DBG(cdev, "reset obex ttyGS%d\n", obex->port_num);
210                         gserial_disconnect(&obex->port);
211                 }
212
213                 if (!obex->port.in->desc || !obex->port.out->desc) {
214                         DBG(cdev, "init obex ttyGS%d\n", obex->port_num);
215                         if (config_ep_by_speed(cdev->gadget, f,
216                                                obex->port.in) ||
217                             config_ep_by_speed(cdev->gadget, f,
218                                                obex->port.out)) {
219                                 obex->port.out->desc = NULL;
220                                 obex->port.in->desc = NULL;
221                                 goto fail;
222                         }
223                 }
224
225                 if (alt == 1) {
226                         DBG(cdev, "activate obex ttyGS%d\n", obex->port_num);
227                         gserial_connect(&obex->port, obex->port_num);
228                 }
229
230         } else
231                 goto fail;
232
233         return 0;
234
235 fail:
236         return -EINVAL;
237 }
238
239 static int obex_get_alt(struct usb_function *f, unsigned intf)
240 {
241         struct f_obex           *obex = func_to_obex(f);
242
243         if (intf == obex->ctrl_id)
244                 return 0;
245
246         return obex->port.in->driver_data ? 1 : 0;
247 }
248
249 static void obex_disable(struct usb_function *f)
250 {
251         struct f_obex   *obex = func_to_obex(f);
252         struct usb_composite_dev *cdev = f->config->cdev;
253
254         DBG(cdev, "obex ttyGS%d disable\n", obex->port_num);
255         gserial_disconnect(&obex->port);
256 }
257
258 /*-------------------------------------------------------------------------*/
259
260 static void obex_connect(struct gserial *g)
261 {
262         struct f_obex           *obex = port_to_obex(g);
263         struct usb_composite_dev *cdev = g->func.config->cdev;
264         int                     status;
265
266         if (!obex->can_activate)
267                 return;
268
269         status = usb_function_activate(&g->func);
270         if (status)
271                 DBG(cdev, "obex ttyGS%d function activate --> %d\n",
272                         obex->port_num, status);
273 }
274
275 static void obex_disconnect(struct gserial *g)
276 {
277         struct f_obex           *obex = port_to_obex(g);
278         struct usb_composite_dev *cdev = g->func.config->cdev;
279         int                     status;
280
281         if (!obex->can_activate)
282                 return;
283
284         status = usb_function_deactivate(&g->func);
285         if (status)
286                 DBG(cdev, "obex ttyGS%d function deactivate --> %d\n",
287                         obex->port_num, status);
288 }
289
290 /*-------------------------------------------------------------------------*/
291
292 static int __init
293 obex_bind(struct usb_configuration *c, struct usb_function *f)
294 {
295         struct usb_composite_dev *cdev = c->cdev;
296         struct f_obex           *obex = func_to_obex(f);
297         int                     status;
298         struct usb_ep           *ep;
299
300         /* allocate instance-specific interface IDs, and patch descriptors */
301
302         status = usb_interface_id(c, f);
303         if (status < 0)
304                 goto fail;
305         obex->ctrl_id = status;
306
307         obex_control_intf.bInterfaceNumber = status;
308         obex_cdc_union_desc.bMasterInterface0 = status;
309
310         status = usb_interface_id(c, f);
311         if (status < 0)
312                 goto fail;
313         obex->data_id = status;
314
315         obex_data_nop_intf.bInterfaceNumber = status;
316         obex_data_intf.bInterfaceNumber = status;
317         obex_cdc_union_desc.bSlaveInterface0 = status;
318
319         /* allocate instance-specific endpoints */
320
321         ep = usb_ep_autoconfig(cdev->gadget, &obex_fs_ep_in_desc);
322         if (!ep)
323                 goto fail;
324         obex->port.in = ep;
325         ep->driver_data = cdev; /* claim */
326
327         ep = usb_ep_autoconfig(cdev->gadget, &obex_fs_ep_out_desc);
328         if (!ep)
329                 goto fail;
330         obex->port.out = ep;
331         ep->driver_data = cdev; /* claim */
332
333         /* copy descriptors, and track endpoint copies */
334         f->descriptors = usb_copy_descriptors(fs_function);
335
336         /* support all relevant hardware speeds... we expect that when
337          * hardware is dual speed, all bulk-capable endpoints work at
338          * both speeds
339          */
340         if (gadget_is_dualspeed(c->cdev->gadget)) {
341
342                 obex_hs_ep_in_desc.bEndpointAddress =
343                                 obex_fs_ep_in_desc.bEndpointAddress;
344                 obex_hs_ep_out_desc.bEndpointAddress =
345                                 obex_fs_ep_out_desc.bEndpointAddress;
346
347                 /* copy descriptors, and track endpoint copies */
348                 f->hs_descriptors = usb_copy_descriptors(hs_function);
349         }
350
351         /* Avoid letting this gadget enumerate until the userspace
352          * OBEX server is active.
353          */
354         status = usb_function_deactivate(f);
355         if (status < 0)
356                 WARNING(cdev, "obex ttyGS%d: can't prevent enumeration, %d\n",
357                         obex->port_num, status);
358         else
359                 obex->can_activate = true;
360
361
362         DBG(cdev, "obex ttyGS%d: %s speed IN/%s OUT/%s\n",
363                         obex->port_num,
364                         gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
365                         obex->port.in->name, obex->port.out->name);
366
367         return 0;
368
369 fail:
370         /* we might as well release our claims on endpoints */
371         if (obex->port.out)
372                 obex->port.out->driver_data = NULL;
373         if (obex->port.in)
374                 obex->port.in->driver_data = NULL;
375
376         ERROR(cdev, "%s/%p: can't bind, err %d\n", f->name, f, status);
377
378         return status;
379 }
380
381 static void
382 obex_unbind(struct usb_configuration *c, struct usb_function *f)
383 {
384         if (gadget_is_dualspeed(c->cdev->gadget))
385                 usb_free_descriptors(f->hs_descriptors);
386         usb_free_descriptors(f->descriptors);
387         kfree(func_to_obex(f));
388 }
389
390 /* Some controllers can't support CDC OBEX ... */
391 static inline bool can_support_obex(struct usb_configuration *c)
392 {
393         /* Since the first interface is a NOP, we can ignore the
394          * issue of multi-interface support on most controllers.
395          *
396          * Altsettings are mandatory, however...
397          */
398         if (!gadget_supports_altsettings(c->cdev->gadget))
399                 return false;
400
401         /* everything else is *probably* fine ... */
402         return true;
403 }
404
405 /**
406  * obex_bind_config - add a CDC OBEX function to a configuration
407  * @c: the configuration to support the CDC OBEX instance
408  * @port_num: /dev/ttyGS* port this interface will use
409  * Context: single threaded during gadget setup
410  *
411  * Returns zero on success, else negative errno.
412  *
413  * Caller must have called @gserial_setup() with enough ports to
414  * handle all the ones it binds.  Caller is also responsible
415  * for calling @gserial_cleanup() before module unload.
416  */
417 int __init obex_bind_config(struct usb_configuration *c, u8 port_num)
418 {
419         struct f_obex   *obex;
420         int             status;
421
422         if (!can_support_obex(c))
423                 return -EINVAL;
424
425         /* maybe allocate device-global string IDs, and patch descriptors */
426         if (obex_string_defs[OBEX_CTRL_IDX].id == 0) {
427                 status = usb_string_id(c->cdev);
428                 if (status < 0)
429                         return status;
430                 obex_string_defs[OBEX_CTRL_IDX].id = status;
431
432                 obex_control_intf.iInterface = status;
433
434                 status = usb_string_id(c->cdev);
435                 if (status < 0)
436                         return status;
437                 obex_string_defs[OBEX_DATA_IDX].id = status;
438
439                 obex_data_nop_intf.iInterface =
440                         obex_data_intf.iInterface = status;
441         }
442
443         /* allocate and initialize one new instance */
444         obex = kzalloc(sizeof *obex, GFP_KERNEL);
445         if (!obex)
446                 return -ENOMEM;
447
448         obex->port_num = port_num;
449
450         obex->port.connect = obex_connect;
451         obex->port.disconnect = obex_disconnect;
452
453         obex->port.func.name = "obex";
454         obex->port.func.strings = obex_strings;
455         /* descriptors are per-instance copies */
456         obex->port.func.bind = obex_bind;
457         obex->port.func.unbind = obex_unbind;
458         obex->port.func.set_alt = obex_set_alt;
459         obex->port.func.get_alt = obex_get_alt;
460         obex->port.func.disable = obex_disable;
461
462         status = usb_add_function(c, &obex->port.func);
463         if (status)
464                 kfree(obex);
465
466         return status;
467 }
468
469 MODULE_AUTHOR("Felipe Balbi");
470 MODULE_LICENSE("GPL");