f_rndis.c 29 KB

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