f_rndis.c 28 KB

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