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/config.h>
42 #include <linux/module.h>
43 #include <linux/kernel.h>
44 #include <linux/init.h>
45 #include <linux/delay.h>
49 MODULE_DESCRIPTION("Low-level driver helper for Lucent Hermes chipset and Prism II HFA384x wireless MAC controller");
50 MODULE_AUTHOR("Pavel Roskin <proski@gnu.org>"
51 " & David Gibson <hermes@gibson.dropbear.id.au>");
52 MODULE_LICENSE("Dual MPL/GPL");
54 /* These are maximum timeouts. Most often, card wil react much faster */
55 #define CMD_BUSY_TIMEOUT (100) /* In iterations of ~1us */
56 #define CMD_INIT_TIMEOUT (50000) /* in iterations of ~10us */
57 #define CMD_COMPL_TIMEOUT (20000) /* in iterations of ~10us */
58 #define ALLOC_COMPL_TIMEOUT (1000) /* in iterations of ~10us */
64 #define DMSG(stuff...) do {printk(KERN_DEBUG "hermes @ %p: " , hw->iobase); \
65 printk(stuff);} while (0)
71 #define DEBUG(lvl, stuff...) if ( (lvl) <= HERMES_DEBUG) DMSG(stuff)
73 #else /* ! HERMES_DEBUG */
75 #define DEBUG(lvl, stuff...) do { } while (0)
77 #endif /* ! HERMES_DEBUG */
84 /* Issue a command to the chip. Waiting for it to complete is the caller's
87 Returns -EBUSY if the command register is busy, 0 on success.
89 Callable from any context.
91 static int hermes_issue_cmd(hermes_t *hw, u16 cmd, u16 param0)
93 int k = CMD_BUSY_TIMEOUT;
96 /* First wait for the command register to unbusy */
97 reg = hermes_read_regn(hw, CMD);
98 while ( (reg & HERMES_CMD_BUSY) && k ) {
101 reg = hermes_read_regn(hw, CMD);
103 if (reg & HERMES_CMD_BUSY) {
107 hermes_write_regn(hw, PARAM2, 0);
108 hermes_write_regn(hw, PARAM1, 0);
109 hermes_write_regn(hw, PARAM0, param0);
110 hermes_write_regn(hw, CMD, cmd);
116 * Function definitions
119 void hermes_struct_init(hermes_t *hw, void __iomem *address, int reg_spacing)
121 hw->iobase = address;
122 hw->reg_spacing = reg_spacing;
125 #ifdef HERMES_DEBUG_BUFFER
127 memset(&hw->dbuf, 0xff, sizeof(hw->dbuf));
128 memset(&hw->profile, 0, sizeof(hw->profile));
132 int hermes_init(hermes_t *hw)
138 /* We don't want to be interrupted while resetting the chipset */
140 hermes_write_regn(hw, INTEN, 0);
141 hermes_write_regn(hw, EVACK, 0xffff);
143 /* Normally it's a "can't happen" for the command register to
144 be busy when we go to issue a command because we are
145 serializing all commands. However we want to have some
146 chance of resetting the card even if it gets into a stupid
147 state, so we actually wait to see if the command register
148 will unbusy itself here. */
149 k = CMD_BUSY_TIMEOUT;
150 reg = hermes_read_regn(hw, CMD);
151 while (k && (reg & HERMES_CMD_BUSY)) {
152 if (reg == 0xffff) /* Special case - the card has probably been removed,
153 so don't wait for the timeout */
158 reg = hermes_read_regn(hw, CMD);
161 /* No need to explicitly handle the timeout - if we've timed
162 out hermes_issue_cmd() will probably return -EBUSY below */
164 /* According to the documentation, EVSTAT may contain
165 obsolete event occurrence information. We have to acknowledge
166 it by writing EVACK. */
167 reg = hermes_read_regn(hw, EVSTAT);
168 hermes_write_regn(hw, EVACK, reg);
170 /* We don't use hermes_docmd_wait here, because the reset wipes
171 the magic constant in SWSUPPORT0 away, and it gets confused */
172 err = hermes_issue_cmd(hw, HERMES_CMD_INIT, 0);
176 reg = hermes_read_regn(hw, EVSTAT);
177 k = CMD_INIT_TIMEOUT;
178 while ( (! (reg & HERMES_EV_CMD)) && k) {
181 reg = hermes_read_regn(hw, EVSTAT);
184 hermes_write_regn(hw, SWSUPPORT0, HERMES_MAGIC);
186 if (! hermes_present(hw)) {
187 DEBUG(0, "hermes @ 0x%x: Card removed during reset.\n",
193 if (! (reg & HERMES_EV_CMD)) {
194 printk(KERN_ERR "hermes @ %p: "
195 "Timeout waiting for card to reset (reg=0x%04x)!\n",
201 status = hermes_read_regn(hw, STATUS);
203 hermes_write_regn(hw, EVACK, HERMES_EV_CMD);
205 if (status & HERMES_STATUS_RESULT)
212 /* Issue a command to the chip, and (busy!) wait for it to
215 * Returns: < 0 on internal error, 0 on success, > 0 on error returned by the firmware
217 * Callable from any context, but locking is your problem. */
218 int hermes_docmd_wait(hermes_t *hw, u16 cmd, u16 parm0,
219 struct hermes_response *resp)
226 err = hermes_issue_cmd(hw, cmd, parm0);
228 if (! hermes_present(hw)) {
230 printk(KERN_WARNING "hermes @ %p: "
231 "Card removed while issuing command "
232 "0x%04x.\n", hw->iobase, cmd);
236 printk(KERN_ERR "hermes @ %p: "
237 "Error %d issuing command 0x%04x.\n",
238 hw->iobase, err, cmd);
242 reg = hermes_read_regn(hw, EVSTAT);
243 k = CMD_COMPL_TIMEOUT;
244 while ( (! (reg & HERMES_EV_CMD)) && k) {
247 reg = hermes_read_regn(hw, EVSTAT);
250 if (! hermes_present(hw)) {
251 printk(KERN_WARNING "hermes @ %p: Card removed "
252 "while waiting for command 0x%04x completion.\n",
258 if (! (reg & HERMES_EV_CMD)) {
259 printk(KERN_ERR "hermes @ %p: Timeout waiting for "
260 "command 0x%04x completion.\n", hw->iobase, cmd);
265 status = hermes_read_regn(hw, STATUS);
267 resp->status = status;
268 resp->resp0 = hermes_read_regn(hw, RESP0);
269 resp->resp1 = hermes_read_regn(hw, RESP1);
270 resp->resp2 = hermes_read_regn(hw, RESP2);
273 hermes_write_regn(hw, EVACK, HERMES_EV_CMD);
275 if (status & HERMES_STATUS_RESULT)
282 int hermes_allocate(hermes_t *hw, u16 size, u16 *fid)
288 if ( (size < HERMES_ALLOC_LEN_MIN) || (size > HERMES_ALLOC_LEN_MAX) )
291 err = hermes_docmd_wait(hw, HERMES_CMD_ALLOC, size, NULL);
296 reg = hermes_read_regn(hw, EVSTAT);
297 k = ALLOC_COMPL_TIMEOUT;
298 while ( (! (reg & HERMES_EV_ALLOC)) && k) {
301 reg = hermes_read_regn(hw, EVSTAT);
304 if (! hermes_present(hw)) {
305 printk(KERN_WARNING "hermes @ %p: "
306 "Card removed waiting for frame allocation.\n",
311 if (! (reg & HERMES_EV_ALLOC)) {
312 printk(KERN_ERR "hermes @ %p: "
313 "Timeout waiting for frame allocation\n",
318 *fid = hermes_read_regn(hw, ALLOCFID);
319 hermes_write_regn(hw, EVACK, HERMES_EV_ALLOC);
325 /* Set up a BAP to read a particular chunk of data from card's internal buffer.
327 * Returns: < 0 on internal failure (errno), 0 on success, >0 on error
330 * Callable from any context */
331 static int hermes_bap_seek(hermes_t *hw, int bap, u16 id, u16 offset)
333 int sreg = bap ? HERMES_SELECT1 : HERMES_SELECT0;
334 int oreg = bap ? HERMES_OFFSET1 : HERMES_OFFSET0;
339 if ( (offset > HERMES_BAP_OFFSET_MAX) || (offset % 2) )
342 k = HERMES_BAP_BUSY_TIMEOUT;
343 reg = hermes_read_reg(hw, oreg);
344 while ((reg & HERMES_OFFSET_BUSY) && k) {
347 reg = hermes_read_reg(hw, oreg);
350 #ifdef HERMES_DEBUG_BUFFER
351 hw->profile[HERMES_BAP_BUSY_TIMEOUT - k]++;
353 if (k < HERMES_BAP_BUSY_TIMEOUT) {
354 struct hermes_debug_entry *e =
355 &hw->dbuf[(hw->dbufp++) % HERMES_DEBUG_BUFSIZE];
359 e->cycles = HERMES_BAP_BUSY_TIMEOUT - k;
363 if (reg & HERMES_OFFSET_BUSY)
366 /* Now we actually set up the transfer */
367 hermes_write_reg(hw, sreg, id);
368 hermes_write_reg(hw, oreg, offset);
370 /* Wait for the BAP to be ready */
371 k = HERMES_BAP_BUSY_TIMEOUT;
372 reg = hermes_read_reg(hw, oreg);
373 while ( (reg & (HERMES_OFFSET_BUSY | HERMES_OFFSET_ERR)) && k) {
376 reg = hermes_read_reg(hw, oreg);
380 printk(KERN_ERR "hermes @ %p: BAP%d offset %s: "
381 "reg=0x%x id=0x%x offset=0x%x\n", hw->iobase, bap,
382 (reg & HERMES_OFFSET_BUSY) ? "timeout" : "error",
385 if (reg & HERMES_OFFSET_BUSY) {
389 return -EIO; /* error or wrong offset */
395 /* Read a block of data from the chip's buffer, via the
396 * BAP. Synchronization/serialization is the caller's problem. len
399 * Returns: < 0 on internal failure (errno), 0 on success, > 0 on error from firmware
401 int hermes_bap_pread(hermes_t *hw, int bap, void *buf, int len,
404 int dreg = bap ? HERMES_DATA1 : HERMES_DATA0;
407 if ( (len < 0) || (len % 2) )
410 err = hermes_bap_seek(hw, bap, id, offset);
414 /* Actually do the transfer */
415 hermes_read_words(hw, dreg, buf, len/2);
421 /* Write a block of data to the chip's buffer, via the
422 * BAP. Synchronization/serialization is the caller's problem. len
425 * Returns: < 0 on internal failure (errno), 0 on success, > 0 on error from firmware
427 int hermes_bap_pwrite(hermes_t *hw, int bap, const void *buf, int len,
430 int dreg = bap ? HERMES_DATA1 : HERMES_DATA0;
433 if ( (len < 0) || (len % 2) )
436 err = hermes_bap_seek(hw, bap, id, offset);
440 /* Actually do the transfer */
441 hermes_write_words(hw, dreg, buf, len/2);
447 /* Write a block of data to the chip's buffer with padding if
448 * neccessary, via the BAP. Synchronization/serialization is the
449 * caller's problem. len must be even.
451 * Returns: < 0 on internal failure (errno), 0 on success, > 0 on error from firmware
453 int hermes_bap_pwrite_pad(hermes_t *hw, int bap, const void *buf, unsigned data_len, int len,
456 int dreg = bap ? HERMES_DATA1 : HERMES_DATA0;
459 if (len < 0 || len % 2 || data_len > len)
462 err = hermes_bap_seek(hw, bap, id, offset);
466 /* Transfer all the complete words of data */
467 hermes_write_words(hw, dreg, buf, data_len/2);
468 /* If there is an odd byte left over pad and transfer it */
472 end[0] = ((unsigned char *)buf)[data_len - 1];
473 hermes_write_words(hw, dreg, end, 1);
476 /* Now send zeros for the padding */
478 hermes_clear_words(hw, dreg, (len - data_len) / 2);
484 /* Read a Length-Type-Value record from the card.
486 * If length is NULL, we ignore the length read from the card, and
487 * read the entire buffer regardless. This is useful because some of
488 * the configuration records appear to have incorrect lengths in
491 * Callable from user or bh context. */
492 int hermes_read_ltv(hermes_t *hw, int bap, u16 rid, unsigned bufsize,
493 u16 *length, void *buf)
496 int dreg = bap ? HERMES_DATA1 : HERMES_DATA0;
500 if ( (bufsize < 0) || (bufsize % 2) )
503 err = hermes_docmd_wait(hw, HERMES_CMD_ACCESS, rid, NULL);
507 err = hermes_bap_seek(hw, bap, rid, 0);
511 rlength = hermes_read_reg(hw, dreg);
516 rtype = hermes_read_reg(hw, dreg);
522 printk(KERN_WARNING "hermes @ %p: %s(): "
523 "rid (0x%04x) does not match type (0x%04x)\n",
524 hw->iobase, __FUNCTION__, rid, rtype);
525 if (HERMES_RECLEN_TO_BYTES(rlength) > bufsize)
526 printk(KERN_WARNING "hermes @ %p: "
527 "Truncating LTV record from %d to %d bytes. "
528 "(rid=0x%04x, len=0x%04x)\n", hw->iobase,
529 HERMES_RECLEN_TO_BYTES(rlength), bufsize, rid, rlength);
531 nwords = min((unsigned)rlength - 1, bufsize / 2);
532 hermes_read_words(hw, dreg, buf, nwords);
537 int hermes_write_ltv(hermes_t *hw, int bap, u16 rid,
538 u16 length, const void *value)
540 int dreg = bap ? HERMES_DATA1 : HERMES_DATA0;
547 err = hermes_bap_seek(hw, bap, rid, 0);
551 hermes_write_reg(hw, dreg, length);
552 hermes_write_reg(hw, dreg, rid);
556 hermes_write_words(hw, dreg, value, count);
558 err = hermes_docmd_wait(hw, HERMES_CMD_ACCESS | HERMES_CMD_WRITE,
564 EXPORT_SYMBOL(hermes_struct_init);
565 EXPORT_SYMBOL(hermes_init);
566 EXPORT_SYMBOL(hermes_docmd_wait);
567 EXPORT_SYMBOL(hermes_allocate);
569 EXPORT_SYMBOL(hermes_bap_pread);
570 EXPORT_SYMBOL(hermes_bap_pwrite);
571 EXPORT_SYMBOL(hermes_bap_pwrite_pad);
572 EXPORT_SYMBOL(hermes_read_ltv);
573 EXPORT_SYMBOL(hermes_write_ltv);
575 static int __init init_hermes(void)
580 static void __exit exit_hermes(void)
584 module_init(init_hermes);
585 module_exit(exit_hermes);