f_ecm.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041
  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. if (ecm->notify->driver_data) {
  450. VDBG(cdev, "reset ecm control %d\n", intf);
  451. usb_ep_disable(ecm->notify);
  452. }
  453. if (!(ecm->notify->desc)) {
  454. VDBG(cdev, "init ecm ctrl %d\n", intf);
  455. if (config_ep_by_speed(cdev->gadget, f, ecm->notify))
  456. goto fail;
  457. }
  458. usb_ep_enable(ecm->notify);
  459. ecm->notify->driver_data = ecm;
  460. /* Data interface has two altsettings, 0 and 1 */
  461. } else if (intf == ecm->data_id) {
  462. if (alt > 1)
  463. goto fail;
  464. if (ecm->port.in_ep->driver_data) {
  465. DBG(cdev, "reset ecm\n");
  466. gether_disconnect(&ecm->port);
  467. }
  468. if (!ecm->port.in_ep->desc ||
  469. !ecm->port.out_ep->desc) {
  470. DBG(cdev, "init ecm\n");
  471. if (config_ep_by_speed(cdev->gadget, f,
  472. ecm->port.in_ep) ||
  473. config_ep_by_speed(cdev->gadget, f,
  474. ecm->port.out_ep)) {
  475. ecm->port.in_ep->desc = NULL;
  476. ecm->port.out_ep->desc = NULL;
  477. goto fail;
  478. }
  479. }
  480. /* CDC Ethernet only sends data in non-default altsettings.
  481. * Changing altsettings resets filters, statistics, etc.
  482. */
  483. if (alt == 1) {
  484. struct net_device *net;
  485. /* Enable zlps by default for ECM conformance;
  486. * override for musb_hdrc (avoids txdma ovhead).
  487. */
  488. ecm->port.is_zlp_ok = !(gadget_is_musbhdrc(cdev->gadget)
  489. );
  490. ecm->port.cdc_filter = DEFAULT_FILTER;
  491. DBG(cdev, "activate ecm\n");
  492. net = gether_connect(&ecm->port);
  493. if (IS_ERR(net))
  494. return PTR_ERR(net);
  495. }
  496. /* NOTE this can be a minor disagreement with the ECM spec,
  497. * which says speed notifications will "always" follow
  498. * connection notifications. But we allow one connect to
  499. * follow another (if the first is in flight), and instead
  500. * just guarantee that a speed notification is always sent.
  501. */
  502. ecm_notify(ecm);
  503. } else
  504. goto fail;
  505. return 0;
  506. fail:
  507. return -EINVAL;
  508. }
  509. /* Because the data interface supports multiple altsettings,
  510. * this ECM function *MUST* implement a get_alt() method.
  511. */
  512. static int ecm_get_alt(struct usb_function *f, unsigned intf)
  513. {
  514. struct f_ecm *ecm = func_to_ecm(f);
  515. if (intf == ecm->ctrl_id)
  516. return 0;
  517. return ecm->port.in_ep->driver_data ? 1 : 0;
  518. }
  519. static void ecm_disable(struct usb_function *f)
  520. {
  521. struct f_ecm *ecm = func_to_ecm(f);
  522. struct usb_composite_dev *cdev = f->config->cdev;
  523. DBG(cdev, "ecm deactivated\n");
  524. if (ecm->port.in_ep->driver_data)
  525. gether_disconnect(&ecm->port);
  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. struct usb_string *us;
  572. int status;
  573. struct usb_ep *ep;
  574. #ifndef USBF_ECM_INCLUDED
  575. struct f_ecm_opts *ecm_opts;
  576. if (!can_support_ecm(cdev->gadget))
  577. return -EINVAL;
  578. ecm_opts = container_of(f->fi, struct f_ecm_opts, func_inst);
  579. /*
  580. * in drivers/usb/gadget/configfs.c:configfs_composite_bind()
  581. * configurations are bound in sequence with list_for_each_entry,
  582. * in each configuration its functions are bound in sequence
  583. * with list_for_each_entry, so we assume no race condition
  584. * with regard to ecm_opts->bound access
  585. */
  586. if (!ecm_opts->bound) {
  587. mutex_lock(&ecm_opts->lock);
  588. gether_set_gadget(ecm_opts->net, cdev->gadget);
  589. status = gether_register_netdev(ecm_opts->net);
  590. mutex_unlock(&ecm_opts->lock);
  591. if (status)
  592. return status;
  593. ecm_opts->bound = true;
  594. }
  595. #endif
  596. us = usb_gstrings_attach(cdev, ecm_strings,
  597. ARRAY_SIZE(ecm_string_defs));
  598. if (IS_ERR(us))
  599. return PTR_ERR(us);
  600. ecm_control_intf.iInterface = us[0].id;
  601. ecm_data_intf.iInterface = us[2].id;
  602. ecm_desc.iMACAddress = us[1].id;
  603. ecm_iad_descriptor.iFunction = us[3].id;
  604. /* allocate instance-specific interface IDs */
  605. status = usb_interface_id(c, f);
  606. if (status < 0)
  607. goto fail;
  608. ecm->ctrl_id = status;
  609. ecm_iad_descriptor.bFirstInterface = status;
  610. ecm_control_intf.bInterfaceNumber = status;
  611. ecm_union_desc.bMasterInterface0 = status;
  612. status = usb_interface_id(c, f);
  613. if (status < 0)
  614. goto fail;
  615. ecm->data_id = status;
  616. ecm_data_nop_intf.bInterfaceNumber = status;
  617. ecm_data_intf.bInterfaceNumber = status;
  618. ecm_union_desc.bSlaveInterface0 = status;
  619. status = -ENODEV;
  620. /* allocate instance-specific endpoints */
  621. ep = usb_ep_autoconfig(cdev->gadget, &fs_ecm_in_desc);
  622. if (!ep)
  623. goto fail;
  624. ecm->port.in_ep = ep;
  625. ep->driver_data = cdev; /* claim */
  626. ep = usb_ep_autoconfig(cdev->gadget, &fs_ecm_out_desc);
  627. if (!ep)
  628. goto fail;
  629. ecm->port.out_ep = ep;
  630. ep->driver_data = cdev; /* claim */
  631. /* NOTE: a status/notification endpoint is *OPTIONAL* but we
  632. * don't treat it that way. It's simpler, and some newer CDC
  633. * profiles (wireless handsets) no longer treat it as optional.
  634. */
  635. ep = usb_ep_autoconfig(cdev->gadget, &fs_ecm_notify_desc);
  636. if (!ep)
  637. goto fail;
  638. ecm->notify = ep;
  639. ep->driver_data = cdev; /* claim */
  640. status = -ENOMEM;
  641. /* allocate notification request and buffer */
  642. ecm->notify_req = usb_ep_alloc_request(ep, GFP_KERNEL);
  643. if (!ecm->notify_req)
  644. goto fail;
  645. ecm->notify_req->buf = kmalloc(ECM_STATUS_BYTECOUNT, GFP_KERNEL);
  646. if (!ecm->notify_req->buf)
  647. goto fail;
  648. ecm->notify_req->context = ecm;
  649. ecm->notify_req->complete = ecm_notify_complete;
  650. /* support all relevant hardware speeds... we expect that when
  651. * hardware is dual speed, all bulk-capable endpoints work at
  652. * both speeds
  653. */
  654. hs_ecm_in_desc.bEndpointAddress = fs_ecm_in_desc.bEndpointAddress;
  655. hs_ecm_out_desc.bEndpointAddress = fs_ecm_out_desc.bEndpointAddress;
  656. hs_ecm_notify_desc.bEndpointAddress =
  657. fs_ecm_notify_desc.bEndpointAddress;
  658. ss_ecm_in_desc.bEndpointAddress = fs_ecm_in_desc.bEndpointAddress;
  659. ss_ecm_out_desc.bEndpointAddress = fs_ecm_out_desc.bEndpointAddress;
  660. ss_ecm_notify_desc.bEndpointAddress =
  661. fs_ecm_notify_desc.bEndpointAddress;
  662. status = usb_assign_descriptors(f, ecm_fs_function, ecm_hs_function,
  663. ecm_ss_function);
  664. if (status)
  665. goto fail;
  666. /* NOTE: all that is done without knowing or caring about
  667. * the network link ... which is unavailable to this code
  668. * until we're activated via set_alt().
  669. */
  670. ecm->port.open = ecm_open;
  671. ecm->port.close = ecm_close;
  672. DBG(cdev, "CDC Ethernet: %s speed IN/%s OUT/%s NOTIFY/%s\n",
  673. gadget_is_superspeed(c->cdev->gadget) ? "super" :
  674. gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
  675. ecm->port.in_ep->name, ecm->port.out_ep->name,
  676. ecm->notify->name);
  677. return 0;
  678. fail:
  679. if (ecm->notify_req) {
  680. kfree(ecm->notify_req->buf);
  681. usb_ep_free_request(ecm->notify, ecm->notify_req);
  682. }
  683. /* we might as well release our claims on endpoints */
  684. if (ecm->notify)
  685. ecm->notify->driver_data = NULL;
  686. if (ecm->port.out_ep)
  687. ecm->port.out_ep->driver_data = NULL;
  688. if (ecm->port.in_ep)
  689. ecm->port.in_ep->driver_data = NULL;
  690. ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
  691. return status;
  692. }
  693. #ifdef USBF_ECM_INCLUDED
  694. static void
  695. ecm_old_unbind(struct usb_configuration *c, struct usb_function *f)
  696. {
  697. struct f_ecm *ecm = func_to_ecm(f);
  698. DBG(c->cdev, "ecm unbind\n");
  699. usb_free_all_descriptors(f);
  700. kfree(ecm->notify_req->buf);
  701. usb_ep_free_request(ecm->notify, ecm->notify_req);
  702. kfree(ecm);
  703. }
  704. /**
  705. * ecm_bind_config - add CDC Ethernet network link to a configuration
  706. * @c: the configuration to support the network link
  707. * @ethaddr: a buffer in which the ethernet address of the host side
  708. * side of the link was recorded
  709. * @dev: eth_dev structure
  710. * Context: single threaded during gadget setup
  711. *
  712. * Returns zero on success, else negative errno.
  713. *
  714. * Caller must have called @gether_setup(). Caller is also responsible
  715. * for calling @gether_cleanup() before module unload.
  716. */
  717. int
  718. ecm_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN],
  719. struct eth_dev *dev)
  720. {
  721. struct f_ecm *ecm;
  722. int status;
  723. if (!can_support_ecm(c->cdev->gadget) || !ethaddr)
  724. return -EINVAL;
  725. /* allocate and initialize one new instance */
  726. ecm = kzalloc(sizeof *ecm, GFP_KERNEL);
  727. if (!ecm)
  728. return -ENOMEM;
  729. /* export host's Ethernet address in CDC format */
  730. snprintf(ecm->ethaddr, sizeof ecm->ethaddr, "%pm", ethaddr);
  731. ecm_string_defs[1].s = ecm->ethaddr;
  732. ecm->port.ioport = dev;
  733. ecm->port.cdc_filter = DEFAULT_FILTER;
  734. ecm->port.func.name = "cdc_ethernet";
  735. /* descriptors are per-instance copies */
  736. ecm->port.func.bind = ecm_bind;
  737. ecm->port.func.unbind = ecm_old_unbind;
  738. ecm->port.func.set_alt = ecm_set_alt;
  739. ecm->port.func.get_alt = ecm_get_alt;
  740. ecm->port.func.setup = ecm_setup;
  741. ecm->port.func.disable = ecm_disable;
  742. status = usb_add_function(c, &ecm->port.func);
  743. if (status)
  744. kfree(ecm);
  745. return status;
  746. }
  747. #else
  748. static inline struct f_ecm_opts *to_f_ecm_opts(struct config_item *item)
  749. {
  750. return container_of(to_config_group(item), struct f_ecm_opts,
  751. func_inst.group);
  752. }
  753. /* f_ecm_item_ops */
  754. USB_ETHERNET_CONFIGFS_ITEM(ecm);
  755. /* f_ecm_opts_dev_addr */
  756. USB_ETHERNET_CONFIGFS_ITEM_ATTR_DEV_ADDR(ecm);
  757. /* f_ecm_opts_host_addr */
  758. USB_ETHERNET_CONFIGFS_ITEM_ATTR_HOST_ADDR(ecm);
  759. /* f_ecm_opts_qmult */
  760. USB_ETHERNET_CONFIGFS_ITEM_ATTR_QMULT(ecm);
  761. /* f_ecm_opts_ifname */
  762. USB_ETHERNET_CONFIGFS_ITEM_ATTR_IFNAME(ecm);
  763. static struct configfs_attribute *ecm_attrs[] = {
  764. &f_ecm_opts_dev_addr.attr,
  765. &f_ecm_opts_host_addr.attr,
  766. &f_ecm_opts_qmult.attr,
  767. &f_ecm_opts_ifname.attr,
  768. NULL,
  769. };
  770. static struct config_item_type ecm_func_type = {
  771. .ct_item_ops = &ecm_item_ops,
  772. .ct_attrs = ecm_attrs,
  773. .ct_owner = THIS_MODULE,
  774. };
  775. static void ecm_free_inst(struct usb_function_instance *f)
  776. {
  777. struct f_ecm_opts *opts;
  778. opts = container_of(f, struct f_ecm_opts, func_inst);
  779. if (opts->bound)
  780. gether_cleanup(netdev_priv(opts->net));
  781. else
  782. free_netdev(opts->net);
  783. kfree(opts);
  784. }
  785. static struct usb_function_instance *ecm_alloc_inst(void)
  786. {
  787. struct f_ecm_opts *opts;
  788. opts = kzalloc(sizeof(*opts), GFP_KERNEL);
  789. if (!opts)
  790. return ERR_PTR(-ENOMEM);
  791. mutex_init(&opts->lock);
  792. opts->func_inst.free_func_inst = ecm_free_inst;
  793. opts->net = gether_setup_default();
  794. if (IS_ERR(opts->net))
  795. return ERR_PTR(PTR_ERR(opts->net));
  796. config_group_init_type_name(&opts->func_inst.group, "", &ecm_func_type);
  797. return &opts->func_inst;
  798. }
  799. static void ecm_free(struct usb_function *f)
  800. {
  801. struct f_ecm *ecm;
  802. struct f_ecm_opts *opts;
  803. ecm = func_to_ecm(f);
  804. opts = container_of(f->fi, struct f_ecm_opts, func_inst);
  805. kfree(ecm);
  806. mutex_lock(&opts->lock);
  807. opts->refcnt--;
  808. mutex_unlock(&opts->lock);
  809. }
  810. static void ecm_unbind(struct usb_configuration *c, struct usb_function *f)
  811. {
  812. struct f_ecm *ecm = func_to_ecm(f);
  813. DBG(c->cdev, "ecm unbind\n");
  814. usb_free_all_descriptors(f);
  815. kfree(ecm->notify_req->buf);
  816. usb_ep_free_request(ecm->notify, ecm->notify_req);
  817. }
  818. struct usb_function *ecm_alloc(struct usb_function_instance *fi)
  819. {
  820. struct f_ecm *ecm;
  821. struct f_ecm_opts *opts;
  822. int status;
  823. /* allocate and initialize one new instance */
  824. ecm = kzalloc(sizeof(*ecm), GFP_KERNEL);
  825. if (!ecm)
  826. return ERR_PTR(-ENOMEM);
  827. opts = container_of(fi, struct f_ecm_opts, func_inst);
  828. mutex_lock(&opts->lock);
  829. opts->refcnt++;
  830. /* export host's Ethernet address in CDC format */
  831. status = gether_get_host_addr_cdc(opts->net, ecm->ethaddr,
  832. sizeof(ecm->ethaddr));
  833. if (status < 12) {
  834. kfree(ecm);
  835. mutex_unlock(&opts->lock);
  836. return ERR_PTR(-EINVAL);
  837. }
  838. ecm_string_defs[1].s = ecm->ethaddr;
  839. ecm->port.ioport = netdev_priv(opts->net);
  840. mutex_unlock(&opts->lock);
  841. ecm->port.cdc_filter = DEFAULT_FILTER;
  842. ecm->port.func.name = "cdc_ethernet";
  843. /* descriptors are per-instance copies */
  844. ecm->port.func.bind = ecm_bind;
  845. ecm->port.func.unbind = ecm_unbind;
  846. ecm->port.func.set_alt = ecm_set_alt;
  847. ecm->port.func.get_alt = ecm_get_alt;
  848. ecm->port.func.setup = ecm_setup;
  849. ecm->port.func.disable = ecm_disable;
  850. ecm->port.func.free_func = ecm_free;
  851. return &ecm->port.func;
  852. }
  853. DECLARE_USB_FUNCTION_INIT(ecm, ecm_alloc_inst, ecm_alloc);
  854. MODULE_LICENSE("GPL");
  855. MODULE_AUTHOR("David Brownell");
  856. #endif