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