f_rndis.c 29 KB

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