f_subset.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /*
  2. * f_subset.c -- "CDC Subset" Ethernet link function driver
  3. *
  4. * Copyright (C) 2003-2005,2008 David Brownell
  5. * Copyright (C) 2008 Nokia Corporation
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. */
  12. #include <linux/slab.h>
  13. #include <linux/kernel.h>
  14. #include <linux/device.h>
  15. #include <linux/etherdevice.h>
  16. #include "u_ether.h"
  17. /*
  18. * This function packages a simple "CDC Subset" Ethernet port with no real
  19. * control mechanisms; just raw data transfer over two bulk endpoints.
  20. * The data transfer model is exactly that of CDC Ethernet, which is
  21. * why we call it the "CDC Subset".
  22. *
  23. * Because it's not standardized, this has some interoperability issues.
  24. * They mostly relate to driver binding, since the data transfer model is
  25. * so simple (CDC Ethernet). The original versions of this protocol used
  26. * specific product/vendor IDs: byteswapped IDs for Digital Equipment's
  27. * SA-1100 "Itsy" board, which could run Linux 2.4 kernels and supported
  28. * daughtercards with USB peripheral connectors. (It was used more often
  29. * with other boards, using the Itsy identifiers.) Linux hosts recognized
  30. * this with CONFIG_USB_ARMLINUX; these devices have only one configuration
  31. * and one interface.
  32. *
  33. * At some point, MCCI defined a (nonconformant) CDC MDLM variant called
  34. * "SAFE", which happens to have a mode which is identical to the "CDC
  35. * Subset" in terms of data transfer and lack of control model. This was
  36. * adopted by later Sharp Zaurus models, and by some other software which
  37. * Linux hosts recognize with CONFIG_USB_NET_ZAURUS.
  38. *
  39. * Because Microsoft's RNDIS drivers are far from robust, we added a few
  40. * descriptors to the CDC Subset code, making this code look like a SAFE
  41. * implementation. This lets you use MCCI's host side MS-Windows drivers
  42. * if you get fed up with RNDIS. It also makes it easier for composite
  43. * drivers to work, since they can use class based binding instead of
  44. * caring about specific product and vendor IDs.
  45. */
  46. struct f_gether {
  47. struct gether port;
  48. char ethaddr[14];
  49. };
  50. static inline struct f_gether *func_to_geth(struct usb_function *f)
  51. {
  52. return container_of(f, struct f_gether, port.func);
  53. }
  54. /*-------------------------------------------------------------------------*/
  55. /*
  56. * "Simple" CDC-subset option is a simple vendor-neutral model that most
  57. * full speed controllers can handle: one interface, two bulk endpoints.
  58. * To assist host side drivers, we fancy it up a bit, and add descriptors so
  59. * some host side drivers will understand it as a "SAFE" variant.
  60. *
  61. * "SAFE" loosely follows CDC WMC MDLM, violating the spec in various ways.
  62. * Data endpoints live in the control interface, there's no data interface.
  63. * And it's not used to talk to a cell phone radio.
  64. */
  65. /* interface descriptor: */
  66. static struct usb_interface_descriptor subset_data_intf __initdata = {
  67. .bLength = sizeof subset_data_intf,
  68. .bDescriptorType = USB_DT_INTERFACE,
  69. /* .bInterfaceNumber = DYNAMIC */
  70. .bAlternateSetting = 0,
  71. .bNumEndpoints = 2,
  72. .bInterfaceClass = USB_CLASS_COMM,
  73. .bInterfaceSubClass = USB_CDC_SUBCLASS_MDLM,
  74. .bInterfaceProtocol = 0,
  75. /* .iInterface = DYNAMIC */
  76. };
  77. static struct usb_cdc_header_desc mdlm_header_desc __initdata = {
  78. .bLength = sizeof mdlm_header_desc,
  79. .bDescriptorType = USB_DT_CS_INTERFACE,
  80. .bDescriptorSubType = USB_CDC_HEADER_TYPE,
  81. .bcdCDC = cpu_to_le16(0x0110),
  82. };
  83. static struct usb_cdc_mdlm_desc mdlm_desc __initdata = {
  84. .bLength = sizeof mdlm_desc,
  85. .bDescriptorType = USB_DT_CS_INTERFACE,
  86. .bDescriptorSubType = USB_CDC_MDLM_TYPE,
  87. .bcdVersion = cpu_to_le16(0x0100),
  88. .bGUID = {
  89. 0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6,
  90. 0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f,
  91. },
  92. };
  93. /* since "usb_cdc_mdlm_detail_desc" is a variable length structure, we
  94. * can't really use its struct. All we do here is say that we're using
  95. * the submode of "SAFE" which directly matches the CDC Subset.
  96. */
  97. static u8 mdlm_detail_desc[] __initdata = {
  98. 6,
  99. USB_DT_CS_INTERFACE,
  100. USB_CDC_MDLM_DETAIL_TYPE,
  101. 0, /* "SAFE" */
  102. 0, /* network control capabilities (none) */
  103. 0, /* network data capabilities ("raw" encapsulation) */
  104. };
  105. static struct usb_cdc_ether_desc ether_desc __initdata = {
  106. .bLength = sizeof ether_desc,
  107. .bDescriptorType = USB_DT_CS_INTERFACE,
  108. .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
  109. /* this descriptor actually adds value, surprise! */
  110. /* .iMACAddress = DYNAMIC */
  111. .bmEthernetStatistics = cpu_to_le32(0), /* no statistics */
  112. .wMaxSegmentSize = cpu_to_le16(ETH_FRAME_LEN),
  113. .wNumberMCFilters = cpu_to_le16(0),
  114. .bNumberPowerFilters = 0,
  115. };
  116. /* full speed support: */
  117. static struct usb_endpoint_descriptor fs_subset_in_desc __initdata = {
  118. .bLength = USB_DT_ENDPOINT_SIZE,
  119. .bDescriptorType = USB_DT_ENDPOINT,
  120. .bEndpointAddress = USB_DIR_IN,
  121. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  122. };
  123. static struct usb_endpoint_descriptor fs_subset_out_desc __initdata = {
  124. .bLength = USB_DT_ENDPOINT_SIZE,
  125. .bDescriptorType = USB_DT_ENDPOINT,
  126. .bEndpointAddress = USB_DIR_OUT,
  127. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  128. };
  129. static struct usb_descriptor_header *fs_eth_function[] __initdata = {
  130. (struct usb_descriptor_header *) &subset_data_intf,
  131. (struct usb_descriptor_header *) &mdlm_header_desc,
  132. (struct usb_descriptor_header *) &mdlm_desc,
  133. (struct usb_descriptor_header *) &mdlm_detail_desc,
  134. (struct usb_descriptor_header *) &ether_desc,
  135. (struct usb_descriptor_header *) &fs_subset_in_desc,
  136. (struct usb_descriptor_header *) &fs_subset_out_desc,
  137. NULL,
  138. };
  139. /* high speed support: */
  140. static struct usb_endpoint_descriptor hs_subset_in_desc __initdata = {
  141. .bLength = USB_DT_ENDPOINT_SIZE,
  142. .bDescriptorType = USB_DT_ENDPOINT,
  143. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  144. .wMaxPacketSize = cpu_to_le16(512),
  145. };
  146. static struct usb_endpoint_descriptor hs_subset_out_desc __initdata = {
  147. .bLength = USB_DT_ENDPOINT_SIZE,
  148. .bDescriptorType = USB_DT_ENDPOINT,
  149. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  150. .wMaxPacketSize = cpu_to_le16(512),
  151. };
  152. static struct usb_descriptor_header *hs_eth_function[] __initdata = {
  153. (struct usb_descriptor_header *) &subset_data_intf,
  154. (struct usb_descriptor_header *) &mdlm_header_desc,
  155. (struct usb_descriptor_header *) &mdlm_desc,
  156. (struct usb_descriptor_header *) &mdlm_detail_desc,
  157. (struct usb_descriptor_header *) &ether_desc,
  158. (struct usb_descriptor_header *) &hs_subset_in_desc,
  159. (struct usb_descriptor_header *) &hs_subset_out_desc,
  160. NULL,
  161. };
  162. /* super speed support: */
  163. static struct usb_endpoint_descriptor ss_subset_in_desc __initdata = {
  164. .bLength = USB_DT_ENDPOINT_SIZE,
  165. .bDescriptorType = USB_DT_ENDPOINT,
  166. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  167. .wMaxPacketSize = cpu_to_le16(1024),
  168. };
  169. static struct usb_endpoint_descriptor ss_subset_out_desc __initdata = {
  170. .bLength = USB_DT_ENDPOINT_SIZE,
  171. .bDescriptorType = USB_DT_ENDPOINT,
  172. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  173. .wMaxPacketSize = cpu_to_le16(1024),
  174. };
  175. static struct usb_ss_ep_comp_descriptor ss_subset_bulk_comp_desc __initdata = {
  176. .bLength = sizeof ss_subset_bulk_comp_desc,
  177. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  178. /* the following 2 values can be tweaked if necessary */
  179. /* .bMaxBurst = 0, */
  180. /* .bmAttributes = 0, */
  181. };
  182. static struct usb_descriptor_header *ss_eth_function[] __initdata = {
  183. (struct usb_descriptor_header *) &subset_data_intf,
  184. (struct usb_descriptor_header *) &mdlm_header_desc,
  185. (struct usb_descriptor_header *) &mdlm_desc,
  186. (struct usb_descriptor_header *) &mdlm_detail_desc,
  187. (struct usb_descriptor_header *) &ether_desc,
  188. (struct usb_descriptor_header *) &ss_subset_in_desc,
  189. (struct usb_descriptor_header *) &ss_subset_bulk_comp_desc,
  190. (struct usb_descriptor_header *) &ss_subset_out_desc,
  191. (struct usb_descriptor_header *) &ss_subset_bulk_comp_desc,
  192. NULL,
  193. };
  194. /* string descriptors: */
  195. static struct usb_string geth_string_defs[] = {
  196. [0].s = "CDC Ethernet Subset/SAFE",
  197. [1].s = NULL /* DYNAMIC */,
  198. { } /* end of list */
  199. };
  200. static struct usb_gadget_strings geth_string_table = {
  201. .language = 0x0409, /* en-us */
  202. .strings = geth_string_defs,
  203. };
  204. static struct usb_gadget_strings *geth_strings[] = {
  205. &geth_string_table,
  206. NULL,
  207. };
  208. /*-------------------------------------------------------------------------*/
  209. static int geth_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
  210. {
  211. struct f_gether *geth = func_to_geth(f);
  212. struct usb_composite_dev *cdev = f->config->cdev;
  213. struct net_device *net;
  214. /* we know alt == 0, so this is an activation or a reset */
  215. if (geth->port.in_ep->driver_data) {
  216. DBG(cdev, "reset cdc subset\n");
  217. gether_disconnect(&geth->port);
  218. }
  219. DBG(cdev, "init + activate cdc subset\n");
  220. if (config_ep_by_speed(cdev->gadget, f, geth->port.in_ep) ||
  221. config_ep_by_speed(cdev->gadget, f, geth->port.out_ep)) {
  222. geth->port.in_ep->desc = NULL;
  223. geth->port.out_ep->desc = NULL;
  224. return -EINVAL;
  225. }
  226. net = gether_connect(&geth->port);
  227. return IS_ERR(net) ? PTR_ERR(net) : 0;
  228. }
  229. static void geth_disable(struct usb_function *f)
  230. {
  231. struct f_gether *geth = func_to_geth(f);
  232. struct usb_composite_dev *cdev = f->config->cdev;
  233. DBG(cdev, "net deactivated\n");
  234. gether_disconnect(&geth->port);
  235. }
  236. /*-------------------------------------------------------------------------*/
  237. /* serial function driver setup/binding */
  238. static int __init
  239. geth_bind(struct usb_configuration *c, struct usb_function *f)
  240. {
  241. struct usb_composite_dev *cdev = c->cdev;
  242. struct f_gether *geth = func_to_geth(f);
  243. int status;
  244. struct usb_ep *ep;
  245. /* allocate instance-specific interface IDs */
  246. status = usb_interface_id(c, f);
  247. if (status < 0)
  248. goto fail;
  249. subset_data_intf.bInterfaceNumber = status;
  250. status = -ENODEV;
  251. /* allocate instance-specific endpoints */
  252. ep = usb_ep_autoconfig(cdev->gadget, &fs_subset_in_desc);
  253. if (!ep)
  254. goto fail;
  255. geth->port.in_ep = ep;
  256. ep->driver_data = cdev; /* claim */
  257. ep = usb_ep_autoconfig(cdev->gadget, &fs_subset_out_desc);
  258. if (!ep)
  259. goto fail;
  260. geth->port.out_ep = ep;
  261. ep->driver_data = cdev; /* claim */
  262. /* copy descriptors, and track endpoint copies */
  263. f->descriptors = usb_copy_descriptors(fs_eth_function);
  264. if (!f->descriptors)
  265. goto fail;
  266. /* support all relevant hardware speeds... we expect that when
  267. * hardware is dual speed, all bulk-capable endpoints work at
  268. * both speeds
  269. */
  270. if (gadget_is_dualspeed(c->cdev->gadget)) {
  271. hs_subset_in_desc.bEndpointAddress =
  272. fs_subset_in_desc.bEndpointAddress;
  273. hs_subset_out_desc.bEndpointAddress =
  274. fs_subset_out_desc.bEndpointAddress;
  275. /* copy descriptors, and track endpoint copies */
  276. f->hs_descriptors = usb_copy_descriptors(hs_eth_function);
  277. if (!f->hs_descriptors)
  278. goto fail;
  279. }
  280. if (gadget_is_superspeed(c->cdev->gadget)) {
  281. ss_subset_in_desc.bEndpointAddress =
  282. fs_subset_in_desc.bEndpointAddress;
  283. ss_subset_out_desc.bEndpointAddress =
  284. fs_subset_out_desc.bEndpointAddress;
  285. /* copy descriptors, and track endpoint copies */
  286. f->ss_descriptors = usb_copy_descriptors(ss_eth_function);
  287. if (!f->ss_descriptors)
  288. goto fail;
  289. }
  290. /* NOTE: all that is done without knowing or caring about
  291. * the network link ... which is unavailable to this code
  292. * until we're activated via set_alt().
  293. */
  294. DBG(cdev, "CDC Subset: %s speed IN/%s OUT/%s\n",
  295. gadget_is_superspeed(c->cdev->gadget) ? "super" :
  296. gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
  297. geth->port.in_ep->name, geth->port.out_ep->name);
  298. return 0;
  299. fail:
  300. if (f->descriptors)
  301. usb_free_descriptors(f->descriptors);
  302. if (f->hs_descriptors)
  303. usb_free_descriptors(f->hs_descriptors);
  304. /* we might as well release our claims on endpoints */
  305. if (geth->port.out_ep->desc)
  306. geth->port.out_ep->driver_data = NULL;
  307. if (geth->port.in_ep->desc)
  308. geth->port.in_ep->driver_data = NULL;
  309. ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
  310. return status;
  311. }
  312. static void
  313. geth_unbind(struct usb_configuration *c, struct usb_function *f)
  314. {
  315. if (gadget_is_superspeed(c->cdev->gadget))
  316. usb_free_descriptors(f->ss_descriptors);
  317. if (gadget_is_dualspeed(c->cdev->gadget))
  318. usb_free_descriptors(f->hs_descriptors);
  319. usb_free_descriptors(f->descriptors);
  320. geth_string_defs[1].s = NULL;
  321. kfree(func_to_geth(f));
  322. }
  323. /**
  324. * geth_bind_config - add CDC Subset network link to a configuration
  325. * @c: the configuration to support the network link
  326. * @ethaddr: a buffer in which the ethernet address of the host side
  327. * side of the link was recorded
  328. * Context: single threaded during gadget setup
  329. *
  330. * Returns zero on success, else negative errno.
  331. *
  332. * Caller must have called @gether_setup(). Caller is also responsible
  333. * for calling @gether_cleanup() before module unload.
  334. */
  335. int __init geth_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN])
  336. {
  337. struct f_gether *geth;
  338. int status;
  339. if (!ethaddr)
  340. return -EINVAL;
  341. /* maybe allocate device-global string IDs */
  342. if (geth_string_defs[0].id == 0) {
  343. /* interface label */
  344. status = usb_string_id(c->cdev);
  345. if (status < 0)
  346. return status;
  347. geth_string_defs[0].id = status;
  348. subset_data_intf.iInterface = status;
  349. /* MAC address */
  350. status = usb_string_id(c->cdev);
  351. if (status < 0)
  352. return status;
  353. geth_string_defs[1].id = status;
  354. ether_desc.iMACAddress = status;
  355. }
  356. /* allocate and initialize one new instance */
  357. geth = kzalloc(sizeof *geth, GFP_KERNEL);
  358. if (!geth)
  359. return -ENOMEM;
  360. /* export host's Ethernet address in CDC format */
  361. snprintf(geth->ethaddr, sizeof geth->ethaddr,
  362. "%02X%02X%02X%02X%02X%02X",
  363. ethaddr[0], ethaddr[1], ethaddr[2],
  364. ethaddr[3], ethaddr[4], ethaddr[5]);
  365. geth_string_defs[1].s = geth->ethaddr;
  366. geth->port.cdc_filter = DEFAULT_FILTER;
  367. geth->port.func.name = "cdc_subset";
  368. geth->port.func.strings = geth_strings;
  369. geth->port.func.bind = geth_bind;
  370. geth->port.func.unbind = geth_unbind;
  371. geth->port.func.set_alt = geth_set_alt;
  372. geth->port.func.disable = geth_disable;
  373. status = usb_add_function(c, &geth->port.func);
  374. if (status) {
  375. geth_string_defs[1].s = NULL;
  376. kfree(geth);
  377. }
  378. return status;
  379. }