f_ecm.c 27 KB

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