hermes.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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/module.h>
  41. #include <linux/kernel.h>
  42. #include <linux/init.h>
  43. #include <linux/delay.h>
  44. #include "hermes.h"
  45. MODULE_DESCRIPTION("Low-level driver helper for Lucent Hermes chipset"
  46. " 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. u16 param1, u16 param2)
  77. {
  78. int k = CMD_BUSY_TIMEOUT;
  79. u16 reg;
  80. /* First wait for the command register to unbusy */
  81. reg = hermes_read_regn(hw, CMD);
  82. while ((reg & HERMES_CMD_BUSY) && k) {
  83. k--;
  84. udelay(1);
  85. reg = hermes_read_regn(hw, CMD);
  86. }
  87. if (reg & HERMES_CMD_BUSY)
  88. return -EBUSY;
  89. hermes_write_regn(hw, PARAM2, param2);
  90. hermes_write_regn(hw, PARAM1, param1);
  91. hermes_write_regn(hw, PARAM0, param0);
  92. hermes_write_regn(hw, CMD, cmd);
  93. return 0;
  94. }
  95. /*
  96. * Function definitions
  97. */
  98. /* For doing cmds that wipe the magic constant in SWSUPPORT0 */
  99. int hermes_doicmd_wait(hermes_t *hw, u16 cmd,
  100. u16 parm0, u16 parm1, u16 parm2,
  101. struct hermes_response *resp)
  102. {
  103. int err = 0;
  104. int k;
  105. u16 status, reg;
  106. err = hermes_issue_cmd(hw, cmd, parm0, parm1, parm2);
  107. if (err)
  108. return err;
  109. reg = hermes_read_regn(hw, EVSTAT);
  110. k = CMD_INIT_TIMEOUT;
  111. while ((!(reg & HERMES_EV_CMD)) && k) {
  112. k--;
  113. udelay(10);
  114. reg = hermes_read_regn(hw, EVSTAT);
  115. }
  116. hermes_write_regn(hw, SWSUPPORT0, HERMES_MAGIC);
  117. if (!hermes_present(hw)) {
  118. DEBUG(0, "hermes @ 0x%x: Card removed during reset.\n",
  119. hw->iobase);
  120. err = -ENODEV;
  121. goto out;
  122. }
  123. if (!(reg & HERMES_EV_CMD)) {
  124. printk(KERN_ERR "hermes @ %p: "
  125. "Timeout waiting for card to reset (reg=0x%04x)!\n",
  126. hw->iobase, reg);
  127. err = -ETIMEDOUT;
  128. goto out;
  129. }
  130. status = hermes_read_regn(hw, STATUS);
  131. if (resp) {
  132. resp->status = status;
  133. resp->resp0 = hermes_read_regn(hw, RESP0);
  134. resp->resp1 = hermes_read_regn(hw, RESP1);
  135. resp->resp2 = hermes_read_regn(hw, RESP2);
  136. }
  137. hermes_write_regn(hw, EVACK, HERMES_EV_CMD);
  138. if (status & HERMES_STATUS_RESULT)
  139. err = -EIO;
  140. out:
  141. return err;
  142. }
  143. EXPORT_SYMBOL(hermes_doicmd_wait);
  144. void hermes_struct_init(hermes_t *hw, void __iomem *address, int reg_spacing)
  145. {
  146. hw->iobase = address;
  147. hw->reg_spacing = reg_spacing;
  148. hw->inten = 0x0;
  149. }
  150. EXPORT_SYMBOL(hermes_struct_init);
  151. int hermes_init(hermes_t *hw)
  152. {
  153. u16 reg;
  154. int err = 0;
  155. int k;
  156. /* We don't want to be interrupted while resetting the chipset */
  157. hw->inten = 0x0;
  158. hermes_write_regn(hw, INTEN, 0);
  159. hermes_write_regn(hw, EVACK, 0xffff);
  160. /* Normally it's a "can't happen" for the command register to
  161. be busy when we go to issue a command because we are
  162. serializing all commands. However we want to have some
  163. chance of resetting the card even if it gets into a stupid
  164. state, so we actually wait to see if the command register
  165. will unbusy itself here. */
  166. k = CMD_BUSY_TIMEOUT;
  167. reg = hermes_read_regn(hw, CMD);
  168. while (k && (reg & HERMES_CMD_BUSY)) {
  169. if (reg == 0xffff) /* Special case - the card has probably been
  170. removed, so don't wait for the timeout */
  171. return -ENODEV;
  172. k--;
  173. udelay(1);
  174. reg = hermes_read_regn(hw, CMD);
  175. }
  176. /* No need to explicitly handle the timeout - if we've timed
  177. out hermes_issue_cmd() will probably return -EBUSY below */
  178. /* According to the documentation, EVSTAT may contain
  179. obsolete event occurrence information. We have to acknowledge
  180. it by writing EVACK. */
  181. reg = hermes_read_regn(hw, EVSTAT);
  182. hermes_write_regn(hw, EVACK, reg);
  183. /* We don't use hermes_docmd_wait here, because the reset wipes
  184. the magic constant in SWSUPPORT0 away, and it gets confused */
  185. err = hermes_doicmd_wait(hw, HERMES_CMD_INIT, 0, 0, 0, NULL);
  186. return err;
  187. }
  188. EXPORT_SYMBOL(hermes_init);
  189. /* Issue a command to the chip, and (busy!) wait for it to
  190. * complete.
  191. *
  192. * Returns:
  193. * < 0 on internal error
  194. * 0 on success
  195. * > 0 on error returned by the firmware
  196. *
  197. * Callable from any context, but locking is your problem. */
  198. int hermes_docmd_wait(hermes_t *hw, u16 cmd, u16 parm0,
  199. struct hermes_response *resp)
  200. {
  201. int err;
  202. int k;
  203. u16 reg;
  204. u16 status;
  205. err = hermes_issue_cmd(hw, cmd, parm0, 0, 0);
  206. if (err) {
  207. if (!hermes_present(hw)) {
  208. if (net_ratelimit())
  209. printk(KERN_WARNING "hermes @ %p: "
  210. "Card removed while issuing command "
  211. "0x%04x.\n", hw->iobase, cmd);
  212. err = -ENODEV;
  213. } else
  214. if (net_ratelimit())
  215. printk(KERN_ERR "hermes @ %p: "
  216. "Error %d issuing command 0x%04x.\n",
  217. hw->iobase, err, cmd);
  218. goto out;
  219. }
  220. reg = hermes_read_regn(hw, EVSTAT);
  221. k = CMD_COMPL_TIMEOUT;
  222. while ((!(reg & HERMES_EV_CMD)) && k) {
  223. k--;
  224. udelay(10);
  225. reg = hermes_read_regn(hw, EVSTAT);
  226. }
  227. if (!hermes_present(hw)) {
  228. printk(KERN_WARNING "hermes @ %p: Card removed "
  229. "while waiting for command 0x%04x completion.\n",
  230. hw->iobase, cmd);
  231. err = -ENODEV;
  232. goto out;
  233. }
  234. if (!(reg & HERMES_EV_CMD)) {
  235. printk(KERN_ERR "hermes @ %p: Timeout waiting for "
  236. "command 0x%04x completion.\n", hw->iobase, cmd);
  237. err = -ETIMEDOUT;
  238. goto out;
  239. }
  240. status = hermes_read_regn(hw, STATUS);
  241. if (resp) {
  242. resp->status = status;
  243. resp->resp0 = hermes_read_regn(hw, RESP0);
  244. resp->resp1 = hermes_read_regn(hw, RESP1);
  245. resp->resp2 = hermes_read_regn(hw, RESP2);
  246. }
  247. hermes_write_regn(hw, EVACK, HERMES_EV_CMD);
  248. if (status & HERMES_STATUS_RESULT)
  249. err = -EIO;
  250. out:
  251. return err;
  252. }
  253. EXPORT_SYMBOL(hermes_docmd_wait);
  254. int hermes_allocate(hermes_t *hw, u16 size, u16 *fid)
  255. {
  256. int err = 0;
  257. int k;
  258. u16 reg;
  259. if ((size < HERMES_ALLOC_LEN_MIN) || (size > HERMES_ALLOC_LEN_MAX))
  260. return -EINVAL;
  261. err = hermes_docmd_wait(hw, HERMES_CMD_ALLOC, size, NULL);
  262. if (err)
  263. return err;
  264. reg = hermes_read_regn(hw, EVSTAT);
  265. k = ALLOC_COMPL_TIMEOUT;
  266. while ((!(reg & HERMES_EV_ALLOC)) && k) {
  267. k--;
  268. udelay(10);
  269. reg = hermes_read_regn(hw, EVSTAT);
  270. }
  271. if (!hermes_present(hw)) {
  272. printk(KERN_WARNING "hermes @ %p: "
  273. "Card removed waiting for frame allocation.\n",
  274. hw->iobase);
  275. return -ENODEV;
  276. }
  277. if (!(reg & HERMES_EV_ALLOC)) {
  278. printk(KERN_ERR "hermes @ %p: "
  279. "Timeout waiting for frame allocation\n",
  280. hw->iobase);
  281. return -ETIMEDOUT;
  282. }
  283. *fid = hermes_read_regn(hw, ALLOCFID);
  284. hermes_write_regn(hw, EVACK, HERMES_EV_ALLOC);
  285. return 0;
  286. }
  287. EXPORT_SYMBOL(hermes_allocate);
  288. /* Set up a BAP to read a particular chunk of data from card's internal buffer.
  289. *
  290. * Returns:
  291. * < 0 on internal failure (errno)
  292. * 0 on success
  293. * > 0 on error
  294. * from firmware
  295. *
  296. * Callable from any context */
  297. static int hermes_bap_seek(hermes_t *hw, int bap, u16 id, u16 offset)
  298. {
  299. int sreg = bap ? HERMES_SELECT1 : HERMES_SELECT0;
  300. int oreg = bap ? HERMES_OFFSET1 : HERMES_OFFSET0;
  301. int k;
  302. u16 reg;
  303. /* Paranoia.. */
  304. if ((offset > HERMES_BAP_OFFSET_MAX) || (offset % 2))
  305. return -EINVAL;
  306. k = HERMES_BAP_BUSY_TIMEOUT;
  307. reg = hermes_read_reg(hw, oreg);
  308. while ((reg & HERMES_OFFSET_BUSY) && k) {
  309. k--;
  310. udelay(1);
  311. reg = hermes_read_reg(hw, oreg);
  312. }
  313. if (reg & HERMES_OFFSET_BUSY)
  314. return -ETIMEDOUT;
  315. /* Now we actually set up the transfer */
  316. hermes_write_reg(hw, sreg, id);
  317. hermes_write_reg(hw, oreg, offset);
  318. /* Wait for the BAP to be ready */
  319. k = HERMES_BAP_BUSY_TIMEOUT;
  320. reg = hermes_read_reg(hw, oreg);
  321. while ((reg & (HERMES_OFFSET_BUSY | HERMES_OFFSET_ERR)) && k) {
  322. k--;
  323. udelay(1);
  324. reg = hermes_read_reg(hw, oreg);
  325. }
  326. if (reg != offset) {
  327. printk(KERN_ERR "hermes @ %p: BAP%d offset %s: "
  328. "reg=0x%x id=0x%x offset=0x%x\n", hw->iobase, bap,
  329. (reg & HERMES_OFFSET_BUSY) ? "timeout" : "error",
  330. reg, id, offset);
  331. if (reg & HERMES_OFFSET_BUSY)
  332. return -ETIMEDOUT;
  333. return -EIO; /* error or wrong offset */
  334. }
  335. return 0;
  336. }
  337. /* Read a block of data from the chip's buffer, via the
  338. * BAP. Synchronization/serialization is the caller's problem. len
  339. * must be even.
  340. *
  341. * Returns:
  342. * < 0 on internal failure (errno)
  343. * 0 on success
  344. * > 0 on error from firmware
  345. */
  346. int hermes_bap_pread(hermes_t *hw, int bap, void *buf, int len,
  347. u16 id, u16 offset)
  348. {
  349. int dreg = bap ? HERMES_DATA1 : HERMES_DATA0;
  350. int err = 0;
  351. if ((len < 0) || (len % 2))
  352. return -EINVAL;
  353. err = hermes_bap_seek(hw, bap, id, offset);
  354. if (err)
  355. goto out;
  356. /* Actually do the transfer */
  357. hermes_read_words(hw, dreg, buf, len/2);
  358. out:
  359. return err;
  360. }
  361. EXPORT_SYMBOL(hermes_bap_pread);
  362. /* Write a block of data to the chip's buffer, via the
  363. * BAP. Synchronization/serialization is the caller's problem.
  364. *
  365. * Returns:
  366. * < 0 on internal failure (errno)
  367. * 0 on success
  368. * > 0 on error from firmware
  369. */
  370. int hermes_bap_pwrite(hermes_t *hw, int bap, const void *buf, int len,
  371. u16 id, u16 offset)
  372. {
  373. int dreg = bap ? HERMES_DATA1 : HERMES_DATA0;
  374. int err = 0;
  375. if (len < 0)
  376. return -EINVAL;
  377. err = hermes_bap_seek(hw, bap, id, offset);
  378. if (err)
  379. goto out;
  380. /* Actually do the transfer */
  381. hermes_write_bytes(hw, dreg, buf, len);
  382. out:
  383. return err;
  384. }
  385. EXPORT_SYMBOL(hermes_bap_pwrite);
  386. /* Read a Length-Type-Value record from the card.
  387. *
  388. * If length is NULL, we ignore the length read from the card, and
  389. * read the entire buffer regardless. This is useful because some of
  390. * the configuration records appear to have incorrect lengths in
  391. * practice.
  392. *
  393. * Callable from user or bh context. */
  394. int hermes_read_ltv(hermes_t *hw, int bap, u16 rid, unsigned bufsize,
  395. u16 *length, void *buf)
  396. {
  397. int err = 0;
  398. int dreg = bap ? HERMES_DATA1 : HERMES_DATA0;
  399. u16 rlength, rtype;
  400. unsigned nwords;
  401. if ((bufsize < 0) || (bufsize % 2))
  402. return -EINVAL;
  403. err = hermes_docmd_wait(hw, HERMES_CMD_ACCESS, rid, NULL);
  404. if (err)
  405. return err;
  406. err = hermes_bap_seek(hw, bap, rid, 0);
  407. if (err)
  408. return err;
  409. rlength = hermes_read_reg(hw, dreg);
  410. if (!rlength)
  411. return -ENODATA;
  412. rtype = hermes_read_reg(hw, dreg);
  413. if (length)
  414. *length = rlength;
  415. if (rtype != rid)
  416. printk(KERN_WARNING "hermes @ %p: %s(): "
  417. "rid (0x%04x) does not match type (0x%04x)\n",
  418. hw->iobase, __func__, rid, rtype);
  419. if (HERMES_RECLEN_TO_BYTES(rlength) > bufsize)
  420. printk(KERN_WARNING "hermes @ %p: "
  421. "Truncating LTV record from %d to %d bytes. "
  422. "(rid=0x%04x, len=0x%04x)\n", hw->iobase,
  423. HERMES_RECLEN_TO_BYTES(rlength), bufsize, rid, rlength);
  424. nwords = min((unsigned)rlength - 1, bufsize / 2);
  425. hermes_read_words(hw, dreg, buf, nwords);
  426. return 0;
  427. }
  428. EXPORT_SYMBOL(hermes_read_ltv);
  429. int hermes_write_ltv(hermes_t *hw, int bap, u16 rid,
  430. u16 length, const void *value)
  431. {
  432. int dreg = bap ? HERMES_DATA1 : HERMES_DATA0;
  433. int err = 0;
  434. unsigned count;
  435. if (length == 0)
  436. return -EINVAL;
  437. err = hermes_bap_seek(hw, bap, rid, 0);
  438. if (err)
  439. return err;
  440. hermes_write_reg(hw, dreg, length);
  441. hermes_write_reg(hw, dreg, rid);
  442. count = length - 1;
  443. hermes_write_bytes(hw, dreg, value, count << 1);
  444. err = hermes_docmd_wait(hw, HERMES_CMD_ACCESS | HERMES_CMD_WRITE,
  445. rid, NULL);
  446. return err;
  447. }
  448. EXPORT_SYMBOL(hermes_write_ltv);
  449. static int __init init_hermes(void)
  450. {
  451. return 0;
  452. }
  453. static void __exit exit_hermes(void)
  454. {
  455. }
  456. module_init(init_hermes);
  457. module_exit(exit_hermes);