f_ecm.c 26 KB

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