blob: 48fabf6b6ecb6a8a94986a44adfdab32ed188f5b [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>
5 *
6 * Based on documentation in the libsmbios package, Copyright (C) 2005 Dell
7 * Inc.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
Joe Percheseb889522011-03-29 15:21:37 -070014#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15
Matthew Garrettad8f07c2009-01-07 18:08:56 -080016#include <linux/module.h>
17#include <linux/kernel.h>
18#include <linux/init.h>
19#include <linux/platform_device.h>
20#include <linux/backlight.h>
21#include <linux/err.h>
22#include <linux/dmi.h>
23#include <linux/io.h>
Hans de Goede4cc8a572013-11-17 14:00:16 +010024#include <linux/rfkill.h>
Matthew Garrettad8f07c2009-01-07 18:08:56 -080025#include <linux/power_supply.h>
26#include <linux/acpi.h>
Stuart Hayes116ee772010-02-10 14:12:13 -050027#include <linux/mm.h>
Matthew Garrett814cb8a2009-12-09 18:23:36 +000028#include <linux/i8042.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Keng-Yu Lin037accf2010-09-28 11:43:31 +080030#include <linux/debugfs.h>
31#include <linux/seq_file.h>
Len Browncad73122009-01-09 17:23:38 -050032#include "../../firmware/dcdbas.h"
Matthew Garrettad8f07c2009-01-07 18:08:56 -080033
34#define BRIGHTNESS_TOKEN 0x7d
35
36/* This structure will be modified by the firmware when we enter
37 * system management mode, hence the volatiles */
38
39struct calling_interface_buffer {
40 u16 class;
41 u16 select;
42 volatile u32 input[4];
43 volatile u32 output[4];
44} __packed;
45
46struct calling_interface_token {
47 u16 tokenID;
48 u16 location;
49 union {
50 u16 value;
51 u16 stringlength;
52 };
53};
54
55struct calling_interface_structure {
56 struct dmi_header header;
57 u16 cmdIOAddress;
58 u8 cmdIOCode;
59 u32 supportedCmds;
60 struct calling_interface_token tokens[];
61} __packed;
62
AceLan Kao2d8b90b2011-10-04 16:25:44 +080063struct quirk_entry {
64 u8 touchpad_led;
65};
66
67static struct quirk_entry *quirks;
68
69static struct quirk_entry quirk_dell_vostro_v130 = {
70 .touchpad_led = 1,
71};
72
73static int dmi_matched(const struct dmi_system_id *dmi)
74{
75 quirks = dmi->driver_data;
76 return 1;
77}
78
Matthew Garrettad8f07c2009-01-07 18:08:56 -080079static int da_command_address;
80static int da_command_code;
81static int da_num_tokens;
82static struct calling_interface_token *da_tokens;
83
Alan Jenkinsada32482009-08-19 15:06:49 +010084static struct platform_driver platform_driver = {
85 .driver = {
86 .name = "dell-laptop",
87 .owner = THIS_MODULE,
88 }
89};
90
91static struct platform_device *platform_device;
Matthew Garrettad8f07c2009-01-07 18:08:56 -080092static struct backlight_device *dell_backlight_device;
Hans de Goede4cc8a572013-11-17 14:00:16 +010093static struct rfkill *wifi_rfkill;
94static struct rfkill *bluetooth_rfkill;
95static struct rfkill *wwan_rfkill;
Matthew Garrettad8f07c2009-01-07 18:08:56 -080096
Uwe Kleine-König145047d2012-03-30 22:05:04 +020097static const struct dmi_system_id dell_device_table[] __initconst = {
Matthew Garrettad8f07c2009-01-07 18:08:56 -080098 {
99 .ident = "Dell laptop",
100 .matches = {
101 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
102 DMI_MATCH(DMI_CHASSIS_TYPE, "8"),
103 },
104 },
Erik Andrencb6a7932010-02-14 11:53:23 -0500105 {
Rezwanul Kabir410d44c2010-06-23 12:02:43 -0500106 .matches = {
107 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
108 DMI_MATCH(DMI_CHASSIS_TYPE, "9"), /*Laptop*/
109 },
110 },
111 {
Erik Andrencb6a7932010-02-14 11:53:23 -0500112 .ident = "Dell Computer Corporation",
113 .matches = {
114 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
115 DMI_MATCH(DMI_CHASSIS_TYPE, "8"),
116 },
117 },
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800118 { }
119};
Dmitry Torokhov35ae64f2011-11-14 00:25:00 -0800120MODULE_DEVICE_TABLE(dmi, dell_device_table);
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800121
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -0800122static struct dmi_system_id dell_quirks[] = {
AceLan Kao2d8b90b2011-10-04 16:25:44 +0800123 {
124 .callback = dmi_matched,
125 .ident = "Dell Vostro V130",
126 .matches = {
127 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
128 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro V130"),
129 },
130 .driver_data = &quirk_dell_vostro_v130,
131 },
132 {
133 .callback = dmi_matched,
134 .ident = "Dell Vostro V131",
135 .matches = {
136 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
137 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro V131"),
138 },
139 .driver_data = &quirk_dell_vostro_v130,
140 },
AceLan Kao2a748852011-11-17 15:30:42 +0800141 {
142 .callback = dmi_matched,
Ang Way Chuang57b31b22012-04-12 13:11:27 +0800143 .ident = "Dell Vostro 3350",
144 .matches = {
145 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
146 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3350"),
147 },
148 .driver_data = &quirk_dell_vostro_v130,
149 },
150 {
151 .callback = dmi_matched,
AceLan Kao2a748852011-11-17 15:30:42 +0800152 .ident = "Dell Vostro 3555",
153 .matches = {
154 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
155 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3555"),
156 },
157 .driver_data = &quirk_dell_vostro_v130,
158 },
159 {
160 .callback = dmi_matched,
161 .ident = "Dell Inspiron N311z",
162 .matches = {
163 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
164 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron N311z"),
165 },
166 .driver_data = &quirk_dell_vostro_v130,
167 },
168 {
169 .callback = dmi_matched,
170 .ident = "Dell Inspiron M5110",
171 .matches = {
172 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
173 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron M5110"),
174 },
175 .driver_data = &quirk_dell_vostro_v130,
176 },
AceLan Kao7f839222012-04-20 11:47:26 +0800177 {
178 .callback = dmi_matched,
179 .ident = "Dell Vostro 3360",
180 .matches = {
181 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
182 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3360"),
183 },
184 .driver_data = &quirk_dell_vostro_v130,
185 },
186 {
187 .callback = dmi_matched,
188 .ident = "Dell Vostro 3460",
189 .matches = {
190 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
191 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3460"),
192 },
193 .driver_data = &quirk_dell_vostro_v130,
194 },
195 {
196 .callback = dmi_matched,
197 .ident = "Dell Vostro 3560",
198 .matches = {
199 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
200 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3560"),
201 },
202 .driver_data = &quirk_dell_vostro_v130,
203 },
AceLan Kaod0e0a4772012-05-22 12:38:51 +0800204 {
205 .callback = dmi_matched,
206 .ident = "Dell Vostro 3450",
207 .matches = {
208 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
209 DMI_MATCH(DMI_PRODUCT_NAME, "Dell System Vostro 3450"),
210 },
211 .driver_data = &quirk_dell_vostro_v130,
212 },
AceLan Kao5f1e88f2012-07-13 16:39:57 +0800213 {
214 .callback = dmi_matched,
215 .ident = "Dell Inspiron 5420",
216 .matches = {
217 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
AceLan Kaoa2174ba2012-08-06 09:48:58 +0800218 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5420"),
AceLan Kao5f1e88f2012-07-13 16:39:57 +0800219 },
220 .driver_data = &quirk_dell_vostro_v130,
221 },
222 {
223 .callback = dmi_matched,
224 .ident = "Dell Inspiron 5520",
225 .matches = {
226 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
AceLan Kaoa2174ba2012-08-06 09:48:58 +0800227 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5520"),
AceLan Kao5f1e88f2012-07-13 16:39:57 +0800228 },
229 .driver_data = &quirk_dell_vostro_v130,
230 },
231 {
232 .callback = dmi_matched,
233 .ident = "Dell Inspiron 5720",
234 .matches = {
235 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
AceLan Kaoa2174ba2012-08-06 09:48:58 +0800236 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5720"),
AceLan Kao5f1e88f2012-07-13 16:39:57 +0800237 },
238 .driver_data = &quirk_dell_vostro_v130,
239 },
240 {
241 .callback = dmi_matched,
242 .ident = "Dell Inspiron 7420",
243 .matches = {
244 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
AceLan Kaoa2174ba2012-08-06 09:48:58 +0800245 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7420"),
AceLan Kao5f1e88f2012-07-13 16:39:57 +0800246 },
247 .driver_data = &quirk_dell_vostro_v130,
248 },
249 {
250 .callback = dmi_matched,
251 .ident = "Dell Inspiron 7520",
252 .matches = {
253 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
AceLan Kaoa2174ba2012-08-06 09:48:58 +0800254 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7520"),
AceLan Kao5f1e88f2012-07-13 16:39:57 +0800255 },
256 .driver_data = &quirk_dell_vostro_v130,
257 },
258 {
259 .callback = dmi_matched,
260 .ident = "Dell Inspiron 7720",
261 .matches = {
262 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
AceLan Kaoa2174ba2012-08-06 09:48:58 +0800263 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7720"),
AceLan Kao5f1e88f2012-07-13 16:39:57 +0800264 },
265 .driver_data = &quirk_dell_vostro_v130,
266 },
Martin Nyhusd62d4212012-03-15 18:25:48 +0100267 { }
AceLan Kao2d8b90b2011-10-04 16:25:44 +0800268};
269
Stuart Hayes116ee772010-02-10 14:12:13 -0500270static struct calling_interface_buffer *buffer;
Ingo Molnar94d8f782010-03-01 09:43:52 -0500271static struct page *bufferpage;
272static DEFINE_MUTEX(buffer_mutex);
Stuart Hayes116ee772010-02-10 14:12:13 -0500273
Matthew Garrettc6760ac2010-02-10 14:44:03 -0500274static int hwswitch_state;
275
Stuart Hayes116ee772010-02-10 14:12:13 -0500276static void get_buffer(void)
277{
278 mutex_lock(&buffer_mutex);
279 memset(buffer, 0, sizeof(struct calling_interface_buffer));
280}
281
282static void release_buffer(void)
283{
284 mutex_unlock(&buffer_mutex);
285}
286
Alan Jenkins4788df42009-08-19 15:06:50 +0100287static void __init parse_da_table(const struct dmi_header *dm)
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800288{
289 /* Final token is a terminator, so we don't want to copy it */
290 int tokens = (dm->length-11)/sizeof(struct calling_interface_token)-1;
David Woodhousefe9ab002013-03-14 13:21:00 +0000291 struct calling_interface_token *new_da_tokens;
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800292 struct calling_interface_structure *table =
293 container_of(dm, struct calling_interface_structure, header);
294
295 /* 4 bytes of table header, plus 7 bytes of Dell header, plus at least
296 6 bytes of entry */
297
298 if (dm->length < 17)
299 return;
300
301 da_command_address = table->cmdIOAddress;
302 da_command_code = table->cmdIOCode;
303
David Woodhousefe9ab002013-03-14 13:21:00 +0000304 new_da_tokens = krealloc(da_tokens, (da_num_tokens + tokens) *
305 sizeof(struct calling_interface_token),
306 GFP_KERNEL);
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800307
David Woodhousefe9ab002013-03-14 13:21:00 +0000308 if (!new_da_tokens)
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800309 return;
David Woodhousefe9ab002013-03-14 13:21:00 +0000310 da_tokens = new_da_tokens;
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800311
312 memcpy(da_tokens+da_num_tokens, table->tokens,
313 sizeof(struct calling_interface_token) * tokens);
314
315 da_num_tokens += tokens;
316}
317
Alan Jenkins4788df42009-08-19 15:06:50 +0100318static void __init find_tokens(const struct dmi_header *dm, void *dummy)
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800319{
320 switch (dm->type) {
321 case 0xd4: /* Indexed IO */
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800322 case 0xd5: /* Protected Area Type 1 */
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800323 case 0xd6: /* Protected Area Type 2 */
324 break;
325 case 0xda: /* Calling interface */
326 parse_da_table(dm);
327 break;
328 }
329}
330
331static int find_token_location(int tokenid)
332{
333 int i;
334 for (i = 0; i < da_num_tokens; i++) {
335 if (da_tokens[i].tokenID == tokenid)
336 return da_tokens[i].location;
337 }
338
339 return -1;
340}
341
342static struct calling_interface_buffer *
343dell_send_request(struct calling_interface_buffer *buffer, int class,
344 int select)
345{
346 struct smi_cmd command;
347
348 command.magic = SMI_CMD_MAGIC;
349 command.command_address = da_command_address;
350 command.command_code = da_command_code;
351 command.ebx = virt_to_phys(buffer);
352 command.ecx = 0x42534931;
353
354 buffer->class = class;
355 buffer->select = select;
356
357 dcdbas_smi_request(&command);
358
359 return buffer;
360}
361
Hans de Goede4cc8a572013-11-17 14:00:16 +0100362/* Derived from information in DellWirelessCtl.cpp:
363 Class 17, select 11 is radio control. It returns an array of 32-bit values.
364
365 Input byte 0 = 0: Wireless information
366
367 result[0]: return code
368 result[1]:
369 Bit 0: Hardware switch supported
370 Bit 1: Wifi locator supported
371 Bit 2: Wifi is supported
372 Bit 3: Bluetooth is supported
373 Bit 4: WWAN is supported
374 Bit 5: Wireless keyboard supported
375 Bits 6-7: Reserved
376 Bit 8: Wifi is installed
377 Bit 9: Bluetooth is installed
378 Bit 10: WWAN is installed
379 Bits 11-15: Reserved
380 Bit 16: Hardware switch is on
381 Bit 17: Wifi is blocked
382 Bit 18: Bluetooth is blocked
383 Bit 19: WWAN is blocked
384 Bits 20-31: Reserved
385 result[2]: NVRAM size in bytes
386 result[3]: NVRAM format version number
387
388 Input byte 0 = 2: Wireless switch configuration
389 result[0]: return code
390 result[1]:
391 Bit 0: Wifi controlled by switch
392 Bit 1: Bluetooth controlled by switch
393 Bit 2: WWAN controlled by switch
394 Bits 3-6: Reserved
395 Bit 7: Wireless switch config locked
396 Bit 8: Wifi locator enabled
397 Bits 9-14: Reserved
398 Bit 15: Wifi locator setting locked
399 Bits 16-31: Reserved
400*/
401
402static int dell_rfkill_set(void *data, bool blocked)
403{
404 int disable = blocked ? 1 : 0;
405 unsigned long radio = (unsigned long)data;
406 int hwswitch_bit = (unsigned long)data - 1;
407 int ret = 0;
408
409 get_buffer();
410 dell_send_request(buffer, 17, 11);
411
412 /* If the hardware switch controls this radio, and the hardware
413 switch is disabled, don't allow changing the software state */
414 if ((hwswitch_state & BIT(hwswitch_bit)) &&
415 !(buffer->output[1] & BIT(16))) {
416 ret = -EINVAL;
417 goto out;
418 }
419
420 buffer->input[0] = (1 | (radio<<8) | (disable << 16));
421 dell_send_request(buffer, 17, 11);
422
423out:
424 release_buffer();
425 return ret;
426}
427
428static void dell_rfkill_query(struct rfkill *rfkill, void *data)
429{
430 int status;
431 int bit = (unsigned long)data + 16;
432 int hwswitch_bit = (unsigned long)data - 1;
433
434 get_buffer();
435 dell_send_request(buffer, 17, 11);
436 status = buffer->output[1];
437 release_buffer();
438
439 rfkill_set_sw_state(rfkill, !!(status & BIT(bit)));
440
441 if (hwswitch_state & (BIT(hwswitch_bit)))
442 rfkill_set_hw_state(rfkill, !(status & BIT(16)));
443}
444
445static const struct rfkill_ops dell_rfkill_ops = {
446 .set_block = dell_rfkill_set,
447 .query = dell_rfkill_query,
448};
449
Keng-Yu Lin037accf2010-09-28 11:43:31 +0800450static struct dentry *dell_laptop_dir;
451
452static int dell_debugfs_show(struct seq_file *s, void *data)
453{
454 int status;
455
456 get_buffer();
457 dell_send_request(buffer, 17, 11);
458 status = buffer->output[1];
459 release_buffer();
460
461 seq_printf(s, "status:\t0x%X\n", status);
462 seq_printf(s, "Bit 0 : Hardware switch supported: %lu\n",
463 status & BIT(0));
464 seq_printf(s, "Bit 1 : Wifi locator supported: %lu\n",
465 (status & BIT(1)) >> 1);
466 seq_printf(s, "Bit 2 : Wifi is supported: %lu\n",
467 (status & BIT(2)) >> 2);
468 seq_printf(s, "Bit 3 : Bluetooth is supported: %lu\n",
469 (status & BIT(3)) >> 3);
470 seq_printf(s, "Bit 4 : WWAN is supported: %lu\n",
471 (status & BIT(4)) >> 4);
472 seq_printf(s, "Bit 5 : Wireless keyboard supported: %lu\n",
473 (status & BIT(5)) >> 5);
474 seq_printf(s, "Bit 8 : Wifi is installed: %lu\n",
475 (status & BIT(8)) >> 8);
476 seq_printf(s, "Bit 9 : Bluetooth is installed: %lu\n",
477 (status & BIT(9)) >> 9);
478 seq_printf(s, "Bit 10: WWAN is installed: %lu\n",
479 (status & BIT(10)) >> 10);
480 seq_printf(s, "Bit 16: Hardware switch is on: %lu\n",
481 (status & BIT(16)) >> 16);
482 seq_printf(s, "Bit 17: Wifi is blocked: %lu\n",
483 (status & BIT(17)) >> 17);
484 seq_printf(s, "Bit 18: Bluetooth is blocked: %lu\n",
485 (status & BIT(18)) >> 18);
486 seq_printf(s, "Bit 19: WWAN is blocked: %lu\n",
487 (status & BIT(19)) >> 19);
488
489 seq_printf(s, "\nhwswitch_state:\t0x%X\n", hwswitch_state);
490 seq_printf(s, "Bit 0 : Wifi controlled by switch: %lu\n",
491 hwswitch_state & BIT(0));
492 seq_printf(s, "Bit 1 : Bluetooth controlled by switch: %lu\n",
493 (hwswitch_state & BIT(1)) >> 1);
494 seq_printf(s, "Bit 2 : WWAN controlled by switch: %lu\n",
495 (hwswitch_state & BIT(2)) >> 2);
496 seq_printf(s, "Bit 7 : Wireless switch config locked: %lu\n",
497 (hwswitch_state & BIT(7)) >> 7);
498 seq_printf(s, "Bit 8 : Wifi locator enabled: %lu\n",
499 (hwswitch_state & BIT(8)) >> 8);
500 seq_printf(s, "Bit 15: Wifi locator setting locked: %lu\n",
501 (hwswitch_state & BIT(15)) >> 15);
502
503 return 0;
504}
505
506static int dell_debugfs_open(struct inode *inode, struct file *file)
507{
508 return single_open(file, dell_debugfs_show, inode->i_private);
509}
510
511static const struct file_operations dell_debugfs_fops = {
512 .owner = THIS_MODULE,
513 .open = dell_debugfs_open,
514 .read = seq_read,
515 .llseek = seq_lseek,
516 .release = single_release,
517};
518
Hans de Goede4cc8a572013-11-17 14:00:16 +0100519static void dell_update_rfkill(struct work_struct *ignored)
520{
521 if (wifi_rfkill)
522 dell_rfkill_query(wifi_rfkill, (void *)1);
523 if (bluetooth_rfkill)
524 dell_rfkill_query(bluetooth_rfkill, (void *)2);
525 if (wwan_rfkill)
526 dell_rfkill_query(wwan_rfkill, (void *)3);
527}
528static DECLARE_DELAYED_WORK(dell_rfkill_work, dell_update_rfkill);
529
530
531static int __init dell_setup_rfkill(void)
532{
533 int status;
534 int ret;
Hans de Goede2a925512013-11-17 14:00:17 +0100535 const char *product;
Hans de Goede4cc8a572013-11-17 14:00:16 +0100536
Hans de Goede2a925512013-11-17 14:00:17 +0100537 /*
538 * rfkill causes trouble on various non Latitudes, according to Dell
539 * actually testing the rfkill functionality is only done on Latitudes.
540 */
541 product = dmi_get_system_info(DMI_PRODUCT_NAME);
542 if (!product || strncmp(product, "Latitude", 8))
Hans de Goede4cc8a572013-11-17 14:00:16 +0100543 return 0;
Hans de Goede4cc8a572013-11-17 14:00:16 +0100544
545 get_buffer();
546 dell_send_request(buffer, 17, 11);
547 status = buffer->output[1];
548 buffer->input[0] = 0x2;
549 dell_send_request(buffer, 17, 11);
550 hwswitch_state = buffer->output[1];
Hans de Goededdde7082013-11-17 14:00:18 +0100551 /* If there is no hwswitch, then clear all hw-controlled bits */
552 if (!(status & BIT(0)))
553 hwswitch_state &= ~7;
Hans de Goede4cc8a572013-11-17 14:00:16 +0100554 release_buffer();
555
556 if ((status & (1<<2|1<<8)) == (1<<2|1<<8)) {
557 wifi_rfkill = rfkill_alloc("dell-wifi", &platform_device->dev,
558 RFKILL_TYPE_WLAN,
559 &dell_rfkill_ops, (void *) 1);
560 if (!wifi_rfkill) {
561 ret = -ENOMEM;
562 goto err_wifi;
563 }
564 ret = rfkill_register(wifi_rfkill);
565 if (ret)
566 goto err_wifi;
567 }
568
569 if ((status & (1<<3|1<<9)) == (1<<3|1<<9)) {
570 bluetooth_rfkill = rfkill_alloc("dell-bluetooth",
571 &platform_device->dev,
572 RFKILL_TYPE_BLUETOOTH,
573 &dell_rfkill_ops, (void *) 2);
574 if (!bluetooth_rfkill) {
575 ret = -ENOMEM;
576 goto err_bluetooth;
577 }
578 ret = rfkill_register(bluetooth_rfkill);
579 if (ret)
580 goto err_bluetooth;
581 }
582
583 if ((status & (1<<4|1<<10)) == (1<<4|1<<10)) {
584 wwan_rfkill = rfkill_alloc("dell-wwan",
585 &platform_device->dev,
586 RFKILL_TYPE_WWAN,
587 &dell_rfkill_ops, (void *) 3);
588 if (!wwan_rfkill) {
589 ret = -ENOMEM;
590 goto err_wwan;
591 }
592 ret = rfkill_register(wwan_rfkill);
593 if (ret)
594 goto err_wwan;
595 }
596
597 return 0;
598err_wwan:
599 rfkill_destroy(wwan_rfkill);
600 if (bluetooth_rfkill)
601 rfkill_unregister(bluetooth_rfkill);
602err_bluetooth:
603 rfkill_destroy(bluetooth_rfkill);
604 if (wifi_rfkill)
605 rfkill_unregister(wifi_rfkill);
606err_wifi:
607 rfkill_destroy(wifi_rfkill);
608
609 return ret;
610}
611
612static void dell_cleanup_rfkill(void)
613{
614 if (wifi_rfkill) {
615 rfkill_unregister(wifi_rfkill);
616 rfkill_destroy(wifi_rfkill);
617 }
618 if (bluetooth_rfkill) {
619 rfkill_unregister(bluetooth_rfkill);
620 rfkill_destroy(bluetooth_rfkill);
621 }
622 if (wwan_rfkill) {
623 rfkill_unregister(wwan_rfkill);
624 rfkill_destroy(wwan_rfkill);
625 }
626}
627
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800628static int dell_send_intensity(struct backlight_device *bd)
629{
Stuart Hayes116ee772010-02-10 14:12:13 -0500630 int ret = 0;
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800631
Stuart Hayes116ee772010-02-10 14:12:13 -0500632 get_buffer();
633 buffer->input[0] = find_token_location(BRIGHTNESS_TOKEN);
634 buffer->input[1] = bd->props.brightness;
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800635
Stuart Hayes116ee772010-02-10 14:12:13 -0500636 if (buffer->input[0] == -1) {
637 ret = -ENODEV;
638 goto out;
639 }
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800640
641 if (power_supply_is_system_supplied() > 0)
Stuart Hayes116ee772010-02-10 14:12:13 -0500642 dell_send_request(buffer, 1, 2);
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800643 else
Stuart Hayes116ee772010-02-10 14:12:13 -0500644 dell_send_request(buffer, 1, 1);
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800645
Stuart Hayes116ee772010-02-10 14:12:13 -0500646out:
647 release_buffer();
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800648 return 0;
649}
650
651static int dell_get_intensity(struct backlight_device *bd)
652{
Stuart Hayes116ee772010-02-10 14:12:13 -0500653 int ret = 0;
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800654
Stuart Hayes116ee772010-02-10 14:12:13 -0500655 get_buffer();
656 buffer->input[0] = find_token_location(BRIGHTNESS_TOKEN);
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800657
Stuart Hayes116ee772010-02-10 14:12:13 -0500658 if (buffer->input[0] == -1) {
659 ret = -ENODEV;
660 goto out;
661 }
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800662
663 if (power_supply_is_system_supplied() > 0)
Stuart Hayes116ee772010-02-10 14:12:13 -0500664 dell_send_request(buffer, 0, 2);
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800665 else
Stuart Hayes116ee772010-02-10 14:12:13 -0500666 dell_send_request(buffer, 0, 1);
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800667
Jose Alonsob4867422011-07-10 15:46:51 -0300668 ret = buffer->output[1];
669
Stuart Hayes116ee772010-02-10 14:12:13 -0500670out:
671 release_buffer();
Jose Alonsob4867422011-07-10 15:46:51 -0300672 return ret;
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800673}
674
Lionel Debrouxacc24722010-11-16 14:14:02 +0100675static const struct backlight_ops dell_ops = {
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800676 .get_brightness = dell_get_intensity,
677 .update_status = dell_send_intensity,
678};
679
Randy Dunlap869f8df2011-11-16 18:20:51 -0800680static void touchpad_led_on(void)
AceLan Kao2d8b90b2011-10-04 16:25:44 +0800681{
682 int command = 0x97;
683 char data = 1;
684 i8042_command(&data, command | 1 << 12);
685}
686
Randy Dunlap869f8df2011-11-16 18:20:51 -0800687static void touchpad_led_off(void)
AceLan Kao2d8b90b2011-10-04 16:25:44 +0800688{
689 int command = 0x97;
690 char data = 2;
691 i8042_command(&data, command | 1 << 12);
692}
693
694static void touchpad_led_set(struct led_classdev *led_cdev,
695 enum led_brightness value)
696{
697 if (value > 0)
698 touchpad_led_on();
699 else
700 touchpad_led_off();
701}
702
703static struct led_classdev touchpad_led = {
704 .name = "dell-laptop::touchpad",
705 .brightness_set = touchpad_led_set,
AceLan Kao2d5de9e2012-01-17 16:18:06 +0800706 .flags = LED_CORE_SUSPENDRESUME,
AceLan Kao2d8b90b2011-10-04 16:25:44 +0800707};
708
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -0800709static int touchpad_led_init(struct device *dev)
AceLan Kao2d8b90b2011-10-04 16:25:44 +0800710{
711 return led_classdev_register(dev, &touchpad_led);
712}
713
714static void touchpad_led_exit(void)
715{
716 led_classdev_unregister(&touchpad_led);
717}
718
Hans de Goede4cc8a572013-11-17 14:00:16 +0100719static bool dell_laptop_i8042_filter(unsigned char data, unsigned char str,
720 struct serio *port)
721{
722 static bool extended;
723
724 if (str & 0x20)
725 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));
735 break;
736 }
737 extended = false;
738 }
739
740 return false;
741}
742
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800743static int __init dell_init(void)
744{
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800745 int max_intensity = 0;
746 int ret;
747
748 if (!dmi_check_system(dell_device_table))
749 return -ENODEV;
750
AceLan Kao2d8b90b2011-10-04 16:25:44 +0800751 quirks = NULL;
752 /* find if this machine support other functions */
753 dmi_check_system(dell_quirks);
754
Jean Delvaree7a19c562009-03-30 21:46:44 +0200755 dmi_walk(find_tokens, NULL);
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800756
757 if (!da_tokens) {
Joe Percheseb889522011-03-29 15:21:37 -0700758 pr_info("Unable to find dmi tokens\n");
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800759 return -ENODEV;
760 }
761
Alan Jenkinsada32482009-08-19 15:06:49 +0100762 ret = platform_driver_register(&platform_driver);
763 if (ret)
764 goto fail_platform_driver;
765 platform_device = platform_device_alloc("dell-laptop", -1);
766 if (!platform_device) {
767 ret = -ENOMEM;
768 goto fail_platform_device1;
769 }
770 ret = platform_device_add(platform_device);
771 if (ret)
772 goto fail_platform_device2;
773
Stuart Hayes116ee772010-02-10 14:12:13 -0500774 /*
775 * Allocate buffer below 4GB for SMI data--only 32-bit physical addr
776 * is passed to SMI handler.
777 */
778 bufferpage = alloc_page(GFP_KERNEL | GFP_DMA32);
Wei Yongjun9f208202013-05-09 10:03:02 +0800779 if (!bufferpage) {
780 ret = -ENOMEM;
Stuart Hayes116ee772010-02-10 14:12:13 -0500781 goto fail_buffer;
Wei Yongjun9f208202013-05-09 10:03:02 +0800782 }
Stuart Hayes116ee772010-02-10 14:12:13 -0500783 buffer = page_address(bufferpage);
Stuart Hayes116ee772010-02-10 14:12:13 -0500784
Hans de Goede4cc8a572013-11-17 14:00:16 +0100785 ret = dell_setup_rfkill();
786
787 if (ret) {
788 pr_warn("Unable to setup rfkill\n");
789 goto fail_rfkill;
790 }
791
792 ret = i8042_install_filter(dell_laptop_i8042_filter);
793 if (ret) {
794 pr_warn("Unable to install key filter\n");
795 goto fail_filter;
796 }
797
AceLan Kao2d8b90b2011-10-04 16:25:44 +0800798 if (quirks && quirks->touchpad_led)
799 touchpad_led_init(&platform_device->dev);
800
Keng-Yu Lin037accf2010-09-28 11:43:31 +0800801 dell_laptop_dir = debugfs_create_dir("dell_laptop", NULL);
Hans de Goede4cc8a572013-11-17 14:00:16 +0100802 if (dell_laptop_dir != NULL)
803 debugfs_create_file("rfkill", 0444, dell_laptop_dir, NULL,
804 &dell_debugfs_fops);
Keng-Yu Lin037accf2010-09-28 11:43:31 +0800805
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800806#ifdef CONFIG_ACPI
807 /* In the event of an ACPI backlight being available, don't
808 * register the platform controller.
809 */
810 if (acpi_video_backlight_support())
811 return 0;
812#endif
813
Stuart Hayes116ee772010-02-10 14:12:13 -0500814 get_buffer();
815 buffer->input[0] = find_token_location(BRIGHTNESS_TOKEN);
816 if (buffer->input[0] != -1) {
817 dell_send_request(buffer, 0, 2);
818 max_intensity = buffer->output[3];
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800819 }
Stuart Hayes116ee772010-02-10 14:12:13 -0500820 release_buffer();
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800821
822 if (max_intensity) {
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500823 struct backlight_properties props;
824 memset(&props, 0, sizeof(struct backlight_properties));
Matthew Garrettbb7ca742011-03-22 16:30:21 -0700825 props.type = BACKLIGHT_PLATFORM;
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500826 props.max_brightness = max_intensity;
827 dell_backlight_device = backlight_device_register("dell_backlight",
828 &platform_device->dev,
829 NULL,
830 &dell_ops,
831 &props);
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800832
833 if (IS_ERR(dell_backlight_device)) {
834 ret = PTR_ERR(dell_backlight_device);
835 dell_backlight_device = NULL;
Alan Jenkins71e9dc72009-08-19 15:06:47 +0100836 goto fail_backlight;
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800837 }
838
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800839 dell_backlight_device->props.brightness =
840 dell_get_intensity(dell_backlight_device);
841 backlight_update_status(dell_backlight_device);
842 }
843
844 return 0;
Alan Jenkins71e9dc72009-08-19 15:06:47 +0100845
846fail_backlight:
Hans de Goede4cc8a572013-11-17 14:00:16 +0100847 i8042_remove_filter(dell_laptop_i8042_filter);
848 cancel_delayed_work_sync(&dell_rfkill_work);
849fail_filter:
850 dell_cleanup_rfkill();
851fail_rfkill:
Stuart Hayes116ee772010-02-10 14:12:13 -0500852 free_page((unsigned long)bufferpage);
853fail_buffer:
Alan Jenkinsada32482009-08-19 15:06:49 +0100854 platform_device_del(platform_device);
855fail_platform_device2:
856 platform_device_put(platform_device);
857fail_platform_device1:
858 platform_driver_unregister(&platform_driver);
859fail_platform_driver:
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800860 kfree(da_tokens);
861 return ret;
862}
863
864static void __exit dell_exit(void)
865{
Keng-Yu Lin037accf2010-09-28 11:43:31 +0800866 debugfs_remove_recursive(dell_laptop_dir);
AceLan Kao2d8b90b2011-10-04 16:25:44 +0800867 if (quirks && quirks->touchpad_led)
868 touchpad_led_exit();
Hans de Goede4cc8a572013-11-17 14:00:16 +0100869 i8042_remove_filter(dell_laptop_i8042_filter);
870 cancel_delayed_work_sync(&dell_rfkill_work);
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800871 backlight_device_unregister(dell_backlight_device);
Hans de Goede4cc8a572013-11-17 14:00:16 +0100872 dell_cleanup_rfkill();
Matthew Garrettfacd61d2010-02-09 14:03:04 -0500873 if (platform_device) {
Matthew Garrett92e00e42010-03-01 09:46:43 -0500874 platform_device_unregister(platform_device);
Matthew Garrettfacd61d2010-02-09 14:03:04 -0500875 platform_driver_unregister(&platform_driver);
876 }
Matthew Garrette5512602010-02-09 14:05:01 -0500877 kfree(da_tokens);
Stuart Hayes116ee772010-02-10 14:12:13 -0500878 free_page((unsigned long)buffer);
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800879}
880
881module_init(dell_init);
882module_exit(dell_exit);
883
884MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
885MODULE_DESCRIPTION("Dell laptop driver");
886MODULE_LICENSE("GPL");