f_rndis.c 25 KB

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