summaryrefstree
path: root/kernel/drivers/misc/drb/dev_ril_bridge.c
blob: 69b4445e2c40dab9b26c4aad6f0fb426fc8dd66e (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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
 * Copyright (C) 2017 Samsung Electronics.
 *
 * This software is licensed under the terms of the GNU General Public
 * License version 2, as published by the Free Software Foundation, and
 * may be copied, distributed, and modified under those terms.
 *
 * 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/device.h>
#include <linux/sysfs.h>
#include <linux/module.h>
#include <linux/notifier.h>
#include <linux/platform_device.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <linux/of_platform.h>
#include <linux/miscdevice.h>
#include <linux/poll.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/skbuff.h>
#include <linux/netdevice.h>

#include <linux/dev_ril_bridge.h>

#define LOG_TAG "drb: "

#define drb_err(fmt, ...) \
	pr_err(LOG_TAG "%s: " pr_fmt(fmt), __func__, ##__VA_ARGS__)

#define drb_debug(fmt, ...) \
	pr_debug(LOG_TAG "%s: " pr_fmt(fmt), __func__, ##__VA_ARGS__)

#define drb_info(fmt, ...) \
	pr_info(LOG_TAG "%s: " pr_fmt(fmt), __func__, ##__VA_ARGS__)

struct drb_dev {
	atomic_t opened;
	wait_queue_head_t wq;
	struct sk_buff_head sk_rx_q;
	struct miscdevice miscdev;
};

static struct drb_dev *drb_dev;

int dev_ril_bridge_send_msg(int id, int size, void *buf)
{
	struct sk_buff *skb;
	struct sk_buff_head *rxq;
	struct sipc_fmt_hdr *sipc_hdr;
	unsigned int alloc_size;
	unsigned int headroom;

	drb_info("id=%d size=%d\n", id, size);
	if (!drb_dev) {
		drb_err("ERR! dev_ril_bridge is not ready\n");
		return -ENODEV;
	}

	rxq = &drb_dev->sk_rx_q;
	headroom = sizeof(struct sipc_fmt_hdr);
	alloc_size = size + headroom;

	skb = alloc_skb(alloc_size, GFP_ATOMIC);
	if (!skb) {
		drb_err("ERR! alloc_skb fail\n");
		return -ENOMEM;
	}

	skb_reserve(skb, headroom);
	memcpy(skb_put(skb, size), buf, size);

	sipc_hdr = (struct sipc_fmt_hdr *)skb_push(skb, headroom);
	sipc_hdr->len = alloc_size;
	sipc_hdr->main_cmd = 0x27;
	sipc_hdr->sub_cmd = id;
	sipc_hdr->cmd_type = 0x05;

	skb_queue_tail(rxq, skb);

	if (atomic_read(&drb_dev->opened) > 0)
		wake_up(&drb_dev->wq);
	else
		return -EPIPE;

	return 0;
}
EXPORT_SYMBOL_GPL(dev_ril_bridge_send_msg);

static RAW_NOTIFIER_HEAD(dev_ril_bridge_chain);

int register_dev_ril_bridge_event_notifier(struct notifier_block *nb)
{
	if (!nb)
		return -ENOENT;

	return raw_notifier_chain_register(&dev_ril_bridge_chain, nb);
}
EXPORT_SYMBOL_GPL(register_dev_ril_bridge_event_notifier);

int unregister_dev_ril_bridge_event_notifier(struct notifier_block *nb)
{
	if (!nb)
		return -ENOENT;

	return raw_notifier_chain_unregister(&dev_ril_bridge_chain, nb);
}
EXPORT_SYMBOL_GPL(unregister_dev_ril_bridge_event_notifier);

static int misc_open(struct inode *inode, struct file *filp)
{
	filp->private_data = (void *)drb_dev;
	atomic_inc(&drb_dev->opened);

	drb_info("drb (opened %d) by %s\n",
			atomic_read(&drb_dev->opened), current->comm);

	return 0;
}

static int misc_release(struct inode *inode, struct file *filp)
{
	struct drb_dev *drb_dev = (struct drb_dev *)filp->private_data;

	if (atomic_dec_and_test(&drb_dev->opened)) {
		skb_queue_purge(&drb_dev->sk_rx_q);
	}
	
	filp->private_data = NULL;

	drb_info("drb (opened %d) by %s\n",
			atomic_read(&drb_dev->opened), current->comm);

	return 0;
}

static unsigned int misc_poll(struct file *filp, struct poll_table_struct *wait)
{
	struct drb_dev *drb_dev = (struct drb_dev *)filp->private_data;
	struct sk_buff_head *rxq;
	int ret = 0;

	if (!drb_dev)
		return POLLERR;

	rxq = &drb_dev->sk_rx_q;

	if (skb_queue_empty(rxq))
		poll_wait(filp, &drb_dev->wq, wait);

	/* drb_dev was already released by shutdown logic */
	if (!drb_dev)
		return POLLERR;

	if (!skb_queue_empty(rxq))
		ret = POLLIN | POLLRDNORM;

	drb_info("poll done by %s (%d)\n", current->comm, ret);

	return ret;
}

static ssize_t misc_write(struct file *filp, const char __user *data,
			size_t count, loff_t *fops)
{
	struct dev_ril_bridge_msg msg;
	struct sipc_fmt_hdr *sipc_hdr;
	char buf[256] = { 0, };
	unsigned int size;
	
	if (count <= sizeof(struct sipc_fmt_hdr)) {
		drb_err("ERR! too small size data(count %lu)\n",
			(unsigned long)count);
		return -EFAULT;
	}

	if (count > sizeof(buf)) {
		drb_err("ERR! pkt_size is bigger than buf_size(pkt:%lu, buf:%lu)\n",
			count, sizeof(buf));
		return -EFAULT;
	}

	size = min_t(unsigned int, (unsigned int)count, (unsigned int)sizeof(buf));
	if (copy_from_user(buf, data, size)) {
		drb_err("ERR! copy_from_user fail(count %lu)\n",
			(unsigned long)count);
		return -EFAULT;
	}

	sipc_hdr = (struct sipc_fmt_hdr *)buf;

	if (sipc_hdr->main_cmd != 0x27 || sipc_hdr->cmd_type != 0x03) {
		drb_err("ERR! wrong cmd(main_cmd=%02x, cmd_type=%02x)\n",
				sipc_hdr->main_cmd, sipc_hdr->cmd_type);
		return -EFAULT;
	}
	
	msg.dev_id = sipc_hdr->sub_cmd;
	msg.data_len = size - (unsigned int)sizeof(struct sipc_fmt_hdr);
	msg.data = (void *)(buf + sizeof(struct sipc_fmt_hdr));

	drb_info("notifier_call: dev_id=%u data_len=%u\n", msg.dev_id, msg.data_len);

	raw_notifier_call_chain(&dev_ril_bridge_chain, 
			sizeof(struct dev_ril_bridge_msg), (void *)&msg);

	return size;
}

static ssize_t misc_read(struct file *filp, char *buf, size_t count,
			loff_t *fops)
{
	struct drb_dev *drb_dev = (struct drb_dev *)filp->private_data;
	struct sk_buff_head *rxq = &drb_dev->sk_rx_q;
	unsigned int cnt = (unsigned int)count;
	struct sk_buff *skb;
	ssize_t copied;

	if (skb_queue_empty(rxq)) {
		long tmo = msecs_to_jiffies(100);
		wait_event_timeout(drb_dev->wq, !skb_queue_empty(rxq), tmo);
	}

	skb = skb_dequeue(rxq);
	if (unlikely(!skb)) {
		drb_err("No data in RXQ\n");
		return 0;
	}

	copied = skb->len > cnt ? cnt : skb->len;

	if (copy_to_user(buf, skb->data, copied)) {
		drb_err("ERR! copy_to_user fail\n");
		dev_kfree_skb_any(skb);
		return -EFAULT;
	}

	drb_info("data:%d copied:%ld qlen:%d\n", skb->len, copied, rxq->qlen);

	if (skb->len > copied) {
		skb_pull(skb, copied);
		skb_queue_head(rxq, skb);
	} else {
		dev_kfree_skb_any(skb);
	}

	return copied;
}

static const struct file_operations misc_io_fops = {
	.owner = THIS_MODULE,
	.open = misc_open,
	.release = misc_release,
	.poll = misc_poll,
	.write = misc_write,
	.read = misc_read,
};

static int __init dev_ril_bridge_init(void)
{
	int err = 0;

	drb_info("+++\n");

	if (drb_dev != NULL) {
		drb_info("already probed\n");
		return 0;
	}

	drb_dev = kzalloc(sizeof(struct drb_dev), GFP_KERNEL);
	if (drb_dev == NULL)
		return -ENOMEM;

	init_waitqueue_head(&drb_dev->wq);
	skb_queue_head_init(&drb_dev->sk_rx_q);

	drb_dev->miscdev.minor = MISC_DYNAMIC_MINOR;
	drb_dev->miscdev.name = "drb";
	drb_dev->miscdev.fops = &misc_io_fops;

	err = misc_register(&drb_dev->miscdev);
	if (err) {
		drb_err("misc_register fail\n");
		goto out;
	}

	drb_info("---\n");

	return 0;

out:
	drb_info("err = %d ---\n", err);
	return err;
}

static void __exit dev_ril_bridge_exit(void)
{
	drb_info("\n");
}

module_init(dev_ril_bridge_init);
module_exit(dev_ril_bridge_exit);

MODULE_DESCRIPTION("dev_ril_bridge driver");
MODULE_LICENSE("GPL");