f_ecm.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871
  1. /*
  2. * f_ecm.c -- USB CDC Ethernet (ECM) link function driver
  3. *
  4. * Copyright (C) 2003-2005,2008 David Brownell
  5. * Copyright (C) 2008 Nokia Corporation
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. */
  12. /* #define VERBOSE_DEBUG */
  13. #include <linux/slab.h>
  14. #include <linux/kernel.h>
  15. #include <linux/device.h>
  16. #include <linux/etherdevice.h>
  17. #include "u_ether.h"
  18. /*
  19. * This function is a "CDC Ethernet Networking Control Model" (CDC ECM)
  20. * Ethernet link. The data transfer model is simple (packets sent and
  21. * received over bulk endpoints using normal short packet termination),
  22. * and the control model exposes various data and optional notifications.
  23. *
  24. * ECM is well standardized and (except for Microsoft) supported by most
  25. * operating systems with USB host support. It's the preferred interop
  26. * solution for Ethernet over USB, at least for firmware based solutions.
  27. * (Hardware solutions tend to be more minimalist.) A newer and simpler
  28. * "Ethernet Emulation Model" (CDC EEM) hasn't yet caught on.
  29. *
  30. * Note that ECM requires the use of "alternate settings" for its data
  31. * interface. This means that the set_alt() method has real work to do,
  32. * and also means that a get_alt() method is required.
  33. */
  34. enum ecm_notify_state {
  35. ECM_NOTIFY_NONE, /* don't notify */
  36. ECM_NOTIFY_CONNECT, /* issue CONNECT next */
  37. ECM_NOTIFY_SPEED, /* issue SPEED_CHANGE next */
  38. };
  39. struct f_ecm {
  40. struct gether port;
  41. u8 ctrl_id, data_id;
  42. char ethaddr[14];
  43. struct usb_ep *notify;
  44. struct usb_request *notify_req;
  45. u8 notify_state;
  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 (!req)
  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. ecm->notify_req = NULL;
  343. status = usb_ep_queue(ecm->notify, req, GFP_ATOMIC);
  344. if (status < 0) {
  345. ecm->notify_req = req;
  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. break;
  368. case -ECONNRESET:
  369. case -ESHUTDOWN:
  370. ecm->notify_state = ECM_NOTIFY_NONE;
  371. break;
  372. default:
  373. DBG(cdev, "event %02x --> %d\n",
  374. event->bNotificationType, req->status);
  375. break;
  376. }
  377. ecm->notify_req = req;
  378. ecm_do_notify(ecm);
  379. }
  380. static int ecm_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
  381. {
  382. struct f_ecm *ecm = func_to_ecm(f);
  383. struct usb_composite_dev *cdev = f->config->cdev;
  384. struct usb_request *req = cdev->req;
  385. int value = -EOPNOTSUPP;
  386. u16 w_index = le16_to_cpu(ctrl->wIndex);
  387. u16 w_value = le16_to_cpu(ctrl->wValue);
  388. u16 w_length = le16_to_cpu(ctrl->wLength);
  389. /* composite driver infrastructure handles everything except
  390. * CDC class messages; interface activation uses set_alt().
  391. */
  392. switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
  393. case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
  394. | USB_CDC_SET_ETHERNET_PACKET_FILTER:
  395. /* see 6.2.30: no data, wIndex = interface,
  396. * wValue = packet filter bitmap
  397. */
  398. if (w_length != 0 || w_index != ecm->ctrl_id)
  399. goto invalid;
  400. DBG(cdev, "packet filter %02x\n", w_value);
  401. /* REVISIT locking of cdc_filter. This assumes the UDC
  402. * driver won't have a concurrent packet TX irq running on
  403. * another CPU; or that if it does, this write is atomic...
  404. */
  405. ecm->port.cdc_filter = w_value;
  406. value = 0;
  407. break;
  408. /* and optionally:
  409. * case USB_CDC_SEND_ENCAPSULATED_COMMAND:
  410. * case USB_CDC_GET_ENCAPSULATED_RESPONSE:
  411. * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
  412. * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
  413. * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
  414. * case USB_CDC_GET_ETHERNET_STATISTIC:
  415. */
  416. default:
  417. invalid:
  418. DBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
  419. ctrl->bRequestType, ctrl->bRequest,
  420. w_value, w_index, w_length);
  421. }
  422. /* respond with data transfer or status phase? */
  423. if (value >= 0) {
  424. DBG(cdev, "ecm req%02x.%02x v%04x i%04x l%d\n",
  425. ctrl->bRequestType, ctrl->bRequest,
  426. w_value, w_index, w_length);
  427. req->zero = 0;
  428. req->length = value;
  429. value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
  430. if (value < 0)
  431. ERROR(cdev, "ecm req %02x.%02x response err %d\n",
  432. ctrl->bRequestType, ctrl->bRequest,
  433. value);
  434. }
  435. /* device either stalls (value < 0) or reports success */
  436. return value;
  437. }
  438. static int ecm_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
  439. {
  440. struct f_ecm *ecm = func_to_ecm(f);
  441. struct usb_composite_dev *cdev = f->config->cdev;
  442. /* Control interface has only altsetting 0 */
  443. if (intf == ecm->ctrl_id) {
  444. if (alt != 0)
  445. goto fail;
  446. if (ecm->notify->driver_data) {
  447. VDBG(cdev, "reset ecm control %d\n", intf);
  448. usb_ep_disable(ecm->notify);
  449. }
  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. ecm->notify->driver_data = ecm;
  457. /* Data interface has two altsettings, 0 and 1 */
  458. } else if (intf == ecm->data_id) {
  459. if (alt > 1)
  460. goto fail;
  461. if (ecm->port.in_ep->driver_data) {
  462. DBG(cdev, "reset ecm\n");
  463. gether_disconnect(&ecm->port);
  464. }
  465. if (!ecm->port.in_ep->desc ||
  466. !ecm->port.out_ep->desc) {
  467. DBG(cdev, "init ecm\n");
  468. if (config_ep_by_speed(cdev->gadget, f,
  469. ecm->port.in_ep) ||
  470. config_ep_by_speed(cdev->gadget, f,
  471. ecm->port.out_ep)) {
  472. ecm->port.in_ep->desc = NULL;
  473. ecm->port.out_ep->desc = NULL;
  474. goto fail;
  475. }
  476. }
  477. /* CDC Ethernet only sends data in non-default altsettings.
  478. * Changing altsettings resets filters, statistics, etc.
  479. */
  480. if (alt == 1) {
  481. struct net_device *net;
  482. /* Enable zlps by default for ECM conformance;
  483. * override for musb_hdrc (avoids txdma ovhead).
  484. */
  485. ecm->port.is_zlp_ok = !(gadget_is_musbhdrc(cdev->gadget)
  486. );
  487. ecm->port.cdc_filter = DEFAULT_FILTER;
  488. DBG(cdev, "activate ecm\n");
  489. net = gether_connect(&ecm->port);
  490. if (IS_ERR(net))
  491. return PTR_ERR(net);
  492. }
  493. /* NOTE this can be a minor disagreement with the ECM spec,
  494. * which says speed notifications will "always" follow
  495. * connection notifications. But we allow one connect to
  496. * follow another (if the first is in flight), and instead
  497. * just guarantee that a speed notification is always sent.
  498. */
  499. ecm_notify(ecm);
  500. } else
  501. goto fail;
  502. return 0;
  503. fail:
  504. return -EINVAL;
  505. }
  506. /* Because the data interface supports multiple altsettings,
  507. * this ECM function *MUST* implement a get_alt() method.
  508. */
  509. static int ecm_get_alt(struct usb_function *f, unsigned intf)
  510. {
  511. struct f_ecm *ecm = func_to_ecm(f);
  512. if (intf == ecm->ctrl_id)
  513. return 0;
  514. return ecm->port.in_ep->driver_data ? 1 : 0;
  515. }
  516. static void ecm_disable(struct usb_function *f)
  517. {
  518. struct f_ecm *ecm = func_to_ecm(f);
  519. struct usb_composite_dev *cdev = f->config->cdev;
  520. DBG(cdev, "ecm deactivated\n");
  521. if (ecm->port.in_ep->driver_data)
  522. gether_disconnect(&ecm->port);
  523. if (ecm->notify->driver_data) {
  524. usb_ep_disable(ecm->notify);
  525. ecm->notify->driver_data = NULL;
  526. ecm->notify->desc = NULL;
  527. }
  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. int status;
  569. struct usb_ep *ep;
  570. /* allocate instance-specific interface IDs */
  571. status = usb_interface_id(c, f);
  572. if (status < 0)
  573. goto fail;
  574. ecm->ctrl_id = status;
  575. ecm_iad_descriptor.bFirstInterface = status;
  576. ecm_control_intf.bInterfaceNumber = status;
  577. ecm_union_desc.bMasterInterface0 = status;
  578. status = usb_interface_id(c, f);
  579. if (status < 0)
  580. goto fail;
  581. ecm->data_id = status;
  582. ecm_data_nop_intf.bInterfaceNumber = status;
  583. ecm_data_intf.bInterfaceNumber = status;
  584. ecm_union_desc.bSlaveInterface0 = status;
  585. status = -ENODEV;
  586. /* allocate instance-specific endpoints */
  587. ep = usb_ep_autoconfig(cdev->gadget, &fs_ecm_in_desc);
  588. if (!ep)
  589. goto fail;
  590. ecm->port.in_ep = ep;
  591. ep->driver_data = cdev; /* claim */
  592. ep = usb_ep_autoconfig(cdev->gadget, &fs_ecm_out_desc);
  593. if (!ep)
  594. goto fail;
  595. ecm->port.out_ep = ep;
  596. ep->driver_data = cdev; /* claim */
  597. /* NOTE: a status/notification endpoint is *OPTIONAL* but we
  598. * don't treat it that way. It's simpler, and some newer CDC
  599. * profiles (wireless handsets) no longer treat it as optional.
  600. */
  601. ep = usb_ep_autoconfig(cdev->gadget, &fs_ecm_notify_desc);
  602. if (!ep)
  603. goto fail;
  604. ecm->notify = ep;
  605. ep->driver_data = cdev; /* claim */
  606. status = -ENOMEM;
  607. /* allocate notification request and buffer */
  608. ecm->notify_req = usb_ep_alloc_request(ep, GFP_KERNEL);
  609. if (!ecm->notify_req)
  610. goto fail;
  611. ecm->notify_req->buf = kmalloc(ECM_STATUS_BYTECOUNT, GFP_KERNEL);
  612. if (!ecm->notify_req->buf)
  613. goto fail;
  614. ecm->notify_req->context = ecm;
  615. ecm->notify_req->complete = ecm_notify_complete;
  616. /* support all relevant hardware speeds... we expect that when
  617. * hardware is dual speed, all bulk-capable endpoints work at
  618. * both speeds
  619. */
  620. hs_ecm_in_desc.bEndpointAddress = fs_ecm_in_desc.bEndpointAddress;
  621. hs_ecm_out_desc.bEndpointAddress = fs_ecm_out_desc.bEndpointAddress;
  622. hs_ecm_notify_desc.bEndpointAddress =
  623. fs_ecm_notify_desc.bEndpointAddress;
  624. ss_ecm_in_desc.bEndpointAddress = fs_ecm_in_desc.bEndpointAddress;
  625. ss_ecm_out_desc.bEndpointAddress = fs_ecm_out_desc.bEndpointAddress;
  626. ss_ecm_notify_desc.bEndpointAddress =
  627. fs_ecm_notify_desc.bEndpointAddress;
  628. status = usb_assign_descriptors(f, ecm_fs_function, ecm_hs_function,
  629. ecm_ss_function);
  630. if (status)
  631. goto fail;
  632. /* NOTE: all that is done without knowing or caring about
  633. * the network link ... which is unavailable to this code
  634. * until we're activated via set_alt().
  635. */
  636. ecm->port.open = ecm_open;
  637. ecm->port.close = ecm_close;
  638. DBG(cdev, "CDC Ethernet: %s speed IN/%s OUT/%s NOTIFY/%s\n",
  639. gadget_is_superspeed(c->cdev->gadget) ? "super" :
  640. gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
  641. ecm->port.in_ep->name, ecm->port.out_ep->name,
  642. ecm->notify->name);
  643. return 0;
  644. fail:
  645. if (ecm->notify_req) {
  646. kfree(ecm->notify_req->buf);
  647. usb_ep_free_request(ecm->notify, ecm->notify_req);
  648. }
  649. /* we might as well release our claims on endpoints */
  650. if (ecm->notify)
  651. ecm->notify->driver_data = NULL;
  652. if (ecm->port.out_ep)
  653. ecm->port.out_ep->driver_data = NULL;
  654. if (ecm->port.in_ep)
  655. ecm->port.in_ep->driver_data = NULL;
  656. ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
  657. return status;
  658. }
  659. static void
  660. ecm_unbind(struct usb_configuration *c, struct usb_function *f)
  661. {
  662. struct f_ecm *ecm = func_to_ecm(f);
  663. DBG(c->cdev, "ecm unbind\n");
  664. ecm_string_defs[0].id = 0;
  665. usb_free_all_descriptors(f);
  666. kfree(ecm->notify_req->buf);
  667. usb_ep_free_request(ecm->notify, ecm->notify_req);
  668. kfree(ecm);
  669. }
  670. /**
  671. * ecm_bind_config - add CDC Ethernet network link to a configuration
  672. * @c: the configuration to support the network link
  673. * @ethaddr: a buffer in which the ethernet address of the host side
  674. * side of the link was recorded
  675. * Context: single threaded during gadget setup
  676. *
  677. * Returns zero on success, else negative errno.
  678. *
  679. * Caller must have called @gether_setup(). Caller is also responsible
  680. * for calling @gether_cleanup() before module unload.
  681. */
  682. int
  683. ecm_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN])
  684. {
  685. struct f_ecm *ecm;
  686. int status;
  687. if (!can_support_ecm(c->cdev->gadget) || !ethaddr)
  688. return -EINVAL;
  689. if (ecm_string_defs[0].id == 0) {
  690. status = usb_string_ids_tab(c->cdev, ecm_string_defs);
  691. if (status)
  692. return status;
  693. ecm_control_intf.iInterface = ecm_string_defs[0].id;
  694. ecm_data_intf.iInterface = ecm_string_defs[2].id;
  695. ecm_desc.iMACAddress = ecm_string_defs[1].id;
  696. ecm_iad_descriptor.iFunction = ecm_string_defs[3].id;
  697. }
  698. /* allocate and initialize one new instance */
  699. ecm = kzalloc(sizeof *ecm, GFP_KERNEL);
  700. if (!ecm)
  701. return -ENOMEM;
  702. /* export host's Ethernet address in CDC format */
  703. snprintf(ecm->ethaddr, sizeof ecm->ethaddr, "%pm", ethaddr);
  704. ecm_string_defs[1].s = ecm->ethaddr;
  705. ecm->port.cdc_filter = DEFAULT_FILTER;
  706. ecm->port.func.name = "cdc_ethernet";
  707. ecm->port.func.strings = ecm_strings;
  708. /* descriptors are per-instance copies */
  709. ecm->port.func.bind = ecm_bind;
  710. ecm->port.func.unbind = ecm_unbind;
  711. ecm->port.func.set_alt = ecm_set_alt;
  712. ecm->port.func.get_alt = ecm_get_alt;
  713. ecm->port.func.setup = ecm_setup;
  714. ecm->port.func.disable = ecm_disable;
  715. status = usb_add_function(c, &ecm->port.func);
  716. if (status)
  717. kfree(ecm);
  718. return status;
  719. }