summaryrefslogtreecommitdiff
path: root/drivers/maru/sensors/maru_pedo.c
blob: ec5efcf18713a615af899499552b773883e57c2b (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
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
/*
 * Maru Virtio Pedometer Sensor Device Driver
 *
 * Copyright (c) 2016 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Contact:
 *  Sooyoung Ha <yoosah.ha@samsung.com>
 *  Jinhyung Choi <jinh0.choi@samsung.com>
 *  SeokYeon Hwang <syeon.hwang@samsung.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.
 *
 * 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 * Contributors:
 * - S-Core Co., Ltd
 *
 */

#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/cdev.h>
#include <linux/uaccess.h>

#include "maru_virtio_sensor.h"

struct maru_pedo_data {
	struct input_dev *input_data;
	struct delayed_work work;

	struct virtio_sensor *vs;

	atomic_t pedo;
	atomic_t enable;
	atomic_t poll_delay;
};

static struct class *pedo_class;
dev_t pedo_devno;
int pedo_major = 0;
struct maru_pedo_data *gdata = NULL;

#define PEDO_MSG_LENGTH 18
#define PEDO_CMD_LENGTH 5
#define SHUB_LIB_PEDOMETER 3
#define SHUB_INST_LIB_ADD ((char)-79)
#define SHUB_INST_LIB_REMOVE ((char)-78)

static char STOP_MSG[] = { 1, 1, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
static char WALK_SLOW_MSG[] = { 1, 1, 3, 1, 0, 0, 0, 1, 0, 56, 30, 3, 3, 15, 0, 0, 0, 0};
static char WALK_MSG[] = { 1, 1, 3, 1, 0, 0, 0, 1, 0, 80, 60, 3, 3, 20, 0, 0, 0, 0};
static char RUN_SLOW_MSG[] = { 1, 1, 3, 0, 1, 0, 0, 1, 0, 110, 120, 4, 8, 30, 0, 0, 0, 0};
static char RUN_MSG[] = { 1, 1, 3, 0, 1, 0, 0, 1, 0, 120, 0xA0, 4, 8, 37, 0, 0, 0, 0};

static char PEDO_ENABLE_CMD[] = {SHUB_INST_LIB_ADD, SHUB_LIB_PEDOMETER, 0, 0, 0};
static char PEDO_DISABLE_CMD[] = {SHUB_INST_LIB_REMOVE, SHUB_LIB_PEDOMETER, 0, 0};

static void maru_pedo_input_work_func(struct work_struct *work)
{
	int pedo = 0;
	int poll_time = 0;
	int enable = -1;
	int ret = 0;
	char sensor_data[__MAX_BUF_SENSOR];
	struct maru_pedo_data *data = container_of((struct delayed_work *)work,
			struct maru_pedo_data, work);

	LOG(1, "maru_pedo_input_work_func starts");

	memset(sensor_data, 0, __MAX_BUF_SENSOR);

	poll_time = atomic_read(&data->poll_delay);
	enable = atomic_read(&data->enable);

	if (enable) {
		mutex_lock(&data->vs->vqlock);
		ret = get_sensor_data(sensor_type_pedo, sensor_data);
		mutex_unlock(&data->vs->vqlock);
		if (!ret) {
			sscanf(sensor_data, "%d", &pedo);
			atomic_set(&data->pedo, pedo);
			LOG(1, "pedo_set %d", pedo);

			input_report_rel(data->input_data, REL_RX, 3);
			input_sync(data->input_data);
		}
	}

	enable = atomic_read(&data->enable);

	if (enable) {
		if (poll_time > 0) {
			schedule_delayed_work(&data->work, nsecs_to_jiffies(poll_time));
		} else {
			schedule_delayed_work(&data->work, 0);
		}
	}
}

static ssize_t maru_enable_show(struct device *dev, struct device_attribute *attr, char *buf)
{
	char sensor_data[__MAX_BUF_SENSOR];
	int ret;
	struct input_dev *input_data = to_input_dev(dev);
	struct maru_pedo_data *data = input_get_drvdata(input_data);

	memset(sensor_data, 0, __MAX_BUF_SENSOR);
	mutex_lock(&data->vs->vqlock);
	ret = get_sensor_data(sensor_type_pedo_enable, sensor_data);
	mutex_unlock(&data->vs->vqlock);
	if (ret)
		return sprintf(buf, "%d", -1);

	return sprintf(buf, "%s", sensor_data);
}

static ssize_t maru_enable_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
{
	struct input_dev *input_data = to_input_dev(dev);
	struct maru_pedo_data *data = input_get_drvdata(input_data);
	int value = simple_strtoul(buf, NULL, 10);

	if (value != 0 && value != 1)
		return count;

	mutex_lock(&data->vs->vqlock);
	set_sensor_data(sensor_type_pedo_enable, buf);
	mutex_unlock(&data->vs->vqlock);

	if (value) {
		if (atomic_read(&data->enable) != 1) {
			atomic_set(&data->enable, 1);
			schedule_delayed_work(&data->work, 0);

		}
	} else {
		if (atomic_read(&data->enable) != 0) {
			atomic_set(&data->enable, 0);
			cancel_delayed_work(&data->work);
		}
	}

	return strnlen(buf, __MAX_BUF_SENSOR);
}

static ssize_t maru_poll_delay_show(struct device *dev, struct device_attribute *attr, char *buf)
{
	char sensor_data[__MAX_BUF_SENSOR];
	int ret;
	struct input_dev *input_data = to_input_dev(dev);
	struct maru_pedo_data *data = input_get_drvdata(input_data);

	memset(sensor_data, 0, __MAX_BUF_SENSOR);
	mutex_lock(&data->vs->vqlock);
	ret = get_sensor_data(sensor_type_pedo_delay, sensor_data);
	mutex_unlock(&data->vs->vqlock);
	if (ret)
		return sprintf(buf, "%d", -1);

	return sprintf(buf, "%s", sensor_data);
}

static ssize_t maru_poll_delay_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
{
	struct input_dev *input_data = to_input_dev(dev);
	struct maru_pedo_data *data = input_get_drvdata(input_data);
	int value = simple_strtoul(buf, NULL, 10);

	if (value < __MIN_DELAY_SENSOR)
		return count;

	mutex_lock(&data->vs->vqlock);
	set_sensor_data(sensor_type_pedo_delay, buf);
	mutex_unlock(&data->vs->vqlock);
	atomic_set(&data->poll_delay, value);

	return strnlen(buf, __MAX_BUF_SENSOR);
}

static struct device_attribute attr_pedo[] = {
	MARU_ATTR_RW(enable),
	MARU_ATTR_RW(poll_delay),
};

static struct attribute *maru_pedo_attribute[] = {
	&attr_pedo[0].attr,
	&attr_pedo[1].attr,
	NULL
};

static struct attribute_group maru_pedo_attribute_group = {
	.attrs = maru_pedo_attribute
};

static void pedo_clear(struct maru_pedo_data *data)
{
	if (data == NULL)
		return;

	if (data->input_data) {
		sysfs_remove_group(&data->input_data->dev.kobj,
			&maru_pedo_attribute_group);
		input_free_device(data->input_data);
	}

	kfree(data);
	data = NULL;
}

static int set_initial_value(struct maru_pedo_data *data)
{
	int delay = 0;
	int ret = 0;
	int enable = 0;
	char sensor_data[__MAX_BUF_SENSOR];

	memset(sensor_data, 0, __MAX_BUF_SENSOR);

	mutex_lock(&data->vs->vqlock);
	ret = get_sensor_data(sensor_type_pedo_delay, sensor_data);
	mutex_unlock(&data->vs->vqlock);
	if (ret) {
		ERR("failed to get initial delay time");
		return ret;
	}

	delay = sensor_atoi(sensor_data);

	mutex_lock(&data->vs->vqlock);
	ret = get_sensor_data(sensor_type_pedo_enable, sensor_data);
	mutex_unlock(&data->vs->vqlock);
	if (ret) {
		ERR("failed to get initial enable");
		return ret;
	}

	enable = sensor_atoi(sensor_data);

	if (delay < 0) {
		ERR("weird value is set initial delay");
		return ret;
	}

	atomic_set(&data->poll_delay, delay);

	if (enable) {
		atomic_set(&data->enable, 1);
		schedule_delayed_work(&data->work, 0);
	}

	return ret;
}

static int create_input_device(struct maru_pedo_data *data)
{
	int ret = 0;
	struct input_dev *input_data = NULL;

	input_data = input_allocate_device();
	if (input_data == NULL) {
		ERR("failed initialing input handler");
		pedo_clear(data);
		return -ENOMEM;
	}

	input_data->name = SENSOR_PEDO_INPUT_NAME;
	input_data->id.bustype = BUS_I2C;

	set_bit(EV_REL, input_data->evbit);
	set_bit(EV_SYN, input_data->evbit);
	input_set_capability(input_data, EV_REL, REL_RX);

	data->input_data = input_data;

	ret = input_register_device(input_data);
	if (ret) {
		ERR("failed to register input data");
		pedo_clear(data);
		return ret;
	}

	input_set_drvdata(input_data, data);

	ret = sysfs_create_group(&input_data->dev.kobj,
			&maru_pedo_attribute_group);
	if (ret) {
		pedo_clear(data);
		ERR("failed initialing devices");
		return ret;
	}

	return ret;
}

static int pedo_open(struct inode *inode, struct file *filp)
{
	filp->private_data = inode->i_cdev;
	LOG(1, "pedometer is opened");
	return 0;
}

static int pedo_close(struct inode *inode, struct file *filp)
{
	LOG(1, "pedometer is closed");
	return 0;
}

static ssize_t pedo_read(struct file *filp, char __user *ubuf, size_t len,
		loff_t *f_pos)
{
	unsigned long ret = 0;
	int pedo = 0;
	LOG(1, "try to read pedometer node");

	pedo = atomic_read(&gdata->pedo);
	LOG(1, "selected pedometer state is %d", pedo);

	switch (pedo) {
	case 1:
		ret = copy_to_user(ubuf, STOP_MSG, PEDO_MSG_LENGTH);
		break;
	case 2:
		ret = copy_to_user(ubuf, WALK_SLOW_MSG, PEDO_MSG_LENGTH);
		break;
	case 3:
		ret = copy_to_user(ubuf, WALK_MSG, PEDO_MSG_LENGTH);
		break;
	case 4:
		ret = copy_to_user(ubuf, RUN_SLOW_MSG, PEDO_MSG_LENGTH);
		break;
	case 5:
		ret = copy_to_user(ubuf, RUN_MSG, PEDO_MSG_LENGTH);
		break;
	default:
		ERR("invalid case pedometer state %d!!", pedo);
		return -EFAULT;
	}
	if (ret) {
		ERR("pedometer copy_to_user failed, errno: %lu", ret);
		return -EFAULT;
	} else {
		LOG(1, "pedometer copy_to_user success.");
		return PEDO_MSG_LENGTH;
	}
}

static ssize_t pedo_write(struct file *filp, const char __user *ubuf, size_t len,
		loff_t *f_pos)
{
	unsigned long ret = 0;
	char cmd[PEDO_CMD_LENGTH] = {'\0', };
	char buf[2] = {'\0', };
	int value = -1;

	LOG(1, "try to write pedometer node %zi", len);

	ret = copy_from_user(cmd, ubuf, len);
	if (ret) {
		ERR("pedometer copy_to_user failed, errno: %lu", ret);
		return -EFAULT;
	}

	if (!memcmp(cmd, PEDO_ENABLE_CMD, 2)) {
		LOG(1, "enable pedometer");
		value = 1;
	} else if (!memcmp(cmd, PEDO_DISABLE_CMD, 2)) {
		LOG(1, "disable pedometer");
		value = 0;
	} else {
		ERR("invalid command %s", cmd);
		return -EFAULT;
	}
	sprintf(buf, "%d", value);
	mutex_lock(&gdata->vs->vqlock);
	set_sensor_data(sensor_type_pedo_enable, buf);
	mutex_unlock(&gdata->vs->vqlock);

	if (value) {
		if (atomic_read(&gdata->enable) != 1) {
			atomic_set(&gdata->enable, 1);
			schedule_delayed_work(&gdata->work, 0);

		}
	} else {
		if (atomic_read(&gdata->enable) != 0) {
			atomic_set(&gdata->enable, 0);
			cancel_delayed_work(&gdata->work);
		}
	}
	return 1;
}

struct file_operations pedo_fops = {
	.owner =	THIS_MODULE,
	.read =		pedo_read,
	.write =	pedo_write,
	.open =		pedo_open,
	.release =	pedo_close,
};

static char *pedo_devnode(struct device *dev, umode_t *mode)
{
	return kasprintf(GFP_KERNEL, "%s", dev_name(dev));
}

int maru_pedo_init(struct virtio_sensor *vs)
{
	int ret = 0;
	int err = 0;
	dev_t dev = 0;
	struct cdev *cdev = NULL;

	INFO("maru_pedo device init starts.");

	cdev = kzalloc(sizeof(struct cdev), GFP_KERNEL);
	if (cdev == NULL) {
		ERR("failed to create pedo cdev.");
		return -ENOMEM;
	}

	gdata = kzalloc(sizeof(struct maru_pedo_data), GFP_KERNEL);
	if (gdata == NULL) {
		ERR("failed to create pedo data.");
		return -ENOMEM;
	}

	vs->pedo_handle = gdata;
	gdata->vs = vs;

	INIT_DELAYED_WORK(&gdata->work, maru_pedo_input_work_func);
	ret = alloc_chrdev_region(&dev, 0, 1, DRIVER_PEDO_NAME);
	if (ret < 0) {
		ERR("Unable to get pedo region, error %d", ret);
		return -1;
	}

	pedo_major = MAJOR(dev);
	LOG(1, "pedo device major num = %d\n", pedo_major);
	pedo_devno = dev;

	pedo_class = class_create(THIS_MODULE, DRIVER_PEDO_NAME);

	if (IS_ERR(pedo_class)) {
		ERR("Unable to create pedo_class, error %d", ret);
		unregister_chrdev_region(dev, 1);
		return PTR_ERR(pedo_class);
	}
	pedo_class->devnode = pedo_devnode;

	cdev_init(cdev, &pedo_fops);
	cdev->owner = THIS_MODULE;
	err = cdev_add(cdev, MKDEV(pedo_major, 0), 1);
	if (err) {
		ERR("unable to add cdev with error %d for pedo device\n", err);
	}

	if (IS_ERR(device_create(pedo_class, NULL, MKDEV(pedo_major, 0), NULL,
		kasprintf(GFP_KERNEL, "%s", DRIVER_PEDO_NAME)))) {
		ERR("failed to create pedo device");
		pedo_clear(gdata);
		return -1;
	}

	ret = create_input_device(gdata);
	if (ret) {
		ERR("failed to create input device");
		return ret;
	}

	ret = set_initial_value(gdata);
	if (ret) {
		ERR("failed to set initial value");
		return ret;
	}

	INFO("maru_pedo device init ends.");

	return ret;
}

int maru_pedo_exit(struct virtio_sensor *vs)
{
	struct maru_pedo_data *data = NULL;

	data = (struct maru_pedo_data *)vs->pedo_handle;
	pedo_clear(data);
	INFO("maru_pedo device exit ends.");
	return 0;
}