1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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: releng/12.0/sys/dev/ath/if_ath_led.c 326255 2017-11-27 14:52:40Z pfg $");
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
82#include <net80211/ieee80211_var.h>
83#include <net80211/ieee80211_regdomain.h>
84#ifdef IEEE80211_SUPPORT_SUPERG
85#include <net80211/ieee80211_superg.h>
86#endif
87#ifdef IEEE80211_SUPPORT_TDMA
88#include <net80211/ieee80211_tdma.h>
89#endif
90
91#include <net/bpf.h>
92
93#ifdef INET
94#include <netinet/in.h>
95#include <netinet/if_ether.h>
96#endif
97
98#include <dev/ath/if_athvar.h>
99#include <dev/ath/ath_hal/ah_devid.h>		/* XXX for softled */
100#include <dev/ath/ath_hal/ah_diagcodes.h>
101
102#include <dev/ath/if_ath_debug.h>
103#include <dev/ath/if_ath_misc.h>
104
105#include <dev/ath/if_ath_led.h>
106
107/*
108 * Software LED driver routines.
109 */
110
111/*
112 * XXX TODO: move the LED sysctls here.
113 */
114
115/*
116 * Configure the hardware for software and LED blinking.
117 * The user may choose to configure part of each, depending upon the
118 * NIC being used.
119 *
120 * This requires the configuration to be set before this function
121 * is called.
122 */
123void
124ath_led_config(struct ath_softc *sc)
125{
126
127	ATH_LOCK(sc);
128	ath_power_set_power_state(sc, HAL_PM_AWAKE);
129	ATH_UNLOCK(sc);
130
131	/* Software LED blinking - GPIO controlled LED */
132	if (sc->sc_softled) {
133		ath_hal_gpioCfgOutput(sc->sc_ah, sc->sc_ledpin,
134		    HAL_GPIO_OUTPUT_MUX_AS_OUTPUT);
135		ath_hal_gpioset(sc->sc_ah, sc->sc_ledpin, !sc->sc_ledon);
136	}
137
138	/* Hardware LED blinking - MAC controlled LED */
139	if (sc->sc_hardled) {
140		/*
141		 * Only enable each LED if required.
142		 *
143		 * Some NICs only have one LED connected; others may
144		 * have GPIO1/GPIO2 connected to other hardware.
145		 */
146		if (sc->sc_led_pwr_pin > 0)
147			ath_hal_gpioCfgOutput(sc->sc_ah, sc->sc_led_pwr_pin,
148			    HAL_GPIO_OUTPUT_MUX_MAC_POWER_LED);
149		if (sc->sc_led_net_pin > 0)
150			ath_hal_gpioCfgOutput(sc->sc_ah, sc->sc_led_net_pin,
151			    HAL_GPIO_OUTPUT_MUX_MAC_NETWORK_LED);
152	}
153
154	ATH_LOCK(sc);
155	ath_power_restore_power_state(sc);
156	ATH_UNLOCK(sc);
157}
158
159static void
160ath_led_done(void *arg)
161{
162	struct ath_softc *sc = arg;
163
164	sc->sc_blinking = 0;
165}
166
167/*
168 * Turn the LED off: flip the pin and then set a timer so no
169 * update will happen for the specified duration.
170 */
171static void
172ath_led_off(void *arg)
173{
174	struct ath_softc *sc = arg;
175
176	ath_hal_gpioset(sc->sc_ah, sc->sc_ledpin, !sc->sc_ledon);
177	callout_reset(&sc->sc_ledtimer, sc->sc_ledoff, ath_led_done, sc);
178}
179
180/*
181 * Blink the LED according to the specified on/off times.
182 */
183static void
184ath_led_blink(struct ath_softc *sc, int on, int off)
185{
186	DPRINTF(sc, ATH_DEBUG_LED, "%s: on %u off %u\n", __func__, on, off);
187	ath_hal_gpioset(sc->sc_ah, sc->sc_ledpin, sc->sc_ledon);
188	sc->sc_blinking = 1;
189	sc->sc_ledoff = off;
190	callout_reset(&sc->sc_ledtimer, on, ath_led_off, sc);
191}
192
193void
194ath_led_event(struct ath_softc *sc, int rix)
195{
196	sc->sc_ledevent = ticks;	/* time of last event */
197	if (sc->sc_blinking)		/* don't interrupt active blink */
198		return;
199	ath_led_blink(sc, sc->sc_hwmap[rix].ledon, sc->sc_hwmap[rix].ledoff);
200}
201