Matthew Garrett | ad8f07c | 2009-01-07 18:08:56 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Driver for Dell laptop extras |
| 3 | * |
| 4 | * Copyright (c) Red Hat <mjg@redhat.com> |
Pali Rohár | 02b2aaa | 2014-12-05 12:53:31 +0100 | [diff] [blame] | 5 | * Copyright (c) 2014 Gabriele Mazzotta <gabriele.mzt@gmail.com> |
| 6 | * Copyright (c) 2014 Pali Rohár <pali.rohar@gmail.com> |
Matthew Garrett | ad8f07c | 2009-01-07 18:08:56 -0800 | [diff] [blame] | 7 | * |
Pali Rohár | 02b2aaa | 2014-12-05 12:53:31 +0100 | [diff] [blame] | 8 | * Based on documentation in the libsmbios package: |
| 9 | * Copyright (C) 2005-2014 Dell Inc. |
Matthew Garrett | ad8f07c | 2009-01-07 18:08:56 -0800 | [diff] [blame] | 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License version 2 as |
| 13 | * published by the Free Software Foundation. |
| 14 | */ |
| 15 | |
Joe Perches | eb88952 | 2011-03-29 15:21:37 -0700 | [diff] [blame] | 16 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 17 | |
Matthew Garrett | ad8f07c | 2009-01-07 18:08:56 -0800 | [diff] [blame] | 18 | #include <linux/module.h> |
| 19 | #include <linux/kernel.h> |
| 20 | #include <linux/init.h> |
| 21 | #include <linux/platform_device.h> |
| 22 | #include <linux/backlight.h> |
| 23 | #include <linux/err.h> |
| 24 | #include <linux/dmi.h> |
| 25 | #include <linux/io.h> |
Hans de Goede | 4cc8a57 | 2013-11-17 14:00:16 +0100 | [diff] [blame] | 26 | #include <linux/rfkill.h> |
Matthew Garrett | ad8f07c | 2009-01-07 18:08:56 -0800 | [diff] [blame] | 27 | #include <linux/power_supply.h> |
| 28 | #include <linux/acpi.h> |
Stuart Hayes | 116ee77 | 2010-02-10 14:12:13 -0500 | [diff] [blame] | 29 | #include <linux/mm.h> |
Matthew Garrett | 814cb8a | 2009-12-09 18:23:36 +0000 | [diff] [blame] | 30 | #include <linux/i8042.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 31 | #include <linux/slab.h> |
Keng-Yu Lin | 037accf | 2010-09-28 11:43:31 +0800 | [diff] [blame] | 32 | #include <linux/debugfs.h> |
| 33 | #include <linux/seq_file.h> |
Len Brown | cad7312 | 2009-01-09 17:23:38 -0500 | [diff] [blame] | 34 | #include "../../firmware/dcdbas.h" |
Matthew Garrett | ad8f07c | 2009-01-07 18:08:56 -0800 | [diff] [blame] | 35 | |
| 36 | #define BRIGHTNESS_TOKEN 0x7d |
Pali Rohár | 02b2aaa | 2014-12-05 12:53:31 +0100 | [diff] [blame] | 37 | #define KBD_LED_OFF_TOKEN 0x01E1 |
| 38 | #define KBD_LED_ON_TOKEN 0x01E2 |
| 39 | #define KBD_LED_AUTO_TOKEN 0x01E3 |
| 40 | #define KBD_LED_AUTO_25_TOKEN 0x02EA |
| 41 | #define KBD_LED_AUTO_50_TOKEN 0x02EB |
| 42 | #define KBD_LED_AUTO_75_TOKEN 0x02EC |
| 43 | #define KBD_LED_AUTO_100_TOKEN 0x02F6 |
Matthew Garrett | ad8f07c | 2009-01-07 18:08:56 -0800 | [diff] [blame] | 44 | |
| 45 | /* This structure will be modified by the firmware when we enter |
| 46 | * system management mode, hence the volatiles */ |
| 47 | |
| 48 | struct calling_interface_buffer { |
| 49 | u16 class; |
| 50 | u16 select; |
| 51 | volatile u32 input[4]; |
| 52 | volatile u32 output[4]; |
| 53 | } __packed; |
| 54 | |
| 55 | struct calling_interface_token { |
| 56 | u16 tokenID; |
| 57 | u16 location; |
| 58 | union { |
| 59 | u16 value; |
| 60 | u16 stringlength; |
| 61 | }; |
| 62 | }; |
| 63 | |
| 64 | struct calling_interface_structure { |
| 65 | struct dmi_header header; |
| 66 | u16 cmdIOAddress; |
| 67 | u8 cmdIOCode; |
| 68 | u32 supportedCmds; |
| 69 | struct calling_interface_token tokens[]; |
| 70 | } __packed; |
| 71 | |
AceLan Kao | 2d8b90b | 2011-10-04 16:25:44 +0800 | [diff] [blame] | 72 | struct quirk_entry { |
| 73 | u8 touchpad_led; |
Pali Rohár | 02b2aaa | 2014-12-05 12:53:31 +0100 | [diff] [blame] | 74 | |
| 75 | int needs_kbd_timeouts; |
| 76 | /* |
| 77 | * Ordered list of timeouts expressed in seconds. |
| 78 | * The list must end with -1 |
| 79 | */ |
| 80 | int kbd_timeouts[]; |
AceLan Kao | 2d8b90b | 2011-10-04 16:25:44 +0800 | [diff] [blame] | 81 | }; |
| 82 | |
| 83 | static struct quirk_entry *quirks; |
| 84 | |
| 85 | static struct quirk_entry quirk_dell_vostro_v130 = { |
| 86 | .touchpad_led = 1, |
| 87 | }; |
| 88 | |
Mathias Krause | 681480c | 2014-07-16 19:43:09 +0200 | [diff] [blame] | 89 | static int __init dmi_matched(const struct dmi_system_id *dmi) |
AceLan Kao | 2d8b90b | 2011-10-04 16:25:44 +0800 | [diff] [blame] | 90 | { |
| 91 | quirks = dmi->driver_data; |
| 92 | return 1; |
| 93 | } |
| 94 | |
Pali Rohár | 02b2aaa | 2014-12-05 12:53:31 +0100 | [diff] [blame] | 95 | /* |
| 96 | * These values come from Windows utility provided by Dell. If any other value |
| 97 | * is used then BIOS silently set timeout to 0 without any error message. |
| 98 | */ |
| 99 | static struct quirk_entry quirk_dell_xps13_9333 = { |
| 100 | .needs_kbd_timeouts = 1, |
| 101 | .kbd_timeouts = { 0, 5, 15, 60, 5 * 60, 15 * 60, -1 }, |
| 102 | }; |
| 103 | |
Matthew Garrett | ad8f07c | 2009-01-07 18:08:56 -0800 | [diff] [blame] | 104 | static int da_command_address; |
| 105 | static int da_command_code; |
| 106 | static int da_num_tokens; |
| 107 | static struct calling_interface_token *da_tokens; |
| 108 | |
Alan Jenkins | ada3248 | 2009-08-19 15:06:49 +0100 | [diff] [blame] | 109 | static struct platform_driver platform_driver = { |
| 110 | .driver = { |
| 111 | .name = "dell-laptop", |
Alan Jenkins | ada3248 | 2009-08-19 15:06:49 +0100 | [diff] [blame] | 112 | } |
| 113 | }; |
| 114 | |
| 115 | static struct platform_device *platform_device; |
Matthew Garrett | ad8f07c | 2009-01-07 18:08:56 -0800 | [diff] [blame] | 116 | static struct backlight_device *dell_backlight_device; |
Hans de Goede | 4cc8a57 | 2013-11-17 14:00:16 +0100 | [diff] [blame] | 117 | static struct rfkill *wifi_rfkill; |
| 118 | static struct rfkill *bluetooth_rfkill; |
| 119 | static struct rfkill *wwan_rfkill; |
Hans de Goede | 8e0e668 | 2013-11-17 14:00:26 +0100 | [diff] [blame] | 120 | static bool force_rfkill; |
| 121 | |
| 122 | module_param(force_rfkill, bool, 0444); |
| 123 | MODULE_PARM_DESC(force_rfkill, "enable rfkill on non whitelisted models"); |
Matthew Garrett | ad8f07c | 2009-01-07 18:08:56 -0800 | [diff] [blame] | 124 | |
Uwe Kleine-König | 145047d | 2012-03-30 22:05:04 +0200 | [diff] [blame] | 125 | static const struct dmi_system_id dell_device_table[] __initconst = { |
Matthew Garrett | ad8f07c | 2009-01-07 18:08:56 -0800 | [diff] [blame] | 126 | { |
| 127 | .ident = "Dell laptop", |
| 128 | .matches = { |
| 129 | DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), |
| 130 | DMI_MATCH(DMI_CHASSIS_TYPE, "8"), |
| 131 | }, |
| 132 | }, |
Erik Andren | cb6a793 | 2010-02-14 11:53:23 -0500 | [diff] [blame] | 133 | { |
Rezwanul Kabir | 410d44c | 2010-06-23 12:02:43 -0500 | [diff] [blame] | 134 | .matches = { |
| 135 | DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), |
| 136 | DMI_MATCH(DMI_CHASSIS_TYPE, "9"), /*Laptop*/ |
| 137 | }, |
| 138 | }, |
| 139 | { |
Erik Andren | cb6a793 | 2010-02-14 11:53:23 -0500 | [diff] [blame] | 140 | .ident = "Dell Computer Corporation", |
| 141 | .matches = { |
| 142 | DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"), |
| 143 | DMI_MATCH(DMI_CHASSIS_TYPE, "8"), |
| 144 | }, |
| 145 | }, |
Matthew Garrett | ad8f07c | 2009-01-07 18:08:56 -0800 | [diff] [blame] | 146 | { } |
| 147 | }; |
Dmitry Torokhov | 35ae64f | 2011-11-14 00:25:00 -0800 | [diff] [blame] | 148 | MODULE_DEVICE_TABLE(dmi, dell_device_table); |
Matthew Garrett | ad8f07c | 2009-01-07 18:08:56 -0800 | [diff] [blame] | 149 | |
Mathias Krause | 681480c | 2014-07-16 19:43:09 +0200 | [diff] [blame] | 150 | static const struct dmi_system_id dell_quirks[] __initconst = { |
AceLan Kao | 2d8b90b | 2011-10-04 16:25:44 +0800 | [diff] [blame] | 151 | { |
| 152 | .callback = dmi_matched, |
| 153 | .ident = "Dell Vostro V130", |
| 154 | .matches = { |
| 155 | DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), |
| 156 | DMI_MATCH(DMI_PRODUCT_NAME, "Vostro V130"), |
| 157 | }, |
| 158 | .driver_data = &quirk_dell_vostro_v130, |
| 159 | }, |
| 160 | { |
| 161 | .callback = dmi_matched, |
| 162 | .ident = "Dell Vostro V131", |
| 163 | .matches = { |
| 164 | DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), |
| 165 | DMI_MATCH(DMI_PRODUCT_NAME, "Vostro V131"), |
| 166 | }, |
| 167 | .driver_data = &quirk_dell_vostro_v130, |
| 168 | }, |
AceLan Kao | 2a74885 | 2011-11-17 15:30:42 +0800 | [diff] [blame] | 169 | { |
| 170 | .callback = dmi_matched, |
Ang Way Chuang | 57b31b2 | 2012-04-12 13:11:27 +0800 | [diff] [blame] | 171 | .ident = "Dell Vostro 3350", |
| 172 | .matches = { |
| 173 | DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), |
| 174 | DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3350"), |
| 175 | }, |
| 176 | .driver_data = &quirk_dell_vostro_v130, |
| 177 | }, |
| 178 | { |
| 179 | .callback = dmi_matched, |
AceLan Kao | 2a74885 | 2011-11-17 15:30:42 +0800 | [diff] [blame] | 180 | .ident = "Dell Vostro 3555", |
| 181 | .matches = { |
| 182 | DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), |
| 183 | DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3555"), |
| 184 | }, |
| 185 | .driver_data = &quirk_dell_vostro_v130, |
| 186 | }, |
| 187 | { |
| 188 | .callback = dmi_matched, |
| 189 | .ident = "Dell Inspiron N311z", |
| 190 | .matches = { |
| 191 | DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), |
| 192 | DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron N311z"), |
| 193 | }, |
| 194 | .driver_data = &quirk_dell_vostro_v130, |
| 195 | }, |
| 196 | { |
| 197 | .callback = dmi_matched, |
| 198 | .ident = "Dell Inspiron M5110", |
| 199 | .matches = { |
| 200 | DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), |
| 201 | DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron M5110"), |
| 202 | }, |
| 203 | .driver_data = &quirk_dell_vostro_v130, |
| 204 | }, |
AceLan Kao | 7f83922 | 2012-04-20 11:47:26 +0800 | [diff] [blame] | 205 | { |
| 206 | .callback = dmi_matched, |
| 207 | .ident = "Dell Vostro 3360", |
| 208 | .matches = { |
| 209 | DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), |
| 210 | DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3360"), |
| 211 | }, |
| 212 | .driver_data = &quirk_dell_vostro_v130, |
| 213 | }, |
| 214 | { |
| 215 | .callback = dmi_matched, |
| 216 | .ident = "Dell Vostro 3460", |
| 217 | .matches = { |
| 218 | DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), |
| 219 | DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3460"), |
| 220 | }, |
| 221 | .driver_data = &quirk_dell_vostro_v130, |
| 222 | }, |
| 223 | { |
| 224 | .callback = dmi_matched, |
| 225 | .ident = "Dell Vostro 3560", |
| 226 | .matches = { |
| 227 | DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), |
| 228 | DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3560"), |
| 229 | }, |
| 230 | .driver_data = &quirk_dell_vostro_v130, |
| 231 | }, |
AceLan Kao | d0e0a477 | 2012-05-22 12:38:51 +0800 | [diff] [blame] | 232 | { |
| 233 | .callback = dmi_matched, |
| 234 | .ident = "Dell Vostro 3450", |
| 235 | .matches = { |
| 236 | DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), |
| 237 | DMI_MATCH(DMI_PRODUCT_NAME, "Dell System Vostro 3450"), |
| 238 | }, |
| 239 | .driver_data = &quirk_dell_vostro_v130, |
| 240 | }, |
AceLan Kao | 5f1e88f | 2012-07-13 16:39:57 +0800 | [diff] [blame] | 241 | { |
| 242 | .callback = dmi_matched, |
| 243 | .ident = "Dell Inspiron 5420", |
| 244 | .matches = { |
| 245 | DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), |
AceLan Kao | a2174ba | 2012-08-06 09:48:58 +0800 | [diff] [blame] | 246 | DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5420"), |
AceLan Kao | 5f1e88f | 2012-07-13 16:39:57 +0800 | [diff] [blame] | 247 | }, |
| 248 | .driver_data = &quirk_dell_vostro_v130, |
| 249 | }, |
| 250 | { |
| 251 | .callback = dmi_matched, |
| 252 | .ident = "Dell Inspiron 5520", |
| 253 | .matches = { |
| 254 | DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), |
AceLan Kao | a2174ba | 2012-08-06 09:48:58 +0800 | [diff] [blame] | 255 | DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5520"), |
AceLan Kao | 5f1e88f | 2012-07-13 16:39:57 +0800 | [diff] [blame] | 256 | }, |
| 257 | .driver_data = &quirk_dell_vostro_v130, |
| 258 | }, |
| 259 | { |
| 260 | .callback = dmi_matched, |
| 261 | .ident = "Dell Inspiron 5720", |
| 262 | .matches = { |
| 263 | DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), |
AceLan Kao | a2174ba | 2012-08-06 09:48:58 +0800 | [diff] [blame] | 264 | DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5720"), |
AceLan Kao | 5f1e88f | 2012-07-13 16:39:57 +0800 | [diff] [blame] | 265 | }, |
| 266 | .driver_data = &quirk_dell_vostro_v130, |
| 267 | }, |
| 268 | { |
| 269 | .callback = dmi_matched, |
| 270 | .ident = "Dell Inspiron 7420", |
| 271 | .matches = { |
| 272 | DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), |
AceLan Kao | a2174ba | 2012-08-06 09:48:58 +0800 | [diff] [blame] | 273 | DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7420"), |
AceLan Kao | 5f1e88f | 2012-07-13 16:39:57 +0800 | [diff] [blame] | 274 | }, |
| 275 | .driver_data = &quirk_dell_vostro_v130, |
| 276 | }, |
| 277 | { |
| 278 | .callback = dmi_matched, |
| 279 | .ident = "Dell Inspiron 7520", |
| 280 | .matches = { |
| 281 | DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), |
AceLan Kao | a2174ba | 2012-08-06 09:48:58 +0800 | [diff] [blame] | 282 | DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7520"), |
AceLan Kao | 5f1e88f | 2012-07-13 16:39:57 +0800 | [diff] [blame] | 283 | }, |
| 284 | .driver_data = &quirk_dell_vostro_v130, |
| 285 | }, |
| 286 | { |
| 287 | .callback = dmi_matched, |
| 288 | .ident = "Dell Inspiron 7720", |
| 289 | .matches = { |
| 290 | DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), |
AceLan Kao | a2174ba | 2012-08-06 09:48:58 +0800 | [diff] [blame] | 291 | DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7720"), |
AceLan Kao | 5f1e88f | 2012-07-13 16:39:57 +0800 | [diff] [blame] | 292 | }, |
| 293 | .driver_data = &quirk_dell_vostro_v130, |
| 294 | }, |
Pali Rohár | 02b2aaa | 2014-12-05 12:53:31 +0100 | [diff] [blame] | 295 | { |
| 296 | .callback = dmi_matched, |
| 297 | .ident = "Dell XPS13 9333", |
| 298 | .matches = { |
| 299 | DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), |
| 300 | DMI_MATCH(DMI_PRODUCT_NAME, "XPS13 9333"), |
| 301 | }, |
| 302 | .driver_data = &quirk_dell_xps13_9333, |
| 303 | }, |
Martin Nyhus | d62d421 | 2012-03-15 18:25:48 +0100 | [diff] [blame] | 304 | { } |
AceLan Kao | 2d8b90b | 2011-10-04 16:25:44 +0800 | [diff] [blame] | 305 | }; |
| 306 | |
Stuart Hayes | 116ee77 | 2010-02-10 14:12:13 -0500 | [diff] [blame] | 307 | static struct calling_interface_buffer *buffer; |
Ingo Molnar | 94d8f78 | 2010-03-01 09:43:52 -0500 | [diff] [blame] | 308 | static struct page *bufferpage; |
| 309 | static DEFINE_MUTEX(buffer_mutex); |
Stuart Hayes | 116ee77 | 2010-02-10 14:12:13 -0500 | [diff] [blame] | 310 | |
Matthew Garrett | c6760ac | 2010-02-10 14:44:03 -0500 | [diff] [blame] | 311 | static int hwswitch_state; |
| 312 | |
Stuart Hayes | 116ee77 | 2010-02-10 14:12:13 -0500 | [diff] [blame] | 313 | static void get_buffer(void) |
| 314 | { |
| 315 | mutex_lock(&buffer_mutex); |
| 316 | memset(buffer, 0, sizeof(struct calling_interface_buffer)); |
| 317 | } |
| 318 | |
| 319 | static void release_buffer(void) |
| 320 | { |
| 321 | mutex_unlock(&buffer_mutex); |
| 322 | } |
| 323 | |
Alan Jenkins | 4788df4 | 2009-08-19 15:06:50 +0100 | [diff] [blame] | 324 | static void __init parse_da_table(const struct dmi_header *dm) |
Matthew Garrett | ad8f07c | 2009-01-07 18:08:56 -0800 | [diff] [blame] | 325 | { |
| 326 | /* Final token is a terminator, so we don't want to copy it */ |
| 327 | int tokens = (dm->length-11)/sizeof(struct calling_interface_token)-1; |
David Woodhouse | fe9ab00 | 2013-03-14 13:21:00 +0000 | [diff] [blame] | 328 | struct calling_interface_token *new_da_tokens; |
Matthew Garrett | ad8f07c | 2009-01-07 18:08:56 -0800 | [diff] [blame] | 329 | struct calling_interface_structure *table = |
| 330 | container_of(dm, struct calling_interface_structure, header); |
| 331 | |
| 332 | /* 4 bytes of table header, plus 7 bytes of Dell header, plus at least |
| 333 | 6 bytes of entry */ |
| 334 | |
| 335 | if (dm->length < 17) |
| 336 | return; |
| 337 | |
| 338 | da_command_address = table->cmdIOAddress; |
| 339 | da_command_code = table->cmdIOCode; |
| 340 | |
David Woodhouse | fe9ab00 | 2013-03-14 13:21:00 +0000 | [diff] [blame] | 341 | new_da_tokens = krealloc(da_tokens, (da_num_tokens + tokens) * |
| 342 | sizeof(struct calling_interface_token), |
| 343 | GFP_KERNEL); |
Matthew Garrett | ad8f07c | 2009-01-07 18:08:56 -0800 | [diff] [blame] | 344 | |
David Woodhouse | fe9ab00 | 2013-03-14 13:21:00 +0000 | [diff] [blame] | 345 | if (!new_da_tokens) |
Matthew Garrett | ad8f07c | 2009-01-07 18:08:56 -0800 | [diff] [blame] | 346 | return; |
David Woodhouse | fe9ab00 | 2013-03-14 13:21:00 +0000 | [diff] [blame] | 347 | da_tokens = new_da_tokens; |
Matthew Garrett | ad8f07c | 2009-01-07 18:08:56 -0800 | [diff] [blame] | 348 | |
| 349 | memcpy(da_tokens+da_num_tokens, table->tokens, |
| 350 | sizeof(struct calling_interface_token) * tokens); |
| 351 | |
| 352 | da_num_tokens += tokens; |
| 353 | } |
| 354 | |
Alan Jenkins | 4788df4 | 2009-08-19 15:06:50 +0100 | [diff] [blame] | 355 | static void __init find_tokens(const struct dmi_header *dm, void *dummy) |
Matthew Garrett | ad8f07c | 2009-01-07 18:08:56 -0800 | [diff] [blame] | 356 | { |
| 357 | switch (dm->type) { |
| 358 | case 0xd4: /* Indexed IO */ |
Matthew Garrett | ad8f07c | 2009-01-07 18:08:56 -0800 | [diff] [blame] | 359 | case 0xd5: /* Protected Area Type 1 */ |
Matthew Garrett | ad8f07c | 2009-01-07 18:08:56 -0800 | [diff] [blame] | 360 | case 0xd6: /* Protected Area Type 2 */ |
| 361 | break; |
| 362 | case 0xda: /* Calling interface */ |
| 363 | parse_da_table(dm); |
| 364 | break; |
| 365 | } |
| 366 | } |
| 367 | |
Pali Rohár | 02b2aaa | 2014-12-05 12:53:31 +0100 | [diff] [blame] | 368 | static int find_token_id(int tokenid) |
Matthew Garrett | ad8f07c | 2009-01-07 18:08:56 -0800 | [diff] [blame] | 369 | { |
| 370 | int i; |
Pali Rohár | 02b2aaa | 2014-12-05 12:53:31 +0100 | [diff] [blame] | 371 | |
Matthew Garrett | ad8f07c | 2009-01-07 18:08:56 -0800 | [diff] [blame] | 372 | for (i = 0; i < da_num_tokens; i++) { |
| 373 | if (da_tokens[i].tokenID == tokenid) |
Pali Rohár | 02b2aaa | 2014-12-05 12:53:31 +0100 | [diff] [blame] | 374 | return i; |
Matthew Garrett | ad8f07c | 2009-01-07 18:08:56 -0800 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | return -1; |
| 378 | } |
| 379 | |
Pali Rohár | 02b2aaa | 2014-12-05 12:53:31 +0100 | [diff] [blame] | 380 | static int find_token_location(int tokenid) |
| 381 | { |
| 382 | int id; |
| 383 | |
| 384 | id = find_token_id(tokenid); |
| 385 | if (id == -1) |
| 386 | return -1; |
| 387 | |
| 388 | return da_tokens[id].location; |
| 389 | } |
| 390 | |
Matthew Garrett | ad8f07c | 2009-01-07 18:08:56 -0800 | [diff] [blame] | 391 | static struct calling_interface_buffer * |
| 392 | dell_send_request(struct calling_interface_buffer *buffer, int class, |
| 393 | int select) |
| 394 | { |
| 395 | struct smi_cmd command; |
| 396 | |
| 397 | command.magic = SMI_CMD_MAGIC; |
| 398 | command.command_address = da_command_address; |
| 399 | command.command_code = da_command_code; |
| 400 | command.ebx = virt_to_phys(buffer); |
| 401 | command.ecx = 0x42534931; |
| 402 | |
| 403 | buffer->class = class; |
| 404 | buffer->select = select; |
| 405 | |
| 406 | dcdbas_smi_request(&command); |
| 407 | |
| 408 | return buffer; |
| 409 | } |
| 410 | |
Pali Rohár | 02b2aaa | 2014-12-05 12:53:31 +0100 | [diff] [blame] | 411 | static inline int dell_smi_error(int value) |
| 412 | { |
| 413 | switch (value) { |
| 414 | case 0: /* Completed successfully */ |
| 415 | return 0; |
| 416 | case -1: /* Completed with error */ |
| 417 | return -EIO; |
| 418 | case -2: /* Function not supported */ |
| 419 | return -ENXIO; |
| 420 | default: /* Unknown error */ |
| 421 | return -EINVAL; |
| 422 | } |
| 423 | } |
| 424 | |
Hans de Goede | 4cc8a57 | 2013-11-17 14:00:16 +0100 | [diff] [blame] | 425 | /* Derived from information in DellWirelessCtl.cpp: |
| 426 | Class 17, select 11 is radio control. It returns an array of 32-bit values. |
| 427 | |
| 428 | Input byte 0 = 0: Wireless information |
| 429 | |
| 430 | result[0]: return code |
| 431 | result[1]: |
| 432 | Bit 0: Hardware switch supported |
| 433 | Bit 1: Wifi locator supported |
| 434 | Bit 2: Wifi is supported |
| 435 | Bit 3: Bluetooth is supported |
| 436 | Bit 4: WWAN is supported |
| 437 | Bit 5: Wireless keyboard supported |
| 438 | Bits 6-7: Reserved |
| 439 | Bit 8: Wifi is installed |
| 440 | Bit 9: Bluetooth is installed |
| 441 | Bit 10: WWAN is installed |
| 442 | Bits 11-15: Reserved |
| 443 | Bit 16: Hardware switch is on |
| 444 | Bit 17: Wifi is blocked |
| 445 | Bit 18: Bluetooth is blocked |
| 446 | Bit 19: WWAN is blocked |
| 447 | Bits 20-31: Reserved |
| 448 | result[2]: NVRAM size in bytes |
| 449 | result[3]: NVRAM format version number |
| 450 | |
| 451 | Input byte 0 = 2: Wireless switch configuration |
| 452 | result[0]: return code |
| 453 | result[1]: |
| 454 | Bit 0: Wifi controlled by switch |
| 455 | Bit 1: Bluetooth controlled by switch |
| 456 | Bit 2: WWAN controlled by switch |
| 457 | Bits 3-6: Reserved |
| 458 | Bit 7: Wireless switch config locked |
| 459 | Bit 8: Wifi locator enabled |
| 460 | Bits 9-14: Reserved |
| 461 | Bit 15: Wifi locator setting locked |
| 462 | Bits 16-31: Reserved |
| 463 | */ |
| 464 | |
| 465 | static int dell_rfkill_set(void *data, bool blocked) |
| 466 | { |
| 467 | int disable = blocked ? 1 : 0; |
| 468 | unsigned long radio = (unsigned long)data; |
| 469 | int hwswitch_bit = (unsigned long)data - 1; |
Hans de Goede | 4cc8a57 | 2013-11-17 14:00:16 +0100 | [diff] [blame] | 470 | |
| 471 | get_buffer(); |
| 472 | dell_send_request(buffer, 17, 11); |
| 473 | |
| 474 | /* If the hardware switch controls this radio, and the hardware |
Hans de Goede | ed11289 | 2013-11-17 14:00:24 +0100 | [diff] [blame] | 475 | switch is disabled, always disable the radio */ |
Hans de Goede | 4cc8a57 | 2013-11-17 14:00:16 +0100 | [diff] [blame] | 476 | if ((hwswitch_state & BIT(hwswitch_bit)) && |
Hans de Goede | 4d39d88 | 2013-11-17 14:00:22 +0100 | [diff] [blame] | 477 | !(buffer->output[1] & BIT(16))) |
Hans de Goede | ed11289 | 2013-11-17 14:00:24 +0100 | [diff] [blame] | 478 | disable = 1; |
Hans de Goede | 4cc8a57 | 2013-11-17 14:00:16 +0100 | [diff] [blame] | 479 | |
| 480 | buffer->input[0] = (1 | (radio<<8) | (disable << 16)); |
| 481 | dell_send_request(buffer, 17, 11); |
| 482 | |
Hans de Goede | 4cc8a57 | 2013-11-17 14:00:16 +0100 | [diff] [blame] | 483 | release_buffer(); |
Hans de Goede | 4d39d88 | 2013-11-17 14:00:22 +0100 | [diff] [blame] | 484 | return 0; |
Hans de Goede | 4cc8a57 | 2013-11-17 14:00:16 +0100 | [diff] [blame] | 485 | } |
| 486 | |
Hans de Goede | 04c9a3a | 2013-11-17 14:00:23 +0100 | [diff] [blame] | 487 | /* Must be called with the buffer held */ |
Hans de Goede | 33f9359 | 2013-11-17 14:00:20 +0100 | [diff] [blame] | 488 | static void dell_rfkill_update_sw_state(struct rfkill *rfkill, int radio, |
| 489 | int status) |
Hans de Goede | d038880 | 2013-11-17 14:00:19 +0100 | [diff] [blame] | 490 | { |
Hans de Goede | 04c9a3a | 2013-11-17 14:00:23 +0100 | [diff] [blame] | 491 | if (status & BIT(0)) { |
| 492 | /* Has hw-switch, sync sw_state to BIOS */ |
| 493 | int block = rfkill_blocked(rfkill); |
| 494 | buffer->input[0] = (1 | (radio << 8) | (block << 16)); |
| 495 | dell_send_request(buffer, 17, 11); |
| 496 | } else { |
Hans de Goede | 3f56588 | 2013-11-17 14:00:21 +0100 | [diff] [blame] | 497 | /* No hw-switch, sync BIOS state to sw_state */ |
| 498 | rfkill_set_sw_state(rfkill, !!(status & BIT(radio + 16))); |
| 499 | } |
Hans de Goede | 33f9359 | 2013-11-17 14:00:20 +0100 | [diff] [blame] | 500 | } |
Hans de Goede | d038880 | 2013-11-17 14:00:19 +0100 | [diff] [blame] | 501 | |
Hans de Goede | 33f9359 | 2013-11-17 14:00:20 +0100 | [diff] [blame] | 502 | static void dell_rfkill_update_hw_state(struct rfkill *rfkill, int radio, |
| 503 | int status) |
| 504 | { |
Hans de Goede | d038880 | 2013-11-17 14:00:19 +0100 | [diff] [blame] | 505 | if (hwswitch_state & (BIT(radio - 1))) |
| 506 | rfkill_set_hw_state(rfkill, !(status & BIT(16))); |
| 507 | } |
| 508 | |
Hans de Goede | 4cc8a57 | 2013-11-17 14:00:16 +0100 | [diff] [blame] | 509 | static void dell_rfkill_query(struct rfkill *rfkill, void *data) |
| 510 | { |
| 511 | int status; |
Hans de Goede | 4cc8a57 | 2013-11-17 14:00:16 +0100 | [diff] [blame] | 512 | |
| 513 | get_buffer(); |
| 514 | dell_send_request(buffer, 17, 11); |
| 515 | status = buffer->output[1]; |
Hans de Goede | 4cc8a57 | 2013-11-17 14:00:16 +0100 | [diff] [blame] | 516 | |
Hans de Goede | 33f9359 | 2013-11-17 14:00:20 +0100 | [diff] [blame] | 517 | dell_rfkill_update_hw_state(rfkill, (unsigned long)data, status); |
Hans de Goede | 04c9a3a | 2013-11-17 14:00:23 +0100 | [diff] [blame] | 518 | |
| 519 | release_buffer(); |
Hans de Goede | 4cc8a57 | 2013-11-17 14:00:16 +0100 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | static const struct rfkill_ops dell_rfkill_ops = { |
| 523 | .set_block = dell_rfkill_set, |
| 524 | .query = dell_rfkill_query, |
| 525 | }; |
| 526 | |
Keng-Yu Lin | 037accf | 2010-09-28 11:43:31 +0800 | [diff] [blame] | 527 | static struct dentry *dell_laptop_dir; |
| 528 | |
| 529 | static int dell_debugfs_show(struct seq_file *s, void *data) |
| 530 | { |
| 531 | int status; |
| 532 | |
| 533 | get_buffer(); |
| 534 | dell_send_request(buffer, 17, 11); |
| 535 | status = buffer->output[1]; |
| 536 | release_buffer(); |
| 537 | |
| 538 | seq_printf(s, "status:\t0x%X\n", status); |
| 539 | seq_printf(s, "Bit 0 : Hardware switch supported: %lu\n", |
| 540 | status & BIT(0)); |
| 541 | seq_printf(s, "Bit 1 : Wifi locator supported: %lu\n", |
| 542 | (status & BIT(1)) >> 1); |
| 543 | seq_printf(s, "Bit 2 : Wifi is supported: %lu\n", |
| 544 | (status & BIT(2)) >> 2); |
| 545 | seq_printf(s, "Bit 3 : Bluetooth is supported: %lu\n", |
| 546 | (status & BIT(3)) >> 3); |
| 547 | seq_printf(s, "Bit 4 : WWAN is supported: %lu\n", |
| 548 | (status & BIT(4)) >> 4); |
| 549 | seq_printf(s, "Bit 5 : Wireless keyboard supported: %lu\n", |
| 550 | (status & BIT(5)) >> 5); |
| 551 | seq_printf(s, "Bit 8 : Wifi is installed: %lu\n", |
| 552 | (status & BIT(8)) >> 8); |
| 553 | seq_printf(s, "Bit 9 : Bluetooth is installed: %lu\n", |
| 554 | (status & BIT(9)) >> 9); |
| 555 | seq_printf(s, "Bit 10: WWAN is installed: %lu\n", |
| 556 | (status & BIT(10)) >> 10); |
| 557 | seq_printf(s, "Bit 16: Hardware switch is on: %lu\n", |
| 558 | (status & BIT(16)) >> 16); |
| 559 | seq_printf(s, "Bit 17: Wifi is blocked: %lu\n", |
| 560 | (status & BIT(17)) >> 17); |
| 561 | seq_printf(s, "Bit 18: Bluetooth is blocked: %lu\n", |
| 562 | (status & BIT(18)) >> 18); |
| 563 | seq_printf(s, "Bit 19: WWAN is blocked: %lu\n", |
| 564 | (status & BIT(19)) >> 19); |
| 565 | |
| 566 | seq_printf(s, "\nhwswitch_state:\t0x%X\n", hwswitch_state); |
| 567 | seq_printf(s, "Bit 0 : Wifi controlled by switch: %lu\n", |
| 568 | hwswitch_state & BIT(0)); |
| 569 | seq_printf(s, "Bit 1 : Bluetooth controlled by switch: %lu\n", |
| 570 | (hwswitch_state & BIT(1)) >> 1); |
| 571 | seq_printf(s, "Bit 2 : WWAN controlled by switch: %lu\n", |
| 572 | (hwswitch_state & BIT(2)) >> 2); |
| 573 | seq_printf(s, "Bit 7 : Wireless switch config locked: %lu\n", |
| 574 | (hwswitch_state & BIT(7)) >> 7); |
| 575 | seq_printf(s, "Bit 8 : Wifi locator enabled: %lu\n", |
| 576 | (hwswitch_state & BIT(8)) >> 8); |
| 577 | seq_printf(s, "Bit 15: Wifi locator setting locked: %lu\n", |
| 578 | (hwswitch_state & BIT(15)) >> 15); |
| 579 | |
| 580 | return 0; |
| 581 | } |
| 582 | |
| 583 | static int dell_debugfs_open(struct inode *inode, struct file *file) |
| 584 | { |
| 585 | return single_open(file, dell_debugfs_show, inode->i_private); |
| 586 | } |
| 587 | |
| 588 | static const struct file_operations dell_debugfs_fops = { |
| 589 | .owner = THIS_MODULE, |
| 590 | .open = dell_debugfs_open, |
| 591 | .read = seq_read, |
| 592 | .llseek = seq_lseek, |
| 593 | .release = single_release, |
| 594 | }; |
| 595 | |
Hans de Goede | 4cc8a57 | 2013-11-17 14:00:16 +0100 | [diff] [blame] | 596 | static void dell_update_rfkill(struct work_struct *ignored) |
| 597 | { |
Hans de Goede | d038880 | 2013-11-17 14:00:19 +0100 | [diff] [blame] | 598 | int status; |
| 599 | |
| 600 | get_buffer(); |
| 601 | dell_send_request(buffer, 17, 11); |
| 602 | status = buffer->output[1]; |
Hans de Goede | d038880 | 2013-11-17 14:00:19 +0100 | [diff] [blame] | 603 | |
Hans de Goede | 33f9359 | 2013-11-17 14:00:20 +0100 | [diff] [blame] | 604 | if (wifi_rfkill) { |
| 605 | dell_rfkill_update_hw_state(wifi_rfkill, 1, status); |
| 606 | dell_rfkill_update_sw_state(wifi_rfkill, 1, status); |
| 607 | } |
| 608 | if (bluetooth_rfkill) { |
| 609 | dell_rfkill_update_hw_state(bluetooth_rfkill, 2, status); |
| 610 | dell_rfkill_update_sw_state(bluetooth_rfkill, 2, status); |
| 611 | } |
| 612 | if (wwan_rfkill) { |
| 613 | dell_rfkill_update_hw_state(wwan_rfkill, 3, status); |
| 614 | dell_rfkill_update_sw_state(wwan_rfkill, 3, status); |
| 615 | } |
Hans de Goede | 04c9a3a | 2013-11-17 14:00:23 +0100 | [diff] [blame] | 616 | |
| 617 | release_buffer(); |
Hans de Goede | 4cc8a57 | 2013-11-17 14:00:16 +0100 | [diff] [blame] | 618 | } |
| 619 | static DECLARE_DELAYED_WORK(dell_rfkill_work, dell_update_rfkill); |
| 620 | |
Hans de Goede | 97f440c | 2013-12-24 20:34:01 +0100 | [diff] [blame] | 621 | static bool dell_laptop_i8042_filter(unsigned char data, unsigned char str, |
| 622 | struct serio *port) |
| 623 | { |
| 624 | static bool extended; |
| 625 | |
Giedrius Statkevičius | 9828037 | 2014-10-18 02:57:20 +0300 | [diff] [blame] | 626 | if (str & I8042_STR_AUXDATA) |
Hans de Goede | 97f440c | 2013-12-24 20:34:01 +0100 | [diff] [blame] | 627 | return false; |
| 628 | |
| 629 | if (unlikely(data == 0xe0)) { |
| 630 | extended = true; |
| 631 | return false; |
| 632 | } else if (unlikely(extended)) { |
| 633 | switch (data) { |
| 634 | case 0x8: |
| 635 | schedule_delayed_work(&dell_rfkill_work, |
| 636 | round_jiffies_relative(HZ / 4)); |
| 637 | break; |
| 638 | } |
| 639 | extended = false; |
| 640 | } |
| 641 | |
| 642 | return false; |
| 643 | } |
Hans de Goede | 4cc8a57 | 2013-11-17 14:00:16 +0100 | [diff] [blame] | 644 | |
| 645 | static int __init dell_setup_rfkill(void) |
| 646 | { |
Hans de Goede | ba5194f | 2013-12-06 12:17:27 +0100 | [diff] [blame] | 647 | int status, ret, whitelisted; |
Hans de Goede | 2a92551 | 2013-11-17 14:00:17 +0100 | [diff] [blame] | 648 | const char *product; |
Hans de Goede | 4cc8a57 | 2013-11-17 14:00:16 +0100 | [diff] [blame] | 649 | |
Hans de Goede | 2a92551 | 2013-11-17 14:00:17 +0100 | [diff] [blame] | 650 | /* |
Hans de Goede | ba5194f | 2013-12-06 12:17:27 +0100 | [diff] [blame] | 651 | * rfkill support causes trouble on various models, mostly Inspirons. |
| 652 | * So we whitelist certain series, and don't support rfkill on others. |
Hans de Goede | 2a92551 | 2013-11-17 14:00:17 +0100 | [diff] [blame] | 653 | */ |
Hans de Goede | ba5194f | 2013-12-06 12:17:27 +0100 | [diff] [blame] | 654 | whitelisted = 0; |
Hans de Goede | 2a92551 | 2013-11-17 14:00:17 +0100 | [diff] [blame] | 655 | product = dmi_get_system_info(DMI_PRODUCT_NAME); |
Hans de Goede | ba5194f | 2013-12-06 12:17:27 +0100 | [diff] [blame] | 656 | if (product && (strncmp(product, "Latitude", 8) == 0 || |
| 657 | strncmp(product, "Precision", 9) == 0)) |
| 658 | whitelisted = 1; |
| 659 | if (!force_rfkill && !whitelisted) |
Hans de Goede | 4cc8a57 | 2013-11-17 14:00:16 +0100 | [diff] [blame] | 660 | return 0; |
Hans de Goede | 4cc8a57 | 2013-11-17 14:00:16 +0100 | [diff] [blame] | 661 | |
| 662 | get_buffer(); |
| 663 | dell_send_request(buffer, 17, 11); |
| 664 | status = buffer->output[1]; |
| 665 | buffer->input[0] = 0x2; |
| 666 | dell_send_request(buffer, 17, 11); |
| 667 | hwswitch_state = buffer->output[1]; |
| 668 | release_buffer(); |
| 669 | |
Hans de Goede | 2bd4ac1 | 2013-11-17 14:00:27 +0100 | [diff] [blame] | 670 | if (!(status & BIT(0))) { |
| 671 | if (force_rfkill) { |
| 672 | /* No hwsitch, clear all hw-controlled bits */ |
| 673 | hwswitch_state &= ~7; |
| 674 | } else { |
| 675 | /* rfkill is only tested on laptops with a hwswitch */ |
| 676 | return 0; |
| 677 | } |
| 678 | } |
| 679 | |
Hans de Goede | 4cc8a57 | 2013-11-17 14:00:16 +0100 | [diff] [blame] | 680 | if ((status & (1<<2|1<<8)) == (1<<2|1<<8)) { |
| 681 | wifi_rfkill = rfkill_alloc("dell-wifi", &platform_device->dev, |
| 682 | RFKILL_TYPE_WLAN, |
| 683 | &dell_rfkill_ops, (void *) 1); |
| 684 | if (!wifi_rfkill) { |
| 685 | ret = -ENOMEM; |
| 686 | goto err_wifi; |
| 687 | } |
| 688 | ret = rfkill_register(wifi_rfkill); |
| 689 | if (ret) |
| 690 | goto err_wifi; |
| 691 | } |
| 692 | |
| 693 | if ((status & (1<<3|1<<9)) == (1<<3|1<<9)) { |
| 694 | bluetooth_rfkill = rfkill_alloc("dell-bluetooth", |
| 695 | &platform_device->dev, |
| 696 | RFKILL_TYPE_BLUETOOTH, |
| 697 | &dell_rfkill_ops, (void *) 2); |
| 698 | if (!bluetooth_rfkill) { |
| 699 | ret = -ENOMEM; |
| 700 | goto err_bluetooth; |
| 701 | } |
| 702 | ret = rfkill_register(bluetooth_rfkill); |
| 703 | if (ret) |
| 704 | goto err_bluetooth; |
| 705 | } |
| 706 | |
| 707 | if ((status & (1<<4|1<<10)) == (1<<4|1<<10)) { |
| 708 | wwan_rfkill = rfkill_alloc("dell-wwan", |
| 709 | &platform_device->dev, |
| 710 | RFKILL_TYPE_WWAN, |
| 711 | &dell_rfkill_ops, (void *) 3); |
| 712 | if (!wwan_rfkill) { |
| 713 | ret = -ENOMEM; |
| 714 | goto err_wwan; |
| 715 | } |
| 716 | ret = rfkill_register(wwan_rfkill); |
| 717 | if (ret) |
| 718 | goto err_wwan; |
| 719 | } |
| 720 | |
Hans de Goede | 97f440c | 2013-12-24 20:34:01 +0100 | [diff] [blame] | 721 | ret = i8042_install_filter(dell_laptop_i8042_filter); |
| 722 | if (ret) { |
| 723 | pr_warn("Unable to install key filter\n"); |
| 724 | goto err_filter; |
| 725 | } |
| 726 | |
Hans de Goede | 4cc8a57 | 2013-11-17 14:00:16 +0100 | [diff] [blame] | 727 | return 0; |
Hans de Goede | 97f440c | 2013-12-24 20:34:01 +0100 | [diff] [blame] | 728 | err_filter: |
| 729 | if (wwan_rfkill) |
| 730 | rfkill_unregister(wwan_rfkill); |
Hans de Goede | 4cc8a57 | 2013-11-17 14:00:16 +0100 | [diff] [blame] | 731 | err_wwan: |
| 732 | rfkill_destroy(wwan_rfkill); |
| 733 | if (bluetooth_rfkill) |
| 734 | rfkill_unregister(bluetooth_rfkill); |
| 735 | err_bluetooth: |
| 736 | rfkill_destroy(bluetooth_rfkill); |
| 737 | if (wifi_rfkill) |
| 738 | rfkill_unregister(wifi_rfkill); |
| 739 | err_wifi: |
| 740 | rfkill_destroy(wifi_rfkill); |
| 741 | |
| 742 | return ret; |
| 743 | } |
| 744 | |
| 745 | static void dell_cleanup_rfkill(void) |
| 746 | { |
| 747 | if (wifi_rfkill) { |
| 748 | rfkill_unregister(wifi_rfkill); |
| 749 | rfkill_destroy(wifi_rfkill); |
| 750 | } |
| 751 | if (bluetooth_rfkill) { |
| 752 | rfkill_unregister(bluetooth_rfkill); |
| 753 | rfkill_destroy(bluetooth_rfkill); |
| 754 | } |
| 755 | if (wwan_rfkill) { |
| 756 | rfkill_unregister(wwan_rfkill); |
| 757 | rfkill_destroy(wwan_rfkill); |
| 758 | } |
| 759 | } |
| 760 | |
Matthew Garrett | ad8f07c | 2009-01-07 18:08:56 -0800 | [diff] [blame] | 761 | static int dell_send_intensity(struct backlight_device *bd) |
| 762 | { |
Stuart Hayes | 116ee77 | 2010-02-10 14:12:13 -0500 | [diff] [blame] | 763 | int ret = 0; |
Matthew Garrett | ad8f07c | 2009-01-07 18:08:56 -0800 | [diff] [blame] | 764 | |
Stuart Hayes | 116ee77 | 2010-02-10 14:12:13 -0500 | [diff] [blame] | 765 | get_buffer(); |
| 766 | buffer->input[0] = find_token_location(BRIGHTNESS_TOKEN); |
| 767 | buffer->input[1] = bd->props.brightness; |
Matthew Garrett | ad8f07c | 2009-01-07 18:08:56 -0800 | [diff] [blame] | 768 | |
Stuart Hayes | 116ee77 | 2010-02-10 14:12:13 -0500 | [diff] [blame] | 769 | if (buffer->input[0] == -1) { |
| 770 | ret = -ENODEV; |
| 771 | goto out; |
| 772 | } |
Matthew Garrett | ad8f07c | 2009-01-07 18:08:56 -0800 | [diff] [blame] | 773 | |
| 774 | if (power_supply_is_system_supplied() > 0) |
Stuart Hayes | 116ee77 | 2010-02-10 14:12:13 -0500 | [diff] [blame] | 775 | dell_send_request(buffer, 1, 2); |
Matthew Garrett | ad8f07c | 2009-01-07 18:08:56 -0800 | [diff] [blame] | 776 | else |
Stuart Hayes | 116ee77 | 2010-02-10 14:12:13 -0500 | [diff] [blame] | 777 | dell_send_request(buffer, 1, 1); |
Matthew Garrett | ad8f07c | 2009-01-07 18:08:56 -0800 | [diff] [blame] | 778 | |
Pali Rohár | 02b2aaa | 2014-12-05 12:53:31 +0100 | [diff] [blame] | 779 | out: |
Stuart Hayes | 116ee77 | 2010-02-10 14:12:13 -0500 | [diff] [blame] | 780 | release_buffer(); |
Wei Yongjun | 7da8fb2 | 2013-11-23 21:02:51 +0800 | [diff] [blame] | 781 | return ret; |
Matthew Garrett | ad8f07c | 2009-01-07 18:08:56 -0800 | [diff] [blame] | 782 | } |
| 783 | |
| 784 | static int dell_get_intensity(struct backlight_device *bd) |
| 785 | { |
Stuart Hayes | 116ee77 | 2010-02-10 14:12:13 -0500 | [diff] [blame] | 786 | int ret = 0; |
Matthew Garrett | ad8f07c | 2009-01-07 18:08:56 -0800 | [diff] [blame] | 787 | |
Stuart Hayes | 116ee77 | 2010-02-10 14:12:13 -0500 | [diff] [blame] | 788 | get_buffer(); |
| 789 | buffer->input[0] = find_token_location(BRIGHTNESS_TOKEN); |
Matthew Garrett | ad8f07c | 2009-01-07 18:08:56 -0800 | [diff] [blame] | 790 | |
Stuart Hayes | 116ee77 | 2010-02-10 14:12:13 -0500 | [diff] [blame] | 791 | if (buffer->input[0] == -1) { |
| 792 | ret = -ENODEV; |
| 793 | goto out; |
| 794 | } |
Matthew Garrett | ad8f07c | 2009-01-07 18:08:56 -0800 | [diff] [blame] | 795 | |
| 796 | if (power_supply_is_system_supplied() > 0) |
Stuart Hayes | 116ee77 | 2010-02-10 14:12:13 -0500 | [diff] [blame] | 797 | dell_send_request(buffer, 0, 2); |
Matthew Garrett | ad8f07c | 2009-01-07 18:08:56 -0800 | [diff] [blame] | 798 | else |
Stuart Hayes | 116ee77 | 2010-02-10 14:12:13 -0500 | [diff] [blame] | 799 | dell_send_request(buffer, 0, 1); |
Matthew Garrett | ad8f07c | 2009-01-07 18:08:56 -0800 | [diff] [blame] | 800 | |
Jose Alonso | b486742 | 2011-07-10 15:46:51 -0300 | [diff] [blame] | 801 | ret = buffer->output[1]; |
| 802 | |
Pali Rohár | 02b2aaa | 2014-12-05 12:53:31 +0100 | [diff] [blame] | 803 | out: |
Stuart Hayes | 116ee77 | 2010-02-10 14:12:13 -0500 | [diff] [blame] | 804 | release_buffer(); |
Jose Alonso | b486742 | 2011-07-10 15:46:51 -0300 | [diff] [blame] | 805 | return ret; |
Matthew Garrett | ad8f07c | 2009-01-07 18:08:56 -0800 | [diff] [blame] | 806 | } |
| 807 | |
Lionel Debroux | acc2472 | 2010-11-16 14:14:02 +0100 | [diff] [blame] | 808 | static const struct backlight_ops dell_ops = { |
Matthew Garrett | ad8f07c | 2009-01-07 18:08:56 -0800 | [diff] [blame] | 809 | .get_brightness = dell_get_intensity, |
| 810 | .update_status = dell_send_intensity, |
| 811 | }; |
| 812 | |
Randy Dunlap | 869f8df | 2011-11-16 18:20:51 -0800 | [diff] [blame] | 813 | static void touchpad_led_on(void) |
AceLan Kao | 2d8b90b | 2011-10-04 16:25:44 +0800 | [diff] [blame] | 814 | { |
| 815 | int command = 0x97; |
| 816 | char data = 1; |
| 817 | i8042_command(&data, command | 1 << 12); |
| 818 | } |
| 819 | |
Randy Dunlap | 869f8df | 2011-11-16 18:20:51 -0800 | [diff] [blame] | 820 | static void touchpad_led_off(void) |
AceLan Kao | 2d8b90b | 2011-10-04 16:25:44 +0800 | [diff] [blame] | 821 | { |
| 822 | int command = 0x97; |
| 823 | char data = 2; |
| 824 | i8042_command(&data, command | 1 << 12); |
| 825 | } |
| 826 | |
| 827 | static void touchpad_led_set(struct led_classdev *led_cdev, |
| 828 | enum led_brightness value) |
| 829 | { |
| 830 | if (value > 0) |
| 831 | touchpad_led_on(); |
| 832 | else |
| 833 | touchpad_led_off(); |
| 834 | } |
| 835 | |
| 836 | static struct led_classdev touchpad_led = { |
| 837 | .name = "dell-laptop::touchpad", |
| 838 | .brightness_set = touchpad_led_set, |
AceLan Kao | 2d5de9e | 2012-01-17 16:18:06 +0800 | [diff] [blame] | 839 | .flags = LED_CORE_SUSPENDRESUME, |
AceLan Kao | 2d8b90b | 2011-10-04 16:25:44 +0800 | [diff] [blame] | 840 | }; |
| 841 | |
Mathias Krause | 681480c | 2014-07-16 19:43:09 +0200 | [diff] [blame] | 842 | static int __init touchpad_led_init(struct device *dev) |
AceLan Kao | 2d8b90b | 2011-10-04 16:25:44 +0800 | [diff] [blame] | 843 | { |
| 844 | return led_classdev_register(dev, &touchpad_led); |
| 845 | } |
| 846 | |
| 847 | static void touchpad_led_exit(void) |
| 848 | { |
| 849 | led_classdev_unregister(&touchpad_led); |
| 850 | } |
| 851 | |
Pali Rohár | 02b2aaa | 2014-12-05 12:53:31 +0100 | [diff] [blame] | 852 | /* |
| 853 | * Derived from information in smbios-keyboard-ctl: |
| 854 | * |
| 855 | * cbClass 4 |
| 856 | * cbSelect 11 |
| 857 | * Keyboard illumination |
| 858 | * cbArg1 determines the function to be performed |
| 859 | * |
| 860 | * cbArg1 0x0 = Get Feature Information |
| 861 | * cbRES1 Standard return codes (0, -1, -2) |
| 862 | * cbRES2, word0 Bitmap of user-selectable modes |
| 863 | * bit 0 Always off (All systems) |
| 864 | * bit 1 Always on (Travis ATG, Siberia) |
| 865 | * bit 2 Auto: ALS-based On; ALS-based Off (Travis ATG) |
| 866 | * bit 3 Auto: ALS- and input-activity-based On; input-activity based Off |
| 867 | * bit 4 Auto: Input-activity-based On; input-activity based Off |
| 868 | * bit 5 Auto: Input-activity-based On (illumination level 25%); input-activity based Off |
| 869 | * bit 6 Auto: Input-activity-based On (illumination level 50%); input-activity based Off |
| 870 | * bit 7 Auto: Input-activity-based On (illumination level 75%); input-activity based Off |
| 871 | * bit 8 Auto: Input-activity-based On (illumination level 100%); input-activity based Off |
| 872 | * bits 9-15 Reserved for future use |
| 873 | * cbRES2, byte2 Reserved for future use |
| 874 | * cbRES2, byte3 Keyboard illumination type |
| 875 | * 0 Reserved |
| 876 | * 1 Tasklight |
| 877 | * 2 Backlight |
| 878 | * 3-255 Reserved for future use |
| 879 | * cbRES3, byte0 Supported auto keyboard illumination trigger bitmap. |
| 880 | * bit 0 Any keystroke |
| 881 | * bit 1 Touchpad activity |
| 882 | * bit 2 Pointing stick |
| 883 | * bit 3 Any mouse |
| 884 | * bits 4-7 Reserved for future use |
| 885 | * cbRES3, byte1 Supported timeout unit bitmap |
| 886 | * bit 0 Seconds |
| 887 | * bit 1 Minutes |
| 888 | * bit 2 Hours |
| 889 | * bit 3 Days |
| 890 | * bits 4-7 Reserved for future use |
| 891 | * cbRES3, byte2 Number of keyboard light brightness levels |
| 892 | * cbRES4, byte0 Maximum acceptable seconds value (0 if seconds not supported). |
| 893 | * cbRES4, byte1 Maximum acceptable minutes value (0 if minutes not supported). |
| 894 | * cbRES4, byte2 Maximum acceptable hours value (0 if hours not supported). |
| 895 | * cbRES4, byte3 Maximum acceptable days value (0 if days not supported) |
| 896 | * |
| 897 | * cbArg1 0x1 = Get Current State |
| 898 | * cbRES1 Standard return codes (0, -1, -2) |
| 899 | * cbRES2, word0 Bitmap of current mode state |
| 900 | * bit 0 Always off (All systems) |
| 901 | * bit 1 Always on (Travis ATG, Siberia) |
| 902 | * bit 2 Auto: ALS-based On; ALS-based Off (Travis ATG) |
| 903 | * bit 3 Auto: ALS- and input-activity-based On; input-activity based Off |
| 904 | * bit 4 Auto: Input-activity-based On; input-activity based Off |
| 905 | * bit 5 Auto: Input-activity-based On (illumination level 25%); input-activity based Off |
| 906 | * bit 6 Auto: Input-activity-based On (illumination level 50%); input-activity based Off |
| 907 | * bit 7 Auto: Input-activity-based On (illumination level 75%); input-activity based Off |
| 908 | * bit 8 Auto: Input-activity-based On (illumination level 100%); input-activity based Off |
| 909 | * bits 9-15 Reserved for future use |
| 910 | * Note: Only One bit can be set |
| 911 | * cbRES2, byte2 Currently active auto keyboard illumination triggers. |
| 912 | * bit 0 Any keystroke |
| 913 | * bit 1 Touchpad activity |
| 914 | * bit 2 Pointing stick |
| 915 | * bit 3 Any mouse |
| 916 | * bits 4-7 Reserved for future use |
| 917 | * cbRES2, byte3 Current Timeout |
| 918 | * bits 7:6 Timeout units indicator: |
| 919 | * 00b Seconds |
| 920 | * 01b Minutes |
| 921 | * 10b Hours |
| 922 | * 11b Days |
| 923 | * bits 5:0 Timeout value (0-63) in sec/min/hr/day |
| 924 | * NOTE: A value of 0 means always on (no timeout) if any bits of RES3 byte |
| 925 | * are set upon return from the [Get feature information] call. |
| 926 | * cbRES3, byte0 Current setting of ALS value that turns the light on or off. |
| 927 | * cbRES3, byte1 Current ALS reading |
| 928 | * cbRES3, byte2 Current keyboard light level. |
| 929 | * |
| 930 | * cbArg1 0x2 = Set New State |
| 931 | * cbRES1 Standard return codes (0, -1, -2) |
| 932 | * cbArg2, word0 Bitmap of current mode state |
| 933 | * bit 0 Always off (All systems) |
| 934 | * bit 1 Always on (Travis ATG, Siberia) |
| 935 | * bit 2 Auto: ALS-based On; ALS-based Off (Travis ATG) |
| 936 | * bit 3 Auto: ALS- and input-activity-based On; input-activity based Off |
| 937 | * bit 4 Auto: Input-activity-based On; input-activity based Off |
| 938 | * bit 5 Auto: Input-activity-based On (illumination level 25%); input-activity based Off |
| 939 | * bit 6 Auto: Input-activity-based On (illumination level 50%); input-activity based Off |
| 940 | * bit 7 Auto: Input-activity-based On (illumination level 75%); input-activity based Off |
| 941 | * bit 8 Auto: Input-activity-based On (illumination level 100%); input-activity based Off |
| 942 | * bits 9-15 Reserved for future use |
| 943 | * Note: Only One bit can be set |
| 944 | * cbArg2, byte2 Desired auto keyboard illumination triggers. Must remain inactive to allow |
| 945 | * keyboard to turn off automatically. |
| 946 | * bit 0 Any keystroke |
| 947 | * bit 1 Touchpad activity |
| 948 | * bit 2 Pointing stick |
| 949 | * bit 3 Any mouse |
| 950 | * bits 4-7 Reserved for future use |
| 951 | * cbArg2, byte3 Desired Timeout |
| 952 | * bits 7:6 Timeout units indicator: |
| 953 | * 00b Seconds |
| 954 | * 01b Minutes |
| 955 | * 10b Hours |
| 956 | * 11b Days |
| 957 | * bits 5:0 Timeout value (0-63) in sec/min/hr/day |
| 958 | * cbArg3, byte0 Desired setting of ALS value that turns the light on or off. |
| 959 | * cbArg3, byte2 Desired keyboard light level. |
| 960 | */ |
| 961 | |
| 962 | |
| 963 | enum kbd_timeout_unit { |
| 964 | KBD_TIMEOUT_SECONDS = 0, |
| 965 | KBD_TIMEOUT_MINUTES, |
| 966 | KBD_TIMEOUT_HOURS, |
| 967 | KBD_TIMEOUT_DAYS, |
| 968 | }; |
| 969 | |
| 970 | enum kbd_mode_bit { |
| 971 | KBD_MODE_BIT_OFF = 0, |
| 972 | KBD_MODE_BIT_ON, |
| 973 | KBD_MODE_BIT_ALS, |
| 974 | KBD_MODE_BIT_TRIGGER_ALS, |
| 975 | KBD_MODE_BIT_TRIGGER, |
| 976 | KBD_MODE_BIT_TRIGGER_25, |
| 977 | KBD_MODE_BIT_TRIGGER_50, |
| 978 | KBD_MODE_BIT_TRIGGER_75, |
| 979 | KBD_MODE_BIT_TRIGGER_100, |
| 980 | }; |
| 981 | |
| 982 | #define kbd_is_als_mode_bit(bit) \ |
| 983 | ((bit) == KBD_MODE_BIT_ALS || (bit) == KBD_MODE_BIT_TRIGGER_ALS) |
| 984 | #define kbd_is_trigger_mode_bit(bit) \ |
| 985 | ((bit) >= KBD_MODE_BIT_TRIGGER_ALS && (bit) <= KBD_MODE_BIT_TRIGGER_100) |
| 986 | #define kbd_is_level_mode_bit(bit) \ |
| 987 | ((bit) >= KBD_MODE_BIT_TRIGGER_25 && (bit) <= KBD_MODE_BIT_TRIGGER_100) |
| 988 | |
| 989 | struct kbd_info { |
| 990 | u16 modes; |
| 991 | u8 type; |
| 992 | u8 triggers; |
| 993 | u8 levels; |
| 994 | u8 seconds; |
| 995 | u8 minutes; |
| 996 | u8 hours; |
| 997 | u8 days; |
| 998 | }; |
| 999 | |
| 1000 | struct kbd_state { |
| 1001 | u8 mode_bit; |
| 1002 | u8 triggers; |
| 1003 | u8 timeout_value; |
| 1004 | u8 timeout_unit; |
| 1005 | u8 als_setting; |
| 1006 | u8 als_value; |
| 1007 | u8 level; |
| 1008 | }; |
| 1009 | |
| 1010 | static const int kbd_tokens[] = { |
| 1011 | KBD_LED_OFF_TOKEN, |
| 1012 | KBD_LED_AUTO_25_TOKEN, |
| 1013 | KBD_LED_AUTO_50_TOKEN, |
| 1014 | KBD_LED_AUTO_75_TOKEN, |
| 1015 | KBD_LED_AUTO_100_TOKEN, |
| 1016 | KBD_LED_ON_TOKEN, |
| 1017 | }; |
| 1018 | |
| 1019 | static u16 kbd_token_bits; |
| 1020 | |
| 1021 | static struct kbd_info kbd_info; |
| 1022 | static bool kbd_als_supported; |
| 1023 | static bool kbd_triggers_supported; |
| 1024 | |
| 1025 | static u8 kbd_mode_levels[16]; |
| 1026 | static int kbd_mode_levels_count; |
| 1027 | |
| 1028 | static u8 kbd_previous_level; |
| 1029 | static u8 kbd_previous_mode_bit; |
| 1030 | |
| 1031 | static bool kbd_led_present; |
| 1032 | |
| 1033 | /* |
| 1034 | * NOTE: there are three ways to set the keyboard backlight level. |
| 1035 | * First, via kbd_state.mode_bit (assigning KBD_MODE_BIT_TRIGGER_* value). |
| 1036 | * Second, via kbd_state.level (assigning numerical value <= kbd_info.levels). |
| 1037 | * Third, via SMBIOS tokens (KBD_LED_* in kbd_tokens) |
| 1038 | * |
| 1039 | * There are laptops which support only one of these methods. If we want to |
| 1040 | * support as many machines as possible we need to implement all three methods. |
| 1041 | * The first two methods use the kbd_state structure. The third uses SMBIOS |
| 1042 | * tokens. If kbd_info.levels == 0, the machine does not support setting the |
| 1043 | * keyboard backlight level via kbd_state.level. |
| 1044 | */ |
| 1045 | |
| 1046 | static int kbd_get_info(struct kbd_info *info) |
| 1047 | { |
| 1048 | u8 units; |
| 1049 | int ret; |
| 1050 | |
| 1051 | get_buffer(); |
| 1052 | |
| 1053 | buffer->input[0] = 0x0; |
| 1054 | dell_send_request(buffer, 4, 11); |
| 1055 | ret = buffer->output[0]; |
| 1056 | |
| 1057 | if (ret) { |
| 1058 | ret = dell_smi_error(ret); |
| 1059 | goto out; |
| 1060 | } |
| 1061 | |
| 1062 | info->modes = buffer->output[1] & 0xFFFF; |
| 1063 | info->type = (buffer->output[1] >> 24) & 0xFF; |
| 1064 | info->triggers = buffer->output[2] & 0xFF; |
| 1065 | units = (buffer->output[2] >> 8) & 0xFF; |
| 1066 | info->levels = (buffer->output[2] >> 16) & 0xFF; |
| 1067 | |
| 1068 | if (units & BIT(0)) |
| 1069 | info->seconds = (buffer->output[3] >> 0) & 0xFF; |
| 1070 | if (units & BIT(1)) |
| 1071 | info->minutes = (buffer->output[3] >> 8) & 0xFF; |
| 1072 | if (units & BIT(2)) |
| 1073 | info->hours = (buffer->output[3] >> 16) & 0xFF; |
| 1074 | if (units & BIT(3)) |
| 1075 | info->days = (buffer->output[3] >> 24) & 0xFF; |
| 1076 | |
| 1077 | out: |
| 1078 | release_buffer(); |
| 1079 | return ret; |
| 1080 | } |
| 1081 | |
| 1082 | static unsigned int kbd_get_max_level(void) |
| 1083 | { |
| 1084 | if (kbd_info.levels != 0) |
| 1085 | return kbd_info.levels; |
| 1086 | if (kbd_mode_levels_count > 0) |
| 1087 | return kbd_mode_levels_count - 1; |
| 1088 | return 0; |
| 1089 | } |
| 1090 | |
| 1091 | static int kbd_get_level(struct kbd_state *state) |
| 1092 | { |
| 1093 | int i; |
| 1094 | |
| 1095 | if (kbd_info.levels != 0) |
| 1096 | return state->level; |
| 1097 | |
| 1098 | if (kbd_mode_levels_count > 0) { |
| 1099 | for (i = 0; i < kbd_mode_levels_count; ++i) |
| 1100 | if (kbd_mode_levels[i] == state->mode_bit) |
| 1101 | return i; |
| 1102 | return 0; |
| 1103 | } |
| 1104 | |
| 1105 | return -EINVAL; |
| 1106 | } |
| 1107 | |
| 1108 | static int kbd_set_level(struct kbd_state *state, u8 level) |
| 1109 | { |
| 1110 | if (kbd_info.levels != 0) { |
| 1111 | if (level != 0) |
| 1112 | kbd_previous_level = level; |
| 1113 | if (state->level == level) |
| 1114 | return 0; |
| 1115 | state->level = level; |
| 1116 | if (level != 0 && state->mode_bit == KBD_MODE_BIT_OFF) |
| 1117 | state->mode_bit = kbd_previous_mode_bit; |
| 1118 | else if (level == 0 && state->mode_bit != KBD_MODE_BIT_OFF) { |
| 1119 | kbd_previous_mode_bit = state->mode_bit; |
| 1120 | state->mode_bit = KBD_MODE_BIT_OFF; |
| 1121 | } |
| 1122 | return 0; |
| 1123 | } |
| 1124 | |
| 1125 | if (kbd_mode_levels_count > 0 && level < kbd_mode_levels_count) { |
| 1126 | if (level != 0) |
| 1127 | kbd_previous_level = level; |
| 1128 | state->mode_bit = kbd_mode_levels[level]; |
| 1129 | return 0; |
| 1130 | } |
| 1131 | |
| 1132 | return -EINVAL; |
| 1133 | } |
| 1134 | |
| 1135 | static int kbd_get_state(struct kbd_state *state) |
| 1136 | { |
| 1137 | int ret; |
| 1138 | |
| 1139 | get_buffer(); |
| 1140 | |
| 1141 | buffer->input[0] = 0x1; |
| 1142 | dell_send_request(buffer, 4, 11); |
| 1143 | ret = buffer->output[0]; |
| 1144 | |
| 1145 | if (ret) { |
| 1146 | ret = dell_smi_error(ret); |
| 1147 | goto out; |
| 1148 | } |
| 1149 | |
| 1150 | state->mode_bit = ffs(buffer->output[1] & 0xFFFF); |
| 1151 | if (state->mode_bit != 0) |
| 1152 | state->mode_bit--; |
| 1153 | |
| 1154 | state->triggers = (buffer->output[1] >> 16) & 0xFF; |
| 1155 | state->timeout_value = (buffer->output[1] >> 24) & 0x3F; |
| 1156 | state->timeout_unit = (buffer->output[1] >> 30) & 0x3; |
| 1157 | state->als_setting = buffer->output[2] & 0xFF; |
| 1158 | state->als_value = (buffer->output[2] >> 8) & 0xFF; |
| 1159 | state->level = (buffer->output[2] >> 16) & 0xFF; |
| 1160 | |
| 1161 | out: |
| 1162 | release_buffer(); |
| 1163 | return ret; |
| 1164 | } |
| 1165 | |
| 1166 | static int kbd_set_state(struct kbd_state *state) |
| 1167 | { |
| 1168 | int ret; |
| 1169 | |
| 1170 | get_buffer(); |
| 1171 | buffer->input[0] = 0x2; |
| 1172 | buffer->input[1] = BIT(state->mode_bit) & 0xFFFF; |
| 1173 | buffer->input[1] |= (state->triggers & 0xFF) << 16; |
| 1174 | buffer->input[1] |= (state->timeout_value & 0x3F) << 24; |
| 1175 | buffer->input[1] |= (state->timeout_unit & 0x3) << 30; |
| 1176 | buffer->input[2] = state->als_setting & 0xFF; |
| 1177 | buffer->input[2] |= (state->level & 0xFF) << 16; |
| 1178 | dell_send_request(buffer, 4, 11); |
| 1179 | ret = buffer->output[0]; |
| 1180 | release_buffer(); |
| 1181 | |
| 1182 | return dell_smi_error(ret); |
| 1183 | } |
| 1184 | |
| 1185 | static int kbd_set_state_safe(struct kbd_state *state, struct kbd_state *old) |
| 1186 | { |
| 1187 | int ret; |
| 1188 | |
| 1189 | ret = kbd_set_state(state); |
| 1190 | if (ret == 0) |
| 1191 | return 0; |
| 1192 | |
| 1193 | /* |
| 1194 | * When setting the new state fails,try to restore the previous one. |
| 1195 | * This is needed on some machines where BIOS sets a default state when |
| 1196 | * setting a new state fails. This default state could be all off. |
| 1197 | */ |
| 1198 | |
| 1199 | if (kbd_set_state(old)) |
| 1200 | pr_err("Setting old previous keyboard state failed\n"); |
| 1201 | |
| 1202 | return ret; |
| 1203 | } |
| 1204 | |
| 1205 | static int kbd_set_token_bit(u8 bit) |
| 1206 | { |
| 1207 | int id; |
| 1208 | int ret; |
| 1209 | |
| 1210 | if (bit >= ARRAY_SIZE(kbd_tokens)) |
| 1211 | return -EINVAL; |
| 1212 | |
| 1213 | id = find_token_id(kbd_tokens[bit]); |
| 1214 | if (id == -1) |
| 1215 | return -EINVAL; |
| 1216 | |
| 1217 | get_buffer(); |
| 1218 | buffer->input[0] = da_tokens[id].location; |
| 1219 | buffer->input[1] = da_tokens[id].value; |
| 1220 | dell_send_request(buffer, 1, 0); |
| 1221 | ret = buffer->output[0]; |
| 1222 | release_buffer(); |
| 1223 | |
| 1224 | return dell_smi_error(ret); |
| 1225 | } |
| 1226 | |
| 1227 | static int kbd_get_token_bit(u8 bit) |
| 1228 | { |
| 1229 | int id; |
| 1230 | int ret; |
| 1231 | int val; |
| 1232 | |
| 1233 | if (bit >= ARRAY_SIZE(kbd_tokens)) |
| 1234 | return -EINVAL; |
| 1235 | |
| 1236 | id = find_token_id(kbd_tokens[bit]); |
| 1237 | if (id == -1) |
| 1238 | return -EINVAL; |
| 1239 | |
| 1240 | get_buffer(); |
| 1241 | buffer->input[0] = da_tokens[id].location; |
| 1242 | dell_send_request(buffer, 0, 0); |
| 1243 | ret = buffer->output[0]; |
| 1244 | val = buffer->output[1]; |
| 1245 | release_buffer(); |
| 1246 | |
| 1247 | if (ret) |
| 1248 | return dell_smi_error(ret); |
| 1249 | |
| 1250 | return (val == da_tokens[id].value); |
| 1251 | } |
| 1252 | |
| 1253 | static int kbd_get_first_active_token_bit(void) |
| 1254 | { |
| 1255 | int i; |
| 1256 | int ret; |
| 1257 | |
| 1258 | for (i = 0; i < ARRAY_SIZE(kbd_tokens); ++i) { |
| 1259 | ret = kbd_get_token_bit(i); |
| 1260 | if (ret == 1) |
| 1261 | return i; |
| 1262 | } |
| 1263 | |
| 1264 | return ret; |
| 1265 | } |
| 1266 | |
| 1267 | static int kbd_get_valid_token_counts(void) |
| 1268 | { |
| 1269 | return hweight16(kbd_token_bits); |
| 1270 | } |
| 1271 | |
| 1272 | static inline int kbd_init_info(void) |
| 1273 | { |
| 1274 | struct kbd_state state; |
| 1275 | int ret; |
| 1276 | int i; |
| 1277 | |
| 1278 | ret = kbd_get_info(&kbd_info); |
| 1279 | if (ret) |
| 1280 | return ret; |
| 1281 | |
| 1282 | kbd_get_state(&state); |
| 1283 | |
| 1284 | /* NOTE: timeout value is stored in 6 bits so max value is 63 */ |
| 1285 | if (kbd_info.seconds > 63) |
| 1286 | kbd_info.seconds = 63; |
| 1287 | if (kbd_info.minutes > 63) |
| 1288 | kbd_info.minutes = 63; |
| 1289 | if (kbd_info.hours > 63) |
| 1290 | kbd_info.hours = 63; |
| 1291 | if (kbd_info.days > 63) |
| 1292 | kbd_info.days = 63; |
| 1293 | |
| 1294 | /* NOTE: On tested machines ON mode did not work and caused |
| 1295 | * problems (turned backlight off) so do not use it |
| 1296 | */ |
| 1297 | kbd_info.modes &= ~BIT(KBD_MODE_BIT_ON); |
| 1298 | |
| 1299 | kbd_previous_level = kbd_get_level(&state); |
| 1300 | kbd_previous_mode_bit = state.mode_bit; |
| 1301 | |
| 1302 | if (kbd_previous_level == 0 && kbd_get_max_level() != 0) |
| 1303 | kbd_previous_level = 1; |
| 1304 | |
| 1305 | if (kbd_previous_mode_bit == KBD_MODE_BIT_OFF) { |
| 1306 | kbd_previous_mode_bit = |
| 1307 | ffs(kbd_info.modes & ~BIT(KBD_MODE_BIT_OFF)); |
| 1308 | if (kbd_previous_mode_bit != 0) |
| 1309 | kbd_previous_mode_bit--; |
| 1310 | } |
| 1311 | |
| 1312 | if (kbd_info.modes & (BIT(KBD_MODE_BIT_ALS) | |
| 1313 | BIT(KBD_MODE_BIT_TRIGGER_ALS))) |
| 1314 | kbd_als_supported = true; |
| 1315 | |
| 1316 | if (kbd_info.modes & ( |
| 1317 | BIT(KBD_MODE_BIT_TRIGGER_ALS) | BIT(KBD_MODE_BIT_TRIGGER) | |
| 1318 | BIT(KBD_MODE_BIT_TRIGGER_25) | BIT(KBD_MODE_BIT_TRIGGER_50) | |
| 1319 | BIT(KBD_MODE_BIT_TRIGGER_75) | BIT(KBD_MODE_BIT_TRIGGER_100) |
| 1320 | )) |
| 1321 | kbd_triggers_supported = true; |
| 1322 | |
| 1323 | /* kbd_mode_levels[0] is reserved, see below */ |
| 1324 | for (i = 0; i < 16; ++i) |
| 1325 | if (kbd_is_level_mode_bit(i) && (BIT(i) & kbd_info.modes)) |
| 1326 | kbd_mode_levels[1 + kbd_mode_levels_count++] = i; |
| 1327 | |
| 1328 | /* |
| 1329 | * Find the first supported mode and assign to kbd_mode_levels[0]. |
| 1330 | * This should be 0 (off), but we cannot depend on the BIOS to |
| 1331 | * support 0. |
| 1332 | */ |
| 1333 | if (kbd_mode_levels_count > 0) { |
| 1334 | for (i = 0; i < 16; ++i) { |
| 1335 | if (BIT(i) & kbd_info.modes) { |
| 1336 | kbd_mode_levels[0] = i; |
| 1337 | break; |
| 1338 | } |
| 1339 | } |
| 1340 | kbd_mode_levels_count++; |
| 1341 | } |
| 1342 | |
| 1343 | return 0; |
| 1344 | |
| 1345 | } |
| 1346 | |
| 1347 | static inline void kbd_init_tokens(void) |
| 1348 | { |
| 1349 | int i; |
| 1350 | |
| 1351 | for (i = 0; i < ARRAY_SIZE(kbd_tokens); ++i) |
| 1352 | if (find_token_id(kbd_tokens[i]) != -1) |
| 1353 | kbd_token_bits |= BIT(i); |
| 1354 | } |
| 1355 | |
| 1356 | static void kbd_init(void) |
| 1357 | { |
| 1358 | int ret; |
| 1359 | |
| 1360 | ret = kbd_init_info(); |
| 1361 | kbd_init_tokens(); |
| 1362 | |
| 1363 | if (kbd_token_bits != 0 || ret == 0) |
| 1364 | kbd_led_present = true; |
| 1365 | } |
| 1366 | |
| 1367 | static ssize_t kbd_led_timeout_store(struct device *dev, |
| 1368 | struct device_attribute *attr, |
| 1369 | const char *buf, size_t count) |
| 1370 | { |
| 1371 | struct kbd_state new_state; |
| 1372 | struct kbd_state state; |
| 1373 | bool convert; |
| 1374 | int value; |
| 1375 | int ret; |
| 1376 | char ch; |
| 1377 | u8 unit; |
| 1378 | int i; |
| 1379 | |
| 1380 | ret = sscanf(buf, "%d %c", &value, &ch); |
| 1381 | if (ret < 1) |
| 1382 | return -EINVAL; |
| 1383 | else if (ret == 1) |
| 1384 | ch = 's'; |
| 1385 | |
| 1386 | if (value < 0) |
| 1387 | return -EINVAL; |
| 1388 | |
| 1389 | convert = false; |
| 1390 | |
| 1391 | switch (ch) { |
| 1392 | case 's': |
| 1393 | if (value > kbd_info.seconds) |
| 1394 | convert = true; |
| 1395 | unit = KBD_TIMEOUT_SECONDS; |
| 1396 | break; |
| 1397 | case 'm': |
| 1398 | if (value > kbd_info.minutes) |
| 1399 | convert = true; |
| 1400 | unit = KBD_TIMEOUT_MINUTES; |
| 1401 | break; |
| 1402 | case 'h': |
| 1403 | if (value > kbd_info.hours) |
| 1404 | convert = true; |
| 1405 | unit = KBD_TIMEOUT_HOURS; |
| 1406 | break; |
| 1407 | case 'd': |
| 1408 | if (value > kbd_info.days) |
| 1409 | convert = true; |
| 1410 | unit = KBD_TIMEOUT_DAYS; |
| 1411 | break; |
| 1412 | default: |
| 1413 | return -EINVAL; |
| 1414 | } |
| 1415 | |
| 1416 | if (quirks && quirks->needs_kbd_timeouts) |
| 1417 | convert = true; |
| 1418 | |
| 1419 | if (convert) { |
| 1420 | /* Convert value from current units to seconds */ |
| 1421 | switch (unit) { |
| 1422 | case KBD_TIMEOUT_DAYS: |
| 1423 | value *= 24; |
| 1424 | case KBD_TIMEOUT_HOURS: |
| 1425 | value *= 60; |
| 1426 | case KBD_TIMEOUT_MINUTES: |
| 1427 | value *= 60; |
| 1428 | unit = KBD_TIMEOUT_SECONDS; |
| 1429 | } |
| 1430 | |
| 1431 | if (quirks && quirks->needs_kbd_timeouts) { |
| 1432 | for (i = 0; quirks->kbd_timeouts[i] != -1; i++) { |
| 1433 | if (value <= quirks->kbd_timeouts[i]) { |
| 1434 | value = quirks->kbd_timeouts[i]; |
| 1435 | break; |
| 1436 | } |
| 1437 | } |
| 1438 | } |
| 1439 | |
| 1440 | if (value <= kbd_info.seconds && kbd_info.seconds) { |
| 1441 | unit = KBD_TIMEOUT_SECONDS; |
| 1442 | } else if (value / 60 <= kbd_info.minutes && kbd_info.minutes) { |
| 1443 | value /= 60; |
| 1444 | unit = KBD_TIMEOUT_MINUTES; |
| 1445 | } else if (value / (60 * 60) <= kbd_info.hours && kbd_info.hours) { |
| 1446 | value /= (60 * 60); |
| 1447 | unit = KBD_TIMEOUT_HOURS; |
| 1448 | } else if (value / (60 * 60 * 24) <= kbd_info.days && kbd_info.days) { |
| 1449 | value /= (60 * 60 * 24); |
| 1450 | unit = KBD_TIMEOUT_DAYS; |
| 1451 | } else { |
| 1452 | return -EINVAL; |
| 1453 | } |
| 1454 | } |
| 1455 | |
| 1456 | ret = kbd_get_state(&state); |
| 1457 | if (ret) |
| 1458 | return ret; |
| 1459 | |
| 1460 | new_state = state; |
| 1461 | new_state.timeout_value = value; |
| 1462 | new_state.timeout_unit = unit; |
| 1463 | |
| 1464 | ret = kbd_set_state_safe(&new_state, &state); |
| 1465 | if (ret) |
| 1466 | return ret; |
| 1467 | |
| 1468 | return count; |
| 1469 | } |
| 1470 | |
| 1471 | static ssize_t kbd_led_timeout_show(struct device *dev, |
| 1472 | struct device_attribute *attr, char *buf) |
| 1473 | { |
| 1474 | struct kbd_state state; |
| 1475 | int ret; |
| 1476 | int len; |
| 1477 | |
| 1478 | ret = kbd_get_state(&state); |
| 1479 | if (ret) |
| 1480 | return ret; |
| 1481 | |
| 1482 | len = sprintf(buf, "%d", state.timeout_value); |
| 1483 | |
| 1484 | switch (state.timeout_unit) { |
| 1485 | case KBD_TIMEOUT_SECONDS: |
| 1486 | return len + sprintf(buf+len, "s\n"); |
| 1487 | case KBD_TIMEOUT_MINUTES: |
| 1488 | return len + sprintf(buf+len, "m\n"); |
| 1489 | case KBD_TIMEOUT_HOURS: |
| 1490 | return len + sprintf(buf+len, "h\n"); |
| 1491 | case KBD_TIMEOUT_DAYS: |
| 1492 | return len + sprintf(buf+len, "d\n"); |
| 1493 | default: |
| 1494 | return -EINVAL; |
| 1495 | } |
| 1496 | |
| 1497 | return len; |
| 1498 | } |
| 1499 | |
| 1500 | static DEVICE_ATTR(stop_timeout, S_IRUGO | S_IWUSR, |
| 1501 | kbd_led_timeout_show, kbd_led_timeout_store); |
| 1502 | |
| 1503 | static const char * const kbd_led_triggers[] = { |
| 1504 | "keyboard", |
| 1505 | "touchpad", |
| 1506 | /*"trackstick"*/ NULL, /* NOTE: trackstick is just alias for touchpad */ |
| 1507 | "mouse", |
| 1508 | }; |
| 1509 | |
| 1510 | static ssize_t kbd_led_triggers_store(struct device *dev, |
| 1511 | struct device_attribute *attr, |
| 1512 | const char *buf, size_t count) |
| 1513 | { |
| 1514 | struct kbd_state new_state; |
| 1515 | struct kbd_state state; |
| 1516 | bool triggers_enabled = false; |
| 1517 | bool als_enabled = false; |
| 1518 | bool disable_als = false; |
| 1519 | bool enable_als = false; |
| 1520 | int trigger_bit = -1; |
| 1521 | char trigger[21]; |
| 1522 | int i, ret; |
| 1523 | |
| 1524 | ret = sscanf(buf, "%20s", trigger); |
| 1525 | if (ret != 1) |
| 1526 | return -EINVAL; |
| 1527 | |
| 1528 | if (trigger[0] != '+' && trigger[0] != '-') |
| 1529 | return -EINVAL; |
| 1530 | |
| 1531 | ret = kbd_get_state(&state); |
| 1532 | if (ret) |
| 1533 | return ret; |
| 1534 | |
| 1535 | if (kbd_als_supported) |
| 1536 | als_enabled = kbd_is_als_mode_bit(state.mode_bit); |
| 1537 | |
| 1538 | if (kbd_triggers_supported) |
| 1539 | triggers_enabled = kbd_is_trigger_mode_bit(state.mode_bit); |
| 1540 | |
| 1541 | if (kbd_als_supported) { |
| 1542 | if (strcmp(trigger, "+als") == 0) { |
| 1543 | if (als_enabled) |
| 1544 | return count; |
| 1545 | enable_als = true; |
| 1546 | } else if (strcmp(trigger, "-als") == 0) { |
| 1547 | if (!als_enabled) |
| 1548 | return count; |
| 1549 | disable_als = true; |
| 1550 | } |
| 1551 | } |
| 1552 | |
| 1553 | if (enable_als || disable_als) { |
| 1554 | new_state = state; |
| 1555 | if (enable_als) { |
| 1556 | if (triggers_enabled) |
| 1557 | new_state.mode_bit = KBD_MODE_BIT_TRIGGER_ALS; |
| 1558 | else |
| 1559 | new_state.mode_bit = KBD_MODE_BIT_ALS; |
| 1560 | } else { |
| 1561 | if (triggers_enabled) { |
| 1562 | new_state.mode_bit = KBD_MODE_BIT_TRIGGER; |
| 1563 | kbd_set_level(&new_state, kbd_previous_level); |
| 1564 | } else { |
| 1565 | new_state.mode_bit = KBD_MODE_BIT_ON; |
| 1566 | } |
| 1567 | } |
| 1568 | if (!(kbd_info.modes & BIT(new_state.mode_bit))) |
| 1569 | return -EINVAL; |
| 1570 | ret = kbd_set_state_safe(&new_state, &state); |
| 1571 | if (ret) |
| 1572 | return ret; |
| 1573 | kbd_previous_mode_bit = new_state.mode_bit; |
| 1574 | return count; |
| 1575 | } |
| 1576 | |
| 1577 | if (kbd_triggers_supported) { |
| 1578 | for (i = 0; i < ARRAY_SIZE(kbd_led_triggers); ++i) { |
| 1579 | if (!(kbd_info.triggers & BIT(i))) |
| 1580 | continue; |
| 1581 | if (!kbd_led_triggers[i]) |
| 1582 | continue; |
| 1583 | if (strcmp(trigger+1, kbd_led_triggers[i]) != 0) |
| 1584 | continue; |
| 1585 | if (trigger[0] == '+' && |
| 1586 | triggers_enabled && (state.triggers & BIT(i))) |
| 1587 | return count; |
| 1588 | if (trigger[0] == '-' && |
| 1589 | (!triggers_enabled || !(state.triggers & BIT(i)))) |
| 1590 | return count; |
| 1591 | trigger_bit = i; |
| 1592 | break; |
| 1593 | } |
| 1594 | } |
| 1595 | |
| 1596 | if (trigger_bit != -1) { |
| 1597 | new_state = state; |
| 1598 | if (trigger[0] == '+') |
| 1599 | new_state.triggers |= BIT(trigger_bit); |
| 1600 | else { |
| 1601 | new_state.triggers &= ~BIT(trigger_bit); |
| 1602 | /* NOTE: trackstick bit (2) must be disabled when |
| 1603 | * disabling touchpad bit (1), otherwise touchpad |
| 1604 | * bit (1) will not be disabled */ |
| 1605 | if (trigger_bit == 1) |
| 1606 | new_state.triggers &= ~BIT(2); |
| 1607 | } |
| 1608 | if ((kbd_info.triggers & new_state.triggers) != |
| 1609 | new_state.triggers) |
| 1610 | return -EINVAL; |
| 1611 | if (new_state.triggers && !triggers_enabled) { |
| 1612 | if (als_enabled) |
| 1613 | new_state.mode_bit = KBD_MODE_BIT_TRIGGER_ALS; |
| 1614 | else { |
| 1615 | new_state.mode_bit = KBD_MODE_BIT_TRIGGER; |
| 1616 | kbd_set_level(&new_state, kbd_previous_level); |
| 1617 | } |
| 1618 | } else if (new_state.triggers == 0) { |
| 1619 | if (als_enabled) |
| 1620 | new_state.mode_bit = KBD_MODE_BIT_ALS; |
| 1621 | else |
| 1622 | kbd_set_level(&new_state, 0); |
| 1623 | } |
| 1624 | if (!(kbd_info.modes & BIT(new_state.mode_bit))) |
| 1625 | return -EINVAL; |
| 1626 | ret = kbd_set_state_safe(&new_state, &state); |
| 1627 | if (ret) |
| 1628 | return ret; |
| 1629 | if (new_state.mode_bit != KBD_MODE_BIT_OFF) |
| 1630 | kbd_previous_mode_bit = new_state.mode_bit; |
| 1631 | return count; |
| 1632 | } |
| 1633 | |
| 1634 | return -EINVAL; |
| 1635 | } |
| 1636 | |
| 1637 | static ssize_t kbd_led_triggers_show(struct device *dev, |
| 1638 | struct device_attribute *attr, char *buf) |
| 1639 | { |
| 1640 | struct kbd_state state; |
| 1641 | bool triggers_enabled; |
| 1642 | int level, i, ret; |
| 1643 | int len = 0; |
| 1644 | |
| 1645 | ret = kbd_get_state(&state); |
| 1646 | if (ret) |
| 1647 | return ret; |
| 1648 | |
| 1649 | len = 0; |
| 1650 | |
| 1651 | if (kbd_triggers_supported) { |
| 1652 | triggers_enabled = kbd_is_trigger_mode_bit(state.mode_bit); |
| 1653 | level = kbd_get_level(&state); |
| 1654 | for (i = 0; i < ARRAY_SIZE(kbd_led_triggers); ++i) { |
| 1655 | if (!(kbd_info.triggers & BIT(i))) |
| 1656 | continue; |
| 1657 | if (!kbd_led_triggers[i]) |
| 1658 | continue; |
| 1659 | if ((triggers_enabled || level <= 0) && |
| 1660 | (state.triggers & BIT(i))) |
| 1661 | buf[len++] = '+'; |
| 1662 | else |
| 1663 | buf[len++] = '-'; |
| 1664 | len += sprintf(buf+len, "%s ", kbd_led_triggers[i]); |
| 1665 | } |
| 1666 | } |
| 1667 | |
| 1668 | if (kbd_als_supported) { |
| 1669 | if (kbd_is_als_mode_bit(state.mode_bit)) |
| 1670 | len += sprintf(buf+len, "+als "); |
| 1671 | else |
| 1672 | len += sprintf(buf+len, "-als "); |
| 1673 | } |
| 1674 | |
| 1675 | if (len) |
| 1676 | buf[len - 1] = '\n'; |
| 1677 | |
| 1678 | return len; |
| 1679 | } |
| 1680 | |
| 1681 | static DEVICE_ATTR(start_triggers, S_IRUGO | S_IWUSR, |
| 1682 | kbd_led_triggers_show, kbd_led_triggers_store); |
| 1683 | |
| 1684 | static ssize_t kbd_led_als_store(struct device *dev, |
| 1685 | struct device_attribute *attr, |
| 1686 | const char *buf, size_t count) |
| 1687 | { |
| 1688 | struct kbd_state state; |
| 1689 | struct kbd_state new_state; |
| 1690 | u8 setting; |
| 1691 | int ret; |
| 1692 | |
| 1693 | ret = kstrtou8(buf, 10, &setting); |
| 1694 | if (ret) |
| 1695 | return ret; |
| 1696 | |
| 1697 | ret = kbd_get_state(&state); |
| 1698 | if (ret) |
| 1699 | return ret; |
| 1700 | |
| 1701 | new_state = state; |
| 1702 | new_state.als_setting = setting; |
| 1703 | |
| 1704 | ret = kbd_set_state_safe(&new_state, &state); |
| 1705 | if (ret) |
| 1706 | return ret; |
| 1707 | |
| 1708 | return count; |
| 1709 | } |
| 1710 | |
| 1711 | static ssize_t kbd_led_als_show(struct device *dev, |
| 1712 | struct device_attribute *attr, char *buf) |
| 1713 | { |
| 1714 | struct kbd_state state; |
| 1715 | int ret; |
| 1716 | |
| 1717 | ret = kbd_get_state(&state); |
| 1718 | if (ret) |
| 1719 | return ret; |
| 1720 | |
| 1721 | return sprintf(buf, "%d\n", state.als_setting); |
| 1722 | } |
| 1723 | |
| 1724 | static DEVICE_ATTR(als_setting, S_IRUGO | S_IWUSR, |
| 1725 | kbd_led_als_show, kbd_led_als_store); |
| 1726 | |
| 1727 | static struct attribute *kbd_led_attrs[] = { |
| 1728 | &dev_attr_stop_timeout.attr, |
| 1729 | &dev_attr_start_triggers.attr, |
| 1730 | &dev_attr_als_setting.attr, |
| 1731 | NULL, |
| 1732 | }; |
| 1733 | ATTRIBUTE_GROUPS(kbd_led); |
| 1734 | |
| 1735 | static enum led_brightness kbd_led_level_get(struct led_classdev *led_cdev) |
| 1736 | { |
| 1737 | int ret; |
| 1738 | u16 num; |
| 1739 | struct kbd_state state; |
| 1740 | |
| 1741 | if (kbd_get_max_level()) { |
| 1742 | ret = kbd_get_state(&state); |
| 1743 | if (ret) |
| 1744 | return 0; |
| 1745 | ret = kbd_get_level(&state); |
| 1746 | if (ret < 0) |
| 1747 | return 0; |
| 1748 | return ret; |
| 1749 | } |
| 1750 | |
| 1751 | if (kbd_get_valid_token_counts()) { |
| 1752 | ret = kbd_get_first_active_token_bit(); |
| 1753 | if (ret < 0) |
| 1754 | return 0; |
| 1755 | for (num = kbd_token_bits; num != 0 && ret > 0; --ret) |
| 1756 | num &= num - 1; /* clear the first bit set */ |
| 1757 | if (num == 0) |
| 1758 | return 0; |
| 1759 | return ffs(num) - 1; |
| 1760 | } |
| 1761 | |
| 1762 | pr_warn("Keyboard brightness level control not supported\n"); |
| 1763 | return 0; |
| 1764 | } |
| 1765 | |
| 1766 | static void kbd_led_level_set(struct led_classdev *led_cdev, |
| 1767 | enum led_brightness value) |
| 1768 | { |
| 1769 | struct kbd_state state; |
| 1770 | struct kbd_state new_state; |
| 1771 | u16 num; |
| 1772 | |
| 1773 | if (kbd_get_max_level()) { |
| 1774 | if (kbd_get_state(&state)) |
| 1775 | return; |
| 1776 | new_state = state; |
| 1777 | if (kbd_set_level(&new_state, value)) |
| 1778 | return; |
| 1779 | kbd_set_state_safe(&new_state, &state); |
| 1780 | return; |
| 1781 | } |
| 1782 | |
| 1783 | if (kbd_get_valid_token_counts()) { |
| 1784 | for (num = kbd_token_bits; num != 0 && value > 0; --value) |
| 1785 | num &= num - 1; /* clear the first bit set */ |
| 1786 | if (num == 0) |
| 1787 | return; |
| 1788 | kbd_set_token_bit(ffs(num) - 1); |
| 1789 | return; |
| 1790 | } |
| 1791 | |
| 1792 | pr_warn("Keyboard brightness level control not supported\n"); |
| 1793 | } |
| 1794 | |
| 1795 | static struct led_classdev kbd_led = { |
| 1796 | .name = "dell::kbd_backlight", |
| 1797 | .brightness_set = kbd_led_level_set, |
| 1798 | .brightness_get = kbd_led_level_get, |
| 1799 | .groups = kbd_led_groups, |
| 1800 | }; |
| 1801 | |
| 1802 | static int __init kbd_led_init(struct device *dev) |
| 1803 | { |
| 1804 | kbd_init(); |
| 1805 | if (!kbd_led_present) |
| 1806 | return -ENODEV; |
| 1807 | kbd_led.max_brightness = kbd_get_max_level(); |
| 1808 | if (!kbd_led.max_brightness) { |
| 1809 | kbd_led.max_brightness = kbd_get_valid_token_counts(); |
| 1810 | if (kbd_led.max_brightness) |
| 1811 | kbd_led.max_brightness--; |
| 1812 | } |
| 1813 | return led_classdev_register(dev, &kbd_led); |
| 1814 | } |
| 1815 | |
| 1816 | static void brightness_set_exit(struct led_classdev *led_cdev, |
| 1817 | enum led_brightness value) |
| 1818 | { |
| 1819 | /* Don't change backlight level on exit */ |
| 1820 | }; |
| 1821 | |
| 1822 | static void kbd_led_exit(void) |
| 1823 | { |
| 1824 | if (!kbd_led_present) |
| 1825 | return; |
| 1826 | kbd_led.brightness_set = brightness_set_exit; |
| 1827 | led_classdev_unregister(&kbd_led); |
| 1828 | } |
| 1829 | |
Matthew Garrett | ad8f07c | 2009-01-07 18:08:56 -0800 | [diff] [blame] | 1830 | static int __init dell_init(void) |
| 1831 | { |
Matthew Garrett | ad8f07c | 2009-01-07 18:08:56 -0800 | [diff] [blame] | 1832 | int max_intensity = 0; |
| 1833 | int ret; |
| 1834 | |
| 1835 | if (!dmi_check_system(dell_device_table)) |
| 1836 | return -ENODEV; |
| 1837 | |
AceLan Kao | 2d8b90b | 2011-10-04 16:25:44 +0800 | [diff] [blame] | 1838 | quirks = NULL; |
| 1839 | /* find if this machine support other functions */ |
| 1840 | dmi_check_system(dell_quirks); |
| 1841 | |
Jean Delvare | e7a19c56 | 2009-03-30 21:46:44 +0200 | [diff] [blame] | 1842 | dmi_walk(find_tokens, NULL); |
Matthew Garrett | ad8f07c | 2009-01-07 18:08:56 -0800 | [diff] [blame] | 1843 | |
| 1844 | if (!da_tokens) { |
Joe Perches | eb88952 | 2011-03-29 15:21:37 -0700 | [diff] [blame] | 1845 | pr_info("Unable to find dmi tokens\n"); |
Matthew Garrett | ad8f07c | 2009-01-07 18:08:56 -0800 | [diff] [blame] | 1846 | return -ENODEV; |
| 1847 | } |
| 1848 | |
Alan Jenkins | ada3248 | 2009-08-19 15:06:49 +0100 | [diff] [blame] | 1849 | ret = platform_driver_register(&platform_driver); |
| 1850 | if (ret) |
| 1851 | goto fail_platform_driver; |
| 1852 | platform_device = platform_device_alloc("dell-laptop", -1); |
| 1853 | if (!platform_device) { |
| 1854 | ret = -ENOMEM; |
| 1855 | goto fail_platform_device1; |
| 1856 | } |
| 1857 | ret = platform_device_add(platform_device); |
| 1858 | if (ret) |
| 1859 | goto fail_platform_device2; |
| 1860 | |
Stuart Hayes | 116ee77 | 2010-02-10 14:12:13 -0500 | [diff] [blame] | 1861 | /* |
| 1862 | * Allocate buffer below 4GB for SMI data--only 32-bit physical addr |
| 1863 | * is passed to SMI handler. |
| 1864 | */ |
| 1865 | bufferpage = alloc_page(GFP_KERNEL | GFP_DMA32); |
Wei Yongjun | 9f20820 | 2013-05-09 10:03:02 +0800 | [diff] [blame] | 1866 | if (!bufferpage) { |
| 1867 | ret = -ENOMEM; |
Stuart Hayes | 116ee77 | 2010-02-10 14:12:13 -0500 | [diff] [blame] | 1868 | goto fail_buffer; |
Wei Yongjun | 9f20820 | 2013-05-09 10:03:02 +0800 | [diff] [blame] | 1869 | } |
Stuart Hayes | 116ee77 | 2010-02-10 14:12:13 -0500 | [diff] [blame] | 1870 | buffer = page_address(bufferpage); |
Stuart Hayes | 116ee77 | 2010-02-10 14:12:13 -0500 | [diff] [blame] | 1871 | |
Hans de Goede | 4cc8a57 | 2013-11-17 14:00:16 +0100 | [diff] [blame] | 1872 | ret = dell_setup_rfkill(); |
| 1873 | |
| 1874 | if (ret) { |
| 1875 | pr_warn("Unable to setup rfkill\n"); |
| 1876 | goto fail_rfkill; |
| 1877 | } |
| 1878 | |
AceLan Kao | 2d8b90b | 2011-10-04 16:25:44 +0800 | [diff] [blame] | 1879 | if (quirks && quirks->touchpad_led) |
| 1880 | touchpad_led_init(&platform_device->dev); |
| 1881 | |
Pali Rohár | 02b2aaa | 2014-12-05 12:53:31 +0100 | [diff] [blame] | 1882 | kbd_led_init(&platform_device->dev); |
| 1883 | |
Keng-Yu Lin | 037accf | 2010-09-28 11:43:31 +0800 | [diff] [blame] | 1884 | dell_laptop_dir = debugfs_create_dir("dell_laptop", NULL); |
Hans de Goede | 4cc8a57 | 2013-11-17 14:00:16 +0100 | [diff] [blame] | 1885 | if (dell_laptop_dir != NULL) |
| 1886 | debugfs_create_file("rfkill", 0444, dell_laptop_dir, NULL, |
| 1887 | &dell_debugfs_fops); |
Keng-Yu Lin | 037accf | 2010-09-28 11:43:31 +0800 | [diff] [blame] | 1888 | |
Matthew Garrett | ad8f07c | 2009-01-07 18:08:56 -0800 | [diff] [blame] | 1889 | #ifdef CONFIG_ACPI |
| 1890 | /* In the event of an ACPI backlight being available, don't |
| 1891 | * register the platform controller. |
| 1892 | */ |
| 1893 | if (acpi_video_backlight_support()) |
| 1894 | return 0; |
| 1895 | #endif |
| 1896 | |
Stuart Hayes | 116ee77 | 2010-02-10 14:12:13 -0500 | [diff] [blame] | 1897 | get_buffer(); |
| 1898 | buffer->input[0] = find_token_location(BRIGHTNESS_TOKEN); |
| 1899 | if (buffer->input[0] != -1) { |
| 1900 | dell_send_request(buffer, 0, 2); |
| 1901 | max_intensity = buffer->output[3]; |
Matthew Garrett | ad8f07c | 2009-01-07 18:08:56 -0800 | [diff] [blame] | 1902 | } |
Stuart Hayes | 116ee77 | 2010-02-10 14:12:13 -0500 | [diff] [blame] | 1903 | release_buffer(); |
Matthew Garrett | ad8f07c | 2009-01-07 18:08:56 -0800 | [diff] [blame] | 1904 | |
| 1905 | if (max_intensity) { |
Matthew Garrett | a19a6ee | 2010-02-17 16:39:44 -0500 | [diff] [blame] | 1906 | struct backlight_properties props; |
| 1907 | memset(&props, 0, sizeof(struct backlight_properties)); |
Matthew Garrett | bb7ca74 | 2011-03-22 16:30:21 -0700 | [diff] [blame] | 1908 | props.type = BACKLIGHT_PLATFORM; |
Matthew Garrett | a19a6ee | 2010-02-17 16:39:44 -0500 | [diff] [blame] | 1909 | props.max_brightness = max_intensity; |
| 1910 | dell_backlight_device = backlight_device_register("dell_backlight", |
| 1911 | &platform_device->dev, |
| 1912 | NULL, |
| 1913 | &dell_ops, |
| 1914 | &props); |
Matthew Garrett | ad8f07c | 2009-01-07 18:08:56 -0800 | [diff] [blame] | 1915 | |
| 1916 | if (IS_ERR(dell_backlight_device)) { |
| 1917 | ret = PTR_ERR(dell_backlight_device); |
| 1918 | dell_backlight_device = NULL; |
Alan Jenkins | 71e9dc7 | 2009-08-19 15:06:47 +0100 | [diff] [blame] | 1919 | goto fail_backlight; |
Matthew Garrett | ad8f07c | 2009-01-07 18:08:56 -0800 | [diff] [blame] | 1920 | } |
| 1921 | |
Matthew Garrett | ad8f07c | 2009-01-07 18:08:56 -0800 | [diff] [blame] | 1922 | dell_backlight_device->props.brightness = |
| 1923 | dell_get_intensity(dell_backlight_device); |
| 1924 | backlight_update_status(dell_backlight_device); |
| 1925 | } |
| 1926 | |
| 1927 | return 0; |
Alan Jenkins | 71e9dc7 | 2009-08-19 15:06:47 +0100 | [diff] [blame] | 1928 | |
| 1929 | fail_backlight: |
Hans de Goede | 4cc8a57 | 2013-11-17 14:00:16 +0100 | [diff] [blame] | 1930 | i8042_remove_filter(dell_laptop_i8042_filter); |
| 1931 | cancel_delayed_work_sync(&dell_rfkill_work); |
Hans de Goede | 4cc8a57 | 2013-11-17 14:00:16 +0100 | [diff] [blame] | 1932 | dell_cleanup_rfkill(); |
| 1933 | fail_rfkill: |
Stuart Hayes | 116ee77 | 2010-02-10 14:12:13 -0500 | [diff] [blame] | 1934 | free_page((unsigned long)bufferpage); |
| 1935 | fail_buffer: |
Alan Jenkins | ada3248 | 2009-08-19 15:06:49 +0100 | [diff] [blame] | 1936 | platform_device_del(platform_device); |
| 1937 | fail_platform_device2: |
| 1938 | platform_device_put(platform_device); |
| 1939 | fail_platform_device1: |
| 1940 | platform_driver_unregister(&platform_driver); |
| 1941 | fail_platform_driver: |
Matthew Garrett | ad8f07c | 2009-01-07 18:08:56 -0800 | [diff] [blame] | 1942 | kfree(da_tokens); |
| 1943 | return ret; |
| 1944 | } |
| 1945 | |
| 1946 | static void __exit dell_exit(void) |
| 1947 | { |
Keng-Yu Lin | 037accf | 2010-09-28 11:43:31 +0800 | [diff] [blame] | 1948 | debugfs_remove_recursive(dell_laptop_dir); |
AceLan Kao | 2d8b90b | 2011-10-04 16:25:44 +0800 | [diff] [blame] | 1949 | if (quirks && quirks->touchpad_led) |
| 1950 | touchpad_led_exit(); |
Pali Rohár | 02b2aaa | 2014-12-05 12:53:31 +0100 | [diff] [blame] | 1951 | kbd_led_exit(); |
Hans de Goede | 4cc8a57 | 2013-11-17 14:00:16 +0100 | [diff] [blame] | 1952 | i8042_remove_filter(dell_laptop_i8042_filter); |
| 1953 | cancel_delayed_work_sync(&dell_rfkill_work); |
Matthew Garrett | ad8f07c | 2009-01-07 18:08:56 -0800 | [diff] [blame] | 1954 | backlight_device_unregister(dell_backlight_device); |
Hans de Goede | 4cc8a57 | 2013-11-17 14:00:16 +0100 | [diff] [blame] | 1955 | dell_cleanup_rfkill(); |
Matthew Garrett | facd61d | 2010-02-09 14:03:04 -0500 | [diff] [blame] | 1956 | if (platform_device) { |
Matthew Garrett | 92e00e4 | 2010-03-01 09:46:43 -0500 | [diff] [blame] | 1957 | platform_device_unregister(platform_device); |
Matthew Garrett | facd61d | 2010-02-09 14:03:04 -0500 | [diff] [blame] | 1958 | platform_driver_unregister(&platform_driver); |
| 1959 | } |
Matthew Garrett | e551260 | 2010-02-09 14:05:01 -0500 | [diff] [blame] | 1960 | kfree(da_tokens); |
Stuart Hayes | 116ee77 | 2010-02-10 14:12:13 -0500 | [diff] [blame] | 1961 | free_page((unsigned long)buffer); |
Matthew Garrett | ad8f07c | 2009-01-07 18:08:56 -0800 | [diff] [blame] | 1962 | } |
| 1963 | |
| 1964 | module_init(dell_init); |
| 1965 | module_exit(dell_exit); |
| 1966 | |
| 1967 | MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>"); |
Pali Rohár | 02b2aaa | 2014-12-05 12:53:31 +0100 | [diff] [blame] | 1968 | MODULE_AUTHOR("Gabriele Mazzotta <gabriele.mzt@gmail.com>"); |
| 1969 | MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>"); |
Matthew Garrett | ad8f07c | 2009-01-07 18:08:56 -0800 | [diff] [blame] | 1970 | MODULE_DESCRIPTION("Dell laptop driver"); |
| 1971 | MODULE_LICENSE("GPL"); |