xref: /linux/drivers/hid/hid-alps.c (revision fe2ec83746e501645709761605c2464a44fd2929)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  Copyright (c) 2016 Masaki Ota <masaki.ota@jp.alps.com>
4  */
5 
6 #include <linux/kernel.h>
7 #include <linux/hid.h>
8 #include <linux/input.h>
9 #include <linux/input/mt.h>
10 #include <linux/module.h>
11 #include <linux/unaligned.h>
12 #include "hid-ids.h"
13 
14 /* ALPS Device Product ID */
15 #define HID_PRODUCT_ID_T3_BTNLESS	0xD0C0
16 #define HID_PRODUCT_ID_COSMO		0x1202
17 #define HID_PRODUCT_ID_U1_PTP_1		0x1207
18 #define HID_PRODUCT_ID_U1			0x1209
19 #define HID_PRODUCT_ID_U1_PTP_2		0x120A
20 #define HID_PRODUCT_ID_U1_DUAL		0x120B
21 #define HID_PRODUCT_ID_T4_BTNLESS	0x120C
22 
23 #define DEV_SINGLEPOINT				0x01
24 #define DEV_DUALPOINT				0x02
25 
26 #define U1_MOUSE_REPORT_ID			0x01 /* Mouse data ReportID */
27 #define U1_ABSOLUTE_REPORT_ID		0x03 /* Absolute data ReportID */
28 #define U1_ABSOLUTE_REPORT_ID_SECD  0x02 /* FW-PTP Absolute data ReportID */
29 #define U1_FEATURE_REPORT_ID		0x05 /* Feature ReportID */
30 #define U1_SP_ABSOLUTE_REPORT_ID	0x06 /* Feature ReportID */
31 
32 #define U1_FEATURE_REPORT_LEN		0x08 /* Feature Report Length */
33 #define U1_FEATURE_REPORT_LEN_ALL	0x0A
34 #define U1_CMD_REGISTER_READ		0xD1
35 #define U1_CMD_REGISTER_WRITE		0xD2
36 
37 #define	U1_DEVTYPE_SP_SUPPORT		0x10 /* SP Support */
38 #define	U1_DISABLE_DEV				0x01
39 #define U1_TP_ABS_MODE				0x02
40 #define	U1_SP_ABS_MODE				0x80
41 
42 #define ADDRESS_U1_DEV_CTRL_1	0x00800040
43 #define ADDRESS_U1_DEVICE_TYP	0x00800043
44 #define ADDRESS_U1_NUM_SENS_X	0x00800047
45 #define ADDRESS_U1_NUM_SENS_Y	0x00800048
46 #define ADDRESS_U1_PITCH_SENS_X	0x00800049
47 #define ADDRESS_U1_PITCH_SENS_Y	0x0080004A
48 #define ADDRESS_U1_RESO_DWN_ABS 0x0080004E
49 #define ADDRESS_U1_PAD_BTN		0x00800052
50 #define ADDRESS_U1_SP_BTN		0x0080009F
51 
52 #define T4_INPUT_REPORT_LEN			sizeof(struct t4_input_report)
53 #define T4_FEATURE_REPORT_LEN		T4_INPUT_REPORT_LEN
54 #define T4_FEATURE_REPORT_ID		7
55 #define T4_CMD_REGISTER_READ			0x08
56 #define T4_CMD_REGISTER_WRITE			0x07
57 
58 #define T4_ADDRESS_BASE				0xC2C0
59 #define PRM_SYS_CONFIG_1			(T4_ADDRESS_BASE + 0x0002)
60 #define T4_PRM_FEED_CONFIG_1		(T4_ADDRESS_BASE + 0x0004)
61 #define T4_PRM_FEED_CONFIG_4		(T4_ADDRESS_BASE + 0x001A)
62 #define T4_PRM_ID_CONFIG_3			(T4_ADDRESS_BASE + 0x00B0)
63 
64 
65 #define T4_FEEDCFG4_ADVANCED_ABS_ENABLE			0x01
66 #define T4_I2C_ABS	0x78
67 
68 #define T4_COUNT_PER_ELECTRODE		256
69 #define MAX_TOUCHES	5
70 
71 enum dev_num {
72 	U1,
73 	T4,
74 	UNKNOWN,
75 };
76 /**
77  * struct alps_dev
78  *
79  * @input: pointer to the kernel input device
80  * @input2: pointer to the kernel input2 device
81  * @hdev: pointer to the struct hid_device
82  *
83  * @dev_type: device type
84  * @max_fingers: total number of fingers
85  * @has_sp: boolean of sp existense
86  * @sp_btn_info: button information
87  * @x_active_len_mm: active area length of X (mm)
88  * @y_active_len_mm: active area length of Y (mm)
89  * @x_max: maximum x coordinate value
90  * @y_max: maximum y coordinate value
91  * @x_min: minimum x coordinate value
92  * @y_min: minimum y coordinate value
93  * @btn_cnt: number of buttons
94  * @sp_btn_cnt: number of stick buttons
95  */
96 struct alps_dev {
97 	struct input_dev *input;
98 	struct input_dev *input2;
99 	struct hid_device *hdev;
100 
101 	enum dev_num dev_type;
102 	u8  max_fingers;
103 	u8  has_sp;
104 	u8	sp_btn_info;
105 	u32	x_active_len_mm;
106 	u32	y_active_len_mm;
107 	u32	x_max;
108 	u32	y_max;
109 	u32	x_min;
110 	u32	y_min;
111 	u32	btn_cnt;
112 	u32	sp_btn_cnt;
113 };
114 
115 struct t4_contact_data {
116 	u8  palm;
117 	u8	x_lo;
118 	u8	x_hi;
119 	u8	y_lo;
120 	u8	y_hi;
121 };
122 
123 struct t4_input_report {
124 	u8  reportID;
125 	u8  numContacts;
126 	struct t4_contact_data contact[5];
127 	u8  button;
128 	u8  track[5];
129 	u8  zx[5], zy[5];
130 	u8  palmTime[5];
131 	u8  kilroy;
132 	u16 timeStamp;
133 };
134 
t4_calc_check_sum(u8 * buffer,unsigned long offset,unsigned long length)135 static u16 t4_calc_check_sum(u8 *buffer,
136 		unsigned long offset, unsigned long length)
137 {
138 	u16 sum1 = 0xFF, sum2 = 0xFF;
139 	unsigned long i = 0;
140 
141 	if (offset + length >= 50)
142 		return 0;
143 
144 	while (length > 0) {
145 		u32 tlen = length > 20 ? 20 : length;
146 
147 		length -= tlen;
148 
149 		do {
150 			sum1 += buffer[offset + i];
151 			sum2 += sum1;
152 			i++;
153 		} while (--tlen > 0);
154 
155 		sum1 = (sum1 & 0xFF) + (sum1 >> 8);
156 		sum2 = (sum2 & 0xFF) + (sum2 >> 8);
157 	}
158 
159 	sum1 = (sum1 & 0xFF) + (sum1 >> 8);
160 	sum2 = (sum2 & 0xFF) + (sum2 >> 8);
161 
162 	return(sum2 << 8 | sum1);
163 }
164 
t4_read_write_register(struct hid_device * hdev,u32 address,u8 * read_val,u8 write_val,bool read_flag)165 static int t4_read_write_register(struct hid_device *hdev, u32 address,
166 	u8 *read_val, u8 write_val, bool read_flag)
167 {
168 	int ret;
169 	u16 check_sum;
170 	u8 *input;
171 	u8 *readbuf = NULL;
172 
173 	input = kzalloc(T4_FEATURE_REPORT_LEN, GFP_KERNEL);
174 	if (!input)
175 		return -ENOMEM;
176 
177 	input[0] = T4_FEATURE_REPORT_ID;
178 	if (read_flag) {
179 		input[1] = T4_CMD_REGISTER_READ;
180 		input[8] = 0x00;
181 	} else {
182 		input[1] = T4_CMD_REGISTER_WRITE;
183 		input[8] = write_val;
184 	}
185 	put_unaligned_le32(address, input + 2);
186 	input[6] = 1;
187 	input[7] = 0;
188 
189 	/* Calculate the checksum */
190 	check_sum = t4_calc_check_sum(input, 1, 8);
191 	input[9] = (u8)check_sum;
192 	input[10] = (u8)(check_sum >> 8);
193 	input[11] = 0;
194 
195 	ret = hid_hw_raw_request(hdev, T4_FEATURE_REPORT_ID, input,
196 			T4_FEATURE_REPORT_LEN,
197 			HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
198 
199 	if (ret < 0) {
200 		dev_err(&hdev->dev, "failed to read command (%d)\n", ret);
201 		goto exit;
202 	}
203 
204 	if (read_flag) {
205 		readbuf = kzalloc(T4_FEATURE_REPORT_LEN, GFP_KERNEL);
206 		if (!readbuf) {
207 			ret = -ENOMEM;
208 			goto exit;
209 		}
210 
211 		ret = hid_hw_raw_request(hdev, T4_FEATURE_REPORT_ID, readbuf,
212 				T4_FEATURE_REPORT_LEN,
213 				HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
214 		if (ret < 0) {
215 			dev_err(&hdev->dev, "failed read register (%d)\n", ret);
216 			goto exit_readbuf;
217 		}
218 
219 		ret = -EINVAL;
220 
221 		if (*(u32 *)&readbuf[6] != address) {
222 			dev_err(&hdev->dev, "read register address error (%x,%x)\n",
223 				*(u32 *)&readbuf[6], address);
224 			goto exit_readbuf;
225 		}
226 
227 		if (*(u16 *)&readbuf[10] != 1) {
228 			dev_err(&hdev->dev, "read register size error (%x)\n",
229 				*(u16 *)&readbuf[10]);
230 			goto exit_readbuf;
231 		}
232 
233 		check_sum = t4_calc_check_sum(readbuf, 6, 7);
234 		if (*(u16 *)&readbuf[13] != check_sum) {
235 			dev_err(&hdev->dev, "read register checksum error (%x,%x)\n",
236 				*(u16 *)&readbuf[13], check_sum);
237 			goto exit_readbuf;
238 		}
239 
240 		*read_val = readbuf[12];
241 	}
242 
243 	ret = 0;
244 
245 exit_readbuf:
246 	kfree(readbuf);
247 exit:
248 	kfree(input);
249 	return ret;
250 }
251 
u1_read_write_register(struct hid_device * hdev,u32 address,u8 * read_val,u8 write_val,bool read_flag)252 static int u1_read_write_register(struct hid_device *hdev, u32 address,
253 	u8 *read_val, u8 write_val, bool read_flag)
254 {
255 	int ret, i;
256 	u8 check_sum;
257 	u8 *input;
258 	u8 *readbuf;
259 
260 	input = kzalloc(U1_FEATURE_REPORT_LEN, GFP_KERNEL);
261 	if (!input)
262 		return -ENOMEM;
263 
264 	input[0] = U1_FEATURE_REPORT_ID;
265 	if (read_flag) {
266 		input[1] = U1_CMD_REGISTER_READ;
267 		input[6] = 0x00;
268 	} else {
269 		input[1] = U1_CMD_REGISTER_WRITE;
270 		input[6] = write_val;
271 	}
272 
273 	put_unaligned_le32(address, input + 2);
274 
275 	/* Calculate the checksum */
276 	check_sum = U1_FEATURE_REPORT_LEN_ALL;
277 	for (i = 0; i < U1_FEATURE_REPORT_LEN - 1; i++)
278 		check_sum += input[i];
279 
280 	input[7] = check_sum;
281 	ret = hid_hw_raw_request(hdev, U1_FEATURE_REPORT_ID, input,
282 			U1_FEATURE_REPORT_LEN,
283 			HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
284 
285 	if (ret < 0) {
286 		dev_err(&hdev->dev, "failed to read command (%d)\n", ret);
287 		goto exit;
288 	}
289 
290 	if (read_flag) {
291 		readbuf = kzalloc(U1_FEATURE_REPORT_LEN, GFP_KERNEL);
292 		if (!readbuf) {
293 			ret = -ENOMEM;
294 			goto exit;
295 		}
296 
297 		ret = hid_hw_raw_request(hdev, U1_FEATURE_REPORT_ID, readbuf,
298 				U1_FEATURE_REPORT_LEN,
299 				HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
300 
301 		if (ret < 0) {
302 			dev_err(&hdev->dev, "failed read register (%d)\n", ret);
303 			kfree(readbuf);
304 			goto exit;
305 		}
306 
307 		*read_val = readbuf[6];
308 
309 		kfree(readbuf);
310 	}
311 
312 	ret = 0;
313 
314 exit:
315 	kfree(input);
316 	return ret;
317 }
318 
t4_raw_event(struct alps_dev * hdata,u8 * data,int size)319 static int t4_raw_event(struct alps_dev *hdata, u8 *data, int size)
320 {
321 	unsigned int x, y, z;
322 	int i;
323 	struct t4_input_report *p_report = (struct t4_input_report *)data;
324 
325 	if (!data)
326 		return 0;
327 	for (i = 0; i < hdata->max_fingers; i++) {
328 		x = p_report->contact[i].x_hi << 8 | p_report->contact[i].x_lo;
329 		y = p_report->contact[i].y_hi << 8 | p_report->contact[i].y_lo;
330 		y = hdata->y_max - y + hdata->y_min;
331 		z = (p_report->contact[i].palm < 0x80 &&
332 			p_report->contact[i].palm > 0) * 62;
333 		if (x == 0xffff) {
334 			x = 0;
335 			y = 0;
336 			z = 0;
337 		}
338 		input_mt_slot(hdata->input, i);
339 
340 		input_mt_report_slot_state(hdata->input,
341 			MT_TOOL_FINGER, z != 0);
342 
343 		if (!z)
344 			continue;
345 
346 		input_report_abs(hdata->input, ABS_MT_POSITION_X, x);
347 		input_report_abs(hdata->input, ABS_MT_POSITION_Y, y);
348 		input_report_abs(hdata->input, ABS_MT_PRESSURE, z);
349 	}
350 	input_mt_sync_frame(hdata->input);
351 
352 	input_report_key(hdata->input, BTN_LEFT, p_report->button);
353 
354 	input_sync(hdata->input);
355 	return 1;
356 }
357 
u1_raw_event(struct alps_dev * hdata,u8 * data,int size)358 static int u1_raw_event(struct alps_dev *hdata, u8 *data, int size)
359 {
360 	unsigned int x, y, z;
361 	int i;
362 	short sp_x, sp_y;
363 
364 	if (!data)
365 		return 0;
366 	switch (data[0]) {
367 	case U1_MOUSE_REPORT_ID:
368 		break;
369 	case U1_FEATURE_REPORT_ID:
370 		break;
371 	case U1_ABSOLUTE_REPORT_ID:
372 	case U1_ABSOLUTE_REPORT_ID_SECD:
373 		for (i = 0; i < hdata->max_fingers; i++) {
374 			u8 *contact = &data[i * 5];
375 
376 			x = get_unaligned_le16(contact + 3);
377 			y = get_unaligned_le16(contact + 5);
378 			z = contact[7] & 0x7F;
379 
380 			input_mt_slot(hdata->input, i);
381 
382 			if (z != 0) {
383 				input_mt_report_slot_state(hdata->input,
384 					MT_TOOL_FINGER, 1);
385 				input_report_abs(hdata->input,
386 					ABS_MT_POSITION_X, x);
387 				input_report_abs(hdata->input,
388 					ABS_MT_POSITION_Y, y);
389 				input_report_abs(hdata->input,
390 					ABS_MT_PRESSURE, z);
391 			} else {
392 				input_mt_report_slot_inactive(hdata->input);
393 			}
394 		}
395 
396 		input_mt_sync_frame(hdata->input);
397 
398 		input_report_key(hdata->input, BTN_LEFT,
399 			data[1] & 0x1);
400 		input_report_key(hdata->input, BTN_RIGHT,
401 			(data[1] & 0x2));
402 		input_report_key(hdata->input, BTN_MIDDLE,
403 			(data[1] & 0x4));
404 
405 		input_sync(hdata->input);
406 
407 		return 1;
408 
409 	case U1_SP_ABSOLUTE_REPORT_ID:
410 		if (!hdata->input2)
411 			return 0;
412 		sp_x = get_unaligned_le16(data+2);
413 		sp_y = get_unaligned_le16(data+4);
414 
415 		sp_x = sp_x / 8;
416 		sp_y = sp_y / 8;
417 
418 		input_report_rel(hdata->input2, REL_X, sp_x);
419 		input_report_rel(hdata->input2, REL_Y, sp_y);
420 
421 		input_report_key(hdata->input2, BTN_LEFT,
422 			data[1] & 0x1);
423 		input_report_key(hdata->input2, BTN_RIGHT,
424 			(data[1] & 0x2));
425 		input_report_key(hdata->input2, BTN_MIDDLE,
426 			(data[1] & 0x4));
427 
428 		input_sync(hdata->input2);
429 
430 		return 1;
431 	}
432 
433 	return 0;
434 }
435 
alps_raw_event(struct hid_device * hdev,struct hid_report * report,u8 * data,int size)436 static int alps_raw_event(struct hid_device *hdev,
437 		struct hid_report *report, u8 *data, int size)
438 {
439 	int ret = 0;
440 	struct alps_dev *hdata = hid_get_drvdata(hdev);
441 
442 	if (!(hdev->claimed & HID_CLAIMED_INPUT) || !hdata->input)
443 		return 0;
444 
445 	switch (hdev->product) {
446 	case HID_PRODUCT_ID_T4_BTNLESS:
447 		ret = t4_raw_event(hdata, data, size);
448 		break;
449 	default:
450 		ret = u1_raw_event(hdata, data, size);
451 		break;
452 	}
453 	return ret;
454 }
455 
alps_post_reset(struct hid_device * hdev)456 static int __maybe_unused alps_post_reset(struct hid_device *hdev)
457 {
458 	int ret = -1;
459 	struct alps_dev *data = hid_get_drvdata(hdev);
460 
461 	switch (data->dev_type) {
462 	case T4:
463 		ret = t4_read_write_register(hdev, T4_PRM_FEED_CONFIG_1,
464 			NULL, T4_I2C_ABS, false);
465 		if (ret < 0) {
466 			dev_err(&hdev->dev, "failed T4_PRM_FEED_CONFIG_1 (%d)\n",
467 				ret);
468 			goto exit;
469 		}
470 
471 		ret = t4_read_write_register(hdev, T4_PRM_FEED_CONFIG_4,
472 			NULL, T4_FEEDCFG4_ADVANCED_ABS_ENABLE, false);
473 		if (ret < 0) {
474 			dev_err(&hdev->dev, "failed T4_PRM_FEED_CONFIG_4 (%d)\n",
475 				ret);
476 			goto exit;
477 		}
478 		break;
479 	case U1:
480 		ret = u1_read_write_register(hdev,
481 			ADDRESS_U1_DEV_CTRL_1, NULL,
482 			U1_TP_ABS_MODE | U1_SP_ABS_MODE, false);
483 		if (ret < 0) {
484 			dev_err(&hdev->dev, "failed to change TP mode (%d)\n",
485 				ret);
486 			goto exit;
487 		}
488 		break;
489 	default:
490 		break;
491 	}
492 
493 exit:
494 	return ret;
495 }
496 
alps_post_resume(struct hid_device * hdev)497 static int __maybe_unused alps_post_resume(struct hid_device *hdev)
498 {
499 	return alps_post_reset(hdev);
500 }
501 
u1_init(struct hid_device * hdev,struct alps_dev * pri_data)502 static int u1_init(struct hid_device *hdev, struct alps_dev *pri_data)
503 {
504 	int ret;
505 	u8 tmp, dev_ctrl, sen_line_num_x, sen_line_num_y;
506 	u8 pitch_x, pitch_y, resolution;
507 
508 	/* Device initialization */
509 	ret = u1_read_write_register(hdev, ADDRESS_U1_DEV_CTRL_1,
510 			&dev_ctrl, 0, true);
511 	if (ret < 0) {
512 		dev_err(&hdev->dev, "failed U1_DEV_CTRL_1 (%d)\n", ret);
513 		goto exit;
514 	}
515 
516 	dev_ctrl &= ~U1_DISABLE_DEV;
517 	dev_ctrl |= U1_TP_ABS_MODE;
518 	ret = u1_read_write_register(hdev, ADDRESS_U1_DEV_CTRL_1,
519 			NULL, dev_ctrl, false);
520 	if (ret < 0) {
521 		dev_err(&hdev->dev, "failed to change TP mode (%d)\n", ret);
522 		goto exit;
523 	}
524 
525 	ret = u1_read_write_register(hdev, ADDRESS_U1_NUM_SENS_X,
526 			&sen_line_num_x, 0, true);
527 	if (ret < 0) {
528 		dev_err(&hdev->dev, "failed U1_NUM_SENS_X (%d)\n", ret);
529 		goto exit;
530 	}
531 
532 	ret = u1_read_write_register(hdev, ADDRESS_U1_NUM_SENS_Y,
533 			&sen_line_num_y, 0, true);
534 	if (ret < 0) {
535 		dev_err(&hdev->dev, "failed U1_NUM_SENS_Y (%d)\n", ret);
536 		goto exit;
537 	}
538 
539 	ret = u1_read_write_register(hdev, ADDRESS_U1_PITCH_SENS_X,
540 			&pitch_x, 0, true);
541 	if (ret < 0) {
542 		dev_err(&hdev->dev, "failed U1_PITCH_SENS_X (%d)\n", ret);
543 		goto exit;
544 	}
545 
546 	ret = u1_read_write_register(hdev, ADDRESS_U1_PITCH_SENS_Y,
547 			&pitch_y, 0, true);
548 	if (ret < 0) {
549 		dev_err(&hdev->dev, "failed U1_PITCH_SENS_Y (%d)\n", ret);
550 		goto exit;
551 	}
552 
553 	ret = u1_read_write_register(hdev, ADDRESS_U1_RESO_DWN_ABS,
554 		&resolution, 0, true);
555 	if (ret < 0) {
556 		dev_err(&hdev->dev, "failed U1_RESO_DWN_ABS (%d)\n", ret);
557 		goto exit;
558 	}
559 	pri_data->x_active_len_mm =
560 		(pitch_x * (sen_line_num_x - 1)) / 10;
561 	pri_data->y_active_len_mm =
562 		(pitch_y * (sen_line_num_y - 1)) / 10;
563 
564 	pri_data->x_max =
565 		(resolution << 2) * (sen_line_num_x - 1);
566 	pri_data->x_min = 1;
567 	pri_data->y_max =
568 		(resolution << 2) * (sen_line_num_y - 1);
569 	pri_data->y_min = 1;
570 
571 	ret = u1_read_write_register(hdev, ADDRESS_U1_PAD_BTN,
572 			&tmp, 0, true);
573 	if (ret < 0) {
574 		dev_err(&hdev->dev, "failed U1_PAD_BTN (%d)\n", ret);
575 		goto exit;
576 	}
577 	if ((tmp & 0x0F) == (tmp & 0xF0) >> 4) {
578 		pri_data->btn_cnt = (tmp & 0x0F);
579 	} else {
580 		/* Button pad */
581 		pri_data->btn_cnt = 1;
582 	}
583 
584 	pri_data->has_sp = 0;
585 	/* Check StickPointer device */
586 	ret = u1_read_write_register(hdev, ADDRESS_U1_DEVICE_TYP,
587 			&tmp, 0, true);
588 	if (ret < 0) {
589 		dev_err(&hdev->dev, "failed U1_DEVICE_TYP (%d)\n", ret);
590 		goto exit;
591 	}
592 	if (tmp & U1_DEVTYPE_SP_SUPPORT) {
593 		dev_ctrl |= U1_SP_ABS_MODE;
594 		ret = u1_read_write_register(hdev, ADDRESS_U1_DEV_CTRL_1,
595 			NULL, dev_ctrl, false);
596 		if (ret < 0) {
597 			dev_err(&hdev->dev, "failed SP mode (%d)\n", ret);
598 			goto exit;
599 		}
600 
601 		ret = u1_read_write_register(hdev, ADDRESS_U1_SP_BTN,
602 			&pri_data->sp_btn_info, 0, true);
603 		if (ret < 0) {
604 			dev_err(&hdev->dev, "failed U1_SP_BTN (%d)\n", ret);
605 			goto exit;
606 		}
607 		pri_data->has_sp = 1;
608 	}
609 	pri_data->max_fingers = 5;
610 exit:
611 	return ret;
612 }
613 
T4_init(struct hid_device * hdev,struct alps_dev * pri_data)614 static int T4_init(struct hid_device *hdev, struct alps_dev *pri_data)
615 {
616 	int ret;
617 	u8 tmp, sen_line_num_x, sen_line_num_y;
618 
619 	ret = t4_read_write_register(hdev, T4_PRM_ID_CONFIG_3, &tmp, 0, true);
620 	if (ret < 0) {
621 		dev_err(&hdev->dev, "failed T4_PRM_ID_CONFIG_3 (%d)\n", ret);
622 		goto exit;
623 	}
624 	sen_line_num_x = 16 + ((tmp & 0x0F)  | (tmp & 0x08 ? 0xF0 : 0));
625 	sen_line_num_y = 12 + (((tmp & 0xF0) >> 4)  | (tmp & 0x80 ? 0xF0 : 0));
626 
627 	pri_data->x_max = sen_line_num_x * T4_COUNT_PER_ELECTRODE;
628 	pri_data->x_min = T4_COUNT_PER_ELECTRODE;
629 	pri_data->y_max = sen_line_num_y * T4_COUNT_PER_ELECTRODE;
630 	pri_data->y_min = T4_COUNT_PER_ELECTRODE;
631 	pri_data->x_active_len_mm = pri_data->y_active_len_mm = 0;
632 	pri_data->btn_cnt = 1;
633 
634 	ret = t4_read_write_register(hdev, PRM_SYS_CONFIG_1, &tmp, 0, true);
635 	if (ret < 0) {
636 		dev_err(&hdev->dev, "failed PRM_SYS_CONFIG_1 (%d)\n", ret);
637 		goto exit;
638 	}
639 	tmp |= 0x02;
640 	ret = t4_read_write_register(hdev, PRM_SYS_CONFIG_1, NULL, tmp, false);
641 	if (ret < 0) {
642 		dev_err(&hdev->dev, "failed PRM_SYS_CONFIG_1 (%d)\n", ret);
643 		goto exit;
644 	}
645 
646 	ret = t4_read_write_register(hdev, T4_PRM_FEED_CONFIG_1,
647 					NULL, T4_I2C_ABS, false);
648 	if (ret < 0) {
649 		dev_err(&hdev->dev, "failed T4_PRM_FEED_CONFIG_1 (%d)\n", ret);
650 		goto exit;
651 	}
652 
653 	ret = t4_read_write_register(hdev, T4_PRM_FEED_CONFIG_4, NULL,
654 				T4_FEEDCFG4_ADVANCED_ABS_ENABLE, false);
655 	if (ret < 0) {
656 		dev_err(&hdev->dev, "failed T4_PRM_FEED_CONFIG_4 (%d)\n", ret);
657 		goto exit;
658 	}
659 	pri_data->max_fingers = 5;
660 	pri_data->has_sp = 0;
661 exit:
662 	return ret;
663 }
664 
alps_sp_open(struct input_dev * dev)665 static int alps_sp_open(struct input_dev *dev)
666 {
667 	struct hid_device *hid = input_get_drvdata(dev);
668 
669 	return hid_hw_open(hid);
670 }
671 
alps_sp_close(struct input_dev * dev)672 static void alps_sp_close(struct input_dev *dev)
673 {
674 	struct hid_device *hid = input_get_drvdata(dev);
675 
676 	hid_hw_close(hid);
677 }
678 
alps_input_configured(struct hid_device * hdev,struct hid_input * hi)679 static int alps_input_configured(struct hid_device *hdev, struct hid_input *hi)
680 {
681 	struct alps_dev *data = hid_get_drvdata(hdev);
682 	struct input_dev *input = hi->input, *input2;
683 	int ret;
684 	int res_x, res_y, i;
685 
686 	data->input = input;
687 
688 	hid_dbg(hdev, "Opening low level driver\n");
689 	ret = hid_hw_open(hdev);
690 	if (ret)
691 		return ret;
692 
693 	/* Allow incoming hid reports */
694 	hid_device_io_start(hdev);
695 	switch (data->dev_type) {
696 	case T4:
697 		ret = T4_init(hdev, data);
698 		break;
699 	case U1:
700 		ret = u1_init(hdev, data);
701 		break;
702 	default:
703 		break;
704 	}
705 
706 	if (ret)
707 		goto exit;
708 
709 	__set_bit(EV_ABS, input->evbit);
710 	input_set_abs_params(input, ABS_MT_POSITION_X,
711 						data->x_min, data->x_max, 0, 0);
712 	input_set_abs_params(input, ABS_MT_POSITION_Y,
713 						data->y_min, data->y_max, 0, 0);
714 
715 	if (data->x_active_len_mm && data->y_active_len_mm) {
716 		res_x = (data->x_max - 1) / data->x_active_len_mm;
717 		res_y = (data->y_max - 1) / data->y_active_len_mm;
718 
719 		input_abs_set_res(input, ABS_MT_POSITION_X, res_x);
720 		input_abs_set_res(input, ABS_MT_POSITION_Y, res_y);
721 	}
722 
723 	input_set_abs_params(input, ABS_MT_PRESSURE, 0, 64, 0, 0);
724 
725 	input_mt_init_slots(input, data->max_fingers, INPUT_MT_POINTER);
726 
727 	__set_bit(EV_KEY, input->evbit);
728 
729 	if (data->btn_cnt == 1)
730 		__set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
731 
732 	for (i = 0; i < data->btn_cnt; i++)
733 		__set_bit(BTN_LEFT + i, input->keybit);
734 
735 	/* Stick device initialization */
736 	if (data->has_sp) {
737 		input2 = input_allocate_device();
738 		if (!input2) {
739 			ret = -ENOMEM;
740 			goto exit;
741 		}
742 
743 		input2->phys = input->phys;
744 		input2->name = "DualPoint Stick";
745 		input2->id.bustype = BUS_I2C;
746 		input2->id.vendor  = input->id.vendor;
747 		input2->id.product = input->id.product;
748 		input2->id.version = input->id.version;
749 		input2->dev.parent = input->dev.parent;
750 
751 		input_set_drvdata(input2, hdev);
752 		input2->open = alps_sp_open;
753 		input2->close = alps_sp_close;
754 
755 		__set_bit(EV_KEY, input2->evbit);
756 		data->sp_btn_cnt = (data->sp_btn_info & 0x0F);
757 		for (i = 0; i < data->sp_btn_cnt; i++)
758 			__set_bit(BTN_LEFT + i, input2->keybit);
759 
760 		__set_bit(EV_REL, input2->evbit);
761 		__set_bit(REL_X, input2->relbit);
762 		__set_bit(REL_Y, input2->relbit);
763 		__set_bit(INPUT_PROP_POINTER, input2->propbit);
764 		__set_bit(INPUT_PROP_POINTING_STICK, input2->propbit);
765 
766 		if (input_register_device(input2)) {
767 			input_free_device(input2);
768 			ret = -ENOENT;
769 			goto exit;
770 		}
771 		data->input2 = input2;
772 	}
773 
774 exit:
775 	hid_device_io_stop(hdev);
776 	hid_hw_close(hdev);
777 	return ret;
778 }
779 
alps_input_mapping(struct hid_device * hdev,struct hid_input * hi,struct hid_field * field,struct hid_usage * usage,unsigned long ** bit,int * max)780 static int alps_input_mapping(struct hid_device *hdev,
781 		struct hid_input *hi, struct hid_field *field,
782 		struct hid_usage *usage, unsigned long **bit, int *max)
783 {
784 	return -1;
785 }
786 
alps_probe(struct hid_device * hdev,const struct hid_device_id * id)787 static int alps_probe(struct hid_device *hdev, const struct hid_device_id *id)
788 {
789 	struct alps_dev *data = NULL;
790 	int ret;
791 	data = devm_kzalloc(&hdev->dev, sizeof(struct alps_dev), GFP_KERNEL);
792 	if (!data)
793 		return -ENOMEM;
794 
795 	data->hdev = hdev;
796 	hid_set_drvdata(hdev, data);
797 
798 	hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
799 
800 	ret = hid_parse(hdev);
801 	if (ret) {
802 		hid_err(hdev, "parse failed\n");
803 		return ret;
804 	}
805 
806 	switch (hdev->product) {
807 	case HID_DEVICE_ID_ALPS_T4_BTNLESS:
808 		data->dev_type = T4;
809 		break;
810 	case HID_DEVICE_ID_ALPS_U1_DUAL:
811 	case HID_DEVICE_ID_ALPS_U1:
812 	case HID_DEVICE_ID_ALPS_U1_UNICORN_LEGACY:
813 		data->dev_type = U1;
814 		break;
815 	default:
816 		data->dev_type = UNKNOWN;
817 	}
818 
819 	ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
820 	if (ret) {
821 		hid_err(hdev, "hw start failed\n");
822 		return ret;
823 	}
824 
825 	return 0;
826 }
827 
alps_remove(struct hid_device * hdev)828 static void alps_remove(struct hid_device *hdev)
829 {
830 	struct alps_dev *data = hid_get_drvdata(hdev);
831 
832 	/*
833 	 * input2 ("DualPoint Stick") is allocated separately and is not
834 	 * tracked in hdev->inputs, so the default remove path
835 	 * (hid_hw_stop -> hidinput_disconnect) does not unregister it.
836 	 *
837 	 * Stop the device first so that no URB callback can touch input2
838 	 * while it is being unregistered, then drop it explicitly.
839 	 */
840 	hid_hw_stop(hdev);
841 
842 	if (data->input2)
843 		input_unregister_device(data->input2);
844 }
845 
846 static const struct hid_device_id alps_id[] = {
847 	{ HID_DEVICE(HID_BUS_ANY, HID_GROUP_ANY,
848 		USB_VENDOR_ID_ALPS_JP, HID_DEVICE_ID_ALPS_U1_DUAL) },
849 	{ HID_DEVICE(HID_BUS_ANY, HID_GROUP_ANY,
850 		USB_VENDOR_ID_ALPS_JP, HID_DEVICE_ID_ALPS_U1) },
851 	{ HID_DEVICE(HID_BUS_ANY, HID_GROUP_ANY,
852 		USB_VENDOR_ID_ALPS_JP, HID_DEVICE_ID_ALPS_U1_UNICORN_LEGACY) },
853 	{ HID_DEVICE(HID_BUS_ANY, HID_GROUP_ANY,
854 		USB_VENDOR_ID_ALPS_JP, HID_DEVICE_ID_ALPS_T4_BTNLESS) },
855 	{ }
856 };
857 MODULE_DEVICE_TABLE(hid, alps_id);
858 
859 static struct hid_driver alps_driver = {
860 	.name = "hid-alps",
861 	.id_table		= alps_id,
862 	.probe			= alps_probe,
863 	.raw_event		= alps_raw_event,
864 	.input_mapping		= alps_input_mapping,
865 	.input_configured	= alps_input_configured,
866 	.resume			= pm_ptr(alps_post_resume),
867 	.reset_resume		= pm_ptr(alps_post_reset),
868 	.remove			= alps_remove,
869 };
870 
871 module_hid_driver(alps_driver);
872 
873 MODULE_AUTHOR("Masaki Ota <masaki.ota@jp.alps.com>");
874 MODULE_DESCRIPTION("ALPS HID driver");
875 MODULE_LICENSE("GPL");
876