blob: ab89103150b506c9f6503fe5adf5b84904017443 [file] [log] [blame]
Matthew Garrettad8f07c2009-01-07 18:08:56 -08001/*
2 * Driver for Dell laptop extras
3 *
4 * Copyright (c) Red Hat <mjg@redhat.com>
Gabriele Mazzotta6cff8d62015-02-19 11:58:29 +01005 * Copyright (c) 2014 Gabriele Mazzotta <gabriele.mzt@gmail.com>
6 * Copyright (c) 2014 Pali Rohár <pali.rohar@gmail.com>
Matthew Garrettad8f07c2009-01-07 18:08:56 -08007 *
Gabriele Mazzotta6cff8d62015-02-19 11:58:29 +01008 * Based on documentation in the libsmbios package:
9 * Copyright (C) 2005-2014 Dell Inc.
Matthew Garrettad8f07c2009-01-07 18:08:56 -080010 *
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 Percheseb889522011-03-29 15:21:37 -070016#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17
Matthew Garrettad8f07c2009-01-07 18:08:56 -080018#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 Goede4cc8a572013-11-17 14:00:16 +010026#include <linux/rfkill.h>
Matthew Garrettad8f07c2009-01-07 18:08:56 -080027#include <linux/power_supply.h>
28#include <linux/acpi.h>
Stuart Hayes116ee772010-02-10 14:12:13 -050029#include <linux/mm.h>
Matthew Garrett814cb8a2009-12-09 18:23:36 +000030#include <linux/i8042.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.h>
Keng-Yu Lin037accf2010-09-28 11:43:31 +080032#include <linux/debugfs.h>
33#include <linux/seq_file.h>
Len Browncad73122009-01-09 17:23:38 -050034#include "../../firmware/dcdbas.h"
Pali Rohárf8358572015-06-06 10:23:30 +020035#include "dell-rbtn.h"
Matthew Garrettad8f07c2009-01-07 18:08:56 -080036
37#define BRIGHTNESS_TOKEN 0x7d
Gabriele Mazzotta6cff8d62015-02-19 11:58:29 +010038#define KBD_LED_OFF_TOKEN 0x01E1
39#define KBD_LED_ON_TOKEN 0x01E2
40#define KBD_LED_AUTO_TOKEN 0x01E3
41#define KBD_LED_AUTO_25_TOKEN 0x02EA
42#define KBD_LED_AUTO_50_TOKEN 0x02EB
43#define KBD_LED_AUTO_75_TOKEN 0x02EC
44#define KBD_LED_AUTO_100_TOKEN 0x02F6
Matthew Garrettad8f07c2009-01-07 18:08:56 -080045
46/* This structure will be modified by the firmware when we enter
47 * system management mode, hence the volatiles */
48
49struct calling_interface_buffer {
50 u16 class;
51 u16 select;
52 volatile u32 input[4];
53 volatile u32 output[4];
54} __packed;
55
56struct calling_interface_token {
57 u16 tokenID;
58 u16 location;
59 union {
60 u16 value;
61 u16 stringlength;
62 };
63};
64
65struct calling_interface_structure {
66 struct dmi_header header;
67 u16 cmdIOAddress;
68 u8 cmdIOCode;
69 u32 supportedCmds;
70 struct calling_interface_token tokens[];
71} __packed;
72
AceLan Kao2d8b90b2011-10-04 16:25:44 +080073struct quirk_entry {
74 u8 touchpad_led;
Gabriele Mazzotta6cff8d62015-02-19 11:58:29 +010075
76 int needs_kbd_timeouts;
77 /*
78 * Ordered list of timeouts expressed in seconds.
79 * The list must end with -1
80 */
81 int kbd_timeouts[];
AceLan Kao2d8b90b2011-10-04 16:25:44 +080082};
83
84static struct quirk_entry *quirks;
85
86static struct quirk_entry quirk_dell_vostro_v130 = {
87 .touchpad_led = 1,
88};
89
Mathias Krause681480c2014-07-16 19:43:09 +020090static int __init dmi_matched(const struct dmi_system_id *dmi)
AceLan Kao2d8b90b2011-10-04 16:25:44 +080091{
92 quirks = dmi->driver_data;
93 return 1;
94}
95
Gabriele Mazzotta6cff8d62015-02-19 11:58:29 +010096/*
97 * These values come from Windows utility provided by Dell. If any other value
98 * is used then BIOS silently set timeout to 0 without any error message.
99 */
100static struct quirk_entry quirk_dell_xps13_9333 = {
101 .needs_kbd_timeouts = 1,
102 .kbd_timeouts = { 0, 5, 15, 60, 5 * 60, 15 * 60, -1 },
103};
104
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800105static int da_command_address;
106static int da_command_code;
107static int da_num_tokens;
108static struct calling_interface_token *da_tokens;
109
Alan Jenkinsada32482009-08-19 15:06:49 +0100110static struct platform_driver platform_driver = {
111 .driver = {
112 .name = "dell-laptop",
Alan Jenkinsada32482009-08-19 15:06:49 +0100113 }
114};
115
116static struct platform_device *platform_device;
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800117static struct backlight_device *dell_backlight_device;
Hans de Goede4cc8a572013-11-17 14:00:16 +0100118static struct rfkill *wifi_rfkill;
119static struct rfkill *bluetooth_rfkill;
120static struct rfkill *wwan_rfkill;
Hans de Goede8e0e668d2013-11-17 14:00:26 +0100121static bool force_rfkill;
122
123module_param(force_rfkill, bool, 0444);
124MODULE_PARM_DESC(force_rfkill, "enable rfkill on non whitelisted models");
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800125
Uwe Kleine-König145047d2012-03-30 22:05:04 +0200126static const struct dmi_system_id dell_device_table[] __initconst = {
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800127 {
128 .ident = "Dell laptop",
129 .matches = {
130 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
131 DMI_MATCH(DMI_CHASSIS_TYPE, "8"),
132 },
133 },
Erik Andrencb6a7932010-02-14 11:53:23 -0500134 {
Rezwanul Kabir410d44c2010-06-23 12:02:43 -0500135 .matches = {
136 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
137 DMI_MATCH(DMI_CHASSIS_TYPE, "9"), /*Laptop*/
138 },
139 },
140 {
Erik Andrencb6a7932010-02-14 11:53:23 -0500141 .ident = "Dell Computer Corporation",
142 .matches = {
143 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
144 DMI_MATCH(DMI_CHASSIS_TYPE, "8"),
145 },
146 },
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800147 { }
148};
Dmitry Torokhov35ae64f2011-11-14 00:25:00 -0800149MODULE_DEVICE_TABLE(dmi, dell_device_table);
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800150
Mathias Krause681480c2014-07-16 19:43:09 +0200151static const struct dmi_system_id dell_quirks[] __initconst = {
AceLan Kao2d8b90b2011-10-04 16:25:44 +0800152 {
153 .callback = dmi_matched,
154 .ident = "Dell Vostro V130",
155 .matches = {
156 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
157 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro V130"),
158 },
159 .driver_data = &quirk_dell_vostro_v130,
160 },
161 {
162 .callback = dmi_matched,
163 .ident = "Dell Vostro V131",
164 .matches = {
165 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
166 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro V131"),
167 },
168 .driver_data = &quirk_dell_vostro_v130,
169 },
AceLan Kao2a748852011-11-17 15:30:42 +0800170 {
171 .callback = dmi_matched,
Ang Way Chuang57b31b22012-04-12 13:11:27 +0800172 .ident = "Dell Vostro 3350",
173 .matches = {
174 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
175 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3350"),
176 },
177 .driver_data = &quirk_dell_vostro_v130,
178 },
179 {
180 .callback = dmi_matched,
AceLan Kao2a748852011-11-17 15:30:42 +0800181 .ident = "Dell Vostro 3555",
182 .matches = {
183 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
184 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3555"),
185 },
186 .driver_data = &quirk_dell_vostro_v130,
187 },
188 {
189 .callback = dmi_matched,
190 .ident = "Dell Inspiron N311z",
191 .matches = {
192 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
193 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron N311z"),
194 },
195 .driver_data = &quirk_dell_vostro_v130,
196 },
197 {
198 .callback = dmi_matched,
199 .ident = "Dell Inspiron M5110",
200 .matches = {
201 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
202 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron M5110"),
203 },
204 .driver_data = &quirk_dell_vostro_v130,
205 },
AceLan Kao7f839222012-04-20 11:47:26 +0800206 {
207 .callback = dmi_matched,
208 .ident = "Dell Vostro 3360",
209 .matches = {
210 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
211 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3360"),
212 },
213 .driver_data = &quirk_dell_vostro_v130,
214 },
215 {
216 .callback = dmi_matched,
217 .ident = "Dell Vostro 3460",
218 .matches = {
219 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
220 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3460"),
221 },
222 .driver_data = &quirk_dell_vostro_v130,
223 },
224 {
225 .callback = dmi_matched,
226 .ident = "Dell Vostro 3560",
227 .matches = {
228 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
229 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3560"),
230 },
231 .driver_data = &quirk_dell_vostro_v130,
232 },
AceLan Kaod0e0a4772012-05-22 12:38:51 +0800233 {
234 .callback = dmi_matched,
235 .ident = "Dell Vostro 3450",
236 .matches = {
237 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
238 DMI_MATCH(DMI_PRODUCT_NAME, "Dell System Vostro 3450"),
239 },
240 .driver_data = &quirk_dell_vostro_v130,
241 },
AceLan Kao5f1e88f42012-07-13 16:39:57 +0800242 {
243 .callback = dmi_matched,
244 .ident = "Dell Inspiron 5420",
245 .matches = {
246 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
AceLan Kaoa2174ba2012-08-06 09:48:58 +0800247 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5420"),
AceLan Kao5f1e88f42012-07-13 16:39:57 +0800248 },
249 .driver_data = &quirk_dell_vostro_v130,
250 },
251 {
252 .callback = dmi_matched,
253 .ident = "Dell Inspiron 5520",
254 .matches = {
255 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
AceLan Kaoa2174ba2012-08-06 09:48:58 +0800256 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5520"),
AceLan Kao5f1e88f42012-07-13 16:39:57 +0800257 },
258 .driver_data = &quirk_dell_vostro_v130,
259 },
260 {
261 .callback = dmi_matched,
262 .ident = "Dell Inspiron 5720",
263 .matches = {
264 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
AceLan Kaoa2174ba2012-08-06 09:48:58 +0800265 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5720"),
AceLan Kao5f1e88f42012-07-13 16:39:57 +0800266 },
267 .driver_data = &quirk_dell_vostro_v130,
268 },
269 {
270 .callback = dmi_matched,
271 .ident = "Dell Inspiron 7420",
272 .matches = {
273 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
AceLan Kaoa2174ba2012-08-06 09:48:58 +0800274 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7420"),
AceLan Kao5f1e88f42012-07-13 16:39:57 +0800275 },
276 .driver_data = &quirk_dell_vostro_v130,
277 },
278 {
279 .callback = dmi_matched,
280 .ident = "Dell Inspiron 7520",
281 .matches = {
282 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
AceLan Kaoa2174ba2012-08-06 09:48:58 +0800283 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7520"),
AceLan Kao5f1e88f42012-07-13 16:39:57 +0800284 },
285 .driver_data = &quirk_dell_vostro_v130,
286 },
287 {
288 .callback = dmi_matched,
289 .ident = "Dell Inspiron 7720",
290 .matches = {
291 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
AceLan Kaoa2174ba2012-08-06 09:48:58 +0800292 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7720"),
AceLan Kao5f1e88f42012-07-13 16:39:57 +0800293 },
294 .driver_data = &quirk_dell_vostro_v130,
295 },
Gabriele Mazzotta6cff8d62015-02-19 11:58:29 +0100296 {
297 .callback = dmi_matched,
298 .ident = "Dell XPS13 9333",
299 .matches = {
300 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
301 DMI_MATCH(DMI_PRODUCT_NAME, "XPS13 9333"),
302 },
303 .driver_data = &quirk_dell_xps13_9333,
304 },
Martin Nyhusd62d4212012-03-15 18:25:48 +0100305 { }
AceLan Kao2d8b90b2011-10-04 16:25:44 +0800306};
307
Stuart Hayes116ee772010-02-10 14:12:13 -0500308static struct calling_interface_buffer *buffer;
Ingo Molnar94d8f782010-03-01 09:43:52 -0500309static struct page *bufferpage;
310static DEFINE_MUTEX(buffer_mutex);
Stuart Hayes116ee772010-02-10 14:12:13 -0500311
Matthew Garrettc6760ac2010-02-10 14:44:03 -0500312static int hwswitch_state;
313
Stuart Hayes116ee772010-02-10 14:12:13 -0500314static void get_buffer(void)
315{
316 mutex_lock(&buffer_mutex);
317 memset(buffer, 0, sizeof(struct calling_interface_buffer));
318}
319
320static void release_buffer(void)
321{
322 mutex_unlock(&buffer_mutex);
323}
324
Alan Jenkins4788df42009-08-19 15:06:50 +0100325static void __init parse_da_table(const struct dmi_header *dm)
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800326{
327 /* Final token is a terminator, so we don't want to copy it */
328 int tokens = (dm->length-11)/sizeof(struct calling_interface_token)-1;
David Woodhousefe9ab002013-03-14 13:21:00 +0000329 struct calling_interface_token *new_da_tokens;
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800330 struct calling_interface_structure *table =
331 container_of(dm, struct calling_interface_structure, header);
332
333 /* 4 bytes of table header, plus 7 bytes of Dell header, plus at least
334 6 bytes of entry */
335
336 if (dm->length < 17)
337 return;
338
339 da_command_address = table->cmdIOAddress;
340 da_command_code = table->cmdIOCode;
341
David Woodhousefe9ab002013-03-14 13:21:00 +0000342 new_da_tokens = krealloc(da_tokens, (da_num_tokens + tokens) *
343 sizeof(struct calling_interface_token),
344 GFP_KERNEL);
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800345
David Woodhousefe9ab002013-03-14 13:21:00 +0000346 if (!new_da_tokens)
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800347 return;
David Woodhousefe9ab002013-03-14 13:21:00 +0000348 da_tokens = new_da_tokens;
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800349
350 memcpy(da_tokens+da_num_tokens, table->tokens,
351 sizeof(struct calling_interface_token) * tokens);
352
353 da_num_tokens += tokens;
354}
355
Alan Jenkins4788df42009-08-19 15:06:50 +0100356static void __init find_tokens(const struct dmi_header *dm, void *dummy)
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800357{
358 switch (dm->type) {
359 case 0xd4: /* Indexed IO */
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800360 case 0xd5: /* Protected Area Type 1 */
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800361 case 0xd6: /* Protected Area Type 2 */
362 break;
363 case 0xda: /* Calling interface */
364 parse_da_table(dm);
365 break;
366 }
367}
368
Gabriele Mazzotta6cff8d62015-02-19 11:58:29 +0100369static int find_token_id(int tokenid)
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800370{
371 int i;
Gabriele Mazzotta6cff8d62015-02-19 11:58:29 +0100372
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800373 for (i = 0; i < da_num_tokens; i++) {
374 if (da_tokens[i].tokenID == tokenid)
Gabriele Mazzotta6cff8d62015-02-19 11:58:29 +0100375 return i;
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800376 }
377
378 return -1;
379}
380
Gabriele Mazzotta6cff8d62015-02-19 11:58:29 +0100381static int find_token_location(int tokenid)
382{
383 int id;
384
385 id = find_token_id(tokenid);
386 if (id == -1)
387 return -1;
388
389 return da_tokens[id].location;
390}
391
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800392static struct calling_interface_buffer *
393dell_send_request(struct calling_interface_buffer *buffer, int class,
394 int select)
395{
396 struct smi_cmd command;
397
398 command.magic = SMI_CMD_MAGIC;
399 command.command_address = da_command_address;
400 command.command_code = da_command_code;
401 command.ebx = virt_to_phys(buffer);
402 command.ecx = 0x42534931;
403
404 buffer->class = class;
405 buffer->select = select;
406
407 dcdbas_smi_request(&command);
408
409 return buffer;
410}
411
Gabriele Mazzotta6cff8d62015-02-19 11:58:29 +0100412static inline int dell_smi_error(int value)
413{
414 switch (value) {
415 case 0: /* Completed successfully */
416 return 0;
417 case -1: /* Completed with error */
418 return -EIO;
419 case -2: /* Function not supported */
420 return -ENXIO;
421 default: /* Unknown error */
422 return -EINVAL;
423 }
424}
425
Pali Rohárf992efb2015-06-21 10:39:26 +0200426/*
427 * Derived from information in smbios-wireless-ctl:
428 *
429 * cbSelect 17, Value 11
430 *
431 * Return Wireless Info
432 * cbArg1, byte0 = 0x00
433 *
434 * cbRes1 Standard return codes (0, -1, -2)
435 * cbRes2 Info bit flags:
436 *
437 * 0 Hardware switch supported (1)
438 * 1 WiFi locator supported (1)
439 * 2 WLAN supported (1)
440 * 3 Bluetooth (BT) supported (1)
441 * 4 WWAN supported (1)
442 * 5 Wireless KBD supported (1)
443 * 6 Uw b supported (1)
444 * 7 WiGig supported (1)
445 * 8 WLAN installed (1)
446 * 9 BT installed (1)
447 * 10 WWAN installed (1)
448 * 11 Uw b installed (1)
449 * 12 WiGig installed (1)
450 * 13-15 Reserved (0)
451 * 16 Hardware (HW) switch is On (1)
452 * 17 WLAN disabled (1)
453 * 18 BT disabled (1)
454 * 19 WWAN disabled (1)
455 * 20 Uw b disabled (1)
456 * 21 WiGig disabled (1)
457 * 20-31 Reserved (0)
458 *
459 * cbRes3 NVRAM size in bytes
460 * cbRes4, byte 0 NVRAM format version number
461 *
462 *
463 * Set QuickSet Radio Disable Flag
464 * cbArg1, byte0 = 0x01
465 * cbArg1, byte1
466 * Radio ID value:
467 * 0 Radio Status
468 * 1 WLAN ID
469 * 2 BT ID
470 * 3 WWAN ID
471 * 4 UWB ID
472 * 5 WIGIG ID
473 * cbArg1, byte2 Flag bits:
474 * 0 QuickSet disables radio (1)
475 * 1-7 Reserved (0)
476 *
477 * cbRes1 Standard return codes (0, -1, -2)
478 * cbRes2 QuickSet (QS) radio disable bit map:
479 * 0 QS disables WLAN
480 * 1 QS disables BT
481 * 2 QS disables WWAN
482 * 3 QS disables UWB
483 * 4 QS disables WIGIG
484 * 5-31 Reserved (0)
485 *
486 * Wireless Switch Configuration
487 * cbArg1, byte0 = 0x02
488 *
489 * cbArg1, byte1
490 * Subcommand:
491 * 0 Get config
492 * 1 Set config
493 * 2 Set WiFi locator enable/disable
494 * cbArg1,byte2
495 * Switch settings (if byte 1==1):
496 * 0 WLAN sw itch control (1)
497 * 1 BT sw itch control (1)
498 * 2 WWAN sw itch control (1)
499 * 3 UWB sw itch control (1)
500 * 4 WiGig sw itch control (1)
501 * 5-7 Reserved (0)
502 * cbArg1, byte2 Enable bits (if byte 1==2):
503 * 0 Enable WiFi locator (1)
504 *
505 * cbRes1 Standard return codes (0, -1, -2)
506 * cbRes2 QuickSet radio disable bit map:
507 * 0 WLAN controlled by sw itch (1)
508 * 1 BT controlled by sw itch (1)
509 * 2 WWAN controlled by sw itch (1)
510 * 3 UWB controlled by sw itch (1)
511 * 4 WiGig controlled by sw itch (1)
512 * 5-6 Reserved (0)
513 * 7 Wireless sw itch config locked (1)
514 * 8 WiFi locator enabled (1)
515 * 9-14 Reserved (0)
516 * 15 WiFi locator setting locked (1)
517 * 16-31 Reserved (0)
518 *
519 * Read Local Config Data (LCD)
520 * cbArg1, byte0 = 0x10
521 * cbArg1, byte1 NVRAM index low byte
522 * cbArg1, byte2 NVRAM index high byte
523 * cbRes1 Standard return codes (0, -1, -2)
524 * cbRes2 4 bytes read from LCD[index]
525 * cbRes3 4 bytes read from LCD[index+4]
526 * cbRes4 4 bytes read from LCD[index+8]
527 *
528 * Write Local Config Data (LCD)
529 * cbArg1, byte0 = 0x11
530 * cbArg1, byte1 NVRAM index low byte
531 * cbArg1, byte2 NVRAM index high byte
532 * cbArg2 4 bytes to w rite at LCD[index]
533 * cbArg3 4 bytes to w rite at LCD[index+4]
534 * cbArg4 4 bytes to w rite at LCD[index+8]
535 * cbRes1 Standard return codes (0, -1, -2)
536 *
537 * Populate Local Config Data from NVRAM
538 * cbArg1, byte0 = 0x12
539 * cbRes1 Standard return codes (0, -1, -2)
540 *
541 * Commit Local Config Data to NVRAM
542 * cbArg1, byte0 = 0x13
543 * cbRes1 Standard return codes (0, -1, -2)
544 */
Hans de Goede4cc8a572013-11-17 14:00:16 +0100545
546static int dell_rfkill_set(void *data, bool blocked)
547{
548 int disable = blocked ? 1 : 0;
549 unsigned long radio = (unsigned long)data;
550 int hwswitch_bit = (unsigned long)data - 1;
Hans de Goede4cc8a572013-11-17 14:00:16 +0100551
552 get_buffer();
553 dell_send_request(buffer, 17, 11);
554
555 /* If the hardware switch controls this radio, and the hardware
Hans de Goedeed112892013-11-17 14:00:24 +0100556 switch is disabled, always disable the radio */
Hans de Goede4cc8a572013-11-17 14:00:16 +0100557 if ((hwswitch_state & BIT(hwswitch_bit)) &&
Hans de Goede4d39d882013-11-17 14:00:22 +0100558 !(buffer->output[1] & BIT(16)))
Hans de Goedeed112892013-11-17 14:00:24 +0100559 disable = 1;
Hans de Goede4cc8a572013-11-17 14:00:16 +0100560
561 buffer->input[0] = (1 | (radio<<8) | (disable << 16));
562 dell_send_request(buffer, 17, 11);
563
Hans de Goede4cc8a572013-11-17 14:00:16 +0100564 release_buffer();
Hans de Goede4d39d882013-11-17 14:00:22 +0100565 return 0;
Hans de Goede4cc8a572013-11-17 14:00:16 +0100566}
567
Hans de Goede04c9a3a2013-11-17 14:00:23 +0100568/* Must be called with the buffer held */
Hans de Goede33f93592013-11-17 14:00:20 +0100569static void dell_rfkill_update_sw_state(struct rfkill *rfkill, int radio,
570 int status)
Hans de Goeded0388802013-11-17 14:00:19 +0100571{
Hans de Goede04c9a3a2013-11-17 14:00:23 +0100572 if (status & BIT(0)) {
573 /* Has hw-switch, sync sw_state to BIOS */
574 int block = rfkill_blocked(rfkill);
575 buffer->input[0] = (1 | (radio << 8) | (block << 16));
576 dell_send_request(buffer, 17, 11);
577 } else {
Hans de Goede3f565882013-11-17 14:00:21 +0100578 /* No hw-switch, sync BIOS state to sw_state */
579 rfkill_set_sw_state(rfkill, !!(status & BIT(radio + 16)));
580 }
Hans de Goede33f93592013-11-17 14:00:20 +0100581}
Hans de Goeded0388802013-11-17 14:00:19 +0100582
Hans de Goede33f93592013-11-17 14:00:20 +0100583static void dell_rfkill_update_hw_state(struct rfkill *rfkill, int radio,
584 int status)
585{
Hans de Goeded0388802013-11-17 14:00:19 +0100586 if (hwswitch_state & (BIT(radio - 1)))
587 rfkill_set_hw_state(rfkill, !(status & BIT(16)));
588}
589
Hans de Goede4cc8a572013-11-17 14:00:16 +0100590static void dell_rfkill_query(struct rfkill *rfkill, void *data)
591{
592 int status;
Hans de Goede4cc8a572013-11-17 14:00:16 +0100593
594 get_buffer();
595 dell_send_request(buffer, 17, 11);
596 status = buffer->output[1];
Hans de Goede4cc8a572013-11-17 14:00:16 +0100597
Hans de Goede33f93592013-11-17 14:00:20 +0100598 dell_rfkill_update_hw_state(rfkill, (unsigned long)data, status);
Hans de Goede04c9a3a2013-11-17 14:00:23 +0100599
600 release_buffer();
Hans de Goede4cc8a572013-11-17 14:00:16 +0100601}
602
603static const struct rfkill_ops dell_rfkill_ops = {
604 .set_block = dell_rfkill_set,
605 .query = dell_rfkill_query,
606};
607
Keng-Yu Lin037accf2010-09-28 11:43:31 +0800608static struct dentry *dell_laptop_dir;
609
610static int dell_debugfs_show(struct seq_file *s, void *data)
611{
612 int status;
613
614 get_buffer();
615 dell_send_request(buffer, 17, 11);
616 status = buffer->output[1];
617 release_buffer();
618
619 seq_printf(s, "status:\t0x%X\n", status);
620 seq_printf(s, "Bit 0 : Hardware switch supported: %lu\n",
621 status & BIT(0));
622 seq_printf(s, "Bit 1 : Wifi locator supported: %lu\n",
623 (status & BIT(1)) >> 1);
624 seq_printf(s, "Bit 2 : Wifi is supported: %lu\n",
625 (status & BIT(2)) >> 2);
626 seq_printf(s, "Bit 3 : Bluetooth is supported: %lu\n",
627 (status & BIT(3)) >> 3);
628 seq_printf(s, "Bit 4 : WWAN is supported: %lu\n",
629 (status & BIT(4)) >> 4);
630 seq_printf(s, "Bit 5 : Wireless keyboard supported: %lu\n",
631 (status & BIT(5)) >> 5);
632 seq_printf(s, "Bit 8 : Wifi is installed: %lu\n",
633 (status & BIT(8)) >> 8);
634 seq_printf(s, "Bit 9 : Bluetooth is installed: %lu\n",
635 (status & BIT(9)) >> 9);
636 seq_printf(s, "Bit 10: WWAN is installed: %lu\n",
637 (status & BIT(10)) >> 10);
638 seq_printf(s, "Bit 16: Hardware switch is on: %lu\n",
639 (status & BIT(16)) >> 16);
640 seq_printf(s, "Bit 17: Wifi is blocked: %lu\n",
641 (status & BIT(17)) >> 17);
642 seq_printf(s, "Bit 18: Bluetooth is blocked: %lu\n",
643 (status & BIT(18)) >> 18);
644 seq_printf(s, "Bit 19: WWAN is blocked: %lu\n",
645 (status & BIT(19)) >> 19);
646
647 seq_printf(s, "\nhwswitch_state:\t0x%X\n", hwswitch_state);
648 seq_printf(s, "Bit 0 : Wifi controlled by switch: %lu\n",
649 hwswitch_state & BIT(0));
650 seq_printf(s, "Bit 1 : Bluetooth controlled by switch: %lu\n",
651 (hwswitch_state & BIT(1)) >> 1);
652 seq_printf(s, "Bit 2 : WWAN controlled by switch: %lu\n",
653 (hwswitch_state & BIT(2)) >> 2);
654 seq_printf(s, "Bit 7 : Wireless switch config locked: %lu\n",
655 (hwswitch_state & BIT(7)) >> 7);
656 seq_printf(s, "Bit 8 : Wifi locator enabled: %lu\n",
657 (hwswitch_state & BIT(8)) >> 8);
658 seq_printf(s, "Bit 15: Wifi locator setting locked: %lu\n",
659 (hwswitch_state & BIT(15)) >> 15);
660
661 return 0;
662}
663
664static int dell_debugfs_open(struct inode *inode, struct file *file)
665{
666 return single_open(file, dell_debugfs_show, inode->i_private);
667}
668
669static const struct file_operations dell_debugfs_fops = {
670 .owner = THIS_MODULE,
671 .open = dell_debugfs_open,
672 .read = seq_read,
673 .llseek = seq_lseek,
674 .release = single_release,
675};
676
Hans de Goede4cc8a572013-11-17 14:00:16 +0100677static void dell_update_rfkill(struct work_struct *ignored)
678{
Hans de Goeded0388802013-11-17 14:00:19 +0100679 int status;
680
681 get_buffer();
682 dell_send_request(buffer, 17, 11);
683 status = buffer->output[1];
Hans de Goeded0388802013-11-17 14:00:19 +0100684
Hans de Goede33f93592013-11-17 14:00:20 +0100685 if (wifi_rfkill) {
686 dell_rfkill_update_hw_state(wifi_rfkill, 1, status);
687 dell_rfkill_update_sw_state(wifi_rfkill, 1, status);
688 }
689 if (bluetooth_rfkill) {
690 dell_rfkill_update_hw_state(bluetooth_rfkill, 2, status);
691 dell_rfkill_update_sw_state(bluetooth_rfkill, 2, status);
692 }
693 if (wwan_rfkill) {
694 dell_rfkill_update_hw_state(wwan_rfkill, 3, status);
695 dell_rfkill_update_sw_state(wwan_rfkill, 3, status);
696 }
Hans de Goede04c9a3a2013-11-17 14:00:23 +0100697
698 release_buffer();
Hans de Goede4cc8a572013-11-17 14:00:16 +0100699}
700static DECLARE_DELAYED_WORK(dell_rfkill_work, dell_update_rfkill);
701
Hans de Goede97f440c2013-12-24 20:34:01 +0100702static bool dell_laptop_i8042_filter(unsigned char data, unsigned char str,
703 struct serio *port)
704{
705 static bool extended;
706
Giedrius Statkevičius98280372014-10-18 02:57:20 +0300707 if (str & I8042_STR_AUXDATA)
Hans de Goede97f440c2013-12-24 20:34:01 +0100708 return false;
709
710 if (unlikely(data == 0xe0)) {
711 extended = true;
712 return false;
713 } else if (unlikely(extended)) {
714 switch (data) {
715 case 0x8:
716 schedule_delayed_work(&dell_rfkill_work,
717 round_jiffies_relative(HZ / 4));
718 break;
719 }
720 extended = false;
721 }
722
723 return false;
724}
Hans de Goede4cc8a572013-11-17 14:00:16 +0100725
Pali Rohárf8358572015-06-06 10:23:30 +0200726static int (*dell_rbtn_notifier_register_func)(struct notifier_block *);
727static int (*dell_rbtn_notifier_unregister_func)(struct notifier_block *);
728
729static int dell_laptop_rbtn_notifier_call(struct notifier_block *nb,
730 unsigned long action, void *data)
731{
732 schedule_delayed_work(&dell_rfkill_work, 0);
733 return NOTIFY_OK;
734}
735
736static struct notifier_block dell_laptop_rbtn_notifier = {
737 .notifier_call = dell_laptop_rbtn_notifier_call,
738};
739
Hans de Goede4cc8a572013-11-17 14:00:16 +0100740static int __init dell_setup_rfkill(void)
741{
Hans de Goedeba5194f2013-12-06 12:17:27 +0100742 int status, ret, whitelisted;
Hans de Goede2a925512013-11-17 14:00:17 +0100743 const char *product;
Hans de Goede4cc8a572013-11-17 14:00:16 +0100744
Hans de Goede2a925512013-11-17 14:00:17 +0100745 /*
Hans de Goedeba5194f2013-12-06 12:17:27 +0100746 * rfkill support causes trouble on various models, mostly Inspirons.
747 * So we whitelist certain series, and don't support rfkill on others.
Hans de Goede2a925512013-11-17 14:00:17 +0100748 */
Hans de Goedeba5194f2013-12-06 12:17:27 +0100749 whitelisted = 0;
Hans de Goede2a925512013-11-17 14:00:17 +0100750 product = dmi_get_system_info(DMI_PRODUCT_NAME);
Hans de Goedeba5194f2013-12-06 12:17:27 +0100751 if (product && (strncmp(product, "Latitude", 8) == 0 ||
752 strncmp(product, "Precision", 9) == 0))
753 whitelisted = 1;
754 if (!force_rfkill && !whitelisted)
Hans de Goede4cc8a572013-11-17 14:00:16 +0100755 return 0;
Hans de Goede4cc8a572013-11-17 14:00:16 +0100756
757 get_buffer();
758 dell_send_request(buffer, 17, 11);
759 status = buffer->output[1];
760 buffer->input[0] = 0x2;
761 dell_send_request(buffer, 17, 11);
762 hwswitch_state = buffer->output[1];
763 release_buffer();
764
Hans de Goede2bd4ac12013-11-17 14:00:27 +0100765 if (!(status & BIT(0))) {
766 if (force_rfkill) {
767 /* No hwsitch, clear all hw-controlled bits */
768 hwswitch_state &= ~7;
769 } else {
770 /* rfkill is only tested on laptops with a hwswitch */
771 return 0;
772 }
773 }
774
Hans de Goede4cc8a572013-11-17 14:00:16 +0100775 if ((status & (1<<2|1<<8)) == (1<<2|1<<8)) {
776 wifi_rfkill = rfkill_alloc("dell-wifi", &platform_device->dev,
777 RFKILL_TYPE_WLAN,
778 &dell_rfkill_ops, (void *) 1);
779 if (!wifi_rfkill) {
780 ret = -ENOMEM;
781 goto err_wifi;
782 }
783 ret = rfkill_register(wifi_rfkill);
784 if (ret)
785 goto err_wifi;
786 }
787
788 if ((status & (1<<3|1<<9)) == (1<<3|1<<9)) {
789 bluetooth_rfkill = rfkill_alloc("dell-bluetooth",
790 &platform_device->dev,
791 RFKILL_TYPE_BLUETOOTH,
792 &dell_rfkill_ops, (void *) 2);
793 if (!bluetooth_rfkill) {
794 ret = -ENOMEM;
795 goto err_bluetooth;
796 }
797 ret = rfkill_register(bluetooth_rfkill);
798 if (ret)
799 goto err_bluetooth;
800 }
801
802 if ((status & (1<<4|1<<10)) == (1<<4|1<<10)) {
803 wwan_rfkill = rfkill_alloc("dell-wwan",
804 &platform_device->dev,
805 RFKILL_TYPE_WWAN,
806 &dell_rfkill_ops, (void *) 3);
807 if (!wwan_rfkill) {
808 ret = -ENOMEM;
809 goto err_wwan;
810 }
811 ret = rfkill_register(wwan_rfkill);
812 if (ret)
813 goto err_wwan;
814 }
815
Pali Rohárf8358572015-06-06 10:23:30 +0200816 /*
817 * Dell Airplane Mode Switch driver (dell-rbtn) supports ACPI devices
818 * which can receive events from HW slider switch.
819 *
820 * Dell SMBIOS on whitelisted models supports controlling radio devices
821 * but does not support receiving HW button switch events. We can use
822 * i8042 filter hook function to receive keyboard data and handle
823 * keycode for HW button.
824 *
825 * So if it is possible we will use Dell Airplane Mode Switch ACPI
826 * driver for receiving HW events and Dell SMBIOS for setting rfkill
827 * states. If ACPI driver or device is not available we will fallback to
828 * i8042 filter hook function.
829 *
830 * To prevent duplicate rfkill devices which control and do same thing,
831 * dell-rbtn driver will automatically remove its own rfkill devices
832 * once function dell_rbtn_notifier_register() is called.
833 */
834
835 dell_rbtn_notifier_register_func =
836 symbol_request(dell_rbtn_notifier_register);
837 if (dell_rbtn_notifier_register_func) {
838 dell_rbtn_notifier_unregister_func =
839 symbol_request(dell_rbtn_notifier_unregister);
840 if (!dell_rbtn_notifier_unregister_func) {
841 symbol_put(dell_rbtn_notifier_register);
842 dell_rbtn_notifier_register_func = NULL;
843 }
844 }
845
846 if (dell_rbtn_notifier_register_func) {
847 ret = dell_rbtn_notifier_register_func(
848 &dell_laptop_rbtn_notifier);
849 symbol_put(dell_rbtn_notifier_register);
850 dell_rbtn_notifier_register_func = NULL;
851 if (ret != 0) {
852 symbol_put(dell_rbtn_notifier_unregister);
853 dell_rbtn_notifier_unregister_func = NULL;
854 }
855 } else {
856 pr_info("Symbols from dell-rbtn acpi driver are not available\n");
857 ret = -ENODEV;
858 }
859
860 if (ret == 0) {
861 pr_info("Using dell-rbtn acpi driver for receiving events\n");
862 } else if (ret != -ENODEV) {
863 pr_warn("Unable to register dell rbtn notifier\n");
Hans de Goede97f440c2013-12-24 20:34:01 +0100864 goto err_filter;
Pali Rohárf8358572015-06-06 10:23:30 +0200865 } else {
866 ret = i8042_install_filter(dell_laptop_i8042_filter);
867 if (ret) {
868 pr_warn("Unable to install key filter\n");
869 goto err_filter;
870 }
871 pr_info("Using i8042 filter function for receiving events\n");
Hans de Goede97f440c2013-12-24 20:34:01 +0100872 }
873
Hans de Goede4cc8a572013-11-17 14:00:16 +0100874 return 0;
Hans de Goede97f440c2013-12-24 20:34:01 +0100875err_filter:
876 if (wwan_rfkill)
877 rfkill_unregister(wwan_rfkill);
Hans de Goede4cc8a572013-11-17 14:00:16 +0100878err_wwan:
879 rfkill_destroy(wwan_rfkill);
880 if (bluetooth_rfkill)
881 rfkill_unregister(bluetooth_rfkill);
882err_bluetooth:
883 rfkill_destroy(bluetooth_rfkill);
884 if (wifi_rfkill)
885 rfkill_unregister(wifi_rfkill);
886err_wifi:
887 rfkill_destroy(wifi_rfkill);
888
889 return ret;
890}
891
892static void dell_cleanup_rfkill(void)
893{
Pali Rohárf8358572015-06-06 10:23:30 +0200894 if (dell_rbtn_notifier_unregister_func) {
895 dell_rbtn_notifier_unregister_func(&dell_laptop_rbtn_notifier);
896 symbol_put(dell_rbtn_notifier_unregister);
897 dell_rbtn_notifier_unregister_func = NULL;
898 } else {
899 i8042_remove_filter(dell_laptop_i8042_filter);
900 }
901 cancel_delayed_work_sync(&dell_rfkill_work);
Hans de Goede4cc8a572013-11-17 14:00:16 +0100902 if (wifi_rfkill) {
903 rfkill_unregister(wifi_rfkill);
904 rfkill_destroy(wifi_rfkill);
905 }
906 if (bluetooth_rfkill) {
907 rfkill_unregister(bluetooth_rfkill);
908 rfkill_destroy(bluetooth_rfkill);
909 }
910 if (wwan_rfkill) {
911 rfkill_unregister(wwan_rfkill);
912 rfkill_destroy(wwan_rfkill);
913 }
914}
915
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800916static int dell_send_intensity(struct backlight_device *bd)
917{
Stuart Hayes116ee772010-02-10 14:12:13 -0500918 int ret = 0;
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800919
Stuart Hayes116ee772010-02-10 14:12:13 -0500920 get_buffer();
921 buffer->input[0] = find_token_location(BRIGHTNESS_TOKEN);
922 buffer->input[1] = bd->props.brightness;
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800923
Stuart Hayes116ee772010-02-10 14:12:13 -0500924 if (buffer->input[0] == -1) {
925 ret = -ENODEV;
926 goto out;
927 }
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800928
929 if (power_supply_is_system_supplied() > 0)
Stuart Hayes116ee772010-02-10 14:12:13 -0500930 dell_send_request(buffer, 1, 2);
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800931 else
Stuart Hayes116ee772010-02-10 14:12:13 -0500932 dell_send_request(buffer, 1, 1);
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800933
Gabriele Mazzotta6cff8d62015-02-19 11:58:29 +0100934 out:
Stuart Hayes116ee772010-02-10 14:12:13 -0500935 release_buffer();
Wei Yongjun7da8fb22013-11-23 21:02:51 +0800936 return ret;
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800937}
938
939static int dell_get_intensity(struct backlight_device *bd)
940{
Stuart Hayes116ee772010-02-10 14:12:13 -0500941 int ret = 0;
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800942
Stuart Hayes116ee772010-02-10 14:12:13 -0500943 get_buffer();
944 buffer->input[0] = find_token_location(BRIGHTNESS_TOKEN);
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800945
Stuart Hayes116ee772010-02-10 14:12:13 -0500946 if (buffer->input[0] == -1) {
947 ret = -ENODEV;
948 goto out;
949 }
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800950
951 if (power_supply_is_system_supplied() > 0)
Stuart Hayes116ee772010-02-10 14:12:13 -0500952 dell_send_request(buffer, 0, 2);
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800953 else
Stuart Hayes116ee772010-02-10 14:12:13 -0500954 dell_send_request(buffer, 0, 1);
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800955
Jose Alonsob4867422011-07-10 15:46:51 -0300956 ret = buffer->output[1];
957
Gabriele Mazzotta6cff8d62015-02-19 11:58:29 +0100958 out:
Stuart Hayes116ee772010-02-10 14:12:13 -0500959 release_buffer();
Jose Alonsob4867422011-07-10 15:46:51 -0300960 return ret;
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800961}
962
Lionel Debrouxacc24722010-11-16 14:14:02 +0100963static const struct backlight_ops dell_ops = {
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800964 .get_brightness = dell_get_intensity,
965 .update_status = dell_send_intensity,
966};
967
Randy Dunlap869f8df2011-11-16 18:20:51 -0800968static void touchpad_led_on(void)
AceLan Kao2d8b90b2011-10-04 16:25:44 +0800969{
970 int command = 0x97;
971 char data = 1;
972 i8042_command(&data, command | 1 << 12);
973}
974
Randy Dunlap869f8df2011-11-16 18:20:51 -0800975static void touchpad_led_off(void)
AceLan Kao2d8b90b2011-10-04 16:25:44 +0800976{
977 int command = 0x97;
978 char data = 2;
979 i8042_command(&data, command | 1 << 12);
980}
981
982static void touchpad_led_set(struct led_classdev *led_cdev,
983 enum led_brightness value)
984{
985 if (value > 0)
986 touchpad_led_on();
987 else
988 touchpad_led_off();
989}
990
991static struct led_classdev touchpad_led = {
992 .name = "dell-laptop::touchpad",
993 .brightness_set = touchpad_led_set,
AceLan Kao2d5de9e2012-01-17 16:18:06 +0800994 .flags = LED_CORE_SUSPENDRESUME,
AceLan Kao2d8b90b2011-10-04 16:25:44 +0800995};
996
Mathias Krause681480c2014-07-16 19:43:09 +0200997static int __init touchpad_led_init(struct device *dev)
AceLan Kao2d8b90b2011-10-04 16:25:44 +0800998{
999 return led_classdev_register(dev, &touchpad_led);
1000}
1001
1002static void touchpad_led_exit(void)
1003{
1004 led_classdev_unregister(&touchpad_led);
1005}
1006
Gabriele Mazzotta6cff8d62015-02-19 11:58:29 +01001007/*
1008 * Derived from information in smbios-keyboard-ctl:
1009 *
1010 * cbClass 4
1011 * cbSelect 11
1012 * Keyboard illumination
1013 * cbArg1 determines the function to be performed
1014 *
1015 * cbArg1 0x0 = Get Feature Information
1016 * cbRES1 Standard return codes (0, -1, -2)
1017 * cbRES2, word0 Bitmap of user-selectable modes
1018 * bit 0 Always off (All systems)
1019 * bit 1 Always on (Travis ATG, Siberia)
1020 * bit 2 Auto: ALS-based On; ALS-based Off (Travis ATG)
1021 * bit 3 Auto: ALS- and input-activity-based On; input-activity based Off
1022 * bit 4 Auto: Input-activity-based On; input-activity based Off
1023 * bit 5 Auto: Input-activity-based On (illumination level 25%); input-activity based Off
1024 * bit 6 Auto: Input-activity-based On (illumination level 50%); input-activity based Off
1025 * bit 7 Auto: Input-activity-based On (illumination level 75%); input-activity based Off
1026 * bit 8 Auto: Input-activity-based On (illumination level 100%); input-activity based Off
1027 * bits 9-15 Reserved for future use
1028 * cbRES2, byte2 Reserved for future use
1029 * cbRES2, byte3 Keyboard illumination type
1030 * 0 Reserved
1031 * 1 Tasklight
1032 * 2 Backlight
1033 * 3-255 Reserved for future use
1034 * cbRES3, byte0 Supported auto keyboard illumination trigger bitmap.
1035 * bit 0 Any keystroke
1036 * bit 1 Touchpad activity
1037 * bit 2 Pointing stick
1038 * bit 3 Any mouse
1039 * bits 4-7 Reserved for future use
1040 * cbRES3, byte1 Supported timeout unit bitmap
1041 * bit 0 Seconds
1042 * bit 1 Minutes
1043 * bit 2 Hours
1044 * bit 3 Days
1045 * bits 4-7 Reserved for future use
1046 * cbRES3, byte2 Number of keyboard light brightness levels
1047 * cbRES4, byte0 Maximum acceptable seconds value (0 if seconds not supported).
1048 * cbRES4, byte1 Maximum acceptable minutes value (0 if minutes not supported).
1049 * cbRES4, byte2 Maximum acceptable hours value (0 if hours not supported).
1050 * cbRES4, byte3 Maximum acceptable days value (0 if days not supported)
1051 *
1052 * cbArg1 0x1 = Get Current State
1053 * cbRES1 Standard return codes (0, -1, -2)
1054 * cbRES2, word0 Bitmap of current mode state
1055 * bit 0 Always off (All systems)
1056 * bit 1 Always on (Travis ATG, Siberia)
1057 * bit 2 Auto: ALS-based On; ALS-based Off (Travis ATG)
1058 * bit 3 Auto: ALS- and input-activity-based On; input-activity based Off
1059 * bit 4 Auto: Input-activity-based On; input-activity based Off
1060 * bit 5 Auto: Input-activity-based On (illumination level 25%); input-activity based Off
1061 * bit 6 Auto: Input-activity-based On (illumination level 50%); input-activity based Off
1062 * bit 7 Auto: Input-activity-based On (illumination level 75%); input-activity based Off
1063 * bit 8 Auto: Input-activity-based On (illumination level 100%); input-activity based Off
1064 * bits 9-15 Reserved for future use
1065 * Note: Only One bit can be set
1066 * cbRES2, byte2 Currently active auto keyboard illumination triggers.
1067 * bit 0 Any keystroke
1068 * bit 1 Touchpad activity
1069 * bit 2 Pointing stick
1070 * bit 3 Any mouse
1071 * bits 4-7 Reserved for future use
1072 * cbRES2, byte3 Current Timeout
1073 * bits 7:6 Timeout units indicator:
1074 * 00b Seconds
1075 * 01b Minutes
1076 * 10b Hours
1077 * 11b Days
1078 * bits 5:0 Timeout value (0-63) in sec/min/hr/day
1079 * NOTE: A value of 0 means always on (no timeout) if any bits of RES3 byte
1080 * are set upon return from the [Get feature information] call.
1081 * cbRES3, byte0 Current setting of ALS value that turns the light on or off.
1082 * cbRES3, byte1 Current ALS reading
1083 * cbRES3, byte2 Current keyboard light level.
1084 *
1085 * cbArg1 0x2 = Set New State
1086 * cbRES1 Standard return codes (0, -1, -2)
1087 * cbArg2, word0 Bitmap of current mode state
1088 * bit 0 Always off (All systems)
1089 * bit 1 Always on (Travis ATG, Siberia)
1090 * bit 2 Auto: ALS-based On; ALS-based Off (Travis ATG)
1091 * bit 3 Auto: ALS- and input-activity-based On; input-activity based Off
1092 * bit 4 Auto: Input-activity-based On; input-activity based Off
1093 * bit 5 Auto: Input-activity-based On (illumination level 25%); input-activity based Off
1094 * bit 6 Auto: Input-activity-based On (illumination level 50%); input-activity based Off
1095 * bit 7 Auto: Input-activity-based On (illumination level 75%); input-activity based Off
1096 * bit 8 Auto: Input-activity-based On (illumination level 100%); input-activity based Off
1097 * bits 9-15 Reserved for future use
1098 * Note: Only One bit can be set
1099 * cbArg2, byte2 Desired auto keyboard illumination triggers. Must remain inactive to allow
1100 * keyboard to turn off automatically.
1101 * bit 0 Any keystroke
1102 * bit 1 Touchpad activity
1103 * bit 2 Pointing stick
1104 * bit 3 Any mouse
1105 * bits 4-7 Reserved for future use
1106 * cbArg2, byte3 Desired Timeout
1107 * bits 7:6 Timeout units indicator:
1108 * 00b Seconds
1109 * 01b Minutes
1110 * 10b Hours
1111 * 11b Days
1112 * bits 5:0 Timeout value (0-63) in sec/min/hr/day
1113 * cbArg3, byte0 Desired setting of ALS value that turns the light on or off.
1114 * cbArg3, byte2 Desired keyboard light level.
1115 */
1116
1117
1118enum kbd_timeout_unit {
1119 KBD_TIMEOUT_SECONDS = 0,
1120 KBD_TIMEOUT_MINUTES,
1121 KBD_TIMEOUT_HOURS,
1122 KBD_TIMEOUT_DAYS,
1123};
1124
1125enum kbd_mode_bit {
1126 KBD_MODE_BIT_OFF = 0,
1127 KBD_MODE_BIT_ON,
1128 KBD_MODE_BIT_ALS,
1129 KBD_MODE_BIT_TRIGGER_ALS,
1130 KBD_MODE_BIT_TRIGGER,
1131 KBD_MODE_BIT_TRIGGER_25,
1132 KBD_MODE_BIT_TRIGGER_50,
1133 KBD_MODE_BIT_TRIGGER_75,
1134 KBD_MODE_BIT_TRIGGER_100,
1135};
1136
1137#define kbd_is_als_mode_bit(bit) \
1138 ((bit) == KBD_MODE_BIT_ALS || (bit) == KBD_MODE_BIT_TRIGGER_ALS)
1139#define kbd_is_trigger_mode_bit(bit) \
1140 ((bit) >= KBD_MODE_BIT_TRIGGER_ALS && (bit) <= KBD_MODE_BIT_TRIGGER_100)
1141#define kbd_is_level_mode_bit(bit) \
1142 ((bit) >= KBD_MODE_BIT_TRIGGER_25 && (bit) <= KBD_MODE_BIT_TRIGGER_100)
1143
1144struct kbd_info {
1145 u16 modes;
1146 u8 type;
1147 u8 triggers;
1148 u8 levels;
1149 u8 seconds;
1150 u8 minutes;
1151 u8 hours;
1152 u8 days;
1153};
1154
1155struct kbd_state {
1156 u8 mode_bit;
1157 u8 triggers;
1158 u8 timeout_value;
1159 u8 timeout_unit;
1160 u8 als_setting;
1161 u8 als_value;
1162 u8 level;
1163};
1164
1165static const int kbd_tokens[] = {
1166 KBD_LED_OFF_TOKEN,
1167 KBD_LED_AUTO_25_TOKEN,
1168 KBD_LED_AUTO_50_TOKEN,
1169 KBD_LED_AUTO_75_TOKEN,
1170 KBD_LED_AUTO_100_TOKEN,
1171 KBD_LED_ON_TOKEN,
1172};
1173
1174static u16 kbd_token_bits;
1175
1176static struct kbd_info kbd_info;
1177static bool kbd_als_supported;
1178static bool kbd_triggers_supported;
1179
1180static u8 kbd_mode_levels[16];
1181static int kbd_mode_levels_count;
1182
1183static u8 kbd_previous_level;
1184static u8 kbd_previous_mode_bit;
1185
1186static bool kbd_led_present;
1187
1188/*
1189 * NOTE: there are three ways to set the keyboard backlight level.
1190 * First, via kbd_state.mode_bit (assigning KBD_MODE_BIT_TRIGGER_* value).
1191 * Second, via kbd_state.level (assigning numerical value <= kbd_info.levels).
1192 * Third, via SMBIOS tokens (KBD_LED_* in kbd_tokens)
1193 *
1194 * There are laptops which support only one of these methods. If we want to
1195 * support as many machines as possible we need to implement all three methods.
1196 * The first two methods use the kbd_state structure. The third uses SMBIOS
1197 * tokens. If kbd_info.levels == 0, the machine does not support setting the
1198 * keyboard backlight level via kbd_state.level.
1199 */
1200
1201static int kbd_get_info(struct kbd_info *info)
1202{
1203 u8 units;
1204 int ret;
1205
1206 get_buffer();
1207
1208 buffer->input[0] = 0x0;
1209 dell_send_request(buffer, 4, 11);
1210 ret = buffer->output[0];
1211
1212 if (ret) {
1213 ret = dell_smi_error(ret);
1214 goto out;
1215 }
1216
1217 info->modes = buffer->output[1] & 0xFFFF;
1218 info->type = (buffer->output[1] >> 24) & 0xFF;
1219 info->triggers = buffer->output[2] & 0xFF;
1220 units = (buffer->output[2] >> 8) & 0xFF;
1221 info->levels = (buffer->output[2] >> 16) & 0xFF;
1222
1223 if (units & BIT(0))
1224 info->seconds = (buffer->output[3] >> 0) & 0xFF;
1225 if (units & BIT(1))
1226 info->minutes = (buffer->output[3] >> 8) & 0xFF;
1227 if (units & BIT(2))
1228 info->hours = (buffer->output[3] >> 16) & 0xFF;
1229 if (units & BIT(3))
1230 info->days = (buffer->output[3] >> 24) & 0xFF;
1231
1232 out:
1233 release_buffer();
1234 return ret;
1235}
1236
1237static unsigned int kbd_get_max_level(void)
1238{
1239 if (kbd_info.levels != 0)
1240 return kbd_info.levels;
1241 if (kbd_mode_levels_count > 0)
1242 return kbd_mode_levels_count - 1;
1243 return 0;
1244}
1245
1246static int kbd_get_level(struct kbd_state *state)
1247{
1248 int i;
1249
1250 if (kbd_info.levels != 0)
1251 return state->level;
1252
1253 if (kbd_mode_levels_count > 0) {
1254 for (i = 0; i < kbd_mode_levels_count; ++i)
1255 if (kbd_mode_levels[i] == state->mode_bit)
1256 return i;
1257 return 0;
1258 }
1259
1260 return -EINVAL;
1261}
1262
1263static int kbd_set_level(struct kbd_state *state, u8 level)
1264{
1265 if (kbd_info.levels != 0) {
1266 if (level != 0)
1267 kbd_previous_level = level;
1268 if (state->level == level)
1269 return 0;
1270 state->level = level;
1271 if (level != 0 && state->mode_bit == KBD_MODE_BIT_OFF)
1272 state->mode_bit = kbd_previous_mode_bit;
1273 else if (level == 0 && state->mode_bit != KBD_MODE_BIT_OFF) {
1274 kbd_previous_mode_bit = state->mode_bit;
1275 state->mode_bit = KBD_MODE_BIT_OFF;
1276 }
1277 return 0;
1278 }
1279
1280 if (kbd_mode_levels_count > 0 && level < kbd_mode_levels_count) {
1281 if (level != 0)
1282 kbd_previous_level = level;
1283 state->mode_bit = kbd_mode_levels[level];
1284 return 0;
1285 }
1286
1287 return -EINVAL;
1288}
1289
1290static int kbd_get_state(struct kbd_state *state)
1291{
1292 int ret;
1293
1294 get_buffer();
1295
1296 buffer->input[0] = 0x1;
1297 dell_send_request(buffer, 4, 11);
1298 ret = buffer->output[0];
1299
1300 if (ret) {
1301 ret = dell_smi_error(ret);
1302 goto out;
1303 }
1304
1305 state->mode_bit = ffs(buffer->output[1] & 0xFFFF);
1306 if (state->mode_bit != 0)
1307 state->mode_bit--;
1308
1309 state->triggers = (buffer->output[1] >> 16) & 0xFF;
1310 state->timeout_value = (buffer->output[1] >> 24) & 0x3F;
1311 state->timeout_unit = (buffer->output[1] >> 30) & 0x3;
1312 state->als_setting = buffer->output[2] & 0xFF;
1313 state->als_value = (buffer->output[2] >> 8) & 0xFF;
1314 state->level = (buffer->output[2] >> 16) & 0xFF;
1315
1316 out:
1317 release_buffer();
1318 return ret;
1319}
1320
1321static int kbd_set_state(struct kbd_state *state)
1322{
1323 int ret;
1324
1325 get_buffer();
1326 buffer->input[0] = 0x2;
1327 buffer->input[1] = BIT(state->mode_bit) & 0xFFFF;
1328 buffer->input[1] |= (state->triggers & 0xFF) << 16;
1329 buffer->input[1] |= (state->timeout_value & 0x3F) << 24;
1330 buffer->input[1] |= (state->timeout_unit & 0x3) << 30;
1331 buffer->input[2] = state->als_setting & 0xFF;
1332 buffer->input[2] |= (state->level & 0xFF) << 16;
1333 dell_send_request(buffer, 4, 11);
1334 ret = buffer->output[0];
1335 release_buffer();
1336
1337 return dell_smi_error(ret);
1338}
1339
1340static int kbd_set_state_safe(struct kbd_state *state, struct kbd_state *old)
1341{
1342 int ret;
1343
1344 ret = kbd_set_state(state);
1345 if (ret == 0)
1346 return 0;
1347
1348 /*
1349 * When setting the new state fails,try to restore the previous one.
1350 * This is needed on some machines where BIOS sets a default state when
1351 * setting a new state fails. This default state could be all off.
1352 */
1353
1354 if (kbd_set_state(old))
1355 pr_err("Setting old previous keyboard state failed\n");
1356
1357 return ret;
1358}
1359
1360static int kbd_set_token_bit(u8 bit)
1361{
1362 int id;
1363 int ret;
1364
1365 if (bit >= ARRAY_SIZE(kbd_tokens))
1366 return -EINVAL;
1367
1368 id = find_token_id(kbd_tokens[bit]);
1369 if (id == -1)
1370 return -EINVAL;
1371
1372 get_buffer();
1373 buffer->input[0] = da_tokens[id].location;
1374 buffer->input[1] = da_tokens[id].value;
1375 dell_send_request(buffer, 1, 0);
1376 ret = buffer->output[0];
1377 release_buffer();
1378
1379 return dell_smi_error(ret);
1380}
1381
1382static int kbd_get_token_bit(u8 bit)
1383{
1384 int id;
1385 int ret;
1386 int val;
1387
1388 if (bit >= ARRAY_SIZE(kbd_tokens))
1389 return -EINVAL;
1390
1391 id = find_token_id(kbd_tokens[bit]);
1392 if (id == -1)
1393 return -EINVAL;
1394
1395 get_buffer();
1396 buffer->input[0] = da_tokens[id].location;
1397 dell_send_request(buffer, 0, 0);
1398 ret = buffer->output[0];
1399 val = buffer->output[1];
1400 release_buffer();
1401
1402 if (ret)
1403 return dell_smi_error(ret);
1404
1405 return (val == da_tokens[id].value);
1406}
1407
1408static int kbd_get_first_active_token_bit(void)
1409{
1410 int i;
1411 int ret;
1412
1413 for (i = 0; i < ARRAY_SIZE(kbd_tokens); ++i) {
1414 ret = kbd_get_token_bit(i);
1415 if (ret == 1)
1416 return i;
1417 }
1418
1419 return ret;
1420}
1421
1422static int kbd_get_valid_token_counts(void)
1423{
1424 return hweight16(kbd_token_bits);
1425}
1426
1427static inline int kbd_init_info(void)
1428{
1429 struct kbd_state state;
1430 int ret;
1431 int i;
1432
1433 ret = kbd_get_info(&kbd_info);
1434 if (ret)
1435 return ret;
1436
1437 kbd_get_state(&state);
1438
1439 /* NOTE: timeout value is stored in 6 bits so max value is 63 */
1440 if (kbd_info.seconds > 63)
1441 kbd_info.seconds = 63;
1442 if (kbd_info.minutes > 63)
1443 kbd_info.minutes = 63;
1444 if (kbd_info.hours > 63)
1445 kbd_info.hours = 63;
1446 if (kbd_info.days > 63)
1447 kbd_info.days = 63;
1448
1449 /* NOTE: On tested machines ON mode did not work and caused
1450 * problems (turned backlight off) so do not use it
1451 */
1452 kbd_info.modes &= ~BIT(KBD_MODE_BIT_ON);
1453
1454 kbd_previous_level = kbd_get_level(&state);
1455 kbd_previous_mode_bit = state.mode_bit;
1456
1457 if (kbd_previous_level == 0 && kbd_get_max_level() != 0)
1458 kbd_previous_level = 1;
1459
1460 if (kbd_previous_mode_bit == KBD_MODE_BIT_OFF) {
1461 kbd_previous_mode_bit =
1462 ffs(kbd_info.modes & ~BIT(KBD_MODE_BIT_OFF));
1463 if (kbd_previous_mode_bit != 0)
1464 kbd_previous_mode_bit--;
1465 }
1466
1467 if (kbd_info.modes & (BIT(KBD_MODE_BIT_ALS) |
1468 BIT(KBD_MODE_BIT_TRIGGER_ALS)))
1469 kbd_als_supported = true;
1470
1471 if (kbd_info.modes & (
1472 BIT(KBD_MODE_BIT_TRIGGER_ALS) | BIT(KBD_MODE_BIT_TRIGGER) |
1473 BIT(KBD_MODE_BIT_TRIGGER_25) | BIT(KBD_MODE_BIT_TRIGGER_50) |
1474 BIT(KBD_MODE_BIT_TRIGGER_75) | BIT(KBD_MODE_BIT_TRIGGER_100)
1475 ))
1476 kbd_triggers_supported = true;
1477
1478 /* kbd_mode_levels[0] is reserved, see below */
1479 for (i = 0; i < 16; ++i)
1480 if (kbd_is_level_mode_bit(i) && (BIT(i) & kbd_info.modes))
1481 kbd_mode_levels[1 + kbd_mode_levels_count++] = i;
1482
1483 /*
1484 * Find the first supported mode and assign to kbd_mode_levels[0].
1485 * This should be 0 (off), but we cannot depend on the BIOS to
1486 * support 0.
1487 */
1488 if (kbd_mode_levels_count > 0) {
1489 for (i = 0; i < 16; ++i) {
1490 if (BIT(i) & kbd_info.modes) {
1491 kbd_mode_levels[0] = i;
1492 break;
1493 }
1494 }
1495 kbd_mode_levels_count++;
1496 }
1497
1498 return 0;
1499
1500}
1501
1502static inline void kbd_init_tokens(void)
1503{
1504 int i;
1505
1506 for (i = 0; i < ARRAY_SIZE(kbd_tokens); ++i)
1507 if (find_token_id(kbd_tokens[i]) != -1)
1508 kbd_token_bits |= BIT(i);
1509}
1510
1511static void kbd_init(void)
1512{
1513 int ret;
1514
1515 ret = kbd_init_info();
1516 kbd_init_tokens();
1517
1518 if (kbd_token_bits != 0 || ret == 0)
1519 kbd_led_present = true;
1520}
1521
1522static ssize_t kbd_led_timeout_store(struct device *dev,
1523 struct device_attribute *attr,
1524 const char *buf, size_t count)
1525{
1526 struct kbd_state new_state;
1527 struct kbd_state state;
1528 bool convert;
1529 int value;
1530 int ret;
1531 char ch;
1532 u8 unit;
1533 int i;
1534
1535 ret = sscanf(buf, "%d %c", &value, &ch);
1536 if (ret < 1)
1537 return -EINVAL;
1538 else if (ret == 1)
1539 ch = 's';
1540
1541 if (value < 0)
1542 return -EINVAL;
1543
1544 convert = false;
1545
1546 switch (ch) {
1547 case 's':
1548 if (value > kbd_info.seconds)
1549 convert = true;
1550 unit = KBD_TIMEOUT_SECONDS;
1551 break;
1552 case 'm':
1553 if (value > kbd_info.minutes)
1554 convert = true;
1555 unit = KBD_TIMEOUT_MINUTES;
1556 break;
1557 case 'h':
1558 if (value > kbd_info.hours)
1559 convert = true;
1560 unit = KBD_TIMEOUT_HOURS;
1561 break;
1562 case 'd':
1563 if (value > kbd_info.days)
1564 convert = true;
1565 unit = KBD_TIMEOUT_DAYS;
1566 break;
1567 default:
1568 return -EINVAL;
1569 }
1570
1571 if (quirks && quirks->needs_kbd_timeouts)
1572 convert = true;
1573
1574 if (convert) {
1575 /* Convert value from current units to seconds */
1576 switch (unit) {
1577 case KBD_TIMEOUT_DAYS:
1578 value *= 24;
1579 case KBD_TIMEOUT_HOURS:
1580 value *= 60;
1581 case KBD_TIMEOUT_MINUTES:
1582 value *= 60;
1583 unit = KBD_TIMEOUT_SECONDS;
1584 }
1585
1586 if (quirks && quirks->needs_kbd_timeouts) {
1587 for (i = 0; quirks->kbd_timeouts[i] != -1; i++) {
1588 if (value <= quirks->kbd_timeouts[i]) {
1589 value = quirks->kbd_timeouts[i];
1590 break;
1591 }
1592 }
1593 }
1594
1595 if (value <= kbd_info.seconds && kbd_info.seconds) {
1596 unit = KBD_TIMEOUT_SECONDS;
1597 } else if (value / 60 <= kbd_info.minutes && kbd_info.minutes) {
1598 value /= 60;
1599 unit = KBD_TIMEOUT_MINUTES;
1600 } else if (value / (60 * 60) <= kbd_info.hours && kbd_info.hours) {
1601 value /= (60 * 60);
1602 unit = KBD_TIMEOUT_HOURS;
1603 } else if (value / (60 * 60 * 24) <= kbd_info.days && kbd_info.days) {
1604 value /= (60 * 60 * 24);
1605 unit = KBD_TIMEOUT_DAYS;
1606 } else {
1607 return -EINVAL;
1608 }
1609 }
1610
1611 ret = kbd_get_state(&state);
1612 if (ret)
1613 return ret;
1614
1615 new_state = state;
1616 new_state.timeout_value = value;
1617 new_state.timeout_unit = unit;
1618
1619 ret = kbd_set_state_safe(&new_state, &state);
1620 if (ret)
1621 return ret;
1622
1623 return count;
1624}
1625
1626static ssize_t kbd_led_timeout_show(struct device *dev,
1627 struct device_attribute *attr, char *buf)
1628{
1629 struct kbd_state state;
1630 int ret;
1631 int len;
1632
1633 ret = kbd_get_state(&state);
1634 if (ret)
1635 return ret;
1636
1637 len = sprintf(buf, "%d", state.timeout_value);
1638
1639 switch (state.timeout_unit) {
1640 case KBD_TIMEOUT_SECONDS:
1641 return len + sprintf(buf+len, "s\n");
1642 case KBD_TIMEOUT_MINUTES:
1643 return len + sprintf(buf+len, "m\n");
1644 case KBD_TIMEOUT_HOURS:
1645 return len + sprintf(buf+len, "h\n");
1646 case KBD_TIMEOUT_DAYS:
1647 return len + sprintf(buf+len, "d\n");
1648 default:
1649 return -EINVAL;
1650 }
1651
1652 return len;
1653}
1654
1655static DEVICE_ATTR(stop_timeout, S_IRUGO | S_IWUSR,
1656 kbd_led_timeout_show, kbd_led_timeout_store);
1657
1658static const char * const kbd_led_triggers[] = {
1659 "keyboard",
1660 "touchpad",
1661 /*"trackstick"*/ NULL, /* NOTE: trackstick is just alias for touchpad */
1662 "mouse",
1663};
1664
1665static ssize_t kbd_led_triggers_store(struct device *dev,
1666 struct device_attribute *attr,
1667 const char *buf, size_t count)
1668{
1669 struct kbd_state new_state;
1670 struct kbd_state state;
1671 bool triggers_enabled = false;
1672 int trigger_bit = -1;
1673 char trigger[21];
1674 int i, ret;
1675
1676 ret = sscanf(buf, "%20s", trigger);
1677 if (ret != 1)
1678 return -EINVAL;
1679
1680 if (trigger[0] != '+' && trigger[0] != '-')
1681 return -EINVAL;
1682
1683 ret = kbd_get_state(&state);
1684 if (ret)
1685 return ret;
1686
1687 if (kbd_triggers_supported)
1688 triggers_enabled = kbd_is_trigger_mode_bit(state.mode_bit);
1689
1690 if (kbd_triggers_supported) {
1691 for (i = 0; i < ARRAY_SIZE(kbd_led_triggers); ++i) {
1692 if (!(kbd_info.triggers & BIT(i)))
1693 continue;
1694 if (!kbd_led_triggers[i])
1695 continue;
1696 if (strcmp(trigger+1, kbd_led_triggers[i]) != 0)
1697 continue;
1698 if (trigger[0] == '+' &&
1699 triggers_enabled && (state.triggers & BIT(i)))
1700 return count;
1701 if (trigger[0] == '-' &&
1702 (!triggers_enabled || !(state.triggers & BIT(i))))
1703 return count;
1704 trigger_bit = i;
1705 break;
1706 }
1707 }
1708
1709 if (trigger_bit != -1) {
1710 new_state = state;
1711 if (trigger[0] == '+')
1712 new_state.triggers |= BIT(trigger_bit);
1713 else {
1714 new_state.triggers &= ~BIT(trigger_bit);
1715 /* NOTE: trackstick bit (2) must be disabled when
1716 * disabling touchpad bit (1), otherwise touchpad
1717 * bit (1) will not be disabled */
1718 if (trigger_bit == 1)
1719 new_state.triggers &= ~BIT(2);
1720 }
1721 if ((kbd_info.triggers & new_state.triggers) !=
1722 new_state.triggers)
1723 return -EINVAL;
1724 if (new_state.triggers && !triggers_enabled) {
1725 new_state.mode_bit = KBD_MODE_BIT_TRIGGER;
1726 kbd_set_level(&new_state, kbd_previous_level);
1727 } else if (new_state.triggers == 0) {
1728 kbd_set_level(&new_state, 0);
1729 }
1730 if (!(kbd_info.modes & BIT(new_state.mode_bit)))
1731 return -EINVAL;
1732 ret = kbd_set_state_safe(&new_state, &state);
1733 if (ret)
1734 return ret;
1735 if (new_state.mode_bit != KBD_MODE_BIT_OFF)
1736 kbd_previous_mode_bit = new_state.mode_bit;
1737 return count;
1738 }
1739
1740 return -EINVAL;
1741}
1742
1743static ssize_t kbd_led_triggers_show(struct device *dev,
1744 struct device_attribute *attr, char *buf)
1745{
1746 struct kbd_state state;
1747 bool triggers_enabled;
1748 int level, i, ret;
1749 int len = 0;
1750
1751 ret = kbd_get_state(&state);
1752 if (ret)
1753 return ret;
1754
1755 len = 0;
1756
1757 if (kbd_triggers_supported) {
1758 triggers_enabled = kbd_is_trigger_mode_bit(state.mode_bit);
1759 level = kbd_get_level(&state);
1760 for (i = 0; i < ARRAY_SIZE(kbd_led_triggers); ++i) {
1761 if (!(kbd_info.triggers & BIT(i)))
1762 continue;
1763 if (!kbd_led_triggers[i])
1764 continue;
1765 if ((triggers_enabled || level <= 0) &&
1766 (state.triggers & BIT(i)))
1767 buf[len++] = '+';
1768 else
1769 buf[len++] = '-';
1770 len += sprintf(buf+len, "%s ", kbd_led_triggers[i]);
1771 }
1772 }
1773
1774 if (len)
1775 buf[len - 1] = '\n';
1776
1777 return len;
1778}
1779
1780static DEVICE_ATTR(start_triggers, S_IRUGO | S_IWUSR,
1781 kbd_led_triggers_show, kbd_led_triggers_store);
1782
1783static ssize_t kbd_led_als_enabled_store(struct device *dev,
1784 struct device_attribute *attr,
1785 const char *buf, size_t count)
1786{
1787 struct kbd_state new_state;
1788 struct kbd_state state;
1789 bool triggers_enabled = false;
1790 int enable;
1791 int ret;
1792
1793 ret = kstrtoint(buf, 0, &enable);
1794 if (ret)
1795 return ret;
1796
1797 ret = kbd_get_state(&state);
1798 if (ret)
1799 return ret;
1800
1801 if (enable == kbd_is_als_mode_bit(state.mode_bit))
1802 return count;
1803
1804 new_state = state;
1805
1806 if (kbd_triggers_supported)
1807 triggers_enabled = kbd_is_trigger_mode_bit(state.mode_bit);
1808
1809 if (enable) {
1810 if (triggers_enabled)
1811 new_state.mode_bit = KBD_MODE_BIT_TRIGGER_ALS;
1812 else
1813 new_state.mode_bit = KBD_MODE_BIT_ALS;
1814 } else {
1815 if (triggers_enabled) {
1816 new_state.mode_bit = KBD_MODE_BIT_TRIGGER;
1817 kbd_set_level(&new_state, kbd_previous_level);
1818 } else {
1819 new_state.mode_bit = KBD_MODE_BIT_ON;
1820 }
1821 }
1822 if (!(kbd_info.modes & BIT(new_state.mode_bit)))
1823 return -EINVAL;
1824
1825 ret = kbd_set_state_safe(&new_state, &state);
1826 if (ret)
1827 return ret;
1828 kbd_previous_mode_bit = new_state.mode_bit;
1829
1830 return count;
1831}
1832
1833static ssize_t kbd_led_als_enabled_show(struct device *dev,
1834 struct device_attribute *attr,
1835 char *buf)
1836{
1837 struct kbd_state state;
1838 bool enabled = false;
1839 int ret;
1840
1841 ret = kbd_get_state(&state);
1842 if (ret)
1843 return ret;
1844 enabled = kbd_is_als_mode_bit(state.mode_bit);
1845
1846 return sprintf(buf, "%d\n", enabled ? 1 : 0);
1847}
1848
1849static DEVICE_ATTR(als_enabled, S_IRUGO | S_IWUSR,
1850 kbd_led_als_enabled_show, kbd_led_als_enabled_store);
1851
1852static ssize_t kbd_led_als_setting_store(struct device *dev,
1853 struct device_attribute *attr,
1854 const char *buf, size_t count)
1855{
1856 struct kbd_state state;
1857 struct kbd_state new_state;
1858 u8 setting;
1859 int ret;
1860
1861 ret = kstrtou8(buf, 10, &setting);
1862 if (ret)
1863 return ret;
1864
1865 ret = kbd_get_state(&state);
1866 if (ret)
1867 return ret;
1868
1869 new_state = state;
1870 new_state.als_setting = setting;
1871
1872 ret = kbd_set_state_safe(&new_state, &state);
1873 if (ret)
1874 return ret;
1875
1876 return count;
1877}
1878
1879static ssize_t kbd_led_als_setting_show(struct device *dev,
1880 struct device_attribute *attr,
1881 char *buf)
1882{
1883 struct kbd_state state;
1884 int ret;
1885
1886 ret = kbd_get_state(&state);
1887 if (ret)
1888 return ret;
1889
1890 return sprintf(buf, "%d\n", state.als_setting);
1891}
1892
1893static DEVICE_ATTR(als_setting, S_IRUGO | S_IWUSR,
1894 kbd_led_als_setting_show, kbd_led_als_setting_store);
1895
1896static struct attribute *kbd_led_attrs[] = {
1897 &dev_attr_stop_timeout.attr,
1898 &dev_attr_start_triggers.attr,
1899 NULL,
1900};
1901
1902static const struct attribute_group kbd_led_group = {
1903 .attrs = kbd_led_attrs,
1904};
1905
1906static struct attribute *kbd_led_als_attrs[] = {
1907 &dev_attr_als_enabled.attr,
1908 &dev_attr_als_setting.attr,
1909 NULL,
1910};
1911
1912static const struct attribute_group kbd_led_als_group = {
1913 .attrs = kbd_led_als_attrs,
1914};
1915
1916static const struct attribute_group *kbd_led_groups[] = {
1917 &kbd_led_group,
1918 &kbd_led_als_group,
1919 NULL,
1920};
1921
1922static enum led_brightness kbd_led_level_get(struct led_classdev *led_cdev)
1923{
1924 int ret;
1925 u16 num;
1926 struct kbd_state state;
1927
1928 if (kbd_get_max_level()) {
1929 ret = kbd_get_state(&state);
1930 if (ret)
1931 return 0;
1932 ret = kbd_get_level(&state);
1933 if (ret < 0)
1934 return 0;
1935 return ret;
1936 }
1937
1938 if (kbd_get_valid_token_counts()) {
1939 ret = kbd_get_first_active_token_bit();
1940 if (ret < 0)
1941 return 0;
1942 for (num = kbd_token_bits; num != 0 && ret > 0; --ret)
1943 num &= num - 1; /* clear the first bit set */
1944 if (num == 0)
1945 return 0;
1946 return ffs(num) - 1;
1947 }
1948
1949 pr_warn("Keyboard brightness level control not supported\n");
1950 return 0;
1951}
1952
1953static void kbd_led_level_set(struct led_classdev *led_cdev,
1954 enum led_brightness value)
1955{
1956 struct kbd_state state;
1957 struct kbd_state new_state;
1958 u16 num;
1959
1960 if (kbd_get_max_level()) {
1961 if (kbd_get_state(&state))
1962 return;
1963 new_state = state;
1964 if (kbd_set_level(&new_state, value))
1965 return;
1966 kbd_set_state_safe(&new_state, &state);
1967 return;
1968 }
1969
1970 if (kbd_get_valid_token_counts()) {
1971 for (num = kbd_token_bits; num != 0 && value > 0; --value)
1972 num &= num - 1; /* clear the first bit set */
1973 if (num == 0)
1974 return;
1975 kbd_set_token_bit(ffs(num) - 1);
1976 return;
1977 }
1978
1979 pr_warn("Keyboard brightness level control not supported\n");
1980}
1981
1982static struct led_classdev kbd_led = {
1983 .name = "dell::kbd_backlight",
1984 .brightness_set = kbd_led_level_set,
1985 .brightness_get = kbd_led_level_get,
1986 .groups = kbd_led_groups,
1987};
1988
1989static int __init kbd_led_init(struct device *dev)
1990{
1991 kbd_init();
1992 if (!kbd_led_present)
1993 return -ENODEV;
1994 if (!kbd_als_supported)
1995 kbd_led_groups[1] = NULL;
1996 kbd_led.max_brightness = kbd_get_max_level();
1997 if (!kbd_led.max_brightness) {
1998 kbd_led.max_brightness = kbd_get_valid_token_counts();
1999 if (kbd_led.max_brightness)
2000 kbd_led.max_brightness--;
2001 }
2002 return led_classdev_register(dev, &kbd_led);
2003}
2004
2005static void brightness_set_exit(struct led_classdev *led_cdev,
2006 enum led_brightness value)
2007{
2008 /* Don't change backlight level on exit */
2009};
2010
2011static void kbd_led_exit(void)
2012{
2013 if (!kbd_led_present)
2014 return;
2015 kbd_led.brightness_set = brightness_set_exit;
2016 led_classdev_unregister(&kbd_led);
2017}
2018
Matthew Garrettad8f07c2009-01-07 18:08:56 -08002019static int __init dell_init(void)
2020{
Matthew Garrettad8f07c2009-01-07 18:08:56 -08002021 int max_intensity = 0;
2022 int ret;
2023
2024 if (!dmi_check_system(dell_device_table))
2025 return -ENODEV;
2026
AceLan Kao2d8b90b2011-10-04 16:25:44 +08002027 quirks = NULL;
2028 /* find if this machine support other functions */
2029 dmi_check_system(dell_quirks);
2030
Jean Delvaree7a19c562009-03-30 21:46:44 +02002031 dmi_walk(find_tokens, NULL);
Matthew Garrettad8f07c2009-01-07 18:08:56 -08002032
2033 if (!da_tokens) {
Joe Percheseb889522011-03-29 15:21:37 -07002034 pr_info("Unable to find dmi tokens\n");
Matthew Garrettad8f07c2009-01-07 18:08:56 -08002035 return -ENODEV;
2036 }
2037
Alan Jenkinsada32482009-08-19 15:06:49 +01002038 ret = platform_driver_register(&platform_driver);
2039 if (ret)
2040 goto fail_platform_driver;
2041 platform_device = platform_device_alloc("dell-laptop", -1);
2042 if (!platform_device) {
2043 ret = -ENOMEM;
2044 goto fail_platform_device1;
2045 }
2046 ret = platform_device_add(platform_device);
2047 if (ret)
2048 goto fail_platform_device2;
2049
Stuart Hayes116ee772010-02-10 14:12:13 -05002050 /*
2051 * Allocate buffer below 4GB for SMI data--only 32-bit physical addr
2052 * is passed to SMI handler.
2053 */
2054 bufferpage = alloc_page(GFP_KERNEL | GFP_DMA32);
Wei Yongjun9f208202013-05-09 10:03:02 +08002055 if (!bufferpage) {
2056 ret = -ENOMEM;
Stuart Hayes116ee772010-02-10 14:12:13 -05002057 goto fail_buffer;
Wei Yongjun9f208202013-05-09 10:03:02 +08002058 }
Stuart Hayes116ee772010-02-10 14:12:13 -05002059 buffer = page_address(bufferpage);
Stuart Hayes116ee772010-02-10 14:12:13 -05002060
Hans de Goede4cc8a572013-11-17 14:00:16 +01002061 ret = dell_setup_rfkill();
2062
2063 if (ret) {
2064 pr_warn("Unable to setup rfkill\n");
2065 goto fail_rfkill;
2066 }
2067
AceLan Kao2d8b90b2011-10-04 16:25:44 +08002068 if (quirks && quirks->touchpad_led)
2069 touchpad_led_init(&platform_device->dev);
2070
Gabriele Mazzotta6cff8d62015-02-19 11:58:29 +01002071 kbd_led_init(&platform_device->dev);
2072
Keng-Yu Lin037accf2010-09-28 11:43:31 +08002073 dell_laptop_dir = debugfs_create_dir("dell_laptop", NULL);
Hans de Goede4cc8a572013-11-17 14:00:16 +01002074 if (dell_laptop_dir != NULL)
2075 debugfs_create_file("rfkill", 0444, dell_laptop_dir, NULL,
2076 &dell_debugfs_fops);
Keng-Yu Lin037accf2010-09-28 11:43:31 +08002077
Matthew Garrettad8f07c2009-01-07 18:08:56 -08002078#ifdef CONFIG_ACPI
2079 /* In the event of an ACPI backlight being available, don't
2080 * register the platform controller.
2081 */
2082 if (acpi_video_backlight_support())
2083 return 0;
2084#endif
2085
Stuart Hayes116ee772010-02-10 14:12:13 -05002086 get_buffer();
2087 buffer->input[0] = find_token_location(BRIGHTNESS_TOKEN);
2088 if (buffer->input[0] != -1) {
2089 dell_send_request(buffer, 0, 2);
2090 max_intensity = buffer->output[3];
Matthew Garrettad8f07c2009-01-07 18:08:56 -08002091 }
Stuart Hayes116ee772010-02-10 14:12:13 -05002092 release_buffer();
Matthew Garrettad8f07c2009-01-07 18:08:56 -08002093
2094 if (max_intensity) {
Matthew Garretta19a6ee2010-02-17 16:39:44 -05002095 struct backlight_properties props;
2096 memset(&props, 0, sizeof(struct backlight_properties));
Matthew Garrettbb7ca742011-03-22 16:30:21 -07002097 props.type = BACKLIGHT_PLATFORM;
Matthew Garretta19a6ee2010-02-17 16:39:44 -05002098 props.max_brightness = max_intensity;
2099 dell_backlight_device = backlight_device_register("dell_backlight",
2100 &platform_device->dev,
2101 NULL,
2102 &dell_ops,
2103 &props);
Matthew Garrettad8f07c2009-01-07 18:08:56 -08002104
2105 if (IS_ERR(dell_backlight_device)) {
2106 ret = PTR_ERR(dell_backlight_device);
2107 dell_backlight_device = NULL;
Alan Jenkins71e9dc72009-08-19 15:06:47 +01002108 goto fail_backlight;
Matthew Garrettad8f07c2009-01-07 18:08:56 -08002109 }
2110
Matthew Garrettad8f07c2009-01-07 18:08:56 -08002111 dell_backlight_device->props.brightness =
2112 dell_get_intensity(dell_backlight_device);
2113 backlight_update_status(dell_backlight_device);
2114 }
2115
2116 return 0;
Alan Jenkins71e9dc72009-08-19 15:06:47 +01002117
2118fail_backlight:
Hans de Goede4cc8a572013-11-17 14:00:16 +01002119 dell_cleanup_rfkill();
2120fail_rfkill:
Stuart Hayes116ee772010-02-10 14:12:13 -05002121 free_page((unsigned long)bufferpage);
2122fail_buffer:
Alan Jenkinsada32482009-08-19 15:06:49 +01002123 platform_device_del(platform_device);
2124fail_platform_device2:
2125 platform_device_put(platform_device);
2126fail_platform_device1:
2127 platform_driver_unregister(&platform_driver);
2128fail_platform_driver:
Matthew Garrettad8f07c2009-01-07 18:08:56 -08002129 kfree(da_tokens);
2130 return ret;
2131}
2132
2133static void __exit dell_exit(void)
2134{
Keng-Yu Lin037accf2010-09-28 11:43:31 +08002135 debugfs_remove_recursive(dell_laptop_dir);
AceLan Kao2d8b90b2011-10-04 16:25:44 +08002136 if (quirks && quirks->touchpad_led)
2137 touchpad_led_exit();
Gabriele Mazzotta6cff8d62015-02-19 11:58:29 +01002138 kbd_led_exit();
Matthew Garrettad8f07c2009-01-07 18:08:56 -08002139 backlight_device_unregister(dell_backlight_device);
Hans de Goede4cc8a572013-11-17 14:00:16 +01002140 dell_cleanup_rfkill();
Matthew Garrettfacd61d2010-02-09 14:03:04 -05002141 if (platform_device) {
Matthew Garrett92e00e42010-03-01 09:46:43 -05002142 platform_device_unregister(platform_device);
Matthew Garrettfacd61d2010-02-09 14:03:04 -05002143 platform_driver_unregister(&platform_driver);
2144 }
Matthew Garrette5512602010-02-09 14:05:01 -05002145 kfree(da_tokens);
Stuart Hayes116ee772010-02-10 14:12:13 -05002146 free_page((unsigned long)buffer);
Matthew Garrettad8f07c2009-01-07 18:08:56 -08002147}
2148
Pali Rohárf8358572015-06-06 10:23:30 +02002149/* dell-rbtn.c driver export functions which will not work correctly (and could
2150 * cause kernel crash) if they are called before dell-rbtn.c init code. This is
2151 * not problem when dell-rbtn.c is compiled as external module. When both files
2152 * (dell-rbtn.c and dell-laptop.c) are compiled statically into kernel, then we
2153 * need to ensure that dell_init() will be called after initializing dell-rbtn.
2154 * This can be achieved by late_initcall() instead module_init().
2155 */
2156late_initcall(dell_init);
Matthew Garrettad8f07c2009-01-07 18:08:56 -08002157module_exit(dell_exit);
2158
2159MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
Gabriele Mazzotta6cff8d62015-02-19 11:58:29 +01002160MODULE_AUTHOR("Gabriele Mazzotta <gabriele.mzt@gmail.com>");
2161MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
Matthew Garrettad8f07c2009-01-07 18:08:56 -08002162MODULE_DESCRIPTION("Dell laptop driver");
2163MODULE_LICENSE("GPL");