f_subset.c 16 KB

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