f_rndis.c 25 KB

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