1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37#define TOSHIBA_ACPI_VERSION "0.19"
38#define PROC_INTERFACE_VERSION 1
39
40#include <linux/kernel.h>
41#include <linux/module.h>
42#include <linux/init.h>
43#include <linux/types.h>
44#include <linux/proc_fs.h>
45#include <linux/backlight.h>
46#include <linux/platform_device.h>
47#include <linux/rfkill.h>
48
49#include <asm/uaccess.h>
50
51#include <acpi/acpi_drivers.h>
52
53MODULE_AUTHOR("John Belmonte");
54MODULE_DESCRIPTION("Toshiba Laptop ACPI Extras Driver");
55MODULE_LICENSE("GPL");
56
57#define MY_LOGPREFIX "toshiba_acpi: "
58#define MY_ERR KERN_ERR MY_LOGPREFIX
59#define MY_NOTICE KERN_NOTICE MY_LOGPREFIX
60#define MY_INFO KERN_INFO MY_LOGPREFIX
61
62
63#define METHOD_LCD_BRIGHTNESS "\\_SB_.PCI0.VGA_.LCD_._BCM"
64#define METHOD_HCI_1 "\\_SB_.VALD.GHCI"
65#define METHOD_HCI_2 "\\_SB_.VALZ.GHCI"
66#define METHOD_VIDEO_OUT "\\_SB_.VALX.DSSX"
67
68
69
70
71
72
73
74
75
76
77
78#define HCI_WORDS 6
79
80
81#define HCI_SET 0xff00
82#define HCI_GET 0xfe00
83
84
85#define HCI_SUCCESS 0x0000
86#define HCI_FAILURE 0x1000
87#define HCI_NOT_SUPPORTED 0x8000
88#define HCI_EMPTY 0x8c00
89
90
91#define HCI_FAN 0x0004
92#define HCI_SYSTEM_EVENT 0x0016
93#define HCI_VIDEO_OUT 0x001c
94#define HCI_HOTKEY_EVENT 0x001e
95#define HCI_LCD_BRIGHTNESS 0x002a
96#define HCI_WIRELESS 0x0056
97
98
99#define HCI_LCD_BRIGHTNESS_BITS 3
100#define HCI_LCD_BRIGHTNESS_SHIFT (16-HCI_LCD_BRIGHTNESS_BITS)
101#define HCI_LCD_BRIGHTNESS_LEVELS (1 << HCI_LCD_BRIGHTNESS_BITS)
102#define HCI_VIDEO_OUT_LCD 0x1
103#define HCI_VIDEO_OUT_CRT 0x2
104#define HCI_VIDEO_OUT_TV 0x4
105#define HCI_WIRELESS_KILL_SWITCH 0x01
106#define HCI_WIRELESS_BT_PRESENT 0x0f
107#define HCI_WIRELESS_BT_ATTACH 0x40
108#define HCI_WIRELESS_BT_POWER 0x80
109
110static const struct acpi_device_id toshiba_device_ids[] = {
111 {"TOS6200", 0},
112 {"TOS6208", 0},
113 {"TOS1900", 0},
114 {"", 0},
115};
116MODULE_DEVICE_TABLE(acpi, toshiba_device_ids);
117
118
119
120
121static __inline__ void _set_bit(u32 * word, u32 mask, int value)
122{
123 *word = (*word & ~mask) | (mask * value);
124}
125
126
127
128
129static int is_valid_acpi_path(const char *methodName)
130{
131 acpi_handle handle;
132 acpi_status status;
133
134 status = acpi_get_handle(NULL, (char *)methodName, &handle);
135 return !ACPI_FAILURE(status);
136}
137
138static int write_acpi_int(const char *methodName, int val)
139{
140 struct acpi_object_list params;
141 union acpi_object in_objs[1];
142 acpi_status status;
143
144 params.count = ARRAY_SIZE(in_objs);
145 params.pointer = in_objs;
146 in_objs[0].type = ACPI_TYPE_INTEGER;
147 in_objs[0].integer.value = val;
148
149 status = acpi_evaluate_object(NULL, (char *)methodName, ¶ms, NULL);
150 return (status == AE_OK);
151}
152
153#if 0
154static int read_acpi_int(const char *methodName, int *pVal)
155{
156 struct acpi_buffer results;
157 union acpi_object out_objs[1];
158 acpi_status status;
159
160 results.length = sizeof(out_objs);
161 results.pointer = out_objs;
162
163 status = acpi_evaluate_object(0, (char *)methodName, 0, &results);
164 *pVal = out_objs[0].integer.value;
165
166 return (status == AE_OK) && (out_objs[0].type == ACPI_TYPE_INTEGER);
167}
168#endif
169
170static const char *method_hci ;
171
172
173
174
175static acpi_status hci_raw(const u32 in[HCI_WORDS], u32 out[HCI_WORDS])
176{
177 struct acpi_object_list params;
178 union acpi_object in_objs[HCI_WORDS];
179 struct acpi_buffer results;
180 union acpi_object out_objs[HCI_WORDS + 1];
181 acpi_status status;
182 int i;
183
184 params.count = HCI_WORDS;
185 params.pointer = in_objs;
186 for (i = 0; i < HCI_WORDS; ++i) {
187 in_objs[i].type = ACPI_TYPE_INTEGER;
188 in_objs[i].integer.value = in[i];
189 }
190
191 results.length = sizeof(out_objs);
192 results.pointer = out_objs;
193
194 status = acpi_evaluate_object(NULL, (char *)method_hci, ¶ms,
195 &results);
196 if ((status == AE_OK) && (out_objs->package.count <= HCI_WORDS)) {
197 for (i = 0; i < out_objs->package.count; ++i) {
198 out[i] = out_objs->package.elements[i].integer.value;
199 }
200 }
201
202 return status;
203}
204
205
206
207
208
209
210
211static acpi_status hci_write1(u32 reg, u32 in1, u32 * result)
212{
213 u32 in[HCI_WORDS] = { HCI_SET, reg, in1, 0, 0, 0 };
214 u32 out[HCI_WORDS];
215 acpi_status status = hci_raw(in, out);
216 *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
217 return status;
218}
219
220static acpi_status hci_read1(u32 reg, u32 * out1, u32 * result)
221{
222 u32 in[HCI_WORDS] = { HCI_GET, reg, 0, 0, 0, 0 };
223 u32 out[HCI_WORDS];
224 acpi_status status = hci_raw(in, out);
225 *out1 = out[2];
226 *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
227 return status;
228}
229
230static acpi_status hci_write2(u32 reg, u32 in1, u32 in2, u32 *result)
231{
232 u32 in[HCI_WORDS] = { HCI_SET, reg, in1, in2, 0, 0 };
233 u32 out[HCI_WORDS];
234 acpi_status status = hci_raw(in, out);
235 *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
236 return status;
237}
238
239static acpi_status hci_read2(u32 reg, u32 *out1, u32 *out2, u32 *result)
240{
241 u32 in[HCI_WORDS] = { HCI_GET, reg, *out1, *out2, 0, 0 };
242 u32 out[HCI_WORDS];
243 acpi_status status = hci_raw(in, out);
244 *out1 = out[2];
245 *out2 = out[3];
246 *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
247 return status;
248}
249
250struct toshiba_acpi_dev {
251 struct platform_device *p_dev;
252 struct rfkill *bt_rfk;
253
254 const char *bt_name;
255
256 struct mutex mutex;
257};
258
259static struct toshiba_acpi_dev toshiba_acpi = {
260 .bt_name = "Toshiba Bluetooth",
261};
262
263
264
265static u32 hci_get_bt_present(bool *present)
266{
267 u32 hci_result;
268 u32 value, value2;
269
270 value = 0;
271 value2 = 0;
272 hci_read2(HCI_WIRELESS, &value, &value2, &hci_result);
273 if (hci_result == HCI_SUCCESS)
274 *present = (value & HCI_WIRELESS_BT_PRESENT) ? true : false;
275
276 return hci_result;
277}
278
279static u32 hci_get_radio_state(bool *radio_state)
280{
281 u32 hci_result;
282 u32 value, value2;
283
284 value = 0;
285 value2 = 0x0001;
286 hci_read2(HCI_WIRELESS, &value, &value2, &hci_result);
287
288 *radio_state = value & HCI_WIRELESS_KILL_SWITCH;
289 return hci_result;
290}
291
292static int bt_rfkill_set_block(void *data, bool blocked)
293{
294 struct toshiba_acpi_dev *dev = data;
295 u32 result1, result2;
296 u32 value;
297 int err;
298 bool radio_state;
299
300 value = (blocked == false);
301
302 mutex_lock(&dev->mutex);
303 if (hci_get_radio_state(&radio_state) != HCI_SUCCESS) {
304 err = -EBUSY;
305 goto out;
306 }
307
308 if (!radio_state) {
309 err = 0;
310 goto out;
311 }
312
313 hci_write2(HCI_WIRELESS, value, HCI_WIRELESS_BT_POWER, &result1);
314 hci_write2(HCI_WIRELESS, value, HCI_WIRELESS_BT_ATTACH, &result2);
315
316 if (result1 != HCI_SUCCESS || result2 != HCI_SUCCESS)
317 err = -EBUSY;
318 else
319 err = 0;
320 out:
321 mutex_unlock(&dev->mutex);
322 return err;
323}
324
325static void bt_rfkill_poll(struct rfkill *rfkill, void *data)
326{
327 bool new_rfk_state;
328 bool value;
329 u32 hci_result;
330 struct toshiba_acpi_dev *dev = data;
331
332 mutex_lock(&dev->mutex);
333
334 hci_result = hci_get_radio_state(&value);
335 if (hci_result != HCI_SUCCESS) {
336
337 mutex_unlock(&dev->mutex);
338 return;
339 }
340
341 new_rfk_state = value;
342
343 mutex_unlock(&dev->mutex);
344
345 if (rfkill_set_hw_state(rfkill, !new_rfk_state))
346 bt_rfkill_set_block(data, true);
347}
348
349static const struct rfkill_ops toshiba_rfk_ops = {
350 .set_block = bt_rfkill_set_block,
351 .poll = bt_rfkill_poll,
352};
353
354static struct proc_dir_entry *toshiba_proc_dir ;
355static struct backlight_device *toshiba_backlight_device;
356static int force_fan;
357static int last_key_event;
358static int key_event_valid;
359
360typedef struct _ProcItem {
361 const char *name;
362 char *(*read_func) (char *);
363 unsigned long (*write_func) (const char *, unsigned long);
364} ProcItem;
365
366
367
368
369static int
370dispatch_read(char *page, char **start, off_t off, int count, int *eof,
371 ProcItem * item)
372{
373 char *p = page;
374 int len;
375
376 if (off == 0)
377 p = item->read_func(p);
378
379
380 len = (p - page);
381 if (len <= off + count)
382 *eof = 1;
383 *start = page + off;
384 len -= off;
385 if (len > count)
386 len = count;
387 if (len < 0)
388 len = 0;
389 return len;
390}
391
392static int
393dispatch_write(struct file *file, const char __user * buffer,
394 unsigned long count, ProcItem * item)
395{
396 int result;
397 char *tmp_buffer;
398
399
400
401
402
403 tmp_buffer = kmalloc(count + 1, GFP_KERNEL);
404 if (!tmp_buffer)
405 return -ENOMEM;
406
407 if (copy_from_user(tmp_buffer, buffer, count)) {
408 result = -EFAULT;
409 } else {
410 tmp_buffer[count] = 0;
411 result = item->write_func(tmp_buffer, count);
412 }
413 kfree(tmp_buffer);
414 return result;
415}
416
417static int get_lcd(struct backlight_device *bd)
418{
419 u32 hci_result;
420 u32 value;
421
422 hci_read1(HCI_LCD_BRIGHTNESS, &value, &hci_result);
423 if (hci_result == HCI_SUCCESS) {
424 return (value >> HCI_LCD_BRIGHTNESS_SHIFT);
425 } else
426 return -EFAULT;
427}
428
429static char *read_lcd(char *p)
430{
431 int value = get_lcd(NULL);
432
433 if (value >= 0) {
434 p += sprintf(p, "brightness: %d\n", value);
435 p += sprintf(p, "brightness_levels: %d\n",
436 HCI_LCD_BRIGHTNESS_LEVELS);
437 } else {
438 printk(MY_ERR "Error reading LCD brightness\n");
439 }
440
441 return p;
442}
443
444static int set_lcd(int value)
445{
446 u32 hci_result;
447
448 value = value << HCI_LCD_BRIGHTNESS_SHIFT;
449 hci_write1(HCI_LCD_BRIGHTNESS, value, &hci_result);
450 if (hci_result != HCI_SUCCESS)
451 return -EFAULT;
452
453 return 0;
454}
455
456static int set_lcd_status(struct backlight_device *bd)
457{
458 return set_lcd(bd->props.brightness);
459}
460
461static unsigned long write_lcd(const char *buffer, unsigned long count)
462{
463 int value;
464 int ret;
465
466 if (sscanf(buffer, " brightness : %i", &value) == 1 &&
467 value >= 0 && value < HCI_LCD_BRIGHTNESS_LEVELS) {
468 ret = set_lcd(value);
469 if (ret == 0)
470 ret = count;
471 } else {
472 ret = -EINVAL;
473 }
474 return ret;
475}
476
477static char *read_video(char *p)
478{
479 u32 hci_result;
480 u32 value;
481
482 hci_read1(HCI_VIDEO_OUT, &value, &hci_result);
483 if (hci_result == HCI_SUCCESS) {
484 int is_lcd = (value & HCI_VIDEO_OUT_LCD) ? 1 : 0;
485 int is_crt = (value & HCI_VIDEO_OUT_CRT) ? 1 : 0;
486 int is_tv = (value & HCI_VIDEO_OUT_TV) ? 1 : 0;
487 p += sprintf(p, "lcd_out: %d\n", is_lcd);
488 p += sprintf(p, "crt_out: %d\n", is_crt);
489 p += sprintf(p, "tv_out: %d\n", is_tv);
490 } else {
491 printk(MY_ERR "Error reading video out status\n");
492 }
493
494 return p;
495}
496
497static unsigned long write_video(const char *buffer, unsigned long count)
498{
499 int value;
500 int remain = count;
501 int lcd_out = -1;
502 int crt_out = -1;
503 int tv_out = -1;
504 u32 hci_result;
505 u32 video_out;
506
507
508
509
510
511 while (remain) {
512 if (sscanf(buffer, " lcd_out : %i", &value) == 1)
513 lcd_out = value & 1;
514 else if (sscanf(buffer, " crt_out : %i", &value) == 1)
515 crt_out = value & 1;
516 else if (sscanf(buffer, " tv_out : %i", &value) == 1)
517 tv_out = value & 1;
518
519 do {
520 ++buffer;
521 --remain;
522 }
523 while (remain && *(buffer - 1) != ';');
524 }
525
526 hci_read1(HCI_VIDEO_OUT, &video_out, &hci_result);
527 if (hci_result == HCI_SUCCESS) {
528 unsigned int new_video_out = video_out;
529 if (lcd_out != -1)
530 _set_bit(&new_video_out, HCI_VIDEO_OUT_LCD, lcd_out);
531 if (crt_out != -1)
532 _set_bit(&new_video_out, HCI_VIDEO_OUT_CRT, crt_out);
533 if (tv_out != -1)
534 _set_bit(&new_video_out, HCI_VIDEO_OUT_TV, tv_out);
535
536
537 if (new_video_out != video_out)
538 write_acpi_int(METHOD_VIDEO_OUT, new_video_out);
539 } else {
540 return -EFAULT;
541 }
542
543 return count;
544}
545
546static char *read_fan(char *p)
547{
548 u32 hci_result;
549 u32 value;
550
551 hci_read1(HCI_FAN, &value, &hci_result);
552 if (hci_result == HCI_SUCCESS) {
553 p += sprintf(p, "running: %d\n", (value > 0));
554 p += sprintf(p, "force_on: %d\n", force_fan);
555 } else {
556 printk(MY_ERR "Error reading fan status\n");
557 }
558
559 return p;
560}
561
562static unsigned long write_fan(const char *buffer, unsigned long count)
563{
564 int value;
565 u32 hci_result;
566
567 if (sscanf(buffer, " force_on : %i", &value) == 1 &&
568 value >= 0 && value <= 1) {
569 hci_write1(HCI_FAN, value, &hci_result);
570 if (hci_result != HCI_SUCCESS)
571 return -EFAULT;
572 else
573 force_fan = value;
574 } else {
575 return -EINVAL;
576 }
577
578 return count;
579}
580
581static char *read_keys(char *p)
582{
583 u32 hci_result;
584 u32 value;
585
586 if (!key_event_valid) {
587 hci_read1(HCI_SYSTEM_EVENT, &value, &hci_result);
588 if (hci_result == HCI_SUCCESS) {
589 key_event_valid = 1;
590 last_key_event = value;
591 } else if (hci_result == HCI_EMPTY) {
592
593 } else if (hci_result == HCI_NOT_SUPPORTED) {
594
595
596
597 hci_write1(HCI_SYSTEM_EVENT, 1, &hci_result);
598 printk(MY_NOTICE "Re-enabled hotkeys\n");
599 } else {
600 printk(MY_ERR "Error reading hotkey status\n");
601 goto end;
602 }
603 }
604
605 p += sprintf(p, "hotkey_ready: %d\n", key_event_valid);
606 p += sprintf(p, "hotkey: 0x%04x\n", last_key_event);
607
608 end:
609 return p;
610}
611
612static unsigned long write_keys(const char *buffer, unsigned long count)
613{
614 int value;
615
616 if (sscanf(buffer, " hotkey_ready : %i", &value) == 1 && value == 0) {
617 key_event_valid = 0;
618 } else {
619 return -EINVAL;
620 }
621
622 return count;
623}
624
625static char *read_version(char *p)
626{
627 p += sprintf(p, "driver: %s\n", TOSHIBA_ACPI_VERSION);
628 p += sprintf(p, "proc_interface: %d\n",
629 PROC_INTERFACE_VERSION);
630 return p;
631}
632
633
634
635
636#define PROC_TOSHIBA "toshiba"
637
638static ProcItem proc_items[] = {
639 {"lcd", read_lcd, write_lcd},
640 {"video", read_video, write_video},
641 {"fan", read_fan, write_fan},
642 {"keys", read_keys, write_keys},
643 {"version", read_version, NULL},
644 {NULL}
645};
646
647static acpi_status __init add_device(void)
648{
649 struct proc_dir_entry *proc;
650 ProcItem *item;
651
652 for (item = proc_items; item->name; ++item) {
653 proc = create_proc_read_entry(item->name,
654 S_IFREG | S_IRUGO | S_IWUSR,
655 toshiba_proc_dir,
656 (read_proc_t *) dispatch_read,
657 item);
658 if (proc && item->write_func)
659 proc->write_proc = (write_proc_t *) dispatch_write;
660 }
661
662 return AE_OK;
663}
664
665static acpi_status remove_device(void)
666{
667 ProcItem *item;
668
669 for (item = proc_items; item->name; ++item)
670 remove_proc_entry(item->name, toshiba_proc_dir);
671 return AE_OK;
672}
673
674static struct backlight_ops toshiba_backlight_data = {
675 .get_brightness = get_lcd,
676 .update_status = set_lcd_status,
677};
678
679static void toshiba_acpi_exit(void)
680{
681 if (toshiba_acpi.bt_rfk) {
682 rfkill_unregister(toshiba_acpi.bt_rfk);
683 rfkill_destroy(toshiba_acpi.bt_rfk);
684 }
685
686 if (toshiba_backlight_device)
687 backlight_device_unregister(toshiba_backlight_device);
688
689 remove_device();
690
691 if (toshiba_proc_dir)
692 remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
693
694 platform_device_unregister(toshiba_acpi.p_dev);
695
696 return;
697}
698
699static int __init toshiba_acpi_init(void)
700{
701 acpi_status status = AE_OK;
702 u32 hci_result;
703 bool bt_present;
704 int ret = 0;
705
706 if (acpi_disabled)
707 return -ENODEV;
708
709
710 if (is_valid_acpi_path(METHOD_HCI_1))
711 method_hci = METHOD_HCI_1;
712 else if (is_valid_acpi_path(METHOD_HCI_2))
713 method_hci = METHOD_HCI_2;
714 else
715 return -ENODEV;
716
717 printk(MY_INFO "Toshiba Laptop ACPI Extras version %s\n",
718 TOSHIBA_ACPI_VERSION);
719 printk(MY_INFO " HCI method: %s\n", method_hci);
720
721 mutex_init(&toshiba_acpi.mutex);
722
723 toshiba_acpi.p_dev = platform_device_register_simple("toshiba_acpi",
724 -1, NULL, 0);
725 if (IS_ERR(toshiba_acpi.p_dev)) {
726 ret = PTR_ERR(toshiba_acpi.p_dev);
727 printk(MY_ERR "unable to register platform device\n");
728 toshiba_acpi.p_dev = NULL;
729 toshiba_acpi_exit();
730 return ret;
731 }
732
733 force_fan = 0;
734 key_event_valid = 0;
735
736
737 hci_write1(HCI_SYSTEM_EVENT, 1, &hci_result);
738
739 toshiba_proc_dir = proc_mkdir(PROC_TOSHIBA, acpi_root_dir);
740 if (!toshiba_proc_dir) {
741 toshiba_acpi_exit();
742 return -ENODEV;
743 } else {
744 status = add_device();
745 if (ACPI_FAILURE(status)) {
746 toshiba_acpi_exit();
747 return -ENODEV;
748 }
749 }
750
751 toshiba_backlight_device = backlight_device_register("toshiba",
752 &toshiba_acpi.p_dev->dev,
753 NULL,
754 &toshiba_backlight_data);
755 if (IS_ERR(toshiba_backlight_device)) {
756 ret = PTR_ERR(toshiba_backlight_device);
757
758 printk(KERN_ERR "Could not register toshiba backlight device\n");
759 toshiba_backlight_device = NULL;
760 toshiba_acpi_exit();
761 return ret;
762 }
763 toshiba_backlight_device->props.max_brightness = HCI_LCD_BRIGHTNESS_LEVELS - 1;
764
765
766 if (hci_get_bt_present(&bt_present) == HCI_SUCCESS && bt_present) {
767 toshiba_acpi.bt_rfk = rfkill_alloc(toshiba_acpi.bt_name,
768 &toshiba_acpi.p_dev->dev,
769 RFKILL_TYPE_BLUETOOTH,
770 &toshiba_rfk_ops,
771 &toshiba_acpi);
772 if (!toshiba_acpi.bt_rfk) {
773 printk(MY_ERR "unable to allocate rfkill device\n");
774 toshiba_acpi_exit();
775 return -ENOMEM;
776 }
777
778 ret = rfkill_register(toshiba_acpi.bt_rfk);
779 if (ret) {
780 printk(MY_ERR "unable to register rfkill device\n");
781 rfkill_destroy(toshiba_acpi.bt_rfk);
782 toshiba_acpi_exit();
783 return ret;
784 }
785 }
786
787 return 0;
788}
789
790module_init(toshiba_acpi_init);
791module_exit(toshiba_acpi_exit);
792