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