f_rndis.c 29 KB

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