f_subset.c 13 KB

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