aboutsummaryrefslogtreecommitdiff
path: root/drivers/misc/stm401/stm401_queue.c
blob: 0817e4af540799f5f3812af8a38485c366eae500 (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
/*
 * Copyright (C) 2010-2013 Motorola Mobility LLC
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
 * 02111-1307, USA
 */

#include <linux/cdev.h>
#include <linux/delay.h>
#include <linux/err.h>
#include <linux/errno.h>
#include <linux/export.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <linux/fs.h>
#include <linux/gpio.h>
#include <linux/i2c.h>
#include <linux/input.h>
#include <linux/input-polldev.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/miscdevice.h>
#include <linux/module.h>
#include <linux/poll.h>
#include <linux/regulator/consumer.h>
#include <linux/slab.h>
#include <linux/switch.h>
#include <linux/time.h>
#include <linux/uaccess.h>
#include <linux/wakelock.h>
#include <linux/workqueue.h>

#include <linux/stm401.h>


int stm401_as_data_buffer_write(struct stm401_data *ps_stm401,
	unsigned char type, unsigned char *data, int size,
	unsigned char status)
{
	int new_head;
	struct stm401_android_sensor_data *buffer;
	struct timespec ts;
	static bool error_reported;

	new_head = (ps_stm401->stm401_as_data_buffer_head + 1)
		& STM401_AS_DATA_QUEUE_MASK;
	if (new_head == ps_stm401->stm401_as_data_buffer_tail) {
		if (!error_reported) {
			dev_err(&ps_stm401->client->dev, "as data buffer full\n");
			error_reported = true;
		}
		wake_up(&ps_stm401->stm401_as_data_wq);
		return 0;
	}
	buffer = &(ps_stm401->stm401_as_data_buffer[new_head]);
	buffer->type = type;
	buffer->status = status;
	if (data != NULL && size > 0) {
		if (size > sizeof(buffer->data)) {
			dev_err(&ps_stm401->client->dev,
				"size %d exceeds as buffer\n", size);
			return 0;
		}
		memcpy(buffer->data, data, size);
	}
	buffer->size = size;

	ktime_get_ts(&ts);
	buffer->timestamp = ts.tv_sec*1000000000LL + ts.tv_nsec;

	ps_stm401->stm401_as_data_buffer_head = new_head;
	wake_up(&ps_stm401->stm401_as_data_wq);

	error_reported = false;
	return 1;
}

int stm401_as_data_buffer_read(struct stm401_data *ps_stm401,
	struct stm401_android_sensor_data *buff)
{
	int new_tail;

	if (ps_stm401->stm401_as_data_buffer_tail
		== ps_stm401->stm401_as_data_buffer_head)
		return 0;

	new_tail = (ps_stm401->stm401_as_data_buffer_tail + 1)
		& STM401_AS_DATA_QUEUE_MASK;

	*buff = ps_stm401->stm401_as_data_buffer[new_tail];

	ps_stm401->stm401_as_data_buffer_tail = new_tail;

	return 1;
}

int stm401_ms_data_buffer_write(struct stm401_data *ps_stm401,
	unsigned char type, unsigned char *data, int size)
{
	int new_head;
	struct stm401_moto_sensor_data *buffer;
	struct timespec ts;
	static bool error_reported;

	new_head = (ps_stm401->stm401_ms_data_buffer_head + 1)
		& STM401_MS_DATA_QUEUE_MASK;
	if (new_head == ps_stm401->stm401_ms_data_buffer_tail) {
		if (!error_reported) {
			dev_err(&ps_stm401->client->dev, "ms data buffer full\n");
			error_reported = true;
		}
		wake_up(&ps_stm401->stm401_ms_data_wq);
		return 0;
	}
	buffer = &(ps_stm401->stm401_ms_data_buffer[new_head]);
	buffer->type = type;
	if (data != NULL && size > 0) {
		if (size > sizeof(buffer->data)) {
			dev_err(&ps_stm401->client->dev,
				"size %d exceeds ms buffer\n", size);
			return 0;
		}
		memcpy(buffer->data, data, size);
	}
	buffer->size = size;

	ktime_get_ts(&ts);
	buffer->timestamp
		= ts.tv_sec*1000000000LL + ts.tv_nsec;

	ps_stm401->stm401_ms_data_buffer_head = new_head;
	wake_up(&ps_stm401->stm401_ms_data_wq);

	error_reported = false;
	return 1;
}

int stm401_ms_data_buffer_read(struct stm401_data *ps_stm401,
	struct stm401_moto_sensor_data *buff)
{
	int new_tail;

	if (ps_stm401->stm401_ms_data_buffer_tail
		== ps_stm401->stm401_ms_data_buffer_head)
		return 0;

	new_tail = (ps_stm401->stm401_ms_data_buffer_tail + 1)
		& STM401_MS_DATA_QUEUE_MASK;

	*buff = ps_stm401->stm401_ms_data_buffer[new_tail];

	ps_stm401->stm401_ms_data_buffer_tail = new_tail;

	return 1;
}

static int stm401_as_open(struct inode *inode, struct file *file)
{
	int err = 0;

	dev_dbg(&stm401_misc_data->client->dev, "stm401_as_open\n");

	err = nonseekable_open(inode, file);
	if (err < 0)
		return err;
	file->private_data = stm401_misc_data;

	return err;
}

static ssize_t stm401_as_read(struct file *file, char __user *buffer,
	size_t size, loff_t *ppos)
{
	int ret;
	struct stm401_android_sensor_data tmp_buff;
	struct stm401_data *ps_stm401 = file->private_data;

	ret = stm401_as_data_buffer_read(ps_stm401, &tmp_buff);
	if (ret == 0)
		return 0;
	ret = copy_to_user(buffer, &tmp_buff,
		sizeof(struct stm401_android_sensor_data));
	if (ret != 0) {
		dev_err(&ps_stm401->client->dev, "Copy error\n");
		return 0;
	}

	return sizeof(struct stm401_android_sensor_data);
}

static unsigned int stm401_as_poll(struct file *file,
	struct poll_table_struct *wait)
{
	unsigned int mask = 0;
	struct stm401_data *ps_stm401 = file->private_data;

	poll_wait(file, &ps_stm401->stm401_as_data_wq, wait);
	if (ps_stm401->stm401_as_data_buffer_head
		!= ps_stm401->stm401_as_data_buffer_tail)
		mask = POLLIN | POLLRDNORM;

	return mask;
}

const struct file_operations stm401_as_fops = {
	.owner = THIS_MODULE,
	.open = stm401_as_open,
	.read = stm401_as_read,
	.poll = stm401_as_poll,
};

static int stm401_ms_open(struct inode *inode, struct file *file)
{
	int err = 0;

	dev_dbg(&stm401_misc_data->client->dev, "stm401_ms_open\n");

	err = nonseekable_open(inode, file);
	if (err < 0)
		return err;
	file->private_data = stm401_misc_data;

	return err;
}

static ssize_t stm401_ms_read(struct file *file, char __user *buffer,
	size_t size, loff_t *ppos)
{
	int ret;
	struct stm401_moto_sensor_data tmp_buff;
	struct stm401_data *ps_stm401 = file->private_data;

	ret = stm401_ms_data_buffer_read(ps_stm401, &tmp_buff);
	if (copy_to_user(buffer, &tmp_buff,
		sizeof(struct stm401_moto_sensor_data))
		!= 0) {
		dev_err(&ps_stm401->client->dev, "Copy error\n");
		ret = 0;
	}

	return ret;
}

static unsigned int stm401_ms_poll(struct file *file,
	struct poll_table_struct *wait)
{
	unsigned int mask = 0;
	struct stm401_data *ps_stm401 = file->private_data;
	poll_wait(file, &ps_stm401->stm401_ms_data_wq, wait);
	if (ps_stm401->stm401_ms_data_buffer_head
		!= ps_stm401->stm401_ms_data_buffer_tail)
		mask = POLLIN | POLLRDNORM;
	return mask;
}

const struct file_operations stm401_ms_fops = {
	.owner = THIS_MODULE,
	.open = stm401_ms_open,
	.read = stm401_ms_read,
	.poll = stm401_ms_poll,
};