f_subset.c 14 KB

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