f_ecm.c 26 KB

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