f_ecm.c 23 KB

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