hermes.c 13 KB

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