f_qc_ecm.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077
  1. /*
  2. * f_qc_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. * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 and
  10. * only version 2 as published by the Free Software Foundation.
  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. #ifdef pr_fmt
  23. #undef pr_fmt
  24. #endif
  25. #define pr_fmt(fmt) "%s: " fmt, __func__
  26. #include <linux/slab.h>
  27. #include <linux/kernel.h>
  28. #include <linux/device.h>
  29. #include <linux/etherdevice.h>
  30. #include "u_ether.h"
  31. #include "u_qc_ether.h"
  32. #include "u_bam_data.h"
  33. #include <mach/ecm_ipa.h>
  34. /*
  35. * This function is a "CDC Ethernet Networking Control Model" (CDC ECM)
  36. * Ethernet link. The data transfer model is simple (packets sent and
  37. * received over bulk endpoints using normal short packet termination),
  38. * and the control model exposes various data and optional notifications.
  39. *
  40. * ECM is well standardized and (except for Microsoft) supported by most
  41. * operating systems with USB host support. It's the preferred interop
  42. * solution for Ethernet over USB, at least for firmware based solutions.
  43. * (Hardware solutions tend to be more minimalist.) A newer and simpler
  44. * "Ethernet Emulation Model" (CDC EEM) hasn't yet caught on.
  45. *
  46. * Note that ECM requires the use of "alternate settings" for its data
  47. * interface. This means that the set_alt() method has real work to do,
  48. * and also means that a get_alt() method is required.
  49. *
  50. * This function is based on USB CDC Ethernet link function driver and
  51. * contains MSM specific implementation.
  52. */
  53. enum ecm_qc_notify_state {
  54. ECM_QC_NOTIFY_NONE, /* don't notify */
  55. ECM_QC_NOTIFY_CONNECT, /* issue CONNECT next */
  56. ECM_QC_NOTIFY_SPEED, /* issue SPEED_CHANGE next */
  57. };
  58. struct f_ecm_qc {
  59. struct qc_gether port;
  60. u8 ctrl_id, data_id;
  61. enum transport_type xport;
  62. char ethaddr[14];
  63. struct usb_ep *notify;
  64. struct usb_request *notify_req;
  65. u8 notify_state;
  66. bool is_open;
  67. struct data_port bam_port;
  68. };
  69. static struct ecm_ipa_params ipa_params;
  70. static inline struct f_ecm_qc *func_to_ecm_qc(struct usb_function *f)
  71. {
  72. return container_of(f, struct f_ecm_qc, port.func);
  73. }
  74. /* peak (theoretical) bulk transfer rate in bits-per-second */
  75. static inline unsigned ecm_qc_bitrate(struct usb_gadget *g)
  76. {
  77. if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
  78. return 13 * 512 * 8 * 1000 * 8;
  79. else
  80. return 19 * 64 * 1 * 1000 * 8;
  81. }
  82. /*-------------------------------------------------------------------------*/
  83. /*
  84. * Include the status endpoint if we can, even though it's optional.
  85. *
  86. * Use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
  87. * packet, to simplify cancellation; and a big transfer interval, to
  88. * waste less bandwidth.
  89. *
  90. * Some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even
  91. * if they ignore the connect/disconnect notifications that real aether
  92. * can provide. More advanced cdc configurations might want to support
  93. * encapsulated commands (vendor-specific, using control-OUT).
  94. */
  95. #define ECM_QC_LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
  96. #define ECM_QC_STATUS_BYTECOUNT 16 /* 8 byte header + data */
  97. /* Currently only one std ecm instance is supported - port index 0. */
  98. #define ECM_QC_NO_PORTS 1
  99. #define ECM_QC_ACTIVE_PORT 0
  100. /* interface descriptor: */
  101. static struct usb_interface_descriptor ecm_qc_control_intf = {
  102. .bLength = sizeof ecm_qc_control_intf,
  103. .bDescriptorType = USB_DT_INTERFACE,
  104. /* .bInterfaceNumber = DYNAMIC */
  105. /* status endpoint is optional; this could be patched later */
  106. .bNumEndpoints = 1,
  107. .bInterfaceClass = USB_CLASS_COMM,
  108. .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET,
  109. .bInterfaceProtocol = USB_CDC_PROTO_NONE,
  110. /* .iInterface = DYNAMIC */
  111. };
  112. static struct usb_cdc_header_desc ecm_qc_header_desc = {
  113. .bLength = sizeof ecm_qc_header_desc,
  114. .bDescriptorType = USB_DT_CS_INTERFACE,
  115. .bDescriptorSubType = USB_CDC_HEADER_TYPE,
  116. .bcdCDC = cpu_to_le16(0x0110),
  117. };
  118. static struct usb_cdc_union_desc ecm_qc_union_desc = {
  119. .bLength = sizeof(ecm_qc_union_desc),
  120. .bDescriptorType = USB_DT_CS_INTERFACE,
  121. .bDescriptorSubType = USB_CDC_UNION_TYPE,
  122. /* .bMasterInterface0 = DYNAMIC */
  123. /* .bSlaveInterface0 = DYNAMIC */
  124. };
  125. static struct usb_cdc_ether_desc ecm_qc_desc = {
  126. .bLength = sizeof ecm_qc_desc,
  127. .bDescriptorType = USB_DT_CS_INTERFACE,
  128. .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
  129. /* this descriptor actually adds value, surprise! */
  130. /* .iMACAddress = DYNAMIC */
  131. .bmEthernetStatistics = cpu_to_le32(0), /* no statistics */
  132. .wMaxSegmentSize = cpu_to_le16(ETH_FRAME_LEN),
  133. .wNumberMCFilters = cpu_to_le16(0),
  134. .bNumberPowerFilters = 0,
  135. };
  136. /* the default data interface has no endpoints ... */
  137. static struct usb_interface_descriptor ecm_qc_data_nop_intf = {
  138. .bLength = sizeof ecm_qc_data_nop_intf,
  139. .bDescriptorType = USB_DT_INTERFACE,
  140. .bInterfaceNumber = 1,
  141. .bAlternateSetting = 0,
  142. .bNumEndpoints = 0,
  143. .bInterfaceClass = USB_CLASS_CDC_DATA,
  144. .bInterfaceSubClass = 0,
  145. .bInterfaceProtocol = 0,
  146. /* .iInterface = DYNAMIC */
  147. };
  148. /* ... but the "real" data interface has two bulk endpoints */
  149. static struct usb_interface_descriptor ecm_qc_data_intf = {
  150. .bLength = sizeof ecm_qc_data_intf,
  151. .bDescriptorType = USB_DT_INTERFACE,
  152. .bInterfaceNumber = 1,
  153. .bAlternateSetting = 1,
  154. .bNumEndpoints = 2,
  155. .bInterfaceClass = USB_CLASS_CDC_DATA,
  156. .bInterfaceSubClass = 0,
  157. .bInterfaceProtocol = 0,
  158. /* .iInterface = DYNAMIC */
  159. };
  160. /* full speed support: */
  161. static struct usb_endpoint_descriptor ecm_qc_fs_notify_desc = {
  162. .bLength = USB_DT_ENDPOINT_SIZE,
  163. .bDescriptorType = USB_DT_ENDPOINT,
  164. .bEndpointAddress = USB_DIR_IN,
  165. .bmAttributes = USB_ENDPOINT_XFER_INT,
  166. .wMaxPacketSize = cpu_to_le16(ECM_QC_STATUS_BYTECOUNT),
  167. .bInterval = 1 << ECM_QC_LOG2_STATUS_INTERVAL_MSEC,
  168. };
  169. static struct usb_endpoint_descriptor ecm_qc_fs_in_desc = {
  170. .bLength = USB_DT_ENDPOINT_SIZE,
  171. .bDescriptorType = USB_DT_ENDPOINT,
  172. .bEndpointAddress = USB_DIR_IN,
  173. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  174. };
  175. static struct usb_endpoint_descriptor ecm_qc_fs_out_desc = {
  176. .bLength = USB_DT_ENDPOINT_SIZE,
  177. .bDescriptorType = USB_DT_ENDPOINT,
  178. .bEndpointAddress = USB_DIR_OUT,
  179. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  180. };
  181. static struct usb_descriptor_header *ecm_qc_fs_function[] = {
  182. /* CDC ECM control descriptors */
  183. (struct usb_descriptor_header *) &ecm_qc_control_intf,
  184. (struct usb_descriptor_header *) &ecm_qc_header_desc,
  185. (struct usb_descriptor_header *) &ecm_qc_union_desc,
  186. (struct usb_descriptor_header *) &ecm_qc_desc,
  187. /* NOTE: status endpoint might need to be removed */
  188. (struct usb_descriptor_header *) &ecm_qc_fs_notify_desc,
  189. /* data interface, altsettings 0 and 1 */
  190. (struct usb_descriptor_header *) &ecm_qc_data_nop_intf,
  191. (struct usb_descriptor_header *) &ecm_qc_data_intf,
  192. (struct usb_descriptor_header *) &ecm_qc_fs_in_desc,
  193. (struct usb_descriptor_header *) &ecm_qc_fs_out_desc,
  194. NULL,
  195. };
  196. /* high speed support: */
  197. static struct usb_endpoint_descriptor ecm_qc_hs_notify_desc = {
  198. .bLength = USB_DT_ENDPOINT_SIZE,
  199. .bDescriptorType = USB_DT_ENDPOINT,
  200. .bEndpointAddress = USB_DIR_IN,
  201. .bmAttributes = USB_ENDPOINT_XFER_INT,
  202. .wMaxPacketSize = cpu_to_le16(ECM_QC_STATUS_BYTECOUNT),
  203. .bInterval = ECM_QC_LOG2_STATUS_INTERVAL_MSEC + 4,
  204. };
  205. static struct usb_endpoint_descriptor ecm_qc_hs_in_desc = {
  206. .bLength = USB_DT_ENDPOINT_SIZE,
  207. .bDescriptorType = USB_DT_ENDPOINT,
  208. .bEndpointAddress = USB_DIR_IN,
  209. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  210. .wMaxPacketSize = cpu_to_le16(512),
  211. };
  212. static struct usb_endpoint_descriptor ecm_qc_hs_out_desc = {
  213. .bLength = USB_DT_ENDPOINT_SIZE,
  214. .bDescriptorType = USB_DT_ENDPOINT,
  215. .bEndpointAddress = USB_DIR_OUT,
  216. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  217. .wMaxPacketSize = cpu_to_le16(512),
  218. };
  219. static struct usb_descriptor_header *ecm_qc_hs_function[] = {
  220. /* CDC ECM control descriptors */
  221. (struct usb_descriptor_header *) &ecm_qc_control_intf,
  222. (struct usb_descriptor_header *) &ecm_qc_header_desc,
  223. (struct usb_descriptor_header *) &ecm_qc_union_desc,
  224. (struct usb_descriptor_header *) &ecm_qc_desc,
  225. /* NOTE: status endpoint might need to be removed */
  226. (struct usb_descriptor_header *) &ecm_qc_hs_notify_desc,
  227. /* data interface, altsettings 0 and 1 */
  228. (struct usb_descriptor_header *) &ecm_qc_data_nop_intf,
  229. (struct usb_descriptor_header *) &ecm_qc_data_intf,
  230. (struct usb_descriptor_header *) &ecm_qc_hs_in_desc,
  231. (struct usb_descriptor_header *) &ecm_qc_hs_out_desc,
  232. NULL,
  233. };
  234. static struct usb_endpoint_descriptor ecm_qc_ss_notify_desc = {
  235. .bLength = USB_DT_ENDPOINT_SIZE,
  236. .bDescriptorType = USB_DT_ENDPOINT,
  237. .bEndpointAddress = USB_DIR_IN,
  238. .bmAttributes = USB_ENDPOINT_XFER_INT,
  239. .wMaxPacketSize = cpu_to_le16(ECM_QC_STATUS_BYTECOUNT),
  240. .bInterval = ECM_QC_LOG2_STATUS_INTERVAL_MSEC + 4,
  241. };
  242. static struct usb_ss_ep_comp_descriptor ecm_qc_ss_notify_comp_desc = {
  243. .bLength = sizeof(ecm_qc_ss_notify_comp_desc),
  244. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  245. /* the following 3 values can be tweaked if necessary */
  246. /* .bMaxBurst = 0, */
  247. /* .bmAttributes = 0, */
  248. .wBytesPerInterval = cpu_to_le16(ECM_QC_STATUS_BYTECOUNT),
  249. };
  250. static struct usb_endpoint_descriptor ecm_qc_ss_in_desc = {
  251. .bLength = USB_DT_ENDPOINT_SIZE,
  252. .bDescriptorType = USB_DT_ENDPOINT,
  253. .bEndpointAddress = USB_DIR_IN,
  254. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  255. .wMaxPacketSize = __constant_cpu_to_le16(1024),
  256. };
  257. static struct usb_ss_ep_comp_descriptor ecm_qc_ss_in_comp_desc = {
  258. .bLength = sizeof(ecm_qc_ss_in_comp_desc),
  259. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  260. /* the following 2 values can be tweaked if necessary */
  261. /* .bMaxBurst = 0, */
  262. /* .bmAttributes = 0, */
  263. };
  264. static struct usb_endpoint_descriptor ecm_qc_ss_out_desc = {
  265. .bLength = USB_DT_ENDPOINT_SIZE,
  266. .bDescriptorType = USB_DT_ENDPOINT,
  267. .bEndpointAddress = USB_DIR_OUT,
  268. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  269. .wMaxPacketSize = __constant_cpu_to_le16(1024),
  270. };
  271. static struct usb_ss_ep_comp_descriptor ecm_qc_ss_out_comp_desc = {
  272. .bLength = sizeof(ecm_qc_ss_out_comp_desc),
  273. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  274. /* the following 2 values can be tweaked if necessary */
  275. /* .bMaxBurst = 0, */
  276. /* .bmAttributes = 0, */
  277. };
  278. static struct usb_descriptor_header *ecm_qc_ss_function[] = {
  279. /* CDC ECM control descriptors */
  280. (struct usb_descriptor_header *) &ecm_qc_control_intf,
  281. (struct usb_descriptor_header *) &ecm_qc_header_desc,
  282. (struct usb_descriptor_header *) &ecm_qc_union_desc,
  283. (struct usb_descriptor_header *) &ecm_qc_desc,
  284. /* NOTE: status endpoint might need to be removed */
  285. (struct usb_descriptor_header *) &ecm_qc_ss_notify_desc,
  286. (struct usb_descriptor_header *) &ecm_qc_ss_notify_comp_desc,
  287. /* data interface, altsettings 0 and 1 */
  288. (struct usb_descriptor_header *) &ecm_qc_data_nop_intf,
  289. (struct usb_descriptor_header *) &ecm_qc_data_intf,
  290. (struct usb_descriptor_header *) &ecm_qc_ss_in_desc,
  291. (struct usb_descriptor_header *) &ecm_qc_ss_in_comp_desc,
  292. (struct usb_descriptor_header *) &ecm_qc_ss_out_desc,
  293. (struct usb_descriptor_header *) &ecm_qc_ss_out_comp_desc,
  294. NULL,
  295. };
  296. /* string descriptors: */
  297. static struct usb_string ecm_qc_string_defs[] = {
  298. [0].s = "CDC Ethernet Control Model (ECM)",
  299. [1].s = NULL /* DYNAMIC */,
  300. [2].s = "CDC Ethernet Data",
  301. { } /* end of list */
  302. };
  303. static struct usb_gadget_strings ecm_qc_string_table = {
  304. .language = 0x0409, /* en-us */
  305. .strings = ecm_qc_string_defs,
  306. };
  307. static struct usb_gadget_strings *ecm_qc_strings[] = {
  308. &ecm_qc_string_table,
  309. NULL,
  310. };
  311. static void ecm_qc_do_notify(struct f_ecm_qc *ecm)
  312. {
  313. struct usb_request *req = ecm->notify_req;
  314. struct usb_cdc_notification *event;
  315. struct usb_composite_dev *cdev = ecm->port.func.config->cdev;
  316. __le32 *data;
  317. int status;
  318. /* notification already in flight? */
  319. if (!req)
  320. return;
  321. event = req->buf;
  322. switch (ecm->notify_state) {
  323. case ECM_QC_NOTIFY_NONE:
  324. return;
  325. case ECM_QC_NOTIFY_CONNECT:
  326. event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
  327. if (ecm->is_open)
  328. event->wValue = cpu_to_le16(1);
  329. else
  330. event->wValue = cpu_to_le16(0);
  331. event->wLength = 0;
  332. req->length = sizeof *event;
  333. DBG(cdev, "notify connect %s\n",
  334. ecm->is_open ? "true" : "false");
  335. ecm->notify_state = ECM_QC_NOTIFY_SPEED;
  336. break;
  337. case ECM_QC_NOTIFY_SPEED:
  338. event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
  339. event->wValue = cpu_to_le16(0);
  340. event->wLength = cpu_to_le16(8);
  341. req->length = ECM_QC_STATUS_BYTECOUNT;
  342. /* SPEED_CHANGE data is up/down speeds in bits/sec */
  343. data = req->buf + sizeof *event;
  344. data[0] = cpu_to_le32(ecm_qc_bitrate(cdev->gadget));
  345. data[1] = data[0];
  346. DBG(cdev, "notify speed %d\n", ecm_qc_bitrate(cdev->gadget));
  347. ecm->notify_state = ECM_QC_NOTIFY_NONE;
  348. break;
  349. }
  350. event->bmRequestType = 0xA1;
  351. event->wIndex = cpu_to_le16(ecm->ctrl_id);
  352. ecm->notify_req = NULL;
  353. status = usb_ep_queue(ecm->notify, req, GFP_ATOMIC);
  354. if (status < 0) {
  355. ecm->notify_req = req;
  356. DBG(cdev, "notify --> %d\n", status);
  357. }
  358. }
  359. static void ecm_qc_notify(struct f_ecm_qc *ecm)
  360. {
  361. /* NOTE on most versions of Linux, host side cdc-ethernet
  362. * won't listen for notifications until its netdevice opens.
  363. * The first notification then sits in the FIFO for a long
  364. * time, and the second one is queued.
  365. */
  366. ecm->notify_state = ECM_QC_NOTIFY_CONNECT;
  367. ecm_qc_do_notify(ecm);
  368. }
  369. static int ecm_qc_bam_setup(void)
  370. {
  371. int ret;
  372. ret = bam_data_setup(ECM_QC_NO_PORTS);
  373. if (ret) {
  374. pr_err("bam_data_setup failed err: %d\n", ret);
  375. return ret;
  376. }
  377. return 0;
  378. }
  379. static int ecm_qc_bam_connect(struct f_ecm_qc *dev)
  380. {
  381. int ret;
  382. u8 src_connection_idx, dst_connection_idx;
  383. struct usb_composite_dev *cdev = dev->port.func.config->cdev;
  384. struct usb_gadget *gadget = cdev->gadget;
  385. enum peer_bam peer_bam = (dev->xport == USB_GADGET_XPORT_BAM2BAM_IPA) ?
  386. IPA_P_BAM : A2_P_BAM;
  387. dev->bam_port.cdev = cdev;
  388. dev->bam_port.func = &dev->port.func;
  389. dev->bam_port.in = dev->port.in_ep;
  390. dev->bam_port.out = dev->port.out_ep;
  391. /* currently we use the first connection */
  392. src_connection_idx = usb_bam_get_connection_idx(gadget->name, peer_bam,
  393. USB_TO_PEER_PERIPHERAL, 0);
  394. dst_connection_idx = usb_bam_get_connection_idx(gadget->name, peer_bam,
  395. PEER_PERIPHERAL_TO_USB, 0);
  396. if (src_connection_idx < 0 || dst_connection_idx < 0) {
  397. pr_err("%s: usb_bam_get_connection_idx failed\n", __func__);
  398. return ret;
  399. }
  400. ret = bam_data_connect(&dev->bam_port, 0, dev->xport,
  401. src_connection_idx, dst_connection_idx, USB_FUNC_ECM);
  402. if (ret) {
  403. pr_err("bam_data_connect failed: err:%d\n", ret);
  404. return ret;
  405. } else {
  406. pr_debug("ecm bam connected\n");
  407. }
  408. dev->is_open = true;
  409. ecm_qc_notify(dev);
  410. return 0;
  411. }
  412. static int ecm_qc_bam_disconnect(struct f_ecm_qc *dev)
  413. {
  414. pr_debug("dev:%pK. Disconnect BAM.\n", dev);
  415. bam_data_disconnect(&dev->bam_port, 0);
  416. return 0;
  417. }
  418. void *ecm_qc_get_ipa_rx_cb(void)
  419. {
  420. return ipa_params.ecm_ipa_rx_dp_notify;
  421. }
  422. void *ecm_qc_get_ipa_tx_cb(void)
  423. {
  424. return ipa_params.ecm_ipa_tx_dp_notify;
  425. }
  426. void *ecm_qc_get_ipa_priv(void)
  427. {
  428. return ipa_params.private;
  429. }
  430. /*-------------------------------------------------------------------------*/
  431. static void ecm_qc_notify_complete(struct usb_ep *ep, struct usb_request *req)
  432. {
  433. struct f_ecm_qc *ecm = req->context;
  434. struct usb_composite_dev *cdev = ecm->port.func.config->cdev;
  435. struct usb_cdc_notification *event = req->buf;
  436. switch (req->status) {
  437. case 0:
  438. /* no fault */
  439. break;
  440. case -ECONNRESET:
  441. case -ESHUTDOWN:
  442. ecm->notify_state = ECM_QC_NOTIFY_NONE;
  443. break;
  444. default:
  445. DBG(cdev, "event %02x --> %d\n",
  446. event->bNotificationType, req->status);
  447. break;
  448. }
  449. ecm->notify_req = req;
  450. ecm_qc_do_notify(ecm);
  451. }
  452. static int ecm_qc_setup(struct usb_function *f,
  453. const struct usb_ctrlrequest *ctrl)
  454. {
  455. struct f_ecm_qc *ecm = func_to_ecm_qc(f);
  456. struct usb_composite_dev *cdev = f->config->cdev;
  457. struct usb_request *req = cdev->req;
  458. int value = -EOPNOTSUPP;
  459. u16 w_index = le16_to_cpu(ctrl->wIndex);
  460. u16 w_value = le16_to_cpu(ctrl->wValue);
  461. u16 w_length = le16_to_cpu(ctrl->wLength);
  462. /* composite driver infrastructure handles everything except
  463. * CDC class messages; interface activation uses set_alt().
  464. */
  465. switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
  466. case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
  467. | USB_CDC_SET_ETHERNET_PACKET_FILTER:
  468. /* see 6.2.30: no data, wIndex = interface,
  469. * wValue = packet filter bitmap
  470. */
  471. if (w_length != 0 || w_index != ecm->ctrl_id)
  472. goto invalid;
  473. DBG(cdev, "packet filter %02x\n", w_value);
  474. /* REVISIT locking of cdc_filter. This assumes the UDC
  475. * driver won't have a concurrent packet TX irq running on
  476. * another CPU; or that if it does, this write is atomic...
  477. */
  478. ecm->port.cdc_filter = w_value;
  479. value = 0;
  480. break;
  481. /* and optionally:
  482. * case USB_CDC_SEND_ENCAPSULATED_COMMAND:
  483. * case USB_CDC_GET_ENCAPSULATED_RESPONSE:
  484. * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
  485. * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
  486. * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
  487. * case USB_CDC_GET_ETHERNET_STATISTIC:
  488. */
  489. default:
  490. invalid:
  491. DBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
  492. ctrl->bRequestType, ctrl->bRequest,
  493. w_value, w_index, w_length);
  494. }
  495. /* respond with data transfer or status phase? */
  496. if (value >= 0) {
  497. DBG(cdev, "ecm req%02x.%02x v%04x i%04x l%d\n",
  498. ctrl->bRequestType, ctrl->bRequest,
  499. w_value, w_index, w_length);
  500. req->zero = 0;
  501. req->length = value;
  502. value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
  503. if (value < 0)
  504. pr_err("ecm req %02x.%02x response err %d\n",
  505. ctrl->bRequestType, ctrl->bRequest,
  506. value);
  507. }
  508. /* device either stalls (value < 0) or reports success */
  509. return value;
  510. }
  511. static int ecm_qc_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
  512. {
  513. struct f_ecm_qc *ecm = func_to_ecm_qc(f);
  514. struct usb_composite_dev *cdev = f->config->cdev;
  515. /* Control interface has only altsetting 0 */
  516. if (intf == ecm->ctrl_id) {
  517. if (alt != 0)
  518. goto fail;
  519. if (ecm->notify->driver_data) {
  520. VDBG(cdev, "reset ecm control %d\n", intf);
  521. usb_ep_disable(ecm->notify);
  522. }
  523. if (!(ecm->notify->desc)) {
  524. VDBG(cdev, "init ecm ctrl %d\n", intf);
  525. if (config_ep_by_speed(cdev->gadget, f, ecm->notify))
  526. goto fail;
  527. }
  528. usb_ep_enable(ecm->notify);
  529. ecm->notify->driver_data = ecm;
  530. /* Data interface has two altsettings, 0 and 1 */
  531. } else if (intf == ecm->data_id) {
  532. if (alt > 1)
  533. goto fail;
  534. if (ecm->port.in_ep->driver_data) {
  535. DBG(cdev, "reset ecm\n");
  536. /* ecm->port is needed for disconnecting the BAM data
  537. * path. Only after the BAM data path is disconnected,
  538. * we can disconnect the port from the network layer.
  539. */
  540. ecm_qc_bam_disconnect(ecm);
  541. if (ecm->xport != USB_GADGET_XPORT_BAM2BAM_IPA)
  542. gether_qc_disconnect_name(&ecm->port, "ecm0");
  543. }
  544. if (!ecm->port.in_ep->desc ||
  545. !ecm->port.out_ep->desc) {
  546. DBG(cdev, "init ecm\n");
  547. if (config_ep_by_speed(cdev->gadget, f,
  548. ecm->port.in_ep) ||
  549. config_ep_by_speed(cdev->gadget, f,
  550. ecm->port.out_ep)) {
  551. ecm->port.in_ep->desc = NULL;
  552. ecm->port.out_ep->desc = NULL;
  553. goto fail;
  554. }
  555. }
  556. /* CDC Ethernet only sends data in non-default altsettings.
  557. * Changing altsettings resets filters, statistics, etc.
  558. */
  559. if (alt == 1) {
  560. struct net_device *net;
  561. /* Enable zlps by default for ECM conformance;
  562. * override for musb_hdrc (avoids txdma ovhead).
  563. */
  564. ecm->port.is_zlp_ok = !(gadget_is_musbhdrc(cdev->gadget)
  565. );
  566. ecm->port.cdc_filter = DEFAULT_FILTER;
  567. DBG(cdev, "activate ecm\n");
  568. if (ecm->xport != USB_GADGET_XPORT_BAM2BAM_IPA) {
  569. net = gether_qc_connect_name(&ecm->port,
  570. "ecm0", true);
  571. if (IS_ERR(net))
  572. return PTR_ERR(net);
  573. }
  574. if (ecm_qc_bam_connect(ecm))
  575. goto fail;
  576. }
  577. /* NOTE this can be a minor disagreement with the ECM spec,
  578. * which says speed notifications will "always" follow
  579. * connection notifications. But we allow one connect to
  580. * follow another (if the first is in flight), and instead
  581. * just guarantee that a speed notification is always sent.
  582. */
  583. ecm_qc_notify(ecm);
  584. } else
  585. goto fail;
  586. return 0;
  587. fail:
  588. return -EINVAL;
  589. }
  590. /* Because the data interface supports multiple altsettings,
  591. * this ECM function *MUST* implement a get_alt() method.
  592. */
  593. static int ecm_qc_get_alt(struct usb_function *f, unsigned intf)
  594. {
  595. struct f_ecm_qc *ecm = func_to_ecm_qc(f);
  596. if (intf == ecm->ctrl_id)
  597. return 0;
  598. return ecm->port.in_ep->driver_data ? 1 : 0;
  599. }
  600. static void ecm_qc_disable(struct usb_function *f)
  601. {
  602. struct f_ecm_qc *ecm = func_to_ecm_qc(f);
  603. struct usb_composite_dev *cdev = ecm->port.func.config->cdev;
  604. DBG(cdev, "ecm deactivated\n");
  605. if (ecm->port.in_ep->driver_data) {
  606. ecm_qc_bam_disconnect(ecm);
  607. if (ecm->xport != USB_GADGET_XPORT_BAM2BAM_IPA)
  608. gether_qc_disconnect_name(&ecm->port, "ecm0");
  609. }
  610. if (ecm->notify->driver_data) {
  611. usb_ep_disable(ecm->notify);
  612. ecm->notify->driver_data = NULL;
  613. ecm->notify->desc = NULL;
  614. }
  615. }
  616. static void ecm_qc_suspend(struct usb_function *f)
  617. {
  618. pr_debug("ecm suspended\n");
  619. bam_data_suspend(ECM_QC_ACTIVE_PORT);
  620. }
  621. static void ecm_qc_resume(struct usb_function *f)
  622. {
  623. pr_debug("ecm resumed\n");
  624. bam_data_resume(ECM_QC_ACTIVE_PORT);
  625. }
  626. /*-------------------------------------------------------------------------*/
  627. /*
  628. * Callbacks let us notify the host about connect/disconnect when the
  629. * net device is opened or closed.
  630. *
  631. * For testing, note that link states on this side include both opened
  632. * and closed variants of:
  633. *
  634. * - disconnected/unconfigured
  635. * - configured but inactive (data alt 0)
  636. * - configured and active (data alt 1)
  637. *
  638. * Each needs to be tested with unplug, rmmod, SET_CONFIGURATION, and
  639. * SET_INTERFACE (altsetting). Remember also that "configured" doesn't
  640. * imply the host is actually polling the notification endpoint, and
  641. * likewise that "active" doesn't imply it's actually using the data
  642. * endpoints for traffic.
  643. */
  644. static void ecm_qc_open(struct qc_gether *geth)
  645. {
  646. struct f_ecm_qc *ecm = func_to_ecm_qc(&geth->func);
  647. DBG(ecm->port.func.config->cdev, "%s\n", __func__);
  648. ecm->is_open = true;
  649. ecm_qc_notify(ecm);
  650. }
  651. static void ecm_qc_close(struct qc_gether *geth)
  652. {
  653. struct f_ecm_qc *ecm = func_to_ecm_qc(&geth->func);
  654. DBG(ecm->port.func.config->cdev, "%s\n", __func__);
  655. ecm->is_open = false;
  656. ecm_qc_notify(ecm);
  657. }
  658. /*-------------------------------------------------------------------------*/
  659. /* ethernet function driver setup/binding */
  660. static int
  661. ecm_qc_bind(struct usb_configuration *c, struct usb_function *f)
  662. {
  663. struct usb_composite_dev *cdev = c->cdev;
  664. struct f_ecm_qc *ecm = func_to_ecm_qc(f);
  665. int status;
  666. struct usb_ep *ep;
  667. /* allocate instance-specific interface IDs */
  668. status = usb_interface_id(c, f);
  669. if (status < 0)
  670. goto fail;
  671. ecm->ctrl_id = status;
  672. ecm_qc_control_intf.bInterfaceNumber = status;
  673. ecm_qc_union_desc.bMasterInterface0 = status;
  674. status = usb_interface_id(c, f);
  675. if (status < 0)
  676. goto fail;
  677. ecm->data_id = status;
  678. ecm_qc_data_nop_intf.bInterfaceNumber = status;
  679. ecm_qc_data_intf.bInterfaceNumber = status;
  680. ecm_qc_union_desc.bSlaveInterface0 = status;
  681. status = -ENODEV;
  682. /* allocate instance-specific endpoints */
  683. ep = usb_ep_autoconfig(cdev->gadget, &ecm_qc_fs_in_desc);
  684. if (!ep)
  685. goto fail;
  686. ecm->port.in_ep = ep;
  687. ep->driver_data = cdev; /* claim */
  688. ep = usb_ep_autoconfig(cdev->gadget, &ecm_qc_fs_out_desc);
  689. if (!ep)
  690. goto fail;
  691. ecm->port.out_ep = ep;
  692. ep->driver_data = cdev; /* claim */
  693. /* NOTE: a status/notification endpoint is *OPTIONAL* but we
  694. * don't treat it that way. It's simpler, and some newer CDC
  695. * profiles (wireless handsets) no longer treat it as optional.
  696. */
  697. ep = usb_ep_autoconfig(cdev->gadget, &ecm_qc_fs_notify_desc);
  698. if (!ep)
  699. goto fail;
  700. ecm->notify = ep;
  701. ep->driver_data = cdev; /* claim */
  702. status = -ENOMEM;
  703. /* allocate notification request and buffer */
  704. ecm->notify_req = usb_ep_alloc_request(ep, GFP_KERNEL);
  705. if (!ecm->notify_req)
  706. goto fail;
  707. ecm->notify_req->buf = kmalloc(ECM_QC_STATUS_BYTECOUNT, GFP_KERNEL);
  708. if (!ecm->notify_req->buf)
  709. goto fail;
  710. ecm->notify_req->context = ecm;
  711. ecm->notify_req->complete = ecm_qc_notify_complete;
  712. /* copy descriptors, and track endpoint copies */
  713. f->fs_descriptors = usb_copy_descriptors(ecm_qc_fs_function);
  714. if (!f->fs_descriptors)
  715. goto fail;
  716. /* support all relevant hardware speeds... we expect that when
  717. * hardware is dual speed, all bulk-capable endpoints work at
  718. * both speeds
  719. */
  720. if (gadget_is_dualspeed(c->cdev->gadget)) {
  721. ecm_qc_hs_in_desc.bEndpointAddress =
  722. ecm_qc_fs_in_desc.bEndpointAddress;
  723. ecm_qc_hs_out_desc.bEndpointAddress =
  724. ecm_qc_fs_out_desc.bEndpointAddress;
  725. ecm_qc_hs_notify_desc.bEndpointAddress =
  726. ecm_qc_fs_notify_desc.bEndpointAddress;
  727. /* copy descriptors, and track endpoint copies */
  728. f->hs_descriptors = usb_copy_descriptors(ecm_qc_hs_function);
  729. if (!f->hs_descriptors)
  730. goto fail;
  731. }
  732. if (gadget_is_superspeed(c->cdev->gadget)) {
  733. ecm_qc_ss_in_desc.bEndpointAddress =
  734. ecm_qc_fs_in_desc.bEndpointAddress;
  735. ecm_qc_ss_out_desc.bEndpointAddress =
  736. ecm_qc_fs_out_desc.bEndpointAddress;
  737. ecm_qc_ss_notify_desc.bEndpointAddress =
  738. ecm_qc_fs_notify_desc.bEndpointAddress;
  739. f->ss_descriptors = usb_copy_descriptors(ecm_qc_ss_function);
  740. if (!f->hs_descriptors)
  741. goto fail;
  742. }
  743. /* NOTE: all that is done without knowing or caring about
  744. * the network link ... which is unavailable to this code
  745. * until we're activated via set_alt().
  746. */
  747. ecm->port.open = ecm_qc_open;
  748. ecm->port.close = ecm_qc_close;
  749. DBG(cdev, "CDC Ethernet: %s speed IN/%s OUT/%s NOTIFY/%s\n",
  750. gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
  751. ecm->port.in_ep->name, ecm->port.out_ep->name,
  752. ecm->notify->name);
  753. return 0;
  754. fail:
  755. if (f->ss_descriptors)
  756. usb_free_descriptors(f->ss_descriptors);
  757. if (f->hs_descriptors)
  758. usb_free_descriptors(f->hs_descriptors);
  759. if (f->fs_descriptors)
  760. usb_free_descriptors(f->fs_descriptors);
  761. if (ecm->notify_req) {
  762. kfree(ecm->notify_req->buf);
  763. usb_ep_free_request(ecm->notify, ecm->notify_req);
  764. }
  765. /* we might as well release our claims on endpoints */
  766. if (ecm->notify)
  767. ecm->notify->driver_data = NULL;
  768. if (ecm->port.out_ep->desc)
  769. ecm->port.out_ep->driver_data = NULL;
  770. if (ecm->port.in_ep->desc)
  771. ecm->port.in_ep->driver_data = NULL;
  772. pr_err("%s: can't bind, err %d\n", f->name, status);
  773. return status;
  774. }
  775. static void
  776. ecm_qc_unbind(struct usb_configuration *c, struct usb_function *f)
  777. {
  778. struct f_ecm_qc *ecm = func_to_ecm_qc(f);
  779. DBG(c->cdev, "ecm unbind\n");
  780. bam_data_destroy(0);
  781. if (gadget_is_superspeed(c->cdev->gadget))
  782. usb_free_descriptors(f->ss_descriptors);
  783. if (gadget_is_dualspeed(c->cdev->gadget))
  784. usb_free_descriptors(f->hs_descriptors);
  785. usb_free_descriptors(f->fs_descriptors);
  786. kfree(ecm->notify_req->buf);
  787. usb_ep_free_request(ecm->notify, ecm->notify_req);
  788. ecm_qc_string_defs[1].s = NULL;
  789. if (ecm->xport == USB_GADGET_XPORT_BAM2BAM_IPA)
  790. ecm_ipa_cleanup(ipa_params.private);
  791. kfree(ecm);
  792. }
  793. /**
  794. * ecm_qc_bind_config - add CDC Ethernet network link to a configuration
  795. * @c: the configuration to support the network link
  796. * @ethaddr: a buffer in which the ethernet address of the host side
  797. * side of the link was recorded
  798. * @xport_name: data path transport type name ("BAM2BAM" or "BAM2BAM_IPA")
  799. * Context: single threaded during gadget setup
  800. *
  801. * Returns zero on success, else negative errno.
  802. *
  803. * Caller must have called @gether_qc_setup(). Caller is also responsible
  804. * for calling @gether_cleanup() before module unload.
  805. */
  806. int
  807. ecm_qc_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN],
  808. char *xport_name)
  809. {
  810. struct f_ecm_qc *ecm;
  811. int status;
  812. if (!can_support_ecm(c->cdev->gadget) || !ethaddr)
  813. return -EINVAL;
  814. status = ecm_qc_bam_setup();
  815. if (status) {
  816. pr_err("bam setup failed");
  817. return status;
  818. }
  819. pr_debug("data transport type is %s", xport_name);
  820. /* maybe allocate device-global string IDs */
  821. if (ecm_qc_string_defs[0].id == 0) {
  822. /* control interface label */
  823. status = usb_string_id(c->cdev);
  824. if (status < 0)
  825. return status;
  826. ecm_qc_string_defs[0].id = status;
  827. ecm_qc_control_intf.iInterface = status;
  828. /* data interface label */
  829. status = usb_string_id(c->cdev);
  830. if (status < 0)
  831. return status;
  832. ecm_qc_string_defs[2].id = status;
  833. ecm_qc_data_intf.iInterface = status;
  834. /* MAC address */
  835. status = usb_string_id(c->cdev);
  836. if (status < 0)
  837. return status;
  838. ecm_qc_string_defs[1].id = status;
  839. ecm_qc_desc.iMACAddress = status;
  840. }
  841. /* allocate and initialize one new instance */
  842. ecm = kzalloc(sizeof *ecm, GFP_KERNEL);
  843. if (!ecm)
  844. return -ENOMEM;
  845. ecm->xport = str_to_xport(xport_name);
  846. pr_debug("set xport = %d", ecm->xport);
  847. /* export host's Ethernet address in CDC format */
  848. if (ecm->xport == USB_GADGET_XPORT_BAM2BAM_IPA) {
  849. gether_qc_get_macs(ipa_params.device_ethaddr,
  850. ipa_params.host_ethaddr);
  851. snprintf(ecm->ethaddr, sizeof ecm->ethaddr,
  852. "%02X%02X%02X%02X%02X%02X",
  853. ipa_params.host_ethaddr[0], ipa_params.host_ethaddr[1],
  854. ipa_params.host_ethaddr[2], ipa_params.host_ethaddr[3],
  855. ipa_params.host_ethaddr[4], ipa_params.host_ethaddr[5]);
  856. } else
  857. snprintf(ecm->ethaddr, sizeof ecm->ethaddr,
  858. "%02X%02X%02X%02X%02X%02X",
  859. ethaddr[0], ethaddr[1], ethaddr[2],
  860. ethaddr[3], ethaddr[4], ethaddr[5]);
  861. ecm_qc_string_defs[1].s = ecm->ethaddr;
  862. ecm->port.cdc_filter = DEFAULT_FILTER;
  863. ecm->port.func.name = "cdc_ethernet";
  864. ecm->port.func.strings = ecm_qc_strings;
  865. /* descriptors are per-instance copies */
  866. ecm->port.func.bind = ecm_qc_bind;
  867. ecm->port.func.unbind = ecm_qc_unbind;
  868. ecm->port.func.set_alt = ecm_qc_set_alt;
  869. ecm->port.func.get_alt = ecm_qc_get_alt;
  870. ecm->port.func.setup = ecm_qc_setup;
  871. ecm->port.func.disable = ecm_qc_disable;
  872. ecm->port.func.suspend = ecm_qc_suspend;
  873. ecm->port.func.resume = ecm_qc_resume;
  874. status = usb_add_function(c, &ecm->port.func);
  875. if (status) {
  876. pr_err("failed to add function");
  877. ecm_qc_string_defs[1].s = NULL;
  878. kfree(ecm);
  879. return status;
  880. }
  881. if (ecm->xport != USB_GADGET_XPORT_BAM2BAM_IPA)
  882. return status;
  883. pr_debug("setting ecm_ipa, host_ethaddr=%pM, device_ethaddr=%pM",
  884. ipa_params.host_ethaddr, ipa_params.device_ethaddr);
  885. status = ecm_ipa_init(&ipa_params);
  886. if (status) {
  887. pr_err("failed to initialize ecm_ipa");
  888. ecm_qc_string_defs[1].s = NULL;
  889. kfree(ecm);
  890. } else {
  891. pr_debug("ecm_ipa successful created");
  892. }
  893. return status;
  894. }