summaryrefstree
path: root/kernel/drivers/ifconn/ifconn_notifier.c
blob: 3fdebd5625553bc8eed393da60d65714d1a0338c (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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include <linux/device.h>
#include <linux/module.h>

#include <linux/notifier.h>
#include <linux/ifconn/ifconn_notifier.h>
#include <linux/ifconn/ifconn_manager.h>

#define DEBUG
#define SET_IFCONN_NOTIFIER_BLOCK(nb, fn, dev) do {	\
		(nb)->notifier_call = (fn);		\
		(nb)->priority = (dev);			\
} while (0)

#define DESTROY_IFCONN_NOTIFIER_BLOCK(nb)			\
		SET_IFCONN_NOTIFIER_BLOCK(nb, NULL, -1)

static char IFCONN_NOTI_DEV_Print[IFCONN_NOTIFY_MAX][10] = {
	{"MANAGER"},
	{"USB"},
	{"BATTERY"},
	{"PDIC"},
	{"MUIC"},
	{"VBUS"},
	{"CCIC"},
	{"DP"},
	{"DPUSB"},
	{"ALL"},
};

static struct ifconn_notifier pnoti[IFCONN_NOTIFY_MAX];

void _ifconn_show_attr(struct ifconn_notifier_template *t)
{
	if (t == NULL)
		return;

	pr_info("%s, src:%s, dest:%s, id:%d, event:%d, data:0x%p\n",
		__func__, IFCONN_NOTI_DEV_Print[t->src], IFCONN_NOTI_DEV_Print[t->dest], t->id, t->event, t->data);
}

int ifconn_notifier_register(struct notifier_block *nb, notifier_fn_t notifier,
			     ifconn_notifier_t listener, ifconn_notifier_t src)
{
	int ret = 0;
	struct ifconn_notifier_template *template = NULL;

	pr_info("%s: src : %d =? listner : %d, nb : %p  register\n", __func__,
		src, listener, nb);

	if (src >= IFCONN_NOTIFY_ALL || src < 0
	    || listener >= IFCONN_NOTIFY_ALL || listener < 0) {
		pr_err("%s: dev index err\n", __func__);
		return -1;
	}

	SET_IFCONN_NOTIFIER_BLOCK(nb, notifier, listener);
	ret = blocking_notifier_chain_register(&(pnoti[src].notifier_call_chain), nb);
	if (ret < 0)
		pr_err("%s: blocking_notifier_chain_register error(%d)\n",
		       __func__, ret);

	pnoti[src].nb[listener] = nb;
	if (pnoti[src].sent[listener] || pnoti[src].sent[IFCONN_NOTIFY_ALL]) {
		pr_info("%s: src : %d, listner : %d, sent : %d, sent all : %d\n", __func__,
			src, listener, pnoti[src].sent[listener], pnoti[src].sent[IFCONN_NOTIFY_ALL]);

		if (!pnoti[src].sent[listener] && pnoti[src].sent[IFCONN_NOTIFY_ALL]) {
			template = &pnoti[src].ifconn_template[IFCONN_NOTIFY_ALL];
			_ifconn_show_attr(template);
			template->src = src;
			template->dest = listener;
		} else {
			template = &(pnoti[src].ifconn_template[listener]);
		}
		_ifconn_show_attr(template);
		ret = nb->notifier_call(nb,	template->id, template);
	}

	return ret;
}

int ifconn_notifier_unregister(ifconn_notifier_t src,
			       ifconn_notifier_t listener)
{
	int ret = 0;
	struct notifier_block *nb = NULL;

	if (src >= IFCONN_NOTIFY_ALL || src < 0
	    || listener >= IFCONN_NOTIFY_ALL || listener < 0) {
		pr_err("%s: dev index err\n", __func__);
		return -1;
	}
	nb = pnoti[src].nb[listener];

	pr_info("%s: listener=%d unregister\n", __func__, nb->priority);

	ret = blocking_notifier_chain_unregister(&(pnoti[src].notifier_call_chain), nb);
	if (ret < 0)
		pr_err("%s: blocking_notifier_chain_unregister error(%d)\n",
		       __func__, ret);
	DESTROY_IFCONN_NOTIFIER_BLOCK(nb);

	return ret;
}

int ifconn_notifier_notify(ifconn_notifier_t src,
			   ifconn_notifier_t listener,
			   int noti_id,
			   int event,
			   ifconn_notifier_data_param_t param_type, void *data)
{
	int ret = 0;
	struct ifconn_notifier_template *template = NULL;
	struct notifier_block *nb = NULL;

	pr_info("%s: enter\n", __func__);

	if (src > IFCONN_NOTIFY_ALL || src < 0
	    || listener > IFCONN_NOTIFY_ALL || listener < 0) {
		pr_err("%s: dev index err\n", __func__);
		return -1;
	}
	pr_info("%s: src = %s, listener = %s, nb = 0x%p notify\n", __func__,
		IFCONN_NOTI_DEV_Print[src], IFCONN_NOTI_DEV_Print[listener],
		pnoti[src].nb[listener]);

	if (param_type == IFCONN_NOTIFY_PARAM_TEMPLATE) {
		template = (struct ifconn_notifier_template *)data;
	} else {
		template = &(pnoti[src].ifconn_template[listener]);
		template->id = (uint64_t) noti_id;
		template->up_src = template->src = (uint64_t) src;
		template->dest = (uint64_t) listener;
		template->cable_type = template->event = event;
		template->data = data;
	}

	_ifconn_show_attr(template);

	if (pnoti[src].nb[listener] == NULL)
		pnoti[src].sent[listener] = true;

	if (listener == IFCONN_NOTIFY_ALL) {
		ret = blocking_notifier_call_chain(&(pnoti[src].notifier_call_chain),
				(unsigned long)noti_id, template);
	} else {
		if (pnoti[src].nb[listener] == NULL) {
			if (param_type == IFCONN_NOTIFY_PARAM_TEMPLATE) {
				memcpy(&(pnoti[src].ifconn_template[listener]), template,
					   sizeof(struct ifconn_notifier_template));
			}
			return -1;
		}

		nb = pnoti[src].nb[listener];
		ret = nb->notifier_call(nb, (unsigned long)noti_id, template);
	}

	switch (ret) {
	case NOTIFY_STOP_MASK:
	case NOTIFY_BAD:
		pr_err("%s: notify error occur(0x%x)\n", __func__, ret);
		break;
	case NOTIFY_DONE:
	case NOTIFY_OK:
		pr_info("%s: notify done(0x%x)\n", __func__, ret);
		break;
	default:
		pr_info("%s: notify status unknown(0x%x)\n", __func__, ret);
		break;
	}

	return ret;
}

static int __init ifconn_notifier_init(void)
{
	int ret = 0;

	pr_info("%s\n", __func__);
	return ret;
}

device_initcall(ifconn_notifier_init);