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