summaryrefslogtreecommitdiffstats
path: root/drivers/platform/tegra/tegra-safety-dev.c
blob: 43be9c7a65ce9ac6027dd83d674102d1d8764736 (plain) (blame)
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/*
 * Copyright (c) 2016-2018, NVIDIA CORPORATION, All rights reserved.
 *
 * 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/cdev.h>
#include <linux/device.h>
#include <linux/fs.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/poll.h>
#include <linux/tegra-ivc.h>
#include <linux/tegra-ivc-instance.h>
#include <linux/wait.h>
#include <asm/ioctls.h>
#include <linux/uaccess.h>
#include <linux/version.h>
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 14, 0)
#include <linux/sched/signal.h>
#endif

#include <linux/tegra-safety-ivc.h>

#define CCIOGNFRAMES _IOR('c', 1, int)
#define CCIOGNBYTES _IOR('c', 2, int)

struct tegra_safety_dev_data {
	struct tegra_safety_ivc_chan *ivc_chan;
	struct cdev cdev;
	struct mutex io_lock;
	wait_queue_head_t read_waitq;
	wait_queue_head_t write_waitq;
	int open_count;
};

static struct tegra_safety_dev_data *safety_dev_data[MAX_SAFETY_CHANNELS];
static DEFINE_MUTEX(tegra_safety_dev_lock_open);
static int tegra_safety_dev_major_number;
static struct class *tegra_safety_dev_class;
extern int ivc_chan_count;

static inline struct tegra_safety_dev_data *get_file_to_devdata(struct file *fp)
{
	return ((struct tegra_safety_dev_data *)fp->private_data);
}

static inline struct ivc *get_file_to_ivc(struct file *fp)
{
	struct tegra_safety_dev_data *dev_data = get_file_to_devdata(fp);

	return &dev_data->ivc_chan->ivc;
}

static int tegra_safety_dev_open(struct inode *in, struct file *f)
{
	unsigned int minor = iminor(in);
	struct tegra_safety_dev_data *dev_data = safety_dev_data[minor];
	int ret = -1;

	if (minor >= ivc_chan_count)
		return -EBADFD;

	ret = mutex_lock_interruptible(&tegra_safety_dev_lock_open);
	if (ret)
		return ret;

	/* For CmdRsp restrict device open to 2 */
	if ((minor == 0) && (dev_data->open_count >= 2)) {
		ret = -1;
		goto error;
	}

	/* For HeartBeat restrict device open to 1 */
	if ((minor == 1) && (dev_data->open_count >= 1)) {
		ret = -1;
		goto error;
	}

	dev_data->open_count++;
	f->private_data = dev_data;
	nonseekable_open(in, f);

error:
	mutex_unlock(&tegra_safety_dev_lock_open);

	return ret;
}

static int tegra_safety_dev_release(struct inode *in, struct file *fp)
{
	unsigned int minor = iminor(in);
	struct tegra_safety_dev_data *dev_data = safety_dev_data[minor];

	mutex_lock(&tegra_safety_dev_lock_open);
	dev_data->open_count--;
	mutex_unlock(&tegra_safety_dev_lock_open);

	return 0;
}

static unsigned int tegra_safety_dev_poll(struct file *fp, poll_table *pt)
{
	struct tegra_safety_dev_data *dev_data = get_file_to_devdata(fp);
	struct ivc *ivc = get_file_to_ivc(fp);
	unsigned int ret = 0;

	poll_wait(fp, &dev_data->read_waitq, pt);
	poll_wait(fp, &dev_data->write_waitq, pt);

	mutex_lock(&dev_data->io_lock);
	if (tegra_ivc_can_read(ivc))
		ret |= (POLLIN | POLLRDNORM);
	if (tegra_ivc_can_write(ivc))
		ret |= (POLLOUT | POLLWRNORM);
	mutex_unlock(&dev_data->io_lock);

	return ret;
}

static ssize_t tegra_safety_dev_read(struct file *fp, char __user *buffer,
		size_t len, loff_t *offset)
{
	struct tegra_safety_dev_data *dev_data = get_file_to_devdata(fp);
	struct ivc *ivc = get_file_to_ivc(fp);
	DEFINE_WAIT(wait);
	size_t maxbytes = len > ivc->frame_size ? ivc->frame_size : len;
	ssize_t ret = 0;
	bool done = false;

	/*
	 * here we are reading maxbytes of data from IVC. If data is
	 * present we will read it, otherwise do the wait.
	 */
	while (!ret && maxbytes) {
		ret = mutex_lock_interruptible(&dev_data->io_lock);
		if (ret)
			return ret;
		prepare_to_wait(&dev_data->read_waitq, &wait,
				TASK_INTERRUPTIBLE);

		done = tegra_ivc_can_read(ivc);
		if (done)
			ret = tegra_ivc_read_user(ivc, buffer, maxbytes);
		mutex_unlock(&dev_data->io_lock);

		if (done)
			goto finish;
		else if (signal_pending(current))
			ret = -EINTR;
		else if (fp->f_flags & O_NONBLOCK)
			ret = -EAGAIN;
		else
			schedule();
finish:
		finish_wait(&dev_data->read_waitq, &wait);
	};

	return ret;
}

static ssize_t tegra_safety_dev_write(struct file *fp,
		const char __user *buffer, size_t len, loff_t *offset)
{
	struct tegra_safety_dev_data *dev_data = get_file_to_devdata(fp);
	struct ivc *ivc = get_file_to_ivc(fp);
	DEFINE_WAIT(wait);
	size_t maxbytes = len > ivc->frame_size ? ivc->frame_size : len;
	ssize_t ret = 0;
	int done = false;

	/*
	 * here we are writing maxbytes of data to IVC. If space is
	 * available we will write it, otherwise do the wait.
	 */
	while (!ret && maxbytes) {
		ret = mutex_lock_interruptible(&dev_data->io_lock);
		if (ret)
			return ret;
		prepare_to_wait(&dev_data->write_waitq, &wait,
				TASK_INTERRUPTIBLE);

		done = tegra_ivc_can_write(ivc);
		if (done)
			ret = tegra_ivc_write_user(ivc, buffer, maxbytes);
		mutex_unlock(&dev_data->io_lock);

		if (done)
			goto finish;
		else if (signal_pending(current))
			ret = -EINTR;
		else if (fp->f_flags & O_NONBLOCK)
			ret = -EAGAIN;
		else
			schedule();
finish:
		finish_wait(&dev_data->write_waitq, &wait);
	}

	return ret;
}

static long tegra_safety_dev_ioctl(struct file *fp, unsigned int cmd,
		unsigned long arg)
{
	struct tegra_safety_dev_data *dev_data = get_file_to_devdata(fp);
	struct ivc *ivc = get_file_to_ivc(fp);
	int val = 0;
	long ret;

	mutex_lock(&dev_data->io_lock);

	switch (cmd) {
	case CCIOGNFRAMES:
		val = ivc->nframes;
		ret = put_user(val, (int __user *)arg);
		break;
	case CCIOGNBYTES:
		val = ivc->frame_size;
		ret = put_user(val, (int __user *)arg);
		break;

	default:
		ret = -ENOTTY;
	}

	mutex_unlock(&dev_data->io_lock);

	return ret;
}

static const struct file_operations tegra_safety_dev_fops = {
	.open = tegra_safety_dev_open,
	.poll = tegra_safety_dev_poll,
	.read = tegra_safety_dev_read,
	.write = tegra_safety_dev_write,
	.release = tegra_safety_dev_release,
	.unlocked_ioctl = tegra_safety_dev_ioctl,
	.compat_ioctl = tegra_safety_dev_ioctl,
	.llseek = no_llseek,
};

void tegra_safety_dev_notify(void)
{
	struct tegra_safety_dev_data *dev_data;
	struct ivc *ivc;
	int can_read, can_write;
	int i;

	if (!safety_dev_data[0])
		return;

	for (i = 0; i < ivc_chan_count; i++) {
		dev_data = safety_dev_data[i];
		if (!dev_data)
			return;

		ivc = &dev_data->ivc_chan->ivc;

		mutex_lock(&dev_data->io_lock);
		can_read = tegra_ivc_can_read(ivc);
		can_write = tegra_ivc_can_write(ivc);
		mutex_unlock(&dev_data->io_lock);

		if (can_read)
			wake_up_interruptible(&dev_data->read_waitq);

		if (can_write)
			wake_up_interruptible(&dev_data->write_waitq);
	}
}

void tegra_safety_class_exit(struct device *dev)
{
	dev_t num = MKDEV(tegra_safety_dev_major_number, 0);

	if (!tegra_safety_dev_class)
		return;

	class_destroy(tegra_safety_dev_class);
	unregister_chrdev_region(num, ivc_chan_count);
	tegra_safety_dev_class = NULL;
}

int tegra_safety_class_init(struct device *dev)
{
	dev_t start;
	int ret;

	ret = alloc_chrdev_region(&start, 0, ivc_chan_count, "safety");
	if (ret) {
		dev_alert(dev, "safety: failed to allocate device numbers\n");
		goto error;
	}
	tegra_safety_dev_major_number = MAJOR(start);

	tegra_safety_dev_class = class_create(THIS_MODULE, "safety_class");
	if (IS_ERR(tegra_safety_dev_class)) {
		dev_alert(dev, "safety: failed to create class\n");
		ret = PTR_ERR(tegra_safety_dev_class);
		goto error;
	}

	return 0;

error:
	tegra_safety_class_exit(dev);

	return ret;
}

int tegra_safety_dev_init(struct device *dev, int index)
{
	struct tegra_safety_ivc *safety_ivc = dev_get_drvdata(dev);
	struct tegra_safety_dev_data *dev_data;
	struct device *char_dev;
	dev_t num;
	int ret;

	if (!tegra_safety_dev_class) {
		ret = tegra_safety_class_init(dev);
		if (ret) {
			dev_err(dev, "safety: class init failed\n");
			goto error;
		}
	}

	dev_data = devm_kzalloc(dev, sizeof(*dev_data), GFP_KERNEL);
	if (!dev_data) {
		dev_alert(dev, "safety: failed to allocate memory\n");
		ret = -ENOMEM;
		goto error;
	}

	cdev_init(&dev_data->cdev, &tegra_safety_dev_fops);
	dev_data->cdev.owner = THIS_MODULE;
	init_waitqueue_head(&dev_data->read_waitq);
	init_waitqueue_head(&dev_data->write_waitq);
	mutex_init(&dev_data->io_lock);

	dev_data->ivc_chan = safety_ivc->ivc_chan[index];
	safety_dev_data[index] = dev_data;
	num = MKDEV(tegra_safety_dev_major_number, index);

	ret = cdev_add(&dev_data->cdev, num, 1);
	if (ret) {
		dev_err(dev, "safety: unable to add character device\n");
		goto error;
	}

	char_dev = device_create(tegra_safety_dev_class, dev, num,
			NULL, "safety%d", index);
	if (IS_ERR(char_dev)) {
		dev_err(dev, "safety: could not create device\n");
		ret = PTR_ERR(char_dev);
		goto error;
	}

	dev_info(dev, "safety: character device %d registered\n", index);

	return ret;

error:
	tegra_safety_dev_exit(dev, index);
	tegra_safety_class_exit(dev);

	return ret;
}

void tegra_safety_dev_exit(struct device *dev, int index)
{
	struct tegra_safety_dev_data *dev_data = safety_dev_data[index];
	dev_t num = MKDEV(tegra_safety_dev_major_number, index);

	if (!dev_data)
		return;

	device_destroy(tegra_safety_dev_class, num);
	cdev_del(&dev_data->cdev);

	safety_dev_data[index] = NULL;

	if (index == (ivc_chan_count-1))
		tegra_safety_class_exit(dev);

	dev_info(dev, "safety: character device %d unregistered\n", index);
}