f_ecm.c 23 KB

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