vserver 1.9.3
[linux-2.6.git] / drivers / net / wireless / hermes.h
1 /* hermes.h
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 I & 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.
14  *
15  * Copyright (C) 2000, David Gibson, Linuxcare Australia.
16  * (C) Copyright David Gibson, IBM Corp. 2001-2003.
17  *
18  * Portions taken from hfa384x.h, Copyright (C) 1999 AbsoluteValue Systems, Inc. All Rights Reserved.
19  *
20  * This file distributed under the GPL, version 2.
21  */
22
23 #ifndef _HERMES_H
24 #define _HERMES_H
25
26 /* Notes on locking:
27  *
28  * As a module of low level hardware access routines, there is no
29  * locking. Users of this module should ensure that they serialize
30  * access to the hermes_t structure, and to the hardware
31 */
32
33 #include <linux/delay.h>
34 #include <linux/if_ether.h>
35 #include <asm/byteorder.h>
36
37 /*
38  * Limits and constants
39  */
40 #define         HERMES_ALLOC_LEN_MIN            (4)
41 #define         HERMES_ALLOC_LEN_MAX            (2400)
42 #define         HERMES_LTV_LEN_MAX              (34)
43 #define         HERMES_BAP_DATALEN_MAX          (4096)
44 #define         HERMES_BAP_OFFSET_MAX           (4096)
45 #define         HERMES_PORTID_MAX               (7)
46 #define         HERMES_NUMPORTS_MAX             (HERMES_PORTID_MAX+1)
47 #define         HERMES_PDR_LEN_MAX              (260)   /* in bytes, from EK */
48 #define         HERMES_PDA_RECS_MAX             (200)   /* a guess */
49 #define         HERMES_PDA_LEN_MAX              (1024)  /* in bytes, from EK */
50 #define         HERMES_SCANRESULT_MAX           (35)
51 #define         HERMES_CHINFORESULT_MAX         (8)
52 #define         HERMES_MAX_MULTICAST            (16)
53 #define         HERMES_MAGIC                    (0x7d1f)
54
55 /*
56  * Hermes register offsets
57  */
58 #define         HERMES_CMD                      (0x00)
59 #define         HERMES_PARAM0                   (0x02)
60 #define         HERMES_PARAM1                   (0x04)
61 #define         HERMES_PARAM2                   (0x06)
62 #define         HERMES_STATUS                   (0x08)
63 #define         HERMES_RESP0                    (0x0A)
64 #define         HERMES_RESP1                    (0x0C)
65 #define         HERMES_RESP2                    (0x0E)
66 #define         HERMES_INFOFID                  (0x10)
67 #define         HERMES_RXFID                    (0x20)
68 #define         HERMES_ALLOCFID                 (0x22)
69 #define         HERMES_TXCOMPLFID               (0x24)
70 #define         HERMES_SELECT0                  (0x18)
71 #define         HERMES_OFFSET0                  (0x1C)
72 #define         HERMES_DATA0                    (0x36)
73 #define         HERMES_SELECT1                  (0x1A)
74 #define         HERMES_OFFSET1                  (0x1E)
75 #define         HERMES_DATA1                    (0x38)
76 #define         HERMES_EVSTAT                   (0x30)
77 #define         HERMES_INTEN                    (0x32)
78 #define         HERMES_EVACK                    (0x34)
79 #define         HERMES_CONTROL                  (0x14)
80 #define         HERMES_SWSUPPORT0               (0x28)
81 #define         HERMES_SWSUPPORT1               (0x2A)
82 #define         HERMES_SWSUPPORT2               (0x2C)
83 #define         HERMES_AUXPAGE                  (0x3A)
84 #define         HERMES_AUXOFFSET                (0x3C)
85 #define         HERMES_AUXDATA                  (0x3E)
86
87 /*
88  * CMD register bitmasks
89  */
90 #define         HERMES_CMD_BUSY                 (0x8000)
91 #define         HERMES_CMD_AINFO                (0x7f00)
92 #define         HERMES_CMD_MACPORT              (0x0700)
93 #define         HERMES_CMD_RECL                 (0x0100)
94 #define         HERMES_CMD_WRITE                (0x0100)
95 #define         HERMES_CMD_PROGMODE             (0x0300)
96 #define         HERMES_CMD_CMDCODE              (0x003f)
97
98 /*
99  * STATUS register bitmasks
100  */
101 #define         HERMES_STATUS_RESULT            (0x7f00)
102 #define         HERMES_STATUS_CMDCODE           (0x003f)
103
104 /*
105  * OFFSET register bitmasks
106  */
107 #define         HERMES_OFFSET_BUSY              (0x8000)
108 #define         HERMES_OFFSET_ERR               (0x4000)
109 #define         HERMES_OFFSET_DATAOFF           (0x0ffe)
110
111 /*
112  * Event register bitmasks (INTEN, EVSTAT, EVACK)
113  */
114 #define         HERMES_EV_TICK                  (0x8000)
115 #define         HERMES_EV_WTERR                 (0x4000)
116 #define         HERMES_EV_INFDROP               (0x2000)
117 #define         HERMES_EV_INFO                  (0x0080)
118 #define         HERMES_EV_DTIM                  (0x0020)
119 #define         HERMES_EV_CMD                   (0x0010)
120 #define         HERMES_EV_ALLOC                 (0x0008)
121 #define         HERMES_EV_TXEXC                 (0x0004)
122 #define         HERMES_EV_TX                    (0x0002)
123 #define         HERMES_EV_RX                    (0x0001)
124
125 /*
126  * Command codes
127  */
128 /*--- Controller Commands ----------------------------*/
129 #define         HERMES_CMD_INIT                 (0x0000)
130 #define         HERMES_CMD_ENABLE               (0x0001)
131 #define         HERMES_CMD_DISABLE              (0x0002)
132 #define         HERMES_CMD_DIAG                 (0x0003)
133
134 /*--- Buffer Mgmt Commands ---------------------------*/
135 #define         HERMES_CMD_ALLOC                (0x000A)
136 #define         HERMES_CMD_TX                   (0x000B)
137
138 /*--- Regulate Commands ------------------------------*/
139 #define         HERMES_CMD_NOTIFY               (0x0010)
140 #define         HERMES_CMD_INQUIRE              (0x0011)
141
142 /*--- Configure Commands -----------------------------*/
143 #define         HERMES_CMD_ACCESS               (0x0021)
144 #define         HERMES_CMD_DOWNLD               (0x0022)
145
146 /*--- Serial I/O Commands ----------------------------*/
147 #define         HERMES_CMD_READMIF              (0x0030)
148 #define         HERMES_CMD_WRITEMIF             (0x0031)
149
150 /*--- Debugging Commands -----------------------------*/
151 #define         HERMES_CMD_TEST                 (0x0038)
152
153
154 /* Test command arguments */
155 #define         HERMES_TEST_SET_CHANNEL         0x0800
156 #define         HERMES_TEST_MONITOR             0x0b00
157 #define         HERMES_TEST_STOP                0x0f00
158
159 /* Authentication algorithms */
160 #define         HERMES_AUTH_OPEN                1
161 #define         HERMES_AUTH_SHARED_KEY          2
162
163 /* WEP settings */
164 #define         HERMES_WEP_PRIVACY_INVOKED      0x0001
165 #define         HERMES_WEP_EXCL_UNENCRYPTED     0x0002
166 #define         HERMES_WEP_HOST_ENCRYPT         0x0010
167 #define         HERMES_WEP_HOST_DECRYPT         0x0080
168
169 /* Symbol hostscan options */
170 #define         HERMES_HOSTSCAN_SYMBOL_5SEC     0x0001
171 #define         HERMES_HOSTSCAN_SYMBOL_ONCE     0x0002
172 #define         HERMES_HOSTSCAN_SYMBOL_PASSIVE  0x0040
173 #define         HERMES_HOSTSCAN_SYMBOL_BCAST    0x0080
174
175 /*
176  * Frame structures and constants
177  */
178
179 #define HERMES_DESCRIPTOR_OFFSET        0
180 #define HERMES_802_11_OFFSET            (14)
181 #define HERMES_802_3_OFFSET             (14+32)
182 #define HERMES_802_2_OFFSET             (14+32+14)
183
184 #define HERMES_RXSTAT_ERR               (0x0003)
185 #define HERMES_RXSTAT_BADCRC            (0x0001)
186 #define HERMES_RXSTAT_UNDECRYPTABLE     (0x0002)
187 #define HERMES_RXSTAT_MACPORT           (0x0700)
188 #define HERMES_RXSTAT_PCF               (0x1000)        /* Frame was received in CF period */
189 #define HERMES_RXSTAT_MSGTYPE           (0xE000)
190 #define HERMES_RXSTAT_1042              (0x2000)        /* RFC-1042 frame */
191 #define HERMES_RXSTAT_TUNNEL            (0x4000)        /* bridge-tunnel encoded frame */
192 #define HERMES_RXSTAT_WMP               (0x6000)        /* Wavelan-II Management Protocol frame */
193
194 struct hermes_tx_descriptor {
195         u16 status;
196         u16 reserved1;
197         u16 reserved2;
198         u32 sw_support;
199         u8 retry_count;
200         u8 tx_rate;
201         u16 tx_control; 
202 } __attribute__ ((packed));
203
204 #define HERMES_TXSTAT_RETRYERR          (0x0001)
205 #define HERMES_TXSTAT_AGEDERR           (0x0002)
206 #define HERMES_TXSTAT_DISCON            (0x0004)
207 #define HERMES_TXSTAT_FORMERR           (0x0008)
208
209 #define HERMES_TXCTRL_TX_OK             (0x0002)        /* ?? interrupt on Tx complete */
210 #define HERMES_TXCTRL_TX_EX             (0x0004)        /* ?? interrupt on Tx exception */
211 #define HERMES_TXCTRL_802_11            (0x0008)        /* We supply 802.11 header */
212 #define HERMES_TXCTRL_ALT_RTRY          (0x0020)
213
214 /* Inquiry constants and data types */
215
216 #define HERMES_INQ_TALLIES              (0xF100)
217 #define HERMES_INQ_SCAN                 (0xF101)
218 #define HERMES_INQ_CHANNELINFO          (0xF102)
219 #define HERMES_INQ_HOSTSCAN             (0xF103)
220 #define HERMES_INQ_HOSTSCAN_SYMBOL      (0xF104)
221 #define HERMES_INQ_LINKSTATUS           (0xF200)
222 #define HERMES_INQ_SEC_STAT_AGERE       (0xF202)
223
224 struct hermes_tallies_frame {
225         u16 TxUnicastFrames;
226         u16 TxMulticastFrames;
227         u16 TxFragments;
228         u16 TxUnicastOctets;
229         u16 TxMulticastOctets;
230         u16 TxDeferredTransmissions;
231         u16 TxSingleRetryFrames;
232         u16 TxMultipleRetryFrames;
233         u16 TxRetryLimitExceeded;
234         u16 TxDiscards;
235         u16 RxUnicastFrames;
236         u16 RxMulticastFrames;
237         u16 RxFragments;
238         u16 RxUnicastOctets;
239         u16 RxMulticastOctets;
240         u16 RxFCSErrors;
241         u16 RxDiscards_NoBuffer;
242         u16 TxDiscardsWrongSA;
243         u16 RxWEPUndecryptable;
244         u16 RxMsgInMsgFragments;
245         u16 RxMsgInBadMsgFragments;
246         /* Those last are probably not available in very old firmwares */
247         u16 RxDiscards_WEPICVError;
248         u16 RxDiscards_WEPExcluded;
249 } __attribute__ ((packed));
250
251 /* Grabbed from wlan-ng - Thanks Mark... - Jean II
252  * This is the result of a scan inquiry command */
253 /* Structure describing info about an Access Point */
254 struct prism2_scan_apinfo {
255         u16 channel;            /* Channel where the AP sits */
256         u16 noise;              /* Noise level */
257         u16 level;              /* Signal level */
258         u8 bssid[ETH_ALEN];     /* MAC address of the Access Point */
259         u16 beacon_interv;      /* Beacon interval */
260         u16 capabilities;       /* Capabilities */
261         u16 essid_len;          /* ESSID length */
262         u8 essid[32];           /* ESSID of the network */
263         u8 rates[10];           /* Bit rate supported */
264         u16 proberesp_rate;     /* Data rate of the response frame */
265         u16 atim;               /* ATIM window time, Kus (hostscan only) */
266 } __attribute__ ((packed));
267
268 /* Same stuff for the Lucent/Agere card.
269  * Thanks to h1kari <h1kari AT dachb0den.com> - Jean II */
270 struct agere_scan_apinfo {
271         u16 channel;            /* Channel where the AP sits */
272         u16 noise;              /* Noise level */
273         u16 level;              /* Signal level */
274         u8 bssid[ETH_ALEN];     /* MAC address of the Access Point */
275         u16 beacon_interv;      /* Beacon interval */
276         u16 capabilities;       /* Capabilities */
277         /* bits: 0-ess, 1-ibss, 4-privacy [wep] */
278         u16 essid_len;          /* ESSID length */
279         u8 essid[32];           /* ESSID of the network */
280 } __attribute__ ((packed));
281
282 /* Moustafa: Scan structure for Symbol cards */
283 struct symbol_scan_apinfo {
284         u8 channel;             /* Channel where the AP sits */
285         u8 unknown1;            /* 8 in 2.9x and 3.9x f/w, 0 otherwise */
286         u16 noise;              /* Noise level */
287         u16 level;              /* Signal level */
288         u8 bssid[ETH_ALEN];     /* MAC address of the Access Point */
289         u16 beacon_interv;      /* Beacon interval */
290         u16 capabilities;       /* Capabilities */
291         /* bits: 0-ess, 1-ibss, 4-privacy [wep] */
292         u16 essid_len;          /* ESSID length */
293         u8 essid[32];           /* ESSID of the network */
294         u16 rates[5];           /* Bit rate supported */
295         u16 basic_rates;        /* Basic rates bitmask */
296         u8 unknown2[6];         /* Always FF:FF:FF:FF:00:00 */
297         u8 unknown3[8];         /* Always 0, appeared in f/w 3.91-68 */
298 } __attribute__ ((packed));
299
300 union hermes_scan_info {
301         struct agere_scan_apinfo        a;
302         struct prism2_scan_apinfo       p;
303         struct symbol_scan_apinfo       s;
304 };
305
306 #define HERMES_LINKSTATUS_NOT_CONNECTED   (0x0000)  
307 #define HERMES_LINKSTATUS_CONNECTED       (0x0001)
308 #define HERMES_LINKSTATUS_DISCONNECTED    (0x0002)
309 #define HERMES_LINKSTATUS_AP_CHANGE       (0x0003)
310 #define HERMES_LINKSTATUS_AP_OUT_OF_RANGE (0x0004)
311 #define HERMES_LINKSTATUS_AP_IN_RANGE     (0x0005)
312 #define HERMES_LINKSTATUS_ASSOC_FAILED    (0x0006)
313   
314 struct hermes_linkstatus {
315         u16 linkstatus;         /* Link status */
316 } __attribute__ ((packed));
317
318 struct hermes_response {
319         u16 status, resp0, resp1, resp2;
320 };
321
322 /* "ID" structure - used for ESSID and station nickname */
323 struct hermes_idstring {
324         u16 len;
325         u16 val[16];
326 } __attribute__ ((packed));
327
328 struct hermes_multicast {
329         u8 addr[HERMES_MAX_MULTICAST][ETH_ALEN];
330 } __attribute__ ((packed));
331
332 // #define HERMES_DEBUG_BUFFER 1
333 #define HERMES_DEBUG_BUFSIZE 4096
334 struct hermes_debug_entry {
335         int bap;
336         u16 id, offset;
337         int cycles;
338 };
339
340 #ifdef __KERNEL__
341
342 /* Timeouts */
343 #define HERMES_BAP_BUSY_TIMEOUT (500) /* In iterations of ~1us */
344
345 /* Basic control structure */
346 typedef struct hermes {
347         unsigned long iobase;
348         int io_space; /* 1 if we IO-mapped IO, 0 for memory-mapped IO? */
349 #define HERMES_IO       1
350 #define HERMES_MEM      0
351         int reg_spacing;
352 #define HERMES_16BIT_REGSPACING 0
353 #define HERMES_32BIT_REGSPACING 1
354
355         u16 inten; /* Which interrupts should be enabled? */
356
357 #ifdef HERMES_DEBUG_BUFFER
358         struct hermes_debug_entry dbuf[HERMES_DEBUG_BUFSIZE];
359         unsigned long dbufp;
360         unsigned long profile[HERMES_BAP_BUSY_TIMEOUT+1];
361 #endif
362 } hermes_t;
363
364 /* Register access convenience macros */
365 #define hermes_read_reg(hw, off) ((hw)->io_space ? \
366         inw((hw)->iobase + ( (off) << (hw)->reg_spacing )) : \
367         readw((hw)->iobase + ( (off) << (hw)->reg_spacing )))
368 #define hermes_write_reg(hw, off, val) do { \
369         if ((hw)->io_space) \
370                 outw_p((val), (hw)->iobase + ((off) << (hw)->reg_spacing)); \
371         else \
372                 writew((val), (hw)->iobase + ((off) << (hw)->reg_spacing)); \
373         } while (0)
374 #define hermes_read_regn(hw, name) hermes_read_reg((hw), HERMES_##name)
375 #define hermes_write_regn(hw, name, val) hermes_write_reg((hw), HERMES_##name, (val))
376
377 /* Function prototypes */
378 void hermes_struct_init(hermes_t *hw, ulong address, int io_space,
379                         int reg_spacing);
380 int hermes_init(hermes_t *hw);
381 int hermes_docmd_wait(hermes_t *hw, u16 cmd, u16 parm0,
382                       struct hermes_response *resp);
383 int hermes_allocate(hermes_t *hw, u16 size, u16 *fid);
384
385 int hermes_bap_pread(hermes_t *hw, int bap, void *buf, unsigned len,
386                        u16 id, u16 offset);
387 int hermes_bap_pwrite(hermes_t *hw, int bap, const void *buf, unsigned len,
388                         u16 id, u16 offset);
389 int hermes_read_ltv(hermes_t *hw, int bap, u16 rid, unsigned buflen,
390                     u16 *length, void *buf);
391 int hermes_write_ltv(hermes_t *hw, int bap, u16 rid,
392                       u16 length, const void *value);
393
394 /* Inline functions */
395
396 static inline int hermes_present(hermes_t *hw)
397 {
398         return hermes_read_regn(hw, SWSUPPORT0) == HERMES_MAGIC;
399 }
400
401 static inline void hermes_set_irqmask(hermes_t *hw, u16 events)
402 {
403         hw->inten = events;
404         hermes_write_regn(hw, INTEN, events);
405 }
406
407 static inline int hermes_enable_port(hermes_t *hw, int port)
408 {
409         return hermes_docmd_wait(hw, HERMES_CMD_ENABLE | (port << 8),
410                                  0, NULL);
411 }
412
413 static inline int hermes_disable_port(hermes_t *hw, int port)
414 {
415         return hermes_docmd_wait(hw, HERMES_CMD_DISABLE | (port << 8), 
416                                  0, NULL);
417 }
418
419 /* Initiate an INQUIRE command (tallies or scan).  The result will come as an
420  * information frame in __orinoco_ev_info() */
421 static inline int hermes_inquire(hermes_t *hw, u16 rid)
422 {
423         return hermes_docmd_wait(hw, HERMES_CMD_INQUIRE, rid, NULL);
424 }
425
426 #define HERMES_BYTES_TO_RECLEN(n) ( (((n)+1)/2) + 1 )
427 #define HERMES_RECLEN_TO_BYTES(n) ( ((n)-1) * 2 )
428
429 /* Note that for the next two, the count is in 16-bit words, not bytes */
430 static inline void hermes_read_words(struct hermes *hw, int off, void *buf, unsigned count)
431 {
432         off = off << hw->reg_spacing;
433
434         if (hw->io_space) {
435                 insw(hw->iobase + off, buf, count);
436         } else {
437                 unsigned i;
438                 u16 *p;
439
440                 /* This needs to *not* byteswap (like insw()) but
441                  * readw() does byteswap hence the conversion.  I hope
442                  * gcc is smart enough to fold away the two swaps on
443                  * big-endian platforms. */
444                 for (i = 0, p = buf; i < count; i++) {
445                         *p++ = cpu_to_le16(readw(hw->iobase + off));
446                 }
447         }
448 }
449
450 static inline void hermes_write_words(struct hermes *hw, int off, const void *buf, unsigned count)
451 {
452         off = off << hw->reg_spacing;
453
454         if (hw->io_space) {
455                 outsw(hw->iobase + off, buf, count);
456         } else {
457                 unsigned i;
458                 const u16 *p;
459
460                 /* This needs to *not* byteswap (like outsw()) but
461                  * writew() does byteswap hence the conversion.  I
462                  * hope gcc is smart enough to fold away the two swaps
463                  * on big-endian platforms. */
464                 for (i = 0, p = buf; i < count; i++) {
465                         writew(le16_to_cpu(*p++), hw->iobase + off);
466                 }
467         }
468 }
469
470 static inline void hermes_clear_words(struct hermes *hw, int off, unsigned count)
471 {
472         unsigned i;
473
474         off = off << hw->reg_spacing;
475
476         if (hw->io_space) {
477                 for (i = 0; i < count; i++)
478                         outw(0, hw->iobase + off);
479         } else {
480                 for (i = 0; i < count; i++)
481                         writew(0, hw->iobase + off);
482         }
483 }
484
485 #define HERMES_READ_RECORD(hw, bap, rid, buf) \
486         (hermes_read_ltv((hw),(bap),(rid), sizeof(*buf), NULL, (buf)))
487 #define HERMES_WRITE_RECORD(hw, bap, rid, buf) \
488         (hermes_write_ltv((hw),(bap),(rid),HERMES_BYTES_TO_RECLEN(sizeof(*buf)),(buf)))
489
490 static inline int hermes_read_wordrec(hermes_t *hw, int bap, u16 rid, u16 *word)
491 {
492         u16 rec;
493         int err;
494
495         err = HERMES_READ_RECORD(hw, bap, rid, &rec);
496         *word = le16_to_cpu(rec);
497         return err;
498 }
499
500 static inline int hermes_write_wordrec(hermes_t *hw, int bap, u16 rid, u16 word)
501 {
502         u16 rec = cpu_to_le16(word);
503         return HERMES_WRITE_RECORD(hw, bap, rid, &rec);
504 }
505
506 #else /* ! __KERNEL__ */
507
508 /* These are provided for the benefit of userspace drivers and testing programs
509    which use ioperm() or iopl() */
510
511 #define hermes_read_reg(base, off) (inw((base) + (off)))
512 #define hermes_write_reg(base, off, val) (outw((val), (base) + (off)))
513
514 #define hermes_read_regn(base, name) (hermes_read_reg((base), HERMES_##name))
515 #define hermes_write_regn(base, name, val) (hermes_write_reg((base), HERMES_##name, (val)))
516
517 /* Note that for the next two, the count is in 16-bit words, not bytes */
518 #define hermes_read_data(base, off, buf, count) (insw((base) + (off), (buf), (count)))
519 #define hermes_write_data(base, off, buf, count) (outsw((base) + (off), (buf), (count)))
520
521 #endif /* ! __KERNEL__ */
522
523 #endif  /* _HERMES_H */