blob: e8fc8da356693d56f8699e6ed3aaa91988296eef [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
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300280static void ibm_handle_init(char *name,
281 acpi_handle *handle, acpi_handle parent,
282 char **paths, int num_paths, char **path)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300283{
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
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300316 dbg_printk(TPACPI_DBG_INIT,
317 "setting up ACPI notify for %s\n", ibm->name);
318
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300319 ret = acpi_bus_get_device(*ibm->handle, &ibm->device);
320 if (ret < 0) {
321 printk(IBM_ERR "%s device not present\n", ibm->name);
322 return -ENODEV;
323 }
324
325 acpi_driver_data(ibm->device) = ibm;
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300326 sprintf(acpi_device_class(ibm->device), "%s/%s",
327 IBM_ACPI_EVENT_PREFIX,
328 ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300329
330 status = acpi_install_notify_handler(*ibm->handle, ibm->type,
331 dispatch_notify, ibm);
332 if (ACPI_FAILURE(status)) {
333 if (status == AE_ALREADY_EXISTS) {
334 printk(IBM_NOTICE "another device driver is already handling %s events\n",
335 ibm->name);
336 } else {
337 printk(IBM_ERR "acpi_install_notify_handler(%s) failed: %d\n",
338 ibm->name, status);
339 }
340 return -ENODEV;
341 }
342 ibm->notify_installed = 1;
343 return 0;
344}
345
346static int __init ibm_device_add(struct acpi_device *device)
347{
348 return 0;
349}
350
Henrique de Moraes Holschuh67001212007-04-21 11:08:25 -0300351static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300352{
353 int ret;
354
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300355 dbg_printk(TPACPI_DBG_INIT,
356 "registering %s as an ACPI driver\n", ibm->name);
357
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300358 ibm->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
359 if (!ibm->driver) {
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300360 printk(IBM_ERR "kzalloc(ibm->driver) failed\n");
361 return -ENOMEM;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300362 }
363
364 sprintf(ibm->driver->name, "%s_%s", IBM_NAME, ibm->name);
365 ibm->driver->ids = ibm->hid;
366 ibm->driver->ops.add = &ibm_device_add;
367
368 ret = acpi_bus_register_driver(ibm->driver);
369 if (ret < 0) {
370 printk(IBM_ERR "acpi_bus_register_driver(%s) failed: %d\n",
371 ibm->hid, ret);
372 kfree(ibm->driver);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300373 ibm->driver = NULL;
374 } else if (!ret)
375 ibm->driver_registered = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300376
377 return ret;
378}
379
380
381/****************************************************************************
382 ****************************************************************************
383 *
384 * Procfs Helpers
385 *
386 ****************************************************************************
387 ****************************************************************************/
388
389static int dispatch_read(char *page, char **start, off_t off, int count,
390 int *eof, void *data)
391{
392 struct ibm_struct *ibm = data;
393 int len;
394
395 if (!ibm || !ibm->read)
396 return -EINVAL;
397
398 len = ibm->read(page);
399 if (len < 0)
400 return len;
401
402 if (len <= off + count)
403 *eof = 1;
404 *start = page + off;
405 len -= off;
406 if (len > count)
407 len = count;
408 if (len < 0)
409 len = 0;
410
411 return len;
412}
413
414static int dispatch_write(struct file *file, const char __user * userbuf,
415 unsigned long count, void *data)
416{
417 struct ibm_struct *ibm = data;
418 char *kernbuf;
419 int ret;
420
421 if (!ibm || !ibm->write)
422 return -EINVAL;
423
424 kernbuf = kmalloc(count + 2, GFP_KERNEL);
425 if (!kernbuf)
426 return -ENOMEM;
427
428 if (copy_from_user(kernbuf, userbuf, count)) {
429 kfree(kernbuf);
430 return -EFAULT;
431 }
432
433 kernbuf[count] = 0;
434 strcat(kernbuf, ",");
435 ret = ibm->write(kernbuf);
436 if (ret == 0)
437 ret = count;
438
439 kfree(kernbuf);
440
441 return ret;
442}
443
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444static char *next_cmd(char **cmds)
445{
446 char *start = *cmds;
447 char *end;
448
449 while ((end = strchr(start, ',')) && end == start)
450 start = end + 1;
451
452 if (!end)
453 return NULL;
454
455 *end = 0;
456 *cmds = end + 1;
457 return start;
458}
459
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300460
461/****************************************************************************
462 ****************************************************************************
463 *
464 * Subdrivers
465 *
466 ****************************************************************************
467 ****************************************************************************/
468
469/*************************************************************************
Henrique de Moraes Holschuh142cfc92007-04-21 11:08:26 -0300470 * thinkpad-acpi init subdriver
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300471 */
472
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300473static int thinkpad_acpi_driver_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
475 printk(IBM_INFO "%s v%s\n", IBM_DESC, IBM_VERSION);
476 printk(IBM_INFO "%s\n", IBM_URL);
477
Henrique de Moraes Holschuh3945ac32007-02-06 19:13:44 -0200478 if (ibm_thinkpad_ec_found)
479 printk(IBM_INFO "ThinkPad EC firmware %s\n",
480 ibm_thinkpad_ec_found);
481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 return 0;
483}
484
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300485static int thinkpad_acpi_driver_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
487 int len = 0;
488
489 len += sprintf(p + len, "driver:\t\t%s\n", IBM_DESC);
490 len += sprintf(p + len, "version:\t%s\n", IBM_VERSION);
491
492 return len;
493}
494
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300495/*************************************************************************
496 * Hotkey subdriver
497 */
498
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400499static int hotkey_supported;
500static int hotkey_mask_supported;
501static int hotkey_orig_status;
502static int hotkey_orig_mask;
503
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300504static int hotkey_init(void)
505{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300506 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
507
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300508 IBM_HANDLE_INIT(hkey);
509
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300510 /* hotkey not supported on 570 */
511 hotkey_supported = hkey_handle != NULL;
512
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300513 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
514 str_supported(hotkey_supported));
515
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300516 if (hotkey_supported) {
517 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
518 A30, R30, R31, T20-22, X20-21, X22-24 */
519 hotkey_mask_supported =
520 acpi_evalf(hkey_handle, NULL, "DHKN", "qv");
521
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300522 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
523 str_supported(hotkey_mask_supported));
524
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300525 if (!hotkey_get(&hotkey_orig_status, &hotkey_orig_mask))
526 return -ENODEV;
527 }
528
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300529 return (hotkey_supported)? 0 : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300530}
531
532static void hotkey_exit(void)
533{
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300534 if (hotkey_supported) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300535 dbg_printk(TPACPI_DBG_EXIT, "restoring original hotkey mask\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300536 hotkey_set(hotkey_orig_status, hotkey_orig_mask);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300537 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300538}
539
540static void hotkey_notify(struct ibm_struct *ibm, u32 event)
541{
542 int hkey;
543
544 if (acpi_evalf(hkey_handle, &hkey, "MHKP", "d"))
545 acpi_bus_generate_event(ibm->device, event, hkey);
546 else {
547 printk(IBM_ERR "unknown hotkey event %d\n", event);
548 acpi_bus_generate_event(ibm->device, event, 0);
549 }
550}
551
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400552static int hotkey_get(int *status, int *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553{
554 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400555 return 0;
556
557 if (hotkey_mask_supported)
558 if (!acpi_evalf(hkey_handle, mask, "DHKN", "d"))
559 return 0;
560
561 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562}
563
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400564static int hotkey_set(int status, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565{
566 int i;
567
568 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 return 0;
570
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400571 if (hotkey_mask_supported)
572 for (i = 0; i < 32; i++) {
573 int bit = ((1 << i) & mask) != 0;
574 if (!acpi_evalf(hkey_handle,
575 NULL, "MHKM", "vdd", i + 1, bit))
576 return 0;
577 }
578
579 return 1;
580}
581
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400582static int hotkey_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583{
584 int status, mask;
585 int len = 0;
586
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400587 if (!hotkey_supported) {
588 len += sprintf(p + len, "status:\t\tnot supported\n");
589 return len;
590 }
591
592 if (!hotkey_get(&status, &mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 return -EIO;
594
595 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400596 if (hotkey_mask_supported) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 len += sprintf(p + len, "mask:\t\t0x%04x\n", mask);
598 len += sprintf(p + len,
599 "commands:\tenable, disable, reset, <mask>\n");
600 } else {
601 len += sprintf(p + len, "mask:\t\tnot supported\n");
602 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
603 }
604
605 return len;
606}
607
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400608static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609{
610 int status, mask;
611 char *cmd;
612 int do_cmd = 0;
613
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400614 if (!hotkey_supported)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 return -ENODEV;
616
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400617 if (!hotkey_get(&status, &mask))
618 return -EIO;
619
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 while ((cmd = next_cmd(&buf))) {
621 if (strlencmp(cmd, "enable") == 0) {
622 status = 1;
623 } else if (strlencmp(cmd, "disable") == 0) {
624 status = 0;
625 } else if (strlencmp(cmd, "reset") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400626 status = hotkey_orig_status;
627 mask = hotkey_orig_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
629 /* mask set */
630 } else if (sscanf(cmd, "%x", &mask) == 1) {
631 /* mask set */
632 } else
633 return -EINVAL;
634 do_cmd = 1;
635 }
636
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400637 if (do_cmd && !hotkey_set(status, mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 return -EIO;
639
640 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400641}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300643/*************************************************************************
644 * Bluetooth subdriver
645 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400647static int bluetooth_supported;
648
649static int bluetooth_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300651 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
652
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300653 IBM_HANDLE_INIT(hkey);
654
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400655 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
656 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
657 bluetooth_supported = hkey_handle &&
658 acpi_evalf(hkey_handle, NULL, "GBDC", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300660 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s\n",
661 str_supported(bluetooth_supported));
662
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300663 return (bluetooth_supported)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664}
665
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400666static int bluetooth_status(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
668 int status;
669
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400670 if (!bluetooth_supported ||
671 !acpi_evalf(hkey_handle, &status, "GBDC", "d"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 status = 0;
673
674 return status;
675}
676
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400677static int bluetooth_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678{
679 int len = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400680 int status = bluetooth_status();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400682 if (!bluetooth_supported)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 len += sprintf(p + len, "status:\t\tnot supported\n");
684 else if (!(status & 1))
685 len += sprintf(p + len, "status:\t\tnot installed\n");
686 else {
687 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 1));
688 len += sprintf(p + len, "commands:\tenable, disable\n");
689 }
690
691 return len;
692}
693
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400694static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400696 int status = bluetooth_status();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 char *cmd;
698 int do_cmd = 0;
699
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400700 if (!bluetooth_supported)
701 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
703 while ((cmd = next_cmd(&buf))) {
704 if (strlencmp(cmd, "enable") == 0) {
705 status |= 2;
706 } else if (strlencmp(cmd, "disable") == 0) {
707 status &= ~2;
708 } else
709 return -EINVAL;
710 do_cmd = 1;
711 }
712
713 if (do_cmd && !acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400714 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
716 return 0;
717}
718
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300719/*************************************************************************
720 * Wan subdriver
721 */
722
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -0400723static int wan_supported;
724
725static int wan_init(void)
726{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300727 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
728
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300729 IBM_HANDLE_INIT(hkey);
730
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -0400731 wan_supported = hkey_handle &&
732 acpi_evalf(hkey_handle, NULL, "GWAN", "qv");
733
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300734 vdbg_printk(TPACPI_DBG_INIT, "wan is %s\n",
735 str_supported(wan_supported));
736
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300737 return (wan_supported)? 0 : 1;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -0400738}
739
740static int wan_status(void)
741{
742 int status;
743
Henrique de Moraes Holschuh8d297262006-11-24 11:47:07 -0200744 if (!wan_supported || !acpi_evalf(hkey_handle, &status, "GWAN", "d"))
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -0400745 status = 0;
746
747 return status;
748}
749
750static int wan_read(char *p)
751{
752 int len = 0;
753 int status = wan_status();
754
755 if (!wan_supported)
756 len += sprintf(p + len, "status:\t\tnot supported\n");
757 else if (!(status & 1))
758 len += sprintf(p + len, "status:\t\tnot installed\n");
759 else {
760 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 1));
761 len += sprintf(p + len, "commands:\tenable, disable\n");
762 }
763
764 return len;
765}
766
767static int wan_write(char *buf)
768{
769 int status = wan_status();
770 char *cmd;
771 int do_cmd = 0;
772
773 if (!wan_supported)
774 return -ENODEV;
775
776 while ((cmd = next_cmd(&buf))) {
777 if (strlencmp(cmd, "enable") == 0) {
778 status |= 2;
779 } else if (strlencmp(cmd, "disable") == 0) {
780 status &= ~2;
781 } else
782 return -EINVAL;
783 do_cmd = 1;
784 }
785
786 if (do_cmd && !acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
787 return -EIO;
788
789 return 0;
790}
791
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300792/*************************************************************************
793 * Video subdriver
794 */
795
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200796static enum video_access_mode video_supported;
797static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400798
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300799IBM_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
800 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
801 "\\_SB.PCI0.VID0", /* 770e */
802 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
803 "\\_SB.PCI0.AGP.VID", /* all others */
804 ); /* R30, R31 */
805
806IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
807
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400808static int video_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400810 int ivga;
811
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300812 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
813
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300814 IBM_HANDLE_INIT(vid);
815 IBM_HANDLE_INIT(vid2);
816
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400817 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
818 /* G41, assume IVGA doesn't change */
819 vid_handle = vid2_handle;
820
821 if (!vid_handle)
822 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300823 video_supported = TPACPI_VIDEO_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400824 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
825 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300826 video_supported = TPACPI_VIDEO_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400827 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
828 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300829 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400830 else
831 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300832 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300834 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
835 str_supported(video_supported != TPACPI_VIDEO_NONE),
836 video_supported);
837
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300838 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839}
840
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300841static void video_exit(void)
842{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300843 dbg_printk(TPACPI_DBG_EXIT, "restoring original video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300844 acpi_evalf(vid_handle, NULL, "_DOS", "vd", video_orig_autosw);
845}
846
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400847static int video_status(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848{
849 int status = 0;
850 int i;
851
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300852 if (video_supported == TPACPI_VIDEO_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400853 if (acpi_evalf(NULL, &i, "\\_SB.PHS", "dd", 0x87))
854 status = i & 3;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300855 } else if (video_supported == TPACPI_VIDEO_770) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400856 if (acpi_evalf(NULL, &i, "\\VCDL", "d"))
857 status |= 0x01 * i;
858 if (acpi_evalf(NULL, &i, "\\VCDC", "d"))
859 status |= 0x02 * i;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300860 } else if (video_supported == TPACPI_VIDEO_NEW) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400861 acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1);
862 if (acpi_evalf(NULL, &i, "\\VCDC", "d"))
863 status |= 0x02 * i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400865 acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0);
866 if (acpi_evalf(NULL, &i, "\\VCDL", "d"))
867 status |= 0x01 * i;
868 if (acpi_evalf(NULL, &i, "\\VCDD", "d"))
869 status |= 0x08 * i;
870 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
872 return status;
873}
874
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400875static int video_autosw(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400877 int autosw = 0;
878
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300879 if (video_supported == TPACPI_VIDEO_570)
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400880 acpi_evalf(vid_handle, &autosw, "SWIT", "d");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300881 else if (video_supported == TPACPI_VIDEO_770 ||
882 video_supported == TPACPI_VIDEO_NEW)
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400883 acpi_evalf(vid_handle, &autosw, "^VDEE", "d");
884
885 return autosw & 1;
886}
887
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400888static int video_switch(void)
889{
890 int autosw = video_autosw();
891 int ret;
892
893 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
894 return -EIO;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300895 ret = video_supported == TPACPI_VIDEO_570 ?
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400896 acpi_evalf(ec_handle, NULL, "_Q16", "v") :
897 acpi_evalf(vid_handle, NULL, "VSWT", "v");
898 acpi_evalf(vid_handle, NULL, "_DOS", "vd", autosw);
899
900 return ret;
901}
902
903static int video_expand(void)
904{
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300905 if (video_supported == TPACPI_VIDEO_570)
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400906 return acpi_evalf(ec_handle, NULL, "_Q17", "v");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300907 else if (video_supported == TPACPI_VIDEO_770)
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400908 return acpi_evalf(vid_handle, NULL, "VEXP", "v");
909 else
910 return acpi_evalf(NULL, NULL, "\\VEXP", "v");
911}
912
913static int video_switch2(int status)
914{
915 int ret;
916
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300917 if (video_supported == TPACPI_VIDEO_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400918 ret = acpi_evalf(NULL, NULL,
919 "\\_SB.PHS2", "vdd", 0x8b, status | 0x80);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300920 } else if (video_supported == TPACPI_VIDEO_770) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400921 int autosw = video_autosw();
922 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
923 return -EIO;
924
925 ret = acpi_evalf(vid_handle, NULL,
926 "ASWT", "vdd", status * 0x100, 0);
927
928 acpi_evalf(vid_handle, NULL, "_DOS", "vd", autosw);
929 } else {
930 ret = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
931 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
932 }
933
934 return ret;
935}
936
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300937static int video_read(char *p)
938{
939 int status = video_status();
940 int autosw = video_autosw();
941 int len = 0;
942
943 if (!video_supported) {
944 len += sprintf(p + len, "status:\t\tnot supported\n");
945 return len;
946 }
947
948 len += sprintf(p + len, "status:\t\tsupported\n");
949 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
950 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300951 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300952 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
953 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
954 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
955 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300956 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300957 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
958 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
959 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
960
961 return len;
962}
963
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400964static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965{
966 char *cmd;
967 int enable, disable, status;
968
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400969 if (!video_supported)
970 return -ENODEV;
971
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 enable = disable = 0;
973
974 while ((cmd = next_cmd(&buf))) {
975 if (strlencmp(cmd, "lcd_enable") == 0) {
976 enable |= 0x01;
977 } else if (strlencmp(cmd, "lcd_disable") == 0) {
978 disable |= 0x01;
979 } else if (strlencmp(cmd, "crt_enable") == 0) {
980 enable |= 0x02;
981 } else if (strlencmp(cmd, "crt_disable") == 0) {
982 disable |= 0x02;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300983 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400984 strlencmp(cmd, "dvi_enable") == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 enable |= 0x08;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300986 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400987 strlencmp(cmd, "dvi_disable") == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 disable |= 0x08;
989 } else if (strlencmp(cmd, "auto_enable") == 0) {
990 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
991 return -EIO;
992 } else if (strlencmp(cmd, "auto_disable") == 0) {
993 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 0))
994 return -EIO;
995 } else if (strlencmp(cmd, "video_switch") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400996 if (!video_switch())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 return -EIO;
998 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400999 if (!video_expand())
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 return -EIO;
1001 } else
1002 return -EINVAL;
1003 }
1004
1005 if (enable || disable) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001006 status = (video_status() & 0x0f & ~disable) | enable;
1007 if (!video_switch2(status))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 return -EIO;
1009 }
1010
1011 return 0;
1012}
1013
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001014/*************************************************************************
1015 * Light (thinklight) subdriver
1016 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001018static int light_supported;
1019static int light_status_supported;
1020
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001021IBM_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
1022IBM_HANDLE(ledb, ec, "LEDB"); /* G4x */
1023
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001024static int light_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001026 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
1027
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001028 IBM_HANDLE_INIT(ledb);
1029 IBM_HANDLE_INIT(lght);
1030 IBM_HANDLE_INIT(cmos);
1031
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001032 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
1033 light_supported = (cmos_handle || lght_handle) && !ledb_handle;
1034
1035 if (light_supported)
1036 /* light status not supported on
1037 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
1038 light_status_supported = acpi_evalf(ec_handle, NULL,
1039 "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001041 vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
1042 str_supported(light_supported));
1043
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001044 return (light_supported)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045}
1046
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001047static int light_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048{
1049 int len = 0;
1050 int status = 0;
1051
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001052 if (!light_supported) {
1053 len += sprintf(p + len, "status:\t\tnot supported\n");
1054 } else if (!light_status_supported) {
1055 len += sprintf(p + len, "status:\t\tunknown\n");
1056 len += sprintf(p + len, "commands:\ton, off\n");
1057 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
1059 return -EIO;
1060 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001061 len += sprintf(p + len, "commands:\ton, off\n");
1062 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
1064 return len;
1065}
1066
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001067static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068{
1069 int cmos_cmd, lght_cmd;
1070 char *cmd;
1071 int success;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001072
1073 if (!light_supported)
1074 return -ENODEV;
1075
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 while ((cmd = next_cmd(&buf))) {
1077 if (strlencmp(cmd, "on") == 0) {
1078 cmos_cmd = 0x0c;
1079 lght_cmd = 1;
1080 } else if (strlencmp(cmd, "off") == 0) {
1081 cmos_cmd = 0x0d;
1082 lght_cmd = 0;
1083 } else
1084 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001085
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 success = cmos_handle ?
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001087 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
1088 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 if (!success)
1090 return -EIO;
1091 }
1092
1093 return 0;
1094}
1095
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001096/*************************************************************************
1097 * Dock subdriver
1098 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03001100#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001101
1102IBM_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
1103 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
1104 "\\_SB.PCI0.PCI1.DOCK", /* all others */
1105 "\\_SB.PCI.ISA.SLCE", /* 570 */
1106 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
1107
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001108/* don't list other alternatives as we install a notify handler on the 570 */
1109IBM_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
1110
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111#define dock_docked() (_sta(dock_handle) & 1)
1112
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001113static int dock_init(void)
1114{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001115 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
1116
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001117 IBM_HANDLE_INIT(dock);
1118 IBM_HANDLE_INIT(pci);
1119
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001120 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
1121 str_supported(dock_handle != NULL));
1122
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001123 return (dock_handle)? 0 : 1;
1124}
1125
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001126static void dock_notify(struct ibm_struct *ibm, u32 event)
1127{
1128 int docked = dock_docked();
1129 int pci = ibm->hid && strstr(ibm->hid, IBM_PCI_HID);
1130
1131 if (event == 1 && !pci) /* 570 */
1132 acpi_bus_generate_event(ibm->device, event, 1); /* button */
1133 else if (event == 1 && pci) /* 570 */
1134 acpi_bus_generate_event(ibm->device, event, 3); /* dock */
1135 else if (event == 3 && docked)
1136 acpi_bus_generate_event(ibm->device, event, 1); /* button */
1137 else if (event == 3 && !docked)
1138 acpi_bus_generate_event(ibm->device, event, 2); /* undock */
1139 else if (event == 0 && docked)
1140 acpi_bus_generate_event(ibm->device, event, 3); /* dock */
1141 else {
1142 printk(IBM_ERR "unknown dock event %d, status %d\n",
1143 event, _sta(dock_handle));
1144 acpi_bus_generate_event(ibm->device, event, 0); /* unknown */
1145 }
1146}
1147
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001148static int dock_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149{
1150 int len = 0;
1151 int docked = dock_docked();
1152
1153 if (!dock_handle)
1154 len += sprintf(p + len, "status:\t\tnot supported\n");
1155 else if (!docked)
1156 len += sprintf(p + len, "status:\t\tundocked\n");
1157 else {
1158 len += sprintf(p + len, "status:\t\tdocked\n");
1159 len += sprintf(p + len, "commands:\tdock, undock\n");
1160 }
1161
1162 return len;
1163}
1164
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001165static int dock_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166{
1167 char *cmd;
1168
1169 if (!dock_docked())
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001170 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
1172 while ((cmd = next_cmd(&buf))) {
1173 if (strlencmp(cmd, "undock") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001174 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
1175 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 return -EIO;
1177 } else if (strlencmp(cmd, "dock") == 0) {
1178 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
1179 return -EIO;
1180 } else
1181 return -EINVAL;
1182 }
1183
1184 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001185}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03001187#endif /* CONFIG_THINKPAD_ACPI_DOCK */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001189/*************************************************************************
1190 * Bay subdriver
1191 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03001193#ifdef CONFIG_THINKPAD_ACPI_BAY
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001194static int bay_status_supported;
1195static int bay_status2_supported;
1196static int bay_eject_supported;
1197static int bay_eject2_supported;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001199IBM_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
1200 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
1201 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
1202 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
1203 ); /* A21e, R30, R31 */
1204IBM_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
1205 "_EJ0", /* all others */
1206 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
1207IBM_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
1208 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
1209 ); /* all others */
1210IBM_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
1211 "_EJ0", /* 770x */
1212 ); /* all others */
1213
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001214static int bay_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001216 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
1217
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001218 IBM_HANDLE_INIT(bay);
1219 if (bay_handle)
1220 IBM_HANDLE_INIT(bay_ej);
1221 IBM_HANDLE_INIT(bay2);
1222 if (bay2_handle)
1223 IBM_HANDLE_INIT(bay2_ej);
1224
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001225 bay_status_supported = bay_handle &&
1226 acpi_evalf(bay_handle, NULL, "_STA", "qv");
1227 bay_status2_supported = bay2_handle &&
1228 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
1229
1230 bay_eject_supported = bay_handle && bay_ej_handle &&
1231 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
1232 bay_eject2_supported = bay2_handle && bay2_ej_handle &&
1233 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001235 vdbg_printk(TPACPI_DBG_INIT,
1236 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
1237 str_supported(bay_status_supported),
1238 str_supported(bay_eject_supported),
1239 str_supported(bay_status2_supported),
1240 str_supported(bay_eject2_supported));
1241
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001242 return (bay_status_supported || bay_eject_supported ||
1243 bay_status2_supported || bay_eject2_supported)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244}
1245
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001246static void bay_notify(struct ibm_struct *ibm, u32 event)
1247{
1248 acpi_bus_generate_event(ibm->device, event, 0);
1249}
1250
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001251#define bay_occupied(b) (_sta(b##_handle) & 1)
1252
1253static int bay_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254{
1255 int len = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001256 int occupied = bay_occupied(bay);
1257 int occupied2 = bay_occupied(bay2);
1258 int eject, eject2;
1259
1260 len += sprintf(p + len, "status:\t\t%s\n", bay_status_supported ?
1261 (occupied ? "occupied" : "unoccupied") :
1262 "not supported");
1263 if (bay_status2_supported)
1264 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
1265 "occupied" : "unoccupied");
1266
1267 eject = bay_eject_supported && occupied;
1268 eject2 = bay_eject2_supported && occupied2;
1269
1270 if (eject && eject2)
1271 len += sprintf(p + len, "commands:\teject, eject2\n");
1272 else if (eject)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 len += sprintf(p + len, "commands:\teject\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001274 else if (eject2)
1275 len += sprintf(p + len, "commands:\teject2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
1277 return len;
1278}
1279
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001280static int bay_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281{
1282 char *cmd;
1283
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001284 if (!bay_eject_supported && !bay_eject2_supported)
1285 return -ENODEV;
1286
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001288 if (bay_eject_supported && strlencmp(cmd, "eject") == 0) {
1289 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
1290 return -EIO;
1291 } else if (bay_eject2_supported &&
1292 strlencmp(cmd, "eject2") == 0) {
1293 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 return -EIO;
1295 } else
1296 return -EINVAL;
1297 }
1298
1299 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001300}
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03001301#endif /* CONFIG_THINKPAD_ACPI_BAY */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001303/*************************************************************************
1304 * CMOS subdriver
1305 */
1306
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001307static int cmos_init(void)
1308{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001309 vdbg_printk(TPACPI_DBG_INIT,
1310 "initializing cmos commands subdriver\n");
1311
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001312 IBM_HANDLE_INIT(cmos);
1313
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001314 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
1315 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001316 return (cmos_handle)? 0 : 1;
1317}
1318
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001319static int cmos_eval(int cmos_cmd)
1320{
1321 if (cmos_handle)
1322 return acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd);
1323 else
1324 return 1;
1325}
1326
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001327static int cmos_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328{
1329 int len = 0;
1330
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001331 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1332 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 if (!cmos_handle)
1334 len += sprintf(p + len, "status:\t\tnot supported\n");
1335 else {
1336 len += sprintf(p + len, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001337 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 }
1339
1340 return len;
1341}
1342
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001343static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344{
1345 char *cmd;
1346 int cmos_cmd;
1347
1348 if (!cmos_handle)
1349 return -EINVAL;
1350
1351 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001352 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
1353 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 /* cmos_cmd set */
1355 } else
1356 return -EINVAL;
1357
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001358 if (!cmos_eval(cmos_cmd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 return -EIO;
1360 }
1361
1362 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001363}
1364
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001365
1366/*************************************************************************
1367 * LED subdriver
1368 */
1369
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02001370static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001371
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001372IBM_HANDLE(led, ec, "SLED", /* 570 */
1373 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
1374 "LED", /* all others */
1375 ); /* R30, R31 */
1376
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001377static int led_init(void)
1378{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001379 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
1380
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001381 IBM_HANDLE_INIT(led);
1382
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001383 if (!led_handle)
1384 /* led not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001385 led_supported = TPACPI_LED_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001386 else if (strlencmp(led_path, "SLED") == 0)
1387 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001388 led_supported = TPACPI_LED_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001389 else if (strlencmp(led_path, "SYSL") == 0)
1390 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001391 led_supported = TPACPI_LED_OLD;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001392 else
1393 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001394 led_supported = TPACPI_LED_NEW;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001395
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001396 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
1397 str_supported(led_supported), led_supported);
1398
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001399 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001400}
1401
1402#define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
1403
1404static int led_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405{
1406 int len = 0;
1407
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001408 if (!led_supported) {
1409 len += sprintf(p + len, "status:\t\tnot supported\n");
1410 return len;
1411 }
1412 len += sprintf(p + len, "status:\t\tsupported\n");
1413
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001414 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001415 /* 570 */
1416 int i, status;
1417 for (i = 0; i < 8; i++) {
1418 if (!acpi_evalf(ec_handle,
1419 &status, "GLED", "dd", 1 << i))
1420 return -EIO;
1421 len += sprintf(p + len, "%d:\t\t%s\n",
1422 i, led_status(status));
1423 }
1424 }
1425
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 len += sprintf(p + len, "commands:\t"
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001427 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
1429 return len;
1430}
1431
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001432/* off, on, blink */
1433static const int led_sled_arg1[] = { 0, 1, 3 };
1434static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
1435static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
1436static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
1437
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001438static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439{
1440 char *cmd;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001441 int led, ind, ret;
1442
1443 if (!led_supported)
1444 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
1446 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001447 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 return -EINVAL;
1449
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001450 if (strstr(cmd, "off")) {
1451 ind = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 } else if (strstr(cmd, "on")) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001453 ind = 1;
1454 } else if (strstr(cmd, "blink")) {
1455 ind = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 } else
1457 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001458
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001459 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001460 /* 570 */
1461 led = 1 << led;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001463 led, led_sled_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 return -EIO;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001465 } else if (led_supported == TPACPI_LED_OLD) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001466 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
1467 led = 1 << led;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001468 ret = ec_write(TPACPI_LED_EC_HLMS, led);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001469 if (ret >= 0)
1470 ret =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001471 ec_write(TPACPI_LED_EC_HLBL,
Henrique de Moraes Holschuhe062e032007-03-23 17:33:55 -03001472 led * led_exp_hlbl[ind]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001473 if (ret >= 0)
1474 ret =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001475 ec_write(TPACPI_LED_EC_HLCL,
Henrique de Moraes Holschuhe062e032007-03-23 17:33:55 -03001476 led * led_exp_hlcl[ind]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001477 if (ret < 0)
1478 return ret;
1479 } else {
1480 /* all others */
1481 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
1482 led, led_led_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 }
1486
1487 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001488}
1489
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001490/*************************************************************************
1491 * Beep subdriver
1492 */
1493
1494IBM_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
1495
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001496static int beep_init(void)
1497{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001498 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
1499
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001500 IBM_HANDLE_INIT(beep);
1501
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001502 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
1503 str_supported(beep_handle != NULL));
1504
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001505 return (beep_handle)? 0 : 1;
1506}
1507
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001508static int beep_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509{
1510 int len = 0;
1511
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001512 if (!beep_handle)
1513 len += sprintf(p + len, "status:\t\tnot supported\n");
1514 else {
1515 len += sprintf(p + len, "status:\t\tsupported\n");
1516 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
1517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
1519 return len;
1520}
1521
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001522static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523{
1524 char *cmd;
1525 int beep_cmd;
1526
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001527 if (!beep_handle)
1528 return -ENODEV;
1529
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001531 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
1532 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 /* beep_cmd set */
1534 } else
1535 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001536 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 return -EIO;
1538 }
1539
1540 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001541}
1542
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001543/*************************************************************************
1544 * Thermal subdriver
1545 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001546
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001547static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001548
1549static int thermal_init(void)
1550{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001551 u8 t, ta1, ta2;
1552 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001553 int acpi_tmp7;
1554
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001555 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
1556
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001557 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001558
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001559 if (ibm_thinkpad_ec_found && experimental) {
1560 /*
1561 * Direct EC access mode: sensors at registers
1562 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
1563 * non-implemented, thermal sensors return 0x80 when
1564 * not available
1565 */
1566
1567 ta1 = ta2 = 0;
1568 for (i = 0; i < 8; i++) {
1569 if (likely(acpi_ec_read(0x78 + i, &t))) {
1570 ta1 |= t;
1571 } else {
1572 ta1 = 0;
1573 break;
1574 }
1575 if (likely(acpi_ec_read(0xC0 + i, &t))) {
1576 ta2 |= t;
1577 } else {
1578 ta1 = 0;
1579 break;
1580 }
1581 }
1582 if (ta1 == 0) {
1583 /* This is sheer paranoia, but we handle it anyway */
1584 if (acpi_tmp7) {
1585 printk(IBM_ERR
1586 "ThinkPad ACPI EC access misbehaving, "
1587 "falling back to ACPI TMPx access mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001588 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001589 } else {
1590 printk(IBM_ERR
1591 "ThinkPad ACPI EC access misbehaving, "
1592 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001593 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001594 }
1595 } else {
1596 thermal_read_mode =
1597 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001598 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001599 }
1600 } else if (acpi_tmp7) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001601 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
1602 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001603 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001604 } else {
1605 /* Standard ACPI TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001606 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001607 }
1608 } else {
1609 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001610 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001611 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001612
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001613 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
1614 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
1615 thermal_read_mode);
1616
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001617 return (thermal_read_mode != TPACPI_THERMAL_NONE)? 0 : 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001618}
1619
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001620static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
1621{
1622 int i, t;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001623 s8 tmp;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001624 char tmpi[] = "TMPi";
1625
1626 if (!s)
1627 return -EINVAL;
1628
1629 switch (thermal_read_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001630#if TPACPI_MAX_THERMAL_SENSORS >= 16
1631 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001632 for (i = 0; i < 8; i++) {
1633 if (!acpi_ec_read(0xC0 + i, &tmp))
1634 return -EIO;
1635 s->temp[i + 8] = tmp * 1000;
1636 }
1637 /* fallthrough */
1638#endif
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001639 case TPACPI_THERMAL_TPEC_8:
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001640 for (i = 0; i < 8; i++) {
1641 if (!acpi_ec_read(0x78 + i, &tmp))
1642 return -EIO;
1643 s->temp[i] = tmp * 1000;
1644 }
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001645 return (thermal_read_mode == TPACPI_THERMAL_TPEC_16) ? 16 : 8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001646
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001647 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001648 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
1649 return -EIO;
1650 for (i = 0; i < 8; i++) {
1651 tmpi[3] = '0' + i;
1652 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
1653 return -EIO;
1654 s->temp[i] = (t - 2732) * 100;
1655 }
1656 return 8;
1657
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001658 case TPACPI_THERMAL_ACPI_TMP07:
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001659 for (i = 0; i < 8; i++) {
1660 tmpi[3] = '0' + i;
1661 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
1662 return -EIO;
1663 s->temp[i] = t * 1000;
1664 }
1665 return 8;
1666
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001667 case TPACPI_THERMAL_NONE:
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001668 default:
1669 return 0;
1670 }
1671}
1672
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001673static int thermal_read(char *p)
1674{
1675 int len = 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001676 int n, i;
1677 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001678
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001679 n = thermal_get_sensors(&t);
1680 if (unlikely(n < 0))
1681 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001682
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001683 len += sprintf(p + len, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001684
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001685 if (n > 0) {
1686 for (i = 0; i < (n - 1); i++)
1687 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
1688 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
1689 } else
1690 len += sprintf(p + len, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001691
1692 return len;
1693}
1694
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001695/*************************************************************************
1696 * EC Dump subdriver
1697 */
1698
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001699static u8 ecdump_regs[256];
1700
1701static int ecdump_read(char *p)
1702{
1703 int len = 0;
1704 int i, j;
1705 u8 v;
1706
1707 len += sprintf(p + len, "EC "
1708 " +00 +01 +02 +03 +04 +05 +06 +07"
1709 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
1710 for (i = 0; i < 256; i += 16) {
1711 len += sprintf(p + len, "EC 0x%02x:", i);
1712 for (j = 0; j < 16; j++) {
1713 if (!acpi_ec_read(i + j, &v))
1714 break;
1715 if (v != ecdump_regs[i + j])
1716 len += sprintf(p + len, " *%02x", v);
1717 else
1718 len += sprintf(p + len, " %02x", v);
1719 ecdump_regs[i + j] = v;
1720 }
1721 len += sprintf(p + len, "\n");
1722 if (j != 16)
1723 break;
1724 }
1725
1726 /* These are way too dangerous to advertise openly... */
1727#if 0
1728 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
1729 " (<offset> is 00-ff, <value> is 00-ff)\n");
1730 len += sprintf(p + len, "commands:\t0x<offset> <value> "
1731 " (<offset> is 00-ff, <value> is 0-255)\n");
1732#endif
1733 return len;
1734}
1735
1736static int ecdump_write(char *buf)
1737{
1738 char *cmd;
1739 int i, v;
1740
1741 while ((cmd = next_cmd(&buf))) {
1742 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
1743 /* i and v set */
1744 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
1745 /* i and v set */
1746 } else
1747 return -EINVAL;
1748 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
1749 if (!acpi_ec_write(i, v))
1750 return -EIO;
1751 } else
1752 return -EINVAL;
1753 }
1754
1755 return 0;
1756}
1757
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001758/*************************************************************************
1759 * Backlight/brightness subdriver
1760 */
Holger Macht8acb0252006-10-20 14:30:28 -07001761
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001762static struct backlight_device *ibm_backlight_device = NULL;
Holger Macht8acb0252006-10-20 14:30:28 -07001763
Richard Purdie599a52d2007-02-10 23:07:48 +00001764static struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02001765 .get_brightness = brightness_get,
1766 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02001767};
1768
1769static int brightness_init(void)
1770{
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02001771 int b;
1772
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001773 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
1774
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02001775 b = brightness_get(NULL);
1776 if (b < 0)
1777 return b;
1778
Yu Luming519ab5f2006-12-19 12:56:15 -08001779 ibm_backlight_device = backlight_device_register("ibm", NULL, NULL,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02001780 &ibm_backlight_data);
1781 if (IS_ERR(ibm_backlight_device)) {
1782 printk(IBM_ERR "Could not register backlight device\n");
1783 return PTR_ERR(ibm_backlight_device);
1784 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001785 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02001786
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02001787 ibm_backlight_device->props.max_brightness = 7;
1788 ibm_backlight_device->props.brightness = b;
1789 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00001790
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02001791 return 0;
1792}
1793
1794static void brightness_exit(void)
1795{
1796 if (ibm_backlight_device) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001797 vdbg_printk(TPACPI_DBG_EXIT,
1798 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02001799 backlight_device_unregister(ibm_backlight_device);
1800 ibm_backlight_device = NULL;
1801 }
1802}
1803
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001804static int brightness_update_status(struct backlight_device *bd)
1805{
1806 return brightness_set(
1807 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
1808 bd->props.power == FB_BLANK_UNBLANK) ?
1809 bd->props.brightness : 0);
1810}
1811
1812static int brightness_get(struct backlight_device *bd)
1813{
1814 u8 level;
1815 if (!acpi_ec_read(brightness_offset, &level))
1816 return -EIO;
1817
1818 level &= 0x7;
1819
1820 return level;
1821}
1822
1823static int brightness_set(int value)
1824{
1825 int cmos_cmd, inc, i;
1826 int current_value = brightness_get(NULL);
1827
1828 value &= 7;
1829
1830 cmos_cmd = value > current_value ? TP_CMOS_BRIGHTNESS_UP : TP_CMOS_BRIGHTNESS_DOWN;
1831 inc = value > current_value ? 1 : -1;
1832 for (i = current_value; i != value; i += inc) {
1833 if (!cmos_eval(cmos_cmd))
1834 return -EIO;
1835 if (!acpi_ec_write(brightness_offset, i + inc))
1836 return -EIO;
1837 }
1838
1839 return 0;
1840}
1841
1842static int brightness_read(char *p)
1843{
1844 int len = 0;
1845 int level;
1846
1847 if ((level = brightness_get(NULL)) < 0) {
1848 len += sprintf(p + len, "level:\t\tunreadable\n");
1849 } else {
1850 len += sprintf(p + len, "level:\t\t%d\n", level & 0x7);
1851 len += sprintf(p + len, "commands:\tup, down\n");
1852 len += sprintf(p + len, "commands:\tlevel <level>"
1853 " (<level> is 0-7)\n");
1854 }
1855
1856 return len;
1857}
1858
1859static int brightness_write(char *buf)
1860{
1861 int level;
1862 int new_level;
1863 char *cmd;
1864
1865 while ((cmd = next_cmd(&buf))) {
1866 if ((level = brightness_get(NULL)) < 0)
1867 return level;
1868 level &= 7;
1869
1870 if (strlencmp(cmd, "up") == 0) {
1871 new_level = level == 7 ? 7 : level + 1;
1872 } else if (strlencmp(cmd, "down") == 0) {
1873 new_level = level == 0 ? 0 : level - 1;
1874 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
1875 new_level >= 0 && new_level <= 7) {
1876 /* new_level set */
1877 } else
1878 return -EINVAL;
1879
1880 brightness_set(new_level);
1881 }
1882
1883 return 0;
1884}
1885
1886/*************************************************************************
1887 * Volume subdriver
1888 */
1889
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001890static int volume_read(char *p)
1891{
1892 int len = 0;
1893 u8 level;
1894
1895 if (!acpi_ec_read(volume_offset, &level)) {
1896 len += sprintf(p + len, "level:\t\tunreadable\n");
1897 } else {
1898 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
1899 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
1900 len += sprintf(p + len, "commands:\tup, down, mute\n");
1901 len += sprintf(p + len, "commands:\tlevel <level>"
1902 " (<level> is 0-15)\n");
1903 }
1904
1905 return len;
1906}
1907
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001908static int volume_write(char *buf)
1909{
1910 int cmos_cmd, inc, i;
1911 u8 level, mute;
1912 int new_level, new_mute;
1913 char *cmd;
1914
1915 while ((cmd = next_cmd(&buf))) {
1916 if (!acpi_ec_read(volume_offset, &level))
1917 return -EIO;
1918 new_mute = mute = level & 0x40;
1919 new_level = level = level & 0xf;
1920
1921 if (strlencmp(cmd, "up") == 0) {
1922 if (mute)
1923 new_mute = 0;
1924 else
1925 new_level = level == 15 ? 15 : level + 1;
1926 } else if (strlencmp(cmd, "down") == 0) {
1927 if (mute)
1928 new_mute = 0;
1929 else
1930 new_level = level == 0 ? 0 : level - 1;
1931 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
1932 new_level >= 0 && new_level <= 15) {
1933 /* new_level set */
1934 } else if (strlencmp(cmd, "mute") == 0) {
1935 new_mute = 0x40;
1936 } else
1937 return -EINVAL;
1938
1939 if (new_level != level) { /* mute doesn't change */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001940 cmos_cmd = new_level > level ? TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001941 inc = new_level > level ? 1 : -1;
1942
1943 if (mute && (!cmos_eval(cmos_cmd) ||
1944 !acpi_ec_write(volume_offset, level)))
1945 return -EIO;
1946
1947 for (i = level; i != new_level; i += inc)
1948 if (!cmos_eval(cmos_cmd) ||
1949 !acpi_ec_write(volume_offset, i + inc))
1950 return -EIO;
1951
Henrique de Moraes Holschuhe062e032007-03-23 17:33:55 -03001952 if (mute && (!cmos_eval(TP_CMOS_VOLUME_MUTE) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001953 !acpi_ec_write(volume_offset,
1954 new_level + mute)))
1955 return -EIO;
1956 }
1957
1958 if (new_mute != mute) { /* level doesn't change */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001959 cmos_cmd = new_mute ? TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001960
1961 if (!cmos_eval(cmos_cmd) ||
1962 !acpi_ec_write(volume_offset, level + new_mute))
1963 return -EIO;
1964 }
1965 }
1966
1967 return 0;
1968}
1969
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001970
1971/*************************************************************************
1972 * Fan subdriver
1973 */
1974
1975/*
1976 * FAN ACCESS MODES
1977 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001978 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001979 * ACPI GFAN method: returns fan level
1980 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001981 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03001982 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001983 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001984 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001985 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
1986 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03001987 * EC 0x2f (HFSP) might be available *for reading*, but do not use
1988 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001989 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001990 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03001991 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
1992 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001993 *
1994 * Fan speed changes of any sort (including those caused by the
1995 * disengaged mode) are usually done slowly by the firmware as the
1996 * maximum ammount of fan duty cycle change per second seems to be
1997 * limited.
1998 *
1999 * Reading is not available if GFAN exists.
2000 * Writing is not available if SFAN exists.
2001 *
2002 * Bits
2003 * 7 automatic mode engaged;
2004 * (default operation mode of the ThinkPad)
2005 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03002006 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002007 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03002008 * the tachometer while the fan controller ramps up
2009 * the speed (which can take up to a few *minutes*).
2010 * Speeds up fan to 100% duty-cycle, which is far above
2011 * the standard RPM levels. It is not impossible that
2012 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002013 * 5-3 unused in some models. Extra bits for fan level
2014 * in others, but still useless as all values above
2015 * 7 map to the same speed as level 7 in these models.
2016 * 2-0 fan level (0..7 usually)
2017 * 0x00 = stop
2018 * 0x07 = max (set when temperatures critical)
2019 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002020 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002021 *
2022 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
2023 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
2024 * does so, its initial value is meaningless (0x07).
2025 *
2026 * For firmware bugs, refer to:
2027 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
2028 *
2029 * ----
2030 *
2031 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
2032 * Main fan tachometer reading (in RPM)
2033 *
2034 * This register is present on all ThinkPads with a new-style EC, and
2035 * it is known not to be present on the A21m/e, and T22, as there is
2036 * something else in offset 0x84 according to the ACPI DSDT. Other
2037 * ThinkPads from this same time period (and earlier) probably lack the
2038 * tachometer as well.
2039 *
2040 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
2041 * was never fixed by IBM to report the EC firmware version string
2042 * probably support the tachometer (like the early X models), so
2043 * detecting it is quite hard. We need more data to know for sure.
2044 *
2045 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
2046 * might result.
2047 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03002048 * FIRMWARE BUG: may go stale while the EC is switching to full speed
2049 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002050 *
2051 * For firmware bugs, refer to:
2052 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
2053 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002054 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002055 * ThinkPad X31, X40, X41. Not available in the X60.
2056 *
2057 * FANS ACPI handle: takes three arguments: low speed, medium speed,
2058 * high speed. ACPI DSDT seems to map these three speeds to levels
2059 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
2060 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
2061 *
2062 * The speeds are stored on handles
2063 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
2064 *
2065 * There are three default speed sets, acessible as handles:
2066 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
2067 *
2068 * ACPI DSDT switches which set is in use depending on various
2069 * factors.
2070 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002071 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002072 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
2073 * but the ACPI tables just mention level 7.
2074 */
2075
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002076static enum fan_status_access_mode fan_status_access_mode;
2077static enum fan_control_access_mode fan_control_access_mode;
2078static enum fan_control_commands fan_control_commands;
2079
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002080static int fan_control_status_known;
2081static u8 fan_control_initial_status;
2082
Len Brown25c68a32006-12-08 04:43:41 -05002083static void fan_watchdog_fire(struct work_struct *ignored);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002084static int fan_watchdog_maxinterval;
Len Brown25c68a32006-12-08 04:43:41 -05002085static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002086
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002087IBM_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
2088IBM_HANDLE(gfan, ec, "GFAN", /* 570 */
2089 "\\FSPD", /* 600e/x, 770e, 770x */
2090 ); /* all others */
2091IBM_HANDLE(sfan, ec, "SFAN", /* 570 */
2092 "JFNS", /* 770x-JL */
2093 ); /* all others */
2094
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002095static int fan_init(void)
2096{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002097 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
2098
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002099 fan_status_access_mode = TPACPI_FAN_NONE;
2100 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002101 fan_control_commands = 0;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002102 fan_control_status_known = 1;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002103 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002104
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002105 IBM_HANDLE_INIT(fans);
2106 IBM_HANDLE_INIT(gfan);
2107 IBM_HANDLE_INIT(sfan);
2108
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002109 if (gfan_handle) {
2110 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002111 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002112 } else {
2113 /* all other ThinkPads: note that even old-style
2114 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002115 if (likely(acpi_ec_read(fan_status_offset,
2116 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002117 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002118
2119 /* In some ThinkPads, neither the EC nor the ACPI
2120 * DSDT initialize the fan status, and it ends up
2121 * being set to 0x07 when it *could* be either
2122 * 0x07 or 0x80.
2123 *
2124 * Enable for TP-1Y (T43), TP-78 (R51e),
2125 * TP-76 (R52), TP-70 (T43, R52), which are known
2126 * to be buggy. */
2127 if (fan_control_initial_status == 0x07 &&
2128 ibm_thinkpad_ec_found &&
2129 ((ibm_thinkpad_ec_found[0] == '1' &&
2130 ibm_thinkpad_ec_found[1] == 'Y') ||
2131 (ibm_thinkpad_ec_found[0] == '7' &&
2132 (ibm_thinkpad_ec_found[1] == '6' ||
2133 ibm_thinkpad_ec_found[1] == '8' ||
2134 ibm_thinkpad_ec_found[1] == '0'))
2135 )) {
2136 printk(IBM_NOTICE
2137 "fan_init: initial fan status is "
2138 "unknown, assuming it is in auto "
2139 "mode\n");
2140 fan_control_status_known = 0;
2141 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002142 } else {
2143 printk(IBM_ERR
2144 "ThinkPad ACPI EC access misbehaving, "
2145 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002146 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002147 }
2148 }
2149
2150 if (sfan_handle) {
2151 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002152 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002153 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002154 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002155 } else {
2156 if (!gfan_handle) {
2157 /* gfan without sfan means no fan control */
2158 /* all other models implement TP EC 0x2f control */
2159
2160 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02002161 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002162 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002163 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002164 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002165 TPACPI_FAN_CMD_SPEED |
2166 TPACPI_FAN_CMD_LEVEL |
2167 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002168 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002169 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02002170 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002171 TPACPI_FAN_CMD_LEVEL |
2172 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002173 }
2174 }
2175 }
2176
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002177 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
2178 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
2179 fan_control_access_mode != TPACPI_FAN_WR_NONE),
2180 fan_status_access_mode, fan_control_access_mode);
2181
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002182 return (fan_status_access_mode != TPACPI_FAN_NONE ||
2183 fan_control_access_mode != TPACPI_FAN_WR_NONE)?
2184 0 : 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002185}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002186
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02002187static int fan_get_status(u8 *status)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002188{
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02002189 u8 s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002190
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02002191 /* TODO:
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002192 * Add TPACPI_FAN_RD_ACPI_FANS ? */
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02002193
Henrique de Moraes Holschuh3ef8a602006-11-24 11:47:10 -02002194 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002195 case TPACPI_FAN_RD_ACPI_GFAN:
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002196 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02002197
2198 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002199 return -EIO;
2200
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02002201 if (likely(status))
2202 *status = s & 0x07;
2203
2204 break;
2205
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002206 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02002207 /* all except 570, 600e/x, 770e, 770x */
2208 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
2209 return -EIO;
2210
2211 if (likely(status))
2212 *status = s;
2213
2214 break;
2215
2216 default:
2217 return -ENXIO;
2218 }
2219
2220 return 0;
2221}
2222
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002223static void fan_exit(void)
2224{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002225 vdbg_printk(TPACPI_DBG_EXIT, "cancelling any pending watchdogs\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002226 cancel_delayed_work(&fan_watchdog_task);
2227 flush_scheduled_work();
2228}
2229
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02002230static int fan_get_speed(unsigned int *speed)
2231{
2232 u8 hi, lo;
2233
2234 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002235 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02002236 /* all except 570, 600e/x, 770e, 770x */
2237 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
2238 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
2239 return -EIO;
2240
2241 if (likely(speed))
2242 *speed = (hi << 8) | lo;
2243
2244 break;
2245
2246 default:
2247 return -ENXIO;
2248 }
2249
2250 return 0;
2251}
2252
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002253static void fan_watchdog_fire(struct work_struct *ignored)
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002254{
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002255 printk(IBM_NOTICE "fan watchdog: enabling fan\n");
2256 if (fan_set_enable()) {
2257 printk(IBM_ERR "fan watchdog: error while enabling fan\n");
2258 /* reschedule for later */
2259 fan_watchdog_reset();
2260 }
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002261}
2262
2263static void fan_watchdog_reset(void)
2264{
2265 static int fan_watchdog_active = 0;
2266
2267 if (fan_watchdog_active)
2268 cancel_delayed_work(&fan_watchdog_task);
2269
2270 if (fan_watchdog_maxinterval > 0) {
2271 fan_watchdog_active = 1;
2272 if (!schedule_delayed_work(&fan_watchdog_task,
2273 msecs_to_jiffies(fan_watchdog_maxinterval
2274 * 1000))) {
2275 printk(IBM_ERR "failed to schedule the fan watchdog, "
2276 "watchdog will not trigger\n");
2277 }
2278 } else
2279 fan_watchdog_active = 0;
2280}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002281
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002282static int fan_set_level(int level)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002283{
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002284 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002285 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002286 if (level >= 0 && level <= 7) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002287 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
2288 return -EIO;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002289 } else
2290 return -EINVAL;
2291 break;
2292
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002293 case TPACPI_FAN_WR_ACPI_FANS:
2294 case TPACPI_FAN_WR_TPEC:
2295 if ((level != TP_EC_FAN_AUTO) &&
2296 (level != TP_EC_FAN_FULLSPEED) &&
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02002297 ((level < 0) || (level > 7)))
2298 return -EINVAL;
2299
2300 if (!acpi_ec_write(fan_status_offset, level))
2301 return -EIO;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002302 else
2303 fan_control_status_known = 1;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02002304 break;
2305
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002306 default:
2307 return -ENXIO;
2308 }
2309 return 0;
2310}
2311
2312static int fan_set_enable(void)
2313{
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002314 u8 s;
2315 int rc;
2316
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002317 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002318 case TPACPI_FAN_WR_ACPI_FANS:
2319 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002320 if ((rc = fan_get_status(&s)) < 0)
2321 return rc;
2322
2323 /* Don't go out of emergency fan mode */
2324 if (s != 7)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002325 s = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002326
2327 if (!acpi_ec_write(fan_status_offset, s))
2328 return -EIO;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002329 else
2330 fan_control_status_known = 1;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002331 break;
2332
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002333 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002334 if ((rc = fan_get_status(&s)) < 0)
2335 return rc;
2336
2337 s &= 0x07;
2338
2339 /* Set fan to at least level 4 */
2340 if (s < 4)
2341 s = 4;
2342
2343 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002344 return -EIO;
2345 break;
2346
2347 default:
2348 return -ENXIO;
2349 }
2350 return 0;
2351}
2352
2353static int fan_set_disable(void)
2354{
2355 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002356 case TPACPI_FAN_WR_ACPI_FANS:
2357 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002358 if (!acpi_ec_write(fan_status_offset, 0x00))
2359 return -EIO;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002360 else
2361 fan_control_status_known = 1;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002362 break;
2363
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002364 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002365 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
2366 return -EIO;
2367 break;
2368
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002369 default:
2370 return -ENXIO;
2371 }
2372 return 0;
2373}
2374
2375static int fan_set_speed(int speed)
2376{
2377 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002378 case TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002379 if (speed >= 0 && speed <= 65535) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002380 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
2381 speed, speed, speed))
2382 return -EIO;
2383 } else
2384 return -EINVAL;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002385 break;
2386
2387 default:
2388 return -ENXIO;
2389 }
2390 return 0;
2391}
2392
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002393static int fan_read(char *p)
2394{
2395 int len = 0;
2396 int rc;
2397 u8 status;
2398 unsigned int speed = 0;
2399
2400 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002401 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002402 /* 570, 600e/x, 770e, 770x */
2403 if ((rc = fan_get_status(&status)) < 0)
2404 return rc;
2405
2406 len += sprintf(p + len, "status:\t\t%s\n"
2407 "level:\t\t%d\n",
2408 (status != 0) ? "enabled" : "disabled", status);
2409 break;
2410
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002411 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002412 /* all except 570, 600e/x, 770e, 770x */
2413 if ((rc = fan_get_status(&status)) < 0)
2414 return rc;
2415
2416 if (unlikely(!fan_control_status_known)) {
2417 if (status != fan_control_initial_status)
2418 fan_control_status_known = 1;
2419 else
2420 /* Return most likely status. In fact, it
2421 * might be the only possible status */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002422 status = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002423 }
2424
2425 len += sprintf(p + len, "status:\t\t%s\n",
2426 (status != 0) ? "enabled" : "disabled");
2427
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002428 if ((rc = fan_get_speed(&speed)) < 0)
2429 return rc;
2430
2431 len += sprintf(p + len, "speed:\t\t%d\n", speed);
2432
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002433 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002434 /* Disengaged mode takes precedence */
2435 len += sprintf(p + len, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002436 else if (status & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002437 len += sprintf(p + len, "level:\t\tauto\n");
2438 else
2439 len += sprintf(p + len, "level:\t\t%d\n", status);
2440 break;
2441
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002442 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002443 default:
2444 len += sprintf(p + len, "status:\t\tnot supported\n");
2445 }
2446
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002447 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002448 len += sprintf(p + len, "commands:\tlevel <level>");
2449
2450 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002451 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002452 len += sprintf(p + len, " (<level> is 0-7)\n");
2453 break;
2454
2455 default:
2456 len += sprintf(p + len, " (<level> is 0-7, "
2457 "auto, disengaged)\n");
2458 break;
2459 }
2460 }
2461
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002462 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002463 len += sprintf(p + len, "commands:\tenable, disable\n"
2464 "commands:\twatchdog <timeout> (<timeout> is 0 (off), "
2465 "1-120 (seconds))\n");
2466
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002467 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002468 len += sprintf(p + len, "commands:\tspeed <speed>"
2469 " (<speed> is 0-65535)\n");
2470
2471 return len;
2472}
2473
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002474static int fan_write_cmd_level(const char *cmd, int *rc)
2475{
2476 int level;
2477
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02002478 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002479 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02002480 else if (strlencmp(cmd, "level disengaged") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002481 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02002482 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002483 return 0;
2484
2485 if ((*rc = fan_set_level(level)) == -ENXIO)
2486 printk(IBM_ERR "level command accepted for unsupported "
2487 "access mode %d", fan_control_access_mode);
2488
2489 return 1;
2490}
2491
2492static int fan_write_cmd_enable(const char *cmd, int *rc)
2493{
2494 if (strlencmp(cmd, "enable") != 0)
2495 return 0;
2496
2497 if ((*rc = fan_set_enable()) == -ENXIO)
2498 printk(IBM_ERR "enable command accepted for unsupported "
2499 "access mode %d", fan_control_access_mode);
2500
2501 return 1;
2502}
2503
2504static int fan_write_cmd_disable(const char *cmd, int *rc)
2505{
2506 if (strlencmp(cmd, "disable") != 0)
2507 return 0;
2508
2509 if ((*rc = fan_set_disable()) == -ENXIO)
2510 printk(IBM_ERR "disable command accepted for unsupported "
2511 "access mode %d", fan_control_access_mode);
2512
2513 return 1;
2514}
2515
2516static int fan_write_cmd_speed(const char *cmd, int *rc)
2517{
2518 int speed;
2519
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02002520 /* TODO:
2521 * Support speed <low> <medium> <high> ? */
2522
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002523 if (sscanf(cmd, "speed %d", &speed) != 1)
2524 return 0;
2525
2526 if ((*rc = fan_set_speed(speed)) == -ENXIO)
2527 printk(IBM_ERR "speed command accepted for unsupported "
2528 "access mode %d", fan_control_access_mode);
2529
2530 return 1;
2531}
2532
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002533static int fan_write_cmd_watchdog(const char *cmd, int *rc)
2534{
2535 int interval;
2536
2537 if (sscanf(cmd, "watchdog %d", &interval) != 1)
2538 return 0;
2539
2540 if (interval < 0 || interval > 120)
2541 *rc = -EINVAL;
2542 else
2543 fan_watchdog_maxinterval = interval;
2544
2545 return 1;
2546}
2547
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002548static int fan_write(char *buf)
2549{
2550 char *cmd;
2551 int rc = 0;
2552
2553 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002554 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002555 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002556 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002557 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002558 fan_write_cmd_disable(cmd, &rc) ||
2559 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002560 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002561 fan_write_cmd_speed(cmd, &rc))
2562 )
2563 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002564 else if (!rc)
2565 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002566 }
2567
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002568 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002569}
2570
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002571/****************************************************************************
2572 ****************************************************************************
2573 *
2574 * Infrastructure
2575 *
2576 ****************************************************************************
2577 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002579/* /proc support */
2580static struct proc_dir_entry *proc_dir = NULL;
2581
2582/* Subdriver registry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583static struct ibm_struct ibms[] = {
2584 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002585 .name = "driver",
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -03002586 .init = thinkpad_acpi_driver_init,
2587 .read = thinkpad_acpi_driver_read,
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002588 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002590 .name = "hotkey",
2591 .hid = IBM_HKEY_HID,
2592 .init = hotkey_init,
2593 .read = hotkey_read,
2594 .write = hotkey_write,
2595 .exit = hotkey_exit,
2596 .notify = hotkey_notify,
2597 .handle = &hkey_handle,
2598 .type = ACPI_DEVICE_NOTIFY,
2599 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002601 .name = "bluetooth",
2602 .init = bluetooth_init,
2603 .read = bluetooth_read,
2604 .write = bluetooth_write,
2605 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 {
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002607 .name = "wan",
2608 .init = wan_init,
2609 .read = wan_read,
2610 .write = wan_write,
2611 .experimental = 1,
2612 },
2613 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002614 .name = "video",
2615 .init = video_init,
2616 .read = video_read,
2617 .write = video_write,
2618 .exit = video_exit,
2619 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002621 .name = "light",
2622 .init = light_init,
2623 .read = light_read,
2624 .write = light_write,
2625 },
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03002626#ifdef CONFIG_THINKPAD_ACPI_DOCK
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002628 .name = "dock",
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002629 .init = dock_init,
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002630 .read = dock_read,
2631 .write = dock_write,
2632 .notify = dock_notify,
2633 .handle = &dock_handle,
2634 .type = ACPI_SYSTEM_NOTIFY,
2635 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002637 .name = "dock",
2638 .hid = IBM_PCI_HID,
2639 .notify = dock_notify,
2640 .handle = &pci_handle,
2641 .type = ACPI_SYSTEM_NOTIFY,
2642 },
Kristen Accardi63e5f242006-02-23 17:56:06 -08002643#endif
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03002644#ifdef CONFIG_THINKPAD_ACPI_BAY
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002646 .name = "bay",
2647 .init = bay_init,
2648 .read = bay_read,
2649 .write = bay_write,
2650 .notify = bay_notify,
2651 .handle = &bay_handle,
2652 .type = ACPI_SYSTEM_NOTIFY,
2653 },
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03002654#endif /* CONFIG_THINKPAD_ACPI_BAY */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002656 .name = "cmos",
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002657 .init = cmos_init,
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002658 .read = cmos_read,
2659 .write = cmos_write,
2660 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002662 .name = "led",
2663 .init = led_init,
2664 .read = led_read,
2665 .write = led_write,
2666 },
2667 {
2668 .name = "beep",
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002669 .init = beep_init,
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002670 .read = beep_read,
2671 .write = beep_write,
2672 },
2673 {
2674 .name = "thermal",
2675 .init = thermal_init,
2676 .read = thermal_read,
2677 },
2678 {
2679 .name = "ecdump",
2680 .read = ecdump_read,
2681 .write = ecdump_write,
2682 .experimental = 1,
2683 },
2684 {
2685 .name = "brightness",
2686 .read = brightness_read,
2687 .write = brightness_write,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02002688 .init = brightness_init,
2689 .exit = brightness_exit,
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002690 },
2691 {
2692 .name = "volume",
2693 .read = volume_read,
2694 .write = volume_write,
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002695 },
2696 {
2697 .name = "fan",
2698 .read = fan_read,
2699 .write = fan_write,
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002700 .init = fan_init,
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002701 .exit = fan_exit,
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002702 .experimental = 1,
2703 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002706/*
2707 * Module and infrastructure proble, init and exit handling
2708 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002710#ifdef CONFIG_THINKPAD_ACPI_DEBUG
2711static const char * str_supported(int is_supported)
2712{
2713 static const char * const text_unsupported = "not supported";
2714
2715 return (is_supported)? text_unsupported + 4 : text_unsupported;
2716}
2717#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
2718
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002719static int __init ibm_init(struct ibm_struct *ibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720{
2721 int ret;
2722 struct proc_dir_entry *entry;
2723
2724 if (ibm->experimental && !experimental)
2725 return 0;
2726
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002727 dbg_printk(TPACPI_DBG_INIT,
2728 "probing for %s\n", ibm->name);
2729
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 if (ibm->init) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002731 ret = ibm->init();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002732 if (ret > 0)
2733 return 0; /* probe failed */
2734 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 return ret;
2736 ibm->init_called = 1;
2737 }
2738
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002739 if (ibm->hid) {
2740 ret = register_tpacpi_subdriver(ibm);
2741 if (ret)
2742 goto err_out;
2743 }
2744
2745 if (ibm->notify) {
2746 ret = setup_notify(ibm);
2747 if (ret == -ENODEV) {
2748 printk(IBM_NOTICE "disabling subdriver %s\n",
2749 ibm->name);
2750 ret = 0;
2751 goto err_out;
2752 }
2753 if (ret < 0)
2754 goto err_out;
2755 }
2756
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002757 dbg_printk(TPACPI_DBG_INIT,
2758 "%s installed\n", ibm->name);
2759
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002760 if (ibm->read) {
2761 entry = create_proc_entry(ibm->name,
2762 S_IFREG | S_IRUGO | S_IWUSR,
2763 proc_dir);
2764 if (!entry) {
2765 printk(IBM_ERR "unable to create proc entry %s\n",
2766 ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002767 ret = -ENODEV;
2768 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002769 }
2770 entry->owner = THIS_MODULE;
2771 entry->data = ibm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 entry->read_proc = &dispatch_read;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002773 if (ibm->write)
2774 entry->write_proc = &dispatch_write;
2775 ibm->proc_created = 1;
2776 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002779
2780err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002781 dbg_printk(TPACPI_DBG_INIT,
2782 "%s: at error exit path with result %d\n",
2783 ibm->name, ret);
2784
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002785 ibm_exit(ibm);
2786 return (ret < 0)? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787}
2788
2789static void ibm_exit(struct ibm_struct *ibm)
2790{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002791 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002792 if (ibm->notify_installed) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002793 dbg_printk(TPACPI_DBG_EXIT,
2794 "%s: acpi_remove_notify_handler\n", ibm->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 acpi_remove_notify_handler(*ibm->handle, ibm->type,
2796 dispatch_notify);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002797 ibm->notify_installed = 0;
2798 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002800 if (ibm->proc_created) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002801 dbg_printk(TPACPI_DBG_EXIT,
2802 "%s: remove_proc_entry\n", ibm->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 remove_proc_entry(ibm->name, proc_dir);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002804 ibm->proc_created = 0;
2805 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806
2807 if (ibm->driver_registered) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002808 dbg_printk(TPACPI_DBG_EXIT,
2809 "%s: acpi_bus_unregister_driver\n", ibm->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 acpi_bus_unregister_driver(ibm->driver);
2811 kfree(ibm->driver);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002812 ibm->driver = NULL;
2813 ibm->driver_registered = 0;
2814 }
2815
2816 if (ibm->init_called && ibm->exit) {
2817 ibm->exit();
2818 ibm->init_called = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 }
2820}
2821
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002822/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002824static char *ibm_thinkpad_ec_found = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02002826static char* __init check_dmi_for_ec(void)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002827{
2828 struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02002829 char ec_fw_string[18];
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002830
2831 /*
2832 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
2833 * X32 or newer, all Z series; Some models must have an
2834 * up-to-date BIOS or they will not be detected.
2835 *
2836 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
2837 */
2838 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02002839 if (sscanf(dev->name,
2840 "IBM ThinkPad Embedded Controller -[%17c",
2841 ec_fw_string) == 1) {
2842 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
2843 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
2844 return kstrdup(ec_fw_string, GFP_KERNEL);
2845 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002846 }
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02002847 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848}
2849
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002850static int __init probe_for_thinkpad(void)
2851{
2852 int is_thinkpad;
2853
2854 if (acpi_disabled)
2855 return -ENODEV;
2856
2857 /*
2858 * Non-ancient models have better DMI tagging, but very old models
2859 * don't.
2860 */
2861 is_thinkpad = dmi_name_in_vendors("ThinkPad");
2862
2863 /* ec is required because many other handles are relative to it */
2864 IBM_HANDLE_INIT(ec);
2865 if (!ec_handle) {
2866 if (is_thinkpad)
2867 printk(IBM_ERR
2868 "Not yet supported ThinkPad detected!\n");
2869 return -ENODEV;
2870 }
2871
2872 return 0;
2873}
2874
2875
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002876/* Module init, exit, parameters */
2877
2878static int __init set_ibm_param(const char *val, struct kernel_param *kp)
2879{
2880 unsigned int i;
2881
2882 for (i = 0; i < ARRAY_SIZE(ibms); i++)
2883 if (strcmp(ibms[i].name, kp->name) == 0 && ibms[i].write) {
2884 if (strlen(val) > sizeof(ibms[i].param) - 2)
2885 return -ENOSPC;
2886 strcpy(ibms[i].param, val);
2887 strcat(ibms[i].param, ",");
2888 return 0;
2889 }
2890
2891 return -EINVAL;
2892}
2893
2894static int experimental;
2895module_param(experimental, int, 0);
2896
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03002897static u32 dbg_level;
2898module_param_named(debug, dbg_level, uint, 0);
2899
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002900#define IBM_PARAM(feature) \
2901 module_param_call(feature, set_ibm_param, NULL, NULL, 0)
2902
2903IBM_PARAM(hotkey);
2904IBM_PARAM(bluetooth);
2905IBM_PARAM(video);
2906IBM_PARAM(light);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03002907#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002908IBM_PARAM(dock);
2909#endif
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03002910#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002911IBM_PARAM(bay);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03002912#endif /* CONFIG_THINKPAD_ACPI_BAY */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002913IBM_PARAM(cmos);
2914IBM_PARAM(led);
2915IBM_PARAM(beep);
2916IBM_PARAM(ecdump);
2917IBM_PARAM(brightness);
2918IBM_PARAM(volume);
2919IBM_PARAM(fan);
2920
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03002921static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922{
2923 int ret, i;
2924
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002925 ret = probe_for_thinkpad();
2926 if (ret)
2927 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002929 ibm_thinkpad_ec_found = check_dmi_for_ec();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002930 IBM_HANDLE_INIT(ecrd);
2931 IBM_HANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932
2933 proc_dir = proc_mkdir(IBM_DIR, acpi_root_dir);
2934 if (!proc_dir) {
2935 printk(IBM_ERR "unable to create proc dir %s", IBM_DIR);
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03002936 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 return -ENODEV;
2938 }
2939 proc_dir->owner = THIS_MODULE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002940
2941 for (i = 0; i < ARRAY_SIZE(ibms); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 ret = ibm_init(&ibms[i]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002943 if (ret >= 0 && *ibms[i].param)
2944 ret = ibms[i].write(ibms[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03002946 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 return ret;
2948 }
2949 }
2950
2951 return 0;
2952}
2953
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03002954static void thinkpad_acpi_module_exit(void)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002955{
2956 int i;
2957
2958 for (i = ARRAY_SIZE(ibms) - 1; i >= 0; i--)
2959 ibm_exit(&ibms[i]);
2960
2961 if (proc_dir)
2962 remove_proc_entry(IBM_DIR, acpi_root_dir);
2963
2964 if (ibm_thinkpad_ec_found)
2965 kfree(ibm_thinkpad_ec_found);
2966}
2967
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03002968module_init(thinkpad_acpi_module_init);
2969module_exit(thinkpad_acpi_module_exit);