blob: c188d43b93c56246cd76e53989941a9981513630 [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>
24#include <linux/rfkill.h>
25#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;
93static struct rfkill *wifi_rfkill;
94static struct rfkill *bluetooth_rfkill;
95static struct rfkill *wwan_rfkill;
96
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
Mario Limoncielloe5fefd02010-02-09 17:41:03 -0500122static struct dmi_system_id __devinitdata dell_blacklist[] = {
123 /* Supported by compal-laptop */
124 {
125 .ident = "Dell Mini 9",
126 .matches = {
127 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
128 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 910"),
129 },
130 },
131 {
132 .ident = "Dell Mini 10",
133 .matches = {
134 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
135 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 1010"),
136 },
137 },
138 {
139 .ident = "Dell Mini 10v",
140 .matches = {
141 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
142 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 1011"),
143 },
144 },
145 {
Victor van den Elzenc3f755e2010-08-15 01:19:33 +0200146 .ident = "Dell Mini 1012",
147 .matches = {
148 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
149 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 1012"),
150 },
151 },
152 {
Mario Limoncielloe5fefd02010-02-09 17:41:03 -0500153 .ident = "Dell Inspiron 11z",
154 .matches = {
155 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
156 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 1110"),
157 },
158 },
159 {
160 .ident = "Dell Mini 12",
161 .matches = {
162 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
163 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 1210"),
164 },
165 },
166 {}
167};
168
AceLan Kao2d8b90b2011-10-04 16:25:44 +0800169static struct dmi_system_id __devinitdata dell_quirks[] = {
170 {
171 .callback = dmi_matched,
172 .ident = "Dell Vostro V130",
173 .matches = {
174 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
175 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro V130"),
176 },
177 .driver_data = &quirk_dell_vostro_v130,
178 },
179 {
180 .callback = dmi_matched,
181 .ident = "Dell Vostro V131",
182 .matches = {
183 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
184 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro V131"),
185 },
186 .driver_data = &quirk_dell_vostro_v130,
187 },
AceLan Kao2a748852011-11-17 15:30:42 +0800188 {
189 .callback = dmi_matched,
Ang Way Chuang57b31b22012-04-12 13:11:27 +0800190 .ident = "Dell Vostro 3350",
191 .matches = {
192 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
193 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3350"),
194 },
195 .driver_data = &quirk_dell_vostro_v130,
196 },
197 {
198 .callback = dmi_matched,
AceLan Kao2a748852011-11-17 15:30:42 +0800199 .ident = "Dell Vostro 3555",
200 .matches = {
201 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
202 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3555"),
203 },
204 .driver_data = &quirk_dell_vostro_v130,
205 },
206 {
207 .callback = dmi_matched,
208 .ident = "Dell Inspiron N311z",
209 .matches = {
210 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
211 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron N311z"),
212 },
213 .driver_data = &quirk_dell_vostro_v130,
214 },
215 {
216 .callback = dmi_matched,
217 .ident = "Dell Inspiron M5110",
218 .matches = {
219 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
220 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron M5110"),
221 },
222 .driver_data = &quirk_dell_vostro_v130,
223 },
AceLan Kao7f839222012-04-20 11:47:26 +0800224 {
225 .callback = dmi_matched,
226 .ident = "Dell Vostro 3360",
227 .matches = {
228 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
229 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3360"),
230 },
231 .driver_data = &quirk_dell_vostro_v130,
232 },
233 {
234 .callback = dmi_matched,
235 .ident = "Dell Vostro 3460",
236 .matches = {
237 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
238 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3460"),
239 },
240 .driver_data = &quirk_dell_vostro_v130,
241 },
242 {
243 .callback = dmi_matched,
244 .ident = "Dell Vostro 3560",
245 .matches = {
246 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
247 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3560"),
248 },
249 .driver_data = &quirk_dell_vostro_v130,
250 },
AceLan Kaod0e0a4772012-05-22 12:38:51 +0800251 {
252 .callback = dmi_matched,
253 .ident = "Dell Vostro 3450",
254 .matches = {
255 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
256 DMI_MATCH(DMI_PRODUCT_NAME, "Dell System Vostro 3450"),
257 },
258 .driver_data = &quirk_dell_vostro_v130,
259 },
Martin Nyhusd62d4212012-03-15 18:25:48 +0100260 { }
AceLan Kao2d8b90b2011-10-04 16:25:44 +0800261};
262
Stuart Hayes116ee772010-02-10 14:12:13 -0500263static struct calling_interface_buffer *buffer;
Ingo Molnar94d8f782010-03-01 09:43:52 -0500264static struct page *bufferpage;
265static DEFINE_MUTEX(buffer_mutex);
Stuart Hayes116ee772010-02-10 14:12:13 -0500266
Matthew Garrettc6760ac2010-02-10 14:44:03 -0500267static int hwswitch_state;
268
Stuart Hayes116ee772010-02-10 14:12:13 -0500269static void get_buffer(void)
270{
271 mutex_lock(&buffer_mutex);
272 memset(buffer, 0, sizeof(struct calling_interface_buffer));
273}
274
275static void release_buffer(void)
276{
277 mutex_unlock(&buffer_mutex);
278}
279
Alan Jenkins4788df42009-08-19 15:06:50 +0100280static void __init parse_da_table(const struct dmi_header *dm)
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800281{
282 /* Final token is a terminator, so we don't want to copy it */
283 int tokens = (dm->length-11)/sizeof(struct calling_interface_token)-1;
284 struct calling_interface_structure *table =
285 container_of(dm, struct calling_interface_structure, header);
286
287 /* 4 bytes of table header, plus 7 bytes of Dell header, plus at least
288 6 bytes of entry */
289
290 if (dm->length < 17)
291 return;
292
293 da_command_address = table->cmdIOAddress;
294 da_command_code = table->cmdIOCode;
295
296 da_tokens = krealloc(da_tokens, (da_num_tokens + tokens) *
297 sizeof(struct calling_interface_token),
298 GFP_KERNEL);
299
300 if (!da_tokens)
301 return;
302
303 memcpy(da_tokens+da_num_tokens, table->tokens,
304 sizeof(struct calling_interface_token) * tokens);
305
306 da_num_tokens += tokens;
307}
308
Alan Jenkins4788df42009-08-19 15:06:50 +0100309static void __init find_tokens(const struct dmi_header *dm, void *dummy)
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800310{
311 switch (dm->type) {
312 case 0xd4: /* Indexed IO */
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800313 case 0xd5: /* Protected Area Type 1 */
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800314 case 0xd6: /* Protected Area Type 2 */
315 break;
316 case 0xda: /* Calling interface */
317 parse_da_table(dm);
318 break;
319 }
320}
321
322static int find_token_location(int tokenid)
323{
324 int i;
325 for (i = 0; i < da_num_tokens; i++) {
326 if (da_tokens[i].tokenID == tokenid)
327 return da_tokens[i].location;
328 }
329
330 return -1;
331}
332
333static struct calling_interface_buffer *
334dell_send_request(struct calling_interface_buffer *buffer, int class,
335 int select)
336{
337 struct smi_cmd command;
338
339 command.magic = SMI_CMD_MAGIC;
340 command.command_address = da_command_address;
341 command.command_code = da_command_code;
342 command.ebx = virt_to_phys(buffer);
343 command.ecx = 0x42534931;
344
345 buffer->class = class;
346 buffer->select = select;
347
348 dcdbas_smi_request(&command);
349
350 return buffer;
351}
352
353/* Derived from information in DellWirelessCtl.cpp:
354 Class 17, select 11 is radio control. It returns an array of 32-bit values.
355
Matthew Garrettc6760ac2010-02-10 14:44:03 -0500356 Input byte 0 = 0: Wireless information
357
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800358 result[0]: return code
359 result[1]:
360 Bit 0: Hardware switch supported
361 Bit 1: Wifi locator supported
362 Bit 2: Wifi is supported
363 Bit 3: Bluetooth is supported
364 Bit 4: WWAN is supported
365 Bit 5: Wireless keyboard supported
366 Bits 6-7: Reserved
367 Bit 8: Wifi is installed
368 Bit 9: Bluetooth is installed
369 Bit 10: WWAN is installed
370 Bits 11-15: Reserved
371 Bit 16: Hardware switch is on
372 Bit 17: Wifi is blocked
373 Bit 18: Bluetooth is blocked
374 Bit 19: WWAN is blocked
375 Bits 20-31: Reserved
376 result[2]: NVRAM size in bytes
377 result[3]: NVRAM format version number
Matthew Garrettc6760ac2010-02-10 14:44:03 -0500378
379 Input byte 0 = 2: Wireless switch configuration
380 result[0]: return code
381 result[1]:
382 Bit 0: Wifi controlled by switch
383 Bit 1: Bluetooth controlled by switch
384 Bit 2: WWAN controlled by switch
385 Bits 3-6: Reserved
386 Bit 7: Wireless switch config locked
387 Bit 8: Wifi locator enabled
388 Bits 9-14: Reserved
389 Bit 15: Wifi locator setting locked
390 Bits 16-31: Reserved
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800391*/
392
Johannes Berg19d337d2009-06-02 13:01:37 +0200393static int dell_rfkill_set(void *data, bool blocked)
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800394{
Johannes Berg624f0de2009-06-15 16:26:47 +0200395 int disable = blocked ? 1 : 0;
Johannes Berg19d337d2009-06-02 13:01:37 +0200396 unsigned long radio = (unsigned long)data;
Matthew Garrettc6760ac2010-02-10 14:44:03 -0500397 int hwswitch_bit = (unsigned long)data - 1;
Stuart Hayes116ee772010-02-10 14:12:13 -0500398 int ret = 0;
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800399
Stuart Hayes116ee772010-02-10 14:12:13 -0500400 get_buffer();
401 dell_send_request(buffer, 17, 11);
Mario Limoncielloec1722a2010-02-09 14:11:05 -0500402
Matthew Garrettc6760ac2010-02-10 14:44:03 -0500403 /* If the hardware switch controls this radio, and the hardware
Keng-Yu Linbe65dde82011-06-27 11:19:03 +0100404 switch is disabled, don't allow changing the software state */
Matthew Garrettc6760ac2010-02-10 14:44:03 -0500405 if ((hwswitch_state & BIT(hwswitch_bit)) &&
Keng-Yu Linbe65dde82011-06-27 11:19:03 +0100406 !(buffer->output[1] & BIT(16))) {
Stuart Hayes116ee772010-02-10 14:12:13 -0500407 ret = -EINVAL;
408 goto out;
409 }
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800410
Stuart Hayes116ee772010-02-10 14:12:13 -0500411 buffer->input[0] = (1 | (radio<<8) | (disable << 16));
412 dell_send_request(buffer, 17, 11);
413
414out:
415 release_buffer();
416 return ret;
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800417}
418
Johannes Berg19d337d2009-06-02 13:01:37 +0200419static void dell_rfkill_query(struct rfkill *rfkill, void *data)
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800420{
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800421 int status;
Johannes Berg19d337d2009-06-02 13:01:37 +0200422 int bit = (unsigned long)data + 16;
Matthew Garrettc6760ac2010-02-10 14:44:03 -0500423 int hwswitch_bit = (unsigned long)data - 1;
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800424
Stuart Hayes116ee772010-02-10 14:12:13 -0500425 get_buffer();
426 dell_send_request(buffer, 17, 11);
427 status = buffer->output[1];
428 release_buffer();
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800429
Matthew Garrette1fbf342009-07-31 03:25:38 +0100430 rfkill_set_sw_state(rfkill, !!(status & BIT(bit)));
Matthew Garrettc6760ac2010-02-10 14:44:03 -0500431
432 if (hwswitch_state & (BIT(hwswitch_bit)))
433 rfkill_set_hw_state(rfkill, !(status & BIT(16)));
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800434}
435
Johannes Berg19d337d2009-06-02 13:01:37 +0200436static const struct rfkill_ops dell_rfkill_ops = {
437 .set_block = dell_rfkill_set,
438 .query = dell_rfkill_query,
439};
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800440
Keng-Yu Lin037accf2010-09-28 11:43:31 +0800441static struct dentry *dell_laptop_dir;
442
443static int dell_debugfs_show(struct seq_file *s, void *data)
444{
445 int status;
446
447 get_buffer();
448 dell_send_request(buffer, 17, 11);
449 status = buffer->output[1];
450 release_buffer();
451
452 seq_printf(s, "status:\t0x%X\n", status);
453 seq_printf(s, "Bit 0 : Hardware switch supported: %lu\n",
454 status & BIT(0));
455 seq_printf(s, "Bit 1 : Wifi locator supported: %lu\n",
456 (status & BIT(1)) >> 1);
457 seq_printf(s, "Bit 2 : Wifi is supported: %lu\n",
458 (status & BIT(2)) >> 2);
459 seq_printf(s, "Bit 3 : Bluetooth is supported: %lu\n",
460 (status & BIT(3)) >> 3);
461 seq_printf(s, "Bit 4 : WWAN is supported: %lu\n",
462 (status & BIT(4)) >> 4);
463 seq_printf(s, "Bit 5 : Wireless keyboard supported: %lu\n",
464 (status & BIT(5)) >> 5);
465 seq_printf(s, "Bit 8 : Wifi is installed: %lu\n",
466 (status & BIT(8)) >> 8);
467 seq_printf(s, "Bit 9 : Bluetooth is installed: %lu\n",
468 (status & BIT(9)) >> 9);
469 seq_printf(s, "Bit 10: WWAN is installed: %lu\n",
470 (status & BIT(10)) >> 10);
471 seq_printf(s, "Bit 16: Hardware switch is on: %lu\n",
472 (status & BIT(16)) >> 16);
473 seq_printf(s, "Bit 17: Wifi is blocked: %lu\n",
474 (status & BIT(17)) >> 17);
475 seq_printf(s, "Bit 18: Bluetooth is blocked: %lu\n",
476 (status & BIT(18)) >> 18);
477 seq_printf(s, "Bit 19: WWAN is blocked: %lu\n",
478 (status & BIT(19)) >> 19);
479
480 seq_printf(s, "\nhwswitch_state:\t0x%X\n", hwswitch_state);
481 seq_printf(s, "Bit 0 : Wifi controlled by switch: %lu\n",
482 hwswitch_state & BIT(0));
483 seq_printf(s, "Bit 1 : Bluetooth controlled by switch: %lu\n",
484 (hwswitch_state & BIT(1)) >> 1);
485 seq_printf(s, "Bit 2 : WWAN controlled by switch: %lu\n",
486 (hwswitch_state & BIT(2)) >> 2);
487 seq_printf(s, "Bit 7 : Wireless switch config locked: %lu\n",
488 (hwswitch_state & BIT(7)) >> 7);
489 seq_printf(s, "Bit 8 : Wifi locator enabled: %lu\n",
490 (hwswitch_state & BIT(8)) >> 8);
491 seq_printf(s, "Bit 15: Wifi locator setting locked: %lu\n",
492 (hwswitch_state & BIT(15)) >> 15);
493
494 return 0;
495}
496
497static int dell_debugfs_open(struct inode *inode, struct file *file)
498{
499 return single_open(file, dell_debugfs_show, inode->i_private);
500}
501
502static const struct file_operations dell_debugfs_fops = {
503 .owner = THIS_MODULE,
504 .open = dell_debugfs_open,
505 .read = seq_read,
506 .llseek = seq_lseek,
507 .release = single_release,
508};
509
Matthew Garrett814cb8a2009-12-09 18:23:36 +0000510static void dell_update_rfkill(struct work_struct *ignored)
511{
512 if (wifi_rfkill)
513 dell_rfkill_query(wifi_rfkill, (void *)1);
514 if (bluetooth_rfkill)
515 dell_rfkill_query(bluetooth_rfkill, (void *)2);
516 if (wwan_rfkill)
517 dell_rfkill_query(wwan_rfkill, (void *)3);
518}
519static DECLARE_DELAYED_WORK(dell_rfkill_work, dell_update_rfkill);
520
521
Alan Jenkins4788df42009-08-19 15:06:50 +0100522static int __init dell_setup_rfkill(void)
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800523{
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800524 int status;
525 int ret;
526
Mario Limoncielloe5fefd02010-02-09 17:41:03 -0500527 if (dmi_check_system(dell_blacklist)) {
Joe Percheseb889522011-03-29 15:21:37 -0700528 pr_info("Blacklisted hardware detected - not enabling rfkill\n");
Mario Limoncielloe5fefd02010-02-09 17:41:03 -0500529 return 0;
530 }
531
Stuart Hayes116ee772010-02-10 14:12:13 -0500532 get_buffer();
533 dell_send_request(buffer, 17, 11);
534 status = buffer->output[1];
Matthew Garrettc6760ac2010-02-10 14:44:03 -0500535 buffer->input[0] = 0x2;
536 dell_send_request(buffer, 17, 11);
537 hwswitch_state = buffer->output[1];
Stuart Hayes116ee772010-02-10 14:12:13 -0500538 release_buffer();
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800539
540 if ((status & (1<<2|1<<8)) == (1<<2|1<<8)) {
Alan Jenkinsada32482009-08-19 15:06:49 +0100541 wifi_rfkill = rfkill_alloc("dell-wifi", &platform_device->dev,
542 RFKILL_TYPE_WLAN,
Johannes Berg19d337d2009-06-02 13:01:37 +0200543 &dell_rfkill_ops, (void *) 1);
544 if (!wifi_rfkill) {
545 ret = -ENOMEM;
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800546 goto err_wifi;
Johannes Berg19d337d2009-06-02 13:01:37 +0200547 }
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800548 ret = rfkill_register(wifi_rfkill);
549 if (ret)
550 goto err_wifi;
551 }
552
553 if ((status & (1<<3|1<<9)) == (1<<3|1<<9)) {
Alan Jenkinsada32482009-08-19 15:06:49 +0100554 bluetooth_rfkill = rfkill_alloc("dell-bluetooth",
555 &platform_device->dev,
Johannes Berg19d337d2009-06-02 13:01:37 +0200556 RFKILL_TYPE_BLUETOOTH,
557 &dell_rfkill_ops, (void *) 2);
558 if (!bluetooth_rfkill) {
559 ret = -ENOMEM;
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800560 goto err_bluetooth;
Johannes Berg19d337d2009-06-02 13:01:37 +0200561 }
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800562 ret = rfkill_register(bluetooth_rfkill);
563 if (ret)
564 goto err_bluetooth;
565 }
566
567 if ((status & (1<<4|1<<10)) == (1<<4|1<<10)) {
Alan Jenkinsada32482009-08-19 15:06:49 +0100568 wwan_rfkill = rfkill_alloc("dell-wwan",
569 &platform_device->dev,
570 RFKILL_TYPE_WWAN,
Johannes Berg19d337d2009-06-02 13:01:37 +0200571 &dell_rfkill_ops, (void *) 3);
572 if (!wwan_rfkill) {
573 ret = -ENOMEM;
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800574 goto err_wwan;
Johannes Berg19d337d2009-06-02 13:01:37 +0200575 }
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800576 ret = rfkill_register(wwan_rfkill);
577 if (ret)
578 goto err_wwan;
579 }
580
581 return 0;
582err_wwan:
Johannes Berg19d337d2009-06-02 13:01:37 +0200583 rfkill_destroy(wwan_rfkill);
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800584 if (bluetooth_rfkill)
Johannes Berg19d337d2009-06-02 13:01:37 +0200585 rfkill_unregister(bluetooth_rfkill);
586err_bluetooth:
587 rfkill_destroy(bluetooth_rfkill);
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800588 if (wifi_rfkill)
Johannes Berg19d337d2009-06-02 13:01:37 +0200589 rfkill_unregister(wifi_rfkill);
590err_wifi:
591 rfkill_destroy(wifi_rfkill);
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800592
593 return ret;
594}
595
Alan Jenkins4311bb22009-08-19 15:06:48 +0100596static void dell_cleanup_rfkill(void)
597{
598 if (wifi_rfkill) {
599 rfkill_unregister(wifi_rfkill);
600 rfkill_destroy(wifi_rfkill);
601 }
602 if (bluetooth_rfkill) {
603 rfkill_unregister(bluetooth_rfkill);
604 rfkill_destroy(bluetooth_rfkill);
605 }
606 if (wwan_rfkill) {
607 rfkill_unregister(wwan_rfkill);
608 rfkill_destroy(wwan_rfkill);
609 }
610}
611
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800612static int dell_send_intensity(struct backlight_device *bd)
613{
Stuart Hayes116ee772010-02-10 14:12:13 -0500614 int ret = 0;
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800615
Stuart Hayes116ee772010-02-10 14:12:13 -0500616 get_buffer();
617 buffer->input[0] = find_token_location(BRIGHTNESS_TOKEN);
618 buffer->input[1] = bd->props.brightness;
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800619
Stuart Hayes116ee772010-02-10 14:12:13 -0500620 if (buffer->input[0] == -1) {
621 ret = -ENODEV;
622 goto out;
623 }
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800624
625 if (power_supply_is_system_supplied() > 0)
Stuart Hayes116ee772010-02-10 14:12:13 -0500626 dell_send_request(buffer, 1, 2);
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800627 else
Stuart Hayes116ee772010-02-10 14:12:13 -0500628 dell_send_request(buffer, 1, 1);
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800629
Stuart Hayes116ee772010-02-10 14:12:13 -0500630out:
631 release_buffer();
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800632 return 0;
633}
634
635static int dell_get_intensity(struct backlight_device *bd)
636{
Stuart Hayes116ee772010-02-10 14:12:13 -0500637 int ret = 0;
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800638
Stuart Hayes116ee772010-02-10 14:12:13 -0500639 get_buffer();
640 buffer->input[0] = find_token_location(BRIGHTNESS_TOKEN);
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800641
Stuart Hayes116ee772010-02-10 14:12:13 -0500642 if (buffer->input[0] == -1) {
643 ret = -ENODEV;
644 goto out;
645 }
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800646
647 if (power_supply_is_system_supplied() > 0)
Stuart Hayes116ee772010-02-10 14:12:13 -0500648 dell_send_request(buffer, 0, 2);
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800649 else
Stuart Hayes116ee772010-02-10 14:12:13 -0500650 dell_send_request(buffer, 0, 1);
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800651
Jose Alonsob4867422011-07-10 15:46:51 -0300652 ret = buffer->output[1];
653
Stuart Hayes116ee772010-02-10 14:12:13 -0500654out:
655 release_buffer();
Jose Alonsob4867422011-07-10 15:46:51 -0300656 return ret;
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800657}
658
Lionel Debrouxacc24722010-11-16 14:14:02 +0100659static const struct backlight_ops dell_ops = {
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800660 .get_brightness = dell_get_intensity,
661 .update_status = dell_send_intensity,
662};
663
Randy Dunlap869f8df2011-11-16 18:20:51 -0800664static void touchpad_led_on(void)
AceLan Kao2d8b90b2011-10-04 16:25:44 +0800665{
666 int command = 0x97;
667 char data = 1;
668 i8042_command(&data, command | 1 << 12);
669}
670
Randy Dunlap869f8df2011-11-16 18:20:51 -0800671static void touchpad_led_off(void)
AceLan Kao2d8b90b2011-10-04 16:25:44 +0800672{
673 int command = 0x97;
674 char data = 2;
675 i8042_command(&data, command | 1 << 12);
676}
677
678static void touchpad_led_set(struct led_classdev *led_cdev,
679 enum led_brightness value)
680{
681 if (value > 0)
682 touchpad_led_on();
683 else
684 touchpad_led_off();
685}
686
687static struct led_classdev touchpad_led = {
688 .name = "dell-laptop::touchpad",
689 .brightness_set = touchpad_led_set,
AceLan Kao2d5de9e2012-01-17 16:18:06 +0800690 .flags = LED_CORE_SUSPENDRESUME,
AceLan Kao2d8b90b2011-10-04 16:25:44 +0800691};
692
693static int __devinit touchpad_led_init(struct device *dev)
694{
695 return led_classdev_register(dev, &touchpad_led);
696}
697
698static void touchpad_led_exit(void)
699{
700 led_classdev_unregister(&touchpad_led);
701}
702
Axel Lin45191692010-07-05 17:21:10 +0800703static bool dell_laptop_i8042_filter(unsigned char data, unsigned char str,
Matthew Garrett814cb8a2009-12-09 18:23:36 +0000704 struct serio *port)
705{
706 static bool extended;
707
708 if (str & 0x20)
709 return false;
710
711 if (unlikely(data == 0xe0)) {
712 extended = true;
713 return false;
714 } else if (unlikely(extended)) {
715 switch (data) {
716 case 0x8:
717 schedule_delayed_work(&dell_rfkill_work,
718 round_jiffies_relative(HZ));
719 break;
720 }
721 extended = false;
722 }
723
724 return false;
725}
726
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800727static int __init dell_init(void)
728{
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800729 int max_intensity = 0;
730 int ret;
731
732 if (!dmi_check_system(dell_device_table))
733 return -ENODEV;
734
AceLan Kao2d8b90b2011-10-04 16:25:44 +0800735 quirks = NULL;
736 /* find if this machine support other functions */
737 dmi_check_system(dell_quirks);
738
Jean Delvaree7a19c52009-03-30 21:46:44 +0200739 dmi_walk(find_tokens, NULL);
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800740
741 if (!da_tokens) {
Joe Percheseb889522011-03-29 15:21:37 -0700742 pr_info("Unable to find dmi tokens\n");
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800743 return -ENODEV;
744 }
745
Alan Jenkinsada32482009-08-19 15:06:49 +0100746 ret = platform_driver_register(&platform_driver);
747 if (ret)
748 goto fail_platform_driver;
749 platform_device = platform_device_alloc("dell-laptop", -1);
750 if (!platform_device) {
751 ret = -ENOMEM;
752 goto fail_platform_device1;
753 }
754 ret = platform_device_add(platform_device);
755 if (ret)
756 goto fail_platform_device2;
757
Stuart Hayes116ee772010-02-10 14:12:13 -0500758 /*
759 * Allocate buffer below 4GB for SMI data--only 32-bit physical addr
760 * is passed to SMI handler.
761 */
762 bufferpage = alloc_page(GFP_KERNEL | GFP_DMA32);
763
764 if (!bufferpage)
765 goto fail_buffer;
766 buffer = page_address(bufferpage);
Stuart Hayes116ee772010-02-10 14:12:13 -0500767
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800768 ret = dell_setup_rfkill();
769
770 if (ret) {
Joe Percheseb889522011-03-29 15:21:37 -0700771 pr_warn("Unable to setup rfkill\n");
Alan Jenkins71e9dc72009-08-19 15:06:47 +0100772 goto fail_rfkill;
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800773 }
774
Matthew Garrett814cb8a2009-12-09 18:23:36 +0000775 ret = i8042_install_filter(dell_laptop_i8042_filter);
776 if (ret) {
Joe Percheseb889522011-03-29 15:21:37 -0700777 pr_warn("Unable to install key filter\n");
Matthew Garrett814cb8a2009-12-09 18:23:36 +0000778 goto fail_filter;
779 }
780
AceLan Kao2d8b90b2011-10-04 16:25:44 +0800781 if (quirks && quirks->touchpad_led)
782 touchpad_led_init(&platform_device->dev);
783
Keng-Yu Lin037accf2010-09-28 11:43:31 +0800784 dell_laptop_dir = debugfs_create_dir("dell_laptop", NULL);
785 if (dell_laptop_dir != NULL)
786 debugfs_create_file("rfkill", 0444, dell_laptop_dir, NULL,
787 &dell_debugfs_fops);
788
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800789#ifdef CONFIG_ACPI
790 /* In the event of an ACPI backlight being available, don't
791 * register the platform controller.
792 */
793 if (acpi_video_backlight_support())
794 return 0;
795#endif
796
Stuart Hayes116ee772010-02-10 14:12:13 -0500797 get_buffer();
798 buffer->input[0] = find_token_location(BRIGHTNESS_TOKEN);
799 if (buffer->input[0] != -1) {
800 dell_send_request(buffer, 0, 2);
801 max_intensity = buffer->output[3];
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800802 }
Stuart Hayes116ee772010-02-10 14:12:13 -0500803 release_buffer();
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800804
805 if (max_intensity) {
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500806 struct backlight_properties props;
807 memset(&props, 0, sizeof(struct backlight_properties));
Matthew Garrettbb7ca742011-03-22 16:30:21 -0700808 props.type = BACKLIGHT_PLATFORM;
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500809 props.max_brightness = max_intensity;
810 dell_backlight_device = backlight_device_register("dell_backlight",
811 &platform_device->dev,
812 NULL,
813 &dell_ops,
814 &props);
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800815
816 if (IS_ERR(dell_backlight_device)) {
817 ret = PTR_ERR(dell_backlight_device);
818 dell_backlight_device = NULL;
Alan Jenkins71e9dc72009-08-19 15:06:47 +0100819 goto fail_backlight;
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800820 }
821
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800822 dell_backlight_device->props.brightness =
823 dell_get_intensity(dell_backlight_device);
824 backlight_update_status(dell_backlight_device);
825 }
826
827 return 0;
Alan Jenkins71e9dc72009-08-19 15:06:47 +0100828
829fail_backlight:
Matthew Garrett814cb8a2009-12-09 18:23:36 +0000830 i8042_remove_filter(dell_laptop_i8042_filter);
Matthew Garrett92e00e42010-03-01 09:46:43 -0500831 cancel_delayed_work_sync(&dell_rfkill_work);
Matthew Garrett814cb8a2009-12-09 18:23:36 +0000832fail_filter:
Alan Jenkins4311bb22009-08-19 15:06:48 +0100833 dell_cleanup_rfkill();
Alan Jenkins71e9dc72009-08-19 15:06:47 +0100834fail_rfkill:
Stuart Hayes116ee772010-02-10 14:12:13 -0500835 free_page((unsigned long)bufferpage);
836fail_buffer:
Alan Jenkinsada32482009-08-19 15:06:49 +0100837 platform_device_del(platform_device);
838fail_platform_device2:
839 platform_device_put(platform_device);
840fail_platform_device1:
841 platform_driver_unregister(&platform_driver);
842fail_platform_driver:
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800843 kfree(da_tokens);
844 return ret;
845}
846
847static void __exit dell_exit(void)
848{
Keng-Yu Lin037accf2010-09-28 11:43:31 +0800849 debugfs_remove_recursive(dell_laptop_dir);
AceLan Kao2d8b90b2011-10-04 16:25:44 +0800850 if (quirks && quirks->touchpad_led)
851 touchpad_led_exit();
Matthew Garrett814cb8a2009-12-09 18:23:36 +0000852 i8042_remove_filter(dell_laptop_i8042_filter);
Matthew Garrett92e00e42010-03-01 09:46:43 -0500853 cancel_delayed_work_sync(&dell_rfkill_work);
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800854 backlight_device_unregister(dell_backlight_device);
Alan Jenkins4311bb22009-08-19 15:06:48 +0100855 dell_cleanup_rfkill();
Matthew Garrettfacd61d2010-02-09 14:03:04 -0500856 if (platform_device) {
Matthew Garrett92e00e42010-03-01 09:46:43 -0500857 platform_device_unregister(platform_device);
Matthew Garrettfacd61d2010-02-09 14:03:04 -0500858 platform_driver_unregister(&platform_driver);
859 }
Matthew Garrette5512602010-02-09 14:05:01 -0500860 kfree(da_tokens);
Stuart Hayes116ee772010-02-10 14:12:13 -0500861 free_page((unsigned long)buffer);
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800862}
863
864module_init(dell_init);
865module_exit(dell_exit);
866
867MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
868MODULE_DESCRIPTION("Dell laptop driver");
869MODULE_LICENSE("GPL");