[PATCH] orinoco: remove debug buffer code and userspace include support
[pandora-kernel.git] / 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.
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>
42 #include <linux/module.h>
43 #include <linux/kernel.h>
44 #include <linux/init.h>
45 #include <linux/delay.h>
46
47 #include "hermes.h"
48
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");
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 */
91 static 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
119 void 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
126 int hermes_init(hermes_t *hw)
127 {
128         u16 status, reg;
129         int err = 0;
130         int k;
131
132         /* We don't want to be interrupted while resetting the chipset */
133         hw->inten = 0x0;
134         hermes_write_regn(hw, INTEN, 0);
135         hermes_write_regn(hw, EVACK, 0xffff);
136
137         /* Normally it's a "can't happen" for the command register to
138            be busy when we go to issue a command because we are
139            serializing all commands.  However we want to have some
140            chance of resetting the card even if it gets into a stupid
141            state, so we actually wait to see if the command register
142            will unbusy itself here. */
143         k = CMD_BUSY_TIMEOUT;
144         reg = hermes_read_regn(hw, CMD);
145         while (k && (reg & HERMES_CMD_BUSY)) {
146                 if (reg == 0xffff) /* Special case - the card has probably been removed,
147                                       so don't wait for the timeout */
148                         return -ENODEV;
149
150                 k--;
151                 udelay(1);
152                 reg = hermes_read_regn(hw, CMD);
153         }
154         
155         /* No need to explicitly handle the timeout - if we've timed
156            out hermes_issue_cmd() will probably return -EBUSY below */
157
158         /* According to the documentation, EVSTAT may contain
159            obsolete event occurrence information.  We have to acknowledge
160            it by writing EVACK. */
161         reg = hermes_read_regn(hw, EVSTAT);
162         hermes_write_regn(hw, EVACK, reg);
163
164         /* We don't use hermes_docmd_wait here, because the reset wipes
165            the magic constant in SWSUPPORT0 away, and it gets confused */
166         err = hermes_issue_cmd(hw, HERMES_CMD_INIT, 0);
167         if (err)
168                 return err;
169
170         reg = hermes_read_regn(hw, EVSTAT);
171         k = CMD_INIT_TIMEOUT;
172         while ( (! (reg & HERMES_EV_CMD)) && k) {
173                 k--;
174                 udelay(10);
175                 reg = hermes_read_regn(hw, EVSTAT);
176         }
177
178         hermes_write_regn(hw, SWSUPPORT0, HERMES_MAGIC);
179
180         if (! hermes_present(hw)) {
181                 DEBUG(0, "hermes @ 0x%x: Card removed during reset.\n",
182                        hw->iobase);
183                 err = -ENODEV;
184                 goto out;
185         }
186                 
187         if (! (reg & HERMES_EV_CMD)) {
188                 printk(KERN_ERR "hermes @ %p: " 
189                        "Timeout waiting for card to reset (reg=0x%04x)!\n",
190                        hw->iobase, reg);
191                 err = -ETIMEDOUT;
192                 goto out;
193         }
194
195         status = hermes_read_regn(hw, STATUS);
196
197         hermes_write_regn(hw, EVACK, HERMES_EV_CMD);
198
199         if (status & HERMES_STATUS_RESULT)
200                 err = -EIO;
201
202  out:
203         return err;
204 }
205
206 /* Issue a command to the chip, and (busy!) wait for it to
207  * complete.
208  *
209  * Returns: < 0 on internal error, 0 on success, > 0 on error returned by the firmware
210  *
211  * Callable from any context, but locking is your problem. */
212 int hermes_docmd_wait(hermes_t *hw, u16 cmd, u16 parm0,
213                       struct hermes_response *resp)
214 {
215         int err;
216         int k;
217         u16 reg;
218         u16 status;
219
220         err = hermes_issue_cmd(hw, cmd, parm0);
221         if (err) {
222                 if (! hermes_present(hw)) {
223                         if (net_ratelimit())
224                                 printk(KERN_WARNING "hermes @ %p: "
225                                        "Card removed while issuing command "
226                                        "0x%04x.\n", hw->iobase, cmd);
227                         err = -ENODEV;
228                 } else 
229                         if (net_ratelimit())
230                                 printk(KERN_ERR "hermes @ %p: "
231                                        "Error %d issuing command 0x%04x.\n",
232                                        hw->iobase, err, cmd);
233                 goto out;
234         }
235
236         reg = hermes_read_regn(hw, EVSTAT);
237         k = CMD_COMPL_TIMEOUT;
238         while ( (! (reg & HERMES_EV_CMD)) && k) {
239                 k--;
240                 udelay(10);
241                 reg = hermes_read_regn(hw, EVSTAT);
242         }
243
244         if (! hermes_present(hw)) {
245                 printk(KERN_WARNING "hermes @ %p: Card removed "
246                        "while waiting for command 0x%04x completion.\n",
247                        hw->iobase, cmd);
248                 err = -ENODEV;
249                 goto out;
250         }
251                 
252         if (! (reg & HERMES_EV_CMD)) {
253                 printk(KERN_ERR "hermes @ %p: Timeout waiting for "
254                        "command 0x%04x completion.\n", hw->iobase, cmd);
255                 err = -ETIMEDOUT;
256                 goto out;
257         }
258
259         status = hermes_read_regn(hw, STATUS);
260         if (resp) {
261                 resp->status = status;
262                 resp->resp0 = hermes_read_regn(hw, RESP0);
263                 resp->resp1 = hermes_read_regn(hw, RESP1);
264                 resp->resp2 = hermes_read_regn(hw, RESP2);
265         }
266
267         hermes_write_regn(hw, EVACK, HERMES_EV_CMD);
268
269         if (status & HERMES_STATUS_RESULT)
270                 err = -EIO;
271
272  out:
273         return err;
274 }
275
276 int hermes_allocate(hermes_t *hw, u16 size, u16 *fid)
277 {
278         int err = 0;
279         int k;
280         u16 reg;
281         
282         if ( (size < HERMES_ALLOC_LEN_MIN) || (size > HERMES_ALLOC_LEN_MAX) )
283                 return -EINVAL;
284
285         err = hermes_docmd_wait(hw, HERMES_CMD_ALLOC, size, NULL);
286         if (err) {
287                 return err;
288         }
289
290         reg = hermes_read_regn(hw, EVSTAT);
291         k = ALLOC_COMPL_TIMEOUT;
292         while ( (! (reg & HERMES_EV_ALLOC)) && k) {
293                 k--;
294                 udelay(10);
295                 reg = hermes_read_regn(hw, EVSTAT);
296         }
297         
298         if (! hermes_present(hw)) {
299                 printk(KERN_WARNING "hermes @ %p: "
300                        "Card removed waiting for frame allocation.\n",
301                        hw->iobase);
302                 return -ENODEV;
303         }
304                 
305         if (! (reg & HERMES_EV_ALLOC)) {
306                 printk(KERN_ERR "hermes @ %p: "
307                        "Timeout waiting for frame allocation\n",
308                        hw->iobase);
309                 return -ETIMEDOUT;
310         }
311
312         *fid = hermes_read_regn(hw, ALLOCFID);
313         hermes_write_regn(hw, EVACK, HERMES_EV_ALLOC);
314         
315         return 0;
316 }
317
318
319 /* Set up a BAP to read a particular chunk of data from card's internal buffer.
320  *
321  * Returns: < 0 on internal failure (errno), 0 on success, >0 on error
322  * from firmware
323  *
324  * Callable from any context */
325 static int hermes_bap_seek(hermes_t *hw, int bap, u16 id, u16 offset)
326 {
327         int sreg = bap ? HERMES_SELECT1 : HERMES_SELECT0;
328         int oreg = bap ? HERMES_OFFSET1 : HERMES_OFFSET0;
329         int k;
330         u16 reg;
331
332         /* Paranoia.. */
333         if ( (offset > HERMES_BAP_OFFSET_MAX) || (offset % 2) )
334                 return -EINVAL;
335
336         k = HERMES_BAP_BUSY_TIMEOUT;
337         reg = hermes_read_reg(hw, oreg);
338         while ((reg & HERMES_OFFSET_BUSY) && k) {
339                 k--;
340                 udelay(1);
341                 reg = hermes_read_reg(hw, oreg);
342         }
343
344         if (reg & HERMES_OFFSET_BUSY)
345                 return -ETIMEDOUT;
346
347         /* Now we actually set up the transfer */
348         hermes_write_reg(hw, sreg, id);
349         hermes_write_reg(hw, oreg, offset);
350
351         /* Wait for the BAP to be ready */
352         k = HERMES_BAP_BUSY_TIMEOUT;
353         reg = hermes_read_reg(hw, oreg);
354         while ( (reg & (HERMES_OFFSET_BUSY | HERMES_OFFSET_ERR)) && k) {
355                 k--;
356                 udelay(1);
357                 reg = hermes_read_reg(hw, oreg);
358         }
359
360         if (reg != offset) {
361                 printk(KERN_ERR "hermes @ %p: BAP%d offset %s: "
362                        "reg=0x%x id=0x%x offset=0x%x\n", hw->iobase, bap,
363                        (reg & HERMES_OFFSET_BUSY) ? "timeout" : "error",
364                        reg, id, offset);
365
366                 if (reg & HERMES_OFFSET_BUSY) {
367                         return -ETIMEDOUT;
368                 }
369
370                 return -EIO;            /* error or wrong offset */
371         }
372
373         return 0;
374 }
375
376 /* Read a block of data from the chip's buffer, via the
377  * BAP. Synchronization/serialization is the caller's problem.  len
378  * must be even.
379  *
380  * Returns: < 0 on internal failure (errno), 0 on success, > 0 on error from firmware
381  */
382 int hermes_bap_pread(hermes_t *hw, int bap, void *buf, int len,
383                      u16 id, u16 offset)
384 {
385         int dreg = bap ? HERMES_DATA1 : HERMES_DATA0;
386         int err = 0;
387
388         if ( (len < 0) || (len % 2) )
389                 return -EINVAL;
390
391         err = hermes_bap_seek(hw, bap, id, offset);
392         if (err)
393                 goto out;
394
395         /* Actually do the transfer */
396         hermes_read_words(hw, dreg, buf, len/2);
397
398  out:
399         return err;
400 }
401
402 /* Write a block of data to the chip's buffer, via the
403  * BAP. Synchronization/serialization is the caller's problem. len
404  * must be even.
405  *
406  * Returns: < 0 on internal failure (errno), 0 on success, > 0 on error from firmware
407  */
408 int hermes_bap_pwrite(hermes_t *hw, int bap, const void *buf, int len,
409                       u16 id, u16 offset)
410 {
411         int dreg = bap ? HERMES_DATA1 : HERMES_DATA0;
412         int err = 0;
413
414         if ( (len < 0) || (len % 2) )
415                 return -EINVAL;
416
417         err = hermes_bap_seek(hw, bap, id, offset);
418         if (err)
419                 goto out;
420         
421         /* Actually do the transfer */
422         hermes_write_words(hw, dreg, buf, len/2);
423
424  out:   
425         return err;
426 }
427
428 /* Write a block of data to the chip's buffer with padding if
429  * neccessary, via the BAP. Synchronization/serialization is the
430  * caller's problem. len must be even.
431  *
432  * Returns: < 0 on internal failure (errno), 0 on success, > 0 on error from firmware
433  */
434 int hermes_bap_pwrite_pad(hermes_t *hw, int bap, const void *buf, unsigned data_len, int len,
435                       u16 id, u16 offset)
436 {
437         int dreg = bap ? HERMES_DATA1 : HERMES_DATA0;
438         int err = 0;
439
440         if (len < 0 || len % 2 || data_len > len)
441                 return -EINVAL;
442
443         err = hermes_bap_seek(hw, bap, id, offset);
444         if (err)
445                 goto out;
446
447         /* Transfer all the complete words of data */
448         hermes_write_words(hw, dreg, buf, data_len/2);
449         /* If there is an odd byte left over pad and transfer it */
450         if (data_len & 1) {
451                 u8 end[2];
452                 end[1] = 0;
453                 end[0] = ((unsigned char *)buf)[data_len - 1];
454                 hermes_write_words(hw, dreg, end, 1);
455                 data_len ++;
456         }
457         /* Now send zeros for the padding */
458         if (data_len < len)
459                 hermes_clear_words(hw, dreg, (len - data_len) / 2);
460         /* Complete */
461  out:
462         return err;
463 }
464
465 /* Read a Length-Type-Value record from the card.
466  *
467  * If length is NULL, we ignore the length read from the card, and
468  * read the entire buffer regardless. This is useful because some of
469  * the configuration records appear to have incorrect lengths in
470  * practice.
471  *
472  * Callable from user or bh context.  */
473 int hermes_read_ltv(hermes_t *hw, int bap, u16 rid, unsigned bufsize,
474                     u16 *length, void *buf)
475 {
476         int err = 0;
477         int dreg = bap ? HERMES_DATA1 : HERMES_DATA0;
478         u16 rlength, rtype;
479         unsigned nwords;
480
481         if ( (bufsize < 0) || (bufsize % 2) )
482                 return -EINVAL;
483
484         err = hermes_docmd_wait(hw, HERMES_CMD_ACCESS, rid, NULL);
485         if (err)
486                 return err;
487
488         err = hermes_bap_seek(hw, bap, rid, 0);
489         if (err)
490                 return err;
491
492         rlength = hermes_read_reg(hw, dreg);
493
494         if (! rlength)
495                 return -ENODATA;
496
497         rtype = hermes_read_reg(hw, dreg);
498
499         if (length)
500                 *length = rlength;
501
502         if (rtype != rid)
503                 printk(KERN_WARNING "hermes @ %p: %s(): "
504                        "rid (0x%04x) does not match type (0x%04x)\n",
505                        hw->iobase, __FUNCTION__, rid, rtype);
506         if (HERMES_RECLEN_TO_BYTES(rlength) > bufsize)
507                 printk(KERN_WARNING "hermes @ %p: "
508                        "Truncating LTV record from %d to %d bytes. "
509                        "(rid=0x%04x, len=0x%04x)\n", hw->iobase,
510                        HERMES_RECLEN_TO_BYTES(rlength), bufsize, rid, rlength);
511
512         nwords = min((unsigned)rlength - 1, bufsize / 2);
513         hermes_read_words(hw, dreg, buf, nwords);
514
515         return 0;
516 }
517
518 int hermes_write_ltv(hermes_t *hw, int bap, u16 rid, 
519                      u16 length, const void *value)
520 {
521         int dreg = bap ? HERMES_DATA1 : HERMES_DATA0;
522         int err = 0;
523         unsigned count;
524
525         if (length == 0)
526                 return -EINVAL;
527
528         err = hermes_bap_seek(hw, bap, rid, 0);
529         if (err)
530                 return err;
531
532         hermes_write_reg(hw, dreg, length);
533         hermes_write_reg(hw, dreg, rid);
534
535         count = length - 1;
536
537         hermes_write_words(hw, dreg, value, count);
538
539         err = hermes_docmd_wait(hw, HERMES_CMD_ACCESS | HERMES_CMD_WRITE, 
540                                 rid, NULL);
541
542         return err;
543 }
544
545 EXPORT_SYMBOL(hermes_struct_init);
546 EXPORT_SYMBOL(hermes_init);
547 EXPORT_SYMBOL(hermes_docmd_wait);
548 EXPORT_SYMBOL(hermes_allocate);
549
550 EXPORT_SYMBOL(hermes_bap_pread);
551 EXPORT_SYMBOL(hermes_bap_pwrite);
552 EXPORT_SYMBOL(hermes_bap_pwrite_pad);
553 EXPORT_SYMBOL(hermes_read_ltv);
554 EXPORT_SYMBOL(hermes_write_ltv);
555
556 static int __init init_hermes(void)
557 {
558         return 0;
559 }
560
561 static void __exit exit_hermes(void)
562 {
563 }
564
565 module_init(init_hermes);
566 module_exit(exit_hermes);