blob: a31d00d570cb3774cd320b40f5a0b03317291a7f [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"
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -030025#define TPACPI_SYSFS_VERSION 0x000100
Borislav Deianov78f81cc2005-08-17 00:00:00 -040026
27/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 * Changelog:
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -030029 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
30 * drivers/misc.
Henrique de Moraes Holschuhf9ff43a2006-11-25 16:37:38 -020031 *
32 * 2006-11-22 0.13 new maintainer
33 * changelog now lives in git commit history, and will
34 * not be updated further in-file.
Henrique de Moraes Holschuh837ca6d2007-03-23 17:33:54 -030035 *
Borislav Deianov78f81cc2005-08-17 00:00:00 -040036 * 2005-08-17 0.12 fix compilation on 2.6.13-rc kernels
37 * 2005-03-17 0.11 support for 600e, 770x
38 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
39 * support for 770e, G41
40 * G40 and G41 don't have a thinklight
41 * temperatures no longer experimental
42 * experimental brightness control
43 * experimental volume control
44 * experimental fan enable/disable
Henrique de Moraes Holschuh837ca6d2007-03-23 17:33:54 -030045 * 2005-01-16 0.10 fix module loading on R30, R31
Borislav Deianov78f81cc2005-08-17 00:00:00 -040046 * 2005-01-16 0.9 support for 570, R30, R31
47 * ultrabay support on A22p, A3x
48 * limit arg for cmos, led, beep, drop experimental status
49 * more capable led control on A21e, A22p, T20-22, X20
50 * experimental temperatures and fan speed
51 * experimental embedded controller register dump
52 * mark more functions as __init, drop incorrect __exit
53 * use MODULE_VERSION
54 * thanks to Henrik Brix Andersen <brix@gentoo.org>
55 * fix parameter passing on module loading
56 * thanks to Rusty Russell <rusty@rustcorp.com.au>
57 * thanks to Jim Radford <radford@blackbean.org>
58 * 2004-11-08 0.8 fix init error case, don't return from a macro
59 * thanks to Chris Wright <chrisw@osdl.org>
60 * 2004-10-23 0.7 fix module loading on A21e, A22p, T20, T21, X20
61 * fix led control on A21e
62 * 2004-10-19 0.6 use acpi_bus_register_driver() to claim HKEY device
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 * 2004-10-18 0.5 thinklight support on A21e, G40, R32, T20, T21, X20
64 * proc file format changed
65 * video_switch command
66 * experimental cmos control
67 * experimental led control
68 * experimental acpi sounds
Borislav Deianov78f81cc2005-08-17 00:00:00 -040069 * 2004-09-16 0.4 support for module parameters
70 * hotkey mask can be prefixed by 0x
71 * video output switching
72 * video expansion control
73 * ultrabay eject support
74 * removed lcd brightness/on/off control, didn't work
75 * 2004-08-17 0.3 support for R40
76 * lcd off, brightness control
77 * thinklight on/off
78 * 2004-08-14 0.2 support for T series, X20
79 * bluetooth enable/disable
80 * hotkey events disabled by default
81 * removed fan control, currently useless
82 * 2004-08-09 0.1 initial release, support for X series
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 */
84
Henrique de Moraes Holschuhf21f85d2007-03-29 01:58:40 -030085#include "thinkpad_acpi.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Henrique de Moraes Holschuhf9ff43a2006-11-25 16:37:38 -020087MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
Borislav Deianov78f81cc2005-08-17 00:00:00 -040088MODULE_DESCRIPTION(IBM_DESC);
89MODULE_VERSION(IBM_VERSION);
90MODULE_LICENSE("GPL");
91
Henrique de Moraes Holschuhd903ac52007-03-29 01:58:42 -030092/* Please remove this in year 2009 */
93MODULE_ALIAS("ibm_acpi");
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095#define __unused __attribute__ ((unused))
96
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -030097/****************************************************************************
98 ****************************************************************************
99 *
100 * ACPI Helpers and device model
101 *
102 ****************************************************************************
103 ****************************************************************************/
104
105/*************************************************************************
106 * ACPI basic handles
107 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109static acpi_handle root_handle = NULL;
110
111#define IBM_HANDLE(object, parent, paths...) \
112 static acpi_handle object##_handle; \
113 static acpi_handle *object##_parent = &parent##_handle; \
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400114 static char *object##_path; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 static char *object##_paths[] = { paths }
116
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400117IBM_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
118 "\\_SB.PCI.ISA.EC", /* 570 */
119 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
120 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
121 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
122 "\\_SB.PCI0.ICH3.EC0", /* R31 */
123 "\\_SB.PCI0.LPC.EC", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300124 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300126IBM_HANDLE(ecrd, ec, "ECRD"); /* 570 */
127IBM_HANDLE(ecwr, ec, "ECWR"); /* 570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300129
130/*************************************************************************
131 * Misc ACPI handles
132 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400134IBM_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, T4x, X31, X40 */
135 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
136 "\\CMS", /* R40, R40e */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300137 ); /* all others */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400138
139IBM_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
140 "^HKEY", /* R30, R31 */
141 "HKEY", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300142 ); /* 570 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400143
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400144
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300145/*************************************************************************
146 * ACPI helpers
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200147 */
148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149static int acpi_evalf(acpi_handle handle,
150 void *res, char *method, char *fmt, ...)
151{
152 char *fmt0 = fmt;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400153 struct acpi_object_list params;
154 union acpi_object in_objs[IBM_MAX_ACPI_ARGS];
155 struct acpi_buffer result, *resultp;
156 union acpi_object out_obj;
157 acpi_status status;
158 va_list ap;
159 char res_type;
160 int success;
161 int quiet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163 if (!*fmt) {
164 printk(IBM_ERR "acpi_evalf() called with empty format\n");
165 return 0;
166 }
167
168 if (*fmt == 'q') {
169 quiet = 1;
170 fmt++;
171 } else
172 quiet = 0;
173
174 res_type = *(fmt++);
175
176 params.count = 0;
177 params.pointer = &in_objs[0];
178
179 va_start(ap, fmt);
180 while (*fmt) {
181 char c = *(fmt++);
182 switch (c) {
183 case 'd': /* int */
184 in_objs[params.count].integer.value = va_arg(ap, int);
185 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
186 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400187 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 default:
189 printk(IBM_ERR "acpi_evalf() called "
190 "with invalid format character '%c'\n", c);
191 return 0;
192 }
193 }
194 va_end(ap);
195
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400196 if (res_type != 'v') {
197 result.length = sizeof(out_obj);
198 result.pointer = &out_obj;
199 resultp = &result;
200 } else
201 resultp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400203 status = acpi_evaluate_object(handle, method, &params, resultp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205 switch (res_type) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400206 case 'd': /* int */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 if (res)
208 *(int *)res = out_obj.integer.value;
209 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
210 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400211 case 'v': /* void */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 success = status == AE_OK;
213 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400214 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 default:
216 printk(IBM_ERR "acpi_evalf() called "
217 "with invalid format character '%c'\n", res_type);
218 return 0;
219 }
220
221 if (!success && !quiet)
222 printk(IBM_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
223 method, fmt0, status);
224
225 return success;
226}
227
228static void __unused acpi_print_int(acpi_handle handle, char *method)
229{
230 int i;
231
232 if (acpi_evalf(handle, &i, method, "d"))
233 printk(IBM_INFO "%s = 0x%x\n", method, i);
234 else
235 printk(IBM_ERR "error calling %s\n", method);
236}
237
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300238static int acpi_ec_read(int i, u8 * p)
239{
240 int v;
241
242 if (ecrd_handle) {
243 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
244 return 0;
245 *p = v;
246 } else {
247 if (ec_read(i, p) < 0)
248 return 0;
249 }
250
251 return 1;
252}
253
254static int acpi_ec_write(int i, u8 v)
255{
256 if (ecwr_handle) {
257 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
258 return 0;
259 } else {
260 if (ec_write(i, v) < 0)
261 return 0;
262 }
263
264 return 1;
265}
266
267static int _sta(acpi_handle handle)
268{
269 int status;
270
271 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
272 status = 0;
273
274 return status;
275}
276
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -0300277static int issue_thinkpad_cmos_command(int cmos_cmd)
278{
279 if (!cmos_handle)
280 return -ENXIO;
281
282 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
283 return -EIO;
284
285 return 0;
286}
287
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300288/*************************************************************************
289 * ACPI device model
290 */
291
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300292static void drv_acpi_handle_init(char *name,
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300293 acpi_handle *handle, acpi_handle parent,
294 char **paths, int num_paths, char **path)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300295{
296 int i;
297 acpi_status status;
298
299 for (i = 0; i < num_paths; i++) {
300 status = acpi_get_handle(parent, paths[i], handle);
301 if (ACPI_SUCCESS(status)) {
302 *path = paths[i];
303 return;
304 }
305 }
306
307 *handle = NULL;
308}
309
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300310static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300311{
312 struct ibm_struct *ibm = data;
313
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300314 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300315 return;
316
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300317 ibm->acpi->notify(ibm, event);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300318}
319
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300320static int __init setup_acpi_notify(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300321{
322 acpi_status status;
323 int ret;
324
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300325 BUG_ON(!ibm->acpi);
326
327 if (!*ibm->acpi->handle)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300328 return 0;
329
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300330 dbg_printk(TPACPI_DBG_INIT,
331 "setting up ACPI notify for %s\n", ibm->name);
332
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300333 ret = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300334 if (ret < 0) {
335 printk(IBM_ERR "%s device not present\n", ibm->name);
336 return -ENODEV;
337 }
338
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300339 acpi_driver_data(ibm->acpi->device) = ibm;
340 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300341 IBM_ACPI_EVENT_PREFIX,
342 ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300343
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300344 status = acpi_install_notify_handler(*ibm->acpi->handle,
345 ibm->acpi->type, dispatch_acpi_notify, ibm);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300346 if (ACPI_FAILURE(status)) {
347 if (status == AE_ALREADY_EXISTS) {
348 printk(IBM_NOTICE "another device driver is already handling %s events\n",
349 ibm->name);
350 } else {
351 printk(IBM_ERR "acpi_install_notify_handler(%s) failed: %d\n",
352 ibm->name, status);
353 }
354 return -ENODEV;
355 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300356 ibm->flags.acpi_notify_installed = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300357 return 0;
358}
359
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300360static int __init tpacpi_device_add(struct acpi_device *device)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300361{
362 return 0;
363}
364
Henrique de Moraes Holschuh67001212007-04-21 11:08:25 -0300365static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300366{
367 int ret;
368
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300369 dbg_printk(TPACPI_DBG_INIT,
370 "registering %s as an ACPI driver\n", ibm->name);
371
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300372 BUG_ON(!ibm->acpi);
373
374 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
375 if (!ibm->acpi->driver) {
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300376 printk(IBM_ERR "kzalloc(ibm->driver) failed\n");
377 return -ENOMEM;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300378 }
379
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300380 sprintf(ibm->acpi->driver->name, "%s_%s", IBM_NAME, ibm->name);
381 ibm->acpi->driver->ids = ibm->acpi->hid;
382 ibm->acpi->driver->ops.add = &tpacpi_device_add;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300383
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300384 ret = acpi_bus_register_driver(ibm->acpi->driver);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300385 if (ret < 0) {
386 printk(IBM_ERR "acpi_bus_register_driver(%s) failed: %d\n",
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300387 ibm->acpi->hid, ret);
388 kfree(ibm->acpi->driver);
389 ibm->acpi->driver = NULL;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300390 } else if (!ret)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300391 ibm->flags.acpi_driver_registered = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300392
393 return ret;
394}
395
396
397/****************************************************************************
398 ****************************************************************************
399 *
400 * Procfs Helpers
401 *
402 ****************************************************************************
403 ****************************************************************************/
404
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300405static int dispatch_procfs_read(char *page, char **start, off_t off,
406 int count, int *eof, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300407{
408 struct ibm_struct *ibm = data;
409 int len;
410
411 if (!ibm || !ibm->read)
412 return -EINVAL;
413
414 len = ibm->read(page);
415 if (len < 0)
416 return len;
417
418 if (len <= off + count)
419 *eof = 1;
420 *start = page + off;
421 len -= off;
422 if (len > count)
423 len = count;
424 if (len < 0)
425 len = 0;
426
427 return len;
428}
429
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300430static int dispatch_procfs_write(struct file *file,
431 const char __user * userbuf,
432 unsigned long count, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300433{
434 struct ibm_struct *ibm = data;
435 char *kernbuf;
436 int ret;
437
438 if (!ibm || !ibm->write)
439 return -EINVAL;
440
441 kernbuf = kmalloc(count + 2, GFP_KERNEL);
442 if (!kernbuf)
443 return -ENOMEM;
444
445 if (copy_from_user(kernbuf, userbuf, count)) {
446 kfree(kernbuf);
447 return -EFAULT;
448 }
449
450 kernbuf[count] = 0;
451 strcat(kernbuf, ",");
452 ret = ibm->write(kernbuf);
453 if (ret == 0)
454 ret = count;
455
456 kfree(kernbuf);
457
458 return ret;
459}
460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461static char *next_cmd(char **cmds)
462{
463 char *start = *cmds;
464 char *end;
465
466 while ((end = strchr(start, ',')) && end == start)
467 start = end + 1;
468
469 if (!end)
470 return NULL;
471
472 *end = 0;
473 *cmds = end + 1;
474 return start;
475}
476
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300477
478/****************************************************************************
479 ****************************************************************************
480 *
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300481 * Device model: hwmon and platform
482 *
483 ****************************************************************************
484 ****************************************************************************/
485
486static struct platform_device *tpacpi_pdev = NULL;
487static struct class_device *tpacpi_hwmon = NULL;
488
489static struct platform_driver tpacpi_pdriver = {
490 .driver = {
491 .name = IBM_DRVR_NAME,
492 .owner = THIS_MODULE,
493 },
494};
495
496
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -0300497/*************************************************************************
498 * thinkpad-acpi driver attributes
499 */
500
501/* interface_version --------------------------------------------------- */
502static ssize_t tpacpi_driver_interface_version_show(
503 struct device_driver *drv,
504 char *buf)
505{
506 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
507}
508
509static DRIVER_ATTR(interface_version, S_IRUGO,
510 tpacpi_driver_interface_version_show, NULL);
511
512/* debug_level --------------------------------------------------------- */
513static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
514 char *buf)
515{
516 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
517}
518
519static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
520 const char *buf, size_t count)
521{
522 unsigned long t;
523 char *endp;
524
525 t = simple_strtoul(buf, &endp, 0);
526 while (*endp && isspace(*endp))
527 endp++;
528 if (*endp)
529 return -EINVAL;
530
531 dbg_level = t;
532
533 return count;
534}
535
536static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
537 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
538
539/* version ------------------------------------------------------------- */
540static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
541 char *buf)
542{
543 return snprintf(buf, PAGE_SIZE, "%s v%s\n", IBM_DESC, IBM_VERSION);
544}
545
546static DRIVER_ATTR(version, S_IRUGO,
547 tpacpi_driver_version_show, NULL);
548
549/* --------------------------------------------------------------------- */
550
551static struct driver_attribute* tpacpi_driver_attributes[] = {
552 &driver_attr_debug_level, &driver_attr_version,
553 &driver_attr_interface_version,
554};
555
556static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
557{
558 int i, res;
559
560 i = 0;
561 res = 0;
562 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
563 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
564 i++;
565 }
566
567 return res;
568}
569
570static void tpacpi_remove_driver_attributes(struct device_driver *drv)
571{
572 int i;
573
574 for(i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
575 driver_remove_file(drv, tpacpi_driver_attributes[i]);
576}
577
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300578/****************************************************************************
579 ****************************************************************************
580 *
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300581 * Subdrivers
582 *
583 ****************************************************************************
584 ****************************************************************************/
585
586/*************************************************************************
Henrique de Moraes Holschuh142cfc92007-04-21 11:08:26 -0300587 * thinkpad-acpi init subdriver
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300588 */
589
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300590static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
592 printk(IBM_INFO "%s v%s\n", IBM_DESC, IBM_VERSION);
593 printk(IBM_INFO "%s\n", IBM_URL);
594
Henrique de Moraes Holschuh3945ac32007-02-06 19:13:44 -0200595 if (ibm_thinkpad_ec_found)
596 printk(IBM_INFO "ThinkPad EC firmware %s\n",
597 ibm_thinkpad_ec_found);
598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 return 0;
600}
601
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300602static int thinkpad_acpi_driver_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603{
604 int len = 0;
605
606 len += sprintf(p + len, "driver:\t\t%s\n", IBM_DESC);
607 len += sprintf(p + len, "version:\t%s\n", IBM_VERSION);
608
609 return len;
610}
611
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300612static struct ibm_struct thinkpad_acpi_driver_data = {
613 .name = "driver",
614 .read = thinkpad_acpi_driver_read,
615};
616
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300617/*************************************************************************
618 * Hotkey subdriver
619 */
620
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400621static int hotkey_orig_status;
622static int hotkey_orig_mask;
623
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300624static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300625{
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300626 int res;
627
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300628 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
629
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300630 IBM_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300631
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300632 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300633 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300634
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300635 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300636 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300637
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300638 if (tp_features.hotkey) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300639 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
640 A30, R30, R31, T20-22, X20-21, X22-24 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300641 tp_features.hotkey_mask =
642 acpi_evalf(hkey_handle, NULL, "DHKN", "qv");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300643
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300644 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300645 str_supported(tp_features.hotkey_mask));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300646
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300647 res = hotkey_get(&hotkey_orig_status, &hotkey_orig_mask);
648 if (res)
649 return res;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300650 }
651
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300652 return (tp_features.hotkey)? 0 : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300653}
654
655static void hotkey_exit(void)
656{
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300657 int res;
658
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300659 if (tp_features.hotkey) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300660 dbg_printk(TPACPI_DBG_EXIT, "restoring original hotkey mask\n");
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300661 res = hotkey_set(hotkey_orig_status, hotkey_orig_mask);
662 if (res)
663 printk(IBM_ERR "failed to restore hotkey to BIOS defaults\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300664 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300665}
666
667static void hotkey_notify(struct ibm_struct *ibm, u32 event)
668{
669 int hkey;
670
671 if (acpi_evalf(hkey_handle, &hkey, "MHKP", "d"))
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300672 acpi_bus_generate_event(ibm->acpi->device, event, hkey);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300673 else {
674 printk(IBM_ERR "unknown hotkey event %d\n", event);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300675 acpi_bus_generate_event(ibm->acpi->device, event, 0);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300676 }
677}
678
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400679static int hotkey_get(int *status, int *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
681 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300682 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400683
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300684 if (tp_features.hotkey_mask)
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400685 if (!acpi_evalf(hkey_handle, mask, "DHKN", "d"))
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300686 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400687
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300688 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689}
690
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400691static int hotkey_set(int status, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692{
693 int i;
694
695 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300696 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300698 if (tp_features.hotkey_mask)
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400699 for (i = 0; i < 32; i++) {
700 int bit = ((1 << i) & mask) != 0;
701 if (!acpi_evalf(hkey_handle,
702 NULL, "MHKM", "vdd", i + 1, bit))
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300703 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400704 }
705
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300706 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400707}
708
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400709static int hotkey_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300711 int res, status, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 int len = 0;
713
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300714 if (!tp_features.hotkey) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400715 len += sprintf(p + len, "status:\t\tnot supported\n");
716 return len;
717 }
718
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300719 res = hotkey_get(&status, &mask);
720 if (res)
721 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
723 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300724 if (tp_features.hotkey_mask) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 len += sprintf(p + len, "mask:\t\t0x%04x\n", mask);
726 len += sprintf(p + len,
727 "commands:\tenable, disable, reset, <mask>\n");
728 } else {
729 len += sprintf(p + len, "mask:\t\tnot supported\n");
730 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
731 }
732
733 return len;
734}
735
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400736static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737{
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300738 int res, status, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 char *cmd;
740 int do_cmd = 0;
741
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300742 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 return -ENODEV;
744
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300745 res = hotkey_get(&status, &mask);
746 if (res)
747 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400748
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 while ((cmd = next_cmd(&buf))) {
750 if (strlencmp(cmd, "enable") == 0) {
751 status = 1;
752 } else if (strlencmp(cmd, "disable") == 0) {
753 status = 0;
754 } else if (strlencmp(cmd, "reset") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400755 status = hotkey_orig_status;
756 mask = hotkey_orig_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
758 /* mask set */
759 } else if (sscanf(cmd, "%x", &mask) == 1) {
760 /* mask set */
761 } else
762 return -EINVAL;
763 do_cmd = 1;
764 }
765
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300766 if (do_cmd) {
767 res = hotkey_set(status, mask);
768 if (res)
769 return res;
770 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
772 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400773}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300775static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300776 .hid = IBM_HKEY_HID,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300777 .notify = hotkey_notify,
778 .handle = &hkey_handle,
779 .type = ACPI_DEVICE_NOTIFY,
780};
781
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300782static struct ibm_struct hotkey_driver_data = {
783 .name = "hotkey",
784 .read = hotkey_read,
785 .write = hotkey_write,
786 .exit = hotkey_exit,
787 .acpi = &ibm_hotkey_acpidriver,
788};
789
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300790/*************************************************************************
791 * Bluetooth subdriver
792 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300794static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795{
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -0300796 int status = 0;
797
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300798 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
799
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300800 IBM_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300801
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400802 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
803 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300804 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -0300805 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -0300807 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
808 str_supported(tp_features.bluetooth),
809 status);
810
811 if (tp_features.bluetooth &&
812 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
813 /* no bluetooth hardware present in system */
814 tp_features.bluetooth = 0;
815 dbg_printk(TPACPI_DBG_INIT,
816 "bluetooth hardware not installed\n");
817 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300818
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300819 return (tp_features.bluetooth)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820}
821
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -0300822static int bluetooth_get_radiosw(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823{
824 int status;
825
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -0300826 if (!tp_features.bluetooth)
827 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -0300829 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
830 return -EIO;
831
832 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
833}
834
835static int bluetooth_set_radiosw(int radio_on)
836{
837 int status;
838
839 if (!tp_features.bluetooth)
840 return -ENODEV;
841
842 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
843 return -EIO;
844 if (radio_on)
845 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
846 else
847 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
848 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
849 return -EIO;
850
851 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852}
853
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400854static int bluetooth_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855{
856 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -0300857 int status = bluetooth_get_radiosw();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300859 if (!tp_features.bluetooth)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 len += sprintf(p + len, "status:\t\tnot supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -0300862 len += sprintf(p + len, "status:\t\t%s\n",
863 (status)? "enabled" : "disabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 len += sprintf(p + len, "commands:\tenable, disable\n");
865 }
866
867 return len;
868}
869
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400870static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300874 if (!tp_features.bluetooth)
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400875 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
877 while ((cmd = next_cmd(&buf))) {
878 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -0300879 bluetooth_set_radiosw(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -0300881 bluetooth_set_radiosw(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 } else
883 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 }
885
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 return 0;
887}
888
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300889static struct ibm_struct bluetooth_driver_data = {
890 .name = "bluetooth",
891 .read = bluetooth_read,
892 .write = bluetooth_write,
893};
894
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300895/*************************************************************************
896 * Wan subdriver
897 */
898
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300899static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -0400900{
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -0300901 int status = 0;
902
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300903 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
904
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300905 IBM_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300906
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300907 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -0300908 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -0400909
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -0300910 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
911 str_supported(tp_features.wan),
912 status);
913
914 if (tp_features.wan &&
915 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
916 /* no wan hardware present in system */
917 tp_features.wan = 0;
918 dbg_printk(TPACPI_DBG_INIT,
919 "wan hardware not installed\n");
920 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300921
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300922 return (tp_features.wan)? 0 : 1;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -0400923}
924
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -0300925static int wan_get_radiosw(void)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -0400926{
927 int status;
928
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -0300929 if (!tp_features.wan)
930 return -ENODEV;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -0400931
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -0300932 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
933 return -EIO;
934
935 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
936}
937
938static int wan_set_radiosw(int radio_on)
939{
940 int status;
941
942 if (!tp_features.wan)
943 return -ENODEV;
944
945 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
946 return -EIO;
947 if (radio_on)
948 status |= TP_ACPI_WANCARD_RADIOSSW;
949 else
950 status &= ~TP_ACPI_WANCARD_RADIOSSW;
951 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
952 return -EIO;
953
954 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -0400955}
956
957static int wan_read(char *p)
958{
959 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -0300960 int status = wan_get_radiosw();
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -0400961
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300962 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -0400963 len += sprintf(p + len, "status:\t\tnot supported\n");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -0400964 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -0300965 len += sprintf(p + len, "status:\t\t%s\n",
966 (status)? "enabled" : "disabled");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -0400967 len += sprintf(p + len, "commands:\tenable, disable\n");
968 }
969
970 return len;
971}
972
973static int wan_write(char *buf)
974{
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -0400975 char *cmd;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -0400976
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300977 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -0400978 return -ENODEV;
979
980 while ((cmd = next_cmd(&buf))) {
981 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -0300982 wan_set_radiosw(1);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -0400983 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -0300984 wan_set_radiosw(0);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -0400985 } else
986 return -EINVAL;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -0400987 }
988
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -0400989 return 0;
990}
991
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300992static struct ibm_struct wan_driver_data = {
993 .name = "wan",
994 .read = wan_read,
995 .write = wan_write,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -0300996 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300997};
998
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300999/*************************************************************************
1000 * Video subdriver
1001 */
1002
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02001003static enum video_access_mode video_supported;
1004static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001005
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001006IBM_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
1007 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
1008 "\\_SB.PCI0.VID0", /* 770e */
1009 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
1010 "\\_SB.PCI0.AGP.VID", /* all others */
1011 ); /* R30, R31 */
1012
1013IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
1014
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001015static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001017 int ivga;
1018
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001019 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
1020
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001021 IBM_ACPIHANDLE_INIT(vid);
1022 IBM_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001023
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001024 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
1025 /* G41, assume IVGA doesn't change */
1026 vid_handle = vid2_handle;
1027
1028 if (!vid_handle)
1029 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001030 video_supported = TPACPI_VIDEO_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001031 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
1032 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001033 video_supported = TPACPI_VIDEO_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001034 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
1035 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001036 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001037 else
1038 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001039 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001041 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
1042 str_supported(video_supported != TPACPI_VIDEO_NONE),
1043 video_supported);
1044
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001045 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046}
1047
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001048static void video_exit(void)
1049{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001050 dbg_printk(TPACPI_DBG_EXIT,
1051 "restoring original video autoswitch mode\n");
1052 if (video_autosw_set(video_orig_autosw))
1053 printk(IBM_ERR "error while trying to restore original "
1054 "video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001055}
1056
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001057static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058{
1059 int status = 0;
1060 int i;
1061
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001062 switch (video_supported) {
1063 case TPACPI_VIDEO_570:
1064 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
1065 TP_ACPI_VIDEO_570_PHSCMD))
1066 return -EIO;
1067 status = i & TP_ACPI_VIDEO_570_PHSMASK;
1068 break;
1069 case TPACPI_VIDEO_770:
1070 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
1071 return -EIO;
1072 if (i)
1073 status |= TP_ACPI_VIDEO_S_LCD;
1074 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
1075 return -EIO;
1076 if (i)
1077 status |= TP_ACPI_VIDEO_S_CRT;
1078 break;
1079 case TPACPI_VIDEO_NEW:
1080 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
1081 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
1082 return -EIO;
1083 if (i)
1084 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001086 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
1087 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
1088 return -EIO;
1089 if (i)
1090 status |= TP_ACPI_VIDEO_S_LCD;
1091 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
1092 return -EIO;
1093 if (i)
1094 status |= TP_ACPI_VIDEO_S_DVI;
1095 break;
1096 default:
1097 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001098 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
1100 return status;
1101}
1102
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001103static int video_outputsw_set(int status)
1104{
1105 int autosw;
1106 int res = 0;
1107
1108 switch (video_supported) {
1109 case TPACPI_VIDEO_570:
1110 res = acpi_evalf(NULL, NULL,
1111 "\\_SB.PHS2", "vdd",
1112 TP_ACPI_VIDEO_570_PHS2CMD,
1113 status | TP_ACPI_VIDEO_570_PHS2SET);
1114 break;
1115 case TPACPI_VIDEO_770:
1116 autosw = video_autosw_get();
1117 if (autosw < 0)
1118 return autosw;
1119
1120 res = video_autosw_set(1);
1121 if (res)
1122 return res;
1123 res = acpi_evalf(vid_handle, NULL,
1124 "ASWT", "vdd", status * 0x100, 0);
1125 if (!autosw && video_autosw_set(autosw)) {
1126 printk(IBM_ERR "video auto-switch left enabled due to error\n");
1127 return -EIO;
1128 }
1129 break;
1130 case TPACPI_VIDEO_NEW:
1131 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
1132 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
1133 break;
1134 default:
1135 return -ENOSYS;
1136 }
1137
1138 return (res)? 0 : -EIO;
1139}
1140
1141static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001143 int autosw = 0;
1144
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001145 switch (video_supported) {
1146 case TPACPI_VIDEO_570:
1147 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
1148 return -EIO;
1149 break;
1150 case TPACPI_VIDEO_770:
1151 case TPACPI_VIDEO_NEW:
1152 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
1153 return -EIO;
1154 break;
1155 default:
1156 return -ENOSYS;
1157 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001158
1159 return autosw & 1;
1160}
1161
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001162static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001163{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001164 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001165 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001166 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001167}
1168
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001169static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001170{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001171 int autosw = video_autosw_get();
1172 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001173
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001174 if (autosw < 0)
1175 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001176
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001177 switch (video_supported) {
1178 case TPACPI_VIDEO_570:
1179 res = video_autosw_set(1);
1180 if (res)
1181 return res;
1182 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
1183 break;
1184 case TPACPI_VIDEO_770:
1185 case TPACPI_VIDEO_NEW:
1186 res = video_autosw_set(1);
1187 if (res)
1188 return res;
1189 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
1190 break;
1191 default:
1192 return -ENOSYS;
1193 }
1194 if (!autosw && video_autosw_set(autosw)) {
1195 printk(IBM_ERR "video auto-switch left enabled due to error\n");
1196 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001197 }
1198
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001199 return (res)? 0 : -EIO;
1200}
1201
1202static int video_expand_toggle(void)
1203{
1204 switch (video_supported) {
1205 case TPACPI_VIDEO_570:
1206 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
1207 0 : -EIO;
1208 case TPACPI_VIDEO_770:
1209 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
1210 0 : -EIO;
1211 case TPACPI_VIDEO_NEW:
1212 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
1213 0 : -EIO;
1214 default:
1215 return -ENOSYS;
1216 }
1217 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001218}
1219
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001220static int video_read(char *p)
1221{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001222 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001223 int len = 0;
1224
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001225 if (video_supported == TPACPI_VIDEO_NONE) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001226 len += sprintf(p + len, "status:\t\tnot supported\n");
1227 return len;
1228 }
1229
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001230 status = video_outputsw_get();
1231 if (status < 0)
1232 return status;
1233
1234 autosw = video_autosw_get();
1235 if (autosw < 0)
1236 return autosw;
1237
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001238 len += sprintf(p + len, "status:\t\tsupported\n");
1239 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
1240 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001241 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001242 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
1243 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
1244 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
1245 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001246 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001247 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
1248 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
1249 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
1250
1251 return len;
1252}
1253
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001254static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255{
1256 char *cmd;
1257 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001258 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001260 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001261 return -ENODEV;
1262
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001263 enable = 0;
1264 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
1266 while ((cmd = next_cmd(&buf))) {
1267 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001268 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001270 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001272 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001274 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001275 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001276 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001277 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001278 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001279 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001280 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001282 res = video_autosw_set(1);
1283 if (res)
1284 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001286 res = video_autosw_set(0);
1287 if (res)
1288 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001290 res = video_outputsw_cycle();
1291 if (res)
1292 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001294 res = video_expand_toggle();
1295 if (res)
1296 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 } else
1298 return -EINVAL;
1299 }
1300
1301 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001302 status = video_outputsw_get();
1303 if (status < 0)
1304 return status;
1305 res = video_outputsw_set((status & ~disable) | enable);
1306 if (res)
1307 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 }
1309
1310 return 0;
1311}
1312
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001313static struct ibm_struct video_driver_data = {
1314 .name = "video",
1315 .read = video_read,
1316 .write = video_write,
1317 .exit = video_exit,
1318};
1319
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001320/*************************************************************************
1321 * Light (thinklight) subdriver
1322 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001324IBM_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
1325IBM_HANDLE(ledb, ec, "LEDB"); /* G4x */
1326
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001327static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001329 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
1330
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001331 IBM_ACPIHANDLE_INIT(ledb);
1332 IBM_ACPIHANDLE_INIT(lght);
1333 IBM_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001334
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001335 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001336 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001337
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001338 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001339 /* light status not supported on
1340 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001341 tp_features.light_status =
1342 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001344 vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001345 str_supported(tp_features.light));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001346
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001347 return (tp_features.light)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348}
1349
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001350static int light_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351{
1352 int len = 0;
1353 int status = 0;
1354
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001355 if (!tp_features.light) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001356 len += sprintf(p + len, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001357 } else if (!tp_features.light_status) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001358 len += sprintf(p + len, "status:\t\tunknown\n");
1359 len += sprintf(p + len, "commands:\ton, off\n");
1360 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
1362 return -EIO;
1363 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001364 len += sprintf(p + len, "commands:\ton, off\n");
1365 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
1367 return len;
1368}
1369
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001370static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371{
1372 int cmos_cmd, lght_cmd;
1373 char *cmd;
1374 int success;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001375
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001376 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001377 return -ENODEV;
1378
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 while ((cmd = next_cmd(&buf))) {
1380 if (strlencmp(cmd, "on") == 0) {
1381 cmos_cmd = 0x0c;
1382 lght_cmd = 1;
1383 } else if (strlencmp(cmd, "off") == 0) {
1384 cmos_cmd = 0x0d;
1385 lght_cmd = 0;
1386 } else
1387 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001388
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 success = cmos_handle ?
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001390 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
1391 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 if (!success)
1393 return -EIO;
1394 }
1395
1396 return 0;
1397}
1398
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001399static struct ibm_struct light_driver_data = {
1400 .name = "light",
1401 .read = light_read,
1402 .write = light_write,
1403};
1404
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001405/*************************************************************************
1406 * Dock subdriver
1407 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03001409#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001410
1411IBM_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
1412 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
1413 "\\_SB.PCI0.PCI1.DOCK", /* all others */
1414 "\\_SB.PCI.ISA.SLCE", /* 570 */
1415 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
1416
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001417/* don't list other alternatives as we install a notify handler on the 570 */
1418IBM_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
1419
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420#define dock_docked() (_sta(dock_handle) & 1)
1421
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001422static int __init dock_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001423{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001424 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
1425
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001426 IBM_ACPIHANDLE_INIT(dock);
1427 IBM_ACPIHANDLE_INIT(pci);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001428
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001429 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
1430 str_supported(dock_handle != NULL));
1431
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001432 return (dock_handle)? 0 : 1;
1433}
1434
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001435static void dock_notify(struct ibm_struct *ibm, u32 event)
1436{
1437 int docked = dock_docked();
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001438 int pci = ibm->acpi->hid && strstr(ibm->acpi->hid, IBM_PCI_HID);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001439
1440 if (event == 1 && !pci) /* 570 */
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001441 acpi_bus_generate_event(ibm->acpi->device, event, 1); /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001442 else if (event == 1 && pci) /* 570 */
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001443 acpi_bus_generate_event(ibm->acpi->device, event, 3); /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001444 else if (event == 3 && docked)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001445 acpi_bus_generate_event(ibm->acpi->device, event, 1); /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001446 else if (event == 3 && !docked)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001447 acpi_bus_generate_event(ibm->acpi->device, event, 2); /* undock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001448 else if (event == 0 && docked)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001449 acpi_bus_generate_event(ibm->acpi->device, event, 3); /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001450 else {
1451 printk(IBM_ERR "unknown dock event %d, status %d\n",
1452 event, _sta(dock_handle));
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001453 acpi_bus_generate_event(ibm->acpi->device, event, 0); /* unknown */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001454 }
1455}
1456
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001457static int dock_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458{
1459 int len = 0;
1460 int docked = dock_docked();
1461
1462 if (!dock_handle)
1463 len += sprintf(p + len, "status:\t\tnot supported\n");
1464 else if (!docked)
1465 len += sprintf(p + len, "status:\t\tundocked\n");
1466 else {
1467 len += sprintf(p + len, "status:\t\tdocked\n");
1468 len += sprintf(p + len, "commands:\tdock, undock\n");
1469 }
1470
1471 return len;
1472}
1473
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001474static int dock_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475{
1476 char *cmd;
1477
1478 if (!dock_docked())
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001479 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
1481 while ((cmd = next_cmd(&buf))) {
1482 if (strlencmp(cmd, "undock") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001483 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
1484 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 return -EIO;
1486 } else if (strlencmp(cmd, "dock") == 0) {
1487 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
1488 return -EIO;
1489 } else
1490 return -EINVAL;
1491 }
1492
1493 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001494}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001496static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001497 {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001498 .notify = dock_notify,
1499 .handle = &dock_handle,
1500 .type = ACPI_SYSTEM_NOTIFY,
1501 },
1502 {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001503 .hid = IBM_PCI_HID,
1504 .notify = dock_notify,
1505 .handle = &pci_handle,
1506 .type = ACPI_SYSTEM_NOTIFY,
1507 },
1508};
1509
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001510static struct ibm_struct dock_driver_data[2] = {
1511 {
1512 .name = "dock",
1513 .read = dock_read,
1514 .write = dock_write,
1515 .acpi = &ibm_dock_acpidriver[0],
1516 },
1517 {
1518 .name = "dock",
1519 .acpi = &ibm_dock_acpidriver[1],
1520 },
1521};
1522
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03001523#endif /* CONFIG_THINKPAD_ACPI_DOCK */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001525/*************************************************************************
1526 * Bay subdriver
1527 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03001529#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001530IBM_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
1531 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
1532 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
1533 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
1534 ); /* A21e, R30, R31 */
1535IBM_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
1536 "_EJ0", /* all others */
1537 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
1538IBM_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
1539 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
1540 ); /* all others */
1541IBM_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
1542 "_EJ0", /* 770x */
1543 ); /* all others */
1544
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001545static int __init bay_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001547 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
1548
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001549 IBM_ACPIHANDLE_INIT(bay);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001550 if (bay_handle)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001551 IBM_ACPIHANDLE_INIT(bay_ej);
1552 IBM_ACPIHANDLE_INIT(bay2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001553 if (bay2_handle)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001554 IBM_ACPIHANDLE_INIT(bay2_ej);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001555
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001556 tp_features.bay_status = bay_handle &&
1557 acpi_evalf(bay_handle, NULL, "_STA", "qv");
1558 tp_features.bay_status2 = bay2_handle &&
1559 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001560
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001561 tp_features.bay_eject = bay_handle && bay_ej_handle &&
1562 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
1563 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
1564 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001566 vdbg_printk(TPACPI_DBG_INIT,
1567 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001568 str_supported(tp_features.bay_status),
1569 str_supported(tp_features.bay_eject),
1570 str_supported(tp_features.bay_status2),
1571 str_supported(tp_features.bay_eject2));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001572
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001573 return (tp_features.bay_status || tp_features.bay_eject ||
1574 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575}
1576
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001577static void bay_notify(struct ibm_struct *ibm, u32 event)
1578{
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001579 acpi_bus_generate_event(ibm->acpi->device, event, 0);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001580}
1581
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001582#define bay_occupied(b) (_sta(b##_handle) & 1)
1583
1584static int bay_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585{
1586 int len = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001587 int occupied = bay_occupied(bay);
1588 int occupied2 = bay_occupied(bay2);
1589 int eject, eject2;
1590
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001591 len += sprintf(p + len, "status:\t\t%s\n",
1592 tp_features.bay_status ?
1593 (occupied ? "occupied" : "unoccupied") :
1594 "not supported");
1595 if (tp_features.bay_status2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001596 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
1597 "occupied" : "unoccupied");
1598
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001599 eject = tp_features.bay_eject && occupied;
1600 eject2 = tp_features.bay_eject2 && occupied2;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001601
1602 if (eject && eject2)
1603 len += sprintf(p + len, "commands:\teject, eject2\n");
1604 else if (eject)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 len += sprintf(p + len, "commands:\teject\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001606 else if (eject2)
1607 len += sprintf(p + len, "commands:\teject2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
1609 return len;
1610}
1611
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001612static int bay_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613{
1614 char *cmd;
1615
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001616 if (!tp_features.bay_eject && !tp_features.bay_eject2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001617 return -ENODEV;
1618
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001620 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001621 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
1622 return -EIO;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001623 } else if (tp_features.bay_eject2 &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001624 strlencmp(cmd, "eject2") == 0) {
1625 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 return -EIO;
1627 } else
1628 return -EINVAL;
1629 }
1630
1631 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001632}
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001633
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001634static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
1635 .notify = bay_notify,
1636 .handle = &bay_handle,
1637 .type = ACPI_SYSTEM_NOTIFY,
1638};
1639
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001640static struct ibm_struct bay_driver_data = {
1641 .name = "bay",
1642 .read = bay_read,
1643 .write = bay_write,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001644 .acpi = &ibm_bay_acpidriver,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001645};
1646
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03001647#endif /* CONFIG_THINKPAD_ACPI_BAY */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001649/*************************************************************************
1650 * CMOS subdriver
1651 */
1652
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001653static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001654{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001655 vdbg_printk(TPACPI_DBG_INIT,
1656 "initializing cmos commands subdriver\n");
1657
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001658 IBM_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001659
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001660 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
1661 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001662 return (cmos_handle)? 0 : 1;
1663}
1664
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001665static int cmos_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666{
1667 int len = 0;
1668
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001669 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1670 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 if (!cmos_handle)
1672 len += sprintf(p + len, "status:\t\tnot supported\n");
1673 else {
1674 len += sprintf(p + len, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001675 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 }
1677
1678 return len;
1679}
1680
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001681static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682{
1683 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03001684 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685
1686 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001687 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
1688 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 /* cmos_cmd set */
1690 } else
1691 return -EINVAL;
1692
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03001693 res = issue_thinkpad_cmos_command(cmos_cmd);
1694 if (res)
1695 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 }
1697
1698 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001699}
1700
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001701static struct ibm_struct cmos_driver_data = {
1702 .name = "cmos",
1703 .read = cmos_read,
1704 .write = cmos_write,
1705};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001706
1707/*************************************************************************
1708 * LED subdriver
1709 */
1710
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02001711static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001712
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001713IBM_HANDLE(led, ec, "SLED", /* 570 */
1714 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
1715 "LED", /* all others */
1716 ); /* R30, R31 */
1717
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001718static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001719{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001720 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
1721
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001722 IBM_ACPIHANDLE_INIT(led);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001723
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001724 if (!led_handle)
1725 /* led not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001726 led_supported = TPACPI_LED_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001727 else if (strlencmp(led_path, "SLED") == 0)
1728 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001729 led_supported = TPACPI_LED_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001730 else if (strlencmp(led_path, "SYSL") == 0)
1731 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001732 led_supported = TPACPI_LED_OLD;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001733 else
1734 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001735 led_supported = TPACPI_LED_NEW;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001736
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001737 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
1738 str_supported(led_supported), led_supported);
1739
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001740 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001741}
1742
1743#define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
1744
1745static int led_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746{
1747 int len = 0;
1748
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001749 if (!led_supported) {
1750 len += sprintf(p + len, "status:\t\tnot supported\n");
1751 return len;
1752 }
1753 len += sprintf(p + len, "status:\t\tsupported\n");
1754
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001755 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001756 /* 570 */
1757 int i, status;
1758 for (i = 0; i < 8; i++) {
1759 if (!acpi_evalf(ec_handle,
1760 &status, "GLED", "dd", 1 << i))
1761 return -EIO;
1762 len += sprintf(p + len, "%d:\t\t%s\n",
1763 i, led_status(status));
1764 }
1765 }
1766
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 len += sprintf(p + len, "commands:\t"
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001768 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
1770 return len;
1771}
1772
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001773/* off, on, blink */
1774static const int led_sled_arg1[] = { 0, 1, 3 };
1775static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
1776static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
1777static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
1778
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001779static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780{
1781 char *cmd;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001782 int led, ind, ret;
1783
1784 if (!led_supported)
1785 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
1787 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001788 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 return -EINVAL;
1790
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001791 if (strstr(cmd, "off")) {
1792 ind = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 } else if (strstr(cmd, "on")) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001794 ind = 1;
1795 } else if (strstr(cmd, "blink")) {
1796 ind = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 } else
1798 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001799
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001800 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001801 /* 570 */
1802 led = 1 << led;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001804 led, led_sled_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 return -EIO;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001806 } else if (led_supported == TPACPI_LED_OLD) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001807 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
1808 led = 1 << led;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001809 ret = ec_write(TPACPI_LED_EC_HLMS, led);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001810 if (ret >= 0)
1811 ret =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001812 ec_write(TPACPI_LED_EC_HLBL,
Henrique de Moraes Holschuhe062e032007-03-23 17:33:55 -03001813 led * led_exp_hlbl[ind]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001814 if (ret >= 0)
1815 ret =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001816 ec_write(TPACPI_LED_EC_HLCL,
Henrique de Moraes Holschuhe062e032007-03-23 17:33:55 -03001817 led * led_exp_hlcl[ind]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001818 if (ret < 0)
1819 return ret;
1820 } else {
1821 /* all others */
1822 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
1823 led, led_led_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001825 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 }
1827
1828 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001829}
1830
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001831static struct ibm_struct led_driver_data = {
1832 .name = "led",
1833 .read = led_read,
1834 .write = led_write,
1835};
1836
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001837/*************************************************************************
1838 * Beep subdriver
1839 */
1840
1841IBM_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
1842
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001843static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001844{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001845 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
1846
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001847 IBM_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001848
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001849 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
1850 str_supported(beep_handle != NULL));
1851
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001852 return (beep_handle)? 0 : 1;
1853}
1854
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001855static int beep_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856{
1857 int len = 0;
1858
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001859 if (!beep_handle)
1860 len += sprintf(p + len, "status:\t\tnot supported\n");
1861 else {
1862 len += sprintf(p + len, "status:\t\tsupported\n");
1863 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
1864 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865
1866 return len;
1867}
1868
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001869static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870{
1871 char *cmd;
1872 int beep_cmd;
1873
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001874 if (!beep_handle)
1875 return -ENODEV;
1876
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001878 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
1879 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 /* beep_cmd set */
1881 } else
1882 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001883 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 return -EIO;
1885 }
1886
1887 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001888}
1889
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001890static struct ibm_struct beep_driver_data = {
1891 .name = "beep",
1892 .read = beep_read,
1893 .write = beep_write,
1894};
1895
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001896/*************************************************************************
1897 * Thermal subdriver
1898 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001899
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001900static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001901
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001902static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001903{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001904 u8 t, ta1, ta2;
1905 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001906 int acpi_tmp7;
1907
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001908 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
1909
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001910 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001911
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001912 if (ibm_thinkpad_ec_found && experimental) {
1913 /*
1914 * Direct EC access mode: sensors at registers
1915 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
1916 * non-implemented, thermal sensors return 0x80 when
1917 * not available
1918 */
1919
1920 ta1 = ta2 = 0;
1921 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03001922 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001923 ta1 |= t;
1924 } else {
1925 ta1 = 0;
1926 break;
1927 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03001928 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001929 ta2 |= t;
1930 } else {
1931 ta1 = 0;
1932 break;
1933 }
1934 }
1935 if (ta1 == 0) {
1936 /* This is sheer paranoia, but we handle it anyway */
1937 if (acpi_tmp7) {
1938 printk(IBM_ERR
1939 "ThinkPad ACPI EC access misbehaving, "
1940 "falling back to ACPI TMPx access mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001941 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001942 } else {
1943 printk(IBM_ERR
1944 "ThinkPad ACPI EC access misbehaving, "
1945 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001946 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001947 }
1948 } else {
1949 thermal_read_mode =
1950 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001951 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001952 }
1953 } else if (acpi_tmp7) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001954 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
1955 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001956 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001957 } else {
1958 /* Standard ACPI TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001959 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001960 }
1961 } else {
1962 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001963 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001964 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001965
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001966 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
1967 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
1968 thermal_read_mode);
1969
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001970 return (thermal_read_mode != TPACPI_THERMAL_NONE)? 0 : 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001971}
1972
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03001973/* idx is zero-based */
1974static int thermal_get_sensor(int idx, s32 *value)
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001975{
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03001976 int t;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001977 s8 tmp;
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03001978 char tmpi[5];
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001979
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03001980 t = TP_EC_THERMAL_TMP0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001981
1982 switch (thermal_read_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001983#if TPACPI_MAX_THERMAL_SENSORS >= 16
1984 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03001985 if (idx >= 8 && idx <= 15) {
1986 t = TP_EC_THERMAL_TMP8;
1987 idx -= 8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001988 }
1989 /* fallthrough */
1990#endif
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001991 case TPACPI_THERMAL_TPEC_8:
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03001992 if (idx <= 7) {
1993 if (!acpi_ec_read(t + idx, &tmp))
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001994 return -EIO;
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03001995 *value = tmp * 1000;
1996 return 0;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001997 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03001998 break;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001999
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002000 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002001 if (idx <= 7) {
2002 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
2003 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
2004 return -EIO;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002005 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
2006 return -EIO;
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002007 *value = (t - 2732) * 100;
2008 return 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002009 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002010 break;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002011
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002012 case TPACPI_THERMAL_ACPI_TMP07:
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002013 if (idx <= 7) {
2014 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002015 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
2016 return -EIO;
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002017 *value = t * 1000;
2018 return 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002019 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002020 break;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002021
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002022 case TPACPI_THERMAL_NONE:
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002023 default:
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002024 return -ENOSYS;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002025 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002026
2027 return -EINVAL;
2028}
2029
2030static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
2031{
2032 int res, i;
2033 int n;
2034
2035 n = 8;
2036 i = 0;
2037
2038 if (!s)
2039 return -EINVAL;
2040
2041 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
2042 n = 16;
2043
2044 for(i = 0 ; i < n; i++) {
2045 res = thermal_get_sensor(i, &s->temp[i]);
2046 if (res)
2047 return res;
2048 }
2049
2050 return n;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002051}
2052
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002053static int thermal_read(char *p)
2054{
2055 int len = 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002056 int n, i;
2057 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002058
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002059 n = thermal_get_sensors(&t);
2060 if (unlikely(n < 0))
2061 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002062
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002063 len += sprintf(p + len, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002064
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002065 if (n > 0) {
2066 for (i = 0; i < (n - 1); i++)
2067 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
2068 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
2069 } else
2070 len += sprintf(p + len, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002071
2072 return len;
2073}
2074
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002075static struct ibm_struct thermal_driver_data = {
2076 .name = "thermal",
2077 .read = thermal_read,
2078};
2079
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002080/*************************************************************************
2081 * EC Dump subdriver
2082 */
2083
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002084static u8 ecdump_regs[256];
2085
2086static int ecdump_read(char *p)
2087{
2088 int len = 0;
2089 int i, j;
2090 u8 v;
2091
2092 len += sprintf(p + len, "EC "
2093 " +00 +01 +02 +03 +04 +05 +06 +07"
2094 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
2095 for (i = 0; i < 256; i += 16) {
2096 len += sprintf(p + len, "EC 0x%02x:", i);
2097 for (j = 0; j < 16; j++) {
2098 if (!acpi_ec_read(i + j, &v))
2099 break;
2100 if (v != ecdump_regs[i + j])
2101 len += sprintf(p + len, " *%02x", v);
2102 else
2103 len += sprintf(p + len, " %02x", v);
2104 ecdump_regs[i + j] = v;
2105 }
2106 len += sprintf(p + len, "\n");
2107 if (j != 16)
2108 break;
2109 }
2110
2111 /* These are way too dangerous to advertise openly... */
2112#if 0
2113 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
2114 " (<offset> is 00-ff, <value> is 00-ff)\n");
2115 len += sprintf(p + len, "commands:\t0x<offset> <value> "
2116 " (<offset> is 00-ff, <value> is 0-255)\n");
2117#endif
2118 return len;
2119}
2120
2121static int ecdump_write(char *buf)
2122{
2123 char *cmd;
2124 int i, v;
2125
2126 while ((cmd = next_cmd(&buf))) {
2127 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
2128 /* i and v set */
2129 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
2130 /* i and v set */
2131 } else
2132 return -EINVAL;
2133 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
2134 if (!acpi_ec_write(i, v))
2135 return -EIO;
2136 } else
2137 return -EINVAL;
2138 }
2139
2140 return 0;
2141}
2142
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002143static struct ibm_struct ecdump_driver_data = {
2144 .name = "ecdump",
2145 .read = ecdump_read,
2146 .write = ecdump_write,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03002147 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002148};
2149
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002150/*************************************************************************
2151 * Backlight/brightness subdriver
2152 */
Holger Macht8acb0252006-10-20 14:30:28 -07002153
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002154static struct backlight_device *ibm_backlight_device = NULL;
Holger Macht8acb0252006-10-20 14:30:28 -07002155
Richard Purdie599a52d2007-02-10 23:07:48 +00002156static struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02002157 .get_brightness = brightness_get,
2158 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02002159};
2160
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002161static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02002162{
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02002163 int b;
2164
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002165 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
2166
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02002167 b = brightness_get(NULL);
2168 if (b < 0)
2169 return b;
2170
Yu Luming519ab5f2006-12-19 12:56:15 -08002171 ibm_backlight_device = backlight_device_register("ibm", NULL, NULL,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02002172 &ibm_backlight_data);
2173 if (IS_ERR(ibm_backlight_device)) {
2174 printk(IBM_ERR "Could not register backlight device\n");
2175 return PTR_ERR(ibm_backlight_device);
2176 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002177 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02002178
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02002179 ibm_backlight_device->props.max_brightness = 7;
2180 ibm_backlight_device->props.brightness = b;
2181 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00002182
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02002183 return 0;
2184}
2185
2186static void brightness_exit(void)
2187{
2188 if (ibm_backlight_device) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002189 vdbg_printk(TPACPI_DBG_EXIT,
2190 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02002191 backlight_device_unregister(ibm_backlight_device);
2192 ibm_backlight_device = NULL;
2193 }
2194}
2195
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002196static int brightness_update_status(struct backlight_device *bd)
2197{
2198 return brightness_set(
2199 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
2200 bd->props.power == FB_BLANK_UNBLANK) ?
2201 bd->props.brightness : 0);
2202}
2203
2204static int brightness_get(struct backlight_device *bd)
2205{
2206 u8 level;
2207 if (!acpi_ec_read(brightness_offset, &level))
2208 return -EIO;
2209
2210 level &= 0x7;
2211
2212 return level;
2213}
2214
2215static int brightness_set(int value)
2216{
2217 int cmos_cmd, inc, i;
2218 int current_value = brightness_get(NULL);
2219
2220 value &= 7;
2221
2222 cmos_cmd = value > current_value ? TP_CMOS_BRIGHTNESS_UP : TP_CMOS_BRIGHTNESS_DOWN;
2223 inc = value > current_value ? 1 : -1;
2224 for (i = current_value; i != value; i += inc) {
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03002225 if (issue_thinkpad_cmos_command(cmos_cmd))
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002226 return -EIO;
2227 if (!acpi_ec_write(brightness_offset, i + inc))
2228 return -EIO;
2229 }
2230
2231 return 0;
2232}
2233
2234static int brightness_read(char *p)
2235{
2236 int len = 0;
2237 int level;
2238
2239 if ((level = brightness_get(NULL)) < 0) {
2240 len += sprintf(p + len, "level:\t\tunreadable\n");
2241 } else {
2242 len += sprintf(p + len, "level:\t\t%d\n", level & 0x7);
2243 len += sprintf(p + len, "commands:\tup, down\n");
2244 len += sprintf(p + len, "commands:\tlevel <level>"
2245 " (<level> is 0-7)\n");
2246 }
2247
2248 return len;
2249}
2250
2251static int brightness_write(char *buf)
2252{
2253 int level;
2254 int new_level;
2255 char *cmd;
2256
2257 while ((cmd = next_cmd(&buf))) {
2258 if ((level = brightness_get(NULL)) < 0)
2259 return level;
2260 level &= 7;
2261
2262 if (strlencmp(cmd, "up") == 0) {
2263 new_level = level == 7 ? 7 : level + 1;
2264 } else if (strlencmp(cmd, "down") == 0) {
2265 new_level = level == 0 ? 0 : level - 1;
2266 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
2267 new_level >= 0 && new_level <= 7) {
2268 /* new_level set */
2269 } else
2270 return -EINVAL;
2271
2272 brightness_set(new_level);
2273 }
2274
2275 return 0;
2276}
2277
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002278static struct ibm_struct brightness_driver_data = {
2279 .name = "brightness",
2280 .read = brightness_read,
2281 .write = brightness_write,
2282 .exit = brightness_exit,
2283};
2284
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002285/*************************************************************************
2286 * Volume subdriver
2287 */
2288
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002289static int volume_read(char *p)
2290{
2291 int len = 0;
2292 u8 level;
2293
2294 if (!acpi_ec_read(volume_offset, &level)) {
2295 len += sprintf(p + len, "level:\t\tunreadable\n");
2296 } else {
2297 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
2298 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
2299 len += sprintf(p + len, "commands:\tup, down, mute\n");
2300 len += sprintf(p + len, "commands:\tlevel <level>"
2301 " (<level> is 0-15)\n");
2302 }
2303
2304 return len;
2305}
2306
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002307static int volume_write(char *buf)
2308{
2309 int cmos_cmd, inc, i;
2310 u8 level, mute;
2311 int new_level, new_mute;
2312 char *cmd;
2313
2314 while ((cmd = next_cmd(&buf))) {
2315 if (!acpi_ec_read(volume_offset, &level))
2316 return -EIO;
2317 new_mute = mute = level & 0x40;
2318 new_level = level = level & 0xf;
2319
2320 if (strlencmp(cmd, "up") == 0) {
2321 if (mute)
2322 new_mute = 0;
2323 else
2324 new_level = level == 15 ? 15 : level + 1;
2325 } else if (strlencmp(cmd, "down") == 0) {
2326 if (mute)
2327 new_mute = 0;
2328 else
2329 new_level = level == 0 ? 0 : level - 1;
2330 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
2331 new_level >= 0 && new_level <= 15) {
2332 /* new_level set */
2333 } else if (strlencmp(cmd, "mute") == 0) {
2334 new_mute = 0x40;
2335 } else
2336 return -EINVAL;
2337
2338 if (new_level != level) { /* mute doesn't change */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002339 cmos_cmd = new_level > level ? TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002340 inc = new_level > level ? 1 : -1;
2341
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03002342 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002343 !acpi_ec_write(volume_offset, level)))
2344 return -EIO;
2345
2346 for (i = level; i != new_level; i += inc)
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03002347 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002348 !acpi_ec_write(volume_offset, i + inc))
2349 return -EIO;
2350
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03002351 if (mute && (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002352 !acpi_ec_write(volume_offset,
2353 new_level + mute)))
2354 return -EIO;
2355 }
2356
2357 if (new_mute != mute) { /* level doesn't change */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002358 cmos_cmd = new_mute ? TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002359
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03002360 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002361 !acpi_ec_write(volume_offset, level + new_mute))
2362 return -EIO;
2363 }
2364 }
2365
2366 return 0;
2367}
2368
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002369static struct ibm_struct volume_driver_data = {
2370 .name = "volume",
2371 .read = volume_read,
2372 .write = volume_write,
2373};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002374
2375/*************************************************************************
2376 * Fan subdriver
2377 */
2378
2379/*
2380 * FAN ACCESS MODES
2381 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002382 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002383 * ACPI GFAN method: returns fan level
2384 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002385 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03002386 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002387 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002388 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002389 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
2390 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03002391 * EC 0x2f (HFSP) might be available *for reading*, but do not use
2392 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002393 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002394 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03002395 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
2396 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002397 *
2398 * Fan speed changes of any sort (including those caused by the
2399 * disengaged mode) are usually done slowly by the firmware as the
2400 * maximum ammount of fan duty cycle change per second seems to be
2401 * limited.
2402 *
2403 * Reading is not available if GFAN exists.
2404 * Writing is not available if SFAN exists.
2405 *
2406 * Bits
2407 * 7 automatic mode engaged;
2408 * (default operation mode of the ThinkPad)
2409 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03002410 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002411 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03002412 * the tachometer while the fan controller ramps up
2413 * the speed (which can take up to a few *minutes*).
2414 * Speeds up fan to 100% duty-cycle, which is far above
2415 * the standard RPM levels. It is not impossible that
2416 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002417 * 5-3 unused in some models. Extra bits for fan level
2418 * in others, but still useless as all values above
2419 * 7 map to the same speed as level 7 in these models.
2420 * 2-0 fan level (0..7 usually)
2421 * 0x00 = stop
2422 * 0x07 = max (set when temperatures critical)
2423 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002424 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002425 *
2426 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
2427 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
2428 * does so, its initial value is meaningless (0x07).
2429 *
2430 * For firmware bugs, refer to:
2431 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
2432 *
2433 * ----
2434 *
2435 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
2436 * Main fan tachometer reading (in RPM)
2437 *
2438 * This register is present on all ThinkPads with a new-style EC, and
2439 * it is known not to be present on the A21m/e, and T22, as there is
2440 * something else in offset 0x84 according to the ACPI DSDT. Other
2441 * ThinkPads from this same time period (and earlier) probably lack the
2442 * tachometer as well.
2443 *
2444 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
2445 * was never fixed by IBM to report the EC firmware version string
2446 * probably support the tachometer (like the early X models), so
2447 * detecting it is quite hard. We need more data to know for sure.
2448 *
2449 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
2450 * might result.
2451 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03002452 * FIRMWARE BUG: may go stale while the EC is switching to full speed
2453 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002454 *
2455 * For firmware bugs, refer to:
2456 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
2457 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002458 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002459 * ThinkPad X31, X40, X41. Not available in the X60.
2460 *
2461 * FANS ACPI handle: takes three arguments: low speed, medium speed,
2462 * high speed. ACPI DSDT seems to map these three speeds to levels
2463 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
2464 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
2465 *
2466 * The speeds are stored on handles
2467 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
2468 *
2469 * There are three default speed sets, acessible as handles:
2470 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
2471 *
2472 * ACPI DSDT switches which set is in use depending on various
2473 * factors.
2474 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002475 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002476 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
2477 * but the ACPI tables just mention level 7.
2478 */
2479
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002480static enum fan_status_access_mode fan_status_access_mode;
2481static enum fan_control_access_mode fan_control_access_mode;
2482static enum fan_control_commands fan_control_commands;
2483
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002484static u8 fan_control_initial_status;
2485
Len Brown25c68a32006-12-08 04:43:41 -05002486static void fan_watchdog_fire(struct work_struct *ignored);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002487static int fan_watchdog_maxinterval;
Len Brown25c68a32006-12-08 04:43:41 -05002488static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002489
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002490IBM_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
2491IBM_HANDLE(gfan, ec, "GFAN", /* 570 */
2492 "\\FSPD", /* 600e/x, 770e, 770x */
2493 ); /* all others */
2494IBM_HANDLE(sfan, ec, "SFAN", /* 570 */
2495 "JFNS", /* 770x-JL */
2496 ); /* all others */
2497
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002498static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002499{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002500 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
2501
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002502 fan_status_access_mode = TPACPI_FAN_NONE;
2503 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002504 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002505 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002506 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002507
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002508 IBM_ACPIHANDLE_INIT(fans);
2509 IBM_ACPIHANDLE_INIT(gfan);
2510 IBM_ACPIHANDLE_INIT(sfan);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002511
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002512 if (gfan_handle) {
2513 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002514 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002515 } else {
2516 /* all other ThinkPads: note that even old-style
2517 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002518 if (likely(acpi_ec_read(fan_status_offset,
2519 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002520 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002521
2522 /* In some ThinkPads, neither the EC nor the ACPI
2523 * DSDT initialize the fan status, and it ends up
2524 * being set to 0x07 when it *could* be either
2525 * 0x07 or 0x80.
2526 *
2527 * Enable for TP-1Y (T43), TP-78 (R51e),
2528 * TP-76 (R52), TP-70 (T43, R52), which are known
2529 * to be buggy. */
2530 if (fan_control_initial_status == 0x07 &&
2531 ibm_thinkpad_ec_found &&
2532 ((ibm_thinkpad_ec_found[0] == '1' &&
2533 ibm_thinkpad_ec_found[1] == 'Y') ||
2534 (ibm_thinkpad_ec_found[0] == '7' &&
2535 (ibm_thinkpad_ec_found[1] == '6' ||
2536 ibm_thinkpad_ec_found[1] == '8' ||
2537 ibm_thinkpad_ec_found[1] == '0'))
2538 )) {
2539 printk(IBM_NOTICE
2540 "fan_init: initial fan status is "
2541 "unknown, assuming it is in auto "
2542 "mode\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002543 tp_features.fan_ctrl_status_undef = 1;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002544 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002545 } else {
2546 printk(IBM_ERR
2547 "ThinkPad ACPI EC access misbehaving, "
2548 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002549 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002550 }
2551 }
2552
2553 if (sfan_handle) {
2554 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002555 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002556 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002557 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002558 } else {
2559 if (!gfan_handle) {
2560 /* gfan without sfan means no fan control */
2561 /* all other models implement TP EC 0x2f control */
2562
2563 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02002564 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002565 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002566 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002567 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002568 TPACPI_FAN_CMD_SPEED |
2569 TPACPI_FAN_CMD_LEVEL |
2570 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002571 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002572 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02002573 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002574 TPACPI_FAN_CMD_LEVEL |
2575 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002576 }
2577 }
2578 }
2579
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002580 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
2581 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
2582 fan_control_access_mode != TPACPI_FAN_WR_NONE),
2583 fan_status_access_mode, fan_control_access_mode);
2584
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002585 return (fan_status_access_mode != TPACPI_FAN_NONE ||
2586 fan_control_access_mode != TPACPI_FAN_WR_NONE)?
2587 0 : 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002588}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002589
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02002590static int fan_get_status(u8 *status)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002591{
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02002592 u8 s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002593
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02002594 /* TODO:
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002595 * Add TPACPI_FAN_RD_ACPI_FANS ? */
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02002596
Henrique de Moraes Holschuh3ef8a602006-11-24 11:47:10 -02002597 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002598 case TPACPI_FAN_RD_ACPI_GFAN:
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002599 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02002600
2601 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002602 return -EIO;
2603
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02002604 if (likely(status))
2605 *status = s & 0x07;
2606
2607 break;
2608
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002609 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02002610 /* all except 570, 600e/x, 770e, 770x */
2611 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
2612 return -EIO;
2613
2614 if (likely(status))
2615 *status = s;
2616
2617 break;
2618
2619 default:
2620 return -ENXIO;
2621 }
2622
2623 return 0;
2624}
2625
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002626static void fan_exit(void)
2627{
Henrique de Moraes Holschuh99fba3f2007-04-21 11:08:44 -03002628 vdbg_printk(TPACPI_DBG_EXIT, "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002629 cancel_delayed_work(&fan_watchdog_task);
2630 flush_scheduled_work();
2631}
2632
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02002633static int fan_get_speed(unsigned int *speed)
2634{
2635 u8 hi, lo;
2636
2637 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002638 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02002639 /* all except 570, 600e/x, 770e, 770x */
2640 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
2641 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
2642 return -EIO;
2643
2644 if (likely(speed))
2645 *speed = (hi << 8) | lo;
2646
2647 break;
2648
2649 default:
2650 return -ENXIO;
2651 }
2652
2653 return 0;
2654}
2655
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002656static void fan_watchdog_fire(struct work_struct *ignored)
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002657{
Henrique de Moraes Holschuh99fba3f2007-04-21 11:08:44 -03002658 int rc;
2659
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002660 printk(IBM_NOTICE "fan watchdog: enabling fan\n");
Henrique de Moraes Holschuh99fba3f2007-04-21 11:08:44 -03002661 rc = fan_set_enable();
2662 if (rc < 0) {
2663 printk(IBM_ERR "fan watchdog: error %d while enabling fan, "
2664 "will try again later...\n", -rc);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002665 /* reschedule for later */
2666 fan_watchdog_reset();
2667 }
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002668}
2669
2670static void fan_watchdog_reset(void)
2671{
2672 static int fan_watchdog_active = 0;
2673
2674 if (fan_watchdog_active)
2675 cancel_delayed_work(&fan_watchdog_task);
2676
2677 if (fan_watchdog_maxinterval > 0) {
2678 fan_watchdog_active = 1;
2679 if (!schedule_delayed_work(&fan_watchdog_task,
2680 msecs_to_jiffies(fan_watchdog_maxinterval
2681 * 1000))) {
2682 printk(IBM_ERR "failed to schedule the fan watchdog, "
2683 "watchdog will not trigger\n");
2684 }
2685 } else
2686 fan_watchdog_active = 0;
2687}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002688
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002689static int fan_set_level(int level)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002690{
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002691 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002692 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002693 if (level >= 0 && level <= 7) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002694 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
2695 return -EIO;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002696 } else
2697 return -EINVAL;
2698 break;
2699
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002700 case TPACPI_FAN_WR_ACPI_FANS:
2701 case TPACPI_FAN_WR_TPEC:
2702 if ((level != TP_EC_FAN_AUTO) &&
2703 (level != TP_EC_FAN_FULLSPEED) &&
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02002704 ((level < 0) || (level > 7)))
2705 return -EINVAL;
2706
2707 if (!acpi_ec_write(fan_status_offset, level))
2708 return -EIO;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002709 else
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002710 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02002711 break;
2712
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002713 default:
2714 return -ENXIO;
2715 }
2716 return 0;
2717}
2718
2719static int fan_set_enable(void)
2720{
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002721 u8 s;
2722 int rc;
2723
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002724 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002725 case TPACPI_FAN_WR_ACPI_FANS:
2726 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002727 if ((rc = fan_get_status(&s)) < 0)
2728 return rc;
2729
2730 /* Don't go out of emergency fan mode */
2731 if (s != 7)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002732 s = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002733
2734 if (!acpi_ec_write(fan_status_offset, s))
2735 return -EIO;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002736 else
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002737 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002738 break;
2739
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002740 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002741 if ((rc = fan_get_status(&s)) < 0)
2742 return rc;
2743
2744 s &= 0x07;
2745
2746 /* Set fan to at least level 4 */
2747 if (s < 4)
2748 s = 4;
2749
2750 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002751 return -EIO;
2752 break;
2753
2754 default:
2755 return -ENXIO;
2756 }
2757 return 0;
2758}
2759
2760static int fan_set_disable(void)
2761{
2762 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002763 case TPACPI_FAN_WR_ACPI_FANS:
2764 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002765 if (!acpi_ec_write(fan_status_offset, 0x00))
2766 return -EIO;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002767 else
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002768 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002769 break;
2770
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002771 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002772 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
2773 return -EIO;
2774 break;
2775
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002776 default:
2777 return -ENXIO;
2778 }
2779 return 0;
2780}
2781
2782static int fan_set_speed(int speed)
2783{
2784 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002785 case TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002786 if (speed >= 0 && speed <= 65535) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002787 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
2788 speed, speed, speed))
2789 return -EIO;
2790 } else
2791 return -EINVAL;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002792 break;
2793
2794 default:
2795 return -ENXIO;
2796 }
2797 return 0;
2798}
2799
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002800static int fan_read(char *p)
2801{
2802 int len = 0;
2803 int rc;
2804 u8 status;
2805 unsigned int speed = 0;
2806
2807 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002808 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002809 /* 570, 600e/x, 770e, 770x */
2810 if ((rc = fan_get_status(&status)) < 0)
2811 return rc;
2812
2813 len += sprintf(p + len, "status:\t\t%s\n"
2814 "level:\t\t%d\n",
2815 (status != 0) ? "enabled" : "disabled", status);
2816 break;
2817
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002818 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002819 /* all except 570, 600e/x, 770e, 770x */
2820 if ((rc = fan_get_status(&status)) < 0)
2821 return rc;
2822
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002823 if (unlikely(tp_features.fan_ctrl_status_undef)) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002824 if (status != fan_control_initial_status)
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002825 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002826 else
2827 /* Return most likely status. In fact, it
2828 * might be the only possible status */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002829 status = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002830 }
2831
2832 len += sprintf(p + len, "status:\t\t%s\n",
2833 (status != 0) ? "enabled" : "disabled");
2834
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002835 if ((rc = fan_get_speed(&speed)) < 0)
2836 return rc;
2837
2838 len += sprintf(p + len, "speed:\t\t%d\n", speed);
2839
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002840 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002841 /* Disengaged mode takes precedence */
2842 len += sprintf(p + len, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002843 else if (status & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002844 len += sprintf(p + len, "level:\t\tauto\n");
2845 else
2846 len += sprintf(p + len, "level:\t\t%d\n", status);
2847 break;
2848
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002849 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002850 default:
2851 len += sprintf(p + len, "status:\t\tnot supported\n");
2852 }
2853
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002854 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002855 len += sprintf(p + len, "commands:\tlevel <level>");
2856
2857 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002858 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002859 len += sprintf(p + len, " (<level> is 0-7)\n");
2860 break;
2861
2862 default:
2863 len += sprintf(p + len, " (<level> is 0-7, "
2864 "auto, disengaged)\n");
2865 break;
2866 }
2867 }
2868
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002869 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002870 len += sprintf(p + len, "commands:\tenable, disable\n"
2871 "commands:\twatchdog <timeout> (<timeout> is 0 (off), "
2872 "1-120 (seconds))\n");
2873
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002874 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002875 len += sprintf(p + len, "commands:\tspeed <speed>"
2876 " (<speed> is 0-65535)\n");
2877
2878 return len;
2879}
2880
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002881static int fan_write_cmd_level(const char *cmd, int *rc)
2882{
2883 int level;
2884
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02002885 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002886 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02002887 else if (strlencmp(cmd, "level disengaged") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002888 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02002889 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002890 return 0;
2891
2892 if ((*rc = fan_set_level(level)) == -ENXIO)
2893 printk(IBM_ERR "level command accepted for unsupported "
2894 "access mode %d", fan_control_access_mode);
2895
2896 return 1;
2897}
2898
2899static int fan_write_cmd_enable(const char *cmd, int *rc)
2900{
2901 if (strlencmp(cmd, "enable") != 0)
2902 return 0;
2903
2904 if ((*rc = fan_set_enable()) == -ENXIO)
2905 printk(IBM_ERR "enable command accepted for unsupported "
2906 "access mode %d", fan_control_access_mode);
2907
2908 return 1;
2909}
2910
2911static int fan_write_cmd_disable(const char *cmd, int *rc)
2912{
2913 if (strlencmp(cmd, "disable") != 0)
2914 return 0;
2915
2916 if ((*rc = fan_set_disable()) == -ENXIO)
2917 printk(IBM_ERR "disable command accepted for unsupported "
2918 "access mode %d", fan_control_access_mode);
2919
2920 return 1;
2921}
2922
2923static int fan_write_cmd_speed(const char *cmd, int *rc)
2924{
2925 int speed;
2926
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02002927 /* TODO:
2928 * Support speed <low> <medium> <high> ? */
2929
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002930 if (sscanf(cmd, "speed %d", &speed) != 1)
2931 return 0;
2932
2933 if ((*rc = fan_set_speed(speed)) == -ENXIO)
2934 printk(IBM_ERR "speed command accepted for unsupported "
2935 "access mode %d", fan_control_access_mode);
2936
2937 return 1;
2938}
2939
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002940static int fan_write_cmd_watchdog(const char *cmd, int *rc)
2941{
2942 int interval;
2943
2944 if (sscanf(cmd, "watchdog %d", &interval) != 1)
2945 return 0;
2946
2947 if (interval < 0 || interval > 120)
2948 *rc = -EINVAL;
2949 else
2950 fan_watchdog_maxinterval = interval;
2951
2952 return 1;
2953}
2954
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002955static int fan_write(char *buf)
2956{
2957 char *cmd;
2958 int rc = 0;
2959
2960 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002961 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002962 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002963 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002964 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002965 fan_write_cmd_disable(cmd, &rc) ||
2966 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002967 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002968 fan_write_cmd_speed(cmd, &rc))
2969 )
2970 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002971 else if (!rc)
2972 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002973 }
2974
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002975 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002976}
2977
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002978static struct ibm_struct fan_driver_data = {
2979 .name = "fan",
2980 .read = fan_read,
2981 .write = fan_write,
2982 .exit = fan_exit,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03002983 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002984};
2985
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002986/****************************************************************************
2987 ****************************************************************************
2988 *
2989 * Infrastructure
2990 *
2991 ****************************************************************************
2992 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002994/* /proc support */
2995static struct proc_dir_entry *proc_dir = NULL;
2996
2997/* Subdriver registry */
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002998static LIST_HEAD(tpacpi_all_drivers);
2999
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003001/*
3002 * Module and infrastructure proble, init and exit handling
3003 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003005#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003006static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003007{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003008 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003009
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003010 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003011}
3012#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
3013
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003014static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015{
3016 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003017 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 struct proc_dir_entry *entry;
3019
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003020 BUG_ON(ibm == NULL);
3021
3022 INIT_LIST_HEAD(&ibm->all_drivers);
3023
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03003024 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 return 0;
3026
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003027 dbg_printk(TPACPI_DBG_INIT,
3028 "probing for %s\n", ibm->name);
3029
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003030 if (iibm->init) {
3031 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003032 if (ret > 0)
3033 return 0; /* probe failed */
3034 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003036
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03003037 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 }
3039
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003040 if (ibm->acpi) {
3041 if (ibm->acpi->hid) {
3042 ret = register_tpacpi_subdriver(ibm);
3043 if (ret)
3044 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003045 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003046
3047 if (ibm->acpi->notify) {
3048 ret = setup_acpi_notify(ibm);
3049 if (ret == -ENODEV) {
3050 printk(IBM_NOTICE "disabling subdriver %s\n",
3051 ibm->name);
3052 ret = 0;
3053 goto err_out;
3054 }
3055 if (ret < 0)
3056 goto err_out;
3057 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003058 }
3059
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003060 dbg_printk(TPACPI_DBG_INIT,
3061 "%s installed\n", ibm->name);
3062
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003063 if (ibm->read) {
3064 entry = create_proc_entry(ibm->name,
3065 S_IFREG | S_IRUGO | S_IWUSR,
3066 proc_dir);
3067 if (!entry) {
3068 printk(IBM_ERR "unable to create proc entry %s\n",
3069 ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003070 ret = -ENODEV;
3071 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003072 }
3073 entry->owner = THIS_MODULE;
3074 entry->data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003075 entry->read_proc = &dispatch_procfs_read;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003076 if (ibm->write)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003077 entry->write_proc = &dispatch_procfs_write;
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03003078 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003079 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003081 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
3082
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003084
3085err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003086 dbg_printk(TPACPI_DBG_INIT,
3087 "%s: at error exit path with result %d\n",
3088 ibm->name, ret);
3089
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003090 ibm_exit(ibm);
3091 return (ret < 0)? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092}
3093
3094static void ibm_exit(struct ibm_struct *ibm)
3095{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003096 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003097
3098 list_del_init(&ibm->all_drivers);
3099
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003100 if (ibm->flags.acpi_notify_installed) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003101 dbg_printk(TPACPI_DBG_EXIT,
3102 "%s: acpi_remove_notify_handler\n", ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003103 BUG_ON(!ibm->acpi);
3104 acpi_remove_notify_handler(*ibm->acpi->handle,
3105 ibm->acpi->type,
3106 dispatch_acpi_notify);
3107 ibm->flags.acpi_notify_installed = 0;
3108 ibm->flags.acpi_notify_installed = 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003109 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03003111 if (ibm->flags.proc_created) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003112 dbg_printk(TPACPI_DBG_EXIT,
3113 "%s: remove_proc_entry\n", ibm->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 remove_proc_entry(ibm->name, proc_dir);
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03003115 ibm->flags.proc_created = 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003116 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003118 if (ibm->flags.acpi_driver_registered) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003119 dbg_printk(TPACPI_DBG_EXIT,
3120 "%s: acpi_bus_unregister_driver\n", ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003121 BUG_ON(!ibm->acpi);
3122 acpi_bus_unregister_driver(ibm->acpi->driver);
3123 kfree(ibm->acpi->driver);
3124 ibm->acpi->driver = NULL;
3125 ibm->flags.acpi_driver_registered = 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003126 }
3127
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03003128 if (ibm->flags.init_called && ibm->exit) {
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003129 ibm->exit();
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03003130 ibm->flags.init_called = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003132
3133 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134}
3135
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003136/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003138static char *ibm_thinkpad_ec_found = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02003140static char* __init check_dmi_for_ec(void)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003141{
3142 struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02003143 char ec_fw_string[18];
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003144
3145 /*
3146 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
3147 * X32 or newer, all Z series; Some models must have an
3148 * up-to-date BIOS or they will not be detected.
3149 *
3150 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
3151 */
3152 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02003153 if (sscanf(dev->name,
3154 "IBM ThinkPad Embedded Controller -[%17c",
3155 ec_fw_string) == 1) {
3156 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
3157 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
3158 return kstrdup(ec_fw_string, GFP_KERNEL);
3159 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003160 }
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02003161 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162}
3163
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003164static int __init probe_for_thinkpad(void)
3165{
3166 int is_thinkpad;
3167
3168 if (acpi_disabled)
3169 return -ENODEV;
3170
3171 /*
3172 * Non-ancient models have better DMI tagging, but very old models
3173 * don't.
3174 */
3175 is_thinkpad = dmi_name_in_vendors("ThinkPad");
3176
3177 /* ec is required because many other handles are relative to it */
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003178 IBM_ACPIHANDLE_INIT(ec);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003179 if (!ec_handle) {
3180 if (is_thinkpad)
3181 printk(IBM_ERR
3182 "Not yet supported ThinkPad detected!\n");
3183 return -ENODEV;
3184 }
3185
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03003186 /*
3187 * Risks a regression on very old machines, but reduces potential
3188 * false positives a damn great deal
3189 */
3190 if (!is_thinkpad)
3191 is_thinkpad = dmi_name_in_vendors("IBM");
3192
3193 if (!is_thinkpad && !force_load)
3194 return -ENODEV;
3195
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003196 return 0;
3197}
3198
3199
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003200/* Module init, exit, parameters */
3201
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003202static struct ibm_init_struct ibms_init[] __initdata = {
3203 {
3204 .init = thinkpad_acpi_driver_init,
3205 .data = &thinkpad_acpi_driver_data,
3206 },
3207 {
3208 .init = hotkey_init,
3209 .data = &hotkey_driver_data,
3210 },
3211 {
3212 .init = bluetooth_init,
3213 .data = &bluetooth_driver_data,
3214 },
3215 {
3216 .init = wan_init,
3217 .data = &wan_driver_data,
3218 },
3219 {
3220 .init = video_init,
3221 .data = &video_driver_data,
3222 },
3223 {
3224 .init = light_init,
3225 .data = &light_driver_data,
3226 },
3227#ifdef CONFIG_THINKPAD_ACPI_DOCK
3228 {
3229 .init = dock_init,
3230 .data = &dock_driver_data[0],
3231 },
3232 {
3233 .data = &dock_driver_data[1],
3234 },
3235#endif
3236#ifdef CONFIG_THINKPAD_ACPI_BAY
3237 {
3238 .init = bay_init,
3239 .data = &bay_driver_data,
3240 },
3241#endif
3242 {
3243 .init = cmos_init,
3244 .data = &cmos_driver_data,
3245 },
3246 {
3247 .init = led_init,
3248 .data = &led_driver_data,
3249 },
3250 {
3251 .init = beep_init,
3252 .data = &beep_driver_data,
3253 },
3254 {
3255 .init = thermal_init,
3256 .data = &thermal_driver_data,
3257 },
3258 {
3259 .data = &ecdump_driver_data,
3260 },
3261 {
3262 .init = brightness_init,
3263 .data = &brightness_driver_data,
3264 },
3265 {
3266 .data = &volume_driver_data,
3267 },
3268 {
3269 .init = fan_init,
3270 .data = &fan_driver_data,
3271 },
3272};
3273
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003274static int __init set_ibm_param(const char *val, struct kernel_param *kp)
3275{
3276 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003277 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003278
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003279 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
3280 ibm = ibms_init[i].data;
3281 BUG_ON(ibm == NULL);
3282
3283 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
3284 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003285 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003286 strcpy(ibms_init[i].param, val);
3287 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003288 return 0;
3289 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003290 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003291
3292 return -EINVAL;
3293}
3294
3295static int experimental;
3296module_param(experimental, int, 0);
3297
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03003298static u32 dbg_level;
3299module_param_named(debug, dbg_level, uint, 0);
3300
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03003301static int force_load;
3302module_param(force_load, int, 0);
3303
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003304#define IBM_PARAM(feature) \
3305 module_param_call(feature, set_ibm_param, NULL, NULL, 0)
3306
3307IBM_PARAM(hotkey);
3308IBM_PARAM(bluetooth);
3309IBM_PARAM(video);
3310IBM_PARAM(light);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003311#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003312IBM_PARAM(dock);
3313#endif
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003314#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003315IBM_PARAM(bay);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003316#endif /* CONFIG_THINKPAD_ACPI_BAY */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003317IBM_PARAM(cmos);
3318IBM_PARAM(led);
3319IBM_PARAM(beep);
3320IBM_PARAM(ecdump);
3321IBM_PARAM(brightness);
3322IBM_PARAM(volume);
3323IBM_PARAM(fan);
3324
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03003325static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326{
3327 int ret, i;
3328
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03003329 /* Driver-level probe */
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003330 ret = probe_for_thinkpad();
3331 if (ret)
3332 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03003334 /* Driver initialization */
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003335 ibm_thinkpad_ec_found = check_dmi_for_ec();
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003336 IBM_ACPIHANDLE_INIT(ecrd);
3337 IBM_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003339 proc_dir = proc_mkdir(IBM_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 if (!proc_dir) {
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003341 printk(IBM_ERR "unable to create proc dir " IBM_PROC_DIR);
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03003342 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 return -ENODEV;
3344 }
3345 proc_dir->owner = THIS_MODULE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003346
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03003347 ret = platform_driver_register(&tpacpi_pdriver);
3348 if (ret) {
3349 printk(IBM_ERR "unable to register platform driver\n");
3350 thinkpad_acpi_module_exit();
3351 return ret;
3352 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03003353 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
3354 if (ret) {
3355 printk(IBM_ERR "unable to create sysfs driver attributes\n");
3356 thinkpad_acpi_module_exit();
3357 return ret;
3358 }
3359
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03003360
3361 /* Device initialization */
3362 tpacpi_pdev = platform_device_register_simple(IBM_DRVR_NAME, -1,
3363 NULL, 0);
3364 if (IS_ERR(tpacpi_pdev)) {
3365 ret = PTR_ERR(tpacpi_pdev);
3366 tpacpi_pdev = NULL;
3367 printk(IBM_ERR "unable to register platform device\n");
3368 thinkpad_acpi_module_exit();
3369 return ret;
3370 }
3371 tpacpi_hwmon = hwmon_device_register(&tpacpi_pdev->dev);
3372 if (IS_ERR(tpacpi_hwmon)) {
3373 ret = PTR_ERR(tpacpi_hwmon);
3374 tpacpi_hwmon = NULL;
3375 printk(IBM_ERR "unable to register hwmon device\n");
3376 thinkpad_acpi_module_exit();
3377 return ret;
3378 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003379 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
3380 ret = ibm_init(&ibms_init[i]);
3381 if (ret >= 0 && *ibms_init[i].param)
3382 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03003384 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 return ret;
3386 }
3387 }
3388
3389 return 0;
3390}
3391
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03003392static void thinkpad_acpi_module_exit(void)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003393{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003394 struct ibm_struct *ibm, *itmp;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003395
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003396 list_for_each_entry_safe_reverse(ibm, itmp,
3397 &tpacpi_all_drivers,
3398 all_drivers) {
3399 ibm_exit(ibm);
3400 }
3401
3402 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003403
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03003404 if (tpacpi_hwmon)
3405 hwmon_device_unregister(tpacpi_hwmon);
3406
3407 if (tpacpi_pdev)
3408 platform_device_unregister(tpacpi_pdev);
3409
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03003410 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03003411 platform_driver_unregister(&tpacpi_pdriver);
3412
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003413 if (proc_dir)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003414 remove_proc_entry(IBM_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003415
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003416 kfree(ibm_thinkpad_ec_found);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003417}
3418
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03003419module_init(thinkpad_acpi_module_init);
3420module_exit(thinkpad_acpi_module_exit);