f_obex.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  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 cur_alt;
  33. u8 port_num;
  34. u8 can_activate;
  35. };
  36. static inline struct f_obex *func_to_obex(struct usb_function *f)
  37. {
  38. return container_of(f, struct f_obex, port.func);
  39. }
  40. static inline struct f_obex *port_to_obex(struct gserial *p)
  41. {
  42. return container_of(p, struct f_obex, port);
  43. }
  44. /*-------------------------------------------------------------------------*/
  45. #define OBEX_CTRL_IDX 0
  46. #define OBEX_DATA_IDX 1
  47. static struct usb_string obex_string_defs[] = {
  48. [OBEX_CTRL_IDX].s = "CDC Object Exchange (OBEX)",
  49. [OBEX_DATA_IDX].s = "CDC OBEX Data",
  50. { }, /* end of list */
  51. };
  52. static struct usb_gadget_strings obex_string_table = {
  53. .language = 0x0409, /* en-US */
  54. .strings = obex_string_defs,
  55. };
  56. static struct usb_gadget_strings *obex_strings[] = {
  57. &obex_string_table,
  58. NULL,
  59. };
  60. /*-------------------------------------------------------------------------*/
  61. static struct usb_interface_descriptor obex_control_intf = {
  62. .bLength = sizeof(obex_control_intf),
  63. .bDescriptorType = USB_DT_INTERFACE,
  64. .bInterfaceNumber = 0,
  65. .bAlternateSetting = 0,
  66. .bNumEndpoints = 0,
  67. .bInterfaceClass = USB_CLASS_COMM,
  68. .bInterfaceSubClass = USB_CDC_SUBCLASS_OBEX,
  69. };
  70. static struct usb_interface_descriptor obex_data_nop_intf = {
  71. .bLength = sizeof(obex_data_nop_intf),
  72. .bDescriptorType = USB_DT_INTERFACE,
  73. .bInterfaceNumber = 1,
  74. .bAlternateSetting = 0,
  75. .bNumEndpoints = 0,
  76. .bInterfaceClass = USB_CLASS_CDC_DATA,
  77. };
  78. static struct usb_interface_descriptor obex_data_intf = {
  79. .bLength = sizeof(obex_data_intf),
  80. .bDescriptorType = USB_DT_INTERFACE,
  81. .bInterfaceNumber = 2,
  82. .bAlternateSetting = 1,
  83. .bNumEndpoints = 2,
  84. .bInterfaceClass = USB_CLASS_CDC_DATA,
  85. };
  86. static struct usb_cdc_header_desc obex_cdc_header_desc = {
  87. .bLength = sizeof(obex_cdc_header_desc),
  88. .bDescriptorType = USB_DT_CS_INTERFACE,
  89. .bDescriptorSubType = USB_CDC_HEADER_TYPE,
  90. .bcdCDC = cpu_to_le16(0x0120),
  91. };
  92. static struct usb_cdc_union_desc obex_cdc_union_desc = {
  93. .bLength = sizeof(obex_cdc_union_desc),
  94. .bDescriptorType = USB_DT_CS_INTERFACE,
  95. .bDescriptorSubType = USB_CDC_UNION_TYPE,
  96. .bMasterInterface0 = 1,
  97. .bSlaveInterface0 = 2,
  98. };
  99. static struct usb_cdc_obex_desc obex_desc = {
  100. .bLength = sizeof(obex_desc),
  101. .bDescriptorType = USB_DT_CS_INTERFACE,
  102. .bDescriptorSubType = USB_CDC_OBEX_TYPE,
  103. .bcdVersion = cpu_to_le16(0x0100),
  104. };
  105. /* High-Speed Support */
  106. static struct usb_endpoint_descriptor obex_hs_ep_out_desc = {
  107. .bLength = USB_DT_ENDPOINT_SIZE,
  108. .bDescriptorType = USB_DT_ENDPOINT,
  109. .bEndpointAddress = USB_DIR_OUT,
  110. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  111. .wMaxPacketSize = cpu_to_le16(512),
  112. };
  113. static struct usb_endpoint_descriptor obex_hs_ep_in_desc = {
  114. .bLength = USB_DT_ENDPOINT_SIZE,
  115. .bDescriptorType = USB_DT_ENDPOINT,
  116. .bEndpointAddress = USB_DIR_IN,
  117. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  118. .wMaxPacketSize = cpu_to_le16(512),
  119. };
  120. static struct usb_descriptor_header *hs_function[] = {
  121. (struct usb_descriptor_header *) &obex_control_intf,
  122. (struct usb_descriptor_header *) &obex_cdc_header_desc,
  123. (struct usb_descriptor_header *) &obex_desc,
  124. (struct usb_descriptor_header *) &obex_cdc_union_desc,
  125. (struct usb_descriptor_header *) &obex_data_nop_intf,
  126. (struct usb_descriptor_header *) &obex_data_intf,
  127. (struct usb_descriptor_header *) &obex_hs_ep_in_desc,
  128. (struct usb_descriptor_header *) &obex_hs_ep_out_desc,
  129. NULL,
  130. };
  131. /* Full-Speed Support */
  132. static struct usb_endpoint_descriptor obex_fs_ep_in_desc = {
  133. .bLength = USB_DT_ENDPOINT_SIZE,
  134. .bDescriptorType = USB_DT_ENDPOINT,
  135. .bEndpointAddress = USB_DIR_IN,
  136. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  137. };
  138. static struct usb_endpoint_descriptor obex_fs_ep_out_desc = {
  139. .bLength = USB_DT_ENDPOINT_SIZE,
  140. .bDescriptorType = USB_DT_ENDPOINT,
  141. .bEndpointAddress = USB_DIR_OUT,
  142. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  143. };
  144. static struct usb_descriptor_header *fs_function[] = {
  145. (struct usb_descriptor_header *) &obex_control_intf,
  146. (struct usb_descriptor_header *) &obex_cdc_header_desc,
  147. (struct usb_descriptor_header *) &obex_desc,
  148. (struct usb_descriptor_header *) &obex_cdc_union_desc,
  149. (struct usb_descriptor_header *) &obex_data_nop_intf,
  150. (struct usb_descriptor_header *) &obex_data_intf,
  151. (struct usb_descriptor_header *) &obex_fs_ep_in_desc,
  152. (struct usb_descriptor_header *) &obex_fs_ep_out_desc,
  153. NULL,
  154. };
  155. /*-------------------------------------------------------------------------*/
  156. static int obex_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
  157. {
  158. struct f_obex *obex = func_to_obex(f);
  159. struct usb_composite_dev *cdev = f->config->cdev;
  160. if (intf == obex->ctrl_id) {
  161. if (alt != 0)
  162. goto fail;
  163. /* NOP */
  164. dev_dbg(&cdev->gadget->dev,
  165. "reset obex ttyGS%d control\n", obex->port_num);
  166. } else if (intf == obex->data_id) {
  167. if (alt > 1)
  168. goto fail;
  169. if (obex->port.in->driver_data) {
  170. dev_dbg(&cdev->gadget->dev,
  171. "reset obex ttyGS%d\n", obex->port_num);
  172. gserial_disconnect(&obex->port);
  173. }
  174. if (!obex->port.in->desc || !obex->port.out->desc) {
  175. dev_dbg(&cdev->gadget->dev,
  176. "init obex ttyGS%d\n", obex->port_num);
  177. if (config_ep_by_speed(cdev->gadget, f,
  178. obex->port.in) ||
  179. config_ep_by_speed(cdev->gadget, f,
  180. obex->port.out)) {
  181. obex->port.out->desc = NULL;
  182. obex->port.in->desc = NULL;
  183. goto fail;
  184. }
  185. }
  186. if (alt == 1) {
  187. dev_dbg(&cdev->gadget->dev,
  188. "activate obex ttyGS%d\n", obex->port_num);
  189. gserial_connect(&obex->port, obex->port_num);
  190. }
  191. } else
  192. goto fail;
  193. obex->cur_alt = alt;
  194. return 0;
  195. fail:
  196. return -EINVAL;
  197. }
  198. static int obex_get_alt(struct usb_function *f, unsigned intf)
  199. {
  200. struct f_obex *obex = func_to_obex(f);
  201. return obex->cur_alt;
  202. }
  203. static void obex_disable(struct usb_function *f)
  204. {
  205. struct f_obex *obex = func_to_obex(f);
  206. struct usb_composite_dev *cdev = f->config->cdev;
  207. dev_dbg(&cdev->gadget->dev, "obex ttyGS%d disable\n", obex->port_num);
  208. gserial_disconnect(&obex->port);
  209. }
  210. /*-------------------------------------------------------------------------*/
  211. static void obex_connect(struct gserial *g)
  212. {
  213. struct f_obex *obex = port_to_obex(g);
  214. struct usb_composite_dev *cdev = g->func.config->cdev;
  215. int status;
  216. if (!obex->can_activate)
  217. return;
  218. status = usb_function_activate(&g->func);
  219. if (status)
  220. dev_dbg(&cdev->gadget->dev,
  221. "obex ttyGS%d function activate --> %d\n",
  222. obex->port_num, status);
  223. }
  224. static void obex_disconnect(struct gserial *g)
  225. {
  226. struct f_obex *obex = port_to_obex(g);
  227. struct usb_composite_dev *cdev = g->func.config->cdev;
  228. int status;
  229. if (!obex->can_activate)
  230. return;
  231. status = usb_function_deactivate(&g->func);
  232. if (status)
  233. dev_dbg(&cdev->gadget->dev,
  234. "obex ttyGS%d function deactivate --> %d\n",
  235. obex->port_num, status);
  236. }
  237. /*-------------------------------------------------------------------------*/
  238. /* Some controllers can't support CDC OBEX ... */
  239. static inline bool can_support_obex(struct usb_configuration *c)
  240. {
  241. /* Since the first interface is a NOP, we can ignore the
  242. * issue of multi-interface support on most controllers.
  243. *
  244. * Altsettings are mandatory, however...
  245. */
  246. if (!gadget_supports_altsettings(c->cdev->gadget))
  247. return false;
  248. /* everything else is *probably* fine ... */
  249. return true;
  250. }
  251. static int obex_bind(struct usb_configuration *c, struct usb_function *f)
  252. {
  253. struct usb_composite_dev *cdev = c->cdev;
  254. struct f_obex *obex = func_to_obex(f);
  255. struct usb_string *us;
  256. int status;
  257. struct usb_ep *ep;
  258. if (!can_support_obex(c))
  259. return -EINVAL;
  260. us = usb_gstrings_attach(cdev, obex_strings,
  261. ARRAY_SIZE(obex_string_defs));
  262. if (IS_ERR(us))
  263. return PTR_ERR(us);
  264. obex_control_intf.iInterface = us[OBEX_CTRL_IDX].id;
  265. obex_data_nop_intf.iInterface = us[OBEX_DATA_IDX].id;
  266. obex_data_intf.iInterface = us[OBEX_DATA_IDX].id;
  267. /* allocate instance-specific interface IDs, and patch descriptors */
  268. status = usb_interface_id(c, f);
  269. if (status < 0)
  270. goto fail;
  271. obex->ctrl_id = status;
  272. obex_control_intf.bInterfaceNumber = status;
  273. obex_cdc_union_desc.bMasterInterface0 = status;
  274. status = usb_interface_id(c, f);
  275. if (status < 0)
  276. goto fail;
  277. obex->data_id = status;
  278. obex_data_nop_intf.bInterfaceNumber = status;
  279. obex_data_intf.bInterfaceNumber = status;
  280. obex_cdc_union_desc.bSlaveInterface0 = status;
  281. /* allocate instance-specific endpoints */
  282. status = -ENODEV;
  283. ep = usb_ep_autoconfig(cdev->gadget, &obex_fs_ep_in_desc);
  284. if (!ep)
  285. goto fail;
  286. obex->port.in = ep;
  287. ep->driver_data = cdev; /* claim */
  288. ep = usb_ep_autoconfig(cdev->gadget, &obex_fs_ep_out_desc);
  289. if (!ep)
  290. goto fail;
  291. obex->port.out = ep;
  292. ep->driver_data = cdev; /* claim */
  293. /* support all relevant hardware speeds... we expect that when
  294. * hardware is dual speed, all bulk-capable endpoints work at
  295. * both speeds
  296. */
  297. obex_hs_ep_in_desc.bEndpointAddress =
  298. obex_fs_ep_in_desc.bEndpointAddress;
  299. obex_hs_ep_out_desc.bEndpointAddress =
  300. obex_fs_ep_out_desc.bEndpointAddress;
  301. status = usb_assign_descriptors(f, fs_function, hs_function, NULL);
  302. if (status)
  303. goto fail;
  304. /* Avoid letting this gadget enumerate until the userspace
  305. * OBEX server is active.
  306. */
  307. status = usb_function_deactivate(f);
  308. if (status < 0)
  309. WARNING(cdev, "obex ttyGS%d: can't prevent enumeration, %d\n",
  310. obex->port_num, status);
  311. else
  312. obex->can_activate = true;
  313. dev_dbg(&cdev->gadget->dev, "obex ttyGS%d: %s speed IN/%s OUT/%s\n",
  314. obex->port_num,
  315. gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
  316. obex->port.in->name, obex->port.out->name);
  317. return 0;
  318. fail:
  319. /* we might as well release our claims on endpoints */
  320. if (obex->port.out)
  321. obex->port.out->driver_data = NULL;
  322. if (obex->port.in)
  323. obex->port.in->driver_data = NULL;
  324. ERROR(cdev, "%s/%p: can't bind, err %d\n", f->name, f, status);
  325. return status;
  326. }
  327. static inline struct f_serial_opts *to_f_serial_opts(struct config_item *item)
  328. {
  329. return container_of(to_config_group(item), struct f_serial_opts,
  330. func_inst.group);
  331. }
  332. CONFIGFS_ATTR_STRUCT(f_serial_opts);
  333. static ssize_t f_obex_attr_show(struct config_item *item,
  334. struct configfs_attribute *attr,
  335. char *page)
  336. {
  337. struct f_serial_opts *opts = to_f_serial_opts(item);
  338. struct f_serial_opts_attribute *f_serial_opts_attr =
  339. container_of(attr, struct f_serial_opts_attribute, attr);
  340. ssize_t ret = 0;
  341. if (f_serial_opts_attr->show)
  342. ret = f_serial_opts_attr->show(opts, page);
  343. return ret;
  344. }
  345. static void obex_attr_release(struct config_item *item)
  346. {
  347. struct f_serial_opts *opts = to_f_serial_opts(item);
  348. usb_put_function_instance(&opts->func_inst);
  349. }
  350. static struct configfs_item_operations obex_item_ops = {
  351. .release = obex_attr_release,
  352. .show_attribute = f_obex_attr_show,
  353. };
  354. static ssize_t f_obex_port_num_show(struct f_serial_opts *opts, char *page)
  355. {
  356. return sprintf(page, "%u\n", opts->port_num);
  357. }
  358. static struct f_serial_opts_attribute f_obex_port_num =
  359. __CONFIGFS_ATTR_RO(port_num, f_obex_port_num_show);
  360. static struct configfs_attribute *acm_attrs[] = {
  361. &f_obex_port_num.attr,
  362. NULL,
  363. };
  364. static struct config_item_type obex_func_type = {
  365. .ct_item_ops = &obex_item_ops,
  366. .ct_attrs = acm_attrs,
  367. .ct_owner = THIS_MODULE,
  368. };
  369. static void obex_free_inst(struct usb_function_instance *f)
  370. {
  371. struct f_serial_opts *opts;
  372. opts = container_of(f, struct f_serial_opts, func_inst);
  373. gserial_free_line(opts->port_num);
  374. kfree(opts);
  375. }
  376. static struct usb_function_instance *obex_alloc_inst(void)
  377. {
  378. struct f_serial_opts *opts;
  379. int ret;
  380. opts = kzalloc(sizeof(*opts), GFP_KERNEL);
  381. if (!opts)
  382. return ERR_PTR(-ENOMEM);
  383. opts->func_inst.free_func_inst = obex_free_inst;
  384. ret = gserial_alloc_line(&opts->port_num);
  385. if (ret) {
  386. kfree(opts);
  387. return ERR_PTR(ret);
  388. }
  389. config_group_init_type_name(&opts->func_inst.group, "",
  390. &obex_func_type);
  391. return &opts->func_inst;
  392. }
  393. static void obex_free(struct usb_function *f)
  394. {
  395. struct f_obex *obex;
  396. obex = func_to_obex(f);
  397. kfree(obex);
  398. }
  399. static void obex_unbind(struct usb_configuration *c, struct usb_function *f)
  400. {
  401. usb_free_all_descriptors(f);
  402. }
  403. static struct usb_function *obex_alloc(struct usb_function_instance *fi)
  404. {
  405. struct f_obex *obex;
  406. struct f_serial_opts *opts;
  407. /* allocate and initialize one new instance */
  408. obex = kzalloc(sizeof(*obex), GFP_KERNEL);
  409. if (!obex)
  410. return ERR_PTR(-ENOMEM);
  411. opts = container_of(fi, struct f_serial_opts, func_inst);
  412. obex->port_num = opts->port_num;
  413. obex->port.connect = obex_connect;
  414. obex->port.disconnect = obex_disconnect;
  415. obex->port.func.name = "obex";
  416. /* descriptors are per-instance copies */
  417. obex->port.func.bind = obex_bind;
  418. obex->port.func.unbind = obex_unbind;
  419. obex->port.func.set_alt = obex_set_alt;
  420. obex->port.func.get_alt = obex_get_alt;
  421. obex->port.func.disable = obex_disable;
  422. obex->port.func.free_func = obex_free;
  423. return &obex->port.func;
  424. }
  425. DECLARE_USB_FUNCTION_INIT(obex, obex_alloc_inst, obex_alloc);
  426. MODULE_AUTHOR("Felipe Balbi");
  427. MODULE_LICENSE("GPL");