]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/ath/if_ath_spectral.c
Import mandoc 1.14.3
[FreeBSD/FreeBSD.git] / sys / dev / ath / if_ath_spectral.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 __FBSDID("$FreeBSD$");
33
34 /*
35  * Implement some basic spectral scan control logic.
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_spectral.h>
73 #include <dev/ath/if_ath_misc.h>
74
75 #include <dev/ath/ath_hal/ah_desc.h>
76
77 struct ath_spectral_state {
78         HAL_SPECTRAL_PARAM      spectral_state;
79
80         /*
81          * Should we enable spectral scan upon
82          * each network interface reset/change?
83          *
84          * This is intended to allow spectral scan
85          * frame reporting during channel scans.
86          *
87          * Later on it can morph into a larger
88          * scale config method where it pushes
89          * a "channel scan" config into the hardware
90          * rather than just the spectral_state
91          * config.
92          */
93         int spectral_enable_after_reset;
94 };
95
96 /*
97  * Methods which are required
98  */
99
100 /*
101  * Attach spectral to the given interface
102  */
103 int
104 ath_spectral_attach(struct ath_softc *sc)
105 {
106         struct ath_spectral_state *ss;
107
108         /*
109          * If spectral isn't supported, don't error - just
110          * quietly complete.
111          */
112         if (! ath_hal_spectral_supported(sc->sc_ah))
113                 return (0);
114
115         ss = malloc(sizeof(struct ath_spectral_state),
116             M_TEMP, M_WAITOK | M_ZERO);
117
118         if (ss == NULL) {
119                 device_printf(sc->sc_dev, "%s: failed to alloc memory\n",
120                     __func__);
121                 return (-ENOMEM);
122         }
123
124         sc->sc_spectral = ss;
125
126         (void) ath_hal_spectral_get_config(sc->sc_ah, &ss->spectral_state);
127
128         return (0);
129 }
130
131 /*
132  * Detach spectral from the given interface
133  */
134 int
135 ath_spectral_detach(struct ath_softc *sc)
136 {
137
138         if (! ath_hal_spectral_supported(sc->sc_ah))
139                 return (0);
140
141         if (sc->sc_spectral != NULL) {
142                 free(sc->sc_spectral, M_TEMP);
143         }
144         return (0);
145 }
146
147 /*
148  * Check whether spectral needs enabling and if so,
149  * flip it on.
150  */
151 int
152 ath_spectral_enable(struct ath_softc *sc, struct ieee80211_channel *ch)
153 {
154         struct ath_spectral_state *ss = sc->sc_spectral;
155
156         /* Default to disable spectral PHY reporting */
157         sc->sc_dospectral = 0;
158
159         if (ss == NULL)
160                 return (0);
161
162         if (ss->spectral_enable_after_reset) {
163                 ath_hal_spectral_configure(sc->sc_ah,
164                     &ss->spectral_state);
165                 (void) ath_hal_spectral_start(sc->sc_ah);
166                 sc->sc_dospectral = 1;
167         }
168         return (0);
169 }
170
171 /*
172  * Handle ioctl requests from the diagnostic interface.
173  *
174  * The initial part of this code resembles ath_ioctl_diag();
175  * it's likely a good idea to reduce duplication between
176  * these two routines.
177  */
178 int
179 ath_ioctl_spectral(struct ath_softc *sc, struct ath_diag *ad)
180 {
181         unsigned int id = ad->ad_id & ATH_DIAG_ID;
182         void *indata = NULL;
183         void *outdata = NULL;
184         u_int32_t insize = ad->ad_in_size;
185         u_int32_t outsize = ad->ad_out_size;
186         int error = 0;
187         HAL_SPECTRAL_PARAM peout;
188         HAL_SPECTRAL_PARAM *pe;
189         struct ath_spectral_state *ss = sc->sc_spectral;
190         int val;
191
192         if (! ath_hal_spectral_supported(sc->sc_ah))
193                 return (EINVAL);
194
195         ATH_LOCK(sc);
196         ath_power_set_power_state(sc, HAL_PM_AWAKE);
197         ATH_UNLOCK(sc);
198
199         if (ad->ad_id & ATH_DIAG_IN) {
200                 /*
201                  * Copy in data.
202                  */
203                 indata = malloc(insize, M_TEMP, M_NOWAIT);
204                 if (indata == NULL) {
205                         error = ENOMEM;
206                         goto bad;
207                 }
208                 error = copyin(ad->ad_in_data, indata, insize);
209                 if (error)
210                         goto bad;
211         }
212         if (ad->ad_id & ATH_DIAG_DYN) {
213                 /*
214                  * Allocate a buffer for the results (otherwise the HAL
215                  * returns a pointer to a buffer where we can read the
216                  * results).  Note that we depend on the HAL leaving this
217                  * pointer for us to use below in reclaiming the buffer;
218                  * may want to be more defensive.
219                  */
220                 outdata = malloc(outsize, M_TEMP, M_NOWAIT);
221                 if (outdata == NULL) {
222                         error = ENOMEM;
223                         goto bad;
224                 }
225         }
226         switch (id) {
227                 case SPECTRAL_CONTROL_GET_PARAMS:
228                         memset(&peout, 0, sizeof(peout));
229                         outsize = sizeof(HAL_SPECTRAL_PARAM);
230                         ath_hal_spectral_get_config(sc->sc_ah, &peout);
231                         pe = (HAL_SPECTRAL_PARAM *) outdata;
232                         memcpy(pe, &peout, sizeof(*pe));
233                         break;
234                 case SPECTRAL_CONTROL_SET_PARAMS:
235                         if (insize < sizeof(HAL_SPECTRAL_PARAM)) {
236                                 error = EINVAL;
237                                 break;
238                         }
239                         pe = (HAL_SPECTRAL_PARAM *) indata;
240                         ath_hal_spectral_configure(sc->sc_ah, pe);
241                         /* Save a local copy of the updated parameters */
242                         ath_hal_spectral_get_config(sc->sc_ah,
243                             &ss->spectral_state);
244                         break;
245                 case SPECTRAL_CONTROL_START:
246                         ath_hal_spectral_configure(sc->sc_ah,
247                             &ss->spectral_state);
248                         (void) ath_hal_spectral_start(sc->sc_ah);
249                         sc->sc_dospectral = 1;
250                         /* XXX need to update the PHY mask in the driver */
251                         break;
252                 case SPECTRAL_CONTROL_STOP:
253                         (void) ath_hal_spectral_stop(sc->sc_ah);
254                         sc->sc_dospectral = 0;
255                         /* XXX need to update the PHY mask in the driver */
256                         break;
257                 case SPECTRAL_CONTROL_ENABLE_AT_RESET:
258                         if (insize < sizeof(int)) {
259                                 device_printf(sc->sc_dev, "%d != %d\n",
260                                     insize,
261                                     (int) sizeof(int));
262                                 error = EINVAL;
263                                 break;
264                         }
265                         if (indata == NULL) {
266                                 device_printf(sc->sc_dev, "indata=NULL\n");
267                                 error = EINVAL;
268                                 break;
269                         }
270                         val = * ((int *) indata);
271                         if (val == 0)
272                                 ss->spectral_enable_after_reset = 0;
273                         else
274                                 ss->spectral_enable_after_reset = 1;
275                         break;
276                 case SPECTRAL_CONTROL_ENABLE:
277                         /* XXX TODO */
278                 case SPECTRAL_CONTROL_DISABLE:
279                         /* XXX TODO */
280                 break;
281                 default:
282                         error = EINVAL;
283         }
284         if (outsize < ad->ad_out_size)
285                 ad->ad_out_size = outsize;
286         if (outdata && copyout(outdata, ad->ad_out_data, ad->ad_out_size))
287                 error = EFAULT;
288 bad:
289         if ((ad->ad_id & ATH_DIAG_IN) && indata != NULL)
290                 free(indata, M_TEMP);
291         if ((ad->ad_id & ATH_DIAG_DYN) && outdata != NULL)
292                 free(outdata, M_TEMP);
293         ATH_LOCK(sc);
294         ath_power_restore_power_state(sc);
295         ATH_UNLOCK(sc);
296
297         return (error);
298 }
299