Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  1. /**************************************************************************
  2. Etherboot - BOOTP/TFTP Bootstrap Program
  3. Prism2 NIC driver for Etherboot
  4. Written by Michael Brown of Fen Systems Ltd
  5. $Id$
  6. ***************************************************************************/
  7. /*
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2, or (at
  11. * your option) any later version.
  12. */
  13. #include <etherboot.h>
  14. #include <nic.h>
  15. #include <gpxe/pci.h>
  16. #include <gpxe/ethernet.h>
  17. /*
  18. * Hard-coded SSID
  19. * Leave blank in order to connect to any available SSID
  20. */
  21. static const char hardcoded_ssid[] = "";
  22. /*
  23. * Maximum number of info packets to wait for on a join attempt.
  24. * Some APs (including the Linksys WAP11) will send a "you are disconnected" packet
  25. * before sending the "you are connected" packet, if the card has previously been
  26. * attached to the AP.
  27. *
  28. * 2 is probably a sensible value, but YMMV.
  29. */
  30. #define MAX_JOIN_INFO_COUNT 2
  31. /*
  32. * Type of Prism2 interface to support
  33. * If not already defined, select PLX
  34. */
  35. #ifndef WLAN_HOSTIF
  36. #define WLAN_HOSTIF WLAN_PLX
  37. #endif
  38. /*
  39. * Include wlan_compat, p80211 and hfa384x header files from Linux Prism2 driver
  40. * We need to hack some defines in order to avoid compiling kernel-specific routines
  41. */
  42. #define __LINUX_WLAN__
  43. #undef __KERNEL__
  44. #define __I386__
  45. #include "wlan_compat.h"
  46. #include "p80211hdr.h"
  47. #include "hfa384x.h"
  48. #define BAP_TIMEOUT ( 5000 )
  49. /*
  50. * A few hacks to make the coding environment more Linux-like. This makes it somewhat
  51. * quicker to convert code from the Linux Prism2 driver.
  52. */
  53. #include <errno.h>
  54. #define __le16_to_cpu(x) (x)
  55. #define __le32_to_cpu(x) (x)
  56. #define __cpu_to_le16(x) (x)
  57. #define __cpu_to_le32(x) (x)
  58. #define hfa384x2host_16(n) (__le16_to_cpu((UINT16)(n)))
  59. #define hfa384x2host_32(n) (__le32_to_cpu((UINT32)(n)))
  60. #define host2hfa384x_16(n) (__cpu_to_le16((UINT16)(n)))
  61. #define host2hfa384x_32(n) (__cpu_to_le32((UINT32)(n)))
  62. /*
  63. * PLX9052 PCI register offsets
  64. * Taken from PLX9052 datasheet available from http://www.plxtech.com/download/9052/databook/9052db-20.pdf
  65. */
  66. #define PLX_LOCAL_CONFIG_REGISTER_BASE ( PCI_BASE_ADDRESS_1 )
  67. #define PLX_LOCAL_ADDRESS_SPACE_0_BASE ( PCI_BASE_ADDRESS_2 )
  68. #define PLX_LOCAL_ADDRESS_SPACE_1_BASE ( PCI_BASE_ADDRESS_3 )
  69. #define PLX_LOCAL_ADDRESS_SPACE_2_BASE ( PCI_BASE_ADDRESS_4 )
  70. #define PLX_LOCAL_ADDRESS_SPACE_3_BASE ( PCI_BASE_ADDRESS_5 )
  71. #define PRISM2_PLX_ATTR_MEM_BASE ( PLX_LOCAL_ADDRESS_SPACE_0_BASE )
  72. #define PRISM2_PLX_IO_BASE ( PLX_LOCAL_ADDRESS_SPACE_1_BASE )
  73. #define PRISM2_PCI_MEM_BASE ( PCI_BASE_ADDRESS_0 )
  74. /*
  75. * PCMCIA CIS types
  76. * Taken from cistpl.h in pcmcia-cs
  77. */
  78. #define CISTPL_VERS_1 ( 0x15 )
  79. #define CISTPL_END ( 0xff )
  80. #define CIS_STEP ( 2 )
  81. #define CISTPL_HEADER_LEN ( 2 * CIS_STEP )
  82. #define CISTPL_LEN_OFF ( 1 * CIS_STEP )
  83. #define CISTPL_VERS_1_STR_OFF ( 4 * CIS_STEP )
  84. /*
  85. * Prism2 constants
  86. * Taken from prism2sta.c in linux-wlan-ng
  87. */
  88. #define COR_OFFSET ( 0x3e0 ) /* COR attribute offset of Prism2 PC card */
  89. #define COR_VALUE ( 0x41 ) /* Enable PC card with irq in level trigger (but interrupts disabled) */
  90. /* NIC specific static variables */
  91. /* The hfa384x_t structure is used extensively in the Linux driver but is ifdef'd out in our include since __KERNEL__ is not defined.
  92. * This is a dummy version that contains only the fields we are interested in.
  93. */
  94. typedef struct hfa384x
  95. {
  96. UINT32 iobase;
  97. UINT32 membase;
  98. UINT16 lastcmd;
  99. UINT16 status; /* in host order */
  100. UINT16 resp0; /* in host order */
  101. UINT16 resp1; /* in host order */
  102. UINT16 resp2; /* in host order */
  103. UINT8 bssid[WLAN_BSSID_LEN];
  104. } hfa384x_t;
  105. /* The global instance of the hardware (i.e. where we store iobase and membase, in the absence of anywhere better to put them */
  106. static hfa384x_t hw_global = {
  107. 0, 0, 0, 0, 0, 0, 0, {0,0,0,0,0,0}
  108. };
  109. /*
  110. * 802.11 headers in addition to those in hfa384x_tx_frame_t (LLC and SNAP)
  111. * Taken from p80211conv.h
  112. */
  113. typedef struct wlan_llc
  114. {
  115. UINT8 dsap;
  116. UINT8 ssap;
  117. UINT8 ctl;
  118. } wlan_llc_t;
  119. static const wlan_llc_t wlan_llc_snap = { 0xaa, 0xaa, 0x03 }; /* LLC header indicating SNAP (?) */
  120. #define WLAN_IEEE_OUI_LEN 3
  121. typedef struct wlan_snap
  122. {
  123. UINT8 oui[WLAN_IEEE_OUI_LEN];
  124. UINT16 type;
  125. } wlan_snap_t;
  126. typedef struct wlan_80211hdr
  127. {
  128. wlan_llc_t llc;
  129. wlan_snap_t snap;
  130. } wlan_80211hdr_t;
  131. /*
  132. * Function prototypes
  133. */
  134. /*
  135. * Hardware-level hfa384x functions
  136. * These are based on the ones in hfa384x.h (which are ifdef'd out since __KERNEL__ is not defined).
  137. * Basically, these functions are the result of hand-evaluating all the ifdefs and defines in the hfa384x.h versions.
  138. */
  139. /* Retrieve the value of one of the MAC registers. */
  140. static inline UINT16 hfa384x_getreg( hfa384x_t *hw, UINT reg )
  141. {
  142. #if (WLAN_HOSTIF == WLAN_PLX)
  143. return inw ( hw->iobase + reg );
  144. #elif (WLAN_HOSTIF == WLAN_PCI)
  145. return readw ( hw->membase + reg );
  146. #endif
  147. }
  148. /* Set the value of one of the MAC registers. */
  149. static inline void hfa384x_setreg( hfa384x_t *hw, UINT16 val, UINT reg )
  150. {
  151. #if (WLAN_HOSTIF == WLAN_PLX)
  152. outw ( val, hw->iobase + reg );
  153. #elif (WLAN_HOSTIF == WLAN_PCI)
  154. writew ( val, hw->membase + reg );
  155. #endif
  156. return;
  157. }
  158. /*
  159. * Noswap versions
  160. * Etherboot is i386 only, so swap and noswap are the same...
  161. */
  162. static inline UINT16 hfa384x_getreg_noswap( hfa384x_t *hw, UINT reg )
  163. {
  164. return hfa384x_getreg ( hw, reg );
  165. }
  166. static inline void hfa384x_setreg_noswap( hfa384x_t *hw, UINT16 val, UINT reg )
  167. {
  168. hfa384x_setreg ( hw, val, reg );
  169. }
  170. /*
  171. * Low-level hfa384x functions
  172. * These are based on the ones in hfa384x.c, modified to work in the Etherboot environment.
  173. */
  174. /*
  175. * hfa384x_docmd_wait
  176. *
  177. * Waits for availability of the Command register, then
  178. * issues the given command. Then polls the Evstat register
  179. * waiting for command completion.
  180. * Arguments:
  181. * hw device structure
  182. * cmd Command in host order
  183. * parm0 Parameter0 in host order
  184. * parm1 Parameter1 in host order
  185. * parm2 Parameter2 in host order
  186. * Returns:
  187. * 0 success
  188. * >0 command indicated error, Status and Resp0-2 are
  189. * in hw structure.
  190. */
  191. static int hfa384x_docmd_wait( hfa384x_t *hw, UINT16 cmd, UINT16 parm0, UINT16 parm1, UINT16 parm2)
  192. {
  193. UINT16 reg = 0;
  194. UINT16 counter = 0;
  195. /* wait for the busy bit to clear */
  196. counter = 0;
  197. reg = hfa384x_getreg(hw, HFA384x_CMD);
  198. while ( HFA384x_CMD_ISBUSY(reg) && (counter < 10) ) {
  199. reg = hfa384x_getreg(hw, HFA384x_CMD);
  200. counter++;
  201. udelay(10);
  202. }
  203. if (HFA384x_CMD_ISBUSY(reg)) {
  204. printf("hfa384x_cmd timeout(1), reg=0x%0hx.\n", reg);
  205. return -ETIMEDOUT;
  206. }
  207. /* busy bit clear, write command */
  208. hfa384x_setreg(hw, parm0, HFA384x_PARAM0);
  209. hfa384x_setreg(hw, parm1, HFA384x_PARAM1);
  210. hfa384x_setreg(hw, parm2, HFA384x_PARAM2);
  211. hw->lastcmd = cmd;
  212. hfa384x_setreg(hw, cmd, HFA384x_CMD);
  213. /* Now wait for completion */
  214. counter = 0;
  215. reg = hfa384x_getreg(hw, HFA384x_EVSTAT);
  216. /* Initialization is the problem. It takes about
  217. 100ms. "normal" commands are typically is about
  218. 200-400 us (I've never seen less than 200). Longer
  219. is better so that we're not hammering the bus. */
  220. while ( !HFA384x_EVSTAT_ISCMD(reg) && (counter < 5000)) {
  221. reg = hfa384x_getreg(hw, HFA384x_EVSTAT);
  222. counter++;
  223. udelay(200);
  224. }
  225. if ( ! HFA384x_EVSTAT_ISCMD(reg) ) {
  226. printf("hfa384x_cmd timeout(2), reg=0x%0hx.\n", reg);
  227. return -ETIMEDOUT;
  228. }
  229. /* Read status and response */
  230. hw->status = hfa384x_getreg(hw, HFA384x_STATUS);
  231. hw->resp0 = hfa384x_getreg(hw, HFA384x_RESP0);
  232. hw->resp1 = hfa384x_getreg(hw, HFA384x_RESP1);
  233. hw->resp2 = hfa384x_getreg(hw, HFA384x_RESP2);
  234. hfa384x_setreg(hw, HFA384x_EVACK_CMD, HFA384x_EVACK);
  235. return HFA384x_STATUS_RESULT_GET(hw->status);
  236. }
  237. /*
  238. * Prepare BAP for access. Assigns FID and RID, sets offset register
  239. * and waits for BAP to become available.
  240. *
  241. * Arguments:
  242. * hw device structure
  243. * id FID or RID, destined for the select register (host order)
  244. * offset An _even_ offset into the buffer for the given FID/RID.
  245. * Returns:
  246. * 0 success
  247. */
  248. static int hfa384x_prepare_bap(hfa384x_t *hw, UINT16 id, UINT16 offset)
  249. {
  250. int result = 0;
  251. UINT16 reg;
  252. UINT16 i;
  253. /* Validate offset, buf, and len */
  254. if ( (offset > HFA384x_BAP_OFFSET_MAX) || (offset % 2) ) {
  255. result = -EINVAL;
  256. } else {
  257. /* Write fid/rid and offset */
  258. hfa384x_setreg(hw, id, HFA384x_SELECT0);
  259. udelay(10);
  260. hfa384x_setreg(hw, offset, HFA384x_OFFSET0);
  261. /* Wait for offset[busy] to clear (see BAP_TIMEOUT) */
  262. i = 0;
  263. do {
  264. reg = hfa384x_getreg(hw, HFA384x_OFFSET0);
  265. if ( i > 0 ) udelay(2);
  266. i++;
  267. } while ( i < BAP_TIMEOUT && HFA384x_OFFSET_ISBUSY(reg));
  268. if ( i >= BAP_TIMEOUT ) {
  269. /* failure */
  270. result = reg;
  271. } else if ( HFA384x_OFFSET_ISERR(reg) ){
  272. /* failure */
  273. result = reg;
  274. }
  275. }
  276. return result;
  277. }
  278. /*
  279. * Copy data from BAP to memory.
  280. *
  281. * Arguments:
  282. * hw device structure
  283. * id FID or RID, destined for the select register (host order)
  284. * offset An _even_ offset into the buffer for the given FID/RID.
  285. * buf ptr to array of bytes
  286. * len length of data to transfer in bytes
  287. * Returns:
  288. * 0 success
  289. */
  290. static int hfa384x_copy_from_bap(hfa384x_t *hw, UINT16 id, UINT16 offset,
  291. void *buf, UINT len)
  292. {
  293. int result = 0;
  294. UINT8 *d = (UINT8*)buf;
  295. UINT16 i;
  296. UINT16 reg = 0;
  297. /* Prepare BAP */
  298. result = hfa384x_prepare_bap ( hw, id, offset );
  299. if ( result == 0 ) {
  300. /* Read even(len) buf contents from data reg */
  301. for ( i = 0; i < (len & 0xfffe); i+=2 ) {
  302. *(UINT16*)(&(d[i])) = hfa384x_getreg_noswap(hw, HFA384x_DATA0);
  303. }
  304. /* If len odd, handle last byte */
  305. if ( len % 2 ){
  306. reg = hfa384x_getreg_noswap(hw, HFA384x_DATA0);
  307. d[len-1] = ((UINT8*)(&reg))[0];
  308. }
  309. }
  310. if (result) {
  311. printf ( "copy_from_bap(%#hx, %#hx, %d) failed, result=%#hx\n", id, offset, len, result);
  312. }
  313. return result;
  314. }
  315. /*
  316. * Copy data from memory to BAP.
  317. *
  318. * Arguments:
  319. * hw device structure
  320. * id FID or RID, destined for the select register (host order)
  321. * offset An _even_ offset into the buffer for the given FID/RID.
  322. * buf ptr to array of bytes
  323. * len length of data to transfer in bytes
  324. * Returns:
  325. * 0 success
  326. */
  327. static int hfa384x_copy_to_bap(hfa384x_t *hw, UINT16 id, UINT16 offset,
  328. void *buf, UINT len)
  329. {
  330. int result = 0;
  331. UINT8 *d = (UINT8*)buf;
  332. UINT16 i;
  333. UINT16 savereg;
  334. /* Prepare BAP */
  335. result = hfa384x_prepare_bap ( hw, id, offset );
  336. if ( result == 0 ) {
  337. /* Write even(len) buf contents to data reg */
  338. for ( i = 0; i < (len & 0xfffe); i+=2 ) {
  339. hfa384x_setreg_noswap(hw, *(UINT16*)(&(d[i])), HFA384x_DATA0);
  340. }
  341. /* If len odd, handle last byte */
  342. if ( len % 2 ){
  343. savereg = hfa384x_getreg_noswap(hw, HFA384x_DATA0);
  344. result = hfa384x_prepare_bap ( hw, id, offset + (len & 0xfffe) );
  345. if ( result == 0 ) {
  346. ((UINT8*)(&savereg))[0] = d[len-1];
  347. hfa384x_setreg_noswap(hw, savereg, HFA384x_DATA0);
  348. }
  349. }
  350. }
  351. if (result) {
  352. printf ( "copy_to_bap(%#hx, %#hx, %d) failed, result=%#hx\n", id, offset, len, result);
  353. }
  354. return result;
  355. }
  356. /*
  357. * Request a given record to be copied to/from the record buffer.
  358. *
  359. * Arguments:
  360. * hw device structure
  361. * write [0|1] copy the record buffer to the given
  362. * configuration record. (host order)
  363. * rid RID of the record to read/write. (host order)
  364. *
  365. * Returns:
  366. * 0 success
  367. */
  368. static inline int hfa384x_cmd_access(hfa384x_t *hw, UINT16 write, UINT16 rid)
  369. {
  370. return hfa384x_docmd_wait(hw, HFA384x_CMD_CMDCODE_SET(HFA384x_CMDCODE_ACCESS) | HFA384x_CMD_WRITE_SET(write), rid, 0, 0);
  371. }
  372. /*
  373. * Performs the sequence necessary to read a config/info item.
  374. *
  375. * Arguments:
  376. * hw device structure
  377. * rid config/info record id (host order)
  378. * buf host side record buffer. Upon return it will
  379. * contain the body portion of the record (minus the
  380. * RID and len).
  381. * len buffer length (in bytes, should match record length)
  382. *
  383. * Returns:
  384. * 0 success
  385. */
  386. static int hfa384x_drvr_getconfig(hfa384x_t *hw, UINT16 rid, void *buf, UINT16 len)
  387. {
  388. int result = 0;
  389. hfa384x_rec_t rec;
  390. /* Request read of RID */
  391. result = hfa384x_cmd_access( hw, 0, rid);
  392. if ( result ) {
  393. printf("Call to hfa384x_cmd_access failed\n");
  394. return -1;
  395. }
  396. /* Copy out record length */
  397. result = hfa384x_copy_from_bap( hw, rid, 0, &rec, sizeof(rec));
  398. if ( result ) {
  399. return -1;
  400. }
  401. /* Validate the record length */
  402. if ( ((hfa384x2host_16(rec.reclen)-1)*2) != len ) { /* note body len calculation in bytes */
  403. printf ( "RID len mismatch, rid=%#hx hlen=%d fwlen=%d\n", rid, len, (hfa384x2host_16(rec.reclen)-1)*2);
  404. return -1;
  405. }
  406. /* Copy out record data */
  407. result = hfa384x_copy_from_bap( hw, rid, sizeof(rec), buf, len);
  408. return result;
  409. }
  410. /*
  411. * Performs the sequence necessary to read a 16/32 bit config/info item
  412. * and convert it to host order.
  413. *
  414. * Arguments:
  415. * hw device structure
  416. * rid config/info record id (in host order)
  417. * val ptr to 16/32 bit buffer to receive value (in host order)
  418. *
  419. * Returns:
  420. * 0 success
  421. */
  422. #if 0 /* Not actually used anywhere */
  423. static int hfa384x_drvr_getconfig16(hfa384x_t *hw, UINT16 rid, void *val)
  424. {
  425. int result = 0;
  426. result = hfa384x_drvr_getconfig(hw, rid, val, sizeof(UINT16));
  427. if ( result == 0 ) {
  428. *((UINT16*)val) = hfa384x2host_16(*((UINT16*)val));
  429. }
  430. return result;
  431. }
  432. #endif
  433. #if 0 /* Not actually used anywhere */
  434. static int hfa384x_drvr_getconfig32(hfa384x_t *hw, UINT16 rid, void *val)
  435. {
  436. int result = 0;
  437. result = hfa384x_drvr_getconfig(hw, rid, val, sizeof(UINT32));
  438. if ( result == 0 ) {
  439. *((UINT32*)val) = hfa384x2host_32(*((UINT32*)val));
  440. }
  441. return result;
  442. }
  443. #endif
  444. /*
  445. * Performs the sequence necessary to write a config/info item.
  446. *
  447. * Arguments:
  448. * hw device structure
  449. * rid config/info record id (in host order)
  450. * buf host side record buffer
  451. * len buffer length (in bytes)
  452. *
  453. * Returns:
  454. * 0 success
  455. */
  456. static int hfa384x_drvr_setconfig(hfa384x_t *hw, UINT16 rid, void *buf, UINT16 len)
  457. {
  458. int result = 0;
  459. hfa384x_rec_t rec;
  460. rec.rid = host2hfa384x_16(rid);
  461. rec.reclen = host2hfa384x_16((len/2) + 1); /* note conversion to words, +1 for rid field */
  462. /* write the record header */
  463. result = hfa384x_copy_to_bap( hw, rid, 0, &rec, sizeof(rec));
  464. if ( result ) {
  465. printf("Failure writing record header\n");
  466. return -1;
  467. }
  468. /* write the record data (if there is any) */
  469. if ( len > 0 ) {
  470. result = hfa384x_copy_to_bap( hw, rid, sizeof(rec), buf, len);
  471. if ( result ) {
  472. printf("Failure writing record data\n");
  473. return -1;
  474. }
  475. }
  476. /* Trigger setting of record */
  477. result = hfa384x_cmd_access( hw, 1, rid);
  478. return result;
  479. }
  480. /*
  481. * Performs the sequence necessary to write a 16/32 bit config/info item.
  482. *
  483. * Arguments:
  484. * hw device structure
  485. * rid config/info record id (in host order)
  486. * val 16/32 bit value to store (in host order)
  487. *
  488. * Returns:
  489. * 0 success
  490. */
  491. static int hfa384x_drvr_setconfig16(hfa384x_t *hw, UINT16 rid, UINT16 *val)
  492. {
  493. UINT16 value;
  494. value = host2hfa384x_16(*val);
  495. return hfa384x_drvr_setconfig(hw, rid, &value, sizeof(UINT16));
  496. }
  497. #if 0 /* Not actually used anywhere */
  498. static int hfa384x_drvr_setconfig32(hfa384x_t *hw, UINT16 rid, UINT32 *val)
  499. {
  500. UINT32 value;
  501. value = host2hfa384x_32(*val);
  502. return hfa384x_drvr_setconfig(hw, rid, &value, sizeof(UINT32));
  503. }
  504. #endif
  505. /*
  506. * Wait for an event, with specified checking interval and timeout.
  507. * Automatically acknolwedges events.
  508. *
  509. * Arguments:
  510. * hw device structure
  511. * event_mask EVSTAT register mask of events to wait for
  512. * event_ack EVACK register set of events to be acknowledged if they happen (can be
  513. * used to acknowledge "ignorable" events in addition to the "main" event)
  514. * wait Time (in us) to wait between each poll of the register
  515. * timeout Maximum number of polls before timing out
  516. * descr Descriptive text string of what is being waited for
  517. * (will be printed out if a timeout happens)
  518. *
  519. * Returns:
  520. * value of EVSTAT register, or 0 on failure
  521. */
  522. static int hfa384x_wait_for_event(hfa384x_t *hw, UINT16 event_mask, UINT16 event_ack, int wait, int timeout, const char *descr)
  523. {
  524. UINT16 reg;
  525. int count = 0;
  526. do {
  527. reg = hfa384x_getreg(hw, HFA384x_EVSTAT);
  528. if ( count > 0 ) udelay(wait);
  529. count++;
  530. } while ( !(reg & event_mask) && count < timeout);
  531. if ( count >= timeout ) {
  532. printf("hfa384x: Timed out waiting for %s\n", descr);
  533. return 0; /* Return failure */
  534. }
  535. /* Acknowledge all events that we were waiting on */
  536. hfa384x_setreg(hw, reg & ( event_mask | event_ack ), HFA384x_EVACK);
  537. return reg;
  538. }
  539. /**************************************************************************
  540. POLL - Wait for a frame
  541. ***************************************************************************/
  542. static int prism2_poll(struct nic *nic, int retrieve)
  543. {
  544. UINT16 reg;
  545. UINT16 rxfid;
  546. UINT16 result;
  547. hfa384x_rx_frame_t rxdesc;
  548. hfa384x_t *hw = &hw_global;
  549. /* Check for received packet */
  550. reg = hfa384x_getreg(hw, HFA384x_EVSTAT);
  551. if ( ! HFA384x_EVSTAT_ISRX(reg) ) {
  552. /* No packet received - return 0 */
  553. return 0;
  554. }
  555. if ( ! retrieve ) return 1;
  556. /* Acknowledge RX event */
  557. hfa384x_setreg(hw, HFA384x_EVACK_RX_SET(1), HFA384x_EVACK);
  558. /* Get RX FID */
  559. rxfid = hfa384x_getreg(hw, HFA384x_RXFID);
  560. /* Get the descriptor (including headers) */
  561. result = hfa384x_copy_from_bap(hw, rxfid, 0, &rxdesc, sizeof(rxdesc));
  562. if ( result ) {
  563. return 0; /* fail */
  564. }
  565. /* Byte order convert once up front. */
  566. rxdesc.status = hfa384x2host_16(rxdesc.status);
  567. rxdesc.time = hfa384x2host_32(rxdesc.time);
  568. rxdesc.data_len = hfa384x2host_16(rxdesc.data_len);
  569. /* Fill in nic->packetlen */
  570. nic->packetlen = rxdesc.data_len;
  571. if ( nic->packetlen > 0 ) {
  572. /* Fill in nic->packet */
  573. /*
  574. * NOTE: Packets as received have an 8-byte header (LLC+SNAP(?)) terminating with the packet type.
  575. * Etherboot expects a 14-byte header terminating with the packet type (it ignores the rest of the
  576. * header), so we use a quick hack to achieve this.
  577. */
  578. result = hfa384x_copy_from_bap(hw, rxfid, HFA384x_RX_DATA_OFF,
  579. nic->packet + ETH_HLEN - sizeof(wlan_80211hdr_t), nic->packetlen);
  580. if ( result ) {
  581. return 0; /* fail */
  582. }
  583. }
  584. return 1; /* Packet successfully received */
  585. }
  586. /**************************************************************************
  587. TRANSMIT - Transmit a frame
  588. ***************************************************************************/
  589. static void prism2_transmit(
  590. struct nic *nic,
  591. const char *d, /* Destination */
  592. unsigned int t, /* Type */
  593. unsigned int s, /* size */
  594. const char *p) /* Packet */
  595. {
  596. hfa384x_t *hw = &hw_global;
  597. hfa384x_tx_frame_t txdesc;
  598. wlan_80211hdr_t p80211hdr = { wlan_llc_snap, {{0,0,0},0} };
  599. UINT16 fid;
  600. UINT16 status;
  601. int result;
  602. // Request FID allocation
  603. result = hfa384x_docmd_wait(hw, HFA384x_CMD_CMDCODE_SET(HFA384x_CMDCODE_ALLOC), HFA384x_DRVR_TXBUF_MAX, 0, 0);
  604. if (result != 0) {
  605. printf("hfa384x: Tx FID allocate command failed: Aborting transmit..\n");
  606. return;
  607. }
  608. if ( !hfa384x_wait_for_event(hw, HFA384x_EVSTAT_ALLOC, HFA384x_EVACK_INFO, 10, 50, "Tx FID to be allocated\n" ) ) return;
  609. fid = hfa384x_getreg(hw, HFA384x_ALLOCFID);
  610. /* Build Tx frame structure */
  611. memset(&txdesc, 0, sizeof(txdesc));
  612. txdesc.tx_control = host2hfa384x_16( HFA384x_TX_MACPORT_SET(0) | HFA384x_TX_STRUCTYPE_SET(1) |
  613. HFA384x_TX_TXEX_SET(1) | HFA384x_TX_TXOK_SET(1) );
  614. txdesc.frame_control = host2ieee16( WLAN_SET_FC_FTYPE(WLAN_FTYPE_DATA) |
  615. WLAN_SET_FC_FSTYPE(WLAN_FSTYPE_DATAONLY) |
  616. WLAN_SET_FC_TODS(1) );
  617. memcpy(txdesc.address1, hw->bssid, WLAN_ADDR_LEN);
  618. memcpy(txdesc.address2, nic->node_addr, WLAN_ADDR_LEN);
  619. memcpy(txdesc.address3, d, WLAN_ADDR_LEN);
  620. txdesc.data_len = host2hfa384x_16( sizeof(txdesc) + sizeof(p80211hdr) + s );
  621. /* Set up SNAP header */
  622. /* Let OUI default to RFC1042 (0x000000) */
  623. p80211hdr.snap.type = htons(t);
  624. /* Copy txdesc, p80211hdr and payload parts to FID */
  625. result = hfa384x_copy_to_bap(hw, fid, 0, &txdesc, sizeof(txdesc));
  626. if ( result ) return; /* fail */
  627. result = hfa384x_copy_to_bap( hw, fid, sizeof(txdesc), &p80211hdr, sizeof(p80211hdr) );
  628. if ( result ) return; /* fail */
  629. result = hfa384x_copy_to_bap( hw, fid, sizeof(txdesc) + sizeof(p80211hdr), (UINT8*)p, s );
  630. if ( result ) return; /* fail */
  631. /* Issue Tx command */
  632. result = hfa384x_docmd_wait(hw, HFA384x_CMD_CMDCODE_SET(HFA384x_CMDCODE_TX), fid, 0, 0);
  633. if ( result != 0 ) {
  634. printf("hfa384x: Transmit failed with result %#hx.\n", result);
  635. return;
  636. }
  637. /* Wait for transmit completion (or exception) */
  638. result = hfa384x_wait_for_event(hw, HFA384x_EVSTAT_TXEXC | HFA384x_EVSTAT_TX, HFA384x_EVACK_INFO,
  639. 200, 500, "Tx to complete\n" );
  640. if ( !result ) return; /* timeout failure */
  641. if ( HFA384x_EVSTAT_ISTXEXC(result) ) {
  642. fid = hfa384x_getreg(hw, HFA384x_TXCOMPLFID);
  643. printf ( "Tx exception occurred with fid %#hx\n", fid );
  644. result = hfa384x_copy_from_bap(hw, fid, 0, &status, sizeof(status));
  645. if ( result ) return; /* fail */
  646. printf("hfa384x: Tx error occurred (status %#hx):\n", status);
  647. if ( HFA384x_TXSTATUS_ISACKERR(status) ) { printf(" ...acknowledgement error\n"); }
  648. if ( HFA384x_TXSTATUS_ISFORMERR(status) ) { printf(" ...format error\n"); }
  649. if ( HFA384x_TXSTATUS_ISDISCON(status) ) { printf(" ...disconnected error\n"); }
  650. if ( HFA384x_TXSTATUS_ISAGEDERR(status) ) { printf(" ...AGED error\n"); }
  651. if ( HFA384x_TXSTATUS_ISRETRYERR(status) ) { printf(" ...retry error\n"); }
  652. return; /* fail */
  653. }
  654. }
  655. /**************************************************************************
  656. DISABLE - Turn off ethernet interface
  657. ***************************************************************************/
  658. static void prism2_disable ( struct nic *nic __unused ) {
  659. /* put the card in its initial state */
  660. }
  661. /**************************************************************************
  662. IRQ - Enable, Disable, or Force interrupts
  663. ***************************************************************************/
  664. static void prism2_irq(struct nic *nic __unused, irq_action_t action __unused)
  665. {
  666. switch ( action ) {
  667. case DISABLE :
  668. break;
  669. case ENABLE :
  670. break;
  671. case FORCE :
  672. break;
  673. }
  674. }
  675. /**************************************************************************
  676. Operations table
  677. ***************************************************************************/
  678. static struct nic_operations prism2_operations = {
  679. .connect = dummy_connect,
  680. .poll = prism2_poll,
  681. .transmit = prism2_transmit,
  682. .irq = prism2_irq,
  683. };
  684. /**************************************************************************
  685. PROBE - Look for an adapter, this routine's visible to the outside
  686. You should omit the last argument struct pci_device * for a non-PCI NIC
  687. ***************************************************************************/
  688. static int prism2_probe ( struct nic *nic, hfa384x_t *hw ) {
  689. int result;
  690. UINT16 tmp16 = 0;
  691. UINT16 infofid;
  692. hfa384x_InfFrame_t inf;
  693. char ssid[HFA384x_RID_CNFDESIREDSSID_LEN];
  694. int info_count = 0;
  695. nic->irqno = 0;
  696. /* Initialize card */
  697. result = hfa384x_docmd_wait(hw, HFA384x_CMDCODE_INIT, 0,0,0); /* Send initialize command */
  698. if ( result ) printf ( "Initialize command returned %#hx\n", result );
  699. hfa384x_setreg(hw, 0, HFA384x_INTEN); /* Disable interrupts */
  700. hfa384x_setreg(hw, 0xffff, HFA384x_EVACK); /* Acknowledge any spurious events */
  701. DBG ( "MAC address %s\n", eth_ntoa ( nic->node_addr ) );
  702. /* Retrieve MAC address (and fill out nic->node_addr) */
  703. hfa384x_drvr_getconfig ( hw, HFA384x_RID_CNFOWNMACADDR, nic->node_addr, HFA384x_RID_CNFOWNMACADDR_LEN );
  704. /* Prepare card for autojoin */
  705. /* This procedure is reverse-engineered from a register-level trace of the Linux driver's join process */
  706. tmp16 = WLAN_DATA_MAXLEN; /* Set maximum data length */
  707. result = hfa384x_drvr_setconfig16(hw, HFA384x_RID_CNFMAXDATALEN, &tmp16);
  708. if ( result ) printf ( "Set Max Data Length command returned %#hx\n", result );
  709. tmp16 = 0x000f; /* Set transmit rate(?) */
  710. result = hfa384x_drvr_setconfig16(hw, HFA384x_RID_TXRATECNTL, &tmp16);
  711. if ( result ) printf ( "Set Transmit Rate command returned %#hx\n", result );
  712. tmp16 = HFA384x_CNFAUTHENTICATION_OPENSYSTEM; /* Set authentication type to OpenSystem */
  713. result = hfa384x_drvr_setconfig16(hw, HFA384x_RID_CNFAUTHENTICATION, &tmp16);
  714. if ( result ) printf ( "Set Authentication Type command returned %#hx\n", result );
  715. /* Set SSID */
  716. memset(ssid, 0, HFA384x_RID_CNFDESIREDSSID_LEN);
  717. for ( tmp16=0; tmp16<sizeof(hardcoded_ssid); tmp16++ ) { ssid[2+tmp16] = hardcoded_ssid[tmp16]; }
  718. ssid[0] = sizeof(hardcoded_ssid) - 1; /* Ignore terminating zero */
  719. result = hfa384x_drvr_setconfig(hw, HFA384x_RID_CNFDESIREDSSID, ssid, HFA384x_RID_CNFDESIREDSSID_LEN); /* Set the SSID */
  720. if ( result ) printf ( "Set SSID command returned %#hx\n", result );
  721. tmp16 = 1; /* Set port type to ESS port */
  722. result = hfa384x_drvr_setconfig16(hw, HFA384x_RID_CNFPORTTYPE, &tmp16);
  723. if ( result ) printf ( "Set port type command returned %#hx\n", result );
  724. /* Enable card */
  725. result = hfa384x_docmd_wait(hw, HFA384x_CMD_CMDCODE_SET(HFA384x_CMDCODE_ENABLE) | HFA384x_CMD_MACPORT_SET(0), 0,0,0);
  726. if ( result ) printf ( "Enable command returned %#hx\n", result );
  727. do {
  728. /* Increment info_count, abort if too many attempts.
  729. * See comment next to definition of MAX_JOIN_INFO_COUNT for explanation.
  730. */
  731. info_count++;
  732. if ( info_count > MAX_JOIN_INFO_COUNT ) {
  733. printf ( "Too many failed attempts - aborting\n" );
  734. return 0;
  735. }
  736. /* Wait for info frame to indicate link status */
  737. if ( sizeof(hardcoded_ssid) == 1 ) {
  738. /* Empty SSID => join to any SSID */
  739. printf ( "Attempting to autojoin to any available access point (attempt %d)...", info_count );
  740. } else {
  741. printf ( "Attempting to autojoin to SSID %s (attempt %d)...", &ssid[2], info_count );
  742. }
  743. if ( !hfa384x_wait_for_event(hw, HFA384x_EVSTAT_INFO, 0, 1000, 2000, "Info event" ) ) return 0;
  744. printf("done\n");
  745. infofid = hfa384x_getreg(hw, HFA384x_INFOFID);
  746. /* Retrieve the length */
  747. result = hfa384x_copy_from_bap( hw, infofid, 0, &inf.framelen, sizeof(UINT16));
  748. if ( result ) return 0; /* fail */
  749. inf.framelen = hfa384x2host_16(inf.framelen);
  750. /* Retrieve the rest */
  751. result = hfa384x_copy_from_bap( hw, infofid, sizeof(UINT16),
  752. &(inf.infotype), inf.framelen * sizeof(UINT16));
  753. if ( result ) return 0; /* fail */
  754. if ( inf.infotype != HFA384x_IT_LINKSTATUS ) {
  755. /* Not a Link Status info frame: die */
  756. printf ( "Unexpected info frame type %#hx (not LinkStatus type)\n", inf.infotype );
  757. return 0;
  758. }
  759. inf.info.linkstatus.linkstatus = hfa384x2host_16(inf.info.linkstatus.linkstatus);
  760. if ( inf.info.linkstatus.linkstatus != HFA384x_LINK_CONNECTED ) {
  761. /* Link not connected - retry */
  762. printf ( "Link not connected (status %#hx)\n", inf.info.linkstatus.linkstatus );
  763. }
  764. } while ( inf.info.linkstatus.linkstatus != HFA384x_LINK_CONNECTED );
  765. /* Retrieve BSSID and print Connected message */
  766. result = hfa384x_drvr_getconfig(hw, HFA384x_RID_CURRENTBSSID, hw->bssid, WLAN_BSSID_LEN);
  767. DBG ( "Link connected (BSSID %s - ", eth_ntoa ( hw->bssid ) );
  768. DBG ( " MAC address %s)\n", eth_ntoa (nic->node_addr ) );
  769. /* point to NIC specific routines */
  770. nic->nic_op = &prism2_operations;
  771. return 1;
  772. }