rndis_host.c 20 KB

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