blob: c4ea5c6c9be97f598e37dc8fed257de684d454b8 [file] [log] [blame]
Masaki Ota25627562016-06-16 18:45:57 +09001/*
2 * Copyright (c) 2016 Masaki Ota <masaki.ota@jp.alps.com>
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the Free
6 * Software Foundation; either version 2 of the License, or (at your option)
7 * any later version.
8 */
9
10#include <linux/kernel.h>
11#include <linux/hid.h>
12#include <linux/input.h>
13#include <linux/input/mt.h>
14#include <linux/module.h>
15#include <asm/unaligned.h>
16#include "hid-ids.h"
17
18/* ALPS Device Product ID */
19#define HID_PRODUCT_ID_T3_BTNLESS 0xD0C0
20#define HID_PRODUCT_ID_COSMO 0x1202
21#define HID_PRODUCT_ID_U1_PTP_1 0x1207
22#define HID_PRODUCT_ID_U1 0x1209
23#define HID_PRODUCT_ID_U1_PTP_2 0x120A
24#define HID_PRODUCT_ID_U1_DUAL 0x120B
25#define HID_PRODUCT_ID_T4_BTNLESS 0x120C
26
27#define DEV_SINGLEPOINT 0x01
28#define DEV_DUALPOINT 0x02
29
30#define U1_MOUSE_REPORT_ID 0x01 /* Mouse data ReportID */
31#define U1_ABSOLUTE_REPORT_ID 0x03 /* Absolute data ReportID */
32#define U1_FEATURE_REPORT_ID 0x05 /* Feature ReportID */
33#define U1_SP_ABSOLUTE_REPORT_ID 0x06 /* Feature ReportID */
34
35#define U1_FEATURE_REPORT_LEN 0x08 /* Feature Report Length */
36#define U1_FEATURE_REPORT_LEN_ALL 0x0A
37#define U1_CMD_REGISTER_READ 0xD1
38#define U1_CMD_REGISTER_WRITE 0xD2
39
40#define U1_DEVTYPE_SP_SUPPORT 0x10 /* SP Support */
41#define U1_DISABLE_DEV 0x01
42#define U1_TP_ABS_MODE 0x02
43#define U1_SP_ABS_MODE 0x80
44
45#define ADDRESS_U1_DEV_CTRL_1 0x00800040
46#define ADDRESS_U1_DEVICE_TYP 0x00800043
47#define ADDRESS_U1_NUM_SENS_X 0x00800047
48#define ADDRESS_U1_NUM_SENS_Y 0x00800048
49#define ADDRESS_U1_PITCH_SENS_X 0x00800049
50#define ADDRESS_U1_PITCH_SENS_Y 0x0080004A
51#define ADDRESS_U1_RESO_DWN_ABS 0x0080004E
52#define ADDRESS_U1_PAD_BTN 0x00800052
53#define ADDRESS_U1_SP_BTN 0x0080009F
54
55#define MAX_TOUCHES 5
56
57/**
58 * struct u1_data
59 *
60 * @input: pointer to the kernel input device
61 * @input2: pointer to the kernel input2 device
62 * @hdev: pointer to the struct hid_device
63 *
64 * @dev_ctrl: device control parameter
65 * @dev_type: device type
66 * @sen_line_num_x: number of sensor line of X
67 * @sen_line_num_y: number of sensor line of Y
68 * @pitch_x: sensor pitch of X
69 * @pitch_y: sensor pitch of Y
70 * @resolution: resolution
71 * @btn_info: button information
72 * @x_active_len_mm: active area length of X (mm)
73 * @y_active_len_mm: active area length of Y (mm)
74 * @x_max: maximum x coordinate value
75 * @y_max: maximum y coordinate value
76 * @btn_cnt: number of buttons
77 * @sp_btn_cnt: number of stick buttons
Masaki Ota5d8c7202017-10-06 11:53:14 +090078 * @has_sp: boolean of sp existense
Masaki Ota25627562016-06-16 18:45:57 +090079 */
80struct u1_dev {
81 struct input_dev *input;
82 struct input_dev *input2;
83 struct hid_device *hdev;
84
85 u8 dev_ctrl;
86 u8 dev_type;
87 u8 sen_line_num_x;
88 u8 sen_line_num_y;
89 u8 pitch_x;
90 u8 pitch_y;
91 u8 resolution;
92 u8 btn_info;
93 u8 sp_btn_info;
94 u32 x_active_len_mm;
95 u32 y_active_len_mm;
96 u32 x_max;
97 u32 y_max;
98 u32 btn_cnt;
99 u32 sp_btn_cnt;
Masaki Ota5d8c7202017-10-06 11:53:14 +0900100 u8 has_sp;
Masaki Ota25627562016-06-16 18:45:57 +0900101};
102
Masaki Ota25627562016-06-16 18:45:57 +0900103static int u1_read_write_register(struct hid_device *hdev, u32 address,
104 u8 *read_val, u8 write_val, bool read_flag)
105{
106 int ret, i;
107 u8 check_sum;
108 u8 *input;
109 u8 *readbuf;
110
Masaki Ota819d64e2016-06-22 13:11:08 +0900111 input = kzalloc(U1_FEATURE_REPORT_LEN, GFP_KERNEL);
Masaki Ota25627562016-06-16 18:45:57 +0900112 if (!input)
113 return -ENOMEM;
114
Masaki Ota25627562016-06-16 18:45:57 +0900115 input[0] = U1_FEATURE_REPORT_ID;
116 if (read_flag) {
117 input[1] = U1_CMD_REGISTER_READ;
118 input[6] = 0x00;
119 } else {
120 input[1] = U1_CMD_REGISTER_WRITE;
121 input[6] = write_val;
122 }
123
124 put_unaligned_le32(address, input + 2);
125
126 /* Calculate the checksum */
127 check_sum = U1_FEATURE_REPORT_LEN_ALL;
128 for (i = 0; i < U1_FEATURE_REPORT_LEN - 1; i++)
129 check_sum += input[i];
130
131 input[7] = check_sum;
132 ret = hid_hw_raw_request(hdev, U1_FEATURE_REPORT_ID, input,
Masaki Ota819d64e2016-06-22 13:11:08 +0900133 U1_FEATURE_REPORT_LEN,
134 HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
Masaki Ota25627562016-06-16 18:45:57 +0900135
136 if (ret < 0) {
137 dev_err(&hdev->dev, "failed to read command (%d)\n", ret);
138 goto exit;
139 }
140
141 if (read_flag) {
Masaki Ota819d64e2016-06-22 13:11:08 +0900142 readbuf = kzalloc(U1_FEATURE_REPORT_LEN, GFP_KERNEL);
143 if (!readbuf) {
Axel Lin7ee2eaa2016-09-11 12:09:01 +0800144 ret = -ENOMEM;
145 goto exit;
Masaki Ota819d64e2016-06-22 13:11:08 +0900146 }
147
Masaki Ota25627562016-06-16 18:45:57 +0900148 ret = hid_hw_raw_request(hdev, U1_FEATURE_REPORT_ID, readbuf,
Masaki Ota819d64e2016-06-22 13:11:08 +0900149 U1_FEATURE_REPORT_LEN,
Jiri Kosina63b3a7d2016-06-20 11:16:18 +0200150 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
Masaki Ota25627562016-06-16 18:45:57 +0900151
152 if (ret < 0) {
153 dev_err(&hdev->dev, "failed read register (%d)\n", ret);
Axel Lin7ee2eaa2016-09-11 12:09:01 +0800154 kfree(readbuf);
Masaki Ota25627562016-06-16 18:45:57 +0900155 goto exit;
156 }
157
158 *read_val = readbuf[6];
Masaki Ota819d64e2016-06-22 13:11:08 +0900159
160 kfree(readbuf);
Masaki Ota25627562016-06-16 18:45:57 +0900161 }
162
Masaki Ota819d64e2016-06-22 13:11:08 +0900163 ret = 0;
Masaki Ota25627562016-06-16 18:45:57 +0900164
165exit:
166 kfree(input);
Masaki Ota25627562016-06-16 18:45:57 +0900167 return ret;
168}
169
170static int alps_raw_event(struct hid_device *hdev,
171 struct hid_report *report, u8 *data, int size)
172{
Masaki Ota819d64e2016-06-22 13:11:08 +0900173 unsigned int x, y, z;
174 int i;
175 short sp_x, sp_y;
Masaki Ota25627562016-06-16 18:45:57 +0900176 struct u1_dev *hdata = hid_get_drvdata(hdev);
177
178 switch (data[0]) {
179 case U1_MOUSE_REPORT_ID:
180 break;
181 case U1_FEATURE_REPORT_ID:
182 break;
183 case U1_ABSOLUTE_REPORT_ID:
184 for (i = 0; i < MAX_TOUCHES; i++) {
Masaki Ota819d64e2016-06-22 13:11:08 +0900185 u8 *contact = &data[i * 5];
186
187 x = get_unaligned_le16(contact + 3);
188 y = get_unaligned_le16(contact + 5);
189 z = contact[7] & 0x7F;
Masaki Ota25627562016-06-16 18:45:57 +0900190
191 input_mt_slot(hdata->input, i);
192
Masaki Ota819d64e2016-06-22 13:11:08 +0900193 if (z != 0) {
Masaki Ota25627562016-06-16 18:45:57 +0900194 input_mt_report_slot_state(hdata->input,
195 MT_TOOL_FINGER, 1);
Masaki Ota9a54cf42016-09-27 14:04:37 +0900196 input_report_abs(hdata->input,
197 ABS_MT_POSITION_X, x);
198 input_report_abs(hdata->input,
199 ABS_MT_POSITION_Y, y);
200 input_report_abs(hdata->input,
201 ABS_MT_PRESSURE, z);
Masaki Ota25627562016-06-16 18:45:57 +0900202 } else {
203 input_mt_report_slot_state(hdata->input,
204 MT_TOOL_FINGER, 0);
Masaki Ota25627562016-06-16 18:45:57 +0900205 }
Masaki Ota25627562016-06-16 18:45:57 +0900206 }
207
208 input_mt_sync_frame(hdata->input);
Masaki Ota25627562016-06-16 18:45:57 +0900209
Masaki Ota819d64e2016-06-22 13:11:08 +0900210 input_report_key(hdata->input, BTN_LEFT,
211 data[1] & 0x1);
212 input_report_key(hdata->input, BTN_RIGHT,
213 (data[1] & 0x2));
214 input_report_key(hdata->input, BTN_MIDDLE,
215 (data[1] & 0x4));
216
217 input_sync(hdata->input);
Masaki Ota25627562016-06-16 18:45:57 +0900218
219 return 1;
220
221 case U1_SP_ABSOLUTE_REPORT_ID:
Masaki Ota819d64e2016-06-22 13:11:08 +0900222 sp_x = get_unaligned_le16(data+2);
223 sp_y = get_unaligned_le16(data+4);
Masaki Ota25627562016-06-16 18:45:57 +0900224
225 sp_x = sp_x / 8;
226 sp_y = sp_y / 8;
227
Masaki Ota819d64e2016-06-22 13:11:08 +0900228 input_report_rel(hdata->input2, REL_X, sp_x);
229 input_report_rel(hdata->input2, REL_Y, sp_y);
Masaki Ota25627562016-06-16 18:45:57 +0900230
Masaki Ota819d64e2016-06-22 13:11:08 +0900231 input_report_key(hdata->input2, BTN_LEFT,
232 data[1] & 0x1);
233 input_report_key(hdata->input2, BTN_RIGHT,
234 (data[1] & 0x2));
235 input_report_key(hdata->input2, BTN_MIDDLE,
236 (data[1] & 0x4));
Masaki Ota25627562016-06-16 18:45:57 +0900237
Masaki Ota819d64e2016-06-22 13:11:08 +0900238 input_sync(hdata->input2);
Masaki Ota25627562016-06-16 18:45:57 +0900239
240 return 1;
241 }
242
243 return 0;
244}
245
246#ifdef CONFIG_PM
247static int alps_post_reset(struct hid_device *hdev)
248{
249 return u1_read_write_register(hdev, ADDRESS_U1_DEV_CTRL_1,
Kai-Heng Feng04fd4cb2016-09-19 15:41:39 +0800250 NULL, U1_TP_ABS_MODE | U1_SP_ABS_MODE, false);
Masaki Ota25627562016-06-16 18:45:57 +0900251}
252
253static int alps_post_resume(struct hid_device *hdev)
254{
255 return u1_read_write_register(hdev, ADDRESS_U1_DEV_CTRL_1,
Kai-Heng Feng04fd4cb2016-09-19 15:41:39 +0800256 NULL, U1_TP_ABS_MODE | U1_SP_ABS_MODE, false);
Masaki Ota25627562016-06-16 18:45:57 +0900257}
258#endif /* CONFIG_PM */
259
Masaki Ota5d8c7202017-10-06 11:53:14 +0900260static int u1_init(struct hid_device *hdev, struct u1_dev *pri_data)
261{
262 int ret;
263
264 /* Device initialization */
265 ret = u1_read_write_register(hdev, ADDRESS_U1_DEV_CTRL_1,
266 &pri_data->dev_ctrl, 0, true);
267 if (ret < 0) {
268 dev_err(&hdev->dev, "failed U1_DEV_CTRL_1 (%d)\n", ret);
269 goto exit;
270 }
271
272 pri_data->dev_ctrl &= ~U1_DISABLE_DEV;
273 pri_data->dev_ctrl |= U1_TP_ABS_MODE;
274 ret = u1_read_write_register(hdev, ADDRESS_U1_DEV_CTRL_1,
275 NULL, pri_data->dev_ctrl, false);
276 if (ret < 0) {
277 dev_err(&hdev->dev, "failed to change TP mode (%d)\n", ret);
278 goto exit;
279 }
280
281 ret = u1_read_write_register(hdev, ADDRESS_U1_NUM_SENS_X,
282 &pri_data->sen_line_num_x, 0, true);
283 if (ret < 0) {
284 dev_err(&hdev->dev, "failed U1_NUM_SENS_X (%d)\n", ret);
285 goto exit;
286 }
287
288 ret = u1_read_write_register(hdev, ADDRESS_U1_NUM_SENS_Y,
289 &pri_data->sen_line_num_y, 0, true);
290 if (ret < 0) {
291 dev_err(&hdev->dev, "failed U1_NUM_SENS_Y (%d)\n", ret);
292 goto exit;
293 }
294
295 ret = u1_read_write_register(hdev, ADDRESS_U1_PITCH_SENS_X,
296 &pri_data->pitch_x, 0, true);
297 if (ret < 0) {
298 dev_err(&hdev->dev, "failed U1_PITCH_SENS_X (%d)\n", ret);
299 goto exit;
300 }
301
302 ret = u1_read_write_register(hdev, ADDRESS_U1_PITCH_SENS_Y,
303 &pri_data->pitch_y, 0, true);
304 if (ret < 0) {
305 dev_err(&hdev->dev, "failed U1_PITCH_SENS_Y (%d)\n", ret);
306 goto exit;
307 }
308
309 ret = u1_read_write_register(hdev, ADDRESS_U1_RESO_DWN_ABS,
310 &pri_data->resolution, 0, true);
311 if (ret < 0) {
312 dev_err(&hdev->dev, "failed U1_RESO_DWN_ABS (%d)\n", ret);
313 goto exit;
314 }
315 pri_data->x_active_len_mm =
316 (pri_data->pitch_x * (pri_data->sen_line_num_x - 1)) / 10;
317 pri_data->y_active_len_mm =
318 (pri_data->pitch_y * (pri_data->sen_line_num_y - 1)) / 10;
319
320 pri_data->x_max =
321 (pri_data->resolution << 2) * (pri_data->sen_line_num_x - 1);
322 pri_data->y_max =
323 (pri_data->resolution << 2) * (pri_data->sen_line_num_y - 1);
324
325 ret = u1_read_write_register(hdev, ADDRESS_U1_PAD_BTN,
326 &pri_data->btn_info, 0, true);
327 if (ret < 0) {
328 dev_err(&hdev->dev, "failed U1_PAD_BTN (%d)\n", ret);
329 goto exit;
330 }
331
332 pri_data->has_sp = 0;
333 /* Check StickPointer device */
334 ret = u1_read_write_register(hdev, ADDRESS_U1_DEVICE_TYP,
335 &pri_data->dev_type, 0, true);
336 if (ret < 0) {
337 dev_err(&hdev->dev, "failed U1_DEVICE_TYP (%d)\n", ret);
338 goto exit;
339 }
340
341 if (pri_data->dev_type & U1_DEVTYPE_SP_SUPPORT) {
342 pri_data->dev_ctrl |= U1_SP_ABS_MODE;
343 ret = u1_read_write_register(hdev, ADDRESS_U1_DEV_CTRL_1,
344 NULL, pri_data->dev_ctrl, false);
345 if (ret < 0) {
346 dev_err(&hdev->dev, "failed SP mode (%d)\n", ret);
347 goto exit;
348 }
349
350 ret = u1_read_write_register(hdev, ADDRESS_U1_SP_BTN,
351 &pri_data->sp_btn_info, 0, true);
352 if (ret < 0) {
353 dev_err(&hdev->dev, "failed U1_SP_BTN (%d)\n", ret);
354 goto exit;
355 }
356 pri_data->has_sp = 1;
357 }
358
359exit:
360 return ret;
361}
362
Masaki Ota25627562016-06-16 18:45:57 +0900363static int alps_input_configured(struct hid_device *hdev, struct hid_input *hi)
364{
365 struct u1_dev *data = hid_get_drvdata(hdev);
366 struct input_dev *input = hi->input, *input2;
Masaki Ota25627562016-06-16 18:45:57 +0900367 int ret;
368 int res_x, res_y, i;
369
Masaki Ota25627562016-06-16 18:45:57 +0900370 data->input = input;
371
372 hid_dbg(hdev, "Opening low level driver\n");
373 ret = hid_hw_open(hdev);
374 if (ret)
375 return ret;
376
377 /* Allow incoming hid reports */
378 hid_device_io_start(hdev);
379
Masaki Ota5d8c7202017-10-06 11:53:14 +0900380 ret = u1_init(hdev, data);
381
382 if (ret)
Masaki Ota25627562016-06-16 18:45:57 +0900383 goto exit;
Masaki Ota25627562016-06-16 18:45:57 +0900384
385 __set_bit(EV_ABS, input->evbit);
Masaki Otace6abcf2017-10-06 11:53:13 +0900386 input_set_abs_params(input, ABS_MT_POSITION_X, 1, data->x_max, 0, 0);
387 input_set_abs_params(input, ABS_MT_POSITION_Y, 1, data->y_max, 0, 0);
Masaki Ota25627562016-06-16 18:45:57 +0900388
Masaki Otace6abcf2017-10-06 11:53:13 +0900389 if (data->x_active_len_mm && data->y_active_len_mm) {
390 res_x = (data->x_max - 1) / data->x_active_len_mm;
391 res_y = (data->y_max - 1) / data->y_active_len_mm;
Masaki Ota25627562016-06-16 18:45:57 +0900392
393 input_abs_set_res(input, ABS_MT_POSITION_X, res_x);
394 input_abs_set_res(input, ABS_MT_POSITION_Y, res_y);
395 }
396
397 input_set_abs_params(input, ABS_MT_PRESSURE, 0, 64, 0, 0);
398
399 input_mt_init_slots(input, MAX_TOUCHES, INPUT_MT_POINTER);
400
401 __set_bit(EV_KEY, input->evbit);
Masaki Otace6abcf2017-10-06 11:53:13 +0900402 if ((data->btn_info & 0x0F) == (data->btn_info & 0xF0) >> 4) {
403 data->btn_cnt = (data->btn_info & 0x0F);
Masaki Ota25627562016-06-16 18:45:57 +0900404 } else {
405 /* Button pad */
Masaki Otace6abcf2017-10-06 11:53:13 +0900406 data->btn_cnt = 1;
Masaki Ota25627562016-06-16 18:45:57 +0900407 __set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
408 }
409
Masaki Otace6abcf2017-10-06 11:53:13 +0900410 for (i = 0; i < data->btn_cnt; i++)
Masaki Ota25627562016-06-16 18:45:57 +0900411 __set_bit(BTN_LEFT + i, input->keybit);
412
413
414 /* Stick device initialization */
Masaki Ota5d8c7202017-10-06 11:53:14 +0900415 if (data->has_sp) {
Masaki Ota25627562016-06-16 18:45:57 +0900416 input2 = input_allocate_device();
417 if (!input2) {
Wei Yongjun46a41b52016-09-21 15:12:05 +0000418 ret = -ENOMEM;
Masaki Ota25627562016-06-16 18:45:57 +0900419 goto exit;
420 }
421
Masaki Ota819d64e2016-06-22 13:11:08 +0900422 data->input2 = input2;
Masaki Ota25627562016-06-16 18:45:57 +0900423 input2->phys = input->phys;
424 input2->name = "DualPoint Stick";
425 input2->id.bustype = BUS_I2C;
426 input2->id.vendor = input->id.vendor;
427 input2->id.product = input->id.product;
428 input2->id.version = input->id.version;
429 input2->dev.parent = input->dev.parent;
430
431 __set_bit(EV_KEY, input2->evbit);
Masaki Otace6abcf2017-10-06 11:53:13 +0900432 data->sp_btn_cnt = (data->sp_btn_info & 0x0F);
433 for (i = 0; i < data->sp_btn_cnt; i++)
Masaki Ota25627562016-06-16 18:45:57 +0900434 __set_bit(BTN_LEFT + i, input2->keybit);
435
436 __set_bit(EV_REL, input2->evbit);
437 __set_bit(REL_X, input2->relbit);
438 __set_bit(REL_Y, input2->relbit);
439 __set_bit(INPUT_PROP_POINTER, input2->propbit);
440 __set_bit(INPUT_PROP_POINTING_STICK, input2->propbit);
441
Wei Yongjun46a41b52016-09-21 15:12:05 +0000442 ret = input_register_device(data->input2);
443 if (ret) {
Masaki Ota25627562016-06-16 18:45:57 +0900444 input_free_device(input2);
445 goto exit;
446 }
447 }
448
449exit:
450 hid_device_io_stop(hdev);
451 hid_hw_close(hdev);
452 return ret;
453}
454
455static int alps_input_mapping(struct hid_device *hdev,
456 struct hid_input *hi, struct hid_field *field,
457 struct hid_usage *usage, unsigned long **bit, int *max)
458{
459 return -1;
460}
461
462static int alps_probe(struct hid_device *hdev, const struct hid_device_id *id)
463{
464 struct u1_dev *data = NULL;
465 int ret;
466
467 data = devm_kzalloc(&hdev->dev, sizeof(struct u1_dev), GFP_KERNEL);
468 if (!data)
469 return -ENOMEM;
470
471 data->hdev = hdev;
472 hid_set_drvdata(hdev, data);
473
474 hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
475
476 ret = hid_parse(hdev);
477 if (ret) {
478 hid_err(hdev, "parse failed\n");
479 return ret;
480 }
481
482 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
483 if (ret) {
484 hid_err(hdev, "hw start failed\n");
485 return ret;
486 }
487
488 return 0;
489}
490
491static void alps_remove(struct hid_device *hdev)
492{
493 hid_hw_stop(hdev);
Masaki Ota25627562016-06-16 18:45:57 +0900494}
495
496static const struct hid_device_id alps_id[] = {
497 { HID_DEVICE(HID_BUS_ANY, HID_GROUP_ANY,
Masaki Ota819d64e2016-06-22 13:11:08 +0900498 USB_VENDOR_ID_ALPS_JP, HID_DEVICE_ID_ALPS_U1_DUAL) },
Masaki Ota25627562016-06-16 18:45:57 +0900499 { }
500};
501MODULE_DEVICE_TABLE(hid, alps_id);
502
503static struct hid_driver alps_driver = {
504 .name = "hid-alps",
505 .id_table = alps_id,
506 .probe = alps_probe,
507 .remove = alps_remove,
508 .raw_event = alps_raw_event,
509 .input_mapping = alps_input_mapping,
510 .input_configured = alps_input_configured,
511#ifdef CONFIG_PM
512 .resume = alps_post_resume,
513 .reset_resume = alps_post_reset,
514#endif
515};
516
517module_hid_driver(alps_driver);
518
519MODULE_AUTHOR("Masaki Ota <masaki.ota@jp.alps.com>");
520MODULE_DESCRIPTION("ALPS HID driver");
521MODULE_LICENSE("GPL");