2 * Copyright (c) 2016 Masaki Ota <masaki.ota@jp.alps.com>
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)
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>
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
27 #define DEV_SINGLEPOINT 0x01
28 #define DEV_DUALPOINT 0x02
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 */
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
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
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
60 * @input: pointer to the kernel input device
61 * @input2: pointer to the kernel input2 device
62 * @hdev: pointer to the struct hid_device
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
80 struct input_dev
*input
;
81 struct input_dev
*input2
;
82 struct hid_device
*hdev
;
101 static struct u1_dev
*priv
;
103 static int u1_read_write_register(struct hid_device
*hdev
, u32 address
,
104 u8
*read_val
, u8 write_val
, bool read_flag
)
111 input
= kzalloc(sizeof(u8
)*U1_FEATURE_REPORT_LEN
, GFP_KERNEL
);
115 readbuf
= kzalloc(sizeof(u8
)*U1_FEATURE_REPORT_LEN
, GFP_KERNEL
);
121 input
[0] = U1_FEATURE_REPORT_ID
;
123 input
[1] = U1_CMD_REGISTER_READ
;
126 input
[1] = U1_CMD_REGISTER_WRITE
;
127 input
[6] = write_val
;
130 put_unaligned_le32(address
, input
+ 2);
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
];
137 input
[7] = check_sum
;
138 ret
= hid_hw_raw_request(hdev
, U1_FEATURE_REPORT_ID
, input
,
139 sizeof(input
), HID_FEATURE_REPORT
, HID_REQ_SET_REPORT
);
142 dev_err(&hdev
->dev
, "failed to read command (%d)\n", ret
);
147 ret
= hid_hw_raw_request(hdev
, U1_FEATURE_REPORT_ID
, readbuf
,
148 sizeof(readbuf
), HID_FEATURE_REPORT
,
152 dev_err(&hdev
->dev
, "failed read register (%d)\n", ret
);
156 *read_val
= readbuf
[6];
169 static int alps_raw_event(struct hid_device
*hdev
,
170 struct hid_report
*report
, u8
*data
, int size
)
172 int x
[MAX_TOUCHES
], y
[MAX_TOUCHES
], z
[MAX_TOUCHES
];
173 int i
, left
, right
, middle
;
174 short sp_x
, sp_y
, sp_z
;
175 struct u1_dev
*hdata
= hid_get_drvdata(hdev
);
178 case U1_MOUSE_REPORT_ID
:
180 case U1_FEATURE_REPORT_ID
:
182 case U1_ABSOLUTE_REPORT_ID
:
183 for (i
= 0; i
< MAX_TOUCHES
; i
++) {
184 x
[i
] = (data
[3+(5*i
)] | (data
[4+(5*i
)] << 8));
185 y
[i
] = (data
[5+(5*i
)] | (data
[6+(5*i
)] << 8));
186 z
[i
] = data
[7+(5*i
)] & 0x7F;
187 left
= data
[1] & 0x1;
188 right
= (data
[1] & 0x2) >> 1;
189 middle
= (data
[1] & 0x4) >> 2;
191 input_mt_slot(hdata
->input
, i
);
194 input_mt_report_slot_state(hdata
->input
,
197 input_mt_report_slot_state(hdata
->input
,
202 input_event(hdata
->input
, EV_ABS
,
203 ABS_MT_POSITION_X
, x
[i
]);
204 input_event(hdata
->input
, EV_ABS
,
205 ABS_MT_POSITION_Y
, y
[i
]);
206 input_event(hdata
->input
, EV_ABS
,
207 ABS_MT_PRESSURE
, z
[i
]);
210 input_mt_sync_frame(hdata
->input
);
211 input_sync(hdata
->input
);
213 input_event(hdata
->input
, EV_KEY
, BTN_LEFT
, left
);
214 input_event(hdata
->input
, EV_KEY
, BTN_RIGHT
, right
);
215 input_event(hdata
->input
, EV_KEY
, BTN_MIDDLE
, middle
);
219 case U1_SP_ABSOLUTE_REPORT_ID
:
220 sp_x
= (data
[2] | (data
[3] << 8));
221 sp_y
= (data
[4] | (data
[5] << 8));
222 sp_z
= (data
[6] | data
[7]) & 0x7FFF;
223 left
= data
[1] & 0x1;
224 right
= (data
[1] & 0x2) >> 1;
225 middle
= (data
[1] & 0x4) >> 2;
230 input_event(priv
->input2
, EV_REL
, REL_X
, sp_x
);
231 input_event(priv
->input2
, EV_REL
, REL_Y
, sp_y
);
233 input_event(priv
->input2
, EV_KEY
, BTN_LEFT
, left
);
234 input_event(priv
->input2
, EV_KEY
, BTN_RIGHT
, right
);
235 input_event(priv
->input2
, EV_KEY
, BTN_MIDDLE
, middle
);
237 input_sync(priv
->input2
);
246 static int alps_post_reset(struct hid_device
*hdev
)
248 return u1_read_write_register(hdev
, ADDRESS_U1_DEV_CTRL_1
,
249 NULL
, U1_TP_ABS_MODE
, false);
252 static int alps_post_resume(struct hid_device
*hdev
)
254 return u1_read_write_register(hdev
, ADDRESS_U1_DEV_CTRL_1
,
255 NULL
, U1_TP_ABS_MODE
, false);
257 #endif /* CONFIG_PM */
259 static int alps_input_configured(struct hid_device
*hdev
, struct hid_input
*hi
)
261 struct u1_dev
*data
= hid_get_drvdata(hdev
);
262 struct input_dev
*input
= hi
->input
, *input2
;
263 struct u1_dev devInfo
;
267 /* Check device product ID */
268 switch (hdev
->product
) {
269 case HID_PRODUCT_ID_U1
:
270 case HID_PRODUCT_ID_U1_DUAL
:
278 hid_dbg(hdev
, "Opening low level driver\n");
279 ret
= hid_hw_open(hdev
);
283 /* Allow incoming hid reports */
284 hid_device_io_start(hdev
);
286 /* Device initialization */
287 ret
= u1_read_write_register(hdev
, ADDRESS_U1_DEV_CTRL_1
,
288 &devInfo
.dev_ctrl
, 0, true);
290 dev_err(&hdev
->dev
, "failed U1_DEV_CTRL_1 (%d)\n", ret
);
294 devInfo
.dev_ctrl
&= ~U1_DISABLE_DEV
;
295 devInfo
.dev_ctrl
|= U1_TP_ABS_MODE
;
296 ret
= u1_read_write_register(hdev
, ADDRESS_U1_DEV_CTRL_1
,
297 NULL
, devInfo
.dev_ctrl
, false);
299 dev_err(&hdev
->dev
, "failed to change TP mode (%d)\n", ret
);
303 ret
= u1_read_write_register(hdev
, ADDRESS_U1_NUM_SENS_X
,
304 &devInfo
.sen_line_num_x
, 0, true);
306 dev_err(&hdev
->dev
, "failed U1_NUM_SENS_X (%d)\n", ret
);
310 ret
= u1_read_write_register(hdev
, ADDRESS_U1_NUM_SENS_Y
,
311 &devInfo
.sen_line_num_y
, 0, true);
313 dev_err(&hdev
->dev
, "failed U1_NUM_SENS_Y (%d)\n", ret
);
317 ret
= u1_read_write_register(hdev
, ADDRESS_U1_PITCH_SENS_X
,
318 &devInfo
.pitch_x
, 0, true);
320 dev_err(&hdev
->dev
, "failed U1_PITCH_SENS_X (%d)\n", ret
);
324 ret
= u1_read_write_register(hdev
, ADDRESS_U1_PITCH_SENS_Y
,
325 &devInfo
.pitch_y
, 0, true);
327 dev_err(&hdev
->dev
, "failed U1_PITCH_SENS_Y (%d)\n", ret
);
331 ret
= u1_read_write_register(hdev
, ADDRESS_U1_RESO_DWN_ABS
,
332 &devInfo
.resolution
, 0, true);
334 dev_err(&hdev
->dev
, "failed U1_RESO_DWN_ABS (%d)\n", ret
);
338 ret
= u1_read_write_register(hdev
, ADDRESS_U1_PAD_BTN
,
339 &devInfo
.btn_info
, 0, true);
341 dev_err(&hdev
->dev
, "failed U1_PAD_BTN (%d)\n", ret
);
345 /* Check StickPointer device */
346 ret
= u1_read_write_register(hdev
, ADDRESS_U1_DEVICE_TYP
,
347 &devInfo
.dev_type
, 0, true);
349 dev_err(&hdev
->dev
, "failed U1_DEVICE_TYP (%d)\n", ret
);
353 devInfo
.x_active_len_mm
=
354 (devInfo
.pitch_x
* (devInfo
.sen_line_num_x
- 1)) / 10;
355 devInfo
.y_active_len_mm
=
356 (devInfo
.pitch_y
* (devInfo
.sen_line_num_y
- 1)) / 10;
359 (devInfo
.resolution
<< 2) * (devInfo
.sen_line_num_x
- 1);
361 (devInfo
.resolution
<< 2) * (devInfo
.sen_line_num_y
- 1);
363 __set_bit(EV_ABS
, input
->evbit
);
364 input_set_abs_params(input
, ABS_MT_POSITION_X
, 1, devInfo
.x_max
, 0, 0);
365 input_set_abs_params(input
, ABS_MT_POSITION_Y
, 1, devInfo
.y_max
, 0, 0);
367 if (devInfo
.x_active_len_mm
&& devInfo
.y_active_len_mm
) {
368 res_x
= (devInfo
.x_max
- 1) / devInfo
.x_active_len_mm
;
369 res_y
= (devInfo
.y_max
- 1) / devInfo
.y_active_len_mm
;
371 input_abs_set_res(input
, ABS_MT_POSITION_X
, res_x
);
372 input_abs_set_res(input
, ABS_MT_POSITION_Y
, res_y
);
375 input_set_abs_params(input
, ABS_MT_PRESSURE
, 0, 64, 0, 0);
377 input_mt_init_slots(input
, MAX_TOUCHES
, INPUT_MT_POINTER
);
379 __set_bit(EV_KEY
, input
->evbit
);
380 if ((devInfo
.btn_info
& 0x0F) == (devInfo
.btn_info
& 0xF0) >> 4) {
381 devInfo
.btn_cnt
= (devInfo
.btn_info
& 0x0F);
385 __set_bit(INPUT_PROP_BUTTONPAD
, input
->propbit
);
388 for (i
= 0; i
< devInfo
.btn_cnt
; i
++)
389 __set_bit(BTN_LEFT
+ i
, input
->keybit
);
392 /* Stick device initialization */
393 if (devInfo
.dev_type
& U1_DEVTYPE_SP_SUPPORT
) {
395 priv
= kzalloc(sizeof(struct u1_dev
), GFP_KERNEL
);
397 hid_device_io_stop(hdev
);
402 input2
= input_allocate_device();
404 input_free_device(input2
);
408 priv
->input2
= input2
;
410 devInfo
.dev_ctrl
|= U1_SP_ABS_MODE
;
411 ret
= u1_read_write_register(hdev
, ADDRESS_U1_DEV_CTRL_1
,
412 NULL
, devInfo
.dev_ctrl
, false);
414 dev_err(&hdev
->dev
, "failed SP mode (%d)\n", ret
);
415 input_free_device(input2
);
419 ret
= u1_read_write_register(hdev
, ADDRESS_U1_SP_BTN
,
420 &devInfo
.sp_btn_info
, 0, true);
422 dev_err(&hdev
->dev
, "failed U1_SP_BTN (%d)\n", ret
);
423 input_free_device(input2
);
427 input2
->phys
= input
->phys
;
428 input2
->name
= "DualPoint Stick";
429 input2
->id
.bustype
= BUS_I2C
;
430 input2
->id
.vendor
= input
->id
.vendor
;
431 input2
->id
.product
= input
->id
.product
;
432 input2
->id
.version
= input
->id
.version
;
433 input2
->dev
.parent
= input
->dev
.parent
;
435 __set_bit(EV_KEY
, input2
->evbit
);
436 devInfo
.sp_btn_cnt
= (devInfo
.sp_btn_info
& 0x0F);
437 for (i
= 0; i
< devInfo
.sp_btn_cnt
; i
++)
438 __set_bit(BTN_LEFT
+ i
, input2
->keybit
);
440 __set_bit(EV_REL
, input2
->evbit
);
441 __set_bit(REL_X
, input2
->relbit
);
442 __set_bit(REL_Y
, input2
->relbit
);
443 __set_bit(INPUT_PROP_POINTER
, input2
->propbit
);
444 __set_bit(INPUT_PROP_POINTING_STICK
, input2
->propbit
);
446 if (input_register_device(priv
->input2
)) {
447 input_free_device(input2
);
453 hid_device_io_stop(hdev
);
458 static int alps_input_mapping(struct hid_device
*hdev
,
459 struct hid_input
*hi
, struct hid_field
*field
,
460 struct hid_usage
*usage
, unsigned long **bit
, int *max
)
465 static int alps_probe(struct hid_device
*hdev
, const struct hid_device_id
*id
)
467 struct u1_dev
*data
= NULL
;
470 data
= devm_kzalloc(&hdev
->dev
, sizeof(struct u1_dev
), GFP_KERNEL
);
475 hid_set_drvdata(hdev
, data
);
477 hdev
->quirks
|= HID_QUIRK_NO_INIT_REPORTS
;
479 ret
= hid_parse(hdev
);
481 hid_err(hdev
, "parse failed\n");
485 ret
= hid_hw_start(hdev
, HID_CONNECT_DEFAULT
);
487 hid_err(hdev
, "hw start failed\n");
494 static void alps_remove(struct hid_device
*hdev
)
500 static const struct hid_device_id alps_id
[] = {
501 { HID_DEVICE(HID_BUS_ANY
, HID_GROUP_ANY
,
502 USB_VENDOR_ID_ALPS_JP
, HID_ANY_ID
) },
505 MODULE_DEVICE_TABLE(hid
, alps_id
);
507 static struct hid_driver alps_driver
= {
511 .remove
= alps_remove
,
512 .raw_event
= alps_raw_event
,
513 .input_mapping
= alps_input_mapping
,
514 .input_configured
= alps_input_configured
,
516 .resume
= alps_post_resume
,
517 .reset_resume
= alps_post_reset
,
521 module_hid_driver(alps_driver
);
523 MODULE_AUTHOR("Masaki Ota <masaki.ota@jp.alps.com>");
524 MODULE_DESCRIPTION("ALPS HID driver");
525 MODULE_LICENSE("GPL");