f_rndis.c 24 KB

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