blob: 9b4eea4c8ff7993f53175432c87eebd10b733092 [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
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -0300276static int issue_thinkpad_cmos_command(int cmos_cmd)
277{
278 if (!cmos_handle)
279 return -ENXIO;
280
281 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
282 return -EIO;
283
284 return 0;
285}
286
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300287/*************************************************************************
288 * ACPI device model
289 */
290
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300291static void drv_acpi_handle_init(char *name,
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300292 acpi_handle *handle, acpi_handle parent,
293 char **paths, int num_paths, char **path)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300294{
295 int i;
296 acpi_status status;
297
298 for (i = 0; i < num_paths; i++) {
299 status = acpi_get_handle(parent, paths[i], handle);
300 if (ACPI_SUCCESS(status)) {
301 *path = paths[i];
302 return;
303 }
304 }
305
306 *handle = NULL;
307}
308
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300309static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300310{
311 struct ibm_struct *ibm = data;
312
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300313 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300314 return;
315
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300316 ibm->acpi->notify(ibm, event);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300317}
318
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300319static int __init setup_acpi_notify(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300320{
321 acpi_status status;
322 int ret;
323
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300324 BUG_ON(!ibm->acpi);
325
326 if (!*ibm->acpi->handle)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300327 return 0;
328
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300329 dbg_printk(TPACPI_DBG_INIT,
330 "setting up ACPI notify for %s\n", ibm->name);
331
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300332 ret = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300333 if (ret < 0) {
334 printk(IBM_ERR "%s device not present\n", ibm->name);
335 return -ENODEV;
336 }
337
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300338 acpi_driver_data(ibm->acpi->device) = ibm;
339 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300340 IBM_ACPI_EVENT_PREFIX,
341 ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300342
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300343 status = acpi_install_notify_handler(*ibm->acpi->handle,
344 ibm->acpi->type, dispatch_acpi_notify, ibm);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300345 if (ACPI_FAILURE(status)) {
346 if (status == AE_ALREADY_EXISTS) {
347 printk(IBM_NOTICE "another device driver is already handling %s events\n",
348 ibm->name);
349 } else {
350 printk(IBM_ERR "acpi_install_notify_handler(%s) failed: %d\n",
351 ibm->name, status);
352 }
353 return -ENODEV;
354 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300355 ibm->flags.acpi_notify_installed = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300356 return 0;
357}
358
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300359static int __init tpacpi_device_add(struct acpi_device *device)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300360{
361 return 0;
362}
363
Henrique de Moraes Holschuh67001212007-04-21 11:08:25 -0300364static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300365{
366 int ret;
367
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300368 dbg_printk(TPACPI_DBG_INIT,
369 "registering %s as an ACPI driver\n", ibm->name);
370
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300371 BUG_ON(!ibm->acpi);
372
373 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
374 if (!ibm->acpi->driver) {
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300375 printk(IBM_ERR "kzalloc(ibm->driver) failed\n");
376 return -ENOMEM;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300377 }
378
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300379 sprintf(ibm->acpi->driver->name, "%s_%s", IBM_NAME, ibm->name);
380 ibm->acpi->driver->ids = ibm->acpi->hid;
381 ibm->acpi->driver->ops.add = &tpacpi_device_add;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300382
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300383 ret = acpi_bus_register_driver(ibm->acpi->driver);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300384 if (ret < 0) {
385 printk(IBM_ERR "acpi_bus_register_driver(%s) failed: %d\n",
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300386 ibm->acpi->hid, ret);
387 kfree(ibm->acpi->driver);
388 ibm->acpi->driver = NULL;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300389 } else if (!ret)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300390 ibm->flags.acpi_driver_registered = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300391
392 return ret;
393}
394
395
396/****************************************************************************
397 ****************************************************************************
398 *
399 * Procfs Helpers
400 *
401 ****************************************************************************
402 ****************************************************************************/
403
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300404static int dispatch_procfs_read(char *page, char **start, off_t off,
405 int count, int *eof, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300406{
407 struct ibm_struct *ibm = data;
408 int len;
409
410 if (!ibm || !ibm->read)
411 return -EINVAL;
412
413 len = ibm->read(page);
414 if (len < 0)
415 return len;
416
417 if (len <= off + count)
418 *eof = 1;
419 *start = page + off;
420 len -= off;
421 if (len > count)
422 len = count;
423 if (len < 0)
424 len = 0;
425
426 return len;
427}
428
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300429static int dispatch_procfs_write(struct file *file,
430 const char __user * userbuf,
431 unsigned long count, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300432{
433 struct ibm_struct *ibm = data;
434 char *kernbuf;
435 int ret;
436
437 if (!ibm || !ibm->write)
438 return -EINVAL;
439
440 kernbuf = kmalloc(count + 2, GFP_KERNEL);
441 if (!kernbuf)
442 return -ENOMEM;
443
444 if (copy_from_user(kernbuf, userbuf, count)) {
445 kfree(kernbuf);
446 return -EFAULT;
447 }
448
449 kernbuf[count] = 0;
450 strcat(kernbuf, ",");
451 ret = ibm->write(kernbuf);
452 if (ret == 0)
453 ret = count;
454
455 kfree(kernbuf);
456
457 return ret;
458}
459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460static char *next_cmd(char **cmds)
461{
462 char *start = *cmds;
463 char *end;
464
465 while ((end = strchr(start, ',')) && end == start)
466 start = end + 1;
467
468 if (!end)
469 return NULL;
470
471 *end = 0;
472 *cmds = end + 1;
473 return start;
474}
475
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300476
477/****************************************************************************
478 ****************************************************************************
479 *
480 * Subdrivers
481 *
482 ****************************************************************************
483 ****************************************************************************/
484
485/*************************************************************************
Henrique de Moraes Holschuh142cfc92007-04-21 11:08:26 -0300486 * thinkpad-acpi init subdriver
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300487 */
488
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300489static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
491 printk(IBM_INFO "%s v%s\n", IBM_DESC, IBM_VERSION);
492 printk(IBM_INFO "%s\n", IBM_URL);
493
Henrique de Moraes Holschuh3945ac32007-02-06 19:13:44 -0200494 if (ibm_thinkpad_ec_found)
495 printk(IBM_INFO "ThinkPad EC firmware %s\n",
496 ibm_thinkpad_ec_found);
497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 return 0;
499}
500
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300501static int thinkpad_acpi_driver_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
503 int len = 0;
504
505 len += sprintf(p + len, "driver:\t\t%s\n", IBM_DESC);
506 len += sprintf(p + len, "version:\t%s\n", IBM_VERSION);
507
508 return len;
509}
510
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300511static struct ibm_struct thinkpad_acpi_driver_data = {
512 .name = "driver",
513 .read = thinkpad_acpi_driver_read,
514};
515
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300516/*************************************************************************
517 * Hotkey subdriver
518 */
519
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400520static int hotkey_orig_status;
521static int hotkey_orig_mask;
522
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300523static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300524{
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300525 int res;
526
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300527 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
528
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300529 IBM_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300530
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300531 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300532 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300533
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300534 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300535 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300536
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300537 if (tp_features.hotkey) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300538 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
539 A30, R30, R31, T20-22, X20-21, X22-24 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300540 tp_features.hotkey_mask =
541 acpi_evalf(hkey_handle, NULL, "DHKN", "qv");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300542
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300543 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300544 str_supported(tp_features.hotkey_mask));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300545
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300546 res = hotkey_get(&hotkey_orig_status, &hotkey_orig_mask);
547 if (res)
548 return res;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300549 }
550
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300551 return (tp_features.hotkey)? 0 : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300552}
553
554static void hotkey_exit(void)
555{
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300556 int res;
557
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300558 if (tp_features.hotkey) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300559 dbg_printk(TPACPI_DBG_EXIT, "restoring original hotkey mask\n");
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300560 res = hotkey_set(hotkey_orig_status, hotkey_orig_mask);
561 if (res)
562 printk(IBM_ERR "failed to restore hotkey to BIOS defaults\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300563 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300564}
565
566static void hotkey_notify(struct ibm_struct *ibm, u32 event)
567{
568 int hkey;
569
570 if (acpi_evalf(hkey_handle, &hkey, "MHKP", "d"))
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300571 acpi_bus_generate_event(ibm->acpi->device, event, hkey);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300572 else {
573 printk(IBM_ERR "unknown hotkey event %d\n", event);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300574 acpi_bus_generate_event(ibm->acpi->device, event, 0);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300575 }
576}
577
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400578static int hotkey_get(int *status, int *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
580 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300581 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400582
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300583 if (tp_features.hotkey_mask)
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400584 if (!acpi_evalf(hkey_handle, mask, "DHKN", "d"))
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300585 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400586
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300587 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588}
589
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400590static int hotkey_set(int status, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
592 int i;
593
594 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300595 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300597 if (tp_features.hotkey_mask)
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400598 for (i = 0; i < 32; i++) {
599 int bit = ((1 << i) & mask) != 0;
600 if (!acpi_evalf(hkey_handle,
601 NULL, "MHKM", "vdd", i + 1, bit))
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300602 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400603 }
604
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300605 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400606}
607
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400608static int hotkey_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609{
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300610 int res, status, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 int len = 0;
612
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300613 if (!tp_features.hotkey) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400614 len += sprintf(p + len, "status:\t\tnot supported\n");
615 return len;
616 }
617
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300618 res = hotkey_get(&status, &mask);
619 if (res)
620 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
622 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300623 if (tp_features.hotkey_mask) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 len += sprintf(p + len, "mask:\t\t0x%04x\n", mask);
625 len += sprintf(p + len,
626 "commands:\tenable, disable, reset, <mask>\n");
627 } else {
628 len += sprintf(p + len, "mask:\t\tnot supported\n");
629 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
630 }
631
632 return len;
633}
634
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400635static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636{
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300637 int res, status, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 char *cmd;
639 int do_cmd = 0;
640
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300641 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 return -ENODEV;
643
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300644 res = hotkey_get(&status, &mask);
645 if (res)
646 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400647
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 while ((cmd = next_cmd(&buf))) {
649 if (strlencmp(cmd, "enable") == 0) {
650 status = 1;
651 } else if (strlencmp(cmd, "disable") == 0) {
652 status = 0;
653 } else if (strlencmp(cmd, "reset") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400654 status = hotkey_orig_status;
655 mask = hotkey_orig_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
657 /* mask set */
658 } else if (sscanf(cmd, "%x", &mask) == 1) {
659 /* mask set */
660 } else
661 return -EINVAL;
662 do_cmd = 1;
663 }
664
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300665 if (do_cmd) {
666 res = hotkey_set(status, mask);
667 if (res)
668 return res;
669 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
671 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400672}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300674static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300675 .hid = IBM_HKEY_HID,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300676 .notify = hotkey_notify,
677 .handle = &hkey_handle,
678 .type = ACPI_DEVICE_NOTIFY,
679};
680
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300681static struct ibm_struct hotkey_driver_data = {
682 .name = "hotkey",
683 .read = hotkey_read,
684 .write = hotkey_write,
685 .exit = hotkey_exit,
686 .acpi = &ibm_hotkey_acpidriver,
687};
688
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300689/*************************************************************************
690 * Bluetooth subdriver
691 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300693static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694{
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -0300695 int status = 0;
696
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300697 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
698
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300699 IBM_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300700
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400701 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
702 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300703 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -0300704 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -0300706 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
707 str_supported(tp_features.bluetooth),
708 status);
709
710 if (tp_features.bluetooth &&
711 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
712 /* no bluetooth hardware present in system */
713 tp_features.bluetooth = 0;
714 dbg_printk(TPACPI_DBG_INIT,
715 "bluetooth hardware not installed\n");
716 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300717
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300718 return (tp_features.bluetooth)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719}
720
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -0300721static int bluetooth_get_radiosw(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722{
723 int status;
724
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -0300725 if (!tp_features.bluetooth)
726 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -0300728 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
729 return -EIO;
730
731 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
732}
733
734static int bluetooth_set_radiosw(int radio_on)
735{
736 int status;
737
738 if (!tp_features.bluetooth)
739 return -ENODEV;
740
741 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
742 return -EIO;
743 if (radio_on)
744 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
745 else
746 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
747 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
748 return -EIO;
749
750 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751}
752
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400753static int bluetooth_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754{
755 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -0300756 int status = bluetooth_get_radiosw();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300758 if (!tp_features.bluetooth)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 len += sprintf(p + len, "status:\t\tnot supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -0300761 len += sprintf(p + len, "status:\t\t%s\n",
762 (status)? "enabled" : "disabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 len += sprintf(p + len, "commands:\tenable, disable\n");
764 }
765
766 return len;
767}
768
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400769static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300773 if (!tp_features.bluetooth)
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400774 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
776 while ((cmd = next_cmd(&buf))) {
777 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -0300778 bluetooth_set_radiosw(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -0300780 bluetooth_set_radiosw(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 } else
782 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 }
784
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 return 0;
786}
787
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300788static struct ibm_struct bluetooth_driver_data = {
789 .name = "bluetooth",
790 .read = bluetooth_read,
791 .write = bluetooth_write,
792};
793
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300794/*************************************************************************
795 * Wan subdriver
796 */
797
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300798static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -0400799{
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -0300800 int status = 0;
801
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300802 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
803
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300804 IBM_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300805
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300806 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -0300807 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -0400808
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -0300809 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
810 str_supported(tp_features.wan),
811 status);
812
813 if (tp_features.wan &&
814 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
815 /* no wan hardware present in system */
816 tp_features.wan = 0;
817 dbg_printk(TPACPI_DBG_INIT,
818 "wan hardware not installed\n");
819 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300820
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300821 return (tp_features.wan)? 0 : 1;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -0400822}
823
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -0300824static int wan_get_radiosw(void)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -0400825{
826 int status;
827
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -0300828 if (!tp_features.wan)
829 return -ENODEV;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -0400830
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -0300831 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
832 return -EIO;
833
834 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
835}
836
837static int wan_set_radiosw(int radio_on)
838{
839 int status;
840
841 if (!tp_features.wan)
842 return -ENODEV;
843
844 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
845 return -EIO;
846 if (radio_on)
847 status |= TP_ACPI_WANCARD_RADIOSSW;
848 else
849 status &= ~TP_ACPI_WANCARD_RADIOSSW;
850 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
851 return -EIO;
852
853 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -0400854}
855
856static int wan_read(char *p)
857{
858 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -0300859 int status = wan_get_radiosw();
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -0400860
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300861 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -0400862 len += sprintf(p + len, "status:\t\tnot supported\n");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -0400863 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -0300864 len += sprintf(p + len, "status:\t\t%s\n",
865 (status)? "enabled" : "disabled");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -0400866 len += sprintf(p + len, "commands:\tenable, disable\n");
867 }
868
869 return len;
870}
871
872static int wan_write(char *buf)
873{
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -0400874 char *cmd;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -0400875
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300876 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -0400877 return -ENODEV;
878
879 while ((cmd = next_cmd(&buf))) {
880 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -0300881 wan_set_radiosw(1);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -0400882 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -0300883 wan_set_radiosw(0);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -0400884 } else
885 return -EINVAL;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -0400886 }
887
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -0400888 return 0;
889}
890
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300891static struct ibm_struct wan_driver_data = {
892 .name = "wan",
893 .read = wan_read,
894 .write = wan_write,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -0300895 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300896};
897
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300898/*************************************************************************
899 * Video subdriver
900 */
901
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200902static enum video_access_mode video_supported;
903static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400904
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300905IBM_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
906 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
907 "\\_SB.PCI0.VID0", /* 770e */
908 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
909 "\\_SB.PCI0.AGP.VID", /* all others */
910 ); /* R30, R31 */
911
912IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
913
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300914static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400916 int ivga;
917
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300918 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
919
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300920 IBM_ACPIHANDLE_INIT(vid);
921 IBM_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300922
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400923 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
924 /* G41, assume IVGA doesn't change */
925 vid_handle = vid2_handle;
926
927 if (!vid_handle)
928 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300929 video_supported = TPACPI_VIDEO_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400930 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
931 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300932 video_supported = TPACPI_VIDEO_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400933 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
934 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300935 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400936 else
937 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300938 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300940 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
941 str_supported(video_supported != TPACPI_VIDEO_NONE),
942 video_supported);
943
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300944 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945}
946
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300947static void video_exit(void)
948{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -0300949 dbg_printk(TPACPI_DBG_EXIT,
950 "restoring original video autoswitch mode\n");
951 if (video_autosw_set(video_orig_autosw))
952 printk(IBM_ERR "error while trying to restore original "
953 "video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300954}
955
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -0300956static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957{
958 int status = 0;
959 int i;
960
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -0300961 switch (video_supported) {
962 case TPACPI_VIDEO_570:
963 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
964 TP_ACPI_VIDEO_570_PHSCMD))
965 return -EIO;
966 status = i & TP_ACPI_VIDEO_570_PHSMASK;
967 break;
968 case TPACPI_VIDEO_770:
969 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
970 return -EIO;
971 if (i)
972 status |= TP_ACPI_VIDEO_S_LCD;
973 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
974 return -EIO;
975 if (i)
976 status |= TP_ACPI_VIDEO_S_CRT;
977 break;
978 case TPACPI_VIDEO_NEW:
979 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
980 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
981 return -EIO;
982 if (i)
983 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -0300985 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
986 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
987 return -EIO;
988 if (i)
989 status |= TP_ACPI_VIDEO_S_LCD;
990 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
991 return -EIO;
992 if (i)
993 status |= TP_ACPI_VIDEO_S_DVI;
994 break;
995 default:
996 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400997 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
999 return status;
1000}
1001
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001002static int video_outputsw_set(int status)
1003{
1004 int autosw;
1005 int res = 0;
1006
1007 switch (video_supported) {
1008 case TPACPI_VIDEO_570:
1009 res = acpi_evalf(NULL, NULL,
1010 "\\_SB.PHS2", "vdd",
1011 TP_ACPI_VIDEO_570_PHS2CMD,
1012 status | TP_ACPI_VIDEO_570_PHS2SET);
1013 break;
1014 case TPACPI_VIDEO_770:
1015 autosw = video_autosw_get();
1016 if (autosw < 0)
1017 return autosw;
1018
1019 res = video_autosw_set(1);
1020 if (res)
1021 return res;
1022 res = acpi_evalf(vid_handle, NULL,
1023 "ASWT", "vdd", status * 0x100, 0);
1024 if (!autosw && video_autosw_set(autosw)) {
1025 printk(IBM_ERR "video auto-switch left enabled due to error\n");
1026 return -EIO;
1027 }
1028 break;
1029 case TPACPI_VIDEO_NEW:
1030 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
1031 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
1032 break;
1033 default:
1034 return -ENOSYS;
1035 }
1036
1037 return (res)? 0 : -EIO;
1038}
1039
1040static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001042 int autosw = 0;
1043
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001044 switch (video_supported) {
1045 case TPACPI_VIDEO_570:
1046 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
1047 return -EIO;
1048 break;
1049 case TPACPI_VIDEO_770:
1050 case TPACPI_VIDEO_NEW:
1051 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
1052 return -EIO;
1053 break;
1054 default:
1055 return -ENOSYS;
1056 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001057
1058 return autosw & 1;
1059}
1060
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001061static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001062{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001063 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001064 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001065 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001066}
1067
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001068static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001069{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001070 int autosw = video_autosw_get();
1071 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001072
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001073 if (autosw < 0)
1074 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001075
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001076 switch (video_supported) {
1077 case TPACPI_VIDEO_570:
1078 res = video_autosw_set(1);
1079 if (res)
1080 return res;
1081 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
1082 break;
1083 case TPACPI_VIDEO_770:
1084 case TPACPI_VIDEO_NEW:
1085 res = video_autosw_set(1);
1086 if (res)
1087 return res;
1088 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
1089 break;
1090 default:
1091 return -ENOSYS;
1092 }
1093 if (!autosw && video_autosw_set(autosw)) {
1094 printk(IBM_ERR "video auto-switch left enabled due to error\n");
1095 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001096 }
1097
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001098 return (res)? 0 : -EIO;
1099}
1100
1101static int video_expand_toggle(void)
1102{
1103 switch (video_supported) {
1104 case TPACPI_VIDEO_570:
1105 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
1106 0 : -EIO;
1107 case TPACPI_VIDEO_770:
1108 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
1109 0 : -EIO;
1110 case TPACPI_VIDEO_NEW:
1111 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
1112 0 : -EIO;
1113 default:
1114 return -ENOSYS;
1115 }
1116 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001117}
1118
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001119static int video_read(char *p)
1120{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001121 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001122 int len = 0;
1123
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001124 if (video_supported == TPACPI_VIDEO_NONE) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001125 len += sprintf(p + len, "status:\t\tnot supported\n");
1126 return len;
1127 }
1128
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001129 status = video_outputsw_get();
1130 if (status < 0)
1131 return status;
1132
1133 autosw = video_autosw_get();
1134 if (autosw < 0)
1135 return autosw;
1136
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001137 len += sprintf(p + len, "status:\t\tsupported\n");
1138 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
1139 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001140 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001141 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
1142 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
1143 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
1144 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001145 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001146 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
1147 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
1148 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
1149
1150 return len;
1151}
1152
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001153static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154{
1155 char *cmd;
1156 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001157 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001159 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001160 return -ENODEV;
1161
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001162 enable = 0;
1163 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
1165 while ((cmd = next_cmd(&buf))) {
1166 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001167 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001169 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001171 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001173 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001174 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001175 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001176 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001177 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001178 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001179 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001181 res = video_autosw_set(1);
1182 if (res)
1183 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001185 res = video_autosw_set(0);
1186 if (res)
1187 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001189 res = video_outputsw_cycle();
1190 if (res)
1191 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001193 res = video_expand_toggle();
1194 if (res)
1195 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 } else
1197 return -EINVAL;
1198 }
1199
1200 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001201 status = video_outputsw_get();
1202 if (status < 0)
1203 return status;
1204 res = video_outputsw_set((status & ~disable) | enable);
1205 if (res)
1206 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 }
1208
1209 return 0;
1210}
1211
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001212static struct ibm_struct video_driver_data = {
1213 .name = "video",
1214 .read = video_read,
1215 .write = video_write,
1216 .exit = video_exit,
1217};
1218
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001219/*************************************************************************
1220 * Light (thinklight) subdriver
1221 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001223IBM_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
1224IBM_HANDLE(ledb, ec, "LEDB"); /* G4x */
1225
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001226static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001228 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
1229
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001230 IBM_ACPIHANDLE_INIT(ledb);
1231 IBM_ACPIHANDLE_INIT(lght);
1232 IBM_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001233
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001234 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001235 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001236
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001237 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001238 /* light status not supported on
1239 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001240 tp_features.light_status =
1241 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001243 vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001244 str_supported(tp_features.light));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001245
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001246 return (tp_features.light)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247}
1248
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001249static int light_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250{
1251 int len = 0;
1252 int status = 0;
1253
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001254 if (!tp_features.light) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001255 len += sprintf(p + len, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001256 } else if (!tp_features.light_status) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001257 len += sprintf(p + len, "status:\t\tunknown\n");
1258 len += sprintf(p + len, "commands:\ton, off\n");
1259 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
1261 return -EIO;
1262 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001263 len += sprintf(p + len, "commands:\ton, off\n");
1264 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
1266 return len;
1267}
1268
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001269static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270{
1271 int cmos_cmd, lght_cmd;
1272 char *cmd;
1273 int success;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001274
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001275 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001276 return -ENODEV;
1277
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 while ((cmd = next_cmd(&buf))) {
1279 if (strlencmp(cmd, "on") == 0) {
1280 cmos_cmd = 0x0c;
1281 lght_cmd = 1;
1282 } else if (strlencmp(cmd, "off") == 0) {
1283 cmos_cmd = 0x0d;
1284 lght_cmd = 0;
1285 } else
1286 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001287
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 success = cmos_handle ?
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001289 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
1290 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 if (!success)
1292 return -EIO;
1293 }
1294
1295 return 0;
1296}
1297
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001298static struct ibm_struct light_driver_data = {
1299 .name = "light",
1300 .read = light_read,
1301 .write = light_write,
1302};
1303
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001304/*************************************************************************
1305 * Dock subdriver
1306 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03001308#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001309
1310IBM_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
1311 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
1312 "\\_SB.PCI0.PCI1.DOCK", /* all others */
1313 "\\_SB.PCI.ISA.SLCE", /* 570 */
1314 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
1315
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001316/* don't list other alternatives as we install a notify handler on the 570 */
1317IBM_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
1318
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319#define dock_docked() (_sta(dock_handle) & 1)
1320
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001321static int __init dock_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001322{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001323 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
1324
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001325 IBM_ACPIHANDLE_INIT(dock);
1326 IBM_ACPIHANDLE_INIT(pci);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001327
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001328 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
1329 str_supported(dock_handle != NULL));
1330
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001331 return (dock_handle)? 0 : 1;
1332}
1333
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001334static void dock_notify(struct ibm_struct *ibm, u32 event)
1335{
1336 int docked = dock_docked();
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001337 int pci = ibm->acpi->hid && strstr(ibm->acpi->hid, IBM_PCI_HID);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001338
1339 if (event == 1 && !pci) /* 570 */
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001340 acpi_bus_generate_event(ibm->acpi->device, event, 1); /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001341 else if (event == 1 && pci) /* 570 */
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001342 acpi_bus_generate_event(ibm->acpi->device, event, 3); /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001343 else if (event == 3 && docked)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001344 acpi_bus_generate_event(ibm->acpi->device, event, 1); /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001345 else if (event == 3 && !docked)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001346 acpi_bus_generate_event(ibm->acpi->device, event, 2); /* undock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001347 else if (event == 0 && docked)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001348 acpi_bus_generate_event(ibm->acpi->device, event, 3); /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001349 else {
1350 printk(IBM_ERR "unknown dock event %d, status %d\n",
1351 event, _sta(dock_handle));
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001352 acpi_bus_generate_event(ibm->acpi->device, event, 0); /* unknown */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001353 }
1354}
1355
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001356static int dock_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357{
1358 int len = 0;
1359 int docked = dock_docked();
1360
1361 if (!dock_handle)
1362 len += sprintf(p + len, "status:\t\tnot supported\n");
1363 else if (!docked)
1364 len += sprintf(p + len, "status:\t\tundocked\n");
1365 else {
1366 len += sprintf(p + len, "status:\t\tdocked\n");
1367 len += sprintf(p + len, "commands:\tdock, undock\n");
1368 }
1369
1370 return len;
1371}
1372
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001373static int dock_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374{
1375 char *cmd;
1376
1377 if (!dock_docked())
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001378 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
1380 while ((cmd = next_cmd(&buf))) {
1381 if (strlencmp(cmd, "undock") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001382 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
1383 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 return -EIO;
1385 } else if (strlencmp(cmd, "dock") == 0) {
1386 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
1387 return -EIO;
1388 } else
1389 return -EINVAL;
1390 }
1391
1392 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001393}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001395static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001396 {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001397 .notify = dock_notify,
1398 .handle = &dock_handle,
1399 .type = ACPI_SYSTEM_NOTIFY,
1400 },
1401 {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001402 .hid = IBM_PCI_HID,
1403 .notify = dock_notify,
1404 .handle = &pci_handle,
1405 .type = ACPI_SYSTEM_NOTIFY,
1406 },
1407};
1408
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001409static struct ibm_struct dock_driver_data[2] = {
1410 {
1411 .name = "dock",
1412 .read = dock_read,
1413 .write = dock_write,
1414 .acpi = &ibm_dock_acpidriver[0],
1415 },
1416 {
1417 .name = "dock",
1418 .acpi = &ibm_dock_acpidriver[1],
1419 },
1420};
1421
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03001422#endif /* CONFIG_THINKPAD_ACPI_DOCK */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001424/*************************************************************************
1425 * Bay subdriver
1426 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03001428#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001429IBM_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
1430 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
1431 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
1432 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
1433 ); /* A21e, R30, R31 */
1434IBM_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
1435 "_EJ0", /* all others */
1436 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
1437IBM_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
1438 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
1439 ); /* all others */
1440IBM_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
1441 "_EJ0", /* 770x */
1442 ); /* all others */
1443
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001444static int __init bay_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001446 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
1447
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001448 IBM_ACPIHANDLE_INIT(bay);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001449 if (bay_handle)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001450 IBM_ACPIHANDLE_INIT(bay_ej);
1451 IBM_ACPIHANDLE_INIT(bay2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001452 if (bay2_handle)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001453 IBM_ACPIHANDLE_INIT(bay2_ej);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001454
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001455 tp_features.bay_status = bay_handle &&
1456 acpi_evalf(bay_handle, NULL, "_STA", "qv");
1457 tp_features.bay_status2 = bay2_handle &&
1458 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001459
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001460 tp_features.bay_eject = bay_handle && bay_ej_handle &&
1461 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
1462 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
1463 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001465 vdbg_printk(TPACPI_DBG_INIT,
1466 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001467 str_supported(tp_features.bay_status),
1468 str_supported(tp_features.bay_eject),
1469 str_supported(tp_features.bay_status2),
1470 str_supported(tp_features.bay_eject2));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001471
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001472 return (tp_features.bay_status || tp_features.bay_eject ||
1473 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474}
1475
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001476static void bay_notify(struct ibm_struct *ibm, u32 event)
1477{
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001478 acpi_bus_generate_event(ibm->acpi->device, event, 0);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001479}
1480
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001481#define bay_occupied(b) (_sta(b##_handle) & 1)
1482
1483static int bay_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484{
1485 int len = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001486 int occupied = bay_occupied(bay);
1487 int occupied2 = bay_occupied(bay2);
1488 int eject, eject2;
1489
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001490 len += sprintf(p + len, "status:\t\t%s\n",
1491 tp_features.bay_status ?
1492 (occupied ? "occupied" : "unoccupied") :
1493 "not supported");
1494 if (tp_features.bay_status2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001495 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
1496 "occupied" : "unoccupied");
1497
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001498 eject = tp_features.bay_eject && occupied;
1499 eject2 = tp_features.bay_eject2 && occupied2;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001500
1501 if (eject && eject2)
1502 len += sprintf(p + len, "commands:\teject, eject2\n");
1503 else if (eject)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 len += sprintf(p + len, "commands:\teject\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001505 else if (eject2)
1506 len += sprintf(p + len, "commands:\teject2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
1508 return len;
1509}
1510
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001511static int bay_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512{
1513 char *cmd;
1514
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001515 if (!tp_features.bay_eject && !tp_features.bay_eject2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001516 return -ENODEV;
1517
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001519 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001520 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
1521 return -EIO;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001522 } else if (tp_features.bay_eject2 &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001523 strlencmp(cmd, "eject2") == 0) {
1524 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 return -EIO;
1526 } else
1527 return -EINVAL;
1528 }
1529
1530 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001531}
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001532
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001533static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
1534 .notify = bay_notify,
1535 .handle = &bay_handle,
1536 .type = ACPI_SYSTEM_NOTIFY,
1537};
1538
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001539static struct ibm_struct bay_driver_data = {
1540 .name = "bay",
1541 .read = bay_read,
1542 .write = bay_write,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001543 .acpi = &ibm_bay_acpidriver,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001544};
1545
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03001546#endif /* CONFIG_THINKPAD_ACPI_BAY */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001548/*************************************************************************
1549 * CMOS subdriver
1550 */
1551
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001552static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001553{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001554 vdbg_printk(TPACPI_DBG_INIT,
1555 "initializing cmos commands subdriver\n");
1556
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001557 IBM_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001558
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001559 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
1560 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001561 return (cmos_handle)? 0 : 1;
1562}
1563
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001564static int cmos_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565{
1566 int len = 0;
1567
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001568 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1569 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 if (!cmos_handle)
1571 len += sprintf(p + len, "status:\t\tnot supported\n");
1572 else {
1573 len += sprintf(p + len, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001574 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 }
1576
1577 return len;
1578}
1579
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001580static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581{
1582 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03001583 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
1585 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001586 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
1587 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 /* cmos_cmd set */
1589 } else
1590 return -EINVAL;
1591
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03001592 res = issue_thinkpad_cmos_command(cmos_cmd);
1593 if (res)
1594 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 }
1596
1597 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001598}
1599
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001600static struct ibm_struct cmos_driver_data = {
1601 .name = "cmos",
1602 .read = cmos_read,
1603 .write = cmos_write,
1604};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001605
1606/*************************************************************************
1607 * LED subdriver
1608 */
1609
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02001610static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001611
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001612IBM_HANDLE(led, ec, "SLED", /* 570 */
1613 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
1614 "LED", /* all others */
1615 ); /* R30, R31 */
1616
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001617static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001618{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001619 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
1620
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001621 IBM_ACPIHANDLE_INIT(led);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001622
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001623 if (!led_handle)
1624 /* led not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001625 led_supported = TPACPI_LED_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001626 else if (strlencmp(led_path, "SLED") == 0)
1627 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001628 led_supported = TPACPI_LED_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001629 else if (strlencmp(led_path, "SYSL") == 0)
1630 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001631 led_supported = TPACPI_LED_OLD;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001632 else
1633 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001634 led_supported = TPACPI_LED_NEW;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001635
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001636 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
1637 str_supported(led_supported), led_supported);
1638
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001639 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001640}
1641
1642#define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
1643
1644static int led_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645{
1646 int len = 0;
1647
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001648 if (!led_supported) {
1649 len += sprintf(p + len, "status:\t\tnot supported\n");
1650 return len;
1651 }
1652 len += sprintf(p + len, "status:\t\tsupported\n");
1653
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001654 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001655 /* 570 */
1656 int i, status;
1657 for (i = 0; i < 8; i++) {
1658 if (!acpi_evalf(ec_handle,
1659 &status, "GLED", "dd", 1 << i))
1660 return -EIO;
1661 len += sprintf(p + len, "%d:\t\t%s\n",
1662 i, led_status(status));
1663 }
1664 }
1665
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 len += sprintf(p + len, "commands:\t"
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001667 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668
1669 return len;
1670}
1671
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001672/* off, on, blink */
1673static const int led_sled_arg1[] = { 0, 1, 3 };
1674static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
1675static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
1676static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
1677
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001678static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679{
1680 char *cmd;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001681 int led, ind, ret;
1682
1683 if (!led_supported)
1684 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685
1686 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001687 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 return -EINVAL;
1689
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001690 if (strstr(cmd, "off")) {
1691 ind = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 } else if (strstr(cmd, "on")) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001693 ind = 1;
1694 } else if (strstr(cmd, "blink")) {
1695 ind = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 } else
1697 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001698
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001699 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001700 /* 570 */
1701 led = 1 << led;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001703 led, led_sled_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 return -EIO;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001705 } else if (led_supported == TPACPI_LED_OLD) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001706 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
1707 led = 1 << led;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001708 ret = ec_write(TPACPI_LED_EC_HLMS, led);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001709 if (ret >= 0)
1710 ret =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001711 ec_write(TPACPI_LED_EC_HLBL,
Henrique de Moraes Holschuhe062e032007-03-23 17:33:55 -03001712 led * led_exp_hlbl[ind]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001713 if (ret >= 0)
1714 ret =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001715 ec_write(TPACPI_LED_EC_HLCL,
Henrique de Moraes Holschuhe062e032007-03-23 17:33:55 -03001716 led * led_exp_hlcl[ind]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001717 if (ret < 0)
1718 return ret;
1719 } else {
1720 /* all others */
1721 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
1722 led, led_led_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001724 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 }
1726
1727 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001728}
1729
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001730static struct ibm_struct led_driver_data = {
1731 .name = "led",
1732 .read = led_read,
1733 .write = led_write,
1734};
1735
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001736/*************************************************************************
1737 * Beep subdriver
1738 */
1739
1740IBM_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
1741
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001742static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001743{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001744 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
1745
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001746 IBM_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001747
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001748 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
1749 str_supported(beep_handle != NULL));
1750
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001751 return (beep_handle)? 0 : 1;
1752}
1753
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001754static int beep_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755{
1756 int len = 0;
1757
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001758 if (!beep_handle)
1759 len += sprintf(p + len, "status:\t\tnot supported\n");
1760 else {
1761 len += sprintf(p + len, "status:\t\tsupported\n");
1762 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
1763 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764
1765 return len;
1766}
1767
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001768static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769{
1770 char *cmd;
1771 int beep_cmd;
1772
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001773 if (!beep_handle)
1774 return -ENODEV;
1775
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001777 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
1778 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 /* beep_cmd set */
1780 } else
1781 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001782 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 return -EIO;
1784 }
1785
1786 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001787}
1788
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001789static struct ibm_struct beep_driver_data = {
1790 .name = "beep",
1791 .read = beep_read,
1792 .write = beep_write,
1793};
1794
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001795/*************************************************************************
1796 * Thermal subdriver
1797 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001798
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001799static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001800
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001801static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001802{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001803 u8 t, ta1, ta2;
1804 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001805 int acpi_tmp7;
1806
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001807 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
1808
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001809 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001810
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001811 if (ibm_thinkpad_ec_found && experimental) {
1812 /*
1813 * Direct EC access mode: sensors at registers
1814 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
1815 * non-implemented, thermal sensors return 0x80 when
1816 * not available
1817 */
1818
1819 ta1 = ta2 = 0;
1820 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03001821 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001822 ta1 |= t;
1823 } else {
1824 ta1 = 0;
1825 break;
1826 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03001827 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001828 ta2 |= t;
1829 } else {
1830 ta1 = 0;
1831 break;
1832 }
1833 }
1834 if (ta1 == 0) {
1835 /* This is sheer paranoia, but we handle it anyway */
1836 if (acpi_tmp7) {
1837 printk(IBM_ERR
1838 "ThinkPad ACPI EC access misbehaving, "
1839 "falling back to ACPI TMPx access mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001840 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001841 } else {
1842 printk(IBM_ERR
1843 "ThinkPad ACPI EC access misbehaving, "
1844 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001845 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001846 }
1847 } else {
1848 thermal_read_mode =
1849 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001850 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001851 }
1852 } else if (acpi_tmp7) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001853 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
1854 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001855 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001856 } else {
1857 /* Standard ACPI TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001858 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001859 }
1860 } else {
1861 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001862 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001863 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001864
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001865 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
1866 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
1867 thermal_read_mode);
1868
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001869 return (thermal_read_mode != TPACPI_THERMAL_NONE)? 0 : 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001870}
1871
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03001872/* idx is zero-based */
1873static int thermal_get_sensor(int idx, s32 *value)
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001874{
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03001875 int t;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001876 s8 tmp;
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03001877 char tmpi[5];
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001878
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03001879 t = TP_EC_THERMAL_TMP0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001880
1881 switch (thermal_read_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001882#if TPACPI_MAX_THERMAL_SENSORS >= 16
1883 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03001884 if (idx >= 8 && idx <= 15) {
1885 t = TP_EC_THERMAL_TMP8;
1886 idx -= 8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001887 }
1888 /* fallthrough */
1889#endif
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001890 case TPACPI_THERMAL_TPEC_8:
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03001891 if (idx <= 7) {
1892 if (!acpi_ec_read(t + idx, &tmp))
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001893 return -EIO;
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03001894 *value = tmp * 1000;
1895 return 0;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001896 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03001897 break;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001898
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001899 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03001900 if (idx <= 7) {
1901 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
1902 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
1903 return -EIO;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001904 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
1905 return -EIO;
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03001906 *value = (t - 2732) * 100;
1907 return 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001908 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03001909 break;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001910
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001911 case TPACPI_THERMAL_ACPI_TMP07:
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03001912 if (idx <= 7) {
1913 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001914 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
1915 return -EIO;
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03001916 *value = t * 1000;
1917 return 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001918 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03001919 break;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001920
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001921 case TPACPI_THERMAL_NONE:
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001922 default:
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03001923 return -ENOSYS;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001924 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03001925
1926 return -EINVAL;
1927}
1928
1929static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
1930{
1931 int res, i;
1932 int n;
1933
1934 n = 8;
1935 i = 0;
1936
1937 if (!s)
1938 return -EINVAL;
1939
1940 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
1941 n = 16;
1942
1943 for(i = 0 ; i < n; i++) {
1944 res = thermal_get_sensor(i, &s->temp[i]);
1945 if (res)
1946 return res;
1947 }
1948
1949 return n;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001950}
1951
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001952static int thermal_read(char *p)
1953{
1954 int len = 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001955 int n, i;
1956 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001957
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001958 n = thermal_get_sensors(&t);
1959 if (unlikely(n < 0))
1960 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001961
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001962 len += sprintf(p + len, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001963
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001964 if (n > 0) {
1965 for (i = 0; i < (n - 1); i++)
1966 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
1967 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
1968 } else
1969 len += sprintf(p + len, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001970
1971 return len;
1972}
1973
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001974static struct ibm_struct thermal_driver_data = {
1975 .name = "thermal",
1976 .read = thermal_read,
1977};
1978
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001979/*************************************************************************
1980 * EC Dump subdriver
1981 */
1982
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001983static u8 ecdump_regs[256];
1984
1985static int ecdump_read(char *p)
1986{
1987 int len = 0;
1988 int i, j;
1989 u8 v;
1990
1991 len += sprintf(p + len, "EC "
1992 " +00 +01 +02 +03 +04 +05 +06 +07"
1993 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
1994 for (i = 0; i < 256; i += 16) {
1995 len += sprintf(p + len, "EC 0x%02x:", i);
1996 for (j = 0; j < 16; j++) {
1997 if (!acpi_ec_read(i + j, &v))
1998 break;
1999 if (v != ecdump_regs[i + j])
2000 len += sprintf(p + len, " *%02x", v);
2001 else
2002 len += sprintf(p + len, " %02x", v);
2003 ecdump_regs[i + j] = v;
2004 }
2005 len += sprintf(p + len, "\n");
2006 if (j != 16)
2007 break;
2008 }
2009
2010 /* These are way too dangerous to advertise openly... */
2011#if 0
2012 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
2013 " (<offset> is 00-ff, <value> is 00-ff)\n");
2014 len += sprintf(p + len, "commands:\t0x<offset> <value> "
2015 " (<offset> is 00-ff, <value> is 0-255)\n");
2016#endif
2017 return len;
2018}
2019
2020static int ecdump_write(char *buf)
2021{
2022 char *cmd;
2023 int i, v;
2024
2025 while ((cmd = next_cmd(&buf))) {
2026 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
2027 /* i and v set */
2028 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
2029 /* i and v set */
2030 } else
2031 return -EINVAL;
2032 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
2033 if (!acpi_ec_write(i, v))
2034 return -EIO;
2035 } else
2036 return -EINVAL;
2037 }
2038
2039 return 0;
2040}
2041
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002042static struct ibm_struct ecdump_driver_data = {
2043 .name = "ecdump",
2044 .read = ecdump_read,
2045 .write = ecdump_write,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03002046 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002047};
2048
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002049/*************************************************************************
2050 * Backlight/brightness subdriver
2051 */
Holger Macht8acb0252006-10-20 14:30:28 -07002052
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002053static struct backlight_device *ibm_backlight_device = NULL;
Holger Macht8acb0252006-10-20 14:30:28 -07002054
Richard Purdie599a52d2007-02-10 23:07:48 +00002055static struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02002056 .get_brightness = brightness_get,
2057 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02002058};
2059
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002060static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02002061{
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02002062 int b;
2063
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002064 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
2065
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02002066 b = brightness_get(NULL);
2067 if (b < 0)
2068 return b;
2069
Yu Luming519ab5f2006-12-19 12:56:15 -08002070 ibm_backlight_device = backlight_device_register("ibm", NULL, NULL,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02002071 &ibm_backlight_data);
2072 if (IS_ERR(ibm_backlight_device)) {
2073 printk(IBM_ERR "Could not register backlight device\n");
2074 return PTR_ERR(ibm_backlight_device);
2075 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002076 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02002077
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02002078 ibm_backlight_device->props.max_brightness = 7;
2079 ibm_backlight_device->props.brightness = b;
2080 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00002081
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02002082 return 0;
2083}
2084
2085static void brightness_exit(void)
2086{
2087 if (ibm_backlight_device) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002088 vdbg_printk(TPACPI_DBG_EXIT,
2089 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02002090 backlight_device_unregister(ibm_backlight_device);
2091 ibm_backlight_device = NULL;
2092 }
2093}
2094
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002095static int brightness_update_status(struct backlight_device *bd)
2096{
2097 return brightness_set(
2098 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
2099 bd->props.power == FB_BLANK_UNBLANK) ?
2100 bd->props.brightness : 0);
2101}
2102
2103static int brightness_get(struct backlight_device *bd)
2104{
2105 u8 level;
2106 if (!acpi_ec_read(brightness_offset, &level))
2107 return -EIO;
2108
2109 level &= 0x7;
2110
2111 return level;
2112}
2113
2114static int brightness_set(int value)
2115{
2116 int cmos_cmd, inc, i;
2117 int current_value = brightness_get(NULL);
2118
2119 value &= 7;
2120
2121 cmos_cmd = value > current_value ? TP_CMOS_BRIGHTNESS_UP : TP_CMOS_BRIGHTNESS_DOWN;
2122 inc = value > current_value ? 1 : -1;
2123 for (i = current_value; i != value; i += inc) {
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03002124 if (issue_thinkpad_cmos_command(cmos_cmd))
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002125 return -EIO;
2126 if (!acpi_ec_write(brightness_offset, i + inc))
2127 return -EIO;
2128 }
2129
2130 return 0;
2131}
2132
2133static int brightness_read(char *p)
2134{
2135 int len = 0;
2136 int level;
2137
2138 if ((level = brightness_get(NULL)) < 0) {
2139 len += sprintf(p + len, "level:\t\tunreadable\n");
2140 } else {
2141 len += sprintf(p + len, "level:\t\t%d\n", level & 0x7);
2142 len += sprintf(p + len, "commands:\tup, down\n");
2143 len += sprintf(p + len, "commands:\tlevel <level>"
2144 " (<level> is 0-7)\n");
2145 }
2146
2147 return len;
2148}
2149
2150static int brightness_write(char *buf)
2151{
2152 int level;
2153 int new_level;
2154 char *cmd;
2155
2156 while ((cmd = next_cmd(&buf))) {
2157 if ((level = brightness_get(NULL)) < 0)
2158 return level;
2159 level &= 7;
2160
2161 if (strlencmp(cmd, "up") == 0) {
2162 new_level = level == 7 ? 7 : level + 1;
2163 } else if (strlencmp(cmd, "down") == 0) {
2164 new_level = level == 0 ? 0 : level - 1;
2165 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
2166 new_level >= 0 && new_level <= 7) {
2167 /* new_level set */
2168 } else
2169 return -EINVAL;
2170
2171 brightness_set(new_level);
2172 }
2173
2174 return 0;
2175}
2176
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002177static struct ibm_struct brightness_driver_data = {
2178 .name = "brightness",
2179 .read = brightness_read,
2180 .write = brightness_write,
2181 .exit = brightness_exit,
2182};
2183
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002184/*************************************************************************
2185 * Volume subdriver
2186 */
2187
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002188static int volume_read(char *p)
2189{
2190 int len = 0;
2191 u8 level;
2192
2193 if (!acpi_ec_read(volume_offset, &level)) {
2194 len += sprintf(p + len, "level:\t\tunreadable\n");
2195 } else {
2196 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
2197 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
2198 len += sprintf(p + len, "commands:\tup, down, mute\n");
2199 len += sprintf(p + len, "commands:\tlevel <level>"
2200 " (<level> is 0-15)\n");
2201 }
2202
2203 return len;
2204}
2205
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002206static int volume_write(char *buf)
2207{
2208 int cmos_cmd, inc, i;
2209 u8 level, mute;
2210 int new_level, new_mute;
2211 char *cmd;
2212
2213 while ((cmd = next_cmd(&buf))) {
2214 if (!acpi_ec_read(volume_offset, &level))
2215 return -EIO;
2216 new_mute = mute = level & 0x40;
2217 new_level = level = level & 0xf;
2218
2219 if (strlencmp(cmd, "up") == 0) {
2220 if (mute)
2221 new_mute = 0;
2222 else
2223 new_level = level == 15 ? 15 : level + 1;
2224 } else if (strlencmp(cmd, "down") == 0) {
2225 if (mute)
2226 new_mute = 0;
2227 else
2228 new_level = level == 0 ? 0 : level - 1;
2229 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
2230 new_level >= 0 && new_level <= 15) {
2231 /* new_level set */
2232 } else if (strlencmp(cmd, "mute") == 0) {
2233 new_mute = 0x40;
2234 } else
2235 return -EINVAL;
2236
2237 if (new_level != level) { /* mute doesn't change */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002238 cmos_cmd = new_level > level ? TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002239 inc = new_level > level ? 1 : -1;
2240
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03002241 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002242 !acpi_ec_write(volume_offset, level)))
2243 return -EIO;
2244
2245 for (i = level; i != new_level; i += inc)
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03002246 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002247 !acpi_ec_write(volume_offset, i + inc))
2248 return -EIO;
2249
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03002250 if (mute && (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002251 !acpi_ec_write(volume_offset,
2252 new_level + mute)))
2253 return -EIO;
2254 }
2255
2256 if (new_mute != mute) { /* level doesn't change */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002257 cmos_cmd = new_mute ? TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002258
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03002259 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002260 !acpi_ec_write(volume_offset, level + new_mute))
2261 return -EIO;
2262 }
2263 }
2264
2265 return 0;
2266}
2267
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002268static struct ibm_struct volume_driver_data = {
2269 .name = "volume",
2270 .read = volume_read,
2271 .write = volume_write,
2272};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002273
2274/*************************************************************************
2275 * Fan subdriver
2276 */
2277
2278/*
2279 * FAN ACCESS MODES
2280 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002281 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002282 * ACPI GFAN method: returns fan level
2283 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002284 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03002285 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002286 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002287 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002288 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
2289 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03002290 * EC 0x2f (HFSP) might be available *for reading*, but do not use
2291 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002292 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002293 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03002294 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
2295 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002296 *
2297 * Fan speed changes of any sort (including those caused by the
2298 * disengaged mode) are usually done slowly by the firmware as the
2299 * maximum ammount of fan duty cycle change per second seems to be
2300 * limited.
2301 *
2302 * Reading is not available if GFAN exists.
2303 * Writing is not available if SFAN exists.
2304 *
2305 * Bits
2306 * 7 automatic mode engaged;
2307 * (default operation mode of the ThinkPad)
2308 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03002309 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002310 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03002311 * the tachometer while the fan controller ramps up
2312 * the speed (which can take up to a few *minutes*).
2313 * Speeds up fan to 100% duty-cycle, which is far above
2314 * the standard RPM levels. It is not impossible that
2315 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002316 * 5-3 unused in some models. Extra bits for fan level
2317 * in others, but still useless as all values above
2318 * 7 map to the same speed as level 7 in these models.
2319 * 2-0 fan level (0..7 usually)
2320 * 0x00 = stop
2321 * 0x07 = max (set when temperatures critical)
2322 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002323 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002324 *
2325 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
2326 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
2327 * does so, its initial value is meaningless (0x07).
2328 *
2329 * For firmware bugs, refer to:
2330 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
2331 *
2332 * ----
2333 *
2334 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
2335 * Main fan tachometer reading (in RPM)
2336 *
2337 * This register is present on all ThinkPads with a new-style EC, and
2338 * it is known not to be present on the A21m/e, and T22, as there is
2339 * something else in offset 0x84 according to the ACPI DSDT. Other
2340 * ThinkPads from this same time period (and earlier) probably lack the
2341 * tachometer as well.
2342 *
2343 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
2344 * was never fixed by IBM to report the EC firmware version string
2345 * probably support the tachometer (like the early X models), so
2346 * detecting it is quite hard. We need more data to know for sure.
2347 *
2348 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
2349 * might result.
2350 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03002351 * FIRMWARE BUG: may go stale while the EC is switching to full speed
2352 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002353 *
2354 * For firmware bugs, refer to:
2355 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
2356 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002357 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002358 * ThinkPad X31, X40, X41. Not available in the X60.
2359 *
2360 * FANS ACPI handle: takes three arguments: low speed, medium speed,
2361 * high speed. ACPI DSDT seems to map these three speeds to levels
2362 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
2363 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
2364 *
2365 * The speeds are stored on handles
2366 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
2367 *
2368 * There are three default speed sets, acessible as handles:
2369 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
2370 *
2371 * ACPI DSDT switches which set is in use depending on various
2372 * factors.
2373 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002374 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002375 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
2376 * but the ACPI tables just mention level 7.
2377 */
2378
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002379static enum fan_status_access_mode fan_status_access_mode;
2380static enum fan_control_access_mode fan_control_access_mode;
2381static enum fan_control_commands fan_control_commands;
2382
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002383static u8 fan_control_initial_status;
2384
Len Brown25c68a32006-12-08 04:43:41 -05002385static void fan_watchdog_fire(struct work_struct *ignored);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002386static int fan_watchdog_maxinterval;
Len Brown25c68a32006-12-08 04:43:41 -05002387static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002388
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002389IBM_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
2390IBM_HANDLE(gfan, ec, "GFAN", /* 570 */
2391 "\\FSPD", /* 600e/x, 770e, 770x */
2392 ); /* all others */
2393IBM_HANDLE(sfan, ec, "SFAN", /* 570 */
2394 "JFNS", /* 770x-JL */
2395 ); /* all others */
2396
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002397static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002398{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002399 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
2400
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002401 fan_status_access_mode = TPACPI_FAN_NONE;
2402 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002403 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002404 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002405 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002406
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002407 IBM_ACPIHANDLE_INIT(fans);
2408 IBM_ACPIHANDLE_INIT(gfan);
2409 IBM_ACPIHANDLE_INIT(sfan);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002410
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002411 if (gfan_handle) {
2412 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002413 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002414 } else {
2415 /* all other ThinkPads: note that even old-style
2416 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002417 if (likely(acpi_ec_read(fan_status_offset,
2418 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002419 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002420
2421 /* In some ThinkPads, neither the EC nor the ACPI
2422 * DSDT initialize the fan status, and it ends up
2423 * being set to 0x07 when it *could* be either
2424 * 0x07 or 0x80.
2425 *
2426 * Enable for TP-1Y (T43), TP-78 (R51e),
2427 * TP-76 (R52), TP-70 (T43, R52), which are known
2428 * to be buggy. */
2429 if (fan_control_initial_status == 0x07 &&
2430 ibm_thinkpad_ec_found &&
2431 ((ibm_thinkpad_ec_found[0] == '1' &&
2432 ibm_thinkpad_ec_found[1] == 'Y') ||
2433 (ibm_thinkpad_ec_found[0] == '7' &&
2434 (ibm_thinkpad_ec_found[1] == '6' ||
2435 ibm_thinkpad_ec_found[1] == '8' ||
2436 ibm_thinkpad_ec_found[1] == '0'))
2437 )) {
2438 printk(IBM_NOTICE
2439 "fan_init: initial fan status is "
2440 "unknown, assuming it is in auto "
2441 "mode\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002442 tp_features.fan_ctrl_status_undef = 1;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002443 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002444 } else {
2445 printk(IBM_ERR
2446 "ThinkPad ACPI EC access misbehaving, "
2447 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002448 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002449 }
2450 }
2451
2452 if (sfan_handle) {
2453 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002454 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002455 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002456 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002457 } else {
2458 if (!gfan_handle) {
2459 /* gfan without sfan means no fan control */
2460 /* all other models implement TP EC 0x2f control */
2461
2462 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02002463 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002464 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002465 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002466 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002467 TPACPI_FAN_CMD_SPEED |
2468 TPACPI_FAN_CMD_LEVEL |
2469 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002470 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002471 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02002472 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002473 TPACPI_FAN_CMD_LEVEL |
2474 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002475 }
2476 }
2477 }
2478
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002479 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
2480 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
2481 fan_control_access_mode != TPACPI_FAN_WR_NONE),
2482 fan_status_access_mode, fan_control_access_mode);
2483
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002484 return (fan_status_access_mode != TPACPI_FAN_NONE ||
2485 fan_control_access_mode != TPACPI_FAN_WR_NONE)?
2486 0 : 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002487}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002488
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02002489static int fan_get_status(u8 *status)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002490{
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02002491 u8 s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002492
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02002493 /* TODO:
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002494 * Add TPACPI_FAN_RD_ACPI_FANS ? */
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02002495
Henrique de Moraes Holschuh3ef8a602006-11-24 11:47:10 -02002496 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002497 case TPACPI_FAN_RD_ACPI_GFAN:
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002498 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02002499
2500 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002501 return -EIO;
2502
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02002503 if (likely(status))
2504 *status = s & 0x07;
2505
2506 break;
2507
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002508 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02002509 /* all except 570, 600e/x, 770e, 770x */
2510 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
2511 return -EIO;
2512
2513 if (likely(status))
2514 *status = s;
2515
2516 break;
2517
2518 default:
2519 return -ENXIO;
2520 }
2521
2522 return 0;
2523}
2524
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002525static void fan_exit(void)
2526{
Henrique de Moraes Holschuh99fba3f2007-04-21 11:08:44 -03002527 vdbg_printk(TPACPI_DBG_EXIT, "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002528 cancel_delayed_work(&fan_watchdog_task);
2529 flush_scheduled_work();
2530}
2531
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02002532static int fan_get_speed(unsigned int *speed)
2533{
2534 u8 hi, lo;
2535
2536 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002537 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02002538 /* all except 570, 600e/x, 770e, 770x */
2539 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
2540 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
2541 return -EIO;
2542
2543 if (likely(speed))
2544 *speed = (hi << 8) | lo;
2545
2546 break;
2547
2548 default:
2549 return -ENXIO;
2550 }
2551
2552 return 0;
2553}
2554
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002555static void fan_watchdog_fire(struct work_struct *ignored)
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002556{
Henrique de Moraes Holschuh99fba3f2007-04-21 11:08:44 -03002557 int rc;
2558
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002559 printk(IBM_NOTICE "fan watchdog: enabling fan\n");
Henrique de Moraes Holschuh99fba3f2007-04-21 11:08:44 -03002560 rc = fan_set_enable();
2561 if (rc < 0) {
2562 printk(IBM_ERR "fan watchdog: error %d while enabling fan, "
2563 "will try again later...\n", -rc);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002564 /* reschedule for later */
2565 fan_watchdog_reset();
2566 }
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002567}
2568
2569static void fan_watchdog_reset(void)
2570{
2571 static int fan_watchdog_active = 0;
2572
2573 if (fan_watchdog_active)
2574 cancel_delayed_work(&fan_watchdog_task);
2575
2576 if (fan_watchdog_maxinterval > 0) {
2577 fan_watchdog_active = 1;
2578 if (!schedule_delayed_work(&fan_watchdog_task,
2579 msecs_to_jiffies(fan_watchdog_maxinterval
2580 * 1000))) {
2581 printk(IBM_ERR "failed to schedule the fan watchdog, "
2582 "watchdog will not trigger\n");
2583 }
2584 } else
2585 fan_watchdog_active = 0;
2586}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002587
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002588static int fan_set_level(int level)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002589{
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002590 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002591 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002592 if (level >= 0 && level <= 7) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002593 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
2594 return -EIO;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002595 } else
2596 return -EINVAL;
2597 break;
2598
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002599 case TPACPI_FAN_WR_ACPI_FANS:
2600 case TPACPI_FAN_WR_TPEC:
2601 if ((level != TP_EC_FAN_AUTO) &&
2602 (level != TP_EC_FAN_FULLSPEED) &&
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02002603 ((level < 0) || (level > 7)))
2604 return -EINVAL;
2605
2606 if (!acpi_ec_write(fan_status_offset, level))
2607 return -EIO;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002608 else
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002609 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02002610 break;
2611
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002612 default:
2613 return -ENXIO;
2614 }
2615 return 0;
2616}
2617
2618static int fan_set_enable(void)
2619{
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002620 u8 s;
2621 int rc;
2622
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002623 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002624 case TPACPI_FAN_WR_ACPI_FANS:
2625 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002626 if ((rc = fan_get_status(&s)) < 0)
2627 return rc;
2628
2629 /* Don't go out of emergency fan mode */
2630 if (s != 7)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002631 s = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002632
2633 if (!acpi_ec_write(fan_status_offset, s))
2634 return -EIO;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002635 else
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002636 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002637 break;
2638
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002639 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002640 if ((rc = fan_get_status(&s)) < 0)
2641 return rc;
2642
2643 s &= 0x07;
2644
2645 /* Set fan to at least level 4 */
2646 if (s < 4)
2647 s = 4;
2648
2649 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002650 return -EIO;
2651 break;
2652
2653 default:
2654 return -ENXIO;
2655 }
2656 return 0;
2657}
2658
2659static int fan_set_disable(void)
2660{
2661 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002662 case TPACPI_FAN_WR_ACPI_FANS:
2663 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002664 if (!acpi_ec_write(fan_status_offset, 0x00))
2665 return -EIO;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002666 else
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002667 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002668 break;
2669
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002670 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002671 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
2672 return -EIO;
2673 break;
2674
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002675 default:
2676 return -ENXIO;
2677 }
2678 return 0;
2679}
2680
2681static int fan_set_speed(int speed)
2682{
2683 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002684 case TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002685 if (speed >= 0 && speed <= 65535) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002686 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
2687 speed, speed, speed))
2688 return -EIO;
2689 } else
2690 return -EINVAL;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002691 break;
2692
2693 default:
2694 return -ENXIO;
2695 }
2696 return 0;
2697}
2698
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002699static int fan_read(char *p)
2700{
2701 int len = 0;
2702 int rc;
2703 u8 status;
2704 unsigned int speed = 0;
2705
2706 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002707 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002708 /* 570, 600e/x, 770e, 770x */
2709 if ((rc = fan_get_status(&status)) < 0)
2710 return rc;
2711
2712 len += sprintf(p + len, "status:\t\t%s\n"
2713 "level:\t\t%d\n",
2714 (status != 0) ? "enabled" : "disabled", status);
2715 break;
2716
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002717 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002718 /* all except 570, 600e/x, 770e, 770x */
2719 if ((rc = fan_get_status(&status)) < 0)
2720 return rc;
2721
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002722 if (unlikely(tp_features.fan_ctrl_status_undef)) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002723 if (status != fan_control_initial_status)
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002724 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002725 else
2726 /* Return most likely status. In fact, it
2727 * might be the only possible status */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002728 status = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002729 }
2730
2731 len += sprintf(p + len, "status:\t\t%s\n",
2732 (status != 0) ? "enabled" : "disabled");
2733
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002734 if ((rc = fan_get_speed(&speed)) < 0)
2735 return rc;
2736
2737 len += sprintf(p + len, "speed:\t\t%d\n", speed);
2738
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002739 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002740 /* Disengaged mode takes precedence */
2741 len += sprintf(p + len, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002742 else if (status & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002743 len += sprintf(p + len, "level:\t\tauto\n");
2744 else
2745 len += sprintf(p + len, "level:\t\t%d\n", status);
2746 break;
2747
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002748 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002749 default:
2750 len += sprintf(p + len, "status:\t\tnot supported\n");
2751 }
2752
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002753 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002754 len += sprintf(p + len, "commands:\tlevel <level>");
2755
2756 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002757 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002758 len += sprintf(p + len, " (<level> is 0-7)\n");
2759 break;
2760
2761 default:
2762 len += sprintf(p + len, " (<level> is 0-7, "
2763 "auto, disengaged)\n");
2764 break;
2765 }
2766 }
2767
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002768 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002769 len += sprintf(p + len, "commands:\tenable, disable\n"
2770 "commands:\twatchdog <timeout> (<timeout> is 0 (off), "
2771 "1-120 (seconds))\n");
2772
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002773 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002774 len += sprintf(p + len, "commands:\tspeed <speed>"
2775 " (<speed> is 0-65535)\n");
2776
2777 return len;
2778}
2779
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002780static int fan_write_cmd_level(const char *cmd, int *rc)
2781{
2782 int level;
2783
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02002784 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002785 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02002786 else if (strlencmp(cmd, "level disengaged") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002787 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02002788 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002789 return 0;
2790
2791 if ((*rc = fan_set_level(level)) == -ENXIO)
2792 printk(IBM_ERR "level command accepted for unsupported "
2793 "access mode %d", fan_control_access_mode);
2794
2795 return 1;
2796}
2797
2798static int fan_write_cmd_enable(const char *cmd, int *rc)
2799{
2800 if (strlencmp(cmd, "enable") != 0)
2801 return 0;
2802
2803 if ((*rc = fan_set_enable()) == -ENXIO)
2804 printk(IBM_ERR "enable command accepted for unsupported "
2805 "access mode %d", fan_control_access_mode);
2806
2807 return 1;
2808}
2809
2810static int fan_write_cmd_disable(const char *cmd, int *rc)
2811{
2812 if (strlencmp(cmd, "disable") != 0)
2813 return 0;
2814
2815 if ((*rc = fan_set_disable()) == -ENXIO)
2816 printk(IBM_ERR "disable command accepted for unsupported "
2817 "access mode %d", fan_control_access_mode);
2818
2819 return 1;
2820}
2821
2822static int fan_write_cmd_speed(const char *cmd, int *rc)
2823{
2824 int speed;
2825
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02002826 /* TODO:
2827 * Support speed <low> <medium> <high> ? */
2828
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002829 if (sscanf(cmd, "speed %d", &speed) != 1)
2830 return 0;
2831
2832 if ((*rc = fan_set_speed(speed)) == -ENXIO)
2833 printk(IBM_ERR "speed command accepted for unsupported "
2834 "access mode %d", fan_control_access_mode);
2835
2836 return 1;
2837}
2838
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002839static int fan_write_cmd_watchdog(const char *cmd, int *rc)
2840{
2841 int interval;
2842
2843 if (sscanf(cmd, "watchdog %d", &interval) != 1)
2844 return 0;
2845
2846 if (interval < 0 || interval > 120)
2847 *rc = -EINVAL;
2848 else
2849 fan_watchdog_maxinterval = interval;
2850
2851 return 1;
2852}
2853
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002854static int fan_write(char *buf)
2855{
2856 char *cmd;
2857 int rc = 0;
2858
2859 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002860 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002861 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002862 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002863 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002864 fan_write_cmd_disable(cmd, &rc) ||
2865 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002866 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002867 fan_write_cmd_speed(cmd, &rc))
2868 )
2869 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002870 else if (!rc)
2871 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002872 }
2873
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002874 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002875}
2876
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002877static struct ibm_struct fan_driver_data = {
2878 .name = "fan",
2879 .read = fan_read,
2880 .write = fan_write,
2881 .exit = fan_exit,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03002882 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002883};
2884
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002885/****************************************************************************
2886 ****************************************************************************
2887 *
2888 * Infrastructure
2889 *
2890 ****************************************************************************
2891 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002893/* /proc support */
2894static struct proc_dir_entry *proc_dir = NULL;
2895
2896/* Subdriver registry */
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002897static LIST_HEAD(tpacpi_all_drivers);
2898
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002900/*
2901 * Module and infrastructure proble, init and exit handling
2902 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002904#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002905static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002906{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002907 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002908
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002909 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002910}
2911#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
2912
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002913static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914{
2915 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002916 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 struct proc_dir_entry *entry;
2918
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002919 BUG_ON(ibm == NULL);
2920
2921 INIT_LIST_HEAD(&ibm->all_drivers);
2922
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03002923 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 return 0;
2925
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002926 dbg_printk(TPACPI_DBG_INIT,
2927 "probing for %s\n", ibm->name);
2928
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002929 if (iibm->init) {
2930 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002931 if (ret > 0)
2932 return 0; /* probe failed */
2933 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002935
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03002936 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 }
2938
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002939 if (ibm->acpi) {
2940 if (ibm->acpi->hid) {
2941 ret = register_tpacpi_subdriver(ibm);
2942 if (ret)
2943 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002944 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002945
2946 if (ibm->acpi->notify) {
2947 ret = setup_acpi_notify(ibm);
2948 if (ret == -ENODEV) {
2949 printk(IBM_NOTICE "disabling subdriver %s\n",
2950 ibm->name);
2951 ret = 0;
2952 goto err_out;
2953 }
2954 if (ret < 0)
2955 goto err_out;
2956 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002957 }
2958
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002959 dbg_printk(TPACPI_DBG_INIT,
2960 "%s installed\n", ibm->name);
2961
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002962 if (ibm->read) {
2963 entry = create_proc_entry(ibm->name,
2964 S_IFREG | S_IRUGO | S_IWUSR,
2965 proc_dir);
2966 if (!entry) {
2967 printk(IBM_ERR "unable to create proc entry %s\n",
2968 ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002969 ret = -ENODEV;
2970 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002971 }
2972 entry->owner = THIS_MODULE;
2973 entry->data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002974 entry->read_proc = &dispatch_procfs_read;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002975 if (ibm->write)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002976 entry->write_proc = &dispatch_procfs_write;
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03002977 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002978 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002980 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
2981
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002983
2984err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002985 dbg_printk(TPACPI_DBG_INIT,
2986 "%s: at error exit path with result %d\n",
2987 ibm->name, ret);
2988
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002989 ibm_exit(ibm);
2990 return (ret < 0)? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991}
2992
2993static void ibm_exit(struct ibm_struct *ibm)
2994{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002995 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002996
2997 list_del_init(&ibm->all_drivers);
2998
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002999 if (ibm->flags.acpi_notify_installed) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003000 dbg_printk(TPACPI_DBG_EXIT,
3001 "%s: acpi_remove_notify_handler\n", ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003002 BUG_ON(!ibm->acpi);
3003 acpi_remove_notify_handler(*ibm->acpi->handle,
3004 ibm->acpi->type,
3005 dispatch_acpi_notify);
3006 ibm->flags.acpi_notify_installed = 0;
3007 ibm->flags.acpi_notify_installed = 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003008 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03003010 if (ibm->flags.proc_created) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003011 dbg_printk(TPACPI_DBG_EXIT,
3012 "%s: remove_proc_entry\n", ibm->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 remove_proc_entry(ibm->name, proc_dir);
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03003014 ibm->flags.proc_created = 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003015 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003017 if (ibm->flags.acpi_driver_registered) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003018 dbg_printk(TPACPI_DBG_EXIT,
3019 "%s: acpi_bus_unregister_driver\n", ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003020 BUG_ON(!ibm->acpi);
3021 acpi_bus_unregister_driver(ibm->acpi->driver);
3022 kfree(ibm->acpi->driver);
3023 ibm->acpi->driver = NULL;
3024 ibm->flags.acpi_driver_registered = 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003025 }
3026
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03003027 if (ibm->flags.init_called && ibm->exit) {
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003028 ibm->exit();
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03003029 ibm->flags.init_called = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003031
3032 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033}
3034
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003035/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003037static char *ibm_thinkpad_ec_found = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02003039static char* __init check_dmi_for_ec(void)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003040{
3041 struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02003042 char ec_fw_string[18];
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003043
3044 /*
3045 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
3046 * X32 or newer, all Z series; Some models must have an
3047 * up-to-date BIOS or they will not be detected.
3048 *
3049 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
3050 */
3051 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02003052 if (sscanf(dev->name,
3053 "IBM ThinkPad Embedded Controller -[%17c",
3054 ec_fw_string) == 1) {
3055 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
3056 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
3057 return kstrdup(ec_fw_string, GFP_KERNEL);
3058 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003059 }
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02003060 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061}
3062
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003063static int __init probe_for_thinkpad(void)
3064{
3065 int is_thinkpad;
3066
3067 if (acpi_disabled)
3068 return -ENODEV;
3069
3070 /*
3071 * Non-ancient models have better DMI tagging, but very old models
3072 * don't.
3073 */
3074 is_thinkpad = dmi_name_in_vendors("ThinkPad");
3075
3076 /* ec is required because many other handles are relative to it */
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003077 IBM_ACPIHANDLE_INIT(ec);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003078 if (!ec_handle) {
3079 if (is_thinkpad)
3080 printk(IBM_ERR
3081 "Not yet supported ThinkPad detected!\n");
3082 return -ENODEV;
3083 }
3084
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03003085 /*
3086 * Risks a regression on very old machines, but reduces potential
3087 * false positives a damn great deal
3088 */
3089 if (!is_thinkpad)
3090 is_thinkpad = dmi_name_in_vendors("IBM");
3091
3092 if (!is_thinkpad && !force_load)
3093 return -ENODEV;
3094
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003095 return 0;
3096}
3097
3098
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003099/* Module init, exit, parameters */
3100
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003101static struct ibm_init_struct ibms_init[] __initdata = {
3102 {
3103 .init = thinkpad_acpi_driver_init,
3104 .data = &thinkpad_acpi_driver_data,
3105 },
3106 {
3107 .init = hotkey_init,
3108 .data = &hotkey_driver_data,
3109 },
3110 {
3111 .init = bluetooth_init,
3112 .data = &bluetooth_driver_data,
3113 },
3114 {
3115 .init = wan_init,
3116 .data = &wan_driver_data,
3117 },
3118 {
3119 .init = video_init,
3120 .data = &video_driver_data,
3121 },
3122 {
3123 .init = light_init,
3124 .data = &light_driver_data,
3125 },
3126#ifdef CONFIG_THINKPAD_ACPI_DOCK
3127 {
3128 .init = dock_init,
3129 .data = &dock_driver_data[0],
3130 },
3131 {
3132 .data = &dock_driver_data[1],
3133 },
3134#endif
3135#ifdef CONFIG_THINKPAD_ACPI_BAY
3136 {
3137 .init = bay_init,
3138 .data = &bay_driver_data,
3139 },
3140#endif
3141 {
3142 .init = cmos_init,
3143 .data = &cmos_driver_data,
3144 },
3145 {
3146 .init = led_init,
3147 .data = &led_driver_data,
3148 },
3149 {
3150 .init = beep_init,
3151 .data = &beep_driver_data,
3152 },
3153 {
3154 .init = thermal_init,
3155 .data = &thermal_driver_data,
3156 },
3157 {
3158 .data = &ecdump_driver_data,
3159 },
3160 {
3161 .init = brightness_init,
3162 .data = &brightness_driver_data,
3163 },
3164 {
3165 .data = &volume_driver_data,
3166 },
3167 {
3168 .init = fan_init,
3169 .data = &fan_driver_data,
3170 },
3171};
3172
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003173static int __init set_ibm_param(const char *val, struct kernel_param *kp)
3174{
3175 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003176 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003177
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003178 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
3179 ibm = ibms_init[i].data;
3180 BUG_ON(ibm == NULL);
3181
3182 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
3183 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003184 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003185 strcpy(ibms_init[i].param, val);
3186 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003187 return 0;
3188 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003189 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003190
3191 return -EINVAL;
3192}
3193
3194static int experimental;
3195module_param(experimental, int, 0);
3196
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03003197static u32 dbg_level;
3198module_param_named(debug, dbg_level, uint, 0);
3199
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03003200static int force_load;
3201module_param(force_load, int, 0);
3202
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003203#define IBM_PARAM(feature) \
3204 module_param_call(feature, set_ibm_param, NULL, NULL, 0)
3205
3206IBM_PARAM(hotkey);
3207IBM_PARAM(bluetooth);
3208IBM_PARAM(video);
3209IBM_PARAM(light);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003210#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003211IBM_PARAM(dock);
3212#endif
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003213#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003214IBM_PARAM(bay);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003215#endif /* CONFIG_THINKPAD_ACPI_BAY */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003216IBM_PARAM(cmos);
3217IBM_PARAM(led);
3218IBM_PARAM(beep);
3219IBM_PARAM(ecdump);
3220IBM_PARAM(brightness);
3221IBM_PARAM(volume);
3222IBM_PARAM(fan);
3223
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03003224static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225{
3226 int ret, i;
3227
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003228 ret = probe_for_thinkpad();
3229 if (ret)
3230 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003232 ibm_thinkpad_ec_found = check_dmi_for_ec();
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003233 IBM_ACPIHANDLE_INIT(ecrd);
3234 IBM_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003236 proc_dir = proc_mkdir(IBM_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 if (!proc_dir) {
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003238 printk(IBM_ERR "unable to create proc dir " IBM_PROC_DIR);
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03003239 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 return -ENODEV;
3241 }
3242 proc_dir->owner = THIS_MODULE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003243
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003244 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
3245 ret = ibm_init(&ibms_init[i]);
3246 if (ret >= 0 && *ibms_init[i].param)
3247 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03003249 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 return ret;
3251 }
3252 }
3253
3254 return 0;
3255}
3256
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03003257static void thinkpad_acpi_module_exit(void)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003258{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003259 struct ibm_struct *ibm, *itmp;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003260
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003261 list_for_each_entry_safe_reverse(ibm, itmp,
3262 &tpacpi_all_drivers,
3263 all_drivers) {
3264 ibm_exit(ibm);
3265 }
3266
3267 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003268
3269 if (proc_dir)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003270 remove_proc_entry(IBM_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003271
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003272 kfree(ibm_thinkpad_ec_found);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003273}
3274
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03003275module_init(thinkpad_acpi_module_init);
3276module_exit(thinkpad_acpi_module_exit);