blob: 8c088687e9543f3afaa5aa0e9063b9a89746eff3 [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
Henrique de Moraes Holschuhb964b432007-07-18 23:45:26 -030095/*
96 * DMI matching for module autoloading
97 *
98 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
99 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
100 *
101 * Only models listed in thinkwiki will be supported, so add yours
102 * if it is not there yet.
103 */
104#define IBM_BIOS_MODULE_ALIAS(__type) \
105 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW")
106
107/* Non-ancient thinkpads */
108MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
109MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
110
111/* Ancient thinkpad BIOSes have to be identified by
112 * BIOS type or model number, and there are far less
113 * BIOS types than model numbers... */
114IBM_BIOS_MODULE_ALIAS("I[B,D,H,I,M,N,O,T,W,V,Y,Z]");
115IBM_BIOS_MODULE_ALIAS("1[0,3,6,8,A-G,I,K,M-P,S,T]");
116IBM_BIOS_MODULE_ALIAS("K[U,X-Z]");
117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118#define __unused __attribute__ ((unused))
119
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300120/****************************************************************************
121 ****************************************************************************
122 *
123 * ACPI Helpers and device model
124 *
125 ****************************************************************************
126 ****************************************************************************/
127
128/*************************************************************************
129 * ACPI basic handles
130 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -0300132static acpi_handle root_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134#define IBM_HANDLE(object, parent, paths...) \
135 static acpi_handle object##_handle; \
136 static acpi_handle *object##_parent = &parent##_handle; \
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400137 static char *object##_path; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 static char *object##_paths[] = { paths }
139
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400140IBM_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
141 "\\_SB.PCI.ISA.EC", /* 570 */
142 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
143 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
144 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
145 "\\_SB.PCI0.ICH3.EC0", /* R31 */
146 "\\_SB.PCI0.LPC.EC", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300147 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300149IBM_HANDLE(ecrd, ec, "ECRD"); /* 570 */
150IBM_HANDLE(ecwr, ec, "ECWR"); /* 570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300152
153/*************************************************************************
154 * Misc ACPI handles
155 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400157IBM_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, T4x, X31, X40 */
158 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
159 "\\CMS", /* R40, R40e */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300160 ); /* all others */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400161
162IBM_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
163 "^HKEY", /* R30, R31 */
164 "HKEY", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300165 ); /* 570 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400166
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400167
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300168/*************************************************************************
169 * ACPI helpers
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200170 */
171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172static int acpi_evalf(acpi_handle handle,
173 void *res, char *method, char *fmt, ...)
174{
175 char *fmt0 = fmt;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400176 struct acpi_object_list params;
177 union acpi_object in_objs[IBM_MAX_ACPI_ARGS];
178 struct acpi_buffer result, *resultp;
179 union acpi_object out_obj;
180 acpi_status status;
181 va_list ap;
182 char res_type;
183 int success;
184 int quiet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
186 if (!*fmt) {
187 printk(IBM_ERR "acpi_evalf() called with empty format\n");
188 return 0;
189 }
190
191 if (*fmt == 'q') {
192 quiet = 1;
193 fmt++;
194 } else
195 quiet = 0;
196
197 res_type = *(fmt++);
198
199 params.count = 0;
200 params.pointer = &in_objs[0];
201
202 va_start(ap, fmt);
203 while (*fmt) {
204 char c = *(fmt++);
205 switch (c) {
206 case 'd': /* int */
207 in_objs[params.count].integer.value = va_arg(ap, int);
208 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
209 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400210 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 default:
212 printk(IBM_ERR "acpi_evalf() called "
213 "with invalid format character '%c'\n", c);
214 return 0;
215 }
216 }
217 va_end(ap);
218
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400219 if (res_type != 'v') {
220 result.length = sizeof(out_obj);
221 result.pointer = &out_obj;
222 resultp = &result;
223 } else
224 resultp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400226 status = acpi_evaluate_object(handle, method, &params, resultp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228 switch (res_type) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400229 case 'd': /* int */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 if (res)
231 *(int *)res = out_obj.integer.value;
232 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
233 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400234 case 'v': /* void */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 success = status == AE_OK;
236 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400237 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 default:
239 printk(IBM_ERR "acpi_evalf() called "
240 "with invalid format character '%c'\n", res_type);
241 return 0;
242 }
243
244 if (!success && !quiet)
245 printk(IBM_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
246 method, fmt0, status);
247
248 return success;
249}
250
251static void __unused acpi_print_int(acpi_handle handle, char *method)
252{
253 int i;
254
255 if (acpi_evalf(handle, &i, method, "d"))
256 printk(IBM_INFO "%s = 0x%x\n", method, i);
257 else
258 printk(IBM_ERR "error calling %s\n", method);
259}
260
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300261static int acpi_ec_read(int i, u8 * p)
262{
263 int v;
264
265 if (ecrd_handle) {
266 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
267 return 0;
268 *p = v;
269 } else {
270 if (ec_read(i, p) < 0)
271 return 0;
272 }
273
274 return 1;
275}
276
277static int acpi_ec_write(int i, u8 v)
278{
279 if (ecwr_handle) {
280 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
281 return 0;
282 } else {
283 if (ec_write(i, v) < 0)
284 return 0;
285 }
286
287 return 1;
288}
289
290static int _sta(acpi_handle handle)
291{
292 int status;
293
294 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
295 status = 0;
296
297 return status;
298}
299
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -0300300static int issue_thinkpad_cmos_command(int cmos_cmd)
301{
302 if (!cmos_handle)
303 return -ENXIO;
304
305 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
306 return -EIO;
307
308 return 0;
309}
310
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300311/*************************************************************************
312 * ACPI device model
313 */
314
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300315static void drv_acpi_handle_init(char *name,
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300316 acpi_handle *handle, acpi_handle parent,
317 char **paths, int num_paths, char **path)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300318{
319 int i;
320 acpi_status status;
321
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300322 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
323 name);
324
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300325 for (i = 0; i < num_paths; i++) {
326 status = acpi_get_handle(parent, paths[i], handle);
327 if (ACPI_SUCCESS(status)) {
328 *path = paths[i];
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300329 dbg_printk(TPACPI_DBG_INIT,
330 "Found ACPI handle %s for %s\n",
331 *path, name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300332 return;
333 }
334 }
335
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300336 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
337 name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300338 *handle = NULL;
339}
340
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300341static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300342{
343 struct ibm_struct *ibm = data;
344
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300345 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300346 return;
347
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300348 ibm->acpi->notify(ibm, event);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300349}
350
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300351static int __init setup_acpi_notify(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300352{
353 acpi_status status;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300354 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300355
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300356 BUG_ON(!ibm->acpi);
357
358 if (!*ibm->acpi->handle)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300359 return 0;
360
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300361 vdbg_printk(TPACPI_DBG_INIT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300362 "setting up ACPI notify for %s\n", ibm->name);
363
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300364 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
365 if (rc < 0) {
366 printk(IBM_ERR "acpi_bus_get_device(%s) failed: %d\n",
367 ibm->name, rc);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300368 return -ENODEV;
369 }
370
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300371 acpi_driver_data(ibm->acpi->device) = ibm;
372 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300373 IBM_ACPI_EVENT_PREFIX,
374 ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300375
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300376 status = acpi_install_notify_handler(*ibm->acpi->handle,
377 ibm->acpi->type, dispatch_acpi_notify, ibm);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300378 if (ACPI_FAILURE(status)) {
379 if (status == AE_ALREADY_EXISTS) {
380 printk(IBM_NOTICE "another device driver is already handling %s events\n",
381 ibm->name);
382 } else {
383 printk(IBM_ERR "acpi_install_notify_handler(%s) failed: %d\n",
384 ibm->name, status);
385 }
386 return -ENODEV;
387 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300388 ibm->flags.acpi_notify_installed = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300389 return 0;
390}
391
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300392static int __init tpacpi_device_add(struct acpi_device *device)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300393{
394 return 0;
395}
396
Henrique de Moraes Holschuh67001212007-04-21 11:08:25 -0300397static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300398{
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300399 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300400
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300401 dbg_printk(TPACPI_DBG_INIT,
402 "registering %s as an ACPI driver\n", ibm->name);
403
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300404 BUG_ON(!ibm->acpi);
405
406 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
407 if (!ibm->acpi->driver) {
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300408 printk(IBM_ERR "kzalloc(ibm->driver) failed\n");
409 return -ENOMEM;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300410 }
411
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300412 sprintf(ibm->acpi->driver->name, "%s_%s", IBM_NAME, ibm->name);
413 ibm->acpi->driver->ids = ibm->acpi->hid;
414 ibm->acpi->driver->ops.add = &tpacpi_device_add;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300415
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300416 rc = acpi_bus_register_driver(ibm->acpi->driver);
417 if (rc < 0) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300418 printk(IBM_ERR "acpi_bus_register_driver(%s) failed: %d\n",
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300419 ibm->acpi->hid, rc);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300420 kfree(ibm->acpi->driver);
421 ibm->acpi->driver = NULL;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300422 } else if (!rc)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300423 ibm->flags.acpi_driver_registered = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300424
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300425 return rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300426}
427
428
429/****************************************************************************
430 ****************************************************************************
431 *
432 * Procfs Helpers
433 *
434 ****************************************************************************
435 ****************************************************************************/
436
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300437static int dispatch_procfs_read(char *page, char **start, off_t off,
438 int count, int *eof, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300439{
440 struct ibm_struct *ibm = data;
441 int len;
442
443 if (!ibm || !ibm->read)
444 return -EINVAL;
445
446 len = ibm->read(page);
447 if (len < 0)
448 return len;
449
450 if (len <= off + count)
451 *eof = 1;
452 *start = page + off;
453 len -= off;
454 if (len > count)
455 len = count;
456 if (len < 0)
457 len = 0;
458
459 return len;
460}
461
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300462static int dispatch_procfs_write(struct file *file,
463 const char __user * userbuf,
464 unsigned long count, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300465{
466 struct ibm_struct *ibm = data;
467 char *kernbuf;
468 int ret;
469
470 if (!ibm || !ibm->write)
471 return -EINVAL;
472
473 kernbuf = kmalloc(count + 2, GFP_KERNEL);
474 if (!kernbuf)
475 return -ENOMEM;
476
477 if (copy_from_user(kernbuf, userbuf, count)) {
478 kfree(kernbuf);
479 return -EFAULT;
480 }
481
482 kernbuf[count] = 0;
483 strcat(kernbuf, ",");
484 ret = ibm->write(kernbuf);
485 if (ret == 0)
486 ret = count;
487
488 kfree(kernbuf);
489
490 return ret;
491}
492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493static char *next_cmd(char **cmds)
494{
495 char *start = *cmds;
496 char *end;
497
498 while ((end = strchr(start, ',')) && end == start)
499 start = end + 1;
500
501 if (!end)
502 return NULL;
503
504 *end = 0;
505 *cmds = end + 1;
506 return start;
507}
508
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300509
510/****************************************************************************
511 ****************************************************************************
512 *
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300513 * Device model: hwmon and platform
514 *
515 ****************************************************************************
516 ****************************************************************************/
517
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -0300518static struct platform_device *tpacpi_pdev;
519static struct class_device *tpacpi_hwmon;
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300520
521static struct platform_driver tpacpi_pdriver = {
522 .driver = {
523 .name = IBM_DRVR_NAME,
524 .owner = THIS_MODULE,
525 },
526};
527
528
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -0300529/*************************************************************************
530 * thinkpad-acpi driver attributes
531 */
532
533/* interface_version --------------------------------------------------- */
534static ssize_t tpacpi_driver_interface_version_show(
535 struct device_driver *drv,
536 char *buf)
537{
538 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
539}
540
541static DRIVER_ATTR(interface_version, S_IRUGO,
542 tpacpi_driver_interface_version_show, NULL);
543
544/* debug_level --------------------------------------------------------- */
545static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
546 char *buf)
547{
548 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
549}
550
551static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
552 const char *buf, size_t count)
553{
554 unsigned long t;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -0300555
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300556 if (parse_strtoul(buf, 0xffff, &t))
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -0300557 return -EINVAL;
558
559 dbg_level = t;
560
561 return count;
562}
563
564static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
565 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
566
567/* version ------------------------------------------------------------- */
568static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
569 char *buf)
570{
571 return snprintf(buf, PAGE_SIZE, "%s v%s\n", IBM_DESC, IBM_VERSION);
572}
573
574static DRIVER_ATTR(version, S_IRUGO,
575 tpacpi_driver_version_show, NULL);
576
577/* --------------------------------------------------------------------- */
578
579static struct driver_attribute* tpacpi_driver_attributes[] = {
580 &driver_attr_debug_level, &driver_attr_version,
581 &driver_attr_interface_version,
582};
583
584static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
585{
586 int i, res;
587
588 i = 0;
589 res = 0;
590 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
591 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
592 i++;
593 }
594
595 return res;
596}
597
598static void tpacpi_remove_driver_attributes(struct device_driver *drv)
599{
600 int i;
601
602 for(i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
603 driver_remove_file(drv, tpacpi_driver_attributes[i]);
604}
605
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300606/*************************************************************************
607 * sysfs support helpers
608 */
609
610struct attribute_set_obj {
611 struct attribute_set s;
612 struct attribute *a;
613} __attribute__((packed));
614
615static struct attribute_set *create_attr_set(unsigned int max_members,
616 const char* name)
617{
618 struct attribute_set_obj *sobj;
619
620 if (max_members == 0)
621 return NULL;
622
623 /* Allocates space for implicit NULL at the end too */
624 sobj = kzalloc(sizeof(struct attribute_set_obj) +
625 max_members * sizeof(struct attribute *),
626 GFP_KERNEL);
627 if (!sobj)
628 return NULL;
629 sobj->s.max_members = max_members;
630 sobj->s.group.attrs = &sobj->a;
631 sobj->s.group.name = name;
632
633 return &sobj->s;
634}
635
636/* not multi-threaded safe, use it in a single thread per set */
637static int add_to_attr_set(struct attribute_set* s, struct attribute *attr)
638{
639 if (!s || !attr)
640 return -EINVAL;
641
642 if (s->members >= s->max_members)
643 return -ENOMEM;
644
645 s->group.attrs[s->members] = attr;
646 s->members++;
647
648 return 0;
649}
650
651static int add_many_to_attr_set(struct attribute_set* s,
652 struct attribute **attr,
653 unsigned int count)
654{
655 int i, res;
656
657 for (i = 0; i < count; i++) {
658 res = add_to_attr_set(s, attr[i]);
659 if (res)
660 return res;
661 }
662
663 return 0;
664}
665
666static void delete_attr_set(struct attribute_set* s, struct kobject *kobj)
667{
668 sysfs_remove_group(kobj, &s->group);
669 destroy_attr_set(s);
670}
671
672static int parse_strtoul(const char *buf,
673 unsigned long max, unsigned long *value)
674{
675 char *endp;
676
677 *value = simple_strtoul(buf, &endp, 0);
678 while (*endp && isspace(*endp))
679 endp++;
680 if (*endp || *value > max)
681 return -EINVAL;
682
683 return 0;
684}
685
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300686/****************************************************************************
687 ****************************************************************************
688 *
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300689 * Subdrivers
690 *
691 ****************************************************************************
692 ****************************************************************************/
693
694/*************************************************************************
Henrique de Moraes Holschuh142cfc92007-04-21 11:08:26 -0300695 * thinkpad-acpi init subdriver
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300696 */
697
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300698static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{
700 printk(IBM_INFO "%s v%s\n", IBM_DESC, IBM_VERSION);
701 printk(IBM_INFO "%s\n", IBM_URL);
702
Henrique de Moraes Holschuh3945ac32007-02-06 19:13:44 -0200703 if (ibm_thinkpad_ec_found)
704 printk(IBM_INFO "ThinkPad EC firmware %s\n",
705 ibm_thinkpad_ec_found);
706
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 return 0;
708}
709
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300710static int thinkpad_acpi_driver_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
712 int len = 0;
713
714 len += sprintf(p + len, "driver:\t\t%s\n", IBM_DESC);
715 len += sprintf(p + len, "version:\t%s\n", IBM_VERSION);
716
717 return len;
718}
719
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300720static struct ibm_struct thinkpad_acpi_driver_data = {
721 .name = "driver",
722 .read = thinkpad_acpi_driver_read,
723};
724
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300725/*************************************************************************
726 * Hotkey subdriver
727 */
728
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400729static int hotkey_orig_status;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -0300730static u32 hotkey_orig_mask;
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -0300731static u32 hotkey_all_mask;
732static u32 hotkey_reserved_mask = 0x00778000;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400733
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -0300734static struct attribute_set *hotkey_dev_attributes;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300735
736/* sysfs hotkey enable ------------------------------------------------- */
737static ssize_t hotkey_enable_show(struct device *dev,
738 struct device_attribute *attr,
739 char *buf)
740{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -0300741 int res, status;
742 u32 mask;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300743
744 res = hotkey_get(&status, &mask);
745 if (res)
746 return res;
747
748 return snprintf(buf, PAGE_SIZE, "%d\n", status);
749}
750
751static ssize_t hotkey_enable_store(struct device *dev,
752 struct device_attribute *attr,
753 const char *buf, size_t count)
754{
755 unsigned long t;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -0300756 int res, status;
757 u32 mask;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300758
759 if (parse_strtoul(buf, 1, &t))
760 return -EINVAL;
761
762 res = hotkey_get(&status, &mask);
763 if (!res)
764 res = hotkey_set(t, mask);
765
766 return (res) ? res : count;
767}
768
769static struct device_attribute dev_attr_hotkey_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -0300770 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300771 hotkey_enable_show, hotkey_enable_store);
772
773/* sysfs hotkey mask --------------------------------------------------- */
774static ssize_t hotkey_mask_show(struct device *dev,
775 struct device_attribute *attr,
776 char *buf)
777{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -0300778 int res, status;
779 u32 mask;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300780
781 res = hotkey_get(&status, &mask);
782 if (res)
783 return res;
784
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -0300785 return snprintf(buf, PAGE_SIZE, "0x%08x\n", mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300786}
787
788static ssize_t hotkey_mask_store(struct device *dev,
789 struct device_attribute *attr,
790 const char *buf, size_t count)
791{
792 unsigned long t;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -0300793 int res, status;
794 u32 mask;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300795
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -0300796 if (parse_strtoul(buf, 0xffffffffUL, &t))
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300797 return -EINVAL;
798
799 res = hotkey_get(&status, &mask);
800 if (!res)
801 hotkey_set(status, t);
802
803 return (res) ? res : count;
804}
805
806static struct device_attribute dev_attr_hotkey_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -0300807 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300808 hotkey_mask_show, hotkey_mask_store);
809
810/* sysfs hotkey bios_enabled ------------------------------------------- */
811static ssize_t hotkey_bios_enabled_show(struct device *dev,
812 struct device_attribute *attr,
813 char *buf)
814{
815 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
816}
817
818static struct device_attribute dev_attr_hotkey_bios_enabled =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -0300819 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300820
821/* sysfs hotkey bios_mask ---------------------------------------------- */
822static ssize_t hotkey_bios_mask_show(struct device *dev,
823 struct device_attribute *attr,
824 char *buf)
825{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -0300826 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300827}
828
829static struct device_attribute dev_attr_hotkey_bios_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -0300830 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300831
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -0300832/* sysfs hotkey all_mask ----------------------------------------------- */
833static ssize_t hotkey_all_mask_show(struct device *dev,
834 struct device_attribute *attr,
835 char *buf)
836{
837 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_all_mask);
838}
839
840static struct device_attribute dev_attr_hotkey_all_mask =
841 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
842
843/* sysfs hotkey recommended_mask --------------------------------------- */
844static ssize_t hotkey_recommended_mask_show(struct device *dev,
845 struct device_attribute *attr,
846 char *buf)
847{
848 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
849 hotkey_all_mask & ~hotkey_reserved_mask);
850}
851
852static struct device_attribute dev_attr_hotkey_recommended_mask =
853 __ATTR(hotkey_recommended_mask, S_IRUGO,
854 hotkey_recommended_mask_show, NULL);
855
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300856/* --------------------------------------------------------------------- */
857
858static struct attribute *hotkey_mask_attributes[] = {
859 &dev_attr_hotkey_mask.attr,
860 &dev_attr_hotkey_bios_enabled.attr,
861 &dev_attr_hotkey_bios_mask.attr,
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -0300862 &dev_attr_hotkey_all_mask.attr,
863 &dev_attr_hotkey_recommended_mask.attr,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300864};
865
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300866static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300867{
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300868 int res;
869
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300870 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
871
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300872 IBM_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -0300873 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300874
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300875 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300876 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300877
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300878 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300879 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300880
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300881 if (tp_features.hotkey) {
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -0300882 hotkey_dev_attributes = create_attr_set(6, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300883 if (!hotkey_dev_attributes)
884 return -ENOMEM;
885 res = add_to_attr_set(hotkey_dev_attributes,
886 &dev_attr_hotkey_enable.attr);
887 if (res)
888 return res;
889
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300890 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
891 A30, R30, R31, T20-22, X20-21, X22-24 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300892 tp_features.hotkey_mask =
893 acpi_evalf(hkey_handle, NULL, "DHKN", "qv");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300894
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300895 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300896 str_supported(tp_features.hotkey_mask));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300897
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -0300898 if (tp_features.hotkey_mask) {
899 /* MHKA available in A31, R40, R40e, T4x, X31, and later */
900 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
901 "MHKA", "qd"))
902 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
903 }
904
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300905 res = hotkey_get(&hotkey_orig_status, &hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300906 if (!res && tp_features.hotkey_mask) {
907 res = add_many_to_attr_set(hotkey_dev_attributes,
908 hotkey_mask_attributes,
909 ARRAY_SIZE(hotkey_mask_attributes));
910 }
911 if (!res)
912 res = register_attr_set_with_sysfs(
913 hotkey_dev_attributes,
914 &tpacpi_pdev->dev.kobj);
915
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300916 if (res)
917 return res;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300918 }
919
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300920 return (tp_features.hotkey)? 0 : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300921}
922
923static void hotkey_exit(void)
924{
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300925 int res;
926
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300927 if (tp_features.hotkey) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300928 dbg_printk(TPACPI_DBG_EXIT, "restoring original hotkey mask\n");
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300929 res = hotkey_set(hotkey_orig_status, hotkey_orig_mask);
930 if (res)
931 printk(IBM_ERR "failed to restore hotkey to BIOS defaults\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300932 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300933
934 if (hotkey_dev_attributes) {
935 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
936 hotkey_dev_attributes = NULL;
937 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300938}
939
940static void hotkey_notify(struct ibm_struct *ibm, u32 event)
941{
942 int hkey;
943
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -0300944 if (event == 0x80 && acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300945 acpi_bus_generate_event(ibm->acpi->device, event, hkey);
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -0300946 } else {
947 printk(IBM_ERR "unknown hotkey notification event %d\n", event);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300948 acpi_bus_generate_event(ibm->acpi->device, event, 0);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300949 }
950}
951
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -0300952/*
953 * Call with hotkey_mutex held
954 */
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -0300955static int hotkey_get(int *status, u32 *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956{
957 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300958 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400959
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300960 if (tp_features.hotkey_mask)
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400961 if (!acpi_evalf(hkey_handle, mask, "DHKN", "d"))
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300962 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400963
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300964 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965}
966
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -0300967/*
968 * Call with hotkey_mutex held
969 */
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -0300970static int hotkey_set(int status, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971{
972 int i;
973
974 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300975 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300977 if (tp_features.hotkey_mask)
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400978 for (i = 0; i < 32; i++) {
979 int bit = ((1 << i) & mask) != 0;
980 if (!acpi_evalf(hkey_handle,
981 NULL, "MHKM", "vdd", i + 1, bit))
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300982 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400983 }
984
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300985 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400986}
987
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300988/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400989static int hotkey_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -0300991 int res, status;
992 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 int len = 0;
994
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300995 if (!tp_features.hotkey) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400996 len += sprintf(p + len, "status:\t\tnot supported\n");
997 return len;
998 }
999
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001000 res = mutex_lock_interruptible(&hotkey_mutex);
1001 if (res < 0)
1002 return res;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001003 res = hotkey_get(&status, &mask);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001004 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001005 if (res)
1006 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
1008 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001009 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001010 len += sprintf(p + len, "mask:\t\t0x%08x\n", mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 len += sprintf(p + len,
1012 "commands:\tenable, disable, reset, <mask>\n");
1013 } else {
1014 len += sprintf(p + len, "mask:\t\tnot supported\n");
1015 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
1016 }
1017
1018 return len;
1019}
1020
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001021static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001023 int res, status;
1024 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 char *cmd;
1026 int do_cmd = 0;
1027
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001028 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 return -ENODEV;
1030
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001031 res = mutex_lock_interruptible(&hotkey_mutex);
1032 if (res < 0)
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001033 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001034
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001035 res = hotkey_get(&status, &mask);
1036 if (res)
1037 goto errexit;
1038
1039 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 while ((cmd = next_cmd(&buf))) {
1041 if (strlencmp(cmd, "enable") == 0) {
1042 status = 1;
1043 } else if (strlencmp(cmd, "disable") == 0) {
1044 status = 0;
1045 } else if (strlencmp(cmd, "reset") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001046 status = hotkey_orig_status;
1047 mask = hotkey_orig_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
1049 /* mask set */
1050 } else if (sscanf(cmd, "%x", &mask) == 1) {
1051 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001052 } else {
1053 res = -EINVAL;
1054 goto errexit;
1055 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 do_cmd = 1;
1057 }
1058
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001059 if (do_cmd)
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001060 res = hotkey_set(status, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001062errexit:
1063 mutex_unlock(&hotkey_mutex);
1064 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001065}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001067static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001068 .hid = IBM_HKEY_HID,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001069 .notify = hotkey_notify,
1070 .handle = &hkey_handle,
1071 .type = ACPI_DEVICE_NOTIFY,
1072};
1073
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001074static struct ibm_struct hotkey_driver_data = {
1075 .name = "hotkey",
1076 .read = hotkey_read,
1077 .write = hotkey_write,
1078 .exit = hotkey_exit,
1079 .acpi = &ibm_hotkey_acpidriver,
1080};
1081
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001082/*************************************************************************
1083 * Bluetooth subdriver
1084 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001086/* sysfs bluetooth enable ---------------------------------------------- */
1087static ssize_t bluetooth_enable_show(struct device *dev,
1088 struct device_attribute *attr,
1089 char *buf)
1090{
1091 int status;
1092
1093 status = bluetooth_get_radiosw();
1094 if (status < 0)
1095 return status;
1096
1097 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
1098}
1099
1100static ssize_t bluetooth_enable_store(struct device *dev,
1101 struct device_attribute *attr,
1102 const char *buf, size_t count)
1103{
1104 unsigned long t;
1105 int res;
1106
1107 if (parse_strtoul(buf, 1, &t))
1108 return -EINVAL;
1109
1110 res = bluetooth_set_radiosw(t);
1111
1112 return (res) ? res : count;
1113}
1114
1115static struct device_attribute dev_attr_bluetooth_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001116 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001117 bluetooth_enable_show, bluetooth_enable_store);
1118
1119/* --------------------------------------------------------------------- */
1120
1121static struct attribute *bluetooth_attributes[] = {
1122 &dev_attr_bluetooth_enable.attr,
1123 NULL
1124};
1125
1126static const struct attribute_group bluetooth_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001127 .attrs = bluetooth_attributes,
1128};
1129
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001130static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001132 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001133 int status = 0;
1134
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001135 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
1136
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001137 IBM_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001138
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001139 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1140 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001141 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001142 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001144 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
1145 str_supported(tp_features.bluetooth),
1146 status);
1147
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001148 if (tp_features.bluetooth) {
1149 if (!(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
1150 /* no bluetooth hardware present in system */
1151 tp_features.bluetooth = 0;
1152 dbg_printk(TPACPI_DBG_INIT,
1153 "bluetooth hardware not installed\n");
1154 } else {
1155 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
1156 &bluetooth_attr_group);
1157 if (res)
1158 return res;
1159 }
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001160 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001161
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001162 return (tp_features.bluetooth)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163}
1164
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001165static void bluetooth_exit(void)
1166{
1167 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
1168 &bluetooth_attr_group);
1169}
1170
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001171static int bluetooth_get_radiosw(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172{
1173 int status;
1174
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001175 if (!tp_features.bluetooth)
1176 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001178 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
1179 return -EIO;
1180
1181 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
1182}
1183
1184static int bluetooth_set_radiosw(int radio_on)
1185{
1186 int status;
1187
1188 if (!tp_features.bluetooth)
1189 return -ENODEV;
1190
1191 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
1192 return -EIO;
1193 if (radio_on)
1194 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
1195 else
1196 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
1197 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
1198 return -EIO;
1199
1200 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201}
1202
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001203/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001204static int bluetooth_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205{
1206 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001207 int status = bluetooth_get_radiosw();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001209 if (!tp_features.bluetooth)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 len += sprintf(p + len, "status:\t\tnot supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001212 len += sprintf(p + len, "status:\t\t%s\n",
1213 (status)? "enabled" : "disabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 len += sprintf(p + len, "commands:\tenable, disable\n");
1215 }
1216
1217 return len;
1218}
1219
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001220static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001224 if (!tp_features.bluetooth)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001225 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
1227 while ((cmd = next_cmd(&buf))) {
1228 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001229 bluetooth_set_radiosw(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001231 bluetooth_set_radiosw(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 } else
1233 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 }
1235
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 return 0;
1237}
1238
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001239static struct ibm_struct bluetooth_driver_data = {
1240 .name = "bluetooth",
1241 .read = bluetooth_read,
1242 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001243 .exit = bluetooth_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001244};
1245
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001246/*************************************************************************
1247 * Wan subdriver
1248 */
1249
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001250/* sysfs wan enable ---------------------------------------------------- */
1251static ssize_t wan_enable_show(struct device *dev,
1252 struct device_attribute *attr,
1253 char *buf)
1254{
1255 int status;
1256
1257 status = wan_get_radiosw();
1258 if (status < 0)
1259 return status;
1260
1261 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
1262}
1263
1264static ssize_t wan_enable_store(struct device *dev,
1265 struct device_attribute *attr,
1266 const char *buf, size_t count)
1267{
1268 unsigned long t;
1269 int res;
1270
1271 if (parse_strtoul(buf, 1, &t))
1272 return -EINVAL;
1273
1274 res = wan_set_radiosw(t);
1275
1276 return (res) ? res : count;
1277}
1278
1279static struct device_attribute dev_attr_wan_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001280 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001281 wan_enable_show, wan_enable_store);
1282
1283/* --------------------------------------------------------------------- */
1284
1285static struct attribute *wan_attributes[] = {
1286 &dev_attr_wan_enable.attr,
1287 NULL
1288};
1289
1290static const struct attribute_group wan_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001291 .attrs = wan_attributes,
1292};
1293
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001294static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001295{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001296 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001297 int status = 0;
1298
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001299 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
1300
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001301 IBM_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001302
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001303 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001304 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001305
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001306 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
1307 str_supported(tp_features.wan),
1308 status);
1309
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001310 if (tp_features.wan) {
1311 if (!(status & TP_ACPI_WANCARD_HWPRESENT)) {
1312 /* no wan hardware present in system */
1313 tp_features.wan = 0;
1314 dbg_printk(TPACPI_DBG_INIT,
1315 "wan hardware not installed\n");
1316 } else {
1317 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
1318 &wan_attr_group);
1319 if (res)
1320 return res;
1321 }
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001322 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001323
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001324 return (tp_features.wan)? 0 : 1;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001325}
1326
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001327static void wan_exit(void)
1328{
1329 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
1330 &wan_attr_group);
1331}
1332
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001333static int wan_get_radiosw(void)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001334{
1335 int status;
1336
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001337 if (!tp_features.wan)
1338 return -ENODEV;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001339
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001340 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
1341 return -EIO;
1342
1343 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
1344}
1345
1346static int wan_set_radiosw(int radio_on)
1347{
1348 int status;
1349
1350 if (!tp_features.wan)
1351 return -ENODEV;
1352
1353 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
1354 return -EIO;
1355 if (radio_on)
1356 status |= TP_ACPI_WANCARD_RADIOSSW;
1357 else
1358 status &= ~TP_ACPI_WANCARD_RADIOSSW;
1359 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
1360 return -EIO;
1361
1362 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001363}
1364
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001365/* procfs -------------------------------------------------------------- */
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001366static int wan_read(char *p)
1367{
1368 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001369 int status = wan_get_radiosw();
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001370
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001371 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001372 len += sprintf(p + len, "status:\t\tnot supported\n");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001373 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001374 len += sprintf(p + len, "status:\t\t%s\n",
1375 (status)? "enabled" : "disabled");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001376 len += sprintf(p + len, "commands:\tenable, disable\n");
1377 }
1378
1379 return len;
1380}
1381
1382static int wan_write(char *buf)
1383{
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001384 char *cmd;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001385
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001386 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001387 return -ENODEV;
1388
1389 while ((cmd = next_cmd(&buf))) {
1390 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001391 wan_set_radiosw(1);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001392 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001393 wan_set_radiosw(0);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001394 } else
1395 return -EINVAL;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001396 }
1397
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001398 return 0;
1399}
1400
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001401static struct ibm_struct wan_driver_data = {
1402 .name = "wan",
1403 .read = wan_read,
1404 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001405 .exit = wan_exit,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03001406 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001407};
1408
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001409/*************************************************************************
1410 * Video subdriver
1411 */
1412
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02001413static enum video_access_mode video_supported;
1414static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001415
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001416IBM_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
1417 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
1418 "\\_SB.PCI0.VID0", /* 770e */
1419 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
1420 "\\_SB.PCI0.AGP.VID", /* all others */
1421 ); /* R30, R31 */
1422
1423IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
1424
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001425static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001427 int ivga;
1428
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001429 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
1430
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001431 IBM_ACPIHANDLE_INIT(vid);
1432 IBM_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001433
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001434 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
1435 /* G41, assume IVGA doesn't change */
1436 vid_handle = vid2_handle;
1437
1438 if (!vid_handle)
1439 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001440 video_supported = TPACPI_VIDEO_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001441 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
1442 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001443 video_supported = TPACPI_VIDEO_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001444 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
1445 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001446 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001447 else
1448 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001449 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001451 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
1452 str_supported(video_supported != TPACPI_VIDEO_NONE),
1453 video_supported);
1454
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001455 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456}
1457
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001458static void video_exit(void)
1459{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001460 dbg_printk(TPACPI_DBG_EXIT,
1461 "restoring original video autoswitch mode\n");
1462 if (video_autosw_set(video_orig_autosw))
1463 printk(IBM_ERR "error while trying to restore original "
1464 "video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001465}
1466
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001467static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468{
1469 int status = 0;
1470 int i;
1471
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001472 switch (video_supported) {
1473 case TPACPI_VIDEO_570:
1474 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
1475 TP_ACPI_VIDEO_570_PHSCMD))
1476 return -EIO;
1477 status = i & TP_ACPI_VIDEO_570_PHSMASK;
1478 break;
1479 case TPACPI_VIDEO_770:
1480 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
1481 return -EIO;
1482 if (i)
1483 status |= TP_ACPI_VIDEO_S_LCD;
1484 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
1485 return -EIO;
1486 if (i)
1487 status |= TP_ACPI_VIDEO_S_CRT;
1488 break;
1489 case TPACPI_VIDEO_NEW:
1490 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
1491 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
1492 return -EIO;
1493 if (i)
1494 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001496 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
1497 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
1498 return -EIO;
1499 if (i)
1500 status |= TP_ACPI_VIDEO_S_LCD;
1501 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
1502 return -EIO;
1503 if (i)
1504 status |= TP_ACPI_VIDEO_S_DVI;
1505 break;
1506 default:
1507 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
1510 return status;
1511}
1512
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001513static int video_outputsw_set(int status)
1514{
1515 int autosw;
1516 int res = 0;
1517
1518 switch (video_supported) {
1519 case TPACPI_VIDEO_570:
1520 res = acpi_evalf(NULL, NULL,
1521 "\\_SB.PHS2", "vdd",
1522 TP_ACPI_VIDEO_570_PHS2CMD,
1523 status | TP_ACPI_VIDEO_570_PHS2SET);
1524 break;
1525 case TPACPI_VIDEO_770:
1526 autosw = video_autosw_get();
1527 if (autosw < 0)
1528 return autosw;
1529
1530 res = video_autosw_set(1);
1531 if (res)
1532 return res;
1533 res = acpi_evalf(vid_handle, NULL,
1534 "ASWT", "vdd", status * 0x100, 0);
1535 if (!autosw && video_autosw_set(autosw)) {
1536 printk(IBM_ERR "video auto-switch left enabled due to error\n");
1537 return -EIO;
1538 }
1539 break;
1540 case TPACPI_VIDEO_NEW:
1541 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
1542 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
1543 break;
1544 default:
1545 return -ENOSYS;
1546 }
1547
1548 return (res)? 0 : -EIO;
1549}
1550
1551static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001553 int autosw = 0;
1554
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001555 switch (video_supported) {
1556 case TPACPI_VIDEO_570:
1557 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
1558 return -EIO;
1559 break;
1560 case TPACPI_VIDEO_770:
1561 case TPACPI_VIDEO_NEW:
1562 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
1563 return -EIO;
1564 break;
1565 default:
1566 return -ENOSYS;
1567 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001568
1569 return autosw & 1;
1570}
1571
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001572static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001573{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001574 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001575 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001576 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001577}
1578
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001579static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001580{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001581 int autosw = video_autosw_get();
1582 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001583
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001584 if (autosw < 0)
1585 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001586
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001587 switch (video_supported) {
1588 case TPACPI_VIDEO_570:
1589 res = video_autosw_set(1);
1590 if (res)
1591 return res;
1592 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
1593 break;
1594 case TPACPI_VIDEO_770:
1595 case TPACPI_VIDEO_NEW:
1596 res = video_autosw_set(1);
1597 if (res)
1598 return res;
1599 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
1600 break;
1601 default:
1602 return -ENOSYS;
1603 }
1604 if (!autosw && video_autosw_set(autosw)) {
1605 printk(IBM_ERR "video auto-switch left enabled due to error\n");
1606 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001607 }
1608
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001609 return (res)? 0 : -EIO;
1610}
1611
1612static int video_expand_toggle(void)
1613{
1614 switch (video_supported) {
1615 case TPACPI_VIDEO_570:
1616 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
1617 0 : -EIO;
1618 case TPACPI_VIDEO_770:
1619 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
1620 0 : -EIO;
1621 case TPACPI_VIDEO_NEW:
1622 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
1623 0 : -EIO;
1624 default:
1625 return -ENOSYS;
1626 }
1627 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001628}
1629
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001630static int video_read(char *p)
1631{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001632 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001633 int len = 0;
1634
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001635 if (video_supported == TPACPI_VIDEO_NONE) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001636 len += sprintf(p + len, "status:\t\tnot supported\n");
1637 return len;
1638 }
1639
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001640 status = video_outputsw_get();
1641 if (status < 0)
1642 return status;
1643
1644 autosw = video_autosw_get();
1645 if (autosw < 0)
1646 return autosw;
1647
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001648 len += sprintf(p + len, "status:\t\tsupported\n");
1649 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
1650 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001651 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001652 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
1653 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
1654 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
1655 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001656 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001657 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
1658 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
1659 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
1660
1661 return len;
1662}
1663
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001664static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665{
1666 char *cmd;
1667 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001668 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001670 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001671 return -ENODEV;
1672
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001673 enable = 0;
1674 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
1676 while ((cmd = next_cmd(&buf))) {
1677 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001678 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001680 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001682 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001684 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001685 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001686 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001687 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001688 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001689 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001690 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001692 res = video_autosw_set(1);
1693 if (res)
1694 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001696 res = video_autosw_set(0);
1697 if (res)
1698 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001700 res = video_outputsw_cycle();
1701 if (res)
1702 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001704 res = video_expand_toggle();
1705 if (res)
1706 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 } else
1708 return -EINVAL;
1709 }
1710
1711 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001712 status = video_outputsw_get();
1713 if (status < 0)
1714 return status;
1715 res = video_outputsw_set((status & ~disable) | enable);
1716 if (res)
1717 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 }
1719
1720 return 0;
1721}
1722
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001723static struct ibm_struct video_driver_data = {
1724 .name = "video",
1725 .read = video_read,
1726 .write = video_write,
1727 .exit = video_exit,
1728};
1729
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001730/*************************************************************************
1731 * Light (thinklight) subdriver
1732 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001734IBM_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
1735IBM_HANDLE(ledb, ec, "LEDB"); /* G4x */
1736
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001737static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001739 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
1740
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001741 IBM_ACPIHANDLE_INIT(ledb);
1742 IBM_ACPIHANDLE_INIT(lght);
1743 IBM_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001744
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001745 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001746 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001747
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001748 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001749 /* light status not supported on
1750 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001751 tp_features.light_status =
1752 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001754 vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001755 str_supported(tp_features.light));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001756
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001757 return (tp_features.light)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758}
1759
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001760static int light_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761{
1762 int len = 0;
1763 int status = 0;
1764
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001765 if (!tp_features.light) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001766 len += sprintf(p + len, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001767 } else if (!tp_features.light_status) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001768 len += sprintf(p + len, "status:\t\tunknown\n");
1769 len += sprintf(p + len, "commands:\ton, off\n");
1770 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
1772 return -EIO;
1773 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001774 len += sprintf(p + len, "commands:\ton, off\n");
1775 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776
1777 return len;
1778}
1779
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001780static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781{
1782 int cmos_cmd, lght_cmd;
1783 char *cmd;
1784 int success;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001785
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001786 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001787 return -ENODEV;
1788
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 while ((cmd = next_cmd(&buf))) {
1790 if (strlencmp(cmd, "on") == 0) {
1791 cmos_cmd = 0x0c;
1792 lght_cmd = 1;
1793 } else if (strlencmp(cmd, "off") == 0) {
1794 cmos_cmd = 0x0d;
1795 lght_cmd = 0;
1796 } else
1797 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001798
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 success = cmos_handle ?
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001800 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
1801 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 if (!success)
1803 return -EIO;
1804 }
1805
1806 return 0;
1807}
1808
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001809static struct ibm_struct light_driver_data = {
1810 .name = "light",
1811 .read = light_read,
1812 .write = light_write,
1813};
1814
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001815/*************************************************************************
1816 * Dock subdriver
1817 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03001819#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001820
1821IBM_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
1822 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
1823 "\\_SB.PCI0.PCI1.DOCK", /* all others */
1824 "\\_SB.PCI.ISA.SLCE", /* 570 */
1825 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
1826
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001827/* don't list other alternatives as we install a notify handler on the 570 */
1828IBM_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
1829
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03001830static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
1831 {
1832 .notify = dock_notify,
1833 .handle = &dock_handle,
1834 .type = ACPI_SYSTEM_NOTIFY,
1835 },
1836 {
1837 .hid = IBM_PCI_HID,
1838 .notify = dock_notify,
1839 .handle = &pci_handle,
1840 .type = ACPI_SYSTEM_NOTIFY,
1841 },
1842};
1843
1844static struct ibm_struct dock_driver_data[2] = {
1845 {
1846 .name = "dock",
1847 .read = dock_read,
1848 .write = dock_write,
1849 .acpi = &ibm_dock_acpidriver[0],
1850 },
1851 {
1852 .name = "dock",
1853 .acpi = &ibm_dock_acpidriver[1],
1854 },
1855};
1856
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857#define dock_docked() (_sta(dock_handle) & 1)
1858
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001859static int __init dock_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001860{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001861 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
1862
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001863 IBM_ACPIHANDLE_INIT(dock);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001864
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001865 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
1866 str_supported(dock_handle != NULL));
1867
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001868 return (dock_handle)? 0 : 1;
1869}
1870
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03001871static int __init dock_init2(struct ibm_init_struct *iibm)
1872{
1873 int dock2_needed;
1874
1875 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
1876
1877 if (dock_driver_data[0].flags.acpi_driver_registered &&
1878 dock_driver_data[0].flags.acpi_notify_installed) {
1879 IBM_ACPIHANDLE_INIT(pci);
1880 dock2_needed = (pci_handle != NULL);
1881 vdbg_printk(TPACPI_DBG_INIT,
1882 "dock PCI handler for the TP 570 is %s\n",
1883 str_supported(dock2_needed));
1884 } else {
1885 vdbg_printk(TPACPI_DBG_INIT,
1886 "dock subdriver part 2 not required\n");
1887 dock2_needed = 0;
1888 }
1889
1890 return (dock2_needed)? 0 : 1;
1891}
1892
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001893static void dock_notify(struct ibm_struct *ibm, u32 event)
1894{
1895 int docked = dock_docked();
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001896 int pci = ibm->acpi->hid && strstr(ibm->acpi->hid, IBM_PCI_HID);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001897
1898 if (event == 1 && !pci) /* 570 */
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001899 acpi_bus_generate_event(ibm->acpi->device, event, 1); /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001900 else if (event == 1 && pci) /* 570 */
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001901 acpi_bus_generate_event(ibm->acpi->device, event, 3); /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001902 else if (event == 3 && docked)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001903 acpi_bus_generate_event(ibm->acpi->device, event, 1); /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001904 else if (event == 3 && !docked)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001905 acpi_bus_generate_event(ibm->acpi->device, event, 2); /* undock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001906 else if (event == 0 && docked)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001907 acpi_bus_generate_event(ibm->acpi->device, event, 3); /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001908 else {
1909 printk(IBM_ERR "unknown dock event %d, status %d\n",
1910 event, _sta(dock_handle));
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001911 acpi_bus_generate_event(ibm->acpi->device, event, 0); /* unknown */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001912 }
1913}
1914
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001915static int dock_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916{
1917 int len = 0;
1918 int docked = dock_docked();
1919
1920 if (!dock_handle)
1921 len += sprintf(p + len, "status:\t\tnot supported\n");
1922 else if (!docked)
1923 len += sprintf(p + len, "status:\t\tundocked\n");
1924 else {
1925 len += sprintf(p + len, "status:\t\tdocked\n");
1926 len += sprintf(p + len, "commands:\tdock, undock\n");
1927 }
1928
1929 return len;
1930}
1931
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001932static int dock_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933{
1934 char *cmd;
1935
1936 if (!dock_docked())
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001937 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938
1939 while ((cmd = next_cmd(&buf))) {
1940 if (strlencmp(cmd, "undock") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001941 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
1942 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 return -EIO;
1944 } else if (strlencmp(cmd, "dock") == 0) {
1945 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
1946 return -EIO;
1947 } else
1948 return -EINVAL;
1949 }
1950
1951 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001952}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03001954#endif /* CONFIG_THINKPAD_ACPI_DOCK */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001956/*************************************************************************
1957 * Bay subdriver
1958 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03001960#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001961IBM_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
1962 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
1963 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
1964 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
1965 ); /* A21e, R30, R31 */
1966IBM_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
1967 "_EJ0", /* all others */
1968 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
1969IBM_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
1970 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
1971 ); /* all others */
1972IBM_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
1973 "_EJ0", /* 770x */
1974 ); /* all others */
1975
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001976static int __init bay_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001978 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
1979
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001980 IBM_ACPIHANDLE_INIT(bay);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001981 if (bay_handle)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001982 IBM_ACPIHANDLE_INIT(bay_ej);
1983 IBM_ACPIHANDLE_INIT(bay2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001984 if (bay2_handle)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001985 IBM_ACPIHANDLE_INIT(bay2_ej);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001986
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001987 tp_features.bay_status = bay_handle &&
1988 acpi_evalf(bay_handle, NULL, "_STA", "qv");
1989 tp_features.bay_status2 = bay2_handle &&
1990 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001991
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001992 tp_features.bay_eject = bay_handle && bay_ej_handle &&
1993 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
1994 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
1995 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001997 vdbg_printk(TPACPI_DBG_INIT,
1998 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001999 str_supported(tp_features.bay_status),
2000 str_supported(tp_features.bay_eject),
2001 str_supported(tp_features.bay_status2),
2002 str_supported(tp_features.bay_eject2));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002003
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002004 return (tp_features.bay_status || tp_features.bay_eject ||
2005 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006}
2007
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002008static void bay_notify(struct ibm_struct *ibm, u32 event)
2009{
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002010 acpi_bus_generate_event(ibm->acpi->device, event, 0);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002011}
2012
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002013#define bay_occupied(b) (_sta(b##_handle) & 1)
2014
2015static int bay_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016{
2017 int len = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002018 int occupied = bay_occupied(bay);
2019 int occupied2 = bay_occupied(bay2);
2020 int eject, eject2;
2021
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002022 len += sprintf(p + len, "status:\t\t%s\n",
2023 tp_features.bay_status ?
2024 (occupied ? "occupied" : "unoccupied") :
2025 "not supported");
2026 if (tp_features.bay_status2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002027 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
2028 "occupied" : "unoccupied");
2029
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002030 eject = tp_features.bay_eject && occupied;
2031 eject2 = tp_features.bay_eject2 && occupied2;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002032
2033 if (eject && eject2)
2034 len += sprintf(p + len, "commands:\teject, eject2\n");
2035 else if (eject)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 len += sprintf(p + len, "commands:\teject\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002037 else if (eject2)
2038 len += sprintf(p + len, "commands:\teject2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039
2040 return len;
2041}
2042
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002043static int bay_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044{
2045 char *cmd;
2046
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002047 if (!tp_features.bay_eject && !tp_features.bay_eject2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002048 return -ENODEV;
2049
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002051 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002052 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
2053 return -EIO;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002054 } else if (tp_features.bay_eject2 &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002055 strlencmp(cmd, "eject2") == 0) {
2056 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 return -EIO;
2058 } else
2059 return -EINVAL;
2060 }
2061
2062 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002063}
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002064
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002065static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
2066 .notify = bay_notify,
2067 .handle = &bay_handle,
2068 .type = ACPI_SYSTEM_NOTIFY,
2069};
2070
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002071static struct ibm_struct bay_driver_data = {
2072 .name = "bay",
2073 .read = bay_read,
2074 .write = bay_write,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002075 .acpi = &ibm_bay_acpidriver,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002076};
2077
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03002078#endif /* CONFIG_THINKPAD_ACPI_BAY */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002080/*************************************************************************
2081 * CMOS subdriver
2082 */
2083
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03002084/* sysfs cmos_command -------------------------------------------------- */
2085static ssize_t cmos_command_store(struct device *dev,
2086 struct device_attribute *attr,
2087 const char *buf, size_t count)
2088{
2089 unsigned long cmos_cmd;
2090 int res;
2091
2092 if (parse_strtoul(buf, 21, &cmos_cmd))
2093 return -EINVAL;
2094
2095 res = issue_thinkpad_cmos_command(cmos_cmd);
2096 return (res)? res : count;
2097}
2098
2099static struct device_attribute dev_attr_cmos_command =
2100 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
2101
2102/* --------------------------------------------------------------------- */
2103
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002104static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002105{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03002106 int res;
2107
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002108 vdbg_printk(TPACPI_DBG_INIT,
2109 "initializing cmos commands subdriver\n");
2110
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002111 IBM_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002112
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002113 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
2114 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03002115
2116 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
2117 if (res)
2118 return res;
2119
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002120 return (cmos_handle)? 0 : 1;
2121}
2122
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03002123static void cmos_exit(void)
2124{
2125 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
2126}
2127
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002128static int cmos_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129{
2130 int len = 0;
2131
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002132 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2133 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 if (!cmos_handle)
2135 len += sprintf(p + len, "status:\t\tnot supported\n");
2136 else {
2137 len += sprintf(p + len, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002138 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 }
2140
2141 return len;
2142}
2143
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002144static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145{
2146 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03002147 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148
2149 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002150 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
2151 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 /* cmos_cmd set */
2153 } else
2154 return -EINVAL;
2155
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03002156 res = issue_thinkpad_cmos_command(cmos_cmd);
2157 if (res)
2158 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 }
2160
2161 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002162}
2163
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002164static struct ibm_struct cmos_driver_data = {
2165 .name = "cmos",
2166 .read = cmos_read,
2167 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03002168 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002169};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002170
2171/*************************************************************************
2172 * LED subdriver
2173 */
2174
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02002175static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002176
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002177IBM_HANDLE(led, ec, "SLED", /* 570 */
2178 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
2179 "LED", /* all others */
2180 ); /* R30, R31 */
2181
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002182static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002183{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002184 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
2185
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002186 IBM_ACPIHANDLE_INIT(led);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002187
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002188 if (!led_handle)
2189 /* led not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002190 led_supported = TPACPI_LED_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002191 else if (strlencmp(led_path, "SLED") == 0)
2192 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002193 led_supported = TPACPI_LED_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002194 else if (strlencmp(led_path, "SYSL") == 0)
2195 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002196 led_supported = TPACPI_LED_OLD;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002197 else
2198 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002199 led_supported = TPACPI_LED_NEW;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002200
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002201 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
2202 str_supported(led_supported), led_supported);
2203
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002204 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002205}
2206
2207#define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
2208
2209static int led_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210{
2211 int len = 0;
2212
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002213 if (!led_supported) {
2214 len += sprintf(p + len, "status:\t\tnot supported\n");
2215 return len;
2216 }
2217 len += sprintf(p + len, "status:\t\tsupported\n");
2218
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002219 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002220 /* 570 */
2221 int i, status;
2222 for (i = 0; i < 8; i++) {
2223 if (!acpi_evalf(ec_handle,
2224 &status, "GLED", "dd", 1 << i))
2225 return -EIO;
2226 len += sprintf(p + len, "%d:\t\t%s\n",
2227 i, led_status(status));
2228 }
2229 }
2230
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 len += sprintf(p + len, "commands:\t"
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002232 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233
2234 return len;
2235}
2236
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002237/* off, on, blink */
2238static const int led_sled_arg1[] = { 0, 1, 3 };
2239static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
2240static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
2241static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
2242
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002243static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244{
2245 char *cmd;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002246 int led, ind, ret;
2247
2248 if (!led_supported)
2249 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250
2251 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002252 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 return -EINVAL;
2254
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002255 if (strstr(cmd, "off")) {
2256 ind = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 } else if (strstr(cmd, "on")) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002258 ind = 1;
2259 } else if (strstr(cmd, "blink")) {
2260 ind = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 } else
2262 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002263
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002264 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002265 /* 570 */
2266 led = 1 << led;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002268 led, led_sled_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 return -EIO;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002270 } else if (led_supported == TPACPI_LED_OLD) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002271 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
2272 led = 1 << led;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002273 ret = ec_write(TPACPI_LED_EC_HLMS, led);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002274 if (ret >= 0)
2275 ret =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002276 ec_write(TPACPI_LED_EC_HLBL,
Henrique de Moraes Holschuhe062e032007-03-23 17:33:55 -03002277 led * led_exp_hlbl[ind]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002278 if (ret >= 0)
2279 ret =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002280 ec_write(TPACPI_LED_EC_HLCL,
Henrique de Moraes Holschuhe062e032007-03-23 17:33:55 -03002281 led * led_exp_hlcl[ind]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002282 if (ret < 0)
2283 return ret;
2284 } else {
2285 /* all others */
2286 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
2287 led, led_led_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 }
2291
2292 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002293}
2294
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002295static struct ibm_struct led_driver_data = {
2296 .name = "led",
2297 .read = led_read,
2298 .write = led_write,
2299};
2300
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002301/*************************************************************************
2302 * Beep subdriver
2303 */
2304
2305IBM_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
2306
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002307static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002308{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002309 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
2310
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002311 IBM_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002312
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002313 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
2314 str_supported(beep_handle != NULL));
2315
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002316 return (beep_handle)? 0 : 1;
2317}
2318
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002319static int beep_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320{
2321 int len = 0;
2322
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002323 if (!beep_handle)
2324 len += sprintf(p + len, "status:\t\tnot supported\n");
2325 else {
2326 len += sprintf(p + len, "status:\t\tsupported\n");
2327 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
2328 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329
2330 return len;
2331}
2332
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002333static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334{
2335 char *cmd;
2336 int beep_cmd;
2337
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002338 if (!beep_handle)
2339 return -ENODEV;
2340
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002342 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
2343 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 /* beep_cmd set */
2345 } else
2346 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002347 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 return -EIO;
2349 }
2350
2351 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002352}
2353
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002354static struct ibm_struct beep_driver_data = {
2355 .name = "beep",
2356 .read = beep_read,
2357 .write = beep_write,
2358};
2359
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002360/*************************************************************************
2361 * Thermal subdriver
2362 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002363
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002364static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002365
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03002366/* sysfs temp##_input -------------------------------------------------- */
2367
2368static ssize_t thermal_temp_input_show(struct device *dev,
2369 struct device_attribute *attr,
2370 char *buf)
2371{
2372 struct sensor_device_attribute *sensor_attr =
2373 to_sensor_dev_attr(attr);
2374 int idx = sensor_attr->index;
2375 s32 value;
2376 int res;
2377
2378 res = thermal_get_sensor(idx, &value);
2379 if (res)
2380 return res;
2381 if (value == TP_EC_THERMAL_TMP_NA * 1000)
2382 return -ENXIO;
2383
2384 return snprintf(buf, PAGE_SIZE, "%d\n", value);
2385}
2386
2387#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
2388 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, thermal_temp_input_show, NULL, _idxB)
2389
2390static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
2391 THERMAL_SENSOR_ATTR_TEMP(1, 0),
2392 THERMAL_SENSOR_ATTR_TEMP(2, 1),
2393 THERMAL_SENSOR_ATTR_TEMP(3, 2),
2394 THERMAL_SENSOR_ATTR_TEMP(4, 3),
2395 THERMAL_SENSOR_ATTR_TEMP(5, 4),
2396 THERMAL_SENSOR_ATTR_TEMP(6, 5),
2397 THERMAL_SENSOR_ATTR_TEMP(7, 6),
2398 THERMAL_SENSOR_ATTR_TEMP(8, 7),
2399 THERMAL_SENSOR_ATTR_TEMP(9, 8),
2400 THERMAL_SENSOR_ATTR_TEMP(10, 9),
2401 THERMAL_SENSOR_ATTR_TEMP(11, 10),
2402 THERMAL_SENSOR_ATTR_TEMP(12, 11),
2403 THERMAL_SENSOR_ATTR_TEMP(13, 12),
2404 THERMAL_SENSOR_ATTR_TEMP(14, 13),
2405 THERMAL_SENSOR_ATTR_TEMP(15, 14),
2406 THERMAL_SENSOR_ATTR_TEMP(16, 15),
2407};
2408
2409#define THERMAL_ATTRS(X) \
2410 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
2411
2412static struct attribute *thermal_temp_input_attr[] = {
2413 THERMAL_ATTRS(8),
2414 THERMAL_ATTRS(9),
2415 THERMAL_ATTRS(10),
2416 THERMAL_ATTRS(11),
2417 THERMAL_ATTRS(12),
2418 THERMAL_ATTRS(13),
2419 THERMAL_ATTRS(14),
2420 THERMAL_ATTRS(15),
2421 THERMAL_ATTRS(0),
2422 THERMAL_ATTRS(1),
2423 THERMAL_ATTRS(2),
2424 THERMAL_ATTRS(3),
2425 THERMAL_ATTRS(4),
2426 THERMAL_ATTRS(5),
2427 THERMAL_ATTRS(6),
2428 THERMAL_ATTRS(7),
2429 NULL
2430};
2431
2432static const struct attribute_group thermal_temp_input16_group = {
2433 .attrs = thermal_temp_input_attr
2434};
2435
2436static const struct attribute_group thermal_temp_input8_group = {
2437 .attrs = &thermal_temp_input_attr[8]
2438};
2439
2440#undef THERMAL_SENSOR_ATTR_TEMP
2441#undef THERMAL_ATTRS
2442
2443/* --------------------------------------------------------------------- */
2444
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002445static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002446{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002447 u8 t, ta1, ta2;
2448 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002449 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03002450 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002451
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002452 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
2453
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002454 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002455
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002456 if (ibm_thinkpad_ec_found && experimental) {
2457 /*
2458 * Direct EC access mode: sensors at registers
2459 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
2460 * non-implemented, thermal sensors return 0x80 when
2461 * not available
2462 */
2463
2464 ta1 = ta2 = 0;
2465 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002466 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002467 ta1 |= t;
2468 } else {
2469 ta1 = 0;
2470 break;
2471 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002472 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002473 ta2 |= t;
2474 } else {
2475 ta1 = 0;
2476 break;
2477 }
2478 }
2479 if (ta1 == 0) {
2480 /* This is sheer paranoia, but we handle it anyway */
2481 if (acpi_tmp7) {
2482 printk(IBM_ERR
2483 "ThinkPad ACPI EC access misbehaving, "
2484 "falling back to ACPI TMPx access mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002485 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002486 } else {
2487 printk(IBM_ERR
2488 "ThinkPad ACPI EC access misbehaving, "
2489 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002490 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002491 }
2492 } else {
2493 thermal_read_mode =
2494 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002495 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002496 }
2497 } else if (acpi_tmp7) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002498 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
2499 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002500 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002501 } else {
2502 /* Standard ACPI TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002503 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002504 }
2505 } else {
2506 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002507 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002508 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002509
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002510 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
2511 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
2512 thermal_read_mode);
2513
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03002514 switch(thermal_read_mode) {
2515 case TPACPI_THERMAL_TPEC_16:
2516 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2517 &thermal_temp_input16_group);
2518 if (res)
2519 return res;
2520 break;
2521 case TPACPI_THERMAL_TPEC_8:
2522 case TPACPI_THERMAL_ACPI_TMP07:
2523 case TPACPI_THERMAL_ACPI_UPDT:
2524 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2525 &thermal_temp_input8_group);
2526 if (res)
2527 return res;
2528 break;
2529 case TPACPI_THERMAL_NONE:
2530 default:
2531 return 1;
2532 }
2533
2534 return 0;
2535}
2536
2537static void thermal_exit(void)
2538{
2539 switch(thermal_read_mode) {
2540 case TPACPI_THERMAL_TPEC_16:
2541 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2542 &thermal_temp_input16_group);
2543 break;
2544 case TPACPI_THERMAL_TPEC_8:
2545 case TPACPI_THERMAL_ACPI_TMP07:
2546 case TPACPI_THERMAL_ACPI_UPDT:
2547 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2548 &thermal_temp_input16_group);
2549 break;
2550 case TPACPI_THERMAL_NONE:
2551 default:
2552 break;
2553 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002554}
2555
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002556/* idx is zero-based */
2557static int thermal_get_sensor(int idx, s32 *value)
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002558{
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002559 int t;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002560 s8 tmp;
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002561 char tmpi[5];
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002562
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002563 t = TP_EC_THERMAL_TMP0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002564
2565 switch (thermal_read_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002566#if TPACPI_MAX_THERMAL_SENSORS >= 16
2567 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002568 if (idx >= 8 && idx <= 15) {
2569 t = TP_EC_THERMAL_TMP8;
2570 idx -= 8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002571 }
2572 /* fallthrough */
2573#endif
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002574 case TPACPI_THERMAL_TPEC_8:
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002575 if (idx <= 7) {
2576 if (!acpi_ec_read(t + idx, &tmp))
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002577 return -EIO;
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002578 *value = tmp * 1000;
2579 return 0;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002580 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002581 break;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002582
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002583 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002584 if (idx <= 7) {
2585 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
2586 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
2587 return -EIO;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002588 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
2589 return -EIO;
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002590 *value = (t - 2732) * 100;
2591 return 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002592 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002593 break;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002594
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002595 case TPACPI_THERMAL_ACPI_TMP07:
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002596 if (idx <= 7) {
2597 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002598 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
2599 return -EIO;
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002600 *value = t * 1000;
2601 return 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002602 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002603 break;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002604
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002605 case TPACPI_THERMAL_NONE:
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002606 default:
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002607 return -ENOSYS;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002608 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002609
2610 return -EINVAL;
2611}
2612
2613static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
2614{
2615 int res, i;
2616 int n;
2617
2618 n = 8;
2619 i = 0;
2620
2621 if (!s)
2622 return -EINVAL;
2623
2624 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
2625 n = 16;
2626
2627 for(i = 0 ; i < n; i++) {
2628 res = thermal_get_sensor(i, &s->temp[i]);
2629 if (res)
2630 return res;
2631 }
2632
2633 return n;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002634}
2635
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002636static int thermal_read(char *p)
2637{
2638 int len = 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002639 int n, i;
2640 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002641
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002642 n = thermal_get_sensors(&t);
2643 if (unlikely(n < 0))
2644 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002645
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002646 len += sprintf(p + len, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002647
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002648 if (n > 0) {
2649 for (i = 0; i < (n - 1); i++)
2650 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
2651 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
2652 } else
2653 len += sprintf(p + len, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002654
2655 return len;
2656}
2657
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002658static struct ibm_struct thermal_driver_data = {
2659 .name = "thermal",
2660 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03002661 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002662};
2663
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002664/*************************************************************************
2665 * EC Dump subdriver
2666 */
2667
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002668static u8 ecdump_regs[256];
2669
2670static int ecdump_read(char *p)
2671{
2672 int len = 0;
2673 int i, j;
2674 u8 v;
2675
2676 len += sprintf(p + len, "EC "
2677 " +00 +01 +02 +03 +04 +05 +06 +07"
2678 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
2679 for (i = 0; i < 256; i += 16) {
2680 len += sprintf(p + len, "EC 0x%02x:", i);
2681 for (j = 0; j < 16; j++) {
2682 if (!acpi_ec_read(i + j, &v))
2683 break;
2684 if (v != ecdump_regs[i + j])
2685 len += sprintf(p + len, " *%02x", v);
2686 else
2687 len += sprintf(p + len, " %02x", v);
2688 ecdump_regs[i + j] = v;
2689 }
2690 len += sprintf(p + len, "\n");
2691 if (j != 16)
2692 break;
2693 }
2694
2695 /* These are way too dangerous to advertise openly... */
2696#if 0
2697 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
2698 " (<offset> is 00-ff, <value> is 00-ff)\n");
2699 len += sprintf(p + len, "commands:\t0x<offset> <value> "
2700 " (<offset> is 00-ff, <value> is 0-255)\n");
2701#endif
2702 return len;
2703}
2704
2705static int ecdump_write(char *buf)
2706{
2707 char *cmd;
2708 int i, v;
2709
2710 while ((cmd = next_cmd(&buf))) {
2711 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
2712 /* i and v set */
2713 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
2714 /* i and v set */
2715 } else
2716 return -EINVAL;
2717 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
2718 if (!acpi_ec_write(i, v))
2719 return -EIO;
2720 } else
2721 return -EINVAL;
2722 }
2723
2724 return 0;
2725}
2726
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002727static struct ibm_struct ecdump_driver_data = {
2728 .name = "ecdump",
2729 .read = ecdump_read,
2730 .write = ecdump_write,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03002731 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002732};
2733
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002734/*************************************************************************
2735 * Backlight/brightness subdriver
2736 */
Holger Macht8acb0252006-10-20 14:30:28 -07002737
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03002738static struct backlight_device *ibm_backlight_device;
Holger Macht8acb0252006-10-20 14:30:28 -07002739
Richard Purdie599a52d2007-02-10 23:07:48 +00002740static struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02002741 .get_brightness = brightness_get,
2742 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02002743};
2744
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002745static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02002746{
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02002747 int b;
2748
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002749 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
2750
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02002751 b = brightness_get(NULL);
2752 if (b < 0)
2753 return b;
2754
Henrique de Moraes Holschuh7d5a0152007-04-24 11:48:20 -03002755 ibm_backlight_device = backlight_device_register(
2756 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
2757 &ibm_backlight_data);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02002758 if (IS_ERR(ibm_backlight_device)) {
2759 printk(IBM_ERR "Could not register backlight device\n");
2760 return PTR_ERR(ibm_backlight_device);
2761 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002762 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02002763
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02002764 ibm_backlight_device->props.max_brightness = 7;
2765 ibm_backlight_device->props.brightness = b;
2766 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00002767
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02002768 return 0;
2769}
2770
2771static void brightness_exit(void)
2772{
2773 if (ibm_backlight_device) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002774 vdbg_printk(TPACPI_DBG_EXIT,
2775 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02002776 backlight_device_unregister(ibm_backlight_device);
2777 ibm_backlight_device = NULL;
2778 }
2779}
2780
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002781static int brightness_update_status(struct backlight_device *bd)
2782{
2783 return brightness_set(
2784 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
2785 bd->props.power == FB_BLANK_UNBLANK) ?
2786 bd->props.brightness : 0);
2787}
2788
2789static int brightness_get(struct backlight_device *bd)
2790{
2791 u8 level;
2792 if (!acpi_ec_read(brightness_offset, &level))
2793 return -EIO;
2794
2795 level &= 0x7;
2796
2797 return level;
2798}
2799
2800static int brightness_set(int value)
2801{
2802 int cmos_cmd, inc, i;
2803 int current_value = brightness_get(NULL);
2804
2805 value &= 7;
2806
2807 cmos_cmd = value > current_value ? TP_CMOS_BRIGHTNESS_UP : TP_CMOS_BRIGHTNESS_DOWN;
2808 inc = value > current_value ? 1 : -1;
2809 for (i = current_value; i != value; i += inc) {
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03002810 if (issue_thinkpad_cmos_command(cmos_cmd))
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002811 return -EIO;
2812 if (!acpi_ec_write(brightness_offset, i + inc))
2813 return -EIO;
2814 }
2815
2816 return 0;
2817}
2818
2819static int brightness_read(char *p)
2820{
2821 int len = 0;
2822 int level;
2823
2824 if ((level = brightness_get(NULL)) < 0) {
2825 len += sprintf(p + len, "level:\t\tunreadable\n");
2826 } else {
2827 len += sprintf(p + len, "level:\t\t%d\n", level & 0x7);
2828 len += sprintf(p + len, "commands:\tup, down\n");
2829 len += sprintf(p + len, "commands:\tlevel <level>"
2830 " (<level> is 0-7)\n");
2831 }
2832
2833 return len;
2834}
2835
2836static int brightness_write(char *buf)
2837{
2838 int level;
2839 int new_level;
2840 char *cmd;
2841
2842 while ((cmd = next_cmd(&buf))) {
2843 if ((level = brightness_get(NULL)) < 0)
2844 return level;
2845 level &= 7;
2846
2847 if (strlencmp(cmd, "up") == 0) {
2848 new_level = level == 7 ? 7 : level + 1;
2849 } else if (strlencmp(cmd, "down") == 0) {
2850 new_level = level == 0 ? 0 : level - 1;
2851 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
2852 new_level >= 0 && new_level <= 7) {
2853 /* new_level set */
2854 } else
2855 return -EINVAL;
2856
2857 brightness_set(new_level);
2858 }
2859
2860 return 0;
2861}
2862
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002863static struct ibm_struct brightness_driver_data = {
2864 .name = "brightness",
2865 .read = brightness_read,
2866 .write = brightness_write,
2867 .exit = brightness_exit,
2868};
2869
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002870/*************************************************************************
2871 * Volume subdriver
2872 */
2873
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002874static int volume_read(char *p)
2875{
2876 int len = 0;
2877 u8 level;
2878
2879 if (!acpi_ec_read(volume_offset, &level)) {
2880 len += sprintf(p + len, "level:\t\tunreadable\n");
2881 } else {
2882 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
2883 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
2884 len += sprintf(p + len, "commands:\tup, down, mute\n");
2885 len += sprintf(p + len, "commands:\tlevel <level>"
2886 " (<level> is 0-15)\n");
2887 }
2888
2889 return len;
2890}
2891
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002892static int volume_write(char *buf)
2893{
2894 int cmos_cmd, inc, i;
2895 u8 level, mute;
2896 int new_level, new_mute;
2897 char *cmd;
2898
2899 while ((cmd = next_cmd(&buf))) {
2900 if (!acpi_ec_read(volume_offset, &level))
2901 return -EIO;
2902 new_mute = mute = level & 0x40;
2903 new_level = level = level & 0xf;
2904
2905 if (strlencmp(cmd, "up") == 0) {
2906 if (mute)
2907 new_mute = 0;
2908 else
2909 new_level = level == 15 ? 15 : level + 1;
2910 } else if (strlencmp(cmd, "down") == 0) {
2911 if (mute)
2912 new_mute = 0;
2913 else
2914 new_level = level == 0 ? 0 : level - 1;
2915 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
2916 new_level >= 0 && new_level <= 15) {
2917 /* new_level set */
2918 } else if (strlencmp(cmd, "mute") == 0) {
2919 new_mute = 0x40;
2920 } else
2921 return -EINVAL;
2922
2923 if (new_level != level) { /* mute doesn't change */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002924 cmos_cmd = new_level > level ? TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002925 inc = new_level > level ? 1 : -1;
2926
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03002927 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002928 !acpi_ec_write(volume_offset, level)))
2929 return -EIO;
2930
2931 for (i = level; i != new_level; i += inc)
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03002932 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002933 !acpi_ec_write(volume_offset, i + inc))
2934 return -EIO;
2935
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03002936 if (mute && (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002937 !acpi_ec_write(volume_offset,
2938 new_level + mute)))
2939 return -EIO;
2940 }
2941
2942 if (new_mute != mute) { /* level doesn't change */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002943 cmos_cmd = new_mute ? TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002944
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03002945 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002946 !acpi_ec_write(volume_offset, level + new_mute))
2947 return -EIO;
2948 }
2949 }
2950
2951 return 0;
2952}
2953
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002954static struct ibm_struct volume_driver_data = {
2955 .name = "volume",
2956 .read = volume_read,
2957 .write = volume_write,
2958};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002959
2960/*************************************************************************
2961 * Fan subdriver
2962 */
2963
2964/*
2965 * FAN ACCESS MODES
2966 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002967 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002968 * ACPI GFAN method: returns fan level
2969 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002970 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03002971 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002972 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002973 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002974 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
2975 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03002976 * EC 0x2f (HFSP) might be available *for reading*, but do not use
2977 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002978 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002979 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03002980 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
2981 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002982 *
2983 * Fan speed changes of any sort (including those caused by the
2984 * disengaged mode) are usually done slowly by the firmware as the
2985 * maximum ammount of fan duty cycle change per second seems to be
2986 * limited.
2987 *
2988 * Reading is not available if GFAN exists.
2989 * Writing is not available if SFAN exists.
2990 *
2991 * Bits
2992 * 7 automatic mode engaged;
2993 * (default operation mode of the ThinkPad)
2994 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03002995 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002996 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03002997 * the tachometer while the fan controller ramps up
2998 * the speed (which can take up to a few *minutes*).
2999 * Speeds up fan to 100% duty-cycle, which is far above
3000 * the standard RPM levels. It is not impossible that
3001 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003002 * 5-3 unused in some models. Extra bits for fan level
3003 * in others, but still useless as all values above
3004 * 7 map to the same speed as level 7 in these models.
3005 * 2-0 fan level (0..7 usually)
3006 * 0x00 = stop
3007 * 0x07 = max (set when temperatures critical)
3008 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003009 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003010 *
3011 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
3012 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
3013 * does so, its initial value is meaningless (0x07).
3014 *
3015 * For firmware bugs, refer to:
3016 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
3017 *
3018 * ----
3019 *
3020 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
3021 * Main fan tachometer reading (in RPM)
3022 *
3023 * This register is present on all ThinkPads with a new-style EC, and
3024 * it is known not to be present on the A21m/e, and T22, as there is
3025 * something else in offset 0x84 according to the ACPI DSDT. Other
3026 * ThinkPads from this same time period (and earlier) probably lack the
3027 * tachometer as well.
3028 *
3029 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
3030 * was never fixed by IBM to report the EC firmware version string
3031 * probably support the tachometer (like the early X models), so
3032 * detecting it is quite hard. We need more data to know for sure.
3033 *
3034 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
3035 * might result.
3036 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03003037 * FIRMWARE BUG: may go stale while the EC is switching to full speed
3038 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003039 *
3040 * For firmware bugs, refer to:
3041 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
3042 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003043 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003044 * ThinkPad X31, X40, X41. Not available in the X60.
3045 *
3046 * FANS ACPI handle: takes three arguments: low speed, medium speed,
3047 * high speed. ACPI DSDT seems to map these three speeds to levels
3048 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
3049 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
3050 *
3051 * The speeds are stored on handles
3052 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
3053 *
3054 * There are three default speed sets, acessible as handles:
3055 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
3056 *
3057 * ACPI DSDT switches which set is in use depending on various
3058 * factors.
3059 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003060 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003061 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
3062 * but the ACPI tables just mention level 7.
3063 */
3064
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003065static enum fan_status_access_mode fan_status_access_mode;
3066static enum fan_control_access_mode fan_control_access_mode;
3067static enum fan_control_commands fan_control_commands;
3068
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02003069static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003070static u8 fan_control_desired_level;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02003071
Len Brown25c68a32006-12-08 04:43:41 -05003072static void fan_watchdog_fire(struct work_struct *ignored);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003073static int fan_watchdog_maxinterval;
Len Brown25c68a32006-12-08 04:43:41 -05003074static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003075
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003076IBM_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
3077IBM_HANDLE(gfan, ec, "GFAN", /* 570 */
3078 "\\FSPD", /* 600e/x, 770e, 770x */
3079 ); /* all others */
3080IBM_HANDLE(sfan, ec, "SFAN", /* 570 */
3081 "JFNS", /* 770x-JL */
3082 ); /* all others */
3083
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003084/*
3085 * SYSFS fan layout: hwmon compatible (device)
3086 *
3087 * pwm*_enable:
3088 * 0: "disengaged" mode
3089 * 1: manual mode
3090 * 2: native EC "auto" mode (recommended, hardware default)
3091 *
3092 * pwm*: set speed in manual mode, ignored otherwise.
3093 * 0 is level 0; 255 is level 7. Intermediate points done with linear
3094 * interpolation.
3095 *
3096 * fan*_input: tachometer reading, RPM
3097 *
3098 *
3099 * SYSFS fan layout: extensions
3100 *
3101 * fan_watchdog (driver):
3102 * fan watchdog interval in seconds, 0 disables (default), max 120
3103 */
3104
3105/* sysfs fan pwm1_enable ----------------------------------------------- */
3106static ssize_t fan_pwm1_enable_show(struct device *dev,
3107 struct device_attribute *attr,
3108 char *buf)
3109{
3110 int res, mode;
3111 u8 status;
3112
3113 res = fan_get_status_safe(&status);
3114 if (res)
3115 return res;
3116
3117 if (unlikely(tp_features.fan_ctrl_status_undef)) {
3118 if (status != fan_control_initial_status) {
3119 tp_features.fan_ctrl_status_undef = 0;
3120 } else {
3121 /* Return most likely status. In fact, it
3122 * might be the only possible status */
3123 status = TP_EC_FAN_AUTO;
3124 }
3125 }
3126
3127 if (status & TP_EC_FAN_FULLSPEED) {
3128 mode = 0;
3129 } else if (status & TP_EC_FAN_AUTO) {
3130 mode = 2;
3131 } else
3132 mode = 1;
3133
3134 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
3135}
3136
3137static ssize_t fan_pwm1_enable_store(struct device *dev,
3138 struct device_attribute *attr,
3139 const char *buf, size_t count)
3140{
3141 unsigned long t;
3142 int res, level;
3143
3144 if (parse_strtoul(buf, 2, &t))
3145 return -EINVAL;
3146
3147 switch (t) {
3148 case 0:
3149 level = TP_EC_FAN_FULLSPEED;
3150 break;
3151 case 1:
3152 level = TPACPI_FAN_LAST_LEVEL;
3153 break;
3154 case 2:
3155 level = TP_EC_FAN_AUTO;
3156 break;
3157 case 3:
3158 /* reserved for software-controlled auto mode */
3159 return -ENOSYS;
3160 default:
3161 return -EINVAL;
3162 }
3163
3164 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03003165 if (res == -ENXIO)
3166 return -EINVAL;
3167 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003168 return res;
3169
3170 fan_watchdog_reset();
3171
3172 return count;
3173}
3174
3175static struct device_attribute dev_attr_fan_pwm1_enable =
3176 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
3177 fan_pwm1_enable_show, fan_pwm1_enable_store);
3178
3179/* sysfs fan pwm1 ------------------------------------------------------ */
3180static ssize_t fan_pwm1_show(struct device *dev,
3181 struct device_attribute *attr,
3182 char *buf)
3183{
3184 int res;
3185 u8 status;
3186
3187 res = fan_get_status_safe(&status);
3188 if (res)
3189 return res;
3190
3191 if (unlikely(tp_features.fan_ctrl_status_undef)) {
3192 if (status != fan_control_initial_status) {
3193 tp_features.fan_ctrl_status_undef = 0;
3194 } else {
3195 status = TP_EC_FAN_AUTO;
3196 }
3197 }
3198
3199 if ((status &
3200 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
3201 status = fan_control_desired_level;
3202
3203 if (status > 7)
3204 status = 7;
3205
3206 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
3207}
3208
3209static ssize_t fan_pwm1_store(struct device *dev,
3210 struct device_attribute *attr,
3211 const char *buf, size_t count)
3212{
3213 unsigned long s;
3214 int rc;
3215 u8 status, newlevel;
3216
3217 if (parse_strtoul(buf, 255, &s))
3218 return -EINVAL;
3219
3220 /* scale down from 0-255 to 0-7 */
3221 newlevel = (s >> 5) & 0x07;
3222
3223 rc = mutex_lock_interruptible(&fan_mutex);
3224 if (rc < 0)
3225 return rc;
3226
3227 rc = fan_get_status(&status);
3228 if (!rc && (status &
3229 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
3230 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03003231 if (rc == -ENXIO)
3232 rc = -EINVAL;
3233 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003234 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03003235 fan_watchdog_reset();
3236 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003237 }
3238
3239 mutex_unlock(&fan_mutex);
3240 return (rc)? rc : count;
3241}
3242
3243static struct device_attribute dev_attr_fan_pwm1 =
3244 __ATTR(pwm1, S_IWUSR | S_IRUGO,
3245 fan_pwm1_show, fan_pwm1_store);
3246
3247/* sysfs fan fan1_input ------------------------------------------------ */
3248static ssize_t fan_fan1_input_show(struct device *dev,
3249 struct device_attribute *attr,
3250 char *buf)
3251{
3252 int res;
3253 unsigned int speed;
3254
3255 res = fan_get_speed(&speed);
3256 if (res < 0)
3257 return res;
3258
3259 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
3260}
3261
3262static struct device_attribute dev_attr_fan_fan1_input =
3263 __ATTR(fan1_input, S_IRUGO,
3264 fan_fan1_input_show, NULL);
3265
3266/* sysfs fan fan_watchdog (driver) ------------------------------------- */
3267static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
3268 char *buf)
3269{
3270 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
3271}
3272
3273static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
3274 const char *buf, size_t count)
3275{
3276 unsigned long t;
3277
3278 if (parse_strtoul(buf, 120, &t))
3279 return -EINVAL;
3280
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03003281 if (!fan_control_allowed)
3282 return -EPERM;
3283
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003284 fan_watchdog_maxinterval = t;
3285 fan_watchdog_reset();
3286
3287 return count;
3288}
3289
3290static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
3291 fan_fan_watchdog_show, fan_fan_watchdog_store);
3292
3293/* --------------------------------------------------------------------- */
3294static struct attribute *fan_attributes[] = {
3295 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
3296 &dev_attr_fan_fan1_input.attr,
3297 NULL
3298};
3299
3300static const struct attribute_group fan_attr_group = {
3301 .attrs = fan_attributes,
3302};
3303
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003304static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003305{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003306 int rc;
3307
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003308 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
3309
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003310 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003311 fan_status_access_mode = TPACPI_FAN_NONE;
3312 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003313 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003314 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003315 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003316 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003317
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003318 IBM_ACPIHANDLE_INIT(fans);
3319 IBM_ACPIHANDLE_INIT(gfan);
3320 IBM_ACPIHANDLE_INIT(sfan);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003321
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003322 if (gfan_handle) {
3323 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003324 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003325 } else {
3326 /* all other ThinkPads: note that even old-style
3327 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02003328 if (likely(acpi_ec_read(fan_status_offset,
3329 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003330 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02003331
3332 /* In some ThinkPads, neither the EC nor the ACPI
3333 * DSDT initialize the fan status, and it ends up
3334 * being set to 0x07 when it *could* be either
3335 * 0x07 or 0x80.
3336 *
3337 * Enable for TP-1Y (T43), TP-78 (R51e),
3338 * TP-76 (R52), TP-70 (T43, R52), which are known
3339 * to be buggy. */
3340 if (fan_control_initial_status == 0x07 &&
3341 ibm_thinkpad_ec_found &&
3342 ((ibm_thinkpad_ec_found[0] == '1' &&
3343 ibm_thinkpad_ec_found[1] == 'Y') ||
3344 (ibm_thinkpad_ec_found[0] == '7' &&
3345 (ibm_thinkpad_ec_found[1] == '6' ||
3346 ibm_thinkpad_ec_found[1] == '8' ||
3347 ibm_thinkpad_ec_found[1] == '0'))
3348 )) {
3349 printk(IBM_NOTICE
3350 "fan_init: initial fan status is "
3351 "unknown, assuming it is in auto "
3352 "mode\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003353 tp_features.fan_ctrl_status_undef = 1;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02003354 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003355 } else {
3356 printk(IBM_ERR
3357 "ThinkPad ACPI EC access misbehaving, "
3358 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003359 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003360 }
3361 }
3362
3363 if (sfan_handle) {
3364 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003365 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02003366 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003367 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003368 } else {
3369 if (!gfan_handle) {
3370 /* gfan without sfan means no fan control */
3371 /* all other models implement TP EC 0x2f control */
3372
3373 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02003374 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003375 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003376 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003377 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003378 TPACPI_FAN_CMD_SPEED |
3379 TPACPI_FAN_CMD_LEVEL |
3380 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003381 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003382 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02003383 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003384 TPACPI_FAN_CMD_LEVEL |
3385 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003386 }
3387 }
3388 }
3389
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003390 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
3391 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
3392 fan_control_access_mode != TPACPI_FAN_WR_NONE),
3393 fan_status_access_mode, fan_control_access_mode);
3394
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03003395 /* fan control master switch */
3396 if (!fan_control_allowed) {
3397 fan_control_access_mode = TPACPI_FAN_WR_NONE;
3398 fan_control_commands = 0;
3399 dbg_printk(TPACPI_DBG_INIT,
3400 "fan control features disabled by parameter\n");
3401 }
3402
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003403 /* update fan_control_desired_level */
3404 if (fan_status_access_mode != TPACPI_FAN_NONE)
3405 fan_get_status_safe(NULL);
3406
3407 if (fan_status_access_mode != TPACPI_FAN_NONE ||
3408 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
3409 rc = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3410 &fan_attr_group);
3411 if (!(rc < 0))
3412 rc = driver_create_file(&tpacpi_pdriver.driver,
3413 &driver_attr_fan_watchdog);
3414 if (rc < 0)
3415 return rc;
3416 return 0;
3417 } else
3418 return 1;
3419}
3420
3421/*
3422 * Call with fan_mutex held
3423 */
3424static void fan_update_desired_level(u8 status)
3425{
3426 if ((status &
3427 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
3428 if (status > 7)
3429 fan_control_desired_level = 7;
3430 else
3431 fan_control_desired_level = status;
3432 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003433}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003434
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02003435static int fan_get_status(u8 *status)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003436{
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02003437 u8 s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003438
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02003439 /* TODO:
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003440 * Add TPACPI_FAN_RD_ACPI_FANS ? */
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02003441
Henrique de Moraes Holschuh3ef8a602006-11-24 11:47:10 -02003442 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003443 case TPACPI_FAN_RD_ACPI_GFAN:
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003444 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02003445
3446 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003447 return -EIO;
3448
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02003449 if (likely(status))
3450 *status = s & 0x07;
3451
3452 break;
3453
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003454 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02003455 /* all except 570, 600e/x, 770e, 770x */
3456 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
3457 return -EIO;
3458
3459 if (likely(status))
3460 *status = s;
3461
3462 break;
3463
3464 default:
3465 return -ENXIO;
3466 }
3467
3468 return 0;
3469}
3470
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003471static int fan_get_status_safe(u8 *status)
3472{
3473 int rc;
3474 u8 s;
3475
3476 rc = mutex_lock_interruptible(&fan_mutex);
3477 if (rc < 0)
3478 return rc;
3479 rc = fan_get_status(&s);
3480 if (!rc)
3481 fan_update_desired_level(s);
3482 mutex_unlock(&fan_mutex);
3483
3484 if (status)
3485 *status = s;
3486
3487 return rc;
3488}
3489
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003490static void fan_exit(void)
3491{
Henrique de Moraes Holschuh99fba3f2007-04-21 11:08:44 -03003492 vdbg_printk(TPACPI_DBG_EXIT, "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003493
3494 /* FIXME: can we really do this unconditionally? */
3495 sysfs_remove_group(&tpacpi_pdev->dev.kobj, &fan_attr_group);
3496 driver_remove_file(&tpacpi_pdriver.driver, &driver_attr_fan_watchdog);
3497
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003498 cancel_delayed_work(&fan_watchdog_task);
3499 flush_scheduled_work();
3500}
3501
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02003502static int fan_get_speed(unsigned int *speed)
3503{
3504 u8 hi, lo;
3505
3506 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003507 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02003508 /* all except 570, 600e/x, 770e, 770x */
3509 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
3510 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
3511 return -EIO;
3512
3513 if (likely(speed))
3514 *speed = (hi << 8) | lo;
3515
3516 break;
3517
3518 default:
3519 return -ENXIO;
3520 }
3521
3522 return 0;
3523}
3524
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003525static void fan_watchdog_fire(struct work_struct *ignored)
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003526{
Henrique de Moraes Holschuh99fba3f2007-04-21 11:08:44 -03003527 int rc;
3528
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003529 printk(IBM_NOTICE "fan watchdog: enabling fan\n");
Henrique de Moraes Holschuh99fba3f2007-04-21 11:08:44 -03003530 rc = fan_set_enable();
3531 if (rc < 0) {
3532 printk(IBM_ERR "fan watchdog: error %d while enabling fan, "
3533 "will try again later...\n", -rc);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003534 /* reschedule for later */
3535 fan_watchdog_reset();
3536 }
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003537}
3538
3539static void fan_watchdog_reset(void)
3540{
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03003541 static int fan_watchdog_active;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003542
Henrique de Moraes Holschuh4985cd02007-04-27 22:00:10 -03003543 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
3544 return;
3545
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003546 if (fan_watchdog_active)
3547 cancel_delayed_work(&fan_watchdog_task);
3548
3549 if (fan_watchdog_maxinterval > 0) {
3550 fan_watchdog_active = 1;
3551 if (!schedule_delayed_work(&fan_watchdog_task,
3552 msecs_to_jiffies(fan_watchdog_maxinterval
3553 * 1000))) {
3554 printk(IBM_ERR "failed to schedule the fan watchdog, "
3555 "watchdog will not trigger\n");
3556 }
3557 } else
3558 fan_watchdog_active = 0;
3559}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003560
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003561static int fan_set_level(int level)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003562{
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03003563 if (!fan_control_allowed)
3564 return -EPERM;
3565
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003566 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003567 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003568 if (level >= 0 && level <= 7) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003569 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003570 return -EIO;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003571 } else
3572 return -EINVAL;
3573 break;
3574
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003575 case TPACPI_FAN_WR_ACPI_FANS:
3576 case TPACPI_FAN_WR_TPEC:
3577 if ((level != TP_EC_FAN_AUTO) &&
3578 (level != TP_EC_FAN_FULLSPEED) &&
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02003579 ((level < 0) || (level > 7)))
3580 return -EINVAL;
3581
Henrique de Moraes Holschuheaa75712007-04-24 11:48:18 -03003582 /* safety net should the EC not support AUTO
3583 * or FULLSPEED mode bits and just ignore them */
3584 if (level & TP_EC_FAN_FULLSPEED)
3585 level |= 7; /* safety min speed 7 */
3586 else if (level & TP_EC_FAN_FULLSPEED)
3587 level |= 4; /* safety min speed 4 */
3588
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003589 if (!acpi_ec_write(fan_status_offset, level))
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02003590 return -EIO;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02003591 else
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003592 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02003593 break;
3594
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003595 default:
3596 return -ENXIO;
3597 }
3598 return 0;
3599}
3600
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003601static int fan_set_level_safe(int level)
3602{
3603 int rc;
3604
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03003605 if (!fan_control_allowed)
3606 return -EPERM;
3607
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003608 rc = mutex_lock_interruptible(&fan_mutex);
3609 if (rc < 0)
3610 return rc;
3611
3612 if (level == TPACPI_FAN_LAST_LEVEL)
3613 level = fan_control_desired_level;
3614
3615 rc = fan_set_level(level);
3616 if (!rc)
3617 fan_update_desired_level(level);
3618
3619 mutex_unlock(&fan_mutex);
3620 return rc;
3621}
3622
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003623static int fan_set_enable(void)
3624{
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02003625 u8 s;
3626 int rc;
3627
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03003628 if (!fan_control_allowed)
3629 return -EPERM;
3630
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003631 rc = mutex_lock_interruptible(&fan_mutex);
3632 if (rc < 0)
3633 return rc;
3634
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003635 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003636 case TPACPI_FAN_WR_ACPI_FANS:
3637 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003638 rc = fan_get_status(&s);
3639 if (rc < 0)
3640 break;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02003641
3642 /* Don't go out of emergency fan mode */
Henrique de Moraes Holschuheaa75712007-04-24 11:48:18 -03003643 if (s != 7) {
3644 s &= 0x07;
3645 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
3646 }
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02003647
3648 if (!acpi_ec_write(fan_status_offset, s))
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003649 rc = -EIO;
3650 else {
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003651 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003652 rc = 0;
3653 }
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02003654 break;
3655
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003656 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003657 rc = fan_get_status(&s);
3658 if (rc < 0)
3659 break;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02003660
3661 s &= 0x07;
3662
3663 /* Set fan to at least level 4 */
Henrique de Moraes Holschuheaa75712007-04-24 11:48:18 -03003664 s |= 4;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02003665
3666 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003667 rc= -EIO;
3668 else
3669 rc = 0;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003670 break;
3671
3672 default:
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003673 rc = -ENXIO;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003674 }
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003675
3676 mutex_unlock(&fan_mutex);
3677 return rc;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003678}
3679
3680static int fan_set_disable(void)
3681{
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003682 int rc;
3683
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03003684 if (!fan_control_allowed)
3685 return -EPERM;
3686
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003687 rc = mutex_lock_interruptible(&fan_mutex);
3688 if (rc < 0)
3689 return rc;
3690
3691 rc = 0;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003692 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003693 case TPACPI_FAN_WR_ACPI_FANS:
3694 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003695 if (!acpi_ec_write(fan_status_offset, 0x00))
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003696 rc = -EIO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003697 else {
3698 fan_control_desired_level = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003699 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003700 }
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003701 break;
3702
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003703 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02003704 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003705 rc = -EIO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003706 else
3707 fan_control_desired_level = 0;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02003708 break;
3709
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003710 default:
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003711 rc = -ENXIO;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003712 }
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003713
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003714
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003715 mutex_unlock(&fan_mutex);
3716 return rc;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003717}
3718
3719static int fan_set_speed(int speed)
3720{
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003721 int rc;
3722
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03003723 if (!fan_control_allowed)
3724 return -EPERM;
3725
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003726 rc = mutex_lock_interruptible(&fan_mutex);
3727 if (rc < 0)
3728 return rc;
3729
3730 rc = 0;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003731 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003732 case TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003733 if (speed >= 0 && speed <= 65535) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003734 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
3735 speed, speed, speed))
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003736 rc = -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003737 } else
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003738 rc = -EINVAL;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003739 break;
3740
3741 default:
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003742 rc = -ENXIO;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003743 }
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003744
3745 mutex_unlock(&fan_mutex);
3746 return rc;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003747}
3748
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003749static int fan_read(char *p)
3750{
3751 int len = 0;
3752 int rc;
3753 u8 status;
3754 unsigned int speed = 0;
3755
3756 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003757 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003758 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003759 if ((rc = fan_get_status_safe(&status)) < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003760 return rc;
3761
3762 len += sprintf(p + len, "status:\t\t%s\n"
3763 "level:\t\t%d\n",
3764 (status != 0) ? "enabled" : "disabled", status);
3765 break;
3766
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003767 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003768 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003769 if ((rc = fan_get_status_safe(&status)) < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003770 return rc;
3771
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003772 if (unlikely(tp_features.fan_ctrl_status_undef)) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003773 if (status != fan_control_initial_status)
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003774 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003775 else
3776 /* Return most likely status. In fact, it
3777 * might be the only possible status */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003778 status = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003779 }
3780
3781 len += sprintf(p + len, "status:\t\t%s\n",
3782 (status != 0) ? "enabled" : "disabled");
3783
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003784 if ((rc = fan_get_speed(&speed)) < 0)
3785 return rc;
3786
3787 len += sprintf(p + len, "speed:\t\t%d\n", speed);
3788
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003789 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003790 /* Disengaged mode takes precedence */
3791 len += sprintf(p + len, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003792 else if (status & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003793 len += sprintf(p + len, "level:\t\tauto\n");
3794 else
3795 len += sprintf(p + len, "level:\t\t%d\n", status);
3796 break;
3797
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003798 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003799 default:
3800 len += sprintf(p + len, "status:\t\tnot supported\n");
3801 }
3802
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003803 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003804 len += sprintf(p + len, "commands:\tlevel <level>");
3805
3806 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003807 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003808 len += sprintf(p + len, " (<level> is 0-7)\n");
3809 break;
3810
3811 default:
3812 len += sprintf(p + len, " (<level> is 0-7, "
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003813 "auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003814 break;
3815 }
3816 }
3817
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003818 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003819 len += sprintf(p + len, "commands:\tenable, disable\n"
3820 "commands:\twatchdog <timeout> (<timeout> is 0 (off), "
3821 "1-120 (seconds))\n");
3822
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003823 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003824 len += sprintf(p + len, "commands:\tspeed <speed>"
3825 " (<speed> is 0-65535)\n");
3826
3827 return len;
3828}
3829
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003830static int fan_write_cmd_level(const char *cmd, int *rc)
3831{
3832 int level;
3833
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02003834 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003835 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003836 else if ((strlencmp(cmd, "level disengaged") == 0) |
3837 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003838 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02003839 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003840 return 0;
3841
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003842 if ((*rc = fan_set_level_safe(level)) == -ENXIO)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003843 printk(IBM_ERR "level command accepted for unsupported "
3844 "access mode %d", fan_control_access_mode);
3845
3846 return 1;
3847}
3848
3849static int fan_write_cmd_enable(const char *cmd, int *rc)
3850{
3851 if (strlencmp(cmd, "enable") != 0)
3852 return 0;
3853
3854 if ((*rc = fan_set_enable()) == -ENXIO)
3855 printk(IBM_ERR "enable command accepted for unsupported "
3856 "access mode %d", fan_control_access_mode);
3857
3858 return 1;
3859}
3860
3861static int fan_write_cmd_disable(const char *cmd, int *rc)
3862{
3863 if (strlencmp(cmd, "disable") != 0)
3864 return 0;
3865
3866 if ((*rc = fan_set_disable()) == -ENXIO)
3867 printk(IBM_ERR "disable command accepted for unsupported "
3868 "access mode %d", fan_control_access_mode);
3869
3870 return 1;
3871}
3872
3873static int fan_write_cmd_speed(const char *cmd, int *rc)
3874{
3875 int speed;
3876
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02003877 /* TODO:
3878 * Support speed <low> <medium> <high> ? */
3879
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003880 if (sscanf(cmd, "speed %d", &speed) != 1)
3881 return 0;
3882
3883 if ((*rc = fan_set_speed(speed)) == -ENXIO)
3884 printk(IBM_ERR "speed command accepted for unsupported "
3885 "access mode %d", fan_control_access_mode);
3886
3887 return 1;
3888}
3889
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003890static int fan_write_cmd_watchdog(const char *cmd, int *rc)
3891{
3892 int interval;
3893
3894 if (sscanf(cmd, "watchdog %d", &interval) != 1)
3895 return 0;
3896
3897 if (interval < 0 || interval > 120)
3898 *rc = -EINVAL;
3899 else
3900 fan_watchdog_maxinterval = interval;
3901
3902 return 1;
3903}
3904
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003905static int fan_write(char *buf)
3906{
3907 char *cmd;
3908 int rc = 0;
3909
3910 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003911 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003912 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003913 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003914 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003915 fan_write_cmd_disable(cmd, &rc) ||
3916 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003917 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003918 fan_write_cmd_speed(cmd, &rc))
3919 )
3920 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003921 else if (!rc)
3922 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003923 }
3924
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003925 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003926}
3927
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003928static struct ibm_struct fan_driver_data = {
3929 .name = "fan",
3930 .read = fan_read,
3931 .write = fan_write,
3932 .exit = fan_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003933};
3934
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003935/****************************************************************************
3936 ****************************************************************************
3937 *
3938 * Infrastructure
3939 *
3940 ****************************************************************************
3941 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003943/* /proc support */
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03003944static struct proc_dir_entry *proc_dir;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003945
3946/* Subdriver registry */
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003947static LIST_HEAD(tpacpi_all_drivers);
3948
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003950/*
3951 * Module and infrastructure proble, init and exit handling
3952 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003954#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003955static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003956{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003957 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003958
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003959 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003960}
3961#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
3962
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003963static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964{
3965 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003966 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967 struct proc_dir_entry *entry;
3968
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003969 BUG_ON(ibm == NULL);
3970
3971 INIT_LIST_HEAD(&ibm->all_drivers);
3972
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03003973 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974 return 0;
3975
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003976 dbg_printk(TPACPI_DBG_INIT,
3977 "probing for %s\n", ibm->name);
3978
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003979 if (iibm->init) {
3980 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003981 if (ret > 0)
3982 return 0; /* probe failed */
3983 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003985
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03003986 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987 }
3988
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003989 if (ibm->acpi) {
3990 if (ibm->acpi->hid) {
3991 ret = register_tpacpi_subdriver(ibm);
3992 if (ret)
3993 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003994 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003995
3996 if (ibm->acpi->notify) {
3997 ret = setup_acpi_notify(ibm);
3998 if (ret == -ENODEV) {
3999 printk(IBM_NOTICE "disabling subdriver %s\n",
4000 ibm->name);
4001 ret = 0;
4002 goto err_out;
4003 }
4004 if (ret < 0)
4005 goto err_out;
4006 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004007 }
4008
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004009 dbg_printk(TPACPI_DBG_INIT,
4010 "%s installed\n", ibm->name);
4011
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004012 if (ibm->read) {
4013 entry = create_proc_entry(ibm->name,
4014 S_IFREG | S_IRUGO | S_IWUSR,
4015 proc_dir);
4016 if (!entry) {
4017 printk(IBM_ERR "unable to create proc entry %s\n",
4018 ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004019 ret = -ENODEV;
4020 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004021 }
4022 entry->owner = THIS_MODULE;
4023 entry->data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004024 entry->read_proc = &dispatch_procfs_read;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004025 if (ibm->write)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004026 entry->write_proc = &dispatch_procfs_write;
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004027 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004028 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004030 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
4031
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004033
4034err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004035 dbg_printk(TPACPI_DBG_INIT,
4036 "%s: at error exit path with result %d\n",
4037 ibm->name, ret);
4038
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004039 ibm_exit(ibm);
4040 return (ret < 0)? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041}
4042
4043static void ibm_exit(struct ibm_struct *ibm)
4044{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004045 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004046
4047 list_del_init(&ibm->all_drivers);
4048
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004049 if (ibm->flags.acpi_notify_installed) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004050 dbg_printk(TPACPI_DBG_EXIT,
4051 "%s: acpi_remove_notify_handler\n", ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004052 BUG_ON(!ibm->acpi);
4053 acpi_remove_notify_handler(*ibm->acpi->handle,
4054 ibm->acpi->type,
4055 dispatch_acpi_notify);
4056 ibm->flags.acpi_notify_installed = 0;
4057 ibm->flags.acpi_notify_installed = 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004058 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004060 if (ibm->flags.proc_created) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004061 dbg_printk(TPACPI_DBG_EXIT,
4062 "%s: remove_proc_entry\n", ibm->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063 remove_proc_entry(ibm->name, proc_dir);
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004064 ibm->flags.proc_created = 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004065 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004067 if (ibm->flags.acpi_driver_registered) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004068 dbg_printk(TPACPI_DBG_EXIT,
4069 "%s: acpi_bus_unregister_driver\n", ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004070 BUG_ON(!ibm->acpi);
4071 acpi_bus_unregister_driver(ibm->acpi->driver);
4072 kfree(ibm->acpi->driver);
4073 ibm->acpi->driver = NULL;
4074 ibm->flags.acpi_driver_registered = 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004075 }
4076
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004077 if (ibm->flags.init_called && ibm->exit) {
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004078 ibm->exit();
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004079 ibm->flags.init_called = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004081
4082 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083}
4084
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004085/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03004087static char *ibm_thinkpad_ec_found;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02004089static char* __init check_dmi_for_ec(void)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004090{
4091 struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02004092 char ec_fw_string[18];
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004093
4094 /*
4095 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
4096 * X32 or newer, all Z series; Some models must have an
4097 * up-to-date BIOS or they will not be detected.
4098 *
4099 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
4100 */
4101 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02004102 if (sscanf(dev->name,
4103 "IBM ThinkPad Embedded Controller -[%17c",
4104 ec_fw_string) == 1) {
4105 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
4106 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
4107 return kstrdup(ec_fw_string, GFP_KERNEL);
4108 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004109 }
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02004110 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111}
4112
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004113static int __init probe_for_thinkpad(void)
4114{
4115 int is_thinkpad;
4116
4117 if (acpi_disabled)
4118 return -ENODEV;
4119
4120 /*
4121 * Non-ancient models have better DMI tagging, but very old models
4122 * don't.
4123 */
4124 is_thinkpad = dmi_name_in_vendors("ThinkPad");
4125
4126 /* ec is required because many other handles are relative to it */
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004127 IBM_ACPIHANDLE_INIT(ec);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004128 if (!ec_handle) {
4129 if (is_thinkpad)
4130 printk(IBM_ERR
4131 "Not yet supported ThinkPad detected!\n");
4132 return -ENODEV;
4133 }
4134
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03004135 /*
4136 * Risks a regression on very old machines, but reduces potential
4137 * false positives a damn great deal
4138 */
4139 if (!is_thinkpad)
4140 is_thinkpad = dmi_name_in_vendors("IBM");
4141
4142 if (!is_thinkpad && !force_load)
4143 return -ENODEV;
4144
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004145 return 0;
4146}
4147
4148
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004149/* Module init, exit, parameters */
4150
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004151static struct ibm_init_struct ibms_init[] __initdata = {
4152 {
4153 .init = thinkpad_acpi_driver_init,
4154 .data = &thinkpad_acpi_driver_data,
4155 },
4156 {
4157 .init = hotkey_init,
4158 .data = &hotkey_driver_data,
4159 },
4160 {
4161 .init = bluetooth_init,
4162 .data = &bluetooth_driver_data,
4163 },
4164 {
4165 .init = wan_init,
4166 .data = &wan_driver_data,
4167 },
4168 {
4169 .init = video_init,
4170 .data = &video_driver_data,
4171 },
4172 {
4173 .init = light_init,
4174 .data = &light_driver_data,
4175 },
4176#ifdef CONFIG_THINKPAD_ACPI_DOCK
4177 {
4178 .init = dock_init,
4179 .data = &dock_driver_data[0],
4180 },
4181 {
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03004182 .init = dock_init2,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004183 .data = &dock_driver_data[1],
4184 },
4185#endif
4186#ifdef CONFIG_THINKPAD_ACPI_BAY
4187 {
4188 .init = bay_init,
4189 .data = &bay_driver_data,
4190 },
4191#endif
4192 {
4193 .init = cmos_init,
4194 .data = &cmos_driver_data,
4195 },
4196 {
4197 .init = led_init,
4198 .data = &led_driver_data,
4199 },
4200 {
4201 .init = beep_init,
4202 .data = &beep_driver_data,
4203 },
4204 {
4205 .init = thermal_init,
4206 .data = &thermal_driver_data,
4207 },
4208 {
4209 .data = &ecdump_driver_data,
4210 },
4211 {
4212 .init = brightness_init,
4213 .data = &brightness_driver_data,
4214 },
4215 {
4216 .data = &volume_driver_data,
4217 },
4218 {
4219 .init = fan_init,
4220 .data = &fan_driver_data,
4221 },
4222};
4223
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004224static int __init set_ibm_param(const char *val, struct kernel_param *kp)
4225{
4226 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004227 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004228
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004229 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
4230 ibm = ibms_init[i].data;
4231 BUG_ON(ibm == NULL);
4232
4233 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
4234 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004235 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004236 strcpy(ibms_init[i].param, val);
4237 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004238 return 0;
4239 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004240 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004241
4242 return -EINVAL;
4243}
4244
4245static int experimental;
4246module_param(experimental, int, 0);
4247
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03004248static u32 dbg_level;
4249module_param_named(debug, dbg_level, uint, 0);
4250
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03004251static int force_load;
4252module_param(force_load, int, 0);
4253
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03004254static int fan_control_allowed;
4255module_param_named(fan_control, fan_control_allowed, int, 0);
4256
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004257#define IBM_PARAM(feature) \
4258 module_param_call(feature, set_ibm_param, NULL, NULL, 0)
4259
4260IBM_PARAM(hotkey);
4261IBM_PARAM(bluetooth);
4262IBM_PARAM(video);
4263IBM_PARAM(light);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03004264#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004265IBM_PARAM(dock);
4266#endif
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03004267#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004268IBM_PARAM(bay);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03004269#endif /* CONFIG_THINKPAD_ACPI_BAY */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004270IBM_PARAM(cmos);
4271IBM_PARAM(led);
4272IBM_PARAM(beep);
4273IBM_PARAM(ecdump);
4274IBM_PARAM(brightness);
4275IBM_PARAM(volume);
4276IBM_PARAM(fan);
4277
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03004278static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279{
4280 int ret, i;
4281
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03004282 /* Driver-level probe */
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004283 ret = probe_for_thinkpad();
4284 if (ret)
4285 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03004287 /* Driver initialization */
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004288 ibm_thinkpad_ec_found = check_dmi_for_ec();
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004289 IBM_ACPIHANDLE_INIT(ecrd);
4290 IBM_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004292 proc_dir = proc_mkdir(IBM_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293 if (!proc_dir) {
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004294 printk(IBM_ERR "unable to create proc dir " IBM_PROC_DIR);
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03004295 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296 return -ENODEV;
4297 }
4298 proc_dir->owner = THIS_MODULE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004299
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03004300 ret = platform_driver_register(&tpacpi_pdriver);
4301 if (ret) {
4302 printk(IBM_ERR "unable to register platform driver\n");
4303 thinkpad_acpi_module_exit();
4304 return ret;
4305 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03004306 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
4307 if (ret) {
4308 printk(IBM_ERR "unable to create sysfs driver attributes\n");
4309 thinkpad_acpi_module_exit();
4310 return ret;
4311 }
4312
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03004313
4314 /* Device initialization */
4315 tpacpi_pdev = platform_device_register_simple(IBM_DRVR_NAME, -1,
4316 NULL, 0);
4317 if (IS_ERR(tpacpi_pdev)) {
4318 ret = PTR_ERR(tpacpi_pdev);
4319 tpacpi_pdev = NULL;
4320 printk(IBM_ERR "unable to register platform device\n");
4321 thinkpad_acpi_module_exit();
4322 return ret;
4323 }
4324 tpacpi_hwmon = hwmon_device_register(&tpacpi_pdev->dev);
4325 if (IS_ERR(tpacpi_hwmon)) {
4326 ret = PTR_ERR(tpacpi_hwmon);
4327 tpacpi_hwmon = NULL;
4328 printk(IBM_ERR "unable to register hwmon device\n");
4329 thinkpad_acpi_module_exit();
4330 return ret;
4331 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004332 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
4333 ret = ibm_init(&ibms_init[i]);
4334 if (ret >= 0 && *ibms_init[i].param)
4335 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03004337 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338 return ret;
4339 }
4340 }
4341
4342 return 0;
4343}
4344
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03004345static void thinkpad_acpi_module_exit(void)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004346{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004347 struct ibm_struct *ibm, *itmp;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004348
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004349 list_for_each_entry_safe_reverse(ibm, itmp,
4350 &tpacpi_all_drivers,
4351 all_drivers) {
4352 ibm_exit(ibm);
4353 }
4354
4355 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004356
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03004357 if (tpacpi_hwmon)
4358 hwmon_device_unregister(tpacpi_hwmon);
4359
4360 if (tpacpi_pdev)
4361 platform_device_unregister(tpacpi_pdev);
4362
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03004363 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03004364 platform_driver_unregister(&tpacpi_pdriver);
4365
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004366 if (proc_dir)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004367 remove_proc_entry(IBM_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004368
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004369 kfree(ibm_thinkpad_ec_found);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004370}
4371
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03004372module_init(thinkpad_acpi_module_init);
4373module_exit(thinkpad_acpi_module_exit);