f_rndis.c 29 KB

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