f_ecm.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * f_ecm.c -- USB CDC Ethernet (ECM) link function driver
  4. *
  5. * Copyright (C) 2003-2005,2008 David Brownell
  6. * Copyright (C) 2008 Nokia Corporation
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. */
  13. /* #define VERBOSE_DEBUG */
  14. #include <linux/slab.h>
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/device.h>
  18. #include <linux/etherdevice.h>
  19. #include "u_ether.h"
  20. #include "u_ether_configfs.h"
  21. #include "u_ecm.h"
  22. /*
  23. * This function is a "CDC Ethernet Networking Control Model" (CDC ECM)
  24. * Ethernet link. The data transfer model is simple (packets sent and
  25. * received over bulk endpoints using normal short packet termination),
  26. * and the control model exposes various data and optional notifications.
  27. *
  28. * ECM is well standardized and (except for Microsoft) supported by most
  29. * operating systems with USB host support. It's the preferred interop
  30. * solution for Ethernet over USB, at least for firmware based solutions.
  31. * (Hardware solutions tend to be more minimalist.) A newer and simpler
  32. * "Ethernet Emulation Model" (CDC EEM) hasn't yet caught on.
  33. *
  34. * Note that ECM requires the use of "alternate settings" for its data
  35. * interface. This means that the set_alt() method has real work to do,
  36. * and also means that a get_alt() method is required.
  37. */
  38. enum ecm_notify_state {
  39. ECM_NOTIFY_NONE, /* don't notify */
  40. ECM_NOTIFY_CONNECT, /* issue CONNECT next */
  41. ECM_NOTIFY_SPEED, /* issue SPEED_CHANGE next */
  42. };
  43. struct f_ecm {
  44. struct gether port;
  45. u8 ctrl_id, data_id;
  46. char ethaddr[14];
  47. struct usb_ep *notify;
  48. struct usb_request *notify_req;
  49. u8 notify_state;
  50. bool is_open;
  51. /* FIXME is_open needs some irq-ish locking
  52. * ... possibly the same as port.ioport
  53. */
  54. };
  55. static inline struct f_ecm *func_to_ecm(struct usb_function *f)
  56. {
  57. return container_of(f, struct f_ecm, port.func);
  58. }
  59. /* peak (theoretical) bulk transfer rate in bits-per-second */
  60. static inline unsigned ecm_bitrate(struct usb_gadget *g)
  61. {
  62. if (gadget_is_superspeed(g) && g->speed == USB_SPEED_SUPER)
  63. return 13 * 1024 * 8 * 1000 * 8;
  64. else if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
  65. return 13 * 512 * 8 * 1000 * 8;
  66. else
  67. return 19 * 64 * 1 * 1000 * 8;
  68. }
  69. /*-------------------------------------------------------------------------*/
  70. /*
  71. * Include the status endpoint if we can, even though it's optional.
  72. *
  73. * Use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
  74. * packet, to simplify cancellation; and a big transfer interval, to
  75. * waste less bandwidth.
  76. *
  77. * Some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even
  78. * if they ignore the connect/disconnect notifications that real aether
  79. * can provide. More advanced cdc configurations might want to support
  80. * encapsulated commands (vendor-specific, using control-OUT).
  81. */
  82. #define ECM_STATUS_INTERVAL_MS 32
  83. #define ECM_STATUS_BYTECOUNT 16 /* 8 byte header + data */
  84. /* interface descriptor: */
  85. static struct usb_interface_assoc_descriptor
  86. ecm_iad_descriptor = {
  87. .bLength = sizeof ecm_iad_descriptor,
  88. .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
  89. /* .bFirstInterface = DYNAMIC, */
  90. .bInterfaceCount = 2, /* control + data */
  91. .bFunctionClass = USB_CLASS_COMM,
  92. .bFunctionSubClass = USB_CDC_SUBCLASS_ETHERNET,
  93. .bFunctionProtocol = USB_CDC_PROTO_NONE,
  94. /* .iFunction = DYNAMIC */
  95. };
  96. static struct usb_interface_descriptor ecm_control_intf = {
  97. .bLength = sizeof ecm_control_intf,
  98. .bDescriptorType = USB_DT_INTERFACE,
  99. /* .bInterfaceNumber = DYNAMIC */
  100. /* status endpoint is optional; this could be patched later */
  101. .bNumEndpoints = 1,
  102. .bInterfaceClass = USB_CLASS_COMM,
  103. .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET,
  104. .bInterfaceProtocol = USB_CDC_PROTO_NONE,
  105. /* .iInterface = DYNAMIC */
  106. };
  107. static struct usb_cdc_header_desc ecm_header_desc = {
  108. .bLength = sizeof ecm_header_desc,
  109. .bDescriptorType = USB_DT_CS_INTERFACE,
  110. .bDescriptorSubType = USB_CDC_HEADER_TYPE,
  111. .bcdCDC = cpu_to_le16(0x0110),
  112. };
  113. static struct usb_cdc_union_desc ecm_union_desc = {
  114. .bLength = sizeof(ecm_union_desc),
  115. .bDescriptorType = USB_DT_CS_INTERFACE,
  116. .bDescriptorSubType = USB_CDC_UNION_TYPE,
  117. /* .bMasterInterface0 = DYNAMIC */
  118. /* .bSlaveInterface0 = DYNAMIC */
  119. };
  120. static struct usb_cdc_ether_desc ecm_desc = {
  121. .bLength = sizeof ecm_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. /* the default data interface has no endpoints ... */
  132. static struct usb_interface_descriptor ecm_data_nop_intf = {
  133. .bLength = sizeof ecm_data_nop_intf,
  134. .bDescriptorType = USB_DT_INTERFACE,
  135. .bInterfaceNumber = 1,
  136. .bAlternateSetting = 0,
  137. .bNumEndpoints = 0,
  138. .bInterfaceClass = USB_CLASS_CDC_DATA,
  139. .bInterfaceSubClass = 0,
  140. .bInterfaceProtocol = 0,
  141. /* .iInterface = DYNAMIC */
  142. };
  143. /* ... but the "real" data interface has two bulk endpoints */
  144. static struct usb_interface_descriptor ecm_data_intf = {
  145. .bLength = sizeof ecm_data_intf,
  146. .bDescriptorType = USB_DT_INTERFACE,
  147. .bInterfaceNumber = 1,
  148. .bAlternateSetting = 1,
  149. .bNumEndpoints = 2,
  150. .bInterfaceClass = USB_CLASS_CDC_DATA,
  151. .bInterfaceSubClass = 0,
  152. .bInterfaceProtocol = 0,
  153. /* .iInterface = DYNAMIC */
  154. };
  155. /* full speed support: */
  156. static struct usb_endpoint_descriptor fs_ecm_notify_desc = {
  157. .bLength = USB_DT_ENDPOINT_SIZE,
  158. .bDescriptorType = USB_DT_ENDPOINT,
  159. .bEndpointAddress = USB_DIR_IN,
  160. .bmAttributes = USB_ENDPOINT_XFER_INT,
  161. .wMaxPacketSize = cpu_to_le16(ECM_STATUS_BYTECOUNT),
  162. .bInterval = ECM_STATUS_INTERVAL_MS,
  163. };
  164. static struct usb_endpoint_descriptor fs_ecm_in_desc = {
  165. .bLength = USB_DT_ENDPOINT_SIZE,
  166. .bDescriptorType = USB_DT_ENDPOINT,
  167. .bEndpointAddress = USB_DIR_IN,
  168. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  169. };
  170. static struct usb_endpoint_descriptor fs_ecm_out_desc = {
  171. .bLength = USB_DT_ENDPOINT_SIZE,
  172. .bDescriptorType = USB_DT_ENDPOINT,
  173. .bEndpointAddress = USB_DIR_OUT,
  174. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  175. };
  176. static struct usb_descriptor_header *ecm_fs_function[] = {
  177. /* CDC ECM control descriptors */
  178. (struct usb_descriptor_header *) &ecm_iad_descriptor,
  179. (struct usb_descriptor_header *) &ecm_control_intf,
  180. (struct usb_descriptor_header *) &ecm_header_desc,
  181. (struct usb_descriptor_header *) &ecm_union_desc,
  182. (struct usb_descriptor_header *) &ecm_desc,
  183. /* NOTE: status endpoint might need to be removed */
  184. (struct usb_descriptor_header *) &fs_ecm_notify_desc,
  185. /* data interface, altsettings 0 and 1 */
  186. (struct usb_descriptor_header *) &ecm_data_nop_intf,
  187. (struct usb_descriptor_header *) &ecm_data_intf,
  188. (struct usb_descriptor_header *) &fs_ecm_in_desc,
  189. (struct usb_descriptor_header *) &fs_ecm_out_desc,
  190. NULL,
  191. };
  192. /* high speed support: */
  193. static struct usb_endpoint_descriptor hs_ecm_notify_desc = {
  194. .bLength = USB_DT_ENDPOINT_SIZE,
  195. .bDescriptorType = USB_DT_ENDPOINT,
  196. .bEndpointAddress = USB_DIR_IN,
  197. .bmAttributes = USB_ENDPOINT_XFER_INT,
  198. .wMaxPacketSize = cpu_to_le16(ECM_STATUS_BYTECOUNT),
  199. .bInterval = USB_MS_TO_HS_INTERVAL(ECM_STATUS_INTERVAL_MS),
  200. };
  201. static struct usb_endpoint_descriptor hs_ecm_in_desc = {
  202. .bLength = USB_DT_ENDPOINT_SIZE,
  203. .bDescriptorType = USB_DT_ENDPOINT,
  204. .bEndpointAddress = USB_DIR_IN,
  205. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  206. .wMaxPacketSize = cpu_to_le16(512),
  207. };
  208. static struct usb_endpoint_descriptor hs_ecm_out_desc = {
  209. .bLength = USB_DT_ENDPOINT_SIZE,
  210. .bDescriptorType = USB_DT_ENDPOINT,
  211. .bEndpointAddress = USB_DIR_OUT,
  212. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  213. .wMaxPacketSize = cpu_to_le16(512),
  214. };
  215. static struct usb_descriptor_header *ecm_hs_function[] = {
  216. /* CDC ECM control descriptors */
  217. (struct usb_descriptor_header *) &ecm_iad_descriptor,
  218. (struct usb_descriptor_header *) &ecm_control_intf,
  219. (struct usb_descriptor_header *) &ecm_header_desc,
  220. (struct usb_descriptor_header *) &ecm_union_desc,
  221. (struct usb_descriptor_header *) &ecm_desc,
  222. /* NOTE: status endpoint might need to be removed */
  223. (struct usb_descriptor_header *) &hs_ecm_notify_desc,
  224. /* data interface, altsettings 0 and 1 */
  225. (struct usb_descriptor_header *) &ecm_data_nop_intf,
  226. (struct usb_descriptor_header *) &ecm_data_intf,
  227. (struct usb_descriptor_header *) &hs_ecm_in_desc,
  228. (struct usb_descriptor_header *) &hs_ecm_out_desc,
  229. NULL,
  230. };
  231. /* super speed support: */
  232. static struct usb_endpoint_descriptor ss_ecm_notify_desc = {
  233. .bLength = USB_DT_ENDPOINT_SIZE,
  234. .bDescriptorType = USB_DT_ENDPOINT,
  235. .bEndpointAddress = USB_DIR_IN,
  236. .bmAttributes = USB_ENDPOINT_XFER_INT,
  237. .wMaxPacketSize = cpu_to_le16(ECM_STATUS_BYTECOUNT),
  238. .bInterval = USB_MS_TO_HS_INTERVAL(ECM_STATUS_INTERVAL_MS),
  239. };
  240. static struct usb_ss_ep_comp_descriptor ss_ecm_intr_comp_desc = {
  241. .bLength = sizeof ss_ecm_intr_comp_desc,
  242. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  243. /* the following 3 values can be tweaked if necessary */
  244. /* .bMaxBurst = 0, */
  245. /* .bmAttributes = 0, */
  246. .wBytesPerInterval = cpu_to_le16(ECM_STATUS_BYTECOUNT),
  247. };
  248. static struct usb_endpoint_descriptor ss_ecm_in_desc = {
  249. .bLength = USB_DT_ENDPOINT_SIZE,
  250. .bDescriptorType = USB_DT_ENDPOINT,
  251. .bEndpointAddress = USB_DIR_IN,
  252. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  253. .wMaxPacketSize = cpu_to_le16(1024),
  254. };
  255. static struct usb_endpoint_descriptor ss_ecm_out_desc = {
  256. .bLength = USB_DT_ENDPOINT_SIZE,
  257. .bDescriptorType = USB_DT_ENDPOINT,
  258. .bEndpointAddress = USB_DIR_OUT,
  259. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  260. .wMaxPacketSize = cpu_to_le16(1024),
  261. };
  262. static struct usb_ss_ep_comp_descriptor ss_ecm_bulk_comp_desc = {
  263. .bLength = sizeof ss_ecm_bulk_comp_desc,
  264. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  265. /* the following 2 values can be tweaked if necessary */
  266. /* .bMaxBurst = 0, */
  267. /* .bmAttributes = 0, */
  268. };
  269. static struct usb_descriptor_header *ecm_ss_function[] = {
  270. /* CDC ECM control descriptors */
  271. (struct usb_descriptor_header *) &ecm_iad_descriptor,
  272. (struct usb_descriptor_header *) &ecm_control_intf,
  273. (struct usb_descriptor_header *) &ecm_header_desc,
  274. (struct usb_descriptor_header *) &ecm_union_desc,
  275. (struct usb_descriptor_header *) &ecm_desc,
  276. /* NOTE: status endpoint might need to be removed */
  277. (struct usb_descriptor_header *) &ss_ecm_notify_desc,
  278. (struct usb_descriptor_header *) &ss_ecm_intr_comp_desc,
  279. /* data interface, altsettings 0 and 1 */
  280. (struct usb_descriptor_header *) &ecm_data_nop_intf,
  281. (struct usb_descriptor_header *) &ecm_data_intf,
  282. (struct usb_descriptor_header *) &ss_ecm_in_desc,
  283. (struct usb_descriptor_header *) &ss_ecm_bulk_comp_desc,
  284. (struct usb_descriptor_header *) &ss_ecm_out_desc,
  285. (struct usb_descriptor_header *) &ss_ecm_bulk_comp_desc,
  286. NULL,
  287. };
  288. /* string descriptors: */
  289. static struct usb_string ecm_string_defs[] = {
  290. [0].s = "CDC Ethernet Control Model (ECM)",
  291. [1].s = "",
  292. [2].s = "CDC Ethernet Data",
  293. [3].s = "CDC ECM",
  294. { } /* end of list */
  295. };
  296. static struct usb_gadget_strings ecm_string_table = {
  297. .language = 0x0409, /* en-us */
  298. .strings = ecm_string_defs,
  299. };
  300. static struct usb_gadget_strings *ecm_strings[] = {
  301. &ecm_string_table,
  302. NULL,
  303. };
  304. /*-------------------------------------------------------------------------*/
  305. static void ecm_do_notify(struct f_ecm *ecm)
  306. {
  307. struct usb_request *req = ecm->notify_req;
  308. struct usb_cdc_notification *event;
  309. struct usb_composite_dev *cdev = ecm->port.func.config->cdev;
  310. __le32 *data;
  311. int status;
  312. /* notification already in flight? */
  313. if (!req)
  314. return;
  315. event = req->buf;
  316. switch (ecm->notify_state) {
  317. case ECM_NOTIFY_NONE:
  318. return;
  319. case ECM_NOTIFY_CONNECT:
  320. event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
  321. if (ecm->is_open)
  322. event->wValue = cpu_to_le16(1);
  323. else
  324. event->wValue = cpu_to_le16(0);
  325. event->wLength = 0;
  326. req->length = sizeof *event;
  327. DBG(cdev, "notify connect %s\n",
  328. ecm->is_open ? "true" : "false");
  329. ecm->notify_state = ECM_NOTIFY_SPEED;
  330. break;
  331. case ECM_NOTIFY_SPEED:
  332. event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
  333. event->wValue = cpu_to_le16(0);
  334. event->wLength = cpu_to_le16(8);
  335. req->length = ECM_STATUS_BYTECOUNT;
  336. /* SPEED_CHANGE data is up/down speeds in bits/sec */
  337. data = req->buf + sizeof *event;
  338. data[0] = cpu_to_le32(ecm_bitrate(cdev->gadget));
  339. data[1] = data[0];
  340. DBG(cdev, "notify speed %d\n", ecm_bitrate(cdev->gadget));
  341. ecm->notify_state = ECM_NOTIFY_NONE;
  342. break;
  343. }
  344. event->bmRequestType = 0xA1;
  345. event->wIndex = cpu_to_le16(ecm->ctrl_id);
  346. ecm->notify_req = NULL;
  347. status = usb_ep_queue(ecm->notify, req, GFP_ATOMIC);
  348. if (status < 0) {
  349. ecm->notify_req = req;
  350. DBG(cdev, "notify --> %d\n", status);
  351. }
  352. }
  353. static void ecm_notify(struct f_ecm *ecm)
  354. {
  355. /* NOTE on most versions of Linux, host side cdc-ethernet
  356. * won't listen for notifications until its netdevice opens.
  357. * The first notification then sits in the FIFO for a long
  358. * time, and the second one is queued.
  359. */
  360. ecm->notify_state = ECM_NOTIFY_CONNECT;
  361. ecm_do_notify(ecm);
  362. }
  363. static void ecm_notify_complete(struct usb_ep *ep, struct usb_request *req)
  364. {
  365. struct f_ecm *ecm = req->context;
  366. struct usb_composite_dev *cdev = ecm->port.func.config->cdev;
  367. struct usb_cdc_notification *event = req->buf;
  368. switch (req->status) {
  369. case 0:
  370. /* no fault */
  371. break;
  372. case -ECONNRESET:
  373. case -ESHUTDOWN:
  374. ecm->notify_state = ECM_NOTIFY_NONE;
  375. break;
  376. default:
  377. DBG(cdev, "event %02x --> %d\n",
  378. event->bNotificationType, req->status);
  379. break;
  380. }
  381. ecm->notify_req = req;
  382. ecm_do_notify(ecm);
  383. }
  384. static int ecm_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
  385. {
  386. struct f_ecm *ecm = func_to_ecm(f);
  387. struct usb_composite_dev *cdev = f->config->cdev;
  388. struct usb_request *req = cdev->req;
  389. int value = -EOPNOTSUPP;
  390. u16 w_index = le16_to_cpu(ctrl->wIndex);
  391. u16 w_value = le16_to_cpu(ctrl->wValue);
  392. u16 w_length = le16_to_cpu(ctrl->wLength);
  393. /* composite driver infrastructure handles everything except
  394. * CDC class messages; interface activation uses set_alt().
  395. */
  396. switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
  397. case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
  398. | USB_CDC_SET_ETHERNET_PACKET_FILTER:
  399. /* see 6.2.30: no data, wIndex = interface,
  400. * wValue = packet filter bitmap
  401. */
  402. if (w_length != 0 || w_index != ecm->ctrl_id)
  403. goto invalid;
  404. DBG(cdev, "packet filter %02x\n", w_value);
  405. /* REVISIT locking of cdc_filter. This assumes the UDC
  406. * driver won't have a concurrent packet TX irq running on
  407. * another CPU; or that if it does, this write is atomic...
  408. */
  409. ecm->port.cdc_filter = w_value;
  410. value = 0;
  411. break;
  412. /* and optionally:
  413. * case USB_CDC_SEND_ENCAPSULATED_COMMAND:
  414. * case USB_CDC_GET_ENCAPSULATED_RESPONSE:
  415. * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
  416. * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
  417. * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
  418. * case USB_CDC_GET_ETHERNET_STATISTIC:
  419. */
  420. default:
  421. invalid:
  422. DBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
  423. ctrl->bRequestType, ctrl->bRequest,
  424. w_value, w_index, w_length);
  425. }
  426. /* respond with data transfer or status phase? */
  427. if (value >= 0) {
  428. DBG(cdev, "ecm req%02x.%02x v%04x i%04x l%d\n",
  429. ctrl->bRequestType, ctrl->bRequest,
  430. w_value, w_index, w_length);
  431. req->zero = 0;
  432. req->length = value;
  433. value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
  434. if (value < 0)
  435. ERROR(cdev, "ecm req %02x.%02x response err %d\n",
  436. ctrl->bRequestType, ctrl->bRequest,
  437. value);
  438. }
  439. /* device either stalls (value < 0) or reports success */
  440. return value;
  441. }
  442. static int ecm_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
  443. {
  444. struct f_ecm *ecm = func_to_ecm(f);
  445. struct usb_composite_dev *cdev = f->config->cdev;
  446. /* Control interface has only altsetting 0 */
  447. if (intf == ecm->ctrl_id) {
  448. if (alt != 0)
  449. goto fail;
  450. VDBG(cdev, "reset ecm control %d\n", intf);
  451. usb_ep_disable(ecm->notify);
  452. if (!(ecm->notify->desc)) {
  453. VDBG(cdev, "init ecm ctrl %d\n", intf);
  454. if (config_ep_by_speed(cdev->gadget, f, ecm->notify))
  455. goto fail;
  456. }
  457. usb_ep_enable(ecm->notify);
  458. /* Data interface has two altsettings, 0 and 1 */
  459. } else if (intf == ecm->data_id) {
  460. if (alt > 1)
  461. goto fail;
  462. if (ecm->port.in_ep->enabled) {
  463. DBG(cdev, "reset ecm\n");
  464. gether_disconnect(&ecm->port);
  465. }
  466. if (!ecm->port.in_ep->desc ||
  467. !ecm->port.out_ep->desc) {
  468. DBG(cdev, "init ecm\n");
  469. if (config_ep_by_speed(cdev->gadget, f,
  470. ecm->port.in_ep) ||
  471. config_ep_by_speed(cdev->gadget, f,
  472. ecm->port.out_ep)) {
  473. ecm->port.in_ep->desc = NULL;
  474. ecm->port.out_ep->desc = NULL;
  475. goto fail;
  476. }
  477. }
  478. /* CDC Ethernet only sends data in non-default altsettings.
  479. * Changing altsettings resets filters, statistics, etc.
  480. */
  481. if (alt == 1) {
  482. struct net_device *net;
  483. /* Enable zlps by default for ECM conformance;
  484. * override for musb_hdrc (avoids txdma ovhead).
  485. */
  486. ecm->port.is_zlp_ok =
  487. gadget_is_zlp_supported(cdev->gadget);
  488. ecm->port.cdc_filter = DEFAULT_FILTER;
  489. DBG(cdev, "activate ecm\n");
  490. net = gether_connect(&ecm->port);
  491. if (IS_ERR(net))
  492. return PTR_ERR(net);
  493. }
  494. /* NOTE this can be a minor disagreement with the ECM spec,
  495. * which says speed notifications will "always" follow
  496. * connection notifications. But we allow one connect to
  497. * follow another (if the first is in flight), and instead
  498. * just guarantee that a speed notification is always sent.
  499. */
  500. ecm_notify(ecm);
  501. } else
  502. goto fail;
  503. return 0;
  504. fail:
  505. return -EINVAL;
  506. }
  507. /* Because the data interface supports multiple altsettings,
  508. * this ECM function *MUST* implement a get_alt() method.
  509. */
  510. static int ecm_get_alt(struct usb_function *f, unsigned intf)
  511. {
  512. struct f_ecm *ecm = func_to_ecm(f);
  513. if (intf == ecm->ctrl_id)
  514. return 0;
  515. return ecm->port.in_ep->enabled ? 1 : 0;
  516. }
  517. static void ecm_disable(struct usb_function *f)
  518. {
  519. struct f_ecm *ecm = func_to_ecm(f);
  520. struct usb_composite_dev *cdev = f->config->cdev;
  521. DBG(cdev, "ecm deactivated\n");
  522. if (ecm->port.in_ep->enabled)
  523. gether_disconnect(&ecm->port);
  524. usb_ep_disable(ecm->notify);
  525. ecm->notify->desc = NULL;
  526. }
  527. /*-------------------------------------------------------------------------*/
  528. /*
  529. * Callbacks let us notify the host about connect/disconnect when the
  530. * net device is opened or closed.
  531. *
  532. * For testing, note that link states on this side include both opened
  533. * and closed variants of:
  534. *
  535. * - disconnected/unconfigured
  536. * - configured but inactive (data alt 0)
  537. * - configured and active (data alt 1)
  538. *
  539. * Each needs to be tested with unplug, rmmod, SET_CONFIGURATION, and
  540. * SET_INTERFACE (altsetting). Remember also that "configured" doesn't
  541. * imply the host is actually polling the notification endpoint, and
  542. * likewise that "active" doesn't imply it's actually using the data
  543. * endpoints for traffic.
  544. */
  545. static void ecm_open(struct gether *geth)
  546. {
  547. struct f_ecm *ecm = func_to_ecm(&geth->func);
  548. DBG(ecm->port.func.config->cdev, "%s\n", __func__);
  549. ecm->is_open = true;
  550. ecm_notify(ecm);
  551. }
  552. static void ecm_close(struct gether *geth)
  553. {
  554. struct f_ecm *ecm = func_to_ecm(&geth->func);
  555. DBG(ecm->port.func.config->cdev, "%s\n", __func__);
  556. ecm->is_open = false;
  557. ecm_notify(ecm);
  558. }
  559. /*-------------------------------------------------------------------------*/
  560. /* ethernet function driver setup/binding */
  561. static int
  562. ecm_bind(struct usb_configuration *c, struct usb_function *f)
  563. {
  564. struct usb_composite_dev *cdev = c->cdev;
  565. struct f_ecm *ecm = func_to_ecm(f);
  566. struct usb_string *us;
  567. int status;
  568. struct usb_ep *ep;
  569. struct f_ecm_opts *ecm_opts;
  570. if (!can_support_ecm(cdev->gadget))
  571. return -EINVAL;
  572. ecm_opts = container_of(f->fi, struct f_ecm_opts, func_inst);
  573. /*
  574. * in drivers/usb/gadget/configfs.c:configfs_composite_bind()
  575. * configurations are bound in sequence with list_for_each_entry,
  576. * in each configuration its functions are bound in sequence
  577. * with list_for_each_entry, so we assume no race condition
  578. * with regard to ecm_opts->bound access
  579. */
  580. if (!ecm_opts->bound) {
  581. mutex_lock(&ecm_opts->lock);
  582. gether_set_gadget(ecm_opts->net, cdev->gadget);
  583. status = gether_register_netdev(ecm_opts->net);
  584. mutex_unlock(&ecm_opts->lock);
  585. if (status)
  586. return status;
  587. ecm_opts->bound = true;
  588. }
  589. us = usb_gstrings_attach(cdev, ecm_strings,
  590. ARRAY_SIZE(ecm_string_defs));
  591. if (IS_ERR(us))
  592. return PTR_ERR(us);
  593. ecm_control_intf.iInterface = us[0].id;
  594. ecm_data_intf.iInterface = us[2].id;
  595. ecm_desc.iMACAddress = us[1].id;
  596. ecm_iad_descriptor.iFunction = us[3].id;
  597. /* allocate instance-specific interface IDs */
  598. status = usb_interface_id(c, f);
  599. if (status < 0)
  600. goto fail;
  601. ecm->ctrl_id = status;
  602. ecm_iad_descriptor.bFirstInterface = status;
  603. ecm_control_intf.bInterfaceNumber = status;
  604. ecm_union_desc.bMasterInterface0 = status;
  605. status = usb_interface_id(c, f);
  606. if (status < 0)
  607. goto fail;
  608. ecm->data_id = status;
  609. ecm_data_nop_intf.bInterfaceNumber = status;
  610. ecm_data_intf.bInterfaceNumber = status;
  611. ecm_union_desc.bSlaveInterface0 = status;
  612. status = -ENODEV;
  613. /* allocate instance-specific endpoints */
  614. ep = usb_ep_autoconfig(cdev->gadget, &fs_ecm_in_desc);
  615. if (!ep)
  616. goto fail;
  617. ecm->port.in_ep = ep;
  618. ep = usb_ep_autoconfig(cdev->gadget, &fs_ecm_out_desc);
  619. if (!ep)
  620. goto fail;
  621. ecm->port.out_ep = ep;
  622. /* NOTE: a status/notification endpoint is *OPTIONAL* but we
  623. * don't treat it that way. It's simpler, and some newer CDC
  624. * profiles (wireless handsets) no longer treat it as optional.
  625. */
  626. ep = usb_ep_autoconfig(cdev->gadget, &fs_ecm_notify_desc);
  627. if (!ep)
  628. goto fail;
  629. ecm->notify = ep;
  630. status = -ENOMEM;
  631. /* allocate notification request and buffer */
  632. ecm->notify_req = usb_ep_alloc_request(ep, GFP_KERNEL);
  633. if (!ecm->notify_req)
  634. goto fail;
  635. ecm->notify_req->buf = kmalloc(ECM_STATUS_BYTECOUNT, GFP_KERNEL);
  636. if (!ecm->notify_req->buf)
  637. goto fail;
  638. ecm->notify_req->context = ecm;
  639. ecm->notify_req->complete = ecm_notify_complete;
  640. /* support all relevant hardware speeds... we expect that when
  641. * hardware is dual speed, all bulk-capable endpoints work at
  642. * both speeds
  643. */
  644. hs_ecm_in_desc.bEndpointAddress = fs_ecm_in_desc.bEndpointAddress;
  645. hs_ecm_out_desc.bEndpointAddress = fs_ecm_out_desc.bEndpointAddress;
  646. hs_ecm_notify_desc.bEndpointAddress =
  647. fs_ecm_notify_desc.bEndpointAddress;
  648. ss_ecm_in_desc.bEndpointAddress = fs_ecm_in_desc.bEndpointAddress;
  649. ss_ecm_out_desc.bEndpointAddress = fs_ecm_out_desc.bEndpointAddress;
  650. ss_ecm_notify_desc.bEndpointAddress =
  651. fs_ecm_notify_desc.bEndpointAddress;
  652. status = usb_assign_descriptors(f, ecm_fs_function, ecm_hs_function,
  653. ecm_ss_function, NULL);
  654. if (status)
  655. goto fail;
  656. /* NOTE: all that is done without knowing or caring about
  657. * the network link ... which is unavailable to this code
  658. * until we're activated via set_alt().
  659. */
  660. ecm->port.open = ecm_open;
  661. ecm->port.close = ecm_close;
  662. DBG(cdev, "CDC Ethernet: %s speed IN/%s OUT/%s NOTIFY/%s\n",
  663. gadget_is_superspeed(c->cdev->gadget) ? "super" :
  664. gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
  665. ecm->port.in_ep->name, ecm->port.out_ep->name,
  666. ecm->notify->name);
  667. return 0;
  668. fail:
  669. if (ecm->notify_req) {
  670. kfree(ecm->notify_req->buf);
  671. usb_ep_free_request(ecm->notify, ecm->notify_req);
  672. }
  673. ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
  674. return status;
  675. }
  676. static inline struct f_ecm_opts *to_f_ecm_opts(struct config_item *item)
  677. {
  678. return container_of(to_config_group(item), struct f_ecm_opts,
  679. func_inst.group);
  680. }
  681. /* f_ecm_item_ops */
  682. USB_ETHERNET_CONFIGFS_ITEM(ecm);
  683. /* f_ecm_opts_dev_addr */
  684. USB_ETHERNET_CONFIGFS_ITEM_ATTR_DEV_ADDR(ecm);
  685. /* f_ecm_opts_host_addr */
  686. USB_ETHERNET_CONFIGFS_ITEM_ATTR_HOST_ADDR(ecm);
  687. /* f_ecm_opts_qmult */
  688. USB_ETHERNET_CONFIGFS_ITEM_ATTR_QMULT(ecm);
  689. /* f_ecm_opts_ifname */
  690. USB_ETHERNET_CONFIGFS_ITEM_ATTR_IFNAME(ecm);
  691. static struct configfs_attribute *ecm_attrs[] = {
  692. &ecm_opts_attr_dev_addr,
  693. &ecm_opts_attr_host_addr,
  694. &ecm_opts_attr_qmult,
  695. &ecm_opts_attr_ifname,
  696. NULL,
  697. };
  698. static struct config_item_type ecm_func_type = {
  699. .ct_item_ops = &ecm_item_ops,
  700. .ct_attrs = ecm_attrs,
  701. .ct_owner = THIS_MODULE,
  702. };
  703. static void ecm_free_inst(struct usb_function_instance *f)
  704. {
  705. struct f_ecm_opts *opts;
  706. opts = container_of(f, struct f_ecm_opts, func_inst);
  707. if (opts->bound)
  708. gether_cleanup(netdev_priv(opts->net));
  709. else
  710. free_netdev(opts->net);
  711. kfree(opts);
  712. }
  713. static struct usb_function_instance *ecm_alloc_inst(void)
  714. {
  715. struct f_ecm_opts *opts;
  716. opts = kzalloc(sizeof(*opts), GFP_KERNEL);
  717. if (!opts)
  718. return ERR_PTR(-ENOMEM);
  719. mutex_init(&opts->lock);
  720. opts->func_inst.free_func_inst = ecm_free_inst;
  721. opts->net = gether_setup_default();
  722. if (IS_ERR(opts->net)) {
  723. struct net_device *net = opts->net;
  724. kfree(opts);
  725. return ERR_CAST(net);
  726. }
  727. config_group_init_type_name(&opts->func_inst.group, "", &ecm_func_type);
  728. return &opts->func_inst;
  729. }
  730. static void ecm_free(struct usb_function *f)
  731. {
  732. struct f_ecm *ecm;
  733. struct f_ecm_opts *opts;
  734. ecm = func_to_ecm(f);
  735. opts = container_of(f->fi, struct f_ecm_opts, func_inst);
  736. kfree(ecm);
  737. mutex_lock(&opts->lock);
  738. opts->refcnt--;
  739. mutex_unlock(&opts->lock);
  740. }
  741. static void ecm_unbind(struct usb_configuration *c, struct usb_function *f)
  742. {
  743. struct f_ecm *ecm = func_to_ecm(f);
  744. DBG(c->cdev, "ecm unbind\n");
  745. usb_free_all_descriptors(f);
  746. kfree(ecm->notify_req->buf);
  747. usb_ep_free_request(ecm->notify, ecm->notify_req);
  748. }
  749. static struct usb_function *ecm_alloc(struct usb_function_instance *fi)
  750. {
  751. struct f_ecm *ecm;
  752. struct f_ecm_opts *opts;
  753. int status;
  754. /* allocate and initialize one new instance */
  755. ecm = kzalloc(sizeof(*ecm), GFP_KERNEL);
  756. if (!ecm)
  757. return ERR_PTR(-ENOMEM);
  758. opts = container_of(fi, struct f_ecm_opts, func_inst);
  759. mutex_lock(&opts->lock);
  760. opts->refcnt++;
  761. /* export host's Ethernet address in CDC format */
  762. status = gether_get_host_addr_cdc(opts->net, ecm->ethaddr,
  763. sizeof(ecm->ethaddr));
  764. if (status < 12) {
  765. kfree(ecm);
  766. mutex_unlock(&opts->lock);
  767. return ERR_PTR(-EINVAL);
  768. }
  769. ecm_string_defs[1].s = ecm->ethaddr;
  770. ecm->port.ioport = netdev_priv(opts->net);
  771. mutex_unlock(&opts->lock);
  772. ecm->port.cdc_filter = DEFAULT_FILTER;
  773. ecm->port.func.name = "cdc_ethernet";
  774. /* descriptors are per-instance copies */
  775. ecm->port.func.bind = ecm_bind;
  776. ecm->port.func.unbind = ecm_unbind;
  777. ecm->port.func.set_alt = ecm_set_alt;
  778. ecm->port.func.get_alt = ecm_get_alt;
  779. ecm->port.func.setup = ecm_setup;
  780. ecm->port.func.disable = ecm_disable;
  781. ecm->port.func.free_func = ecm_free;
  782. return &ecm->port.func;
  783. }
  784. DECLARE_USB_FUNCTION_INIT(ecm, ecm_alloc_inst, ecm_alloc);
  785. MODULE_LICENSE("GPL");
  786. MODULE_AUTHOR("David Brownell");