Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *  HID driver for Asus notebook built-in keyboard.
0004  *  Fixes small logical maximum to match usage maximum.
0005  *
0006  *  Currently supported devices are:
0007  *    EeeBook X205TA
0008  *    VivoBook E200HA
0009  *
0010  *  Copyright (c) 2016 Yusuke Fujimaki <usk.fujimaki@gmail.com>
0011  *
0012  *  This module based on hid-ortek by
0013  *  Copyright (c) 2010 Johnathon Harris <jmharris@gmail.com>
0014  *  Copyright (c) 2011 Jiri Kosina
0015  *
0016  *  This module has been updated to add support for Asus i2c touchpad.
0017  *
0018  *  Copyright (c) 2016 Brendan McGrath <redmcg@redmandi.dyndns.org>
0019  *  Copyright (c) 2016 Victor Vlasenko <victor.vlasenko@sysgears.com>
0020  *  Copyright (c) 2016 Frederik Wenigwieser <frederik.wenigwieser@gmail.com>
0021  */
0022 
0023 /*
0024  */
0025 
0026 #include <linux/dmi.h>
0027 #include <linux/hid.h>
0028 #include <linux/module.h>
0029 #include <linux/platform_data/x86/asus-wmi.h>
0030 #include <linux/input/mt.h>
0031 #include <linux/usb.h> /* For to_usb_interface for T100 touchpad intf check */
0032 #include <linux/power_supply.h>
0033 
0034 #include "hid-ids.h"
0035 
0036 MODULE_AUTHOR("Yusuke Fujimaki <usk.fujimaki@gmail.com>");
0037 MODULE_AUTHOR("Brendan McGrath <redmcg@redmandi.dyndns.org>");
0038 MODULE_AUTHOR("Victor Vlasenko <victor.vlasenko@sysgears.com>");
0039 MODULE_AUTHOR("Frederik Wenigwieser <frederik.wenigwieser@gmail.com>");
0040 MODULE_DESCRIPTION("Asus HID Keyboard and TouchPad");
0041 
0042 #define T100_TPAD_INTF 2
0043 #define MEDION_E1239T_TPAD_INTF 1
0044 
0045 #define E1239T_TP_TOGGLE_REPORT_ID 0x05
0046 #define T100CHI_MOUSE_REPORT_ID 0x06
0047 #define FEATURE_REPORT_ID 0x0d
0048 #define INPUT_REPORT_ID 0x5d
0049 #define FEATURE_KBD_REPORT_ID 0x5a
0050 #define FEATURE_KBD_REPORT_SIZE 16
0051 #define FEATURE_KBD_LED_REPORT_ID1 0x5d
0052 #define FEATURE_KBD_LED_REPORT_ID2 0x5e
0053 
0054 #define SUPPORT_KBD_BACKLIGHT BIT(0)
0055 
0056 #define MAX_TOUCH_MAJOR 8
0057 #define MAX_PRESSURE 128
0058 
0059 #define BTN_LEFT_MASK 0x01
0060 #define CONTACT_TOOL_TYPE_MASK 0x80
0061 #define CONTACT_X_MSB_MASK 0xf0
0062 #define CONTACT_Y_MSB_MASK 0x0f
0063 #define CONTACT_TOUCH_MAJOR_MASK 0x07
0064 #define CONTACT_PRESSURE_MASK 0x7f
0065 
0066 #define BATTERY_REPORT_ID   (0x03)
0067 #define BATTERY_REPORT_SIZE (1 + 8)
0068 #define BATTERY_LEVEL_MAX   ((u8)255)
0069 #define BATTERY_STAT_DISCONNECT (0)
0070 #define BATTERY_STAT_CHARGING   (1)
0071 #define BATTERY_STAT_FULL   (2)
0072 
0073 #define QUIRK_FIX_NOTEBOOK_REPORT   BIT(0)
0074 #define QUIRK_NO_INIT_REPORTS       BIT(1)
0075 #define QUIRK_SKIP_INPUT_MAPPING    BIT(2)
0076 #define QUIRK_IS_MULTITOUCH     BIT(3)
0077 #define QUIRK_NO_CONSUMER_USAGES    BIT(4)
0078 #define QUIRK_USE_KBD_BACKLIGHT     BIT(5)
0079 #define QUIRK_T100_KEYBOARD     BIT(6)
0080 #define QUIRK_T100CHI           BIT(7)
0081 #define QUIRK_G752_KEYBOARD     BIT(8)
0082 #define QUIRK_T90CHI            BIT(9)
0083 #define QUIRK_MEDION_E1239T     BIT(10)
0084 #define QUIRK_ROG_NKEY_KEYBOARD     BIT(11)
0085 #define QUIRK_ROG_CLAYMORE_II_KEYBOARD BIT(12)
0086 
0087 #define I2C_KEYBOARD_QUIRKS         (QUIRK_FIX_NOTEBOOK_REPORT | \
0088                          QUIRK_NO_INIT_REPORTS | \
0089                          QUIRK_NO_CONSUMER_USAGES)
0090 #define I2C_TOUCHPAD_QUIRKS         (QUIRK_NO_INIT_REPORTS | \
0091                          QUIRK_SKIP_INPUT_MAPPING | \
0092                          QUIRK_IS_MULTITOUCH)
0093 
0094 #define TRKID_SGN       ((TRKID_MAX + 1) >> 1)
0095 
0096 struct asus_kbd_leds {
0097     struct led_classdev cdev;
0098     struct hid_device *hdev;
0099     struct work_struct work;
0100     unsigned int brightness;
0101     bool removed;
0102 };
0103 
0104 struct asus_touchpad_info {
0105     int max_x;
0106     int max_y;
0107     int res_x;
0108     int res_y;
0109     int contact_size;
0110     int max_contacts;
0111     int report_size;
0112 };
0113 
0114 struct asus_drvdata {
0115     unsigned long quirks;
0116     struct hid_device *hdev;
0117     struct input_dev *input;
0118     struct input_dev *tp_kbd_input;
0119     struct asus_kbd_leds *kbd_backlight;
0120     const struct asus_touchpad_info *tp;
0121     bool enable_backlight;
0122     struct power_supply *battery;
0123     struct power_supply_desc battery_desc;
0124     int battery_capacity;
0125     int battery_stat;
0126     bool battery_in_query;
0127     unsigned long battery_next_query;
0128 };
0129 
0130 static int asus_report_battery(struct asus_drvdata *, u8 *, int);
0131 
0132 static const struct asus_touchpad_info asus_i2c_tp = {
0133     .max_x = 2794,
0134     .max_y = 1758,
0135     .contact_size = 5,
0136     .max_contacts = 5,
0137     .report_size = 28 /* 2 byte header + 5 * 5 + 1 byte footer */,
0138 };
0139 
0140 static const struct asus_touchpad_info asus_t100ta_tp = {
0141     .max_x = 2240,
0142     .max_y = 1120,
0143     .res_x = 30, /* units/mm */
0144     .res_y = 27, /* units/mm */
0145     .contact_size = 5,
0146     .max_contacts = 5,
0147     .report_size = 28 /* 2 byte header + 5 * 5 + 1 byte footer */,
0148 };
0149 
0150 static const struct asus_touchpad_info asus_t100ha_tp = {
0151     .max_x = 2640,
0152     .max_y = 1320,
0153     .res_x = 30, /* units/mm */
0154     .res_y = 29, /* units/mm */
0155     .contact_size = 5,
0156     .max_contacts = 5,
0157     .report_size = 28 /* 2 byte header + 5 * 5 + 1 byte footer */,
0158 };
0159 
0160 static const struct asus_touchpad_info asus_t200ta_tp = {
0161     .max_x = 3120,
0162     .max_y = 1716,
0163     .res_x = 30, /* units/mm */
0164     .res_y = 28, /* units/mm */
0165     .contact_size = 5,
0166     .max_contacts = 5,
0167     .report_size = 28 /* 2 byte header + 5 * 5 + 1 byte footer */,
0168 };
0169 
0170 static const struct asus_touchpad_info asus_t100chi_tp = {
0171     .max_x = 2640,
0172     .max_y = 1320,
0173     .res_x = 31, /* units/mm */
0174     .res_y = 29, /* units/mm */
0175     .contact_size = 3,
0176     .max_contacts = 4,
0177     .report_size = 15 /* 2 byte header + 3 * 4 + 1 byte footer */,
0178 };
0179 
0180 static const struct asus_touchpad_info medion_e1239t_tp = {
0181     .max_x = 2640,
0182     .max_y = 1380,
0183     .res_x = 29, /* units/mm */
0184     .res_y = 28, /* units/mm */
0185     .contact_size = 5,
0186     .max_contacts = 5,
0187     .report_size = 32 /* 2 byte header + 5 * 5 + 5 byte footer */,
0188 };
0189 
0190 static void asus_report_contact_down(struct asus_drvdata *drvdat,
0191         int toolType, u8 *data)
0192 {
0193     struct input_dev *input = drvdat->input;
0194     int touch_major, pressure, x, y;
0195 
0196     x = (data[0] & CONTACT_X_MSB_MASK) << 4 | data[1];
0197     y = drvdat->tp->max_y - ((data[0] & CONTACT_Y_MSB_MASK) << 8 | data[2]);
0198 
0199     input_report_abs(input, ABS_MT_POSITION_X, x);
0200     input_report_abs(input, ABS_MT_POSITION_Y, y);
0201 
0202     if (drvdat->tp->contact_size < 5)
0203         return;
0204 
0205     if (toolType == MT_TOOL_PALM) {
0206         touch_major = MAX_TOUCH_MAJOR;
0207         pressure = MAX_PRESSURE;
0208     } else {
0209         touch_major = (data[3] >> 4) & CONTACT_TOUCH_MAJOR_MASK;
0210         pressure = data[4] & CONTACT_PRESSURE_MASK;
0211     }
0212 
0213     input_report_abs(input, ABS_MT_TOUCH_MAJOR, touch_major);
0214     input_report_abs(input, ABS_MT_PRESSURE, pressure);
0215 }
0216 
0217 /* Required for Synaptics Palm Detection */
0218 static void asus_report_tool_width(struct asus_drvdata *drvdat)
0219 {
0220     struct input_mt *mt = drvdat->input->mt;
0221     struct input_mt_slot *oldest;
0222     int oldid, count, i;
0223 
0224     if (drvdat->tp->contact_size < 5)
0225         return;
0226 
0227     oldest = NULL;
0228     oldid = mt->trkid;
0229     count = 0;
0230 
0231     for (i = 0; i < mt->num_slots; ++i) {
0232         struct input_mt_slot *ps = &mt->slots[i];
0233         int id = input_mt_get_value(ps, ABS_MT_TRACKING_ID);
0234 
0235         if (id < 0)
0236             continue;
0237         if ((id - oldid) & TRKID_SGN) {
0238             oldest = ps;
0239             oldid = id;
0240         }
0241         count++;
0242     }
0243 
0244     if (oldest) {
0245         input_report_abs(drvdat->input, ABS_TOOL_WIDTH,
0246             input_mt_get_value(oldest, ABS_MT_TOUCH_MAJOR));
0247     }
0248 }
0249 
0250 static int asus_report_input(struct asus_drvdata *drvdat, u8 *data, int size)
0251 {
0252     int i, toolType = MT_TOOL_FINGER;
0253     u8 *contactData = data + 2;
0254 
0255     if (size != drvdat->tp->report_size)
0256         return 0;
0257 
0258     for (i = 0; i < drvdat->tp->max_contacts; i++) {
0259         bool down = !!(data[1] & BIT(i+3));
0260 
0261         if (drvdat->tp->contact_size >= 5)
0262             toolType = contactData[3] & CONTACT_TOOL_TYPE_MASK ?
0263                         MT_TOOL_PALM : MT_TOOL_FINGER;
0264 
0265         input_mt_slot(drvdat->input, i);
0266         input_mt_report_slot_state(drvdat->input, toolType, down);
0267 
0268         if (down) {
0269             asus_report_contact_down(drvdat, toolType, contactData);
0270             contactData += drvdat->tp->contact_size;
0271         }
0272     }
0273 
0274     input_report_key(drvdat->input, BTN_LEFT, data[1] & BTN_LEFT_MASK);
0275     asus_report_tool_width(drvdat);
0276 
0277     input_mt_sync_frame(drvdat->input);
0278     input_sync(drvdat->input);
0279 
0280     return 1;
0281 }
0282 
0283 static int asus_e1239t_event(struct asus_drvdata *drvdat, u8 *data, int size)
0284 {
0285     if (size != 3)
0286         return 0;
0287 
0288     /* Handle broken mute key which only sends press events */
0289     if (!drvdat->tp &&
0290         data[0] == 0x02 && data[1] == 0xe2 && data[2] == 0x00) {
0291         input_report_key(drvdat->input, KEY_MUTE, 1);
0292         input_sync(drvdat->input);
0293         input_report_key(drvdat->input, KEY_MUTE, 0);
0294         input_sync(drvdat->input);
0295         return 1;
0296     }
0297 
0298     /* Handle custom touchpad toggle key which only sends press events */
0299     if (drvdat->tp_kbd_input &&
0300         data[0] == 0x05 && data[1] == 0x02 && data[2] == 0x28) {
0301         input_report_key(drvdat->tp_kbd_input, KEY_F21, 1);
0302         input_sync(drvdat->tp_kbd_input);
0303         input_report_key(drvdat->tp_kbd_input, KEY_F21, 0);
0304         input_sync(drvdat->tp_kbd_input);
0305         return 1;
0306     }
0307 
0308     return 0;
0309 }
0310 
0311 static int asus_event(struct hid_device *hdev, struct hid_field *field,
0312               struct hid_usage *usage, __s32 value)
0313 {
0314     if ((usage->hid & HID_USAGE_PAGE) == 0xff310000 &&
0315         (usage->hid & HID_USAGE) != 0x00 &&
0316         (usage->hid & HID_USAGE) != 0xff && !usage->type) {
0317         hid_warn(hdev, "Unmapped Asus vendor usagepage code 0x%02x\n",
0318              usage->hid & HID_USAGE);
0319     }
0320 
0321     return 0;
0322 }
0323 
0324 static int asus_raw_event(struct hid_device *hdev,
0325         struct hid_report *report, u8 *data, int size)
0326 {
0327     struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
0328 
0329     if (drvdata->battery && data[0] == BATTERY_REPORT_ID)
0330         return asus_report_battery(drvdata, data, size);
0331 
0332     if (drvdata->tp && data[0] == INPUT_REPORT_ID)
0333         return asus_report_input(drvdata, data, size);
0334 
0335     if (drvdata->quirks & QUIRK_MEDION_E1239T)
0336         return asus_e1239t_event(drvdata, data, size);
0337 
0338     if (drvdata->quirks & QUIRK_USE_KBD_BACKLIGHT) {
0339         /*
0340          * Skip these report ID, the device emits a continuous stream associated
0341          * with the AURA mode it is in which looks like an 'echo'.
0342         */
0343         if (report->id == FEATURE_KBD_LED_REPORT_ID1 ||
0344                 report->id == FEATURE_KBD_LED_REPORT_ID2) {
0345             return -1;
0346         /* Additional report filtering */
0347         } else if (report->id == FEATURE_KBD_REPORT_ID) {
0348             /*
0349              * G14 and G15 send these codes on some keypresses with no
0350              * discernable reason for doing so. We'll filter them out to avoid
0351              * unmapped warning messages later.
0352             */
0353             if (data[1] == 0xea || data[1] == 0xec || data[1] == 0x02 ||
0354                     data[1] == 0x8a || data[1] == 0x9e) {
0355                 return -1;
0356             }
0357         }
0358         if (drvdata->quirks & QUIRK_ROG_NKEY_KEYBOARD) {
0359             /*
0360              * G713 and G733 send these codes on some keypresses, depending on
0361              * the key pressed it can trigger a shutdown event if not caught.
0362             */
0363             if(data[0] == 0x02 && data[1] == 0x30) {
0364                 return -1;
0365             }
0366         }
0367 
0368     }
0369 
0370     if (drvdata->quirks & QUIRK_ROG_CLAYMORE_II_KEYBOARD) {
0371         /*
0372          * CLAYMORE II keyboard sends this packet when it goes to sleep
0373          * this causes the whole system to go into suspend.
0374         */
0375 
0376         if(size == 2 && data[0] == 0x02 && data[1] == 0x00) {
0377             return -1;
0378         }
0379     }
0380 
0381     return 0;
0382 }
0383 
0384 static int asus_kbd_set_report(struct hid_device *hdev, u8 *buf, size_t buf_size)
0385 {
0386     unsigned char *dmabuf;
0387     int ret;
0388 
0389     dmabuf = kmemdup(buf, buf_size, GFP_KERNEL);
0390     if (!dmabuf)
0391         return -ENOMEM;
0392 
0393     /*
0394      * The report ID should be set from the incoming buffer due to LED and key
0395      * interfaces having different pages
0396     */
0397     ret = hid_hw_raw_request(hdev, buf[0], dmabuf,
0398                  buf_size, HID_FEATURE_REPORT,
0399                  HID_REQ_SET_REPORT);
0400     kfree(dmabuf);
0401 
0402     return ret;
0403 }
0404 
0405 static int asus_kbd_init(struct hid_device *hdev)
0406 {
0407     u8 buf[] = { FEATURE_KBD_REPORT_ID, 0x41, 0x53, 0x55, 0x53, 0x20, 0x54,
0408              0x65, 0x63, 0x68, 0x2e, 0x49, 0x6e, 0x63, 0x2e, 0x00 };
0409     int ret;
0410 
0411     ret = asus_kbd_set_report(hdev, buf, sizeof(buf));
0412     if (ret < 0)
0413         hid_err(hdev, "Asus failed to send init command: %d\n", ret);
0414 
0415     return ret;
0416 }
0417 
0418 static int asus_kbd_get_functions(struct hid_device *hdev,
0419                   unsigned char *kbd_func)
0420 {
0421     u8 buf[] = { FEATURE_KBD_REPORT_ID, 0x05, 0x20, 0x31, 0x00, 0x08 };
0422     u8 *readbuf;
0423     int ret;
0424 
0425     ret = asus_kbd_set_report(hdev, buf, sizeof(buf));
0426     if (ret < 0) {
0427         hid_err(hdev, "Asus failed to send configuration command: %d\n", ret);
0428         return ret;
0429     }
0430 
0431     readbuf = kzalloc(FEATURE_KBD_REPORT_SIZE, GFP_KERNEL);
0432     if (!readbuf)
0433         return -ENOMEM;
0434 
0435     ret = hid_hw_raw_request(hdev, FEATURE_KBD_REPORT_ID, readbuf,
0436                  FEATURE_KBD_REPORT_SIZE, HID_FEATURE_REPORT,
0437                  HID_REQ_GET_REPORT);
0438     if (ret < 0) {
0439         hid_err(hdev, "Asus failed to request functions: %d\n", ret);
0440         kfree(readbuf);
0441         return ret;
0442     }
0443 
0444     *kbd_func = readbuf[6];
0445 
0446     kfree(readbuf);
0447     return ret;
0448 }
0449 
0450 static int rog_nkey_led_init(struct hid_device *hdev)
0451 {
0452     u8 buf_init_start[] = { FEATURE_KBD_LED_REPORT_ID1, 0xB9 };
0453     u8 buf_init2[] = { FEATURE_KBD_LED_REPORT_ID1, 0x41, 0x53, 0x55, 0x53, 0x20,
0454                 0x54, 0x65, 0x63, 0x68, 0x2e, 0x49, 0x6e, 0x63, 0x2e, 0x00 };
0455     u8 buf_init3[] = { FEATURE_KBD_LED_REPORT_ID1,
0456                         0x05, 0x20, 0x31, 0x00, 0x08 };
0457     int ret;
0458 
0459     hid_info(hdev, "Asus initialise N-KEY Device");
0460     /* The first message is an init start */
0461     ret = asus_kbd_set_report(hdev, buf_init_start, sizeof(buf_init_start));
0462     if (ret < 0) {
0463         hid_warn(hdev, "Asus failed to send init start command: %d\n", ret);
0464         return ret;
0465     }
0466     /* Followed by a string */
0467     ret = asus_kbd_set_report(hdev, buf_init2, sizeof(buf_init2));
0468     if (ret < 0) {
0469         hid_warn(hdev, "Asus failed to send init command 1.0: %d\n", ret);
0470         return ret;
0471     }
0472     /* Followed by a string */
0473     ret = asus_kbd_set_report(hdev, buf_init3, sizeof(buf_init3));
0474     if (ret < 0) {
0475         hid_warn(hdev, "Asus failed to send init command 1.1: %d\n", ret);
0476         return ret;
0477     }
0478 
0479     /* begin second report ID with same data */
0480     buf_init2[0] = FEATURE_KBD_LED_REPORT_ID2;
0481     buf_init3[0] = FEATURE_KBD_LED_REPORT_ID2;
0482 
0483     ret = asus_kbd_set_report(hdev, buf_init2, sizeof(buf_init2));
0484     if (ret < 0) {
0485         hid_warn(hdev, "Asus failed to send init command 2.0: %d\n", ret);
0486         return ret;
0487     }
0488     ret = asus_kbd_set_report(hdev, buf_init3, sizeof(buf_init3));
0489     if (ret < 0)
0490         hid_warn(hdev, "Asus failed to send init command 2.1: %d\n", ret);
0491 
0492     return ret;
0493 }
0494 
0495 static void asus_kbd_backlight_set(struct led_classdev *led_cdev,
0496                    enum led_brightness brightness)
0497 {
0498     struct asus_kbd_leds *led = container_of(led_cdev, struct asus_kbd_leds,
0499                          cdev);
0500     led->brightness = brightness;
0501     schedule_work(&led->work);
0502 }
0503 
0504 static enum led_brightness asus_kbd_backlight_get(struct led_classdev *led_cdev)
0505 {
0506     struct asus_kbd_leds *led = container_of(led_cdev, struct asus_kbd_leds,
0507                          cdev);
0508 
0509     return led->brightness;
0510 }
0511 
0512 static void asus_kbd_backlight_work(struct work_struct *work)
0513 {
0514     struct asus_kbd_leds *led = container_of(work, struct asus_kbd_leds, work);
0515     u8 buf[] = { FEATURE_KBD_REPORT_ID, 0xba, 0xc5, 0xc4, 0x00 };
0516     int ret;
0517 
0518     if (led->removed)
0519         return;
0520 
0521     buf[4] = led->brightness;
0522 
0523     ret = asus_kbd_set_report(led->hdev, buf, sizeof(buf));
0524     if (ret < 0)
0525         hid_err(led->hdev, "Asus failed to set keyboard backlight: %d\n", ret);
0526 }
0527 
0528 /* WMI-based keyboard backlight LED control (via asus-wmi driver) takes
0529  * precedence. We only activate HID-based backlight control when the
0530  * WMI control is not available.
0531  */
0532 static bool asus_kbd_wmi_led_control_present(struct hid_device *hdev)
0533 {
0534     u32 value;
0535     int ret;
0536 
0537     if (!IS_ENABLED(CONFIG_ASUS_WMI))
0538         return false;
0539 
0540     ret = asus_wmi_evaluate_method(ASUS_WMI_METHODID_DSTS,
0541                        ASUS_WMI_DEVID_KBD_BACKLIGHT, 0, &value);
0542     hid_dbg(hdev, "WMI backlight check: rc %d value %x", ret, value);
0543     if (ret)
0544         return false;
0545 
0546     return !!(value & ASUS_WMI_DSTS_PRESENCE_BIT);
0547 }
0548 
0549 static int asus_kbd_register_leds(struct hid_device *hdev)
0550 {
0551     struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
0552     unsigned char kbd_func;
0553     int ret;
0554 
0555     if (drvdata->quirks & QUIRK_ROG_NKEY_KEYBOARD) {
0556         ret = rog_nkey_led_init(hdev);
0557         if (ret < 0)
0558             return ret;
0559     } else {
0560         /* Initialize keyboard */
0561         ret = asus_kbd_init(hdev);
0562         if (ret < 0)
0563             return ret;
0564 
0565         /* Get keyboard functions */
0566         ret = asus_kbd_get_functions(hdev, &kbd_func);
0567         if (ret < 0)
0568             return ret;
0569 
0570         /* Check for backlight support */
0571         if (!(kbd_func & SUPPORT_KBD_BACKLIGHT))
0572             return -ENODEV;
0573     }
0574 
0575     drvdata->kbd_backlight = devm_kzalloc(&hdev->dev,
0576                           sizeof(struct asus_kbd_leds),
0577                           GFP_KERNEL);
0578     if (!drvdata->kbd_backlight)
0579         return -ENOMEM;
0580 
0581     drvdata->kbd_backlight->removed = false;
0582     drvdata->kbd_backlight->brightness = 0;
0583     drvdata->kbd_backlight->hdev = hdev;
0584     drvdata->kbd_backlight->cdev.name = "asus::kbd_backlight";
0585     drvdata->kbd_backlight->cdev.max_brightness = 3;
0586     drvdata->kbd_backlight->cdev.brightness_set = asus_kbd_backlight_set;
0587     drvdata->kbd_backlight->cdev.brightness_get = asus_kbd_backlight_get;
0588     INIT_WORK(&drvdata->kbd_backlight->work, asus_kbd_backlight_work);
0589 
0590     ret = devm_led_classdev_register(&hdev->dev, &drvdata->kbd_backlight->cdev);
0591     if (ret < 0) {
0592         /* No need to have this still around */
0593         devm_kfree(&hdev->dev, drvdata->kbd_backlight);
0594     }
0595 
0596     return ret;
0597 }
0598 
0599 /*
0600  * [0]       REPORT_ID (same value defined in report descriptor)
0601  * [1]       rest battery level. range [0..255]
0602  * [2]..[7]  Bluetooth hardware address (MAC address)
0603  * [8]       charging status
0604  *            = 0 : AC offline / discharging
0605  *            = 1 : AC online  / charging
0606  *            = 2 : AC online  / fully charged
0607  */
0608 static int asus_parse_battery(struct asus_drvdata *drvdata, u8 *data, int size)
0609 {
0610     u8 sts;
0611     u8 lvl;
0612     int val;
0613 
0614     lvl = data[1];
0615     sts = data[8];
0616 
0617     drvdata->battery_capacity = ((int)lvl * 100) / (int)BATTERY_LEVEL_MAX;
0618 
0619     switch (sts) {
0620     case BATTERY_STAT_CHARGING:
0621         val = POWER_SUPPLY_STATUS_CHARGING;
0622         break;
0623     case BATTERY_STAT_FULL:
0624         val = POWER_SUPPLY_STATUS_FULL;
0625         break;
0626     case BATTERY_STAT_DISCONNECT:
0627     default:
0628         val = POWER_SUPPLY_STATUS_DISCHARGING;
0629         break;
0630     }
0631     drvdata->battery_stat = val;
0632 
0633     return 0;
0634 }
0635 
0636 static int asus_report_battery(struct asus_drvdata *drvdata, u8 *data, int size)
0637 {
0638     /* notify only the autonomous event by device */
0639     if ((drvdata->battery_in_query == false) &&
0640              (size == BATTERY_REPORT_SIZE))
0641         power_supply_changed(drvdata->battery);
0642 
0643     return 0;
0644 }
0645 
0646 static int asus_battery_query(struct asus_drvdata *drvdata)
0647 {
0648     u8 *buf;
0649     int ret = 0;
0650 
0651     buf = kmalloc(BATTERY_REPORT_SIZE, GFP_KERNEL);
0652     if (!buf)
0653         return -ENOMEM;
0654 
0655     drvdata->battery_in_query = true;
0656     ret = hid_hw_raw_request(drvdata->hdev, BATTERY_REPORT_ID,
0657                 buf, BATTERY_REPORT_SIZE,
0658                 HID_INPUT_REPORT, HID_REQ_GET_REPORT);
0659     drvdata->battery_in_query = false;
0660     if (ret == BATTERY_REPORT_SIZE)
0661         ret = asus_parse_battery(drvdata, buf, BATTERY_REPORT_SIZE);
0662     else
0663         ret = -ENODATA;
0664 
0665     kfree(buf);
0666 
0667     return ret;
0668 }
0669 
0670 static enum power_supply_property asus_battery_props[] = {
0671     POWER_SUPPLY_PROP_STATUS,
0672     POWER_SUPPLY_PROP_PRESENT,
0673     POWER_SUPPLY_PROP_CAPACITY,
0674     POWER_SUPPLY_PROP_SCOPE,
0675     POWER_SUPPLY_PROP_MODEL_NAME,
0676 };
0677 
0678 #define QUERY_MIN_INTERVAL  (60 * HZ)   /* 60[sec] */
0679 
0680 static int asus_battery_get_property(struct power_supply *psy,
0681                 enum power_supply_property psp,
0682                 union power_supply_propval *val)
0683 {
0684     struct asus_drvdata *drvdata = power_supply_get_drvdata(psy);
0685     int ret = 0;
0686 
0687     switch (psp) {
0688     case POWER_SUPPLY_PROP_STATUS:
0689     case POWER_SUPPLY_PROP_CAPACITY:
0690         if (time_before(drvdata->battery_next_query, jiffies)) {
0691             drvdata->battery_next_query =
0692                      jiffies + QUERY_MIN_INTERVAL;
0693             ret = asus_battery_query(drvdata);
0694             if (ret)
0695                 return ret;
0696         }
0697         if (psp == POWER_SUPPLY_PROP_STATUS)
0698             val->intval = drvdata->battery_stat;
0699         else
0700             val->intval = drvdata->battery_capacity;
0701         break;
0702     case POWER_SUPPLY_PROP_PRESENT:
0703         val->intval = 1;
0704         break;
0705     case POWER_SUPPLY_PROP_SCOPE:
0706         val->intval = POWER_SUPPLY_SCOPE_DEVICE;
0707         break;
0708     case POWER_SUPPLY_PROP_MODEL_NAME:
0709         val->strval = drvdata->hdev->name;
0710         break;
0711     default:
0712         ret = -EINVAL;
0713         break;
0714     }
0715 
0716     return ret;
0717 }
0718 
0719 static int asus_battery_probe(struct hid_device *hdev)
0720 {
0721     struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
0722     struct power_supply_config pscfg = { .drv_data = drvdata };
0723     int ret = 0;
0724 
0725     drvdata->battery_capacity = 0;
0726     drvdata->battery_stat = POWER_SUPPLY_STATUS_UNKNOWN;
0727     drvdata->battery_in_query = false;
0728 
0729     drvdata->battery_desc.properties = asus_battery_props;
0730     drvdata->battery_desc.num_properties = ARRAY_SIZE(asus_battery_props);
0731     drvdata->battery_desc.get_property = asus_battery_get_property;
0732     drvdata->battery_desc.type = POWER_SUPPLY_TYPE_BATTERY;
0733     drvdata->battery_desc.use_for_apm = 0;
0734     drvdata->battery_desc.name = devm_kasprintf(&hdev->dev, GFP_KERNEL,
0735                     "asus-keyboard-%s-battery",
0736                     strlen(hdev->uniq) ?
0737                     hdev->uniq : dev_name(&hdev->dev));
0738     if (!drvdata->battery_desc.name)
0739         return -ENOMEM;
0740 
0741     drvdata->battery_next_query = jiffies;
0742 
0743     drvdata->battery = devm_power_supply_register(&hdev->dev,
0744                 &(drvdata->battery_desc), &pscfg);
0745     if (IS_ERR(drvdata->battery)) {
0746         ret = PTR_ERR(drvdata->battery);
0747         drvdata->battery = NULL;
0748         hid_err(hdev, "Unable to register battery device\n");
0749         return ret;
0750     }
0751 
0752     power_supply_powers(drvdata->battery, &hdev->dev);
0753 
0754     return ret;
0755 }
0756 
0757 static int asus_input_configured(struct hid_device *hdev, struct hid_input *hi)
0758 {
0759     struct input_dev *input = hi->input;
0760     struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
0761 
0762     /* T100CHI uses MULTI_INPUT, bind the touchpad to the mouse hid_input */
0763     if (drvdata->quirks & QUIRK_T100CHI &&
0764         hi->report->id != T100CHI_MOUSE_REPORT_ID)
0765         return 0;
0766 
0767     /* Handle MULTI_INPUT on E1239T mouse/touchpad USB interface */
0768     if (drvdata->tp && (drvdata->quirks & QUIRK_MEDION_E1239T)) {
0769         switch (hi->report->id) {
0770         case E1239T_TP_TOGGLE_REPORT_ID:
0771             input_set_capability(input, EV_KEY, KEY_F21);
0772             input->name = "Asus Touchpad Keys";
0773             drvdata->tp_kbd_input = input;
0774             return 0;
0775         case INPUT_REPORT_ID:
0776             break; /* Touchpad report, handled below */
0777         default:
0778             return 0; /* Ignore other reports */
0779         }
0780     }
0781 
0782     if (drvdata->tp) {
0783         int ret;
0784 
0785         input_set_abs_params(input, ABS_MT_POSITION_X, 0,
0786                      drvdata->tp->max_x, 0, 0);
0787         input_set_abs_params(input, ABS_MT_POSITION_Y, 0,
0788                      drvdata->tp->max_y, 0, 0);
0789         input_abs_set_res(input, ABS_MT_POSITION_X, drvdata->tp->res_x);
0790         input_abs_set_res(input, ABS_MT_POSITION_Y, drvdata->tp->res_y);
0791 
0792         if (drvdata->tp->contact_size >= 5) {
0793             input_set_abs_params(input, ABS_TOOL_WIDTH, 0,
0794                          MAX_TOUCH_MAJOR, 0, 0);
0795             input_set_abs_params(input, ABS_MT_TOUCH_MAJOR, 0,
0796                          MAX_TOUCH_MAJOR, 0, 0);
0797             input_set_abs_params(input, ABS_MT_PRESSURE, 0,
0798                           MAX_PRESSURE, 0, 0);
0799         }
0800 
0801         __set_bit(BTN_LEFT, input->keybit);
0802         __set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
0803 
0804         ret = input_mt_init_slots(input, drvdata->tp->max_contacts,
0805                       INPUT_MT_POINTER);
0806 
0807         if (ret) {
0808             hid_err(hdev, "Asus input mt init slots failed: %d\n", ret);
0809             return ret;
0810         }
0811     }
0812 
0813     drvdata->input = input;
0814 
0815     if (drvdata->enable_backlight &&
0816         !asus_kbd_wmi_led_control_present(hdev) &&
0817         asus_kbd_register_leds(hdev))
0818         hid_warn(hdev, "Failed to initialize backlight.\n");
0819 
0820     return 0;
0821 }
0822 
0823 #define asus_map_key_clear(c)   hid_map_usage_clear(hi, usage, bit, \
0824                             max, EV_KEY, (c))
0825 static int asus_input_mapping(struct hid_device *hdev,
0826         struct hid_input *hi, struct hid_field *field,
0827         struct hid_usage *usage, unsigned long **bit,
0828         int *max)
0829 {
0830     struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
0831 
0832     if (drvdata->quirks & QUIRK_SKIP_INPUT_MAPPING) {
0833         /* Don't map anything from the HID report.
0834          * We do it all manually in asus_input_configured
0835          */
0836         return -1;
0837     }
0838 
0839     /*
0840      * Ignore a bunch of bogus collections in the T100CHI descriptor.
0841      * This avoids a bunch of non-functional hid_input devices getting
0842      * created because of the T100CHI using HID_QUIRK_MULTI_INPUT.
0843      */
0844     if ((drvdata->quirks & (QUIRK_T100CHI | QUIRK_T90CHI)) &&
0845         (field->application == (HID_UP_GENDESK | 0x0080) ||
0846          field->application == HID_GD_MOUSE ||
0847          usage->hid == (HID_UP_GENDEVCTRLS | 0x0024) ||
0848          usage->hid == (HID_UP_GENDEVCTRLS | 0x0025) ||
0849          usage->hid == (HID_UP_GENDEVCTRLS | 0x0026)))
0850         return -1;
0851 
0852     /* ASUS-specific keyboard hotkeys and led backlight */
0853     if ((usage->hid & HID_USAGE_PAGE) == HID_UP_ASUSVENDOR) {
0854         switch (usage->hid & HID_USAGE) {
0855         case 0x10: asus_map_key_clear(KEY_BRIGHTNESSDOWN);  break;
0856         case 0x20: asus_map_key_clear(KEY_BRIGHTNESSUP);        break;
0857         case 0x35: asus_map_key_clear(KEY_DISPLAY_OFF);     break;
0858         case 0x6c: asus_map_key_clear(KEY_SLEEP);       break;
0859         case 0x7c: asus_map_key_clear(KEY_MICMUTE);     break;
0860         case 0x82: asus_map_key_clear(KEY_CAMERA);      break;
0861         case 0x88: asus_map_key_clear(KEY_RFKILL);          break;
0862         case 0xb5: asus_map_key_clear(KEY_CALC);            break;
0863         case 0xc4: asus_map_key_clear(KEY_KBDILLUMUP);      break;
0864         case 0xc5: asus_map_key_clear(KEY_KBDILLUMDOWN);        break;
0865 
0866         /* ASUS touchpad toggle */
0867         case 0x6b: asus_map_key_clear(KEY_F21);         break;
0868 
0869         /* ROG key */
0870         case 0x38: asus_map_key_clear(KEY_PROG1);       break;
0871 
0872         /* Fn+C ASUS Splendid */
0873         case 0xba: asus_map_key_clear(KEY_PROG2);       break;
0874 
0875         /* Fn+Space Power4Gear Hybrid */
0876         case 0x5c: asus_map_key_clear(KEY_PROG3);       break;
0877 
0878         /* Fn+F5 "fan" symbol on FX503VD */
0879         case 0x99: asus_map_key_clear(KEY_PROG4);       break;
0880 
0881         /* Fn+F5 "fan" symbol on N-Key keyboard */
0882         case 0xae: asus_map_key_clear(KEY_PROG4);       break;
0883 
0884         /* Fn+Ret "Calc" symbol on N-Key keyboard */
0885         case 0x92: asus_map_key_clear(KEY_CALC);        break;
0886 
0887         /* Fn+Left Aura mode previous on N-Key keyboard */
0888         case 0xb2: asus_map_key_clear(KEY_PROG2);       break;
0889 
0890         /* Fn+Right Aura mode next on N-Key keyboard */
0891         case 0xb3: asus_map_key_clear(KEY_PROG3);       break;
0892 
0893         default:
0894             /* ASUS lazily declares 256 usages, ignore the rest,
0895              * as some make the keyboard appear as a pointer device. */
0896             return -1;
0897         }
0898 
0899         /*
0900          * Check and enable backlight only on devices with UsagePage ==
0901          * 0xff31 to avoid initializing the keyboard firmware multiple
0902          * times on devices with multiple HID descriptors but same
0903          * PID/VID.
0904          */
0905         if (drvdata->quirks & QUIRK_USE_KBD_BACKLIGHT)
0906             drvdata->enable_backlight = true;
0907 
0908         set_bit(EV_REP, hi->input->evbit);
0909         return 1;
0910     }
0911 
0912     if ((usage->hid & HID_USAGE_PAGE) == HID_UP_MSVENDOR) {
0913         switch (usage->hid & HID_USAGE) {
0914         case 0xff01: asus_map_key_clear(BTN_1); break;
0915         case 0xff02: asus_map_key_clear(BTN_2); break;
0916         case 0xff03: asus_map_key_clear(BTN_3); break;
0917         case 0xff04: asus_map_key_clear(BTN_4); break;
0918         case 0xff05: asus_map_key_clear(BTN_5); break;
0919         case 0xff06: asus_map_key_clear(BTN_6); break;
0920         case 0xff07: asus_map_key_clear(BTN_7); break;
0921         case 0xff08: asus_map_key_clear(BTN_8); break;
0922         case 0xff09: asus_map_key_clear(BTN_9); break;
0923         case 0xff0a: asus_map_key_clear(BTN_A); break;
0924         case 0xff0b: asus_map_key_clear(BTN_B); break;
0925         case 0x00f1: asus_map_key_clear(KEY_WLAN);  break;
0926         case 0x00f2: asus_map_key_clear(KEY_BRIGHTNESSDOWN);    break;
0927         case 0x00f3: asus_map_key_clear(KEY_BRIGHTNESSUP);  break;
0928         case 0x00f4: asus_map_key_clear(KEY_DISPLAY_OFF);   break;
0929         case 0x00f7: asus_map_key_clear(KEY_CAMERA);    break;
0930         case 0x00f8: asus_map_key_clear(KEY_PROG1); break;
0931         default:
0932             return 0;
0933         }
0934 
0935         set_bit(EV_REP, hi->input->evbit);
0936         return 1;
0937     }
0938 
0939     if (drvdata->quirks & QUIRK_NO_CONSUMER_USAGES &&
0940         (usage->hid & HID_USAGE_PAGE) == HID_UP_CONSUMER) {
0941         switch (usage->hid & HID_USAGE) {
0942         case 0xe2: /* Mute */
0943         case 0xe9: /* Volume up */
0944         case 0xea: /* Volume down */
0945             return 0;
0946         default:
0947             /* Ignore dummy Consumer usages which make the
0948              * keyboard incorrectly appear as a pointer device.
0949              */
0950             return -1;
0951         }
0952     }
0953 
0954     /*
0955      * The mute button is broken and only sends press events, we
0956      * deal with this in our raw_event handler, so do not map it.
0957      */
0958     if ((drvdata->quirks & QUIRK_MEDION_E1239T) &&
0959         usage->hid == (HID_UP_CONSUMER | 0xe2)) {
0960         input_set_capability(hi->input, EV_KEY, KEY_MUTE);
0961         return -1;
0962     }
0963 
0964     return 0;
0965 }
0966 
0967 static int asus_start_multitouch(struct hid_device *hdev)
0968 {
0969     int ret;
0970     static const unsigned char buf[] = {
0971         FEATURE_REPORT_ID, 0x00, 0x03, 0x01, 0x00
0972     };
0973     unsigned char *dmabuf = kmemdup(buf, sizeof(buf), GFP_KERNEL);
0974 
0975     if (!dmabuf) {
0976         ret = -ENOMEM;
0977         hid_err(hdev, "Asus failed to alloc dma buf: %d\n", ret);
0978         return ret;
0979     }
0980 
0981     ret = hid_hw_raw_request(hdev, dmabuf[0], dmabuf, sizeof(buf),
0982                     HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
0983 
0984     kfree(dmabuf);
0985 
0986     if (ret != sizeof(buf)) {
0987         hid_err(hdev, "Asus failed to start multitouch: %d\n", ret);
0988         return ret;
0989     }
0990 
0991     return 0;
0992 }
0993 
0994 static int __maybe_unused asus_reset_resume(struct hid_device *hdev)
0995 {
0996     struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
0997 
0998     if (drvdata->tp)
0999         return asus_start_multitouch(hdev);
1000 
1001     return 0;
1002 }
1003 
1004 static int asus_probe(struct hid_device *hdev, const struct hid_device_id *id)
1005 {
1006     int ret;
1007     struct asus_drvdata *drvdata;
1008 
1009     drvdata = devm_kzalloc(&hdev->dev, sizeof(*drvdata), GFP_KERNEL);
1010     if (drvdata == NULL) {
1011         hid_err(hdev, "Can't alloc Asus descriptor\n");
1012         return -ENOMEM;
1013     }
1014 
1015     hid_set_drvdata(hdev, drvdata);
1016 
1017     drvdata->quirks = id->driver_data;
1018 
1019     /*
1020      * T90CHI's keyboard dock returns same ID values as T100CHI's dock.
1021      * Thus, identify T90CHI dock with product name string.
1022      */
1023     if (strstr(hdev->name, "T90CHI")) {
1024         drvdata->quirks &= ~QUIRK_T100CHI;
1025         drvdata->quirks |= QUIRK_T90CHI;
1026     }
1027 
1028     if (drvdata->quirks & QUIRK_IS_MULTITOUCH)
1029         drvdata->tp = &asus_i2c_tp;
1030 
1031     if ((drvdata->quirks & QUIRK_T100_KEYBOARD) && hid_is_usb(hdev)) {
1032         struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
1033 
1034         if (intf->altsetting->desc.bInterfaceNumber == T100_TPAD_INTF) {
1035             drvdata->quirks = QUIRK_SKIP_INPUT_MAPPING;
1036             /*
1037              * The T100HA uses the same USB-ids as the T100TAF and
1038              * the T200TA uses the same USB-ids as the T100TA, while
1039              * both have different max x/y values as the T100TA[F].
1040              */
1041             if (dmi_match(DMI_PRODUCT_NAME, "T100HAN"))
1042                 drvdata->tp = &asus_t100ha_tp;
1043             else if (dmi_match(DMI_PRODUCT_NAME, "T200TA"))
1044                 drvdata->tp = &asus_t200ta_tp;
1045             else
1046                 drvdata->tp = &asus_t100ta_tp;
1047         }
1048     }
1049 
1050     if (drvdata->quirks & QUIRK_T100CHI) {
1051         /*
1052          * All functionality is on a single HID interface and for
1053          * userspace the touchpad must be a separate input_dev.
1054          */
1055         hdev->quirks |= HID_QUIRK_MULTI_INPUT;
1056         drvdata->tp = &asus_t100chi_tp;
1057     }
1058 
1059     if ((drvdata->quirks & QUIRK_MEDION_E1239T) && hid_is_usb(hdev)) {
1060         struct usb_host_interface *alt =
1061             to_usb_interface(hdev->dev.parent)->altsetting;
1062 
1063         if (alt->desc.bInterfaceNumber == MEDION_E1239T_TPAD_INTF) {
1064             /* For separate input-devs for tp and tp toggle key */
1065             hdev->quirks |= HID_QUIRK_MULTI_INPUT;
1066             drvdata->quirks |= QUIRK_SKIP_INPUT_MAPPING;
1067             drvdata->tp = &medion_e1239t_tp;
1068         }
1069     }
1070 
1071     if (drvdata->quirks & QUIRK_NO_INIT_REPORTS)
1072         hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
1073 
1074     drvdata->hdev = hdev;
1075 
1076     if (drvdata->quirks & (QUIRK_T100CHI | QUIRK_T90CHI)) {
1077         ret = asus_battery_probe(hdev);
1078         if (ret) {
1079             hid_err(hdev,
1080                 "Asus hid battery_probe failed: %d\n", ret);
1081             return ret;
1082         }
1083     }
1084 
1085     ret = hid_parse(hdev);
1086     if (ret) {
1087         hid_err(hdev, "Asus hid parse failed: %d\n", ret);
1088         return ret;
1089     }
1090 
1091     ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
1092     if (ret) {
1093         hid_err(hdev, "Asus hw start failed: %d\n", ret);
1094         return ret;
1095     }
1096 
1097     if (!drvdata->input) {
1098         hid_err(hdev, "Asus input not registered\n");
1099         ret = -ENOMEM;
1100         goto err_stop_hw;
1101     }
1102 
1103     if (drvdata->tp) {
1104         drvdata->input->name = "Asus TouchPad";
1105     } else {
1106         drvdata->input->name = "Asus Keyboard";
1107     }
1108 
1109     if (drvdata->tp) {
1110         ret = asus_start_multitouch(hdev);
1111         if (ret)
1112             goto err_stop_hw;
1113     }
1114 
1115     return 0;
1116 err_stop_hw:
1117     hid_hw_stop(hdev);
1118     return ret;
1119 }
1120 
1121 static void asus_remove(struct hid_device *hdev)
1122 {
1123     struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
1124 
1125     if (drvdata->kbd_backlight) {
1126         drvdata->kbd_backlight->removed = true;
1127         cancel_work_sync(&drvdata->kbd_backlight->work);
1128     }
1129 
1130     hid_hw_stop(hdev);
1131 }
1132 
1133 static const __u8 asus_g752_fixed_rdesc[] = {
1134         0x19, 0x00,         /*   Usage Minimum (0x00)       */
1135         0x2A, 0xFF, 0x00,       /*   Usage Maximum (0xFF)       */
1136 };
1137 
1138 static __u8 *asus_report_fixup(struct hid_device *hdev, __u8 *rdesc,
1139         unsigned int *rsize)
1140 {
1141     struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
1142 
1143     if (drvdata->quirks & QUIRK_FIX_NOTEBOOK_REPORT &&
1144             *rsize >= 56 && rdesc[54] == 0x25 && rdesc[55] == 0x65) {
1145         hid_info(hdev, "Fixing up Asus notebook report descriptor\n");
1146         rdesc[55] = 0xdd;
1147     }
1148     /* For the T100TA/T200TA keyboard dock */
1149     if (drvdata->quirks & QUIRK_T100_KEYBOARD &&
1150          (*rsize == 76 || *rsize == 101) &&
1151          rdesc[73] == 0x81 && rdesc[74] == 0x01) {
1152         hid_info(hdev, "Fixing up Asus T100 keyb report descriptor\n");
1153         rdesc[74] &= ~HID_MAIN_ITEM_CONSTANT;
1154     }
1155     /* For the T100CHI/T90CHI keyboard dock */
1156     if (drvdata->quirks & (QUIRK_T100CHI | QUIRK_T90CHI)) {
1157         int rsize_orig;
1158         int offs;
1159 
1160         if (drvdata->quirks & QUIRK_T100CHI) {
1161             rsize_orig = 403;
1162             offs = 388;
1163         } else {
1164             rsize_orig = 306;
1165             offs = 291;
1166         }
1167 
1168         /*
1169          * Change Usage (76h) to Usage Minimum (00h), Usage Maximum
1170          * (FFh) and clear the flags in the Input() byte.
1171          * Note the descriptor has a bogus 0 byte at the end so we
1172          * only need 1 extra byte.
1173          */
1174         if (*rsize == rsize_orig &&
1175             rdesc[offs] == 0x09 && rdesc[offs + 1] == 0x76) {
1176             *rsize = rsize_orig + 1;
1177             rdesc = kmemdup(rdesc, *rsize, GFP_KERNEL);
1178             if (!rdesc)
1179                 return NULL;
1180 
1181             hid_info(hdev, "Fixing up %s keyb report descriptor\n",
1182                 drvdata->quirks & QUIRK_T100CHI ?
1183                 "T100CHI" : "T90CHI");
1184             memmove(rdesc + offs + 4, rdesc + offs + 2, 12);
1185             rdesc[offs] = 0x19;
1186             rdesc[offs + 1] = 0x00;
1187             rdesc[offs + 2] = 0x29;
1188             rdesc[offs + 3] = 0xff;
1189             rdesc[offs + 14] = 0x00;
1190         }
1191     }
1192 
1193     if (drvdata->quirks & QUIRK_G752_KEYBOARD &&
1194          *rsize == 75 && rdesc[61] == 0x15 && rdesc[62] == 0x00) {
1195         /* report is missing usage mninum and maximum */
1196         __u8 *new_rdesc;
1197         size_t new_size = *rsize + sizeof(asus_g752_fixed_rdesc);
1198 
1199         new_rdesc = devm_kzalloc(&hdev->dev, new_size, GFP_KERNEL);
1200         if (new_rdesc == NULL)
1201             return rdesc;
1202 
1203         hid_info(hdev, "Fixing up Asus G752 keyb report descriptor\n");
1204         /* copy the valid part */
1205         memcpy(new_rdesc, rdesc, 61);
1206         /* insert missing part */
1207         memcpy(new_rdesc + 61, asus_g752_fixed_rdesc, sizeof(asus_g752_fixed_rdesc));
1208         /* copy remaining data */
1209         memcpy(new_rdesc + 61 + sizeof(asus_g752_fixed_rdesc), rdesc + 61, *rsize - 61);
1210 
1211         *rsize = new_size;
1212         rdesc = new_rdesc;
1213     }
1214 
1215     if (drvdata->quirks & QUIRK_ROG_NKEY_KEYBOARD &&
1216             *rsize == 331 && rdesc[190] == 0x85 && rdesc[191] == 0x5a &&
1217             rdesc[204] == 0x95 && rdesc[205] == 0x05) {
1218         hid_info(hdev, "Fixing up Asus N-KEY keyb report descriptor\n");
1219         rdesc[205] = 0x01;
1220     }
1221 
1222     return rdesc;
1223 }
1224 
1225 static const struct hid_device_id asus_devices[] = {
1226     { HID_I2C_DEVICE(USB_VENDOR_ID_ASUSTEK,
1227         USB_DEVICE_ID_ASUSTEK_I2C_KEYBOARD), I2C_KEYBOARD_QUIRKS},
1228     { HID_I2C_DEVICE(USB_VENDOR_ID_ASUSTEK,
1229         USB_DEVICE_ID_ASUSTEK_I2C_TOUCHPAD), I2C_TOUCHPAD_QUIRKS },
1230     { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
1231         USB_DEVICE_ID_ASUSTEK_ROG_KEYBOARD1), QUIRK_USE_KBD_BACKLIGHT },
1232     { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
1233         USB_DEVICE_ID_ASUSTEK_ROG_KEYBOARD2), QUIRK_USE_KBD_BACKLIGHT },
1234     { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
1235         USB_DEVICE_ID_ASUSTEK_ROG_KEYBOARD3), QUIRK_G752_KEYBOARD },
1236     { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
1237         USB_DEVICE_ID_ASUSTEK_FX503VD_KEYBOARD),
1238       QUIRK_USE_KBD_BACKLIGHT },
1239     { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
1240         USB_DEVICE_ID_ASUSTEK_ROG_NKEY_KEYBOARD),
1241       QUIRK_USE_KBD_BACKLIGHT | QUIRK_ROG_NKEY_KEYBOARD },
1242     { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
1243         USB_DEVICE_ID_ASUSTEK_ROG_NKEY_KEYBOARD2),
1244       QUIRK_USE_KBD_BACKLIGHT | QUIRK_ROG_NKEY_KEYBOARD },
1245     { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
1246         USB_DEVICE_ID_ASUSTEK_ROG_CLAYMORE_II_KEYBOARD),
1247       QUIRK_ROG_CLAYMORE_II_KEYBOARD },
1248     { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
1249         USB_DEVICE_ID_ASUSTEK_T100TA_KEYBOARD),
1250       QUIRK_T100_KEYBOARD | QUIRK_NO_CONSUMER_USAGES },
1251     { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
1252         USB_DEVICE_ID_ASUSTEK_T100TAF_KEYBOARD),
1253       QUIRK_T100_KEYBOARD | QUIRK_NO_CONSUMER_USAGES },
1254     { HID_USB_DEVICE(USB_VENDOR_ID_CHICONY, USB_DEVICE_ID_ASUS_AK1D) },
1255     { HID_USB_DEVICE(USB_VENDOR_ID_TURBOX, USB_DEVICE_ID_ASUS_MD_5110) },
1256     { HID_USB_DEVICE(USB_VENDOR_ID_JESS, USB_DEVICE_ID_ASUS_MD_5112) },
1257     { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_ASUSTEK,
1258         USB_DEVICE_ID_ASUSTEK_T100CHI_KEYBOARD), QUIRK_T100CHI },
1259     { HID_USB_DEVICE(USB_VENDOR_ID_ITE, USB_DEVICE_ID_ITE_MEDION_E1239T),
1260         QUIRK_MEDION_E1239T },
1261     /*
1262      * Note bind to the HID_GROUP_GENERIC group, so that we only bind to the keyboard
1263      * part, while letting hid-multitouch.c handle the touchpad.
1264      */
1265     { HID_DEVICE(BUS_USB, HID_GROUP_GENERIC,
1266         USB_VENDOR_ID_ASUSTEK, USB_DEVICE_ID_ASUSTEK_T101HA_KEYBOARD) },
1267     { }
1268 };
1269 MODULE_DEVICE_TABLE(hid, asus_devices);
1270 
1271 static struct hid_driver asus_driver = {
1272     .name           = "asus",
1273     .id_table       = asus_devices,
1274     .report_fixup       = asus_report_fixup,
1275     .probe                  = asus_probe,
1276     .remove         = asus_remove,
1277     .input_mapping          = asus_input_mapping,
1278     .input_configured       = asus_input_configured,
1279 #ifdef CONFIG_PM
1280     .reset_resume           = asus_reset_resume,
1281 #endif
1282     .event          = asus_event,
1283     .raw_event      = asus_raw_event
1284 };
1285 module_hid_driver(asus_driver);
1286 
1287 MODULE_LICENSE("GPL");