f_obex.c 13 KB

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