PageRenderTime 63ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/openbar/rpi-linux
C | 510 lines | 320 code | 95 blank | 95 comment | 14 complexity | 651633674b2d4c9ce995d9b52e39f3a2 MD5 | raw file
  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->enabled) {
  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 PTR_ERR_OR_ZERO(net);
  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. struct f_gether_opts *gether_opts;
  250. gether_opts = container_of(f->fi, struct f_gether_opts, func_inst);
  251. /*
  252. * in drivers/usb/gadget/configfs.c:configfs_composite_bind()
  253. * configurations are bound in sequence with list_for_each_entry,
  254. * in each configuration its functions are bound in sequence
  255. * with list_for_each_entry, so we assume no race condition
  256. * with regard to gether_opts->bound access
  257. */
  258. if (!gether_opts->bound) {
  259. mutex_lock(&gether_opts->lock);
  260. gether_set_gadget(gether_opts->net, cdev->gadget);
  261. status = gether_register_netdev(gether_opts->net);
  262. mutex_unlock(&gether_opts->lock);
  263. if (status)
  264. return status;
  265. gether_opts->bound = true;
  266. }
  267. us = usb_gstrings_attach(cdev, geth_strings,
  268. ARRAY_SIZE(geth_string_defs));
  269. if (IS_ERR(us))
  270. return PTR_ERR(us);
  271. subset_data_intf.iInterface = us[0].id;
  272. ether_desc.iMACAddress = us[1].id;
  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 = usb_ep_autoconfig(cdev->gadget, &fs_subset_out_desc);
  285. if (!ep)
  286. goto fail;
  287. geth->port.out_ep = ep;
  288. /* support all relevant hardware speeds... we expect that when
  289. * hardware is dual speed, all bulk-capable endpoints work at
  290. * both speeds
  291. */
  292. hs_subset_in_desc.bEndpointAddress = fs_subset_in_desc.bEndpointAddress;
  293. hs_subset_out_desc.bEndpointAddress =
  294. fs_subset_out_desc.bEndpointAddress;
  295. ss_subset_in_desc.bEndpointAddress = fs_subset_in_desc.bEndpointAddress;
  296. ss_subset_out_desc.bEndpointAddress =
  297. fs_subset_out_desc.bEndpointAddress;
  298. status = usb_assign_descriptors(f, fs_eth_function, hs_eth_function,
  299. ss_eth_function);
  300. if (status)
  301. goto fail;
  302. /* NOTE: all that is done without knowing or caring about
  303. * the network link ... which is unavailable to this code
  304. * until we're activated via set_alt().
  305. */
  306. DBG(cdev, "CDC Subset: %s speed IN/%s OUT/%s\n",
  307. gadget_is_superspeed(c->cdev->gadget) ? "super" :
  308. gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
  309. geth->port.in_ep->name, geth->port.out_ep->name);
  310. return 0;
  311. fail:
  312. ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
  313. return status;
  314. }
  315. static inline struct f_gether_opts *to_f_gether_opts(struct config_item *item)
  316. {
  317. return container_of(to_config_group(item), struct f_gether_opts,
  318. func_inst.group);
  319. }
  320. /* f_gether_item_ops */
  321. USB_ETHERNET_CONFIGFS_ITEM(gether);
  322. /* f_gether_opts_dev_addr */
  323. USB_ETHERNET_CONFIGFS_ITEM_ATTR_DEV_ADDR(gether);
  324. /* f_gether_opts_host_addr */
  325. USB_ETHERNET_CONFIGFS_ITEM_ATTR_HOST_ADDR(gether);
  326. /* f_gether_opts_qmult */
  327. USB_ETHERNET_CONFIGFS_ITEM_ATTR_QMULT(gether);
  328. /* f_gether_opts_ifname */
  329. USB_ETHERNET_CONFIGFS_ITEM_ATTR_IFNAME(gether);
  330. static struct configfs_attribute *gether_attrs[] = {
  331. &gether_opts_attr_dev_addr,
  332. &gether_opts_attr_host_addr,
  333. &gether_opts_attr_qmult,
  334. &gether_opts_attr_ifname,
  335. NULL,
  336. };
  337. static struct config_item_type gether_func_type = {
  338. .ct_item_ops = &gether_item_ops,
  339. .ct_attrs = gether_attrs,
  340. .ct_owner = THIS_MODULE,
  341. };
  342. static void geth_free_inst(struct usb_function_instance *f)
  343. {
  344. struct f_gether_opts *opts;
  345. opts = container_of(f, struct f_gether_opts, func_inst);
  346. if (opts->bound)
  347. gether_cleanup(netdev_priv(opts->net));
  348. else
  349. free_netdev(opts->net);
  350. kfree(opts);
  351. }
  352. static struct usb_function_instance *geth_alloc_inst(void)
  353. {
  354. struct f_gether_opts *opts;
  355. opts = kzalloc(sizeof(*opts), GFP_KERNEL);
  356. if (!opts)
  357. return ERR_PTR(-ENOMEM);
  358. mutex_init(&opts->lock);
  359. opts->func_inst.free_func_inst = geth_free_inst;
  360. opts->net = gether_setup_default();
  361. if (IS_ERR(opts->net)) {
  362. struct net_device *net = opts->net;
  363. kfree(opts);
  364. return ERR_CAST(net);
  365. }
  366. config_group_init_type_name(&opts->func_inst.group, "",
  367. &gether_func_type);
  368. return &opts->func_inst;
  369. }
  370. static void geth_free(struct usb_function *f)
  371. {
  372. struct f_gether *eth;
  373. eth = func_to_geth(f);
  374. kfree(eth);
  375. }
  376. static void geth_unbind(struct usb_configuration *c, struct usb_function *f)
  377. {
  378. geth_string_defs[0].id = 0;
  379. usb_free_all_descriptors(f);
  380. }
  381. static struct usb_function *geth_alloc(struct usb_function_instance *fi)
  382. {
  383. struct f_gether *geth;
  384. struct f_gether_opts *opts;
  385. int status;
  386. /* allocate and initialize one new instance */
  387. geth = kzalloc(sizeof(*geth), GFP_KERNEL);
  388. if (!geth)
  389. return ERR_PTR(-ENOMEM);
  390. opts = container_of(fi, struct f_gether_opts, func_inst);
  391. mutex_lock(&opts->lock);
  392. opts->refcnt++;
  393. /* export host's Ethernet address in CDC format */
  394. status = gether_get_host_addr_cdc(opts->net, geth->ethaddr,
  395. sizeof(geth->ethaddr));
  396. if (status < 12) {
  397. kfree(geth);
  398. mutex_unlock(&opts->lock);
  399. return ERR_PTR(-EINVAL);
  400. }
  401. geth_string_defs[1].s = geth->ethaddr;
  402. geth->port.ioport = netdev_priv(opts->net);
  403. mutex_unlock(&opts->lock);
  404. geth->port.cdc_filter = DEFAULT_FILTER;
  405. geth->port.func.name = "cdc_subset";
  406. geth->port.func.bind = geth_bind;
  407. geth->port.func.unbind = geth_unbind;
  408. geth->port.func.set_alt = geth_set_alt;
  409. geth->port.func.disable = geth_disable;
  410. geth->port.func.free_func = geth_free;
  411. return &geth->port.func;
  412. }
  413. DECLARE_USB_FUNCTION_INIT(geth, geth_alloc_inst, geth_alloc);
  414. MODULE_LICENSE("GPL");
  415. MODULE_AUTHOR("David Brownell");