f_ecm.c 26 KB

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