blob: 50dac0c9b67090750a58f91ea8cd3f192c27f0b2 [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
97static const struct dmi_system_id __initdata dell_device_table[] = {
98 {
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 },
188};
189
Stuart Hayes116ee772010-02-10 14:12:13 -0500190static struct calling_interface_buffer *buffer;
Ingo Molnar94d8f782010-03-01 09:43:52 -0500191static struct page *bufferpage;
192static DEFINE_MUTEX(buffer_mutex);
Stuart Hayes116ee772010-02-10 14:12:13 -0500193
Matthew Garrettc6760ac2010-02-10 14:44:03 -0500194static int hwswitch_state;
195
Stuart Hayes116ee772010-02-10 14:12:13 -0500196static void get_buffer(void)
197{
198 mutex_lock(&buffer_mutex);
199 memset(buffer, 0, sizeof(struct calling_interface_buffer));
200}
201
202static void release_buffer(void)
203{
204 mutex_unlock(&buffer_mutex);
205}
206
Alan Jenkins4788df42009-08-19 15:06:50 +0100207static void __init parse_da_table(const struct dmi_header *dm)
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800208{
209 /* Final token is a terminator, so we don't want to copy it */
210 int tokens = (dm->length-11)/sizeof(struct calling_interface_token)-1;
211 struct calling_interface_structure *table =
212 container_of(dm, struct calling_interface_structure, header);
213
214 /* 4 bytes of table header, plus 7 bytes of Dell header, plus at least
215 6 bytes of entry */
216
217 if (dm->length < 17)
218 return;
219
220 da_command_address = table->cmdIOAddress;
221 da_command_code = table->cmdIOCode;
222
223 da_tokens = krealloc(da_tokens, (da_num_tokens + tokens) *
224 sizeof(struct calling_interface_token),
225 GFP_KERNEL);
226
227 if (!da_tokens)
228 return;
229
230 memcpy(da_tokens+da_num_tokens, table->tokens,
231 sizeof(struct calling_interface_token) * tokens);
232
233 da_num_tokens += tokens;
234}
235
Alan Jenkins4788df42009-08-19 15:06:50 +0100236static void __init find_tokens(const struct dmi_header *dm, void *dummy)
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800237{
238 switch (dm->type) {
239 case 0xd4: /* Indexed IO */
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800240 case 0xd5: /* Protected Area Type 1 */
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800241 case 0xd6: /* Protected Area Type 2 */
242 break;
243 case 0xda: /* Calling interface */
244 parse_da_table(dm);
245 break;
246 }
247}
248
249static int find_token_location(int tokenid)
250{
251 int i;
252 for (i = 0; i < da_num_tokens; i++) {
253 if (da_tokens[i].tokenID == tokenid)
254 return da_tokens[i].location;
255 }
256
257 return -1;
258}
259
260static struct calling_interface_buffer *
261dell_send_request(struct calling_interface_buffer *buffer, int class,
262 int select)
263{
264 struct smi_cmd command;
265
266 command.magic = SMI_CMD_MAGIC;
267 command.command_address = da_command_address;
268 command.command_code = da_command_code;
269 command.ebx = virt_to_phys(buffer);
270 command.ecx = 0x42534931;
271
272 buffer->class = class;
273 buffer->select = select;
274
275 dcdbas_smi_request(&command);
276
277 return buffer;
278}
279
280/* Derived from information in DellWirelessCtl.cpp:
281 Class 17, select 11 is radio control. It returns an array of 32-bit values.
282
Matthew Garrettc6760ac2010-02-10 14:44:03 -0500283 Input byte 0 = 0: Wireless information
284
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800285 result[0]: return code
286 result[1]:
287 Bit 0: Hardware switch supported
288 Bit 1: Wifi locator supported
289 Bit 2: Wifi is supported
290 Bit 3: Bluetooth is supported
291 Bit 4: WWAN is supported
292 Bit 5: Wireless keyboard supported
293 Bits 6-7: Reserved
294 Bit 8: Wifi is installed
295 Bit 9: Bluetooth is installed
296 Bit 10: WWAN is installed
297 Bits 11-15: Reserved
298 Bit 16: Hardware switch is on
299 Bit 17: Wifi is blocked
300 Bit 18: Bluetooth is blocked
301 Bit 19: WWAN is blocked
302 Bits 20-31: Reserved
303 result[2]: NVRAM size in bytes
304 result[3]: NVRAM format version number
Matthew Garrettc6760ac2010-02-10 14:44:03 -0500305
306 Input byte 0 = 2: Wireless switch configuration
307 result[0]: return code
308 result[1]:
309 Bit 0: Wifi controlled by switch
310 Bit 1: Bluetooth controlled by switch
311 Bit 2: WWAN controlled by switch
312 Bits 3-6: Reserved
313 Bit 7: Wireless switch config locked
314 Bit 8: Wifi locator enabled
315 Bits 9-14: Reserved
316 Bit 15: Wifi locator setting locked
317 Bits 16-31: Reserved
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800318*/
319
Johannes Berg19d337d2009-06-02 13:01:37 +0200320static int dell_rfkill_set(void *data, bool blocked)
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800321{
Johannes Berg624f0de2009-06-15 16:26:47 +0200322 int disable = blocked ? 1 : 0;
Johannes Berg19d337d2009-06-02 13:01:37 +0200323 unsigned long radio = (unsigned long)data;
Matthew Garrettc6760ac2010-02-10 14:44:03 -0500324 int hwswitch_bit = (unsigned long)data - 1;
Stuart Hayes116ee772010-02-10 14:12:13 -0500325 int ret = 0;
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800326
Stuart Hayes116ee772010-02-10 14:12:13 -0500327 get_buffer();
328 dell_send_request(buffer, 17, 11);
Mario Limoncielloec1722a2010-02-09 14:11:05 -0500329
Matthew Garrettc6760ac2010-02-10 14:44:03 -0500330 /* If the hardware switch controls this radio, and the hardware
Keng-Yu Linbe65dde82011-06-27 11:19:03 +0100331 switch is disabled, don't allow changing the software state */
Matthew Garrettc6760ac2010-02-10 14:44:03 -0500332 if ((hwswitch_state & BIT(hwswitch_bit)) &&
Keng-Yu Linbe65dde82011-06-27 11:19:03 +0100333 !(buffer->output[1] & BIT(16))) {
Stuart Hayes116ee772010-02-10 14:12:13 -0500334 ret = -EINVAL;
335 goto out;
336 }
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800337
Stuart Hayes116ee772010-02-10 14:12:13 -0500338 buffer->input[0] = (1 | (radio<<8) | (disable << 16));
339 dell_send_request(buffer, 17, 11);
340
341out:
342 release_buffer();
343 return ret;
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800344}
345
Johannes Berg19d337d2009-06-02 13:01:37 +0200346static void dell_rfkill_query(struct rfkill *rfkill, void *data)
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800347{
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800348 int status;
Johannes Berg19d337d2009-06-02 13:01:37 +0200349 int bit = (unsigned long)data + 16;
Matthew Garrettc6760ac2010-02-10 14:44:03 -0500350 int hwswitch_bit = (unsigned long)data - 1;
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800351
Stuart Hayes116ee772010-02-10 14:12:13 -0500352 get_buffer();
353 dell_send_request(buffer, 17, 11);
354 status = buffer->output[1];
355 release_buffer();
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800356
Matthew Garrette1fbf342009-07-31 03:25:38 +0100357 rfkill_set_sw_state(rfkill, !!(status & BIT(bit)));
Matthew Garrettc6760ac2010-02-10 14:44:03 -0500358
359 if (hwswitch_state & (BIT(hwswitch_bit)))
360 rfkill_set_hw_state(rfkill, !(status & BIT(16)));
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800361}
362
Johannes Berg19d337d2009-06-02 13:01:37 +0200363static const struct rfkill_ops dell_rfkill_ops = {
364 .set_block = dell_rfkill_set,
365 .query = dell_rfkill_query,
366};
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800367
Keng-Yu Lin037accf2010-09-28 11:43:31 +0800368static struct dentry *dell_laptop_dir;
369
370static int dell_debugfs_show(struct seq_file *s, void *data)
371{
372 int status;
373
374 get_buffer();
375 dell_send_request(buffer, 17, 11);
376 status = buffer->output[1];
377 release_buffer();
378
379 seq_printf(s, "status:\t0x%X\n", status);
380 seq_printf(s, "Bit 0 : Hardware switch supported: %lu\n",
381 status & BIT(0));
382 seq_printf(s, "Bit 1 : Wifi locator supported: %lu\n",
383 (status & BIT(1)) >> 1);
384 seq_printf(s, "Bit 2 : Wifi is supported: %lu\n",
385 (status & BIT(2)) >> 2);
386 seq_printf(s, "Bit 3 : Bluetooth is supported: %lu\n",
387 (status & BIT(3)) >> 3);
388 seq_printf(s, "Bit 4 : WWAN is supported: %lu\n",
389 (status & BIT(4)) >> 4);
390 seq_printf(s, "Bit 5 : Wireless keyboard supported: %lu\n",
391 (status & BIT(5)) >> 5);
392 seq_printf(s, "Bit 8 : Wifi is installed: %lu\n",
393 (status & BIT(8)) >> 8);
394 seq_printf(s, "Bit 9 : Bluetooth is installed: %lu\n",
395 (status & BIT(9)) >> 9);
396 seq_printf(s, "Bit 10: WWAN is installed: %lu\n",
397 (status & BIT(10)) >> 10);
398 seq_printf(s, "Bit 16: Hardware switch is on: %lu\n",
399 (status & BIT(16)) >> 16);
400 seq_printf(s, "Bit 17: Wifi is blocked: %lu\n",
401 (status & BIT(17)) >> 17);
402 seq_printf(s, "Bit 18: Bluetooth is blocked: %lu\n",
403 (status & BIT(18)) >> 18);
404 seq_printf(s, "Bit 19: WWAN is blocked: %lu\n",
405 (status & BIT(19)) >> 19);
406
407 seq_printf(s, "\nhwswitch_state:\t0x%X\n", hwswitch_state);
408 seq_printf(s, "Bit 0 : Wifi controlled by switch: %lu\n",
409 hwswitch_state & BIT(0));
410 seq_printf(s, "Bit 1 : Bluetooth controlled by switch: %lu\n",
411 (hwswitch_state & BIT(1)) >> 1);
412 seq_printf(s, "Bit 2 : WWAN controlled by switch: %lu\n",
413 (hwswitch_state & BIT(2)) >> 2);
414 seq_printf(s, "Bit 7 : Wireless switch config locked: %lu\n",
415 (hwswitch_state & BIT(7)) >> 7);
416 seq_printf(s, "Bit 8 : Wifi locator enabled: %lu\n",
417 (hwswitch_state & BIT(8)) >> 8);
418 seq_printf(s, "Bit 15: Wifi locator setting locked: %lu\n",
419 (hwswitch_state & BIT(15)) >> 15);
420
421 return 0;
422}
423
424static int dell_debugfs_open(struct inode *inode, struct file *file)
425{
426 return single_open(file, dell_debugfs_show, inode->i_private);
427}
428
429static const struct file_operations dell_debugfs_fops = {
430 .owner = THIS_MODULE,
431 .open = dell_debugfs_open,
432 .read = seq_read,
433 .llseek = seq_lseek,
434 .release = single_release,
435};
436
Matthew Garrett814cb8a2009-12-09 18:23:36 +0000437static void dell_update_rfkill(struct work_struct *ignored)
438{
439 if (wifi_rfkill)
440 dell_rfkill_query(wifi_rfkill, (void *)1);
441 if (bluetooth_rfkill)
442 dell_rfkill_query(bluetooth_rfkill, (void *)2);
443 if (wwan_rfkill)
444 dell_rfkill_query(wwan_rfkill, (void *)3);
445}
446static DECLARE_DELAYED_WORK(dell_rfkill_work, dell_update_rfkill);
447
448
Alan Jenkins4788df42009-08-19 15:06:50 +0100449static int __init dell_setup_rfkill(void)
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800450{
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800451 int status;
452 int ret;
453
Mario Limoncielloe5fefd02010-02-09 17:41:03 -0500454 if (dmi_check_system(dell_blacklist)) {
Joe Percheseb889522011-03-29 15:21:37 -0700455 pr_info("Blacklisted hardware detected - not enabling rfkill\n");
Mario Limoncielloe5fefd02010-02-09 17:41:03 -0500456 return 0;
457 }
458
Stuart Hayes116ee772010-02-10 14:12:13 -0500459 get_buffer();
460 dell_send_request(buffer, 17, 11);
461 status = buffer->output[1];
Matthew Garrettc6760ac2010-02-10 14:44:03 -0500462 buffer->input[0] = 0x2;
463 dell_send_request(buffer, 17, 11);
464 hwswitch_state = buffer->output[1];
Stuart Hayes116ee772010-02-10 14:12:13 -0500465 release_buffer();
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800466
467 if ((status & (1<<2|1<<8)) == (1<<2|1<<8)) {
Alan Jenkinsada32482009-08-19 15:06:49 +0100468 wifi_rfkill = rfkill_alloc("dell-wifi", &platform_device->dev,
469 RFKILL_TYPE_WLAN,
Johannes Berg19d337d2009-06-02 13:01:37 +0200470 &dell_rfkill_ops, (void *) 1);
471 if (!wifi_rfkill) {
472 ret = -ENOMEM;
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800473 goto err_wifi;
Johannes Berg19d337d2009-06-02 13:01:37 +0200474 }
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800475 ret = rfkill_register(wifi_rfkill);
476 if (ret)
477 goto err_wifi;
478 }
479
480 if ((status & (1<<3|1<<9)) == (1<<3|1<<9)) {
Alan Jenkinsada32482009-08-19 15:06:49 +0100481 bluetooth_rfkill = rfkill_alloc("dell-bluetooth",
482 &platform_device->dev,
Johannes Berg19d337d2009-06-02 13:01:37 +0200483 RFKILL_TYPE_BLUETOOTH,
484 &dell_rfkill_ops, (void *) 2);
485 if (!bluetooth_rfkill) {
486 ret = -ENOMEM;
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800487 goto err_bluetooth;
Johannes Berg19d337d2009-06-02 13:01:37 +0200488 }
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800489 ret = rfkill_register(bluetooth_rfkill);
490 if (ret)
491 goto err_bluetooth;
492 }
493
494 if ((status & (1<<4|1<<10)) == (1<<4|1<<10)) {
Alan Jenkinsada32482009-08-19 15:06:49 +0100495 wwan_rfkill = rfkill_alloc("dell-wwan",
496 &platform_device->dev,
497 RFKILL_TYPE_WWAN,
Johannes Berg19d337d2009-06-02 13:01:37 +0200498 &dell_rfkill_ops, (void *) 3);
499 if (!wwan_rfkill) {
500 ret = -ENOMEM;
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800501 goto err_wwan;
Johannes Berg19d337d2009-06-02 13:01:37 +0200502 }
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800503 ret = rfkill_register(wwan_rfkill);
504 if (ret)
505 goto err_wwan;
506 }
507
508 return 0;
509err_wwan:
Johannes Berg19d337d2009-06-02 13:01:37 +0200510 rfkill_destroy(wwan_rfkill);
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800511 if (bluetooth_rfkill)
Johannes Berg19d337d2009-06-02 13:01:37 +0200512 rfkill_unregister(bluetooth_rfkill);
513err_bluetooth:
514 rfkill_destroy(bluetooth_rfkill);
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800515 if (wifi_rfkill)
Johannes Berg19d337d2009-06-02 13:01:37 +0200516 rfkill_unregister(wifi_rfkill);
517err_wifi:
518 rfkill_destroy(wifi_rfkill);
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800519
520 return ret;
521}
522
Alan Jenkins4311bb22009-08-19 15:06:48 +0100523static void dell_cleanup_rfkill(void)
524{
525 if (wifi_rfkill) {
526 rfkill_unregister(wifi_rfkill);
527 rfkill_destroy(wifi_rfkill);
528 }
529 if (bluetooth_rfkill) {
530 rfkill_unregister(bluetooth_rfkill);
531 rfkill_destroy(bluetooth_rfkill);
532 }
533 if (wwan_rfkill) {
534 rfkill_unregister(wwan_rfkill);
535 rfkill_destroy(wwan_rfkill);
536 }
537}
538
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800539static int dell_send_intensity(struct backlight_device *bd)
540{
Stuart Hayes116ee772010-02-10 14:12:13 -0500541 int ret = 0;
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800542
Stuart Hayes116ee772010-02-10 14:12:13 -0500543 get_buffer();
544 buffer->input[0] = find_token_location(BRIGHTNESS_TOKEN);
545 buffer->input[1] = bd->props.brightness;
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800546
Stuart Hayes116ee772010-02-10 14:12:13 -0500547 if (buffer->input[0] == -1) {
548 ret = -ENODEV;
549 goto out;
550 }
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800551
552 if (power_supply_is_system_supplied() > 0)
Stuart Hayes116ee772010-02-10 14:12:13 -0500553 dell_send_request(buffer, 1, 2);
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800554 else
Stuart Hayes116ee772010-02-10 14:12:13 -0500555 dell_send_request(buffer, 1, 1);
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800556
Stuart Hayes116ee772010-02-10 14:12:13 -0500557out:
558 release_buffer();
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800559 return 0;
560}
561
562static int dell_get_intensity(struct backlight_device *bd)
563{
Stuart Hayes116ee772010-02-10 14:12:13 -0500564 int ret = 0;
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800565
Stuart Hayes116ee772010-02-10 14:12:13 -0500566 get_buffer();
567 buffer->input[0] = find_token_location(BRIGHTNESS_TOKEN);
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800568
Stuart Hayes116ee772010-02-10 14:12:13 -0500569 if (buffer->input[0] == -1) {
570 ret = -ENODEV;
571 goto out;
572 }
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800573
574 if (power_supply_is_system_supplied() > 0)
Stuart Hayes116ee772010-02-10 14:12:13 -0500575 dell_send_request(buffer, 0, 2);
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800576 else
Stuart Hayes116ee772010-02-10 14:12:13 -0500577 dell_send_request(buffer, 0, 1);
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800578
Jose Alonsob4867422011-07-10 15:46:51 -0300579 ret = buffer->output[1];
580
Stuart Hayes116ee772010-02-10 14:12:13 -0500581out:
582 release_buffer();
Jose Alonsob4867422011-07-10 15:46:51 -0300583 return ret;
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800584}
585
Lionel Debrouxacc24722010-11-16 14:14:02 +0100586static const struct backlight_ops dell_ops = {
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800587 .get_brightness = dell_get_intensity,
588 .update_status = dell_send_intensity,
589};
590
Randy Dunlap869f8df2011-11-16 18:20:51 -0800591static void touchpad_led_on(void)
AceLan Kao2d8b90b2011-10-04 16:25:44 +0800592{
593 int command = 0x97;
594 char data = 1;
595 i8042_command(&data, command | 1 << 12);
596}
597
Randy Dunlap869f8df2011-11-16 18:20:51 -0800598static void touchpad_led_off(void)
AceLan Kao2d8b90b2011-10-04 16:25:44 +0800599{
600 int command = 0x97;
601 char data = 2;
602 i8042_command(&data, command | 1 << 12);
603}
604
605static void touchpad_led_set(struct led_classdev *led_cdev,
606 enum led_brightness value)
607{
608 if (value > 0)
609 touchpad_led_on();
610 else
611 touchpad_led_off();
612}
613
614static struct led_classdev touchpad_led = {
615 .name = "dell-laptop::touchpad",
616 .brightness_set = touchpad_led_set,
617};
618
619static int __devinit touchpad_led_init(struct device *dev)
620{
621 return led_classdev_register(dev, &touchpad_led);
622}
623
624static void touchpad_led_exit(void)
625{
626 led_classdev_unregister(&touchpad_led);
627}
628
Axel Lin45191692010-07-05 17:21:10 +0800629static bool dell_laptop_i8042_filter(unsigned char data, unsigned char str,
Matthew Garrett814cb8a2009-12-09 18:23:36 +0000630 struct serio *port)
631{
632 static bool extended;
633
634 if (str & 0x20)
635 return false;
636
637 if (unlikely(data == 0xe0)) {
638 extended = true;
639 return false;
640 } else if (unlikely(extended)) {
641 switch (data) {
642 case 0x8:
643 schedule_delayed_work(&dell_rfkill_work,
644 round_jiffies_relative(HZ));
645 break;
646 }
647 extended = false;
648 }
649
650 return false;
651}
652
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800653static int __init dell_init(void)
654{
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800655 int max_intensity = 0;
656 int ret;
657
658 if (!dmi_check_system(dell_device_table))
659 return -ENODEV;
660
AceLan Kao2d8b90b2011-10-04 16:25:44 +0800661 quirks = NULL;
662 /* find if this machine support other functions */
663 dmi_check_system(dell_quirks);
664
Jean Delvaree7a19c52009-03-30 21:46:44 +0200665 dmi_walk(find_tokens, NULL);
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800666
667 if (!da_tokens) {
Joe Percheseb889522011-03-29 15:21:37 -0700668 pr_info("Unable to find dmi tokens\n");
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800669 return -ENODEV;
670 }
671
Alan Jenkinsada32482009-08-19 15:06:49 +0100672 ret = platform_driver_register(&platform_driver);
673 if (ret)
674 goto fail_platform_driver;
675 platform_device = platform_device_alloc("dell-laptop", -1);
676 if (!platform_device) {
677 ret = -ENOMEM;
678 goto fail_platform_device1;
679 }
680 ret = platform_device_add(platform_device);
681 if (ret)
682 goto fail_platform_device2;
683
Stuart Hayes116ee772010-02-10 14:12:13 -0500684 /*
685 * Allocate buffer below 4GB for SMI data--only 32-bit physical addr
686 * is passed to SMI handler.
687 */
688 bufferpage = alloc_page(GFP_KERNEL | GFP_DMA32);
689
690 if (!bufferpage)
691 goto fail_buffer;
692 buffer = page_address(bufferpage);
Stuart Hayes116ee772010-02-10 14:12:13 -0500693
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800694 ret = dell_setup_rfkill();
695
696 if (ret) {
Joe Percheseb889522011-03-29 15:21:37 -0700697 pr_warn("Unable to setup rfkill\n");
Alan Jenkins71e9dc72009-08-19 15:06:47 +0100698 goto fail_rfkill;
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800699 }
700
Matthew Garrett814cb8a2009-12-09 18:23:36 +0000701 ret = i8042_install_filter(dell_laptop_i8042_filter);
702 if (ret) {
Joe Percheseb889522011-03-29 15:21:37 -0700703 pr_warn("Unable to install key filter\n");
Matthew Garrett814cb8a2009-12-09 18:23:36 +0000704 goto fail_filter;
705 }
706
AceLan Kao2d8b90b2011-10-04 16:25:44 +0800707 if (quirks && quirks->touchpad_led)
708 touchpad_led_init(&platform_device->dev);
709
Keng-Yu Lin037accf2010-09-28 11:43:31 +0800710 dell_laptop_dir = debugfs_create_dir("dell_laptop", NULL);
711 if (dell_laptop_dir != NULL)
712 debugfs_create_file("rfkill", 0444, dell_laptop_dir, NULL,
713 &dell_debugfs_fops);
714
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800715#ifdef CONFIG_ACPI
716 /* In the event of an ACPI backlight being available, don't
717 * register the platform controller.
718 */
719 if (acpi_video_backlight_support())
720 return 0;
721#endif
722
Stuart Hayes116ee772010-02-10 14:12:13 -0500723 get_buffer();
724 buffer->input[0] = find_token_location(BRIGHTNESS_TOKEN);
725 if (buffer->input[0] != -1) {
726 dell_send_request(buffer, 0, 2);
727 max_intensity = buffer->output[3];
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800728 }
Stuart Hayes116ee772010-02-10 14:12:13 -0500729 release_buffer();
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800730
731 if (max_intensity) {
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500732 struct backlight_properties props;
733 memset(&props, 0, sizeof(struct backlight_properties));
Matthew Garrettbb7ca742011-03-22 16:30:21 -0700734 props.type = BACKLIGHT_PLATFORM;
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500735 props.max_brightness = max_intensity;
736 dell_backlight_device = backlight_device_register("dell_backlight",
737 &platform_device->dev,
738 NULL,
739 &dell_ops,
740 &props);
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800741
742 if (IS_ERR(dell_backlight_device)) {
743 ret = PTR_ERR(dell_backlight_device);
744 dell_backlight_device = NULL;
Alan Jenkins71e9dc72009-08-19 15:06:47 +0100745 goto fail_backlight;
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800746 }
747
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800748 dell_backlight_device->props.brightness =
749 dell_get_intensity(dell_backlight_device);
750 backlight_update_status(dell_backlight_device);
751 }
752
753 return 0;
Alan Jenkins71e9dc72009-08-19 15:06:47 +0100754
755fail_backlight:
Matthew Garrett814cb8a2009-12-09 18:23:36 +0000756 i8042_remove_filter(dell_laptop_i8042_filter);
Matthew Garrett92e00e42010-03-01 09:46:43 -0500757 cancel_delayed_work_sync(&dell_rfkill_work);
Matthew Garrett814cb8a2009-12-09 18:23:36 +0000758fail_filter:
Alan Jenkins4311bb22009-08-19 15:06:48 +0100759 dell_cleanup_rfkill();
Alan Jenkins71e9dc72009-08-19 15:06:47 +0100760fail_rfkill:
Stuart Hayes116ee772010-02-10 14:12:13 -0500761 free_page((unsigned long)bufferpage);
762fail_buffer:
Alan Jenkinsada32482009-08-19 15:06:49 +0100763 platform_device_del(platform_device);
764fail_platform_device2:
765 platform_device_put(platform_device);
766fail_platform_device1:
767 platform_driver_unregister(&platform_driver);
768fail_platform_driver:
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800769 kfree(da_tokens);
770 return ret;
771}
772
773static void __exit dell_exit(void)
774{
Keng-Yu Lin037accf2010-09-28 11:43:31 +0800775 debugfs_remove_recursive(dell_laptop_dir);
AceLan Kao2d8b90b2011-10-04 16:25:44 +0800776 if (quirks && quirks->touchpad_led)
777 touchpad_led_exit();
Matthew Garrett814cb8a2009-12-09 18:23:36 +0000778 i8042_remove_filter(dell_laptop_i8042_filter);
Matthew Garrett92e00e42010-03-01 09:46:43 -0500779 cancel_delayed_work_sync(&dell_rfkill_work);
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800780 backlight_device_unregister(dell_backlight_device);
Alan Jenkins4311bb22009-08-19 15:06:48 +0100781 dell_cleanup_rfkill();
Matthew Garrettfacd61d2010-02-09 14:03:04 -0500782 if (platform_device) {
Matthew Garrett92e00e42010-03-01 09:46:43 -0500783 platform_device_unregister(platform_device);
Matthew Garrettfacd61d2010-02-09 14:03:04 -0500784 platform_driver_unregister(&platform_driver);
785 }
Matthew Garrette5512602010-02-09 14:05:01 -0500786 kfree(da_tokens);
Stuart Hayes116ee772010-02-10 14:12:13 -0500787 free_page((unsigned long)buffer);
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800788}
789
790module_init(dell_init);
791module_exit(dell_exit);
792
793MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
794MODULE_DESCRIPTION("Dell laptop driver");
795MODULE_LICENSE("GPL");