xref: /freebsd/sys/dev/ath/if_ath_btcoex.c (revision ee5cf11617a9b7f034d95c639bd4d27d1f09e848)
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 <machine/bus.h>
52 #include <machine/resource.h>
53 #include <sys/bus.h>
54 
55 #include <sys/socket.h>
56 
57 #include <net/if.h>
58 #include <net/if_var.h>
59 #include <net/if_media.h>
60 #include <net/if_arp.h>
61 #include <net/ethernet.h>		/* XXX for ether_sprintf */
62 
63 #include <net80211/ieee80211_var.h>
64 
65 #include <net/bpf.h>
66 
67 #ifdef INET
68 #include <netinet/in.h>
69 #include <netinet/if_ether.h>
70 #endif
71 
72 #include <dev/ath/if_athvar.h>
73 #include <dev/ath/if_ath_btcoex.h>
74 
75 /*
76  * Initial AR9285 / (WB195) bluetooth coexistence settings,
77  * just for experimentation.
78  *
79  * Return 0 for OK; errno for error.
80  *
81  * XXX TODO: There needs to be a PCIe workaround to disable ASPM if
82  * bluetooth coexistence is enabled.
83  */
84 static int
85 ath_btcoex_cfg_wb195(struct ath_softc *sc)
86 {
87 	HAL_BT_COEX_INFO btinfo;
88 	HAL_BT_COEX_CONFIG btconfig;
89 	struct ath_hal *ah = sc->sc_ah;
90 
91 	if (! ath_hal_btcoex_supported(ah))
92 		return (EINVAL);
93 
94 	bzero(&btinfo, sizeof(btinfo));
95 	bzero(&btconfig, sizeof(btconfig));
96 
97 	device_printf(sc->sc_dev, "Enabling WB195 BTCOEX\n");
98 
99 	btinfo.bt_module = HAL_BT_MODULE_JANUS;
100 	btinfo.bt_coex_config = HAL_BT_COEX_CFG_3WIRE;
101 	/*
102 	 * These are the three GPIO pins hooked up between the AR9285 and
103 	 * the AR3011.
104 	 */
105 	btinfo.bt_gpio_bt_active = 6;
106 	btinfo.bt_gpio_bt_priority = 7;
107 	btinfo.bt_gpio_wlan_active = 5;
108 	btinfo.bt_active_polarity = 1;	/* XXX not used */
109 	btinfo.bt_single_ant = 1;	/* 1 antenna on ar9285 ? */
110 	btinfo.bt_isolation = 0;	/* in dB, not used */
111 
112 	ath_hal_btcoex_set_info(ah, &btinfo);
113 
114 	btconfig.bt_time_extend = 0;
115 	btconfig.bt_txstate_extend = 1;	/* true */
116 	btconfig.bt_txframe_extend = 1;	/* true */
117 	btconfig.bt_mode = HAL_BT_COEX_MODE_SLOTTED;
118 	btconfig.bt_quiet_collision = 1;	/* true */
119 	btconfig.bt_rxclear_polarity = 1;	/* true */
120 	btconfig.bt_priority_time = 2;
121 	btconfig.bt_first_slot_time = 5;
122 	btconfig.bt_hold_rxclear = 1;	/* true */
123 
124 	ath_hal_btcoex_set_config(ah, &btconfig);
125 
126 	/*
127 	 * Enable antenna diversity.
128 	 */
129 	ath_hal_btcoex_set_parameter(ah, HAL_BT_COEX_ANTENNA_DIVERSITY, 1);
130 
131 	return (0);
132 }
133 
134 /*
135  * Initial AR9485 / (WB225) bluetooth coexistence settings,
136  * just for experimentation.
137  *
138  * Return 0 for OK; errno for error.
139  */
140 static int
141 ath_btcoex_cfg_wb225(struct ath_softc *sc)
142 {
143 	HAL_BT_COEX_INFO btinfo;
144 	HAL_BT_COEX_CONFIG btconfig;
145 	struct ath_hal *ah = sc->sc_ah;
146 
147 	if (! ath_hal_btcoex_supported(ah))
148 		return (EINVAL);
149 
150 	bzero(&btinfo, sizeof(btinfo));
151 	bzero(&btconfig, sizeof(btconfig));
152 
153 	device_printf(sc->sc_dev, "Enabling WB225 BTCOEX\n");
154 
155 	btinfo.bt_module = HAL_BT_MODULE_JANUS;	/* XXX not used? */
156 	btinfo.bt_coex_config = HAL_BT_COEX_CFG_3WIRE;
157 	/*
158 	 * These are the three GPIO pins hooked up between the AR9485 and
159 	 * the bluetooth module.
160 	 */
161 	btinfo.bt_gpio_bt_active = 4;
162 	btinfo.bt_gpio_bt_priority = 8;
163 	btinfo.bt_gpio_wlan_active = 5;
164 
165 	btinfo.bt_active_polarity = 1;	/* XXX not used */
166 	btinfo.bt_single_ant = 1;	/* 1 antenna on ar9285 ? */
167 	btinfo.bt_isolation = 0;	/* in dB, not used */
168 
169 	ath_hal_btcoex_set_info(ah, &btinfo);
170 
171 	btconfig.bt_time_extend = 0;
172 	btconfig.bt_txstate_extend = 1;	/* true */
173 	btconfig.bt_txframe_extend = 1;	/* true */
174 	btconfig.bt_mode = HAL_BT_COEX_MODE_SLOTTED;
175 	btconfig.bt_quiet_collision = 1;	/* true */
176 	btconfig.bt_rxclear_polarity = 1;	/* true */
177 	btconfig.bt_priority_time = 2;
178 	btconfig.bt_first_slot_time = 5;
179 	btconfig.bt_hold_rxclear = 1;	/* true */
180 
181 	ath_hal_btcoex_set_config(ah, &btconfig);
182 
183 	/*
184 	 * Enable antenna diversity.
185 	 */
186 	ath_hal_btcoex_set_parameter(ah, HAL_BT_COEX_ANTENNA_DIVERSITY, 1);
187 
188 	return (0);
189 }
190 
191 /*
192  * Initial AR9462 / (WB222) bluetooth coexistence settings,
193  * just for experimentation.
194  *
195  * Return 0 for OK; errno for error.
196  */
197 static int
198 ath_btcoex_cfg_wb222(struct ath_softc *sc)
199 {
200 	HAL_BT_COEX_INFO btinfo;
201 	HAL_BT_COEX_CONFIG btconfig;
202 	struct ath_hal *ah = sc->sc_ah;
203 
204 	if (! ath_hal_btcoex_supported(ah))
205 		return (EINVAL);
206 
207 	bzero(&btinfo, sizeof(btinfo));
208 	bzero(&btconfig, sizeof(btconfig));
209 
210 	device_printf(sc->sc_dev, "Enabling WB222 BTCOEX\n");
211 
212 	btinfo.bt_module = HAL_BT_MODULE_JANUS;	/* XXX not used? */
213 	btinfo.bt_coex_config = HAL_BT_COEX_CFG_MCI;
214 
215 	/*
216 	 * MCI uses a completely different interface to speak
217 	 * to the bluetooth module - it's a command based
218 	 * thing over a serial line, rather than
219 	 * state pins to/from the bluetooth module.
220 	 *
221 	 * So, the GPIO configuration, polarity, etc
222 	 * doesn't matter on MCI devices; it's just
223 	 * completely ignored by the HAL.
224 	 */
225 	btinfo.bt_gpio_bt_active = 4;
226 	btinfo.bt_gpio_bt_priority = 8;
227 	btinfo.bt_gpio_wlan_active = 5;
228 
229 	btinfo.bt_active_polarity = 1;	/* XXX not used */
230 	btinfo.bt_single_ant = 0;	/* 2 antenna on WB222 */
231 	btinfo.bt_isolation = 0;	/* in dB, not used */
232 
233 	ath_hal_btcoex_set_info(ah, &btinfo);
234 
235 	btconfig.bt_time_extend = 0;
236 	btconfig.bt_txstate_extend = 1;	/* true */
237 	btconfig.bt_txframe_extend = 1;	/* true */
238 	btconfig.bt_mode = HAL_BT_COEX_MODE_SLOTTED;
239 	btconfig.bt_quiet_collision = 1;	/* true */
240 	btconfig.bt_rxclear_polarity = 1;	/* true */
241 	btconfig.bt_priority_time = 2;
242 	btconfig.bt_first_slot_time = 5;
243 	btconfig.bt_hold_rxclear = 1;	/* true */
244 
245 	ath_hal_btcoex_set_config(ah, &btconfig);
246 
247 	/*
248 	 * Enable antenna diversity.
249 	 */
250 	ath_hal_btcoex_set_parameter(ah, HAL_BT_COEX_ANTENNA_DIVERSITY, 1);
251 
252 	return (0);
253 }
254 
255 /*
256  * Initial QCA9565 / (WB335B) bluetooth coexistence settings,
257  * just for experimentation.
258  *
259  * Return 0 for OK; errno for error.
260  */
261 static int
262 ath_btcoex_cfg_wb335b(struct ath_softc *sc)
263 {
264 	HAL_BT_COEX_INFO btinfo;
265 	HAL_BT_COEX_CONFIG btconfig;
266 	struct ath_hal *ah = sc->sc_ah;
267 
268 	if (! ath_hal_btcoex_supported(ah))
269 		return (EINVAL);
270 
271 	bzero(&btinfo, sizeof(btinfo));
272 	bzero(&btconfig, sizeof(btconfig));
273 
274 	device_printf(sc->sc_dev, "Enabling WB335B BTCOEX\n");
275 
276 	btinfo.bt_module = HAL_BT_MODULE_JANUS;	/* XXX not used? */
277 	btinfo.bt_coex_config = HAL_BT_COEX_CFG_MCI;
278 
279 	/*
280 	 * MCI uses a completely different interface to speak
281 	 * to the bluetooth module - it's a command based
282 	 * thing over a serial line, rather than
283 	 * state pins to/from the bluetooth module.
284 	 *
285 	 * So, the GPIO configuration, polarity, etc
286 	 * doesn't matter on MCI devices; it's just
287 	 * completely ignored by the HAL.
288 	 */
289 	btinfo.bt_gpio_bt_active = 4;
290 	btinfo.bt_gpio_bt_priority = 8;
291 	btinfo.bt_gpio_wlan_active = 5;
292 
293 	btinfo.bt_active_polarity = 1;	/* XXX not used */
294 	btinfo.bt_single_ant = 0;	/* 2 antenna on WB335 */
295 	btinfo.bt_isolation = 0;	/* in dB, not used */
296 
297 	ath_hal_btcoex_set_info(ah, &btinfo);
298 
299 	btconfig.bt_time_extend = 0;
300 	btconfig.bt_txstate_extend = 1;	/* true */
301 	btconfig.bt_txframe_extend = 1;	/* true */
302 	btconfig.bt_mode = HAL_BT_COEX_MODE_SLOTTED;
303 	btconfig.bt_quiet_collision = 1;	/* true */
304 	btconfig.bt_rxclear_polarity = 1;	/* true */
305 	btconfig.bt_priority_time = 2;
306 	btconfig.bt_first_slot_time = 5;
307 	btconfig.bt_hold_rxclear = 1;	/* true */
308 
309 	ath_hal_btcoex_set_config(ah, &btconfig);
310 
311 	/*
312 	 * Enable antenna diversity.
313 	 */
314 	ath_hal_btcoex_set_parameter(ah, HAL_BT_COEX_ANTENNA_DIVERSITY, 1);
315 
316 	return (0);
317 }
318 
319 
320 #if 0
321 /*
322  * When using bluetooth coexistence, ASPM needs to be disabled
323  * otherwise the sleeping interferes with the bluetooth (USB)
324  * operation and the MAC sleep/wakeup hardware.
325  *
326  * The PCIe powersave routine also needs to not be called
327  * by the driver during suspend/resume, else things will get
328  * a little odd.  Check Linux ath9k for more details.
329  */
330 static int
331 ath_btcoex_aspm_wb195(struct ath_softc *sc)
332 {
333 
334 	/* XXX TODO: clear device ASPM L0S and L1 */
335 	/* XXX TODO: clear _parent_ ASPM L0S and L1 */
336 }
337 #endif
338 
339 /*
340  * Methods which are required
341  */
342 
343 /*
344  * Attach btcoex to the given interface
345  */
346 int
347 ath_btcoex_attach(struct ath_softc *sc)
348 {
349 	int ret;
350 	struct ath_hal *ah = sc->sc_ah;
351 	const char *profname;
352 
353 	/*
354 	 * No chipset bluetooth coexistence? Then do nothing.
355 	 */
356 	if (! ath_hal_btcoex_supported(ah))
357 		return (0);
358 
359 	/*
360 	 * Look at the hints to determine which bluetooth
361 	 * profile to configure.
362 	 */
363 	ret = resource_string_value(device_get_name(sc->sc_dev),
364 	    device_get_unit(sc->sc_dev),
365 	    "btcoex_profile",
366 	    &profname);
367 	if (ret != 0) {
368 		/* nothing to do */
369 		return (0);
370 	}
371 
372 	if (strncmp(profname, "wb195", 5) == 0) {
373 		ret = ath_btcoex_cfg_wb195(sc);
374 	} else if (strncmp(profname, "wb222", 5) == 0) {
375 		ret = ath_btcoex_cfg_wb222(sc);
376 	} else if (strncmp(profname, "wb225", 5) == 0) {
377 		ret = ath_btcoex_cfg_wb225(sc);
378 	} else if (strncmp(profname, "wb335", 5) == 0) {
379 		ret = ath_btcoex_cfg_wb335b(sc);
380 	} else {
381 		return (0);
382 	}
383 
384 	/*
385 	 * Propagate up failure from the actual attach phase.
386 	 */
387 	if (ret != 0)
388 		return (ret);
389 
390 	return (0);
391 }
392 
393 /*
394  * Detach btcoex from the given interface
395  */
396 int
397 ath_btcoex_detach(struct ath_softc *sc)
398 {
399 
400 	return (0);
401 }
402 
403 /*
404  * Configure or disable bluetooth coexistence on the given channel.
405  *
406  * For AR9285/AR9287/AR9485, we'll never see a 5GHz channel, so we just
407  * assume bluetooth coexistence is always on.
408  *
409  * For AR9462, we may see a 5GHz channel; bluetooth coexistence should
410  * not be enabled on those channels.
411  */
412 int
413 ath_btcoex_enable(struct ath_softc *sc, const struct ieee80211_channel *chan)
414 {
415 
416 	return (0);
417 }
418 
419 /*
420  * Handle ioctl requests from the diagnostic interface.
421  *
422  * The initial part of this code resembles ath_ioctl_diag();
423  * it's likely a good idea to reduce duplication between
424  * these two routines.
425  */
426 int
427 ath_btcoex_ioctl(struct ath_softc *sc, struct ath_diag *ad)
428 {
429 	unsigned int id = ad->ad_id & ATH_DIAG_ID;
430 	void *indata = NULL;
431 	void *outdata = NULL;
432 	u_int32_t insize = ad->ad_in_size;
433 	u_int32_t outsize = ad->ad_out_size;
434 	int error = 0;
435 //	int val;
436 
437 	if (ad->ad_id & ATH_DIAG_IN) {
438 		/*
439 		 * Copy in data.
440 		 */
441 		indata = malloc(insize, M_TEMP, M_NOWAIT);
442 		if (indata == NULL) {
443 			error = ENOMEM;
444 			goto bad;
445 		}
446 		error = copyin(ad->ad_in_data, indata, insize);
447 		if (error)
448 			goto bad;
449 	}
450 	if (ad->ad_id & ATH_DIAG_DYN) {
451 		/*
452 		 * Allocate a buffer for the results (otherwise the HAL
453 		 * returns a pointer to a buffer where we can read the
454 		 * results).  Note that we depend on the HAL leaving this
455 		 * pointer for us to use below in reclaiming the buffer;
456 		 * may want to be more defensive.
457 		 */
458 		outdata = malloc(outsize, M_TEMP, M_NOWAIT);
459 		if (outdata == NULL) {
460 			error = ENOMEM;
461 			goto bad;
462 		}
463 	}
464 	switch (id) {
465 		default:
466 			error = EINVAL;
467 	}
468 	if (outsize < ad->ad_out_size)
469 		ad->ad_out_size = outsize;
470 	if (outdata && copyout(outdata, ad->ad_out_data, ad->ad_out_size))
471 		error = EFAULT;
472 bad:
473 	if ((ad->ad_id & ATH_DIAG_IN) && indata != NULL)
474 		free(indata, M_TEMP);
475 	if ((ad->ad_id & ATH_DIAG_DYN) && outdata != NULL)
476 		free(outdata, M_TEMP);
477 	return (error);
478 }
479 
480