Home | History | Annotate | Line # | Download | only in ic
rtwvar.h revision 1.1
      1 /* $NetBSD: rtwvar.h,v 1.1 2004/09/26 02:29:15 dyoung Exp $ */
      2 /*-
      3  * Copyright (c) 2004, 2005 David Young.  All rights reserved.
      4  *
      5  * Driver for the Realtek RTL8180 802.11 MAC/BBP by David Young.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. The name of David Young may not be used to endorse or promote
     16  *    products derived from this software without specific prior
     17  *    written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY David Young ``AS IS'' AND ANY
     20  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
     21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
     22  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL David
     23  * Young BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
     25  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     27  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     28  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
     30  * OF SUCH DAMAGE.
     31  */
     32 
     33 #ifndef _DEV_IC_RTWVAR_H_
     34 #define	_DEV_IC_RTWVAR_H_
     35 
     36 #include <sys/queue.h>
     37 #include <sys/callout.h>
     38 
     39 #ifdef RTW_DEBUG
     40 extern int rtw_debug;
     41 #define RTW_DPRINTF(x)	if (rtw_debug > 0) printf x
     42 #define RTW_DPRINTF2(x)	if (rtw_debug > 1) printf x
     43 #define RTW_DPRINTF3(x)	if (rtw_debug > 2) printf x
     44 #define	DPRINTF(sc, x)	if ((sc)->sc_ic.ic_if.if_flags & IFF_DEBUG) printf x
     45 #define	DPRINTF2(sc, x)	if ((sc)->sc_ic.ic_if.if_flags & IFF_DEBUG) RTW_DPRINTF2(x)
     46 #define	DPRINTF3(sc, x)	if ((sc)->sc_ic.ic_if.if_flags & IFF_DEBUG) RTW_DPRINTF3(x)
     47 #else /* RTW_DEBUG */
     48 #define RTW_DPRINTF(x)
     49 #define RTW_DPRINTF2(x)
     50 #define RTW_DPRINTF3(x)
     51 #define	DPRINTF(sc, x)
     52 #define	DPRINTF2(sc, x)
     53 #define	DPRINTF3(sc, x)
     54 #endif /* RTW_DEBUG */
     55 
     56 #if 0
     57 enum rtw_rftype {
     58 	RTW_RFTYPE_INTERSIL = 0,
     59 	RTW_RFTYPE_RFMD,
     60 	RTW_RFTYPE_PHILIPS,
     61 	RTW_RFTYPE_MAXIM
     62 };
     63 #endif
     64 
     65 enum rtw_locale {
     66 	RTW_LOCALE_USA = 0,
     67 	RTW_LOCALE_EUROPE,
     68 	RTW_LOCALE_JAPAN,
     69 	RTW_LOCALE_UNKNOWN
     70 };
     71 
     72 enum rtw_rfchipid {
     73 	RTW_RFCHIPID_RESERVED = 0,
     74 	RTW_RFCHIPID_INTERSIL = 1,
     75 	RTW_RFCHIPID_RFMD = 2,
     76 	RTW_RFCHIPID_PHILIPS = 3,
     77 	RTW_RFCHIPID_MAXIM = 4,
     78 	RTW_RFCHIPID_GCT = 5
     79 };
     80 
     81 /* sc_flags */
     82 #define RTW_F_ENABLED		0x00000001	/* chip is enabled */
     83 #define RTW_F_DIGPHY		0x00000002	/* digital PHY */
     84 #define RTW_F_DFLANTB		0x00000004	/* B antenna is default */
     85 #define RTW_F_ANTDIV		0x00000010	/* h/w antenna diversity */
     86 #define RTW_F_9356SROM		0x00000020	/* 93c56 SROM */
     87 #define RTW_F_SLEEP		0x00000040	/* chip is enabled */
     88 	/* all PHY flags */
     89 #define RTW_F_ALLPHY		(RTW_F_DIGPHY|RTW_F_DFLANTB|RTW_F_ANTDIV)
     90 
     91 struct rtw_regs {
     92 	bus_space_tag_t		r_bt;
     93 	bus_space_handle_t	r_bh;
     94 };
     95 
     96 #define RTW_SR_GET(sr, ofs) \
     97     (((sr)->sr_content[(ofs)/2] >> (((ofs) % 2 == 0) ? 0 : 8)) & 0xff)
     98 
     99 #define RTW_SR_GET16(sr, ofs) \
    100     (RTW_SR_GET((sr), (ofs)) | (RTW_SR_GET((sr), (ofs) + 1) << 8))
    101 
    102 struct rtw_srom {
    103 	u_int16_t		*sr_content;
    104 	u_int16_t		sr_size;
    105 };
    106 
    107 struct rtw_rxctl {
    108 	struct mbuf			*srx_mbuf;
    109 	bus_dmamap_t			srx_dmamap;
    110 };
    111 
    112 struct rtw_txctl {
    113 	SIMPLEQ_ENTRY(rtw_txctl)	stx_q;
    114 	struct mbuf			*stx_mbuf;
    115 	bus_dmamap_t			stx_dmamap;
    116 	struct ieee80211_node		*stx_ni;	/* destination node */
    117 	u_int				stx_first;	/* 1st hw descriptor */
    118 	u_int				stx_last;	/* last hw descriptor */
    119 };
    120 
    121 #define RTW_NTXPRI	4	/* number of Tx priorities */
    122 #define RTW_TXPRILO	0
    123 #define RTW_TXPRIMD	1
    124 #define RTW_TXPRIHI	2
    125 #define RTW_TXPRIBCN	3	/* beacon priority */
    126 
    127 #define RTW_MAXPKTSEGS		32	/* max 32 segments per Tx packet */
    128 
    129 #define CASSERT(cond, complaint) complaint[(cond) ? 0 : -1] = complaint[(cond) ? 0 : -1]
    130 
    131 #define RTW_NTXDESC_ROUNDUP(n) \
    132     roundup(n, RTW_DESC_ALIGNMENT / sizeof(struct rtw_txdesc))
    133 
    134 #define RTW_TXQLEN_ROUNDUP(n) \
    135     (RTW_NTXDESC_ROUNDUP(n * RTW_MAXPKTSEGS) / RTW_MAXPKTSEGS)
    136 
    137 #define RTW_RXQLEN_ROUNDUP(n) \
    138     roundup(n, RTW_DESC_ALIGNMENT / sizeof(struct rtw_rxctl))
    139 
    140 /* The descriptor rings must begin on RTW_DESC_ALIGNMENT boundaries.
    141  * I allocate them consecutively from one buffer, so just round up.
    142  */
    143 #define RTW_TXQLENLO	RTW_TXQLEN_ROUNDUP(64)	/* high-priority queue length */
    144 #define RTW_TXQLENMD	RTW_TXQLEN_ROUNDUP(32)	/* medium-priority */
    145 #define RTW_TXQLENHI	RTW_TXQLEN_ROUNDUP(16)	/* high-priority */
    146 #define RTW_TXQLENBCN	1			/* beacon */
    147 
    148 #define RTW_NTXDESCLO	(RTW_TXQLENLO * RTW_MAXPKTSEGS)
    149 #define RTW_NTXDESCMD	(RTW_TXQLENMD * RTW_MAXPKTSEGS)
    150 #define RTW_NTXDESCHI	(RTW_TXQLENHI * RTW_MAXPKTSEGS)
    151 #define RTW_NTXDESCBCN	(RTW_TXQLENBCN * RTW_MAXPKTSEGS)
    152 
    153 #define RTW_NTXDESCTOTAL	(RTW_NTXDESCLO + RTW_NTXDESCMD + \
    154 				 RTW_NTXDESCHI + RTW_NTXDESCBCN)
    155 
    156 #define RTW_RXQLEN	RTW_RXQLEN_ROUNDUP(32)
    157 #define RTW_NRXDESC	RTW_RXQLEN
    158 
    159 struct rtw_txdesc_blk {
    160 	u_int			htc_ndesc;
    161 	u_int			htc_next;
    162 	u_int			htc_nfree;
    163 	bus_addr_t		htc_physbase;
    164 	bus_addr_t		htc_ofs;
    165 	struct rtw_txdesc	*htc_desc;
    166 };
    167 
    168 #define RTW_NEXT_DESC(htc, idx) \
    169     (htc->htc_physbase + \
    170      sizeof(struct rtw_txdesc) * ((idx + 1) % htc->htc_ndesc))
    171 
    172 SIMPLEQ_HEAD(rtw_txq, rtw_txctl);
    173 
    174 struct rtw_txctl_blk {
    175 	/* dirty/free s/w descriptors */
    176 	struct rtw_txq		stc_dirtyq;
    177 	struct rtw_txq		stc_freeq;
    178 	u_int			stc_ndesc;
    179 	struct rtw_txctl	*stc_desc;
    180 };
    181 
    182 struct rtw_descs {
    183 	struct rtw_txdesc	hd_txlo[RTW_NTXDESCLO];
    184 	struct rtw_txdesc	hd_txmd[RTW_NTXDESCMD];
    185 	struct rtw_txdesc	hd_txhi[RTW_NTXDESCMD];
    186 	struct rtw_rxdesc	hd_rx[RTW_NRXDESC];
    187 	struct rtw_txdesc	hd_bcn[RTW_NTXDESCBCN];
    188 };
    189 #define RTW_DESC_OFFSET(ring, i)	offsetof(struct rtw_descs, ring[i])
    190 #define RTW_RING_OFFSET(ring)		RTW_DESC_OFFSET(ring, 0)
    191 #define RTW_RING_BASE(sc, ring)		((sc)->sc_desc_physaddr + \
    192 					 RTW_RING_OFFSET(ring))
    193 
    194 /* Radio capture format for RTL8180. */
    195 
    196 #define RTW_RX_RADIOTAP_PRESENT	\
    197 	((1 << IEEE80211_RADIOTAP_FLAGS) | (1 << IEEE80211_RADIOTAP_RATE) | \
    198 	 (1 << IEEE80211_RADIOTAP_CHANNEL) | \
    199 	 (1 << IEEE80211_RADIOTAP_DB_ANTSIGNAL))
    200 
    201 struct rtw_rx_radiotap_header {
    202 	struct ieee80211_radiotap_header	rr_ihdr;
    203 	u_int8_t				rr_flags;
    204 	u_int8_t				rr_rate;
    205 	u_int16_t				rr_chan_freq;
    206 	u_int16_t				rr_chan_flags;
    207 	u_int8_t				rr_antsignal;
    208 } __attribute__((__packed__));
    209 
    210 #define RTW_TX_RADIOTAP_PRESENT	((1 << IEEE80211_RADIOTAP_FLAGS) | \
    211 				 (1 << IEEE80211_RADIOTAP_RATE) | \
    212 				 (1 << IEEE80211_RADIOTAP_CHANNEL))
    213 
    214 struct rtw_tx_radiotap_header {
    215 	struct ieee80211_radiotap_header	rt_ihdr;
    216 	u_int8_t				rt_flags;
    217 	u_int8_t				rt_rate;
    218 	u_int16_t				rt_chan_freq;
    219 	u_int16_t				rt_chan_flags;
    220 } __attribute__((__packed__));
    221 
    222 enum rtw_attach_state {FINISHED, FINISH_DESCMAP_LOAD, FINISH_DESCMAP_CREATE,
    223 	FINISH_DESC_MAP, FINISH_DESC_ALLOC, FINISH_RXMAPS_CREATE,
    224 	FINISH_TXMAPS_CREATE, FINISH_RESET, FINISH_READ_SROM, FINISH_PARSE_SROM,
    225 	FINISH_RF_ATTACH, FINISH_ID_STA, FINISH_TXDESCBLK_SETUP,
    226 	FINISH_TXCTLBLK_SETUP, DETACHED};
    227 
    228 struct rtw_hooks {
    229 	void			*rh_shutdown;	/* shutdown hook */
    230 	void			*rh_power;	/* power management hook */
    231 };
    232 
    233 struct rtw_mtbl {
    234 	int			(*mt_newstate)(struct ieee80211com *,
    235 					enum ieee80211_state, int);
    236 	void			(*mt_recv_mgmt)(struct ieee80211com *,
    237 				    struct mbuf *, struct ieee80211_node *,
    238 				    int, int, u_int32_t);
    239 	struct ieee80211_node	*(*mt_node_alloc)(struct ieee80211com *);
    240 	void			(*mt_node_free)(struct ieee80211com *,
    241 					struct ieee80211_node *);
    242 };
    243 
    244 enum rtw_pwrstate { RTW_OFF = 0, RTW_SLEEP, RTW_ON };
    245 
    246 typedef void (*rtw_continuous_tx_cb_t)(void *arg, int);
    247 
    248 struct rtw_phy {
    249 	struct rtw_rf	*p_rf;
    250 	struct rtw_regs	*p_regs;
    251 };
    252 
    253 struct rtw_bbpset {
    254 	u_int	bb_antatten;
    255 	u_int	bb_chestlim;
    256 	u_int	bb_chsqlim;
    257 	u_int	bb_ifagcdet;
    258 	u_int	bb_ifagcini;
    259 	u_int	bb_ifagclimit;
    260 	u_int	bb_lnadet;
    261 	u_int	bb_sys1;
    262 	u_int	bb_sys2;
    263 	u_int	bb_sys3;
    264 	u_int	bb_trl;
    265 	u_int	bb_txagc;
    266 };
    267 
    268 struct rtw_rf {
    269 	void	(*rf_destroy)(struct rtw_rf *);
    270 	/* args: frequency, txpower, power state */
    271 	int	(*rf_init)(struct rtw_rf *, u_int, u_int8_t,
    272 	                  enum rtw_pwrstate);
    273 	/* arg: power state */
    274 	int	(*rf_pwrstate)(struct rtw_rf *, enum rtw_pwrstate);
    275 	/* arg: frequency */
    276 	int	(*rf_tune)(struct rtw_rf *, u_int);
    277 	/* arg: txpower */
    278 	int	(*rf_txpower)(struct rtw_rf *, u_int8_t);
    279 	rtw_continuous_tx_cb_t	rf_continuous_tx_cb;
    280 	void			*rf_continuous_tx_arg;
    281 	struct rtw_bbpset	rf_bbpset;
    282 };
    283 
    284 static __inline void
    285 rtw_rf_destroy(struct rtw_rf *rf)
    286 {
    287 	(*rf->rf_destroy)(rf);
    288 }
    289 
    290 static __inline int
    291 rtw_rf_init(struct rtw_rf *rf, u_int freq, u_int8_t opaque_txpower,
    292     enum rtw_pwrstate power)
    293 {
    294 	return (*rf->rf_init)(rf, freq, opaque_txpower, power);
    295 }
    296 
    297 static __inline int
    298 rtw_rf_pwrstate(struct rtw_rf *rf, enum rtw_pwrstate power)
    299 {
    300 	return (*rf->rf_pwrstate)(rf, power);
    301 }
    302 
    303 static __inline int
    304 rtw_rf_tune(struct rtw_rf *rf, u_int freq)
    305 {
    306 	return (*rf->rf_tune)(rf, freq);
    307 }
    308 
    309 static __inline int
    310 rtw_rf_txpower(struct rtw_rf *rf, u_int8_t opaque_txpower)
    311 {
    312 	return (*rf->rf_txpower)(rf, opaque_txpower);
    313 }
    314 
    315 typedef int (*rtw_rf_write_t)(struct rtw_regs *, enum rtw_rfchipid, u_int,
    316     u_int32_t);
    317 
    318 struct rtw_rfbus {
    319 	struct rtw_regs		*b_regs;
    320 	rtw_rf_write_t		b_write;
    321 };
    322 
    323 static __inline int
    324 rtw_rfbus_write(struct rtw_rfbus *bus, enum rtw_rfchipid rfchipid, u_int addr,
    325     u_int32_t val)
    326 {
    327 	return (*bus->b_write)(bus->b_regs, rfchipid, addr, val);
    328 }
    329 
    330 struct rtw_max2820 {
    331 	struct rtw_rf		mx_rf;
    332 	struct rtw_rfbus	mx_bus;
    333 	int			mx_is_a;	/* 1: MAX2820A/MAX2821A */
    334 };
    335 
    336 struct rtw_sa2400 {
    337 	struct rtw_rf		sa_rf;
    338 	struct rtw_rfbus	sa_bus;
    339 	int			sa_digphy;	/* 1: digital PHY */
    340 };
    341 
    342 typedef void (*rtw_pwrstate_t)(struct rtw_regs *, enum rtw_pwrstate, int);
    343 
    344 struct rtw_softc {
    345 	struct device		sc_dev;
    346 	struct ieee80211com	sc_ic;
    347 	struct rtw_regs		sc_regs;
    348 	bus_dma_tag_t		sc_dmat;
    349 	u_int32_t		sc_flags;
    350 
    351 #if 0
    352 	enum rtw_rftype		sc_rftype;
    353 #endif
    354 	enum rtw_attach_state	sc_attach_state;
    355 	enum rtw_rfchipid	sc_rfchipid;
    356 	enum rtw_locale		sc_locale;
    357 	u_int8_t		sc_phydelay;
    358 
    359 	/* s/w Tx/Rx descriptors */
    360 	struct rtw_txctl_blk	sc_txctl_blk[RTW_NTXPRI];
    361 	struct rtw_rxctl	sc_rxctl[RTW_RXQLEN];
    362 	u_int			sc_txq;
    363 	u_int			sc_txnext;
    364 
    365 	struct rtw_txdesc_blk	sc_txdesc_blk[RTW_NTXPRI];
    366 	struct rtw_rxdesc	*sc_rxdesc;
    367 	u_int			sc_rxnext;
    368 
    369 	struct rtw_descs	*sc_descs;
    370 
    371 	bus_dma_segment_t	sc_desc_segs;
    372 	int			sc_desc_nsegs;
    373 	bus_dmamap_t		sc_desc_dmamap;
    374 #define	sc_desc_physaddr sc_desc_dmamap->dm_segs[0].ds_addr
    375 
    376 	struct rtw_srom		sc_srom;
    377 
    378 	enum rtw_pwrstate	sc_pwrstate;
    379 
    380 	rtw_pwrstate_t		sc_pwrstate_cb;
    381 
    382 	struct rtw_rf		*sc_rf;
    383 
    384 	u_int16_t		sc_inten;
    385 
    386 	/* interrupt acknowledge hook */
    387 	void (*sc_intr_ack) __P((struct rtw_regs *));
    388 
    389 	int			(*sc_enable)(struct rtw_softc *);
    390 	void			(*sc_disable)(struct rtw_softc *);
    391 	void			(*sc_power)(struct rtw_softc *, int);
    392 	struct rtw_mtbl		sc_mtbl;
    393 	struct rtw_hooks	sc_hooks;
    394 
    395 	caddr_t			sc_radiobpf;
    396 
    397 	struct callout		sc_scan_ch;
    398 	u_int			sc_cur_chan;
    399 
    400 	u_int32_t		sc_tsfth;	/* most significant TSFT bits */
    401 	u_int32_t		sc_rcr;		/* RTW_RCR */
    402 	u_int8_t		sc_csthr;	/* carrier-sense threshold */
    403 
    404 	int			sc_do_tick;	/* indicate 1s ticks */
    405 	struct timeval		sc_tick0;	/* first tick */
    406 
    407 	uint8_t		sc_rev;			/* PCI/Cardbus revision */
    408 
    409 	union {
    410 		struct rtw_rx_radiotap_header	tap;
    411 		u_int8_t			pad[64];
    412 	} sc_rxtapu;
    413 	union {
    414 		struct rtw_tx_radiotap_header	tap;
    415 		u_int8_t			pad[64];
    416 	} sc_txtapu;
    417 };
    418 
    419 #define	sc_if		sc_ic.ic_if
    420 #define sc_rxtap	sc_rxtapu.tap
    421 #define sc_txtap	sc_txtapu.tap
    422 
    423 void rtw_txdac_enable(struct rtw_regs *, int);
    424 void rtw_anaparm_enable(struct rtw_regs *, int);
    425 void rtw_config0123_enable(struct rtw_regs *, int);
    426 void rtw_continuous_tx_enable(struct rtw_regs *, int);
    427 
    428 void rtw_attach(struct rtw_softc *);
    429 int rtw_detach(struct rtw_softc *);
    430 int rtw_intr(void *);
    431 
    432 void rtw_disable(struct rtw_softc *);
    433 int rtw_enable(struct rtw_softc *);
    434 
    435 int rtw_activate(struct device *, enum devact);
    436 void rtw_power(int, void *);
    437 void rtw_shutdown(void *);
    438 
    439 const char *rtw_pwrstate_string(enum rtw_pwrstate);
    440 
    441 #endif /* _DEV_IC_RTWVAR_H_ */
    442