rndis_host.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. /*
  2. * Host Side support for RNDIS Networking Links
  3. * Copyright (C) 2005 by David Brownell
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <linux/module.h>
  19. #include <linux/netdevice.h>
  20. #include <linux/etherdevice.h>
  21. #include <linux/ethtool.h>
  22. #include <linux/workqueue.h>
  23. #include <linux/slab.h>
  24. #include <linux/mii.h>
  25. #include <linux/usb.h>
  26. #include <linux/usb/cdc.h>
  27. #include <linux/usb/usbnet.h>
  28. #include <linux/usb/rndis_host.h>
  29. /*
  30. * RNDIS is NDIS remoted over USB. It's a MSFT variant of CDC ACM ... of
  31. * course ACM was intended for modems, not Ethernet links! USB's standard
  32. * for Ethernet links is "CDC Ethernet", which is significantly simpler.
  33. *
  34. * NOTE that Microsoft's "RNDIS 1.0" specification is incomplete. Issues
  35. * include:
  36. * - Power management in particular relies on information that's scattered
  37. * through other documentation, and which is incomplete or incorrect even
  38. * there.
  39. * - There are various undocumented protocol requirements, such as the
  40. * need to send unused garbage in control-OUT messages.
  41. * - In some cases, MS-Windows will emit undocumented requests; this
  42. * matters more to peripheral implementations than host ones.
  43. *
  44. * Moreover there's a no-open-specs variant of RNDIS called "ActiveSync".
  45. *
  46. * For these reasons and others, ** USE OF RNDIS IS STRONGLY DISCOURAGED ** in
  47. * favor of such non-proprietary alternatives as CDC Ethernet or the newer (and
  48. * currently rare) "Ethernet Emulation Model" (EEM).
  49. */
  50. /*
  51. * RNDIS notifications from device: command completion; "reverse"
  52. * keepalives; etc
  53. */
  54. void rndis_status(struct usbnet *dev, struct urb *urb)
  55. {
  56. netdev_dbg(dev->net, "rndis status urb, len %d stat %d\n",
  57. urb->actual_length, urb->status);
  58. // FIXME for keepalives, respond immediately (asynchronously)
  59. // if not an RNDIS status, do like cdc_status(dev,urb) does
  60. }
  61. EXPORT_SYMBOL_GPL(rndis_status);
  62. /*
  63. * RNDIS indicate messages.
  64. */
  65. static void rndis_msg_indicate(struct usbnet *dev, struct rndis_indicate *msg,
  66. int buflen)
  67. {
  68. struct cdc_state *info = (void *)&dev->data;
  69. struct device *udev = &info->control->dev;
  70. if (dev->driver_info->indication) {
  71. dev->driver_info->indication(dev, msg, buflen);
  72. } else {
  73. u32 status = le32_to_cpu(msg->status);
  74. switch (status) {
  75. case RNDIS_STATUS_MEDIA_CONNECT:
  76. dev_info(udev, "rndis media connect\n");
  77. break;
  78. case RNDIS_STATUS_MEDIA_DISCONNECT:
  79. dev_info(udev, "rndis media disconnect\n");
  80. break;
  81. default:
  82. dev_info(udev, "rndis indication: 0x%08x\n", status);
  83. }
  84. }
  85. }
  86. /*
  87. * RPC done RNDIS-style. Caller guarantees:
  88. * - message is properly byteswapped
  89. * - there's no other request pending
  90. * - buf can hold up to 1KB response (required by RNDIS spec)
  91. * On return, the first few entries are already byteswapped.
  92. *
  93. * Call context is likely probe(), before interface name is known,
  94. * which is why we won't try to use it in the diagnostics.
  95. */
  96. int rndis_command(struct usbnet *dev, struct rndis_msg_hdr *buf, int buflen)
  97. {
  98. struct cdc_state *info = (void *) &dev->data;
  99. struct usb_cdc_notification notification;
  100. int master_ifnum;
  101. int retval;
  102. int partial;
  103. unsigned count;
  104. u32 xid = 0, msg_len, request_id, msg_type, rsp,
  105. status;
  106. /* REVISIT when this gets called from contexts other than probe() or
  107. * disconnect(): either serialize, or dispatch responses on xid
  108. */
  109. msg_type = le32_to_cpu(buf->msg_type);
  110. /* Issue the request; xid is unique, don't bother byteswapping it */
  111. if (likely(msg_type != RNDIS_MSG_HALT && msg_type != RNDIS_MSG_RESET)) {
  112. xid = dev->xid++;
  113. if (!xid)
  114. xid = dev->xid++;
  115. buf->request_id = (__force __le32) xid;
  116. }
  117. master_ifnum = info->control->cur_altsetting->desc.bInterfaceNumber;
  118. retval = usb_control_msg(dev->udev,
  119. usb_sndctrlpipe(dev->udev, 0),
  120. USB_CDC_SEND_ENCAPSULATED_COMMAND,
  121. USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  122. 0, master_ifnum,
  123. buf, le32_to_cpu(buf->msg_len),
  124. RNDIS_CONTROL_TIMEOUT_MS);
  125. if (unlikely(retval < 0 || xid == 0))
  126. return retval;
  127. /* Some devices don't respond on the control channel until
  128. * polled on the status channel, so do that first. */
  129. if (dev->driver_info->data & RNDIS_DRIVER_DATA_POLL_STATUS) {
  130. retval = usb_interrupt_msg(
  131. dev->udev,
  132. usb_rcvintpipe(dev->udev,
  133. dev->status->desc.bEndpointAddress),
  134. &notification, sizeof(notification), &partial,
  135. RNDIS_CONTROL_TIMEOUT_MS);
  136. if (unlikely(retval < 0))
  137. return retval;
  138. }
  139. /* Poll the control channel; the request probably completed immediately */
  140. rsp = le32_to_cpu(buf->msg_type) | RNDIS_MSG_COMPLETION;
  141. for (count = 0; count < 10; count++) {
  142. memset(buf, 0, CONTROL_BUFFER_SIZE);
  143. retval = usb_control_msg(dev->udev,
  144. usb_rcvctrlpipe(dev->udev, 0),
  145. USB_CDC_GET_ENCAPSULATED_RESPONSE,
  146. USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  147. 0, master_ifnum,
  148. buf, buflen,
  149. RNDIS_CONTROL_TIMEOUT_MS);
  150. if (likely(retval >= 8)) {
  151. msg_type = le32_to_cpu(buf->msg_type);
  152. msg_len = le32_to_cpu(buf->msg_len);
  153. status = le32_to_cpu(buf->status);
  154. request_id = (__force u32) buf->request_id;
  155. if (likely(msg_type == rsp)) {
  156. if (likely(request_id == xid)) {
  157. if (unlikely(rsp == RNDIS_MSG_RESET_C))
  158. return 0;
  159. if (likely(RNDIS_STATUS_SUCCESS ==
  160. status))
  161. return 0;
  162. dev_dbg(&info->control->dev,
  163. "rndis reply status %08x\n",
  164. status);
  165. return -EL3RST;
  166. }
  167. dev_dbg(&info->control->dev,
  168. "rndis reply id %d expected %d\n",
  169. request_id, xid);
  170. /* then likely retry */
  171. } else switch (msg_type) {
  172. case RNDIS_MSG_INDICATE: /* fault/event */
  173. rndis_msg_indicate(dev, (void *)buf, buflen);
  174. break;
  175. case RNDIS_MSG_KEEPALIVE: { /* ping */
  176. struct rndis_keepalive_c *msg = (void *)buf;
  177. msg->msg_type = cpu_to_le32(RNDIS_MSG_KEEPALIVE_C);
  178. msg->msg_len = cpu_to_le32(sizeof *msg);
  179. msg->status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
  180. retval = usb_control_msg(dev->udev,
  181. usb_sndctrlpipe(dev->udev, 0),
  182. USB_CDC_SEND_ENCAPSULATED_COMMAND,
  183. USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  184. 0, master_ifnum,
  185. msg, sizeof *msg,
  186. RNDIS_CONTROL_TIMEOUT_MS);
  187. if (unlikely(retval < 0))
  188. dev_dbg(&info->control->dev,
  189. "rndis keepalive err %d\n",
  190. retval);
  191. }
  192. break;
  193. default:
  194. dev_dbg(&info->control->dev,
  195. "unexpected rndis msg %08x len %d\n",
  196. le32_to_cpu(buf->msg_type), msg_len);
  197. }
  198. } else {
  199. /* device probably issued a protocol stall; ignore */
  200. dev_dbg(&info->control->dev,
  201. "rndis response error, code %d\n", retval);
  202. }
  203. msleep(20);
  204. }
  205. dev_dbg(&info->control->dev, "rndis response timeout\n");
  206. return -ETIMEDOUT;
  207. }
  208. EXPORT_SYMBOL_GPL(rndis_command);
  209. /*
  210. * rndis_query:
  211. *
  212. * Performs a query for @oid along with 0 or more bytes of payload as
  213. * specified by @in_len. If @reply_len is not set to -1 then the reply
  214. * length is checked against this value, resulting in an error if it
  215. * doesn't match.
  216. *
  217. * NOTE: Adding a payload exactly or greater than the size of the expected
  218. * response payload is an evident requirement MSFT added for ActiveSync.
  219. *
  220. * The only exception is for OIDs that return a variably sized response,
  221. * in which case no payload should be added. This undocumented (and
  222. * nonsensical!) issue was found by sniffing protocol requests from the
  223. * ActiveSync 4.1 Windows driver.
  224. */
  225. static int rndis_query(struct usbnet *dev, struct usb_interface *intf,
  226. void *buf, u32 oid, u32 in_len,
  227. void **reply, int *reply_len)
  228. {
  229. int retval;
  230. union {
  231. void *buf;
  232. struct rndis_msg_hdr *header;
  233. struct rndis_query *get;
  234. struct rndis_query_c *get_c;
  235. } u;
  236. u32 off, len;
  237. u.buf = buf;
  238. memset(u.get, 0, sizeof *u.get + in_len);
  239. u.get->msg_type = cpu_to_le32(RNDIS_MSG_QUERY);
  240. u.get->msg_len = cpu_to_le32(sizeof *u.get + in_len);
  241. u.get->oid = cpu_to_le32(oid);
  242. u.get->len = cpu_to_le32(in_len);
  243. u.get->offset = cpu_to_le32(20);
  244. retval = rndis_command(dev, u.header, CONTROL_BUFFER_SIZE);
  245. if (unlikely(retval < 0)) {
  246. dev_err(&intf->dev, "RNDIS_MSG_QUERY(0x%08x) failed, %d\n",
  247. oid, retval);
  248. return retval;
  249. }
  250. off = le32_to_cpu(u.get_c->offset);
  251. len = le32_to_cpu(u.get_c->len);
  252. if (unlikely((8 + off + len) > CONTROL_BUFFER_SIZE))
  253. goto response_error;
  254. if (*reply_len != -1 && len != *reply_len)
  255. goto response_error;
  256. *reply = (unsigned char *) &u.get_c->request_id + off;
  257. *reply_len = len;
  258. return retval;
  259. response_error:
  260. dev_err(&intf->dev, "RNDIS_MSG_QUERY(0x%08x) "
  261. "invalid response - off %d len %d\n",
  262. oid, off, len);
  263. return -EDOM;
  264. }
  265. /* same as usbnet_netdev_ops but MTU change not allowed */
  266. static const struct net_device_ops rndis_netdev_ops = {
  267. .ndo_open = usbnet_open,
  268. .ndo_stop = usbnet_stop,
  269. .ndo_start_xmit = usbnet_start_xmit,
  270. .ndo_tx_timeout = usbnet_tx_timeout,
  271. .ndo_set_mac_address = eth_mac_addr,
  272. .ndo_validate_addr = eth_validate_addr,
  273. };
  274. int
  275. generic_rndis_bind(struct usbnet *dev, struct usb_interface *intf, int flags)
  276. {
  277. int retval;
  278. struct net_device *net = dev->net;
  279. struct cdc_state *info = (void *) &dev->data;
  280. union {
  281. void *buf;
  282. struct rndis_msg_hdr *header;
  283. struct rndis_init *init;
  284. struct rndis_init_c *init_c;
  285. struct rndis_query *get;
  286. struct rndis_query_c *get_c;
  287. struct rndis_set *set;
  288. struct rndis_set_c *set_c;
  289. struct rndis_halt *halt;
  290. } u;
  291. u32 tmp;
  292. __le32 phym_unspec, *phym;
  293. int reply_len;
  294. unsigned char *bp;
  295. /* we can't rely on i/o from stack working, or stack allocation */
  296. u.buf = kmalloc(CONTROL_BUFFER_SIZE, GFP_KERNEL);
  297. if (!u.buf)
  298. return -ENOMEM;
  299. retval = usbnet_generic_cdc_bind(dev, intf);
  300. if (retval < 0)
  301. goto fail;
  302. u.init->msg_type = cpu_to_le32(RNDIS_MSG_INIT);
  303. u.init->msg_len = cpu_to_le32(sizeof *u.init);
  304. u.init->major_version = cpu_to_le32(1);
  305. u.init->minor_version = cpu_to_le32(0);
  306. /* max transfer (in spec) is 0x4000 at full speed, but for
  307. * TX we'll stick to one Ethernet packet plus RNDIS framing.
  308. * For RX we handle drivers that zero-pad to end-of-packet.
  309. * Don't let userspace change these settings.
  310. *
  311. * NOTE: there still seems to be wierdness here, as if we need
  312. * to do some more things to make sure WinCE targets accept this.
  313. * They default to jumbograms of 8KB or 16KB, which is absurd
  314. * for such low data rates and which is also more than Linux
  315. * can usually expect to allocate for SKB data...
  316. */
  317. net->hard_header_len += sizeof (struct rndis_data_hdr);
  318. dev->hard_mtu = net->mtu + net->hard_header_len;
  319. dev->maxpacket = usb_maxpacket(dev->udev, dev->out, 1);
  320. if (dev->maxpacket == 0) {
  321. netif_dbg(dev, probe, dev->net,
  322. "dev->maxpacket can't be 0\n");
  323. retval = -EINVAL;
  324. goto fail_and_release;
  325. }
  326. dev->rx_urb_size = dev->hard_mtu + (dev->maxpacket + 1);
  327. dev->rx_urb_size &= ~(dev->maxpacket - 1);
  328. u.init->max_transfer_size = cpu_to_le32(dev->rx_urb_size);
  329. net->netdev_ops = &rndis_netdev_ops;
  330. retval = rndis_command(dev, u.header, CONTROL_BUFFER_SIZE);
  331. if (unlikely(retval < 0)) {
  332. /* it might not even be an RNDIS device!! */
  333. dev_err(&intf->dev, "RNDIS init failed, %d\n", retval);
  334. goto fail_and_release;
  335. }
  336. tmp = le32_to_cpu(u.init_c->max_transfer_size);
  337. if (tmp < dev->hard_mtu) {
  338. if (tmp <= net->hard_header_len) {
  339. dev_err(&intf->dev,
  340. "dev can't take %u byte packets (max %u)\n",
  341. dev->hard_mtu, tmp);
  342. retval = -EINVAL;
  343. goto halt_fail_and_release;
  344. }
  345. dev_warn(&intf->dev,
  346. "dev can't take %u byte packets (max %u), "
  347. "adjusting MTU to %u\n",
  348. dev->hard_mtu, tmp, tmp - net->hard_header_len);
  349. dev->hard_mtu = tmp;
  350. net->mtu = dev->hard_mtu - net->hard_header_len;
  351. }
  352. /* REVISIT: peripheral "alignment" request is ignored ... */
  353. dev_dbg(&intf->dev,
  354. "hard mtu %u (%u from dev), rx buflen %Zu, align %d\n",
  355. dev->hard_mtu, tmp, dev->rx_urb_size,
  356. 1 << le32_to_cpu(u.init_c->packet_alignment));
  357. /* module has some device initialization code needs to be done right
  358. * after RNDIS_INIT */
  359. if (dev->driver_info->early_init &&
  360. dev->driver_info->early_init(dev) != 0)
  361. goto halt_fail_and_release;
  362. /* Check physical medium */
  363. phym = NULL;
  364. reply_len = sizeof *phym;
  365. retval = rndis_query(dev, intf, u.buf,
  366. RNDIS_OID_GEN_PHYSICAL_MEDIUM,
  367. 0, (void **) &phym, &reply_len);
  368. if (retval != 0 || !phym) {
  369. /* OID is optional so don't fail here. */
  370. phym_unspec = cpu_to_le32(RNDIS_PHYSICAL_MEDIUM_UNSPECIFIED);
  371. phym = &phym_unspec;
  372. }
  373. if ((flags & FLAG_RNDIS_PHYM_WIRELESS) &&
  374. le32_to_cpup(phym) != RNDIS_PHYSICAL_MEDIUM_WIRELESS_LAN) {
  375. netif_dbg(dev, probe, dev->net,
  376. "driver requires wireless physical medium, but device is not\n");
  377. retval = -ENODEV;
  378. goto halt_fail_and_release;
  379. }
  380. if ((flags & FLAG_RNDIS_PHYM_NOT_WIRELESS) &&
  381. le32_to_cpup(phym) == RNDIS_PHYSICAL_MEDIUM_WIRELESS_LAN) {
  382. netif_dbg(dev, probe, dev->net,
  383. "driver requires non-wireless physical medium, but device is wireless.\n");
  384. retval = -ENODEV;
  385. goto halt_fail_and_release;
  386. }
  387. /* Get designated host ethernet address */
  388. reply_len = ETH_ALEN;
  389. retval = rndis_query(dev, intf, u.buf,
  390. RNDIS_OID_802_3_PERMANENT_ADDRESS,
  391. 48, (void **) &bp, &reply_len);
  392. if (unlikely(retval< 0)) {
  393. dev_err(&intf->dev, "rndis get ethaddr, %d\n", retval);
  394. goto halt_fail_and_release;
  395. }
  396. memcpy(net->dev_addr, bp, ETH_ALEN);
  397. /* set a nonzero filter to enable data transfers */
  398. memset(u.set, 0, sizeof *u.set);
  399. u.set->msg_type = cpu_to_le32(RNDIS_MSG_SET);
  400. u.set->msg_len = cpu_to_le32(4 + sizeof *u.set);
  401. u.set->oid = cpu_to_le32(RNDIS_OID_GEN_CURRENT_PACKET_FILTER);
  402. u.set->len = cpu_to_le32(4);
  403. u.set->offset = cpu_to_le32((sizeof *u.set) - 8);
  404. *(__le32 *)(u.buf + sizeof *u.set) = cpu_to_le32(RNDIS_DEFAULT_FILTER);
  405. retval = rndis_command(dev, u.header, CONTROL_BUFFER_SIZE);
  406. if (unlikely(retval < 0)) {
  407. dev_err(&intf->dev, "rndis set packet filter, %d\n", retval);
  408. goto halt_fail_and_release;
  409. }
  410. retval = 0;
  411. kfree(u.buf);
  412. return retval;
  413. halt_fail_and_release:
  414. memset(u.halt, 0, sizeof *u.halt);
  415. u.halt->msg_type = cpu_to_le32(RNDIS_MSG_HALT);
  416. u.halt->msg_len = cpu_to_le32(sizeof *u.halt);
  417. (void) rndis_command(dev, (void *)u.halt, CONTROL_BUFFER_SIZE);
  418. fail_and_release:
  419. usb_set_intfdata(info->data, NULL);
  420. usb_driver_release_interface(driver_of(intf), info->data);
  421. info->data = NULL;
  422. fail:
  423. kfree(u.buf);
  424. return retval;
  425. }
  426. EXPORT_SYMBOL_GPL(generic_rndis_bind);
  427. static int rndis_bind(struct usbnet *dev, struct usb_interface *intf)
  428. {
  429. return generic_rndis_bind(dev, intf, FLAG_RNDIS_PHYM_NOT_WIRELESS);
  430. }
  431. void rndis_unbind(struct usbnet *dev, struct usb_interface *intf)
  432. {
  433. struct rndis_halt *halt;
  434. /* try to clear any rndis state/activity (no i/o from stack!) */
  435. halt = kzalloc(CONTROL_BUFFER_SIZE, GFP_KERNEL);
  436. if (halt) {
  437. halt->msg_type = cpu_to_le32(RNDIS_MSG_HALT);
  438. halt->msg_len = cpu_to_le32(sizeof *halt);
  439. (void) rndis_command(dev, (void *)halt, CONTROL_BUFFER_SIZE);
  440. kfree(halt);
  441. }
  442. usbnet_cdc_unbind(dev, intf);
  443. }
  444. EXPORT_SYMBOL_GPL(rndis_unbind);
  445. /*
  446. * DATA -- host must not write zlps
  447. */
  448. int rndis_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
  449. {
  450. /* This check is no longer done by usbnet */
  451. if (skb->len < dev->net->hard_header_len)
  452. return 0;
  453. /* peripheral may have batched packets to us... */
  454. while (likely(skb->len)) {
  455. struct rndis_data_hdr *hdr = (void *)skb->data;
  456. struct sk_buff *skb2;
  457. u32 msg_type, msg_len, data_offset, data_len;
  458. msg_type = le32_to_cpu(hdr->msg_type);
  459. msg_len = le32_to_cpu(hdr->msg_len);
  460. data_offset = le32_to_cpu(hdr->data_offset);
  461. data_len = le32_to_cpu(hdr->data_len);
  462. /* don't choke if we see oob, per-packet data, etc */
  463. if (unlikely(msg_type != RNDIS_MSG_PACKET || skb->len < msg_len
  464. || (data_offset + data_len + 8) > msg_len)) {
  465. dev->net->stats.rx_frame_errors++;
  466. netdev_dbg(dev->net, "bad rndis message %d/%d/%d/%d, len %d\n",
  467. le32_to_cpu(hdr->msg_type),
  468. msg_len, data_offset, data_len, skb->len);
  469. return 0;
  470. }
  471. skb_pull(skb, 8 + data_offset);
  472. /* at most one packet left? */
  473. if (likely((data_len - skb->len) <= sizeof *hdr)) {
  474. skb_trim(skb, data_len);
  475. break;
  476. }
  477. /* try to return all the packets in the batch */
  478. skb2 = skb_clone(skb, GFP_ATOMIC);
  479. if (unlikely(!skb2))
  480. break;
  481. skb_pull(skb, msg_len - sizeof *hdr);
  482. skb_trim(skb2, data_len);
  483. usbnet_skb_return(dev, skb2);
  484. }
  485. /* caller will usbnet_skb_return the remaining packet */
  486. return 1;
  487. }
  488. EXPORT_SYMBOL_GPL(rndis_rx_fixup);
  489. struct sk_buff *
  490. rndis_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags)
  491. {
  492. struct rndis_data_hdr *hdr;
  493. struct sk_buff *skb2;
  494. unsigned len = skb->len;
  495. if (likely(!skb_cloned(skb))) {
  496. int room = skb_headroom(skb);
  497. /* enough head room as-is? */
  498. if (unlikely((sizeof *hdr) <= room))
  499. goto fill;
  500. /* enough room, but needs to be readjusted? */
  501. room += skb_tailroom(skb);
  502. if (likely((sizeof *hdr) <= room)) {
  503. skb->data = memmove(skb->head + sizeof *hdr,
  504. skb->data, len);
  505. skb_set_tail_pointer(skb, len);
  506. goto fill;
  507. }
  508. }
  509. /* create a new skb, with the correct size (and tailpad) */
  510. skb2 = skb_copy_expand(skb, sizeof *hdr, 1, flags);
  511. dev_kfree_skb_any(skb);
  512. if (unlikely(!skb2))
  513. return skb2;
  514. skb = skb2;
  515. /* fill out the RNDIS header. we won't bother trying to batch
  516. * packets; Linux minimizes wasted bandwidth through tx queues.
  517. */
  518. fill:
  519. hdr = (void *) __skb_push(skb, sizeof *hdr);
  520. memset(hdr, 0, sizeof *hdr);
  521. hdr->msg_type = cpu_to_le32(RNDIS_MSG_PACKET);
  522. hdr->msg_len = cpu_to_le32(skb->len);
  523. hdr->data_offset = cpu_to_le32(sizeof(*hdr) - 8);
  524. hdr->data_len = cpu_to_le32(len);
  525. /* FIXME make the last packet always be short ... */
  526. return skb;
  527. }
  528. EXPORT_SYMBOL_GPL(rndis_tx_fixup);
  529. static const struct driver_info rndis_info = {
  530. .description = "RNDIS device",
  531. .flags = FLAG_ETHER | FLAG_POINTTOPOINT | FLAG_FRAMING_RN | FLAG_NO_SETINT,
  532. .bind = rndis_bind,
  533. .unbind = rndis_unbind,
  534. .status = rndis_status,
  535. .rx_fixup = rndis_rx_fixup,
  536. .tx_fixup = rndis_tx_fixup,
  537. };
  538. static const struct driver_info rndis_poll_status_info = {
  539. .description = "RNDIS device (poll status before control)",
  540. .flags = FLAG_ETHER | FLAG_POINTTOPOINT | FLAG_FRAMING_RN | FLAG_NO_SETINT,
  541. .data = RNDIS_DRIVER_DATA_POLL_STATUS,
  542. .bind = rndis_bind,
  543. .unbind = rndis_unbind,
  544. .status = rndis_status,
  545. .rx_fixup = rndis_rx_fixup,
  546. .tx_fixup = rndis_tx_fixup,
  547. };
  548. /*-------------------------------------------------------------------------*/
  549. static const struct usb_device_id products [] = {
  550. {
  551. /* 2Wire HomePortal 1000SW */
  552. USB_DEVICE_AND_INTERFACE_INFO(0x1630, 0x0042,
  553. USB_CLASS_COMM, 2 /* ACM */, 0x0ff),
  554. .driver_info = (unsigned long) &rndis_poll_status_info,
  555. }, {
  556. /* RNDIS is MSFT's un-official variant of CDC ACM */
  557. USB_INTERFACE_INFO(USB_CLASS_COMM, 2 /* ACM */, 0x0ff),
  558. .driver_info = (unsigned long) &rndis_info,
  559. }, {
  560. /* "ActiveSync" is an undocumented variant of RNDIS, used in WM5 */
  561. USB_INTERFACE_INFO(USB_CLASS_MISC, 1, 1),
  562. .driver_info = (unsigned long) &rndis_poll_status_info,
  563. }, {
  564. /* RNDIS for tethering */
  565. USB_INTERFACE_INFO(USB_CLASS_WIRELESS_CONTROLLER, 1, 3),
  566. .driver_info = (unsigned long) &rndis_info,
  567. },
  568. { }, // END
  569. };
  570. MODULE_DEVICE_TABLE(usb, products);
  571. static struct usb_driver rndis_driver = {
  572. .name = "rndis_host",
  573. .id_table = products,
  574. .probe = usbnet_probe,
  575. .disconnect = usbnet_disconnect,
  576. .suspend = usbnet_suspend,
  577. .resume = usbnet_resume,
  578. .disable_hub_initiated_lpm = 1,
  579. };
  580. module_usb_driver(rndis_driver);
  581. MODULE_AUTHOR("David Brownell");
  582. MODULE_DESCRIPTION("USB Host side RNDIS driver");
  583. MODULE_LICENSE("GPL");