f_obex.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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. /* #define VERBOSE_DEBUG */
  15. #include <linux/slab.h>
  16. #include <linux/kernel.h>
  17. #include <linux/device.h>
  18. #include "u_serial.h"
  19. #include "gadget_chips.h"
  20. /*
  21. * This CDC OBEX function support just packages a TTY-ish byte stream.
  22. * A user mode server will put it into "raw" mode and handle all the
  23. * relevant protocol details ... this is just a kernel passthrough.
  24. * When possible, we prevent gadget enumeration until that server is
  25. * ready to handle the commands.
  26. */
  27. struct f_obex {
  28. struct gserial port;
  29. u8 ctrl_id;
  30. u8 data_id;
  31. u8 port_num;
  32. u8 can_activate;
  33. };
  34. static inline struct f_obex *func_to_obex(struct usb_function *f)
  35. {
  36. return container_of(f, struct f_obex, port.func);
  37. }
  38. static inline struct f_obex *port_to_obex(struct gserial *p)
  39. {
  40. return container_of(p, struct f_obex, port);
  41. }
  42. /*-------------------------------------------------------------------------*/
  43. #define OBEX_CTRL_IDX 0
  44. #define OBEX_DATA_IDX 1
  45. static struct usb_string obex_string_defs[] = {
  46. [OBEX_CTRL_IDX].s = "CDC Object Exchange (OBEX)",
  47. [OBEX_DATA_IDX].s = "CDC OBEX Data",
  48. { }, /* end of list */
  49. };
  50. static struct usb_gadget_strings obex_string_table = {
  51. .language = 0x0409, /* en-US */
  52. .strings = obex_string_defs,
  53. };
  54. static struct usb_gadget_strings *obex_strings[] = {
  55. &obex_string_table,
  56. NULL,
  57. };
  58. /*-------------------------------------------------------------------------*/
  59. static struct usb_interface_descriptor obex_control_intf __initdata = {
  60. .bLength = sizeof(obex_control_intf),
  61. .bDescriptorType = USB_DT_INTERFACE,
  62. .bInterfaceNumber = 0,
  63. .bAlternateSetting = 0,
  64. .bNumEndpoints = 0,
  65. .bInterfaceClass = USB_CLASS_COMM,
  66. .bInterfaceSubClass = USB_CDC_SUBCLASS_OBEX,
  67. };
  68. static struct usb_interface_descriptor obex_data_nop_intf __initdata = {
  69. .bLength = sizeof(obex_data_nop_intf),
  70. .bDescriptorType = USB_DT_INTERFACE,
  71. .bInterfaceNumber = 1,
  72. .bAlternateSetting = 0,
  73. .bNumEndpoints = 0,
  74. .bInterfaceClass = USB_CLASS_CDC_DATA,
  75. };
  76. static struct usb_interface_descriptor obex_data_intf __initdata = {
  77. .bLength = sizeof(obex_data_intf),
  78. .bDescriptorType = USB_DT_INTERFACE,
  79. .bInterfaceNumber = 2,
  80. .bAlternateSetting = 1,
  81. .bNumEndpoints = 2,
  82. .bInterfaceClass = USB_CLASS_CDC_DATA,
  83. };
  84. static struct usb_cdc_header_desc obex_cdc_header_desc __initdata = {
  85. .bLength = sizeof(obex_cdc_header_desc),
  86. .bDescriptorType = USB_DT_CS_INTERFACE,
  87. .bDescriptorSubType = USB_CDC_HEADER_TYPE,
  88. .bcdCDC = cpu_to_le16(0x0120),
  89. };
  90. static struct usb_cdc_union_desc obex_cdc_union_desc __initdata = {
  91. .bLength = sizeof(obex_cdc_union_desc),
  92. .bDescriptorType = USB_DT_CS_INTERFACE,
  93. .bDescriptorSubType = USB_CDC_UNION_TYPE,
  94. .bMasterInterface0 = 1,
  95. .bSlaveInterface0 = 2,
  96. };
  97. static struct usb_cdc_obex_desc obex_desc __initdata = {
  98. .bLength = sizeof(obex_desc),
  99. .bDescriptorType = USB_DT_CS_INTERFACE,
  100. .bDescriptorSubType = USB_CDC_OBEX_TYPE,
  101. .bcdVersion = cpu_to_le16(0x0100),
  102. };
  103. /* High-Speed Support */
  104. static struct usb_endpoint_descriptor obex_hs_ep_out_desc __initdata = {
  105. .bLength = USB_DT_ENDPOINT_SIZE,
  106. .bDescriptorType = USB_DT_ENDPOINT,
  107. .bEndpointAddress = USB_DIR_OUT,
  108. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  109. .wMaxPacketSize = cpu_to_le16(512),
  110. };
  111. static struct usb_endpoint_descriptor obex_hs_ep_in_desc __initdata = {
  112. .bLength = USB_DT_ENDPOINT_SIZE,
  113. .bDescriptorType = USB_DT_ENDPOINT,
  114. .bEndpointAddress = USB_DIR_IN,
  115. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  116. .wMaxPacketSize = cpu_to_le16(512),
  117. };
  118. static struct usb_descriptor_header *hs_function[] __initdata = {
  119. (struct usb_descriptor_header *) &obex_control_intf,
  120. (struct usb_descriptor_header *) &obex_cdc_header_desc,
  121. (struct usb_descriptor_header *) &obex_desc,
  122. (struct usb_descriptor_header *) &obex_cdc_union_desc,
  123. (struct usb_descriptor_header *) &obex_data_nop_intf,
  124. (struct usb_descriptor_header *) &obex_data_intf,
  125. (struct usb_descriptor_header *) &obex_hs_ep_in_desc,
  126. (struct usb_descriptor_header *) &obex_hs_ep_out_desc,
  127. NULL,
  128. };
  129. /* Full-Speed Support */
  130. static struct usb_endpoint_descriptor obex_fs_ep_in_desc __initdata = {
  131. .bLength = USB_DT_ENDPOINT_SIZE,
  132. .bDescriptorType = USB_DT_ENDPOINT,
  133. .bEndpointAddress = USB_DIR_IN,
  134. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  135. };
  136. static struct usb_endpoint_descriptor obex_fs_ep_out_desc __initdata = {
  137. .bLength = USB_DT_ENDPOINT_SIZE,
  138. .bDescriptorType = USB_DT_ENDPOINT,
  139. .bEndpointAddress = USB_DIR_OUT,
  140. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  141. };
  142. static struct usb_descriptor_header *fs_function[] __initdata = {
  143. (struct usb_descriptor_header *) &obex_control_intf,
  144. (struct usb_descriptor_header *) &obex_cdc_header_desc,
  145. (struct usb_descriptor_header *) &obex_desc,
  146. (struct usb_descriptor_header *) &obex_cdc_union_desc,
  147. (struct usb_descriptor_header *) &obex_data_nop_intf,
  148. (struct usb_descriptor_header *) &obex_data_intf,
  149. (struct usb_descriptor_header *) &obex_fs_ep_in_desc,
  150. (struct usb_descriptor_header *) &obex_fs_ep_out_desc,
  151. NULL,
  152. };
  153. /*-------------------------------------------------------------------------*/
  154. static int obex_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
  155. {
  156. struct f_obex *obex = func_to_obex(f);
  157. struct usb_composite_dev *cdev = f->config->cdev;
  158. if (intf == obex->ctrl_id) {
  159. if (alt != 0)
  160. goto fail;
  161. /* NOP */
  162. DBG(cdev, "reset obex ttyGS%d control\n", obex->port_num);
  163. } else if (intf == obex->data_id) {
  164. if (alt > 1)
  165. goto fail;
  166. if (obex->port.in->driver_data) {
  167. DBG(cdev, "reset obex ttyGS%d\n", obex->port_num);
  168. gserial_disconnect(&obex->port);
  169. }
  170. if (!obex->port.in->desc || !obex->port.out->desc) {
  171. DBG(cdev, "init obex ttyGS%d\n", obex->port_num);
  172. if (config_ep_by_speed(cdev->gadget, f,
  173. obex->port.in) ||
  174. config_ep_by_speed(cdev->gadget, f,
  175. obex->port.out)) {
  176. obex->port.out->desc = NULL;
  177. obex->port.in->desc = NULL;
  178. goto fail;
  179. }
  180. }
  181. if (alt == 1) {
  182. DBG(cdev, "activate obex ttyGS%d\n", obex->port_num);
  183. gserial_connect(&obex->port, obex->port_num);
  184. }
  185. } else
  186. goto fail;
  187. return 0;
  188. fail:
  189. return -EINVAL;
  190. }
  191. static int obex_get_alt(struct usb_function *f, unsigned intf)
  192. {
  193. struct f_obex *obex = func_to_obex(f);
  194. if (intf == obex->ctrl_id)
  195. return 0;
  196. return obex->port.in->driver_data ? 1 : 0;
  197. }
  198. static void obex_disable(struct usb_function *f)
  199. {
  200. struct f_obex *obex = func_to_obex(f);
  201. struct usb_composite_dev *cdev = f->config->cdev;
  202. DBG(cdev, "obex ttyGS%d disable\n", obex->port_num);
  203. gserial_disconnect(&obex->port);
  204. }
  205. /*-------------------------------------------------------------------------*/
  206. static void obex_connect(struct gserial *g)
  207. {
  208. struct f_obex *obex = port_to_obex(g);
  209. struct usb_composite_dev *cdev = g->func.config->cdev;
  210. int status;
  211. if (!obex->can_activate)
  212. return;
  213. status = usb_function_activate(&g->func);
  214. if (status)
  215. DBG(cdev, "obex ttyGS%d function activate --> %d\n",
  216. obex->port_num, status);
  217. }
  218. static void obex_disconnect(struct gserial *g)
  219. {
  220. struct f_obex *obex = port_to_obex(g);
  221. struct usb_composite_dev *cdev = g->func.config->cdev;
  222. int status;
  223. if (!obex->can_activate)
  224. return;
  225. status = usb_function_deactivate(&g->func);
  226. if (status)
  227. DBG(cdev, "obex ttyGS%d function deactivate --> %d\n",
  228. obex->port_num, status);
  229. }
  230. /*-------------------------------------------------------------------------*/
  231. static int __init
  232. obex_bind(struct usb_configuration *c, struct usb_function *f)
  233. {
  234. struct usb_composite_dev *cdev = c->cdev;
  235. struct f_obex *obex = func_to_obex(f);
  236. int status;
  237. struct usb_ep *ep;
  238. /* allocate instance-specific interface IDs, and patch descriptors */
  239. status = usb_interface_id(c, f);
  240. if (status < 0)
  241. goto fail;
  242. obex->ctrl_id = status;
  243. obex_control_intf.bInterfaceNumber = status;
  244. obex_cdc_union_desc.bMasterInterface0 = status;
  245. status = usb_interface_id(c, f);
  246. if (status < 0)
  247. goto fail;
  248. obex->data_id = status;
  249. obex_data_nop_intf.bInterfaceNumber = status;
  250. obex_data_intf.bInterfaceNumber = status;
  251. obex_cdc_union_desc.bSlaveInterface0 = status;
  252. /* allocate instance-specific endpoints */
  253. ep = usb_ep_autoconfig(cdev->gadget, &obex_fs_ep_in_desc);
  254. if (!ep)
  255. goto fail;
  256. obex->port.in = ep;
  257. ep->driver_data = cdev; /* claim */
  258. ep = usb_ep_autoconfig(cdev->gadget, &obex_fs_ep_out_desc);
  259. if (!ep)
  260. goto fail;
  261. obex->port.out = ep;
  262. ep->driver_data = cdev; /* claim */
  263. /* copy descriptors, and track endpoint copies */
  264. f->descriptors = usb_copy_descriptors(fs_function);
  265. /* support all relevant hardware speeds... we expect that when
  266. * hardware is dual speed, all bulk-capable endpoints work at
  267. * both speeds
  268. */
  269. if (gadget_is_dualspeed(c->cdev->gadget)) {
  270. obex_hs_ep_in_desc.bEndpointAddress =
  271. obex_fs_ep_in_desc.bEndpointAddress;
  272. obex_hs_ep_out_desc.bEndpointAddress =
  273. obex_fs_ep_out_desc.bEndpointAddress;
  274. /* copy descriptors, and track endpoint copies */
  275. f->hs_descriptors = usb_copy_descriptors(hs_function);
  276. }
  277. /* Avoid letting this gadget enumerate until the userspace
  278. * OBEX server is active.
  279. */
  280. status = usb_function_deactivate(f);
  281. if (status < 0)
  282. WARNING(cdev, "obex ttyGS%d: can't prevent enumeration, %d\n",
  283. obex->port_num, status);
  284. else
  285. obex->can_activate = true;
  286. DBG(cdev, "obex ttyGS%d: %s speed IN/%s OUT/%s\n",
  287. obex->port_num,
  288. gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
  289. obex->port.in->name, obex->port.out->name);
  290. return 0;
  291. fail:
  292. /* we might as well release our claims on endpoints */
  293. if (obex->port.out)
  294. obex->port.out->driver_data = NULL;
  295. if (obex->port.in)
  296. obex->port.in->driver_data = NULL;
  297. ERROR(cdev, "%s/%p: can't bind, err %d\n", f->name, f, status);
  298. return status;
  299. }
  300. static void
  301. obex_unbind(struct usb_configuration *c, struct usb_function *f)
  302. {
  303. if (gadget_is_dualspeed(c->cdev->gadget))
  304. usb_free_descriptors(f->hs_descriptors);
  305. usb_free_descriptors(f->descriptors);
  306. kfree(func_to_obex(f));
  307. }
  308. /* Some controllers can't support CDC OBEX ... */
  309. static inline bool can_support_obex(struct usb_configuration *c)
  310. {
  311. /* Since the first interface is a NOP, we can ignore the
  312. * issue of multi-interface support on most controllers.
  313. *
  314. * Altsettings are mandatory, however...
  315. */
  316. if (!gadget_supports_altsettings(c->cdev->gadget))
  317. return false;
  318. /* everything else is *probably* fine ... */
  319. return true;
  320. }
  321. /**
  322. * obex_bind_config - add a CDC OBEX function to a configuration
  323. * @c: the configuration to support the CDC OBEX instance
  324. * @port_num: /dev/ttyGS* port this interface will use
  325. * Context: single threaded during gadget setup
  326. *
  327. * Returns zero on success, else negative errno.
  328. *
  329. * Caller must have called @gserial_setup() with enough ports to
  330. * handle all the ones it binds. Caller is also responsible
  331. * for calling @gserial_cleanup() before module unload.
  332. */
  333. int __init obex_bind_config(struct usb_configuration *c, u8 port_num)
  334. {
  335. struct f_obex *obex;
  336. int status;
  337. if (!can_support_obex(c))
  338. return -EINVAL;
  339. /* maybe allocate device-global string IDs, and patch descriptors */
  340. if (obex_string_defs[OBEX_CTRL_IDX].id == 0) {
  341. status = usb_string_id(c->cdev);
  342. if (status < 0)
  343. return status;
  344. obex_string_defs[OBEX_CTRL_IDX].id = status;
  345. obex_control_intf.iInterface = status;
  346. status = usb_string_id(c->cdev);
  347. if (status < 0)
  348. return status;
  349. obex_string_defs[OBEX_DATA_IDX].id = status;
  350. obex_data_nop_intf.iInterface =
  351. obex_data_intf.iInterface = status;
  352. }
  353. /* allocate and initialize one new instance */
  354. obex = kzalloc(sizeof *obex, GFP_KERNEL);
  355. if (!obex)
  356. return -ENOMEM;
  357. obex->port_num = port_num;
  358. obex->port.connect = obex_connect;
  359. obex->port.disconnect = obex_disconnect;
  360. obex->port.func.name = "obex";
  361. obex->port.func.strings = obex_strings;
  362. /* descriptors are per-instance copies */
  363. obex->port.func.bind = obex_bind;
  364. obex->port.func.unbind = obex_unbind;
  365. obex->port.func.set_alt = obex_set_alt;
  366. obex->port.func.get_alt = obex_get_alt;
  367. obex->port.func.disable = obex_disable;
  368. status = usb_add_function(c, &obex->port.func);
  369. if (status)
  370. kfree(obex);
  371. return status;
  372. }
  373. MODULE_AUTHOR("Felipe Balbi");
  374. MODULE_LICENSE("GPL");