rndis.c 33 KB

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