xref: /freebsd/sys/dev/ath/if_ath_led.c (revision 9f23cbd6cae82fd77edfad7173432fa8dccd0a95)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer,
12  *    without modification.
13  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
14  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
15  *    redistribution must be conditioned upon including a substantially
16  *    similar Disclaimer requirement for further binary redistribution.
17  *
18  * NO WARRANTY
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
22  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
24  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
27  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
29  * THE POSSIBILITY OF SUCH DAMAGES.
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 /*
36  * Driver for the Atheros Wireless LAN controller.
37  *
38  * This software is derived from work of Atsushi Onoe; his contribution
39  * is greatly appreciated.
40  */
41 
42 #include "opt_inet.h"
43 #include "opt_ath.h"
44 /*
45  * This is needed for register operations which are performed
46  * by the driver - eg, calls to ath_hal_gettsf32().
47  */
48 #include "opt_ah.h"
49 #include "opt_wlan.h"
50 
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/sysctl.h>
54 #include <sys/mbuf.h>
55 #include <sys/malloc.h>
56 #include <sys/lock.h>
57 #include <sys/mutex.h>
58 #include <sys/kernel.h>
59 #include <sys/socket.h>
60 #include <sys/sockio.h>
61 #include <sys/errno.h>
62 #include <sys/callout.h>
63 #include <sys/bus.h>
64 #include <sys/endian.h>
65 #include <sys/kthread.h>
66 #include <sys/taskqueue.h>
67 #include <sys/priv.h>
68 #include <sys/module.h>
69 #include <sys/ktr.h>
70 #include <sys/smp.h>	/* for mp_ncpus */
71 
72 #include <machine/bus.h>
73 
74 #include <net/if.h>
75 #include <net/if_dl.h>
76 #include <net/if_media.h>
77 #include <net/if_types.h>
78 #include <net/if_arp.h>
79 #include <net/ethernet.h>
80 #include <net/if_llc.h>
81 #include <net/if_var.h>
82 
83 #include <net80211/ieee80211_var.h>
84 #include <net80211/ieee80211_regdomain.h>
85 #ifdef IEEE80211_SUPPORT_SUPERG
86 #include <net80211/ieee80211_superg.h>
87 #endif
88 #ifdef IEEE80211_SUPPORT_TDMA
89 #include <net80211/ieee80211_tdma.h>
90 #endif
91 
92 #include <net/bpf.h>
93 
94 #ifdef INET
95 #include <netinet/in.h>
96 #include <netinet/if_ether.h>
97 #endif
98 
99 #include <dev/ath/if_athvar.h>
100 #include <dev/ath/ath_hal/ah_devid.h>		/* XXX for softled */
101 #include <dev/ath/ath_hal/ah_diagcodes.h>
102 
103 #include <dev/ath/if_ath_debug.h>
104 #include <dev/ath/if_ath_misc.h>
105 
106 #include <dev/ath/if_ath_led.h>
107 
108 /*
109  * Software LED driver routines.
110  */
111 
112 /*
113  * XXX TODO: move the LED sysctls here.
114  */
115 
116 /*
117  * Configure the hardware for software and LED blinking.
118  * The user may choose to configure part of each, depending upon the
119  * NIC being used.
120  *
121  * This requires the configuration to be set before this function
122  * is called.
123  */
124 void
125 ath_led_config(struct ath_softc *sc)
126 {
127 
128 	ATH_LOCK(sc);
129 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
130 	ATH_UNLOCK(sc);
131 
132 	/* Software LED blinking - GPIO controlled LED */
133 	if (sc->sc_softled) {
134 		ath_hal_gpioCfgOutput(sc->sc_ah, sc->sc_ledpin,
135 		    HAL_GPIO_OUTPUT_MUX_AS_OUTPUT);
136 		ath_hal_gpioset(sc->sc_ah, sc->sc_ledpin, !sc->sc_ledon);
137 	}
138 
139 	/* Hardware LED blinking - MAC controlled LED */
140 	if (sc->sc_hardled) {
141 		/*
142 		 * Only enable each LED if required.
143 		 *
144 		 * Some NICs only have one LED connected; others may
145 		 * have GPIO1/GPIO2 connected to other hardware.
146 		 */
147 		if (sc->sc_led_pwr_pin > 0)
148 			ath_hal_gpioCfgOutput(sc->sc_ah, sc->sc_led_pwr_pin,
149 			    HAL_GPIO_OUTPUT_MUX_MAC_POWER_LED);
150 		if (sc->sc_led_net_pin > 0)
151 			ath_hal_gpioCfgOutput(sc->sc_ah, sc->sc_led_net_pin,
152 			    HAL_GPIO_OUTPUT_MUX_MAC_NETWORK_LED);
153 	}
154 
155 	ATH_LOCK(sc);
156 	ath_power_restore_power_state(sc);
157 	ATH_UNLOCK(sc);
158 }
159 
160 static void
161 ath_led_done(void *arg)
162 {
163 	struct ath_softc *sc = arg;
164 
165 	sc->sc_blinking = 0;
166 }
167 
168 /*
169  * Turn the LED off: flip the pin and then set a timer so no
170  * update will happen for the specified duration.
171  */
172 static void
173 ath_led_off(void *arg)
174 {
175 	struct ath_softc *sc = arg;
176 
177 	ath_hal_gpioset(sc->sc_ah, sc->sc_ledpin, !sc->sc_ledon);
178 	callout_reset(&sc->sc_ledtimer, sc->sc_ledoff, ath_led_done, sc);
179 }
180 
181 /*
182  * Blink the LED according to the specified on/off times.
183  */
184 static void
185 ath_led_blink(struct ath_softc *sc, int on, int off)
186 {
187 	DPRINTF(sc, ATH_DEBUG_LED, "%s: on %u off %u\n", __func__, on, off);
188 	ath_hal_gpioset(sc->sc_ah, sc->sc_ledpin, sc->sc_ledon);
189 	sc->sc_blinking = 1;
190 	sc->sc_ledoff = off;
191 	callout_reset(&sc->sc_ledtimer, on, ath_led_off, sc);
192 }
193 
194 void
195 ath_led_event(struct ath_softc *sc, int rix)
196 {
197 	sc->sc_ledevent = ticks;	/* time of last event */
198 	if (sc->sc_blinking)		/* don't interrupt active blink */
199 		return;
200 	ath_led_blink(sc, sc->sc_hwmap[rix].ledon, sc->sc_hwmap[rix].ledoff);
201 }
202