blob: a5efd06523ddcc17c79940dab5435fa31a2bdbf3 [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 }
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -0300342 ibm->flags.notify_installed = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300343 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)
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -0300375 ibm->flags.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 Holschuha5763f22007-04-21 11:08:33 -0300473static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
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 Holschuha5763f22007-04-21 11:08:33 -0300495static struct ibm_struct thinkpad_acpi_driver_data = {
496 .name = "driver",
497 .read = thinkpad_acpi_driver_read,
498};
499
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300500/*************************************************************************
501 * Hotkey subdriver
502 */
503
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400504static int hotkey_supported;
505static int hotkey_mask_supported;
506static int hotkey_orig_status;
507static int hotkey_orig_mask;
508
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300509static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300510{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300511 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
512
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300513 IBM_HANDLE_INIT(hkey);
514
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300515 /* hotkey not supported on 570 */
516 hotkey_supported = hkey_handle != NULL;
517
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300518 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
519 str_supported(hotkey_supported));
520
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300521 if (hotkey_supported) {
522 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
523 A30, R30, R31, T20-22, X20-21, X22-24 */
524 hotkey_mask_supported =
525 acpi_evalf(hkey_handle, NULL, "DHKN", "qv");
526
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300527 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
528 str_supported(hotkey_mask_supported));
529
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300530 if (!hotkey_get(&hotkey_orig_status, &hotkey_orig_mask))
531 return -ENODEV;
532 }
533
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300534 return (hotkey_supported)? 0 : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300535}
536
537static void hotkey_exit(void)
538{
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300539 if (hotkey_supported) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300540 dbg_printk(TPACPI_DBG_EXIT, "restoring original hotkey mask\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300541 hotkey_set(hotkey_orig_status, hotkey_orig_mask);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300542 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300543}
544
545static void hotkey_notify(struct ibm_struct *ibm, u32 event)
546{
547 int hkey;
548
549 if (acpi_evalf(hkey_handle, &hkey, "MHKP", "d"))
550 acpi_bus_generate_event(ibm->device, event, hkey);
551 else {
552 printk(IBM_ERR "unknown hotkey event %d\n", event);
553 acpi_bus_generate_event(ibm->device, event, 0);
554 }
555}
556
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400557static int hotkey_get(int *status, int *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558{
559 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400560 return 0;
561
562 if (hotkey_mask_supported)
563 if (!acpi_evalf(hkey_handle, mask, "DHKN", "d"))
564 return 0;
565
566 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567}
568
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400569static int hotkey_set(int status, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570{
571 int i;
572
573 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 return 0;
575
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400576 if (hotkey_mask_supported)
577 for (i = 0; i < 32; i++) {
578 int bit = ((1 << i) & mask) != 0;
579 if (!acpi_evalf(hkey_handle,
580 NULL, "MHKM", "vdd", i + 1, bit))
581 return 0;
582 }
583
584 return 1;
585}
586
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400587static int hotkey_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
589 int status, mask;
590 int len = 0;
591
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400592 if (!hotkey_supported) {
593 len += sprintf(p + len, "status:\t\tnot supported\n");
594 return len;
595 }
596
597 if (!hotkey_get(&status, &mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 return -EIO;
599
600 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400601 if (hotkey_mask_supported) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 len += sprintf(p + len, "mask:\t\t0x%04x\n", mask);
603 len += sprintf(p + len,
604 "commands:\tenable, disable, reset, <mask>\n");
605 } else {
606 len += sprintf(p + len, "mask:\t\tnot supported\n");
607 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
608 }
609
610 return len;
611}
612
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400613static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614{
615 int status, mask;
616 char *cmd;
617 int do_cmd = 0;
618
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400619 if (!hotkey_supported)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 return -ENODEV;
621
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400622 if (!hotkey_get(&status, &mask))
623 return -EIO;
624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 while ((cmd = next_cmd(&buf))) {
626 if (strlencmp(cmd, "enable") == 0) {
627 status = 1;
628 } else if (strlencmp(cmd, "disable") == 0) {
629 status = 0;
630 } else if (strlencmp(cmd, "reset") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400631 status = hotkey_orig_status;
632 mask = hotkey_orig_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
634 /* mask set */
635 } else if (sscanf(cmd, "%x", &mask) == 1) {
636 /* mask set */
637 } else
638 return -EINVAL;
639 do_cmd = 1;
640 }
641
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400642 if (do_cmd && !hotkey_set(status, mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 return -EIO;
644
645 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400646}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300648static struct ibm_struct hotkey_driver_data = {
649 .name = "hotkey",
650 .hid = IBM_HKEY_HID,
651 .read = hotkey_read,
652 .write = hotkey_write,
653 .exit = hotkey_exit,
654 .notify = hotkey_notify,
655 .handle = &hkey_handle,
656 .type = ACPI_DEVICE_NOTIFY,
657};
658
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300659/*************************************************************************
660 * Bluetooth subdriver
661 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400663static int bluetooth_supported;
664
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300665static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300667 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
668
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300669 IBM_HANDLE_INIT(hkey);
670
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400671 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
672 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
673 bluetooth_supported = hkey_handle &&
674 acpi_evalf(hkey_handle, NULL, "GBDC", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300676 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s\n",
677 str_supported(bluetooth_supported));
678
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300679 return (bluetooth_supported)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680}
681
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400682static int bluetooth_status(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683{
684 int status;
685
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400686 if (!bluetooth_supported ||
687 !acpi_evalf(hkey_handle, &status, "GBDC", "d"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 status = 0;
689
690 return status;
691}
692
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400693static int bluetooth_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694{
695 int len = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400696 int status = bluetooth_status();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400698 if (!bluetooth_supported)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 len += sprintf(p + len, "status:\t\tnot supported\n");
700 else if (!(status & 1))
701 len += sprintf(p + len, "status:\t\tnot installed\n");
702 else {
703 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 1));
704 len += sprintf(p + len, "commands:\tenable, disable\n");
705 }
706
707 return len;
708}
709
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400710static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400712 int status = bluetooth_status();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 char *cmd;
714 int do_cmd = 0;
715
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400716 if (!bluetooth_supported)
717 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
719 while ((cmd = next_cmd(&buf))) {
720 if (strlencmp(cmd, "enable") == 0) {
721 status |= 2;
722 } else if (strlencmp(cmd, "disable") == 0) {
723 status &= ~2;
724 } else
725 return -EINVAL;
726 do_cmd = 1;
727 }
728
729 if (do_cmd && !acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400730 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
732 return 0;
733}
734
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300735static struct ibm_struct bluetooth_driver_data = {
736 .name = "bluetooth",
737 .read = bluetooth_read,
738 .write = bluetooth_write,
739};
740
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300741/*************************************************************************
742 * Wan subdriver
743 */
744
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -0400745static int wan_supported;
746
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300747static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -0400748{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300749 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
750
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300751 IBM_HANDLE_INIT(hkey);
752
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -0400753 wan_supported = hkey_handle &&
754 acpi_evalf(hkey_handle, NULL, "GWAN", "qv");
755
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300756 vdbg_printk(TPACPI_DBG_INIT, "wan is %s\n",
757 str_supported(wan_supported));
758
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300759 return (wan_supported)? 0 : 1;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -0400760}
761
762static int wan_status(void)
763{
764 int status;
765
Henrique de Moraes Holschuh8d297262006-11-24 11:47:07 -0200766 if (!wan_supported || !acpi_evalf(hkey_handle, &status, "GWAN", "d"))
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -0400767 status = 0;
768
769 return status;
770}
771
772static int wan_read(char *p)
773{
774 int len = 0;
775 int status = wan_status();
776
777 if (!wan_supported)
778 len += sprintf(p + len, "status:\t\tnot supported\n");
779 else if (!(status & 1))
780 len += sprintf(p + len, "status:\t\tnot installed\n");
781 else {
782 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 1));
783 len += sprintf(p + len, "commands:\tenable, disable\n");
784 }
785
786 return len;
787}
788
789static int wan_write(char *buf)
790{
791 int status = wan_status();
792 char *cmd;
793 int do_cmd = 0;
794
795 if (!wan_supported)
796 return -ENODEV;
797
798 while ((cmd = next_cmd(&buf))) {
799 if (strlencmp(cmd, "enable") == 0) {
800 status |= 2;
801 } else if (strlencmp(cmd, "disable") == 0) {
802 status &= ~2;
803 } else
804 return -EINVAL;
805 do_cmd = 1;
806 }
807
808 if (do_cmd && !acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
809 return -EIO;
810
811 return 0;
812}
813
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300814static struct ibm_struct wan_driver_data = {
815 .name = "wan",
816 .read = wan_read,
817 .write = wan_write,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -0300818 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300819};
820
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300821/*************************************************************************
822 * Video subdriver
823 */
824
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200825static enum video_access_mode video_supported;
826static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400827
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300828IBM_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
829 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
830 "\\_SB.PCI0.VID0", /* 770e */
831 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
832 "\\_SB.PCI0.AGP.VID", /* all others */
833 ); /* R30, R31 */
834
835IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
836
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300837static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400839 int ivga;
840
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300841 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
842
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300843 IBM_HANDLE_INIT(vid);
844 IBM_HANDLE_INIT(vid2);
845
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400846 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
847 /* G41, assume IVGA doesn't change */
848 vid_handle = vid2_handle;
849
850 if (!vid_handle)
851 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300852 video_supported = TPACPI_VIDEO_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400853 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
854 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300855 video_supported = TPACPI_VIDEO_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400856 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
857 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300858 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400859 else
860 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300861 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300863 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
864 str_supported(video_supported != TPACPI_VIDEO_NONE),
865 video_supported);
866
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300867 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868}
869
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300870static void video_exit(void)
871{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300872 dbg_printk(TPACPI_DBG_EXIT, "restoring original video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300873 acpi_evalf(vid_handle, NULL, "_DOS", "vd", video_orig_autosw);
874}
875
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400876static int video_status(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877{
878 int status = 0;
879 int i;
880
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300881 if (video_supported == TPACPI_VIDEO_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400882 if (acpi_evalf(NULL, &i, "\\_SB.PHS", "dd", 0x87))
883 status = i & 3;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300884 } else if (video_supported == TPACPI_VIDEO_770) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400885 if (acpi_evalf(NULL, &i, "\\VCDL", "d"))
886 status |= 0x01 * i;
887 if (acpi_evalf(NULL, &i, "\\VCDC", "d"))
888 status |= 0x02 * i;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300889 } else if (video_supported == TPACPI_VIDEO_NEW) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400890 acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1);
891 if (acpi_evalf(NULL, &i, "\\VCDC", "d"))
892 status |= 0x02 * i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400894 acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0);
895 if (acpi_evalf(NULL, &i, "\\VCDL", "d"))
896 status |= 0x01 * i;
897 if (acpi_evalf(NULL, &i, "\\VCDD", "d"))
898 status |= 0x08 * i;
899 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
901 return status;
902}
903
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400904static int video_autosw(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400906 int autosw = 0;
907
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300908 if (video_supported == TPACPI_VIDEO_570)
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400909 acpi_evalf(vid_handle, &autosw, "SWIT", "d");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300910 else if (video_supported == TPACPI_VIDEO_770 ||
911 video_supported == TPACPI_VIDEO_NEW)
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400912 acpi_evalf(vid_handle, &autosw, "^VDEE", "d");
913
914 return autosw & 1;
915}
916
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400917static int video_switch(void)
918{
919 int autosw = video_autosw();
920 int ret;
921
922 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
923 return -EIO;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300924 ret = video_supported == TPACPI_VIDEO_570 ?
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400925 acpi_evalf(ec_handle, NULL, "_Q16", "v") :
926 acpi_evalf(vid_handle, NULL, "VSWT", "v");
927 acpi_evalf(vid_handle, NULL, "_DOS", "vd", autosw);
928
929 return ret;
930}
931
932static int video_expand(void)
933{
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300934 if (video_supported == TPACPI_VIDEO_570)
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400935 return acpi_evalf(ec_handle, NULL, "_Q17", "v");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300936 else if (video_supported == TPACPI_VIDEO_770)
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400937 return acpi_evalf(vid_handle, NULL, "VEXP", "v");
938 else
939 return acpi_evalf(NULL, NULL, "\\VEXP", "v");
940}
941
942static int video_switch2(int status)
943{
944 int ret;
945
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300946 if (video_supported == TPACPI_VIDEO_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400947 ret = acpi_evalf(NULL, NULL,
948 "\\_SB.PHS2", "vdd", 0x8b, status | 0x80);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300949 } else if (video_supported == TPACPI_VIDEO_770) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400950 int autosw = video_autosw();
951 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
952 return -EIO;
953
954 ret = acpi_evalf(vid_handle, NULL,
955 "ASWT", "vdd", status * 0x100, 0);
956
957 acpi_evalf(vid_handle, NULL, "_DOS", "vd", autosw);
958 } else {
959 ret = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
960 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
961 }
962
963 return ret;
964}
965
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300966static int video_read(char *p)
967{
968 int status = video_status();
969 int autosw = video_autosw();
970 int len = 0;
971
972 if (!video_supported) {
973 len += sprintf(p + len, "status:\t\tnot supported\n");
974 return len;
975 }
976
977 len += sprintf(p + len, "status:\t\tsupported\n");
978 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
979 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300980 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300981 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
982 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
983 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
984 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300985 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300986 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
987 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
988 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
989
990 return len;
991}
992
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400993static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994{
995 char *cmd;
996 int enable, disable, status;
997
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400998 if (!video_supported)
999 return -ENODEV;
1000
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 enable = disable = 0;
1002
1003 while ((cmd = next_cmd(&buf))) {
1004 if (strlencmp(cmd, "lcd_enable") == 0) {
1005 enable |= 0x01;
1006 } else if (strlencmp(cmd, "lcd_disable") == 0) {
1007 disable |= 0x01;
1008 } else if (strlencmp(cmd, "crt_enable") == 0) {
1009 enable |= 0x02;
1010 } else if (strlencmp(cmd, "crt_disable") == 0) {
1011 disable |= 0x02;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001012 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001013 strlencmp(cmd, "dvi_enable") == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 enable |= 0x08;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001015 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001016 strlencmp(cmd, "dvi_disable") == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 disable |= 0x08;
1018 } else if (strlencmp(cmd, "auto_enable") == 0) {
1019 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
1020 return -EIO;
1021 } else if (strlencmp(cmd, "auto_disable") == 0) {
1022 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 0))
1023 return -EIO;
1024 } else if (strlencmp(cmd, "video_switch") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001025 if (!video_switch())
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 return -EIO;
1027 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001028 if (!video_expand())
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 return -EIO;
1030 } else
1031 return -EINVAL;
1032 }
1033
1034 if (enable || disable) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001035 status = (video_status() & 0x0f & ~disable) | enable;
1036 if (!video_switch2(status))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 return -EIO;
1038 }
1039
1040 return 0;
1041}
1042
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001043static struct ibm_struct video_driver_data = {
1044 .name = "video",
1045 .read = video_read,
1046 .write = video_write,
1047 .exit = video_exit,
1048};
1049
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001050/*************************************************************************
1051 * Light (thinklight) subdriver
1052 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001054static int light_supported;
1055static int light_status_supported;
1056
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001057IBM_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
1058IBM_HANDLE(ledb, ec, "LEDB"); /* G4x */
1059
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001060static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001062 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
1063
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001064 IBM_HANDLE_INIT(ledb);
1065 IBM_HANDLE_INIT(lght);
1066 IBM_HANDLE_INIT(cmos);
1067
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001068 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
1069 light_supported = (cmos_handle || lght_handle) && !ledb_handle;
1070
1071 if (light_supported)
1072 /* light status not supported on
1073 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
1074 light_status_supported = acpi_evalf(ec_handle, NULL,
1075 "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001077 vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
1078 str_supported(light_supported));
1079
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001080 return (light_supported)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081}
1082
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001083static int light_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084{
1085 int len = 0;
1086 int status = 0;
1087
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001088 if (!light_supported) {
1089 len += sprintf(p + len, "status:\t\tnot supported\n");
1090 } else if (!light_status_supported) {
1091 len += sprintf(p + len, "status:\t\tunknown\n");
1092 len += sprintf(p + len, "commands:\ton, off\n");
1093 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
1095 return -EIO;
1096 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001097 len += sprintf(p + len, "commands:\ton, off\n");
1098 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
1100 return len;
1101}
1102
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001103static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104{
1105 int cmos_cmd, lght_cmd;
1106 char *cmd;
1107 int success;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001108
1109 if (!light_supported)
1110 return -ENODEV;
1111
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 while ((cmd = next_cmd(&buf))) {
1113 if (strlencmp(cmd, "on") == 0) {
1114 cmos_cmd = 0x0c;
1115 lght_cmd = 1;
1116 } else if (strlencmp(cmd, "off") == 0) {
1117 cmos_cmd = 0x0d;
1118 lght_cmd = 0;
1119 } else
1120 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001121
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 success = cmos_handle ?
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001123 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
1124 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 if (!success)
1126 return -EIO;
1127 }
1128
1129 return 0;
1130}
1131
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001132static struct ibm_struct light_driver_data = {
1133 .name = "light",
1134 .read = light_read,
1135 .write = light_write,
1136};
1137
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001138/*************************************************************************
1139 * Dock subdriver
1140 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03001142#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001143
1144IBM_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
1145 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
1146 "\\_SB.PCI0.PCI1.DOCK", /* all others */
1147 "\\_SB.PCI.ISA.SLCE", /* 570 */
1148 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
1149
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001150/* don't list other alternatives as we install a notify handler on the 570 */
1151IBM_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
1152
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153#define dock_docked() (_sta(dock_handle) & 1)
1154
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001155static int __init dock_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001156{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001157 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
1158
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001159 IBM_HANDLE_INIT(dock);
1160 IBM_HANDLE_INIT(pci);
1161
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001162 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
1163 str_supported(dock_handle != NULL));
1164
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001165 return (dock_handle)? 0 : 1;
1166}
1167
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001168static void dock_notify(struct ibm_struct *ibm, u32 event)
1169{
1170 int docked = dock_docked();
1171 int pci = ibm->hid && strstr(ibm->hid, IBM_PCI_HID);
1172
1173 if (event == 1 && !pci) /* 570 */
1174 acpi_bus_generate_event(ibm->device, event, 1); /* button */
1175 else if (event == 1 && pci) /* 570 */
1176 acpi_bus_generate_event(ibm->device, event, 3); /* dock */
1177 else if (event == 3 && docked)
1178 acpi_bus_generate_event(ibm->device, event, 1); /* button */
1179 else if (event == 3 && !docked)
1180 acpi_bus_generate_event(ibm->device, event, 2); /* undock */
1181 else if (event == 0 && docked)
1182 acpi_bus_generate_event(ibm->device, event, 3); /* dock */
1183 else {
1184 printk(IBM_ERR "unknown dock event %d, status %d\n",
1185 event, _sta(dock_handle));
1186 acpi_bus_generate_event(ibm->device, event, 0); /* unknown */
1187 }
1188}
1189
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001190static int dock_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191{
1192 int len = 0;
1193 int docked = dock_docked();
1194
1195 if (!dock_handle)
1196 len += sprintf(p + len, "status:\t\tnot supported\n");
1197 else if (!docked)
1198 len += sprintf(p + len, "status:\t\tundocked\n");
1199 else {
1200 len += sprintf(p + len, "status:\t\tdocked\n");
1201 len += sprintf(p + len, "commands:\tdock, undock\n");
1202 }
1203
1204 return len;
1205}
1206
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001207static int dock_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208{
1209 char *cmd;
1210
1211 if (!dock_docked())
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001212 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
1214 while ((cmd = next_cmd(&buf))) {
1215 if (strlencmp(cmd, "undock") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001216 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
1217 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 return -EIO;
1219 } else if (strlencmp(cmd, "dock") == 0) {
1220 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
1221 return -EIO;
1222 } else
1223 return -EINVAL;
1224 }
1225
1226 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001227}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001229static struct ibm_struct dock_driver_data[2] = {
1230 {
1231 .name = "dock",
1232 .read = dock_read,
1233 .write = dock_write,
1234 .notify = dock_notify,
1235 .handle = &dock_handle,
1236 .type = ACPI_SYSTEM_NOTIFY,
1237 },
1238 {
1239 .name = "dock",
1240 .hid = IBM_PCI_HID,
1241 .notify = dock_notify,
1242 .handle = &pci_handle,
1243 .type = ACPI_SYSTEM_NOTIFY,
1244 },
1245};
1246
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03001247#endif /* CONFIG_THINKPAD_ACPI_DOCK */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001249/*************************************************************************
1250 * Bay subdriver
1251 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03001253#ifdef CONFIG_THINKPAD_ACPI_BAY
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001254static int bay_status_supported;
1255static int bay_status2_supported;
1256static int bay_eject_supported;
1257static int bay_eject2_supported;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001259IBM_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
1260 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
1261 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
1262 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
1263 ); /* A21e, R30, R31 */
1264IBM_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
1265 "_EJ0", /* all others */
1266 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
1267IBM_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
1268 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
1269 ); /* all others */
1270IBM_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
1271 "_EJ0", /* 770x */
1272 ); /* all others */
1273
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001274static int __init bay_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001276 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
1277
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001278 IBM_HANDLE_INIT(bay);
1279 if (bay_handle)
1280 IBM_HANDLE_INIT(bay_ej);
1281 IBM_HANDLE_INIT(bay2);
1282 if (bay2_handle)
1283 IBM_HANDLE_INIT(bay2_ej);
1284
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001285 bay_status_supported = bay_handle &&
1286 acpi_evalf(bay_handle, NULL, "_STA", "qv");
1287 bay_status2_supported = bay2_handle &&
1288 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
1289
1290 bay_eject_supported = bay_handle && bay_ej_handle &&
1291 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
1292 bay_eject2_supported = bay2_handle && bay2_ej_handle &&
1293 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001295 vdbg_printk(TPACPI_DBG_INIT,
1296 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
1297 str_supported(bay_status_supported),
1298 str_supported(bay_eject_supported),
1299 str_supported(bay_status2_supported),
1300 str_supported(bay_eject2_supported));
1301
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001302 return (bay_status_supported || bay_eject_supported ||
1303 bay_status2_supported || bay_eject2_supported)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304}
1305
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001306static void bay_notify(struct ibm_struct *ibm, u32 event)
1307{
1308 acpi_bus_generate_event(ibm->device, event, 0);
1309}
1310
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001311#define bay_occupied(b) (_sta(b##_handle) & 1)
1312
1313static int bay_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314{
1315 int len = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001316 int occupied = bay_occupied(bay);
1317 int occupied2 = bay_occupied(bay2);
1318 int eject, eject2;
1319
1320 len += sprintf(p + len, "status:\t\t%s\n", bay_status_supported ?
1321 (occupied ? "occupied" : "unoccupied") :
1322 "not supported");
1323 if (bay_status2_supported)
1324 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
1325 "occupied" : "unoccupied");
1326
1327 eject = bay_eject_supported && occupied;
1328 eject2 = bay_eject2_supported && occupied2;
1329
1330 if (eject && eject2)
1331 len += sprintf(p + len, "commands:\teject, eject2\n");
1332 else if (eject)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 len += sprintf(p + len, "commands:\teject\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001334 else if (eject2)
1335 len += sprintf(p + len, "commands:\teject2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
1337 return len;
1338}
1339
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001340static int bay_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341{
1342 char *cmd;
1343
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001344 if (!bay_eject_supported && !bay_eject2_supported)
1345 return -ENODEV;
1346
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001348 if (bay_eject_supported && strlencmp(cmd, "eject") == 0) {
1349 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
1350 return -EIO;
1351 } else if (bay_eject2_supported &&
1352 strlencmp(cmd, "eject2") == 0) {
1353 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 return -EIO;
1355 } else
1356 return -EINVAL;
1357 }
1358
1359 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001360}
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001361
1362static struct ibm_struct bay_driver_data = {
1363 .name = "bay",
1364 .read = bay_read,
1365 .write = bay_write,
1366 .notify = bay_notify,
1367 .handle = &bay_handle,
1368 .type = ACPI_SYSTEM_NOTIFY,
1369};
1370
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03001371#endif /* CONFIG_THINKPAD_ACPI_BAY */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001373/*************************************************************************
1374 * CMOS subdriver
1375 */
1376
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001377static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001378{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001379 vdbg_printk(TPACPI_DBG_INIT,
1380 "initializing cmos commands subdriver\n");
1381
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001382 IBM_HANDLE_INIT(cmos);
1383
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001384 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
1385 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001386 return (cmos_handle)? 0 : 1;
1387}
1388
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001389static int cmos_eval(int cmos_cmd)
1390{
1391 if (cmos_handle)
1392 return acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd);
1393 else
1394 return 1;
1395}
1396
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001397static int cmos_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398{
1399 int len = 0;
1400
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001401 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1402 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 if (!cmos_handle)
1404 len += sprintf(p + len, "status:\t\tnot supported\n");
1405 else {
1406 len += sprintf(p + len, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001407 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 }
1409
1410 return len;
1411}
1412
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001413static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414{
1415 char *cmd;
1416 int cmos_cmd;
1417
1418 if (!cmos_handle)
1419 return -EINVAL;
1420
1421 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001422 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
1423 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 /* cmos_cmd set */
1425 } else
1426 return -EINVAL;
1427
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001428 if (!cmos_eval(cmos_cmd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 return -EIO;
1430 }
1431
1432 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001433}
1434
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001435static struct ibm_struct cmos_driver_data = {
1436 .name = "cmos",
1437 .read = cmos_read,
1438 .write = cmos_write,
1439};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001440
1441/*************************************************************************
1442 * LED subdriver
1443 */
1444
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02001445static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001446
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001447IBM_HANDLE(led, ec, "SLED", /* 570 */
1448 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
1449 "LED", /* all others */
1450 ); /* R30, R31 */
1451
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001452static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001453{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001454 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
1455
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001456 IBM_HANDLE_INIT(led);
1457
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001458 if (!led_handle)
1459 /* led not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001460 led_supported = TPACPI_LED_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001461 else if (strlencmp(led_path, "SLED") == 0)
1462 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001463 led_supported = TPACPI_LED_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001464 else if (strlencmp(led_path, "SYSL") == 0)
1465 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001466 led_supported = TPACPI_LED_OLD;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001467 else
1468 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001469 led_supported = TPACPI_LED_NEW;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001470
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001471 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
1472 str_supported(led_supported), led_supported);
1473
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001474 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001475}
1476
1477#define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
1478
1479static int led_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480{
1481 int len = 0;
1482
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001483 if (!led_supported) {
1484 len += sprintf(p + len, "status:\t\tnot supported\n");
1485 return len;
1486 }
1487 len += sprintf(p + len, "status:\t\tsupported\n");
1488
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001489 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001490 /* 570 */
1491 int i, status;
1492 for (i = 0; i < 8; i++) {
1493 if (!acpi_evalf(ec_handle,
1494 &status, "GLED", "dd", 1 << i))
1495 return -EIO;
1496 len += sprintf(p + len, "%d:\t\t%s\n",
1497 i, led_status(status));
1498 }
1499 }
1500
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 len += sprintf(p + len, "commands:\t"
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001502 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
1504 return len;
1505}
1506
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001507/* off, on, blink */
1508static const int led_sled_arg1[] = { 0, 1, 3 };
1509static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
1510static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
1511static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
1512
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001513static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514{
1515 char *cmd;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001516 int led, ind, ret;
1517
1518 if (!led_supported)
1519 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
1521 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001522 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 return -EINVAL;
1524
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001525 if (strstr(cmd, "off")) {
1526 ind = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 } else if (strstr(cmd, "on")) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001528 ind = 1;
1529 } else if (strstr(cmd, "blink")) {
1530 ind = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 } else
1532 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001533
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001534 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001535 /* 570 */
1536 led = 1 << led;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001538 led, led_sled_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 return -EIO;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001540 } else if (led_supported == TPACPI_LED_OLD) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001541 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
1542 led = 1 << led;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001543 ret = ec_write(TPACPI_LED_EC_HLMS, led);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001544 if (ret >= 0)
1545 ret =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001546 ec_write(TPACPI_LED_EC_HLBL,
Henrique de Moraes Holschuhe062e032007-03-23 17:33:55 -03001547 led * led_exp_hlbl[ind]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001548 if (ret >= 0)
1549 ret =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001550 ec_write(TPACPI_LED_EC_HLCL,
Henrique de Moraes Holschuhe062e032007-03-23 17:33:55 -03001551 led * led_exp_hlcl[ind]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001552 if (ret < 0)
1553 return ret;
1554 } else {
1555 /* all others */
1556 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
1557 led, led_led_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001559 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 }
1561
1562 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001563}
1564
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001565static struct ibm_struct led_driver_data = {
1566 .name = "led",
1567 .read = led_read,
1568 .write = led_write,
1569};
1570
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001571/*************************************************************************
1572 * Beep subdriver
1573 */
1574
1575IBM_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
1576
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001577static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001578{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001579 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
1580
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001581 IBM_HANDLE_INIT(beep);
1582
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001583 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
1584 str_supported(beep_handle != NULL));
1585
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001586 return (beep_handle)? 0 : 1;
1587}
1588
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001589static int beep_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590{
1591 int len = 0;
1592
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001593 if (!beep_handle)
1594 len += sprintf(p + len, "status:\t\tnot supported\n");
1595 else {
1596 len += sprintf(p + len, "status:\t\tsupported\n");
1597 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
1598 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
1600 return len;
1601}
1602
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001603static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604{
1605 char *cmd;
1606 int beep_cmd;
1607
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001608 if (!beep_handle)
1609 return -ENODEV;
1610
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001612 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
1613 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 /* beep_cmd set */
1615 } else
1616 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001617 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 return -EIO;
1619 }
1620
1621 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001622}
1623
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001624static struct ibm_struct beep_driver_data = {
1625 .name = "beep",
1626 .read = beep_read,
1627 .write = beep_write,
1628};
1629
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001630/*************************************************************************
1631 * Thermal subdriver
1632 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001633
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001634static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001635
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001636static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001637{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001638 u8 t, ta1, ta2;
1639 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001640 int acpi_tmp7;
1641
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001642 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
1643
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001644 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001645
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001646 if (ibm_thinkpad_ec_found && experimental) {
1647 /*
1648 * Direct EC access mode: sensors at registers
1649 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
1650 * non-implemented, thermal sensors return 0x80 when
1651 * not available
1652 */
1653
1654 ta1 = ta2 = 0;
1655 for (i = 0; i < 8; i++) {
1656 if (likely(acpi_ec_read(0x78 + i, &t))) {
1657 ta1 |= t;
1658 } else {
1659 ta1 = 0;
1660 break;
1661 }
1662 if (likely(acpi_ec_read(0xC0 + i, &t))) {
1663 ta2 |= t;
1664 } else {
1665 ta1 = 0;
1666 break;
1667 }
1668 }
1669 if (ta1 == 0) {
1670 /* This is sheer paranoia, but we handle it anyway */
1671 if (acpi_tmp7) {
1672 printk(IBM_ERR
1673 "ThinkPad ACPI EC access misbehaving, "
1674 "falling back to ACPI TMPx access mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001675 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001676 } else {
1677 printk(IBM_ERR
1678 "ThinkPad ACPI EC access misbehaving, "
1679 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001680 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001681 }
1682 } else {
1683 thermal_read_mode =
1684 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001685 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001686 }
1687 } else if (acpi_tmp7) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001688 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
1689 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001690 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001691 } else {
1692 /* Standard ACPI TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001693 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001694 }
1695 } else {
1696 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001697 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001698 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001699
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001700 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
1701 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
1702 thermal_read_mode);
1703
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001704 return (thermal_read_mode != TPACPI_THERMAL_NONE)? 0 : 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001705}
1706
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001707static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
1708{
1709 int i, t;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001710 s8 tmp;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001711 char tmpi[] = "TMPi";
1712
1713 if (!s)
1714 return -EINVAL;
1715
1716 switch (thermal_read_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001717#if TPACPI_MAX_THERMAL_SENSORS >= 16
1718 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001719 for (i = 0; i < 8; i++) {
1720 if (!acpi_ec_read(0xC0 + i, &tmp))
1721 return -EIO;
1722 s->temp[i + 8] = tmp * 1000;
1723 }
1724 /* fallthrough */
1725#endif
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001726 case TPACPI_THERMAL_TPEC_8:
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001727 for (i = 0; i < 8; i++) {
1728 if (!acpi_ec_read(0x78 + i, &tmp))
1729 return -EIO;
1730 s->temp[i] = tmp * 1000;
1731 }
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001732 return (thermal_read_mode == TPACPI_THERMAL_TPEC_16) ? 16 : 8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001733
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001734 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001735 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
1736 return -EIO;
1737 for (i = 0; i < 8; i++) {
1738 tmpi[3] = '0' + i;
1739 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
1740 return -EIO;
1741 s->temp[i] = (t - 2732) * 100;
1742 }
1743 return 8;
1744
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001745 case TPACPI_THERMAL_ACPI_TMP07:
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001746 for (i = 0; i < 8; i++) {
1747 tmpi[3] = '0' + i;
1748 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
1749 return -EIO;
1750 s->temp[i] = t * 1000;
1751 }
1752 return 8;
1753
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001754 case TPACPI_THERMAL_NONE:
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001755 default:
1756 return 0;
1757 }
1758}
1759
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001760static int thermal_read(char *p)
1761{
1762 int len = 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001763 int n, i;
1764 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001765
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001766 n = thermal_get_sensors(&t);
1767 if (unlikely(n < 0))
1768 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001769
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001770 len += sprintf(p + len, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001771
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001772 if (n > 0) {
1773 for (i = 0; i < (n - 1); i++)
1774 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
1775 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
1776 } else
1777 len += sprintf(p + len, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001778
1779 return len;
1780}
1781
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001782static struct ibm_struct thermal_driver_data = {
1783 .name = "thermal",
1784 .read = thermal_read,
1785};
1786
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001787/*************************************************************************
1788 * EC Dump subdriver
1789 */
1790
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001791static u8 ecdump_regs[256];
1792
1793static int ecdump_read(char *p)
1794{
1795 int len = 0;
1796 int i, j;
1797 u8 v;
1798
1799 len += sprintf(p + len, "EC "
1800 " +00 +01 +02 +03 +04 +05 +06 +07"
1801 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
1802 for (i = 0; i < 256; i += 16) {
1803 len += sprintf(p + len, "EC 0x%02x:", i);
1804 for (j = 0; j < 16; j++) {
1805 if (!acpi_ec_read(i + j, &v))
1806 break;
1807 if (v != ecdump_regs[i + j])
1808 len += sprintf(p + len, " *%02x", v);
1809 else
1810 len += sprintf(p + len, " %02x", v);
1811 ecdump_regs[i + j] = v;
1812 }
1813 len += sprintf(p + len, "\n");
1814 if (j != 16)
1815 break;
1816 }
1817
1818 /* These are way too dangerous to advertise openly... */
1819#if 0
1820 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
1821 " (<offset> is 00-ff, <value> is 00-ff)\n");
1822 len += sprintf(p + len, "commands:\t0x<offset> <value> "
1823 " (<offset> is 00-ff, <value> is 0-255)\n");
1824#endif
1825 return len;
1826}
1827
1828static int ecdump_write(char *buf)
1829{
1830 char *cmd;
1831 int i, v;
1832
1833 while ((cmd = next_cmd(&buf))) {
1834 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
1835 /* i and v set */
1836 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
1837 /* i and v set */
1838 } else
1839 return -EINVAL;
1840 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
1841 if (!acpi_ec_write(i, v))
1842 return -EIO;
1843 } else
1844 return -EINVAL;
1845 }
1846
1847 return 0;
1848}
1849
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001850static struct ibm_struct ecdump_driver_data = {
1851 .name = "ecdump",
1852 .read = ecdump_read,
1853 .write = ecdump_write,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03001854 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001855};
1856
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001857/*************************************************************************
1858 * Backlight/brightness subdriver
1859 */
Holger Macht8acb0252006-10-20 14:30:28 -07001860
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001861static struct backlight_device *ibm_backlight_device = NULL;
Holger Macht8acb0252006-10-20 14:30:28 -07001862
Richard Purdie599a52d2007-02-10 23:07:48 +00001863static struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02001864 .get_brightness = brightness_get,
1865 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02001866};
1867
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001868static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02001869{
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02001870 int b;
1871
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001872 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
1873
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02001874 b = brightness_get(NULL);
1875 if (b < 0)
1876 return b;
1877
Yu Luming519ab5f2006-12-19 12:56:15 -08001878 ibm_backlight_device = backlight_device_register("ibm", NULL, NULL,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02001879 &ibm_backlight_data);
1880 if (IS_ERR(ibm_backlight_device)) {
1881 printk(IBM_ERR "Could not register backlight device\n");
1882 return PTR_ERR(ibm_backlight_device);
1883 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001884 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02001885
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02001886 ibm_backlight_device->props.max_brightness = 7;
1887 ibm_backlight_device->props.brightness = b;
1888 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00001889
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02001890 return 0;
1891}
1892
1893static void brightness_exit(void)
1894{
1895 if (ibm_backlight_device) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001896 vdbg_printk(TPACPI_DBG_EXIT,
1897 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02001898 backlight_device_unregister(ibm_backlight_device);
1899 ibm_backlight_device = NULL;
1900 }
1901}
1902
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001903static int brightness_update_status(struct backlight_device *bd)
1904{
1905 return brightness_set(
1906 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
1907 bd->props.power == FB_BLANK_UNBLANK) ?
1908 bd->props.brightness : 0);
1909}
1910
1911static int brightness_get(struct backlight_device *bd)
1912{
1913 u8 level;
1914 if (!acpi_ec_read(brightness_offset, &level))
1915 return -EIO;
1916
1917 level &= 0x7;
1918
1919 return level;
1920}
1921
1922static int brightness_set(int value)
1923{
1924 int cmos_cmd, inc, i;
1925 int current_value = brightness_get(NULL);
1926
1927 value &= 7;
1928
1929 cmos_cmd = value > current_value ? TP_CMOS_BRIGHTNESS_UP : TP_CMOS_BRIGHTNESS_DOWN;
1930 inc = value > current_value ? 1 : -1;
1931 for (i = current_value; i != value; i += inc) {
1932 if (!cmos_eval(cmos_cmd))
1933 return -EIO;
1934 if (!acpi_ec_write(brightness_offset, i + inc))
1935 return -EIO;
1936 }
1937
1938 return 0;
1939}
1940
1941static int brightness_read(char *p)
1942{
1943 int len = 0;
1944 int level;
1945
1946 if ((level = brightness_get(NULL)) < 0) {
1947 len += sprintf(p + len, "level:\t\tunreadable\n");
1948 } else {
1949 len += sprintf(p + len, "level:\t\t%d\n", level & 0x7);
1950 len += sprintf(p + len, "commands:\tup, down\n");
1951 len += sprintf(p + len, "commands:\tlevel <level>"
1952 " (<level> is 0-7)\n");
1953 }
1954
1955 return len;
1956}
1957
1958static int brightness_write(char *buf)
1959{
1960 int level;
1961 int new_level;
1962 char *cmd;
1963
1964 while ((cmd = next_cmd(&buf))) {
1965 if ((level = brightness_get(NULL)) < 0)
1966 return level;
1967 level &= 7;
1968
1969 if (strlencmp(cmd, "up") == 0) {
1970 new_level = level == 7 ? 7 : level + 1;
1971 } else if (strlencmp(cmd, "down") == 0) {
1972 new_level = level == 0 ? 0 : level - 1;
1973 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
1974 new_level >= 0 && new_level <= 7) {
1975 /* new_level set */
1976 } else
1977 return -EINVAL;
1978
1979 brightness_set(new_level);
1980 }
1981
1982 return 0;
1983}
1984
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001985static struct ibm_struct brightness_driver_data = {
1986 .name = "brightness",
1987 .read = brightness_read,
1988 .write = brightness_write,
1989 .exit = brightness_exit,
1990};
1991
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001992/*************************************************************************
1993 * Volume subdriver
1994 */
1995
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001996static int volume_read(char *p)
1997{
1998 int len = 0;
1999 u8 level;
2000
2001 if (!acpi_ec_read(volume_offset, &level)) {
2002 len += sprintf(p + len, "level:\t\tunreadable\n");
2003 } else {
2004 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
2005 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
2006 len += sprintf(p + len, "commands:\tup, down, mute\n");
2007 len += sprintf(p + len, "commands:\tlevel <level>"
2008 " (<level> is 0-15)\n");
2009 }
2010
2011 return len;
2012}
2013
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002014static int volume_write(char *buf)
2015{
2016 int cmos_cmd, inc, i;
2017 u8 level, mute;
2018 int new_level, new_mute;
2019 char *cmd;
2020
2021 while ((cmd = next_cmd(&buf))) {
2022 if (!acpi_ec_read(volume_offset, &level))
2023 return -EIO;
2024 new_mute = mute = level & 0x40;
2025 new_level = level = level & 0xf;
2026
2027 if (strlencmp(cmd, "up") == 0) {
2028 if (mute)
2029 new_mute = 0;
2030 else
2031 new_level = level == 15 ? 15 : level + 1;
2032 } else if (strlencmp(cmd, "down") == 0) {
2033 if (mute)
2034 new_mute = 0;
2035 else
2036 new_level = level == 0 ? 0 : level - 1;
2037 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
2038 new_level >= 0 && new_level <= 15) {
2039 /* new_level set */
2040 } else if (strlencmp(cmd, "mute") == 0) {
2041 new_mute = 0x40;
2042 } else
2043 return -EINVAL;
2044
2045 if (new_level != level) { /* mute doesn't change */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002046 cmos_cmd = new_level > level ? TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002047 inc = new_level > level ? 1 : -1;
2048
2049 if (mute && (!cmos_eval(cmos_cmd) ||
2050 !acpi_ec_write(volume_offset, level)))
2051 return -EIO;
2052
2053 for (i = level; i != new_level; i += inc)
2054 if (!cmos_eval(cmos_cmd) ||
2055 !acpi_ec_write(volume_offset, i + inc))
2056 return -EIO;
2057
Henrique de Moraes Holschuhe062e032007-03-23 17:33:55 -03002058 if (mute && (!cmos_eval(TP_CMOS_VOLUME_MUTE) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002059 !acpi_ec_write(volume_offset,
2060 new_level + mute)))
2061 return -EIO;
2062 }
2063
2064 if (new_mute != mute) { /* level doesn't change */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002065 cmos_cmd = new_mute ? TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002066
2067 if (!cmos_eval(cmos_cmd) ||
2068 !acpi_ec_write(volume_offset, level + new_mute))
2069 return -EIO;
2070 }
2071 }
2072
2073 return 0;
2074}
2075
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002076static struct ibm_struct volume_driver_data = {
2077 .name = "volume",
2078 .read = volume_read,
2079 .write = volume_write,
2080};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002081
2082/*************************************************************************
2083 * Fan subdriver
2084 */
2085
2086/*
2087 * FAN ACCESS MODES
2088 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002089 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002090 * ACPI GFAN method: returns fan level
2091 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002092 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03002093 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002094 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002095 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002096 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
2097 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03002098 * EC 0x2f (HFSP) might be available *for reading*, but do not use
2099 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002100 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002101 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03002102 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
2103 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002104 *
2105 * Fan speed changes of any sort (including those caused by the
2106 * disengaged mode) are usually done slowly by the firmware as the
2107 * maximum ammount of fan duty cycle change per second seems to be
2108 * limited.
2109 *
2110 * Reading is not available if GFAN exists.
2111 * Writing is not available if SFAN exists.
2112 *
2113 * Bits
2114 * 7 automatic mode engaged;
2115 * (default operation mode of the ThinkPad)
2116 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03002117 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002118 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03002119 * the tachometer while the fan controller ramps up
2120 * the speed (which can take up to a few *minutes*).
2121 * Speeds up fan to 100% duty-cycle, which is far above
2122 * the standard RPM levels. It is not impossible that
2123 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002124 * 5-3 unused in some models. Extra bits for fan level
2125 * in others, but still useless as all values above
2126 * 7 map to the same speed as level 7 in these models.
2127 * 2-0 fan level (0..7 usually)
2128 * 0x00 = stop
2129 * 0x07 = max (set when temperatures critical)
2130 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002131 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002132 *
2133 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
2134 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
2135 * does so, its initial value is meaningless (0x07).
2136 *
2137 * For firmware bugs, refer to:
2138 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
2139 *
2140 * ----
2141 *
2142 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
2143 * Main fan tachometer reading (in RPM)
2144 *
2145 * This register is present on all ThinkPads with a new-style EC, and
2146 * it is known not to be present on the A21m/e, and T22, as there is
2147 * something else in offset 0x84 according to the ACPI DSDT. Other
2148 * ThinkPads from this same time period (and earlier) probably lack the
2149 * tachometer as well.
2150 *
2151 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
2152 * was never fixed by IBM to report the EC firmware version string
2153 * probably support the tachometer (like the early X models), so
2154 * detecting it is quite hard. We need more data to know for sure.
2155 *
2156 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
2157 * might result.
2158 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03002159 * FIRMWARE BUG: may go stale while the EC is switching to full speed
2160 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002161 *
2162 * For firmware bugs, refer to:
2163 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
2164 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002165 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002166 * ThinkPad X31, X40, X41. Not available in the X60.
2167 *
2168 * FANS ACPI handle: takes three arguments: low speed, medium speed,
2169 * high speed. ACPI DSDT seems to map these three speeds to levels
2170 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
2171 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
2172 *
2173 * The speeds are stored on handles
2174 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
2175 *
2176 * There are three default speed sets, acessible as handles:
2177 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
2178 *
2179 * ACPI DSDT switches which set is in use depending on various
2180 * factors.
2181 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002182 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002183 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
2184 * but the ACPI tables just mention level 7.
2185 */
2186
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002187static enum fan_status_access_mode fan_status_access_mode;
2188static enum fan_control_access_mode fan_control_access_mode;
2189static enum fan_control_commands fan_control_commands;
2190
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002191static int fan_control_status_known;
2192static u8 fan_control_initial_status;
2193
Len Brown25c68a32006-12-08 04:43:41 -05002194static void fan_watchdog_fire(struct work_struct *ignored);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002195static int fan_watchdog_maxinterval;
Len Brown25c68a32006-12-08 04:43:41 -05002196static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002197
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002198IBM_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
2199IBM_HANDLE(gfan, ec, "GFAN", /* 570 */
2200 "\\FSPD", /* 600e/x, 770e, 770x */
2201 ); /* all others */
2202IBM_HANDLE(sfan, ec, "SFAN", /* 570 */
2203 "JFNS", /* 770x-JL */
2204 ); /* all others */
2205
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002206static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002207{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002208 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
2209
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002210 fan_status_access_mode = TPACPI_FAN_NONE;
2211 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002212 fan_control_commands = 0;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002213 fan_control_status_known = 1;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002214 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002215
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002216 IBM_HANDLE_INIT(fans);
2217 IBM_HANDLE_INIT(gfan);
2218 IBM_HANDLE_INIT(sfan);
2219
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002220 if (gfan_handle) {
2221 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002222 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002223 } else {
2224 /* all other ThinkPads: note that even old-style
2225 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002226 if (likely(acpi_ec_read(fan_status_offset,
2227 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002228 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002229
2230 /* In some ThinkPads, neither the EC nor the ACPI
2231 * DSDT initialize the fan status, and it ends up
2232 * being set to 0x07 when it *could* be either
2233 * 0x07 or 0x80.
2234 *
2235 * Enable for TP-1Y (T43), TP-78 (R51e),
2236 * TP-76 (R52), TP-70 (T43, R52), which are known
2237 * to be buggy. */
2238 if (fan_control_initial_status == 0x07 &&
2239 ibm_thinkpad_ec_found &&
2240 ((ibm_thinkpad_ec_found[0] == '1' &&
2241 ibm_thinkpad_ec_found[1] == 'Y') ||
2242 (ibm_thinkpad_ec_found[0] == '7' &&
2243 (ibm_thinkpad_ec_found[1] == '6' ||
2244 ibm_thinkpad_ec_found[1] == '8' ||
2245 ibm_thinkpad_ec_found[1] == '0'))
2246 )) {
2247 printk(IBM_NOTICE
2248 "fan_init: initial fan status is "
2249 "unknown, assuming it is in auto "
2250 "mode\n");
2251 fan_control_status_known = 0;
2252 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002253 } else {
2254 printk(IBM_ERR
2255 "ThinkPad ACPI EC access misbehaving, "
2256 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002257 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002258 }
2259 }
2260
2261 if (sfan_handle) {
2262 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002263 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002264 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002265 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002266 } else {
2267 if (!gfan_handle) {
2268 /* gfan without sfan means no fan control */
2269 /* all other models implement TP EC 0x2f control */
2270
2271 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02002272 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002273 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002274 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002275 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002276 TPACPI_FAN_CMD_SPEED |
2277 TPACPI_FAN_CMD_LEVEL |
2278 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002279 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002280 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02002281 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002282 TPACPI_FAN_CMD_LEVEL |
2283 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002284 }
2285 }
2286 }
2287
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002288 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
2289 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
2290 fan_control_access_mode != TPACPI_FAN_WR_NONE),
2291 fan_status_access_mode, fan_control_access_mode);
2292
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002293 return (fan_status_access_mode != TPACPI_FAN_NONE ||
2294 fan_control_access_mode != TPACPI_FAN_WR_NONE)?
2295 0 : 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002296}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002297
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02002298static int fan_get_status(u8 *status)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002299{
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02002300 u8 s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002301
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02002302 /* TODO:
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002303 * Add TPACPI_FAN_RD_ACPI_FANS ? */
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02002304
Henrique de Moraes Holschuh3ef8a602006-11-24 11:47:10 -02002305 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002306 case TPACPI_FAN_RD_ACPI_GFAN:
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002307 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02002308
2309 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002310 return -EIO;
2311
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02002312 if (likely(status))
2313 *status = s & 0x07;
2314
2315 break;
2316
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002317 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02002318 /* all except 570, 600e/x, 770e, 770x */
2319 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
2320 return -EIO;
2321
2322 if (likely(status))
2323 *status = s;
2324
2325 break;
2326
2327 default:
2328 return -ENXIO;
2329 }
2330
2331 return 0;
2332}
2333
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002334static void fan_exit(void)
2335{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002336 vdbg_printk(TPACPI_DBG_EXIT, "cancelling any pending watchdogs\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002337 cancel_delayed_work(&fan_watchdog_task);
2338 flush_scheduled_work();
2339}
2340
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02002341static int fan_get_speed(unsigned int *speed)
2342{
2343 u8 hi, lo;
2344
2345 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002346 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02002347 /* all except 570, 600e/x, 770e, 770x */
2348 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
2349 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
2350 return -EIO;
2351
2352 if (likely(speed))
2353 *speed = (hi << 8) | lo;
2354
2355 break;
2356
2357 default:
2358 return -ENXIO;
2359 }
2360
2361 return 0;
2362}
2363
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002364static void fan_watchdog_fire(struct work_struct *ignored)
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002365{
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002366 printk(IBM_NOTICE "fan watchdog: enabling fan\n");
2367 if (fan_set_enable()) {
2368 printk(IBM_ERR "fan watchdog: error while enabling fan\n");
2369 /* reschedule for later */
2370 fan_watchdog_reset();
2371 }
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002372}
2373
2374static void fan_watchdog_reset(void)
2375{
2376 static int fan_watchdog_active = 0;
2377
2378 if (fan_watchdog_active)
2379 cancel_delayed_work(&fan_watchdog_task);
2380
2381 if (fan_watchdog_maxinterval > 0) {
2382 fan_watchdog_active = 1;
2383 if (!schedule_delayed_work(&fan_watchdog_task,
2384 msecs_to_jiffies(fan_watchdog_maxinterval
2385 * 1000))) {
2386 printk(IBM_ERR "failed to schedule the fan watchdog, "
2387 "watchdog will not trigger\n");
2388 }
2389 } else
2390 fan_watchdog_active = 0;
2391}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002392
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002393static int fan_set_level(int level)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002394{
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002395 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002396 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002397 if (level >= 0 && level <= 7) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002398 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
2399 return -EIO;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002400 } else
2401 return -EINVAL;
2402 break;
2403
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002404 case TPACPI_FAN_WR_ACPI_FANS:
2405 case TPACPI_FAN_WR_TPEC:
2406 if ((level != TP_EC_FAN_AUTO) &&
2407 (level != TP_EC_FAN_FULLSPEED) &&
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02002408 ((level < 0) || (level > 7)))
2409 return -EINVAL;
2410
2411 if (!acpi_ec_write(fan_status_offset, level))
2412 return -EIO;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002413 else
2414 fan_control_status_known = 1;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02002415 break;
2416
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002417 default:
2418 return -ENXIO;
2419 }
2420 return 0;
2421}
2422
2423static int fan_set_enable(void)
2424{
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002425 u8 s;
2426 int rc;
2427
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002428 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002429 case TPACPI_FAN_WR_ACPI_FANS:
2430 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002431 if ((rc = fan_get_status(&s)) < 0)
2432 return rc;
2433
2434 /* Don't go out of emergency fan mode */
2435 if (s != 7)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002436 s = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002437
2438 if (!acpi_ec_write(fan_status_offset, s))
2439 return -EIO;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002440 else
2441 fan_control_status_known = 1;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002442 break;
2443
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002444 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002445 if ((rc = fan_get_status(&s)) < 0)
2446 return rc;
2447
2448 s &= 0x07;
2449
2450 /* Set fan to at least level 4 */
2451 if (s < 4)
2452 s = 4;
2453
2454 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002455 return -EIO;
2456 break;
2457
2458 default:
2459 return -ENXIO;
2460 }
2461 return 0;
2462}
2463
2464static int fan_set_disable(void)
2465{
2466 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002467 case TPACPI_FAN_WR_ACPI_FANS:
2468 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002469 if (!acpi_ec_write(fan_status_offset, 0x00))
2470 return -EIO;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002471 else
2472 fan_control_status_known = 1;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002473 break;
2474
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002475 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002476 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
2477 return -EIO;
2478 break;
2479
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002480 default:
2481 return -ENXIO;
2482 }
2483 return 0;
2484}
2485
2486static int fan_set_speed(int speed)
2487{
2488 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002489 case TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002490 if (speed >= 0 && speed <= 65535) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002491 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
2492 speed, speed, speed))
2493 return -EIO;
2494 } else
2495 return -EINVAL;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002496 break;
2497
2498 default:
2499 return -ENXIO;
2500 }
2501 return 0;
2502}
2503
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002504static int fan_read(char *p)
2505{
2506 int len = 0;
2507 int rc;
2508 u8 status;
2509 unsigned int speed = 0;
2510
2511 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002512 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002513 /* 570, 600e/x, 770e, 770x */
2514 if ((rc = fan_get_status(&status)) < 0)
2515 return rc;
2516
2517 len += sprintf(p + len, "status:\t\t%s\n"
2518 "level:\t\t%d\n",
2519 (status != 0) ? "enabled" : "disabled", status);
2520 break;
2521
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002522 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002523 /* all except 570, 600e/x, 770e, 770x */
2524 if ((rc = fan_get_status(&status)) < 0)
2525 return rc;
2526
2527 if (unlikely(!fan_control_status_known)) {
2528 if (status != fan_control_initial_status)
2529 fan_control_status_known = 1;
2530 else
2531 /* Return most likely status. In fact, it
2532 * might be the only possible status */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002533 status = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002534 }
2535
2536 len += sprintf(p + len, "status:\t\t%s\n",
2537 (status != 0) ? "enabled" : "disabled");
2538
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002539 if ((rc = fan_get_speed(&speed)) < 0)
2540 return rc;
2541
2542 len += sprintf(p + len, "speed:\t\t%d\n", speed);
2543
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002544 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002545 /* Disengaged mode takes precedence */
2546 len += sprintf(p + len, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002547 else if (status & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002548 len += sprintf(p + len, "level:\t\tauto\n");
2549 else
2550 len += sprintf(p + len, "level:\t\t%d\n", status);
2551 break;
2552
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002553 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002554 default:
2555 len += sprintf(p + len, "status:\t\tnot supported\n");
2556 }
2557
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002558 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002559 len += sprintf(p + len, "commands:\tlevel <level>");
2560
2561 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002562 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002563 len += sprintf(p + len, " (<level> is 0-7)\n");
2564 break;
2565
2566 default:
2567 len += sprintf(p + len, " (<level> is 0-7, "
2568 "auto, disengaged)\n");
2569 break;
2570 }
2571 }
2572
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002573 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002574 len += sprintf(p + len, "commands:\tenable, disable\n"
2575 "commands:\twatchdog <timeout> (<timeout> is 0 (off), "
2576 "1-120 (seconds))\n");
2577
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002578 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002579 len += sprintf(p + len, "commands:\tspeed <speed>"
2580 " (<speed> is 0-65535)\n");
2581
2582 return len;
2583}
2584
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002585static int fan_write_cmd_level(const char *cmd, int *rc)
2586{
2587 int level;
2588
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02002589 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002590 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02002591 else if (strlencmp(cmd, "level disengaged") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002592 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02002593 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002594 return 0;
2595
2596 if ((*rc = fan_set_level(level)) == -ENXIO)
2597 printk(IBM_ERR "level command accepted for unsupported "
2598 "access mode %d", fan_control_access_mode);
2599
2600 return 1;
2601}
2602
2603static int fan_write_cmd_enable(const char *cmd, int *rc)
2604{
2605 if (strlencmp(cmd, "enable") != 0)
2606 return 0;
2607
2608 if ((*rc = fan_set_enable()) == -ENXIO)
2609 printk(IBM_ERR "enable command accepted for unsupported "
2610 "access mode %d", fan_control_access_mode);
2611
2612 return 1;
2613}
2614
2615static int fan_write_cmd_disable(const char *cmd, int *rc)
2616{
2617 if (strlencmp(cmd, "disable") != 0)
2618 return 0;
2619
2620 if ((*rc = fan_set_disable()) == -ENXIO)
2621 printk(IBM_ERR "disable command accepted for unsupported "
2622 "access mode %d", fan_control_access_mode);
2623
2624 return 1;
2625}
2626
2627static int fan_write_cmd_speed(const char *cmd, int *rc)
2628{
2629 int speed;
2630
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02002631 /* TODO:
2632 * Support speed <low> <medium> <high> ? */
2633
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002634 if (sscanf(cmd, "speed %d", &speed) != 1)
2635 return 0;
2636
2637 if ((*rc = fan_set_speed(speed)) == -ENXIO)
2638 printk(IBM_ERR "speed command accepted for unsupported "
2639 "access mode %d", fan_control_access_mode);
2640
2641 return 1;
2642}
2643
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002644static int fan_write_cmd_watchdog(const char *cmd, int *rc)
2645{
2646 int interval;
2647
2648 if (sscanf(cmd, "watchdog %d", &interval) != 1)
2649 return 0;
2650
2651 if (interval < 0 || interval > 120)
2652 *rc = -EINVAL;
2653 else
2654 fan_watchdog_maxinterval = interval;
2655
2656 return 1;
2657}
2658
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002659static int fan_write(char *buf)
2660{
2661 char *cmd;
2662 int rc = 0;
2663
2664 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002665 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002666 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002667 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002668 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002669 fan_write_cmd_disable(cmd, &rc) ||
2670 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002671 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002672 fan_write_cmd_speed(cmd, &rc))
2673 )
2674 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002675 else if (!rc)
2676 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002677 }
2678
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002679 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002680}
2681
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002682static struct ibm_struct fan_driver_data = {
2683 .name = "fan",
2684 .read = fan_read,
2685 .write = fan_write,
2686 .exit = fan_exit,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03002687 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002688};
2689
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002690/****************************************************************************
2691 ****************************************************************************
2692 *
2693 * Infrastructure
2694 *
2695 ****************************************************************************
2696 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002698/* /proc support */
2699static struct proc_dir_entry *proc_dir = NULL;
2700
2701/* Subdriver registry */
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002702static LIST_HEAD(tpacpi_all_drivers);
2703
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002705/*
2706 * Module and infrastructure proble, init and exit handling
2707 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002709#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002710static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002711{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002712 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002713
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002714 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002715}
2716#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
2717
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002718static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719{
2720 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002721 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 struct proc_dir_entry *entry;
2723
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002724 BUG_ON(ibm == NULL);
2725
2726 INIT_LIST_HEAD(&ibm->all_drivers);
2727
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03002728 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 return 0;
2730
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002731 dbg_printk(TPACPI_DBG_INIT,
2732 "probing for %s\n", ibm->name);
2733
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002734 if (iibm->init) {
2735 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002736 if (ret > 0)
2737 return 0; /* probe failed */
2738 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002740
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03002741 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 }
2743
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002744 if (ibm->hid) {
2745 ret = register_tpacpi_subdriver(ibm);
2746 if (ret)
2747 goto err_out;
2748 }
2749
2750 if (ibm->notify) {
2751 ret = setup_notify(ibm);
2752 if (ret == -ENODEV) {
2753 printk(IBM_NOTICE "disabling subdriver %s\n",
2754 ibm->name);
2755 ret = 0;
2756 goto err_out;
2757 }
2758 if (ret < 0)
2759 goto err_out;
2760 }
2761
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002762 dbg_printk(TPACPI_DBG_INIT,
2763 "%s installed\n", ibm->name);
2764
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002765 if (ibm->read) {
2766 entry = create_proc_entry(ibm->name,
2767 S_IFREG | S_IRUGO | S_IWUSR,
2768 proc_dir);
2769 if (!entry) {
2770 printk(IBM_ERR "unable to create proc entry %s\n",
2771 ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002772 ret = -ENODEV;
2773 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002774 }
2775 entry->owner = THIS_MODULE;
2776 entry->data = ibm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 entry->read_proc = &dispatch_read;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002778 if (ibm->write)
2779 entry->write_proc = &dispatch_write;
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03002780 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002781 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002783 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
2784
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002786
2787err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002788 dbg_printk(TPACPI_DBG_INIT,
2789 "%s: at error exit path with result %d\n",
2790 ibm->name, ret);
2791
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002792 ibm_exit(ibm);
2793 return (ret < 0)? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794}
2795
2796static void ibm_exit(struct ibm_struct *ibm)
2797{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002798 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002799
2800 list_del_init(&ibm->all_drivers);
2801
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03002802 if (ibm->flags.notify_installed) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002803 dbg_printk(TPACPI_DBG_EXIT,
2804 "%s: acpi_remove_notify_handler\n", ibm->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 acpi_remove_notify_handler(*ibm->handle, ibm->type,
2806 dispatch_notify);
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03002807 ibm->flags.notify_installed = 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002808 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03002810 if (ibm->flags.proc_created) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002811 dbg_printk(TPACPI_DBG_EXIT,
2812 "%s: remove_proc_entry\n", ibm->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 remove_proc_entry(ibm->name, proc_dir);
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03002814 ibm->flags.proc_created = 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002815 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03002817 if (ibm->flags.driver_registered) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002818 dbg_printk(TPACPI_DBG_EXIT,
2819 "%s: acpi_bus_unregister_driver\n", ibm->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 acpi_bus_unregister_driver(ibm->driver);
2821 kfree(ibm->driver);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002822 ibm->driver = NULL;
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03002823 ibm->flags.driver_registered = 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002824 }
2825
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03002826 if (ibm->flags.init_called && ibm->exit) {
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002827 ibm->exit();
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03002828 ibm->flags.init_called = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002830
2831 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832}
2833
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002834/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002836static char *ibm_thinkpad_ec_found = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02002838static char* __init check_dmi_for_ec(void)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002839{
2840 struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02002841 char ec_fw_string[18];
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002842
2843 /*
2844 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
2845 * X32 or newer, all Z series; Some models must have an
2846 * up-to-date BIOS or they will not be detected.
2847 *
2848 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
2849 */
2850 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02002851 if (sscanf(dev->name,
2852 "IBM ThinkPad Embedded Controller -[%17c",
2853 ec_fw_string) == 1) {
2854 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
2855 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
2856 return kstrdup(ec_fw_string, GFP_KERNEL);
2857 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002858 }
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02002859 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860}
2861
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002862static int __init probe_for_thinkpad(void)
2863{
2864 int is_thinkpad;
2865
2866 if (acpi_disabled)
2867 return -ENODEV;
2868
2869 /*
2870 * Non-ancient models have better DMI tagging, but very old models
2871 * don't.
2872 */
2873 is_thinkpad = dmi_name_in_vendors("ThinkPad");
2874
2875 /* ec is required because many other handles are relative to it */
2876 IBM_HANDLE_INIT(ec);
2877 if (!ec_handle) {
2878 if (is_thinkpad)
2879 printk(IBM_ERR
2880 "Not yet supported ThinkPad detected!\n");
2881 return -ENODEV;
2882 }
2883
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03002884 /*
2885 * Risks a regression on very old machines, but reduces potential
2886 * false positives a damn great deal
2887 */
2888 if (!is_thinkpad)
2889 is_thinkpad = dmi_name_in_vendors("IBM");
2890
2891 if (!is_thinkpad && !force_load)
2892 return -ENODEV;
2893
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002894 return 0;
2895}
2896
2897
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002898/* Module init, exit, parameters */
2899
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002900static struct ibm_init_struct ibms_init[] __initdata = {
2901 {
2902 .init = thinkpad_acpi_driver_init,
2903 .data = &thinkpad_acpi_driver_data,
2904 },
2905 {
2906 .init = hotkey_init,
2907 .data = &hotkey_driver_data,
2908 },
2909 {
2910 .init = bluetooth_init,
2911 .data = &bluetooth_driver_data,
2912 },
2913 {
2914 .init = wan_init,
2915 .data = &wan_driver_data,
2916 },
2917 {
2918 .init = video_init,
2919 .data = &video_driver_data,
2920 },
2921 {
2922 .init = light_init,
2923 .data = &light_driver_data,
2924 },
2925#ifdef CONFIG_THINKPAD_ACPI_DOCK
2926 {
2927 .init = dock_init,
2928 .data = &dock_driver_data[0],
2929 },
2930 {
2931 .data = &dock_driver_data[1],
2932 },
2933#endif
2934#ifdef CONFIG_THINKPAD_ACPI_BAY
2935 {
2936 .init = bay_init,
2937 .data = &bay_driver_data,
2938 },
2939#endif
2940 {
2941 .init = cmos_init,
2942 .data = &cmos_driver_data,
2943 },
2944 {
2945 .init = led_init,
2946 .data = &led_driver_data,
2947 },
2948 {
2949 .init = beep_init,
2950 .data = &beep_driver_data,
2951 },
2952 {
2953 .init = thermal_init,
2954 .data = &thermal_driver_data,
2955 },
2956 {
2957 .data = &ecdump_driver_data,
2958 },
2959 {
2960 .init = brightness_init,
2961 .data = &brightness_driver_data,
2962 },
2963 {
2964 .data = &volume_driver_data,
2965 },
2966 {
2967 .init = fan_init,
2968 .data = &fan_driver_data,
2969 },
2970};
2971
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002972static int __init set_ibm_param(const char *val, struct kernel_param *kp)
2973{
2974 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002975 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002976
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002977 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
2978 ibm = ibms_init[i].data;
2979 BUG_ON(ibm == NULL);
2980
2981 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
2982 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002983 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002984 strcpy(ibms_init[i].param, val);
2985 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002986 return 0;
2987 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002988 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002989
2990 return -EINVAL;
2991}
2992
2993static int experimental;
2994module_param(experimental, int, 0);
2995
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03002996static u32 dbg_level;
2997module_param_named(debug, dbg_level, uint, 0);
2998
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03002999static int force_load;
3000module_param(force_load, int, 0);
3001
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003002#define IBM_PARAM(feature) \
3003 module_param_call(feature, set_ibm_param, NULL, NULL, 0)
3004
3005IBM_PARAM(hotkey);
3006IBM_PARAM(bluetooth);
3007IBM_PARAM(video);
3008IBM_PARAM(light);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003009#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003010IBM_PARAM(dock);
3011#endif
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003012#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003013IBM_PARAM(bay);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003014#endif /* CONFIG_THINKPAD_ACPI_BAY */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003015IBM_PARAM(cmos);
3016IBM_PARAM(led);
3017IBM_PARAM(beep);
3018IBM_PARAM(ecdump);
3019IBM_PARAM(brightness);
3020IBM_PARAM(volume);
3021IBM_PARAM(fan);
3022
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03003023static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024{
3025 int ret, i;
3026
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003027 ret = probe_for_thinkpad();
3028 if (ret)
3029 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003031 ibm_thinkpad_ec_found = check_dmi_for_ec();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003032 IBM_HANDLE_INIT(ecrd);
3033 IBM_HANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034
3035 proc_dir = proc_mkdir(IBM_DIR, acpi_root_dir);
3036 if (!proc_dir) {
3037 printk(IBM_ERR "unable to create proc dir %s", IBM_DIR);
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03003038 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 return -ENODEV;
3040 }
3041 proc_dir->owner = THIS_MODULE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003042
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003043 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
3044 ret = ibm_init(&ibms_init[i]);
3045 if (ret >= 0 && *ibms_init[i].param)
3046 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03003048 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 return ret;
3050 }
3051 }
3052
3053 return 0;
3054}
3055
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03003056static void thinkpad_acpi_module_exit(void)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003057{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003058 struct ibm_struct *ibm, *itmp;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003059
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003060 list_for_each_entry_safe_reverse(ibm, itmp,
3061 &tpacpi_all_drivers,
3062 all_drivers) {
3063 ibm_exit(ibm);
3064 }
3065
3066 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003067
3068 if (proc_dir)
3069 remove_proc_entry(IBM_DIR, acpi_root_dir);
3070
3071 if (ibm_thinkpad_ec_found)
3072 kfree(ibm_thinkpad_ec_found);
3073}
3074
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03003075module_init(thinkpad_acpi_module_init);
3076module_exit(thinkpad_acpi_module_exit);