rndis.c 32 KB

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