f_rndis.c 29 KB

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