2 * Copyright (c) 2008 Weongyo Jeong <weongyo@FreeBSD.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/cdefs.h>
18 __FBSDID("$FreeBSD$");
19 #include <sys/param.h>
20 #include <sys/sockio.h>
21 #include <sys/sysctl.h>
23 #include <sys/mutex.h>
25 #include <sys/kernel.h>
26 #include <sys/socket.h>
27 #include <sys/systm.h>
28 #include <sys/malloc.h>
29 #include <sys/module.h>
31 #include <sys/endian.h>
34 #include <machine/bus.h>
35 #include <machine/resource.h>
39 #include <net/if_arp.h>
40 #include <net/ethernet.h>
41 #include <net/if_dl.h>
42 #include <net/if_media.h>
43 #include <net/if_types.h>
46 #include <netinet/in.h>
47 #include <netinet/in_systm.h>
48 #include <netinet/in_var.h>
49 #include <netinet/if_ether.h>
50 #include <netinet/ip.h>
53 #include <net80211/ieee80211_var.h>
54 #include <net80211/ieee80211_regdomain.h>
55 #include <net80211/ieee80211_radiotap.h>
57 #include <dev/usb/usb.h>
58 #include <dev/usb/usbdi.h>
61 #include <dev/usb/wlan/if_urtwreg.h>
62 #include <dev/usb/wlan/if_urtwvar.h>
64 static SYSCTL_NODE(_hw_usb, OID_AUTO, urtw, CTLFLAG_RW, 0, "USB Realtek 8187L");
67 SYSCTL_INT(_hw_usb_urtw, OID_AUTO, debug, CTLFLAG_RW | CTLFLAG_TUN, &urtw_debug, 0,
68 "control debugging printfs");
69 TUNABLE_INT("hw.usb.urtw.debug", &urtw_debug);
71 URTW_DEBUG_XMIT = 0x00000001, /* basic xmit operation */
72 URTW_DEBUG_RECV = 0x00000002, /* basic recv operation */
73 URTW_DEBUG_RESET = 0x00000004, /* reset processing */
74 URTW_DEBUG_TX_PROC = 0x00000008, /* tx ISR proc */
75 URTW_DEBUG_RX_PROC = 0x00000010, /* rx ISR proc */
76 URTW_DEBUG_STATE = 0x00000020, /* 802.11 state transitions */
77 URTW_DEBUG_STAT = 0x00000040, /* statistic */
78 URTW_DEBUG_INIT = 0x00000080, /* initialization of dev */
79 URTW_DEBUG_TXSTATUS = 0x00000100, /* tx status */
80 URTW_DEBUG_ANY = 0xffffffff
82 #define DPRINTF(sc, m, fmt, ...) do { \
83 if (sc->sc_debug & (m)) \
84 printf(fmt, __VA_ARGS__); \
87 #define DPRINTF(sc, m, fmt, ...) do { \
91 static int urtw_preamble_mode = URTW_PREAMBLE_MODE_LONG;
92 SYSCTL_INT(_hw_usb_urtw, OID_AUTO, preamble_mode, CTLFLAG_RW | CTLFLAG_TUN,
93 &urtw_preamble_mode, 0, "set the preable mode (long or short)");
94 TUNABLE_INT("hw.usb.urtw.preamble_mode", &urtw_preamble_mode);
96 /* recognized device vendors/products */
97 #define urtw_lookup(v, p) \
98 ((const struct urtw_type *)usb_lookup(urtw_devs, v, p))
99 #define URTW_DEV_B(v,p) \
100 { USB_VPI(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, URTW_REV_RTL8187B) }
101 #define URTW_DEV_L(v,p) \
102 { USB_VPI(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, URTW_REV_RTL8187L) }
103 #define URTW_REV_RTL8187B 0
104 #define URTW_REV_RTL8187L 1
105 static const STRUCT_USB_HOST_ID urtw_devs[] = {
106 URTW_DEV_B(NETGEAR, WG111V3),
107 URTW_DEV_B(REALTEK, RTL8187B_0),
108 URTW_DEV_B(REALTEK, RTL8187B_1),
109 URTW_DEV_B(REALTEK, RTL8187B_2),
110 URTW_DEV_B(SITECOMEU, WL168V4),
111 URTW_DEV_L(ASUS, P5B_WIFI),
112 URTW_DEV_L(BELKIN, F5D7050E),
113 URTW_DEV_L(LINKSYS4, WUSB54GCV2),
114 URTW_DEV_L(NETGEAR, WG111V2),
115 URTW_DEV_L(REALTEK, RTL8187),
116 URTW_DEV_L(SITECOMEU, WL168V1),
117 URTW_DEV_L(SURECOM, EP9001G2A),
118 { USB_VPI(USB_VENDOR_OVISLINK, 0x8187, URTW_REV_RTL8187L) },
119 { USB_VPI(USB_VENDOR_DICKSMITH, 0x9401, URTW_REV_RTL8187L) },
120 { USB_VPI(USB_VENDOR_HP, 0xca02, URTW_REV_RTL8187L) },
121 { USB_VPI(USB_VENDOR_LOGITEC, 0x010c, URTW_REV_RTL8187L) },
122 { USB_VPI(USB_VENDOR_NETGEAR, 0x6100, URTW_REV_RTL8187L) },
123 { USB_VPI(USB_VENDOR_SPHAIRON, 0x0150, URTW_REV_RTL8187L) },
124 { USB_VPI(USB_VENDOR_QCOM, 0x6232, URTW_REV_RTL8187L) },
129 #define urtw_read8_m(sc, val, data) do { \
130 error = urtw_read8_c(sc, val, data); \
134 #define urtw_write8_m(sc, val, data) do { \
135 error = urtw_write8_c(sc, val, data); \
139 #define urtw_read16_m(sc, val, data) do { \
140 error = urtw_read16_c(sc, val, data); \
144 #define urtw_write16_m(sc, val, data) do { \
145 error = urtw_write16_c(sc, val, data); \
149 #define urtw_read32_m(sc, val, data) do { \
150 error = urtw_read32_c(sc, val, data); \
154 #define urtw_write32_m(sc, val, data) do { \
155 error = urtw_write32_c(sc, val, data); \
159 #define urtw_8187_write_phy_ofdm(sc, val, data) do { \
160 error = urtw_8187_write_phy_ofdm_c(sc, val, data); \
164 #define urtw_8187_write_phy_cck(sc, val, data) do { \
165 error = urtw_8187_write_phy_cck_c(sc, val, data); \
169 #define urtw_8225_write(sc, val, data) do { \
170 error = urtw_8225_write_c(sc, val, data); \
180 static uint8_t urtw_8225_agc[] = {
181 0x9e, 0x9e, 0x9e, 0x9e, 0x9e, 0x9e, 0x9e, 0x9e, 0x9d, 0x9c, 0x9b,
182 0x9a, 0x99, 0x98, 0x97, 0x96, 0x95, 0x94, 0x93, 0x92, 0x91, 0x90,
183 0x8f, 0x8e, 0x8d, 0x8c, 0x8b, 0x8a, 0x89, 0x88, 0x87, 0x86, 0x85,
184 0x84, 0x83, 0x82, 0x81, 0x80, 0x3f, 0x3e, 0x3d, 0x3c, 0x3b, 0x3a,
185 0x39, 0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x31, 0x30, 0x2f,
186 0x2e, 0x2d, 0x2c, 0x2b, 0x2a, 0x29, 0x28, 0x27, 0x26, 0x25, 0x24,
187 0x23, 0x22, 0x21, 0x20, 0x1f, 0x1e, 0x1d, 0x1c, 0x1b, 0x1a, 0x19,
188 0x18, 0x17, 0x16, 0x15, 0x14, 0x13, 0x12, 0x11, 0x10, 0x0f, 0x0e,
189 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08, 0x07, 0x06, 0x05, 0x04, 0x03,
190 0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
191 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
192 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01
195 static uint8_t urtw_8225z2_agc[] = {
196 0x5e, 0x5e, 0x5e, 0x5e, 0x5d, 0x5b, 0x59, 0x57, 0x55, 0x53, 0x51,
197 0x4f, 0x4d, 0x4b, 0x49, 0x47, 0x45, 0x43, 0x41, 0x3f, 0x3d, 0x3b,
198 0x39, 0x37, 0x35, 0x33, 0x31, 0x2f, 0x2d, 0x2b, 0x29, 0x27, 0x25,
199 0x23, 0x21, 0x1f, 0x1d, 0x1b, 0x19, 0x17, 0x15, 0x13, 0x11, 0x0f,
200 0x0d, 0x0b, 0x09, 0x07, 0x05, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01,
201 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x19, 0x19,
202 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x20, 0x21, 0x22, 0x23,
203 0x24, 0x25, 0x26, 0x26, 0x27, 0x27, 0x28, 0x28, 0x29, 0x2a, 0x2a,
204 0x2a, 0x2b, 0x2b, 0x2b, 0x2c, 0x2c, 0x2c, 0x2d, 0x2d, 0x2d, 0x2d,
205 0x2e, 0x2e, 0x2e, 0x2e, 0x2f, 0x2f, 0x2f, 0x30, 0x30, 0x31, 0x31,
206 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31,
207 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31
210 static uint32_t urtw_8225_channel[] = {
211 0x0000, /* dummy channel 0 */
228 static uint8_t urtw_8225_gain[] = {
229 0x23, 0x88, 0x7c, 0xa5, /* -82dbm */
230 0x23, 0x88, 0x7c, 0xb5, /* -82dbm */
231 0x23, 0x88, 0x7c, 0xc5, /* -82dbm */
232 0x33, 0x80, 0x79, 0xc5, /* -78dbm */
233 0x43, 0x78, 0x76, 0xc5, /* -74dbm */
234 0x53, 0x60, 0x73, 0xc5, /* -70dbm */
235 0x63, 0x58, 0x70, 0xc5, /* -66dbm */
238 static struct urtw_pair urtw_8225_rf_part1[] = {
239 { 0x00, 0x0067 }, { 0x01, 0x0fe0 }, { 0x02, 0x044d }, { 0x03, 0x0441 },
240 { 0x04, 0x0486 }, { 0x05, 0x0bc0 }, { 0x06, 0x0ae6 }, { 0x07, 0x082a },
241 { 0x08, 0x001f }, { 0x09, 0x0334 }, { 0x0a, 0x0fd4 }, { 0x0b, 0x0391 },
242 { 0x0c, 0x0050 }, { 0x0d, 0x06db }, { 0x0e, 0x0029 }, { 0x0f, 0x0914 },
245 static struct urtw_pair urtw_8225_rf_part2[] = {
246 { 0x00, 0x01 }, { 0x01, 0x02 }, { 0x02, 0x42 }, { 0x03, 0x00 },
247 { 0x04, 0x00 }, { 0x05, 0x00 }, { 0x06, 0x40 }, { 0x07, 0x00 },
248 { 0x08, 0x40 }, { 0x09, 0xfe }, { 0x0a, 0x09 }, { 0x0b, 0x80 },
249 { 0x0c, 0x01 }, { 0x0e, 0xd3 }, { 0x0f, 0x38 }, { 0x10, 0x84 },
250 { 0x11, 0x06 }, { 0x12, 0x20 }, { 0x13, 0x20 }, { 0x14, 0x00 },
251 { 0x15, 0x40 }, { 0x16, 0x00 }, { 0x17, 0x40 }, { 0x18, 0xef },
252 { 0x19, 0x19 }, { 0x1a, 0x20 }, { 0x1b, 0x76 }, { 0x1c, 0x04 },
253 { 0x1e, 0x95 }, { 0x1f, 0x75 }, { 0x20, 0x1f }, { 0x21, 0x27 },
254 { 0x22, 0x16 }, { 0x24, 0x46 }, { 0x25, 0x20 }, { 0x26, 0x90 },
258 static struct urtw_pair urtw_8225_rf_part3[] = {
259 { 0x00, 0x98 }, { 0x03, 0x20 }, { 0x04, 0x7e }, { 0x05, 0x12 },
260 { 0x06, 0xfc }, { 0x07, 0x78 }, { 0x08, 0x2e }, { 0x10, 0x9b },
261 { 0x11, 0x88 }, { 0x12, 0x47 }, { 0x13, 0xd0 }, { 0x19, 0x00 },
262 { 0x1a, 0xa0 }, { 0x1b, 0x08 }, { 0x40, 0x86 }, { 0x41, 0x8d },
263 { 0x42, 0x15 }, { 0x43, 0x18 }, { 0x44, 0x1f }, { 0x45, 0x1e },
264 { 0x46, 0x1a }, { 0x47, 0x15 }, { 0x48, 0x10 }, { 0x49, 0x0a },
265 { 0x4a, 0x05 }, { 0x4b, 0x02 }, { 0x4c, 0x05 }
268 static uint16_t urtw_8225_rxgain[] = {
269 0x0400, 0x0401, 0x0402, 0x0403, 0x0404, 0x0405, 0x0408, 0x0409,
270 0x040a, 0x040b, 0x0502, 0x0503, 0x0504, 0x0505, 0x0540, 0x0541,
271 0x0542, 0x0543, 0x0544, 0x0545, 0x0580, 0x0581, 0x0582, 0x0583,
272 0x0584, 0x0585, 0x0588, 0x0589, 0x058a, 0x058b, 0x0643, 0x0644,
273 0x0645, 0x0680, 0x0681, 0x0682, 0x0683, 0x0684, 0x0685, 0x0688,
274 0x0689, 0x068a, 0x068b, 0x068c, 0x0742, 0x0743, 0x0744, 0x0745,
275 0x0780, 0x0781, 0x0782, 0x0783, 0x0784, 0x0785, 0x0788, 0x0789,
276 0x078a, 0x078b, 0x078c, 0x078d, 0x0790, 0x0791, 0x0792, 0x0793,
277 0x0794, 0x0795, 0x0798, 0x0799, 0x079a, 0x079b, 0x079c, 0x079d,
278 0x07a0, 0x07a1, 0x07a2, 0x07a3, 0x07a4, 0x07a5, 0x07a8, 0x07a9,
279 0x07aa, 0x07ab, 0x07ac, 0x07ad, 0x07b0, 0x07b1, 0x07b2, 0x07b3,
280 0x07b4, 0x07b5, 0x07b8, 0x07b9, 0x07ba, 0x07bb, 0x07bb
283 static uint8_t urtw_8225_threshold[] = {
284 0x8d, 0x8d, 0x8d, 0x8d, 0x9d, 0xad, 0xbd,
287 static uint8_t urtw_8225_tx_gain_cck_ofdm[] = {
288 0x02, 0x06, 0x0e, 0x1e, 0x3e, 0x7e
291 static uint8_t urtw_8225_txpwr_cck[] = {
292 0x18, 0x17, 0x15, 0x11, 0x0c, 0x08, 0x04, 0x02,
293 0x1b, 0x1a, 0x17, 0x13, 0x0e, 0x09, 0x04, 0x02,
294 0x1f, 0x1e, 0x1a, 0x15, 0x10, 0x0a, 0x05, 0x02,
295 0x22, 0x21, 0x1d, 0x18, 0x11, 0x0b, 0x06, 0x02,
296 0x26, 0x25, 0x21, 0x1b, 0x14, 0x0d, 0x06, 0x03,
297 0x2b, 0x2a, 0x25, 0x1e, 0x16, 0x0e, 0x07, 0x03
300 static uint8_t urtw_8225_txpwr_cck_ch14[] = {
301 0x18, 0x17, 0x15, 0x0c, 0x00, 0x00, 0x00, 0x00,
302 0x1b, 0x1a, 0x17, 0x0e, 0x00, 0x00, 0x00, 0x00,
303 0x1f, 0x1e, 0x1a, 0x0f, 0x00, 0x00, 0x00, 0x00,
304 0x22, 0x21, 0x1d, 0x11, 0x00, 0x00, 0x00, 0x00,
305 0x26, 0x25, 0x21, 0x13, 0x00, 0x00, 0x00, 0x00,
306 0x2b, 0x2a, 0x25, 0x15, 0x00, 0x00, 0x00, 0x00
309 static uint8_t urtw_8225_txpwr_ofdm[]={
310 0x80, 0x90, 0xa2, 0xb5, 0xcb, 0xe4
313 static uint8_t urtw_8225v2_gain_bg[]={
314 0x23, 0x15, 0xa5, /* -82-1dbm */
315 0x23, 0x15, 0xb5, /* -82-2dbm */
316 0x23, 0x15, 0xc5, /* -82-3dbm */
317 0x33, 0x15, 0xc5, /* -78dbm */
318 0x43, 0x15, 0xc5, /* -74dbm */
319 0x53, 0x15, 0xc5, /* -70dbm */
320 0x63, 0x15, 0xc5, /* -66dbm */
323 static struct urtw_pair urtw_8225v2_rf_part1[] = {
324 { 0x00, 0x02bf }, { 0x01, 0x0ee0 }, { 0x02, 0x044d }, { 0x03, 0x0441 },
325 { 0x04, 0x08c3 }, { 0x05, 0x0c72 }, { 0x06, 0x00e6 }, { 0x07, 0x082a },
326 { 0x08, 0x003f }, { 0x09, 0x0335 }, { 0x0a, 0x09d4 }, { 0x0b, 0x07bb },
327 { 0x0c, 0x0850 }, { 0x0d, 0x0cdf }, { 0x0e, 0x002b }, { 0x0f, 0x0114 }
330 static struct urtw_pair urtw_8225v2b_rf_part0[] = {
331 { 0x00, 0x00b7 }, { 0x01, 0x0ee0 }, { 0x02, 0x044d }, { 0x03, 0x0441 },
332 { 0x04, 0x08c3 }, { 0x05, 0x0c72 }, { 0x06, 0x00e6 }, { 0x07, 0x082a },
333 { 0x08, 0x003f }, { 0x09, 0x0335 }, { 0x0a, 0x09d4 }, { 0x0b, 0x07bb },
334 { 0x0c, 0x0850 }, { 0x0d, 0x0cdf }, { 0x0e, 0x002b }, { 0x0f, 0x0114 }
337 static struct urtw_pair urtw_8225v2b_rf_part1[] = {
338 {0x0f0, 0x32}, {0x0f1, 0x32}, {0x0f2, 0x00},
339 {0x0f3, 0x00}, {0x0f4, 0x32}, {0x0f5, 0x43},
340 {0x0f6, 0x00}, {0x0f7, 0x00}, {0x0f8, 0x46},
341 {0x0f9, 0xa4}, {0x0fa, 0x00}, {0x0fb, 0x00},
342 {0x0fc, 0x96}, {0x0fd, 0xa4}, {0x0fe, 0x00},
343 {0x0ff, 0x00}, {0x158, 0x4b}, {0x159, 0x00},
344 {0x15a, 0x4b}, {0x15b, 0x00}, {0x160, 0x4b},
345 {0x161, 0x09}, {0x162, 0x4b}, {0x163, 0x09},
346 {0x1ce, 0x0f}, {0x1cf, 0x00}, {0x1e0, 0xff},
347 {0x1e1, 0x0f}, {0x1e2, 0x00}, {0x1f0, 0x4e},
348 {0x1f1, 0x01}, {0x1f2, 0x02}, {0x1f3, 0x03},
349 {0x1f4, 0x04}, {0x1f5, 0x05}, {0x1f6, 0x06},
350 {0x1f7, 0x07}, {0x1f8, 0x08}, {0x24e, 0x00},
351 {0x20c, 0x04}, {0x221, 0x61}, {0x222, 0x68},
352 {0x223, 0x6f}, {0x224, 0x76}, {0x225, 0x7d},
353 {0x226, 0x84}, {0x227, 0x8d}, {0x24d, 0x08},
354 {0x250, 0x05}, {0x251, 0xf5}, {0x252, 0x04},
355 {0x253, 0xa0}, {0x254, 0x1f}, {0x255, 0x23},
356 {0x256, 0x45}, {0x257, 0x67}, {0x258, 0x08},
357 {0x259, 0x08}, {0x25a, 0x08}, {0x25b, 0x08},
358 {0x260, 0x08}, {0x261, 0x08}, {0x262, 0x08},
359 {0x263, 0x08}, {0x264, 0xcf}, {0x272, 0x56},
360 {0x273, 0x9a}, {0x034, 0xf0}, {0x035, 0x0f},
361 {0x05b, 0x40}, {0x084, 0x88}, {0x085, 0x24},
362 {0x088, 0x54}, {0x08b, 0xb8}, {0x08c, 0x07},
363 {0x08d, 0x00}, {0x094, 0x1b}, {0x095, 0x12},
364 {0x096, 0x00}, {0x097, 0x06}, {0x09d, 0x1a},
365 {0x09f, 0x10}, {0x0b4, 0x22}, {0x0be, 0x80},
366 {0x0db, 0x00}, {0x0ee, 0x00}, {0x091, 0x03},
367 {0x24c, 0x00}, {0x39f, 0x00}, {0x08c, 0x01},
368 {0x08d, 0x10}, {0x08e, 0x08}, {0x08f, 0x00}
371 static struct urtw_pair urtw_8225v2_rf_part2[] = {
372 { 0x00, 0x01 }, { 0x01, 0x02 }, { 0x02, 0x42 }, { 0x03, 0x00 },
373 { 0x04, 0x00 }, { 0x05, 0x00 }, { 0x06, 0x40 }, { 0x07, 0x00 },
374 { 0x08, 0x40 }, { 0x09, 0xfe }, { 0x0a, 0x08 }, { 0x0b, 0x80 },
375 { 0x0c, 0x01 }, { 0x0d, 0x43 }, { 0x0e, 0xd3 }, { 0x0f, 0x38 },
376 { 0x10, 0x84 }, { 0x11, 0x07 }, { 0x12, 0x20 }, { 0x13, 0x20 },
377 { 0x14, 0x00 }, { 0x15, 0x40 }, { 0x16, 0x00 }, { 0x17, 0x40 },
378 { 0x18, 0xef }, { 0x19, 0x19 }, { 0x1a, 0x20 }, { 0x1b, 0x15 },
379 { 0x1c, 0x04 }, { 0x1d, 0xc5 }, { 0x1e, 0x95 }, { 0x1f, 0x75 },
380 { 0x20, 0x1f }, { 0x21, 0x17 }, { 0x22, 0x16 }, { 0x23, 0x80 },
381 { 0x24, 0x46 }, { 0x25, 0x00 }, { 0x26, 0x90 }, { 0x27, 0x88 }
384 static struct urtw_pair urtw_8225v2b_rf_part2[] = {
385 { 0x00, 0x10 }, { 0x01, 0x0d }, { 0x02, 0x01 }, { 0x03, 0x00 },
386 { 0x04, 0x14 }, { 0x05, 0xfb }, { 0x06, 0xfb }, { 0x07, 0x60 },
387 { 0x08, 0x00 }, { 0x09, 0x60 }, { 0x0a, 0x00 }, { 0x0b, 0x00 },
388 { 0x0c, 0x00 }, { 0x0d, 0x5c }, { 0x0e, 0x00 }, { 0x0f, 0x00 },
389 { 0x10, 0x40 }, { 0x11, 0x00 }, { 0x12, 0x40 }, { 0x13, 0x00 },
390 { 0x14, 0x00 }, { 0x15, 0x00 }, { 0x16, 0xa8 }, { 0x17, 0x26 },
391 { 0x18, 0x32 }, { 0x19, 0x33 }, { 0x1a, 0x07 }, { 0x1b, 0xa5 },
392 { 0x1c, 0x6f }, { 0x1d, 0x55 }, { 0x1e, 0xc8 }, { 0x1f, 0xb3 },
393 { 0x20, 0x0a }, { 0x21, 0xe1 }, { 0x22, 0x2C }, { 0x23, 0x8a },
394 { 0x24, 0x86 }, { 0x25, 0x83 }, { 0x26, 0x34 }, { 0x27, 0x0f },
395 { 0x28, 0x4f }, { 0x29, 0x24 }, { 0x2a, 0x6f }, { 0x2b, 0xc2 },
396 { 0x2c, 0x6b }, { 0x2d, 0x40 }, { 0x2e, 0x80 }, { 0x2f, 0x00 },
397 { 0x30, 0xc0 }, { 0x31, 0xc1 }, { 0x32, 0x58 }, { 0x33, 0xf1 },
398 { 0x34, 0x00 }, { 0x35, 0xe4 }, { 0x36, 0x90 }, { 0x37, 0x3e },
399 { 0x38, 0x6d }, { 0x39, 0x3c }, { 0x3a, 0xfb }, { 0x3b, 0x07 }
402 static struct urtw_pair urtw_8225v2_rf_part3[] = {
403 { 0x00, 0x98 }, { 0x03, 0x20 }, { 0x04, 0x7e }, { 0x05, 0x12 },
404 { 0x06, 0xfc }, { 0x07, 0x78 }, { 0x08, 0x2e }, { 0x09, 0x11 },
405 { 0x0a, 0x17 }, { 0x0b, 0x11 }, { 0x10, 0x9b }, { 0x11, 0x88 },
406 { 0x12, 0x47 }, { 0x13, 0xd0 }, { 0x19, 0x00 }, { 0x1a, 0xa0 },
407 { 0x1b, 0x08 }, { 0x1d, 0x00 }, { 0x40, 0x86 }, { 0x41, 0x9d },
408 { 0x42, 0x15 }, { 0x43, 0x18 }, { 0x44, 0x36 }, { 0x45, 0x35 },
409 { 0x46, 0x2e }, { 0x47, 0x25 }, { 0x48, 0x1c }, { 0x49, 0x12 },
410 { 0x4a, 0x09 }, { 0x4b, 0x04 }, { 0x4c, 0x05 }
413 static uint16_t urtw_8225v2_rxgain[] = {
414 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0008, 0x0009,
415 0x000a, 0x000b, 0x0102, 0x0103, 0x0104, 0x0105, 0x0140, 0x0141,
416 0x0142, 0x0143, 0x0144, 0x0145, 0x0180, 0x0181, 0x0182, 0x0183,
417 0x0184, 0x0185, 0x0188, 0x0189, 0x018a, 0x018b, 0x0243, 0x0244,
418 0x0245, 0x0280, 0x0281, 0x0282, 0x0283, 0x0284, 0x0285, 0x0288,
419 0x0289, 0x028a, 0x028b, 0x028c, 0x0342, 0x0343, 0x0344, 0x0345,
420 0x0380, 0x0381, 0x0382, 0x0383, 0x0384, 0x0385, 0x0388, 0x0389,
421 0x038a, 0x038b, 0x038c, 0x038d, 0x0390, 0x0391, 0x0392, 0x0393,
422 0x0394, 0x0395, 0x0398, 0x0399, 0x039a, 0x039b, 0x039c, 0x039d,
423 0x03a0, 0x03a1, 0x03a2, 0x03a3, 0x03a4, 0x03a5, 0x03a8, 0x03a9,
424 0x03aa, 0x03ab, 0x03ac, 0x03ad, 0x03b0, 0x03b1, 0x03b2, 0x03b3,
425 0x03b4, 0x03b5, 0x03b8, 0x03b9, 0x03ba, 0x03bb, 0x03bb
428 static uint16_t urtw_8225v2b_rxgain[] = {
429 0x0400, 0x0401, 0x0402, 0x0403, 0x0404, 0x0405, 0x0408, 0x0409,
430 0x040a, 0x040b, 0x0502, 0x0503, 0x0504, 0x0505, 0x0540, 0x0541,
431 0x0542, 0x0543, 0x0544, 0x0545, 0x0580, 0x0581, 0x0582, 0x0583,
432 0x0584, 0x0585, 0x0588, 0x0589, 0x058a, 0x058b, 0x0643, 0x0644,
433 0x0645, 0x0680, 0x0681, 0x0682, 0x0683, 0x0684, 0x0685, 0x0688,
434 0x0689, 0x068a, 0x068b, 0x068c, 0x0742, 0x0743, 0x0744, 0x0745,
435 0x0780, 0x0781, 0x0782, 0x0783, 0x0784, 0x0785, 0x0788, 0x0789,
436 0x078a, 0x078b, 0x078c, 0x078d, 0x0790, 0x0791, 0x0792, 0x0793,
437 0x0794, 0x0795, 0x0798, 0x0799, 0x079a, 0x079b, 0x079c, 0x079d,
438 0x07a0, 0x07a1, 0x07a2, 0x07a3, 0x07a4, 0x07a5, 0x07a8, 0x07a9,
439 0x03aa, 0x03ab, 0x03ac, 0x03ad, 0x03b0, 0x03b1, 0x03b2, 0x03b3,
440 0x03b4, 0x03b5, 0x03b8, 0x03b9, 0x03ba, 0x03bb, 0x03bb
443 static uint8_t urtw_8225v2_tx_gain_cck_ofdm[] = {
444 0x00, 0x01, 0x02, 0x03, 0x04, 0x05,
445 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
446 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11,
447 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
448 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d,
449 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23,
452 static uint8_t urtw_8225v2_txpwr_cck[] = {
453 0x36, 0x35, 0x2e, 0x25, 0x1c, 0x12, 0x09, 0x04
456 static uint8_t urtw_8225v2_txpwr_cck_ch14[] = {
457 0x36, 0x35, 0x2e, 0x1b, 0x00, 0x00, 0x00, 0x00
460 static uint8_t urtw_8225v2b_txpwr_cck[] = {
461 0x36, 0x35, 0x2e, 0x25, 0x1c, 0x12, 0x09, 0x04,
462 0x30, 0x2f, 0x29, 0x21, 0x19, 0x10, 0x08, 0x03,
463 0x2b, 0x2a, 0x25, 0x1e, 0x16, 0x0e, 0x07, 0x03,
464 0x26, 0x25, 0x21, 0x1b, 0x14, 0x0d, 0x06, 0x03
467 static uint8_t urtw_8225v2b_txpwr_cck_ch14[] = {
468 0x36, 0x35, 0x2e, 0x1b, 0x00, 0x00, 0x00, 0x00,
469 0x30, 0x2f, 0x29, 0x15, 0x00, 0x00, 0x00, 0x00,
470 0x30, 0x2f, 0x29, 0x15, 0x00, 0x00, 0x00, 0x00,
471 0x30, 0x2f, 0x29, 0x15, 0x00, 0x00, 0x00, 0x00
474 static struct urtw_pair urtw_ratetable[] = {
475 { 2, 0 }, { 4, 1 }, { 11, 2 }, { 12, 4 }, { 18, 5 },
476 { 22, 3 }, { 24, 6 }, { 36, 7 }, { 48, 8 }, { 72, 9 },
477 { 96, 10 }, { 108, 11 }
480 static const uint8_t urtw_8187b_reg_table[][3] = {
481 { 0xf0, 0x32, 0 }, { 0xf1, 0x32, 0 }, { 0xf2, 0x00, 0 },
482 { 0xf3, 0x00, 0 }, { 0xf4, 0x32, 0 }, { 0xf5, 0x43, 0 },
483 { 0xf6, 0x00, 0 }, { 0xf7, 0x00, 0 }, { 0xf8, 0x46, 0 },
484 { 0xf9, 0xa4, 0 }, { 0xfa, 0x00, 0 }, { 0xfb, 0x00, 0 },
485 { 0xfc, 0x96, 0 }, { 0xfd, 0xa4, 0 }, { 0xfe, 0x00, 0 },
486 { 0xff, 0x00, 0 }, { 0x58, 0x4b, 1 }, { 0x59, 0x00, 1 },
487 { 0x5a, 0x4b, 1 }, { 0x5b, 0x00, 1 }, { 0x60, 0x4b, 1 },
488 { 0x61, 0x09, 1 }, { 0x62, 0x4b, 1 }, { 0x63, 0x09, 1 },
489 { 0xce, 0x0f, 1 }, { 0xcf, 0x00, 1 }, { 0xe0, 0xff, 1 },
490 { 0xe1, 0x0f, 1 }, { 0xe2, 0x00, 1 }, { 0xf0, 0x4e, 1 },
491 { 0xf1, 0x01, 1 }, { 0xf2, 0x02, 1 }, { 0xf3, 0x03, 1 },
492 { 0xf4, 0x04, 1 }, { 0xf5, 0x05, 1 }, { 0xf6, 0x06, 1 },
493 { 0xf7, 0x07, 1 }, { 0xf8, 0x08, 1 }, { 0x4e, 0x00, 2 },
494 { 0x0c, 0x04, 2 }, { 0x21, 0x61, 2 }, { 0x22, 0x68, 2 },
495 { 0x23, 0x6f, 2 }, { 0x24, 0x76, 2 }, { 0x25, 0x7d, 2 },
496 { 0x26, 0x84, 2 }, { 0x27, 0x8d, 2 }, { 0x4d, 0x08, 2 },
497 { 0x50, 0x05, 2 }, { 0x51, 0xf5, 2 }, { 0x52, 0x04, 2 },
498 { 0x53, 0xa0, 2 }, { 0x54, 0x1f, 2 }, { 0x55, 0x23, 2 },
499 { 0x56, 0x45, 2 }, { 0x57, 0x67, 2 }, { 0x58, 0x08, 2 },
500 { 0x59, 0x08, 2 }, { 0x5a, 0x08, 2 }, { 0x5b, 0x08, 2 },
501 { 0x60, 0x08, 2 }, { 0x61, 0x08, 2 }, { 0x62, 0x08, 2 },
502 { 0x63, 0x08, 2 }, { 0x64, 0xcf, 2 }, { 0x72, 0x56, 2 },
503 { 0x73, 0x9a, 2 }, { 0x34, 0xf0, 0 }, { 0x35, 0x0f, 0 },
504 { 0x5b, 0x40, 0 }, { 0x84, 0x88, 0 }, { 0x85, 0x24, 0 },
505 { 0x88, 0x54, 0 }, { 0x8b, 0xb8, 0 }, { 0x8c, 0x07, 0 },
506 { 0x8d, 0x00, 0 }, { 0x94, 0x1b, 0 }, { 0x95, 0x12, 0 },
507 { 0x96, 0x00, 0 }, { 0x97, 0x06, 0 }, { 0x9d, 0x1a, 0 },
508 { 0x9f, 0x10, 0 }, { 0xb4, 0x22, 0 }, { 0xbe, 0x80, 0 },
509 { 0xdb, 0x00, 0 }, { 0xee, 0x00, 0 }, { 0x91, 0x03, 0 },
510 { 0x4c, 0x00, 2 }, { 0x9f, 0x00, 3 }, { 0x8c, 0x01, 0 },
511 { 0x8d, 0x10, 0 }, { 0x8e, 0x08, 0 }, { 0x8f, 0x00, 0 }
514 static usb_callback_t urtw_bulk_rx_callback;
515 static usb_callback_t urtw_bulk_tx_callback;
516 static usb_callback_t urtw_bulk_tx_status_callback;
518 static const struct usb_config urtw_8187b_usbconfig[URTW_8187B_N_XFERS] = {
519 [URTW_8187B_BULK_RX] = {
522 .direction = UE_DIR_IN,
529 .callback = urtw_bulk_rx_callback
531 [URTW_8187B_BULK_TX_STATUS] = {
534 .direction = UE_DIR_IN,
535 .bufsize = sizeof(uint64_t),
540 .callback = urtw_bulk_tx_status_callback
542 [URTW_8187B_BULK_TX_BE] = {
544 .endpoint = URTW_8187B_TXPIPE_BE,
545 .direction = UE_DIR_OUT,
546 .bufsize = URTW_TX_MAXSIZE * URTW_TX_DATA_LIST_COUNT,
548 .force_short_xfer = 1,
551 .callback = urtw_bulk_tx_callback,
552 .timeout = URTW_DATA_TIMEOUT
554 [URTW_8187B_BULK_TX_BK] = {
556 .endpoint = URTW_8187B_TXPIPE_BK,
557 .direction = UE_DIR_OUT,
558 .bufsize = URTW_TX_MAXSIZE,
561 .force_short_xfer = 1,
564 .callback = urtw_bulk_tx_callback,
565 .timeout = URTW_DATA_TIMEOUT
567 [URTW_8187B_BULK_TX_VI] = {
569 .endpoint = URTW_8187B_TXPIPE_VI,
570 .direction = UE_DIR_OUT,
571 .bufsize = URTW_TX_MAXSIZE,
574 .force_short_xfer = 1,
577 .callback = urtw_bulk_tx_callback,
578 .timeout = URTW_DATA_TIMEOUT
580 [URTW_8187B_BULK_TX_VO] = {
582 .endpoint = URTW_8187B_TXPIPE_VO,
583 .direction = UE_DIR_OUT,
584 .bufsize = URTW_TX_MAXSIZE,
587 .force_short_xfer = 1,
590 .callback = urtw_bulk_tx_callback,
591 .timeout = URTW_DATA_TIMEOUT
593 [URTW_8187B_BULK_TX_EP12] = {
596 .direction = UE_DIR_OUT,
597 .bufsize = URTW_TX_MAXSIZE,
600 .force_short_xfer = 1,
603 .callback = urtw_bulk_tx_callback,
604 .timeout = URTW_DATA_TIMEOUT
608 static const struct usb_config urtw_8187l_usbconfig[URTW_8187L_N_XFERS] = {
609 [URTW_8187L_BULK_RX] = {
612 .direction = UE_DIR_IN,
619 .callback = urtw_bulk_rx_callback
621 [URTW_8187L_BULK_TX_LOW] = {
624 .direction = UE_DIR_OUT,
625 .bufsize = URTW_TX_MAXSIZE * URTW_TX_DATA_LIST_COUNT,
627 .force_short_xfer = 1,
630 .callback = urtw_bulk_tx_callback,
631 .timeout = URTW_DATA_TIMEOUT
633 [URTW_8187L_BULK_TX_NORMAL] = {
636 .direction = UE_DIR_OUT,
637 .bufsize = URTW_TX_MAXSIZE,
640 .force_short_xfer = 1,
643 .callback = urtw_bulk_tx_callback,
644 .timeout = URTW_DATA_TIMEOUT
648 static struct ieee80211vap *urtw_vap_create(struct ieee80211com *,
649 const char [IFNAMSIZ], int, enum ieee80211_opmode,
650 int, const uint8_t [IEEE80211_ADDR_LEN],
651 const uint8_t [IEEE80211_ADDR_LEN]);
652 static void urtw_vap_delete(struct ieee80211vap *);
653 static void urtw_init(void *);
654 static void urtw_stop(struct ifnet *);
655 static void urtw_stop_locked(struct ifnet *);
656 static int urtw_ioctl(struct ifnet *, u_long, caddr_t);
657 static void urtw_start(struct ifnet *);
658 static int urtw_alloc_rx_data_list(struct urtw_softc *);
659 static int urtw_alloc_tx_data_list(struct urtw_softc *);
660 static int urtw_raw_xmit(struct ieee80211_node *, struct mbuf *,
661 const struct ieee80211_bpf_params *);
662 static void urtw_scan_start(struct ieee80211com *);
663 static void urtw_scan_end(struct ieee80211com *);
664 static void urtw_set_channel(struct ieee80211com *);
665 static void urtw_update_mcast(struct ifnet *);
666 static int urtw_tx_start(struct urtw_softc *,
667 struct ieee80211_node *, struct mbuf *,
668 struct urtw_data *, int);
669 static int urtw_newstate(struct ieee80211vap *,
670 enum ieee80211_state, int);
671 static void urtw_led_ch(void *);
672 static void urtw_ledtask(void *, int);
673 static void urtw_watchdog(void *);
674 static void urtw_set_multi(void *);
675 static int urtw_isbmode(uint16_t);
676 static uint16_t urtw_rate2rtl(uint32_t);
677 static uint16_t urtw_rtl2rate(uint32_t);
678 static usb_error_t urtw_set_rate(struct urtw_softc *);
679 static usb_error_t urtw_update_msr(struct urtw_softc *);
680 static usb_error_t urtw_read8_c(struct urtw_softc *, int, uint8_t *);
681 static usb_error_t urtw_read16_c(struct urtw_softc *, int, uint16_t *);
682 static usb_error_t urtw_read32_c(struct urtw_softc *, int, uint32_t *);
683 static usb_error_t urtw_write8_c(struct urtw_softc *, int, uint8_t);
684 static usb_error_t urtw_write16_c(struct urtw_softc *, int, uint16_t);
685 static usb_error_t urtw_write32_c(struct urtw_softc *, int, uint32_t);
686 static usb_error_t urtw_eprom_cs(struct urtw_softc *, int);
687 static usb_error_t urtw_eprom_ck(struct urtw_softc *);
688 static usb_error_t urtw_eprom_sendbits(struct urtw_softc *, int16_t *,
690 static usb_error_t urtw_eprom_read32(struct urtw_softc *, uint32_t,
692 static usb_error_t urtw_eprom_readbit(struct urtw_softc *, int16_t *);
693 static usb_error_t urtw_eprom_writebit(struct urtw_softc *, int16_t);
694 static usb_error_t urtw_get_macaddr(struct urtw_softc *);
695 static usb_error_t urtw_get_txpwr(struct urtw_softc *);
696 static usb_error_t urtw_get_rfchip(struct urtw_softc *);
697 static usb_error_t urtw_led_init(struct urtw_softc *);
698 static usb_error_t urtw_8185_rf_pins_enable(struct urtw_softc *);
699 static usb_error_t urtw_8185_tx_antenna(struct urtw_softc *, uint8_t);
700 static usb_error_t urtw_8187_write_phy(struct urtw_softc *, uint8_t,
702 static usb_error_t urtw_8187_write_phy_ofdm_c(struct urtw_softc *,
704 static usb_error_t urtw_8187_write_phy_cck_c(struct urtw_softc *, uint8_t,
706 static usb_error_t urtw_8225_setgain(struct urtw_softc *, int16_t);
707 static usb_error_t urtw_8225_usb_init(struct urtw_softc *);
708 static usb_error_t urtw_8225_write_c(struct urtw_softc *, uint8_t,
710 static usb_error_t urtw_8225_write_s16(struct urtw_softc *, uint8_t, int,
712 static usb_error_t urtw_8225_read(struct urtw_softc *, uint8_t,
714 static usb_error_t urtw_8225_rf_init(struct urtw_softc *);
715 static usb_error_t urtw_8225_rf_set_chan(struct urtw_softc *, int);
716 static usb_error_t urtw_8225_rf_set_sens(struct urtw_softc *, int);
717 static usb_error_t urtw_8225_set_txpwrlvl(struct urtw_softc *, int);
718 static usb_error_t urtw_8225_rf_stop(struct urtw_softc *);
719 static usb_error_t urtw_8225v2_rf_init(struct urtw_softc *);
720 static usb_error_t urtw_8225v2_rf_set_chan(struct urtw_softc *, int);
721 static usb_error_t urtw_8225v2_set_txpwrlvl(struct urtw_softc *, int);
722 static usb_error_t urtw_8225v2_setgain(struct urtw_softc *, int16_t);
723 static usb_error_t urtw_8225_isv2(struct urtw_softc *, int *);
724 static usb_error_t urtw_8225v2b_rf_init(struct urtw_softc *);
725 static usb_error_t urtw_8225v2b_rf_set_chan(struct urtw_softc *, int);
726 static usb_error_t urtw_read8e(struct urtw_softc *, int, uint8_t *);
727 static usb_error_t urtw_write8e(struct urtw_softc *, int, uint8_t);
728 static usb_error_t urtw_8180_set_anaparam(struct urtw_softc *, uint32_t);
729 static usb_error_t urtw_8185_set_anaparam2(struct urtw_softc *, uint32_t);
730 static usb_error_t urtw_intr_enable(struct urtw_softc *);
731 static usb_error_t urtw_intr_disable(struct urtw_softc *);
732 static usb_error_t urtw_reset(struct urtw_softc *);
733 static usb_error_t urtw_led_on(struct urtw_softc *, int);
734 static usb_error_t urtw_led_ctl(struct urtw_softc *, int);
735 static usb_error_t urtw_led_blink(struct urtw_softc *);
736 static usb_error_t urtw_led_mode0(struct urtw_softc *, int);
737 static usb_error_t urtw_led_mode1(struct urtw_softc *, int);
738 static usb_error_t urtw_led_mode2(struct urtw_softc *, int);
739 static usb_error_t urtw_led_mode3(struct urtw_softc *, int);
740 static usb_error_t urtw_rx_setconf(struct urtw_softc *);
741 static usb_error_t urtw_rx_enable(struct urtw_softc *);
742 static usb_error_t urtw_tx_enable(struct urtw_softc *sc);
743 static void urtw_free_tx_data_list(struct urtw_softc *);
744 static void urtw_free_rx_data_list(struct urtw_softc *);
745 static void urtw_free_data_list(struct urtw_softc *,
746 struct urtw_data data[], int, int);
747 static usb_error_t urtw_adapter_start(struct urtw_softc *);
748 static usb_error_t urtw_adapter_start_b(struct urtw_softc *);
749 static usb_error_t urtw_set_mode(struct urtw_softc *, uint32_t);
750 static usb_error_t urtw_8187b_cmd_reset(struct urtw_softc *);
751 static usb_error_t urtw_do_request(struct urtw_softc *,
752 struct usb_device_request *, void *);
753 static usb_error_t urtw_8225v2b_set_txpwrlvl(struct urtw_softc *, int);
754 static usb_error_t urtw_led_off(struct urtw_softc *, int);
755 static void urtw_abort_xfers(struct urtw_softc *);
756 static struct urtw_data *
757 urtw_getbuf(struct urtw_softc *sc);
758 static int urtw_compute_txtime(uint16_t, uint16_t, uint8_t,
760 static void urtw_updateslot(struct ifnet *);
761 static void urtw_updateslottask(void *, int);
762 static void urtw_sysctl_node(struct urtw_softc *);
765 urtw_match(device_t dev)
767 struct usb_attach_arg *uaa = device_get_ivars(dev);
769 if (uaa->usb_mode != USB_MODE_HOST)
771 if (uaa->info.bConfigIndex != URTW_CONFIG_INDEX)
773 if (uaa->info.bIfaceIndex != URTW_IFACE_INDEX)
776 return (usbd_lookup_id_by_uaa(urtw_devs, sizeof(urtw_devs), uaa));
780 urtw_attach(device_t dev)
782 const struct usb_config *setup_start;
784 struct urtw_softc *sc = device_get_softc(dev);
785 struct usb_attach_arg *uaa = device_get_ivars(dev);
786 struct ieee80211com *ic;
788 uint8_t bands, iface_index = URTW_IFACE_INDEX; /* XXX */
793 device_set_usb_desc(dev);
796 sc->sc_udev = uaa->device;
797 if (USB_GET_DRIVER_INFO(uaa) == URTW_REV_RTL8187B)
798 sc->sc_flags |= URTW_RTL8187B;
800 sc->sc_debug = urtw_debug;
803 mtx_init(&sc->sc_mtx, device_get_nameunit(sc->sc_dev), MTX_NETWORK_LOCK,
805 usb_callout_init_mtx(&sc->sc_led_ch, &sc->sc_mtx, 0);
806 TASK_INIT(&sc->sc_led_task, 0, urtw_ledtask, sc);
807 TASK_INIT(&sc->sc_updateslot_task, 0, urtw_updateslottask, sc);
808 callout_init(&sc->sc_watchdog_ch, 0);
810 if (sc->sc_flags & URTW_RTL8187B) {
811 setup_start = urtw_8187b_usbconfig;
812 n_setup = URTW_8187B_N_XFERS;
814 setup_start = urtw_8187l_usbconfig;
815 n_setup = URTW_8187L_N_XFERS;
818 error = usbd_transfer_setup(uaa->device, &iface_index, sc->sc_xfer,
819 setup_start, n_setup, sc, &sc->sc_mtx);
821 device_printf(dev, "could not allocate USB transfers, "
822 "err=%s\n", usbd_errstr(error));
827 if (sc->sc_flags & URTW_RTL8187B) {
829 usbd_xfer_get_frame_buffer(sc->sc_xfer[
830 URTW_8187B_BULK_TX_BE], 0);
833 usbd_xfer_get_frame_buffer(sc->sc_xfer[
834 URTW_8187L_BULK_TX_LOW], 0);
839 urtw_read32_m(sc, URTW_RX, &data);
840 sc->sc_epromtype = (data & URTW_RX_9356SEL) ? URTW_EEPROM_93C56 :
843 error = urtw_get_rfchip(sc);
846 error = urtw_get_macaddr(sc);
849 error = urtw_get_txpwr(sc);
852 error = urtw_led_init(sc);
858 sc->sc_rts_retry = URTW_DEFAULT_RTS_RETRY;
859 sc->sc_tx_retry = URTW_DEFAULT_TX_RETRY;
861 sc->sc_preamble_mode = urtw_preamble_mode;
863 ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211);
865 device_printf(sc->sc_dev, "can not allocate ifnet\n");
871 if_initname(ifp, "urtw", device_get_unit(sc->sc_dev));
872 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
873 ifp->if_init = urtw_init;
874 ifp->if_ioctl = urtw_ioctl;
875 ifp->if_start = urtw_start;
876 /* XXX URTW_TX_DATA_LIST_COUNT */
877 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
878 ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
879 IFQ_SET_READY(&ifp->if_snd);
883 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
884 ic->ic_opmode = IEEE80211_M_STA; /* default to BSS mode */
886 /* set device capabilities */
888 IEEE80211_C_STA | /* station mode */
889 IEEE80211_C_MONITOR | /* monitor mode supported */
890 IEEE80211_C_TXPMGT | /* tx power management */
891 IEEE80211_C_SHPREAMBLE | /* short preamble supported */
892 IEEE80211_C_SHSLOT | /* short slot time supported */
893 IEEE80211_C_BGSCAN | /* capable of bg scanning */
894 IEEE80211_C_WPA; /* 802.11i */
897 setbit(&bands, IEEE80211_MODE_11B);
898 setbit(&bands, IEEE80211_MODE_11G);
899 ieee80211_init_channels(ic, NULL, &bands);
901 ieee80211_ifattach(ic, sc->sc_bssid);
902 ic->ic_raw_xmit = urtw_raw_xmit;
903 ic->ic_scan_start = urtw_scan_start;
904 ic->ic_scan_end = urtw_scan_end;
905 ic->ic_set_channel = urtw_set_channel;
906 ic->ic_updateslot = urtw_updateslot;
907 ic->ic_vap_create = urtw_vap_create;
908 ic->ic_vap_delete = urtw_vap_delete;
909 ic->ic_update_mcast = urtw_update_mcast;
911 ieee80211_radiotap_attach(ic,
912 &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap),
913 URTW_TX_RADIOTAP_PRESENT,
914 &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
915 URTW_RX_RADIOTAP_PRESENT);
917 urtw_sysctl_node(sc);
920 ieee80211_announce(ic);
923 fail: URTW_UNLOCK(sc);
924 fail1: usbd_transfer_unsetup(sc->sc_xfer, (sc->sc_flags & URTW_RTL8187B) ?
925 URTW_8187B_N_XFERS : URTW_8187L_N_XFERS);
931 urtw_detach(device_t dev)
933 struct urtw_softc *sc = device_get_softc(dev);
934 struct ifnet *ifp = sc->sc_ifp;
935 struct ieee80211com *ic = ifp->if_l2com;
937 unsigned int n_xfers;
939 /* Prevent further ioctls */
941 sc->sc_flags |= URTW_DETACHED;
946 ieee80211_draintask(ic, &sc->sc_updateslot_task);
947 ieee80211_draintask(ic, &sc->sc_led_task);
949 usb_callout_drain(&sc->sc_led_ch);
950 callout_drain(&sc->sc_watchdog_ch);
952 n_xfers = (sc->sc_flags & URTW_RTL8187B) ?
953 URTW_8187B_N_XFERS : URTW_8187L_N_XFERS;
955 /* prevent further allocations from RX/TX data lists */
957 STAILQ_INIT(&sc->sc_tx_active);
958 STAILQ_INIT(&sc->sc_tx_inactive);
959 STAILQ_INIT(&sc->sc_tx_pending);
961 STAILQ_INIT(&sc->sc_rx_active);
962 STAILQ_INIT(&sc->sc_rx_inactive);
965 /* drain USB transfers */
966 for (x = 0; x != n_xfers; x++)
967 usbd_transfer_drain(sc->sc_xfer[x]);
969 /* free data buffers */
971 urtw_free_tx_data_list(sc);
972 urtw_free_rx_data_list(sc);
975 /* free USB transfers and some data buffers */
976 usbd_transfer_unsetup(sc->sc_xfer, n_xfers);
978 ieee80211_ifdetach(ic);
980 mtx_destroy(&sc->sc_mtx);
985 urtw_free_tx_data_list(struct urtw_softc *sc)
987 urtw_free_data_list(sc, sc->sc_tx, URTW_TX_DATA_LIST_COUNT, 0);
991 urtw_free_rx_data_list(struct urtw_softc *sc)
993 urtw_free_data_list(sc, sc->sc_rx, URTW_RX_DATA_LIST_COUNT, 1);
997 urtw_free_data_list(struct urtw_softc *sc, struct urtw_data data[], int ndata,
1002 for (i = 0; i < ndata; i++) {
1003 struct urtw_data *dp = &data[i];
1005 if (fillmbuf == 1) {
1006 if (dp->m != NULL) {
1014 if (dp->ni != NULL) {
1015 ieee80211_free_node(dp->ni);
1021 static struct ieee80211vap *
1022 urtw_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
1023 enum ieee80211_opmode opmode, int flags,
1024 const uint8_t bssid[IEEE80211_ADDR_LEN],
1025 const uint8_t mac[IEEE80211_ADDR_LEN])
1027 struct urtw_vap *uvp;
1028 struct ieee80211vap *vap;
1030 if (!TAILQ_EMPTY(&ic->ic_vaps)) /* only one at a time */
1032 uvp = (struct urtw_vap *) malloc(sizeof(struct urtw_vap),
1033 M_80211_VAP, M_NOWAIT | M_ZERO);
1037 /* enable s/w bmiss handling for sta mode */
1039 if (ieee80211_vap_setup(ic, vap, name, unit, opmode,
1040 flags | IEEE80211_CLONE_NOBEACONS, bssid, mac) != 0) {
1042 free(uvp, M_80211_VAP);
1046 /* override state transition machine */
1047 uvp->newstate = vap->iv_newstate;
1048 vap->iv_newstate = urtw_newstate;
1050 /* complete setup */
1051 ieee80211_vap_attach(vap, ieee80211_media_change,
1052 ieee80211_media_status);
1053 ic->ic_opmode = opmode;
1058 urtw_vap_delete(struct ieee80211vap *vap)
1060 struct urtw_vap *uvp = URTW_VAP(vap);
1062 ieee80211_vap_detach(vap);
1063 free(uvp, M_80211_VAP);
1067 urtw_init_locked(void *arg)
1070 struct urtw_softc *sc = arg;
1071 struct ifnet *ifp = sc->sc_ifp;
1074 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1075 urtw_stop_locked(ifp);
1077 error = (sc->sc_flags & URTW_RTL8187B) ? urtw_adapter_start_b(sc) :
1078 urtw_adapter_start(sc);
1082 /* reset softc variables */
1085 if (!(sc->sc_flags & URTW_INIT_ONCE)) {
1086 ret = urtw_alloc_rx_data_list(sc);
1089 ret = urtw_alloc_tx_data_list(sc);
1092 sc->sc_flags |= URTW_INIT_ONCE;
1095 error = urtw_rx_enable(sc);
1098 error = urtw_tx_enable(sc);
1102 if (sc->sc_flags & URTW_RTL8187B)
1103 usbd_transfer_start(sc->sc_xfer[URTW_8187B_BULK_TX_STATUS]);
1105 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1106 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1108 callout_reset(&sc->sc_watchdog_ch, hz, urtw_watchdog, sc);
1114 urtw_init(void *arg)
1116 struct urtw_softc *sc = arg;
1119 urtw_init_locked(arg);
1124 urtw_adapter_start_b(struct urtw_softc *sc)
1126 #define N(a) ((int)(sizeof(a) / sizeof((a)[0])))
1130 error = urtw_set_mode(sc, URTW_EPROM_CMD_CONFIG);
1134 urtw_read8_m(sc, URTW_CONFIG3, &data8);
1135 urtw_write8_m(sc, URTW_CONFIG3,
1136 data8 | URTW_CONFIG3_ANAPARAM_WRITE | URTW_CONFIG3_GNT_SELECT);
1137 urtw_write32_m(sc, URTW_ANAPARAM2, URTW_8187B_8225_ANAPARAM2_ON);
1138 urtw_write32_m(sc, URTW_ANAPARAM, URTW_8187B_8225_ANAPARAM_ON);
1139 urtw_write8_m(sc, URTW_ANAPARAM3, URTW_8187B_8225_ANAPARAM3_ON);
1141 urtw_write8_m(sc, 0x61, 0x10);
1142 urtw_read8_m(sc, 0x62, &data8);
1143 urtw_write8_m(sc, 0x62, data8 & ~(1 << 5));
1144 urtw_write8_m(sc, 0x62, data8 | (1 << 5));
1146 urtw_read8_m(sc, URTW_CONFIG3, &data8);
1147 data8 &= ~URTW_CONFIG3_ANAPARAM_WRITE;
1148 urtw_write8_m(sc, URTW_CONFIG3, data8);
1150 error = urtw_set_mode(sc, URTW_EPROM_CMD_NORMAL);
1154 error = urtw_8187b_cmd_reset(sc);
1158 error = sc->sc_rf_init(sc);
1161 urtw_write8_m(sc, URTW_CMD, URTW_CMD_RX_ENABLE | URTW_CMD_TX_ENABLE);
1163 /* fix RTL8187B RX stall */
1164 error = urtw_intr_enable(sc);
1168 error = urtw_write8e(sc, 0x41, 0xf4);
1171 error = urtw_write8e(sc, 0x40, 0x00);
1174 error = urtw_write8e(sc, 0x42, 0x00);
1177 error = urtw_write8e(sc, 0x42, 0x01);
1180 error = urtw_write8e(sc, 0x40, 0x0f);
1183 error = urtw_write8e(sc, 0x42, 0x00);
1186 error = urtw_write8e(sc, 0x42, 0x01);
1190 urtw_read8_m(sc, 0xdb, &data8);
1191 urtw_write8_m(sc, 0xdb, data8 | (1 << 2));
1192 urtw_write16_m(sc, 0x372, 0x59fa);
1193 urtw_write16_m(sc, 0x374, 0x59d2);
1194 urtw_write16_m(sc, 0x376, 0x59d2);
1195 urtw_write16_m(sc, 0x378, 0x19fa);
1196 urtw_write16_m(sc, 0x37a, 0x19fa);
1197 urtw_write16_m(sc, 0x37c, 0x00d0);
1198 urtw_write8_m(sc, 0x61, 0);
1200 urtw_write8_m(sc, 0x180, 0x0f);
1201 urtw_write8_m(sc, 0x183, 0x03);
1202 urtw_write8_m(sc, 0xda, 0x10);
1203 urtw_write8_m(sc, 0x24d, 0x08);
1204 urtw_write32_m(sc, URTW_HSSI_PARA, 0x0600321b);
1206 urtw_write16_m(sc, 0x1ec, 0x800); /* RX MAX SIZE */
1213 urtw_adapter_start(struct urtw_softc *sc)
1217 error = urtw_reset(sc);
1221 urtw_write8_m(sc, URTW_ADDR_MAGIC1, 0);
1222 urtw_write8_m(sc, URTW_GPIO, 0);
1225 urtw_write8_m(sc, URTW_ADDR_MAGIC1, 4);
1226 error = urtw_led_ctl(sc, URTW_LED_CTL_POWER_ON);
1230 error = urtw_set_mode(sc, URTW_EPROM_CMD_CONFIG);
1233 /* applying MAC address again. */
1234 urtw_write32_m(sc, URTW_MAC0, ((uint32_t *)sc->sc_bssid)[0]);
1235 urtw_write16_m(sc, URTW_MAC4, ((uint32_t *)sc->sc_bssid)[1] & 0xffff);
1236 error = urtw_set_mode(sc, URTW_EPROM_CMD_NORMAL);
1240 error = urtw_update_msr(sc);
1244 urtw_write32_m(sc, URTW_INT_TIMEOUT, 0);
1245 urtw_write8_m(sc, URTW_WPA_CONFIG, 0);
1246 urtw_write8_m(sc, URTW_RATE_FALLBACK, URTW_RATE_FALLBACK_ENABLE | 0x1);
1247 error = urtw_set_rate(sc);
1251 error = sc->sc_rf_init(sc);
1254 if (sc->sc_rf_set_sens != NULL)
1255 sc->sc_rf_set_sens(sc, sc->sc_sens);
1257 /* XXX correct? to call write16 */
1258 urtw_write16_m(sc, URTW_PSR, 1);
1259 urtw_write16_m(sc, URTW_ADDR_MAGIC2, 0x10);
1260 urtw_write8_m(sc, URTW_TALLY_SEL, 0x80);
1261 urtw_write8_m(sc, URTW_ADDR_MAGIC3, 0x60);
1262 /* XXX correct? to call write16 */
1263 urtw_write16_m(sc, URTW_PSR, 0);
1264 urtw_write8_m(sc, URTW_ADDR_MAGIC1, 4);
1266 error = urtw_intr_enable(sc);
1275 urtw_set_mode(struct urtw_softc *sc, uint32_t mode)
1280 urtw_read8_m(sc, URTW_EPROM_CMD, &data);
1281 data = (data & ~URTW_EPROM_CMD_MASK) | (mode << URTW_EPROM_CMD_SHIFT);
1282 data = data & ~(URTW_EPROM_CS | URTW_EPROM_CK);
1283 urtw_write8_m(sc, URTW_EPROM_CMD, data);
1289 urtw_8187b_cmd_reset(struct urtw_softc *sc)
1295 /* XXX the code can be duplicate with urtw_reset(). */
1296 urtw_read8_m(sc, URTW_CMD, &data8);
1297 data8 = (data8 & 0x2) | URTW_CMD_RST;
1298 urtw_write8_m(sc, URTW_CMD, data8);
1300 for (i = 0; i < 20; i++) {
1301 usb_pause_mtx(&sc->sc_mtx, 2);
1302 urtw_read8_m(sc, URTW_CMD, &data8);
1303 if (!(data8 & URTW_CMD_RST))
1307 device_printf(sc->sc_dev, "reset timeout\n");
1315 urtw_do_request(struct urtw_softc *sc,
1316 struct usb_device_request *req, void *data)
1321 URTW_ASSERT_LOCKED(sc);
1324 err = usbd_do_request_flags(sc->sc_udev, &sc->sc_mtx,
1325 req, data, 0, NULL, 250 /* ms */);
1329 DPRINTF(sc, URTW_DEBUG_INIT,
1330 "Control request failed, %s (retrying)\n",
1332 usb_pause_mtx(&sc->sc_mtx, hz / 100);
1338 urtw_stop_locked(struct ifnet *ifp)
1340 struct urtw_softc *sc = ifp->if_softc;
1344 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
1346 error = urtw_intr_disable(sc);
1349 urtw_read8_m(sc, URTW_CMD, &data8);
1350 data8 &= ~(URTW_CMD_RX_ENABLE | URTW_CMD_TX_ENABLE);
1351 urtw_write8_m(sc, URTW_CMD, data8);
1353 error = sc->sc_rf_stop(sc);
1357 error = urtw_set_mode(sc, URTW_EPROM_CMD_CONFIG);
1360 urtw_read8_m(sc, URTW_CONFIG4, &data8);
1361 urtw_write8_m(sc, URTW_CONFIG4, data8 | URTW_CONFIG4_VCOOFF);
1362 error = urtw_set_mode(sc, URTW_EPROM_CMD_NORMAL);
1367 device_printf(sc->sc_dev, "failed to stop (%s)\n",
1368 usbd_errstr(error));
1370 usb_callout_stop(&sc->sc_led_ch);
1371 callout_stop(&sc->sc_watchdog_ch);
1373 urtw_abort_xfers(sc);
1377 urtw_stop(struct ifnet *ifp)
1379 struct urtw_softc *sc = ifp->if_softc;
1382 urtw_stop_locked(ifp);
1387 urtw_abort_xfers(struct urtw_softc *sc)
1391 URTW_ASSERT_LOCKED(sc);
1393 max = (sc->sc_flags & URTW_RTL8187B) ? URTW_8187B_N_XFERS :
1396 /* abort any pending transfers */
1397 for (i = 0; i < max; i++)
1398 usbd_transfer_stop(sc->sc_xfer[i]);
1402 urtw_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1404 struct urtw_softc *sc = ifp->if_softc;
1405 struct ieee80211com *ic = ifp->if_l2com;
1406 struct ifreq *ifr = (struct ifreq *) data;
1411 error = (sc->sc_flags & URTW_DETACHED) ? ENXIO : 0;
1418 if (ifp->if_flags & IFF_UP) {
1419 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1420 if ((ifp->if_flags ^ sc->sc_if_flags) &
1421 (IFF_ALLMULTI | IFF_PROMISC))
1424 urtw_init(ifp->if_softc);
1428 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1431 sc->sc_if_flags = ifp->if_flags;
1433 ieee80211_start_all(ic);
1436 error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
1439 error = ether_ioctl(ifp, cmd, data);
1449 urtw_start(struct ifnet *ifp)
1451 struct urtw_data *bf;
1452 struct urtw_softc *sc = ifp->if_softc;
1453 struct ieee80211_node *ni;
1456 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
1461 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
1464 bf = urtw_getbuf(sc);
1466 IFQ_DRV_PREPEND(&ifp->if_snd, m);
1470 ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
1471 m->m_pkthdr.rcvif = NULL;
1473 if (urtw_tx_start(sc, ni, m, bf, URTW_PRIORITY_NORMAL) != 0) {
1475 STAILQ_INSERT_HEAD(&sc->sc_tx_inactive, bf, next);
1476 ieee80211_free_node(ni);
1481 callout_reset(&sc->sc_watchdog_ch, hz, urtw_watchdog, sc);
1487 urtw_alloc_data_list(struct urtw_softc *sc, struct urtw_data data[],
1488 int ndata, int maxsz, void *dma_buf)
1492 for (i = 0; i < ndata; i++) {
1493 struct urtw_data *dp = &data[i];
1496 if (dma_buf == NULL) {
1497 dp->m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1498 if (dp->m == NULL) {
1499 device_printf(sc->sc_dev,
1500 "could not allocate rx mbuf\n");
1504 dp->buf = mtod(dp->m, uint8_t *);
1507 dp->buf = ((uint8_t *)dma_buf) +
1514 fail: urtw_free_data_list(sc, data, ndata, 1);
1519 urtw_alloc_rx_data_list(struct urtw_softc *sc)
1523 error = urtw_alloc_data_list(sc,
1524 sc->sc_rx, URTW_RX_DATA_LIST_COUNT,
1525 MCLBYTES, NULL /* mbufs */);
1529 STAILQ_INIT(&sc->sc_rx_active);
1530 STAILQ_INIT(&sc->sc_rx_inactive);
1532 for (i = 0; i < URTW_RX_DATA_LIST_COUNT; i++)
1533 STAILQ_INSERT_HEAD(&sc->sc_rx_inactive, &sc->sc_rx[i], next);
1539 urtw_alloc_tx_data_list(struct urtw_softc *sc)
1543 error = urtw_alloc_data_list(sc,
1544 sc->sc_tx, URTW_TX_DATA_LIST_COUNT, URTW_TX_MAXSIZE,
1545 sc->sc_tx_dma_buf /* no mbufs */);
1549 STAILQ_INIT(&sc->sc_tx_active);
1550 STAILQ_INIT(&sc->sc_tx_inactive);
1551 STAILQ_INIT(&sc->sc_tx_pending);
1553 for (i = 0; i < URTW_TX_DATA_LIST_COUNT; i++)
1554 STAILQ_INSERT_HEAD(&sc->sc_tx_inactive, &sc->sc_tx[i],
1561 urtw_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
1562 const struct ieee80211_bpf_params *params)
1564 struct ieee80211com *ic = ni->ni_ic;
1565 struct ifnet *ifp = ic->ic_ifp;
1566 struct urtw_data *bf;
1567 struct urtw_softc *sc = ifp->if_softc;
1569 /* prevent management frames from being sent if we're not ready */
1570 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1572 ieee80211_free_node(ni);
1576 bf = urtw_getbuf(sc);
1578 ieee80211_free_node(ni);
1581 return (ENOBUFS); /* XXX */
1585 if (urtw_tx_start(sc, ni, m, bf, URTW_PRIORITY_LOW) != 0) {
1586 ieee80211_free_node(ni);
1588 STAILQ_INSERT_HEAD(&sc->sc_tx_inactive, bf, next);
1599 urtw_scan_start(struct ieee80211com *ic)
1602 /* XXX do nothing? */
1606 urtw_scan_end(struct ieee80211com *ic)
1609 /* XXX do nothing? */
1613 urtw_set_channel(struct ieee80211com *ic)
1615 struct urtw_softc *sc = ic->ic_ifp->if_softc;
1616 struct ifnet *ifp = sc->sc_ifp;
1617 uint32_t data, orig;
1621 * if the user set a channel explicitly using ifconfig(8) this function
1622 * can be called earlier than we're expected that in some cases the
1623 * initialization would be failed if setting a channel is called before
1624 * the init have done.
1626 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
1629 if (sc->sc_curchan != NULL && sc->sc_curchan == ic->ic_curchan)
1635 * during changing th channel we need to temporarily be disable
1638 urtw_read32_m(sc, URTW_TX_CONF, &orig);
1639 data = orig & ~URTW_TX_LOOPBACK_MASK;
1640 urtw_write32_m(sc, URTW_TX_CONF, data | URTW_TX_LOOPBACK_MAC);
1642 error = sc->sc_rf_set_chan(sc, ieee80211_chan2ieee(ic, ic->ic_curchan));
1645 usb_pause_mtx(&sc->sc_mtx, 10);
1646 urtw_write32_m(sc, URTW_TX_CONF, orig);
1648 urtw_write16_m(sc, URTW_ATIM_WND, 2);
1649 urtw_write16_m(sc, URTW_ATIM_TR_ITV, 100);
1650 urtw_write16_m(sc, URTW_BEACON_INTERVAL, 100);
1651 urtw_write16_m(sc, URTW_BEACON_INTERVAL_TIME, 100);
1656 sc->sc_curchan = ic->ic_curchan;
1659 device_printf(sc->sc_dev, "could not change the channel\n");
1663 urtw_update_mcast(struct ifnet *ifp)
1666 /* XXX do nothing? */
1670 urtw_tx_start(struct urtw_softc *sc, struct ieee80211_node *ni, struct mbuf *m0,
1671 struct urtw_data *data, int prior)
1673 struct ifnet *ifp = sc->sc_ifp;
1674 struct ieee80211_frame *wh = mtod(m0, struct ieee80211_frame *);
1675 struct ieee80211_key *k;
1676 const struct ieee80211_txparam *tp;
1677 struct ieee80211com *ic = ifp->if_l2com;
1678 struct ieee80211vap *vap = ni->ni_vap;
1679 struct usb_xfer *rtl8187b_pipes[URTW_8187B_TXPIPE_MAX] = {
1680 sc->sc_xfer[URTW_8187B_BULK_TX_BE],
1681 sc->sc_xfer[URTW_8187B_BULK_TX_BK],
1682 sc->sc_xfer[URTW_8187B_BULK_TX_VI],
1683 sc->sc_xfer[URTW_8187B_BULK_TX_VO]
1685 struct usb_xfer *xfer;
1686 int dur = 0, rtsdur = 0, rtsenable = 0, ctsenable = 0, rate,
1687 pkttime = 0, txdur = 0, isshort = 0, xferlen;
1688 uint16_t acktime, rtstime, ctstime;
1692 URTW_ASSERT_LOCKED(sc);
1697 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1698 k = ieee80211_crypto_encap(ni, m0);
1700 device_printf(sc->sc_dev,
1701 "ieee80211_crypto_encap returns NULL.\n");
1702 /* XXX we don't expect the fragmented frames */
1707 /* in case packet header moved, reset pointer */
1708 wh = mtod(m0, struct ieee80211_frame *);
1711 if (ieee80211_radiotap_active_vap(vap)) {
1712 struct urtw_tx_radiotap_header *tap = &sc->sc_txtap;
1714 /* XXX Are variables correct? */
1716 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
1717 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
1719 ieee80211_radiotap_tx(vap, m0);
1722 if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_MGT ||
1723 (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_CTL) {
1724 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
1725 rate = tp->mgmtrate;
1727 tp = &vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)];
1728 /* for data frames */
1729 if (IEEE80211_IS_MULTICAST(wh->i_addr1))
1730 rate = tp->mcastrate;
1731 else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE)
1732 rate = tp->ucastrate;
1734 rate = urtw_rtl2rate(sc->sc_currate);
1737 sc->sc_stats.txrates[sc->sc_currate]++;
1739 if (IEEE80211_IS_MULTICAST(wh->i_addr1))
1740 txdur = pkttime = urtw_compute_txtime(m0->m_pkthdr.len +
1741 IEEE80211_CRC_LEN, rate, 0, 0);
1743 acktime = urtw_compute_txtime(14, 2,0, 0);
1744 if ((m0->m_pkthdr.len + 4) > vap->iv_rtsthreshold) {
1747 rtstime = urtw_compute_txtime(URTW_ACKCTS_LEN, 2, 0, 0);
1748 ctstime = urtw_compute_txtime(14, 2, 0, 0);
1749 pkttime = urtw_compute_txtime(m0->m_pkthdr.len +
1750 IEEE80211_CRC_LEN, rate, 0, isshort);
1751 rtsdur = ctstime + pkttime + acktime +
1752 3 * URTW_ASIFS_TIME;
1753 txdur = rtstime + rtsdur;
1755 rtsenable = ctsenable = rtsdur = 0;
1756 pkttime = urtw_compute_txtime(m0->m_pkthdr.len +
1757 IEEE80211_CRC_LEN, rate, 0, isshort);
1758 txdur = pkttime + URTW_ASIFS_TIME + acktime;
1761 if (wh->i_fc[1] & IEEE80211_FC1_MORE_FRAG)
1762 dur = urtw_compute_txtime(m0->m_pkthdr.len +
1763 IEEE80211_CRC_LEN, rate, 0, isshort) +
1764 3 * URTW_ASIFS_TIME +
1767 dur = URTW_ASIFS_TIME + acktime;
1769 USETW(wh->i_dur, dur);
1771 xferlen = m0->m_pkthdr.len;
1772 xferlen += (sc->sc_flags & URTW_RTL8187B) ? (4 * 8) : (4 * 3);
1773 if ((0 == xferlen % 64) || (0 == xferlen % 512))
1776 memset(data->buf, 0, URTW_TX_MAXSIZE);
1777 flags = m0->m_pkthdr.len & 0xfff;
1778 flags |= URTW_TX_FLAG_NO_ENC;
1779 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
1780 (ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE) &&
1781 (sc->sc_preamble_mode == URTW_PREAMBLE_MODE_SHORT) &&
1782 (sc->sc_currate != 0))
1783 flags |= URTW_TX_FLAG_SPLCP;
1784 if (wh->i_fc[1] & IEEE80211_FC1_MORE_FRAG)
1785 flags |= URTW_TX_FLAG_MOREFRAG;
1787 flags |= (sc->sc_currate & 0xf) << URTW_TX_FLAG_TXRATE_SHIFT;
1789 if (sc->sc_flags & URTW_RTL8187B) {
1790 struct urtw_8187b_txhdr *tx;
1792 tx = (struct urtw_8187b_txhdr *)data->buf;
1794 flags |= URTW_TX_FLAG_CTS;
1796 flags |= URTW_TX_FLAG_RTS;
1797 flags |= (urtw_rate2rtl(11) & 0xf) <<
1798 URTW_TX_FLAG_RTSRATE_SHIFT;
1799 tx->rtsdur = rtsdur;
1801 tx->flag = htole32(flags);
1803 if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
1804 IEEE80211_FC0_TYPE_MGT &&
1805 (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) ==
1806 IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1809 tx->retry = URTW_TX_MAXRETRY;
1810 m_copydata(m0, 0, m0->m_pkthdr.len, (uint8_t *)(tx + 1));
1812 struct urtw_8187l_txhdr *tx;
1814 tx = (struct urtw_8187l_txhdr *)data->buf;
1816 flags |= URTW_TX_FLAG_RTS;
1817 tx->rtsdur = rtsdur;
1819 flags |= (urtw_rate2rtl(11) & 0xf) << URTW_TX_FLAG_RTSRATE_SHIFT;
1820 tx->flag = htole32(flags);
1821 tx->retry = 3; /* CW minimum */
1822 tx->retry = 7 << 4; /* CW maximum */
1823 tx->retry = URTW_TX_MAXRETRY << 8; /* retry limitation */
1824 m_copydata(m0, 0, m0->m_pkthdr.len, (uint8_t *)(tx + 1));
1827 data->buflen = xferlen;
1831 if (sc->sc_flags & URTW_RTL8187B) {
1832 switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
1833 case IEEE80211_FC0_TYPE_CTL:
1834 case IEEE80211_FC0_TYPE_MGT:
1835 xfer = sc->sc_xfer[URTW_8187B_BULK_TX_EP12];
1838 KASSERT(M_WME_GETAC(m0) < URTW_8187B_TXPIPE_MAX,
1839 ("unsupported WME pipe %d", M_WME_GETAC(m0)));
1840 xfer = rtl8187b_pipes[M_WME_GETAC(m0)];
1844 xfer = (prior == URTW_PRIORITY_LOW) ?
1845 sc->sc_xfer[URTW_8187L_BULK_TX_LOW] :
1846 sc->sc_xfer[URTW_8187L_BULK_TX_NORMAL];
1848 STAILQ_INSERT_TAIL(&sc->sc_tx_pending, data, next);
1849 usbd_transfer_start(xfer);
1851 error = urtw_led_ctl(sc, URTW_LED_CTL_TX);
1853 device_printf(sc->sc_dev, "could not control LED (%d)\n",
1859 urtw_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1861 struct ieee80211com *ic = vap->iv_ic;
1862 struct urtw_softc *sc = ic->ic_ifp->if_softc;
1863 struct urtw_vap *uvp = URTW_VAP(vap);
1864 struct ieee80211_node *ni;
1865 usb_error_t error = 0;
1867 DPRINTF(sc, URTW_DEBUG_STATE, "%s: %s -> %s\n", __func__,
1868 ieee80211_state_name[vap->iv_state],
1869 ieee80211_state_name[nstate]);
1871 sc->sc_state = nstate;
1873 IEEE80211_UNLOCK(ic);
1875 usb_callout_stop(&sc->sc_led_ch);
1876 callout_stop(&sc->sc_watchdog_ch);
1879 case IEEE80211_S_INIT:
1880 case IEEE80211_S_SCAN:
1881 case IEEE80211_S_AUTH:
1882 case IEEE80211_S_ASSOC:
1884 case IEEE80211_S_RUN:
1885 ni = ieee80211_ref_node(vap->iv_bss);
1886 /* setting bssid. */
1887 urtw_write32_m(sc, URTW_BSSID, ((uint32_t *)ni->ni_bssid)[0]);
1888 urtw_write16_m(sc, URTW_BSSID + 4,
1889 ((uint16_t *)ni->ni_bssid)[2]);
1890 urtw_update_msr(sc);
1891 /* XXX maybe the below would be incorrect. */
1892 urtw_write16_m(sc, URTW_ATIM_WND, 2);
1893 urtw_write16_m(sc, URTW_ATIM_TR_ITV, 100);
1894 urtw_write16_m(sc, URTW_BEACON_INTERVAL, 0x64);
1895 urtw_write16_m(sc, URTW_BEACON_INTERVAL_TIME, 100);
1896 error = urtw_led_ctl(sc, URTW_LED_CTL_LINK);
1898 device_printf(sc->sc_dev,
1899 "could not control LED (%d)\n", error);
1900 ieee80211_free_node(ni);
1908 return (uvp->newstate(vap, nstate, arg));
1912 urtw_watchdog(void *arg)
1914 struct urtw_softc *sc = arg;
1915 struct ifnet *ifp = sc->sc_ifp;
1917 if (sc->sc_txtimer > 0) {
1918 if (--sc->sc_txtimer == 0) {
1919 device_printf(sc->sc_dev, "device timeout\n");
1923 callout_reset(&sc->sc_watchdog_ch, hz, urtw_watchdog, sc);
1928 urtw_set_multi(void *arg)
1930 struct urtw_softc *sc = arg;
1931 struct ifnet *ifp = sc->sc_ifp;
1933 if (!(ifp->if_flags & IFF_UP))
1937 * XXX don't know how to set a device. Lack of docs. Just try to set
1938 * IFF_ALLMULTI flag here.
1940 ifp->if_flags |= IFF_ALLMULTI;
1944 urtw_set_rate(struct urtw_softc *sc)
1946 int i, basic_rate, min_rr_rate, max_rr_rate;
1950 basic_rate = urtw_rate2rtl(48);
1951 min_rr_rate = urtw_rate2rtl(12);
1952 max_rr_rate = urtw_rate2rtl(48);
1954 urtw_write8_m(sc, URTW_RESP_RATE,
1955 max_rr_rate << URTW_RESP_MAX_RATE_SHIFT |
1956 min_rr_rate << URTW_RESP_MIN_RATE_SHIFT);
1958 urtw_read16_m(sc, URTW_BRSR, &data);
1959 data &= ~URTW_BRSR_MBR_8185;
1961 for (i = 0; i <= basic_rate; i++)
1964 urtw_write16_m(sc, URTW_BRSR, data);
1970 urtw_rate2rtl(uint32_t rate)
1972 #define N(a) ((int)(sizeof(a) / sizeof((a)[0])))
1975 for (i = 0; i < N(urtw_ratetable); i++) {
1976 if (rate == urtw_ratetable[i].reg)
1977 return urtw_ratetable[i].val;
1985 urtw_rtl2rate(uint32_t rate)
1987 #define N(a) ((int)(sizeof(a) / sizeof((a)[0])))
1990 for (i = 0; i < N(urtw_ratetable); i++) {
1991 if (rate == urtw_ratetable[i].val)
1992 return urtw_ratetable[i].reg;
2000 urtw_update_msr(struct urtw_softc *sc)
2002 struct ifnet *ifp = sc->sc_ifp;
2003 struct ieee80211com *ic = ifp->if_l2com;
2007 urtw_read8_m(sc, URTW_MSR, &data);
2008 data &= ~URTW_MSR_LINK_MASK;
2010 if (sc->sc_state == IEEE80211_S_RUN) {
2011 switch (ic->ic_opmode) {
2012 case IEEE80211_M_STA:
2013 case IEEE80211_M_MONITOR:
2014 data |= URTW_MSR_LINK_STA;
2015 if (sc->sc_flags & URTW_RTL8187B)
2016 data |= URTW_MSR_LINK_ENEDCA;
2018 case IEEE80211_M_IBSS:
2019 data |= URTW_MSR_LINK_ADHOC;
2021 case IEEE80211_M_HOSTAP:
2022 data |= URTW_MSR_LINK_HOSTAP;
2025 DPRINTF(sc, URTW_DEBUG_STATE,
2026 "unsupported operation mode 0x%x\n",
2028 error = USB_ERR_INVAL;
2032 data |= URTW_MSR_LINK_NONE;
2034 urtw_write8_m(sc, URTW_MSR, data);
2040 urtw_read8_c(struct urtw_softc *sc, int val, uint8_t *data)
2042 struct usb_device_request req;
2045 URTW_ASSERT_LOCKED(sc);
2047 req.bmRequestType = UT_READ_VENDOR_DEVICE;
2048 req.bRequest = URTW_8187_GETREGS_REQ;
2049 USETW(req.wValue, (val & 0xff) | 0xff00);
2050 USETW(req.wIndex, (val >> 8) & 0x3);
2051 USETW(req.wLength, sizeof(uint8_t));
2053 error = urtw_do_request(sc, &req, data);
2058 urtw_read16_c(struct urtw_softc *sc, int val, uint16_t *data)
2060 struct usb_device_request req;
2063 URTW_ASSERT_LOCKED(sc);
2065 req.bmRequestType = UT_READ_VENDOR_DEVICE;
2066 req.bRequest = URTW_8187_GETREGS_REQ;
2067 USETW(req.wValue, (val & 0xff) | 0xff00);
2068 USETW(req.wIndex, (val >> 8) & 0x3);
2069 USETW(req.wLength, sizeof(uint16_t));
2071 error = urtw_do_request(sc, &req, data);
2076 urtw_read32_c(struct urtw_softc *sc, int val, uint32_t *data)
2078 struct usb_device_request req;
2081 URTW_ASSERT_LOCKED(sc);
2083 req.bmRequestType = UT_READ_VENDOR_DEVICE;
2084 req.bRequest = URTW_8187_GETREGS_REQ;
2085 USETW(req.wValue, (val & 0xff) | 0xff00);
2086 USETW(req.wIndex, (val >> 8) & 0x3);
2087 USETW(req.wLength, sizeof(uint32_t));
2089 error = urtw_do_request(sc, &req, data);
2094 urtw_write8_c(struct urtw_softc *sc, int val, uint8_t data)
2096 struct usb_device_request req;
2098 URTW_ASSERT_LOCKED(sc);
2100 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
2101 req.bRequest = URTW_8187_SETREGS_REQ;
2102 USETW(req.wValue, (val & 0xff) | 0xff00);
2103 USETW(req.wIndex, (val >> 8) & 0x3);
2104 USETW(req.wLength, sizeof(uint8_t));
2106 return (urtw_do_request(sc, &req, &data));
2110 urtw_write16_c(struct urtw_softc *sc, int val, uint16_t data)
2112 struct usb_device_request req;
2114 URTW_ASSERT_LOCKED(sc);
2116 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
2117 req.bRequest = URTW_8187_SETREGS_REQ;
2118 USETW(req.wValue, (val & 0xff) | 0xff00);
2119 USETW(req.wIndex, (val >> 8) & 0x3);
2120 USETW(req.wLength, sizeof(uint16_t));
2122 return (urtw_do_request(sc, &req, &data));
2126 urtw_write32_c(struct urtw_softc *sc, int val, uint32_t data)
2128 struct usb_device_request req;
2130 URTW_ASSERT_LOCKED(sc);
2132 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
2133 req.bRequest = URTW_8187_SETREGS_REQ;
2134 USETW(req.wValue, (val & 0xff) | 0xff00);
2135 USETW(req.wIndex, (val >> 8) & 0x3);
2136 USETW(req.wLength, sizeof(uint32_t));
2138 return (urtw_do_request(sc, &req, &data));
2142 urtw_get_macaddr(struct urtw_softc *sc)
2147 error = urtw_eprom_read32(sc, URTW_EPROM_MACADDR, &data);
2150 sc->sc_bssid[0] = data & 0xff;
2151 sc->sc_bssid[1] = (data & 0xff00) >> 8;
2152 error = urtw_eprom_read32(sc, URTW_EPROM_MACADDR + 1, &data);
2155 sc->sc_bssid[2] = data & 0xff;
2156 sc->sc_bssid[3] = (data & 0xff00) >> 8;
2157 error = urtw_eprom_read32(sc, URTW_EPROM_MACADDR + 2, &data);
2160 sc->sc_bssid[4] = data & 0xff;
2161 sc->sc_bssid[5] = (data & 0xff00) >> 8;
2167 urtw_eprom_read32(struct urtw_softc *sc, uint32_t addr, uint32_t *data)
2169 #define URTW_READCMD_LEN 3
2171 int16_t addrstr[8], data16, readcmd[] = { 1, 1, 0 };
2174 /* NB: make sure the buffer is initialized */
2177 /* enable EPROM programming */
2178 urtw_write8_m(sc, URTW_EPROM_CMD, URTW_EPROM_CMD_PROGRAM_MODE);
2179 DELAY(URTW_EPROM_DELAY);
2181 error = urtw_eprom_cs(sc, URTW_EPROM_ENABLE);
2184 error = urtw_eprom_ck(sc);
2187 error = urtw_eprom_sendbits(sc, readcmd, URTW_READCMD_LEN);
2190 if (sc->sc_epromtype == URTW_EEPROM_93C56) {
2192 addrstr[0] = addr & (1 << 7);
2193 addrstr[1] = addr & (1 << 6);
2194 addrstr[2] = addr & (1 << 5);
2195 addrstr[3] = addr & (1 << 4);
2196 addrstr[4] = addr & (1 << 3);
2197 addrstr[5] = addr & (1 << 2);
2198 addrstr[6] = addr & (1 << 1);
2199 addrstr[7] = addr & (1 << 0);
2202 addrstr[0] = addr & (1 << 5);
2203 addrstr[1] = addr & (1 << 4);
2204 addrstr[2] = addr & (1 << 3);
2205 addrstr[3] = addr & (1 << 2);
2206 addrstr[4] = addr & (1 << 1);
2207 addrstr[5] = addr & (1 << 0);
2209 error = urtw_eprom_sendbits(sc, addrstr, addrlen);
2213 error = urtw_eprom_writebit(sc, 0);
2217 for (i = 0; i < 16; i++) {
2218 error = urtw_eprom_ck(sc);
2221 error = urtw_eprom_readbit(sc, &data16);
2225 (*data) |= (data16 << (15 - i));
2228 error = urtw_eprom_cs(sc, URTW_EPROM_DISABLE);
2231 error = urtw_eprom_ck(sc);
2235 /* now disable EPROM programming */
2236 urtw_write8_m(sc, URTW_EPROM_CMD, URTW_EPROM_CMD_NORMAL_MODE);
2239 #undef URTW_READCMD_LEN
2243 urtw_eprom_cs(struct urtw_softc *sc, int able)
2248 urtw_read8_m(sc, URTW_EPROM_CMD, &data);
2249 if (able == URTW_EPROM_ENABLE)
2250 urtw_write8_m(sc, URTW_EPROM_CMD, data | URTW_EPROM_CS);
2252 urtw_write8_m(sc, URTW_EPROM_CMD, data & ~URTW_EPROM_CS);
2253 DELAY(URTW_EPROM_DELAY);
2259 urtw_eprom_ck(struct urtw_softc *sc)
2265 urtw_read8_m(sc, URTW_EPROM_CMD, &data);
2266 urtw_write8_m(sc, URTW_EPROM_CMD, data | URTW_EPROM_CK);
2267 DELAY(URTW_EPROM_DELAY);
2269 urtw_read8_m(sc, URTW_EPROM_CMD, &data);
2270 urtw_write8_m(sc, URTW_EPROM_CMD, data & ~URTW_EPROM_CK);
2271 DELAY(URTW_EPROM_DELAY);
2277 urtw_eprom_readbit(struct urtw_softc *sc, int16_t *data)
2282 urtw_read8_m(sc, URTW_EPROM_CMD, &data8);
2283 *data = (data8 & URTW_EPROM_READBIT) ? 1 : 0;
2284 DELAY(URTW_EPROM_DELAY);
2291 urtw_eprom_writebit(struct urtw_softc *sc, int16_t bit)
2296 urtw_read8_m(sc, URTW_EPROM_CMD, &data);
2298 urtw_write8_m(sc, URTW_EPROM_CMD, data | URTW_EPROM_WRITEBIT);
2300 urtw_write8_m(sc, URTW_EPROM_CMD, data & ~URTW_EPROM_WRITEBIT);
2301 DELAY(URTW_EPROM_DELAY);
2307 urtw_eprom_sendbits(struct urtw_softc *sc, int16_t *buf, int buflen)
2310 usb_error_t error = 0;
2312 for (i = 0; i < buflen; i++) {
2313 error = urtw_eprom_writebit(sc, buf[i]);
2316 error = urtw_eprom_ck(sc);
2326 urtw_get_txpwr(struct urtw_softc *sc)
2332 error = urtw_eprom_read32(sc, URTW_EPROM_TXPW_BASE, &data);
2335 sc->sc_txpwr_cck_base = data & 0xf;
2336 sc->sc_txpwr_ofdm_base = (data >> 4) & 0xf;
2338 for (i = 1, j = 0; i < 6; i += 2, j++) {
2339 error = urtw_eprom_read32(sc, URTW_EPROM_TXPW0 + j, &data);
2342 sc->sc_txpwr_cck[i] = data & 0xf;
2343 sc->sc_txpwr_cck[i + 1] = (data & 0xf00) >> 8;
2344 sc->sc_txpwr_ofdm[i] = (data & 0xf0) >> 4;
2345 sc->sc_txpwr_ofdm[i + 1] = (data & 0xf000) >> 12;
2347 for (i = 1, j = 0; i < 4; i += 2, j++) {
2348 error = urtw_eprom_read32(sc, URTW_EPROM_TXPW1 + j, &data);
2351 sc->sc_txpwr_cck[i + 6] = data & 0xf;
2352 sc->sc_txpwr_cck[i + 6 + 1] = (data & 0xf00) >> 8;
2353 sc->sc_txpwr_ofdm[i + 6] = (data & 0xf0) >> 4;
2354 sc->sc_txpwr_ofdm[i + 6 + 1] = (data & 0xf000) >> 12;
2356 if (sc->sc_flags & URTW_RTL8187B) {
2357 error = urtw_eprom_read32(sc, URTW_EPROM_TXPW2, &data);
2360 sc->sc_txpwr_cck[1 + 6 + 4] = data & 0xf;
2361 sc->sc_txpwr_ofdm[1 + 6 + 4] = (data & 0xf0) >> 4;
2362 error = urtw_eprom_read32(sc, 0x0a, &data);
2365 sc->sc_txpwr_cck[2 + 6 + 4] = data & 0xf;
2366 sc->sc_txpwr_ofdm[2 + 6 + 4] = (data & 0xf0) >> 4;
2367 error = urtw_eprom_read32(sc, 0x1c, &data);
2370 sc->sc_txpwr_cck[3 + 6 + 4] = data & 0xf;
2371 sc->sc_txpwr_cck[3 + 6 + 4 + 1] = (data & 0xf00) >> 8;
2372 sc->sc_txpwr_ofdm[3 + 6 + 4] = (data & 0xf0) >> 4;
2373 sc->sc_txpwr_ofdm[3 + 6 + 4 + 1] = (data & 0xf000) >> 12;
2375 for (i = 1, j = 0; i < 4; i += 2, j++) {
2376 error = urtw_eprom_read32(sc, URTW_EPROM_TXPW2 + j,
2380 sc->sc_txpwr_cck[i + 6 + 4] = data & 0xf;
2381 sc->sc_txpwr_cck[i + 6 + 4 + 1] = (data & 0xf00) >> 8;
2382 sc->sc_txpwr_ofdm[i + 6 + 4] = (data & 0xf0) >> 4;
2383 sc->sc_txpwr_ofdm[i + 6 + 4 + 1] = (data & 0xf000) >> 12;
2392 urtw_get_rfchip(struct urtw_softc *sc)
2399 if (sc->sc_flags & URTW_RTL8187B) {
2400 urtw_read8_m(sc, 0xe1, &data8);
2403 sc->sc_flags |= URTW_RTL8187B_REV_B;
2406 sc->sc_flags |= URTW_RTL8187B_REV_D;
2409 sc->sc_flags |= URTW_RTL8187B_REV_E;
2412 device_printf(sc->sc_dev, "unknown type: %#x\n", data8);
2413 sc->sc_flags |= URTW_RTL8187B_REV_B;
2417 urtw_read32_m(sc, URTW_TX_CONF, &data);
2418 switch (data & URTW_TX_HWMASK) {
2419 case URTW_TX_R8187vD_B:
2420 sc->sc_flags |= URTW_RTL8187B;
2422 case URTW_TX_R8187vD:
2425 device_printf(sc->sc_dev, "unknown RTL8187L type: %#x\n",
2426 data & URTW_TX_HWMASK);
2431 error = urtw_eprom_read32(sc, URTW_EPROM_RFCHIPID, &data);
2434 switch (data & 0xff) {
2435 case URTW_EPROM_RFCHIPID_RTL8225U:
2436 error = urtw_8225_isv2(sc, &ret);
2440 sc->sc_rf_init = urtw_8225_rf_init;
2441 sc->sc_rf_set_sens = urtw_8225_rf_set_sens;
2442 sc->sc_rf_set_chan = urtw_8225_rf_set_chan;
2443 sc->sc_rf_stop = urtw_8225_rf_stop;
2445 sc->sc_rf_init = urtw_8225v2_rf_init;
2446 sc->sc_rf_set_chan = urtw_8225v2_rf_set_chan;
2447 sc->sc_rf_stop = urtw_8225_rf_stop;
2449 sc->sc_max_sens = URTW_8225_RF_MAX_SENS;
2450 sc->sc_sens = URTW_8225_RF_DEF_SENS;
2452 case URTW_EPROM_RFCHIPID_RTL8225Z2:
2453 sc->sc_rf_init = urtw_8225v2b_rf_init;
2454 sc->sc_rf_set_chan = urtw_8225v2b_rf_set_chan;
2455 sc->sc_max_sens = URTW_8225_RF_MAX_SENS;
2456 sc->sc_sens = URTW_8225_RF_DEF_SENS;
2457 sc->sc_rf_stop = urtw_8225_rf_stop;
2460 DPRINTF(sc, URTW_DEBUG_STATE,
2461 "unsupported RF chip %d\n", data & 0xff);
2462 error = USB_ERR_INVAL;
2466 device_printf(sc->sc_dev, "%s rf %s hwrev %s\n",
2467 (sc->sc_flags & URTW_RTL8187B) ? "rtl8187b" : "rtl8187l",
2468 ((data & 0xff) == URTW_EPROM_RFCHIPID_RTL8225U) ? "rtl8225u" :
2470 (sc->sc_flags & URTW_RTL8187B) ? ((data8 == 0) ? "b" :
2471 (data8 == 1) ? "d" : "e") : "none");
2479 urtw_led_init(struct urtw_softc *sc)
2484 urtw_read8_m(sc, URTW_PSR, &sc->sc_psr);
2485 error = urtw_eprom_read32(sc, URTW_EPROM_SWREV, &rev);
2489 switch (rev & URTW_EPROM_CID_MASK) {
2490 case URTW_EPROM_CID_ALPHA0:
2491 sc->sc_strategy = URTW_SW_LED_MODE1;
2493 case URTW_EPROM_CID_SERCOMM_PS:
2494 sc->sc_strategy = URTW_SW_LED_MODE3;
2496 case URTW_EPROM_CID_HW_LED:
2497 sc->sc_strategy = URTW_HW_LED;
2499 case URTW_EPROM_CID_RSVD0:
2500 case URTW_EPROM_CID_RSVD1:
2502 sc->sc_strategy = URTW_SW_LED_MODE0;
2506 sc->sc_gpio_ledpin = URTW_LED_PIN_GPIO0;
2514 urtw_8225_rf_init(struct urtw_softc *sc)
2516 #define N(a) ((int)(sizeof(a) / sizeof((a)[0])))
2521 error = urtw_8180_set_anaparam(sc, URTW_8225_ANAPARAM_ON);
2525 error = urtw_8225_usb_init(sc);
2529 urtw_write32_m(sc, URTW_RF_TIMING, 0x000a8008);
2530 urtw_read16_m(sc, URTW_BRSR, &data); /* XXX ??? */
2531 urtw_write16_m(sc, URTW_BRSR, 0xffff);
2532 urtw_write32_m(sc, URTW_RF_PARA, 0x100044);
2534 error = urtw_set_mode(sc, URTW_EPROM_CMD_CONFIG);
2537 urtw_write8_m(sc, URTW_CONFIG3, 0x44);
2538 error = urtw_set_mode(sc, URTW_EPROM_CMD_NORMAL);
2542 error = urtw_8185_rf_pins_enable(sc);
2545 usb_pause_mtx(&sc->sc_mtx, 1000);
2547 for (i = 0; i < N(urtw_8225_rf_part1); i++) {
2548 urtw_8225_write(sc, urtw_8225_rf_part1[i].reg,
2549 urtw_8225_rf_part1[i].val);
2550 usb_pause_mtx(&sc->sc_mtx, 1);
2552 usb_pause_mtx(&sc->sc_mtx, 100);
2554 URTW_8225_ADDR_2_MAGIC, URTW_8225_ADDR_2_DATA_MAGIC1);
2555 usb_pause_mtx(&sc->sc_mtx, 200);
2557 URTW_8225_ADDR_2_MAGIC, URTW_8225_ADDR_2_DATA_MAGIC2);
2558 usb_pause_mtx(&sc->sc_mtx, 200);
2560 URTW_8225_ADDR_0_MAGIC, URTW_8225_ADDR_0_DATA_MAGIC3);
2562 for (i = 0; i < 95; i++) {
2563 urtw_8225_write(sc, URTW_8225_ADDR_1_MAGIC, (uint8_t)(i + 1));
2564 urtw_8225_write(sc, URTW_8225_ADDR_2_MAGIC, urtw_8225_rxgain[i]);
2568 URTW_8225_ADDR_0_MAGIC, URTW_8225_ADDR_0_DATA_MAGIC4);
2570 URTW_8225_ADDR_0_MAGIC, URTW_8225_ADDR_0_DATA_MAGIC5);
2572 for (i = 0; i < 128; i++) {
2573 urtw_8187_write_phy_ofdm(sc, 0xb, urtw_8225_agc[i]);
2574 usb_pause_mtx(&sc->sc_mtx, 1);
2575 urtw_8187_write_phy_ofdm(sc, 0xa, (uint8_t)i + 0x80);
2576 usb_pause_mtx(&sc->sc_mtx, 1);
2579 for (i = 0; i < N(urtw_8225_rf_part2); i++) {
2580 urtw_8187_write_phy_ofdm(sc, urtw_8225_rf_part2[i].reg,
2581 urtw_8225_rf_part2[i].val);
2582 usb_pause_mtx(&sc->sc_mtx, 1);
2585 error = urtw_8225_setgain(sc, 4);
2589 for (i = 0; i < N(urtw_8225_rf_part3); i++) {
2590 urtw_8187_write_phy_cck(sc, urtw_8225_rf_part3[i].reg,
2591 urtw_8225_rf_part3[i].val);
2592 usb_pause_mtx(&sc->sc_mtx, 1);
2595 urtw_write8_m(sc, URTW_TESTR, 0x0d);
2597 error = urtw_8225_set_txpwrlvl(sc, 1);
2601 urtw_8187_write_phy_cck(sc, 0x10, 0x9b);
2602 usb_pause_mtx(&sc->sc_mtx, 1);
2603 urtw_8187_write_phy_ofdm(sc, 0x26, 0x90);
2604 usb_pause_mtx(&sc->sc_mtx, 1);
2606 /* TX ant A, 0x0 for B */
2607 error = urtw_8185_tx_antenna(sc, 0x3);
2610 urtw_write32_m(sc, URTW_HSSI_PARA, 0x3dc00002);
2612 error = urtw_8225_rf_set_chan(sc, 1);
2619 urtw_8185_rf_pins_enable(struct urtw_softc *sc)
2621 usb_error_t error = 0;
2623 urtw_write16_m(sc, URTW_RF_PINS_ENABLE, 0x1ff7);
2629 urtw_8185_tx_antenna(struct urtw_softc *sc, uint8_t ant)
2633 urtw_write8_m(sc, URTW_TX_ANTENNA, ant);
2634 usb_pause_mtx(&sc->sc_mtx, 1);
2640 urtw_8187_write_phy_ofdm_c(struct urtw_softc *sc, uint8_t addr, uint32_t data)
2644 return urtw_8187_write_phy(sc, addr, data);
2648 urtw_8187_write_phy_cck_c(struct urtw_softc *sc, uint8_t addr, uint32_t data)
2652 return urtw_8187_write_phy(sc, addr, data | 0x10000);
2656 urtw_8187_write_phy(struct urtw_softc *sc, uint8_t addr, uint32_t data)
2661 phyw = ((data << 8) | (addr | 0x80));
2662 urtw_write8_m(sc, URTW_PHY_MAGIC4, ((phyw & 0xff000000) >> 24));
2663 urtw_write8_m(sc, URTW_PHY_MAGIC3, ((phyw & 0x00ff0000) >> 16));
2664 urtw_write8_m(sc, URTW_PHY_MAGIC2, ((phyw & 0x0000ff00) >> 8));
2665 urtw_write8_m(sc, URTW_PHY_MAGIC1, ((phyw & 0x000000ff)));
2666 usb_pause_mtx(&sc->sc_mtx, 1);
2672 urtw_8225_setgain(struct urtw_softc *sc, int16_t gain)
2676 urtw_8187_write_phy_ofdm(sc, 0x0d, urtw_8225_gain[gain * 4]);
2677 urtw_8187_write_phy_ofdm(sc, 0x1b, urtw_8225_gain[gain * 4 + 2]);
2678 urtw_8187_write_phy_ofdm(sc, 0x1d, urtw_8225_gain[gain * 4 + 3]);
2679 urtw_8187_write_phy_ofdm(sc, 0x23, urtw_8225_gain[gain * 4 + 1]);
2685 urtw_8225_usb_init(struct urtw_softc *sc)
2690 urtw_write8_m(sc, URTW_RF_PINS_SELECT + 1, 0);
2691 urtw_write8_m(sc, URTW_GPIO, 0);
2692 error = urtw_read8e(sc, 0x53, &data);
2695 error = urtw_write8e(sc, 0x53, data | (1 << 7));
2698 urtw_write8_m(sc, URTW_RF_PINS_SELECT + 1, 4);
2699 urtw_write8_m(sc, URTW_GPIO, 0x20);
2700 urtw_write8_m(sc, URTW_GP_ENABLE, 0);
2702 urtw_write16_m(sc, URTW_RF_PINS_OUTPUT, 0x80);
2703 urtw_write16_m(sc, URTW_RF_PINS_SELECT, 0x80);
2704 urtw_write16_m(sc, URTW_RF_PINS_ENABLE, 0x80);
2706 usb_pause_mtx(&sc->sc_mtx, 500);
2712 urtw_8225_write_c(struct urtw_softc *sc, uint8_t addr, uint16_t data)
2714 uint16_t d80, d82, d84;
2717 urtw_read16_m(sc, URTW_RF_PINS_OUTPUT, &d80);
2718 d80 &= URTW_RF_PINS_MAGIC1;
2719 urtw_read16_m(sc, URTW_RF_PINS_ENABLE, &d82);
2720 urtw_read16_m(sc, URTW_RF_PINS_SELECT, &d84);
2721 d84 &= URTW_RF_PINS_MAGIC2;
2722 urtw_write16_m(sc, URTW_RF_PINS_ENABLE, d82 | URTW_RF_PINS_MAGIC3);
2723 urtw_write16_m(sc, URTW_RF_PINS_SELECT, d84 | URTW_RF_PINS_MAGIC3);
2726 urtw_write16_m(sc, URTW_RF_PINS_OUTPUT, d80 | URTW_BB_HOST_BANG_EN);
2728 urtw_write16_m(sc, URTW_RF_PINS_OUTPUT, d80);
2731 error = urtw_8225_write_s16(sc, addr, 0x8225, &data);
2735 urtw_write16_m(sc, URTW_RF_PINS_OUTPUT, d80 | URTW_BB_HOST_BANG_EN);
2737 urtw_write16_m(sc, URTW_RF_PINS_OUTPUT, d80 | URTW_BB_HOST_BANG_EN);
2738 urtw_write16_m(sc, URTW_RF_PINS_SELECT, d84);
2739 usb_pause_mtx(&sc->sc_mtx, 2);
2745 urtw_8225_write_s16(struct urtw_softc *sc, uint8_t addr, int index,
2750 struct usb_device_request req;
2751 usb_error_t error = 0;
2755 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
2756 req.bRequest = URTW_8187_SETREGS_REQ;
2757 USETW(req.wValue, addr);
2758 USETW(req.wIndex, index);
2759 USETW(req.wLength, sizeof(uint16_t));
2760 buf[0] = (data16 & 0x00ff);
2761 buf[1] = (data16 & 0xff00) >> 8;
2763 error = urtw_do_request(sc, &req, buf);
2769 urtw_8225_rf_set_chan(struct urtw_softc *sc, int chan)
2773 error = urtw_8225_set_txpwrlvl(sc, chan);
2776 urtw_8225_write(sc, URTW_8225_ADDR_7_MAGIC, urtw_8225_channel[chan]);
2777 usb_pause_mtx(&sc->sc_mtx, 10);
2783 urtw_8225_rf_set_sens(struct urtw_softc *sc, int sens)
2787 if (sens < 0 || sens > 6)
2792 URTW_8225_ADDR_C_MAGIC, URTW_8225_ADDR_C_DATA_MAGIC1);
2795 URTW_8225_ADDR_C_MAGIC, URTW_8225_ADDR_C_DATA_MAGIC2);
2798 error = urtw_8225_setgain(sc, sens);
2802 urtw_8187_write_phy_cck(sc, 0x41, urtw_8225_threshold[sens]);
2809 urtw_8225_set_txpwrlvl(struct urtw_softc *sc, int chan)
2812 uint8_t *cck_pwltable;
2813 uint8_t cck_pwrlvl_max, ofdm_pwrlvl_min, ofdm_pwrlvl_max;
2814 uint8_t cck_pwrlvl = sc->sc_txpwr_cck[chan] & 0xff;
2815 uint8_t ofdm_pwrlvl = sc->sc_txpwr_ofdm[chan] & 0xff;
2818 cck_pwrlvl_max = 11;
2819 ofdm_pwrlvl_max = 25; /* 12 -> 25 */
2820 ofdm_pwrlvl_min = 10;
2822 /* CCK power setting */
2823 cck_pwrlvl = (cck_pwrlvl > cck_pwrlvl_max) ? cck_pwrlvl_max : cck_pwrlvl;
2824 idx = cck_pwrlvl % 6;
2825 set = cck_pwrlvl / 6;
2826 cck_pwltable = (chan == 14) ? urtw_8225_txpwr_cck_ch14 :
2827 urtw_8225_txpwr_cck;
2829 urtw_write8_m(sc, URTW_TX_GAIN_CCK,
2830 urtw_8225_tx_gain_cck_ofdm[set] >> 1);
2831 for (i = 0; i < 8; i++) {
2832 urtw_8187_write_phy_cck(sc, 0x44 + i,
2833 cck_pwltable[idx * 8 + i]);
2835 usb_pause_mtx(&sc->sc_mtx, 1);
2837 /* OFDM power setting */
2838 ofdm_pwrlvl = (ofdm_pwrlvl > (ofdm_pwrlvl_max - ofdm_pwrlvl_min)) ?
2839 ofdm_pwrlvl_max : ofdm_pwrlvl + ofdm_pwrlvl_min;
2840 ofdm_pwrlvl = (ofdm_pwrlvl > 35) ? 35 : ofdm_pwrlvl;
2842 idx = ofdm_pwrlvl % 6;
2843 set = ofdm_pwrlvl / 6;
2845 error = urtw_8185_set_anaparam2(sc, URTW_8225_ANAPARAM2_ON);
2848 urtw_8187_write_phy_ofdm(sc, 2, 0x42);
2849 urtw_8187_write_phy_ofdm(sc, 6, 0);
2850 urtw_8187_write_phy_ofdm(sc, 8, 0);
2852 urtw_write8_m(sc, URTW_TX_GAIN_OFDM,
2853 urtw_8225_tx_gain_cck_ofdm[set] >> 1);
2854 urtw_8187_write_phy_ofdm(sc, 0x5, urtw_8225_txpwr_ofdm[idx]);
2855 urtw_8187_write_phy_ofdm(sc, 0x7, urtw_8225_txpwr_ofdm[idx]);
2856 usb_pause_mtx(&sc->sc_mtx, 1);
2863 urtw_8225_rf_stop(struct urtw_softc *sc)
2868 urtw_8225_write(sc, 0x4, 0x1f);
2870 error = urtw_set_mode(sc, URTW_EPROM_CMD_CONFIG);
2874 urtw_read8_m(sc, URTW_CONFIG3, &data);
2875 urtw_write8_m(sc, URTW_CONFIG3, data | URTW_CONFIG3_ANAPARAM_WRITE);
2876 if (sc->sc_flags & URTW_RTL8187B) {
2877 urtw_write32_m(sc, URTW_ANAPARAM2,
2878 URTW_8187B_8225_ANAPARAM2_OFF);
2879 urtw_write32_m(sc, URTW_ANAPARAM, URTW_8187B_8225_ANAPARAM_OFF);
2880 urtw_write32_m(sc, URTW_ANAPARAM3,
2881 URTW_8187B_8225_ANAPARAM3_OFF);
2883 urtw_write32_m(sc, URTW_ANAPARAM2, URTW_8225_ANAPARAM2_OFF);
2884 urtw_write32_m(sc, URTW_ANAPARAM, URTW_8225_ANAPARAM_OFF);
2887 urtw_write8_m(sc, URTW_CONFIG3, data & ~URTW_CONFIG3_ANAPARAM_WRITE);
2888 error = urtw_set_mode(sc, URTW_EPROM_CMD_NORMAL);
2897 urtw_8225v2_rf_init(struct urtw_softc *sc)
2899 #define N(a) ((int)(sizeof(a) / sizeof((a)[0])))
2905 error = urtw_8180_set_anaparam(sc, URTW_8225_ANAPARAM_ON);
2909 error = urtw_8225_usb_init(sc);
2913 urtw_write32_m(sc, URTW_RF_TIMING, 0x000a8008);
2914 urtw_read16_m(sc, URTW_BRSR, &data); /* XXX ??? */
2915 urtw_write16_m(sc, URTW_BRSR, 0xffff);
2916 urtw_write32_m(sc, URTW_RF_PARA, 0x100044);
2918 error = urtw_set_mode(sc, URTW_EPROM_CMD_CONFIG);
2921 urtw_write8_m(sc, URTW_CONFIG3, 0x44);
2922 error = urtw_set_mode(sc, URTW_EPROM_CMD_NORMAL);
2926 error = urtw_8185_rf_pins_enable(sc);
2930 usb_pause_mtx(&sc->sc_mtx, 500);
2932 for (i = 0; i < N(urtw_8225v2_rf_part1); i++) {
2933 urtw_8225_write(sc, urtw_8225v2_rf_part1[i].reg,
2934 urtw_8225v2_rf_part1[i].val);
2936 usb_pause_mtx(&sc->sc_mtx, 50);
2939 URTW_8225_ADDR_0_MAGIC, URTW_8225_ADDR_0_DATA_MAGIC1);
2941 for (i = 0; i < 95; i++) {
2942 urtw_8225_write(sc, URTW_8225_ADDR_1_MAGIC, (uint8_t)(i + 1));
2943 urtw_8225_write(sc, URTW_8225_ADDR_2_MAGIC,
2944 urtw_8225v2_rxgain[i]);
2948 URTW_8225_ADDR_3_MAGIC, URTW_8225_ADDR_3_DATA_MAGIC1);
2950 URTW_8225_ADDR_5_MAGIC, URTW_8225_ADDR_5_DATA_MAGIC1);
2952 URTW_8225_ADDR_0_MAGIC, URTW_8225_ADDR_0_DATA_MAGIC2);
2954 URTW_8225_ADDR_2_MAGIC, URTW_8225_ADDR_2_DATA_MAGIC1);
2955 usb_pause_mtx(&sc->sc_mtx, 100);
2957 URTW_8225_ADDR_2_MAGIC, URTW_8225_ADDR_2_DATA_MAGIC2);
2958 usb_pause_mtx(&sc->sc_mtx, 100);
2960 error = urtw_8225_read(sc, URTW_8225_ADDR_6_MAGIC, &data32);
2963 if (data32 != URTW_8225_ADDR_6_DATA_MAGIC1)
2964 device_printf(sc->sc_dev, "expect 0xe6!! (0x%x)\n", data32);
2965 if (!(data32 & URTW_8225_ADDR_6_DATA_MAGIC2)) {
2967 URTW_8225_ADDR_2_MAGIC, URTW_8225_ADDR_2_DATA_MAGIC1);
2968 usb_pause_mtx(&sc->sc_mtx, 100);
2970 URTW_8225_ADDR_2_MAGIC, URTW_8225_ADDR_2_DATA_MAGIC2);
2971 usb_pause_mtx(&sc->sc_mtx, 50);
2972 error = urtw_8225_read(sc, URTW_8225_ADDR_6_MAGIC, &data32);
2975 if (!(data32 & URTW_8225_ADDR_6_DATA_MAGIC2))
2976 device_printf(sc->sc_dev, "RF calibration failed\n");
2978 usb_pause_mtx(&sc->sc_mtx, 100);
2981 URTW_8225_ADDR_0_MAGIC, URTW_8225_ADDR_0_DATA_MAGIC6);
2982 for (i = 0; i < 128; i++) {
2983 urtw_8187_write_phy_ofdm(sc, 0xb, urtw_8225_agc[i]);
2984 urtw_8187_write_phy_ofdm(sc, 0xa, (uint8_t)i + 0x80);
2987 for (i = 0; i < N(urtw_8225v2_rf_part2); i++) {
2988 urtw_8187_write_phy_ofdm(sc, urtw_8225v2_rf_part2[i].reg,
2989 urtw_8225v2_rf_part2[i].val);
2992 error = urtw_8225v2_setgain(sc, 4);
2996 for (i = 0; i < N(urtw_8225v2_rf_part3); i++) {
2997 urtw_8187_write_phy_cck(sc, urtw_8225v2_rf_part3[i].reg,
2998 urtw_8225v2_rf_part3[i].val);
3001 urtw_write8_m(sc, URTW_TESTR, 0x0d);
3003 error = urtw_8225v2_set_txpwrlvl(sc, 1);
3007 urtw_8187_write_phy_cck(sc, 0x10, 0x9b);
3008 urtw_8187_write_phy_ofdm(sc, 0x26, 0x90);
3010 /* TX ant A, 0x0 for B */
3011 error = urtw_8185_tx_antenna(sc, 0x3);
3014 urtw_write32_m(sc, URTW_HSSI_PARA, 0x3dc00002);
3016 error = urtw_8225_rf_set_chan(sc, 1);
3023 urtw_8225v2_rf_set_chan(struct urtw_softc *sc, int chan)
3027 error = urtw_8225v2_set_txpwrlvl(sc, chan);
3031 urtw_8225_write(sc, URTW_8225_ADDR_7_MAGIC, urtw_8225_channel[chan]);
3032 usb_pause_mtx(&sc->sc_mtx, 10);
3038 urtw_8225_read(struct urtw_softc *sc, uint8_t addr, uint32_t *data)
3042 uint8_t rlen = 12, wlen = 6;
3043 uint16_t o1, o2, o3, tmp;
3044 uint32_t d2w = ((uint32_t)(addr & 0x1f)) << 27;
3045 uint32_t mask = 0x80000000, value = 0;
3048 urtw_read16_m(sc, URTW_RF_PINS_OUTPUT, &o1);
3049 urtw_read16_m(sc, URTW_RF_PINS_ENABLE, &o2);
3050 urtw_read16_m(sc, URTW_RF_PINS_SELECT, &o3);
3051 urtw_write16_m(sc, URTW_RF_PINS_ENABLE, o2 | URTW_RF_PINS_MAGIC4);
3052 urtw_write16_m(sc, URTW_RF_PINS_SELECT, o3 | URTW_RF_PINS_MAGIC4);
3053 o1 &= ~URTW_RF_PINS_MAGIC4;
3054 urtw_write16_m(sc, URTW_RF_PINS_OUTPUT, o1 | URTW_BB_HOST_BANG_EN);
3056 urtw_write16_m(sc, URTW_RF_PINS_OUTPUT, o1);
3059 for (i = 0; i < (wlen / 2); i++, mask = mask >> 1) {
3060 bit = ((d2w & mask) != 0) ? 1 : 0;
3062 urtw_write16_m(sc, URTW_RF_PINS_OUTPUT, bit | o1);
3064 urtw_write16_m(sc, URTW_RF_PINS_OUTPUT, bit | o1 |
3065 URTW_BB_HOST_BANG_CLK);
3067 urtw_write16_m(sc, URTW_RF_PINS_OUTPUT, bit | o1 |
3068 URTW_BB_HOST_BANG_CLK);
3073 bit = ((d2w & mask) != 0) ? 1 : 0;
3074 urtw_write16_m(sc, URTW_RF_PINS_OUTPUT, bit | o1 |
3075 URTW_BB_HOST_BANG_CLK);
3077 urtw_write16_m(sc, URTW_RF_PINS_OUTPUT, bit | o1 |
3078 URTW_BB_HOST_BANG_CLK);
3080 urtw_write16_m(sc, URTW_RF_PINS_OUTPUT, bit | o1);
3083 urtw_write16_m(sc, URTW_RF_PINS_OUTPUT, bit | o1 | URTW_BB_HOST_BANG_RW |
3084 URTW_BB_HOST_BANG_CLK);
3086 urtw_write16_m(sc, URTW_RF_PINS_OUTPUT, bit | o1 | URTW_BB_HOST_BANG_RW);
3088 urtw_write16_m(sc, URTW_RF_PINS_OUTPUT, o1 | URTW_BB_HOST_BANG_RW);
3092 for (i = 0; i < rlen; i++, mask = mask >> 1) {
3093 urtw_write16_m(sc, URTW_RF_PINS_OUTPUT,
3094 o1 | URTW_BB_HOST_BANG_RW);
3096 urtw_write16_m(sc, URTW_RF_PINS_OUTPUT,
3097 o1 | URTW_BB_HOST_BANG_RW | URTW_BB_HOST_BANG_CLK);
3099 urtw_write16_m(sc, URTW_RF_PINS_OUTPUT,
3100 o1 | URTW_BB_HOST_BANG_RW | URTW_BB_HOST_BANG_CLK);
3102 urtw_write16_m(sc, URTW_RF_PINS_OUTPUT,
3103 o1 | URTW_BB_HOST_BANG_RW | URTW_BB_HOST_BANG_CLK);
3106 urtw_read16_m(sc, URTW_RF_PINS_INPUT, &tmp);
3107 value |= ((tmp & URTW_BB_HOST_BANG_CLK) ? mask : 0);
3108 urtw_write16_m(sc, URTW_RF_PINS_OUTPUT,
3109 o1 | URTW_BB_HOST_BANG_RW);
3113 urtw_write16_m(sc, URTW_RF_PINS_OUTPUT, o1 | URTW_BB_HOST_BANG_EN |
3114 URTW_BB_HOST_BANG_RW);
3117 urtw_write16_m(sc, URTW_RF_PINS_ENABLE, o2);
3118 urtw_write16_m(sc, URTW_RF_PINS_SELECT, o3);
3119 urtw_write16_m(sc, URTW_RF_PINS_OUTPUT, URTW_RF_PINS_OUTPUT_MAGIC1);
3129 urtw_8225v2_set_txpwrlvl(struct urtw_softc *sc, int chan)
3132 uint8_t *cck_pwrtable;
3133 uint8_t cck_pwrlvl_max = 15, ofdm_pwrlvl_max = 25, ofdm_pwrlvl_min = 10;
3134 uint8_t cck_pwrlvl = sc->sc_txpwr_cck[chan] & 0xff;
3135 uint8_t ofdm_pwrlvl = sc->sc_txpwr_ofdm[chan] & 0xff;
3138 /* CCK power setting */
3139 cck_pwrlvl = (cck_pwrlvl > cck_pwrlvl_max) ? cck_pwrlvl_max : cck_pwrlvl;
3140 cck_pwrlvl += sc->sc_txpwr_cck_base;
3141 cck_pwrlvl = (cck_pwrlvl > 35) ? 35 : cck_pwrlvl;
3142 cck_pwrtable = (chan == 14) ? urtw_8225v2_txpwr_cck_ch14 :
3143 urtw_8225v2_txpwr_cck;
3145 for (i = 0; i < 8; i++)
3146 urtw_8187_write_phy_cck(sc, 0x44 + i, cck_pwrtable[i]);
3148 urtw_write8_m(sc, URTW_TX_GAIN_CCK,
3149 urtw_8225v2_tx_gain_cck_ofdm[cck_pwrlvl]);
3150 usb_pause_mtx(&sc->sc_mtx, 1);
3152 /* OFDM power setting */
3153 ofdm_pwrlvl = (ofdm_pwrlvl > (ofdm_pwrlvl_max - ofdm_pwrlvl_min)) ?
3154 ofdm_pwrlvl_max : ofdm_pwrlvl + ofdm_pwrlvl_min;
3155 ofdm_pwrlvl += sc->sc_txpwr_ofdm_base;
3156 ofdm_pwrlvl = (ofdm_pwrlvl > 35) ? 35 : ofdm_pwrlvl;
3158 error = urtw_8185_set_anaparam2(sc, URTW_8225_ANAPARAM2_ON);
3162 urtw_8187_write_phy_ofdm(sc, 2, 0x42);
3163 urtw_8187_write_phy_ofdm(sc, 5, 0x0);
3164 urtw_8187_write_phy_ofdm(sc, 6, 0x40);
3165 urtw_8187_write_phy_ofdm(sc, 7, 0x0);
3166 urtw_8187_write_phy_ofdm(sc, 8, 0x40);
3168 urtw_write8_m(sc, URTW_TX_GAIN_OFDM,
3169 urtw_8225v2_tx_gain_cck_ofdm[ofdm_pwrlvl]);
3170 usb_pause_mtx(&sc->sc_mtx, 1);
3176 urtw_8225v2_setgain(struct urtw_softc *sc, int16_t gain)
3182 gainp = urtw_8225v2_gain_bg;
3183 urtw_8187_write_phy_ofdm(sc, 0x0d, gainp[gain * 3]);
3184 usb_pause_mtx(&sc->sc_mtx, 1);
3185 urtw_8187_write_phy_ofdm(sc, 0x1b, gainp[gain * 3 + 1]);
3186 usb_pause_mtx(&sc->sc_mtx, 1);
3187 urtw_8187_write_phy_ofdm(sc, 0x1d, gainp[gain * 3 + 2]);
3188 usb_pause_mtx(&sc->sc_mtx, 1);
3189 urtw_8187_write_phy_ofdm(sc, 0x21, 0x17);
3190 usb_pause_mtx(&sc->sc_mtx, 1);
3196 urtw_8225_isv2(struct urtw_softc *sc, int *ret)
3203 urtw_write16_m(sc, URTW_RF_PINS_OUTPUT, URTW_RF_PINS_MAGIC5);
3204 urtw_write16_m(sc, URTW_RF_PINS_SELECT, URTW_RF_PINS_MAGIC5);
3205 urtw_write16_m(sc, URTW_RF_PINS_ENABLE, URTW_RF_PINS_MAGIC5);
3206 usb_pause_mtx(&sc->sc_mtx, 500);
3208 urtw_8225_write(sc, URTW_8225_ADDR_0_MAGIC,
3209 URTW_8225_ADDR_0_DATA_MAGIC1);
3211 error = urtw_8225_read(sc, URTW_8225_ADDR_8_MAGIC, &data);
3214 if (data != URTW_8225_ADDR_8_DATA_MAGIC1)
3217 error = urtw_8225_read(sc, URTW_8225_ADDR_9_MAGIC, &data);
3220 if (data != URTW_8225_ADDR_9_DATA_MAGIC1)
3224 urtw_8225_write(sc, URTW_8225_ADDR_0_MAGIC,
3225 URTW_8225_ADDR_0_DATA_MAGIC2);
3231 urtw_8225v2b_rf_init(struct urtw_softc *sc)
3233 #define N(a) ((int)(sizeof(a) / sizeof((a)[0])))
3238 error = urtw_set_mode(sc, URTW_EPROM_CMD_CONFIG);
3243 * initialize extra registers on 8187
3245 urtw_write16_m(sc, URTW_BRSR_8187B, 0xfff);
3248 urtw_read8_m(sc, URTW_CW_CONF, &data8);
3249 data8 |= URTW_CW_CONF_PERPACKET_RETRY;
3250 urtw_write8_m(sc, URTW_CW_CONF, data8);
3253 urtw_read8_m(sc, URTW_TX_AGC_CTL, &data8);
3254 data8 |= URTW_TX_AGC_CTL_PERPACKET_GAIN;
3255 urtw_write8_m(sc, URTW_TX_AGC_CTL, data8);
3257 /* Auto Rate Fallback Control */
3258 #define URTW_ARFR 0x1e0
3259 urtw_write16_m(sc, URTW_ARFR, 0xfff);
3260 urtw_read8_m(sc, URTW_RATE_FALLBACK, &data8);
3261 urtw_write8_m(sc, URTW_RATE_FALLBACK,
3262 data8 | URTW_RATE_FALLBACK_ENABLE);
3264 urtw_read8_m(sc, URTW_MSR, &data8);
3265 urtw_write8_m(sc, URTW_MSR, data8 & 0xf3);
3266 urtw_read8_m(sc, URTW_MSR, &data8);
3267 urtw_write8_m(sc, URTW_MSR, data8 | URTW_MSR_LINK_ENEDCA);
3268 urtw_write8_m(sc, URTW_ACM_CONTROL, sc->sc_acmctl);
3270 urtw_write16_m(sc, URTW_ATIM_WND, 2);
3271 urtw_write16_m(sc, URTW_BEACON_INTERVAL, 100);
3272 #define URTW_FEMR_FOR_8187B 0x1d4
3273 urtw_write16_m(sc, URTW_FEMR_FOR_8187B, 0xffff);
3276 urtw_read8_m(sc, URTW_CONFIG1, &data8);
3277 data8 = (data8 & 0x3f) | 0x80;
3278 urtw_write8_m(sc, URTW_CONFIG1, data8);
3280 /* applying MAC address again. */
3281 urtw_write32_m(sc, URTW_MAC0, ((uint32_t *)sc->sc_bssid)[0]);
3282 urtw_write16_m(sc, URTW_MAC4, ((uint32_t *)sc->sc_bssid)[1] & 0xffff);
3284 error = urtw_set_mode(sc, URTW_EPROM_CMD_NORMAL);
3288 urtw_write8_m(sc, URTW_WPA_CONFIG, 0);
3293 for (i = 0; i < N(urtw_8225v2b_rf_part1); i++)
3294 urtw_write8_m(sc, urtw_8225v2b_rf_part1[i].reg,
3295 urtw_8225v2b_rf_part1[i].val);
3296 urtw_write16_m(sc, URTW_TID_AC_MAP, 0xfa50);
3297 urtw_write16_m(sc, URTW_INT_MIG, 0x0000);
3298 urtw_write32_m(sc, 0x1f0, 0);
3299 urtw_write32_m(sc, 0x1f4, 0);
3300 urtw_write8_m(sc, 0x1f8, 0);
3301 urtw_write32_m(sc, URTW_RF_TIMING, 0x4001);
3303 #define URTW_RFSW_CTRL 0x272
3304 urtw_write16_m(sc, URTW_RFSW_CTRL, 0x569a);
3309 error = urtw_set_mode(sc, URTW_EPROM_CMD_CONFIG);
3312 urtw_read8_m(sc, URTW_CONFIG3, &data8);
3313 urtw_write8_m(sc, URTW_CONFIG3,
3314 data8 | URTW_CONFIG3_ANAPARAM_WRITE);
3316 error = urtw_set_mode(sc, URTW_EPROM_CMD_NORMAL);
3320 /* setup RFE initial timing */
3321 urtw_write16_m(sc, URTW_RF_PINS_OUTPUT, 0x0480);
3322 urtw_write16_m(sc, URTW_RF_PINS_SELECT, 0x2488);
3323 urtw_write16_m(sc, URTW_RF_PINS_ENABLE, 0x1fff);
3324 usb_pause_mtx(&sc->sc_mtx, 1100);
3326 for (i = 0; i < N(urtw_8225v2b_rf_part0); i++) {
3327 urtw_8225_write(sc, urtw_8225v2b_rf_part0[i].reg,
3328 urtw_8225v2b_rf_part0[i].val);
3329 usb_pause_mtx(&sc->sc_mtx, 1);
3331 urtw_8225_write(sc, 0x00, 0x01b7);
3333 for (i = 0; i < 95; i++) {
3334 urtw_8225_write(sc, URTW_8225_ADDR_1_MAGIC, (uint8_t)(i + 1));
3335 usb_pause_mtx(&sc->sc_mtx, 1);
3336 urtw_8225_write(sc, URTW_8225_ADDR_2_MAGIC,
3337 urtw_8225v2b_rxgain[i]);
3338 usb_pause_mtx(&sc->sc_mtx, 1);
3341 urtw_8225_write(sc, URTW_8225_ADDR_3_MAGIC, 0x080);
3342 usb_pause_mtx(&sc->sc_mtx, 1);
3343 urtw_8225_write(sc, URTW_8225_ADDR_5_MAGIC, 0x004);
3344 usb_pause_mtx(&sc->sc_mtx, 1);
3345 urtw_8225_write(sc, URTW_8225_ADDR_0_MAGIC, 0x0b7);
3346 usb_pause_mtx(&sc->sc_mtx, 1);
3347 usb_pause_mtx(&sc->sc_mtx, 3000);
3348 urtw_8225_write(sc, URTW_8225_ADDR_2_MAGIC, 0xc4d);
3349 usb_pause_mtx(&sc->sc_mtx, 2000);
3350 urtw_8225_write(sc, URTW_8225_ADDR_2_MAGIC, 0x44d);
3351 usb_pause_mtx(&sc->sc_mtx, 1);
3352 urtw_8225_write(sc, URTW_8225_ADDR_0_MAGIC, 0x2bf);
3353 usb_pause_mtx(&sc->sc_mtx, 1);
3355 urtw_write8_m(sc, URTW_TX_GAIN_CCK, 0x03);
3356 urtw_write8_m(sc, URTW_TX_GAIN_OFDM, 0x07);
3357 urtw_write8_m(sc, URTW_TX_ANTENNA, 0x03);
3359 urtw_8187_write_phy_ofdm(sc, 0x80, 0x12);
3360 for (i = 0; i < 128; i++) {
3361 uint32_t addr, data;
3363 data = (urtw_8225z2_agc[i] << 8) | 0x0000008f;
3364 addr = ((i + 0x80) << 8) | 0x0000008e;
3366 urtw_8187_write_phy_ofdm(sc, data & 0x7f, (data >> 8) & 0xff);
3367 urtw_8187_write_phy_ofdm(sc, addr & 0x7f, (addr >> 8) & 0xff);
3368 urtw_8187_write_phy_ofdm(sc, 0x0e, 0x00);
3370 urtw_8187_write_phy_ofdm(sc, 0x80, 0x10);
3372 for (i = 0; i < N(urtw_8225v2b_rf_part2); i++)
3373 urtw_8187_write_phy_ofdm(sc, i, urtw_8225v2b_rf_part2[i].val);
3375 urtw_write32_m(sc, URTW_8187B_AC_VO, (7 << 12) | (3 << 8) | 0x1c);
3376 urtw_write32_m(sc, URTW_8187B_AC_VI, (7 << 12) | (3 << 8) | 0x1c);
3377 urtw_write32_m(sc, URTW_8187B_AC_BE, (7 << 12) | (3 << 8) | 0x1c);
3378 urtw_write32_m(sc, URTW_8187B_AC_BK, (7 << 12) | (3 << 8) | 0x1c);
3380 urtw_8187_write_phy_ofdm(sc, 0x97, 0x46);
3381 urtw_8187_write_phy_ofdm(sc, 0xa4, 0xb6);
3382 urtw_8187_write_phy_ofdm(sc, 0x85, 0xfc);
3383 urtw_8187_write_phy_cck(sc, 0xc1, 0x88);
3391 urtw_8225v2b_rf_set_chan(struct urtw_softc *sc, int chan)
3395 error = urtw_8225v2b_set_txpwrlvl(sc, chan);
3399 urtw_8225_write(sc, URTW_8225_ADDR_7_MAGIC, urtw_8225_channel[chan]);
3400 usb_pause_mtx(&sc->sc_mtx, 10);
3406 urtw_8225v2b_set_txpwrlvl(struct urtw_softc *sc, int chan)
3409 uint8_t *cck_pwrtable;
3410 uint8_t cck_pwrlvl_max = 15;
3411 uint8_t cck_pwrlvl = sc->sc_txpwr_cck[chan] & 0xff;
3412 uint8_t ofdm_pwrlvl = sc->sc_txpwr_ofdm[chan] & 0xff;
3415 /* CCK power setting */
3416 cck_pwrlvl = (cck_pwrlvl > cck_pwrlvl_max) ?
3417 ((sc->sc_flags & URTW_RTL8187B_REV_B) ? cck_pwrlvl_max : 22) :
3418 (cck_pwrlvl + ((sc->sc_flags & URTW_RTL8187B_REV_B) ? 0 : 7));
3419 cck_pwrlvl += sc->sc_txpwr_cck_base;
3420 cck_pwrlvl = (cck_pwrlvl > 35) ? 35 : cck_pwrlvl;
3421 cck_pwrtable = (chan == 14) ? urtw_8225v2b_txpwr_cck_ch14 :
3422 urtw_8225v2b_txpwr_cck;
3424 if (sc->sc_flags & URTW_RTL8187B_REV_B)
3425 cck_pwrtable += (cck_pwrlvl <= 6) ? 0 :
3426 ((cck_pwrlvl <= 11) ? 8 : 16);
3428 cck_pwrtable += (cck_pwrlvl <= 5) ? 0 :
3429 ((cck_pwrlvl <= 11) ? 8 : ((cck_pwrlvl <= 17) ? 16 : 24));
3431 for (i = 0; i < 8; i++)
3432 urtw_8187_write_phy_cck(sc, 0x44 + i, cck_pwrtable[i]);
3434 urtw_write8_m(sc, URTW_TX_GAIN_CCK,
3435 urtw_8225v2_tx_gain_cck_ofdm[cck_pwrlvl] << 1);
3436 usb_pause_mtx(&sc->sc_mtx, 1);
3438 /* OFDM power setting */
3439 ofdm_pwrlvl = (ofdm_pwrlvl > 15) ?
3440 ((sc->sc_flags & URTW_RTL8187B_REV_B) ? 17 : 25) :
3441 (ofdm_pwrlvl + ((sc->sc_flags & URTW_RTL8187B_REV_B) ? 2 : 10));
3442 ofdm_pwrlvl += sc->sc_txpwr_ofdm_base;
3443 ofdm_pwrlvl = (ofdm_pwrlvl > 35) ? 35 : ofdm_pwrlvl;
3445 urtw_write8_m(sc, URTW_TX_GAIN_OFDM,
3446 urtw_8225v2_tx_gain_cck_ofdm[ofdm_pwrlvl] << 1);
3448 if (sc->sc_flags & URTW_RTL8187B_REV_B) {
3449 if (ofdm_pwrlvl <= 11) {
3450 urtw_8187_write_phy_ofdm(sc, 0x87, 0x60);
3451 urtw_8187_write_phy_ofdm(sc, 0x89, 0x60);
3453 urtw_8187_write_phy_ofdm(sc, 0x87, 0x5c);
3454 urtw_8187_write_phy_ofdm(sc, 0x89, 0x5c);
3457 if (ofdm_pwrlvl <= 11) {
3458 urtw_8187_write_phy_ofdm(sc, 0x87, 0x5c);
3459 urtw_8187_write_phy_ofdm(sc, 0x89, 0x5c);
3460 } else if (ofdm_pwrlvl <= 17) {
3461 urtw_8187_write_phy_ofdm(sc, 0x87, 0x54);
3462 urtw_8187_write_phy_ofdm(sc, 0x89, 0x54);
3464 urtw_8187_write_phy_ofdm(sc, 0x87, 0x50);
3465 urtw_8187_write_phy_ofdm(sc, 0x89, 0x50);
3468 usb_pause_mtx(&sc->sc_mtx, 1);
3474 urtw_read8e(struct urtw_softc *sc, int val, uint8_t *data)
3476 struct usb_device_request req;
3479 req.bmRequestType = UT_READ_VENDOR_DEVICE;
3480 req.bRequest = URTW_8187_GETREGS_REQ;
3481 USETW(req.wValue, val | 0xfe00);
3482 USETW(req.wIndex, 0);
3483 USETW(req.wLength, sizeof(uint8_t));
3485 error = urtw_do_request(sc, &req, data);
3490 urtw_write8e(struct urtw_softc *sc, int val, uint8_t data)
3492 struct usb_device_request req;
3494 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
3495 req.bRequest = URTW_8187_SETREGS_REQ;
3496 USETW(req.wValue, val | 0xfe00);
3497 USETW(req.wIndex, 0);
3498 USETW(req.wLength, sizeof(uint8_t));
3500 return (urtw_do_request(sc, &req, &data));
3504 urtw_8180_set_anaparam(struct urtw_softc *sc, uint32_t val)
3509 error = urtw_set_mode(sc, URTW_EPROM_CMD_CONFIG);
3513 urtw_read8_m(sc, URTW_CONFIG3, &data);
3514 urtw_write8_m(sc, URTW_CONFIG3, data | URTW_CONFIG3_ANAPARAM_WRITE);
3515 urtw_write32_m(sc, URTW_ANAPARAM, val);
3516 urtw_read8_m(sc, URTW_CONFIG3, &data);
3517 urtw_write8_m(sc, URTW_CONFIG3, data & ~URTW_CONFIG3_ANAPARAM_WRITE);
3519 error = urtw_set_mode(sc, URTW_EPROM_CMD_NORMAL);
3527 urtw_8185_set_anaparam2(struct urtw_softc *sc, uint32_t val)
3532 error = urtw_set_mode(sc, URTW_EPROM_CMD_CONFIG);
3536 urtw_read8_m(sc, URTW_CONFIG3, &data);
3537 urtw_write8_m(sc, URTW_CONFIG3, data | URTW_CONFIG3_ANAPARAM_WRITE);
3538 urtw_write32_m(sc, URTW_ANAPARAM2, val);
3539 urtw_read8_m(sc, URTW_CONFIG3, &data);
3540 urtw_write8_m(sc, URTW_CONFIG3, data & ~URTW_CONFIG3_ANAPARAM_WRITE);
3542 error = urtw_set_mode(sc, URTW_EPROM_CMD_NORMAL);
3550 urtw_intr_enable(struct urtw_softc *sc)
3554 urtw_write16_m(sc, URTW_INTR_MASK, 0xffff);
3560 urtw_intr_disable(struct urtw_softc *sc)
3564 urtw_write16_m(sc, URTW_INTR_MASK, 0);
3570 urtw_reset(struct urtw_softc *sc)
3575 error = urtw_8180_set_anaparam(sc, URTW_8225_ANAPARAM_ON);
3578 error = urtw_8185_set_anaparam2(sc, URTW_8225_ANAPARAM2_ON);
3582 error = urtw_intr_disable(sc);
3585 usb_pause_mtx(&sc->sc_mtx, 100);
3587 error = urtw_write8e(sc, 0x18, 0x10);
3590 error = urtw_write8e(sc, 0x18, 0x11);
3593 error = urtw_write8e(sc, 0x18, 0x00);
3596 usb_pause_mtx(&sc->sc_mtx, 100);
3598 urtw_read8_m(sc, URTW_CMD, &data);
3599 data = (data & 0x2) | URTW_CMD_RST;
3600 urtw_write8_m(sc, URTW_CMD, data);
3601 usb_pause_mtx(&sc->sc_mtx, 100);
3603 urtw_read8_m(sc, URTW_CMD, &data);
3604 if (data & URTW_CMD_RST) {
3605 device_printf(sc->sc_dev, "reset timeout\n");
3609 error = urtw_set_mode(sc, URTW_EPROM_CMD_LOAD);
3612 usb_pause_mtx(&sc->sc_mtx, 100);
3614 error = urtw_8180_set_anaparam(sc, URTW_8225_ANAPARAM_ON);
3617 error = urtw_8185_set_anaparam2(sc, URTW_8225_ANAPARAM2_ON);
3625 urtw_led_ctl(struct urtw_softc *sc, int mode)
3627 usb_error_t error = 0;
3629 switch (sc->sc_strategy) {
3630 case URTW_SW_LED_MODE0:
3631 error = urtw_led_mode0(sc, mode);
3633 case URTW_SW_LED_MODE1:
3634 error = urtw_led_mode1(sc, mode);
3636 case URTW_SW_LED_MODE2:
3637 error = urtw_led_mode2(sc, mode);
3639 case URTW_SW_LED_MODE3:
3640 error = urtw_led_mode3(sc, mode);
3643 DPRINTF(sc, URTW_DEBUG_STATE,
3644 "unsupported LED mode %d\n", sc->sc_strategy);
3645 error = USB_ERR_INVAL;
3653 urtw_led_mode0(struct urtw_softc *sc, int mode)
3657 case URTW_LED_CTL_POWER_ON:
3658 sc->sc_gpio_ledstate = URTW_LED_POWER_ON_BLINK;
3660 case URTW_LED_CTL_TX:
3661 if (sc->sc_gpio_ledinprogress == 1)
3664 sc->sc_gpio_ledstate = URTW_LED_BLINK_NORMAL;
3665 sc->sc_gpio_blinktime = 2;
3667 case URTW_LED_CTL_LINK:
3668 sc->sc_gpio_ledstate = URTW_LED_ON;
3671 DPRINTF(sc, URTW_DEBUG_STATE,
3672 "unsupported LED mode 0x%x", mode);
3673 return (USB_ERR_INVAL);
3676 switch (sc->sc_gpio_ledstate) {
3678 if (sc->sc_gpio_ledinprogress != 0)
3680 urtw_led_on(sc, URTW_LED_GPIO);
3682 case URTW_LED_BLINK_NORMAL:
3683 if (sc->sc_gpio_ledinprogress != 0)
3685 sc->sc_gpio_ledinprogress = 1;
3686 sc->sc_gpio_blinkstate = (sc->sc_gpio_ledon != 0) ?
3687 URTW_LED_OFF : URTW_LED_ON;
3688 usb_callout_reset(&sc->sc_led_ch, hz, urtw_led_ch, sc);
3690 case URTW_LED_POWER_ON_BLINK:
3691 urtw_led_on(sc, URTW_LED_GPIO);
3692 usb_pause_mtx(&sc->sc_mtx, 100);
3693 urtw_led_off(sc, URTW_LED_GPIO);
3696 DPRINTF(sc, URTW_DEBUG_STATE,
3697 "unknown LED status 0x%x", sc->sc_gpio_ledstate);
3698 return (USB_ERR_INVAL);
3704 urtw_led_mode1(struct urtw_softc *sc, int mode)
3706 return (USB_ERR_INVAL);
3710 urtw_led_mode2(struct urtw_softc *sc, int mode)
3712 return (USB_ERR_INVAL);
3716 urtw_led_mode3(struct urtw_softc *sc, int mode)
3718 return (USB_ERR_INVAL);
3722 urtw_led_on(struct urtw_softc *sc, int type)
3726 if (type == URTW_LED_GPIO) {
3727 switch (sc->sc_gpio_ledpin) {
3728 case URTW_LED_PIN_GPIO0:
3729 urtw_write8_m(sc, URTW_GPIO, 0x01);
3730 urtw_write8_m(sc, URTW_GP_ENABLE, 0x00);
3733 DPRINTF(sc, URTW_DEBUG_STATE,
3734 "unsupported LED PIN type 0x%x",
3735 sc->sc_gpio_ledpin);
3736 error = USB_ERR_INVAL;
3740 DPRINTF(sc, URTW_DEBUG_STATE,
3741 "unsupported LED type 0x%x", type);
3742 error = USB_ERR_INVAL;
3746 sc->sc_gpio_ledon = 1;
3752 urtw_led_off(struct urtw_softc *sc, int type)
3756 if (type == URTW_LED_GPIO) {
3757 switch (sc->sc_gpio_ledpin) {
3758 case URTW_LED_PIN_GPIO0:
3759 urtw_write8_m(sc, URTW_GPIO, URTW_GPIO_DATA_MAGIC1);
3761 URTW_GP_ENABLE, URTW_GP_ENABLE_DATA_MAGIC1);
3764 DPRINTF(sc, URTW_DEBUG_STATE,
3765 "unsupported LED PIN type 0x%x",
3766 sc->sc_gpio_ledpin);
3767 error = USB_ERR_INVAL;
3771 DPRINTF(sc, URTW_DEBUG_STATE,
3772 "unsupported LED type 0x%x", type);
3773 error = USB_ERR_INVAL;
3777 sc->sc_gpio_ledon = 0;
3784 urtw_led_ch(void *arg)
3786 struct urtw_softc *sc = arg;
3787 struct ifnet *ifp = sc->sc_ifp;
3788 struct ieee80211com *ic = ifp->if_l2com;
3790 ieee80211_runtask(ic, &sc->sc_led_task);
3794 urtw_ledtask(void *arg, int pending)
3796 struct urtw_softc *sc = arg;
3798 if (sc->sc_strategy != URTW_SW_LED_MODE0) {
3799 DPRINTF(sc, URTW_DEBUG_STATE,
3800 "could not process a LED strategy 0x%x",
3811 urtw_led_blink(struct urtw_softc *sc)
3816 if (sc->sc_gpio_blinkstate == URTW_LED_ON)
3817 error = urtw_led_on(sc, URTW_LED_GPIO);
3819 error = urtw_led_off(sc, URTW_LED_GPIO);
3820 sc->sc_gpio_blinktime--;
3821 if (sc->sc_gpio_blinktime == 0)
3824 if (sc->sc_gpio_ledstate != URTW_LED_BLINK_NORMAL &&
3825 sc->sc_gpio_ledstate != URTW_LED_BLINK_SLOWLY &&
3826 sc->sc_gpio_ledstate != URTW_LED_BLINK_CM3)
3830 if (sc->sc_gpio_ledstate == URTW_LED_ON &&
3831 sc->sc_gpio_ledon == 0)
3832 error = urtw_led_on(sc, URTW_LED_GPIO);
3833 else if (sc->sc_gpio_ledstate == URTW_LED_OFF &&
3834 sc->sc_gpio_ledon == 1)
3835 error = urtw_led_off(sc, URTW_LED_GPIO);
3837 sc->sc_gpio_blinktime = 0;
3838 sc->sc_gpio_ledinprogress = 0;
3842 sc->sc_gpio_blinkstate = (sc->sc_gpio_blinkstate != URTW_LED_ON) ?
3843 URTW_LED_ON : URTW_LED_OFF;
3845 switch (sc->sc_gpio_ledstate) {
3846 case URTW_LED_BLINK_NORMAL:
3847 usb_callout_reset(&sc->sc_led_ch, hz, urtw_led_ch, sc);
3850 DPRINTF(sc, URTW_DEBUG_STATE,
3851 "unknown LED status 0x%x",
3852 sc->sc_gpio_ledstate);
3853 return (USB_ERR_INVAL);
3859 urtw_rx_enable(struct urtw_softc *sc)
3864 usbd_transfer_start((sc->sc_flags & URTW_RTL8187B) ?
3865 sc->sc_xfer[URTW_8187B_BULK_RX] : sc->sc_xfer[URTW_8187L_BULK_RX]);
3867 error = urtw_rx_setconf(sc);
3871 if ((sc->sc_flags & URTW_RTL8187B) == 0) {
3872 urtw_read8_m(sc, URTW_CMD, &data);
3873 urtw_write8_m(sc, URTW_CMD, data | URTW_CMD_RX_ENABLE);
3880 urtw_tx_enable(struct urtw_softc *sc)
3886 if (sc->sc_flags & URTW_RTL8187B) {
3887 urtw_read32_m(sc, URTW_TX_CONF, &data);
3888 data &= ~URTW_TX_LOOPBACK_MASK;
3889 data &= ~(URTW_TX_DPRETRY_MASK | URTW_TX_RTSRETRY_MASK);
3890 data &= ~(URTW_TX_NOCRC | URTW_TX_MXDMA_MASK);
3891 data &= ~URTW_TX_SWPLCPLEN;
3892 data |= URTW_TX_HW_SEQNUM | URTW_TX_DISREQQSIZE |
3893 (7 << 8) | /* short retry limit */
3894 (7 << 0) | /* long retry limit */
3895 (7 << 21); /* MAX TX DMA */
3896 urtw_write32_m(sc, URTW_TX_CONF, data);
3898 urtw_read8_m(sc, URTW_MSR, &data8);
3899 data8 |= URTW_MSR_LINK_ENEDCA;
3900 urtw_write8_m(sc, URTW_MSR, data8);
3904 urtw_read8_m(sc, URTW_CW_CONF, &data8);
3905 data8 &= ~(URTW_CW_CONF_PERPACKET_CW | URTW_CW_CONF_PERPACKET_RETRY);
3906 urtw_write8_m(sc, URTW_CW_CONF, data8);
3908 urtw_read8_m(sc, URTW_TX_AGC_CTL, &data8);
3909 data8 &= ~URTW_TX_AGC_CTL_PERPACKET_GAIN;
3910 data8 &= ~URTW_TX_AGC_CTL_PERPACKET_ANTSEL;
3911 data8 &= ~URTW_TX_AGC_CTL_FEEDBACK_ANT;
3912 urtw_write8_m(sc, URTW_TX_AGC_CTL, data8);
3914 urtw_read32_m(sc, URTW_TX_CONF, &data);
3915 data &= ~URTW_TX_LOOPBACK_MASK;
3916 data |= URTW_TX_LOOPBACK_NONE;
3917 data &= ~(URTW_TX_DPRETRY_MASK | URTW_TX_RTSRETRY_MASK);
3918 data |= sc->sc_tx_retry << URTW_TX_DPRETRY_SHIFT;
3919 data |= sc->sc_rts_retry << URTW_TX_RTSRETRY_SHIFT;
3920 data &= ~(URTW_TX_NOCRC | URTW_TX_MXDMA_MASK);
3921 data |= URTW_TX_MXDMA_2048 | URTW_TX_CWMIN | URTW_TX_DISCW;
3922 data &= ~URTW_TX_SWPLCPLEN;
3923 data |= URTW_TX_NOICV;
3924 urtw_write32_m(sc, URTW_TX_CONF, data);
3926 urtw_read8_m(sc, URTW_CMD, &data8);
3927 urtw_write8_m(sc, URTW_CMD, data8 | URTW_CMD_TX_ENABLE);
3933 urtw_rx_setconf(struct urtw_softc *sc)
3935 struct ifnet *ifp = sc->sc_ifp;
3936 struct ieee80211com *ic = ifp->if_l2com;
3940 urtw_read32_m(sc, URTW_RX, &data);
3941 data = data &~ URTW_RX_FILTER_MASK;
3942 if (sc->sc_flags & URTW_RTL8187B) {
3943 data = data | URTW_RX_FILTER_MNG | URTW_RX_FILTER_DATA |
3944 URTW_RX_FILTER_MCAST | URTW_RX_FILTER_BCAST |
3945 URTW_RX_FILTER_NICMAC | URTW_RX_CHECK_BSSID |
3946 URTW_RX_FIFO_THRESHOLD_NONE |
3947 URTW_MAX_RX_DMA_2048 |
3948 URTW_RX_AUTORESETPHY | URTW_RCR_ONLYERLPKT;
3950 data = data | URTW_RX_FILTER_MNG | URTW_RX_FILTER_DATA;
3951 data = data | URTW_RX_FILTER_BCAST | URTW_RX_FILTER_MCAST;
3953 if (ic->ic_opmode == IEEE80211_M_MONITOR) {
3954 data = data | URTW_RX_FILTER_ICVERR;
3955 data = data | URTW_RX_FILTER_PWR;
3957 if (sc->sc_crcmon == 1 && ic->ic_opmode == IEEE80211_M_MONITOR)
3958 data = data | URTW_RX_FILTER_CRCERR;
3960 if (ic->ic_opmode == IEEE80211_M_MONITOR ||
3961 (ifp->if_flags & (IFF_ALLMULTI | IFF_PROMISC))) {
3962 data = data | URTW_RX_FILTER_ALLMAC;
3964 data = data | URTW_RX_FILTER_NICMAC;
3965 data = data | URTW_RX_CHECK_BSSID;
3968 data = data &~ URTW_RX_FIFO_THRESHOLD_MASK;
3969 data = data | URTW_RX_FIFO_THRESHOLD_NONE |
3970 URTW_RX_AUTORESETPHY;
3971 data = data &~ URTW_MAX_RX_DMA_MASK;
3972 data = data | URTW_MAX_RX_DMA_2048 | URTW_RCR_ONLYERLPKT;
3975 urtw_write32_m(sc, URTW_RX, data);
3980 static struct mbuf *
3981 urtw_rxeof(struct usb_xfer *xfer, struct urtw_data *data, int *rssi_p,
3984 int actlen, flen, rssi;
3985 struct ieee80211_frame *wh;
3986 struct mbuf *m, *mnew;
3987 struct urtw_softc *sc = data->sc;
3988 struct ifnet *ifp = sc->sc_ifp;
3989 struct ieee80211com *ic = ifp->if_l2com;
3990 uint8_t noise = 0, rate;
3992 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
3994 if (actlen < (int)URTW_MIN_RXBUFSZ) {
3999 if (sc->sc_flags & URTW_RTL8187B) {
4000 struct urtw_8187b_rxhdr *rx;
4002 rx = (struct urtw_8187b_rxhdr *)(data->buf +
4003 (actlen - (sizeof(struct urtw_8187b_rxhdr))));
4004 flen = le32toh(rx->flag) & 0xfff;
4005 if (flen > actlen) {
4009 rate = (le32toh(rx->flag) >> URTW_RX_FLAG_RXRATE_SHIFT) & 0xf;
4011 rssi = rx->rssi & URTW_RX_RSSI_MASK;
4014 struct urtw_8187l_rxhdr *rx;
4016 rx = (struct urtw_8187l_rxhdr *)(data->buf +
4017 (actlen - (sizeof(struct urtw_8187l_rxhdr))));
4018 flen = le32toh(rx->flag) & 0xfff;
4019 if (flen > actlen) {
4024 rate = (le32toh(rx->flag) >> URTW_RX_FLAG_RXRATE_SHIFT) & 0xf;
4026 rssi = rx->rssi & URTW_RX_8187L_RSSI_MASK;
4030 mnew = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
4038 data->buf = mtod(mnew, uint8_t *);
4041 m->m_pkthdr.rcvif = ifp;
4042 m->m_pkthdr.len = m->m_len = flen - IEEE80211_CRC_LEN;
4044 if (ieee80211_radiotap_active(ic)) {
4045 struct urtw_rx_radiotap_header *tap = &sc->sc_rxtap;
4047 /* XXX Are variables correct? */
4048 tap->wr_chan_freq = htole16(ic->ic_curchan->ic_freq);
4049 tap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
4050 tap->wr_dbm_antsignal = (int8_t)rssi;
4053 wh = mtod(m, struct ieee80211_frame *);
4054 if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_DATA)
4055 sc->sc_currate = (rate > 0) ? rate : sc->sc_currate;
4058 *nf_p = noise; /* XXX correct? */
4064 urtw_bulk_rx_callback(struct usb_xfer *xfer, usb_error_t error)
4066 struct urtw_softc *sc = usbd_xfer_softc(xfer);
4067 struct ifnet *ifp = sc->sc_ifp;
4068 struct ieee80211com *ic = ifp->if_l2com;
4069 struct ieee80211_frame *wh;
4070 struct ieee80211_node *ni;
4071 struct mbuf *m = NULL;
4072 struct urtw_data *data;
4076 URTW_ASSERT_LOCKED(sc);
4078 switch (USB_GET_STATE(xfer)) {
4079 case USB_ST_TRANSFERRED:
4080 data = STAILQ_FIRST(&sc->sc_rx_active);
4083 STAILQ_REMOVE_HEAD(&sc->sc_rx_active, next);
4084 m = urtw_rxeof(xfer, data, &rssi, &nf);
4085 STAILQ_INSERT_TAIL(&sc->sc_rx_inactive, data, next);
4089 data = STAILQ_FIRST(&sc->sc_rx_inactive);
4091 KASSERT(m == NULL, ("mbuf isn't NULL"));
4094 STAILQ_REMOVE_HEAD(&sc->sc_rx_inactive, next);
4095 STAILQ_INSERT_TAIL(&sc->sc_rx_active, data, next);
4096 usbd_xfer_set_frame_data(xfer, 0, data->buf,
4097 usbd_xfer_max_len(xfer));
4098 usbd_transfer_submit(xfer);
4101 * To avoid LOR we should unlock our private mutex here to call
4102 * ieee80211_input() because here is at the end of a USB
4103 * callback and safe to unlock.
4107 wh = mtod(m, struct ieee80211_frame *);
4108 ni = ieee80211_find_rxnode(ic,
4109 (struct ieee80211_frame_min *)wh);
4111 (void) ieee80211_input(ni, m, rssi, nf);
4112 /* node is no longer needed */
4113 ieee80211_free_node(ni);
4115 (void) ieee80211_input_all(ic, m, rssi, nf);
4121 /* needs it to the inactive queue due to a error. */
4122 data = STAILQ_FIRST(&sc->sc_rx_active);
4124 STAILQ_REMOVE_HEAD(&sc->sc_rx_active, next);
4125 STAILQ_INSERT_TAIL(&sc->sc_rx_inactive, data, next);
4127 if (error != USB_ERR_CANCELLED) {
4128 usbd_xfer_set_stall(xfer);
4136 #define URTW_STATUS_TYPE_TXCLOSE 1
4137 #define URTW_STATUS_TYPE_BEACON_INTR 0
4140 urtw_txstatus_eof(struct usb_xfer *xfer)
4142 struct urtw_softc *sc = usbd_xfer_softc(xfer);
4143 struct ifnet *ifp = sc->sc_ifp;
4144 int actlen, type, pktretry, seq;
4147 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
4149 if (actlen != sizeof(uint64_t))
4152 val = le64toh(sc->sc_txstatus);
4153 type = (val >> 30) & 0x3;
4154 if (type == URTW_STATUS_TYPE_TXCLOSE) {
4155 pktretry = val & 0xff;
4156 seq = (val >> 16) & 0xff;
4157 if (pktretry == URTW_TX_MAXRETRY)
4159 DPRINTF(sc, URTW_DEBUG_TXSTATUS, "pktretry %d seq %#x\n",
4165 urtw_bulk_tx_status_callback(struct usb_xfer *xfer, usb_error_t error)
4167 struct urtw_softc *sc = usbd_xfer_softc(xfer);
4168 struct ifnet *ifp = sc->sc_ifp;
4169 void *dma_buf = usbd_xfer_get_frame_buffer(xfer, 0);
4171 URTW_ASSERT_LOCKED(sc);
4173 switch (USB_GET_STATE(xfer)) {
4174 case USB_ST_TRANSFERRED:
4175 urtw_txstatus_eof(xfer);
4179 memcpy(dma_buf, &sc->sc_txstatus, sizeof(uint64_t));
4180 usbd_transfer_submit(xfer);
4183 if (error != USB_ERR_CANCELLED) {
4184 usbd_xfer_set_stall(xfer);
4193 urtw_txeof(struct usb_xfer *xfer, struct urtw_data *data)
4195 struct urtw_softc *sc = usbd_xfer_softc(xfer);
4196 struct ifnet *ifp = sc->sc_ifp;
4199 URTW_ASSERT_LOCKED(sc);
4202 * Do any tx complete callback. Note this must be done before releasing
4203 * the node reference.
4207 if (m->m_flags & M_TXCB) {
4209 ieee80211_process_callback(data->ni, m, 0);
4215 ieee80211_free_node(data->ni);
4220 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
4224 urtw_bulk_tx_callback(struct usb_xfer *xfer, usb_error_t error)
4226 struct urtw_softc *sc = usbd_xfer_softc(xfer);
4227 struct ifnet *ifp = sc->sc_ifp;
4228 struct urtw_data *data;
4230 URTW_ASSERT_LOCKED(sc);
4232 switch (USB_GET_STATE(xfer)) {
4233 case USB_ST_TRANSFERRED:
4234 data = STAILQ_FIRST(&sc->sc_tx_active);
4237 STAILQ_REMOVE_HEAD(&sc->sc_tx_active, next);
4238 urtw_txeof(xfer, data);
4239 STAILQ_INSERT_TAIL(&sc->sc_tx_inactive, data, next);
4243 data = STAILQ_FIRST(&sc->sc_tx_pending);
4245 DPRINTF(sc, URTW_DEBUG_XMIT,
4246 "%s: empty pending queue\n", __func__);
4249 STAILQ_REMOVE_HEAD(&sc->sc_tx_pending, next);
4250 STAILQ_INSERT_TAIL(&sc->sc_tx_active, data, next);
4252 usbd_xfer_set_frame_data(xfer, 0, data->buf, data->buflen);
4253 usbd_transfer_submit(xfer);
4260 data = STAILQ_FIRST(&sc->sc_tx_active);
4263 if (data->ni != NULL) {
4264 ieee80211_free_node(data->ni);
4268 if (error != USB_ERR_CANCELLED) {
4269 usbd_xfer_set_stall(xfer);
4276 static struct urtw_data *
4277 _urtw_getbuf(struct urtw_softc *sc)
4279 struct urtw_data *bf;
4281 bf = STAILQ_FIRST(&sc->sc_tx_inactive);
4283 STAILQ_REMOVE_HEAD(&sc->sc_tx_inactive, next);
4287 DPRINTF(sc, URTW_DEBUG_XMIT, "%s: %s\n", __func__,
4288 "out of xmit buffers");
4292 static struct urtw_data *
4293 urtw_getbuf(struct urtw_softc *sc)
4295 struct urtw_data *bf;
4297 URTW_ASSERT_LOCKED(sc);
4299 bf = _urtw_getbuf(sc);
4301 struct ifnet *ifp = sc->sc_ifp;
4303 DPRINTF(sc, URTW_DEBUG_XMIT, "%s: stop queue\n", __func__);
4304 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
4310 urtw_isbmode(uint16_t rate)
4313 return ((rate <= 22 && rate != 12 && rate != 18) ||
4314 rate == 44) ? (1) : (0);
4318 urtw_rate2dbps(uint16_t rate)
4338 urtw_compute_txtime(uint16_t framelen, uint16_t rate,
4339 uint8_t ismgt, uint8_t isshort)
4341 uint16_t ceiling, frametime, n_dbps;
4343 if (urtw_isbmode(rate)) {
4344 if (ismgt || !isshort || rate == 2)
4345 frametime = (uint16_t)(144 + 48 +
4346 (framelen * 8 / (rate / 2)));
4348 frametime = (uint16_t)(72 + 24 +
4349 (framelen * 8 / (rate / 2)));
4350 if ((framelen * 8 % (rate / 2)) != 0)
4353 n_dbps = urtw_rate2dbps(rate);
4354 ceiling = (16 + 8 * framelen + 6) / n_dbps
4355 + (((16 + 8 * framelen + 6) % n_dbps) ? 1 : 0);
4356 frametime = (uint16_t)(16 + 4 + 4 * ceiling + 6);
4362 * Callback from the 802.11 layer to update the
4363 * slot time based on the current setting.
4366 urtw_updateslot(struct ifnet *ifp)
4368 struct urtw_softc *sc = ifp->if_softc;
4369 struct ieee80211com *ic = ifp->if_l2com;
4371 ieee80211_runtask(ic, &sc->sc_updateslot_task);
4375 urtw_updateslottask(void *arg, int pending)
4377 struct urtw_softc *sc = arg;
4378 struct ifnet *ifp = sc->sc_ifp;
4379 struct ieee80211com *ic = ifp->if_l2com;
4382 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
4386 if (sc->sc_flags & URTW_RTL8187B) {
4387 urtw_write8_m(sc, URTW_SIFS, 0x22);
4388 if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan))
4389 urtw_write8_m(sc, URTW_SLOT, 0x9);
4391 urtw_write8_m(sc, URTW_SLOT, 0x14);
4392 urtw_write8_m(sc, URTW_8187B_EIFS, 0x5b);
4393 urtw_write8_m(sc, URTW_CARRIER_SCOUNT, 0x5b);
4395 urtw_write8_m(sc, URTW_SIFS, 0x22);
4396 if (sc->sc_state == IEEE80211_S_ASSOC &&
4397 ic->ic_flags & IEEE80211_F_SHSLOT)
4398 urtw_write8_m(sc, URTW_SLOT, 0x9);
4400 urtw_write8_m(sc, URTW_SLOT, 0x14);
4401 if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan)) {
4402 urtw_write8_m(sc, URTW_DIFS, 0x14);
4403 urtw_write8_m(sc, URTW_EIFS, 0x5b - 0x14);
4404 urtw_write8_m(sc, URTW_CW_VAL, 0x73);
4406 urtw_write8_m(sc, URTW_DIFS, 0x24);
4407 urtw_write8_m(sc, URTW_EIFS, 0x5b - 0x24);
4408 urtw_write8_m(sc, URTW_CW_VAL, 0xa5);
4416 urtw_sysctl_node(struct urtw_softc *sc)
4418 #define URTW_SYSCTL_STAT_ADD32(c, h, n, p, d) \
4419 SYSCTL_ADD_UINT(c, h, OID_AUTO, n, CTLFLAG_RD, p, 0, d)
4420 struct sysctl_ctx_list *ctx;
4421 struct sysctl_oid_list *child, *parent;
4422 struct sysctl_oid *tree;
4423 struct urtw_stats *stats = &sc->sc_stats;
4425 ctx = device_get_sysctl_ctx(sc->sc_dev);
4426 child = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->sc_dev));
4428 tree = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, "stats", CTLFLAG_RD,
4429 NULL, "URTW statistics");
4430 parent = SYSCTL_CHILDREN(tree);
4432 /* Tx statistics. */
4433 tree = SYSCTL_ADD_NODE(ctx, parent, OID_AUTO, "tx", CTLFLAG_RD,
4434 NULL, "Tx MAC statistics");
4435 child = SYSCTL_CHILDREN(tree);
4436 URTW_SYSCTL_STAT_ADD32(ctx, child, "1m", &stats->txrates[0],
4438 URTW_SYSCTL_STAT_ADD32(ctx, child, "2m", &stats->txrates[1],
4440 URTW_SYSCTL_STAT_ADD32(ctx, child, "5.5m", &stats->txrates[2],
4442 URTW_SYSCTL_STAT_ADD32(ctx, child, "6m", &stats->txrates[4],
4444 URTW_SYSCTL_STAT_ADD32(ctx, child, "9m", &stats->txrates[5],
4446 URTW_SYSCTL_STAT_ADD32(ctx, child, "11m", &stats->txrates[3],
4448 URTW_SYSCTL_STAT_ADD32(ctx, child, "12m", &stats->txrates[6],
4450 URTW_SYSCTL_STAT_ADD32(ctx, child, "18m", &stats->txrates[7],
4452 URTW_SYSCTL_STAT_ADD32(ctx, child, "24m", &stats->txrates[8],
4454 URTW_SYSCTL_STAT_ADD32(ctx, child, "36m", &stats->txrates[9],
4456 URTW_SYSCTL_STAT_ADD32(ctx, child, "48m", &stats->txrates[10],
4458 URTW_SYSCTL_STAT_ADD32(ctx, child, "54m", &stats->txrates[11],
4460 #undef URTW_SYSCTL_STAT_ADD32
4463 static device_method_t urtw_methods[] = {
4464 DEVMETHOD(device_probe, urtw_match),
4465 DEVMETHOD(device_attach, urtw_attach),
4466 DEVMETHOD(device_detach, urtw_detach),
4469 static driver_t urtw_driver = {
4471 .methods = urtw_methods,
4472 .size = sizeof(struct urtw_softc)
4474 static devclass_t urtw_devclass;
4476 DRIVER_MODULE(urtw, uhub, urtw_driver, urtw_devclass, NULL, 0);
4477 MODULE_DEPEND(urtw, wlan, 1, 1, 1);
4478 MODULE_DEPEND(urtw, usb, 1, 1, 1);
4479 MODULE_VERSION(urtw, 1);