xref: /illumos-gate/usr/src/uts/common/io/urtw/urtw.c (revision 15c07adc)
1 /*
2  * Copyright (c) 2018, Joyent, Inc.
3  */
4 
5 /*
6  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
7  * Use is subject to license terms.
8  */
9 
10 /*
11  * Copyright (c) 2008 Weongyo Jeong
12  * All rights reserved.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer,
19  *    without modification.
20  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
21  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
22  *    redistribution must be conditioned upon including a substantially
23  *    similar Disclaimer requirement for further binary redistribution.
24  *
25  * NO WARRANTY
26  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
29  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
30  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
31  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
34  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
36  * THE POSSIBILITY OF SUCH DAMAGES.
37  */
38 #include <sys/sysmacros.h>
39 #include <sys/strsubr.h>
40 #include <sys/strsun.h>
41 #include <sys/mac_provider.h>
42 #include <sys/mac_wifi.h>
43 #include <sys/net80211.h>
44 #define	USBDRV_MAJOR_VER	2
45 #define	USBDRV_MINOR_VER	0
46 #include <sys/usb/usba.h>
47 #include <sys/usb/usba/usba_types.h>
48 
49 #include "urtw_reg.h"
50 #include "urtw_var.h"
51 
52 static void *urtw_soft_state_p = NULL;
53 
54 #define	URTW_TXBUF_SIZE  	(IEEE80211_MAX_LEN)
55 #define	URTW_RXBUF_SIZE  	(URTW_TXBUF_SIZE)
56 /*
57  * device operations
58  */
59 static int urtw_attach(dev_info_t *, ddi_attach_cmd_t);
60 static int urtw_detach(dev_info_t *, ddi_detach_cmd_t);
61 
62 /*
63  * Module Loading Data & Entry Points
64  */
65 DDI_DEFINE_STREAM_OPS(urtw_dev_ops, nulldev, nulldev, urtw_attach,
66     urtw_detach, nodev, NULL, D_MP, NULL, ddi_quiesce_not_needed);
67 
68 static struct modldrv urtw_modldrv = {
69 	&mod_driverops,		/* Type of module.  This one is a driver */
70 	"RTL8187L/B driver v1.2",	/* short description */
71 	&urtw_dev_ops		/* driver specific ops */
72 };
73 
74 static struct modlinkage modlinkage = {
75 	MODREV_1,
76 	(void *)&urtw_modldrv,
77 	NULL
78 };
79 
80 static int	urtw_m_stat(void *,  uint_t, uint64_t *);
81 static int	urtw_m_start(void *);
82 static void	urtw_m_stop(void *);
83 static int	urtw_m_promisc(void *, boolean_t);
84 static int	urtw_m_multicst(void *, boolean_t, const uint8_t *);
85 static int	urtw_m_unicst(void *, const uint8_t *);
86 static mblk_t	*urtw_m_tx(void *, mblk_t *);
87 static void	urtw_m_ioctl(void *, queue_t *, mblk_t *);
88 static int	urtw_m_setprop(void *, const char *, mac_prop_id_t,
89     uint_t, const void *);
90 static int	urtw_m_getprop(void *, const char *, mac_prop_id_t,
91     uint_t, void *);
92 static void	urtw_m_propinfo(void *, const char *, mac_prop_id_t,
93     mac_prop_info_handle_t);
94 
95 static mac_callbacks_t urtw_m_callbacks = {
96 	MC_IOCTL | MC_SETPROP | MC_GETPROP | MC_PROPINFO,
97 	urtw_m_stat,
98 	urtw_m_start,
99 	urtw_m_stop,
100 	urtw_m_promisc,
101 	urtw_m_multicst,
102 	urtw_m_unicst,
103 	urtw_m_tx,
104 	NULL,
105 	urtw_m_ioctl,
106 	NULL,
107 	NULL,
108 	NULL,
109 	urtw_m_setprop,
110 	urtw_m_getprop,
111 	urtw_m_propinfo
112 };
113 
114 static int  urtw_tx_start(struct urtw_softc *, mblk_t *, int);
115 static int  urtw_rx_start(struct urtw_softc *);
116 
117 
118 /*
119  * Supported rates for 802.11b/g modes (in 500Kbps unit).
120  */
121 static const struct ieee80211_rateset urtw_rateset_11b =
122 	{ 4, { 2, 4, 11, 22 } };
123 
124 static const struct ieee80211_rateset urtw_rateset_11g =
125 	{ 12, { 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108 } };
126 
127 #define	USB_VENDOR_DICKSMITH	0x1371		/* Dick Smith Electronics */
128 #define	USB_VENDOR_LOGITEC	0x0789		/* Logitec */
129 #define	USB_VENDOR_NETGEAR	0x0846		/* BayNETGEAR */
130 #define	USB_VENDOR_REALTEK	0x0bda		/* Realtek */
131 #define	USB_VENDOR_SPHAIRON	0x114b		/* Sphairon Access Systems */
132 #define	USB_VENDOR_SURECOM	0x0769		/* Surecom Technology */
133 #define	USB_VENDOR_BELKIN	0x050d		/* Belkin Components */
134 #define	USB_VENDOR_SITECOMEU	0x0df6		/* Sitecom Europe */
135 
136 #define	USB_PRODUCT_SPHAIRON_RTL8187	0x0150		/* RTL8187 */
137 #define	USB_PRODUCT_DICKSMITH_RTL8187	0x9401		/* RTL8187 */
138 #define	USB_PRODUCT_LOGITEC_RTL8187	0x010c		/* RTL8187 */
139 #define	USB_PRODUCT_REALTEK_RTL8187	0x8187		/* RTL8187 */
140 #define	USB_PRODUCT_NETGEAR_WG111V2	0x6a00		/* WG111v2 */
141 #define	USB_PRODUCT_SURECOM_EP9001G2A	0x11f2		/* EP-9001-G rev 2A */
142 #define	USB_PRODUCT_BELKIN_F5D7050E	0x705e		/* F5D705E 54g */
143 #define	USB_PRODUCT_NETGEAR_WG111V3	0x4260		/* WG111v3 */
144 #define	USB_PRODUCT_REALTEK_RTL8187B_0	0x8189		/* RTL8187B */
145 #define	USB_PRODUCT_REALTEK_RTL8187B_1	0x8197		/* RTL8187B */
146 #define	USB_PRODUCT_REALTEK_RTL8187B_2	0x8198		/* RTL8187B */
147 #define	USB_PRODUCT_SITECOMEU_WL168	0x0028		/* WL-168 */
148 
149 #define	USB_PRODUCT_ANY	0xffff
150 
151 struct usb_devno {
152 	uint16_t v;
153 	uint16_t p;
154 };
155 
156 /*
157  * Recognized device vendors/products.
158  */
159 static struct urtw_type {
160 	struct usb_devno	dev;
161 	uint8_t			rev;
162 } urtw_devs[] = {
163 #define	URTW_DEV_RTL8187(v, p)	\
164 	    { { USB_VENDOR_##v, USB_PRODUCT_##v##_##p }, URTW_HWREV_8187 }
165 #define	URTW_DEV_RTL8187B(v, p)	\
166 	    { { USB_VENDOR_##v, USB_PRODUCT_##v##_##p }, URTW_HWREV_8187B }
167 	/* Realtek RTL8187 devices. */
168 	URTW_DEV_RTL8187(DICKSMITH,	RTL8187),
169 	URTW_DEV_RTL8187(LOGITEC,	RTL8187),
170 	URTW_DEV_RTL8187(NETGEAR,	WG111V2),
171 	URTW_DEV_RTL8187(REALTEK,	RTL8187),
172 	URTW_DEV_RTL8187(SPHAIRON,	RTL8187),
173 	URTW_DEV_RTL8187(SURECOM,	EP9001G2A),
174 	/* Realtek RTL8187B devices. */
175 	URTW_DEV_RTL8187B(BELKIN,	F5D7050E),
176 	URTW_DEV_RTL8187B(NETGEAR,	WG111V3),
177 	URTW_DEV_RTL8187B(REALTEK,	RTL8187B_0),
178 	URTW_DEV_RTL8187B(REALTEK,	RTL8187B_1),
179 	URTW_DEV_RTL8187B(REALTEK,	RTL8187B_2),
180 	URTW_DEV_RTL8187B(SITECOMEU,	WL168)
181 #undef	URTW_DEV_RTL8187
182 #undef	URTW_DEV_RTL8187B
183 };
184 
185 /*
186  * Search for a vendor/product pair in an array.  The item size is
187  * given as an argument.
188  */
189 struct urtw_type *
usb_match_device(struct urtw_type * tbl,uint32_t nentries,uint16_t vendor,uint16_t product)190 usb_match_device(struct urtw_type *tbl, uint32_t nentries,
191 	uint16_t vendor, uint16_t product)
192 {
193 	while (nentries-- > 0) {
194 		uint16_t tproduct = tbl[nentries].dev.p;
195 		if (tbl[nentries].dev.v == vendor &&
196 		    (tproduct == product || tproduct == USB_PRODUCT_ANY))
197 			return (&tbl[nentries]);
198 	}
199 	return (NULL);
200 }
201 
202 #define	usb_lookup(tbl, vendor, product) \
203 	usb_match_device(tbl, sizeof (tbl) / sizeof ((tbl)[0]), \
204 		(vendor), (product))
205 
206 #define	urtw_lookup(v, p)	(usb_lookup(urtw_devs, v, p))
207 
208 struct urtw_pair {
209 	uint32_t	reg;
210 	uint32_t	val;
211 };
212 
213 struct urtw_pair_idx {
214 	uint8_t		reg;
215 	uint8_t		val;
216 	uint8_t		idx;
217 };
218 
219 static struct urtw_pair_idx urtw_8187b_regtbl[] = {
220 	{ 0xf0, 0x32, 0 }, { 0xf1, 0x32, 0 }, { 0xf2, 0x00, 0 },
221 	{ 0xf3, 0x00, 0 }, { 0xf4, 0x32, 0 }, { 0xf5, 0x43, 0 },
222 	{ 0xf6, 0x00, 0 }, { 0xf7, 0x00, 0 }, { 0xf8, 0x46, 0 },
223 	{ 0xf9, 0xa4, 0 }, { 0xfa, 0x00, 0 }, { 0xfb, 0x00, 0 },
224 	{ 0xfc, 0x96, 0 }, { 0xfd, 0xa4, 0 }, { 0xfe, 0x00, 0 },
225 	{ 0xff, 0x00, 0 },
226 
227 	{ 0x58, 0x4b, 1 }, { 0x59, 0x00, 1 }, { 0x5a, 0x4b, 1 },
228 	{ 0x5b, 0x00, 1 }, { 0x60, 0x4b, 1 }, { 0x61, 0x09, 1 },
229 	{ 0x62, 0x4b, 1 }, { 0x63, 0x09, 1 }, { 0xce, 0x0f, 1 },
230 	{ 0xcf, 0x00, 1 }, { 0xe0, 0xff, 1 }, { 0xe1, 0x0f, 1 },
231 	{ 0xe2, 0x00, 1 }, { 0xf0, 0x4e, 1 }, { 0xf1, 0x01, 1 },
232 	{ 0xf2, 0x02, 1 }, { 0xf3, 0x03, 1 }, { 0xf4, 0x04, 1 },
233 	{ 0xf5, 0x05, 1 }, { 0xf6, 0x06, 1 }, { 0xf7, 0x07, 1 },
234 	{ 0xf8, 0x08, 1 },
235 
236 	{ 0x4e, 0x00, 2 }, { 0x0c, 0x04, 2 }, { 0x21, 0x61, 2 },
237 	{ 0x22, 0x68, 2 }, { 0x23, 0x6f, 2 }, { 0x24, 0x76, 2 },
238 	{ 0x25, 0x7d, 2 }, { 0x26, 0x84, 2 }, { 0x27, 0x8d, 2 },
239 	{ 0x4d, 0x08, 2 }, { 0x50, 0x05, 2 }, { 0x51, 0xf5, 2 },
240 	{ 0x52, 0x04, 2 }, { 0x53, 0xa0, 2 }, { 0x54, 0x1f, 2 },
241 	{ 0x55, 0x23, 2 }, { 0x56, 0x45, 2 }, { 0x57, 0x67, 2 },
242 	{ 0x58, 0x08, 2 }, { 0x59, 0x08, 2 }, { 0x5a, 0x08, 2 },
243 	{ 0x5b, 0x08, 2 }, { 0x60, 0x08, 2 }, { 0x61, 0x08, 2 },
244 	{ 0x62, 0x08, 2 }, { 0x63, 0x08, 2 }, { 0x64, 0xcf, 2 },
245 	{ 0x72, 0x56, 2 }, { 0x73, 0x9a, 2 },
246 
247 	{ 0x34, 0xf0, 0 }, { 0x35, 0x0f, 0 }, { 0x5b, 0x40, 0 },
248 	{ 0x84, 0x88, 0 }, { 0x85, 0x24, 0 }, { 0x88, 0x54, 0 },
249 	{ 0x8b, 0xb8, 0 }, { 0x8c, 0x07, 0 }, { 0x8d, 0x00, 0 },
250 	{ 0x94, 0x1b, 0 }, { 0x95, 0x12, 0 }, { 0x96, 0x00, 0 },
251 	{ 0x97, 0x06, 0 }, { 0x9d, 0x1a, 0 }, { 0x9f, 0x10, 0 },
252 	{ 0xb4, 0x22, 0 }, { 0xbe, 0x80, 0 }, { 0xdb, 0x00, 0 },
253 	{ 0xee, 0x00, 0 }, { 0x91, 0x03, 0 },
254 
255 	{ 0x4c, 0x00, 2 }, { 0x9f, 0x00, 3 }, { 0x8c, 0x01, 0 },
256 	{ 0x8d, 0x10, 0 }, { 0x8e, 0x08, 0 }, { 0x8f, 0x00, 0 }
257 };
258 
259 static uint8_t urtw_8225_agc[] = {
260 	0x9e, 0x9e, 0x9e, 0x9e, 0x9e, 0x9e, 0x9e, 0x9e, 0x9d, 0x9c, 0x9b,
261 	0x9a, 0x99, 0x98, 0x97, 0x96, 0x95, 0x94, 0x93, 0x92, 0x91, 0x90,
262 	0x8f, 0x8e, 0x8d, 0x8c, 0x8b, 0x8a, 0x89, 0x88, 0x87, 0x86, 0x85,
263 	0x84, 0x83, 0x82, 0x81, 0x80, 0x3f, 0x3e, 0x3d, 0x3c, 0x3b, 0x3a,
264 	0x39, 0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x31, 0x30, 0x2f,
265 	0x2e, 0x2d, 0x2c, 0x2b, 0x2a, 0x29, 0x28, 0x27, 0x26, 0x25, 0x24,
266 	0x23, 0x22, 0x21, 0x20, 0x1f, 0x1e, 0x1d, 0x1c, 0x1b, 0x1a, 0x19,
267 	0x18, 0x17, 0x16, 0x15, 0x14, 0x13, 0x12, 0x11, 0x10, 0x0f, 0x0e,
268 	0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08, 0x07, 0x06, 0x05, 0x04, 0x03,
269 	0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
270 	0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
271 	0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01
272 };
273 
274 static uint8_t urtw_8225v2_agc[] = {
275 	0x5e, 0x5e, 0x5e, 0x5e, 0x5d, 0x5b, 0x59, 0x57,
276 	0x55, 0x53, 0x51, 0x4f, 0x4d, 0x4b, 0x49, 0x47,
277 	0x45, 0x43, 0x41, 0x3f, 0x3d, 0x3b, 0x39, 0x37,
278 	0x35, 0x33, 0x31, 0x2f, 0x2d, 0x2b, 0x29, 0x27,
279 	0x25, 0x23, 0x21, 0x1f, 0x1d, 0x1b, 0x19, 0x17,
280 	0x15, 0x13, 0x11, 0x0f, 0x0d, 0x0b, 0x09, 0x07,
281 	0x05, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
282 	0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
283 	0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19,
284 	0x19, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26,
285 	0x26, 0x27, 0x27, 0x28, 0x28, 0x29, 0x2a, 0x2a,
286 	0x2a, 0x2b, 0x2b, 0x2b, 0x2c, 0x2c, 0x2c, 0x2d,
287 	0x2d, 0x2d, 0x2d, 0x2e, 0x2e, 0x2e, 0x2e, 0x2f,
288 	0x2f, 0x2f, 0x30, 0x30, 0x31, 0x31, 0x31, 0x31,
289 	0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31,
290 	0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31
291 };
292 
293 static uint8_t urtw_8225v2_ofdm[] = {
294 	0x10, 0x0d, 0x01, 0x00, 0x14, 0xfb, 0xfb, 0x60,
295 	0x00, 0x60, 0x00, 0x00, 0x00, 0x5c, 0x00, 0x00,
296 	0x40, 0x00, 0x40, 0x00, 0x00, 0x00, 0xa8, 0x26,
297 	0x32, 0x33, 0x07, 0xa5, 0x6f, 0x55, 0xc8, 0xb3,
298 	0x0a, 0xe1, 0x2c, 0x8a, 0x86, 0x83, 0x34, 0x0f,
299 	0x4f, 0x24, 0x6f, 0xc2, 0x6b, 0x40, 0x80, 0x00,
300 	0xc0, 0xc1, 0x58, 0xf1, 0x00, 0xe4, 0x90, 0x3e,
301 	0x6d, 0x3c, 0xfb, 0x07
302 };
303 
304 static uint32_t urtw_8225_channel[] = {
305 	0x0000,		/* dummy channel 0  */
306 	0x085c,		/* 1  */
307 	0x08dc,		/* 2  */
308 	0x095c,		/* 3  */
309 	0x09dc,		/* 4  */
310 	0x0a5c,		/* 5  */
311 	0x0adc,		/* 6  */
312 	0x0b5c,		/* 7  */
313 	0x0bdc,		/* 8  */
314 	0x0c5c,		/* 9  */
315 	0x0cdc,		/* 10  */
316 	0x0d5c,		/* 11  */
317 	0x0ddc,		/* 12  */
318 	0x0e5c,		/* 13  */
319 	0x0f72,		/* 14  */
320 };
321 
322 static uint8_t urtw_8225_gain[] = {
323 	0x23, 0x88, 0x7c, 0xa5,		/* -82dbm  */
324 	0x23, 0x88, 0x7c, 0xb5,		/* -82dbm  */
325 	0x23, 0x88, 0x7c, 0xc5,		/* -82dbm  */
326 	0x33, 0x80, 0x79, 0xc5,		/* -78dbm  */
327 	0x43, 0x78, 0x76, 0xc5,		/* -74dbm  */
328 	0x53, 0x60, 0x73, 0xc5,		/* -70dbm  */
329 	0x63, 0x58, 0x70, 0xc5,		/* -66dbm  */
330 };
331 
332 static struct urtw_pair urtw_8225_rf_part1[] = {
333 	{ 0x00, 0x0067 }, { 0x01, 0x0fe0 }, { 0x02, 0x044d }, { 0x03, 0x0441 },
334 	{ 0x04, 0x0486 }, { 0x05, 0x0bc0 }, { 0x06, 0x0ae6 }, { 0x07, 0x082a },
335 	{ 0x08, 0x001f }, { 0x09, 0x0334 }, { 0x0a, 0x0fd4 }, { 0x0b, 0x0391 },
336 	{ 0x0c, 0x0050 }, { 0x0d, 0x06db }, { 0x0e, 0x0029 }, { 0x0f, 0x0914 },
337 };
338 
339 static struct urtw_pair urtw_8225_rf_part2[] = {
340 	{ 0x00, 0x01 }, { 0x01, 0x02 }, { 0x02, 0x42 }, { 0x03, 0x00 },
341 	{ 0x04, 0x00 }, { 0x05, 0x00 }, { 0x06, 0x40 }, { 0x07, 0x00 },
342 	{ 0x08, 0x40 }, { 0x09, 0xfe }, { 0x0a, 0x09 }, { 0x0b, 0x80 },
343 	{ 0x0c, 0x01 }, { 0x0e, 0xd3 }, { 0x0f, 0x38 }, { 0x10, 0x84 },
344 	{ 0x11, 0x06 }, { 0x12, 0x20 }, { 0x13, 0x20 }, { 0x14, 0x00 },
345 	{ 0x15, 0x40 }, { 0x16, 0x00 }, { 0x17, 0x40 }, { 0x18, 0xef },
346 	{ 0x19, 0x19 }, { 0x1a, 0x20 }, { 0x1b, 0x76 }, { 0x1c, 0x04 },
347 	{ 0x1e, 0x95 }, { 0x1f, 0x75 }, { 0x20, 0x1f }, { 0x21, 0x27 },
348 	{ 0x22, 0x16 }, { 0x24, 0x46 }, { 0x25, 0x20 }, { 0x26, 0x90 },
349 	{ 0x27, 0x88 }
350 };
351 
352 static struct urtw_pair urtw_8225_rf_part3[] = {
353 	{ 0x00, 0x98 }, { 0x03, 0x20 }, { 0x04, 0x7e }, { 0x05, 0x12 },
354 	{ 0x06, 0xfc }, { 0x07, 0x78 }, { 0x08, 0x2e }, { 0x10, 0x9b },
355 	{ 0x11, 0x88 }, { 0x12, 0x47 }, { 0x13, 0xd0 }, { 0x19, 0x00 },
356 	{ 0x1a, 0xa0 }, { 0x1b, 0x08 }, { 0x40, 0x86 }, { 0x41, 0x8d },
357 	{ 0x42, 0x15 }, { 0x43, 0x18 }, { 0x44, 0x1f }, { 0x45, 0x1e },
358 	{ 0x46, 0x1a }, { 0x47, 0x15 }, { 0x48, 0x10 }, { 0x49, 0x0a },
359 	{ 0x4a, 0x05 }, { 0x4b, 0x02 }, { 0x4c, 0x05 }
360 };
361 
362 static uint16_t urtw_8225_rxgain[] = {
363 	0x0400, 0x0401, 0x0402, 0x0403, 0x0404, 0x0405, 0x0408, 0x0409,
364 	0x040a, 0x040b, 0x0502, 0x0503, 0x0504, 0x0505, 0x0540, 0x0541,
365 	0x0542, 0x0543, 0x0544, 0x0545, 0x0580, 0x0581, 0x0582, 0x0583,
366 	0x0584, 0x0585, 0x0588, 0x0589, 0x058a, 0x058b, 0x0643, 0x0644,
367 	0x0645, 0x0680, 0x0681, 0x0682, 0x0683, 0x0684, 0x0685, 0x0688,
368 	0x0689, 0x068a, 0x068b, 0x068c, 0x0742, 0x0743, 0x0744, 0x0745,
369 	0x0780, 0x0781, 0x0782, 0x0783, 0x0784, 0x0785, 0x0788, 0x0789,
370 	0x078a, 0x078b, 0x078c, 0x078d, 0x0790, 0x0791, 0x0792, 0x0793,
371 	0x0794, 0x0795, 0x0798, 0x0799, 0x079a, 0x079b, 0x079c, 0x079d,
372 	0x07a0, 0x07a1, 0x07a2, 0x07a3, 0x07a4, 0x07a5, 0x07a8, 0x07a9,
373 	0x07aa, 0x07ab, 0x07ac, 0x07ad, 0x07b0, 0x07b1, 0x07b2, 0x07b3,
374 	0x07b4, 0x07b5, 0x07b8, 0x07b9, 0x07ba, 0x07bb, 0x07bb
375 };
376 
377 static uint8_t urtw_8225_threshold[] = {
378 	0x8d, 0x8d, 0x8d, 0x8d, 0x9d, 0xad, 0xbd,
379 };
380 
381 static uint8_t urtw_8225_tx_gain_cck_ofdm[] = {
382 	0x02, 0x06, 0x0e, 0x1e, 0x3e, 0x7e
383 };
384 
385 static uint8_t urtw_8225_txpwr_cck[] = {
386 	0x18, 0x17, 0x15, 0x11, 0x0c, 0x08, 0x04, 0x02,
387 	0x1b, 0x1a, 0x17, 0x13, 0x0e, 0x09, 0x04, 0x02,
388 	0x1f, 0x1e, 0x1a, 0x15, 0x10, 0x0a, 0x05, 0x02,
389 	0x22, 0x21, 0x1d, 0x18, 0x11, 0x0b, 0x06, 0x02,
390 	0x26, 0x25, 0x21, 0x1b, 0x14, 0x0d, 0x06, 0x03,
391 	0x2b, 0x2a, 0x25, 0x1e, 0x16, 0x0e, 0x07, 0x03
392 };
393 
394 static uint8_t urtw_8225_txpwr_cck_ch14[] = {
395 	0x18, 0x17, 0x15, 0x0c, 0x00, 0x00, 0x00, 0x00,
396 	0x1b, 0x1a, 0x17, 0x0e, 0x00, 0x00, 0x00, 0x00,
397 	0x1f, 0x1e, 0x1a, 0x0f, 0x00, 0x00, 0x00, 0x00,
398 	0x22, 0x21, 0x1d, 0x11, 0x00, 0x00, 0x00, 0x00,
399 	0x26, 0x25, 0x21, 0x13, 0x00, 0x00, 0x00, 0x00,
400 	0x2b, 0x2a, 0x25, 0x15, 0x00, 0x00, 0x00, 0x00
401 };
402 
403 static uint8_t urtw_8225_txpwr_ofdm[] = {
404 	0x80, 0x90, 0xa2, 0xb5, 0xcb, 0xe4
405 };
406 
407 static uint8_t urtw_8225v2_gain_bg[] = {
408 	0x23, 0x15, 0xa5,		/* -82-1dbm  */
409 	0x23, 0x15, 0xb5,		/* -82-2dbm  */
410 	0x23, 0x15, 0xc5,		/* -82-3dbm  */
411 	0x33, 0x15, 0xc5,		/* -78dbm  */
412 	0x43, 0x15, 0xc5,		/* -74dbm  */
413 	0x53, 0x15, 0xc5,		/* -70dbm  */
414 	0x63, 0x15, 0xc5,		/* -66dbm  */
415 };
416 
417 static struct urtw_pair urtw_8225v2_rf_part1[] = {
418 	{ 0x00, 0x02bf }, { 0x01, 0x0ee0 }, { 0x02, 0x044d }, { 0x03, 0x0441 },
419 	{ 0x04, 0x08c3 }, { 0x05, 0x0c72 }, { 0x06, 0x00e6 }, { 0x07, 0x082a },
420 	{ 0x08, 0x003f }, { 0x09, 0x0335 }, { 0x0a, 0x09d4 }, { 0x0b, 0x07bb },
421 	{ 0x0c, 0x0850 }, { 0x0d, 0x0cdf }, { 0x0e, 0x002b }, { 0x0f, 0x0114 }
422 };
423 
424 static struct urtw_pair urtw_8225v2_rf_part2[] = {
425 	{ 0x00, 0x01 }, { 0x01, 0x02 }, { 0x02, 0x42 }, { 0x03, 0x00 },
426 	{ 0x04, 0x00 },	{ 0x05, 0x00 }, { 0x06, 0x40 }, { 0x07, 0x00 },
427 	{ 0x08, 0x40 }, { 0x09, 0xfe }, { 0x0a, 0x08 }, { 0x0b, 0x80 },
428 	{ 0x0c, 0x01 }, { 0x0d, 0x43 }, { 0x0e, 0xd3 }, { 0x0f, 0x38 },
429 	{ 0x10, 0x84 }, { 0x11, 0x07 }, { 0x12, 0x20 }, { 0x13, 0x20 },
430 	{ 0x14, 0x00 }, { 0x15, 0x40 }, { 0x16, 0x00 }, { 0x17, 0x40 },
431 	{ 0x18, 0xef }, { 0x19, 0x19 }, { 0x1a, 0x20 }, { 0x1b, 0x15 },
432 	{ 0x1c, 0x04 }, { 0x1d, 0xc5 }, { 0x1e, 0x95 }, { 0x1f, 0x75 },
433 	{ 0x20, 0x1f }, { 0x21, 0x17 }, { 0x22, 0x16 }, { 0x23, 0x80 },
434 	{ 0x24, 0x46 }, { 0x25, 0x00 }, { 0x26, 0x90 }, { 0x27, 0x88 }
435 };
436 
437 static struct urtw_pair urtw_8225v2_rf_part3[] = {
438 	{ 0x00, 0x98 }, { 0x03, 0x20 }, { 0x04, 0x7e }, { 0x05, 0x12 },
439 	{ 0x06, 0xfc }, { 0x07, 0x78 }, { 0x08, 0x2e }, { 0x09, 0x11 },
440 	{ 0x0a, 0x17 }, { 0x0b, 0x11 }, { 0x10, 0x9b }, { 0x11, 0x88 },
441 	{ 0x12, 0x47 }, { 0x13, 0xd0 }, { 0x19, 0x00 }, { 0x1a, 0xa0 },
442 	{ 0x1b, 0x08 }, { 0x1d, 0x00 }, { 0x40, 0x86 }, { 0x41, 0x9d },
443 	{ 0x42, 0x15 }, { 0x43, 0x18 }, { 0x44, 0x36 }, { 0x45, 0x35 },
444 	{ 0x46, 0x2e }, { 0x47, 0x25 }, { 0x48, 0x1c }, { 0x49, 0x12 },
445 	{ 0x4a, 0x09 }, { 0x4b, 0x04 }, { 0x4c, 0x05 }
446 };
447 
448 static uint16_t urtw_8225v2_rxgain[] = {
449 	0x0400, 0x0401, 0x0402, 0x0403, 0x0404, 0x0405, 0x0408, 0x0409,
450 	0x040a, 0x040b, 0x0502, 0x0503, 0x0504, 0x0505, 0x0540, 0x0541,
451 	0x0542, 0x0543, 0x0544, 0x0545, 0x0580, 0x0581, 0x0582, 0x0583,
452 	0x0584, 0x0585, 0x0588, 0x0589, 0x058a, 0x058b, 0x0643, 0x0644,
453 	0x0645, 0x0680, 0x0681, 0x0682, 0x0683, 0x0684, 0x0685, 0x0688,
454 	0x0689, 0x068a, 0x068b, 0x068c, 0x0742, 0x0743, 0x0744, 0x0745,
455 	0x0780, 0x0781, 0x0782, 0x0783, 0x0784, 0x0785, 0x0788, 0x0789,
456 	0x078a, 0x078b, 0x078c, 0x078d, 0x0790, 0x0791, 0x0792, 0x0793,
457 	0x0794, 0x0795, 0x0798, 0x0799, 0x079a, 0x079b, 0x079c, 0x079d,
458 	0x07a0, 0x07a1, 0x07a2, 0x07a3, 0x07a4, 0x07a5, 0x07a8, 0x07a9,
459 	0x03aa, 0x03ab, 0x03ac, 0x03ad, 0x03b0, 0x03b1, 0x03b2, 0x03b3,
460 	0x03b4, 0x03b5, 0x03b8, 0x03b9, 0x03ba, 0x03bb, 0x03bb
461 };
462 
463 static uint8_t urtw_8225v2_tx_gain_cck_ofdm[] = {
464 	0x00, 0x01, 0x02, 0x03, 0x04, 0x05,
465 	0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
466 	0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11,
467 	0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
468 	0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d,
469 	0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23,
470 };
471 
472 static uint8_t urtw_8225v2_txpwr_cck[] = {
473 	0x36, 0x35, 0x2e, 0x25, 0x1c, 0x12, 0x09, 0x04,
474 	0x30, 0x2f, 0x29, 0x21, 0x19, 0x10, 0x08, 0x03,
475 	0x2b, 0x2a, 0x25, 0x1e, 0x16, 0x0e, 0x07, 0x03,
476 	0x26, 0x25, 0x21, 0x1b, 0x14, 0x0d, 0x06, 0x03
477 };
478 
479 static uint8_t urtw_8225v2_txpwr_cck_ch14[] = {
480 	0x36, 0x35, 0x2e, 0x1b, 0x00, 0x00, 0x00, 0x00,
481 	0x30, 0x2f, 0x29, 0x15, 0x00, 0x00, 0x00, 0x00,
482 	0x30, 0x2f, 0x29, 0x15, 0x00, 0x00, 0x00, 0x00,
483 	0x30, 0x2f, 0x29, 0x15, 0x00, 0x00, 0x00, 0x00
484 };
485 
486 static struct urtw_pair urtw_8225v2_b_rf[] = {
487 	{ 0x00, 0x00b7 }, { 0x01, 0x0ee0 }, { 0x02, 0x044d }, { 0x03, 0x0441 },
488 	{ 0x04, 0x08c3 }, { 0x05, 0x0c72 }, { 0x06, 0x00e6 }, { 0x07, 0x082a },
489 	{ 0x08, 0x003f }, { 0x09, 0x0335 }, { 0x0a, 0x09d4 }, { 0x0b, 0x07bb },
490 	{ 0x0c, 0x0850 }, { 0x0d, 0x0cdf }, { 0x0e, 0x002b }, { 0x0f, 0x0114 },
491 	{ 0x00, 0x01b7 }
492 };
493 
494 static struct urtw_pair urtw_ratetable[] = {
495 	{  2,  0 }, {   4,  1 }, { 11, 2 }, { 12, 4 }, { 18, 5 },
496 	{ 22,  3 }, {  24,  6 }, { 36, 7 }, { 48, 8 }, { 72, 9 },
497 	{ 96, 10 }, { 108, 11 }
498 };
499 
500 static int		urtw_8187_init(void *);
501 static void		urtw_stop(struct urtw_softc *);
502 static int		urtw_set_channel(struct urtw_softc *);
503 static void
504 urtw_rxeof(usb_pipe_handle_t, usb_bulk_req_t *);
505 static int
506 urtw_newstate(struct ieee80211com *, enum ieee80211_state, int);
507 static usbd_status
508 urtw_read8_c(struct urtw_softc *, int, uint8_t *, uint8_t);
509 static usbd_status
510 urtw_read16_c(struct urtw_softc *, int, uint16_t *, uint8_t);
511 static usbd_status
512 urtw_read32_c(struct urtw_softc *, int, uint32_t *, uint8_t);
513 static usbd_status
514 urtw_write8_c(struct urtw_softc *, int, uint8_t, uint8_t);
515 static usbd_status
516 urtw_write16_c(struct urtw_softc *, int, uint16_t, uint8_t);
517 static usbd_status
518 urtw_write32_c(struct urtw_softc *, int, uint32_t, uint8_t);
519 static usbd_status	urtw_eprom_cs(struct urtw_softc *, int);
520 static usbd_status	urtw_eprom_ck(struct urtw_softc *);
521 static usbd_status	urtw_eprom_sendbits(struct urtw_softc *, int16_t *,
522 			    int);
523 static usbd_status	urtw_eprom_read32(struct urtw_softc *, uint32_t,
524 			    uint32_t *);
525 static usbd_status	urtw_eprom_readbit(struct urtw_softc *, int16_t *);
526 static usbd_status	urtw_eprom_writebit(struct urtw_softc *, int16_t);
527 static usbd_status	urtw_get_macaddr(struct urtw_softc *);
528 static usbd_status	urtw_get_txpwr(struct urtw_softc *);
529 static usbd_status	urtw_get_rfchip(struct urtw_softc *);
530 static usbd_status	urtw_led_init(struct urtw_softc *);
531 static usbd_status
532 urtw_8225_read(struct urtw_softc *, uint8_t, uint32_t *);
533 static usbd_status	urtw_8225_rf_init(struct urtw_rf *);
534 static usbd_status	urtw_8225_rf_set_chan(struct urtw_rf *, int);
535 static usbd_status	urtw_8225_rf_set_sens(struct urtw_rf *);
536 static usbd_status	urtw_8225v2_rf_init(struct urtw_rf *);
537 static usbd_status	urtw_8225v2_rf_set_chan(struct urtw_rf *, int);
538 static usbd_status	urtw_open_pipes(struct urtw_softc *);
539 static void urtw_close_pipes(struct urtw_softc *);
540 static void urtw_led_launch(void *);
541 
542 static void	urtw_8187b_update_wmm(struct urtw_softc *);
543 static usbd_status	urtw_8187b_reset(struct urtw_softc *);
544 static int	urtw_8187b_init(void *);
545 static void	urtw_8225v2_b_config_mac(struct urtw_softc *);
546 static void	urtw_8225v2_b_init_rfe(struct urtw_softc *);
547 static usbd_status	urtw_8225v2_b_update_chan(struct urtw_softc *);
548 static usbd_status	urtw_8225v2_b_rf_init(struct urtw_rf *);
549 static usbd_status	urtw_8225v2_b_rf_set_chan(struct urtw_rf *, int);
550 static void urtw_8225v2_b_set_txpwrlvl(struct urtw_softc *, int);
551 
552 #ifdef DEBUG
553 
554 #define	URTW_DEBUG_XMIT		0x00000001
555 #define	URTW_DEBUG_RECV		0x00000002
556 #define	URTW_DEBUG_LED 		0x00000004
557 #define	URTW_DEBUG_GLD 		0x00000008
558 #define	URTW_DEBUG_RF		0x00000010
559 #define	URTW_DEBUG_ATTACH 	0x00000020
560 #define	URTW_DEBUG_ACTIVE 	0x00000040
561 #define	URTW_DEBUG_HWTYPE	0x00000080
562 #define	URTW_DEBUG_DEVREQ	0x00000100
563 #define	URTW_DEBUG_HOTPLUG	0x00000200
564 #define	URTW_DEBUG_STATE	0x00000400
565 #define	URTW_DEBUG_TX_PROC	0x00000800
566 #define	URTW_DEBUG_RX_PROC 	0x00001000
567 #define	URTW_DEBUG_EEPROM	0x00002000
568 #define	URTW_DEBUG_RESET	0x00004000
569 #define	URTW_DEBUG_ANY		0xffffffff
570 
571 uint32_t urtw8187_dbg_flags = 0;
572 static void
urtw8187_dbg(dev_info_t * dip,int level,const char * fmt,...)573 urtw8187_dbg(dev_info_t *dip, int level, const char *fmt, ...)
574 {
575 	char		msg_buffer[255];
576 	va_list	ap;
577 
578 	if (dip == NULL) {
579 		return;
580 	}
581 
582 	va_start(ap, fmt);
583 	(void) vsprintf(msg_buffer, fmt, ap);
584 	cmn_err(level, "%s%d: %s", ddi_get_name(dip),
585 	    ddi_get_instance(dip), msg_buffer);
586 	va_end(ap);
587 }
588 
589 #define	URTW8187_DBG(l, x) do {\
590 	_NOTE(CONSTANTCONDITION) \
591 	if ((l) & urtw8187_dbg_flags) \
592 		urtw8187_dbg x;\
593 	_NOTE(CONSTANTCONDITION) \
594 } while (0)
595 #else
596 #define	URTW8187_DBG(l, x)
597 #endif
598 
599 static usbd_status
urtw_led_init(struct urtw_softc * sc)600 urtw_led_init(struct urtw_softc *sc)
601 {
602 	uint32_t rev;
603 	usbd_status error;
604 
605 	if (error = urtw_read8_c(sc, URTW_PSR, &sc->sc_psr, 0))
606 		goto fail;
607 	error = urtw_eprom_read32(sc, URTW_EPROM_SWREV, &rev);
608 	if (error != 0)
609 		goto fail;
610 
611 	switch (rev & URTW_EPROM_CID_MASK) {
612 	case URTW_EPROM_CID_ALPHA0:
613 		sc->sc_strategy = URTW_SW_LED_MODE1;
614 		break;
615 	case URTW_EPROM_CID_SERCOMM_PS:
616 		sc->sc_strategy = URTW_SW_LED_MODE3;
617 		break;
618 	case URTW_EPROM_CID_HW_LED:
619 		sc->sc_strategy = URTW_HW_LED;
620 		break;
621 	case URTW_EPROM_CID_RSVD0:
622 	case URTW_EPROM_CID_RSVD1:
623 	default:
624 		sc->sc_strategy = URTW_SW_LED_MODE0;
625 		break;
626 	}
627 
628 	sc->sc_gpio_ledpin = URTW_LED_PIN_GPIO0;
629 
630 fail:
631 	return (error);
632 }
633 
634 static usbd_status
urtw_8225_write_s16(struct urtw_softc * sc,uint8_t addr,int index,uint16_t * data)635 urtw_8225_write_s16(struct urtw_softc *sc, uint8_t addr, int index,
636     uint16_t *data)
637 {
638 	usb_ctrl_setup_t req;
639 	usb_cr_t cr;
640 	usb_cb_flags_t cf;
641 	mblk_t *mp = 0;
642 	uint16_t data16;
643 	usbd_status error;
644 
645 	data16 = *data;
646 	bzero(&req, sizeof (req));
647 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
648 	req.bRequest = URTW_8187_SETREGS_REQ;
649 	req.wValue = addr;
650 	req.wIndex = (uint16_t)index;
651 	req.wLength = sizeof (uint16_t);
652 	req.attrs = USB_ATTRS_NONE;
653 
654 	mp = allocb(sizeof (uint16_t), BPRI_MED);
655 	if (mp == 0) {
656 		cmn_err(CE_WARN, "urtw_8225_write_s16: allocb failed\n");
657 		return (-1);
658 	}
659 	*(mp->b_rptr) = (data16 & 0x00ff);
660 	*(mp->b_rptr + 1) = (data16 & 0xff00) >> 8;
661 	mp->b_wptr += sizeof (uint16_t);
662 	error = usb_pipe_ctrl_xfer_wait(sc->sc_udev->dev_default_ph, &req, &mp,
663 	    &cr, &cf, 0);
664 	if (error != USB_SUCCESS) {
665 		URTW8187_DBG(URTW_DEBUG_DEVREQ, (sc->sc_dev, CE_CONT,
666 		    "urtw_8225_write_s16: could not set regs:"
667 		    "cr:%s(%d), cf:(%x)\n", usb_str_cr(cr), cr, cf));
668 	}
669 	if (mp)
670 		freemsg(mp);
671 	return (error);
672 
673 }
674 
675 static usbd_status
urtw_8225_read(struct urtw_softc * sc,uint8_t addr,uint32_t * data)676 urtw_8225_read(struct urtw_softc *sc, uint8_t addr, uint32_t *data)
677 {
678 	int i;
679 	int16_t bit;
680 	uint8_t rlen = 12, wlen = 6;
681 	uint16_t o1, o2, o3, tmp;
682 	uint32_t d2w = ((uint32_t)(addr & 0x1f)) << 27;
683 	uint32_t mask = 0x80000000, value = 0;
684 	usbd_status error;
685 
686 	if (error = urtw_read16_c(sc, URTW_RF_PINS_OUTPUT, &o1, 0))
687 		goto fail;
688 	if (error = urtw_read16_c(sc, URTW_RF_PINS_ENABLE, &o2, 0))
689 		goto fail;
690 	if (error = urtw_read16_c(sc, URTW_RF_PINS_SELECT, &o3, 0))
691 		goto fail;
692 	if (error = urtw_write16_c(sc, URTW_RF_PINS_ENABLE, o2 | 0xf, 0))
693 		goto fail;
694 	if (error = urtw_write16_c(sc, URTW_RF_PINS_SELECT, o3 | 0xf, 0))
695 		goto fail;
696 	o1 &= ~0xf;
697 	if (error = urtw_write16_c(sc, URTW_RF_PINS_OUTPUT,
698 	    o1 | URTW_BB_HOST_BANG_EN, 0))
699 		goto fail;
700 	DELAY(5);
701 	if (error = urtw_write16_c(sc, URTW_RF_PINS_OUTPUT, o1, 0))
702 		goto fail;
703 	DELAY(5);
704 
705 	for (i = 0; i < (wlen / 2); i++, mask = mask >> 1) {
706 		bit = ((d2w & mask) != 0) ? 1 : 0;
707 
708 		if (error = urtw_write16_c(sc, URTW_RF_PINS_OUTPUT,
709 		    bit | o1, 0))
710 			goto fail;
711 		DELAY(2);
712 		if (error = urtw_write16_c(sc, URTW_RF_PINS_OUTPUT,
713 		    bit | o1 | URTW_BB_HOST_BANG_CLK, 0))
714 			goto fail;
715 		DELAY(2);
716 		if (error = urtw_write16_c(sc, URTW_RF_PINS_OUTPUT,
717 		    bit | o1 | URTW_BB_HOST_BANG_CLK, 0))
718 			goto fail;
719 		DELAY(2);
720 		mask = mask >> 1;
721 		if (i == 2)
722 			break;
723 		bit = ((d2w & mask) != 0) ? 1 : 0;
724 		if (error = urtw_write16_c(sc, URTW_RF_PINS_OUTPUT,
725 		    bit | o1 | URTW_BB_HOST_BANG_CLK, 0))
726 			goto fail;
727 		DELAY(2);
728 		if (error = urtw_write16_c(sc, URTW_RF_PINS_OUTPUT,
729 		    bit | o1 | URTW_BB_HOST_BANG_CLK, 0))
730 			goto fail;
731 		DELAY(2);
732 		if (error = urtw_write16_c(sc, URTW_RF_PINS_OUTPUT,
733 		    bit | o1, 0))
734 			goto fail;
735 		DELAY(1);
736 	}
737 	if (error = urtw_write16_c(sc, URTW_RF_PINS_OUTPUT,
738 	    bit | o1 | URTW_BB_HOST_BANG_RW | URTW_BB_HOST_BANG_CLK, 0))
739 		goto fail;
740 	DELAY(2);
741 	if (error = urtw_write16_c(sc, URTW_RF_PINS_OUTPUT,
742 	    bit | o1 | URTW_BB_HOST_BANG_RW, 0))
743 		goto fail;
744 	DELAY(2);
745 	if (error = urtw_write16_c(sc, URTW_RF_PINS_OUTPUT,
746 	    o1 | URTW_BB_HOST_BANG_RW, 0))
747 		goto fail;
748 	DELAY(2);
749 
750 	mask = 0x800;
751 	for (i = 0; i < rlen; i++, mask = mask >> 1) {
752 		if (error = urtw_write16_c(sc, URTW_RF_PINS_OUTPUT,
753 		    o1 | URTW_BB_HOST_BANG_RW, 0))
754 			goto fail;
755 		DELAY(2);
756 		if (error = urtw_write16_c(sc, URTW_RF_PINS_OUTPUT,
757 		    o1 | URTW_BB_HOST_BANG_RW | URTW_BB_HOST_BANG_CLK, 0))
758 			goto fail;
759 		DELAY(2);
760 		if (error = urtw_write16_c(sc, URTW_RF_PINS_OUTPUT,
761 		    o1 | URTW_BB_HOST_BANG_RW | URTW_BB_HOST_BANG_CLK, 0))
762 			goto fail;
763 		DELAY(2);
764 		if (error = urtw_write16_c(sc, URTW_RF_PINS_OUTPUT,
765 		    o1 | URTW_BB_HOST_BANG_RW | URTW_BB_HOST_BANG_CLK, 0))
766 			goto fail;
767 		DELAY(2);
768 
769 		if (error = urtw_read16_c(sc, URTW_RF_PINS_INPUT, &tmp, 0))
770 			goto fail;
771 		value |= ((tmp & URTW_BB_HOST_BANG_CLK) ? mask : 0);
772 		if (error = urtw_write16_c(sc, URTW_RF_PINS_OUTPUT,
773 		    o1 | URTW_BB_HOST_BANG_RW, 0))
774 			goto fail;
775 		DELAY(2);
776 	}
777 
778 	if (error = urtw_write16_c(sc, URTW_RF_PINS_OUTPUT,
779 	    o1 | URTW_BB_HOST_BANG_EN |
780 	    URTW_BB_HOST_BANG_RW, 0))
781 		goto fail;
782 	DELAY(2);
783 
784 	if (error = urtw_write16_c(sc, URTW_RF_PINS_ENABLE, o2, 0))
785 		goto fail;
786 	if (error = urtw_write16_c(sc, URTW_RF_PINS_SELECT, o3, 0))
787 		goto fail;
788 	error = urtw_write16_c(sc, URTW_RF_PINS_OUTPUT, 0x3a0, 0);
789 
790 	if (data != NULL)
791 		*data = value;
792 fail:
793 	return (error);
794 }
795 
796 static void
urtw_delay_ms(int t)797 urtw_delay_ms(int t)
798 {
799 	DELAY(t * 1000);
800 }
801 
802 static usbd_status
urtw_8225_write_c(struct urtw_softc * sc,uint8_t addr,uint16_t data)803 urtw_8225_write_c(struct urtw_softc *sc, uint8_t addr, uint16_t data)
804 {
805 	uint16_t d80, d82, d84;
806 	usbd_status error;
807 
808 	if (error = urtw_read16_c(sc, URTW_RF_PINS_OUTPUT, &d80, 0))
809 		goto fail;
810 	d80 &= 0xfff3;
811 	if (error = urtw_read16_c(sc, URTW_RF_PINS_ENABLE, &d82, 0))
812 		goto fail;
813 	if (error = urtw_read16_c(sc, URTW_RF_PINS_SELECT, &d84, 0))
814 		goto fail;
815 	d84 &= 0xfff0;
816 	if (error = urtw_write16_c(sc, URTW_RF_PINS_ENABLE,
817 	    d82 | 0x0007, 0))
818 		goto fail;
819 	if (error = urtw_write16_c(sc, URTW_RF_PINS_SELECT,
820 	    d84 | 0x0007, 0))
821 		goto fail;
822 
823 	if (error = urtw_write16_c(sc, URTW_RF_PINS_OUTPUT,
824 	    d80 | URTW_BB_HOST_BANG_EN, 0))
825 		goto fail;
826 	urtw_delay_ms(2);
827 	if (error = urtw_write16_c(sc, URTW_RF_PINS_OUTPUT, d80, 0))
828 		goto fail;
829 
830 	error = urtw_8225_write_s16(sc, addr, 0x8225, &data);
831 	if (error != 0)
832 		goto fail;
833 
834 	if (error = urtw_write16_c(sc, URTW_RF_PINS_OUTPUT,
835 	    d80 | URTW_BB_HOST_BANG_EN, 0))
836 		goto fail;
837 	if (error = urtw_write16_c(sc, URTW_RF_PINS_OUTPUT,
838 	    d80 | URTW_BB_HOST_BANG_EN, 0))
839 		goto fail;
840 	error = urtw_write16_c(sc, URTW_RF_PINS_SELECT, d84, 0);
841 	urtw_delay_ms(2);
842 fail:
843 	return (error);
844 }
845 
846 static usbd_status
urtw_8225_isv2(struct urtw_softc * sc,int * ret)847 urtw_8225_isv2(struct urtw_softc *sc, int *ret)
848 {
849 	uint32_t data;
850 	usbd_status error;
851 
852 	*ret = 1;
853 
854 	if (error = urtw_write16_c(sc, URTW_RF_PINS_OUTPUT, 0x0080, 0))
855 		goto fail;
856 	if (error = urtw_write16_c(sc, URTW_RF_PINS_SELECT, 0x0080, 0))
857 		goto fail;
858 	if (error = urtw_write16_c(sc, URTW_RF_PINS_ENABLE, 0x0080, 0))
859 		goto fail;
860 	urtw_delay_ms(300);
861 
862 	if (error = urtw_8225_write_c(sc, 0x0, 0x1b7))
863 		goto fail;
864 
865 	error = urtw_8225_read(sc, 0x8, &data);
866 	if (error != 0)
867 		goto fail;
868 	if (data != 0x588)
869 		*ret = 0;
870 	else {
871 		error = urtw_8225_read(sc, 0x9, &data);
872 		if (error != 0)
873 			goto fail;
874 		if (data != 0x700)
875 			*ret = 0;
876 	}
877 
878 	error = urtw_8225_write_c(sc, 0x0, 0xb7);
879 fail:
880 	return (error);
881 }
882 
883 static usbd_status
urtw_get_rfchip(struct urtw_softc * sc)884 urtw_get_rfchip(struct urtw_softc *sc)
885 {
886 	struct urtw_rf *rf = &sc->sc_rf;
887 	int ret;
888 	uint32_t data;
889 	usbd_status error;
890 
891 	rf->rf_sc = sc;
892 
893 	if (sc->sc_hwrev & URTW_HWREV_8187) {
894 		error = urtw_eprom_read32(sc, URTW_EPROM_RFCHIPID, &data);
895 		if (error != 0) {
896 			cmn_err(CE_WARN, "RF ID read failed\n");
897 			return (-1);
898 		}
899 		switch (data & 0xff) {
900 		case URTW_EPROM_RFCHIPID_RTL8225U:
901 			error = urtw_8225_isv2(sc, &ret);
902 			if (error != 0) {
903 				URTW8187_DBG(URTW_DEBUG_HWTYPE,
904 				    (sc->sc_dev, CE_CONT,
905 				    "8225 version check failed\n"));
906 				goto fail;
907 			}
908 			if (ret == 0) {
909 				URTW8187_DBG(URTW_DEBUG_HWTYPE,
910 				    (sc->sc_dev, CE_CONT,
911 				    "8225 detected\n"));
912 				rf->init = urtw_8225_rf_init;
913 				rf->set_chan = urtw_8225_rf_set_chan;
914 				rf->set_sens = urtw_8225_rf_set_sens;
915 			} else {
916 				URTW8187_DBG(URTW_DEBUG_HWTYPE,
917 				    (sc->sc_dev, CE_CONT,
918 				    "8225 v2 detected\n"));
919 				rf->init = urtw_8225v2_rf_init;
920 				rf->set_chan = urtw_8225v2_rf_set_chan;
921 				rf->set_sens = NULL;
922 			}
923 			break;
924 		default:
925 			goto fail;
926 		}
927 	} else {
928 		URTW8187_DBG(URTW_DEBUG_HWTYPE,
929 		    (sc->sc_dev, CE_CONT,
930 		    "8225 v2 [b] detected\n"));
931 		rf->init = urtw_8225v2_b_rf_init;
932 		rf->set_chan = urtw_8225v2_b_rf_set_chan;
933 		rf->set_sens = NULL;
934 	}
935 
936 	rf->max_sens = URTW_8225_RF_MAX_SENS;
937 	rf->sens = URTW_8225_RF_DEF_SENS;
938 
939 	return (0);
940 
941 fail:
942 	cmn_err(CE_WARN, "unsupported RF chip %d\n", data & 0xff);
943 	return (-1);
944 }
945 
946 static usbd_status
urtw_get_txpwr(struct urtw_softc * sc)947 urtw_get_txpwr(struct urtw_softc *sc)
948 {
949 	int i, j;
950 	uint32_t data;
951 	usbd_status error;
952 
953 	error = urtw_eprom_read32(sc, URTW_EPROM_TXPW_BASE, &data);
954 	if (error != 0)
955 		goto fail;
956 	sc->sc_txpwr_cck_base = data & 0xf;
957 	sc->sc_txpwr_ofdm_base = (data >> 4) & 0xf;
958 
959 	for (i = 1, j = 0; i < 6; i += 2, j++) {
960 		error = urtw_eprom_read32(sc, URTW_EPROM_TXPW0 + j, &data);
961 		if (error != 0)
962 			goto fail;
963 		sc->sc_txpwr_cck[i] = data & 0xf;
964 		sc->sc_txpwr_cck[i + 1] = (data & 0xf00) >> 8;
965 		sc->sc_txpwr_ofdm[i] = (data & 0xf0) >> 4;
966 		sc->sc_txpwr_ofdm[i + 1] = (data & 0xf000) >> 12;
967 	}
968 	for (i = 1, j = 0; i < 4; i += 2, j++) {
969 		error = urtw_eprom_read32(sc, URTW_EPROM_TXPW1 + j, &data);
970 		if (error != 0)
971 			goto fail;
972 		sc->sc_txpwr_cck[i + 6] = data & 0xf;
973 		sc->sc_txpwr_cck[i + 6 + 1] = (data & 0xf00) >> 8;
974 		sc->sc_txpwr_ofdm[i + 6] = (data & 0xf0) >> 4;
975 		sc->sc_txpwr_ofdm[i + 6 + 1] = (data & 0xf000) >> 12;
976 	}
977 	if (sc->sc_hwrev & URTW_HWREV_8187) {
978 		for (i = 1, j = 0; i < 4; i += 2, j++) {
979 			error = urtw_eprom_read32(sc, URTW_EPROM_TXPW2 + j,
980 			    &data);
981 			if (error != 0)
982 				goto fail;
983 			sc->sc_txpwr_cck[i + 6 + 4] = data & 0xf;
984 			sc->sc_txpwr_cck[i + 6 + 4 + 1] = (data & 0xf00) >> 8;
985 			sc->sc_txpwr_ofdm[i + 6 + 4] = (data & 0xf0) >> 4;
986 			sc->sc_txpwr_ofdm[i + 6 + 4 + 1] =
987 			    (data & 0xf000) >> 12;
988 		}
989 	} else {
990 		/* Channel 11. */
991 		error = urtw_eprom_read32(sc, 0x1b, &data);
992 		if (error != 0)
993 			goto fail;
994 		sc->sc_txpwr_cck[11] = data & 0xf;
995 		sc->sc_txpwr_ofdm[11] = (data & 0xf0) >> 4;
996 
997 		/* Channel 12. */
998 		error = urtw_eprom_read32(sc, 0xa, &data);
999 		if (error != 0)
1000 			goto fail;
1001 		sc->sc_txpwr_cck[12] = data & 0xf;
1002 		sc->sc_txpwr_ofdm[12] = (data & 0xf0) >> 4;
1003 
1004 		/* Channel 13, 14. */
1005 		error = urtw_eprom_read32(sc, 0x1c, &data);
1006 		if (error != 0)
1007 			goto fail;
1008 		sc->sc_txpwr_cck[13] = data & 0xf;
1009 		sc->sc_txpwr_ofdm[13] = (data & 0xf0) >> 4;
1010 		sc->sc_txpwr_cck[14] = (data & 0xf00) >> 8;
1011 		sc->sc_txpwr_ofdm[14] = (data & 0xf000) >> 12;
1012 	}
1013 fail:
1014 	return (error);
1015 }
1016 
1017 
1018 static usbd_status
urtw_get_macaddr(struct urtw_softc * sc)1019 urtw_get_macaddr(struct urtw_softc *sc)
1020 {
1021 	uint32_t data;
1022 	usbd_status error;
1023 	uint8_t *m = 0;
1024 
1025 	error = urtw_eprom_read32(sc, URTW_EPROM_MACADDR, &data);
1026 	if (error != 0)
1027 		goto fail;
1028 	sc->sc_bssid[0] = data & 0xff;
1029 	sc->sc_bssid[1] = (data & 0xff00) >> 8;
1030 	error = urtw_eprom_read32(sc, URTW_EPROM_MACADDR + 1, &data);
1031 	if (error != 0)
1032 		goto fail;
1033 	sc->sc_bssid[2] = data & 0xff;
1034 	sc->sc_bssid[3] = (data & 0xff00) >> 8;
1035 	error = urtw_eprom_read32(sc, URTW_EPROM_MACADDR + 2, &data);
1036 	if (error != 0)
1037 		goto fail;
1038 	sc->sc_bssid[4] = data & 0xff;
1039 	sc->sc_bssid[5] = (data & 0xff00) >> 8;
1040 	bcopy(sc->sc_bssid, sc->sc_ic.ic_macaddr, IEEE80211_ADDR_LEN);
1041 	m = sc->sc_bssid;
1042 	URTW8187_DBG(URTW_DEBUG_HWTYPE, (sc->sc_dev, CE_CONT,
1043 	    "MAC: %x:%x:%x:%x:%x:%x\n",
1044 	    m[0], m[1], m[2], m[3], m[4], m[5]));
1045 fail:
1046 	return (error);
1047 }
1048 
1049 static usbd_status
urtw_eprom_read32(struct urtw_softc * sc,uint32_t addr,uint32_t * data)1050 urtw_eprom_read32(struct urtw_softc *sc, uint32_t addr, uint32_t *data)
1051 {
1052 #define	URTW_READCMD_LEN	3
1053 	int addrlen, i;
1054 	int16_t addrstr[8], data16, readcmd[] = { 1, 1, 0 };
1055 	usbd_status error;
1056 
1057 	/* NB: make sure the buffer is initialized  */
1058 	*data = 0;
1059 
1060 	/* enable EPROM programming */
1061 	if (error = urtw_write8_c(sc, URTW_EPROM_CMD,
1062 	    URTW_EPROM_CMD_PROGRAM_MODE, 0))
1063 		goto fail;
1064 	DELAY(URTW_EPROM_DELAY);
1065 
1066 	error = urtw_eprom_cs(sc, URTW_EPROM_ENABLE);
1067 	if (error != 0)
1068 		goto fail;
1069 	error = urtw_eprom_ck(sc);
1070 	if (error != 0)
1071 		goto fail;
1072 	error = urtw_eprom_sendbits(sc, readcmd, URTW_READCMD_LEN);
1073 	if (error != 0)
1074 		goto fail;
1075 	if (sc->sc_epromtype == URTW_EEPROM_93C56) {
1076 		addrlen = 8;
1077 		addrstr[0] = addr & (1 << 7);
1078 		addrstr[1] = addr & (1 << 6);
1079 		addrstr[2] = addr & (1 << 5);
1080 		addrstr[3] = addr & (1 << 4);
1081 		addrstr[4] = addr & (1 << 3);
1082 		addrstr[5] = addr & (1 << 2);
1083 		addrstr[6] = addr & (1 << 1);
1084 		addrstr[7] = addr & (1 << 0);
1085 	} else {
1086 		addrlen = 6;
1087 		addrstr[0] = addr & (1 << 5);
1088 		addrstr[1] = addr & (1 << 4);
1089 		addrstr[2] = addr & (1 << 3);
1090 		addrstr[3] = addr & (1 << 2);
1091 		addrstr[4] = addr & (1 << 1);
1092 		addrstr[5] = addr & (1 << 0);
1093 	}
1094 	error = urtw_eprom_sendbits(sc, addrstr, addrlen);
1095 	if (error != 0)
1096 		goto fail;
1097 
1098 	error = urtw_eprom_writebit(sc, 0);
1099 	if (error != 0)
1100 		goto fail;
1101 
1102 	for (i = 0; i < 16; i++) {
1103 		error = urtw_eprom_ck(sc);
1104 		if (error != 0)
1105 			goto fail;
1106 		error = urtw_eprom_readbit(sc, &data16);
1107 		if (error != 0)
1108 			goto fail;
1109 
1110 		(*data) |= (data16 << (15 - i));
1111 	}
1112 
1113 	error = urtw_eprom_cs(sc, URTW_EPROM_DISABLE);
1114 	if (error != 0)
1115 		goto fail;
1116 	error = urtw_eprom_ck(sc);
1117 	if (error != 0)
1118 		goto fail;
1119 
1120 	/* now disable EPROM programming */
1121 	error = urtw_write8_c(sc, URTW_EPROM_CMD,
1122 	    URTW_EPROM_CMD_NORMAL_MODE, 0);
1123 fail:
1124 	return (error);
1125 #undef URTW_READCMD_LEN
1126 }
1127 
1128 static usbd_status
urtw_eprom_readbit(struct urtw_softc * sc,int16_t * data)1129 urtw_eprom_readbit(struct urtw_softc *sc, int16_t *data)
1130 {
1131 	uint8_t data8;
1132 	usbd_status error;
1133 
1134 	error = urtw_read8_c(sc, URTW_EPROM_CMD, &data8, 0);
1135 	*data = (data8 & URTW_EPROM_READBIT) ? 1 : 0;
1136 	DELAY(URTW_EPROM_DELAY);
1137 	return (error);
1138 }
1139 
1140 static usbd_status
urtw_eprom_sendbits(struct urtw_softc * sc,int16_t * buf,int buflen)1141 urtw_eprom_sendbits(struct urtw_softc *sc, int16_t *buf, int buflen)
1142 {
1143 	int i = 0;
1144 	usbd_status error;
1145 
1146 	for (i = 0; i < buflen; i++) {
1147 		error = urtw_eprom_writebit(sc, buf[i]);
1148 		if (error != 0)
1149 			goto fail;
1150 		error = urtw_eprom_ck(sc);
1151 		if (error != 0)
1152 			goto fail;
1153 	}
1154 fail:
1155 	return (error);
1156 }
1157 
1158 static usbd_status
urtw_eprom_writebit(struct urtw_softc * sc,int16_t bit)1159 urtw_eprom_writebit(struct urtw_softc *sc, int16_t bit)
1160 {
1161 	uint8_t data;
1162 	usbd_status error;
1163 
1164 	if (error = urtw_read8_c(sc, URTW_EPROM_CMD, &data, 0))
1165 		goto fail;
1166 	if (bit != 0)
1167 		error = urtw_write8_c(sc, URTW_EPROM_CMD,
1168 		    data | URTW_EPROM_WRITEBIT, 0);
1169 	else
1170 		error = urtw_write8_c(sc, URTW_EPROM_CMD,
1171 		    data & ~URTW_EPROM_WRITEBIT, 0);
1172 	DELAY(URTW_EPROM_DELAY);
1173 fail:
1174 	return (error);
1175 }
1176 
1177 static usbd_status
urtw_eprom_ck(struct urtw_softc * sc)1178 urtw_eprom_ck(struct urtw_softc *sc)
1179 {
1180 	uint8_t data;
1181 	usbd_status error;
1182 
1183 	/* masking  */
1184 	if (error = urtw_read8_c(sc, URTW_EPROM_CMD, &data, 0))
1185 		goto fail;
1186 	if (error = urtw_write8_c(sc, URTW_EPROM_CMD, data | URTW_EPROM_CK, 0))
1187 		goto fail;
1188 	DELAY(URTW_EPROM_DELAY);
1189 	/* unmasking  */
1190 	if (error = urtw_read8_c(sc, URTW_EPROM_CMD, &data, 0))
1191 		goto fail;
1192 	error = urtw_write8_c(sc, URTW_EPROM_CMD, data & ~URTW_EPROM_CK, 0);
1193 	DELAY(URTW_EPROM_DELAY);
1194 fail:
1195 	return (error);
1196 }
1197 
1198 static usbd_status
urtw_eprom_cs(struct urtw_softc * sc,int able)1199 urtw_eprom_cs(struct urtw_softc *sc, int able)
1200 {
1201 	uint8_t data;
1202 	usbd_status error;
1203 
1204 	if (error = urtw_read8_c(sc, URTW_EPROM_CMD, &data, 0))
1205 		goto fail;
1206 	if (able == URTW_EPROM_ENABLE)
1207 		error = urtw_write8_c(sc, URTW_EPROM_CMD,
1208 		    data | URTW_EPROM_CS, 0);
1209 	else
1210 		error = urtw_write8_c(sc, URTW_EPROM_CMD,
1211 		    data & ~URTW_EPROM_CS, 0);
1212 	DELAY(URTW_EPROM_DELAY);
1213 fail:
1214 	return (error);
1215 }
1216 
1217 static usbd_status
urtw_read8_c(struct urtw_softc * sc,int val,uint8_t * data,uint8_t idx)1218 urtw_read8_c(struct urtw_softc *sc, int val, uint8_t *data, uint8_t idx)
1219 {
1220 	usb_ctrl_setup_t req;
1221 	usb_cr_t cr;
1222 	usb_cb_flags_t cf;
1223 	mblk_t *mp = NULL;
1224 	usbd_status error;
1225 
1226 	bzero(&req, sizeof (req));
1227 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
1228 	req.bRequest = URTW_8187_GETREGS_REQ;
1229 	req.wValue = val | 0xff00;
1230 	req.wIndex = idx & 0x03;
1231 	req.wLength = sizeof (uint8_t);
1232 
1233 	error = usb_pipe_ctrl_xfer_wait(sc->sc_udev->dev_default_ph, &req, &mp,
1234 	    &cr, &cf, 0);
1235 
1236 	if (error != USB_SUCCESS) {
1237 		URTW8187_DBG(URTW_DEBUG_DEVREQ, (sc->sc_dev, CE_CONT,
1238 		    "urtw_read8_c: get regs req failed :"
1239 		    " cr:%s(%d), cf:(%x)\n", usb_str_cr(cr), cr, cf));
1240 		return (error);
1241 	}
1242 	bcopy(mp->b_rptr, data, sizeof (uint8_t));
1243 	if (mp)
1244 		freemsg(mp);
1245 	return (error);
1246 }
1247 
1248 static usbd_status
urtw_read8e(struct urtw_softc * sc,int val,uint8_t * data)1249 urtw_read8e(struct urtw_softc *sc, int val, uint8_t *data)
1250 {
1251 	usb_ctrl_setup_t req;
1252 	usb_cr_t cr;
1253 	usb_cb_flags_t cf;
1254 	mblk_t *mp = NULL;
1255 	usbd_status error;
1256 
1257 	bzero(&req, sizeof (req));
1258 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
1259 	req.bRequest = URTW_8187_GETREGS_REQ;
1260 	req.wValue = val | 0xfe00;
1261 	req.wIndex = 0;
1262 	req.wLength = sizeof (uint8_t);
1263 	req.attrs = USB_ATTRS_AUTOCLEARING;
1264 	error = usb_pipe_ctrl_xfer_wait(sc->sc_udev->dev_default_ph, &req, &mp,
1265 	    &cr, &cf, 0);
1266 
1267 	if (error != USB_SUCCESS) {
1268 		URTW8187_DBG(URTW_DEBUG_DEVREQ, (sc->sc_dev, CE_CONT,
1269 		    "urtw_read8e: get regs req failed :"
1270 		    " cr:%s(%d), cf:(%x)\n", usb_str_cr(cr), cr, cf));
1271 		return (error);
1272 	}
1273 
1274 	if (mp) {
1275 		bcopy(mp->b_rptr, data, sizeof (uint8_t));
1276 		freemsg(mp);
1277 	}
1278 	return (error);
1279 }
1280 
1281 static usbd_status
urtw_read16_c(struct urtw_softc * sc,int val,uint16_t * data,uint8_t idx)1282 urtw_read16_c(struct urtw_softc *sc, int val, uint16_t *data, uint8_t idx)
1283 {
1284 	usb_ctrl_setup_t req;
1285 	usb_cr_t cr;
1286 	usb_cb_flags_t cf;
1287 	mblk_t *mp = NULL;
1288 	usbd_status error;
1289 
1290 	bzero(&req, sizeof (req));
1291 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
1292 	req.bRequest = URTW_8187_GETREGS_REQ;
1293 	req.wValue = val | 0xff00;
1294 	req.wIndex = idx & 0x03;
1295 	req.wLength = sizeof (uint16_t);
1296 	req.attrs = USB_ATTRS_AUTOCLEARING;
1297 	error = usb_pipe_ctrl_xfer_wait(sc->sc_udev->dev_default_ph, &req, &mp,
1298 	    &cr, &cf, 0);
1299 
1300 	if (error != USB_SUCCESS) {
1301 		URTW8187_DBG(URTW_DEBUG_DEVREQ, (sc->sc_dev, CE_CONT,
1302 		    "urtw_read16_c: get regs req failed :"
1303 		    " cr:%s(%d), cf:(%x)\n",
1304 		    usb_str_cr(cr), cr, cf));
1305 		return (error);
1306 	}
1307 	if (mp) {
1308 		bcopy(mp->b_rptr, data, sizeof (uint16_t));
1309 		freemsg(mp);
1310 	}
1311 	return (error);
1312 }
1313 
1314 static usbd_status
urtw_read32_c(struct urtw_softc * sc,int val,uint32_t * data,uint8_t idx)1315 urtw_read32_c(struct urtw_softc *sc, int val, uint32_t *data, uint8_t idx)
1316 {
1317 	usb_ctrl_setup_t req;
1318 	usb_cr_t cr;
1319 	usb_cb_flags_t cf;
1320 	mblk_t *mp = NULL;
1321 	usbd_status error;
1322 
1323 	bzero(&req, sizeof (req));
1324 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
1325 	req.bRequest = URTW_8187_GETREGS_REQ;
1326 	req.wValue = val | 0xff00;
1327 	req.wIndex = idx & 0x03;
1328 	req.wLength = sizeof (uint32_t);
1329 	req.attrs = USB_ATTRS_AUTOCLEARING;
1330 
1331 	error = usb_pipe_ctrl_xfer_wait(sc->sc_udev->dev_default_ph, &req, &mp,
1332 	    &cr, &cf, 0);
1333 
1334 	if (error != USB_SUCCESS) {
1335 		URTW8187_DBG(URTW_DEBUG_DEVREQ, (sc->sc_dev, CE_CONT,
1336 		    "urtw_read32_c: get regs req failed :"
1337 		    " cr:%s(%d), cf:(%x)\n", usb_str_cr(cr), cr, cf));
1338 		return (error);
1339 	}
1340 
1341 	if (mp) {
1342 		bcopy(mp->b_rptr, data, sizeof (uint32_t));
1343 		freemsg(mp);
1344 	}
1345 	return (error);
1346 }
1347 
1348 static usbd_status
urtw_write8_c(struct urtw_softc * sc,int val,uint8_t data,uint8_t idx)1349 urtw_write8_c(struct urtw_softc *sc, int val, uint8_t data, uint8_t idx)
1350 {
1351 	usb_ctrl_setup_t req;
1352 	usb_cr_t cr;
1353 	usb_cb_flags_t cf;
1354 	mblk_t *mp = 0;
1355 	int error;
1356 
1357 	bzero(&req, sizeof (req));
1358 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1359 	req.bRequest = URTW_8187_SETREGS_REQ;
1360 	req.wValue = val | 0xff00;
1361 	req.wIndex = idx & 0x03;
1362 	req.wLength = sizeof (uint8_t);
1363 	req.attrs = USB_ATTRS_NONE;
1364 
1365 	mp = allocb(sizeof (uint32_t), BPRI_MED);
1366 	if (mp == NULL) {
1367 		cmn_err(CE_CONT, "urtw_write8_c: failed alloc mblk.");
1368 		return (-1);
1369 	}
1370 	*(uint8_t *)(mp->b_rptr) = data;
1371 	mp->b_wptr += sizeof (uint8_t);
1372 	error = usb_pipe_ctrl_xfer_wait(sc->sc_udev->dev_default_ph, &req, &mp,
1373 	    &cr, &cf, 0);
1374 	if (error != USB_SUCCESS) {
1375 		URTW8187_DBG(URTW_DEBUG_DEVREQ, (sc->sc_dev, CE_CONT,
1376 		    "urtw_write8_c: could not set regs:"
1377 		    "cr:%s(%d), cf:(%x)\n", usb_str_cr(cr), cr, cf));
1378 	}
1379 	if (mp)
1380 		freemsg(mp);
1381 	return (error);
1382 }
1383 
1384 static usbd_status
urtw_write8e(struct urtw_softc * sc,int val,uint8_t data)1385 urtw_write8e(struct urtw_softc *sc, int val, uint8_t data)
1386 {
1387 	usb_ctrl_setup_t req;
1388 	usb_cr_t cr;
1389 	usb_cb_flags_t cf;
1390 	mblk_t *mp = 0;
1391 	int error;
1392 
1393 	bzero(&req, sizeof (req));
1394 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1395 	req.bRequest = URTW_8187_SETREGS_REQ;
1396 	req.wValue = val | 0xfe00;
1397 	req.wIndex = 0;
1398 	req.wLength = sizeof (uint8_t);
1399 	req.attrs = USB_ATTRS_NONE;
1400 
1401 	mp = allocb(sizeof (uint8_t), BPRI_MED);
1402 	if (mp == NULL) {
1403 		cmn_err(CE_CONT, "urtw_write8e: failed alloc mblk.");
1404 		return (-1);
1405 	}
1406 	*(mp->b_rptr) = data;
1407 	mp->b_wptr += sizeof (uint8_t);
1408 
1409 	error = usb_pipe_ctrl_xfer_wait(sc->sc_udev->dev_default_ph, &req, &mp,
1410 	    &cr, &cf, 0);
1411 	if (error != USB_SUCCESS) {
1412 		URTW8187_DBG(URTW_DEBUG_DEVREQ, (sc->sc_dev, CE_CONT,
1413 		    "urtw_write8e: could not set regs:"
1414 		    "cr:%s(%d), cf:(%x)\n",
1415 		    usb_str_cr(cr), cr, cf));
1416 	}
1417 	if (mp)
1418 		freemsg(mp);
1419 	return (error);
1420 }
1421 
1422 static usbd_status
urtw_write16_c(struct urtw_softc * sc,int val,uint16_t data,uint8_t idx)1423 urtw_write16_c(struct urtw_softc *sc, int val, uint16_t data, uint8_t idx)
1424 {
1425 	usb_ctrl_setup_t req;
1426 	usb_cr_t cr;
1427 	usb_cb_flags_t cf;
1428 	mblk_t *mp = 0;
1429 	int error;
1430 
1431 	bzero(&req, sizeof (req));
1432 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1433 	req.bRequest = URTW_8187_SETREGS_REQ;
1434 	req.wValue = val | 0xff00;
1435 	req.wIndex = idx & 0x03;
1436 	req.wLength = sizeof (uint16_t);
1437 	req.attrs = USB_ATTRS_NONE;
1438 
1439 	mp = allocb(sizeof (uint16_t), BPRI_MED);
1440 	if (mp == NULL) {
1441 		cmn_err(CE_CONT, "urtw_write16_c: failed alloc mblk.");
1442 		return (-1);
1443 	}
1444 	*(uint16_t *)(uintptr_t)(mp->b_rptr) = data;
1445 	mp->b_wptr += sizeof (uint16_t);
1446 	error = usb_pipe_ctrl_xfer_wait(sc->sc_udev->dev_default_ph, &req, &mp,
1447 	    &cr, &cf, 0);
1448 	if (error != USB_SUCCESS) {
1449 		URTW8187_DBG(URTW_DEBUG_DEVREQ, (sc->sc_dev, CE_CONT,
1450 		    "urtw_write16_c: could not set regs:"
1451 		    "cr:%s(%d), cf:(%x)\n",
1452 		    usb_str_cr(cr), cr, cf));
1453 	}
1454 	if (mp)
1455 		freemsg(mp);
1456 	return (error);
1457 }
1458 
1459 static usbd_status
urtw_write32_c(struct urtw_softc * sc,int val,uint32_t data,uint8_t idx)1460 urtw_write32_c(struct urtw_softc *sc, int val, uint32_t data, uint8_t idx)
1461 {
1462 	usb_ctrl_setup_t req;
1463 	usb_cr_t cr;
1464 	usb_cb_flags_t cf;
1465 	mblk_t *mp = 0;
1466 	int error;
1467 
1468 	bzero(&req, sizeof (req));
1469 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1470 	req.bRequest = URTW_8187_SETREGS_REQ;
1471 	req.wValue = val | 0xff00;
1472 	req.wIndex = idx & 0x03;
1473 	req.wLength = sizeof (uint32_t);
1474 	req.attrs = USB_ATTRS_NONE;
1475 
1476 	mp = allocb(sizeof (uint32_t), BPRI_MED);
1477 	if (mp == NULL) {
1478 		cmn_err(CE_CONT, "urtw_write32_c: failed alloc mblk.");
1479 		return (-1);
1480 	}
1481 	*(uint32_t *)(uintptr_t)(mp->b_rptr) = data;
1482 	mp->b_wptr += sizeof (uint32_t);
1483 	error = usb_pipe_ctrl_xfer_wait(sc->sc_udev->dev_default_ph, &req, &mp,
1484 	    &cr, &cf, 0);
1485 	if (error != USB_SUCCESS) {
1486 		URTW8187_DBG(URTW_DEBUG_DEVREQ, (sc->sc_dev, CE_CONT,
1487 		    "urtw_write32_c: could not set regs:"
1488 		    "cr:%s(%d), cf:(%x)\n",
1489 		    usb_str_cr(cr), cr, cf));
1490 	}
1491 
1492 	if (mp)
1493 		freemsg(mp);
1494 	return (error);
1495 }
1496 
1497 static usbd_status
urtw_set_mode(struct urtw_softc * sc,uint32_t mode)1498 urtw_set_mode(struct urtw_softc *sc, uint32_t mode)
1499 {
1500 	uint8_t data;
1501 	usbd_status error;
1502 
1503 	if (error = urtw_read8_c(sc, URTW_EPROM_CMD, &data, 0))
1504 		goto fail;
1505 	data = (data & ~URTW_EPROM_CMD_MASK) | (mode << URTW_EPROM_CMD_SHIFT);
1506 	data = data & ~(URTW_EPROM_CS | URTW_EPROM_CK);
1507 	error = urtw_write8_c(sc, URTW_EPROM_CMD, data, 0);
1508 fail:
1509 	return (error);
1510 }
1511 
1512 static usbd_status
urtw_8180_set_anaparam(struct urtw_softc * sc,uint32_t val)1513 urtw_8180_set_anaparam(struct urtw_softc *sc, uint32_t val)
1514 {
1515 	uint8_t data;
1516 	usbd_status error;
1517 
1518 	error = urtw_set_mode(sc, URTW_EPROM_CMD_CONFIG);
1519 	if (error)
1520 		goto fail;
1521 
1522 	if (error = urtw_read8_c(sc, URTW_CONFIG3, &data, 0))
1523 		goto fail;
1524 	if (error = urtw_write8_c(sc, URTW_CONFIG3,
1525 	    data | URTW_CONFIG3_ANAPARAM_WRITE, 0))
1526 		goto fail;
1527 	if (error = urtw_write32_c(sc, URTW_ANAPARAM, val, 0))
1528 		goto fail;
1529 	if (error = urtw_read8_c(sc, URTW_CONFIG3, &data, 0))
1530 		goto fail;
1531 	if (error = urtw_write8_c(sc, URTW_CONFIG3,
1532 	    data & ~URTW_CONFIG3_ANAPARAM_WRITE, 0))
1533 		goto fail;
1534 
1535 	error = urtw_set_mode(sc, URTW_EPROM_CMD_NORMAL);
1536 	if (error)
1537 		goto fail;
1538 fail:
1539 	return (error);
1540 }
1541 
1542 static usbd_status
urtw_8185_set_anaparam2(struct urtw_softc * sc,uint32_t val)1543 urtw_8185_set_anaparam2(struct urtw_softc *sc, uint32_t val)
1544 {
1545 	uint8_t data;
1546 	usbd_status error;
1547 
1548 	error = urtw_set_mode(sc, URTW_EPROM_CMD_CONFIG);
1549 	if (error)
1550 		goto fail;
1551 
1552 	if (error = urtw_read8_c(sc, URTW_CONFIG3, &data, 0))
1553 		goto fail;
1554 	if (error = urtw_write8_c(sc, URTW_CONFIG3,
1555 	    data | URTW_CONFIG3_ANAPARAM_WRITE, 0))
1556 		goto fail;
1557 	if (error = urtw_write32_c(sc, URTW_ANAPARAM2, val, 0))
1558 		goto fail;
1559 	if (error = urtw_read8_c(sc, URTW_CONFIG3, &data, 0))
1560 		goto fail;
1561 	if (error = urtw_write8_c(sc, URTW_CONFIG3,
1562 	    data & ~URTW_CONFIG3_ANAPARAM_WRITE, 0))
1563 		goto fail;
1564 
1565 	error = urtw_set_mode(sc, URTW_EPROM_CMD_NORMAL);
1566 	if (error)
1567 		goto fail;
1568 fail:
1569 	return (error);
1570 }
1571 
1572 static usbd_status
urtw_intr_disable(struct urtw_softc * sc)1573 urtw_intr_disable(struct urtw_softc *sc)
1574 {
1575 	usbd_status error;
1576 
1577 	error = urtw_write16_c(sc, URTW_INTR_MASK, 0, 0);
1578 	return (error);
1579 }
1580 
1581 static usbd_status
urtw_8187_reset(struct urtw_softc * sc)1582 urtw_8187_reset(struct urtw_softc *sc)
1583 {
1584 	uint8_t data;
1585 	usbd_status error;
1586 
1587 	error = urtw_8180_set_anaparam(sc, URTW_8187_8225_ANAPARAM_ON);
1588 	if (error)
1589 		goto fail;
1590 	error = urtw_8185_set_anaparam2(sc, URTW_8187_8225_ANAPARAM2_ON);
1591 	if (error)
1592 		goto fail;
1593 
1594 	error = urtw_intr_disable(sc);
1595 	if (error)
1596 		goto fail;
1597 	urtw_delay_ms(50);
1598 
1599 	error = urtw_write8e(sc, 0x18, 0x10);
1600 	if (error != 0)
1601 		goto fail;
1602 	error = urtw_write8e(sc, 0x18, 0x11);
1603 	if (error != 0)
1604 		goto fail;
1605 	error = urtw_write8e(sc, 0x18, 0x00);
1606 	if (error != 0)
1607 		goto fail;
1608 	urtw_delay_ms(50);
1609 
1610 	if (error = urtw_read8_c(sc, URTW_CMD, &data, 0))
1611 		goto fail;
1612 	data = (data & 2) | URTW_CMD_RST;
1613 	if (error = urtw_write8_c(sc, URTW_CMD, data, 0))
1614 		goto fail;
1615 	urtw_delay_ms(50);
1616 
1617 	if (error = urtw_read8_c(sc, URTW_CMD, &data, 0))
1618 		goto fail;
1619 	if (data & URTW_CMD_RST) {
1620 		cmn_err(CE_CONT, "urtw reset timeout\n");
1621 		goto fail;
1622 	}
1623 	error = urtw_set_mode(sc, URTW_EPROM_CMD_LOAD);
1624 	if (error)
1625 		goto fail;
1626 	urtw_delay_ms(50);
1627 
1628 	error = urtw_8180_set_anaparam(sc, URTW_8187_8225_ANAPARAM_ON);
1629 	if (error)
1630 		goto fail;
1631 	error = urtw_8185_set_anaparam2(sc, URTW_8187_8225_ANAPARAM2_ON);
1632 	if (error)
1633 		goto fail;
1634 fail:
1635 	return (error);
1636 }
1637 
1638 static usbd_status
urtw_led_on(struct urtw_softc * sc,int type)1639 urtw_led_on(struct urtw_softc *sc, int type)
1640 {
1641 	if (type == URTW_LED_GPIO) {
1642 		switch (sc->sc_gpio_ledpin) {
1643 		case URTW_LED_PIN_GPIO0:
1644 			(void) urtw_write8_c(sc, URTW_GPIO, 0x01, 0);
1645 			(void) urtw_write8_c(sc, URTW_GP_ENABLE, 0x00, 0);
1646 			break;
1647 		default:
1648 			cmn_err(CE_WARN, "unsupported LED PIN type 0x%x",
1649 			    sc->sc_gpio_ledpin);
1650 			/* never reach  */
1651 		}
1652 	} else {
1653 		cmn_err(CE_WARN, "unsupported LED type 0x%x", type);
1654 		/* never reach  */
1655 	}
1656 
1657 	sc->sc_gpio_ledon = 1;
1658 	return (0);
1659 }
1660 
1661 static usbd_status
urtw_led_off(struct urtw_softc * sc,int type)1662 urtw_led_off(struct urtw_softc *sc, int type)
1663 {
1664 	if (type == URTW_LED_GPIO) {
1665 		switch (sc->sc_gpio_ledpin) {
1666 		case URTW_LED_PIN_GPIO0:
1667 			(void) urtw_write8_c(sc, URTW_GPIO, 0x01, 0);
1668 			(void) urtw_write8_c(sc, URTW_GP_ENABLE, 0x01, 0);
1669 			break;
1670 		default:
1671 			cmn_err(CE_WARN, "unsupported LED PIN type 0x%x",
1672 			    sc->sc_gpio_ledpin);
1673 			/* never reach  */
1674 		}
1675 	} else {
1676 		cmn_err(CE_WARN, "unsupported LED type 0x%x", type);
1677 		/* never reach  */
1678 	}
1679 
1680 	sc->sc_gpio_ledon = 0;
1681 	return (0);
1682 }
1683 
1684 static usbd_status
urtw_led_mode0(struct urtw_softc * sc,int mode)1685 urtw_led_mode0(struct urtw_softc *sc, int mode)
1686 {
1687 	URTW8187_DBG(URTW_DEBUG_LED, (sc->sc_dev, CE_CONT,
1688 	    "urtw_led_mode0: mode = %d\n", mode));
1689 	switch (mode) {
1690 	case URTW_LED_CTL_POWER_ON:
1691 		sc->sc_gpio_ledstate = URTW_LED_POWER_ON_BLINK;
1692 		break;
1693 	case URTW_LED_CTL_TX:
1694 		if (sc->sc_gpio_ledinprogress == 1)
1695 			return (0);
1696 		sc->sc_gpio_ledstate = URTW_LED_BLINK_NORMAL;
1697 		sc->sc_gpio_blinktime =
1698 		    (sc->sc_ic.ic_state == IEEE80211_S_RUN ? 4:2);
1699 		break;
1700 	case URTW_LED_CTL_LINK:
1701 		sc->sc_gpio_ledstate = URTW_LED_ON;
1702 		break;
1703 	default:
1704 		cmn_err(CE_CONT, "unsupported LED mode 0x%x", mode);
1705 		/* never reach  */
1706 	}
1707 
1708 	switch (sc->sc_gpio_ledstate) {
1709 	case URTW_LED_ON:
1710 		if (sc->sc_gpio_ledinprogress != 0)
1711 			break;
1712 		(void) urtw_led_on(sc, URTW_LED_GPIO);
1713 		break;
1714 	case URTW_LED_BLINK_NORMAL:
1715 		if (sc->sc_gpio_ledinprogress != 0)
1716 			break;
1717 		sc->sc_gpio_ledinprogress = 1;
1718 		sc->sc_gpio_blinkstate = (sc->sc_gpio_ledon != 0) ?
1719 		    URTW_LED_OFF : URTW_LED_ON;
1720 		URTW_LEDLOCK(sc);
1721 		if (sc->sc_led_ch == 0) {
1722 			URTW8187_DBG(URTW_DEBUG_LED, (sc->sc_dev, CE_CONT,
1723 			    "urtw_led_mode0: restart led timer\n"));
1724 			sc->sc_led_ch = timeout(urtw_led_launch,
1725 			    (void *)sc,
1726 			    drv_usectohz((sc->sc_ic.ic_state ==
1727 			    IEEE80211_S_RUN) ?
1728 			    URTW_LED_LINKON_BLINK :
1729 			    URTW_LED_LINKOFF_BLINK));
1730 			sc->sc_gpio_ledinprogress = 0;
1731 		}
1732 		URTW_LEDUNLOCK(sc);
1733 		break;
1734 	case URTW_LED_POWER_ON_BLINK:
1735 		(void) urtw_led_on(sc, URTW_LED_GPIO);
1736 		urtw_delay_ms(100);
1737 		(void) urtw_led_off(sc, URTW_LED_GPIO);
1738 		break;
1739 	default:
1740 		URTW8187_DBG(URTW_DEBUG_LED, (sc->sc_dev, CE_CONT,
1741 		    "urtw_led_mode0: unknown LED status 0x%x",
1742 		    sc->sc_gpio_ledstate));
1743 	}
1744 	return (0);
1745 }
1746 
1747 static usbd_status
urtw_led_mode1(struct urtw_softc * sc,int mode)1748 urtw_led_mode1(struct urtw_softc *sc, int mode)
1749 {
1750 	cmn_err(CE_WARN, "urtw sc %p, mode %d not supported", (void *)sc, mode);
1751 	return (USBD_INVAL);
1752 }
1753 
1754 static usbd_status
urtw_led_mode2(struct urtw_softc * sc,int mode)1755 urtw_led_mode2(struct urtw_softc *sc, int mode)
1756 {
1757 	cmn_err(CE_WARN, "urtw sc %p, mode %d not supported", (void *)sc, mode);
1758 	return (USBD_INVAL);
1759 }
1760 
1761 static usbd_status
urtw_led_mode3(struct urtw_softc * sc,int mode)1762 urtw_led_mode3(struct urtw_softc *sc, int mode)
1763 {
1764 	cmn_err(CE_WARN, "urtw sc %p, mode %d not supported", (void *)sc, mode);
1765 	return (USBD_INVAL);
1766 }
1767 
1768 static usbd_status
urtw_led_blink(struct urtw_softc * sc)1769 urtw_led_blink(struct urtw_softc *sc)
1770 {
1771 	uint8_t ing = 0;
1772 
1773 	URTW8187_DBG(URTW_DEBUG_LED, (sc->sc_dev, CE_CONT,
1774 	    "urtw_led_blink: gpio_blinkstate %d\n",
1775 	    sc->sc_gpio_blinkstate));
1776 	if (sc->sc_gpio_blinkstate == URTW_LED_ON)
1777 		(void) urtw_led_on(sc, URTW_LED_GPIO);
1778 	else
1779 		(void) urtw_led_off(sc, URTW_LED_GPIO);
1780 	sc->sc_gpio_blinktime--;
1781 	if (sc->sc_gpio_blinktime == 0)
1782 		ing = 1;
1783 	else {
1784 		if (sc->sc_gpio_ledstate != URTW_LED_BLINK_NORMAL &&
1785 		    sc->sc_gpio_ledstate != URTW_LED_BLINK_SLOWLY &&
1786 		    sc->sc_gpio_ledstate != URTW_LED_BLINK_CM3)
1787 			ing = 1;
1788 	}
1789 	if (ing == 1) {
1790 		if (sc->sc_gpio_ledstate == URTW_LED_ON &&
1791 		    sc->sc_gpio_ledon == 0)
1792 			(void) urtw_led_on(sc, URTW_LED_GPIO);
1793 		else if (sc->sc_gpio_ledstate == URTW_LED_OFF &&
1794 		    sc->sc_gpio_ledon == 1)
1795 			(void) urtw_led_off(sc, URTW_LED_GPIO);
1796 
1797 		sc->sc_gpio_blinktime = 0;
1798 		sc->sc_gpio_ledinprogress = 0;
1799 		return (0);
1800 	}
1801 
1802 	sc->sc_gpio_blinkstate = (sc->sc_gpio_blinkstate != URTW_LED_ON) ?
1803 	    URTW_LED_ON : URTW_LED_OFF;
1804 
1805 	switch (sc->sc_gpio_ledstate) {
1806 	case URTW_LED_BLINK_NORMAL:
1807 		URTW8187_DBG(URTW_DEBUG_LED, (sc->sc_dev, CE_CONT,
1808 		    "URTW_LED_BLINK_NORMAL\n"));
1809 		return (1);
1810 	default:
1811 		URTW8187_DBG(URTW_DEBUG_LED, (sc->sc_dev, CE_CONT,
1812 		    "unknown LED status 0x%x", sc->sc_gpio_ledstate));
1813 	}
1814 	return (0);
1815 }
1816 
1817 static usbd_status
urtw_led_ctl(struct urtw_softc * sc,int mode)1818 urtw_led_ctl(struct urtw_softc *sc, int mode)
1819 {
1820 	usbd_status error = 0;
1821 
1822 	switch (sc->sc_strategy) {
1823 	case URTW_SW_LED_MODE0:
1824 		error = urtw_led_mode0(sc, mode);
1825 		break;
1826 	case URTW_SW_LED_MODE1:
1827 		error = urtw_led_mode1(sc, mode);
1828 		break;
1829 	case URTW_SW_LED_MODE2:
1830 		error = urtw_led_mode2(sc, mode);
1831 		break;
1832 	case URTW_SW_LED_MODE3:
1833 		error = urtw_led_mode3(sc, mode);
1834 		break;
1835 	default:
1836 		cmn_err(CE_CONT, "unsupported LED mode %d\n", sc->sc_strategy);
1837 		/* never reach  */
1838 		return (-1);
1839 	}
1840 
1841 	return (error);
1842 }
1843 
1844 static usbd_status
urtw_update_msr(struct urtw_softc * sc,int nstate)1845 urtw_update_msr(struct urtw_softc *sc, int nstate)
1846 {
1847 	struct ieee80211com *ic = &sc->sc_ic;
1848 	uint8_t data;
1849 	usbd_status error;
1850 
1851 	if (error = urtw_read8_c(sc, URTW_MSR, &data, 0))
1852 		goto fail;
1853 	data &= ~URTW_MSR_LINK_MASK;
1854 
1855 	/* Should always be set. */
1856 	if (sc->sc_hwrev & URTW_HWREV_8187B)
1857 		data |= URTW_MSR_LINK_ENEDCA;
1858 
1859 	if (nstate == IEEE80211_S_RUN) {
1860 		switch (ic->ic_opmode) {
1861 		case IEEE80211_M_STA:
1862 		case IEEE80211_M_MONITOR:
1863 			data |= URTW_MSR_LINK_STA;
1864 			break;
1865 		case IEEE80211_M_IBSS:
1866 			data |= URTW_MSR_LINK_ADHOC;
1867 			break;
1868 		case IEEE80211_M_HOSTAP:
1869 			data |= URTW_MSR_LINK_HOSTAP;
1870 			break;
1871 		default:
1872 			cmn_err(CE_CONT, "unsupported operation mode 0x%x\n",
1873 			    ic->ic_opmode);
1874 			return (-1);
1875 		}
1876 	} else
1877 		data |= URTW_MSR_LINK_NONE;
1878 
1879 	error = urtw_write8_c(sc, URTW_MSR, data, 0);
1880 fail:
1881 	return (error);
1882 }
1883 
1884 static uint16_t
urtw_rate2rtl(int rate)1885 urtw_rate2rtl(int rate)
1886 {
1887 #define	N(a)	(sizeof (a) / sizeof ((a)[0]))
1888 	int i;
1889 
1890 	for (i = 0; i < N(urtw_ratetable); i++) {
1891 		if (rate == urtw_ratetable[i].reg)
1892 			return (urtw_ratetable[i].val);
1893 	}
1894 	return (3);
1895 #undef N
1896 }
1897 
1898 static uint16_t
urtw_rtl2rate(int rate)1899 urtw_rtl2rate(int rate)
1900 {
1901 #define	N(a)	(sizeof (a) / sizeof ((a)[0]))
1902 	int i;
1903 
1904 	for (i = 0; i < N(urtw_ratetable); i++) {
1905 		if (rate == urtw_ratetable[i].val)
1906 			return (urtw_ratetable[i].reg);
1907 	}
1908 
1909 	return (0);
1910 #undef N
1911 }
1912 
1913 static usbd_status
urtw_set_rate(struct urtw_softc * sc)1914 urtw_set_rate(struct urtw_softc *sc)
1915 {
1916 	int i, basic_rate, min_rr_rate, max_rr_rate;
1917 	uint16_t data;
1918 	usbd_status error;
1919 
1920 	basic_rate = urtw_rate2rtl(48);
1921 	min_rr_rate = urtw_rate2rtl(12);
1922 	max_rr_rate = urtw_rate2rtl(48);
1923 	if (error = urtw_write8_c(sc, URTW_RESP_RATE,
1924 	    max_rr_rate << URTW_RESP_MAX_RATE_SHIFT |
1925 	    min_rr_rate << URTW_RESP_MIN_RATE_SHIFT, 0))
1926 		goto fail;
1927 
1928 	if (error = urtw_read16_c(sc, URTW_BRSR, &data, 0))
1929 		goto fail;
1930 	data &= ~URTW_BRSR_MBR_8185;
1931 
1932 	for (i = 0; i <= basic_rate; i++)
1933 		data |= (1 << i);
1934 
1935 	error = urtw_write16_c(sc, URTW_BRSR, data, 0);
1936 fail:
1937 	return (error);
1938 }
1939 
1940 static usbd_status
urtw_intr_enable(struct urtw_softc * sc)1941 urtw_intr_enable(struct urtw_softc *sc)
1942 {
1943 	usbd_status error;
1944 
1945 	error = urtw_write16_c(sc, URTW_INTR_MASK, 0xffff, 0);
1946 	return (error);
1947 }
1948 
1949 static usbd_status
urtw_rx_setconf(struct urtw_softc * sc)1950 urtw_rx_setconf(struct urtw_softc *sc)
1951 {
1952 	struct ieee80211com *ic = &sc->sc_ic;
1953 	uint32_t data, a, b;
1954 	usbd_status error;
1955 
1956 	if (urtw_read32_c(sc, URTW_RX, &data, 0))
1957 		goto fail;
1958 	data = data &~ URTW_RX_FILTER_MASK;
1959 	data = data | URTW_RX_FILTER_MNG | URTW_RX_FILTER_DATA;
1960 	data = data | URTW_RX_FILTER_BCAST | URTW_RX_FILTER_MCAST;
1961 
1962 	if (ic->ic_opmode == IEEE80211_M_MONITOR) {
1963 		data = data | URTW_RX_FILTER_ICVERR;
1964 		data = data | URTW_RX_FILTER_PWR;
1965 	}
1966 	if (sc->sc_crcmon == 1 && ic->ic_opmode == IEEE80211_M_MONITOR)
1967 		data = data | URTW_RX_FILTER_CRCERR;
1968 	data = data | URTW_RX_FILTER_NICMAC;
1969 	data = data | URTW_RX_CHECK_BSSID;
1970 	data = data &~ URTW_RX_FIFO_THRESHOLD_MASK;
1971 	data = data | URTW_RX_FIFO_THRESHOLD_NONE | URTW_RX_AUTORESETPHY;
1972 	data = data &~ URTW_MAX_RX_DMA_MASK;
1973 	a = URTW_MAX_RX_DMA_2048;
1974 	b = 0x80000000;
1975 	data = data | a | b;
1976 
1977 	error = urtw_write32_c(sc, URTW_RX, data, 0);
1978 fail:
1979 	return (error);
1980 }
1981 
1982 static usbd_status
urtw_rx_enable(struct urtw_softc * sc)1983 urtw_rx_enable(struct urtw_softc *sc)
1984 {
1985 	int i;
1986 	usbd_status error;
1987 	uint8_t data;
1988 
1989 	sc->rx_queued = 0;
1990 	for (i = 0; i < URTW_RX_DATA_LIST_COUNT; i++) {
1991 		if (urtw_rx_start(sc) != 0) {
1992 			return (USB_FAILURE);
1993 		}
1994 	}
1995 
1996 	error = urtw_rx_setconf(sc);
1997 	if (error != 0)
1998 		goto fail;
1999 
2000 	if (error = urtw_read8_c(sc, URTW_CMD, &data, 0))
2001 		goto fail;
2002 	error = urtw_write8_c(sc, URTW_CMD, data | URTW_CMD_RX_ENABLE, 0);
2003 fail:
2004 	return (error);
2005 }
2006 
2007 void
urtw_tx_enable(struct urtw_softc * sc)2008 urtw_tx_enable(struct urtw_softc *sc)
2009 {
2010 	uint8_t data8;
2011 	uint32_t data;
2012 
2013 	if (sc->sc_hwrev & URTW_HWREV_8187) {
2014 		(void) urtw_read8_c(sc, URTW_CW_CONF, &data8, 0);
2015 		data8 &= ~(URTW_CW_CONF_PERPACKET_CW |
2016 		    URTW_CW_CONF_PERPACKET_RETRY);
2017 		(void) urtw_write8_c(sc, URTW_CW_CONF, data8, 0);
2018 		(void) urtw_read8_c(sc, URTW_TX_AGC_CTL, &data8, 0);
2019 		data8 &= ~URTW_TX_AGC_CTL_PERPACKET_GAIN;
2020 		data8 &= ~URTW_TX_AGC_CTL_PERPACKET_ANTSEL;
2021 		data8 &= ~URTW_TX_AGC_CTL_FEEDBACK_ANT;
2022 		(void) urtw_write8_c(sc, URTW_TX_AGC_CTL, data8, 0);
2023 
2024 		(void) urtw_read32_c(sc, URTW_TX_CONF, &data, 0);
2025 		data &= ~URTW_TX_LOOPBACK_MASK;
2026 		data |= URTW_TX_LOOPBACK_NONE;
2027 		data &= ~(URTW_TX_DPRETRY_MASK | URTW_TX_RTSRETRY_MASK);
2028 		data |= sc->sc_tx_retry << URTW_TX_DPRETRY_SHIFT;
2029 		data |= sc->sc_rts_retry << URTW_TX_RTSRETRY_SHIFT;
2030 		data &= ~(URTW_TX_NOCRC | URTW_TX_MXDMA_MASK);
2031 		data |= URTW_TX_MXDMA_2048 | URTW_TX_CWMIN | URTW_TX_DISCW;
2032 		data &= ~URTW_TX_SWPLCPLEN;
2033 		data |= URTW_TX_NOICV;
2034 		(void) urtw_write32_c(sc, URTW_TX_CONF, data, 0);
2035 	} else {
2036 		data = URTW_TX_DURPROCMODE | URTW_TX_DISREQQSIZE |
2037 		    URTW_TX_MXDMA_2048 | URTW_TX_SHORTRETRY |
2038 		    URTW_TX_LONGRETRY;
2039 		(void) urtw_write32_c(sc, URTW_TX_CONF, data, 0);
2040 	}
2041 
2042 	(void) urtw_read8_c(sc, URTW_CMD, &data8, 0);
2043 	(void) urtw_write8_c(sc, URTW_CMD, data8 | URTW_CMD_TX_ENABLE, 0);
2044 }
2045 
2046 static int
urtw_8187_init(void * arg)2047 urtw_8187_init(void *arg)
2048 {
2049 	struct urtw_softc *sc = arg;
2050 	usbd_status error;
2051 	struct urtw_rf *rf = &sc->sc_rf;
2052 	int i;
2053 
2054 	urtw_stop(sc);
2055 	URTW_LOCK(sc);
2056 	error = urtw_8187_reset(sc);
2057 	if (error)
2058 		goto fail;
2059 
2060 	(void) urtw_write8_c(sc, 0x85, 0, 0);
2061 	(void) urtw_write8_c(sc, URTW_GPIO, 0, 0);
2062 
2063 	/* for led */
2064 	(void) urtw_write8_c(sc, 0x85, 4, 0);
2065 	error = urtw_led_ctl(sc, URTW_LED_CTL_POWER_ON);
2066 	if (error != 0)
2067 		goto fail;
2068 
2069 	error = urtw_set_mode(sc, URTW_EPROM_CMD_CONFIG);
2070 	if (error)
2071 		goto fail;
2072 
2073 	/* applying MAC address again.  */
2074 	for (i = 0; i < IEEE80211_ADDR_LEN; i++)
2075 		(void) urtw_write8_c(sc, URTW_MAC0 + i,
2076 		    sc->sc_ic.ic_macaddr[i], 0);
2077 	error = urtw_set_mode(sc, URTW_EPROM_CMD_NORMAL);
2078 	if (error)
2079 		goto fail;
2080 
2081 	error = urtw_update_msr(sc, IEEE80211_S_INIT);
2082 	if (error)
2083 		goto fail;
2084 
2085 	(void) urtw_write32_c(sc, URTW_INT_TIMEOUT, 0, 0);
2086 	(void) urtw_write8_c(sc, URTW_WPA_CONFIG, 0, 0);
2087 	(void) urtw_write8_c(sc, URTW_RATE_FALLBACK, 0x81, 0);
2088 	error = urtw_set_rate(sc);
2089 	if (error != 0)
2090 		goto fail;
2091 
2092 	error = rf->init(rf);
2093 	if (error != 0)
2094 		goto fail;
2095 	if (rf->set_sens != NULL)
2096 		rf->set_sens(rf);
2097 
2098 	(void) urtw_write16_c(sc, 0x5e, 1, 0);
2099 	(void) urtw_write16_c(sc, 0xfe, 0x10, 0);
2100 	(void) urtw_write8_c(sc, URTW_TALLY_SEL, 0x80, 0);
2101 	(void) urtw_write8_c(sc, 0xff, 0x60, 0);
2102 	(void) urtw_write16_c(sc, 0x5e, 0, 0);
2103 	(void) urtw_write8_c(sc, 0x85, 4, 0);
2104 
2105 	error = urtw_intr_enable(sc);
2106 	if (error != 0)
2107 		goto fail;
2108 
2109 	error = urtw_open_pipes(sc);
2110 	if (error != 0)
2111 		goto fail;
2112 	sc->sc_tx_low_queued = 0;
2113 	sc->sc_tx_normal_queued = 0;
2114 	error = urtw_rx_enable(sc);
2115 	if (error != 0)
2116 		goto fail;
2117 	urtw_tx_enable(sc);
2118 
2119 	if (error == 0) {
2120 		URTW8187_DBG(URTW_DEBUG_ACTIVE, (sc->sc_dev,
2121 		    CE_CONT, "urtw_8187_init: succesfully done\n"));
2122 		sc->sc_flags |= URTW_FLAG_RUNNING;
2123 		URTW_UNLOCK(sc);
2124 		return (error);
2125 	}
2126 
2127 fail:
2128 	URTW_UNLOCK(sc);
2129 	urtw_stop(sc);
2130 	return (EIO);
2131 }
2132 
2133 
2134 static usbd_status
urtw_8225_usb_init(struct urtw_softc * sc)2135 urtw_8225_usb_init(struct urtw_softc *sc)
2136 {
2137 	uint8_t data;
2138 	usbd_status error;
2139 
2140 	if (error = urtw_write8_c(sc, URTW_RF_PINS_SELECT + 1, 0, 0))
2141 		goto fail;
2142 	if (error = urtw_write8_c(sc, URTW_GPIO, 0, 0))
2143 		goto fail;
2144 	if (error = urtw_read8e(sc, 0x53, &data))
2145 		goto fail;
2146 	if (error = urtw_write8e(sc, 0x53, data | (1 << 7)))
2147 		goto fail;
2148 	if (error = urtw_write8_c(sc, URTW_RF_PINS_SELECT + 1, 4, 0))
2149 		goto fail;
2150 	if (error = urtw_write8_c(sc, URTW_GPIO, 0x20, 0))
2151 		goto fail;
2152 	if (error = urtw_write8_c(sc, URTW_GP_ENABLE, 0, 0))
2153 		goto fail;
2154 	if (error = urtw_write16_c(sc, URTW_RF_PINS_OUTPUT, 0x80, 0))
2155 		goto fail;
2156 	if (error = urtw_write16_c(sc, URTW_RF_PINS_SELECT, 0x80, 0))
2157 		goto fail;
2158 	error = urtw_write16_c(sc, URTW_RF_PINS_ENABLE, 0x80, 0);
2159 
2160 	urtw_delay_ms(100);
2161 fail:
2162 	return (error);
2163 }
2164 
2165 static usbd_status
urtw_8185_rf_pins_enable(struct urtw_softc * sc)2166 urtw_8185_rf_pins_enable(struct urtw_softc *sc)
2167 {
2168 	usbd_status error = 0;
2169 
2170 	error = urtw_write16_c(sc, URTW_RF_PINS_ENABLE, 0x1ff7, 0);
2171 	return (error);
2172 }
2173 
2174 static usbd_status
urtw_8187_write_phy(struct urtw_softc * sc,uint8_t addr,uint32_t data)2175 urtw_8187_write_phy(struct urtw_softc *sc, uint8_t addr, uint32_t data)
2176 {
2177 	uint32_t phyw;
2178 	usbd_status error;
2179 
2180 	phyw = ((data << 8) | (addr | 0x80));
2181 	if (error = urtw_write8_c(sc, 0x7f, ((phyw & 0xff000000) >> 24), 0))
2182 		goto fail;
2183 	if (error = urtw_write8_c(sc, 0x7e, ((phyw & 0x00ff0000) >> 16), 0))
2184 		goto fail;
2185 	if (error = urtw_write8_c(sc, 0x7d, ((phyw & 0x0000ff00) >> 8), 0))
2186 		goto fail;
2187 	error = urtw_write8_c(sc, 0x7c, (phyw & 0x000000ff), 0);
2188 	/*
2189 	 * Delay removed from 8185 to 8187.
2190 	 * usbd_delay_ms(sc->sc_udev, 1);
2191 	 */
2192 fail:
2193 	return (error);
2194 }
2195 
2196 static usbd_status
urtw_8187_write_phy_ofdm_c(struct urtw_softc * sc,uint8_t addr,uint32_t data)2197 urtw_8187_write_phy_ofdm_c(struct urtw_softc *sc, uint8_t addr, uint32_t data)
2198 {
2199 	data = data & 0xff;
2200 	return (urtw_8187_write_phy(sc, addr, data));
2201 }
2202 
2203 static usbd_status
urtw_8187_write_phy_cck_c(struct urtw_softc * sc,uint8_t addr,uint32_t data)2204 urtw_8187_write_phy_cck_c(struct urtw_softc *sc, uint8_t addr, uint32_t data)
2205 {
2206 	data = data & 0xff;
2207 	return (urtw_8187_write_phy(sc, addr, (data | 0x10000)));
2208 }
2209 
2210 static usbd_status
urtw_8225_setgain(struct urtw_softc * sc,int16_t gain)2211 urtw_8225_setgain(struct urtw_softc *sc, int16_t gain)
2212 {
2213 	usbd_status error;
2214 
2215 	if (error = urtw_8187_write_phy_ofdm_c(sc, 0x0d,
2216 	    urtw_8225_gain[gain * 4]))
2217 		goto fail;
2218 	if (error = urtw_8187_write_phy_ofdm_c(sc, 0x1b,
2219 	    urtw_8225_gain[gain * 4 + 2]))
2220 		goto fail;
2221 	if (error = urtw_8187_write_phy_ofdm_c(sc, 0x1d,
2222 	    urtw_8225_gain[gain * 4 + 3]))
2223 		goto fail;
2224 	error = urtw_8187_write_phy_ofdm_c(sc, 0x23,
2225 	    urtw_8225_gain[gain * 4 + 1]);
2226 fail:
2227 	return (error);
2228 }
2229 
2230 static usbd_status
urtw_8225_set_txpwrlvl(struct urtw_softc * sc,int chan)2231 urtw_8225_set_txpwrlvl(struct urtw_softc *sc, int chan)
2232 {
2233 	int i, idx, set;
2234 	uint8_t *cck_pwltable;
2235 	uint8_t cck_pwrlvl_max, ofdm_pwrlvl_min, ofdm_pwrlvl_max;
2236 	uint8_t cck_pwrlvl = sc->sc_txpwr_cck[chan] & 0xff;
2237 	uint8_t ofdm_pwrlvl = sc->sc_txpwr_ofdm[chan] & 0xff;
2238 	usbd_status error;
2239 
2240 	cck_pwrlvl_max = 11;
2241 	ofdm_pwrlvl_max = 25;	/* 12 -> 25  */
2242 	ofdm_pwrlvl_min = 10;
2243 
2244 	/* CCK power setting */
2245 	cck_pwrlvl = (cck_pwrlvl > cck_pwrlvl_max) ?
2246 	    cck_pwrlvl_max : cck_pwrlvl;
2247 	idx = cck_pwrlvl % 6;
2248 	set = cck_pwrlvl / 6;
2249 	cck_pwltable = (chan == 14) ? urtw_8225_txpwr_cck_ch14 :
2250 	    urtw_8225_txpwr_cck;
2251 
2252 	if (error = urtw_write8_c(sc, URTW_TX_GAIN_CCK,
2253 	    urtw_8225_tx_gain_cck_ofdm[set] >> 1, 0))
2254 		goto fail;
2255 	for (i = 0; i < 8; i++) {
2256 		if (error = urtw_8187_write_phy_cck_c(sc, 0x44 + i,
2257 		    cck_pwltable[idx * 8 + i]))
2258 			goto fail;
2259 	}
2260 	urtw_delay_ms(1);
2261 	/* OFDM power setting */
2262 	ofdm_pwrlvl = (ofdm_pwrlvl > (ofdm_pwrlvl_max - ofdm_pwrlvl_min)) ?
2263 	    ofdm_pwrlvl_max : ofdm_pwrlvl + ofdm_pwrlvl_min;
2264 	ofdm_pwrlvl = (ofdm_pwrlvl > 35) ? 35 : ofdm_pwrlvl;
2265 	idx = ofdm_pwrlvl % 6;
2266 	set = ofdm_pwrlvl / 6;
2267 
2268 	error = urtw_8185_set_anaparam2(sc, URTW_8187_8225_ANAPARAM2_ON);
2269 	if (error)
2270 		goto fail;
2271 	if (error = urtw_8187_write_phy_ofdm_c(sc, 2, 0x42))
2272 		goto fail;
2273 	if (error = urtw_8187_write_phy_ofdm_c(sc, 6, 0))
2274 		goto fail;
2275 	if (error = urtw_8187_write_phy_ofdm_c(sc, 8, 0))
2276 		goto fail;
2277 
2278 	if (error = urtw_write8_c(sc, URTW_TX_GAIN_OFDM,
2279 	    urtw_8225_tx_gain_cck_ofdm[set] >> 1, 0))
2280 		goto fail;
2281 	if (error = urtw_8187_write_phy_ofdm_c(sc, 0x5,
2282 	    urtw_8225_txpwr_ofdm[idx]))
2283 		goto fail;
2284 	error = urtw_8187_write_phy_ofdm_c(sc, 0x7,
2285 	    urtw_8225_txpwr_ofdm[idx]);
2286 	urtw_delay_ms(1);
2287 fail:
2288 	return (error);
2289 }
2290 
2291 static usbd_status
urtw_8185_tx_antenna(struct urtw_softc * sc,uint8_t ant)2292 urtw_8185_tx_antenna(struct urtw_softc *sc, uint8_t ant)
2293 {
2294 	usbd_status error;
2295 
2296 	error = urtw_write8_c(sc, URTW_TX_ANTENNA, ant, 0);
2297 	urtw_delay_ms(1);
2298 	return (error);
2299 }
2300 
2301 static usbd_status
urtw_8225_rf_init(struct urtw_rf * rf)2302 urtw_8225_rf_init(struct urtw_rf *rf)
2303 {
2304 #define	N(a)	(sizeof (a) / sizeof ((a)[0]))
2305 	int i;
2306 	uint16_t data;
2307 	usbd_status error;
2308 	struct urtw_softc *sc = rf->rf_sc;
2309 
2310 	error = urtw_8180_set_anaparam(sc, URTW_8187_8225_ANAPARAM_ON);
2311 	if (error)
2312 		goto fail;
2313 
2314 	if (error = urtw_8225_usb_init(sc))
2315 		goto fail;
2316 	if (error = urtw_write32_c(sc, URTW_RF_TIMING, 0x000a8008, 0))
2317 		goto fail;
2318 	if (error = urtw_read16_c(sc, URTW_BRSR, &data, 0))
2319 		goto fail;
2320 	if (error = urtw_write16_c(sc, URTW_BRSR, 0xffff, 0))
2321 		goto fail;
2322 	if (error = urtw_write32_c(sc, URTW_RF_PARA, 0x100044, 0))
2323 		goto fail;
2324 
2325 	if (error = urtw_set_mode(sc, URTW_EPROM_CMD_CONFIG))
2326 		goto fail;
2327 	if (error = urtw_write8_c(sc, URTW_CONFIG3, 0x44, 0))
2328 		goto fail;
2329 	if (error = urtw_set_mode(sc, URTW_EPROM_CMD_NORMAL))
2330 		goto fail;
2331 	if (error = urtw_8185_rf_pins_enable(sc))
2332 		goto fail;
2333 	urtw_delay_ms(100);
2334 
2335 	for (i = 0; i < N(urtw_8225_rf_part1); i++) {
2336 		if (error = urtw_8225_write_c(sc, urtw_8225_rf_part1[i].reg,
2337 		    urtw_8225_rf_part1[i].val))
2338 			goto fail;
2339 		urtw_delay_ms(1);
2340 	}
2341 	urtw_delay_ms(50);
2342 	if (error = urtw_8225_write_c(sc, 0x2, 0xc4d))
2343 		goto fail;
2344 	urtw_delay_ms(50);
2345 	if (error = urtw_8225_write_c(sc, 0x2, 0x44d))
2346 		goto fail;
2347 	urtw_delay_ms(50);
2348 	if (error = urtw_8225_write_c(sc, 0x0, 0x127))
2349 		goto fail;
2350 
2351 	for (i = 0; i < 95; i++) {
2352 		if (error = urtw_8225_write_c(sc, 0x1, (uint8_t)(i + 1)))
2353 			goto fail;
2354 		if (error = urtw_8225_write_c(sc, 0x2, urtw_8225_rxgain[i]))
2355 			goto fail;
2356 	}
2357 
2358 	if (error = urtw_8225_write_c(sc, 0x0, 0x27))
2359 		goto fail;
2360 	if (error = urtw_8225_write_c(sc, 0x0, 0x22f))
2361 		goto fail;
2362 
2363 	for (i = 0; i < 128; i++) {
2364 		if (error = urtw_8187_write_phy_ofdm_c(sc, 0xb,
2365 		    urtw_8225_agc[i]))
2366 			goto fail;
2367 		urtw_delay_ms(1);
2368 		if (error = urtw_8187_write_phy_ofdm_c(sc, 0xa,
2369 		    (uint8_t)i + 0x80))
2370 			goto fail;
2371 		urtw_delay_ms(1);
2372 	}
2373 
2374 	for (i = 0; i < N(urtw_8225_rf_part2); i++) {
2375 		if (error = urtw_8187_write_phy_ofdm_c(sc,
2376 		    urtw_8225_rf_part2[i].reg,
2377 		    urtw_8225_rf_part2[i].val))
2378 			goto fail;
2379 		urtw_delay_ms(1);
2380 	}
2381 	error = urtw_8225_setgain(sc, 4);
2382 	if (error)
2383 		goto fail;
2384 
2385 	for (i = 0; i < N(urtw_8225_rf_part3); i++) {
2386 		if (error = urtw_8187_write_phy_cck_c(sc,
2387 		    urtw_8225_rf_part3[i].reg,
2388 		    urtw_8225_rf_part3[i].val))
2389 			goto fail;
2390 		urtw_delay_ms(1);
2391 	}
2392 
2393 	if (error = urtw_write8_c(sc, 0x5b, 0x0d, 0))
2394 		goto fail;
2395 	if (error = urtw_8225_set_txpwrlvl(sc, 1))
2396 		goto fail;
2397 	if (error = urtw_8187_write_phy_cck_c(sc, 0x10, 0x9b))
2398 		goto fail;
2399 	urtw_delay_ms(1);
2400 	if (error = urtw_8187_write_phy_ofdm_c(sc, 0x26, 0x90))
2401 		goto fail;
2402 	urtw_delay_ms(1);
2403 
2404 	/* TX ant A, 0x0 for B */
2405 	if (error = urtw_8185_tx_antenna(sc, 0x3))
2406 		goto fail;
2407 	if (error = urtw_write32_c(sc, 0x94, 0x3dc00002, 0))
2408 		goto fail;
2409 
2410 	error = urtw_8225_rf_set_chan(rf,
2411 	    ieee80211_chan2ieee(&sc->sc_ic, sc->sc_ic.ic_curchan));
2412 fail:
2413 	return (error);
2414 #undef N
2415 }
2416 
2417 static usbd_status
urtw_8225_rf_set_chan(struct urtw_rf * rf,int chan)2418 urtw_8225_rf_set_chan(struct urtw_rf *rf, int chan)
2419 {
2420 #define	IEEE80211_CHAN_G	\
2421 	(IEEE80211_CHAN_2GHZ | IEEE80211_CHAN_DYN)
2422 #define	IEEE80211_IS_CHAN_G(_c)		\
2423 	(((_c)->ich_flags & IEEE80211_CHAN_G) == IEEE80211_CHAN_G)
2424 
2425 	struct urtw_softc *sc = rf->rf_sc;
2426 	struct ieee80211com *ic = &sc->sc_ic;
2427 	struct ieee80211_channel *c = ic->ic_curchan;
2428 	short gset = (IEEE80211_IS_CHAN_G(c)) ? 1 : 0;
2429 	usbd_status error;
2430 
2431 	if (error = urtw_8225_set_txpwrlvl(sc, chan))
2432 		goto fail;
2433 	if (urtw_8225_write_c(sc, 0x7, urtw_8225_channel[chan]))
2434 		goto fail;
2435 	urtw_delay_ms(10);
2436 
2437 	if (error = urtw_write8_c(sc, URTW_SIFS, 0x22, 0))
2438 		goto fail;
2439 
2440 	if (ic->ic_state == IEEE80211_S_ASSOC &&
2441 	    ic->ic_flags & IEEE80211_F_SHSLOT)
2442 		if (error = urtw_write8_c(sc, URTW_SLOT, 0x9, 0))
2443 			goto fail;
2444 	else
2445 		if (error = urtw_write8_c(sc, URTW_SLOT, 0x14, 0))
2446 			goto fail;
2447 	if (gset) {
2448 		/* for G */
2449 		if (error = urtw_write8_c(sc, URTW_DIFS, 0x14, 0))
2450 			goto fail;
2451 		if (error = urtw_write8_c(sc, URTW_EIFS, 0x5b - 0x14, 0))
2452 			goto fail;
2453 		error = urtw_write8_c(sc, URTW_CW_VAL, 0x73, 0);
2454 	} else {
2455 		/* for B */
2456 		if (error = urtw_write8_c(sc, URTW_DIFS, 0x24, 0))
2457 			goto fail;
2458 		if (error = urtw_write8_c(sc, URTW_EIFS, 0x5b - 0x24, 0))
2459 			goto fail;
2460 		error = urtw_write8_c(sc, URTW_CW_VAL, 0xa5, 0);
2461 	}
2462 
2463 fail:
2464 	return (error);
2465 }
2466 
2467 static usbd_status
urtw_8225_rf_set_sens(struct urtw_rf * rf)2468 urtw_8225_rf_set_sens(struct urtw_rf *rf)
2469 {
2470 	usbd_status error;
2471 	struct urtw_softc *sc = rf->rf_sc;
2472 
2473 	if (rf->sens < 0 || rf->sens > 6)
2474 		return (-1);
2475 
2476 	if (rf->sens > 4)
2477 		if (error = urtw_8225_write_c(sc, 0x0c, 0x850))
2478 			goto fail;
2479 	else
2480 		if (error = urtw_8225_write_c(sc, 0x0c, 0x50))
2481 			goto fail;
2482 
2483 	rf->sens = 6 - rf->sens;
2484 	if (error = urtw_8225_setgain(sc, rf->sens))
2485 		goto fail;
2486 	error = urtw_8187_write_phy_cck_c(sc, 0x41,
2487 	    urtw_8225_threshold[rf->sens]);
2488 fail:
2489 	return (error);
2490 }
2491 
2492 static void
urtw_stop(struct urtw_softc * sc)2493 urtw_stop(struct urtw_softc *sc)
2494 {
2495 	URTW_LOCK(sc);
2496 	sc->sc_flags &= ~URTW_FLAG_RUNNING;
2497 	URTW_UNLOCK(sc);
2498 	urtw_close_pipes(sc);
2499 }
2500 
2501 static int
urtw_isbmode(uint16_t rate)2502 urtw_isbmode(uint16_t rate)
2503 {
2504 
2505 	rate = urtw_rtl2rate(rate);
2506 
2507 	return ((rate <= 22 && rate != 12 && rate != 18)?(1) : (0));
2508 }
2509 
2510 /* ARGSUSED */
2511 static void
urtw_rxeof(usb_pipe_handle_t pipe,usb_bulk_req_t * req)2512 urtw_rxeof(usb_pipe_handle_t pipe, usb_bulk_req_t *req)
2513 {
2514 	struct urtw_softc *sc = (struct urtw_softc *)req->bulk_client_private;
2515 	struct ieee80211com *ic = &sc->sc_ic;
2516 	int actlen, len,  flen,  rssi;
2517 	uint8_t *desc, rate;
2518 	struct ieee80211_frame *wh;
2519 	struct ieee80211_node *ni = 0;
2520 	mblk_t *mp = 0;
2521 	uint8_t *rxbuf;
2522 
2523 	mp = req->bulk_data;
2524 	req->bulk_data = NULL;
2525 	if (req->bulk_completion_reason != USB_CR_OK ||
2526 	    mp == NULL) {
2527 		sc->sc_rx_err++;
2528 		URTW8187_DBG(URTW_DEBUG_RX_PROC, (sc->sc_dev, CE_CONT,
2529 		    "urtw_rxeof failed! %d, mp %p\n",
2530 		    req->bulk_completion_reason, mp));
2531 		req->bulk_data = mp;
2532 		goto fail;
2533 	}
2534 
2535 	actlen = MBLKL(mp);
2536 	rxbuf = (uint8_t *)mp->b_rptr;
2537 
2538 	if (sc->sc_hwrev & URTW_HWREV_8187)
2539 		/* 4 dword and 4 byte CRC  */
2540 		len = actlen - (4 * 4);
2541 	else
2542 		/* 5 dword and 4 byte CRC */
2543 		len = actlen - (4 * 5);
2544 
2545 	desc = rxbuf + len;
2546 	flen = ((desc[1] & 0x0f) << 8) + (desc[0] & 0xff);
2547 	if (flen > actlen) {
2548 		cmn_err(CE_CONT, "urtw_rxeof: impossible: flen %d, actlen %d\n",
2549 		    flen, actlen);
2550 		sc->sc_rx_err++;
2551 		req->bulk_data = mp;
2552 		goto fail;
2553 	}
2554 
2555 	rate = (desc[2] & 0xf0) >> 4;
2556 	if (sc->sc_hwrev & URTW_HWREV_8187) {
2557 		rssi = (desc[6] & 0xfe) >> 1;
2558 
2559 		/* XXX correct? */
2560 		if (!urtw_isbmode(rate)) {
2561 			rssi = (rssi > 90) ? 90 : ((rssi < 25) ? 25 : rssi);
2562 			rssi = ((90 - rssi) * 100) / 65;
2563 		} else {
2564 			rssi = (rssi > 90) ? 95 : ((rssi < 30) ? 30 : rssi);
2565 			rssi = ((95 - rssi) * 100) / 65;
2566 		}
2567 	} else {
2568 		rssi = 14 + desc[13]/2;
2569 		if (rssi >= 95)
2570 			rssi = 95;
2571 		URTW8187_DBG(URTW_DEBUG_RX_PROC, (sc->sc_dev, CE_CONT,
2572 		    "urtw_rxeof: rssi %u\n", rssi));
2573 	}
2574 
2575 	mp->b_wptr = mp->b_rptr + flen - 4;
2576 	wh = (struct ieee80211_frame *)mp->b_rptr;
2577 	if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK)
2578 	    == IEEE80211_FC0_TYPE_DATA) {
2579 		sc->sc_currate = (rate > 0) ? rate : sc->sc_currate;
2580 		URTW8187_DBG(URTW_DEBUG_RX_PROC, (sc->sc_dev, CE_CONT,
2581 		    "urtw_rxeof: update sc_currate to %u\n",
2582 		    sc->sc_currate));
2583 	}
2584 	ni = ieee80211_find_rxnode(ic, wh);
2585 
2586 	/* send the frame to the 802.11 layer */
2587 	(void) ieee80211_input(ic, mp, ni, rssi, 0);
2588 
2589 	/* node is no longer needed */
2590 	ieee80211_free_node(ni);
2591 fail:
2592 	mutex_enter(&sc->rx_lock);
2593 	sc->rx_queued--;
2594 	mutex_exit(&sc->rx_lock);
2595 	usb_free_bulk_req(req);
2596 	if (URTW_IS_RUNNING(sc) && !URTW_IS_SUSPENDING(sc))
2597 		(void) urtw_rx_start(sc);
2598 }
2599 
2600 static usbd_status
urtw_8225v2_setgain(struct urtw_softc * sc,int16_t gain)2601 urtw_8225v2_setgain(struct urtw_softc *sc, int16_t gain)
2602 {
2603 	uint8_t *gainp;
2604 	usbd_status error;
2605 
2606 	/* XXX for A?  */
2607 	gainp = urtw_8225v2_gain_bg;
2608 	if (error = urtw_8187_write_phy_ofdm_c(sc, 0x0d, gainp[gain * 3]))
2609 		goto fail;
2610 	urtw_delay_ms(1);
2611 	if (error = urtw_8187_write_phy_ofdm_c(sc, 0x1b, gainp[gain * 3 + 1]))
2612 	urtw_delay_ms(1);
2613 	if (error = urtw_8187_write_phy_ofdm_c(sc, 0x1d, gainp[gain * 3 + 2]))
2614 		goto fail;
2615 	urtw_delay_ms(1);
2616 	if (error = urtw_8187_write_phy_ofdm_c(sc, 0x21, 0x17))
2617 		goto fail;
2618 	urtw_delay_ms(1);
2619 fail:
2620 	return (error);
2621 }
2622 
2623 static usbd_status
urtw_8225v2_set_txpwrlvl(struct urtw_softc * sc,int chan)2624 urtw_8225v2_set_txpwrlvl(struct urtw_softc *sc, int chan)
2625 {
2626 	int i;
2627 	uint8_t *cck_pwrtable;
2628 	uint8_t cck_pwrlvl_max = 15, ofdm_pwrlvl_max = 25, ofdm_pwrlvl_min = 10;
2629 	uint8_t cck_pwrlvl = sc->sc_txpwr_cck[chan] & 0xff;
2630 	uint8_t ofdm_pwrlvl = sc->sc_txpwr_ofdm[chan] & 0xff;
2631 	usbd_status error;
2632 
2633 	/* CCK power setting */
2634 	cck_pwrlvl = (cck_pwrlvl > cck_pwrlvl_max) ?
2635 	    cck_pwrlvl_max : cck_pwrlvl;
2636 	cck_pwrlvl += sc->sc_txpwr_cck_base;
2637 	cck_pwrlvl = (cck_pwrlvl > 35) ? 35 : cck_pwrlvl;
2638 	cck_pwrtable = (chan == 14) ? urtw_8225v2_txpwr_cck_ch14 :
2639 	    urtw_8225v2_txpwr_cck;
2640 
2641 	for (i = 0; i < 8; i++) {
2642 		if (error = urtw_8187_write_phy_cck_c(sc, 0x44 + i,
2643 		    cck_pwrtable[i]))
2644 			goto fail;
2645 	}
2646 	if (error = urtw_write8_c(sc, URTW_TX_GAIN_CCK,
2647 	    urtw_8225v2_tx_gain_cck_ofdm[cck_pwrlvl], 0))
2648 		goto fail;
2649 	urtw_delay_ms(1);
2650 
2651 	/* OFDM power setting */
2652 	ofdm_pwrlvl = (ofdm_pwrlvl > (ofdm_pwrlvl_max - ofdm_pwrlvl_min)) ?
2653 	    ofdm_pwrlvl_max : ofdm_pwrlvl + ofdm_pwrlvl_min;
2654 	ofdm_pwrlvl += sc->sc_txpwr_ofdm_base;
2655 	ofdm_pwrlvl = (ofdm_pwrlvl > 35) ? 35 : ofdm_pwrlvl;
2656 
2657 	error = urtw_8185_set_anaparam2(sc, URTW_8187_8225_ANAPARAM2_ON);
2658 	if (error)
2659 		goto fail;
2660 
2661 	if (error = urtw_8187_write_phy_ofdm_c(sc, 2, 0x42))
2662 		goto fail;
2663 	if (error = urtw_8187_write_phy_ofdm_c(sc, 5, 0x0))
2664 		goto fail;
2665 	if (error = urtw_8187_write_phy_ofdm_c(sc, 6, 0x40))
2666 		goto fail;
2667 	if (error = urtw_8187_write_phy_ofdm_c(sc, 7, 0x0))
2668 		goto fail;
2669 	if (error = urtw_8187_write_phy_ofdm_c(sc, 8, 0x40))
2670 		goto fail;
2671 
2672 	error = urtw_write8_c(sc, URTW_TX_GAIN_OFDM,
2673 	    urtw_8225v2_tx_gain_cck_ofdm[ofdm_pwrlvl], 0);
2674 	urtw_delay_ms(1);
2675 fail:
2676 	return (error);
2677 }
2678 
2679 static usbd_status
urtw_8225v2_rf_init(struct urtw_rf * rf)2680 urtw_8225v2_rf_init(struct urtw_rf *rf)
2681 {
2682 #define	N(a)	(sizeof (a)/ sizeof ((a)[0]))
2683 	int i;
2684 	uint16_t data;
2685 	uint32_t data32;
2686 	usbd_status error;
2687 	struct urtw_softc *sc = rf->rf_sc;
2688 
2689 	if (error = urtw_8180_set_anaparam(sc, URTW_8187_8225_ANAPARAM_ON))
2690 		goto fail;
2691 	if (error = urtw_8225_usb_init(sc))
2692 		goto fail;
2693 	if (error = urtw_write32_c(sc, URTW_RF_TIMING, 0x000a8008, 0))
2694 		goto fail;
2695 	if (error = urtw_read16_c(sc, URTW_BRSR, &data, 0))
2696 		goto fail;
2697 	if (error = urtw_write16_c(sc, URTW_BRSR, 0xffff, 0))
2698 		goto fail;
2699 	if (error = urtw_write32_c(sc, URTW_RF_PARA, 0x100044, 0))
2700 		goto fail;
2701 	if (error = urtw_set_mode(sc, URTW_EPROM_CMD_CONFIG))
2702 		goto fail;
2703 	if (error = urtw_write8_c(sc, URTW_CONFIG3, 0x44, 0))
2704 		goto fail;
2705 	if (error = urtw_set_mode(sc, URTW_EPROM_CMD_NORMAL))
2706 		goto fail;
2707 	if (error = urtw_8185_rf_pins_enable(sc))
2708 		goto fail;
2709 
2710 	urtw_delay_ms(500);
2711 
2712 	for (i = 0; i < N(urtw_8225v2_rf_part1); i++) {
2713 		if (error = urtw_8225_write_c(sc, urtw_8225v2_rf_part1[i].reg,
2714 		    urtw_8225v2_rf_part1[i].val))
2715 			goto fail;
2716 		urtw_delay_ms(1);
2717 	}
2718 	urtw_delay_ms(100);
2719 
2720 	if (error = urtw_8225_write_c(sc, 0x0, 0x1b7))
2721 		goto fail;
2722 
2723 	for (i = 0; i < 95; i++) {
2724 		if (error = urtw_8225_write_c(sc, 0x1, (uint8_t)(i + 1)))
2725 			goto fail;
2726 		urtw_delay_ms(1);
2727 		if (error = urtw_8225_write_c(sc, 0x2, urtw_8225v2_rxgain[i]))
2728 			goto fail;
2729 		urtw_delay_ms(1);
2730 	}
2731 
2732 	if (error = urtw_8225_write_c(sc, 0x3, 0x2))
2733 		goto fail;
2734 	urtw_delay_ms(1);
2735 	if (error = urtw_8225_write_c(sc, 0x5, 0x4))
2736 		goto fail;
2737 	urtw_delay_ms(1);
2738 	if (error = urtw_8225_write_c(sc, 0x0, 0xb7))
2739 		goto fail;
2740 	urtw_delay_ms(1);
2741 	if (error = urtw_8225_write_c(sc, 0x2, 0xc4d))
2742 		goto fail;
2743 	urtw_delay_ms(100);
2744 	if (error = urtw_8225_write_c(sc, 0x2, 0x44d))
2745 		goto fail;
2746 	urtw_delay_ms(100);
2747 
2748 	if (error = urtw_8225_read(sc, 0x6, &data32))
2749 		goto fail;
2750 	if (data32 != 0xe6) {
2751 		error = (-1);
2752 		cmn_err(CE_WARN, "expect 0xe6!! (0x%x)\n", data32);
2753 		goto fail;
2754 	}
2755 	if (!(data32 & 0x80)) {
2756 		if (error = urtw_8225_write_c(sc, 0x02, 0x0c4d))
2757 			goto fail;
2758 		urtw_delay_ms(200);
2759 		if (error = urtw_8225_write_c(sc, 0x02, 0x044d))
2760 			goto fail;
2761 		urtw_delay_ms(100);
2762 		if (error = urtw_8225_read(sc, 0x6, &data32))
2763 			goto fail;
2764 		if (!(data32 & 0x80))
2765 			cmn_err(CE_CONT, "RF calibration failed\n");
2766 	}
2767 	urtw_delay_ms(200);
2768 
2769 	if (error = urtw_8225_write_c(sc, 0x0, 0x2bf))
2770 		goto fail;
2771 	for (i = 0; i < 128; i++) {
2772 		if (error = urtw_8187_write_phy_ofdm_c(sc, 0xb,
2773 		    urtw_8225_agc[i]))
2774 			goto fail;
2775 		urtw_delay_ms(1);
2776 		if (error = urtw_8187_write_phy_ofdm_c(sc, 0xa,
2777 		    (uint8_t)i + 0x80))
2778 			goto fail;
2779 		urtw_delay_ms(1);
2780 	}
2781 	urtw_delay_ms(1);
2782 
2783 	for (i = 0; i < N(urtw_8225v2_rf_part2); i++) {
2784 		if (error = urtw_8187_write_phy_ofdm_c(sc,
2785 		    urtw_8225v2_rf_part2[i].reg,
2786 		    urtw_8225v2_rf_part2[i].val))
2787 			goto fail;
2788 		urtw_delay_ms(1);
2789 	}
2790 	error = urtw_8225v2_setgain(sc, 4);
2791 	if (error)
2792 		goto fail;
2793 
2794 	for (i = 0; i < N(urtw_8225v2_rf_part3); i++) {
2795 		if (error = urtw_8187_write_phy_cck_c(sc,
2796 		    urtw_8225v2_rf_part3[i].reg,
2797 		    urtw_8225v2_rf_part3[i].val))
2798 			goto fail;
2799 		urtw_delay_ms(1);
2800 	}
2801 
2802 	if (error = urtw_write8_c(sc, 0x5b, 0x0d, 0))
2803 		goto fail;
2804 	if (error = urtw_8225v2_set_txpwrlvl(sc, 1))
2805 		goto fail;
2806 	if (error = urtw_8187_write_phy_cck_c(sc, 0x10, 0x9b))
2807 		goto fail;
2808 	urtw_delay_ms(1);
2809 	if (error = urtw_8187_write_phy_ofdm_c(sc, 0x26, 0x90))
2810 		goto fail;
2811 	urtw_delay_ms(1);
2812 
2813 	/* TX ant A, 0x0 for B */
2814 	if (error = urtw_8185_tx_antenna(sc, 0x3))
2815 		goto fail;
2816 	if (error = urtw_write32_c(sc, 0x94, 0x3dc00002, 0))
2817 		goto fail;
2818 
2819 	error = urtw_8225_rf_set_chan(rf,
2820 	    ieee80211_chan2ieee(&sc->sc_ic, sc->sc_ic.ic_curchan));
2821 fail:
2822 	return (error);
2823 #undef N
2824 }
2825 
2826 static usbd_status
urtw_8225v2_rf_set_chan(struct urtw_rf * rf,int chan)2827 urtw_8225v2_rf_set_chan(struct urtw_rf *rf, int chan)
2828 {
2829 	struct urtw_softc *sc = rf->rf_sc;
2830 	struct ieee80211com *ic = &sc->sc_ic;
2831 	struct ieee80211_channel *c = ic->ic_curchan;
2832 	short gset = (IEEE80211_IS_CHAN_G(c)) ? 1 : 0;
2833 	usbd_status error;
2834 
2835 	if (error = urtw_8225v2_set_txpwrlvl(sc, chan))
2836 		goto fail;
2837 
2838 	if (error = urtw_8225_write_c(sc, 0x7, urtw_8225_channel[chan]))
2839 		goto fail;
2840 
2841 	urtw_delay_ms(10);
2842 
2843 	if (error = urtw_write8_c(sc, URTW_SIFS, 0x22, 0))
2844 		goto fail;
2845 
2846 	if (ic->ic_state == IEEE80211_S_ASSOC &&
2847 	    ic->ic_flags & IEEE80211_F_SHSLOT) {
2848 		if (error = urtw_write8_c(sc, URTW_SLOT, 0x9, 0))
2849 			goto fail;
2850 	} else
2851 		if (error = urtw_write8_c(sc, URTW_SLOT, 0x14, 0))
2852 			goto fail;
2853 	if (gset) {
2854 		/* for G */
2855 		if (error = urtw_write8_c(sc, URTW_DIFS, 0x14, 0))
2856 			goto fail;
2857 		if (error = urtw_write8_c(sc, URTW_EIFS, 0x5b - 0x14, 0))
2858 			goto fail;
2859 		if (error = urtw_write8_c(sc, URTW_CW_VAL, 0x73, 0))
2860 			goto fail;
2861 	} else {
2862 		/* for B */
2863 		if (error = urtw_write8_c(sc, URTW_DIFS, 0x24, 0))
2864 			goto fail;
2865 		if (error = urtw_write8_c(sc, URTW_EIFS, 0x5b - 0x24, 0))
2866 			goto fail;
2867 		if (error = urtw_write8_c(sc, URTW_CW_VAL, 0xa5, 0))
2868 			goto fail;
2869 	}
2870 
2871 fail:
2872 	return (error);
2873 }
2874 
2875 static int
urtw_set_channel(struct urtw_softc * sc)2876 urtw_set_channel(struct urtw_softc *sc)
2877 {
2878 	struct ieee80211com *ic = &sc->sc_ic;
2879 	struct urtw_rf *rf = &sc->sc_rf;
2880 	uint32_t data;
2881 	usbd_status error;
2882 
2883 	if (error = urtw_read32_c(sc, URTW_TX_CONF, &data, 0))
2884 		goto fail;
2885 	data &= ~URTW_TX_LOOPBACK_MASK;
2886 	if (error = urtw_write32_c(sc, URTW_TX_CONF,
2887 	    data | URTW_TX_LOOPBACK_MAC, 0))
2888 		goto fail;
2889 	error = rf->set_chan(rf, ieee80211_chan2ieee(ic, ic->ic_curchan));
2890 	if (error)
2891 		goto fail;
2892 	urtw_delay_ms(20);
2893 	error = urtw_write32_c(sc, URTW_TX_CONF,
2894 	    data | URTW_TX_LOOPBACK_NONE, 0);
2895 fail:
2896 	return (error);
2897 }
2898 
2899 /* ARGSUSED */
2900 static void
urtw_txeof_low(usb_pipe_handle_t pipe,usb_bulk_req_t * req)2901 urtw_txeof_low(usb_pipe_handle_t pipe, usb_bulk_req_t *req)
2902 {
2903 	struct urtw_softc *sc = (struct urtw_softc *)req->bulk_client_private;
2904 	struct ieee80211com *ic = &sc->sc_ic;
2905 
2906 	URTW8187_DBG(URTW_DEBUG_TX_PROC, (sc->sc_dev, CE_CONT,
2907 	    "urtw_txeof_low(): cr:%s(%d), flags:0x%x, tx_queued:%d",
2908 	    usb_str_cr(req->bulk_completion_reason),
2909 	    req->bulk_completion_reason,
2910 	    req->bulk_cb_flags,
2911 	    sc->sc_tx_low_queued));
2912 	mutex_enter(&sc->tx_lock);
2913 	if (req->bulk_completion_reason != USB_CR_OK) {
2914 		ic->ic_stats.is_tx_failed++;
2915 		goto fail;
2916 	}
2917 
2918 	if (sc->sc_need_sched) {
2919 		sc->sc_need_sched = 0;
2920 		mac_tx_update(ic->ic_mach);
2921 	}
2922 fail:
2923 	sc->sc_tx_low_queued--;
2924 	mutex_exit(&sc->tx_lock);
2925 	usb_free_bulk_req(req);
2926 }
2927 
2928 /* ARGSUSED */
2929 static void
urtw_txeof_normal(usb_pipe_handle_t pipe,usb_bulk_req_t * req)2930 urtw_txeof_normal(usb_pipe_handle_t pipe, usb_bulk_req_t *req)
2931 {
2932 	struct urtw_softc *sc = (struct urtw_softc *)req->bulk_client_private;
2933 	struct ieee80211com *ic = &sc->sc_ic;
2934 
2935 	URTW8187_DBG(URTW_DEBUG_ACTIVE, (sc->sc_dev, CE_CONT,
2936 	    "urtw_txeof_normal(): cr:%s(%d), flags:0x%x, tx_queued:%d",
2937 	    usb_str_cr(req->bulk_completion_reason),
2938 	    req->bulk_completion_reason,
2939 	    req->bulk_cb_flags,
2940 	    sc->sc_tx_normal_queued));
2941 
2942 	mutex_enter(&sc->tx_lock);
2943 	if (req->bulk_completion_reason != USB_CR_OK) {
2944 		ic->ic_stats.is_tx_failed++;
2945 		goto fail;
2946 	}
2947 
2948 	if (sc->sc_need_sched) {
2949 		sc->sc_need_sched = 0;
2950 		mac_tx_update(ic->ic_mach);
2951 	}
2952 fail:
2953 	sc->sc_tx_normal_queued--;
2954 	mutex_exit(&sc->tx_lock);
2955 	usb_free_bulk_req(req);
2956 }
2957 
2958 
2959 static int
urtw_get_rate(struct ieee80211com * ic)2960 urtw_get_rate(struct ieee80211com *ic)
2961 {
2962 	uint8_t (*rates)[IEEE80211_RATE_MAXSIZE];
2963 	int rate;
2964 
2965 	rates = &ic->ic_bss->in_rates.ir_rates;
2966 
2967 	if (ic->ic_fixed_rate != IEEE80211_FIXED_RATE_NONE)
2968 		rate =