acer_acpi - Add detection for bluetooth and maximum brightness
[acer_acpi.git] / acer_acpi.c
blobada8d9bbf3d71c6ea91e96f771d60f9a528b87c7
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_MAILLED
598 * New interface (The WMID interface)
600 static acpi_status
601 WMI_execute_u32(u32 method_id, u32 in, u32 *out)
603 struct acpi_buffer input = { (acpi_size) sizeof(u32), (void *)(&in) };
604 struct acpi_buffer result = { ACPI_ALLOCATE_BUFFER, NULL };
605 union acpi_object *obj;
606 u32 tmp;
607 acpi_status status;
609 DEBUG(2, " WMI_execute_u32:\n");
610 status = wmi_acer_evaluate_method(WMID_GUID1, 1, method_id, &input, &result);
611 DEBUG(2, " In: 0x%08x\n", in);
613 if (ACPI_FAILURE(status))
614 return status;
616 obj = (union acpi_object *) result.pointer;
617 if (obj && obj->type == ACPI_TYPE_BUFFER && obj->buffer.length == sizeof(u32)) {
618 tmp = *((u32 *) obj->buffer.pointer);
619 DEBUG(2, " Out: 0x%08x\n", tmp);
620 } else {
621 tmp = 0;
622 if (obj) {
623 DEBUG(2, " Got unexpected result of type %d\n", obj->type);
624 } else {
625 DEBUG(2, " Got unexpected null result\n");
629 if (out)
630 *out = tmp;
632 if (result.length > 0 && result.pointer)
633 kfree(result.pointer);
635 DEBUG(2, " Returning from WMI_execute_u32:\n");
636 return status;
639 static acpi_status WMID_get_u8(u8 *value, u32 cap, struct Interface *iface) {
640 acpi_status status;
641 u32 result;
642 u32 method_id = 0;
644 DEBUG(2, " WMID_get_u8: cap=%d\n", cap);
645 switch (cap) {
646 case ACER_CAP_WIRELESS:
647 method_id = ACER_WMID_GET_WIRELESS_METHODID;
648 break;
649 case ACER_CAP_BLUETOOTH:
650 method_id = ACER_WMID_GET_BLUETOOTH_METHODID;
651 break;
652 case ACER_CAP_BRIGHTNESS:
653 method_id = ACER_WMID_GET_BRIGHTNESS_METHODID;
654 break;
655 case ACER_CAP_THREEG:
656 method_id = ACER_WMID_GET_THREEG_METHODID;
657 break;
658 case ACER_CAP_MAILLED:
659 if (quirks->mailled == 1) {
660 ec_read(0x9f, value);
661 *value &= 0x01;
662 return 0;
664 case ACER_CAP_TOUCHPAD_READ:
665 switch (quirks->touchpad) {
666 case 1:
667 ec_read(0x9e, value);
668 *value = 1 - ((*value >> 3) & 0x01);
669 return 0;
670 case 2:
671 ec_read(0x74, value);
672 *value = ((*value >> 3) & 0x01);
673 return 0;
674 default:
675 break;
677 case ACER_CAP_TEMPERATURE_OVERRIDE:
678 if (quirks->temperature_override == 1) {
679 ec_read(0xa9, value);
680 return 0;
682 default:
683 return AE_BAD_ADDRESS;
685 status = WMI_execute_u32(method_id, 0, &result);
686 DEBUG(2, " WMI_execute_u32 status=%d:\n", status);
688 if (ACPI_SUCCESS(status))
689 *value = (u8)result;
691 DEBUG(2, " Returning from WMID_get_u8:\n");
692 return status;
695 static acpi_status WMID_set_u8(u8 value, u32 cap, struct Interface *iface) {
696 u32 method_id = 0;
698 switch (cap) {
699 case ACER_CAP_BRIGHTNESS:
700 method_id = ACER_WMID_SET_BRIGHTNESS_METHODID;
701 break;
702 case ACER_CAP_WIRELESS:
703 method_id = ACER_WMID_SET_WIRELESS_METHODID;
704 break;
705 case ACER_CAP_BLUETOOTH:
706 method_id = ACER_WMID_SET_BLUETOOTH_METHODID;
707 break;
708 case ACER_CAP_THREEG:
709 method_id = ACER_WMID_SET_THREEG_METHODID;
710 break;
711 case ACER_CAP_MAILLED:
712 if (quirks->mailled == 1) {
713 send_kbd_cmd(0x59, value ? 0x92 : 0x93);
714 return 0;
716 case ACER_CAP_TEMPERATURE_OVERRIDE:
717 if (quirks->temperature_override == 1) {
718 ec_write(0xa9, value);
719 return 0;
721 default:
722 return AE_BAD_ADDRESS;
724 return WMI_execute_u32(method_id, (u32)value, NULL);
727 static acpi_status WMID_set_capabilities(void) {
728 struct acpi_buffer out = {ACPI_ALLOCATE_BUFFER, NULL};
729 union acpi_object *obj;
730 acpi_status status;
731 u32 devices;
733 status = wmi_acer_query_block(WMID_GUID2, 1, &out);
734 if (ACPI_FAILURE(status))
735 return -EFAULT;
737 obj = (union acpi_object *) out.pointer;
738 if (obj && obj->type == ACPI_TYPE_BUFFER && obj->buffer.length == sizeof(u32)) {
739 devices = *((u32 *) obj->buffer.pointer);
740 } else {
741 return -EIO;
744 /* Not sure on the meaning of the relevant bits yet */
745 interface->capability |= ACER_CAP_WIRELESS;
746 interface->capability |= ACER_CAP_THREEG;
748 if (devices & 0x10) {
749 DEBUG(1, "Bluetooth hardware available - enabling\n");
750 interface->capability |= ACER_CAP_BLUETOOTH;
753 if (!(devices & 0x20)) {
754 DEBUG(1, "Maximum brightness quirk detected - enabling\n");
755 max_brightness = 0x9;
758 return status;
762 static struct Interface WMID_interface = {
763 .type = ACER_WMID,
766 #ifdef CONFIG_PROC
768 * High-level Procfs file handlers
771 static int
772 dispatch_read(char *page, char **start, off_t off, int count, int *eof,
773 struct ProcItem *item)
775 char *p = page;
776 int len;
778 DEBUG(2, " dispatch_read: \n");
779 if (off == 0)
780 p = item->read_func(p, item->capability);
781 len = (p - page);
782 if (len <= off + count)
783 *eof = 1;
784 *start = page + off;
785 len -= off;
786 if (len > count)
787 len = count;
788 if (len < 0)
789 len = 0;
790 return len;
793 static int
794 dispatch_write(struct file *file, const char __user *buffer,
795 unsigned long count, struct ProcItem *item)
797 int result;
798 char *tmp_buffer;
801 * Arg buffer points to userspace memory, which can't be accessed
802 * directly. Since we're making a copy, zero-terminate the
803 * destination so that sscanf can be used on it safely.
805 tmp_buffer = kmalloc(count + 1, GFP_KERNEL);
806 if (copy_from_user(tmp_buffer, buffer, count)) {
807 result = -EFAULT;
808 } else {
809 tmp_buffer[count] = 0;
810 result = item->write_func(tmp_buffer, count, item->capability);
812 kfree(tmp_buffer);
813 return result;
815 #endif
818 * Generic Device (interface-independent)
821 static acpi_status get_bool(bool *value, u32 cap) {
822 acpi_status status = AE_BAD_ADDRESS;
823 u8 tmp = 0;
825 DEBUG(2, " get_bool: cap=%d, interface type=%d\n",
826 cap, interface->type);
827 switch (interface->type) {
828 case ACER_AMW0:
829 status = AMW0_get_bool(value, cap, interface);
830 break;
831 case ACER_AMW0_V2:
832 if (cap == ACER_CAP_MAILLED) {
833 status = AMW0_get_bool(value, cap, interface);
834 break;
836 case ACER_WMID:
837 status = WMID_get_u8(&tmp, cap, interface);
838 *value = (tmp == 1) ? 1 : 0;
839 break;
841 DEBUG(2, " Returning from get_bool:\n");
842 return status;
845 static acpi_status set_bool(int value, u32 cap) {
846 acpi_status status = AE_BAD_PARAMETER;
848 DEBUG(2, " set_bool: cap=%d, interface type=%d, value=%d\n",
849 cap, interface->type, value);
850 if ((value == 0 || value == 1) && (interface->capability & cap)) {
851 switch (interface->type) {
852 case ACER_AMW0:
853 status = AMW0_set_bool(value == 1, cap, interface);
854 break;
855 case ACER_AMW0_V2:
856 if (cap == ACER_CAP_MAILLED) {
857 status = AMW0_set_bool(value == 1, cap,
858 interface);
859 break;
861 case ACER_WMID:
862 status = WMID_set_u8(value == 1, cap, interface);
863 break;
866 return status;
869 static acpi_status get_u8(u8 *value, u32 cap) {
870 DEBUG(2, " get_u8: cap=%d\n", cap);
871 switch (interface->type) {
872 case ACER_AMW0:
873 return AMW0_get_u8(value, cap, interface);
874 break;
875 case ACER_AMW0_V2:
876 case ACER_WMID:
877 return WMID_get_u8(value, cap, interface);
878 break;
879 default:
880 return AE_BAD_ADDRESS;
884 static acpi_status set_u8(u8 value, u8 min, u8 max, u32 cap) {
886 DEBUG(2, " set_u8: cap=%d, interface type=%d, value=%d\n",
887 cap, interface->type, value);
889 if ((value >= min && value <= max) && (interface->capability & cap) ) {
890 switch (interface->type) {
891 case ACER_AMW0:
892 return AMW0_set_u8(value, cap, interface);
893 case ACER_AMW0_V2:
894 case ACER_WMID:
895 return WMID_set_u8(value, cap, interface);
896 default:
897 return AE_BAD_PARAMETER;
900 return AE_BAD_PARAMETER;
903 /* Each _u8 needs a small wrapper that sets the boundary values */
904 static acpi_status set_brightness(u8 value)
906 return set_u8(value, 0, max_brightness, ACER_CAP_BRIGHTNESS);
909 static acpi_status set_temperature_override(u8 value)
911 return set_u8(value, 0, ACER_MAX_TEMPERATURE_OVERRIDE, ACER_CAP_TEMPERATURE_OVERRIDE);
914 static void __init acer_commandline_init(void)
916 DEBUG(1, "Commandline args: mailled(%d) wireless(%d) bluetooth(%d) brightness(%d)\n",
917 mailled, wireless, bluetooth, brightness);
920 * These will all fail silently if the value given is invalid, or the
921 * capability isn't available on the given interface
923 set_bool(mailled, ACER_CAP_MAILLED);
924 set_bool(wireless, ACER_CAP_WIRELESS);
925 set_bool(bluetooth, ACER_CAP_BLUETOOTH);
926 set_bool(threeg, ACER_CAP_THREEG);
927 set_temperature_override(fan_temperature_override);
928 set_brightness((u8)brightness);
931 #ifdef CONFIG_PROC
933 * Procfs interface (deprecated)
935 static char *read_bool(char *p, u32 cap)
937 bool result;
938 acpi_status status;
940 DEBUG(2, " read_bool: cap=%d\n", cap);
941 status = get_bool(&result, cap);
942 if (ACPI_SUCCESS(status))
943 p += sprintf(p, "%d\n", result);
944 else
945 p += sprintf(p, "Read error" );
946 return p;
949 static unsigned long write_bool(const char *buffer, unsigned long count, u32 cap)
951 int value;
953 DEBUG(2, " write_bool: cap=%d, interface type=%d\n buffer=%s\n",
954 cap, interface->type, buffer);
956 if (sscanf(buffer, "%i", &value) == 1) {
957 acpi_status status = set_bool(value, cap);
958 if (ACPI_FAILURE(status))
959 return -EINVAL;
960 } else {
961 return -EINVAL;
963 return count;
966 static char *read_u8(char *p, u32 cap)
968 u8 result;
969 acpi_status status;
971 DEBUG(2, " read_u8: cap=%d\n", cap);
972 status = get_u8(&result, cap);
973 if (ACPI_SUCCESS(status))
974 p += sprintf(p, "%u\n", result);
975 else
976 p += sprintf(p, "Read error" );
977 return p;
980 static unsigned long write_u8(const char *buffer, unsigned long count, u32 cap)
982 int value;
983 acpi_status (*set_method)(u8);
985 /* Choose the appropriate set_u8 wrapper here, based on the capability */
986 switch (cap) {
987 case ACER_CAP_BRIGHTNESS:
988 set_method = set_brightness;
989 break;
990 case ACER_CAP_TEMPERATURE_OVERRIDE:
991 set_method = set_temperature_override;
992 break;
993 default:
994 return -EINVAL;
997 if (sscanf(buffer, "%i", &value) == 1) {
998 acpi_status status = (*set_method)(value);
999 if (ACPI_FAILURE(status))
1000 return -EINVAL;
1001 } else {
1002 return -EINVAL;
1004 return count;
1007 static char *read_version(char *p, u32 cap)
1009 p += sprintf(p, "%s\n", ACER_ACPI_VERSION);
1010 return p;
1013 static char *read_interface(char *p, u32 cap)
1015 p += sprintf(p, "%s\n", (interface->type == ACER_AMW0 ) ? "AMW0": "WMID");
1016 return p;
1019 static struct ProcItem proc_items[] = {
1020 {"mailled", read_bool, write_bool, ACER_CAP_MAILLED},
1021 {"bluetooth", read_bool, write_bool, ACER_CAP_BLUETOOTH},
1022 {"wireless", read_bool, write_bool, ACER_CAP_WIRELESS},
1023 {"brightness", read_u8, write_u8, ACER_CAP_BRIGHTNESS},
1024 {"threeg", read_bool, write_bool, ACER_CAP_THREEG},
1025 {"touchpad", read_bool, NULL, ACER_CAP_TOUCHPAD_READ},
1026 {"fan_temperature_override", read_u8, write_u8, ACER_CAP_TEMPERATURE_OVERRIDE},
1027 {"version", read_version, NULL, ACER_CAP_ANY},
1028 {"interface", read_interface, NULL, ACER_CAP_ANY},
1029 {NULL}
1032 static int __init add_proc_entries(void)
1034 struct proc_dir_entry *proc;
1035 struct ProcItem *item;
1037 for (item = proc_items; item->name; ++item) {
1039 * Only add the proc file if the current interface actually
1040 * supports it
1042 if (interface->capability & item->capability) {
1043 proc = create_proc_read_entry(item->name,
1044 S_IFREG | S_IRUGO | S_IWUSR,
1045 acer_proc_dir,
1046 (read_proc_t *) dispatch_read,
1047 item);
1048 if (proc)
1049 proc->owner = THIS_MODULE;
1050 if (proc && item->write_func)
1051 proc->write_proc = (write_proc_t *) dispatch_write;
1055 return 0;
1058 static int __exit remove_proc_entries(void)
1060 struct ProcItem *item;
1062 for (item = proc_items; item->name; ++item)
1063 remove_proc_entry(item->name, acer_proc_dir);
1064 return 0;
1066 #endif
1069 * LED device (Mail LED only, no other LEDs known yet)
1071 static void mail_led_set(struct led_classdev *led_cdev, enum led_brightness value)
1073 bool tmp = value;
1074 set_bool(tmp, ACER_CAP_MAILLED);
1077 static struct led_classdev mail_led = {
1078 .name = "acer_acpi:mail",
1079 .brightness_set = mail_led_set,
1082 static void acer_led_init(struct device *dev)
1084 led_classdev_register(dev, &mail_led);
1087 static void acer_led_exit(void)
1089 led_classdev_unregister(&mail_led);
1092 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18)
1094 * Backlight device
1096 static struct backlight_device *acer_backlight_device;
1098 static int read_brightness(struct backlight_device *bd)
1100 u8 value;
1101 get_u8(&value, ACER_CAP_BRIGHTNESS);
1102 return value;
1105 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,20)
1106 static int update_bl_status(struct backlight_device *bd)
1108 set_brightness(bd->props->brightness);
1109 return 0;
1112 static struct backlight_properties acer_backlight_properties = {
1113 .get_brightness = read_brightness,
1114 .update_status = update_bl_status,
1117 static int __init acer_backlight_init(struct device *dev)
1119 struct backlight_device *bd;
1121 DEBUG(1, "Loading backlight driver\n");
1122 bd = backlight_device_register("acer_acpi", dev, NULL, &acer_backlight_properties);
1123 if (IS_ERR(bd)) {
1124 printk(ACER_ERR "Could not register Acer backlight device\n");
1125 acer_backlight_device = NULL;
1126 return PTR_ERR(bd);
1129 acer_backlight_device = bd;
1131 bd->props->max_brightness = max_brightness;
1132 return 0;
1134 #else
1135 static int update_bl_status(struct backlight_device *bd)
1137 set_brightness(bd->props.brightness);
1138 return 0;
1141 static struct backlight_ops acer_backlight_ops = {
1142 .get_brightness = read_brightness,
1143 .update_status = update_bl_status,
1146 static int __init acer_backlight_init(struct device *dev)
1148 struct backlight_device *bd;
1150 DEBUG(1, "Loading backlight driver\n");
1151 bd = backlight_device_register("acer_acpi", dev, NULL, &acer_backlight_ops);
1152 if (IS_ERR(bd)) {
1153 printk(ACER_ERR "Could not register Acer backlight device\n");
1154 acer_backlight_device = NULL;
1155 return PTR_ERR(bd);
1158 acer_backlight_device = bd;
1160 bd->props.max_brightness = max_brightness;
1161 bd->props.brightness = read_brightness(NULL);
1162 backlight_update_status(bd);
1163 return 0;
1165 #endif
1167 static void __exit acer_backlight_exit(void)
1169 backlight_device_unregister(acer_backlight_device);
1171 #endif
1174 * Read/ write bool sysfs macro
1176 #define show_set_bool(value, cap) \
1177 static ssize_t \
1178 show_bool_##value(struct device *dev, struct device_attribute *attr, \
1179 char *buf) \
1181 bool result; \
1182 acpi_status status = get_bool(&result, cap); \
1183 if (ACPI_SUCCESS(status)) \
1184 return sprintf(buf, "%d\n", result); \
1185 return sprintf(buf, "Read error" ); \
1188 static ssize_t \
1189 set_bool_##value(struct device *dev, struct device_attribute *attr, \
1190 const char *buf, size_t count) \
1192 bool tmp = simple_strtoul(buf, NULL, 10); \
1193 acpi_status status = set_bool(tmp, cap); \
1194 if (ACPI_FAILURE(status)) \
1195 return -EINVAL; \
1196 return count; \
1198 static DEVICE_ATTR(value, S_IWUGO | S_IRUGO | S_IWUSR, \
1199 show_bool_##value, set_bool_##value);
1201 show_set_bool(wireless, ACER_CAP_WIRELESS);
1202 show_set_bool(bluetooth, ACER_CAP_BLUETOOTH);
1203 show_set_bool(threeg, ACER_CAP_THREEG);
1204 show_set_bool(fan_temperature_override, ACER_CAP_TEMPERATURE_OVERRIDE);
1207 * Read-only bool sysfs macro
1209 #define show_bool(value, cap) \
1210 static ssize_t \
1211 show_bool_##value(struct device *dev, struct device_attribute *attr, \
1212 char *buf) \
1214 bool result; \
1215 acpi_status status = get_bool(&result, cap); \
1216 if (ACPI_SUCCESS(status)) \
1217 return sprintf(buf, "%d\n", result); \
1218 return sprintf(buf, "Read error" ); \
1220 static DEVICE_ATTR(value, S_IWUGO | S_IRUGO | S_IWUSR, \
1221 show_bool_##value, NULL);
1223 show_bool(touchpad, ACER_CAP_TOUCHPAD_READ);
1226 * Read interface sysfs macro
1228 static ssize_t show_interface(struct device *dev, struct device_attribute *attr,
1229 char *buf)
1231 switch (interface->type) {
1232 case ACER_AMW0:
1233 return sprintf(buf, "AMW0\n");
1234 case ACER_AMW0_V2:
1235 return sprintf(buf, "AMW0 v2\n");
1236 case ACER_WMID:
1237 return sprintf(buf, "WMID\n");
1238 default:
1239 return sprintf(buf, "Error!\n");
1243 static DEVICE_ATTR(interface, S_IWUGO | S_IRUGO | S_IWUSR, show_interface, NULL);
1245 static ssize_t show_devices(struct device *dev, struct device_attribute *attr,
1246 char *buf)
1248 struct acpi_buffer out = {ACPI_ALLOCATE_BUFFER, NULL};
1249 union acpi_object *obj;
1250 acpi_status status;
1251 u32 tmp;
1253 status = wmi_acer_query_block(WMID_GUID2, 1, &out);
1254 if (ACPI_FAILURE(status))
1255 return sprintf(buf, "Error\n");
1257 obj = (union acpi_object *) out.pointer;
1258 if (obj && obj->type == ACPI_TYPE_BUFFER && obj->buffer.length == sizeof(u32)) {
1259 tmp = *((u32 *) obj->buffer.pointer);
1260 } else {
1261 return sprintf(buf, "Error\n");
1264 return sprintf(buf, "%u\n", tmp);
1267 static DEVICE_ATTR(devices, S_IWUGO | S_IRUGO | S_IWUSR, show_devices, NULL);
1270 * Platform device
1272 static int __devinit acer_platform_probe(struct platform_device *device)
1274 if (has_cap(ACER_CAP_MAILLED))
1275 acer_led_init(&device->dev);
1276 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18)
1277 if (has_cap(ACER_CAP_BRIGHTNESS))
1278 acer_backlight_init(&device->dev);
1279 #endif
1280 return 0;
1283 static int acer_platform_remove(struct platform_device *device)
1285 if (has_cap(ACER_CAP_MAILLED))
1286 acer_led_exit();
1287 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18)
1288 if (has_cap(ACER_CAP_BRIGHTNESS))
1289 acer_backlight_exit();
1290 #endif
1291 return 0;
1294 static int acer_platform_suspend(struct platform_device *device, pm_message_t state)
1297 * WMID fix for suspend-to-disk - save all current states now so we can
1298 * restore them on resume
1300 bool value;
1301 u8 u8value;
1302 struct acer_data *data = &interface->data;
1304 #define save_bool_device(device, cap) \
1305 if (has_cap(cap)) {\
1306 get_bool(&value, cap);\
1307 data->device = value;\
1310 #define save_u8_device(device, cap) \
1311 if (has_cap(cap)) {\
1312 get_u8(&u8value, cap);\
1313 data->device = u8value;\
1316 save_bool_device(wireless, ACER_CAP_WIRELESS);
1317 save_bool_device(bluetooth, ACER_CAP_BLUETOOTH);
1318 save_bool_device(threeg, ACER_CAP_THREEG);
1319 save_bool_device(mailled, ACER_CAP_MAILLED);
1320 save_u8_device(brightness, ACER_CAP_BRIGHTNESS);
1322 return 0;
1325 static int acer_platform_resume(struct platform_device *device)
1327 struct acer_data *data = &interface->data;
1329 #define restore_bool_device(device, cap) \
1330 if (has_cap(cap))\
1331 set_bool(data->device, cap);\
1333 restore_bool_device(wireless, ACER_CAP_WIRELESS);
1334 restore_bool_device(bluetooth, ACER_CAP_BLUETOOTH);
1335 restore_bool_device(threeg, ACER_CAP_THREEG);
1336 restore_bool_device(mailled, ACER_CAP_MAILLED);
1338 if (has_cap(ACER_CAP_BRIGHTNESS))
1339 set_brightness((u8)data->brightness);
1341 /* Check if this laptop requires the keyboard quirk */
1342 if (quirks->mmkeys != 0) {
1343 set_keyboard_quirk();
1344 printk(ACER_INFO "Setting keyboard quirk to enable multimedia keys\n");
1347 return 0;
1350 static struct platform_driver acer_platform_driver = {
1351 .driver = {
1352 .name = "acer_acpi",
1353 .owner = THIS_MODULE,
1355 .probe = acer_platform_probe,
1356 .remove = acer_platform_remove,
1357 .suspend = acer_platform_suspend,
1358 .resume = acer_platform_resume,
1361 static struct platform_device *acer_platform_device;
1363 static int remove_sysfs(struct platform_device *device)
1365 #define remove_device_file(value, cap) \
1366 if (has_cap(cap)) \
1367 device_remove_file(&device->dev, &dev_attr_##value);
1369 if (wmi_acer_has_guid(WMID_GUID2)) {
1370 device_remove_file(&device->dev, &dev_attr_devices);
1373 remove_device_file(wireless, ACER_CAP_WIRELESS);
1374 remove_device_file(bluetooth, ACER_CAP_BLUETOOTH);
1375 remove_device_file(threeg, ACER_CAP_THREEG);
1376 remove_device_file(interface, ACER_CAP_ANY);
1377 remove_device_file(fan_temperature_override, ACER_CAP_TEMPERATURE_OVERRIDE);
1378 remove_device_file(touchpad, ACER_CAP_TOUCHPAD_READ);
1379 return 0;
1382 static int create_sysfs(void)
1384 int retval = -ENOMEM;
1386 #define add_device_file(value, cap) \
1387 if (has_cap(cap)) {\
1388 retval = device_create_file(&acer_platform_device->dev, &dev_attr_##value);\
1389 if (retval)\
1390 goto error;\
1393 if (wmi_acer_has_guid(WMID_GUID2)) {
1394 retval = device_create_file(&acer_platform_device->dev, &dev_attr_devices);
1395 if (retval)
1396 goto error;
1399 add_device_file(wireless, ACER_CAP_WIRELESS);
1400 add_device_file(bluetooth, ACER_CAP_BLUETOOTH);
1401 add_device_file(threeg, ACER_CAP_THREEG);
1402 add_device_file(interface, ACER_CAP_ANY);
1403 add_device_file(fan_temperature_override, ACER_CAP_TEMPERATURE_OVERRIDE);
1404 add_device_file(touchpad, ACER_CAP_TOUCHPAD_READ);
1406 return 0;
1408 error:
1409 remove_sysfs(acer_platform_device);
1410 return retval;
1413 static int __init acer_acpi_init(void)
1415 printk(ACER_INFO "Acer Laptop ACPI Extras version %s\n",
1416 ACER_ACPI_VERSION);
1418 /* Find if this laptop requires any quirks */
1419 DEBUG(1, "Finding quirks\n");
1420 find_quirks();
1423 * Detect which WMI interface we're using.
1425 if (wmi_acer_has_guid(AMW0_GUID1)) {
1426 interface = &AMW0_interface;
1428 if (wmi_acer_has_guid(WMID_GUID1)) {
1429 DEBUG(0, "Detected Acer AMW0 version 2 interface\n");
1430 interface = &AMW0_V2_interface;
1431 } else {
1432 DEBUG(0, "Detected Acer AMW0 version 1 interface\n");
1433 if (!quirks->brightness) {
1434 quirks->brightness = 1;
1435 interface->capability |= ACER_CAP_BRIGHTNESS;
1438 } else if (wmi_acer_has_guid(WMID_GUID1)) {
1439 DEBUG(0, "Detected Acer WMID interface\n");
1440 interface = &WMID_interface;
1441 } else {
1442 printk(ACER_ERR "No or unsupported WMI interface, unable to load.\n");
1443 return -ENODEV;
1446 if (wmi_acer_has_guid(WMID_GUID2)) {
1447 if (ACPI_FAILURE(WMID_set_capabilities())) {
1448 printk(ACER_ERR "Unable to detect available devices\n");
1449 return -ENODEV;
1453 #ifdef CONFIG_PROC
1454 /* Create the proc entries */
1455 acer_proc_dir = proc_mkdir(PROC_ACER, acpi_root_dir);
1456 if (!acer_proc_dir) {
1457 printk(ACER_ERR "Unable to create /proc entries, aborting.\n");
1458 goto error_proc_mkdir;
1461 acer_proc_dir->owner = THIS_MODULE;
1462 if (add_proc_entries()) {
1463 printk(ACER_ERR "Unable to create /proc entries, aborting.\n");
1464 goto error_proc_add;
1466 #endif
1469 * Register the driver
1471 if (platform_driver_register(&acer_platform_driver)) {
1472 printk(ACER_ERR "Unable to register platform driver, aborting.\n");
1473 goto error_platform_register;
1475 acer_platform_device = platform_device_alloc("acer_acpi", -1);
1476 platform_device_add(acer_platform_device);
1478 create_sysfs();
1480 DEBUG(1, "Driver registered\n");
1482 /* Override any initial settings with values from the commandline */
1483 acer_commandline_init();
1485 return 0;
1487 error_platform_register:
1488 #ifdef CONFIG_PROC
1489 remove_proc_entries();
1490 error_proc_add:
1491 if (acer_proc_dir)
1492 remove_proc_entry(PROC_ACER, acpi_root_dir);
1493 error_proc_mkdir:
1494 #endif
1495 return -ENODEV;
1498 static void __exit acer_acpi_exit(void)
1500 remove_sysfs(acer_platform_device);
1501 platform_device_del(acer_platform_device);
1502 platform_driver_unregister(&acer_platform_driver);
1504 #ifdef CONFIG_PROC
1505 remove_proc_entries();
1507 if (acer_proc_dir)
1508 remove_proc_entry(PROC_ACER, acpi_root_dir);
1509 #endif
1510 printk(ACER_INFO "Acer Laptop ACPI Extras unloaded\n");
1511 return;
1514 module_init(acer_acpi_init);
1515 module_exit(acer_acpi_exit);