PageRenderTime 65ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/usb/gadget/function/f_subset.c

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