You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

prism2.c 31KB

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