f_rndis.c 25 KB

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