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