f_rndis.c 26 KB

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