aboutsummaryrefslogtreecommitdiff
path: root/drivers/misc/modbus_ext.c
blob: f4838e4dcffe30928a6f10b6dd475722debc9c9a (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
/*
 * Copyright (C) 2016 Motorola Mobility, Inc.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/notifier.h>

#include <linux/mods/modbus_ext.h>

static DEFINE_MUTEX(modbus_ext_mutex);
static BLOCKING_NOTIFIER_HEAD(modbus_ext_notifier_list);
static struct modbus_ext_status modbus_ext_current_status;

/*
 * Add caller to receive notifications of changes in attach state.
 */
int modbus_ext_register_notifier(struct notifier_block *nb,
		modbus_ext_cb cb)
{
	int ret =  blocking_notifier_chain_register(&modbus_ext_notifier_list,
							nb);

	if (cb && !ret) {
		struct modbus_ext_status st;

		mutex_lock(&modbus_ext_mutex);
		st = modbus_ext_current_status;
		mutex_unlock(&modbus_ext_mutex);

		cb(&st);
	}
	return ret;
}
EXPORT_SYMBOL(modbus_ext_register_notifier);

/*
 * Remove caller from receiving notifications of changes in attach state
 */
int modbus_ext_unregister_notifier(struct notifier_block *nb)
{
	return blocking_notifier_chain_unregister(&modbus_ext_notifier_list,
							nb);
}
EXPORT_SYMBOL(modbus_ext_unregister_notifier);

/*
 * Get the current attach state for the SL interface
 */
void modbus_ext_get_state(struct modbus_ext_status *status)
{
	mutex_lock(&modbus_ext_mutex);
	*status = modbus_ext_current_status;
	mutex_unlock(&modbus_ext_mutex);
}
EXPORT_SYMBOL(modbus_ext_get_state);

/*
 * Set the current attach state for the SL interface
 */
void modbus_ext_set_state(const struct modbus_ext_status *status)
{
	struct modbus_ext_status st;

	mutex_lock(&modbus_ext_mutex);
	modbus_ext_current_status = *status;
	st = *status;
	mutex_unlock(&modbus_ext_mutex);

	blocking_notifier_call_chain(&modbus_ext_notifier_list, st.active, &st);
}
EXPORT_SYMBOL(modbus_ext_set_state);