xref: /OK3568_Linux_fs/kernel/drivers/input/touchscreen/gt1x/gt1x_extents.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /* drivers/input/touchscreen/gt1x_extents.c
2  *
3  * 2010 - 2014 Goodix Technology.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be a reference
11  * to you, when you are integrating the GOODiX's CTP IC into your system,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * Version: 1.4
17  * Release Date:  2015/07/10
18  */
19 
20 #include <linux/compat.h>
21 #include <linux/interrupt.h>
22 #include <linux/i2c.h>
23 #include <linux/sched.h>
24 #include <linux/kthread.h>
25 #include <linux/wait.h>
26 #include <linux/time.h>
27 #include <linux/delay.h>
28 #include <linux/device.h>
29 #include <linux/miscdevice.h>
30 #include <linux/input.h>
31 
32 #include <linux/uaccess.h>
33 #include <linux/proc_fs.h>	/*proc */
34 
35 #include <asm/ioctl.h>
36 #include "gt1x_generic.h"
37 
38 #if GTP_GESTURE_WAKEUP
39 
40 #define GESTURE_NODE "goodix_gesture"
41 #define GESTURE_MAX_POINT_COUNT    64
42 
43 #pragma pack(1)
44 typedef struct {
45 	u8 ic_msg[6];		/*from the first byte */
46 	u8 gestures[4];
47 	u8 data[3 + GESTURE_MAX_POINT_COUNT * 4 + 80];	/*80 bytes for extra data */
48 } st_gesture_data;
49 #pragma pack()
50 
51 #define SETBIT(longlong, bit)   (longlong[bit/8] |=  (1 << bit%8))
52 #define CLEARBIT(longlong, bit) (longlong[bit/8] &= (~(1 << bit%8)))
53 #define QUERYBIT(longlong, bit) (!!(longlong[bit/8] & (1 << bit%8)))
54 
55 #define CHKBITS_32          32
56 #define CHKBITS_16          16
57 #define CHKBITS_8           8
58 
59 int gesture_enabled;    /* module switch */
60 DOZE_T gesture_doze_status = DOZE_DISABLED; /* doze status */
61 
62 static u8 gestures_flag[32]; /* gesture flag, every bit stands for a gesture */
63 static st_gesture_data gesture_data; /* gesture data buffer */
64 static struct mutex gesture_data_mutex; /* lock for gesture data */
65 
gt1x_gesture_data_read(struct file * file,char __user * page,size_t size,loff_t * ppos)66 static ssize_t gt1x_gesture_data_read(struct file *file, char __user *page, size_t size, loff_t *ppos)
67 {
68 	s32 ret = -1;
69 	GTP_DEBUG("visit gt1x_gesture_data_read. ppos:%d", (int)*ppos);
70 	if (*ppos) {
71 		return 0;
72 	}
73 	if (size == 4) {
74 		ret = copy_to_user(((u8 __user *) page), "GT1X", 4);
75 		return 4;
76 	}
77 	ret = simple_read_from_buffer(page, size, ppos, &gesture_data, sizeof(gesture_data));
78 
79 	GTP_DEBUG("Got the gesture data.");
80 	return ret;
81 }
82 
gt1x_gesture_data_write(struct file * filp,const char __user * buff,size_t len,loff_t * off)83 static ssize_t gt1x_gesture_data_write(struct file *filp, const char __user *buff, size_t len, loff_t *off)
84 {
85 	s32 ret = 0;
86 
87 	GTP_DEBUG_FUNC();
88 
89 	ret = copy_from_user(&gesture_enabled, buff, 1);
90 	if (ret) {
91 		GTP_ERROR("copy_from_user failed.");
92 		return -EPERM;
93 	}
94 
95 	GTP_DEBUG("gesture enabled:%x, ret:%d", gesture_enabled, ret);
96 
97 	return len;
98 }
99 
100 /**
101  * calc_checksum - Calc checksum.
102  * @buf: data to be calc
103  * @len: length of buf.
104  * @bits: checkbits
105  * Return true-pass, false:not pass.
106  */
calc_checksum(u8 * buf,int len,int bits)107 static bool calc_checksum(u8 *buf, int len, int bits)
108 {
109 	int i;
110 
111 	if (bits == CHKBITS_16) {
112 		u16 chksum, *b = (u16 *)buf;
113 
114 		if (len % 2) {
115 			return false;
116 		}
117 
118 		len /= 2;
119 		for (i = 0, chksum = 0; i < len; i++) {
120 			if (i == len - 1)
121 				chksum += le16_to_cpu(b[i]);
122 			else
123 				chksum += be16_to_cpu(b[i]);
124 		}
125 		return chksum == 0 ? true : false;
126 	} else if (bits == CHKBITS_8) {
127 		u8 chksum;
128 
129 		for (i = 0, chksum = 0; i < len; i++) {
130 			chksum += buf[i];
131 		}
132 		return chksum == 0 ? true : false;
133 	}
134 	return false;
135 }
136 
gesture_enter_doze(void)137 int gesture_enter_doze(void)
138 {
139 	int retry = 0;
140 
141 	GTP_DEBUG_FUNC();
142 	GTP_DEBUG("Entering doze mode...");
143 	while (retry++ < 5) {
144 		if (!gt1x_send_cmd(0x08, 0)) {
145 			gesture_doze_status = DOZE_ENABLED;
146 			GTP_DEBUG("Working in doze mode!");
147 			return 0;
148 		}
149 		usleep_range(10000, 11000);
150 	}
151 	GTP_ERROR("Send doze cmd failed.");
152 	return -1;
153 }
154 
gesture_event_handler(struct input_dev * dev)155 s32 gesture_event_handler(struct input_dev *dev)
156 {
157 	u8 doze_buf[4] = { 0 }, ges_type;
158 	static int err_flag1, err_flag2;
159 	int len, extra_len, need_chk;
160 	unsigned int key_code;
161 	s32 ret = 0;
162 
163 	if (DOZE_ENABLED != gesture_doze_status) {
164 		return -1;
165 	}
166 
167 	/** package: -head 4B + track points + extra info-
168 	 * - head -
169 	 *  doze_buf[0]: gesture type,
170 	 *  doze_buf[1]: number of gesture points ,
171 	 *  doze_buf[2]: protocol type,
172 	 *  doze_buf[3]: gesture extra data length.
173 	 */
174 	ret = gt1x_i2c_read(GTP_REG_WAKEUP_GESTURE, doze_buf, 4);
175 	if (ret < 0) {
176 		return 0;
177 	}
178 
179 	ges_type = doze_buf[0];
180 	len = doze_buf[1];
181 	need_chk = doze_buf[2] & 0x80;
182 	extra_len = doze_buf[3];
183 
184 	GTP_DEBUG("0x%x = 0x%02X,0x%02X,0x%02X,0x%02X", GTP_REG_WAKEUP_GESTURE,
185 			doze_buf[0], doze_buf[1], doze_buf[2], doze_buf[3]);
186 
187 	if (len > GESTURE_MAX_POINT_COUNT) {
188 		GTP_ERROR("Gesture contain too many points!(%d)", len);
189 		len = GESTURE_MAX_POINT_COUNT;
190 	}
191 
192 	if (extra_len > 32) {
193 		GTP_ERROR("Gesture contain too many extra data!(%d)", extra_len);
194 		extra_len = 32;
195 	}
196 
197 	/* get gesture extra info */
198 	if (extra_len >= 0) {
199 		u8 ges_data[extra_len + 1];
200 
201 		/* head 4 + extra data * 4 + chksum 1 */
202 		ret = gt1x_i2c_read(GTP_REG_WAKEUP_GESTURE + 4,
203 				ges_data, extra_len + 1);
204 		if (ret < 0) {
205 			GTP_ERROR("Read extra gesture data failed.");
206 			return 0;
207 		}
208 
209 		if (likely(need_chk)) { /* calc checksum */
210 			bool val;
211 
212 			ges_data[extra_len] += doze_buf[0] + doze_buf[1]
213 				+ doze_buf[2] + doze_buf[3];
214 
215 			val = calc_checksum(ges_data, extra_len + 1, CHKBITS_8);
216 			if (unlikely(!val)) { /* check failed */
217 				GTP_ERROR("Gesture checksum error.");
218 				if (err_flag1) {
219 					err_flag1 = 0;
220 					ret = 0;
221 					goto clear_reg;
222 				} else {
223 					/* just return 0 without clear reg,
224 					   this will receive another int, we
225 					   check the data in the next frame */
226 					err_flag1 = 1;
227 					return 0;
228 				}
229 			}
230 
231 			err_flag1 = 0;
232 		}
233 
234 		mutex_lock(&gesture_data_mutex);
235 		memcpy(&gesture_data.data[4 + len * 4], ges_data, extra_len);
236 		mutex_unlock(&gesture_data_mutex);
237 	}
238 
239 	/* check gesture type (if available?) */
240 	if (ges_type == 0 || !QUERYBIT(gestures_flag, ges_type)) {
241 		GTP_INFO("Gesture[0x%02X] has been disabled.", doze_buf[0]);
242 		doze_buf[0] = 0x00;
243 		gt1x_i2c_write(GTP_REG_WAKEUP_GESTURE, doze_buf, 1);
244 		gesture_enter_doze();
245 		return 0;
246 	}
247 
248 	/* get gesture point data */
249 	if (len > 0) { /* coor num * 4 + chksum 2*/
250 		u8 ges_data[len * 4 + 2];
251 
252 		ret = gt1x_i2c_read(GES_BUFFER_ADDR, ges_data, len * 4);
253 		if (ret < 0) {
254 			GTP_ERROR("Read gesture data failed.");
255 			return 0;
256 		}
257 
258 		/* checksum reg for gesture point data */
259 		ret = gt1x_i2c_read(0x819F, &ges_data[len * 4], 2);
260 		if (ret < 0) {
261 			GTP_ERROR("Read gesture data failed.");
262 			return 0;
263 		}
264 
265 		if (likely(need_chk)) {
266 			bool val = calc_checksum(ges_data,
267 					len * 4 + 2, CHKBITS_16);
268 			if (unlikely(!val)) { /* check failed */
269 				GTP_ERROR("Gesture checksum error.");
270 				if (err_flag2) {
271 					err_flag2 = 0;
272 					ret = 0;
273 					goto clear_reg;
274 				} else {
275 					err_flag2 = 1;
276 					return 0;
277 				}
278 			}
279 
280 			err_flag2 = 0;
281 		}
282 
283 		mutex_lock(&gesture_data_mutex);
284 		memcpy(&gesture_data.data[4], ges_data, len * 4);
285 		mutex_unlock(&gesture_data_mutex);
286 	}
287 
288 	mutex_lock(&gesture_data_mutex);
289 	gesture_data.data[0] = ges_type;	/*gesture type*/
290 	gesture_data.data[1] = len;	        /*gesture points number*/
291 	gesture_data.data[2] = doze_buf[2] & 0x7F; /*protocol type*/
292 	gesture_data.data[3] = extra_len;   /*gesture date length*/
293 	mutex_unlock(&gesture_data_mutex);
294 
295 	/* get key code */
296 	key_code = ges_type < 16 ? KEY_GES_CUSTOM : KEY_GES_REGULAR;
297 	GTP_DEBUG("Gesture: 0x%02X, points: %d", doze_buf[0], doze_buf[1]);
298 
299 	input_report_key(dev, key_code, 1);
300 	input_sync(dev);
301 	input_report_key(dev, key_code, 0);
302 	input_sync(dev);
303 
304 clear_reg:
305 	doze_buf[0] = 0; /*clear ges flag*/
306 	gt1x_i2c_write(GTP_REG_WAKEUP_GESTURE, doze_buf, 1);
307 	return ret;
308 }
309 
gesture_clear_wakeup_data(void)310 void gesture_clear_wakeup_data(void)
311 {
312 	mutex_lock(&gesture_data_mutex);
313 	memset(gesture_data.data, 0, 4);
314 	mutex_unlock(&gesture_data_mutex);
315 }
316 
gt1x_gesture_debug(int on)317 void gt1x_gesture_debug(int on)
318 {
319 	if (on) {
320 		gesture_enabled = 1;
321 		memset(gestures_flag, 0xFF, sizeof(gestures_flag));
322 	} else {
323 		gesture_enabled = 0;
324 		memset(gestures_flag, 0x00, sizeof(gestures_flag));
325 		gesture_doze_status = DOZE_DISABLED;
326 	}
327 	GTP_DEBUG("Gesture debug %s", on ? "on":"off");
328 }
329 
330 #endif /* GTP_GESTURE_WAKEUP */
331 
332 /*HotKnot module*/
333 #if GTP_HOTKNOT
334 #define HOTKNOT_NODE "hotknot"
335 #define HOTKNOT_VERSION  "GOODIX,GT1X"
336 u8 hotknot_enabled;
337 u8 hotknot_transfer_mode;
338 
hotknot_open(struct inode * node,struct file * flip)339 static int hotknot_open(struct inode *node, struct file *flip)
340 {
341 	GTP_DEBUG("Hotknot is enabled.");
342 	hotknot_enabled = 1;
343 	return 0;
344 }
345 
hotknot_release(struct inode * node,struct file * filp)346 static int hotknot_release(struct inode *node, struct file *filp)
347 {
348 	GTP_DEBUG("Hotknot is disabled.");
349 	hotknot_enabled = 0;
350 	return 0;
351 }
352 
hotknot_enter_transfer_mode(void)353 static s32 hotknot_enter_transfer_mode(void)
354 {
355 	int ret = 0;
356 	u8 buffer[5] = { 0 };
357 
358 	hotknot_transfer_mode = 1;
359 #if GTP_ESD_PROTECT
360 	gt1x_esd_switch(SWITCH_OFF);
361 #endif
362 
363 	gt1x_irq_disable();
364 	gt1x_send_cmd(GTP_CMD_HN_TRANSFER, 0);
365 	msleep(100);
366 	gt1x_irq_enable();
367 
368 	ret = gt1x_i2c_read(0x8140, buffer, sizeof(buffer));
369 	if (ret) {
370 		hotknot_transfer_mode = 0;
371 		return ret;
372 	}
373 
374 	buffer[4] = 0;
375 	GTP_DEBUG("enter transfer mode: %s ", buffer);
376 	if (strcmp(buffer, "GHot")) {
377 		hotknot_transfer_mode = 0;
378 		return ERROR_HN_VER;
379 	}
380 
381 	return 0;
382 }
383 
hotknot_load_hotknot_subsystem(void)384 static s32 hotknot_load_hotknot_subsystem(void)
385 {
386 	return hotknot_enter_transfer_mode();
387 }
388 
hotknot_load_authentication_subsystem(void)389 static s32 hotknot_load_authentication_subsystem(void)
390 {
391 	s32 ret = 0;
392 	u8 buffer[5] = { 0 };
393 	ret = gt1x_hold_ss51_dsp_no_reset();
394 	if (ret < 0) {
395 		GTP_ERROR("Hold ss51 fail!");
396 		return ERROR;
397 	}
398 
399 	if (gt1x_chip_type == CHIP_TYPE_GT1X) {
400 		GTP_INFO("hotknot load jump code.");
401 		ret = gt1x_load_patch(gt1x_patch_jump_fw, 4096, 0, 1024 * 8);
402 		if (ret < 0) {
403 			GTP_ERROR("Load jump code fail!");
404 			return ret;
405 		}
406 		GTP_INFO("hotknot load auth code.");
407 		ret = gt1x_load_patch(hotknot_auth_fw, 4096, 4096, 1024 * 8);
408 		if (ret < 0) {
409 			GTP_ERROR("Load auth system fail!");
410 			return ret;
411 		}
412 	} else { /* GT2X */
413 		GTP_INFO("hotknot load auth code.");
414 		ret = gt1x_load_patch(hotknot_auth_fw, 4096, 0, 1024 * 6);
415 		if (ret < 0) {
416 			GTP_ERROR("load auth system fail!");
417 			return ret;
418 		}
419 	}
420 
421 	ret = gt1x_startup_patch();
422 	if (ret < 0) {
423 		GTP_ERROR("Startup auth system fail!");
424 		return ret;
425 	}
426 	ret = gt1x_i2c_read(GTP_REG_VERSION, buffer, 4);
427 	if (ret < 0) {
428 		GTP_ERROR("i2c read error!");
429 		return ERROR_IIC;
430 	}
431 	buffer[4] = 0;
432 	GTP_INFO("Current System version: %s", buffer);
433 	return 0;
434 }
435 
hotknot_recovery_main_system(void)436 static s32 hotknot_recovery_main_system(void)
437 {
438 	gt1x_irq_disable();
439 	gt1x_reset_guitar();
440 	gt1x_irq_enable();
441 #if GTP_ESD_PROTECT
442 	gt1x_esd_switch(SWITCH_ON);
443 #endif
444 	hotknot_transfer_mode = 0;
445 	return 0;
446 }
447 
448 #if HOTKNOT_BLOCK_RW
449 DECLARE_WAIT_QUEUE_HEAD(bp_waiter);
450 static u8 got_hotknot_state;
451 static u8 got_hotknot_extra_state;
452 static u8 wait_hotknot_state;
453 static u8 force_wake_flag;
454 static u8 block_enable;
455 s32 hotknot_paired_flag;
456 
hotknot_block_rw(u8 rqst_hotknot_state,s32 wait_hotknot_timeout)457 static s32 hotknot_block_rw(u8 rqst_hotknot_state, s32 wait_hotknot_timeout)
458 {
459 	s32 ret = 0;
460 
461 	wait_hotknot_state |= rqst_hotknot_state;
462 	GTP_DEBUG("Goodix tool received wait polling state:0x%x,timeout:%d, all wait state:0x%x", rqst_hotknot_state, wait_hotknot_timeout, wait_hotknot_state);
463 	got_hotknot_state &= (~rqst_hotknot_state);
464 
465 	set_current_state(TASK_INTERRUPTIBLE);
466 	if (wait_hotknot_timeout <= 0) {
467 		wait_event_interruptible(bp_waiter, force_wake_flag || rqst_hotknot_state == (got_hotknot_state & rqst_hotknot_state));
468 	} else {
469 		wait_event_interruptible_timeout(bp_waiter, force_wake_flag || rqst_hotknot_state == (got_hotknot_state & rqst_hotknot_state), wait_hotknot_timeout);
470 	}
471 
472 	wait_hotknot_state &= (~rqst_hotknot_state);
473 
474 	if (rqst_hotknot_state != (got_hotknot_state & rqst_hotknot_state)) {
475 		GTP_ERROR("Wait 0x%x block polling waiter failed.", rqst_hotknot_state);
476 		ret = -1;
477 	}
478 
479 	force_wake_flag = 0;
480 	return ret;
481 }
482 
hotknot_wakeup_block(void)483 static void hotknot_wakeup_block(void)
484 {
485 	GTP_DEBUG("Manual wakeup all block polling waiter!");
486 	got_hotknot_state = 0;
487 	wait_hotknot_state = 0;
488 	force_wake_flag = 1;
489 	wake_up_interruptible(&bp_waiter);
490 }
491 
hotknot_event_handler(u8 * data)492 s32 hotknot_event_handler(u8 *data)
493 {
494 	u8 hn_pxy_state = 0;
495 	u8 hn_pxy_state_bak = 0;
496 	static u8 hn_paired_cnt;
497 	u8 hn_state_buf[10] = { 0 };
498 	u8 finger = data[0];
499 	u8 id = 0;
500 
501 	if (block_enable && !hotknot_paired_flag && (finger & 0x0F)) {
502 		id = data[1];
503 		hn_pxy_state = data[2] & 0x80;
504 		hn_pxy_state_bak = data[3] & 0x80;
505 		if ((32 == id) && (0x80 == hn_pxy_state) && (0x80 == hn_pxy_state_bak)) {
506 #ifdef HN_DBLCFM_PAIRED
507 			if (hn_paired_cnt++ < 2) {
508 				return 0;
509 			}
510 #endif
511 			GTP_DEBUG("HotKnot paired!");
512 			if (wait_hotknot_state & HN_DEVICE_PAIRED) {
513 				GTP_DEBUG("INT wakeup HN_DEVICE_PAIRED block polling waiter");
514 				got_hotknot_state |= HN_DEVICE_PAIRED;
515 				wake_up_interruptible(&bp_waiter);
516 			}
517 			block_enable = 0;
518 			hotknot_paired_flag = 1;
519 			return 0;
520 		} else {
521 			got_hotknot_state &= (~HN_DEVICE_PAIRED);
522 			hn_paired_cnt = 0;
523 		}
524 	}
525 
526 	if (hotknot_paired_flag) {
527 		s32 ret = -1;
528 		ret = gt1x_i2c_read(GTP_REG_HN_STATE, hn_state_buf, 6);
529 		if (ret < 0) {
530 			GTP_ERROR("I2C transfer error. errno:%d\n ", ret);
531 			return 0;
532 		}
533 
534 		got_hotknot_state = 0;
535 
536 		GTP_DEBUG("wait_hotknot_state:%x", wait_hotknot_state);
537 		GTP_DEBUG("[0x8800~0x8803]=0x%x,0x%x,0x%x,0x%x", hn_state_buf[0], hn_state_buf[1], hn_state_buf[2], hn_state_buf[3]);
538 
539 		if (wait_hotknot_state & HN_MASTER_SEND) {
540 			if ((0x03 == hn_state_buf[0]) || (0x04 == hn_state_buf[0])
541 			    || (0x07 == hn_state_buf[0])) {
542 				GTP_DEBUG("Wakeup HN_MASTER_SEND block polling waiter");
543 				got_hotknot_state |= HN_MASTER_SEND;
544 				got_hotknot_extra_state = hn_state_buf[0];
545 				wake_up_interruptible(&bp_waiter);
546 			}
547 		} else if (wait_hotknot_state & HN_SLAVE_RECEIVED) {
548 			if ((0x03 == hn_state_buf[1]) || (0x04 == hn_state_buf[1])
549 			    || (0x07 == hn_state_buf[1])) {
550 				GTP_DEBUG("Wakeup HN_SLAVE_RECEIVED block polling waiter:0x%x", hn_state_buf[1]);
551 				got_hotknot_state |= HN_SLAVE_RECEIVED;
552 				got_hotknot_extra_state = hn_state_buf[1];
553 				wake_up_interruptible(&bp_waiter);
554 			}
555 		} else if (wait_hotknot_state & HN_MASTER_DEPARTED) {
556 			if (0x07 == hn_state_buf[0]) {
557 				GTP_DEBUG("Wakeup HN_MASTER_DEPARTED block polling waiter");
558 				got_hotknot_state |= HN_MASTER_DEPARTED;
559 				wake_up_interruptible(&bp_waiter);
560 			}
561 		} else if (wait_hotknot_state & HN_SLAVE_DEPARTED) {
562 			if (0x07 == hn_state_buf[1]) {
563 				GTP_DEBUG("Wakeup HN_SLAVE_DEPARTED block polling waiter");
564 				got_hotknot_state |= HN_SLAVE_DEPARTED;
565 				wake_up_interruptible(&bp_waiter);
566 			}
567 		}
568 		return 0;
569 	}
570 
571 	return -1;
572 }
573 #endif /*HOTKNOT_BLOCK_RW*/
574 #endif /*GTP_HOTKNOT*/
575 
576 #define GOODIX_MAGIC_NUMBER        'G'
577 #define NEGLECT_SIZE_MASK           (~(_IOC_SIZEMASK << _IOC_SIZESHIFT))
578 
579 #define GESTURE_ENABLE              _IO(GOODIX_MAGIC_NUMBER, 1)
580 #define GESTURE_DISABLE             _IO(GOODIX_MAGIC_NUMBER, 2)
581 #define GESTURE_FLAG_SET            _IO(GOODIX_MAGIC_NUMBER, 3)
582 #define GESTURE_FLAG_CLEAR          _IO(GOODIX_MAGIC_NUMBER, 4)
583 /*#define SET_ENABLED_GESTURE         (_IOW(GOODIX_MAGIC_NUMBER, 5, u8) & NEGLECT_SIZE_MASK)*/
584 #define GESTURE_DATA_OBTAIN         (_IOR(GOODIX_MAGIC_NUMBER, 6, u8) & NEGLECT_SIZE_MASK)
585 #define GESTURE_DATA_ERASE          _IO(GOODIX_MAGIC_NUMBER, 7)
586 
587 /*#define HOTKNOT_LOAD_SUBSYSTEM      (_IOW(GOODIX_MAGIC_NUMBER, 6, u8) & NEGLECT_SIZE_MASK)*/
588 #define HOTKNOT_LOAD_HOTKNOT        _IO(GOODIX_MAGIC_NUMBER, 20)
589 #define HOTKNOT_LOAD_AUTHENTICATION _IO(GOODIX_MAGIC_NUMBER, 21)
590 #define HOTKNOT_RECOVERY_MAIN       _IO(GOODIX_MAGIC_NUMBER, 22)
591 /*#define HOTKNOT_BLOCK_RW            (_IOW(GOODIX_MAGIC_NUMBER, 6, u8) & NEGLECT_SIZE_MASK)*/
592 #define HOTKNOT_DEVICES_PAIRED      _IO(GOODIX_MAGIC_NUMBER, 23)
593 #define HOTKNOT_MASTER_SEND         _IO(GOODIX_MAGIC_NUMBER, 24)
594 #define HOTKNOT_SLAVE_RECEIVE       _IO(GOODIX_MAGIC_NUMBER, 25)
595 /*#define HOTKNOT_DEVICES_COMMUNICATION*/
596 #define HOTKNOT_MASTER_DEPARTED     _IO(GOODIX_MAGIC_NUMBER, 26)
597 #define HOTKNOT_SLAVE_DEPARTED      _IO(GOODIX_MAGIC_NUMBER, 27)
598 #define HOTKNOT_VENDOR_VERSION      (_IOR(GOODIX_MAGIC_NUMBER, 28, u8) & NEGLECT_SIZE_MASK)
599 #define HOTKNOT_WAKEUP_BLOCK        _IO(GOODIX_MAGIC_NUMBER, 29)
600 
601 #define IO_IIC_READ                  (_IOR(GOODIX_MAGIC_NUMBER, 100, u8) & NEGLECT_SIZE_MASK)
602 #define IO_IIC_WRITE                 (_IOW(GOODIX_MAGIC_NUMBER, 101, u8) & NEGLECT_SIZE_MASK)
603 #define IO_RESET_GUITAR              _IO(GOODIX_MAGIC_NUMBER, 102)
604 #define IO_DISABLE_IRQ               _IO(GOODIX_MAGIC_NUMBER, 103)
605 #define IO_ENABLE_IRQ                _IO(GOODIX_MAGIC_NUMBER, 104)
606 #define IO_GET_VERISON               (_IOR(GOODIX_MAGIC_NUMBER, 110, u8) & NEGLECT_SIZE_MASK)
607 #define IO_PRINT                     (_IOW(GOODIX_MAGIC_NUMBER, 111, u8) & NEGLECT_SIZE_MASK)
608 #define IO_VERSION                   "V1.3-20150420"
609 
610 #define CMD_HEAD_LENGTH             20
io_iic_read(u8 * data,void __user * arg)611 static s32 io_iic_read(u8 *data, void __user *arg)
612 {
613 	s32 err = ERROR;
614 	s32 data_length = 0;
615 	u16 addr = 0;
616 
617 	err = copy_from_user(data, arg, CMD_HEAD_LENGTH);
618 	if (err) {
619 		GTP_ERROR("Can't access the memory.");
620 		return ERROR_MEM;
621 	}
622 
623 	addr = data[0] << 8 | data[1];
624 	data_length = data[2] << 8 | data[3];
625 
626 	err = gt1x_i2c_read(addr, &data[CMD_HEAD_LENGTH], data_length);
627 	if (!err) {
628 		err = copy_to_user(&((u8 __user *) arg)[CMD_HEAD_LENGTH], &data[CMD_HEAD_LENGTH], data_length);
629 		if (err) {
630 			GTP_ERROR("ERROR when copy to user.[addr: %04x], [read length:%d]", addr, data_length);
631 			return ERROR_MEM;
632 		}
633 		err = CMD_HEAD_LENGTH + data_length;
634 	}
635 	/*GTP_DEBUG("IIC_READ.addr:0x%4x, length:%d, ret:%d", addr, data_length, err);*/
636 	/*GTP_DEBUG_ARRAY((&data[CMD_HEAD_LENGTH]), data_length);*/
637 
638 	return err;
639 }
640 
io_iic_write(u8 * data)641 static s32 io_iic_write(u8 *data)
642 {
643 	s32 err = ERROR;
644 	s32 data_length = 0;
645 	u16 addr = 0;
646 
647 	addr = data[0] << 8 | data[1];
648 	data_length = data[2] << 8 | data[3];
649 
650 	err = gt1x_i2c_write(addr, &data[CMD_HEAD_LENGTH], data_length);
651 	if (!err) {
652 		err = CMD_HEAD_LENGTH + data_length;
653 	}
654 
655 	return err;
656 }
657 
658 /*
659  *@return, 0:operate successfully
660  *        > 0: the length of memory size ioctl has accessed,
661  *        error otherwise.
662  */
gt1x_ioctl(struct file * file,unsigned int cmd,unsigned long arg)663 static long gt1x_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
664 {
665 	u32 value = 0;
666 	s32 ret = 0;
667 	u8 *data = NULL;
668 	int cnt = 30;
669 
670 	/* Blocking when firmwaer updating */
671 	while (cnt-- && update_info.status) {
672 		ssleep(1);
673 	}
674 	/*GTP_DEBUG("IOCTL CMD:%x", cmd);*/
675 	/* GTP_DEBUG("command:%d, length:%d, rw:%s",
676 	   _IOC_NR(cmd),
677 	   _IOC_SIZE(cmd),
678 	   (_IOC_DIR(cmd) & _IOC_READ) ? "read" : (_IOC_DIR(cmd) & _IOC_WRITE) ? "write" : "-");
679 	   */
680 	if (_IOC_DIR(cmd)) {
681 		s32 err = -1;
682 		s32 data_length = _IOC_SIZE(cmd);
683 		data = kzalloc(data_length, GFP_KERNEL);
684 		memset(data, 0, data_length);
685 
686 		if (_IOC_DIR(cmd) & _IOC_WRITE) {
687 			err = copy_from_user(data, (void __user *)arg, data_length);
688 			if (err) {
689 				GTP_ERROR("Can't access the memory.");
690 				kfree(data);
691 				return -1;
692 			}
693 		}
694 	} else {
695 		value = (u32) arg;
696 	}
697 
698 	switch (cmd & NEGLECT_SIZE_MASK) {
699 	case IO_GET_VERISON:
700 		if ((u8 __user *) arg) {
701 			ret = copy_to_user(((u8 __user *) arg), IO_VERSION, sizeof(IO_VERSION));
702 			if (!ret) {
703 				ret = sizeof(IO_VERSION);
704 			}
705 			GTP_INFO("%s", IO_VERSION);
706 		}
707 		break;
708 	case IO_IIC_READ:
709 		ret = io_iic_read(data, (void __user *)arg);
710 		break;
711 
712 	case IO_IIC_WRITE:
713 		ret = io_iic_write(data);
714 		break;
715 
716 	case IO_RESET_GUITAR:
717 		gt1x_irq_disable();
718 		gt1x_reset_guitar();
719 		gt1x_irq_enable();
720 		break;
721 
722 	case IO_DISABLE_IRQ:
723 		gt1x_irq_disable();
724 #if GTP_ESD_PROTECT
725 		gt1x_esd_switch(SWITCH_OFF);
726 #endif
727 		break;
728 
729 	case IO_ENABLE_IRQ:
730 		gt1x_irq_enable();
731 #if GTP_ESD_PROTECT
732 		gt1x_esd_switch(SWITCH_ON);
733 #endif
734 		break;
735 
736 		/*print a string to syc log messages between application and kernel.*/
737 	case IO_PRINT:
738 		if (data)
739 			GTP_INFO("%s", (char *)data);
740 		break;
741 
742 #if GTP_GESTURE_WAKEUP
743 	case GESTURE_ENABLE:
744 		GTP_DEBUG("Gesture switch ON.");
745 		gesture_enabled = 1;
746 		break;
747 
748 	case GESTURE_DISABLE:
749 		GTP_DEBUG("Gesture switch OFF.");
750 		gesture_enabled = 0;
751 		break;
752 
753 	case GESTURE_FLAG_SET:
754 		SETBIT(gestures_flag, (u8) value);
755 		GTP_DEBUG("Gesture flag: 0x%02X enabled.", value);
756 		break;
757 
758 	case GESTURE_FLAG_CLEAR:
759 		CLEARBIT(gestures_flag, (u8) value);
760 		GTP_DEBUG("Gesture flag: 0x%02X disabled.", value);
761 		break;
762 
763 	case GESTURE_DATA_OBTAIN:
764 		GTP_DEBUG("Obtain gesture data.");
765 		mutex_lock(&gesture_data_mutex);
766 		ret = copy_to_user(((u8 __user *) arg), &gesture_data.data, 4 + gesture_data.data[1] * 4 + gesture_data.data[3]);
767 		if (ret) {
768 			GTP_ERROR("ERROR when copy gesture data to user.");
769 			ret = ERROR_MEM;
770 		} else {
771 			ret = 4 + gesture_data.data[1] * 4 + gesture_data.data[3];
772 		}
773 		mutex_unlock(&gesture_data_mutex);
774 		break;
775 
776 	case GESTURE_DATA_ERASE:
777 		GTP_DEBUG("ERASE_GESTURE_DATA");
778 		gesture_clear_wakeup_data();
779 		break;
780 #endif /*GTP_GESTURE_WAKEUP*/
781 
782 #if GTP_HOTKNOT
783 	case HOTKNOT_VENDOR_VERSION:
784 		ret =  copy_to_user(((u8 __user *) arg), HOTKNOT_VERSION, sizeof(HOTKNOT_VERSION));
785 		if (!ret) {
786 			ret = sizeof(HOTKNOT_VERSION);
787 		}
788 		break;
789 	case HOTKNOT_LOAD_HOTKNOT:
790 		ret = hotknot_load_hotknot_subsystem();
791 		break;
792 
793 	case HOTKNOT_LOAD_AUTHENTICATION:
794 #if GTP_ESD_PROTECT
795 		gt1x_esd_switch(SWITCH_OFF);
796 #endif
797 		ret = hotknot_load_authentication_subsystem();
798 		break;
799 
800 	case HOTKNOT_RECOVERY_MAIN:
801 		ret = hotknot_recovery_main_system();
802 		break;
803 #if HOTKNOT_BLOCK_RW
804 	case HOTKNOT_DEVICES_PAIRED:
805 		hotknot_paired_flag = 0;
806 		force_wake_flag = 0;
807 		block_enable = 1;
808 		ret = hotknot_block_rw(HN_DEVICE_PAIRED, (s32) value);
809 		break;
810 
811 	case HOTKNOT_MASTER_SEND:
812 		ret = hotknot_block_rw(HN_MASTER_SEND, (s32) value);
813 		if (!ret)
814 			ret = got_hotknot_extra_state;
815 		break;
816 
817 	case HOTKNOT_SLAVE_RECEIVE:
818 		ret = hotknot_block_rw(HN_SLAVE_RECEIVED, (s32) value);
819 		if (!ret)
820 			ret = got_hotknot_extra_state;
821 		break;
822 
823 	case HOTKNOT_MASTER_DEPARTED:
824 		ret = hotknot_block_rw(HN_MASTER_DEPARTED, (s32) value);
825 		break;
826 
827 	case HOTKNOT_SLAVE_DEPARTED:
828 		ret = hotknot_block_rw(HN_SLAVE_DEPARTED, (s32) value);
829 		break;
830 
831 	case HOTKNOT_WAKEUP_BLOCK:
832 		hotknot_wakeup_block();
833 		break;
834 #endif /*HOTKNOT_BLOCK_RW*/
835 #endif /*GTP_HOTKNOT*/
836 
837 	default:
838 		GTP_INFO("Unknown cmd.");
839 		ret = -1;
840 		break;
841 	}
842 
843 	if (data != NULL) {
844 		kfree(data);
845 	}
846 
847 	return ret;
848 }
849 
850 #ifdef CONFIG_COMPAT
gt1x_compat_ioctl(struct file * file,unsigned int cmd,unsigned long arg)851 static long gt1x_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
852 {
853 	void __user *arg32 = compat_ptr(arg);
854 
855 	if (!file->f_op || !file->f_op->unlocked_ioctl)
856 		return -ENOTTY;
857 
858 	return file->f_op->unlocked_ioctl(file, cmd, (unsigned long)arg32);
859 }
860 #endif
861 
862 static const struct file_operations gt1x_fops = {
863 	.owner = THIS_MODULE,
864 #if GTP_GESTURE_WAKEUP
865 	.read = gt1x_gesture_data_read,
866 	.write = gt1x_gesture_data_write,
867 #endif
868 	.unlocked_ioctl = gt1x_ioctl,
869 #ifdef CONFIG_COMPAT
870     .compat_ioctl = gt1x_compat_ioctl,
871 #endif
872 };
873 
874 #if GTP_HOTKNOT
875 static const struct file_operations hotknot_fops = {
876 	.open = hotknot_open,
877 	.release = hotknot_release,
878 	.unlocked_ioctl = gt1x_ioctl,
879 #ifdef CONFIG_COMPAT
880     .compat_ioctl = gt1x_compat_ioctl,
881 #endif
882 };
883 
884 static struct miscdevice hotknot_misc_device = {
885 	.minor = MISC_DYNAMIC_MINOR,
886 	.name = HOTKNOT_NODE,
887 	.fops = &hotknot_fops,
888 };
889 #endif
890 
gt1x_init_node(void)891 s32 gt1x_init_node(void)
892 {
893 #if GTP_GESTURE_WAKEUP
894 	struct proc_dir_entry *proc_entry = NULL;
895 	mutex_init(&gesture_data_mutex);
896 	memset(gestures_flag, 0, sizeof(gestures_flag));
897 	memset((u8 *) &gesture_data, 0, sizeof(st_gesture_data));
898 
899 	proc_entry = proc_create(GESTURE_NODE, 0755, NULL, &gt1x_fops);
900 	if (proc_entry == NULL) {
901 		GTP_ERROR("CAN't create proc entry /proc/%s.", GESTURE_NODE);
902 		return -1;
903 	} else {
904 		GTP_INFO("Created proc entry /proc/%s.", GESTURE_NODE);
905 	}
906 #endif
907 
908 #if GTP_HOTKNOT
909 	if (misc_register(&hotknot_misc_device)) {
910 		GTP_ERROR("CAN't create misc device in /dev/hotknot.");
911 		return -1;
912 	} else {
913 		GTP_INFO("Created misc device in /dev/hotknot.");
914 	}
915 #endif
916 	return 0;
917 }
918 
gt1x_deinit_node(void)919 void gt1x_deinit_node(void)
920 {
921 #if GTP_GESTURE_WAKEUP
922 	remove_proc_entry(GESTURE_NODE, NULL);
923 #endif
924 
925 #if GTP_HOTKNOT
926 	misc_deregister(&hotknot_misc_device);
927 #endif
928 }
929