Patch from Jim - refactor ACPI calls in AMW0 and WMID execute into one function
[acer_acpi.git] / acer_acpi.c
blobfb4c7f6da3367ff7d9e00c6cc9d27a38674dbb23
1 /*
2 * acer_acpi.c - Acer Laptop ACPI Extras
5 * Copyright (C) 2005 E.M. Smith
6 * Copyright (C) 2007 Carlos Corbacho <cathectic@gmail.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * The devolpment page for this driver is located at
24 * http://code.google.com/p/aceracpi
26 * Credits:
28 * John Belmonte - the Toshiba ACPI driver I've adapted for this module.
29 * Julien Lerouge & Karol Kozimor - ASUS Acpi driver authors.
30 * Olaf Tauber - developer of acerhk, the inspiration to solve the 64-bit
31 * driver problem for my Aspire 5024.
32 * Mathieu Segaud - solved the ACPI problem that needed a double-modprobe
33 * in version 0.2 and below.
34 * Carlos Corbacho - added initial status support for wireless/ mail/
35 * bluetooth, added module parameter support to turn
36 * hardware/ LEDs on and off at module loading (thanks
37 * again to acerhk for the inspiration)
38 * Jim Ramsay - Figured out and added support for WMID interface
42 #define ACER_ACPI_VERSION "0.6"
43 #define PROC_INTERFACE_VERSION 1
44 #define PROC_ACER "acer"
46 #include <linux/kernel.h>
47 #include <linux/module.h>
48 #include <linux/init.h>
49 #include <linux/types.h>
50 #include <linux/proc_fs.h>
51 #include <linux/delay.h>
52 #include <linux/suspend.h>
53 #include <asm/uaccess.h>
55 #include <acpi/acpi_drivers.h>
57 MODULE_AUTHOR("Mark Smith");
58 MODULE_DESCRIPTION("Acer Laptop ACPI Extras Driver");
59 MODULE_LICENSE("GPL");
61 #define MY_LOGPREFIX "acer_acpi: "
62 #define MY_ERR KERN_ERR MY_LOGPREFIX
63 #define MY_NOTICE KERN_NOTICE MY_LOGPREFIX
64 #define MY_INFO KERN_INFO MY_LOGPREFIX
66 #define DEBUG(level, message...) { \
67 if (debug >= level) \
68 printk(KERN_DEBUG MY_LOGPREFIX message);\
72 * On the 5580, brightness values range from 0x0 to 0xf, inclusive.
73 * This may vary on other machines.
75 #define ACER_MAX_BRIGHTNESS 0xf
78 * Magic Number -
79 * Meaning is unknown - this number is required for writing to ACPI
80 * (it's also used in acerhk when directly accessing the EC)
82 #define ACER_WRITE 0x9610
85 * Bit masks for the old AMW0 interface
86 * These could vary between the particular interface
88 #define ACER_AMW0_WIRELESS_MASK 0x35
89 #define ACER_AMW0_BLUETOOTH_MASK 0x34
90 #define ACER_AMW0_MAILLED_MASK 0x31
93 * Method IDs for new WMID interface
94 * These could be different for other untested machines
96 #define ACER_WMID_GET_WIRELESS_METHODID 1
97 #define ACER_WMID_GET_BLUETOOTH_METHODID 2
98 #define ACER_WMID_GET_BRIGHTNESS_METHODID 3
99 #define ACER_WMID_SET_WIRELESS_METHODID 4
100 #define ACER_WMID_SET_BLUETOOTH_METHODID 5
101 #define ACER_WMID_SET_BRIGHTNESS_METHODID 6
102 #define ACER_WMID_GET_THREEG_METHODID 10
103 #define ACER_WMID_SET_THREEG_METHODID 11
106 * Acer ACPI method paths
108 * TODO: It may be possbile to autodetect these, since these are all at HID PNP0C14
110 #define AMW0_METHOD "\\_SB_.AMW0.WMAB"
111 #define AMW0_GETDATA "\\_SB_.AMW0._WED"
113 #define WMID_METHOD "\\_SB.WMID.WMBA"
114 #define WMID_GETDATA "\\_SB.WMID._WED"
117 * Interface capability flags
119 #define ACER_CAP_MAILLED (1<<0)
120 #define ACER_CAP_WIRELESS (1<<1)
121 #define ACER_CAP_BLUETOOTH (1<<2)
122 #define ACER_CAP_BRIGHTNESS (1<<3)
123 #define ACER_CAP_THREEG (1<<4)
124 #define ACER_CAP_ANY (0xffffffff)
127 * Presumed start states -
128 * For the old-style interfaces, there is no way to know for certain what the start state
129 * is for any of the parameters (ACPI does not provide any methods or store this
130 * anywhere). We therefore start with an unknown state (this is also how acerhk
131 * does it); we then change the status so that we are in a known state (I
132 * suspect LaunchManager on Windows does something similar, since the wireless
133 * appears to turn off as soon as it launches).
135 * Plus, we can't tell which features are enabled or disabled on a specific
136 * model, just ranges - e.g. The 5020 series can _support_ bluetooth; but the
137 * 5021 has no bluetooth, whilst the 5024 does. However, the BIOS identifies
138 * both laptops as 5020 - we can't tell them apart!
140 * Basically the code works like this:
141 * - On init, any values specified on the commandline are set.
142 * - For interfaces where the current values cannot be detected and which
143 * have not been set on the commandline, we set them to some sane default
144 * (disabled)
146 * See AMW0_init and acer_commandline_init
149 #define ACER_DEFAULT_WIRELESS 0
150 #define ACER_DEFAULT_BLUETOOTH 0
151 #define ACER_DEFAULT_MAILLED 0
152 #define ACER_DEFAULT_THREEG 0
153 #define ACER_DEFAULT_BRIGHTNESS ACER_MAX_BRIGHTNESS
155 static int wireless = -1;
156 static int bluetooth = -1;
157 static int mailled = -1;
158 static int brightness = -1;
159 static int threeg = -1;
160 static int debug = 0;
162 module_param(mailled, int, 0444);
163 module_param(wireless, int, 0444);
164 module_param(bluetooth, int, 0444);
165 module_param(brightness, int, 0444);
166 module_param(threeg, int, 0444);
167 module_param(debug, int, 0664);
168 MODULE_PARM_DESC(wireless, "Set initial state of Wireless hardware");
169 MODULE_PARM_DESC(bluetooth, "Set initial state of Bluetooth hardware");
170 MODULE_PARM_DESC(mailled, "Set initial state of Mail LED");
171 MODULE_PARM_DESC(brightness, "Set initial LCD backlight brightness");
172 MODULE_PARM_DESC(threeg, "Set initial state of 3G hardware");
173 MODULE_PARM_DESC(debug, "Debugging verbosity level (0=least 2=most)");
175 typedef struct _ProcItem {
176 const char *name;
177 char *(*read_func) (char *, uint32_t);
178 unsigned long (*write_func) (const char *, unsigned long, uint32_t);
179 unsigned int capability;
180 } ProcItem;
182 struct acer_hotk {
183 struct acpi_device *device;
184 acpi_handle handle;
187 static struct proc_dir_entry *acer_proc_dir;
189 static int is_valid_acpi_path(const char *methodName)
191 acpi_handle handle;
192 acpi_status status;
194 status = acpi_get_handle(NULL, (char *)methodName, &handle);
195 return ACPI_SUCCESS(status);
198 /* Each low-level interface must define at least some of the following */
199 typedef struct _Interface {
201 * The capabilities this interface provides
202 * In the future, these can be removed/added at runtime when we have a
203 * way of detecting what capabilities are /actually/ present on an
204 * interface
206 uint32_t capability;
209 * Initializes an interface, should allocate the interface-specific
210 * data
212 acpi_status (*init) (struct _Interface*);
215 * Frees an interface, should free the interface-specific data
217 void (*free) (struct _Interface*);
220 * Gets and sets various data types.
221 * First paramater: Value to set, or pointer to place got value into
222 * Second parameter: Specific capability being requested
223 * Third paramater: Pointer to this interface
225 acpi_status (*get_bool) (bool*, uint32_t, struct _Interface*);
226 acpi_status (*set_bool) (bool, uint32_t, struct _Interface*);
227 acpi_status (*get_u8) (uint8_t*, uint32_t, struct _Interface*);
228 acpi_status (*set_u8) (uint8_t, uint32_t, struct _Interface*);
231 * Interface-specific private data member. Must *not* be touched by
232 * anyone outside of this struct
234 void *data;
235 } Interface;
237 /* The static interface pointer, points to the currently detected interface */
238 static Interface *interface;
241 * General interface convenience methods
244 /* These *_via_u8 use the interface's *_u8 methods to emulate other gets/sets */
245 static acpi_status get_bool_via_u8(bool *value, uint32_t cap, Interface *iface) {
246 acpi_status status;
247 uint8_t result;
249 status = iface->get_u8(&result, cap, iface);
251 if (ACPI_SUCCESS(status))
252 *value = (result != 0);
254 return status;
257 static acpi_status set_bool_via_u8(bool value, uint32_t cap, Interface *iface) {
258 uint8_t v = value ? 1 : 0;
260 return iface->set_u8(v, cap, iface);
263 /* General wrapper around the ACPI call */
264 static acpi_status
265 WMI_execute(char *methodPath, uint32_t methodId, const struct acpi_buffer *in, struct acpi_buffer *out) {
266 struct acpi_object_list input;
267 union acpi_object params[3];
268 acpi_status status = AE_OK;
270 /* WMI calling convention:
271 * methodPath( instance, methodId, input_buffer )
272 * - instance is always 1, since there's only this module
273 * - methodId is the method number within the current method group.
274 * - Input buffer is ignored for read-only commands
275 * - May return a buffer of results (optional)
277 input.count = 3;
278 input.pointer = params;
279 params[0].type = ACPI_TYPE_INTEGER;
280 params[0].integer.value = 0x01;
281 params[1].type = ACPI_TYPE_INTEGER;
282 params[1].integer.value = methodId;
283 params[2].type = ACPI_TYPE_BUFFER;
284 params[2].buffer.length = in->length;
285 params[2].buffer.pointer = in->pointer;
287 DEBUG(2, "Doing %s( 1, %u, [%llu-byte buffer] )\n", methodPath, methodId, in->length);
289 status = acpi_evaluate_object(NULL, methodPath, &input, out);
291 DEBUG(2, " Execution status: %d\n", status);
292 DEBUG(2, " Result: %llu bytes\n", out ? out->length : 0 );
294 return status;
299 * Old interface (now known as the AMW0 interface)
301 typedef struct _WMAB_args {
302 u32 eax;
303 u32 ebx;
304 u32 ecx;
305 u32 edx;
306 } WMAB_args;
308 typedef struct _AMW0_Data {
309 int mailled;
310 int wireless;
311 int bluetooth;
312 } AMW0_Data;
314 static acpi_status WMAB_execute(WMAB_args * regbuf, struct acpi_buffer *result)
316 struct acpi_buffer input;
317 acpi_status status;
318 input.length = sizeof(WMAB_args);
319 input.pointer = (u8*)regbuf;
321 status = WMI_execute( AMW0_METHOD, 1, &input, result);
322 DEBUG(2, " Args: 0x%08x 0x%08x 0x%08x 0x%08x\n", regbuf->eax, regbuf->ebx, regbuf->ecx, regbuf->edx );
324 return status;
327 static acpi_status AMW0_init(Interface *iface) {
328 WMAB_args args;
329 acpi_status status;
330 AMW0_Data *data;
332 /* Allocate our private data structure */
333 iface->data = kmalloc(sizeof(AMW0_Data), GFP_KERNEL);
334 data = (AMW0_Data*)iface->data;
337 * Call the interface once so the BIOS knows it's to notify us of
338 * events via PNP0C14
340 memset(&args, 0, sizeof(WMAB_args));
341 args.eax = ACER_WRITE;
342 args.ebx = 0;
343 status = WMAB_execute(&args, NULL);
346 * If the commandline doesn't specify these, we need to force them to
347 * the default values
349 if (mailled == -1)
350 mailled = ACER_DEFAULT_MAILLED;
351 if (wireless == -1)
352 wireless = ACER_DEFAULT_WIRELESS;
353 if (bluetooth == -1)
354 bluetooth = ACER_DEFAULT_BLUETOOTH;
357 * Set the cached "current" values to impossible ones so that
358 * acer_commandline_init will definitely set them.
360 data->wireless = data->mailled = data->bluetooth = -1;
362 return status;
365 static void AMW0_free(Interface *iface) {
366 /* Free our private data structure */
367 kfree(iface->data);
370 static acpi_status AMW0_get_bool(bool *value, uint32_t cap, Interface *iface)
372 AMW0_Data *data = iface->data;
374 /* Currently no way to query the state, so just return the cached value */
375 switch (cap) {
376 case ACER_CAP_MAILLED:
377 *value = data->mailled;
378 break;
379 case ACER_CAP_WIRELESS:
380 *value = data->wireless;
381 break;
382 case ACER_CAP_BLUETOOTH:
383 *value = data->bluetooth;
384 break;
385 default:
386 return AE_BAD_ADDRESS;
388 return AE_OK;
391 static acpi_status AMW0_set_bool(bool value, uint32_t cap, Interface *iface)
393 WMAB_args args;
394 acpi_status status;
396 args.eax = ACER_WRITE;
397 args.ebx = value ? (1<<8) : 0;
399 switch (cap) {
400 case ACER_CAP_MAILLED:
401 args.ebx |= ACER_AMW0_MAILLED_MASK;
402 break;
403 case ACER_CAP_WIRELESS:
404 args.ebx |= ACER_AMW0_WIRELESS_MASK;
405 break;
406 case ACER_CAP_BLUETOOTH:
407 args.ebx |= ACER_AMW0_BLUETOOTH_MASK;
408 break;
409 default:
410 return AE_BAD_ADDRESS;
413 /* Actually do the set */
414 status = WMAB_execute(&args, NULL);
417 * Currently no way to query the state, so cache the new value on
418 * success
420 if (ACPI_SUCCESS(status)) {
421 AMW0_Data *data = iface->data;
422 switch (cap) {
423 case ACER_CAP_MAILLED:
424 data->mailled = value;
425 break;
426 case ACER_CAP_WIRELESS:
427 data->wireless = value;
428 break;
429 case ACER_CAP_BLUETOOTH:
430 data->bluetooth = value;
431 break;
435 return status;
438 static Interface AMW0_interface = {
439 .capability = (
440 ACER_CAP_MAILLED |
441 ACER_CAP_WIRELESS |
442 ACER_CAP_BLUETOOTH
444 .init = AMW0_init,
445 .free = AMW0_free,
446 .get_bool = AMW0_get_bool,
447 .set_bool = AMW0_set_bool,
451 * New interface (The WMID interface)
454 static acpi_status
455 WMI_execute_uint32(uint32_t methodId, uint32_t in, uint32_t *out)
457 struct acpi_buffer input = { (acpi_size)sizeof(uint32_t), (void*)(&in) };
458 struct acpi_buffer result = { ACPI_ALLOCATE_BUFFER, NULL };
459 union acpi_object *obj;
460 uint32_t tmp;
461 acpi_status status;
463 status = WMI_execute(WMID_METHOD, methodId, &input, &result);
464 DEBUG(2, " In: 0x%08x\n", in);
466 if (ACPI_FAILURE(status))
467 return status;
469 obj = (union acpi_object *)result.pointer;
470 if (obj && obj->type == ACPI_TYPE_BUFFER && obj->buffer.length == sizeof(uint32_t)) {
471 tmp = *((uint32_t*)obj->buffer.pointer);
472 DEBUG(2, " Out: 0x%08x\n", tmp);
473 } else {
474 tmp = 0;
475 if (obj) {
476 DEBUG(2, " Got unexpected result of type %d\n", obj->type);
477 } else {
478 DEBUG(2, " Got unexpected null result\n");
482 if (out)
483 *out = tmp;
485 if (result.length > 0 && result.pointer)
486 kfree(result.pointer);
488 return status;
491 static acpi_status WMID_get_u8(uint8_t *value, uint32_t cap, Interface *iface) {
492 acpi_status status;
493 uint32_t result;
494 uint32_t methodId = 0;
496 switch (cap) {
497 case ACER_CAP_WIRELESS:
498 methodId = ACER_WMID_GET_WIRELESS_METHODID;
499 break;
500 case ACER_CAP_BLUETOOTH:
501 methodId = ACER_WMID_GET_BLUETOOTH_METHODID;
502 break;
503 case ACER_CAP_BRIGHTNESS:
504 methodId = ACER_WMID_GET_BRIGHTNESS_METHODID;
505 break;
506 case ACER_CAP_THREEG:
507 methodId = ACER_WMID_GET_THREEG_METHODID;
508 break;
509 default:
510 return AE_BAD_ADDRESS;
512 status = WMI_execute_uint32(methodId, 0, &result);
514 if (ACPI_SUCCESS(status))
515 *value = (uint8_t)result;
517 return status;
520 static acpi_status WMID_set_u8(uint8_t value, uint32_t cap, Interface *iface) {
521 uint32_t methodId = 0;
523 switch (cap) {
524 case ACER_CAP_BRIGHTNESS:
525 methodId = ACER_WMID_SET_BRIGHTNESS_METHODID;
526 break;
527 case ACER_CAP_WIRELESS:
528 methodId = ACER_WMID_SET_WIRELESS_METHODID;
529 break;
530 case ACER_CAP_BLUETOOTH:
531 methodId = ACER_WMID_SET_BLUETOOTH_METHODID;
532 case ACER_CAP_THREEG:
533 methodId = ACER_WMID_SET_THREEG_METHODID;
534 break;
535 default:
536 return AE_BAD_ADDRESS;
538 return WMI_execute_uint32(methodId, (uint32_t)value, NULL);
542 static Interface WMID_interface = {
543 .capability = (
544 ACER_CAP_WIRELESS |
545 ACER_CAP_BLUETOOTH |
546 ACER_CAP_BRIGHTNESS |
547 ACER_CAP_THREEG
549 .get_bool = get_bool_via_u8,
550 .set_bool = set_bool_via_u8,
551 .get_u8 = WMID_get_u8,
552 .set_u8 = WMID_set_u8,
553 .data = NULL,
557 * High-level Procfs file handlers
560 static int
561 dispatch_read(char *page, char **start, off_t off, int count, int *eof,
562 ProcItem * item)
564 char *p = page;
565 int len;
567 if (off == 0)
568 p = item->read_func(p, item->capability);
571 * ISSUE: I don't understand this code
573 len = (p - page);
574 if (len <= off + count)
575 *eof = 1;
576 *start = page + off;
577 len -= off;
578 if (len > count)
579 len = count;
580 if (len < 0)
581 len = 0;
582 return len;
585 static int
586 dispatch_write(struct file *file, const char __user * buffer,
587 unsigned long count, ProcItem * item)
589 int result;
590 char *tmp_buffer;
593 * Arg buffer points to userspace memory, which can't be accessed
594 * directly. Since we're making a copy, zero-terminate the
595 * destination so that sscanf can be used on it safely.
597 tmp_buffer = kmalloc(count + 1, GFP_KERNEL);
598 if (copy_from_user(tmp_buffer, buffer, count)) {
599 result = -EFAULT;
600 } else {
601 tmp_buffer[count] = 0;
602 result = item->write_func(tmp_buffer, count, item->capability);
604 kfree(tmp_buffer);
605 return result;
609 * Generic Device (interface-independent)
612 static acpi_status get_bool(bool *value, uint32_t cap) {
613 acpi_status status = AE_BAD_ADDRESS;
614 if (interface->get_bool)
615 status = interface->get_bool(value, cap, interface);
616 return status;
619 static acpi_status set_bool(int value, uint32_t cap) {
620 acpi_status status = AE_BAD_PARAMETER;
621 if ((value == 0 || value == 1) &&
622 (interface->capability & cap)) {
623 if (interface->get_bool) {
624 /* If possible, only set if the value has changed */
625 bool actual;
626 status = interface->get_bool(&actual, cap, interface);
627 if (ACPI_SUCCESS(status) && actual == (bool)value)
628 return status;
630 if (interface->set_bool)
631 status = interface->set_bool(value == 1, cap, interface);
633 return status;
637 static acpi_status get_u8(uint8_t *value, uint32_t cap) {
638 acpi_status status = AE_BAD_ADDRESS;
639 if (interface->get_u8)
640 status = interface->get_u8(value, cap, interface);
641 return status;
644 static acpi_status set_u8(uint8_t value, uint8_t min, uint8_t max, uint32_t cap) {
645 acpi_status status = AE_BAD_PARAMETER;
646 if ((value >= min && value <= max) &&
647 (interface->capability & cap) ) {
648 if (interface->get_u8) {
649 /* If possible, only set if the value has changed */
650 uint8_t actual;
651 status = interface->get_u8(&actual, cap, interface);
652 if (ACPI_SUCCESS(status) && actual == value)
653 return status;
655 if (interface->set_u8)
656 status = interface->set_u8(value, cap, interface);
658 return status;
661 /* Each _u8 needs a small wrapper that sets the boundary values */
662 static acpi_status set_brightness(uint8_t value)
664 return set_u8(value, 0, ACER_MAX_BRIGHTNESS, ACER_CAP_BRIGHTNESS);
667 static void acpi_commandline_init(void)
669 DEBUG(1, "Commandline args: mailled(%d) wireless(%d) bluetooth(%d) brightness(%d)\n",
670 mailled, wireless, bluetooth, brightness);
673 * These will all fail silently if the value given is invalid, or the
674 * capability isn't available on the given interface
676 set_bool(mailled, ACER_CAP_MAILLED);
677 set_bool(wireless, ACER_CAP_WIRELESS);
678 set_bool(bluetooth, ACER_CAP_BLUETOOTH);
679 set_bool(threeg, ACER_CAP_THREEG);
680 set_brightness((uint8_t)brightness);
684 * Procfs interface
686 static char *read_bool(char *p, uint32_t cap)
688 bool result;
689 acpi_status status = get_bool(&result, cap);
690 if (ACPI_SUCCESS(status))
691 p += sprintf(p, "%d\n", result);
692 else
693 p += sprintf(p, "Read error" );
694 return p;
697 static unsigned long write_bool(const char *buffer, unsigned long count, uint32_t cap)
699 int value;
702 * For now, we are still supporting the "enabled: %i" - this _will_ be deprecated in 0.7
704 if ((sscanf(buffer, " enabled : %i", &value) == 1
705 || sscanf(buffer, "%i", &value) == 1)) {
706 acpi_status status = set_bool(value, cap);
707 if (ACPI_FAILURE(status))
708 return -EINVAL;
709 } else {
710 return -EINVAL;
712 return count;
715 static char *read_u8(char *p, uint32_t cap)
717 uint8_t result;
718 acpi_status status = get_u8(&result, cap);
719 if (ACPI_SUCCESS(status))
720 p += sprintf(p, "%u\n", result);
721 else
722 p += sprintf(p, "Read error" );
723 return p;
726 static unsigned long write_u8(const char *buffer, unsigned long count, uint32_t cap)
728 int value;
729 acpi_status (*set_method)(uint8_t);
731 /* Choose the appropriate set_u8 wrapper here, based on the capability */
732 switch (cap) {
733 case ACER_CAP_BRIGHTNESS:
734 set_method = set_brightness;
735 break;
736 default:
737 return -EINVAL;
740 if (sscanf(buffer, "%i", &value) == 1) {
741 acpi_status status = (*set_method)(value);
742 if (ACPI_FAILURE(status))
743 return -EINVAL;
744 } else {
745 return -EINVAL;
747 return count;
750 static char *read_version(char *p, uint32_t cap)
752 p += sprintf(p, "driver: %s\n", ACER_ACPI_VERSION);
753 p += sprintf(p, "proc_interface: %d\n",
754 PROC_INTERFACE_VERSION);
755 return p;
758 ProcItem proc_items[] = {
759 {"mailled", read_bool, write_bool, ACER_CAP_MAILLED},
760 {"bluetooth", read_bool, write_bool, ACER_CAP_BLUETOOTH},
761 {"wireless", read_bool, write_bool, ACER_CAP_WIRELESS},
762 {"brightness", read_u8, write_u8, ACER_CAP_BRIGHTNESS},
763 {"threeg", read_bool, write_bool, ACER_CAP_THREEG},
764 {"version", read_version, NULL, ACER_CAP_ANY},
765 {NULL}
768 static acpi_status __init add_proc_entries(void)
770 struct proc_dir_entry *proc;
771 ProcItem *item;
773 for (item = proc_items; item->name; ++item) {
775 * Only add the proc file if the current interface actually
776 * supports it
778 if (interface->capability & item->capability) {
779 proc = create_proc_read_entry(item->name,
780 S_IFREG | S_IRUGO | S_IWUSR,
781 acer_proc_dir,
782 (read_proc_t *) dispatch_read,
783 item);
784 if (proc)
785 proc->owner = THIS_MODULE;
786 if (proc && item->write_func)
787 proc->write_proc = (write_proc_t *) dispatch_write;
791 return AE_OK;
794 static acpi_status __exit remove_proc_entries(void)
796 ProcItem *item;
798 for (item = proc_items; item->name; ++item)
799 remove_proc_entry(item->name, acer_proc_dir);
800 return AE_OK;
804 * TODO: make this actually do useful stuff, if we ever see events
806 static void acer_acerkeys_notify(acpi_handle handle, u32 event, void *data)
808 struct acer_hotk *hotk = (struct acer_hotk *)data;
810 if (!hotk)
811 return;
812 printk(MY_ERR "Got an event!! %X", event);
814 return;
817 static int acpi_acerkeys_add(struct acpi_device *device)
819 struct acer_hotk *hotk = NULL;
820 acpi_status status = AE_OK;
822 if (!device)
823 return -EINVAL;
825 hotk =
826 (struct acer_hotk *)kmalloc(sizeof(struct acer_hotk), GFP_KERNEL);
827 if (!hotk)
828 return -ENOMEM;
829 memset(hotk, 0, sizeof(struct acer_hotk));
830 hotk->handle = device->handle;
831 strcpy(acpi_device_name(device), "Acer Laptop ACPI Extras");
832 strcpy(acpi_device_class(device), "hkey");
833 acpi_driver_data(device) = hotk;
834 hotk->device = device;
836 status = acpi_install_notify_handler(hotk->handle, ACPI_SYSTEM_NOTIFY,
837 acer_acerkeys_notify, hotk);
838 if (ACPI_FAILURE(status))
839 printk(MY_ERR "Error installing notify handler.\n");
840 return 0;
843 static int acpi_acerkeys_remove(struct acpi_device *device, int type)
845 acpi_status status = 0;
846 struct acer_hotk *hotk = NULL;
848 if (!device || !acpi_driver_data(device))
849 return -EINVAL;
850 hotk = (struct acer_hotk *)acpi_driver_data(device);
852 status = acpi_remove_notify_handler(hotk->handle, ACPI_SYSTEM_NOTIFY,
853 acer_acerkeys_notify);
854 if (ACPI_FAILURE(status))
855 printk(MY_ERR "Error removing notify handler.\n");
856 kfree(hotk);
858 return 0;
861 static struct acpi_driver acpi_acerkeys = {
862 .name = "acer_acpi",
863 .class = "hotkey",
864 .ids = "PNP0C14",
865 .ops = {
866 .add = acpi_acerkeys_add,
867 .remove = acpi_acerkeys_remove,
871 static int __init acer_acpi_init(void)
873 acpi_status status = AE_OK;
875 printk(MY_INFO "Acer Laptop ACPI Extras version %s\n",
876 ACER_ACPI_VERSION);
877 if (acpi_disabled) {
878 printk(MY_ERR "ACPI Disabled, unable to load.\n");
879 return -ENODEV;
883 * Detect which WMI interface we're using.
885 * TODO: This could be more dynamic, and perhaps done in part by the
886 * acpi_bus driver?
888 if (is_valid_acpi_path(AMW0_METHOD)) {
889 DEBUG(0, "Detected ACER AMW0 interface\n");
890 interface = &AMW0_interface;
891 } else if (is_valid_acpi_path(WMID_METHOD)) {
892 DEBUG(0, "Detected ACER WMID interface\n");
893 interface = &WMID_interface;
894 } else {
895 printk(MY_ERR "No or unsupported WMI interface, unable to load.\n");
896 goto error_no_interface;
899 /* Now that we have a known interface, initialize it */
900 if (interface->init) {
901 status = interface->init(interface);
902 if (ACPI_FAILURE(status)) {
903 printk(MY_ERR "Interface initialization failed.\n");
904 goto error_interface_init;
908 /* Create the proc entries */
909 acer_proc_dir = proc_mkdir(PROC_ACER, acpi_root_dir);
910 if (!acer_proc_dir) {
911 printk(MY_ERR "Unable to create /proc entries, aborting.\n");
912 goto error_proc_mkdir;
915 acer_proc_dir->owner = THIS_MODULE;
916 status = add_proc_entries();
917 if (ACPI_FAILURE(status)) {
918 printk(MY_ERR "Unable to create /proc entries, aborting.\n");
919 goto error_proc_add;
923 * Register the hotkeys driver
925 * TODO: Does this do anything? Can we use the bus detection code to
926 * check for the interface or all or part of the method ID path?
928 status = acpi_bus_register_driver(&acpi_acerkeys);
929 if (ACPI_FAILURE(status)) {
930 printk(MY_ERR "Unable to register driver, aborting.\n");
931 goto error_acpi_bus_register;
934 /* Finally, override any initial settings with values from the commandline */
935 acpi_commandline_init();
937 return 0;
939 error_acpi_bus_register:
940 remove_proc_entries();
941 error_proc_add:
942 if (acer_proc_dir)
943 remove_proc_entry(PROC_ACER, acpi_root_dir);
944 error_proc_mkdir:
945 if (interface->free)
946 interface->free(interface);
947 error_interface_init:
948 error_no_interface:
949 return -ENODEV;
952 static void __exit acer_acpi_exit(void)
954 acpi_bus_unregister_driver(&acpi_acerkeys);
956 remove_proc_entries();
958 if (acer_proc_dir)
959 remove_proc_entry(PROC_ACER, acpi_root_dir);
961 if (interface->free)
962 interface->free(interface);
964 printk(MY_INFO "Acer Laptop ACPI Extras unloaded\n");
965 return;
968 module_init(acer_acpi_init);
969 module_exit(acer_acpi_exit);