rndis_host.c 21 KB

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