hermes.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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 and Prism II HFA384x wireless MAC controller");
  46. MODULE_AUTHOR("Pavel Roskin <proski@gnu.org>"
  47. " & David Gibson <hermes@gibson.dropbear.id.au>");
  48. MODULE_LICENSE("Dual MPL/GPL");
  49. /* These are maximum timeouts. Most often, card wil react much faster */
  50. #define CMD_BUSY_TIMEOUT (100) /* In iterations of ~1us */
  51. #define CMD_INIT_TIMEOUT (50000) /* in iterations of ~10us */
  52. #define CMD_COMPL_TIMEOUT (20000) /* in iterations of ~10us */
  53. #define ALLOC_COMPL_TIMEOUT (1000) /* in iterations of ~10us */
  54. /*
  55. * Debugging helpers
  56. */
  57. #define DMSG(stuff...) do {printk(KERN_DEBUG "hermes @ %p: " , hw->iobase); \
  58. printk(stuff);} while (0)
  59. #undef HERMES_DEBUG
  60. #ifdef HERMES_DEBUG
  61. #include <stdarg.h>
  62. #define DEBUG(lvl, stuff...) if ( (lvl) <= HERMES_DEBUG) DMSG(stuff)
  63. #else /* ! HERMES_DEBUG */
  64. #define DEBUG(lvl, stuff...) do { } while (0)
  65. #endif /* ! HERMES_DEBUG */
  66. /*
  67. * Internal functions
  68. */
  69. /* Issue a command to the chip. Waiting for it to complete is the caller's
  70. problem.
  71. Returns -EBUSY if the command register is busy, 0 on success.
  72. Callable from any context.
  73. */
  74. static int hermes_issue_cmd(hermes_t *hw, u16 cmd, u16 param0,
  75. u16 param1, u16 param2)
  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, 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 removed,
  170. 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: < 0 on internal error, 0 on success, > 0 on error returned by the firmware
  193. *
  194. * Callable from any context, but locking is your problem. */
  195. int hermes_docmd_wait(hermes_t *hw, u16 cmd, u16 parm0,
  196. struct hermes_response *resp)
  197. {
  198. int err;
  199. int k;
  200. u16 reg;
  201. u16 status;
  202. err = hermes_issue_cmd(hw, cmd, parm0, 0, 0);
  203. if (err) {
  204. if (! hermes_present(hw)) {
  205. if (net_ratelimit())
  206. printk(KERN_WARNING "hermes @ %p: "
  207. "Card removed while issuing command "
  208. "0x%04x.\n", hw->iobase, cmd);
  209. err = -ENODEV;
  210. } else
  211. if (net_ratelimit())
  212. printk(KERN_ERR "hermes @ %p: "
  213. "Error %d issuing command 0x%04x.\n",
  214. hw->iobase, err, cmd);
  215. goto out;
  216. }
  217. reg = hermes_read_regn(hw, EVSTAT);
  218. k = CMD_COMPL_TIMEOUT;
  219. while ( (! (reg & HERMES_EV_CMD)) && k) {
  220. k--;
  221. udelay(10);
  222. reg = hermes_read_regn(hw, EVSTAT);
  223. }
  224. if (! hermes_present(hw)) {
  225. printk(KERN_WARNING "hermes @ %p: Card removed "
  226. "while waiting for command 0x%04x completion.\n",
  227. hw->iobase, cmd);
  228. err = -ENODEV;
  229. goto out;
  230. }
  231. if (! (reg & HERMES_EV_CMD)) {
  232. printk(KERN_ERR "hermes @ %p: Timeout waiting for "
  233. "command 0x%04x completion.\n", hw->iobase, cmd);
  234. err = -ETIMEDOUT;
  235. goto out;
  236. }
  237. status = hermes_read_regn(hw, STATUS);
  238. if (resp) {
  239. resp->status = status;
  240. resp->resp0 = hermes_read_regn(hw, RESP0);
  241. resp->resp1 = hermes_read_regn(hw, RESP1);
  242. resp->resp2 = hermes_read_regn(hw, RESP2);
  243. }
  244. hermes_write_regn(hw, EVACK, HERMES_EV_CMD);
  245. if (status & HERMES_STATUS_RESULT)
  246. err = -EIO;
  247. out:
  248. return err;
  249. }
  250. EXPORT_SYMBOL(hermes_docmd_wait);
  251. int hermes_allocate(hermes_t *hw, u16 size, u16 *fid)
  252. {
  253. int err = 0;
  254. int k;
  255. u16 reg;
  256. if ( (size < HERMES_ALLOC_LEN_MIN) || (size > HERMES_ALLOC_LEN_MAX) )
  257. return -EINVAL;
  258. err = hermes_docmd_wait(hw, HERMES_CMD_ALLOC, size, NULL);
  259. if (err) {
  260. return err;
  261. }
  262. reg = hermes_read_regn(hw, EVSTAT);
  263. k = ALLOC_COMPL_TIMEOUT;
  264. while ( (! (reg & HERMES_EV_ALLOC)) && k) {
  265. k--;
  266. udelay(10);
  267. reg = hermes_read_regn(hw, EVSTAT);
  268. }
  269. if (! hermes_present(hw)) {
  270. printk(KERN_WARNING "hermes @ %p: "
  271. "Card removed waiting for frame allocation.\n",
  272. hw->iobase);
  273. return -ENODEV;
  274. }
  275. if (! (reg & HERMES_EV_ALLOC)) {
  276. printk(KERN_ERR "hermes @ %p: "
  277. "Timeout waiting for frame allocation\n",
  278. hw->iobase);
  279. return -ETIMEDOUT;
  280. }
  281. *fid = hermes_read_regn(hw, ALLOCFID);
  282. hermes_write_regn(hw, EVACK, HERMES_EV_ALLOC);
  283. return 0;
  284. }
  285. EXPORT_SYMBOL(hermes_allocate);
  286. /* Set up a BAP to read a particular chunk of data from card's internal buffer.
  287. *
  288. * Returns: < 0 on internal failure (errno), 0 on success, >0 on error
  289. * from firmware
  290. *
  291. * Callable from any context */
  292. static int hermes_bap_seek(hermes_t *hw, int bap, u16 id, u16 offset)
  293. {
  294. int sreg = bap ? HERMES_SELECT1 : HERMES_SELECT0;
  295. int oreg = bap ? HERMES_OFFSET1 : HERMES_OFFSET0;
  296. int k;
  297. u16 reg;
  298. /* Paranoia.. */
  299. if ( (offset > HERMES_BAP_OFFSET_MAX) || (offset % 2) )
  300. return -EINVAL;
  301. k = HERMES_BAP_BUSY_TIMEOUT;
  302. reg = hermes_read_reg(hw, oreg);
  303. while ((reg & HERMES_OFFSET_BUSY) && k) {
  304. k--;
  305. udelay(1);
  306. reg = hermes_read_reg(hw, oreg);
  307. }
  308. if (reg & HERMES_OFFSET_BUSY)
  309. return -ETIMEDOUT;
  310. /* Now we actually set up the transfer */
  311. hermes_write_reg(hw, sreg, id);
  312. hermes_write_reg(hw, oreg, offset);
  313. /* Wait for the BAP to be ready */
  314. k = HERMES_BAP_BUSY_TIMEOUT;
  315. reg = hermes_read_reg(hw, oreg);
  316. while ( (reg & (HERMES_OFFSET_BUSY | HERMES_OFFSET_ERR)) && k) {
  317. k--;
  318. udelay(1);
  319. reg = hermes_read_reg(hw, oreg);
  320. }
  321. if (reg != offset) {
  322. printk(KERN_ERR "hermes @ %p: BAP%d offset %s: "
  323. "reg=0x%x id=0x%x offset=0x%x\n", hw->iobase, bap,
  324. (reg & HERMES_OFFSET_BUSY) ? "timeout" : "error",
  325. reg, id, offset);
  326. if (reg & HERMES_OFFSET_BUSY) {
  327. return -ETIMEDOUT;
  328. }
  329. return -EIO; /* error or wrong offset */
  330. }
  331. return 0;
  332. }
  333. /* Read a block of data from the chip's buffer, via the
  334. * BAP. Synchronization/serialization is the caller's problem. len
  335. * must be even.
  336. *
  337. * Returns: < 0 on internal failure (errno), 0 on success, > 0 on error from firmware
  338. */
  339. int hermes_bap_pread(hermes_t *hw, int bap, void *buf, int len,
  340. u16 id, u16 offset)
  341. {
  342. int dreg = bap ? HERMES_DATA1 : HERMES_DATA0;
  343. int err = 0;
  344. if ( (len < 0) || (len % 2) )
  345. return -EINVAL;
  346. err = hermes_bap_seek(hw, bap, id, offset);
  347. if (err)
  348. goto out;
  349. /* Actually do the transfer */
  350. hermes_read_words(hw, dreg, buf, len/2);
  351. out:
  352. return err;
  353. }
  354. EXPORT_SYMBOL(hermes_bap_pread);
  355. /* Write a block of data to the chip's buffer, via the
  356. * BAP. Synchronization/serialization is the caller's problem.
  357. *
  358. * Returns: < 0 on internal failure (errno), 0 on success, > 0 on error from firmware
  359. */
  360. int hermes_bap_pwrite(hermes_t *hw, int bap, const void *buf, int len,
  361. u16 id, u16 offset)
  362. {
  363. int dreg = bap ? HERMES_DATA1 : HERMES_DATA0;
  364. int err = 0;
  365. if (len < 0)
  366. return -EINVAL;
  367. err = hermes_bap_seek(hw, bap, id, offset);
  368. if (err)
  369. goto out;
  370. /* Actually do the transfer */
  371. hermes_write_bytes(hw, dreg, buf, len);
  372. out:
  373. return err;
  374. }
  375. EXPORT_SYMBOL(hermes_bap_pwrite);
  376. /* Read a Length-Type-Value record from the card.
  377. *
  378. * If length is NULL, we ignore the length read from the card, and
  379. * read the entire buffer regardless. This is useful because some of
  380. * the configuration records appear to have incorrect lengths in
  381. * practice.
  382. *
  383. * Callable from user or bh context. */
  384. int hermes_read_ltv(hermes_t *hw, int bap, u16 rid, unsigned bufsize,
  385. u16 *length, void *buf)
  386. {
  387. int err = 0;
  388. int dreg = bap ? HERMES_DATA1 : HERMES_DATA0;
  389. u16 rlength, rtype;
  390. unsigned nwords;
  391. if ( (bufsize < 0) || (bufsize % 2) )
  392. return -EINVAL;
  393. err = hermes_docmd_wait(hw, HERMES_CMD_ACCESS, rid, NULL);
  394. if (err)
  395. return err;
  396. err = hermes_bap_seek(hw, bap, rid, 0);
  397. if (err)
  398. return err;
  399. rlength = hermes_read_reg(hw, dreg);
  400. if (! rlength)
  401. return -ENODATA;
  402. rtype = hermes_read_reg(hw, dreg);
  403. if (length)
  404. *length = rlength;
  405. if (rtype != rid)
  406. printk(KERN_WARNING "hermes @ %p: %s(): "
  407. "rid (0x%04x) does not match type (0x%04x)\n",
  408. hw->iobase, __func__, rid, rtype);
  409. if (HERMES_RECLEN_TO_BYTES(rlength) > bufsize)
  410. printk(KERN_WARNING "hermes @ %p: "
  411. "Truncating LTV record from %d to %d bytes. "
  412. "(rid=0x%04x, len=0x%04x)\n", hw->iobase,
  413. HERMES_RECLEN_TO_BYTES(rlength), bufsize, rid, rlength);
  414. nwords = min((unsigned)rlength - 1, bufsize / 2);
  415. hermes_read_words(hw, dreg, buf, nwords);
  416. return 0;
  417. }
  418. EXPORT_SYMBOL(hermes_read_ltv);
  419. int hermes_write_ltv(hermes_t *hw, int bap, u16 rid,
  420. u16 length, const void *value)
  421. {
  422. int dreg = bap ? HERMES_DATA1 : HERMES_DATA0;
  423. int err = 0;
  424. unsigned count;
  425. if (length == 0)
  426. return -EINVAL;
  427. err = hermes_bap_seek(hw, bap, rid, 0);
  428. if (err)
  429. return err;
  430. hermes_write_reg(hw, dreg, length);
  431. hermes_write_reg(hw, dreg, rid);
  432. count = length - 1;
  433. hermes_write_bytes(hw, dreg, value, count << 1);
  434. err = hermes_docmd_wait(hw, HERMES_CMD_ACCESS | HERMES_CMD_WRITE,
  435. rid, NULL);
  436. return err;
  437. }
  438. EXPORT_SYMBOL(hermes_write_ltv);
  439. static int __init init_hermes(void)
  440. {
  441. return 0;
  442. }
  443. static void __exit exit_hermes(void)
  444. {
  445. }
  446. module_init(init_hermes);
  447. module_exit(exit_hermes);