f_obex.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * f_obex.c -- USB CDC OBEX function driver
  4. *
  5. * Copyright (C) 2008 Nokia Corporation
  6. * Contact: Felipe Balbi <[email protected]>
  7. *
  8. * Based on f_acm.c by Al Borchers and David Brownell.
  9. */
  10. /* #define VERBOSE_DEBUG */
  11. #include <linux/slab.h>
  12. #include <linux/kernel.h>
  13. #include <linux/device.h>
  14. #include <linux/module.h>
  15. #include "u_serial.h"
  16. /*
  17. * This CDC OBEX function support just packages a TTY-ish byte stream.
  18. * A user mode server will put it into "raw" mode and handle all the
  19. * relevant protocol details ... this is just a kernel passthrough.
  20. * When possible, we prevent gadget enumeration until that server is
  21. * ready to handle the commands.
  22. */
  23. struct f_obex {
  24. struct gserial port;
  25. u8 ctrl_id;
  26. u8 data_id;
  27. u8 cur_alt;
  28. u8 port_num;
  29. };
  30. static inline struct f_obex *func_to_obex(struct usb_function *f)
  31. {
  32. return container_of(f, struct f_obex, port.func);
  33. }
  34. static inline struct f_obex *port_to_obex(struct gserial *p)
  35. {
  36. return container_of(p, struct f_obex, port);
  37. }
  38. /*-------------------------------------------------------------------------*/
  39. #define OBEX_CTRL_IDX 0
  40. #define OBEX_DATA_IDX 1
  41. static struct usb_string obex_string_defs[] = {
  42. [OBEX_CTRL_IDX].s = "CDC Object Exchange (OBEX)",
  43. [OBEX_DATA_IDX].s = "CDC OBEX Data",
  44. { }, /* end of list */
  45. };
  46. static struct usb_gadget_strings obex_string_table = {
  47. .language = 0x0409, /* en-US */
  48. .strings = obex_string_defs,
  49. };
  50. static struct usb_gadget_strings *obex_strings[] = {
  51. &obex_string_table,
  52. NULL,
  53. };
  54. /*-------------------------------------------------------------------------*/
  55. static struct usb_interface_descriptor obex_control_intf = {
  56. .bLength = sizeof(obex_control_intf),
  57. .bDescriptorType = USB_DT_INTERFACE,
  58. .bInterfaceNumber = 0,
  59. .bAlternateSetting = 0,
  60. .bNumEndpoints = 0,
  61. .bInterfaceClass = USB_CLASS_COMM,
  62. .bInterfaceSubClass = USB_CDC_SUBCLASS_OBEX,
  63. };
  64. static struct usb_interface_descriptor obex_data_nop_intf = {
  65. .bLength = sizeof(obex_data_nop_intf),
  66. .bDescriptorType = USB_DT_INTERFACE,
  67. .bInterfaceNumber = 1,
  68. .bAlternateSetting = 0,
  69. .bNumEndpoints = 0,
  70. .bInterfaceClass = USB_CLASS_CDC_DATA,
  71. };
  72. static struct usb_interface_descriptor obex_data_intf = {
  73. .bLength = sizeof(obex_data_intf),
  74. .bDescriptorType = USB_DT_INTERFACE,
  75. .bInterfaceNumber = 2,
  76. .bAlternateSetting = 1,
  77. .bNumEndpoints = 2,
  78. .bInterfaceClass = USB_CLASS_CDC_DATA,
  79. };
  80. static struct usb_cdc_header_desc obex_cdc_header_desc = {
  81. .bLength = sizeof(obex_cdc_header_desc),
  82. .bDescriptorType = USB_DT_CS_INTERFACE,
  83. .bDescriptorSubType = USB_CDC_HEADER_TYPE,
  84. .bcdCDC = cpu_to_le16(0x0120),
  85. };
  86. static struct usb_cdc_union_desc obex_cdc_union_desc = {
  87. .bLength = sizeof(obex_cdc_union_desc),
  88. .bDescriptorType = USB_DT_CS_INTERFACE,
  89. .bDescriptorSubType = USB_CDC_UNION_TYPE,
  90. .bMasterInterface0 = 1,
  91. .bSlaveInterface0 = 2,
  92. };
  93. static struct usb_cdc_obex_desc obex_desc = {
  94. .bLength = sizeof(obex_desc),
  95. .bDescriptorType = USB_DT_CS_INTERFACE,
  96. .bDescriptorSubType = USB_CDC_OBEX_TYPE,
  97. .bcdVersion = cpu_to_le16(0x0100),
  98. };
  99. /* High-Speed Support */
  100. static struct usb_endpoint_descriptor obex_hs_ep_out_desc = {
  101. .bLength = USB_DT_ENDPOINT_SIZE,
  102. .bDescriptorType = USB_DT_ENDPOINT,
  103. .bEndpointAddress = USB_DIR_OUT,
  104. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  105. .wMaxPacketSize = cpu_to_le16(512),
  106. };
  107. static struct usb_endpoint_descriptor obex_hs_ep_in_desc = {
  108. .bLength = USB_DT_ENDPOINT_SIZE,
  109. .bDescriptorType = USB_DT_ENDPOINT,
  110. .bEndpointAddress = USB_DIR_IN,
  111. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  112. .wMaxPacketSize = cpu_to_le16(512),
  113. };
  114. static struct usb_descriptor_header *hs_function[] = {
  115. (struct usb_descriptor_header *) &obex_control_intf,
  116. (struct usb_descriptor_header *) &obex_cdc_header_desc,
  117. (struct usb_descriptor_header *) &obex_desc,
  118. (struct usb_descriptor_header *) &obex_cdc_union_desc,
  119. (struct usb_descriptor_header *) &obex_data_nop_intf,
  120. (struct usb_descriptor_header *) &obex_data_intf,
  121. (struct usb_descriptor_header *) &obex_hs_ep_in_desc,
  122. (struct usb_descriptor_header *) &obex_hs_ep_out_desc,
  123. NULL,
  124. };
  125. /* Full-Speed Support */
  126. static struct usb_endpoint_descriptor obex_fs_ep_in_desc = {
  127. .bLength = USB_DT_ENDPOINT_SIZE,
  128. .bDescriptorType = USB_DT_ENDPOINT,
  129. .bEndpointAddress = USB_DIR_IN,
  130. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  131. };
  132. static struct usb_endpoint_descriptor obex_fs_ep_out_desc = {
  133. .bLength = USB_DT_ENDPOINT_SIZE,
  134. .bDescriptorType = USB_DT_ENDPOINT,
  135. .bEndpointAddress = USB_DIR_OUT,
  136. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  137. };
  138. static struct usb_descriptor_header *fs_function[] = {
  139. (struct usb_descriptor_header *) &obex_control_intf,
  140. (struct usb_descriptor_header *) &obex_cdc_header_desc,
  141. (struct usb_descriptor_header *) &obex_desc,
  142. (struct usb_descriptor_header *) &obex_cdc_union_desc,
  143. (struct usb_descriptor_header *) &obex_data_nop_intf,
  144. (struct usb_descriptor_header *) &obex_data_intf,
  145. (struct usb_descriptor_header *) &obex_fs_ep_in_desc,
  146. (struct usb_descriptor_header *) &obex_fs_ep_out_desc,
  147. NULL,
  148. };
  149. /*-------------------------------------------------------------------------*/
  150. static int obex_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
  151. {
  152. struct f_obex *obex = func_to_obex(f);
  153. struct usb_composite_dev *cdev = f->config->cdev;
  154. if (intf == obex->ctrl_id) {
  155. if (alt != 0)
  156. goto fail;
  157. /* NOP */
  158. dev_dbg(&cdev->gadget->dev,
  159. "reset obex ttyGS%d control\n", obex->port_num);
  160. } else if (intf == obex->data_id) {
  161. if (alt > 1)
  162. goto fail;
  163. if (obex->port.in->enabled) {
  164. dev_dbg(&cdev->gadget->dev,
  165. "reset obex ttyGS%d\n", obex->port_num);
  166. gserial_disconnect(&obex->port);
  167. }
  168. if (!obex->port.in->desc || !obex->port.out->desc) {
  169. dev_dbg(&cdev->gadget->dev,
  170. "init obex ttyGS%d\n", obex->port_num);
  171. if (config_ep_by_speed(cdev->gadget, f,
  172. obex->port.in) ||
  173. config_ep_by_speed(cdev->gadget, f,
  174. obex->port.out)) {
  175. obex->port.out->desc = NULL;
  176. obex->port.in->desc = NULL;
  177. goto fail;
  178. }
  179. }
  180. if (alt == 1) {
  181. dev_dbg(&cdev->gadget->dev,
  182. "activate obex ttyGS%d\n", obex->port_num);
  183. gserial_connect(&obex->port, obex->port_num);
  184. }
  185. } else
  186. goto fail;
  187. obex->cur_alt = alt;
  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. return obex->cur_alt;
  196. }
  197. static void obex_disable(struct usb_function *f)
  198. {
  199. struct f_obex *obex = func_to_obex(f);
  200. struct usb_composite_dev *cdev = f->config->cdev;
  201. dev_dbg(&cdev->gadget->dev, "obex ttyGS%d disable\n", obex->port_num);
  202. gserial_disconnect(&obex->port);
  203. }
  204. /*-------------------------------------------------------------------------*/
  205. static void obex_connect(struct gserial *g)
  206. {
  207. struct f_obex *obex = port_to_obex(g);
  208. struct usb_composite_dev *cdev = g->func.config->cdev;
  209. int status;
  210. status = usb_function_activate(&g->func);
  211. if (status)
  212. dev_dbg(&cdev->gadget->dev,
  213. "obex ttyGS%d function activate --> %d\n",
  214. obex->port_num, status);
  215. }
  216. static void obex_disconnect(struct gserial *g)
  217. {
  218. struct f_obex *obex = port_to_obex(g);
  219. struct usb_composite_dev *cdev = g->func.config->cdev;
  220. int status;
  221. status = usb_function_deactivate(&g->func);
  222. if (status)
  223. dev_dbg(&cdev->gadget->dev,
  224. "obex ttyGS%d function deactivate --> %d\n",
  225. obex->port_num, status);
  226. }
  227. /*-------------------------------------------------------------------------*/
  228. /* Some controllers can't support CDC OBEX ... */
  229. static inline bool can_support_obex(struct usb_configuration *c)
  230. {
  231. /* Since the first interface is a NOP, we can ignore the
  232. * issue of multi-interface support on most controllers.
  233. *
  234. * Altsettings are mandatory, however...
  235. */
  236. if (!gadget_is_altset_supported(c->cdev->gadget))
  237. return false;
  238. /* everything else is *probably* fine ... */
  239. return true;
  240. }
  241. static int 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. struct usb_string *us;
  246. int status;
  247. struct usb_ep *ep;
  248. if (!can_support_obex(c))
  249. return -EINVAL;
  250. us = usb_gstrings_attach(cdev, obex_strings,
  251. ARRAY_SIZE(obex_string_defs));
  252. if (IS_ERR(us))
  253. return PTR_ERR(us);
  254. obex_control_intf.iInterface = us[OBEX_CTRL_IDX].id;
  255. obex_data_nop_intf.iInterface = us[OBEX_DATA_IDX].id;
  256. obex_data_intf.iInterface = us[OBEX_DATA_IDX].id;
  257. /* allocate instance-specific interface IDs, and patch descriptors */
  258. status = usb_interface_id(c, f);
  259. if (status < 0)
  260. goto fail;
  261. obex->ctrl_id = status;
  262. obex_control_intf.bInterfaceNumber = status;
  263. obex_cdc_union_desc.bMasterInterface0 = status;
  264. status = usb_interface_id(c, f);
  265. if (status < 0)
  266. goto fail;
  267. obex->data_id = status;
  268. obex_data_nop_intf.bInterfaceNumber = status;
  269. obex_data_intf.bInterfaceNumber = status;
  270. obex_cdc_union_desc.bSlaveInterface0 = status;
  271. /* allocate instance-specific endpoints */
  272. status = -ENODEV;
  273. ep = usb_ep_autoconfig(cdev->gadget, &obex_fs_ep_in_desc);
  274. if (!ep)
  275. goto fail;
  276. obex->port.in = ep;
  277. ep = usb_ep_autoconfig(cdev->gadget, &obex_fs_ep_out_desc);
  278. if (!ep)
  279. goto fail;
  280. obex->port.out = ep;
  281. /* support all relevant hardware speeds... we expect that when
  282. * hardware is dual speed, all bulk-capable endpoints work at
  283. * both speeds
  284. */
  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. status = usb_assign_descriptors(f, fs_function, hs_function, NULL,
  290. NULL);
  291. if (status)
  292. goto fail;
  293. dev_dbg(&cdev->gadget->dev, "obex ttyGS%d: %s speed IN/%s OUT/%s\n",
  294. obex->port_num,
  295. gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
  296. obex->port.in->name, obex->port.out->name);
  297. return 0;
  298. fail:
  299. ERROR(cdev, "%s/%p: can't bind, err %d\n", f->name, f, status);
  300. return status;
  301. }
  302. static inline struct f_serial_opts *to_f_serial_opts(struct config_item *item)
  303. {
  304. return container_of(to_config_group(item), struct f_serial_opts,
  305. func_inst.group);
  306. }
  307. static void obex_attr_release(struct config_item *item)
  308. {
  309. struct f_serial_opts *opts = to_f_serial_opts(item);
  310. usb_put_function_instance(&opts->func_inst);
  311. }
  312. static struct configfs_item_operations obex_item_ops = {
  313. .release = obex_attr_release,
  314. };
  315. static ssize_t f_obex_port_num_show(struct config_item *item, char *page)
  316. {
  317. return sprintf(page, "%u\n", to_f_serial_opts(item)->port_num);
  318. }
  319. CONFIGFS_ATTR_RO(f_obex_, port_num);
  320. static struct configfs_attribute *acm_attrs[] = {
  321. &f_obex_attr_port_num,
  322. NULL,
  323. };
  324. static const struct config_item_type obex_func_type = {
  325. .ct_item_ops = &obex_item_ops,
  326. .ct_attrs = acm_attrs,
  327. .ct_owner = THIS_MODULE,
  328. };
  329. static void obex_free_inst(struct usb_function_instance *f)
  330. {
  331. struct f_serial_opts *opts;
  332. opts = container_of(f, struct f_serial_opts, func_inst);
  333. gserial_free_line(opts->port_num);
  334. kfree(opts);
  335. }
  336. static struct usb_function_instance *obex_alloc_inst(void)
  337. {
  338. struct f_serial_opts *opts;
  339. int ret;
  340. opts = kzalloc(sizeof(*opts), GFP_KERNEL);
  341. if (!opts)
  342. return ERR_PTR(-ENOMEM);
  343. opts->func_inst.free_func_inst = obex_free_inst;
  344. ret = gserial_alloc_line_no_console(&opts->port_num);
  345. if (ret) {
  346. kfree(opts);
  347. return ERR_PTR(ret);
  348. }
  349. config_group_init_type_name(&opts->func_inst.group, "",
  350. &obex_func_type);
  351. return &opts->func_inst;
  352. }
  353. static void obex_free(struct usb_function *f)
  354. {
  355. struct f_obex *obex;
  356. obex = func_to_obex(f);
  357. kfree(obex);
  358. }
  359. static void obex_unbind(struct usb_configuration *c, struct usb_function *f)
  360. {
  361. usb_free_all_descriptors(f);
  362. }
  363. static struct usb_function *obex_alloc(struct usb_function_instance *fi)
  364. {
  365. struct f_obex *obex;
  366. struct f_serial_opts *opts;
  367. /* allocate and initialize one new instance */
  368. obex = kzalloc(sizeof(*obex), GFP_KERNEL);
  369. if (!obex)
  370. return ERR_PTR(-ENOMEM);
  371. opts = container_of(fi, struct f_serial_opts, func_inst);
  372. obex->port_num = opts->port_num;
  373. obex->port.connect = obex_connect;
  374. obex->port.disconnect = obex_disconnect;
  375. obex->port.func.name = "obex";
  376. /* descriptors are per-instance copies */
  377. obex->port.func.bind = obex_bind;
  378. obex->port.func.unbind = obex_unbind;
  379. obex->port.func.set_alt = obex_set_alt;
  380. obex->port.func.get_alt = obex_get_alt;
  381. obex->port.func.disable = obex_disable;
  382. obex->port.func.free_func = obex_free;
  383. obex->port.func.bind_deactivated = true;
  384. return &obex->port.func;
  385. }
  386. DECLARE_USB_FUNCTION_INIT(obex, obex_alloc_inst, obex_alloc);
  387. MODULE_AUTHOR("Felipe Balbi");
  388. MODULE_LICENSE("GPL");