xref: /linux/drivers/net/wireless/intersil/p54/led.c (revision e5c86679d5e864947a52fb31e45a425dea3e7fa9)
1 /*
2  * Common code for mac80211 Prism54 drivers
3  *
4  * Copyright (c) 2006, Michael Wu <flamingice@sourmilk.net>
5  * Copyright (c) 2007-2009, Christian Lamparter <chunkeey@web.de>
6  * Copyright 2008, Johannes Berg <johannes@sipsolutions.net>
7  *
8  * Based on:
9  * - the islsm (softmac prism54) driver, which is:
10  *   Copyright 2004-2006 Jean-Baptiste Note <jbnote@gmail.com>, et al.
11  * - stlc45xx driver
12  *   Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies).
13  *
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License version 2 as
16  * published by the Free Software Foundation.
17  */
18 
19 #include <linux/firmware.h>
20 #include <linux/etherdevice.h>
21 
22 #include <net/mac80211.h>
23 #ifdef CONFIG_P54_LEDS
24 #include <linux/leds.h>
25 #endif /* CONFIG_P54_LEDS */
26 
27 #include "p54.h"
28 #include "lmac.h"
29 
30 static void p54_update_leds(struct work_struct *work)
31 {
32 	struct p54_common *priv = container_of(work, struct p54_common,
33 					       led_work.work);
34 	int err, i, tmp, blink_delay = 400;
35 	bool rerun = false;
36 
37 	/* Don't toggle the LED, when the device is down. */
38 	if (priv->mode == NL80211_IFTYPE_UNSPECIFIED)
39 		return ;
40 
41 	for (i = 0; i < ARRAY_SIZE(priv->leds); i++)
42 		if (priv->leds[i].toggled) {
43 			priv->softled_state |= BIT(i);
44 
45 			tmp = 70 + 200 / (priv->leds[i].toggled);
46 			if (tmp < blink_delay)
47 				blink_delay = tmp;
48 
49 			if (priv->leds[i].led_dev.brightness == LED_OFF)
50 				rerun = true;
51 
52 			priv->leds[i].toggled =
53 				!!priv->leds[i].led_dev.brightness;
54 		} else
55 			priv->softled_state &= ~BIT(i);
56 
57 	err = p54_set_leds(priv);
58 	if (err && net_ratelimit())
59 		wiphy_err(priv->hw->wiphy,
60 			  "failed to update LEDs (%d).\n", err);
61 
62 	if (rerun)
63 		ieee80211_queue_delayed_work(priv->hw, &priv->led_work,
64 			msecs_to_jiffies(blink_delay));
65 }
66 
67 static void p54_led_brightness_set(struct led_classdev *led_dev,
68 				   enum led_brightness brightness)
69 {
70 	struct p54_led_dev *led = container_of(led_dev, struct p54_led_dev,
71 					       led_dev);
72 	struct ieee80211_hw *dev = led->hw_dev;
73 	struct p54_common *priv = dev->priv;
74 
75 	if (priv->mode == NL80211_IFTYPE_UNSPECIFIED)
76 		return ;
77 
78 	if ((brightness) && (led->registered)) {
79 		led->toggled++;
80 		ieee80211_queue_delayed_work(priv->hw, &priv->led_work, HZ/10);
81 	}
82 }
83 
84 static int p54_register_led(struct p54_common *priv,
85 			    unsigned int led_index,
86 			    char *name, const char *trigger)
87 {
88 	struct p54_led_dev *led = &priv->leds[led_index];
89 	int err;
90 
91 	if (led->registered)
92 		return -EEXIST;
93 
94 	snprintf(led->name, sizeof(led->name), "p54-%s::%s",
95 		 wiphy_name(priv->hw->wiphy), name);
96 	led->hw_dev = priv->hw;
97 	led->index = led_index;
98 	led->led_dev.name = led->name;
99 	led->led_dev.default_trigger = trigger;
100 	led->led_dev.brightness_set = p54_led_brightness_set;
101 
102 	err = led_classdev_register(wiphy_dev(priv->hw->wiphy), &led->led_dev);
103 	if (err)
104 		wiphy_err(priv->hw->wiphy,
105 			  "Failed to register %s LED.\n", name);
106 	else
107 		led->registered = 1;
108 
109 	return err;
110 }
111 
112 int p54_init_leds(struct p54_common *priv)
113 {
114 	int err;
115 
116 	/*
117 	 * TODO:
118 	 * Figure out if the EEPROM contains some hints about the number
119 	 * of available/programmable LEDs of the device.
120 	 */
121 
122 	INIT_DELAYED_WORK(&priv->led_work, p54_update_leds);
123 
124 	err = p54_register_led(priv, 0, "assoc",
125 			       ieee80211_get_assoc_led_name(priv->hw));
126 	if (err)
127 		return err;
128 
129 	err = p54_register_led(priv, 1, "tx",
130 			       ieee80211_get_tx_led_name(priv->hw));
131 	if (err)
132 		return err;
133 
134 	err = p54_register_led(priv, 2, "rx",
135 			       ieee80211_get_rx_led_name(priv->hw));
136 	if (err)
137 		return err;
138 
139 	err = p54_register_led(priv, 3, "radio",
140 			       ieee80211_get_radio_led_name(priv->hw));
141 	if (err)
142 		return err;
143 
144 	err = p54_set_leds(priv);
145 	return err;
146 }
147 
148 void p54_unregister_leds(struct p54_common *priv)
149 {
150 	int i;
151 
152 	for (i = 0; i < ARRAY_SIZE(priv->leds); i++) {
153 		if (priv->leds[i].registered) {
154 			priv->leds[i].registered = false;
155 			priv->leds[i].toggled = 0;
156 			led_classdev_unregister(&priv->leds[i].led_dev);
157 		}
158 	}
159 
160 	cancel_delayed_work_sync(&priv->led_work);
161 }
162