f_obex.c 12 KB

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