f_rndis.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  1. /*
  2. * f_rndis.c -- RNDIS link function driver
  3. *
  4. * Copyright (C) 2003-2005,2008 David Brownell
  5. * Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger
  6. * Copyright (C) 2008 Nokia Corporation
  7. * Copyright (C) 2009 Samsung Electronics
  8. * Author: Michal Nazarewicz (mina86@mina86.com)
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. */
  15. /* #define VERBOSE_DEBUG */
  16. #include <linux/slab.h>
  17. #include <linux/kernel.h>
  18. #include <linux/device.h>
  19. #include <linux/etherdevice.h>
  20. #include <linux/atomic.h>
  21. #include "u_ether.h"
  22. #include "rndis.h"
  23. #define F_RNDIS_LOG "USB_RNDIS"
  24. #define f_rndis_debug 0
  25. /*
  26. * This function is an RNDIS Ethernet port -- a Microsoft protocol that's
  27. * been promoted instead of the standard CDC Ethernet. The published RNDIS
  28. * spec is ambiguous, incomplete, and needlessly complex. Variants such as
  29. * ActiveSync have even worse status in terms of specification.
  30. *
  31. * In short: it's a protocol controlled by (and for) Microsoft, not for an
  32. * Open ecosystem or markets. Linux supports it *only* because Microsoft
  33. * doesn't support the CDC Ethernet standard.
  34. *
  35. * The RNDIS data transfer model is complex, with multiple Ethernet packets
  36. * per USB message, and out of band data. The control model is built around
  37. * what's essentially an "RNDIS RPC" protocol. It's all wrapped in a CDC ACM
  38. * (modem, not Ethernet) veneer, with those ACM descriptors being entirely
  39. * useless (they're ignored). RNDIS expects to be the only function in its
  40. * configuration, so it's no real help if you need composite devices; and
  41. * it expects to be the first configuration too.
  42. *
  43. * There is a single technical advantage of RNDIS over CDC Ethernet, if you
  44. * discount the fluff that its RPC can be made to deliver: it doesn't need
  45. * a NOP altsetting for the data interface. That lets it work on some of the
  46. * "so smart it's stupid" hardware which takes over configuration changes
  47. * from the software, and adds restrictions like "no altsettings".
  48. *
  49. * Unfortunately MSFT's RNDIS drivers are buggy. They hang or oops, and
  50. * have all sorts of contrary-to-specification oddities that can prevent
  51. * them from working sanely. Since bugfixes (or accurate specs, letting
  52. * Linux work around those bugs) are unlikely to ever come from MSFT, you
  53. * may want to avoid using RNDIS on purely operational grounds.
  54. *
  55. * Omissions from the RNDIS 1.0 specification include:
  56. *
  57. * - Power management ... references data that's scattered around lots
  58. * of other documentation, which is incorrect/incomplete there too.
  59. *
  60. * - There are various undocumented protocol requirements, like the need
  61. * to send garbage in some control-OUT messages.
  62. *
  63. * - MS-Windows drivers sometimes emit undocumented requests.
  64. */
  65. static unsigned int rndis_dl_max_pkt_per_xfer = 3;
  66. module_param(rndis_dl_max_pkt_per_xfer, uint, S_IRUGO | S_IWUSR);
  67. MODULE_PARM_DESC(rndis_dl_max_pkt_per_xfer,
  68. "Maximum packets per transfer for DL aggregation");
  69. static unsigned int rndis_ul_max_pkt_per_xfer = 1;
  70. module_param(rndis_ul_max_pkt_per_xfer, uint, S_IRUGO | S_IWUSR);
  71. MODULE_PARM_DESC(rndis_ul_max_pkt_per_xfer,
  72. "Maximum packets per transfer for UL aggregation");
  73. struct f_rndis {
  74. struct gether port;
  75. u8 ctrl_id, data_id;
  76. u8 ethaddr[ETH_ALEN];
  77. u32 vendorID;
  78. const char *manufacturer;
  79. int config;
  80. struct usb_ep *notify;
  81. struct usb_request *notify_req;
  82. atomic_t notify_count;
  83. };
  84. static inline struct f_rndis *func_to_rndis(struct usb_function *f)
  85. {
  86. return container_of(f, struct f_rndis, port.func);
  87. }
  88. /* peak (theoretical) bulk transfer rate in bits-per-second */
  89. static unsigned int bitrate(struct usb_gadget *g)
  90. {
  91. if (gadget_is_superspeed(g) && g->speed == USB_SPEED_SUPER)
  92. return 13 * 1024 * 8 * 1000 * 8;
  93. else if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
  94. return 13 * 512 * 8 * 1000 * 8;
  95. else
  96. return 19 * 64 * 1 * 1000 * 8;
  97. }
  98. /*-------------------------------------------------------------------------*/
  99. /*
  100. */
  101. #define RNDIS_STATUS_INTERVAL_MS 32
  102. #define STATUS_BYTECOUNT 8 /* 8 bytes data */
  103. /* interface descriptor: */
  104. static struct usb_interface_descriptor rndis_control_intf = {
  105. .bLength = sizeof rndis_control_intf,
  106. .bDescriptorType = USB_DT_INTERFACE,
  107. /* .bInterfaceNumber = DYNAMIC */
  108. /* status endpoint is optional; this could be patched later */
  109. .bNumEndpoints = 1,
  110. .bInterfaceClass = USB_CLASS_COMM,
  111. .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
  112. .bInterfaceProtocol = USB_CDC_ACM_PROTO_VENDOR,
  113. /* .iInterface = DYNAMIC */
  114. };
  115. static struct usb_cdc_header_desc header_desc = {
  116. .bLength = sizeof header_desc,
  117. .bDescriptorType = USB_DT_CS_INTERFACE,
  118. .bDescriptorSubType = USB_CDC_HEADER_TYPE,
  119. .bcdCDC = cpu_to_le16(0x0110),
  120. };
  121. static struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor = {
  122. .bLength = sizeof call_mgmt_descriptor,
  123. .bDescriptorType = USB_DT_CS_INTERFACE,
  124. .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
  125. .bmCapabilities = 0x00,
  126. .bDataInterface = 0x01,
  127. };
  128. static struct usb_cdc_acm_descriptor rndis_acm_descriptor = {
  129. .bLength = sizeof rndis_acm_descriptor,
  130. .bDescriptorType = USB_DT_CS_INTERFACE,
  131. .bDescriptorSubType = USB_CDC_ACM_TYPE,
  132. .bmCapabilities = 0x00,
  133. };
  134. static struct usb_cdc_union_desc rndis_union_desc = {
  135. .bLength = sizeof(rndis_union_desc),
  136. .bDescriptorType = USB_DT_CS_INTERFACE,
  137. .bDescriptorSubType = USB_CDC_UNION_TYPE,
  138. /* .bMasterInterface0 = DYNAMIC */
  139. /* .bSlaveInterface0 = DYNAMIC */
  140. };
  141. /* the data interface has two bulk endpoints */
  142. static struct usb_interface_descriptor rndis_data_intf = {
  143. .bLength = sizeof rndis_data_intf,
  144. .bDescriptorType = USB_DT_INTERFACE,
  145. /* .bInterfaceNumber = DYNAMIC */
  146. .bNumEndpoints = 2,
  147. .bInterfaceClass = USB_CLASS_CDC_DATA,
  148. .bInterfaceSubClass = 0,
  149. .bInterfaceProtocol = 0,
  150. /* .iInterface = DYNAMIC */
  151. };
  152. static struct usb_interface_assoc_descriptor
  153. rndis_iad_descriptor = {
  154. .bLength = sizeof rndis_iad_descriptor,
  155. .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
  156. .bFirstInterface = 0, /* XXX, hardcoded */
  157. .bInterfaceCount = 2, // control + data
  158. .bFunctionClass = USB_CLASS_COMM,
  159. .bFunctionSubClass = USB_CDC_SUBCLASS_ETHERNET,
  160. .bFunctionProtocol = USB_CDC_PROTO_NONE,
  161. /* .iFunction = DYNAMIC */
  162. };
  163. /* full speed support: */
  164. static struct usb_endpoint_descriptor fs_notify_desc = {
  165. .bLength = USB_DT_ENDPOINT_SIZE,
  166. .bDescriptorType = USB_DT_ENDPOINT,
  167. .bEndpointAddress = USB_DIR_IN,
  168. .bmAttributes = USB_ENDPOINT_XFER_INT,
  169. .wMaxPacketSize = cpu_to_le16(STATUS_BYTECOUNT),
  170. .bInterval = RNDIS_STATUS_INTERVAL_MS,
  171. };
  172. static struct usb_endpoint_descriptor fs_in_desc = {
  173. .bLength = USB_DT_ENDPOINT_SIZE,
  174. .bDescriptorType = USB_DT_ENDPOINT,
  175. .bEndpointAddress = USB_DIR_IN,
  176. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  177. };
  178. static struct usb_endpoint_descriptor fs_out_desc = {
  179. .bLength = USB_DT_ENDPOINT_SIZE,
  180. .bDescriptorType = USB_DT_ENDPOINT,
  181. .bEndpointAddress = USB_DIR_OUT,
  182. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  183. };
  184. static struct usb_descriptor_header *eth_fs_function[] = {
  185. (struct usb_descriptor_header *) &rndis_iad_descriptor,
  186. /* control interface matches ACM, not Ethernet */
  187. (struct usb_descriptor_header *) &rndis_control_intf,
  188. (struct usb_descriptor_header *) &header_desc,
  189. (struct usb_descriptor_header *) &call_mgmt_descriptor,
  190. (struct usb_descriptor_header *) &rndis_acm_descriptor,
  191. (struct usb_descriptor_header *) &rndis_union_desc,
  192. (struct usb_descriptor_header *) &fs_notify_desc,
  193. /* data interface has no altsetting */
  194. (struct usb_descriptor_header *) &rndis_data_intf,
  195. (struct usb_descriptor_header *) &fs_in_desc,
  196. (struct usb_descriptor_header *) &fs_out_desc,
  197. NULL,
  198. };
  199. /* high speed support: */
  200. static struct usb_endpoint_descriptor hs_notify_desc = {
  201. .bLength = USB_DT_ENDPOINT_SIZE,
  202. .bDescriptorType = USB_DT_ENDPOINT,
  203. .bEndpointAddress = USB_DIR_IN,
  204. .bmAttributes = USB_ENDPOINT_XFER_INT,
  205. .wMaxPacketSize = cpu_to_le16(STATUS_BYTECOUNT),
  206. .bInterval = USB_MS_TO_HS_INTERVAL(RNDIS_STATUS_INTERVAL_MS)
  207. };
  208. static struct usb_endpoint_descriptor hs_in_desc = {
  209. .bLength = USB_DT_ENDPOINT_SIZE,
  210. .bDescriptorType = USB_DT_ENDPOINT,
  211. .bEndpointAddress = USB_DIR_IN,
  212. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  213. .wMaxPacketSize = cpu_to_le16(512),
  214. };
  215. static struct usb_endpoint_descriptor hs_out_desc = {
  216. .bLength = USB_DT_ENDPOINT_SIZE,
  217. .bDescriptorType = USB_DT_ENDPOINT,
  218. .bEndpointAddress = USB_DIR_OUT,
  219. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  220. .wMaxPacketSize = cpu_to_le16(512),
  221. };
  222. static struct usb_descriptor_header *eth_hs_function[] = {
  223. (struct usb_descriptor_header *) &rndis_iad_descriptor,
  224. /* control interface matches ACM, not Ethernet */
  225. (struct usb_descriptor_header *) &rndis_control_intf,
  226. (struct usb_descriptor_header *) &header_desc,
  227. (struct usb_descriptor_header *) &call_mgmt_descriptor,
  228. (struct usb_descriptor_header *) &rndis_acm_descriptor,
  229. (struct usb_descriptor_header *) &rndis_union_desc,
  230. (struct usb_descriptor_header *) &hs_notify_desc,
  231. /* data interface has no altsetting */
  232. (struct usb_descriptor_header *) &rndis_data_intf,
  233. (struct usb_descriptor_header *) &hs_in_desc,
  234. (struct usb_descriptor_header *) &hs_out_desc,
  235. NULL,
  236. };
  237. /* super speed support: */
  238. static struct usb_endpoint_descriptor ss_notify_desc = {
  239. .bLength = USB_DT_ENDPOINT_SIZE,
  240. .bDescriptorType = USB_DT_ENDPOINT,
  241. .bEndpointAddress = USB_DIR_IN,
  242. .bmAttributes = USB_ENDPOINT_XFER_INT,
  243. .wMaxPacketSize = cpu_to_le16(STATUS_BYTECOUNT),
  244. .bInterval = USB_MS_TO_HS_INTERVAL(RNDIS_STATUS_INTERVAL_MS)
  245. };
  246. static struct usb_ss_ep_comp_descriptor ss_intr_comp_desc = {
  247. .bLength = sizeof ss_intr_comp_desc,
  248. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  249. /* the following 3 values can be tweaked if necessary */
  250. /* .bMaxBurst = 0, */
  251. /* .bmAttributes = 0, */
  252. .wBytesPerInterval = cpu_to_le16(STATUS_BYTECOUNT),
  253. };
  254. static struct usb_endpoint_descriptor ss_in_desc = {
  255. .bLength = USB_DT_ENDPOINT_SIZE,
  256. .bDescriptorType = USB_DT_ENDPOINT,
  257. .bEndpointAddress = USB_DIR_IN,
  258. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  259. .wMaxPacketSize = cpu_to_le16(1024),
  260. };
  261. static struct usb_endpoint_descriptor ss_out_desc = {
  262. .bLength = USB_DT_ENDPOINT_SIZE,
  263. .bDescriptorType = USB_DT_ENDPOINT,
  264. .bEndpointAddress = USB_DIR_OUT,
  265. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  266. .wMaxPacketSize = cpu_to_le16(1024),
  267. };
  268. static struct usb_ss_ep_comp_descriptor ss_bulk_comp_desc = {
  269. .bLength = sizeof ss_bulk_comp_desc,
  270. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  271. /* the following 2 values can be tweaked if necessary */
  272. /* .bMaxBurst = 0, */
  273. /* .bmAttributes = 0, */
  274. };
  275. static struct usb_descriptor_header *eth_ss_function[] = {
  276. (struct usb_descriptor_header *) &rndis_iad_descriptor,
  277. /* control interface matches ACM, not Ethernet */
  278. (struct usb_descriptor_header *) &rndis_control_intf,
  279. (struct usb_descriptor_header *) &header_desc,
  280. (struct usb_descriptor_header *) &call_mgmt_descriptor,
  281. (struct usb_descriptor_header *) &rndis_acm_descriptor,
  282. (struct usb_descriptor_header *) &rndis_union_desc,
  283. (struct usb_descriptor_header *) &ss_notify_desc,
  284. (struct usb_descriptor_header *) &ss_intr_comp_desc,
  285. /* data interface has no altsetting */
  286. (struct usb_descriptor_header *) &rndis_data_intf,
  287. (struct usb_descriptor_header *) &ss_in_desc,
  288. (struct usb_descriptor_header *) &ss_bulk_comp_desc,
  289. (struct usb_descriptor_header *) &ss_out_desc,
  290. (struct usb_descriptor_header *) &ss_bulk_comp_desc,
  291. NULL,
  292. };
  293. /* string descriptors: */
  294. static struct usb_string rndis_string_defs[] = {
  295. [0].s = "RNDIS Communications Control",
  296. [1].s = "RNDIS Ethernet Data",
  297. [2].s = "RNDIS",
  298. { } /* end of list */
  299. };
  300. static struct usb_gadget_strings rndis_string_table = {
  301. .language = 0x0409, /* en-us */
  302. .strings = rndis_string_defs,
  303. };
  304. static struct usb_gadget_strings *rndis_strings[] = {
  305. &rndis_string_table,
  306. NULL,
  307. };
  308. u32 rndis_test_last_resp_id = 0;
  309. /*-------------------------------------------------------------------------*/
  310. static struct sk_buff *rndis_add_header(struct gether *port,
  311. struct sk_buff *skb)
  312. {
  313. struct sk_buff *skb2;
  314. struct rndis_packet_msg_type *header = NULL;
  315. struct f_rndis *rndis = func_to_rndis(&port->func);
  316. if (rndis->port.multi_pkt_xfer) {
  317. if (port->header) {
  318. header = port->header;
  319. memset(header, 0, sizeof(*header));
  320. header->MessageType = cpu_to_le32(RNDIS_MSG_PACKET);
  321. header->MessageLength = cpu_to_le32(skb->len +
  322. sizeof(*header));
  323. header->DataOffset = cpu_to_le32(36);
  324. header->DataLength = cpu_to_le32(skb->len);
  325. pr_debug("MessageLength:%d DataLength:%d\n",
  326. header->MessageLength,
  327. header->DataLength);
  328. return skb;
  329. } else {
  330. pr_err("RNDIS header is NULL.\n");
  331. return NULL;
  332. }
  333. } else {
  334. skb2 = skb_realloc_headroom(skb,
  335. sizeof(struct rndis_packet_msg_type));
  336. if (skb2)
  337. rndis_add_hdr(skb2);
  338. dev_kfree_skb(skb);
  339. return skb2;
  340. }
  341. }
  342. static void rndis_response_available(void *_rndis)
  343. {
  344. struct f_rndis *rndis = _rndis;
  345. struct usb_request *req = rndis->notify_req;
  346. struct usb_composite_dev *cdev = rndis->port.func.config->cdev;
  347. __le32 *data = req->buf;
  348. int status;
  349. if (atomic_inc_return(&rndis->notify_count) != 1)
  350. return;
  351. /* Send RNDIS RESPONSE_AVAILABLE notification; a
  352. * USB_CDC_NOTIFY_RESPONSE_AVAILABLE "should" work too
  353. *
  354. * This is the only notification defined by RNDIS.
  355. */
  356. data[0] = cpu_to_le32(1);
  357. data[1] = cpu_to_le32(0);
  358. status = usb_ep_queue(rndis->notify, req, GFP_ATOMIC);
  359. if (status) {
  360. atomic_dec(&rndis->notify_count);
  361. DBG(cdev, "notify/0 --> %d\n", status);
  362. }
  363. }
  364. static void rndis_response_complete(struct usb_ep *ep, struct usb_request *req)
  365. {
  366. struct f_rndis *rndis = req->context;
  367. struct usb_composite_dev *cdev;
  368. int status = req->status;
  369. if (!rndis->port.func.config || !rndis->port.func.config->cdev)
  370. return;
  371. else
  372. cdev = rndis->port.func.config->cdev;
  373. /* after TX:
  374. * - USB_CDC_GET_ENCAPSULATED_RESPONSE (ep0/control)
  375. * - RNDIS_RESPONSE_AVAILABLE (status/irq)
  376. */
  377. switch (status) {
  378. case -ECONNRESET:
  379. case -ESHUTDOWN:
  380. /* connection gone */
  381. atomic_set(&rndis->notify_count, 0);
  382. break;
  383. default:
  384. DBG(cdev, "RNDIS %s response error %d, %d/%d\n",
  385. ep->name, status,
  386. req->actual, req->length);
  387. /* FALLTHROUGH */
  388. case 0:
  389. if (ep != rndis->notify)
  390. break;
  391. /* handle multiple pending RNDIS_RESPONSE_AVAILABLE
  392. * notifications by resending until we're done
  393. */
  394. if (atomic_dec_and_test(&rndis->notify_count))
  395. break;
  396. status = usb_ep_queue(rndis->notify, req, GFP_ATOMIC);
  397. if (status) {
  398. atomic_dec(&rndis->notify_count);
  399. DBG(cdev, "notify/1 --> %d\n", status);
  400. }
  401. break;
  402. }
  403. }
  404. static void rndis_command_complete(struct usb_ep *ep, struct usb_request *req)
  405. {
  406. struct f_rndis *rndis = req->context;
  407. struct usb_composite_dev *cdev;
  408. int status;
  409. rndis_init_msg_type *buf;
  410. if (!rndis->port.func.config || !rndis->port.func.config->cdev)
  411. return;
  412. else
  413. cdev = rndis->port.func.config->cdev;
  414. /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */
  415. // spin_lock(&dev->lock);
  416. status = rndis_msg_parser(rndis->config, (u8 *) req->buf);
  417. if (status < 0)
  418. pr_err("RNDIS command error %d, %d/%d\n",
  419. status, req->actual, req->length);
  420. buf = (rndis_init_msg_type *)req->buf;
  421. if (buf->MessageType == RNDIS_MSG_INIT) {
  422. if (buf->MaxTransferSize > 2048){
  423. rndis->port.multi_pkt_xfer = 1;
  424. rndis->port.dl_max_transfer_len = buf->MaxTransferSize;
  425. gether_update_dl_max_xfer_size(&rndis->port,
  426. rndis->port.dl_max_transfer_len);
  427. }else{
  428. rndis->port.multi_pkt_xfer = 0;
  429. }
  430. DBG(cdev, "%s: MaxTransferSize: %d : Multi_pkt_txr: %s\n",
  431. __func__, buf->MaxTransferSize,
  432. rndis->port.multi_pkt_xfer ? "enabled" :
  433. "disabled");
  434. if (rndis_dl_max_pkt_per_xfer <= 1)
  435. rndis->port.multi_pkt_xfer = 0;
  436. }
  437. // spin_unlock(&dev->lock);
  438. }
  439. static int
  440. rndis_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
  441. {
  442. struct f_rndis *rndis = func_to_rndis(f);
  443. struct usb_composite_dev *cdev = f->config->cdev;
  444. struct usb_request *req = cdev->req;
  445. int value = -EOPNOTSUPP;
  446. u16 w_index = le16_to_cpu(ctrl->wIndex);
  447. u16 w_value = le16_to_cpu(ctrl->wValue);
  448. u16 w_length = le16_to_cpu(ctrl->wLength);
  449. /* composite driver infrastructure handles everything except
  450. * CDC class messages; interface activation uses set_alt().
  451. */
  452. if (f_rndis_debug){
  453. pr_debug("rndis_setup , ctrl->bRequestType is 0x%x, ctrl->bRequest is 0x%x, w_index is 0x%x, w_value is 0x%x\n" ,
  454. ctrl->bRequestType , ctrl->bRequest, w_index, w_value) ;
  455. }
  456. switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
  457. /* RNDIS uses the CDC command encapsulation mechanism to implement
  458. * an RPC scheme, with much getting/setting of attributes by OID.
  459. */
  460. case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
  461. | USB_CDC_SEND_ENCAPSULATED_COMMAND:
  462. if (w_value || w_index != rndis->ctrl_id)
  463. goto invalid;
  464. /* read the request; process it later */
  465. value = w_length;
  466. req->complete = rndis_command_complete;
  467. req->context = rndis;
  468. /* later, rndis_response_available() sends a notification */
  469. break;
  470. case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
  471. | USB_CDC_GET_ENCAPSULATED_RESPONSE:
  472. if (w_value || w_index != rndis->ctrl_id)
  473. goto invalid;
  474. else {
  475. u8 *buf;
  476. u32 n;
  477. u32 MsgType, MsgLength, MsgID;
  478. __le32 *tmp;
  479. /* return the result */
  480. buf = rndis_get_next_response(rndis->config, &n);
  481. if (buf) {
  482. memcpy(req->buf, buf, n);
  483. req->complete = rndis_response_complete;
  484. req->context = rndis;
  485. tmp = (__le32 *)buf;
  486. MsgType = get_unaligned_le32(tmp++);
  487. MsgLength = get_unaligned_le32(tmp++);
  488. MsgID = get_unaligned_le32(tmp++);
  489. rndis_free_response(rndis->config, buf);
  490. value = n;
  491. if (f_rndis_debug){
  492. pr_debug("rndis_setup, rndis response MsgLength %d, msg type is 0x%x, RequestID is 0x%x\n", MsgLength, MsgType, MsgID);
  493. }
  494. rndis_test_last_resp_id = MsgID ;
  495. }
  496. /* else stalls ... spec says to avoid that */
  497. }
  498. break;
  499. default:
  500. invalid:
  501. pr_err("invalid control req%02x.%02x v%04x i%04x l%d\n",
  502. ctrl->bRequestType, ctrl->bRequest,
  503. w_value, w_index, w_length);
  504. VDBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
  505. ctrl->bRequestType, ctrl->bRequest,
  506. w_value, w_index, w_length);
  507. }
  508. /* respond with data transfer or status phase? */
  509. if (value >= 0) {
  510. DBG(cdev, "rndis req%02x.%02x v%04x i%04x l%d\n",
  511. ctrl->bRequestType, ctrl->bRequest,
  512. w_value, w_index, w_length);
  513. req->zero = (value < w_length);
  514. req->length = value;
  515. value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
  516. if (value < 0)
  517. ERROR(cdev, "rndis response on err %d\n", value);
  518. }
  519. /* device either stalls (value < 0) or reports success */
  520. return value;
  521. }
  522. static int rndis_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
  523. {
  524. struct f_rndis *rndis = func_to_rndis(f);
  525. struct usb_composite_dev *cdev = f->config->cdev;
  526. /* we know alt == 0 */
  527. printk( F_RNDIS_LOG "rndis_set_alt - interface : %d, rndis ctrl id : %d, rndis data id : %d\n" ,
  528. intf, rndis->ctrl_id, rndis->data_id);
  529. if (intf == rndis->ctrl_id) {
  530. if (rndis->notify->driver_data) {
  531. VDBG(cdev, "reset rndis control %d\n", intf);
  532. printk( F_RNDIS_LOG "reset rndis control %d\n", intf);
  533. usb_ep_disable(rndis->notify);
  534. }
  535. if (!rndis->notify->desc) {
  536. VDBG(cdev, "init rndis ctrl %d\n", intf);
  537. printk( F_RNDIS_LOG "init rndis ctrl %d\n", intf);
  538. if (config_ep_by_speed(cdev->gadget, f, rndis->notify))
  539. goto fail;
  540. }
  541. usb_ep_enable(rndis->notify);
  542. rndis->notify->driver_data = rndis;
  543. } else if (intf == rndis->data_id) {
  544. struct net_device *net;
  545. if (rndis->port.in_ep->driver_data) {
  546. DBG(cdev, "reset rndis\n");
  547. printk( F_RNDIS_LOG "reset rndis\n");
  548. gether_disconnect(&rndis->port);
  549. }
  550. if (!rndis->port.in_ep->desc || !rndis->port.out_ep->desc) {
  551. DBG(cdev, "init rndis\n");
  552. printk( F_RNDIS_LOG "init rndis\n");
  553. if (config_ep_by_speed(cdev->gadget, f,
  554. rndis->port.in_ep) ||
  555. config_ep_by_speed(cdev->gadget, f,
  556. rndis->port.out_ep)) {
  557. rndis->port.in_ep->desc = NULL;
  558. rndis->port.out_ep->desc = NULL;
  559. goto fail;
  560. }
  561. }
  562. /* Avoid ZLPs; they can be troublesome. */
  563. rndis->port.is_zlp_ok = false;
  564. /* RNDIS should be in the "RNDIS uninitialized" state,
  565. * either never activated or after rndis_uninit().
  566. *
  567. * We don't want data to flow here until a nonzero packet
  568. * filter is set, at which point it enters "RNDIS data
  569. * initialized" state ... but we do want the endpoints
  570. * to be activated. It's a strange little state.
  571. *
  572. * REVISIT the RNDIS gadget code has done this wrong for a
  573. * very long time. We need another call to the link layer
  574. * code -- gether_updown(...bool) maybe -- to do it right.
  575. */
  576. rndis->port.cdc_filter = 0;
  577. DBG(cdev, "RNDIS RX/TX early activation ... \n");
  578. net = gether_connect(&rndis->port);
  579. if (IS_ERR(net))
  580. return PTR_ERR(net);
  581. rndis_set_param_dev(rndis->config, net,
  582. &rndis->port.cdc_filter);
  583. } else
  584. goto fail;
  585. return 0;
  586. fail:
  587. return -EINVAL;
  588. }
  589. static void rndis_disable(struct usb_function *f)
  590. {
  591. struct f_rndis *rndis = func_to_rndis(f);
  592. struct usb_composite_dev *cdev = f->config->cdev;
  593. if (!rndis->notify->driver_data)
  594. return;
  595. DBG(cdev, "rndis deactivated\n");
  596. printk( F_RNDIS_LOG "rndis deactivated\n");
  597. rndis_uninit(rndis->config);
  598. gether_disconnect(&rndis->port);
  599. usb_ep_disable(rndis->notify);
  600. rndis->notify->driver_data = NULL;
  601. }
  602. /*-------------------------------------------------------------------------*/
  603. /*
  604. * This isn't quite the same mechanism as CDC Ethernet, since the
  605. * notification scheme passes less data, but the same set of link
  606. * states must be tested. A key difference is that altsettings are
  607. * not used to tell whether the link should send packets or not.
  608. */
  609. static void rndis_open(struct gether *geth)
  610. {
  611. struct f_rndis *rndis = func_to_rndis(&geth->func);
  612. struct usb_composite_dev *cdev = geth->func.config->cdev;
  613. DBG(cdev, "%s\n", __func__);
  614. printk( F_RNDIS_LOG "%s\n", __func__);
  615. rndis_set_param_medium(rndis->config, RNDIS_MEDIUM_802_3,
  616. bitrate(cdev->gadget) / 100);
  617. rndis_signal_connect(rndis->config);
  618. }
  619. static void rndis_close(struct gether *geth)
  620. {
  621. struct f_rndis *rndis = func_to_rndis(&geth->func);
  622. DBG(geth->func.config->cdev, "%s\n", __func__);
  623. printk( F_RNDIS_LOG "%s\n", __func__);
  624. rndis_set_param_medium(rndis->config, RNDIS_MEDIUM_802_3, 0);
  625. rndis_signal_disconnect(rndis->config);
  626. }
  627. /*-------------------------------------------------------------------------*/
  628. /* ethernet function driver setup/binding */
  629. static int
  630. rndis_bind(struct usb_configuration *c, struct usb_function *f)
  631. {
  632. struct usb_composite_dev *cdev = c->cdev;
  633. struct f_rndis *rndis = func_to_rndis(f);
  634. int status;
  635. struct usb_ep *ep;
  636. printk( F_RNDIS_LOG \
  637. "%s: rndis_bind begin \n", \
  638. __func__);
  639. /* allocate instance-specific interface IDs */
  640. status = usb_interface_id(c, f);
  641. if (status < 0)
  642. goto fail;
  643. rndis->ctrl_id = status;
  644. rndis_iad_descriptor.bFirstInterface = status;
  645. rndis_control_intf.bInterfaceNumber = status;
  646. rndis_union_desc.bMasterInterface0 = status;
  647. status = usb_interface_id(c, f);
  648. if (status < 0)
  649. goto fail;
  650. rndis->data_id = status;
  651. rndis_data_intf.bInterfaceNumber = status;
  652. rndis_union_desc.bSlaveInterface0 = status;
  653. status = -ENODEV;
  654. /* allocate instance-specific endpoints */
  655. ep = usb_ep_autoconfig(cdev->gadget, &fs_in_desc);
  656. if (!ep)
  657. goto fail;
  658. rndis->port.in_ep = ep;
  659. ep->driver_data = cdev; /* claim */
  660. ep = usb_ep_autoconfig(cdev->gadget, &fs_out_desc);
  661. if (!ep)
  662. goto fail;
  663. rndis->port.out_ep = ep;
  664. ep->driver_data = cdev; /* claim */
  665. /* NOTE: a status/notification endpoint is, strictly speaking,
  666. * optional. We don't treat it that way though! It's simpler,
  667. * and some newer profiles don't treat it as optional.
  668. */
  669. ep = usb_ep_autoconfig(cdev->gadget, &fs_notify_desc);
  670. if (!ep)
  671. goto fail;
  672. rndis->notify = ep;
  673. ep->driver_data = cdev; /* claim */
  674. status = -ENOMEM;
  675. /* allocate notification request and buffer */
  676. rndis->notify_req = usb_ep_alloc_request(ep, GFP_KERNEL);
  677. if (!rndis->notify_req)
  678. goto fail;
  679. #if defined(CONFIG_64BIT) && defined(CONFIG_MTK_LM_MODE)
  680. rndis->notify_req->buf = kmalloc(STATUS_BYTECOUNT, GFP_KERNEL | GFP_DMA);
  681. #else
  682. rndis->notify_req->buf = kmalloc(STATUS_BYTECOUNT, GFP_KERNEL);
  683. #endif
  684. if (!rndis->notify_req->buf)
  685. goto fail;
  686. rndis->notify_req->length = STATUS_BYTECOUNT;
  687. rndis->notify_req->context = rndis;
  688. rndis->notify_req->complete = rndis_response_complete;
  689. /* support all relevant hardware speeds... we expect that when
  690. * hardware is dual speed, all bulk-capable endpoints work at
  691. * both speeds
  692. */
  693. hs_in_desc.bEndpointAddress = fs_in_desc.bEndpointAddress;
  694. hs_out_desc.bEndpointAddress = fs_out_desc.bEndpointAddress;
  695. hs_notify_desc.bEndpointAddress = fs_notify_desc.bEndpointAddress;
  696. ss_in_desc.bEndpointAddress = fs_in_desc.bEndpointAddress;
  697. ss_out_desc.bEndpointAddress = fs_out_desc.bEndpointAddress;
  698. ss_notify_desc.bEndpointAddress = fs_notify_desc.bEndpointAddress;
  699. status = usb_assign_descriptors(f, eth_fs_function, eth_hs_function,
  700. eth_ss_function);
  701. if (status)
  702. goto fail;
  703. rndis->port.open = rndis_open;
  704. rndis->port.close = rndis_close;
  705. status = rndis_register(rndis_response_available, rndis);
  706. if (status < 0)
  707. goto fail;
  708. rndis->config = status;
  709. rndis_set_param_medium(rndis->config, RNDIS_MEDIUM_802_3, 0);
  710. rndis_set_host_mac(rndis->config, rndis->ethaddr);
  711. rndis_set_max_pkt_xfer(rndis->config, rndis_ul_max_pkt_per_xfer);
  712. if (rndis->manufacturer && rndis->vendorID &&
  713. rndis_set_param_vendor(rndis->config, rndis->vendorID,
  714. rndis->manufacturer))
  715. goto fail;
  716. /* NOTE: all that is done without knowing or caring about
  717. * the network link ... which is unavailable to this code
  718. * until we're activated via set_alt().
  719. */
  720. printk( F_RNDIS_LOG
  721. "RNDIS: %s speed IN/%s OUT/%s NOTIFY/%s\n",
  722. gadget_is_superspeed(c->cdev->gadget) ? "super" :
  723. gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
  724. rndis->port.in_ep->name, rndis->port.out_ep->name,
  725. rndis->notify->name);
  726. DBG(cdev, "RNDIS: %s speed IN/%s OUT/%s NOTIFY/%s\n",
  727. gadget_is_superspeed(c->cdev->gadget) ? "super" :
  728. gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
  729. rndis->port.in_ep->name, rndis->port.out_ep->name,
  730. rndis->notify->name);
  731. return 0;
  732. fail:
  733. usb_free_all_descriptors(f);
  734. if (rndis->notify_req) {
  735. kfree(rndis->notify_req->buf);
  736. usb_ep_free_request(rndis->notify, rndis->notify_req);
  737. }
  738. /* we might as well release our claims on endpoints */
  739. if (rndis->notify)
  740. rndis->notify->driver_data = NULL;
  741. if (rndis->port.out_ep)
  742. rndis->port.out_ep->driver_data = NULL;
  743. if (rndis->port.in_ep)
  744. rndis->port.in_ep->driver_data = NULL;
  745. ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
  746. return status;
  747. }
  748. static void
  749. rndis_unbind(struct usb_configuration *c, struct usb_function *f)
  750. {
  751. struct f_rndis *rndis = func_to_rndis(f);
  752. printk( F_RNDIS_LOG \
  753. "%s: rndis_unbind \n", \
  754. __func__);
  755. rndis_deregister(rndis->config);
  756. rndis_exit();
  757. rndis_string_defs[0].id = 0;
  758. usb_free_all_descriptors(f);
  759. kfree(rndis->notify_req->buf);
  760. usb_ep_free_request(rndis->notify, rndis->notify_req);
  761. kfree(rndis);
  762. }
  763. /* Some controllers can't support RNDIS ... */
  764. static inline bool can_support_rndis(struct usb_configuration *c)
  765. {
  766. /* everything else is *presumably* fine */
  767. return true;
  768. }
  769. int
  770. rndis_bind_config_vendor(struct usb_configuration *c, u8 ethaddr[ETH_ALEN],
  771. u32 vendorID, const char *manufacturer, struct eth_dev *dev)
  772. {
  773. struct f_rndis *rndis;
  774. int status;
  775. if (!can_support_rndis(c) || !ethaddr)
  776. return -EINVAL;
  777. /* setup RNDIS itself */
  778. status = rndis_init();
  779. if (status < 0)
  780. return status;
  781. if (rndis_string_defs[0].id == 0) {
  782. status = usb_string_ids_tab(c->cdev, rndis_string_defs);
  783. if (status)
  784. return status;
  785. rndis_control_intf.iInterface = rndis_string_defs[0].id;
  786. rndis_data_intf.iInterface = rndis_string_defs[1].id;
  787. rndis_iad_descriptor.iFunction = rndis_string_defs[2].id;
  788. }
  789. /* allocate and initialize one new instance */
  790. status = -ENOMEM;
  791. rndis = kzalloc(sizeof *rndis, GFP_KERNEL);
  792. if (!rndis)
  793. goto fail;
  794. memcpy(rndis->ethaddr, ethaddr, ETH_ALEN);
  795. rndis->vendorID = vendorID;
  796. rndis->manufacturer = manufacturer;
  797. rndis->port.ioport = dev;
  798. /* RNDIS activates when the host changes this filter */
  799. rndis->port.cdc_filter = 0;
  800. /* RNDIS has special (and complex) framing */
  801. rndis->port.header_len = sizeof(struct rndis_packet_msg_type);
  802. rndis->port.wrap = rndis_add_header;
  803. rndis->port.unwrap = rndis_rm_hdr;
  804. rndis->port.ul_max_pkts_per_xfer = rndis_ul_max_pkt_per_xfer;
  805. rndis->port.dl_max_pkts_per_xfer = rndis_dl_max_pkt_per_xfer;
  806. rndis->port.func.name = "rndis";
  807. rndis->port.func.strings = rndis_strings;
  808. /* descriptors are per-instance copies */
  809. rndis->port.func.bind = rndis_bind;
  810. rndis->port.func.unbind = rndis_unbind;
  811. rndis->port.func.set_alt = rndis_set_alt;
  812. rndis->port.func.setup = rndis_setup;
  813. rndis->port.func.disable = rndis_disable;
  814. status = usb_add_function(c, &rndis->port.func);
  815. if (status) {
  816. kfree(rndis);
  817. fail:
  818. rndis_exit();
  819. }
  820. printk( F_RNDIS_LOG \
  821. "%s: rndis_bind_config_vendor done, status is %d \n", \
  822. __func__, status);
  823. return status;
  824. }