blob: ca6d15cdc5f01b4ccd467378b76806b1db5f23be [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;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -0300523
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300524 if (parse_strtoul(buf, 0xffff, &t))
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -0300525 return -EINVAL;
526
527 dbg_level = t;
528
529 return count;
530}
531
532static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
533 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
534
535/* version ------------------------------------------------------------- */
536static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
537 char *buf)
538{
539 return snprintf(buf, PAGE_SIZE, "%s v%s\n", IBM_DESC, IBM_VERSION);
540}
541
542static DRIVER_ATTR(version, S_IRUGO,
543 tpacpi_driver_version_show, NULL);
544
545/* --------------------------------------------------------------------- */
546
547static struct driver_attribute* tpacpi_driver_attributes[] = {
548 &driver_attr_debug_level, &driver_attr_version,
549 &driver_attr_interface_version,
550};
551
552static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
553{
554 int i, res;
555
556 i = 0;
557 res = 0;
558 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
559 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
560 i++;
561 }
562
563 return res;
564}
565
566static void tpacpi_remove_driver_attributes(struct device_driver *drv)
567{
568 int i;
569
570 for(i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
571 driver_remove_file(drv, tpacpi_driver_attributes[i]);
572}
573
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300574/*************************************************************************
575 * sysfs support helpers
576 */
577
578struct attribute_set_obj {
579 struct attribute_set s;
580 struct attribute *a;
581} __attribute__((packed));
582
583static struct attribute_set *create_attr_set(unsigned int max_members,
584 const char* name)
585{
586 struct attribute_set_obj *sobj;
587
588 if (max_members == 0)
589 return NULL;
590
591 /* Allocates space for implicit NULL at the end too */
592 sobj = kzalloc(sizeof(struct attribute_set_obj) +
593 max_members * sizeof(struct attribute *),
594 GFP_KERNEL);
595 if (!sobj)
596 return NULL;
597 sobj->s.max_members = max_members;
598 sobj->s.group.attrs = &sobj->a;
599 sobj->s.group.name = name;
600
601 return &sobj->s;
602}
603
604/* not multi-threaded safe, use it in a single thread per set */
605static int add_to_attr_set(struct attribute_set* s, struct attribute *attr)
606{
607 if (!s || !attr)
608 return -EINVAL;
609
610 if (s->members >= s->max_members)
611 return -ENOMEM;
612
613 s->group.attrs[s->members] = attr;
614 s->members++;
615
616 return 0;
617}
618
619static int add_many_to_attr_set(struct attribute_set* s,
620 struct attribute **attr,
621 unsigned int count)
622{
623 int i, res;
624
625 for (i = 0; i < count; i++) {
626 res = add_to_attr_set(s, attr[i]);
627 if (res)
628 return res;
629 }
630
631 return 0;
632}
633
634static void delete_attr_set(struct attribute_set* s, struct kobject *kobj)
635{
636 sysfs_remove_group(kobj, &s->group);
637 destroy_attr_set(s);
638}
639
640static int parse_strtoul(const char *buf,
641 unsigned long max, unsigned long *value)
642{
643 char *endp;
644
645 *value = simple_strtoul(buf, &endp, 0);
646 while (*endp && isspace(*endp))
647 endp++;
648 if (*endp || *value > max)
649 return -EINVAL;
650
651 return 0;
652}
653
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300654/****************************************************************************
655 ****************************************************************************
656 *
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300657 * Subdrivers
658 *
659 ****************************************************************************
660 ****************************************************************************/
661
662/*************************************************************************
Henrique de Moraes Holschuh142cfc92007-04-21 11:08:26 -0300663 * thinkpad-acpi init subdriver
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300664 */
665
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300666static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
668 printk(IBM_INFO "%s v%s\n", IBM_DESC, IBM_VERSION);
669 printk(IBM_INFO "%s\n", IBM_URL);
670
Henrique de Moraes Holschuh3945ac32007-02-06 19:13:44 -0200671 if (ibm_thinkpad_ec_found)
672 printk(IBM_INFO "ThinkPad EC firmware %s\n",
673 ibm_thinkpad_ec_found);
674
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 return 0;
676}
677
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300678static int thinkpad_acpi_driver_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679{
680 int len = 0;
681
682 len += sprintf(p + len, "driver:\t\t%s\n", IBM_DESC);
683 len += sprintf(p + len, "version:\t%s\n", IBM_VERSION);
684
685 return len;
686}
687
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300688static struct ibm_struct thinkpad_acpi_driver_data = {
689 .name = "driver",
690 .read = thinkpad_acpi_driver_read,
691};
692
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300693/*************************************************************************
694 * Hotkey subdriver
695 */
696
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400697static int hotkey_orig_status;
698static int hotkey_orig_mask;
699
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300700static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300701{
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300702 int res;
703
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300704 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
705
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300706 IBM_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300707
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300708 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300709 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300710
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300711 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300712 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300713
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300714 if (tp_features.hotkey) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300715 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
716 A30, R30, R31, T20-22, X20-21, X22-24 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300717 tp_features.hotkey_mask =
718 acpi_evalf(hkey_handle, NULL, "DHKN", "qv");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300719
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300720 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300721 str_supported(tp_features.hotkey_mask));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300722
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300723 res = hotkey_get(&hotkey_orig_status, &hotkey_orig_mask);
724 if (res)
725 return res;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300726 }
727
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300728 return (tp_features.hotkey)? 0 : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300729}
730
731static void hotkey_exit(void)
732{
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300733 int res;
734
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300735 if (tp_features.hotkey) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300736 dbg_printk(TPACPI_DBG_EXIT, "restoring original hotkey mask\n");
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300737 res = hotkey_set(hotkey_orig_status, hotkey_orig_mask);
738 if (res)
739 printk(IBM_ERR "failed to restore hotkey to BIOS defaults\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300740 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300741}
742
743static void hotkey_notify(struct ibm_struct *ibm, u32 event)
744{
745 int hkey;
746
747 if (acpi_evalf(hkey_handle, &hkey, "MHKP", "d"))
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300748 acpi_bus_generate_event(ibm->acpi->device, event, hkey);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300749 else {
750 printk(IBM_ERR "unknown hotkey event %d\n", event);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300751 acpi_bus_generate_event(ibm->acpi->device, event, 0);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300752 }
753}
754
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400755static int hotkey_get(int *status, int *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756{
757 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300758 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400759
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300760 if (tp_features.hotkey_mask)
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400761 if (!acpi_evalf(hkey_handle, mask, "DHKN", "d"))
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300762 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400763
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300764 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765}
766
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400767static int hotkey_set(int status, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768{
769 int i;
770
771 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300772 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300774 if (tp_features.hotkey_mask)
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400775 for (i = 0; i < 32; i++) {
776 int bit = ((1 << i) & mask) != 0;
777 if (!acpi_evalf(hkey_handle,
778 NULL, "MHKM", "vdd", i + 1, bit))
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300779 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400780 }
781
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300782 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400783}
784
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400785static int hotkey_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300787 int res, status, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 int len = 0;
789
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300790 if (!tp_features.hotkey) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400791 len += sprintf(p + len, "status:\t\tnot supported\n");
792 return len;
793 }
794
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300795 res = hotkey_get(&status, &mask);
796 if (res)
797 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
799 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300800 if (tp_features.hotkey_mask) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 len += sprintf(p + len, "mask:\t\t0x%04x\n", mask);
802 len += sprintf(p + len,
803 "commands:\tenable, disable, reset, <mask>\n");
804 } else {
805 len += sprintf(p + len, "mask:\t\tnot supported\n");
806 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
807 }
808
809 return len;
810}
811
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400812static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813{
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300814 int res, status, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 char *cmd;
816 int do_cmd = 0;
817
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300818 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 return -ENODEV;
820
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300821 res = hotkey_get(&status, &mask);
822 if (res)
823 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400824
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 while ((cmd = next_cmd(&buf))) {
826 if (strlencmp(cmd, "enable") == 0) {
827 status = 1;
828 } else if (strlencmp(cmd, "disable") == 0) {
829 status = 0;
830 } else if (strlencmp(cmd, "reset") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400831 status = hotkey_orig_status;
832 mask = hotkey_orig_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
834 /* mask set */
835 } else if (sscanf(cmd, "%x", &mask) == 1) {
836 /* mask set */
837 } else
838 return -EINVAL;
839 do_cmd = 1;
840 }
841
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300842 if (do_cmd) {
843 res = hotkey_set(status, mask);
844 if (res)
845 return res;
846 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
848 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400849}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300851static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300852 .hid = IBM_HKEY_HID,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300853 .notify = hotkey_notify,
854 .handle = &hkey_handle,
855 .type = ACPI_DEVICE_NOTIFY,
856};
857
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300858static struct ibm_struct hotkey_driver_data = {
859 .name = "hotkey",
860 .read = hotkey_read,
861 .write = hotkey_write,
862 .exit = hotkey_exit,
863 .acpi = &ibm_hotkey_acpidriver,
864};
865
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300866/*************************************************************************
867 * Bluetooth subdriver
868 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300870static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -0300872 int status = 0;
873
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300874 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
875
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300876 IBM_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300877
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400878 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
879 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300880 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -0300881 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -0300883 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
884 str_supported(tp_features.bluetooth),
885 status);
886
887 if (tp_features.bluetooth &&
888 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
889 /* no bluetooth hardware present in system */
890 tp_features.bluetooth = 0;
891 dbg_printk(TPACPI_DBG_INIT,
892 "bluetooth hardware not installed\n");
893 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300894
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300895 return (tp_features.bluetooth)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896}
897
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -0300898static int bluetooth_get_radiosw(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899{
900 int status;
901
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -0300902 if (!tp_features.bluetooth)
903 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -0300905 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
906 return -EIO;
907
908 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
909}
910
911static int bluetooth_set_radiosw(int radio_on)
912{
913 int status;
914
915 if (!tp_features.bluetooth)
916 return -ENODEV;
917
918 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
919 return -EIO;
920 if (radio_on)
921 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
922 else
923 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
924 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
925 return -EIO;
926
927 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928}
929
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400930static int bluetooth_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931{
932 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -0300933 int status = bluetooth_get_radiosw();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300935 if (!tp_features.bluetooth)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 len += sprintf(p + len, "status:\t\tnot supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -0300938 len += sprintf(p + len, "status:\t\t%s\n",
939 (status)? "enabled" : "disabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 len += sprintf(p + len, "commands:\tenable, disable\n");
941 }
942
943 return len;
944}
945
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400946static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300950 if (!tp_features.bluetooth)
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400951 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
953 while ((cmd = next_cmd(&buf))) {
954 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -0300955 bluetooth_set_radiosw(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -0300957 bluetooth_set_radiosw(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 } else
959 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 }
961
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 return 0;
963}
964
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300965static struct ibm_struct bluetooth_driver_data = {
966 .name = "bluetooth",
967 .read = bluetooth_read,
968 .write = bluetooth_write,
969};
970
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300971/*************************************************************************
972 * Wan subdriver
973 */
974
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300975static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -0400976{
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -0300977 int status = 0;
978
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300979 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
980
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300981 IBM_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300982
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300983 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -0300984 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -0400985
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -0300986 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
987 str_supported(tp_features.wan),
988 status);
989
990 if (tp_features.wan &&
991 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
992 /* no wan hardware present in system */
993 tp_features.wan = 0;
994 dbg_printk(TPACPI_DBG_INIT,
995 "wan hardware not installed\n");
996 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300997
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300998 return (tp_features.wan)? 0 : 1;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -0400999}
1000
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001001static int wan_get_radiosw(void)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001002{
1003 int status;
1004
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001005 if (!tp_features.wan)
1006 return -ENODEV;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001007
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001008 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
1009 return -EIO;
1010
1011 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
1012}
1013
1014static int wan_set_radiosw(int radio_on)
1015{
1016 int status;
1017
1018 if (!tp_features.wan)
1019 return -ENODEV;
1020
1021 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
1022 return -EIO;
1023 if (radio_on)
1024 status |= TP_ACPI_WANCARD_RADIOSSW;
1025 else
1026 status &= ~TP_ACPI_WANCARD_RADIOSSW;
1027 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
1028 return -EIO;
1029
1030 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001031}
1032
1033static int wan_read(char *p)
1034{
1035 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001036 int status = wan_get_radiosw();
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001037
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001038 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001039 len += sprintf(p + len, "status:\t\tnot supported\n");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001040 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001041 len += sprintf(p + len, "status:\t\t%s\n",
1042 (status)? "enabled" : "disabled");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001043 len += sprintf(p + len, "commands:\tenable, disable\n");
1044 }
1045
1046 return len;
1047}
1048
1049static int wan_write(char *buf)
1050{
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001051 char *cmd;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001052
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001053 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001054 return -ENODEV;
1055
1056 while ((cmd = next_cmd(&buf))) {
1057 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001058 wan_set_radiosw(1);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001059 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001060 wan_set_radiosw(0);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001061 } else
1062 return -EINVAL;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001063 }
1064
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001065 return 0;
1066}
1067
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001068static struct ibm_struct wan_driver_data = {
1069 .name = "wan",
1070 .read = wan_read,
1071 .write = wan_write,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03001072 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001073};
1074
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001075/*************************************************************************
1076 * Video subdriver
1077 */
1078
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02001079static enum video_access_mode video_supported;
1080static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001081
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001082IBM_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
1083 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
1084 "\\_SB.PCI0.VID0", /* 770e */
1085 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
1086 "\\_SB.PCI0.AGP.VID", /* all others */
1087 ); /* R30, R31 */
1088
1089IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
1090
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001091static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001093 int ivga;
1094
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001095 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
1096
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001097 IBM_ACPIHANDLE_INIT(vid);
1098 IBM_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001099
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001100 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
1101 /* G41, assume IVGA doesn't change */
1102 vid_handle = vid2_handle;
1103
1104 if (!vid_handle)
1105 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001106 video_supported = TPACPI_VIDEO_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001107 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
1108 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001109 video_supported = TPACPI_VIDEO_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001110 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
1111 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001112 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001113 else
1114 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001115 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001117 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
1118 str_supported(video_supported != TPACPI_VIDEO_NONE),
1119 video_supported);
1120
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001121 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122}
1123
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001124static void video_exit(void)
1125{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001126 dbg_printk(TPACPI_DBG_EXIT,
1127 "restoring original video autoswitch mode\n");
1128 if (video_autosw_set(video_orig_autosw))
1129 printk(IBM_ERR "error while trying to restore original "
1130 "video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001131}
1132
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001133static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134{
1135 int status = 0;
1136 int i;
1137
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001138 switch (video_supported) {
1139 case TPACPI_VIDEO_570:
1140 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
1141 TP_ACPI_VIDEO_570_PHSCMD))
1142 return -EIO;
1143 status = i & TP_ACPI_VIDEO_570_PHSMASK;
1144 break;
1145 case TPACPI_VIDEO_770:
1146 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
1147 return -EIO;
1148 if (i)
1149 status |= TP_ACPI_VIDEO_S_LCD;
1150 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
1151 return -EIO;
1152 if (i)
1153 status |= TP_ACPI_VIDEO_S_CRT;
1154 break;
1155 case TPACPI_VIDEO_NEW:
1156 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
1157 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
1158 return -EIO;
1159 if (i)
1160 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001162 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
1163 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
1164 return -EIO;
1165 if (i)
1166 status |= TP_ACPI_VIDEO_S_LCD;
1167 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
1168 return -EIO;
1169 if (i)
1170 status |= TP_ACPI_VIDEO_S_DVI;
1171 break;
1172 default:
1173 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001174 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
1176 return status;
1177}
1178
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001179static int video_outputsw_set(int status)
1180{
1181 int autosw;
1182 int res = 0;
1183
1184 switch (video_supported) {
1185 case TPACPI_VIDEO_570:
1186 res = acpi_evalf(NULL, NULL,
1187 "\\_SB.PHS2", "vdd",
1188 TP_ACPI_VIDEO_570_PHS2CMD,
1189 status | TP_ACPI_VIDEO_570_PHS2SET);
1190 break;
1191 case TPACPI_VIDEO_770:
1192 autosw = video_autosw_get();
1193 if (autosw < 0)
1194 return autosw;
1195
1196 res = video_autosw_set(1);
1197 if (res)
1198 return res;
1199 res = acpi_evalf(vid_handle, NULL,
1200 "ASWT", "vdd", status * 0x100, 0);
1201 if (!autosw && video_autosw_set(autosw)) {
1202 printk(IBM_ERR "video auto-switch left enabled due to error\n");
1203 return -EIO;
1204 }
1205 break;
1206 case TPACPI_VIDEO_NEW:
1207 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
1208 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
1209 break;
1210 default:
1211 return -ENOSYS;
1212 }
1213
1214 return (res)? 0 : -EIO;
1215}
1216
1217static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001219 int autosw = 0;
1220
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001221 switch (video_supported) {
1222 case TPACPI_VIDEO_570:
1223 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
1224 return -EIO;
1225 break;
1226 case TPACPI_VIDEO_770:
1227 case TPACPI_VIDEO_NEW:
1228 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
1229 return -EIO;
1230 break;
1231 default:
1232 return -ENOSYS;
1233 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001234
1235 return autosw & 1;
1236}
1237
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001238static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001239{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001240 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001241 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001242 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001243}
1244
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001245static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001246{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001247 int autosw = video_autosw_get();
1248 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001249
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001250 if (autosw < 0)
1251 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001252
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001253 switch (video_supported) {
1254 case TPACPI_VIDEO_570:
1255 res = video_autosw_set(1);
1256 if (res)
1257 return res;
1258 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
1259 break;
1260 case TPACPI_VIDEO_770:
1261 case TPACPI_VIDEO_NEW:
1262 res = video_autosw_set(1);
1263 if (res)
1264 return res;
1265 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
1266 break;
1267 default:
1268 return -ENOSYS;
1269 }
1270 if (!autosw && video_autosw_set(autosw)) {
1271 printk(IBM_ERR "video auto-switch left enabled due to error\n");
1272 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001273 }
1274
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001275 return (res)? 0 : -EIO;
1276}
1277
1278static int video_expand_toggle(void)
1279{
1280 switch (video_supported) {
1281 case TPACPI_VIDEO_570:
1282 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
1283 0 : -EIO;
1284 case TPACPI_VIDEO_770:
1285 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
1286 0 : -EIO;
1287 case TPACPI_VIDEO_NEW:
1288 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
1289 0 : -EIO;
1290 default:
1291 return -ENOSYS;
1292 }
1293 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001294}
1295
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001296static int video_read(char *p)
1297{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001298 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001299 int len = 0;
1300
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001301 if (video_supported == TPACPI_VIDEO_NONE) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001302 len += sprintf(p + len, "status:\t\tnot supported\n");
1303 return len;
1304 }
1305
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001306 status = video_outputsw_get();
1307 if (status < 0)
1308 return status;
1309
1310 autosw = video_autosw_get();
1311 if (autosw < 0)
1312 return autosw;
1313
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001314 len += sprintf(p + len, "status:\t\tsupported\n");
1315 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
1316 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001317 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001318 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
1319 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
1320 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
1321 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001322 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001323 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
1324 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
1325 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
1326
1327 return len;
1328}
1329
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001330static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331{
1332 char *cmd;
1333 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001334 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001336 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001337 return -ENODEV;
1338
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001339 enable = 0;
1340 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
1342 while ((cmd = next_cmd(&buf))) {
1343 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001344 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001346 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001348 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001350 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001351 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001352 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001353 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001354 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001355 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001356 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001358 res = video_autosw_set(1);
1359 if (res)
1360 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001362 res = video_autosw_set(0);
1363 if (res)
1364 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001366 res = video_outputsw_cycle();
1367 if (res)
1368 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001370 res = video_expand_toggle();
1371 if (res)
1372 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 } else
1374 return -EINVAL;
1375 }
1376
1377 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001378 status = video_outputsw_get();
1379 if (status < 0)
1380 return status;
1381 res = video_outputsw_set((status & ~disable) | enable);
1382 if (res)
1383 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 }
1385
1386 return 0;
1387}
1388
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001389static struct ibm_struct video_driver_data = {
1390 .name = "video",
1391 .read = video_read,
1392 .write = video_write,
1393 .exit = video_exit,
1394};
1395
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001396/*************************************************************************
1397 * Light (thinklight) subdriver
1398 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001400IBM_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
1401IBM_HANDLE(ledb, ec, "LEDB"); /* G4x */
1402
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001403static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001405 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
1406
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001407 IBM_ACPIHANDLE_INIT(ledb);
1408 IBM_ACPIHANDLE_INIT(lght);
1409 IBM_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001410
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001411 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001412 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001413
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001414 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001415 /* light status not supported on
1416 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001417 tp_features.light_status =
1418 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001420 vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001421 str_supported(tp_features.light));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001422
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001423 return (tp_features.light)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424}
1425
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001426static int light_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427{
1428 int len = 0;
1429 int status = 0;
1430
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001431 if (!tp_features.light) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001432 len += sprintf(p + len, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001433 } else if (!tp_features.light_status) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001434 len += sprintf(p + len, "status:\t\tunknown\n");
1435 len += sprintf(p + len, "commands:\ton, off\n");
1436 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
1438 return -EIO;
1439 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001440 len += sprintf(p + len, "commands:\ton, off\n");
1441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
1443 return len;
1444}
1445
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001446static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447{
1448 int cmos_cmd, lght_cmd;
1449 char *cmd;
1450 int success;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001451
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001452 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001453 return -ENODEV;
1454
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 while ((cmd = next_cmd(&buf))) {
1456 if (strlencmp(cmd, "on") == 0) {
1457 cmos_cmd = 0x0c;
1458 lght_cmd = 1;
1459 } else if (strlencmp(cmd, "off") == 0) {
1460 cmos_cmd = 0x0d;
1461 lght_cmd = 0;
1462 } else
1463 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001464
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 success = cmos_handle ?
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001466 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
1467 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 if (!success)
1469 return -EIO;
1470 }
1471
1472 return 0;
1473}
1474
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001475static struct ibm_struct light_driver_data = {
1476 .name = "light",
1477 .read = light_read,
1478 .write = light_write,
1479};
1480
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001481/*************************************************************************
1482 * Dock subdriver
1483 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03001485#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001486
1487IBM_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
1488 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
1489 "\\_SB.PCI0.PCI1.DOCK", /* all others */
1490 "\\_SB.PCI.ISA.SLCE", /* 570 */
1491 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
1492
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001493/* don't list other alternatives as we install a notify handler on the 570 */
1494IBM_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
1495
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496#define dock_docked() (_sta(dock_handle) & 1)
1497
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001498static int __init dock_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001499{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001500 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
1501
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001502 IBM_ACPIHANDLE_INIT(dock);
1503 IBM_ACPIHANDLE_INIT(pci);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001504
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001505 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
1506 str_supported(dock_handle != NULL));
1507
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001508 return (dock_handle)? 0 : 1;
1509}
1510
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001511static void dock_notify(struct ibm_struct *ibm, u32 event)
1512{
1513 int docked = dock_docked();
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001514 int pci = ibm->acpi->hid && strstr(ibm->acpi->hid, IBM_PCI_HID);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001515
1516 if (event == 1 && !pci) /* 570 */
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001517 acpi_bus_generate_event(ibm->acpi->device, event, 1); /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001518 else if (event == 1 && pci) /* 570 */
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001519 acpi_bus_generate_event(ibm->acpi->device, event, 3); /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001520 else if (event == 3 && docked)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001521 acpi_bus_generate_event(ibm->acpi->device, event, 1); /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001522 else if (event == 3 && !docked)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001523 acpi_bus_generate_event(ibm->acpi->device, event, 2); /* undock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001524 else if (event == 0 && docked)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001525 acpi_bus_generate_event(ibm->acpi->device, event, 3); /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001526 else {
1527 printk(IBM_ERR "unknown dock event %d, status %d\n",
1528 event, _sta(dock_handle));
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001529 acpi_bus_generate_event(ibm->acpi->device, event, 0); /* unknown */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001530 }
1531}
1532
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001533static int dock_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534{
1535 int len = 0;
1536 int docked = dock_docked();
1537
1538 if (!dock_handle)
1539 len += sprintf(p + len, "status:\t\tnot supported\n");
1540 else if (!docked)
1541 len += sprintf(p + len, "status:\t\tundocked\n");
1542 else {
1543 len += sprintf(p + len, "status:\t\tdocked\n");
1544 len += sprintf(p + len, "commands:\tdock, undock\n");
1545 }
1546
1547 return len;
1548}
1549
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001550static int dock_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551{
1552 char *cmd;
1553
1554 if (!dock_docked())
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001555 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
1557 while ((cmd = next_cmd(&buf))) {
1558 if (strlencmp(cmd, "undock") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001559 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
1560 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 return -EIO;
1562 } else if (strlencmp(cmd, "dock") == 0) {
1563 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
1564 return -EIO;
1565 } else
1566 return -EINVAL;
1567 }
1568
1569 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001570}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001572static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001573 {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001574 .notify = dock_notify,
1575 .handle = &dock_handle,
1576 .type = ACPI_SYSTEM_NOTIFY,
1577 },
1578 {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001579 .hid = IBM_PCI_HID,
1580 .notify = dock_notify,
1581 .handle = &pci_handle,
1582 .type = ACPI_SYSTEM_NOTIFY,
1583 },
1584};
1585
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001586static struct ibm_struct dock_driver_data[2] = {
1587 {
1588 .name = "dock",
1589 .read = dock_read,
1590 .write = dock_write,
1591 .acpi = &ibm_dock_acpidriver[0],
1592 },
1593 {
1594 .name = "dock",
1595 .acpi = &ibm_dock_acpidriver[1],
1596 },
1597};
1598
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03001599#endif /* CONFIG_THINKPAD_ACPI_DOCK */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001601/*************************************************************************
1602 * Bay subdriver
1603 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03001605#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001606IBM_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
1607 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
1608 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
1609 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
1610 ); /* A21e, R30, R31 */
1611IBM_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
1612 "_EJ0", /* all others */
1613 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
1614IBM_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
1615 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
1616 ); /* all others */
1617IBM_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
1618 "_EJ0", /* 770x */
1619 ); /* all others */
1620
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001621static int __init bay_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001623 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
1624
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001625 IBM_ACPIHANDLE_INIT(bay);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001626 if (bay_handle)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001627 IBM_ACPIHANDLE_INIT(bay_ej);
1628 IBM_ACPIHANDLE_INIT(bay2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001629 if (bay2_handle)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001630 IBM_ACPIHANDLE_INIT(bay2_ej);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001631
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001632 tp_features.bay_status = bay_handle &&
1633 acpi_evalf(bay_handle, NULL, "_STA", "qv");
1634 tp_features.bay_status2 = bay2_handle &&
1635 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001636
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001637 tp_features.bay_eject = bay_handle && bay_ej_handle &&
1638 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
1639 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
1640 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001642 vdbg_printk(TPACPI_DBG_INIT,
1643 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001644 str_supported(tp_features.bay_status),
1645 str_supported(tp_features.bay_eject),
1646 str_supported(tp_features.bay_status2),
1647 str_supported(tp_features.bay_eject2));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001648
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001649 return (tp_features.bay_status || tp_features.bay_eject ||
1650 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651}
1652
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001653static void bay_notify(struct ibm_struct *ibm, u32 event)
1654{
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001655 acpi_bus_generate_event(ibm->acpi->device, event, 0);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001656}
1657
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001658#define bay_occupied(b) (_sta(b##_handle) & 1)
1659
1660static int bay_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661{
1662 int len = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001663 int occupied = bay_occupied(bay);
1664 int occupied2 = bay_occupied(bay2);
1665 int eject, eject2;
1666
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001667 len += sprintf(p + len, "status:\t\t%s\n",
1668 tp_features.bay_status ?
1669 (occupied ? "occupied" : "unoccupied") :
1670 "not supported");
1671 if (tp_features.bay_status2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001672 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
1673 "occupied" : "unoccupied");
1674
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001675 eject = tp_features.bay_eject && occupied;
1676 eject2 = tp_features.bay_eject2 && occupied2;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001677
1678 if (eject && eject2)
1679 len += sprintf(p + len, "commands:\teject, eject2\n");
1680 else if (eject)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 len += sprintf(p + len, "commands:\teject\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001682 else if (eject2)
1683 len += sprintf(p + len, "commands:\teject2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684
1685 return len;
1686}
1687
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001688static int bay_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689{
1690 char *cmd;
1691
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001692 if (!tp_features.bay_eject && !tp_features.bay_eject2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001693 return -ENODEV;
1694
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001696 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001697 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
1698 return -EIO;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001699 } else if (tp_features.bay_eject2 &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001700 strlencmp(cmd, "eject2") == 0) {
1701 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 return -EIO;
1703 } else
1704 return -EINVAL;
1705 }
1706
1707 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001708}
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001709
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001710static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
1711 .notify = bay_notify,
1712 .handle = &bay_handle,
1713 .type = ACPI_SYSTEM_NOTIFY,
1714};
1715
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001716static struct ibm_struct bay_driver_data = {
1717 .name = "bay",
1718 .read = bay_read,
1719 .write = bay_write,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001720 .acpi = &ibm_bay_acpidriver,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001721};
1722
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03001723#endif /* CONFIG_THINKPAD_ACPI_BAY */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001725/*************************************************************************
1726 * CMOS subdriver
1727 */
1728
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001729static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001730{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001731 vdbg_printk(TPACPI_DBG_INIT,
1732 "initializing cmos commands subdriver\n");
1733
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001734 IBM_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001735
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001736 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
1737 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001738 return (cmos_handle)? 0 : 1;
1739}
1740
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001741static int cmos_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742{
1743 int len = 0;
1744
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001745 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1746 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 if (!cmos_handle)
1748 len += sprintf(p + len, "status:\t\tnot supported\n");
1749 else {
1750 len += sprintf(p + len, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001751 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 }
1753
1754 return len;
1755}
1756
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001757static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758{
1759 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03001760 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
1762 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001763 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
1764 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 /* cmos_cmd set */
1766 } else
1767 return -EINVAL;
1768
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03001769 res = issue_thinkpad_cmos_command(cmos_cmd);
1770 if (res)
1771 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 }
1773
1774 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001775}
1776
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001777static struct ibm_struct cmos_driver_data = {
1778 .name = "cmos",
1779 .read = cmos_read,
1780 .write = cmos_write,
1781};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001782
1783/*************************************************************************
1784 * LED subdriver
1785 */
1786
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02001787static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001788
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001789IBM_HANDLE(led, ec, "SLED", /* 570 */
1790 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
1791 "LED", /* all others */
1792 ); /* R30, R31 */
1793
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001794static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001795{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001796 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
1797
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001798 IBM_ACPIHANDLE_INIT(led);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001799
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001800 if (!led_handle)
1801 /* led not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001802 led_supported = TPACPI_LED_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001803 else if (strlencmp(led_path, "SLED") == 0)
1804 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001805 led_supported = TPACPI_LED_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001806 else if (strlencmp(led_path, "SYSL") == 0)
1807 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001808 led_supported = TPACPI_LED_OLD;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001809 else
1810 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001811 led_supported = TPACPI_LED_NEW;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001812
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001813 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
1814 str_supported(led_supported), led_supported);
1815
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001816 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001817}
1818
1819#define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
1820
1821static int led_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822{
1823 int len = 0;
1824
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001825 if (!led_supported) {
1826 len += sprintf(p + len, "status:\t\tnot supported\n");
1827 return len;
1828 }
1829 len += sprintf(p + len, "status:\t\tsupported\n");
1830
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001831 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001832 /* 570 */
1833 int i, status;
1834 for (i = 0; i < 8; i++) {
1835 if (!acpi_evalf(ec_handle,
1836 &status, "GLED", "dd", 1 << i))
1837 return -EIO;
1838 len += sprintf(p + len, "%d:\t\t%s\n",
1839 i, led_status(status));
1840 }
1841 }
1842
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 len += sprintf(p + len, "commands:\t"
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001844 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845
1846 return len;
1847}
1848
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001849/* off, on, blink */
1850static const int led_sled_arg1[] = { 0, 1, 3 };
1851static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
1852static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
1853static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
1854
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001855static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856{
1857 char *cmd;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001858 int led, ind, ret;
1859
1860 if (!led_supported)
1861 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
1863 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001864 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 return -EINVAL;
1866
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001867 if (strstr(cmd, "off")) {
1868 ind = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 } else if (strstr(cmd, "on")) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001870 ind = 1;
1871 } else if (strstr(cmd, "blink")) {
1872 ind = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 } else
1874 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001875
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001876 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001877 /* 570 */
1878 led = 1 << led;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001880 led, led_sled_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 return -EIO;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001882 } else if (led_supported == TPACPI_LED_OLD) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001883 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
1884 led = 1 << led;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001885 ret = ec_write(TPACPI_LED_EC_HLMS, led);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001886 if (ret >= 0)
1887 ret =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001888 ec_write(TPACPI_LED_EC_HLBL,
Henrique de Moraes Holschuhe062e032007-03-23 17:33:55 -03001889 led * led_exp_hlbl[ind]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001890 if (ret >= 0)
1891 ret =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001892 ec_write(TPACPI_LED_EC_HLCL,
Henrique de Moraes Holschuhe062e032007-03-23 17:33:55 -03001893 led * led_exp_hlcl[ind]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001894 if (ret < 0)
1895 return ret;
1896 } else {
1897 /* all others */
1898 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
1899 led, led_led_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001901 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 }
1903
1904 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001905}
1906
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001907static struct ibm_struct led_driver_data = {
1908 .name = "led",
1909 .read = led_read,
1910 .write = led_write,
1911};
1912
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001913/*************************************************************************
1914 * Beep subdriver
1915 */
1916
1917IBM_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
1918
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001919static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001920{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001921 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
1922
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001923 IBM_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001924
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001925 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
1926 str_supported(beep_handle != NULL));
1927
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001928 return (beep_handle)? 0 : 1;
1929}
1930
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001931static int beep_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932{
1933 int len = 0;
1934
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001935 if (!beep_handle)
1936 len += sprintf(p + len, "status:\t\tnot supported\n");
1937 else {
1938 len += sprintf(p + len, "status:\t\tsupported\n");
1939 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
1940 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941
1942 return len;
1943}
1944
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001945static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946{
1947 char *cmd;
1948 int beep_cmd;
1949
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001950 if (!beep_handle)
1951 return -ENODEV;
1952
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001954 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
1955 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 /* beep_cmd set */
1957 } else
1958 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001959 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 return -EIO;
1961 }
1962
1963 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001964}
1965
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001966static struct ibm_struct beep_driver_data = {
1967 .name = "beep",
1968 .read = beep_read,
1969 .write = beep_write,
1970};
1971
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001972/*************************************************************************
1973 * Thermal subdriver
1974 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001975
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001976static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001977
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001978static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001979{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001980 u8 t, ta1, ta2;
1981 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001982 int acpi_tmp7;
1983
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001984 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
1985
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001986 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001987
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001988 if (ibm_thinkpad_ec_found && experimental) {
1989 /*
1990 * Direct EC access mode: sensors at registers
1991 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
1992 * non-implemented, thermal sensors return 0x80 when
1993 * not available
1994 */
1995
1996 ta1 = ta2 = 0;
1997 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03001998 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001999 ta1 |= t;
2000 } else {
2001 ta1 = 0;
2002 break;
2003 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002004 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002005 ta2 |= t;
2006 } else {
2007 ta1 = 0;
2008 break;
2009 }
2010 }
2011 if (ta1 == 0) {
2012 /* This is sheer paranoia, but we handle it anyway */
2013 if (acpi_tmp7) {
2014 printk(IBM_ERR
2015 "ThinkPad ACPI EC access misbehaving, "
2016 "falling back to ACPI TMPx access mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002017 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002018 } else {
2019 printk(IBM_ERR
2020 "ThinkPad ACPI EC access misbehaving, "
2021 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002022 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002023 }
2024 } else {
2025 thermal_read_mode =
2026 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002027 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002028 }
2029 } else if (acpi_tmp7) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002030 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
2031 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002032 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002033 } else {
2034 /* Standard ACPI TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002035 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002036 }
2037 } else {
2038 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002039 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002040 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002041
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002042 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
2043 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
2044 thermal_read_mode);
2045
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002046 return (thermal_read_mode != TPACPI_THERMAL_NONE)? 0 : 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002047}
2048
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002049/* idx is zero-based */
2050static int thermal_get_sensor(int idx, s32 *value)
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002051{
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002052 int t;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002053 s8 tmp;
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002054 char tmpi[5];
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002055
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002056 t = TP_EC_THERMAL_TMP0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002057
2058 switch (thermal_read_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002059#if TPACPI_MAX_THERMAL_SENSORS >= 16
2060 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002061 if (idx >= 8 && idx <= 15) {
2062 t = TP_EC_THERMAL_TMP8;
2063 idx -= 8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002064 }
2065 /* fallthrough */
2066#endif
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002067 case TPACPI_THERMAL_TPEC_8:
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002068 if (idx <= 7) {
2069 if (!acpi_ec_read(t + idx, &tmp))
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002070 return -EIO;
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002071 *value = tmp * 1000;
2072 return 0;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002073 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002074 break;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002075
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002076 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002077 if (idx <= 7) {
2078 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
2079 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
2080 return -EIO;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002081 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
2082 return -EIO;
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002083 *value = (t - 2732) * 100;
2084 return 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002085 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002086 break;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002087
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002088 case TPACPI_THERMAL_ACPI_TMP07:
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002089 if (idx <= 7) {
2090 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002091 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
2092 return -EIO;
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002093 *value = t * 1000;
2094 return 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002095 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002096 break;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002097
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002098 case TPACPI_THERMAL_NONE:
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002099 default:
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002100 return -ENOSYS;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002101 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002102
2103 return -EINVAL;
2104}
2105
2106static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
2107{
2108 int res, i;
2109 int n;
2110
2111 n = 8;
2112 i = 0;
2113
2114 if (!s)
2115 return -EINVAL;
2116
2117 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
2118 n = 16;
2119
2120 for(i = 0 ; i < n; i++) {
2121 res = thermal_get_sensor(i, &s->temp[i]);
2122 if (res)
2123 return res;
2124 }
2125
2126 return n;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002127}
2128
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002129static int thermal_read(char *p)
2130{
2131 int len = 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002132 int n, i;
2133 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002134
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002135 n = thermal_get_sensors(&t);
2136 if (unlikely(n < 0))
2137 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002138
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002139 len += sprintf(p + len, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002140
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002141 if (n > 0) {
2142 for (i = 0; i < (n - 1); i++)
2143 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
2144 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
2145 } else
2146 len += sprintf(p + len, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002147
2148 return len;
2149}
2150
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002151static struct ibm_struct thermal_driver_data = {
2152 .name = "thermal",
2153 .read = thermal_read,
2154};
2155
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002156/*************************************************************************
2157 * EC Dump subdriver
2158 */
2159
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002160static u8 ecdump_regs[256];
2161
2162static int ecdump_read(char *p)
2163{
2164 int len = 0;
2165 int i, j;
2166 u8 v;
2167
2168 len += sprintf(p + len, "EC "
2169 " +00 +01 +02 +03 +04 +05 +06 +07"
2170 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
2171 for (i = 0; i < 256; i += 16) {
2172 len += sprintf(p + len, "EC 0x%02x:", i);
2173 for (j = 0; j < 16; j++) {
2174 if (!acpi_ec_read(i + j, &v))
2175 break;
2176 if (v != ecdump_regs[i + j])
2177 len += sprintf(p + len, " *%02x", v);
2178 else
2179 len += sprintf(p + len, " %02x", v);
2180 ecdump_regs[i + j] = v;
2181 }
2182 len += sprintf(p + len, "\n");
2183 if (j != 16)
2184 break;
2185 }
2186
2187 /* These are way too dangerous to advertise openly... */
2188#if 0
2189 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
2190 " (<offset> is 00-ff, <value> is 00-ff)\n");
2191 len += sprintf(p + len, "commands:\t0x<offset> <value> "
2192 " (<offset> is 00-ff, <value> is 0-255)\n");
2193#endif
2194 return len;
2195}
2196
2197static int ecdump_write(char *buf)
2198{
2199 char *cmd;
2200 int i, v;
2201
2202 while ((cmd = next_cmd(&buf))) {
2203 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
2204 /* i and v set */
2205 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
2206 /* i and v set */
2207 } else
2208 return -EINVAL;
2209 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
2210 if (!acpi_ec_write(i, v))
2211 return -EIO;
2212 } else
2213 return -EINVAL;
2214 }
2215
2216 return 0;
2217}
2218
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002219static struct ibm_struct ecdump_driver_data = {
2220 .name = "ecdump",
2221 .read = ecdump_read,
2222 .write = ecdump_write,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03002223 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002224};
2225
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002226/*************************************************************************
2227 * Backlight/brightness subdriver
2228 */
Holger Macht8acb0252006-10-20 14:30:28 -07002229
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002230static struct backlight_device *ibm_backlight_device = NULL;
Holger Macht8acb0252006-10-20 14:30:28 -07002231
Richard Purdie599a52d2007-02-10 23:07:48 +00002232static struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02002233 .get_brightness = brightness_get,
2234 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02002235};
2236
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002237static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02002238{
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02002239 int b;
2240
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002241 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
2242
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02002243 b = brightness_get(NULL);
2244 if (b < 0)
2245 return b;
2246
Yu Luming519ab5f2006-12-19 12:56:15 -08002247 ibm_backlight_device = backlight_device_register("ibm", NULL, NULL,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02002248 &ibm_backlight_data);
2249 if (IS_ERR(ibm_backlight_device)) {
2250 printk(IBM_ERR "Could not register backlight device\n");
2251 return PTR_ERR(ibm_backlight_device);
2252 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002253 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02002254
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02002255 ibm_backlight_device->props.max_brightness = 7;
2256 ibm_backlight_device->props.brightness = b;
2257 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00002258
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02002259 return 0;
2260}
2261
2262static void brightness_exit(void)
2263{
2264 if (ibm_backlight_device) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002265 vdbg_printk(TPACPI_DBG_EXIT,
2266 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02002267 backlight_device_unregister(ibm_backlight_device);
2268 ibm_backlight_device = NULL;
2269 }
2270}
2271
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002272static int brightness_update_status(struct backlight_device *bd)
2273{
2274 return brightness_set(
2275 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
2276 bd->props.power == FB_BLANK_UNBLANK) ?
2277 bd->props.brightness : 0);
2278}
2279
2280static int brightness_get(struct backlight_device *bd)
2281{
2282 u8 level;
2283 if (!acpi_ec_read(brightness_offset, &level))
2284 return -EIO;
2285
2286 level &= 0x7;
2287
2288 return level;
2289}
2290
2291static int brightness_set(int value)
2292{
2293 int cmos_cmd, inc, i;
2294 int current_value = brightness_get(NULL);
2295
2296 value &= 7;
2297
2298 cmos_cmd = value > current_value ? TP_CMOS_BRIGHTNESS_UP : TP_CMOS_BRIGHTNESS_DOWN;
2299 inc = value > current_value ? 1 : -1;
2300 for (i = current_value; i != value; i += inc) {
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03002301 if (issue_thinkpad_cmos_command(cmos_cmd))
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002302 return -EIO;
2303 if (!acpi_ec_write(brightness_offset, i + inc))
2304 return -EIO;
2305 }
2306
2307 return 0;
2308}
2309
2310static int brightness_read(char *p)
2311{
2312 int len = 0;
2313 int level;
2314
2315 if ((level = brightness_get(NULL)) < 0) {
2316 len += sprintf(p + len, "level:\t\tunreadable\n");
2317 } else {
2318 len += sprintf(p + len, "level:\t\t%d\n", level & 0x7);
2319 len += sprintf(p + len, "commands:\tup, down\n");
2320 len += sprintf(p + len, "commands:\tlevel <level>"
2321 " (<level> is 0-7)\n");
2322 }
2323
2324 return len;
2325}
2326
2327static int brightness_write(char *buf)
2328{
2329 int level;
2330 int new_level;
2331 char *cmd;
2332
2333 while ((cmd = next_cmd(&buf))) {
2334 if ((level = brightness_get(NULL)) < 0)
2335 return level;
2336 level &= 7;
2337
2338 if (strlencmp(cmd, "up") == 0) {
2339 new_level = level == 7 ? 7 : level + 1;
2340 } else if (strlencmp(cmd, "down") == 0) {
2341 new_level = level == 0 ? 0 : level - 1;
2342 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
2343 new_level >= 0 && new_level <= 7) {
2344 /* new_level set */
2345 } else
2346 return -EINVAL;
2347
2348 brightness_set(new_level);
2349 }
2350
2351 return 0;
2352}
2353
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002354static struct ibm_struct brightness_driver_data = {
2355 .name = "brightness",
2356 .read = brightness_read,
2357 .write = brightness_write,
2358 .exit = brightness_exit,
2359};
2360
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002361/*************************************************************************
2362 * Volume subdriver
2363 */
2364
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002365static int volume_read(char *p)
2366{
2367 int len = 0;
2368 u8 level;
2369
2370 if (!acpi_ec_read(volume_offset, &level)) {
2371 len += sprintf(p + len, "level:\t\tunreadable\n");
2372 } else {
2373 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
2374 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
2375 len += sprintf(p + len, "commands:\tup, down, mute\n");
2376 len += sprintf(p + len, "commands:\tlevel <level>"
2377 " (<level> is 0-15)\n");
2378 }
2379
2380 return len;
2381}
2382
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002383static int volume_write(char *buf)
2384{
2385 int cmos_cmd, inc, i;
2386 u8 level, mute;
2387 int new_level, new_mute;
2388 char *cmd;
2389
2390 while ((cmd = next_cmd(&buf))) {
2391 if (!acpi_ec_read(volume_offset, &level))
2392 return -EIO;
2393 new_mute = mute = level & 0x40;
2394 new_level = level = level & 0xf;
2395
2396 if (strlencmp(cmd, "up") == 0) {
2397 if (mute)
2398 new_mute = 0;
2399 else
2400 new_level = level == 15 ? 15 : level + 1;
2401 } else if (strlencmp(cmd, "down") == 0) {
2402 if (mute)
2403 new_mute = 0;
2404 else
2405 new_level = level == 0 ? 0 : level - 1;
2406 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
2407 new_level >= 0 && new_level <= 15) {
2408 /* new_level set */
2409 } else if (strlencmp(cmd, "mute") == 0) {
2410 new_mute = 0x40;
2411 } else
2412 return -EINVAL;
2413
2414 if (new_level != level) { /* mute doesn't change */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002415 cmos_cmd = new_level > level ? TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002416 inc = new_level > level ? 1 : -1;
2417
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03002418 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002419 !acpi_ec_write(volume_offset, level)))
2420 return -EIO;
2421
2422 for (i = level; i != new_level; i += inc)
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03002423 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002424 !acpi_ec_write(volume_offset, i + inc))
2425 return -EIO;
2426
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03002427 if (mute && (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002428 !acpi_ec_write(volume_offset,
2429 new_level + mute)))
2430 return -EIO;
2431 }
2432
2433 if (new_mute != mute) { /* level doesn't change */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002434 cmos_cmd = new_mute ? TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002435
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03002436 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002437 !acpi_ec_write(volume_offset, level + new_mute))
2438 return -EIO;
2439 }
2440 }
2441
2442 return 0;
2443}
2444
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002445static struct ibm_struct volume_driver_data = {
2446 .name = "volume",
2447 .read = volume_read,
2448 .write = volume_write,
2449};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002450
2451/*************************************************************************
2452 * Fan subdriver
2453 */
2454
2455/*
2456 * FAN ACCESS MODES
2457 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002458 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002459 * ACPI GFAN method: returns fan level
2460 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002461 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03002462 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002463 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002464 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002465 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
2466 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03002467 * EC 0x2f (HFSP) might be available *for reading*, but do not use
2468 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002469 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002470 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03002471 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
2472 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002473 *
2474 * Fan speed changes of any sort (including those caused by the
2475 * disengaged mode) are usually done slowly by the firmware as the
2476 * maximum ammount of fan duty cycle change per second seems to be
2477 * limited.
2478 *
2479 * Reading is not available if GFAN exists.
2480 * Writing is not available if SFAN exists.
2481 *
2482 * Bits
2483 * 7 automatic mode engaged;
2484 * (default operation mode of the ThinkPad)
2485 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03002486 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002487 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03002488 * the tachometer while the fan controller ramps up
2489 * the speed (which can take up to a few *minutes*).
2490 * Speeds up fan to 100% duty-cycle, which is far above
2491 * the standard RPM levels. It is not impossible that
2492 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002493 * 5-3 unused in some models. Extra bits for fan level
2494 * in others, but still useless as all values above
2495 * 7 map to the same speed as level 7 in these models.
2496 * 2-0 fan level (0..7 usually)
2497 * 0x00 = stop
2498 * 0x07 = max (set when temperatures critical)
2499 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002500 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002501 *
2502 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
2503 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
2504 * does so, its initial value is meaningless (0x07).
2505 *
2506 * For firmware bugs, refer to:
2507 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
2508 *
2509 * ----
2510 *
2511 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
2512 * Main fan tachometer reading (in RPM)
2513 *
2514 * This register is present on all ThinkPads with a new-style EC, and
2515 * it is known not to be present on the A21m/e, and T22, as there is
2516 * something else in offset 0x84 according to the ACPI DSDT. Other
2517 * ThinkPads from this same time period (and earlier) probably lack the
2518 * tachometer as well.
2519 *
2520 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
2521 * was never fixed by IBM to report the EC firmware version string
2522 * probably support the tachometer (like the early X models), so
2523 * detecting it is quite hard. We need more data to know for sure.
2524 *
2525 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
2526 * might result.
2527 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03002528 * FIRMWARE BUG: may go stale while the EC is switching to full speed
2529 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002530 *
2531 * For firmware bugs, refer to:
2532 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
2533 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002534 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002535 * ThinkPad X31, X40, X41. Not available in the X60.
2536 *
2537 * FANS ACPI handle: takes three arguments: low speed, medium speed,
2538 * high speed. ACPI DSDT seems to map these three speeds to levels
2539 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
2540 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
2541 *
2542 * The speeds are stored on handles
2543 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
2544 *
2545 * There are three default speed sets, acessible as handles:
2546 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
2547 *
2548 * ACPI DSDT switches which set is in use depending on various
2549 * factors.
2550 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002551 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002552 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
2553 * but the ACPI tables just mention level 7.
2554 */
2555
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002556static enum fan_status_access_mode fan_status_access_mode;
2557static enum fan_control_access_mode fan_control_access_mode;
2558static enum fan_control_commands fan_control_commands;
2559
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002560static u8 fan_control_initial_status;
2561
Len Brown25c68a32006-12-08 04:43:41 -05002562static void fan_watchdog_fire(struct work_struct *ignored);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002563static int fan_watchdog_maxinterval;
Len Brown25c68a32006-12-08 04:43:41 -05002564static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002565
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002566IBM_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
2567IBM_HANDLE(gfan, ec, "GFAN", /* 570 */
2568 "\\FSPD", /* 600e/x, 770e, 770x */
2569 ); /* all others */
2570IBM_HANDLE(sfan, ec, "SFAN", /* 570 */
2571 "JFNS", /* 770x-JL */
2572 ); /* all others */
2573
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002574static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002575{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002576 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
2577
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002578 fan_status_access_mode = TPACPI_FAN_NONE;
2579 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002580 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002581 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002582 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002583
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002584 IBM_ACPIHANDLE_INIT(fans);
2585 IBM_ACPIHANDLE_INIT(gfan);
2586 IBM_ACPIHANDLE_INIT(sfan);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002587
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002588 if (gfan_handle) {
2589 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002590 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002591 } else {
2592 /* all other ThinkPads: note that even old-style
2593 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002594 if (likely(acpi_ec_read(fan_status_offset,
2595 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002596 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002597
2598 /* In some ThinkPads, neither the EC nor the ACPI
2599 * DSDT initialize the fan status, and it ends up
2600 * being set to 0x07 when it *could* be either
2601 * 0x07 or 0x80.
2602 *
2603 * Enable for TP-1Y (T43), TP-78 (R51e),
2604 * TP-76 (R52), TP-70 (T43, R52), which are known
2605 * to be buggy. */
2606 if (fan_control_initial_status == 0x07 &&
2607 ibm_thinkpad_ec_found &&
2608 ((ibm_thinkpad_ec_found[0] == '1' &&
2609 ibm_thinkpad_ec_found[1] == 'Y') ||
2610 (ibm_thinkpad_ec_found[0] == '7' &&
2611 (ibm_thinkpad_ec_found[1] == '6' ||
2612 ibm_thinkpad_ec_found[1] == '8' ||
2613 ibm_thinkpad_ec_found[1] == '0'))
2614 )) {
2615 printk(IBM_NOTICE
2616 "fan_init: initial fan status is "
2617 "unknown, assuming it is in auto "
2618 "mode\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002619 tp_features.fan_ctrl_status_undef = 1;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002620 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002621 } else {
2622 printk(IBM_ERR
2623 "ThinkPad ACPI EC access misbehaving, "
2624 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002625 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002626 }
2627 }
2628
2629 if (sfan_handle) {
2630 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002631 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002632 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002633 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002634 } else {
2635 if (!gfan_handle) {
2636 /* gfan without sfan means no fan control */
2637 /* all other models implement TP EC 0x2f control */
2638
2639 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02002640 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002641 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002642 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002643 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002644 TPACPI_FAN_CMD_SPEED |
2645 TPACPI_FAN_CMD_LEVEL |
2646 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002647 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002648 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02002649 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002650 TPACPI_FAN_CMD_LEVEL |
2651 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002652 }
2653 }
2654 }
2655
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002656 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
2657 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
2658 fan_control_access_mode != TPACPI_FAN_WR_NONE),
2659 fan_status_access_mode, fan_control_access_mode);
2660
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002661 return (fan_status_access_mode != TPACPI_FAN_NONE ||
2662 fan_control_access_mode != TPACPI_FAN_WR_NONE)?
2663 0 : 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002664}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002665
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02002666static int fan_get_status(u8 *status)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002667{
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02002668 u8 s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002669
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02002670 /* TODO:
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002671 * Add TPACPI_FAN_RD_ACPI_FANS ? */
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02002672
Henrique de Moraes Holschuh3ef8a602006-11-24 11:47:10 -02002673 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002674 case TPACPI_FAN_RD_ACPI_GFAN:
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002675 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02002676
2677 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002678 return -EIO;
2679
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02002680 if (likely(status))
2681 *status = s & 0x07;
2682
2683 break;
2684
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002685 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02002686 /* all except 570, 600e/x, 770e, 770x */
2687 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
2688 return -EIO;
2689
2690 if (likely(status))
2691 *status = s;
2692
2693 break;
2694
2695 default:
2696 return -ENXIO;
2697 }
2698
2699 return 0;
2700}
2701
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002702static void fan_exit(void)
2703{
Henrique de Moraes Holschuh99fba3f2007-04-21 11:08:44 -03002704 vdbg_printk(TPACPI_DBG_EXIT, "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002705 cancel_delayed_work(&fan_watchdog_task);
2706 flush_scheduled_work();
2707}
2708
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02002709static int fan_get_speed(unsigned int *speed)
2710{
2711 u8 hi, lo;
2712
2713 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002714 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02002715 /* all except 570, 600e/x, 770e, 770x */
2716 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
2717 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
2718 return -EIO;
2719
2720 if (likely(speed))
2721 *speed = (hi << 8) | lo;
2722
2723 break;
2724
2725 default:
2726 return -ENXIO;
2727 }
2728
2729 return 0;
2730}
2731
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002732static void fan_watchdog_fire(struct work_struct *ignored)
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002733{
Henrique de Moraes Holschuh99fba3f2007-04-21 11:08:44 -03002734 int rc;
2735
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002736 printk(IBM_NOTICE "fan watchdog: enabling fan\n");
Henrique de Moraes Holschuh99fba3f2007-04-21 11:08:44 -03002737 rc = fan_set_enable();
2738 if (rc < 0) {
2739 printk(IBM_ERR "fan watchdog: error %d while enabling fan, "
2740 "will try again later...\n", -rc);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002741 /* reschedule for later */
2742 fan_watchdog_reset();
2743 }
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002744}
2745
2746static void fan_watchdog_reset(void)
2747{
2748 static int fan_watchdog_active = 0;
2749
2750 if (fan_watchdog_active)
2751 cancel_delayed_work(&fan_watchdog_task);
2752
2753 if (fan_watchdog_maxinterval > 0) {
2754 fan_watchdog_active = 1;
2755 if (!schedule_delayed_work(&fan_watchdog_task,
2756 msecs_to_jiffies(fan_watchdog_maxinterval
2757 * 1000))) {
2758 printk(IBM_ERR "failed to schedule the fan watchdog, "
2759 "watchdog will not trigger\n");
2760 }
2761 } else
2762 fan_watchdog_active = 0;
2763}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002764
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002765static int fan_set_level(int level)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002766{
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002767 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002768 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002769 if (level >= 0 && level <= 7) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002770 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
2771 return -EIO;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002772 } else
2773 return -EINVAL;
2774 break;
2775
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002776 case TPACPI_FAN_WR_ACPI_FANS:
2777 case TPACPI_FAN_WR_TPEC:
2778 if ((level != TP_EC_FAN_AUTO) &&
2779 (level != TP_EC_FAN_FULLSPEED) &&
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02002780 ((level < 0) || (level > 7)))
2781 return -EINVAL;
2782
2783 if (!acpi_ec_write(fan_status_offset, level))
2784 return -EIO;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002785 else
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002786 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02002787 break;
2788
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002789 default:
2790 return -ENXIO;
2791 }
2792 return 0;
2793}
2794
2795static int fan_set_enable(void)
2796{
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002797 u8 s;
2798 int rc;
2799
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002800 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002801 case TPACPI_FAN_WR_ACPI_FANS:
2802 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002803 if ((rc = fan_get_status(&s)) < 0)
2804 return rc;
2805
2806 /* Don't go out of emergency fan mode */
2807 if (s != 7)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002808 s = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002809
2810 if (!acpi_ec_write(fan_status_offset, s))
2811 return -EIO;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002812 else
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002813 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002814 break;
2815
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002816 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002817 if ((rc = fan_get_status(&s)) < 0)
2818 return rc;
2819
2820 s &= 0x07;
2821
2822 /* Set fan to at least level 4 */
2823 if (s < 4)
2824 s = 4;
2825
2826 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002827 return -EIO;
2828 break;
2829
2830 default:
2831 return -ENXIO;
2832 }
2833 return 0;
2834}
2835
2836static int fan_set_disable(void)
2837{
2838 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002839 case TPACPI_FAN_WR_ACPI_FANS:
2840 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002841 if (!acpi_ec_write(fan_status_offset, 0x00))
2842 return -EIO;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002843 else
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002844 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002845 break;
2846
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002847 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002848 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
2849 return -EIO;
2850 break;
2851
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002852 default:
2853 return -ENXIO;
2854 }
2855 return 0;
2856}
2857
2858static int fan_set_speed(int speed)
2859{
2860 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002861 case TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002862 if (speed >= 0 && speed <= 65535) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002863 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
2864 speed, speed, speed))
2865 return -EIO;
2866 } else
2867 return -EINVAL;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002868 break;
2869
2870 default:
2871 return -ENXIO;
2872 }
2873 return 0;
2874}
2875
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002876static int fan_read(char *p)
2877{
2878 int len = 0;
2879 int rc;
2880 u8 status;
2881 unsigned int speed = 0;
2882
2883 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002884 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002885 /* 570, 600e/x, 770e, 770x */
2886 if ((rc = fan_get_status(&status)) < 0)
2887 return rc;
2888
2889 len += sprintf(p + len, "status:\t\t%s\n"
2890 "level:\t\t%d\n",
2891 (status != 0) ? "enabled" : "disabled", status);
2892 break;
2893
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002894 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002895 /* all except 570, 600e/x, 770e, 770x */
2896 if ((rc = fan_get_status(&status)) < 0)
2897 return rc;
2898
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002899 if (unlikely(tp_features.fan_ctrl_status_undef)) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002900 if (status != fan_control_initial_status)
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002901 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002902 else
2903 /* Return most likely status. In fact, it
2904 * might be the only possible status */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002905 status = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002906 }
2907
2908 len += sprintf(p + len, "status:\t\t%s\n",
2909 (status != 0) ? "enabled" : "disabled");
2910
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002911 if ((rc = fan_get_speed(&speed)) < 0)
2912 return rc;
2913
2914 len += sprintf(p + len, "speed:\t\t%d\n", speed);
2915
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002916 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002917 /* Disengaged mode takes precedence */
2918 len += sprintf(p + len, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002919 else if (status & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002920 len += sprintf(p + len, "level:\t\tauto\n");
2921 else
2922 len += sprintf(p + len, "level:\t\t%d\n", status);
2923 break;
2924
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002925 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002926 default:
2927 len += sprintf(p + len, "status:\t\tnot supported\n");
2928 }
2929
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002930 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002931 len += sprintf(p + len, "commands:\tlevel <level>");
2932
2933 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002934 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002935 len += sprintf(p + len, " (<level> is 0-7)\n");
2936 break;
2937
2938 default:
2939 len += sprintf(p + len, " (<level> is 0-7, "
2940 "auto, disengaged)\n");
2941 break;
2942 }
2943 }
2944
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002945 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002946 len += sprintf(p + len, "commands:\tenable, disable\n"
2947 "commands:\twatchdog <timeout> (<timeout> is 0 (off), "
2948 "1-120 (seconds))\n");
2949
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002950 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002951 len += sprintf(p + len, "commands:\tspeed <speed>"
2952 " (<speed> is 0-65535)\n");
2953
2954 return len;
2955}
2956
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002957static int fan_write_cmd_level(const char *cmd, int *rc)
2958{
2959 int level;
2960
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02002961 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002962 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02002963 else if (strlencmp(cmd, "level disengaged") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002964 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02002965 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002966 return 0;
2967
2968 if ((*rc = fan_set_level(level)) == -ENXIO)
2969 printk(IBM_ERR "level command accepted for unsupported "
2970 "access mode %d", fan_control_access_mode);
2971
2972 return 1;
2973}
2974
2975static int fan_write_cmd_enable(const char *cmd, int *rc)
2976{
2977 if (strlencmp(cmd, "enable") != 0)
2978 return 0;
2979
2980 if ((*rc = fan_set_enable()) == -ENXIO)
2981 printk(IBM_ERR "enable command accepted for unsupported "
2982 "access mode %d", fan_control_access_mode);
2983
2984 return 1;
2985}
2986
2987static int fan_write_cmd_disable(const char *cmd, int *rc)
2988{
2989 if (strlencmp(cmd, "disable") != 0)
2990 return 0;
2991
2992 if ((*rc = fan_set_disable()) == -ENXIO)
2993 printk(IBM_ERR "disable command accepted for unsupported "
2994 "access mode %d", fan_control_access_mode);
2995
2996 return 1;
2997}
2998
2999static int fan_write_cmd_speed(const char *cmd, int *rc)
3000{
3001 int speed;
3002
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02003003 /* TODO:
3004 * Support speed <low> <medium> <high> ? */
3005
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003006 if (sscanf(cmd, "speed %d", &speed) != 1)
3007 return 0;
3008
3009 if ((*rc = fan_set_speed(speed)) == -ENXIO)
3010 printk(IBM_ERR "speed command accepted for unsupported "
3011 "access mode %d", fan_control_access_mode);
3012
3013 return 1;
3014}
3015
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003016static int fan_write_cmd_watchdog(const char *cmd, int *rc)
3017{
3018 int interval;
3019
3020 if (sscanf(cmd, "watchdog %d", &interval) != 1)
3021 return 0;
3022
3023 if (interval < 0 || interval > 120)
3024 *rc = -EINVAL;
3025 else
3026 fan_watchdog_maxinterval = interval;
3027
3028 return 1;
3029}
3030
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003031static int fan_write(char *buf)
3032{
3033 char *cmd;
3034 int rc = 0;
3035
3036 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003037 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003038 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003039 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003040 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003041 fan_write_cmd_disable(cmd, &rc) ||
3042 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003043 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003044 fan_write_cmd_speed(cmd, &rc))
3045 )
3046 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003047 else if (!rc)
3048 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003049 }
3050
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003051 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003052}
3053
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003054static struct ibm_struct fan_driver_data = {
3055 .name = "fan",
3056 .read = fan_read,
3057 .write = fan_write,
3058 .exit = fan_exit,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03003059 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003060};
3061
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003062/****************************************************************************
3063 ****************************************************************************
3064 *
3065 * Infrastructure
3066 *
3067 ****************************************************************************
3068 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003070/* /proc support */
3071static struct proc_dir_entry *proc_dir = NULL;
3072
3073/* Subdriver registry */
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003074static LIST_HEAD(tpacpi_all_drivers);
3075
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003077/*
3078 * Module and infrastructure proble, init and exit handling
3079 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003081#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003082static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003083{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003084 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003085
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003086 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003087}
3088#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
3089
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003090static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091{
3092 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003093 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 struct proc_dir_entry *entry;
3095
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003096 BUG_ON(ibm == NULL);
3097
3098 INIT_LIST_HEAD(&ibm->all_drivers);
3099
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03003100 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 return 0;
3102
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003103 dbg_printk(TPACPI_DBG_INIT,
3104 "probing for %s\n", ibm->name);
3105
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003106 if (iibm->init) {
3107 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003108 if (ret > 0)
3109 return 0; /* probe failed */
3110 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003112
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03003113 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 }
3115
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003116 if (ibm->acpi) {
3117 if (ibm->acpi->hid) {
3118 ret = register_tpacpi_subdriver(ibm);
3119 if (ret)
3120 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003121 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003122
3123 if (ibm->acpi->notify) {
3124 ret = setup_acpi_notify(ibm);
3125 if (ret == -ENODEV) {
3126 printk(IBM_NOTICE "disabling subdriver %s\n",
3127 ibm->name);
3128 ret = 0;
3129 goto err_out;
3130 }
3131 if (ret < 0)
3132 goto err_out;
3133 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003134 }
3135
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003136 dbg_printk(TPACPI_DBG_INIT,
3137 "%s installed\n", ibm->name);
3138
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003139 if (ibm->read) {
3140 entry = create_proc_entry(ibm->name,
3141 S_IFREG | S_IRUGO | S_IWUSR,
3142 proc_dir);
3143 if (!entry) {
3144 printk(IBM_ERR "unable to create proc entry %s\n",
3145 ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003146 ret = -ENODEV;
3147 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003148 }
3149 entry->owner = THIS_MODULE;
3150 entry->data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003151 entry->read_proc = &dispatch_procfs_read;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003152 if (ibm->write)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003153 entry->write_proc = &dispatch_procfs_write;
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03003154 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003155 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003157 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
3158
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003160
3161err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003162 dbg_printk(TPACPI_DBG_INIT,
3163 "%s: at error exit path with result %d\n",
3164 ibm->name, ret);
3165
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003166 ibm_exit(ibm);
3167 return (ret < 0)? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168}
3169
3170static void ibm_exit(struct ibm_struct *ibm)
3171{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003172 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003173
3174 list_del_init(&ibm->all_drivers);
3175
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003176 if (ibm->flags.acpi_notify_installed) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003177 dbg_printk(TPACPI_DBG_EXIT,
3178 "%s: acpi_remove_notify_handler\n", ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003179 BUG_ON(!ibm->acpi);
3180 acpi_remove_notify_handler(*ibm->acpi->handle,
3181 ibm->acpi->type,
3182 dispatch_acpi_notify);
3183 ibm->flags.acpi_notify_installed = 0;
3184 ibm->flags.acpi_notify_installed = 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003185 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03003187 if (ibm->flags.proc_created) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003188 dbg_printk(TPACPI_DBG_EXIT,
3189 "%s: remove_proc_entry\n", ibm->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190 remove_proc_entry(ibm->name, proc_dir);
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03003191 ibm->flags.proc_created = 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003192 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003194 if (ibm->flags.acpi_driver_registered) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003195 dbg_printk(TPACPI_DBG_EXIT,
3196 "%s: acpi_bus_unregister_driver\n", ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003197 BUG_ON(!ibm->acpi);
3198 acpi_bus_unregister_driver(ibm->acpi->driver);
3199 kfree(ibm->acpi->driver);
3200 ibm->acpi->driver = NULL;
3201 ibm->flags.acpi_driver_registered = 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003202 }
3203
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03003204 if (ibm->flags.init_called && ibm->exit) {
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003205 ibm->exit();
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03003206 ibm->flags.init_called = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003208
3209 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210}
3211
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003212/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003214static char *ibm_thinkpad_ec_found = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02003216static char* __init check_dmi_for_ec(void)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003217{
3218 struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02003219 char ec_fw_string[18];
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003220
3221 /*
3222 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
3223 * X32 or newer, all Z series; Some models must have an
3224 * up-to-date BIOS or they will not be detected.
3225 *
3226 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
3227 */
3228 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02003229 if (sscanf(dev->name,
3230 "IBM ThinkPad Embedded Controller -[%17c",
3231 ec_fw_string) == 1) {
3232 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
3233 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
3234 return kstrdup(ec_fw_string, GFP_KERNEL);
3235 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003236 }
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02003237 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238}
3239
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003240static int __init probe_for_thinkpad(void)
3241{
3242 int is_thinkpad;
3243
3244 if (acpi_disabled)
3245 return -ENODEV;
3246
3247 /*
3248 * Non-ancient models have better DMI tagging, but very old models
3249 * don't.
3250 */
3251 is_thinkpad = dmi_name_in_vendors("ThinkPad");
3252
3253 /* ec is required because many other handles are relative to it */
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003254 IBM_ACPIHANDLE_INIT(ec);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003255 if (!ec_handle) {
3256 if (is_thinkpad)
3257 printk(IBM_ERR
3258 "Not yet supported ThinkPad detected!\n");
3259 return -ENODEV;
3260 }
3261
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03003262 /*
3263 * Risks a regression on very old machines, but reduces potential
3264 * false positives a damn great deal
3265 */
3266 if (!is_thinkpad)
3267 is_thinkpad = dmi_name_in_vendors("IBM");
3268
3269 if (!is_thinkpad && !force_load)
3270 return -ENODEV;
3271
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003272 return 0;
3273}
3274
3275
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003276/* Module init, exit, parameters */
3277
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003278static struct ibm_init_struct ibms_init[] __initdata = {
3279 {
3280 .init = thinkpad_acpi_driver_init,
3281 .data = &thinkpad_acpi_driver_data,
3282 },
3283 {
3284 .init = hotkey_init,
3285 .data = &hotkey_driver_data,
3286 },
3287 {
3288 .init = bluetooth_init,
3289 .data = &bluetooth_driver_data,
3290 },
3291 {
3292 .init = wan_init,
3293 .data = &wan_driver_data,
3294 },
3295 {
3296 .init = video_init,
3297 .data = &video_driver_data,
3298 },
3299 {
3300 .init = light_init,
3301 .data = &light_driver_data,
3302 },
3303#ifdef CONFIG_THINKPAD_ACPI_DOCK
3304 {
3305 .init = dock_init,
3306 .data = &dock_driver_data[0],
3307 },
3308 {
3309 .data = &dock_driver_data[1],
3310 },
3311#endif
3312#ifdef CONFIG_THINKPAD_ACPI_BAY
3313 {
3314 .init = bay_init,
3315 .data = &bay_driver_data,
3316 },
3317#endif
3318 {
3319 .init = cmos_init,
3320 .data = &cmos_driver_data,
3321 },
3322 {
3323 .init = led_init,
3324 .data = &led_driver_data,
3325 },
3326 {
3327 .init = beep_init,
3328 .data = &beep_driver_data,
3329 },
3330 {
3331 .init = thermal_init,
3332 .data = &thermal_driver_data,
3333 },
3334 {
3335 .data = &ecdump_driver_data,
3336 },
3337 {
3338 .init = brightness_init,
3339 .data = &brightness_driver_data,
3340 },
3341 {
3342 .data = &volume_driver_data,
3343 },
3344 {
3345 .init = fan_init,
3346 .data = &fan_driver_data,
3347 },
3348};
3349
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003350static int __init set_ibm_param(const char *val, struct kernel_param *kp)
3351{
3352 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003353 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003354
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003355 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
3356 ibm = ibms_init[i].data;
3357 BUG_ON(ibm == NULL);
3358
3359 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
3360 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003361 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003362 strcpy(ibms_init[i].param, val);
3363 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003364 return 0;
3365 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003366 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003367
3368 return -EINVAL;
3369}
3370
3371static int experimental;
3372module_param(experimental, int, 0);
3373
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03003374static u32 dbg_level;
3375module_param_named(debug, dbg_level, uint, 0);
3376
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03003377static int force_load;
3378module_param(force_load, int, 0);
3379
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003380#define IBM_PARAM(feature) \
3381 module_param_call(feature, set_ibm_param, NULL, NULL, 0)
3382
3383IBM_PARAM(hotkey);
3384IBM_PARAM(bluetooth);
3385IBM_PARAM(video);
3386IBM_PARAM(light);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003387#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003388IBM_PARAM(dock);
3389#endif
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003390#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003391IBM_PARAM(bay);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003392#endif /* CONFIG_THINKPAD_ACPI_BAY */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003393IBM_PARAM(cmos);
3394IBM_PARAM(led);
3395IBM_PARAM(beep);
3396IBM_PARAM(ecdump);
3397IBM_PARAM(brightness);
3398IBM_PARAM(volume);
3399IBM_PARAM(fan);
3400
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03003401static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402{
3403 int ret, i;
3404
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03003405 /* Driver-level probe */
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003406 ret = probe_for_thinkpad();
3407 if (ret)
3408 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03003410 /* Driver initialization */
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003411 ibm_thinkpad_ec_found = check_dmi_for_ec();
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003412 IBM_ACPIHANDLE_INIT(ecrd);
3413 IBM_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003415 proc_dir = proc_mkdir(IBM_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 if (!proc_dir) {
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003417 printk(IBM_ERR "unable to create proc dir " IBM_PROC_DIR);
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03003418 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 return -ENODEV;
3420 }
3421 proc_dir->owner = THIS_MODULE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003422
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03003423 ret = platform_driver_register(&tpacpi_pdriver);
3424 if (ret) {
3425 printk(IBM_ERR "unable to register platform driver\n");
3426 thinkpad_acpi_module_exit();
3427 return ret;
3428 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03003429 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
3430 if (ret) {
3431 printk(IBM_ERR "unable to create sysfs driver attributes\n");
3432 thinkpad_acpi_module_exit();
3433 return ret;
3434 }
3435
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03003436
3437 /* Device initialization */
3438 tpacpi_pdev = platform_device_register_simple(IBM_DRVR_NAME, -1,
3439 NULL, 0);
3440 if (IS_ERR(tpacpi_pdev)) {
3441 ret = PTR_ERR(tpacpi_pdev);
3442 tpacpi_pdev = NULL;
3443 printk(IBM_ERR "unable to register platform device\n");
3444 thinkpad_acpi_module_exit();
3445 return ret;
3446 }
3447 tpacpi_hwmon = hwmon_device_register(&tpacpi_pdev->dev);
3448 if (IS_ERR(tpacpi_hwmon)) {
3449 ret = PTR_ERR(tpacpi_hwmon);
3450 tpacpi_hwmon = NULL;
3451 printk(IBM_ERR "unable to register hwmon device\n");
3452 thinkpad_acpi_module_exit();
3453 return ret;
3454 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003455 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
3456 ret = ibm_init(&ibms_init[i]);
3457 if (ret >= 0 && *ibms_init[i].param)
3458 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03003460 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461 return ret;
3462 }
3463 }
3464
3465 return 0;
3466}
3467
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03003468static void thinkpad_acpi_module_exit(void)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003469{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003470 struct ibm_struct *ibm, *itmp;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003471
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003472 list_for_each_entry_safe_reverse(ibm, itmp,
3473 &tpacpi_all_drivers,
3474 all_drivers) {
3475 ibm_exit(ibm);
3476 }
3477
3478 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003479
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03003480 if (tpacpi_hwmon)
3481 hwmon_device_unregister(tpacpi_hwmon);
3482
3483 if (tpacpi_pdev)
3484 platform_device_unregister(tpacpi_pdev);
3485
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03003486 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03003487 platform_driver_unregister(&tpacpi_pdriver);
3488
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003489 if (proc_dir)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003490 remove_proc_entry(IBM_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003491
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003492 kfree(ibm_thinkpad_ec_found);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003493}
3494
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03003495module_init(thinkpad_acpi_module_init);
3496module_exit(thinkpad_acpi_module_exit);