f_subset.c 13 KB

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