f_obex.c 13 KB

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