summaryrefstree
path: root/kernel/drivers/input/touchscreen/synaptics_dsx/rmi_db.c
blob: e8d9c1cbba8e436088f06dceaeac8e90c1d166e3 (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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
/* Synaptics Register Mapped Interface (RMI4) I2C Physical Layer Driver.
 * Copyright (c) 2007-2012, Synaptics Incorporated
 *
 * 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/kernel.h>
#include <linux/module.h>
#include <asm/unaligned.h>
#include <asm/siginfo.h>
#include <mach/cpufreq.h>
#include <linux/slab.h>
#include <linux/i2c.h>
#include <linux/interrupt.h>
#include <linux/delay.h>
#include <linux/input.h>
#include <linux/gpio.h>
#include <linux/uaccess.h>
#include <linux/signal.h>
#include <linux/i2c/synaptics_rmi.h>
#include "synaptics_i2c_rmi.h"

#define REG_ADDR_LIMIT 0xFFFF

static ssize_t rmidb_sysfs_data_show(struct file *data_file,
		struct kobject *kobj, struct bin_attribute *attributes,
		char *buf, loff_t pos, size_t count);

static ssize_t rmidb_sysfs_data_store(struct file *data_file,
		struct kobject *kobj, struct bin_attribute *attributes,
		char *buf, loff_t pos, size_t count);

static ssize_t rmidb_sysfs_pid_show(struct device *dev,
		struct device_attribute *attr, char *buf);

static ssize_t rmidb_sysfs_pid_store(struct device *dev,
		struct device_attribute *attr, const char *buf, size_t count);

static ssize_t rmidb_sysfs_term_store(struct device *dev,
		struct device_attribute *attr, const char *buf, size_t count);

static ssize_t rmidb_sysfs_address_show(struct device *dev,
		struct device_attribute *attr, char *buf);

static ssize_t rmidb_sysfs_address_store(struct device *dev,
		struct device_attribute *attr, const char *buf, size_t count);

static ssize_t rmidb_sysfs_length_show(struct device *dev,
		struct device_attribute *attr, char *buf);

static ssize_t rmidb_sysfs_length_store(struct device *dev,
		struct device_attribute *attr, const char *buf, size_t count);

static ssize_t rmidb_sysfs_query_base_addr_show(struct device *dev,
		struct device_attribute *attr, char *buf);

static ssize_t rmidb_sysfs_control_base_addr_show(struct device *dev,
		struct device_attribute *attr, char *buf);

static ssize_t rmidb_sysfs_data_base_addr_show(struct device *dev,
		struct device_attribute *attr, char *buf);

static ssize_t rmidb_sysfs_command_base_addr_show(struct device *dev,
		struct device_attribute *attr, char *buf);

struct rmidb_handle {
	pid_t pid;
	unsigned char intr_mask;
	unsigned char intr_reg_num;
	unsigned short query_base_addr;
	unsigned short control_base_addr;
	unsigned short data_base_addr;
	unsigned short command_base_addr;
	unsigned short address;
	unsigned int length;
	struct siginfo interrupt_signal;
	struct siginfo terminate_signal;
	struct task_struct *task;
	struct synaptics_rmi4_data *rmi4_data;
	struct synaptics_rmi4_exp_fn_ptr *fn_ptr;
	struct kobject *sysfs_dir;
};

static struct bin_attribute attr_data = {
	.attr = {
		.name = "data",
		.mode = (S_IRUGO | S_IWUSR | S_IWGRP),
	},
	.size = 0,
	.read = rmidb_sysfs_data_show,
	.write = rmidb_sysfs_data_store,
};

static struct device_attribute attrs[] = {
	__ATTR(pid, S_IRUGO | S_IWUSR | S_IWGRP,
			rmidb_sysfs_pid_show,
			rmidb_sysfs_pid_store),
	__ATTR(term, S_IWUSR | S_IWGRP,
			synaptics_rmi4_show_error,
			rmidb_sysfs_term_store),
	__ATTR(address, S_IRUGO | S_IWUSR | S_IWGRP,
			rmidb_sysfs_address_show,
			rmidb_sysfs_address_store),
	__ATTR(length, S_IRUGO | S_IWUSR | S_IWGRP,
			rmidb_sysfs_length_show,
			rmidb_sysfs_length_store),
	__ATTR(query_base_addr, S_IRUGO,
			rmidb_sysfs_query_base_addr_show,
			synaptics_rmi4_store_error),
	__ATTR(control_base_addr, S_IRUGO,
			rmidb_sysfs_control_base_addr_show,
			synaptics_rmi4_store_error),
	__ATTR(data_base_addr, S_IRUGO,
			rmidb_sysfs_data_base_addr_show,
			synaptics_rmi4_store_error),
	__ATTR(command_base_addr, S_IRUGO,
			rmidb_sysfs_command_base_addr_show,
			synaptics_rmi4_store_error),
};

static struct rmidb_handle *rmidb;

static ssize_t rmidb_sysfs_data_show(struct file *data_file,
		struct kobject *kobj, struct bin_attribute *attributes,
		char *buf, loff_t pos, size_t count)
{
	int retval;
	unsigned int data_length = rmidb->length;

	if (data_length > (REG_ADDR_LIMIT - rmidb->address))
		data_length = REG_ADDR_LIMIT - rmidb->address;

	if (count < data_length) {
		tsp_debug_err(true, &rmidb->rmi4_data->i2c_client->dev,
				"%s: Not enough space (%d bytes) in buffer\n",
				__func__, count);
		return -EINVAL;
	}

	if (data_length) {
		retval = rmidb->fn_ptr->read(rmidb->rmi4_data,
				rmidb->address,
				(unsigned char *)buf,
				data_length);
		if (retval < 0) {
			tsp_debug_err(true, &rmidb->rmi4_data->i2c_client->dev,
					"%s: Failed to read data\n",
					__func__);
			return retval;
		}
	} else {
		return -EINVAL;
	}

	return data_length;
}

static ssize_t rmidb_sysfs_data_store(struct file *data_file,
		struct kobject *kobj, struct bin_attribute *attributes,
		char *buf, loff_t pos, size_t count)
{
	int retval;
	unsigned int data_length = rmidb->length;

	if (data_length > (REG_ADDR_LIMIT - rmidb->address))
		data_length = REG_ADDR_LIMIT - rmidb->address;

	if (data_length) {
		retval = rmidb->fn_ptr->write(rmidb->rmi4_data,
				rmidb->address,
				(unsigned char *)buf,
				data_length);
		if (retval < 0) {
			tsp_debug_err(true, &rmidb->rmi4_data->i2c_client->dev,
					"%s: Failed to write data\n",
					__func__);
			return retval;
		}
	} else {
		return -EINVAL;
	}

	return count;
}

static ssize_t rmidb_sysfs_pid_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	return snprintf(buf, PAGE_SIZE, "%u\n", rmidb->pid);
}

static ssize_t rmidb_sysfs_pid_store(struct device *dev,
		struct device_attribute *attr, const char *buf, size_t count)
{
	unsigned int input;

	if (sscanf(buf, "%u", &input) != 1)
		return -EINVAL;

	rmidb->pid = input;

	if (rmidb->pid) {
		rmidb->task = pid_task(find_vpid(rmidb->pid), PIDTYPE_PID);
		if (!rmidb->task) {
			tsp_debug_err(true, &rmidb->rmi4_data->i2c_client->dev,
					"%s: Failed to locate debug app PID\n",
					__func__);
			return -EINVAL;
		}
	}

	return count;
}

static ssize_t rmidb_sysfs_term_store(struct device *dev,
		struct device_attribute *attr, const char *buf, size_t count)
{
	unsigned int input;

	if (sscanf(buf, "%u", &input) != 1)
		return -EINVAL;

	if (input != 1)
		return -EINVAL;

	if (rmidb->pid)
		send_sig_info(SIGTERM, &rmidb->terminate_signal, rmidb->task);

	return count;
}

static ssize_t rmidb_sysfs_address_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	return snprintf(buf, PAGE_SIZE, "0x%04x\n", rmidb->address);
}

static ssize_t rmidb_sysfs_address_store(struct device *dev,
		struct device_attribute *attr, const char *buf, size_t count)
{
	unsigned int input;

	if (sscanf(buf, "%u", &input) != 1)
		return -EINVAL;

	if (input > REG_ADDR_LIMIT)
		return -EINVAL;

	rmidb->address = (unsigned short)input;

	return count;
}

static ssize_t rmidb_sysfs_length_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	return snprintf(buf, PAGE_SIZE, "%u\n", rmidb->length);
}

static ssize_t rmidb_sysfs_length_store(struct device *dev,
		struct device_attribute *attr, const char *buf, size_t count)
{
	unsigned int input;

	if (sscanf(buf, "%u", &input) != 1)
		return -EINVAL;

	if (input > REG_ADDR_LIMIT)
		return -EINVAL;

	rmidb->length = input;

	return count;
}

static ssize_t rmidb_sysfs_query_base_addr_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	return snprintf(buf, PAGE_SIZE, "0x%04x\n", rmidb->query_base_addr);
}

static ssize_t rmidb_sysfs_control_base_addr_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	return snprintf(buf, PAGE_SIZE, "0x%04x\n", rmidb->control_base_addr);
}

static ssize_t rmidb_sysfs_data_base_addr_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	return snprintf(buf, PAGE_SIZE, "0x%04x\n", rmidb->data_base_addr);
}

static ssize_t rmidb_sysfs_command_base_addr_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	return snprintf(buf, PAGE_SIZE, "0x%04x\n", rmidb->command_base_addr);
}

static int rmidb_scan_pdt(void)
{
	int retval;
	unsigned short ii;
	unsigned char page;
	unsigned char intr_count = 0;
	unsigned char intr_offset;
	bool fdb_found = false;
	struct synaptics_rmi4_fn_desc rmi_fd;

	for (page = 0; page < PAGES_TO_SERVICE; page++) {
		for (ii = PDT_START; ii > PDT_END; ii -= PDT_ENTRY_SIZE) {
			ii |= (page << 8);

			retval = rmidb->fn_ptr->read(rmidb->rmi4_data,
					ii,
					(unsigned char *)&rmi_fd,
					sizeof(rmi_fd));
			if (retval < 0)
				return retval;

			if (!rmi_fd.fn_number)
				break;

			if (rmi_fd.fn_number == SYNAPTICS_RMI4_FDB) {
				fdb_found = true;
				goto fdb_found;
			}

			intr_count += (rmi_fd.intr_src_count & MASK_3BIT);
		}
	}

	if (!fdb_found) {
		tsp_debug_err(true, &rmidb->rmi4_data->i2c_client->dev,
				"%s: Failed to find FDB\n",
				__func__);
		return 0;
	}

fdb_found:
	rmidb->query_base_addr = rmi_fd.query_base_addr | (page << 8);
	rmidb->control_base_addr = rmi_fd.ctrl_base_addr | (page << 8);
	rmidb->data_base_addr = rmi_fd.data_base_addr | (page << 8);
	rmidb->command_base_addr = rmi_fd.cmd_base_addr | (page << 8);

	rmidb->intr_reg_num = (intr_count + 7) / 8;
	if (rmidb->intr_reg_num != 0)
		rmidb->intr_reg_num -= 1;

	rmidb->intr_mask = 0;
	intr_offset = intr_count % 8;
	for (ii = intr_offset;
			ii < ((rmi_fd.intr_src_count & MASK_3BIT) +
				intr_offset);
			ii++) {
		rmidb->intr_mask |= 1 << ii;
	}

	return 0;
}

static void rmidb_attn(struct synaptics_rmi4_data *rmi4_data,
		unsigned char intr_mask)
{
	if (!rmidb)
		return;

	if (rmidb->pid && (rmidb->intr_mask & intr_mask))
		send_sig_info(SIGIO, &rmidb->interrupt_signal, rmidb->task);

	return;
}

static int rmidb_init(struct synaptics_rmi4_data *rmi4_data)
{
	int retval;
	unsigned char attr_count;
	int attr_count_num;

	rmidb = kzalloc(sizeof(*rmidb), GFP_KERNEL);
	if (!rmidb) {
		tsp_debug_err(true, &rmi4_data->i2c_client->dev,
				"%s: Failed to alloc mem for rmidb\n",
				__func__);
		retval = -ENOMEM;
		goto err_rmidb;
	}

	rmidb->fn_ptr =  kzalloc(sizeof(*(rmidb->fn_ptr)), GFP_KERNEL);
	if (!rmidb->fn_ptr) {
		tsp_debug_err(true, &rmi4_data->i2c_client->dev,
				"%s: Failed to alloc mem for fn_ptr\n",
				__func__);
		retval = -ENOMEM;
		goto err_fn_ptr;
	}

	memset(&rmidb->interrupt_signal, 0, sizeof(rmidb->interrupt_signal));
	rmidb->interrupt_signal.si_signo = SIGIO;
	rmidb->interrupt_signal.si_code = SI_USER;

	memset(&rmidb->terminate_signal, 0, sizeof(rmidb->terminate_signal));
	rmidb->terminate_signal.si_signo = SIGTERM;
	rmidb->terminate_signal.si_code = SI_USER;

	rmidb->fn_ptr->read = rmi4_data->i2c_read;
	rmidb->fn_ptr->write = rmi4_data->i2c_write;
	rmidb->fn_ptr->enable = rmi4_data->irq_enable;
	rmidb->rmi4_data = rmi4_data;

	retval = rmidb_scan_pdt();
	if (retval < 0) {
		retval = 0;
		goto err_scan_pdt;
	}

	rmidb->sysfs_dir = kobject_create_and_add("rmidb",
			&rmi4_data->input_dev->dev.kobj);
	if (!rmidb->sysfs_dir) {
		tsp_debug_err(true, &rmi4_data->i2c_client->dev,
				"%s: Failed to create sysfs directory\n",
				__func__);
		retval = -ENODEV;
		goto err_sysfs_dir;
	}

	retval = sysfs_create_bin_file(rmidb->sysfs_dir,
			&attr_data);
	if (retval < 0) {
		tsp_debug_err(true, &rmi4_data->i2c_client->dev,
				"%s: Failed to create sysfs bin file\n",
				__func__);
		goto err_sysfs_bin;
	}

	for (attr_count = 0; attr_count < ARRAY_SIZE(attrs); attr_count++) {
		retval = sysfs_create_file(rmidb->sysfs_dir,
				&attrs[attr_count].attr);
		if (retval < 0) {
			tsp_debug_err(true, &rmi4_data->input_dev->dev,
					"%s: Failed to create sysfs attributes\n",
					__func__);
			retval = -ENODEV;
			goto err_sysfs_attrs;
		}
	}

	return 0;

err_sysfs_attrs:
	attr_count_num = (int)attr_count;
	for (attr_count_num--; attr_count_num >= 0; attr_count_num--)
		sysfs_remove_file(rmidb->sysfs_dir, &attrs[attr_count].attr);

	sysfs_remove_bin_file(rmidb->sysfs_dir, &attr_data);

err_sysfs_bin:
	kobject_put(rmidb->sysfs_dir);

err_sysfs_dir:
err_scan_pdt:
	kfree(rmidb->fn_ptr);

err_fn_ptr:
	kfree(rmidb);
	rmidb = NULL;

err_rmidb:
	return retval;
}

static void rmidb_remove(struct synaptics_rmi4_data *rmi4_data)
{
	unsigned char attr_count;

	if (!rmidb)
		goto exit;

	for (attr_count = 0; attr_count < ARRAY_SIZE(attrs); attr_count++)
		sysfs_remove_file(rmidb->sysfs_dir, &attrs[attr_count].attr);

	sysfs_remove_bin_file(rmidb->sysfs_dir, &attr_data);

	kobject_put(rmidb->sysfs_dir);

	kfree(rmidb->fn_ptr);
	kfree(rmidb);
	rmidb = NULL;

exit:
	return;
}

int rmidb_module_register(struct synaptics_rmi4_data *rmi4_data)
{
	int retval;

	retval = synaptics_rmi4_new_function(RMI_DB,
			rmi4_data,
			rmidb_init,
			NULL,
			rmidb_remove,
			rmidb_attn);

	return retval;
}