1 // SPDX-License-Identifier: GPL-2.0-only
3 * Common code for mac80211 Prism54 drivers
5 * Copyright (c) 2006, Michael Wu <flamingice@sourmilk.net>
6 * Copyright (c) 2007-2009, Christian Lamparter <chunkeey@web.de>
7 * Copyright 2008, Johannes Berg <johannes@sipsolutions.net>
10 * - the islsm (softmac prism54) driver, which is:
11 * Copyright 2004-2006 Jean-Baptiste Note <jbnote@gmail.com>, et al.
13 * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies).
16 #include <linux/firmware.h>
17 #include <linux/etherdevice.h>
19 #include <net/mac80211.h>
20 #ifdef CONFIG_P54_LEDS
21 #include <linux/leds.h>
22 #endif /* CONFIG_P54_LEDS */
27 static void p54_update_leds(struct work_struct
*work
)
29 struct p54_common
*priv
= container_of(work
, struct p54_common
,
31 int err
, i
, tmp
, blink_delay
= 400;
34 /* Don't toggle the LED, when the device is down. */
35 if (priv
->mode
== NL80211_IFTYPE_UNSPECIFIED
)
38 for (i
= 0; i
< ARRAY_SIZE(priv
->leds
); i
++)
39 if (priv
->leds
[i
].toggled
) {
40 priv
->softled_state
|= BIT(i
);
42 tmp
= 70 + 200 / (priv
->leds
[i
].toggled
);
43 if (tmp
< blink_delay
)
46 if (priv
->leds
[i
].led_dev
.brightness
== LED_OFF
)
49 priv
->leds
[i
].toggled
=
50 !!priv
->leds
[i
].led_dev
.brightness
;
52 priv
->softled_state
&= ~BIT(i
);
54 err
= p54_set_leds(priv
);
55 if (err
&& net_ratelimit())
56 wiphy_err(priv
->hw
->wiphy
,
57 "failed to update LEDs (%d).\n", err
);
60 ieee80211_queue_delayed_work(priv
->hw
, &priv
->led_work
,
61 msecs_to_jiffies(blink_delay
));
64 static void p54_led_brightness_set(struct led_classdev
*led_dev
,
65 enum led_brightness brightness
)
67 struct p54_led_dev
*led
= container_of(led_dev
, struct p54_led_dev
,
69 struct ieee80211_hw
*dev
= led
->hw_dev
;
70 struct p54_common
*priv
= dev
->priv
;
72 if (priv
->mode
== NL80211_IFTYPE_UNSPECIFIED
)
75 if ((brightness
) && (led
->registered
)) {
77 ieee80211_queue_delayed_work(priv
->hw
, &priv
->led_work
, HZ
/10);
81 static int p54_register_led(struct p54_common
*priv
,
82 unsigned int led_index
,
83 char *name
, const char *trigger
)
85 struct p54_led_dev
*led
= &priv
->leds
[led_index
];
91 snprintf(led
->name
, sizeof(led
->name
), "p54-%s::%s",
92 wiphy_name(priv
->hw
->wiphy
), name
);
93 led
->hw_dev
= priv
->hw
;
94 led
->index
= led_index
;
95 led
->led_dev
.name
= led
->name
;
96 led
->led_dev
.default_trigger
= trigger
;
97 led
->led_dev
.brightness_set
= p54_led_brightness_set
;
99 err
= led_classdev_register(wiphy_dev(priv
->hw
->wiphy
), &led
->led_dev
);
101 wiphy_err(priv
->hw
->wiphy
,
102 "Failed to register %s LED.\n", name
);
109 int p54_init_leds(struct p54_common
*priv
)
115 * Figure out if the EEPROM contains some hints about the number
116 * of available/programmable LEDs of the device.
119 INIT_DELAYED_WORK(&priv
->led_work
, p54_update_leds
);
121 err
= p54_register_led(priv
, 0, "assoc",
122 ieee80211_get_assoc_led_name(priv
->hw
));
126 err
= p54_register_led(priv
, 1, "tx",
127 ieee80211_get_tx_led_name(priv
->hw
));
131 err
= p54_register_led(priv
, 2, "rx",
132 ieee80211_get_rx_led_name(priv
->hw
));
136 err
= p54_register_led(priv
, 3, "radio",
137 ieee80211_get_radio_led_name(priv
->hw
));
141 err
= p54_set_leds(priv
);
145 void p54_unregister_leds(struct p54_common
*priv
)
149 for (i
= 0; i
< ARRAY_SIZE(priv
->leds
); i
++) {
150 if (priv
->leds
[i
].registered
) {
151 priv
->leds
[i
].registered
= false;
152 priv
->leds
[i
].toggled
= 0;
153 led_classdev_unregister(&priv
->leds
[i
].led_dev
);
157 cancel_delayed_work_sync(&priv
->led_work
);