Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Holger Macht | c926355 | 2006-10-20 14:30:29 -0700 | [diff] [blame] | 44 | #include <linux/backlight.h> |
| 45 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | #include <asm/uaccess.h> |
| 47 | |
| 48 | #include <acpi/acpi_drivers.h> |
| 49 | |
| 50 | MODULE_AUTHOR("John Belmonte"); |
| 51 | MODULE_DESCRIPTION("Toshiba Laptop ACPI Extras Driver"); |
| 52 | MODULE_LICENSE("GPL"); |
| 53 | |
| 54 | #define MY_LOGPREFIX "toshiba_acpi: " |
| 55 | #define MY_ERR KERN_ERR MY_LOGPREFIX |
| 56 | #define MY_NOTICE KERN_NOTICE MY_LOGPREFIX |
| 57 | #define MY_INFO KERN_INFO MY_LOGPREFIX |
| 58 | |
| 59 | /* Toshiba ACPI method paths */ |
| 60 | #define METHOD_LCD_BRIGHTNESS "\\_SB_.PCI0.VGA_.LCD_._BCM" |
| 61 | #define METHOD_HCI_1 "\\_SB_.VALD.GHCI" |
| 62 | #define METHOD_HCI_2 "\\_SB_.VALZ.GHCI" |
| 63 | #define METHOD_VIDEO_OUT "\\_SB_.VALX.DSSX" |
| 64 | |
| 65 | /* Toshiba HCI interface definitions |
| 66 | * |
| 67 | * HCI is Toshiba's "Hardware Control Interface" which is supposed to |
| 68 | * be uniform across all their models. Ideally we would just call |
| 69 | * dedicated ACPI methods instead of using this primitive interface. |
| 70 | * However the ACPI methods seem to be incomplete in some areas (for |
| 71 | * example they allow setting, but not reading, the LCD brightness value), |
| 72 | * so this is still useful. |
| 73 | */ |
| 74 | |
| 75 | #define HCI_WORDS 6 |
| 76 | |
| 77 | /* operations */ |
| 78 | #define HCI_SET 0xff00 |
| 79 | #define HCI_GET 0xfe00 |
| 80 | |
| 81 | /* return codes */ |
| 82 | #define HCI_SUCCESS 0x0000 |
| 83 | #define HCI_FAILURE 0x1000 |
| 84 | #define HCI_NOT_SUPPORTED 0x8000 |
| 85 | #define HCI_EMPTY 0x8c00 |
| 86 | |
| 87 | /* registers */ |
| 88 | #define HCI_FAN 0x0004 |
| 89 | #define HCI_SYSTEM_EVENT 0x0016 |
| 90 | #define HCI_VIDEO_OUT 0x001c |
| 91 | #define HCI_HOTKEY_EVENT 0x001e |
| 92 | #define HCI_LCD_BRIGHTNESS 0x002a |
| 93 | |
| 94 | /* field definitions */ |
| 95 | #define HCI_LCD_BRIGHTNESS_BITS 3 |
| 96 | #define HCI_LCD_BRIGHTNESS_SHIFT (16-HCI_LCD_BRIGHTNESS_BITS) |
| 97 | #define HCI_LCD_BRIGHTNESS_LEVELS (1 << HCI_LCD_BRIGHTNESS_BITS) |
| 98 | #define HCI_VIDEO_OUT_LCD 0x1 |
| 99 | #define HCI_VIDEO_OUT_CRT 0x2 |
| 100 | #define HCI_VIDEO_OUT_TV 0x4 |
| 101 | |
arvidjaar@mail.ru | 4db42c5 | 2008-03-04 15:06:34 -0800 | [diff] [blame] | 102 | static const struct acpi_device_id toshiba_device_ids[] = { |
| 103 | {"TOS6200", 0}, |
| 104 | {"TOS1900", 0}, |
| 105 | {"", 0}, |
| 106 | }; |
| 107 | MODULE_DEVICE_TABLE(acpi, toshiba_device_ids); |
| 108 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | /* utility |
| 110 | */ |
| 111 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 112 | static __inline__ void _set_bit(u32 * word, u32 mask, int value) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | { |
| 114 | *word = (*word & ~mask) | (mask * value); |
| 115 | } |
| 116 | |
| 117 | /* acpi interface wrappers |
| 118 | */ |
| 119 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 120 | static int is_valid_acpi_path(const char *methodName) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | { |
| 122 | acpi_handle handle; |
| 123 | acpi_status status; |
| 124 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 125 | status = acpi_get_handle(NULL, (char *)methodName, &handle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | return !ACPI_FAILURE(status); |
| 127 | } |
| 128 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 129 | static int write_acpi_int(const char *methodName, int val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | { |
| 131 | struct acpi_object_list params; |
| 132 | union acpi_object in_objs[1]; |
| 133 | acpi_status status; |
| 134 | |
Ahmed S. Darwish | b2b7910 | 2007-02-06 16:14:43 -0800 | [diff] [blame] | 135 | params.count = ARRAY_SIZE(in_objs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | params.pointer = in_objs; |
| 137 | in_objs[0].type = ACPI_TYPE_INTEGER; |
| 138 | in_objs[0].integer.value = val; |
| 139 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 140 | status = acpi_evaluate_object(NULL, (char *)methodName, ¶ms, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | return (status == AE_OK); |
| 142 | } |
| 143 | |
| 144 | #if 0 |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 145 | static int read_acpi_int(const char *methodName, int *pVal) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | { |
| 147 | struct acpi_buffer results; |
| 148 | union acpi_object out_objs[1]; |
| 149 | acpi_status status; |
| 150 | |
| 151 | results.length = sizeof(out_objs); |
| 152 | results.pointer = out_objs; |
| 153 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 154 | status = acpi_evaluate_object(0, (char *)methodName, 0, &results); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | *pVal = out_objs[0].integer.value; |
| 156 | |
| 157 | return (status == AE_OK) && (out_objs[0].type == ACPI_TYPE_INTEGER); |
| 158 | } |
| 159 | #endif |
| 160 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 161 | static const char *method_hci /*= 0*/ ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | |
| 163 | /* Perform a raw HCI call. Here we don't care about input or output buffer |
| 164 | * format. |
| 165 | */ |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 166 | static acpi_status hci_raw(const u32 in[HCI_WORDS], u32 out[HCI_WORDS]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | { |
| 168 | struct acpi_object_list params; |
| 169 | union acpi_object in_objs[HCI_WORDS]; |
| 170 | struct acpi_buffer results; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 171 | union acpi_object out_objs[HCI_WORDS + 1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | acpi_status status; |
| 173 | int i; |
| 174 | |
| 175 | params.count = HCI_WORDS; |
| 176 | params.pointer = in_objs; |
| 177 | for (i = 0; i < HCI_WORDS; ++i) { |
| 178 | in_objs[i].type = ACPI_TYPE_INTEGER; |
| 179 | in_objs[i].integer.value = in[i]; |
| 180 | } |
| 181 | |
| 182 | results.length = sizeof(out_objs); |
| 183 | results.pointer = out_objs; |
| 184 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 185 | status = acpi_evaluate_object(NULL, (char *)method_hci, ¶ms, |
| 186 | &results); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | if ((status == AE_OK) && (out_objs->package.count <= HCI_WORDS)) { |
| 188 | for (i = 0; i < out_objs->package.count; ++i) { |
| 189 | out[i] = out_objs->package.elements[i].integer.value; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | return status; |
| 194 | } |
| 195 | |
| 196 | /* common hci tasks (get or set one value) |
| 197 | * |
| 198 | * In addition to the ACPI status, the HCI system returns a result which |
| 199 | * may be useful (such as "not supported"). |
| 200 | */ |
| 201 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 202 | static acpi_status hci_write1(u32 reg, u32 in1, u32 * result) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | { |
| 204 | u32 in[HCI_WORDS] = { HCI_SET, reg, in1, 0, 0, 0 }; |
| 205 | u32 out[HCI_WORDS]; |
| 206 | acpi_status status = hci_raw(in, out); |
| 207 | *result = (status == AE_OK) ? out[0] : HCI_FAILURE; |
| 208 | return status; |
| 209 | } |
| 210 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 211 | static acpi_status hci_read1(u32 reg, u32 * out1, u32 * result) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | { |
| 213 | u32 in[HCI_WORDS] = { HCI_GET, reg, 0, 0, 0, 0 }; |
| 214 | u32 out[HCI_WORDS]; |
| 215 | acpi_status status = hci_raw(in, out); |
| 216 | *out1 = out[2]; |
| 217 | *result = (status == AE_OK) ? out[0] : HCI_FAILURE; |
| 218 | return status; |
| 219 | } |
| 220 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 221 | static struct proc_dir_entry *toshiba_proc_dir /*= 0*/ ; |
Holger Macht | c926355 | 2006-10-20 14:30:29 -0700 | [diff] [blame] | 222 | static struct backlight_device *toshiba_backlight_device; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 223 | static int force_fan; |
| 224 | static int last_key_event; |
| 225 | static int key_event_valid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 227 | typedef struct _ProcItem { |
| 228 | const char *name; |
| 229 | char *(*read_func) (char *); |
| 230 | unsigned long (*write_func) (const char *, unsigned long); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | } ProcItem; |
| 232 | |
| 233 | /* proc file handlers |
| 234 | */ |
| 235 | |
| 236 | static int |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 237 | dispatch_read(char *page, char **start, off_t off, int count, int *eof, |
| 238 | ProcItem * item) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 240 | char *p = page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | int len; |
| 242 | |
| 243 | if (off == 0) |
| 244 | p = item->read_func(p); |
| 245 | |
| 246 | /* ISSUE: I don't understand this code */ |
| 247 | len = (p - page); |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 248 | if (len <= off + count) |
| 249 | *eof = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | *start = page + off; |
| 251 | len -= off; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 252 | if (len > count) |
| 253 | len = count; |
| 254 | if (len < 0) |
| 255 | len = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | return len; |
| 257 | } |
| 258 | |
| 259 | static int |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 260 | dispatch_write(struct file *file, const char __user * buffer, |
| 261 | unsigned long count, ProcItem * item) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | { |
| 263 | int result; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 264 | char *tmp_buffer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | |
| 266 | /* Arg buffer points to userspace memory, which can't be accessed |
| 267 | * directly. Since we're making a copy, zero-terminate the |
| 268 | * destination so that sscanf can be used on it safely. |
| 269 | */ |
| 270 | tmp_buffer = kmalloc(count + 1, GFP_KERNEL); |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 271 | if (!tmp_buffer) |
Panagiotis Issaris | f422415 | 2005-03-30 22:15:36 -0500 | [diff] [blame] | 272 | return -ENOMEM; |
| 273 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | if (copy_from_user(tmp_buffer, buffer, count)) { |
| 275 | result = -EFAULT; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 276 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | tmp_buffer[count] = 0; |
| 278 | result = item->write_func(tmp_buffer, count); |
| 279 | } |
| 280 | kfree(tmp_buffer); |
| 281 | return result; |
| 282 | } |
| 283 | |
Holger Macht | c926355 | 2006-10-20 14:30:29 -0700 | [diff] [blame] | 284 | static int get_lcd(struct backlight_device *bd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | { |
| 286 | u32 hci_result; |
| 287 | u32 value; |
| 288 | |
| 289 | hci_read1(HCI_LCD_BRIGHTNESS, &value, &hci_result); |
| 290 | if (hci_result == HCI_SUCCESS) { |
Holger Macht | c926355 | 2006-10-20 14:30:29 -0700 | [diff] [blame] | 291 | return (value >> HCI_LCD_BRIGHTNESS_SHIFT); |
| 292 | } else |
| 293 | return -EFAULT; |
| 294 | } |
| 295 | |
| 296 | static char *read_lcd(char *p) |
| 297 | { |
| 298 | int value = get_lcd(NULL); |
| 299 | |
| 300 | if (value >= 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | p += sprintf(p, "brightness: %d\n", value); |
| 302 | p += sprintf(p, "brightness_levels: %d\n", |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 303 | HCI_LCD_BRIGHTNESS_LEVELS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | } else { |
| 305 | printk(MY_ERR "Error reading LCD brightness\n"); |
| 306 | } |
| 307 | |
| 308 | return p; |
| 309 | } |
| 310 | |
Holger Macht | c926355 | 2006-10-20 14:30:29 -0700 | [diff] [blame] | 311 | static int set_lcd(int value) |
| 312 | { |
| 313 | u32 hci_result; |
| 314 | |
| 315 | value = value << HCI_LCD_BRIGHTNESS_SHIFT; |
| 316 | hci_write1(HCI_LCD_BRIGHTNESS, value, &hci_result); |
| 317 | if (hci_result != HCI_SUCCESS) |
| 318 | return -EFAULT; |
| 319 | |
| 320 | return 0; |
| 321 | } |
| 322 | |
| 323 | static int set_lcd_status(struct backlight_device *bd) |
| 324 | { |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame] | 325 | return set_lcd(bd->props.brightness); |
Holger Macht | c926355 | 2006-10-20 14:30:29 -0700 | [diff] [blame] | 326 | } |
| 327 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 328 | static unsigned long write_lcd(const char *buffer, unsigned long count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | { |
| 330 | int value; |
Matthijs van Otterdijk | c8af57e | 2007-01-05 16:37:03 -0800 | [diff] [blame] | 331 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | |
| 333 | if (sscanf(buffer, " brightness : %i", &value) == 1 && |
Matthijs van Otterdijk | c8af57e | 2007-01-05 16:37:03 -0800 | [diff] [blame] | 334 | value >= 0 && value < HCI_LCD_BRIGHTNESS_LEVELS) { |
Holger Macht | c926355 | 2006-10-20 14:30:29 -0700 | [diff] [blame] | 335 | ret = set_lcd(value); |
Matthijs van Otterdijk | c8af57e | 2007-01-05 16:37:03 -0800 | [diff] [blame] | 336 | if (ret == 0) |
| 337 | ret = count; |
| 338 | } else { |
Holger Macht | c926355 | 2006-10-20 14:30:29 -0700 | [diff] [blame] | 339 | ret = -EINVAL; |
Matthijs van Otterdijk | c8af57e | 2007-01-05 16:37:03 -0800 | [diff] [blame] | 340 | } |
Holger Macht | c926355 | 2006-10-20 14:30:29 -0700 | [diff] [blame] | 341 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | } |
| 343 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 344 | static char *read_video(char *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | { |
| 346 | u32 hci_result; |
| 347 | u32 value; |
| 348 | |
| 349 | hci_read1(HCI_VIDEO_OUT, &value, &hci_result); |
| 350 | if (hci_result == HCI_SUCCESS) { |
| 351 | int is_lcd = (value & HCI_VIDEO_OUT_LCD) ? 1 : 0; |
| 352 | int is_crt = (value & HCI_VIDEO_OUT_CRT) ? 1 : 0; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 353 | int is_tv = (value & HCI_VIDEO_OUT_TV) ? 1 : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | p += sprintf(p, "lcd_out: %d\n", is_lcd); |
| 355 | p += sprintf(p, "crt_out: %d\n", is_crt); |
| 356 | p += sprintf(p, "tv_out: %d\n", is_tv); |
| 357 | } else { |
| 358 | printk(MY_ERR "Error reading video out status\n"); |
| 359 | } |
| 360 | |
| 361 | return p; |
| 362 | } |
| 363 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 364 | static unsigned long write_video(const char *buffer, unsigned long count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | { |
| 366 | int value; |
| 367 | int remain = count; |
| 368 | int lcd_out = -1; |
| 369 | int crt_out = -1; |
| 370 | int tv_out = -1; |
| 371 | u32 hci_result; |
Al Viro | b4482a4 | 2007-10-14 19:35:40 +0100 | [diff] [blame] | 372 | u32 video_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | |
| 374 | /* scan expression. Multiple expressions may be delimited with ; |
| 375 | * |
| 376 | * NOTE: to keep scanning simple, invalid fields are ignored |
| 377 | */ |
| 378 | while (remain) { |
| 379 | if (sscanf(buffer, " lcd_out : %i", &value) == 1) |
| 380 | lcd_out = value & 1; |
| 381 | else if (sscanf(buffer, " crt_out : %i", &value) == 1) |
| 382 | crt_out = value & 1; |
| 383 | else if (sscanf(buffer, " tv_out : %i", &value) == 1) |
| 384 | tv_out = value & 1; |
| 385 | /* advance to one character past the next ; */ |
| 386 | do { |
| 387 | ++buffer; |
| 388 | --remain; |
| 389 | } |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 390 | while (remain && *(buffer - 1) != ';'); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | hci_read1(HCI_VIDEO_OUT, &video_out, &hci_result); |
| 394 | if (hci_result == HCI_SUCCESS) { |
Harvey Harrison | 9e113e0 | 2008-09-22 14:37:29 -0700 | [diff] [blame] | 395 | unsigned int new_video_out = video_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | if (lcd_out != -1) |
| 397 | _set_bit(&new_video_out, HCI_VIDEO_OUT_LCD, lcd_out); |
| 398 | if (crt_out != -1) |
| 399 | _set_bit(&new_video_out, HCI_VIDEO_OUT_CRT, crt_out); |
| 400 | if (tv_out != -1) |
| 401 | _set_bit(&new_video_out, HCI_VIDEO_OUT_TV, tv_out); |
| 402 | /* To avoid unnecessary video disruption, only write the new |
| 403 | * video setting if something changed. */ |
| 404 | if (new_video_out != video_out) |
| 405 | write_acpi_int(METHOD_VIDEO_OUT, new_video_out); |
| 406 | } else { |
| 407 | return -EFAULT; |
| 408 | } |
| 409 | |
| 410 | return count; |
| 411 | } |
| 412 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 413 | static char *read_fan(char *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | { |
| 415 | u32 hci_result; |
| 416 | u32 value; |
| 417 | |
| 418 | hci_read1(HCI_FAN, &value, &hci_result); |
| 419 | if (hci_result == HCI_SUCCESS) { |
| 420 | p += sprintf(p, "running: %d\n", (value > 0)); |
| 421 | p += sprintf(p, "force_on: %d\n", force_fan); |
| 422 | } else { |
| 423 | printk(MY_ERR "Error reading fan status\n"); |
| 424 | } |
| 425 | |
| 426 | return p; |
| 427 | } |
| 428 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 429 | static unsigned long write_fan(const char *buffer, unsigned long count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | { |
| 431 | int value; |
| 432 | u32 hci_result; |
| 433 | |
| 434 | if (sscanf(buffer, " force_on : %i", &value) == 1 && |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 435 | value >= 0 && value <= 1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | hci_write1(HCI_FAN, value, &hci_result); |
| 437 | if (hci_result != HCI_SUCCESS) |
| 438 | return -EFAULT; |
| 439 | else |
| 440 | force_fan = value; |
| 441 | } else { |
| 442 | return -EINVAL; |
| 443 | } |
| 444 | |
| 445 | return count; |
| 446 | } |
| 447 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 448 | static char *read_keys(char *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | { |
| 450 | u32 hci_result; |
| 451 | u32 value; |
| 452 | |
| 453 | if (!key_event_valid) { |
| 454 | hci_read1(HCI_SYSTEM_EVENT, &value, &hci_result); |
| 455 | if (hci_result == HCI_SUCCESS) { |
| 456 | key_event_valid = 1; |
| 457 | last_key_event = value; |
| 458 | } else if (hci_result == HCI_EMPTY) { |
| 459 | /* better luck next time */ |
| 460 | } else if (hci_result == HCI_NOT_SUPPORTED) { |
| 461 | /* This is a workaround for an unresolved issue on |
| 462 | * some machines where system events sporadically |
| 463 | * become disabled. */ |
| 464 | hci_write1(HCI_SYSTEM_EVENT, 1, &hci_result); |
| 465 | printk(MY_NOTICE "Re-enabled hotkeys\n"); |
| 466 | } else { |
| 467 | printk(MY_ERR "Error reading hotkey status\n"); |
| 468 | goto end; |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | p += sprintf(p, "hotkey_ready: %d\n", key_event_valid); |
| 473 | p += sprintf(p, "hotkey: 0x%04x\n", last_key_event); |
| 474 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 475 | end: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | return p; |
| 477 | } |
| 478 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 479 | static unsigned long write_keys(const char *buffer, unsigned long count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | { |
| 481 | int value; |
| 482 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 483 | if (sscanf(buffer, " hotkey_ready : %i", &value) == 1 && value == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | key_event_valid = 0; |
| 485 | } else { |
| 486 | return -EINVAL; |
| 487 | } |
| 488 | |
| 489 | return count; |
| 490 | } |
| 491 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 492 | static char *read_version(char *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | { |
| 494 | p += sprintf(p, "driver: %s\n", TOSHIBA_ACPI_VERSION); |
| 495 | p += sprintf(p, "proc_interface: %d\n", |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 496 | PROC_INTERFACE_VERSION); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | return p; |
| 498 | } |
| 499 | |
| 500 | /* proc and module init |
| 501 | */ |
| 502 | |
| 503 | #define PROC_TOSHIBA "toshiba" |
| 504 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 505 | static ProcItem proc_items[] = { |
| 506 | {"lcd", read_lcd, write_lcd}, |
| 507 | {"video", read_video, write_video}, |
| 508 | {"fan", read_fan, write_fan}, |
| 509 | {"keys", read_keys, write_keys}, |
| 510 | {"version", read_version, NULL}, |
| 511 | {NULL} |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | }; |
| 513 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 514 | static acpi_status __init add_device(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 516 | struct proc_dir_entry *proc; |
| 517 | ProcItem *item; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 519 | for (item = proc_items; item->name; ++item) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | proc = create_proc_read_entry(item->name, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 521 | S_IFREG | S_IRUGO | S_IWUSR, |
| 522 | toshiba_proc_dir, |
| 523 | (read_proc_t *) dispatch_read, |
| 524 | item); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | if (proc) |
| 526 | proc->owner = THIS_MODULE; |
| 527 | if (proc && item->write_func) |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 528 | proc->write_proc = (write_proc_t *) dispatch_write; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | return AE_OK; |
| 532 | } |
| 533 | |
Randy Dunlap | b1d93de | 2007-06-16 10:16:02 -0700 | [diff] [blame] | 534 | static acpi_status remove_device(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 536 | ProcItem *item; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | |
| 538 | for (item = proc_items; item->name; ++item) |
| 539 | remove_proc_entry(item->name, toshiba_proc_dir); |
| 540 | return AE_OK; |
| 541 | } |
| 542 | |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame] | 543 | static struct backlight_ops toshiba_backlight_data = { |
Holger Macht | c926355 | 2006-10-20 14:30:29 -0700 | [diff] [blame] | 544 | .get_brightness = get_lcd, |
| 545 | .update_status = set_lcd_status, |
Holger Macht | c926355 | 2006-10-20 14:30:29 -0700 | [diff] [blame] | 546 | }; |
| 547 | |
Sam Ravnborg | b2b77b2 | 2007-06-01 00:47:12 -0700 | [diff] [blame] | 548 | static void toshiba_acpi_exit(void) |
Holger Macht | c926355 | 2006-10-20 14:30:29 -0700 | [diff] [blame] | 549 | { |
| 550 | if (toshiba_backlight_device) |
| 551 | backlight_device_unregister(toshiba_backlight_device); |
| 552 | |
| 553 | remove_device(); |
| 554 | |
| 555 | if (toshiba_proc_dir) |
| 556 | remove_proc_entry(PROC_TOSHIBA, acpi_root_dir); |
| 557 | |
| 558 | return; |
| 559 | } |
| 560 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 561 | static int __init toshiba_acpi_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | { |
| 563 | acpi_status status = AE_OK; |
| 564 | u32 hci_result; |
| 565 | |
| 566 | if (acpi_disabled) |
| 567 | return -ENODEV; |
Luming Yu | fb9802f | 2005-03-18 18:03:45 -0500 | [diff] [blame] | 568 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | /* simple device detection: look for HCI method */ |
| 570 | if (is_valid_acpi_path(METHOD_HCI_1)) |
| 571 | method_hci = METHOD_HCI_1; |
| 572 | else if (is_valid_acpi_path(METHOD_HCI_2)) |
| 573 | method_hci = METHOD_HCI_2; |
| 574 | else |
| 575 | return -ENODEV; |
| 576 | |
| 577 | printk(MY_INFO "Toshiba Laptop ACPI Extras version %s\n", |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 578 | TOSHIBA_ACPI_VERSION); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | printk(MY_INFO " HCI method: %s\n", method_hci); |
| 580 | |
| 581 | force_fan = 0; |
| 582 | key_event_valid = 0; |
| 583 | |
| 584 | /* enable event fifo */ |
| 585 | hci_write1(HCI_SYSTEM_EVENT, 1, &hci_result); |
| 586 | |
| 587 | toshiba_proc_dir = proc_mkdir(PROC_TOSHIBA, acpi_root_dir); |
| 588 | if (!toshiba_proc_dir) { |
| 589 | status = AE_ERROR; |
| 590 | } else { |
| 591 | toshiba_proc_dir->owner = THIS_MODULE; |
| 592 | status = add_device(); |
| 593 | if (ACPI_FAILURE(status)) |
| 594 | remove_proc_entry(PROC_TOSHIBA, acpi_root_dir); |
| 595 | } |
| 596 | |
Yu Luming | 519ab5f | 2006-12-19 12:56:15 -0800 | [diff] [blame] | 597 | toshiba_backlight_device = backlight_device_register("toshiba",NULL, |
| 598 | NULL, |
Holger Macht | c926355 | 2006-10-20 14:30:29 -0700 | [diff] [blame] | 599 | &toshiba_backlight_data); |
| 600 | if (IS_ERR(toshiba_backlight_device)) { |
Andrey Borzenkov | 1299342 | 2007-11-14 16:58:28 -0800 | [diff] [blame] | 601 | int ret = PTR_ERR(toshiba_backlight_device); |
| 602 | |
Holger Macht | c926355 | 2006-10-20 14:30:29 -0700 | [diff] [blame] | 603 | printk(KERN_ERR "Could not register toshiba backlight device\n"); |
| 604 | toshiba_backlight_device = NULL; |
| 605 | toshiba_acpi_exit(); |
Andrey Borzenkov | 1299342 | 2007-11-14 16:58:28 -0800 | [diff] [blame] | 606 | return ret; |
Holger Macht | c926355 | 2006-10-20 14:30:29 -0700 | [diff] [blame] | 607 | } |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame] | 608 | toshiba_backlight_device->props.max_brightness = HCI_LCD_BRIGHTNESS_LEVELS - 1; |
Holger Macht | c926355 | 2006-10-20 14:30:29 -0700 | [diff] [blame] | 609 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | return (ACPI_SUCCESS(status)) ? 0 : -ENODEV; |
| 611 | } |
| 612 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | module_init(toshiba_acpi_init); |
| 614 | module_exit(toshiba_acpi_exit); |