[TCP]: Introduce tcp_hdrlen() and tcp_optlen()
[deliverable/linux.git] / drivers / misc / asus-laptop.c
CommitLineData
85091b71
CC
1/*
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>
6b7091e7
CC
43#include <linux/backlight.h>
44#include <linux/fb.h>
be18cdab 45#include <linux/leds.h>
85091b71
CC
46#include <linux/platform_device.h>
47#include <acpi/acpi_drivers.h>
48#include <acpi/acpi_bus.h>
49#include <asm/uaccess.h>
50
51#define ASUS_LAPTOP_VERSION "0.40"
52
53#define ASUS_HOTK_NAME "Asus Laptop Support"
54#define ASUS_HOTK_CLASS "hotkey"
55#define ASUS_HOTK_DEVICE_NAME "Hotkey"
56#define ASUS_HOTK_HID "ATK0100"
57#define ASUS_HOTK_FILE "asus-laptop"
58#define ASUS_HOTK_PREFIX "\\_SB.ATKD."
59
6b7091e7
CC
60/*
61 * Some events we use, same for all Asus
62 */
63#define ATKD_BR_UP 0x10
64#define ATKD_BR_DOWN 0x20
65#define ATKD_LCD_ON 0x33
66#define ATKD_LCD_OFF 0x34
67
4564de17
CC
68/*
69 * Known bits returned by \_SB.ATKD.HWRS
70 */
71#define WL_HWRS 0x80
72#define BT_HWRS 0x100
73
be18cdab
CC
74/*
75 * Flags for hotk status
4564de17 76 * WL_ON and BT_ON are also used for wireless_status()
be18cdab 77 */
4564de17
CC
78#define WL_ON 0x01 //internal Wifi
79#define BT_ON 0x02 //internal Bluetooth
be18cdab
CC
80#define MLED_ON 0x04 //mail LED
81#define TLED_ON 0x08 //touchpad LED
8def05fa
LB
82#define RLED_ON 0x10 //Record LED
83#define PLED_ON 0x20 //Phone LED
84#define LCD_ON 0x40 //LCD backlight
be18cdab 85
85091b71
CC
86#define ASUS_LOG ASUS_HOTK_FILE ": "
87#define ASUS_ERR KERN_ERR ASUS_LOG
88#define ASUS_WARNING KERN_WARNING ASUS_LOG
89#define ASUS_NOTICE KERN_NOTICE ASUS_LOG
90#define ASUS_INFO KERN_INFO ASUS_LOG
91#define ASUS_DEBUG KERN_DEBUG ASUS_LOG
92
93MODULE_AUTHOR("Julien Lerouge, Karol Kozimor, Corentin Chary");
94MODULE_DESCRIPTION(ASUS_HOTK_NAME);
95MODULE_LICENSE("GPL");
96
97#define ASUS_HANDLE(object, paths...) \
98 static acpi_handle object##_handle = NULL; \
99 static char *object##_paths[] = { paths }
100
be18cdab
CC
101/* LED */
102ASUS_HANDLE(mled_set, ASUS_HOTK_PREFIX "MLED");
103ASUS_HANDLE(tled_set, ASUS_HOTK_PREFIX "TLED");
8def05fa
LB
104ASUS_HANDLE(rled_set, ASUS_HOTK_PREFIX "RLED"); /* W1JC */
105ASUS_HANDLE(pled_set, ASUS_HOTK_PREFIX "PLED"); /* A7J */
be18cdab 106
722ad971
CC
107/* LEDD */
108ASUS_HANDLE(ledd_set, ASUS_HOTK_PREFIX "SLCM");
109
4564de17
CC
110/* Bluetooth and WLAN
111 * WLED and BLED are not handled like other XLED, because in some dsdt
112 * they also control the WLAN/Bluetooth device.
113 */
114ASUS_HANDLE(wl_switch, ASUS_HOTK_PREFIX "WLED");
115ASUS_HANDLE(bt_switch, ASUS_HOTK_PREFIX "BLED");
8def05fa 116ASUS_HANDLE(wireless_status, ASUS_HOTK_PREFIX "RSTS"); /* All new models */
4564de17 117
6b7091e7
CC
118/* Brightness */
119ASUS_HANDLE(brightness_set, ASUS_HOTK_PREFIX "SPLV");
120ASUS_HANDLE(brightness_get, ASUS_HOTK_PREFIX "GPLV");
121
122/* Backlight */
8def05fa
LB
123ASUS_HANDLE(lcd_switch, "\\_SB.PCI0.SBRG.EC0._Q10", /* All new models */
124 "\\_SB.PCI0.ISA.EC0._Q10", /* A1x */
125 "\\_SB.PCI0.PX40.ECD0._Q10", /* L3C */
126 "\\_SB.PCI0.PX40.EC0.Q10", /* M1A */
127 "\\_SB.PCI0.LPCB.EC0._Q10", /* P30 */
128 "\\_SB.PCI0.PX40.Q10", /* S1x */
129 "\\Q10"); /* A2x, L2D, L3D, M2E */
6b7091e7 130
78127b4a
CC
131/* Display */
132ASUS_HANDLE(display_set, ASUS_HOTK_PREFIX "SDSP");
8def05fa
LB
133ASUS_HANDLE(display_get, "\\_SB.PCI0.P0P1.VGA.GETD", /* A6B, A6K A6R A7D F3JM L4R M6R A3G
134 M6A M6V VX-1 V6J V6V W3Z */
135 "\\_SB.PCI0.P0P2.VGA.GETD", /* A3E A4K, A4D A4L A6J A7J A8J Z71V M9V
78127b4a 136 S5A M5A z33A W1Jc W2V */
8def05fa
LB
137 "\\_SB.PCI0.P0P3.VGA.GETD", /* A6V A6Q */
138 "\\_SB.PCI0.P0PA.VGA.GETD", /* A6T, A6M */
139 "\\_SB.PCI0.PCI1.VGAC.NMAP", /* L3C */
140 "\\_SB.PCI0.VGA.GETD", /* Z96F */
141 "\\ACTD", /* A2D */
142 "\\ADVG", /* A4G Z71A W1N W5A W5F M2N M3N M5N M6N S1N S5N */
143 "\\DNXT", /* P30 */
144 "\\INFB", /* A2H D1 L2D L3D L3H L2E L5D L5C M1A M2E L4L W3V */
145 "\\SSTE"); /* A3F A6F A3N A3L M6N W3N W6A */
146
147ASUS_HANDLE(ls_switch, ASUS_HOTK_PREFIX "ALSC"); /* Z71A Z71V */
148ASUS_HANDLE(ls_level, ASUS_HOTK_PREFIX "ALSL"); /* Z71A Z71V */
8b857353 149
85091b71
CC
150/*
151 * This is the main structure, we can use it to store anything interesting
152 * about the hotk device
153 */
154struct asus_hotk {
8def05fa 155 char *name; //laptop name
85091b71
CC
156 struct acpi_device *device; //the device we are in
157 acpi_handle handle; //the handle of the hotk device
158 char status; //status of the hotk, for LEDs, ...
722ad971 159 u32 ledd_status; //status of the LED display
8def05fa
LB
160 u8 light_level; //light sensor level
161 u8 light_switch; //light sensor switch value
85091b71
CC
162 u16 event_count[128]; //count for each event TODO make this better
163};
164
165/*
166 * This header is made available to allow proper configuration given model,
167 * revision number , ... this info cannot go in struct asus_hotk because it is
168 * available before the hotk
169 */
170static struct acpi_table_header *asus_info;
171
172/* The actual device the driver binds to */
173static struct asus_hotk *hotk;
174
175/*
176 * The hotkey driver declaration
177 */
178static int asus_hotk_add(struct acpi_device *device);
179static int asus_hotk_remove(struct acpi_device *device, int type);
180static struct acpi_driver asus_hotk_driver = {
181 .name = ASUS_HOTK_NAME,
182 .class = ASUS_HOTK_CLASS,
183 .ids = ASUS_HOTK_HID,
184 .ops = {
185 .add = asus_hotk_add,
186 .remove = asus_hotk_remove,
187 },
188};
189
6b7091e7
CC
190/* The backlight device /sys/class/backlight */
191static struct backlight_device *asus_backlight_device;
192
193/*
194 * The backlight class declaration
195 */
196static int read_brightness(struct backlight_device *bd);
197static int update_bl_status(struct backlight_device *bd);
599a52d1 198static struct backlight_ops asusbl_ops = {
8def05fa
LB
199 .get_brightness = read_brightness,
200 .update_status = update_bl_status,
6b7091e7
CC
201};
202
be18cdab
CC
203/* These functions actually update the LED's, and are called from a
204 * workqueue. By doing this as separate work rather than when the LED
205 * subsystem asks, we avoid messing with the Asus ACPI stuff during a
206 * potentially bad time, such as a timer interrupt. */
207static struct workqueue_struct *led_workqueue;
208
209#define ASUS_LED(object, ledname) \
210 static void object##_led_set(struct led_classdev *led_cdev, \
211 enum led_brightness value); \
212 static void object##_led_update(struct work_struct *ignored); \
213 static int object##_led_wk; \
f110ef58 214 static DECLARE_WORK(object##_led_work, object##_led_update); \
be18cdab
CC
215 static struct led_classdev object##_led = { \
216 .name = "asus:" ledname, \
217 .brightness_set = object##_led_set, \
218 }
219
220ASUS_LED(mled, "mail");
221ASUS_LED(tled, "touchpad");
222ASUS_LED(rled, "record");
223ASUS_LED(pled, "phone");
224
85091b71
CC
225/*
226 * This function evaluates an ACPI method, given an int as parameter, the
227 * method is searched within the scope of the handle, can be NULL. The output
228 * of the method is written is output, which can also be NULL
229 *
230 * returns 1 if write is successful, 0 else.
231 */
232static int write_acpi_int(acpi_handle handle, const char *method, int val,
233 struct acpi_buffer *output)
234{
235 struct acpi_object_list params; //list of input parameters (an int here)
236 union acpi_object in_obj; //the only param we use
237 acpi_status status;
238
239 params.count = 1;
240 params.pointer = &in_obj;
241 in_obj.type = ACPI_TYPE_INTEGER;
242 in_obj.integer.value = val;
243
244 status = acpi_evaluate_object(handle, (char *)method, &params, output);
245 return (status == AE_OK);
246}
247
248static int read_acpi_int(acpi_handle handle, const char *method, int *val,
249 struct acpi_object_list *params)
250{
251 struct acpi_buffer output;
252 union acpi_object out_obj;
253 acpi_status status;
254
255 output.length = sizeof(out_obj);
256 output.pointer = &out_obj;
257
258 status = acpi_evaluate_object(handle, (char *)method, params, &output);
259 *val = out_obj.integer.value;
260 return (status == AE_OK) && (out_obj.type == ACPI_TYPE_INTEGER);
261}
262
8def05fa
LB
263static int read_wireless_status(int mask)
264{
4564de17
CC
265 int status;
266
267 if (!wireless_status_handle)
268 return (hotk->status & mask) ? 1 : 0;
269
270 if (read_acpi_int(wireless_status_handle, NULL, &status, NULL)) {
271 return (status & mask) ? 1 : 0;
272 } else
273 printk(ASUS_WARNING "Error reading Wireless status\n");
274
275 return (hotk->status & mask) ? 1 : 0;
276}
277
be18cdab
CC
278/* Generic LED functions */
279static int read_status(int mask)
280{
4564de17
CC
281 /* There is a special method for both wireless devices */
282 if (mask == BT_ON || mask == WL_ON)
283 return read_wireless_status(mask);
284
be18cdab
CC
285 return (hotk->status & mask) ? 1 : 0;
286}
287
8def05fa 288static void write_status(acpi_handle handle, int out, int mask, int invert)
be18cdab
CC
289{
290 hotk->status = (out) ? (hotk->status | mask) : (hotk->status & ~mask);
291
292 if (invert) /* invert target value */
293 out = !out & 0x1;
294
295 if (handle && !write_acpi_int(handle, NULL, out, NULL))
296 printk(ASUS_WARNING " write failed\n");
297}
298
299/* /sys/class/led handlers */
300#define ASUS_LED_HANDLER(object, mask, invert) \
301 static void object##_led_set(struct led_classdev *led_cdev, \
302 enum led_brightness value) \
303 { \
304 object##_led_wk = value; \
305 queue_work(led_workqueue, &object##_led_work); \
306 } \
307 static void object##_led_update(struct work_struct *ignored) \
308 { \
309 int value = object##_led_wk; \
310 write_status(object##_set_handle, value, (mask), (invert)); \
311 }
312
313ASUS_LED_HANDLER(mled, MLED_ON, 1);
314ASUS_LED_HANDLER(pled, PLED_ON, 0);
315ASUS_LED_HANDLER(rled, RLED_ON, 0);
316ASUS_LED_HANDLER(tled, TLED_ON, 0);
317
6b7091e7
CC
318static int get_lcd_state(void)
319{
320 return read_status(LCD_ON);
321}
322
323static int set_lcd_state(int value)
324{
325 int lcd = 0;
326 acpi_status status = 0;
327
328 lcd = value ? 1 : 0;
329
330 if (lcd == get_lcd_state())
331 return 0;
332
8def05fa 333 if (lcd_switch_handle) {
6b7091e7
CC
334 status = acpi_evaluate_object(lcd_switch_handle,
335 NULL, NULL, NULL);
336
337 if (ACPI_FAILURE(status))
338 printk(ASUS_WARNING "Error switching LCD\n");
339 }
340
341 write_status(NULL, lcd, LCD_ON, 0);
342 return 0;
343}
344
345static void lcd_blank(int blank)
346{
347 struct backlight_device *bd = asus_backlight_device;
348
8def05fa 349 if (bd) {
599a52d1 350 bd->props.power = blank;
28ee086d 351 backlight_update_status(bd);
6b7091e7
CC
352 }
353}
354
355static int read_brightness(struct backlight_device *bd)
356{
357 int value;
358
359 if (!read_acpi_int(brightness_get_handle, NULL, &value, NULL))
360 printk(ASUS_WARNING "Error reading brightness\n");
361
362 return value;
363}
364
365static int set_brightness(struct backlight_device *bd, int value)
366{
367 int ret = 0;
368
369 value = (0 < value) ? ((15 < value) ? 15 : value) : 0;
370 /* 0 <= value <= 15 */
371
372 if (!write_acpi_int(brightness_set_handle, NULL, value, NULL)) {
373 printk(ASUS_WARNING "Error changing brightness\n");
374 ret = -EIO;
375 }
376
377 return ret;
378}
379
380static int update_bl_status(struct backlight_device *bd)
381{
382 int rv;
599a52d1 383 int value = bd->props.brightness;
6b7091e7
CC
384
385 rv = set_brightness(bd, value);
8def05fa 386 if (rv)
6b7091e7
CC
387 return rv;
388
599a52d1 389 value = (bd->props.power == FB_BLANK_UNBLANK) ? 1 : 0;
6b7091e7
CC
390 return set_lcd_state(value);
391}
392
85091b71
CC
393/*
394 * Platform device handlers
395 */
396
397/*
398 * We write our info in page, we begin at offset off and cannot write more
399 * than count bytes. We set eof to 1 if we handle those 2 values. We return the
400 * number of bytes written in page
401 */
402static ssize_t show_infos(struct device *dev,
8def05fa 403 struct device_attribute *attr, char *page)
85091b71
CC
404{
405 int len = 0;
406 int temp;
407 char buf[16]; //enough for all info
408 /*
409 * We use the easy way, we don't care of off and count, so we don't set eof
410 * to 1
411 */
412
413 len += sprintf(page, ASUS_HOTK_NAME " " ASUS_LAPTOP_VERSION "\n");
414 len += sprintf(page + len, "Model reference : %s\n", hotk->name);
415 /*
416 * The SFUN method probably allows the original driver to get the list
417 * of features supported by a given model. For now, 0x0100 or 0x0800
418 * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card.
419 * The significance of others is yet to be found.
420 */
421 if (read_acpi_int(hotk->handle, "SFUN", &temp, NULL))
422 len +=
423 sprintf(page + len, "SFUN value : 0x%04x\n", temp);
424 /*
425 * Another value for userspace: the ASYM method returns 0x02 for
426 * battery low and 0x04 for battery critical, its readings tend to be
427 * more accurate than those provided by _BST.
428 * Note: since not all the laptops provide this method, errors are
429 * silently ignored.
430 */
431 if (read_acpi_int(hotk->handle, "ASYM", &temp, NULL))
432 len +=
433 sprintf(page + len, "ASYM value : 0x%04x\n", temp);
434 if (asus_info) {
435 snprintf(buf, 16, "%d", asus_info->length);
436 len += sprintf(page + len, "DSDT length : %s\n", buf);
437 snprintf(buf, 16, "%d", asus_info->checksum);
438 len += sprintf(page + len, "DSDT checksum : %s\n", buf);
439 snprintf(buf, 16, "%d", asus_info->revision);
440 len += sprintf(page + len, "DSDT revision : %s\n", buf);
441 snprintf(buf, 7, "%s", asus_info->oem_id);
442 len += sprintf(page + len, "OEM id : %s\n", buf);
443 snprintf(buf, 9, "%s", asus_info->oem_table_id);
444 len += sprintf(page + len, "OEM table id : %s\n", buf);
445 snprintf(buf, 16, "%x", asus_info->oem_revision);
446 len += sprintf(page + len, "OEM revision : 0x%s\n", buf);
447 snprintf(buf, 5, "%s", asus_info->asl_compiler_id);
448 len += sprintf(page + len, "ASL comp vendor id : %s\n", buf);
449 snprintf(buf, 16, "%x", asus_info->asl_compiler_revision);
450 len += sprintf(page + len, "ASL comp revision : 0x%s\n", buf);
451 }
452
453 return len;
454}
455
456static int parse_arg(const char *buf, unsigned long count, int *val)
457{
458 if (!count)
459 return 0;
460 if (count > 31)
461 return -EINVAL;
462 if (sscanf(buf, "%i", val) != 1)
463 return -EINVAL;
464 return count;
465}
466
4564de17
CC
467static ssize_t store_status(const char *buf, size_t count,
468 acpi_handle handle, int mask, int invert)
469{
470 int rv, value;
471 int out = 0;
472
473 rv = parse_arg(buf, count, &value);
474 if (rv > 0)
475 out = value ? 1 : 0;
476
477 write_status(handle, out, mask, invert);
478
479 return rv;
480}
481
722ad971
CC
482/*
483 * LEDD display
484 */
485static ssize_t show_ledd(struct device *dev,
486 struct device_attribute *attr, char *buf)
487{
488 return sprintf(buf, "0x%08x\n", hotk->ledd_status);
489}
490
491static ssize_t store_ledd(struct device *dev, struct device_attribute *attr,
492 const char *buf, size_t count)
493{
494 int rv, value;
495
496 rv = parse_arg(buf, count, &value);
497 if (rv > 0) {
498 if (!write_acpi_int(ledd_set_handle, NULL, value, NULL))
499 printk(ASUS_WARNING "LED display write failed\n");
500 else
501 hotk->ledd_status = (u32) value;
502 }
503 return rv;
504}
505
4564de17
CC
506/*
507 * WLAN
508 */
509static ssize_t show_wlan(struct device *dev,
510 struct device_attribute *attr, char *buf)
511{
512 return sprintf(buf, "%d\n", read_status(WL_ON));
513}
514
515static ssize_t store_wlan(struct device *dev, struct device_attribute *attr,
516 const char *buf, size_t count)
517{
518 return store_status(buf, count, wl_switch_handle, WL_ON, 0);
519}
520
521/*
522 * Bluetooth
523 */
524static ssize_t show_bluetooth(struct device *dev,
525 struct device_attribute *attr, char *buf)
526{
527 return sprintf(buf, "%d\n", read_status(BT_ON));
528}
529
8def05fa
LB
530static ssize_t store_bluetooth(struct device *dev,
531 struct device_attribute *attr, const char *buf,
532 size_t count)
4564de17
CC
533{
534 return store_status(buf, count, bt_switch_handle, BT_ON, 0);
535}
536
78127b4a
CC
537/*
538 * Display
539 */
540static void set_display(int value)
541{
542 /* no sanity check needed for now */
543 if (!write_acpi_int(display_set_handle, NULL, value, NULL))
544 printk(ASUS_WARNING "Error setting display\n");
545 return;
546}
547
548static int read_display(void)
549{
550 int value = 0;
551
552 /* In most of the case, we know how to set the display, but sometime
553 we can't read it */
8def05fa 554 if (display_get_handle) {
78127b4a
CC
555 if (!read_acpi_int(display_get_handle, NULL, &value, NULL))
556 printk(ASUS_WARNING "Error reading display status\n");
557 }
558
559 value &= 0x0F; /* needed for some models, shouldn't hurt others */
560
561 return value;
562}
8def05fa 563
78127b4a
CC
564/*
565 * Now, *this* one could be more user-friendly, but so far, no-one has
566 * complained. The significance of bits is the same as in store_disp()
567 */
568static ssize_t show_disp(struct device *dev,
569 struct device_attribute *attr, char *buf)
570{
571 return sprintf(buf, "%d\n", read_display());
572}
573
574/*
575 * Experimental support for display switching. As of now: 1 should activate
576 * the LCD output, 2 should do for CRT, 4 for TV-Out and 8 for DVI.
577 * Any combination (bitwise) of these will suffice. I never actually tested 4
578 * displays hooked up simultaneously, so be warned. See the acpi4asus README
579 * for more info.
580 */
581static ssize_t store_disp(struct device *dev, struct device_attribute *attr,
582 const char *buf, size_t count)
583{
584 int rv, value;
585
586 rv = parse_arg(buf, count, &value);
587 if (rv > 0)
588 set_display(value);
589 return rv;
590}
591
8b857353
CC
592/*
593 * Light Sens
594 */
595static void set_light_sens_switch(int value)
596{
597 if (!write_acpi_int(ls_switch_handle, NULL, value, NULL))
598 printk(ASUS_WARNING "Error setting light sensor switch\n");
599 hotk->light_switch = value;
600}
601
602static ssize_t show_lssw(struct device *dev,
603 struct device_attribute *attr, char *buf)
604{
605 return sprintf(buf, "%d\n", hotk->light_switch);
606}
607
608static ssize_t store_lssw(struct device *dev, struct device_attribute *attr,
609 const char *buf, size_t count)
610{
611 int rv, value;
612
613 rv = parse_arg(buf, count, &value);
614 if (rv > 0)
615 set_light_sens_switch(value ? 1 : 0);
616
617 return rv;
618}
619
620static void set_light_sens_level(int value)
621{
622 if (!write_acpi_int(ls_level_handle, NULL, value, NULL))
623 printk(ASUS_WARNING "Error setting light sensor level\n");
624 hotk->light_level = value;
625}
626
627static ssize_t show_lslvl(struct device *dev,
628 struct device_attribute *attr, char *buf)
629{
630 return sprintf(buf, "%d\n", hotk->light_level);
631}
632
633static ssize_t store_lslvl(struct device *dev, struct device_attribute *attr,
634 const char *buf, size_t count)
635{
636 int rv, value;
637
638 rv = parse_arg(buf, count, &value);
639 if (rv > 0) {
640 value = (0 < value) ? ((15 < value) ? 15 : value) : 0;
641 /* 0 <= value <= 15 */
642 set_light_sens_level(value);
643 }
644
645 return rv;
646}
647
85091b71
CC
648static void asus_hotk_notify(acpi_handle handle, u32 event, void *data)
649{
650 /* TODO Find a better way to handle events count. */
651 if (!hotk)
652 return;
653
6b7091e7
CC
654 /*
655 * We need to tell the backlight device when the backlight power is
656 * switched
657 */
658 if (event == ATKD_LCD_ON) {
659 write_status(NULL, 1, LCD_ON, 0);
660 lcd_blank(FB_BLANK_UNBLANK);
8def05fa 661 } else if (event == ATKD_LCD_OFF) {
6b7091e7
CC
662 write_status(NULL, 0, LCD_ON, 0);
663 lcd_blank(FB_BLANK_POWERDOWN);
664 }
665
85091b71
CC
666 acpi_bus_generate_event(hotk->device, event,
667 hotk->event_count[event % 128]++);
668
669 return;
670}
671
672#define ASUS_CREATE_DEVICE_ATTR(_name) \
673 struct device_attribute dev_attr_##_name = { \
674 .attr = { \
675 .name = __stringify(_name), \
676 .mode = 0, \
677 .owner = THIS_MODULE }, \
678 .show = NULL, \
679 .store = NULL, \
680 }
681
682#define ASUS_SET_DEVICE_ATTR(_name, _mode, _show, _store) \
683 do { \
684 dev_attr_##_name.attr.mode = _mode; \
685 dev_attr_##_name.show = _show; \
686 dev_attr_##_name.store = _store; \
687 } while(0)
688
689static ASUS_CREATE_DEVICE_ATTR(infos);
4564de17
CC
690static ASUS_CREATE_DEVICE_ATTR(wlan);
691static ASUS_CREATE_DEVICE_ATTR(bluetooth);
78127b4a 692static ASUS_CREATE_DEVICE_ATTR(display);
722ad971 693static ASUS_CREATE_DEVICE_ATTR(ledd);
8b857353
CC
694static ASUS_CREATE_DEVICE_ATTR(ls_switch);
695static ASUS_CREATE_DEVICE_ATTR(ls_level);
85091b71
CC
696
697static struct attribute *asuspf_attributes[] = {
8def05fa
LB
698 &dev_attr_infos.attr,
699 &dev_attr_wlan.attr,
700 &dev_attr_bluetooth.attr,
701 &dev_attr_display.attr,
702 &dev_attr_ledd.attr,
703 &dev_attr_ls_switch.attr,
704 &dev_attr_ls_level.attr,
705 NULL
85091b71
CC
706};
707
708static struct attribute_group asuspf_attribute_group = {
8def05fa 709 .attrs = asuspf_attributes
85091b71
CC
710};
711
712static struct platform_driver asuspf_driver = {
8def05fa
LB
713 .driver = {
714 .name = ASUS_HOTK_FILE,
715 .owner = THIS_MODULE,
716 }
85091b71
CC
717};
718
719static struct platform_device *asuspf_device;
720
85091b71
CC
721static void asus_hotk_add_fs(void)
722{
723 ASUS_SET_DEVICE_ATTR(infos, 0444, show_infos, NULL);
4564de17
CC
724
725 if (wl_switch_handle)
726 ASUS_SET_DEVICE_ATTR(wlan, 0644, show_wlan, store_wlan);
727
728 if (bt_switch_handle)
729 ASUS_SET_DEVICE_ATTR(bluetooth, 0644,
730 show_bluetooth, store_bluetooth);
78127b4a
CC
731
732 if (display_set_handle && display_get_handle)
733 ASUS_SET_DEVICE_ATTR(display, 0644, show_disp, store_disp);
8def05fa 734 else if (display_set_handle)
78127b4a
CC
735 ASUS_SET_DEVICE_ATTR(display, 0200, NULL, store_disp);
736
722ad971
CC
737 if (ledd_set_handle)
738 ASUS_SET_DEVICE_ATTR(ledd, 0644, show_ledd, store_ledd);
739
8b857353
CC
740 if (ls_switch_handle && ls_level_handle) {
741 ASUS_SET_DEVICE_ATTR(ls_level, 0644, show_lslvl, store_lslvl);
742 ASUS_SET_DEVICE_ATTR(ls_switch, 0644, show_lssw, store_lssw);
743 }
85091b71
CC
744}
745
8def05fa 746static int asus_handle_init(char *name, acpi_handle * handle,
85091b71
CC
747 char **paths, int num_paths)
748{
749 int i;
750 acpi_status status;
751
752 for (i = 0; i < num_paths; i++) {
753 status = acpi_get_handle(NULL, paths[i], handle);
754 if (ACPI_SUCCESS(status))
755 return 0;
756 }
757
758 *handle = NULL;
759 return -ENODEV;
760}
761
762#define ASUS_HANDLE_INIT(object) \
763 asus_handle_init(#object, &object##_handle, object##_paths, \
764 ARRAY_SIZE(object##_paths))
765
85091b71
CC
766/*
767 * This function is used to initialize the hotk with right values. In this
768 * method, we can make all the detection we want, and modify the hotk struct
769 */
770static int asus_hotk_get_info(void)
771{
772 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
85091b71 773 union acpi_object *model = NULL;
4564de17 774 int bsts_result, hwrs_result;
85091b71
CC
775 char *string = NULL;
776 acpi_status status;
777
778 /*
779 * Get DSDT headers early enough to allow for differentiating between
780 * models, but late enough to allow acpi_bus_register_driver() to fail
781 * before doing anything ACPI-specific. Should we encounter a machine,
782 * which needs special handling (i.e. its hotkey device has a different
783 * HID), this bit will be moved. A global variable asus_info contains
784 * the DSDT header.
785 */
894d79be 786 status = acpi_get_table(ACPI_SIG_DSDT, 1, &asus_info);
85091b71
CC
787 if (ACPI_FAILURE(status))
788 printk(ASUS_WARNING "Couldn't get the DSDT table header\n");
85091b71
CC
789
790 /* We have to write 0 on init this far for all ASUS models */
791 if (!write_acpi_int(hotk->handle, "INIT", 0, &buffer)) {
792 printk(ASUS_ERR "Hotkey initialization failed\n");
793 return -ENODEV;
794 }
795
796 /* This needs to be called for some laptops to init properly */
797 if (!read_acpi_int(hotk->handle, "BSTS", &bsts_result, NULL))
798 printk(ASUS_WARNING "Error calling BSTS\n");
799 else if (bsts_result)
800 printk(ASUS_NOTICE "BSTS called, 0x%02x returned\n",
801 bsts_result);
802
803 /*
804 * Try to match the object returned by INIT to the specific model.
805 * Handle every possible object (or the lack of thereof) the DSDT
806 * writers might throw at us. When in trouble, we pass NULL to
807 * asus_model_match() and try something completely different.
808 */
809 if (buffer.pointer) {
810 model = buffer.pointer;
811 switch (model->type) {
812 case ACPI_TYPE_STRING:
813 string = model->string.pointer;
814 break;
815 case ACPI_TYPE_BUFFER:
816 string = model->buffer.pointer;
817 break;
818 default:
819 string = "";
820 break;
821 }
822 }
823 hotk->name = kstrdup(string, GFP_KERNEL);
824 if (!hotk->name)
825 return -ENOMEM;
826
8def05fa 827 if (*string)
85091b71
CC
828 printk(ASUS_NOTICE " %s model detected\n", string);
829
be18cdab
CC
830 ASUS_HANDLE_INIT(mled_set);
831 ASUS_HANDLE_INIT(tled_set);
832 ASUS_HANDLE_INIT(rled_set);
833 ASUS_HANDLE_INIT(pled_set);
834
722ad971
CC
835 ASUS_HANDLE_INIT(ledd_set);
836
4564de17
CC
837 /*
838 * The HWRS method return informations about the hardware.
839 * 0x80 bit is for WLAN, 0x100 for Bluetooth.
840 * The significance of others is yet to be found.
841 * If we don't find the method, we assume the device are present.
842 */
843 if (!read_acpi_int(hotk->handle, "HRWS", &hwrs_result, NULL))
844 hwrs_result = WL_HWRS | BT_HWRS;
845
8def05fa 846 if (hwrs_result & WL_HWRS)
4564de17 847 ASUS_HANDLE_INIT(wl_switch);
8def05fa 848 if (hwrs_result & BT_HWRS)
4564de17
CC
849 ASUS_HANDLE_INIT(bt_switch);
850
851 ASUS_HANDLE_INIT(wireless_status);
852
6b7091e7
CC
853 ASUS_HANDLE_INIT(brightness_set);
854 ASUS_HANDLE_INIT(brightness_get);
855
856 ASUS_HANDLE_INIT(lcd_switch);
857
78127b4a
CC
858 ASUS_HANDLE_INIT(display_set);
859 ASUS_HANDLE_INIT(display_get);
860
8b857353
CC
861 /* There is a lot of models with "ALSL", but a few get
862 a real light sens, so we need to check it. */
8def05fa 863 if (ASUS_HANDLE_INIT(ls_switch))
8b857353
CC
864 ASUS_HANDLE_INIT(ls_level);
865
85091b71
CC
866 kfree(model);
867
868 return AE_OK;
869}
870
871static int asus_hotk_check(void)
872{
873 int result = 0;
874
875 result = acpi_bus_get_status(hotk->device);
876 if (result)
877 return result;
878
879 if (hotk->device->status.present) {
880 result = asus_hotk_get_info();
881 } else {
882 printk(ASUS_ERR "Hotkey device not present, aborting\n");
883 return -EINVAL;
884 }
885
886 return result;
887}
888
889static int asus_hotk_found;
890
891static int asus_hotk_add(struct acpi_device *device)
892{
893 acpi_status status = AE_OK;
894 int result;
895
896 if (!device)
897 return -EINVAL;
898
899 printk(ASUS_NOTICE "Asus Laptop Support version %s\n",
900 ASUS_LAPTOP_VERSION);
901
902 hotk = kmalloc(sizeof(struct asus_hotk), GFP_KERNEL);
903 if (!hotk)
904 return -ENOMEM;
905 memset(hotk, 0, sizeof(struct asus_hotk));
906
907 hotk->handle = device->handle;
908 strcpy(acpi_device_name(device), ASUS_HOTK_DEVICE_NAME);
909 strcpy(acpi_device_class(device), ASUS_HOTK_CLASS);
910 acpi_driver_data(device) = hotk;
911 hotk->device = device;
912
913 result = asus_hotk_check();
914 if (result)
915 goto end;
916
917 asus_hotk_add_fs();
918
919 /*
920 * We install the handler, it will receive the hotk in parameter, so, we
921 * could add other data to the hotk struct
922 */
923 status = acpi_install_notify_handler(hotk->handle, ACPI_SYSTEM_NOTIFY,
924 asus_hotk_notify, hotk);
925 if (ACPI_FAILURE(status))
926 printk(ASUS_ERR "Error installing notify handler\n");
927
928 asus_hotk_found = 1;
929
6b7091e7 930 /* WLED and BLED are on by default */
4564de17
CC
931 write_status(bt_switch_handle, 1, BT_ON, 0);
932 write_status(wl_switch_handle, 1, WL_ON, 0);
933
6b7091e7
CC
934 /* LCD Backlight is on by default */
935 write_status(NULL, 1, LCD_ON, 0);
936
722ad971
CC
937 /* LED display is off by default */
938 hotk->ledd_status = 0xFFF;
939
8def05fa
LB
940 /* Set initial values of light sensor and level */
941 hotk->light_switch = 1; /* Default to light sensor disabled */
942 hotk->light_level = 0; /* level 5 for sensor sensitivity */
8b857353 943
8def05fa
LB
944 if (ls_switch_handle)
945 set_light_sens_switch(hotk->light_switch);
8b857353 946
8def05fa
LB
947 if (ls_level_handle)
948 set_light_sens_level(hotk->light_level);
8b857353 949
85091b71
CC
950 end:
951 if (result) {
952 kfree(hotk->name);
953 kfree(hotk);
954 }
955
956 return result;
957}
958
959static int asus_hotk_remove(struct acpi_device *device, int type)
960{
961 acpi_status status = 0;
962
963 if (!device || !acpi_driver_data(device))
964 return -EINVAL;
965
966 status = acpi_remove_notify_handler(hotk->handle, ACPI_SYSTEM_NOTIFY,
967 asus_hotk_notify);
968 if (ACPI_FAILURE(status))
969 printk(ASUS_ERR "Error removing notify handler\n");
970
971 kfree(hotk->name);
972 kfree(hotk);
973
974 return 0;
975}
976
6b7091e7
CC
977static void asus_backlight_exit(void)
978{
8def05fa 979 if (asus_backlight_device)
6b7091e7
CC
980 backlight_device_unregister(asus_backlight_device);
981}
982
be18cdab
CC
983#define ASUS_LED_UNREGISTER(object) \
984 if(object##_led.class_dev \
985 && !IS_ERR(object##_led.class_dev)) \
986 led_classdev_unregister(&object##_led)
987
988static void asus_led_exit(void)
989{
990 ASUS_LED_UNREGISTER(mled);
991 ASUS_LED_UNREGISTER(tled);
992 ASUS_LED_UNREGISTER(pled);
993 ASUS_LED_UNREGISTER(rled);
994
995 destroy_workqueue(led_workqueue);
996}
997
85091b71
CC
998static void __exit asus_laptop_exit(void)
999{
6b7091e7 1000 asus_backlight_exit();
be18cdab
CC
1001 asus_led_exit();
1002
85091b71 1003 acpi_bus_unregister_driver(&asus_hotk_driver);
8def05fa
LB
1004 sysfs_remove_group(&asuspf_device->dev.kobj, &asuspf_attribute_group);
1005 platform_device_unregister(asuspf_device);
1006 platform_driver_unregister(&asuspf_driver);
85091b71
CC
1007}
1008
8def05fa 1009static int asus_backlight_init(struct device *dev)
6b7091e7
CC
1010{
1011 struct backlight_device *bd;
1012
8def05fa
LB
1013 if (brightness_set_handle && lcd_switch_handle) {
1014 bd = backlight_device_register(ASUS_HOTK_FILE, dev,
599a52d1 1015 NULL, &asusbl_ops);
8def05fa 1016 if (IS_ERR(bd)) {
6b7091e7
CC
1017 printk(ASUS_ERR
1018 "Could not register asus backlight device\n");
1019 asus_backlight_device = NULL;
1020 return PTR_ERR(bd);
1021 }
1022
1023 asus_backlight_device = bd;
1024
599a52d1
RP
1025 bd->props.max_brightness = 15;
1026 bd->props.brightness = read_brightness(NULL);
1027 bd->props.power = FB_BLANK_UNBLANK;
28ee086d 1028 backlight_update_status(bd);
6b7091e7
CC
1029 }
1030 return 0;
1031}
1032
be18cdab 1033static int asus_led_register(acpi_handle handle,
8def05fa 1034 struct led_classdev *ldev, struct device *dev)
be18cdab 1035{
8def05fa 1036 if (!handle)
be18cdab
CC
1037 return 0;
1038
1039 return led_classdev_register(dev, ldev);
1040}
8def05fa 1041
be18cdab
CC
1042#define ASUS_LED_REGISTER(object, device) \
1043 asus_led_register(object##_set_handle, &object##_led, device)
1044
8def05fa 1045static int asus_led_init(struct device *dev)
be18cdab
CC
1046{
1047 int rv;
1048
1049 rv = ASUS_LED_REGISTER(mled, dev);
8def05fa 1050 if (rv)
be18cdab
CC
1051 return rv;
1052
1053 rv = ASUS_LED_REGISTER(tled, dev);
8def05fa 1054 if (rv)
be18cdab
CC
1055 return rv;
1056
1057 rv = ASUS_LED_REGISTER(rled, dev);
8def05fa 1058 if (rv)
be18cdab
CC
1059 return rv;
1060
1061 rv = ASUS_LED_REGISTER(pled, dev);
8def05fa 1062 if (rv)
be18cdab
CC
1063 return rv;
1064
1065 led_workqueue = create_singlethread_workqueue("led_workqueue");
8def05fa 1066 if (!led_workqueue)
be18cdab
CC
1067 return -ENOMEM;
1068
1069 return 0;
1070}
1071
85091b71
CC
1072static int __init asus_laptop_init(void)
1073{
be18cdab 1074 struct device *dev;
85091b71
CC
1075 int result;
1076
1077 if (acpi_disabled)
1078 return -ENODEV;
1079
85091b71
CC
1080 result = acpi_bus_register_driver(&asus_hotk_driver);
1081 if (result < 0)
1082 return result;
1083
1084 /*
1085 * This is a bit of a kludge. We only want this module loaded
1086 * for ASUS systems, but there's currently no way to probe the
1087 * ACPI namespace for ASUS HIDs. So we just return failure if
1088 * we didn't find one, which will cause the module to be
1089 * unloaded.
1090 */
1091 if (!asus_hotk_found) {
1092 acpi_bus_unregister_driver(&asus_hotk_driver);
1093 return -ENODEV;
1094 }
1095
be18cdab
CC
1096 dev = acpi_get_physical_device(hotk->device->handle);
1097
6b7091e7 1098 result = asus_backlight_init(dev);
8def05fa 1099 if (result)
6b7091e7
CC
1100 goto fail_backlight;
1101
be18cdab 1102 result = asus_led_init(dev);
8def05fa 1103 if (result)
be18cdab
CC
1104 goto fail_led;
1105
8def05fa 1106 /* Register platform stuff */
85091b71 1107 result = platform_driver_register(&asuspf_driver);
8def05fa
LB
1108 if (result)
1109 goto fail_platform_driver;
85091b71 1110
8def05fa
LB
1111 asuspf_device = platform_device_alloc(ASUS_HOTK_FILE, -1);
1112 if (!asuspf_device) {
1113 result = -ENOMEM;
1114 goto fail_platform_device1;
1115 }
85091b71 1116
8def05fa
LB
1117 result = platform_device_add(asuspf_device);
1118 if (result)
1119 goto fail_platform_device2;
85091b71 1120
8def05fa 1121 result = sysfs_create_group(&asuspf_device->dev.kobj,
85091b71 1122 &asuspf_attribute_group);
8def05fa
LB
1123 if (result)
1124 goto fail_sysfs;
85091b71 1125
8def05fa 1126 return 0;
85091b71 1127
8def05fa
LB
1128 fail_sysfs:
1129 platform_device_del(asuspf_device);
85091b71 1130
8def05fa 1131 fail_platform_device2:
85091b71
CC
1132 platform_device_put(asuspf_device);
1133
8def05fa
LB
1134 fail_platform_device1:
1135 platform_driver_unregister(&asuspf_driver);
85091b71 1136
8def05fa 1137 fail_platform_driver:
be18cdab
CC
1138 asus_led_exit();
1139
8def05fa 1140 fail_led:
6b7091e7
CC
1141 asus_backlight_exit();
1142
8def05fa 1143 fail_backlight:
85091b71
CC
1144
1145 return result;
1146}
1147
1148module_init(asus_laptop_init);
1149module_exit(asus_laptop_exit);
This page took 0.091605 seconds and 5 git commands to generate.