blob: eeab39418c71aba40b46c8c9da6c2bd047dbb047 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -03002 * thinkpad_acpi.c - ThinkPad ACPI Extras
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 *
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
Henrique de Moraes Holschuha62bc912007-03-23 17:33:58 -03006 * Copyright (C) 2006-2007 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
Henrique de Moraes Holschuha62bc912007-03-23 17:33:58 -030020 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
Borislav Deianov78f81cc2005-08-17 00:00:00 -040022 */
23
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -030024#define IBM_VERSION "0.14"
Borislav Deianov78f81cc2005-08-17 00:00:00 -040025
26/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 * Changelog:
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -030028 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
29 * drivers/misc.
Henrique de Moraes Holschuhf9ff43a2006-11-25 16:37:38 -020030 *
31 * 2006-11-22 0.13 new maintainer
32 * changelog now lives in git commit history, and will
33 * not be updated further in-file.
Henrique de Moraes Holschuh837ca6d2007-03-23 17:33:54 -030034 *
Borislav Deianov78f81cc2005-08-17 00:00:00 -040035 * 2005-08-17 0.12 fix compilation on 2.6.13-rc kernels
36 * 2005-03-17 0.11 support for 600e, 770x
37 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
38 * support for 770e, G41
39 * G40 and G41 don't have a thinklight
40 * temperatures no longer experimental
41 * experimental brightness control
42 * experimental volume control
43 * experimental fan enable/disable
Henrique de Moraes Holschuh837ca6d2007-03-23 17:33:54 -030044 * 2005-01-16 0.10 fix module loading on R30, R31
Borislav Deianov78f81cc2005-08-17 00:00:00 -040045 * 2005-01-16 0.9 support for 570, R30, R31
46 * ultrabay support on A22p, A3x
47 * limit arg for cmos, led, beep, drop experimental status
48 * more capable led control on A21e, A22p, T20-22, X20
49 * experimental temperatures and fan speed
50 * experimental embedded controller register dump
51 * mark more functions as __init, drop incorrect __exit
52 * use MODULE_VERSION
53 * thanks to Henrik Brix Andersen <brix@gentoo.org>
54 * fix parameter passing on module loading
55 * thanks to Rusty Russell <rusty@rustcorp.com.au>
56 * thanks to Jim Radford <radford@blackbean.org>
57 * 2004-11-08 0.8 fix init error case, don't return from a macro
58 * thanks to Chris Wright <chrisw@osdl.org>
59 * 2004-10-23 0.7 fix module loading on A21e, A22p, T20, T21, X20
60 * fix led control on A21e
61 * 2004-10-19 0.6 use acpi_bus_register_driver() to claim HKEY device
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 * 2004-10-18 0.5 thinklight support on A21e, G40, R32, T20, T21, X20
63 * proc file format changed
64 * video_switch command
65 * experimental cmos control
66 * experimental led control
67 * experimental acpi sounds
Borislav Deianov78f81cc2005-08-17 00:00:00 -040068 * 2004-09-16 0.4 support for module parameters
69 * hotkey mask can be prefixed by 0x
70 * video output switching
71 * video expansion control
72 * ultrabay eject support
73 * removed lcd brightness/on/off control, didn't work
74 * 2004-08-17 0.3 support for R40
75 * lcd off, brightness control
76 * thinklight on/off
77 * 2004-08-14 0.2 support for T series, X20
78 * bluetooth enable/disable
79 * hotkey events disabled by default
80 * removed fan control, currently useless
81 * 2004-08-09 0.1 initial release, support for X series
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 */
83
Henrique de Moraes Holschuhf21f85d2007-03-29 01:58:40 -030084#include "thinkpad_acpi.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Henrique de Moraes Holschuhf9ff43a2006-11-25 16:37:38 -020086MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
Borislav Deianov78f81cc2005-08-17 00:00:00 -040087MODULE_DESCRIPTION(IBM_DESC);
88MODULE_VERSION(IBM_VERSION);
89MODULE_LICENSE("GPL");
90
Henrique de Moraes Holschuhd903ac52007-03-29 01:58:42 -030091/* Please remove this in year 2009 */
92MODULE_ALIAS("ibm_acpi");
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094#define __unused __attribute__ ((unused))
95
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -030096/****************************************************************************
97 ****************************************************************************
98 *
99 * ACPI Helpers and device model
100 *
101 ****************************************************************************
102 ****************************************************************************/
103
104/*************************************************************************
105 * ACPI basic handles
106 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108static acpi_handle root_handle = NULL;
109
110#define IBM_HANDLE(object, parent, paths...) \
111 static acpi_handle object##_handle; \
112 static acpi_handle *object##_parent = &parent##_handle; \
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400113 static char *object##_path; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 static char *object##_paths[] = { paths }
115
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400116IBM_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
117 "\\_SB.PCI.ISA.EC", /* 570 */
118 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
119 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
120 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
121 "\\_SB.PCI0.ICH3.EC0", /* R31 */
122 "\\_SB.PCI0.LPC.EC", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300123 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300125IBM_HANDLE(ecrd, ec, "ECRD"); /* 570 */
126IBM_HANDLE(ecwr, ec, "ECWR"); /* 570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300128
129/*************************************************************************
130 * Misc ACPI handles
131 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400133IBM_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, T4x, X31, X40 */
134 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
135 "\\CMS", /* R40, R40e */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300136 ); /* all others */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400137
138IBM_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
139 "^HKEY", /* R30, R31 */
140 "HKEY", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300141 ); /* 570 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400142
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400143
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300144/*************************************************************************
145 * ACPI helpers
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200146 */
147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148static int acpi_evalf(acpi_handle handle,
149 void *res, char *method, char *fmt, ...)
150{
151 char *fmt0 = fmt;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400152 struct acpi_object_list params;
153 union acpi_object in_objs[IBM_MAX_ACPI_ARGS];
154 struct acpi_buffer result, *resultp;
155 union acpi_object out_obj;
156 acpi_status status;
157 va_list ap;
158 char res_type;
159 int success;
160 int quiet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162 if (!*fmt) {
163 printk(IBM_ERR "acpi_evalf() called with empty format\n");
164 return 0;
165 }
166
167 if (*fmt == 'q') {
168 quiet = 1;
169 fmt++;
170 } else
171 quiet = 0;
172
173 res_type = *(fmt++);
174
175 params.count = 0;
176 params.pointer = &in_objs[0];
177
178 va_start(ap, fmt);
179 while (*fmt) {
180 char c = *(fmt++);
181 switch (c) {
182 case 'd': /* int */
183 in_objs[params.count].integer.value = va_arg(ap, int);
184 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
185 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400186 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 default:
188 printk(IBM_ERR "acpi_evalf() called "
189 "with invalid format character '%c'\n", c);
190 return 0;
191 }
192 }
193 va_end(ap);
194
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400195 if (res_type != 'v') {
196 result.length = sizeof(out_obj);
197 result.pointer = &out_obj;
198 resultp = &result;
199 } else
200 resultp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400202 status = acpi_evaluate_object(handle, method, &params, resultp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
204 switch (res_type) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400205 case 'd': /* int */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 if (res)
207 *(int *)res = out_obj.integer.value;
208 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
209 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400210 case 'v': /* void */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 success = status == AE_OK;
212 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400213 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 default:
215 printk(IBM_ERR "acpi_evalf() called "
216 "with invalid format character '%c'\n", res_type);
217 return 0;
218 }
219
220 if (!success && !quiet)
221 printk(IBM_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
222 method, fmt0, status);
223
224 return success;
225}
226
227static void __unused acpi_print_int(acpi_handle handle, char *method)
228{
229 int i;
230
231 if (acpi_evalf(handle, &i, method, "d"))
232 printk(IBM_INFO "%s = 0x%x\n", method, i);
233 else
234 printk(IBM_ERR "error calling %s\n", method);
235}
236
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300237static int acpi_ec_read(int i, u8 * p)
238{
239 int v;
240
241 if (ecrd_handle) {
242 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
243 return 0;
244 *p = v;
245 } else {
246 if (ec_read(i, p) < 0)
247 return 0;
248 }
249
250 return 1;
251}
252
253static int acpi_ec_write(int i, u8 v)
254{
255 if (ecwr_handle) {
256 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
257 return 0;
258 } else {
259 if (ec_write(i, v) < 0)
260 return 0;
261 }
262
263 return 1;
264}
265
266static int _sta(acpi_handle handle)
267{
268 int status;
269
270 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
271 status = 0;
272
273 return status;
274}
275
276/*************************************************************************
277 * ACPI device model
278 */
279
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300280static void ibm_handle_init(char *name,
281 acpi_handle *handle, acpi_handle parent,
282 char **paths, int num_paths, char **path)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300283{
284 int i;
285 acpi_status status;
286
287 for (i = 0; i < num_paths; i++) {
288 status = acpi_get_handle(parent, paths[i], handle);
289 if (ACPI_SUCCESS(status)) {
290 *path = paths[i];
291 return;
292 }
293 }
294
295 *handle = NULL;
296}
297
298static void dispatch_notify(acpi_handle handle, u32 event, void *data)
299{
300 struct ibm_struct *ibm = data;
301
302 if (!ibm || !ibm->notify)
303 return;
304
305 ibm->notify(ibm, event);
306}
307
308static int __init setup_notify(struct ibm_struct *ibm)
309{
310 acpi_status status;
311 int ret;
312
313 if (!*ibm->handle)
314 return 0;
315
316 ret = acpi_bus_get_device(*ibm->handle, &ibm->device);
317 if (ret < 0) {
318 printk(IBM_ERR "%s device not present\n", ibm->name);
319 return -ENODEV;
320 }
321
322 acpi_driver_data(ibm->device) = ibm;
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300323 sprintf(acpi_device_class(ibm->device), "%s/%s",
324 IBM_ACPI_EVENT_PREFIX,
325 ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300326
327 status = acpi_install_notify_handler(*ibm->handle, ibm->type,
328 dispatch_notify, ibm);
329 if (ACPI_FAILURE(status)) {
330 if (status == AE_ALREADY_EXISTS) {
331 printk(IBM_NOTICE "another device driver is already handling %s events\n",
332 ibm->name);
333 } else {
334 printk(IBM_ERR "acpi_install_notify_handler(%s) failed: %d\n",
335 ibm->name, status);
336 }
337 return -ENODEV;
338 }
339 ibm->notify_installed = 1;
340 return 0;
341}
342
343static int __init ibm_device_add(struct acpi_device *device)
344{
345 return 0;
346}
347
Henrique de Moraes Holschuh67001212007-04-21 11:08:25 -0300348static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300349{
350 int ret;
351
352 ibm->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
353 if (!ibm->driver) {
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300354 printk(IBM_ERR "kzalloc(ibm->driver) failed\n");
355 return -ENOMEM;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300356 }
357
358 sprintf(ibm->driver->name, "%s_%s", IBM_NAME, ibm->name);
359 ibm->driver->ids = ibm->hid;
360 ibm->driver->ops.add = &ibm_device_add;
361
362 ret = acpi_bus_register_driver(ibm->driver);
363 if (ret < 0) {
364 printk(IBM_ERR "acpi_bus_register_driver(%s) failed: %d\n",
365 ibm->hid, ret);
366 kfree(ibm->driver);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300367 ibm->driver = NULL;
368 } else if (!ret)
369 ibm->driver_registered = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300370
371 return ret;
372}
373
374
375/****************************************************************************
376 ****************************************************************************
377 *
378 * Procfs Helpers
379 *
380 ****************************************************************************
381 ****************************************************************************/
382
383static int dispatch_read(char *page, char **start, off_t off, int count,
384 int *eof, void *data)
385{
386 struct ibm_struct *ibm = data;
387 int len;
388
389 if (!ibm || !ibm->read)
390 return -EINVAL;
391
392 len = ibm->read(page);
393 if (len < 0)
394 return len;
395
396 if (len <= off + count)
397 *eof = 1;
398 *start = page + off;
399 len -= off;
400 if (len > count)
401 len = count;
402 if (len < 0)
403 len = 0;
404
405 return len;
406}
407
408static int dispatch_write(struct file *file, const char __user * userbuf,
409 unsigned long count, void *data)
410{
411 struct ibm_struct *ibm = data;
412 char *kernbuf;
413 int ret;
414
415 if (!ibm || !ibm->write)
416 return -EINVAL;
417
418 kernbuf = kmalloc(count + 2, GFP_KERNEL);
419 if (!kernbuf)
420 return -ENOMEM;
421
422 if (copy_from_user(kernbuf, userbuf, count)) {
423 kfree(kernbuf);
424 return -EFAULT;
425 }
426
427 kernbuf[count] = 0;
428 strcat(kernbuf, ",");
429 ret = ibm->write(kernbuf);
430 if (ret == 0)
431 ret = count;
432
433 kfree(kernbuf);
434
435 return ret;
436}
437
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438static char *next_cmd(char **cmds)
439{
440 char *start = *cmds;
441 char *end;
442
443 while ((end = strchr(start, ',')) && end == start)
444 start = end + 1;
445
446 if (!end)
447 return NULL;
448
449 *end = 0;
450 *cmds = end + 1;
451 return start;
452}
453
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300454
455/****************************************************************************
456 ****************************************************************************
457 *
458 * Subdrivers
459 *
460 ****************************************************************************
461 ****************************************************************************/
462
463/*************************************************************************
Henrique de Moraes Holschuh142cfc92007-04-21 11:08:26 -0300464 * thinkpad-acpi init subdriver
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300465 */
466
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300467static int thinkpad_acpi_driver_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
469 printk(IBM_INFO "%s v%s\n", IBM_DESC, IBM_VERSION);
470 printk(IBM_INFO "%s\n", IBM_URL);
471
Henrique de Moraes Holschuh3945ac32007-02-06 19:13:44 -0200472 if (ibm_thinkpad_ec_found)
473 printk(IBM_INFO "ThinkPad EC firmware %s\n",
474 ibm_thinkpad_ec_found);
475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 return 0;
477}
478
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300479static int thinkpad_acpi_driver_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
481 int len = 0;
482
483 len += sprintf(p + len, "driver:\t\t%s\n", IBM_DESC);
484 len += sprintf(p + len, "version:\t%s\n", IBM_VERSION);
485
486 return len;
487}
488
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300489/*************************************************************************
490 * Hotkey subdriver
491 */
492
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400493static int hotkey_supported;
494static int hotkey_mask_supported;
495static int hotkey_orig_status;
496static int hotkey_orig_mask;
497
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300498static int hotkey_init(void)
499{
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300500 IBM_HANDLE_INIT(hkey);
501
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300502 /* hotkey not supported on 570 */
503 hotkey_supported = hkey_handle != NULL;
504
505 if (hotkey_supported) {
506 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
507 A30, R30, R31, T20-22, X20-21, X22-24 */
508 hotkey_mask_supported =
509 acpi_evalf(hkey_handle, NULL, "DHKN", "qv");
510
511 if (!hotkey_get(&hotkey_orig_status, &hotkey_orig_mask))
512 return -ENODEV;
513 }
514
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300515 return (hotkey_supported)? 0 : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300516}
517
518static void hotkey_exit(void)
519{
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300520 if (hotkey_supported) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300521 hotkey_set(hotkey_orig_status, hotkey_orig_mask);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300522 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300523}
524
525static void hotkey_notify(struct ibm_struct *ibm, u32 event)
526{
527 int hkey;
528
529 if (acpi_evalf(hkey_handle, &hkey, "MHKP", "d"))
530 acpi_bus_generate_event(ibm->device, event, hkey);
531 else {
532 printk(IBM_ERR "unknown hotkey event %d\n", event);
533 acpi_bus_generate_event(ibm->device, event, 0);
534 }
535}
536
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400537static int hotkey_get(int *status, int *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
539 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400540 return 0;
541
542 if (hotkey_mask_supported)
543 if (!acpi_evalf(hkey_handle, mask, "DHKN", "d"))
544 return 0;
545
546 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547}
548
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400549static int hotkey_set(int status, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
551 int i;
552
553 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 return 0;
555
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400556 if (hotkey_mask_supported)
557 for (i = 0; i < 32; i++) {
558 int bit = ((1 << i) & mask) != 0;
559 if (!acpi_evalf(hkey_handle,
560 NULL, "MHKM", "vdd", i + 1, bit))
561 return 0;
562 }
563
564 return 1;
565}
566
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400567static int hotkey_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
569 int status, mask;
570 int len = 0;
571
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400572 if (!hotkey_supported) {
573 len += sprintf(p + len, "status:\t\tnot supported\n");
574 return len;
575 }
576
577 if (!hotkey_get(&status, &mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 return -EIO;
579
580 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400581 if (hotkey_mask_supported) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 len += sprintf(p + len, "mask:\t\t0x%04x\n", mask);
583 len += sprintf(p + len,
584 "commands:\tenable, disable, reset, <mask>\n");
585 } else {
586 len += sprintf(p + len, "mask:\t\tnot supported\n");
587 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
588 }
589
590 return len;
591}
592
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400593static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
595 int status, mask;
596 char *cmd;
597 int do_cmd = 0;
598
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400599 if (!hotkey_supported)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 return -ENODEV;
601
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400602 if (!hotkey_get(&status, &mask))
603 return -EIO;
604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 while ((cmd = next_cmd(&buf))) {
606 if (strlencmp(cmd, "enable") == 0) {
607 status = 1;
608 } else if (strlencmp(cmd, "disable") == 0) {
609 status = 0;
610 } else if (strlencmp(cmd, "reset") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400611 status = hotkey_orig_status;
612 mask = hotkey_orig_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
614 /* mask set */
615 } else if (sscanf(cmd, "%x", &mask) == 1) {
616 /* mask set */
617 } else
618 return -EINVAL;
619 do_cmd = 1;
620 }
621
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400622 if (do_cmd && !hotkey_set(status, mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 return -EIO;
624
625 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400626}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300628/*************************************************************************
629 * Bluetooth subdriver
630 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400632static int bluetooth_supported;
633
634static int bluetooth_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300636 IBM_HANDLE_INIT(hkey);
637
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400638 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
639 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
640 bluetooth_supported = hkey_handle &&
641 acpi_evalf(hkey_handle, NULL, "GBDC", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300643 return (bluetooth_supported)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644}
645
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400646static int bluetooth_status(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
648 int status;
649
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400650 if (!bluetooth_supported ||
651 !acpi_evalf(hkey_handle, &status, "GBDC", "d"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 status = 0;
653
654 return status;
655}
656
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400657static int bluetooth_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658{
659 int len = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400660 int status = bluetooth_status();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400662 if (!bluetooth_supported)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 len += sprintf(p + len, "status:\t\tnot supported\n");
664 else if (!(status & 1))
665 len += sprintf(p + len, "status:\t\tnot installed\n");
666 else {
667 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 1));
668 len += sprintf(p + len, "commands:\tenable, disable\n");
669 }
670
671 return len;
672}
673
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400674static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400676 int status = bluetooth_status();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 char *cmd;
678 int do_cmd = 0;
679
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400680 if (!bluetooth_supported)
681 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
683 while ((cmd = next_cmd(&buf))) {
684 if (strlencmp(cmd, "enable") == 0) {
685 status |= 2;
686 } else if (strlencmp(cmd, "disable") == 0) {
687 status &= ~2;
688 } else
689 return -EINVAL;
690 do_cmd = 1;
691 }
692
693 if (do_cmd && !acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400694 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
696 return 0;
697}
698
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300699/*************************************************************************
700 * Wan subdriver
701 */
702
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -0400703static int wan_supported;
704
705static int wan_init(void)
706{
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300707 IBM_HANDLE_INIT(hkey);
708
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -0400709 wan_supported = hkey_handle &&
710 acpi_evalf(hkey_handle, NULL, "GWAN", "qv");
711
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300712 return (wan_supported)? 0 : 1;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -0400713}
714
715static int wan_status(void)
716{
717 int status;
718
Henrique de Moraes Holschuh8d297262006-11-24 11:47:07 -0200719 if (!wan_supported || !acpi_evalf(hkey_handle, &status, "GWAN", "d"))
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -0400720 status = 0;
721
722 return status;
723}
724
725static int wan_read(char *p)
726{
727 int len = 0;
728 int status = wan_status();
729
730 if (!wan_supported)
731 len += sprintf(p + len, "status:\t\tnot supported\n");
732 else if (!(status & 1))
733 len += sprintf(p + len, "status:\t\tnot installed\n");
734 else {
735 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 1));
736 len += sprintf(p + len, "commands:\tenable, disable\n");
737 }
738
739 return len;
740}
741
742static int wan_write(char *buf)
743{
744 int status = wan_status();
745 char *cmd;
746 int do_cmd = 0;
747
748 if (!wan_supported)
749 return -ENODEV;
750
751 while ((cmd = next_cmd(&buf))) {
752 if (strlencmp(cmd, "enable") == 0) {
753 status |= 2;
754 } else if (strlencmp(cmd, "disable") == 0) {
755 status &= ~2;
756 } else
757 return -EINVAL;
758 do_cmd = 1;
759 }
760
761 if (do_cmd && !acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
762 return -EIO;
763
764 return 0;
765}
766
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300767/*************************************************************************
768 * Video subdriver
769 */
770
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200771static enum video_access_mode video_supported;
772static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400773
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300774IBM_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
775 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
776 "\\_SB.PCI0.VID0", /* 770e */
777 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
778 "\\_SB.PCI0.AGP.VID", /* all others */
779 ); /* R30, R31 */
780
781IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
782
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400783static int video_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400785 int ivga;
786
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300787 IBM_HANDLE_INIT(vid);
788 IBM_HANDLE_INIT(vid2);
789
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400790 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
791 /* G41, assume IVGA doesn't change */
792 vid_handle = vid2_handle;
793
794 if (!vid_handle)
795 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300796 video_supported = TPACPI_VIDEO_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400797 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
798 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300799 video_supported = TPACPI_VIDEO_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400800 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
801 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300802 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400803 else
804 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300805 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300807 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808}
809
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300810static void video_exit(void)
811{
812 acpi_evalf(vid_handle, NULL, "_DOS", "vd", video_orig_autosw);
813}
814
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400815static int video_status(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816{
817 int status = 0;
818 int i;
819
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300820 if (video_supported == TPACPI_VIDEO_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400821 if (acpi_evalf(NULL, &i, "\\_SB.PHS", "dd", 0x87))
822 status = i & 3;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300823 } else if (video_supported == TPACPI_VIDEO_770) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400824 if (acpi_evalf(NULL, &i, "\\VCDL", "d"))
825 status |= 0x01 * i;
826 if (acpi_evalf(NULL, &i, "\\VCDC", "d"))
827 status |= 0x02 * i;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300828 } else if (video_supported == TPACPI_VIDEO_NEW) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400829 acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1);
830 if (acpi_evalf(NULL, &i, "\\VCDC", "d"))
831 status |= 0x02 * i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400833 acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0);
834 if (acpi_evalf(NULL, &i, "\\VCDL", "d"))
835 status |= 0x01 * i;
836 if (acpi_evalf(NULL, &i, "\\VCDD", "d"))
837 status |= 0x08 * i;
838 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
840 return status;
841}
842
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400843static int video_autosw(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400845 int autosw = 0;
846
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300847 if (video_supported == TPACPI_VIDEO_570)
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400848 acpi_evalf(vid_handle, &autosw, "SWIT", "d");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300849 else if (video_supported == TPACPI_VIDEO_770 ||
850 video_supported == TPACPI_VIDEO_NEW)
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400851 acpi_evalf(vid_handle, &autosw, "^VDEE", "d");
852
853 return autosw & 1;
854}
855
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400856static int video_switch(void)
857{
858 int autosw = video_autosw();
859 int ret;
860
861 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
862 return -EIO;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300863 ret = video_supported == TPACPI_VIDEO_570 ?
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400864 acpi_evalf(ec_handle, NULL, "_Q16", "v") :
865 acpi_evalf(vid_handle, NULL, "VSWT", "v");
866 acpi_evalf(vid_handle, NULL, "_DOS", "vd", autosw);
867
868 return ret;
869}
870
871static int video_expand(void)
872{
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300873 if (video_supported == TPACPI_VIDEO_570)
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400874 return acpi_evalf(ec_handle, NULL, "_Q17", "v");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300875 else if (video_supported == TPACPI_VIDEO_770)
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400876 return acpi_evalf(vid_handle, NULL, "VEXP", "v");
877 else
878 return acpi_evalf(NULL, NULL, "\\VEXP", "v");
879}
880
881static int video_switch2(int status)
882{
883 int ret;
884
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300885 if (video_supported == TPACPI_VIDEO_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400886 ret = acpi_evalf(NULL, NULL,
887 "\\_SB.PHS2", "vdd", 0x8b, status | 0x80);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300888 } else if (video_supported == TPACPI_VIDEO_770) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400889 int autosw = video_autosw();
890 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
891 return -EIO;
892
893 ret = acpi_evalf(vid_handle, NULL,
894 "ASWT", "vdd", status * 0x100, 0);
895
896 acpi_evalf(vid_handle, NULL, "_DOS", "vd", autosw);
897 } else {
898 ret = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
899 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
900 }
901
902 return ret;
903}
904
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300905static int video_read(char *p)
906{
907 int status = video_status();
908 int autosw = video_autosw();
909 int len = 0;
910
911 if (!video_supported) {
912 len += sprintf(p + len, "status:\t\tnot supported\n");
913 return len;
914 }
915
916 len += sprintf(p + len, "status:\t\tsupported\n");
917 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
918 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300919 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300920 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
921 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
922 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
923 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300924 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300925 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
926 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
927 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
928
929 return len;
930}
931
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400932static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933{
934 char *cmd;
935 int enable, disable, status;
936
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400937 if (!video_supported)
938 return -ENODEV;
939
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 enable = disable = 0;
941
942 while ((cmd = next_cmd(&buf))) {
943 if (strlencmp(cmd, "lcd_enable") == 0) {
944 enable |= 0x01;
945 } else if (strlencmp(cmd, "lcd_disable") == 0) {
946 disable |= 0x01;
947 } else if (strlencmp(cmd, "crt_enable") == 0) {
948 enable |= 0x02;
949 } else if (strlencmp(cmd, "crt_disable") == 0) {
950 disable |= 0x02;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300951 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400952 strlencmp(cmd, "dvi_enable") == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 enable |= 0x08;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -0300954 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400955 strlencmp(cmd, "dvi_disable") == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 disable |= 0x08;
957 } else if (strlencmp(cmd, "auto_enable") == 0) {
958 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
959 return -EIO;
960 } else if (strlencmp(cmd, "auto_disable") == 0) {
961 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 0))
962 return -EIO;
963 } else if (strlencmp(cmd, "video_switch") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400964 if (!video_switch())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 return -EIO;
966 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400967 if (!video_expand())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 return -EIO;
969 } else
970 return -EINVAL;
971 }
972
973 if (enable || disable) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400974 status = (video_status() & 0x0f & ~disable) | enable;
975 if (!video_switch2(status))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 return -EIO;
977 }
978
979 return 0;
980}
981
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300982/*************************************************************************
983 * Light (thinklight) subdriver
984 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400986static int light_supported;
987static int light_status_supported;
988
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300989IBM_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
990IBM_HANDLE(ledb, ec, "LEDB"); /* G4x */
991
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400992static int light_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993{
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300994 IBM_HANDLE_INIT(ledb);
995 IBM_HANDLE_INIT(lght);
996 IBM_HANDLE_INIT(cmos);
997
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400998 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
999 light_supported = (cmos_handle || lght_handle) && !ledb_handle;
1000
1001 if (light_supported)
1002 /* light status not supported on
1003 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
1004 light_status_supported = acpi_evalf(ec_handle, NULL,
1005 "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001007 return (light_supported)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008}
1009
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001010static int light_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011{
1012 int len = 0;
1013 int status = 0;
1014
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001015 if (!light_supported) {
1016 len += sprintf(p + len, "status:\t\tnot supported\n");
1017 } else if (!light_status_supported) {
1018 len += sprintf(p + len, "status:\t\tunknown\n");
1019 len += sprintf(p + len, "commands:\ton, off\n");
1020 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
1022 return -EIO;
1023 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001024 len += sprintf(p + len, "commands:\ton, off\n");
1025 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026
1027 return len;
1028}
1029
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001030static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031{
1032 int cmos_cmd, lght_cmd;
1033 char *cmd;
1034 int success;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001035
1036 if (!light_supported)
1037 return -ENODEV;
1038
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 while ((cmd = next_cmd(&buf))) {
1040 if (strlencmp(cmd, "on") == 0) {
1041 cmos_cmd = 0x0c;
1042 lght_cmd = 1;
1043 } else if (strlencmp(cmd, "off") == 0) {
1044 cmos_cmd = 0x0d;
1045 lght_cmd = 0;
1046 } else
1047 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001048
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 success = cmos_handle ?
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001050 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
1051 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 if (!success)
1053 return -EIO;
1054 }
1055
1056 return 0;
1057}
1058
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001059/*************************************************************************
1060 * Dock subdriver
1061 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03001063#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001064
1065IBM_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
1066 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
1067 "\\_SB.PCI0.PCI1.DOCK", /* all others */
1068 "\\_SB.PCI.ISA.SLCE", /* 570 */
1069 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
1070
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001071/* don't list other alternatives as we install a notify handler on the 570 */
1072IBM_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
1073
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074#define dock_docked() (_sta(dock_handle) & 1)
1075
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001076static int dock_init(void)
1077{
1078 IBM_HANDLE_INIT(dock);
1079 IBM_HANDLE_INIT(pci);
1080
1081 return (dock_handle)? 0 : 1;
1082}
1083
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001084static void dock_notify(struct ibm_struct *ibm, u32 event)
1085{
1086 int docked = dock_docked();
1087 int pci = ibm->hid && strstr(ibm->hid, IBM_PCI_HID);
1088
1089 if (event == 1 && !pci) /* 570 */
1090 acpi_bus_generate_event(ibm->device, event, 1); /* button */
1091 else if (event == 1 && pci) /* 570 */
1092 acpi_bus_generate_event(ibm->device, event, 3); /* dock */
1093 else if (event == 3 && docked)
1094 acpi_bus_generate_event(ibm->device, event, 1); /* button */
1095 else if (event == 3 && !docked)
1096 acpi_bus_generate_event(ibm->device, event, 2); /* undock */
1097 else if (event == 0 && docked)
1098 acpi_bus_generate_event(ibm->device, event, 3); /* dock */
1099 else {
1100 printk(IBM_ERR "unknown dock event %d, status %d\n",
1101 event, _sta(dock_handle));
1102 acpi_bus_generate_event(ibm->device, event, 0); /* unknown */
1103 }
1104}
1105
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001106static int dock_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107{
1108 int len = 0;
1109 int docked = dock_docked();
1110
1111 if (!dock_handle)
1112 len += sprintf(p + len, "status:\t\tnot supported\n");
1113 else if (!docked)
1114 len += sprintf(p + len, "status:\t\tundocked\n");
1115 else {
1116 len += sprintf(p + len, "status:\t\tdocked\n");
1117 len += sprintf(p + len, "commands:\tdock, undock\n");
1118 }
1119
1120 return len;
1121}
1122
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001123static int dock_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124{
1125 char *cmd;
1126
1127 if (!dock_docked())
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001128 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
1130 while ((cmd = next_cmd(&buf))) {
1131 if (strlencmp(cmd, "undock") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001132 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
1133 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 return -EIO;
1135 } else if (strlencmp(cmd, "dock") == 0) {
1136 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
1137 return -EIO;
1138 } else
1139 return -EINVAL;
1140 }
1141
1142 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001143}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03001145#endif /* CONFIG_THINKPAD_ACPI_DOCK */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001147/*************************************************************************
1148 * Bay subdriver
1149 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03001151#ifdef CONFIG_THINKPAD_ACPI_BAY
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001152static int bay_status_supported;
1153static int bay_status2_supported;
1154static int bay_eject_supported;
1155static int bay_eject2_supported;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001157IBM_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
1158 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
1159 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
1160 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
1161 ); /* A21e, R30, R31 */
1162IBM_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
1163 "_EJ0", /* all others */
1164 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
1165IBM_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
1166 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
1167 ); /* all others */
1168IBM_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
1169 "_EJ0", /* 770x */
1170 ); /* all others */
1171
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001172static int bay_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173{
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001174 IBM_HANDLE_INIT(bay);
1175 if (bay_handle)
1176 IBM_HANDLE_INIT(bay_ej);
1177 IBM_HANDLE_INIT(bay2);
1178 if (bay2_handle)
1179 IBM_HANDLE_INIT(bay2_ej);
1180
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001181 bay_status_supported = bay_handle &&
1182 acpi_evalf(bay_handle, NULL, "_STA", "qv");
1183 bay_status2_supported = bay2_handle &&
1184 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
1185
1186 bay_eject_supported = bay_handle && bay_ej_handle &&
1187 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
1188 bay_eject2_supported = bay2_handle && bay2_ej_handle &&
1189 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001191 return (bay_status_supported || bay_eject_supported ||
1192 bay_status2_supported || bay_eject2_supported)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193}
1194
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001195static void bay_notify(struct ibm_struct *ibm, u32 event)
1196{
1197 acpi_bus_generate_event(ibm->device, event, 0);
1198}
1199
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001200#define bay_occupied(b) (_sta(b##_handle) & 1)
1201
1202static int bay_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203{
1204 int len = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001205 int occupied = bay_occupied(bay);
1206 int occupied2 = bay_occupied(bay2);
1207 int eject, eject2;
1208
1209 len += sprintf(p + len, "status:\t\t%s\n", bay_status_supported ?
1210 (occupied ? "occupied" : "unoccupied") :
1211 "not supported");
1212 if (bay_status2_supported)
1213 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
1214 "occupied" : "unoccupied");
1215
1216 eject = bay_eject_supported && occupied;
1217 eject2 = bay_eject2_supported && occupied2;
1218
1219 if (eject && eject2)
1220 len += sprintf(p + len, "commands:\teject, eject2\n");
1221 else if (eject)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 len += sprintf(p + len, "commands:\teject\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001223 else if (eject2)
1224 len += sprintf(p + len, "commands:\teject2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
1226 return len;
1227}
1228
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001229static int bay_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230{
1231 char *cmd;
1232
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001233 if (!bay_eject_supported && !bay_eject2_supported)
1234 return -ENODEV;
1235
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001237 if (bay_eject_supported && strlencmp(cmd, "eject") == 0) {
1238 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
1239 return -EIO;
1240 } else if (bay_eject2_supported &&
1241 strlencmp(cmd, "eject2") == 0) {
1242 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 return -EIO;
1244 } else
1245 return -EINVAL;
1246 }
1247
1248 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001249}
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03001250#endif /* CONFIG_THINKPAD_ACPI_BAY */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001252/*************************************************************************
1253 * CMOS subdriver
1254 */
1255
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001256static int cmos_init(void)
1257{
1258 IBM_HANDLE_INIT(cmos);
1259
1260 return (cmos_handle)? 0 : 1;
1261}
1262
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001263static int cmos_eval(int cmos_cmd)
1264{
1265 if (cmos_handle)
1266 return acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd);
1267 else
1268 return 1;
1269}
1270
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001271static int cmos_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272{
1273 int len = 0;
1274
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001275 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1276 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 if (!cmos_handle)
1278 len += sprintf(p + len, "status:\t\tnot supported\n");
1279 else {
1280 len += sprintf(p + len, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001281 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 }
1283
1284 return len;
1285}
1286
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001287static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288{
1289 char *cmd;
1290 int cmos_cmd;
1291
1292 if (!cmos_handle)
1293 return -EINVAL;
1294
1295 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001296 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
1297 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 /* cmos_cmd set */
1299 } else
1300 return -EINVAL;
1301
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001302 if (!cmos_eval(cmos_cmd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 return -EIO;
1304 }
1305
1306 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001307}
1308
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001309
1310/*************************************************************************
1311 * LED subdriver
1312 */
1313
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02001314static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001315
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001316IBM_HANDLE(led, ec, "SLED", /* 570 */
1317 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
1318 "LED", /* all others */
1319 ); /* R30, R31 */
1320
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001321static int led_init(void)
1322{
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001323 IBM_HANDLE_INIT(led);
1324
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001325 if (!led_handle)
1326 /* led not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001327 led_supported = TPACPI_LED_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001328 else if (strlencmp(led_path, "SLED") == 0)
1329 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001330 led_supported = TPACPI_LED_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001331 else if (strlencmp(led_path, "SYSL") == 0)
1332 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001333 led_supported = TPACPI_LED_OLD;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001334 else
1335 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001336 led_supported = TPACPI_LED_NEW;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001337
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001338 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001339}
1340
1341#define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
1342
1343static int led_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344{
1345 int len = 0;
1346
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001347 if (!led_supported) {
1348 len += sprintf(p + len, "status:\t\tnot supported\n");
1349 return len;
1350 }
1351 len += sprintf(p + len, "status:\t\tsupported\n");
1352
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001353 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001354 /* 570 */
1355 int i, status;
1356 for (i = 0; i < 8; i++) {
1357 if (!acpi_evalf(ec_handle,
1358 &status, "GLED", "dd", 1 << i))
1359 return -EIO;
1360 len += sprintf(p + len, "%d:\t\t%s\n",
1361 i, led_status(status));
1362 }
1363 }
1364
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 len += sprintf(p + len, "commands:\t"
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001366 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
1368 return len;
1369}
1370
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001371/* off, on, blink */
1372static const int led_sled_arg1[] = { 0, 1, 3 };
1373static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
1374static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
1375static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
1376
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001377static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378{
1379 char *cmd;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001380 int led, ind, ret;
1381
1382 if (!led_supported)
1383 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
1385 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001386 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 return -EINVAL;
1388
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001389 if (strstr(cmd, "off")) {
1390 ind = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 } else if (strstr(cmd, "on")) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001392 ind = 1;
1393 } else if (strstr(cmd, "blink")) {
1394 ind = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 } else
1396 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001397
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001398 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001399 /* 570 */
1400 led = 1 << led;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001402 led, led_sled_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 return -EIO;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001404 } else if (led_supported == TPACPI_LED_OLD) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001405 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
1406 led = 1 << led;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001407 ret = ec_write(TPACPI_LED_EC_HLMS, led);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001408 if (ret >= 0)
1409 ret =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001410 ec_write(TPACPI_LED_EC_HLBL,
Henrique de Moraes Holschuhe062e032007-03-23 17:33:55 -03001411 led * led_exp_hlbl[ind]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001412 if (ret >= 0)
1413 ret =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001414 ec_write(TPACPI_LED_EC_HLCL,
Henrique de Moraes Holschuhe062e032007-03-23 17:33:55 -03001415 led * led_exp_hlcl[ind]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001416 if (ret < 0)
1417 return ret;
1418 } else {
1419 /* all others */
1420 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
1421 led, led_led_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001423 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 }
1425
1426 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001427}
1428
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001429/*************************************************************************
1430 * Beep subdriver
1431 */
1432
1433IBM_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
1434
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001435static int beep_init(void)
1436{
1437 IBM_HANDLE_INIT(beep);
1438
1439 return (beep_handle)? 0 : 1;
1440}
1441
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001442static int beep_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443{
1444 int len = 0;
1445
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001446 if (!beep_handle)
1447 len += sprintf(p + len, "status:\t\tnot supported\n");
1448 else {
1449 len += sprintf(p + len, "status:\t\tsupported\n");
1450 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
1451 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
1453 return len;
1454}
1455
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001456static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457{
1458 char *cmd;
1459 int beep_cmd;
1460
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001461 if (!beep_handle)
1462 return -ENODEV;
1463
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001465 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
1466 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 /* beep_cmd set */
1468 } else
1469 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001470 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 return -EIO;
1472 }
1473
1474 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001475}
1476
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001477/*************************************************************************
1478 * Thermal subdriver
1479 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001480
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001481static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001482
1483static int thermal_init(void)
1484{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001485 u8 t, ta1, ta2;
1486 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001487 int acpi_tmp7;
1488
1489 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001490
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001491 if (ibm_thinkpad_ec_found && experimental) {
1492 /*
1493 * Direct EC access mode: sensors at registers
1494 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
1495 * non-implemented, thermal sensors return 0x80 when
1496 * not available
1497 */
1498
1499 ta1 = ta2 = 0;
1500 for (i = 0; i < 8; i++) {
1501 if (likely(acpi_ec_read(0x78 + i, &t))) {
1502 ta1 |= t;
1503 } else {
1504 ta1 = 0;
1505 break;
1506 }
1507 if (likely(acpi_ec_read(0xC0 + i, &t))) {
1508 ta2 |= t;
1509 } else {
1510 ta1 = 0;
1511 break;
1512 }
1513 }
1514 if (ta1 == 0) {
1515 /* This is sheer paranoia, but we handle it anyway */
1516 if (acpi_tmp7) {
1517 printk(IBM_ERR
1518 "ThinkPad ACPI EC access misbehaving, "
1519 "falling back to ACPI TMPx access mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001520 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001521 } else {
1522 printk(IBM_ERR
1523 "ThinkPad ACPI EC access misbehaving, "
1524 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001525 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001526 }
1527 } else {
1528 thermal_read_mode =
1529 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001530 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001531 }
1532 } else if (acpi_tmp7) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001533 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
1534 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001535 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001536 } else {
1537 /* Standard ACPI TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001538 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001539 }
1540 } else {
1541 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001542 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001543 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001544
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001545 return (thermal_read_mode != TPACPI_THERMAL_NONE)? 0 : 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001546}
1547
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001548static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
1549{
1550 int i, t;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001551 s8 tmp;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001552 char tmpi[] = "TMPi";
1553
1554 if (!s)
1555 return -EINVAL;
1556
1557 switch (thermal_read_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001558#if TPACPI_MAX_THERMAL_SENSORS >= 16
1559 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001560 for (i = 0; i < 8; i++) {
1561 if (!acpi_ec_read(0xC0 + i, &tmp))
1562 return -EIO;
1563 s->temp[i + 8] = tmp * 1000;
1564 }
1565 /* fallthrough */
1566#endif
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001567 case TPACPI_THERMAL_TPEC_8:
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001568 for (i = 0; i < 8; i++) {
1569 if (!acpi_ec_read(0x78 + i, &tmp))
1570 return -EIO;
1571 s->temp[i] = tmp * 1000;
1572 }
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001573 return (thermal_read_mode == TPACPI_THERMAL_TPEC_16) ? 16 : 8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001574
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001575 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001576 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
1577 return -EIO;
1578 for (i = 0; i < 8; i++) {
1579 tmpi[3] = '0' + i;
1580 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
1581 return -EIO;
1582 s->temp[i] = (t - 2732) * 100;
1583 }
1584 return 8;
1585
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001586 case TPACPI_THERMAL_ACPI_TMP07:
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001587 for (i = 0; i < 8; i++) {
1588 tmpi[3] = '0' + i;
1589 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
1590 return -EIO;
1591 s->temp[i] = t * 1000;
1592 }
1593 return 8;
1594
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001595 case TPACPI_THERMAL_NONE:
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001596 default:
1597 return 0;
1598 }
1599}
1600
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001601static int thermal_read(char *p)
1602{
1603 int len = 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001604 int n, i;
1605 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001606
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001607 n = thermal_get_sensors(&t);
1608 if (unlikely(n < 0))
1609 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001610
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001611 len += sprintf(p + len, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001612
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001613 if (n > 0) {
1614 for (i = 0; i < (n - 1); i++)
1615 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
1616 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
1617 } else
1618 len += sprintf(p + len, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001619
1620 return len;
1621}
1622
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001623/*************************************************************************
1624 * EC Dump subdriver
1625 */
1626
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001627static u8 ecdump_regs[256];
1628
1629static int ecdump_read(char *p)
1630{
1631 int len = 0;
1632 int i, j;
1633 u8 v;
1634
1635 len += sprintf(p + len, "EC "
1636 " +00 +01 +02 +03 +04 +05 +06 +07"
1637 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
1638 for (i = 0; i < 256; i += 16) {
1639 len += sprintf(p + len, "EC 0x%02x:", i);
1640 for (j = 0; j < 16; j++) {
1641 if (!acpi_ec_read(i + j, &v))
1642 break;
1643 if (v != ecdump_regs[i + j])
1644 len += sprintf(p + len, " *%02x", v);
1645 else
1646 len += sprintf(p + len, " %02x", v);
1647 ecdump_regs[i + j] = v;
1648 }
1649 len += sprintf(p + len, "\n");
1650 if (j != 16)
1651 break;
1652 }
1653
1654 /* These are way too dangerous to advertise openly... */
1655#if 0
1656 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
1657 " (<offset> is 00-ff, <value> is 00-ff)\n");
1658 len += sprintf(p + len, "commands:\t0x<offset> <value> "
1659 " (<offset> is 00-ff, <value> is 0-255)\n");
1660#endif
1661 return len;
1662}
1663
1664static int ecdump_write(char *buf)
1665{
1666 char *cmd;
1667 int i, v;
1668
1669 while ((cmd = next_cmd(&buf))) {
1670 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
1671 /* i and v set */
1672 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
1673 /* i and v set */
1674 } else
1675 return -EINVAL;
1676 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
1677 if (!acpi_ec_write(i, v))
1678 return -EIO;
1679 } else
1680 return -EINVAL;
1681 }
1682
1683 return 0;
1684}
1685
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001686/*************************************************************************
1687 * Backlight/brightness subdriver
1688 */
Holger Macht8acb0252006-10-20 14:30:28 -07001689
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001690static struct backlight_device *ibm_backlight_device = NULL;
Holger Macht8acb0252006-10-20 14:30:28 -07001691
Richard Purdie599a52d2007-02-10 23:07:48 +00001692static struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02001693 .get_brightness = brightness_get,
1694 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02001695};
1696
1697static int brightness_init(void)
1698{
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02001699 int b;
1700
1701 b = brightness_get(NULL);
1702 if (b < 0)
1703 return b;
1704
Yu Luming519ab5f2006-12-19 12:56:15 -08001705 ibm_backlight_device = backlight_device_register("ibm", NULL, NULL,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02001706 &ibm_backlight_data);
1707 if (IS_ERR(ibm_backlight_device)) {
1708 printk(IBM_ERR "Could not register backlight device\n");
1709 return PTR_ERR(ibm_backlight_device);
1710 }
1711
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02001712 ibm_backlight_device->props.max_brightness = 7;
1713 ibm_backlight_device->props.brightness = b;
1714 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00001715
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02001716 return 0;
1717}
1718
1719static void brightness_exit(void)
1720{
1721 if (ibm_backlight_device) {
1722 backlight_device_unregister(ibm_backlight_device);
1723 ibm_backlight_device = NULL;
1724 }
1725}
1726
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001727static int brightness_update_status(struct backlight_device *bd)
1728{
1729 return brightness_set(
1730 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
1731 bd->props.power == FB_BLANK_UNBLANK) ?
1732 bd->props.brightness : 0);
1733}
1734
1735static int brightness_get(struct backlight_device *bd)
1736{
1737 u8 level;
1738 if (!acpi_ec_read(brightness_offset, &level))
1739 return -EIO;
1740
1741 level &= 0x7;
1742
1743 return level;
1744}
1745
1746static int brightness_set(int value)
1747{
1748 int cmos_cmd, inc, i;
1749 int current_value = brightness_get(NULL);
1750
1751 value &= 7;
1752
1753 cmos_cmd = value > current_value ? TP_CMOS_BRIGHTNESS_UP : TP_CMOS_BRIGHTNESS_DOWN;
1754 inc = value > current_value ? 1 : -1;
1755 for (i = current_value; i != value; i += inc) {
1756 if (!cmos_eval(cmos_cmd))
1757 return -EIO;
1758 if (!acpi_ec_write(brightness_offset, i + inc))
1759 return -EIO;
1760 }
1761
1762 return 0;
1763}
1764
1765static int brightness_read(char *p)
1766{
1767 int len = 0;
1768 int level;
1769
1770 if ((level = brightness_get(NULL)) < 0) {
1771 len += sprintf(p + len, "level:\t\tunreadable\n");
1772 } else {
1773 len += sprintf(p + len, "level:\t\t%d\n", level & 0x7);
1774 len += sprintf(p + len, "commands:\tup, down\n");
1775 len += sprintf(p + len, "commands:\tlevel <level>"
1776 " (<level> is 0-7)\n");
1777 }
1778
1779 return len;
1780}
1781
1782static int brightness_write(char *buf)
1783{
1784 int level;
1785 int new_level;
1786 char *cmd;
1787
1788 while ((cmd = next_cmd(&buf))) {
1789 if ((level = brightness_get(NULL)) < 0)
1790 return level;
1791 level &= 7;
1792
1793 if (strlencmp(cmd, "up") == 0) {
1794 new_level = level == 7 ? 7 : level + 1;
1795 } else if (strlencmp(cmd, "down") == 0) {
1796 new_level = level == 0 ? 0 : level - 1;
1797 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
1798 new_level >= 0 && new_level <= 7) {
1799 /* new_level set */
1800 } else
1801 return -EINVAL;
1802
1803 brightness_set(new_level);
1804 }
1805
1806 return 0;
1807}
1808
1809/*************************************************************************
1810 * Volume subdriver
1811 */
1812
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001813static int volume_read(char *p)
1814{
1815 int len = 0;
1816 u8 level;
1817
1818 if (!acpi_ec_read(volume_offset, &level)) {
1819 len += sprintf(p + len, "level:\t\tunreadable\n");
1820 } else {
1821 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
1822 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
1823 len += sprintf(p + len, "commands:\tup, down, mute\n");
1824 len += sprintf(p + len, "commands:\tlevel <level>"
1825 " (<level> is 0-15)\n");
1826 }
1827
1828 return len;
1829}
1830
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001831static int volume_write(char *buf)
1832{
1833 int cmos_cmd, inc, i;
1834 u8 level, mute;
1835 int new_level, new_mute;
1836 char *cmd;
1837
1838 while ((cmd = next_cmd(&buf))) {
1839 if (!acpi_ec_read(volume_offset, &level))
1840 return -EIO;
1841 new_mute = mute = level & 0x40;
1842 new_level = level = level & 0xf;
1843
1844 if (strlencmp(cmd, "up") == 0) {
1845 if (mute)
1846 new_mute = 0;
1847 else
1848 new_level = level == 15 ? 15 : level + 1;
1849 } else if (strlencmp(cmd, "down") == 0) {
1850 if (mute)
1851 new_mute = 0;
1852 else
1853 new_level = level == 0 ? 0 : level - 1;
1854 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
1855 new_level >= 0 && new_level <= 15) {
1856 /* new_level set */
1857 } else if (strlencmp(cmd, "mute") == 0) {
1858 new_mute = 0x40;
1859 } else
1860 return -EINVAL;
1861
1862 if (new_level != level) { /* mute doesn't change */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001863 cmos_cmd = new_level > level ? TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001864 inc = new_level > level ? 1 : -1;
1865
1866 if (mute && (!cmos_eval(cmos_cmd) ||
1867 !acpi_ec_write(volume_offset, level)))
1868 return -EIO;
1869
1870 for (i = level; i != new_level; i += inc)
1871 if (!cmos_eval(cmos_cmd) ||
1872 !acpi_ec_write(volume_offset, i + inc))
1873 return -EIO;
1874
Henrique de Moraes Holschuhe062e032007-03-23 17:33:55 -03001875 if (mute && (!cmos_eval(TP_CMOS_VOLUME_MUTE) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001876 !acpi_ec_write(volume_offset,
1877 new_level + mute)))
1878 return -EIO;
1879 }
1880
1881 if (new_mute != mute) { /* level doesn't change */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001882 cmos_cmd = new_mute ? TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001883
1884 if (!cmos_eval(cmos_cmd) ||
1885 !acpi_ec_write(volume_offset, level + new_mute))
1886 return -EIO;
1887 }
1888 }
1889
1890 return 0;
1891}
1892
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001893
1894/*************************************************************************
1895 * Fan subdriver
1896 */
1897
1898/*
1899 * FAN ACCESS MODES
1900 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001901 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001902 * ACPI GFAN method: returns fan level
1903 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001904 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03001905 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001906 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001907 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001908 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
1909 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03001910 * EC 0x2f (HFSP) might be available *for reading*, but do not use
1911 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001912 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001913 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03001914 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
1915 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001916 *
1917 * Fan speed changes of any sort (including those caused by the
1918 * disengaged mode) are usually done slowly by the firmware as the
1919 * maximum ammount of fan duty cycle change per second seems to be
1920 * limited.
1921 *
1922 * Reading is not available if GFAN exists.
1923 * Writing is not available if SFAN exists.
1924 *
1925 * Bits
1926 * 7 automatic mode engaged;
1927 * (default operation mode of the ThinkPad)
1928 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03001929 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001930 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03001931 * the tachometer while the fan controller ramps up
1932 * the speed (which can take up to a few *minutes*).
1933 * Speeds up fan to 100% duty-cycle, which is far above
1934 * the standard RPM levels. It is not impossible that
1935 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001936 * 5-3 unused in some models. Extra bits for fan level
1937 * in others, but still useless as all values above
1938 * 7 map to the same speed as level 7 in these models.
1939 * 2-0 fan level (0..7 usually)
1940 * 0x00 = stop
1941 * 0x07 = max (set when temperatures critical)
1942 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001943 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001944 *
1945 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
1946 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
1947 * does so, its initial value is meaningless (0x07).
1948 *
1949 * For firmware bugs, refer to:
1950 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
1951 *
1952 * ----
1953 *
1954 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
1955 * Main fan tachometer reading (in RPM)
1956 *
1957 * This register is present on all ThinkPads with a new-style EC, and
1958 * it is known not to be present on the A21m/e, and T22, as there is
1959 * something else in offset 0x84 according to the ACPI DSDT. Other
1960 * ThinkPads from this same time period (and earlier) probably lack the
1961 * tachometer as well.
1962 *
1963 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
1964 * was never fixed by IBM to report the EC firmware version string
1965 * probably support the tachometer (like the early X models), so
1966 * detecting it is quite hard. We need more data to know for sure.
1967 *
1968 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
1969 * might result.
1970 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03001971 * FIRMWARE BUG: may go stale while the EC is switching to full speed
1972 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001973 *
1974 * For firmware bugs, refer to:
1975 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
1976 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001977 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001978 * ThinkPad X31, X40, X41. Not available in the X60.
1979 *
1980 * FANS ACPI handle: takes three arguments: low speed, medium speed,
1981 * high speed. ACPI DSDT seems to map these three speeds to levels
1982 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
1983 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
1984 *
1985 * The speeds are stored on handles
1986 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
1987 *
1988 * There are three default speed sets, acessible as handles:
1989 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
1990 *
1991 * ACPI DSDT switches which set is in use depending on various
1992 * factors.
1993 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001994 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001995 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
1996 * but the ACPI tables just mention level 7.
1997 */
1998
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001999static enum fan_status_access_mode fan_status_access_mode;
2000static enum fan_control_access_mode fan_control_access_mode;
2001static enum fan_control_commands fan_control_commands;
2002
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002003static int fan_control_status_known;
2004static u8 fan_control_initial_status;
2005
Len Brown25c68a32006-12-08 04:43:41 -05002006static void fan_watchdog_fire(struct work_struct *ignored);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002007static int fan_watchdog_maxinterval;
Len Brown25c68a32006-12-08 04:43:41 -05002008static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002009
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002010IBM_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
2011IBM_HANDLE(gfan, ec, "GFAN", /* 570 */
2012 "\\FSPD", /* 600e/x, 770e, 770x */
2013 ); /* all others */
2014IBM_HANDLE(sfan, ec, "SFAN", /* 570 */
2015 "JFNS", /* 770x-JL */
2016 ); /* all others */
2017
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002018static int fan_init(void)
2019{
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002020 fan_status_access_mode = TPACPI_FAN_NONE;
2021 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002022 fan_control_commands = 0;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002023 fan_control_status_known = 1;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002024 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002025
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002026 IBM_HANDLE_INIT(fans);
2027 IBM_HANDLE_INIT(gfan);
2028 IBM_HANDLE_INIT(sfan);
2029
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002030 if (gfan_handle) {
2031 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002032 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002033 } else {
2034 /* all other ThinkPads: note that even old-style
2035 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002036 if (likely(acpi_ec_read(fan_status_offset,
2037 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002038 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002039
2040 /* In some ThinkPads, neither the EC nor the ACPI
2041 * DSDT initialize the fan status, and it ends up
2042 * being set to 0x07 when it *could* be either
2043 * 0x07 or 0x80.
2044 *
2045 * Enable for TP-1Y (T43), TP-78 (R51e),
2046 * TP-76 (R52), TP-70 (T43, R52), which are known
2047 * to be buggy. */
2048 if (fan_control_initial_status == 0x07 &&
2049 ibm_thinkpad_ec_found &&
2050 ((ibm_thinkpad_ec_found[0] == '1' &&
2051 ibm_thinkpad_ec_found[1] == 'Y') ||
2052 (ibm_thinkpad_ec_found[0] == '7' &&
2053 (ibm_thinkpad_ec_found[1] == '6' ||
2054 ibm_thinkpad_ec_found[1] == '8' ||
2055 ibm_thinkpad_ec_found[1] == '0'))
2056 )) {
2057 printk(IBM_NOTICE
2058 "fan_init: initial fan status is "
2059 "unknown, assuming it is in auto "
2060 "mode\n");
2061 fan_control_status_known = 0;
2062 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002063 } else {
2064 printk(IBM_ERR
2065 "ThinkPad ACPI EC access misbehaving, "
2066 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002067 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002068 }
2069 }
2070
2071 if (sfan_handle) {
2072 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002073 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002074 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002075 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002076 } else {
2077 if (!gfan_handle) {
2078 /* gfan without sfan means no fan control */
2079 /* all other models implement TP EC 0x2f control */
2080
2081 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02002082 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002083 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002084 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002085 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002086 TPACPI_FAN_CMD_SPEED |
2087 TPACPI_FAN_CMD_LEVEL |
2088 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002089 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002090 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02002091 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002092 TPACPI_FAN_CMD_LEVEL |
2093 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002094 }
2095 }
2096 }
2097
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002098 return (fan_status_access_mode != TPACPI_FAN_NONE ||
2099 fan_control_access_mode != TPACPI_FAN_WR_NONE)?
2100 0 : 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002101}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002102
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02002103static int fan_get_status(u8 *status)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002104{
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02002105 u8 s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002106
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02002107 /* TODO:
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002108 * Add TPACPI_FAN_RD_ACPI_FANS ? */
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02002109
Henrique de Moraes Holschuh3ef8a602006-11-24 11:47:10 -02002110 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002111 case TPACPI_FAN_RD_ACPI_GFAN:
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002112 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02002113
2114 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002115 return -EIO;
2116
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02002117 if (likely(status))
2118 *status = s & 0x07;
2119
2120 break;
2121
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002122 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02002123 /* all except 570, 600e/x, 770e, 770x */
2124 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
2125 return -EIO;
2126
2127 if (likely(status))
2128 *status = s;
2129
2130 break;
2131
2132 default:
2133 return -ENXIO;
2134 }
2135
2136 return 0;
2137}
2138
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002139static void fan_exit(void)
2140{
2141 cancel_delayed_work(&fan_watchdog_task);
2142 flush_scheduled_work();
2143}
2144
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02002145static int fan_get_speed(unsigned int *speed)
2146{
2147 u8 hi, lo;
2148
2149 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002150 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02002151 /* all except 570, 600e/x, 770e, 770x */
2152 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
2153 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
2154 return -EIO;
2155
2156 if (likely(speed))
2157 *speed = (hi << 8) | lo;
2158
2159 break;
2160
2161 default:
2162 return -ENXIO;
2163 }
2164
2165 return 0;
2166}
2167
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002168static void fan_watchdog_fire(struct work_struct *ignored)
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002169{
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002170 printk(IBM_NOTICE "fan watchdog: enabling fan\n");
2171 if (fan_set_enable()) {
2172 printk(IBM_ERR "fan watchdog: error while enabling fan\n");
2173 /* reschedule for later */
2174 fan_watchdog_reset();
2175 }
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002176}
2177
2178static void fan_watchdog_reset(void)
2179{
2180 static int fan_watchdog_active = 0;
2181
2182 if (fan_watchdog_active)
2183 cancel_delayed_work(&fan_watchdog_task);
2184
2185 if (fan_watchdog_maxinterval > 0) {
2186 fan_watchdog_active = 1;
2187 if (!schedule_delayed_work(&fan_watchdog_task,
2188 msecs_to_jiffies(fan_watchdog_maxinterval
2189 * 1000))) {
2190 printk(IBM_ERR "failed to schedule the fan watchdog, "
2191 "watchdog will not trigger\n");
2192 }
2193 } else
2194 fan_watchdog_active = 0;
2195}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002196
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002197static int fan_set_level(int level)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002198{
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002199 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002200 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002201 if (level >= 0 && level <= 7) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002202 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
2203 return -EIO;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002204 } else
2205 return -EINVAL;
2206 break;
2207
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002208 case TPACPI_FAN_WR_ACPI_FANS:
2209 case TPACPI_FAN_WR_TPEC:
2210 if ((level != TP_EC_FAN_AUTO) &&
2211 (level != TP_EC_FAN_FULLSPEED) &&
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02002212 ((level < 0) || (level > 7)))
2213 return -EINVAL;
2214
2215 if (!acpi_ec_write(fan_status_offset, level))
2216 return -EIO;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002217 else
2218 fan_control_status_known = 1;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02002219 break;
2220
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002221 default:
2222 return -ENXIO;
2223 }
2224 return 0;
2225}
2226
2227static int fan_set_enable(void)
2228{
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002229 u8 s;
2230 int rc;
2231
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002232 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002233 case TPACPI_FAN_WR_ACPI_FANS:
2234 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002235 if ((rc = fan_get_status(&s)) < 0)
2236 return rc;
2237
2238 /* Don't go out of emergency fan mode */
2239 if (s != 7)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002240 s = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002241
2242 if (!acpi_ec_write(fan_status_offset, s))
2243 return -EIO;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002244 else
2245 fan_control_status_known = 1;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002246 break;
2247
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002248 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002249 if ((rc = fan_get_status(&s)) < 0)
2250 return rc;
2251
2252 s &= 0x07;
2253
2254 /* Set fan to at least level 4 */
2255 if (s < 4)
2256 s = 4;
2257
2258 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002259 return -EIO;
2260 break;
2261
2262 default:
2263 return -ENXIO;
2264 }
2265 return 0;
2266}
2267
2268static int fan_set_disable(void)
2269{
2270 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002271 case TPACPI_FAN_WR_ACPI_FANS:
2272 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002273 if (!acpi_ec_write(fan_status_offset, 0x00))
2274 return -EIO;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002275 else
2276 fan_control_status_known = 1;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002277 break;
2278
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002279 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002280 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
2281 return -EIO;
2282 break;
2283
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002284 default:
2285 return -ENXIO;
2286 }
2287 return 0;
2288}
2289
2290static int fan_set_speed(int speed)
2291{
2292 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002293 case TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002294 if (speed >= 0 && speed <= 65535) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002295 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
2296 speed, speed, speed))
2297 return -EIO;
2298 } else
2299 return -EINVAL;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002300 break;
2301
2302 default:
2303 return -ENXIO;
2304 }
2305 return 0;
2306}
2307
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002308static int fan_read(char *p)
2309{
2310 int len = 0;
2311 int rc;
2312 u8 status;
2313 unsigned int speed = 0;
2314
2315 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002316 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002317 /* 570, 600e/x, 770e, 770x */
2318 if ((rc = fan_get_status(&status)) < 0)
2319 return rc;
2320
2321 len += sprintf(p + len, "status:\t\t%s\n"
2322 "level:\t\t%d\n",
2323 (status != 0) ? "enabled" : "disabled", status);
2324 break;
2325
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002326 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002327 /* all except 570, 600e/x, 770e, 770x */
2328 if ((rc = fan_get_status(&status)) < 0)
2329 return rc;
2330
2331 if (unlikely(!fan_control_status_known)) {
2332 if (status != fan_control_initial_status)
2333 fan_control_status_known = 1;
2334 else
2335 /* Return most likely status. In fact, it
2336 * might be the only possible status */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002337 status = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002338 }
2339
2340 len += sprintf(p + len, "status:\t\t%s\n",
2341 (status != 0) ? "enabled" : "disabled");
2342
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002343 if ((rc = fan_get_speed(&speed)) < 0)
2344 return rc;
2345
2346 len += sprintf(p + len, "speed:\t\t%d\n", speed);
2347
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002348 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002349 /* Disengaged mode takes precedence */
2350 len += sprintf(p + len, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002351 else if (status & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002352 len += sprintf(p + len, "level:\t\tauto\n");
2353 else
2354 len += sprintf(p + len, "level:\t\t%d\n", status);
2355 break;
2356
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002357 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002358 default:
2359 len += sprintf(p + len, "status:\t\tnot supported\n");
2360 }
2361
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002362 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002363 len += sprintf(p + len, "commands:\tlevel <level>");
2364
2365 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002366 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002367 len += sprintf(p + len, " (<level> is 0-7)\n");
2368 break;
2369
2370 default:
2371 len += sprintf(p + len, " (<level> is 0-7, "
2372 "auto, disengaged)\n");
2373 break;
2374 }
2375 }
2376
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002377 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002378 len += sprintf(p + len, "commands:\tenable, disable\n"
2379 "commands:\twatchdog <timeout> (<timeout> is 0 (off), "
2380 "1-120 (seconds))\n");
2381
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002382 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002383 len += sprintf(p + len, "commands:\tspeed <speed>"
2384 " (<speed> is 0-65535)\n");
2385
2386 return len;
2387}
2388
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002389static int fan_write_cmd_level(const char *cmd, int *rc)
2390{
2391 int level;
2392
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02002393 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002394 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02002395 else if (strlencmp(cmd, "level disengaged") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002396 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02002397 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002398 return 0;
2399
2400 if ((*rc = fan_set_level(level)) == -ENXIO)
2401 printk(IBM_ERR "level command accepted for unsupported "
2402 "access mode %d", fan_control_access_mode);
2403
2404 return 1;
2405}
2406
2407static int fan_write_cmd_enable(const char *cmd, int *rc)
2408{
2409 if (strlencmp(cmd, "enable") != 0)
2410 return 0;
2411
2412 if ((*rc = fan_set_enable()) == -ENXIO)
2413 printk(IBM_ERR "enable command accepted for unsupported "
2414 "access mode %d", fan_control_access_mode);
2415
2416 return 1;
2417}
2418
2419static int fan_write_cmd_disable(const char *cmd, int *rc)
2420{
2421 if (strlencmp(cmd, "disable") != 0)
2422 return 0;
2423
2424 if ((*rc = fan_set_disable()) == -ENXIO)
2425 printk(IBM_ERR "disable command accepted for unsupported "
2426 "access mode %d", fan_control_access_mode);
2427
2428 return 1;
2429}
2430
2431static int fan_write_cmd_speed(const char *cmd, int *rc)
2432{
2433 int speed;
2434
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02002435 /* TODO:
2436 * Support speed <low> <medium> <high> ? */
2437
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002438 if (sscanf(cmd, "speed %d", &speed) != 1)
2439 return 0;
2440
2441 if ((*rc = fan_set_speed(speed)) == -ENXIO)
2442 printk(IBM_ERR "speed command accepted for unsupported "
2443 "access mode %d", fan_control_access_mode);
2444
2445 return 1;
2446}
2447
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002448static int fan_write_cmd_watchdog(const char *cmd, int *rc)
2449{
2450 int interval;
2451
2452 if (sscanf(cmd, "watchdog %d", &interval) != 1)
2453 return 0;
2454
2455 if (interval < 0 || interval > 120)
2456 *rc = -EINVAL;
2457 else
2458 fan_watchdog_maxinterval = interval;
2459
2460 return 1;
2461}
2462
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002463static int fan_write(char *buf)
2464{
2465 char *cmd;
2466 int rc = 0;
2467
2468 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002469 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002470 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002471 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002472 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002473 fan_write_cmd_disable(cmd, &rc) ||
2474 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002475 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002476 fan_write_cmd_speed(cmd, &rc))
2477 )
2478 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002479 else if (!rc)
2480 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002481 }
2482
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002483 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002484}
2485
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002486/****************************************************************************
2487 ****************************************************************************
2488 *
2489 * Infrastructure
2490 *
2491 ****************************************************************************
2492 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002494/* /proc support */
2495static struct proc_dir_entry *proc_dir = NULL;
2496
2497/* Subdriver registry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498static struct ibm_struct ibms[] = {
2499 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002500 .name = "driver",
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -03002501 .init = thinkpad_acpi_driver_init,
2502 .read = thinkpad_acpi_driver_read,
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002503 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002505 .name = "hotkey",
2506 .hid = IBM_HKEY_HID,
2507 .init = hotkey_init,
2508 .read = hotkey_read,
2509 .write = hotkey_write,
2510 .exit = hotkey_exit,
2511 .notify = hotkey_notify,
2512 .handle = &hkey_handle,
2513 .type = ACPI_DEVICE_NOTIFY,
2514 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002516 .name = "bluetooth",
2517 .init = bluetooth_init,
2518 .read = bluetooth_read,
2519 .write = bluetooth_write,
2520 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 {
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002522 .name = "wan",
2523 .init = wan_init,
2524 .read = wan_read,
2525 .write = wan_write,
2526 .experimental = 1,
2527 },
2528 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002529 .name = "video",
2530 .init = video_init,
2531 .read = video_read,
2532 .write = video_write,
2533 .exit = video_exit,
2534 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002536 .name = "light",
2537 .init = light_init,
2538 .read = light_read,
2539 .write = light_write,
2540 },
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03002541#ifdef CONFIG_THINKPAD_ACPI_DOCK
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002543 .name = "dock",
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002544 .init = dock_init,
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002545 .read = dock_read,
2546 .write = dock_write,
2547 .notify = dock_notify,
2548 .handle = &dock_handle,
2549 .type = ACPI_SYSTEM_NOTIFY,
2550 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002552 .name = "dock",
2553 .hid = IBM_PCI_HID,
2554 .notify = dock_notify,
2555 .handle = &pci_handle,
2556 .type = ACPI_SYSTEM_NOTIFY,
2557 },
Kristen Accardi63e5f242006-02-23 17:56:06 -08002558#endif
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03002559#ifdef CONFIG_THINKPAD_ACPI_BAY
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002561 .name = "bay",
2562 .init = bay_init,
2563 .read = bay_read,
2564 .write = bay_write,
2565 .notify = bay_notify,
2566 .handle = &bay_handle,
2567 .type = ACPI_SYSTEM_NOTIFY,
2568 },
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03002569#endif /* CONFIG_THINKPAD_ACPI_BAY */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002571 .name = "cmos",
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002572 .init = cmos_init,
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002573 .read = cmos_read,
2574 .write = cmos_write,
2575 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002577 .name = "led",
2578 .init = led_init,
2579 .read = led_read,
2580 .write = led_write,
2581 },
2582 {
2583 .name = "beep",
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002584 .init = beep_init,
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002585 .read = beep_read,
2586 .write = beep_write,
2587 },
2588 {
2589 .name = "thermal",
2590 .init = thermal_init,
2591 .read = thermal_read,
2592 },
2593 {
2594 .name = "ecdump",
2595 .read = ecdump_read,
2596 .write = ecdump_write,
2597 .experimental = 1,
2598 },
2599 {
2600 .name = "brightness",
2601 .read = brightness_read,
2602 .write = brightness_write,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02002603 .init = brightness_init,
2604 .exit = brightness_exit,
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002605 },
2606 {
2607 .name = "volume",
2608 .read = volume_read,
2609 .write = volume_write,
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002610 },
2611 {
2612 .name = "fan",
2613 .read = fan_read,
2614 .write = fan_write,
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002615 .init = fan_init,
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002616 .exit = fan_exit,
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002617 .experimental = 1,
2618 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002621/*
2622 * Module and infrastructure proble, init and exit handling
2623 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002625static int __init ibm_init(struct ibm_struct *ibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626{
2627 int ret;
2628 struct proc_dir_entry *entry;
2629
2630 if (ibm->experimental && !experimental)
2631 return 0;
2632
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 if (ibm->init) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002634 ret = ibm->init();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002635 if (ret > 0)
2636 return 0; /* probe failed */
2637 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 return ret;
2639 ibm->init_called = 1;
2640 }
2641
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002642 if (ibm->hid) {
2643 ret = register_tpacpi_subdriver(ibm);
2644 if (ret)
2645 goto err_out;
2646 }
2647
2648 if (ibm->notify) {
2649 ret = setup_notify(ibm);
2650 if (ret == -ENODEV) {
2651 printk(IBM_NOTICE "disabling subdriver %s\n",
2652 ibm->name);
2653 ret = 0;
2654 goto err_out;
2655 }
2656 if (ret < 0)
2657 goto err_out;
2658 }
2659
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002660 if (ibm->read) {
2661 entry = create_proc_entry(ibm->name,
2662 S_IFREG | S_IRUGO | S_IWUSR,
2663 proc_dir);
2664 if (!entry) {
2665 printk(IBM_ERR "unable to create proc entry %s\n",
2666 ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002667 ret = -ENODEV;
2668 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002669 }
2670 entry->owner = THIS_MODULE;
2671 entry->data = ibm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 entry->read_proc = &dispatch_read;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002673 if (ibm->write)
2674 entry->write_proc = &dispatch_write;
2675 ibm->proc_created = 1;
2676 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002679
2680err_out:
2681 ibm_exit(ibm);
2682 return (ret < 0)? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683}
2684
2685static void ibm_exit(struct ibm_struct *ibm)
2686{
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002687 if (ibm->notify_installed) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 acpi_remove_notify_handler(*ibm->handle, ibm->type,
2689 dispatch_notify);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002690 ibm->notify_installed = 0;
2691 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002693 if (ibm->proc_created) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 remove_proc_entry(ibm->name, proc_dir);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002695 ibm->proc_created = 0;
2696 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697
2698 if (ibm->driver_registered) {
2699 acpi_bus_unregister_driver(ibm->driver);
2700 kfree(ibm->driver);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002701 ibm->driver = NULL;
2702 ibm->driver_registered = 0;
2703 }
2704
2705 if (ibm->init_called && ibm->exit) {
2706 ibm->exit();
2707 ibm->init_called = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 }
2709}
2710
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002711/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002713static char *ibm_thinkpad_ec_found = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02002715static char* __init check_dmi_for_ec(void)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002716{
2717 struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02002718 char ec_fw_string[18];
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002719
2720 /*
2721 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
2722 * X32 or newer, all Z series; Some models must have an
2723 * up-to-date BIOS or they will not be detected.
2724 *
2725 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
2726 */
2727 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02002728 if (sscanf(dev->name,
2729 "IBM ThinkPad Embedded Controller -[%17c",
2730 ec_fw_string) == 1) {
2731 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
2732 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
2733 return kstrdup(ec_fw_string, GFP_KERNEL);
2734 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002735 }
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02002736 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737}
2738
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002739static int __init probe_for_thinkpad(void)
2740{
2741 int is_thinkpad;
2742
2743 if (acpi_disabled)
2744 return -ENODEV;
2745
2746 /*
2747 * Non-ancient models have better DMI tagging, but very old models
2748 * don't.
2749 */
2750 is_thinkpad = dmi_name_in_vendors("ThinkPad");
2751
2752 /* ec is required because many other handles are relative to it */
2753 IBM_HANDLE_INIT(ec);
2754 if (!ec_handle) {
2755 if (is_thinkpad)
2756 printk(IBM_ERR
2757 "Not yet supported ThinkPad detected!\n");
2758 return -ENODEV;
2759 }
2760
2761 return 0;
2762}
2763
2764
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002765/* Module init, exit, parameters */
2766
2767static int __init set_ibm_param(const char *val, struct kernel_param *kp)
2768{
2769 unsigned int i;
2770
2771 for (i = 0; i < ARRAY_SIZE(ibms); i++)
2772 if (strcmp(ibms[i].name, kp->name) == 0 && ibms[i].write) {
2773 if (strlen(val) > sizeof(ibms[i].param) - 2)
2774 return -ENOSPC;
2775 strcpy(ibms[i].param, val);
2776 strcat(ibms[i].param, ",");
2777 return 0;
2778 }
2779
2780 return -EINVAL;
2781}
2782
2783static int experimental;
2784module_param(experimental, int, 0);
2785
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03002786static u32 dbg_level;
2787module_param_named(debug, dbg_level, uint, 0);
2788
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002789#define IBM_PARAM(feature) \
2790 module_param_call(feature, set_ibm_param, NULL, NULL, 0)
2791
2792IBM_PARAM(hotkey);
2793IBM_PARAM(bluetooth);
2794IBM_PARAM(video);
2795IBM_PARAM(light);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03002796#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002797IBM_PARAM(dock);
2798#endif
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03002799#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002800IBM_PARAM(bay);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03002801#endif /* CONFIG_THINKPAD_ACPI_BAY */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002802IBM_PARAM(cmos);
2803IBM_PARAM(led);
2804IBM_PARAM(beep);
2805IBM_PARAM(ecdump);
2806IBM_PARAM(brightness);
2807IBM_PARAM(volume);
2808IBM_PARAM(fan);
2809
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03002810static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811{
2812 int ret, i;
2813
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002814 ret = probe_for_thinkpad();
2815 if (ret)
2816 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002818 ibm_thinkpad_ec_found = check_dmi_for_ec();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002819 IBM_HANDLE_INIT(ecrd);
2820 IBM_HANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821
2822 proc_dir = proc_mkdir(IBM_DIR, acpi_root_dir);
2823 if (!proc_dir) {
2824 printk(IBM_ERR "unable to create proc dir %s", IBM_DIR);
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03002825 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 return -ENODEV;
2827 }
2828 proc_dir->owner = THIS_MODULE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002829
2830 for (i = 0; i < ARRAY_SIZE(ibms); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 ret = ibm_init(&ibms[i]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002832 if (ret >= 0 && *ibms[i].param)
2833 ret = ibms[i].write(ibms[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03002835 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 return ret;
2837 }
2838 }
2839
2840 return 0;
2841}
2842
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03002843static void thinkpad_acpi_module_exit(void)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002844{
2845 int i;
2846
2847 for (i = ARRAY_SIZE(ibms) - 1; i >= 0; i--)
2848 ibm_exit(&ibms[i]);
2849
2850 if (proc_dir)
2851 remove_proc_entry(IBM_DIR, acpi_root_dir);
2852
2853 if (ibm_thinkpad_ec_found)
2854 kfree(ibm_thinkpad_ec_found);
2855}
2856
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03002857module_init(thinkpad_acpi_module_init);
2858module_exit(thinkpad_acpi_module_exit);