hermes.c 13 KB

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