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