Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *  Copyright (c) 2016 Masaki Ota <masaki.ota@jp.alps.com>
0004  */
0005 
0006 #include <linux/kernel.h>
0007 #include <linux/hid.h>
0008 #include <linux/input.h>
0009 #include <linux/input/mt.h>
0010 #include <linux/module.h>
0011 #include <asm/unaligned.h>
0012 #include "hid-ids.h"
0013 
0014 /* ALPS Device Product ID */
0015 #define HID_PRODUCT_ID_T3_BTNLESS   0xD0C0
0016 #define HID_PRODUCT_ID_COSMO        0x1202
0017 #define HID_PRODUCT_ID_U1_PTP_1     0x1207
0018 #define HID_PRODUCT_ID_U1           0x1209
0019 #define HID_PRODUCT_ID_U1_PTP_2     0x120A
0020 #define HID_PRODUCT_ID_U1_DUAL      0x120B
0021 #define HID_PRODUCT_ID_T4_BTNLESS   0x120C
0022 
0023 #define DEV_SINGLEPOINT             0x01
0024 #define DEV_DUALPOINT               0x02
0025 
0026 #define U1_MOUSE_REPORT_ID          0x01 /* Mouse data ReportID */
0027 #define U1_ABSOLUTE_REPORT_ID       0x03 /* Absolute data ReportID */
0028 #define U1_ABSOLUTE_REPORT_ID_SECD  0x02 /* FW-PTP Absolute data ReportID */
0029 #define U1_FEATURE_REPORT_ID        0x05 /* Feature ReportID */
0030 #define U1_SP_ABSOLUTE_REPORT_ID    0x06 /* Feature ReportID */
0031 
0032 #define U1_FEATURE_REPORT_LEN       0x08 /* Feature Report Length */
0033 #define U1_FEATURE_REPORT_LEN_ALL   0x0A
0034 #define U1_CMD_REGISTER_READ        0xD1
0035 #define U1_CMD_REGISTER_WRITE       0xD2
0036 
0037 #define U1_DEVTYPE_SP_SUPPORT       0x10 /* SP Support */
0038 #define U1_DISABLE_DEV              0x01
0039 #define U1_TP_ABS_MODE              0x02
0040 #define U1_SP_ABS_MODE              0x80
0041 
0042 #define ADDRESS_U1_DEV_CTRL_1   0x00800040
0043 #define ADDRESS_U1_DEVICE_TYP   0x00800043
0044 #define ADDRESS_U1_NUM_SENS_X   0x00800047
0045 #define ADDRESS_U1_NUM_SENS_Y   0x00800048
0046 #define ADDRESS_U1_PITCH_SENS_X 0x00800049
0047 #define ADDRESS_U1_PITCH_SENS_Y 0x0080004A
0048 #define ADDRESS_U1_RESO_DWN_ABS 0x0080004E
0049 #define ADDRESS_U1_PAD_BTN      0x00800052
0050 #define ADDRESS_U1_SP_BTN       0x0080009F
0051 
0052 #define T4_INPUT_REPORT_LEN         sizeof(struct t4_input_report)
0053 #define T4_FEATURE_REPORT_LEN       T4_INPUT_REPORT_LEN
0054 #define T4_FEATURE_REPORT_ID        7
0055 #define T4_CMD_REGISTER_READ            0x08
0056 #define T4_CMD_REGISTER_WRITE           0x07
0057 
0058 #define T4_ADDRESS_BASE             0xC2C0
0059 #define PRM_SYS_CONFIG_1            (T4_ADDRESS_BASE + 0x0002)
0060 #define T4_PRM_FEED_CONFIG_1        (T4_ADDRESS_BASE + 0x0004)
0061 #define T4_PRM_FEED_CONFIG_4        (T4_ADDRESS_BASE + 0x001A)
0062 #define T4_PRM_ID_CONFIG_3          (T4_ADDRESS_BASE + 0x00B0)
0063 
0064 
0065 #define T4_FEEDCFG4_ADVANCED_ABS_ENABLE         0x01
0066 #define T4_I2C_ABS  0x78
0067 
0068 #define T4_COUNT_PER_ELECTRODE      256
0069 #define MAX_TOUCHES 5
0070 
0071 enum dev_num {
0072     U1,
0073     T4,
0074     UNKNOWN,
0075 };
0076 /**
0077  * struct alps_dev
0078  *
0079  * @input: pointer to the kernel input device
0080  * @input2: pointer to the kernel input2 device
0081  * @hdev: pointer to the struct hid_device
0082  *
0083  * @dev_type: device type
0084  * @max_fingers: total number of fingers
0085  * @has_sp: boolean of sp existense
0086  * @sp_btn_info: button information
0087  * @x_active_len_mm: active area length of X (mm)
0088  * @y_active_len_mm: active area length of Y (mm)
0089  * @x_max: maximum x coordinate value
0090  * @y_max: maximum y coordinate value
0091  * @x_min: minimum x coordinate value
0092  * @y_min: minimum y coordinate value
0093  * @btn_cnt: number of buttons
0094  * @sp_btn_cnt: number of stick buttons
0095  */
0096 struct alps_dev {
0097     struct input_dev *input;
0098     struct input_dev *input2;
0099     struct hid_device *hdev;
0100 
0101     enum dev_num dev_type;
0102     u8  max_fingers;
0103     u8  has_sp;
0104     u8  sp_btn_info;
0105     u32 x_active_len_mm;
0106     u32 y_active_len_mm;
0107     u32 x_max;
0108     u32 y_max;
0109     u32 x_min;
0110     u32 y_min;
0111     u32 btn_cnt;
0112     u32 sp_btn_cnt;
0113 };
0114 
0115 struct t4_contact_data {
0116     u8  palm;
0117     u8  x_lo;
0118     u8  x_hi;
0119     u8  y_lo;
0120     u8  y_hi;
0121 };
0122 
0123 struct t4_input_report {
0124     u8  reportID;
0125     u8  numContacts;
0126     struct t4_contact_data contact[5];
0127     u8  button;
0128     u8  track[5];
0129     u8  zx[5], zy[5];
0130     u8  palmTime[5];
0131     u8  kilroy;
0132     u16 timeStamp;
0133 };
0134 
0135 static u16 t4_calc_check_sum(u8 *buffer,
0136         unsigned long offset, unsigned long length)
0137 {
0138     u16 sum1 = 0xFF, sum2 = 0xFF;
0139     unsigned long i = 0;
0140 
0141     if (offset + length >= 50)
0142         return 0;
0143 
0144     while (length > 0) {
0145         u32 tlen = length > 20 ? 20 : length;
0146 
0147         length -= tlen;
0148 
0149         do {
0150             sum1 += buffer[offset + i];
0151             sum2 += sum1;
0152             i++;
0153         } while (--tlen > 0);
0154 
0155         sum1 = (sum1 & 0xFF) + (sum1 >> 8);
0156         sum2 = (sum2 & 0xFF) + (sum2 >> 8);
0157     }
0158 
0159     sum1 = (sum1 & 0xFF) + (sum1 >> 8);
0160     sum2 = (sum2 & 0xFF) + (sum2 >> 8);
0161 
0162     return(sum2 << 8 | sum1);
0163 }
0164 
0165 static int t4_read_write_register(struct hid_device *hdev, u32 address,
0166     u8 *read_val, u8 write_val, bool read_flag)
0167 {
0168     int ret;
0169     u16 check_sum;
0170     u8 *input;
0171     u8 *readbuf = NULL;
0172 
0173     input = kzalloc(T4_FEATURE_REPORT_LEN, GFP_KERNEL);
0174     if (!input)
0175         return -ENOMEM;
0176 
0177     input[0] = T4_FEATURE_REPORT_ID;
0178     if (read_flag) {
0179         input[1] = T4_CMD_REGISTER_READ;
0180         input[8] = 0x00;
0181     } else {
0182         input[1] = T4_CMD_REGISTER_WRITE;
0183         input[8] = write_val;
0184     }
0185     put_unaligned_le32(address, input + 2);
0186     input[6] = 1;
0187     input[7] = 0;
0188 
0189     /* Calculate the checksum */
0190     check_sum = t4_calc_check_sum(input, 1, 8);
0191     input[9] = (u8)check_sum;
0192     input[10] = (u8)(check_sum >> 8);
0193     input[11] = 0;
0194 
0195     ret = hid_hw_raw_request(hdev, T4_FEATURE_REPORT_ID, input,
0196             T4_FEATURE_REPORT_LEN,
0197             HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
0198 
0199     if (ret < 0) {
0200         dev_err(&hdev->dev, "failed to read command (%d)\n", ret);
0201         goto exit;
0202     }
0203 
0204     if (read_flag) {
0205         readbuf = kzalloc(T4_FEATURE_REPORT_LEN, GFP_KERNEL);
0206         if (!readbuf) {
0207             ret = -ENOMEM;
0208             goto exit;
0209         }
0210 
0211         ret = hid_hw_raw_request(hdev, T4_FEATURE_REPORT_ID, readbuf,
0212                 T4_FEATURE_REPORT_LEN,
0213                 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
0214         if (ret < 0) {
0215             dev_err(&hdev->dev, "failed read register (%d)\n", ret);
0216             goto exit_readbuf;
0217         }
0218 
0219         ret = -EINVAL;
0220 
0221         if (*(u32 *)&readbuf[6] != address) {
0222             dev_err(&hdev->dev, "read register address error (%x,%x)\n",
0223                 *(u32 *)&readbuf[6], address);
0224             goto exit_readbuf;
0225         }
0226 
0227         if (*(u16 *)&readbuf[10] != 1) {
0228             dev_err(&hdev->dev, "read register size error (%x)\n",
0229                 *(u16 *)&readbuf[10]);
0230             goto exit_readbuf;
0231         }
0232 
0233         check_sum = t4_calc_check_sum(readbuf, 6, 7);
0234         if (*(u16 *)&readbuf[13] != check_sum) {
0235             dev_err(&hdev->dev, "read register checksum error (%x,%x)\n",
0236                 *(u16 *)&readbuf[13], check_sum);
0237             goto exit_readbuf;
0238         }
0239 
0240         *read_val = readbuf[12];
0241     }
0242 
0243     ret = 0;
0244 
0245 exit_readbuf:
0246     kfree(readbuf);
0247 exit:
0248     kfree(input);
0249     return ret;
0250 }
0251 
0252 static int u1_read_write_register(struct hid_device *hdev, u32 address,
0253     u8 *read_val, u8 write_val, bool read_flag)
0254 {
0255     int ret, i;
0256     u8 check_sum;
0257     u8 *input;
0258     u8 *readbuf;
0259 
0260     input = kzalloc(U1_FEATURE_REPORT_LEN, GFP_KERNEL);
0261     if (!input)
0262         return -ENOMEM;
0263 
0264     input[0] = U1_FEATURE_REPORT_ID;
0265     if (read_flag) {
0266         input[1] = U1_CMD_REGISTER_READ;
0267         input[6] = 0x00;
0268     } else {
0269         input[1] = U1_CMD_REGISTER_WRITE;
0270         input[6] = write_val;
0271     }
0272 
0273     put_unaligned_le32(address, input + 2);
0274 
0275     /* Calculate the checksum */
0276     check_sum = U1_FEATURE_REPORT_LEN_ALL;
0277     for (i = 0; i < U1_FEATURE_REPORT_LEN - 1; i++)
0278         check_sum += input[i];
0279 
0280     input[7] = check_sum;
0281     ret = hid_hw_raw_request(hdev, U1_FEATURE_REPORT_ID, input,
0282             U1_FEATURE_REPORT_LEN,
0283             HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
0284 
0285     if (ret < 0) {
0286         dev_err(&hdev->dev, "failed to read command (%d)\n", ret);
0287         goto exit;
0288     }
0289 
0290     if (read_flag) {
0291         readbuf = kzalloc(U1_FEATURE_REPORT_LEN, GFP_KERNEL);
0292         if (!readbuf) {
0293             ret = -ENOMEM;
0294             goto exit;
0295         }
0296 
0297         ret = hid_hw_raw_request(hdev, U1_FEATURE_REPORT_ID, readbuf,
0298                 U1_FEATURE_REPORT_LEN,
0299                 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
0300 
0301         if (ret < 0) {
0302             dev_err(&hdev->dev, "failed read register (%d)\n", ret);
0303             kfree(readbuf);
0304             goto exit;
0305         }
0306 
0307         *read_val = readbuf[6];
0308 
0309         kfree(readbuf);
0310     }
0311 
0312     ret = 0;
0313 
0314 exit:
0315     kfree(input);
0316     return ret;
0317 }
0318 
0319 static int t4_raw_event(struct alps_dev *hdata, u8 *data, int size)
0320 {
0321     unsigned int x, y, z;
0322     int i;
0323     struct t4_input_report *p_report = (struct t4_input_report *)data;
0324 
0325     if (!data)
0326         return 0;
0327     for (i = 0; i < hdata->max_fingers; i++) {
0328         x = p_report->contact[i].x_hi << 8 | p_report->contact[i].x_lo;
0329         y = p_report->contact[i].y_hi << 8 | p_report->contact[i].y_lo;
0330         y = hdata->y_max - y + hdata->y_min;
0331         z = (p_report->contact[i].palm < 0x80 &&
0332             p_report->contact[i].palm > 0) * 62;
0333         if (x == 0xffff) {
0334             x = 0;
0335             y = 0;
0336             z = 0;
0337         }
0338         input_mt_slot(hdata->input, i);
0339 
0340         input_mt_report_slot_state(hdata->input,
0341             MT_TOOL_FINGER, z != 0);
0342 
0343         if (!z)
0344             continue;
0345 
0346         input_report_abs(hdata->input, ABS_MT_POSITION_X, x);
0347         input_report_abs(hdata->input, ABS_MT_POSITION_Y, y);
0348         input_report_abs(hdata->input, ABS_MT_PRESSURE, z);
0349     }
0350     input_mt_sync_frame(hdata->input);
0351 
0352     input_report_key(hdata->input, BTN_LEFT, p_report->button);
0353 
0354     input_sync(hdata->input);
0355     return 1;
0356 }
0357 
0358 static int u1_raw_event(struct alps_dev *hdata, u8 *data, int size)
0359 {
0360     unsigned int x, y, z;
0361     int i;
0362     short sp_x, sp_y;
0363 
0364     if (!data)
0365         return 0;
0366     switch (data[0]) {
0367     case U1_MOUSE_REPORT_ID:
0368         break;
0369     case U1_FEATURE_REPORT_ID:
0370         break;
0371     case U1_ABSOLUTE_REPORT_ID:
0372     case U1_ABSOLUTE_REPORT_ID_SECD:
0373         for (i = 0; i < hdata->max_fingers; i++) {
0374             u8 *contact = &data[i * 5];
0375 
0376             x = get_unaligned_le16(contact + 3);
0377             y = get_unaligned_le16(contact + 5);
0378             z = contact[7] & 0x7F;
0379 
0380             input_mt_slot(hdata->input, i);
0381 
0382             if (z != 0) {
0383                 input_mt_report_slot_state(hdata->input,
0384                     MT_TOOL_FINGER, 1);
0385                 input_report_abs(hdata->input,
0386                     ABS_MT_POSITION_X, x);
0387                 input_report_abs(hdata->input,
0388                     ABS_MT_POSITION_Y, y);
0389                 input_report_abs(hdata->input,
0390                     ABS_MT_PRESSURE, z);
0391             } else {
0392                 input_mt_report_slot_inactive(hdata->input);
0393             }
0394         }
0395 
0396         input_mt_sync_frame(hdata->input);
0397 
0398         input_report_key(hdata->input, BTN_LEFT,
0399             data[1] & 0x1);
0400         input_report_key(hdata->input, BTN_RIGHT,
0401             (data[1] & 0x2));
0402         input_report_key(hdata->input, BTN_MIDDLE,
0403             (data[1] & 0x4));
0404 
0405         input_sync(hdata->input);
0406 
0407         return 1;
0408 
0409     case U1_SP_ABSOLUTE_REPORT_ID:
0410         sp_x = get_unaligned_le16(data+2);
0411         sp_y = get_unaligned_le16(data+4);
0412 
0413         sp_x = sp_x / 8;
0414         sp_y = sp_y / 8;
0415 
0416         input_report_rel(hdata->input2, REL_X, sp_x);
0417         input_report_rel(hdata->input2, REL_Y, sp_y);
0418 
0419         input_report_key(hdata->input2, BTN_LEFT,
0420             data[1] & 0x1);
0421         input_report_key(hdata->input2, BTN_RIGHT,
0422             (data[1] & 0x2));
0423         input_report_key(hdata->input2, BTN_MIDDLE,
0424             (data[1] & 0x4));
0425 
0426         input_sync(hdata->input2);
0427 
0428         return 1;
0429     }
0430 
0431     return 0;
0432 }
0433 
0434 static int alps_raw_event(struct hid_device *hdev,
0435         struct hid_report *report, u8 *data, int size)
0436 {
0437     int ret = 0;
0438     struct alps_dev *hdata = hid_get_drvdata(hdev);
0439 
0440     switch (hdev->product) {
0441     case HID_PRODUCT_ID_T4_BTNLESS:
0442         ret = t4_raw_event(hdata, data, size);
0443         break;
0444     default:
0445         ret = u1_raw_event(hdata, data, size);
0446         break;
0447     }
0448     return ret;
0449 }
0450 
0451 static int __maybe_unused alps_post_reset(struct hid_device *hdev)
0452 {
0453     int ret = -1;
0454     struct alps_dev *data = hid_get_drvdata(hdev);
0455 
0456     switch (data->dev_type) {
0457     case T4:
0458         ret = t4_read_write_register(hdev, T4_PRM_FEED_CONFIG_1,
0459             NULL, T4_I2C_ABS, false);
0460         if (ret < 0) {
0461             dev_err(&hdev->dev, "failed T4_PRM_FEED_CONFIG_1 (%d)\n",
0462                 ret);
0463             goto exit;
0464         }
0465 
0466         ret = t4_read_write_register(hdev, T4_PRM_FEED_CONFIG_4,
0467             NULL, T4_FEEDCFG4_ADVANCED_ABS_ENABLE, false);
0468         if (ret < 0) {
0469             dev_err(&hdev->dev, "failed T4_PRM_FEED_CONFIG_4 (%d)\n",
0470                 ret);
0471             goto exit;
0472         }
0473         break;
0474     case U1:
0475         ret = u1_read_write_register(hdev,
0476             ADDRESS_U1_DEV_CTRL_1, NULL,
0477             U1_TP_ABS_MODE | U1_SP_ABS_MODE, false);
0478         if (ret < 0) {
0479             dev_err(&hdev->dev, "failed to change TP mode (%d)\n",
0480                 ret);
0481             goto exit;
0482         }
0483         break;
0484     default:
0485         break;
0486     }
0487 
0488 exit:
0489     return ret;
0490 }
0491 
0492 static int __maybe_unused alps_post_resume(struct hid_device *hdev)
0493 {
0494     return alps_post_reset(hdev);
0495 }
0496 
0497 static int u1_init(struct hid_device *hdev, struct alps_dev *pri_data)
0498 {
0499     int ret;
0500     u8 tmp, dev_ctrl, sen_line_num_x, sen_line_num_y;
0501     u8 pitch_x, pitch_y, resolution;
0502 
0503     /* Device initialization */
0504     ret = u1_read_write_register(hdev, ADDRESS_U1_DEV_CTRL_1,
0505             &dev_ctrl, 0, true);
0506     if (ret < 0) {
0507         dev_err(&hdev->dev, "failed U1_DEV_CTRL_1 (%d)\n", ret);
0508         goto exit;
0509     }
0510 
0511     dev_ctrl &= ~U1_DISABLE_DEV;
0512     dev_ctrl |= U1_TP_ABS_MODE;
0513     ret = u1_read_write_register(hdev, ADDRESS_U1_DEV_CTRL_1,
0514             NULL, dev_ctrl, false);
0515     if (ret < 0) {
0516         dev_err(&hdev->dev, "failed to change TP mode (%d)\n", ret);
0517         goto exit;
0518     }
0519 
0520     ret = u1_read_write_register(hdev, ADDRESS_U1_NUM_SENS_X,
0521             &sen_line_num_x, 0, true);
0522     if (ret < 0) {
0523         dev_err(&hdev->dev, "failed U1_NUM_SENS_X (%d)\n", ret);
0524         goto exit;
0525     }
0526 
0527     ret = u1_read_write_register(hdev, ADDRESS_U1_NUM_SENS_Y,
0528             &sen_line_num_y, 0, true);
0529     if (ret < 0) {
0530         dev_err(&hdev->dev, "failed U1_NUM_SENS_Y (%d)\n", ret);
0531         goto exit;
0532     }
0533 
0534     ret = u1_read_write_register(hdev, ADDRESS_U1_PITCH_SENS_X,
0535             &pitch_x, 0, true);
0536     if (ret < 0) {
0537         dev_err(&hdev->dev, "failed U1_PITCH_SENS_X (%d)\n", ret);
0538         goto exit;
0539     }
0540 
0541     ret = u1_read_write_register(hdev, ADDRESS_U1_PITCH_SENS_Y,
0542             &pitch_y, 0, true);
0543     if (ret < 0) {
0544         dev_err(&hdev->dev, "failed U1_PITCH_SENS_Y (%d)\n", ret);
0545         goto exit;
0546     }
0547 
0548     ret = u1_read_write_register(hdev, ADDRESS_U1_RESO_DWN_ABS,
0549         &resolution, 0, true);
0550     if (ret < 0) {
0551         dev_err(&hdev->dev, "failed U1_RESO_DWN_ABS (%d)\n", ret);
0552         goto exit;
0553     }
0554     pri_data->x_active_len_mm =
0555         (pitch_x * (sen_line_num_x - 1)) / 10;
0556     pri_data->y_active_len_mm =
0557         (pitch_y * (sen_line_num_y - 1)) / 10;
0558 
0559     pri_data->x_max =
0560         (resolution << 2) * (sen_line_num_x - 1);
0561     pri_data->x_min = 1;
0562     pri_data->y_max =
0563         (resolution << 2) * (sen_line_num_y - 1);
0564     pri_data->y_min = 1;
0565 
0566     ret = u1_read_write_register(hdev, ADDRESS_U1_PAD_BTN,
0567             &tmp, 0, true);
0568     if (ret < 0) {
0569         dev_err(&hdev->dev, "failed U1_PAD_BTN (%d)\n", ret);
0570         goto exit;
0571     }
0572     if ((tmp & 0x0F) == (tmp & 0xF0) >> 4) {
0573         pri_data->btn_cnt = (tmp & 0x0F);
0574     } else {
0575         /* Button pad */
0576         pri_data->btn_cnt = 1;
0577     }
0578 
0579     pri_data->has_sp = 0;
0580     /* Check StickPointer device */
0581     ret = u1_read_write_register(hdev, ADDRESS_U1_DEVICE_TYP,
0582             &tmp, 0, true);
0583     if (ret < 0) {
0584         dev_err(&hdev->dev, "failed U1_DEVICE_TYP (%d)\n", ret);
0585         goto exit;
0586     }
0587     if (tmp & U1_DEVTYPE_SP_SUPPORT) {
0588         dev_ctrl |= U1_SP_ABS_MODE;
0589         ret = u1_read_write_register(hdev, ADDRESS_U1_DEV_CTRL_1,
0590             NULL, dev_ctrl, false);
0591         if (ret < 0) {
0592             dev_err(&hdev->dev, "failed SP mode (%d)\n", ret);
0593             goto exit;
0594         }
0595 
0596         ret = u1_read_write_register(hdev, ADDRESS_U1_SP_BTN,
0597             &pri_data->sp_btn_info, 0, true);
0598         if (ret < 0) {
0599             dev_err(&hdev->dev, "failed U1_SP_BTN (%d)\n", ret);
0600             goto exit;
0601         }
0602         pri_data->has_sp = 1;
0603     }
0604     pri_data->max_fingers = 5;
0605 exit:
0606     return ret;
0607 }
0608 
0609 static int T4_init(struct hid_device *hdev, struct alps_dev *pri_data)
0610 {
0611     int ret;
0612     u8 tmp, sen_line_num_x, sen_line_num_y;
0613 
0614     ret = t4_read_write_register(hdev, T4_PRM_ID_CONFIG_3, &tmp, 0, true);
0615     if (ret < 0) {
0616         dev_err(&hdev->dev, "failed T4_PRM_ID_CONFIG_3 (%d)\n", ret);
0617         goto exit;
0618     }
0619     sen_line_num_x = 16 + ((tmp & 0x0F)  | (tmp & 0x08 ? 0xF0 : 0));
0620     sen_line_num_y = 12 + (((tmp & 0xF0) >> 4)  | (tmp & 0x80 ? 0xF0 : 0));
0621 
0622     pri_data->x_max = sen_line_num_x * T4_COUNT_PER_ELECTRODE;
0623     pri_data->x_min = T4_COUNT_PER_ELECTRODE;
0624     pri_data->y_max = sen_line_num_y * T4_COUNT_PER_ELECTRODE;
0625     pri_data->y_min = T4_COUNT_PER_ELECTRODE;
0626     pri_data->x_active_len_mm = pri_data->y_active_len_mm = 0;
0627     pri_data->btn_cnt = 1;
0628 
0629     ret = t4_read_write_register(hdev, PRM_SYS_CONFIG_1, &tmp, 0, true);
0630     if (ret < 0) {
0631         dev_err(&hdev->dev, "failed PRM_SYS_CONFIG_1 (%d)\n", ret);
0632         goto exit;
0633     }
0634     tmp |= 0x02;
0635     ret = t4_read_write_register(hdev, PRM_SYS_CONFIG_1, NULL, tmp, false);
0636     if (ret < 0) {
0637         dev_err(&hdev->dev, "failed PRM_SYS_CONFIG_1 (%d)\n", ret);
0638         goto exit;
0639     }
0640 
0641     ret = t4_read_write_register(hdev, T4_PRM_FEED_CONFIG_1,
0642                     NULL, T4_I2C_ABS, false);
0643     if (ret < 0) {
0644         dev_err(&hdev->dev, "failed T4_PRM_FEED_CONFIG_1 (%d)\n", ret);
0645         goto exit;
0646     }
0647 
0648     ret = t4_read_write_register(hdev, T4_PRM_FEED_CONFIG_4, NULL,
0649                 T4_FEEDCFG4_ADVANCED_ABS_ENABLE, false);
0650     if (ret < 0) {
0651         dev_err(&hdev->dev, "failed T4_PRM_FEED_CONFIG_4 (%d)\n", ret);
0652         goto exit;
0653     }
0654     pri_data->max_fingers = 5;
0655     pri_data->has_sp = 0;
0656 exit:
0657     return ret;
0658 }
0659 
0660 static int alps_sp_open(struct input_dev *dev)
0661 {
0662     struct hid_device *hid = input_get_drvdata(dev);
0663 
0664     return hid_hw_open(hid);
0665 }
0666 
0667 static void alps_sp_close(struct input_dev *dev)
0668 {
0669     struct hid_device *hid = input_get_drvdata(dev);
0670 
0671     hid_hw_close(hid);
0672 }
0673 
0674 static int alps_input_configured(struct hid_device *hdev, struct hid_input *hi)
0675 {
0676     struct alps_dev *data = hid_get_drvdata(hdev);
0677     struct input_dev *input = hi->input, *input2;
0678     int ret;
0679     int res_x, res_y, i;
0680 
0681     data->input = input;
0682 
0683     hid_dbg(hdev, "Opening low level driver\n");
0684     ret = hid_hw_open(hdev);
0685     if (ret)
0686         return ret;
0687 
0688     /* Allow incoming hid reports */
0689     hid_device_io_start(hdev);
0690     switch (data->dev_type) {
0691     case T4:
0692         ret = T4_init(hdev, data);
0693         break;
0694     case U1:
0695         ret = u1_init(hdev, data);
0696         break;
0697     default:
0698         break;
0699     }
0700 
0701     if (ret)
0702         goto exit;
0703 
0704     __set_bit(EV_ABS, input->evbit);
0705     input_set_abs_params(input, ABS_MT_POSITION_X,
0706                         data->x_min, data->x_max, 0, 0);
0707     input_set_abs_params(input, ABS_MT_POSITION_Y,
0708                         data->y_min, data->y_max, 0, 0);
0709 
0710     if (data->x_active_len_mm && data->y_active_len_mm) {
0711         res_x = (data->x_max - 1) / data->x_active_len_mm;
0712         res_y = (data->y_max - 1) / data->y_active_len_mm;
0713 
0714         input_abs_set_res(input, ABS_MT_POSITION_X, res_x);
0715         input_abs_set_res(input, ABS_MT_POSITION_Y, res_y);
0716     }
0717 
0718     input_set_abs_params(input, ABS_MT_PRESSURE, 0, 64, 0, 0);
0719 
0720     input_mt_init_slots(input, data->max_fingers, INPUT_MT_POINTER);
0721 
0722     __set_bit(EV_KEY, input->evbit);
0723 
0724     if (data->btn_cnt == 1)
0725         __set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
0726 
0727     for (i = 0; i < data->btn_cnt; i++)
0728         __set_bit(BTN_LEFT + i, input->keybit);
0729 
0730     /* Stick device initialization */
0731     if (data->has_sp) {
0732         input2 = input_allocate_device();
0733         if (!input2) {
0734             ret = -ENOMEM;
0735             goto exit;
0736         }
0737 
0738         data->input2 = input2;
0739         input2->phys = input->phys;
0740         input2->name = "DualPoint Stick";
0741         input2->id.bustype = BUS_I2C;
0742         input2->id.vendor  = input->id.vendor;
0743         input2->id.product = input->id.product;
0744         input2->id.version = input->id.version;
0745         input2->dev.parent = input->dev.parent;
0746 
0747         input_set_drvdata(input2, hdev);
0748         input2->open = alps_sp_open;
0749         input2->close = alps_sp_close;
0750 
0751         __set_bit(EV_KEY, input2->evbit);
0752         data->sp_btn_cnt = (data->sp_btn_info & 0x0F);
0753         for (i = 0; i < data->sp_btn_cnt; i++)
0754             __set_bit(BTN_LEFT + i, input2->keybit);
0755 
0756         __set_bit(EV_REL, input2->evbit);
0757         __set_bit(REL_X, input2->relbit);
0758         __set_bit(REL_Y, input2->relbit);
0759         __set_bit(INPUT_PROP_POINTER, input2->propbit);
0760         __set_bit(INPUT_PROP_POINTING_STICK, input2->propbit);
0761 
0762         if (input_register_device(data->input2)) {
0763             input_free_device(input2);
0764             ret = -ENOENT;
0765             goto exit;
0766         }
0767     }
0768 
0769 exit:
0770     hid_device_io_stop(hdev);
0771     hid_hw_close(hdev);
0772     return ret;
0773 }
0774 
0775 static int alps_input_mapping(struct hid_device *hdev,
0776         struct hid_input *hi, struct hid_field *field,
0777         struct hid_usage *usage, unsigned long **bit, int *max)
0778 {
0779     return -1;
0780 }
0781 
0782 static int alps_probe(struct hid_device *hdev, const struct hid_device_id *id)
0783 {
0784     struct alps_dev *data = NULL;
0785     int ret;
0786     data = devm_kzalloc(&hdev->dev, sizeof(struct alps_dev), GFP_KERNEL);
0787     if (!data)
0788         return -ENOMEM;
0789 
0790     data->hdev = hdev;
0791     hid_set_drvdata(hdev, data);
0792 
0793     hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
0794 
0795     ret = hid_parse(hdev);
0796     if (ret) {
0797         hid_err(hdev, "parse failed\n");
0798         return ret;
0799     }
0800 
0801     switch (hdev->product) {
0802     case HID_DEVICE_ID_ALPS_T4_BTNLESS:
0803         data->dev_type = T4;
0804         break;
0805     case HID_DEVICE_ID_ALPS_U1_DUAL:
0806     case HID_DEVICE_ID_ALPS_U1:
0807     case HID_DEVICE_ID_ALPS_U1_UNICORN_LEGACY:
0808         data->dev_type = U1;
0809         break;
0810     default:
0811         data->dev_type = UNKNOWN;
0812     }
0813 
0814     ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
0815     if (ret) {
0816         hid_err(hdev, "hw start failed\n");
0817         return ret;
0818     }
0819 
0820     return 0;
0821 }
0822 
0823 static void alps_remove(struct hid_device *hdev)
0824 {
0825     hid_hw_stop(hdev);
0826 }
0827 
0828 static const struct hid_device_id alps_id[] = {
0829     { HID_DEVICE(HID_BUS_ANY, HID_GROUP_ANY,
0830         USB_VENDOR_ID_ALPS_JP, HID_DEVICE_ID_ALPS_U1_DUAL) },
0831     { HID_DEVICE(HID_BUS_ANY, HID_GROUP_ANY,
0832         USB_VENDOR_ID_ALPS_JP, HID_DEVICE_ID_ALPS_U1) },
0833     { HID_DEVICE(HID_BUS_ANY, HID_GROUP_ANY,
0834         USB_VENDOR_ID_ALPS_JP, HID_DEVICE_ID_ALPS_U1_UNICORN_LEGACY) },
0835     { HID_DEVICE(HID_BUS_ANY, HID_GROUP_ANY,
0836         USB_VENDOR_ID_ALPS_JP, HID_DEVICE_ID_ALPS_T4_BTNLESS) },
0837     { }
0838 };
0839 MODULE_DEVICE_TABLE(hid, alps_id);
0840 
0841 static struct hid_driver alps_driver = {
0842     .name = "hid-alps",
0843     .id_table       = alps_id,
0844     .probe          = alps_probe,
0845     .remove         = alps_remove,
0846     .raw_event      = alps_raw_event,
0847     .input_mapping      = alps_input_mapping,
0848     .input_configured   = alps_input_configured,
0849 #ifdef CONFIG_PM
0850     .resume         = alps_post_resume,
0851     .reset_resume       = alps_post_reset,
0852 #endif
0853 };
0854 
0855 module_hid_driver(alps_driver);
0856 
0857 MODULE_AUTHOR("Masaki Ota <masaki.ota@jp.alps.com>");
0858 MODULE_DESCRIPTION("ALPS HID driver");
0859 MODULE_LICENSE("GPL");