0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0011
0012 #include <linux/kernel.h>
0013 #include <linux/input.h>
0014 #include <linux/input/sparse-keymap.h>
0015 #include <linux/acpi.h>
0016 #include <linux/backlight.h>
0017 #include <linux/slab.h>
0018 #include <linux/module.h>
0019 #include <acpi/video.h>
0020
0021 MODULE_AUTHOR("Thomas Renninger <trenn@suse.de>");
0022 MODULE_DESCRIPTION("MSI laptop WMI hotkeys driver");
0023 MODULE_LICENSE("GPL");
0024
0025 #define DRV_NAME "msi-wmi"
0026
0027 #define MSIWMI_BIOS_GUID "551A1F84-FBDD-4125-91DB-3EA8F44F1D45"
0028 #define MSIWMI_MSI_EVENT_GUID "B6F3EEF2-3D2F-49DC-9DE3-85BCE18C62F2"
0029 #define MSIWMI_WIND_EVENT_GUID "5B3CC38A-40D9-7245-8AE6-1145B751BE3F"
0030
0031 MODULE_ALIAS("wmi:" MSIWMI_BIOS_GUID);
0032 MODULE_ALIAS("wmi:" MSIWMI_MSI_EVENT_GUID);
0033 MODULE_ALIAS("wmi:" MSIWMI_WIND_EVENT_GUID);
0034
0035 enum msi_scancodes {
0036
0037 MSI_KEY_BRIGHTNESSUP = 0xD0,
0038 MSI_KEY_BRIGHTNESSDOWN,
0039 MSI_KEY_VOLUMEUP,
0040 MSI_KEY_VOLUMEDOWN,
0041 MSI_KEY_MUTE,
0042
0043 WIND_KEY_TOUCHPAD = 0x08,
0044 WIND_KEY_BLUETOOTH = 0x56,
0045 WIND_KEY_CAMERA,
0046 WIND_KEY_WLAN = 0x5f,
0047 WIND_KEY_TURBO,
0048 WIND_KEY_ECO = 0x69,
0049 };
0050 static struct key_entry msi_wmi_keymap[] = {
0051 { KE_KEY, MSI_KEY_BRIGHTNESSUP, {KEY_BRIGHTNESSUP} },
0052 { KE_KEY, MSI_KEY_BRIGHTNESSDOWN, {KEY_BRIGHTNESSDOWN} },
0053 { KE_KEY, MSI_KEY_VOLUMEUP, {KEY_VOLUMEUP} },
0054 { KE_KEY, MSI_KEY_VOLUMEDOWN, {KEY_VOLUMEDOWN} },
0055 { KE_KEY, MSI_KEY_MUTE, {KEY_MUTE} },
0056
0057
0058 { KE_IGNORE, WIND_KEY_TOUCHPAD, {KEY_TOUCHPAD_TOGGLE} },
0059 { KE_IGNORE, WIND_KEY_BLUETOOTH, {KEY_BLUETOOTH} },
0060 { KE_IGNORE, WIND_KEY_CAMERA, {KEY_CAMERA} },
0061 { KE_IGNORE, WIND_KEY_WLAN, {KEY_WLAN} },
0062
0063
0064 { KE_IGNORE, 0x00 },
0065 { KE_IGNORE, 0x62 },
0066 { KE_IGNORE, 0x63 },
0067
0068
0069 { KE_KEY, WIND_KEY_TURBO, {KEY_PROG1} },
0070 { KE_KEY, WIND_KEY_ECO, {KEY_PROG2} },
0071
0072 { KE_END, 0 }
0073 };
0074
0075 static ktime_t last_pressed;
0076
0077 static const struct {
0078 const char *guid;
0079 bool quirk_last_pressed;
0080 } *event_wmi, event_wmis[] = {
0081 { MSIWMI_MSI_EVENT_GUID, true },
0082 { MSIWMI_WIND_EVENT_GUID, false },
0083 };
0084
0085 static struct backlight_device *backlight;
0086
0087 static int backlight_map[] = { 0x00, 0x33, 0x66, 0x99, 0xCC, 0xFF };
0088
0089 static struct input_dev *msi_wmi_input_dev;
0090
0091 static int msi_wmi_query_block(int instance, int *ret)
0092 {
0093 acpi_status status;
0094 union acpi_object *obj;
0095
0096 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
0097
0098 status = wmi_query_block(MSIWMI_BIOS_GUID, instance, &output);
0099 if (ACPI_FAILURE(status))
0100 return -EIO;
0101
0102 obj = output.pointer;
0103
0104 if (!obj || obj->type != ACPI_TYPE_INTEGER) {
0105 if (obj) {
0106 pr_err("query block returned object "
0107 "type: %d - buffer length:%d\n", obj->type,
0108 obj->type == ACPI_TYPE_BUFFER ?
0109 obj->buffer.length : 0);
0110 }
0111 kfree(obj);
0112 return -EINVAL;
0113 }
0114 *ret = obj->integer.value;
0115 kfree(obj);
0116 return 0;
0117 }
0118
0119 static int msi_wmi_set_block(int instance, int value)
0120 {
0121 acpi_status status;
0122
0123 struct acpi_buffer input = { sizeof(int), &value };
0124
0125 pr_debug("Going to set block of instance: %d - value: %d\n",
0126 instance, value);
0127
0128 status = wmi_set_block(MSIWMI_BIOS_GUID, instance, &input);
0129
0130 return ACPI_SUCCESS(status) ? 0 : 1;
0131 }
0132
0133 static int bl_get(struct backlight_device *bd)
0134 {
0135 int level, err, ret;
0136
0137
0138 err = msi_wmi_query_block(1, &ret);
0139 if (err) {
0140 pr_err("Could not query backlight: %d\n", err);
0141 return -EINVAL;
0142 }
0143 pr_debug("Get: Query block returned: %d\n", ret);
0144 for (level = 0; level < ARRAY_SIZE(backlight_map); level++) {
0145 if (backlight_map[level] == ret) {
0146 pr_debug("Current backlight level: 0x%X - index: %d\n",
0147 backlight_map[level], level);
0148 break;
0149 }
0150 }
0151 if (level == ARRAY_SIZE(backlight_map)) {
0152 pr_err("get: Invalid brightness value: 0x%X\n", ret);
0153 return -EINVAL;
0154 }
0155 return level;
0156 }
0157
0158 static int bl_set_status(struct backlight_device *bd)
0159 {
0160 int bright = bd->props.brightness;
0161 if (bright >= ARRAY_SIZE(backlight_map) || bright < 0)
0162 return -EINVAL;
0163
0164
0165 return msi_wmi_set_block(0, backlight_map[bright]);
0166 }
0167
0168 static const struct backlight_ops msi_backlight_ops = {
0169 .get_brightness = bl_get,
0170 .update_status = bl_set_status,
0171 };
0172
0173 static void msi_wmi_notify(u32 value, void *context)
0174 {
0175 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
0176 struct key_entry *key;
0177 union acpi_object *obj;
0178 acpi_status status;
0179
0180 status = wmi_get_event_data(value, &response);
0181 if (status != AE_OK) {
0182 pr_info("bad event status 0x%x\n", status);
0183 return;
0184 }
0185
0186 obj = (union acpi_object *)response.pointer;
0187
0188 if (obj && obj->type == ACPI_TYPE_INTEGER) {
0189 int eventcode = obj->integer.value;
0190 pr_debug("Eventcode: 0x%x\n", eventcode);
0191 key = sparse_keymap_entry_from_scancode(msi_wmi_input_dev,
0192 eventcode);
0193 if (!key) {
0194 pr_info("Unknown key pressed - %x\n", eventcode);
0195 goto msi_wmi_notify_exit;
0196 }
0197
0198 if (event_wmi->quirk_last_pressed) {
0199 ktime_t cur = ktime_get_real();
0200 ktime_t diff = ktime_sub(cur, last_pressed);
0201
0202
0203 if (ktime_to_us(diff) < 1000 * 50) {
0204 pr_debug("Suppressed key event 0x%X - "
0205 "Last press was %lld us ago\n",
0206 key->code, ktime_to_us(diff));
0207 goto msi_wmi_notify_exit;
0208 }
0209 last_pressed = cur;
0210 }
0211
0212 if (key->type == KE_KEY &&
0213
0214 (backlight ||
0215 (key->code != MSI_KEY_BRIGHTNESSUP &&
0216 key->code != MSI_KEY_BRIGHTNESSDOWN))) {
0217 pr_debug("Send key: 0x%X - Input layer keycode: %d\n",
0218 key->code, key->keycode);
0219 sparse_keymap_report_entry(msi_wmi_input_dev, key, 1,
0220 true);
0221 }
0222 } else
0223 pr_info("Unknown event received\n");
0224
0225 msi_wmi_notify_exit:
0226 kfree(response.pointer);
0227 }
0228
0229 static int __init msi_wmi_backlight_setup(void)
0230 {
0231 int err;
0232 struct backlight_properties props;
0233
0234 memset(&props, 0, sizeof(struct backlight_properties));
0235 props.type = BACKLIGHT_PLATFORM;
0236 props.max_brightness = ARRAY_SIZE(backlight_map) - 1;
0237 backlight = backlight_device_register(DRV_NAME, NULL, NULL,
0238 &msi_backlight_ops,
0239 &props);
0240 if (IS_ERR(backlight))
0241 return PTR_ERR(backlight);
0242
0243 err = bl_get(NULL);
0244 if (err < 0) {
0245 backlight_device_unregister(backlight);
0246 return err;
0247 }
0248
0249 backlight->props.brightness = err;
0250
0251 return 0;
0252 }
0253
0254 static int __init msi_wmi_input_setup(void)
0255 {
0256 int err;
0257
0258 msi_wmi_input_dev = input_allocate_device();
0259 if (!msi_wmi_input_dev)
0260 return -ENOMEM;
0261
0262 msi_wmi_input_dev->name = "MSI WMI hotkeys";
0263 msi_wmi_input_dev->phys = "wmi/input0";
0264 msi_wmi_input_dev->id.bustype = BUS_HOST;
0265
0266 err = sparse_keymap_setup(msi_wmi_input_dev, msi_wmi_keymap, NULL);
0267 if (err)
0268 goto err_free_dev;
0269
0270 err = input_register_device(msi_wmi_input_dev);
0271
0272 if (err)
0273 goto err_free_dev;
0274
0275 last_pressed = 0;
0276
0277 return 0;
0278
0279 err_free_dev:
0280 input_free_device(msi_wmi_input_dev);
0281 return err;
0282 }
0283
0284 static int __init msi_wmi_init(void)
0285 {
0286 int err;
0287 int i;
0288
0289 for (i = 0; i < ARRAY_SIZE(event_wmis); i++) {
0290 if (!wmi_has_guid(event_wmis[i].guid))
0291 continue;
0292
0293 err = msi_wmi_input_setup();
0294 if (err) {
0295 pr_err("Unable to setup input device\n");
0296 return err;
0297 }
0298
0299 err = wmi_install_notify_handler(event_wmis[i].guid,
0300 msi_wmi_notify, NULL);
0301 if (ACPI_FAILURE(err)) {
0302 pr_err("Unable to setup WMI notify handler\n");
0303 goto err_free_input;
0304 }
0305
0306 pr_debug("Event handler installed\n");
0307 event_wmi = &event_wmis[i];
0308 break;
0309 }
0310
0311 if (wmi_has_guid(MSIWMI_BIOS_GUID) &&
0312 acpi_video_get_backlight_type() == acpi_backlight_vendor) {
0313 err = msi_wmi_backlight_setup();
0314 if (err) {
0315 pr_err("Unable to setup backlight device\n");
0316 goto err_uninstall_handler;
0317 }
0318 pr_debug("Backlight device created\n");
0319 }
0320
0321 if (!event_wmi && !backlight) {
0322 pr_err("This machine doesn't have neither MSI-hotkeys nor backlight through WMI\n");
0323 return -ENODEV;
0324 }
0325
0326 return 0;
0327
0328 err_uninstall_handler:
0329 if (event_wmi)
0330 wmi_remove_notify_handler(event_wmi->guid);
0331 err_free_input:
0332 if (event_wmi)
0333 input_unregister_device(msi_wmi_input_dev);
0334 return err;
0335 }
0336
0337 static void __exit msi_wmi_exit(void)
0338 {
0339 if (event_wmi) {
0340 wmi_remove_notify_handler(event_wmi->guid);
0341 input_unregister_device(msi_wmi_input_dev);
0342 }
0343 backlight_device_unregister(backlight);
0344 }
0345
0346 module_init(msi_wmi_init);
0347 module_exit(msi_wmi_exit);