blob: 9724613c28a6398497ddab91e5bf85e236688732 [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);
Pali Rohár2e19f932015-06-21 10:41:42 +0200632 seq_printf(s, "Bit 6 : UWB supported: %lu\n",
633 (status & BIT(6)) >> 6);
634 seq_printf(s, "Bit 7 : WiGig supported: %lu\n",
635 (status & BIT(7)) >> 7);
Keng-Yu Lin037accf2010-09-28 11:43:31 +0800636 seq_printf(s, "Bit 8 : Wifi is installed: %lu\n",
637 (status & BIT(8)) >> 8);
638 seq_printf(s, "Bit 9 : Bluetooth is installed: %lu\n",
639 (status & BIT(9)) >> 9);
640 seq_printf(s, "Bit 10: WWAN is installed: %lu\n",
641 (status & BIT(10)) >> 10);
Pali Rohár2e19f932015-06-21 10:41:42 +0200642 seq_printf(s, "Bit 11: UWB installed: %lu\n",
643 (status & BIT(11)) >> 11);
644 seq_printf(s, "Bit 12: WiGig installed: %lu\n",
645 (status & BIT(12)) >> 12);
646
Keng-Yu Lin037accf2010-09-28 11:43:31 +0800647 seq_printf(s, "Bit 16: Hardware switch is on: %lu\n",
648 (status & BIT(16)) >> 16);
649 seq_printf(s, "Bit 17: Wifi is blocked: %lu\n",
650 (status & BIT(17)) >> 17);
651 seq_printf(s, "Bit 18: Bluetooth is blocked: %lu\n",
652 (status & BIT(18)) >> 18);
653 seq_printf(s, "Bit 19: WWAN is blocked: %lu\n",
654 (status & BIT(19)) >> 19);
Pali Rohár2e19f932015-06-21 10:41:42 +0200655 seq_printf(s, "Bit 20: UWB is blocked: %lu\n",
656 (status & BIT(20)) >> 20);
657 seq_printf(s, "Bit 21: WiGig is blocked: %lu\n",
658 (status & BIT(21)) >> 21);
Keng-Yu Lin037accf2010-09-28 11:43:31 +0800659
660 seq_printf(s, "\nhwswitch_state:\t0x%X\n", hwswitch_state);
661 seq_printf(s, "Bit 0 : Wifi controlled by switch: %lu\n",
662 hwswitch_state & BIT(0));
663 seq_printf(s, "Bit 1 : Bluetooth controlled by switch: %lu\n",
664 (hwswitch_state & BIT(1)) >> 1);
665 seq_printf(s, "Bit 2 : WWAN controlled by switch: %lu\n",
666 (hwswitch_state & BIT(2)) >> 2);
Pali Rohár2e19f932015-06-21 10:41:42 +0200667 seq_printf(s, "Bit 3 : UWB controlled by switch: %lu\n",
668 (hwswitch_state & BIT(3)) >> 3);
669 seq_printf(s, "Bit 4 : WiGig controlled by switch: %lu\n",
670 (hwswitch_state & BIT(4)) >> 4);
Keng-Yu Lin037accf2010-09-28 11:43:31 +0800671 seq_printf(s, "Bit 7 : Wireless switch config locked: %lu\n",
672 (hwswitch_state & BIT(7)) >> 7);
673 seq_printf(s, "Bit 8 : Wifi locator enabled: %lu\n",
674 (hwswitch_state & BIT(8)) >> 8);
675 seq_printf(s, "Bit 15: Wifi locator setting locked: %lu\n",
676 (hwswitch_state & BIT(15)) >> 15);
677
678 return 0;
679}
680
681static int dell_debugfs_open(struct inode *inode, struct file *file)
682{
683 return single_open(file, dell_debugfs_show, inode->i_private);
684}
685
686static const struct file_operations dell_debugfs_fops = {
687 .owner = THIS_MODULE,
688 .open = dell_debugfs_open,
689 .read = seq_read,
690 .llseek = seq_lseek,
691 .release = single_release,
692};
693
Hans de Goede4cc8a572013-11-17 14:00:16 +0100694static void dell_update_rfkill(struct work_struct *ignored)
695{
Hans de Goeded0388802013-11-17 14:00:19 +0100696 int status;
697
698 get_buffer();
699 dell_send_request(buffer, 17, 11);
700 status = buffer->output[1];
Hans de Goeded0388802013-11-17 14:00:19 +0100701
Hans de Goede33f93592013-11-17 14:00:20 +0100702 if (wifi_rfkill) {
703 dell_rfkill_update_hw_state(wifi_rfkill, 1, status);
704 dell_rfkill_update_sw_state(wifi_rfkill, 1, status);
705 }
706 if (bluetooth_rfkill) {
707 dell_rfkill_update_hw_state(bluetooth_rfkill, 2, status);
708 dell_rfkill_update_sw_state(bluetooth_rfkill, 2, status);
709 }
710 if (wwan_rfkill) {
711 dell_rfkill_update_hw_state(wwan_rfkill, 3, status);
712 dell_rfkill_update_sw_state(wwan_rfkill, 3, status);
713 }
Hans de Goede04c9a3a2013-11-17 14:00:23 +0100714
715 release_buffer();
Hans de Goede4cc8a572013-11-17 14:00:16 +0100716}
717static DECLARE_DELAYED_WORK(dell_rfkill_work, dell_update_rfkill);
718
Hans de Goede97f440c2013-12-24 20:34:01 +0100719static bool dell_laptop_i8042_filter(unsigned char data, unsigned char str,
720 struct serio *port)
721{
722 static bool extended;
723
Giedrius Statkevičius98280372014-10-18 02:57:20 +0300724 if (str & I8042_STR_AUXDATA)
Hans de Goede97f440c2013-12-24 20:34:01 +0100725 return false;
726
727 if (unlikely(data == 0xe0)) {
728 extended = true;
729 return false;
730 } else if (unlikely(extended)) {
731 switch (data) {
732 case 0x8:
733 schedule_delayed_work(&dell_rfkill_work,
734 round_jiffies_relative(HZ / 4));
735 break;
736 }
737 extended = false;
738 }
739
740 return false;
741}
Hans de Goede4cc8a572013-11-17 14:00:16 +0100742
Pali Rohárf8358572015-06-06 10:23:30 +0200743static int (*dell_rbtn_notifier_register_func)(struct notifier_block *);
744static int (*dell_rbtn_notifier_unregister_func)(struct notifier_block *);
745
746static int dell_laptop_rbtn_notifier_call(struct notifier_block *nb,
747 unsigned long action, void *data)
748{
749 schedule_delayed_work(&dell_rfkill_work, 0);
750 return NOTIFY_OK;
751}
752
753static struct notifier_block dell_laptop_rbtn_notifier = {
754 .notifier_call = dell_laptop_rbtn_notifier_call,
755};
756
Hans de Goede4cc8a572013-11-17 14:00:16 +0100757static int __init dell_setup_rfkill(void)
758{
Hans de Goedeba5194f2013-12-06 12:17:27 +0100759 int status, ret, whitelisted;
Hans de Goede2a925512013-11-17 14:00:17 +0100760 const char *product;
Hans de Goede4cc8a572013-11-17 14:00:16 +0100761
Hans de Goede2a925512013-11-17 14:00:17 +0100762 /*
Hans de Goedeba5194f2013-12-06 12:17:27 +0100763 * rfkill support causes trouble on various models, mostly Inspirons.
764 * So we whitelist certain series, and don't support rfkill on others.
Hans de Goede2a925512013-11-17 14:00:17 +0100765 */
Hans de Goedeba5194f2013-12-06 12:17:27 +0100766 whitelisted = 0;
Hans de Goede2a925512013-11-17 14:00:17 +0100767 product = dmi_get_system_info(DMI_PRODUCT_NAME);
Hans de Goedeba5194f2013-12-06 12:17:27 +0100768 if (product && (strncmp(product, "Latitude", 8) == 0 ||
769 strncmp(product, "Precision", 9) == 0))
770 whitelisted = 1;
771 if (!force_rfkill && !whitelisted)
Hans de Goede4cc8a572013-11-17 14:00:16 +0100772 return 0;
Hans de Goede4cc8a572013-11-17 14:00:16 +0100773
774 get_buffer();
775 dell_send_request(buffer, 17, 11);
776 status = buffer->output[1];
777 buffer->input[0] = 0x2;
778 dell_send_request(buffer, 17, 11);
779 hwswitch_state = buffer->output[1];
780 release_buffer();
781
Hans de Goede2bd4ac12013-11-17 14:00:27 +0100782 if (!(status & BIT(0))) {
783 if (force_rfkill) {
784 /* No hwsitch, clear all hw-controlled bits */
785 hwswitch_state &= ~7;
786 } else {
787 /* rfkill is only tested on laptops with a hwswitch */
788 return 0;
789 }
790 }
791
Hans de Goede4cc8a572013-11-17 14:00:16 +0100792 if ((status & (1<<2|1<<8)) == (1<<2|1<<8)) {
793 wifi_rfkill = rfkill_alloc("dell-wifi", &platform_device->dev,
794 RFKILL_TYPE_WLAN,
795 &dell_rfkill_ops, (void *) 1);
796 if (!wifi_rfkill) {
797 ret = -ENOMEM;
798 goto err_wifi;
799 }
800 ret = rfkill_register(wifi_rfkill);
801 if (ret)
802 goto err_wifi;
803 }
804
805 if ((status & (1<<3|1<<9)) == (1<<3|1<<9)) {
806 bluetooth_rfkill = rfkill_alloc("dell-bluetooth",
807 &platform_device->dev,
808 RFKILL_TYPE_BLUETOOTH,
809 &dell_rfkill_ops, (void *) 2);
810 if (!bluetooth_rfkill) {
811 ret = -ENOMEM;
812 goto err_bluetooth;
813 }
814 ret = rfkill_register(bluetooth_rfkill);
815 if (ret)
816 goto err_bluetooth;
817 }
818
819 if ((status & (1<<4|1<<10)) == (1<<4|1<<10)) {
820 wwan_rfkill = rfkill_alloc("dell-wwan",
821 &platform_device->dev,
822 RFKILL_TYPE_WWAN,
823 &dell_rfkill_ops, (void *) 3);
824 if (!wwan_rfkill) {
825 ret = -ENOMEM;
826 goto err_wwan;
827 }
828 ret = rfkill_register(wwan_rfkill);
829 if (ret)
830 goto err_wwan;
831 }
832
Pali Rohárf8358572015-06-06 10:23:30 +0200833 /*
834 * Dell Airplane Mode Switch driver (dell-rbtn) supports ACPI devices
835 * which can receive events from HW slider switch.
836 *
837 * Dell SMBIOS on whitelisted models supports controlling radio devices
838 * but does not support receiving HW button switch events. We can use
839 * i8042 filter hook function to receive keyboard data and handle
840 * keycode for HW button.
841 *
842 * So if it is possible we will use Dell Airplane Mode Switch ACPI
843 * driver for receiving HW events and Dell SMBIOS for setting rfkill
844 * states. If ACPI driver or device is not available we will fallback to
845 * i8042 filter hook function.
846 *
847 * To prevent duplicate rfkill devices which control and do same thing,
848 * dell-rbtn driver will automatically remove its own rfkill devices
849 * once function dell_rbtn_notifier_register() is called.
850 */
851
852 dell_rbtn_notifier_register_func =
853 symbol_request(dell_rbtn_notifier_register);
854 if (dell_rbtn_notifier_register_func) {
855 dell_rbtn_notifier_unregister_func =
856 symbol_request(dell_rbtn_notifier_unregister);
857 if (!dell_rbtn_notifier_unregister_func) {
858 symbol_put(dell_rbtn_notifier_register);
859 dell_rbtn_notifier_register_func = NULL;
860 }
861 }
862
863 if (dell_rbtn_notifier_register_func) {
864 ret = dell_rbtn_notifier_register_func(
865 &dell_laptop_rbtn_notifier);
866 symbol_put(dell_rbtn_notifier_register);
867 dell_rbtn_notifier_register_func = NULL;
868 if (ret != 0) {
869 symbol_put(dell_rbtn_notifier_unregister);
870 dell_rbtn_notifier_unregister_func = NULL;
871 }
872 } else {
873 pr_info("Symbols from dell-rbtn acpi driver are not available\n");
874 ret = -ENODEV;
875 }
876
877 if (ret == 0) {
878 pr_info("Using dell-rbtn acpi driver for receiving events\n");
879 } else if (ret != -ENODEV) {
880 pr_warn("Unable to register dell rbtn notifier\n");
Hans de Goede97f440c2013-12-24 20:34:01 +0100881 goto err_filter;
Pali Rohárf8358572015-06-06 10:23:30 +0200882 } else {
883 ret = i8042_install_filter(dell_laptop_i8042_filter);
884 if (ret) {
885 pr_warn("Unable to install key filter\n");
886 goto err_filter;
887 }
888 pr_info("Using i8042 filter function for receiving events\n");
Hans de Goede97f440c2013-12-24 20:34:01 +0100889 }
890
Hans de Goede4cc8a572013-11-17 14:00:16 +0100891 return 0;
Hans de Goede97f440c2013-12-24 20:34:01 +0100892err_filter:
893 if (wwan_rfkill)
894 rfkill_unregister(wwan_rfkill);
Hans de Goede4cc8a572013-11-17 14:00:16 +0100895err_wwan:
896 rfkill_destroy(wwan_rfkill);
897 if (bluetooth_rfkill)
898 rfkill_unregister(bluetooth_rfkill);
899err_bluetooth:
900 rfkill_destroy(bluetooth_rfkill);
901 if (wifi_rfkill)
902 rfkill_unregister(wifi_rfkill);
903err_wifi:
904 rfkill_destroy(wifi_rfkill);
905
906 return ret;
907}
908
909static void dell_cleanup_rfkill(void)
910{
Pali Rohárf8358572015-06-06 10:23:30 +0200911 if (dell_rbtn_notifier_unregister_func) {
912 dell_rbtn_notifier_unregister_func(&dell_laptop_rbtn_notifier);
913 symbol_put(dell_rbtn_notifier_unregister);
914 dell_rbtn_notifier_unregister_func = NULL;
915 } else {
916 i8042_remove_filter(dell_laptop_i8042_filter);
917 }
918 cancel_delayed_work_sync(&dell_rfkill_work);
Hans de Goede4cc8a572013-11-17 14:00:16 +0100919 if (wifi_rfkill) {
920 rfkill_unregister(wifi_rfkill);
921 rfkill_destroy(wifi_rfkill);
922 }
923 if (bluetooth_rfkill) {
924 rfkill_unregister(bluetooth_rfkill);
925 rfkill_destroy(bluetooth_rfkill);
926 }
927 if (wwan_rfkill) {
928 rfkill_unregister(wwan_rfkill);
929 rfkill_destroy(wwan_rfkill);
930 }
931}
932
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800933static int dell_send_intensity(struct backlight_device *bd)
934{
Stuart Hayes116ee772010-02-10 14:12:13 -0500935 int ret = 0;
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800936
Stuart Hayes116ee772010-02-10 14:12:13 -0500937 get_buffer();
938 buffer->input[0] = find_token_location(BRIGHTNESS_TOKEN);
939 buffer->input[1] = bd->props.brightness;
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800940
Stuart Hayes116ee772010-02-10 14:12:13 -0500941 if (buffer->input[0] == -1) {
942 ret = -ENODEV;
943 goto out;
944 }
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800945
946 if (power_supply_is_system_supplied() > 0)
Stuart Hayes116ee772010-02-10 14:12:13 -0500947 dell_send_request(buffer, 1, 2);
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800948 else
Stuart Hayes116ee772010-02-10 14:12:13 -0500949 dell_send_request(buffer, 1, 1);
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800950
Gabriele Mazzotta6cff8d62015-02-19 11:58:29 +0100951 out:
Stuart Hayes116ee772010-02-10 14:12:13 -0500952 release_buffer();
Wei Yongjun7da8fb22013-11-23 21:02:51 +0800953 return ret;
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800954}
955
956static int dell_get_intensity(struct backlight_device *bd)
957{
Stuart Hayes116ee772010-02-10 14:12:13 -0500958 int ret = 0;
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800959
Stuart Hayes116ee772010-02-10 14:12:13 -0500960 get_buffer();
961 buffer->input[0] = find_token_location(BRIGHTNESS_TOKEN);
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800962
Stuart Hayes116ee772010-02-10 14:12:13 -0500963 if (buffer->input[0] == -1) {
964 ret = -ENODEV;
965 goto out;
966 }
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800967
968 if (power_supply_is_system_supplied() > 0)
Stuart Hayes116ee772010-02-10 14:12:13 -0500969 dell_send_request(buffer, 0, 2);
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800970 else
Stuart Hayes116ee772010-02-10 14:12:13 -0500971 dell_send_request(buffer, 0, 1);
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800972
Jose Alonsob4867422011-07-10 15:46:51 -0300973 ret = buffer->output[1];
974
Gabriele Mazzotta6cff8d62015-02-19 11:58:29 +0100975 out:
Stuart Hayes116ee772010-02-10 14:12:13 -0500976 release_buffer();
Jose Alonsob4867422011-07-10 15:46:51 -0300977 return ret;
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800978}
979
Lionel Debrouxacc24722010-11-16 14:14:02 +0100980static const struct backlight_ops dell_ops = {
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800981 .get_brightness = dell_get_intensity,
982 .update_status = dell_send_intensity,
983};
984
Randy Dunlap869f8df2011-11-16 18:20:51 -0800985static void touchpad_led_on(void)
AceLan Kao2d8b90b2011-10-04 16:25:44 +0800986{
987 int command = 0x97;
988 char data = 1;
989 i8042_command(&data, command | 1 << 12);
990}
991
Randy Dunlap869f8df2011-11-16 18:20:51 -0800992static void touchpad_led_off(void)
AceLan Kao2d8b90b2011-10-04 16:25:44 +0800993{
994 int command = 0x97;
995 char data = 2;
996 i8042_command(&data, command | 1 << 12);
997}
998
999static void touchpad_led_set(struct led_classdev *led_cdev,
1000 enum led_brightness value)
1001{
1002 if (value > 0)
1003 touchpad_led_on();
1004 else
1005 touchpad_led_off();
1006}
1007
1008static struct led_classdev touchpad_led = {
1009 .name = "dell-laptop::touchpad",
1010 .brightness_set = touchpad_led_set,
AceLan Kao2d5de9e2012-01-17 16:18:06 +08001011 .flags = LED_CORE_SUSPENDRESUME,
AceLan Kao2d8b90b2011-10-04 16:25:44 +08001012};
1013
Mathias Krause681480c2014-07-16 19:43:09 +02001014static int __init touchpad_led_init(struct device *dev)
AceLan Kao2d8b90b2011-10-04 16:25:44 +08001015{
1016 return led_classdev_register(dev, &touchpad_led);
1017}
1018
1019static void touchpad_led_exit(void)
1020{
1021 led_classdev_unregister(&touchpad_led);
1022}
1023
Gabriele Mazzotta6cff8d62015-02-19 11:58:29 +01001024/*
1025 * Derived from information in smbios-keyboard-ctl:
1026 *
1027 * cbClass 4
1028 * cbSelect 11
1029 * Keyboard illumination
1030 * cbArg1 determines the function to be performed
1031 *
1032 * cbArg1 0x0 = Get Feature Information
1033 * cbRES1 Standard return codes (0, -1, -2)
1034 * cbRES2, word0 Bitmap of user-selectable modes
1035 * bit 0 Always off (All systems)
1036 * bit 1 Always on (Travis ATG, Siberia)
1037 * bit 2 Auto: ALS-based On; ALS-based Off (Travis ATG)
1038 * bit 3 Auto: ALS- and input-activity-based On; input-activity based Off
1039 * bit 4 Auto: Input-activity-based On; input-activity based Off
1040 * bit 5 Auto: Input-activity-based On (illumination level 25%); input-activity based Off
1041 * bit 6 Auto: Input-activity-based On (illumination level 50%); input-activity based Off
1042 * bit 7 Auto: Input-activity-based On (illumination level 75%); input-activity based Off
1043 * bit 8 Auto: Input-activity-based On (illumination level 100%); input-activity based Off
1044 * bits 9-15 Reserved for future use
1045 * cbRES2, byte2 Reserved for future use
1046 * cbRES2, byte3 Keyboard illumination type
1047 * 0 Reserved
1048 * 1 Tasklight
1049 * 2 Backlight
1050 * 3-255 Reserved for future use
1051 * cbRES3, byte0 Supported auto keyboard illumination trigger bitmap.
1052 * bit 0 Any keystroke
1053 * bit 1 Touchpad activity
1054 * bit 2 Pointing stick
1055 * bit 3 Any mouse
1056 * bits 4-7 Reserved for future use
1057 * cbRES3, byte1 Supported timeout unit bitmap
1058 * bit 0 Seconds
1059 * bit 1 Minutes
1060 * bit 2 Hours
1061 * bit 3 Days
1062 * bits 4-7 Reserved for future use
1063 * cbRES3, byte2 Number of keyboard light brightness levels
1064 * cbRES4, byte0 Maximum acceptable seconds value (0 if seconds not supported).
1065 * cbRES4, byte1 Maximum acceptable minutes value (0 if minutes not supported).
1066 * cbRES4, byte2 Maximum acceptable hours value (0 if hours not supported).
1067 * cbRES4, byte3 Maximum acceptable days value (0 if days not supported)
1068 *
1069 * cbArg1 0x1 = Get Current State
1070 * cbRES1 Standard return codes (0, -1, -2)
1071 * cbRES2, word0 Bitmap of current mode state
1072 * bit 0 Always off (All systems)
1073 * bit 1 Always on (Travis ATG, Siberia)
1074 * bit 2 Auto: ALS-based On; ALS-based Off (Travis ATG)
1075 * bit 3 Auto: ALS- and input-activity-based On; input-activity based Off
1076 * bit 4 Auto: Input-activity-based On; input-activity based Off
1077 * bit 5 Auto: Input-activity-based On (illumination level 25%); input-activity based Off
1078 * bit 6 Auto: Input-activity-based On (illumination level 50%); input-activity based Off
1079 * bit 7 Auto: Input-activity-based On (illumination level 75%); input-activity based Off
1080 * bit 8 Auto: Input-activity-based On (illumination level 100%); input-activity based Off
1081 * bits 9-15 Reserved for future use
1082 * Note: Only One bit can be set
1083 * cbRES2, byte2 Currently active auto keyboard illumination triggers.
1084 * bit 0 Any keystroke
1085 * bit 1 Touchpad activity
1086 * bit 2 Pointing stick
1087 * bit 3 Any mouse
1088 * bits 4-7 Reserved for future use
1089 * cbRES2, byte3 Current Timeout
1090 * bits 7:6 Timeout units indicator:
1091 * 00b Seconds
1092 * 01b Minutes
1093 * 10b Hours
1094 * 11b Days
1095 * bits 5:0 Timeout value (0-63) in sec/min/hr/day
1096 * NOTE: A value of 0 means always on (no timeout) if any bits of RES3 byte
1097 * are set upon return from the [Get feature information] call.
1098 * cbRES3, byte0 Current setting of ALS value that turns the light on or off.
1099 * cbRES3, byte1 Current ALS reading
1100 * cbRES3, byte2 Current keyboard light level.
1101 *
1102 * cbArg1 0x2 = Set New State
1103 * cbRES1 Standard return codes (0, -1, -2)
1104 * cbArg2, word0 Bitmap of current mode state
1105 * bit 0 Always off (All systems)
1106 * bit 1 Always on (Travis ATG, Siberia)
1107 * bit 2 Auto: ALS-based On; ALS-based Off (Travis ATG)
1108 * bit 3 Auto: ALS- and input-activity-based On; input-activity based Off
1109 * bit 4 Auto: Input-activity-based On; input-activity based Off
1110 * bit 5 Auto: Input-activity-based On (illumination level 25%); input-activity based Off
1111 * bit 6 Auto: Input-activity-based On (illumination level 50%); input-activity based Off
1112 * bit 7 Auto: Input-activity-based On (illumination level 75%); input-activity based Off
1113 * bit 8 Auto: Input-activity-based On (illumination level 100%); input-activity based Off
1114 * bits 9-15 Reserved for future use
1115 * Note: Only One bit can be set
1116 * cbArg2, byte2 Desired auto keyboard illumination triggers. Must remain inactive to allow
1117 * keyboard to turn off automatically.
1118 * bit 0 Any keystroke
1119 * bit 1 Touchpad activity
1120 * bit 2 Pointing stick
1121 * bit 3 Any mouse
1122 * bits 4-7 Reserved for future use
1123 * cbArg2, byte3 Desired Timeout
1124 * bits 7:6 Timeout units indicator:
1125 * 00b Seconds
1126 * 01b Minutes
1127 * 10b Hours
1128 * 11b Days
1129 * bits 5:0 Timeout value (0-63) in sec/min/hr/day
1130 * cbArg3, byte0 Desired setting of ALS value that turns the light on or off.
1131 * cbArg3, byte2 Desired keyboard light level.
1132 */
1133
1134
1135enum kbd_timeout_unit {
1136 KBD_TIMEOUT_SECONDS = 0,
1137 KBD_TIMEOUT_MINUTES,
1138 KBD_TIMEOUT_HOURS,
1139 KBD_TIMEOUT_DAYS,
1140};
1141
1142enum kbd_mode_bit {
1143 KBD_MODE_BIT_OFF = 0,
1144 KBD_MODE_BIT_ON,
1145 KBD_MODE_BIT_ALS,
1146 KBD_MODE_BIT_TRIGGER_ALS,
1147 KBD_MODE_BIT_TRIGGER,
1148 KBD_MODE_BIT_TRIGGER_25,
1149 KBD_MODE_BIT_TRIGGER_50,
1150 KBD_MODE_BIT_TRIGGER_75,
1151 KBD_MODE_BIT_TRIGGER_100,
1152};
1153
1154#define kbd_is_als_mode_bit(bit) \
1155 ((bit) == KBD_MODE_BIT_ALS || (bit) == KBD_MODE_BIT_TRIGGER_ALS)
1156#define kbd_is_trigger_mode_bit(bit) \
1157 ((bit) >= KBD_MODE_BIT_TRIGGER_ALS && (bit) <= KBD_MODE_BIT_TRIGGER_100)
1158#define kbd_is_level_mode_bit(bit) \
1159 ((bit) >= KBD_MODE_BIT_TRIGGER_25 && (bit) <= KBD_MODE_BIT_TRIGGER_100)
1160
1161struct kbd_info {
1162 u16 modes;
1163 u8 type;
1164 u8 triggers;
1165 u8 levels;
1166 u8 seconds;
1167 u8 minutes;
1168 u8 hours;
1169 u8 days;
1170};
1171
1172struct kbd_state {
1173 u8 mode_bit;
1174 u8 triggers;
1175 u8 timeout_value;
1176 u8 timeout_unit;
1177 u8 als_setting;
1178 u8 als_value;
1179 u8 level;
1180};
1181
1182static const int kbd_tokens[] = {
1183 KBD_LED_OFF_TOKEN,
1184 KBD_LED_AUTO_25_TOKEN,
1185 KBD_LED_AUTO_50_TOKEN,
1186 KBD_LED_AUTO_75_TOKEN,
1187 KBD_LED_AUTO_100_TOKEN,
1188 KBD_LED_ON_TOKEN,
1189};
1190
1191static u16 kbd_token_bits;
1192
1193static struct kbd_info kbd_info;
1194static bool kbd_als_supported;
1195static bool kbd_triggers_supported;
1196
1197static u8 kbd_mode_levels[16];
1198static int kbd_mode_levels_count;
1199
1200static u8 kbd_previous_level;
1201static u8 kbd_previous_mode_bit;
1202
1203static bool kbd_led_present;
1204
1205/*
1206 * NOTE: there are three ways to set the keyboard backlight level.
1207 * First, via kbd_state.mode_bit (assigning KBD_MODE_BIT_TRIGGER_* value).
1208 * Second, via kbd_state.level (assigning numerical value <= kbd_info.levels).
1209 * Third, via SMBIOS tokens (KBD_LED_* in kbd_tokens)
1210 *
1211 * There are laptops which support only one of these methods. If we want to
1212 * support as many machines as possible we need to implement all three methods.
1213 * The first two methods use the kbd_state structure. The third uses SMBIOS
1214 * tokens. If kbd_info.levels == 0, the machine does not support setting the
1215 * keyboard backlight level via kbd_state.level.
1216 */
1217
1218static int kbd_get_info(struct kbd_info *info)
1219{
1220 u8 units;
1221 int ret;
1222
1223 get_buffer();
1224
1225 buffer->input[0] = 0x0;
1226 dell_send_request(buffer, 4, 11);
1227 ret = buffer->output[0];
1228
1229 if (ret) {
1230 ret = dell_smi_error(ret);
1231 goto out;
1232 }
1233
1234 info->modes = buffer->output[1] & 0xFFFF;
1235 info->type = (buffer->output[1] >> 24) & 0xFF;
1236 info->triggers = buffer->output[2] & 0xFF;
1237 units = (buffer->output[2] >> 8) & 0xFF;
1238 info->levels = (buffer->output[2] >> 16) & 0xFF;
1239
1240 if (units & BIT(0))
1241 info->seconds = (buffer->output[3] >> 0) & 0xFF;
1242 if (units & BIT(1))
1243 info->minutes = (buffer->output[3] >> 8) & 0xFF;
1244 if (units & BIT(2))
1245 info->hours = (buffer->output[3] >> 16) & 0xFF;
1246 if (units & BIT(3))
1247 info->days = (buffer->output[3] >> 24) & 0xFF;
1248
1249 out:
1250 release_buffer();
1251 return ret;
1252}
1253
1254static unsigned int kbd_get_max_level(void)
1255{
1256 if (kbd_info.levels != 0)
1257 return kbd_info.levels;
1258 if (kbd_mode_levels_count > 0)
1259 return kbd_mode_levels_count - 1;
1260 return 0;
1261}
1262
1263static int kbd_get_level(struct kbd_state *state)
1264{
1265 int i;
1266
1267 if (kbd_info.levels != 0)
1268 return state->level;
1269
1270 if (kbd_mode_levels_count > 0) {
1271 for (i = 0; i < kbd_mode_levels_count; ++i)
1272 if (kbd_mode_levels[i] == state->mode_bit)
1273 return i;
1274 return 0;
1275 }
1276
1277 return -EINVAL;
1278}
1279
1280static int kbd_set_level(struct kbd_state *state, u8 level)
1281{
1282 if (kbd_info.levels != 0) {
1283 if (level != 0)
1284 kbd_previous_level = level;
1285 if (state->level == level)
1286 return 0;
1287 state->level = level;
1288 if (level != 0 && state->mode_bit == KBD_MODE_BIT_OFF)
1289 state->mode_bit = kbd_previous_mode_bit;
1290 else if (level == 0 && state->mode_bit != KBD_MODE_BIT_OFF) {
1291 kbd_previous_mode_bit = state->mode_bit;
1292 state->mode_bit = KBD_MODE_BIT_OFF;
1293 }
1294 return 0;
1295 }
1296
1297 if (kbd_mode_levels_count > 0 && level < kbd_mode_levels_count) {
1298 if (level != 0)
1299 kbd_previous_level = level;
1300 state->mode_bit = kbd_mode_levels[level];
1301 return 0;
1302 }
1303
1304 return -EINVAL;
1305}
1306
1307static int kbd_get_state(struct kbd_state *state)
1308{
1309 int ret;
1310
1311 get_buffer();
1312
1313 buffer->input[0] = 0x1;
1314 dell_send_request(buffer, 4, 11);
1315 ret = buffer->output[0];
1316
1317 if (ret) {
1318 ret = dell_smi_error(ret);
1319 goto out;
1320 }
1321
1322 state->mode_bit = ffs(buffer->output[1] & 0xFFFF);
1323 if (state->mode_bit != 0)
1324 state->mode_bit--;
1325
1326 state->triggers = (buffer->output[1] >> 16) & 0xFF;
1327 state->timeout_value = (buffer->output[1] >> 24) & 0x3F;
1328 state->timeout_unit = (buffer->output[1] >> 30) & 0x3;
1329 state->als_setting = buffer->output[2] & 0xFF;
1330 state->als_value = (buffer->output[2] >> 8) & 0xFF;
1331 state->level = (buffer->output[2] >> 16) & 0xFF;
1332
1333 out:
1334 release_buffer();
1335 return ret;
1336}
1337
1338static int kbd_set_state(struct kbd_state *state)
1339{
1340 int ret;
1341
1342 get_buffer();
1343 buffer->input[0] = 0x2;
1344 buffer->input[1] = BIT(state->mode_bit) & 0xFFFF;
1345 buffer->input[1] |= (state->triggers & 0xFF) << 16;
1346 buffer->input[1] |= (state->timeout_value & 0x3F) << 24;
1347 buffer->input[1] |= (state->timeout_unit & 0x3) << 30;
1348 buffer->input[2] = state->als_setting & 0xFF;
1349 buffer->input[2] |= (state->level & 0xFF) << 16;
1350 dell_send_request(buffer, 4, 11);
1351 ret = buffer->output[0];
1352 release_buffer();
1353
1354 return dell_smi_error(ret);
1355}
1356
1357static int kbd_set_state_safe(struct kbd_state *state, struct kbd_state *old)
1358{
1359 int ret;
1360
1361 ret = kbd_set_state(state);
1362 if (ret == 0)
1363 return 0;
1364
1365 /*
1366 * When setting the new state fails,try to restore the previous one.
1367 * This is needed on some machines where BIOS sets a default state when
1368 * setting a new state fails. This default state could be all off.
1369 */
1370
1371 if (kbd_set_state(old))
1372 pr_err("Setting old previous keyboard state failed\n");
1373
1374 return ret;
1375}
1376
1377static int kbd_set_token_bit(u8 bit)
1378{
1379 int id;
1380 int ret;
1381
1382 if (bit >= ARRAY_SIZE(kbd_tokens))
1383 return -EINVAL;
1384
1385 id = find_token_id(kbd_tokens[bit]);
1386 if (id == -1)
1387 return -EINVAL;
1388
1389 get_buffer();
1390 buffer->input[0] = da_tokens[id].location;
1391 buffer->input[1] = da_tokens[id].value;
1392 dell_send_request(buffer, 1, 0);
1393 ret = buffer->output[0];
1394 release_buffer();
1395
1396 return dell_smi_error(ret);
1397}
1398
1399static int kbd_get_token_bit(u8 bit)
1400{
1401 int id;
1402 int ret;
1403 int val;
1404
1405 if (bit >= ARRAY_SIZE(kbd_tokens))
1406 return -EINVAL;
1407
1408 id = find_token_id(kbd_tokens[bit]);
1409 if (id == -1)
1410 return -EINVAL;
1411
1412 get_buffer();
1413 buffer->input[0] = da_tokens[id].location;
1414 dell_send_request(buffer, 0, 0);
1415 ret = buffer->output[0];
1416 val = buffer->output[1];
1417 release_buffer();
1418
1419 if (ret)
1420 return dell_smi_error(ret);
1421
1422 return (val == da_tokens[id].value);
1423}
1424
1425static int kbd_get_first_active_token_bit(void)
1426{
1427 int i;
1428 int ret;
1429
1430 for (i = 0; i < ARRAY_SIZE(kbd_tokens); ++i) {
1431 ret = kbd_get_token_bit(i);
1432 if (ret == 1)
1433 return i;
1434 }
1435
1436 return ret;
1437}
1438
1439static int kbd_get_valid_token_counts(void)
1440{
1441 return hweight16(kbd_token_bits);
1442}
1443
1444static inline int kbd_init_info(void)
1445{
1446 struct kbd_state state;
1447 int ret;
1448 int i;
1449
1450 ret = kbd_get_info(&kbd_info);
1451 if (ret)
1452 return ret;
1453
1454 kbd_get_state(&state);
1455
1456 /* NOTE: timeout value is stored in 6 bits so max value is 63 */
1457 if (kbd_info.seconds > 63)
1458 kbd_info.seconds = 63;
1459 if (kbd_info.minutes > 63)
1460 kbd_info.minutes = 63;
1461 if (kbd_info.hours > 63)
1462 kbd_info.hours = 63;
1463 if (kbd_info.days > 63)
1464 kbd_info.days = 63;
1465
1466 /* NOTE: On tested machines ON mode did not work and caused
1467 * problems (turned backlight off) so do not use it
1468 */
1469 kbd_info.modes &= ~BIT(KBD_MODE_BIT_ON);
1470
1471 kbd_previous_level = kbd_get_level(&state);
1472 kbd_previous_mode_bit = state.mode_bit;
1473
1474 if (kbd_previous_level == 0 && kbd_get_max_level() != 0)
1475 kbd_previous_level = 1;
1476
1477 if (kbd_previous_mode_bit == KBD_MODE_BIT_OFF) {
1478 kbd_previous_mode_bit =
1479 ffs(kbd_info.modes & ~BIT(KBD_MODE_BIT_OFF));
1480 if (kbd_previous_mode_bit != 0)
1481 kbd_previous_mode_bit--;
1482 }
1483
1484 if (kbd_info.modes & (BIT(KBD_MODE_BIT_ALS) |
1485 BIT(KBD_MODE_BIT_TRIGGER_ALS)))
1486 kbd_als_supported = true;
1487
1488 if (kbd_info.modes & (
1489 BIT(KBD_MODE_BIT_TRIGGER_ALS) | BIT(KBD_MODE_BIT_TRIGGER) |
1490 BIT(KBD_MODE_BIT_TRIGGER_25) | BIT(KBD_MODE_BIT_TRIGGER_50) |
1491 BIT(KBD_MODE_BIT_TRIGGER_75) | BIT(KBD_MODE_BIT_TRIGGER_100)
1492 ))
1493 kbd_triggers_supported = true;
1494
1495 /* kbd_mode_levels[0] is reserved, see below */
1496 for (i = 0; i < 16; ++i)
1497 if (kbd_is_level_mode_bit(i) && (BIT(i) & kbd_info.modes))
1498 kbd_mode_levels[1 + kbd_mode_levels_count++] = i;
1499
1500 /*
1501 * Find the first supported mode and assign to kbd_mode_levels[0].
1502 * This should be 0 (off), but we cannot depend on the BIOS to
1503 * support 0.
1504 */
1505 if (kbd_mode_levels_count > 0) {
1506 for (i = 0; i < 16; ++i) {
1507 if (BIT(i) & kbd_info.modes) {
1508 kbd_mode_levels[0] = i;
1509 break;
1510 }
1511 }
1512 kbd_mode_levels_count++;
1513 }
1514
1515 return 0;
1516
1517}
1518
1519static inline void kbd_init_tokens(void)
1520{
1521 int i;
1522
1523 for (i = 0; i < ARRAY_SIZE(kbd_tokens); ++i)
1524 if (find_token_id(kbd_tokens[i]) != -1)
1525 kbd_token_bits |= BIT(i);
1526}
1527
1528static void kbd_init(void)
1529{
1530 int ret;
1531
1532 ret = kbd_init_info();
1533 kbd_init_tokens();
1534
1535 if (kbd_token_bits != 0 || ret == 0)
1536 kbd_led_present = true;
1537}
1538
1539static ssize_t kbd_led_timeout_store(struct device *dev,
1540 struct device_attribute *attr,
1541 const char *buf, size_t count)
1542{
1543 struct kbd_state new_state;
1544 struct kbd_state state;
1545 bool convert;
1546 int value;
1547 int ret;
1548 char ch;
1549 u8 unit;
1550 int i;
1551
1552 ret = sscanf(buf, "%d %c", &value, &ch);
1553 if (ret < 1)
1554 return -EINVAL;
1555 else if (ret == 1)
1556 ch = 's';
1557
1558 if (value < 0)
1559 return -EINVAL;
1560
1561 convert = false;
1562
1563 switch (ch) {
1564 case 's':
1565 if (value > kbd_info.seconds)
1566 convert = true;
1567 unit = KBD_TIMEOUT_SECONDS;
1568 break;
1569 case 'm':
1570 if (value > kbd_info.minutes)
1571 convert = true;
1572 unit = KBD_TIMEOUT_MINUTES;
1573 break;
1574 case 'h':
1575 if (value > kbd_info.hours)
1576 convert = true;
1577 unit = KBD_TIMEOUT_HOURS;
1578 break;
1579 case 'd':
1580 if (value > kbd_info.days)
1581 convert = true;
1582 unit = KBD_TIMEOUT_DAYS;
1583 break;
1584 default:
1585 return -EINVAL;
1586 }
1587
1588 if (quirks && quirks->needs_kbd_timeouts)
1589 convert = true;
1590
1591 if (convert) {
1592 /* Convert value from current units to seconds */
1593 switch (unit) {
1594 case KBD_TIMEOUT_DAYS:
1595 value *= 24;
1596 case KBD_TIMEOUT_HOURS:
1597 value *= 60;
1598 case KBD_TIMEOUT_MINUTES:
1599 value *= 60;
1600 unit = KBD_TIMEOUT_SECONDS;
1601 }
1602
1603 if (quirks && quirks->needs_kbd_timeouts) {
1604 for (i = 0; quirks->kbd_timeouts[i] != -1; i++) {
1605 if (value <= quirks->kbd_timeouts[i]) {
1606 value = quirks->kbd_timeouts[i];
1607 break;
1608 }
1609 }
1610 }
1611
1612 if (value <= kbd_info.seconds && kbd_info.seconds) {
1613 unit = KBD_TIMEOUT_SECONDS;
1614 } else if (value / 60 <= kbd_info.minutes && kbd_info.minutes) {
1615 value /= 60;
1616 unit = KBD_TIMEOUT_MINUTES;
1617 } else if (value / (60 * 60) <= kbd_info.hours && kbd_info.hours) {
1618 value /= (60 * 60);
1619 unit = KBD_TIMEOUT_HOURS;
1620 } else if (value / (60 * 60 * 24) <= kbd_info.days && kbd_info.days) {
1621 value /= (60 * 60 * 24);
1622 unit = KBD_TIMEOUT_DAYS;
1623 } else {
1624 return -EINVAL;
1625 }
1626 }
1627
1628 ret = kbd_get_state(&state);
1629 if (ret)
1630 return ret;
1631
1632 new_state = state;
1633 new_state.timeout_value = value;
1634 new_state.timeout_unit = unit;
1635
1636 ret = kbd_set_state_safe(&new_state, &state);
1637 if (ret)
1638 return ret;
1639
1640 return count;
1641}
1642
1643static ssize_t kbd_led_timeout_show(struct device *dev,
1644 struct device_attribute *attr, char *buf)
1645{
1646 struct kbd_state state;
1647 int ret;
1648 int len;
1649
1650 ret = kbd_get_state(&state);
1651 if (ret)
1652 return ret;
1653
1654 len = sprintf(buf, "%d", state.timeout_value);
1655
1656 switch (state.timeout_unit) {
1657 case KBD_TIMEOUT_SECONDS:
1658 return len + sprintf(buf+len, "s\n");
1659 case KBD_TIMEOUT_MINUTES:
1660 return len + sprintf(buf+len, "m\n");
1661 case KBD_TIMEOUT_HOURS:
1662 return len + sprintf(buf+len, "h\n");
1663 case KBD_TIMEOUT_DAYS:
1664 return len + sprintf(buf+len, "d\n");
1665 default:
1666 return -EINVAL;
1667 }
1668
1669 return len;
1670}
1671
1672static DEVICE_ATTR(stop_timeout, S_IRUGO | S_IWUSR,
1673 kbd_led_timeout_show, kbd_led_timeout_store);
1674
1675static const char * const kbd_led_triggers[] = {
1676 "keyboard",
1677 "touchpad",
1678 /*"trackstick"*/ NULL, /* NOTE: trackstick is just alias for touchpad */
1679 "mouse",
1680};
1681
1682static ssize_t kbd_led_triggers_store(struct device *dev,
1683 struct device_attribute *attr,
1684 const char *buf, size_t count)
1685{
1686 struct kbd_state new_state;
1687 struct kbd_state state;
1688 bool triggers_enabled = false;
1689 int trigger_bit = -1;
1690 char trigger[21];
1691 int i, ret;
1692
1693 ret = sscanf(buf, "%20s", trigger);
1694 if (ret != 1)
1695 return -EINVAL;
1696
1697 if (trigger[0] != '+' && trigger[0] != '-')
1698 return -EINVAL;
1699
1700 ret = kbd_get_state(&state);
1701 if (ret)
1702 return ret;
1703
1704 if (kbd_triggers_supported)
1705 triggers_enabled = kbd_is_trigger_mode_bit(state.mode_bit);
1706
1707 if (kbd_triggers_supported) {
1708 for (i = 0; i < ARRAY_SIZE(kbd_led_triggers); ++i) {
1709 if (!(kbd_info.triggers & BIT(i)))
1710 continue;
1711 if (!kbd_led_triggers[i])
1712 continue;
1713 if (strcmp(trigger+1, kbd_led_triggers[i]) != 0)
1714 continue;
1715 if (trigger[0] == '+' &&
1716 triggers_enabled && (state.triggers & BIT(i)))
1717 return count;
1718 if (trigger[0] == '-' &&
1719 (!triggers_enabled || !(state.triggers & BIT(i))))
1720 return count;
1721 trigger_bit = i;
1722 break;
1723 }
1724 }
1725
1726 if (trigger_bit != -1) {
1727 new_state = state;
1728 if (trigger[0] == '+')
1729 new_state.triggers |= BIT(trigger_bit);
1730 else {
1731 new_state.triggers &= ~BIT(trigger_bit);
1732 /* NOTE: trackstick bit (2) must be disabled when
1733 * disabling touchpad bit (1), otherwise touchpad
1734 * bit (1) will not be disabled */
1735 if (trigger_bit == 1)
1736 new_state.triggers &= ~BIT(2);
1737 }
1738 if ((kbd_info.triggers & new_state.triggers) !=
1739 new_state.triggers)
1740 return -EINVAL;
1741 if (new_state.triggers && !triggers_enabled) {
1742 new_state.mode_bit = KBD_MODE_BIT_TRIGGER;
1743 kbd_set_level(&new_state, kbd_previous_level);
1744 } else if (new_state.triggers == 0) {
1745 kbd_set_level(&new_state, 0);
1746 }
1747 if (!(kbd_info.modes & BIT(new_state.mode_bit)))
1748 return -EINVAL;
1749 ret = kbd_set_state_safe(&new_state, &state);
1750 if (ret)
1751 return ret;
1752 if (new_state.mode_bit != KBD_MODE_BIT_OFF)
1753 kbd_previous_mode_bit = new_state.mode_bit;
1754 return count;
1755 }
1756
1757 return -EINVAL;
1758}
1759
1760static ssize_t kbd_led_triggers_show(struct device *dev,
1761 struct device_attribute *attr, char *buf)
1762{
1763 struct kbd_state state;
1764 bool triggers_enabled;
1765 int level, i, ret;
1766 int len = 0;
1767
1768 ret = kbd_get_state(&state);
1769 if (ret)
1770 return ret;
1771
1772 len = 0;
1773
1774 if (kbd_triggers_supported) {
1775 triggers_enabled = kbd_is_trigger_mode_bit(state.mode_bit);
1776 level = kbd_get_level(&state);
1777 for (i = 0; i < ARRAY_SIZE(kbd_led_triggers); ++i) {
1778 if (!(kbd_info.triggers & BIT(i)))
1779 continue;
1780 if (!kbd_led_triggers[i])
1781 continue;
1782 if ((triggers_enabled || level <= 0) &&
1783 (state.triggers & BIT(i)))
1784 buf[len++] = '+';
1785 else
1786 buf[len++] = '-';
1787 len += sprintf(buf+len, "%s ", kbd_led_triggers[i]);
1788 }
1789 }
1790
1791 if (len)
1792 buf[len - 1] = '\n';
1793
1794 return len;
1795}
1796
1797static DEVICE_ATTR(start_triggers, S_IRUGO | S_IWUSR,
1798 kbd_led_triggers_show, kbd_led_triggers_store);
1799
1800static ssize_t kbd_led_als_enabled_store(struct device *dev,
1801 struct device_attribute *attr,
1802 const char *buf, size_t count)
1803{
1804 struct kbd_state new_state;
1805 struct kbd_state state;
1806 bool triggers_enabled = false;
1807 int enable;
1808 int ret;
1809
1810 ret = kstrtoint(buf, 0, &enable);
1811 if (ret)
1812 return ret;
1813
1814 ret = kbd_get_state(&state);
1815 if (ret)
1816 return ret;
1817
1818 if (enable == kbd_is_als_mode_bit(state.mode_bit))
1819 return count;
1820
1821 new_state = state;
1822
1823 if (kbd_triggers_supported)
1824 triggers_enabled = kbd_is_trigger_mode_bit(state.mode_bit);
1825
1826 if (enable) {
1827 if (triggers_enabled)
1828 new_state.mode_bit = KBD_MODE_BIT_TRIGGER_ALS;
1829 else
1830 new_state.mode_bit = KBD_MODE_BIT_ALS;
1831 } else {
1832 if (triggers_enabled) {
1833 new_state.mode_bit = KBD_MODE_BIT_TRIGGER;
1834 kbd_set_level(&new_state, kbd_previous_level);
1835 } else {
1836 new_state.mode_bit = KBD_MODE_BIT_ON;
1837 }
1838 }
1839 if (!(kbd_info.modes & BIT(new_state.mode_bit)))
1840 return -EINVAL;
1841
1842 ret = kbd_set_state_safe(&new_state, &state);
1843 if (ret)
1844 return ret;
1845 kbd_previous_mode_bit = new_state.mode_bit;
1846
1847 return count;
1848}
1849
1850static ssize_t kbd_led_als_enabled_show(struct device *dev,
1851 struct device_attribute *attr,
1852 char *buf)
1853{
1854 struct kbd_state state;
1855 bool enabled = false;
1856 int ret;
1857
1858 ret = kbd_get_state(&state);
1859 if (ret)
1860 return ret;
1861 enabled = kbd_is_als_mode_bit(state.mode_bit);
1862
1863 return sprintf(buf, "%d\n", enabled ? 1 : 0);
1864}
1865
1866static DEVICE_ATTR(als_enabled, S_IRUGO | S_IWUSR,
1867 kbd_led_als_enabled_show, kbd_led_als_enabled_store);
1868
1869static ssize_t kbd_led_als_setting_store(struct device *dev,
1870 struct device_attribute *attr,
1871 const char *buf, size_t count)
1872{
1873 struct kbd_state state;
1874 struct kbd_state new_state;
1875 u8 setting;
1876 int ret;
1877
1878 ret = kstrtou8(buf, 10, &setting);
1879 if (ret)
1880 return ret;
1881
1882 ret = kbd_get_state(&state);
1883 if (ret)
1884 return ret;
1885
1886 new_state = state;
1887 new_state.als_setting = setting;
1888
1889 ret = kbd_set_state_safe(&new_state, &state);
1890 if (ret)
1891 return ret;
1892
1893 return count;
1894}
1895
1896static ssize_t kbd_led_als_setting_show(struct device *dev,
1897 struct device_attribute *attr,
1898 char *buf)
1899{
1900 struct kbd_state state;
1901 int ret;
1902
1903 ret = kbd_get_state(&state);
1904 if (ret)
1905 return ret;
1906
1907 return sprintf(buf, "%d\n", state.als_setting);
1908}
1909
1910static DEVICE_ATTR(als_setting, S_IRUGO | S_IWUSR,
1911 kbd_led_als_setting_show, kbd_led_als_setting_store);
1912
1913static struct attribute *kbd_led_attrs[] = {
1914 &dev_attr_stop_timeout.attr,
1915 &dev_attr_start_triggers.attr,
1916 NULL,
1917};
1918
1919static const struct attribute_group kbd_led_group = {
1920 .attrs = kbd_led_attrs,
1921};
1922
1923static struct attribute *kbd_led_als_attrs[] = {
1924 &dev_attr_als_enabled.attr,
1925 &dev_attr_als_setting.attr,
1926 NULL,
1927};
1928
1929static const struct attribute_group kbd_led_als_group = {
1930 .attrs = kbd_led_als_attrs,
1931};
1932
1933static const struct attribute_group *kbd_led_groups[] = {
1934 &kbd_led_group,
1935 &kbd_led_als_group,
1936 NULL,
1937};
1938
1939static enum led_brightness kbd_led_level_get(struct led_classdev *led_cdev)
1940{
1941 int ret;
1942 u16 num;
1943 struct kbd_state state;
1944
1945 if (kbd_get_max_level()) {
1946 ret = kbd_get_state(&state);
1947 if (ret)
1948 return 0;
1949 ret = kbd_get_level(&state);
1950 if (ret < 0)
1951 return 0;
1952 return ret;
1953 }
1954
1955 if (kbd_get_valid_token_counts()) {
1956 ret = kbd_get_first_active_token_bit();
1957 if (ret < 0)
1958 return 0;
1959 for (num = kbd_token_bits; num != 0 && ret > 0; --ret)
1960 num &= num - 1; /* clear the first bit set */
1961 if (num == 0)
1962 return 0;
1963 return ffs(num) - 1;
1964 }
1965
1966 pr_warn("Keyboard brightness level control not supported\n");
1967 return 0;
1968}
1969
1970static void kbd_led_level_set(struct led_classdev *led_cdev,
1971 enum led_brightness value)
1972{
1973 struct kbd_state state;
1974 struct kbd_state new_state;
1975 u16 num;
1976
1977 if (kbd_get_max_level()) {
1978 if (kbd_get_state(&state))
1979 return;
1980 new_state = state;
1981 if (kbd_set_level(&new_state, value))
1982 return;
1983 kbd_set_state_safe(&new_state, &state);
1984 return;
1985 }
1986
1987 if (kbd_get_valid_token_counts()) {
1988 for (num = kbd_token_bits; num != 0 && value > 0; --value)
1989 num &= num - 1; /* clear the first bit set */
1990 if (num == 0)
1991 return;
1992 kbd_set_token_bit(ffs(num) - 1);
1993 return;
1994 }
1995
1996 pr_warn("Keyboard brightness level control not supported\n");
1997}
1998
1999static struct led_classdev kbd_led = {
2000 .name = "dell::kbd_backlight",
2001 .brightness_set = kbd_led_level_set,
2002 .brightness_get = kbd_led_level_get,
2003 .groups = kbd_led_groups,
2004};
2005
2006static int __init kbd_led_init(struct device *dev)
2007{
2008 kbd_init();
2009 if (!kbd_led_present)
2010 return -ENODEV;
2011 if (!kbd_als_supported)
2012 kbd_led_groups[1] = NULL;
2013 kbd_led.max_brightness = kbd_get_max_level();
2014 if (!kbd_led.max_brightness) {
2015 kbd_led.max_brightness = kbd_get_valid_token_counts();
2016 if (kbd_led.max_brightness)
2017 kbd_led.max_brightness--;
2018 }
2019 return led_classdev_register(dev, &kbd_led);
2020}
2021
2022static void brightness_set_exit(struct led_classdev *led_cdev,
2023 enum led_brightness value)
2024{
2025 /* Don't change backlight level on exit */
2026};
2027
2028static void kbd_led_exit(void)
2029{
2030 if (!kbd_led_present)
2031 return;
2032 kbd_led.brightness_set = brightness_set_exit;
2033 led_classdev_unregister(&kbd_led);
2034}
2035
Matthew Garrettad8f07c2009-01-07 18:08:56 -08002036static int __init dell_init(void)
2037{
Matthew Garrettad8f07c2009-01-07 18:08:56 -08002038 int max_intensity = 0;
2039 int ret;
2040
2041 if (!dmi_check_system(dell_device_table))
2042 return -ENODEV;
2043
AceLan Kao2d8b90b2011-10-04 16:25:44 +08002044 quirks = NULL;
2045 /* find if this machine support other functions */
2046 dmi_check_system(dell_quirks);
2047
Jean Delvaree7a19c562009-03-30 21:46:44 +02002048 dmi_walk(find_tokens, NULL);
Matthew Garrettad8f07c2009-01-07 18:08:56 -08002049
2050 if (!da_tokens) {
Joe Percheseb889522011-03-29 15:21:37 -07002051 pr_info("Unable to find dmi tokens\n");
Matthew Garrettad8f07c2009-01-07 18:08:56 -08002052 return -ENODEV;
2053 }
2054
Alan Jenkinsada32482009-08-19 15:06:49 +01002055 ret = platform_driver_register(&platform_driver);
2056 if (ret)
2057 goto fail_platform_driver;
2058 platform_device = platform_device_alloc("dell-laptop", -1);
2059 if (!platform_device) {
2060 ret = -ENOMEM;
2061 goto fail_platform_device1;
2062 }
2063 ret = platform_device_add(platform_device);
2064 if (ret)
2065 goto fail_platform_device2;
2066
Stuart Hayes116ee772010-02-10 14:12:13 -05002067 /*
2068 * Allocate buffer below 4GB for SMI data--only 32-bit physical addr
2069 * is passed to SMI handler.
2070 */
2071 bufferpage = alloc_page(GFP_KERNEL | GFP_DMA32);
Wei Yongjun9f208202013-05-09 10:03:02 +08002072 if (!bufferpage) {
2073 ret = -ENOMEM;
Stuart Hayes116ee772010-02-10 14:12:13 -05002074 goto fail_buffer;
Wei Yongjun9f208202013-05-09 10:03:02 +08002075 }
Stuart Hayes116ee772010-02-10 14:12:13 -05002076 buffer = page_address(bufferpage);
Stuart Hayes116ee772010-02-10 14:12:13 -05002077
Hans de Goede4cc8a572013-11-17 14:00:16 +01002078 ret = dell_setup_rfkill();
2079
2080 if (ret) {
2081 pr_warn("Unable to setup rfkill\n");
2082 goto fail_rfkill;
2083 }
2084
AceLan Kao2d8b90b2011-10-04 16:25:44 +08002085 if (quirks && quirks->touchpad_led)
2086 touchpad_led_init(&platform_device->dev);
2087
Gabriele Mazzotta6cff8d62015-02-19 11:58:29 +01002088 kbd_led_init(&platform_device->dev);
2089
Keng-Yu Lin037accf2010-09-28 11:43:31 +08002090 dell_laptop_dir = debugfs_create_dir("dell_laptop", NULL);
Hans de Goede4cc8a572013-11-17 14:00:16 +01002091 if (dell_laptop_dir != NULL)
2092 debugfs_create_file("rfkill", 0444, dell_laptop_dir, NULL,
2093 &dell_debugfs_fops);
Keng-Yu Lin037accf2010-09-28 11:43:31 +08002094
Matthew Garrettad8f07c2009-01-07 18:08:56 -08002095#ifdef CONFIG_ACPI
2096 /* In the event of an ACPI backlight being available, don't
2097 * register the platform controller.
2098 */
2099 if (acpi_video_backlight_support())
2100 return 0;
2101#endif
2102
Stuart Hayes116ee772010-02-10 14:12:13 -05002103 get_buffer();
2104 buffer->input[0] = find_token_location(BRIGHTNESS_TOKEN);
2105 if (buffer->input[0] != -1) {
2106 dell_send_request(buffer, 0, 2);
2107 max_intensity = buffer->output[3];
Matthew Garrettad8f07c2009-01-07 18:08:56 -08002108 }
Stuart Hayes116ee772010-02-10 14:12:13 -05002109 release_buffer();
Matthew Garrettad8f07c2009-01-07 18:08:56 -08002110
2111 if (max_intensity) {
Matthew Garretta19a6ee2010-02-17 16:39:44 -05002112 struct backlight_properties props;
2113 memset(&props, 0, sizeof(struct backlight_properties));
Matthew Garrettbb7ca742011-03-22 16:30:21 -07002114 props.type = BACKLIGHT_PLATFORM;
Matthew Garretta19a6ee2010-02-17 16:39:44 -05002115 props.max_brightness = max_intensity;
2116 dell_backlight_device = backlight_device_register("dell_backlight",
2117 &platform_device->dev,
2118 NULL,
2119 &dell_ops,
2120 &props);
Matthew Garrettad8f07c2009-01-07 18:08:56 -08002121
2122 if (IS_ERR(dell_backlight_device)) {
2123 ret = PTR_ERR(dell_backlight_device);
2124 dell_backlight_device = NULL;
Alan Jenkins71e9dc72009-08-19 15:06:47 +01002125 goto fail_backlight;
Matthew Garrettad8f07c2009-01-07 18:08:56 -08002126 }
2127
Matthew Garrettad8f07c2009-01-07 18:08:56 -08002128 dell_backlight_device->props.brightness =
2129 dell_get_intensity(dell_backlight_device);
2130 backlight_update_status(dell_backlight_device);
2131 }
2132
2133 return 0;
Alan Jenkins71e9dc72009-08-19 15:06:47 +01002134
2135fail_backlight:
Hans de Goede4cc8a572013-11-17 14:00:16 +01002136 dell_cleanup_rfkill();
2137fail_rfkill:
Stuart Hayes116ee772010-02-10 14:12:13 -05002138 free_page((unsigned long)bufferpage);
2139fail_buffer:
Alan Jenkinsada32482009-08-19 15:06:49 +01002140 platform_device_del(platform_device);
2141fail_platform_device2:
2142 platform_device_put(platform_device);
2143fail_platform_device1:
2144 platform_driver_unregister(&platform_driver);
2145fail_platform_driver:
Matthew Garrettad8f07c2009-01-07 18:08:56 -08002146 kfree(da_tokens);
2147 return ret;
2148}
2149
2150static void __exit dell_exit(void)
2151{
Keng-Yu Lin037accf2010-09-28 11:43:31 +08002152 debugfs_remove_recursive(dell_laptop_dir);
AceLan Kao2d8b90b2011-10-04 16:25:44 +08002153 if (quirks && quirks->touchpad_led)
2154 touchpad_led_exit();
Gabriele Mazzotta6cff8d62015-02-19 11:58:29 +01002155 kbd_led_exit();
Matthew Garrettad8f07c2009-01-07 18:08:56 -08002156 backlight_device_unregister(dell_backlight_device);
Hans de Goede4cc8a572013-11-17 14:00:16 +01002157 dell_cleanup_rfkill();
Matthew Garrettfacd61d2010-02-09 14:03:04 -05002158 if (platform_device) {
Matthew Garrett92e00e42010-03-01 09:46:43 -05002159 platform_device_unregister(platform_device);
Matthew Garrettfacd61d2010-02-09 14:03:04 -05002160 platform_driver_unregister(&platform_driver);
2161 }
Matthew Garrette5512602010-02-09 14:05:01 -05002162 kfree(da_tokens);
Stuart Hayes116ee772010-02-10 14:12:13 -05002163 free_page((unsigned long)buffer);
Matthew Garrettad8f07c2009-01-07 18:08:56 -08002164}
2165
Pali Rohárf8358572015-06-06 10:23:30 +02002166/* dell-rbtn.c driver export functions which will not work correctly (and could
2167 * cause kernel crash) if they are called before dell-rbtn.c init code. This is
2168 * not problem when dell-rbtn.c is compiled as external module. When both files
2169 * (dell-rbtn.c and dell-laptop.c) are compiled statically into kernel, then we
2170 * need to ensure that dell_init() will be called after initializing dell-rbtn.
2171 * This can be achieved by late_initcall() instead module_init().
2172 */
2173late_initcall(dell_init);
Matthew Garrettad8f07c2009-01-07 18:08:56 -08002174module_exit(dell_exit);
2175
2176MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
Gabriele Mazzotta6cff8d62015-02-19 11:58:29 +01002177MODULE_AUTHOR("Gabriele Mazzotta <gabriele.mzt@gmail.com>");
2178MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
Matthew Garrettad8f07c2009-01-07 18:08:56 -08002179MODULE_DESCRIPTION("Dell laptop driver");
2180MODULE_LICENSE("GPL");