f_subset.c 15 KB

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