PageRenderTime 1188ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/usb/gadget/f_rndis.c

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