f_ecm.c 26 KB

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