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