blob: 809ec8400ec59fd7c91d766fe35a66aa7279c31d [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 Holschuh8d376cd2007-04-21 11:08:37 -0300280static void drv_acpi_handle_init(char *name,
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300281 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
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300298static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300299{
300 struct ibm_struct *ibm = data;
301
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300302 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300303 return;
304
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300305 ibm->acpi->notify(ibm, event);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300306}
307
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300308static int __init setup_acpi_notify(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300309{
310 acpi_status status;
311 int ret;
312
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300313 BUG_ON(!ibm->acpi);
314
315 if (!*ibm->acpi->handle)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300316 return 0;
317
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300318 dbg_printk(TPACPI_DBG_INIT,
319 "setting up ACPI notify for %s\n", ibm->name);
320
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300321 ret = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300322 if (ret < 0) {
323 printk(IBM_ERR "%s device not present\n", ibm->name);
324 return -ENODEV;
325 }
326
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300327 acpi_driver_data(ibm->acpi->device) = ibm;
328 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300329 IBM_ACPI_EVENT_PREFIX,
330 ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300331
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300332 status = acpi_install_notify_handler(*ibm->acpi->handle,
333 ibm->acpi->type, dispatch_acpi_notify, ibm);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300334 if (ACPI_FAILURE(status)) {
335 if (status == AE_ALREADY_EXISTS) {
336 printk(IBM_NOTICE "another device driver is already handling %s events\n",
337 ibm->name);
338 } else {
339 printk(IBM_ERR "acpi_install_notify_handler(%s) failed: %d\n",
340 ibm->name, status);
341 }
342 return -ENODEV;
343 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300344 ibm->flags.acpi_notify_installed = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300345 return 0;
346}
347
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300348static int __init tpacpi_device_add(struct acpi_device *device)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300349{
350 return 0;
351}
352
Henrique de Moraes Holschuh67001212007-04-21 11:08:25 -0300353static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300354{
355 int ret;
356
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300357 dbg_printk(TPACPI_DBG_INIT,
358 "registering %s as an ACPI driver\n", ibm->name);
359
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300360 BUG_ON(!ibm->acpi);
361
362 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
363 if (!ibm->acpi->driver) {
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300364 printk(IBM_ERR "kzalloc(ibm->driver) failed\n");
365 return -ENOMEM;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300366 }
367
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300368 sprintf(ibm->acpi->driver->name, "%s_%s", IBM_NAME, ibm->name);
369 ibm->acpi->driver->ids = ibm->acpi->hid;
370 ibm->acpi->driver->ops.add = &tpacpi_device_add;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300371
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300372 ret = acpi_bus_register_driver(ibm->acpi->driver);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300373 if (ret < 0) {
374 printk(IBM_ERR "acpi_bus_register_driver(%s) failed: %d\n",
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300375 ibm->acpi->hid, ret);
376 kfree(ibm->acpi->driver);
377 ibm->acpi->driver = NULL;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300378 } else if (!ret)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300379 ibm->flags.acpi_driver_registered = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300380
381 return ret;
382}
383
384
385/****************************************************************************
386 ****************************************************************************
387 *
388 * Procfs Helpers
389 *
390 ****************************************************************************
391 ****************************************************************************/
392
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300393static int dispatch_procfs_read(char *page, char **start, off_t off,
394 int count, int *eof, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300395{
396 struct ibm_struct *ibm = data;
397 int len;
398
399 if (!ibm || !ibm->read)
400 return -EINVAL;
401
402 len = ibm->read(page);
403 if (len < 0)
404 return len;
405
406 if (len <= off + count)
407 *eof = 1;
408 *start = page + off;
409 len -= off;
410 if (len > count)
411 len = count;
412 if (len < 0)
413 len = 0;
414
415 return len;
416}
417
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300418static int dispatch_procfs_write(struct file *file,
419 const char __user * userbuf,
420 unsigned long count, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300421{
422 struct ibm_struct *ibm = data;
423 char *kernbuf;
424 int ret;
425
426 if (!ibm || !ibm->write)
427 return -EINVAL;
428
429 kernbuf = kmalloc(count + 2, GFP_KERNEL);
430 if (!kernbuf)
431 return -ENOMEM;
432
433 if (copy_from_user(kernbuf, userbuf, count)) {
434 kfree(kernbuf);
435 return -EFAULT;
436 }
437
438 kernbuf[count] = 0;
439 strcat(kernbuf, ",");
440 ret = ibm->write(kernbuf);
441 if (ret == 0)
442 ret = count;
443
444 kfree(kernbuf);
445
446 return ret;
447}
448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449static char *next_cmd(char **cmds)
450{
451 char *start = *cmds;
452 char *end;
453
454 while ((end = strchr(start, ',')) && end == start)
455 start = end + 1;
456
457 if (!end)
458 return NULL;
459
460 *end = 0;
461 *cmds = end + 1;
462 return start;
463}
464
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300465
466/****************************************************************************
467 ****************************************************************************
468 *
469 * Subdrivers
470 *
471 ****************************************************************************
472 ****************************************************************************/
473
474/*************************************************************************
Henrique de Moraes Holschuh142cfc92007-04-21 11:08:26 -0300475 * thinkpad-acpi init subdriver
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300476 */
477
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300478static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479{
480 printk(IBM_INFO "%s v%s\n", IBM_DESC, IBM_VERSION);
481 printk(IBM_INFO "%s\n", IBM_URL);
482
Henrique de Moraes Holschuh3945ac32007-02-06 19:13:44 -0200483 if (ibm_thinkpad_ec_found)
484 printk(IBM_INFO "ThinkPad EC firmware %s\n",
485 ibm_thinkpad_ec_found);
486
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 return 0;
488}
489
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300490static int thinkpad_acpi_driver_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
492 int len = 0;
493
494 len += sprintf(p + len, "driver:\t\t%s\n", IBM_DESC);
495 len += sprintf(p + len, "version:\t%s\n", IBM_VERSION);
496
497 return len;
498}
499
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300500static struct ibm_struct thinkpad_acpi_driver_data = {
501 .name = "driver",
502 .read = thinkpad_acpi_driver_read,
503};
504
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300505/*************************************************************************
506 * Hotkey subdriver
507 */
508
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400509static int hotkey_orig_status;
510static int hotkey_orig_mask;
511
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300512static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300513{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300514 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
515
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300516 IBM_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300517
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300518 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300519 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300520
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300521 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300522 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300523
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300524 if (tp_features.hotkey) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300525 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
526 A30, R30, R31, T20-22, X20-21, X22-24 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300527 tp_features.hotkey_mask =
528 acpi_evalf(hkey_handle, NULL, "DHKN", "qv");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300529
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300530 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300531 str_supported(tp_features.hotkey_mask));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300532
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300533 if (!hotkey_get(&hotkey_orig_status, &hotkey_orig_mask))
534 return -ENODEV;
535 }
536
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300537 return (tp_features.hotkey)? 0 : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300538}
539
540static void hotkey_exit(void)
541{
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300542 if (tp_features.hotkey) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300543 dbg_printk(TPACPI_DBG_EXIT, "restoring original hotkey mask\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300544 hotkey_set(hotkey_orig_status, hotkey_orig_mask);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300545 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300546}
547
548static void hotkey_notify(struct ibm_struct *ibm, u32 event)
549{
550 int hkey;
551
552 if (acpi_evalf(hkey_handle, &hkey, "MHKP", "d"))
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300553 acpi_bus_generate_event(ibm->acpi->device, event, hkey);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300554 else {
555 printk(IBM_ERR "unknown hotkey event %d\n", event);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300556 acpi_bus_generate_event(ibm->acpi->device, event, 0);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300557 }
558}
559
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400560static int hotkey_get(int *status, int *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
562 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400563 return 0;
564
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300565 if (tp_features.hotkey_mask)
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400566 if (!acpi_evalf(hkey_handle, mask, "DHKN", "d"))
567 return 0;
568
569 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570}
571
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400572static int hotkey_set(int status, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573{
574 int i;
575
576 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 return 0;
578
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300579 if (tp_features.hotkey_mask)
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400580 for (i = 0; i < 32; i++) {
581 int bit = ((1 << i) & mask) != 0;
582 if (!acpi_evalf(hkey_handle,
583 NULL, "MHKM", "vdd", i + 1, bit))
584 return 0;
585 }
586
587 return 1;
588}
589
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400590static int hotkey_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
592 int status, mask;
593 int len = 0;
594
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300595 if (!tp_features.hotkey) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400596 len += sprintf(p + len, "status:\t\tnot supported\n");
597 return len;
598 }
599
600 if (!hotkey_get(&status, &mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 return -EIO;
602
603 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300604 if (tp_features.hotkey_mask) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 len += sprintf(p + len, "mask:\t\t0x%04x\n", mask);
606 len += sprintf(p + len,
607 "commands:\tenable, disable, reset, <mask>\n");
608 } else {
609 len += sprintf(p + len, "mask:\t\tnot supported\n");
610 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
611 }
612
613 return len;
614}
615
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400616static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617{
618 int status, mask;
619 char *cmd;
620 int do_cmd = 0;
621
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300622 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 return -ENODEV;
624
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400625 if (!hotkey_get(&status, &mask))
626 return -EIO;
627
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 while ((cmd = next_cmd(&buf))) {
629 if (strlencmp(cmd, "enable") == 0) {
630 status = 1;
631 } else if (strlencmp(cmd, "disable") == 0) {
632 status = 0;
633 } else if (strlencmp(cmd, "reset") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400634 status = hotkey_orig_status;
635 mask = hotkey_orig_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
637 /* mask set */
638 } else if (sscanf(cmd, "%x", &mask) == 1) {
639 /* mask set */
640 } else
641 return -EINVAL;
642 do_cmd = 1;
643 }
644
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400645 if (do_cmd && !hotkey_set(status, mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 return -EIO;
647
648 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400649}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300651static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300652 .hid = IBM_HKEY_HID,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300653 .notify = hotkey_notify,
654 .handle = &hkey_handle,
655 .type = ACPI_DEVICE_NOTIFY,
656};
657
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300658static struct ibm_struct hotkey_driver_data = {
659 .name = "hotkey",
660 .read = hotkey_read,
661 .write = hotkey_write,
662 .exit = hotkey_exit,
663 .acpi = &ibm_hotkey_acpidriver,
664};
665
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300666/*************************************************************************
667 * Bluetooth subdriver
668 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300670static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300672 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
673
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300674 IBM_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300675
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400676 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
677 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300678 tp_features.bluetooth = hkey_handle &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400679 acpi_evalf(hkey_handle, NULL, "GBDC", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300681 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300682 str_supported(tp_features.bluetooth));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300683
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300684 return (tp_features.bluetooth)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685}
686
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400687static int bluetooth_status(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{
689 int status;
690
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300691 if (!tp_features.bluetooth ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400692 !acpi_evalf(hkey_handle, &status, "GBDC", "d"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 status = 0;
694
695 return status;
696}
697
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400698static int bluetooth_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{
700 int len = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400701 int status = bluetooth_status();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300703 if (!tp_features.bluetooth)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 len += sprintf(p + len, "status:\t\tnot supported\n");
705 else if (!(status & 1))
706 len += sprintf(p + len, "status:\t\tnot installed\n");
707 else {
708 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 1));
709 len += sprintf(p + len, "commands:\tenable, disable\n");
710 }
711
712 return len;
713}
714
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400715static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400717 int status = bluetooth_status();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 char *cmd;
719 int do_cmd = 0;
720
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300721 if (!tp_features.bluetooth)
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400722 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
724 while ((cmd = next_cmd(&buf))) {
725 if (strlencmp(cmd, "enable") == 0) {
726 status |= 2;
727 } else if (strlencmp(cmd, "disable") == 0) {
728 status &= ~2;
729 } else
730 return -EINVAL;
731 do_cmd = 1;
732 }
733
734 if (do_cmd && !acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400735 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
737 return 0;
738}
739
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300740static struct ibm_struct bluetooth_driver_data = {
741 .name = "bluetooth",
742 .read = bluetooth_read,
743 .write = bluetooth_write,
744};
745
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300746/*************************************************************************
747 * Wan subdriver
748 */
749
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300750static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -0400751{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300752 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
753
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300754 IBM_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300755
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300756 tp_features.wan = hkey_handle &&
757 acpi_evalf(hkey_handle, NULL, "GWAN", "qv");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -0400758
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300759 vdbg_printk(TPACPI_DBG_INIT, "wan is %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300760 str_supported(tp_features.wan));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300761
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300762 return (tp_features.wan)? 0 : 1;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -0400763}
764
765static int wan_status(void)
766{
767 int status;
768
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300769 if (!tp_features.wan ||
770 !acpi_evalf(hkey_handle, &status, "GWAN", "d"))
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -0400771 status = 0;
772
773 return status;
774}
775
776static int wan_read(char *p)
777{
778 int len = 0;
779 int status = wan_status();
780
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300781 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -0400782 len += sprintf(p + len, "status:\t\tnot supported\n");
783 else if (!(status & 1))
784 len += sprintf(p + len, "status:\t\tnot installed\n");
785 else {
786 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 1));
787 len += sprintf(p + len, "commands:\tenable, disable\n");
788 }
789
790 return len;
791}
792
793static int wan_write(char *buf)
794{
795 int status = wan_status();
796 char *cmd;
797 int do_cmd = 0;
798
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300799 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -0400800 return -ENODEV;
801
802 while ((cmd = next_cmd(&buf))) {
803 if (strlencmp(cmd, "enable") == 0) {
804 status |= 2;
805 } else if (strlencmp(cmd, "disable") == 0) {
806 status &= ~2;
807 } else
808 return -EINVAL;
809 do_cmd = 1;
810 }
811
812 if (do_cmd && !acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
813 return -EIO;
814
815 return 0;
816}
817
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300818static struct ibm_struct wan_driver_data = {
819 .name = "wan",
820 .read = wan_read,
821 .write = wan_write,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -0300822 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300823};
824
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300825/*************************************************************************
826 * Video subdriver
827 */
828
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200829static enum video_access_mode video_supported;
830static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400831
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300832IBM_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
833 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
834 "\\_SB.PCI0.VID0", /* 770e */
835 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
836 "\\_SB.PCI0.AGP.VID", /* all others */
837 ); /* R30, R31 */
838
839IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
840
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300841static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400843 int ivga;
844
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300845 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
846
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300847 IBM_ACPIHANDLE_INIT(vid);
848 IBM_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300849
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400850 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
851 /* G41, assume IVGA doesn't change */
852 vid_handle = vid2_handle;
853
854 if (!vid_handle)
855 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300856 video_supported = TPACPI_VIDEO_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400857 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
858 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300859 video_supported = TPACPI_VIDEO_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400860 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
861 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300862 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400863 else
864 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300865 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300867 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
868 str_supported(video_supported != TPACPI_VIDEO_NONE),
869 video_supported);
870
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300871 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872}
873
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300874static void video_exit(void)
875{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300876 dbg_printk(TPACPI_DBG_EXIT, "restoring original video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300877 acpi_evalf(vid_handle, NULL, "_DOS", "vd", video_orig_autosw);
878}
879
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400880static int video_status(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881{
882 int status = 0;
883 int i;
884
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300885 if (video_supported == TPACPI_VIDEO_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400886 if (acpi_evalf(NULL, &i, "\\_SB.PHS", "dd", 0x87))
887 status = i & 3;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300888 } else if (video_supported == TPACPI_VIDEO_770) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400889 if (acpi_evalf(NULL, &i, "\\VCDL", "d"))
890 status |= 0x01 * i;
891 if (acpi_evalf(NULL, &i, "\\VCDC", "d"))
892 status |= 0x02 * i;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300893 } else if (video_supported == TPACPI_VIDEO_NEW) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400894 acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1);
895 if (acpi_evalf(NULL, &i, "\\VCDC", "d"))
896 status |= 0x02 * i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400898 acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0);
899 if (acpi_evalf(NULL, &i, "\\VCDL", "d"))
900 status |= 0x01 * i;
901 if (acpi_evalf(NULL, &i, "\\VCDD", "d"))
902 status |= 0x08 * i;
903 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
905 return status;
906}
907
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400908static int video_autosw(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400910 int autosw = 0;
911
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300912 if (video_supported == TPACPI_VIDEO_570)
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400913 acpi_evalf(vid_handle, &autosw, "SWIT", "d");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300914 else if (video_supported == TPACPI_VIDEO_770 ||
915 video_supported == TPACPI_VIDEO_NEW)
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400916 acpi_evalf(vid_handle, &autosw, "^VDEE", "d");
917
918 return autosw & 1;
919}
920
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400921static int video_switch(void)
922{
923 int autosw = video_autosw();
924 int ret;
925
926 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
927 return -EIO;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300928 ret = video_supported == TPACPI_VIDEO_570 ?
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400929 acpi_evalf(ec_handle, NULL, "_Q16", "v") :
930 acpi_evalf(vid_handle, NULL, "VSWT", "v");
931 acpi_evalf(vid_handle, NULL, "_DOS", "vd", autosw);
932
933 return ret;
934}
935
936static int video_expand(void)
937{
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300938 if (video_supported == TPACPI_VIDEO_570)
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400939 return acpi_evalf(ec_handle, NULL, "_Q17", "v");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300940 else if (video_supported == TPACPI_VIDEO_770)
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400941 return acpi_evalf(vid_handle, NULL, "VEXP", "v");
942 else
943 return acpi_evalf(NULL, NULL, "\\VEXP", "v");
944}
945
946static int video_switch2(int status)
947{
948 int ret;
949
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300950 if (video_supported == TPACPI_VIDEO_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400951 ret = acpi_evalf(NULL, NULL,
952 "\\_SB.PHS2", "vdd", 0x8b, status | 0x80);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300953 } else if (video_supported == TPACPI_VIDEO_770) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400954 int autosw = video_autosw();
955 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
956 return -EIO;
957
958 ret = acpi_evalf(vid_handle, NULL,
959 "ASWT", "vdd", status * 0x100, 0);
960
961 acpi_evalf(vid_handle, NULL, "_DOS", "vd", autosw);
962 } else {
963 ret = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
964 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
965 }
966
967 return ret;
968}
969
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300970static int video_read(char *p)
971{
972 int status = video_status();
973 int autosw = video_autosw();
974 int len = 0;
975
976 if (!video_supported) {
977 len += sprintf(p + len, "status:\t\tnot supported\n");
978 return len;
979 }
980
981 len += sprintf(p + len, "status:\t\tsupported\n");
982 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
983 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300984 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300985 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
986 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
987 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
988 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300989 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300990 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
991 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
992 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
993
994 return len;
995}
996
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400997static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998{
999 char *cmd;
1000 int enable, disable, status;
1001
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001002 if (!video_supported)
1003 return -ENODEV;
1004
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 enable = disable = 0;
1006
1007 while ((cmd = next_cmd(&buf))) {
1008 if (strlencmp(cmd, "lcd_enable") == 0) {
1009 enable |= 0x01;
1010 } else if (strlencmp(cmd, "lcd_disable") == 0) {
1011 disable |= 0x01;
1012 } else if (strlencmp(cmd, "crt_enable") == 0) {
1013 enable |= 0x02;
1014 } else if (strlencmp(cmd, "crt_disable") == 0) {
1015 disable |= 0x02;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001016 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001017 strlencmp(cmd, "dvi_enable") == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 enable |= 0x08;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001019 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001020 strlencmp(cmd, "dvi_disable") == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 disable |= 0x08;
1022 } else if (strlencmp(cmd, "auto_enable") == 0) {
1023 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
1024 return -EIO;
1025 } else if (strlencmp(cmd, "auto_disable") == 0) {
1026 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 0))
1027 return -EIO;
1028 } else if (strlencmp(cmd, "video_switch") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001029 if (!video_switch())
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 return -EIO;
1031 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001032 if (!video_expand())
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 return -EIO;
1034 } else
1035 return -EINVAL;
1036 }
1037
1038 if (enable || disable) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001039 status = (video_status() & 0x0f & ~disable) | enable;
1040 if (!video_switch2(status))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 return -EIO;
1042 }
1043
1044 return 0;
1045}
1046
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001047static struct ibm_struct video_driver_data = {
1048 .name = "video",
1049 .read = video_read,
1050 .write = video_write,
1051 .exit = video_exit,
1052};
1053
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001054/*************************************************************************
1055 * Light (thinklight) subdriver
1056 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001058IBM_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
1059IBM_HANDLE(ledb, ec, "LEDB"); /* G4x */
1060
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001061static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001063 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
1064
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001065 IBM_ACPIHANDLE_INIT(ledb);
1066 IBM_ACPIHANDLE_INIT(lght);
1067 IBM_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001068
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001069 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001070 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001071
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001072 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001073 /* light status not supported on
1074 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001075 tp_features.light_status =
1076 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001078 vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001079 str_supported(tp_features.light));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001080
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001081 return (tp_features.light)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082}
1083
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001084static int light_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085{
1086 int len = 0;
1087 int status = 0;
1088
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001089 if (!tp_features.light) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001090 len += sprintf(p + len, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001091 } else if (!tp_features.light_status) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001092 len += sprintf(p + len, "status:\t\tunknown\n");
1093 len += sprintf(p + len, "commands:\ton, off\n");
1094 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
1096 return -EIO;
1097 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001098 len += sprintf(p + len, "commands:\ton, off\n");
1099 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
1101 return len;
1102}
1103
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001104static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105{
1106 int cmos_cmd, lght_cmd;
1107 char *cmd;
1108 int success;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001109
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001110 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001111 return -ENODEV;
1112
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 while ((cmd = next_cmd(&buf))) {
1114 if (strlencmp(cmd, "on") == 0) {
1115 cmos_cmd = 0x0c;
1116 lght_cmd = 1;
1117 } else if (strlencmp(cmd, "off") == 0) {
1118 cmos_cmd = 0x0d;
1119 lght_cmd = 0;
1120 } else
1121 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001122
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 success = cmos_handle ?
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001124 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
1125 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 if (!success)
1127 return -EIO;
1128 }
1129
1130 return 0;
1131}
1132
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001133static struct ibm_struct light_driver_data = {
1134 .name = "light",
1135 .read = light_read,
1136 .write = light_write,
1137};
1138
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001139/*************************************************************************
1140 * Dock subdriver
1141 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03001143#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001144
1145IBM_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
1146 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
1147 "\\_SB.PCI0.PCI1.DOCK", /* all others */
1148 "\\_SB.PCI.ISA.SLCE", /* 570 */
1149 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
1150
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001151/* don't list other alternatives as we install a notify handler on the 570 */
1152IBM_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
1153
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154#define dock_docked() (_sta(dock_handle) & 1)
1155
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001156static int __init dock_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001157{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001158 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
1159
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001160 IBM_ACPIHANDLE_INIT(dock);
1161 IBM_ACPIHANDLE_INIT(pci);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001162
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001163 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
1164 str_supported(dock_handle != NULL));
1165
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001166 return (dock_handle)? 0 : 1;
1167}
1168
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001169static void dock_notify(struct ibm_struct *ibm, u32 event)
1170{
1171 int docked = dock_docked();
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001172 int pci = ibm->acpi->hid && strstr(ibm->acpi->hid, IBM_PCI_HID);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001173
1174 if (event == 1 && !pci) /* 570 */
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001175 acpi_bus_generate_event(ibm->acpi->device, event, 1); /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001176 else if (event == 1 && pci) /* 570 */
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001177 acpi_bus_generate_event(ibm->acpi->device, event, 3); /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001178 else if (event == 3 && docked)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001179 acpi_bus_generate_event(ibm->acpi->device, event, 1); /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001180 else if (event == 3 && !docked)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001181 acpi_bus_generate_event(ibm->acpi->device, event, 2); /* undock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001182 else if (event == 0 && docked)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001183 acpi_bus_generate_event(ibm->acpi->device, event, 3); /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001184 else {
1185 printk(IBM_ERR "unknown dock event %d, status %d\n",
1186 event, _sta(dock_handle));
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001187 acpi_bus_generate_event(ibm->acpi->device, event, 0); /* unknown */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001188 }
1189}
1190
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001191static int dock_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192{
1193 int len = 0;
1194 int docked = dock_docked();
1195
1196 if (!dock_handle)
1197 len += sprintf(p + len, "status:\t\tnot supported\n");
1198 else if (!docked)
1199 len += sprintf(p + len, "status:\t\tundocked\n");
1200 else {
1201 len += sprintf(p + len, "status:\t\tdocked\n");
1202 len += sprintf(p + len, "commands:\tdock, undock\n");
1203 }
1204
1205 return len;
1206}
1207
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001208static int dock_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209{
1210 char *cmd;
1211
1212 if (!dock_docked())
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001213 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
1215 while ((cmd = next_cmd(&buf))) {
1216 if (strlencmp(cmd, "undock") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001217 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
1218 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 return -EIO;
1220 } else if (strlencmp(cmd, "dock") == 0) {
1221 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
1222 return -EIO;
1223 } else
1224 return -EINVAL;
1225 }
1226
1227 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001228}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001230static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001231 {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001232 .notify = dock_notify,
1233 .handle = &dock_handle,
1234 .type = ACPI_SYSTEM_NOTIFY,
1235 },
1236 {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001237 .hid = IBM_PCI_HID,
1238 .notify = dock_notify,
1239 .handle = &pci_handle,
1240 .type = ACPI_SYSTEM_NOTIFY,
1241 },
1242};
1243
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001244static struct ibm_struct dock_driver_data[2] = {
1245 {
1246 .name = "dock",
1247 .read = dock_read,
1248 .write = dock_write,
1249 .acpi = &ibm_dock_acpidriver[0],
1250 },
1251 {
1252 .name = "dock",
1253 .acpi = &ibm_dock_acpidriver[1],
1254 },
1255};
1256
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03001257#endif /* CONFIG_THINKPAD_ACPI_DOCK */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001259/*************************************************************************
1260 * Bay subdriver
1261 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03001263#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001264IBM_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
1265 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
1266 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
1267 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
1268 ); /* A21e, R30, R31 */
1269IBM_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
1270 "_EJ0", /* all others */
1271 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
1272IBM_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
1273 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
1274 ); /* all others */
1275IBM_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
1276 "_EJ0", /* 770x */
1277 ); /* all others */
1278
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001279static int __init bay_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001281 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
1282
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001283 IBM_ACPIHANDLE_INIT(bay);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001284 if (bay_handle)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001285 IBM_ACPIHANDLE_INIT(bay_ej);
1286 IBM_ACPIHANDLE_INIT(bay2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001287 if (bay2_handle)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001288 IBM_ACPIHANDLE_INIT(bay2_ej);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001289
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001290 tp_features.bay_status = bay_handle &&
1291 acpi_evalf(bay_handle, NULL, "_STA", "qv");
1292 tp_features.bay_status2 = bay2_handle &&
1293 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001294
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001295 tp_features.bay_eject = bay_handle && bay_ej_handle &&
1296 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
1297 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
1298 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001300 vdbg_printk(TPACPI_DBG_INIT,
1301 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001302 str_supported(tp_features.bay_status),
1303 str_supported(tp_features.bay_eject),
1304 str_supported(tp_features.bay_status2),
1305 str_supported(tp_features.bay_eject2));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001306
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001307 return (tp_features.bay_status || tp_features.bay_eject ||
1308 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309}
1310
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001311static void bay_notify(struct ibm_struct *ibm, u32 event)
1312{
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001313 acpi_bus_generate_event(ibm->acpi->device, event, 0);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001314}
1315
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001316#define bay_occupied(b) (_sta(b##_handle) & 1)
1317
1318static int bay_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319{
1320 int len = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001321 int occupied = bay_occupied(bay);
1322 int occupied2 = bay_occupied(bay2);
1323 int eject, eject2;
1324
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001325 len += sprintf(p + len, "status:\t\t%s\n",
1326 tp_features.bay_status ?
1327 (occupied ? "occupied" : "unoccupied") :
1328 "not supported");
1329 if (tp_features.bay_status2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001330 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
1331 "occupied" : "unoccupied");
1332
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001333 eject = tp_features.bay_eject && occupied;
1334 eject2 = tp_features.bay_eject2 && occupied2;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001335
1336 if (eject && eject2)
1337 len += sprintf(p + len, "commands:\teject, eject2\n");
1338 else if (eject)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 len += sprintf(p + len, "commands:\teject\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001340 else if (eject2)
1341 len += sprintf(p + len, "commands:\teject2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
1343 return len;
1344}
1345
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001346static int bay_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347{
1348 char *cmd;
1349
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001350 if (!tp_features.bay_eject && !tp_features.bay_eject2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001351 return -ENODEV;
1352
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001354 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001355 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
1356 return -EIO;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001357 } else if (tp_features.bay_eject2 &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001358 strlencmp(cmd, "eject2") == 0) {
1359 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 return -EIO;
1361 } else
1362 return -EINVAL;
1363 }
1364
1365 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001366}
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001367
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001368static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
1369 .notify = bay_notify,
1370 .handle = &bay_handle,
1371 .type = ACPI_SYSTEM_NOTIFY,
1372};
1373
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001374static struct ibm_struct bay_driver_data = {
1375 .name = "bay",
1376 .read = bay_read,
1377 .write = bay_write,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001378 .acpi = &ibm_bay_acpidriver,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001379};
1380
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03001381#endif /* CONFIG_THINKPAD_ACPI_BAY */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001383/*************************************************************************
1384 * CMOS subdriver
1385 */
1386
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001387static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001388{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001389 vdbg_printk(TPACPI_DBG_INIT,
1390 "initializing cmos commands subdriver\n");
1391
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001392 IBM_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001393
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001394 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
1395 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001396 return (cmos_handle)? 0 : 1;
1397}
1398
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001399static int cmos_eval(int cmos_cmd)
1400{
1401 if (cmos_handle)
1402 return acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd);
1403 else
1404 return 1;
1405}
1406
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001407static int cmos_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408{
1409 int len = 0;
1410
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001411 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1412 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 if (!cmos_handle)
1414 len += sprintf(p + len, "status:\t\tnot supported\n");
1415 else {
1416 len += sprintf(p + len, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001417 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 }
1419
1420 return len;
1421}
1422
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001423static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424{
1425 char *cmd;
1426 int cmos_cmd;
1427
1428 if (!cmos_handle)
1429 return -EINVAL;
1430
1431 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001432 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
1433 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 /* cmos_cmd set */
1435 } else
1436 return -EINVAL;
1437
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001438 if (!cmos_eval(cmos_cmd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 return -EIO;
1440 }
1441
1442 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001443}
1444
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001445static struct ibm_struct cmos_driver_data = {
1446 .name = "cmos",
1447 .read = cmos_read,
1448 .write = cmos_write,
1449};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001450
1451/*************************************************************************
1452 * LED subdriver
1453 */
1454
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02001455static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001456
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001457IBM_HANDLE(led, ec, "SLED", /* 570 */
1458 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
1459 "LED", /* all others */
1460 ); /* R30, R31 */
1461
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001462static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001463{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001464 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
1465
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001466 IBM_ACPIHANDLE_INIT(led);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001467
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001468 if (!led_handle)
1469 /* led not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001470 led_supported = TPACPI_LED_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001471 else if (strlencmp(led_path, "SLED") == 0)
1472 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001473 led_supported = TPACPI_LED_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001474 else if (strlencmp(led_path, "SYSL") == 0)
1475 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001476 led_supported = TPACPI_LED_OLD;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001477 else
1478 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001479 led_supported = TPACPI_LED_NEW;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001480
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001481 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
1482 str_supported(led_supported), led_supported);
1483
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001484 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001485}
1486
1487#define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
1488
1489static int led_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490{
1491 int len = 0;
1492
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001493 if (!led_supported) {
1494 len += sprintf(p + len, "status:\t\tnot supported\n");
1495 return len;
1496 }
1497 len += sprintf(p + len, "status:\t\tsupported\n");
1498
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001499 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001500 /* 570 */
1501 int i, status;
1502 for (i = 0; i < 8; i++) {
1503 if (!acpi_evalf(ec_handle,
1504 &status, "GLED", "dd", 1 << i))
1505 return -EIO;
1506 len += sprintf(p + len, "%d:\t\t%s\n",
1507 i, led_status(status));
1508 }
1509 }
1510
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 len += sprintf(p + len, "commands:\t"
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001512 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
1514 return len;
1515}
1516
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001517/* off, on, blink */
1518static const int led_sled_arg1[] = { 0, 1, 3 };
1519static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
1520static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
1521static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
1522
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001523static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524{
1525 char *cmd;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001526 int led, ind, ret;
1527
1528 if (!led_supported)
1529 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
1531 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001532 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 return -EINVAL;
1534
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001535 if (strstr(cmd, "off")) {
1536 ind = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 } else if (strstr(cmd, "on")) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001538 ind = 1;
1539 } else if (strstr(cmd, "blink")) {
1540 ind = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 } else
1542 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001543
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001544 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001545 /* 570 */
1546 led = 1 << led;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001548 led, led_sled_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 return -EIO;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001550 } else if (led_supported == TPACPI_LED_OLD) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001551 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
1552 led = 1 << led;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001553 ret = ec_write(TPACPI_LED_EC_HLMS, led);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001554 if (ret >= 0)
1555 ret =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001556 ec_write(TPACPI_LED_EC_HLBL,
Henrique de Moraes Holschuhe062e032007-03-23 17:33:55 -03001557 led * led_exp_hlbl[ind]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001558 if (ret >= 0)
1559 ret =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001560 ec_write(TPACPI_LED_EC_HLCL,
Henrique de Moraes Holschuhe062e032007-03-23 17:33:55 -03001561 led * led_exp_hlcl[ind]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001562 if (ret < 0)
1563 return ret;
1564 } else {
1565 /* all others */
1566 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
1567 led, led_led_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001569 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 }
1571
1572 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001573}
1574
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001575static struct ibm_struct led_driver_data = {
1576 .name = "led",
1577 .read = led_read,
1578 .write = led_write,
1579};
1580
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001581/*************************************************************************
1582 * Beep subdriver
1583 */
1584
1585IBM_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
1586
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001587static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001588{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001589 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
1590
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001591 IBM_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001592
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001593 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
1594 str_supported(beep_handle != NULL));
1595
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001596 return (beep_handle)? 0 : 1;
1597}
1598
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001599static int beep_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600{
1601 int len = 0;
1602
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001603 if (!beep_handle)
1604 len += sprintf(p + len, "status:\t\tnot supported\n");
1605 else {
1606 len += sprintf(p + len, "status:\t\tsupported\n");
1607 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
1608 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
1610 return len;
1611}
1612
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001613static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614{
1615 char *cmd;
1616 int beep_cmd;
1617
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001618 if (!beep_handle)
1619 return -ENODEV;
1620
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001622 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
1623 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 /* beep_cmd set */
1625 } else
1626 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001627 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 return -EIO;
1629 }
1630
1631 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001632}
1633
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001634static struct ibm_struct beep_driver_data = {
1635 .name = "beep",
1636 .read = beep_read,
1637 .write = beep_write,
1638};
1639
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001640/*************************************************************************
1641 * Thermal subdriver
1642 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001643
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001644static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001645
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001646static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001647{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001648 u8 t, ta1, ta2;
1649 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001650 int acpi_tmp7;
1651
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001652 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
1653
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001654 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001655
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001656 if (ibm_thinkpad_ec_found && experimental) {
1657 /*
1658 * Direct EC access mode: sensors at registers
1659 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
1660 * non-implemented, thermal sensors return 0x80 when
1661 * not available
1662 */
1663
1664 ta1 = ta2 = 0;
1665 for (i = 0; i < 8; i++) {
1666 if (likely(acpi_ec_read(0x78 + i, &t))) {
1667 ta1 |= t;
1668 } else {
1669 ta1 = 0;
1670 break;
1671 }
1672 if (likely(acpi_ec_read(0xC0 + i, &t))) {
1673 ta2 |= t;
1674 } else {
1675 ta1 = 0;
1676 break;
1677 }
1678 }
1679 if (ta1 == 0) {
1680 /* This is sheer paranoia, but we handle it anyway */
1681 if (acpi_tmp7) {
1682 printk(IBM_ERR
1683 "ThinkPad ACPI EC access misbehaving, "
1684 "falling back to ACPI TMPx access mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001685 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001686 } else {
1687 printk(IBM_ERR
1688 "ThinkPad ACPI EC access misbehaving, "
1689 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001690 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001691 }
1692 } else {
1693 thermal_read_mode =
1694 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001695 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001696 }
1697 } else if (acpi_tmp7) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001698 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
1699 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001700 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001701 } else {
1702 /* Standard ACPI TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001703 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001704 }
1705 } else {
1706 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001707 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001708 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001709
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001710 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
1711 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
1712 thermal_read_mode);
1713
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001714 return (thermal_read_mode != TPACPI_THERMAL_NONE)? 0 : 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001715}
1716
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001717static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
1718{
1719 int i, t;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001720 s8 tmp;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001721 char tmpi[] = "TMPi";
1722
1723 if (!s)
1724 return -EINVAL;
1725
1726 switch (thermal_read_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001727#if TPACPI_MAX_THERMAL_SENSORS >= 16
1728 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001729 for (i = 0; i < 8; i++) {
1730 if (!acpi_ec_read(0xC0 + i, &tmp))
1731 return -EIO;
1732 s->temp[i + 8] = tmp * 1000;
1733 }
1734 /* fallthrough */
1735#endif
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001736 case TPACPI_THERMAL_TPEC_8:
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001737 for (i = 0; i < 8; i++) {
1738 if (!acpi_ec_read(0x78 + i, &tmp))
1739 return -EIO;
1740 s->temp[i] = tmp * 1000;
1741 }
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001742 return (thermal_read_mode == TPACPI_THERMAL_TPEC_16) ? 16 : 8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001743
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001744 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001745 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
1746 return -EIO;
1747 for (i = 0; i < 8; i++) {
1748 tmpi[3] = '0' + i;
1749 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
1750 return -EIO;
1751 s->temp[i] = (t - 2732) * 100;
1752 }
1753 return 8;
1754
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001755 case TPACPI_THERMAL_ACPI_TMP07:
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001756 for (i = 0; i < 8; i++) {
1757 tmpi[3] = '0' + i;
1758 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
1759 return -EIO;
1760 s->temp[i] = t * 1000;
1761 }
1762 return 8;
1763
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001764 case TPACPI_THERMAL_NONE:
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001765 default:
1766 return 0;
1767 }
1768}
1769
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001770static int thermal_read(char *p)
1771{
1772 int len = 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001773 int n, i;
1774 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001775
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001776 n = thermal_get_sensors(&t);
1777 if (unlikely(n < 0))
1778 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001779
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001780 len += sprintf(p + len, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001781
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001782 if (n > 0) {
1783 for (i = 0; i < (n - 1); i++)
1784 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
1785 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
1786 } else
1787 len += sprintf(p + len, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001788
1789 return len;
1790}
1791
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001792static struct ibm_struct thermal_driver_data = {
1793 .name = "thermal",
1794 .read = thermal_read,
1795};
1796
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001797/*************************************************************************
1798 * EC Dump subdriver
1799 */
1800
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001801static u8 ecdump_regs[256];
1802
1803static int ecdump_read(char *p)
1804{
1805 int len = 0;
1806 int i, j;
1807 u8 v;
1808
1809 len += sprintf(p + len, "EC "
1810 " +00 +01 +02 +03 +04 +05 +06 +07"
1811 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
1812 for (i = 0; i < 256; i += 16) {
1813 len += sprintf(p + len, "EC 0x%02x:", i);
1814 for (j = 0; j < 16; j++) {
1815 if (!acpi_ec_read(i + j, &v))
1816 break;
1817 if (v != ecdump_regs[i + j])
1818 len += sprintf(p + len, " *%02x", v);
1819 else
1820 len += sprintf(p + len, " %02x", v);
1821 ecdump_regs[i + j] = v;
1822 }
1823 len += sprintf(p + len, "\n");
1824 if (j != 16)
1825 break;
1826 }
1827
1828 /* These are way too dangerous to advertise openly... */
1829#if 0
1830 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
1831 " (<offset> is 00-ff, <value> is 00-ff)\n");
1832 len += sprintf(p + len, "commands:\t0x<offset> <value> "
1833 " (<offset> is 00-ff, <value> is 0-255)\n");
1834#endif
1835 return len;
1836}
1837
1838static int ecdump_write(char *buf)
1839{
1840 char *cmd;
1841 int i, v;
1842
1843 while ((cmd = next_cmd(&buf))) {
1844 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
1845 /* i and v set */
1846 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
1847 /* i and v set */
1848 } else
1849 return -EINVAL;
1850 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
1851 if (!acpi_ec_write(i, v))
1852 return -EIO;
1853 } else
1854 return -EINVAL;
1855 }
1856
1857 return 0;
1858}
1859
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001860static struct ibm_struct ecdump_driver_data = {
1861 .name = "ecdump",
1862 .read = ecdump_read,
1863 .write = ecdump_write,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03001864 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001865};
1866
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001867/*************************************************************************
1868 * Backlight/brightness subdriver
1869 */
Holger Macht8acb0252006-10-20 14:30:28 -07001870
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001871static struct backlight_device *ibm_backlight_device = NULL;
Holger Macht8acb0252006-10-20 14:30:28 -07001872
Richard Purdie599a52d2007-02-10 23:07:48 +00001873static struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02001874 .get_brightness = brightness_get,
1875 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02001876};
1877
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001878static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02001879{
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02001880 int b;
1881
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001882 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
1883
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02001884 b = brightness_get(NULL);
1885 if (b < 0)
1886 return b;
1887
Yu Luming519ab5f2006-12-19 12:56:15 -08001888 ibm_backlight_device = backlight_device_register("ibm", NULL, NULL,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02001889 &ibm_backlight_data);
1890 if (IS_ERR(ibm_backlight_device)) {
1891 printk(IBM_ERR "Could not register backlight device\n");
1892 return PTR_ERR(ibm_backlight_device);
1893 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001894 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02001895
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02001896 ibm_backlight_device->props.max_brightness = 7;
1897 ibm_backlight_device->props.brightness = b;
1898 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00001899
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02001900 return 0;
1901}
1902
1903static void brightness_exit(void)
1904{
1905 if (ibm_backlight_device) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001906 vdbg_printk(TPACPI_DBG_EXIT,
1907 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02001908 backlight_device_unregister(ibm_backlight_device);
1909 ibm_backlight_device = NULL;
1910 }
1911}
1912
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001913static int brightness_update_status(struct backlight_device *bd)
1914{
1915 return brightness_set(
1916 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
1917 bd->props.power == FB_BLANK_UNBLANK) ?
1918 bd->props.brightness : 0);
1919}
1920
1921static int brightness_get(struct backlight_device *bd)
1922{
1923 u8 level;
1924 if (!acpi_ec_read(brightness_offset, &level))
1925 return -EIO;
1926
1927 level &= 0x7;
1928
1929 return level;
1930}
1931
1932static int brightness_set(int value)
1933{
1934 int cmos_cmd, inc, i;
1935 int current_value = brightness_get(NULL);
1936
1937 value &= 7;
1938
1939 cmos_cmd = value > current_value ? TP_CMOS_BRIGHTNESS_UP : TP_CMOS_BRIGHTNESS_DOWN;
1940 inc = value > current_value ? 1 : -1;
1941 for (i = current_value; i != value; i += inc) {
1942 if (!cmos_eval(cmos_cmd))
1943 return -EIO;
1944 if (!acpi_ec_write(brightness_offset, i + inc))
1945 return -EIO;
1946 }
1947
1948 return 0;
1949}
1950
1951static int brightness_read(char *p)
1952{
1953 int len = 0;
1954 int level;
1955
1956 if ((level = brightness_get(NULL)) < 0) {
1957 len += sprintf(p + len, "level:\t\tunreadable\n");
1958 } else {
1959 len += sprintf(p + len, "level:\t\t%d\n", level & 0x7);
1960 len += sprintf(p + len, "commands:\tup, down\n");
1961 len += sprintf(p + len, "commands:\tlevel <level>"
1962 " (<level> is 0-7)\n");
1963 }
1964
1965 return len;
1966}
1967
1968static int brightness_write(char *buf)
1969{
1970 int level;
1971 int new_level;
1972 char *cmd;
1973
1974 while ((cmd = next_cmd(&buf))) {
1975 if ((level = brightness_get(NULL)) < 0)
1976 return level;
1977 level &= 7;
1978
1979 if (strlencmp(cmd, "up") == 0) {
1980 new_level = level == 7 ? 7 : level + 1;
1981 } else if (strlencmp(cmd, "down") == 0) {
1982 new_level = level == 0 ? 0 : level - 1;
1983 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
1984 new_level >= 0 && new_level <= 7) {
1985 /* new_level set */
1986 } else
1987 return -EINVAL;
1988
1989 brightness_set(new_level);
1990 }
1991
1992 return 0;
1993}
1994
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001995static struct ibm_struct brightness_driver_data = {
1996 .name = "brightness",
1997 .read = brightness_read,
1998 .write = brightness_write,
1999 .exit = brightness_exit,
2000};
2001
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002002/*************************************************************************
2003 * Volume subdriver
2004 */
2005
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002006static int volume_read(char *p)
2007{
2008 int len = 0;
2009 u8 level;
2010
2011 if (!acpi_ec_read(volume_offset, &level)) {
2012 len += sprintf(p + len, "level:\t\tunreadable\n");
2013 } else {
2014 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
2015 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
2016 len += sprintf(p + len, "commands:\tup, down, mute\n");
2017 len += sprintf(p + len, "commands:\tlevel <level>"
2018 " (<level> is 0-15)\n");
2019 }
2020
2021 return len;
2022}
2023
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002024static int volume_write(char *buf)
2025{
2026 int cmos_cmd, inc, i;
2027 u8 level, mute;
2028 int new_level, new_mute;
2029 char *cmd;
2030
2031 while ((cmd = next_cmd(&buf))) {
2032 if (!acpi_ec_read(volume_offset, &level))
2033 return -EIO;
2034 new_mute = mute = level & 0x40;
2035 new_level = level = level & 0xf;
2036
2037 if (strlencmp(cmd, "up") == 0) {
2038 if (mute)
2039 new_mute = 0;
2040 else
2041 new_level = level == 15 ? 15 : level + 1;
2042 } else if (strlencmp(cmd, "down") == 0) {
2043 if (mute)
2044 new_mute = 0;
2045 else
2046 new_level = level == 0 ? 0 : level - 1;
2047 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
2048 new_level >= 0 && new_level <= 15) {
2049 /* new_level set */
2050 } else if (strlencmp(cmd, "mute") == 0) {
2051 new_mute = 0x40;
2052 } else
2053 return -EINVAL;
2054
2055 if (new_level != level) { /* mute doesn't change */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002056 cmos_cmd = new_level > level ? TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002057 inc = new_level > level ? 1 : -1;
2058
2059 if (mute && (!cmos_eval(cmos_cmd) ||
2060 !acpi_ec_write(volume_offset, level)))
2061 return -EIO;
2062
2063 for (i = level; i != new_level; i += inc)
2064 if (!cmos_eval(cmos_cmd) ||
2065 !acpi_ec_write(volume_offset, i + inc))
2066 return -EIO;
2067
Henrique de Moraes Holschuhe062e032007-03-23 17:33:55 -03002068 if (mute && (!cmos_eval(TP_CMOS_VOLUME_MUTE) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002069 !acpi_ec_write(volume_offset,
2070 new_level + mute)))
2071 return -EIO;
2072 }
2073
2074 if (new_mute != mute) { /* level doesn't change */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002075 cmos_cmd = new_mute ? TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002076
2077 if (!cmos_eval(cmos_cmd) ||
2078 !acpi_ec_write(volume_offset, level + new_mute))
2079 return -EIO;
2080 }
2081 }
2082
2083 return 0;
2084}
2085
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002086static struct ibm_struct volume_driver_data = {
2087 .name = "volume",
2088 .read = volume_read,
2089 .write = volume_write,
2090};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002091
2092/*************************************************************************
2093 * Fan subdriver
2094 */
2095
2096/*
2097 * FAN ACCESS MODES
2098 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002099 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002100 * ACPI GFAN method: returns fan level
2101 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002102 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03002103 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002104 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002105 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002106 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
2107 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03002108 * EC 0x2f (HFSP) might be available *for reading*, but do not use
2109 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002110 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002111 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03002112 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
2113 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002114 *
2115 * Fan speed changes of any sort (including those caused by the
2116 * disengaged mode) are usually done slowly by the firmware as the
2117 * maximum ammount of fan duty cycle change per second seems to be
2118 * limited.
2119 *
2120 * Reading is not available if GFAN exists.
2121 * Writing is not available if SFAN exists.
2122 *
2123 * Bits
2124 * 7 automatic mode engaged;
2125 * (default operation mode of the ThinkPad)
2126 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03002127 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002128 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03002129 * the tachometer while the fan controller ramps up
2130 * the speed (which can take up to a few *minutes*).
2131 * Speeds up fan to 100% duty-cycle, which is far above
2132 * the standard RPM levels. It is not impossible that
2133 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002134 * 5-3 unused in some models. Extra bits for fan level
2135 * in others, but still useless as all values above
2136 * 7 map to the same speed as level 7 in these models.
2137 * 2-0 fan level (0..7 usually)
2138 * 0x00 = stop
2139 * 0x07 = max (set when temperatures critical)
2140 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002141 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002142 *
2143 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
2144 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
2145 * does so, its initial value is meaningless (0x07).
2146 *
2147 * For firmware bugs, refer to:
2148 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
2149 *
2150 * ----
2151 *
2152 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
2153 * Main fan tachometer reading (in RPM)
2154 *
2155 * This register is present on all ThinkPads with a new-style EC, and
2156 * it is known not to be present on the A21m/e, and T22, as there is
2157 * something else in offset 0x84 according to the ACPI DSDT. Other
2158 * ThinkPads from this same time period (and earlier) probably lack the
2159 * tachometer as well.
2160 *
2161 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
2162 * was never fixed by IBM to report the EC firmware version string
2163 * probably support the tachometer (like the early X models), so
2164 * detecting it is quite hard. We need more data to know for sure.
2165 *
2166 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
2167 * might result.
2168 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03002169 * FIRMWARE BUG: may go stale while the EC is switching to full speed
2170 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002171 *
2172 * For firmware bugs, refer to:
2173 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
2174 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002175 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002176 * ThinkPad X31, X40, X41. Not available in the X60.
2177 *
2178 * FANS ACPI handle: takes three arguments: low speed, medium speed,
2179 * high speed. ACPI DSDT seems to map these three speeds to levels
2180 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
2181 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
2182 *
2183 * The speeds are stored on handles
2184 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
2185 *
2186 * There are three default speed sets, acessible as handles:
2187 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
2188 *
2189 * ACPI DSDT switches which set is in use depending on various
2190 * factors.
2191 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002192 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002193 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
2194 * but the ACPI tables just mention level 7.
2195 */
2196
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002197static enum fan_status_access_mode fan_status_access_mode;
2198static enum fan_control_access_mode fan_control_access_mode;
2199static enum fan_control_commands fan_control_commands;
2200
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002201static u8 fan_control_initial_status;
2202
Len Brown25c68a32006-12-08 04:43:41 -05002203static void fan_watchdog_fire(struct work_struct *ignored);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002204static int fan_watchdog_maxinterval;
Len Brown25c68a32006-12-08 04:43:41 -05002205static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002206
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002207IBM_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
2208IBM_HANDLE(gfan, ec, "GFAN", /* 570 */
2209 "\\FSPD", /* 600e/x, 770e, 770x */
2210 ); /* all others */
2211IBM_HANDLE(sfan, ec, "SFAN", /* 570 */
2212 "JFNS", /* 770x-JL */
2213 ); /* all others */
2214
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002215static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002216{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002217 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
2218
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002219 fan_status_access_mode = TPACPI_FAN_NONE;
2220 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002221 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002222 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002223 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002224
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002225 IBM_ACPIHANDLE_INIT(fans);
2226 IBM_ACPIHANDLE_INIT(gfan);
2227 IBM_ACPIHANDLE_INIT(sfan);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002228
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002229 if (gfan_handle) {
2230 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002231 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002232 } else {
2233 /* all other ThinkPads: note that even old-style
2234 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002235 if (likely(acpi_ec_read(fan_status_offset,
2236 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002237 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002238
2239 /* In some ThinkPads, neither the EC nor the ACPI
2240 * DSDT initialize the fan status, and it ends up
2241 * being set to 0x07 when it *could* be either
2242 * 0x07 or 0x80.
2243 *
2244 * Enable for TP-1Y (T43), TP-78 (R51e),
2245 * TP-76 (R52), TP-70 (T43, R52), which are known
2246 * to be buggy. */
2247 if (fan_control_initial_status == 0x07 &&
2248 ibm_thinkpad_ec_found &&
2249 ((ibm_thinkpad_ec_found[0] == '1' &&
2250 ibm_thinkpad_ec_found[1] == 'Y') ||
2251 (ibm_thinkpad_ec_found[0] == '7' &&
2252 (ibm_thinkpad_ec_found[1] == '6' ||
2253 ibm_thinkpad_ec_found[1] == '8' ||
2254 ibm_thinkpad_ec_found[1] == '0'))
2255 )) {
2256 printk(IBM_NOTICE
2257 "fan_init: initial fan status is "
2258 "unknown, assuming it is in auto "
2259 "mode\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002260 tp_features.fan_ctrl_status_undef = 1;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002261 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002262 } else {
2263 printk(IBM_ERR
2264 "ThinkPad ACPI EC access misbehaving, "
2265 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002266 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002267 }
2268 }
2269
2270 if (sfan_handle) {
2271 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002272 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002273 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002274 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002275 } else {
2276 if (!gfan_handle) {
2277 /* gfan without sfan means no fan control */
2278 /* all other models implement TP EC 0x2f control */
2279
2280 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02002281 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002282 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002283 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002284 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002285 TPACPI_FAN_CMD_SPEED |
2286 TPACPI_FAN_CMD_LEVEL |
2287 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002288 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002289 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02002290 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002291 TPACPI_FAN_CMD_LEVEL |
2292 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002293 }
2294 }
2295 }
2296
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002297 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
2298 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
2299 fan_control_access_mode != TPACPI_FAN_WR_NONE),
2300 fan_status_access_mode, fan_control_access_mode);
2301
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002302 return (fan_status_access_mode != TPACPI_FAN_NONE ||
2303 fan_control_access_mode != TPACPI_FAN_WR_NONE)?
2304 0 : 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002305}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002306
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02002307static int fan_get_status(u8 *status)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002308{
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02002309 u8 s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002310
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02002311 /* TODO:
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002312 * Add TPACPI_FAN_RD_ACPI_FANS ? */
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02002313
Henrique de Moraes Holschuh3ef8a602006-11-24 11:47:10 -02002314 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002315 case TPACPI_FAN_RD_ACPI_GFAN:
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002316 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02002317
2318 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002319 return -EIO;
2320
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02002321 if (likely(status))
2322 *status = s & 0x07;
2323
2324 break;
2325
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002326 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02002327 /* all except 570, 600e/x, 770e, 770x */
2328 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
2329 return -EIO;
2330
2331 if (likely(status))
2332 *status = s;
2333
2334 break;
2335
2336 default:
2337 return -ENXIO;
2338 }
2339
2340 return 0;
2341}
2342
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002343static void fan_exit(void)
2344{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002345 vdbg_printk(TPACPI_DBG_EXIT, "cancelling any pending watchdogs\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002346 cancel_delayed_work(&fan_watchdog_task);
2347 flush_scheduled_work();
2348}
2349
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02002350static int fan_get_speed(unsigned int *speed)
2351{
2352 u8 hi, lo;
2353
2354 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002355 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02002356 /* all except 570, 600e/x, 770e, 770x */
2357 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
2358 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
2359 return -EIO;
2360
2361 if (likely(speed))
2362 *speed = (hi << 8) | lo;
2363
2364 break;
2365
2366 default:
2367 return -ENXIO;
2368 }
2369
2370 return 0;
2371}
2372
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002373static void fan_watchdog_fire(struct work_struct *ignored)
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002374{
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002375 printk(IBM_NOTICE "fan watchdog: enabling fan\n");
2376 if (fan_set_enable()) {
2377 printk(IBM_ERR "fan watchdog: error while enabling fan\n");
2378 /* reschedule for later */
2379 fan_watchdog_reset();
2380 }
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002381}
2382
2383static void fan_watchdog_reset(void)
2384{
2385 static int fan_watchdog_active = 0;
2386
2387 if (fan_watchdog_active)
2388 cancel_delayed_work(&fan_watchdog_task);
2389
2390 if (fan_watchdog_maxinterval > 0) {
2391 fan_watchdog_active = 1;
2392 if (!schedule_delayed_work(&fan_watchdog_task,
2393 msecs_to_jiffies(fan_watchdog_maxinterval
2394 * 1000))) {
2395 printk(IBM_ERR "failed to schedule the fan watchdog, "
2396 "watchdog will not trigger\n");
2397 }
2398 } else
2399 fan_watchdog_active = 0;
2400}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002401
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002402static int fan_set_level(int level)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002403{
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002404 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002405 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002406 if (level >= 0 && level <= 7) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002407 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
2408 return -EIO;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002409 } else
2410 return -EINVAL;
2411 break;
2412
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002413 case TPACPI_FAN_WR_ACPI_FANS:
2414 case TPACPI_FAN_WR_TPEC:
2415 if ((level != TP_EC_FAN_AUTO) &&
2416 (level != TP_EC_FAN_FULLSPEED) &&
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02002417 ((level < 0) || (level > 7)))
2418 return -EINVAL;
2419
2420 if (!acpi_ec_write(fan_status_offset, level))
2421 return -EIO;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002422 else
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002423 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02002424 break;
2425
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002426 default:
2427 return -ENXIO;
2428 }
2429 return 0;
2430}
2431
2432static int fan_set_enable(void)
2433{
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002434 u8 s;
2435 int rc;
2436
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002437 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002438 case TPACPI_FAN_WR_ACPI_FANS:
2439 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002440 if ((rc = fan_get_status(&s)) < 0)
2441 return rc;
2442
2443 /* Don't go out of emergency fan mode */
2444 if (s != 7)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002445 s = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002446
2447 if (!acpi_ec_write(fan_status_offset, s))
2448 return -EIO;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002449 else
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002450 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002451 break;
2452
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002453 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002454 if ((rc = fan_get_status(&s)) < 0)
2455 return rc;
2456
2457 s &= 0x07;
2458
2459 /* Set fan to at least level 4 */
2460 if (s < 4)
2461 s = 4;
2462
2463 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002464 return -EIO;
2465 break;
2466
2467 default:
2468 return -ENXIO;
2469 }
2470 return 0;
2471}
2472
2473static int fan_set_disable(void)
2474{
2475 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002476 case TPACPI_FAN_WR_ACPI_FANS:
2477 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002478 if (!acpi_ec_write(fan_status_offset, 0x00))
2479 return -EIO;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002480 else
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002481 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002482 break;
2483
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002484 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002485 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
2486 return -EIO;
2487 break;
2488
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002489 default:
2490 return -ENXIO;
2491 }
2492 return 0;
2493}
2494
2495static int fan_set_speed(int speed)
2496{
2497 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002498 case TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002499 if (speed >= 0 && speed <= 65535) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002500 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
2501 speed, speed, speed))
2502 return -EIO;
2503 } else
2504 return -EINVAL;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002505 break;
2506
2507 default:
2508 return -ENXIO;
2509 }
2510 return 0;
2511}
2512
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002513static int fan_read(char *p)
2514{
2515 int len = 0;
2516 int rc;
2517 u8 status;
2518 unsigned int speed = 0;
2519
2520 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002521 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002522 /* 570, 600e/x, 770e, 770x */
2523 if ((rc = fan_get_status(&status)) < 0)
2524 return rc;
2525
2526 len += sprintf(p + len, "status:\t\t%s\n"
2527 "level:\t\t%d\n",
2528 (status != 0) ? "enabled" : "disabled", status);
2529 break;
2530
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002531 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002532 /* all except 570, 600e/x, 770e, 770x */
2533 if ((rc = fan_get_status(&status)) < 0)
2534 return rc;
2535
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002536 if (unlikely(tp_features.fan_ctrl_status_undef)) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002537 if (status != fan_control_initial_status)
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002538 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002539 else
2540 /* Return most likely status. In fact, it
2541 * might be the only possible status */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002542 status = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002543 }
2544
2545 len += sprintf(p + len, "status:\t\t%s\n",
2546 (status != 0) ? "enabled" : "disabled");
2547
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002548 if ((rc = fan_get_speed(&speed)) < 0)
2549 return rc;
2550
2551 len += sprintf(p + len, "speed:\t\t%d\n", speed);
2552
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002553 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002554 /* Disengaged mode takes precedence */
2555 len += sprintf(p + len, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002556 else if (status & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002557 len += sprintf(p + len, "level:\t\tauto\n");
2558 else
2559 len += sprintf(p + len, "level:\t\t%d\n", status);
2560 break;
2561
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002562 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002563 default:
2564 len += sprintf(p + len, "status:\t\tnot supported\n");
2565 }
2566
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002567 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002568 len += sprintf(p + len, "commands:\tlevel <level>");
2569
2570 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002571 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002572 len += sprintf(p + len, " (<level> is 0-7)\n");
2573 break;
2574
2575 default:
2576 len += sprintf(p + len, " (<level> is 0-7, "
2577 "auto, disengaged)\n");
2578 break;
2579 }
2580 }
2581
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002582 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002583 len += sprintf(p + len, "commands:\tenable, disable\n"
2584 "commands:\twatchdog <timeout> (<timeout> is 0 (off), "
2585 "1-120 (seconds))\n");
2586
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002587 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002588 len += sprintf(p + len, "commands:\tspeed <speed>"
2589 " (<speed> is 0-65535)\n");
2590
2591 return len;
2592}
2593
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002594static int fan_write_cmd_level(const char *cmd, int *rc)
2595{
2596 int level;
2597
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02002598 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002599 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02002600 else if (strlencmp(cmd, "level disengaged") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002601 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02002602 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002603 return 0;
2604
2605 if ((*rc = fan_set_level(level)) == -ENXIO)
2606 printk(IBM_ERR "level command accepted for unsupported "
2607 "access mode %d", fan_control_access_mode);
2608
2609 return 1;
2610}
2611
2612static int fan_write_cmd_enable(const char *cmd, int *rc)
2613{
2614 if (strlencmp(cmd, "enable") != 0)
2615 return 0;
2616
2617 if ((*rc = fan_set_enable()) == -ENXIO)
2618 printk(IBM_ERR "enable command accepted for unsupported "
2619 "access mode %d", fan_control_access_mode);
2620
2621 return 1;
2622}
2623
2624static int fan_write_cmd_disable(const char *cmd, int *rc)
2625{
2626 if (strlencmp(cmd, "disable") != 0)
2627 return 0;
2628
2629 if ((*rc = fan_set_disable()) == -ENXIO)
2630 printk(IBM_ERR "disable command accepted for unsupported "
2631 "access mode %d", fan_control_access_mode);
2632
2633 return 1;
2634}
2635
2636static int fan_write_cmd_speed(const char *cmd, int *rc)
2637{
2638 int speed;
2639
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02002640 /* TODO:
2641 * Support speed <low> <medium> <high> ? */
2642
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002643 if (sscanf(cmd, "speed %d", &speed) != 1)
2644 return 0;
2645
2646 if ((*rc = fan_set_speed(speed)) == -ENXIO)
2647 printk(IBM_ERR "speed command accepted for unsupported "
2648 "access mode %d", fan_control_access_mode);
2649
2650 return 1;
2651}
2652
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002653static int fan_write_cmd_watchdog(const char *cmd, int *rc)
2654{
2655 int interval;
2656
2657 if (sscanf(cmd, "watchdog %d", &interval) != 1)
2658 return 0;
2659
2660 if (interval < 0 || interval > 120)
2661 *rc = -EINVAL;
2662 else
2663 fan_watchdog_maxinterval = interval;
2664
2665 return 1;
2666}
2667
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002668static int fan_write(char *buf)
2669{
2670 char *cmd;
2671 int rc = 0;
2672
2673 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002674 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002675 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002676 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002677 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002678 fan_write_cmd_disable(cmd, &rc) ||
2679 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002680 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002681 fan_write_cmd_speed(cmd, &rc))
2682 )
2683 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002684 else if (!rc)
2685 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002686 }
2687
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002688 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002689}
2690
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002691static struct ibm_struct fan_driver_data = {
2692 .name = "fan",
2693 .read = fan_read,
2694 .write = fan_write,
2695 .exit = fan_exit,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03002696 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002697};
2698
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002699/****************************************************************************
2700 ****************************************************************************
2701 *
2702 * Infrastructure
2703 *
2704 ****************************************************************************
2705 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002707/* /proc support */
2708static struct proc_dir_entry *proc_dir = NULL;
2709
2710/* Subdriver registry */
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002711static LIST_HEAD(tpacpi_all_drivers);
2712
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002714/*
2715 * Module and infrastructure proble, init and exit handling
2716 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002718#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002719static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002720{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002721 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002722
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002723 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002724}
2725#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
2726
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002727static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728{
2729 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002730 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 struct proc_dir_entry *entry;
2732
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002733 BUG_ON(ibm == NULL);
2734
2735 INIT_LIST_HEAD(&ibm->all_drivers);
2736
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03002737 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 return 0;
2739
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002740 dbg_printk(TPACPI_DBG_INIT,
2741 "probing for %s\n", ibm->name);
2742
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002743 if (iibm->init) {
2744 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002745 if (ret > 0)
2746 return 0; /* probe failed */
2747 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002749
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03002750 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 }
2752
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002753 if (ibm->acpi) {
2754 if (ibm->acpi->hid) {
2755 ret = register_tpacpi_subdriver(ibm);
2756 if (ret)
2757 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002758 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002759
2760 if (ibm->acpi->notify) {
2761 ret = setup_acpi_notify(ibm);
2762 if (ret == -ENODEV) {
2763 printk(IBM_NOTICE "disabling subdriver %s\n",
2764 ibm->name);
2765 ret = 0;
2766 goto err_out;
2767 }
2768 if (ret < 0)
2769 goto err_out;
2770 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002771 }
2772
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002773 dbg_printk(TPACPI_DBG_INIT,
2774 "%s installed\n", ibm->name);
2775
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002776 if (ibm->read) {
2777 entry = create_proc_entry(ibm->name,
2778 S_IFREG | S_IRUGO | S_IWUSR,
2779 proc_dir);
2780 if (!entry) {
2781 printk(IBM_ERR "unable to create proc entry %s\n",
2782 ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002783 ret = -ENODEV;
2784 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002785 }
2786 entry->owner = THIS_MODULE;
2787 entry->data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002788 entry->read_proc = &dispatch_procfs_read;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002789 if (ibm->write)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002790 entry->write_proc = &dispatch_procfs_write;
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03002791 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002792 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002794 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
2795
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002797
2798err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002799 dbg_printk(TPACPI_DBG_INIT,
2800 "%s: at error exit path with result %d\n",
2801 ibm->name, ret);
2802
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002803 ibm_exit(ibm);
2804 return (ret < 0)? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805}
2806
2807static void ibm_exit(struct ibm_struct *ibm)
2808{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002809 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002810
2811 list_del_init(&ibm->all_drivers);
2812
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002813 if (ibm->flags.acpi_notify_installed) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002814 dbg_printk(TPACPI_DBG_EXIT,
2815 "%s: acpi_remove_notify_handler\n", ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002816 BUG_ON(!ibm->acpi);
2817 acpi_remove_notify_handler(*ibm->acpi->handle,
2818 ibm->acpi->type,
2819 dispatch_acpi_notify);
2820 ibm->flags.acpi_notify_installed = 0;
2821 ibm->flags.acpi_notify_installed = 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002822 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03002824 if (ibm->flags.proc_created) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002825 dbg_printk(TPACPI_DBG_EXIT,
2826 "%s: remove_proc_entry\n", ibm->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 remove_proc_entry(ibm->name, proc_dir);
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03002828 ibm->flags.proc_created = 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002829 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002831 if (ibm->flags.acpi_driver_registered) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002832 dbg_printk(TPACPI_DBG_EXIT,
2833 "%s: acpi_bus_unregister_driver\n", ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002834 BUG_ON(!ibm->acpi);
2835 acpi_bus_unregister_driver(ibm->acpi->driver);
2836 kfree(ibm->acpi->driver);
2837 ibm->acpi->driver = NULL;
2838 ibm->flags.acpi_driver_registered = 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002839 }
2840
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03002841 if (ibm->flags.init_called && ibm->exit) {
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002842 ibm->exit();
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03002843 ibm->flags.init_called = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002845
2846 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847}
2848
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002849/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002851static char *ibm_thinkpad_ec_found = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02002853static char* __init check_dmi_for_ec(void)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002854{
2855 struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02002856 char ec_fw_string[18];
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002857
2858 /*
2859 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
2860 * X32 or newer, all Z series; Some models must have an
2861 * up-to-date BIOS or they will not be detected.
2862 *
2863 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
2864 */
2865 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02002866 if (sscanf(dev->name,
2867 "IBM ThinkPad Embedded Controller -[%17c",
2868 ec_fw_string) == 1) {
2869 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
2870 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
2871 return kstrdup(ec_fw_string, GFP_KERNEL);
2872 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002873 }
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02002874 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875}
2876
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002877static int __init probe_for_thinkpad(void)
2878{
2879 int is_thinkpad;
2880
2881 if (acpi_disabled)
2882 return -ENODEV;
2883
2884 /*
2885 * Non-ancient models have better DMI tagging, but very old models
2886 * don't.
2887 */
2888 is_thinkpad = dmi_name_in_vendors("ThinkPad");
2889
2890 /* ec is required because many other handles are relative to it */
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002891 IBM_ACPIHANDLE_INIT(ec);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002892 if (!ec_handle) {
2893 if (is_thinkpad)
2894 printk(IBM_ERR
2895 "Not yet supported ThinkPad detected!\n");
2896 return -ENODEV;
2897 }
2898
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03002899 /*
2900 * Risks a regression on very old machines, but reduces potential
2901 * false positives a damn great deal
2902 */
2903 if (!is_thinkpad)
2904 is_thinkpad = dmi_name_in_vendors("IBM");
2905
2906 if (!is_thinkpad && !force_load)
2907 return -ENODEV;
2908
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002909 return 0;
2910}
2911
2912
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002913/* Module init, exit, parameters */
2914
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002915static struct ibm_init_struct ibms_init[] __initdata = {
2916 {
2917 .init = thinkpad_acpi_driver_init,
2918 .data = &thinkpad_acpi_driver_data,
2919 },
2920 {
2921 .init = hotkey_init,
2922 .data = &hotkey_driver_data,
2923 },
2924 {
2925 .init = bluetooth_init,
2926 .data = &bluetooth_driver_data,
2927 },
2928 {
2929 .init = wan_init,
2930 .data = &wan_driver_data,
2931 },
2932 {
2933 .init = video_init,
2934 .data = &video_driver_data,
2935 },
2936 {
2937 .init = light_init,
2938 .data = &light_driver_data,
2939 },
2940#ifdef CONFIG_THINKPAD_ACPI_DOCK
2941 {
2942 .init = dock_init,
2943 .data = &dock_driver_data[0],
2944 },
2945 {
2946 .data = &dock_driver_data[1],
2947 },
2948#endif
2949#ifdef CONFIG_THINKPAD_ACPI_BAY
2950 {
2951 .init = bay_init,
2952 .data = &bay_driver_data,
2953 },
2954#endif
2955 {
2956 .init = cmos_init,
2957 .data = &cmos_driver_data,
2958 },
2959 {
2960 .init = led_init,
2961 .data = &led_driver_data,
2962 },
2963 {
2964 .init = beep_init,
2965 .data = &beep_driver_data,
2966 },
2967 {
2968 .init = thermal_init,
2969 .data = &thermal_driver_data,
2970 },
2971 {
2972 .data = &ecdump_driver_data,
2973 },
2974 {
2975 .init = brightness_init,
2976 .data = &brightness_driver_data,
2977 },
2978 {
2979 .data = &volume_driver_data,
2980 },
2981 {
2982 .init = fan_init,
2983 .data = &fan_driver_data,
2984 },
2985};
2986
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002987static int __init set_ibm_param(const char *val, struct kernel_param *kp)
2988{
2989 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002990 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002991
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002992 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
2993 ibm = ibms_init[i].data;
2994 BUG_ON(ibm == NULL);
2995
2996 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
2997 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002998 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002999 strcpy(ibms_init[i].param, val);
3000 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003001 return 0;
3002 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003003 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003004
3005 return -EINVAL;
3006}
3007
3008static int experimental;
3009module_param(experimental, int, 0);
3010
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03003011static u32 dbg_level;
3012module_param_named(debug, dbg_level, uint, 0);
3013
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03003014static int force_load;
3015module_param(force_load, int, 0);
3016
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003017#define IBM_PARAM(feature) \
3018 module_param_call(feature, set_ibm_param, NULL, NULL, 0)
3019
3020IBM_PARAM(hotkey);
3021IBM_PARAM(bluetooth);
3022IBM_PARAM(video);
3023IBM_PARAM(light);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003024#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003025IBM_PARAM(dock);
3026#endif
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003027#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003028IBM_PARAM(bay);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003029#endif /* CONFIG_THINKPAD_ACPI_BAY */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003030IBM_PARAM(cmos);
3031IBM_PARAM(led);
3032IBM_PARAM(beep);
3033IBM_PARAM(ecdump);
3034IBM_PARAM(brightness);
3035IBM_PARAM(volume);
3036IBM_PARAM(fan);
3037
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03003038static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039{
3040 int ret, i;
3041
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003042 ret = probe_for_thinkpad();
3043 if (ret)
3044 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003046 ibm_thinkpad_ec_found = check_dmi_for_ec();
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003047 IBM_ACPIHANDLE_INIT(ecrd);
3048 IBM_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003050 proc_dir = proc_mkdir(IBM_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 if (!proc_dir) {
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003052 printk(IBM_ERR "unable to create proc dir " IBM_PROC_DIR);
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03003053 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 return -ENODEV;
3055 }
3056 proc_dir->owner = THIS_MODULE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003057
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003058 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
3059 ret = ibm_init(&ibms_init[i]);
3060 if (ret >= 0 && *ibms_init[i].param)
3061 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03003063 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 return ret;
3065 }
3066 }
3067
3068 return 0;
3069}
3070
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03003071static void thinkpad_acpi_module_exit(void)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003072{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003073 struct ibm_struct *ibm, *itmp;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003074
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003075 list_for_each_entry_safe_reverse(ibm, itmp,
3076 &tpacpi_all_drivers,
3077 all_drivers) {
3078 ibm_exit(ibm);
3079 }
3080
3081 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003082
3083 if (proc_dir)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003084 remove_proc_entry(IBM_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003085
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003086 kfree(ibm_thinkpad_ec_found);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003087}
3088
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03003089module_init(thinkpad_acpi_module_init);
3090module_exit(thinkpad_acpi_module_exit);