rndis.c 32 KB

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