blob: eba0d9d2b7c53015bc96c4f909e6e728faf2e52e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* 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
41#include <linux/config.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/kernel.h>
Pavel Roskinef846bf2005-09-23 04:18:07 -040044#include <linux/init.h>
45#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47#include "hermes.h"
48
49MODULE_DESCRIPTION("Low-level driver helper for Lucent Hermes chipset and Prism II HFA384x wireless MAC controller");
50MODULE_AUTHOR("Pavel Roskin <proski@gnu.org>"
51 " & David Gibson <hermes@gibson.dropbear.id.au>");
52MODULE_LICENSE("Dual MPL/GPL");
53
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 */
59
60/*
61 * Debugging helpers
62 */
63
64#define DMSG(stuff...) do {printk(KERN_DEBUG "hermes @ %p: " , hw->iobase); \
65 printk(stuff);} while (0)
66
67#undef HERMES_DEBUG
68#ifdef HERMES_DEBUG
69#include <stdarg.h>
70
71#define DEBUG(lvl, stuff...) if ( (lvl) <= HERMES_DEBUG) DMSG(stuff)
72
73#else /* ! HERMES_DEBUG */
74
75#define DEBUG(lvl, stuff...) do { } while (0)
76
77#endif /* ! HERMES_DEBUG */
78
79
80/*
81 * Internal functions
82 */
83
84/* Issue a command to the chip. Waiting for it to complete is the caller's
85 problem.
86
87 Returns -EBUSY if the command register is busy, 0 on success.
88
89 Callable from any context.
90*/
91static int hermes_issue_cmd(hermes_t *hw, u16 cmd, u16 param0)
92{
93 int k = CMD_BUSY_TIMEOUT;
94 u16 reg;
95
96 /* First wait for the command register to unbusy */
97 reg = hermes_read_regn(hw, CMD);
98 while ( (reg & HERMES_CMD_BUSY) && k ) {
99 k--;
100 udelay(1);
101 reg = hermes_read_regn(hw, CMD);
102 }
103 if (reg & HERMES_CMD_BUSY) {
104 return -EBUSY;
105 }
106
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);
111
112 return 0;
113}
114
115/*
116 * Function definitions
117 */
118
119void hermes_struct_init(hermes_t *hw, void __iomem *address, int reg_spacing)
120{
121 hw->iobase = address;
122 hw->reg_spacing = reg_spacing;
123 hw->inten = 0x0;
124
125#ifdef HERMES_DEBUG_BUFFER
126 hw->dbufp = 0;
127 memset(&hw->dbuf, 0xff, sizeof(hw->dbuf));
128 memset(&hw->profile, 0, sizeof(hw->profile));
129#endif
130}
131
132int hermes_init(hermes_t *hw)
133{
134 u16 status, reg;
135 int err = 0;
136 int k;
137
138 /* We don't want to be interrupted while resetting the chipset */
139 hw->inten = 0x0;
140 hermes_write_regn(hw, INTEN, 0);
141 hermes_write_regn(hw, EVACK, 0xffff);
142
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 */
154 return -ENODEV;
155
156 k--;
157 udelay(1);
158 reg = hermes_read_regn(hw, CMD);
159 }
160
161 /* No need to explicitly handle the timeout - if we've timed
162 out hermes_issue_cmd() will probably return -EBUSY below */
163
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);
169
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);
173 if (err)
174 return err;
175
176 reg = hermes_read_regn(hw, EVSTAT);
177 k = CMD_INIT_TIMEOUT;
178 while ( (! (reg & HERMES_EV_CMD)) && k) {
179 k--;
180 udelay(10);
181 reg = hermes_read_regn(hw, EVSTAT);
182 }
183
184 hermes_write_regn(hw, SWSUPPORT0, HERMES_MAGIC);
185
186 if (! hermes_present(hw)) {
187 DEBUG(0, "hermes @ 0x%x: Card removed during reset.\n",
188 hw->iobase);
189 err = -ENODEV;
190 goto out;
191 }
192
193 if (! (reg & HERMES_EV_CMD)) {
194 printk(KERN_ERR "hermes @ %p: "
195 "Timeout waiting for card to reset (reg=0x%04x)!\n",
196 hw->iobase, reg);
197 err = -ETIMEDOUT;
198 goto out;
199 }
200
201 status = hermes_read_regn(hw, STATUS);
202
203 hermes_write_regn(hw, EVACK, HERMES_EV_CMD);
204
205 if (status & HERMES_STATUS_RESULT)
206 err = -EIO;
207
208 out:
209 return err;
210}
211
212/* Issue a command to the chip, and (busy!) wait for it to
213 * complete.
214 *
215 * Returns: < 0 on internal error, 0 on success, > 0 on error returned by the firmware
216 *
217 * Callable from any context, but locking is your problem. */
218int hermes_docmd_wait(hermes_t *hw, u16 cmd, u16 parm0,
219 struct hermes_response *resp)
220{
221 int err;
222 int k;
223 u16 reg;
224 u16 status;
225
226 err = hermes_issue_cmd(hw, cmd, parm0);
227 if (err) {
228 if (! hermes_present(hw)) {
229 if (net_ratelimit())
230 printk(KERN_WARNING "hermes @ %p: "
231 "Card removed while issuing command "
232 "0x%04x.\n", hw->iobase, cmd);
233 err = -ENODEV;
234 } else
235 if (net_ratelimit())
236 printk(KERN_ERR "hermes @ %p: "
237 "Error %d issuing command 0x%04x.\n",
238 hw->iobase, err, cmd);
239 goto out;
240 }
241
242 reg = hermes_read_regn(hw, EVSTAT);
243 k = CMD_COMPL_TIMEOUT;
244 while ( (! (reg & HERMES_EV_CMD)) && k) {
245 k--;
246 udelay(10);
247 reg = hermes_read_regn(hw, EVSTAT);
248 }
249
250 if (! hermes_present(hw)) {
251 printk(KERN_WARNING "hermes @ %p: Card removed "
252 "while waiting for command 0x%04x completion.\n",
253 hw->iobase, cmd);
254 err = -ENODEV;
255 goto out;
256 }
257
258 if (! (reg & HERMES_EV_CMD)) {
259 printk(KERN_ERR "hermes @ %p: Timeout waiting for "
260 "command 0x%04x completion.\n", hw->iobase, cmd);
261 err = -ETIMEDOUT;
262 goto out;
263 }
264
265 status = hermes_read_regn(hw, STATUS);
266 if (resp) {
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);
271 }
272
273 hermes_write_regn(hw, EVACK, HERMES_EV_CMD);
274
275 if (status & HERMES_STATUS_RESULT)
276 err = -EIO;
277
278 out:
279 return err;
280}
281
282int hermes_allocate(hermes_t *hw, u16 size, u16 *fid)
283{
284 int err = 0;
285 int k;
286 u16 reg;
287
288 if ( (size < HERMES_ALLOC_LEN_MIN) || (size > HERMES_ALLOC_LEN_MAX) )
289 return -EINVAL;
290
291 err = hermes_docmd_wait(hw, HERMES_CMD_ALLOC, size, NULL);
292 if (err) {
293 return err;
294 }
295
296 reg = hermes_read_regn(hw, EVSTAT);
297 k = ALLOC_COMPL_TIMEOUT;
298 while ( (! (reg & HERMES_EV_ALLOC)) && k) {
299 k--;
300 udelay(10);
301 reg = hermes_read_regn(hw, EVSTAT);
302 }
303
304 if (! hermes_present(hw)) {
305 printk(KERN_WARNING "hermes @ %p: "
306 "Card removed waiting for frame allocation.\n",
307 hw->iobase);
308 return -ENODEV;
309 }
310
311 if (! (reg & HERMES_EV_ALLOC)) {
312 printk(KERN_ERR "hermes @ %p: "
313 "Timeout waiting for frame allocation\n",
314 hw->iobase);
315 return -ETIMEDOUT;
316 }
317
318 *fid = hermes_read_regn(hw, ALLOCFID);
319 hermes_write_regn(hw, EVACK, HERMES_EV_ALLOC);
320
321 return 0;
322}
323
324
325/* Set up a BAP to read a particular chunk of data from card's internal buffer.
326 *
327 * Returns: < 0 on internal failure (errno), 0 on success, >0 on error
328 * from firmware
329 *
330 * Callable from any context */
331static int hermes_bap_seek(hermes_t *hw, int bap, u16 id, u16 offset)
332{
333 int sreg = bap ? HERMES_SELECT1 : HERMES_SELECT0;
334 int oreg = bap ? HERMES_OFFSET1 : HERMES_OFFSET0;
335 int k;
336 u16 reg;
337
338 /* Paranoia.. */
339 if ( (offset > HERMES_BAP_OFFSET_MAX) || (offset % 2) )
340 return -EINVAL;
341
342 k = HERMES_BAP_BUSY_TIMEOUT;
343 reg = hermes_read_reg(hw, oreg);
344 while ((reg & HERMES_OFFSET_BUSY) && k) {
345 k--;
346 udelay(1);
347 reg = hermes_read_reg(hw, oreg);
348 }
349
350#ifdef HERMES_DEBUG_BUFFER
351 hw->profile[HERMES_BAP_BUSY_TIMEOUT - k]++;
352
353 if (k < HERMES_BAP_BUSY_TIMEOUT) {
354 struct hermes_debug_entry *e =
355 &hw->dbuf[(hw->dbufp++) % HERMES_DEBUG_BUFSIZE];
356 e->bap = bap;
357 e->id = id;
358 e->offset = offset;
359 e->cycles = HERMES_BAP_BUSY_TIMEOUT - k;
360 }
361#endif
362
363 if (reg & HERMES_OFFSET_BUSY)
364 return -ETIMEDOUT;
365
366 /* Now we actually set up the transfer */
367 hermes_write_reg(hw, sreg, id);
368 hermes_write_reg(hw, oreg, offset);
369
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) {
374 k--;
375 udelay(1);
376 reg = hermes_read_reg(hw, oreg);
377 }
378
379 if (reg != offset) {
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",
383 reg, id, offset);
384
385 if (reg & HERMES_OFFSET_BUSY) {
386 return -ETIMEDOUT;
387 }
388
389 return -EIO; /* error or wrong offset */
390 }
391
392 return 0;
393}
394
395/* Read a block of data from the chip's buffer, via the
396 * BAP. Synchronization/serialization is the caller's problem. len
397 * must be even.
398 *
399 * Returns: < 0 on internal failure (errno), 0 on success, > 0 on error from firmware
400 */
401int hermes_bap_pread(hermes_t *hw, int bap, void *buf, unsigned len,
402 u16 id, u16 offset)
403{
404 int dreg = bap ? HERMES_DATA1 : HERMES_DATA0;
405 int err = 0;
406
407 if ( (len < 0) || (len % 2) )
408 return -EINVAL;
409
410 err = hermes_bap_seek(hw, bap, id, offset);
411 if (err)
412 goto out;
413
414 /* Actually do the transfer */
415 hermes_read_words(hw, dreg, buf, len/2);
416
417 out:
418 return err;
419}
420
421/* Write a block of data to the chip's buffer, via the
422 * BAP. Synchronization/serialization is the caller's problem. len
423 * must be even.
424 *
425 * Returns: < 0 on internal failure (errno), 0 on success, > 0 on error from firmware
426 */
427int hermes_bap_pwrite(hermes_t *hw, int bap, const void *buf, unsigned len,
428 u16 id, u16 offset)
429{
430 int dreg = bap ? HERMES_DATA1 : HERMES_DATA0;
431 int err = 0;
432
433 if ( (len < 0) || (len % 2) )
434 return -EINVAL;
435
436 err = hermes_bap_seek(hw, bap, id, offset);
437 if (err)
438 goto out;
439
440 /* Actually do the transfer */
441 hermes_write_words(hw, dreg, buf, len/2);
442
443 out:
444 return err;
445}
446
447/* Read a Length-Type-Value record from the card.
448 *
449 * If length is NULL, we ignore the length read from the card, and
450 * read the entire buffer regardless. This is useful because some of
451 * the configuration records appear to have incorrect lengths in
452 * practice.
453 *
454 * Callable from user or bh context. */
455int hermes_read_ltv(hermes_t *hw, int bap, u16 rid, unsigned bufsize,
456 u16 *length, void *buf)
457{
458 int err = 0;
459 int dreg = bap ? HERMES_DATA1 : HERMES_DATA0;
460 u16 rlength, rtype;
461 unsigned nwords;
462
463 if ( (bufsize < 0) || (bufsize % 2) )
464 return -EINVAL;
465
466 err = hermes_docmd_wait(hw, HERMES_CMD_ACCESS, rid, NULL);
467 if (err)
468 return err;
469
470 err = hermes_bap_seek(hw, bap, rid, 0);
471 if (err)
472 return err;
473
474 rlength = hermes_read_reg(hw, dreg);
475
476 if (! rlength)
477 return -ENODATA;
478
479 rtype = hermes_read_reg(hw, dreg);
480
481 if (length)
482 *length = rlength;
483
484 if (rtype != rid)
485 printk(KERN_WARNING "hermes @ %p: %s(): "
486 "rid (0x%04x) does not match type (0x%04x)\n",
487 hw->iobase, __FUNCTION__, rid, rtype);
488 if (HERMES_RECLEN_TO_BYTES(rlength) > bufsize)
489 printk(KERN_WARNING "hermes @ %p: "
490 "Truncating LTV record from %d to %d bytes. "
491 "(rid=0x%04x, len=0x%04x)\n", hw->iobase,
492 HERMES_RECLEN_TO_BYTES(rlength), bufsize, rid, rlength);
493
494 nwords = min((unsigned)rlength - 1, bufsize / 2);
495 hermes_read_words(hw, dreg, buf, nwords);
496
497 return 0;
498}
499
500int hermes_write_ltv(hermes_t *hw, int bap, u16 rid,
501 u16 length, const void *value)
502{
503 int dreg = bap ? HERMES_DATA1 : HERMES_DATA0;
504 int err = 0;
505 unsigned count;
506
507 if (length == 0)
508 return -EINVAL;
509
510 err = hermes_bap_seek(hw, bap, rid, 0);
511 if (err)
512 return err;
513
514 hermes_write_reg(hw, dreg, length);
515 hermes_write_reg(hw, dreg, rid);
516
517 count = length - 1;
518
519 hermes_write_words(hw, dreg, value, count);
520
521 err = hermes_docmd_wait(hw, HERMES_CMD_ACCESS | HERMES_CMD_WRITE,
522 rid, NULL);
523
524 return err;
525}
526
527EXPORT_SYMBOL(hermes_struct_init);
528EXPORT_SYMBOL(hermes_init);
529EXPORT_SYMBOL(hermes_docmd_wait);
530EXPORT_SYMBOL(hermes_allocate);
531
532EXPORT_SYMBOL(hermes_bap_pread);
533EXPORT_SYMBOL(hermes_bap_pwrite);
534EXPORT_SYMBOL(hermes_read_ltv);
535EXPORT_SYMBOL(hermes_write_ltv);
536
537static int __init init_hermes(void)
538{
539 return 0;
540}
541
542static void __exit exit_hermes(void)
543{
544}
545
546module_init(init_hermes);
547module_exit(exit_hermes);