blob: d153871fd5ab241740a60e51090458aeeb3c7cde [file] [log] [blame]
Eric Coopere59f8792008-03-13 12:55:46 +01001/*
2 * eepc-laptop.c - Asus Eee PC extras
3 *
4 * Based on asus_acpi.c as patched for the Eee PC by Asus:
5 * ftp://ftp.asus.com/pub/ASUS/EeePC/701/ASUS_ACPI_071126.rar
6 * Based on eee.c from eeepc-linux
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
19#include <linux/kernel.h>
20#include <linux/module.h>
21#include <linux/init.h>
22#include <linux/types.h>
23#include <linux/platform_device.h>
Corentin Charya5fa4292008-03-13 12:56:37 +010024#include <linux/backlight.h>
25#include <linux/fb.h>
Corentin Charye1faa9d2008-03-13 12:57:18 +010026#include <linux/hwmon.h>
27#include <linux/hwmon-sysfs.h>
Eric Coopere59f8792008-03-13 12:55:46 +010028#include <acpi/acpi_drivers.h>
29#include <acpi/acpi_bus.h>
30#include <linux/uaccess.h>
Matthew Garretta195dcd2008-08-19 12:13:20 +010031#include <linux/input.h>
32#include <linux/rfkill.h>
Eric Coopere59f8792008-03-13 12:55:46 +010033
34#define EEEPC_LAPTOP_VERSION "0.1"
35
36#define EEEPC_HOTK_NAME "Eee PC Hotkey Driver"
37#define EEEPC_HOTK_FILE "eeepc"
38#define EEEPC_HOTK_CLASS "hotkey"
39#define EEEPC_HOTK_DEVICE_NAME "Hotkey"
40#define EEEPC_HOTK_HID "ASUS010"
41
42#define EEEPC_LOG EEEPC_HOTK_FILE ": "
43#define EEEPC_ERR KERN_ERR EEEPC_LOG
44#define EEEPC_WARNING KERN_WARNING EEEPC_LOG
45#define EEEPC_NOTICE KERN_NOTICE EEEPC_LOG
46#define EEEPC_INFO KERN_INFO EEEPC_LOG
47
48/*
49 * Definitions for Asus EeePC
50 */
51#define NOTIFY_WLAN_ON 0x10
Corentin Charya5fa4292008-03-13 12:56:37 +010052#define NOTIFY_BRN_MIN 0x20
53#define NOTIFY_BRN_MAX 0x2f
Eric Coopere59f8792008-03-13 12:55:46 +010054
55enum {
56 DISABLE_ASL_WLAN = 0x0001,
57 DISABLE_ASL_BLUETOOTH = 0x0002,
58 DISABLE_ASL_IRDA = 0x0004,
59 DISABLE_ASL_CAMERA = 0x0008,
60 DISABLE_ASL_TV = 0x0010,
61 DISABLE_ASL_GPS = 0x0020,
62 DISABLE_ASL_DISPLAYSWITCH = 0x0040,
63 DISABLE_ASL_MODEM = 0x0080,
64 DISABLE_ASL_CARDREADER = 0x0100
65};
66
67enum {
68 CM_ASL_WLAN = 0,
69 CM_ASL_BLUETOOTH,
70 CM_ASL_IRDA,
71 CM_ASL_1394,
72 CM_ASL_CAMERA,
73 CM_ASL_TV,
74 CM_ASL_GPS,
75 CM_ASL_DVDROM,
76 CM_ASL_DISPLAYSWITCH,
77 CM_ASL_PANELBRIGHT,
78 CM_ASL_BIOSFLASH,
79 CM_ASL_ACPIFLASH,
80 CM_ASL_CPUFV,
81 CM_ASL_CPUTEMPERATURE,
82 CM_ASL_FANCPU,
83 CM_ASL_FANCHASSIS,
84 CM_ASL_USBPORT1,
85 CM_ASL_USBPORT2,
86 CM_ASL_USBPORT3,
87 CM_ASL_MODEM,
88 CM_ASL_CARDREADER,
89 CM_ASL_LID
90};
91
Adrian Bunk14109462008-06-25 19:25:47 +030092static const char *cm_getv[] = {
Jonathan McDowell3af9bfc2008-12-03 20:31:11 +000093 "WLDG", "BTHG", NULL, NULL,
Eric Coopere59f8792008-03-13 12:55:46 +010094 "CAMG", NULL, NULL, NULL,
95 NULL, "PBLG", NULL, NULL,
96 "CFVG", NULL, NULL, NULL,
97 "USBG", NULL, NULL, "MODG",
98 "CRDG", "LIDG"
99};
100
Adrian Bunk14109462008-06-25 19:25:47 +0300101static const char *cm_setv[] = {
Jonathan McDowell3af9bfc2008-12-03 20:31:11 +0000102 "WLDS", "BTHS", NULL, NULL,
Eric Coopere59f8792008-03-13 12:55:46 +0100103 "CAMS", NULL, NULL, NULL,
104 "SDSP", "PBLS", "HDPS", NULL,
105 "CFVS", NULL, NULL, NULL,
106 "USBG", NULL, NULL, "MODS",
107 "CRDS", NULL
108};
109
Corentin Charye1faa9d2008-03-13 12:57:18 +0100110#define EEEPC_EC "\\_SB.PCI0.SBRG.EC0."
111
112#define EEEPC_EC_FAN_PWM EEEPC_EC "SC02" /* Fan PWM duty cycle (%) */
113#define EEEPC_EC_SC02 0x63
114#define EEEPC_EC_FAN_HRPM EEEPC_EC "SC05" /* High byte, fan speed (RPM) */
115#define EEEPC_EC_FAN_LRPM EEEPC_EC "SC06" /* Low byte, fan speed (RPM) */
116#define EEEPC_EC_FAN_CTRL EEEPC_EC "SFB3" /* Byte containing SF25 */
117#define EEEPC_EC_SFB3 0xD3
118
Eric Coopere59f8792008-03-13 12:55:46 +0100119/*
120 * This is the main structure, we can use it to store useful information
121 * about the hotk device
122 */
123struct eeepc_hotk {
124 struct acpi_device *device; /* the device we are in */
125 acpi_handle handle; /* the handle of the hotk device */
126 u32 cm_supported; /* the control methods supported
127 by this BIOS */
128 uint init_flag; /* Init flags */
129 u16 event_count[128]; /* count for each event */
Matthew Garretta195dcd2008-08-19 12:13:20 +0100130 struct input_dev *inputdev;
131 u16 *keycode_map;
132 struct rfkill *eeepc_wlan_rfkill;
133 struct rfkill *eeepc_bluetooth_rfkill;
Eric Coopere59f8792008-03-13 12:55:46 +0100134};
135
136/* The actual device the driver binds to */
137static struct eeepc_hotk *ehotk;
138
139/* Platform device/driver */
140static struct platform_driver platform_driver = {
141 .driver = {
142 .name = EEEPC_HOTK_FILE,
143 .owner = THIS_MODULE,
144 }
145};
146
147static struct platform_device *platform_device;
148
Matthew Garretta195dcd2008-08-19 12:13:20 +0100149struct key_entry {
150 char type;
151 u8 code;
152 u16 keycode;
153};
154
155enum { KE_KEY, KE_END };
156
157static struct key_entry eeepc_keymap[] = {
158 /* Sleep already handled via generic ACPI code */
159 {KE_KEY, 0x10, KEY_WLAN },
160 {KE_KEY, 0x12, KEY_PROG1 },
161 {KE_KEY, 0x13, KEY_MUTE },
162 {KE_KEY, 0x14, KEY_VOLUMEDOWN },
163 {KE_KEY, 0x15, KEY_VOLUMEUP },
Matthew Garrettb5f6f262009-01-20 16:17:46 +0100164 {KE_KEY, 0x1a, KEY_COFFEE },
165 {KE_KEY, 0x1b, KEY_ZOOM },
166 {KE_KEY, 0x1c, KEY_PROG2 },
167 {KE_KEY, 0x1d, KEY_PROG3 },
Matthew Garretta195dcd2008-08-19 12:13:20 +0100168 {KE_KEY, 0x30, KEY_SWITCHVIDEOMODE },
169 {KE_KEY, 0x31, KEY_SWITCHVIDEOMODE },
170 {KE_KEY, 0x32, KEY_SWITCHVIDEOMODE },
171 {KE_END, 0},
172};
173
Eric Coopere59f8792008-03-13 12:55:46 +0100174/*
175 * The hotkey driver declaration
176 */
177static int eeepc_hotk_add(struct acpi_device *device);
178static int eeepc_hotk_remove(struct acpi_device *device, int type);
179
180static const struct acpi_device_id eeepc_device_ids[] = {
181 {EEEPC_HOTK_HID, 0},
182 {"", 0},
183};
184MODULE_DEVICE_TABLE(acpi, eeepc_device_ids);
185
186static struct acpi_driver eeepc_hotk_driver = {
187 .name = EEEPC_HOTK_NAME,
188 .class = EEEPC_HOTK_CLASS,
189 .ids = eeepc_device_ids,
190 .ops = {
191 .add = eeepc_hotk_add,
192 .remove = eeepc_hotk_remove,
193 },
194};
195
Corentin Charya5fa4292008-03-13 12:56:37 +0100196/* The backlight device /sys/class/backlight */
197static struct backlight_device *eeepc_backlight_device;
198
Corentin Charye1faa9d2008-03-13 12:57:18 +0100199/* The hwmon device */
200static struct device *eeepc_hwmon_device;
201
Corentin Charya5fa4292008-03-13 12:56:37 +0100202/*
203 * The backlight class declaration
204 */
205static int read_brightness(struct backlight_device *bd);
206static int update_bl_status(struct backlight_device *bd);
207static struct backlight_ops eeepcbl_ops = {
208 .get_brightness = read_brightness,
209 .update_status = update_bl_status,
210};
211
Eric Coopere59f8792008-03-13 12:55:46 +0100212MODULE_AUTHOR("Corentin Chary, Eric Cooper");
213MODULE_DESCRIPTION(EEEPC_HOTK_NAME);
214MODULE_LICENSE("GPL");
215
216/*
217 * ACPI Helpers
218 */
219static int write_acpi_int(acpi_handle handle, const char *method, int val,
220 struct acpi_buffer *output)
221{
222 struct acpi_object_list params;
223 union acpi_object in_obj;
224 acpi_status status;
225
226 params.count = 1;
227 params.pointer = &in_obj;
228 in_obj.type = ACPI_TYPE_INTEGER;
229 in_obj.integer.value = val;
230
231 status = acpi_evaluate_object(handle, (char *)method, &params, output);
232 return (status == AE_OK ? 0 : -1);
233}
234
235static int read_acpi_int(acpi_handle handle, const char *method, int *val)
236{
237 acpi_status status;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400238 unsigned long long result;
Eric Coopere59f8792008-03-13 12:55:46 +0100239
240 status = acpi_evaluate_integer(handle, (char *)method, NULL, &result);
241 if (ACPI_FAILURE(status)) {
242 *val = -1;
243 return -1;
244 } else {
245 *val = result;
246 return 0;
247 }
248}
249
250static int set_acpi(int cm, int value)
251{
252 if (ehotk->cm_supported & (0x1 << cm)) {
253 const char *method = cm_setv[cm];
254 if (method == NULL)
255 return -ENODEV;
256 if (write_acpi_int(ehotk->handle, method, value, NULL))
257 printk(EEEPC_WARNING "Error writing %s\n", method);
258 }
259 return 0;
260}
261
262static int get_acpi(int cm)
263{
264 int value = -1;
265 if ((ehotk->cm_supported & (0x1 << cm))) {
266 const char *method = cm_getv[cm];
267 if (method == NULL)
268 return -ENODEV;
269 if (read_acpi_int(ehotk->handle, method, &value))
270 printk(EEEPC_WARNING "Error reading %s\n", method);
271 }
272 return value;
273}
274
275/*
Corentin Charya5fa4292008-03-13 12:56:37 +0100276 * Backlight
277 */
278static int read_brightness(struct backlight_device *bd)
279{
280 return get_acpi(CM_ASL_PANELBRIGHT);
281}
282
283static int set_brightness(struct backlight_device *bd, int value)
284{
285 value = max(0, min(15, value));
286 return set_acpi(CM_ASL_PANELBRIGHT, value);
287}
288
289static int update_bl_status(struct backlight_device *bd)
290{
291 return set_brightness(bd, bd->props.brightness);
292}
293
294/*
Matthew Garretta195dcd2008-08-19 12:13:20 +0100295 * Rfkill helpers
296 */
297
298static int eeepc_wlan_rfkill_set(void *data, enum rfkill_state state)
299{
300 if (state == RFKILL_STATE_SOFT_BLOCKED)
301 return set_acpi(CM_ASL_WLAN, 0);
302 else
303 return set_acpi(CM_ASL_WLAN, 1);
304}
305
306static int eeepc_wlan_rfkill_state(void *data, enum rfkill_state *state)
307{
308 if (get_acpi(CM_ASL_WLAN) == 1)
309 *state = RFKILL_STATE_UNBLOCKED;
310 else
311 *state = RFKILL_STATE_SOFT_BLOCKED;
312 return 0;
313}
314
315static int eeepc_bluetooth_rfkill_set(void *data, enum rfkill_state state)
316{
317 if (state == RFKILL_STATE_SOFT_BLOCKED)
318 return set_acpi(CM_ASL_BLUETOOTH, 0);
319 else
320 return set_acpi(CM_ASL_BLUETOOTH, 1);
321}
322
323static int eeepc_bluetooth_rfkill_state(void *data, enum rfkill_state *state)
324{
325 if (get_acpi(CM_ASL_BLUETOOTH) == 1)
326 *state = RFKILL_STATE_UNBLOCKED;
327 else
328 *state = RFKILL_STATE_SOFT_BLOCKED;
329 return 0;
330}
331
332/*
Eric Coopere59f8792008-03-13 12:55:46 +0100333 * Sys helpers
334 */
335static int parse_arg(const char *buf, unsigned long count, int *val)
336{
337 if (!count)
338 return 0;
339 if (sscanf(buf, "%i", val) != 1)
340 return -EINVAL;
341 return count;
342}
343
344static ssize_t store_sys_acpi(int cm, const char *buf, size_t count)
345{
346 int rv, value;
347
348 rv = parse_arg(buf, count, &value);
349 if (rv > 0)
350 set_acpi(cm, value);
351 return rv;
352}
353
354static ssize_t show_sys_acpi(int cm, char *buf)
355{
356 return sprintf(buf, "%d\n", get_acpi(cm));
357}
358
359#define EEEPC_CREATE_DEVICE_ATTR(_name, _cm) \
360 static ssize_t show_##_name(struct device *dev, \
361 struct device_attribute *attr, \
362 char *buf) \
363 { \
364 return show_sys_acpi(_cm, buf); \
365 } \
366 static ssize_t store_##_name(struct device *dev, \
367 struct device_attribute *attr, \
368 const char *buf, size_t count) \
369 { \
370 return store_sys_acpi(_cm, buf, count); \
371 } \
372 static struct device_attribute dev_attr_##_name = { \
373 .attr = { \
374 .name = __stringify(_name), \
375 .mode = 0644 }, \
376 .show = show_##_name, \
377 .store = store_##_name, \
378 }
379
380EEEPC_CREATE_DEVICE_ATTR(camera, CM_ASL_CAMERA);
381EEEPC_CREATE_DEVICE_ATTR(cardr, CM_ASL_CARDREADER);
382EEEPC_CREATE_DEVICE_ATTR(disp, CM_ASL_DISPLAYSWITCH);
Eric Coopere59f8792008-03-13 12:55:46 +0100383
384static struct attribute *platform_attributes[] = {
385 &dev_attr_camera.attr,
386 &dev_attr_cardr.attr,
387 &dev_attr_disp.attr,
Eric Coopere59f8792008-03-13 12:55:46 +0100388 NULL
389};
390
391static struct attribute_group platform_attribute_group = {
392 .attrs = platform_attributes
393};
394
395/*
396 * Hotkey functions
397 */
Matthew Garretta195dcd2008-08-19 12:13:20 +0100398static struct key_entry *eepc_get_entry_by_scancode(int code)
399{
400 struct key_entry *key;
401
402 for (key = eeepc_keymap; key->type != KE_END; key++)
403 if (code == key->code)
404 return key;
405
406 return NULL;
407}
408
409static struct key_entry *eepc_get_entry_by_keycode(int code)
410{
411 struct key_entry *key;
412
413 for (key = eeepc_keymap; key->type != KE_END; key++)
414 if (code == key->keycode && key->type == KE_KEY)
415 return key;
416
417 return NULL;
418}
419
420static int eeepc_getkeycode(struct input_dev *dev, int scancode, int *keycode)
421{
422 struct key_entry *key = eepc_get_entry_by_scancode(scancode);
423
424 if (key && key->type == KE_KEY) {
425 *keycode = key->keycode;
426 return 0;
427 }
428
429 return -EINVAL;
430}
431
432static int eeepc_setkeycode(struct input_dev *dev, int scancode, int keycode)
433{
434 struct key_entry *key;
435 int old_keycode;
436
437 if (keycode < 0 || keycode > KEY_MAX)
438 return -EINVAL;
439
440 key = eepc_get_entry_by_scancode(scancode);
441 if (key && key->type == KE_KEY) {
442 old_keycode = key->keycode;
443 key->keycode = keycode;
444 set_bit(keycode, dev->keybit);
445 if (!eepc_get_entry_by_keycode(old_keycode))
446 clear_bit(old_keycode, dev->keybit);
447 return 0;
448 }
449
450 return -EINVAL;
451}
452
Eric Coopere59f8792008-03-13 12:55:46 +0100453static int eeepc_hotk_check(void)
454{
Matthew Garretta195dcd2008-08-19 12:13:20 +0100455 const struct key_entry *key;
Eric Coopere59f8792008-03-13 12:55:46 +0100456 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
457 int result;
458
459 result = acpi_bus_get_status(ehotk->device);
460 if (result)
461 return result;
462 if (ehotk->device->status.present) {
463 if (write_acpi_int(ehotk->handle, "INIT", ehotk->init_flag,
464 &buffer)) {
465 printk(EEEPC_ERR "Hotkey initialization failed\n");
466 return -ENODEV;
467 } else {
468 printk(EEEPC_NOTICE "Hotkey init flags 0x%x\n",
469 ehotk->init_flag);
470 }
471 /* get control methods supported */
472 if (read_acpi_int(ehotk->handle, "CMSG"
473 , &ehotk->cm_supported)) {
474 printk(EEEPC_ERR
475 "Get control methods supported failed\n");
476 return -ENODEV;
477 } else {
478 printk(EEEPC_INFO
479 "Get control methods supported: 0x%x\n",
480 ehotk->cm_supported);
481 }
Matthew Garretta195dcd2008-08-19 12:13:20 +0100482 ehotk->inputdev = input_allocate_device();
483 if (!ehotk->inputdev) {
484 printk(EEEPC_INFO "Unable to allocate input device\n");
485 return 0;
486 }
487 ehotk->inputdev->name = "Asus EeePC extra buttons";
488 ehotk->inputdev->phys = EEEPC_HOTK_FILE "/input0";
489 ehotk->inputdev->id.bustype = BUS_HOST;
490 ehotk->inputdev->getkeycode = eeepc_getkeycode;
491 ehotk->inputdev->setkeycode = eeepc_setkeycode;
492
493 for (key = eeepc_keymap; key->type != KE_END; key++) {
494 switch (key->type) {
495 case KE_KEY:
496 set_bit(EV_KEY, ehotk->inputdev->evbit);
497 set_bit(key->keycode, ehotk->inputdev->keybit);
498 break;
499 }
500 }
501 result = input_register_device(ehotk->inputdev);
502 if (result) {
503 printk(EEEPC_INFO "Unable to register input device\n");
504 input_free_device(ehotk->inputdev);
505 return 0;
506 }
Eric Coopere59f8792008-03-13 12:55:46 +0100507 } else {
508 printk(EEEPC_ERR "Hotkey device not present, aborting\n");
509 return -EINVAL;
510 }
511 return 0;
512}
513
Corentin Charya5fa4292008-03-13 12:56:37 +0100514static void notify_brn(void)
515{
516 struct backlight_device *bd = eeepc_backlight_device;
517 bd->props.brightness = read_brightness(bd);
518}
519
Eric Coopere59f8792008-03-13 12:55:46 +0100520static void eeepc_hotk_notify(acpi_handle handle, u32 event, void *data)
521{
Matthew Garretta195dcd2008-08-19 12:13:20 +0100522 static struct key_entry *key;
Eric Coopere59f8792008-03-13 12:55:46 +0100523 if (!ehotk)
524 return;
Corentin Charya5fa4292008-03-13 12:56:37 +0100525 if (event >= NOTIFY_BRN_MIN && event <= NOTIFY_BRN_MAX)
526 notify_brn();
Eric Coopere59f8792008-03-13 12:55:46 +0100527 acpi_bus_generate_proc_event(ehotk->device, event,
528 ehotk->event_count[event % 128]++);
Matthew Garretta195dcd2008-08-19 12:13:20 +0100529 if (ehotk->inputdev) {
530 key = eepc_get_entry_by_scancode(event);
531 if (key) {
532 switch (key->type) {
533 case KE_KEY:
534 input_report_key(ehotk->inputdev, key->keycode,
535 1);
536 input_sync(ehotk->inputdev);
537 input_report_key(ehotk->inputdev, key->keycode,
538 0);
539 input_sync(ehotk->inputdev);
540 break;
541 }
542 }
543 }
Eric Coopere59f8792008-03-13 12:55:46 +0100544}
545
546static int eeepc_hotk_add(struct acpi_device *device)
547{
548 acpi_status status = AE_OK;
549 int result;
550
551 if (!device)
552 return -EINVAL;
553 printk(EEEPC_NOTICE EEEPC_HOTK_NAME "\n");
554 ehotk = kzalloc(sizeof(struct eeepc_hotk), GFP_KERNEL);
555 if (!ehotk)
556 return -ENOMEM;
557 ehotk->init_flag = DISABLE_ASL_WLAN | DISABLE_ASL_DISPLAYSWITCH;
558 ehotk->handle = device->handle;
559 strcpy(acpi_device_name(device), EEEPC_HOTK_DEVICE_NAME);
560 strcpy(acpi_device_class(device), EEEPC_HOTK_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700561 device->driver_data = ehotk;
Eric Coopere59f8792008-03-13 12:55:46 +0100562 ehotk->device = device;
563 result = eeepc_hotk_check();
564 if (result)
565 goto end;
566 status = acpi_install_notify_handler(ehotk->handle, ACPI_SYSTEM_NOTIFY,
567 eeepc_hotk_notify, ehotk);
568 if (ACPI_FAILURE(status))
569 printk(EEEPC_ERR "Error installing notify handler\n");
Matthew Garretta195dcd2008-08-19 12:13:20 +0100570
571 if (get_acpi(CM_ASL_WLAN) != -1) {
572 ehotk->eeepc_wlan_rfkill = rfkill_allocate(&device->dev,
573 RFKILL_TYPE_WLAN);
574
575 if (!ehotk->eeepc_wlan_rfkill)
576 goto end;
577
578 ehotk->eeepc_wlan_rfkill->name = "eeepc-wlan";
579 ehotk->eeepc_wlan_rfkill->toggle_radio = eeepc_wlan_rfkill_set;
580 ehotk->eeepc_wlan_rfkill->get_state = eeepc_wlan_rfkill_state;
581 if (get_acpi(CM_ASL_WLAN) == 1)
582 ehotk->eeepc_wlan_rfkill->state =
583 RFKILL_STATE_UNBLOCKED;
584 else
585 ehotk->eeepc_wlan_rfkill->state =
586 RFKILL_STATE_SOFT_BLOCKED;
587 rfkill_register(ehotk->eeepc_wlan_rfkill);
588 }
589
590 if (get_acpi(CM_ASL_BLUETOOTH) != -1) {
591 ehotk->eeepc_bluetooth_rfkill =
592 rfkill_allocate(&device->dev, RFKILL_TYPE_BLUETOOTH);
593
594 if (!ehotk->eeepc_bluetooth_rfkill)
595 goto end;
596
597 ehotk->eeepc_bluetooth_rfkill->name = "eeepc-bluetooth";
598 ehotk->eeepc_bluetooth_rfkill->toggle_radio =
599 eeepc_bluetooth_rfkill_set;
600 ehotk->eeepc_bluetooth_rfkill->get_state =
601 eeepc_bluetooth_rfkill_state;
602 if (get_acpi(CM_ASL_BLUETOOTH) == 1)
603 ehotk->eeepc_bluetooth_rfkill->state =
604 RFKILL_STATE_UNBLOCKED;
605 else
606 ehotk->eeepc_bluetooth_rfkill->state =
607 RFKILL_STATE_SOFT_BLOCKED;
608 rfkill_register(ehotk->eeepc_bluetooth_rfkill);
609 }
610
Eric Coopere59f8792008-03-13 12:55:46 +0100611 end:
612 if (result) {
613 kfree(ehotk);
614 ehotk = NULL;
615 }
616 return result;
617}
618
619static int eeepc_hotk_remove(struct acpi_device *device, int type)
620{
621 acpi_status status = 0;
622
623 if (!device || !acpi_driver_data(device))
624 return -EINVAL;
625 status = acpi_remove_notify_handler(ehotk->handle, ACPI_SYSTEM_NOTIFY,
626 eeepc_hotk_notify);
627 if (ACPI_FAILURE(status))
628 printk(EEEPC_ERR "Error removing notify handler\n");
629 kfree(ehotk);
630 return 0;
631}
632
633/*
Corentin Charye1faa9d2008-03-13 12:57:18 +0100634 * Hwmon
635 */
636static int eeepc_get_fan_pwm(void)
637{
638 int value = 0;
639
640 read_acpi_int(NULL, EEEPC_EC_FAN_PWM, &value);
Corentin Chary04dcd842008-10-09 15:33:57 +0200641 value = value * 255 / 100;
Corentin Charye1faa9d2008-03-13 12:57:18 +0100642 return (value);
643}
644
645static void eeepc_set_fan_pwm(int value)
646{
Corentin Chary04dcd842008-10-09 15:33:57 +0200647 value = SENSORS_LIMIT(value, 0, 255);
648 value = value * 100 / 255;
Corentin Charye1faa9d2008-03-13 12:57:18 +0100649 ec_write(EEEPC_EC_SC02, value);
650}
651
652static int eeepc_get_fan_rpm(void)
653{
654 int high = 0;
655 int low = 0;
656
657 read_acpi_int(NULL, EEEPC_EC_FAN_HRPM, &high);
658 read_acpi_int(NULL, EEEPC_EC_FAN_LRPM, &low);
659 return (high << 8 | low);
660}
661
662static int eeepc_get_fan_ctrl(void)
663{
664 int value = 0;
665
666 read_acpi_int(NULL, EEEPC_EC_FAN_CTRL, &value);
667 return ((value & 0x02 ? 1 : 0));
668}
669
670static void eeepc_set_fan_ctrl(int manual)
671{
672 int value = 0;
673
674 read_acpi_int(NULL, EEEPC_EC_FAN_CTRL, &value);
675 if (manual)
676 value |= 0x02;
677 else
678 value &= ~0x02;
679 ec_write(EEEPC_EC_SFB3, value);
680}
681
682static ssize_t store_sys_hwmon(void (*set)(int), const char *buf, size_t count)
683{
684 int rv, value;
685
686 rv = parse_arg(buf, count, &value);
687 if (rv > 0)
688 set(value);
689 return rv;
690}
691
692static ssize_t show_sys_hwmon(int (*get)(void), char *buf)
693{
694 return sprintf(buf, "%d\n", get());
695}
696
697#define EEEPC_CREATE_SENSOR_ATTR(_name, _mode, _set, _get) \
698 static ssize_t show_##_name(struct device *dev, \
699 struct device_attribute *attr, \
700 char *buf) \
701 { \
702 return show_sys_hwmon(_set, buf); \
703 } \
704 static ssize_t store_##_name(struct device *dev, \
705 struct device_attribute *attr, \
706 const char *buf, size_t count) \
707 { \
708 return store_sys_hwmon(_get, buf, count); \
709 } \
710 static SENSOR_DEVICE_ATTR(_name, _mode, show_##_name, store_##_name, 0);
711
712EEEPC_CREATE_SENSOR_ATTR(fan1_input, S_IRUGO, eeepc_get_fan_rpm, NULL);
Corentin Chary04dcd842008-10-09 15:33:57 +0200713EEEPC_CREATE_SENSOR_ATTR(pwm1, S_IRUGO | S_IWUSR,
Corentin Charye1faa9d2008-03-13 12:57:18 +0100714 eeepc_get_fan_pwm, eeepc_set_fan_pwm);
715EEEPC_CREATE_SENSOR_ATTR(pwm1_enable, S_IRUGO | S_IWUSR,
716 eeepc_get_fan_ctrl, eeepc_set_fan_ctrl);
717
Corentin Chary04dcd842008-10-09 15:33:57 +0200718static ssize_t
719show_name(struct device *dev, struct device_attribute *attr, char *buf)
720{
721 return sprintf(buf, "eeepc\n");
722}
723static SENSOR_DEVICE_ATTR(name, S_IRUGO, show_name, NULL, 0);
724
Corentin Charye1faa9d2008-03-13 12:57:18 +0100725static struct attribute *hwmon_attributes[] = {
Corentin Chary04dcd842008-10-09 15:33:57 +0200726 &sensor_dev_attr_pwm1.dev_attr.attr,
Corentin Charye1faa9d2008-03-13 12:57:18 +0100727 &sensor_dev_attr_fan1_input.dev_attr.attr,
728 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
Corentin Chary04dcd842008-10-09 15:33:57 +0200729 &sensor_dev_attr_name.dev_attr.attr,
Corentin Charye1faa9d2008-03-13 12:57:18 +0100730 NULL
731};
732
733static struct attribute_group hwmon_attribute_group = {
734 .attrs = hwmon_attributes
735};
736
737/*
Eric Coopere59f8792008-03-13 12:55:46 +0100738 * exit/init
739 */
Corentin Charya5fa4292008-03-13 12:56:37 +0100740static void eeepc_backlight_exit(void)
741{
742 if (eeepc_backlight_device)
743 backlight_device_unregister(eeepc_backlight_device);
Corentin Charya9df80c2009-01-20 16:17:40 +0100744 eeepc_backlight_device = NULL;
745}
746
747static void eeepc_rfkill_exit(void)
748{
Matthew Garretta195dcd2008-08-19 12:13:20 +0100749 if (ehotk->eeepc_wlan_rfkill)
750 rfkill_unregister(ehotk->eeepc_wlan_rfkill);
751 if (ehotk->eeepc_bluetooth_rfkill)
752 rfkill_unregister(ehotk->eeepc_bluetooth_rfkill);
Corentin Charya9df80c2009-01-20 16:17:40 +0100753}
754
755static void eeepc_input_exit(void)
756{
757 if (ehotk->inputdev)
758 input_unregister_device(ehotk->inputdev);
Corentin Charya5fa4292008-03-13 12:56:37 +0100759}
760
Corentin Charye1faa9d2008-03-13 12:57:18 +0100761static void eeepc_hwmon_exit(void)
762{
763 struct device *hwmon;
764
765 hwmon = eeepc_hwmon_device;
766 if (!hwmon)
767 return ;
Corentin Charye1faa9d2008-03-13 12:57:18 +0100768 sysfs_remove_group(&hwmon->kobj,
769 &hwmon_attribute_group);
Matthew Garrettf1441312008-08-20 14:08:57 -0700770 hwmon_device_unregister(hwmon);
Corentin Charye1faa9d2008-03-13 12:57:18 +0100771 eeepc_hwmon_device = NULL;
772}
773
Eric Coopere59f8792008-03-13 12:55:46 +0100774static void __exit eeepc_laptop_exit(void)
775{
Corentin Charya5fa4292008-03-13 12:56:37 +0100776 eeepc_backlight_exit();
Corentin Charya9df80c2009-01-20 16:17:40 +0100777 eeepc_rfkill_exit();
778 eeepc_input_exit();
Corentin Charye1faa9d2008-03-13 12:57:18 +0100779 eeepc_hwmon_exit();
Eric Coopere59f8792008-03-13 12:55:46 +0100780 acpi_bus_unregister_driver(&eeepc_hotk_driver);
781 sysfs_remove_group(&platform_device->dev.kobj,
782 &platform_attribute_group);
783 platform_device_unregister(platform_device);
784 platform_driver_unregister(&platform_driver);
785}
786
Corentin Charya5fa4292008-03-13 12:56:37 +0100787static int eeepc_backlight_init(struct device *dev)
788{
789 struct backlight_device *bd;
790
791 bd = backlight_device_register(EEEPC_HOTK_FILE, dev,
792 NULL, &eeepcbl_ops);
793 if (IS_ERR(bd)) {
794 printk(EEEPC_ERR
795 "Could not register eeepc backlight device\n");
796 eeepc_backlight_device = NULL;
797 return PTR_ERR(bd);
798 }
799 eeepc_backlight_device = bd;
800 bd->props.max_brightness = 15;
801 bd->props.brightness = read_brightness(NULL);
802 bd->props.power = FB_BLANK_UNBLANK;
803 backlight_update_status(bd);
804 return 0;
805}
806
Corentin Charye1faa9d2008-03-13 12:57:18 +0100807static int eeepc_hwmon_init(struct device *dev)
808{
809 struct device *hwmon;
810 int result;
811
812 hwmon = hwmon_device_register(dev);
813 if (IS_ERR(hwmon)) {
814 printk(EEEPC_ERR
815 "Could not register eeepc hwmon device\n");
816 eeepc_hwmon_device = NULL;
817 return PTR_ERR(hwmon);
818 }
819 eeepc_hwmon_device = hwmon;
820 result = sysfs_create_group(&hwmon->kobj,
821 &hwmon_attribute_group);
822 if (result)
823 eeepc_hwmon_exit();
824 return result;
825}
826
Eric Coopere59f8792008-03-13 12:55:46 +0100827static int __init eeepc_laptop_init(void)
828{
829 struct device *dev;
830 int result;
831
832 if (acpi_disabled)
833 return -ENODEV;
834 result = acpi_bus_register_driver(&eeepc_hotk_driver);
835 if (result < 0)
836 return result;
837 if (!ehotk) {
838 acpi_bus_unregister_driver(&eeepc_hotk_driver);
839 return -ENODEV;
840 }
841 dev = acpi_get_physical_device(ehotk->device->handle);
Thomas Renningera2bf8c02008-08-01 17:37:59 +0200842
843 if (!acpi_video_backlight_support()) {
844 result = eeepc_backlight_init(dev);
845 if (result)
846 goto fail_backlight;
847 } else
848 printk(EEEPC_INFO "Backlight controlled by ACPI video "
849 "driver\n");
850
Corentin Charye1faa9d2008-03-13 12:57:18 +0100851 result = eeepc_hwmon_init(dev);
852 if (result)
853 goto fail_hwmon;
Eric Coopere59f8792008-03-13 12:55:46 +0100854 /* Register platform stuff */
855 result = platform_driver_register(&platform_driver);
856 if (result)
857 goto fail_platform_driver;
858 platform_device = platform_device_alloc(EEEPC_HOTK_FILE, -1);
859 if (!platform_device) {
860 result = -ENOMEM;
861 goto fail_platform_device1;
862 }
863 result = platform_device_add(platform_device);
864 if (result)
865 goto fail_platform_device2;
866 result = sysfs_create_group(&platform_device->dev.kobj,
867 &platform_attribute_group);
868 if (result)
869 goto fail_sysfs;
870 return 0;
871fail_sysfs:
872 platform_device_del(platform_device);
873fail_platform_device2:
874 platform_device_put(platform_device);
875fail_platform_device1:
876 platform_driver_unregister(&platform_driver);
877fail_platform_driver:
Corentin Charye1faa9d2008-03-13 12:57:18 +0100878 eeepc_hwmon_exit();
879fail_hwmon:
Corentin Charya5fa4292008-03-13 12:56:37 +0100880 eeepc_backlight_exit();
881fail_backlight:
Corentin Charya9df80c2009-01-20 16:17:40 +0100882 eeepc_input_exit();
883 eeepc_rfkill_exit();
Eric Coopere59f8792008-03-13 12:55:46 +0100884 return result;
885}
886
887module_init(eeepc_laptop_init);
888module_exit(eeepc_laptop_exit);