f_ecm.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902
  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_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 = 1 << LOG2_STATUS_INTERVAL_MSEC,
  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 = LOG2_STATUS_INTERVAL_MSEC + 4,
  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 = LOG2_STATUS_INTERVAL_MSEC + 4,
  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_control_intf,
  268. (struct usb_descriptor_header *) &ecm_header_desc,
  269. (struct usb_descriptor_header *) &ecm_union_desc,
  270. (struct usb_descriptor_header *) &ecm_desc,
  271. /* NOTE: status endpoint might need to be removed */
  272. (struct usb_descriptor_header *) &ss_ecm_notify_desc,
  273. (struct usb_descriptor_header *) &ss_ecm_intr_comp_desc,
  274. /* data interface, altsettings 0 and 1 */
  275. (struct usb_descriptor_header *) &ecm_data_nop_intf,
  276. (struct usb_descriptor_header *) &ecm_data_intf,
  277. (struct usb_descriptor_header *) &ss_ecm_in_desc,
  278. (struct usb_descriptor_header *) &ss_ecm_bulk_comp_desc,
  279. (struct usb_descriptor_header *) &ss_ecm_out_desc,
  280. (struct usb_descriptor_header *) &ss_ecm_bulk_comp_desc,
  281. NULL,
  282. };
  283. /* string descriptors: */
  284. static struct usb_string ecm_string_defs[] = {
  285. [0].s = "CDC Ethernet Control Model (ECM)",
  286. [1].s = NULL /* DYNAMIC */,
  287. [2].s = "CDC Ethernet Data",
  288. [3].s = "CDC ECM",
  289. { } /* end of list */
  290. };
  291. static struct usb_gadget_strings ecm_string_table = {
  292. .language = 0x0409, /* en-us */
  293. .strings = ecm_string_defs,
  294. };
  295. static struct usb_gadget_strings *ecm_strings[] = {
  296. &ecm_string_table,
  297. NULL,
  298. };
  299. /*-------------------------------------------------------------------------*/
  300. static void ecm_do_notify(struct f_ecm *ecm)
  301. {
  302. struct usb_request *req = ecm->notify_req;
  303. struct usb_cdc_notification *event;
  304. struct usb_composite_dev *cdev = ecm->port.func.config->cdev;
  305. __le32 *data;
  306. int status;
  307. /* notification already in flight? */
  308. if (!req)
  309. return;
  310. event = req->buf;
  311. switch (ecm->notify_state) {
  312. case ECM_NOTIFY_NONE:
  313. return;
  314. case ECM_NOTIFY_CONNECT:
  315. event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
  316. if (ecm->is_open)
  317. event->wValue = cpu_to_le16(1);
  318. else
  319. event->wValue = cpu_to_le16(0);
  320. event->wLength = 0;
  321. req->length = sizeof *event;
  322. DBG(cdev, "notify connect %s\n",
  323. ecm->is_open ? "true" : "false");
  324. ecm->notify_state = ECM_NOTIFY_SPEED;
  325. break;
  326. case ECM_NOTIFY_SPEED:
  327. event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
  328. event->wValue = cpu_to_le16(0);
  329. event->wLength = cpu_to_le16(8);
  330. req->length = ECM_STATUS_BYTECOUNT;
  331. /* SPEED_CHANGE data is up/down speeds in bits/sec */
  332. data = req->buf + sizeof *event;
  333. data[0] = cpu_to_le32(ecm_bitrate(cdev->gadget));
  334. data[1] = data[0];
  335. DBG(cdev, "notify speed %d\n", ecm_bitrate(cdev->gadget));
  336. ecm->notify_state = ECM_NOTIFY_NONE;
  337. break;
  338. }
  339. event->bmRequestType = 0xA1;
  340. event->wIndex = cpu_to_le16(ecm->ctrl_id);
  341. ecm->notify_req = NULL;
  342. status = usb_ep_queue(ecm->notify, req, GFP_ATOMIC);
  343. if (status < 0) {
  344. ecm->notify_req = req;
  345. DBG(cdev, "notify --> %d\n", status);
  346. }
  347. }
  348. static void ecm_notify(struct f_ecm *ecm)
  349. {
  350. /* NOTE on most versions of Linux, host side cdc-ethernet
  351. * won't listen for notifications until its netdevice opens.
  352. * The first notification then sits in the FIFO for a long
  353. * time, and the second one is queued.
  354. */
  355. ecm->notify_state = ECM_NOTIFY_CONNECT;
  356. ecm_do_notify(ecm);
  357. }
  358. static void ecm_notify_complete(struct usb_ep *ep, struct usb_request *req)
  359. {
  360. struct f_ecm *ecm = req->context;
  361. struct usb_composite_dev *cdev = ecm->port.func.config->cdev;
  362. struct usb_cdc_notification *event = req->buf;
  363. switch (req->status) {
  364. case 0:
  365. /* no fault */
  366. break;
  367. case -ECONNRESET:
  368. case -ESHUTDOWN:
  369. ecm->notify_state = ECM_NOTIFY_NONE;
  370. break;
  371. default:
  372. DBG(cdev, "event %02x --> %d\n",
  373. event->bNotificationType, req->status);
  374. break;
  375. }
  376. ecm->notify_req = req;
  377. ecm_do_notify(ecm);
  378. }
  379. static int ecm_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
  380. {
  381. struct f_ecm *ecm = func_to_ecm(f);
  382. struct usb_composite_dev *cdev = f->config->cdev;
  383. struct usb_request *req = cdev->req;
  384. int value = -EOPNOTSUPP;
  385. u16 w_index = le16_to_cpu(ctrl->wIndex);
  386. u16 w_value = le16_to_cpu(ctrl->wValue);
  387. u16 w_length = le16_to_cpu(ctrl->wLength);
  388. /* composite driver infrastructure handles everything except
  389. * CDC class messages; interface activation uses set_alt().
  390. */
  391. switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
  392. case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
  393. | USB_CDC_SET_ETHERNET_PACKET_FILTER:
  394. /* see 6.2.30: no data, wIndex = interface,
  395. * wValue = packet filter bitmap
  396. */
  397. if (w_length != 0 || w_index != ecm->ctrl_id)
  398. goto invalid;
  399. DBG(cdev, "packet filter %02x\n", w_value);
  400. /* REVISIT locking of cdc_filter. This assumes the UDC
  401. * driver won't have a concurrent packet TX irq running on
  402. * another CPU; or that if it does, this write is atomic...
  403. */
  404. ecm->port.cdc_filter = w_value;
  405. value = 0;
  406. break;
  407. /* and optionally:
  408. * case USB_CDC_SEND_ENCAPSULATED_COMMAND:
  409. * case USB_CDC_GET_ENCAPSULATED_RESPONSE:
  410. * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
  411. * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
  412. * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
  413. * case USB_CDC_GET_ETHERNET_STATISTIC:
  414. */
  415. default:
  416. invalid:
  417. DBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
  418. ctrl->bRequestType, ctrl->bRequest,
  419. w_value, w_index, w_length);
  420. }
  421. /* respond with data transfer or status phase? */
  422. if (value >= 0) {
  423. DBG(cdev, "ecm req%02x.%02x v%04x i%04x l%d\n",
  424. ctrl->bRequestType, ctrl->bRequest,
  425. w_value, w_index, w_length);
  426. req->zero = 0;
  427. req->length = value;
  428. value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
  429. if (value < 0)
  430. ERROR(cdev, "ecm req %02x.%02x response err %d\n",
  431. ctrl->bRequestType, ctrl->bRequest,
  432. value);
  433. }
  434. /* device either stalls (value < 0) or reports success */
  435. return value;
  436. }
  437. static int ecm_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
  438. {
  439. struct f_ecm *ecm = func_to_ecm(f);
  440. struct usb_composite_dev *cdev = f->config->cdev;
  441. /* Control interface has only altsetting 0 */
  442. if (intf == ecm->ctrl_id) {
  443. if (alt != 0)
  444. goto fail;
  445. if (ecm->notify->driver_data) {
  446. VDBG(cdev, "reset ecm control %d\n", intf);
  447. usb_ep_disable(ecm->notify);
  448. }
  449. if (!(ecm->notify->desc)) {
  450. VDBG(cdev, "init ecm ctrl %d\n", intf);
  451. if (config_ep_by_speed(cdev->gadget, f, ecm->notify))
  452. goto fail;
  453. }
  454. usb_ep_enable(ecm->notify);
  455. ecm->notify->driver_data = ecm;
  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->driver_data) {
  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 = !(gadget_is_musbhdrc(cdev->gadget)
  485. );
  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->driver_data ? 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->driver_data) {
  521. gether_disconnect(&ecm->port);
  522. } else {
  523. ecm->port.in_ep->desc = NULL;
  524. ecm->port.out_ep->desc = NULL;
  525. }
  526. if (ecm->notify->driver_data) {
  527. usb_ep_disable(ecm->notify);
  528. ecm->notify->driver_data = NULL;
  529. ecm->notify->desc = NULL;
  530. }
  531. }
  532. /*-------------------------------------------------------------------------*/
  533. /*
  534. * Callbacks let us notify the host about connect/disconnect when the
  535. * net device is opened or closed.
  536. *
  537. * For testing, note that link states on this side include both opened
  538. * and closed variants of:
  539. *
  540. * - disconnected/unconfigured
  541. * - configured but inactive (data alt 0)
  542. * - configured and active (data alt 1)
  543. *
  544. * Each needs to be tested with unplug, rmmod, SET_CONFIGURATION, and
  545. * SET_INTERFACE (altsetting). Remember also that "configured" doesn't
  546. * imply the host is actually polling the notification endpoint, and
  547. * likewise that "active" doesn't imply it's actually using the data
  548. * endpoints for traffic.
  549. */
  550. static void ecm_open(struct gether *geth)
  551. {
  552. struct f_ecm *ecm = func_to_ecm(&geth->func);
  553. DBG(ecm->port.func.config->cdev, "%s\n", __func__);
  554. ecm->is_open = true;
  555. ecm_notify(ecm);
  556. }
  557. static void ecm_close(struct gether *geth)
  558. {
  559. struct f_ecm *ecm = func_to_ecm(&geth->func);
  560. DBG(ecm->port.func.config->cdev, "%s\n", __func__);
  561. ecm->is_open = false;
  562. ecm_notify(ecm);
  563. }
  564. /*-------------------------------------------------------------------------*/
  565. /* ethernet function driver setup/binding */
  566. static int
  567. ecm_bind(struct usb_configuration *c, struct usb_function *f)
  568. {
  569. struct usb_composite_dev *cdev = c->cdev;
  570. struct f_ecm *ecm = func_to_ecm(f);
  571. int status;
  572. struct usb_ep *ep;
  573. /* allocate instance-specific interface IDs */
  574. status = usb_interface_id(c, f);
  575. if (status < 0)
  576. goto fail;
  577. ecm->ctrl_id = status;
  578. ecm_iad_descriptor.bFirstInterface = status;
  579. ecm_control_intf.bInterfaceNumber = status;
  580. ecm_union_desc.bMasterInterface0 = status;
  581. status = usb_interface_id(c, f);
  582. if (status < 0)
  583. goto fail;
  584. ecm->data_id = status;
  585. ecm_data_nop_intf.bInterfaceNumber = status;
  586. ecm_data_intf.bInterfaceNumber = status;
  587. ecm_union_desc.bSlaveInterface0 = status;
  588. status = -ENODEV;
  589. /* allocate instance-specific endpoints */
  590. ep = usb_ep_autoconfig(cdev->gadget, &fs_ecm_in_desc);
  591. if (!ep)
  592. goto fail;
  593. ecm->port.in_ep = ep;
  594. ep->driver_data = cdev; /* claim */
  595. ep = usb_ep_autoconfig(cdev->gadget, &fs_ecm_out_desc);
  596. if (!ep)
  597. goto fail;
  598. ecm->port.out_ep = ep;
  599. ep->driver_data = cdev; /* claim */
  600. /* NOTE: a status/notification endpoint is *OPTIONAL* but we
  601. * don't treat it that way. It's simpler, and some newer CDC
  602. * profiles (wireless handsets) no longer treat it as optional.
  603. */
  604. ep = usb_ep_autoconfig(cdev->gadget, &fs_ecm_notify_desc);
  605. if (!ep)
  606. goto fail;
  607. ecm->notify = ep;
  608. ep->driver_data = cdev; /* claim */
  609. status = -ENOMEM;
  610. /* allocate notification request and buffer */
  611. ecm->notify_req = usb_ep_alloc_request(ep, GFP_KERNEL);
  612. if (!ecm->notify_req)
  613. goto fail;
  614. ecm->notify_req->buf = kmalloc(ECM_STATUS_BYTECOUNT, GFP_KERNEL);
  615. if (!ecm->notify_req->buf)
  616. goto fail;
  617. ecm->notify_req->context = ecm;
  618. ecm->notify_req->complete = ecm_notify_complete;
  619. /* support all relevant hardware speeds... we expect that when
  620. * hardware is dual speed, all bulk-capable endpoints work at
  621. * both speeds
  622. */
  623. hs_ecm_in_desc.bEndpointAddress = fs_ecm_in_desc.bEndpointAddress;
  624. hs_ecm_out_desc.bEndpointAddress = fs_ecm_out_desc.bEndpointAddress;
  625. hs_ecm_notify_desc.bEndpointAddress =
  626. fs_ecm_notify_desc.bEndpointAddress;
  627. ss_ecm_in_desc.bEndpointAddress = fs_ecm_in_desc.bEndpointAddress;
  628. ss_ecm_out_desc.bEndpointAddress = fs_ecm_out_desc.bEndpointAddress;
  629. ss_ecm_notify_desc.bEndpointAddress =
  630. fs_ecm_notify_desc.bEndpointAddress;
  631. status = usb_assign_descriptors(f, ecm_fs_function, ecm_hs_function,
  632. ecm_ss_function);
  633. if (status)
  634. goto fail;
  635. /* NOTE: all that is done without knowing or caring about
  636. * the network link ... which is unavailable to this code
  637. * until we're activated via set_alt().
  638. */
  639. ecm->port.open = ecm_open;
  640. ecm->port.close = ecm_close;
  641. DBG(cdev, "CDC Ethernet: %s speed IN/%s OUT/%s NOTIFY/%s\n",
  642. gadget_is_superspeed(c->cdev->gadget) ? "super" :
  643. gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
  644. ecm->port.in_ep->name, ecm->port.out_ep->name,
  645. ecm->notify->name);
  646. return 0;
  647. fail:
  648. if (ecm->notify_req) {
  649. kfree(ecm->notify_req->buf);
  650. usb_ep_free_request(ecm->notify, ecm->notify_req);
  651. }
  652. /* we might as well release our claims on endpoints */
  653. if (ecm->notify)
  654. ecm->notify->driver_data = NULL;
  655. if (ecm->port.out_ep)
  656. ecm->port.out_ep->driver_data = NULL;
  657. if (ecm->port.in_ep)
  658. ecm->port.in_ep->driver_data = NULL;
  659. ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
  660. return status;
  661. }
  662. static void
  663. ecm_unbind(struct usb_configuration *c, struct usb_function *f)
  664. {
  665. struct f_ecm *ecm = func_to_ecm(f);
  666. DBG(c->cdev, "ecm unbind\n");
  667. usb_free_all_descriptors(f);
  668. kfree(ecm->notify_req->buf);
  669. usb_ep_free_request(ecm->notify, ecm->notify_req);
  670. ecm_string_defs[1].s = NULL;
  671. kfree(ecm);
  672. }
  673. /**
  674. * ecm_bind_config - add CDC Ethernet network link to a configuration
  675. * @c: the configuration to support the network link
  676. * @ethaddr: a buffer in which the ethernet address of the host side
  677. * side of the link was recorded
  678. * Context: single threaded during gadget setup
  679. *
  680. * Returns zero on success, else negative errno.
  681. *
  682. * Caller must have called @gether_setup(). Caller is also responsible
  683. * for calling @gether_cleanup() before module unload.
  684. */
  685. int
  686. ecm_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN])
  687. {
  688. struct f_ecm *ecm;
  689. int status;
  690. if (!can_support_ecm(c->cdev->gadget) || !ethaddr)
  691. return -EINVAL;
  692. /* maybe allocate device-global string IDs */
  693. if (ecm_string_defs[0].id == 0) {
  694. /* control interface label */
  695. status = usb_string_id(c->cdev);
  696. if (status < 0)
  697. return status;
  698. ecm_string_defs[0].id = status;
  699. ecm_control_intf.iInterface = status;
  700. /* data interface label */
  701. status = usb_string_id(c->cdev);
  702. if (status < 0)
  703. return status;
  704. ecm_string_defs[2].id = status;
  705. ecm_data_intf.iInterface = status;
  706. /* MAC address */
  707. status = usb_string_id(c->cdev);
  708. if (status < 0)
  709. return status;
  710. ecm_string_defs[1].id = status;
  711. ecm_desc.iMACAddress = status;
  712. /* IAD label */
  713. status = usb_string_id(c->cdev);
  714. if (status < 0)
  715. return status;
  716. ecm_string_defs[3].id = status;
  717. ecm_iad_descriptor.iFunction = status;
  718. }
  719. /* allocate and initialize one new instance */
  720. ecm = kzalloc(sizeof *ecm, GFP_KERNEL);
  721. if (!ecm)
  722. return -ENOMEM;
  723. /* export host's Ethernet address in CDC format */
  724. snprintf(ecm->ethaddr, sizeof ecm->ethaddr,
  725. "%02X%02X%02X%02X%02X%02X",
  726. ethaddr[0], ethaddr[1], ethaddr[2],
  727. ethaddr[3], ethaddr[4], ethaddr[5]);
  728. ecm_string_defs[1].s = ecm->ethaddr;
  729. ecm->port.cdc_filter = DEFAULT_FILTER;
  730. ecm->port.func.name = "cdc_ethernet";
  731. ecm->port.func.strings = ecm_strings;
  732. /* descriptors are per-instance copies */
  733. ecm->port.func.bind = ecm_bind;
  734. ecm->port.func.unbind = ecm_unbind;
  735. ecm->port.func.set_alt = ecm_set_alt;
  736. ecm->port.func.get_alt = ecm_get_alt;
  737. ecm->port.func.setup = ecm_setup;
  738. ecm->port.func.disable = ecm_disable;
  739. status = usb_add_function(c, &ecm->port.func);
  740. if (status) {
  741. ecm_string_defs[1].s = NULL;
  742. kfree(ecm);
  743. }
  744. return status;
  745. }