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
8 * This is not a complete driver, just low-level access routines for
9 * the MAC controller itself.
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
16 * Copyright (C) 2000, David Gibson, Linuxcare Australia.
17 * (C) Copyright David Gibson, IBM Corp. 2001-2003.
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/
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.
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.
41 #include <linux/module.h>
42 #include <linux/kernel.h>
43 #include <linux/init.h>
44 #include <linux/delay.h>
48 /* These are maximum timeouts. Most often, card wil react much faster */
49 #define CMD_BUSY_TIMEOUT (100) /* In iterations of ~1us */
50 #define CMD_INIT_TIMEOUT (50000) /* in iterations of ~10us */
51 #define CMD_COMPL_TIMEOUT (20000) /* in iterations of ~10us */
52 #define ALLOC_COMPL_TIMEOUT (1000) /* in iterations of ~10us */
58 #define DMSG(stuff...) do {printk(KERN_DEBUG "hermes @ %p: " , hw->iobase); \
59 printk(stuff); } while (0)
65 #define DEBUG(lvl, stuff...) if ((lvl) <= HERMES_DEBUG) DMSG(stuff)
67 #else /* ! HERMES_DEBUG */
69 #define DEBUG(lvl, stuff...) do { } while (0)
71 #endif /* ! HERMES_DEBUG */
78 /* Issue a command to the chip. Waiting for it to complete is the caller's
81 Returns -EBUSY if the command register is busy, 0 on success.
83 Callable from any context.
85 static int hermes_issue_cmd(hermes_t *hw, u16 cmd, u16 param0,
86 u16 param1, u16 param2)
88 int k = CMD_BUSY_TIMEOUT;
91 /* First wait for the command register to unbusy */
92 reg = hermes_read_regn(hw, CMD);
93 while ((reg & HERMES_CMD_BUSY) && k) {
96 reg = hermes_read_regn(hw, CMD);
98 if (reg & HERMES_CMD_BUSY)
101 hermes_write_regn(hw, PARAM2, param2);
102 hermes_write_regn(hw, PARAM1, param1);
103 hermes_write_regn(hw, PARAM0, param0);
104 hermes_write_regn(hw, CMD, cmd);
110 * Function definitions
113 /* For doing cmds that wipe the magic constant in SWSUPPORT0 */
114 int hermes_doicmd_wait(hermes_t *hw, u16 cmd,
115 u16 parm0, u16 parm1, u16 parm2,
116 struct hermes_response *resp)
122 err = hermes_issue_cmd(hw, cmd, parm0, parm1, parm2);
126 reg = hermes_read_regn(hw, EVSTAT);
127 k = CMD_INIT_TIMEOUT;
128 while ((!(reg & HERMES_EV_CMD)) && k) {
131 reg = hermes_read_regn(hw, EVSTAT);
134 hermes_write_regn(hw, SWSUPPORT0, HERMES_MAGIC);
136 if (!hermes_present(hw)) {
137 DEBUG(0, "hermes @ 0x%x: Card removed during reset.\n",
143 if (!(reg & HERMES_EV_CMD)) {
144 printk(KERN_ERR "hermes @ %p: "
145 "Timeout waiting for card to reset (reg=0x%04x)!\n",
151 status = hermes_read_regn(hw, STATUS);
153 resp->status = status;
154 resp->resp0 = hermes_read_regn(hw, RESP0);
155 resp->resp1 = hermes_read_regn(hw, RESP1);
156 resp->resp2 = hermes_read_regn(hw, RESP2);
159 hermes_write_regn(hw, EVACK, HERMES_EV_CMD);
161 if (status & HERMES_STATUS_RESULT)
166 EXPORT_SYMBOL(hermes_doicmd_wait);
168 void hermes_struct_init(hermes_t *hw, void __iomem *address, int reg_spacing)
170 hw->iobase = address;
171 hw->reg_spacing = reg_spacing;
174 EXPORT_SYMBOL(hermes_struct_init);
176 int hermes_init(hermes_t *hw)
182 /* We don't want to be interrupted while resetting the chipset */
184 hermes_write_regn(hw, INTEN, 0);
185 hermes_write_regn(hw, EVACK, 0xffff);
187 /* Normally it's a "can't happen" for the command register to
188 be busy when we go to issue a command because we are
189 serializing all commands. However we want to have some
190 chance of resetting the card even if it gets into a stupid
191 state, so we actually wait to see if the command register
192 will unbusy itself here. */
193 k = CMD_BUSY_TIMEOUT;
194 reg = hermes_read_regn(hw, CMD);
195 while (k && (reg & HERMES_CMD_BUSY)) {
196 if (reg == 0xffff) /* Special case - the card has probably been
197 removed, so don't wait for the timeout */
202 reg = hermes_read_regn(hw, CMD);
205 /* No need to explicitly handle the timeout - if we've timed
206 out hermes_issue_cmd() will probably return -EBUSY below */
208 /* According to the documentation, EVSTAT may contain
209 obsolete event occurrence information. We have to acknowledge
210 it by writing EVACK. */
211 reg = hermes_read_regn(hw, EVSTAT);
212 hermes_write_regn(hw, EVACK, reg);
214 /* We don't use hermes_docmd_wait here, because the reset wipes
215 the magic constant in SWSUPPORT0 away, and it gets confused */
216 err = hermes_doicmd_wait(hw, HERMES_CMD_INIT, 0, 0, 0, NULL);
220 EXPORT_SYMBOL(hermes_init);
222 /* Issue a command to the chip, and (busy!) wait for it to
226 * < 0 on internal error
228 * > 0 on error returned by the firmware
230 * Callable from any context, but locking is your problem. */
231 int hermes_docmd_wait(hermes_t *hw, u16 cmd, u16 parm0,
232 struct hermes_response *resp)
239 err = hermes_issue_cmd(hw, cmd, parm0, 0, 0);
241 if (!hermes_present(hw)) {
243 printk(KERN_WARNING "hermes @ %p: "
244 "Card removed while issuing command "
245 "0x%04x.\n", hw->iobase, cmd);
249 printk(KERN_ERR "hermes @ %p: "
250 "Error %d issuing command 0x%04x.\n",
251 hw->iobase, err, cmd);
255 reg = hermes_read_regn(hw, EVSTAT);
256 k = CMD_COMPL_TIMEOUT;
257 while ((!(reg & HERMES_EV_CMD)) && k) {
260 reg = hermes_read_regn(hw, EVSTAT);
263 if (!hermes_present(hw)) {
264 printk(KERN_WARNING "hermes @ %p: Card removed "
265 "while waiting for command 0x%04x completion.\n",
271 if (!(reg & HERMES_EV_CMD)) {
272 printk(KERN_ERR "hermes @ %p: Timeout waiting for "
273 "command 0x%04x completion.\n", hw->iobase, cmd);
278 status = hermes_read_regn(hw, STATUS);
280 resp->status = status;
281 resp->resp0 = hermes_read_regn(hw, RESP0);
282 resp->resp1 = hermes_read_regn(hw, RESP1);
283 resp->resp2 = hermes_read_regn(hw, RESP2);
286 hermes_write_regn(hw, EVACK, HERMES_EV_CMD);
288 if (status & HERMES_STATUS_RESULT)
294 EXPORT_SYMBOL(hermes_docmd_wait);
296 int hermes_allocate(hermes_t *hw, u16 size, u16 *fid)
302 if ((size < HERMES_ALLOC_LEN_MIN) || (size > HERMES_ALLOC_LEN_MAX))
305 err = hermes_docmd_wait(hw, HERMES_CMD_ALLOC, size, NULL);
309 reg = hermes_read_regn(hw, EVSTAT);
310 k = ALLOC_COMPL_TIMEOUT;
311 while ((!(reg & HERMES_EV_ALLOC)) && k) {
314 reg = hermes_read_regn(hw, EVSTAT);
317 if (!hermes_present(hw)) {
318 printk(KERN_WARNING "hermes @ %p: "
319 "Card removed waiting for frame allocation.\n",
324 if (!(reg & HERMES_EV_ALLOC)) {
325 printk(KERN_ERR "hermes @ %p: "
326 "Timeout waiting for frame allocation\n",
331 *fid = hermes_read_regn(hw, ALLOCFID);
332 hermes_write_regn(hw, EVACK, HERMES_EV_ALLOC);
336 EXPORT_SYMBOL(hermes_allocate);
338 /* Set up a BAP to read a particular chunk of data from card's internal buffer.
341 * < 0 on internal failure (errno)
346 * Callable from any context */
347 static int hermes_bap_seek(hermes_t *hw, int bap, u16 id, u16 offset)
349 int sreg = bap ? HERMES_SELECT1 : HERMES_SELECT0;
350 int oreg = bap ? HERMES_OFFSET1 : HERMES_OFFSET0;
355 if ((offset > HERMES_BAP_OFFSET_MAX) || (offset % 2))
358 k = HERMES_BAP_BUSY_TIMEOUT;
359 reg = hermes_read_reg(hw, oreg);
360 while ((reg & HERMES_OFFSET_BUSY) && k) {
363 reg = hermes_read_reg(hw, oreg);
366 if (reg & HERMES_OFFSET_BUSY)
369 /* Now we actually set up the transfer */
370 hermes_write_reg(hw, sreg, id);
371 hermes_write_reg(hw, oreg, offset);
373 /* Wait for the BAP to be ready */
374 k = HERMES_BAP_BUSY_TIMEOUT;
375 reg = hermes_read_reg(hw, oreg);
376 while ((reg & (HERMES_OFFSET_BUSY | HERMES_OFFSET_ERR)) && k) {
379 reg = hermes_read_reg(hw, oreg);
383 printk(KERN_ERR "hermes @ %p: BAP%d offset %s: "
384 "reg=0x%x id=0x%x offset=0x%x\n", hw->iobase, bap,
385 (reg & HERMES_OFFSET_BUSY) ? "timeout" : "error",
388 if (reg & HERMES_OFFSET_BUSY)
391 return -EIO; /* error or wrong offset */
397 /* Read a block of data from the chip's buffer, via the
398 * BAP. Synchronization/serialization is the caller's problem. len
402 * < 0 on internal failure (errno)
404 * > 0 on error from firmware
406 int hermes_bap_pread(hermes_t *hw, int bap, void *buf, int len,
409 int dreg = bap ? HERMES_DATA1 : HERMES_DATA0;
412 if ((len < 0) || (len % 2))
415 err = hermes_bap_seek(hw, bap, id, offset);
419 /* Actually do the transfer */
420 hermes_read_words(hw, dreg, buf, len/2);
425 EXPORT_SYMBOL(hermes_bap_pread);
427 /* Write a block of data to the chip's buffer, via the
428 * BAP. Synchronization/serialization is the caller's problem.
431 * < 0 on internal failure (errno)
433 * > 0 on error from firmware
435 int hermes_bap_pwrite(hermes_t *hw, int bap, const void *buf, int len,
438 int dreg = bap ? HERMES_DATA1 : HERMES_DATA0;
444 err = hermes_bap_seek(hw, bap, id, offset);
448 /* Actually do the transfer */
449 hermes_write_bytes(hw, dreg, buf, len);
454 EXPORT_SYMBOL(hermes_bap_pwrite);
456 /* Read a Length-Type-Value record from the card.
458 * If length is NULL, we ignore the length read from the card, and
459 * read the entire buffer regardless. This is useful because some of
460 * the configuration records appear to have incorrect lengths in
463 * Callable from user or bh context. */
464 int hermes_read_ltv(hermes_t *hw, int bap, u16 rid, unsigned bufsize,
465 u16 *length, void *buf)
468 int dreg = bap ? HERMES_DATA1 : HERMES_DATA0;
472 if ((bufsize < 0) || (bufsize % 2))
475 err = hermes_docmd_wait(hw, HERMES_CMD_ACCESS, rid, NULL);
479 err = hermes_bap_seek(hw, bap, rid, 0);
483 rlength = hermes_read_reg(hw, dreg);
488 rtype = hermes_read_reg(hw, dreg);
494 printk(KERN_WARNING "hermes @ %p: %s(): "
495 "rid (0x%04x) does not match type (0x%04x)\n",
496 hw->iobase, __func__, rid, rtype);
497 if (HERMES_RECLEN_TO_BYTES(rlength) > bufsize)
498 printk(KERN_WARNING "hermes @ %p: "
499 "Truncating LTV record from %d to %d bytes. "
500 "(rid=0x%04x, len=0x%04x)\n", hw->iobase,
501 HERMES_RECLEN_TO_BYTES(rlength), bufsize, rid, rlength);
503 nwords = min((unsigned)rlength - 1, bufsize / 2);
504 hermes_read_words(hw, dreg, buf, nwords);
508 EXPORT_SYMBOL(hermes_read_ltv);
510 int hermes_write_ltv(hermes_t *hw, int bap, u16 rid,
511 u16 length, const void *value)
513 int dreg = bap ? HERMES_DATA1 : HERMES_DATA0;
520 err = hermes_bap_seek(hw, bap, rid, 0);
524 hermes_write_reg(hw, dreg, length);
525 hermes_write_reg(hw, dreg, rid);
529 hermes_write_bytes(hw, dreg, value, count << 1);
531 err = hermes_docmd_wait(hw, HERMES_CMD_ACCESS | HERMES_CMD_WRITE,
536 EXPORT_SYMBOL(hermes_write_ltv);