blob: 79abc6841e3033666455531c85c29806c97160bc [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 Holschuh40ca9fd2007-04-24 11:48:15 -0300707 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300708
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300709 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300710 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300711
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300712 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300713 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300714
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300715 if (tp_features.hotkey) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300716 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
717 A30, R30, R31, T20-22, X20-21, X22-24 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300718 tp_features.hotkey_mask =
719 acpi_evalf(hkey_handle, NULL, "DHKN", "qv");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300720
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300721 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300722 str_supported(tp_features.hotkey_mask));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300723
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300724 res = hotkey_get(&hotkey_orig_status, &hotkey_orig_mask);
725 if (res)
726 return res;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300727 }
728
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300729 return (tp_features.hotkey)? 0 : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300730}
731
732static void hotkey_exit(void)
733{
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300734 int res;
735
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300736 if (tp_features.hotkey) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300737 dbg_printk(TPACPI_DBG_EXIT, "restoring original hotkey mask\n");
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300738 res = hotkey_set(hotkey_orig_status, hotkey_orig_mask);
739 if (res)
740 printk(IBM_ERR "failed to restore hotkey to BIOS defaults\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300741 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300742}
743
744static void hotkey_notify(struct ibm_struct *ibm, u32 event)
745{
746 int hkey;
747
748 if (acpi_evalf(hkey_handle, &hkey, "MHKP", "d"))
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300749 acpi_bus_generate_event(ibm->acpi->device, event, hkey);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300750 else {
751 printk(IBM_ERR "unknown hotkey event %d\n", event);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300752 acpi_bus_generate_event(ibm->acpi->device, event, 0);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300753 }
754}
755
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -0300756/*
757 * Call with hotkey_mutex held
758 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400759static int hotkey_get(int *status, int *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760{
761 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300762 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400763
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300764 if (tp_features.hotkey_mask)
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400765 if (!acpi_evalf(hkey_handle, mask, "DHKN", "d"))
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300766 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400767
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300768 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769}
770
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -0300771/*
772 * Call with hotkey_mutex held
773 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400774static int hotkey_set(int status, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
776 int i;
777
778 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300779 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300781 if (tp_features.hotkey_mask)
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400782 for (i = 0; i < 32; i++) {
783 int bit = ((1 << i) & mask) != 0;
784 if (!acpi_evalf(hkey_handle,
785 NULL, "MHKM", "vdd", i + 1, bit))
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300786 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400787 }
788
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300789 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400790}
791
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400792static int hotkey_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793{
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300794 int res, status, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 int len = 0;
796
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300797 if (!tp_features.hotkey) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400798 len += sprintf(p + len, "status:\t\tnot supported\n");
799 return len;
800 }
801
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -0300802 res = mutex_lock_interruptible(&hotkey_mutex);
803 if (res < 0)
804 return res;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300805 res = hotkey_get(&status, &mask);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -0300806 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300807 if (res)
808 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
810 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300811 if (tp_features.hotkey_mask) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 len += sprintf(p + len, "mask:\t\t0x%04x\n", mask);
813 len += sprintf(p + len,
814 "commands:\tenable, disable, reset, <mask>\n");
815 } else {
816 len += sprintf(p + len, "mask:\t\tnot supported\n");
817 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
818 }
819
820 return len;
821}
822
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400823static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824{
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300825 int res, status, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 char *cmd;
827 int do_cmd = 0;
828
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300829 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 return -ENODEV;
831
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -0300832 res = mutex_lock_interruptible(&hotkey_mutex);
833 if (res < 0)
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300834 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400835
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -0300836 res = hotkey_get(&status, &mask);
837 if (res)
838 goto errexit;
839
840 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 while ((cmd = next_cmd(&buf))) {
842 if (strlencmp(cmd, "enable") == 0) {
843 status = 1;
844 } else if (strlencmp(cmd, "disable") == 0) {
845 status = 0;
846 } else if (strlencmp(cmd, "reset") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400847 status = hotkey_orig_status;
848 mask = hotkey_orig_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
850 /* mask set */
851 } else if (sscanf(cmd, "%x", &mask) == 1) {
852 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -0300853 } else {
854 res = -EINVAL;
855 goto errexit;
856 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 do_cmd = 1;
858 }
859
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -0300860 if (do_cmd)
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300861 res = hotkey_set(status, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -0300863errexit:
864 mutex_unlock(&hotkey_mutex);
865 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400866}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300868static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300869 .hid = IBM_HKEY_HID,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300870 .notify = hotkey_notify,
871 .handle = &hkey_handle,
872 .type = ACPI_DEVICE_NOTIFY,
873};
874
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300875static struct ibm_struct hotkey_driver_data = {
876 .name = "hotkey",
877 .read = hotkey_read,
878 .write = hotkey_write,
879 .exit = hotkey_exit,
880 .acpi = &ibm_hotkey_acpidriver,
881};
882
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300883/*************************************************************************
884 * Bluetooth subdriver
885 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300887static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888{
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -0300889 int status = 0;
890
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300891 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
892
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300893 IBM_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300894
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400895 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
896 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300897 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -0300898 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -0300900 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
901 str_supported(tp_features.bluetooth),
902 status);
903
904 if (tp_features.bluetooth &&
905 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
906 /* no bluetooth hardware present in system */
907 tp_features.bluetooth = 0;
908 dbg_printk(TPACPI_DBG_INIT,
909 "bluetooth hardware not installed\n");
910 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300911
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300912 return (tp_features.bluetooth)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913}
914
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -0300915static int bluetooth_get_radiosw(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916{
917 int status;
918
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -0300919 if (!tp_features.bluetooth)
920 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -0300922 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
923 return -EIO;
924
925 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
926}
927
928static int bluetooth_set_radiosw(int radio_on)
929{
930 int status;
931
932 if (!tp_features.bluetooth)
933 return -ENODEV;
934
935 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
936 return -EIO;
937 if (radio_on)
938 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
939 else
940 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
941 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
942 return -EIO;
943
944 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945}
946
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400947static int bluetooth_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948{
949 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -0300950 int status = bluetooth_get_radiosw();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300952 if (!tp_features.bluetooth)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 len += sprintf(p + len, "status:\t\tnot supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -0300955 len += sprintf(p + len, "status:\t\t%s\n",
956 (status)? "enabled" : "disabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 len += sprintf(p + len, "commands:\tenable, disable\n");
958 }
959
960 return len;
961}
962
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400963static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300967 if (!tp_features.bluetooth)
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400968 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
970 while ((cmd = next_cmd(&buf))) {
971 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -0300972 bluetooth_set_radiosw(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -0300974 bluetooth_set_radiosw(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 } else
976 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 }
978
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 return 0;
980}
981
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300982static struct ibm_struct bluetooth_driver_data = {
983 .name = "bluetooth",
984 .read = bluetooth_read,
985 .write = bluetooth_write,
986};
987
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300988/*************************************************************************
989 * Wan subdriver
990 */
991
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300992static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -0400993{
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -0300994 int status = 0;
995
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300996 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
997
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300998 IBM_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300999
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001000 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001001 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001002
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001003 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
1004 str_supported(tp_features.wan),
1005 status);
1006
1007 if (tp_features.wan &&
1008 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
1009 /* no wan hardware present in system */
1010 tp_features.wan = 0;
1011 dbg_printk(TPACPI_DBG_INIT,
1012 "wan hardware not installed\n");
1013 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001014
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001015 return (tp_features.wan)? 0 : 1;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001016}
1017
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001018static int wan_get_radiosw(void)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001019{
1020 int status;
1021
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001022 if (!tp_features.wan)
1023 return -ENODEV;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001024
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001025 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
1026 return -EIO;
1027
1028 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
1029}
1030
1031static int wan_set_radiosw(int radio_on)
1032{
1033 int status;
1034
1035 if (!tp_features.wan)
1036 return -ENODEV;
1037
1038 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
1039 return -EIO;
1040 if (radio_on)
1041 status |= TP_ACPI_WANCARD_RADIOSSW;
1042 else
1043 status &= ~TP_ACPI_WANCARD_RADIOSSW;
1044 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
1045 return -EIO;
1046
1047 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001048}
1049
1050static int wan_read(char *p)
1051{
1052 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001053 int status = wan_get_radiosw();
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001054
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001055 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001056 len += sprintf(p + len, "status:\t\tnot supported\n");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001057 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001058 len += sprintf(p + len, "status:\t\t%s\n",
1059 (status)? "enabled" : "disabled");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001060 len += sprintf(p + len, "commands:\tenable, disable\n");
1061 }
1062
1063 return len;
1064}
1065
1066static int wan_write(char *buf)
1067{
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001068 char *cmd;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001069
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001070 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001071 return -ENODEV;
1072
1073 while ((cmd = next_cmd(&buf))) {
1074 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001075 wan_set_radiosw(1);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001076 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001077 wan_set_radiosw(0);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001078 } else
1079 return -EINVAL;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001080 }
1081
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001082 return 0;
1083}
1084
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001085static struct ibm_struct wan_driver_data = {
1086 .name = "wan",
1087 .read = wan_read,
1088 .write = wan_write,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03001089 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001090};
1091
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001092/*************************************************************************
1093 * Video subdriver
1094 */
1095
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02001096static enum video_access_mode video_supported;
1097static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001098
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001099IBM_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
1100 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
1101 "\\_SB.PCI0.VID0", /* 770e */
1102 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
1103 "\\_SB.PCI0.AGP.VID", /* all others */
1104 ); /* R30, R31 */
1105
1106IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
1107
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001108static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001110 int ivga;
1111
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001112 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
1113
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001114 IBM_ACPIHANDLE_INIT(vid);
1115 IBM_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001116
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001117 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
1118 /* G41, assume IVGA doesn't change */
1119 vid_handle = vid2_handle;
1120
1121 if (!vid_handle)
1122 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001123 video_supported = TPACPI_VIDEO_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001124 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
1125 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001126 video_supported = TPACPI_VIDEO_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001127 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
1128 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001129 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001130 else
1131 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001132 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001134 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
1135 str_supported(video_supported != TPACPI_VIDEO_NONE),
1136 video_supported);
1137
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001138 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139}
1140
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001141static void video_exit(void)
1142{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001143 dbg_printk(TPACPI_DBG_EXIT,
1144 "restoring original video autoswitch mode\n");
1145 if (video_autosw_set(video_orig_autosw))
1146 printk(IBM_ERR "error while trying to restore original "
1147 "video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001148}
1149
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001150static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151{
1152 int status = 0;
1153 int i;
1154
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001155 switch (video_supported) {
1156 case TPACPI_VIDEO_570:
1157 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
1158 TP_ACPI_VIDEO_570_PHSCMD))
1159 return -EIO;
1160 status = i & TP_ACPI_VIDEO_570_PHSMASK;
1161 break;
1162 case TPACPI_VIDEO_770:
1163 if (!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, "\\VCDC", "d"))
1168 return -EIO;
1169 if (i)
1170 status |= TP_ACPI_VIDEO_S_CRT;
1171 break;
1172 case TPACPI_VIDEO_NEW:
1173 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
1174 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
1175 return -EIO;
1176 if (i)
1177 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001179 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
1180 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
1181 return -EIO;
1182 if (i)
1183 status |= TP_ACPI_VIDEO_S_LCD;
1184 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
1185 return -EIO;
1186 if (i)
1187 status |= TP_ACPI_VIDEO_S_DVI;
1188 break;
1189 default:
1190 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001191 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
1193 return status;
1194}
1195
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001196static int video_outputsw_set(int status)
1197{
1198 int autosw;
1199 int res = 0;
1200
1201 switch (video_supported) {
1202 case TPACPI_VIDEO_570:
1203 res = acpi_evalf(NULL, NULL,
1204 "\\_SB.PHS2", "vdd",
1205 TP_ACPI_VIDEO_570_PHS2CMD,
1206 status | TP_ACPI_VIDEO_570_PHS2SET);
1207 break;
1208 case TPACPI_VIDEO_770:
1209 autosw = video_autosw_get();
1210 if (autosw < 0)
1211 return autosw;
1212
1213 res = video_autosw_set(1);
1214 if (res)
1215 return res;
1216 res = acpi_evalf(vid_handle, NULL,
1217 "ASWT", "vdd", status * 0x100, 0);
1218 if (!autosw && video_autosw_set(autosw)) {
1219 printk(IBM_ERR "video auto-switch left enabled due to error\n");
1220 return -EIO;
1221 }
1222 break;
1223 case TPACPI_VIDEO_NEW:
1224 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
1225 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
1226 break;
1227 default:
1228 return -ENOSYS;
1229 }
1230
1231 return (res)? 0 : -EIO;
1232}
1233
1234static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001236 int autosw = 0;
1237
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001238 switch (video_supported) {
1239 case TPACPI_VIDEO_570:
1240 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
1241 return -EIO;
1242 break;
1243 case TPACPI_VIDEO_770:
1244 case TPACPI_VIDEO_NEW:
1245 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
1246 return -EIO;
1247 break;
1248 default:
1249 return -ENOSYS;
1250 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001251
1252 return autosw & 1;
1253}
1254
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001255static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001256{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001257 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001258 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001259 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001260}
1261
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001262static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001263{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001264 int autosw = video_autosw_get();
1265 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001266
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001267 if (autosw < 0)
1268 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001269
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001270 switch (video_supported) {
1271 case TPACPI_VIDEO_570:
1272 res = video_autosw_set(1);
1273 if (res)
1274 return res;
1275 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
1276 break;
1277 case TPACPI_VIDEO_770:
1278 case TPACPI_VIDEO_NEW:
1279 res = video_autosw_set(1);
1280 if (res)
1281 return res;
1282 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
1283 break;
1284 default:
1285 return -ENOSYS;
1286 }
1287 if (!autosw && video_autosw_set(autosw)) {
1288 printk(IBM_ERR "video auto-switch left enabled due to error\n");
1289 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001290 }
1291
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001292 return (res)? 0 : -EIO;
1293}
1294
1295static int video_expand_toggle(void)
1296{
1297 switch (video_supported) {
1298 case TPACPI_VIDEO_570:
1299 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
1300 0 : -EIO;
1301 case TPACPI_VIDEO_770:
1302 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
1303 0 : -EIO;
1304 case TPACPI_VIDEO_NEW:
1305 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
1306 0 : -EIO;
1307 default:
1308 return -ENOSYS;
1309 }
1310 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001311}
1312
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001313static int video_read(char *p)
1314{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001315 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001316 int len = 0;
1317
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001318 if (video_supported == TPACPI_VIDEO_NONE) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001319 len += sprintf(p + len, "status:\t\tnot supported\n");
1320 return len;
1321 }
1322
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001323 status = video_outputsw_get();
1324 if (status < 0)
1325 return status;
1326
1327 autosw = video_autosw_get();
1328 if (autosw < 0)
1329 return autosw;
1330
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001331 len += sprintf(p + len, "status:\t\tsupported\n");
1332 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
1333 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001334 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001335 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
1336 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
1337 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
1338 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001339 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001340 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
1341 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
1342 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
1343
1344 return len;
1345}
1346
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001347static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348{
1349 char *cmd;
1350 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001351 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001353 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001354 return -ENODEV;
1355
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001356 enable = 0;
1357 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
1359 while ((cmd = next_cmd(&buf))) {
1360 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001361 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001363 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001365 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001367 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001368 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001369 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001370 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001371 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001372 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001373 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001375 res = video_autosw_set(1);
1376 if (res)
1377 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001379 res = video_autosw_set(0);
1380 if (res)
1381 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001383 res = video_outputsw_cycle();
1384 if (res)
1385 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001387 res = video_expand_toggle();
1388 if (res)
1389 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 } else
1391 return -EINVAL;
1392 }
1393
1394 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001395 status = video_outputsw_get();
1396 if (status < 0)
1397 return status;
1398 res = video_outputsw_set((status & ~disable) | enable);
1399 if (res)
1400 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 }
1402
1403 return 0;
1404}
1405
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001406static struct ibm_struct video_driver_data = {
1407 .name = "video",
1408 .read = video_read,
1409 .write = video_write,
1410 .exit = video_exit,
1411};
1412
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001413/*************************************************************************
1414 * Light (thinklight) subdriver
1415 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001417IBM_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
1418IBM_HANDLE(ledb, ec, "LEDB"); /* G4x */
1419
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001420static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001422 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
1423
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001424 IBM_ACPIHANDLE_INIT(ledb);
1425 IBM_ACPIHANDLE_INIT(lght);
1426 IBM_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001427
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001428 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001429 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001430
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001431 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001432 /* light status not supported on
1433 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001434 tp_features.light_status =
1435 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001437 vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001438 str_supported(tp_features.light));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001439
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001440 return (tp_features.light)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441}
1442
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001443static int light_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444{
1445 int len = 0;
1446 int status = 0;
1447
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001448 if (!tp_features.light) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001449 len += sprintf(p + len, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001450 } else if (!tp_features.light_status) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001451 len += sprintf(p + len, "status:\t\tunknown\n");
1452 len += sprintf(p + len, "commands:\ton, off\n");
1453 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
1455 return -EIO;
1456 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001457 len += sprintf(p + len, "commands:\ton, off\n");
1458 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
1460 return len;
1461}
1462
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001463static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464{
1465 int cmos_cmd, lght_cmd;
1466 char *cmd;
1467 int success;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001468
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001469 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001470 return -ENODEV;
1471
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 while ((cmd = next_cmd(&buf))) {
1473 if (strlencmp(cmd, "on") == 0) {
1474 cmos_cmd = 0x0c;
1475 lght_cmd = 1;
1476 } else if (strlencmp(cmd, "off") == 0) {
1477 cmos_cmd = 0x0d;
1478 lght_cmd = 0;
1479 } else
1480 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001481
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 success = cmos_handle ?
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001483 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
1484 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 if (!success)
1486 return -EIO;
1487 }
1488
1489 return 0;
1490}
1491
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001492static struct ibm_struct light_driver_data = {
1493 .name = "light",
1494 .read = light_read,
1495 .write = light_write,
1496};
1497
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001498/*************************************************************************
1499 * Dock subdriver
1500 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03001502#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001503
1504IBM_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
1505 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
1506 "\\_SB.PCI0.PCI1.DOCK", /* all others */
1507 "\\_SB.PCI.ISA.SLCE", /* 570 */
1508 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
1509
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001510/* don't list other alternatives as we install a notify handler on the 570 */
1511IBM_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
1512
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513#define dock_docked() (_sta(dock_handle) & 1)
1514
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001515static int __init dock_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001516{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001517 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
1518
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001519 IBM_ACPIHANDLE_INIT(dock);
1520 IBM_ACPIHANDLE_INIT(pci);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001521
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001522 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
1523 str_supported(dock_handle != NULL));
1524
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001525 return (dock_handle)? 0 : 1;
1526}
1527
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001528static void dock_notify(struct ibm_struct *ibm, u32 event)
1529{
1530 int docked = dock_docked();
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001531 int pci = ibm->acpi->hid && strstr(ibm->acpi->hid, IBM_PCI_HID);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001532
1533 if (event == 1 && !pci) /* 570 */
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001534 acpi_bus_generate_event(ibm->acpi->device, event, 1); /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001535 else if (event == 1 && pci) /* 570 */
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001536 acpi_bus_generate_event(ibm->acpi->device, event, 3); /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001537 else if (event == 3 && docked)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001538 acpi_bus_generate_event(ibm->acpi->device, event, 1); /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001539 else if (event == 3 && !docked)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001540 acpi_bus_generate_event(ibm->acpi->device, event, 2); /* undock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001541 else if (event == 0 && docked)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001542 acpi_bus_generate_event(ibm->acpi->device, event, 3); /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001543 else {
1544 printk(IBM_ERR "unknown dock event %d, status %d\n",
1545 event, _sta(dock_handle));
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001546 acpi_bus_generate_event(ibm->acpi->device, event, 0); /* unknown */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001547 }
1548}
1549
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001550static int dock_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551{
1552 int len = 0;
1553 int docked = dock_docked();
1554
1555 if (!dock_handle)
1556 len += sprintf(p + len, "status:\t\tnot supported\n");
1557 else if (!docked)
1558 len += sprintf(p + len, "status:\t\tundocked\n");
1559 else {
1560 len += sprintf(p + len, "status:\t\tdocked\n");
1561 len += sprintf(p + len, "commands:\tdock, undock\n");
1562 }
1563
1564 return len;
1565}
1566
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001567static int dock_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568{
1569 char *cmd;
1570
1571 if (!dock_docked())
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001572 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573
1574 while ((cmd = next_cmd(&buf))) {
1575 if (strlencmp(cmd, "undock") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001576 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
1577 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 return -EIO;
1579 } else if (strlencmp(cmd, "dock") == 0) {
1580 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
1581 return -EIO;
1582 } else
1583 return -EINVAL;
1584 }
1585
1586 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001587}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001589static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001590 {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001591 .notify = dock_notify,
1592 .handle = &dock_handle,
1593 .type = ACPI_SYSTEM_NOTIFY,
1594 },
1595 {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001596 .hid = IBM_PCI_HID,
1597 .notify = dock_notify,
1598 .handle = &pci_handle,
1599 .type = ACPI_SYSTEM_NOTIFY,
1600 },
1601};
1602
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001603static struct ibm_struct dock_driver_data[2] = {
1604 {
1605 .name = "dock",
1606 .read = dock_read,
1607 .write = dock_write,
1608 .acpi = &ibm_dock_acpidriver[0],
1609 },
1610 {
1611 .name = "dock",
1612 .acpi = &ibm_dock_acpidriver[1],
1613 },
1614};
1615
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03001616#endif /* CONFIG_THINKPAD_ACPI_DOCK */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001618/*************************************************************************
1619 * Bay subdriver
1620 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03001622#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001623IBM_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
1624 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
1625 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
1626 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
1627 ); /* A21e, R30, R31 */
1628IBM_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
1629 "_EJ0", /* all others */
1630 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
1631IBM_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
1632 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
1633 ); /* all others */
1634IBM_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
1635 "_EJ0", /* 770x */
1636 ); /* all others */
1637
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001638static int __init bay_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001640 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
1641
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001642 IBM_ACPIHANDLE_INIT(bay);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001643 if (bay_handle)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001644 IBM_ACPIHANDLE_INIT(bay_ej);
1645 IBM_ACPIHANDLE_INIT(bay2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001646 if (bay2_handle)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001647 IBM_ACPIHANDLE_INIT(bay2_ej);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001648
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001649 tp_features.bay_status = bay_handle &&
1650 acpi_evalf(bay_handle, NULL, "_STA", "qv");
1651 tp_features.bay_status2 = bay2_handle &&
1652 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001653
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001654 tp_features.bay_eject = bay_handle && bay_ej_handle &&
1655 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
1656 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
1657 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001659 vdbg_printk(TPACPI_DBG_INIT,
1660 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001661 str_supported(tp_features.bay_status),
1662 str_supported(tp_features.bay_eject),
1663 str_supported(tp_features.bay_status2),
1664 str_supported(tp_features.bay_eject2));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001665
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001666 return (tp_features.bay_status || tp_features.bay_eject ||
1667 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668}
1669
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001670static void bay_notify(struct ibm_struct *ibm, u32 event)
1671{
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001672 acpi_bus_generate_event(ibm->acpi->device, event, 0);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001673}
1674
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001675#define bay_occupied(b) (_sta(b##_handle) & 1)
1676
1677static int bay_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678{
1679 int len = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001680 int occupied = bay_occupied(bay);
1681 int occupied2 = bay_occupied(bay2);
1682 int eject, eject2;
1683
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001684 len += sprintf(p + len, "status:\t\t%s\n",
1685 tp_features.bay_status ?
1686 (occupied ? "occupied" : "unoccupied") :
1687 "not supported");
1688 if (tp_features.bay_status2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001689 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
1690 "occupied" : "unoccupied");
1691
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001692 eject = tp_features.bay_eject && occupied;
1693 eject2 = tp_features.bay_eject2 && occupied2;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001694
1695 if (eject && eject2)
1696 len += sprintf(p + len, "commands:\teject, eject2\n");
1697 else if (eject)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 len += sprintf(p + len, "commands:\teject\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001699 else if (eject2)
1700 len += sprintf(p + len, "commands:\teject2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701
1702 return len;
1703}
1704
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001705static int bay_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706{
1707 char *cmd;
1708
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001709 if (!tp_features.bay_eject && !tp_features.bay_eject2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001710 return -ENODEV;
1711
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001713 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001714 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
1715 return -EIO;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001716 } else if (tp_features.bay_eject2 &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001717 strlencmp(cmd, "eject2") == 0) {
1718 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 return -EIO;
1720 } else
1721 return -EINVAL;
1722 }
1723
1724 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001725}
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001726
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001727static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
1728 .notify = bay_notify,
1729 .handle = &bay_handle,
1730 .type = ACPI_SYSTEM_NOTIFY,
1731};
1732
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001733static struct ibm_struct bay_driver_data = {
1734 .name = "bay",
1735 .read = bay_read,
1736 .write = bay_write,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001737 .acpi = &ibm_bay_acpidriver,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001738};
1739
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03001740#endif /* CONFIG_THINKPAD_ACPI_BAY */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001742/*************************************************************************
1743 * CMOS subdriver
1744 */
1745
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001746static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001747{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001748 vdbg_printk(TPACPI_DBG_INIT,
1749 "initializing cmos commands subdriver\n");
1750
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001751 IBM_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001752
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001753 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
1754 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001755 return (cmos_handle)? 0 : 1;
1756}
1757
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001758static int cmos_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759{
1760 int len = 0;
1761
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001762 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1763 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 if (!cmos_handle)
1765 len += sprintf(p + len, "status:\t\tnot supported\n");
1766 else {
1767 len += sprintf(p + len, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001768 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 }
1770
1771 return len;
1772}
1773
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001774static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775{
1776 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03001777 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778
1779 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001780 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
1781 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 /* cmos_cmd set */
1783 } else
1784 return -EINVAL;
1785
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03001786 res = issue_thinkpad_cmos_command(cmos_cmd);
1787 if (res)
1788 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 }
1790
1791 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001792}
1793
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001794static struct ibm_struct cmos_driver_data = {
1795 .name = "cmos",
1796 .read = cmos_read,
1797 .write = cmos_write,
1798};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001799
1800/*************************************************************************
1801 * LED subdriver
1802 */
1803
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02001804static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001805
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001806IBM_HANDLE(led, ec, "SLED", /* 570 */
1807 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
1808 "LED", /* all others */
1809 ); /* R30, R31 */
1810
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001811static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001812{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001813 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
1814
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001815 IBM_ACPIHANDLE_INIT(led);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001816
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001817 if (!led_handle)
1818 /* led not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001819 led_supported = TPACPI_LED_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001820 else if (strlencmp(led_path, "SLED") == 0)
1821 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001822 led_supported = TPACPI_LED_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001823 else if (strlencmp(led_path, "SYSL") == 0)
1824 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001825 led_supported = TPACPI_LED_OLD;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001826 else
1827 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001828 led_supported = TPACPI_LED_NEW;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001829
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001830 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
1831 str_supported(led_supported), led_supported);
1832
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001833 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001834}
1835
1836#define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
1837
1838static int led_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839{
1840 int len = 0;
1841
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001842 if (!led_supported) {
1843 len += sprintf(p + len, "status:\t\tnot supported\n");
1844 return len;
1845 }
1846 len += sprintf(p + len, "status:\t\tsupported\n");
1847
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001848 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001849 /* 570 */
1850 int i, status;
1851 for (i = 0; i < 8; i++) {
1852 if (!acpi_evalf(ec_handle,
1853 &status, "GLED", "dd", 1 << i))
1854 return -EIO;
1855 len += sprintf(p + len, "%d:\t\t%s\n",
1856 i, led_status(status));
1857 }
1858 }
1859
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 len += sprintf(p + len, "commands:\t"
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001861 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
1863 return len;
1864}
1865
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001866/* off, on, blink */
1867static const int led_sled_arg1[] = { 0, 1, 3 };
1868static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
1869static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
1870static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
1871
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001872static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873{
1874 char *cmd;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001875 int led, ind, ret;
1876
1877 if (!led_supported)
1878 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879
1880 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001881 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 return -EINVAL;
1883
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001884 if (strstr(cmd, "off")) {
1885 ind = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 } else if (strstr(cmd, "on")) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001887 ind = 1;
1888 } else if (strstr(cmd, "blink")) {
1889 ind = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 } else
1891 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001892
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001893 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001894 /* 570 */
1895 led = 1 << led;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001897 led, led_sled_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 return -EIO;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001899 } else if (led_supported == TPACPI_LED_OLD) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001900 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
1901 led = 1 << led;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001902 ret = ec_write(TPACPI_LED_EC_HLMS, led);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001903 if (ret >= 0)
1904 ret =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001905 ec_write(TPACPI_LED_EC_HLBL,
Henrique de Moraes Holschuhe062e032007-03-23 17:33:55 -03001906 led * led_exp_hlbl[ind]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001907 if (ret >= 0)
1908 ret =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001909 ec_write(TPACPI_LED_EC_HLCL,
Henrique de Moraes Holschuhe062e032007-03-23 17:33:55 -03001910 led * led_exp_hlcl[ind]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001911 if (ret < 0)
1912 return ret;
1913 } else {
1914 /* all others */
1915 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
1916 led, led_led_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001918 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 }
1920
1921 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001922}
1923
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001924static struct ibm_struct led_driver_data = {
1925 .name = "led",
1926 .read = led_read,
1927 .write = led_write,
1928};
1929
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001930/*************************************************************************
1931 * Beep subdriver
1932 */
1933
1934IBM_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
1935
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001936static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001937{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001938 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
1939
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001940 IBM_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001941
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001942 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
1943 str_supported(beep_handle != NULL));
1944
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001945 return (beep_handle)? 0 : 1;
1946}
1947
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001948static int beep_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949{
1950 int len = 0;
1951
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001952 if (!beep_handle)
1953 len += sprintf(p + len, "status:\t\tnot supported\n");
1954 else {
1955 len += sprintf(p + len, "status:\t\tsupported\n");
1956 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
1957 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958
1959 return len;
1960}
1961
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001962static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963{
1964 char *cmd;
1965 int beep_cmd;
1966
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001967 if (!beep_handle)
1968 return -ENODEV;
1969
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001971 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
1972 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 /* beep_cmd set */
1974 } else
1975 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001976 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 return -EIO;
1978 }
1979
1980 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001981}
1982
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001983static struct ibm_struct beep_driver_data = {
1984 .name = "beep",
1985 .read = beep_read,
1986 .write = beep_write,
1987};
1988
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001989/*************************************************************************
1990 * Thermal subdriver
1991 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001992
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001993static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001994
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03001995/* sysfs temp##_input -------------------------------------------------- */
1996
1997static ssize_t thermal_temp_input_show(struct device *dev,
1998 struct device_attribute *attr,
1999 char *buf)
2000{
2001 struct sensor_device_attribute *sensor_attr =
2002 to_sensor_dev_attr(attr);
2003 int idx = sensor_attr->index;
2004 s32 value;
2005 int res;
2006
2007 res = thermal_get_sensor(idx, &value);
2008 if (res)
2009 return res;
2010 if (value == TP_EC_THERMAL_TMP_NA * 1000)
2011 return -ENXIO;
2012
2013 return snprintf(buf, PAGE_SIZE, "%d\n", value);
2014}
2015
2016#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
2017 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, thermal_temp_input_show, NULL, _idxB)
2018
2019static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
2020 THERMAL_SENSOR_ATTR_TEMP(1, 0),
2021 THERMAL_SENSOR_ATTR_TEMP(2, 1),
2022 THERMAL_SENSOR_ATTR_TEMP(3, 2),
2023 THERMAL_SENSOR_ATTR_TEMP(4, 3),
2024 THERMAL_SENSOR_ATTR_TEMP(5, 4),
2025 THERMAL_SENSOR_ATTR_TEMP(6, 5),
2026 THERMAL_SENSOR_ATTR_TEMP(7, 6),
2027 THERMAL_SENSOR_ATTR_TEMP(8, 7),
2028 THERMAL_SENSOR_ATTR_TEMP(9, 8),
2029 THERMAL_SENSOR_ATTR_TEMP(10, 9),
2030 THERMAL_SENSOR_ATTR_TEMP(11, 10),
2031 THERMAL_SENSOR_ATTR_TEMP(12, 11),
2032 THERMAL_SENSOR_ATTR_TEMP(13, 12),
2033 THERMAL_SENSOR_ATTR_TEMP(14, 13),
2034 THERMAL_SENSOR_ATTR_TEMP(15, 14),
2035 THERMAL_SENSOR_ATTR_TEMP(16, 15),
2036};
2037
2038#define THERMAL_ATTRS(X) \
2039 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
2040
2041static struct attribute *thermal_temp_input_attr[] = {
2042 THERMAL_ATTRS(8),
2043 THERMAL_ATTRS(9),
2044 THERMAL_ATTRS(10),
2045 THERMAL_ATTRS(11),
2046 THERMAL_ATTRS(12),
2047 THERMAL_ATTRS(13),
2048 THERMAL_ATTRS(14),
2049 THERMAL_ATTRS(15),
2050 THERMAL_ATTRS(0),
2051 THERMAL_ATTRS(1),
2052 THERMAL_ATTRS(2),
2053 THERMAL_ATTRS(3),
2054 THERMAL_ATTRS(4),
2055 THERMAL_ATTRS(5),
2056 THERMAL_ATTRS(6),
2057 THERMAL_ATTRS(7),
2058 NULL
2059};
2060
2061static const struct attribute_group thermal_temp_input16_group = {
2062 .attrs = thermal_temp_input_attr
2063};
2064
2065static const struct attribute_group thermal_temp_input8_group = {
2066 .attrs = &thermal_temp_input_attr[8]
2067};
2068
2069#undef THERMAL_SENSOR_ATTR_TEMP
2070#undef THERMAL_ATTRS
2071
2072/* --------------------------------------------------------------------- */
2073
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002074static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002075{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002076 u8 t, ta1, ta2;
2077 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002078 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03002079 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002080
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002081 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
2082
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002083 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002084
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002085 if (ibm_thinkpad_ec_found && experimental) {
2086 /*
2087 * Direct EC access mode: sensors at registers
2088 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
2089 * non-implemented, thermal sensors return 0x80 when
2090 * not available
2091 */
2092
2093 ta1 = ta2 = 0;
2094 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002095 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002096 ta1 |= t;
2097 } else {
2098 ta1 = 0;
2099 break;
2100 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002101 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002102 ta2 |= t;
2103 } else {
2104 ta1 = 0;
2105 break;
2106 }
2107 }
2108 if (ta1 == 0) {
2109 /* This is sheer paranoia, but we handle it anyway */
2110 if (acpi_tmp7) {
2111 printk(IBM_ERR
2112 "ThinkPad ACPI EC access misbehaving, "
2113 "falling back to ACPI TMPx access mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002114 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002115 } else {
2116 printk(IBM_ERR
2117 "ThinkPad ACPI EC access misbehaving, "
2118 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002119 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002120 }
2121 } else {
2122 thermal_read_mode =
2123 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002124 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002125 }
2126 } else if (acpi_tmp7) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002127 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
2128 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002129 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002130 } else {
2131 /* Standard ACPI TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002132 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002133 }
2134 } else {
2135 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002136 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002137 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002138
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002139 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
2140 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
2141 thermal_read_mode);
2142
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03002143 switch(thermal_read_mode) {
2144 case TPACPI_THERMAL_TPEC_16:
2145 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2146 &thermal_temp_input16_group);
2147 if (res)
2148 return res;
2149 break;
2150 case TPACPI_THERMAL_TPEC_8:
2151 case TPACPI_THERMAL_ACPI_TMP07:
2152 case TPACPI_THERMAL_ACPI_UPDT:
2153 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2154 &thermal_temp_input8_group);
2155 if (res)
2156 return res;
2157 break;
2158 case TPACPI_THERMAL_NONE:
2159 default:
2160 return 1;
2161 }
2162
2163 return 0;
2164}
2165
2166static void thermal_exit(void)
2167{
2168 switch(thermal_read_mode) {
2169 case TPACPI_THERMAL_TPEC_16:
2170 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2171 &thermal_temp_input16_group);
2172 break;
2173 case TPACPI_THERMAL_TPEC_8:
2174 case TPACPI_THERMAL_ACPI_TMP07:
2175 case TPACPI_THERMAL_ACPI_UPDT:
2176 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2177 &thermal_temp_input16_group);
2178 break;
2179 case TPACPI_THERMAL_NONE:
2180 default:
2181 break;
2182 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002183}
2184
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002185/* idx is zero-based */
2186static int thermal_get_sensor(int idx, s32 *value)
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002187{
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002188 int t;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002189 s8 tmp;
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002190 char tmpi[5];
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002191
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002192 t = TP_EC_THERMAL_TMP0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002193
2194 switch (thermal_read_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002195#if TPACPI_MAX_THERMAL_SENSORS >= 16
2196 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002197 if (idx >= 8 && idx <= 15) {
2198 t = TP_EC_THERMAL_TMP8;
2199 idx -= 8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002200 }
2201 /* fallthrough */
2202#endif
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002203 case TPACPI_THERMAL_TPEC_8:
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002204 if (idx <= 7) {
2205 if (!acpi_ec_read(t + idx, &tmp))
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002206 return -EIO;
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002207 *value = tmp * 1000;
2208 return 0;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002209 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002210 break;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002211
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002212 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002213 if (idx <= 7) {
2214 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
2215 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
2216 return -EIO;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002217 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
2218 return -EIO;
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002219 *value = (t - 2732) * 100;
2220 return 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002221 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002222 break;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002223
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002224 case TPACPI_THERMAL_ACPI_TMP07:
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002225 if (idx <= 7) {
2226 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002227 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
2228 return -EIO;
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002229 *value = t * 1000;
2230 return 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002231 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002232 break;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002233
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002234 case TPACPI_THERMAL_NONE:
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002235 default:
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002236 return -ENOSYS;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002237 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002238
2239 return -EINVAL;
2240}
2241
2242static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
2243{
2244 int res, i;
2245 int n;
2246
2247 n = 8;
2248 i = 0;
2249
2250 if (!s)
2251 return -EINVAL;
2252
2253 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
2254 n = 16;
2255
2256 for(i = 0 ; i < n; i++) {
2257 res = thermal_get_sensor(i, &s->temp[i]);
2258 if (res)
2259 return res;
2260 }
2261
2262 return n;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002263}
2264
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002265static int thermal_read(char *p)
2266{
2267 int len = 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002268 int n, i;
2269 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002270
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002271 n = thermal_get_sensors(&t);
2272 if (unlikely(n < 0))
2273 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002274
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002275 len += sprintf(p + len, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002276
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002277 if (n > 0) {
2278 for (i = 0; i < (n - 1); i++)
2279 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
2280 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
2281 } else
2282 len += sprintf(p + len, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002283
2284 return len;
2285}
2286
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002287static struct ibm_struct thermal_driver_data = {
2288 .name = "thermal",
2289 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03002290 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002291};
2292
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002293/*************************************************************************
2294 * EC Dump subdriver
2295 */
2296
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002297static u8 ecdump_regs[256];
2298
2299static int ecdump_read(char *p)
2300{
2301 int len = 0;
2302 int i, j;
2303 u8 v;
2304
2305 len += sprintf(p + len, "EC "
2306 " +00 +01 +02 +03 +04 +05 +06 +07"
2307 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
2308 for (i = 0; i < 256; i += 16) {
2309 len += sprintf(p + len, "EC 0x%02x:", i);
2310 for (j = 0; j < 16; j++) {
2311 if (!acpi_ec_read(i + j, &v))
2312 break;
2313 if (v != ecdump_regs[i + j])
2314 len += sprintf(p + len, " *%02x", v);
2315 else
2316 len += sprintf(p + len, " %02x", v);
2317 ecdump_regs[i + j] = v;
2318 }
2319 len += sprintf(p + len, "\n");
2320 if (j != 16)
2321 break;
2322 }
2323
2324 /* These are way too dangerous to advertise openly... */
2325#if 0
2326 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
2327 " (<offset> is 00-ff, <value> is 00-ff)\n");
2328 len += sprintf(p + len, "commands:\t0x<offset> <value> "
2329 " (<offset> is 00-ff, <value> is 0-255)\n");
2330#endif
2331 return len;
2332}
2333
2334static int ecdump_write(char *buf)
2335{
2336 char *cmd;
2337 int i, v;
2338
2339 while ((cmd = next_cmd(&buf))) {
2340 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
2341 /* i and v set */
2342 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
2343 /* i and v set */
2344 } else
2345 return -EINVAL;
2346 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
2347 if (!acpi_ec_write(i, v))
2348 return -EIO;
2349 } else
2350 return -EINVAL;
2351 }
2352
2353 return 0;
2354}
2355
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002356static struct ibm_struct ecdump_driver_data = {
2357 .name = "ecdump",
2358 .read = ecdump_read,
2359 .write = ecdump_write,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03002360 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002361};
2362
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002363/*************************************************************************
2364 * Backlight/brightness subdriver
2365 */
Holger Macht8acb0252006-10-20 14:30:28 -07002366
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002367static struct backlight_device *ibm_backlight_device = NULL;
Holger Macht8acb0252006-10-20 14:30:28 -07002368
Richard Purdie599a52d2007-02-10 23:07:48 +00002369static struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02002370 .get_brightness = brightness_get,
2371 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02002372};
2373
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002374static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02002375{
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02002376 int b;
2377
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002378 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
2379
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02002380 b = brightness_get(NULL);
2381 if (b < 0)
2382 return b;
2383
Yu Luming519ab5f2006-12-19 12:56:15 -08002384 ibm_backlight_device = backlight_device_register("ibm", NULL, NULL,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02002385 &ibm_backlight_data);
2386 if (IS_ERR(ibm_backlight_device)) {
2387 printk(IBM_ERR "Could not register backlight device\n");
2388 return PTR_ERR(ibm_backlight_device);
2389 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002390 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02002391
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02002392 ibm_backlight_device->props.max_brightness = 7;
2393 ibm_backlight_device->props.brightness = b;
2394 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00002395
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02002396 return 0;
2397}
2398
2399static void brightness_exit(void)
2400{
2401 if (ibm_backlight_device) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002402 vdbg_printk(TPACPI_DBG_EXIT,
2403 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02002404 backlight_device_unregister(ibm_backlight_device);
2405 ibm_backlight_device = NULL;
2406 }
2407}
2408
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002409static int brightness_update_status(struct backlight_device *bd)
2410{
2411 return brightness_set(
2412 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
2413 bd->props.power == FB_BLANK_UNBLANK) ?
2414 bd->props.brightness : 0);
2415}
2416
2417static int brightness_get(struct backlight_device *bd)
2418{
2419 u8 level;
2420 if (!acpi_ec_read(brightness_offset, &level))
2421 return -EIO;
2422
2423 level &= 0x7;
2424
2425 return level;
2426}
2427
2428static int brightness_set(int value)
2429{
2430 int cmos_cmd, inc, i;
2431 int current_value = brightness_get(NULL);
2432
2433 value &= 7;
2434
2435 cmos_cmd = value > current_value ? TP_CMOS_BRIGHTNESS_UP : TP_CMOS_BRIGHTNESS_DOWN;
2436 inc = value > current_value ? 1 : -1;
2437 for (i = current_value; i != value; i += inc) {
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03002438 if (issue_thinkpad_cmos_command(cmos_cmd))
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002439 return -EIO;
2440 if (!acpi_ec_write(brightness_offset, i + inc))
2441 return -EIO;
2442 }
2443
2444 return 0;
2445}
2446
2447static int brightness_read(char *p)
2448{
2449 int len = 0;
2450 int level;
2451
2452 if ((level = brightness_get(NULL)) < 0) {
2453 len += sprintf(p + len, "level:\t\tunreadable\n");
2454 } else {
2455 len += sprintf(p + len, "level:\t\t%d\n", level & 0x7);
2456 len += sprintf(p + len, "commands:\tup, down\n");
2457 len += sprintf(p + len, "commands:\tlevel <level>"
2458 " (<level> is 0-7)\n");
2459 }
2460
2461 return len;
2462}
2463
2464static int brightness_write(char *buf)
2465{
2466 int level;
2467 int new_level;
2468 char *cmd;
2469
2470 while ((cmd = next_cmd(&buf))) {
2471 if ((level = brightness_get(NULL)) < 0)
2472 return level;
2473 level &= 7;
2474
2475 if (strlencmp(cmd, "up") == 0) {
2476 new_level = level == 7 ? 7 : level + 1;
2477 } else if (strlencmp(cmd, "down") == 0) {
2478 new_level = level == 0 ? 0 : level - 1;
2479 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
2480 new_level >= 0 && new_level <= 7) {
2481 /* new_level set */
2482 } else
2483 return -EINVAL;
2484
2485 brightness_set(new_level);
2486 }
2487
2488 return 0;
2489}
2490
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002491static struct ibm_struct brightness_driver_data = {
2492 .name = "brightness",
2493 .read = brightness_read,
2494 .write = brightness_write,
2495 .exit = brightness_exit,
2496};
2497
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002498/*************************************************************************
2499 * Volume subdriver
2500 */
2501
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002502static int volume_read(char *p)
2503{
2504 int len = 0;
2505 u8 level;
2506
2507 if (!acpi_ec_read(volume_offset, &level)) {
2508 len += sprintf(p + len, "level:\t\tunreadable\n");
2509 } else {
2510 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
2511 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
2512 len += sprintf(p + len, "commands:\tup, down, mute\n");
2513 len += sprintf(p + len, "commands:\tlevel <level>"
2514 " (<level> is 0-15)\n");
2515 }
2516
2517 return len;
2518}
2519
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002520static int volume_write(char *buf)
2521{
2522 int cmos_cmd, inc, i;
2523 u8 level, mute;
2524 int new_level, new_mute;
2525 char *cmd;
2526
2527 while ((cmd = next_cmd(&buf))) {
2528 if (!acpi_ec_read(volume_offset, &level))
2529 return -EIO;
2530 new_mute = mute = level & 0x40;
2531 new_level = level = level & 0xf;
2532
2533 if (strlencmp(cmd, "up") == 0) {
2534 if (mute)
2535 new_mute = 0;
2536 else
2537 new_level = level == 15 ? 15 : level + 1;
2538 } else if (strlencmp(cmd, "down") == 0) {
2539 if (mute)
2540 new_mute = 0;
2541 else
2542 new_level = level == 0 ? 0 : level - 1;
2543 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
2544 new_level >= 0 && new_level <= 15) {
2545 /* new_level set */
2546 } else if (strlencmp(cmd, "mute") == 0) {
2547 new_mute = 0x40;
2548 } else
2549 return -EINVAL;
2550
2551 if (new_level != level) { /* mute doesn't change */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002552 cmos_cmd = new_level > level ? TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002553 inc = new_level > level ? 1 : -1;
2554
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03002555 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002556 !acpi_ec_write(volume_offset, level)))
2557 return -EIO;
2558
2559 for (i = level; i != new_level; i += inc)
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03002560 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002561 !acpi_ec_write(volume_offset, i + inc))
2562 return -EIO;
2563
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03002564 if (mute && (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002565 !acpi_ec_write(volume_offset,
2566 new_level + mute)))
2567 return -EIO;
2568 }
2569
2570 if (new_mute != mute) { /* level doesn't change */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002571 cmos_cmd = new_mute ? TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002572
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03002573 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002574 !acpi_ec_write(volume_offset, level + new_mute))
2575 return -EIO;
2576 }
2577 }
2578
2579 return 0;
2580}
2581
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002582static struct ibm_struct volume_driver_data = {
2583 .name = "volume",
2584 .read = volume_read,
2585 .write = volume_write,
2586};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002587
2588/*************************************************************************
2589 * Fan subdriver
2590 */
2591
2592/*
2593 * FAN ACCESS MODES
2594 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002595 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002596 * ACPI GFAN method: returns fan level
2597 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002598 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03002599 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002600 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002601 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002602 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
2603 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03002604 * EC 0x2f (HFSP) might be available *for reading*, but do not use
2605 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002606 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002607 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03002608 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
2609 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002610 *
2611 * Fan speed changes of any sort (including those caused by the
2612 * disengaged mode) are usually done slowly by the firmware as the
2613 * maximum ammount of fan duty cycle change per second seems to be
2614 * limited.
2615 *
2616 * Reading is not available if GFAN exists.
2617 * Writing is not available if SFAN exists.
2618 *
2619 * Bits
2620 * 7 automatic mode engaged;
2621 * (default operation mode of the ThinkPad)
2622 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03002623 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002624 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03002625 * the tachometer while the fan controller ramps up
2626 * the speed (which can take up to a few *minutes*).
2627 * Speeds up fan to 100% duty-cycle, which is far above
2628 * the standard RPM levels. It is not impossible that
2629 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002630 * 5-3 unused in some models. Extra bits for fan level
2631 * in others, but still useless as all values above
2632 * 7 map to the same speed as level 7 in these models.
2633 * 2-0 fan level (0..7 usually)
2634 * 0x00 = stop
2635 * 0x07 = max (set when temperatures critical)
2636 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002637 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002638 *
2639 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
2640 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
2641 * does so, its initial value is meaningless (0x07).
2642 *
2643 * For firmware bugs, refer to:
2644 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
2645 *
2646 * ----
2647 *
2648 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
2649 * Main fan tachometer reading (in RPM)
2650 *
2651 * This register is present on all ThinkPads with a new-style EC, and
2652 * it is known not to be present on the A21m/e, and T22, as there is
2653 * something else in offset 0x84 according to the ACPI DSDT. Other
2654 * ThinkPads from this same time period (and earlier) probably lack the
2655 * tachometer as well.
2656 *
2657 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
2658 * was never fixed by IBM to report the EC firmware version string
2659 * probably support the tachometer (like the early X models), so
2660 * detecting it is quite hard. We need more data to know for sure.
2661 *
2662 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
2663 * might result.
2664 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03002665 * FIRMWARE BUG: may go stale while the EC is switching to full speed
2666 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002667 *
2668 * For firmware bugs, refer to:
2669 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
2670 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002671 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002672 * ThinkPad X31, X40, X41. Not available in the X60.
2673 *
2674 * FANS ACPI handle: takes three arguments: low speed, medium speed,
2675 * high speed. ACPI DSDT seems to map these three speeds to levels
2676 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
2677 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
2678 *
2679 * The speeds are stored on handles
2680 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
2681 *
2682 * There are three default speed sets, acessible as handles:
2683 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
2684 *
2685 * ACPI DSDT switches which set is in use depending on various
2686 * factors.
2687 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002688 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002689 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
2690 * but the ACPI tables just mention level 7.
2691 */
2692
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002693static enum fan_status_access_mode fan_status_access_mode;
2694static enum fan_control_access_mode fan_control_access_mode;
2695static enum fan_control_commands fan_control_commands;
2696
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002697static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03002698static u8 fan_control_desired_level;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002699
Len Brown25c68a32006-12-08 04:43:41 -05002700static void fan_watchdog_fire(struct work_struct *ignored);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002701static int fan_watchdog_maxinterval;
Len Brown25c68a32006-12-08 04:43:41 -05002702static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002703
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002704IBM_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
2705IBM_HANDLE(gfan, ec, "GFAN", /* 570 */
2706 "\\FSPD", /* 600e/x, 770e, 770x */
2707 ); /* all others */
2708IBM_HANDLE(sfan, ec, "SFAN", /* 570 */
2709 "JFNS", /* 770x-JL */
2710 ); /* all others */
2711
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03002712/*
2713 * SYSFS fan layout: hwmon compatible (device)
2714 *
2715 * pwm*_enable:
2716 * 0: "disengaged" mode
2717 * 1: manual mode
2718 * 2: native EC "auto" mode (recommended, hardware default)
2719 *
2720 * pwm*: set speed in manual mode, ignored otherwise.
2721 * 0 is level 0; 255 is level 7. Intermediate points done with linear
2722 * interpolation.
2723 *
2724 * fan*_input: tachometer reading, RPM
2725 *
2726 *
2727 * SYSFS fan layout: extensions
2728 *
2729 * fan_watchdog (driver):
2730 * fan watchdog interval in seconds, 0 disables (default), max 120
2731 */
2732
2733/* sysfs fan pwm1_enable ----------------------------------------------- */
2734static ssize_t fan_pwm1_enable_show(struct device *dev,
2735 struct device_attribute *attr,
2736 char *buf)
2737{
2738 int res, mode;
2739 u8 status;
2740
2741 res = fan_get_status_safe(&status);
2742 if (res)
2743 return res;
2744
2745 if (unlikely(tp_features.fan_ctrl_status_undef)) {
2746 if (status != fan_control_initial_status) {
2747 tp_features.fan_ctrl_status_undef = 0;
2748 } else {
2749 /* Return most likely status. In fact, it
2750 * might be the only possible status */
2751 status = TP_EC_FAN_AUTO;
2752 }
2753 }
2754
2755 if (status & TP_EC_FAN_FULLSPEED) {
2756 mode = 0;
2757 } else if (status & TP_EC_FAN_AUTO) {
2758 mode = 2;
2759 } else
2760 mode = 1;
2761
2762 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
2763}
2764
2765static ssize_t fan_pwm1_enable_store(struct device *dev,
2766 struct device_attribute *attr,
2767 const char *buf, size_t count)
2768{
2769 unsigned long t;
2770 int res, level;
2771
2772 if (parse_strtoul(buf, 2, &t))
2773 return -EINVAL;
2774
2775 switch (t) {
2776 case 0:
2777 level = TP_EC_FAN_FULLSPEED;
2778 break;
2779 case 1:
2780 level = TPACPI_FAN_LAST_LEVEL;
2781 break;
2782 case 2:
2783 level = TP_EC_FAN_AUTO;
2784 break;
2785 case 3:
2786 /* reserved for software-controlled auto mode */
2787 return -ENOSYS;
2788 default:
2789 return -EINVAL;
2790 }
2791
2792 res = fan_set_level_safe(level);
2793 if (res < 0)
2794 return res;
2795
2796 fan_watchdog_reset();
2797
2798 return count;
2799}
2800
2801static struct device_attribute dev_attr_fan_pwm1_enable =
2802 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
2803 fan_pwm1_enable_show, fan_pwm1_enable_store);
2804
2805/* sysfs fan pwm1 ------------------------------------------------------ */
2806static ssize_t fan_pwm1_show(struct device *dev,
2807 struct device_attribute *attr,
2808 char *buf)
2809{
2810 int res;
2811 u8 status;
2812
2813 res = fan_get_status_safe(&status);
2814 if (res)
2815 return res;
2816
2817 if (unlikely(tp_features.fan_ctrl_status_undef)) {
2818 if (status != fan_control_initial_status) {
2819 tp_features.fan_ctrl_status_undef = 0;
2820 } else {
2821 status = TP_EC_FAN_AUTO;
2822 }
2823 }
2824
2825 if ((status &
2826 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
2827 status = fan_control_desired_level;
2828
2829 if (status > 7)
2830 status = 7;
2831
2832 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
2833}
2834
2835static ssize_t fan_pwm1_store(struct device *dev,
2836 struct device_attribute *attr,
2837 const char *buf, size_t count)
2838{
2839 unsigned long s;
2840 int rc;
2841 u8 status, newlevel;
2842
2843 if (parse_strtoul(buf, 255, &s))
2844 return -EINVAL;
2845
2846 /* scale down from 0-255 to 0-7 */
2847 newlevel = (s >> 5) & 0x07;
2848
2849 rc = mutex_lock_interruptible(&fan_mutex);
2850 if (rc < 0)
2851 return rc;
2852
2853 rc = fan_get_status(&status);
2854 if (!rc && (status &
2855 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
2856 rc = fan_set_level(newlevel);
2857 if (!rc)
2858 fan_update_desired_level(newlevel);
2859 fan_watchdog_reset();
2860 }
2861
2862 mutex_unlock(&fan_mutex);
2863 return (rc)? rc : count;
2864}
2865
2866static struct device_attribute dev_attr_fan_pwm1 =
2867 __ATTR(pwm1, S_IWUSR | S_IRUGO,
2868 fan_pwm1_show, fan_pwm1_store);
2869
2870/* sysfs fan fan1_input ------------------------------------------------ */
2871static ssize_t fan_fan1_input_show(struct device *dev,
2872 struct device_attribute *attr,
2873 char *buf)
2874{
2875 int res;
2876 unsigned int speed;
2877
2878 res = fan_get_speed(&speed);
2879 if (res < 0)
2880 return res;
2881
2882 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
2883}
2884
2885static struct device_attribute dev_attr_fan_fan1_input =
2886 __ATTR(fan1_input, S_IRUGO,
2887 fan_fan1_input_show, NULL);
2888
2889/* sysfs fan fan_watchdog (driver) ------------------------------------- */
2890static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
2891 char *buf)
2892{
2893 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
2894}
2895
2896static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
2897 const char *buf, size_t count)
2898{
2899 unsigned long t;
2900
2901 if (parse_strtoul(buf, 120, &t))
2902 return -EINVAL;
2903
2904 fan_watchdog_maxinterval = t;
2905 fan_watchdog_reset();
2906
2907 return count;
2908}
2909
2910static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
2911 fan_fan_watchdog_show, fan_fan_watchdog_store);
2912
2913/* --------------------------------------------------------------------- */
2914static struct attribute *fan_attributes[] = {
2915 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
2916 &dev_attr_fan_fan1_input.attr,
2917 NULL
2918};
2919
2920static const struct attribute_group fan_attr_group = {
2921 .attrs = fan_attributes,
2922};
2923
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002924static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002925{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03002926 int rc;
2927
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002928 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
2929
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002930 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002931 fan_status_access_mode = TPACPI_FAN_NONE;
2932 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002933 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002934 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002935 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03002936 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002937
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002938 IBM_ACPIHANDLE_INIT(fans);
2939 IBM_ACPIHANDLE_INIT(gfan);
2940 IBM_ACPIHANDLE_INIT(sfan);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002941
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002942 if (gfan_handle) {
2943 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002944 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002945 } else {
2946 /* all other ThinkPads: note that even old-style
2947 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002948 if (likely(acpi_ec_read(fan_status_offset,
2949 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002950 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002951
2952 /* In some ThinkPads, neither the EC nor the ACPI
2953 * DSDT initialize the fan status, and it ends up
2954 * being set to 0x07 when it *could* be either
2955 * 0x07 or 0x80.
2956 *
2957 * Enable for TP-1Y (T43), TP-78 (R51e),
2958 * TP-76 (R52), TP-70 (T43, R52), which are known
2959 * to be buggy. */
2960 if (fan_control_initial_status == 0x07 &&
2961 ibm_thinkpad_ec_found &&
2962 ((ibm_thinkpad_ec_found[0] == '1' &&
2963 ibm_thinkpad_ec_found[1] == 'Y') ||
2964 (ibm_thinkpad_ec_found[0] == '7' &&
2965 (ibm_thinkpad_ec_found[1] == '6' ||
2966 ibm_thinkpad_ec_found[1] == '8' ||
2967 ibm_thinkpad_ec_found[1] == '0'))
2968 )) {
2969 printk(IBM_NOTICE
2970 "fan_init: initial fan status is "
2971 "unknown, assuming it is in auto "
2972 "mode\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002973 tp_features.fan_ctrl_status_undef = 1;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002974 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002975 } else {
2976 printk(IBM_ERR
2977 "ThinkPad ACPI EC access misbehaving, "
2978 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002979 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002980 }
2981 }
2982
2983 if (sfan_handle) {
2984 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002985 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002986 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002987 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002988 } else {
2989 if (!gfan_handle) {
2990 /* gfan without sfan means no fan control */
2991 /* all other models implement TP EC 0x2f control */
2992
2993 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02002994 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002995 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002996 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002997 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002998 TPACPI_FAN_CMD_SPEED |
2999 TPACPI_FAN_CMD_LEVEL |
3000 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003001 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003002 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02003003 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003004 TPACPI_FAN_CMD_LEVEL |
3005 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003006 }
3007 }
3008 }
3009
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003010 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
3011 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
3012 fan_control_access_mode != TPACPI_FAN_WR_NONE),
3013 fan_status_access_mode, fan_control_access_mode);
3014
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003015 /* update fan_control_desired_level */
3016 if (fan_status_access_mode != TPACPI_FAN_NONE)
3017 fan_get_status_safe(NULL);
3018
3019 if (fan_status_access_mode != TPACPI_FAN_NONE ||
3020 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
3021 rc = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3022 &fan_attr_group);
3023 if (!(rc < 0))
3024 rc = driver_create_file(&tpacpi_pdriver.driver,
3025 &driver_attr_fan_watchdog);
3026 if (rc < 0)
3027 return rc;
3028 return 0;
3029 } else
3030 return 1;
3031}
3032
3033/*
3034 * Call with fan_mutex held
3035 */
3036static void fan_update_desired_level(u8 status)
3037{
3038 if ((status &
3039 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
3040 if (status > 7)
3041 fan_control_desired_level = 7;
3042 else
3043 fan_control_desired_level = status;
3044 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003045}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003046
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02003047static int fan_get_status(u8 *status)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003048{
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02003049 u8 s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003050
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02003051 /* TODO:
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003052 * Add TPACPI_FAN_RD_ACPI_FANS ? */
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02003053
Henrique de Moraes Holschuh3ef8a602006-11-24 11:47:10 -02003054 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003055 case TPACPI_FAN_RD_ACPI_GFAN:
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003056 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02003057
3058 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003059 return -EIO;
3060
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02003061 if (likely(status))
3062 *status = s & 0x07;
3063
3064 break;
3065
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003066 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02003067 /* all except 570, 600e/x, 770e, 770x */
3068 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
3069 return -EIO;
3070
3071 if (likely(status))
3072 *status = s;
3073
3074 break;
3075
3076 default:
3077 return -ENXIO;
3078 }
3079
3080 return 0;
3081}
3082
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003083static int fan_get_status_safe(u8 *status)
3084{
3085 int rc;
3086 u8 s;
3087
3088 rc = mutex_lock_interruptible(&fan_mutex);
3089 if (rc < 0)
3090 return rc;
3091 rc = fan_get_status(&s);
3092 if (!rc)
3093 fan_update_desired_level(s);
3094 mutex_unlock(&fan_mutex);
3095
3096 if (status)
3097 *status = s;
3098
3099 return rc;
3100}
3101
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003102static void fan_exit(void)
3103{
Henrique de Moraes Holschuh99fba3f2007-04-21 11:08:44 -03003104 vdbg_printk(TPACPI_DBG_EXIT, "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003105
3106 /* FIXME: can we really do this unconditionally? */
3107 sysfs_remove_group(&tpacpi_pdev->dev.kobj, &fan_attr_group);
3108 driver_remove_file(&tpacpi_pdriver.driver, &driver_attr_fan_watchdog);
3109
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003110 cancel_delayed_work(&fan_watchdog_task);
3111 flush_scheduled_work();
3112}
3113
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02003114static int fan_get_speed(unsigned int *speed)
3115{
3116 u8 hi, lo;
3117
3118 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003119 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02003120 /* all except 570, 600e/x, 770e, 770x */
3121 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
3122 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
3123 return -EIO;
3124
3125 if (likely(speed))
3126 *speed = (hi << 8) | lo;
3127
3128 break;
3129
3130 default:
3131 return -ENXIO;
3132 }
3133
3134 return 0;
3135}
3136
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003137static void fan_watchdog_fire(struct work_struct *ignored)
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003138{
Henrique de Moraes Holschuh99fba3f2007-04-21 11:08:44 -03003139 int rc;
3140
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003141 printk(IBM_NOTICE "fan watchdog: enabling fan\n");
Henrique de Moraes Holschuh99fba3f2007-04-21 11:08:44 -03003142 rc = fan_set_enable();
3143 if (rc < 0) {
3144 printk(IBM_ERR "fan watchdog: error %d while enabling fan, "
3145 "will try again later...\n", -rc);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003146 /* reschedule for later */
3147 fan_watchdog_reset();
3148 }
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003149}
3150
3151static void fan_watchdog_reset(void)
3152{
3153 static int fan_watchdog_active = 0;
3154
3155 if (fan_watchdog_active)
3156 cancel_delayed_work(&fan_watchdog_task);
3157
3158 if (fan_watchdog_maxinterval > 0) {
3159 fan_watchdog_active = 1;
3160 if (!schedule_delayed_work(&fan_watchdog_task,
3161 msecs_to_jiffies(fan_watchdog_maxinterval
3162 * 1000))) {
3163 printk(IBM_ERR "failed to schedule the fan watchdog, "
3164 "watchdog will not trigger\n");
3165 }
3166 } else
3167 fan_watchdog_active = 0;
3168}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003169
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003170static int fan_set_level(int level)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003171{
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003172 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003173 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003174 if (level >= 0 && level <= 7) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003175 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003176 return -EIO;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003177 } else
3178 return -EINVAL;
3179 break;
3180
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003181 case TPACPI_FAN_WR_ACPI_FANS:
3182 case TPACPI_FAN_WR_TPEC:
3183 if ((level != TP_EC_FAN_AUTO) &&
3184 (level != TP_EC_FAN_FULLSPEED) &&
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02003185 ((level < 0) || (level > 7)))
3186 return -EINVAL;
3187
Henrique de Moraes Holschuheaa75712007-04-24 11:48:18 -03003188 /* safety net should the EC not support AUTO
3189 * or FULLSPEED mode bits and just ignore them */
3190 if (level & TP_EC_FAN_FULLSPEED)
3191 level |= 7; /* safety min speed 7 */
3192 else if (level & TP_EC_FAN_FULLSPEED)
3193 level |= 4; /* safety min speed 4 */
3194
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003195 if (!acpi_ec_write(fan_status_offset, level))
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02003196 return -EIO;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02003197 else
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003198 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02003199 break;
3200
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003201 default:
3202 return -ENXIO;
3203 }
3204 return 0;
3205}
3206
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003207static int fan_set_level_safe(int level)
3208{
3209 int rc;
3210
3211 rc = mutex_lock_interruptible(&fan_mutex);
3212 if (rc < 0)
3213 return rc;
3214
3215 if (level == TPACPI_FAN_LAST_LEVEL)
3216 level = fan_control_desired_level;
3217
3218 rc = fan_set_level(level);
3219 if (!rc)
3220 fan_update_desired_level(level);
3221
3222 mutex_unlock(&fan_mutex);
3223 return rc;
3224}
3225
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003226static int fan_set_enable(void)
3227{
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02003228 u8 s;
3229 int rc;
3230
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003231 rc = mutex_lock_interruptible(&fan_mutex);
3232 if (rc < 0)
3233 return rc;
3234
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003235 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003236 case TPACPI_FAN_WR_ACPI_FANS:
3237 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003238 rc = fan_get_status(&s);
3239 if (rc < 0)
3240 break;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02003241
3242 /* Don't go out of emergency fan mode */
Henrique de Moraes Holschuheaa75712007-04-24 11:48:18 -03003243 if (s != 7) {
3244 s &= 0x07;
3245 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
3246 }
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02003247
3248 if (!acpi_ec_write(fan_status_offset, s))
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003249 rc = -EIO;
3250 else {
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003251 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003252 rc = 0;
3253 }
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02003254 break;
3255
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003256 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003257 rc = fan_get_status(&s);
3258 if (rc < 0)
3259 break;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02003260
3261 s &= 0x07;
3262
3263 /* Set fan to at least level 4 */
Henrique de Moraes Holschuheaa75712007-04-24 11:48:18 -03003264 s |= 4;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02003265
3266 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003267 rc= -EIO;
3268 else
3269 rc = 0;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003270 break;
3271
3272 default:
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003273 rc = -ENXIO;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003274 }
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003275
3276 mutex_unlock(&fan_mutex);
3277 return rc;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003278}
3279
3280static int fan_set_disable(void)
3281{
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003282 int rc;
3283
3284 rc = mutex_lock_interruptible(&fan_mutex);
3285 if (rc < 0)
3286 return rc;
3287
3288 rc = 0;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003289 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003290 case TPACPI_FAN_WR_ACPI_FANS:
3291 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003292 if (!acpi_ec_write(fan_status_offset, 0x00))
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003293 rc = -EIO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003294 else {
3295 fan_control_desired_level = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003296 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003297 }
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003298 break;
3299
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003300 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02003301 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003302 rc = -EIO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003303 else
3304 fan_control_desired_level = 0;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02003305 break;
3306
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003307 default:
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003308 rc = -ENXIO;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003309 }
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003310
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003311
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003312 mutex_unlock(&fan_mutex);
3313 return rc;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003314}
3315
3316static int fan_set_speed(int speed)
3317{
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003318 int rc;
3319
3320 rc = mutex_lock_interruptible(&fan_mutex);
3321 if (rc < 0)
3322 return rc;
3323
3324 rc = 0;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003325 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003326 case TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003327 if (speed >= 0 && speed <= 65535) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003328 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
3329 speed, speed, speed))
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003330 rc = -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003331 } else
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003332 rc = -EINVAL;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003333 break;
3334
3335 default:
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003336 rc = -ENXIO;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003337 }
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003338
3339 mutex_unlock(&fan_mutex);
3340 return rc;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003341}
3342
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003343static int fan_read(char *p)
3344{
3345 int len = 0;
3346 int rc;
3347 u8 status;
3348 unsigned int speed = 0;
3349
3350 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003351 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003352 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003353 if ((rc = fan_get_status_safe(&status)) < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003354 return rc;
3355
3356 len += sprintf(p + len, "status:\t\t%s\n"
3357 "level:\t\t%d\n",
3358 (status != 0) ? "enabled" : "disabled", status);
3359 break;
3360
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003361 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003362 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003363 if ((rc = fan_get_status_safe(&status)) < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003364 return rc;
3365
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003366 if (unlikely(tp_features.fan_ctrl_status_undef)) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003367 if (status != fan_control_initial_status)
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003368 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003369 else
3370 /* Return most likely status. In fact, it
3371 * might be the only possible status */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003372 status = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003373 }
3374
3375 len += sprintf(p + len, "status:\t\t%s\n",
3376 (status != 0) ? "enabled" : "disabled");
3377
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003378 if ((rc = fan_get_speed(&speed)) < 0)
3379 return rc;
3380
3381 len += sprintf(p + len, "speed:\t\t%d\n", speed);
3382
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003383 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003384 /* Disengaged mode takes precedence */
3385 len += sprintf(p + len, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003386 else if (status & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003387 len += sprintf(p + len, "level:\t\tauto\n");
3388 else
3389 len += sprintf(p + len, "level:\t\t%d\n", status);
3390 break;
3391
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003392 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003393 default:
3394 len += sprintf(p + len, "status:\t\tnot supported\n");
3395 }
3396
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003397 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003398 len += sprintf(p + len, "commands:\tlevel <level>");
3399
3400 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003401 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003402 len += sprintf(p + len, " (<level> is 0-7)\n");
3403 break;
3404
3405 default:
3406 len += sprintf(p + len, " (<level> is 0-7, "
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003407 "auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003408 break;
3409 }
3410 }
3411
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003412 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003413 len += sprintf(p + len, "commands:\tenable, disable\n"
3414 "commands:\twatchdog <timeout> (<timeout> is 0 (off), "
3415 "1-120 (seconds))\n");
3416
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003417 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003418 len += sprintf(p + len, "commands:\tspeed <speed>"
3419 " (<speed> is 0-65535)\n");
3420
3421 return len;
3422}
3423
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003424static int fan_write_cmd_level(const char *cmd, int *rc)
3425{
3426 int level;
3427
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02003428 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003429 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003430 else if ((strlencmp(cmd, "level disengaged") == 0) |
3431 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003432 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02003433 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003434 return 0;
3435
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003436 if ((*rc = fan_set_level_safe(level)) == -ENXIO)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003437 printk(IBM_ERR "level command accepted for unsupported "
3438 "access mode %d", fan_control_access_mode);
3439
3440 return 1;
3441}
3442
3443static int fan_write_cmd_enable(const char *cmd, int *rc)
3444{
3445 if (strlencmp(cmd, "enable") != 0)
3446 return 0;
3447
3448 if ((*rc = fan_set_enable()) == -ENXIO)
3449 printk(IBM_ERR "enable command accepted for unsupported "
3450 "access mode %d", fan_control_access_mode);
3451
3452 return 1;
3453}
3454
3455static int fan_write_cmd_disable(const char *cmd, int *rc)
3456{
3457 if (strlencmp(cmd, "disable") != 0)
3458 return 0;
3459
3460 if ((*rc = fan_set_disable()) == -ENXIO)
3461 printk(IBM_ERR "disable command accepted for unsupported "
3462 "access mode %d", fan_control_access_mode);
3463
3464 return 1;
3465}
3466
3467static int fan_write_cmd_speed(const char *cmd, int *rc)
3468{
3469 int speed;
3470
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02003471 /* TODO:
3472 * Support speed <low> <medium> <high> ? */
3473
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003474 if (sscanf(cmd, "speed %d", &speed) != 1)
3475 return 0;
3476
3477 if ((*rc = fan_set_speed(speed)) == -ENXIO)
3478 printk(IBM_ERR "speed command accepted for unsupported "
3479 "access mode %d", fan_control_access_mode);
3480
3481 return 1;
3482}
3483
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003484static int fan_write_cmd_watchdog(const char *cmd, int *rc)
3485{
3486 int interval;
3487
3488 if (sscanf(cmd, "watchdog %d", &interval) != 1)
3489 return 0;
3490
3491 if (interval < 0 || interval > 120)
3492 *rc = -EINVAL;
3493 else
3494 fan_watchdog_maxinterval = interval;
3495
3496 return 1;
3497}
3498
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003499static int fan_write(char *buf)
3500{
3501 char *cmd;
3502 int rc = 0;
3503
3504 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003505 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003506 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003507 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003508 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003509 fan_write_cmd_disable(cmd, &rc) ||
3510 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003511 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003512 fan_write_cmd_speed(cmd, &rc))
3513 )
3514 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003515 else if (!rc)
3516 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003517 }
3518
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003519 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003520}
3521
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003522static struct ibm_struct fan_driver_data = {
3523 .name = "fan",
3524 .read = fan_read,
3525 .write = fan_write,
3526 .exit = fan_exit,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03003527 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003528};
3529
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003530/****************************************************************************
3531 ****************************************************************************
3532 *
3533 * Infrastructure
3534 *
3535 ****************************************************************************
3536 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003538/* /proc support */
3539static struct proc_dir_entry *proc_dir = NULL;
3540
3541/* Subdriver registry */
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003542static LIST_HEAD(tpacpi_all_drivers);
3543
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003545/*
3546 * Module and infrastructure proble, init and exit handling
3547 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003549#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003550static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003551{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003552 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003553
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003554 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003555}
3556#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
3557
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003558static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559{
3560 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003561 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562 struct proc_dir_entry *entry;
3563
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003564 BUG_ON(ibm == NULL);
3565
3566 INIT_LIST_HEAD(&ibm->all_drivers);
3567
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03003568 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569 return 0;
3570
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003571 dbg_printk(TPACPI_DBG_INIT,
3572 "probing for %s\n", ibm->name);
3573
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003574 if (iibm->init) {
3575 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003576 if (ret > 0)
3577 return 0; /* probe failed */
3578 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003580
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03003581 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582 }
3583
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003584 if (ibm->acpi) {
3585 if (ibm->acpi->hid) {
3586 ret = register_tpacpi_subdriver(ibm);
3587 if (ret)
3588 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003589 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003590
3591 if (ibm->acpi->notify) {
3592 ret = setup_acpi_notify(ibm);
3593 if (ret == -ENODEV) {
3594 printk(IBM_NOTICE "disabling subdriver %s\n",
3595 ibm->name);
3596 ret = 0;
3597 goto err_out;
3598 }
3599 if (ret < 0)
3600 goto err_out;
3601 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003602 }
3603
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003604 dbg_printk(TPACPI_DBG_INIT,
3605 "%s installed\n", ibm->name);
3606
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003607 if (ibm->read) {
3608 entry = create_proc_entry(ibm->name,
3609 S_IFREG | S_IRUGO | S_IWUSR,
3610 proc_dir);
3611 if (!entry) {
3612 printk(IBM_ERR "unable to create proc entry %s\n",
3613 ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003614 ret = -ENODEV;
3615 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003616 }
3617 entry->owner = THIS_MODULE;
3618 entry->data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003619 entry->read_proc = &dispatch_procfs_read;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003620 if (ibm->write)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003621 entry->write_proc = &dispatch_procfs_write;
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03003622 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003623 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003625 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
3626
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003628
3629err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003630 dbg_printk(TPACPI_DBG_INIT,
3631 "%s: at error exit path with result %d\n",
3632 ibm->name, ret);
3633
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003634 ibm_exit(ibm);
3635 return (ret < 0)? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636}
3637
3638static void ibm_exit(struct ibm_struct *ibm)
3639{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003640 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003641
3642 list_del_init(&ibm->all_drivers);
3643
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003644 if (ibm->flags.acpi_notify_installed) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003645 dbg_printk(TPACPI_DBG_EXIT,
3646 "%s: acpi_remove_notify_handler\n", ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003647 BUG_ON(!ibm->acpi);
3648 acpi_remove_notify_handler(*ibm->acpi->handle,
3649 ibm->acpi->type,
3650 dispatch_acpi_notify);
3651 ibm->flags.acpi_notify_installed = 0;
3652 ibm->flags.acpi_notify_installed = 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003653 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03003655 if (ibm->flags.proc_created) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003656 dbg_printk(TPACPI_DBG_EXIT,
3657 "%s: remove_proc_entry\n", ibm->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658 remove_proc_entry(ibm->name, proc_dir);
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03003659 ibm->flags.proc_created = 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003660 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003662 if (ibm->flags.acpi_driver_registered) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003663 dbg_printk(TPACPI_DBG_EXIT,
3664 "%s: acpi_bus_unregister_driver\n", ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003665 BUG_ON(!ibm->acpi);
3666 acpi_bus_unregister_driver(ibm->acpi->driver);
3667 kfree(ibm->acpi->driver);
3668 ibm->acpi->driver = NULL;
3669 ibm->flags.acpi_driver_registered = 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003670 }
3671
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03003672 if (ibm->flags.init_called && ibm->exit) {
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003673 ibm->exit();
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03003674 ibm->flags.init_called = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003676
3677 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678}
3679
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003680/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003682static char *ibm_thinkpad_ec_found = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02003684static char* __init check_dmi_for_ec(void)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003685{
3686 struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02003687 char ec_fw_string[18];
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003688
3689 /*
3690 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
3691 * X32 or newer, all Z series; Some models must have an
3692 * up-to-date BIOS or they will not be detected.
3693 *
3694 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
3695 */
3696 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02003697 if (sscanf(dev->name,
3698 "IBM ThinkPad Embedded Controller -[%17c",
3699 ec_fw_string) == 1) {
3700 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
3701 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
3702 return kstrdup(ec_fw_string, GFP_KERNEL);
3703 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003704 }
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02003705 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706}
3707
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003708static int __init probe_for_thinkpad(void)
3709{
3710 int is_thinkpad;
3711
3712 if (acpi_disabled)
3713 return -ENODEV;
3714
3715 /*
3716 * Non-ancient models have better DMI tagging, but very old models
3717 * don't.
3718 */
3719 is_thinkpad = dmi_name_in_vendors("ThinkPad");
3720
3721 /* ec is required because many other handles are relative to it */
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003722 IBM_ACPIHANDLE_INIT(ec);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003723 if (!ec_handle) {
3724 if (is_thinkpad)
3725 printk(IBM_ERR
3726 "Not yet supported ThinkPad detected!\n");
3727 return -ENODEV;
3728 }
3729
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03003730 /*
3731 * Risks a regression on very old machines, but reduces potential
3732 * false positives a damn great deal
3733 */
3734 if (!is_thinkpad)
3735 is_thinkpad = dmi_name_in_vendors("IBM");
3736
3737 if (!is_thinkpad && !force_load)
3738 return -ENODEV;
3739
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003740 return 0;
3741}
3742
3743
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003744/* Module init, exit, parameters */
3745
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003746static struct ibm_init_struct ibms_init[] __initdata = {
3747 {
3748 .init = thinkpad_acpi_driver_init,
3749 .data = &thinkpad_acpi_driver_data,
3750 },
3751 {
3752 .init = hotkey_init,
3753 .data = &hotkey_driver_data,
3754 },
3755 {
3756 .init = bluetooth_init,
3757 .data = &bluetooth_driver_data,
3758 },
3759 {
3760 .init = wan_init,
3761 .data = &wan_driver_data,
3762 },
3763 {
3764 .init = video_init,
3765 .data = &video_driver_data,
3766 },
3767 {
3768 .init = light_init,
3769 .data = &light_driver_data,
3770 },
3771#ifdef CONFIG_THINKPAD_ACPI_DOCK
3772 {
3773 .init = dock_init,
3774 .data = &dock_driver_data[0],
3775 },
3776 {
3777 .data = &dock_driver_data[1],
3778 },
3779#endif
3780#ifdef CONFIG_THINKPAD_ACPI_BAY
3781 {
3782 .init = bay_init,
3783 .data = &bay_driver_data,
3784 },
3785#endif
3786 {
3787 .init = cmos_init,
3788 .data = &cmos_driver_data,
3789 },
3790 {
3791 .init = led_init,
3792 .data = &led_driver_data,
3793 },
3794 {
3795 .init = beep_init,
3796 .data = &beep_driver_data,
3797 },
3798 {
3799 .init = thermal_init,
3800 .data = &thermal_driver_data,
3801 },
3802 {
3803 .data = &ecdump_driver_data,
3804 },
3805 {
3806 .init = brightness_init,
3807 .data = &brightness_driver_data,
3808 },
3809 {
3810 .data = &volume_driver_data,
3811 },
3812 {
3813 .init = fan_init,
3814 .data = &fan_driver_data,
3815 },
3816};
3817
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003818static int __init set_ibm_param(const char *val, struct kernel_param *kp)
3819{
3820 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003821 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003822
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003823 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
3824 ibm = ibms_init[i].data;
3825 BUG_ON(ibm == NULL);
3826
3827 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
3828 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003829 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003830 strcpy(ibms_init[i].param, val);
3831 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003832 return 0;
3833 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003834 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003835
3836 return -EINVAL;
3837}
3838
3839static int experimental;
3840module_param(experimental, int, 0);
3841
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03003842static u32 dbg_level;
3843module_param_named(debug, dbg_level, uint, 0);
3844
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03003845static int force_load;
3846module_param(force_load, int, 0);
3847
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003848#define IBM_PARAM(feature) \
3849 module_param_call(feature, set_ibm_param, NULL, NULL, 0)
3850
3851IBM_PARAM(hotkey);
3852IBM_PARAM(bluetooth);
3853IBM_PARAM(video);
3854IBM_PARAM(light);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003855#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003856IBM_PARAM(dock);
3857#endif
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003858#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003859IBM_PARAM(bay);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003860#endif /* CONFIG_THINKPAD_ACPI_BAY */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003861IBM_PARAM(cmos);
3862IBM_PARAM(led);
3863IBM_PARAM(beep);
3864IBM_PARAM(ecdump);
3865IBM_PARAM(brightness);
3866IBM_PARAM(volume);
3867IBM_PARAM(fan);
3868
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03003869static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870{
3871 int ret, i;
3872
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03003873 /* Driver-level probe */
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003874 ret = probe_for_thinkpad();
3875 if (ret)
3876 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03003878 /* Driver initialization */
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003879 ibm_thinkpad_ec_found = check_dmi_for_ec();
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003880 IBM_ACPIHANDLE_INIT(ecrd);
3881 IBM_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003883 proc_dir = proc_mkdir(IBM_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884 if (!proc_dir) {
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003885 printk(IBM_ERR "unable to create proc dir " IBM_PROC_DIR);
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03003886 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887 return -ENODEV;
3888 }
3889 proc_dir->owner = THIS_MODULE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003890
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03003891 ret = platform_driver_register(&tpacpi_pdriver);
3892 if (ret) {
3893 printk(IBM_ERR "unable to register platform driver\n");
3894 thinkpad_acpi_module_exit();
3895 return ret;
3896 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03003897 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
3898 if (ret) {
3899 printk(IBM_ERR "unable to create sysfs driver attributes\n");
3900 thinkpad_acpi_module_exit();
3901 return ret;
3902 }
3903
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03003904
3905 /* Device initialization */
3906 tpacpi_pdev = platform_device_register_simple(IBM_DRVR_NAME, -1,
3907 NULL, 0);
3908 if (IS_ERR(tpacpi_pdev)) {
3909 ret = PTR_ERR(tpacpi_pdev);
3910 tpacpi_pdev = NULL;
3911 printk(IBM_ERR "unable to register platform device\n");
3912 thinkpad_acpi_module_exit();
3913 return ret;
3914 }
3915 tpacpi_hwmon = hwmon_device_register(&tpacpi_pdev->dev);
3916 if (IS_ERR(tpacpi_hwmon)) {
3917 ret = PTR_ERR(tpacpi_hwmon);
3918 tpacpi_hwmon = NULL;
3919 printk(IBM_ERR "unable to register hwmon device\n");
3920 thinkpad_acpi_module_exit();
3921 return ret;
3922 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003923 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
3924 ret = ibm_init(&ibms_init[i]);
3925 if (ret >= 0 && *ibms_init[i].param)
3926 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03003928 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929 return ret;
3930 }
3931 }
3932
3933 return 0;
3934}
3935
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03003936static void thinkpad_acpi_module_exit(void)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003937{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003938 struct ibm_struct *ibm, *itmp;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003939
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003940 list_for_each_entry_safe_reverse(ibm, itmp,
3941 &tpacpi_all_drivers,
3942 all_drivers) {
3943 ibm_exit(ibm);
3944 }
3945
3946 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003947
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03003948 if (tpacpi_hwmon)
3949 hwmon_device_unregister(tpacpi_hwmon);
3950
3951 if (tpacpi_pdev)
3952 platform_device_unregister(tpacpi_pdev);
3953
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03003954 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03003955 platform_driver_unregister(&tpacpi_pdriver);
3956
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003957 if (proc_dir)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003958 remove_proc_entry(IBM_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003959
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003960 kfree(ibm_thinkpad_ec_found);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003961}
3962
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03003963module_init(thinkpad_acpi_module_init);
3964module_exit(thinkpad_acpi_module_exit);