[S390] Remove error checking from copy_oldmem_page()
[linux/fpc-iii.git] / drivers / usb / gadget / f_obex.c
blobe3f74bf5da2d835b18b284a4c6d51e03fdb7fbab
1 /*
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>
21 #include "u_serial.h"
22 #include "gadget_chips.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.
33 struct f_obex {
34 struct gserial port;
35 u8 ctrl_id;
36 u8 data_id;
37 u8 port_num;
38 u8 can_activate;
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[] = {
68 &obex_string_table,
69 NULL,
72 /*-------------------------------------------------------------------------*/
74 static struct usb_interface_descriptor obex_control_intf __initdata = {
75 .bLength = sizeof(obex_control_intf),
76 .bDescriptorType = USB_DT_INTERFACE,
77 .bInterfaceNumber = 0,
79 .bAlternateSetting = 0,
80 .bNumEndpoints = 0,
81 .bInterfaceClass = USB_CLASS_COMM,
82 .bInterfaceSubClass = USB_CDC_SUBCLASS_OBEX,
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,
90 .bAlternateSetting = 0,
91 .bNumEndpoints = 0,
92 .bInterfaceClass = USB_CLASS_CDC_DATA,
95 static struct usb_interface_descriptor obex_data_intf __initdata = {
96 .bLength = sizeof(obex_data_intf),
97 .bDescriptorType = USB_DT_INTERFACE,
98 .bInterfaceNumber = 2,
100 .bAlternateSetting = 1,
101 .bNumEndpoints = 2,
102 .bInterfaceClass = USB_CLASS_CDC_DATA,
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),
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,
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),
127 /* High-Speed Support */
129 static struct usb_endpoint_descriptor obex_hs_ep_out_desc __initdata = {
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 __initdata = {
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[] __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,
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,
160 /* Full-Speed Support */
162 static struct usb_endpoint_descriptor obex_fs_ep_in_desc __initdata = {
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 __initdata = {
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[] __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,
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,
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) {
199 if (alt != 0)
200 goto fail;
201 /* NOP */
202 DBG(cdev, "reset obex ttyGS%d control\n", obex->port_num);
204 } else if (intf == obex->data_id) {
205 if (alt > 1)
206 goto fail;
208 if (obex->port.in->driver_data) {
209 DBG(cdev, "reset obex ttyGS%d\n", obex->port_num);
210 gserial_disconnect(&obex->port);
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;
225 if (alt == 1) {
226 DBG(cdev, "activate obex ttyGS%d\n", obex->port_num);
227 gserial_connect(&obex->port, obex->port_num);
230 } else
231 goto fail;
233 return 0;
235 fail:
236 return -EINVAL;
239 static int obex_get_alt(struct usb_function *f, unsigned intf)
241 struct f_obex *obex = func_to_obex(f);
243 if (intf == obex->ctrl_id)
244 return 0;
246 return obex->port.in->driver_data ? 1 : 0;
249 static void obex_disable(struct usb_function *f)
251 struct f_obex *obex = func_to_obex(f);
252 struct usb_composite_dev *cdev = f->config->cdev;
254 DBG(cdev, "obex ttyGS%d disable\n", obex->port_num);
255 gserial_disconnect(&obex->port);
258 /*-------------------------------------------------------------------------*/
260 static void obex_connect(struct gserial *g)
262 struct f_obex *obex = port_to_obex(g);
263 struct usb_composite_dev *cdev = g->func.config->cdev;
264 int status;
266 if (!obex->can_activate)
267 return;
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);
275 static void obex_disconnect(struct gserial *g)
277 struct f_obex *obex = port_to_obex(g);
278 struct usb_composite_dev *cdev = g->func.config->cdev;
279 int status;
281 if (!obex->can_activate)
282 return;
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);
290 /*-------------------------------------------------------------------------*/
292 static int __init
293 obex_bind(struct usb_configuration *c, struct usb_function *f)
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;
300 /* allocate instance-specific interface IDs, and patch descriptors */
302 status = usb_interface_id(c, f);
303 if (status < 0)
304 goto fail;
305 obex->ctrl_id = status;
307 obex_control_intf.bInterfaceNumber = status;
308 obex_cdc_union_desc.bMasterInterface0 = status;
310 status = usb_interface_id(c, f);
311 if (status < 0)
312 goto fail;
313 obex->data_id = status;
315 obex_data_nop_intf.bInterfaceNumber = status;
316 obex_data_intf.bInterfaceNumber = status;
317 obex_cdc_union_desc.bSlaveInterface0 = status;
319 /* allocate instance-specific endpoints */
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 */
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 */
333 /* copy descriptors, and track endpoint copies */
334 f->descriptors = usb_copy_descriptors(fs_function);
336 /* support all relevant hardware speeds... we expect that when
337 * hardware is dual speed, all bulk-capable endpoints work at
338 * both speeds
340 if (gadget_is_dualspeed(c->cdev->gadget)) {
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;
347 /* copy descriptors, and track endpoint copies */
348 f->hs_descriptors = usb_copy_descriptors(hs_function);
351 /* Avoid letting this gadget enumerate until the userspace
352 * OBEX server is active.
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;
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);
367 return 0;
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;
376 ERROR(cdev, "%s/%p: can't bind, err %d\n", f->name, f, status);
378 return status;
381 static void
382 obex_unbind(struct usb_configuration *c, struct usb_function *f)
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));
390 /* Some controllers can't support CDC OBEX ... */
391 static inline bool can_support_obex(struct usb_configuration *c)
393 /* Since the first interface is a NOP, we can ignore the
394 * issue of multi-interface support on most controllers.
396 * Altsettings are mandatory, however...
398 if (!gadget_supports_altsettings(c->cdev->gadget))
399 return false;
401 /* everything else is *probably* fine ... */
402 return true;
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
411 * Returns zero on success, else negative errno.
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.
417 int __init obex_bind_config(struct usb_configuration *c, u8 port_num)
419 struct f_obex *obex;
420 int status;
422 if (!can_support_obex(c))
423 return -EINVAL;
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;
432 obex_control_intf.iInterface = status;
434 status = usb_string_id(c->cdev);
435 if (status < 0)
436 return status;
437 obex_string_defs[OBEX_DATA_IDX].id = status;
439 obex_data_nop_intf.iInterface =
440 obex_data_intf.iInterface = status;
443 /* allocate and initialize one new instance */
444 obex = kzalloc(sizeof *obex, GFP_KERNEL);
445 if (!obex)
446 return -ENOMEM;
448 obex->port_num = port_num;
450 obex->port.connect = obex_connect;
451 obex->port.disconnect = obex_disconnect;
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;
462 status = usb_add_function(c, &obex->port.func);
463 if (status)
464 kfree(obex);
466 return status;
469 MODULE_AUTHOR("Felipe Balbi");
470 MODULE_LICENSE("GPL");