f_ecm.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  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 LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
  79. #define ECM_STATUS_BYTECOUNT 16 /* 8 byte header + data */
  80. /* interface descriptor: */
  81. static struct usb_interface_descriptor ecm_control_intf = {
  82. .bLength = sizeof ecm_control_intf,
  83. .bDescriptorType = USB_DT_INTERFACE,
  84. /* .bInterfaceNumber = DYNAMIC */
  85. /* status endpoint is optional; this could be patched later */
  86. .bNumEndpoints = 1,
  87. .bInterfaceClass = USB_CLASS_COMM,
  88. .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET,
  89. .bInterfaceProtocol = USB_CDC_PROTO_NONE,
  90. /* .iInterface = DYNAMIC */
  91. };
  92. static struct usb_cdc_header_desc ecm_header_desc = {
  93. .bLength = sizeof ecm_header_desc,
  94. .bDescriptorType = USB_DT_CS_INTERFACE,
  95. .bDescriptorSubType = USB_CDC_HEADER_TYPE,
  96. .bcdCDC = cpu_to_le16(0x0110),
  97. };
  98. static struct usb_cdc_union_desc ecm_union_desc = {
  99. .bLength = sizeof(ecm_union_desc),
  100. .bDescriptorType = USB_DT_CS_INTERFACE,
  101. .bDescriptorSubType = USB_CDC_UNION_TYPE,
  102. /* .bMasterInterface0 = DYNAMIC */
  103. /* .bSlaveInterface0 = DYNAMIC */
  104. };
  105. static struct usb_cdc_ether_desc ecm_desc = {
  106. .bLength = sizeof ecm_desc,
  107. .bDescriptorType = USB_DT_CS_INTERFACE,
  108. .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
  109. /* this descriptor actually adds value, surprise! */
  110. /* .iMACAddress = DYNAMIC */
  111. .bmEthernetStatistics = cpu_to_le32(0), /* no statistics */
  112. .wMaxSegmentSize = cpu_to_le16(ETH_FRAME_LEN),
  113. .wNumberMCFilters = cpu_to_le16(0),
  114. .bNumberPowerFilters = 0,
  115. };
  116. /* the default data interface has no endpoints ... */
  117. static struct usb_interface_descriptor ecm_data_nop_intf = {
  118. .bLength = sizeof ecm_data_nop_intf,
  119. .bDescriptorType = USB_DT_INTERFACE,
  120. .bInterfaceNumber = 1,
  121. .bAlternateSetting = 0,
  122. .bNumEndpoints = 0,
  123. .bInterfaceClass = USB_CLASS_CDC_DATA,
  124. .bInterfaceSubClass = 0,
  125. .bInterfaceProtocol = 0,
  126. /* .iInterface = DYNAMIC */
  127. };
  128. /* ... but the "real" data interface has two bulk endpoints */
  129. static struct usb_interface_descriptor ecm_data_intf = {
  130. .bLength = sizeof ecm_data_intf,
  131. .bDescriptorType = USB_DT_INTERFACE,
  132. .bInterfaceNumber = 1,
  133. .bAlternateSetting = 1,
  134. .bNumEndpoints = 2,
  135. .bInterfaceClass = USB_CLASS_CDC_DATA,
  136. .bInterfaceSubClass = 0,
  137. .bInterfaceProtocol = 0,
  138. /* .iInterface = DYNAMIC */
  139. };
  140. /* full speed support: */
  141. static struct usb_endpoint_descriptor fs_ecm_notify_desc = {
  142. .bLength = USB_DT_ENDPOINT_SIZE,
  143. .bDescriptorType = USB_DT_ENDPOINT,
  144. .bEndpointAddress = USB_DIR_IN,
  145. .bmAttributes = USB_ENDPOINT_XFER_INT,
  146. .wMaxPacketSize = cpu_to_le16(ECM_STATUS_BYTECOUNT),
  147. .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
  148. };
  149. static struct usb_endpoint_descriptor fs_ecm_in_desc = {
  150. .bLength = USB_DT_ENDPOINT_SIZE,
  151. .bDescriptorType = USB_DT_ENDPOINT,
  152. .bEndpointAddress = USB_DIR_IN,
  153. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  154. };
  155. static struct usb_endpoint_descriptor fs_ecm_out_desc = {
  156. .bLength = USB_DT_ENDPOINT_SIZE,
  157. .bDescriptorType = USB_DT_ENDPOINT,
  158. .bEndpointAddress = USB_DIR_OUT,
  159. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  160. };
  161. static struct usb_descriptor_header *ecm_fs_function[] = {
  162. /* CDC ECM control descriptors */
  163. (struct usb_descriptor_header *) &ecm_control_intf,
  164. (struct usb_descriptor_header *) &ecm_header_desc,
  165. (struct usb_descriptor_header *) &ecm_union_desc,
  166. (struct usb_descriptor_header *) &ecm_desc,
  167. /* NOTE: status endpoint might need to be removed */
  168. (struct usb_descriptor_header *) &fs_ecm_notify_desc,
  169. /* data interface, altsettings 0 and 1 */
  170. (struct usb_descriptor_header *) &ecm_data_nop_intf,
  171. (struct usb_descriptor_header *) &ecm_data_intf,
  172. (struct usb_descriptor_header *) &fs_ecm_in_desc,
  173. (struct usb_descriptor_header *) &fs_ecm_out_desc,
  174. NULL,
  175. };
  176. /* high speed support: */
  177. static struct usb_endpoint_descriptor hs_ecm_notify_desc = {
  178. .bLength = USB_DT_ENDPOINT_SIZE,
  179. .bDescriptorType = USB_DT_ENDPOINT,
  180. .bEndpointAddress = USB_DIR_IN,
  181. .bmAttributes = USB_ENDPOINT_XFER_INT,
  182. .wMaxPacketSize = cpu_to_le16(ECM_STATUS_BYTECOUNT),
  183. .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
  184. };
  185. static struct usb_endpoint_descriptor hs_ecm_in_desc = {
  186. .bLength = USB_DT_ENDPOINT_SIZE,
  187. .bDescriptorType = USB_DT_ENDPOINT,
  188. .bEndpointAddress = USB_DIR_IN,
  189. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  190. .wMaxPacketSize = cpu_to_le16(512),
  191. };
  192. static struct usb_endpoint_descriptor hs_ecm_out_desc = {
  193. .bLength = USB_DT_ENDPOINT_SIZE,
  194. .bDescriptorType = USB_DT_ENDPOINT,
  195. .bEndpointAddress = USB_DIR_OUT,
  196. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  197. .wMaxPacketSize = cpu_to_le16(512),
  198. };
  199. static struct usb_descriptor_header *ecm_hs_function[] = {
  200. /* CDC ECM control descriptors */
  201. (struct usb_descriptor_header *) &ecm_control_intf,
  202. (struct usb_descriptor_header *) &ecm_header_desc,
  203. (struct usb_descriptor_header *) &ecm_union_desc,
  204. (struct usb_descriptor_header *) &ecm_desc,
  205. /* NOTE: status endpoint might need to be removed */
  206. (struct usb_descriptor_header *) &hs_ecm_notify_desc,
  207. /* data interface, altsettings 0 and 1 */
  208. (struct usb_descriptor_header *) &ecm_data_nop_intf,
  209. (struct usb_descriptor_header *) &ecm_data_intf,
  210. (struct usb_descriptor_header *) &hs_ecm_in_desc,
  211. (struct usb_descriptor_header *) &hs_ecm_out_desc,
  212. NULL,
  213. };
  214. /* super speed support: */
  215. static struct usb_endpoint_descriptor ss_ecm_notify_desc = {
  216. .bLength = USB_DT_ENDPOINT_SIZE,
  217. .bDescriptorType = USB_DT_ENDPOINT,
  218. .bEndpointAddress = USB_DIR_IN,
  219. .bmAttributes = USB_ENDPOINT_XFER_INT,
  220. .wMaxPacketSize = cpu_to_le16(ECM_STATUS_BYTECOUNT),
  221. .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
  222. };
  223. static struct usb_ss_ep_comp_descriptor ss_ecm_intr_comp_desc = {
  224. .bLength = sizeof ss_ecm_intr_comp_desc,
  225. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  226. /* the following 3 values can be tweaked if necessary */
  227. /* .bMaxBurst = 0, */
  228. /* .bmAttributes = 0, */
  229. .wBytesPerInterval = cpu_to_le16(ECM_STATUS_BYTECOUNT),
  230. };
  231. static struct usb_endpoint_descriptor ss_ecm_in_desc = {
  232. .bLength = USB_DT_ENDPOINT_SIZE,
  233. .bDescriptorType = USB_DT_ENDPOINT,
  234. .bEndpointAddress = USB_DIR_IN,
  235. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  236. .wMaxPacketSize = cpu_to_le16(1024),
  237. };
  238. static struct usb_endpoint_descriptor ss_ecm_out_desc = {
  239. .bLength = USB_DT_ENDPOINT_SIZE,
  240. .bDescriptorType = USB_DT_ENDPOINT,
  241. .bEndpointAddress = USB_DIR_OUT,
  242. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  243. .wMaxPacketSize = cpu_to_le16(1024),
  244. };
  245. static struct usb_ss_ep_comp_descriptor ss_ecm_bulk_comp_desc = {
  246. .bLength = sizeof ss_ecm_bulk_comp_desc,
  247. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  248. /* the following 2 values can be tweaked if necessary */
  249. /* .bMaxBurst = 0, */
  250. /* .bmAttributes = 0, */
  251. };
  252. static struct usb_descriptor_header *ecm_ss_function[] = {
  253. /* CDC ECM control descriptors */
  254. (struct usb_descriptor_header *) &ecm_control_intf,
  255. (struct usb_descriptor_header *) &ecm_header_desc,
  256. (struct usb_descriptor_header *) &ecm_union_desc,
  257. (struct usb_descriptor_header *) &ecm_desc,
  258. /* NOTE: status endpoint might need to be removed */
  259. (struct usb_descriptor_header *) &ss_ecm_notify_desc,
  260. (struct usb_descriptor_header *) &ss_ecm_intr_comp_desc,
  261. /* data interface, altsettings 0 and 1 */
  262. (struct usb_descriptor_header *) &ecm_data_nop_intf,
  263. (struct usb_descriptor_header *) &ecm_data_intf,
  264. (struct usb_descriptor_header *) &ss_ecm_in_desc,
  265. (struct usb_descriptor_header *) &ss_ecm_bulk_comp_desc,
  266. (struct usb_descriptor_header *) &ss_ecm_out_desc,
  267. (struct usb_descriptor_header *) &ss_ecm_bulk_comp_desc,
  268. NULL,
  269. };
  270. /* string descriptors: */
  271. static struct usb_string ecm_string_defs[] = {
  272. [0].s = "CDC Ethernet Control Model (ECM)",
  273. [1].s = NULL /* DYNAMIC */,
  274. [2].s = "CDC Ethernet Data",
  275. { } /* end of list */
  276. };
  277. static struct usb_gadget_strings ecm_string_table = {
  278. .language = 0x0409, /* en-us */
  279. .strings = ecm_string_defs,
  280. };
  281. static struct usb_gadget_strings *ecm_strings[] = {
  282. &ecm_string_table,
  283. NULL,
  284. };
  285. /*-------------------------------------------------------------------------*/
  286. static void ecm_do_notify(struct f_ecm *ecm)
  287. {
  288. struct usb_request *req = ecm->notify_req;
  289. struct usb_cdc_notification *event;
  290. struct usb_composite_dev *cdev = ecm->port.func.config->cdev;
  291. __le32 *data;
  292. int status;
  293. /* notification already in flight? */
  294. if (!req)
  295. return;
  296. event = req->buf;
  297. switch (ecm->notify_state) {
  298. case ECM_NOTIFY_NONE:
  299. return;
  300. case ECM_NOTIFY_CONNECT:
  301. event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
  302. if (ecm->is_open)
  303. event->wValue = cpu_to_le16(1);
  304. else
  305. event->wValue = cpu_to_le16(0);
  306. event->wLength = 0;
  307. req->length = sizeof *event;
  308. DBG(cdev, "notify connect %s\n",
  309. ecm->is_open ? "true" : "false");
  310. ecm->notify_state = ECM_NOTIFY_SPEED;
  311. break;
  312. case ECM_NOTIFY_SPEED:
  313. event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
  314. event->wValue = cpu_to_le16(0);
  315. event->wLength = cpu_to_le16(8);
  316. req->length = ECM_STATUS_BYTECOUNT;
  317. /* SPEED_CHANGE data is up/down speeds in bits/sec */
  318. data = req->buf + sizeof *event;
  319. data[0] = cpu_to_le32(ecm_bitrate(cdev->gadget));
  320. data[1] = data[0];
  321. DBG(cdev, "notify speed %d\n", ecm_bitrate(cdev->gadget));
  322. ecm->notify_state = ECM_NOTIFY_NONE;
  323. break;
  324. }
  325. event->bmRequestType = 0xA1;
  326. event->wIndex = cpu_to_le16(ecm->ctrl_id);
  327. ecm->notify_req = NULL;
  328. status = usb_ep_queue(ecm->notify, req, GFP_ATOMIC);
  329. if (status < 0) {
  330. ecm->notify_req = req;
  331. DBG(cdev, "notify --> %d\n", status);
  332. }
  333. }
  334. static void ecm_notify(struct f_ecm *ecm)
  335. {
  336. /* NOTE on most versions of Linux, host side cdc-ethernet
  337. * won't listen for notifications until its netdevice opens.
  338. * The first notification then sits in the FIFO for a long
  339. * time, and the second one is queued.
  340. */
  341. ecm->notify_state = ECM_NOTIFY_CONNECT;
  342. ecm_do_notify(ecm);
  343. }
  344. static void ecm_notify_complete(struct usb_ep *ep, struct usb_request *req)
  345. {
  346. struct f_ecm *ecm = req->context;
  347. struct usb_composite_dev *cdev = ecm->port.func.config->cdev;
  348. struct usb_cdc_notification *event = req->buf;
  349. switch (req->status) {
  350. case 0:
  351. /* no fault */
  352. break;
  353. case -ECONNRESET:
  354. case -ESHUTDOWN:
  355. ecm->notify_state = ECM_NOTIFY_NONE;
  356. break;
  357. default:
  358. DBG(cdev, "event %02x --> %d\n",
  359. event->bNotificationType, req->status);
  360. break;
  361. }
  362. ecm->notify_req = req;
  363. ecm_do_notify(ecm);
  364. }
  365. static int ecm_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
  366. {
  367. struct f_ecm *ecm = func_to_ecm(f);
  368. struct usb_composite_dev *cdev = f->config->cdev;
  369. struct usb_request *req = cdev->req;
  370. int value = -EOPNOTSUPP;
  371. u16 w_index = le16_to_cpu(ctrl->wIndex);
  372. u16 w_value = le16_to_cpu(ctrl->wValue);
  373. u16 w_length = le16_to_cpu(ctrl->wLength);
  374. /* composite driver infrastructure handles everything except
  375. * CDC class messages; interface activation uses set_alt().
  376. */
  377. switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
  378. case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
  379. | USB_CDC_SET_ETHERNET_PACKET_FILTER:
  380. /* see 6.2.30: no data, wIndex = interface,
  381. * wValue = packet filter bitmap
  382. */
  383. if (w_length != 0 || w_index != ecm->ctrl_id)
  384. goto invalid;
  385. DBG(cdev, "packet filter %02x\n", w_value);
  386. /* REVISIT locking of cdc_filter. This assumes the UDC
  387. * driver won't have a concurrent packet TX irq running on
  388. * another CPU; or that if it does, this write is atomic...
  389. */
  390. ecm->port.cdc_filter = w_value;
  391. value = 0;
  392. break;
  393. /* and optionally:
  394. * case USB_CDC_SEND_ENCAPSULATED_COMMAND:
  395. * case USB_CDC_GET_ENCAPSULATED_RESPONSE:
  396. * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
  397. * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
  398. * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
  399. * case USB_CDC_GET_ETHERNET_STATISTIC:
  400. */
  401. default:
  402. invalid:
  403. DBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
  404. ctrl->bRequestType, ctrl->bRequest,
  405. w_value, w_index, w_length);
  406. }
  407. /* respond with data transfer or status phase? */
  408. if (value >= 0) {
  409. DBG(cdev, "ecm req%02x.%02x v%04x i%04x l%d\n",
  410. ctrl->bRequestType, ctrl->bRequest,
  411. w_value, w_index, w_length);
  412. req->zero = 0;
  413. req->length = value;
  414. value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
  415. if (value < 0)
  416. ERROR(cdev, "ecm req %02x.%02x response err %d\n",
  417. ctrl->bRequestType, ctrl->bRequest,
  418. value);
  419. }
  420. /* device either stalls (value < 0) or reports success */
  421. return value;
  422. }
  423. static int ecm_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
  424. {
  425. struct f_ecm *ecm = func_to_ecm(f);
  426. struct usb_composite_dev *cdev = f->config->cdev;
  427. /* Control interface has only altsetting 0 */
  428. if (intf == ecm->ctrl_id) {
  429. if (alt != 0)
  430. goto fail;
  431. if (ecm->notify->driver_data) {
  432. VDBG(cdev, "reset ecm control %d\n", intf);
  433. usb_ep_disable(ecm->notify);
  434. }
  435. if (!(ecm->notify->desc)) {
  436. VDBG(cdev, "init ecm ctrl %d\n", intf);
  437. if (config_ep_by_speed(cdev->gadget, f, ecm->notify))
  438. goto fail;
  439. }
  440. usb_ep_enable(ecm->notify);
  441. ecm->notify->driver_data = ecm;
  442. /* Data interface has two altsettings, 0 and 1 */
  443. } else if (intf == ecm->data_id) {
  444. if (alt > 1)
  445. goto fail;
  446. if (ecm->port.in_ep->driver_data) {
  447. DBG(cdev, "reset ecm\n");
  448. gether_disconnect(&ecm->port);
  449. }
  450. if (!ecm->port.in_ep->desc ||
  451. !ecm->port.out_ep->desc) {
  452. DBG(cdev, "init ecm\n");
  453. if (config_ep_by_speed(cdev->gadget, f,
  454. ecm->port.in_ep) ||
  455. config_ep_by_speed(cdev->gadget, f,
  456. ecm->port.out_ep)) {
  457. ecm->port.in_ep->desc = NULL;
  458. ecm->port.out_ep->desc = NULL;
  459. goto fail;
  460. }
  461. }
  462. /* CDC Ethernet only sends data in non-default altsettings.
  463. * Changing altsettings resets filters, statistics, etc.
  464. */
  465. if (alt == 1) {
  466. struct net_device *net;
  467. /* Enable zlps by default for ECM conformance;
  468. * override for musb_hdrc (avoids txdma ovhead).
  469. */
  470. ecm->port.is_zlp_ok = !(gadget_is_musbhdrc(cdev->gadget)
  471. );
  472. ecm->port.cdc_filter = DEFAULT_FILTER;
  473. DBG(cdev, "activate ecm\n");
  474. net = gether_connect(&ecm->port);
  475. if (IS_ERR(net))
  476. return PTR_ERR(net);
  477. }
  478. /* NOTE this can be a minor disagreement with the ECM spec,
  479. * which says speed notifications will "always" follow
  480. * connection notifications. But we allow one connect to
  481. * follow another (if the first is in flight), and instead
  482. * just guarantee that a speed notification is always sent.
  483. */
  484. ecm_notify(ecm);
  485. } else
  486. goto fail;
  487. return 0;
  488. fail:
  489. return -EINVAL;
  490. }
  491. /* Because the data interface supports multiple altsettings,
  492. * this ECM function *MUST* implement a get_alt() method.
  493. */
  494. static int ecm_get_alt(struct usb_function *f, unsigned intf)
  495. {
  496. struct f_ecm *ecm = func_to_ecm(f);
  497. if (intf == ecm->ctrl_id)
  498. return 0;
  499. return ecm->port.in_ep->driver_data ? 1 : 0;
  500. }
  501. static void ecm_disable(struct usb_function *f)
  502. {
  503. struct f_ecm *ecm = func_to_ecm(f);
  504. struct usb_composite_dev *cdev = f->config->cdev;
  505. DBG(cdev, "ecm deactivated\n");
  506. if (ecm->port.in_ep->driver_data)
  507. gether_disconnect(&ecm->port);
  508. if (ecm->notify->driver_data) {
  509. usb_ep_disable(ecm->notify);
  510. ecm->notify->driver_data = NULL;
  511. ecm->notify->desc = NULL;
  512. }
  513. }
  514. /*-------------------------------------------------------------------------*/
  515. /*
  516. * Callbacks let us notify the host about connect/disconnect when the
  517. * net device is opened or closed.
  518. *
  519. * For testing, note that link states on this side include both opened
  520. * and closed variants of:
  521. *
  522. * - disconnected/unconfigured
  523. * - configured but inactive (data alt 0)
  524. * - configured and active (data alt 1)
  525. *
  526. * Each needs to be tested with unplug, rmmod, SET_CONFIGURATION, and
  527. * SET_INTERFACE (altsetting). Remember also that "configured" doesn't
  528. * imply the host is actually polling the notification endpoint, and
  529. * likewise that "active" doesn't imply it's actually using the data
  530. * endpoints for traffic.
  531. */
  532. static void ecm_open(struct gether *geth)
  533. {
  534. struct f_ecm *ecm = func_to_ecm(&geth->func);
  535. DBG(ecm->port.func.config->cdev, "%s\n", __func__);
  536. ecm->is_open = true;
  537. ecm_notify(ecm);
  538. }
  539. static void ecm_close(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 = false;
  544. ecm_notify(ecm);
  545. }
  546. /*-------------------------------------------------------------------------*/
  547. /* ethernet function driver setup/binding */
  548. static int
  549. ecm_bind(struct usb_configuration *c, struct usb_function *f)
  550. {
  551. struct usb_composite_dev *cdev = c->cdev;
  552. struct f_ecm *ecm = func_to_ecm(f);
  553. int status;
  554. struct usb_ep *ep;
  555. /* allocate instance-specific interface IDs */
  556. status = usb_interface_id(c, f);
  557. if (status < 0)
  558. goto fail;
  559. ecm->ctrl_id = status;
  560. ecm_control_intf.bInterfaceNumber = status;
  561. ecm_union_desc.bMasterInterface0 = status;
  562. status = usb_interface_id(c, f);
  563. if (status < 0)
  564. goto fail;
  565. ecm->data_id = status;
  566. ecm_data_nop_intf.bInterfaceNumber = status;
  567. ecm_data_intf.bInterfaceNumber = status;
  568. ecm_union_desc.bSlaveInterface0 = status;
  569. status = -ENODEV;
  570. /* allocate instance-specific endpoints */
  571. ep = usb_ep_autoconfig(cdev->gadget, &fs_ecm_in_desc);
  572. if (!ep)
  573. goto fail;
  574. ecm->port.in_ep = ep;
  575. ep->driver_data = cdev; /* claim */
  576. ep = usb_ep_autoconfig(cdev->gadget, &fs_ecm_out_desc);
  577. if (!ep)
  578. goto fail;
  579. ecm->port.out_ep = ep;
  580. ep->driver_data = cdev; /* claim */
  581. /* NOTE: a status/notification endpoint is *OPTIONAL* but we
  582. * don't treat it that way. It's simpler, and some newer CDC
  583. * profiles (wireless handsets) no longer treat it as optional.
  584. */
  585. ep = usb_ep_autoconfig(cdev->gadget, &fs_ecm_notify_desc);
  586. if (!ep)
  587. goto fail;
  588. ecm->notify = ep;
  589. ep->driver_data = cdev; /* claim */
  590. status = -ENOMEM;
  591. /* allocate notification request and buffer */
  592. ecm->notify_req = usb_ep_alloc_request(ep, GFP_KERNEL);
  593. if (!ecm->notify_req)
  594. goto fail;
  595. ecm->notify_req->buf = kmalloc(ECM_STATUS_BYTECOUNT, GFP_KERNEL);
  596. if (!ecm->notify_req->buf)
  597. goto fail;
  598. ecm->notify_req->context = ecm;
  599. ecm->notify_req->complete = ecm_notify_complete;
  600. /* copy descriptors, and track endpoint copies */
  601. f->descriptors = usb_copy_descriptors(ecm_fs_function);
  602. if (!f->descriptors)
  603. goto fail;
  604. /* support all relevant hardware speeds... we expect that when
  605. * hardware is dual speed, all bulk-capable endpoints work at
  606. * both speeds
  607. */
  608. if (gadget_is_dualspeed(c->cdev->gadget)) {
  609. hs_ecm_in_desc.bEndpointAddress =
  610. fs_ecm_in_desc.bEndpointAddress;
  611. hs_ecm_out_desc.bEndpointAddress =
  612. fs_ecm_out_desc.bEndpointAddress;
  613. hs_ecm_notify_desc.bEndpointAddress =
  614. fs_ecm_notify_desc.bEndpointAddress;
  615. /* copy descriptors, and track endpoint copies */
  616. f->hs_descriptors = usb_copy_descriptors(ecm_hs_function);
  617. if (!f->hs_descriptors)
  618. goto fail;
  619. }
  620. if (gadget_is_superspeed(c->cdev->gadget)) {
  621. ss_ecm_in_desc.bEndpointAddress =
  622. fs_ecm_in_desc.bEndpointAddress;
  623. ss_ecm_out_desc.bEndpointAddress =
  624. fs_ecm_out_desc.bEndpointAddress;
  625. ss_ecm_notify_desc.bEndpointAddress =
  626. fs_ecm_notify_desc.bEndpointAddress;
  627. /* copy descriptors, and track endpoint copies */
  628. f->ss_descriptors = usb_copy_descriptors(ecm_ss_function);
  629. if (!f->ss_descriptors)
  630. goto fail;
  631. }
  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 (f->descriptors)
  646. usb_free_descriptors(f->descriptors);
  647. if (f->hs_descriptors)
  648. usb_free_descriptors(f->hs_descriptors);
  649. if (ecm->notify_req) {
  650. kfree(ecm->notify_req->buf);
  651. usb_ep_free_request(ecm->notify, ecm->notify_req);
  652. }
  653. /* we might as well release our claims on endpoints */
  654. if (ecm->notify)
  655. ecm->notify->driver_data = NULL;
  656. if (ecm->port.out_ep->desc)
  657. ecm->port.out_ep->driver_data = NULL;
  658. if (ecm->port.in_ep->desc)
  659. ecm->port.in_ep->driver_data = NULL;
  660. ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
  661. return status;
  662. }
  663. static void
  664. ecm_unbind(struct usb_configuration *c, struct usb_function *f)
  665. {
  666. struct f_ecm *ecm = func_to_ecm(f);
  667. DBG(c->cdev, "ecm unbind\n");
  668. if (gadget_is_superspeed(c->cdev->gadget))
  669. usb_free_descriptors(f->ss_descriptors);
  670. if (gadget_is_dualspeed(c->cdev->gadget))
  671. usb_free_descriptors(f->hs_descriptors);
  672. usb_free_descriptors(f->descriptors);
  673. kfree(ecm->notify_req->buf);
  674. usb_ep_free_request(ecm->notify, ecm->notify_req);
  675. ecm_string_defs[1].s = NULL;
  676. kfree(ecm);
  677. }
  678. /**
  679. * ecm_bind_config - add CDC Ethernet network link to a configuration
  680. * @c: the configuration to support the network link
  681. * @ethaddr: a buffer in which the ethernet address of the host side
  682. * side of the link was recorded
  683. * Context: single threaded during gadget setup
  684. *
  685. * Returns zero on success, else negative errno.
  686. *
  687. * Caller must have called @gether_setup(). Caller is also responsible
  688. * for calling @gether_cleanup() before module unload.
  689. */
  690. int
  691. ecm_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN])
  692. {
  693. struct f_ecm *ecm;
  694. int status;
  695. if (!can_support_ecm(c->cdev->gadget) || !ethaddr)
  696. return -EINVAL;
  697. /* maybe allocate device-global string IDs */
  698. if (ecm_string_defs[0].id == 0) {
  699. /* control interface label */
  700. status = usb_string_id(c->cdev);
  701. if (status < 0)
  702. return status;
  703. ecm_string_defs[0].id = status;
  704. ecm_control_intf.iInterface = status;
  705. /* data interface label */
  706. status = usb_string_id(c->cdev);
  707. if (status < 0)
  708. return status;
  709. ecm_string_defs[2].id = status;
  710. ecm_data_intf.iInterface = status;
  711. /* MAC address */
  712. status = usb_string_id(c->cdev);
  713. if (status < 0)
  714. return status;
  715. ecm_string_defs[1].id = status;
  716. ecm_desc.iMACAddress = status;
  717. }
  718. /* allocate and initialize one new instance */
  719. ecm = kzalloc(sizeof *ecm, GFP_KERNEL);
  720. if (!ecm)
  721. return -ENOMEM;
  722. /* export host's Ethernet address in CDC format */
  723. snprintf(ecm->ethaddr, sizeof ecm->ethaddr,
  724. "%02X%02X%02X%02X%02X%02X",
  725. ethaddr[0], ethaddr[1], ethaddr[2],
  726. ethaddr[3], ethaddr[4], ethaddr[5]);
  727. ecm_string_defs[1].s = ecm->ethaddr;
  728. ecm->port.cdc_filter = DEFAULT_FILTER;
  729. ecm->port.func.name = "cdc_ethernet";
  730. ecm->port.func.strings = ecm_strings;
  731. /* descriptors are per-instance copies */
  732. ecm->port.func.bind = ecm_bind;
  733. ecm->port.func.unbind = ecm_unbind;
  734. ecm->port.func.set_alt = ecm_set_alt;
  735. ecm->port.func.get_alt = ecm_get_alt;
  736. ecm->port.func.setup = ecm_setup;
  737. ecm->port.func.disable = ecm_disable;
  738. status = usb_add_function(c, &ecm->port.func);
  739. if (status) {
  740. ecm_string_defs[1].s = NULL;
  741. kfree(ecm);
  742. }
  743. return status;
  744. }