rndis_host.c 19 KB

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