drm: bridge: dw-hdmi: optimize edid reading process
[firefly-linux-kernel-4.4.55.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 T4_INPUT_REPORT_LEN                     sizeof(T4_INPUT_REPORT)
56 #define T4_FEATURE_REPORT_LEN           T4_INPUT_REPORT_LEN
57 #define T4_FEATURE_REPORT_ID            7
58 #define T4_CMD_REGISTER_READ                    0x08
59 #define T4_CMD_REGISTER_WRITE                   0x07
60
61 #define T4_ADDRESS_BASE                         0xC2C0
62 #define PRM_SYS_CONFIG_1                        (T4_ADDRESS_BASE + 0x0002)
63 #define T4_PRM_FEED_CONFIG_1            (T4_ADDRESS_BASE + 0x0004)
64 #define T4_PRM_FEED_CONFIG_4            (T4_ADDRESS_BASE + 0x001A)
65 #define T4_PRM_ID_CONFIG_3                      (T4_ADDRESS_BASE + 0x00B0)
66
67 #define T4_FEEDCFG4_ADVANCED_ABS_ENABLE                 0x01
68 #define T4_I2C_ABS      0x78
69
70 #define T4_COUNT_PER_ELECTRODE          256
71 #define MAX_TOUCHES     5
72
73 typedef enum {
74         U1,
75         T4,
76         UNKNOWN,
77 } DEV_TYPE;
78 /**
79  * struct u1_data
80  *
81  * @input: pointer to the kernel input device
82  * @input2: pointer to the kernel input2 device
83  * @hdev: pointer to the struct hid_device
84  *
85  * @dev_type: device type
86  * @max_fingers: total number of fingers
87  * @has_sp: boolean of sp existense
88  * @sp_btn_info: button information
89  * @x_active_len_mm: active area length of X (mm)
90  * @y_active_len_mm: active area length of Y (mm)
91  * @x_max: maximum x coordinate value
92  * @y_max: maximum y coordinate value
93  * @x_min: minimum x coordinate value
94  * @y_min: minimum y coordinate value
95  * @btn_cnt: number of buttons
96  * @sp_btn_cnt: number of stick buttons
97  */
98 struct alps_dev {
99         struct input_dev *input;
100         struct input_dev *input2;
101         struct hid_device *hdev;
102
103         DEV_TYPE dev_type;
104         u8  max_fingers;
105         u8  has_sp;
106         u8      sp_btn_info;
107         u32     x_active_len_mm;
108         u32     y_active_len_mm;
109         u32     x_max;
110         u32     y_max;
111         u32     x_min;
112         u32     y_min;
113         u32     btn_cnt;
114         u32     sp_btn_cnt;
115 };
116
117 typedef struct _T4_CONTACT_DATA {
118         u8  Palm;
119         u8      x_lo;
120         u8      x_hi;
121         u8      y_lo;
122         u8      y_hi;
123 } T4_CONTACT_DATA, *PT4_CONTACT_DATA;
124
125 typedef struct _T4_INPUT_REPORT {
126         u8  ReportID;
127         u8  NumContacts;
128         T4_CONTACT_DATA Contact[5];
129         u8  Button;
130         u8  Track[5];
131         u8  ZX[5], ZY[5];
132         u8  PalmTime[5];
133         u8  Kilroy;
134         u16 TimeStamp;
135 } T4_INPUT_REPORT, *PT4_INPUT_REPORT;
136
137 static u16 t4_calc_check_sum(u8 *buffer, unsigned long offset, unsigned long length)
138 {
139         u16 sum1 = 0xFF, sum2 = 0xFF;
140         unsigned long i = 0;
141
142         if (offset + length >= 50)
143                 return 0;
144
145         while (length > 0) {
146                 u32 tlen = length > 20 ? 20 : length;
147
148                 length -= tlen;
149
150                 do {
151                         sum1 += buffer[offset + i];
152                         sum2 += sum1;
153                         i++;
154                 } while (--tlen > 0);
155
156                 sum1 = (sum1 & 0xFF) + (sum1 >> 8);
157                 sum2 = (sum2 & 0xFF) + (sum2 >> 8);
158         }
159
160         sum1 = (sum1 & 0xFF) + (sum1 >> 8);
161         sum2 = (sum2 & 0xFF) + (sum2 >> 8);
162
163         return(sum2 << 8 | sum1);
164 }
165
166 static int T4_read_write_register(struct hid_device *hdev, u32 address,
167         u8 *read_val, u8 write_val, bool read_flag)
168 {
169         int ret;
170         u16 check_sum;
171         u8 *input;
172         u8 *readbuf;
173
174         input = kzalloc(T4_FEATURE_REPORT_LEN, GFP_KERNEL);
175         if (!input)
176                 return -ENOMEM;
177
178         input[0] = T4_FEATURE_REPORT_ID;
179         if (read_flag) {
180                 input[1] = T4_CMD_REGISTER_READ;
181                 input[8] = 0x00;
182         } else {
183                 input[1] = T4_CMD_REGISTER_WRITE;
184                 input[8] = write_val;
185         }
186         put_unaligned_le32(address, input + 2);
187         input[6] = 1;
188         input[7] = 0;
189
190         /*Calculate amd append the checksum*/
191         check_sum = t4_calc_check_sum(input, 1, 8);
192         input[9] = (u8)check_sum;
193         input[10] = (u8)(check_sum >> 8);
194         input[11] = 0;
195
196         ret = hid_hw_raw_request(hdev, T4_FEATURE_REPORT_ID, input,
197                         T4_FEATURE_REPORT_LEN,
198                         HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
199
200         if (ret < 0) {
201                 dev_err(&hdev->dev, "failed to read command (%d)\n", ret);
202                 goto exit;
203         }
204
205         if (read_flag) {
206                 readbuf = kzalloc(T4_FEATURE_REPORT_LEN, GFP_KERNEL);
207                 if (!readbuf) {
208                         kfree(input);
209                         return -ENOMEM;
210                 }
211
212                 ret = hid_hw_raw_request(hdev, T4_FEATURE_REPORT_ID, readbuf,
213                                 T4_FEATURE_REPORT_LEN,
214                                 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
215                 if (ret < 0) {
216                         dev_err(&hdev->dev, "failed read register (%d)\n", ret);
217                         goto exit;
218                 }
219                 if (*(u32 *)&readbuf[6] != address)
220                         hid_alert(hdev, "T4_read_write_register address error %x %x\n", *(u32 *)&readbuf[6], address);
221                 if (*(u16 *)&readbuf[10] != 1)
222                         hid_alert(hdev, "T4_read_write_register size error %x\n", *(u16 *)&readbuf[10]);
223                 check_sum = t4_calc_check_sum(readbuf, 6, 7);
224                 if (*(u16 *)&readbuf[13] != check_sum)
225                         hid_alert(hdev, "T4_read_write_register checksum error %x %x\n", *(u16 *)&readbuf[13], check_sum);
226                 *read_val = readbuf[12];
227
228                 kfree(readbuf);
229         }
230
231         ret = 0;
232
233 exit:
234         kfree(input);
235         return ret;
236 }
237
238 static int u1_read_write_register(struct hid_device *hdev, u32 address,
239         u8 *read_val, u8 write_val, bool read_flag)
240 {
241         int ret, i;
242         u8 check_sum;
243         u8 *input;
244         u8 *readbuf;
245
246         input = kzalloc(U1_FEATURE_REPORT_LEN, GFP_KERNEL);
247         if (!input)
248                 return -ENOMEM;
249
250         input[0] = U1_FEATURE_REPORT_ID;
251         if (read_flag) {
252                 input[1] = U1_CMD_REGISTER_READ;
253                 input[6] = 0x00;
254         } else {
255                 input[1] = U1_CMD_REGISTER_WRITE;
256                 input[6] = write_val;
257         }
258
259         put_unaligned_le32(address, input + 2);
260
261         /* Calculate the checksum */
262         check_sum = U1_FEATURE_REPORT_LEN_ALL;
263         for (i = 0; i < U1_FEATURE_REPORT_LEN - 1; i++)
264                 check_sum += input[i];
265
266         input[7] = check_sum;
267         ret = hid_hw_raw_request(hdev, U1_FEATURE_REPORT_ID, input,
268                         U1_FEATURE_REPORT_LEN,
269                         HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
270
271         if (ret < 0) {
272                 dev_err(&hdev->dev, "failed to read command (%d)\n", ret);
273                 goto exit;
274         }
275
276         if (read_flag) {
277                 readbuf = kzalloc(U1_FEATURE_REPORT_LEN, GFP_KERNEL);
278                 if (!readbuf) {
279                         kfree(input);
280                         return -ENOMEM;
281                 }
282
283                 ret = hid_hw_raw_request(hdev, U1_FEATURE_REPORT_ID, readbuf,
284                                 U1_FEATURE_REPORT_LEN,
285                                 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
286
287                 if (ret < 0) {
288                         dev_err(&hdev->dev, "failed read register (%d)\n", ret);
289                         goto exit;
290                 }
291
292                 *read_val = readbuf[6];
293
294                 kfree(readbuf);
295         }
296
297         ret = 0;
298
299 exit:
300         kfree(input);
301         return ret;
302 }
303
304 static int T4_raw_event(struct alps_dev *hdata, u8 *data, int size)
305 {
306         unsigned int x, y, z;
307         int i;
308         PT4_INPUT_REPORT p_report = (PT4_INPUT_REPORT)data;
309
310         if (!data)
311                 return 0;
312         for (i = 0; i < hdata->max_fingers; i++) {
313                 x = p_report->Contact[i].x_hi << 8 | p_report->Contact[i].x_lo;
314                 y = p_report->Contact[i].y_hi << 8 | p_report->Contact[i].y_lo;
315                 y = hdata->y_max - y + hdata->y_min;
316                 z = (p_report->Contact[i].Palm < 0x80 && p_report->Contact[i].Palm > 0) * 62;
317                 if (x == 0xffff)
318                         x = 0, y = 0, z = 0;
319                 input_mt_slot(hdata->input, i);
320
321                 if (z != 0) {
322                         input_mt_report_slot_state(hdata->input, MT_TOOL_FINGER, 1);
323                 } else {
324                         input_mt_report_slot_state(hdata->input, MT_TOOL_FINGER, 0);
325                         input_report_abs(hdata->input, ABS_MT_PRESSURE, 0);
326                         continue;
327                 }
328
329                 input_report_abs(hdata->input, ABS_MT_POSITION_X, x);
330                 input_report_abs(hdata->input, ABS_MT_POSITION_Y, y);
331                 input_report_abs(hdata->input, ABS_MT_PRESSURE, z);
332         }
333         input_mt_sync_frame(hdata->input);
334
335         input_report_key(hdata->input, BTN_LEFT,        p_report->Button);
336
337         input_sync(hdata->input);
338         return 1;
339 }
340
341 static int U1_raw_event(struct alps_dev *hdata, u8 *data, int size)
342 {
343         unsigned int x, y, z;
344         short sp_x, sp_y;
345         int i;
346
347         if (!data)
348                 return 0;
349         switch (data[0]) {
350         case U1_MOUSE_REPORT_ID:
351                 break;
352         case U1_FEATURE_REPORT_ID:
353                 break;
354         case U1_ABSOLUTE_REPORT_ID:
355                 for (i = 0; i < hdata->max_fingers; i++) {
356                         u8 *contact = &data[i * 5];
357
358                         x = get_unaligned_le16(contact + 3);
359                         y = get_unaligned_le16(contact + 5);
360                         z = contact[7] & 0x7F;
361
362                         input_mt_slot(hdata->input, i);
363
364                         if (z != 0) {
365                                 input_mt_report_slot_state(hdata->input,
366                                         MT_TOOL_FINGER, 1);
367
368                                 input_report_abs(hdata->input, ABS_MT_POSITION_X, x);
369                                 input_report_abs(hdata->input, ABS_MT_POSITION_Y, y);
370                                 input_report_abs(hdata->input, ABS_MT_PRESSURE, z);
371                         } else {
372                                 input_mt_report_slot_state(hdata->input,
373                                         MT_TOOL_FINGER, 0);
374                         }
375                 }
376
377                 input_mt_sync_frame(hdata->input);
378
379                 input_report_key(hdata->input, BTN_LEFT,
380                         data[1] & 0x1);
381                 input_report_key(hdata->input, BTN_RIGHT,
382                         (data[1] & 0x2) >> 1);
383                 input_report_key(hdata->input, BTN_MIDDLE,
384                         (data[1] & 0x4) >> 2);
385
386                 input_sync(hdata->input);
387
388                 return 1;
389
390         case U1_SP_ABSOLUTE_REPORT_ID:
391                 sp_x = get_unaligned_le16(data + 2);
392                 sp_y = get_unaligned_le16(data + 4);
393
394                 sp_x = sp_x / 8;
395                 sp_y = sp_y / 8;
396
397                 input_report_rel(hdata->input2, REL_X, sp_x);
398                 input_report_rel(hdata->input2, REL_Y, sp_y);
399
400                 input_report_key(hdata->input2, BTN_LEFT,
401                         data[1] & 0x1);
402                 input_report_key(hdata->input2, BTN_RIGHT,
403                         (data[1] & 0x2) >> 1);
404                 input_report_key(hdata->input2, BTN_MIDDLE,
405                         (data[1] & 0x4) >> 2);
406
407                 input_sync(hdata->input2);
408
409                 return 1;
410         }
411         return 0;
412 }
413
414 static int alps_raw_event(struct hid_device *hdev,
415                 struct hid_report *report, u8 *data, int size)
416 {
417         int ret = 0;
418         struct alps_dev *hdata = hid_get_drvdata(hdev);
419
420         if (hdev->product == HID_PRODUCT_ID_T4_BTNLESS)
421                 ret = T4_raw_event(hdata, data, size);
422         else
423                 ret = U1_raw_event(hdata, data, size);
424         return ret;
425 }
426
427 #ifdef CONFIG_PM
428 static int alps_post_reset(struct hid_device *hdev)
429 {
430         int ret = -1;
431         struct alps_dev *data = hid_get_drvdata(hdev);
432
433         switch (data->dev_type) {
434         case T4:
435                 ret = T4_read_write_register(hdev, T4_PRM_FEED_CONFIG_1,
436                         NULL, T4_I2C_ABS, false);
437                 ret = T4_read_write_register(hdev, T4_PRM_FEED_CONFIG_4,
438                         NULL, T4_FEEDCFG4_ADVANCED_ABS_ENABLE, false);
439                 break;
440         case U1:
441                 ret = u1_read_write_register(hdev, ADDRESS_U1_DEV_CTRL_1,
442                         NULL, U1_TP_ABS_MODE | U1_SP_ABS_MODE, false);
443                 break;
444         default:
445                 break;
446         }
447         return ret;
448 }
449
450 static int alps_post_resume(struct hid_device *hdev)
451 {
452         return alps_post_reset(hdev);
453 }
454 #endif /* CONFIG_PM */
455
456 static int u1_init(struct hid_device *hdev, struct alps_dev *pri_data)
457 {
458         int ret;
459         u8 tmp, dev_ctrl, sen_line_num_x, sen_line_num_y, pitch_x, pitch_y, resolution;
460         /* Device initialization */
461         ret = u1_read_write_register(hdev, ADDRESS_U1_DEV_CTRL_1,
462                         &dev_ctrl, 0, true);
463         if (ret < 0) {
464                 dev_err(&hdev->dev, "failed U1_DEV_CTRL_1 (%d)\n", ret);
465                 goto exit;
466         }
467
468         dev_ctrl &= ~U1_DISABLE_DEV;
469         dev_ctrl |= U1_TP_ABS_MODE;
470         ret = u1_read_write_register(hdev, ADDRESS_U1_DEV_CTRL_1,
471                         NULL, dev_ctrl, false);
472         if (ret < 0) {
473                 dev_err(&hdev->dev, "failed to change TP mode (%d)\n", ret);
474                 goto exit;
475         }
476
477         ret = u1_read_write_register(hdev, ADDRESS_U1_NUM_SENS_X,
478                         &sen_line_num_x, 0, true);
479         if (ret < 0) {
480                 dev_err(&hdev->dev, "failed U1_NUM_SENS_X (%d)\n", ret);
481                 goto exit;
482         }
483
484         ret = u1_read_write_register(hdev, ADDRESS_U1_NUM_SENS_Y,
485                         &sen_line_num_y, 0, true);
486                 if (ret < 0) {
487                 dev_err(&hdev->dev, "failed U1_NUM_SENS_Y (%d)\n", ret);
488                 goto exit;
489         }
490
491         ret = u1_read_write_register(hdev, ADDRESS_U1_PITCH_SENS_X,
492                         &pitch_x, 0, true);
493         if (ret < 0) {
494                 dev_err(&hdev->dev, "failed U1_PITCH_SENS_X (%d)\n", ret);
495                 goto exit;
496         }
497
498         ret = u1_read_write_register(hdev, ADDRESS_U1_PITCH_SENS_Y,
499                         &pitch_y, 0, true);
500         if (ret < 0) {
501                 dev_err(&hdev->dev, "failed U1_PITCH_SENS_Y (%d)\n", ret);
502                 goto exit;
503         }
504
505         ret = u1_read_write_register(hdev, ADDRESS_U1_RESO_DWN_ABS,
506                 &resolution, 0, true);
507         if (ret < 0) {
508                 dev_err(&hdev->dev, "failed U1_RESO_DWN_ABS (%d)\n", ret);
509                 goto exit;
510         }
511         pri_data->x_active_len_mm =
512                 (pitch_x * (sen_line_num_x - 1)) / 10;
513         pri_data->y_active_len_mm =
514                 (pitch_y * (sen_line_num_y - 1)) / 10;
515
516         pri_data->x_max =
517                 (resolution << 2) * (sen_line_num_x - 1);
518         pri_data->x_min = 1;
519         pri_data->y_max =
520                 (resolution << 2) * (sen_line_num_y - 1);
521         pri_data->y_min = 1;
522
523         ret = u1_read_write_register(hdev, ADDRESS_U1_PAD_BTN,
524                         &tmp, 0, true);
525         if (ret < 0) {
526                 dev_err(&hdev->dev, "failed U1_PAD_BTN (%d)\n", ret);
527                 goto exit;
528         }
529         if ((tmp & 0x0F) == (tmp & 0xF0) >> 4) {
530                 pri_data->btn_cnt = (tmp & 0x0F);
531         } else {
532                 /* Button pad */
533                 pri_data->btn_cnt = 1;
534         }
535
536         pri_data->has_sp = 0;
537         /* Check StickPointer device */
538         ret = u1_read_write_register(hdev, ADDRESS_U1_DEVICE_TYP,
539                         &tmp, 0, true);
540         if (ret < 0) {
541                 dev_err(&hdev->dev, "failed U1_DEVICE_TYP (%d)\n", ret);
542                 goto exit;
543         }
544         if (tmp & U1_DEVTYPE_SP_SUPPORT) {
545                 dev_ctrl |= U1_SP_ABS_MODE;
546                 ret = u1_read_write_register(hdev, ADDRESS_U1_DEV_CTRL_1,
547                         NULL, dev_ctrl, false);
548                 if (ret < 0) {
549                         dev_err(&hdev->dev, "failed SP mode (%d)\n", ret);
550                         goto exit;
551                 }
552
553                 ret = u1_read_write_register(hdev, ADDRESS_U1_SP_BTN,
554                         &pri_data->sp_btn_info, 0, true);
555                 if (ret < 0) {
556                         dev_err(&hdev->dev, "failed U1_SP_BTN (%d)\n", ret);
557                         goto exit;
558                 }
559                 pri_data->has_sp = 1;
560         }
561         pri_data->max_fingers = 5;
562 exit:
563         return ret;
564 }
565
566 static int T4_init(struct hid_device *hdev, struct alps_dev *pri_data)
567 {
568         int ret;
569         u8 tmp, sen_line_num_x, sen_line_num_y;
570
571         ret = T4_read_write_register(hdev, T4_PRM_ID_CONFIG_3, &tmp, 0, true);
572         if (ret < 0) {
573                 dev_err(&hdev->dev, "failed T4_PRM_ID_CONFIG_3 (%d)\n", ret);
574                 goto exit;
575         }
576         sen_line_num_x = 16 + ((tmp & 0x0F)  | (tmp & 0x08 ? 0xF0 : 0));
577         sen_line_num_y = 12 + (((tmp & 0xF0) >> 4)  | (tmp & 0x80 ? 0xF0 : 0));
578
579         pri_data->x_max = sen_line_num_x * T4_COUNT_PER_ELECTRODE;
580         pri_data->x_min = T4_COUNT_PER_ELECTRODE;
581         pri_data->y_max = sen_line_num_y * T4_COUNT_PER_ELECTRODE;
582         pri_data->y_min = T4_COUNT_PER_ELECTRODE;
583         pri_data->x_active_len_mm = 0;
584         pri_data->y_active_len_mm = 0;
585         pri_data->btn_cnt = 1;
586
587         ret = T4_read_write_register(hdev, PRM_SYS_CONFIG_1, &tmp, 0, true);
588         if (ret < 0) {
589                 dev_err(&hdev->dev, "failed PRM_SYS_CONFIG_1 (%d)\n", ret);
590                 goto exit;
591         }
592         tmp |= 0x02;
593         ret = T4_read_write_register(hdev, PRM_SYS_CONFIG_1, NULL, tmp, false);
594         if (ret < 0) {
595                 dev_err(&hdev->dev, "failed PRM_SYS_CONFIG_1 (%d)\n", ret);
596                 goto exit;
597         }
598
599         ret = T4_read_write_register(hdev, T4_PRM_FEED_CONFIG_1, NULL, T4_I2C_ABS, false);
600         if (ret < 0) {
601                 dev_err(&hdev->dev, "failed T4_PRM_FEED_CONFIG_1 (%d)\n", ret);
602                 goto exit;
603         }
604
605         ret = T4_read_write_register (hdev, T4_PRM_FEED_CONFIG_4, NULL, T4_FEEDCFG4_ADVANCED_ABS_ENABLE, false);
606         if (ret < 0) {
607                 dev_err(&hdev->dev, "failed T4_PRM_FEED_CONFIG_4 (%d)\n", ret);
608                 goto exit;
609         }
610         pri_data->max_fingers = 5;
611         pri_data->has_sp = 0;
612 exit:
613         return ret;
614 }
615
616 static int alps_input_configured(struct hid_device *hdev, struct hid_input *hi)
617 {
618         struct alps_dev *data = hid_get_drvdata(hdev);
619         struct input_dev *input = hi->input, *input2;
620         int ret;
621         int res_x, res_y, i;
622
623         data->input = input;
624
625         hid_dbg(hdev, "Opening low level driver\n");
626         ret = hid_hw_open(hdev);
627         if (ret)
628                 return ret;
629
630         /* Allow incoming hid reports */
631         hid_device_io_start(hdev);
632         if (data->dev_type == T4)
633                 ret = T4_init(hdev, data);
634         else
635                 ret = u1_init(hdev, data);
636         if (ret)
637                 goto exit;
638
639         __set_bit(EV_ABS, input->evbit);
640         input_set_abs_params(input, ABS_MT_POSITION_X, data->x_min, data->x_max, 0, 0);
641         input_set_abs_params(input, ABS_MT_POSITION_Y, data->y_min, data->y_max, 0, 0);
642
643         if (data->x_active_len_mm && data->y_active_len_mm) {
644                 res_x = (data->x_max - 1) / data->x_active_len_mm;
645                 res_y = (data->y_max - 1) / data->y_active_len_mm;
646
647                 input_abs_set_res(input, ABS_MT_POSITION_X, res_x);
648                 input_abs_set_res(input, ABS_MT_POSITION_Y, res_y);
649         }
650
651         input_set_abs_params(input, ABS_MT_PRESSURE, 0, 64, 0, 0);
652
653         input_mt_init_slots(input, data->max_fingers, INPUT_MT_POINTER);
654
655         __set_bit(EV_KEY, input->evbit);
656
657         if (data->btn_cnt == 1)
658                 __set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
659
660         for (i = 0; i < data->btn_cnt; i++)
661                 __set_bit(BTN_LEFT + i, input->keybit);
662
663         /* Stick device initialization */
664         if (data->has_sp) {
665                 input2 = input_allocate_device();
666                 if (!input2) {
667                         input_free_device(input2);
668                         goto exit;
669                 }
670
671                 data->input2 = input2;
672                 input2->phys = input->phys;
673                 input2->name = "DualPoint Stick";
674                 input2->id.bustype = BUS_I2C;
675                 input2->id.vendor  = input->id.vendor;
676                 input2->id.product = input->id.product;
677                 input2->id.version = input->id.version;
678                 input2->dev.parent = input->dev.parent;
679
680                 __set_bit(EV_KEY, input2->evbit);
681                 data->sp_btn_cnt = (data->sp_btn_info & 0x0F);
682                 for (i = 0; i < data->sp_btn_cnt; i++)
683                         __set_bit(BTN_LEFT + i, input2->keybit);
684
685                 __set_bit(EV_REL, input2->evbit);
686                 __set_bit(REL_X, input2->relbit);
687                 __set_bit(REL_Y, input2->relbit);
688                 __set_bit(INPUT_PROP_POINTER, input2->propbit);
689                 __set_bit(INPUT_PROP_POINTING_STICK, input2->propbit);
690
691                 if (input_register_device(data->input2)) {
692                         input_free_device(input2);
693                         goto exit;
694                 }
695         }
696
697 exit:
698         hid_device_io_stop(hdev);
699         hid_hw_close(hdev);
700         return ret;
701 }
702
703 static int alps_input_mapping(struct hid_device *hdev,
704                 struct hid_input *hi, struct hid_field *field,
705                 struct hid_usage *usage, unsigned long **bit, int *max)
706 {
707         return -1;
708 }
709
710 static int alps_probe(struct hid_device *hdev, const struct hid_device_id *id)
711 {
712         struct alps_dev *data = NULL;
713         int ret;
714
715         data = devm_kzalloc(&hdev->dev, sizeof(struct alps_dev), GFP_KERNEL);
716         if (!data)
717                 return -ENOMEM;
718
719         data->hdev = hdev;
720         hid_set_drvdata(hdev, data);
721
722         hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
723
724         ret = hid_parse(hdev);
725         if (ret) {
726                 hid_err(hdev, "parse failed\n");
727                 return ret;
728         }
729         switch (hdev->product) {
730         case HID_DEVICE_ID_ALPS_T4_BTNLESS:
731                 data->dev_type = T4;
732                 break;
733         case HID_DEVICE_ID_ALPS_U1_DUAL:
734         case HID_DEVICE_ID_ALPS_U1:
735                 data->dev_type = U1;
736                 break;
737         default:
738                 data->dev_type = UNKNOWN;
739                 break;
740         }
741         ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
742         if (ret) {
743                 hid_err(hdev, "hw start failed\n");
744                 return ret;
745         }
746
747         return 0;
748 }
749
750 static void alps_remove(struct hid_device *hdev)
751 {
752         hid_hw_stop(hdev);
753 }
754
755 static const struct hid_device_id alps_id[] = {
756         { HID_DEVICE(HID_BUS_ANY, HID_GROUP_ANY,
757                 USB_VENDOR_ID_ALPS_JP, HID_DEVICE_ID_ALPS_U1_DUAL) },
758         { HID_DEVICE(HID_BUS_ANY, HID_GROUP_ANY,
759                 USB_VENDOR_ID_ALPS_JP, HID_DEVICE_ID_ALPS_U1) },
760         { HID_DEVICE(HID_BUS_ANY, HID_GROUP_ANY,
761                 USB_VENDOR_ID_ALPS_JP, HID_DEVICE_ID_ALPS_T4_BTNLESS) },
762         { }
763 };
764 MODULE_DEVICE_TABLE(hid, alps_id);
765
766 static struct hid_driver alps_driver = {
767         .name = "hid-alps",
768         .id_table               = alps_id,
769         .probe                  = alps_probe,
770         .remove                 = alps_remove,
771         .raw_event              = alps_raw_event,
772         .input_mapping          = alps_input_mapping,
773         .input_configured       = alps_input_configured,
774 #ifdef CONFIG_PM
775         .resume                 = alps_post_resume,
776         .reset_resume           = alps_post_reset,
777 #endif
778 };
779
780 module_hid_driver(alps_driver);
781
782 MODULE_AUTHOR("Masaki Ota <masaki.ota@jp.alps.com>");
783 MODULE_DESCRIPTION("ALPS HID driver");
784 MODULE_LICENSE("GPL");