xref: /dragonfly/sys/dev/netif/ath/ath/if_ath_btcoex.c (revision 896f2e3a)
1 /*-
2  * Copyright (c) 2013 Adrian Chadd <adrian@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer,
10  *    without modification.
11  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13  *    redistribution must be conditioned upon including a substantially
14  *    similar Disclaimer requirement for further binary redistribution.
15  *
16  * NO WARRANTY
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
20  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
22  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27  * THE POSSIBILITY OF SUCH DAMAGES.
28  *
29  * $FreeBSD$
30  */
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 
34 /*
35  * This implements some very basic bluetooth coexistence methods for
36  * the ath(4) hardware.
37  */
38 #include "opt_ath.h"
39 #include "opt_inet.h"
40 #include "opt_wlan.h"
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/sysctl.h>
45 #include <sys/kernel.h>
46 #include <sys/lock.h>
47 #include <sys/malloc.h>
48 #include <sys/mutex.h>
49 #include <sys/errno.h>
50 
51 #include <sys/bus.h>
52 
53 #include <sys/socket.h>
54 
55 #include <net/if.h>
56 #include <net/if_var.h>
57 #include <net/if_media.h>
58 #include <net/if_arp.h>
59 #include <net/ethernet.h>		/* XXX for ether_sprintf */
60 
61 #include <netproto/802_11/ieee80211_var.h>
62 
63 #include <net/bpf.h>
64 
65 #ifdef INET
66 #include <netinet/in.h>
67 #include <netinet/if_ether.h>
68 #endif
69 
70 #include <dev/netif/ath/ath/if_athvar.h>
71 #include <dev/netif/ath/ath/if_ath_btcoex.h>
72 
73 /*
74  * Initial AR9285 / (WB195) bluetooth coexistence settings,
75  * just for experimentation.
76  *
77  * Return 0 for OK; errno for error.
78  *
79  * XXX TODO: There needs to be a PCIe workaround to disable ASPM if
80  * bluetooth coexistence is enabled.
81  */
82 static int
83 ath_btcoex_cfg_wb195(struct ath_softc *sc)
84 {
85 	HAL_BT_COEX_INFO btinfo;
86 	HAL_BT_COEX_CONFIG btconfig;
87 	struct ath_hal *ah = sc->sc_ah;
88 
89 	if (! ath_hal_btcoex_supported(ah))
90 		return (EINVAL);
91 
92 	bzero(&btinfo, sizeof(btinfo));
93 	bzero(&btconfig, sizeof(btconfig));
94 
95 	device_printf(sc->sc_dev, "Enabling WB195 BTCOEX\n");
96 
97 	btinfo.bt_module = HAL_BT_MODULE_JANUS;
98 	btinfo.bt_coex_config = HAL_BT_COEX_CFG_3WIRE;
99 	/*
100 	 * These are the three GPIO pins hooked up between the AR9285 and
101 	 * the AR3011.
102 	 */
103 	btinfo.bt_gpio_bt_active = 6;
104 	btinfo.bt_gpio_bt_priority = 7;
105 	btinfo.bt_gpio_wlan_active = 5;
106 	btinfo.bt_active_polarity = 1;	/* XXX not used */
107 	btinfo.bt_single_ant = 1;	/* 1 antenna on ar9285 ? */
108 	btinfo.bt_isolation = 0;	/* in dB, not used */
109 
110 	ath_hal_btcoex_set_info(ah, &btinfo);
111 
112 	btconfig.bt_time_extend = 0;
113 	btconfig.bt_txstate_extend = 1;	/* true */
114 	btconfig.bt_txframe_extend = 1;	/* true */
115 	btconfig.bt_mode = HAL_BT_COEX_MODE_SLOTTED;
116 	btconfig.bt_quiet_collision = 1;	/* true */
117 	btconfig.bt_rxclear_polarity = 1;	/* true */
118 	btconfig.bt_priority_time = 2;
119 	btconfig.bt_first_slot_time = 5;
120 	btconfig.bt_hold_rxclear = 1;	/* true */
121 
122 	ath_hal_btcoex_set_config(ah, &btconfig);
123 
124 	/*
125 	 * Enable antenna diversity.
126 	 */
127 	ath_hal_btcoex_set_parameter(ah, HAL_BT_COEX_ANTENNA_DIVERSITY, 1);
128 
129 	return (0);
130 }
131 
132 /*
133  * Initial AR9485 / (WB225) bluetooth coexistence settings,
134  * just for experimentation.
135  *
136  * Return 0 for OK; errno for error.
137  */
138 static int
139 ath_btcoex_cfg_wb225(struct ath_softc *sc)
140 {
141 	HAL_BT_COEX_INFO btinfo;
142 	HAL_BT_COEX_CONFIG btconfig;
143 	struct ath_hal *ah = sc->sc_ah;
144 
145 	if (! ath_hal_btcoex_supported(ah))
146 		return (EINVAL);
147 
148 	bzero(&btinfo, sizeof(btinfo));
149 	bzero(&btconfig, sizeof(btconfig));
150 
151 	device_printf(sc->sc_dev, "Enabling WB225 BTCOEX\n");
152 
153 	btinfo.bt_module = HAL_BT_MODULE_JANUS;	/* XXX not used? */
154 	btinfo.bt_coex_config = HAL_BT_COEX_CFG_3WIRE;
155 	/*
156 	 * These are the three GPIO pins hooked up between the AR9485 and
157 	 * the bluetooth module.
158 	 */
159 	btinfo.bt_gpio_bt_active = 4;
160 	btinfo.bt_gpio_bt_priority = 8;
161 	btinfo.bt_gpio_wlan_active = 5;
162 
163 	btinfo.bt_active_polarity = 1;	/* XXX not used */
164 	btinfo.bt_single_ant = 1;	/* 1 antenna on ar9285 ? */
165 	btinfo.bt_isolation = 0;	/* in dB, not used */
166 
167 	ath_hal_btcoex_set_info(ah, &btinfo);
168 
169 	btconfig.bt_time_extend = 0;
170 	btconfig.bt_txstate_extend = 1;	/* true */
171 	btconfig.bt_txframe_extend = 1;	/* true */
172 	btconfig.bt_mode = HAL_BT_COEX_MODE_SLOTTED;
173 	btconfig.bt_quiet_collision = 1;	/* true */
174 	btconfig.bt_rxclear_polarity = 1;	/* true */
175 	btconfig.bt_priority_time = 2;
176 	btconfig.bt_first_slot_time = 5;
177 	btconfig.bt_hold_rxclear = 1;	/* true */
178 
179 	ath_hal_btcoex_set_config(ah, &btconfig);
180 
181 	/*
182 	 * Enable antenna diversity.
183 	 */
184 	ath_hal_btcoex_set_parameter(ah, HAL_BT_COEX_ANTENNA_DIVERSITY, 1);
185 
186 	return (0);
187 }
188 
189 /*
190  * Initial AR9462 / (WB222) bluetooth coexistence settings,
191  * just for experimentation.
192  *
193  * Return 0 for OK; errno for error.
194  */
195 static int
196 ath_btcoex_cfg_wb222(struct ath_softc *sc)
197 {
198 	HAL_BT_COEX_INFO btinfo;
199 	HAL_BT_COEX_CONFIG btconfig;
200 	struct ath_hal *ah = sc->sc_ah;
201 
202 	if (! ath_hal_btcoex_supported(ah))
203 		return (EINVAL);
204 
205 	bzero(&btinfo, sizeof(btinfo));
206 	bzero(&btconfig, sizeof(btconfig));
207 
208 	device_printf(sc->sc_dev, "Enabling WB222 BTCOEX\n");
209 
210 	btinfo.bt_module = HAL_BT_MODULE_JANUS;	/* XXX not used? */
211 	btinfo.bt_coex_config = HAL_BT_COEX_CFG_MCI;
212 
213 	/*
214 	 * MCI uses a completely different interface to speak
215 	 * to the bluetooth module - it's a command based
216 	 * thing over a serial line, rather than
217 	 * state pins to/from the bluetooth module.
218 	 *
219 	 * So, the GPIO configuration, polarity, etc
220 	 * doesn't matter on MCI devices; it's just
221 	 * completely ignored by the HAL.
222 	 */
223 	btinfo.bt_gpio_bt_active = 4;
224 	btinfo.bt_gpio_bt_priority = 8;
225 	btinfo.bt_gpio_wlan_active = 5;
226 
227 	btinfo.bt_active_polarity = 1;	/* XXX not used */
228 	btinfo.bt_single_ant = 0;	/* 2 antenna on WB222 */
229 	btinfo.bt_isolation = 0;	/* in dB, not used */
230 
231 	ath_hal_btcoex_set_info(ah, &btinfo);
232 
233 	btconfig.bt_time_extend = 0;
234 	btconfig.bt_txstate_extend = 1;	/* true */
235 	btconfig.bt_txframe_extend = 1;	/* true */
236 	btconfig.bt_mode = HAL_BT_COEX_MODE_SLOTTED;
237 	btconfig.bt_quiet_collision = 1;	/* true */
238 	btconfig.bt_rxclear_polarity = 1;	/* true */
239 	btconfig.bt_priority_time = 2;
240 	btconfig.bt_first_slot_time = 5;
241 	btconfig.bt_hold_rxclear = 1;	/* true */
242 
243 	ath_hal_btcoex_set_config(ah, &btconfig);
244 
245 	/*
246 	 * Enable antenna diversity.
247 	 */
248 	ath_hal_btcoex_set_parameter(ah, HAL_BT_COEX_ANTENNA_DIVERSITY, 1);
249 
250 	return (0);
251 }
252 
253 
254 
255 
256 #if 0
257 /*
258  * When using bluetooth coexistence, ASPM needs to be disabled
259  * otherwise the sleeping interferes with the bluetooth (USB)
260  * operation and the MAC sleep/wakeup hardware.
261  *
262  * The PCIe powersave routine also needs to not be called
263  * by the driver during suspend/resume, else things will get
264  * a little odd.  Check Linux ath9k for more details.
265  */
266 static int
267 ath_btcoex_aspm_wb195(struct ath_softc *sc)
268 {
269 
270 	/* XXX TODO: clear device ASPM L0S and L1 */
271 	/* XXX TODO: clear _parent_ ASPM L0S and L1 */
272 }
273 #endif
274 
275 /*
276  * Methods which are required
277  */
278 
279 /*
280  * Attach btcoex to the given interface
281  */
282 int
283 ath_btcoex_attach(struct ath_softc *sc)
284 {
285 	int ret;
286 	struct ath_hal *ah = sc->sc_ah;
287 	const char *profname;
288 
289 	/*
290 	 * No chipset bluetooth coexistence? Then do nothing.
291 	 */
292 	if (! ath_hal_btcoex_supported(ah))
293 		return (0);
294 
295 	/*
296 	 * Look at the hints to determine which bluetooth
297 	 * profile to configure.
298 	 */
299 	ret = resource_string_value(device_get_name(sc->sc_dev),
300 	    device_get_unit(sc->sc_dev),
301 	    "btcoex_profile",
302 	    &profname);
303 	if (ret != 0) {
304 		/* nothing to do */
305 		return (0);
306 	}
307 
308 	if (strncmp(profname, "wb195", 5) == 0) {
309 		ret = ath_btcoex_cfg_wb195(sc);
310 	} else if (strncmp(profname, "wb222", 5) == 0) {
311 		ret = ath_btcoex_cfg_wb222(sc);
312 	} else if (strncmp(profname, "wb225", 5) == 0) {
313 		ret = ath_btcoex_cfg_wb225(sc);
314 	} else {
315 		return (0);
316 	}
317 
318 	/*
319 	 * Propagate up failure from the actual attach phase.
320 	 */
321 	if (ret != 0)
322 		return (ret);
323 
324 	return (0);
325 }
326 
327 /*
328  * Detach btcoex from the given interface
329  */
330 int
331 ath_btcoex_detach(struct ath_softc *sc)
332 {
333 
334 	return (0);
335 }
336 
337 /*
338  * Configure or disable bluetooth coexistence on the given channel.
339  *
340  * For AR9285/AR9287/AR9485, we'll never see a 5GHz channel, so we just
341  * assume bluetooth coexistence is always on.
342  *
343  * For AR9462, we may see a 5GHz channel; bluetooth coexistence should
344  * not be enabled on those channels.
345  */
346 int
347 ath_btcoex_enable(struct ath_softc *sc, const struct ieee80211_channel *chan)
348 {
349 
350 	return (0);
351 }
352 
353 /*
354  * Handle ioctl requests from the diagnostic interface.
355  *
356  * The initial part of this code resembles ath_ioctl_diag();
357  * it's likely a good idea to reduce duplication between
358  * these two routines.
359  */
360 int
361 ath_btcoex_ioctl(struct ath_softc *sc, struct ath_diag *ad)
362 {
363 	unsigned int id = ad->ad_id & ATH_DIAG_ID;
364 	void *indata = NULL;
365 	void *outdata = NULL;
366 	u_int32_t insize = ad->ad_in_size;
367 	u_int32_t outsize = ad->ad_out_size;
368 	int error = 0;
369 //	int val;
370 
371 	if (ad->ad_id & ATH_DIAG_IN) {
372 		/*
373 		 * Copy in data.
374 		 */
375 		indata = kmalloc(insize, M_TEMP, M_INTWAIT);
376 		if (indata == NULL) {
377 			error = ENOMEM;
378 			goto bad;
379 		}
380 		error = copyin(ad->ad_in_data, indata, insize);
381 		if (error)
382 			goto bad;
383 	}
384 	if (ad->ad_id & ATH_DIAG_DYN) {
385 		/*
386 		 * Allocate a buffer for the results (otherwise the HAL
387 		 * returns a pointer to a buffer where we can read the
388 		 * results).  Note that we depend on the HAL leaving this
389 		 * pointer for us to use below in reclaiming the buffer;
390 		 * may want to be more defensive.
391 		 */
392 		outdata = kmalloc(outsize, M_TEMP, M_INTWAIT);
393 		if (outdata == NULL) {
394 			error = ENOMEM;
395 			goto bad;
396 		}
397 	}
398 	switch (id) {
399 		default:
400 			error = EINVAL;
401 	}
402 	if (outsize < ad->ad_out_size)
403 		ad->ad_out_size = outsize;
404 	if (outdata && copyout(outdata, ad->ad_out_data, ad->ad_out_size))
405 		error = EFAULT;
406 bad:
407 	if ((ad->ad_id & ATH_DIAG_IN) && indata != NULL)
408 		kfree(indata, M_TEMP);
409 	if ((ad->ad_id & ATH_DIAG_DYN) && outdata != NULL)
410 		kfree(outdata, M_TEMP);
411 	return (error);
412 }
413 
414