blob: 1683bfe15b354cb8c221a8a238e3e4d0aead1c0a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -03002 * thinkpad_acpi.c - ThinkPad ACPI Extras
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 *
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
Henrique de Moraes Holschuha62bc912007-03-23 17:33:58 -03006 * Copyright (C) 2006-2007 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
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
Henrique de Moraes Holschuha62bc912007-03-23 17:33:58 -030020 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
Borislav Deianov78f81cc2005-08-17 00:00:00 -040022 */
23
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -030024#define IBM_VERSION "0.14"
Borislav Deianov78f81cc2005-08-17 00:00:00 -040025
26/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 * Changelog:
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -030028 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
29 * drivers/misc.
Henrique de Moraes Holschuhf9ff43a2006-11-25 16:37:38 -020030 *
31 * 2006-11-22 0.13 new maintainer
32 * changelog now lives in git commit history, and will
33 * not be updated further in-file.
Henrique de Moraes Holschuh837ca6d2007-03-23 17:33:54 -030034 *
Borislav Deianov78f81cc2005-08-17 00:00:00 -040035 * 2005-08-17 0.12 fix compilation on 2.6.13-rc kernels
36 * 2005-03-17 0.11 support for 600e, 770x
37 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
38 * support for 770e, G41
39 * G40 and G41 don't have a thinklight
40 * temperatures no longer experimental
41 * experimental brightness control
42 * experimental volume control
43 * experimental fan enable/disable
Henrique de Moraes Holschuh837ca6d2007-03-23 17:33:54 -030044 * 2005-01-16 0.10 fix module loading on R30, R31
Borislav Deianov78f81cc2005-08-17 00:00:00 -040045 * 2005-01-16 0.9 support for 570, R30, R31
46 * ultrabay support on A22p, A3x
47 * limit arg for cmos, led, beep, drop experimental status
48 * more capable led control on A21e, A22p, T20-22, X20
49 * experimental temperatures and fan speed
50 * experimental embedded controller register dump
51 * mark more functions as __init, drop incorrect __exit
52 * use MODULE_VERSION
53 * thanks to Henrik Brix Andersen <brix@gentoo.org>
54 * fix parameter passing on module loading
55 * thanks to Rusty Russell <rusty@rustcorp.com.au>
56 * thanks to Jim Radford <radford@blackbean.org>
57 * 2004-11-08 0.8 fix init error case, don't return from a macro
58 * thanks to Chris Wright <chrisw@osdl.org>
59 * 2004-10-23 0.7 fix module loading on A21e, A22p, T20, T21, X20
60 * fix led control on A21e
61 * 2004-10-19 0.6 use acpi_bus_register_driver() to claim HKEY device
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 * 2004-10-18 0.5 thinklight support on A21e, G40, R32, T20, T21, X20
63 * proc file format changed
64 * video_switch command
65 * experimental cmos control
66 * experimental led control
67 * experimental acpi sounds
Borislav Deianov78f81cc2005-08-17 00:00:00 -040068 * 2004-09-16 0.4 support for module parameters
69 * hotkey mask can be prefixed by 0x
70 * video output switching
71 * video expansion control
72 * ultrabay eject support
73 * removed lcd brightness/on/off control, didn't work
74 * 2004-08-17 0.3 support for R40
75 * lcd off, brightness control
76 * thinklight on/off
77 * 2004-08-14 0.2 support for T series, X20
78 * bluetooth enable/disable
79 * hotkey events disabled by default
80 * removed fan control, currently useless
81 * 2004-08-09 0.1 initial release, support for X series
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 */
83
Henrique de Moraes Holschuhf21f85d2007-03-29 01:58:40 -030084#include "thinkpad_acpi.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Henrique de Moraes Holschuhf9ff43a2006-11-25 16:37:38 -020086MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
Borislav Deianov78f81cc2005-08-17 00:00:00 -040087MODULE_DESCRIPTION(IBM_DESC);
88MODULE_VERSION(IBM_VERSION);
89MODULE_LICENSE("GPL");
90
Henrique de Moraes Holschuhd903ac52007-03-29 01:58:42 -030091/* Please remove this in year 2009 */
92MODULE_ALIAS("ibm_acpi");
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094#define __unused __attribute__ ((unused))
95
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -030096/****************************************************************************
97 ****************************************************************************
98 *
99 * ACPI Helpers and device model
100 *
101 ****************************************************************************
102 ****************************************************************************/
103
104/*************************************************************************
105 * ACPI basic handles
106 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108static acpi_handle root_handle = NULL;
109
110#define IBM_HANDLE(object, parent, paths...) \
111 static acpi_handle object##_handle; \
112 static acpi_handle *object##_parent = &parent##_handle; \
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400113 static char *object##_path; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 static char *object##_paths[] = { paths }
115
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400116IBM_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
117 "\\_SB.PCI.ISA.EC", /* 570 */
118 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
119 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
120 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
121 "\\_SB.PCI0.ICH3.EC0", /* R31 */
122 "\\_SB.PCI0.LPC.EC", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300123 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300125IBM_HANDLE(ecrd, ec, "ECRD"); /* 570 */
126IBM_HANDLE(ecwr, ec, "ECWR"); /* 570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300128
129/*************************************************************************
130 * Misc ACPI handles
131 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400133IBM_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, T4x, X31, X40 */
134 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
135 "\\CMS", /* R40, R40e */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300136 ); /* all others */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400137
138IBM_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
139 "^HKEY", /* R30, R31 */
140 "HKEY", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300141 ); /* 570 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400142
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400143
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300144/*************************************************************************
145 * ACPI helpers
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200146 */
147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148static int acpi_evalf(acpi_handle handle,
149 void *res, char *method, char *fmt, ...)
150{
151 char *fmt0 = fmt;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400152 struct acpi_object_list params;
153 union acpi_object in_objs[IBM_MAX_ACPI_ARGS];
154 struct acpi_buffer result, *resultp;
155 union acpi_object out_obj;
156 acpi_status status;
157 va_list ap;
158 char res_type;
159 int success;
160 int quiet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162 if (!*fmt) {
163 printk(IBM_ERR "acpi_evalf() called with empty format\n");
164 return 0;
165 }
166
167 if (*fmt == 'q') {
168 quiet = 1;
169 fmt++;
170 } else
171 quiet = 0;
172
173 res_type = *(fmt++);
174
175 params.count = 0;
176 params.pointer = &in_objs[0];
177
178 va_start(ap, fmt);
179 while (*fmt) {
180 char c = *(fmt++);
181 switch (c) {
182 case 'd': /* int */
183 in_objs[params.count].integer.value = va_arg(ap, int);
184 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
185 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400186 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 default:
188 printk(IBM_ERR "acpi_evalf() called "
189 "with invalid format character '%c'\n", c);
190 return 0;
191 }
192 }
193 va_end(ap);
194
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400195 if (res_type != 'v') {
196 result.length = sizeof(out_obj);
197 result.pointer = &out_obj;
198 resultp = &result;
199 } else
200 resultp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400202 status = acpi_evaluate_object(handle, method, &params, resultp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
204 switch (res_type) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400205 case 'd': /* int */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 if (res)
207 *(int *)res = out_obj.integer.value;
208 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
209 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400210 case 'v': /* void */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 success = status == AE_OK;
212 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400213 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 default:
215 printk(IBM_ERR "acpi_evalf() called "
216 "with invalid format character '%c'\n", res_type);
217 return 0;
218 }
219
220 if (!success && !quiet)
221 printk(IBM_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
222 method, fmt0, status);
223
224 return success;
225}
226
227static void __unused acpi_print_int(acpi_handle handle, char *method)
228{
229 int i;
230
231 if (acpi_evalf(handle, &i, method, "d"))
232 printk(IBM_INFO "%s = 0x%x\n", method, i);
233 else
234 printk(IBM_ERR "error calling %s\n", method);
235}
236
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300237static int acpi_ec_read(int i, u8 * p)
238{
239 int v;
240
241 if (ecrd_handle) {
242 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
243 return 0;
244 *p = v;
245 } else {
246 if (ec_read(i, p) < 0)
247 return 0;
248 }
249
250 return 1;
251}
252
253static int acpi_ec_write(int i, u8 v)
254{
255 if (ecwr_handle) {
256 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
257 return 0;
258 } else {
259 if (ec_write(i, v) < 0)
260 return 0;
261 }
262
263 return 1;
264}
265
266static int _sta(acpi_handle handle)
267{
268 int status;
269
270 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
271 status = 0;
272
273 return status;
274}
275
276/*************************************************************************
277 * ACPI device model
278 */
279
280static void __init ibm_handle_init(char *name,
281 acpi_handle * handle, acpi_handle parent,
282 char **paths, int num_paths, char **path)
283{
284 int i;
285 acpi_status status;
286
287 for (i = 0; i < num_paths; i++) {
288 status = acpi_get_handle(parent, paths[i], handle);
289 if (ACPI_SUCCESS(status)) {
290 *path = paths[i];
291 return;
292 }
293 }
294
295 *handle = NULL;
296}
297
298static void dispatch_notify(acpi_handle handle, u32 event, void *data)
299{
300 struct ibm_struct *ibm = data;
301
302 if (!ibm || !ibm->notify)
303 return;
304
305 ibm->notify(ibm, event);
306}
307
308static int __init setup_notify(struct ibm_struct *ibm)
309{
310 acpi_status status;
311 int ret;
312
313 if (!*ibm->handle)
314 return 0;
315
316 ret = acpi_bus_get_device(*ibm->handle, &ibm->device);
317 if (ret < 0) {
318 printk(IBM_ERR "%s device not present\n", ibm->name);
319 return -ENODEV;
320 }
321
322 acpi_driver_data(ibm->device) = ibm;
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300323 sprintf(acpi_device_class(ibm->device), "%s/%s",
324 IBM_ACPI_EVENT_PREFIX,
325 ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300326
327 status = acpi_install_notify_handler(*ibm->handle, ibm->type,
328 dispatch_notify, ibm);
329 if (ACPI_FAILURE(status)) {
330 if (status == AE_ALREADY_EXISTS) {
331 printk(IBM_NOTICE "another device driver is already handling %s events\n",
332 ibm->name);
333 } else {
334 printk(IBM_ERR "acpi_install_notify_handler(%s) failed: %d\n",
335 ibm->name, status);
336 }
337 return -ENODEV;
338 }
339 ibm->notify_installed = 1;
340 return 0;
341}
342
343static int __init ibm_device_add(struct acpi_device *device)
344{
345 return 0;
346}
347
Henrique de Moraes Holschuh67001212007-04-21 11:08:25 -0300348static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300349{
350 int ret;
351
352 ibm->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
353 if (!ibm->driver) {
354 printk(IBM_ERR "kmalloc(ibm->driver) failed\n");
355 return -1;
356 }
357
358 sprintf(ibm->driver->name, "%s_%s", IBM_NAME, ibm->name);
359 ibm->driver->ids = ibm->hid;
360 ibm->driver->ops.add = &ibm_device_add;
361
362 ret = acpi_bus_register_driver(ibm->driver);
363 if (ret < 0) {
364 printk(IBM_ERR "acpi_bus_register_driver(%s) failed: %d\n",
365 ibm->hid, ret);
366 kfree(ibm->driver);
367 }
368
369 return ret;
370}
371
372
373/****************************************************************************
374 ****************************************************************************
375 *
376 * Procfs Helpers
377 *
378 ****************************************************************************
379 ****************************************************************************/
380
381static int dispatch_read(char *page, char **start, off_t off, int count,
382 int *eof, void *data)
383{
384 struct ibm_struct *ibm = data;
385 int len;
386
387 if (!ibm || !ibm->read)
388 return -EINVAL;
389
390 len = ibm->read(page);
391 if (len < 0)
392 return len;
393
394 if (len <= off + count)
395 *eof = 1;
396 *start = page + off;
397 len -= off;
398 if (len > count)
399 len = count;
400 if (len < 0)
401 len = 0;
402
403 return len;
404}
405
406static int dispatch_write(struct file *file, const char __user * userbuf,
407 unsigned long count, void *data)
408{
409 struct ibm_struct *ibm = data;
410 char *kernbuf;
411 int ret;
412
413 if (!ibm || !ibm->write)
414 return -EINVAL;
415
416 kernbuf = kmalloc(count + 2, GFP_KERNEL);
417 if (!kernbuf)
418 return -ENOMEM;
419
420 if (copy_from_user(kernbuf, userbuf, count)) {
421 kfree(kernbuf);
422 return -EFAULT;
423 }
424
425 kernbuf[count] = 0;
426 strcat(kernbuf, ",");
427 ret = ibm->write(kernbuf);
428 if (ret == 0)
429 ret = count;
430
431 kfree(kernbuf);
432
433 return ret;
434}
435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436static char *next_cmd(char **cmds)
437{
438 char *start = *cmds;
439 char *end;
440
441 while ((end = strchr(start, ',')) && end == start)
442 start = end + 1;
443
444 if (!end)
445 return NULL;
446
447 *end = 0;
448 *cmds = end + 1;
449 return start;
450}
451
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300452
453/****************************************************************************
454 ****************************************************************************
455 *
456 * Subdrivers
457 *
458 ****************************************************************************
459 ****************************************************************************/
460
461/*************************************************************************
Henrique de Moraes Holschuh142cfc92007-04-21 11:08:26 -0300462 * thinkpad-acpi init subdriver
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300463 */
464
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300465static int thinkpad_acpi_driver_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
467 printk(IBM_INFO "%s v%s\n", IBM_DESC, IBM_VERSION);
468 printk(IBM_INFO "%s\n", IBM_URL);
469
Henrique de Moraes Holschuh3945ac32007-02-06 19:13:44 -0200470 if (ibm_thinkpad_ec_found)
471 printk(IBM_INFO "ThinkPad EC firmware %s\n",
472 ibm_thinkpad_ec_found);
473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 return 0;
475}
476
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300477static int thinkpad_acpi_driver_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
479 int len = 0;
480
481 len += sprintf(p + len, "driver:\t\t%s\n", IBM_DESC);
482 len += sprintf(p + len, "version:\t%s\n", IBM_VERSION);
483
484 return len;
485}
486
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300487/*************************************************************************
488 * Hotkey subdriver
489 */
490
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400491static int hotkey_supported;
492static int hotkey_mask_supported;
493static int hotkey_orig_status;
494static int hotkey_orig_mask;
495
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300496static int hotkey_init(void)
497{
498 /* hotkey not supported on 570 */
499 hotkey_supported = hkey_handle != NULL;
500
501 if (hotkey_supported) {
502 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
503 A30, R30, R31, T20-22, X20-21, X22-24 */
504 hotkey_mask_supported =
505 acpi_evalf(hkey_handle, NULL, "DHKN", "qv");
506
507 if (!hotkey_get(&hotkey_orig_status, &hotkey_orig_mask))
508 return -ENODEV;
509 }
510
511 return 0;
512}
513
514static void hotkey_exit(void)
515{
516 if (hotkey_supported)
517 hotkey_set(hotkey_orig_status, hotkey_orig_mask);
518}
519
520static void hotkey_notify(struct ibm_struct *ibm, u32 event)
521{
522 int hkey;
523
524 if (acpi_evalf(hkey_handle, &hkey, "MHKP", "d"))
525 acpi_bus_generate_event(ibm->device, event, hkey);
526 else {
527 printk(IBM_ERR "unknown hotkey event %d\n", event);
528 acpi_bus_generate_event(ibm->device, event, 0);
529 }
530}
531
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400532static int hotkey_get(int *status, int *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533{
534 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400535 return 0;
536
537 if (hotkey_mask_supported)
538 if (!acpi_evalf(hkey_handle, mask, "DHKN", "d"))
539 return 0;
540
541 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542}
543
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400544static int hotkey_set(int status, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
546 int i;
547
548 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 return 0;
550
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400551 if (hotkey_mask_supported)
552 for (i = 0; i < 32; i++) {
553 int bit = ((1 << i) & mask) != 0;
554 if (!acpi_evalf(hkey_handle,
555 NULL, "MHKM", "vdd", i + 1, bit))
556 return 0;
557 }
558
559 return 1;
560}
561
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400562static int hotkey_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
564 int status, mask;
565 int len = 0;
566
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400567 if (!hotkey_supported) {
568 len += sprintf(p + len, "status:\t\tnot supported\n");
569 return len;
570 }
571
572 if (!hotkey_get(&status, &mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 return -EIO;
574
575 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400576 if (hotkey_mask_supported) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 len += sprintf(p + len, "mask:\t\t0x%04x\n", mask);
578 len += sprintf(p + len,
579 "commands:\tenable, disable, reset, <mask>\n");
580 } else {
581 len += sprintf(p + len, "mask:\t\tnot supported\n");
582 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
583 }
584
585 return len;
586}
587
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400588static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
590 int status, mask;
591 char *cmd;
592 int do_cmd = 0;
593
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400594 if (!hotkey_supported)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 return -ENODEV;
596
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400597 if (!hotkey_get(&status, &mask))
598 return -EIO;
599
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 while ((cmd = next_cmd(&buf))) {
601 if (strlencmp(cmd, "enable") == 0) {
602 status = 1;
603 } else if (strlencmp(cmd, "disable") == 0) {
604 status = 0;
605 } else if (strlencmp(cmd, "reset") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400606 status = hotkey_orig_status;
607 mask = hotkey_orig_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
609 /* mask set */
610 } else if (sscanf(cmd, "%x", &mask) == 1) {
611 /* mask set */
612 } else
613 return -EINVAL;
614 do_cmd = 1;
615 }
616
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400617 if (do_cmd && !hotkey_set(status, mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 return -EIO;
619
620 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400621}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300623/*************************************************************************
624 * Bluetooth subdriver
625 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400627static int bluetooth_supported;
628
629static int bluetooth_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400631 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
632 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
633 bluetooth_supported = hkey_handle &&
634 acpi_evalf(hkey_handle, NULL, "GBDC", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
636 return 0;
637}
638
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400639static int bluetooth_status(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
641 int status;
642
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400643 if (!bluetooth_supported ||
644 !acpi_evalf(hkey_handle, &status, "GBDC", "d"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 status = 0;
646
647 return status;
648}
649
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400650static int bluetooth_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651{
652 int len = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400653 int status = bluetooth_status();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400655 if (!bluetooth_supported)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 len += sprintf(p + len, "status:\t\tnot supported\n");
657 else if (!(status & 1))
658 len += sprintf(p + len, "status:\t\tnot installed\n");
659 else {
660 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 1));
661 len += sprintf(p + len, "commands:\tenable, disable\n");
662 }
663
664 return len;
665}
666
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400667static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400669 int status = bluetooth_status();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 char *cmd;
671 int do_cmd = 0;
672
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400673 if (!bluetooth_supported)
674 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
676 while ((cmd = next_cmd(&buf))) {
677 if (strlencmp(cmd, "enable") == 0) {
678 status |= 2;
679 } else if (strlencmp(cmd, "disable") == 0) {
680 status &= ~2;
681 } else
682 return -EINVAL;
683 do_cmd = 1;
684 }
685
686 if (do_cmd && !acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400687 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
689 return 0;
690}
691
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300692/*************************************************************************
693 * Wan subdriver
694 */
695
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -0400696static int wan_supported;
697
698static int wan_init(void)
699{
700 wan_supported = hkey_handle &&
701 acpi_evalf(hkey_handle, NULL, "GWAN", "qv");
702
703 return 0;
704}
705
706static int wan_status(void)
707{
708 int status;
709
Henrique de Moraes Holschuh8d297262006-11-24 11:47:07 -0200710 if (!wan_supported || !acpi_evalf(hkey_handle, &status, "GWAN", "d"))
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -0400711 status = 0;
712
713 return status;
714}
715
716static int wan_read(char *p)
717{
718 int len = 0;
719 int status = wan_status();
720
721 if (!wan_supported)
722 len += sprintf(p + len, "status:\t\tnot supported\n");
723 else if (!(status & 1))
724 len += sprintf(p + len, "status:\t\tnot installed\n");
725 else {
726 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 1));
727 len += sprintf(p + len, "commands:\tenable, disable\n");
728 }
729
730 return len;
731}
732
733static int wan_write(char *buf)
734{
735 int status = wan_status();
736 char *cmd;
737 int do_cmd = 0;
738
739 if (!wan_supported)
740 return -ENODEV;
741
742 while ((cmd = next_cmd(&buf))) {
743 if (strlencmp(cmd, "enable") == 0) {
744 status |= 2;
745 } else if (strlencmp(cmd, "disable") == 0) {
746 status &= ~2;
747 } else
748 return -EINVAL;
749 do_cmd = 1;
750 }
751
752 if (do_cmd && !acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
753 return -EIO;
754
755 return 0;
756}
757
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300758/*************************************************************************
759 * Video subdriver
760 */
761
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200762static enum video_access_mode video_supported;
763static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400764
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300765IBM_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
766 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
767 "\\_SB.PCI0.VID0", /* 770e */
768 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
769 "\\_SB.PCI0.AGP.VID", /* all others */
770 ); /* R30, R31 */
771
772IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
773
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400774static int video_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400776 int ivga;
777
778 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
779 /* G41, assume IVGA doesn't change */
780 vid_handle = vid2_handle;
781
782 if (!vid_handle)
783 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300784 video_supported = TPACPI_VIDEO_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400785 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
786 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300787 video_supported = TPACPI_VIDEO_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400788 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
789 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300790 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400791 else
792 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300793 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
795 return 0;
796}
797
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300798static void video_exit(void)
799{
800 acpi_evalf(vid_handle, NULL, "_DOS", "vd", video_orig_autosw);
801}
802
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400803static int video_status(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804{
805 int status = 0;
806 int i;
807
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300808 if (video_supported == TPACPI_VIDEO_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400809 if (acpi_evalf(NULL, &i, "\\_SB.PHS", "dd", 0x87))
810 status = i & 3;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300811 } else if (video_supported == TPACPI_VIDEO_770) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400812 if (acpi_evalf(NULL, &i, "\\VCDL", "d"))
813 status |= 0x01 * i;
814 if (acpi_evalf(NULL, &i, "\\VCDC", "d"))
815 status |= 0x02 * i;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300816 } else if (video_supported == TPACPI_VIDEO_NEW) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400817 acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1);
818 if (acpi_evalf(NULL, &i, "\\VCDC", "d"))
819 status |= 0x02 * i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400821 acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0);
822 if (acpi_evalf(NULL, &i, "\\VCDL", "d"))
823 status |= 0x01 * i;
824 if (acpi_evalf(NULL, &i, "\\VCDD", "d"))
825 status |= 0x08 * i;
826 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
828 return status;
829}
830
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400831static int video_autosw(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400833 int autosw = 0;
834
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300835 if (video_supported == TPACPI_VIDEO_570)
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400836 acpi_evalf(vid_handle, &autosw, "SWIT", "d");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300837 else if (video_supported == TPACPI_VIDEO_770 ||
838 video_supported == TPACPI_VIDEO_NEW)
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400839 acpi_evalf(vid_handle, &autosw, "^VDEE", "d");
840
841 return autosw & 1;
842}
843
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400844static int video_switch(void)
845{
846 int autosw = video_autosw();
847 int ret;
848
849 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
850 return -EIO;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300851 ret = video_supported == TPACPI_VIDEO_570 ?
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400852 acpi_evalf(ec_handle, NULL, "_Q16", "v") :
853 acpi_evalf(vid_handle, NULL, "VSWT", "v");
854 acpi_evalf(vid_handle, NULL, "_DOS", "vd", autosw);
855
856 return ret;
857}
858
859static int video_expand(void)
860{
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300861 if (video_supported == TPACPI_VIDEO_570)
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400862 return acpi_evalf(ec_handle, NULL, "_Q17", "v");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300863 else if (video_supported == TPACPI_VIDEO_770)
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400864 return acpi_evalf(vid_handle, NULL, "VEXP", "v");
865 else
866 return acpi_evalf(NULL, NULL, "\\VEXP", "v");
867}
868
869static int video_switch2(int status)
870{
871 int ret;
872
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300873 if (video_supported == TPACPI_VIDEO_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400874 ret = acpi_evalf(NULL, NULL,
875 "\\_SB.PHS2", "vdd", 0x8b, status | 0x80);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300876 } else if (video_supported == TPACPI_VIDEO_770) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400877 int autosw = video_autosw();
878 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
879 return -EIO;
880
881 ret = acpi_evalf(vid_handle, NULL,
882 "ASWT", "vdd", status * 0x100, 0);
883
884 acpi_evalf(vid_handle, NULL, "_DOS", "vd", autosw);
885 } else {
886 ret = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
887 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
888 }
889
890 return ret;
891}
892
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300893static int video_read(char *p)
894{
895 int status = video_status();
896 int autosw = video_autosw();
897 int len = 0;
898
899 if (!video_supported) {
900 len += sprintf(p + len, "status:\t\tnot supported\n");
901 return len;
902 }
903
904 len += sprintf(p + len, "status:\t\tsupported\n");
905 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
906 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300907 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300908 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
909 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
910 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
911 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300912 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300913 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
914 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
915 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
916
917 return len;
918}
919
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400920static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921{
922 char *cmd;
923 int enable, disable, status;
924
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400925 if (!video_supported)
926 return -ENODEV;
927
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 enable = disable = 0;
929
930 while ((cmd = next_cmd(&buf))) {
931 if (strlencmp(cmd, "lcd_enable") == 0) {
932 enable |= 0x01;
933 } else if (strlencmp(cmd, "lcd_disable") == 0) {
934 disable |= 0x01;
935 } else if (strlencmp(cmd, "crt_enable") == 0) {
936 enable |= 0x02;
937 } else if (strlencmp(cmd, "crt_disable") == 0) {
938 disable |= 0x02;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300939 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400940 strlencmp(cmd, "dvi_enable") == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 enable |= 0x08;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300942 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400943 strlencmp(cmd, "dvi_disable") == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 disable |= 0x08;
945 } else if (strlencmp(cmd, "auto_enable") == 0) {
946 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
947 return -EIO;
948 } else if (strlencmp(cmd, "auto_disable") == 0) {
949 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 0))
950 return -EIO;
951 } else if (strlencmp(cmd, "video_switch") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400952 if (!video_switch())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 return -EIO;
954 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400955 if (!video_expand())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 return -EIO;
957 } else
958 return -EINVAL;
959 }
960
961 if (enable || disable) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400962 status = (video_status() & 0x0f & ~disable) | enable;
963 if (!video_switch2(status))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 return -EIO;
965 }
966
967 return 0;
968}
969
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300970/*************************************************************************
971 * Light (thinklight) subdriver
972 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400974static int light_supported;
975static int light_status_supported;
976
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300977IBM_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
978IBM_HANDLE(ledb, ec, "LEDB"); /* G4x */
979
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400980static int light_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400982 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
983 light_supported = (cmos_handle || lght_handle) && !ledb_handle;
984
985 if (light_supported)
986 /* light status not supported on
987 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
988 light_status_supported = acpi_evalf(ec_handle, NULL,
989 "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
991 return 0;
992}
993
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400994static int light_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995{
996 int len = 0;
997 int status = 0;
998
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400999 if (!light_supported) {
1000 len += sprintf(p + len, "status:\t\tnot supported\n");
1001 } else if (!light_status_supported) {
1002 len += sprintf(p + len, "status:\t\tunknown\n");
1003 len += sprintf(p + len, "commands:\ton, off\n");
1004 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
1006 return -EIO;
1007 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001008 len += sprintf(p + len, "commands:\ton, off\n");
1009 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
1011 return len;
1012}
1013
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001014static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015{
1016 int cmos_cmd, lght_cmd;
1017 char *cmd;
1018 int success;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001019
1020 if (!light_supported)
1021 return -ENODEV;
1022
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 while ((cmd = next_cmd(&buf))) {
1024 if (strlencmp(cmd, "on") == 0) {
1025 cmos_cmd = 0x0c;
1026 lght_cmd = 1;
1027 } else if (strlencmp(cmd, "off") == 0) {
1028 cmos_cmd = 0x0d;
1029 lght_cmd = 0;
1030 } else
1031 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001032
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 success = cmos_handle ?
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001034 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
1035 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 if (!success)
1037 return -EIO;
1038 }
1039
1040 return 0;
1041}
1042
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001043/*************************************************************************
1044 * Dock subdriver
1045 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001047/* don't list other alternatives as we install a notify handler on the 570 */
1048IBM_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
Henrique de Moraes Holschuhd2fadbb2007-01-11 02:58:15 -05001049
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03001050#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001051
1052IBM_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
1053 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
1054 "\\_SB.PCI0.PCI1.DOCK", /* all others */
1055 "\\_SB.PCI.ISA.SLCE", /* 570 */
1056 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
1057
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058#define dock_docked() (_sta(dock_handle) & 1)
1059
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001060static void dock_notify(struct ibm_struct *ibm, u32 event)
1061{
1062 int docked = dock_docked();
1063 int pci = ibm->hid && strstr(ibm->hid, IBM_PCI_HID);
1064
1065 if (event == 1 && !pci) /* 570 */
1066 acpi_bus_generate_event(ibm->device, event, 1); /* button */
1067 else if (event == 1 && pci) /* 570 */
1068 acpi_bus_generate_event(ibm->device, event, 3); /* dock */
1069 else if (event == 3 && docked)
1070 acpi_bus_generate_event(ibm->device, event, 1); /* button */
1071 else if (event == 3 && !docked)
1072 acpi_bus_generate_event(ibm->device, event, 2); /* undock */
1073 else if (event == 0 && docked)
1074 acpi_bus_generate_event(ibm->device, event, 3); /* dock */
1075 else {
1076 printk(IBM_ERR "unknown dock event %d, status %d\n",
1077 event, _sta(dock_handle));
1078 acpi_bus_generate_event(ibm->device, event, 0); /* unknown */
1079 }
1080}
1081
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001082static int dock_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083{
1084 int len = 0;
1085 int docked = dock_docked();
1086
1087 if (!dock_handle)
1088 len += sprintf(p + len, "status:\t\tnot supported\n");
1089 else if (!docked)
1090 len += sprintf(p + len, "status:\t\tundocked\n");
1091 else {
1092 len += sprintf(p + len, "status:\t\tdocked\n");
1093 len += sprintf(p + len, "commands:\tdock, undock\n");
1094 }
1095
1096 return len;
1097}
1098
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001099static int dock_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100{
1101 char *cmd;
1102
1103 if (!dock_docked())
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001104 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
1106 while ((cmd = next_cmd(&buf))) {
1107 if (strlencmp(cmd, "undock") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001108 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
1109 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 return -EIO;
1111 } else if (strlencmp(cmd, "dock") == 0) {
1112 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
1113 return -EIO;
1114 } else
1115 return -EINVAL;
1116 }
1117
1118 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001119}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03001121#endif /* CONFIG_THINKPAD_ACPI_DOCK */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001123/*************************************************************************
1124 * Bay subdriver
1125 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03001127#ifdef CONFIG_THINKPAD_ACPI_BAY
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001128static int bay_status_supported;
1129static int bay_status2_supported;
1130static int bay_eject_supported;
1131static int bay_eject2_supported;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001133IBM_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
1134 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
1135 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
1136 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
1137 ); /* A21e, R30, R31 */
1138IBM_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
1139 "_EJ0", /* all others */
1140 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
1141IBM_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
1142 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
1143 ); /* all others */
1144IBM_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
1145 "_EJ0", /* 770x */
1146 ); /* all others */
1147
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001148static int bay_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001150 bay_status_supported = bay_handle &&
1151 acpi_evalf(bay_handle, NULL, "_STA", "qv");
1152 bay_status2_supported = bay2_handle &&
1153 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
1154
1155 bay_eject_supported = bay_handle && bay_ej_handle &&
1156 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
1157 bay_eject2_supported = bay2_handle && bay2_ej_handle &&
1158 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
1160 return 0;
1161}
1162
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001163static void bay_notify(struct ibm_struct *ibm, u32 event)
1164{
1165 acpi_bus_generate_event(ibm->device, event, 0);
1166}
1167
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001168#define bay_occupied(b) (_sta(b##_handle) & 1)
1169
1170static int bay_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171{
1172 int len = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001173 int occupied = bay_occupied(bay);
1174 int occupied2 = bay_occupied(bay2);
1175 int eject, eject2;
1176
1177 len += sprintf(p + len, "status:\t\t%s\n", bay_status_supported ?
1178 (occupied ? "occupied" : "unoccupied") :
1179 "not supported");
1180 if (bay_status2_supported)
1181 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
1182 "occupied" : "unoccupied");
1183
1184 eject = bay_eject_supported && occupied;
1185 eject2 = bay_eject2_supported && occupied2;
1186
1187 if (eject && eject2)
1188 len += sprintf(p + len, "commands:\teject, eject2\n");
1189 else if (eject)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 len += sprintf(p + len, "commands:\teject\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001191 else if (eject2)
1192 len += sprintf(p + len, "commands:\teject2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
1194 return len;
1195}
1196
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001197static int bay_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198{
1199 char *cmd;
1200
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001201 if (!bay_eject_supported && !bay_eject2_supported)
1202 return -ENODEV;
1203
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001205 if (bay_eject_supported && strlencmp(cmd, "eject") == 0) {
1206 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
1207 return -EIO;
1208 } else if (bay_eject2_supported &&
1209 strlencmp(cmd, "eject2") == 0) {
1210 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 return -EIO;
1212 } else
1213 return -EINVAL;
1214 }
1215
1216 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001217}
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03001218#endif /* CONFIG_THINKPAD_ACPI_BAY */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001220/*************************************************************************
1221 * CMOS subdriver
1222 */
1223
1224static int cmos_eval(int cmos_cmd)
1225{
1226 if (cmos_handle)
1227 return acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd);
1228 else
1229 return 1;
1230}
1231
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001232static int cmos_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233{
1234 int len = 0;
1235
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001236 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1237 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 if (!cmos_handle)
1239 len += sprintf(p + len, "status:\t\tnot supported\n");
1240 else {
1241 len += sprintf(p + len, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001242 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 }
1244
1245 return len;
1246}
1247
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001248static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249{
1250 char *cmd;
1251 int cmos_cmd;
1252
1253 if (!cmos_handle)
1254 return -EINVAL;
1255
1256 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001257 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
1258 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 /* cmos_cmd set */
1260 } else
1261 return -EINVAL;
1262
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001263 if (!cmos_eval(cmos_cmd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 return -EIO;
1265 }
1266
1267 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001268}
1269
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001270
1271/*************************************************************************
1272 * LED subdriver
1273 */
1274
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02001275static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001276
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001277IBM_HANDLE(led, ec, "SLED", /* 570 */
1278 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
1279 "LED", /* all others */
1280 ); /* R30, R31 */
1281
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001282static int led_init(void)
1283{
1284 if (!led_handle)
1285 /* led not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001286 led_supported = TPACPI_LED_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001287 else if (strlencmp(led_path, "SLED") == 0)
1288 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001289 led_supported = TPACPI_LED_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001290 else if (strlencmp(led_path, "SYSL") == 0)
1291 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001292 led_supported = TPACPI_LED_OLD;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001293 else
1294 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001295 led_supported = TPACPI_LED_NEW;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001296
1297 return 0;
1298}
1299
1300#define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
1301
1302static int led_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303{
1304 int len = 0;
1305
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001306 if (!led_supported) {
1307 len += sprintf(p + len, "status:\t\tnot supported\n");
1308 return len;
1309 }
1310 len += sprintf(p + len, "status:\t\tsupported\n");
1311
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001312 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001313 /* 570 */
1314 int i, status;
1315 for (i = 0; i < 8; i++) {
1316 if (!acpi_evalf(ec_handle,
1317 &status, "GLED", "dd", 1 << i))
1318 return -EIO;
1319 len += sprintf(p + len, "%d:\t\t%s\n",
1320 i, led_status(status));
1321 }
1322 }
1323
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 len += sprintf(p + len, "commands:\t"
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001325 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
1327 return len;
1328}
1329
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001330/* off, on, blink */
1331static const int led_sled_arg1[] = { 0, 1, 3 };
1332static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
1333static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
1334static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
1335
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001336static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337{
1338 char *cmd;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001339 int led, ind, ret;
1340
1341 if (!led_supported)
1342 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
1344 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001345 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 return -EINVAL;
1347
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001348 if (strstr(cmd, "off")) {
1349 ind = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 } else if (strstr(cmd, "on")) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001351 ind = 1;
1352 } else if (strstr(cmd, "blink")) {
1353 ind = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 } else
1355 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001356
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001357 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001358 /* 570 */
1359 led = 1 << led;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001361 led, led_sled_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 return -EIO;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001363 } else if (led_supported == TPACPI_LED_OLD) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001364 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
1365 led = 1 << led;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001366 ret = ec_write(TPACPI_LED_EC_HLMS, led);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001367 if (ret >= 0)
1368 ret =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001369 ec_write(TPACPI_LED_EC_HLBL,
Henrique de Moraes Holschuhe062e032007-03-23 17:33:55 -03001370 led * led_exp_hlbl[ind]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001371 if (ret >= 0)
1372 ret =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001373 ec_write(TPACPI_LED_EC_HLCL,
Henrique de Moraes Holschuhe062e032007-03-23 17:33:55 -03001374 led * led_exp_hlcl[ind]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001375 if (ret < 0)
1376 return ret;
1377 } else {
1378 /* all others */
1379 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
1380 led, led_led_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001382 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 }
1384
1385 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001386}
1387
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001388/*************************************************************************
1389 * Beep subdriver
1390 */
1391
1392IBM_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
1393
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001394static int beep_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395{
1396 int len = 0;
1397
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001398 if (!beep_handle)
1399 len += sprintf(p + len, "status:\t\tnot supported\n");
1400 else {
1401 len += sprintf(p + len, "status:\t\tsupported\n");
1402 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
1403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
1405 return len;
1406}
1407
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001408static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409{
1410 char *cmd;
1411 int beep_cmd;
1412
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001413 if (!beep_handle)
1414 return -ENODEV;
1415
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001417 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
1418 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 /* beep_cmd set */
1420 } else
1421 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001422 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 return -EIO;
1424 }
1425
1426 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001427}
1428
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001429/*************************************************************************
1430 * Thermal subdriver
1431 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001432
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001433static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001434
1435static int thermal_init(void)
1436{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001437 u8 t, ta1, ta2;
1438 int i;
1439 int acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001440
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001441 if (ibm_thinkpad_ec_found && experimental) {
1442 /*
1443 * Direct EC access mode: sensors at registers
1444 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
1445 * non-implemented, thermal sensors return 0x80 when
1446 * not available
1447 */
1448
1449 ta1 = ta2 = 0;
1450 for (i = 0; i < 8; i++) {
1451 if (likely(acpi_ec_read(0x78 + i, &t))) {
1452 ta1 |= t;
1453 } else {
1454 ta1 = 0;
1455 break;
1456 }
1457 if (likely(acpi_ec_read(0xC0 + i, &t))) {
1458 ta2 |= t;
1459 } else {
1460 ta1 = 0;
1461 break;
1462 }
1463 }
1464 if (ta1 == 0) {
1465 /* This is sheer paranoia, but we handle it anyway */
1466 if (acpi_tmp7) {
1467 printk(IBM_ERR
1468 "ThinkPad ACPI EC access misbehaving, "
1469 "falling back to ACPI TMPx access mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001470 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001471 } else {
1472 printk(IBM_ERR
1473 "ThinkPad ACPI EC access misbehaving, "
1474 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001475 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001476 }
1477 } else {
1478 thermal_read_mode =
1479 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001480 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001481 }
1482 } else if (acpi_tmp7) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001483 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
1484 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001485 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001486 } else {
1487 /* Standard ACPI TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001488 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001489 }
1490 } else {
1491 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001492 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001493 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001494
1495 return 0;
1496}
1497
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001498static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
1499{
1500 int i, t;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001501 s8 tmp;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001502 char tmpi[] = "TMPi";
1503
1504 if (!s)
1505 return -EINVAL;
1506
1507 switch (thermal_read_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001508#if TPACPI_MAX_THERMAL_SENSORS >= 16
1509 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001510 for (i = 0; i < 8; i++) {
1511 if (!acpi_ec_read(0xC0 + i, &tmp))
1512 return -EIO;
1513 s->temp[i + 8] = tmp * 1000;
1514 }
1515 /* fallthrough */
1516#endif
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001517 case TPACPI_THERMAL_TPEC_8:
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001518 for (i = 0; i < 8; i++) {
1519 if (!acpi_ec_read(0x78 + i, &tmp))
1520 return -EIO;
1521 s->temp[i] = tmp * 1000;
1522 }
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001523 return (thermal_read_mode == TPACPI_THERMAL_TPEC_16) ? 16 : 8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001524
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001525 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001526 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
1527 return -EIO;
1528 for (i = 0; i < 8; i++) {
1529 tmpi[3] = '0' + i;
1530 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
1531 return -EIO;
1532 s->temp[i] = (t - 2732) * 100;
1533 }
1534 return 8;
1535
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001536 case TPACPI_THERMAL_ACPI_TMP07:
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001537 for (i = 0; i < 8; i++) {
1538 tmpi[3] = '0' + i;
1539 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
1540 return -EIO;
1541 s->temp[i] = t * 1000;
1542 }
1543 return 8;
1544
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001545 case TPACPI_THERMAL_NONE:
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001546 default:
1547 return 0;
1548 }
1549}
1550
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001551static int thermal_read(char *p)
1552{
1553 int len = 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001554 int n, i;
1555 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001556
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001557 n = thermal_get_sensors(&t);
1558 if (unlikely(n < 0))
1559 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001560
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001561 len += sprintf(p + len, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001562
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001563 if (n > 0) {
1564 for (i = 0; i < (n - 1); i++)
1565 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
1566 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
1567 } else
1568 len += sprintf(p + len, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001569
1570 return len;
1571}
1572
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001573/*************************************************************************
1574 * EC Dump subdriver
1575 */
1576
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001577static u8 ecdump_regs[256];
1578
1579static int ecdump_read(char *p)
1580{
1581 int len = 0;
1582 int i, j;
1583 u8 v;
1584
1585 len += sprintf(p + len, "EC "
1586 " +00 +01 +02 +03 +04 +05 +06 +07"
1587 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
1588 for (i = 0; i < 256; i += 16) {
1589 len += sprintf(p + len, "EC 0x%02x:", i);
1590 for (j = 0; j < 16; j++) {
1591 if (!acpi_ec_read(i + j, &v))
1592 break;
1593 if (v != ecdump_regs[i + j])
1594 len += sprintf(p + len, " *%02x", v);
1595 else
1596 len += sprintf(p + len, " %02x", v);
1597 ecdump_regs[i + j] = v;
1598 }
1599 len += sprintf(p + len, "\n");
1600 if (j != 16)
1601 break;
1602 }
1603
1604 /* These are way too dangerous to advertise openly... */
1605#if 0
1606 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
1607 " (<offset> is 00-ff, <value> is 00-ff)\n");
1608 len += sprintf(p + len, "commands:\t0x<offset> <value> "
1609 " (<offset> is 00-ff, <value> is 0-255)\n");
1610#endif
1611 return len;
1612}
1613
1614static int ecdump_write(char *buf)
1615{
1616 char *cmd;
1617 int i, v;
1618
1619 while ((cmd = next_cmd(&buf))) {
1620 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
1621 /* i and v set */
1622 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
1623 /* i and v set */
1624 } else
1625 return -EINVAL;
1626 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
1627 if (!acpi_ec_write(i, v))
1628 return -EIO;
1629 } else
1630 return -EINVAL;
1631 }
1632
1633 return 0;
1634}
1635
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001636/*************************************************************************
1637 * Backlight/brightness subdriver
1638 */
Holger Macht8acb0252006-10-20 14:30:28 -07001639
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001640static struct backlight_device *ibm_backlight_device = NULL;
Holger Macht8acb0252006-10-20 14:30:28 -07001641
Richard Purdie599a52d2007-02-10 23:07:48 +00001642static struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02001643 .get_brightness = brightness_get,
1644 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02001645};
1646
1647static int brightness_init(void)
1648{
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02001649 int b;
1650
1651 b = brightness_get(NULL);
1652 if (b < 0)
1653 return b;
1654
Yu Luming519ab5f2006-12-19 12:56:15 -08001655 ibm_backlight_device = backlight_device_register("ibm", NULL, NULL,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02001656 &ibm_backlight_data);
1657 if (IS_ERR(ibm_backlight_device)) {
1658 printk(IBM_ERR "Could not register backlight device\n");
1659 return PTR_ERR(ibm_backlight_device);
1660 }
1661
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02001662 ibm_backlight_device->props.max_brightness = 7;
1663 ibm_backlight_device->props.brightness = b;
1664 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00001665
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02001666 return 0;
1667}
1668
1669static void brightness_exit(void)
1670{
1671 if (ibm_backlight_device) {
1672 backlight_device_unregister(ibm_backlight_device);
1673 ibm_backlight_device = NULL;
1674 }
1675}
1676
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001677static int brightness_update_status(struct backlight_device *bd)
1678{
1679 return brightness_set(
1680 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
1681 bd->props.power == FB_BLANK_UNBLANK) ?
1682 bd->props.brightness : 0);
1683}
1684
1685static int brightness_get(struct backlight_device *bd)
1686{
1687 u8 level;
1688 if (!acpi_ec_read(brightness_offset, &level))
1689 return -EIO;
1690
1691 level &= 0x7;
1692
1693 return level;
1694}
1695
1696static int brightness_set(int value)
1697{
1698 int cmos_cmd, inc, i;
1699 int current_value = brightness_get(NULL);
1700
1701 value &= 7;
1702
1703 cmos_cmd = value > current_value ? TP_CMOS_BRIGHTNESS_UP : TP_CMOS_BRIGHTNESS_DOWN;
1704 inc = value > current_value ? 1 : -1;
1705 for (i = current_value; i != value; i += inc) {
1706 if (!cmos_eval(cmos_cmd))
1707 return -EIO;
1708 if (!acpi_ec_write(brightness_offset, i + inc))
1709 return -EIO;
1710 }
1711
1712 return 0;
1713}
1714
1715static int brightness_read(char *p)
1716{
1717 int len = 0;
1718 int level;
1719
1720 if ((level = brightness_get(NULL)) < 0) {
1721 len += sprintf(p + len, "level:\t\tunreadable\n");
1722 } else {
1723 len += sprintf(p + len, "level:\t\t%d\n", level & 0x7);
1724 len += sprintf(p + len, "commands:\tup, down\n");
1725 len += sprintf(p + len, "commands:\tlevel <level>"
1726 " (<level> is 0-7)\n");
1727 }
1728
1729 return len;
1730}
1731
1732static int brightness_write(char *buf)
1733{
1734 int level;
1735 int new_level;
1736 char *cmd;
1737
1738 while ((cmd = next_cmd(&buf))) {
1739 if ((level = brightness_get(NULL)) < 0)
1740 return level;
1741 level &= 7;
1742
1743 if (strlencmp(cmd, "up") == 0) {
1744 new_level = level == 7 ? 7 : level + 1;
1745 } else if (strlencmp(cmd, "down") == 0) {
1746 new_level = level == 0 ? 0 : level - 1;
1747 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
1748 new_level >= 0 && new_level <= 7) {
1749 /* new_level set */
1750 } else
1751 return -EINVAL;
1752
1753 brightness_set(new_level);
1754 }
1755
1756 return 0;
1757}
1758
1759/*************************************************************************
1760 * Volume subdriver
1761 */
1762
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001763static int volume_read(char *p)
1764{
1765 int len = 0;
1766 u8 level;
1767
1768 if (!acpi_ec_read(volume_offset, &level)) {
1769 len += sprintf(p + len, "level:\t\tunreadable\n");
1770 } else {
1771 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
1772 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
1773 len += sprintf(p + len, "commands:\tup, down, mute\n");
1774 len += sprintf(p + len, "commands:\tlevel <level>"
1775 " (<level> is 0-15)\n");
1776 }
1777
1778 return len;
1779}
1780
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001781static int volume_write(char *buf)
1782{
1783 int cmos_cmd, inc, i;
1784 u8 level, mute;
1785 int new_level, new_mute;
1786 char *cmd;
1787
1788 while ((cmd = next_cmd(&buf))) {
1789 if (!acpi_ec_read(volume_offset, &level))
1790 return -EIO;
1791 new_mute = mute = level & 0x40;
1792 new_level = level = level & 0xf;
1793
1794 if (strlencmp(cmd, "up") == 0) {
1795 if (mute)
1796 new_mute = 0;
1797 else
1798 new_level = level == 15 ? 15 : level + 1;
1799 } else if (strlencmp(cmd, "down") == 0) {
1800 if (mute)
1801 new_mute = 0;
1802 else
1803 new_level = level == 0 ? 0 : level - 1;
1804 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
1805 new_level >= 0 && new_level <= 15) {
1806 /* new_level set */
1807 } else if (strlencmp(cmd, "mute") == 0) {
1808 new_mute = 0x40;
1809 } else
1810 return -EINVAL;
1811
1812 if (new_level != level) { /* mute doesn't change */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001813 cmos_cmd = new_level > level ? TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001814 inc = new_level > level ? 1 : -1;
1815
1816 if (mute && (!cmos_eval(cmos_cmd) ||
1817 !acpi_ec_write(volume_offset, level)))
1818 return -EIO;
1819
1820 for (i = level; i != new_level; i += inc)
1821 if (!cmos_eval(cmos_cmd) ||
1822 !acpi_ec_write(volume_offset, i + inc))
1823 return -EIO;
1824
Henrique de Moraes Holschuhe062e032007-03-23 17:33:55 -03001825 if (mute && (!cmos_eval(TP_CMOS_VOLUME_MUTE) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001826 !acpi_ec_write(volume_offset,
1827 new_level + mute)))
1828 return -EIO;
1829 }
1830
1831 if (new_mute != mute) { /* level doesn't change */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001832 cmos_cmd = new_mute ? TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001833
1834 if (!cmos_eval(cmos_cmd) ||
1835 !acpi_ec_write(volume_offset, level + new_mute))
1836 return -EIO;
1837 }
1838 }
1839
1840 return 0;
1841}
1842
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001843
1844/*************************************************************************
1845 * Fan subdriver
1846 */
1847
1848/*
1849 * FAN ACCESS MODES
1850 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001851 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001852 * ACPI GFAN method: returns fan level
1853 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001854 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001855 * EC 0x2f not available if GFAN exists
1856 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001857 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001858 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
1859 *
1860 * EC 0x2f might be available *for reading*, but never for writing.
1861 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001862 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001863 * ThinkPad EC register 0x2f (HFSP): fan control loop mode Supported
1864 * on almost all ThinkPads
1865 *
1866 * Fan speed changes of any sort (including those caused by the
1867 * disengaged mode) are usually done slowly by the firmware as the
1868 * maximum ammount of fan duty cycle change per second seems to be
1869 * limited.
1870 *
1871 * Reading is not available if GFAN exists.
1872 * Writing is not available if SFAN exists.
1873 *
1874 * Bits
1875 * 7 automatic mode engaged;
1876 * (default operation mode of the ThinkPad)
1877 * fan level is ignored in this mode.
1878 * 6 disengage mode (takes precedence over bit 7);
1879 * not available on all thinkpads. May disable
1880 * the tachometer, and speeds up fan to 100% duty-cycle,
1881 * which speeds it up far above the standard RPM
1882 * levels. It is not impossible that it could cause
1883 * hardware damage.
1884 * 5-3 unused in some models. Extra bits for fan level
1885 * in others, but still useless as all values above
1886 * 7 map to the same speed as level 7 in these models.
1887 * 2-0 fan level (0..7 usually)
1888 * 0x00 = stop
1889 * 0x07 = max (set when temperatures critical)
1890 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001891 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001892 *
1893 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
1894 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
1895 * does so, its initial value is meaningless (0x07).
1896 *
1897 * For firmware bugs, refer to:
1898 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
1899 *
1900 * ----
1901 *
1902 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
1903 * Main fan tachometer reading (in RPM)
1904 *
1905 * This register is present on all ThinkPads with a new-style EC, and
1906 * it is known not to be present on the A21m/e, and T22, as there is
1907 * something else in offset 0x84 according to the ACPI DSDT. Other
1908 * ThinkPads from this same time period (and earlier) probably lack the
1909 * tachometer as well.
1910 *
1911 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
1912 * was never fixed by IBM to report the EC firmware version string
1913 * probably support the tachometer (like the early X models), so
1914 * detecting it is quite hard. We need more data to know for sure.
1915 *
1916 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
1917 * might result.
1918 *
1919 * FIRMWARE BUG: when EC 0x2f bit 6 is set (disengaged mode), this
1920 * register is not invalidated in ThinkPads that disable tachometer
1921 * readings. Thus, the tachometer readings go stale.
1922 *
1923 * For firmware bugs, refer to:
1924 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
1925 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001926 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001927 * ThinkPad X31, X40, X41. Not available in the X60.
1928 *
1929 * FANS ACPI handle: takes three arguments: low speed, medium speed,
1930 * high speed. ACPI DSDT seems to map these three speeds to levels
1931 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
1932 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
1933 *
1934 * The speeds are stored on handles
1935 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
1936 *
1937 * There are three default speed sets, acessible as handles:
1938 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
1939 *
1940 * ACPI DSDT switches which set is in use depending on various
1941 * factors.
1942 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001943 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001944 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
1945 * but the ACPI tables just mention level 7.
1946 */
1947
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001948static enum fan_status_access_mode fan_status_access_mode;
1949static enum fan_control_access_mode fan_control_access_mode;
1950static enum fan_control_commands fan_control_commands;
1951
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02001952static int fan_control_status_known;
1953static u8 fan_control_initial_status;
1954
Len Brown25c68a32006-12-08 04:43:41 -05001955static void fan_watchdog_fire(struct work_struct *ignored);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02001956static int fan_watchdog_maxinterval;
Len Brown25c68a32006-12-08 04:43:41 -05001957static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02001958
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001959IBM_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
1960IBM_HANDLE(gfan, ec, "GFAN", /* 570 */
1961 "\\FSPD", /* 600e/x, 770e, 770x */
1962 ); /* all others */
1963IBM_HANDLE(sfan, ec, "SFAN", /* 570 */
1964 "JFNS", /* 770x-JL */
1965 ); /* all others */
1966
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001967static int fan_init(void)
1968{
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001969 fan_status_access_mode = TPACPI_FAN_NONE;
1970 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001971 fan_control_commands = 0;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02001972 fan_control_status_known = 1;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02001973 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001974
1975 if (gfan_handle) {
1976 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001977 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001978 } else {
1979 /* all other ThinkPads: note that even old-style
1980 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02001981 if (likely(acpi_ec_read(fan_status_offset,
1982 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001983 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02001984
1985 /* In some ThinkPads, neither the EC nor the ACPI
1986 * DSDT initialize the fan status, and it ends up
1987 * being set to 0x07 when it *could* be either
1988 * 0x07 or 0x80.
1989 *
1990 * Enable for TP-1Y (T43), TP-78 (R51e),
1991 * TP-76 (R52), TP-70 (T43, R52), which are known
1992 * to be buggy. */
1993 if (fan_control_initial_status == 0x07 &&
1994 ibm_thinkpad_ec_found &&
1995 ((ibm_thinkpad_ec_found[0] == '1' &&
1996 ibm_thinkpad_ec_found[1] == 'Y') ||
1997 (ibm_thinkpad_ec_found[0] == '7' &&
1998 (ibm_thinkpad_ec_found[1] == '6' ||
1999 ibm_thinkpad_ec_found[1] == '8' ||
2000 ibm_thinkpad_ec_found[1] == '0'))
2001 )) {
2002 printk(IBM_NOTICE
2003 "fan_init: initial fan status is "
2004 "unknown, assuming it is in auto "
2005 "mode\n");
2006 fan_control_status_known = 0;
2007 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002008 } else {
2009 printk(IBM_ERR
2010 "ThinkPad ACPI EC access misbehaving, "
2011 "fan status and control unavailable\n");
2012 return 0;
2013 }
2014 }
2015
2016 if (sfan_handle) {
2017 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002018 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002019 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002020 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002021 } else {
2022 if (!gfan_handle) {
2023 /* gfan without sfan means no fan control */
2024 /* all other models implement TP EC 0x2f control */
2025
2026 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02002027 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002028 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002029 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002030 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002031 TPACPI_FAN_CMD_SPEED |
2032 TPACPI_FAN_CMD_LEVEL |
2033 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002034 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002035 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02002036 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002037 TPACPI_FAN_CMD_LEVEL |
2038 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002039 }
2040 }
2041 }
2042
2043 return 0;
2044}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002045
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02002046static int fan_get_status(u8 *status)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002047{
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02002048 u8 s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002049
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02002050 /* TODO:
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002051 * Add TPACPI_FAN_RD_ACPI_FANS ? */
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02002052
Henrique de Moraes Holschuh3ef8a602006-11-24 11:47:10 -02002053 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002054 case TPACPI_FAN_RD_ACPI_GFAN:
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002055 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02002056
2057 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002058 return -EIO;
2059
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02002060 if (likely(status))
2061 *status = s & 0x07;
2062
2063 break;
2064
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002065 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02002066 /* all except 570, 600e/x, 770e, 770x */
2067 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
2068 return -EIO;
2069
2070 if (likely(status))
2071 *status = s;
2072
2073 break;
2074
2075 default:
2076 return -ENXIO;
2077 }
2078
2079 return 0;
2080}
2081
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002082static void fan_exit(void)
2083{
2084 cancel_delayed_work(&fan_watchdog_task);
2085 flush_scheduled_work();
2086}
2087
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02002088static int fan_get_speed(unsigned int *speed)
2089{
2090 u8 hi, lo;
2091
2092 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002093 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02002094 /* all except 570, 600e/x, 770e, 770x */
2095 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
2096 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
2097 return -EIO;
2098
2099 if (likely(speed))
2100 *speed = (hi << 8) | lo;
2101
2102 break;
2103
2104 default:
2105 return -ENXIO;
2106 }
2107
2108 return 0;
2109}
2110
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002111static void fan_watchdog_fire(struct work_struct *ignored)
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002112{
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002113 printk(IBM_NOTICE "fan watchdog: enabling fan\n");
2114 if (fan_set_enable()) {
2115 printk(IBM_ERR "fan watchdog: error while enabling fan\n");
2116 /* reschedule for later */
2117 fan_watchdog_reset();
2118 }
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002119}
2120
2121static void fan_watchdog_reset(void)
2122{
2123 static int fan_watchdog_active = 0;
2124
2125 if (fan_watchdog_active)
2126 cancel_delayed_work(&fan_watchdog_task);
2127
2128 if (fan_watchdog_maxinterval > 0) {
2129 fan_watchdog_active = 1;
2130 if (!schedule_delayed_work(&fan_watchdog_task,
2131 msecs_to_jiffies(fan_watchdog_maxinterval
2132 * 1000))) {
2133 printk(IBM_ERR "failed to schedule the fan watchdog, "
2134 "watchdog will not trigger\n");
2135 }
2136 } else
2137 fan_watchdog_active = 0;
2138}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002139
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002140static int fan_set_level(int level)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002141{
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002142 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002143 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002144 if (level >= 0 && level <= 7) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002145 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
2146 return -EIO;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002147 } else
2148 return -EINVAL;
2149 break;
2150
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002151 case TPACPI_FAN_WR_ACPI_FANS:
2152 case TPACPI_FAN_WR_TPEC:
2153 if ((level != TP_EC_FAN_AUTO) &&
2154 (level != TP_EC_FAN_FULLSPEED) &&
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02002155 ((level < 0) || (level > 7)))
2156 return -EINVAL;
2157
2158 if (!acpi_ec_write(fan_status_offset, level))
2159 return -EIO;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002160 else
2161 fan_control_status_known = 1;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02002162 break;
2163
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002164 default:
2165 return -ENXIO;
2166 }
2167 return 0;
2168}
2169
2170static int fan_set_enable(void)
2171{
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002172 u8 s;
2173 int rc;
2174
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002175 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002176 case TPACPI_FAN_WR_ACPI_FANS:
2177 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002178 if ((rc = fan_get_status(&s)) < 0)
2179 return rc;
2180
2181 /* Don't go out of emergency fan mode */
2182 if (s != 7)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002183 s = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002184
2185 if (!acpi_ec_write(fan_status_offset, s))
2186 return -EIO;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002187 else
2188 fan_control_status_known = 1;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002189 break;
2190
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002191 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002192 if ((rc = fan_get_status(&s)) < 0)
2193 return rc;
2194
2195 s &= 0x07;
2196
2197 /* Set fan to at least level 4 */
2198 if (s < 4)
2199 s = 4;
2200
2201 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002202 return -EIO;
2203 break;
2204
2205 default:
2206 return -ENXIO;
2207 }
2208 return 0;
2209}
2210
2211static int fan_set_disable(void)
2212{
2213 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002214 case TPACPI_FAN_WR_ACPI_FANS:
2215 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002216 if (!acpi_ec_write(fan_status_offset, 0x00))
2217 return -EIO;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002218 else
2219 fan_control_status_known = 1;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002220 break;
2221
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002222 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002223 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
2224 return -EIO;
2225 break;
2226
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002227 default:
2228 return -ENXIO;
2229 }
2230 return 0;
2231}
2232
2233static int fan_set_speed(int speed)
2234{
2235 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002236 case TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002237 if (speed >= 0 && speed <= 65535) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002238 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
2239 speed, speed, speed))
2240 return -EIO;
2241 } else
2242 return -EINVAL;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002243 break;
2244
2245 default:
2246 return -ENXIO;
2247 }
2248 return 0;
2249}
2250
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002251static int fan_read(char *p)
2252{
2253 int len = 0;
2254 int rc;
2255 u8 status;
2256 unsigned int speed = 0;
2257
2258 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002259 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002260 /* 570, 600e/x, 770e, 770x */
2261 if ((rc = fan_get_status(&status)) < 0)
2262 return rc;
2263
2264 len += sprintf(p + len, "status:\t\t%s\n"
2265 "level:\t\t%d\n",
2266 (status != 0) ? "enabled" : "disabled", status);
2267 break;
2268
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002269 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002270 /* all except 570, 600e/x, 770e, 770x */
2271 if ((rc = fan_get_status(&status)) < 0)
2272 return rc;
2273
2274 if (unlikely(!fan_control_status_known)) {
2275 if (status != fan_control_initial_status)
2276 fan_control_status_known = 1;
2277 else
2278 /* Return most likely status. In fact, it
2279 * might be the only possible status */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002280 status = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002281 }
2282
2283 len += sprintf(p + len, "status:\t\t%s\n",
2284 (status != 0) ? "enabled" : "disabled");
2285
2286 /* No ThinkPad boots on disengaged mode, we can safely
2287 * assume the tachometer is online if fan control status
2288 * was unknown */
2289 if ((rc = fan_get_speed(&speed)) < 0)
2290 return rc;
2291
2292 len += sprintf(p + len, "speed:\t\t%d\n", speed);
2293
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002294 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002295 /* Disengaged mode takes precedence */
2296 len += sprintf(p + len, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002297 else if (status & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002298 len += sprintf(p + len, "level:\t\tauto\n");
2299 else
2300 len += sprintf(p + len, "level:\t\t%d\n", status);
2301 break;
2302
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002303 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002304 default:
2305 len += sprintf(p + len, "status:\t\tnot supported\n");
2306 }
2307
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002308 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002309 len += sprintf(p + len, "commands:\tlevel <level>");
2310
2311 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002312 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002313 len += sprintf(p + len, " (<level> is 0-7)\n");
2314 break;
2315
2316 default:
2317 len += sprintf(p + len, " (<level> is 0-7, "
2318 "auto, disengaged)\n");
2319 break;
2320 }
2321 }
2322
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002323 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002324 len += sprintf(p + len, "commands:\tenable, disable\n"
2325 "commands:\twatchdog <timeout> (<timeout> is 0 (off), "
2326 "1-120 (seconds))\n");
2327
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002328 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002329 len += sprintf(p + len, "commands:\tspeed <speed>"
2330 " (<speed> is 0-65535)\n");
2331
2332 return len;
2333}
2334
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002335static int fan_write_cmd_level(const char *cmd, int *rc)
2336{
2337 int level;
2338
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02002339 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002340 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02002341 else if (strlencmp(cmd, "level disengaged") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002342 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02002343 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002344 return 0;
2345
2346 if ((*rc = fan_set_level(level)) == -ENXIO)
2347 printk(IBM_ERR "level command accepted for unsupported "
2348 "access mode %d", fan_control_access_mode);
2349
2350 return 1;
2351}
2352
2353static int fan_write_cmd_enable(const char *cmd, int *rc)
2354{
2355 if (strlencmp(cmd, "enable") != 0)
2356 return 0;
2357
2358 if ((*rc = fan_set_enable()) == -ENXIO)
2359 printk(IBM_ERR "enable command accepted for unsupported "
2360 "access mode %d", fan_control_access_mode);
2361
2362 return 1;
2363}
2364
2365static int fan_write_cmd_disable(const char *cmd, int *rc)
2366{
2367 if (strlencmp(cmd, "disable") != 0)
2368 return 0;
2369
2370 if ((*rc = fan_set_disable()) == -ENXIO)
2371 printk(IBM_ERR "disable command accepted for unsupported "
2372 "access mode %d", fan_control_access_mode);
2373
2374 return 1;
2375}
2376
2377static int fan_write_cmd_speed(const char *cmd, int *rc)
2378{
2379 int speed;
2380
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02002381 /* TODO:
2382 * Support speed <low> <medium> <high> ? */
2383
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002384 if (sscanf(cmd, "speed %d", &speed) != 1)
2385 return 0;
2386
2387 if ((*rc = fan_set_speed(speed)) == -ENXIO)
2388 printk(IBM_ERR "speed command accepted for unsupported "
2389 "access mode %d", fan_control_access_mode);
2390
2391 return 1;
2392}
2393
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002394static int fan_write_cmd_watchdog(const char *cmd, int *rc)
2395{
2396 int interval;
2397
2398 if (sscanf(cmd, "watchdog %d", &interval) != 1)
2399 return 0;
2400
2401 if (interval < 0 || interval > 120)
2402 *rc = -EINVAL;
2403 else
2404 fan_watchdog_maxinterval = interval;
2405
2406 return 1;
2407}
2408
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002409static int fan_write(char *buf)
2410{
2411 char *cmd;
2412 int rc = 0;
2413
2414 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002415 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002416 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002417 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002418 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002419 fan_write_cmd_disable(cmd, &rc) ||
2420 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002421 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002422 fan_write_cmd_speed(cmd, &rc))
2423 )
2424 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002425 else if (!rc)
2426 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002427 }
2428
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002429 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002430}
2431
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002432/****************************************************************************
2433 ****************************************************************************
2434 *
2435 * Infrastructure
2436 *
2437 ****************************************************************************
2438 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002440/* /proc support */
2441static struct proc_dir_entry *proc_dir = NULL;
2442
2443/* Subdriver registry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444static struct ibm_struct ibms[] = {
2445 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002446 .name = "driver",
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -03002447 .init = thinkpad_acpi_driver_init,
2448 .read = thinkpad_acpi_driver_read,
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002449 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002451 .name = "hotkey",
2452 .hid = IBM_HKEY_HID,
2453 .init = hotkey_init,
2454 .read = hotkey_read,
2455 .write = hotkey_write,
2456 .exit = hotkey_exit,
2457 .notify = hotkey_notify,
2458 .handle = &hkey_handle,
2459 .type = ACPI_DEVICE_NOTIFY,
2460 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002462 .name = "bluetooth",
2463 .init = bluetooth_init,
2464 .read = bluetooth_read,
2465 .write = bluetooth_write,
2466 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 {
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002468 .name = "wan",
2469 .init = wan_init,
2470 .read = wan_read,
2471 .write = wan_write,
2472 .experimental = 1,
2473 },
2474 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002475 .name = "video",
2476 .init = video_init,
2477 .read = video_read,
2478 .write = video_write,
2479 .exit = video_exit,
2480 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002482 .name = "light",
2483 .init = light_init,
2484 .read = light_read,
2485 .write = light_write,
2486 },
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03002487#ifdef CONFIG_THINKPAD_ACPI_DOCK
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002489 .name = "dock",
2490 .read = dock_read,
2491 .write = dock_write,
2492 .notify = dock_notify,
2493 .handle = &dock_handle,
2494 .type = ACPI_SYSTEM_NOTIFY,
2495 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002497 .name = "dock",
2498 .hid = IBM_PCI_HID,
2499 .notify = dock_notify,
2500 .handle = &pci_handle,
2501 .type = ACPI_SYSTEM_NOTIFY,
2502 },
Kristen Accardi63e5f242006-02-23 17:56:06 -08002503#endif
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03002504#ifdef CONFIG_THINKPAD_ACPI_BAY
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002506 .name = "bay",
2507 .init = bay_init,
2508 .read = bay_read,
2509 .write = bay_write,
2510 .notify = bay_notify,
2511 .handle = &bay_handle,
2512 .type = ACPI_SYSTEM_NOTIFY,
2513 },
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03002514#endif /* CONFIG_THINKPAD_ACPI_BAY */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002516 .name = "cmos",
2517 .read = cmos_read,
2518 .write = cmos_write,
2519 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002521 .name = "led",
2522 .init = led_init,
2523 .read = led_read,
2524 .write = led_write,
2525 },
2526 {
2527 .name = "beep",
2528 .read = beep_read,
2529 .write = beep_write,
2530 },
2531 {
2532 .name = "thermal",
2533 .init = thermal_init,
2534 .read = thermal_read,
2535 },
2536 {
2537 .name = "ecdump",
2538 .read = ecdump_read,
2539 .write = ecdump_write,
2540 .experimental = 1,
2541 },
2542 {
2543 .name = "brightness",
2544 .read = brightness_read,
2545 .write = brightness_write,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02002546 .init = brightness_init,
2547 .exit = brightness_exit,
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002548 },
2549 {
2550 .name = "volume",
2551 .read = volume_read,
2552 .write = volume_write,
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002553 },
2554 {
2555 .name = "fan",
2556 .read = fan_read,
2557 .write = fan_write,
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002558 .init = fan_init,
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002559 .exit = fan_exit,
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002560 .experimental = 1,
2561 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002564/*
2565 * Module and infrastructure proble, init and exit handling
2566 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002568static int __init ibm_init(struct ibm_struct *ibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569{
2570 int ret;
2571 struct proc_dir_entry *entry;
2572
2573 if (ibm->experimental && !experimental)
2574 return 0;
2575
2576 if (ibm->hid) {
Henrique de Moraes Holschuh67001212007-04-21 11:08:25 -03002577 ret = register_tpacpi_subdriver(ibm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 if (ret < 0)
2579 return ret;
2580 ibm->driver_registered = 1;
2581 }
2582
2583 if (ibm->init) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002584 ret = ibm->init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 if (ret != 0)
2586 return ret;
2587 ibm->init_called = 1;
2588 }
2589
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002590 if (ibm->read) {
2591 entry = create_proc_entry(ibm->name,
2592 S_IFREG | S_IRUGO | S_IWUSR,
2593 proc_dir);
2594 if (!entry) {
2595 printk(IBM_ERR "unable to create proc entry %s\n",
2596 ibm->name);
2597 return -ENODEV;
2598 }
2599 entry->owner = THIS_MODULE;
2600 entry->data = ibm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 entry->read_proc = &dispatch_read;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002602 if (ibm->write)
2603 entry->write_proc = &dispatch_write;
2604 ibm->proc_created = 1;
2605 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606
2607 if (ibm->notify) {
2608 ret = setup_notify(ibm);
Henrique de Moraes Holschuh96e89af2007-03-15 16:15:06 -03002609 if (ret == -ENODEV) {
2610 printk(IBM_NOTICE "disabling subdriver %s\n",
2611 ibm->name);
2612 ibm_exit(ibm);
2613 return 0;
2614 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 if (ret < 0)
2616 return ret;
2617 }
2618
2619 return 0;
2620}
2621
2622static void ibm_exit(struct ibm_struct *ibm)
2623{
2624 if (ibm->notify_installed)
2625 acpi_remove_notify_handler(*ibm->handle, ibm->type,
2626 dispatch_notify);
2627
2628 if (ibm->proc_created)
2629 remove_proc_entry(ibm->name, proc_dir);
2630
2631 if (ibm->init_called && ibm->exit)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002632 ibm->exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633
2634 if (ibm->driver_registered) {
2635 acpi_bus_unregister_driver(ibm->driver);
2636 kfree(ibm->driver);
2637 }
2638}
2639
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002640/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002642static char *ibm_thinkpad_ec_found = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02002644static char* __init check_dmi_for_ec(void)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002645{
2646 struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02002647 char ec_fw_string[18];
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002648
2649 /*
2650 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
2651 * X32 or newer, all Z series; Some models must have an
2652 * up-to-date BIOS or they will not be detected.
2653 *
2654 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
2655 */
2656 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02002657 if (sscanf(dev->name,
2658 "IBM ThinkPad Embedded Controller -[%17c",
2659 ec_fw_string) == 1) {
2660 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
2661 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
2662 return kstrdup(ec_fw_string, GFP_KERNEL);
2663 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002664 }
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02002665 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666}
2667
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002668/* Module init, exit, parameters */
2669
2670static int __init set_ibm_param(const char *val, struct kernel_param *kp)
2671{
2672 unsigned int i;
2673
2674 for (i = 0; i < ARRAY_SIZE(ibms); i++)
2675 if (strcmp(ibms[i].name, kp->name) == 0 && ibms[i].write) {
2676 if (strlen(val) > sizeof(ibms[i].param) - 2)
2677 return -ENOSPC;
2678 strcpy(ibms[i].param, val);
2679 strcat(ibms[i].param, ",");
2680 return 0;
2681 }
2682
2683 return -EINVAL;
2684}
2685
2686static int experimental;
2687module_param(experimental, int, 0);
2688
2689#define IBM_PARAM(feature) \
2690 module_param_call(feature, set_ibm_param, NULL, NULL, 0)
2691
2692IBM_PARAM(hotkey);
2693IBM_PARAM(bluetooth);
2694IBM_PARAM(video);
2695IBM_PARAM(light);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03002696#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002697IBM_PARAM(dock);
2698#endif
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03002699#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002700IBM_PARAM(bay);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03002701#endif /* CONFIG_THINKPAD_ACPI_BAY */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002702IBM_PARAM(cmos);
2703IBM_PARAM(led);
2704IBM_PARAM(beep);
2705IBM_PARAM(ecdump);
2706IBM_PARAM(brightness);
2707IBM_PARAM(volume);
2708IBM_PARAM(fan);
2709
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03002710static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711{
2712 int ret, i;
2713
2714 if (acpi_disabled)
2715 return -ENODEV;
2716
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002717 /* ec is required because many other handles are relative to it */
2718 IBM_HANDLE_INIT(ec);
2719 if (!ec_handle) {
2720 printk(IBM_ERR "ec object not found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 return -ENODEV;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002722 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002724 /* Models with newer firmware report the EC in DMI */
2725 ibm_thinkpad_ec_found = check_dmi_for_ec();
2726
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 /* these handles are not required */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002728 IBM_HANDLE_INIT(vid);
2729 IBM_HANDLE_INIT(vid2);
2730 IBM_HANDLE_INIT(ledb);
2731 IBM_HANDLE_INIT(led);
2732 IBM_HANDLE_INIT(hkey);
2733 IBM_HANDLE_INIT(lght);
2734 IBM_HANDLE_INIT(cmos);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03002735#ifdef CONFIG_THINKPAD_ACPI_DOCK
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002736 IBM_HANDLE_INIT(dock);
Kristen Accardi63e5f242006-02-23 17:56:06 -08002737#endif
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002738 IBM_HANDLE_INIT(pci);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03002739#ifdef CONFIG_THINKPAD_ACPI_BAY
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002740 IBM_HANDLE_INIT(bay);
2741 if (bay_handle)
2742 IBM_HANDLE_INIT(bay_ej);
2743 IBM_HANDLE_INIT(bay2);
2744 if (bay2_handle)
2745 IBM_HANDLE_INIT(bay2_ej);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03002746#endif /* CONFIG_THINKPAD_ACPI_BAY */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002747 IBM_HANDLE_INIT(beep);
2748 IBM_HANDLE_INIT(ecrd);
2749 IBM_HANDLE_INIT(ecwr);
2750 IBM_HANDLE_INIT(fans);
2751 IBM_HANDLE_INIT(gfan);
2752 IBM_HANDLE_INIT(sfan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753
2754 proc_dir = proc_mkdir(IBM_DIR, acpi_root_dir);
2755 if (!proc_dir) {
2756 printk(IBM_ERR "unable to create proc dir %s", IBM_DIR);
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03002757 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 return -ENODEV;
2759 }
2760 proc_dir->owner = THIS_MODULE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002761
2762 for (i = 0; i < ARRAY_SIZE(ibms); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 ret = ibm_init(&ibms[i]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002764 if (ret >= 0 && *ibms[i].param)
2765 ret = ibms[i].write(ibms[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03002767 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 return ret;
2769 }
2770 }
2771
2772 return 0;
2773}
2774
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03002775static void thinkpad_acpi_module_exit(void)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002776{
2777 int i;
2778
2779 for (i = ARRAY_SIZE(ibms) - 1; i >= 0; i--)
2780 ibm_exit(&ibms[i]);
2781
2782 if (proc_dir)
2783 remove_proc_entry(IBM_DIR, acpi_root_dir);
2784
2785 if (ibm_thinkpad_ec_found)
2786 kfree(ibm_thinkpad_ec_found);
2787}
2788
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03002789module_init(thinkpad_acpi_module_init);
2790module_exit(thinkpad_acpi_module_exit);