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