f_obex.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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/kernel.h>
  25. #include <linux/utsname.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. *
  34. * REVISIT this driver shouldn't actually activate before that user mode
  35. * server is ready to respond! When the "serial gadget" utility code
  36. * adds open/close notifications, this driver should use them with new
  37. * (TBS) composite gadget hooks that wrap usb_gadget_disconnect() and
  38. * usb_gadget_connect() calls with refcounts ... disconnect() when we
  39. * bind, then connect() when the user server code is ready to respond.
  40. */
  41. struct obex_ep_descs {
  42. struct usb_endpoint_descriptor *obex_in;
  43. struct usb_endpoint_descriptor *obex_out;
  44. };
  45. struct f_obex {
  46. struct gserial port;
  47. u8 ctrl_id;
  48. u8 data_id;
  49. u8 port_num;
  50. struct obex_ep_descs fs;
  51. struct obex_ep_descs hs;
  52. };
  53. static inline struct f_obex *func_to_obex(struct usb_function *f)
  54. {
  55. return container_of(f, struct f_obex, port.func);
  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 = __constant_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 = __constant_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 = __constant_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 = __constant_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 int __init
  218. obex_bind(struct usb_configuration *c, struct usb_function *f)
  219. {
  220. struct usb_composite_dev *cdev = c->cdev;
  221. struct f_obex *obex = func_to_obex(f);
  222. int status;
  223. struct usb_ep *ep;
  224. /* allocate instance-specific interface IDs, and patch descriptors */
  225. status = usb_interface_id(c, f);
  226. if (status < 0)
  227. goto fail;
  228. obex->ctrl_id = status;
  229. obex_control_intf.bInterfaceNumber = status;
  230. obex_cdc_union_desc.bMasterInterface0 = status;
  231. status = usb_interface_id(c, f);
  232. if (status < 0)
  233. goto fail;
  234. obex->data_id = status;
  235. obex_data_nop_intf.bInterfaceNumber = status;
  236. obex_data_intf.bInterfaceNumber = status;
  237. obex_cdc_union_desc.bSlaveInterface0 = status;
  238. /* allocate instance-specific endpoints */
  239. ep = usb_ep_autoconfig(cdev->gadget, &obex_fs_ep_in_desc);
  240. if (!ep)
  241. goto fail;
  242. obex->port.in = ep;
  243. ep->driver_data = cdev; /* claim */
  244. ep = usb_ep_autoconfig(cdev->gadget, &obex_fs_ep_out_desc);
  245. if (!ep)
  246. goto fail;
  247. obex->port.out = ep;
  248. ep->driver_data = cdev; /* claim */
  249. /* copy descriptors, and track endpoint copies */
  250. f->descriptors = usb_copy_descriptors(fs_function);
  251. obex->fs.obex_in = usb_find_endpoint(fs_function,
  252. f->descriptors, &obex_fs_ep_in_desc);
  253. obex->fs.obex_out = usb_find_endpoint(fs_function,
  254. f->descriptors, &obex_fs_ep_out_desc);
  255. /* support all relevant hardware speeds... we expect that when
  256. * hardware is dual speed, all bulk-capable endpoints work at
  257. * both speeds
  258. */
  259. if (gadget_is_dualspeed(c->cdev->gadget)) {
  260. obex_hs_ep_in_desc.bEndpointAddress =
  261. obex_fs_ep_in_desc.bEndpointAddress;
  262. obex_hs_ep_out_desc.bEndpointAddress =
  263. obex_fs_ep_out_desc.bEndpointAddress;
  264. /* copy descriptors, and track endpoint copies */
  265. f->hs_descriptors = usb_copy_descriptors(hs_function);
  266. obex->hs.obex_in = usb_find_endpoint(hs_function,
  267. f->descriptors, &obex_hs_ep_in_desc);
  268. obex->hs.obex_out = usb_find_endpoint(hs_function,
  269. f->descriptors, &obex_hs_ep_out_desc);
  270. }
  271. DBG(cdev, "obex ttyGS%d: %s speed IN/%s OUT/%s\n",
  272. obex->port_num,
  273. gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
  274. obex->port.in->name, obex->port.out->name);
  275. return 0;
  276. fail:
  277. /* we might as well release our claims on endpoints */
  278. if (obex->port.out)
  279. obex->port.out->driver_data = NULL;
  280. if (obex->port.in)
  281. obex->port.in->driver_data = NULL;
  282. ERROR(cdev, "%s/%p: can't bind, err %d\n", f->name, f, status);
  283. return status;
  284. }
  285. static void
  286. obex_unbind(struct usb_configuration *c, struct usb_function *f)
  287. {
  288. if (gadget_is_dualspeed(c->cdev->gadget))
  289. usb_free_descriptors(f->hs_descriptors);
  290. usb_free_descriptors(f->descriptors);
  291. kfree(func_to_obex(f));
  292. }
  293. /* Some controllers can't support CDC OBEX ... */
  294. static inline bool can_support_obex(struct usb_configuration *c)
  295. {
  296. /* Since the first interface is a NOP, we can ignore the
  297. * issue of multi-interface support on most controllers.
  298. *
  299. * Altsettings are mandatory, however...
  300. */
  301. if (!gadget_supports_altsettings(c->cdev->gadget))
  302. return false;
  303. /* everything else is *probably* fine ... */
  304. return true;
  305. }
  306. /**
  307. * obex_bind_config - add a CDC OBEX function to a configuration
  308. * @c: the configuration to support the CDC OBEX instance
  309. * @port_num: /dev/ttyGS* port this interface will use
  310. * Context: single threaded during gadget setup
  311. *
  312. * Returns zero on success, else negative errno.
  313. *
  314. * Caller must have called @gserial_setup() with enough ports to
  315. * handle all the ones it binds. Caller is also responsible
  316. * for calling @gserial_cleanup() before module unload.
  317. */
  318. int __init obex_bind_config(struct usb_configuration *c, u8 port_num)
  319. {
  320. struct f_obex *obex;
  321. int status;
  322. if (!can_support_obex(c))
  323. return -EINVAL;
  324. /* maybe allocate device-global string IDs, and patch descriptors */
  325. if (obex_string_defs[OBEX_CTRL_IDX].id == 0) {
  326. status = usb_string_id(c->cdev);
  327. if (status < 0)
  328. return status;
  329. obex_string_defs[OBEX_CTRL_IDX].id = status;
  330. obex_control_intf.iInterface = status;
  331. status = usb_string_id(c->cdev);
  332. if (status < 0)
  333. return status;
  334. obex_string_defs[OBEX_DATA_IDX].id = status;
  335. obex_data_nop_intf.iInterface =
  336. obex_data_intf.iInterface = status;
  337. }
  338. /* allocate and initialize one new instance */
  339. obex = kzalloc(sizeof *obex, GFP_KERNEL);
  340. if (!obex)
  341. return -ENOMEM;
  342. obex->port_num = port_num;
  343. obex->port.func.name = "obex";
  344. obex->port.func.strings = obex_strings;
  345. /* descriptors are per-instance copies */
  346. obex->port.func.bind = obex_bind;
  347. obex->port.func.unbind = obex_unbind;
  348. obex->port.func.set_alt = obex_set_alt;
  349. obex->port.func.get_alt = obex_get_alt;
  350. obex->port.func.disable = obex_disable;
  351. status = usb_add_function(c, &obex->port.func);
  352. if (status)
  353. kfree(obex);
  354. return status;
  355. }
  356. MODULE_AUTHOR("Felipe Balbi");
  357. MODULE_LICENSE("GPL");