rndis.c 33 KB

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