blob: 40e60fc2e596daead5c656396aa84fca0587868b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * toshiba_acpi.c - Toshiba Laptop ACPI Extras
3 *
4 *
5 * Copyright (C) 2002-2004 John Belmonte
philipl@overt.orgc41a40c2008-08-30 11:57:39 -04006 * Copyright (C) 2008 Philip Langdale
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 *
23 * The devolpment page for this driver is located at
24 * http://memebeam.org/toys/ToshibaAcpiDriver.
25 *
26 * Credits:
27 * Jonathan A. Buzzard - Toshiba HCI info, and critical tips on reverse
28 * engineering the Windows drivers
29 * Yasushi Nagato - changes for linux kernel 2.4 -> 2.5
30 * Rob Miller - TV out and hotkeys help
31 *
32 *
33 * TODO
34 *
35 */
36
philipl@overt.orgc41a40c2008-08-30 11:57:39 -040037#define TOSHIBA_ACPI_VERSION "0.19"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#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>
Holger Machtc9263552006-10-20 14:30:29 -070045#include <linux/backlight.h>
philipl@overt.orgc41a40c2008-08-30 11:57:39 -040046#include <linux/platform_device.h>
47#include <linux/rfkill.h>
48#include <linux/input-polldev.h>
Holger Machtc9263552006-10-20 14:30:29 -070049
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <asm/uaccess.h>
51
52#include <acpi/acpi_drivers.h>
53
54MODULE_AUTHOR("John Belmonte");
55MODULE_DESCRIPTION("Toshiba Laptop ACPI Extras Driver");
56MODULE_LICENSE("GPL");
57
58#define MY_LOGPREFIX "toshiba_acpi: "
59#define MY_ERR KERN_ERR MY_LOGPREFIX
60#define MY_NOTICE KERN_NOTICE MY_LOGPREFIX
61#define MY_INFO KERN_INFO MY_LOGPREFIX
62
63/* Toshiba ACPI method paths */
64#define METHOD_LCD_BRIGHTNESS "\\_SB_.PCI0.VGA_.LCD_._BCM"
65#define METHOD_HCI_1 "\\_SB_.VALD.GHCI"
66#define METHOD_HCI_2 "\\_SB_.VALZ.GHCI"
67#define METHOD_VIDEO_OUT "\\_SB_.VALX.DSSX"
68
69/* Toshiba HCI interface definitions
70 *
71 * HCI is Toshiba's "Hardware Control Interface" which is supposed to
72 * be uniform across all their models. Ideally we would just call
73 * dedicated ACPI methods instead of using this primitive interface.
74 * However the ACPI methods seem to be incomplete in some areas (for
75 * example they allow setting, but not reading, the LCD brightness value),
76 * so this is still useful.
77 */
78
79#define HCI_WORDS 6
80
81/* operations */
82#define HCI_SET 0xff00
83#define HCI_GET 0xfe00
84
85/* return codes */
86#define HCI_SUCCESS 0x0000
87#define HCI_FAILURE 0x1000
88#define HCI_NOT_SUPPORTED 0x8000
89#define HCI_EMPTY 0x8c00
90
91/* registers */
92#define HCI_FAN 0x0004
93#define HCI_SYSTEM_EVENT 0x0016
94#define HCI_VIDEO_OUT 0x001c
95#define HCI_HOTKEY_EVENT 0x001e
96#define HCI_LCD_BRIGHTNESS 0x002a
philipl@overt.orgc41a40c2008-08-30 11:57:39 -040097#define HCI_WIRELESS 0x0056
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99/* field definitions */
100#define HCI_LCD_BRIGHTNESS_BITS 3
101#define HCI_LCD_BRIGHTNESS_SHIFT (16-HCI_LCD_BRIGHTNESS_BITS)
102#define HCI_LCD_BRIGHTNESS_LEVELS (1 << HCI_LCD_BRIGHTNESS_BITS)
103#define HCI_VIDEO_OUT_LCD 0x1
104#define HCI_VIDEO_OUT_CRT 0x2
105#define HCI_VIDEO_OUT_TV 0x4
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400106#define HCI_WIRELESS_KILL_SWITCH 0x01
107#define HCI_WIRELESS_BT_PRESENT 0x0f
108#define HCI_WIRELESS_BT_ATTACH 0x40
109#define HCI_WIRELESS_BT_POWER 0x80
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
arvidjaar@mail.ru4db42c52008-03-04 15:06:34 -0800111static const struct acpi_device_id toshiba_device_ids[] = {
112 {"TOS6200", 0},
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400113 {"TOS6208", 0},
arvidjaar@mail.ru4db42c52008-03-04 15:06:34 -0800114 {"TOS1900", 0},
115 {"", 0},
116};
117MODULE_DEVICE_TABLE(acpi, toshiba_device_ids);
118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119/* utility
120 */
121
Len Brown4be44fc2005-08-05 00:44:28 -0400122static __inline__ void _set_bit(u32 * word, u32 mask, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
124 *word = (*word & ~mask) | (mask * value);
125}
126
127/* acpi interface wrappers
128 */
129
Len Brown4be44fc2005-08-05 00:44:28 -0400130static int is_valid_acpi_path(const char *methodName)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
132 acpi_handle handle;
133 acpi_status status;
134
Len Brown4be44fc2005-08-05 00:44:28 -0400135 status = acpi_get_handle(NULL, (char *)methodName, &handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 return !ACPI_FAILURE(status);
137}
138
Len Brown4be44fc2005-08-05 00:44:28 -0400139static int write_acpi_int(const char *methodName, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
141 struct acpi_object_list params;
142 union acpi_object in_objs[1];
143 acpi_status status;
144
Ahmed S. Darwishb2b79102007-02-06 16:14:43 -0800145 params.count = ARRAY_SIZE(in_objs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 params.pointer = in_objs;
147 in_objs[0].type = ACPI_TYPE_INTEGER;
148 in_objs[0].integer.value = val;
149
Len Brown4be44fc2005-08-05 00:44:28 -0400150 status = acpi_evaluate_object(NULL, (char *)methodName, &params, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 return (status == AE_OK);
152}
153
154#if 0
Len Brown4be44fc2005-08-05 00:44:28 -0400155static int read_acpi_int(const char *methodName, int *pVal)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
157 struct acpi_buffer results;
158 union acpi_object out_objs[1];
159 acpi_status status;
160
161 results.length = sizeof(out_objs);
162 results.pointer = out_objs;
163
Len Brown4be44fc2005-08-05 00:44:28 -0400164 status = acpi_evaluate_object(0, (char *)methodName, 0, &results);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 *pVal = out_objs[0].integer.value;
166
167 return (status == AE_OK) && (out_objs[0].type == ACPI_TYPE_INTEGER);
168}
169#endif
170
Len Brown4be44fc2005-08-05 00:44:28 -0400171static const char *method_hci /*= 0*/ ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173/* Perform a raw HCI call. Here we don't care about input or output buffer
174 * format.
175 */
Len Brown4be44fc2005-08-05 00:44:28 -0400176static acpi_status hci_raw(const u32 in[HCI_WORDS], u32 out[HCI_WORDS])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
178 struct acpi_object_list params;
179 union acpi_object in_objs[HCI_WORDS];
180 struct acpi_buffer results;
Len Brown4be44fc2005-08-05 00:44:28 -0400181 union acpi_object out_objs[HCI_WORDS + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 acpi_status status;
183 int i;
184
185 params.count = HCI_WORDS;
186 params.pointer = in_objs;
187 for (i = 0; i < HCI_WORDS; ++i) {
188 in_objs[i].type = ACPI_TYPE_INTEGER;
189 in_objs[i].integer.value = in[i];
190 }
191
192 results.length = sizeof(out_objs);
193 results.pointer = out_objs;
194
Len Brown4be44fc2005-08-05 00:44:28 -0400195 status = acpi_evaluate_object(NULL, (char *)method_hci, &params,
196 &results);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 if ((status == AE_OK) && (out_objs->package.count <= HCI_WORDS)) {
198 for (i = 0; i < out_objs->package.count; ++i) {
199 out[i] = out_objs->package.elements[i].integer.value;
200 }
201 }
202
203 return status;
204}
205
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400206/* common hci tasks (get or set one or two value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 *
208 * In addition to the ACPI status, the HCI system returns a result which
209 * may be useful (such as "not supported").
210 */
211
Len Brown4be44fc2005-08-05 00:44:28 -0400212static acpi_status hci_write1(u32 reg, u32 in1, u32 * result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
214 u32 in[HCI_WORDS] = { HCI_SET, reg, in1, 0, 0, 0 };
215 u32 out[HCI_WORDS];
216 acpi_status status = hci_raw(in, out);
217 *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
218 return status;
219}
220
Len Brown4be44fc2005-08-05 00:44:28 -0400221static acpi_status hci_read1(u32 reg, u32 * out1, u32 * result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222{
223 u32 in[HCI_WORDS] = { HCI_GET, reg, 0, 0, 0, 0 };
224 u32 out[HCI_WORDS];
225 acpi_status status = hci_raw(in, out);
226 *out1 = out[2];
227 *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
228 return status;
229}
230
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400231static acpi_status hci_write2(u32 reg, u32 in1, u32 in2, u32 *result)
232{
233 u32 in[HCI_WORDS] = { HCI_SET, reg, in1, in2, 0, 0 };
234 u32 out[HCI_WORDS];
235 acpi_status status = hci_raw(in, out);
236 *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
237 return status;
238}
239
240static acpi_status hci_read2(u32 reg, u32 *out1, u32 *out2, u32 *result)
241{
242 u32 in[HCI_WORDS] = { HCI_GET, reg, *out1, *out2, 0, 0 };
243 u32 out[HCI_WORDS];
244 acpi_status status = hci_raw(in, out);
245 *out1 = out[2];
246 *out2 = out[3];
247 *result = (status == AE_OK) ? out[0] : HCI_FAILURE;
248 return status;
249}
250
251struct toshiba_acpi_dev {
252 struct platform_device *p_dev;
253 struct rfkill *rfk_dev;
254 struct input_polled_dev *poll_dev;
255
256 const char *bt_name;
257 const char *rfk_name;
258
259 bool last_rfk_state;
260
261 struct mutex mutex;
262};
263
264static struct toshiba_acpi_dev toshiba_acpi = {
265 .bt_name = "Toshiba Bluetooth",
266 .rfk_name = "Toshiba RFKill Switch",
267 .last_rfk_state = false,
268};
269
270/* Bluetooth rfkill handlers */
271
272static u32 hci_get_bt_present(bool *present)
273{
274 u32 hci_result;
275 u32 value, value2;
276
277 value = 0;
278 value2 = 0;
279 hci_read2(HCI_WIRELESS, &value, &value2, &hci_result);
280 if (hci_result == HCI_SUCCESS)
281 *present = (value & HCI_WIRELESS_BT_PRESENT) ? true : false;
282
283 return hci_result;
284}
285
286static u32 hci_get_bt_on(bool *on)
287{
288 u32 hci_result;
289 u32 value, value2;
290
291 value = 0;
292 value2 = 0x0001;
293 hci_read2(HCI_WIRELESS, &value, &value2, &hci_result);
294 if (hci_result == HCI_SUCCESS)
295 *on = (value & HCI_WIRELESS_BT_POWER) &&
296 (value & HCI_WIRELESS_BT_ATTACH);
297
298 return hci_result;
299}
300
301static u32 hci_get_radio_state(bool *radio_state)
302{
303 u32 hci_result;
304 u32 value, value2;
305
306 value = 0;
307 value2 = 0x0001;
308 hci_read2(HCI_WIRELESS, &value, &value2, &hci_result);
309
310 *radio_state = value & HCI_WIRELESS_KILL_SWITCH;
311 return hci_result;
312}
313
314static int bt_rfkill_toggle_radio(void *data, enum rfkill_state state)
315{
316 u32 result1, result2;
317 u32 value;
318 bool radio_state;
319 struct toshiba_acpi_dev *dev = data;
320
321 value = (state == RFKILL_STATE_UNBLOCKED);
322
323 if (hci_get_radio_state(&radio_state) != HCI_SUCCESS)
324 return -EFAULT;
325
326 switch (state) {
327 case RFKILL_STATE_UNBLOCKED:
328 if (!radio_state)
329 return -EPERM;
330 break;
331 case RFKILL_STATE_SOFT_BLOCKED:
332 break;
333 default:
334 return -EINVAL;
335 }
336
337 mutex_lock(&dev->mutex);
338 hci_write2(HCI_WIRELESS, value, HCI_WIRELESS_BT_POWER, &result1);
339 hci_write2(HCI_WIRELESS, value, HCI_WIRELESS_BT_ATTACH, &result2);
340 mutex_unlock(&dev->mutex);
341
342 if (result1 != HCI_SUCCESS || result2 != HCI_SUCCESS)
343 return -EFAULT;
344
345 return 0;
346}
347
348static void bt_poll_rfkill(struct input_polled_dev *poll_dev)
349{
350 bool state_changed;
351 bool new_rfk_state;
352 bool value;
353 u32 hci_result;
354 struct toshiba_acpi_dev *dev = poll_dev->private;
355
356 hci_result = hci_get_radio_state(&value);
357 if (hci_result != HCI_SUCCESS)
358 return; /* Can't do anything useful */
359
360 new_rfk_state = value;
361
362 mutex_lock(&dev->mutex);
363 state_changed = new_rfk_state != dev->last_rfk_state;
364 dev->last_rfk_state = new_rfk_state;
365 mutex_unlock(&dev->mutex);
366
367 if (unlikely(state_changed)) {
368 rfkill_force_state(dev->rfk_dev,
369 new_rfk_state ?
370 RFKILL_STATE_SOFT_BLOCKED :
371 RFKILL_STATE_HARD_BLOCKED);
372 input_report_switch(poll_dev->input, SW_RFKILL_ALL,
373 new_rfk_state);
Len Browncab08962008-10-24 15:39:47 -0400374 input_sync(poll_dev->input);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400375 }
376}
377
Len Brown4be44fc2005-08-05 00:44:28 -0400378static struct proc_dir_entry *toshiba_proc_dir /*= 0*/ ;
Holger Machtc9263552006-10-20 14:30:29 -0700379static struct backlight_device *toshiba_backlight_device;
Len Brown4be44fc2005-08-05 00:44:28 -0400380static int force_fan;
381static int last_key_event;
382static int key_event_valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Len Brown4be44fc2005-08-05 00:44:28 -0400384typedef struct _ProcItem {
385 const char *name;
386 char *(*read_func) (char *);
387 unsigned long (*write_func) (const char *, unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388} ProcItem;
389
390/* proc file handlers
391 */
392
393static int
Len Brown4be44fc2005-08-05 00:44:28 -0400394dispatch_read(char *page, char **start, off_t off, int count, int *eof,
395 ProcItem * item)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396{
Len Brown4be44fc2005-08-05 00:44:28 -0400397 char *p = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 int len;
399
400 if (off == 0)
401 p = item->read_func(p);
402
403 /* ISSUE: I don't understand this code */
404 len = (p - page);
Len Brown4be44fc2005-08-05 00:44:28 -0400405 if (len <= off + count)
406 *eof = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 *start = page + off;
408 len -= off;
Len Brown4be44fc2005-08-05 00:44:28 -0400409 if (len > count)
410 len = count;
411 if (len < 0)
412 len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 return len;
414}
415
416static int
Len Brown4be44fc2005-08-05 00:44:28 -0400417dispatch_write(struct file *file, const char __user * buffer,
418 unsigned long count, ProcItem * item)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{
420 int result;
Len Brown4be44fc2005-08-05 00:44:28 -0400421 char *tmp_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
423 /* Arg buffer points to userspace memory, which can't be accessed
424 * directly. Since we're making a copy, zero-terminate the
425 * destination so that sscanf can be used on it safely.
426 */
427 tmp_buffer = kmalloc(count + 1, GFP_KERNEL);
Len Brown4be44fc2005-08-05 00:44:28 -0400428 if (!tmp_buffer)
Panagiotis Issarisf4224152005-03-30 22:15:36 -0500429 return -ENOMEM;
430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 if (copy_from_user(tmp_buffer, buffer, count)) {
432 result = -EFAULT;
Len Brown4be44fc2005-08-05 00:44:28 -0400433 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 tmp_buffer[count] = 0;
435 result = item->write_func(tmp_buffer, count);
436 }
437 kfree(tmp_buffer);
438 return result;
439}
440
Holger Machtc9263552006-10-20 14:30:29 -0700441static int get_lcd(struct backlight_device *bd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442{
443 u32 hci_result;
444 u32 value;
445
446 hci_read1(HCI_LCD_BRIGHTNESS, &value, &hci_result);
447 if (hci_result == HCI_SUCCESS) {
Holger Machtc9263552006-10-20 14:30:29 -0700448 return (value >> HCI_LCD_BRIGHTNESS_SHIFT);
449 } else
450 return -EFAULT;
451}
452
453static char *read_lcd(char *p)
454{
455 int value = get_lcd(NULL);
456
457 if (value >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 p += sprintf(p, "brightness: %d\n", value);
459 p += sprintf(p, "brightness_levels: %d\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400460 HCI_LCD_BRIGHTNESS_LEVELS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 } else {
462 printk(MY_ERR "Error reading LCD brightness\n");
463 }
464
465 return p;
466}
467
Holger Machtc9263552006-10-20 14:30:29 -0700468static int set_lcd(int value)
469{
470 u32 hci_result;
471
472 value = value << HCI_LCD_BRIGHTNESS_SHIFT;
473 hci_write1(HCI_LCD_BRIGHTNESS, value, &hci_result);
474 if (hci_result != HCI_SUCCESS)
475 return -EFAULT;
476
477 return 0;
478}
479
480static int set_lcd_status(struct backlight_device *bd)
481{
Richard Purdie599a52d2007-02-10 23:07:48 +0000482 return set_lcd(bd->props.brightness);
Holger Machtc9263552006-10-20 14:30:29 -0700483}
484
Len Brown4be44fc2005-08-05 00:44:28 -0400485static unsigned long write_lcd(const char *buffer, unsigned long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
487 int value;
Matthijs van Otterdijkc8af57e2007-01-05 16:37:03 -0800488 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
490 if (sscanf(buffer, " brightness : %i", &value) == 1 &&
Matthijs van Otterdijkc8af57e2007-01-05 16:37:03 -0800491 value >= 0 && value < HCI_LCD_BRIGHTNESS_LEVELS) {
Holger Machtc9263552006-10-20 14:30:29 -0700492 ret = set_lcd(value);
Matthijs van Otterdijkc8af57e2007-01-05 16:37:03 -0800493 if (ret == 0)
494 ret = count;
495 } else {
Holger Machtc9263552006-10-20 14:30:29 -0700496 ret = -EINVAL;
Matthijs van Otterdijkc8af57e2007-01-05 16:37:03 -0800497 }
Holger Machtc9263552006-10-20 14:30:29 -0700498 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499}
500
Len Brown4be44fc2005-08-05 00:44:28 -0400501static char *read_video(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
503 u32 hci_result;
504 u32 value;
505
506 hci_read1(HCI_VIDEO_OUT, &value, &hci_result);
507 if (hci_result == HCI_SUCCESS) {
508 int is_lcd = (value & HCI_VIDEO_OUT_LCD) ? 1 : 0;
509 int is_crt = (value & HCI_VIDEO_OUT_CRT) ? 1 : 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400510 int is_tv = (value & HCI_VIDEO_OUT_TV) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 p += sprintf(p, "lcd_out: %d\n", is_lcd);
512 p += sprintf(p, "crt_out: %d\n", is_crt);
513 p += sprintf(p, "tv_out: %d\n", is_tv);
514 } else {
515 printk(MY_ERR "Error reading video out status\n");
516 }
517
518 return p;
519}
520
Len Brown4be44fc2005-08-05 00:44:28 -0400521static unsigned long write_video(const char *buffer, unsigned long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
523 int value;
524 int remain = count;
525 int lcd_out = -1;
526 int crt_out = -1;
527 int tv_out = -1;
528 u32 hci_result;
Al Virob4482a42007-10-14 19:35:40 +0100529 u32 video_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
531 /* scan expression. Multiple expressions may be delimited with ;
532 *
533 * NOTE: to keep scanning simple, invalid fields are ignored
534 */
535 while (remain) {
536 if (sscanf(buffer, " lcd_out : %i", &value) == 1)
537 lcd_out = value & 1;
538 else if (sscanf(buffer, " crt_out : %i", &value) == 1)
539 crt_out = value & 1;
540 else if (sscanf(buffer, " tv_out : %i", &value) == 1)
541 tv_out = value & 1;
542 /* advance to one character past the next ; */
543 do {
544 ++buffer;
545 --remain;
546 }
Len Brown4be44fc2005-08-05 00:44:28 -0400547 while (remain && *(buffer - 1) != ';');
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 }
549
550 hci_read1(HCI_VIDEO_OUT, &video_out, &hci_result);
551 if (hci_result == HCI_SUCCESS) {
Harvey Harrison9e113e02008-09-22 14:37:29 -0700552 unsigned int new_video_out = video_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 if (lcd_out != -1)
554 _set_bit(&new_video_out, HCI_VIDEO_OUT_LCD, lcd_out);
555 if (crt_out != -1)
556 _set_bit(&new_video_out, HCI_VIDEO_OUT_CRT, crt_out);
557 if (tv_out != -1)
558 _set_bit(&new_video_out, HCI_VIDEO_OUT_TV, tv_out);
559 /* To avoid unnecessary video disruption, only write the new
560 * video setting if something changed. */
561 if (new_video_out != video_out)
562 write_acpi_int(METHOD_VIDEO_OUT, new_video_out);
563 } else {
564 return -EFAULT;
565 }
566
567 return count;
568}
569
Len Brown4be44fc2005-08-05 00:44:28 -0400570static char *read_fan(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571{
572 u32 hci_result;
573 u32 value;
574
575 hci_read1(HCI_FAN, &value, &hci_result);
576 if (hci_result == HCI_SUCCESS) {
577 p += sprintf(p, "running: %d\n", (value > 0));
578 p += sprintf(p, "force_on: %d\n", force_fan);
579 } else {
580 printk(MY_ERR "Error reading fan status\n");
581 }
582
583 return p;
584}
585
Len Brown4be44fc2005-08-05 00:44:28 -0400586static unsigned long write_fan(const char *buffer, unsigned long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587{
588 int value;
589 u32 hci_result;
590
591 if (sscanf(buffer, " force_on : %i", &value) == 1 &&
Len Brown4be44fc2005-08-05 00:44:28 -0400592 value >= 0 && value <= 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 hci_write1(HCI_FAN, value, &hci_result);
594 if (hci_result != HCI_SUCCESS)
595 return -EFAULT;
596 else
597 force_fan = value;
598 } else {
599 return -EINVAL;
600 }
601
602 return count;
603}
604
Len Brown4be44fc2005-08-05 00:44:28 -0400605static char *read_keys(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
607 u32 hci_result;
608 u32 value;
609
610 if (!key_event_valid) {
611 hci_read1(HCI_SYSTEM_EVENT, &value, &hci_result);
612 if (hci_result == HCI_SUCCESS) {
613 key_event_valid = 1;
614 last_key_event = value;
615 } else if (hci_result == HCI_EMPTY) {
616 /* better luck next time */
617 } else if (hci_result == HCI_NOT_SUPPORTED) {
618 /* This is a workaround for an unresolved issue on
619 * some machines where system events sporadically
620 * become disabled. */
621 hci_write1(HCI_SYSTEM_EVENT, 1, &hci_result);
622 printk(MY_NOTICE "Re-enabled hotkeys\n");
623 } else {
624 printk(MY_ERR "Error reading hotkey status\n");
625 goto end;
626 }
627 }
628
629 p += sprintf(p, "hotkey_ready: %d\n", key_event_valid);
630 p += sprintf(p, "hotkey: 0x%04x\n", last_key_event);
631
Len Brown4be44fc2005-08-05 00:44:28 -0400632 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 return p;
634}
635
Len Brown4be44fc2005-08-05 00:44:28 -0400636static unsigned long write_keys(const char *buffer, unsigned long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
638 int value;
639
Len Brown4be44fc2005-08-05 00:44:28 -0400640 if (sscanf(buffer, " hotkey_ready : %i", &value) == 1 && value == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 key_event_valid = 0;
642 } else {
643 return -EINVAL;
644 }
645
646 return count;
647}
648
Len Brown4be44fc2005-08-05 00:44:28 -0400649static char *read_version(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650{
651 p += sprintf(p, "driver: %s\n", TOSHIBA_ACPI_VERSION);
652 p += sprintf(p, "proc_interface: %d\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400653 PROC_INTERFACE_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 return p;
655}
656
657/* proc and module init
658 */
659
660#define PROC_TOSHIBA "toshiba"
661
Len Brown4be44fc2005-08-05 00:44:28 -0400662static ProcItem proc_items[] = {
663 {"lcd", read_lcd, write_lcd},
664 {"video", read_video, write_video},
665 {"fan", read_fan, write_fan},
666 {"keys", read_keys, write_keys},
667 {"version", read_version, NULL},
668 {NULL}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669};
670
Len Brown4be44fc2005-08-05 00:44:28 -0400671static acpi_status __init add_device(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
Len Brown4be44fc2005-08-05 00:44:28 -0400673 struct proc_dir_entry *proc;
674 ProcItem *item;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
Len Brown4be44fc2005-08-05 00:44:28 -0400676 for (item = proc_items; item->name; ++item) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 proc = create_proc_read_entry(item->name,
Len Brown4be44fc2005-08-05 00:44:28 -0400678 S_IFREG | S_IRUGO | S_IWUSR,
679 toshiba_proc_dir,
680 (read_proc_t *) dispatch_read,
681 item);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 if (proc)
683 proc->owner = THIS_MODULE;
684 if (proc && item->write_func)
Len Brown4be44fc2005-08-05 00:44:28 -0400685 proc->write_proc = (write_proc_t *) dispatch_write;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 }
687
688 return AE_OK;
689}
690
Randy Dunlapb1d93de2007-06-16 10:16:02 -0700691static acpi_status remove_device(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692{
Len Brown4be44fc2005-08-05 00:44:28 -0400693 ProcItem *item;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695 for (item = proc_items; item->name; ++item)
696 remove_proc_entry(item->name, toshiba_proc_dir);
697 return AE_OK;
698}
699
Richard Purdie599a52d2007-02-10 23:07:48 +0000700static struct backlight_ops toshiba_backlight_data = {
Holger Machtc9263552006-10-20 14:30:29 -0700701 .get_brightness = get_lcd,
702 .update_status = set_lcd_status,
Holger Machtc9263552006-10-20 14:30:29 -0700703};
704
Sam Ravnborgb2b77b22007-06-01 00:47:12 -0700705static void toshiba_acpi_exit(void)
Holger Machtc9263552006-10-20 14:30:29 -0700706{
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400707 if (toshiba_acpi.poll_dev) {
708 input_unregister_polled_device(toshiba_acpi.poll_dev);
709 input_free_polled_device(toshiba_acpi.poll_dev);
710 }
711
712 if (toshiba_acpi.rfk_dev)
713 rfkill_unregister(toshiba_acpi.rfk_dev);
714
Holger Machtc9263552006-10-20 14:30:29 -0700715 if (toshiba_backlight_device)
716 backlight_device_unregister(toshiba_backlight_device);
717
718 remove_device();
719
720 if (toshiba_proc_dir)
721 remove_proc_entry(PROC_TOSHIBA, acpi_root_dir);
722
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400723 platform_device_unregister(toshiba_acpi.p_dev);
724
Holger Machtc9263552006-10-20 14:30:29 -0700725 return;
726}
727
Len Brown4be44fc2005-08-05 00:44:28 -0400728static int __init toshiba_acpi_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729{
730 acpi_status status = AE_OK;
731 u32 hci_result;
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400732 bool bt_present;
733 bool bt_on;
734 bool radio_on;
735 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
737 if (acpi_disabled)
738 return -ENODEV;
Luming Yufb9802f2005-03-18 18:03:45 -0500739
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 /* simple device detection: look for HCI method */
741 if (is_valid_acpi_path(METHOD_HCI_1))
742 method_hci = METHOD_HCI_1;
743 else if (is_valid_acpi_path(METHOD_HCI_2))
744 method_hci = METHOD_HCI_2;
745 else
746 return -ENODEV;
747
748 printk(MY_INFO "Toshiba Laptop ACPI Extras version %s\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400749 TOSHIBA_ACPI_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 printk(MY_INFO " HCI method: %s\n", method_hci);
751
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400752 mutex_init(&toshiba_acpi.mutex);
753
754 toshiba_acpi.p_dev = platform_device_register_simple("toshiba_acpi",
755 -1, NULL, 0);
756 if (IS_ERR(toshiba_acpi.p_dev)) {
757 ret = PTR_ERR(toshiba_acpi.p_dev);
758 printk(MY_ERR "unable to register platform device\n");
759 toshiba_acpi.p_dev = NULL;
760 toshiba_acpi_exit();
761 return ret;
762 }
763
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 force_fan = 0;
765 key_event_valid = 0;
766
767 /* enable event fifo */
768 hci_write1(HCI_SYSTEM_EVENT, 1, &hci_result);
769
770 toshiba_proc_dir = proc_mkdir(PROC_TOSHIBA, acpi_root_dir);
771 if (!toshiba_proc_dir) {
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400772 toshiba_acpi_exit();
773 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 } else {
775 toshiba_proc_dir->owner = THIS_MODULE;
776 status = add_device();
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400777 if (ACPI_FAILURE(status)) {
778 toshiba_acpi_exit();
779 return -ENODEV;
780 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 }
782
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400783 toshiba_backlight_device = backlight_device_register("toshiba",
784 &toshiba_acpi.p_dev->dev,
Yu Luming519ab5f2006-12-19 12:56:15 -0800785 NULL,
Holger Machtc9263552006-10-20 14:30:29 -0700786 &toshiba_backlight_data);
787 if (IS_ERR(toshiba_backlight_device)) {
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400788 ret = PTR_ERR(toshiba_backlight_device);
Andrey Borzenkov12993422007-11-14 16:58:28 -0800789
Holger Machtc9263552006-10-20 14:30:29 -0700790 printk(KERN_ERR "Could not register toshiba backlight device\n");
791 toshiba_backlight_device = NULL;
792 toshiba_acpi_exit();
Andrey Borzenkov12993422007-11-14 16:58:28 -0800793 return ret;
Holger Machtc9263552006-10-20 14:30:29 -0700794 }
Richard Purdie599a52d2007-02-10 23:07:48 +0000795 toshiba_backlight_device->props.max_brightness = HCI_LCD_BRIGHTNESS_LEVELS - 1;
Holger Machtc9263552006-10-20 14:30:29 -0700796
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400797 /* Register rfkill switch for Bluetooth */
798 if (hci_get_bt_present(&bt_present) == HCI_SUCCESS && bt_present) {
799 toshiba_acpi.rfk_dev = rfkill_allocate(&toshiba_acpi.p_dev->dev,
800 RFKILL_TYPE_BLUETOOTH);
801 if (!toshiba_acpi.rfk_dev) {
802 printk(MY_ERR "unable to allocate rfkill device\n");
803 toshiba_acpi_exit();
804 return -ENOMEM;
805 }
806
807 toshiba_acpi.rfk_dev->name = toshiba_acpi.bt_name;
808 toshiba_acpi.rfk_dev->toggle_radio = bt_rfkill_toggle_radio;
809 toshiba_acpi.rfk_dev->user_claim_unsupported = 1;
810 toshiba_acpi.rfk_dev->data = &toshiba_acpi;
811
812 if (hci_get_bt_on(&bt_on) == HCI_SUCCESS && bt_on) {
813 toshiba_acpi.rfk_dev->state = RFKILL_STATE_UNBLOCKED;
814 } else if (hci_get_radio_state(&radio_on) == HCI_SUCCESS &&
815 radio_on) {
816 toshiba_acpi.rfk_dev->state = RFKILL_STATE_SOFT_BLOCKED;
817 } else {
818 toshiba_acpi.rfk_dev->state = RFKILL_STATE_HARD_BLOCKED;
819 }
820
821 ret = rfkill_register(toshiba_acpi.rfk_dev);
822 if (ret) {
823 printk(MY_ERR "unable to register rfkill device\n");
824 toshiba_acpi_exit();
825 return -ENOMEM;
826 }
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400827
Frederik Deweerdt38aefbc2008-12-15 13:54:19 -0800828 /* Register input device for kill switch */
829 toshiba_acpi.poll_dev = input_allocate_polled_device();
830 if (!toshiba_acpi.poll_dev) {
831 printk(MY_ERR
832 "unable to allocate kill-switch input device\n");
833 toshiba_acpi_exit();
834 return -ENOMEM;
835 }
836 toshiba_acpi.poll_dev->private = &toshiba_acpi;
837 toshiba_acpi.poll_dev->poll = bt_poll_rfkill;
838 toshiba_acpi.poll_dev->poll_interval = 1000; /* msecs */
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400839
Frederik Deweerdt38aefbc2008-12-15 13:54:19 -0800840 toshiba_acpi.poll_dev->input->name = toshiba_acpi.rfk_name;
841 toshiba_acpi.poll_dev->input->id.bustype = BUS_HOST;
842 /* Toshiba USB ID */
843 toshiba_acpi.poll_dev->input->id.vendor = 0x0930;
844 set_bit(EV_SW, toshiba_acpi.poll_dev->input->evbit);
845 set_bit(SW_RFKILL_ALL, toshiba_acpi.poll_dev->input->swbit);
846 input_report_switch(toshiba_acpi.poll_dev->input,
847 SW_RFKILL_ALL, TRUE);
848 input_sync(toshiba_acpi.poll_dev->input);
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400849
Frederik Deweerdt38aefbc2008-12-15 13:54:19 -0800850 ret = input_register_polled_device(toshiba_acpi.poll_dev);
851 if (ret) {
852 printk(MY_ERR
853 "unable to register kill-switch input device\n");
854 toshiba_acpi_exit();
855 return ret;
856 }
philipl@overt.orgc41a40c2008-08-30 11:57:39 -0400857 }
858
859 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860}
861
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862module_init(toshiba_acpi_init);
863module_exit(toshiba_acpi_exit);