Home | History | Annotate | Line # | Download | only in usb
if_ural.c revision 1.11.10.1
      1  1.11.10.1   gdamore /*	$NetBSD: if_ural.c,v 1.11.10.1 2006/07/13 17:49:44 gdamore Exp $ */
      2  1.11.10.1   gdamore /*	$FreeBSD: /repoman/r/ncvs/src/sys/dev/usb/if_ural.c,v 1.40 2006/06/02 23:14:40 sam Exp $	*/
      3        1.1  drochner 
      4        1.1  drochner /*-
      5  1.11.10.1   gdamore  * Copyright (c) 2005, 2006
      6        1.1  drochner  *	Damien Bergamini <damien.bergamini (at) free.fr>
      7        1.1  drochner  *
      8        1.1  drochner  * Permission to use, copy, modify, and distribute this software for any
      9        1.1  drochner  * purpose with or without fee is hereby granted, provided that the above
     10        1.1  drochner  * copyright notice and this permission notice appear in all copies.
     11        1.1  drochner  *
     12        1.1  drochner  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     13        1.1  drochner  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     14        1.1  drochner  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     15        1.1  drochner  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     16        1.1  drochner  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     17        1.1  drochner  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     18        1.1  drochner  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     19        1.1  drochner  */
     20        1.1  drochner 
     21        1.1  drochner /*-
     22        1.1  drochner  * Ralink Technology RT2500USB chipset driver
     23        1.1  drochner  * http://www.ralinktech.com/
     24        1.1  drochner  */
     25        1.1  drochner 
     26        1.1  drochner #include <sys/cdefs.h>
     27  1.11.10.1   gdamore __KERNEL_RCSID(0, "$NetBSD: if_ural.c,v 1.11.10.1 2006/07/13 17:49:44 gdamore Exp $");
     28        1.1  drochner 
     29        1.1  drochner #include "bpfilter.h"
     30        1.1  drochner 
     31        1.1  drochner #include <sys/param.h>
     32        1.1  drochner #include <sys/sockio.h>
     33        1.1  drochner #include <sys/sysctl.h>
     34        1.1  drochner #include <sys/mbuf.h>
     35        1.1  drochner #include <sys/kernel.h>
     36        1.1  drochner #include <sys/socket.h>
     37        1.1  drochner #include <sys/systm.h>
     38        1.1  drochner #include <sys/malloc.h>
     39        1.1  drochner #include <sys/conf.h>
     40        1.1  drochner #include <sys/device.h>
     41        1.1  drochner 
     42        1.1  drochner #include <machine/bus.h>
     43        1.1  drochner #include <machine/endian.h>
     44        1.1  drochner #include <machine/intr.h>
     45        1.1  drochner 
     46        1.1  drochner #if NBPFILTER > 0
     47        1.1  drochner #include <net/bpf.h>
     48        1.1  drochner #endif
     49        1.1  drochner #include <net/if.h>
     50        1.1  drochner #include <net/if_arp.h>
     51        1.1  drochner #include <net/if_dl.h>
     52        1.1  drochner #include <net/if_ether.h>
     53        1.1  drochner #include <net/if_media.h>
     54        1.1  drochner #include <net/if_types.h>
     55        1.1  drochner 
     56        1.1  drochner #include <netinet/in.h>
     57        1.1  drochner #include <netinet/in_systm.h>
     58        1.1  drochner #include <netinet/in_var.h>
     59        1.1  drochner #include <netinet/ip.h>
     60        1.1  drochner 
     61        1.2  drochner #include <net80211/ieee80211_netbsd.h>
     62        1.1  drochner #include <net80211/ieee80211_var.h>
     63        1.1  drochner #include <net80211/ieee80211_radiotap.h>
     64        1.1  drochner 
     65        1.1  drochner #include <dev/usb/usb.h>
     66        1.1  drochner #include <dev/usb/usbdi.h>
     67        1.1  drochner #include <dev/usb/usbdi_util.h>
     68        1.1  drochner #include <dev/usb/usbdevs.h>
     69        1.1  drochner 
     70        1.1  drochner #include <dev/usb/if_uralreg.h>
     71        1.1  drochner #include <dev/usb/if_uralvar.h>
     72        1.1  drochner 
     73        1.1  drochner #ifdef USB_DEBUG
     74        1.1  drochner #define URAL_DEBUG
     75        1.1  drochner #endif
     76        1.1  drochner 
     77        1.1  drochner #ifdef URAL_DEBUG
     78        1.1  drochner #define DPRINTF(x)	do { if (ural_debug) logprintf x; } while (0)
     79        1.1  drochner #define DPRINTFN(n, x)	do { if (ural_debug >= (n)) logprintf x; } while (0)
     80        1.1  drochner int ural_debug = 0;
     81        1.1  drochner #else
     82        1.1  drochner #define DPRINTF(x)
     83        1.1  drochner #define DPRINTFN(n, x)
     84        1.1  drochner #endif
     85        1.1  drochner 
     86        1.1  drochner /* various supported device vendors/products */
     87        1.1  drochner static const struct usb_devno ural_devs[] = {
     88        1.1  drochner 	{ USB_VENDOR_ASUSTEK,		USB_PRODUCT_ASUSTEK_WL167G },
     89        1.1  drochner 	{ USB_VENDOR_ASUSTEK,		USB_PRODUCT_RALINK_RT2570 },
     90        1.2  drochner 	{ USB_VENDOR_BELKIN,		USB_PRODUCT_BELKIN_F5D7050 },
     91        1.4  drochner 	{ USB_VENDOR_CISCOLINKSYS,	USB_PRODUCT_CISCOLINKSYS_WUSB54G },
     92        1.4  drochner 	{ USB_VENDOR_CISCOLINKSYS,	USB_PRODUCT_CISCOLINKSYS_WUSB54GP },
     93        1.1  drochner 	{ USB_VENDOR_CONCEPTRONIC,	USB_PRODUCT_CONCEPTRONIC_C54RU },
     94        1.1  drochner 	{ USB_VENDOR_DLINK,		USB_PRODUCT_DLINK_DWLG122 },
     95        1.4  drochner 	{ USB_VENDOR_GIGABYTE,		USB_PRODUCT_GIGABYTE_GNWBKG },
     96        1.4  drochner 	{ USB_VENDOR_GUILLEMOT,		USB_PRODUCT_GUILLEMOT_HWGUSB254 },
     97        1.1  drochner 	{ USB_VENDOR_MELCO,		USB_PRODUCT_MELCO_KG54 },
     98        1.1  drochner 	{ USB_VENDOR_MELCO,		USB_PRODUCT_MELCO_KG54AI },
     99        1.4  drochner 	{ USB_VENDOR_MELCO,		USB_PRODUCT_MELCO_KG54YB },
    100        1.4  drochner 	{ USB_VENDOR_MSI,		USB_PRODUCT_MSI_MS6861 },
    101        1.4  drochner 	{ USB_VENDOR_MSI,		USB_PRODUCT_MSI_MS6865 },
    102        1.4  drochner 	{ USB_VENDOR_MSI,		USB_PRODUCT_MSI_MS6869 },
    103        1.1  drochner 	{ USB_VENDOR_RALINK,		USB_PRODUCT_RALINK_RT2570 },
    104        1.1  drochner 	{ USB_VENDOR_RALINK,		USB_PRODUCT_RALINK_RT2570_2 },
    105        1.4  drochner 	{ USB_VENDOR_RALINK,		USB_PRODUCT_RALINK_RT2570_3 },
    106        1.8  jmcneill 	{ USB_VENDOR_RALINK_2,		USB_PRODUCT_RALINK_2_RT2570 },
    107        1.1  drochner 	{ USB_VENDOR_SMC,		USB_PRODUCT_SMC_2862WG },
    108        1.4  drochner 	{ USB_VENDOR_SURECOM,		USB_PRODUCT_SURECOM_EP9001G },
    109        1.4  drochner 	{ USB_VENDOR_VTECH,		USB_PRODUCT_VTECH_RT2570 },
    110        1.4  drochner 	{ USB_VENDOR_ZINWELL,		USB_PRODUCT_ZINWELL_ZWXG261 },
    111        1.1  drochner };
    112        1.1  drochner 
    113        1.1  drochner Static int		ural_alloc_tx_list(struct ural_softc *);
    114        1.1  drochner Static void		ural_free_tx_list(struct ural_softc *);
    115        1.1  drochner Static int		ural_alloc_rx_list(struct ural_softc *);
    116        1.1  drochner Static void		ural_free_rx_list(struct ural_softc *);
    117        1.1  drochner Static int		ural_media_change(struct ifnet *);
    118        1.1  drochner Static void		ural_next_scan(void *);
    119        1.1  drochner Static void		ural_task(void *);
    120        1.1  drochner Static int		ural_newstate(struct ieee80211com *,
    121        1.1  drochner 			    enum ieee80211_state, int);
    122  1.11.10.1   gdamore Static int		ural_rxrate(struct ural_rx_desc *);
    123        1.1  drochner Static void		ural_txeof(usbd_xfer_handle, usbd_private_handle,
    124        1.1  drochner 			    usbd_status);
    125        1.1  drochner Static void		ural_rxeof(usbd_xfer_handle, usbd_private_handle,
    126        1.1  drochner 			    usbd_status);
    127  1.11.10.1   gdamore Static int		ural_ack_rate(struct ieee80211com *, int);
    128        1.1  drochner Static uint16_t		ural_txtime(int, int, uint32_t);
    129        1.1  drochner Static uint8_t		ural_plcp_signal(int);
    130        1.1  drochner Static void		ural_setup_tx_desc(struct ural_softc *,
    131        1.1  drochner 			    struct ural_tx_desc *, uint32_t, int, int);
    132        1.1  drochner Static int		ural_tx_bcn(struct ural_softc *, struct mbuf *,
    133        1.1  drochner 			    struct ieee80211_node *);
    134        1.1  drochner Static int		ural_tx_mgt(struct ural_softc *, struct mbuf *,
    135        1.1  drochner 			    struct ieee80211_node *);
    136        1.1  drochner Static int		ural_tx_data(struct ural_softc *, struct mbuf *,
    137        1.1  drochner 			    struct ieee80211_node *);
    138        1.1  drochner Static void		ural_start(struct ifnet *);
    139        1.1  drochner Static void		ural_watchdog(struct ifnet *);
    140  1.11.10.1   gdamore Static int		ural_reset(struct ifnet *);
    141        1.1  drochner Static int		ural_ioctl(struct ifnet *, u_long, caddr_t);
    142  1.11.10.1   gdamore Static void		ural_set_testmode(struct ural_softc *);
    143        1.1  drochner Static void		ural_eeprom_read(struct ural_softc *, uint16_t, void *,
    144        1.1  drochner 			    int);
    145        1.1  drochner Static uint16_t		ural_read(struct ural_softc *, uint16_t);
    146        1.1  drochner Static void		ural_read_multi(struct ural_softc *, uint16_t, void *,
    147        1.1  drochner 			    int);
    148        1.1  drochner Static void		ural_write(struct ural_softc *, uint16_t, uint16_t);
    149        1.1  drochner Static void		ural_write_multi(struct ural_softc *, uint16_t, void *,
    150        1.1  drochner 			    int);
    151        1.1  drochner Static void		ural_bbp_write(struct ural_softc *, uint8_t, uint8_t);
    152        1.1  drochner Static uint8_t		ural_bbp_read(struct ural_softc *, uint8_t);
    153        1.1  drochner Static void		ural_rf_write(struct ural_softc *, uint8_t, uint32_t);
    154        1.1  drochner Static void		ural_set_chan(struct ural_softc *,
    155        1.1  drochner 			    struct ieee80211_channel *);
    156        1.1  drochner Static void		ural_disable_rf_tune(struct ural_softc *);
    157        1.1  drochner Static void		ural_enable_tsf_sync(struct ural_softc *);
    158  1.11.10.1   gdamore Static void		ural_update_slot(struct ifnet *);
    159  1.11.10.1   gdamore Static void		ural_set_txpreamble(struct ural_softc *);
    160  1.11.10.1   gdamore Static void		ural_set_basicrates(struct ural_softc *);
    161        1.1  drochner Static void		ural_set_bssid(struct ural_softc *, uint8_t *);
    162        1.1  drochner Static void		ural_set_macaddr(struct ural_softc *, uint8_t *);
    163        1.1  drochner Static void		ural_update_promisc(struct ural_softc *);
    164        1.1  drochner Static const char	*ural_get_rf(int);
    165        1.1  drochner Static void		ural_read_eeprom(struct ural_softc *);
    166        1.1  drochner Static int		ural_bbp_init(struct ural_softc *);
    167        1.1  drochner Static void		ural_set_txantenna(struct ural_softc *, int);
    168        1.1  drochner Static void		ural_set_rxantenna(struct ural_softc *, int);
    169        1.1  drochner Static int		ural_init(struct ifnet *);
    170        1.1  drochner Static void		ural_stop(struct ifnet *, int);
    171  1.11.10.1   gdamore Static void		ural_amrr_start(struct ural_softc *,
    172  1.11.10.1   gdamore 			    struct ieee80211_node *);
    173  1.11.10.1   gdamore Static void		ural_amrr_timeout(void *);
    174  1.11.10.1   gdamore Static void		ural_amrr_update(usbd_xfer_handle, usbd_private_handle,
    175  1.11.10.1   gdamore 			    usbd_status status);
    176  1.11.10.1   gdamore Static void		ural_ratectl(struct ural_amrr *,
    177  1.11.10.1   gdamore 			    struct ieee80211_node *);
    178        1.1  drochner 
    179        1.1  drochner /*
    180        1.1  drochner  * Supported rates for 802.11a/b/g modes (in 500Kbps unit).
    181        1.1  drochner  */
    182        1.1  drochner static const struct ieee80211_rateset ural_rateset_11a =
    183        1.1  drochner 	{ 8, { 12, 18, 24, 36, 48, 72, 96, 108 } };
    184        1.1  drochner 
    185        1.1  drochner static const struct ieee80211_rateset ural_rateset_11b =
    186        1.1  drochner 	{ 4, { 2, 4, 11, 22 } };
    187        1.1  drochner 
    188        1.1  drochner static const struct ieee80211_rateset ural_rateset_11g =
    189        1.1  drochner 	{ 12, { 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108 } };
    190        1.1  drochner 
    191        1.1  drochner /*
    192        1.1  drochner  * Default values for MAC registers; values taken from the reference driver.
    193        1.1  drochner  */
    194        1.1  drochner static const struct {
    195        1.1  drochner 	uint16_t	reg;
    196        1.1  drochner 	uint16_t	val;
    197        1.1  drochner } ural_def_mac[] = {
    198        1.1  drochner 	{ RAL_TXRX_CSR5,  0x8c8d },
    199        1.1  drochner 	{ RAL_TXRX_CSR6,  0x8b8a },
    200        1.1  drochner 	{ RAL_TXRX_CSR7,  0x8687 },
    201        1.1  drochner 	{ RAL_TXRX_CSR8,  0x0085 },
    202        1.1  drochner 	{ RAL_MAC_CSR13,  0x1111 },
    203        1.1  drochner 	{ RAL_MAC_CSR14,  0x1e11 },
    204        1.1  drochner 	{ RAL_TXRX_CSR21, 0xe78f },
    205        1.1  drochner 	{ RAL_MAC_CSR9,   0xff1d },
    206        1.1  drochner 	{ RAL_MAC_CSR11,  0x0002 },
    207        1.1  drochner 	{ RAL_MAC_CSR22,  0x0053 },
    208        1.1  drochner 	{ RAL_MAC_CSR15,  0x0000 },
    209        1.1  drochner 	{ RAL_MAC_CSR8,   0x0780 },
    210        1.1  drochner 	{ RAL_TXRX_CSR19, 0x0000 },
    211        1.1  drochner 	{ RAL_TXRX_CSR18, 0x005a },
    212        1.1  drochner 	{ RAL_PHY_CSR2,   0x0000 },
    213        1.1  drochner 	{ RAL_TXRX_CSR0,  0x1ec0 },
    214        1.1  drochner 	{ RAL_PHY_CSR4,   0x000f }
    215        1.1  drochner };
    216        1.1  drochner 
    217        1.1  drochner /*
    218        1.1  drochner  * Default values for BBP registers; values taken from the reference driver.
    219        1.1  drochner  */
    220        1.1  drochner static const struct {
    221        1.1  drochner 	uint8_t	reg;
    222        1.1  drochner 	uint8_t	val;
    223        1.1  drochner } ural_def_bbp[] = {
    224        1.1  drochner 	{  3, 0x02 },
    225        1.1  drochner 	{  4, 0x19 },
    226        1.1  drochner 	{ 14, 0x1c },
    227        1.1  drochner 	{ 15, 0x30 },
    228        1.1  drochner 	{ 16, 0xac },
    229        1.1  drochner 	{ 17, 0x48 },
    230        1.1  drochner 	{ 18, 0x18 },
    231        1.1  drochner 	{ 19, 0xff },
    232        1.1  drochner 	{ 20, 0x1e },
    233        1.1  drochner 	{ 21, 0x08 },
    234        1.1  drochner 	{ 22, 0x08 },
    235        1.1  drochner 	{ 23, 0x08 },
    236        1.1  drochner 	{ 24, 0x80 },
    237        1.1  drochner 	{ 25, 0x50 },
    238        1.1  drochner 	{ 26, 0x08 },
    239        1.1  drochner 	{ 27, 0x23 },
    240        1.1  drochner 	{ 30, 0x10 },
    241        1.1  drochner 	{ 31, 0x2b },
    242        1.1  drochner 	{ 32, 0xb9 },
    243        1.1  drochner 	{ 34, 0x12 },
    244        1.1  drochner 	{ 35, 0x50 },
    245        1.1  drochner 	{ 39, 0xc4 },
    246        1.1  drochner 	{ 40, 0x02 },
    247        1.1  drochner 	{ 41, 0x60 },
    248        1.1  drochner 	{ 53, 0x10 },
    249        1.1  drochner 	{ 54, 0x18 },
    250        1.1  drochner 	{ 56, 0x08 },
    251        1.1  drochner 	{ 57, 0x10 },
    252        1.1  drochner 	{ 58, 0x08 },
    253        1.1  drochner 	{ 61, 0x60 },
    254        1.1  drochner 	{ 62, 0x10 },
    255        1.1  drochner 	{ 75, 0xff }
    256        1.1  drochner };
    257        1.1  drochner 
    258        1.1  drochner /*
    259        1.1  drochner  * Default values for RF register R2 indexed by channel numbers.
    260        1.1  drochner  */
    261        1.1  drochner static const uint32_t ural_rf2522_r2[] = {
    262        1.1  drochner 	0x307f6, 0x307fb, 0x30800, 0x30805, 0x3080a, 0x3080f, 0x30814,
    263        1.1  drochner 	0x30819, 0x3081e, 0x30823, 0x30828, 0x3082d, 0x30832, 0x3083e
    264        1.1  drochner };
    265        1.1  drochner 
    266        1.1  drochner static const uint32_t ural_rf2523_r2[] = {
    267        1.1  drochner 	0x00327, 0x00328, 0x00329, 0x0032a, 0x0032b, 0x0032c, 0x0032d,
    268        1.1  drochner 	0x0032e, 0x0032f, 0x00340, 0x00341, 0x00342, 0x00343, 0x00346
    269        1.1  drochner };
    270        1.1  drochner 
    271        1.1  drochner static const uint32_t ural_rf2524_r2[] = {
    272        1.1  drochner 	0x00327, 0x00328, 0x00329, 0x0032a, 0x0032b, 0x0032c, 0x0032d,
    273        1.1  drochner 	0x0032e, 0x0032f, 0x00340, 0x00341, 0x00342, 0x00343, 0x00346
    274        1.1  drochner };
    275        1.1  drochner 
    276        1.1  drochner static const uint32_t ural_rf2525_r2[] = {
    277        1.1  drochner 	0x20327, 0x20328, 0x20329, 0x2032a, 0x2032b, 0x2032c, 0x2032d,
    278        1.1  drochner 	0x2032e, 0x2032f, 0x20340, 0x20341, 0x20342, 0x20343, 0x20346
    279        1.1  drochner };
    280        1.1  drochner 
    281        1.1  drochner static const uint32_t ural_rf2525_hi_r2[] = {
    282        1.1  drochner 	0x2032f, 0x20340, 0x20341, 0x20342, 0x20343, 0x20344, 0x20345,
    283        1.1  drochner 	0x20346, 0x20347, 0x20348, 0x20349, 0x2034a, 0x2034b, 0x2034e
    284        1.1  drochner };
    285        1.1  drochner 
    286        1.1  drochner static const uint32_t ural_rf2525e_r2[] = {
    287        1.1  drochner 	0x2044d, 0x2044e, 0x2044f, 0x20460, 0x20461, 0x20462, 0x20463,
    288        1.1  drochner 	0x20464, 0x20465, 0x20466, 0x20467, 0x20468, 0x20469, 0x2046b
    289        1.1  drochner };
    290        1.1  drochner 
    291        1.1  drochner static const uint32_t ural_rf2526_hi_r2[] = {
    292        1.1  drochner 	0x0022a, 0x0022b, 0x0022b, 0x0022c, 0x0022c, 0x0022d, 0x0022d,
    293        1.1  drochner 	0x0022e, 0x0022e, 0x0022f, 0x0022d, 0x00240, 0x00240, 0x00241
    294        1.1  drochner };
    295        1.1  drochner 
    296        1.1  drochner static const uint32_t ural_rf2526_r2[] = {
    297        1.1  drochner 	0x00226, 0x00227, 0x00227, 0x00228, 0x00228, 0x00229, 0x00229,
    298        1.1  drochner 	0x0022a, 0x0022a, 0x0022b, 0x0022b, 0x0022c, 0x0022c, 0x0022d
    299        1.1  drochner };
    300        1.1  drochner 
    301        1.1  drochner /*
    302        1.1  drochner  * For dual-band RF, RF registers R1 and R4 also depend on channel number;
    303        1.1  drochner  * values taken from the reference driver.
    304        1.1  drochner  */
    305        1.1  drochner static const struct {
    306        1.1  drochner 	uint8_t		chan;
    307        1.1  drochner 	uint32_t	r1;
    308        1.1  drochner 	uint32_t	r2;
    309        1.1  drochner 	uint32_t	r4;
    310        1.1  drochner } ural_rf5222[] = {
    311        1.1  drochner 	{   1, 0x08808, 0x0044d, 0x00282 },
    312        1.1  drochner 	{   2, 0x08808, 0x0044e, 0x00282 },
    313        1.1  drochner 	{   3, 0x08808, 0x0044f, 0x00282 },
    314        1.1  drochner 	{   4, 0x08808, 0x00460, 0x00282 },
    315        1.1  drochner 	{   5, 0x08808, 0x00461, 0x00282 },
    316        1.1  drochner 	{   6, 0x08808, 0x00462, 0x00282 },
    317        1.1  drochner 	{   7, 0x08808, 0x00463, 0x00282 },
    318        1.1  drochner 	{   8, 0x08808, 0x00464, 0x00282 },
    319        1.1  drochner 	{   9, 0x08808, 0x00465, 0x00282 },
    320        1.1  drochner 	{  10, 0x08808, 0x00466, 0x00282 },
    321        1.1  drochner 	{  11, 0x08808, 0x00467, 0x00282 },
    322        1.1  drochner 	{  12, 0x08808, 0x00468, 0x00282 },
    323        1.1  drochner 	{  13, 0x08808, 0x00469, 0x00282 },
    324        1.1  drochner 	{  14, 0x08808, 0x0046b, 0x00286 },
    325        1.1  drochner 
    326        1.1  drochner 	{  36, 0x08804, 0x06225, 0x00287 },
    327        1.1  drochner 	{  40, 0x08804, 0x06226, 0x00287 },
    328        1.1  drochner 	{  44, 0x08804, 0x06227, 0x00287 },
    329        1.1  drochner 	{  48, 0x08804, 0x06228, 0x00287 },
    330        1.1  drochner 	{  52, 0x08804, 0x06229, 0x00287 },
    331        1.1  drochner 	{  56, 0x08804, 0x0622a, 0x00287 },
    332        1.1  drochner 	{  60, 0x08804, 0x0622b, 0x00287 },
    333        1.1  drochner 	{  64, 0x08804, 0x0622c, 0x00287 },
    334        1.1  drochner 
    335        1.1  drochner 	{ 100, 0x08804, 0x02200, 0x00283 },
    336        1.1  drochner 	{ 104, 0x08804, 0x02201, 0x00283 },
    337        1.1  drochner 	{ 108, 0x08804, 0x02202, 0x00283 },
    338        1.1  drochner 	{ 112, 0x08804, 0x02203, 0x00283 },
    339        1.1  drochner 	{ 116, 0x08804, 0x02204, 0x00283 },
    340        1.1  drochner 	{ 120, 0x08804, 0x02205, 0x00283 },
    341        1.1  drochner 	{ 124, 0x08804, 0x02206, 0x00283 },
    342        1.1  drochner 	{ 128, 0x08804, 0x02207, 0x00283 },
    343        1.1  drochner 	{ 132, 0x08804, 0x02208, 0x00283 },
    344        1.1  drochner 	{ 136, 0x08804, 0x02209, 0x00283 },
    345        1.1  drochner 	{ 140, 0x08804, 0x0220a, 0x00283 },
    346        1.1  drochner 
    347        1.1  drochner 	{ 149, 0x08808, 0x02429, 0x00281 },
    348        1.1  drochner 	{ 153, 0x08808, 0x0242b, 0x00281 },
    349        1.1  drochner 	{ 157, 0x08808, 0x0242d, 0x00281 },
    350        1.1  drochner 	{ 161, 0x08808, 0x0242f, 0x00281 }
    351        1.1  drochner };
    352        1.1  drochner 
    353        1.1  drochner USB_DECLARE_DRIVER(ural);
    354        1.1  drochner 
    355        1.1  drochner USB_MATCH(ural)
    356        1.1  drochner {
    357        1.1  drochner 	USB_MATCH_START(ural, uaa);
    358        1.1  drochner 
    359        1.1  drochner 	if (uaa->iface != NULL)
    360        1.1  drochner 		return UMATCH_NONE;
    361        1.1  drochner 
    362        1.1  drochner 	return (usb_lookup(ural_devs, uaa->vendor, uaa->product) != NULL) ?
    363        1.1  drochner 	    UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
    364        1.1  drochner }
    365        1.1  drochner 
    366        1.1  drochner USB_ATTACH(ural)
    367        1.1  drochner {
    368        1.1  drochner 	USB_ATTACH_START(ural, sc, uaa);
    369        1.1  drochner 	struct ieee80211com *ic = &sc->sc_ic;
    370        1.2  drochner 	struct ifnet *ifp = &sc->sc_if;
    371        1.1  drochner 	usb_interface_descriptor_t *id;
    372        1.1  drochner 	usb_endpoint_descriptor_t *ed;
    373        1.1  drochner 	usbd_status error;
    374        1.1  drochner 	char *devinfop;
    375        1.1  drochner 	int i;
    376        1.1  drochner 
    377        1.1  drochner 	sc->sc_udev = uaa->device;
    378        1.1  drochner 
    379        1.1  drochner 	devinfop = usbd_devinfo_alloc(sc->sc_udev, 0);
    380        1.1  drochner 	USB_ATTACH_SETUP;
    381        1.1  drochner 	printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfop);
    382        1.1  drochner 	usbd_devinfo_free(devinfop);
    383        1.1  drochner 
    384        1.1  drochner 	if (usbd_set_config_no(sc->sc_udev, RAL_CONFIG_NO, 0) != 0) {
    385        1.1  drochner 		printf("%s: could not set configuration no\n",
    386        1.1  drochner 		    USBDEVNAME(sc->sc_dev));
    387        1.1  drochner 		USB_ATTACH_ERROR_RETURN;
    388        1.1  drochner 	}
    389        1.1  drochner 
    390        1.1  drochner 	/* get the first interface handle */
    391        1.1  drochner 	error = usbd_device2interface_handle(sc->sc_udev, RAL_IFACE_INDEX,
    392        1.1  drochner 	    &sc->sc_iface);
    393        1.1  drochner 	if (error != 0) {
    394        1.1  drochner 		printf("%s: could not get interface handle\n",
    395        1.1  drochner 		    USBDEVNAME(sc->sc_dev));
    396        1.1  drochner 		USB_ATTACH_ERROR_RETURN;
    397        1.1  drochner 	}
    398        1.1  drochner 
    399        1.1  drochner 	/*
    400        1.1  drochner 	 * Find endpoints.
    401        1.1  drochner 	 */
    402        1.1  drochner 	id = usbd_get_interface_descriptor(sc->sc_iface);
    403        1.1  drochner 
    404        1.1  drochner 	sc->sc_rx_no = sc->sc_tx_no = -1;
    405        1.1  drochner 	for (i = 0; i < id->bNumEndpoints; i++) {
    406        1.1  drochner 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
    407        1.1  drochner 		if (ed == NULL) {
    408  1.11.10.1   gdamore 			printf("%s: no endpoint descriptor for %d\n",
    409        1.1  drochner 			    USBDEVNAME(sc->sc_dev), i);
    410        1.1  drochner 			USB_ATTACH_ERROR_RETURN;
    411        1.1  drochner 		}
    412        1.1  drochner 
    413        1.1  drochner 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    414        1.1  drochner 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
    415        1.1  drochner 			sc->sc_rx_no = ed->bEndpointAddress;
    416        1.1  drochner 		else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
    417        1.1  drochner 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
    418        1.1  drochner 			sc->sc_tx_no = ed->bEndpointAddress;
    419        1.1  drochner 	}
    420        1.1  drochner 	if (sc->sc_rx_no == -1 || sc->sc_tx_no == -1) {
    421        1.1  drochner 		printf("%s: missing endpoint\n", USBDEVNAME(sc->sc_dev));
    422        1.1  drochner 		USB_ATTACH_ERROR_RETURN;
    423        1.1  drochner 	}
    424        1.1  drochner 
    425        1.1  drochner 	usb_init_task(&sc->sc_task, ural_task, sc);
    426        1.1  drochner 	callout_init(&sc->scan_ch);
    427  1.11.10.1   gdamore 	callout_init(&sc->amrr_ch);
    428        1.1  drochner 
    429        1.1  drochner 	/* retrieve RT2570 rev. no */
    430        1.1  drochner 	sc->asic_rev = ural_read(sc, RAL_MAC_CSR0);
    431        1.1  drochner 
    432        1.1  drochner 	/* retrieve MAC address and various other things from EEPROM */
    433        1.1  drochner 	ural_read_eeprom(sc);
    434        1.1  drochner 
    435  1.11.10.1   gdamore 	printf("%s: MAC/BBP RT2570 (rev 0x%02x), RF %s\n",
    436  1.11.10.1   gdamore 	    USBDEVNAME(sc->sc_dev), sc->asic_rev, ural_get_rf(sc->rf_rev));
    437  1.11.10.1   gdamore 
    438  1.11.10.1   gdamore 	ifp->if_softc = sc;
    439  1.11.10.1   gdamore 	memcpy(ifp->if_xname, USBDEVNAME(sc->sc_dev), IFNAMSIZ);
    440  1.11.10.1   gdamore 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    441  1.11.10.1   gdamore 	ifp->if_init = ural_init;
    442  1.11.10.1   gdamore 	ifp->if_ioctl = ural_ioctl;
    443  1.11.10.1   gdamore 	ifp->if_start = ural_start;
    444  1.11.10.1   gdamore 	ifp->if_watchdog = ural_watchdog;
    445  1.11.10.1   gdamore 	IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
    446  1.11.10.1   gdamore 	IFQ_SET_READY(&ifp->if_snd);
    447        1.1  drochner 
    448        1.2  drochner 	ic->ic_ifp = ifp;
    449        1.1  drochner 	ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
    450        1.1  drochner 	ic->ic_opmode = IEEE80211_M_STA; /* default to BSS mode */
    451        1.1  drochner 	ic->ic_state = IEEE80211_S_INIT;
    452        1.1  drochner 
    453        1.1  drochner 	/* set device capabilities */
    454  1.11.10.1   gdamore 	ic->ic_caps =
    455  1.11.10.1   gdamore 	    IEEE80211_C_IBSS |		/* IBSS mode supported */
    456  1.11.10.1   gdamore 	    IEEE80211_C_MONITOR |	/* monitor mode supported */
    457  1.11.10.1   gdamore 	    IEEE80211_C_HOSTAP |	/* HostAp mode supported */
    458  1.11.10.1   gdamore 	    IEEE80211_C_TXPMGT |	/* tx power management */
    459  1.11.10.1   gdamore 	    IEEE80211_C_SHPREAMBLE |	/* short preamble supported */
    460  1.11.10.1   gdamore 	    IEEE80211_C_SHSLOT |	/* short slot time supported */
    461  1.11.10.1   gdamore 	    IEEE80211_C_WPA;		/* 802.11i */
    462        1.1  drochner 
    463        1.1  drochner 	if (sc->rf_rev == RAL_RF_5222) {
    464        1.1  drochner 		/* set supported .11a rates */
    465        1.1  drochner 		ic->ic_sup_rates[IEEE80211_MODE_11A] = ural_rateset_11a;
    466        1.1  drochner 
    467        1.1  drochner 		/* set supported .11a channels */
    468        1.1  drochner 		for (i = 36; i <= 64; i += 4) {
    469        1.1  drochner 			ic->ic_channels[i].ic_freq =
    470        1.1  drochner 			    ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
    471        1.1  drochner 			ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
    472        1.1  drochner 		}
    473        1.1  drochner 		for (i = 100; i <= 140; i += 4) {
    474        1.1  drochner 			ic->ic_channels[i].ic_freq =
    475        1.1  drochner 			    ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
    476        1.1  drochner 			ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
    477        1.1  drochner 		}
    478        1.1  drochner 		for (i = 149; i <= 161; i += 4) {
    479        1.1  drochner 			ic->ic_channels[i].ic_freq =
    480        1.1  drochner 			    ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
    481        1.1  drochner 			ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
    482        1.1  drochner 		}
    483        1.1  drochner 	}
    484        1.1  drochner 
    485        1.1  drochner 	/* set supported .11b and .11g rates */
    486        1.1  drochner 	ic->ic_sup_rates[IEEE80211_MODE_11B] = ural_rateset_11b;
    487        1.1  drochner 	ic->ic_sup_rates[IEEE80211_MODE_11G] = ural_rateset_11g;
    488        1.1  drochner 
    489        1.1  drochner 	/* set supported .11b and .11g channels (1 through 14) */
    490        1.1  drochner 	for (i = 1; i <= 14; i++) {
    491        1.1  drochner 		ic->ic_channels[i].ic_freq =
    492        1.1  drochner 		    ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ);
    493        1.1  drochner 		ic->ic_channels[i].ic_flags =
    494        1.1  drochner 		    IEEE80211_CHAN_CCK | IEEE80211_CHAN_OFDM |
    495        1.1  drochner 		    IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
    496        1.1  drochner 	}
    497        1.1  drochner 
    498        1.1  drochner 	if_attach(ifp);
    499        1.1  drochner 	ieee80211_ifattach(ic);
    500  1.11.10.1   gdamore 	ic->ic_reset = ural_reset;
    501        1.1  drochner 
    502        1.1  drochner 	/* override state transition machine */
    503        1.1  drochner 	sc->sc_newstate = ic->ic_newstate;
    504        1.1  drochner 	ic->ic_newstate = ural_newstate;
    505        1.1  drochner 	ieee80211_media_init(ic, ural_media_change, ieee80211_media_status);
    506        1.1  drochner 
    507        1.1  drochner #if NBPFILTER > 0
    508        1.1  drochner 	bpfattach2(ifp, DLT_IEEE802_11_RADIO,
    509        1.1  drochner 	    sizeof (struct ieee80211_frame) + 64, &sc->sc_drvbpf);
    510        1.1  drochner 
    511        1.1  drochner 	sc->sc_rxtap_len = sizeof sc->sc_rxtapu;
    512        1.1  drochner 	sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
    513        1.1  drochner 	sc->sc_rxtap.wr_ihdr.it_present = htole32(RAL_RX_RADIOTAP_PRESENT);
    514        1.1  drochner 
    515        1.1  drochner 	sc->sc_txtap_len = sizeof sc->sc_txtapu;
    516        1.1  drochner 	sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
    517        1.1  drochner 	sc->sc_txtap.wt_ihdr.it_present = htole32(RAL_TX_RADIOTAP_PRESENT);
    518        1.1  drochner #endif
    519        1.1  drochner 
    520        1.4  drochner 	ieee80211_announce(ic);
    521        1.4  drochner 
    522        1.1  drochner 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
    523        1.1  drochner 	    USBDEV(sc->sc_dev));
    524        1.1  drochner 
    525        1.1  drochner 	USB_ATTACH_SUCCESS_RETURN;
    526        1.1  drochner }
    527        1.1  drochner 
    528        1.1  drochner USB_DETACH(ural)
    529        1.1  drochner {
    530        1.1  drochner 	USB_DETACH_START(ural, sc);
    531        1.2  drochner 	struct ieee80211com *ic = &sc->sc_ic;
    532        1.2  drochner 	struct ifnet *ifp = &sc->sc_if;
    533        1.1  drochner 	int s;
    534        1.1  drochner 
    535        1.1  drochner 	s = splusb();
    536        1.1  drochner 
    537  1.11.10.1   gdamore 	ural_stop(ifp, 1);
    538        1.1  drochner 	usb_rem_task(sc->sc_udev, &sc->sc_task);
    539        1.1  drochner 	callout_stop(&sc->scan_ch);
    540  1.11.10.1   gdamore 	callout_stop(&sc->amrr_ch);
    541  1.11.10.1   gdamore 
    542  1.11.10.1   gdamore 	if (sc->amrr_xfer != NULL) {
    543  1.11.10.1   gdamore 		usbd_free_xfer(sc->amrr_xfer);
    544  1.11.10.1   gdamore 		sc->amrr_xfer = NULL;
    545  1.11.10.1   gdamore 	}
    546        1.1  drochner 
    547        1.1  drochner 	if (sc->sc_rx_pipeh != NULL) {
    548        1.1  drochner 		usbd_abort_pipe(sc->sc_rx_pipeh);
    549        1.1  drochner 		usbd_close_pipe(sc->sc_rx_pipeh);
    550        1.1  drochner 	}
    551        1.1  drochner 
    552        1.1  drochner 	if (sc->sc_tx_pipeh != NULL) {
    553        1.1  drochner 		usbd_abort_pipe(sc->sc_tx_pipeh);
    554        1.1  drochner 		usbd_close_pipe(sc->sc_tx_pipeh);
    555        1.1  drochner 	}
    556        1.1  drochner 
    557        1.1  drochner 	ural_free_rx_list(sc);
    558        1.1  drochner 	ural_free_tx_list(sc);
    559        1.1  drochner 
    560        1.1  drochner #if NBPFILTER > 0
    561        1.1  drochner 	bpfdetach(ifp);
    562        1.1  drochner #endif
    563        1.2  drochner 	ieee80211_ifdetach(ic);
    564        1.1  drochner 	if_detach(ifp);
    565        1.1  drochner 
    566        1.1  drochner 	splx(s);
    567        1.1  drochner 
    568        1.1  drochner 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
    569        1.1  drochner 	    USBDEV(sc->sc_dev));
    570        1.1  drochner 
    571        1.1  drochner 	return 0;
    572        1.1  drochner }
    573        1.1  drochner 
    574        1.1  drochner Static int
    575        1.1  drochner ural_alloc_tx_list(struct ural_softc *sc)
    576        1.1  drochner {
    577        1.1  drochner 	struct ural_tx_data *data;
    578        1.1  drochner 	int i, error;
    579        1.1  drochner 
    580        1.1  drochner 	sc->tx_queued = 0;
    581        1.1  drochner 
    582        1.1  drochner 	for (i = 0; i < RAL_TX_LIST_COUNT; i++) {
    583        1.1  drochner 		data = &sc->tx_data[i];
    584        1.1  drochner 
    585        1.1  drochner 		data->sc = sc;
    586        1.1  drochner 
    587        1.1  drochner 		data->xfer = usbd_alloc_xfer(sc->sc_udev);
    588        1.1  drochner 		if (data->xfer == NULL) {
    589        1.1  drochner 			printf("%s: could not allocate tx xfer\n",
    590        1.1  drochner 			    USBDEVNAME(sc->sc_dev));
    591        1.1  drochner 			error = ENOMEM;
    592        1.1  drochner 			goto fail;
    593        1.1  drochner 		}
    594        1.1  drochner 
    595        1.1  drochner 		data->buf = usbd_alloc_buffer(data->xfer,
    596        1.1  drochner 		    RAL_TX_DESC_SIZE + MCLBYTES);
    597        1.1  drochner 		if (data->buf == NULL) {
    598        1.1  drochner 			printf("%s: could not allocate tx buffer\n",
    599        1.1  drochner 			    USBDEVNAME(sc->sc_dev));
    600        1.1  drochner 			error = ENOMEM;
    601        1.1  drochner 			goto fail;
    602        1.1  drochner 		}
    603        1.1  drochner 	}
    604        1.1  drochner 
    605        1.1  drochner 	return 0;
    606        1.1  drochner 
    607        1.1  drochner fail:	ural_free_tx_list(sc);
    608        1.1  drochner 	return error;
    609        1.1  drochner }
    610        1.1  drochner 
    611        1.1  drochner Static void
    612        1.1  drochner ural_free_tx_list(struct ural_softc *sc)
    613        1.1  drochner {
    614        1.1  drochner 	struct ural_tx_data *data;
    615        1.1  drochner 	int i;
    616        1.1  drochner 
    617        1.1  drochner 	for (i = 0; i < RAL_TX_LIST_COUNT; i++) {
    618        1.1  drochner 		data = &sc->tx_data[i];
    619        1.1  drochner 
    620        1.1  drochner 		if (data->xfer != NULL) {
    621        1.1  drochner 			usbd_free_xfer(data->xfer);
    622        1.1  drochner 			data->xfer = NULL;
    623        1.1  drochner 		}
    624        1.1  drochner 
    625        1.1  drochner 		if (data->ni != NULL) {
    626        1.1  drochner 			ieee80211_free_node(data->ni);
    627        1.1  drochner 			data->ni = NULL;
    628        1.1  drochner 		}
    629        1.1  drochner 	}
    630        1.1  drochner }
    631        1.1  drochner 
    632        1.1  drochner Static int
    633        1.1  drochner ural_alloc_rx_list(struct ural_softc *sc)
    634        1.1  drochner {
    635        1.1  drochner 	struct ural_rx_data *data;
    636        1.1  drochner 	int i, error;
    637        1.1  drochner 
    638        1.1  drochner 	for (i = 0; i < RAL_RX_LIST_COUNT; i++) {
    639        1.1  drochner 		data = &sc->rx_data[i];
    640        1.1  drochner 
    641        1.1  drochner 		data->sc = sc;
    642        1.1  drochner 
    643        1.1  drochner 		data->xfer = usbd_alloc_xfer(sc->sc_udev);
    644        1.1  drochner 		if (data->xfer == NULL) {
    645        1.1  drochner 			printf("%s: could not allocate rx xfer\n",
    646        1.1  drochner 			    USBDEVNAME(sc->sc_dev));
    647        1.1  drochner 			error = ENOMEM;
    648        1.1  drochner 			goto fail;
    649        1.1  drochner 		}
    650        1.1  drochner 
    651        1.1  drochner 		if (usbd_alloc_buffer(data->xfer, MCLBYTES) == NULL) {
    652        1.1  drochner 			printf("%s: could not allocate rx buffer\n",
    653        1.1  drochner 			    USBDEVNAME(sc->sc_dev));
    654        1.1  drochner 			error = ENOMEM;
    655        1.1  drochner 			goto fail;
    656        1.1  drochner 		}
    657        1.1  drochner 
    658        1.1  drochner 		MGETHDR(data->m, M_DONTWAIT, MT_DATA);
    659        1.1  drochner 		if (data->m == NULL) {
    660        1.1  drochner 			printf("%s: could not allocate rx mbuf\n",
    661        1.1  drochner 			    USBDEVNAME(sc->sc_dev));
    662        1.1  drochner 			error = ENOMEM;
    663        1.1  drochner 			goto fail;
    664        1.1  drochner 		}
    665        1.1  drochner 
    666        1.1  drochner 		MCLGET(data->m, M_DONTWAIT);
    667        1.1  drochner 		if (!(data->m->m_flags & M_EXT)) {
    668        1.1  drochner 			printf("%s: could not allocate rx mbuf cluster\n",
    669        1.1  drochner 			    USBDEVNAME(sc->sc_dev));
    670        1.1  drochner 			error = ENOMEM;
    671        1.1  drochner 			goto fail;
    672        1.1  drochner 		}
    673        1.1  drochner 
    674        1.1  drochner 		data->buf = mtod(data->m, uint8_t *);
    675        1.1  drochner 	}
    676        1.1  drochner 
    677        1.1  drochner 	return 0;
    678        1.1  drochner 
    679        1.1  drochner fail:	ural_free_tx_list(sc);
    680        1.1  drochner 	return error;
    681        1.1  drochner }
    682        1.1  drochner 
    683        1.1  drochner Static void
    684        1.1  drochner ural_free_rx_list(struct ural_softc *sc)
    685        1.1  drochner {
    686        1.1  drochner 	struct ural_rx_data *data;
    687        1.1  drochner 	int i;
    688        1.1  drochner 
    689        1.1  drochner 	for (i = 0; i < RAL_RX_LIST_COUNT; i++) {
    690        1.1  drochner 		data = &sc->rx_data[i];
    691        1.1  drochner 
    692        1.1  drochner 		if (data->xfer != NULL) {
    693        1.1  drochner 			usbd_free_xfer(data->xfer);
    694        1.1  drochner 			data->xfer = NULL;
    695        1.1  drochner 		}
    696        1.1  drochner 
    697        1.1  drochner 		if (data->m != NULL) {
    698        1.1  drochner 			m_freem(data->m);
    699        1.1  drochner 			data->m = NULL;
    700        1.1  drochner 		}
    701        1.1  drochner 	}
    702        1.1  drochner }
    703        1.1  drochner 
    704        1.1  drochner Static int
    705        1.1  drochner ural_media_change(struct ifnet *ifp)
    706        1.1  drochner {
    707        1.1  drochner 	int error;
    708        1.1  drochner 
    709        1.1  drochner 	error = ieee80211_media_change(ifp);
    710        1.1  drochner 	if (error != ENETRESET)
    711        1.1  drochner 		return error;
    712        1.1  drochner 
    713        1.1  drochner 	if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING))
    714        1.1  drochner 		ural_init(ifp);
    715        1.1  drochner 
    716        1.1  drochner 	return 0;
    717        1.1  drochner }
    718        1.1  drochner 
    719        1.1  drochner /*
    720        1.1  drochner  * This function is called periodically (every 200ms) during scanning to
    721        1.1  drochner  * switch from one channel to another.
    722        1.1  drochner  */
    723        1.1  drochner Static void
    724        1.1  drochner ural_next_scan(void *arg)
    725        1.1  drochner {
    726        1.1  drochner 	struct ural_softc *sc = arg;
    727        1.1  drochner 	struct ieee80211com *ic = &sc->sc_ic;
    728        1.1  drochner 
    729        1.1  drochner 	if (ic->ic_state == IEEE80211_S_SCAN)
    730        1.1  drochner 		ieee80211_next_scan(ic);
    731        1.1  drochner }
    732        1.1  drochner 
    733        1.1  drochner Static void
    734        1.1  drochner ural_task(void *arg)
    735        1.1  drochner {
    736        1.1  drochner 	struct ural_softc *sc = arg;
    737        1.1  drochner 	struct ieee80211com *ic = &sc->sc_ic;
    738        1.1  drochner 	enum ieee80211_state ostate;
    739  1.11.10.1   gdamore 	struct ieee80211_node *ni;
    740        1.1  drochner 	struct mbuf *m;
    741        1.1  drochner 
    742        1.1  drochner 	ostate = ic->ic_state;
    743        1.1  drochner 
    744        1.1  drochner 	switch (sc->sc_state) {
    745        1.1  drochner 	case IEEE80211_S_INIT:
    746        1.1  drochner 		if (ostate == IEEE80211_S_RUN) {
    747        1.1  drochner 			/* abort TSF synchronization */
    748        1.1  drochner 			ural_write(sc, RAL_TXRX_CSR19, 0);
    749        1.1  drochner 
    750        1.1  drochner 			/* force tx led to stop blinking */
    751        1.1  drochner 			ural_write(sc, RAL_MAC_CSR20, 0);
    752        1.1  drochner 		}
    753        1.1  drochner 		break;
    754        1.1  drochner 
    755        1.1  drochner 	case IEEE80211_S_SCAN:
    756        1.9     skrll 		ural_set_chan(sc, ic->ic_curchan);
    757        1.1  drochner 		callout_reset(&sc->scan_ch, hz / 5, ural_next_scan, sc);
    758        1.1  drochner 		break;
    759        1.1  drochner 
    760        1.1  drochner 	case IEEE80211_S_AUTH:
    761        1.9     skrll 		ural_set_chan(sc, ic->ic_curchan);
    762        1.1  drochner 		break;
    763        1.1  drochner 
    764        1.1  drochner 	case IEEE80211_S_ASSOC:
    765        1.9     skrll 		ural_set_chan(sc, ic->ic_curchan);
    766        1.1  drochner 		break;
    767        1.1  drochner 
    768        1.1  drochner 	case IEEE80211_S_RUN:
    769        1.9     skrll 		ural_set_chan(sc, ic->ic_curchan);
    770        1.1  drochner 
    771  1.11.10.1   gdamore 		ni = ic->ic_bss;
    772  1.11.10.1   gdamore 
    773  1.11.10.1   gdamore 		if (ic->ic_opmode != IEEE80211_M_MONITOR) {
    774  1.11.10.1   gdamore 			ural_update_slot(ic->ic_ifp);
    775  1.11.10.1   gdamore 			ural_set_txpreamble(sc);
    776  1.11.10.1   gdamore 			ural_set_basicrates(sc);
    777  1.11.10.1   gdamore 			ural_set_bssid(sc, ni->ni_bssid);
    778  1.11.10.1   gdamore 		}
    779        1.1  drochner 
    780        1.1  drochner 		if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
    781        1.1  drochner 		    ic->ic_opmode == IEEE80211_M_IBSS) {
    782  1.11.10.1   gdamore 			m = ieee80211_beacon_alloc(ic, ni, &sc->sc_bo);
    783        1.1  drochner 			if (m == NULL) {
    784        1.1  drochner 				printf("%s: could not allocate beacon\n",
    785        1.1  drochner 				    USBDEVNAME(sc->sc_dev));
    786        1.1  drochner 				return;
    787        1.1  drochner 			}
    788        1.1  drochner 
    789  1.11.10.1   gdamore 			if (ural_tx_bcn(sc, m, ni) != 0) {
    790        1.1  drochner 				m_freem(m);
    791  1.11.10.1   gdamore 				printf("%s: could not send beacon\n",
    792        1.1  drochner 				    USBDEVNAME(sc->sc_dev));
    793        1.1  drochner 				return;
    794        1.1  drochner 			}
    795        1.1  drochner 
    796        1.1  drochner 			/* beacon is no longer needed */
    797        1.1  drochner 			m_freem(m);
    798        1.1  drochner 		}
    799        1.1  drochner 
    800        1.1  drochner 		/* make tx led blink on tx (controlled by ASIC) */
    801        1.1  drochner 		ural_write(sc, RAL_MAC_CSR20, 1);
    802        1.1  drochner 
    803        1.1  drochner 		if (ic->ic_opmode != IEEE80211_M_MONITOR)
    804        1.1  drochner 			ural_enable_tsf_sync(sc);
    805  1.11.10.1   gdamore 
    806  1.11.10.1   gdamore 		/* enable automatic rate adaptation in STA mode */
    807  1.11.10.1   gdamore 		if (ic->ic_opmode == IEEE80211_M_STA &&
    808  1.11.10.1   gdamore 		    ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE)
    809  1.11.10.1   gdamore 			ural_amrr_start(sc, ni);
    810  1.11.10.1   gdamore 
    811        1.1  drochner 		break;
    812        1.1  drochner 	}
    813        1.1  drochner 
    814        1.1  drochner 	sc->sc_newstate(ic, sc->sc_state, -1);
    815        1.1  drochner }
    816        1.1  drochner 
    817        1.1  drochner Static int
    818        1.1  drochner ural_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
    819        1.1  drochner {
    820        1.1  drochner 	struct ural_softc *sc = ic->ic_ifp->if_softc;
    821        1.1  drochner 
    822        1.1  drochner 	usb_rem_task(sc->sc_udev, &sc->sc_task);
    823        1.1  drochner 	callout_stop(&sc->scan_ch);
    824  1.11.10.1   gdamore 	callout_stop(&sc->amrr_ch);
    825        1.1  drochner 
    826        1.1  drochner 	/* do it in a process context */
    827        1.1  drochner 	sc->sc_state = nstate;
    828        1.1  drochner 	usb_add_task(sc->sc_udev, &sc->sc_task);
    829        1.1  drochner 
    830        1.1  drochner 	return 0;
    831        1.1  drochner }
    832        1.1  drochner 
    833        1.1  drochner /* quickly determine if a given rate is CCK or OFDM */
    834        1.1  drochner #define RAL_RATE_IS_OFDM(rate) ((rate) >= 12 && (rate) != 22)
    835        1.1  drochner 
    836        1.1  drochner #define RAL_ACK_SIZE	14	/* 10 + 4(FCS) */
    837        1.1  drochner #define RAL_CTS_SIZE	14	/* 10 + 4(FCS) */
    838  1.11.10.1   gdamore 
    839  1.11.10.1   gdamore #define RAL_SIFS		10	/* us */
    840  1.11.10.1   gdamore 
    841  1.11.10.1   gdamore #define RAL_RXTX_TURNAROUND	5	/* us */
    842  1.11.10.1   gdamore 
    843  1.11.10.1   gdamore /*
    844  1.11.10.1   gdamore  * This function is only used by the Rx radiotap code.
    845  1.11.10.1   gdamore  */
    846  1.11.10.1   gdamore Static int
    847  1.11.10.1   gdamore ural_rxrate(struct ural_rx_desc *desc)
    848  1.11.10.1   gdamore {
    849  1.11.10.1   gdamore 	if (le32toh(desc->flags) & RAL_RX_OFDM) {
    850  1.11.10.1   gdamore 		/* reverse function of ural_plcp_signal */
    851  1.11.10.1   gdamore 		switch (desc->rate) {
    852  1.11.10.1   gdamore 		case 0xb:	return 12;
    853  1.11.10.1   gdamore 		case 0xf:	return 18;
    854  1.11.10.1   gdamore 		case 0xa:	return 24;
    855  1.11.10.1   gdamore 		case 0xe:	return 36;
    856  1.11.10.1   gdamore 		case 0x9:	return 48;
    857  1.11.10.1   gdamore 		case 0xd:	return 72;
    858  1.11.10.1   gdamore 		case 0x8:	return 96;
    859  1.11.10.1   gdamore 		case 0xc:	return 108;
    860  1.11.10.1   gdamore 		}
    861  1.11.10.1   gdamore 	} else {
    862  1.11.10.1   gdamore 		if (desc->rate == 10)
    863  1.11.10.1   gdamore 			return 2;
    864  1.11.10.1   gdamore 		if (desc->rate == 20)
    865  1.11.10.1   gdamore 			return 4;
    866  1.11.10.1   gdamore 		if (desc->rate == 55)
    867  1.11.10.1   gdamore 			return 11;
    868  1.11.10.1   gdamore 		if (desc->rate == 110)
    869  1.11.10.1   gdamore 			return 22;
    870  1.11.10.1   gdamore 	}
    871  1.11.10.1   gdamore 	return 2;	/* should not get there */
    872  1.11.10.1   gdamore }
    873        1.1  drochner 
    874        1.1  drochner Static void
    875        1.1  drochner ural_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
    876        1.1  drochner {
    877        1.1  drochner 	struct ural_tx_data *data = priv;
    878        1.1  drochner 	struct ural_softc *sc = data->sc;
    879        1.2  drochner 	struct ifnet *ifp = &sc->sc_if;
    880        1.1  drochner 	int s;
    881        1.1  drochner 
    882        1.1  drochner 	if (status != USBD_NORMAL_COMPLETION) {
    883        1.1  drochner 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
    884        1.1  drochner 			return;
    885        1.1  drochner 
    886        1.1  drochner 		printf("%s: could not transmit buffer: %s\n",
    887        1.1  drochner 		    USBDEVNAME(sc->sc_dev), usbd_errstr(status));
    888        1.1  drochner 
    889        1.1  drochner 		if (status == USBD_STALLED)
    890       1.10  augustss 			usbd_clear_endpoint_stall_async(sc->sc_tx_pipeh);
    891        1.1  drochner 
    892        1.1  drochner 		ifp->if_oerrors++;
    893        1.1  drochner 		return;
    894        1.1  drochner 	}
    895        1.1  drochner 
    896        1.1  drochner 	s = splnet();
    897        1.1  drochner 
    898        1.1  drochner 	m_freem(data->m);
    899        1.1  drochner 	data->m = NULL;
    900        1.1  drochner 	ieee80211_free_node(data->ni);
    901        1.1  drochner 	data->ni = NULL;
    902        1.1  drochner 
    903        1.1  drochner 	sc->tx_queued--;
    904        1.1  drochner 	ifp->if_opackets++;
    905        1.1  drochner 
    906        1.1  drochner 	DPRINTFN(10, ("tx done\n"));
    907        1.1  drochner 
    908        1.1  drochner 	sc->sc_tx_timer = 0;
    909        1.1  drochner 	ifp->if_flags &= ~IFF_OACTIVE;
    910        1.1  drochner 	ural_start(ifp);
    911        1.1  drochner 
    912        1.1  drochner 	splx(s);
    913        1.1  drochner }
    914        1.1  drochner 
    915        1.1  drochner Static void
    916        1.1  drochner ural_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
    917        1.1  drochner {
    918        1.1  drochner 	struct ural_rx_data *data = priv;
    919        1.1  drochner 	struct ural_softc *sc = data->sc;
    920        1.1  drochner 	struct ieee80211com *ic = &sc->sc_ic;
    921        1.2  drochner 	struct ifnet *ifp = &sc->sc_if;
    922        1.1  drochner 	struct ural_rx_desc *desc;
    923  1.11.10.1   gdamore 	struct ieee80211_frame *wh;
    924        1.1  drochner 	struct ieee80211_node *ni;
    925  1.11.10.1   gdamore 	struct mbuf *mnew, *m;
    926        1.1  drochner 	int s, len;
    927        1.1  drochner 
    928        1.1  drochner 	if (status != USBD_NORMAL_COMPLETION) {
    929        1.1  drochner 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
    930        1.1  drochner 			return;
    931        1.1  drochner 
    932        1.1  drochner 		if (status == USBD_STALLED)
    933       1.10  augustss 			usbd_clear_endpoint_stall_async(sc->sc_rx_pipeh);
    934        1.1  drochner 		goto skip;
    935        1.1  drochner 	}
    936        1.1  drochner 
    937        1.1  drochner 	usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL);
    938        1.1  drochner 
    939  1.11.10.1   gdamore 	if (len < RAL_RX_DESC_SIZE + IEEE80211_MIN_LEN) {
    940  1.11.10.1   gdamore 		DPRINTF(("%s: xfer too short %d\n", USBDEVNAME(sc->sc_dev),
    941  1.11.10.1   gdamore 		    len));
    942        1.1  drochner 		ifp->if_ierrors++;
    943        1.1  drochner 		goto skip;
    944        1.1  drochner 	}
    945        1.1  drochner 
    946        1.1  drochner 	/* rx descriptor is located at the end */
    947        1.1  drochner 	desc = (struct ural_rx_desc *)(data->buf + len - RAL_RX_DESC_SIZE);
    948        1.1  drochner 
    949  1.11.10.1   gdamore 	if ((le32toh(desc->flags) & RAL_RX_PHY_ERROR) ||
    950  1.11.10.1   gdamore 	    (le32toh(desc->flags) & RAL_RX_CRC_ERROR)) {
    951        1.1  drochner 		/*
    952        1.1  drochner 		 * This should not happen since we did not request to receive
    953        1.1  drochner 		 * those frames when we filled RAL_TXRX_CSR2.
    954        1.1  drochner 		 */
    955        1.1  drochner 		DPRINTFN(5, ("PHY or CRC error\n"));
    956        1.1  drochner 		ifp->if_ierrors++;
    957        1.1  drochner 		goto skip;
    958        1.1  drochner 	}
    959        1.1  drochner 
    960  1.11.10.1   gdamore 	MGETHDR(mnew, M_DONTWAIT, MT_DATA);
    961  1.11.10.1   gdamore 	if (mnew == NULL) {
    962  1.11.10.1   gdamore 		ifp->if_ierrors++;
    963  1.11.10.1   gdamore 		goto skip;
    964  1.11.10.1   gdamore 	}
    965  1.11.10.1   gdamore 
    966  1.11.10.1   gdamore 	MCLGET(mnew, M_DONTWAIT);
    967  1.11.10.1   gdamore 	if (!(mnew->m_flags & M_EXT)) {
    968  1.11.10.1   gdamore 		ifp->if_ierrors++;
    969  1.11.10.1   gdamore 		m_freem(mnew);
    970  1.11.10.1   gdamore 		goto skip;
    971  1.11.10.1   gdamore 	}
    972  1.11.10.1   gdamore 
    973        1.1  drochner 	m = data->m;
    974  1.11.10.1   gdamore 	data->m = mnew;
    975  1.11.10.1   gdamore 	data->buf = mtod(data->m, uint8_t *);
    976  1.11.10.1   gdamore 
    977  1.11.10.1   gdamore 	/* finalize mbuf */
    978        1.1  drochner 	m->m_pkthdr.rcvif = ifp;
    979        1.1  drochner 	m->m_pkthdr.len = m->m_len = (le32toh(desc->flags) >> 16) & 0xfff;
    980  1.11.10.1   gdamore 	m->m_flags |= M_HASFCS;	/* h/w leaves FCS */
    981        1.1  drochner 
    982        1.1  drochner 	s = splnet();
    983        1.1  drochner 
    984        1.1  drochner #if NBPFILTER > 0
    985        1.1  drochner 	if (sc->sc_drvbpf != NULL) {
    986        1.1  drochner 		struct ural_rx_radiotap_header *tap = &sc->sc_rxtap;
    987        1.1  drochner 
    988        1.5  drochner 		tap->wr_flags = IEEE80211_RADIOTAP_F_FCS;
    989  1.11.10.1   gdamore 		tap->wr_rate = ural_rxrate(desc);
    990  1.11.10.1   gdamore 		tap->wr_chan_freq = htole16(ic->ic_curchan->ic_freq);
    991  1.11.10.1   gdamore 		tap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
    992        1.1  drochner 		tap->wr_antenna = sc->rx_ant;
    993        1.1  drochner 		tap->wr_antsignal = desc->rssi;
    994        1.1  drochner 
    995        1.1  drochner 		bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_rxtap_len, m);
    996        1.1  drochner 	}
    997        1.1  drochner #endif
    998        1.1  drochner 
    999  1.11.10.1   gdamore 	wh = mtod(m, struct ieee80211_frame *);
   1000  1.11.10.1   gdamore 	ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *)wh);
   1001        1.1  drochner 
   1002        1.1  drochner 	/* send the frame to the 802.11 layer */
   1003        1.1  drochner 	ieee80211_input(ic, m, ni, desc->rssi, 0);
   1004        1.1  drochner 
   1005        1.1  drochner 	/* node is no longer needed */
   1006        1.1  drochner 	ieee80211_free_node(ni);
   1007        1.1  drochner 
   1008        1.1  drochner 	splx(s);
   1009        1.1  drochner 
   1010        1.1  drochner 	DPRINTFN(15, ("rx done\n"));
   1011        1.1  drochner 
   1012        1.1  drochner skip:	/* setup a new transfer */
   1013        1.1  drochner 	usbd_setup_xfer(xfer, sc->sc_rx_pipeh, data, data->buf, MCLBYTES,
   1014        1.1  drochner 	    USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, ural_rxeof);
   1015        1.1  drochner 	usbd_transfer(xfer);
   1016        1.1  drochner }
   1017        1.1  drochner 
   1018        1.1  drochner /*
   1019        1.1  drochner  * Return the expected ack rate for a frame transmitted at rate `rate'.
   1020        1.1  drochner  * XXX: this should depend on the destination node basic rate set.
   1021        1.1  drochner  */
   1022        1.1  drochner Static int
   1023  1.11.10.1   gdamore ural_ack_rate(struct ieee80211com *ic, int rate)
   1024        1.1  drochner {
   1025        1.1  drochner 	switch (rate) {
   1026        1.1  drochner 	/* CCK rates */
   1027        1.1  drochner 	case 2:
   1028        1.1  drochner 		return 2;
   1029        1.1  drochner 	case 4:
   1030        1.1  drochner 	case 11:
   1031        1.1  drochner 	case 22:
   1032  1.11.10.1   gdamore 		return (ic->ic_curmode == IEEE80211_MODE_11B) ? 4 : rate;
   1033        1.1  drochner 
   1034        1.1  drochner 	/* OFDM rates */
   1035        1.1  drochner 	case 12:
   1036        1.1  drochner 	case 18:
   1037        1.1  drochner 		return 12;
   1038        1.1  drochner 	case 24:
   1039        1.1  drochner 	case 36:
   1040        1.1  drochner 		return 24;
   1041        1.1  drochner 	case 48:
   1042        1.1  drochner 	case 72:
   1043        1.1  drochner 	case 96:
   1044        1.1  drochner 	case 108:
   1045        1.1  drochner 		return 48;
   1046        1.1  drochner 	}
   1047        1.1  drochner 
   1048        1.1  drochner 	/* default to 1Mbps */
   1049        1.1  drochner 	return 2;
   1050        1.1  drochner }
   1051        1.1  drochner 
   1052        1.1  drochner /*
   1053        1.1  drochner  * Compute the duration (in us) needed to transmit `len' bytes at rate `rate'.
   1054        1.1  drochner  * The function automatically determines the operating mode depending on the
   1055        1.1  drochner  * given rate. `flags' indicates whether short preamble is in use or not.
   1056        1.1  drochner  */
   1057        1.1  drochner Static uint16_t
   1058        1.1  drochner ural_txtime(int len, int rate, uint32_t flags)
   1059        1.1  drochner {
   1060        1.1  drochner 	uint16_t txtime;
   1061        1.1  drochner 
   1062        1.1  drochner 	if (RAL_RATE_IS_OFDM(rate)) {
   1063  1.11.10.1   gdamore 		/* IEEE Std 802.11a-1999, pp. 37 */
   1064  1.11.10.1   gdamore 		txtime = (8 + 4 * len + 3 + rate - 1) / rate;
   1065  1.11.10.1   gdamore 		txtime = 16 + 4 + 4 * txtime + 6;
   1066        1.1  drochner 	} else {
   1067  1.11.10.1   gdamore 		/* IEEE Std 802.11b-1999, pp. 28 */
   1068  1.11.10.1   gdamore 		txtime = (16 * len + rate - 1) / rate;
   1069        1.1  drochner 		if (rate != 2 && (flags & IEEE80211_F_SHPREAMBLE))
   1070  1.11.10.1   gdamore 			txtime +=  72 + 24;
   1071        1.1  drochner 		else
   1072  1.11.10.1   gdamore 			txtime += 144 + 48;
   1073        1.1  drochner 	}
   1074        1.1  drochner 	return txtime;
   1075        1.1  drochner }
   1076        1.1  drochner 
   1077        1.1  drochner Static uint8_t
   1078        1.1  drochner ural_plcp_signal(int rate)
   1079        1.1  drochner {
   1080        1.1  drochner 	switch (rate) {
   1081        1.1  drochner 	/* CCK rates (returned values are device-dependent) */
   1082        1.1  drochner 	case 2:		return 0x0;
   1083        1.1  drochner 	case 4:		return 0x1;
   1084        1.1  drochner 	case 11:	return 0x2;
   1085        1.1  drochner 	case 22:	return 0x3;
   1086        1.1  drochner 
   1087        1.1  drochner 	/* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
   1088        1.1  drochner 	case 12:	return 0xb;
   1089        1.1  drochner 	case 18:	return 0xf;
   1090        1.1  drochner 	case 24:	return 0xa;
   1091        1.1  drochner 	case 36:	return 0xe;
   1092        1.1  drochner 	case 48:	return 0x9;
   1093        1.1  drochner 	case 72:	return 0xd;
   1094        1.1  drochner 	case 96:	return 0x8;
   1095        1.1  drochner 	case 108:	return 0xc;
   1096        1.1  drochner 
   1097        1.1  drochner 	/* unsupported rates (should not get there) */
   1098        1.1  drochner 	default:	return 0xff;
   1099        1.1  drochner 	}
   1100        1.1  drochner }
   1101        1.1  drochner 
   1102        1.1  drochner Static void
   1103        1.1  drochner ural_setup_tx_desc(struct ural_softc *sc, struct ural_tx_desc *desc,
   1104        1.1  drochner     uint32_t flags, int len, int rate)
   1105        1.1  drochner {
   1106        1.1  drochner 	struct ieee80211com *ic = &sc->sc_ic;
   1107        1.1  drochner 	uint16_t plcp_length;
   1108        1.1  drochner 	int remainder;
   1109        1.1  drochner 
   1110        1.1  drochner 	desc->flags = htole32(flags);
   1111        1.1  drochner 	desc->flags |= htole32(RAL_TX_NEWSEQ);
   1112        1.1  drochner 	desc->flags |= htole32(len << 16);
   1113        1.1  drochner 
   1114  1.11.10.1   gdamore 	desc->wme = htole16(RAL_AIFSN(2) | RAL_LOGCWMIN(3) | RAL_LOGCWMAX(5));
   1115        1.4  drochner 	desc->wme |= htole16(RAL_IVOFFSET(sizeof (struct ieee80211_frame)));
   1116        1.1  drochner 
   1117  1.11.10.1   gdamore 	/* setup PLCP fields */
   1118  1.11.10.1   gdamore 	desc->plcp_signal  = ural_plcp_signal(rate);
   1119        1.1  drochner 	desc->plcp_service = 4;
   1120        1.1  drochner 
   1121  1.11.10.1   gdamore 	len += IEEE80211_CRC_LEN;
   1122        1.1  drochner 	if (RAL_RATE_IS_OFDM(rate)) {
   1123  1.11.10.1   gdamore 		desc->flags |= htole32(RAL_TX_OFDM);
   1124  1.11.10.1   gdamore 
   1125        1.1  drochner 		plcp_length = len & 0xfff;
   1126  1.11.10.1   gdamore 		desc->plcp_length_hi = plcp_length >> 6;
   1127  1.11.10.1   gdamore 		desc->plcp_length_lo = plcp_length & 0x3f;
   1128        1.1  drochner 	} else {
   1129  1.11.10.1   gdamore 		plcp_length = (16 * len + rate - 1) / rate;
   1130  1.11.10.1   gdamore 		if (rate == 22) {
   1131  1.11.10.1   gdamore 			remainder = (16 * len) % 22;
   1132  1.11.10.1   gdamore 			if (remainder != 0 && remainder < 7)
   1133        1.1  drochner 				desc->plcp_service |= RAL_PLCP_LENGEXT;
   1134        1.1  drochner 		}
   1135  1.11.10.1   gdamore 		desc->plcp_length_hi = plcp_length >> 8;
   1136  1.11.10.1   gdamore 		desc->plcp_length_lo = plcp_length & 0xff;
   1137        1.1  drochner 
   1138  1.11.10.1   gdamore 		if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
   1139  1.11.10.1   gdamore 			desc->plcp_signal |= 0x08;
   1140  1.11.10.1   gdamore 	}
   1141        1.1  drochner 
   1142        1.1  drochner 	desc->iv = 0;
   1143        1.1  drochner 	desc->eiv = 0;
   1144        1.1  drochner }
   1145        1.1  drochner 
   1146        1.1  drochner #define RAL_TX_TIMEOUT	5000
   1147        1.1  drochner 
   1148        1.1  drochner Static int
   1149        1.1  drochner ural_tx_bcn(struct ural_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
   1150        1.1  drochner {
   1151        1.1  drochner 	struct ural_tx_desc *desc;
   1152        1.1  drochner 	usbd_xfer_handle xfer;
   1153        1.1  drochner 	uint8_t cmd = 0;
   1154  1.11.10.1   gdamore 	usbd_status error;
   1155        1.1  drochner 	uint8_t *buf;
   1156        1.1  drochner 	int xferlen, rate;
   1157        1.1  drochner 
   1158  1.11.10.1   gdamore 	rate = IEEE80211_IS_CHAN_5GHZ(ni->ni_chan) ? 12 : 2;
   1159        1.1  drochner 
   1160        1.1  drochner 	xfer = usbd_alloc_xfer(sc->sc_udev);
   1161        1.1  drochner 	if (xfer == NULL)
   1162        1.1  drochner 		return ENOMEM;
   1163        1.1  drochner 
   1164        1.1  drochner 	/* xfer length needs to be a multiple of two! */
   1165        1.1  drochner 	xferlen = (RAL_TX_DESC_SIZE + m0->m_pkthdr.len + 1) & ~1;
   1166        1.1  drochner 
   1167        1.1  drochner 	buf = usbd_alloc_buffer(xfer, xferlen);
   1168        1.1  drochner 	if (buf == NULL) {
   1169        1.1  drochner 		usbd_free_xfer(xfer);
   1170        1.1  drochner 		return ENOMEM;
   1171        1.1  drochner 	}
   1172        1.1  drochner 
   1173        1.1  drochner 	usbd_setup_xfer(xfer, sc->sc_tx_pipeh, NULL, &cmd, sizeof cmd,
   1174        1.1  drochner 	    USBD_FORCE_SHORT_XFER, RAL_TX_TIMEOUT, NULL);
   1175        1.1  drochner 
   1176        1.1  drochner 	error = usbd_sync_transfer(xfer);
   1177        1.1  drochner 	if (error != 0) {
   1178        1.1  drochner 		usbd_free_xfer(xfer);
   1179        1.1  drochner 		return error;
   1180        1.1  drochner 	}
   1181        1.1  drochner 
   1182        1.1  drochner 	desc = (struct ural_tx_desc *)buf;
   1183        1.1  drochner 
   1184        1.1  drochner 	m_copydata(m0, 0, m0->m_pkthdr.len, buf + RAL_TX_DESC_SIZE);
   1185        1.1  drochner 	ural_setup_tx_desc(sc, desc, RAL_TX_IFS_NEWBACKOFF | RAL_TX_TIMESTAMP,
   1186        1.1  drochner 	    m0->m_pkthdr.len, rate);
   1187        1.1  drochner 
   1188        1.1  drochner 	DPRINTFN(10, ("sending beacon frame len=%u rate=%u xfer len=%u\n",
   1189        1.1  drochner 	    m0->m_pkthdr.len, rate, xferlen));
   1190        1.1  drochner 
   1191        1.1  drochner 	usbd_setup_xfer(xfer, sc->sc_tx_pipeh, NULL, buf, xferlen,
   1192        1.1  drochner 	    USBD_FORCE_SHORT_XFER | USBD_NO_COPY, RAL_TX_TIMEOUT, NULL);
   1193        1.1  drochner 
   1194        1.1  drochner 	error = usbd_sync_transfer(xfer);
   1195        1.1  drochner 	usbd_free_xfer(xfer);
   1196        1.1  drochner 
   1197        1.1  drochner 	return error;
   1198        1.1  drochner }
   1199        1.1  drochner 
   1200        1.1  drochner Static int
   1201        1.1  drochner ural_tx_mgt(struct ural_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
   1202        1.1  drochner {
   1203        1.1  drochner 	struct ieee80211com *ic = &sc->sc_ic;
   1204        1.1  drochner 	struct ural_tx_desc *desc;
   1205        1.1  drochner 	struct ural_tx_data *data;
   1206        1.1  drochner 	struct ieee80211_frame *wh;
   1207        1.1  drochner 	uint32_t flags = 0;
   1208        1.1  drochner 	uint16_t dur;
   1209        1.1  drochner 	usbd_status error;
   1210        1.1  drochner 	int xferlen, rate;
   1211        1.1  drochner 
   1212        1.1  drochner 	data = &sc->tx_data[0];
   1213        1.1  drochner 	desc = (struct ural_tx_desc *)data->buf;
   1214        1.1  drochner 
   1215  1.11.10.1   gdamore 	rate = IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan) ? 12 : 2;
   1216  1.11.10.1   gdamore 
   1217        1.1  drochner 	data->m = m0;
   1218        1.1  drochner 	data->ni = ni;
   1219        1.1  drochner 
   1220        1.1  drochner 	wh = mtod(m0, struct ieee80211_frame *);
   1221        1.1  drochner 
   1222        1.1  drochner 	if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
   1223        1.1  drochner 		flags |= RAL_TX_ACK;
   1224        1.1  drochner 
   1225        1.1  drochner 		dur = ural_txtime(RAL_ACK_SIZE, rate, ic->ic_flags) + RAL_SIFS;
   1226        1.1  drochner 		*(uint16_t *)wh->i_dur = htole16(dur);
   1227        1.1  drochner 
   1228        1.1  drochner 		/* tell hardware to add timestamp for probe responses */
   1229  1.11.10.1   gdamore 		if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
   1230  1.11.10.1   gdamore 		    IEEE80211_FC0_TYPE_MGT &&
   1231  1.11.10.1   gdamore 		    (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) ==
   1232  1.11.10.1   gdamore 		    IEEE80211_FC0_SUBTYPE_PROBE_RESP)
   1233        1.1  drochner 			flags |= RAL_TX_TIMESTAMP;
   1234        1.1  drochner 	}
   1235        1.1  drochner 
   1236        1.7  drochner #if NBPFILTER > 0
   1237        1.7  drochner 	if (sc->sc_drvbpf != NULL) {
   1238        1.7  drochner 		struct ural_tx_radiotap_header *tap = &sc->sc_txtap;
   1239        1.7  drochner 
   1240        1.7  drochner 		tap->wt_flags = 0;
   1241        1.7  drochner 		tap->wt_rate = rate;
   1242        1.9     skrll 		tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
   1243        1.9     skrll 		tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
   1244        1.7  drochner 		tap->wt_antenna = sc->tx_ant;
   1245        1.7  drochner 
   1246        1.7  drochner 		bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0);
   1247        1.7  drochner 	}
   1248        1.7  drochner #endif
   1249        1.7  drochner 
   1250        1.1  drochner 	m_copydata(m0, 0, m0->m_pkthdr.len, data->buf + RAL_TX_DESC_SIZE);
   1251        1.1  drochner 	ural_setup_tx_desc(sc, desc, flags, m0->m_pkthdr.len, rate);
   1252        1.1  drochner 
   1253  1.11.10.1   gdamore 	/* align end on a 2-bytes boundary */
   1254        1.1  drochner 	xferlen = (RAL_TX_DESC_SIZE + m0->m_pkthdr.len + 1) & ~1;
   1255        1.1  drochner 
   1256  1.11.10.1   gdamore 	/*
   1257  1.11.10.1   gdamore 	 * No space left in the last URB to store the extra 2 bytes, force
   1258  1.11.10.1   gdamore 	 * sending of another URB.
   1259  1.11.10.1   gdamore 	 */
   1260  1.11.10.1   gdamore 	if ((xferlen % 64) == 0)
   1261  1.11.10.1   gdamore 		xferlen += 2;
   1262  1.11.10.1   gdamore 
   1263        1.1  drochner 	DPRINTFN(10, ("sending mgt frame len=%u rate=%u xfer len=%u\n",
   1264        1.1  drochner 	    m0->m_pkthdr.len, rate, xferlen));
   1265        1.1  drochner 
   1266  1.11.10.1   gdamore 	usbd_setup_xfer(data->xfer, sc->sc_tx_pipeh, data, data->buf,
   1267  1.11.10.1   gdamore 	    xferlen, USBD_FORCE_SHORT_XFER | USBD_NO_COPY, RAL_TX_TIMEOUT,
   1268  1.11.10.1   gdamore 	    ural_txeof);
   1269        1.1  drochner 
   1270        1.1  drochner 	error = usbd_transfer(data->xfer);
   1271  1.11.10.1   gdamore 	if (error != USBD_NORMAL_COMPLETION && error != USBD_IN_PROGRESS)
   1272        1.1  drochner 		return error;
   1273        1.1  drochner 
   1274        1.1  drochner 	sc->tx_queued++;
   1275        1.1  drochner 
   1276        1.1  drochner 	return 0;
   1277        1.1  drochner }
   1278        1.1  drochner 
   1279        1.1  drochner Static int
   1280        1.1  drochner ural_tx_data(struct ural_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
   1281        1.1  drochner {
   1282        1.1  drochner 	struct ieee80211com *ic = &sc->sc_ic;
   1283        1.1  drochner 	struct ural_tx_desc *desc;
   1284        1.1  drochner 	struct ural_tx_data *data;
   1285        1.1  drochner 	struct ieee80211_frame *wh;
   1286        1.1  drochner 	struct ieee80211_key *k;
   1287        1.1  drochner 	uint32_t flags = 0;
   1288        1.1  drochner 	uint16_t dur;
   1289        1.1  drochner 	usbd_status error;
   1290        1.1  drochner 	int xferlen, rate;
   1291        1.1  drochner 
   1292        1.2  drochner 	wh = mtod(m0, struct ieee80211_frame *);
   1293        1.2  drochner 
   1294  1.11.10.1   gdamore 	if (ic->ic_fixed_rate != IEEE80211_FIXED_RATE_NONE)
   1295  1.11.10.1   gdamore 		rate = ic->ic_bss->ni_rates.rs_rates[ic->ic_fixed_rate];
   1296  1.11.10.1   gdamore 	else
   1297  1.11.10.1   gdamore 		rate = ni->ni_rates.rs_rates[ni->ni_txrate];
   1298        1.1  drochner 
   1299        1.1  drochner 	rate &= IEEE80211_RATE_VAL;
   1300        1.1  drochner 
   1301        1.2  drochner 	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
   1302        1.1  drochner 		k = ieee80211_crypto_encap(ic, ni, m0);
   1303        1.3    dyoung 		if (k == NULL) {
   1304        1.3    dyoung 			m_freem(m0);
   1305        1.1  drochner 			return ENOBUFS;
   1306        1.3    dyoung 		}
   1307        1.2  drochner 
   1308        1.2  drochner 		/* packet header may have moved, reset our local pointer */
   1309        1.2  drochner 		wh = mtod(m0, struct ieee80211_frame *);
   1310        1.1  drochner 	}
   1311        1.1  drochner 
   1312        1.1  drochner 	data = &sc->tx_data[0];
   1313        1.1  drochner 	desc = (struct ural_tx_desc *)data->buf;
   1314        1.1  drochner 
   1315        1.1  drochner 	data->m = m0;
   1316        1.1  drochner 	data->ni = ni;
   1317        1.1  drochner 
   1318        1.1  drochner 	if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
   1319        1.1  drochner 		flags |= RAL_TX_ACK;
   1320        1.1  drochner 		flags |= RAL_TX_RETRY(7);
   1321        1.1  drochner 
   1322  1.11.10.1   gdamore 		dur = ural_txtime(RAL_ACK_SIZE, ural_ack_rate(ic, rate),
   1323        1.1  drochner 		    ic->ic_flags) + RAL_SIFS;
   1324        1.1  drochner 		*(uint16_t *)wh->i_dur = htole16(dur);
   1325        1.1  drochner 	}
   1326        1.1  drochner 
   1327  1.11.10.1   gdamore #if NBPFILTER > 0
   1328  1.11.10.1   gdamore 	if (sc->sc_drvbpf != NULL) {
   1329  1.11.10.1   gdamore 		struct ural_tx_radiotap_header *tap = &sc->sc_txtap;
   1330  1.11.10.1   gdamore 
   1331  1.11.10.1   gdamore 		tap->wt_flags = 0;
   1332  1.11.10.1   gdamore 		tap->wt_rate = rate;
   1333  1.11.10.1   gdamore 		tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
   1334  1.11.10.1   gdamore 		tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
   1335  1.11.10.1   gdamore 		tap->wt_antenna = sc->tx_ant;
   1336  1.11.10.1   gdamore 
   1337  1.11.10.1   gdamore 		bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0);
   1338  1.11.10.1   gdamore 	}
   1339  1.11.10.1   gdamore #endif
   1340  1.11.10.1   gdamore 
   1341        1.1  drochner 	m_copydata(m0, 0, m0->m_pkthdr.len, data->buf + RAL_TX_DESC_SIZE);
   1342        1.1  drochner 	ural_setup_tx_desc(sc, desc, flags, m0->m_pkthdr.len, rate);
   1343        1.1  drochner 
   1344  1.11.10.1   gdamore 	/* align end on a 2-bytes boundary */
   1345        1.1  drochner 	xferlen = (RAL_TX_DESC_SIZE + m0->m_pkthdr.len + 1) & ~1;
   1346        1.1  drochner 
   1347  1.11.10.1   gdamore 	/*
   1348  1.11.10.1   gdamore 	 * No space left in the last URB to store the extra 2 bytes, force
   1349  1.11.10.1   gdamore 	 * sending of another URB.
   1350  1.11.10.1   gdamore 	 */
   1351  1.11.10.1   gdamore 	if ((xferlen % 64) == 0)
   1352  1.11.10.1   gdamore 		xferlen += 2;
   1353  1.11.10.1   gdamore 
   1354        1.1  drochner 	DPRINTFN(10, ("sending data frame len=%u rate=%u xfer len=%u\n",
   1355        1.1  drochner 	    m0->m_pkthdr.len, rate, xferlen));
   1356        1.1  drochner 
   1357  1.11.10.1   gdamore 	usbd_setup_xfer(data->xfer, sc->sc_tx_pipeh, data, data->buf,
   1358  1.11.10.1   gdamore 	    xferlen, USBD_FORCE_SHORT_XFER | USBD_NO_COPY, RAL_TX_TIMEOUT,
   1359  1.11.10.1   gdamore 	    ural_txeof);
   1360        1.1  drochner 
   1361        1.1  drochner 	error = usbd_transfer(data->xfer);
   1362  1.11.10.1   gdamore 	if (error != USBD_NORMAL_COMPLETION && error != USBD_IN_PROGRESS)
   1363        1.1  drochner 		return error;
   1364        1.1  drochner 
   1365        1.1  drochner 	sc->tx_queued++;
   1366        1.1  drochner 
   1367        1.1  drochner 	return 0;
   1368        1.1  drochner }
   1369        1.1  drochner 
   1370        1.1  drochner Static void
   1371        1.1  drochner ural_start(struct ifnet *ifp)
   1372        1.1  drochner {
   1373        1.1  drochner 	struct ural_softc *sc = ifp->if_softc;
   1374        1.1  drochner 	struct ieee80211com *ic = &sc->sc_ic;
   1375  1.11.10.1   gdamore 	struct mbuf *m0;
   1376        1.1  drochner 	struct ether_header *eh;
   1377        1.1  drochner 	struct ieee80211_node *ni;
   1378        1.1  drochner 
   1379        1.1  drochner 	for (;;) {
   1380        1.1  drochner 		IF_POLL(&ic->ic_mgtq, m0);
   1381        1.1  drochner 		if (m0 != NULL) {
   1382        1.1  drochner 			if (sc->tx_queued >= RAL_TX_LIST_COUNT) {
   1383        1.1  drochner 				ifp->if_flags |= IFF_OACTIVE;
   1384        1.1  drochner 				break;
   1385        1.1  drochner 			}
   1386        1.1  drochner 			IF_DEQUEUE(&ic->ic_mgtq, m0);
   1387        1.1  drochner 
   1388        1.1  drochner 			ni = (struct ieee80211_node *)m0->m_pkthdr.rcvif;
   1389        1.1  drochner 			m0->m_pkthdr.rcvif = NULL;
   1390        1.1  drochner #if NBPFILTER > 0
   1391        1.1  drochner 			if (ic->ic_rawbpf != NULL)
   1392        1.1  drochner 				bpf_mtap(ic->ic_rawbpf, m0);
   1393        1.1  drochner #endif
   1394        1.1  drochner 			if (ural_tx_mgt(sc, m0, ni) != 0)
   1395        1.1  drochner 				break;
   1396        1.1  drochner 
   1397        1.1  drochner 		} else {
   1398        1.1  drochner 			if (ic->ic_state != IEEE80211_S_RUN)
   1399        1.1  drochner 				break;
   1400        1.1  drochner 			IFQ_DEQUEUE(&ifp->if_snd, m0);
   1401        1.1  drochner 			if (m0 == NULL)
   1402        1.1  drochner 				break;
   1403        1.1  drochner 			if (sc->tx_queued >= RAL_TX_LIST_COUNT) {
   1404        1.1  drochner 				IF_PREPEND(&ifp->if_snd, m0);
   1405        1.1  drochner 				ifp->if_flags |= IFF_OACTIVE;
   1406        1.1  drochner 				break;
   1407        1.1  drochner 			}
   1408        1.1  drochner 
   1409        1.2  drochner 			if (m0->m_len < sizeof (struct ether_header) &&
   1410        1.2  drochner 			    !(m0 = m_pullup(m0, sizeof (struct ether_header))))
   1411        1.1  drochner 				continue;
   1412        1.1  drochner 
   1413        1.1  drochner 			eh = mtod(m0, struct ether_header *);
   1414        1.1  drochner 			ni = ieee80211_find_txnode(ic, eh->ether_dhost);
   1415        1.1  drochner 			if (ni == NULL) {
   1416        1.1  drochner 				m_freem(m0);
   1417        1.1  drochner 				continue;
   1418        1.1  drochner 			}
   1419        1.1  drochner #if NBPFILTER > 0
   1420        1.1  drochner 			if (ifp->if_bpf != NULL)
   1421        1.1  drochner 				bpf_mtap(ifp->if_bpf, m0);
   1422        1.1  drochner #endif
   1423        1.1  drochner 			m0 = ieee80211_encap(ic, m0, ni);
   1424        1.4  drochner 			if (m0 == NULL) {
   1425        1.4  drochner 				ieee80211_free_node(ni);
   1426        1.1  drochner 				continue;
   1427        1.4  drochner 			}
   1428        1.1  drochner #if NBPFILTER > 0
   1429        1.1  drochner 			if (ic->ic_rawbpf != NULL)
   1430        1.1  drochner 				bpf_mtap(ic->ic_rawbpf, m0);
   1431        1.1  drochner #endif
   1432        1.1  drochner 			if (ural_tx_data(sc, m0, ni) != 0) {
   1433        1.1  drochner 				ieee80211_free_node(ni);
   1434        1.1  drochner 				ifp->if_oerrors++;
   1435        1.1  drochner 				break;
   1436        1.1  drochner 			}
   1437        1.1  drochner 		}
   1438        1.1  drochner 
   1439        1.1  drochner 		sc->sc_tx_timer = 5;
   1440        1.1  drochner 		ifp->if_timer = 1;
   1441        1.1  drochner 	}
   1442        1.1  drochner }
   1443        1.1  drochner 
   1444        1.1  drochner Static void
   1445        1.1  drochner ural_watchdog(struct ifnet *ifp)
   1446        1.1  drochner {
   1447        1.1  drochner 	struct ural_softc *sc = ifp->if_softc;
   1448        1.2  drochner 	struct ieee80211com *ic = &sc->sc_ic;
   1449        1.1  drochner 
   1450        1.1  drochner 	ifp->if_timer = 0;
   1451        1.1  drochner 
   1452        1.1  drochner 	if (sc->sc_tx_timer > 0) {
   1453        1.1  drochner 		if (--sc->sc_tx_timer == 0) {
   1454        1.1  drochner 			printf("%s: device timeout\n", USBDEVNAME(sc->sc_dev));
   1455  1.11.10.1   gdamore 			/*ural_init(sc); XXX needs a process context! */
   1456        1.1  drochner 			ifp->if_oerrors++;
   1457        1.1  drochner 			return;
   1458        1.1  drochner 		}
   1459        1.1  drochner 		ifp->if_timer = 1;
   1460        1.1  drochner 	}
   1461        1.1  drochner 
   1462        1.2  drochner 	ieee80211_watchdog(ic);
   1463        1.1  drochner }
   1464        1.1  drochner 
   1465  1.11.10.1   gdamore /*
   1466  1.11.10.1   gdamore  * This function allows for fast channel switching in monitor mode (used by
   1467  1.11.10.1   gdamore  * net-mgmt/kismet). In IBSS mode, we must explicitly reset the interface to
   1468  1.11.10.1   gdamore  * generate a new beacon frame.
   1469  1.11.10.1   gdamore  */
   1470  1.11.10.1   gdamore Static int
   1471  1.11.10.1   gdamore ural_reset(struct ifnet *ifp)
   1472  1.11.10.1   gdamore {
   1473  1.11.10.1   gdamore 	struct ural_softc *sc = ifp->if_softc;
   1474  1.11.10.1   gdamore 	struct ieee80211com *ic = &sc->sc_ic;
   1475  1.11.10.1   gdamore 
   1476  1.11.10.1   gdamore 	if (ic->ic_opmode != IEEE80211_M_MONITOR)
   1477  1.11.10.1   gdamore 		return ENETRESET;
   1478  1.11.10.1   gdamore 
   1479  1.11.10.1   gdamore 	ural_set_chan(sc, ic->ic_curchan);
   1480  1.11.10.1   gdamore 
   1481  1.11.10.1   gdamore 	return 0;
   1482  1.11.10.1   gdamore }
   1483  1.11.10.1   gdamore 
   1484        1.1  drochner Static int
   1485        1.1  drochner ural_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
   1486        1.1  drochner {
   1487        1.1  drochner 	struct ural_softc *sc = ifp->if_softc;
   1488        1.1  drochner 	struct ieee80211com *ic = &sc->sc_ic;
   1489        1.1  drochner 	int s, error = 0;
   1490        1.1  drochner 
   1491        1.1  drochner 	s = splnet();
   1492        1.1  drochner 
   1493        1.1  drochner 	switch (cmd) {
   1494        1.1  drochner 	case SIOCSIFFLAGS:
   1495        1.1  drochner 		if (ifp->if_flags & IFF_UP) {
   1496        1.1  drochner 			if (ifp->if_flags & IFF_RUNNING)
   1497        1.1  drochner 				ural_update_promisc(sc);
   1498        1.1  drochner 			else
   1499        1.1  drochner 				ural_init(ifp);
   1500        1.1  drochner 		} else {
   1501        1.1  drochner 			if (ifp->if_flags & IFF_RUNNING)
   1502        1.1  drochner 				ural_stop(ifp, 1);
   1503        1.1  drochner 		}
   1504        1.1  drochner 		break;
   1505        1.2  drochner 
   1506        1.1  drochner 	default:
   1507        1.1  drochner 		error = ieee80211_ioctl(ic, cmd, data);
   1508        1.1  drochner 	}
   1509        1.1  drochner 
   1510        1.1  drochner 	if (error == ENETRESET) {
   1511        1.1  drochner 		if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) ==
   1512        1.1  drochner 		    (IFF_UP | IFF_RUNNING))
   1513        1.1  drochner 			ural_init(ifp);
   1514        1.1  drochner 		error = 0;
   1515        1.1  drochner 	}
   1516        1.1  drochner 
   1517        1.1  drochner 	splx(s);
   1518        1.1  drochner 
   1519        1.1  drochner 	return error;
   1520        1.1  drochner }
   1521        1.1  drochner 
   1522        1.1  drochner Static void
   1523  1.11.10.1   gdamore ural_set_testmode(struct ural_softc *sc)
   1524  1.11.10.1   gdamore {
   1525  1.11.10.1   gdamore 	usb_device_request_t req;
   1526  1.11.10.1   gdamore 	usbd_status error;
   1527  1.11.10.1   gdamore 
   1528  1.11.10.1   gdamore 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
   1529  1.11.10.1   gdamore 	req.bRequest = RAL_VENDOR_REQUEST;
   1530  1.11.10.1   gdamore 	USETW(req.wValue, 4);
   1531  1.11.10.1   gdamore 	USETW(req.wIndex, 1);
   1532  1.11.10.1   gdamore 	USETW(req.wLength, 0);
   1533  1.11.10.1   gdamore 
   1534  1.11.10.1   gdamore 	error = usbd_do_request(sc->sc_udev, &req, NULL);
   1535  1.11.10.1   gdamore 	if (error != 0) {
   1536  1.11.10.1   gdamore 		printf("%s: could not set test mode: %s\n",
   1537  1.11.10.1   gdamore 		    USBDEVNAME(sc->sc_dev), usbd_errstr(error));
   1538  1.11.10.1   gdamore 	}
   1539  1.11.10.1   gdamore }
   1540  1.11.10.1   gdamore 
   1541  1.11.10.1   gdamore Static void
   1542        1.1  drochner ural_eeprom_read(struct ural_softc *sc, uint16_t addr, void *buf, int len)
   1543        1.1  drochner {
   1544        1.1  drochner 	usb_device_request_t req;
   1545        1.1  drochner 	usbd_status error;
   1546        1.1  drochner 
   1547        1.1  drochner 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
   1548        1.1  drochner 	req.bRequest = RAL_READ_EEPROM;
   1549        1.1  drochner 	USETW(req.wValue, 0);
   1550        1.1  drochner 	USETW(req.wIndex, addr);
   1551        1.1  drochner 	USETW(req.wLength, len);
   1552        1.1  drochner 
   1553        1.1  drochner 	error = usbd_do_request(sc->sc_udev, &req, buf);
   1554        1.1  drochner 	if (error != 0) {
   1555        1.1  drochner 		printf("%s: could not read EEPROM: %s\n",
   1556        1.1  drochner 		    USBDEVNAME(sc->sc_dev), usbd_errstr(error));
   1557        1.1  drochner 	}
   1558        1.1  drochner }
   1559        1.1  drochner 
   1560        1.1  drochner Static uint16_t
   1561        1.1  drochner ural_read(struct ural_softc *sc, uint16_t reg)
   1562        1.1  drochner {
   1563        1.1  drochner 	usb_device_request_t req;
   1564        1.1  drochner 	usbd_status error;
   1565        1.1  drochner 	uint16_t val;
   1566        1.1  drochner 
   1567        1.1  drochner 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
   1568        1.1  drochner 	req.bRequest = RAL_READ_MAC;
   1569        1.1  drochner 	USETW(req.wValue, 0);
   1570        1.1  drochner 	USETW(req.wIndex, reg);
   1571        1.1  drochner 	USETW(req.wLength, sizeof (uint16_t));
   1572        1.1  drochner 
   1573        1.1  drochner 	error = usbd_do_request(sc->sc_udev, &req, &val);
   1574        1.1  drochner 	if (error != 0) {
   1575        1.1  drochner 		printf("%s: could not read MAC register: %s\n",
   1576        1.1  drochner 		    USBDEVNAME(sc->sc_dev), usbd_errstr(error));
   1577        1.1  drochner 		return 0;
   1578        1.1  drochner 	}
   1579        1.1  drochner 
   1580        1.1  drochner 	return le16toh(val);
   1581        1.1  drochner }
   1582        1.1  drochner 
   1583        1.1  drochner Static void
   1584        1.1  drochner ural_read_multi(struct ural_softc *sc, uint16_t reg, void *buf, int len)
   1585        1.1  drochner {
   1586        1.1  drochner 	usb_device_request_t req;
   1587        1.1  drochner 	usbd_status error;
   1588        1.1  drochner 
   1589        1.1  drochner 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
   1590        1.1  drochner 	req.bRequest = RAL_READ_MULTI_MAC;
   1591        1.1  drochner 	USETW(req.wValue, 0);
   1592        1.1  drochner 	USETW(req.wIndex, reg);
   1593        1.1  drochner 	USETW(req.wLength, len);
   1594        1.1  drochner 
   1595        1.1  drochner 	error = usbd_do_request(sc->sc_udev, &req, buf);
   1596        1.1  drochner 	if (error != 0) {
   1597        1.1  drochner 		printf("%s: could not read MAC register: %s\n",
   1598        1.1  drochner 		    USBDEVNAME(sc->sc_dev), usbd_errstr(error));
   1599        1.1  drochner 	}
   1600        1.1  drochner }
   1601        1.1  drochner 
   1602        1.1  drochner Static void
   1603        1.1  drochner ural_write(struct ural_softc *sc, uint16_t reg, uint16_t val)
   1604        1.1  drochner {
   1605        1.1  drochner 	usb_device_request_t req;
   1606        1.1  drochner 	usbd_status error;
   1607        1.1  drochner 
   1608        1.1  drochner 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
   1609        1.1  drochner 	req.bRequest = RAL_WRITE_MAC;
   1610        1.1  drochner 	USETW(req.wValue, val);
   1611        1.1  drochner 	USETW(req.wIndex, reg);
   1612        1.1  drochner 	USETW(req.wLength, 0);
   1613        1.1  drochner 
   1614        1.1  drochner 	error = usbd_do_request(sc->sc_udev, &req, NULL);
   1615        1.1  drochner 	if (error != 0) {
   1616        1.1  drochner 		printf("%s: could not write MAC register: %s\n",
   1617        1.1  drochner 		    USBDEVNAME(sc->sc_dev), usbd_errstr(error));
   1618        1.1  drochner 	}
   1619        1.1  drochner }
   1620        1.1  drochner 
   1621        1.1  drochner Static void
   1622        1.1  drochner ural_write_multi(struct ural_softc *sc, uint16_t reg, void *buf, int len)
   1623        1.1  drochner {
   1624        1.1  drochner 	usb_device_request_t req;
   1625        1.1  drochner 	usbd_status error;
   1626        1.1  drochner 
   1627        1.1  drochner 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
   1628        1.1  drochner 	req.bRequest = RAL_WRITE_MULTI_MAC;
   1629        1.1  drochner 	USETW(req.wValue, 0);
   1630        1.1  drochner 	USETW(req.wIndex, reg);
   1631        1.1  drochner 	USETW(req.wLength, len);
   1632        1.1  drochner 
   1633        1.1  drochner 	error = usbd_do_request(sc->sc_udev, &req, buf);
   1634        1.1  drochner 	if (error != 0) {
   1635        1.1  drochner 		printf("%s: could not write MAC register: %s\n",
   1636        1.1  drochner 		    USBDEVNAME(sc->sc_dev), usbd_errstr(error));
   1637        1.1  drochner 	}
   1638        1.1  drochner }
   1639        1.1  drochner 
   1640        1.1  drochner Static void
   1641        1.1  drochner ural_bbp_write(struct ural_softc *sc, uint8_t reg, uint8_t val)
   1642        1.1  drochner {
   1643        1.1  drochner 	uint16_t tmp;
   1644        1.1  drochner 	int ntries;
   1645        1.1  drochner 
   1646        1.1  drochner 	for (ntries = 0; ntries < 5; ntries++) {
   1647        1.1  drochner 		if (!(ural_read(sc, RAL_PHY_CSR8) & RAL_BBP_BUSY))
   1648        1.1  drochner 			break;
   1649        1.1  drochner 	}
   1650        1.1  drochner 	if (ntries == 5) {
   1651        1.1  drochner 		printf("%s: could not write to BBP\n", USBDEVNAME(sc->sc_dev));
   1652        1.1  drochner 		return;
   1653        1.1  drochner 	}
   1654        1.1  drochner 
   1655        1.1  drochner 	tmp = reg << 8 | val;
   1656        1.1  drochner 	ural_write(sc, RAL_PHY_CSR7, tmp);
   1657        1.1  drochner }
   1658        1.1  drochner 
   1659        1.1  drochner Static uint8_t
   1660        1.1  drochner ural_bbp_read(struct ural_softc *sc, uint8_t reg)
   1661        1.1  drochner {
   1662        1.1  drochner 	uint16_t val;
   1663        1.1  drochner 	int ntries;
   1664        1.1  drochner 
   1665        1.1  drochner 	val = RAL_BBP_WRITE | reg << 8;
   1666        1.1  drochner 	ural_write(sc, RAL_PHY_CSR7, val);
   1667        1.1  drochner 
   1668        1.1  drochner 	for (ntries = 0; ntries < 5; ntries++) {
   1669        1.1  drochner 		if (!(ural_read(sc, RAL_PHY_CSR8) & RAL_BBP_BUSY))
   1670        1.1  drochner 			break;
   1671        1.1  drochner 	}
   1672        1.1  drochner 	if (ntries == 5) {
   1673        1.1  drochner 		printf("%s: could not read BBP\n", USBDEVNAME(sc->sc_dev));
   1674        1.1  drochner 		return 0;
   1675        1.1  drochner 	}
   1676        1.1  drochner 
   1677        1.1  drochner 	return ural_read(sc, RAL_PHY_CSR7) & 0xff;
   1678        1.1  drochner }
   1679        1.1  drochner 
   1680        1.1  drochner Static void
   1681        1.1  drochner ural_rf_write(struct ural_softc *sc, uint8_t reg, uint32_t val)
   1682        1.1  drochner {
   1683        1.1  drochner 	uint32_t tmp;
   1684        1.1  drochner 	int ntries;
   1685        1.1  drochner 
   1686        1.1  drochner 	for (ntries = 0; ntries < 5; ntries++) {
   1687        1.1  drochner 		if (!(ural_read(sc, RAL_PHY_CSR10) & RAL_RF_LOBUSY))
   1688        1.1  drochner 			break;
   1689        1.1  drochner 	}
   1690        1.1  drochner 	if (ntries == 5) {
   1691        1.1  drochner 		printf("%s: could not write to RF\n", USBDEVNAME(sc->sc_dev));
   1692        1.1  drochner 		return;
   1693        1.1  drochner 	}
   1694        1.1  drochner 
   1695        1.1  drochner 	tmp = RAL_RF_BUSY | RAL_RF_20BIT | (val & 0xfffff) << 2 | (reg & 0x3);
   1696        1.1  drochner 	ural_write(sc, RAL_PHY_CSR9,  tmp & 0xffff);
   1697        1.1  drochner 	ural_write(sc, RAL_PHY_CSR10, tmp >> 16);
   1698        1.1  drochner 
   1699        1.1  drochner 	/* remember last written value in sc */
   1700        1.1  drochner 	sc->rf_regs[reg] = val;
   1701        1.1  drochner 
   1702        1.1  drochner 	DPRINTFN(15, ("RF R[%u] <- 0x%05x\n", reg & 0x3, val & 0xfffff));
   1703        1.1  drochner }
   1704        1.1  drochner 
   1705        1.1  drochner Static void
   1706        1.1  drochner ural_set_chan(struct ural_softc *sc, struct ieee80211_channel *c)
   1707        1.1  drochner {
   1708        1.1  drochner 	struct ieee80211com *ic = &sc->sc_ic;
   1709        1.1  drochner 	uint8_t power, tmp;
   1710        1.1  drochner 	u_int i, chan;
   1711        1.1  drochner 
   1712        1.1  drochner 	chan = ieee80211_chan2ieee(ic, c);
   1713        1.1  drochner 	if (chan == 0 || chan == IEEE80211_CHAN_ANY)
   1714        1.1  drochner 		return;
   1715        1.1  drochner 
   1716        1.1  drochner 	if (IEEE80211_IS_CHAN_2GHZ(c))
   1717        1.1  drochner 		power = min(sc->txpow[chan - 1], 31);
   1718        1.1  drochner 	else
   1719        1.1  drochner 		power = 31;
   1720        1.1  drochner 
   1721  1.11.10.1   gdamore 	/* adjust txpower using ifconfig settings */
   1722  1.11.10.1   gdamore 	power -= (100 - ic->ic_txpowlimit) / 8;
   1723  1.11.10.1   gdamore 
   1724        1.1  drochner 	DPRINTFN(2, ("setting channel to %u, txpower to %u\n", chan, power));
   1725        1.1  drochner 
   1726        1.1  drochner 	switch (sc->rf_rev) {
   1727        1.1  drochner 	case RAL_RF_2522:
   1728        1.1  drochner 		ural_rf_write(sc, RAL_RF1, 0x00814);
   1729        1.1  drochner 		ural_rf_write(sc, RAL_RF2, ural_rf2522_r2[chan - 1]);
   1730        1.1  drochner 		ural_rf_write(sc, RAL_RF3, power << 7 | 0x00040);
   1731        1.1  drochner 		break;
   1732        1.1  drochner 
   1733        1.1  drochner 	case RAL_RF_2523:
   1734        1.1  drochner 		ural_rf_write(sc, RAL_RF1, 0x08804);
   1735        1.1  drochner 		ural_rf_write(sc, RAL_RF2, ural_rf2523_r2[chan - 1]);
   1736        1.1  drochner 		ural_rf_write(sc, RAL_RF3, power << 7 | 0x38044);
   1737        1.1  drochner 		ural_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
   1738        1.1  drochner 		break;
   1739        1.1  drochner 
   1740        1.1  drochner 	case RAL_RF_2524:
   1741        1.1  drochner 		ural_rf_write(sc, RAL_RF1, 0x0c808);
   1742        1.1  drochner 		ural_rf_write(sc, RAL_RF2, ural_rf2524_r2[chan - 1]);
   1743        1.1  drochner 		ural_rf_write(sc, RAL_RF3, power << 7 | 0x00040);
   1744        1.1  drochner 		ural_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
   1745        1.1  drochner 		break;
   1746        1.1  drochner 
   1747        1.1  drochner 	case RAL_RF_2525:
   1748        1.1  drochner 		ural_rf_write(sc, RAL_RF1, 0x08808);
   1749        1.1  drochner 		ural_rf_write(sc, RAL_RF2, ural_rf2525_hi_r2[chan - 1]);
   1750        1.1  drochner 		ural_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
   1751        1.1  drochner 		ural_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
   1752        1.1  drochner 
   1753        1.1  drochner 		ural_rf_write(sc, RAL_RF1, 0x08808);
   1754        1.1  drochner 		ural_rf_write(sc, RAL_RF2, ural_rf2525_r2[chan - 1]);
   1755        1.1  drochner 		ural_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
   1756        1.1  drochner 		ural_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
   1757        1.1  drochner 		break;
   1758        1.1  drochner 
   1759        1.1  drochner 	case RAL_RF_2525E:
   1760        1.1  drochner 		ural_rf_write(sc, RAL_RF1, 0x08808);
   1761        1.1  drochner 		ural_rf_write(sc, RAL_RF2, ural_rf2525e_r2[chan - 1]);
   1762        1.1  drochner 		ural_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
   1763        1.1  drochner 		ural_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00286 : 0x00282);
   1764        1.1  drochner 		break;
   1765        1.1  drochner 
   1766        1.1  drochner 	case RAL_RF_2526:
   1767        1.1  drochner 		ural_rf_write(sc, RAL_RF2, ural_rf2526_hi_r2[chan - 1]);
   1768        1.1  drochner 		ural_rf_write(sc, RAL_RF4, (chan & 1) ? 0x00386 : 0x00381);
   1769        1.1  drochner 		ural_rf_write(sc, RAL_RF1, 0x08804);
   1770        1.1  drochner 
   1771        1.1  drochner 		ural_rf_write(sc, RAL_RF2, ural_rf2526_r2[chan - 1]);
   1772        1.1  drochner 		ural_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
   1773        1.1  drochner 		ural_rf_write(sc, RAL_RF4, (chan & 1) ? 0x00386 : 0x00381);
   1774        1.1  drochner 		break;
   1775        1.1  drochner 
   1776        1.1  drochner 	/* dual-band RF */
   1777        1.1  drochner 	case RAL_RF_5222:
   1778  1.11.10.1   gdamore 		for (i = 0; ural_rf5222[i].chan != chan; i++);
   1779        1.1  drochner 
   1780  1.11.10.1   gdamore 		ural_rf_write(sc, RAL_RF1, ural_rf5222[i].r1);
   1781  1.11.10.1   gdamore 		ural_rf_write(sc, RAL_RF2, ural_rf5222[i].r2);
   1782  1.11.10.1   gdamore 		ural_rf_write(sc, RAL_RF3, power << 7 | 0x00040);
   1783  1.11.10.1   gdamore 		ural_rf_write(sc, RAL_RF4, ural_rf5222[i].r4);
   1784        1.1  drochner 		break;
   1785        1.1  drochner 	}
   1786        1.1  drochner 
   1787        1.1  drochner 	if (ic->ic_opmode != IEEE80211_M_MONITOR &&
   1788        1.1  drochner 	    ic->ic_state != IEEE80211_S_SCAN) {
   1789        1.1  drochner 		/* set Japan filter bit for channel 14 */
   1790        1.1  drochner 		tmp = ural_bbp_read(sc, 70);
   1791        1.1  drochner 
   1792        1.1  drochner 		tmp &= ~RAL_JAPAN_FILTER;
   1793        1.1  drochner 		if (chan == 14)
   1794        1.1  drochner 			tmp |= RAL_JAPAN_FILTER;
   1795        1.1  drochner 
   1796        1.1  drochner 		ural_bbp_write(sc, 70, tmp);
   1797        1.1  drochner 
   1798        1.1  drochner 		/* clear CRC errors */
   1799        1.1  drochner 		ural_read(sc, RAL_STA_CSR0);
   1800        1.1  drochner 
   1801  1.11.10.1   gdamore 		DELAY(10000);
   1802        1.1  drochner 		ural_disable_rf_tune(sc);
   1803        1.1  drochner 	}
   1804        1.1  drochner }
   1805        1.1  drochner 
   1806        1.1  drochner /*
   1807        1.1  drochner  * Disable RF auto-tuning.
   1808        1.1  drochner  */
   1809        1.1  drochner Static void
   1810        1.1  drochner ural_disable_rf_tune(struct ural_softc *sc)
   1811        1.1  drochner {
   1812        1.1  drochner 	uint32_t tmp;
   1813        1.1  drochner 
   1814        1.1  drochner 	if (sc->rf_rev != RAL_RF_2523) {
   1815        1.1  drochner 		tmp = sc->rf_regs[RAL_RF1] & ~RAL_RF1_AUTOTUNE;
   1816        1.1  drochner 		ural_rf_write(sc, RAL_RF1, tmp);
   1817        1.1  drochner 	}
   1818        1.1  drochner 
   1819        1.1  drochner 	tmp = sc->rf_regs[RAL_RF3] & ~RAL_RF3_AUTOTUNE;
   1820        1.1  drochner 	ural_rf_write(sc, RAL_RF3, tmp);
   1821        1.1  drochner 
   1822        1.1  drochner 	DPRINTFN(2, ("disabling RF autotune\n"));
   1823        1.1  drochner }
   1824        1.1  drochner 
   1825        1.1  drochner /*
   1826        1.1  drochner  * Refer to IEEE Std 802.11-1999 pp. 123 for more information on TSF
   1827        1.1  drochner  * synchronization.
   1828        1.1  drochner  */
   1829        1.1  drochner Static void
   1830        1.1  drochner ural_enable_tsf_sync(struct ural_softc *sc)
   1831        1.1  drochner {
   1832        1.1  drochner 	struct ieee80211com *ic = &sc->sc_ic;
   1833        1.1  drochner 	uint16_t logcwmin, preload, tmp;
   1834        1.1  drochner 
   1835        1.1  drochner 	/* first, disable TSF synchronization */
   1836        1.1  drochner 	ural_write(sc, RAL_TXRX_CSR19, 0);
   1837        1.1  drochner 
   1838        1.1  drochner 	tmp = (16 * ic->ic_bss->ni_intval) << 4;
   1839        1.1  drochner 	ural_write(sc, RAL_TXRX_CSR18, tmp);
   1840        1.1  drochner 
   1841        1.1  drochner 	logcwmin = (ic->ic_opmode == IEEE80211_M_IBSS) ? 2 : 0;
   1842        1.1  drochner 	preload = (ic->ic_opmode == IEEE80211_M_IBSS) ? 320 : 6;
   1843        1.1  drochner 	tmp = logcwmin << 12 | preload;
   1844        1.1  drochner 	ural_write(sc, RAL_TXRX_CSR20, tmp);
   1845        1.1  drochner 
   1846        1.1  drochner 	/* finally, enable TSF synchronization */
   1847        1.1  drochner 	tmp = RAL_ENABLE_TSF | RAL_ENABLE_TBCN;
   1848        1.1  drochner 	if (ic->ic_opmode == IEEE80211_M_STA)
   1849        1.1  drochner 		tmp |= RAL_ENABLE_TSF_SYNC(1);
   1850        1.1  drochner 	else
   1851        1.1  drochner 		tmp |= RAL_ENABLE_TSF_SYNC(2) | RAL_ENABLE_BEACON_GENERATOR;
   1852        1.1  drochner 	ural_write(sc, RAL_TXRX_CSR19, tmp);
   1853        1.1  drochner 
   1854        1.1  drochner 	DPRINTF(("enabling TSF synchronization\n"));
   1855        1.1  drochner }
   1856        1.1  drochner 
   1857        1.1  drochner Static void
   1858  1.11.10.1   gdamore ural_update_slot(struct ifnet *ifp)
   1859  1.11.10.1   gdamore {
   1860  1.11.10.1   gdamore 	struct ural_softc *sc = ifp->if_softc;
   1861  1.11.10.1   gdamore 	struct ieee80211com *ic = &sc->sc_ic;
   1862  1.11.10.1   gdamore 	uint16_t slottime, sifs, eifs;
   1863  1.11.10.1   gdamore 
   1864  1.11.10.1   gdamore 	slottime = (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20;
   1865  1.11.10.1   gdamore 
   1866  1.11.10.1   gdamore 	/*
   1867  1.11.10.1   gdamore 	 * These settings may sound a bit inconsistent but this is what the
   1868  1.11.10.1   gdamore 	 * reference driver does.
   1869  1.11.10.1   gdamore 	 */
   1870  1.11.10.1   gdamore 	if (ic->ic_curmode == IEEE80211_MODE_11B) {
   1871  1.11.10.1   gdamore 		sifs = 16 - RAL_RXTX_TURNAROUND;
   1872  1.11.10.1   gdamore 		eifs = 364;
   1873  1.11.10.1   gdamore 	} else {
   1874  1.11.10.1   gdamore 		sifs = 10 - RAL_RXTX_TURNAROUND;
   1875  1.11.10.1   gdamore 		eifs = 64;
   1876  1.11.10.1   gdamore 	}
   1877  1.11.10.1   gdamore 
   1878  1.11.10.1   gdamore 	ural_write(sc, RAL_MAC_CSR10, slottime);
   1879  1.11.10.1   gdamore 	ural_write(sc, RAL_MAC_CSR11, sifs);
   1880  1.11.10.1   gdamore 	ural_write(sc, RAL_MAC_CSR12, eifs);
   1881  1.11.10.1   gdamore }
   1882  1.11.10.1   gdamore 
   1883  1.11.10.1   gdamore Static void
   1884  1.11.10.1   gdamore ural_set_txpreamble(struct ural_softc *sc)
   1885  1.11.10.1   gdamore {
   1886  1.11.10.1   gdamore 	uint16_t tmp;
   1887  1.11.10.1   gdamore 
   1888  1.11.10.1   gdamore 	tmp = ural_read(sc, RAL_TXRX_CSR10);
   1889  1.11.10.1   gdamore 
   1890  1.11.10.1   gdamore 	tmp &= ~RAL_SHORT_PREAMBLE;
   1891  1.11.10.1   gdamore 	if (sc->sc_ic.ic_flags & IEEE80211_F_SHPREAMBLE)
   1892  1.11.10.1   gdamore 		tmp |= RAL_SHORT_PREAMBLE;
   1893  1.11.10.1   gdamore 
   1894  1.11.10.1   gdamore 	ural_write(sc, RAL_TXRX_CSR10, tmp);
   1895  1.11.10.1   gdamore }
   1896  1.11.10.1   gdamore 
   1897  1.11.10.1   gdamore Static void
   1898  1.11.10.1   gdamore ural_set_basicrates(struct ural_softc *sc)
   1899  1.11.10.1   gdamore {
   1900  1.11.10.1   gdamore 	struct ieee80211com *ic = &sc->sc_ic;
   1901  1.11.10.1   gdamore 
   1902  1.11.10.1   gdamore 	/* update basic rate set */
   1903  1.11.10.1   gdamore 	if (ic->ic_curmode == IEEE80211_MODE_11B) {
   1904  1.11.10.1   gdamore 		/* 11b basic rates: 1, 2Mbps */
   1905  1.11.10.1   gdamore 		ural_write(sc, RAL_TXRX_CSR11, 0x3);
   1906  1.11.10.1   gdamore 	} else if (IEEE80211_IS_CHAN_5GHZ(ic->ic_bss->ni_chan)) {
   1907  1.11.10.1   gdamore 		/* 11a basic rates: 6, 12, 24Mbps */
   1908  1.11.10.1   gdamore 		ural_write(sc, RAL_TXRX_CSR11, 0x150);
   1909  1.11.10.1   gdamore 	} else {
   1910  1.11.10.1   gdamore 		/* 11g basic rates: 1, 2, 5.5, 11, 6, 12, 24Mbps */
   1911  1.11.10.1   gdamore 		ural_write(sc, RAL_TXRX_CSR11, 0x15f);
   1912  1.11.10.1   gdamore 	}
   1913  1.11.10.1   gdamore }
   1914  1.11.10.1   gdamore 
   1915  1.11.10.1   gdamore Static void
   1916        1.1  drochner ural_set_bssid(struct ural_softc *sc, uint8_t *bssid)
   1917        1.1  drochner {
   1918        1.1  drochner 	uint16_t tmp;
   1919        1.1  drochner 
   1920        1.1  drochner 	tmp = bssid[0] | bssid[1] << 8;
   1921        1.1  drochner 	ural_write(sc, RAL_MAC_CSR5, tmp);
   1922        1.1  drochner 
   1923        1.1  drochner 	tmp = bssid[2] | bssid[3] << 8;
   1924        1.1  drochner 	ural_write(sc, RAL_MAC_CSR6, tmp);
   1925        1.1  drochner 
   1926        1.1  drochner 	tmp = bssid[4] | bssid[5] << 8;
   1927        1.1  drochner 	ural_write(sc, RAL_MAC_CSR7, tmp);
   1928        1.1  drochner 
   1929        1.1  drochner 	DPRINTF(("setting BSSID to %s\n", ether_sprintf(bssid)));
   1930        1.1  drochner }
   1931        1.1  drochner 
   1932        1.1  drochner Static void
   1933        1.1  drochner ural_set_macaddr(struct ural_softc *sc, uint8_t *addr)
   1934        1.1  drochner {
   1935        1.1  drochner 	uint16_t tmp;
   1936        1.1  drochner 
   1937        1.1  drochner 	tmp = addr[0] | addr[1] << 8;
   1938        1.1  drochner 	ural_write(sc, RAL_MAC_CSR2, tmp);
   1939        1.1  drochner 
   1940        1.1  drochner 	tmp = addr[2] | addr[3] << 8;
   1941        1.1  drochner 	ural_write(sc, RAL_MAC_CSR3, tmp);
   1942        1.1  drochner 
   1943        1.1  drochner 	tmp = addr[4] | addr[5] << 8;
   1944        1.1  drochner 	ural_write(sc, RAL_MAC_CSR4, tmp);
   1945        1.1  drochner 
   1946        1.2  drochner 	DPRINTF(("setting MAC address to %s\n", ether_sprintf(addr)));
   1947        1.1  drochner }
   1948        1.1  drochner 
   1949        1.1  drochner Static void
   1950        1.1  drochner ural_update_promisc(struct ural_softc *sc)
   1951        1.1  drochner {
   1952  1.11.10.1   gdamore 	struct ifnet *ifp = sc->sc_ic.ic_ifp;
   1953  1.11.10.1   gdamore 	uint32_t tmp;
   1954        1.1  drochner 
   1955        1.1  drochner 	tmp = ural_read(sc, RAL_TXRX_CSR2);
   1956        1.1  drochner 
   1957        1.1  drochner 	tmp &= ~RAL_DROP_NOT_TO_ME;
   1958        1.1  drochner 	if (!(ifp->if_flags & IFF_PROMISC))
   1959        1.1  drochner 		tmp |= RAL_DROP_NOT_TO_ME;
   1960        1.1  drochner 
   1961        1.1  drochner 	ural_write(sc, RAL_TXRX_CSR2, tmp);
   1962        1.1  drochner 
   1963        1.1  drochner 	DPRINTF(("%s promiscuous mode\n", (ifp->if_flags & IFF_PROMISC) ?
   1964        1.1  drochner 	    "entering" : "leaving"));
   1965        1.1  drochner }
   1966        1.1  drochner 
   1967        1.1  drochner Static const char *
   1968        1.1  drochner ural_get_rf(int rev)
   1969        1.1  drochner {
   1970        1.1  drochner 	switch (rev) {
   1971        1.1  drochner 	case RAL_RF_2522:	return "RT2522";
   1972        1.1  drochner 	case RAL_RF_2523:	return "RT2523";
   1973        1.1  drochner 	case RAL_RF_2524:	return "RT2524";
   1974        1.1  drochner 	case RAL_RF_2525:	return "RT2525";
   1975        1.1  drochner 	case RAL_RF_2525E:	return "RT2525e";
   1976        1.1  drochner 	case RAL_RF_2526:	return "RT2526";
   1977        1.1  drochner 	case RAL_RF_5222:	return "RT5222";
   1978        1.1  drochner 	default:		return "unknown";
   1979        1.1  drochner 	}
   1980        1.1  drochner }
   1981        1.1  drochner 
   1982        1.1  drochner Static void
   1983        1.1  drochner ural_read_eeprom(struct ural_softc *sc)
   1984        1.1  drochner {
   1985        1.1  drochner 	struct ieee80211com *ic = &sc->sc_ic;
   1986        1.1  drochner 	uint16_t val;
   1987        1.1  drochner 
   1988        1.1  drochner 	ural_eeprom_read(sc, RAL_EEPROM_CONFIG0, &val, 2);
   1989        1.1  drochner 	val = le16toh(val);
   1990        1.1  drochner 	sc->rf_rev =   (val >> 11) & 0x7;
   1991        1.1  drochner 	sc->hw_radio = (val >> 10) & 0x1;
   1992        1.1  drochner 	sc->led_mode = (val >> 6)  & 0x7;
   1993        1.1  drochner 	sc->rx_ant =   (val >> 4)  & 0x3;
   1994        1.1  drochner 	sc->tx_ant =   (val >> 2)  & 0x3;
   1995        1.1  drochner 	sc->nb_ant =   val & 0x3;
   1996        1.1  drochner 
   1997        1.1  drochner 	/* read MAC address */
   1998        1.1  drochner 	ural_eeprom_read(sc, RAL_EEPROM_ADDRESS, ic->ic_myaddr, 6);
   1999        1.1  drochner 
   2000        1.1  drochner 	/* read default values for BBP registers */
   2001        1.1  drochner 	ural_eeprom_read(sc, RAL_EEPROM_BBP_BASE, sc->bbp_prom, 2 * 16);
   2002        1.1  drochner 
   2003        1.1  drochner 	/* read Tx power for all b/g channels */
   2004        1.1  drochner 	ural_eeprom_read(sc, RAL_EEPROM_TXPOWER, sc->txpow, 14);
   2005        1.1  drochner }
   2006        1.1  drochner 
   2007        1.1  drochner Static int
   2008        1.1  drochner ural_bbp_init(struct ural_softc *sc)
   2009        1.1  drochner {
   2010        1.1  drochner #define N(a)	(sizeof (a) / sizeof ((a)[0]))
   2011        1.1  drochner 	int i, ntries;
   2012        1.1  drochner 
   2013        1.1  drochner 	/* wait for BBP to be ready */
   2014        1.1  drochner 	for (ntries = 0; ntries < 100; ntries++) {
   2015        1.1  drochner 		if (ural_bbp_read(sc, RAL_BBP_VERSION) != 0)
   2016        1.1  drochner 			break;
   2017        1.1  drochner 		DELAY(1000);
   2018        1.1  drochner 	}
   2019        1.1  drochner 	if (ntries == 100) {
   2020        1.1  drochner 		printf("%s: timeout waiting for BBP\n", USBDEVNAME(sc->sc_dev));
   2021        1.1  drochner 		return EIO;
   2022        1.1  drochner 	}
   2023        1.1  drochner 
   2024        1.1  drochner 	/* initialize BBP registers to default values */
   2025        1.1  drochner 	for (i = 0; i < N(ural_def_bbp); i++)
   2026        1.1  drochner 		ural_bbp_write(sc, ural_def_bbp[i].reg, ural_def_bbp[i].val);
   2027        1.1  drochner 
   2028        1.1  drochner #if 0
   2029        1.1  drochner 	/* initialize BBP registers to values stored in EEPROM */
   2030        1.1  drochner 	for (i = 0; i < 16; i++) {
   2031        1.1  drochner 		if (sc->bbp_prom[i].reg == 0xff)
   2032        1.1  drochner 			continue;
   2033        1.1  drochner 		ural_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val);
   2034        1.1  drochner 	}
   2035        1.1  drochner #endif
   2036        1.1  drochner 
   2037        1.1  drochner 	return 0;
   2038        1.1  drochner #undef N
   2039        1.1  drochner }
   2040        1.1  drochner 
   2041        1.1  drochner Static void
   2042        1.1  drochner ural_set_txantenna(struct ural_softc *sc, int antenna)
   2043        1.1  drochner {
   2044        1.1  drochner 	uint16_t tmp;
   2045        1.1  drochner 	uint8_t tx;
   2046        1.1  drochner 
   2047        1.1  drochner 	tx = ural_bbp_read(sc, RAL_BBP_TX) & ~RAL_BBP_ANTMASK;
   2048        1.1  drochner 	if (antenna == 1)
   2049        1.1  drochner 		tx |= RAL_BBP_ANTA;
   2050        1.1  drochner 	else if (antenna == 2)
   2051        1.1  drochner 		tx |= RAL_BBP_ANTB;
   2052        1.1  drochner 	else
   2053        1.1  drochner 		tx |= RAL_BBP_DIVERSITY;
   2054        1.1  drochner 
   2055        1.1  drochner 	/* need to force I/Q flip for RF 2525e, 2526 and 5222 */
   2056        1.1  drochner 	if (sc->rf_rev == RAL_RF_2525E || sc->rf_rev == RAL_RF_2526 ||
   2057        1.1  drochner 	    sc->rf_rev == RAL_RF_5222)
   2058        1.1  drochner 		tx |= RAL_BBP_FLIPIQ;
   2059        1.1  drochner 
   2060        1.1  drochner 	ural_bbp_write(sc, RAL_BBP_TX, tx);
   2061        1.1  drochner 
   2062  1.11.10.1   gdamore 	/* update values in PHY_CSR5 and PHY_CSR6 */
   2063        1.1  drochner 	tmp = ural_read(sc, RAL_PHY_CSR5) & ~0x7;
   2064        1.1  drochner 	ural_write(sc, RAL_PHY_CSR5, tmp | (tx & 0x7));
   2065        1.1  drochner 
   2066        1.1  drochner 	tmp = ural_read(sc, RAL_PHY_CSR6) & ~0x7;
   2067        1.1  drochner 	ural_write(sc, RAL_PHY_CSR6, tmp | (tx & 0x7));
   2068        1.1  drochner }
   2069        1.1  drochner 
   2070        1.1  drochner Static void
   2071        1.1  drochner ural_set_rxantenna(struct ural_softc *sc, int antenna)
   2072        1.1  drochner {
   2073        1.1  drochner 	uint8_t rx;
   2074        1.1  drochner 
   2075        1.1  drochner 	rx = ural_bbp_read(sc, RAL_BBP_RX) & ~RAL_BBP_ANTMASK;
   2076        1.1  drochner 	if (antenna == 1)
   2077        1.1  drochner 		rx |= RAL_BBP_ANTA;
   2078        1.1  drochner 	else if (antenna == 2)
   2079        1.1  drochner 		rx |= RAL_BBP_ANTB;
   2080        1.1  drochner 	else
   2081        1.1  drochner 		rx |= RAL_BBP_DIVERSITY;
   2082        1.1  drochner 
   2083        1.1  drochner 	/* need to force no I/Q flip for RF 2525e and 2526 */
   2084        1.1  drochner 	if (sc->rf_rev == RAL_RF_2525E || sc->rf_rev == RAL_RF_2526)
   2085        1.1  drochner 		rx &= ~RAL_BBP_FLIPIQ;
   2086        1.1  drochner 
   2087        1.1  drochner 	ural_bbp_write(sc, RAL_BBP_RX, rx);
   2088        1.1  drochner }
   2089        1.1  drochner 
   2090        1.1  drochner Static int
   2091        1.1  drochner ural_init(struct ifnet *ifp)
   2092        1.1  drochner {
   2093        1.1  drochner #define N(a)	(sizeof (a) / sizeof ((a)[0]))
   2094        1.1  drochner 	struct ural_softc *sc = ifp->if_softc;
   2095        1.1  drochner 	struct ieee80211com *ic = &sc->sc_ic;
   2096        1.1  drochner 	struct ieee80211_key *wk;
   2097        1.1  drochner 	struct ural_rx_data *data;
   2098  1.11.10.1   gdamore 	uint16_t tmp;
   2099        1.1  drochner 	usbd_status error;
   2100        1.1  drochner 	int i, ntries;
   2101        1.1  drochner 
   2102  1.11.10.1   gdamore 	ural_set_testmode(sc);
   2103  1.11.10.1   gdamore 	ural_write(sc, 0x308, 0x00f0);	/* XXX magic */
   2104  1.11.10.1   gdamore 
   2105        1.1  drochner 	ural_stop(ifp, 0);
   2106        1.1  drochner 
   2107        1.1  drochner 	/* initialize MAC registers to default values */
   2108        1.1  drochner 	for (i = 0; i < N(ural_def_mac); i++)
   2109        1.1  drochner 		ural_write(sc, ural_def_mac[i].reg, ural_def_mac[i].val);
   2110        1.1  drochner 
   2111        1.1  drochner 	/* wait for BBP and RF to wake up (this can take a long time!) */
   2112        1.1  drochner 	for (ntries = 0; ntries < 100; ntries++) {
   2113        1.1  drochner 		tmp = ural_read(sc, RAL_MAC_CSR17);
   2114        1.1  drochner 		if ((tmp & (RAL_BBP_AWAKE | RAL_RF_AWAKE)) ==
   2115        1.1  drochner 		    (RAL_BBP_AWAKE | RAL_RF_AWAKE))
   2116        1.1  drochner 			break;
   2117        1.1  drochner 		DELAY(1000);
   2118        1.1  drochner 	}
   2119        1.1  drochner 	if (ntries == 100) {
   2120        1.1  drochner 		printf("%s: timeout waiting for BBP/RF to wakeup\n",
   2121        1.1  drochner 		    USBDEVNAME(sc->sc_dev));
   2122        1.1  drochner 		error = EIO;
   2123        1.1  drochner 		goto fail;
   2124        1.1  drochner 	}
   2125        1.1  drochner 
   2126        1.1  drochner 	/* we're ready! */
   2127        1.1  drochner 	ural_write(sc, RAL_MAC_CSR1, RAL_HOST_READY);
   2128        1.1  drochner 
   2129  1.11.10.1   gdamore 	/* set basic rate set (will be updated later) */
   2130  1.11.10.1   gdamore 	ural_write(sc, RAL_TXRX_CSR11, 0x15f);
   2131        1.1  drochner 
   2132        1.1  drochner 	error = ural_bbp_init(sc);
   2133        1.1  drochner 	if (error != 0)
   2134        1.1  drochner 		goto fail;
   2135        1.1  drochner 
   2136        1.1  drochner 	/* set default BSS channel */
   2137        1.9     skrll 	ural_set_chan(sc, ic->ic_curchan);
   2138        1.1  drochner 
   2139        1.1  drochner 	/* clear statistic registers (STA_CSR0 to STA_CSR10) */
   2140  1.11.10.1   gdamore 	ural_read_multi(sc, RAL_STA_CSR0, sc->sta, sizeof sc->sta);
   2141        1.1  drochner 
   2142  1.11.10.1   gdamore 	ural_set_txantenna(sc, sc->tx_ant);
   2143  1.11.10.1   gdamore 	ural_set_rxantenna(sc, sc->rx_ant);
   2144        1.1  drochner 
   2145        1.1  drochner 	IEEE80211_ADDR_COPY(ic->ic_myaddr, LLADDR(ifp->if_sadl));
   2146        1.1  drochner 	ural_set_macaddr(sc, ic->ic_myaddr);
   2147        1.1  drochner 
   2148        1.1  drochner 	/*
   2149        1.1  drochner 	 * Copy WEP keys into adapter's memory (SEC_CSR0 to SEC_CSR31).
   2150        1.1  drochner 	 */
   2151        1.1  drochner 	for (i = 0; i < IEEE80211_WEP_NKID; i++) {
   2152  1.11.10.1   gdamore 		wk = &ic->ic_crypto.cs_nw_keys[i];
   2153        1.4  drochner 		ural_write_multi(sc, wk->wk_keyix * IEEE80211_KEYBUF_SIZE +
   2154        1.4  drochner 		    RAL_SEC_CSR0, wk->wk_key, IEEE80211_KEYBUF_SIZE);
   2155        1.1  drochner 	}
   2156        1.1  drochner 
   2157        1.1  drochner 	/*
   2158  1.11.10.1   gdamore 	 * Allocate xfer for AMRR statistics requests.
   2159  1.11.10.1   gdamore 	 */
   2160  1.11.10.1   gdamore 	sc->amrr_xfer = usbd_alloc_xfer(sc->sc_udev);
   2161  1.11.10.1   gdamore 	if (sc->amrr_xfer == NULL) {
   2162  1.11.10.1   gdamore 		printf("%s: could not allocate AMRR xfer\n",
   2163  1.11.10.1   gdamore 		    USBDEVNAME(sc->sc_dev));
   2164  1.11.10.1   gdamore 		goto fail;
   2165  1.11.10.1   gdamore 	}
   2166  1.11.10.1   gdamore 
   2167  1.11.10.1   gdamore 	/*
   2168        1.1  drochner 	 * Open Tx and Rx USB bulk pipes.
   2169        1.1  drochner 	 */
   2170        1.1  drochner 	error = usbd_open_pipe(sc->sc_iface, sc->sc_tx_no, USBD_EXCLUSIVE_USE,
   2171        1.1  drochner 	    &sc->sc_tx_pipeh);
   2172        1.1  drochner 	if (error != 0) {
   2173        1.1  drochner 		printf("%s: could not open Tx pipe: %s\n",
   2174        1.1  drochner 		    USBDEVNAME(sc->sc_dev), usbd_errstr(error));
   2175        1.1  drochner 		goto fail;
   2176        1.1  drochner 	}
   2177        1.1  drochner 
   2178        1.1  drochner 	error = usbd_open_pipe(sc->sc_iface, sc->sc_rx_no, USBD_EXCLUSIVE_USE,
   2179        1.1  drochner 	    &sc->sc_rx_pipeh);
   2180        1.1  drochner 	if (error != 0) {
   2181        1.1  drochner 		printf("%s: could not open Rx pipe: %s\n",
   2182        1.1  drochner 		    USBDEVNAME(sc->sc_dev), usbd_errstr(error));
   2183        1.1  drochner 		goto fail;
   2184        1.1  drochner 	}
   2185        1.1  drochner 
   2186        1.1  drochner 	/*
   2187        1.1  drochner 	 * Allocate Tx and Rx xfer queues.
   2188        1.1  drochner 	 */
   2189        1.1  drochner 	error = ural_alloc_tx_list(sc);
   2190        1.1  drochner 	if (error != 0) {
   2191        1.1  drochner 		printf("%s: could not allocate Tx list\n",
   2192        1.1  drochner 		    USBDEVNAME(sc->sc_dev));
   2193        1.1  drochner 		goto fail;
   2194        1.1  drochner 	}
   2195        1.1  drochner 
   2196        1.1  drochner 	error = ural_alloc_rx_list(sc);
   2197        1.1  drochner 	if (error != 0) {
   2198        1.1  drochner 		printf("%s: could not allocate Rx list\n",
   2199        1.1  drochner 		    USBDEVNAME(sc->sc_dev));
   2200        1.1  drochner 		goto fail;
   2201        1.1  drochner 	}
   2202        1.1  drochner 
   2203        1.1  drochner 	/*
   2204        1.1  drochner 	 * Start up the receive pipe.
   2205        1.1  drochner 	 */
   2206        1.1  drochner 	for (i = 0; i < RAL_RX_LIST_COUNT; i++) {
   2207        1.1  drochner 		data = &sc->rx_data[i];
   2208        1.1  drochner 
   2209        1.1  drochner 		usbd_setup_xfer(data->xfer, sc->sc_rx_pipeh, data, data->buf,
   2210        1.1  drochner 		    MCLBYTES, USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, ural_rxeof);
   2211        1.1  drochner 		usbd_transfer(data->xfer);
   2212        1.1  drochner 	}
   2213        1.1  drochner 
   2214        1.1  drochner 	/* kick Rx */
   2215        1.1  drochner 	tmp = RAL_DROP_PHY_ERROR | RAL_DROP_CRC_ERROR;
   2216        1.1  drochner 	if (ic->ic_opmode != IEEE80211_M_MONITOR) {
   2217        1.1  drochner 		tmp |= RAL_DROP_CTL | RAL_DROP_VERSION_ERROR;
   2218        1.1  drochner 		if (ic->ic_opmode != IEEE80211_M_HOSTAP)
   2219        1.1  drochner 			tmp |= RAL_DROP_TODS;
   2220        1.1  drochner 		if (!(ifp->if_flags & IFF_PROMISC))
   2221        1.1  drochner 			tmp |= RAL_DROP_NOT_TO_ME;
   2222        1.1  drochner 	}
   2223        1.1  drochner 	ural_write(sc, RAL_TXRX_CSR2, tmp);
   2224        1.1  drochner 
   2225        1.1  drochner 	ifp->if_flags &= ~IFF_OACTIVE;
   2226        1.1  drochner 	ifp->if_flags |= IFF_RUNNING;
   2227        1.1  drochner 
   2228  1.11.10.1   gdamore 	if (ic->ic_opmode != IEEE80211_M_MONITOR) {
   2229  1.11.10.1   gdamore 		if (ic->ic_roaming != IEEE80211_ROAMING_MANUAL)
   2230  1.11.10.1   gdamore 			ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
   2231  1.11.10.1   gdamore 	} else
   2232        1.1  drochner 		ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
   2233        1.1  drochner 
   2234        1.1  drochner 	return 0;
   2235        1.1  drochner 
   2236        1.1  drochner fail:	ural_stop(ifp, 1);
   2237        1.1  drochner 	return error;
   2238        1.1  drochner #undef N
   2239        1.1  drochner }
   2240        1.1  drochner 
   2241        1.1  drochner Static void
   2242        1.1  drochner ural_stop(struct ifnet *ifp, int disable)
   2243        1.1  drochner {
   2244        1.1  drochner 	struct ural_softc *sc = ifp->if_softc;
   2245        1.1  drochner 	struct ieee80211com *ic = &sc->sc_ic;
   2246        1.1  drochner 
   2247        1.1  drochner 	ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
   2248        1.1  drochner 
   2249        1.4  drochner 	sc->sc_tx_timer = 0;
   2250        1.4  drochner 	ifp->if_timer = 0;
   2251        1.4  drochner 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   2252        1.4  drochner 
   2253        1.1  drochner 	/* disable Rx */
   2254        1.1  drochner 	ural_write(sc, RAL_TXRX_CSR2, RAL_DISABLE_RX);
   2255        1.1  drochner 
   2256        1.1  drochner 	/* reset ASIC and BBP (but won't reset MAC registers!) */
   2257        1.1  drochner 	ural_write(sc, RAL_MAC_CSR1, RAL_RESET_ASIC | RAL_RESET_BBP);
   2258        1.1  drochner 	ural_write(sc, RAL_MAC_CSR1, 0);
   2259        1.1  drochner 
   2260  1.11.10.1   gdamore 	if (sc->amrr_xfer != NULL) {
   2261  1.11.10.1   gdamore 		usbd_free_xfer(sc->amrr_xfer);
   2262  1.11.10.1   gdamore 		sc->amrr_xfer = NULL;
   2263  1.11.10.1   gdamore 	}
   2264  1.11.10.1   gdamore 
   2265        1.1  drochner 	if (sc->sc_rx_pipeh != NULL) {
   2266        1.1  drochner 		usbd_abort_pipe(sc->sc_rx_pipeh);
   2267        1.1  drochner 		usbd_close_pipe(sc->sc_rx_pipeh);
   2268        1.1  drochner 		sc->sc_rx_pipeh = NULL;
   2269        1.1  drochner 	}
   2270        1.1  drochner 
   2271        1.1  drochner 	if (sc->sc_tx_pipeh != NULL) {
   2272        1.1  drochner 		usbd_abort_pipe(sc->sc_tx_pipeh);
   2273        1.1  drochner 		usbd_close_pipe(sc->sc_tx_pipeh);
   2274        1.1  drochner 		sc->sc_tx_pipeh = NULL;
   2275        1.1  drochner 	}
   2276        1.1  drochner 
   2277        1.1  drochner 	ural_free_rx_list(sc);
   2278        1.1  drochner 	ural_free_tx_list(sc);
   2279        1.1  drochner }
   2280        1.1  drochner 
   2281        1.1  drochner int
   2282        1.1  drochner ural_activate(device_ptr_t self, enum devact act)
   2283        1.1  drochner {
   2284        1.2  drochner 	struct ural_softc *sc = (struct ural_softc *)self;
   2285        1.2  drochner 
   2286        1.1  drochner 	switch (act) {
   2287        1.1  drochner 	case DVACT_ACTIVATE:
   2288        1.1  drochner 		return EOPNOTSUPP;
   2289        1.2  drochner 		break;
   2290        1.1  drochner 
   2291        1.1  drochner 	case DVACT_DEACTIVATE:
   2292        1.2  drochner 		if_deactivate(&sc->sc_if);
   2293        1.1  drochner 		break;
   2294        1.1  drochner 	}
   2295        1.1  drochner 
   2296        1.1  drochner 	return 0;
   2297        1.1  drochner }
   2298  1.11.10.1   gdamore 
   2299  1.11.10.1   gdamore #define URAL_AMRR_MIN_SUCCESS_THRESHOLD	 1
   2300  1.11.10.1   gdamore #define URAL_AMRR_MAX_SUCCESS_THRESHOLD	10
   2301  1.11.10.1   gdamore 
   2302  1.11.10.1   gdamore Static void
   2303  1.11.10.1   gdamore ural_amrr_start(struct ural_softc *sc, struct ieee80211_node *ni)
   2304  1.11.10.1   gdamore {
   2305  1.11.10.1   gdamore 	struct ural_amrr *amrr = &sc->amrr;
   2306  1.11.10.1   gdamore 	int i;
   2307  1.11.10.1   gdamore 
   2308  1.11.10.1   gdamore 	/* clear statistic registers (STA_CSR0 to STA_CSR10) */
   2309  1.11.10.1   gdamore 	ural_read_multi(sc, RAL_STA_CSR0, sc->sta, sizeof sc->sta);
   2310  1.11.10.1   gdamore 
   2311  1.11.10.1   gdamore 	amrr->success = 0;
   2312  1.11.10.1   gdamore 	amrr->recovery = 0;
   2313  1.11.10.1   gdamore 	amrr->txcnt = amrr->retrycnt = 0;
   2314  1.11.10.1   gdamore 	amrr->success_threshold = URAL_AMRR_MIN_SUCCESS_THRESHOLD;
   2315  1.11.10.1   gdamore 
   2316  1.11.10.1   gdamore 	/* set rate to some reasonable initial value */
   2317  1.11.10.1   gdamore 	for (i = ni->ni_rates.rs_nrates - 1;
   2318  1.11.10.1   gdamore 	     i > 0 && (ni->ni_rates.rs_rates[i] & IEEE80211_RATE_VAL) > 72;
   2319  1.11.10.1   gdamore 	     i--);
   2320  1.11.10.1   gdamore 
   2321  1.11.10.1   gdamore 	ni->ni_txrate = i;
   2322  1.11.10.1   gdamore 
   2323  1.11.10.1   gdamore 	callout_reset(&sc->amrr_ch, hz, ural_amrr_timeout, sc);
   2324  1.11.10.1   gdamore }
   2325  1.11.10.1   gdamore 
   2326  1.11.10.1   gdamore Static void
   2327  1.11.10.1   gdamore ural_amrr_timeout(void *arg)
   2328  1.11.10.1   gdamore {
   2329  1.11.10.1   gdamore 	struct ural_softc *sc = (struct ural_softc *)arg;
   2330  1.11.10.1   gdamore 	usb_device_request_t req;
   2331  1.11.10.1   gdamore 	int s;
   2332  1.11.10.1   gdamore 
   2333  1.11.10.1   gdamore 	s = splusb();
   2334  1.11.10.1   gdamore 
   2335  1.11.10.1   gdamore 	/*
   2336  1.11.10.1   gdamore 	 * Asynchronously read statistic registers (cleared by read).
   2337  1.11.10.1   gdamore 	 */
   2338  1.11.10.1   gdamore 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
   2339  1.11.10.1   gdamore 	req.bRequest = RAL_READ_MULTI_MAC;
   2340  1.11.10.1   gdamore 	USETW(req.wValue, 0);
   2341  1.11.10.1   gdamore 	USETW(req.wIndex, RAL_STA_CSR0);
   2342  1.11.10.1   gdamore 	USETW(req.wLength, sizeof sc->sta);
   2343  1.11.10.1   gdamore 
   2344  1.11.10.1   gdamore 	usbd_setup_default_xfer(sc->amrr_xfer, sc->sc_udev, sc,
   2345  1.11.10.1   gdamore 	    USBD_DEFAULT_TIMEOUT, &req, sc->sta, sizeof sc->sta, 0,
   2346  1.11.10.1   gdamore 	    ural_amrr_update);
   2347  1.11.10.1   gdamore 	(void)usbd_transfer(sc->amrr_xfer);
   2348  1.11.10.1   gdamore 
   2349  1.11.10.1   gdamore 	splx(s);
   2350  1.11.10.1   gdamore }
   2351  1.11.10.1   gdamore 
   2352  1.11.10.1   gdamore Static void
   2353  1.11.10.1   gdamore ural_amrr_update(usbd_xfer_handle xfer, usbd_private_handle priv,
   2354  1.11.10.1   gdamore     usbd_status status)
   2355  1.11.10.1   gdamore {
   2356  1.11.10.1   gdamore 	struct ural_softc *sc = (struct ural_softc *)priv;
   2357  1.11.10.1   gdamore 	struct ural_amrr *amrr = &sc->amrr;
   2358  1.11.10.1   gdamore 	struct ifnet *ifp = sc->sc_ic.ic_ifp;
   2359  1.11.10.1   gdamore 
   2360  1.11.10.1   gdamore 	if (status != USBD_NORMAL_COMPLETION) {
   2361  1.11.10.1   gdamore 		printf("%s: could not retrieve Tx statistics - "
   2362  1.11.10.1   gdamore 		    "cancelling automatic rate control\n",
   2363  1.11.10.1   gdamore 		    USBDEVNAME(sc->sc_dev));
   2364  1.11.10.1   gdamore 		return;
   2365  1.11.10.1   gdamore 	}
   2366  1.11.10.1   gdamore 
   2367  1.11.10.1   gdamore 	/* count TX retry-fail as Tx errors */
   2368  1.11.10.1   gdamore 	ifp->if_oerrors += sc->sta[9];
   2369  1.11.10.1   gdamore 
   2370  1.11.10.1   gdamore 	amrr->retrycnt =
   2371  1.11.10.1   gdamore 	    sc->sta[7] +	/* TX one-retry ok count */
   2372  1.11.10.1   gdamore 	    sc->sta[8] +	/* TX more-retry ok count */
   2373  1.11.10.1   gdamore 	    sc->sta[9];		/* TX retry-fail count */
   2374  1.11.10.1   gdamore 
   2375  1.11.10.1   gdamore 	amrr->txcnt =
   2376  1.11.10.1   gdamore 	    amrr->retrycnt +
   2377  1.11.10.1   gdamore 	    sc->sta[6];		/* TX no-retry ok count */
   2378  1.11.10.1   gdamore 
   2379  1.11.10.1   gdamore 	ural_ratectl(amrr, sc->sc_ic.ic_bss);
   2380  1.11.10.1   gdamore 
   2381  1.11.10.1   gdamore 	callout_reset(&sc->amrr_ch, hz, ural_amrr_timeout, sc);
   2382  1.11.10.1   gdamore }
   2383  1.11.10.1   gdamore 
   2384  1.11.10.1   gdamore /*-
   2385  1.11.10.1   gdamore  * Naive implementation of the Adaptive Multi Rate Retry algorithm:
   2386  1.11.10.1   gdamore  *     "IEEE 802.11 Rate Adaptation: A Practical Approach"
   2387  1.11.10.1   gdamore  *     Mathieu Lacage, Hossein Manshaei, Thierry Turletti
   2388  1.11.10.1   gdamore  *     INRIA Sophia - Projet Planete
   2389  1.11.10.1   gdamore  *     http://www-sop.inria.fr/rapports/sophia/RR-5208.html
   2390  1.11.10.1   gdamore  *
   2391  1.11.10.1   gdamore  * This algorithm is particularly well suited for ural since it does not
   2392  1.11.10.1   gdamore  * require per-frame retry statistics.  Note however that since h/w does
   2393  1.11.10.1   gdamore  * not provide per-frame stats, we can't do per-node rate adaptation and
   2394  1.11.10.1   gdamore  * thus automatic rate adaptation is only enabled in STA operating mode.
   2395  1.11.10.1   gdamore  */
   2396  1.11.10.1   gdamore #define is_success(amrr)	\
   2397  1.11.10.1   gdamore 	((amrr)->retrycnt < (amrr)->txcnt / 10)
   2398  1.11.10.1   gdamore #define is_failure(amrr)	\
   2399  1.11.10.1   gdamore 	((amrr)->retrycnt > (amrr)->txcnt / 3)
   2400  1.11.10.1   gdamore #define is_enough(amrr)		\
   2401  1.11.10.1   gdamore 	((amrr)->txcnt > 10)
   2402  1.11.10.1   gdamore #define is_min_rate(ni)		\
   2403  1.11.10.1   gdamore 	((ni)->ni_txrate == 0)
   2404  1.11.10.1   gdamore #define is_max_rate(ni)		\
   2405  1.11.10.1   gdamore 	((ni)->ni_txrate == (ni)->ni_rates.rs_nrates - 1)
   2406  1.11.10.1   gdamore #define increase_rate(ni)	\
   2407  1.11.10.1   gdamore 	((ni)->ni_txrate++)
   2408  1.11.10.1   gdamore #define decrease_rate(ni)	\
   2409  1.11.10.1   gdamore 	((ni)->ni_txrate--)
   2410  1.11.10.1   gdamore #define reset_cnt(amrr)		\
   2411  1.11.10.1   gdamore 	do { (amrr)->txcnt = (amrr)->retrycnt = 0; } while (0)
   2412  1.11.10.1   gdamore Static void
   2413  1.11.10.1   gdamore ural_ratectl(struct ural_amrr *amrr, struct ieee80211_node *ni)
   2414  1.11.10.1   gdamore {
   2415  1.11.10.1   gdamore 	int need_change = 0;
   2416  1.11.10.1   gdamore 
   2417  1.11.10.1   gdamore 	if (is_success(amrr) && is_enough(amrr)) {
   2418  1.11.10.1   gdamore 		amrr->success++;
   2419  1.11.10.1   gdamore 		if (amrr->success >= amrr->success_threshold &&
   2420  1.11.10.1   gdamore 		    !is_max_rate(ni)) {
   2421  1.11.10.1   gdamore 			amrr->recovery = 1;
   2422  1.11.10.1   gdamore 			amrr->success = 0;
   2423  1.11.10.1   gdamore 			increase_rate(ni);
   2424  1.11.10.1   gdamore 			need_change = 1;
   2425  1.11.10.1   gdamore 		} else {
   2426  1.11.10.1   gdamore 			amrr->recovery = 0;
   2427  1.11.10.1   gdamore 		}
   2428  1.11.10.1   gdamore 	} else if (is_failure(amrr)) {
   2429  1.11.10.1   gdamore 		amrr->success = 0;
   2430  1.11.10.1   gdamore 		if (!is_min_rate(ni)) {
   2431  1.11.10.1   gdamore 			if (amrr->recovery) {
   2432  1.11.10.1   gdamore 				amrr->success_threshold *= 2;
   2433  1.11.10.1   gdamore 				if (amrr->success_threshold >
   2434  1.11.10.1   gdamore 				    URAL_AMRR_MAX_SUCCESS_THRESHOLD)
   2435  1.11.10.1   gdamore 					amrr->success_threshold =
   2436  1.11.10.1   gdamore 					    URAL_AMRR_MAX_SUCCESS_THRESHOLD;
   2437  1.11.10.1   gdamore 			} else {
   2438  1.11.10.1   gdamore 				amrr->success_threshold =
   2439  1.11.10.1   gdamore 				    URAL_AMRR_MIN_SUCCESS_THRESHOLD;
   2440  1.11.10.1   gdamore 			}
   2441  1.11.10.1   gdamore 			decrease_rate(ni);
   2442  1.11.10.1   gdamore 			need_change = 1;
   2443  1.11.10.1   gdamore 		}
   2444  1.11.10.1   gdamore 		amrr->recovery = 0;	/* original paper was incorrect */
   2445  1.11.10.1   gdamore 	}
   2446  1.11.10.1   gdamore 
   2447  1.11.10.1   gdamore 	if (is_enough(amrr) || need_change)
   2448  1.11.10.1   gdamore 		reset_cnt(amrr);
   2449  1.11.10.1   gdamore }
   2450