added mtd driver
[linux-2.4.git] / drivers / acpi / toshiba_acpi.c
1 /*
2  *  toshiba_acpi.c - Toshiba Laptop ACPI Extras
3  *
4  *
5  *  Copyright (C) 2002-2004 John Belmonte
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  *
21  *
22  *  The devolpment page for this driver is located at
23  *  http://memebeam.org/toys/ToshibaAcpiDriver.
24  *
25  *  Credits:
26  *      Jonathan A. Buzzard - Toshiba HCI info, and critical tips on reverse
27  *              engineering the Windows drivers
28  *      Yasushi Nagato - changes for linux kernel 2.4 -> 2.5
29  *      Rob Miller - TV out and hotkeys help
30  *
31  *
32  *  TODO
33  *
34  */
35
36 #define TOSHIBA_ACPI_VERSION    "0.18"
37 #define PROC_INTERFACE_VERSION  1
38
39 #include <linux/kernel.h>
40 #include <linux/module.h>
41 #include <linux/init.h>
42 #include <linux/types.h>
43 #include <linux/proc_fs.h>
44 #include <asm/uaccess.h>
45 #include <linux/version.h>
46
47 #include <acpi/acpi_drivers.h>
48
49 MODULE_AUTHOR("John Belmonte");
50 MODULE_DESCRIPTION("Toshiba Laptop ACPI Extras Driver");
51 MODULE_LICENSE("GPL");
52
53 #define MY_LOGPREFIX "toshiba_acpi: "
54 #define MY_ERR KERN_ERR MY_LOGPREFIX
55 #define MY_NOTICE KERN_NOTICE MY_LOGPREFIX
56 #define MY_INFO KERN_INFO MY_LOGPREFIX
57
58 /* Toshiba ACPI method paths */
59 #define METHOD_LCD_BRIGHTNESS   "\\_SB_.PCI0.VGA_.LCD_._BCM"
60 #define METHOD_HCI_1            "\\_SB_.VALD.GHCI"
61 #define METHOD_HCI_2            "\\_SB_.VALZ.GHCI"
62 #define METHOD_VIDEO_OUT        "\\_SB_.VALX.DSSX"
63
64 /* Toshiba HCI interface definitions
65  *
66  * HCI is Toshiba's "Hardware Control Interface" which is supposed to
67  * be uniform across all their models.  Ideally we would just call
68  * dedicated ACPI methods instead of using this primitive interface.
69  * However the ACPI methods seem to be incomplete in some areas (for
70  * example they allow setting, but not reading, the LCD brightness value),
71  * so this is still useful.
72  */
73
74 #define HCI_WORDS                       6
75
76 /* operations */
77 #define HCI_SET                         0xff00
78 #define HCI_GET                         0xfe00
79
80 /* return codes */
81 #define HCI_SUCCESS                     0x0000
82 #define HCI_FAILURE                     0x1000
83 #define HCI_NOT_SUPPORTED               0x8000
84 #define HCI_EMPTY                       0x8c00
85
86 /* registers */
87 #define HCI_FAN                         0x0004
88 #define HCI_SYSTEM_EVENT                0x0016
89 #define HCI_VIDEO_OUT                   0x001c
90 #define HCI_HOTKEY_EVENT                0x001e
91 #define HCI_LCD_BRIGHTNESS              0x002a
92
93 /* field definitions */
94 #define HCI_LCD_BRIGHTNESS_BITS         3
95 #define HCI_LCD_BRIGHTNESS_SHIFT        (16-HCI_LCD_BRIGHTNESS_BITS)
96 #define HCI_LCD_BRIGHTNESS_LEVELS       (1 << HCI_LCD_BRIGHTNESS_BITS)
97 #define HCI_VIDEO_OUT_LCD               0x1
98 #define HCI_VIDEO_OUT_CRT               0x2
99 #define HCI_VIDEO_OUT_TV                0x4
100
101 /* utility
102  */
103
104 static __inline__ void
105 _set_bit(u32* word, u32 mask, int value)
106 {
107         *word = (*word & ~mask) | (mask * value);
108 }
109
110 /* acpi interface wrappers
111  */
112
113 static int
114 is_valid_acpi_path(const char* methodName)
115 {
116         acpi_handle handle;
117         acpi_status status;
118
119         status = acpi_get_handle(0, (char*)methodName, &handle);
120         return !ACPI_FAILURE(status);
121 }
122
123 static int
124 write_acpi_int(const char* methodName, int val)
125 {
126         struct acpi_object_list params;
127         union acpi_object in_objs[1];
128         acpi_status status;
129
130         params.count = sizeof(in_objs)/sizeof(in_objs[0]);
131         params.pointer = in_objs;
132         in_objs[0].type = ACPI_TYPE_INTEGER;
133         in_objs[0].integer.value = val;
134
135         status = acpi_evaluate_object(0, (char*)methodName, &params, 0);
136         return (status == AE_OK);
137 }
138
139 #if 0
140 static int
141 read_acpi_int(const char* methodName, int* pVal)
142 {
143         struct acpi_buffer results;
144         union acpi_object out_objs[1];
145         acpi_status status;
146
147         results.length = sizeof(out_objs);
148         results.pointer = out_objs;
149
150         status = acpi_evaluate_object(0, (char*)methodName, 0, &results);
151         *pVal = out_objs[0].integer.value;
152
153         return (status == AE_OK) && (out_objs[0].type == ACPI_TYPE_INTEGER);
154 }
155 #endif
156
157 static const char*              method_hci /*= 0*/;
158
159 /* Perform a raw HCI call.  Here we don't care about input or output buffer
160  * format.
161  */
162 static acpi_status
163 hci_raw(const u32 in[HCI_WORDS], u32 out[HCI_WORDS])
164 {
165         struct acpi_object_list params;
166         union acpi_object in_objs[HCI_WORDS];
167         struct acpi_buffer results;
168         union acpi_object out_objs[HCI_WORDS+1];
169         acpi_status status;
170         int i;
171
172         params.count = HCI_WORDS;
173         params.pointer = in_objs;
174         for (i = 0; i < HCI_WORDS; ++i) {
175                 in_objs[i].type = ACPI_TYPE_INTEGER;
176                 in_objs[i].integer.value = in[i];
177         }
178
179         results.length = sizeof(out_objs);
180         results.pointer = out_objs;
181
182         status = acpi_evaluate_object(0, (char*)method_hci, &params,
183                 &results);
184         if ((status == AE_OK) && (out_objs->package.count <= HCI_WORDS)) {
185                 for (i = 0; i < out_objs->package.count; ++i) {
186                         out[i] = out_objs->package.elements[i].integer.value;
187                 }
188         }
189
190         return status;
191 }
192
193 /* common hci tasks (get or set one value)
194  *
195  * In addition to the ACPI status, the HCI system returns a result which
196  * may be useful (such as "not supported").
197  */
198
199 static acpi_status
200 hci_write1(u32 reg, u32 in1, u32* result)
201 {
202         u32 in[HCI_WORDS] = { HCI_SET, reg, in1, 0, 0, 0 };
203         u32 out[HCI_WORDS];
204         acpi_status status = hci_raw(in, out);
205         *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
206         return status;
207 }
208
209 static acpi_status
210 hci_read1(u32 reg, u32* out1, u32* result)
211 {
212         u32 in[HCI_WORDS] = { HCI_GET, reg, 0, 0, 0, 0 };
213         u32 out[HCI_WORDS];
214         acpi_status status = hci_raw(in, out);
215         *out1 = out[2];
216         *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
217         return status;
218 }
219
220 static struct proc_dir_entry*   toshiba_proc_dir /*= 0*/;
221 static int                      force_fan;
222 static int                      last_key_event;
223 static int                      key_event_valid;
224
225 typedef struct _ProcItem
226 {
227         const char* name;
228         char* (*read_func)(char*);
229         unsigned long (*write_func)(const char*, unsigned long);
230 } ProcItem;
231
232 /* proc file handlers
233  */
234
235 static int
236 dispatch_read(char* page, char** start, off_t off, int count, int* eof,
237         ProcItem* item)
238 {
239         char* p = page;
240         int len;
241
242         if (off == 0)
243                 p = item->read_func(p);
244
245         /* ISSUE: I don't understand this code */
246         len = (p - page);
247         if (len <= off+count) *eof = 1;
248         *start = page + off;
249         len -= off;
250         if (len>count) len = count;
251         if (len<0) len = 0;
252         return len;
253 }
254
255 static int
256 dispatch_write(struct file* file, const char* buffer,
257         unsigned long count, ProcItem* item)
258 {
259         int result;
260         char* tmp_buffer;
261
262         /* Arg buffer points to userspace memory, which can't be accessed
263          * directly.  Since we're making a copy, zero-terminate the
264          * destination so that sscanf can be used on it safely.
265          */
266         tmp_buffer = kmalloc(count + 1, GFP_KERNEL);
267         if (copy_from_user(tmp_buffer, buffer, count)) {
268                 result = -EFAULT;
269         }
270         else {
271                 tmp_buffer[count] = 0;
272                 result = item->write_func(tmp_buffer, count);
273         }
274         kfree(tmp_buffer);
275         return result;
276 }
277
278 static char*
279 read_lcd(char* p)
280 {
281         u32 hci_result;
282         u32 value;
283
284         hci_read1(HCI_LCD_BRIGHTNESS, &value, &hci_result);
285         if (hci_result == HCI_SUCCESS) {
286                 value = value >> HCI_LCD_BRIGHTNESS_SHIFT;
287                 p += sprintf(p, "brightness:              %d\n", value);
288                 p += sprintf(p, "brightness_levels:       %d\n",
289                         HCI_LCD_BRIGHTNESS_LEVELS);
290         } else {
291                 printk(MY_ERR "Error reading LCD brightness\n");
292         }
293
294         return p;
295 }
296
297 static unsigned long
298 write_lcd(const char* buffer, unsigned long count)
299 {
300         int value;
301         u32 hci_result;
302
303         if (sscanf(buffer, " brightness : %i", &value) == 1 &&
304                         value >= 0 && value < HCI_LCD_BRIGHTNESS_LEVELS) {
305                 value = value << HCI_LCD_BRIGHTNESS_SHIFT;
306                 hci_write1(HCI_LCD_BRIGHTNESS, value, &hci_result);
307                 if (hci_result != HCI_SUCCESS)
308                         return -EFAULT;
309         } else {
310                 return -EINVAL;
311         }
312
313         return count;
314 }
315
316 static char*
317 read_video(char* p)
318 {
319         u32 hci_result;
320         u32 value;
321
322         hci_read1(HCI_VIDEO_OUT, &value, &hci_result);
323         if (hci_result == HCI_SUCCESS) {
324                 int is_lcd = (value & HCI_VIDEO_OUT_LCD) ? 1 : 0;
325                 int is_crt = (value & HCI_VIDEO_OUT_CRT) ? 1 : 0;
326                 int is_tv  = (value & HCI_VIDEO_OUT_TV ) ? 1 : 0;
327                 p += sprintf(p, "lcd_out:                 %d\n", is_lcd);
328                 p += sprintf(p, "crt_out:                 %d\n", is_crt);
329                 p += sprintf(p, "tv_out:                  %d\n", is_tv);
330         } else {
331                 printk(MY_ERR "Error reading video out status\n");
332         }
333
334         return p;
335 }
336
337 static unsigned long
338 write_video(const char* buffer, unsigned long count)
339 {
340         int value;
341         int remain = count;
342         int lcd_out = -1;
343         int crt_out = -1;
344         int tv_out = -1;
345         u32 hci_result;
346         int video_out;
347
348         /* scan expression.  Multiple expressions may be delimited with ;
349          *
350          *  NOTE: to keep scanning simple, invalid fields are ignored
351          */
352         while (remain) {
353                 if (sscanf(buffer, " lcd_out : %i", &value) == 1)
354                         lcd_out = value & 1;
355                 else if (sscanf(buffer, " crt_out : %i", &value) == 1)
356                         crt_out = value & 1;
357                 else if (sscanf(buffer, " tv_out : %i", &value) == 1)
358                         tv_out = value & 1;
359                 /* advance to one character past the next ; */
360                 do {
361                         ++buffer;
362                         --remain;
363                 }
364                 while (remain && *(buffer-1) != ';');
365         }
366
367         hci_read1(HCI_VIDEO_OUT, &video_out, &hci_result);
368         if (hci_result == HCI_SUCCESS) {
369                 int new_video_out = video_out;
370                 if (lcd_out != -1)
371                         _set_bit(&new_video_out, HCI_VIDEO_OUT_LCD, lcd_out);
372                 if (crt_out != -1)
373                         _set_bit(&new_video_out, HCI_VIDEO_OUT_CRT, crt_out);
374                 if (tv_out != -1)
375                         _set_bit(&new_video_out, HCI_VIDEO_OUT_TV, tv_out);
376                 /* To avoid unnecessary video disruption, only write the new
377                  * video setting if something changed. */
378                 if (new_video_out != video_out)
379                         write_acpi_int(METHOD_VIDEO_OUT, new_video_out);
380         } else {
381                 return -EFAULT;
382         }
383
384         return count;
385 }
386
387 static char*
388 read_fan(char* p)
389 {
390         u32 hci_result;
391         u32 value;
392
393         hci_read1(HCI_FAN, &value, &hci_result);
394         if (hci_result == HCI_SUCCESS) {
395                 p += sprintf(p, "running:                 %d\n", (value > 0));
396                 p += sprintf(p, "force_on:                %d\n", force_fan);
397         } else {
398                 printk(MY_ERR "Error reading fan status\n");
399         }
400
401         return p;
402 }
403
404 static unsigned long
405 write_fan(const char* buffer, unsigned long count)
406 {
407         int value;
408         u32 hci_result;
409
410         if (sscanf(buffer, " force_on : %i", &value) == 1 &&
411                         value >= 0 && value <= 1) {
412                 hci_write1(HCI_FAN, value, &hci_result);
413                 if (hci_result != HCI_SUCCESS)
414                         return -EFAULT;
415                 else
416                         force_fan = value;
417         } else {
418                 return -EINVAL;
419         }
420
421         return count;
422 }
423
424 static char*
425 read_keys(char* p)
426 {
427         u32 hci_result;
428         u32 value;
429
430         if (!key_event_valid) {
431                 hci_read1(HCI_SYSTEM_EVENT, &value, &hci_result);
432                 if (hci_result == HCI_SUCCESS) {
433                         key_event_valid = 1;
434                         last_key_event = value;
435                 } else if (hci_result == HCI_EMPTY) {
436                         /* better luck next time */
437                 } else if (hci_result == HCI_NOT_SUPPORTED) {
438                         /* This is a workaround for an unresolved issue on
439                          * some machines where system events sporadically
440                          * become disabled. */
441                         hci_write1(HCI_SYSTEM_EVENT, 1, &hci_result);
442                         printk(MY_NOTICE "Re-enabled hotkeys\n");
443                 } else {
444                         printk(MY_ERR "Error reading hotkey status\n");
445                         goto end;
446                 }
447         }
448
449         p += sprintf(p, "hotkey_ready:            %d\n", key_event_valid);
450         p += sprintf(p, "hotkey:                  0x%04x\n", last_key_event);
451
452 end:
453         return p;
454 }
455
456 static unsigned long
457 write_keys(const char* buffer, unsigned long count)
458 {
459         int value;
460
461         if (sscanf(buffer, " hotkey_ready : %i", &value) == 1 &&
462                         value == 0) {
463                 key_event_valid = 0;
464         } else {
465                 return -EINVAL;
466         }
467
468         return count;
469 }
470
471 static char*
472 read_version(char* p)
473 {
474         p += sprintf(p, "driver:                  %s\n", TOSHIBA_ACPI_VERSION);
475         p += sprintf(p, "proc_interface:          %d\n",
476                 PROC_INTERFACE_VERSION);
477         return p;
478 }
479
480 /* proc and module init
481  */
482
483 #define PROC_TOSHIBA            "toshiba"
484
485 ProcItem proc_items[] =
486 {
487         { "lcd"         , read_lcd      , write_lcd     },
488         { "video"       , read_video    , write_video   },
489         { "fan"         , read_fan      , write_fan     },
490         { "keys"        , read_keys     , write_keys    },
491         { "version"     , read_version  , 0             },
492         { 0             , 0             , 0             },
493 };
494
495 static acpi_status __init
496 add_device(void)
497 {
498         struct proc_dir_entry* proc;
499         ProcItem* item;
500
501         for (item = proc_items; item->name; ++item)
502         {
503                 proc = create_proc_read_entry(item->name,
504                         S_IFREG | S_IRUGO | S_IWUSR,
505                         toshiba_proc_dir, (read_proc_t*)dispatch_read, item);
506                 if (proc)
507                         proc->owner = THIS_MODULE;
508                 if (proc && item->write_func)
509                         proc->write_proc = (write_proc_t*)dispatch_write;
510         }
511
512         return(AE_OK);
513 }
514
515 static acpi_status __exit
516 remove_device(void)
517 {
518         ProcItem* item;
519
520         for (item = proc_items; item->name; ++item)
521                 remove_proc_entry(item->name, toshiba_proc_dir);
522         return(AE_OK);
523 }
524
525 static int __init
526 toshiba_acpi_init(void)
527 {
528         acpi_status status = AE_OK;
529         u32 hci_result;
530
531         if (acpi_disabled)
532                 return -ENODEV;
533         /* simple device detection: look for HCI method */
534         if (is_valid_acpi_path(METHOD_HCI_1))
535                 method_hci = METHOD_HCI_1;
536         else if (is_valid_acpi_path(METHOD_HCI_2))
537                 method_hci = METHOD_HCI_2;
538         else
539                 return -ENODEV;
540
541         printk(MY_INFO "Toshiba Laptop ACPI Extras version %s\n",
542                 TOSHIBA_ACPI_VERSION);
543         printk(MY_INFO "    HCI method: %s\n", method_hci);
544
545         force_fan = 0;
546         key_event_valid = 0;
547
548         /* enable event fifo */
549         hci_write1(HCI_SYSTEM_EVENT, 1, &hci_result);
550
551         toshiba_proc_dir = proc_mkdir(PROC_TOSHIBA, acpi_root_dir);
552         if (!toshiba_proc_dir) {
553                 status = AE_ERROR;
554         } else {
555                 toshiba_proc_dir->owner = THIS_MODULE;
556                 status = add_device();
557                 if (ACPI_FAILURE(status))
558                         remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
559         }
560
561         return (ACPI_SUCCESS(status)) ? 0 : -ENODEV;
562 }
563
564 static void __exit
565 toshiba_acpi_exit(void)
566 {
567         remove_device();
568
569         if (toshiba_proc_dir)
570                 remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
571
572         return;
573 }
574
575 module_init(toshiba_acpi_init);
576 module_exit(toshiba_acpi_exit);