Xenomai  3.0.8
rt2x00.h
1 /* rt2x00.h
2  *
3  * Copyright (C) 2004 - 2005 rt2x00-2.0.0-b3 SourceForge Project
4  * <http://rt2x00.serialmonkey.com>
5  * 2006 rtnet adaption by Daniel Gregorek
6  * <dxg@gmx.de>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the
20  * Free Software Foundation, Inc.,
21  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22  */
23 
24 /*
25  Module: rt2x00
26  Abstract: rt2x00 global information.
27  Supported chipsets: RT2560
28 */
29 
30 #ifndef RT2X00_H
31 #define RT2X00_H
32 
33 #include <linux/netdevice.h>
34 #include <linux/wireless.h>
35 
36 #include <rtnet_port.h>
37 #include <rtwlan.h>
38 
39 #define MAX_UNITS 2
40 
41 /*
42  * Module information.
43  */
44 #define DRV_NAME "rt2x00"
45 #define DRV_VERSION "0.1"
46 #define DRV_AUTHOR "Daniel Gregorek <dxg@gmx.de>"
47 //#define CONFIG_RT2X00_DEBUG
48 
49 /*
50  * Debug defines.
51  * The debug variable will be exported by the device specific module.
52  * For this reason this variable must be set to extern to make it accessible
53  * to the core module as well.
54  */
55 #ifdef CONFIG_RT2X00_DEBUG
56 extern int rt2x00_debug_level;
57 #define DEBUG_PRINTK(__message...) do{ rtdm_printk(__message); }while(0)
58 #else /* CONFIG_RT2X00_DEBUG */
59 #define DEBUG_PRINTK(__message...) do{ }while(0)
60 #endif /* CONFIG_RT2X00_DEBUG */
61 
62 /*
63  * Various debug levels.
64  * PANIC and ERROR indicates serious problems within the module,
65  * these should never be ignored and thus we will always print the message.
66  */
67 #define PANIC(__message,__args...) rtdm_printk(KERN_PANIC DRV_NAME "->%s: Panic - " __message,__FUNCTION__,##__args);
68 #define ERROR(__message,__args...) rtdm_printk(KERN_ERR DRV_NAME "->%s: Error - " __message,__FUNCTION__,##__args);
69 #define WARNING(__message,__args...) rtdm_printk(KERN_WARNING DRV_NAME "->%s: Warning - " __message,__FUNCTION__,##__args);
70 #define NOTICE(__message,__args...) rtdm_printk(KERN_NOTICE DRV_NAME "->%s: Notice - " __message,__FUNCTION__,##__args);
71 #define INFO(__message,__args...) rtdm_printk(KERN_INFO DRV_NAME "->%s: Info - " __message,__FUNCTION__,##__args);
72 #define DEBUG(__message,__args...) DEBUG_PRINTK(KERN_DEBUG DRV_NAME "->%s: Debug - " __message,__FUNCTION__,##__args);
73 
74 /*
75  * RT2x00 ring types.
76  */
77 
78 /*
79  * Ring names.
80  */
81 #define RING_RX 0x01 /* Ring used for receiving packets. */
82 #define RING_TX 0x02 /* Ring used for transmitting normal packets. */
83 
84 /*
85  * Ring sizes.
86  */
87 #define DATA_FRAME_SIZE 2432
88 #define MGMT_FRAME_SIZE 256
89 
90 /*
91  * RT2x00 xmit flags.
92  */
93 #define XMIT_IFS_SIFS 0x0001
94 #define XMIT_IFS_BACKOFF 0x0002
95 #define XMIT_IFS_NEW_BACKOFF 0x0004
96 #define XMIT_IFS_NONE 0x0008
97 #define XMIT_NEW_SEQUENCE 0x0010
98 #define XMIT_ACK 0x0020
99 #define XMIT_TIMESTAMP 0x0040
100 #define XMIT_RTS 0x0080
101 #define XMIT_OFDM 0x0100
102 #define XMIT_LONG_RETRY 0x0200
103 #define XMIT_MORE_FRAGS 0x0400
104 #define XMIT_SHORT_PREAMBLE 0x0800
105 #define XMIT_START 0x1000
106 
107 /*
108  * RT2x00 Statistics flags.
109  */
110 #define STATS_TX_RESULT 0x01
111 #define STATS_TX_RETRY_COUNT 0x02
112 #define STATS_RX_CRC 0x10
113 #define STATS_RX_PHYSICAL 0x20
114 #define STATS_RX_QUALITY 0x40
115 #define STATS_RX_DROP 0x80
116 
117 /*
118  * TX result flags.
119  */
120 #define TX_SUCCESS 0
121 #define TX_SUCCESS_RETRY 1
122 #define TX_FAIL_RETRY 2
123 #define TX_FAIL_INVALID 3
124 #define TX_FAIL_OTHER 4
125 
126 /*
127  * Channel type defines.
128  */
129 #define CHANNEL_OFDM 0x01
130 #define CHANNEL_UNII_LOW 0x02
131 #define CHANNEL_HIPERLAN2 0x04
132 #define CHANNEL_UNII_HIGH 0x08
133 
134 #define CHANNEL_OFDM_MIN 1
135 #define CHANNEL_OFDM_MAX 14
136 #define CHANNEL_UNII_LOW_MIN 36
137 #define CHANNEL_UNII_LOW_MAX 64
138 #define CHANNEL_HIPERLAN2_MIN 100
139 #define CHANNEL_HIPERLAN2_MAX 140
140 #define CHANNEL_UNII_HIGH_MIN 149
141 #define CHANNEL_UNII_HIGH_MAX 161
142 
143 /*
144  * Device 802.11abg capabilities.
145  */
146 static struct _rt2x00_capabilities{
147  u8 txpower[6];
148  u8 bitrate[12];
149 } __attribute__ ((packed)) capabilities = {
150  /*
151  * tx-power.
152  */
153  .txpower = {
154  3, 12, 25, 50, 75, 100,
155  },
156 
157  /*
158  * Bitrates
159  */
160  .bitrate = {
161  2, 4, 11, 22, /* CCK. */
162  12, 18, 24, 36, 48, 72, 96, 108, /* OFDM. */
163  },
164 };
165 
166 struct _rt2x00_config{
167 
168  u8 config_flags;
169 #define CONFIG_DROP_BCAST 0x0001
170 #define CONFIG_DROP_MCAST 0x0002
171 #define CONFIG_AUTORESP 0x0004
172 
173  u8 antenna_tx;
174  u8 antenna_rx;
175 
176  u8 bssid[ETH_ALEN];
177  u8 short_retry;
178  u8 long_retry;
179 
180  u8 channel;
181  u8 bitrate; /* 0.5Mbit/sec */
182  u8 txpower; /* % */
183 
184  u8 bbpsens;
185 
186  /*
187  * LED status
188  */
189  u8 led_status;
190 
191  u16 __pad2; /* For alignment only. */
192 
193  /*
194  * Duration values in us.
195  */
196  u8 plcp;
197  u8 sifs;
198  u8 slot_time;
199 
200  /*
201  * Configuration values that have to be updated to device.
202  */
203  u16 update_flags;
204 #define UPDATE_ALL_CONFIG 0xffff
205 #define UPDATE_BSSID 0x0001
206 #define UPDATE_PACKET_FILTER 0x0002
207 #define UPDATE_CHANNEL 0x0004
208 #define UPDATE_BITRATE 0x0008
209 #define UPDATE_RETRY 0x0010
210 #define UPDATE_TXPOWER 0x0020
211 #define UPDATE_ANTENNA 0x0040
212 #define UPDATE_DURATION 0x0080
213 #define UPDATE_PREAMBLE 0x0100
214 #define UPDATE_AUTORESP 0x0200
215 #define UPDATE_LED_STATUS 0x0400
216 #define UPDATE_BBPSENS 0x0800
217 
218 } __attribute__ ((packed));
219 
220 struct _rt2x00_core {
221 
222  /*
223  * RT2x00 device status flags (atomic read/write access).
224  */
225  unsigned long flags;
226 
227 #define DEVICE_ENABLED 0 /* Device has been opened. */
228 #define DEVICE_AWAKE 1 /* Device is not suspended. */
229 #define DEVICE_RADIO_ON 2 /* Device antenna is enabled. */
230 #define DEVICE_CONFIG_UPDATE 3 /* Device is updating configuration. */
231 
232  /*
233  * Device handler.
234  */
235  struct _rt2x00_dev_handler *handler;
236 
237  /*
238  * RTnet device we belong to.
239  */
240  struct rtnet_device *rtnet_dev;
241 
242  /*
243  * RTwlan stack structure.
244  */
245  struct rtwlan_device *rtwlan_dev;
246 
247  /*
248  * Device configuration.
249  */
250  struct _rt2x00_config config;
251 
252  void * priv;
253 
254 } __attribute__ ((packed));
255 
256 /*
257  * Device specific handlers.
258  */
259 struct _rt2x00_dev_handler {
260 
261  /*
262  * Device specific module.
263  */
264  struct module *dev_module;
265 
266  /*
267  * Initialization handlers.
268  */
269  int (*dev_probe)(struct _rt2x00_core *core, void *priv);
270  int (*dev_remove)(struct _rt2x00_core *core);
271 
272  /*
273  * Radio control.
274  */
275  int (*dev_radio_on)(struct _rt2x00_core *core);
276  int (*dev_radio_off)(struct _rt2x00_core *core);
277 
278  /*
279  * Configuration handlers.
280  */
281  int (*dev_update_config)(struct _rt2x00_core *core, u16 update_flags);
282 
283  /*
284  * xmit handler.
285  */
286  int (*dev_xmit_packet)(struct _rt2x00_core *core, struct rtskb *rtskb, u16 rate, u16 xmit_flags);
287 
288  /*
289  * Handler for direct access to register from core.
290  */
291  int (*dev_register_access)(struct _rt2x00_core * core, int request, u32 address, u32 * value);
292 
293 } __attribute__ ((packed));
294 
295 static inline void * rt2x00_priv(const struct _rt2x00_core *core) {
296 
297  return core->priv;
298 }
299 
300 /*
301  * Duration calculations
302  * The rate variable passed is: 2 * real_rate (in Mb/s).
303  * Therefore length has to be multiplied with 8 to convert bytes to bits and mulltiply the length
304  * with 2 to compensate for the difference between real_rate and the rate variable.
305  */
306 #define ACK_SIZE 14
307 #define IEEE80211_HEADER 24
308 
309 static inline u16
310 get_duration(const unsigned int size, const u8 rate) {
311 
312  return ((size * 8 * 2) / rate);
313 }
314 
315 static inline u16
316 get_duration_res(const unsigned int size, const u8 rate) {
317 
318  return ((size * 8 * 2) % rate);
319 }
320 
321 static inline u16
322 get_preamble(const struct _rt2x00_config *config) {
323 
324  return 144;
325 }
326 
327 
328 /*
329  * Register handlers.
330  * We store the position of a register field inside a field structure,
331  * This will simplify the process of setting and reading a certain field
332  * inside the register.
333  */
334 struct _rt2x00_field16{
335  u16 bit_offset;
336  u16 bit_mask;
337 } __attribute__ ((packed));
338 
339 struct _rt2x00_field32{
340  u32 bit_offset;
341  u32 bit_mask;
342 } __attribute__ ((packed));
343 
344 #define FIELD16(__offset, __mask) ( (struct _rt2x00_field16) { (__offset), (__mask) } )
345 #define FIELD32(__offset, __mask) ( (struct _rt2x00_field32) { (__offset), (__mask) } )
346 
347 static inline void
348 rt2x00_set_field32(u32 *reg, const struct _rt2x00_field32 field, const u32 value) {
349 
350  *reg &= cpu_to_le32(~(field.bit_mask));
351  *reg |= cpu_to_le32((value << field.bit_offset) & field.bit_mask);
352 }
353 
354 static inline void
355 rt2x00_set_field32_nb(u32 *reg, const struct _rt2x00_field32 field, const u32 value) {
356 
357  *reg &= ~(field.bit_mask);
358  *reg |= (value << field.bit_offset) & field.bit_mask;
359 }
360 
361 static inline u32
362 rt2x00_get_field32(const u32 reg, const struct _rt2x00_field32 field) {
363 
364  return (le32_to_cpu(reg) & field.bit_mask) >> field.bit_offset;
365 }
366 
367 static inline u32
368 rt2x00_get_field32_nb(const u32 reg, const struct _rt2x00_field32 field) {
369 
370  return (reg & field.bit_mask) >> field.bit_offset;
371 }
372 
373 static inline void
374 rt2x00_set_field16(u16 *reg, const struct _rt2x00_field16 field, const u16 value) {
375 
376  *reg &= cpu_to_le16(~(field.bit_mask));
377  *reg |= cpu_to_le16((value << field.bit_offset) & field.bit_mask);
378 }
379 
380 static inline void
381 rt2x00_set_field16_nb(u16 *reg, const struct _rt2x00_field16 field, const u16 value) {
382 
383  *reg &= ~(field.bit_mask);
384  *reg |= (value << field.bit_offset) & field.bit_mask;
385 }
386 
387 static inline u16
388 rt2x00_get_field16(const u16 reg, const struct _rt2x00_field16 field) {
389 
390  return (le16_to_cpu(reg) & field.bit_mask) >> field.bit_offset;
391 }
392 
393 static inline u16
394 rt2x00_get_field16_nb(const u16 reg, const struct _rt2x00_field16 field) {
395 
396  return (reg & field.bit_mask) >> field.bit_offset;
397 }
398 
399 /*
400  * rf register sructure for channel selection.
401  */
402 struct _rf_channel{
403  u32 rf1;
404  u32 rf2;
405  u32 rf3;
406  u32 rf4;
407 }__attribute__ ((packed));
408 
409 /*
410  * Chipset identification
411  * The chipset on the device is composed of a RT and RF chip.
412  * The chipset combination is important for determining device capabilities.
413  */
414 struct _rt2x00_chip{
415  u16 rt;
416  u16 rf;
417 } __attribute__ ((packed));
418 
419 /*
420  * Set chipset data.
421  * Some rf values for RT2400 devices are equal to rf values for RT2500 devices.
422  * To prevent problems, all rf values will be masked to clearly seperate each chipset.
423  */
424 static inline void
425 set_chip(struct _rt2x00_chip *chipset, const u16 rt, const u16 rf) {
426 
427  INFO("Chipset detected - rt: %04x, rf: %04x.\n", rt, rf);
428 
429  chipset->rt = rt;
430  chipset->rf = rf | (chipset->rt & 0xff00);
431 }
432 
433 static inline char
434 rt2x00_rt(const struct _rt2x00_chip *chipset, const u16 chip) {
435 
436  return (chipset->rt == chip);
437 }
438 
439 static inline char
440 rt2x00_rf(const struct _rt2x00_chip *chipset, const u16 chip) {
441 
442  return (chipset->rf == chip);
443 }
444 
445 static inline u16
446 rt2x00_get_rf(const struct _rt2x00_chip *chipset) {
447 
448  return chipset->rf;
449 }
450 
451 /*
452  * _data_ring
453  * Data rings are used by the device to send and receive packets.
454  * The data_addr is the base address of the data memory.
455  * Device specifice information is pointed to by the priv pointer.
456  * The index values may only be changed with the functions ring_index_inc()
457  * and ring_index_done_inc().
458  */
459 struct _data_ring {
460 
461  /*
462  * Base address of packet ring.
463  */
464  dma_addr_t data_dma;
465  void *data_addr;
466 
467  /*
468  * Private device specific data.
469  */
470  void *priv;
471  struct _rt2x00_core *core;
472 
473  /*
474  * Current index values.
475  */
476  u8 index;
477  u8 index_done;
478 
479  /*
480  * Ring type set with RING_* define.
481  */
482  u8 ring_type;
483 
484  /*
485  * Number of entries in this ring.
486  */
487  u8 max_entries;
488 
489  /*
490  * Size of packet and descriptor in bytes.
491  */
492  u16 entry_size;
493  u16 desc_size;
494 
495  /*
496  * Total allocated memory size.
497  */
498  u32 mem_size;
499 } __attribute__ ((packed));
500 
501 /*
502  * Number of entries in a packet ring.
503  */
504 #define RX_ENTRIES 8
505 #define TX_ENTRIES 8
506 #define ATIM_ENTRIES 1
507 #define PRIO_ENTRIES 2
508 #define BEACON_ENTRIES 1
509 
510 /*
511  * Initialization and cleanup routines.
512  */
513 static inline void rt2x00_init_ring(
514  struct _rt2x00_core *core,
515  struct _data_ring *ring,
516  const u8 ring_type,
517  const u16 max_entries,
518  const u16 entry_size,
519  const u16 desc_size) {
520 
521  ring->core = core;
522  ring->index = 0;
523  ring->index_done = 0;
524  ring->ring_type = ring_type;
525  ring->max_entries = max_entries;
526  ring->entry_size = entry_size;
527  ring->desc_size = desc_size;
528  ring->mem_size = ring->max_entries * (ring->desc_size + ring->entry_size);
529 }
530 
531 static inline void rt2x00_deinit_ring(struct _data_ring *ring) {
532 
533  ring->core = NULL;
534  ring->index = 0;
535  ring->index_done = 0;
536  ring->ring_type = 0;
537  ring->max_entries = 0;
538  ring->entry_size = 0;
539  ring->desc_size = 0;
540  ring->mem_size = 0;
541 }
542 
543 /*
544  * Ring index manipulation functions.
545  */
546 static inline void rt2x00_ring_index_inc(struct _data_ring *ring) {
547 
548  ring->index = (++ring->index < ring->max_entries) ? ring->index : 0;
549 }
550 
551 static inline void rt2x00_ring_index_done_inc(struct _data_ring *ring) {
552 
553  ring->index_done = (++ring->index_done < ring->max_entries) ? ring->index_done : 0;
554 }
555 
556 static inline void rt2x00_ring_clear_index(struct _data_ring *ring) {
557 
558  ring->index = 0;
559  ring->index_done = 0;
560 }
561 
562 static inline u8 rt2x00_ring_empty(struct _data_ring *ring) {
563 
564  return ring->index_done == ring->index;
565 }
566 
567 static inline u8 rt2x00_ring_free_entries(struct _data_ring *ring) {
568 
569  if(ring->index >= ring->index_done)
570  return ring->max_entries - (ring->index - ring->index_done);
571  else
572  return ring->index_done - ring->index;
573 }
574 
575 /*
576  * Return PLCP value matching the rate.
577  * PLCP values according to ieee802.11a-1999 p.14.
578  */
579 static inline u8 rt2x00_get_plcp(const u8 rate) {
580 
581  u8 counter = 0x00;
582  u8 plcp[12] = {
583  0x00, 0x01, 0x02, 0x03, /* CCK. */
584  0x0b, 0x0f, 0x0a, 0x0e, 0x09, 0x0d, 0x08, 0x0c, /* OFDM. */
585  };
586 
587  for(; counter < 12; counter++){
588  if(capabilities.bitrate[counter] == rate)
589  return plcp[counter];
590  }
591 
592  return 0xff;
593 }
594 
595 #define OFDM_CHANNEL(__channel) ( (__channel) >= CHANNEL_OFDM_MIN && (__channel) <= CHANNEL_OFDM_MAX )
596 #define UNII_LOW_CHANNEL(__channel) ( (__channel) >= CHANNEL_UNII_LOW_MIN && (__channel) <= CHANNEL_UNII_LOW_MAX )
597 #define HIPERLAN2_CHANNEL(__channel) ( (__channel) >= CHANNEL_HIPERLAN2_MIN && (__channel) <= CHANNEL_HIPERLAN2_MAX )
598 #define UNII_HIGH_CHANNEL(__channel) ( (__channel) >= CHANNEL_UNII_HIGH_MIN && (__channel) <= CHANNEL_UNII_HIGH_MAX )
599 
600 /*
601  * Return the index value of the channel starting from the first channel of the range.
602  * Where range can be OFDM, UNII (low), HiperLAN2 or UNII (high).
603  */
604 static inline int rt2x00_get_channel_index(const u8 channel) {
605 
606  if(OFDM_CHANNEL(channel))
607  return (channel - 1);
608 
609  if(channel % 4)
610  return -EINVAL;
611 
612  if(UNII_LOW_CHANNEL(channel))
613  return ((channel - CHANNEL_UNII_LOW_MIN) / 4);
614  else if(HIPERLAN2_CHANNEL(channel))
615  return ((channel - CHANNEL_HIPERLAN2_MIN) / 4);
616  else if(UNII_HIGH_CHANNEL(channel))
617  return ((channel - CHANNEL_UNII_HIGH_MIN) / 4);
618  return -EINVAL;
619 }
620 
621 
622 /*
623  * RT2x00 core module functions that can be used in the device specific modules.
624  */
625 extern struct rtnet_device * rt2x00_core_probe(struct _rt2x00_dev_handler * handler, void *priv, u32 sizeof_dev);
626 extern void rt2x00_core_remove(struct rtnet_device *rtnet_dev);
627 
628 #endif
629 
630