rndis.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315
  1. /*
  2. * RNDIS MSG parser
  3. *
  4. * Authors: Benedikt Spranger, Pengutronix
  5. * Robert Schwebel, Pengutronix
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * version 2, as published by the Free Software Foundation.
  10. *
  11. * This software was originally developed in conformance with
  12. * Microsoft's Remote NDIS Specification License Agreement.
  13. *
  14. * 03/12/2004 Kai-Uwe Bloem <linux-development@auerswald.de>
  15. * Fixed message length bug in init_response
  16. *
  17. * 03/25/2004 Kai-Uwe Bloem <linux-development@auerswald.de>
  18. * Fixed rndis_rm_hdr length bug.
  19. *
  20. * Copyright (C) 2004 by David Brownell
  21. * updates to merge with Linux 2.6, better match RNDIS spec
  22. */
  23. #include <common.h>
  24. #include <net.h>
  25. #include <malloc.h>
  26. #include <linux/types.h>
  27. #include <linux/list.h>
  28. #include <linux/netdevice.h>
  29. #include <asm/byteorder.h>
  30. #include <asm/unaligned.h>
  31. #include <asm/errno.h>
  32. #undef RNDIS_PM
  33. #undef RNDIS_WAKEUP
  34. #undef VERBOSE
  35. #include "rndis.h"
  36. #define ETH_ALEN 6 /* Octets in one ethernet addr */
  37. #define ETH_HLEN 14 /* Total octets in header. */
  38. #define ETH_ZLEN 60 /* Min. octets in frame sans FCS */
  39. #define ETH_DATA_LEN 1500 /* Max. octets in payload */
  40. #define ETH_FRAME_LEN PKTSIZE_ALIGN /* Max. octets in frame sans FCS */
  41. #define ENOTSUPP 524 /* Operation is not supported */
  42. /*
  43. * The driver for your USB chip needs to support ep0 OUT to work with
  44. * RNDIS, plus all three CDC Ethernet endpoints (interrupt not optional).
  45. *
  46. * Windows hosts need an INF file like Documentation/usb/linux.inf
  47. * and will be happier if you provide the host_addr module parameter.
  48. */
  49. #define RNDIS_MAX_CONFIGS 1
  50. static rndis_params rndis_per_dev_params[RNDIS_MAX_CONFIGS];
  51. /* Driver Version */
  52. static const __le32 rndis_driver_version = __constant_cpu_to_le32(1);
  53. /* Function Prototypes */
  54. static rndis_resp_t *rndis_add_response(int configNr, u32 length);
  55. /* supported OIDs */
  56. static const u32 oid_supported_list[] = {
  57. /* the general stuff */
  58. OID_GEN_SUPPORTED_LIST,
  59. OID_GEN_HARDWARE_STATUS,
  60. OID_GEN_MEDIA_SUPPORTED,
  61. OID_GEN_MEDIA_IN_USE,
  62. OID_GEN_MAXIMUM_FRAME_SIZE,
  63. OID_GEN_LINK_SPEED,
  64. OID_GEN_TRANSMIT_BLOCK_SIZE,
  65. OID_GEN_RECEIVE_BLOCK_SIZE,
  66. OID_GEN_VENDOR_ID,
  67. OID_GEN_VENDOR_DESCRIPTION,
  68. OID_GEN_VENDOR_DRIVER_VERSION,
  69. OID_GEN_CURRENT_PACKET_FILTER,
  70. OID_GEN_MAXIMUM_TOTAL_SIZE,
  71. OID_GEN_MEDIA_CONNECT_STATUS,
  72. OID_GEN_PHYSICAL_MEDIUM,
  73. #if 0
  74. OID_GEN_RNDIS_CONFIG_PARAMETER,
  75. #endif
  76. /* the statistical stuff */
  77. OID_GEN_XMIT_OK,
  78. OID_GEN_RCV_OK,
  79. OID_GEN_XMIT_ERROR,
  80. OID_GEN_RCV_ERROR,
  81. OID_GEN_RCV_NO_BUFFER,
  82. #ifdef RNDIS_OPTIONAL_STATS
  83. OID_GEN_DIRECTED_BYTES_XMIT,
  84. OID_GEN_DIRECTED_FRAMES_XMIT,
  85. OID_GEN_MULTICAST_BYTES_XMIT,
  86. OID_GEN_MULTICAST_FRAMES_XMIT,
  87. OID_GEN_BROADCAST_BYTES_XMIT,
  88. OID_GEN_BROADCAST_FRAMES_XMIT,
  89. OID_GEN_DIRECTED_BYTES_RCV,
  90. OID_GEN_DIRECTED_FRAMES_RCV,
  91. OID_GEN_MULTICAST_BYTES_RCV,
  92. OID_GEN_MULTICAST_FRAMES_RCV,
  93. OID_GEN_BROADCAST_BYTES_RCV,
  94. OID_GEN_BROADCAST_FRAMES_RCV,
  95. OID_GEN_RCV_CRC_ERROR,
  96. OID_GEN_TRANSMIT_QUEUE_LENGTH,
  97. #endif /* RNDIS_OPTIONAL_STATS */
  98. /* mandatory 802.3 */
  99. /* the general stuff */
  100. OID_802_3_PERMANENT_ADDRESS,
  101. OID_802_3_CURRENT_ADDRESS,
  102. OID_802_3_MULTICAST_LIST,
  103. OID_802_3_MAC_OPTIONS,
  104. OID_802_3_MAXIMUM_LIST_SIZE,
  105. /* the statistical stuff */
  106. OID_802_3_RCV_ERROR_ALIGNMENT,
  107. OID_802_3_XMIT_ONE_COLLISION,
  108. OID_802_3_XMIT_MORE_COLLISIONS,
  109. #ifdef RNDIS_OPTIONAL_STATS
  110. OID_802_3_XMIT_DEFERRED,
  111. OID_802_3_XMIT_MAX_COLLISIONS,
  112. OID_802_3_RCV_OVERRUN,
  113. OID_802_3_XMIT_UNDERRUN,
  114. OID_802_3_XMIT_HEARTBEAT_FAILURE,
  115. OID_802_3_XMIT_TIMES_CRS_LOST,
  116. OID_802_3_XMIT_LATE_COLLISIONS,
  117. #endif /* RNDIS_OPTIONAL_STATS */
  118. #ifdef RNDIS_PM
  119. /* PM and wakeup are mandatory for USB: */
  120. /* power management */
  121. OID_PNP_CAPABILITIES,
  122. OID_PNP_QUERY_POWER,
  123. OID_PNP_SET_POWER,
  124. #ifdef RNDIS_WAKEUP
  125. /* wake up host */
  126. OID_PNP_ENABLE_WAKE_UP,
  127. OID_PNP_ADD_WAKE_UP_PATTERN,
  128. OID_PNP_REMOVE_WAKE_UP_PATTERN,
  129. #endif /* RNDIS_WAKEUP */
  130. #endif /* RNDIS_PM */
  131. };
  132. /* NDIS Functions */
  133. static int gen_ndis_query_resp(int configNr, u32 OID, u8 *buf,
  134. unsigned buf_len, rndis_resp_t *r)
  135. {
  136. int retval = -ENOTSUPP;
  137. u32 length = 4; /* usually */
  138. __le32 *outbuf;
  139. int i, count;
  140. rndis_query_cmplt_type *resp;
  141. rndis_params *params;
  142. if (!r)
  143. return -ENOMEM;
  144. resp = (rndis_query_cmplt_type *) r->buf;
  145. if (!resp)
  146. return -ENOMEM;
  147. #if defined(DEBUG) && defined(DEBUG_VERBOSE)
  148. if (buf_len) {
  149. debug("query OID %08x value, len %d:\n", OID, buf_len);
  150. for (i = 0; i < buf_len; i += 16) {
  151. debug("%03d: %08x %08x %08x %08x\n", i,
  152. get_unaligned_le32(&buf[i]),
  153. get_unaligned_le32(&buf[i + 4]),
  154. get_unaligned_le32(&buf[i + 8]),
  155. get_unaligned_le32(&buf[i + 12]));
  156. }
  157. }
  158. #endif
  159. /* response goes here, right after the header */
  160. outbuf = (__le32 *) &resp[1];
  161. resp->InformationBufferOffset = __constant_cpu_to_le32(16);
  162. params = &rndis_per_dev_params[configNr];
  163. switch (OID) {
  164. /* general oids (table 4-1) */
  165. /* mandatory */
  166. case OID_GEN_SUPPORTED_LIST:
  167. debug("%s: OID_GEN_SUPPORTED_LIST\n", __func__);
  168. length = sizeof(oid_supported_list);
  169. count = length / sizeof(u32);
  170. for (i = 0; i < count; i++)
  171. outbuf[i] = cpu_to_le32(oid_supported_list[i]);
  172. retval = 0;
  173. break;
  174. /* mandatory */
  175. case OID_GEN_HARDWARE_STATUS:
  176. debug("%s: OID_GEN_HARDWARE_STATUS\n", __func__);
  177. /*
  178. * Bogus question!
  179. * Hardware must be ready to receive high level protocols.
  180. * BTW:
  181. * reddite ergo quae sunt Caesaris Caesari
  182. * et quae sunt Dei Deo!
  183. */
  184. *outbuf = __constant_cpu_to_le32(0);
  185. retval = 0;
  186. break;
  187. /* mandatory */
  188. case OID_GEN_MEDIA_SUPPORTED:
  189. debug("%s: OID_GEN_MEDIA_SUPPORTED\n", __func__);
  190. *outbuf = cpu_to_le32(params->medium);
  191. retval = 0;
  192. break;
  193. /* mandatory */
  194. case OID_GEN_MEDIA_IN_USE:
  195. debug("%s: OID_GEN_MEDIA_IN_USE\n", __func__);
  196. /* one medium, one transport... (maybe you do it better) */
  197. *outbuf = cpu_to_le32(params->medium);
  198. retval = 0;
  199. break;
  200. /* mandatory */
  201. case OID_GEN_MAXIMUM_FRAME_SIZE:
  202. debug("%s: OID_GEN_MAXIMUM_FRAME_SIZE\n", __func__);
  203. if (params->dev) {
  204. *outbuf = cpu_to_le32(params->mtu);
  205. retval = 0;
  206. }
  207. break;
  208. /* mandatory */
  209. case OID_GEN_LINK_SPEED:
  210. #if defined(DEBUG) && defined(DEBUG_VERBOSE)
  211. debug("%s: OID_GEN_LINK_SPEED\n", __func__);
  212. #endif
  213. if (params->media_state == NDIS_MEDIA_STATE_DISCONNECTED)
  214. *outbuf = __constant_cpu_to_le32(0);
  215. else
  216. *outbuf = cpu_to_le32(params->speed);
  217. retval = 0;
  218. break;
  219. /* mandatory */
  220. case OID_GEN_TRANSMIT_BLOCK_SIZE:
  221. debug("%s: OID_GEN_TRANSMIT_BLOCK_SIZE\n", __func__);
  222. if (params->dev) {
  223. *outbuf = cpu_to_le32(params->mtu);
  224. retval = 0;
  225. }
  226. break;
  227. /* mandatory */
  228. case OID_GEN_RECEIVE_BLOCK_SIZE:
  229. debug("%s: OID_GEN_RECEIVE_BLOCK_SIZE\n", __func__);
  230. if (params->dev) {
  231. *outbuf = cpu_to_le32(params->mtu);
  232. retval = 0;
  233. }
  234. break;
  235. /* mandatory */
  236. case OID_GEN_VENDOR_ID:
  237. debug("%s: OID_GEN_VENDOR_ID\n", __func__);
  238. *outbuf = cpu_to_le32(params->vendorID);
  239. retval = 0;
  240. break;
  241. /* mandatory */
  242. case OID_GEN_VENDOR_DESCRIPTION:
  243. debug("%s: OID_GEN_VENDOR_DESCRIPTION\n", __func__);
  244. length = strlen(params->vendorDescr);
  245. memcpy(outbuf, params->vendorDescr, length);
  246. retval = 0;
  247. break;
  248. case OID_GEN_VENDOR_DRIVER_VERSION:
  249. debug("%s: OID_GEN_VENDOR_DRIVER_VERSION\n", __func__);
  250. /* Created as LE */
  251. *outbuf = rndis_driver_version;
  252. retval = 0;
  253. break;
  254. /* mandatory */
  255. case OID_GEN_CURRENT_PACKET_FILTER:
  256. debug("%s: OID_GEN_CURRENT_PACKET_FILTER\n", __func__);
  257. *outbuf = cpu_to_le32(*params->filter);
  258. retval = 0;
  259. break;
  260. /* mandatory */
  261. case OID_GEN_MAXIMUM_TOTAL_SIZE:
  262. debug("%s: OID_GEN_MAXIMUM_TOTAL_SIZE\n", __func__);
  263. *outbuf = __constant_cpu_to_le32(RNDIS_MAX_TOTAL_SIZE);
  264. retval = 0;
  265. break;
  266. /* mandatory */
  267. case OID_GEN_MEDIA_CONNECT_STATUS:
  268. #if defined(DEBUG) && defined(DEBUG_VERBOSE)
  269. debug("%s: OID_GEN_MEDIA_CONNECT_STATUS\n", __func__);
  270. #endif
  271. *outbuf = cpu_to_le32(params->media_state);
  272. retval = 0;
  273. break;
  274. case OID_GEN_PHYSICAL_MEDIUM:
  275. debug("%s: OID_GEN_PHYSICAL_MEDIUM\n", __func__);
  276. *outbuf = __constant_cpu_to_le32(0);
  277. retval = 0;
  278. break;
  279. /*
  280. * The RNDIS specification is incomplete/wrong. Some versions
  281. * of MS-Windows expect OIDs that aren't specified there. Other
  282. * versions emit undefined RNDIS messages. DOCUMENT ALL THESE!
  283. */
  284. case OID_GEN_MAC_OPTIONS: /* from WinME */
  285. debug("%s: OID_GEN_MAC_OPTIONS\n", __func__);
  286. *outbuf = __constant_cpu_to_le32(
  287. NDIS_MAC_OPTION_RECEIVE_SERIALIZED
  288. | NDIS_MAC_OPTION_FULL_DUPLEX);
  289. retval = 0;
  290. break;
  291. /* statistics OIDs (table 4-2) */
  292. /* mandatory */
  293. case OID_GEN_XMIT_OK:
  294. #if defined(DEBUG) && defined(DEBUG_VERBOSE)
  295. debug("%s: OID_GEN_XMIT_OK\n", __func__);
  296. #endif
  297. if (params->stats) {
  298. *outbuf = cpu_to_le32(
  299. params->stats->tx_packets -
  300. params->stats->tx_errors -
  301. params->stats->tx_dropped);
  302. retval = 0;
  303. }
  304. break;
  305. /* mandatory */
  306. case OID_GEN_RCV_OK:
  307. #if defined(DEBUG) && defined(DEBUG_VERBOSE)
  308. debug("%s: OID_GEN_RCV_OK\n", __func__);
  309. #endif
  310. if (params->stats) {
  311. *outbuf = cpu_to_le32(
  312. params->stats->rx_packets -
  313. params->stats->rx_errors -
  314. params->stats->rx_dropped);
  315. retval = 0;
  316. }
  317. break;
  318. /* mandatory */
  319. case OID_GEN_XMIT_ERROR:
  320. #if defined(DEBUG) && defined(DEBUG_VERBOSE)
  321. debug("%s: OID_GEN_XMIT_ERROR\n", __func__);
  322. #endif
  323. if (params->stats) {
  324. *outbuf = cpu_to_le32(params->stats->tx_errors);
  325. retval = 0;
  326. }
  327. break;
  328. /* mandatory */
  329. case OID_GEN_RCV_ERROR:
  330. #if defined(DEBUG) && defined(DEBUG_VERBOSE)
  331. debug("%s: OID_GEN_RCV_ERROR\n", __func__);
  332. #endif
  333. if (params->stats) {
  334. *outbuf = cpu_to_le32(params->stats->rx_errors);
  335. retval = 0;
  336. }
  337. break;
  338. /* mandatory */
  339. case OID_GEN_RCV_NO_BUFFER:
  340. debug("%s: OID_GEN_RCV_NO_BUFFER\n", __func__);
  341. if (params->stats) {
  342. *outbuf = cpu_to_le32(params->stats->rx_dropped);
  343. retval = 0;
  344. }
  345. break;
  346. #ifdef RNDIS_OPTIONAL_STATS
  347. case OID_GEN_DIRECTED_BYTES_XMIT:
  348. debug("%s: OID_GEN_DIRECTED_BYTES_XMIT\n", __func__);
  349. /*
  350. * Aunt Tilly's size of shoes
  351. * minus antarctica count of penguins
  352. * divided by weight of Alpha Centauri
  353. */
  354. if (params->stats) {
  355. *outbuf = cpu_to_le32(
  356. (params->stats->tx_packets -
  357. params->stats->tx_errors -
  358. params->stats->tx_dropped)
  359. * 123);
  360. retval = 0;
  361. }
  362. break;
  363. case OID_GEN_DIRECTED_FRAMES_XMIT:
  364. debug("%s: OID_GEN_DIRECTED_FRAMES_XMIT\n", __func__);
  365. /* dito */
  366. if (params->stats) {
  367. *outbuf = cpu_to_le32(
  368. (params->stats->tx_packets -
  369. params->stats->tx_errors -
  370. params->stats->tx_dropped)
  371. / 123);
  372. retval = 0;
  373. }
  374. break;
  375. case OID_GEN_MULTICAST_BYTES_XMIT:
  376. debug("%s: OID_GEN_MULTICAST_BYTES_XMIT\n", __func__);
  377. if (params->stats) {
  378. *outbuf = cpu_to_le32(params->stats->multicast * 1234);
  379. retval = 0;
  380. }
  381. break;
  382. case OID_GEN_MULTICAST_FRAMES_XMIT:
  383. debug("%s: OID_GEN_MULTICAST_FRAMES_XMIT\n", __func__);
  384. if (params->stats) {
  385. *outbuf = cpu_to_le32(params->stats->multicast);
  386. retval = 0;
  387. }
  388. break;
  389. case OID_GEN_BROADCAST_BYTES_XMIT:
  390. debug("%s: OID_GEN_BROADCAST_BYTES_XMIT\n", __func__);
  391. if (params->stats) {
  392. *outbuf = cpu_to_le32(params->stats->tx_packets/42*255);
  393. retval = 0;
  394. }
  395. break;
  396. case OID_GEN_BROADCAST_FRAMES_XMIT:
  397. debug("%s: OID_GEN_BROADCAST_FRAMES_XMIT\n", __func__);
  398. if (params->stats) {
  399. *outbuf = cpu_to_le32(params->stats->tx_packets / 42);
  400. retval = 0;
  401. }
  402. break;
  403. case OID_GEN_DIRECTED_BYTES_RCV:
  404. debug("%s: OID_GEN_DIRECTED_BYTES_RCV\n", __func__);
  405. *outbuf = __constant_cpu_to_le32(0);
  406. retval = 0;
  407. break;
  408. case OID_GEN_DIRECTED_FRAMES_RCV:
  409. debug("%s: OID_GEN_DIRECTED_FRAMES_RCV\n", __func__);
  410. *outbuf = __constant_cpu_to_le32(0);
  411. retval = 0;
  412. break;
  413. case OID_GEN_MULTICAST_BYTES_RCV:
  414. debug("%s: OID_GEN_MULTICAST_BYTES_RCV\n", __func__);
  415. if (params->stats) {
  416. *outbuf = cpu_to_le32(params->stats->multicast * 1111);
  417. retval = 0;
  418. }
  419. break;
  420. case OID_GEN_MULTICAST_FRAMES_RCV:
  421. debug("%s: OID_GEN_MULTICAST_FRAMES_RCV\n", __func__);
  422. if (params->stats) {
  423. *outbuf = cpu_to_le32(params->stats->multicast);
  424. retval = 0;
  425. }
  426. break;
  427. case OID_GEN_BROADCAST_BYTES_RCV:
  428. debug("%s: OID_GEN_BROADCAST_BYTES_RCV\n", __func__);
  429. if (params->stats) {
  430. *outbuf = cpu_to_le32(params->stats->rx_packets/42*255);
  431. retval = 0;
  432. }
  433. break;
  434. case OID_GEN_BROADCAST_FRAMES_RCV:
  435. debug("%s: OID_GEN_BROADCAST_FRAMES_RCV\n", __func__);
  436. if (params->stats) {
  437. *outbuf = cpu_to_le32(params->stats->rx_packets / 42);
  438. retval = 0;
  439. }
  440. break;
  441. case OID_GEN_RCV_CRC_ERROR:
  442. debug("%s: OID_GEN_RCV_CRC_ERROR\n", __func__);
  443. if (params->stats) {
  444. *outbuf = cpu_to_le32(params->stats->rx_crc_errors);
  445. retval = 0;
  446. }
  447. break;
  448. case OID_GEN_TRANSMIT_QUEUE_LENGTH:
  449. debug("%s: OID_GEN_TRANSMIT_QUEUE_LENGTH\n", __func__);
  450. *outbuf = __constant_cpu_to_le32(0);
  451. retval = 0;
  452. break;
  453. #endif /* RNDIS_OPTIONAL_STATS */
  454. /* ieee802.3 OIDs (table 4-3) */
  455. /* mandatory */
  456. case OID_802_3_PERMANENT_ADDRESS:
  457. debug("%s: OID_802_3_PERMANENT_ADDRESS\n", __func__);
  458. if (params->dev) {
  459. length = ETH_ALEN;
  460. memcpy(outbuf, params->host_mac, length);
  461. retval = 0;
  462. }
  463. break;
  464. /* mandatory */
  465. case OID_802_3_CURRENT_ADDRESS:
  466. debug("%s: OID_802_3_CURRENT_ADDRESS\n", __func__);
  467. if (params->dev) {
  468. length = ETH_ALEN;
  469. memcpy(outbuf, params->host_mac, length);
  470. retval = 0;
  471. }
  472. break;
  473. /* mandatory */
  474. case OID_802_3_MULTICAST_LIST:
  475. debug("%s: OID_802_3_MULTICAST_LIST\n", __func__);
  476. /* Multicast base address only */
  477. *outbuf = __constant_cpu_to_le32(0xE0000000);
  478. retval = 0;
  479. break;
  480. /* mandatory */
  481. case OID_802_3_MAXIMUM_LIST_SIZE:
  482. debug("%s: OID_802_3_MAXIMUM_LIST_SIZE\n", __func__);
  483. /* Multicast base address only */
  484. *outbuf = __constant_cpu_to_le32(1);
  485. retval = 0;
  486. break;
  487. case OID_802_3_MAC_OPTIONS:
  488. debug("%s: OID_802_3_MAC_OPTIONS\n", __func__);
  489. break;
  490. /* ieee802.3 statistics OIDs (table 4-4) */
  491. /* mandatory */
  492. case OID_802_3_RCV_ERROR_ALIGNMENT:
  493. debug("%s: OID_802_3_RCV_ERROR_ALIGNMENT\n", __func__);
  494. if (params->stats) {
  495. *outbuf = cpu_to_le32(params->stats->rx_frame_errors);
  496. retval = 0;
  497. }
  498. break;
  499. /* mandatory */
  500. case OID_802_3_XMIT_ONE_COLLISION:
  501. debug("%s: OID_802_3_XMIT_ONE_COLLISION\n", __func__);
  502. *outbuf = __constant_cpu_to_le32(0);
  503. retval = 0;
  504. break;
  505. /* mandatory */
  506. case OID_802_3_XMIT_MORE_COLLISIONS:
  507. debug("%s: OID_802_3_XMIT_MORE_COLLISIONS\n", __func__);
  508. *outbuf = __constant_cpu_to_le32(0);
  509. retval = 0;
  510. break;
  511. #ifdef RNDIS_OPTIONAL_STATS
  512. case OID_802_3_XMIT_DEFERRED:
  513. debug("%s: OID_802_3_XMIT_DEFERRED\n", __func__);
  514. /* TODO */
  515. break;
  516. case OID_802_3_XMIT_MAX_COLLISIONS:
  517. debug("%s: OID_802_3_XMIT_MAX_COLLISIONS\n", __func__);
  518. /* TODO */
  519. break;
  520. case OID_802_3_RCV_OVERRUN:
  521. debug("%s: OID_802_3_RCV_OVERRUN\n", __func__);
  522. /* TODO */
  523. break;
  524. case OID_802_3_XMIT_UNDERRUN:
  525. debug("%s: OID_802_3_XMIT_UNDERRUN\n", __func__);
  526. /* TODO */
  527. break;
  528. case OID_802_3_XMIT_HEARTBEAT_FAILURE:
  529. debug("%s: OID_802_3_XMIT_HEARTBEAT_FAILURE\n", __func__);
  530. /* TODO */
  531. break;
  532. case OID_802_3_XMIT_TIMES_CRS_LOST:
  533. debug("%s: OID_802_3_XMIT_TIMES_CRS_LOST\n", __func__);
  534. /* TODO */
  535. break;
  536. case OID_802_3_XMIT_LATE_COLLISIONS:
  537. debug("%s: OID_802_3_XMIT_LATE_COLLISIONS\n", __func__);
  538. /* TODO */
  539. break;
  540. #endif /* RNDIS_OPTIONAL_STATS */
  541. #ifdef RNDIS_PM
  542. /* power management OIDs (table 4-5) */
  543. case OID_PNP_CAPABILITIES:
  544. debug("%s: OID_PNP_CAPABILITIES\n", __func__);
  545. /* for now, no wakeup capabilities */
  546. length = sizeof(struct NDIS_PNP_CAPABILITIES);
  547. memset(outbuf, 0, length);
  548. retval = 0;
  549. break;
  550. case OID_PNP_QUERY_POWER:
  551. debug("%s: OID_PNP_QUERY_POWER D%d\n", __func__,
  552. get_unaligned_le32(buf) - 1);
  553. /*
  554. * only suspend is a real power state, and
  555. * it can't be entered by OID_PNP_SET_POWER...
  556. */
  557. length = 0;
  558. retval = 0;
  559. break;
  560. #endif
  561. default:
  562. debug("%s: query unknown OID 0x%08X\n", __func__, OID);
  563. }
  564. if (retval < 0)
  565. length = 0;
  566. resp->InformationBufferLength = cpu_to_le32(length);
  567. r->length = length + sizeof *resp;
  568. resp->MessageLength = cpu_to_le32(r->length);
  569. return retval;
  570. }
  571. static int gen_ndis_set_resp(u8 configNr, u32 OID, u8 *buf, u32 buf_len,
  572. rndis_resp_t *r)
  573. {
  574. rndis_set_cmplt_type *resp;
  575. int retval = -ENOTSUPP;
  576. struct rndis_params *params;
  577. #if (defined(DEBUG) && defined(DEBUG_VERBOSE)) || defined(RNDIS_PM)
  578. int i;
  579. #endif
  580. if (!r)
  581. return -ENOMEM;
  582. resp = (rndis_set_cmplt_type *) r->buf;
  583. if (!resp)
  584. return -ENOMEM;
  585. #if defined(DEBUG) && defined(DEBUG_VERBOSE)
  586. if (buf_len) {
  587. debug("set OID %08x value, len %d:\n", OID, buf_len);
  588. for (i = 0; i < buf_len; i += 16) {
  589. debug("%03d: %08x %08x %08x %08x\n", i,
  590. get_unaligned_le32(&buf[i]),
  591. get_unaligned_le32(&buf[i + 4]),
  592. get_unaligned_le32(&buf[i + 8]),
  593. get_unaligned_le32(&buf[i + 12]));
  594. }
  595. }
  596. #endif
  597. params = &rndis_per_dev_params[configNr];
  598. switch (OID) {
  599. case OID_GEN_CURRENT_PACKET_FILTER:
  600. /*
  601. * these NDIS_PACKET_TYPE_* bitflags are shared with
  602. * cdc_filter; it's not RNDIS-specific
  603. * NDIS_PACKET_TYPE_x == USB_CDC_PACKET_TYPE_x for x in:
  604. * PROMISCUOUS, DIRECTED,
  605. * MULTICAST, ALL_MULTICAST, BROADCAST
  606. */
  607. *params->filter = (u16) get_unaligned_le32(buf);
  608. debug("%s: OID_GEN_CURRENT_PACKET_FILTER %08x\n",
  609. __func__, *params->filter);
  610. /*
  611. * this call has a significant side effect: it's
  612. * what makes the packet flow start and stop, like
  613. * activating the CDC Ethernet altsetting.
  614. */
  615. #ifdef RNDIS_PM
  616. update_linkstate:
  617. #endif
  618. retval = 0;
  619. if (*params->filter)
  620. params->state = RNDIS_DATA_INITIALIZED;
  621. else
  622. params->state = RNDIS_INITIALIZED;
  623. break;
  624. case OID_802_3_MULTICAST_LIST:
  625. /* I think we can ignore this */
  626. debug("%s: OID_802_3_MULTICAST_LIST\n", __func__);
  627. retval = 0;
  628. break;
  629. #if 0
  630. case OID_GEN_RNDIS_CONFIG_PARAMETER:
  631. {
  632. struct rndis_config_parameter *param;
  633. param = (struct rndis_config_parameter *) buf;
  634. debug("%s: OID_GEN_RNDIS_CONFIG_PARAMETER '%*s'\n",
  635. __func__,
  636. min(cpu_to_le32(param->ParameterNameLength), 80),
  637. buf + param->ParameterNameOffset);
  638. retval = 0;
  639. }
  640. break;
  641. #endif
  642. #ifdef RNDIS_PM
  643. case OID_PNP_SET_POWER:
  644. /*
  645. * The only real power state is USB suspend, and RNDIS requests
  646. * can't enter it; this one isn't really about power. After
  647. * resuming, Windows forces a reset, and then SET_POWER D0.
  648. * FIXME ... then things go batty; Windows wedges itself.
  649. */
  650. i = get_unaligned_le32(buf);
  651. debug("%s: OID_PNP_SET_POWER D%d\n", __func__, i - 1);
  652. switch (i) {
  653. case NdisDeviceStateD0:
  654. *params->filter = params->saved_filter;
  655. goto update_linkstate;
  656. case NdisDeviceStateD3:
  657. case NdisDeviceStateD2:
  658. case NdisDeviceStateD1:
  659. params->saved_filter = *params->filter;
  660. retval = 0;
  661. break;
  662. }
  663. break;
  664. #ifdef RNDIS_WAKEUP
  665. /*
  666. * no wakeup support advertised, so wakeup OIDs always fail:
  667. * - OID_PNP_ENABLE_WAKE_UP
  668. * - OID_PNP_{ADD,REMOVE}_WAKE_UP_PATTERN
  669. */
  670. #endif
  671. #endif /* RNDIS_PM */
  672. default:
  673. debug("%s: set unknown OID 0x%08X, size %d\n",
  674. __func__, OID, buf_len);
  675. }
  676. return retval;
  677. }
  678. /*
  679. * Response Functions
  680. */
  681. static int rndis_init_response(int configNr, rndis_init_msg_type *buf)
  682. {
  683. rndis_init_cmplt_type *resp;
  684. rndis_resp_t *r;
  685. if (!rndis_per_dev_params[configNr].dev)
  686. return -ENOTSUPP;
  687. r = rndis_add_response(configNr, sizeof(rndis_init_cmplt_type));
  688. if (!r)
  689. return -ENOMEM;
  690. resp = (rndis_init_cmplt_type *) r->buf;
  691. resp->MessageType = __constant_cpu_to_le32(
  692. REMOTE_NDIS_INITIALIZE_CMPLT);
  693. resp->MessageLength = __constant_cpu_to_le32(52);
  694. resp->RequestID = get_unaligned(&buf->RequestID); /* Still LE in msg buffer */
  695. resp->Status = __constant_cpu_to_le32(RNDIS_STATUS_SUCCESS);
  696. resp->MajorVersion = __constant_cpu_to_le32(RNDIS_MAJOR_VERSION);
  697. resp->MinorVersion = __constant_cpu_to_le32(RNDIS_MINOR_VERSION);
  698. resp->DeviceFlags = __constant_cpu_to_le32(RNDIS_DF_CONNECTIONLESS);
  699. resp->Medium = __constant_cpu_to_le32(RNDIS_MEDIUM_802_3);
  700. resp->MaxPacketsPerTransfer = __constant_cpu_to_le32(1);
  701. resp->MaxTransferSize = cpu_to_le32(
  702. rndis_per_dev_params[configNr].mtu
  703. + ETHER_HDR_SIZE
  704. + sizeof(struct rndis_packet_msg_type)
  705. + 22);
  706. resp->PacketAlignmentFactor = __constant_cpu_to_le32(0);
  707. resp->AFListOffset = __constant_cpu_to_le32(0);
  708. resp->AFListSize = __constant_cpu_to_le32(0);
  709. if (rndis_per_dev_params[configNr].ack)
  710. rndis_per_dev_params[configNr].ack(
  711. rndis_per_dev_params[configNr].dev);
  712. return 0;
  713. }
  714. static int rndis_query_response(int configNr, rndis_query_msg_type *buf)
  715. {
  716. rndis_query_cmplt_type *resp;
  717. rndis_resp_t *r;
  718. debug("%s: OID = %08X\n", __func__, get_unaligned_le32(&buf->OID));
  719. if (!rndis_per_dev_params[configNr].dev)
  720. return -ENOTSUPP;
  721. /*
  722. * we need more memory:
  723. * gen_ndis_query_resp expects enough space for
  724. * rndis_query_cmplt_type followed by data.
  725. * oid_supported_list is the largest data reply
  726. */
  727. r = rndis_add_response(configNr,
  728. sizeof(oid_supported_list) + sizeof(rndis_query_cmplt_type));
  729. if (!r)
  730. return -ENOMEM;
  731. resp = (rndis_query_cmplt_type *) r->buf;
  732. resp->MessageType = __constant_cpu_to_le32(REMOTE_NDIS_QUERY_CMPLT);
  733. resp->RequestID = get_unaligned(&buf->RequestID); /* Still LE in msg buffer */
  734. if (gen_ndis_query_resp(configNr, get_unaligned_le32(&buf->OID),
  735. get_unaligned_le32(&buf->InformationBufferOffset)
  736. + 8 + (u8 *) buf,
  737. get_unaligned_le32(&buf->InformationBufferLength),
  738. r)) {
  739. /* OID not supported */
  740. resp->Status = __constant_cpu_to_le32(
  741. RNDIS_STATUS_NOT_SUPPORTED);
  742. resp->MessageLength = __constant_cpu_to_le32(sizeof *resp);
  743. resp->InformationBufferLength = __constant_cpu_to_le32(0);
  744. resp->InformationBufferOffset = __constant_cpu_to_le32(0);
  745. } else
  746. resp->Status = __constant_cpu_to_le32(RNDIS_STATUS_SUCCESS);
  747. if (rndis_per_dev_params[configNr].ack)
  748. rndis_per_dev_params[configNr].ack(
  749. rndis_per_dev_params[configNr].dev);
  750. return 0;
  751. }
  752. static int rndis_set_response(int configNr, rndis_set_msg_type *buf)
  753. {
  754. u32 BufLength, BufOffset;
  755. rndis_set_cmplt_type *resp;
  756. rndis_resp_t *r;
  757. r = rndis_add_response(configNr, sizeof(rndis_set_cmplt_type));
  758. if (!r)
  759. return -ENOMEM;
  760. resp = (rndis_set_cmplt_type *) r->buf;
  761. BufLength = get_unaligned_le32(&buf->InformationBufferLength);
  762. BufOffset = get_unaligned_le32(&buf->InformationBufferOffset);
  763. #ifdef VERBOSE
  764. debug("%s: Length: %d\n", __func__, BufLength);
  765. debug("%s: Offset: %d\n", __func__, BufOffset);
  766. debug("%s: InfoBuffer: ", __func__);
  767. for (i = 0; i < BufLength; i++)
  768. debug("%02x ", *(((u8 *) buf) + i + 8 + BufOffset));
  769. debug("\n");
  770. #endif
  771. resp->MessageType = __constant_cpu_to_le32(REMOTE_NDIS_SET_CMPLT);
  772. resp->MessageLength = __constant_cpu_to_le32(16);
  773. resp->RequestID = get_unaligned(&buf->RequestID); /* Still LE in msg buffer */
  774. if (gen_ndis_set_resp(configNr, get_unaligned_le32(&buf->OID),
  775. ((u8 *) buf) + 8 + BufOffset, BufLength, r))
  776. resp->Status = __constant_cpu_to_le32(
  777. RNDIS_STATUS_NOT_SUPPORTED);
  778. else
  779. resp->Status = __constant_cpu_to_le32(RNDIS_STATUS_SUCCESS);
  780. if (rndis_per_dev_params[configNr].ack)
  781. rndis_per_dev_params[configNr].ack(
  782. rndis_per_dev_params[configNr].dev);
  783. return 0;
  784. }
  785. static int rndis_reset_response(int configNr, rndis_reset_msg_type *buf)
  786. {
  787. rndis_reset_cmplt_type *resp;
  788. rndis_resp_t *r;
  789. r = rndis_add_response(configNr, sizeof(rndis_reset_cmplt_type));
  790. if (!r)
  791. return -ENOMEM;
  792. resp = (rndis_reset_cmplt_type *) r->buf;
  793. resp->MessageType = __constant_cpu_to_le32(REMOTE_NDIS_RESET_CMPLT);
  794. resp->MessageLength = __constant_cpu_to_le32(16);
  795. resp->Status = __constant_cpu_to_le32(RNDIS_STATUS_SUCCESS);
  796. /* resent information */
  797. resp->AddressingReset = __constant_cpu_to_le32(1);
  798. if (rndis_per_dev_params[configNr].ack)
  799. rndis_per_dev_params[configNr].ack(
  800. rndis_per_dev_params[configNr].dev);
  801. return 0;
  802. }
  803. static int rndis_keepalive_response(int configNr,
  804. rndis_keepalive_msg_type *buf)
  805. {
  806. rndis_keepalive_cmplt_type *resp;
  807. rndis_resp_t *r;
  808. /* host "should" check only in RNDIS_DATA_INITIALIZED state */
  809. r = rndis_add_response(configNr, sizeof(rndis_keepalive_cmplt_type));
  810. if (!r)
  811. return -ENOMEM;
  812. resp = (rndis_keepalive_cmplt_type *) r->buf;
  813. resp->MessageType = __constant_cpu_to_le32(
  814. REMOTE_NDIS_KEEPALIVE_CMPLT);
  815. resp->MessageLength = __constant_cpu_to_le32(16);
  816. resp->RequestID = get_unaligned(&buf->RequestID); /* Still LE in msg buffer */
  817. resp->Status = __constant_cpu_to_le32(RNDIS_STATUS_SUCCESS);
  818. if (rndis_per_dev_params[configNr].ack)
  819. rndis_per_dev_params[configNr].ack(
  820. rndis_per_dev_params[configNr].dev);
  821. return 0;
  822. }
  823. /*
  824. * Device to Host Comunication
  825. */
  826. static int rndis_indicate_status_msg(int configNr, u32 status)
  827. {
  828. rndis_indicate_status_msg_type *resp;
  829. rndis_resp_t *r;
  830. if (rndis_per_dev_params[configNr].state == RNDIS_UNINITIALIZED)
  831. return -ENOTSUPP;
  832. r = rndis_add_response(configNr,
  833. sizeof(rndis_indicate_status_msg_type));
  834. if (!r)
  835. return -ENOMEM;
  836. resp = (rndis_indicate_status_msg_type *) r->buf;
  837. resp->MessageType = __constant_cpu_to_le32(
  838. REMOTE_NDIS_INDICATE_STATUS_MSG);
  839. resp->MessageLength = __constant_cpu_to_le32(20);
  840. resp->Status = cpu_to_le32(status);
  841. resp->StatusBufferLength = __constant_cpu_to_le32(0);
  842. resp->StatusBufferOffset = __constant_cpu_to_le32(0);
  843. if (rndis_per_dev_params[configNr].ack)
  844. rndis_per_dev_params[configNr].ack(
  845. rndis_per_dev_params[configNr].dev);
  846. return 0;
  847. }
  848. int rndis_signal_connect(int configNr)
  849. {
  850. rndis_per_dev_params[configNr].media_state
  851. = NDIS_MEDIA_STATE_CONNECTED;
  852. return rndis_indicate_status_msg(configNr,
  853. RNDIS_STATUS_MEDIA_CONNECT);
  854. }
  855. int rndis_signal_disconnect(int configNr)
  856. {
  857. rndis_per_dev_params[configNr].media_state
  858. = NDIS_MEDIA_STATE_DISCONNECTED;
  859. #ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT
  860. return rndis_indicate_status_msg(configNr,
  861. RNDIS_STATUS_MEDIA_DISCONNECT);
  862. #else
  863. return 0;
  864. #endif
  865. }
  866. void rndis_uninit(int configNr)
  867. {
  868. u8 *buf;
  869. u32 length;
  870. if (configNr >= RNDIS_MAX_CONFIGS)
  871. return;
  872. rndis_per_dev_params[configNr].used = 0;
  873. rndis_per_dev_params[configNr].state = RNDIS_UNINITIALIZED;
  874. /* drain the response queue */
  875. while ((buf = rndis_get_next_response(configNr, &length)))
  876. rndis_free_response(configNr, buf);
  877. }
  878. void rndis_set_host_mac(int configNr, const u8 *addr)
  879. {
  880. rndis_per_dev_params[configNr].host_mac = addr;
  881. }
  882. enum rndis_state rndis_get_state(int configNr)
  883. {
  884. if (configNr >= RNDIS_MAX_CONFIGS || configNr < 0)
  885. return -ENOTSUPP;
  886. return rndis_per_dev_params[configNr].state;
  887. }
  888. /*
  889. * Message Parser
  890. */
  891. int rndis_msg_parser(u8 configNr, u8 *buf)
  892. {
  893. u32 MsgType, MsgLength;
  894. __le32 *tmp;
  895. struct rndis_params *params;
  896. debug("%s: configNr = %d, %p\n", __func__, configNr, buf);
  897. if (!buf)
  898. return -ENOMEM;
  899. tmp = (__le32 *) buf;
  900. MsgType = get_unaligned_le32(tmp++);
  901. MsgLength = get_unaligned_le32(tmp++);
  902. if (configNr >= RNDIS_MAX_CONFIGS)
  903. return -ENOTSUPP;
  904. params = &rndis_per_dev_params[configNr];
  905. /*
  906. * NOTE: RNDIS is *EXTREMELY* chatty ... Windows constantly polls for
  907. * rx/tx statistics and link status, in addition to KEEPALIVE traffic
  908. * and normal HC level polling to see if there's any IN traffic.
  909. */
  910. /* For USB: responses may take up to 10 seconds */
  911. switch (MsgType) {
  912. case REMOTE_NDIS_INITIALIZE_MSG:
  913. debug("%s: REMOTE_NDIS_INITIALIZE_MSG\n", __func__);
  914. params->state = RNDIS_INITIALIZED;
  915. return rndis_init_response(configNr,
  916. (rndis_init_msg_type *) buf);
  917. case REMOTE_NDIS_HALT_MSG:
  918. debug("%s: REMOTE_NDIS_HALT_MSG\n", __func__);
  919. params->state = RNDIS_UNINITIALIZED;
  920. return 0;
  921. case REMOTE_NDIS_QUERY_MSG:
  922. return rndis_query_response(configNr,
  923. (rndis_query_msg_type *) buf);
  924. case REMOTE_NDIS_SET_MSG:
  925. return rndis_set_response(configNr,
  926. (rndis_set_msg_type *) buf);
  927. case REMOTE_NDIS_RESET_MSG:
  928. debug("%s: REMOTE_NDIS_RESET_MSG\n", __func__);
  929. return rndis_reset_response(configNr,
  930. (rndis_reset_msg_type *) buf);
  931. case REMOTE_NDIS_KEEPALIVE_MSG:
  932. /* For USB: host does this every 5 seconds */
  933. #if defined(DEBUG) && defined(DEBUG_VERBOSE)
  934. debug("%s: REMOTE_NDIS_KEEPALIVE_MSG\n", __func__);
  935. #endif
  936. return rndis_keepalive_response(configNr,
  937. (rndis_keepalive_msg_type *) buf);
  938. default:
  939. /*
  940. * At least Windows XP emits some undefined RNDIS messages.
  941. * In one case those messages seemed to relate to the host
  942. * suspending itself.
  943. */
  944. debug("%s: unknown RNDIS message 0x%08X len %d\n",
  945. __func__ , MsgType, MsgLength);
  946. {
  947. unsigned i;
  948. for (i = 0; i < MsgLength; i += 16) {
  949. debug("%03d: "
  950. " %02x %02x %02x %02x"
  951. " %02x %02x %02x %02x"
  952. " %02x %02x %02x %02x"
  953. " %02x %02x %02x %02x"
  954. "\n",
  955. i,
  956. buf[i], buf[i+1],
  957. buf[i+2], buf[i+3],
  958. buf[i+4], buf[i+5],
  959. buf[i+6], buf[i+7],
  960. buf[i+8], buf[i+9],
  961. buf[i+10], buf[i+11],
  962. buf[i+12], buf[i+13],
  963. buf[i+14], buf[i+15]);
  964. }
  965. }
  966. break;
  967. }
  968. return -ENOTSUPP;
  969. }
  970. int rndis_register(int (*rndis_control_ack)(struct eth_device *))
  971. {
  972. u8 i;
  973. for (i = 0; i < RNDIS_MAX_CONFIGS; i++) {
  974. if (!rndis_per_dev_params[i].used) {
  975. rndis_per_dev_params[i].used = 1;
  976. rndis_per_dev_params[i].ack = rndis_control_ack;
  977. debug("%s: configNr = %d\n", __func__, i);
  978. return i;
  979. }
  980. }
  981. debug("%s failed\n", __func__);
  982. return -1;
  983. }
  984. void rndis_deregister(int configNr)
  985. {
  986. debug("%s: configNr = %d\n", __func__, configNr);
  987. if (configNr >= RNDIS_MAX_CONFIGS)
  988. return;
  989. rndis_per_dev_params[configNr].used = 0;
  990. return;
  991. }
  992. int rndis_set_param_dev(u8 configNr, struct eth_device *dev, int mtu,
  993. struct net_device_stats *stats, u16 *cdc_filter)
  994. {
  995. debug("%s: configNr = %d\n", __func__, configNr);
  996. if (!dev || !stats)
  997. return -1;
  998. if (configNr >= RNDIS_MAX_CONFIGS)
  999. return -1;
  1000. rndis_per_dev_params[configNr].dev = dev;
  1001. rndis_per_dev_params[configNr].stats = stats;
  1002. rndis_per_dev_params[configNr].mtu = mtu;
  1003. rndis_per_dev_params[configNr].filter = cdc_filter;
  1004. return 0;
  1005. }
  1006. int rndis_set_param_vendor(u8 configNr, u32 vendorID, const char *vendorDescr)
  1007. {
  1008. debug("%s: configNr = %d\n", __func__, configNr);
  1009. if (!vendorDescr)
  1010. return -1;
  1011. if (configNr >= RNDIS_MAX_CONFIGS)
  1012. return -1;
  1013. rndis_per_dev_params[configNr].vendorID = vendorID;
  1014. rndis_per_dev_params[configNr].vendorDescr = vendorDescr;
  1015. return 0;
  1016. }
  1017. int rndis_set_param_medium(u8 configNr, u32 medium, u32 speed)
  1018. {
  1019. debug("%s: configNr = %d, %u %u\n", __func__, configNr, medium, speed);
  1020. if (configNr >= RNDIS_MAX_CONFIGS)
  1021. return -1;
  1022. rndis_per_dev_params[configNr].medium = medium;
  1023. rndis_per_dev_params[configNr].speed = speed;
  1024. return 0;
  1025. }
  1026. void rndis_add_hdr(void *buf, int length)
  1027. {
  1028. struct rndis_packet_msg_type *header;
  1029. header = buf;
  1030. memset(header, 0, sizeof *header);
  1031. header->MessageType = __constant_cpu_to_le32(REMOTE_NDIS_PACKET_MSG);
  1032. header->MessageLength = cpu_to_le32(length + sizeof *header);
  1033. header->DataOffset = __constant_cpu_to_le32(36);
  1034. header->DataLength = cpu_to_le32(length);
  1035. }
  1036. void rndis_free_response(int configNr, u8 *buf)
  1037. {
  1038. rndis_resp_t *r;
  1039. struct list_head *act, *tmp;
  1040. list_for_each_safe(act, tmp,
  1041. &(rndis_per_dev_params[configNr].resp_queue))
  1042. {
  1043. r = list_entry(act, rndis_resp_t, list);
  1044. if (r && r->buf == buf) {
  1045. list_del(&r->list);
  1046. free(r);
  1047. }
  1048. }
  1049. }
  1050. u8 *rndis_get_next_response(int configNr, u32 *length)
  1051. {
  1052. rndis_resp_t *r;
  1053. struct list_head *act, *tmp;
  1054. if (!length)
  1055. return NULL;
  1056. list_for_each_safe(act, tmp,
  1057. &(rndis_per_dev_params[configNr].resp_queue))
  1058. {
  1059. r = list_entry(act, rndis_resp_t, list);
  1060. if (!r->send) {
  1061. r->send = 1;
  1062. *length = r->length;
  1063. return r->buf;
  1064. }
  1065. }
  1066. return NULL;
  1067. }
  1068. static rndis_resp_t *rndis_add_response(int configNr, u32 length)
  1069. {
  1070. rndis_resp_t *r;
  1071. /* NOTE: this gets copied into ether.c USB_BUFSIZ bytes ... */
  1072. r = malloc(sizeof(rndis_resp_t) + length);
  1073. if (!r)
  1074. return NULL;
  1075. r->buf = (u8 *) (r + 1);
  1076. r->length = length;
  1077. r->send = 0;
  1078. list_add_tail(&r->list,
  1079. &(rndis_per_dev_params[configNr].resp_queue));
  1080. return r;
  1081. }
  1082. int rndis_rm_hdr(void *buf, int length)
  1083. {
  1084. /* tmp points to a struct rndis_packet_msg_type */
  1085. __le32 *tmp = buf;
  1086. int offs, len;
  1087. /* MessageType, MessageLength */
  1088. if (__constant_cpu_to_le32(REMOTE_NDIS_PACKET_MSG)
  1089. != get_unaligned(tmp++))
  1090. return -EINVAL;
  1091. tmp++;
  1092. /* DataOffset, DataLength */
  1093. offs = get_unaligned_le32(tmp++) + 8 /* offset of DataOffset */;
  1094. if (offs != sizeof(struct rndis_packet_msg_type))
  1095. debug("%s: unexpected DataOffset: %d\n", __func__, offs);
  1096. if (offs >= length)
  1097. return -EOVERFLOW;
  1098. len = get_unaligned_le32(tmp++);
  1099. if (len + sizeof(struct rndis_packet_msg_type) != length)
  1100. debug("%s: unexpected DataLength: %d, packet length=%d\n",
  1101. __func__, len, length);
  1102. memmove(buf, buf + offs, len);
  1103. return offs;
  1104. }
  1105. int rndis_init(void)
  1106. {
  1107. u8 i;
  1108. for (i = 0; i < RNDIS_MAX_CONFIGS; i++) {
  1109. rndis_per_dev_params[i].confignr = i;
  1110. rndis_per_dev_params[i].used = 0;
  1111. rndis_per_dev_params[i].state = RNDIS_UNINITIALIZED;
  1112. rndis_per_dev_params[i].media_state
  1113. = NDIS_MEDIA_STATE_DISCONNECTED;
  1114. INIT_LIST_HEAD(&(rndis_per_dev_params[i].resp_queue));
  1115. }
  1116. return 0;
  1117. }
  1118. void rndis_exit(void)
  1119. {
  1120. /* Nothing to do */
  1121. }