]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/staging/pcc-acpi/pcc-acpi.c
e44181ead26d837afd39b805871821b7985f532d
[linux-2.6-omap-h63xx.git] / drivers / staging / pcc-acpi / pcc-acpi.c
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>
7  *
8  *  derived from toshiba_acpi.c, Copyright (C) 2002-2004 John Belmonte
9  *
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.
13  *
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.
18  *
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
22  *
23  *---------------------------------------------------------------------------
24  *
25  * ChangeLog:
26  *
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 allow HAL to access.
31  *                      merge dbronaugh's enhancement
32  *                      Aug.17, 2004 David Bronaugh (dbronaugh)
33  *                              - Added screen brightness setting interface
34  *                                Thanks to the FreeBSD crew (acpi_panasonic.c authors)
35  *                                for the ideas I needed to accomplish it
36  *
37  *      May.29, 2006    Hiroshi Miura <miura@da-cha.org>
38  *              -v0.8.4 follow to change keyinput structure
39  *                      thanks Fabian Yamaguchi <fabs@cs.tu-berlin.de>,
40  *                      Jacob Bower <jacob.bower@ic.ac.uk> and
41  *                      Hiroshi Yokota for providing solutions.
42  *
43  *      Oct.02, 2004    Hiroshi Miura <miura@da-cha.org>
44  *              -v0.8.2 merge code of YOKOTA Hiroshi <yokota@netlab.is.tsukuba.ac.jp>.
45  *                      Add sticky key mode interface.
46  *                      Refactoring acpi_pcc_generete_keyinput().
47  *
48  *      Sep.15, 2004    Hiroshi Miura <miura@da-cha.org>
49  *              -v0.8   Generate key input event on input subsystem.
50  *                      This is based on yet another driver written by Ryuta Nakanishi.
51  *
52  *      Sep.10, 2004    Hiroshi Miura <miura@da-cha.org>
53  *              -v0.7   Change proc interface functions using seq_file
54  *                      facility as same as other ACPI drivers.
55  *
56  *      Aug.28, 2004    Hiroshi Miura <miura@da-cha.org>
57  *              -v0.6.4 Fix a silly error with status checking
58  *
59  *      Aug.25, 2004    Hiroshi Miura <miura@da-cha.org>
60  *              -v0.6.3 replace read_acpi_int by standard function acpi_evaluate_integer
61  *                      some clean up and make smart copyright notice.
62  *                      fix return value of pcc_acpi_get_key()
63  *                      fix checking return value of acpi_bus_register_driver()
64  *
65  *      Aug.22, 2004    David Bronaugh <dbronaugh@linuxboxen.org>
66  *              -v0.6.2 Add check on ACPI data (num_sifr)
67  *                      Coding style cleanups, better error messages/handling
68  *                      Fixed an off-by-one error in memory allocation
69  *
70  *      Aug.21, 2004    David Bronaugh <dbronaugh@linuxboxen.org>
71  *              -v0.6.1 Fix a silly error with status checking
72  *
73  *      Aug.20, 2004    David Bronaugh <dbronaugh@linuxboxen.org>
74  *              - v0.6  Correct brightness controls to reflect reality
75  *                      based on information gleaned by Hiroshi Miura
76  *                      and discussions with Hiroshi Miura
77  *
78  *      Aug.10, 2004    Hiroshi Miura <miura@da-cha.org>
79  *              - v0.5  support LCD brightness control
80  *                      based on the disclosed information by MEI.
81  *
82  *      Jul.25, 2004    Hiroshi Miura <miura@da-cha.org>
83  *              - v0.4  first post version
84  *                      add function to retrive SIFR
85  *
86  *      Jul.24, 2004    Hiroshi Miura <miura@da-cha.org>
87  *              - v0.3  get proper status of hotkey
88  *
89  *      Jul.22, 2004    Hiroshi Miura <miura@da-cha.org>
90  *              - v0.2  add HotKey handler
91  *
92  *      Jul.17, 2004    Hiroshi Miura <miura@da-cha.org>
93  *              - v0.1  start from toshiba_acpi driver written by John Belmonte
94  *
95  */
96
97 #define ACPI_PCC_VERSION        "0.9+hy"
98
99 #include <acpi/acpi_bus.h>
100 #include <acpi/acpi_drivers.h>
101 #include <asm/uaccess.h>
102 #include <linux/ctype.h>
103 #include <linux/init.h>
104 #include <linux/input.h>
105 #include <linux/kernel.h>
106 #include <linux/module.h>
107 #include <linux/proc_fs.h>
108 #include <linux/seq_file.h>
109 #include <linux/slab.h>
110 #include <linux/types.h>
111 #include <linux/version.h>
112
113
114 /*************************************************************************
115  * "seq" file template definition.
116  */
117 /* "seq" initializer */
118 #define SEQ_OPEN_FS(_open_func_name_, _show_func_name_) \
119 static int _open_func_name_(struct inode *inode, struct file *file) \
120 {                                                                     \
121         return single_open(file, _show_func_name_, PDE(inode)->data);  \
122 }
123
124 /*-------------------------------------------------------------------------
125  * "seq" fops template for read-only files.
126  */
127 #define SEQ_FILEOPS_R(_open_func_name_) \
128 { \
129         .open    = _open_func_name_,              \
130         .read    = seq_read,                      \
131         .llseek  = seq_lseek,                     \
132         .release = single_release,                \
133 }
134
135 /*------------------------------------------------------------------------
136  * "seq" fops template for read-write files.
137  */
138 #define SEQ_FILEOPS_RW(_open_func_name_, _write_func_name_) \
139 { \
140         .open    = _open_func_name_ ,             \
141         .read    = seq_read,                      \
142         .write   = _write_func_name_,             \
143         .llseek  = seq_lseek,                     \
144         .release = single_release,                \
145 }
146
147 /*
148  * "seq" file template definition ended.
149  ***************************************************************************
150  */
151 #ifndef ACPI_HOTKEY_COMPONENT
152 #define ACPI_HOTKEY_COMPONENT   0x10000000
153 #endif
154
155 #define _COMPONENT              ACPI_HOTKEY_COMPONENT
156 ACPI_MODULE_NAME("pcc_acpi");
157
158 MODULE_AUTHOR("Hiroshi Miura, Hiroshi Yokota");
159 MODULE_DESCRIPTION("ACPI HotKey driver for Panasonic Let's Note laptops");
160 MODULE_LICENSE("GPL");
161
162 #define LOGPREFIX "pcc_acpi: "
163
164 /****************************************************
165  * Define ACPI PATHs
166  ****************************************************/
167 /* Lets note hotkeys */
168 #define METHOD_HKEY_QUERY       "HINF"
169 #define METHOD_HKEY_SQTY        "SQTY"
170 #define METHOD_HKEY_SINF        "SINF"
171 #define METHOD_HKEY_SSET        "SSET"
172 #define HKEY_NOTIFY              0x80
173
174 /* for brightness control */
175 #define LCD_MAX_BRIGHTNESS 255
176 /* This may be magical -- beware */
177 #define LCD_BRIGHTNESS_INCREMENT 17
178 /* Registers of SINF */
179 #define SINF_LCD_BRIGHTNESS 4
180
181 /*******************************************************************
182  *
183  * definitions for /proc/ interface
184  *
185  *******************************************************************/
186 #define ACPI_PCC_DRIVER_NAME    "pcc_acpi"
187 #define ACPI_PCC_DEVICE_NAME    "PCCExtra"
188 #define ACPI_PCC_CLASS          "pcc"
189 #define PROC_PCC                ACPI_PCC_CLASS
190
191 #define ACPI_PCC_INPUT_PHYS     "panasonic/hkey0"
192
193 /* This is transitional definition */
194 #ifndef KEY_BATT
195 # define KEY_BATT 227
196 #endif
197
198 #define PROC_STR_MAX_LEN  8
199
200 #define BUS_PCC_HOTKEY BUS_I8042 /*0x1a*/ /* FIXME: BUS_I8042? */
201
202 /* Fn+F4/F5 confricts with Shift+F1/F2  */
203 /* This hack avoids key number confrict */
204 #define PCC_KEYINPUT_MODE (0)
205
206 /* LCD_TYPEs: 0 = Normal, 1 = Semi-transparent
207    ENV_STATEs: Normal temp=0x01, High temp=0x81, N/A=0x00
208 */
209 enum SINF_BITS { SINF_NUM_BATTERIES = 0,
210                  SINF_LCD_TYPE,      /* 1 */
211                  SINF_AC_MAX_BRIGHT, SINF_AC_MIN_BRIGHT, SINF_AC_CUR_BRIGHT,  /* 2, 3, 4 */
212                              /* 4 = R1 only handle SINF_AC_CUR_BRIGHT as SINF_CUR_BRIGHT and don't know AC state */
213                  SINF_DC_MAX_BRIGHT, SINF_DC_MIN_BRIGHT, SINF_DC_CUR_BRIGHT,  /* 5, 6, 7 */
214                  SINF_MUTE,
215                  SINF_RESERVED,      SINF_ENV_STATE, /* 9, 10 */
216                  SINF_STICKY_KEY = 0x80,
217 };
218
219 static struct acpi_device_id pcc_device_ids[] = {
220         {"MAT0012", 0},
221         {"MAT0013", 0},
222         {"MAT0018", 0},
223         {"MAT0019", 0},
224         {"",        0},
225 };
226 MODULE_DEVICE_TABLE(acpi, pcc_device_ids);
227
228
229 static int __devinit acpi_pcc_hotkey_add(struct acpi_device *device);
230 static int __devexit acpi_pcc_hotkey_remove(struct acpi_device *device, int type);
231 static int acpi_pcc_hotkey_resume(struct acpi_device *device);
232
233
234 static struct acpi_driver acpi_pcc_driver = {
235         .name =         ACPI_PCC_DRIVER_NAME,
236         .class =        ACPI_PCC_CLASS,
237         .ids =          pcc_device_ids,
238         .ops =          {
239                                 .add =          acpi_pcc_hotkey_add,
240                                 .remove =       __devexit_p(acpi_pcc_hotkey_remove),
241 #ifdef CONFIG_PM
242                                 /*.suspend =      acpi_pcc_hotkey_suspend,*/
243                                 .resume =       acpi_pcc_hotkey_resume,
244 #endif
245                         },
246 };
247
248 struct acpi_hotkey {
249         acpi_handle             handle;
250         struct acpi_device      *device;
251         struct proc_dir_entry   *proc_dir_entry;
252         unsigned long           num_sifr;
253         unsigned long           status;
254         struct input_dev        *input_dev;
255         int                     sticky_mode;
256 };
257
258 struct pcc_keyinput {
259         struct acpi_hotkey      *hotkey;
260         int key_mode;
261 };
262
263 /* *************************************************************************
264    Hotkey driver core
265    ************************************************************************* */
266 /* -------------------------------------------------------------------------
267                            method access functions
268    ------------------------------------------------------------------------- */
269 static int acpi_pcc_write_sset(struct acpi_hotkey *hotkey, int func, int val)
270 {
271         union acpi_object in_objs[] = {
272                 { .integer.type  = ACPI_TYPE_INTEGER,
273                   .integer.value = func, },
274                 { .integer.type  = ACPI_TYPE_INTEGER,
275                   .integer.value = val, },
276         };
277         struct acpi_object_list params = {
278                 .count   = ARRAY_SIZE(in_objs),
279                 .pointer = in_objs,
280         };
281         acpi_status status;
282
283         ACPI_FUNCTION_TRACE("acpi_pcc_write_sset");
284
285         status = acpi_evaluate_object(hotkey->handle, METHOD_HKEY_SSET, &params, NULL);
286
287         return_VALUE(status == AE_OK ? AE_OK : AE_ERROR);
288 }
289
290 static inline int acpi_pcc_get_sqty(struct acpi_device *device)
291 {
292         unsigned long s;
293         acpi_status status;
294
295         ACPI_FUNCTION_TRACE("acpi_pcc_get_sqty");
296
297         status = acpi_evaluate_integer(device->handle, METHOD_HKEY_SQTY, NULL, &s);
298         if (ACPI_SUCCESS(status)) {
299                 return_VALUE(s);
300         } else {
301                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
302                                   "evaluation error HKEY.SQTY\n"));
303                 return_VALUE(-EINVAL);
304         }
305 }
306
307 static int acpi_pcc_retrieve_biosdata(struct acpi_hotkey *hotkey, u32* sinf)
308 {
309         acpi_status status;
310         struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
311         union acpi_object *hkey = NULL;
312         int i;
313
314         ACPI_FUNCTION_TRACE("acpi_pcc_retrieve_biosdata");
315
316         status = acpi_evaluate_object(hotkey->handle, METHOD_HKEY_SINF, 0 , &buffer);
317         if (ACPI_FAILURE(status)) {
318                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "evaluation error HKEY.SINF\n"));
319                 status = AE_ERROR;
320                 return_VALUE(status);
321         }
322
323         hkey = buffer.pointer;
324         if (!hkey || (hkey->type != ACPI_TYPE_PACKAGE)) {
325                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid HKEY.SINF\n"));
326                 goto free_buffer;
327         }
328
329         if (hotkey->num_sifr < hkey->package.count) {
330                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "SQTY reports bad SINF length\n"));
331                 status = AE_ERROR;
332                 goto free_buffer;
333         }
334
335         for (i = 0; i < hkey->package.count; i++) {
336                 union acpi_object *element = &(hkey->package.elements[i]);
337                 if (likely(element->type == ACPI_TYPE_INTEGER)) {
338                         sinf[i] = element->integer.value;
339                 } else {
340                         ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid HKEY.SINF data\n"));
341                         status = AE_ERROR;
342                         break;
343                 }
344         }
345         sinf[hkey->package.count] = -1;
346
347  free_buffer:
348         kfree(buffer.pointer);
349         return_VALUE(status == AE_OK ? AE_OK : AE_ERROR);
350 }
351
352 static int acpi_pcc_read_sinf_field(struct seq_file *seq, int field)
353 {
354         struct acpi_hotkey *hotkey = (struct acpi_hotkey *) seq->private;
355         u32 sinf[hotkey->num_sifr + 1];
356
357         ACPI_FUNCTION_TRACE("acpi_pcc_read_sinf_field");
358
359         if (ACPI_SUCCESS(acpi_pcc_retrieve_biosdata(hotkey, sinf))) {
360                 seq_printf(seq, "%u\n", sinf[field]);
361         } else {
362                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Couldn't retrieve BIOS data\n"));
363         }
364
365         return_VALUE(AE_OK);
366 }
367
368 /* -------------------------------------------------------------------------
369                        user interface functions
370    ------------------------------------------------------------------------- */
371 /* read methods */
372 /* Sinf read methods */
373 #define PCC_SINF_READ_F(_name_, FUNC) \
374 static int _name_  (struct seq_file *seq, void *offset) \
375 { \
376         return_VALUE(ACPI_SUCCESS(acpi_pcc_read_sinf_field(seq, (FUNC)))  ? 0 : -EINVAL); \
377 }
378
379 PCC_SINF_READ_F(acpi_pcc_numbatteries_show,      SINF_NUM_BATTERIES);
380 PCC_SINF_READ_F(acpi_pcc_lcdtype_show,           SINF_LCD_TYPE);
381 PCC_SINF_READ_F(acpi_pcc_ac_brightness_max_show, SINF_AC_MAX_BRIGHT);
382 PCC_SINF_READ_F(acpi_pcc_ac_brightness_min_show, SINF_AC_MIN_BRIGHT);
383 PCC_SINF_READ_F(acpi_pcc_ac_brightness_show,     SINF_AC_CUR_BRIGHT);
384 PCC_SINF_READ_F(acpi_pcc_dc_brightness_max_show, SINF_DC_MAX_BRIGHT);
385 PCC_SINF_READ_F(acpi_pcc_dc_brightness_min_show, SINF_DC_MIN_BRIGHT);
386 PCC_SINF_READ_F(acpi_pcc_dc_brightness_show,     SINF_DC_CUR_BRIGHT);
387 PCC_SINF_READ_F(acpi_pcc_brightness_show,        SINF_AC_CUR_BRIGHT);
388 PCC_SINF_READ_F(acpi_pcc_mute_show,              SINF_MUTE);
389
390 static int acpi_pcc_sticky_key_show(struct seq_file *seq, void *offset)
391 {
392         struct acpi_hotkey *hotkey = seq->private;
393
394         ACPI_FUNCTION_TRACE("acpi_pcc_sticky_key_show");
395
396         if (!hotkey || !hotkey->device) {
397                 return_VALUE(-EINVAL);
398         }
399
400         seq_printf(seq, "%d\n", hotkey->sticky_mode);
401
402         return_VALUE(0);
403 }
404
405 static int acpi_pcc_keyinput_show(struct seq_file *seq, void *offset)
406 {
407         struct acpi_hotkey      *hotkey = (struct acpi_hotkey *) seq->private;
408         struct input_dev        *hotk_input_dev = hotkey->input_dev;
409         struct pcc_keyinput     *keyinput = input_get_drvdata(hotk_input_dev);
410
411         ACPI_FUNCTION_TRACE("acpi_pcc_keyinput_show");
412
413         seq_printf(seq, "%d\n", keyinput->key_mode);
414
415         return_VALUE(0);
416 }
417
418 static int acpi_pcc_version_show(struct seq_file *seq, void *offset)
419 {
420         struct acpi_hotkey *hotkey = (struct acpi_hotkey *) seq->private;
421
422         ACPI_FUNCTION_TRACE("acpi_pcc_version_show");
423
424         if (!hotkey || !hotkey->device) {
425                 return_VALUE(-EINVAL);
426         }
427
428         seq_printf(seq, "%s version %s\n", ACPI_PCC_DRIVER_NAME, ACPI_PCC_VERSION);
429         seq_printf(seq, "%li functions\n", hotkey->num_sifr);
430
431         return_VALUE(0);
432 }
433
434 /* write methods */
435 static ssize_t acpi_pcc_write_single_flag (struct file *file,
436                                            const char __user *buffer,
437                                            size_t count,
438                                            int sinf_func)
439 {
440         struct seq_file         *seq = file->private_data;
441         struct acpi_hotkey      *hotkey = seq->private;
442         char                    write_string[PROC_STR_MAX_LEN];
443         u32                     val;
444
445         ACPI_FUNCTION_TRACE("acpi_pcc_write_single_flag");
446
447         if (!hotkey || (count > sizeof(write_string) - 1)) {
448                 return_VALUE(-EINVAL);
449         }
450
451         if (copy_from_user(write_string, buffer, count)) {
452                 return_VALUE(-EFAULT);
453         }
454         write_string[count] = '\0';
455
456         if ((sscanf(write_string, "%3i", &val) == 1) &&
457             (val == 0 || val == 1)) {
458                 acpi_pcc_write_sset(hotkey, sinf_func, val);
459         }
460
461         return_VALUE(count);
462 }
463
464 static unsigned long acpi_pcc_write_brightness(struct file *file, const char __user *buffer,
465                                                size_t count,
466                                                int min_index, int max_index,
467                                                int cur_index)
468 {
469         struct seq_file         *seq = (struct seq_file *)file->private_data;
470         struct acpi_hotkey      *hotkey = (struct acpi_hotkey *)seq->private;
471         char                    write_string[PROC_STR_MAX_LEN];
472         u32 bright;
473         u32 sinf[hotkey->num_sifr + 1];
474
475         ACPI_FUNCTION_TRACE("acpi_pcc_write_brightness");
476
477         if (!hotkey || (count > sizeof(write_string) - 1)) {
478                 return_VALUE(-EINVAL);
479         }
480
481         if (copy_from_user(write_string, buffer, count)) {
482                 return_VALUE(-EFAULT);
483         }
484
485         write_string[count] = '\0';
486
487         if (ACPI_FAILURE(acpi_pcc_retrieve_biosdata(hotkey, sinf))) {
488                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Couldn't retrieve BIOS data\n"));
489                 goto end;
490         }
491
492         if ((sscanf(write_string, "%4i", &bright) == 1) &&
493             (bright >= sinf[min_index]                ) &&
494             (bright <= sinf[max_index]                )) {
495                 acpi_pcc_write_sset(hotkey, cur_index, bright);
496         }
497
498 end:
499         return_VALUE(count);
500 }
501
502 static ssize_t acpi_pcc_write_ac_brightness(struct file *file, const char __user *buffer,
503                                          size_t count, loff_t *ppos)
504 {
505         return_VALUE(acpi_pcc_write_brightness(file, buffer, count,
506                                                SINF_AC_MIN_BRIGHT,
507                                                SINF_AC_MAX_BRIGHT,
508                                                SINF_AC_CUR_BRIGHT));
509 }
510
511 static ssize_t acpi_pcc_write_dc_brightness(struct file *file, const char __user *buffer,
512                                          size_t count, loff_t *ppos)
513 {
514         return_VALUE(acpi_pcc_write_brightness(file, buffer, count,
515                                                SINF_DC_MIN_BRIGHT,
516                                                SINF_DC_MAX_BRIGHT,
517                                                SINF_DC_CUR_BRIGHT));
518 }
519
520 static ssize_t acpi_pcc_write_no_brightness(struct file *file, const char __user *buffer,
521                                          size_t count, loff_t *ppos)
522 {
523         return acpi_pcc_write_brightness(file, buffer, count, SINF_AC_MIN_BRIGHT,
524                                          SINF_AC_MAX_BRIGHT,
525                                          SINF_AC_CUR_BRIGHT);
526 }
527
528 static ssize_t acpi_pcc_write_mute (struct file *file,
529                                     const char __user *buffer,
530                                     size_t count, loff_t *ppos)
531 {
532         return_VALUE(acpi_pcc_write_single_flag(file, buffer, count, SINF_MUTE));
533 }
534
535 static ssize_t acpi_pcc_write_sticky_key (struct file *file,
536                                           const char __user *buffer,
537                                           size_t count, loff_t *ppos)
538 {
539         struct seq_file     *seq = (struct seq_file *)file->private_data;
540         struct acpi_hotkey  *hotkey = (struct acpi_hotkey *)seq->private;
541         char                 write_string[PROC_STR_MAX_LEN];
542         int                  mode;
543
544         ACPI_FUNCTION_TRACE("acpi_pcc_write_sticky_key");
545
546         if (!hotkey || (count > sizeof(write_string) - 1)) {
547                 return_VALUE(-EINVAL);
548         }
549
550         if (copy_from_user(write_string, buffer, count)) {
551                 return_VALUE(-EFAULT);
552         }
553         write_string[count] = '\0';
554
555         if ((sscanf(write_string, "%3i", &mode) == 1) &&
556             (mode == 0 || mode == 1)) {
557                 acpi_pcc_write_sset(hotkey, SINF_STICKY_KEY, mode);
558                 hotkey->sticky_mode = mode;
559         }
560
561         return_VALUE(count);
562 }
563
564 static ssize_t acpi_pcc_write_keyinput(struct file *file, const char __user *buffer,
565                                          size_t count, loff_t *ppos)
566 {
567         struct seq_file         *seq = (struct seq_file *)file->private_data;
568         struct acpi_hotkey      *hotkey = (struct acpi_hotkey *)seq->private;
569         struct pcc_keyinput     *keyinput;
570         char                    write_string[PROC_STR_MAX_LEN];
571         int                     key_mode;
572
573         ACPI_FUNCTION_TRACE("acpi_pcc_write_keyinput");
574
575         if (!hotkey || (count > (sizeof(write_string) - 1))) {
576                 return_VALUE(-EINVAL);
577         }
578
579         if (copy_from_user(write_string, buffer, count)) {
580                 return_VALUE(-EFAULT);
581         }
582
583         write_string[count] = '\0';
584
585         if ((sscanf(write_string, "%4i", &key_mode) == 1) &&
586             (key_mode == 0 || key_mode == 1)) {
587                 keyinput = input_get_drvdata(hotkey->input_dev);
588                 keyinput->key_mode = key_mode;
589         }
590
591         return_VALUE(count);
592 }
593
594 /* -------------------------------------------------------------------------
595                             hotkey driver
596    ------------------------------------------------------------------------- */
597 static void acpi_pcc_generete_keyinput(struct acpi_hotkey *hotkey)
598 {
599         struct input_dev    *hotk_input_dev = hotkey->input_dev;
600         struct pcc_keyinput *keyinput = input_get_drvdata(hotk_input_dev);
601         int hinf = hotkey->status;
602         int key_code, hkey_num;
603         const int key_map[] = {
604                 /*  0 */ -1,
605                 /*  1 */ KEY_BRIGHTNESSDOWN,
606                 /*  2 */ KEY_BRIGHTNESSUP,
607                 /*  3 */ -1, /* vga/lcd switch event is not occur on hotkey driver. */
608                 /*  4 */ KEY_MUTE,
609                 /*  5 */ KEY_VOLUMEDOWN,
610                 /*  6 */ KEY_VOLUMEUP,
611                 /*  7 */ KEY_SLEEP,
612                 /*  8 */ -1, /* Change CPU boost: do nothing */
613                 /*  9 */ KEY_BATT,
614                 /* 10 */ KEY_SUSPEND,
615         };
616
617         ACPI_FUNCTION_TRACE("acpi_pcc_generete_keyinput");
618
619         if (keyinput->key_mode == 0) { return_VOID; }
620
621         hkey_num = hinf & 0xf;
622
623         if ((       0 > hkey_num           ) ||
624             (hkey_num > ARRAY_SIZE(key_map))) {
625                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
626                                   "hotkey number out of range: %d\n",
627                                   hkey_num));
628                 return_VOID;
629         }
630
631         key_code = key_map[hkey_num];
632
633         if (key_code != -1) {
634                 int pushed = (hinf & 0x80) ? TRUE : FALSE;
635
636                 input_report_key(hotk_input_dev, key_code, pushed);
637                 input_sync(hotk_input_dev);
638         }
639 }
640
641 static int acpi_pcc_hotkey_get_key(struct acpi_hotkey *hotkey)
642 {
643         unsigned long result;
644         acpi_status status = AE_OK;
645
646         ACPI_FUNCTION_TRACE("acpi_pcc_hotkey_get_key");
647
648         status = acpi_evaluate_integer(hotkey->handle, METHOD_HKEY_QUERY, NULL, &result);
649         if (likely(ACPI_SUCCESS(status))) {
650                 hotkey->status = result;
651         } else {
652                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "error getting hotkey status\n"));
653         }
654
655         return_VALUE(status == AE_OK);
656 }
657
658 void acpi_pcc_hotkey_notify(acpi_handle handle, u32 event, void *data)
659 {
660         struct acpi_hotkey *hotkey = (struct acpi_hotkey *) data;
661
662         ACPI_FUNCTION_TRACE("acpi_pcc_hotkey_notify");
663
664         switch(event) {
665         case HKEY_NOTIFY:
666                 if (acpi_pcc_hotkey_get_key(hotkey)) {
667                         /* generate event like '"pcc HKEY 00000080 00000084"' when Fn+F4 pressed */
668                         acpi_bus_generate_proc_event(hotkey->device, event, hotkey->status);
669                 }
670                 acpi_pcc_generete_keyinput(hotkey);
671                 break;
672         default:
673                 /* nothing to do */
674                 break;
675         }
676         return_VOID;
677 }
678
679 /* *************************************************************************
680    FS Interface (/proc)
681    ************************************************************************* */
682 /* oepn proc file fs*/
683 SEQ_OPEN_FS(acpi_pcc_dc_brightness_open_fs,     acpi_pcc_dc_brightness_show);
684 SEQ_OPEN_FS(acpi_pcc_numbatteries_open_fs,      acpi_pcc_numbatteries_show);
685 SEQ_OPEN_FS(acpi_pcc_lcdtype_open_fs,           acpi_pcc_lcdtype_show);
686 SEQ_OPEN_FS(acpi_pcc_ac_brightness_max_open_fs, acpi_pcc_ac_brightness_max_show);
687 SEQ_OPEN_FS(acpi_pcc_ac_brightness_min_open_fs, acpi_pcc_ac_brightness_min_show);
688 SEQ_OPEN_FS(acpi_pcc_ac_brightness_open_fs,     acpi_pcc_ac_brightness_show);
689 SEQ_OPEN_FS(acpi_pcc_dc_brightness_max_open_fs, acpi_pcc_dc_brightness_max_show);
690 SEQ_OPEN_FS(acpi_pcc_dc_brightness_min_open_fs, acpi_pcc_dc_brightness_min_show);
691 SEQ_OPEN_FS(acpi_pcc_brightness_open_fs,        acpi_pcc_brightness_show);
692 SEQ_OPEN_FS(acpi_pcc_mute_open_fs,              acpi_pcc_mute_show);
693 SEQ_OPEN_FS(acpi_pcc_version_open_fs,           acpi_pcc_version_show);
694 SEQ_OPEN_FS(acpi_pcc_keyinput_open_fs,          acpi_pcc_keyinput_show);
695 SEQ_OPEN_FS(acpi_pcc_sticky_key_open_fs,        acpi_pcc_sticky_key_show);
696
697 typedef struct file_operations fops_t;
698 static fops_t acpi_pcc_numbatteries_fops      = SEQ_FILEOPS_R (acpi_pcc_numbatteries_open_fs);
699 static fops_t acpi_pcc_lcdtype_fops           = SEQ_FILEOPS_R (acpi_pcc_lcdtype_open_fs);
700 static fops_t acpi_pcc_mute_fops              = SEQ_FILEOPS_RW(acpi_pcc_mute_open_fs, acpi_pcc_write_mute);
701 static fops_t acpi_pcc_ac_brightness_fops     = SEQ_FILEOPS_RW(acpi_pcc_ac_brightness_open_fs, acpi_pcc_write_ac_brightness);
702 static fops_t acpi_pcc_ac_brightness_max_fops = SEQ_FILEOPS_R (acpi_pcc_ac_brightness_max_open_fs);
703 static fops_t acpi_pcc_ac_brightness_min_fops = SEQ_FILEOPS_R (acpi_pcc_ac_brightness_min_open_fs);
704 static fops_t acpi_pcc_dc_brightness_fops     = SEQ_FILEOPS_RW(acpi_pcc_dc_brightness_open_fs, acpi_pcc_write_dc_brightness);
705 static fops_t acpi_pcc_dc_brightness_max_fops = SEQ_FILEOPS_R (acpi_pcc_dc_brightness_max_open_fs);
706 static fops_t acpi_pcc_dc_brightness_min_fops = SEQ_FILEOPS_R (acpi_pcc_dc_brightness_min_open_fs);
707 static fops_t acpi_pcc_brightness_fops        = SEQ_FILEOPS_RW(acpi_pcc_brightness_open_fs, acpi_pcc_write_no_brightness);
708 static fops_t acpi_pcc_sticky_key_fops        = SEQ_FILEOPS_RW(acpi_pcc_sticky_key_open_fs, acpi_pcc_write_sticky_key);
709 static fops_t acpi_pcc_keyinput_fops          = SEQ_FILEOPS_RW(acpi_pcc_keyinput_open_fs, acpi_pcc_write_keyinput);
710 static fops_t acpi_pcc_version_fops           = SEQ_FILEOPS_R (acpi_pcc_version_open_fs);
711
712 typedef struct _ProcItem
713 {
714         const char* name;
715         struct file_operations *fops;
716         mode_t flag;
717 } ProcItem;
718
719 /* Note: These functions map *exactly* to the SINF/SSET functions */
720 ProcItem acpi_pcc_proc_items_sifr[] = {
721         { "num_batteries",      &acpi_pcc_numbatteries_fops,     S_IRUGO },
722         { "lcd_type",           &acpi_pcc_lcdtype_fops,          S_IRUGO },
723         { "ac_brightness_max" , &acpi_pcc_ac_brightness_max_fops,S_IRUGO },
724         { "ac_brightness_min" , &acpi_pcc_ac_brightness_min_fops,S_IRUGO },
725         { "ac_brightness" ,     &acpi_pcc_ac_brightness_fops,    S_IFREG | S_IRUGO | S_IWUSR },
726         { "dc_brightness_max" , &acpi_pcc_dc_brightness_max_fops,S_IRUGO },
727         { "dc_brightness_min" , &acpi_pcc_dc_brightness_min_fops,S_IRUGO },
728         { "dc_brightness" ,     &acpi_pcc_dc_brightness_fops,    S_IFREG | S_IRUGO | S_IWUSR },
729         { "brightness" ,        &acpi_pcc_brightness_fops,    S_IFREG | S_IRUGO | S_IWUSR },
730         { "mute",               &acpi_pcc_mute_fops,             S_IFREG | S_IRUGO | S_IWUSR },
731         { NULL, NULL, 0 },
732 };
733
734 ProcItem acpi_pcc_proc_items[] = {
735         { "sticky_key",         &acpi_pcc_sticky_key_fops,       S_IFREG | S_IRUGO | S_IWUSR },
736         { "keyinput",           &acpi_pcc_keyinput_fops,         S_IFREG | S_IRUGO | S_IWUSR },
737         { "version",            &acpi_pcc_version_fops,          S_IRUGO },
738         { NULL, NULL, 0 },
739 };
740
741 static int __devinit acpi_pcc_add_device(struct acpi_device *device,
742                                          ProcItem *proc_items,
743                                          int num)
744 {
745         struct acpi_hotkey *hotkey = (struct acpi_hotkey*)acpi_driver_data(device);
746         struct proc_dir_entry* proc;
747         ProcItem* item;
748         int i;
749
750         for (item = proc_items, i = 0; item->name && i < num; ++item, ++i) {
751                 proc = create_proc_entry(item->name, item->flag, hotkey->proc_dir_entry);
752                 if (likely(proc)) {
753                         proc->proc_fops = item->fops;
754                         proc->data = hotkey;
755                         proc->owner = THIS_MODULE;
756                 } else {
757                         while (i-- > 0) {
758                                 item--;
759                                 remove_proc_entry(item->name, hotkey->proc_dir_entry);
760                         }
761                         return_VALUE(-ENODEV);
762                 }
763         }
764         return_VALUE(0);
765 }
766
767 static int __devinit acpi_pcc_proc_init(struct acpi_device *device)
768 {
769         struct proc_dir_entry *acpi_pcc_dir;
770         struct acpi_hotkey    *hotkey = (struct acpi_hotkey*)acpi_driver_data(device);
771         acpi_status status;
772
773         ACPI_FUNCTION_TRACE("acpi_pcc_proc_init");
774
775         acpi_pcc_dir = proc_mkdir(PROC_PCC, acpi_root_dir);
776
777         if (unlikely(!acpi_pcc_dir)) {
778                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Couldn't create dir in /proc\n"));
779                 return_VALUE(-ENODEV);
780         }
781
782         acpi_pcc_dir->owner = THIS_MODULE;
783         hotkey->proc_dir_entry = acpi_pcc_dir;
784
785         status =  acpi_pcc_add_device(device, acpi_pcc_proc_items_sifr, hotkey->num_sifr);
786         status |= acpi_pcc_add_device(device, acpi_pcc_proc_items,      ARRAY_SIZE(acpi_pcc_proc_items));
787         if (unlikely(status)) {
788                 remove_proc_entry(PROC_PCC, acpi_root_dir);
789                 hotkey->proc_dir_entry = NULL;
790                 return_VALUE(-ENODEV);
791         }
792
793         return_VALUE(status);
794 }
795
796 static void __devexit acpi_pcc_remove_device(struct acpi_device *device,
797                                              ProcItem *proc_items,
798                                              int num)
799 {
800         struct acpi_hotkey *hotkey = (struct acpi_hotkey*)acpi_driver_data(device);
801         ProcItem* item;
802         int i;
803
804         for (item = proc_items, i = 0; item->name != NULL && i < num; ++item, ++i) {
805                 remove_proc_entry(item->name, hotkey->proc_dir_entry);
806         }
807
808         return_VOID;
809 }
810
811 /* *************************************************************************
812    Power Management
813    ************************************************************************* */
814 #ifdef CONFIG_PM
815 static int acpi_pcc_hotkey_resume(struct acpi_device *device)
816 {
817         struct acpi_hotkey *hotkey = acpi_driver_data(device);
818         acpi_status         status = AE_OK;
819
820         ACPI_FUNCTION_TRACE("acpi_pcc_hotkey_resume");
821
822         if (device == NULL || hotkey == NULL) { return_VALUE(-EINVAL); }
823
824         if (hotkey->num_sifr != 0) {
825                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Sticky mode restore: %d\n", hotkey->sticky_mode));
826
827                 status = acpi_pcc_write_sset(hotkey, SINF_STICKY_KEY, hotkey->sticky_mode);
828         }
829         if (status != AE_OK) { return_VALUE(-EINVAL); }
830
831         return_VALUE(0);
832 }
833 #endif
834
835 /* *************************************************************************
836    Module init/remove
837    ************************************************************************* */
838 /* -------------------------------------------------------------------------
839    input
840    ------------------------------------------------------------------------- */
841 static int __devinit acpi_pcc_init_input(struct acpi_hotkey *hotkey)
842 {
843         struct input_dev    *hotk_input_dev;
844         struct pcc_keyinput *pcc_keyinput;
845         int error;
846
847         ACPI_FUNCTION_TRACE("acpi_pcc_init_input");
848
849         hotk_input_dev = input_allocate_device();
850         if (hotk_input_dev == NULL) {
851                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Couldn't allocate input device for hotkey"));
852                 goto err_input;
853         }
854
855         pcc_keyinput = kcalloc(1, sizeof(struct pcc_keyinput), GFP_KERNEL);
856
857         if (pcc_keyinput == NULL) {
858                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Couldn't allocate mem for private data"));
859                 goto err_pcc;
860         }
861
862         hotk_input_dev->evbit[0] = BIT(EV_KEY);
863
864         set_bit(KEY_BRIGHTNESSDOWN, hotk_input_dev->keybit);
865         set_bit(KEY_BRIGHTNESSUP,   hotk_input_dev->keybit);
866         set_bit(KEY_MUTE,           hotk_input_dev->keybit);
867         set_bit(KEY_VOLUMEDOWN,     hotk_input_dev->keybit);
868         set_bit(KEY_VOLUMEUP,       hotk_input_dev->keybit);
869         set_bit(KEY_SLEEP,          hotk_input_dev->keybit);
870         set_bit(KEY_BATT,           hotk_input_dev->keybit);
871         set_bit(KEY_SUSPEND,        hotk_input_dev->keybit);
872
873         hotk_input_dev->name       = ACPI_PCC_DRIVER_NAME;
874         hotk_input_dev->phys       = ACPI_PCC_INPUT_PHYS;
875         hotk_input_dev->id.bustype = BUS_PCC_HOTKEY;
876         hotk_input_dev->id.vendor  = 0x0001;
877         hotk_input_dev->id.product = 0x0001;
878         hotk_input_dev->id.version = 0x0100;
879
880         pcc_keyinput->key_mode = PCC_KEYINPUT_MODE;
881         pcc_keyinput->hotkey   = hotkey;
882
883         input_set_drvdata(hotk_input_dev, pcc_keyinput);
884
885         hotkey->input_dev = hotk_input_dev;
886
887         error = input_register_device(hotk_input_dev);
888
889         if (error) {
890                 goto err_pcc;
891         }
892
893         return_VALUE(0);
894
895  err_pcc:
896         input_unregister_device(hotk_input_dev);
897  err_input:
898         return_VALUE(-ENOMEM);
899 }
900
901 static void __devexit acpi_pcc_remove_input(struct acpi_hotkey *hotkey)
902 {
903         struct input_dev    *hotk_input_dev;
904         struct pcc_keyinput *pcc_keyinput;
905
906         ACPI_FUNCTION_TRACE("acpi_pcc_remove_input");
907
908         if (hotkey == NULL) {
909                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Can't free memory"));
910                 return_VOID;
911         }
912
913         hotk_input_dev = hotkey->input_dev;
914         pcc_keyinput   = input_get_drvdata(hotk_input_dev);
915
916         input_unregister_device(hotk_input_dev);
917
918         kfree(pcc_keyinput);
919 }
920
921 /* -------------------------------------------------------------------------
922    ACPI
923    ------------------------------------------------------------------------- */
924 static int __devinit acpi_pcc_hotkey_add (struct acpi_device *device)
925 {
926         acpi_status             status = AE_OK;
927         struct acpi_hotkey      *hotkey = NULL;
928         int sifr_status, num_sifr, result;
929
930         ACPI_FUNCTION_TRACE("acpi_pcc_hotkey_add");
931
932         if (device == NULL) {
933                 return_VALUE(-EINVAL);
934         }
935
936         sifr_status = acpi_pcc_get_sqty(device);
937
938         if (sifr_status > 255) {
939                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "num_sifr too large"));
940                 return_VALUE(-ENODEV);
941         }
942
943         if (sifr_status < 0) {
944                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "not support SQTY"));
945                 num_sifr = 0;
946         } else {
947                 num_sifr = sifr_status;
948         }
949
950         hotkey = kcalloc(1, sizeof(struct acpi_hotkey), GFP_KERNEL);
951         if (hotkey == NULL) {
952                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Couldn't allocate mem for hotkey"));
953                 return_VALUE(-ENOMEM);
954         }
955
956         hotkey->device   = device;
957         hotkey->handle   = device->handle;
958         hotkey->num_sifr = num_sifr;
959         acpi_driver_data(device) = hotkey;
960         strcpy(acpi_device_name(device),  ACPI_PCC_DEVICE_NAME);
961         strcpy(acpi_device_class(device), ACPI_PCC_CLASS);
962
963         status = acpi_install_notify_handler(hotkey->handle,
964                                              ACPI_DEVICE_NOTIFY,
965                                              acpi_pcc_hotkey_notify,
966                                              hotkey);
967
968         if (ACPI_FAILURE(status)) {
969                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error installing notify handler\n"));
970                 kfree(hotkey);
971                 return_VALUE(-ENODEV);
972         }
973
974         result = acpi_pcc_init_input(hotkey);
975         if (result != 0) {
976                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error installing keyinput handler\n"));
977                 kfree(hotkey);
978                 return_VALUE(result);
979         }
980
981         return_VALUE(acpi_pcc_proc_init(device));
982 }
983
984 static int __devexit acpi_pcc_hotkey_remove(struct acpi_device *device, int type)
985 {
986         acpi_status             status = AE_OK;
987         struct acpi_hotkey      *hotkey = acpi_driver_data(device);
988
989         ACPI_FUNCTION_TRACE("acpi_pcc_hotkey_remove");
990
991         if (!device || !hotkey) {
992                 return_VALUE(-EINVAL);
993         }
994
995         if (hotkey->proc_dir_entry) {
996                 acpi_pcc_remove_device(device, acpi_pcc_proc_items_sifr, hotkey->num_sifr);
997                 acpi_pcc_remove_device(device, acpi_pcc_proc_items,      ARRAY_SIZE(acpi_pcc_proc_items));
998                 remove_proc_entry(PROC_PCC, acpi_root_dir);
999         }
1000
1001         status = acpi_remove_notify_handler(hotkey->handle,
1002                     ACPI_DEVICE_NOTIFY, acpi_pcc_hotkey_notify);
1003
1004         if (ACPI_FAILURE(status)) {
1005                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error removing notify handler\n"));
1006         }
1007
1008         acpi_pcc_remove_input(hotkey);
1009         if (hotkey != NULL) {
1010                 kfree(hotkey);
1011         }
1012         return_VALUE(status == AE_OK);
1013 }
1014
1015 /* *********************************************************************
1016    Module entry point
1017    ********************************************************************* */
1018 static int __init acpi_pcc_init(void)
1019 {
1020         int result;
1021
1022         ACPI_FUNCTION_TRACE("acpi_pcc_init");
1023
1024         printk(KERN_INFO LOGPREFIX "loading...\n");
1025
1026         if (acpi_disabled) {
1027                 printk(KERN_INFO LOGPREFIX "ACPI disabled.\n");
1028                 return_VALUE(-ENODEV);
1029         }
1030
1031         result = acpi_bus_register_driver(&acpi_pcc_driver);
1032         if (result < 0) {
1033                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error registering hotkey driver\n"));
1034                 return_VALUE(-ENODEV);
1035         }
1036
1037         return_VALUE(result);
1038 }
1039
1040 static void __exit acpi_pcc_exit(void)
1041 {
1042         ACPI_FUNCTION_TRACE("acpi_pcc_exit");
1043
1044         printk(KERN_INFO LOGPREFIX "unloading...\n");
1045
1046         acpi_bus_unregister_driver(&acpi_pcc_driver);
1047
1048         return_VOID;
1049 }
1050
1051 module_init(acpi_pcc_init);
1052 module_exit(acpi_pcc_exit);