2 * toshiba_acpi.c - Toshiba Laptop ACPI Extras
5 * Copyright (C) 2002-2003 John Belmonte
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.
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.
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
22 * The devolpment page for this driver is located at
23 * http://memebeam.org/toys/ToshibaAcpiDriver.
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
36 #define TOSHIBA_ACPI_VERSION "0.15"
37 #define PROC_INTERFACE_VERSION 1
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 <linux/version.h>
46 #include <acpi/acpi_drivers.h>
48 MODULE_AUTHOR("John Belmonte");
49 MODULE_DESCRIPTION("Toshiba Laptop ACPI Extras Driver");
50 MODULE_LICENSE("GPL");
52 /* Toshiba ACPI method paths */
53 #define METHOD_LCD_BRIGHTNESS "\\_SB_.PCI0.VGA_.LCD_._BCM"
54 #define METHOD_HCI "\\_SB_.VALD.GHCI"
55 #define METHOD_VIDEO_OUT "\\_SB_.VALX.DSSX"
57 /* Toshiba HCI interface definitions
59 * HCI is Toshiba's "Hardware Control Interface" which is supposed to
60 * be uniform across all their models. Ideally we would just call
61 * dedicated ACPI methods instead of using this primitive interface.
62 * However the ACPI methods seem to be incomplete in some areas (for
63 * example they allow setting, but not reading, the LCD brightness value),
64 * so this is still useful.
70 #define HCI_SET 0xff00
71 #define HCI_GET 0xfe00
74 #define HCI_SUCCESS 0x0000
75 #define HCI_FAILURE 0x1000
76 #define HCI_NOT_SUPPORTED 0x8000
77 #define HCI_EMPTY 0x8c00
80 #define HCI_FAN 0x0004
81 #define HCI_SYSTEM_EVENT 0x0016
82 #define HCI_VIDEO_OUT 0x001c
83 #define HCI_HOTKEY_EVENT 0x001e
84 #define HCI_LCD_BRIGHTNESS 0x002a
86 /* field definitions */
87 #define HCI_LCD_BRIGHTNESS_BITS 3
88 #define HCI_LCD_BRIGHTNESS_SHIFT (16-HCI_LCD_BRIGHTNESS_BITS)
89 #define HCI_LCD_BRIGHTNESS_LEVELS (1 << HCI_LCD_BRIGHTNESS_BITS)
90 #define HCI_VIDEO_OUT_LCD 0x1
91 #define HCI_VIDEO_OUT_CRT 0x2
92 #define HCI_VIDEO_OUT_TV 0x4
97 static __inline__
void
98 _set_bit(u32
* word
, u32 mask
, int value
)
100 *word
= (*word
& ~mask
) | (mask
* value
);
103 /* an sscanf that takes explicit string length */
105 snscanf(const char* str
, int n
, const char* format
, ...)
109 char* str2
= kmalloc(n
+ 1, GFP_KERNEL
);
110 if (str2
== 0) return 0;
111 strncpy(str2
, str
, n
);
113 va_start(args
, format
);
114 result
= vsscanf(str2
, format
, args
);
120 /* acpi interface wrappers
124 write_acpi_int(const char* methodName
, int val
)
126 struct acpi_object_list params
;
127 union acpi_object in_objs
[1];
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
;
135 status
= acpi_evaluate_object(0, (char*)methodName
, ¶ms
, 0);
136 return (status
== AE_OK
);
141 read_acpi_int(const char* methodName
, int* pVal
)
143 struct acpi_buffer results
;
144 union acpi_object out_objs
[1];
147 results
.length
= sizeof(out_objs
);
148 results
.pointer
= out_objs
;
150 status
= acpi_evaluate_object(0, (char*)methodName
, 0, &results
);
151 *pVal
= out_objs
[0].integer
.value
;
153 return (status
== AE_OK
) && (out_objs
[0].type
== ACPI_TYPE_INTEGER
);
157 /* Perform a raw HCI call. Here we don't care about input or output buffer
161 hci_raw(const u32 in
[HCI_WORDS
], u32 out
[HCI_WORDS
])
163 struct acpi_object_list params
;
164 union acpi_object in_objs
[HCI_WORDS
];
165 struct acpi_buffer results
;
166 union acpi_object out_objs
[HCI_WORDS
+1];
170 params
.count
= HCI_WORDS
;
171 params
.pointer
= in_objs
;
172 for (i
= 0; i
< HCI_WORDS
; ++i
) {
173 in_objs
[i
].type
= ACPI_TYPE_INTEGER
;
174 in_objs
[i
].integer
.value
= in
[i
];
177 results
.length
= sizeof(out_objs
);
178 results
.pointer
= out_objs
;
180 status
= acpi_evaluate_object(0, METHOD_HCI
, ¶ms
,
182 if ((status
== AE_OK
) && (out_objs
->package
.count
<= HCI_WORDS
)) {
183 for (i
= 0; i
< out_objs
->package
.count
; ++i
) {
184 out
[i
] = out_objs
->package
.elements
[i
].integer
.value
;
191 /* common hci tasks (get or set one value)
193 * In addition to the ACPI status, the HCI system returns a result which
194 * may be useful (such as "not supported").
198 hci_write1(u32 reg
, u32 in1
, u32
* result
)
200 u32 in
[HCI_WORDS
] = { HCI_SET
, reg
, in1
, 0, 0, 0 };
202 acpi_status status
= hci_raw(in
, out
);
203 *result
= (status
== AE_OK
) ? out
[0] : HCI_FAILURE
;
208 hci_read1(u32 reg
, u32
* out1
, u32
* result
)
210 u32 in
[HCI_WORDS
] = { HCI_GET
, reg
, 0, 0, 0, 0 };
212 acpi_status status
= hci_raw(in
, out
);
214 *result
= (status
== AE_OK
) ? out
[0] : HCI_FAILURE
;
218 static struct proc_dir_entry
* toshiba_proc_dir
= NULL
;
219 static int force_fan
;
220 static int last_key_event
;
221 static int key_event_valid
;
223 typedef struct _ProcItem
226 char* (*read_func
)(char*);
227 unsigned long (*write_func
)(const char*, unsigned long);
230 /* proc file handlers
234 dispatch_read(char* page
, char** start
, off_t off
, int count
, int* eof
,
241 p
= item
->read_func(p
);
243 /* ISSUE: I don't understand this code */
245 if (len
<= off
+count
) *eof
= 1;
248 if (len
>count
) len
= count
;
254 dispatch_write(struct file
* file
, const char* buffer
, unsigned long count
,
257 return item
->write_func(buffer
, count
);
266 hci_read1(HCI_LCD_BRIGHTNESS
, &value
, &hci_result
);
267 if (hci_result
== HCI_SUCCESS
) {
268 value
= value
>> HCI_LCD_BRIGHTNESS_SHIFT
;
269 p
+= sprintf(p
, "brightness: %d\n", value
);
270 p
+= sprintf(p
, "brightness_levels: %d\n",
271 HCI_LCD_BRIGHTNESS_LEVELS
);
273 p
+= sprintf(p
, "ERROR\n");
280 write_lcd(const char* buffer
, unsigned long count
)
285 if (snscanf(buffer
, count
, " brightness : %i", &value
) == 1 &&
286 value
>= 0 && value
< HCI_LCD_BRIGHTNESS_LEVELS
) {
287 value
= value
<< HCI_LCD_BRIGHTNESS_SHIFT
;
288 hci_write1(HCI_LCD_BRIGHTNESS
, value
, &hci_result
);
289 if (hci_result
!= HCI_SUCCESS
)
304 hci_read1(HCI_VIDEO_OUT
, &value
, &hci_result
);
305 if (hci_result
== HCI_SUCCESS
) {
306 int is_lcd
= (value
& HCI_VIDEO_OUT_LCD
) ? 1 : 0;
307 int is_crt
= (value
& HCI_VIDEO_OUT_CRT
) ? 1 : 0;
308 int is_tv
= (value
& HCI_VIDEO_OUT_TV
) ? 1 : 0;
309 p
+= sprintf(p
, "lcd_out: %d\n", is_lcd
);
310 p
+= sprintf(p
, "crt_out: %d\n", is_crt
);
311 p
+= sprintf(p
, "tv_out: %d\n", is_tv
);
313 p
+= sprintf(p
, "ERROR\n");
320 write_video(const char* buffer
, unsigned long count
)
323 const char* buffer_end
= buffer
+ count
;
330 /* scan expression. Multiple expressions may be delimited with ; */
332 if (snscanf(buffer
, count
, " lcd_out : %i", &value
) == 1)
334 else if (snscanf(buffer
, count
, " crt_out : %i", &value
) == 1)
336 else if (snscanf(buffer
, count
, " tv_out : %i", &value
) == 1)
338 /* advance to one character past the next ; */
340 while ((buffer
< buffer_end
) && (*(buffer
-1) != ';'));
341 } while (buffer
< buffer_end
);
343 hci_read1(HCI_VIDEO_OUT
, &video_out
, &hci_result
);
344 if (hci_result
== HCI_SUCCESS
) {
345 int new_video_out
= video_out
;
347 _set_bit(&new_video_out
, HCI_VIDEO_OUT_LCD
, lcd_out
);
349 _set_bit(&new_video_out
, HCI_VIDEO_OUT_CRT
, crt_out
);
351 _set_bit(&new_video_out
, HCI_VIDEO_OUT_TV
, tv_out
);
352 /* To avoid unnecessary video disruption, only write the new
353 * video setting if something changed. */
354 if (new_video_out
!= video_out
)
355 write_acpi_int(METHOD_VIDEO_OUT
, new_video_out
);
367 hci_read1(HCI_FAN
, &value
, &hci_result
);
368 if (hci_result
== HCI_SUCCESS
) {
369 p
+= sprintf(p
, "running: %d\n", (value
> 0));
370 p
+= sprintf(p
, "force_on: %d\n", force_fan
);
372 p
+= sprintf(p
, "ERROR\n");
379 write_fan(const char* buffer
, unsigned long count
)
384 if (snscanf(buffer
, count
, " force_on : %i", &value
) == 1 &&
385 value
>= 0 && value
<= 1) {
386 hci_write1(HCI_FAN
, value
, &hci_result
);
387 if (hci_result
!= HCI_SUCCESS
)
404 if (!key_event_valid
) {
405 hci_read1(HCI_SYSTEM_EVENT
, &value
, &hci_result
);
406 if (hci_result
== HCI_SUCCESS
) {
408 last_key_event
= value
;
409 } else if (hci_result
== HCI_EMPTY
) {
410 /* better luck next time */
411 } else if (hci_result
== HCI_NOT_SUPPORTED
) {
412 /* This is a workaround for an unresolved issue on
413 * some machines where system events sporadically
414 * become disabled. */
415 hci_write1(HCI_SYSTEM_EVENT
, 1, &hci_result
);
417 p
+= sprintf(p
, "ERROR\n");
422 p
+= sprintf(p
, "hotkey_ready: %d\n", key_event_valid
);
423 p
+= sprintf(p
, "hotkey: 0x%04x\n", last_key_event
);
430 write_keys(const char* buffer
, unsigned long count
)
434 if (snscanf(buffer
, count
, " hotkey_ready : %i", &value
) == 1 &&
445 read_version(char* p
)
447 p
+= sprintf(p
, "driver: %s\n", TOSHIBA_ACPI_VERSION
);
448 p
+= sprintf(p
, "proc_interface: %d\n",
449 PROC_INTERFACE_VERSION
);
453 /* proc and module init
456 #define PROC_TOSHIBA "toshiba"
458 ProcItem proc_items
[] =
460 { "lcd" , read_lcd
, write_lcd
},
461 { "video" , read_video
, write_video
},
462 { "fan" , read_fan
, write_fan
},
463 { "keys" , read_keys
, write_keys
},
464 { "version" , read_version
, 0 },
471 struct proc_dir_entry
* proc
;
474 for (item
= proc_items
; item
->name
; ++item
)
476 proc
= create_proc_read_entry(item
->name
,
477 S_IFREG
| S_IRUGO
| S_IWUSR
,
478 toshiba_proc_dir
, (read_proc_t
*)dispatch_read
, item
);
479 if (proc
&& item
->write_func
)
480 proc
->write_proc
= (write_proc_t
*)dispatch_write
;
491 for (item
= proc_items
; item
->name
; ++item
)
492 remove_proc_entry(item
->name
, toshiba_proc_dir
);
497 toshiba_acpi_init(void)
499 acpi_status status
= AE_OK
;
503 /* simple device detection: try reading an HCI register */
504 hci_read1(HCI_LCD_BRIGHTNESS
, &value
, &hci_result
);
505 if (hci_result
!= HCI_SUCCESS
)
508 printk("Toshiba Laptop ACPI Extras version %s\n", TOSHIBA_ACPI_VERSION
);
513 /* enable event fifo */
514 hci_write1(HCI_SYSTEM_EVENT
, 1, &hci_result
);
516 toshiba_proc_dir
= proc_mkdir(PROC_TOSHIBA
, acpi_root_dir
);
517 if (!toshiba_proc_dir
) {
520 status
= add_device();
521 if (ACPI_FAILURE(status
))
522 remove_proc_entry(PROC_TOSHIBA
, acpi_root_dir
);
525 return (ACPI_SUCCESS(status
)) ? 0 : -ENODEV
;
529 toshiba_acpi_exit(void)
533 if (toshiba_proc_dir
)
534 remove_proc_entry(PROC_TOSHIBA
, acpi_root_dir
);
539 module_init(toshiba_acpi_init
);
540 module_exit(toshiba_acpi_exit
);