wmi-acer - Fix error test for ACPI bus register
[acer_acpi.git] / acer_acpi.c
blob511ff68ade4c8ae88f9215c29bccc9f2a5b77403
1 /*
2 * Acer Laptop ACPI Extras
4 * Copyright (C) 2005-2007 E.M. Smith
5 * Copyright (C) 2007 Carlos Corbacho <cathectic@gmail.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * The devolpment page for this driver is located at
23 * http://code.google.com/p/aceracpi
25 * Credits:
27 * John Belmonte - the Toshiba ACPI driver originally adapted for this module.
28 * Julien Lerouge & Karol Kozimor - ASUS Acpi driver authors.
29 * Olaf Tauber - developer of acerhk, the inspiration to solve the 64-bit
30 * driver problem for my Aspire 5024.
31 * Mathieu Segaud - solved the ACPI problem that needed a double-modprobe
32 * in version 0.2 and below.
33 * Jim Ramsay - Figured out and added support for WMID interface
36 #define ACER_ACPI_VERSION "0.10.0"
39 * Comment the following line out to remove /proc support
41 #define CONFIG_PROC
43 #ifdef CONFIG_PROC
44 #define PROC_ACER "acer"
45 #include <linux/proc_fs.h>
46 #endif
48 #include <linux/kernel.h>
49 #include <linux/module.h>
50 #include <linux/init.h>
51 #include <linux/types.h>
52 #include <linux/delay.h>
53 #include <linux/version.h>
55 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,17)
56 #include <asm/uaccess.h>
57 #else
58 #include <linux/uaccess.h>
59 #endif
61 #include <linux/io.h>
62 #include <linux/dmi.h>
63 #include <linux/backlight.h>
64 #include <linux/leds.h>
65 #include <linux/platform_device.h>
67 #include <acpi/acpi_drivers.h>
69 #include "wmi-acer.h"
71 /* Workaround needed for older kernels */
72 #ifndef bool
73 #define bool int
74 #endif
76 MODULE_AUTHOR("Mark Smith, Carlos Corbacho");
77 MODULE_DESCRIPTION("Acer Laptop ACPI Extras Driver");
78 MODULE_LICENSE("GPL");
80 MODULE_ALIAS("dmi:*:*Acer*:*:");
82 #define ACER_LOGPREFIX "acer_acpi: "
83 #define ACER_ERR KERN_ERR ACER_LOGPREFIX
84 #define ACER_NOTICE KERN_NOTICE ACER_LOGPREFIX
85 #define ACER_INFO KERN_INFO ACER_LOGPREFIX
87 #define DEBUG(level, message...) { \
88 if (debug >= level) \
89 printk(KERN_DEBUG ACER_LOGPREFIX message);\
93 * The maximum temperature one can set for fan control override.
94 * Doesn't propably make much sense if over 80 degrees celsius though...
96 #define ACER_MAX_TEMPERATURE_OVERRIDE 150
99 * The following defines quirks to get some specific functions to work
100 * which are known to not be supported over ACPI (such as the mail LED
101 * on WMID based Acer's)
103 struct acer_quirks {
104 const char *vendor;
105 const char *model;
106 u16 quirks;
110 * Keyboard controller ports
112 #define ACER_KBD_STATUS_REG 0x64 /* Status register (R) */
113 #define ACER_KBD_CNTL_REG 0x64 /* Controller command register (W) */
114 #define ACER_KBD_DATA_REG 0x60 /* Keyboard data register (R/W) */
117 * Magic Number
118 * Meaning is unknown - this number is required for writing to ACPI for AMW0
119 * (it's also used in acerhk when directly accessing the EC)
121 #define ACER_AMW0_WRITE 0x9610
124 * Bit masks for the old AMW0 interface
126 #define ACER_AMW0_WIRELESS_MASK 0x35
127 #define ACER_AMW0_BLUETOOTH_MASK 0x34
128 #define ACER_AMW0_MAILLED_MASK 0x31
131 * Method IDs for new WMID interface
133 #define ACER_WMID_GET_WIRELESS_METHODID 1
134 #define ACER_WMID_GET_BLUETOOTH_METHODID 2
135 #define ACER_WMID_GET_BRIGHTNESS_METHODID 3
136 #define ACER_WMID_SET_WIRELESS_METHODID 4
137 #define ACER_WMID_SET_BLUETOOTH_METHODID 5
138 #define ACER_WMID_SET_BRIGHTNESS_METHODID 6
139 #define ACER_WMID_GET_THREEG_METHODID 10
140 #define ACER_WMID_SET_THREEG_METHODID 11
143 * Acer ACPI method GUIDs
145 #define AMW0_GUID1 "67C3371D-95A3-4C37-BB61-DD47B491DAAB"
146 #define WMID_GUID1 "6AF4F258-B401-42fd-BE91-3D4AC2D7C0D3"
147 #define WMID_GUID2 "95764E09-FB56-4e83-B31A-37761F60994A"
150 * Interface capability flags
152 #define ACER_CAP_MAILLED (1<<0)
153 #define ACER_CAP_WIRELESS (1<<1)
154 #define ACER_CAP_BLUETOOTH (1<<2)
155 #define ACER_CAP_BRIGHTNESS (1<<3)
156 #define ACER_CAP_THREEG (1<<4)
157 #define ACER_CAP_TOUCHPAD_READ (1<<5)
158 #define ACER_CAP_TEMPERATURE_OVERRIDE (1<<6)
159 #define ACER_CAP_ANY (0xFFFFFFFF)
162 * Interface type flags
164 enum interface_flags {
165 ACER_AMW0,
166 ACER_AMW0_V2,
167 ACER_WMID,
171 * Presumed start states -
172 * On some AMW0 laptops, we do not yet know how to get the device status from
173 * the EC, so we must store this ourselves.
175 * Plus, we can't tell which features are enabled or disabled on a specific
176 * model - e.g. The 5020 series can _support_ bluetooth; but the 5021 has no
177 * bluetooth, whilst the 5024 does. However, the BIOS identifies both laptops
178 * as 5020, and you can add bluetooth later.
180 * Basically the code works like this:
181 * - On init, any values specified on the commandline are set.
182 * - For interfaces where the current values cannot be detected and which
183 * have not been set on the commandline, we set them to some sane default
184 * (disabled)
186 * See AMW0_init and acer_commandline_init
189 #define ACER_DEFAULT_WIRELESS 0
190 #define ACER_DEFAULT_BLUETOOTH 0
191 #define ACER_DEFAULT_MAILLED 0
192 #define ACER_DEFAULT_THREEG 0
194 static int max_brightness = 0xF;
196 static int wireless = -1;
197 static int bluetooth = -1;
198 static int mailled = -1;
199 static int brightness = -1;
200 static int threeg = -1;
201 static int fan_temperature_override = -1;
202 static int debug = 0;
203 static int force_series;
205 module_param(mailled, int, 0444);
206 module_param(wireless, int, 0444);
207 module_param(bluetooth, int, 0444);
208 module_param(brightness, int, 0444);
209 module_param(threeg, int, 0444);
210 module_param(force_series, int, 0444);
211 module_param(fan_temperature_override, int, 0444);
212 module_param(debug, int, 0664);
213 MODULE_PARM_DESC(wireless, "Set initial state of Wireless hardware");
214 MODULE_PARM_DESC(bluetooth, "Set initial state of Bluetooth hardware");
215 MODULE_PARM_DESC(mailled, "Set initial state of Mail LED");
216 MODULE_PARM_DESC(brightness, "Set initial LCD backlight brightness");
217 MODULE_PARM_DESC(threeg, "Set initial state of 3G hardware");
218 MODULE_PARM_DESC(fan_temperature_override, "Set initial state of the 'FAN temperature-override'");
219 MODULE_PARM_DESC(debug, "Debugging verbosity level (0=least 2=most)");
220 MODULE_PARM_DESC(force_series, "Force a different laptop series for extra features (5020, 5720 or 2490)");
222 #ifdef CONFIG_PROC
223 struct ProcItem {
224 const char *name;
225 char *(*read_func) (char *, u32);
226 unsigned long (*write_func) (const char *, unsigned long, u32);
227 unsigned int capability;
230 static struct proc_dir_entry *acer_proc_dir;
231 #endif
234 * Wait for the keyboard controller to become ready
236 static int wait_kbd_write(void)
238 int i = 0;
239 while ((inb(ACER_KBD_STATUS_REG) & 0x02) && (i < 10000)) {
240 udelay(50);
241 i++;
243 return -(i == 10000);
246 static void send_kbd_cmd(u8 cmd, u8 val)
248 preempt_disable();
249 if (!wait_kbd_write())
250 outb(cmd, ACER_KBD_CNTL_REG);
251 if (!wait_kbd_write())
252 outb(val, ACER_KBD_DATA_REG);
253 preempt_enable_no_resched();
256 static void set_keyboard_quirk(void)
258 send_kbd_cmd(0x59, 0x90);
261 struct acer_data {
262 int mailled;
263 int wireless;
264 int bluetooth;
265 int threeg;
266 int brightness;
269 /* Each low-level interface must define at least some of the following */
270 struct Interface {
271 u32 type; /* WMI device type */
272 u32 capability; /* The capabilities this interface provides */
273 struct acer_data data; /* Private data for interface */
276 /* The static interface pointer, points to the currently detected interface */
277 static struct Interface *interface;
280 * Embedded Controller quirks
281 * Some laptops require us to directly access the EC to either enable or query
282 * features that are not available through ACPI.
285 struct quirk_entry {
286 u8 wireless;
287 u8 mailled;
288 u8 brightness;
289 u8 touchpad;
290 u8 temperature_override;
291 u8 mmkeys;
292 u8 bluetooth;
295 static struct quirk_entry *quirks;
297 static void set_quirks(void)
299 if (quirks->mailled != 0) {
300 interface->capability |= ACER_CAP_MAILLED;
301 DEBUG(1, "Using EC direct-access quirk for mail LED\n");
304 if (quirks->touchpad != 0) {
305 interface->capability |= ACER_CAP_TOUCHPAD_READ;
306 DEBUG(1, "Using EC direct-access quirk for reading touchpad status\n");
309 if (quirks->temperature_override != 0) {
310 interface->capability |= ACER_CAP_TEMPERATURE_OVERRIDE;
311 DEBUG(1, "Using EC direct-access quirk for temperature override setting (fan)\n");
314 if (quirks->brightness != 0) {
315 interface->capability |= ACER_CAP_BRIGHTNESS;
316 DEBUG(1, "Using EC direct-access quirk for backlight brightness\n");
319 if (quirks->mmkeys != 0) {
320 set_keyboard_quirk();
321 printk(ACER_INFO "Setting keyboard quirk to enable multimedia keys\n");
324 if (quirks->bluetooth != 0) {
325 interface->capability |= ACER_CAP_BLUETOOTH;
326 DEBUG(1, "Using EC direct-access quirk for bluetooth\n");
329 if (quirks->wireless != 0) {
330 interface->capability |= ACER_CAP_WIRELESS;
331 DEBUG(1, "Using EC direct-access quirk for wireless\n");
335 static int dmi_matched(struct dmi_system_id *dmi)
337 quirks = dmi->driver_data;
338 return 0;
341 static struct quirk_entry quirk_unknown = {
344 /* Same Mail LED quirk as TM2490, but does not require keyboard quirk */
345 static struct quirk_entry quirk_acer_aspire_5100 = {
346 .mailled = 1,
349 static struct quirk_entry quirk_acer_travelmate_2490 = {
350 .mmkeys = 1,
351 .mailled = 1,
352 .temperature_override = 1,
353 .touchpad = 1,
356 static struct quirk_entry quirk_acer_travelmate_5720 = {
357 .touchpad = 2,
360 static struct dmi_system_id acer_quirks[] = {
362 .callback = dmi_matched,
363 .ident = "Acer Aspire 3100",
364 .matches = {
365 DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
366 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 3100"),
368 .driver_data = &quirk_acer_aspire_5100,
371 .callback = dmi_matched,
372 .ident = "Acer Aspire 5100",
373 .matches = {
374 DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
375 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5100"),
377 .driver_data = &quirk_acer_aspire_5100,
380 .callback = dmi_matched,
381 .ident = "Acer Aspire 5630",
382 .matches = {
383 DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
384 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5630"),
386 .driver_data = &quirk_acer_travelmate_2490,
389 .callback = dmi_matched,
390 .ident = "Acer Aspire 5650",
391 .matches = {
392 DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
393 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5650"),
395 .driver_data = &quirk_acer_travelmate_2490,
398 .callback = dmi_matched,
399 .ident = "Acer Aspire 5680",
400 .matches = {
401 DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
402 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5680"),
404 .driver_data = &quirk_acer_travelmate_2490,
407 .callback = dmi_matched,
408 .ident = "Acer Extensa 5220",
409 .matches = {
410 DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
411 DMI_MATCH(DMI_PRODUCT_NAME, "Extensa 5220"),
413 .driver_data = &quirk_acer_travelmate_5720,
416 .callback = dmi_matched,
417 .ident = "Acer TravelMate 2490",
418 .matches = {
419 DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
420 DMI_MATCH(DMI_PRODUCT_NAME, "TravelMate 2490"),
422 .driver_data = &quirk_acer_travelmate_2490,
425 .callback = dmi_matched,
426 .ident = "Acer TravelMate 5720",
427 .matches = {
428 DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
429 DMI_MATCH(DMI_PRODUCT_NAME, "TravelMate 5720"),
431 .driver_data = &quirk_acer_travelmate_5720,
436 /* Find which quirks are needed for a particular vendor/ model pair */
437 static void find_quirks(void)
439 DEBUG (1, "Looking for quirks\n");
440 if (!force_series) {
441 dmi_check_system(acer_quirks);
442 } else if (force_series == 2490) {
443 DEBUG(0, "Forcing Acer TravelMate 2490\n");
444 quirks = &quirk_acer_travelmate_2490;
447 if (quirks == NULL) {
448 DEBUG(1, "No quirks known for this laptop\n");
449 quirks = &quirk_unknown;
451 set_quirks();
455 * General interface convenience methods
458 static bool has_cap(u32 cap)
460 if ((interface->capability & cap) != 0) {
461 return 1;
463 return 0;
467 * Old interface (now known as the AMW0 interface)
469 struct WMAB_args {
470 u32 eax;
471 u32 ebx;
472 u32 ecx;
473 u32 edx;
476 static acpi_status WMAB_execute(struct WMAB_args *regbuf, struct acpi_buffer *result)
478 struct acpi_buffer input;
479 acpi_status status;
480 input.length = sizeof(struct WMAB_args);
481 input.pointer = (u8*)regbuf;
483 status = wmi_acer_evaluate_method(AMW0_GUID1, 1, 1, &input, result);
484 DEBUG(2, " Args: 0x%08x 0x%08x 0x%08x 0x%08x\n", regbuf->eax, regbuf->ebx, regbuf->ecx, regbuf->edx );
486 return status;
489 static acpi_status AMW0_get_bool(bool *value, u32 cap, struct Interface *iface)
491 u8 result;
493 DEBUG(2, " AMW0_get_bool: cap=%d\n", cap);
494 switch (cap) {
495 case ACER_CAP_MAILLED:
496 switch (quirks->mailled) {
497 default:
498 ec_read(0x0A, &result);
499 *value = (result >> 7) & 0x01;
500 return 0;
502 break;
503 case ACER_CAP_WIRELESS:
504 switch (quirks->wireless) {
505 default:
506 ec_read(0x0A, &result);
507 *value = (result >> 2) & 0x01;
508 return 0;
510 break;
511 case ACER_CAP_BLUETOOTH:
512 switch (quirks->bluetooth) {
513 default:
514 ec_read(0x0A, &result);
515 *value = (result >> 4) & 0x01;
516 return 0;
518 break;
519 default:
520 return AE_BAD_ADDRESS;
522 return AE_OK;
525 static acpi_status AMW0_set_bool(bool value, u32 cap, struct Interface *iface)
527 struct WMAB_args args;
529 args.eax = ACER_AMW0_WRITE;
530 args.ebx = value ? (1<<8) : 0;
531 args.ecx = args.edx = 0;
533 switch (cap) {
534 case ACER_CAP_MAILLED:
535 args.ebx |= ACER_AMW0_MAILLED_MASK;
536 break;
537 case ACER_CAP_WIRELESS:
538 args.ebx |= ACER_AMW0_WIRELESS_MASK;
539 break;
540 case ACER_CAP_BLUETOOTH:
541 args.ebx |= ACER_AMW0_BLUETOOTH_MASK;
542 break;
543 default:
544 return AE_BAD_ADDRESS;
547 /* Actually do the set */
548 return WMAB_execute(&args, NULL);
551 static acpi_status AMW0_get_u8(u8 *value, u32 cap, struct Interface *iface) {
552 switch (cap) {
553 case ACER_CAP_BRIGHTNESS:
554 switch (quirks->brightness) {
555 default:
556 return ec_read(0x83, value);
558 break;
559 default:
560 return AE_BAD_ADDRESS;
562 return AE_OK;
565 static acpi_status AMW0_set_u8(u8 value, u32 cap, struct Interface *iface) {
566 switch (cap) {
567 case ACER_CAP_BRIGHTNESS:
568 switch (quirks->brightness) {
569 case 1:
570 return ec_write(0x83, value);
571 default:
572 return AE_BAD_ADDRESS;
573 break;
575 default:
576 return AE_BAD_ADDRESS;
578 return AE_OK;
581 static struct Interface AMW0_interface = {
582 .type = ACER_AMW0,
583 .capability = (
584 ACER_CAP_MAILLED |
585 ACER_CAP_WIRELESS |
586 ACER_CAP_BLUETOOTH
590 static struct Interface AMW0_V2_interface = {
591 .type = ACER_AMW0_V2,
592 .capability = (
593 ACER_CAP_BRIGHTNESS |
594 ACER_CAP_MAILLED
599 * New interface (The WMID interface)
601 static acpi_status
602 WMI_execute_u32(u32 method_id, u32 in, u32 *out)
604 struct acpi_buffer input = { (acpi_size) sizeof(u32), (void *)(&in) };
605 struct acpi_buffer result = { ACPI_ALLOCATE_BUFFER, NULL };
606 union acpi_object *obj;
607 u32 tmp;
608 acpi_status status;
610 DEBUG(2, " WMI_execute_u32:\n");
611 status = wmi_acer_evaluate_method(WMID_GUID1, 1, method_id, &input, &result);
612 DEBUG(2, " In: 0x%08x\n", in);
614 if (ACPI_FAILURE(status))
615 return status;
617 obj = (union acpi_object *) result.pointer;
618 if (obj && obj->type == ACPI_TYPE_BUFFER && obj->buffer.length == sizeof(u32)) {
619 tmp = *((u32 *) obj->buffer.pointer);
620 DEBUG(2, " Out: 0x%08x\n", tmp);
621 } else {
622 tmp = 0;
623 if (obj) {
624 DEBUG(2, " Got unexpected result of type %d\n", obj->type);
625 } else {
626 DEBUG(2, " Got unexpected null result\n");
630 if (out)
631 *out = tmp;
633 if (result.length > 0 && result.pointer)
634 kfree(result.pointer);
636 DEBUG(2, " Returning from WMI_execute_u32:\n");
637 return status;
640 static acpi_status WMID_get_u8(u8 *value, u32 cap, struct Interface *iface) {
641 acpi_status status;
642 u32 result;
643 u32 method_id = 0;
645 DEBUG(2, " WMID_get_u8: cap=%d\n", cap);
646 switch (cap) {
647 case ACER_CAP_WIRELESS:
648 method_id = ACER_WMID_GET_WIRELESS_METHODID;
649 break;
650 case ACER_CAP_BLUETOOTH:
651 method_id = ACER_WMID_GET_BLUETOOTH_METHODID;
652 break;
653 case ACER_CAP_BRIGHTNESS:
654 method_id = ACER_WMID_GET_BRIGHTNESS_METHODID;
655 break;
656 case ACER_CAP_THREEG:
657 method_id = ACER_WMID_GET_THREEG_METHODID;
658 break;
659 case ACER_CAP_MAILLED:
660 if (quirks->mailled == 1) {
661 ec_read(0x9f, value);
662 *value &= 0x01;
663 return 0;
665 case ACER_CAP_TOUCHPAD_READ:
666 switch (quirks->touchpad) {
667 case 1:
668 ec_read(0x9e, value);
669 *value = 1 - ((*value >> 3) & 0x01);
670 return 0;
671 case 2:
672 ec_read(0x74, value);
673 *value = ((*value >> 3) & 0x01);
674 return 0;
675 default:
676 break;
678 case ACER_CAP_TEMPERATURE_OVERRIDE:
679 if (quirks->temperature_override == 1) {
680 ec_read(0xa9, value);
681 return 0;
683 default:
684 return AE_BAD_ADDRESS;
686 status = WMI_execute_u32(method_id, 0, &result);
687 DEBUG(2, " WMI_execute_u32 status=%d:\n", status);
689 if (ACPI_SUCCESS(status))
690 *value = (u8)result;
692 DEBUG(2, " Returning from WMID_get_u8:\n");
693 return status;
696 static acpi_status WMID_set_u8(u8 value, u32 cap, struct Interface *iface) {
697 u32 method_id = 0;
699 switch (cap) {
700 case ACER_CAP_BRIGHTNESS:
701 method_id = ACER_WMID_SET_BRIGHTNESS_METHODID;
702 break;
703 case ACER_CAP_WIRELESS:
704 method_id = ACER_WMID_SET_WIRELESS_METHODID;
705 break;
706 case ACER_CAP_BLUETOOTH:
707 method_id = ACER_WMID_SET_BLUETOOTH_METHODID;
708 break;
709 case ACER_CAP_THREEG:
710 method_id = ACER_WMID_SET_THREEG_METHODID;
711 break;
712 case ACER_CAP_MAILLED:
713 if (quirks->mailled == 1) {
714 send_kbd_cmd(0x59, value ? 0x92 : 0x93);
715 return 0;
717 case ACER_CAP_TEMPERATURE_OVERRIDE:
718 if (quirks->temperature_override == 1) {
719 ec_write(0xa9, value);
720 return 0;
722 default:
723 return AE_BAD_ADDRESS;
725 return WMI_execute_u32(method_id, (u32)value, NULL);
728 static acpi_status WMID_set_capabilities(void) {
729 struct acpi_buffer out = {ACPI_ALLOCATE_BUFFER, NULL};
730 union acpi_object *obj;
731 acpi_status status;
732 u32 devices;
734 status = wmi_acer_query_block(WMID_GUID2, 1, &out);
735 if (ACPI_FAILURE(status))
736 return status;
738 obj = (union acpi_object *) out.pointer;
739 if (obj && obj->type == ACPI_TYPE_BUFFER && obj->buffer.length == sizeof(u32)) {
740 devices = *((u32 *) obj->buffer.pointer);
741 } else {
742 return AE_ERROR;
745 /* Not sure on the meaning of the relevant bits yet */
746 interface->capability |= ACER_CAP_WIRELESS;
747 interface->capability |= ACER_CAP_THREEG;
749 if (devices & 0x10) {
750 DEBUG(1, "Bluetooth hardware available - enabling\n");
751 interface->capability |= ACER_CAP_BLUETOOTH;
754 if (!(devices & 0x20)) {
755 DEBUG(1, "Maximum brightness quirk detected - enabling\n");
756 max_brightness = 0x9;
759 return status;
763 static struct Interface WMID_interface = {
764 .type = ACER_WMID,
765 .capability = ACER_CAP_BRIGHTNESS,
768 #ifdef CONFIG_PROC
770 * High-level Procfs file handlers
773 static int
774 dispatch_read(char *page, char **start, off_t off, int count, int *eof,
775 struct ProcItem *item)
777 char *p = page;
778 int len;
780 DEBUG(2, " dispatch_read: \n");
781 if (off == 0)
782 p = item->read_func(p, item->capability);
783 len = (p - page);
784 if (len <= off + count)
785 *eof = 1;
786 *start = page + off;
787 len -= off;
788 if (len > count)
789 len = count;
790 if (len < 0)
791 len = 0;
792 return len;
795 static int
796 dispatch_write(struct file *file, const char __user *buffer,
797 unsigned long count, struct ProcItem *item)
799 int result;
800 char *tmp_buffer;
803 * Arg buffer points to userspace memory, which can't be accessed
804 * directly. Since we're making a copy, zero-terminate the
805 * destination so that sscanf can be used on it safely.
807 tmp_buffer = kmalloc(count + 1, GFP_KERNEL);
808 if (copy_from_user(tmp_buffer, buffer, count)) {
809 result = -EFAULT;
810 } else {
811 tmp_buffer[count] = 0;
812 result = item->write_func(tmp_buffer, count, item->capability);
814 kfree(tmp_buffer);
815 return result;
817 #endif
820 * Generic Device (interface-independent)
823 static acpi_status get_bool(bool *value, u32 cap) {
824 acpi_status status = AE_BAD_ADDRESS;
825 u8 tmp = 0;
827 DEBUG(2, " get_bool: cap=%d, interface type=%d\n",
828 cap, interface->type);
829 switch (interface->type) {
830 case ACER_AMW0:
831 status = AMW0_get_bool(value, cap, interface);
832 break;
833 case ACER_AMW0_V2:
834 if (cap == ACER_CAP_MAILLED) {
835 status = AMW0_get_bool(value, cap, interface);
836 break;
838 case ACER_WMID:
839 status = WMID_get_u8(&tmp, cap, interface);
840 *value = (tmp == 1) ? 1 : 0;
841 break;
843 DEBUG(2, " Returning from get_bool:\n");
844 return status;
847 static acpi_status set_bool(int value, u32 cap) {
848 acpi_status status = AE_BAD_PARAMETER;
850 DEBUG(2, " set_bool: cap=%d, interface type=%d, value=%d\n",
851 cap, interface->type, value);
852 if ((value == 0 || value == 1) && (interface->capability & cap)) {
853 switch (interface->type) {
854 case ACER_AMW0:
855 status = AMW0_set_bool(value == 1, cap, interface);
856 break;
857 case ACER_AMW0_V2:
858 if (cap == ACER_CAP_MAILLED) {
859 status = AMW0_set_bool(value == 1, cap,
860 interface);
861 break;
863 case ACER_WMID:
864 status = WMID_set_u8(value == 1, cap, interface);
865 break;
868 return status;
871 static acpi_status get_u8(u8 *value, u32 cap) {
872 DEBUG(2, " get_u8: cap=%d\n", cap);
873 switch (interface->type) {
874 case ACER_AMW0:
875 return AMW0_get_u8(value, cap, interface);
876 break;
877 case ACER_AMW0_V2:
878 case ACER_WMID:
879 return WMID_get_u8(value, cap, interface);
880 break;
881 default:
882 return AE_BAD_ADDRESS;
886 static acpi_status set_u8(u8 value, u8 min, u8 max, u32 cap) {
888 DEBUG(2, " set_u8: cap=%d, interface type=%d, value=%d\n",
889 cap, interface->type, value);
891 if ((value >= min && value <= max) && (interface->capability & cap) ) {
892 switch (interface->type) {
893 case ACER_AMW0:
894 return AMW0_set_u8(value, cap, interface);
895 case ACER_AMW0_V2:
896 case ACER_WMID:
897 return WMID_set_u8(value, cap, interface);
898 default:
899 return AE_BAD_PARAMETER;
902 return AE_BAD_PARAMETER;
905 /* Each _u8 needs a small wrapper that sets the boundary values */
906 static acpi_status set_brightness(u8 value)
908 return set_u8(value, 0, max_brightness, ACER_CAP_BRIGHTNESS);
911 static acpi_status set_temperature_override(u8 value)
913 return set_u8(value, 0, ACER_MAX_TEMPERATURE_OVERRIDE, ACER_CAP_TEMPERATURE_OVERRIDE);
916 static void __init acer_commandline_init(void)
918 DEBUG(1, "Commandline args: mailled(%d) wireless(%d) bluetooth(%d) brightness(%d)\n",
919 mailled, wireless, bluetooth, brightness);
922 * These will all fail silently if the value given is invalid, or the
923 * capability isn't available on the given interface
925 set_bool(mailled, ACER_CAP_MAILLED);
926 set_bool(wireless, ACER_CAP_WIRELESS);
927 set_bool(bluetooth, ACER_CAP_BLUETOOTH);
928 set_bool(threeg, ACER_CAP_THREEG);
929 set_temperature_override(fan_temperature_override);
930 set_brightness((u8)brightness);
933 #ifdef CONFIG_PROC
935 * Procfs interface (deprecated)
937 static char *read_bool(char *p, u32 cap)
939 bool result;
940 acpi_status status;
942 DEBUG(2, " read_bool: cap=%d\n", cap);
943 status = get_bool(&result, cap);
944 if (ACPI_SUCCESS(status))
945 p += sprintf(p, "%d\n", result);
946 else
947 p += sprintf(p, "Read error" );
948 return p;
951 static unsigned long write_bool(const char *buffer, unsigned long count, u32 cap)
953 int value;
955 DEBUG(2, " write_bool: cap=%d, interface type=%d\n buffer=%s\n",
956 cap, interface->type, buffer);
958 if (sscanf(buffer, "%i", &value) == 1) {
959 acpi_status status = set_bool(value, cap);
960 if (ACPI_FAILURE(status))
961 return -EINVAL;
962 } else {
963 return -EINVAL;
965 return count;
968 static char *read_u8(char *p, u32 cap)
970 u8 result;
971 acpi_status status;
973 DEBUG(2, " read_u8: cap=%d\n", cap);
974 status = get_u8(&result, cap);
975 if (ACPI_SUCCESS(status))
976 p += sprintf(p, "%u\n", result);
977 else
978 p += sprintf(p, "Read error" );
979 return p;
982 static unsigned long write_u8(const char *buffer, unsigned long count, u32 cap)
984 int value;
985 acpi_status (*set_method)(u8);
987 /* Choose the appropriate set_u8 wrapper here, based on the capability */
988 switch (cap) {
989 case ACER_CAP_BRIGHTNESS:
990 set_method = set_brightness;
991 break;
992 case ACER_CAP_TEMPERATURE_OVERRIDE:
993 set_method = set_temperature_override;
994 break;
995 default:
996 return -EINVAL;
999 if (sscanf(buffer, "%i", &value) == 1) {
1000 acpi_status status = (*set_method)(value);
1001 if (ACPI_FAILURE(status))
1002 return -EINVAL;
1003 } else {
1004 return -EINVAL;
1006 return count;
1009 static char *read_version(char *p, u32 cap)
1011 p += sprintf(p, "%s\n", ACER_ACPI_VERSION);
1012 return p;
1015 static char *read_interface(char *p, u32 cap)
1017 p += sprintf(p, "%s\n", (interface->type == ACER_AMW0 ) ? "AMW0": "WMID");
1018 return p;
1021 static struct ProcItem proc_items[] = {
1022 {"mailled", read_bool, write_bool, ACER_CAP_MAILLED},
1023 {"bluetooth", read_bool, write_bool, ACER_CAP_BLUETOOTH},
1024 {"wireless", read_bool, write_bool, ACER_CAP_WIRELESS},
1025 {"brightness", read_u8, write_u8, ACER_CAP_BRIGHTNESS},
1026 {"threeg", read_bool, write_bool, ACER_CAP_THREEG},
1027 {"touchpad", read_bool, NULL, ACER_CAP_TOUCHPAD_READ},
1028 {"fan_temperature_override", read_u8, write_u8, ACER_CAP_TEMPERATURE_OVERRIDE},
1029 {"version", read_version, NULL, ACER_CAP_ANY},
1030 {"interface", read_interface, NULL, ACER_CAP_ANY},
1031 {NULL}
1034 static int __init add_proc_entries(void)
1036 struct proc_dir_entry *proc;
1037 struct ProcItem *item;
1039 for (item = proc_items; item->name; ++item) {
1041 * Only add the proc file if the current interface actually
1042 * supports it
1044 if (interface->capability & item->capability) {
1045 proc = create_proc_read_entry(item->name,
1046 S_IFREG | S_IRUGO | S_IWUSR,
1047 acer_proc_dir,
1048 (read_proc_t *) dispatch_read,
1049 item);
1050 if (proc)
1051 proc->owner = THIS_MODULE;
1052 if (proc && item->write_func)
1053 proc->write_proc = (write_proc_t *) dispatch_write;
1057 return 0;
1060 static int __exit remove_proc_entries(void)
1062 struct ProcItem *item;
1064 for (item = proc_items; item->name; ++item)
1065 remove_proc_entry(item->name, acer_proc_dir);
1066 return 0;
1068 #endif
1071 * LED device (Mail LED only, no other LEDs known yet)
1073 static void mail_led_set(struct led_classdev *led_cdev, enum led_brightness value)
1075 bool tmp = value;
1076 set_bool(tmp, ACER_CAP_MAILLED);
1079 static struct led_classdev mail_led = {
1080 .name = "acer_acpi:mail",
1081 .brightness_set = mail_led_set,
1084 static void acer_led_init(struct device *dev)
1086 led_classdev_register(dev, &mail_led);
1089 static void acer_led_exit(void)
1091 led_classdev_unregister(&mail_led);
1094 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18)
1096 * Backlight device
1098 static struct backlight_device *acer_backlight_device;
1100 static int read_brightness(struct backlight_device *bd)
1102 u8 value;
1103 get_u8(&value, ACER_CAP_BRIGHTNESS);
1104 return value;
1107 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,20)
1108 static int update_bl_status(struct backlight_device *bd)
1110 set_brightness(bd->props->brightness);
1111 return 0;
1114 static struct backlight_properties acer_backlight_properties = {
1115 .get_brightness = read_brightness,
1116 .update_status = update_bl_status,
1119 static int __init acer_backlight_init(struct device *dev)
1121 struct backlight_device *bd;
1123 DEBUG(1, "Loading backlight driver\n");
1124 bd = backlight_device_register("acer_acpi", dev, NULL, &acer_backlight_properties);
1125 if (IS_ERR(bd)) {
1126 printk(ACER_ERR "Could not register Acer backlight device\n");
1127 acer_backlight_device = NULL;
1128 return PTR_ERR(bd);
1131 acer_backlight_device = bd;
1133 bd->props->max_brightness = max_brightness;
1134 return 0;
1136 #else
1137 static int update_bl_status(struct backlight_device *bd)
1139 set_brightness(bd->props.brightness);
1140 return 0;
1143 static struct backlight_ops acer_backlight_ops = {
1144 .get_brightness = read_brightness,
1145 .update_status = update_bl_status,
1148 static int __init acer_backlight_init(struct device *dev)
1150 struct backlight_device *bd;
1152 DEBUG(1, "Loading backlight driver\n");
1153 bd = backlight_device_register("acer_acpi", dev, NULL, &acer_backlight_ops);
1154 if (IS_ERR(bd)) {
1155 printk(ACER_ERR "Could not register Acer backlight device\n");
1156 acer_backlight_device = NULL;
1157 return PTR_ERR(bd);
1160 acer_backlight_device = bd;
1162 bd->props.max_brightness = max_brightness;
1163 bd->props.brightness = read_brightness(NULL);
1164 backlight_update_status(bd);
1165 return 0;
1167 #endif
1169 static void __exit acer_backlight_exit(void)
1171 backlight_device_unregister(acer_backlight_device);
1173 #endif
1176 * Read/ write bool sysfs macro
1178 #define show_set_bool(value, cap) \
1179 static ssize_t \
1180 show_bool_##value(struct device *dev, struct device_attribute *attr, \
1181 char *buf) \
1183 bool result; \
1184 acpi_status status = get_bool(&result, cap); \
1185 if (ACPI_SUCCESS(status)) \
1186 return sprintf(buf, "%d\n", result); \
1187 return sprintf(buf, "Read error" ); \
1190 static ssize_t \
1191 set_bool_##value(struct device *dev, struct device_attribute *attr, \
1192 const char *buf, size_t count) \
1194 bool tmp = simple_strtoul(buf, NULL, 10); \
1195 acpi_status status = set_bool(tmp, cap); \
1196 if (ACPI_FAILURE(status)) \
1197 return -EINVAL; \
1198 return count; \
1200 static DEVICE_ATTR(value, S_IWUGO | S_IRUGO | S_IWUSR, \
1201 show_bool_##value, set_bool_##value);
1203 show_set_bool(wireless, ACER_CAP_WIRELESS);
1204 show_set_bool(bluetooth, ACER_CAP_BLUETOOTH);
1205 show_set_bool(threeg, ACER_CAP_THREEG);
1206 show_set_bool(fan_temperature_override, ACER_CAP_TEMPERATURE_OVERRIDE);
1209 * Read-only bool sysfs macro
1211 #define show_bool(value, cap) \
1212 static ssize_t \
1213 show_bool_##value(struct device *dev, struct device_attribute *attr, \
1214 char *buf) \
1216 bool result; \
1217 acpi_status status = get_bool(&result, cap); \
1218 if (ACPI_SUCCESS(status)) \
1219 return sprintf(buf, "%d\n", result); \
1220 return sprintf(buf, "Read error" ); \
1222 static DEVICE_ATTR(value, S_IWUGO | S_IRUGO | S_IWUSR, \
1223 show_bool_##value, NULL);
1225 show_bool(touchpad, ACER_CAP_TOUCHPAD_READ);
1228 * Read interface sysfs macro
1230 static ssize_t show_interface(struct device *dev, struct device_attribute *attr,
1231 char *buf)
1233 switch (interface->type) {
1234 case ACER_AMW0:
1235 return sprintf(buf, "AMW0\n");
1236 case ACER_AMW0_V2:
1237 return sprintf(buf, "AMW0 v2\n");
1238 case ACER_WMID:
1239 return sprintf(buf, "WMID\n");
1240 default:
1241 return sprintf(buf, "Error!\n");
1245 static DEVICE_ATTR(interface, S_IWUGO | S_IRUGO | S_IWUSR, show_interface, NULL);
1247 static ssize_t show_devices(struct device *dev, struct device_attribute *attr,
1248 char *buf)
1250 struct acpi_buffer out = {ACPI_ALLOCATE_BUFFER, NULL};
1251 union acpi_object *obj;
1252 acpi_status status;
1253 u32 tmp;
1255 status = wmi_acer_query_block(WMID_GUID2, 1, &out);
1256 if (ACPI_FAILURE(status))
1257 return sprintf(buf, "Error\n");
1259 obj = (union acpi_object *) out.pointer;
1260 if (obj && obj->type == ACPI_TYPE_BUFFER && obj->buffer.length == sizeof(u32)) {
1261 tmp = *((u32 *) obj->buffer.pointer);
1262 } else {
1263 return sprintf(buf, "Error\n");
1266 return sprintf(buf, "%u\n", tmp);
1269 static DEVICE_ATTR(devices, S_IWUGO | S_IRUGO | S_IWUSR, show_devices, NULL);
1272 * Platform device
1274 static int __devinit acer_platform_probe(struct platform_device *device)
1276 if (has_cap(ACER_CAP_MAILLED))
1277 acer_led_init(&device->dev);
1278 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18)
1279 if (has_cap(ACER_CAP_BRIGHTNESS))
1280 acer_backlight_init(&device->dev);
1281 #endif
1282 return 0;
1285 static int acer_platform_remove(struct platform_device *device)
1287 if (has_cap(ACER_CAP_MAILLED))
1288 acer_led_exit();
1289 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18)
1290 if (has_cap(ACER_CAP_BRIGHTNESS))
1291 acer_backlight_exit();
1292 #endif
1293 return 0;
1296 static int acer_platform_suspend(struct platform_device *device, pm_message_t state)
1299 * WMID fix for suspend-to-disk - save all current states now so we can
1300 * restore them on resume
1302 bool value;
1303 u8 u8value;
1304 struct acer_data *data = &interface->data;
1306 #define save_bool_device(device, cap) \
1307 if (has_cap(cap)) {\
1308 get_bool(&value, cap);\
1309 data->device = value;\
1312 #define save_u8_device(device, cap) \
1313 if (has_cap(cap)) {\
1314 get_u8(&u8value, cap);\
1315 data->device = u8value;\
1318 save_bool_device(wireless, ACER_CAP_WIRELESS);
1319 save_bool_device(bluetooth, ACER_CAP_BLUETOOTH);
1320 save_bool_device(threeg, ACER_CAP_THREEG);
1321 save_bool_device(mailled, ACER_CAP_MAILLED);
1322 save_u8_device(brightness, ACER_CAP_BRIGHTNESS);
1324 return 0;
1327 static int acer_platform_resume(struct platform_device *device)
1329 struct acer_data *data = &interface->data;
1331 #define restore_bool_device(device, cap) \
1332 if (has_cap(cap))\
1333 set_bool(data->device, cap);\
1335 restore_bool_device(wireless, ACER_CAP_WIRELESS);
1336 restore_bool_device(bluetooth, ACER_CAP_BLUETOOTH);
1337 restore_bool_device(threeg, ACER_CAP_THREEG);
1338 restore_bool_device(mailled, ACER_CAP_MAILLED);
1340 if (has_cap(ACER_CAP_BRIGHTNESS))
1341 set_brightness((u8)data->brightness);
1343 /* Check if this laptop requires the keyboard quirk */
1344 if (quirks->mmkeys != 0) {
1345 set_keyboard_quirk();
1346 printk(ACER_INFO "Setting keyboard quirk to enable multimedia keys\n");
1349 return 0;
1352 static struct platform_driver acer_platform_driver = {
1353 .driver = {
1354 .name = "acer_acpi",
1355 .owner = THIS_MODULE,
1357 .probe = acer_platform_probe,
1358 .remove = acer_platform_remove,
1359 .suspend = acer_platform_suspend,
1360 .resume = acer_platform_resume,
1363 static struct platform_device *acer_platform_device;
1365 static int remove_sysfs(struct platform_device *device)
1367 #define remove_device_file(value, cap) \
1368 if (has_cap(cap)) \
1369 device_remove_file(&device->dev, &dev_attr_##value);
1371 if (wmi_acer_has_guid(WMID_GUID2)) {
1372 device_remove_file(&device->dev, &dev_attr_devices);
1375 remove_device_file(wireless, ACER_CAP_WIRELESS);
1376 remove_device_file(bluetooth, ACER_CAP_BLUETOOTH);
1377 remove_device_file(threeg, ACER_CAP_THREEG);
1378 remove_device_file(interface, ACER_CAP_ANY);
1379 remove_device_file(fan_temperature_override, ACER_CAP_TEMPERATURE_OVERRIDE);
1380 remove_device_file(touchpad, ACER_CAP_TOUCHPAD_READ);
1381 return 0;
1384 static int create_sysfs(void)
1386 int retval = -ENOMEM;
1388 #define add_device_file(value, cap) \
1389 if (has_cap(cap)) {\
1390 retval = device_create_file(&acer_platform_device->dev, &dev_attr_##value);\
1391 if (retval)\
1392 goto error;\
1395 if (wmi_acer_has_guid(WMID_GUID2)) {
1396 retval = device_create_file(&acer_platform_device->dev, &dev_attr_devices);
1397 if (retval)
1398 goto error;
1401 add_device_file(wireless, ACER_CAP_WIRELESS);
1402 add_device_file(bluetooth, ACER_CAP_BLUETOOTH);
1403 add_device_file(threeg, ACER_CAP_THREEG);
1404 add_device_file(interface, ACER_CAP_ANY);
1405 add_device_file(fan_temperature_override, ACER_CAP_TEMPERATURE_OVERRIDE);
1406 add_device_file(touchpad, ACER_CAP_TOUCHPAD_READ);
1408 return 0;
1410 error:
1411 remove_sysfs(acer_platform_device);
1412 return retval;
1415 static int __init acer_acpi_init(void)
1417 printk(ACER_INFO "Acer Laptop ACPI Extras version %s\n",
1418 ACER_ACPI_VERSION);
1420 /* Find if this laptop requires any quirks */
1421 DEBUG(1, "Finding quirks\n");
1422 find_quirks();
1425 * Detect which WMI interface we're using.
1427 if (wmi_acer_has_guid(AMW0_GUID1)) {
1428 interface = &AMW0_interface;
1430 if (wmi_acer_has_guid(WMID_GUID1)) {
1431 DEBUG(0, "Detected Acer AMW0 version 2 interface\n");
1432 interface = &AMW0_V2_interface;
1433 } else {
1434 DEBUG(0, "Detected Acer AMW0 version 1 interface\n");
1435 if (!quirks->brightness) {
1436 quirks->brightness = 1;
1437 interface->capability |= ACER_CAP_BRIGHTNESS;
1440 } else if (wmi_acer_has_guid(WMID_GUID1)) {
1441 DEBUG(0, "Detected Acer WMID interface\n");
1442 interface = &WMID_interface;
1443 } else {
1444 printk(ACER_ERR "No or unsupported WMI interface, unable to load.\n");
1445 return -ENODEV;
1448 if (wmi_acer_has_guid(WMID_GUID2)) {
1449 if (ACPI_FAILURE(WMID_set_capabilities())) {
1450 printk(ACER_ERR "Unable to detect available devices\n");
1451 return -ENODEV;
1455 #ifdef CONFIG_PROC
1456 /* Create the proc entries */
1457 acer_proc_dir = proc_mkdir(PROC_ACER, acpi_root_dir);
1458 if (!acer_proc_dir) {
1459 printk(ACER_ERR "Unable to create /proc entries, aborting.\n");
1460 goto error_proc_mkdir;
1463 acer_proc_dir->owner = THIS_MODULE;
1464 if (add_proc_entries()) {
1465 printk(ACER_ERR "Unable to create /proc entries, aborting.\n");
1466 goto error_proc_add;
1468 #endif
1471 * Register the driver
1473 if (platform_driver_register(&acer_platform_driver)) {
1474 printk(ACER_ERR "Unable to register platform driver, aborting.\n");
1475 goto error_platform_register;
1477 acer_platform_device = platform_device_alloc("acer_acpi", -1);
1478 platform_device_add(acer_platform_device);
1480 create_sysfs();
1482 DEBUG(1, "Driver registered\n");
1484 /* Override any initial settings with values from the commandline */
1485 acer_commandline_init();
1487 return 0;
1489 error_platform_register:
1490 #ifdef CONFIG_PROC
1491 remove_proc_entries();
1492 error_proc_add:
1493 if (acer_proc_dir)
1494 remove_proc_entry(PROC_ACER, acpi_root_dir);
1495 error_proc_mkdir:
1496 #endif
1497 return -ENODEV;
1500 static void __exit acer_acpi_exit(void)
1502 remove_sysfs(acer_platform_device);
1503 platform_device_del(acer_platform_device);
1504 platform_driver_unregister(&acer_platform_driver);
1506 #ifdef CONFIG_PROC
1507 remove_proc_entries();
1509 if (acer_proc_dir)
1510 remove_proc_entry(PROC_ACER, acpi_root_dir);
1511 #endif
1512 printk(ACER_INFO "Acer Laptop ACPI Extras unloaded\n");
1513 return;
1516 module_init(acer_acpi_init);
1517 module_exit(acer_acpi_exit);