summaryrefstree
path: root/kernel/drivers/usb/notify/external_notify.c
blob: 90d1427ad4410538cd961f7626be569d66943867 (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
/*
 * Copyright (C) 2016-2017 Samsung Electronics Co. Ltd.
 *
 * 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.
 */

 /* usb notify layer v3.2 */

#define pr_fmt(fmt) "usb_notify: " fmt

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/err.h>
#include <linux/usb.h>
#include <linux/usb_notify.h>

struct external_notify_struct {
	struct blocking_notifier_head notifier_call_chain;
	int call_chain_init;
};

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

#define DESTROY_EXTERNAL_NOTIFY_BLOCK(nb)			\
		SET_EXTERNAL_NOTIFY_BLOCK(nb, NULL, -1)

static struct external_notify_struct external_notifier;

static const char *cmd_string(unsigned long cmd)
{
	switch (cmd) {
	case EXTERNAL_NOTIFY_3S_NODEVICE:
		return "3s_no_device";
	case EXTERNAL_NOTIFY_DEVICE_CONNECT:
		return "device_connect";
	case EXTERNAL_NOTIFY_HOSTBLOCK_PRE:
		return "host_block_pre";
	case EXTERNAL_NOTIFY_HOSTBLOCK_POST:
		return "host_block_post";
	case EXTERNAL_NOTIFY_MDMBLOCK_PRE:
		return "mdm_block_pre";
	case EXTERNAL_NOTIFY_MDMBLOCK_POST:
		return "mdm_block_post";
	case EXTERNAL_NOTIFY_POWERROLE:
		return "power_role_notify";
	case EXTERNAL_NOTIFY_DEVICEADD:
		return "host_mode_device_added";
	default:
		return "undefined";
	}
}

static const char *listener_string(int  listener)
{
	switch (listener) {
	case EXTERNAL_NOTIFY_DEV_MUIC:
		return "muic";
	case EXTERNAL_NOTIFY_DEV_CHARGER:
		return "charger";
	case EXTERNAL_NOTIFY_DEV_PDIC:
		return "pdic";
	default:
		return "undefined";
	}
}

static int create_external_notify(void)
{
	if (!external_notifier.call_chain_init) {
		pr_info("%s\n", __func__);
		BLOCKING_INIT_NOTIFIER_HEAD
				(&(external_notifier.notifier_call_chain));
		external_notifier.call_chain_init = 1;
	}
	return 0;
}

int usb_external_notify_register(struct notifier_block *nb,
			notifier_fn_t notifier, int listener)
{
	int ret = 0;

	pr_info("%s: listener=(%s)%d register\n", __func__,
			listener_string(listener), listener);

	create_external_notify();

	SET_EXTERNAL_NOTIFY_BLOCK(nb, notifier, listener);
	ret = blocking_notifier_chain_register
				(&(external_notifier.notifier_call_chain), nb);
	if (ret < 0)
		pr_err("%s: blocking_notifier_chain_register error(%d)\n",
				__func__, ret);

	return ret;
}

int usb_external_notify_unregister(struct notifier_block *nb)
{
	int ret = 0;

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

	ret = blocking_notifier_chain_unregister
				(&(external_notifier.notifier_call_chain), nb);
	if (ret < 0)
		pr_err("%s: blocking_notifier_chain_unregister error(%d)\n",
				__func__, ret);
	DESTROY_EXTERNAL_NOTIFY_BLOCK(nb);

	return ret;
}

int send_external_notify(unsigned long cmd, int data)
{
	int ret = 0;

	pr_info("%s: cmd=%s(%lu), data=%d\n", __func__, cmd_string(cmd),
						cmd, data);

	create_external_notify();

	ret = blocking_notifier_call_chain
		(&(external_notifier.notifier_call_chain),
			cmd, (void *)&(data));

	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 external_notifier_init(void)
{
	int ret = 0;

	pr_info("%s\n", __func__);
	create_external_notify();

	return ret;
}
module_init(external_notifier_init);