hermes.c 13 KB

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