Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *  HID driver for N-Trig touchscreens
0004  *
0005  *  Copyright (c) 2008-2010 Rafi Rubin
0006  *  Copyright (c) 2009-2010 Stephane Chatty
0007  */
0008 
0009 /*
0010  */
0011 
0012 #include <linux/device.h>
0013 #include <linux/hid.h>
0014 #include <linux/usb.h>
0015 #include "usbhid/usbhid.h"
0016 #include <linux/module.h>
0017 #include <linux/slab.h>
0018 
0019 #include "hid-ids.h"
0020 
0021 #define NTRIG_DUPLICATE_USAGES  0x001
0022 
0023 static unsigned int min_width;
0024 module_param(min_width, uint, 0644);
0025 MODULE_PARM_DESC(min_width, "Minimum touch contact width to accept.");
0026 
0027 static unsigned int min_height;
0028 module_param(min_height, uint, 0644);
0029 MODULE_PARM_DESC(min_height, "Minimum touch contact height to accept.");
0030 
0031 static unsigned int activate_slack = 1;
0032 module_param(activate_slack, uint, 0644);
0033 MODULE_PARM_DESC(activate_slack, "Number of touch frames to ignore at "
0034          "the start of touch input.");
0035 
0036 static unsigned int deactivate_slack = 4;
0037 module_param(deactivate_slack, uint, 0644);
0038 MODULE_PARM_DESC(deactivate_slack, "Number of empty frames to ignore before "
0039          "deactivating touch.");
0040 
0041 static unsigned int activation_width = 64;
0042 module_param(activation_width, uint, 0644);
0043 MODULE_PARM_DESC(activation_width, "Width threshold to immediately start "
0044          "processing touch events.");
0045 
0046 static unsigned int activation_height = 32;
0047 module_param(activation_height, uint, 0644);
0048 MODULE_PARM_DESC(activation_height, "Height threshold to immediately start "
0049          "processing touch events.");
0050 
0051 struct ntrig_data {
0052     /* Incoming raw values for a single contact */
0053     __u16 x, y, w, h;
0054     __u16 id;
0055 
0056     bool tipswitch;
0057     bool confidence;
0058     bool first_contact_touch;
0059 
0060     bool reading_mt;
0061 
0062     __u8 mt_footer[4];
0063     __u8 mt_foot_count;
0064 
0065     /* The current activation state. */
0066     __s8 act_state;
0067 
0068     /* Empty frames to ignore before recognizing the end of activity */
0069     __s8 deactivate_slack;
0070 
0071     /* Frames to ignore before acknowledging the start of activity */
0072     __s8 activate_slack;
0073 
0074     /* Minimum size contact to accept */
0075     __u16 min_width;
0076     __u16 min_height;
0077 
0078     /* Threshold to override activation slack */
0079     __u16 activation_width;
0080     __u16 activation_height;
0081 
0082     __u16 sensor_logical_width;
0083     __u16 sensor_logical_height;
0084     __u16 sensor_physical_width;
0085     __u16 sensor_physical_height;
0086 };
0087 
0088 
0089 /*
0090  * This function converts the 4 byte raw firmware code into
0091  * a string containing 5 comma separated numbers.
0092  */
0093 static int ntrig_version_string(unsigned char *raw, char *buf)
0094 {
0095     __u8 a =  (raw[1] & 0x0e) >> 1;
0096     __u8 b =  (raw[0] & 0x3c) >> 2;
0097     __u8 c = ((raw[0] & 0x03) << 3) | ((raw[3] & 0xe0) >> 5);
0098     __u8 d = ((raw[3] & 0x07) << 3) | ((raw[2] & 0xe0) >> 5);
0099     __u8 e =   raw[2] & 0x07;
0100 
0101     /*
0102      * As yet unmapped bits:
0103      * 0b11000000 0b11110001 0b00011000 0b00011000
0104      */
0105 
0106     return sprintf(buf, "%u.%u.%u.%u.%u", a, b, c, d, e);
0107 }
0108 
0109 static inline int ntrig_get_mode(struct hid_device *hdev)
0110 {
0111     struct hid_report *report = hdev->report_enum[HID_FEATURE_REPORT].
0112                     report_id_hash[0x0d];
0113 
0114     if (!report || report->maxfield < 1 ||
0115         report->field[0]->report_count < 1)
0116         return -EINVAL;
0117 
0118     hid_hw_request(hdev, report, HID_REQ_GET_REPORT);
0119     hid_hw_wait(hdev);
0120     return (int)report->field[0]->value[0];
0121 }
0122 
0123 static inline void ntrig_set_mode(struct hid_device *hdev, const int mode)
0124 {
0125     struct hid_report *report;
0126     __u8 mode_commands[4] = { 0xe, 0xf, 0x1b, 0x10 };
0127 
0128     if (mode < 0 || mode > 3)
0129         return;
0130 
0131     report = hdev->report_enum[HID_FEATURE_REPORT].
0132          report_id_hash[mode_commands[mode]];
0133 
0134     if (!report)
0135         return;
0136 
0137     hid_hw_request(hdev, report, HID_REQ_GET_REPORT);
0138 }
0139 
0140 static void ntrig_report_version(struct hid_device *hdev)
0141 {
0142     int ret;
0143     char buf[20];
0144     struct usb_device *usb_dev = hid_to_usb_dev(hdev);
0145     unsigned char *data = kmalloc(8, GFP_KERNEL);
0146 
0147     if (!data)
0148         goto err_free;
0149 
0150     ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
0151                   USB_REQ_CLEAR_FEATURE,
0152                   USB_TYPE_CLASS | USB_RECIP_INTERFACE |
0153                   USB_DIR_IN,
0154                   0x30c, 1, data, 8,
0155                   USB_CTRL_SET_TIMEOUT);
0156 
0157     if (ret == 8) {
0158         ret = ntrig_version_string(&data[2], buf);
0159 
0160         hid_info(hdev, "Firmware version: %s (%02x%02x %02x%02x)\n",
0161              buf, data[2], data[3], data[4], data[5]);
0162     }
0163 
0164 err_free:
0165     kfree(data);
0166 }
0167 
0168 static ssize_t show_phys_width(struct device *dev,
0169                    struct device_attribute *attr,
0170                    char *buf)
0171 {
0172     struct hid_device *hdev = to_hid_device(dev);
0173     struct ntrig_data *nd = hid_get_drvdata(hdev);
0174 
0175     return sprintf(buf, "%d\n", nd->sensor_physical_width);
0176 }
0177 
0178 static DEVICE_ATTR(sensor_physical_width, S_IRUGO, show_phys_width, NULL);
0179 
0180 static ssize_t show_phys_height(struct device *dev,
0181                 struct device_attribute *attr,
0182                 char *buf)
0183 {
0184     struct hid_device *hdev = to_hid_device(dev);
0185     struct ntrig_data *nd = hid_get_drvdata(hdev);
0186 
0187     return sprintf(buf, "%d\n", nd->sensor_physical_height);
0188 }
0189 
0190 static DEVICE_ATTR(sensor_physical_height, S_IRUGO, show_phys_height, NULL);
0191 
0192 static ssize_t show_log_width(struct device *dev,
0193                   struct device_attribute *attr,
0194                   char *buf)
0195 {
0196     struct hid_device *hdev = to_hid_device(dev);
0197     struct ntrig_data *nd = hid_get_drvdata(hdev);
0198 
0199     return sprintf(buf, "%d\n", nd->sensor_logical_width);
0200 }
0201 
0202 static DEVICE_ATTR(sensor_logical_width, S_IRUGO, show_log_width, NULL);
0203 
0204 static ssize_t show_log_height(struct device *dev,
0205                    struct device_attribute *attr,
0206                    char *buf)
0207 {
0208     struct hid_device *hdev = to_hid_device(dev);
0209     struct ntrig_data *nd = hid_get_drvdata(hdev);
0210 
0211     return sprintf(buf, "%d\n", nd->sensor_logical_height);
0212 }
0213 
0214 static DEVICE_ATTR(sensor_logical_height, S_IRUGO, show_log_height, NULL);
0215 
0216 static ssize_t show_min_width(struct device *dev,
0217                   struct device_attribute *attr,
0218                   char *buf)
0219 {
0220     struct hid_device *hdev = to_hid_device(dev);
0221     struct ntrig_data *nd = hid_get_drvdata(hdev);
0222 
0223     return sprintf(buf, "%d\n", nd->min_width *
0224                     nd->sensor_physical_width /
0225                     nd->sensor_logical_width);
0226 }
0227 
0228 static ssize_t set_min_width(struct device *dev,
0229                  struct device_attribute *attr,
0230                  const char *buf, size_t count)
0231 {
0232     struct hid_device *hdev = to_hid_device(dev);
0233     struct ntrig_data *nd = hid_get_drvdata(hdev);
0234 
0235     unsigned long val;
0236 
0237     if (kstrtoul(buf, 0, &val))
0238         return -EINVAL;
0239 
0240     if (val > nd->sensor_physical_width)
0241         return -EINVAL;
0242 
0243     nd->min_width = val * nd->sensor_logical_width /
0244                   nd->sensor_physical_width;
0245 
0246     return count;
0247 }
0248 
0249 static DEVICE_ATTR(min_width, S_IWUSR | S_IRUGO, show_min_width, set_min_width);
0250 
0251 static ssize_t show_min_height(struct device *dev,
0252                    struct device_attribute *attr,
0253                    char *buf)
0254 {
0255     struct hid_device *hdev = to_hid_device(dev);
0256     struct ntrig_data *nd = hid_get_drvdata(hdev);
0257 
0258     return sprintf(buf, "%d\n", nd->min_height *
0259                     nd->sensor_physical_height /
0260                     nd->sensor_logical_height);
0261 }
0262 
0263 static ssize_t set_min_height(struct device *dev,
0264                   struct device_attribute *attr,
0265                   const char *buf, size_t count)
0266 {
0267     struct hid_device *hdev = to_hid_device(dev);
0268     struct ntrig_data *nd = hid_get_drvdata(hdev);
0269 
0270     unsigned long val;
0271 
0272     if (kstrtoul(buf, 0, &val))
0273         return -EINVAL;
0274 
0275     if (val > nd->sensor_physical_height)
0276         return -EINVAL;
0277 
0278     nd->min_height = val * nd->sensor_logical_height /
0279                    nd->sensor_physical_height;
0280 
0281     return count;
0282 }
0283 
0284 static DEVICE_ATTR(min_height, S_IWUSR | S_IRUGO, show_min_height,
0285            set_min_height);
0286 
0287 static ssize_t show_activate_slack(struct device *dev,
0288                    struct device_attribute *attr,
0289                    char *buf)
0290 {
0291     struct hid_device *hdev = to_hid_device(dev);
0292     struct ntrig_data *nd = hid_get_drvdata(hdev);
0293 
0294     return sprintf(buf, "%d\n", nd->activate_slack);
0295 }
0296 
0297 static ssize_t set_activate_slack(struct device *dev,
0298                   struct device_attribute *attr,
0299                   const char *buf, size_t count)
0300 {
0301     struct hid_device *hdev = to_hid_device(dev);
0302     struct ntrig_data *nd = hid_get_drvdata(hdev);
0303 
0304     unsigned long val;
0305 
0306     if (kstrtoul(buf, 0, &val))
0307         return -EINVAL;
0308 
0309     if (val > 0x7f)
0310         return -EINVAL;
0311 
0312     nd->activate_slack = val;
0313 
0314     return count;
0315 }
0316 
0317 static DEVICE_ATTR(activate_slack, S_IWUSR | S_IRUGO, show_activate_slack,
0318            set_activate_slack);
0319 
0320 static ssize_t show_activation_width(struct device *dev,
0321                      struct device_attribute *attr,
0322                      char *buf)
0323 {
0324     struct hid_device *hdev = to_hid_device(dev);
0325     struct ntrig_data *nd = hid_get_drvdata(hdev);
0326 
0327     return sprintf(buf, "%d\n", nd->activation_width *
0328                     nd->sensor_physical_width /
0329                     nd->sensor_logical_width);
0330 }
0331 
0332 static ssize_t set_activation_width(struct device *dev,
0333                     struct device_attribute *attr,
0334                     const char *buf, size_t count)
0335 {
0336     struct hid_device *hdev = to_hid_device(dev);
0337     struct ntrig_data *nd = hid_get_drvdata(hdev);
0338 
0339     unsigned long val;
0340 
0341     if (kstrtoul(buf, 0, &val))
0342         return -EINVAL;
0343 
0344     if (val > nd->sensor_physical_width)
0345         return -EINVAL;
0346 
0347     nd->activation_width = val * nd->sensor_logical_width /
0348                      nd->sensor_physical_width;
0349 
0350     return count;
0351 }
0352 
0353 static DEVICE_ATTR(activation_width, S_IWUSR | S_IRUGO, show_activation_width,
0354            set_activation_width);
0355 
0356 static ssize_t show_activation_height(struct device *dev,
0357                       struct device_attribute *attr,
0358                       char *buf)
0359 {
0360     struct hid_device *hdev = to_hid_device(dev);
0361     struct ntrig_data *nd = hid_get_drvdata(hdev);
0362 
0363     return sprintf(buf, "%d\n", nd->activation_height *
0364                     nd->sensor_physical_height /
0365                     nd->sensor_logical_height);
0366 }
0367 
0368 static ssize_t set_activation_height(struct device *dev,
0369                      struct device_attribute *attr,
0370                      const char *buf, size_t count)
0371 {
0372     struct hid_device *hdev = to_hid_device(dev);
0373     struct ntrig_data *nd = hid_get_drvdata(hdev);
0374 
0375     unsigned long val;
0376 
0377     if (kstrtoul(buf, 0, &val))
0378         return -EINVAL;
0379 
0380     if (val > nd->sensor_physical_height)
0381         return -EINVAL;
0382 
0383     nd->activation_height = val * nd->sensor_logical_height /
0384                       nd->sensor_physical_height;
0385 
0386     return count;
0387 }
0388 
0389 static DEVICE_ATTR(activation_height, S_IWUSR | S_IRUGO,
0390            show_activation_height, set_activation_height);
0391 
0392 static ssize_t show_deactivate_slack(struct device *dev,
0393                      struct device_attribute *attr,
0394                      char *buf)
0395 {
0396     struct hid_device *hdev = to_hid_device(dev);
0397     struct ntrig_data *nd = hid_get_drvdata(hdev);
0398 
0399     return sprintf(buf, "%d\n", -nd->deactivate_slack);
0400 }
0401 
0402 static ssize_t set_deactivate_slack(struct device *dev,
0403                     struct device_attribute *attr,
0404                     const char *buf, size_t count)
0405 {
0406     struct hid_device *hdev = to_hid_device(dev);
0407     struct ntrig_data *nd = hid_get_drvdata(hdev);
0408 
0409     unsigned long val;
0410 
0411     if (kstrtoul(buf, 0, &val))
0412         return -EINVAL;
0413 
0414     /*
0415      * No more than 8 terminal frames have been observed so far
0416      * and higher slack is highly likely to leave the single
0417      * touch emulation stuck down.
0418      */
0419     if (val > 7)
0420         return -EINVAL;
0421 
0422     nd->deactivate_slack = -val;
0423 
0424     return count;
0425 }
0426 
0427 static DEVICE_ATTR(deactivate_slack, S_IWUSR | S_IRUGO, show_deactivate_slack,
0428            set_deactivate_slack);
0429 
0430 static struct attribute *sysfs_attrs[] = {
0431     &dev_attr_sensor_physical_width.attr,
0432     &dev_attr_sensor_physical_height.attr,
0433     &dev_attr_sensor_logical_width.attr,
0434     &dev_attr_sensor_logical_height.attr,
0435     &dev_attr_min_height.attr,
0436     &dev_attr_min_width.attr,
0437     &dev_attr_activate_slack.attr,
0438     &dev_attr_activation_width.attr,
0439     &dev_attr_activation_height.attr,
0440     &dev_attr_deactivate_slack.attr,
0441     NULL
0442 };
0443 
0444 static const struct attribute_group ntrig_attribute_group = {
0445     .attrs = sysfs_attrs
0446 };
0447 
0448 /*
0449  * this driver is aimed at two firmware versions in circulation:
0450  *  - dual pen/finger single touch
0451  *  - finger multitouch, pen not working
0452  */
0453 
0454 static int ntrig_input_mapping(struct hid_device *hdev, struct hid_input *hi,
0455                    struct hid_field *field, struct hid_usage *usage,
0456                    unsigned long **bit, int *max)
0457 {
0458     struct ntrig_data *nd = hid_get_drvdata(hdev);
0459 
0460     /* No special mappings needed for the pen and single touch */
0461     if (field->physical)
0462         return 0;
0463 
0464     switch (usage->hid & HID_USAGE_PAGE) {
0465     case HID_UP_GENDESK:
0466         switch (usage->hid) {
0467         case HID_GD_X:
0468             hid_map_usage(hi, usage, bit, max,
0469                     EV_ABS, ABS_MT_POSITION_X);
0470             input_set_abs_params(hi->input, ABS_X,
0471                     field->logical_minimum,
0472                     field->logical_maximum, 0, 0);
0473 
0474             if (!nd->sensor_logical_width) {
0475                 nd->sensor_logical_width =
0476                     field->logical_maximum -
0477                     field->logical_minimum;
0478                 nd->sensor_physical_width =
0479                     field->physical_maximum -
0480                     field->physical_minimum;
0481                 nd->activation_width = activation_width *
0482                     nd->sensor_logical_width /
0483                     nd->sensor_physical_width;
0484                 nd->min_width = min_width *
0485                     nd->sensor_logical_width /
0486                     nd->sensor_physical_width;
0487             }
0488             return 1;
0489         case HID_GD_Y:
0490             hid_map_usage(hi, usage, bit, max,
0491                     EV_ABS, ABS_MT_POSITION_Y);
0492             input_set_abs_params(hi->input, ABS_Y,
0493                     field->logical_minimum,
0494                     field->logical_maximum, 0, 0);
0495 
0496             if (!nd->sensor_logical_height) {
0497                 nd->sensor_logical_height =
0498                     field->logical_maximum -
0499                     field->logical_minimum;
0500                 nd->sensor_physical_height =
0501                     field->physical_maximum -
0502                     field->physical_minimum;
0503                 nd->activation_height = activation_height *
0504                     nd->sensor_logical_height /
0505                     nd->sensor_physical_height;
0506                 nd->min_height = min_height *
0507                     nd->sensor_logical_height /
0508                     nd->sensor_physical_height;
0509             }
0510             return 1;
0511         }
0512         return 0;
0513 
0514     case HID_UP_DIGITIZER:
0515         switch (usage->hid) {
0516         /* we do not want to map these for now */
0517         case HID_DG_CONTACTID: /* Not trustworthy, squelch for now */
0518         case HID_DG_INPUTMODE:
0519         case HID_DG_DEVICEINDEX:
0520         case HID_DG_CONTACTMAX:
0521             return -1;
0522 
0523         /* width/height mapped on TouchMajor/TouchMinor/Orientation */
0524         case HID_DG_WIDTH:
0525             hid_map_usage(hi, usage, bit, max,
0526                       EV_ABS, ABS_MT_TOUCH_MAJOR);
0527             return 1;
0528         case HID_DG_HEIGHT:
0529             hid_map_usage(hi, usage, bit, max,
0530                       EV_ABS, ABS_MT_TOUCH_MINOR);
0531             input_set_abs_params(hi->input, ABS_MT_ORIENTATION,
0532                          0, 1, 0, 0);
0533             return 1;
0534         }
0535         return 0;
0536 
0537     case 0xff000000:
0538         /* we do not want to map these: no input-oriented meaning */
0539         return -1;
0540     }
0541 
0542     return 0;
0543 }
0544 
0545 static int ntrig_input_mapped(struct hid_device *hdev, struct hid_input *hi,
0546                   struct hid_field *field, struct hid_usage *usage,
0547                   unsigned long **bit, int *max)
0548 {
0549     /* No special mappings needed for the pen and single touch */
0550     if (field->physical)
0551         return 0;
0552 
0553     if (usage->type == EV_KEY || usage->type == EV_REL
0554             || usage->type == EV_ABS)
0555         clear_bit(usage->code, *bit);
0556 
0557     return 0;
0558 }
0559 
0560 /*
0561  * this function is called upon all reports
0562  * so that we can filter contact point information,
0563  * decide whether we are in multi or single touch mode
0564  * and call input_mt_sync after each point if necessary
0565  */
0566 static int ntrig_event (struct hid_device *hid, struct hid_field *field,
0567             struct hid_usage *usage, __s32 value)
0568 {
0569     struct ntrig_data *nd = hid_get_drvdata(hid);
0570     struct input_dev *input;
0571 
0572     /* Skip processing if not a claimed input */
0573     if (!(hid->claimed & HID_CLAIMED_INPUT))
0574         goto not_claimed_input;
0575 
0576     /* This function is being called before the structures are fully
0577      * initialized */
0578     if(!(field->hidinput && field->hidinput->input))
0579         return -EINVAL;
0580 
0581     input = field->hidinput->input;
0582 
0583     /* No special handling needed for the pen */
0584     if (field->application == HID_DG_PEN)
0585         return 0;
0586 
0587     switch (usage->hid) {
0588     case 0xff000001:
0589         /* Tag indicating the start of a multitouch group */
0590         nd->reading_mt = true;
0591         nd->first_contact_touch = false;
0592         break;
0593     case HID_DG_TIPSWITCH:
0594         nd->tipswitch = value;
0595         /* Prevent emission of touch until validated */
0596         return 1;
0597     case HID_DG_CONFIDENCE:
0598         nd->confidence = value;
0599         break;
0600     case HID_GD_X:
0601         nd->x = value;
0602         /* Clear the contact footer */
0603         nd->mt_foot_count = 0;
0604         break;
0605     case HID_GD_Y:
0606         nd->y = value;
0607         break;
0608     case HID_DG_CONTACTID:
0609         nd->id = value;
0610         break;
0611     case HID_DG_WIDTH:
0612         nd->w = value;
0613         break;
0614     case HID_DG_HEIGHT:
0615         nd->h = value;
0616         /*
0617          * when in single touch mode, this is the last
0618          * report received in a finger event. We want
0619          * to emit a normal (X, Y) position
0620          */
0621         if (!nd->reading_mt) {
0622             /*
0623              * TipSwitch indicates the presence of a
0624              * finger in single touch mode.
0625              */
0626             input_report_key(input, BTN_TOUCH,
0627                      nd->tipswitch);
0628             input_report_key(input, BTN_TOOL_DOUBLETAP,
0629                      nd->tipswitch);
0630             input_event(input, EV_ABS, ABS_X, nd->x);
0631             input_event(input, EV_ABS, ABS_Y, nd->y);
0632         }
0633         break;
0634     case 0xff000002:
0635         /*
0636          * we receive this when the device is in multitouch
0637          * mode. The first of the three values tagged with
0638          * this usage tells if the contact point is real
0639          * or a placeholder
0640          */
0641 
0642         /* Shouldn't get more than 4 footer packets, so skip */
0643         if (nd->mt_foot_count >= 4)
0644             break;
0645 
0646         nd->mt_footer[nd->mt_foot_count++] = value;
0647 
0648         /* if the footer isn't complete break */
0649         if (nd->mt_foot_count != 4)
0650             break;
0651 
0652         /* Pen activity signal. */
0653         if (nd->mt_footer[2]) {
0654             /*
0655              * When the pen deactivates touch, we see a
0656              * bogus frame with ContactCount > 0.
0657              * We can
0658              * save a bit of work by ensuring act_state < 0
0659              * even if deactivation slack is turned off.
0660              */
0661             nd->act_state = deactivate_slack - 1;
0662             nd->confidence = false;
0663             break;
0664         }
0665 
0666         /*
0667          * The first footer value indicates the presence of a
0668          * finger.
0669          */
0670         if (nd->mt_footer[0]) {
0671             /*
0672              * We do not want to process contacts under
0673              * the size threshold, but do not want to
0674              * ignore them for activation state
0675              */
0676             if (nd->w < nd->min_width ||
0677                 nd->h < nd->min_height)
0678                 nd->confidence = false;
0679         } else
0680             break;
0681 
0682         if (nd->act_state > 0) {
0683             /*
0684              * Contact meets the activation size threshold
0685              */
0686             if (nd->w >= nd->activation_width &&
0687                 nd->h >= nd->activation_height) {
0688                 if (nd->id)
0689                     /*
0690                      * first contact, activate now
0691                      */
0692                     nd->act_state = 0;
0693                 else {
0694                     /*
0695                      * avoid corrupting this frame
0696                      * but ensure next frame will
0697                      * be active
0698                      */
0699                     nd->act_state = 1;
0700                     break;
0701                 }
0702             } else
0703                 /*
0704                  * Defer adjusting the activation state
0705                  * until the end of the frame.
0706                  */
0707                 break;
0708         }
0709 
0710         /* Discarding this contact */
0711         if (!nd->confidence)
0712             break;
0713 
0714         /* emit a normal (X, Y) for the first point only */
0715         if (nd->id == 0) {
0716             /*
0717              * TipSwitch is superfluous in multitouch
0718              * mode.  The footer events tell us
0719              * if there is a finger on the screen or
0720              * not.
0721              */
0722             nd->first_contact_touch = nd->confidence;
0723             input_event(input, EV_ABS, ABS_X, nd->x);
0724             input_event(input, EV_ABS, ABS_Y, nd->y);
0725         }
0726 
0727         /* Emit MT events */
0728         input_event(input, EV_ABS, ABS_MT_POSITION_X, nd->x);
0729         input_event(input, EV_ABS, ABS_MT_POSITION_Y, nd->y);
0730 
0731         /*
0732          * Translate from height and width to size
0733          * and orientation.
0734          */
0735         if (nd->w > nd->h) {
0736             input_event(input, EV_ABS,
0737                     ABS_MT_ORIENTATION, 1);
0738             input_event(input, EV_ABS,
0739                     ABS_MT_TOUCH_MAJOR, nd->w);
0740             input_event(input, EV_ABS,
0741                     ABS_MT_TOUCH_MINOR, nd->h);
0742         } else {
0743             input_event(input, EV_ABS,
0744                     ABS_MT_ORIENTATION, 0);
0745             input_event(input, EV_ABS,
0746                     ABS_MT_TOUCH_MAJOR, nd->h);
0747             input_event(input, EV_ABS,
0748                     ABS_MT_TOUCH_MINOR, nd->w);
0749         }
0750         input_mt_sync(field->hidinput->input);
0751         break;
0752 
0753     case HID_DG_CONTACTCOUNT: /* End of a multitouch group */
0754         if (!nd->reading_mt) /* Just to be sure */
0755             break;
0756 
0757         nd->reading_mt = false;
0758 
0759 
0760         /*
0761          * Activation state machine logic:
0762          *
0763          * Fundamental states:
0764          *  state >  0: Inactive
0765          *  state <= 0: Active
0766          *  state <  -deactivate_slack:
0767          *       Pen termination of touch
0768          *
0769          * Specific values of interest
0770          *  state == activate_slack
0771          *       no valid input since the last reset
0772          *
0773          *  state == 0
0774          *       general operational state
0775          *
0776          *  state == -deactivate_slack
0777          *       read sufficient empty frames to accept
0778          *       the end of input and reset
0779          */
0780 
0781         if (nd->act_state > 0) { /* Currently inactive */
0782             if (value)
0783                 /*
0784                  * Consider each live contact as
0785                  * evidence of intentional activity.
0786                  */
0787                 nd->act_state = (nd->act_state > value)
0788                         ? nd->act_state - value
0789                         : 0;
0790             else
0791                 /*
0792                  * Empty frame before we hit the
0793                  * activity threshold, reset.
0794                  */
0795                 nd->act_state = nd->activate_slack;
0796 
0797             /*
0798              * Entered this block inactive and no
0799              * coordinates sent this frame, so hold off
0800              * on button state.
0801              */
0802             break;
0803         } else { /* Currently active */
0804             if (value && nd->act_state >=
0805                      nd->deactivate_slack)
0806                 /*
0807                  * Live point: clear accumulated
0808                  * deactivation count.
0809                  */
0810                 nd->act_state = 0;
0811             else if (nd->act_state <= nd->deactivate_slack)
0812                 /*
0813                  * We've consumed the deactivation
0814                  * slack, time to deactivate and reset.
0815                  */
0816                 nd->act_state =
0817                     nd->activate_slack;
0818             else { /* Move towards deactivation */
0819                 nd->act_state--;
0820                 break;
0821             }
0822         }
0823 
0824         if (nd->first_contact_touch && nd->act_state <= 0) {
0825             /*
0826              * Check to see if we're ready to start
0827              * emitting touch events.
0828              *
0829              * Note: activation slack will decrease over
0830              * the course of the frame, and it will be
0831              * inconsistent from the start to the end of
0832              * the frame.  However if the frame starts
0833              * with slack, first_contact_touch will still
0834              * be 0 and we will not get to this point.
0835              */
0836             input_report_key(input, BTN_TOOL_DOUBLETAP, 1);
0837             input_report_key(input, BTN_TOUCH, 1);
0838         } else {
0839             input_report_key(input, BTN_TOOL_DOUBLETAP, 0);
0840             input_report_key(input, BTN_TOUCH, 0);
0841         }
0842         break;
0843 
0844     default:
0845         /* fall-back to the generic hidinput handling */
0846         return 0;
0847     }
0848 
0849 not_claimed_input:
0850 
0851     /* we have handled the hidinput part, now remains hiddev */
0852     if ((hid->claimed & HID_CLAIMED_HIDDEV) && hid->hiddev_hid_event)
0853         hid->hiddev_hid_event(hid, field, usage, value);
0854 
0855     return 1;
0856 }
0857 
0858 static int ntrig_input_configured(struct hid_device *hid,
0859         struct hid_input *hidinput)
0860 
0861 {
0862     struct input_dev *input = hidinput->input;
0863 
0864     if (hidinput->report->maxfield < 1)
0865         return 0;
0866 
0867     switch (hidinput->report->field[0]->application) {
0868     case HID_DG_PEN:
0869         input->name = "N-Trig Pen";
0870         break;
0871     case HID_DG_TOUCHSCREEN:
0872         /* These keys are redundant for fingers, clear them
0873          * to prevent incorrect identification */
0874         __clear_bit(BTN_TOOL_PEN, input->keybit);
0875         __clear_bit(BTN_TOOL_FINGER, input->keybit);
0876         __clear_bit(BTN_0, input->keybit);
0877         __set_bit(BTN_TOOL_DOUBLETAP, input->keybit);
0878         /*
0879          * The physical touchscreen (single touch)
0880          * input has a value for physical, whereas
0881          * the multitouch only has logical input
0882          * fields.
0883          */
0884         input->name = (hidinput->report->field[0]->physical) ?
0885                             "N-Trig Touchscreen" :
0886                             "N-Trig MultiTouch";
0887         break;
0888     }
0889 
0890     return 0;
0891 }
0892 
0893 static int ntrig_probe(struct hid_device *hdev, const struct hid_device_id *id)
0894 {
0895     int ret;
0896     struct ntrig_data *nd;
0897     struct hid_report *report;
0898 
0899     if (id->driver_data)
0900         hdev->quirks |= HID_QUIRK_MULTI_INPUT
0901                 | HID_QUIRK_NO_INIT_REPORTS;
0902 
0903     nd = kmalloc(sizeof(struct ntrig_data), GFP_KERNEL);
0904     if (!nd) {
0905         hid_err(hdev, "cannot allocate N-Trig data\n");
0906         return -ENOMEM;
0907     }
0908 
0909     nd->reading_mt = false;
0910     nd->min_width = 0;
0911     nd->min_height = 0;
0912     nd->activate_slack = activate_slack;
0913     nd->act_state = activate_slack;
0914     nd->deactivate_slack = -deactivate_slack;
0915     nd->sensor_logical_width = 1;
0916     nd->sensor_logical_height = 1;
0917     nd->sensor_physical_width = 1;
0918     nd->sensor_physical_height = 1;
0919 
0920     hid_set_drvdata(hdev, nd);
0921 
0922     ret = hid_parse(hdev);
0923     if (ret) {
0924         hid_err(hdev, "parse failed\n");
0925         goto err_free;
0926     }
0927 
0928     ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
0929     if (ret) {
0930         hid_err(hdev, "hw start failed\n");
0931         goto err_free;
0932     }
0933 
0934     /* This is needed for devices with more recent firmware versions */
0935     report = hdev->report_enum[HID_FEATURE_REPORT].report_id_hash[0x0a];
0936     if (report) {
0937         /* Let the device settle to ensure the wakeup message gets
0938          * through */
0939         hid_hw_wait(hdev);
0940         hid_hw_request(hdev, report, HID_REQ_GET_REPORT);
0941 
0942         /*
0943          * Sanity check: if the current mode is invalid reset it to
0944          * something reasonable.
0945          */
0946         if (ntrig_get_mode(hdev) >= 4)
0947             ntrig_set_mode(hdev, 3);
0948     }
0949 
0950     ntrig_report_version(hdev);
0951 
0952     ret = sysfs_create_group(&hdev->dev.kobj,
0953             &ntrig_attribute_group);
0954     if (ret)
0955         hid_err(hdev, "cannot create sysfs group\n");
0956 
0957     return 0;
0958 err_free:
0959     kfree(nd);
0960     return ret;
0961 }
0962 
0963 static void ntrig_remove(struct hid_device *hdev)
0964 {
0965     sysfs_remove_group(&hdev->dev.kobj,
0966                &ntrig_attribute_group);
0967     hid_hw_stop(hdev);
0968     kfree(hid_get_drvdata(hdev));
0969 }
0970 
0971 static const struct hid_device_id ntrig_devices[] = {
0972     { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN),
0973         .driver_data = NTRIG_DUPLICATE_USAGES },
0974     { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_1),
0975         .driver_data = NTRIG_DUPLICATE_USAGES },
0976     { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_2),
0977         .driver_data = NTRIG_DUPLICATE_USAGES },
0978     { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_3),
0979         .driver_data = NTRIG_DUPLICATE_USAGES },
0980     { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_4),
0981         .driver_data = NTRIG_DUPLICATE_USAGES },
0982     { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_5),
0983         .driver_data = NTRIG_DUPLICATE_USAGES },
0984     { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_6),
0985         .driver_data = NTRIG_DUPLICATE_USAGES },
0986     { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_7),
0987         .driver_data = NTRIG_DUPLICATE_USAGES },
0988     { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_8),
0989         .driver_data = NTRIG_DUPLICATE_USAGES },
0990     { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_9),
0991         .driver_data = NTRIG_DUPLICATE_USAGES },
0992     { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_10),
0993         .driver_data = NTRIG_DUPLICATE_USAGES },
0994     { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_11),
0995         .driver_data = NTRIG_DUPLICATE_USAGES },
0996     { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_12),
0997         .driver_data = NTRIG_DUPLICATE_USAGES },
0998     { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_13),
0999         .driver_data = NTRIG_DUPLICATE_USAGES },
1000     { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_14),
1001         .driver_data = NTRIG_DUPLICATE_USAGES },
1002     { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_15),
1003         .driver_data = NTRIG_DUPLICATE_USAGES },
1004     { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_16),
1005         .driver_data = NTRIG_DUPLICATE_USAGES },
1006     { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_17),
1007         .driver_data = NTRIG_DUPLICATE_USAGES },
1008     { HID_USB_DEVICE(USB_VENDOR_ID_NTRIG, USB_DEVICE_ID_NTRIG_TOUCH_SCREEN_18),
1009         .driver_data = NTRIG_DUPLICATE_USAGES },
1010     { }
1011 };
1012 MODULE_DEVICE_TABLE(hid, ntrig_devices);
1013 
1014 static const struct hid_usage_id ntrig_grabbed_usages[] = {
1015     { HID_ANY_ID, HID_ANY_ID, HID_ANY_ID },
1016     { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1 }
1017 };
1018 
1019 static struct hid_driver ntrig_driver = {
1020     .name = "ntrig",
1021     .id_table = ntrig_devices,
1022     .probe = ntrig_probe,
1023     .remove = ntrig_remove,
1024     .input_mapping = ntrig_input_mapping,
1025     .input_mapped = ntrig_input_mapped,
1026     .input_configured = ntrig_input_configured,
1027     .usage_table = ntrig_grabbed_usages,
1028     .event = ntrig_event,
1029 };
1030 module_hid_driver(ntrig_driver);
1031 
1032 MODULE_LICENSE("GPL");