File: /usr/src/linux/drivers/net/wireless/hermes.c
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 <hermes@gibson.dropbear.id.au>
17 *
18 * This file distributed under the GPL, version 2. */
19
20 #include <linux/module.h>
21 #include <linux/types.h>
22 #include <linux/threads.h>
23 #include <linux/smp.h>
24 #include <asm/io.h>
25 #include <linux/ptrace.h>
26 #include <linux/delay.h>
27 #include <linux/init.h>
28 #include <linux/kernel.h>
29 #include <asm/errno.h>
30
31 #include "hermes.h"
32
33 static char version[] __initdata = "hermes.c: 1 Aug 2001 David Gibson <hermes@gibson.dropbear.id.au>";
34 MODULE_DESCRIPTION("Low-level driver helper for Lucent Hermes chipset and Prism II HFA384x wireless MAC controller");
35 MODULE_AUTHOR("David Gibson <hermes@gibson.dropbear.id.au>");
36 MODULE_LICENSE("GPL");
37
38 /* These are maximum timeouts. Most often, card wil react much faster */
39 #define CMD_BUSY_TIMEOUT (100) /* In iterations of ~1us */
40 #define CMD_INIT_TIMEOUT (50000) /* in iterations of ~10us */
41 #define CMD_COMPL_TIMEOUT (20000) /* in iterations of ~10us */
42 #define ALLOC_COMPL_TIMEOUT (1000) /* in iterations of ~10us */
43 #define BAP_BUSY_TIMEOUT (500) /* In iterations of ~1us */
44
45 #define MAX(a, b) ( (a) > (b) ? (a) : (b) )
46 #define MIN(a, b) ( (a) < (b) ? (a) : (b) )
47
48 /*
49 * Debugging helpers
50 */
51
52 #undef HERMES_DEBUG
53 #ifdef HERMES_DEBUG
54
55 #include <stdarg.h>
56
57 #define DMSG(stuff...) do {printk(KERN_DEBUG "hermes @ 0x%x: " , hw->iobase); \
58 printk(#stuff);} while (0)
59
60 #define DEBUG(lvl, stuff...) if ( (lvl) <= HERMES_DEBUG) DMSG(#stuff)
61
62 #else /* ! HERMES_DEBUG */
63
64 #define DEBUG(lvl, stuff...) do { } while (0)
65
66 #endif /* ! HERMES_DEBUG */
67
68 /*
69 * Internal functions
70 */
71
72 /* Issue a command to the chip. Waiting for it to complete is the caller's
73 problem.
74
75 Returns -EBUSY if the command register is busy, 0 on success.
76
77 Callable from any context.
78 */
79 static int hermes_issue_cmd(hermes_t *hw, uint16_t cmd, uint16_t param0)
80 {
81 uint16_t reg;
82
83 /* First check that the command register is not busy */
84 reg = hermes_read_regn(hw, CMD);
85 if (reg & HERMES_CMD_BUSY) {
86 return -EBUSY;
87 }
88
89 hermes_write_regn(hw, PARAM2, 0);
90 hermes_write_regn(hw, PARAM1, 0);
91 hermes_write_regn(hw, PARAM0, param0);
92 hermes_write_regn(hw, CMD, cmd);
93
94 return 0;
95 }
96
97 /*
98 * Function definitions
99 */
100
101 void hermes_struct_init(hermes_t *hw, uint io)
102 {
103 hw->iobase = io;
104 hw->inten = 0x0;
105 }
106
107 int hermes_reset(hermes_t *hw)
108 {
109 uint16_t status, reg;
110 int err = 0;
111 int k;
112
113 /* We don't want to be interrupted while resetting the chipset */
114 hw->inten = 0x0;
115 hermes_write_regn(hw, INTEN, 0);
116 hermes_write_regn(hw, EVACK, 0xffff);
117
118 /* Normally it's a "can't happen" for the command register to
119 be busy when we go to issue a command because we are
120 serializing all commands. However we want to have some
121 chance of resetting the card even if it gets into a stupid
122 state, so we actually wait to see if the command register
123 will unbusy itself here. */
124 k = CMD_BUSY_TIMEOUT;
125 reg = hermes_read_regn(hw, CMD);
126 while (k && (reg & HERMES_CMD_BUSY)) {
127 if (reg == 0xffff) /* Special case - the card has probably been removed,
128 so don't wait for the timeout */
129 return -ENODEV;
130
131 k--;
132 udelay(1);
133 reg = hermes_read_regn(hw, CMD);
134 }
135
136 /* No need to explicitly handle the timeout - if we've timed
137 out hermes_issue_cmd() will probably return -EBUSY below */
138
139 /* According to the documentation, EVSTAT may contain
140 obsolete event occurrence information. We have to acknowledge
141 it by writing EVACK. */
142 reg = hermes_read_regn(hw, EVSTAT);
143 hermes_write_regn(hw, EVACK, reg);
144
145 /* We don't use hermes_docmd_wait here, because the reset wipes
146 the magic constant in SWSUPPORT0 away, and it gets confused */
147 err = hermes_issue_cmd(hw, HERMES_CMD_INIT, 0);
148 if (err)
149 return err;
150
151 reg = hermes_read_regn(hw, EVSTAT);
152 k = CMD_INIT_TIMEOUT;
153 while ( (! (reg & HERMES_EV_CMD)) && k) {
154 k--;
155 udelay(10);
156 reg = hermes_read_regn(hw, EVSTAT);
157 }
158
159 DEBUG(0, "Reset completed in %d iterations\n", CMD_INIT_TIMEOUT - k);
160
161 hermes_write_regn(hw, SWSUPPORT0, HERMES_MAGIC);
162
163 if (! hermes_present(hw)) {
164 DEBUG(0, "hermes @ 0x%x: Card removed during reset.\n",
165 hw->iobase);
166 err = -ENODEV;
167 goto out;
168 }
169
170 if (! (reg & HERMES_EV_CMD)) {
171 printk(KERN_ERR "hermes @ 0x%x: Timeout waiting for card to reset (reg=0x%04x)!\n",
172 hw->iobase, reg);
173 err = -ETIMEDOUT;
174 goto out;
175 }
176
177 status = hermes_read_regn(hw, STATUS);
178
179 hermes_write_regn(hw, EVACK, HERMES_EV_CMD);
180
181 err = status & HERMES_STATUS_RESULT;
182
183 out:
184 return err;
185 }
186
187 /* Issue a command to the chip, and (busy!) wait for it to
188 * complete.
189 *
190 * Returns: < 0 on internal error, 0 on success, > 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, uint16_t cmd, uint16_t parm0, hermes_response_t *resp)
194 {
195 int err;
196 int k;
197 uint16_t reg;
198
199 err = hermes_issue_cmd(hw, cmd, parm0);
200 if (err) {
201 if (! hermes_present(hw)) {
202 printk(KERN_WARNING "hermes @ 0x%x: Card removed while issuing command.\n",
203 hw->iobase);
204 err = -ENODEV;
205 } else
206 printk(KERN_ERR "hermes @ 0x%x: CMD register busy in hermes_issue_command().\n",
207 hw->iobase);
208 goto out;
209 }
210
211 reg = hermes_read_regn(hw, EVSTAT);
212 k = CMD_COMPL_TIMEOUT;
213 while ( (! (reg & HERMES_EV_CMD)) && k) {
214 k--;
215 udelay(10);
216 reg = hermes_read_regn(hw, EVSTAT);
217 }
218
219 if (! hermes_present(hw)) {
220 printk(KERN_WARNING "hermes @ 0x%x: Card removed while waiting for command completion.\n",
221 hw->iobase);
222 err = -ENODEV;
223 goto out;
224 }
225
226 if (! (reg & HERMES_EV_CMD)) {
227 printk(KERN_ERR "hermes @ 0x%x: Timeout waiting for command completion.\n",
228 hw->iobase);
229 err = -ETIMEDOUT;
230 goto out;
231 }
232
233 resp->status = hermes_read_regn(hw, STATUS);
234 resp->resp0 = hermes_read_regn(hw, RESP0);
235 resp->resp1 = hermes_read_regn(hw, RESP1);
236 resp->resp2 = hermes_read_regn(hw, RESP2);
237
238 hermes_write_regn(hw, EVACK, HERMES_EV_CMD);
239
240 err = resp->status & HERMES_STATUS_RESULT;
241
242 out:
243 return err;
244 }
245
246 int hermes_allocate(hermes_t *hw, uint16_t size, uint16_t *fid)
247 {
248 int err = 0;
249 hermes_response_t resp;
250 int k;
251 uint16_t reg;
252
253 if ( (size < HERMES_ALLOC_LEN_MIN) || (size > HERMES_ALLOC_LEN_MAX) )
254 return -EINVAL;
255
256 err = hermes_docmd_wait(hw, HERMES_CMD_ALLOC, size, &resp);
257 if (err) {
258 printk(KERN_WARNING "hermes @ 0x%x: Frame allocation command failed (0x%X).\n",
259 hw->iobase, err);
260 return err;
261 }
262
263 reg = hermes_read_regn(hw, EVSTAT);
264 k = ALLOC_COMPL_TIMEOUT;
265 while ( (! (reg & HERMES_EV_ALLOC)) && k) {
266 k--;
267 udelay(10);
268 reg = hermes_read_regn(hw, EVSTAT);
269 }
270
271 if (! hermes_present(hw)) {
272 printk(KERN_WARNING "hermes @ 0x%x: Card removed waiting for frame allocation.\n",
273 hw->iobase);
274 return -ENODEV;
275 }
276
277 if (! (reg & HERMES_EV_ALLOC)) {
278 printk(KERN_ERR "hermes @ 0x%x: Timeout waiting for frame allocation\n",
279 hw->iobase);
280 return -ETIMEDOUT;
281 }
282
283 *fid = hermes_read_regn(hw, ALLOCFID);
284 hermes_write_regn(hw, EVACK, HERMES_EV_ALLOC);
285
286 return 0;
287 }
288
289 /* Set up a BAP to read a particular chunk of data from card's internal buffer.
290 *
291 * Returns: < 0 on internal failure (errno), 0 on success, >0 on error
292 * from firmware
293 *
294 * Callable from any context */
295 static int hermes_bap_seek(hermes_t *hw, int bap, uint16_t id, uint16_t offset)
296 {
297 int sreg = bap ? HERMES_SELECT1 : HERMES_SELECT0;
298 int oreg = bap ? HERMES_OFFSET1 : HERMES_OFFSET0;
299 int k;
300 uint16_t reg;
301
302 /* Paranoia.. */
303 if ( (offset > HERMES_BAP_OFFSET_MAX) || (offset % 2) )
304 return -EINVAL;
305
306 k = BAP_BUSY_TIMEOUT;
307 reg = hermes_read_reg(hw, oreg);
308
309 if (reg & HERMES_OFFSET_BUSY)
310 return -EBUSY;
311
312 /* Now we actually set up the transfer */
313 hermes_write_reg(hw, sreg, id);
314 hermes_write_reg(hw, oreg, offset);
315
316 /* Wait for the BAP to be ready */
317 k = BAP_BUSY_TIMEOUT;
318 reg = hermes_read_reg(hw, oreg);
319 while ( (reg & (HERMES_OFFSET_BUSY | HERMES_OFFSET_ERR)) && k) {
320 k--;
321 udelay(1);
322 reg = hermes_read_reg(hw, oreg);
323 }
324
325 if (reg & HERMES_OFFSET_BUSY) {
326 DEBUG(1,"hermes_bap_seek: timeout\n");
327 return -ETIMEDOUT;
328 }
329
330 if (reg & HERMES_OFFSET_ERR) {
331 DEBUG(1,"hermes_bap_seek: BAP error\n");
332 return -EIO;
333 }
334
335
336 return 0;
337 }
338
339 /* Read a block of data from the chip's buffer, via the
340 * BAP. Synchronization/serialization is the caller's problem. len
341 * must be even.
342 *
343 * Returns: < 0 on internal failure (errno), 0 on success, > 0 on error from firmware
344 */
345 int hermes_bap_pread(hermes_t *hw, int bap, void *buf, uint16_t len,
346 uint16_t id, uint16_t offset)
347 {
348 int dreg = bap ? HERMES_DATA1 : HERMES_DATA0;
349 int err = 0;
350
351 if (len % 2)
352 return -EINVAL;
353
354 err = hermes_bap_seek(hw, bap, id, offset);
355 if (err)
356 goto out;
357
358 /* Actually do the transfer */
359 hermes_read_data(hw, dreg, buf, len/2);
360
361 out:
362 return err;
363 }
364
365 /* Write a block of data to the chip's buffer, via the
366 * BAP. Synchronization/serialization is the caller's problem. len
367 * must be even.
368 *
369 * Returns: < 0 on internal failure (errno), 0 on success, > 0 on error from firmware
370 */
371 int hermes_bap_pwrite(hermes_t *hw, int bap, const void *buf, uint16_t len,
372 uint16_t id, uint16_t offset)
373 {
374 int dreg = bap ? HERMES_DATA1 : HERMES_DATA0;
375 int err = 0;
376
377 if (len % 2)
378 return -EINVAL;
379
380 err = hermes_bap_seek(hw, bap, id, offset);
381 if (err)
382 goto out;
383
384 /* Actually do the transfer */
385 hermes_write_data(hw, dreg, buf, len/2);
386
387 out:
388 return err;
389 }
390
391 /* Read a Length-Type-Value record from the card.
392 *
393 * If length is NULL, we ignore the length read from the card, and
394 * read the entire buffer regardless. This is useful because some of
395 * the configuration records appear to have incorrect lengths in
396 * practice.
397 *
398 * Callable from user or bh context. */
399 int hermes_read_ltv(hermes_t *hw, int bap, uint16_t rid, int buflen,
400 uint16_t *length, void *buf)
401 {
402 int err = 0;
403 int dreg = bap ? HERMES_DATA1 : HERMES_DATA0;
404 uint16_t rlength, rtype;
405 hermes_response_t resp;
406 int count;
407
408 if (buflen % 2)
409 return -EINVAL;
410
411 err = hermes_docmd_wait(hw, HERMES_CMD_ACCESS, rid, &resp);
412 if (err)
413 goto out;
414
415 err = hermes_bap_seek(hw, bap, rid, 0);
416 if (err)
417 goto out;
418
419 rlength = hermes_read_reg(hw, dreg);
420 rtype = hermes_read_reg(hw, dreg);
421
422 if (length)
423 *length = rlength;
424
425 if (rtype != rid)
426 printk(KERN_WARNING "hermes_read_ltv(): rid (0x%04x) does "
427 "not match type (0x%04x)\n", rid, rtype);
428 if (HERMES_RECLEN_TO_BYTES(rlength) > buflen)
429 printk(KERN_WARNING "hermes @ 0x%x: Truncating LTV record from %d to %d bytes. "
430 "(rid=0x%04x, len=0x%04x)\n", hw->iobase,
431 HERMES_RECLEN_TO_BYTES(rlength), buflen, rid, rlength);
432
433 /* For now we always read the whole buffer, the
434 lengths in the records seem to be wrong, frequently */
435 count = buflen / 2;
436
437 #if 0
438 if (length)
439 count = (MIN(buflen, rlength) + 1) / 2;
440 else {
441 count = buflen / 2;
442 if (rlength != buflen)
443 printk(KERN_WARNING "hermes_read_ltv(): Incorrect \
444 record length %d instead of %d on RID 0x%04x\n", rlength, buflen, rid);
445 }
446 #endif
447 hermes_read_data(hw, dreg, buf, count);
448
449 out:
450 return err;
451 }
452
453 int hermes_write_ltv(hermes_t *hw, int bap, uint16_t rid,
454 uint16_t length, const void *value)
455 {
456 int dreg = bap ? HERMES_DATA1 : HERMES_DATA0;
457 int err = 0;
458 hermes_response_t resp;
459 int count;
460
461 DEBUG(3, "write_ltv(): bap=%d rid=0x%04x length=%d (value=0x%04x)\n",
462 bap, rid, length, * ((uint16_t *)value));
463
464 err = hermes_bap_seek(hw, bap, rid, 0);
465 if (err)
466 goto out;
467
468 hermes_write_reg(hw, dreg, length);
469 hermes_write_reg(hw, dreg, rid);
470
471 count = length - 1;
472
473 hermes_write_data(hw, dreg, value, count);
474
475 err = hermes_docmd_wait(hw, HERMES_CMD_ACCESS | HERMES_CMD_WRITE,
476 rid, &resp);
477
478 out:
479 return err;
480 }
481
482 EXPORT_SYMBOL(hermes_struct_init);
483 EXPORT_SYMBOL(hermes_reset);
484 EXPORT_SYMBOL(hermes_docmd_wait);
485 EXPORT_SYMBOL(hermes_allocate);
486
487 EXPORT_SYMBOL(hermes_bap_pread);
488 EXPORT_SYMBOL(hermes_bap_pwrite);
489 EXPORT_SYMBOL(hermes_read_ltv);
490 EXPORT_SYMBOL(hermes_write_ltv);
491
492 static int __init init_hermes(void)
493 {
494 printk(KERN_DEBUG "%s\n", version);
495
496 return 0;
497 }
498
499 module_init(init_hermes);
500