Revert toshiba_acpi strlcpy'fication as per John Belmonte.
[linux-2.6/history.git] / drivers / acpi / toshiba_acpi.c
blob0cb16126e317b74309ca85a7ac0a1cda7f2a0dc5
1 /*
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.
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
32 * TODO
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.
67 #define HCI_WORDS 6
69 /* operations */
70 #define HCI_SET 0xff00
71 #define HCI_GET 0xfe00
73 /* return codes */
74 #define HCI_SUCCESS 0x0000
75 #define HCI_FAILURE 0x1000
76 #define HCI_NOT_SUPPORTED 0x8000
77 #define HCI_EMPTY 0x8c00
79 /* registers */
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
94 /* utility
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 */
104 static int
105 snscanf(const char* str, int n, const char* format, ...)
107 va_list args;
108 int result;
109 char* str2 = kmalloc(n + 1, GFP_KERNEL);
110 if (str2 == 0) return 0;
111 strncpy(str2, str, n);
112 str2[n] = 0;
113 va_start(args, format);
114 result = vsscanf(str2, format, args);
115 va_end(args);
116 kfree(str2);
117 return result;
120 /* acpi interface wrappers
123 static int
124 write_acpi_int(const char* methodName, int val)
126 struct acpi_object_list params;
127 union acpi_object in_objs[1];
128 acpi_status status;
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, &params, 0);
136 return (status == AE_OK);
139 #if 0
140 static int
141 read_acpi_int(const char* methodName, int* pVal)
143 struct acpi_buffer results;
144 union acpi_object out_objs[1];
145 acpi_status status;
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);
155 #endif
157 /* Perform a raw HCI call. Here we don't care about input or output buffer
158 * format.
160 static acpi_status
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];
167 acpi_status status;
168 int i;
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, &params,
181 &results);
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;
188 return status;
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").
197 static acpi_status
198 hci_write1(u32 reg, u32 in1, u32* result)
200 u32 in[HCI_WORDS] = { HCI_SET, reg, in1, 0, 0, 0 };
201 u32 out[HCI_WORDS];
202 acpi_status status = hci_raw(in, out);
203 *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
204 return status;
207 static acpi_status
208 hci_read1(u32 reg, u32* out1, u32* result)
210 u32 in[HCI_WORDS] = { HCI_GET, reg, 0, 0, 0, 0 };
211 u32 out[HCI_WORDS];
212 acpi_status status = hci_raw(in, out);
213 *out1 = out[2];
214 *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
215 return status;
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
225 const char* name;
226 char* (*read_func)(char*);
227 unsigned long (*write_func)(const char*, unsigned long);
228 } ProcItem;
230 /* proc file handlers
233 static int
234 dispatch_read(char* page, char** start, off_t off, int count, int* eof,
235 ProcItem* item)
237 char* p = page;
238 int len;
240 if (off == 0)
241 p = item->read_func(p);
243 /* ISSUE: I don't understand this code */
244 len = (p - page);
245 if (len <= off+count) *eof = 1;
246 *start = page + off;
247 len -= off;
248 if (len>count) len = count;
249 if (len<0) len = 0;
250 return len;
253 static int
254 dispatch_write(struct file* file, const char* buffer, unsigned long count,
255 ProcItem* item)
257 return item->write_func(buffer, count);
260 static char*
261 read_lcd(char* p)
263 u32 hci_result;
264 u32 value;
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);
272 } else {
273 p += sprintf(p, "ERROR\n");
276 return p;
279 static unsigned long
280 write_lcd(const char* buffer, unsigned long count)
282 int value;
283 u32 hci_result;
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)
290 return -EFAULT;
291 } else {
292 return -EINVAL;
295 return count;
298 static char*
299 read_video(char* p)
301 u32 hci_result;
302 u32 value;
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);
312 } else {
313 p += sprintf(p, "ERROR\n");
316 return p;
319 static unsigned long
320 write_video(const char* buffer, unsigned long count)
322 int value;
323 const char* buffer_end = buffer + count;
324 int lcd_out = -1;
325 int crt_out = -1;
326 int tv_out = -1;
327 u32 hci_result;
328 int video_out;
330 /* scan expression. Multiple expressions may be delimited with ; */
331 do {
332 if (snscanf(buffer, count, " lcd_out : %i", &value) == 1)
333 lcd_out = value & 1;
334 else if (snscanf(buffer, count, " crt_out : %i", &value) == 1)
335 crt_out = value & 1;
336 else if (snscanf(buffer, count, " tv_out : %i", &value) == 1)
337 tv_out = value & 1;
338 /* advance to one character past the next ; */
339 do ++buffer;
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;
346 if (lcd_out != -1)
347 _set_bit(&new_video_out, HCI_VIDEO_OUT_LCD, lcd_out);
348 if (crt_out != -1)
349 _set_bit(&new_video_out, HCI_VIDEO_OUT_CRT, crt_out);
350 if (tv_out != -1)
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);
358 return count;
361 static char*
362 read_fan(char* p)
364 u32 hci_result;
365 u32 value;
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);
371 } else {
372 p += sprintf(p, "ERROR\n");
375 return p;
378 static unsigned long
379 write_fan(const char* buffer, unsigned long count)
381 int value;
382 u32 hci_result;
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)
388 return -EFAULT;
389 else
390 force_fan = value;
391 } else {
392 return -EINVAL;
395 return count;
398 static char*
399 read_keys(char* p)
401 u32 hci_result;
402 u32 value;
404 if (!key_event_valid) {
405 hci_read1(HCI_SYSTEM_EVENT, &value, &hci_result);
406 if (hci_result == HCI_SUCCESS) {
407 key_event_valid = 1;
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);
416 } else {
417 p += sprintf(p, "ERROR\n");
418 goto end;
422 p += sprintf(p, "hotkey_ready: %d\n", key_event_valid);
423 p += sprintf(p, "hotkey: 0x%04x\n", last_key_event);
425 end:
426 return p;
429 static unsigned long
430 write_keys(const char* buffer, unsigned long count)
432 int value;
434 if (snscanf(buffer, count, " hotkey_ready : %i", &value) == 1 &&
435 value == 0) {
436 key_event_valid = 0;
437 } else {
438 return -EINVAL;
441 return count;
444 static char*
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);
450 return p;
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 },
465 { 0 , 0 , 0 },
468 static acpi_status
469 add_device(void)
471 struct proc_dir_entry* proc;
472 ProcItem* item;
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;
483 return(AE_OK);
486 static acpi_status
487 remove_device(void)
489 ProcItem* item;
491 for (item = proc_items; item->name; ++item)
492 remove_proc_entry(item->name, toshiba_proc_dir);
493 return(AE_OK);
496 static int __init
497 toshiba_acpi_init(void)
499 acpi_status status = AE_OK;
500 int value;
501 u32 hci_result;
503 /* simple device detection: try reading an HCI register */
504 hci_read1(HCI_LCD_BRIGHTNESS, &value, &hci_result);
505 if (hci_result != HCI_SUCCESS)
506 return -ENODEV;
508 printk("Toshiba Laptop ACPI Extras version %s\n", TOSHIBA_ACPI_VERSION);
510 force_fan = 0;
511 key_event_valid = 0;
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) {
518 status = AE_ERROR;
519 } else {
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;
528 static void __exit
529 toshiba_acpi_exit(void)
531 remove_device();
533 if (toshiba_proc_dir)
534 remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
536 return;
539 module_init(toshiba_acpi_init);
540 module_exit(toshiba_acpi_exit);