summaryrefslogtreecommitdiff
path: root/drivers/net/can/led.c
blob: a3d99a8fd2d19ee46f846448d2707f8dadf3e4af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
 * Copyright 2012, Fabio Baltieri <fabio.baltieri@gmail.com>
 * Copyright 2012, Kurt Van Dijck <kurt.van.dijck@eia.be>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include <linux/module.h>
#include <linux/device.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/netdevice.h>
#include <linux/can/dev.h>

#include <linux/can/led.h>

static unsigned long led_delay = 50;
module_param(led_delay, ulong, 0644);
MODULE_PARM_DESC(led_delay,
		"blink delay time for activity leds (msecs, default: 50).");

/* Trigger a LED event in response to a CAN device event */
void can_led_event(struct net_device *netdev, enum can_led_event event)
{
	struct can_priv *priv = netdev_priv(netdev);

	switch (event) {
	case CAN_LED_EVENT_OPEN:
		led_trigger_event(priv->tx_led_trig, LED_FULL);
		led_trigger_event(priv->rx_led_trig, LED_FULL);
		break;
	case CAN_LED_EVENT_STOP:
		led_trigger_event(priv->tx_led_trig, LED_OFF);
		led_trigger_event(priv->rx_led_trig, LED_OFF);
		break;
	case CAN_LED_EVENT_TX:
		if (led_delay)
			led_trigger_blink_oneshot(priv->tx_led_trig,
						  &led_delay, &led_delay, 1);
		break;
	case CAN_LED_EVENT_RX:
		if (led_delay)
			led_trigger_blink_oneshot(priv->rx_led_trig,
						  &led_delay, &led_delay, 1);
		break;
	}
}
EXPORT_SYMBOL_GPL(can_led_event);

static void can_led_release(struct device *gendev, void *res)
{
	struct can_priv *priv = netdev_priv(to_net_dev(gendev));

	led_trigger_unregister_simple(priv->tx_led_trig);
	led_trigger_unregister_simple(priv->rx_led_trig);
}

/* Register CAN LED triggers for a CAN device
 *
 * This is normally called from a driver's probe function
 */
void devm_can_led_init(struct net_device *netdev)
{
	struct can_priv *priv = netdev_priv(netdev);
	void *res;

	res = devres_alloc(can_led_release, 0, GFP_KERNEL);
	if (!res) {
		netdev_err(netdev, "cannot register LED triggers\n");
		return;
	}

	snprintf(priv->tx_led_trig_name, sizeof(priv->tx_led_trig_name),
		 "%s-tx", netdev->name);
	snprintf(priv->rx_led_trig_name, sizeof(priv->rx_led_trig_name),
		 "%s-rx", netdev->name);

	led_trigger_register_simple(priv->tx_led_trig_name,
				    &priv->tx_led_trig);
	led_trigger_register_simple(priv->rx_led_trig_name,
				    &priv->rx_led_trig);

	devres_add(&netdev->dev, res);
}
EXPORT_SYMBOL_GPL(devm_can_led_init);

/* NETDEV rename notifier to rename the associated led triggers too */
static int can_led_notifier(struct notifier_block *nb, unsigned long msg,
			    void *ptr)
{
	struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
	struct can_priv *priv = safe_candev_priv(netdev);
	char name[CAN_LED_NAME_SZ];

	if (!priv)
		return NOTIFY_DONE;

	if (msg == NETDEV_CHANGENAME) {
		snprintf(name, sizeof(name), "%s-tx", netdev->name);
		led_trigger_rename_static(name, priv->tx_led_trig);

		snprintf(name, sizeof(name), "%s-rx", netdev->name);
		led_trigger_rename_static(name, priv->rx_led_trig);
	}

	return NOTIFY_DONE;
}

/* notifier block for netdevice event */
static struct notifier_block can_netdev_notifier __read_mostly = {
	.notifier_call = can_led_notifier,
};

int __init can_led_notifier_init(void)
{
	return register_netdevice_notifier(&can_netdev_notifier);
}

void __exit can_led_notifier_exit(void)
{
	unregister_netdevice_notifier(&can_netdev_notifier);
}