pcc-acpi: fix compile with new stricter ACPI types
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / staging / pcc-acpi / pcc-acpi.c
blob3b34a3e4885a25edaf0552661426bde0dbdfd697
1 /*
2 * Panasonic HotKey and lcd brightness control Extra driver
3 * (C) 2004 Hiroshi Miura <miura@da-cha.org>
4 * (C) 2004 NTT DATA Intellilink Co. http://www.intellilink.co.jp/
5 * (C) YOKOTA Hiroshi <yokota (at) netlab. is. tsukuba. ac. jp>
6 * (C) 2004 David Bronaugh <dbronaugh>
8 * derived from toshiba_acpi.c, Copyright (C) 2002-2004 John Belmonte
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * publicshed by the Free Software Foundation.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 *---------------------------------------------------------------------------
25 * ChangeLog:
27 * Nov.04, 2006 Hiroshi Miura <miura@da-cha.org>
28 * -v0.9 remove warning about section reference.
29 * remove acpi_os_free
30 * add /proc/acpi/pcc/brightness interface to
31 * allow HAL to access.
32 * merge dbronaugh's enhancement
33 * Aug.17, 2004 David Bronaugh (dbronaugh)
34 * - Added screen brightness setting interface
35 * Thanks to the FreeBSD crew
36 * (acpi_panasonic.c authors)
37 * for the ideas I needed to accomplish it
39 * May.29, 2006 Hiroshi Miura <miura@da-cha.org>
40 * -v0.8.4 follow to change keyinput structure
41 * thanks Fabian Yamaguchi <fabs@cs.tu-berlin.de>,
42 * Jacob Bower <jacob.bower@ic.ac.uk> and
43 * Hiroshi Yokota for providing solutions.
45 * Oct.02, 2004 Hiroshi Miura <miura@da-cha.org>
46 * -v0.8.2 merge code of YOKOTA Hiroshi
47 * <yokota@netlab.is.tsukuba.ac.jp>.
48 * Add sticky key mode interface.
49 * Refactoring acpi_pcc_generete_keyinput().
51 * Sep.15, 2004 Hiroshi Miura <miura@da-cha.org>
52 * -v0.8 Generate key input event on input subsystem.
53 * This is based on yet another driver
54 * written by Ryuta Nakanishi.
56 * Sep.10, 2004 Hiroshi Miura <miura@da-cha.org>
57 * -v0.7 Change proc interface functions using seq_file
58 * facility as same as other ACPI drivers.
60 * Aug.28, 2004 Hiroshi Miura <miura@da-cha.org>
61 * -v0.6.4 Fix a silly error with status checking
63 * Aug.25, 2004 Hiroshi Miura <miura@da-cha.org>
64 * -v0.6.3 replace read_acpi_int by standard
65 * function acpi_evaluate_integer
66 * some clean up and make smart copyright notice.
67 * fix return value of pcc_acpi_get_key()
68 * fix checking return value of acpi_bus_register_driver()
70 * Aug.22, 2004 David Bronaugh <dbronaugh@linuxboxen.org>
71 * -v0.6.2 Add check on ACPI data (num_sifr)
72 * Coding style cleanups, better error messages/handling
73 * Fixed an off-by-one error in memory allocation
75 * Aug.21, 2004 David Bronaugh <dbronaugh@linuxboxen.org>
76 * -v0.6.1 Fix a silly error with status checking
78 * Aug.20, 2004 David Bronaugh <dbronaugh@linuxboxen.org>
79 * - v0.6 Correct brightness controls to reflect reality
80 * based on information gleaned by Hiroshi Miura
81 * and discussions with Hiroshi Miura
83 * Aug.10, 2004 Hiroshi Miura <miura@da-cha.org>
84 * - v0.5 support LCD brightness control
85 * based on the disclosed information by MEI.
87 * Jul.25, 2004 Hiroshi Miura <miura@da-cha.org>
88 * - v0.4 first post version
89 * add function to retrive SIFR
91 * Jul.24, 2004 Hiroshi Miura <miura@da-cha.org>
92 * - v0.3 get proper status of hotkey
94 * Jul.22, 2004 Hiroshi Miura <miura@da-cha.org>
95 * - v0.2 add HotKey handler
97 * Jul.17, 2004 Hiroshi Miura <miura@da-cha.org>
98 * - v0.1 start from toshiba_acpi driver written by John Belmonte
102 #define ACPI_PCC_VERSION "0.9+hy"
104 #include <linux/kernel.h>
105 #include <linux/module.h>
106 #include <linux/types.h>
107 #include <linux/ctype.h>
108 #include <linux/init.h>
109 #include <linux/input.h>
110 #include <linux/proc_fs.h>
111 #include <linux/seq_file.h>
112 #include <linux/slab.h>
113 #include <linux/uaccess.h>
114 #include <acpi/acpi_bus.h>
115 #include <acpi/acpi_drivers.h>
118 /*************************************************************************
119 * "seq" file template definition.
121 /* "seq" initializer */
122 #define SEQ_OPEN_FS(_open_func_name_, _show_func_name_) \
123 static int _open_func_name_(struct inode *inode, struct file *file) \
125 return single_open(file, _show_func_name_, PDE(inode)->data); \
128 /*-------------------------------------------------------------------------
129 * "seq" fops template for read-only files.
131 #define SEQ_FILEOPS_R(_open_func_name_) \
133 .open = _open_func_name_, \
134 .read = seq_read, \
135 .llseek = seq_lseek, \
136 .release = single_release, \
139 /*------------------------------------------------------------------------
140 * "seq" fops template for read-write files.
142 #define SEQ_FILEOPS_RW(_open_func_name_, _write_func_name_) \
144 .open = _open_func_name_ , \
145 .read = seq_read, \
146 .write = _write_func_name_, \
147 .llseek = seq_lseek, \
148 .release = single_release, \
152 * "seq" file template definition ended.
153 ***************************************************************************
155 #ifndef ACPI_HOTKEY_COMPONENT
156 #define ACPI_HOTKEY_COMPONENT 0x10000000
157 #endif
159 #define _COMPONENT ACPI_HOTKEY_COMPONENT
160 ACPI_MODULE_NAME("pcc_acpi");
162 MODULE_AUTHOR("Hiroshi Miura, Hiroshi Yokota");
163 MODULE_DESCRIPTION("ACPI HotKey driver for Panasonic Let's Note laptops");
164 MODULE_LICENSE("GPL");
166 #define LOGPREFIX "pcc_acpi: "
168 /****************************************************
169 * Define ACPI PATHs
170 ****************************************************/
171 /* Lets note hotkeys */
172 #define METHOD_HKEY_QUERY "HINF"
173 #define METHOD_HKEY_SQTY "SQTY"
174 #define METHOD_HKEY_SINF "SINF"
175 #define METHOD_HKEY_SSET "SSET"
176 #define HKEY_NOTIFY 0x80
178 /* for brightness control */
179 #define LCD_MAX_BRIGHTNESS 255
180 /* This may be magical -- beware */
181 #define LCD_BRIGHTNESS_INCREMENT 17
182 /* Registers of SINF */
183 #define SINF_LCD_BRIGHTNESS 4
185 /*******************************************************************
187 * definitions for /proc/ interface
189 *******************************************************************/
190 #define ACPI_PCC_DRIVER_NAME "pcc_acpi"
191 #define ACPI_PCC_DEVICE_NAME "PCCExtra"
192 #define ACPI_PCC_CLASS "pcc"
193 #define PROC_PCC ACPI_PCC_CLASS
195 #define ACPI_PCC_INPUT_PHYS "panasonic/hkey0"
197 /* This is transitional definition */
198 #ifndef KEY_BATT
199 # define KEY_BATT 227
200 #endif
202 #define PROC_STR_MAX_LEN 8
204 #define BUS_PCC_HOTKEY BUS_I8042 /*0x1a*/ /* FIXME: BUS_I8042? */
206 /* Fn+F4/F5 confricts with Shift+F1/F2 */
207 /* This hack avoids key number confrict */
208 #define PCC_KEYINPUT_MODE (0)
210 /* LCD_TYPEs: 0 = Normal, 1 = Semi-transparent
211 ENV_STATEs: Normal temp=0x01, High temp=0x81, N/A=0x00
213 enum SINF_BITS { SINF_NUM_BATTERIES = 0,
214 SINF_LCD_TYPE,
215 SINF_AC_MAX_BRIGHT,
216 SINF_AC_MIN_BRIGHT,
217 SINF_AC_CUR_BRIGHT,
218 /* 4 = R1 only handle SINF_AC_CUR_BRIGHT
219 * as SINF_CUR_BRIGHT and don't know AC state */
220 SINF_DC_MAX_BRIGHT,
221 SINF_DC_MIN_BRIGHT,
222 SINF_DC_CUR_BRIGHT,
223 SINF_MUTE,
224 SINF_RESERVED,
225 SINF_ENV_STATE, /* 10 */
226 SINF_STICKY_KEY = 0x80,
229 static struct acpi_device_id pcc_device_ids[] = {
230 {"MAT0012", 0},
231 {"MAT0013", 0},
232 {"MAT0018", 0},
233 {"MAT0019", 0},
234 {"", 0},
236 MODULE_DEVICE_TABLE(acpi, pcc_device_ids);
239 static int __devinit acpi_pcc_hotkey_add(struct acpi_device *device);
240 static int __devexit acpi_pcc_hotkey_remove(struct acpi_device *device,
241 int type);
242 static int acpi_pcc_hotkey_resume(struct acpi_device *device);
245 static struct acpi_driver acpi_pcc_driver = {
246 .name = ACPI_PCC_DRIVER_NAME,
247 .class = ACPI_PCC_CLASS,
248 .ids = pcc_device_ids,
249 .ops = {
250 .add = acpi_pcc_hotkey_add,
251 .remove = __devexit_p(acpi_pcc_hotkey_remove),
252 #ifdef CONFIG_PM
253 /*.suspend = acpi_pcc_hotkey_suspend,*/
254 .resume = acpi_pcc_hotkey_resume,
255 #endif
259 struct acpi_hotkey {
260 acpi_handle handle;
261 struct acpi_device *device;
262 struct proc_dir_entry *proc_dir_entry;
263 unsigned long num_sifr;
264 unsigned long status;
265 struct input_dev *input_dev;
266 int sticky_mode;
269 struct pcc_keyinput {
270 struct acpi_hotkey *hotkey;
271 int key_mode;
274 /* *************************************************************************
275 Hotkey driver core
276 ************************************************************************* */
277 /* -------------------------------------------------------------------------
278 method access functions
279 ------------------------------------------------------------------------- */
280 static int acpi_pcc_write_sset(struct acpi_hotkey *hotkey, int func, int val)
282 union acpi_object in_objs[] = {
283 { .integer.type = ACPI_TYPE_INTEGER,
284 .integer.value = func, },
285 { .integer.type = ACPI_TYPE_INTEGER,
286 .integer.value = val, },
288 struct acpi_object_list params = {
289 .count = ARRAY_SIZE(in_objs),
290 .pointer = in_objs,
292 acpi_status status;
294 ACPI_FUNCTION_TRACE("acpi_pcc_write_sset");
296 status = acpi_evaluate_object(hotkey->handle, METHOD_HKEY_SSET,
297 &params, NULL);
299 return_VALUE(status == AE_OK ? AE_OK : AE_ERROR);
302 static inline int acpi_pcc_get_sqty(struct acpi_device *device)
304 unsigned long s;
305 acpi_status status;
307 ACPI_FUNCTION_TRACE("acpi_pcc_get_sqty");
309 status = acpi_evaluate_integer(device->handle, METHOD_HKEY_SQTY,
310 NULL, &s);
311 if (ACPI_SUCCESS(status)) {
312 return_VALUE(s);
313 } else {
314 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
315 "evaluation error HKEY.SQTY\n"));
316 return_VALUE(-EINVAL);
320 static int acpi_pcc_retrieve_biosdata(struct acpi_hotkey *hotkey, u32 *sinf)
322 acpi_status status;
323 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
324 union acpi_object *hkey = NULL;
325 int i;
327 ACPI_FUNCTION_TRACE("acpi_pcc_retrieve_biosdata");
329 status = acpi_evaluate_object(hotkey->handle, METHOD_HKEY_SINF, 0,
330 &buffer);
331 if (ACPI_FAILURE(status)) {
332 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
333 "evaluation error HKEY.SINF\n"));
334 status = AE_ERROR;
335 return_VALUE(status);
338 hkey = buffer.pointer;
339 if (!hkey || (hkey->type != ACPI_TYPE_PACKAGE)) {
340 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid HKEY.SINF\n"));
341 goto free_buffer;
344 if (hotkey->num_sifr < hkey->package.count) {
345 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
346 "SQTY reports bad SINF length\n"));
347 status = AE_ERROR;
348 goto free_buffer;
351 for (i = 0; i < hkey->package.count; i++) {
352 union acpi_object *element = &(hkey->package.elements[i]);
353 if (likely(element->type == ACPI_TYPE_INTEGER)) {
354 sinf[i] = element->integer.value;
355 } else {
356 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
357 "Invalid HKEY.SINF data\n"));
358 status = AE_ERROR;
359 break;
362 sinf[hkey->package.count] = -1;
364 free_buffer:
365 kfree(buffer.pointer);
366 return_VALUE(status == AE_OK ? AE_OK : AE_ERROR);
369 static int acpi_pcc_read_sinf_field(struct seq_file *seq, int field)
371 struct acpi_hotkey *hotkey = (struct acpi_hotkey *) seq->private;
372 u32 sinf[hotkey->num_sifr + 1];
374 ACPI_FUNCTION_TRACE("acpi_pcc_read_sinf_field");
376 if (ACPI_SUCCESS(acpi_pcc_retrieve_biosdata(hotkey, sinf)))
377 seq_printf(seq, "%u\n", sinf[field]);
378 else
379 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
380 "Couldn't retrieve BIOS data\n"));
382 return_VALUE(AE_OK);
385 /* -------------------------------------------------------------------------
386 user interface functions
387 ------------------------------------------------------------------------- */
388 /* read methods */
389 /* Sinf read methods */
390 #define PCC_SINF_READ_F(_name_, FUNC) \
391 static int _name_(struct seq_file *seq, void *offset) \
393 return_VALUE(ACPI_SUCCESS(acpi_pcc_read_sinf_field(seq, \
394 (FUNC))) \
395 ? 0 : -EINVAL); \
398 PCC_SINF_READ_F(acpi_pcc_numbatteries_show, SINF_NUM_BATTERIES);
399 PCC_SINF_READ_F(acpi_pcc_lcdtype_show, SINF_LCD_TYPE);
400 PCC_SINF_READ_F(acpi_pcc_ac_brightness_max_show, SINF_AC_MAX_BRIGHT);
401 PCC_SINF_READ_F(acpi_pcc_ac_brightness_min_show, SINF_AC_MIN_BRIGHT);
402 PCC_SINF_READ_F(acpi_pcc_ac_brightness_show, SINF_AC_CUR_BRIGHT);
403 PCC_SINF_READ_F(acpi_pcc_dc_brightness_max_show, SINF_DC_MAX_BRIGHT);
404 PCC_SINF_READ_F(acpi_pcc_dc_brightness_min_show, SINF_DC_MIN_BRIGHT);
405 PCC_SINF_READ_F(acpi_pcc_dc_brightness_show, SINF_DC_CUR_BRIGHT);
406 PCC_SINF_READ_F(acpi_pcc_brightness_show, SINF_AC_CUR_BRIGHT);
407 PCC_SINF_READ_F(acpi_pcc_mute_show, SINF_MUTE);
409 static int acpi_pcc_sticky_key_show(struct seq_file *seq, void *offset)
411 struct acpi_hotkey *hotkey = seq->private;
413 ACPI_FUNCTION_TRACE("acpi_pcc_sticky_key_show");
415 if (!hotkey || !hotkey->device)
416 return_VALUE(-EINVAL);
418 seq_printf(seq, "%d\n", hotkey->sticky_mode);
420 return_VALUE(0);
423 static int acpi_pcc_keyinput_show(struct seq_file *seq, void *offset)
425 struct acpi_hotkey *hotkey = seq->private;
426 struct input_dev *hotk_input_dev = hotkey->input_dev;
427 struct pcc_keyinput *keyinput = input_get_drvdata(hotk_input_dev);
429 ACPI_FUNCTION_TRACE("acpi_pcc_keyinput_show");
431 seq_printf(seq, "%d\n", keyinput->key_mode);
433 return_VALUE(0);
436 static int acpi_pcc_version_show(struct seq_file *seq, void *offset)
438 struct acpi_hotkey *hotkey = seq->private;
440 ACPI_FUNCTION_TRACE("acpi_pcc_version_show");
442 if (!hotkey || !hotkey->device)
443 return_VALUE(-EINVAL);
445 seq_printf(seq, "%s version %s\n", ACPI_PCC_DRIVER_NAME,
446 ACPI_PCC_VERSION);
447 seq_printf(seq, "%li functions\n", hotkey->num_sifr);
449 return_VALUE(0);
452 /* write methods */
453 static ssize_t acpi_pcc_write_single_flag(struct file *file,
454 const char __user *buffer,
455 size_t count,
456 int sinf_func)
458 struct seq_file *seq = file->private_data;
459 struct acpi_hotkey *hotkey = seq->private;
460 char write_string[PROC_STR_MAX_LEN];
461 u32 val;
463 ACPI_FUNCTION_TRACE("acpi_pcc_write_single_flag");
465 if (!hotkey || (count > sizeof(write_string) - 1))
466 return_VALUE(-EINVAL);
468 if (copy_from_user(write_string, buffer, count))
469 return_VALUE(-EFAULT);
471 write_string[count] = '\0';
473 if ((sscanf(write_string, "%3i", &val) == 1) &&
474 (val == 0 || val == 1))
475 acpi_pcc_write_sset(hotkey, sinf_func, val);
477 return_VALUE(count);
480 static unsigned long acpi_pcc_write_brightness(struct file *file,
481 const char __user *buffer,
482 size_t count,
483 int min_index, int max_index,
484 int cur_index)
486 struct seq_file *seq = (struct seq_file *)file->private_data;
487 struct acpi_hotkey *hotkey = (struct acpi_hotkey *)seq->private;
488 char write_string[PROC_STR_MAX_LEN];
489 u32 bright;
490 u32 sinf[hotkey->num_sifr + 1];
492 ACPI_FUNCTION_TRACE("acpi_pcc_write_brightness");
494 if (!hotkey || (count > sizeof(write_string) - 1))
495 return_VALUE(-EINVAL);
497 if (copy_from_user(write_string, buffer, count))
498 return_VALUE(-EFAULT);
500 write_string[count] = '\0';
502 if (ACPI_FAILURE(acpi_pcc_retrieve_biosdata(hotkey, sinf))) {
503 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
504 "Couldn't retrieve BIOS data\n"));
505 goto end;
508 if ((sscanf(write_string, "%4i", &bright) == 1) &&
509 (bright >= sinf[min_index]) &&
510 (bright <= sinf[max_index]))
511 acpi_pcc_write_sset(hotkey, cur_index, bright);
513 end:
514 return_VALUE(count);
517 static ssize_t acpi_pcc_write_ac_brightness(struct file *file,
518 const char __user *buffer,
519 size_t count, loff_t *ppos)
521 return_VALUE(acpi_pcc_write_brightness(file, buffer, count,
522 SINF_AC_MIN_BRIGHT,
523 SINF_AC_MAX_BRIGHT,
524 SINF_AC_CUR_BRIGHT));
527 static ssize_t acpi_pcc_write_dc_brightness(struct file *file,
528 const char __user *buffer,
529 size_t count, loff_t *ppos)
531 return_VALUE(acpi_pcc_write_brightness(file, buffer, count,
532 SINF_DC_MIN_BRIGHT,
533 SINF_DC_MAX_BRIGHT,
534 SINF_DC_CUR_BRIGHT));
537 static ssize_t acpi_pcc_write_no_brightness(struct file *file,
538 const char __user *buffer,
539 size_t count, loff_t *ppos)
541 return acpi_pcc_write_brightness(file, buffer, count,
542 SINF_AC_MIN_BRIGHT,
543 SINF_AC_MAX_BRIGHT,
544 SINF_AC_CUR_BRIGHT);
547 static ssize_t acpi_pcc_write_mute(struct file *file,
548 const char __user *buffer,
549 size_t count, loff_t *ppos)
551 return_VALUE(acpi_pcc_write_single_flag(file, buffer, count,
552 SINF_MUTE));
555 static ssize_t acpi_pcc_write_sticky_key(struct file *file,
556 const char __user *buffer,
557 size_t count, loff_t *ppos)
559 struct seq_file *seq = (struct seq_file *)file->private_data;
560 struct acpi_hotkey *hotkey = (struct acpi_hotkey *)seq->private;
561 char write_string[PROC_STR_MAX_LEN];
562 int mode;
564 ACPI_FUNCTION_TRACE("acpi_pcc_write_sticky_key");
566 if (!hotkey || (count > sizeof(write_string) - 1))
567 return_VALUE(-EINVAL);
569 if (copy_from_user(write_string, buffer, count))
570 return_VALUE(-EFAULT);
572 write_string[count] = '\0';
574 if ((sscanf(write_string, "%3i", &mode) == 1) &&
575 (mode == 0 || mode == 1)) {
576 acpi_pcc_write_sset(hotkey, SINF_STICKY_KEY, mode);
577 hotkey->sticky_mode = mode;
580 return_VALUE(count);
583 static ssize_t acpi_pcc_write_keyinput(struct file *file,
584 const char __user *buffer,
585 size_t count, loff_t *ppos)
587 struct seq_file *seq = (struct seq_file *)file->private_data;
588 struct acpi_hotkey *hotkey = (struct acpi_hotkey *)seq->private;
589 struct pcc_keyinput *keyinput;
590 char write_string[PROC_STR_MAX_LEN];
591 int key_mode;
593 ACPI_FUNCTION_TRACE("acpi_pcc_write_keyinput");
595 if (!hotkey || (count > (sizeof(write_string) - 1)))
596 return_VALUE(-EINVAL);
598 if (copy_from_user(write_string, buffer, count))
599 return_VALUE(-EFAULT);
601 write_string[count] = '\0';
603 if ((sscanf(write_string, "%4i", &key_mode) == 1) &&
604 (key_mode == 0 || key_mode == 1)) {
605 keyinput = input_get_drvdata(hotkey->input_dev);
606 keyinput->key_mode = key_mode;
609 return_VALUE(count);
612 /* -------------------------------------------------------------------------
613 hotkey driver
614 ------------------------------------------------------------------------- */
615 static void acpi_pcc_generete_keyinput(struct acpi_hotkey *hotkey)
617 struct input_dev *hotk_input_dev = hotkey->input_dev;
618 struct pcc_keyinput *keyinput = input_get_drvdata(hotk_input_dev);
619 int hinf = hotkey->status;
620 int key_code, hkey_num;
621 const int key_map[] = {
622 /* 0 */ -1,
623 /* 1 */ KEY_BRIGHTNESSDOWN,
624 /* 2 */ KEY_BRIGHTNESSUP,
625 /* 3 */ -1, /* vga/lcd switch event is not occur on
626 hotkey driver. */
627 /* 4 */ KEY_MUTE,
628 /* 5 */ KEY_VOLUMEDOWN,
629 /* 6 */ KEY_VOLUMEUP,
630 /* 7 */ KEY_SLEEP,
631 /* 8 */ -1, /* Change CPU boost: do nothing */
632 /* 9 */ KEY_BATT,
633 /* 10 */ KEY_SUSPEND,
636 ACPI_FUNCTION_TRACE("acpi_pcc_generete_keyinput");
638 if (keyinput->key_mode == 0)
639 return_VOID;
641 hkey_num = hinf & 0xf;
643 if ((0 > hkey_num) ||
644 (hkey_num > ARRAY_SIZE(key_map))) {
645 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
646 "hotkey number out of range: %d\n",
647 hkey_num));
648 return_VOID;
651 key_code = key_map[hkey_num];
653 if (key_code != -1) {
654 int pushed = (hinf & 0x80) ? TRUE : FALSE;
656 input_report_key(hotk_input_dev, key_code, pushed);
657 input_sync(hotk_input_dev);
661 static int acpi_pcc_hotkey_get_key(struct acpi_hotkey *hotkey)
663 unsigned long result;
664 acpi_status status = AE_OK;
666 ACPI_FUNCTION_TRACE("acpi_pcc_hotkey_get_key");
668 status = acpi_evaluate_integer(hotkey->handle, METHOD_HKEY_QUERY,
669 NULL, &result);
670 if (likely(ACPI_SUCCESS(status)))
671 hotkey->status = result;
672 else
673 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
674 "error getting hotkey status\n"));
676 return_VALUE(status == AE_OK);
679 void acpi_pcc_hotkey_notify(acpi_handle handle, u32 event, void *data)
681 struct acpi_hotkey *hotkey = (struct acpi_hotkey *) data;
683 ACPI_FUNCTION_TRACE("acpi_pcc_hotkey_notify");
685 switch (event) {
686 case HKEY_NOTIFY:
687 if (acpi_pcc_hotkey_get_key(hotkey)) {
688 /* generate event like '"pcc HKEY 00000080 00000084"'
689 * when Fn+F4 pressed */
690 acpi_bus_generate_proc_event(hotkey->device, event,
691 hotkey->status);
693 acpi_pcc_generete_keyinput(hotkey);
694 break;
695 default:
696 /* nothing to do */
697 break;
699 return_VOID;
702 /* *************************************************************************
703 FS Interface (/proc)
704 ************************************************************************* */
705 /* oepn proc file fs*/
706 SEQ_OPEN_FS(acpi_pcc_dc_brightness_open_fs, acpi_pcc_dc_brightness_show);
707 SEQ_OPEN_FS(acpi_pcc_numbatteries_open_fs, acpi_pcc_numbatteries_show);
708 SEQ_OPEN_FS(acpi_pcc_lcdtype_open_fs, acpi_pcc_lcdtype_show);
709 SEQ_OPEN_FS(acpi_pcc_ac_brightness_max_open_fs,
710 acpi_pcc_ac_brightness_max_show);
711 SEQ_OPEN_FS(acpi_pcc_ac_brightness_min_open_fs,
712 acpi_pcc_ac_brightness_min_show);
713 SEQ_OPEN_FS(acpi_pcc_ac_brightness_open_fs, acpi_pcc_ac_brightness_show);
714 SEQ_OPEN_FS(acpi_pcc_dc_brightness_max_open_fs,
715 acpi_pcc_dc_brightness_max_show);
716 SEQ_OPEN_FS(acpi_pcc_dc_brightness_min_open_fs,
717 acpi_pcc_dc_brightness_min_show);
718 SEQ_OPEN_FS(acpi_pcc_brightness_open_fs, acpi_pcc_brightness_show);
719 SEQ_OPEN_FS(acpi_pcc_mute_open_fs, acpi_pcc_mute_show);
720 SEQ_OPEN_FS(acpi_pcc_version_open_fs, acpi_pcc_version_show);
721 SEQ_OPEN_FS(acpi_pcc_keyinput_open_fs, acpi_pcc_keyinput_show);
722 SEQ_OPEN_FS(acpi_pcc_sticky_key_open_fs, acpi_pcc_sticky_key_show);
724 static struct file_operations acpi_pcc_numbatteries_fops =
725 SEQ_FILEOPS_R(acpi_pcc_numbatteries_open_fs);
726 static struct file_operations acpi_pcc_lcdtype_fops =
727 SEQ_FILEOPS_R(acpi_pcc_lcdtype_open_fs);
728 static struct file_operations acpi_pcc_mute_fops =
729 SEQ_FILEOPS_RW(acpi_pcc_mute_open_fs, acpi_pcc_write_mute);
730 static struct file_operations acpi_pcc_ac_brightness_fops =
731 SEQ_FILEOPS_RW(acpi_pcc_ac_brightness_open_fs,
732 acpi_pcc_write_ac_brightness);
733 static struct file_operations acpi_pcc_ac_brightness_max_fops =
734 SEQ_FILEOPS_R(acpi_pcc_ac_brightness_max_open_fs);
735 static struct file_operations acpi_pcc_ac_brightness_min_fops =
736 SEQ_FILEOPS_R(acpi_pcc_ac_brightness_min_open_fs);
737 static struct file_operations acpi_pcc_dc_brightness_fops =
738 SEQ_FILEOPS_RW(acpi_pcc_dc_brightness_open_fs,
739 acpi_pcc_write_dc_brightness);
740 static struct file_operations acpi_pcc_dc_brightness_max_fops =
741 SEQ_FILEOPS_R(acpi_pcc_dc_brightness_max_open_fs);
742 static struct file_operations acpi_pcc_dc_brightness_min_fops =
743 SEQ_FILEOPS_R(acpi_pcc_dc_brightness_min_open_fs);
744 static struct file_operations acpi_pcc_brightness_fops =
745 SEQ_FILEOPS_RW(acpi_pcc_brightness_open_fs,
746 acpi_pcc_write_no_brightness);
747 static struct file_operations acpi_pcc_sticky_key_fops =
748 SEQ_FILEOPS_RW(acpi_pcc_sticky_key_open_fs, acpi_pcc_write_sticky_key);
749 static struct file_operations acpi_pcc_keyinput_fops =
750 SEQ_FILEOPS_RW(acpi_pcc_keyinput_open_fs, acpi_pcc_write_keyinput);
751 static struct file_operations acpi_pcc_version_fops =
752 SEQ_FILEOPS_R(acpi_pcc_version_open_fs);
754 struct proc_item {
755 const char *name;
756 struct file_operations *fops;
757 mode_t flag;
760 /* Note: These functions map *exactly* to the SINF/SSET functions */
761 struct proc_item acpi_pcc_proc_items_sifr[] = {
762 { "num_batteries", &acpi_pcc_numbatteries_fops, S_IRUGO },
763 { "lcd_type", &acpi_pcc_lcdtype_fops, S_IRUGO },
764 { "ac_brightness_max", &acpi_pcc_ac_brightness_max_fops, S_IRUGO },
765 { "ac_brightness_min", &acpi_pcc_ac_brightness_min_fops, S_IRUGO },
766 { "ac_brightness", &acpi_pcc_ac_brightness_fops,
767 S_IFREG | S_IRUGO | S_IWUSR },
768 { "dc_brightness_max", &acpi_pcc_dc_brightness_max_fops, S_IRUGO },
769 { "dc_brightness_min", &acpi_pcc_dc_brightness_min_fops, S_IRUGO },
770 { "dc_brightness", &acpi_pcc_dc_brightness_fops,
771 S_IFREG | S_IRUGO | S_IWUSR },
772 { "brightness", &acpi_pcc_brightness_fops, S_IFREG | S_IRUGO | S_IWUSR},
773 { "mute", &acpi_pcc_mute_fops, S_IFREG | S_IRUGO | S_IWUSR },
774 { NULL, NULL, 0 },
777 struct proc_item acpi_pcc_proc_items[] = {
778 { "sticky_key", &acpi_pcc_sticky_key_fops, S_IFREG | S_IRUGO | S_IWUSR},
779 { "keyinput", &acpi_pcc_keyinput_fops, S_IFREG | S_IRUGO | S_IWUSR },
780 { "version", &acpi_pcc_version_fops, S_IRUGO },
781 { NULL, NULL, 0 },
784 static int __devinit acpi_pcc_add_device(struct acpi_device *device,
785 struct proc_item *proc_items,
786 int num)
788 struct acpi_hotkey *hotkey = acpi_driver_data(device);
789 struct proc_dir_entry *proc;
790 struct proc_item *item;
791 int i;
793 for (item = proc_items, i = 0; item->name && i < num; ++item, ++i) {
794 proc = create_proc_entry(item->name, item->flag,
795 hotkey->proc_dir_entry);
796 if (likely(proc)) {
797 proc->proc_fops = item->fops;
798 proc->data = hotkey;
799 proc->owner = THIS_MODULE;
800 } else {
801 while (i-- > 0) {
802 item--;
803 remove_proc_entry(item->name,
804 hotkey->proc_dir_entry);
806 return_VALUE(-ENODEV);
809 return_VALUE(0);
812 static int __devinit acpi_pcc_proc_init(struct acpi_device *device)
814 struct proc_dir_entry *acpi_pcc_dir;
815 struct acpi_hotkey *hotkey = acpi_driver_data(device);
816 acpi_status status;
818 ACPI_FUNCTION_TRACE("acpi_pcc_proc_init");
820 acpi_pcc_dir = proc_mkdir(PROC_PCC, acpi_root_dir);
822 if (unlikely(!acpi_pcc_dir)) {
823 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
824 "Couldn't create dir in /proc\n"));
825 return_VALUE(-ENODEV);
828 acpi_pcc_dir->owner = THIS_MODULE;
829 hotkey->proc_dir_entry = acpi_pcc_dir;
831 status = acpi_pcc_add_device(device, acpi_pcc_proc_items_sifr,
832 hotkey->num_sifr);
833 status |= acpi_pcc_add_device(device, acpi_pcc_proc_items,
834 ARRAY_SIZE(acpi_pcc_proc_items));
835 if (unlikely(status)) {
836 remove_proc_entry(PROC_PCC, acpi_root_dir);
837 hotkey->proc_dir_entry = NULL;
838 return_VALUE(-ENODEV);
841 return_VALUE(status);
844 static void __devexit acpi_pcc_remove_device(struct acpi_device *device,
845 struct proc_item *proc_items,
846 int num)
848 struct acpi_hotkey *hotkey = acpi_driver_data(device);
849 struct proc_item *item;
850 int i;
852 for (item = proc_items, i = 0;
853 item->name != NULL && i < num;
854 ++item, ++i) {
855 remove_proc_entry(item->name, hotkey->proc_dir_entry);
858 return_VOID;
861 /* *************************************************************************
862 Power Management
863 ************************************************************************* */
864 #ifdef CONFIG_PM
865 static int acpi_pcc_hotkey_resume(struct acpi_device *device)
867 struct acpi_hotkey *hotkey = acpi_driver_data(device);
868 acpi_status status = AE_OK;
870 ACPI_FUNCTION_TRACE("acpi_pcc_hotkey_resume");
872 if (device == NULL || hotkey == NULL)
873 return_VALUE(-EINVAL);
875 if (hotkey->num_sifr != 0) {
876 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Sticky mode restore: %d\n",
877 hotkey->sticky_mode));
879 status = acpi_pcc_write_sset(hotkey, SINF_STICKY_KEY,
880 hotkey->sticky_mode);
882 if (status != AE_OK)
883 return_VALUE(-EINVAL);
885 return_VALUE(0);
887 #endif
889 /* *************************************************************************
890 Module init/remove
891 ************************************************************************* */
892 /* -------------------------------------------------------------------------
893 input
894 ------------------------------------------------------------------------- */
895 static int __devinit acpi_pcc_init_input(struct acpi_hotkey *hotkey)
897 struct input_dev *hotk_input_dev;
898 struct pcc_keyinput *pcc_keyinput;
899 int error;
901 ACPI_FUNCTION_TRACE("acpi_pcc_init_input");
903 hotk_input_dev = input_allocate_device();
904 if (hotk_input_dev == NULL) {
905 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
906 "Couldn't allocate input device for hotkey"));
907 goto err_input;
910 pcc_keyinput = kcalloc(1, sizeof(struct pcc_keyinput), GFP_KERNEL);
912 if (pcc_keyinput == NULL) {
913 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
914 "Couldn't allocate mem for private data"));
915 goto err_pcc;
918 hotk_input_dev->evbit[0] = BIT(EV_KEY);
920 set_bit(KEY_BRIGHTNESSDOWN, hotk_input_dev->keybit);
921 set_bit(KEY_BRIGHTNESSUP, hotk_input_dev->keybit);
922 set_bit(KEY_MUTE, hotk_input_dev->keybit);
923 set_bit(KEY_VOLUMEDOWN, hotk_input_dev->keybit);
924 set_bit(KEY_VOLUMEUP, hotk_input_dev->keybit);
925 set_bit(KEY_SLEEP, hotk_input_dev->keybit);
926 set_bit(KEY_BATT, hotk_input_dev->keybit);
927 set_bit(KEY_SUSPEND, hotk_input_dev->keybit);
929 hotk_input_dev->name = ACPI_PCC_DRIVER_NAME;
930 hotk_input_dev->phys = ACPI_PCC_INPUT_PHYS;
931 hotk_input_dev->id.bustype = BUS_PCC_HOTKEY;
932 hotk_input_dev->id.vendor = 0x0001;
933 hotk_input_dev->id.product = 0x0001;
934 hotk_input_dev->id.version = 0x0100;
936 pcc_keyinput->key_mode = PCC_KEYINPUT_MODE;
937 pcc_keyinput->hotkey = hotkey;
939 input_set_drvdata(hotk_input_dev, pcc_keyinput);
941 hotkey->input_dev = hotk_input_dev;
943 error = input_register_device(hotk_input_dev);
945 if (error)
946 goto err_pcc;
948 return_VALUE(0);
950 err_pcc:
951 input_unregister_device(hotk_input_dev);
952 err_input:
953 return_VALUE(-ENOMEM);
956 static void __devexit acpi_pcc_remove_input(struct acpi_hotkey *hotkey)
958 struct input_dev *hotk_input_dev;
959 struct pcc_keyinput *pcc_keyinput;
961 ACPI_FUNCTION_TRACE("acpi_pcc_remove_input");
963 if (hotkey == NULL) {
964 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Can't free memory"));
965 return_VOID;
968 hotk_input_dev = hotkey->input_dev;
969 pcc_keyinput = input_get_drvdata(hotk_input_dev);
971 input_unregister_device(hotk_input_dev);
973 kfree(pcc_keyinput);
976 /* -------------------------------------------------------------------------
977 ACPI
978 ------------------------------------------------------------------------- */
979 static int __devinit acpi_pcc_hotkey_add(struct acpi_device *device)
981 acpi_status status = AE_OK;
982 struct acpi_hotkey *hotkey = NULL;
983 int sifr_status, num_sifr, result;
985 ACPI_FUNCTION_TRACE("acpi_pcc_hotkey_add");
987 if (device == NULL)
988 return_VALUE(-EINVAL);
990 sifr_status = acpi_pcc_get_sqty(device);
992 if (sifr_status > 255) {
993 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "num_sifr too large"));
994 return_VALUE(-ENODEV);
997 if (sifr_status < 0) {
998 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "not support SQTY"));
999 num_sifr = 0;
1000 } else {
1001 num_sifr = sifr_status;
1004 hotkey = kcalloc(1, sizeof(struct acpi_hotkey), GFP_KERNEL);
1005 if (hotkey == NULL) {
1006 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1007 "Couldn't allocate mem for hotkey"));
1008 return_VALUE(-ENOMEM);
1011 hotkey->device = device;
1012 hotkey->handle = device->handle;
1013 hotkey->num_sifr = num_sifr;
1014 device->driver_data = hotkey;
1015 strcpy(acpi_device_name(device), ACPI_PCC_DEVICE_NAME);
1016 strcpy(acpi_device_class(device), ACPI_PCC_CLASS);
1018 status = acpi_install_notify_handler(hotkey->handle,
1019 ACPI_DEVICE_NOTIFY,
1020 acpi_pcc_hotkey_notify,
1021 hotkey);
1023 if (ACPI_FAILURE(status)) {
1024 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1025 "Error installing notify handler\n"));
1026 kfree(hotkey);
1027 return_VALUE(-ENODEV);
1030 result = acpi_pcc_init_input(hotkey);
1031 if (result != 0) {
1032 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1033 "Error installing keyinput handler\n"));
1034 kfree(hotkey);
1035 return_VALUE(result);
1038 return_VALUE(acpi_pcc_proc_init(device));
1041 static int __devexit acpi_pcc_hotkey_remove(struct acpi_device *device,
1042 int type)
1044 acpi_status status = AE_OK;
1045 struct acpi_hotkey *hotkey = acpi_driver_data(device);
1047 ACPI_FUNCTION_TRACE("acpi_pcc_hotkey_remove");
1049 if (!device || !hotkey)
1050 return_VALUE(-EINVAL);
1052 if (hotkey->proc_dir_entry) {
1053 acpi_pcc_remove_device(device, acpi_pcc_proc_items_sifr,
1054 hotkey->num_sifr);
1055 acpi_pcc_remove_device(device, acpi_pcc_proc_items,
1056 ARRAY_SIZE(acpi_pcc_proc_items));
1057 remove_proc_entry(PROC_PCC, acpi_root_dir);
1060 status = acpi_remove_notify_handler(hotkey->handle,
1061 ACPI_DEVICE_NOTIFY, acpi_pcc_hotkey_notify);
1063 if (ACPI_FAILURE(status)) {
1064 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1065 "Error removing notify handler\n"));
1068 acpi_pcc_remove_input(hotkey);
1069 kfree(hotkey);
1070 return_VALUE(status == AE_OK);
1073 /* *********************************************************************
1074 Module entry point
1075 ********************************************************************* */
1076 static int __init acpi_pcc_init(void)
1078 int result;
1080 ACPI_FUNCTION_TRACE("acpi_pcc_init");
1082 printk(KERN_INFO LOGPREFIX "loading...\n");
1084 if (acpi_disabled) {
1085 printk(KERN_INFO LOGPREFIX "ACPI disabled.\n");
1086 return_VALUE(-ENODEV);
1089 result = acpi_bus_register_driver(&acpi_pcc_driver);
1090 if (result < 0) {
1091 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1092 "Error registering hotkey driver\n"));
1093 return_VALUE(-ENODEV);
1096 return_VALUE(result);
1099 static void __exit acpi_pcc_exit(void)
1101 ACPI_FUNCTION_TRACE("acpi_pcc_exit");
1103 printk(KERN_INFO LOGPREFIX "unloading...\n");
1105 acpi_bus_unregister_driver(&acpi_pcc_driver);
1107 return_VOID;
1110 module_init(acpi_pcc_init);
1111 module_exit(acpi_pcc_exit);