blob: 222d4fba0ffa874226889c3b8078f9994d591e12 [file] [log] [blame]
Corentin Chary85091b72007-01-26 14:04:30 +01001/*
2 * asus-laptop.c - Asus Laptop Support
3 *
4 *
5 * Copyright (C) 2002-2005 Julien Lerouge, 2003-2006 Karol Kozimor
6 * Copyright (C) 2006 Corentin Chary
7 *
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.
12 *
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.
17 *
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
21 *
22 *
23 * The development page for this driver is located at
24 * http://sourceforge.net/projects/acpi4asus/
25 *
26 * Credits:
27 * Pontus Fuchs - Helper functions, cleanup
28 * Johann Wiesner - Small compile fixes
29 * John Belmonte - ACPI code for Toshiba laptop was a good starting point.
30 * Eric Burghard - LED display support for W1N
31 * Josh Green - Light Sens support
32 * Thomas Tuttle - His first patch for led support was very helpfull
33 *
34 */
35
36#include <linux/autoconf.h>
37#include <linux/kernel.h>
38#include <linux/module.h>
39#include <linux/init.h>
40#include <linux/types.h>
41#include <linux/err.h>
42#include <linux/proc_fs.h>
Corentin Charybe18cda2007-01-26 14:04:35 +010043#include <linux/leds.h>
Corentin Chary85091b72007-01-26 14:04:30 +010044#include <linux/platform_device.h>
45#include <acpi/acpi_drivers.h>
46#include <acpi/acpi_bus.h>
47#include <asm/uaccess.h>
48
49#define ASUS_LAPTOP_VERSION "0.40"
50
51#define ASUS_HOTK_NAME "Asus Laptop Support"
52#define ASUS_HOTK_CLASS "hotkey"
53#define ASUS_HOTK_DEVICE_NAME "Hotkey"
54#define ASUS_HOTK_HID "ATK0100"
55#define ASUS_HOTK_FILE "asus-laptop"
56#define ASUS_HOTK_PREFIX "\\_SB.ATKD."
57
Corentin Charybe18cda2007-01-26 14:04:35 +010058/*
Corentin Chary4564de12007-01-26 14:04:40 +010059 * Known bits returned by \_SB.ATKD.HWRS
Corentin Charybe18cda2007-01-26 14:04:35 +010060 */
Corentin Chary4564de12007-01-26 14:04:40 +010061#define WL_HWRS 0x80
62#define BT_HWRS 0x100
63
64/*
65 * Flags for hotk status
66 * WL_ON and BT_ON are also used for wireless_status()
67 */
68#define WL_ON 0x01 //internal Wifi
69#define BT_ON 0x02 //internal Bluetooth
Corentin Charybe18cda2007-01-26 14:04:35 +010070#define MLED_ON 0x04 //mail LED
71#define TLED_ON 0x08 //touchpad LED
72#define RLED_ON 0x10 //Record LED
73#define PLED_ON 0x20 //Phone LED
74
Corentin Chary85091b72007-01-26 14:04:30 +010075#define ASUS_LOG ASUS_HOTK_FILE ": "
76#define ASUS_ERR KERN_ERR ASUS_LOG
77#define ASUS_WARNING KERN_WARNING ASUS_LOG
78#define ASUS_NOTICE KERN_NOTICE ASUS_LOG
79#define ASUS_INFO KERN_INFO ASUS_LOG
80#define ASUS_DEBUG KERN_DEBUG ASUS_LOG
81
82MODULE_AUTHOR("Julien Lerouge, Karol Kozimor, Corentin Chary");
83MODULE_DESCRIPTION(ASUS_HOTK_NAME);
84MODULE_LICENSE("GPL");
85
86#define ASUS_HANDLE(object, paths...) \
87 static acpi_handle object##_handle = NULL; \
88 static char *object##_paths[] = { paths }
89
Corentin Charybe18cda2007-01-26 14:04:35 +010090/* LED */
91ASUS_HANDLE(mled_set, ASUS_HOTK_PREFIX "MLED");
92ASUS_HANDLE(tled_set, ASUS_HOTK_PREFIX "TLED");
93ASUS_HANDLE(rled_set, ASUS_HOTK_PREFIX "RLED"); /* W1JC */
94ASUS_HANDLE(pled_set, ASUS_HOTK_PREFIX "PLED"); /* A7J */
95
Corentin Chary4564de12007-01-26 14:04:40 +010096/* Bluetooth and WLAN
97 * WLED and BLED are not handled like other XLED, because in some dsdt
98 * they also control the WLAN/Bluetooth device.
99 */
100ASUS_HANDLE(wl_switch, ASUS_HOTK_PREFIX "WLED");
101ASUS_HANDLE(bt_switch, ASUS_HOTK_PREFIX "BLED");
102ASUS_HANDLE(wireless_status, ASUS_HOTK_PREFIX "RSTS"); /* All new models */
103
Corentin Chary85091b72007-01-26 14:04:30 +0100104/*
105 * This is the main structure, we can use it to store anything interesting
106 * about the hotk device
107 */
108struct asus_hotk {
109 char *name; //laptop name
110 struct acpi_device *device; //the device we are in
111 acpi_handle handle; //the handle of the hotk device
112 char status; //status of the hotk, for LEDs, ...
113 u16 event_count[128]; //count for each event TODO make this better
114};
115
116/*
117 * This header is made available to allow proper configuration given model,
118 * revision number , ... this info cannot go in struct asus_hotk because it is
119 * available before the hotk
120 */
121static struct acpi_table_header *asus_info;
122
123/* The actual device the driver binds to */
124static struct asus_hotk *hotk;
125
126/*
127 * The hotkey driver declaration
128 */
129static int asus_hotk_add(struct acpi_device *device);
130static int asus_hotk_remove(struct acpi_device *device, int type);
131static struct acpi_driver asus_hotk_driver = {
132 .name = ASUS_HOTK_NAME,
133 .class = ASUS_HOTK_CLASS,
134 .ids = ASUS_HOTK_HID,
135 .ops = {
136 .add = asus_hotk_add,
137 .remove = asus_hotk_remove,
138 },
139};
140
Corentin Charybe18cda2007-01-26 14:04:35 +0100141/* These functions actually update the LED's, and are called from a
142 * workqueue. By doing this as separate work rather than when the LED
143 * subsystem asks, we avoid messing with the Asus ACPI stuff during a
144 * potentially bad time, such as a timer interrupt. */
145static struct workqueue_struct *led_workqueue;
146
147#define ASUS_LED(object, ledname) \
148 static void object##_led_set(struct led_classdev *led_cdev, \
149 enum led_brightness value); \
150 static void object##_led_update(struct work_struct *ignored); \
151 static int object##_led_wk; \
152 DECLARE_WORK(object##_led_work, object##_led_update); \
153 static struct led_classdev object##_led = { \
154 .name = "asus:" ledname, \
155 .brightness_set = object##_led_set, \
156 }
157
158ASUS_LED(mled, "mail");
159ASUS_LED(tled, "touchpad");
160ASUS_LED(rled, "record");
161ASUS_LED(pled, "phone");
162
Corentin Chary85091b72007-01-26 14:04:30 +0100163/*
164 * This function evaluates an ACPI method, given an int as parameter, the
165 * method is searched within the scope of the handle, can be NULL. The output
166 * of the method is written is output, which can also be NULL
167 *
168 * returns 1 if write is successful, 0 else.
169 */
170static int write_acpi_int(acpi_handle handle, const char *method, int val,
171 struct acpi_buffer *output)
172{
173 struct acpi_object_list params; //list of input parameters (an int here)
174 union acpi_object in_obj; //the only param we use
175 acpi_status status;
176
177 params.count = 1;
178 params.pointer = &in_obj;
179 in_obj.type = ACPI_TYPE_INTEGER;
180 in_obj.integer.value = val;
181
182 status = acpi_evaluate_object(handle, (char *)method, &params, output);
183 return (status == AE_OK);
184}
185
186static int read_acpi_int(acpi_handle handle, const char *method, int *val,
187 struct acpi_object_list *params)
188{
189 struct acpi_buffer output;
190 union acpi_object out_obj;
191 acpi_status status;
192
193 output.length = sizeof(out_obj);
194 output.pointer = &out_obj;
195
196 status = acpi_evaluate_object(handle, (char *)method, params, &output);
197 *val = out_obj.integer.value;
198 return (status == AE_OK) && (out_obj.type == ACPI_TYPE_INTEGER);
199}
200
Corentin Chary4564de12007-01-26 14:04:40 +0100201static int read_wireless_status(int mask) {
202 int status;
203
204 if (!wireless_status_handle)
205 return (hotk->status & mask) ? 1 : 0;
206
207 if (read_acpi_int(wireless_status_handle, NULL, &status, NULL)) {
208 return (status & mask) ? 1 : 0;
209 } else
210 printk(ASUS_WARNING "Error reading Wireless status\n");
211
212 return (hotk->status & mask) ? 1 : 0;
213}
214
Corentin Charybe18cda2007-01-26 14:04:35 +0100215/* Generic LED functions */
216static int read_status(int mask)
217{
Corentin Chary4564de12007-01-26 14:04:40 +0100218 /* There is a special method for both wireless devices */
219 if (mask == BT_ON || mask == WL_ON)
220 return read_wireless_status(mask);
221
Corentin Charybe18cda2007-01-26 14:04:35 +0100222 return (hotk->status & mask) ? 1 : 0;
223}
224
225static void write_status(acpi_handle handle, int out, int mask,
Corentin Chary4564de12007-01-26 14:04:40 +0100226 int invert)
Corentin Charybe18cda2007-01-26 14:04:35 +0100227{
228 hotk->status = (out) ? (hotk->status | mask) : (hotk->status & ~mask);
229
230 if (invert) /* invert target value */
231 out = !out & 0x1;
232
233 if (handle && !write_acpi_int(handle, NULL, out, NULL))
234 printk(ASUS_WARNING " write failed\n");
235}
236
237/* /sys/class/led handlers */
238#define ASUS_LED_HANDLER(object, mask, invert) \
239 static void object##_led_set(struct led_classdev *led_cdev, \
240 enum led_brightness value) \
241 { \
242 object##_led_wk = value; \
243 queue_work(led_workqueue, &object##_led_work); \
244 } \
245 static void object##_led_update(struct work_struct *ignored) \
246 { \
247 int value = object##_led_wk; \
248 write_status(object##_set_handle, value, (mask), (invert)); \
249 }
250
251ASUS_LED_HANDLER(mled, MLED_ON, 1);
252ASUS_LED_HANDLER(pled, PLED_ON, 0);
253ASUS_LED_HANDLER(rled, RLED_ON, 0);
254ASUS_LED_HANDLER(tled, TLED_ON, 0);
255
Corentin Chary85091b72007-01-26 14:04:30 +0100256/*
257 * Platform device handlers
258 */
259
260/*
261 * We write our info in page, we begin at offset off and cannot write more
262 * than count bytes. We set eof to 1 if we handle those 2 values. We return the
263 * number of bytes written in page
264 */
265static ssize_t show_infos(struct device *dev,
266 struct device_attribute *attr, char *page)
267{
268 int len = 0;
269 int temp;
270 char buf[16]; //enough for all info
271 /*
272 * We use the easy way, we don't care of off and count, so we don't set eof
273 * to 1
274 */
275
276 len += sprintf(page, ASUS_HOTK_NAME " " ASUS_LAPTOP_VERSION "\n");
277 len += sprintf(page + len, "Model reference : %s\n", hotk->name);
278 /*
279 * The SFUN method probably allows the original driver to get the list
280 * of features supported by a given model. For now, 0x0100 or 0x0800
281 * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card.
282 * The significance of others is yet to be found.
283 */
284 if (read_acpi_int(hotk->handle, "SFUN", &temp, NULL))
285 len +=
286 sprintf(page + len, "SFUN value : 0x%04x\n", temp);
287 /*
288 * Another value for userspace: the ASYM method returns 0x02 for
289 * battery low and 0x04 for battery critical, its readings tend to be
290 * more accurate than those provided by _BST.
291 * Note: since not all the laptops provide this method, errors are
292 * silently ignored.
293 */
294 if (read_acpi_int(hotk->handle, "ASYM", &temp, NULL))
295 len +=
296 sprintf(page + len, "ASYM value : 0x%04x\n", temp);
297 if (asus_info) {
298 snprintf(buf, 16, "%d", asus_info->length);
299 len += sprintf(page + len, "DSDT length : %s\n", buf);
300 snprintf(buf, 16, "%d", asus_info->checksum);
301 len += sprintf(page + len, "DSDT checksum : %s\n", buf);
302 snprintf(buf, 16, "%d", asus_info->revision);
303 len += sprintf(page + len, "DSDT revision : %s\n", buf);
304 snprintf(buf, 7, "%s", asus_info->oem_id);
305 len += sprintf(page + len, "OEM id : %s\n", buf);
306 snprintf(buf, 9, "%s", asus_info->oem_table_id);
307 len += sprintf(page + len, "OEM table id : %s\n", buf);
308 snprintf(buf, 16, "%x", asus_info->oem_revision);
309 len += sprintf(page + len, "OEM revision : 0x%s\n", buf);
310 snprintf(buf, 5, "%s", asus_info->asl_compiler_id);
311 len += sprintf(page + len, "ASL comp vendor id : %s\n", buf);
312 snprintf(buf, 16, "%x", asus_info->asl_compiler_revision);
313 len += sprintf(page + len, "ASL comp revision : 0x%s\n", buf);
314 }
315
316 return len;
317}
318
319static int parse_arg(const char *buf, unsigned long count, int *val)
320{
321 if (!count)
322 return 0;
323 if (count > 31)
324 return -EINVAL;
325 if (sscanf(buf, "%i", val) != 1)
326 return -EINVAL;
327 return count;
328}
329
Corentin Chary4564de12007-01-26 14:04:40 +0100330static ssize_t store_status(const char *buf, size_t count,
331 acpi_handle handle, int mask, int invert)
332{
333 int rv, value;
334 int out = 0;
335
336 rv = parse_arg(buf, count, &value);
337 if (rv > 0)
338 out = value ? 1 : 0;
339
340 write_status(handle, out, mask, invert);
341
342 return rv;
343}
344
345/*
346 * WLAN
347 */
348static ssize_t show_wlan(struct device *dev,
349 struct device_attribute *attr, char *buf)
350{
351 return sprintf(buf, "%d\n", read_status(WL_ON));
352}
353
354static ssize_t store_wlan(struct device *dev, struct device_attribute *attr,
355 const char *buf, size_t count)
356{
357 return store_status(buf, count, wl_switch_handle, WL_ON, 0);
358}
359
360/*
361 * Bluetooth
362 */
363static ssize_t show_bluetooth(struct device *dev,
364 struct device_attribute *attr, char *buf)
365{
366 return sprintf(buf, "%d\n", read_status(BT_ON));
367}
368
369static ssize_t store_bluetooth(struct device *dev, struct device_attribute *attr,
370 const char *buf, size_t count)
371{
372 return store_status(buf, count, bt_switch_handle, BT_ON, 0);
373}
374
Corentin Chary85091b72007-01-26 14:04:30 +0100375static void asus_hotk_notify(acpi_handle handle, u32 event, void *data)
376{
377 /* TODO Find a better way to handle events count. */
378 if (!hotk)
379 return;
380
381 acpi_bus_generate_event(hotk->device, event,
382 hotk->event_count[event % 128]++);
383
384 return;
385}
386
387#define ASUS_CREATE_DEVICE_ATTR(_name) \
388 struct device_attribute dev_attr_##_name = { \
389 .attr = { \
390 .name = __stringify(_name), \
391 .mode = 0, \
392 .owner = THIS_MODULE }, \
393 .show = NULL, \
394 .store = NULL, \
395 }
396
397#define ASUS_SET_DEVICE_ATTR(_name, _mode, _show, _store) \
398 do { \
399 dev_attr_##_name.attr.mode = _mode; \
400 dev_attr_##_name.show = _show; \
401 dev_attr_##_name.store = _store; \
402 } while(0)
403
404static ASUS_CREATE_DEVICE_ATTR(infos);
Corentin Chary4564de12007-01-26 14:04:40 +0100405static ASUS_CREATE_DEVICE_ATTR(wlan);
406static ASUS_CREATE_DEVICE_ATTR(bluetooth);
Corentin Chary85091b72007-01-26 14:04:30 +0100407
408static struct attribute *asuspf_attributes[] = {
409 &dev_attr_infos.attr,
Corentin Chary4564de12007-01-26 14:04:40 +0100410 &dev_attr_wlan.attr,
411 &dev_attr_bluetooth.attr,
Corentin Chary85091b72007-01-26 14:04:30 +0100412 NULL
413};
414
415static struct attribute_group asuspf_attribute_group = {
416 .attrs = asuspf_attributes
417};
418
419static struct platform_driver asuspf_driver = {
420 .driver = {
421 .name = ASUS_HOTK_FILE,
422 .owner = THIS_MODULE,
423 }
424};
425
426static struct platform_device *asuspf_device;
427
428
429static void asus_hotk_add_fs(void)
430{
431 ASUS_SET_DEVICE_ATTR(infos, 0444, show_infos, NULL);
Corentin Chary4564de12007-01-26 14:04:40 +0100432
433 if (wl_switch_handle)
434 ASUS_SET_DEVICE_ATTR(wlan, 0644, show_wlan, store_wlan);
435
436 if (bt_switch_handle)
437 ASUS_SET_DEVICE_ATTR(bluetooth, 0644,
438 show_bluetooth, store_bluetooth);
Corentin Chary85091b72007-01-26 14:04:30 +0100439}
440
441static int asus_handle_init(char *name, acpi_handle *handle,
442 char **paths, int num_paths)
443{
444 int i;
445 acpi_status status;
446
447 for (i = 0; i < num_paths; i++) {
448 status = acpi_get_handle(NULL, paths[i], handle);
449 if (ACPI_SUCCESS(status))
450 return 0;
451 }
452
453 *handle = NULL;
454 return -ENODEV;
455}
456
457#define ASUS_HANDLE_INIT(object) \
458 asus_handle_init(#object, &object##_handle, object##_paths, \
459 ARRAY_SIZE(object##_paths))
460
461
462/*
463 * This function is used to initialize the hotk with right values. In this
464 * method, we can make all the detection we want, and modify the hotk struct
465 */
466static int asus_hotk_get_info(void)
467{
468 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
469 struct acpi_buffer dsdt = { ACPI_ALLOCATE_BUFFER, NULL };
470 union acpi_object *model = NULL;
Corentin Chary4564de12007-01-26 14:04:40 +0100471 int bsts_result, hwrs_result;
Corentin Chary85091b72007-01-26 14:04:30 +0100472 char *string = NULL;
473 acpi_status status;
474
475 /*
476 * Get DSDT headers early enough to allow for differentiating between
477 * models, but late enough to allow acpi_bus_register_driver() to fail
478 * before doing anything ACPI-specific. Should we encounter a machine,
479 * which needs special handling (i.e. its hotkey device has a different
480 * HID), this bit will be moved. A global variable asus_info contains
481 * the DSDT header.
482 */
483 status = acpi_get_table(ACPI_TABLE_ID_DSDT, 1, &dsdt);
484 if (ACPI_FAILURE(status))
485 printk(ASUS_WARNING "Couldn't get the DSDT table header\n");
486 else
487 asus_info = dsdt.pointer;
488
489 /* We have to write 0 on init this far for all ASUS models */
490 if (!write_acpi_int(hotk->handle, "INIT", 0, &buffer)) {
491 printk(ASUS_ERR "Hotkey initialization failed\n");
492 return -ENODEV;
493 }
494
495 /* This needs to be called for some laptops to init properly */
496 if (!read_acpi_int(hotk->handle, "BSTS", &bsts_result, NULL))
497 printk(ASUS_WARNING "Error calling BSTS\n");
498 else if (bsts_result)
499 printk(ASUS_NOTICE "BSTS called, 0x%02x returned\n",
500 bsts_result);
501
502 /*
503 * Try to match the object returned by INIT to the specific model.
504 * Handle every possible object (or the lack of thereof) the DSDT
505 * writers might throw at us. When in trouble, we pass NULL to
506 * asus_model_match() and try something completely different.
507 */
508 if (buffer.pointer) {
509 model = buffer.pointer;
510 switch (model->type) {
511 case ACPI_TYPE_STRING:
512 string = model->string.pointer;
513 break;
514 case ACPI_TYPE_BUFFER:
515 string = model->buffer.pointer;
516 break;
517 default:
518 string = "";
519 break;
520 }
521 }
522 hotk->name = kstrdup(string, GFP_KERNEL);
523 if (!hotk->name)
524 return -ENOMEM;
525
526 if(*string)
527 printk(ASUS_NOTICE " %s model detected\n", string);
528
Corentin Charybe18cda2007-01-26 14:04:35 +0100529 ASUS_HANDLE_INIT(mled_set);
530 ASUS_HANDLE_INIT(tled_set);
531 ASUS_HANDLE_INIT(rled_set);
532 ASUS_HANDLE_INIT(pled_set);
533
Corentin Chary4564de12007-01-26 14:04:40 +0100534 /*
535 * The HWRS method return informations about the hardware.
536 * 0x80 bit is for WLAN, 0x100 for Bluetooth.
537 * The significance of others is yet to be found.
538 * If we don't find the method, we assume the device are present.
539 */
540 if (!read_acpi_int(hotk->handle, "HRWS", &hwrs_result, NULL))
541 hwrs_result = WL_HWRS | BT_HWRS;
542
543 if(hwrs_result & WL_HWRS)
544 ASUS_HANDLE_INIT(wl_switch);
545 if(hwrs_result & BT_HWRS)
546 ASUS_HANDLE_INIT(bt_switch);
547
548 ASUS_HANDLE_INIT(wireless_status);
549
Corentin Chary85091b72007-01-26 14:04:30 +0100550 kfree(model);
551
552 return AE_OK;
553}
554
555static int asus_hotk_check(void)
556{
557 int result = 0;
558
559 result = acpi_bus_get_status(hotk->device);
560 if (result)
561 return result;
562
563 if (hotk->device->status.present) {
564 result = asus_hotk_get_info();
565 } else {
566 printk(ASUS_ERR "Hotkey device not present, aborting\n");
567 return -EINVAL;
568 }
569
570 return result;
571}
572
573static int asus_hotk_found;
574
575static int asus_hotk_add(struct acpi_device *device)
576{
577 acpi_status status = AE_OK;
578 int result;
579
580 if (!device)
581 return -EINVAL;
582
583 printk(ASUS_NOTICE "Asus Laptop Support version %s\n",
584 ASUS_LAPTOP_VERSION);
585
586 hotk = kmalloc(sizeof(struct asus_hotk), GFP_KERNEL);
587 if (!hotk)
588 return -ENOMEM;
589 memset(hotk, 0, sizeof(struct asus_hotk));
590
591 hotk->handle = device->handle;
592 strcpy(acpi_device_name(device), ASUS_HOTK_DEVICE_NAME);
593 strcpy(acpi_device_class(device), ASUS_HOTK_CLASS);
594 acpi_driver_data(device) = hotk;
595 hotk->device = device;
596
597 result = asus_hotk_check();
598 if (result)
599 goto end;
600
601 asus_hotk_add_fs();
602
603 /*
604 * We install the handler, it will receive the hotk in parameter, so, we
605 * could add other data to the hotk struct
606 */
607 status = acpi_install_notify_handler(hotk->handle, ACPI_SYSTEM_NOTIFY,
608 asus_hotk_notify, hotk);
609 if (ACPI_FAILURE(status))
610 printk(ASUS_ERR "Error installing notify handler\n");
611
612 asus_hotk_found = 1;
613
Corentin Chary4564de12007-01-26 14:04:40 +0100614 /* WLED and BLED are on by default */
615 write_status(bt_switch_handle, 1, BT_ON, 0);
616 write_status(wl_switch_handle, 1, WL_ON, 0);
617
Corentin Chary85091b72007-01-26 14:04:30 +0100618 end:
619 if (result) {
620 kfree(hotk->name);
621 kfree(hotk);
622 }
623
624 return result;
625}
626
627static int asus_hotk_remove(struct acpi_device *device, int type)
628{
629 acpi_status status = 0;
630
631 if (!device || !acpi_driver_data(device))
632 return -EINVAL;
633
634 status = acpi_remove_notify_handler(hotk->handle, ACPI_SYSTEM_NOTIFY,
635 asus_hotk_notify);
636 if (ACPI_FAILURE(status))
637 printk(ASUS_ERR "Error removing notify handler\n");
638
639 kfree(hotk->name);
640 kfree(hotk);
641
642 return 0;
643}
644
Corentin Charybe18cda2007-01-26 14:04:35 +0100645#define ASUS_LED_UNREGISTER(object) \
646 if(object##_led.class_dev \
647 && !IS_ERR(object##_led.class_dev)) \
648 led_classdev_unregister(&object##_led)
649
650static void asus_led_exit(void)
651{
652 ASUS_LED_UNREGISTER(mled);
653 ASUS_LED_UNREGISTER(tled);
654 ASUS_LED_UNREGISTER(pled);
655 ASUS_LED_UNREGISTER(rled);
656
657 destroy_workqueue(led_workqueue);
658}
659
Corentin Chary85091b72007-01-26 14:04:30 +0100660static void __exit asus_laptop_exit(void)
661{
Corentin Charybe18cda2007-01-26 14:04:35 +0100662 asus_led_exit();
663
Corentin Chary85091b72007-01-26 14:04:30 +0100664 acpi_bus_unregister_driver(&asus_hotk_driver);
665 sysfs_remove_group(&asuspf_device->dev.kobj, &asuspf_attribute_group);
666 platform_device_unregister(asuspf_device);
667 platform_driver_unregister(&asuspf_driver);
668
669 kfree(asus_info);
670}
671
Corentin Charybe18cda2007-01-26 14:04:35 +0100672static int asus_led_register(acpi_handle handle,
673 struct led_classdev * ldev,
674 struct device * dev)
675{
676 if(!handle)
677 return 0;
678
679 return led_classdev_register(dev, ldev);
680}
681#define ASUS_LED_REGISTER(object, device) \
682 asus_led_register(object##_set_handle, &object##_led, device)
683
684static int asus_led_init(struct device * dev)
685{
686 int rv;
687
688 rv = ASUS_LED_REGISTER(mled, dev);
689 if(rv)
690 return rv;
691
692 rv = ASUS_LED_REGISTER(tled, dev);
693 if(rv)
694 return rv;
695
696 rv = ASUS_LED_REGISTER(rled, dev);
697 if(rv)
698 return rv;
699
700 rv = ASUS_LED_REGISTER(pled, dev);
701 if(rv)
702 return rv;
703
704 led_workqueue = create_singlethread_workqueue("led_workqueue");
705 if(!led_workqueue)
706 return -ENOMEM;
707
708 return 0;
709}
710
Corentin Chary85091b72007-01-26 14:04:30 +0100711static int __init asus_laptop_init(void)
712{
Corentin Charybe18cda2007-01-26 14:04:35 +0100713 struct device *dev;
Corentin Chary85091b72007-01-26 14:04:30 +0100714 int result;
715
716 if (acpi_disabled)
717 return -ENODEV;
718
719 if (!acpi_specific_hotkey_enabled) {
720 printk(ASUS_ERR "Using generic hotkey driver\n");
721 return -ENODEV;
722 }
723
724 result = acpi_bus_register_driver(&asus_hotk_driver);
725 if (result < 0)
726 return result;
727
728 /*
729 * This is a bit of a kludge. We only want this module loaded
730 * for ASUS systems, but there's currently no way to probe the
731 * ACPI namespace for ASUS HIDs. So we just return failure if
732 * we didn't find one, which will cause the module to be
733 * unloaded.
734 */
735 if (!asus_hotk_found) {
736 acpi_bus_unregister_driver(&asus_hotk_driver);
737 return -ENODEV;
738 }
739
Corentin Charybe18cda2007-01-26 14:04:35 +0100740 dev = acpi_get_physical_device(hotk->device->handle);
741
742 result = asus_led_init(dev);
743 if(result)
744 goto fail_led;
745
Corentin Chary85091b72007-01-26 14:04:30 +0100746 /* Register platform stuff */
747 result = platform_driver_register(&asuspf_driver);
748 if (result)
749 goto fail_platform_driver;
750
751 asuspf_device = platform_device_alloc(ASUS_HOTK_FILE, -1);
752 if (!asuspf_device) {
753 result = -ENOMEM;
754 goto fail_platform_device1;
755 }
756
757 result = platform_device_add(asuspf_device);
758 if (result)
759 goto fail_platform_device2;
760
761 result = sysfs_create_group(&asuspf_device->dev.kobj,
762 &asuspf_attribute_group);
763 if (result)
764 goto fail_sysfs;
765
766 return 0;
767
768fail_sysfs:
769 platform_device_del(asuspf_device);
770
771fail_platform_device2:
772 platform_device_put(asuspf_device);
773
774fail_platform_device1:
775 platform_driver_unregister(&asuspf_driver);
776
777fail_platform_driver:
Corentin Charybe18cda2007-01-26 14:04:35 +0100778 asus_led_exit();
779
780fail_led:
Corentin Chary85091b72007-01-26 14:04:30 +0100781
782 return result;
783}
784
785module_init(asus_laptop_init);
786module_exit(asus_laptop_exit);