f_rndis.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  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. struct rndis_params *params;
  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. if (!skb)
  308. return NULL;
  309. skb2 = skb_realloc_headroom(skb, sizeof(struct rndis_packet_msg_type));
  310. rndis_add_hdr(skb2);
  311. dev_kfree_skb(skb);
  312. return skb2;
  313. }
  314. static void rndis_response_available(void *_rndis)
  315. {
  316. struct f_rndis *rndis = _rndis;
  317. struct usb_request *req = rndis->notify_req;
  318. struct usb_composite_dev *cdev = rndis->port.func.config->cdev;
  319. __le32 *data = req->buf;
  320. int status;
  321. if (atomic_inc_return(&rndis->notify_count) != 1)
  322. return;
  323. /* Send RNDIS RESPONSE_AVAILABLE notification; a
  324. * USB_CDC_NOTIFY_RESPONSE_AVAILABLE "should" work too
  325. *
  326. * This is the only notification defined by RNDIS.
  327. */
  328. data[0] = cpu_to_le32(1);
  329. data[1] = cpu_to_le32(0);
  330. status = usb_ep_queue(rndis->notify, req, GFP_ATOMIC);
  331. if (status) {
  332. atomic_dec(&rndis->notify_count);
  333. DBG(cdev, "notify/0 --> %d\n", status);
  334. }
  335. }
  336. static void rndis_response_complete(struct usb_ep *ep, struct usb_request *req)
  337. {
  338. struct f_rndis *rndis = req->context;
  339. struct usb_composite_dev *cdev = rndis->port.func.config->cdev;
  340. int status = req->status;
  341. /* after TX:
  342. * - USB_CDC_GET_ENCAPSULATED_RESPONSE (ep0/control)
  343. * - RNDIS_RESPONSE_AVAILABLE (status/irq)
  344. */
  345. switch (status) {
  346. case -ECONNRESET:
  347. case -ESHUTDOWN:
  348. /* connection gone */
  349. atomic_set(&rndis->notify_count, 0);
  350. break;
  351. default:
  352. DBG(cdev, "RNDIS %s response error %d, %d/%d\n",
  353. ep->name, status,
  354. req->actual, req->length);
  355. /* FALLTHROUGH */
  356. case 0:
  357. if (ep != rndis->notify)
  358. break;
  359. /* handle multiple pending RNDIS_RESPONSE_AVAILABLE
  360. * notifications by resending until we're done
  361. */
  362. if (atomic_dec_and_test(&rndis->notify_count))
  363. break;
  364. status = usb_ep_queue(rndis->notify, req, GFP_ATOMIC);
  365. if (status) {
  366. atomic_dec(&rndis->notify_count);
  367. DBG(cdev, "notify/1 --> %d\n", status);
  368. }
  369. break;
  370. }
  371. }
  372. static void rndis_command_complete(struct usb_ep *ep, struct usb_request *req)
  373. {
  374. struct f_rndis *rndis = req->context;
  375. int status;
  376. /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */
  377. // spin_lock(&dev->lock);
  378. status = rndis_msg_parser(rndis->params, (u8 *) req->buf);
  379. if (status < 0)
  380. pr_err("RNDIS command error %d, %d/%d\n",
  381. status, req->actual, req->length);
  382. // spin_unlock(&dev->lock);
  383. }
  384. static int
  385. rndis_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
  386. {
  387. struct f_rndis *rndis = func_to_rndis(f);
  388. struct usb_composite_dev *cdev = f->config->cdev;
  389. struct usb_request *req = cdev->req;
  390. int value = -EOPNOTSUPP;
  391. u16 w_index = le16_to_cpu(ctrl->wIndex);
  392. u16 w_value = le16_to_cpu(ctrl->wValue);
  393. u16 w_length = le16_to_cpu(ctrl->wLength);
  394. /* composite driver infrastructure handles everything except
  395. * CDC class messages; interface activation uses set_alt().
  396. */
  397. switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
  398. /* RNDIS uses the CDC command encapsulation mechanism to implement
  399. * an RPC scheme, with much getting/setting of attributes by OID.
  400. */
  401. case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
  402. | USB_CDC_SEND_ENCAPSULATED_COMMAND:
  403. if (w_value || w_index != rndis->ctrl_id)
  404. goto invalid;
  405. /* read the request; process it later */
  406. value = w_length;
  407. req->complete = rndis_command_complete;
  408. req->context = rndis;
  409. /* later, rndis_response_available() sends a notification */
  410. break;
  411. case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
  412. | USB_CDC_GET_ENCAPSULATED_RESPONSE:
  413. if (w_value || w_index != rndis->ctrl_id)
  414. goto invalid;
  415. else {
  416. u8 *buf;
  417. u32 n;
  418. /* return the result */
  419. buf = rndis_get_next_response(rndis->params, &n);
  420. if (buf) {
  421. memcpy(req->buf, buf, n);
  422. req->complete = rndis_response_complete;
  423. req->context = rndis;
  424. rndis_free_response(rndis->params, buf);
  425. value = n;
  426. }
  427. /* else stalls ... spec says to avoid that */
  428. }
  429. break;
  430. default:
  431. invalid:
  432. VDBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
  433. ctrl->bRequestType, ctrl->bRequest,
  434. w_value, w_index, w_length);
  435. }
  436. /* respond with data transfer or status phase? */
  437. if (value >= 0) {
  438. DBG(cdev, "rndis req%02x.%02x v%04x i%04x l%d\n",
  439. ctrl->bRequestType, ctrl->bRequest,
  440. w_value, w_index, w_length);
  441. req->zero = (value < w_length);
  442. req->length = value;
  443. value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
  444. if (value < 0)
  445. ERROR(cdev, "rndis response on err %d\n", value);
  446. }
  447. /* device either stalls (value < 0) or reports success */
  448. return value;
  449. }
  450. static int rndis_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
  451. {
  452. struct f_rndis *rndis = func_to_rndis(f);
  453. struct usb_composite_dev *cdev = f->config->cdev;
  454. /* we know alt == 0 */
  455. if (intf == rndis->ctrl_id) {
  456. VDBG(cdev, "reset rndis control %d\n", intf);
  457. usb_ep_disable(rndis->notify);
  458. if (!rndis->notify->desc) {
  459. VDBG(cdev, "init rndis ctrl %d\n", intf);
  460. if (config_ep_by_speed(cdev->gadget, f, rndis->notify))
  461. goto fail;
  462. }
  463. usb_ep_enable(rndis->notify);
  464. } else if (intf == rndis->data_id) {
  465. struct net_device *net;
  466. if (rndis->port.in_ep->enabled) {
  467. DBG(cdev, "reset rndis\n");
  468. gether_disconnect(&rndis->port);
  469. }
  470. if (!rndis->port.in_ep->desc || !rndis->port.out_ep->desc) {
  471. DBG(cdev, "init rndis\n");
  472. if (config_ep_by_speed(cdev->gadget, f,
  473. rndis->port.in_ep) ||
  474. config_ep_by_speed(cdev->gadget, f,
  475. rndis->port.out_ep)) {
  476. rndis->port.in_ep->desc = NULL;
  477. rndis->port.out_ep->desc = NULL;
  478. goto fail;
  479. }
  480. }
  481. /* Avoid ZLPs; they can be troublesome. */
  482. rndis->port.is_zlp_ok = false;
  483. /* RNDIS should be in the "RNDIS uninitialized" state,
  484. * either never activated or after rndis_uninit().
  485. *
  486. * We don't want data to flow here until a nonzero packet
  487. * filter is set, at which point it enters "RNDIS data
  488. * initialized" state ... but we do want the endpoints
  489. * to be activated. It's a strange little state.
  490. *
  491. * REVISIT the RNDIS gadget code has done this wrong for a
  492. * very long time. We need another call to the link layer
  493. * code -- gether_updown(...bool) maybe -- to do it right.
  494. */
  495. rndis->port.cdc_filter = 0;
  496. DBG(cdev, "RNDIS RX/TX early activation ... \n");
  497. net = gether_connect(&rndis->port);
  498. if (IS_ERR(net))
  499. return PTR_ERR(net);
  500. rndis_set_param_dev(rndis->params, net,
  501. &rndis->port.cdc_filter);
  502. } else
  503. goto fail;
  504. return 0;
  505. fail:
  506. return -EINVAL;
  507. }
  508. static void rndis_disable(struct usb_function *f)
  509. {
  510. struct f_rndis *rndis = func_to_rndis(f);
  511. struct usb_composite_dev *cdev = f->config->cdev;
  512. if (!rndis->notify->enabled)
  513. return;
  514. DBG(cdev, "rndis deactivated\n");
  515. rndis_uninit(rndis->params);
  516. gether_disconnect(&rndis->port);
  517. usb_ep_disable(rndis->notify);
  518. }
  519. /*-------------------------------------------------------------------------*/
  520. /*
  521. * This isn't quite the same mechanism as CDC Ethernet, since the
  522. * notification scheme passes less data, but the same set of link
  523. * states must be tested. A key difference is that altsettings are
  524. * not used to tell whether the link should send packets or not.
  525. */
  526. static void rndis_open(struct gether *geth)
  527. {
  528. struct f_rndis *rndis = func_to_rndis(&geth->func);
  529. struct usb_composite_dev *cdev = geth->func.config->cdev;
  530. DBG(cdev, "%s\n", __func__);
  531. rndis_set_param_medium(rndis->params, RNDIS_MEDIUM_802_3,
  532. bitrate(cdev->gadget) / 100);
  533. rndis_signal_connect(rndis->params);
  534. }
  535. static void rndis_close(struct gether *geth)
  536. {
  537. struct f_rndis *rndis = func_to_rndis(&geth->func);
  538. DBG(geth->func.config->cdev, "%s\n", __func__);
  539. rndis_set_param_medium(rndis->params, RNDIS_MEDIUM_802_3, 0);
  540. rndis_signal_disconnect(rndis->params);
  541. }
  542. /*-------------------------------------------------------------------------*/
  543. /* Some controllers can't support RNDIS ... */
  544. static inline bool can_support_rndis(struct usb_configuration *c)
  545. {
  546. /* everything else is *presumably* fine */
  547. return true;
  548. }
  549. /* ethernet function driver setup/binding */
  550. static int
  551. rndis_bind(struct usb_configuration *c, struct usb_function *f)
  552. {
  553. struct usb_composite_dev *cdev = c->cdev;
  554. struct f_rndis *rndis = func_to_rndis(f);
  555. struct usb_string *us;
  556. int status;
  557. struct usb_ep *ep;
  558. struct f_rndis_opts *rndis_opts;
  559. if (!can_support_rndis(c))
  560. return -EINVAL;
  561. rndis_opts = container_of(f->fi, struct f_rndis_opts, func_inst);
  562. if (cdev->use_os_string) {
  563. f->os_desc_table = kzalloc(sizeof(*f->os_desc_table),
  564. GFP_KERNEL);
  565. if (!f->os_desc_table)
  566. return -ENOMEM;
  567. f->os_desc_n = 1;
  568. f->os_desc_table[0].os_desc = &rndis_opts->rndis_os_desc;
  569. }
  570. rndis_iad_descriptor.bFunctionClass = rndis_opts->class;
  571. rndis_iad_descriptor.bFunctionSubClass = rndis_opts->subclass;
  572. rndis_iad_descriptor.bFunctionProtocol = rndis_opts->protocol;
  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 = usb_ep_autoconfig(cdev->gadget, &fs_out_desc);
  620. if (!ep)
  621. goto fail;
  622. rndis->port.out_ep = ep;
  623. /* NOTE: a status/notification endpoint is, strictly speaking,
  624. * optional. We don't treat it that way though! It's simpler,
  625. * and some newer profiles don't treat it as optional.
  626. */
  627. ep = usb_ep_autoconfig(cdev->gadget, &fs_notify_desc);
  628. if (!ep)
  629. goto fail;
  630. rndis->notify = ep;
  631. status = -ENOMEM;
  632. /* allocate notification request and buffer */
  633. rndis->notify_req = usb_ep_alloc_request(ep, GFP_KERNEL);
  634. if (!rndis->notify_req)
  635. goto fail;
  636. rndis->notify_req->buf = kmalloc(STATUS_BYTECOUNT, GFP_KERNEL);
  637. if (!rndis->notify_req->buf)
  638. goto fail;
  639. rndis->notify_req->length = STATUS_BYTECOUNT;
  640. rndis->notify_req->context = rndis;
  641. rndis->notify_req->complete = rndis_response_complete;
  642. /* support all relevant hardware speeds... we expect that when
  643. * hardware is dual speed, all bulk-capable endpoints work at
  644. * both speeds
  645. */
  646. hs_in_desc.bEndpointAddress = fs_in_desc.bEndpointAddress;
  647. hs_out_desc.bEndpointAddress = fs_out_desc.bEndpointAddress;
  648. hs_notify_desc.bEndpointAddress = fs_notify_desc.bEndpointAddress;
  649. ss_in_desc.bEndpointAddress = fs_in_desc.bEndpointAddress;
  650. ss_out_desc.bEndpointAddress = fs_out_desc.bEndpointAddress;
  651. ss_notify_desc.bEndpointAddress = fs_notify_desc.bEndpointAddress;
  652. status = usb_assign_descriptors(f, eth_fs_function, eth_hs_function,
  653. eth_ss_function, NULL);
  654. if (status)
  655. goto fail;
  656. rndis->port.open = rndis_open;
  657. rndis->port.close = rndis_close;
  658. rndis_set_param_medium(rndis->params, RNDIS_MEDIUM_802_3, 0);
  659. rndis_set_host_mac(rndis->params, rndis->ethaddr);
  660. if (rndis->manufacturer && rndis->vendorID &&
  661. rndis_set_param_vendor(rndis->params, rndis->vendorID,
  662. rndis->manufacturer)) {
  663. status = -EINVAL;
  664. goto fail_free_descs;
  665. }
  666. /* NOTE: all that is done without knowing or caring about
  667. * the network link ... which is unavailable to this code
  668. * until we're activated via set_alt().
  669. */
  670. DBG(cdev, "RNDIS: %s speed IN/%s OUT/%s NOTIFY/%s\n",
  671. gadget_is_superspeed(c->cdev->gadget) ? "super" :
  672. gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
  673. rndis->port.in_ep->name, rndis->port.out_ep->name,
  674. rndis->notify->name);
  675. return 0;
  676. fail_free_descs:
  677. usb_free_all_descriptors(f);
  678. fail:
  679. kfree(f->os_desc_table);
  680. f->os_desc_n = 0;
  681. if (rndis->notify_req) {
  682. kfree(rndis->notify_req->buf);
  683. usb_ep_free_request(rndis->notify, rndis->notify_req);
  684. }
  685. ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
  686. return status;
  687. }
  688. void rndis_borrow_net(struct usb_function_instance *f, struct net_device *net)
  689. {
  690. struct f_rndis_opts *opts;
  691. opts = container_of(f, struct f_rndis_opts, func_inst);
  692. if (opts->bound)
  693. gether_cleanup(netdev_priv(opts->net));
  694. else
  695. free_netdev(opts->net);
  696. opts->borrowed_net = opts->bound = true;
  697. opts->net = net;
  698. }
  699. EXPORT_SYMBOL_GPL(rndis_borrow_net);
  700. static inline struct f_rndis_opts *to_f_rndis_opts(struct config_item *item)
  701. {
  702. return container_of(to_config_group(item), struct f_rndis_opts,
  703. func_inst.group);
  704. }
  705. /* f_rndis_item_ops */
  706. USB_ETHERNET_CONFIGFS_ITEM(rndis);
  707. /* f_rndis_opts_dev_addr */
  708. USB_ETHERNET_CONFIGFS_ITEM_ATTR_DEV_ADDR(rndis);
  709. /* f_rndis_opts_host_addr */
  710. USB_ETHERNET_CONFIGFS_ITEM_ATTR_HOST_ADDR(rndis);
  711. /* f_rndis_opts_qmult */
  712. USB_ETHERNET_CONFIGFS_ITEM_ATTR_QMULT(rndis);
  713. /* f_rndis_opts_ifname */
  714. USB_ETHERNET_CONFIGFS_ITEM_ATTR_IFNAME(rndis);
  715. /* f_rndis_opts_class */
  716. USB_ETHER_CONFIGFS_ITEM_ATTR_U8_RW(rndis, class);
  717. /* f_rndis_opts_subclass */
  718. USB_ETHER_CONFIGFS_ITEM_ATTR_U8_RW(rndis, subclass);
  719. /* f_rndis_opts_protocol */
  720. USB_ETHER_CONFIGFS_ITEM_ATTR_U8_RW(rndis, protocol);
  721. static struct configfs_attribute *rndis_attrs[] = {
  722. &rndis_opts_attr_dev_addr,
  723. &rndis_opts_attr_host_addr,
  724. &rndis_opts_attr_qmult,
  725. &rndis_opts_attr_ifname,
  726. &rndis_opts_attr_class,
  727. &rndis_opts_attr_subclass,
  728. &rndis_opts_attr_protocol,
  729. NULL,
  730. };
  731. static struct config_item_type rndis_func_type = {
  732. .ct_item_ops = &rndis_item_ops,
  733. .ct_attrs = rndis_attrs,
  734. .ct_owner = THIS_MODULE,
  735. };
  736. static void rndis_free_inst(struct usb_function_instance *f)
  737. {
  738. struct f_rndis_opts *opts;
  739. opts = container_of(f, struct f_rndis_opts, func_inst);
  740. if (!opts->borrowed_net) {
  741. if (opts->bound)
  742. gether_cleanup(netdev_priv(opts->net));
  743. else
  744. free_netdev(opts->net);
  745. }
  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. opts = kzalloc(sizeof(*opts), GFP_KERNEL);
  754. if (!opts)
  755. return ERR_PTR(-ENOMEM);
  756. opts->rndis_os_desc.ext_compat_id = opts->rndis_ext_compat_id;
  757. mutex_init(&opts->lock);
  758. opts->func_inst.free_func_inst = rndis_free_inst;
  759. opts->net = gether_setup_default();
  760. if (IS_ERR(opts->net)) {
  761. struct net_device *net = opts->net;
  762. kfree(opts);
  763. return ERR_CAST(net);
  764. }
  765. INIT_LIST_HEAD(&opts->rndis_os_desc.ext_prop);
  766. opts->class = rndis_iad_descriptor.bFunctionClass;
  767. opts->subclass = rndis_iad_descriptor.bFunctionSubClass;
  768. opts->protocol = rndis_iad_descriptor.bFunctionProtocol;
  769. descs[0] = &opts->rndis_os_desc;
  770. names[0] = "rndis";
  771. config_group_init_type_name(&opts->func_inst.group, "",
  772. &rndis_func_type);
  773. usb_os_desc_prepare_interf_dir(&opts->func_inst.group, 1, descs,
  774. names, THIS_MODULE);
  775. return &opts->func_inst;
  776. }
  777. static void rndis_free(struct usb_function *f)
  778. {
  779. struct f_rndis *rndis;
  780. struct f_rndis_opts *opts;
  781. rndis = func_to_rndis(f);
  782. rndis_deregister(rndis->params);
  783. opts = container_of(f->fi, struct f_rndis_opts, func_inst);
  784. kfree(rndis);
  785. mutex_lock(&opts->lock);
  786. opts->refcnt--;
  787. mutex_unlock(&opts->lock);
  788. }
  789. static void rndis_unbind(struct usb_configuration *c, struct usb_function *f)
  790. {
  791. struct f_rndis *rndis = func_to_rndis(f);
  792. kfree(f->os_desc_table);
  793. f->os_desc_n = 0;
  794. usb_free_all_descriptors(f);
  795. kfree(rndis->notify_req->buf);
  796. usb_ep_free_request(rndis->notify, rndis->notify_req);
  797. }
  798. static struct usb_function *rndis_alloc(struct usb_function_instance *fi)
  799. {
  800. struct f_rndis *rndis;
  801. struct f_rndis_opts *opts;
  802. struct rndis_params *params;
  803. /* allocate and initialize one new instance */
  804. rndis = kzalloc(sizeof(*rndis), GFP_KERNEL);
  805. if (!rndis)
  806. return ERR_PTR(-ENOMEM);
  807. opts = container_of(fi, struct f_rndis_opts, func_inst);
  808. mutex_lock(&opts->lock);
  809. opts->refcnt++;
  810. gether_get_host_addr_u8(opts->net, rndis->ethaddr);
  811. rndis->vendorID = opts->vendor_id;
  812. rndis->manufacturer = opts->manufacturer;
  813. rndis->port.ioport = netdev_priv(opts->net);
  814. mutex_unlock(&opts->lock);
  815. /* RNDIS activates when the host changes this filter */
  816. rndis->port.cdc_filter = 0;
  817. /* RNDIS has special (and complex) framing */
  818. rndis->port.header_len = sizeof(struct rndis_packet_msg_type);
  819. rndis->port.wrap = rndis_add_header;
  820. rndis->port.unwrap = rndis_rm_hdr;
  821. rndis->port.func.name = "rndis";
  822. /* descriptors are per-instance copies */
  823. rndis->port.func.bind = rndis_bind;
  824. rndis->port.func.unbind = rndis_unbind;
  825. rndis->port.func.set_alt = rndis_set_alt;
  826. rndis->port.func.setup = rndis_setup;
  827. rndis->port.func.disable = rndis_disable;
  828. rndis->port.func.free_func = rndis_free;
  829. params = rndis_register(rndis_response_available, rndis);
  830. if (IS_ERR(params)) {
  831. kfree(rndis);
  832. return ERR_CAST(params);
  833. }
  834. rndis->params = params;
  835. return &rndis->port.func;
  836. }
  837. DECLARE_USB_FUNCTION_INIT(rndis, rndis_alloc_inst, rndis_alloc);
  838. MODULE_LICENSE("GPL");
  839. MODULE_AUTHOR("David Brownell");