HID: alps: remove variables local to u1_init() from the device struct
[linux-block.git] / drivers / hid / hid-alps.c
CommitLineData
2562756d
MO
1/*
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 *
2562756d 64 * @dev_type: device type
5992262d
MO
65 * @max_fingers: total number of fingers
66 * @has_sp: boolean of sp existense
67 * @sp_btn_info: button information
2562756d
MO
68 * @x_active_len_mm: active area length of X (mm)
69 * @y_active_len_mm: active area length of Y (mm)
70 * @x_max: maximum x coordinate value
71 * @y_max: maximum y coordinate value
c7083d3f
MO
72 * @x_min: minimum x coordinate value
73 * @y_min: minimum y coordinate value
2562756d
MO
74 * @btn_cnt: number of buttons
75 * @sp_btn_cnt: number of stick buttons
76 */
77struct u1_dev {
78 struct input_dev *input;
79 struct input_dev *input2;
80 struct hid_device *hdev;
81
5992262d
MO
82 u8 max_fingers;
83 u8 has_sp;
2562756d
MO
84 u8 sp_btn_info;
85 u32 x_active_len_mm;
86 u32 y_active_len_mm;
87 u32 x_max;
88 u32 y_max;
c7083d3f
MO
89 u32 x_min;
90 u32 y_min;
2562756d
MO
91 u32 btn_cnt;
92 u32 sp_btn_cnt;
93};
94
2562756d
MO
95static int u1_read_write_register(struct hid_device *hdev, u32 address,
96 u8 *read_val, u8 write_val, bool read_flag)
97{
98 int ret, i;
99 u8 check_sum;
100 u8 *input;
101 u8 *readbuf;
102
819d64e5 103 input = kzalloc(U1_FEATURE_REPORT_LEN, GFP_KERNEL);
2562756d
MO
104 if (!input)
105 return -ENOMEM;
106
2562756d
MO
107 input[0] = U1_FEATURE_REPORT_ID;
108 if (read_flag) {
109 input[1] = U1_CMD_REGISTER_READ;
110 input[6] = 0x00;
111 } else {
112 input[1] = U1_CMD_REGISTER_WRITE;
113 input[6] = write_val;
114 }
115
116 put_unaligned_le32(address, input + 2);
117
118 /* Calculate the checksum */
119 check_sum = U1_FEATURE_REPORT_LEN_ALL;
120 for (i = 0; i < U1_FEATURE_REPORT_LEN - 1; i++)
121 check_sum += input[i];
122
123 input[7] = check_sum;
124 ret = hid_hw_raw_request(hdev, U1_FEATURE_REPORT_ID, input,
819d64e5
MO
125 U1_FEATURE_REPORT_LEN,
126 HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
2562756d
MO
127
128 if (ret < 0) {
129 dev_err(&hdev->dev, "failed to read command (%d)\n", ret);
130 goto exit;
131 }
132
133 if (read_flag) {
819d64e5
MO
134 readbuf = kzalloc(U1_FEATURE_REPORT_LEN, GFP_KERNEL);
135 if (!readbuf) {
7ee2eaa3
AL
136 ret = -ENOMEM;
137 goto exit;
819d64e5
MO
138 }
139
2562756d 140 ret = hid_hw_raw_request(hdev, U1_FEATURE_REPORT_ID, readbuf,
819d64e5 141 U1_FEATURE_REPORT_LEN,
63b3a7d0 142 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
2562756d
MO
143
144 if (ret < 0) {
145 dev_err(&hdev->dev, "failed read register (%d)\n", ret);
7ee2eaa3 146 kfree(readbuf);
2562756d
MO
147 goto exit;
148 }
149
150 *read_val = readbuf[6];
819d64e5
MO
151
152 kfree(readbuf);
2562756d
MO
153 }
154
819d64e5 155 ret = 0;
2562756d
MO
156
157exit:
158 kfree(input);
2562756d
MO
159 return ret;
160}
161
162static int alps_raw_event(struct hid_device *hdev,
163 struct hid_report *report, u8 *data, int size)
164{
819d64e5
MO
165 unsigned int x, y, z;
166 int i;
167 short sp_x, sp_y;
2562756d
MO
168 struct u1_dev *hdata = hid_get_drvdata(hdev);
169
170 switch (data[0]) {
171 case U1_MOUSE_REPORT_ID:
172 break;
173 case U1_FEATURE_REPORT_ID:
174 break;
175 case U1_ABSOLUTE_REPORT_ID:
176 for (i = 0; i < MAX_TOUCHES; i++) {
819d64e5
MO
177 u8 *contact = &data[i * 5];
178
179 x = get_unaligned_le16(contact + 3);
180 y = get_unaligned_le16(contact + 5);
181 z = contact[7] & 0x7F;
2562756d
MO
182
183 input_mt_slot(hdata->input, i);
184
819d64e5 185 if (z != 0) {
2562756d
MO
186 input_mt_report_slot_state(hdata->input,
187 MT_TOOL_FINGER, 1);
9a54cf46
MO
188 input_report_abs(hdata->input,
189 ABS_MT_POSITION_X, x);
190 input_report_abs(hdata->input,
191 ABS_MT_POSITION_Y, y);
192 input_report_abs(hdata->input,
193 ABS_MT_PRESSURE, z);
2562756d
MO
194 } else {
195 input_mt_report_slot_state(hdata->input,
196 MT_TOOL_FINGER, 0);
2562756d 197 }
2562756d
MO
198 }
199
200 input_mt_sync_frame(hdata->input);
2562756d 201
819d64e5
MO
202 input_report_key(hdata->input, BTN_LEFT,
203 data[1] & 0x1);
204 input_report_key(hdata->input, BTN_RIGHT,
205 (data[1] & 0x2));
206 input_report_key(hdata->input, BTN_MIDDLE,
207 (data[1] & 0x4));
208
209 input_sync(hdata->input);
2562756d
MO
210
211 return 1;
212
213 case U1_SP_ABSOLUTE_REPORT_ID:
819d64e5
MO
214 sp_x = get_unaligned_le16(data+2);
215 sp_y = get_unaligned_le16(data+4);
2562756d
MO
216
217 sp_x = sp_x / 8;
218 sp_y = sp_y / 8;
219
819d64e5
MO
220 input_report_rel(hdata->input2, REL_X, sp_x);
221 input_report_rel(hdata->input2, REL_Y, sp_y);
2562756d 222
819d64e5
MO
223 input_report_key(hdata->input2, BTN_LEFT,
224 data[1] & 0x1);
225 input_report_key(hdata->input2, BTN_RIGHT,
226 (data[1] & 0x2));
227 input_report_key(hdata->input2, BTN_MIDDLE,
228 (data[1] & 0x4));
2562756d 229
819d64e5 230 input_sync(hdata->input2);
2562756d
MO
231
232 return 1;
233 }
234
235 return 0;
236}
237
238#ifdef CONFIG_PM
239static int alps_post_reset(struct hid_device *hdev)
240{
241 return u1_read_write_register(hdev, ADDRESS_U1_DEV_CTRL_1,
04fd4cb0 242 NULL, U1_TP_ABS_MODE | U1_SP_ABS_MODE, false);
2562756d
MO
243}
244
245static int alps_post_resume(struct hid_device *hdev)
246{
247 return u1_read_write_register(hdev, ADDRESS_U1_DEV_CTRL_1,
04fd4cb0 248 NULL, U1_TP_ABS_MODE | U1_SP_ABS_MODE, false);
2562756d
MO
249}
250#endif /* CONFIG_PM */
251
5d8c720d 252static int u1_init(struct hid_device *hdev, struct u1_dev *pri_data)
2562756d 253{
2562756d 254 int ret;
5992262d
MO
255 u8 tmp, dev_ctrl, sen_line_num_x, sen_line_num_y;
256 u8 pitch_x, pitch_y, resolution;
2562756d
MO
257
258 /* Device initialization */
259 ret = u1_read_write_register(hdev, ADDRESS_U1_DEV_CTRL_1,
5992262d 260 &dev_ctrl, 0, true);
2562756d
MO
261 if (ret < 0) {
262 dev_err(&hdev->dev, "failed U1_DEV_CTRL_1 (%d)\n", ret);
263 goto exit;
264 }
265
5992262d
MO
266 dev_ctrl &= ~U1_DISABLE_DEV;
267 dev_ctrl |= U1_TP_ABS_MODE;
2562756d 268 ret = u1_read_write_register(hdev, ADDRESS_U1_DEV_CTRL_1,
5992262d 269 NULL, dev_ctrl, false);
2562756d
MO
270 if (ret < 0) {
271 dev_err(&hdev->dev, "failed to change TP mode (%d)\n", ret);
272 goto exit;
273 }
274
275 ret = u1_read_write_register(hdev, ADDRESS_U1_NUM_SENS_X,
5992262d 276 &sen_line_num_x, 0, true);
2562756d
MO
277 if (ret < 0) {
278 dev_err(&hdev->dev, "failed U1_NUM_SENS_X (%d)\n", ret);
279 goto exit;
280 }
281
282 ret = u1_read_write_register(hdev, ADDRESS_U1_NUM_SENS_Y,
5992262d 283 &sen_line_num_y, 0, true);
2562756d
MO
284 if (ret < 0) {
285 dev_err(&hdev->dev, "failed U1_NUM_SENS_Y (%d)\n", ret);
286 goto exit;
287 }
288
289 ret = u1_read_write_register(hdev, ADDRESS_U1_PITCH_SENS_X,
5992262d 290 &pitch_x, 0, true);
2562756d
MO
291 if (ret < 0) {
292 dev_err(&hdev->dev, "failed U1_PITCH_SENS_X (%d)\n", ret);
293 goto exit;
294 }
295
296 ret = u1_read_write_register(hdev, ADDRESS_U1_PITCH_SENS_Y,
5992262d 297 &pitch_y, 0, true);
2562756d
MO
298 if (ret < 0) {
299 dev_err(&hdev->dev, "failed U1_PITCH_SENS_Y (%d)\n", ret);
300 goto exit;
301 }
302
303 ret = u1_read_write_register(hdev, ADDRESS_U1_RESO_DWN_ABS,
5992262d 304 &resolution, 0, true);
2562756d
MO
305 if (ret < 0) {
306 dev_err(&hdev->dev, "failed U1_RESO_DWN_ABS (%d)\n", ret);
307 goto exit;
308 }
5d8c720d 309 pri_data->x_active_len_mm =
5992262d 310 (pitch_x * (sen_line_num_x - 1)) / 10;
5d8c720d 311 pri_data->y_active_len_mm =
5992262d 312 (pitch_y * (sen_line_num_y - 1)) / 10;
5d8c720d
MO
313
314 pri_data->x_max =
5992262d 315 (resolution << 2) * (sen_line_num_x - 1);
c7083d3f 316 pri_data->x_min = 1;
5d8c720d 317 pri_data->y_max =
5992262d 318 (resolution << 2) * (sen_line_num_y - 1);
c7083d3f 319 pri_data->y_min = 1;
2562756d
MO
320
321 ret = u1_read_write_register(hdev, ADDRESS_U1_PAD_BTN,
5992262d 322 &tmp, 0, true);
2562756d
MO
323 if (ret < 0) {
324 dev_err(&hdev->dev, "failed U1_PAD_BTN (%d)\n", ret);
325 goto exit;
326 }
5992262d
MO
327 if ((tmp & 0x0F) == (tmp & 0xF0) >> 4) {
328 pri_data->btn_cnt = (tmp & 0x0F);
c7083d3f
MO
329 } else {
330 /* Button pad */
331 pri_data->btn_cnt = 1;
332 }
2562756d 333
5d8c720d 334 pri_data->has_sp = 0;
2562756d
MO
335 /* Check StickPointer device */
336 ret = u1_read_write_register(hdev, ADDRESS_U1_DEVICE_TYP,
5992262d 337 &tmp, 0, true);
2562756d
MO
338 if (ret < 0) {
339 dev_err(&hdev->dev, "failed U1_DEVICE_TYP (%d)\n", ret);
340 goto exit;
341 }
5992262d
MO
342 if (tmp & U1_DEVTYPE_SP_SUPPORT) {
343 dev_ctrl |= U1_SP_ABS_MODE;
5d8c720d 344 ret = u1_read_write_register(hdev, ADDRESS_U1_DEV_CTRL_1,
5992262d 345 NULL, dev_ctrl, false);
5d8c720d
MO
346 if (ret < 0) {
347 dev_err(&hdev->dev, "failed SP mode (%d)\n", ret);
348 goto exit;
349 }
350
351 ret = u1_read_write_register(hdev, ADDRESS_U1_SP_BTN,
352 &pri_data->sp_btn_info, 0, true);
353 if (ret < 0) {
354 dev_err(&hdev->dev, "failed U1_SP_BTN (%d)\n", ret);
355 goto exit;
356 }
357 pri_data->has_sp = 1;
358 }
c7083d3f 359 pri_data->max_fingers = 5;
5d8c720d
MO
360exit:
361 return ret;
362}
363
364static int alps_input_configured(struct hid_device *hdev, struct hid_input *hi)
365{
366 struct u1_dev *data = hid_get_drvdata(hdev);
367 struct input_dev *input = hi->input, *input2;
368 int ret;
369 int res_x, res_y, i;
370
371 data->input = input;
372
373 hid_dbg(hdev, "Opening low level driver\n");
374 ret = hid_hw_open(hdev);
375 if (ret)
376 return ret;
377
378 /* Allow incoming hid reports */
379 hid_device_io_start(hdev);
380
381 ret = u1_init(hdev, data);
382
383 if (ret)
384 goto exit;
2562756d
MO
385
386 __set_bit(EV_ABS, input->evbit);
c7083d3f
MO
387 input_set_abs_params(input, ABS_MT_POSITION_X,
388 data->x_min, data->x_max, 0, 0);
389 input_set_abs_params(input, ABS_MT_POSITION_Y,
390 data->y_min, data->y_max, 0, 0);
2562756d 391
ce6abcf8
MO
392 if (data->x_active_len_mm && data->y_active_len_mm) {
393 res_x = (data->x_max - 1) / data->x_active_len_mm;
394 res_y = (data->y_max - 1) / data->y_active_len_mm;
2562756d
MO
395
396 input_abs_set_res(input, ABS_MT_POSITION_X, res_x);
397 input_abs_set_res(input, ABS_MT_POSITION_Y, res_y);
398 }
399
400 input_set_abs_params(input, ABS_MT_PRESSURE, 0, 64, 0, 0);
401
c7083d3f 402 input_mt_init_slots(input, data->max_fingers, INPUT_MT_POINTER);
2562756d
MO
403
404 __set_bit(EV_KEY, input->evbit);
c7083d3f
MO
405
406 if (data->btn_cnt == 1)
2562756d 407 __set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
2562756d 408
ce6abcf8 409 for (i = 0; i < data->btn_cnt; i++)
2562756d
MO
410 __set_bit(BTN_LEFT + i, input->keybit);
411
2562756d 412 /* Stick device initialization */
5d8c720d 413 if (data->has_sp) {
2562756d
MO
414 input2 = input_allocate_device();
415 if (!input2) {
c7083d3f 416 input_free_device(input2);
2562756d
MO
417 goto exit;
418 }
419
819d64e5 420 data->input2 = input2;
2562756d
MO
421 input2->phys = input->phys;
422 input2->name = "DualPoint Stick";
423 input2->id.bustype = BUS_I2C;
424 input2->id.vendor = input->id.vendor;
425 input2->id.product = input->id.product;
426 input2->id.version = input->id.version;
427 input2->dev.parent = input->dev.parent;
428
429 __set_bit(EV_KEY, input2->evbit);
ce6abcf8
MO
430 data->sp_btn_cnt = (data->sp_btn_info & 0x0F);
431 for (i = 0; i < data->sp_btn_cnt; i++)
2562756d
MO
432 __set_bit(BTN_LEFT + i, input2->keybit);
433
434 __set_bit(EV_REL, input2->evbit);
435 __set_bit(REL_X, input2->relbit);
436 __set_bit(REL_Y, input2->relbit);
437 __set_bit(INPUT_PROP_POINTER, input2->propbit);
438 __set_bit(INPUT_PROP_POINTING_STICK, input2->propbit);
439
c7083d3f 440 if (input_register_device(data->input2)) {
2562756d
MO
441 input_free_device(input2);
442 goto exit;
443 }
444 }
445
446exit:
447 hid_device_io_stop(hdev);
448 hid_hw_close(hdev);
449 return ret;
450}
451
452static int alps_input_mapping(struct hid_device *hdev,
453 struct hid_input *hi, struct hid_field *field,
454 struct hid_usage *usage, unsigned long **bit, int *max)
455{
456 return -1;
457}
458
459static int alps_probe(struct hid_device *hdev, const struct hid_device_id *id)
460{
461 struct u1_dev *data = NULL;
462 int ret;
463
464 data = devm_kzalloc(&hdev->dev, sizeof(struct u1_dev), GFP_KERNEL);
465 if (!data)
466 return -ENOMEM;
467
468 data->hdev = hdev;
469 hid_set_drvdata(hdev, data);
470
471 hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
472
473 ret = hid_parse(hdev);
474 if (ret) {
475 hid_err(hdev, "parse failed\n");
476 return ret;
477 }
478
479 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
480 if (ret) {
481 hid_err(hdev, "hw start failed\n");
482 return ret;
483 }
484
485 return 0;
486}
487
488static void alps_remove(struct hid_device *hdev)
489{
490 hid_hw_stop(hdev);
2562756d
MO
491}
492
493static const struct hid_device_id alps_id[] = {
494 { HID_DEVICE(HID_BUS_ANY, HID_GROUP_ANY,
819d64e5 495 USB_VENDOR_ID_ALPS_JP, HID_DEVICE_ID_ALPS_U1_DUAL) },
2562756d
MO
496 { }
497};
498MODULE_DEVICE_TABLE(hid, alps_id);
499
500static struct hid_driver alps_driver = {
501 .name = "hid-alps",
502 .id_table = alps_id,
503 .probe = alps_probe,
504 .remove = alps_remove,
505 .raw_event = alps_raw_event,
506 .input_mapping = alps_input_mapping,
507 .input_configured = alps_input_configured,
508#ifdef CONFIG_PM
509 .resume = alps_post_resume,
510 .reset_resume = alps_post_reset,
511#endif
512};
513
514module_hid_driver(alps_driver);
515
516MODULE_AUTHOR("Masaki Ota <masaki.ota@jp.alps.com>");
517MODULE_DESCRIPTION("ALPS HID driver");
518MODULE_LICENSE("GPL");