summaryrefslogtreecommitdiff
path: root/drivers/video/tegra/dc/ext/events.c
blob: e16a00963e697d5968bd7276d451ef923b2e0512 (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
/*
 * drivers/video/tegra/dc/ext/events.c
 *
 * Copyright (c) 2011-2013, NVIDIA CORPORATION, All rights reserved.
 *
 * Author: Robert Morell <rmorell@nvidia.com>
 *
 * 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/err.h>
#include <linux/fs.h>
#include <linux/list.h>
#include <linux/poll.h>
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/uaccess.h>

#include "tegra_dc_ext_priv.h"

static DECLARE_WAIT_QUEUE_HEAD(event_wait);

unsigned int tegra_dc_ext_event_poll(struct file *filp, poll_table *wait)
{
	struct tegra_dc_ext_control_user *user = filp->private_data;
	unsigned int mask = 0;

	poll_wait(filp, &event_wait, wait);

	if (atomic_read(&user->num_events))
		mask |= POLLIN;

	return mask;
}

static int get_next_event(struct tegra_dc_ext_control_user *user,
			  struct tegra_dc_ext_event_list *event,
			  bool block)
{
	struct list_head *list = &user->event_list;
	struct tegra_dc_ext_event_list *next_event;
	int ret;

	if (block) {
		ret = wait_event_interruptible(event_wait,
			atomic_read(&user->num_events));

		if (unlikely(ret)) {
			if (ret == -ERESTARTSYS)
				return -EAGAIN;
			return ret;
		}
	} else {
		if (!atomic_read(&user->num_events))
			return 0;
	}

	mutex_lock(&user->lock);

	BUG_ON(list_empty(list));
	next_event = list_first_entry(list, struct tegra_dc_ext_event_list,
			list);
	*event = *next_event;
	list_del(&next_event->list);
	kfree(next_event);

	atomic_dec(&user->num_events);

	mutex_unlock(&user->lock);

	return 1;
}

ssize_t tegra_dc_ext_event_read(struct file *filp, char __user *buf,
				size_t size, loff_t *ppos)
{
	struct tegra_dc_ext_control_user *user = filp->private_data;
	struct tegra_dc_ext_event_list event_elem;
	struct tegra_dc_ext_event *event = &event_elem.event;
	ssize_t retval = 0, to_copy, event_size, pending;
	loff_t previously_copied = 0;
	char *to_copy_ptr;

	if (size == 0)
		return 0;

	if (user->partial_copy) {
		/*
		 * We didn't transfer the entire event last time, need to
		 * finish it up
		 */
		event_elem = user->event_to_copy;
		previously_copied = user->partial_copy;
	} else {
		/* Get the next event, if any */
		pending = get_next_event(user, &event_elem,
			!(filp->f_flags & O_NONBLOCK));
		if (pending <= 0)
			return pending;
	}

	/* Write the event to the user */
	event_size = sizeof(*event) + event->data_size;
	BUG_ON(event_size <= previously_copied);
	event_size -= previously_copied;

	to_copy_ptr = (char *)event + previously_copied;
	to_copy = min_t(ssize_t, size, event_size);
	if (copy_to_user(buf, to_copy_ptr, to_copy)) {
		retval = -EFAULT;
		to_copy = 0;
	}

	/* Note that we currently only deliver one event at a time */

	if (event_size > to_copy) {
		/*
		 * We were only able to copy part of this event.  Stash it for
		 * next time.
		 */
		user->event_to_copy = event_elem;
		user->partial_copy = previously_copied + to_copy;
	} else {
		user->partial_copy = 0;
	}

	return to_copy ? to_copy : retval;
}

static int tegra_dc_ext_queue_event(struct tegra_dc_ext_control *control,
				    struct tegra_dc_ext_event *event)
{
	struct list_head *cur;
	int retval = 0;

	mutex_lock(&control->lock);
	list_for_each(cur, &control->users) {
		struct tegra_dc_ext_control_user *user;
		struct tegra_dc_ext_event_list *ev_list;

		user = container_of(cur, struct tegra_dc_ext_control_user,
			list);
		mutex_lock(&user->lock);

		if (!(user->event_mask & event->type)) {
			mutex_unlock(&user->lock);
			continue;
		}

		ev_list = kmalloc(sizeof(*ev_list), GFP_KERNEL);
		if (!ev_list) {
			retval = -ENOMEM;
			mutex_unlock(&user->lock);
			continue;
		}

		memcpy(&ev_list->event, event,
			sizeof(*event) + event->data_size);

		list_add_tail(&ev_list->list, &user->event_list);

		atomic_inc(&user->num_events);

		mutex_unlock(&user->lock);
	}
	mutex_unlock(&control->lock);

	/* Is it worth it to track waiters with more granularity? */
	wake_up(&event_wait);

	return retval;
}

int tegra_dc_ext_queue_hotplug(struct tegra_dc_ext_control *control, int output)
{
	struct {
		struct tegra_dc_ext_event event;
		struct tegra_dc_ext_control_event_hotplug hotplug;
	} __packed pack;

	pack.event.type = TEGRA_DC_EXT_EVENT_HOTPLUG;
	pack.event.data_size = sizeof(pack.hotplug);

	pack.hotplug.handle = output;

	tegra_dc_ext_queue_event(control, &pack.event);

	return 0;
}

int tegra_dc_ext_queue_bandwidth_renegotiate(
		struct tegra_dc_ext_control *control, int output,
		struct tegra_dc_bw_data *data)
{
	struct {
		struct tegra_dc_ext_event event;
		struct tegra_dc_ext_control_event_bandwidth bandwidth;
	} __packed pack;

	if (data == NULL)
		pack.event.type = TEGRA_DC_EXT_EVENT_BANDWIDTH_DEC;
	else {
		if (data->avail_bw > data->resvd_bw)
			pack.event.type = TEGRA_DC_EXT_EVENT_BANDWIDTH_INC;
		else
			pack.event.type = TEGRA_DC_EXT_EVENT_BANDWIDTH_DEC;

		pack.bandwidth.total_bw = data->total_bw;
		pack.bandwidth.avail_bw = data->avail_bw;
		pack.bandwidth.resvd_bw = data->resvd_bw;
	}

	pack.event.data_size = sizeof(pack.bandwidth);

	pack.bandwidth.handle = output;

	tegra_dc_ext_queue_event(control, &pack.event);

	return 0;
}