blob: d3841de6a8cf199ec08b24bd85f50a0af0490d37 [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
63static int da_command_address;
64static int da_command_code;
65static int da_num_tokens;
66static struct calling_interface_token *da_tokens;
67
Alan Jenkinsada32482009-08-19 15:06:49 +010068static struct platform_driver platform_driver = {
69 .driver = {
70 .name = "dell-laptop",
71 .owner = THIS_MODULE,
72 }
73};
74
75static struct platform_device *platform_device;
Matthew Garrettad8f07c2009-01-07 18:08:56 -080076static struct backlight_device *dell_backlight_device;
77static struct rfkill *wifi_rfkill;
78static struct rfkill *bluetooth_rfkill;
79static struct rfkill *wwan_rfkill;
80
81static const struct dmi_system_id __initdata dell_device_table[] = {
82 {
83 .ident = "Dell laptop",
84 .matches = {
85 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
86 DMI_MATCH(DMI_CHASSIS_TYPE, "8"),
87 },
88 },
Erik Andrencb6a7932010-02-14 11:53:23 -050089 {
Rezwanul Kabir410d44c2010-06-23 12:02:43 -050090 .matches = {
91 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
92 DMI_MATCH(DMI_CHASSIS_TYPE, "9"), /*Laptop*/
93 },
94 },
95 {
Erik Andrencb6a7932010-02-14 11:53:23 -050096 .ident = "Dell Computer Corporation",
97 .matches = {
98 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
99 DMI_MATCH(DMI_CHASSIS_TYPE, "8"),
100 },
101 },
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800102 { }
103};
104
Mario Limoncielloe5fefd02010-02-09 17:41:03 -0500105static struct dmi_system_id __devinitdata dell_blacklist[] = {
106 /* Supported by compal-laptop */
107 {
108 .ident = "Dell Mini 9",
109 .matches = {
110 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
111 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 910"),
112 },
113 },
114 {
115 .ident = "Dell Mini 10",
116 .matches = {
117 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
118 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 1010"),
119 },
120 },
121 {
122 .ident = "Dell Mini 10v",
123 .matches = {
124 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
125 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 1011"),
126 },
127 },
128 {
Victor van den Elzenc3f755e2010-08-15 01:19:33 +0200129 .ident = "Dell Mini 1012",
130 .matches = {
131 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
132 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 1012"),
133 },
134 },
135 {
Mario Limoncielloe5fefd02010-02-09 17:41:03 -0500136 .ident = "Dell Inspiron 11z",
137 .matches = {
138 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
139 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 1110"),
140 },
141 },
142 {
143 .ident = "Dell Mini 12",
144 .matches = {
145 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
146 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 1210"),
147 },
148 },
149 {}
150};
151
Stuart Hayes116ee772010-02-10 14:12:13 -0500152static struct calling_interface_buffer *buffer;
Ingo Molnar94d8f782010-03-01 09:43:52 -0500153static struct page *bufferpage;
154static DEFINE_MUTEX(buffer_mutex);
Stuart Hayes116ee772010-02-10 14:12:13 -0500155
Matthew Garrettc6760ac2010-02-10 14:44:03 -0500156static int hwswitch_state;
157
Stuart Hayes116ee772010-02-10 14:12:13 -0500158static void get_buffer(void)
159{
160 mutex_lock(&buffer_mutex);
161 memset(buffer, 0, sizeof(struct calling_interface_buffer));
162}
163
164static void release_buffer(void)
165{
166 mutex_unlock(&buffer_mutex);
167}
168
Alan Jenkins4788df42009-08-19 15:06:50 +0100169static void __init parse_da_table(const struct dmi_header *dm)
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800170{
171 /* Final token is a terminator, so we don't want to copy it */
172 int tokens = (dm->length-11)/sizeof(struct calling_interface_token)-1;
173 struct calling_interface_structure *table =
174 container_of(dm, struct calling_interface_structure, header);
175
176 /* 4 bytes of table header, plus 7 bytes of Dell header, plus at least
177 6 bytes of entry */
178
179 if (dm->length < 17)
180 return;
181
182 da_command_address = table->cmdIOAddress;
183 da_command_code = table->cmdIOCode;
184
185 da_tokens = krealloc(da_tokens, (da_num_tokens + tokens) *
186 sizeof(struct calling_interface_token),
187 GFP_KERNEL);
188
189 if (!da_tokens)
190 return;
191
192 memcpy(da_tokens+da_num_tokens, table->tokens,
193 sizeof(struct calling_interface_token) * tokens);
194
195 da_num_tokens += tokens;
196}
197
Alan Jenkins4788df42009-08-19 15:06:50 +0100198static void __init find_tokens(const struct dmi_header *dm, void *dummy)
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800199{
200 switch (dm->type) {
201 case 0xd4: /* Indexed IO */
202 break;
203 case 0xd5: /* Protected Area Type 1 */
204 break;
205 case 0xd6: /* Protected Area Type 2 */
206 break;
207 case 0xda: /* Calling interface */
208 parse_da_table(dm);
209 break;
210 }
211}
212
213static int find_token_location(int tokenid)
214{
215 int i;
216 for (i = 0; i < da_num_tokens; i++) {
217 if (da_tokens[i].tokenID == tokenid)
218 return da_tokens[i].location;
219 }
220
221 return -1;
222}
223
224static struct calling_interface_buffer *
225dell_send_request(struct calling_interface_buffer *buffer, int class,
226 int select)
227{
228 struct smi_cmd command;
229
230 command.magic = SMI_CMD_MAGIC;
231 command.command_address = da_command_address;
232 command.command_code = da_command_code;
233 command.ebx = virt_to_phys(buffer);
234 command.ecx = 0x42534931;
235
236 buffer->class = class;
237 buffer->select = select;
238
239 dcdbas_smi_request(&command);
240
241 return buffer;
242}
243
244/* Derived from information in DellWirelessCtl.cpp:
245 Class 17, select 11 is radio control. It returns an array of 32-bit values.
246
Matthew Garrettc6760ac2010-02-10 14:44:03 -0500247 Input byte 0 = 0: Wireless information
248
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800249 result[0]: return code
250 result[1]:
251 Bit 0: Hardware switch supported
252 Bit 1: Wifi locator supported
253 Bit 2: Wifi is supported
254 Bit 3: Bluetooth is supported
255 Bit 4: WWAN is supported
256 Bit 5: Wireless keyboard supported
257 Bits 6-7: Reserved
258 Bit 8: Wifi is installed
259 Bit 9: Bluetooth is installed
260 Bit 10: WWAN is installed
261 Bits 11-15: Reserved
262 Bit 16: Hardware switch is on
263 Bit 17: Wifi is blocked
264 Bit 18: Bluetooth is blocked
265 Bit 19: WWAN is blocked
266 Bits 20-31: Reserved
267 result[2]: NVRAM size in bytes
268 result[3]: NVRAM format version number
Matthew Garrettc6760ac2010-02-10 14:44:03 -0500269
270 Input byte 0 = 2: Wireless switch configuration
271 result[0]: return code
272 result[1]:
273 Bit 0: Wifi controlled by switch
274 Bit 1: Bluetooth controlled by switch
275 Bit 2: WWAN controlled by switch
276 Bits 3-6: Reserved
277 Bit 7: Wireless switch config locked
278 Bit 8: Wifi locator enabled
279 Bits 9-14: Reserved
280 Bit 15: Wifi locator setting locked
281 Bits 16-31: Reserved
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800282*/
283
Johannes Berg19d337d2009-06-02 13:01:37 +0200284static int dell_rfkill_set(void *data, bool blocked)
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800285{
Johannes Berg624f0de2009-06-15 16:26:47 +0200286 int disable = blocked ? 1 : 0;
Johannes Berg19d337d2009-06-02 13:01:37 +0200287 unsigned long radio = (unsigned long)data;
Matthew Garrettc6760ac2010-02-10 14:44:03 -0500288 int hwswitch_bit = (unsigned long)data - 1;
Stuart Hayes116ee772010-02-10 14:12:13 -0500289 int ret = 0;
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800290
Stuart Hayes116ee772010-02-10 14:12:13 -0500291 get_buffer();
292 dell_send_request(buffer, 17, 11);
Mario Limoncielloec1722a2010-02-09 14:11:05 -0500293
Matthew Garrettc6760ac2010-02-10 14:44:03 -0500294 /* If the hardware switch controls this radio, and the hardware
Keng-Yu Lina3d77412011-02-15 17:36:07 +0800295 switch is disabled, don't allow changing the software state.
296 If the hardware switch is reported as not supported, always
297 fire the SMI to toggle the killswitch. */
Matthew Garrettc6760ac2010-02-10 14:44:03 -0500298 if ((hwswitch_state & BIT(hwswitch_bit)) &&
Keng-Yu Lina3d77412011-02-15 17:36:07 +0800299 !(buffer->output[1] & BIT(16)) &&
300 (buffer->output[1] & BIT(0))) {
Stuart Hayes116ee772010-02-10 14:12:13 -0500301 ret = -EINVAL;
302 goto out;
303 }
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800304
Stuart Hayes116ee772010-02-10 14:12:13 -0500305 buffer->input[0] = (1 | (radio<<8) | (disable << 16));
306 dell_send_request(buffer, 17, 11);
307
308out:
309 release_buffer();
310 return ret;
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800311}
312
Johannes Berg19d337d2009-06-02 13:01:37 +0200313static void dell_rfkill_query(struct rfkill *rfkill, void *data)
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800314{
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800315 int status;
Johannes Berg19d337d2009-06-02 13:01:37 +0200316 int bit = (unsigned long)data + 16;
Matthew Garrettc6760ac2010-02-10 14:44:03 -0500317 int hwswitch_bit = (unsigned long)data - 1;
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800318
Stuart Hayes116ee772010-02-10 14:12:13 -0500319 get_buffer();
320 dell_send_request(buffer, 17, 11);
321 status = buffer->output[1];
322 release_buffer();
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800323
Matthew Garrette1fbf342009-07-31 03:25:38 +0100324 rfkill_set_sw_state(rfkill, !!(status & BIT(bit)));
Matthew Garrettc6760ac2010-02-10 14:44:03 -0500325
326 if (hwswitch_state & (BIT(hwswitch_bit)))
327 rfkill_set_hw_state(rfkill, !(status & BIT(16)));
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800328}
329
Johannes Berg19d337d2009-06-02 13:01:37 +0200330static const struct rfkill_ops dell_rfkill_ops = {
331 .set_block = dell_rfkill_set,
332 .query = dell_rfkill_query,
333};
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800334
Keng-Yu Lin037accf2010-09-28 11:43:31 +0800335static struct dentry *dell_laptop_dir;
336
337static int dell_debugfs_show(struct seq_file *s, void *data)
338{
339 int status;
340
341 get_buffer();
342 dell_send_request(buffer, 17, 11);
343 status = buffer->output[1];
344 release_buffer();
345
346 seq_printf(s, "status:\t0x%X\n", status);
347 seq_printf(s, "Bit 0 : Hardware switch supported: %lu\n",
348 status & BIT(0));
349 seq_printf(s, "Bit 1 : Wifi locator supported: %lu\n",
350 (status & BIT(1)) >> 1);
351 seq_printf(s, "Bit 2 : Wifi is supported: %lu\n",
352 (status & BIT(2)) >> 2);
353 seq_printf(s, "Bit 3 : Bluetooth is supported: %lu\n",
354 (status & BIT(3)) >> 3);
355 seq_printf(s, "Bit 4 : WWAN is supported: %lu\n",
356 (status & BIT(4)) >> 4);
357 seq_printf(s, "Bit 5 : Wireless keyboard supported: %lu\n",
358 (status & BIT(5)) >> 5);
359 seq_printf(s, "Bit 8 : Wifi is installed: %lu\n",
360 (status & BIT(8)) >> 8);
361 seq_printf(s, "Bit 9 : Bluetooth is installed: %lu\n",
362 (status & BIT(9)) >> 9);
363 seq_printf(s, "Bit 10: WWAN is installed: %lu\n",
364 (status & BIT(10)) >> 10);
365 seq_printf(s, "Bit 16: Hardware switch is on: %lu\n",
366 (status & BIT(16)) >> 16);
367 seq_printf(s, "Bit 17: Wifi is blocked: %lu\n",
368 (status & BIT(17)) >> 17);
369 seq_printf(s, "Bit 18: Bluetooth is blocked: %lu\n",
370 (status & BIT(18)) >> 18);
371 seq_printf(s, "Bit 19: WWAN is blocked: %lu\n",
372 (status & BIT(19)) >> 19);
373
374 seq_printf(s, "\nhwswitch_state:\t0x%X\n", hwswitch_state);
375 seq_printf(s, "Bit 0 : Wifi controlled by switch: %lu\n",
376 hwswitch_state & BIT(0));
377 seq_printf(s, "Bit 1 : Bluetooth controlled by switch: %lu\n",
378 (hwswitch_state & BIT(1)) >> 1);
379 seq_printf(s, "Bit 2 : WWAN controlled by switch: %lu\n",
380 (hwswitch_state & BIT(2)) >> 2);
381 seq_printf(s, "Bit 7 : Wireless switch config locked: %lu\n",
382 (hwswitch_state & BIT(7)) >> 7);
383 seq_printf(s, "Bit 8 : Wifi locator enabled: %lu\n",
384 (hwswitch_state & BIT(8)) >> 8);
385 seq_printf(s, "Bit 15: Wifi locator setting locked: %lu\n",
386 (hwswitch_state & BIT(15)) >> 15);
387
388 return 0;
389}
390
391static int dell_debugfs_open(struct inode *inode, struct file *file)
392{
393 return single_open(file, dell_debugfs_show, inode->i_private);
394}
395
396static const struct file_operations dell_debugfs_fops = {
397 .owner = THIS_MODULE,
398 .open = dell_debugfs_open,
399 .read = seq_read,
400 .llseek = seq_lseek,
401 .release = single_release,
402};
403
Matthew Garrett814cb8a2009-12-09 18:23:36 +0000404static void dell_update_rfkill(struct work_struct *ignored)
405{
Keng-Yu Lina3d77412011-02-15 17:36:07 +0800406 int status;
407
408 get_buffer();
409 dell_send_request(buffer, 17, 11);
410 status = buffer->output[1];
411 release_buffer();
412
413 /* if hardware rfkill is not supported, set it explicitly */
414 if (!(status & BIT(0))) {
415 if (wifi_rfkill)
416 dell_rfkill_set((void *)1, !((status & BIT(17)) >> 17));
417 if (bluetooth_rfkill)
418 dell_rfkill_set((void *)2, !((status & BIT(18)) >> 18));
419 if (wwan_rfkill)
420 dell_rfkill_set((void *)3, !((status & BIT(19)) >> 19));
421 }
422
Matthew Garrett814cb8a2009-12-09 18:23:36 +0000423 if (wifi_rfkill)
424 dell_rfkill_query(wifi_rfkill, (void *)1);
425 if (bluetooth_rfkill)
426 dell_rfkill_query(bluetooth_rfkill, (void *)2);
427 if (wwan_rfkill)
428 dell_rfkill_query(wwan_rfkill, (void *)3);
429}
430static DECLARE_DELAYED_WORK(dell_rfkill_work, dell_update_rfkill);
431
432
Alan Jenkins4788df42009-08-19 15:06:50 +0100433static int __init dell_setup_rfkill(void)
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800434{
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800435 int status;
436 int ret;
437
Mario Limoncielloe5fefd02010-02-09 17:41:03 -0500438 if (dmi_check_system(dell_blacklist)) {
Joe Percheseb889522011-03-29 15:21:37 -0700439 pr_info("Blacklisted hardware detected - not enabling rfkill\n");
Mario Limoncielloe5fefd02010-02-09 17:41:03 -0500440 return 0;
441 }
442
Stuart Hayes116ee772010-02-10 14:12:13 -0500443 get_buffer();
444 dell_send_request(buffer, 17, 11);
445 status = buffer->output[1];
Matthew Garrettc6760ac2010-02-10 14:44:03 -0500446 buffer->input[0] = 0x2;
447 dell_send_request(buffer, 17, 11);
448 hwswitch_state = buffer->output[1];
Stuart Hayes116ee772010-02-10 14:12:13 -0500449 release_buffer();
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800450
451 if ((status & (1<<2|1<<8)) == (1<<2|1<<8)) {
Alan Jenkinsada32482009-08-19 15:06:49 +0100452 wifi_rfkill = rfkill_alloc("dell-wifi", &platform_device->dev,
453 RFKILL_TYPE_WLAN,
Johannes Berg19d337d2009-06-02 13:01:37 +0200454 &dell_rfkill_ops, (void *) 1);
455 if (!wifi_rfkill) {
456 ret = -ENOMEM;
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800457 goto err_wifi;
Johannes Berg19d337d2009-06-02 13:01:37 +0200458 }
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800459 ret = rfkill_register(wifi_rfkill);
460 if (ret)
461 goto err_wifi;
462 }
463
464 if ((status & (1<<3|1<<9)) == (1<<3|1<<9)) {
Alan Jenkinsada32482009-08-19 15:06:49 +0100465 bluetooth_rfkill = rfkill_alloc("dell-bluetooth",
466 &platform_device->dev,
Johannes Berg19d337d2009-06-02 13:01:37 +0200467 RFKILL_TYPE_BLUETOOTH,
468 &dell_rfkill_ops, (void *) 2);
469 if (!bluetooth_rfkill) {
470 ret = -ENOMEM;
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800471 goto err_bluetooth;
Johannes Berg19d337d2009-06-02 13:01:37 +0200472 }
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800473 ret = rfkill_register(bluetooth_rfkill);
474 if (ret)
475 goto err_bluetooth;
476 }
477
478 if ((status & (1<<4|1<<10)) == (1<<4|1<<10)) {
Alan Jenkinsada32482009-08-19 15:06:49 +0100479 wwan_rfkill = rfkill_alloc("dell-wwan",
480 &platform_device->dev,
481 RFKILL_TYPE_WWAN,
Johannes Berg19d337d2009-06-02 13:01:37 +0200482 &dell_rfkill_ops, (void *) 3);
483 if (!wwan_rfkill) {
484 ret = -ENOMEM;
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800485 goto err_wwan;
Johannes Berg19d337d2009-06-02 13:01:37 +0200486 }
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800487 ret = rfkill_register(wwan_rfkill);
488 if (ret)
489 goto err_wwan;
490 }
491
492 return 0;
493err_wwan:
Johannes Berg19d337d2009-06-02 13:01:37 +0200494 rfkill_destroy(wwan_rfkill);
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800495 if (bluetooth_rfkill)
Johannes Berg19d337d2009-06-02 13:01:37 +0200496 rfkill_unregister(bluetooth_rfkill);
497err_bluetooth:
498 rfkill_destroy(bluetooth_rfkill);
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800499 if (wifi_rfkill)
Johannes Berg19d337d2009-06-02 13:01:37 +0200500 rfkill_unregister(wifi_rfkill);
501err_wifi:
502 rfkill_destroy(wifi_rfkill);
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800503
504 return ret;
505}
506
Alan Jenkins4311bb22009-08-19 15:06:48 +0100507static void dell_cleanup_rfkill(void)
508{
509 if (wifi_rfkill) {
510 rfkill_unregister(wifi_rfkill);
511 rfkill_destroy(wifi_rfkill);
512 }
513 if (bluetooth_rfkill) {
514 rfkill_unregister(bluetooth_rfkill);
515 rfkill_destroy(bluetooth_rfkill);
516 }
517 if (wwan_rfkill) {
518 rfkill_unregister(wwan_rfkill);
519 rfkill_destroy(wwan_rfkill);
520 }
521}
522
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800523static int dell_send_intensity(struct backlight_device *bd)
524{
Stuart Hayes116ee772010-02-10 14:12:13 -0500525 int ret = 0;
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800526
Stuart Hayes116ee772010-02-10 14:12:13 -0500527 get_buffer();
528 buffer->input[0] = find_token_location(BRIGHTNESS_TOKEN);
529 buffer->input[1] = bd->props.brightness;
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800530
Stuart Hayes116ee772010-02-10 14:12:13 -0500531 if (buffer->input[0] == -1) {
532 ret = -ENODEV;
533 goto out;
534 }
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800535
536 if (power_supply_is_system_supplied() > 0)
Stuart Hayes116ee772010-02-10 14:12:13 -0500537 dell_send_request(buffer, 1, 2);
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800538 else
Stuart Hayes116ee772010-02-10 14:12:13 -0500539 dell_send_request(buffer, 1, 1);
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800540
Stuart Hayes116ee772010-02-10 14:12:13 -0500541out:
542 release_buffer();
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800543 return 0;
544}
545
546static int dell_get_intensity(struct backlight_device *bd)
547{
Stuart Hayes116ee772010-02-10 14:12:13 -0500548 int ret = 0;
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800549
Stuart Hayes116ee772010-02-10 14:12:13 -0500550 get_buffer();
551 buffer->input[0] = find_token_location(BRIGHTNESS_TOKEN);
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800552
Stuart Hayes116ee772010-02-10 14:12:13 -0500553 if (buffer->input[0] == -1) {
554 ret = -ENODEV;
555 goto out;
556 }
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800557
558 if (power_supply_is_system_supplied() > 0)
Stuart Hayes116ee772010-02-10 14:12:13 -0500559 dell_send_request(buffer, 0, 2);
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800560 else
Stuart Hayes116ee772010-02-10 14:12:13 -0500561 dell_send_request(buffer, 0, 1);
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800562
Stuart Hayes116ee772010-02-10 14:12:13 -0500563out:
564 release_buffer();
565 if (ret)
566 return ret;
567 return buffer->output[1];
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800568}
569
Lionel Debrouxacc24722010-11-16 14:14:02 +0100570static const struct backlight_ops dell_ops = {
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800571 .get_brightness = dell_get_intensity,
572 .update_status = dell_send_intensity,
573};
574
Axel Lin45191692010-07-05 17:21:10 +0800575static bool dell_laptop_i8042_filter(unsigned char data, unsigned char str,
Matthew Garrett814cb8a2009-12-09 18:23:36 +0000576 struct serio *port)
577{
578 static bool extended;
579
580 if (str & 0x20)
581 return false;
582
583 if (unlikely(data == 0xe0)) {
584 extended = true;
585 return false;
586 } else if (unlikely(extended)) {
587 switch (data) {
588 case 0x8:
589 schedule_delayed_work(&dell_rfkill_work,
590 round_jiffies_relative(HZ));
591 break;
592 }
593 extended = false;
594 }
595
596 return false;
597}
598
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800599static int __init dell_init(void)
600{
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800601 int max_intensity = 0;
602 int ret;
603
604 if (!dmi_check_system(dell_device_table))
605 return -ENODEV;
606
Jean Delvaree7a19c562009-03-30 21:46:44 +0200607 dmi_walk(find_tokens, NULL);
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800608
609 if (!da_tokens) {
Joe Percheseb889522011-03-29 15:21:37 -0700610 pr_info("Unable to find dmi tokens\n");
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800611 return -ENODEV;
612 }
613
Alan Jenkinsada32482009-08-19 15:06:49 +0100614 ret = platform_driver_register(&platform_driver);
615 if (ret)
616 goto fail_platform_driver;
617 platform_device = platform_device_alloc("dell-laptop", -1);
618 if (!platform_device) {
619 ret = -ENOMEM;
620 goto fail_platform_device1;
621 }
622 ret = platform_device_add(platform_device);
623 if (ret)
624 goto fail_platform_device2;
625
Stuart Hayes116ee772010-02-10 14:12:13 -0500626 /*
627 * Allocate buffer below 4GB for SMI data--only 32-bit physical addr
628 * is passed to SMI handler.
629 */
630 bufferpage = alloc_page(GFP_KERNEL | GFP_DMA32);
631
632 if (!bufferpage)
633 goto fail_buffer;
634 buffer = page_address(bufferpage);
635 mutex_init(&buffer_mutex);
636
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800637 ret = dell_setup_rfkill();
638
639 if (ret) {
Joe Percheseb889522011-03-29 15:21:37 -0700640 pr_warn("Unable to setup rfkill\n");
Alan Jenkins71e9dc72009-08-19 15:06:47 +0100641 goto fail_rfkill;
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800642 }
643
Matthew Garrett814cb8a2009-12-09 18:23:36 +0000644 ret = i8042_install_filter(dell_laptop_i8042_filter);
645 if (ret) {
Joe Percheseb889522011-03-29 15:21:37 -0700646 pr_warn("Unable to install key filter\n");
Matthew Garrett814cb8a2009-12-09 18:23:36 +0000647 goto fail_filter;
648 }
649
Keng-Yu Lin037accf2010-09-28 11:43:31 +0800650 dell_laptop_dir = debugfs_create_dir("dell_laptop", NULL);
651 if (dell_laptop_dir != NULL)
652 debugfs_create_file("rfkill", 0444, dell_laptop_dir, NULL,
653 &dell_debugfs_fops);
654
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800655#ifdef CONFIG_ACPI
656 /* In the event of an ACPI backlight being available, don't
657 * register the platform controller.
658 */
659 if (acpi_video_backlight_support())
660 return 0;
661#endif
662
Stuart Hayes116ee772010-02-10 14:12:13 -0500663 get_buffer();
664 buffer->input[0] = find_token_location(BRIGHTNESS_TOKEN);
665 if (buffer->input[0] != -1) {
666 dell_send_request(buffer, 0, 2);
667 max_intensity = buffer->output[3];
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800668 }
Stuart Hayes116ee772010-02-10 14:12:13 -0500669 release_buffer();
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800670
671 if (max_intensity) {
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500672 struct backlight_properties props;
673 memset(&props, 0, sizeof(struct backlight_properties));
Matthew Garrettbb7ca742011-03-22 16:30:21 -0700674 props.type = BACKLIGHT_PLATFORM;
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500675 props.max_brightness = max_intensity;
676 dell_backlight_device = backlight_device_register("dell_backlight",
677 &platform_device->dev,
678 NULL,
679 &dell_ops,
680 &props);
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800681
682 if (IS_ERR(dell_backlight_device)) {
683 ret = PTR_ERR(dell_backlight_device);
684 dell_backlight_device = NULL;
Alan Jenkins71e9dc72009-08-19 15:06:47 +0100685 goto fail_backlight;
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800686 }
687
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800688 dell_backlight_device->props.brightness =
689 dell_get_intensity(dell_backlight_device);
690 backlight_update_status(dell_backlight_device);
691 }
692
693 return 0;
Alan Jenkins71e9dc72009-08-19 15:06:47 +0100694
695fail_backlight:
Matthew Garrett814cb8a2009-12-09 18:23:36 +0000696 i8042_remove_filter(dell_laptop_i8042_filter);
Matthew Garrett92e00e42010-03-01 09:46:43 -0500697 cancel_delayed_work_sync(&dell_rfkill_work);
Matthew Garrett814cb8a2009-12-09 18:23:36 +0000698fail_filter:
Alan Jenkins4311bb22009-08-19 15:06:48 +0100699 dell_cleanup_rfkill();
Alan Jenkins71e9dc72009-08-19 15:06:47 +0100700fail_rfkill:
Stuart Hayes116ee772010-02-10 14:12:13 -0500701 free_page((unsigned long)bufferpage);
702fail_buffer:
Alan Jenkinsada32482009-08-19 15:06:49 +0100703 platform_device_del(platform_device);
704fail_platform_device2:
705 platform_device_put(platform_device);
706fail_platform_device1:
707 platform_driver_unregister(&platform_driver);
708fail_platform_driver:
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800709 kfree(da_tokens);
710 return ret;
711}
712
713static void __exit dell_exit(void)
714{
Keng-Yu Lin037accf2010-09-28 11:43:31 +0800715 debugfs_remove_recursive(dell_laptop_dir);
Matthew Garrett814cb8a2009-12-09 18:23:36 +0000716 i8042_remove_filter(dell_laptop_i8042_filter);
Matthew Garrett92e00e42010-03-01 09:46:43 -0500717 cancel_delayed_work_sync(&dell_rfkill_work);
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800718 backlight_device_unregister(dell_backlight_device);
Alan Jenkins4311bb22009-08-19 15:06:48 +0100719 dell_cleanup_rfkill();
Matthew Garrettfacd61d2010-02-09 14:03:04 -0500720 if (platform_device) {
Matthew Garrett92e00e42010-03-01 09:46:43 -0500721 platform_device_unregister(platform_device);
Matthew Garrettfacd61d2010-02-09 14:03:04 -0500722 platform_driver_unregister(&platform_driver);
723 }
Matthew Garrette5512602010-02-09 14:05:01 -0500724 kfree(da_tokens);
Stuart Hayes116ee772010-02-10 14:12:13 -0500725 free_page((unsigned long)buffer);
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800726}
727
728module_init(dell_init);
729module_exit(dell_exit);
730
731MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
732MODULE_DESCRIPTION("Dell laptop driver");
733MODULE_LICENSE("GPL");
734MODULE_ALIAS("dmi:*svnDellInc.:*:ct8:*");
Rezwanul Kabir410d44c2010-06-23 12:02:43 -0500735MODULE_ALIAS("dmi:*svnDellInc.:*:ct9:*");
Erik Andrencb6a7932010-02-14 11:53:23 -0500736MODULE_ALIAS("dmi:*svnDellComputerCorporation.:*:ct8:*");