hermes.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. /* hermes.c
  2. *
  3. * Driver core for the "Hermes" wireless MAC controller, as used in
  4. * the Lucent Orinoco and Cabletron RoamAbout cards. It should also
  5. * work on the hfa3841 and hfa3842 MAC controller chips used in the
  6. * Prism II chipsets.
  7. *
  8. * This is not a complete driver, just low-level access routines for
  9. * the MAC controller itself.
  10. *
  11. * Based on the prism2 driver from Absolute Value Systems' linux-wlan
  12. * project, the Linux wvlan_cs driver, Lucent's HCF-Light
  13. * (wvlan_hcf.c) library, and the NetBSD wireless driver (in no
  14. * particular order).
  15. *
  16. * Copyright (C) 2000, David Gibson, Linuxcare Australia.
  17. * (C) Copyright David Gibson, IBM Corp. 2001-2003.
  18. *
  19. * The contents of this file are subject to the Mozilla Public License
  20. * Version 1.1 (the "License"); you may not use this file except in
  21. * compliance with the License. You may obtain a copy of the License
  22. * at http://www.mozilla.org/MPL/
  23. *
  24. * Software distributed under the License is distributed on an "AS IS"
  25. * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
  26. * the License for the specific language governing rights and
  27. * limitations under the License.
  28. *
  29. * Alternatively, the contents of this file may be used under the
  30. * terms of the GNU General Public License version 2 (the "GPL"), in
  31. * which case the provisions of the GPL are applicable instead of the
  32. * above. If you wish to allow the use of your version of this file
  33. * only under the terms of the GPL and not to allow others to use your
  34. * version of this file under the MPL, indicate your decision by
  35. * deleting the provisions above and replace them with the notice and
  36. * other provisions required by the GPL. If you do not delete the
  37. * provisions above, a recipient may use your version of this file
  38. * under either the MPL or the GPL.
  39. */
  40. #include <linux/config.h>
  41. #include <linux/module.h>
  42. #include <linux/kernel.h>
  43. #include <linux/init.h>
  44. #include <linux/delay.h>
  45. #include "hermes.h"
  46. MODULE_DESCRIPTION("Low-level driver helper for Lucent Hermes chipset and Prism II HFA384x wireless MAC controller");
  47. MODULE_AUTHOR("Pavel Roskin <proski@gnu.org>"
  48. " & David Gibson <hermes@gibson.dropbear.id.au>");
  49. MODULE_LICENSE("Dual MPL/GPL");
  50. /* These are maximum timeouts. Most often, card wil react much faster */
  51. #define CMD_BUSY_TIMEOUT (100) /* In iterations of ~1us */
  52. #define CMD_INIT_TIMEOUT (50000) /* in iterations of ~10us */
  53. #define CMD_COMPL_TIMEOUT (20000) /* in iterations of ~10us */
  54. #define ALLOC_COMPL_TIMEOUT (1000) /* in iterations of ~10us */
  55. /*
  56. * Debugging helpers
  57. */
  58. #define DMSG(stuff...) do {printk(KERN_DEBUG "hermes @ %p: " , hw->iobase); \
  59. printk(stuff);} while (0)
  60. #undef HERMES_DEBUG
  61. #ifdef HERMES_DEBUG
  62. #include <stdarg.h>
  63. #define DEBUG(lvl, stuff...) if ( (lvl) <= HERMES_DEBUG) DMSG(stuff)
  64. #else /* ! HERMES_DEBUG */
  65. #define DEBUG(lvl, stuff...) do { } while (0)
  66. #endif /* ! HERMES_DEBUG */
  67. /*
  68. * Internal functions
  69. */
  70. /* Issue a command to the chip. Waiting for it to complete is the caller's
  71. problem.
  72. Returns -EBUSY if the command register is busy, 0 on success.
  73. Callable from any context.
  74. */
  75. static int hermes_issue_cmd(hermes_t *hw, u16 cmd, u16 param0)
  76. {
  77. int k = CMD_BUSY_TIMEOUT;
  78. u16 reg;
  79. /* First wait for the command register to unbusy */
  80. reg = hermes_read_regn(hw, CMD);
  81. while ( (reg & HERMES_CMD_BUSY) && k ) {
  82. k--;
  83. udelay(1);
  84. reg = hermes_read_regn(hw, CMD);
  85. }
  86. if (reg & HERMES_CMD_BUSY) {
  87. return -EBUSY;
  88. }
  89. hermes_write_regn(hw, PARAM2, 0);
  90. hermes_write_regn(hw, PARAM1, 0);
  91. hermes_write_regn(hw, PARAM0, param0);
  92. hermes_write_regn(hw, CMD, cmd);
  93. return 0;
  94. }
  95. /*
  96. * Function definitions
  97. */
  98. void hermes_struct_init(hermes_t *hw, void __iomem *address, int reg_spacing)
  99. {
  100. hw->iobase = address;
  101. hw->reg_spacing = reg_spacing;
  102. hw->inten = 0x0;
  103. #ifdef HERMES_DEBUG_BUFFER
  104. hw->dbufp = 0;
  105. memset(&hw->dbuf, 0xff, sizeof(hw->dbuf));
  106. memset(&hw->profile, 0, sizeof(hw->profile));
  107. #endif
  108. }
  109. int hermes_init(hermes_t *hw)
  110. {
  111. u16 status, reg;
  112. int err = 0;
  113. int k;
  114. /* We don't want to be interrupted while resetting the chipset */
  115. hw->inten = 0x0;
  116. hermes_write_regn(hw, INTEN, 0);
  117. hermes_write_regn(hw, EVACK, 0xffff);
  118. /* Normally it's a "can't happen" for the command register to
  119. be busy when we go to issue a command because we are
  120. serializing all commands. However we want to have some
  121. chance of resetting the card even if it gets into a stupid
  122. state, so we actually wait to see if the command register
  123. will unbusy itself here. */
  124. k = CMD_BUSY_TIMEOUT;
  125. reg = hermes_read_regn(hw, CMD);
  126. while (k && (reg & HERMES_CMD_BUSY)) {
  127. if (reg == 0xffff) /* Special case - the card has probably been removed,
  128. so don't wait for the timeout */
  129. return -ENODEV;
  130. k--;
  131. udelay(1);
  132. reg = hermes_read_regn(hw, CMD);
  133. }
  134. /* No need to explicitly handle the timeout - if we've timed
  135. out hermes_issue_cmd() will probably return -EBUSY below */
  136. /* According to the documentation, EVSTAT may contain
  137. obsolete event occurrence information. We have to acknowledge
  138. it by writing EVACK. */
  139. reg = hermes_read_regn(hw, EVSTAT);
  140. hermes_write_regn(hw, EVACK, reg);
  141. /* We don't use hermes_docmd_wait here, because the reset wipes
  142. the magic constant in SWSUPPORT0 away, and it gets confused */
  143. err = hermes_issue_cmd(hw, HERMES_CMD_INIT, 0);
  144. if (err)
  145. return err;
  146. reg = hermes_read_regn(hw, EVSTAT);
  147. k = CMD_INIT_TIMEOUT;
  148. while ( (! (reg & HERMES_EV_CMD)) && k) {
  149. k--;
  150. udelay(10);
  151. reg = hermes_read_regn(hw, EVSTAT);
  152. }
  153. hermes_write_regn(hw, SWSUPPORT0, HERMES_MAGIC);
  154. if (! hermes_present(hw)) {
  155. DEBUG(0, "hermes @ 0x%x: Card removed during reset.\n",
  156. hw->iobase);
  157. err = -ENODEV;
  158. goto out;
  159. }
  160. if (! (reg & HERMES_EV_CMD)) {
  161. printk(KERN_ERR "hermes @ %p: "
  162. "Timeout waiting for card to reset (reg=0x%04x)!\n",
  163. hw->iobase, reg);
  164. err = -ETIMEDOUT;
  165. goto out;
  166. }
  167. status = hermes_read_regn(hw, STATUS);
  168. hermes_write_regn(hw, EVACK, HERMES_EV_CMD);
  169. if (status & HERMES_STATUS_RESULT)
  170. err = -EIO;
  171. out:
  172. return err;
  173. }
  174. /* Issue a command to the chip, and (busy!) wait for it to
  175. * complete.
  176. *
  177. * Returns: < 0 on internal error, 0 on success, > 0 on error returned by the firmware
  178. *
  179. * Callable from any context, but locking is your problem. */
  180. int hermes_docmd_wait(hermes_t *hw, u16 cmd, u16 parm0,
  181. struct hermes_response *resp)
  182. {
  183. int err;
  184. int k;
  185. u16 reg;
  186. u16 status;
  187. err = hermes_issue_cmd(hw, cmd, parm0);
  188. if (err) {
  189. if (! hermes_present(hw)) {
  190. if (net_ratelimit())
  191. printk(KERN_WARNING "hermes @ %p: "
  192. "Card removed while issuing command "
  193. "0x%04x.\n", hw->iobase, cmd);
  194. err = -ENODEV;
  195. } else
  196. if (net_ratelimit())
  197. printk(KERN_ERR "hermes @ %p: "
  198. "Error %d issuing command 0x%04x.\n",
  199. hw->iobase, err, cmd);
  200. goto out;
  201. }
  202. reg = hermes_read_regn(hw, EVSTAT);
  203. k = CMD_COMPL_TIMEOUT;
  204. while ( (! (reg & HERMES_EV_CMD)) && k) {
  205. k--;
  206. udelay(10);
  207. reg = hermes_read_regn(hw, EVSTAT);
  208. }
  209. if (! hermes_present(hw)) {
  210. printk(KERN_WARNING "hermes @ %p: Card removed "
  211. "while waiting for command 0x%04x completion.\n",
  212. hw->iobase, cmd);
  213. err = -ENODEV;
  214. goto out;
  215. }
  216. if (! (reg & HERMES_EV_CMD)) {
  217. printk(KERN_ERR "hermes @ %p: Timeout waiting for "
  218. "command 0x%04x completion.\n", hw->iobase, cmd);
  219. err = -ETIMEDOUT;
  220. goto out;
  221. }
  222. status = hermes_read_regn(hw, STATUS);
  223. if (resp) {
  224. resp->status = status;
  225. resp->resp0 = hermes_read_regn(hw, RESP0);
  226. resp->resp1 = hermes_read_regn(hw, RESP1);
  227. resp->resp2 = hermes_read_regn(hw, RESP2);
  228. }
  229. hermes_write_regn(hw, EVACK, HERMES_EV_CMD);
  230. if (status & HERMES_STATUS_RESULT)
  231. err = -EIO;
  232. out:
  233. return err;
  234. }
  235. int hermes_allocate(hermes_t *hw, u16 size, u16 *fid)
  236. {
  237. int err = 0;
  238. int k;
  239. u16 reg;
  240. if ( (size < HERMES_ALLOC_LEN_MIN) || (size > HERMES_ALLOC_LEN_MAX) )
  241. return -EINVAL;
  242. err = hermes_docmd_wait(hw, HERMES_CMD_ALLOC, size, NULL);
  243. if (err) {
  244. return err;
  245. }
  246. reg = hermes_read_regn(hw, EVSTAT);
  247. k = ALLOC_COMPL_TIMEOUT;
  248. while ( (! (reg & HERMES_EV_ALLOC)) && k) {
  249. k--;
  250. udelay(10);
  251. reg = hermes_read_regn(hw, EVSTAT);
  252. }
  253. if (! hermes_present(hw)) {
  254. printk(KERN_WARNING "hermes @ %p: "
  255. "Card removed waiting for frame allocation.\n",
  256. hw->iobase);
  257. return -ENODEV;
  258. }
  259. if (! (reg & HERMES_EV_ALLOC)) {
  260. printk(KERN_ERR "hermes @ %p: "
  261. "Timeout waiting for frame allocation\n",
  262. hw->iobase);
  263. return -ETIMEDOUT;
  264. }
  265. *fid = hermes_read_regn(hw, ALLOCFID);
  266. hermes_write_regn(hw, EVACK, HERMES_EV_ALLOC);
  267. return 0;
  268. }
  269. /* Set up a BAP to read a particular chunk of data from card's internal buffer.
  270. *
  271. * Returns: < 0 on internal failure (errno), 0 on success, >0 on error
  272. * from firmware
  273. *
  274. * Callable from any context */
  275. static int hermes_bap_seek(hermes_t *hw, int bap, u16 id, u16 offset)
  276. {
  277. int sreg = bap ? HERMES_SELECT1 : HERMES_SELECT0;
  278. int oreg = bap ? HERMES_OFFSET1 : HERMES_OFFSET0;
  279. int k;
  280. u16 reg;
  281. /* Paranoia.. */
  282. if ( (offset > HERMES_BAP_OFFSET_MAX) || (offset % 2) )
  283. return -EINVAL;
  284. k = HERMES_BAP_BUSY_TIMEOUT;
  285. reg = hermes_read_reg(hw, oreg);
  286. while ((reg & HERMES_OFFSET_BUSY) && k) {
  287. k--;
  288. udelay(1);
  289. reg = hermes_read_reg(hw, oreg);
  290. }
  291. #ifdef HERMES_DEBUG_BUFFER
  292. hw->profile[HERMES_BAP_BUSY_TIMEOUT - k]++;
  293. if (k < HERMES_BAP_BUSY_TIMEOUT) {
  294. struct hermes_debug_entry *e =
  295. &hw->dbuf[(hw->dbufp++) % HERMES_DEBUG_BUFSIZE];
  296. e->bap = bap;
  297. e->id = id;
  298. e->offset = offset;
  299. e->cycles = HERMES_BAP_BUSY_TIMEOUT - k;
  300. }
  301. #endif
  302. if (reg & HERMES_OFFSET_BUSY)
  303. return -ETIMEDOUT;
  304. /* Now we actually set up the transfer */
  305. hermes_write_reg(hw, sreg, id);
  306. hermes_write_reg(hw, oreg, offset);
  307. /* Wait for the BAP to be ready */
  308. k = HERMES_BAP_BUSY_TIMEOUT;
  309. reg = hermes_read_reg(hw, oreg);
  310. while ( (reg & (HERMES_OFFSET_BUSY | HERMES_OFFSET_ERR)) && k) {
  311. k--;
  312. udelay(1);
  313. reg = hermes_read_reg(hw, oreg);
  314. }
  315. if (reg != offset) {
  316. printk(KERN_ERR "hermes @ %p: BAP%d offset %s: "
  317. "reg=0x%x id=0x%x offset=0x%x\n", hw->iobase, bap,
  318. (reg & HERMES_OFFSET_BUSY) ? "timeout" : "error",
  319. reg, id, offset);
  320. if (reg & HERMES_OFFSET_BUSY) {
  321. return -ETIMEDOUT;
  322. }
  323. return -EIO; /* error or wrong offset */
  324. }
  325. return 0;
  326. }
  327. /* Read a block of data from the chip's buffer, via the
  328. * BAP. Synchronization/serialization is the caller's problem. len
  329. * must be even.
  330. *
  331. * Returns: < 0 on internal failure (errno), 0 on success, > 0 on error from firmware
  332. */
  333. int hermes_bap_pread(hermes_t *hw, int bap, void *buf, int len,
  334. u16 id, u16 offset)
  335. {
  336. int dreg = bap ? HERMES_DATA1 : HERMES_DATA0;
  337. int err = 0;
  338. if ( (len < 0) || (len % 2) )
  339. return -EINVAL;
  340. err = hermes_bap_seek(hw, bap, id, offset);
  341. if (err)
  342. goto out;
  343. /* Actually do the transfer */
  344. hermes_read_words(hw, dreg, buf, len/2);
  345. out:
  346. return err;
  347. }
  348. /* Write a block of data to the chip's buffer, via the
  349. * BAP. Synchronization/serialization is the caller's problem. len
  350. * must be even.
  351. *
  352. * Returns: < 0 on internal failure (errno), 0 on success, > 0 on error from firmware
  353. */
  354. int hermes_bap_pwrite(hermes_t *hw, int bap, const void *buf, int len,
  355. u16 id, u16 offset)
  356. {
  357. int dreg = bap ? HERMES_DATA1 : HERMES_DATA0;
  358. int err = 0;
  359. if ( (len < 0) || (len % 2) )
  360. return -EINVAL;
  361. err = hermes_bap_seek(hw, bap, id, offset);
  362. if (err)
  363. goto out;
  364. /* Actually do the transfer */
  365. hermes_write_words(hw, dreg, buf, len/2);
  366. out:
  367. return err;
  368. }
  369. /* Write a block of data to the chip's buffer with padding if
  370. * neccessary, via the BAP. Synchronization/serialization is the
  371. * caller's problem. len must be even.
  372. *
  373. * Returns: < 0 on internal failure (errno), 0 on success, > 0 on error from firmware
  374. */
  375. int hermes_bap_pwrite_pad(hermes_t *hw, int bap, const void *buf, unsigned data_len, int len,
  376. u16 id, u16 offset)
  377. {
  378. int dreg = bap ? HERMES_DATA1 : HERMES_DATA0;
  379. int err = 0;
  380. if (len < 0 || len % 2 || data_len > len)
  381. return -EINVAL;
  382. err = hermes_bap_seek(hw, bap, id, offset);
  383. if (err)
  384. goto out;
  385. /* Transfer all the complete words of data */
  386. hermes_write_words(hw, dreg, buf, data_len/2);
  387. /* If there is an odd byte left over pad and transfer it */
  388. if (data_len & 1) {
  389. u8 end[2];
  390. end[1] = 0;
  391. end[0] = ((unsigned char *)buf)[data_len - 1];
  392. hermes_write_words(hw, dreg, end, 1);
  393. data_len ++;
  394. }
  395. /* Now send zeros for the padding */
  396. if (data_len < len)
  397. hermes_clear_words(hw, dreg, (len - data_len) / 2);
  398. /* Complete */
  399. out:
  400. return err;
  401. }
  402. /* Read a Length-Type-Value record from the card.
  403. *
  404. * If length is NULL, we ignore the length read from the card, and
  405. * read the entire buffer regardless. This is useful because some of
  406. * the configuration records appear to have incorrect lengths in
  407. * practice.
  408. *
  409. * Callable from user or bh context. */
  410. int hermes_read_ltv(hermes_t *hw, int bap, u16 rid, unsigned bufsize,
  411. u16 *length, void *buf)
  412. {
  413. int err = 0;
  414. int dreg = bap ? HERMES_DATA1 : HERMES_DATA0;
  415. u16 rlength, rtype;
  416. unsigned nwords;
  417. if ( (bufsize < 0) || (bufsize % 2) )
  418. return -EINVAL;
  419. err = hermes_docmd_wait(hw, HERMES_CMD_ACCESS, rid, NULL);
  420. if (err)
  421. return err;
  422. err = hermes_bap_seek(hw, bap, rid, 0);
  423. if (err)
  424. return err;
  425. rlength = hermes_read_reg(hw, dreg);
  426. if (! rlength)
  427. return -ENODATA;
  428. rtype = hermes_read_reg(hw, dreg);
  429. if (length)
  430. *length = rlength;
  431. if (rtype != rid)
  432. printk(KERN_WARNING "hermes @ %p: %s(): "
  433. "rid (0x%04x) does not match type (0x%04x)\n",
  434. hw->iobase, __FUNCTION__, rid, rtype);
  435. if (HERMES_RECLEN_TO_BYTES(rlength) > bufsize)
  436. printk(KERN_WARNING "hermes @ %p: "
  437. "Truncating LTV record from %d to %d bytes. "
  438. "(rid=0x%04x, len=0x%04x)\n", hw->iobase,
  439. HERMES_RECLEN_TO_BYTES(rlength), bufsize, rid, rlength);
  440. nwords = min((unsigned)rlength - 1, bufsize / 2);
  441. hermes_read_words(hw, dreg, buf, nwords);
  442. return 0;
  443. }
  444. int hermes_write_ltv(hermes_t *hw, int bap, u16 rid,
  445. u16 length, const void *value)
  446. {
  447. int dreg = bap ? HERMES_DATA1 : HERMES_DATA0;
  448. int err = 0;
  449. unsigned count;
  450. if (length == 0)
  451. return -EINVAL;
  452. err = hermes_bap_seek(hw, bap, rid, 0);
  453. if (err)
  454. return err;
  455. hermes_write_reg(hw, dreg, length);
  456. hermes_write_reg(hw, dreg, rid);
  457. count = length - 1;
  458. hermes_write_words(hw, dreg, value, count);
  459. err = hermes_docmd_wait(hw, HERMES_CMD_ACCESS | HERMES_CMD_WRITE,
  460. rid, NULL);
  461. return err;
  462. }
  463. EXPORT_SYMBOL(hermes_struct_init);
  464. EXPORT_SYMBOL(hermes_init);
  465. EXPORT_SYMBOL(hermes_docmd_wait);
  466. EXPORT_SYMBOL(hermes_allocate);
  467. EXPORT_SYMBOL(hermes_bap_pread);
  468. EXPORT_SYMBOL(hermes_bap_pwrite);
  469. EXPORT_SYMBOL(hermes_bap_pwrite_pad);
  470. EXPORT_SYMBOL(hermes_read_ltv);
  471. EXPORT_SYMBOL(hermes_write_ltv);
  472. static int __init init_hermes(void)
  473. {
  474. return 0;
  475. }
  476. static void __exit exit_hermes(void)
  477. {
  478. }
  479. module_init(init_hermes);
  480. module_exit(exit_hermes);