rndis_host.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  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. // #define DEBUG // error path messages, extra info
  20. // #define VERBOSE // more; success messages
  21. #include <linux/module.h>
  22. #include <linux/init.h>
  23. #include <linux/netdevice.h>
  24. #include <linux/etherdevice.h>
  25. #include <linux/ethtool.h>
  26. #include <linux/workqueue.h>
  27. #include <linux/mii.h>
  28. #include <linux/usb.h>
  29. #include <linux/usb/cdc.h>
  30. #include "usbnet.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. * CONTROL uses CDC "encapsulated commands" with funky notifications.
  54. * - control-out: SEND_ENCAPSULATED
  55. * - interrupt-in: RESPONSE_AVAILABLE
  56. * - control-in: GET_ENCAPSULATED
  57. *
  58. * We'll try to ignore the RESPONSE_AVAILABLE notifications.
  59. *
  60. * REVISIT some RNDIS implementations seem to have curious issues still
  61. * to be resolved.
  62. */
  63. struct rndis_msg_hdr {
  64. __le32 msg_type; /* RNDIS_MSG_* */
  65. __le32 msg_len;
  66. // followed by data that varies between messages
  67. __le32 request_id;
  68. __le32 status;
  69. // ... and more
  70. } __attribute__ ((packed));
  71. /* MS-Windows uses this strange size, but RNDIS spec says 1024 minimum */
  72. #define CONTROL_BUFFER_SIZE 1025
  73. /* RNDIS defines an (absurdly huge) 10 second control timeout,
  74. * but ActiveSync seems to use a more usual 5 second timeout
  75. * (which matches the USB 2.0 spec).
  76. */
  77. #define RNDIS_CONTROL_TIMEOUT_MS (5 * 1000)
  78. #define ccpu2 __constant_cpu_to_le32
  79. #define RNDIS_MSG_COMPLETION ccpu2(0x80000000)
  80. /* codes for "msg_type" field of rndis messages;
  81. * only the data channel uses packet messages (maybe batched);
  82. * everything else goes on the control channel.
  83. */
  84. #define RNDIS_MSG_PACKET ccpu2(0x00000001) /* 1-N packets */
  85. #define RNDIS_MSG_INIT ccpu2(0x00000002)
  86. #define RNDIS_MSG_INIT_C (RNDIS_MSG_INIT|RNDIS_MSG_COMPLETION)
  87. #define RNDIS_MSG_HALT ccpu2(0x00000003)
  88. #define RNDIS_MSG_QUERY ccpu2(0x00000004)
  89. #define RNDIS_MSG_QUERY_C (RNDIS_MSG_QUERY|RNDIS_MSG_COMPLETION)
  90. #define RNDIS_MSG_SET ccpu2(0x00000005)
  91. #define RNDIS_MSG_SET_C (RNDIS_MSG_SET|RNDIS_MSG_COMPLETION)
  92. #define RNDIS_MSG_RESET ccpu2(0x00000006)
  93. #define RNDIS_MSG_RESET_C (RNDIS_MSG_RESET|RNDIS_MSG_COMPLETION)
  94. #define RNDIS_MSG_INDICATE ccpu2(0x00000007)
  95. #define RNDIS_MSG_KEEPALIVE ccpu2(0x00000008)
  96. #define RNDIS_MSG_KEEPALIVE_C (RNDIS_MSG_KEEPALIVE|RNDIS_MSG_COMPLETION)
  97. /* codes for "status" field of completion messages */
  98. #define RNDIS_STATUS_SUCCESS ccpu2(0x00000000)
  99. #define RNDIS_STATUS_FAILURE ccpu2(0xc0000001)
  100. #define RNDIS_STATUS_INVALID_DATA ccpu2(0xc0010015)
  101. #define RNDIS_STATUS_NOT_SUPPORTED ccpu2(0xc00000bb)
  102. #define RNDIS_STATUS_MEDIA_CONNECT ccpu2(0x4001000b)
  103. #define RNDIS_STATUS_MEDIA_DISCONNECT ccpu2(0x4001000c)
  104. struct rndis_data_hdr {
  105. __le32 msg_type; /* RNDIS_MSG_PACKET */
  106. __le32 msg_len; // rndis_data_hdr + data_len + pad
  107. __le32 data_offset; // 36 -- right after header
  108. __le32 data_len; // ... real packet size
  109. __le32 oob_data_offset; // zero
  110. __le32 oob_data_len; // zero
  111. __le32 num_oob; // zero
  112. __le32 packet_data_offset; // zero
  113. __le32 packet_data_len; // zero
  114. __le32 vc_handle; // zero
  115. __le32 reserved; // zero
  116. } __attribute__ ((packed));
  117. struct rndis_init { /* OUT */
  118. // header and:
  119. __le32 msg_type; /* RNDIS_MSG_INIT */
  120. __le32 msg_len; // 24
  121. __le32 request_id;
  122. __le32 major_version; // of rndis (1.0)
  123. __le32 minor_version;
  124. __le32 max_transfer_size;
  125. } __attribute__ ((packed));
  126. struct rndis_init_c { /* IN */
  127. // header and:
  128. __le32 msg_type; /* RNDIS_MSG_INIT_C */
  129. __le32 msg_len;
  130. __le32 request_id;
  131. __le32 status;
  132. __le32 major_version; // of rndis (1.0)
  133. __le32 minor_version;
  134. __le32 device_flags;
  135. __le32 medium; // zero == 802.3
  136. __le32 max_packets_per_message;
  137. __le32 max_transfer_size;
  138. __le32 packet_alignment; // max 7; (1<<n) bytes
  139. __le32 af_list_offset; // zero
  140. __le32 af_list_size; // zero
  141. } __attribute__ ((packed));
  142. struct rndis_halt { /* OUT (no reply) */
  143. // header and:
  144. __le32 msg_type; /* RNDIS_MSG_HALT */
  145. __le32 msg_len;
  146. __le32 request_id;
  147. } __attribute__ ((packed));
  148. struct rndis_query { /* OUT */
  149. // header and:
  150. __le32 msg_type; /* RNDIS_MSG_QUERY */
  151. __le32 msg_len;
  152. __le32 request_id;
  153. __le32 oid;
  154. __le32 len;
  155. __le32 offset;
  156. /*?*/ __le32 handle; // zero
  157. } __attribute__ ((packed));
  158. struct rndis_query_c { /* IN */
  159. // header and:
  160. __le32 msg_type; /* RNDIS_MSG_QUERY_C */
  161. __le32 msg_len;
  162. __le32 request_id;
  163. __le32 status;
  164. __le32 len;
  165. __le32 offset;
  166. } __attribute__ ((packed));
  167. struct rndis_set { /* OUT */
  168. // header and:
  169. __le32 msg_type; /* RNDIS_MSG_SET */
  170. __le32 msg_len;
  171. __le32 request_id;
  172. __le32 oid;
  173. __le32 len;
  174. __le32 offset;
  175. /*?*/ __le32 handle; // zero
  176. } __attribute__ ((packed));
  177. struct rndis_set_c { /* IN */
  178. // header and:
  179. __le32 msg_type; /* RNDIS_MSG_SET_C */
  180. __le32 msg_len;
  181. __le32 request_id;
  182. __le32 status;
  183. } __attribute__ ((packed));
  184. struct rndis_reset { /* IN */
  185. // header and:
  186. __le32 msg_type; /* RNDIS_MSG_RESET */
  187. __le32 msg_len;
  188. __le32 reserved;
  189. } __attribute__ ((packed));
  190. struct rndis_reset_c { /* OUT */
  191. // header and:
  192. __le32 msg_type; /* RNDIS_MSG_RESET_C */
  193. __le32 msg_len;
  194. __le32 status;
  195. __le32 addressing_lost;
  196. } __attribute__ ((packed));
  197. struct rndis_indicate { /* IN (unrequested) */
  198. // header and:
  199. __le32 msg_type; /* RNDIS_MSG_INDICATE */
  200. __le32 msg_len;
  201. __le32 status;
  202. __le32 length;
  203. __le32 offset;
  204. /**/ __le32 diag_status;
  205. __le32 error_offset;
  206. /**/ __le32 message;
  207. } __attribute__ ((packed));
  208. struct rndis_keepalive { /* OUT (optionally IN) */
  209. // header and:
  210. __le32 msg_type; /* RNDIS_MSG_KEEPALIVE */
  211. __le32 msg_len;
  212. __le32 request_id;
  213. } __attribute__ ((packed));
  214. struct rndis_keepalive_c { /* IN (optionally OUT) */
  215. // header and:
  216. __le32 msg_type; /* RNDIS_MSG_KEEPALIVE_C */
  217. __le32 msg_len;
  218. __le32 request_id;
  219. __le32 status;
  220. } __attribute__ ((packed));
  221. /* NOTE: about 30 OIDs are "mandatory" for peripherals to support ... and
  222. * there are gobs more that may optionally be supported. We'll avoid as much
  223. * of that mess as possible.
  224. */
  225. #define OID_802_3_PERMANENT_ADDRESS ccpu2(0x01010101)
  226. #define OID_GEN_MAXIMUM_FRAME_SIZE ccpu2(0x00010106)
  227. #define OID_GEN_CURRENT_PACKET_FILTER ccpu2(0x0001010e)
  228. /*
  229. * RNDIS notifications from device: command completion; "reverse"
  230. * keepalives; etc
  231. */
  232. static void rndis_status(struct usbnet *dev, struct urb *urb)
  233. {
  234. devdbg(dev, "rndis status urb, len %d stat %d",
  235. urb->actual_length, urb->status);
  236. // FIXME for keepalives, respond immediately (asynchronously)
  237. // if not an RNDIS status, do like cdc_status(dev,urb) does
  238. }
  239. /*
  240. * RPC done RNDIS-style. Caller guarantees:
  241. * - message is properly byteswapped
  242. * - there's no other request pending
  243. * - buf can hold up to 1KB response (required by RNDIS spec)
  244. * On return, the first few entries are already byteswapped.
  245. *
  246. * Call context is likely probe(), before interface name is known,
  247. * which is why we won't try to use it in the diagnostics.
  248. */
  249. static int rndis_command(struct usbnet *dev, struct rndis_msg_hdr *buf)
  250. {
  251. struct cdc_state *info = (void *) &dev->data;
  252. int master_ifnum;
  253. int retval;
  254. unsigned count;
  255. __le32 rsp;
  256. u32 xid = 0, msg_len, request_id;
  257. /* REVISIT when this gets called from contexts other than probe() or
  258. * disconnect(): either serialize, or dispatch responses on xid
  259. */
  260. /* Issue the request; xid is unique, don't bother byteswapping it */
  261. if (likely(buf->msg_type != RNDIS_MSG_HALT
  262. && buf->msg_type != RNDIS_MSG_RESET)) {
  263. xid = dev->xid++;
  264. if (!xid)
  265. xid = dev->xid++;
  266. buf->request_id = (__force __le32) xid;
  267. }
  268. master_ifnum = info->control->cur_altsetting->desc.bInterfaceNumber;
  269. retval = usb_control_msg(dev->udev,
  270. usb_sndctrlpipe(dev->udev, 0),
  271. USB_CDC_SEND_ENCAPSULATED_COMMAND,
  272. USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  273. 0, master_ifnum,
  274. buf, le32_to_cpu(buf->msg_len),
  275. RNDIS_CONTROL_TIMEOUT_MS);
  276. if (unlikely(retval < 0 || xid == 0))
  277. return retval;
  278. // FIXME Seems like some devices discard responses when
  279. // we time out and cancel our "get response" requests...
  280. // so, this is fragile. Probably need to poll for status.
  281. /* ignore status endpoint, just poll the control channel;
  282. * the request probably completed immediately
  283. */
  284. rsp = buf->msg_type | RNDIS_MSG_COMPLETION;
  285. for (count = 0; count < 10; count++) {
  286. memset(buf, 0, CONTROL_BUFFER_SIZE);
  287. retval = usb_control_msg(dev->udev,
  288. usb_rcvctrlpipe(dev->udev, 0),
  289. USB_CDC_GET_ENCAPSULATED_RESPONSE,
  290. USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  291. 0, master_ifnum,
  292. buf, CONTROL_BUFFER_SIZE,
  293. RNDIS_CONTROL_TIMEOUT_MS);
  294. if (likely(retval >= 8)) {
  295. msg_len = le32_to_cpu(buf->msg_len);
  296. request_id = (__force u32) buf->request_id;
  297. if (likely(buf->msg_type == rsp)) {
  298. if (likely(request_id == xid)) {
  299. if (unlikely(rsp == RNDIS_MSG_RESET_C))
  300. return 0;
  301. if (likely(RNDIS_STATUS_SUCCESS
  302. == buf->status))
  303. return 0;
  304. dev_dbg(&info->control->dev,
  305. "rndis reply status %08x\n",
  306. le32_to_cpu(buf->status));
  307. return -EL3RST;
  308. }
  309. dev_dbg(&info->control->dev,
  310. "rndis reply id %d expected %d\n",
  311. request_id, xid);
  312. /* then likely retry */
  313. } else switch (buf->msg_type) {
  314. case RNDIS_MSG_INDICATE: { /* fault */
  315. // struct rndis_indicate *msg = (void *)buf;
  316. dev_info(&info->control->dev,
  317. "rndis fault indication\n");
  318. }
  319. break;
  320. case RNDIS_MSG_KEEPALIVE: { /* ping */
  321. struct rndis_keepalive_c *msg = (void *)buf;
  322. msg->msg_type = RNDIS_MSG_KEEPALIVE_C;
  323. msg->msg_len = ccpu2(sizeof *msg);
  324. msg->status = RNDIS_STATUS_SUCCESS;
  325. retval = usb_control_msg(dev->udev,
  326. usb_sndctrlpipe(dev->udev, 0),
  327. USB_CDC_SEND_ENCAPSULATED_COMMAND,
  328. USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  329. 0, master_ifnum,
  330. msg, sizeof *msg,
  331. RNDIS_CONTROL_TIMEOUT_MS);
  332. if (unlikely(retval < 0))
  333. dev_dbg(&info->control->dev,
  334. "rndis keepalive err %d\n",
  335. retval);
  336. }
  337. break;
  338. default:
  339. dev_dbg(&info->control->dev,
  340. "unexpected rndis msg %08x len %d\n",
  341. le32_to_cpu(buf->msg_type), msg_len);
  342. }
  343. } else {
  344. /* device probably issued a protocol stall; ignore */
  345. dev_dbg(&info->control->dev,
  346. "rndis response error, code %d\n", retval);
  347. }
  348. msleep(2);
  349. }
  350. dev_dbg(&info->control->dev, "rndis response timeout\n");
  351. return -ETIMEDOUT;
  352. }
  353. /*
  354. * rndis_query:
  355. *
  356. * Performs a query for @oid along with 0 or more bytes of payload as
  357. * specified by @in_len. If @reply_len is not set to -1 then the reply
  358. * length is checked against this value, resulting in an error if it
  359. * doesn't match.
  360. *
  361. * NOTE: Adding a payload exactly or greater than the size of the expected
  362. * response payload is an evident requirement MSFT added for ActiveSync.
  363. *
  364. * The only exception is for OIDs that return a variably sized response,
  365. * in which case no payload should be added. This undocumented (and
  366. * nonsensical!) issue was found by sniffing protocol requests from the
  367. * ActiveSync 4.1 Windows driver.
  368. */
  369. static int rndis_query(struct usbnet *dev, struct usb_interface *intf,
  370. void *buf, u32 oid, u32 in_len,
  371. void **reply, int *reply_len)
  372. {
  373. int retval;
  374. union {
  375. void *buf;
  376. struct rndis_msg_hdr *header;
  377. struct rndis_query *get;
  378. struct rndis_query_c *get_c;
  379. } u;
  380. u32 off, len;
  381. u.buf = buf;
  382. memset(u.get, 0, sizeof *u.get + in_len);
  383. u.get->msg_type = RNDIS_MSG_QUERY;
  384. u.get->msg_len = cpu_to_le32(sizeof *u.get + in_len);
  385. u.get->oid = oid;
  386. u.get->len = cpu_to_le32(in_len);
  387. u.get->offset = ccpu2(20);
  388. retval = rndis_command(dev, u.header);
  389. if (unlikely(retval < 0)) {
  390. dev_err(&intf->dev, "RNDIS_MSG_QUERY(0x%08x) failed, %d\n",
  391. oid, retval);
  392. return retval;
  393. }
  394. off = le32_to_cpu(u.get_c->offset);
  395. len = le32_to_cpu(u.get_c->len);
  396. if (unlikely((8 + off + len) > CONTROL_BUFFER_SIZE))
  397. goto response_error;
  398. if (*reply_len != -1 && len != *reply_len)
  399. goto response_error;
  400. *reply = (unsigned char *) &u.get_c->request_id + off;
  401. *reply_len = len;
  402. return retval;
  403. response_error:
  404. dev_err(&intf->dev, "RNDIS_MSG_QUERY(0x%08x) "
  405. "invalid response - off %d len %d\n",
  406. oid, off, len);
  407. return -EDOM;
  408. }
  409. static int rndis_bind(struct usbnet *dev, struct usb_interface *intf)
  410. {
  411. int retval;
  412. struct net_device *net = dev->net;
  413. struct cdc_state *info = (void *) &dev->data;
  414. union {
  415. void *buf;
  416. struct rndis_msg_hdr *header;
  417. struct rndis_init *init;
  418. struct rndis_init_c *init_c;
  419. struct rndis_query *get;
  420. struct rndis_query_c *get_c;
  421. struct rndis_set *set;
  422. struct rndis_set_c *set_c;
  423. } u;
  424. u32 tmp;
  425. int reply_len;
  426. unsigned char *bp;
  427. /* we can't rely on i/o from stack working, or stack allocation */
  428. u.buf = kmalloc(CONTROL_BUFFER_SIZE, GFP_KERNEL);
  429. if (!u.buf)
  430. return -ENOMEM;
  431. retval = usbnet_generic_cdc_bind(dev, intf);
  432. if (retval < 0)
  433. goto fail;
  434. u.init->msg_type = RNDIS_MSG_INIT;
  435. u.init->msg_len = ccpu2(sizeof *u.init);
  436. u.init->major_version = ccpu2(1);
  437. u.init->minor_version = ccpu2(0);
  438. /* max transfer (in spec) is 0x4000 at full speed, but for
  439. * TX we'll stick to one Ethernet packet plus RNDIS framing.
  440. * For RX we handle drivers that zero-pad to end-of-packet.
  441. * Don't let userspace change these settings.
  442. *
  443. * NOTE: there still seems to be wierdness here, as if we need
  444. * to do some more things to make sure WinCE targets accept this.
  445. * They default to jumbograms of 8KB or 16KB, which is absurd
  446. * for such low data rates and which is also more than Linux
  447. * can usually expect to allocate for SKB data...
  448. */
  449. net->hard_header_len += sizeof (struct rndis_data_hdr);
  450. dev->hard_mtu = net->mtu + net->hard_header_len;
  451. dev->rx_urb_size = dev->hard_mtu + (dev->maxpacket + 1);
  452. dev->rx_urb_size &= ~(dev->maxpacket - 1);
  453. u.init->max_transfer_size = cpu_to_le32(dev->rx_urb_size);
  454. net->change_mtu = NULL;
  455. retval = rndis_command(dev, u.header);
  456. if (unlikely(retval < 0)) {
  457. /* it might not even be an RNDIS device!! */
  458. dev_err(&intf->dev, "RNDIS init failed, %d\n", retval);
  459. goto fail_and_release;
  460. }
  461. tmp = le32_to_cpu(u.init_c->max_transfer_size);
  462. if (tmp < dev->hard_mtu) {
  463. dev_err(&intf->dev,
  464. "dev can't take %u byte packets (max %u)\n",
  465. dev->hard_mtu, tmp);
  466. goto fail_and_release;
  467. }
  468. /* REVISIT: peripheral "alignment" request is ignored ... */
  469. dev_dbg(&intf->dev,
  470. "hard mtu %u (%u from dev), rx buflen %Zu, align %d\n",
  471. dev->hard_mtu, tmp, dev->rx_urb_size,
  472. 1 << le32_to_cpu(u.init_c->packet_alignment));
  473. /* Get designated host ethernet address */
  474. reply_len = ETH_ALEN;
  475. retval = rndis_query(dev, intf, u.buf, OID_802_3_PERMANENT_ADDRESS,
  476. 48, (void **) &bp, &reply_len);
  477. if (unlikely(retval< 0)) {
  478. dev_err(&intf->dev, "rndis get ethaddr, %d\n", retval);
  479. goto fail_and_release;
  480. }
  481. memcpy(net->dev_addr, bp, ETH_ALEN);
  482. /* set a nonzero filter to enable data transfers */
  483. memset(u.set, 0, sizeof *u.set);
  484. u.set->msg_type = RNDIS_MSG_SET;
  485. u.set->msg_len = ccpu2(4 + sizeof *u.set);
  486. u.set->oid = OID_GEN_CURRENT_PACKET_FILTER;
  487. u.set->len = ccpu2(4);
  488. u.set->offset = ccpu2((sizeof *u.set) - 8);
  489. *(__le32 *)(u.buf + sizeof *u.set) = ccpu2(DEFAULT_FILTER);
  490. retval = rndis_command(dev, u.header);
  491. if (unlikely(retval < 0)) {
  492. dev_err(&intf->dev, "rndis set packet filter, %d\n", retval);
  493. goto fail_and_release;
  494. }
  495. retval = 0;
  496. kfree(u.buf);
  497. return retval;
  498. fail_and_release:
  499. usb_set_intfdata(info->data, NULL);
  500. usb_driver_release_interface(driver_of(intf), info->data);
  501. info->data = NULL;
  502. fail:
  503. kfree(u.buf);
  504. return retval;
  505. }
  506. static void rndis_unbind(struct usbnet *dev, struct usb_interface *intf)
  507. {
  508. struct rndis_halt *halt;
  509. /* try to clear any rndis state/activity (no i/o from stack!) */
  510. halt = kzalloc(sizeof *halt, GFP_KERNEL);
  511. if (halt) {
  512. halt->msg_type = RNDIS_MSG_HALT;
  513. halt->msg_len = ccpu2(sizeof *halt);
  514. (void) rndis_command(dev, (void *)halt);
  515. kfree(halt);
  516. }
  517. return usbnet_cdc_unbind(dev, intf);
  518. }
  519. /*
  520. * DATA -- host must not write zlps
  521. */
  522. static int rndis_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
  523. {
  524. /* peripheral may have batched packets to us... */
  525. while (likely(skb->len)) {
  526. struct rndis_data_hdr *hdr = (void *)skb->data;
  527. struct sk_buff *skb2;
  528. u32 msg_len, data_offset, data_len;
  529. msg_len = le32_to_cpu(hdr->msg_len);
  530. data_offset = le32_to_cpu(hdr->data_offset);
  531. data_len = le32_to_cpu(hdr->data_len);
  532. /* don't choke if we see oob, per-packet data, etc */
  533. if (unlikely(hdr->msg_type != RNDIS_MSG_PACKET
  534. || skb->len < msg_len
  535. || (data_offset + data_len + 8) > msg_len)) {
  536. dev->stats.rx_frame_errors++;
  537. devdbg(dev, "bad rndis message %d/%d/%d/%d, len %d",
  538. le32_to_cpu(hdr->msg_type),
  539. msg_len, data_offset, data_len, skb->len);
  540. return 0;
  541. }
  542. skb_pull(skb, 8 + data_offset);
  543. /* at most one packet left? */
  544. if (likely((data_len - skb->len) <= sizeof *hdr)) {
  545. skb_trim(skb, data_len);
  546. break;
  547. }
  548. /* try to return all the packets in the batch */
  549. skb2 = skb_clone(skb, GFP_ATOMIC);
  550. if (unlikely(!skb2))
  551. break;
  552. skb_pull(skb, msg_len - sizeof *hdr);
  553. skb_trim(skb2, data_len);
  554. usbnet_skb_return(dev, skb2);
  555. }
  556. /* caller will usbnet_skb_return the remaining packet */
  557. return 1;
  558. }
  559. static struct sk_buff *
  560. rndis_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags)
  561. {
  562. struct rndis_data_hdr *hdr;
  563. struct sk_buff *skb2;
  564. unsigned len = skb->len;
  565. if (likely(!skb_cloned(skb))) {
  566. int room = skb_headroom(skb);
  567. /* enough head room as-is? */
  568. if (unlikely((sizeof *hdr) <= room))
  569. goto fill;
  570. /* enough room, but needs to be readjusted? */
  571. room += skb_tailroom(skb);
  572. if (likely((sizeof *hdr) <= room)) {
  573. skb->data = memmove(skb->head + sizeof *hdr,
  574. skb->data, len);
  575. skb_set_tail_pointer(skb, len);
  576. goto fill;
  577. }
  578. }
  579. /* create a new skb, with the correct size (and tailpad) */
  580. skb2 = skb_copy_expand(skb, sizeof *hdr, 1, flags);
  581. dev_kfree_skb_any(skb);
  582. if (unlikely(!skb2))
  583. return skb2;
  584. skb = skb2;
  585. /* fill out the RNDIS header. we won't bother trying to batch
  586. * packets; Linux minimizes wasted bandwidth through tx queues.
  587. */
  588. fill:
  589. hdr = (void *) __skb_push(skb, sizeof *hdr);
  590. memset(hdr, 0, sizeof *hdr);
  591. hdr->msg_type = RNDIS_MSG_PACKET;
  592. hdr->msg_len = cpu_to_le32(skb->len);
  593. hdr->data_offset = ccpu2(sizeof(*hdr) - 8);
  594. hdr->data_len = cpu_to_le32(len);
  595. /* FIXME make the last packet always be short ... */
  596. return skb;
  597. }
  598. static const struct driver_info rndis_info = {
  599. .description = "RNDIS device",
  600. .flags = FLAG_ETHER | FLAG_FRAMING_RN | FLAG_NO_SETINT,
  601. .bind = rndis_bind,
  602. .unbind = rndis_unbind,
  603. .status = rndis_status,
  604. .rx_fixup = rndis_rx_fixup,
  605. .tx_fixup = rndis_tx_fixup,
  606. };
  607. #undef ccpu2
  608. /*-------------------------------------------------------------------------*/
  609. static const struct usb_device_id products [] = {
  610. {
  611. /* RNDIS is MSFT's un-official variant of CDC ACM */
  612. USB_INTERFACE_INFO(USB_CLASS_COMM, 2 /* ACM */, 0x0ff),
  613. .driver_info = (unsigned long) &rndis_info,
  614. }, {
  615. /* "ActiveSync" is an undocumented variant of RNDIS, used in WM5 */
  616. USB_INTERFACE_INFO(USB_CLASS_MISC, 1, 1),
  617. .driver_info = (unsigned long) &rndis_info,
  618. },
  619. { }, // END
  620. };
  621. MODULE_DEVICE_TABLE(usb, products);
  622. static struct usb_driver rndis_driver = {
  623. .name = "rndis_host",
  624. .id_table = products,
  625. .probe = usbnet_probe,
  626. .disconnect = usbnet_disconnect,
  627. .suspend = usbnet_suspend,
  628. .resume = usbnet_resume,
  629. };
  630. static int __init rndis_init(void)
  631. {
  632. return usb_register(&rndis_driver);
  633. }
  634. module_init(rndis_init);
  635. static void __exit rndis_exit(void)
  636. {
  637. usb_deregister(&rndis_driver);
  638. }
  639. module_exit(rndis_exit);
  640. MODULE_AUTHOR("David Brownell");
  641. MODULE_DESCRIPTION("USB Host side RNDIS driver");
  642. MODULE_LICENSE("GPL");