blob: 3cf37bb55e9a6c712cbf21700c27b61a40fc1ec5 [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
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -0300736static int hotkey_get_wlsw(int *status)
737{
738 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
739 return -EIO;
740 return 0;
741}
742
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300743/* sysfs hotkey enable ------------------------------------------------- */
744static ssize_t hotkey_enable_show(struct device *dev,
745 struct device_attribute *attr,
746 char *buf)
747{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -0300748 int res, status;
749 u32 mask;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300750
751 res = hotkey_get(&status, &mask);
752 if (res)
753 return res;
754
755 return snprintf(buf, PAGE_SIZE, "%d\n", status);
756}
757
758static ssize_t hotkey_enable_store(struct device *dev,
759 struct device_attribute *attr,
760 const char *buf, size_t count)
761{
762 unsigned long t;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -0300763 int res, status;
764 u32 mask;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300765
766 if (parse_strtoul(buf, 1, &t))
767 return -EINVAL;
768
769 res = hotkey_get(&status, &mask);
770 if (!res)
771 res = hotkey_set(t, mask);
772
773 return (res) ? res : count;
774}
775
776static struct device_attribute dev_attr_hotkey_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -0300777 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300778 hotkey_enable_show, hotkey_enable_store);
779
780/* sysfs hotkey mask --------------------------------------------------- */
781static ssize_t hotkey_mask_show(struct device *dev,
782 struct device_attribute *attr,
783 char *buf)
784{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -0300785 int res, status;
786 u32 mask;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300787
788 res = hotkey_get(&status, &mask);
789 if (res)
790 return res;
791
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -0300792 return snprintf(buf, PAGE_SIZE, "0x%08x\n", mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300793}
794
795static ssize_t hotkey_mask_store(struct device *dev,
796 struct device_attribute *attr,
797 const char *buf, size_t count)
798{
799 unsigned long t;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -0300800 int res, status;
801 u32 mask;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300802
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -0300803 if (parse_strtoul(buf, 0xffffffffUL, &t))
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300804 return -EINVAL;
805
806 res = hotkey_get(&status, &mask);
807 if (!res)
808 hotkey_set(status, t);
809
810 return (res) ? res : count;
811}
812
813static struct device_attribute dev_attr_hotkey_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -0300814 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300815 hotkey_mask_show, hotkey_mask_store);
816
817/* sysfs hotkey bios_enabled ------------------------------------------- */
818static ssize_t hotkey_bios_enabled_show(struct device *dev,
819 struct device_attribute *attr,
820 char *buf)
821{
822 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
823}
824
825static struct device_attribute dev_attr_hotkey_bios_enabled =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -0300826 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300827
828/* sysfs hotkey bios_mask ---------------------------------------------- */
829static ssize_t hotkey_bios_mask_show(struct device *dev,
830 struct device_attribute *attr,
831 char *buf)
832{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -0300833 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300834}
835
836static struct device_attribute dev_attr_hotkey_bios_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -0300837 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300838
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -0300839/* sysfs hotkey all_mask ----------------------------------------------- */
840static ssize_t hotkey_all_mask_show(struct device *dev,
841 struct device_attribute *attr,
842 char *buf)
843{
844 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_all_mask);
845}
846
847static struct device_attribute dev_attr_hotkey_all_mask =
848 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
849
850/* sysfs hotkey recommended_mask --------------------------------------- */
851static ssize_t hotkey_recommended_mask_show(struct device *dev,
852 struct device_attribute *attr,
853 char *buf)
854{
855 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
856 hotkey_all_mask & ~hotkey_reserved_mask);
857}
858
859static struct device_attribute dev_attr_hotkey_recommended_mask =
860 __ATTR(hotkey_recommended_mask, S_IRUGO,
861 hotkey_recommended_mask_show, NULL);
862
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -0300863/* sysfs hotkey radio_sw ----------------------------------------------- */
864static ssize_t hotkey_radio_sw_show(struct device *dev,
865 struct device_attribute *attr,
866 char *buf)
867{
868 int res, s;
869 res = hotkey_get_wlsw(&s);
870 if (res < 0)
871 return res;
872
873 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
874}
875
876static struct device_attribute dev_attr_hotkey_radio_sw =
877 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
878
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300879/* --------------------------------------------------------------------- */
880
881static struct attribute *hotkey_mask_attributes[] = {
882 &dev_attr_hotkey_mask.attr,
883 &dev_attr_hotkey_bios_enabled.attr,
884 &dev_attr_hotkey_bios_mask.attr,
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -0300885 &dev_attr_hotkey_all_mask.attr,
886 &dev_attr_hotkey_recommended_mask.attr,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300887};
888
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300889static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300890{
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300891 int res;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -0300892 int status;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300893
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300894 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
895
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300896 IBM_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -0300897 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300898
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300899 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300900 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300901
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300902 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300903 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300904
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300905 if (tp_features.hotkey) {
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -0300906 hotkey_dev_attributes = create_attr_set(7, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300907 if (!hotkey_dev_attributes)
908 return -ENOMEM;
909 res = add_to_attr_set(hotkey_dev_attributes,
910 &dev_attr_hotkey_enable.attr);
911 if (res)
912 return res;
913
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300914 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
915 A30, R30, R31, T20-22, X20-21, X22-24 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300916 tp_features.hotkey_mask =
917 acpi_evalf(hkey_handle, NULL, "DHKN", "qv");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300918
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300919 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300920 str_supported(tp_features.hotkey_mask));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300921
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -0300922 if (tp_features.hotkey_mask) {
923 /* MHKA available in A31, R40, R40e, T4x, X31, and later */
924 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
925 "MHKA", "qd"))
926 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
927 }
928
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300929 res = hotkey_get(&hotkey_orig_status, &hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300930 if (!res && tp_features.hotkey_mask) {
931 res = add_many_to_attr_set(hotkey_dev_attributes,
932 hotkey_mask_attributes,
933 ARRAY_SIZE(hotkey_mask_attributes));
934 }
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -0300935
936 /* Not all thinkpads have a hardware radio switch */
937 if (!res && acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
938 tp_features.hotkey_wlsw = 1;
939 printk(IBM_INFO
940 "radio switch found; radios are %s\n",
941 enabled(status, 0));
942 res = add_to_attr_set(hotkey_dev_attributes,
943 &dev_attr_hotkey_radio_sw.attr);
944 }
945
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300946 if (!res)
947 res = register_attr_set_with_sysfs(
948 hotkey_dev_attributes,
949 &tpacpi_pdev->dev.kobj);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300950 if (res)
951 return res;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300952 }
953
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300954 return (tp_features.hotkey)? 0 : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300955}
956
957static void hotkey_exit(void)
958{
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300959 int res;
960
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300961 if (tp_features.hotkey) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300962 dbg_printk(TPACPI_DBG_EXIT, "restoring original hotkey mask\n");
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300963 res = hotkey_set(hotkey_orig_status, hotkey_orig_mask);
964 if (res)
965 printk(IBM_ERR "failed to restore hotkey to BIOS defaults\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300966 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300967
968 if (hotkey_dev_attributes) {
969 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
970 hotkey_dev_attributes = NULL;
971 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300972}
973
974static void hotkey_notify(struct ibm_struct *ibm, u32 event)
975{
976 int hkey;
977
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -0300978 if (event == 0x80 && acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300979 acpi_bus_generate_event(ibm->acpi->device, event, hkey);
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -0300980 } else {
981 printk(IBM_ERR "unknown hotkey notification event %d\n", event);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300982 acpi_bus_generate_event(ibm->acpi->device, event, 0);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300983 }
984}
985
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -0300986/*
987 * Call with hotkey_mutex held
988 */
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -0300989static int hotkey_get(int *status, u32 *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990{
991 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300992 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400993
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300994 if (tp_features.hotkey_mask)
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400995 if (!acpi_evalf(hkey_handle, mask, "DHKN", "d"))
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300996 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400997
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300998 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999}
1000
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001001/*
1002 * Call with hotkey_mutex held
1003 */
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001004static int hotkey_set(int status, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005{
1006 int i;
1007
1008 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001009 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001011 if (tp_features.hotkey_mask)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001012 for (i = 0; i < 32; i++) {
1013 int bit = ((1 << i) & mask) != 0;
1014 if (!acpi_evalf(hkey_handle,
1015 NULL, "MHKM", "vdd", i + 1, bit))
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001016 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001017 }
1018
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001019 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001020}
1021
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001022/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001023static int hotkey_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001025 int res, status;
1026 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 int len = 0;
1028
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001029 if (!tp_features.hotkey) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001030 len += sprintf(p + len, "status:\t\tnot supported\n");
1031 return len;
1032 }
1033
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001034 res = mutex_lock_interruptible(&hotkey_mutex);
1035 if (res < 0)
1036 return res;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001037 res = hotkey_get(&status, &mask);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001038 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001039 if (res)
1040 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
1042 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001043 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001044 len += sprintf(p + len, "mask:\t\t0x%08x\n", mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 len += sprintf(p + len,
1046 "commands:\tenable, disable, reset, <mask>\n");
1047 } else {
1048 len += sprintf(p + len, "mask:\t\tnot supported\n");
1049 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
1050 }
1051
1052 return len;
1053}
1054
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001055static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001057 int res, status;
1058 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 char *cmd;
1060 int do_cmd = 0;
1061
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001062 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 return -ENODEV;
1064
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001065 res = mutex_lock_interruptible(&hotkey_mutex);
1066 if (res < 0)
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001067 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001068
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001069 res = hotkey_get(&status, &mask);
1070 if (res)
1071 goto errexit;
1072
1073 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 while ((cmd = next_cmd(&buf))) {
1075 if (strlencmp(cmd, "enable") == 0) {
1076 status = 1;
1077 } else if (strlencmp(cmd, "disable") == 0) {
1078 status = 0;
1079 } else if (strlencmp(cmd, "reset") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001080 status = hotkey_orig_status;
1081 mask = hotkey_orig_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
1083 /* mask set */
1084 } else if (sscanf(cmd, "%x", &mask) == 1) {
1085 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001086 } else {
1087 res = -EINVAL;
1088 goto errexit;
1089 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 do_cmd = 1;
1091 }
1092
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001093 if (do_cmd)
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001094 res = hotkey_set(status, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001096errexit:
1097 mutex_unlock(&hotkey_mutex);
1098 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001099}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001101static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001102 .hid = IBM_HKEY_HID,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001103 .notify = hotkey_notify,
1104 .handle = &hkey_handle,
1105 .type = ACPI_DEVICE_NOTIFY,
1106};
1107
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001108static struct ibm_struct hotkey_driver_data = {
1109 .name = "hotkey",
1110 .read = hotkey_read,
1111 .write = hotkey_write,
1112 .exit = hotkey_exit,
1113 .acpi = &ibm_hotkey_acpidriver,
1114};
1115
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001116/*************************************************************************
1117 * Bluetooth subdriver
1118 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001120/* sysfs bluetooth enable ---------------------------------------------- */
1121static ssize_t bluetooth_enable_show(struct device *dev,
1122 struct device_attribute *attr,
1123 char *buf)
1124{
1125 int status;
1126
1127 status = bluetooth_get_radiosw();
1128 if (status < 0)
1129 return status;
1130
1131 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
1132}
1133
1134static ssize_t bluetooth_enable_store(struct device *dev,
1135 struct device_attribute *attr,
1136 const char *buf, size_t count)
1137{
1138 unsigned long t;
1139 int res;
1140
1141 if (parse_strtoul(buf, 1, &t))
1142 return -EINVAL;
1143
1144 res = bluetooth_set_radiosw(t);
1145
1146 return (res) ? res : count;
1147}
1148
1149static struct device_attribute dev_attr_bluetooth_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001150 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001151 bluetooth_enable_show, bluetooth_enable_store);
1152
1153/* --------------------------------------------------------------------- */
1154
1155static struct attribute *bluetooth_attributes[] = {
1156 &dev_attr_bluetooth_enable.attr,
1157 NULL
1158};
1159
1160static const struct attribute_group bluetooth_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001161 .attrs = bluetooth_attributes,
1162};
1163
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001164static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001166 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001167 int status = 0;
1168
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001169 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
1170
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001171 IBM_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001172
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001173 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1174 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001175 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001176 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001178 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
1179 str_supported(tp_features.bluetooth),
1180 status);
1181
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001182 if (tp_features.bluetooth) {
1183 if (!(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
1184 /* no bluetooth hardware present in system */
1185 tp_features.bluetooth = 0;
1186 dbg_printk(TPACPI_DBG_INIT,
1187 "bluetooth hardware not installed\n");
1188 } else {
1189 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
1190 &bluetooth_attr_group);
1191 if (res)
1192 return res;
1193 }
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001194 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001195
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001196 return (tp_features.bluetooth)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197}
1198
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001199static void bluetooth_exit(void)
1200{
1201 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
1202 &bluetooth_attr_group);
1203}
1204
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001205static int bluetooth_get_radiosw(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206{
1207 int status;
1208
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001209 if (!tp_features.bluetooth)
1210 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001212 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
1213 return -EIO;
1214
1215 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
1216}
1217
1218static int bluetooth_set_radiosw(int radio_on)
1219{
1220 int status;
1221
1222 if (!tp_features.bluetooth)
1223 return -ENODEV;
1224
1225 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
1226 return -EIO;
1227 if (radio_on)
1228 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
1229 else
1230 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
1231 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
1232 return -EIO;
1233
1234 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235}
1236
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001237/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001238static int bluetooth_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239{
1240 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001241 int status = bluetooth_get_radiosw();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001243 if (!tp_features.bluetooth)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 len += sprintf(p + len, "status:\t\tnot supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001246 len += sprintf(p + len, "status:\t\t%s\n",
1247 (status)? "enabled" : "disabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 len += sprintf(p + len, "commands:\tenable, disable\n");
1249 }
1250
1251 return len;
1252}
1253
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001254static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001258 if (!tp_features.bluetooth)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001259 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
1261 while ((cmd = next_cmd(&buf))) {
1262 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001263 bluetooth_set_radiosw(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001265 bluetooth_set_radiosw(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 } else
1267 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 }
1269
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 return 0;
1271}
1272
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001273static struct ibm_struct bluetooth_driver_data = {
1274 .name = "bluetooth",
1275 .read = bluetooth_read,
1276 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001277 .exit = bluetooth_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001278};
1279
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001280/*************************************************************************
1281 * Wan subdriver
1282 */
1283
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001284/* sysfs wan enable ---------------------------------------------------- */
1285static ssize_t wan_enable_show(struct device *dev,
1286 struct device_attribute *attr,
1287 char *buf)
1288{
1289 int status;
1290
1291 status = wan_get_radiosw();
1292 if (status < 0)
1293 return status;
1294
1295 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
1296}
1297
1298static ssize_t wan_enable_store(struct device *dev,
1299 struct device_attribute *attr,
1300 const char *buf, size_t count)
1301{
1302 unsigned long t;
1303 int res;
1304
1305 if (parse_strtoul(buf, 1, &t))
1306 return -EINVAL;
1307
1308 res = wan_set_radiosw(t);
1309
1310 return (res) ? res : count;
1311}
1312
1313static struct device_attribute dev_attr_wan_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001314 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001315 wan_enable_show, wan_enable_store);
1316
1317/* --------------------------------------------------------------------- */
1318
1319static struct attribute *wan_attributes[] = {
1320 &dev_attr_wan_enable.attr,
1321 NULL
1322};
1323
1324static const struct attribute_group wan_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001325 .attrs = wan_attributes,
1326};
1327
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001328static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001329{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001330 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001331 int status = 0;
1332
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001333 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
1334
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001335 IBM_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001336
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001337 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001338 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001339
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001340 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
1341 str_supported(tp_features.wan),
1342 status);
1343
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001344 if (tp_features.wan) {
1345 if (!(status & TP_ACPI_WANCARD_HWPRESENT)) {
1346 /* no wan hardware present in system */
1347 tp_features.wan = 0;
1348 dbg_printk(TPACPI_DBG_INIT,
1349 "wan hardware not installed\n");
1350 } else {
1351 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
1352 &wan_attr_group);
1353 if (res)
1354 return res;
1355 }
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001356 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001357
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001358 return (tp_features.wan)? 0 : 1;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001359}
1360
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001361static void wan_exit(void)
1362{
1363 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
1364 &wan_attr_group);
1365}
1366
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001367static int wan_get_radiosw(void)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001368{
1369 int status;
1370
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001371 if (!tp_features.wan)
1372 return -ENODEV;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001373
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001374 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
1375 return -EIO;
1376
1377 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
1378}
1379
1380static int wan_set_radiosw(int radio_on)
1381{
1382 int status;
1383
1384 if (!tp_features.wan)
1385 return -ENODEV;
1386
1387 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
1388 return -EIO;
1389 if (radio_on)
1390 status |= TP_ACPI_WANCARD_RADIOSSW;
1391 else
1392 status &= ~TP_ACPI_WANCARD_RADIOSSW;
1393 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
1394 return -EIO;
1395
1396 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001397}
1398
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001399/* procfs -------------------------------------------------------------- */
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001400static int wan_read(char *p)
1401{
1402 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001403 int status = wan_get_radiosw();
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001404
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001405 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001406 len += sprintf(p + len, "status:\t\tnot supported\n");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001407 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001408 len += sprintf(p + len, "status:\t\t%s\n",
1409 (status)? "enabled" : "disabled");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001410 len += sprintf(p + len, "commands:\tenable, disable\n");
1411 }
1412
1413 return len;
1414}
1415
1416static int wan_write(char *buf)
1417{
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001418 char *cmd;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001419
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001420 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001421 return -ENODEV;
1422
1423 while ((cmd = next_cmd(&buf))) {
1424 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001425 wan_set_radiosw(1);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001426 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001427 wan_set_radiosw(0);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001428 } else
1429 return -EINVAL;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001430 }
1431
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001432 return 0;
1433}
1434
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001435static struct ibm_struct wan_driver_data = {
1436 .name = "wan",
1437 .read = wan_read,
1438 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001439 .exit = wan_exit,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03001440 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001441};
1442
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001443/*************************************************************************
1444 * Video subdriver
1445 */
1446
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02001447static enum video_access_mode video_supported;
1448static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001449
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001450IBM_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
1451 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
1452 "\\_SB.PCI0.VID0", /* 770e */
1453 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
1454 "\\_SB.PCI0.AGP.VID", /* all others */
1455 ); /* R30, R31 */
1456
1457IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
1458
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001459static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001461 int ivga;
1462
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001463 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
1464
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001465 IBM_ACPIHANDLE_INIT(vid);
1466 IBM_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001467
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001468 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
1469 /* G41, assume IVGA doesn't change */
1470 vid_handle = vid2_handle;
1471
1472 if (!vid_handle)
1473 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001474 video_supported = TPACPI_VIDEO_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001475 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
1476 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001477 video_supported = TPACPI_VIDEO_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001478 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
1479 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001480 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001481 else
1482 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001483 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001485 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
1486 str_supported(video_supported != TPACPI_VIDEO_NONE),
1487 video_supported);
1488
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001489 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490}
1491
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001492static void video_exit(void)
1493{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001494 dbg_printk(TPACPI_DBG_EXIT,
1495 "restoring original video autoswitch mode\n");
1496 if (video_autosw_set(video_orig_autosw))
1497 printk(IBM_ERR "error while trying to restore original "
1498 "video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001499}
1500
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001501static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502{
1503 int status = 0;
1504 int i;
1505
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001506 switch (video_supported) {
1507 case TPACPI_VIDEO_570:
1508 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
1509 TP_ACPI_VIDEO_570_PHSCMD))
1510 return -EIO;
1511 status = i & TP_ACPI_VIDEO_570_PHSMASK;
1512 break;
1513 case TPACPI_VIDEO_770:
1514 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
1515 return -EIO;
1516 if (i)
1517 status |= TP_ACPI_VIDEO_S_LCD;
1518 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
1519 return -EIO;
1520 if (i)
1521 status |= TP_ACPI_VIDEO_S_CRT;
1522 break;
1523 case TPACPI_VIDEO_NEW:
1524 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
1525 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
1526 return -EIO;
1527 if (i)
1528 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001530 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
1531 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
1532 return -EIO;
1533 if (i)
1534 status |= TP_ACPI_VIDEO_S_LCD;
1535 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
1536 return -EIO;
1537 if (i)
1538 status |= TP_ACPI_VIDEO_S_DVI;
1539 break;
1540 default:
1541 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001542 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
1544 return status;
1545}
1546
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001547static int video_outputsw_set(int status)
1548{
1549 int autosw;
1550 int res = 0;
1551
1552 switch (video_supported) {
1553 case TPACPI_VIDEO_570:
1554 res = acpi_evalf(NULL, NULL,
1555 "\\_SB.PHS2", "vdd",
1556 TP_ACPI_VIDEO_570_PHS2CMD,
1557 status | TP_ACPI_VIDEO_570_PHS2SET);
1558 break;
1559 case TPACPI_VIDEO_770:
1560 autosw = video_autosw_get();
1561 if (autosw < 0)
1562 return autosw;
1563
1564 res = video_autosw_set(1);
1565 if (res)
1566 return res;
1567 res = acpi_evalf(vid_handle, NULL,
1568 "ASWT", "vdd", status * 0x100, 0);
1569 if (!autosw && video_autosw_set(autosw)) {
1570 printk(IBM_ERR "video auto-switch left enabled due to error\n");
1571 return -EIO;
1572 }
1573 break;
1574 case TPACPI_VIDEO_NEW:
1575 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
1576 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
1577 break;
1578 default:
1579 return -ENOSYS;
1580 }
1581
1582 return (res)? 0 : -EIO;
1583}
1584
1585static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001587 int autosw = 0;
1588
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001589 switch (video_supported) {
1590 case TPACPI_VIDEO_570:
1591 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
1592 return -EIO;
1593 break;
1594 case TPACPI_VIDEO_770:
1595 case TPACPI_VIDEO_NEW:
1596 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
1597 return -EIO;
1598 break;
1599 default:
1600 return -ENOSYS;
1601 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001602
1603 return autosw & 1;
1604}
1605
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001606static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001607{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001608 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001609 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001610 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001611}
1612
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001613static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001614{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001615 int autosw = video_autosw_get();
1616 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001617
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001618 if (autosw < 0)
1619 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001620
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001621 switch (video_supported) {
1622 case TPACPI_VIDEO_570:
1623 res = video_autosw_set(1);
1624 if (res)
1625 return res;
1626 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
1627 break;
1628 case TPACPI_VIDEO_770:
1629 case TPACPI_VIDEO_NEW:
1630 res = video_autosw_set(1);
1631 if (res)
1632 return res;
1633 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
1634 break;
1635 default:
1636 return -ENOSYS;
1637 }
1638 if (!autosw && video_autosw_set(autosw)) {
1639 printk(IBM_ERR "video auto-switch left enabled due to error\n");
1640 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001641 }
1642
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001643 return (res)? 0 : -EIO;
1644}
1645
1646static int video_expand_toggle(void)
1647{
1648 switch (video_supported) {
1649 case TPACPI_VIDEO_570:
1650 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
1651 0 : -EIO;
1652 case TPACPI_VIDEO_770:
1653 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
1654 0 : -EIO;
1655 case TPACPI_VIDEO_NEW:
1656 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
1657 0 : -EIO;
1658 default:
1659 return -ENOSYS;
1660 }
1661 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001662}
1663
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001664static int video_read(char *p)
1665{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001666 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001667 int len = 0;
1668
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001669 if (video_supported == TPACPI_VIDEO_NONE) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001670 len += sprintf(p + len, "status:\t\tnot supported\n");
1671 return len;
1672 }
1673
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001674 status = video_outputsw_get();
1675 if (status < 0)
1676 return status;
1677
1678 autosw = video_autosw_get();
1679 if (autosw < 0)
1680 return autosw;
1681
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001682 len += sprintf(p + len, "status:\t\tsupported\n");
1683 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
1684 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001685 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001686 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
1687 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
1688 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
1689 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001690 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001691 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
1692 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
1693 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
1694
1695 return len;
1696}
1697
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001698static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699{
1700 char *cmd;
1701 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001702 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001704 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001705 return -ENODEV;
1706
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001707 enable = 0;
1708 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709
1710 while ((cmd = next_cmd(&buf))) {
1711 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001712 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001714 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001716 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001718 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001719 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001720 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001721 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001722 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001723 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001724 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001726 res = video_autosw_set(1);
1727 if (res)
1728 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001730 res = video_autosw_set(0);
1731 if (res)
1732 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001734 res = video_outputsw_cycle();
1735 if (res)
1736 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001738 res = video_expand_toggle();
1739 if (res)
1740 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 } else
1742 return -EINVAL;
1743 }
1744
1745 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001746 status = video_outputsw_get();
1747 if (status < 0)
1748 return status;
1749 res = video_outputsw_set((status & ~disable) | enable);
1750 if (res)
1751 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 }
1753
1754 return 0;
1755}
1756
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001757static struct ibm_struct video_driver_data = {
1758 .name = "video",
1759 .read = video_read,
1760 .write = video_write,
1761 .exit = video_exit,
1762};
1763
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001764/*************************************************************************
1765 * Light (thinklight) subdriver
1766 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001768IBM_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
1769IBM_HANDLE(ledb, ec, "LEDB"); /* G4x */
1770
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001771static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001773 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
1774
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001775 IBM_ACPIHANDLE_INIT(ledb);
1776 IBM_ACPIHANDLE_INIT(lght);
1777 IBM_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001778
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001779 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001780 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001781
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001782 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001783 /* light status not supported on
1784 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001785 tp_features.light_status =
1786 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001788 vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001789 str_supported(tp_features.light));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001790
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001791 return (tp_features.light)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792}
1793
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001794static int light_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795{
1796 int len = 0;
1797 int status = 0;
1798
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001799 if (!tp_features.light) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001800 len += sprintf(p + len, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001801 } else if (!tp_features.light_status) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001802 len += sprintf(p + len, "status:\t\tunknown\n");
1803 len += sprintf(p + len, "commands:\ton, off\n");
1804 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
1806 return -EIO;
1807 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001808 len += sprintf(p + len, "commands:\ton, off\n");
1809 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810
1811 return len;
1812}
1813
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001814static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815{
1816 int cmos_cmd, lght_cmd;
1817 char *cmd;
1818 int success;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001819
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001820 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001821 return -ENODEV;
1822
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 while ((cmd = next_cmd(&buf))) {
1824 if (strlencmp(cmd, "on") == 0) {
1825 cmos_cmd = 0x0c;
1826 lght_cmd = 1;
1827 } else if (strlencmp(cmd, "off") == 0) {
1828 cmos_cmd = 0x0d;
1829 lght_cmd = 0;
1830 } else
1831 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001832
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 success = cmos_handle ?
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001834 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
1835 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 if (!success)
1837 return -EIO;
1838 }
1839
1840 return 0;
1841}
1842
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001843static struct ibm_struct light_driver_data = {
1844 .name = "light",
1845 .read = light_read,
1846 .write = light_write,
1847};
1848
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001849/*************************************************************************
1850 * Dock subdriver
1851 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03001853#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001854
1855IBM_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
1856 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
1857 "\\_SB.PCI0.PCI1.DOCK", /* all others */
1858 "\\_SB.PCI.ISA.SLCE", /* 570 */
1859 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
1860
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001861/* don't list other alternatives as we install a notify handler on the 570 */
1862IBM_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
1863
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03001864static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
1865 {
1866 .notify = dock_notify,
1867 .handle = &dock_handle,
1868 .type = ACPI_SYSTEM_NOTIFY,
1869 },
1870 {
1871 .hid = IBM_PCI_HID,
1872 .notify = dock_notify,
1873 .handle = &pci_handle,
1874 .type = ACPI_SYSTEM_NOTIFY,
1875 },
1876};
1877
1878static struct ibm_struct dock_driver_data[2] = {
1879 {
1880 .name = "dock",
1881 .read = dock_read,
1882 .write = dock_write,
1883 .acpi = &ibm_dock_acpidriver[0],
1884 },
1885 {
1886 .name = "dock",
1887 .acpi = &ibm_dock_acpidriver[1],
1888 },
1889};
1890
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891#define dock_docked() (_sta(dock_handle) & 1)
1892
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001893static int __init dock_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001894{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001895 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
1896
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001897 IBM_ACPIHANDLE_INIT(dock);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001898
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001899 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
1900 str_supported(dock_handle != NULL));
1901
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001902 return (dock_handle)? 0 : 1;
1903}
1904
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03001905static int __init dock_init2(struct ibm_init_struct *iibm)
1906{
1907 int dock2_needed;
1908
1909 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
1910
1911 if (dock_driver_data[0].flags.acpi_driver_registered &&
1912 dock_driver_data[0].flags.acpi_notify_installed) {
1913 IBM_ACPIHANDLE_INIT(pci);
1914 dock2_needed = (pci_handle != NULL);
1915 vdbg_printk(TPACPI_DBG_INIT,
1916 "dock PCI handler for the TP 570 is %s\n",
1917 str_supported(dock2_needed));
1918 } else {
1919 vdbg_printk(TPACPI_DBG_INIT,
1920 "dock subdriver part 2 not required\n");
1921 dock2_needed = 0;
1922 }
1923
1924 return (dock2_needed)? 0 : 1;
1925}
1926
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001927static void dock_notify(struct ibm_struct *ibm, u32 event)
1928{
1929 int docked = dock_docked();
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001930 int pci = ibm->acpi->hid && strstr(ibm->acpi->hid, IBM_PCI_HID);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001931
1932 if (event == 1 && !pci) /* 570 */
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001933 acpi_bus_generate_event(ibm->acpi->device, event, 1); /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001934 else if (event == 1 && pci) /* 570 */
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001935 acpi_bus_generate_event(ibm->acpi->device, event, 3); /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001936 else if (event == 3 && docked)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001937 acpi_bus_generate_event(ibm->acpi->device, event, 1); /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001938 else if (event == 3 && !docked)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001939 acpi_bus_generate_event(ibm->acpi->device, event, 2); /* undock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001940 else if (event == 0 && docked)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001941 acpi_bus_generate_event(ibm->acpi->device, event, 3); /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001942 else {
1943 printk(IBM_ERR "unknown dock event %d, status %d\n",
1944 event, _sta(dock_handle));
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001945 acpi_bus_generate_event(ibm->acpi->device, event, 0); /* unknown */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001946 }
1947}
1948
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001949static int dock_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950{
1951 int len = 0;
1952 int docked = dock_docked();
1953
1954 if (!dock_handle)
1955 len += sprintf(p + len, "status:\t\tnot supported\n");
1956 else if (!docked)
1957 len += sprintf(p + len, "status:\t\tundocked\n");
1958 else {
1959 len += sprintf(p + len, "status:\t\tdocked\n");
1960 len += sprintf(p + len, "commands:\tdock, undock\n");
1961 }
1962
1963 return len;
1964}
1965
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001966static int dock_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967{
1968 char *cmd;
1969
1970 if (!dock_docked())
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001971 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972
1973 while ((cmd = next_cmd(&buf))) {
1974 if (strlencmp(cmd, "undock") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001975 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
1976 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 return -EIO;
1978 } else if (strlencmp(cmd, "dock") == 0) {
1979 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
1980 return -EIO;
1981 } else
1982 return -EINVAL;
1983 }
1984
1985 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001986}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03001988#endif /* CONFIG_THINKPAD_ACPI_DOCK */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001990/*************************************************************************
1991 * Bay subdriver
1992 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03001994#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001995IBM_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
1996 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
1997 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
1998 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
1999 ); /* A21e, R30, R31 */
2000IBM_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
2001 "_EJ0", /* all others */
2002 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
2003IBM_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
2004 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
2005 ); /* all others */
2006IBM_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
2007 "_EJ0", /* 770x */
2008 ); /* all others */
2009
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002010static int __init bay_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002012 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
2013
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002014 IBM_ACPIHANDLE_INIT(bay);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002015 if (bay_handle)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002016 IBM_ACPIHANDLE_INIT(bay_ej);
2017 IBM_ACPIHANDLE_INIT(bay2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002018 if (bay2_handle)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002019 IBM_ACPIHANDLE_INIT(bay2_ej);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002020
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002021 tp_features.bay_status = bay_handle &&
2022 acpi_evalf(bay_handle, NULL, "_STA", "qv");
2023 tp_features.bay_status2 = bay2_handle &&
2024 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002025
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002026 tp_features.bay_eject = bay_handle && bay_ej_handle &&
2027 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
2028 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
2029 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002031 vdbg_printk(TPACPI_DBG_INIT,
2032 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002033 str_supported(tp_features.bay_status),
2034 str_supported(tp_features.bay_eject),
2035 str_supported(tp_features.bay_status2),
2036 str_supported(tp_features.bay_eject2));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002037
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002038 return (tp_features.bay_status || tp_features.bay_eject ||
2039 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040}
2041
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002042static void bay_notify(struct ibm_struct *ibm, u32 event)
2043{
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002044 acpi_bus_generate_event(ibm->acpi->device, event, 0);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002045}
2046
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002047#define bay_occupied(b) (_sta(b##_handle) & 1)
2048
2049static int bay_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050{
2051 int len = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002052 int occupied = bay_occupied(bay);
2053 int occupied2 = bay_occupied(bay2);
2054 int eject, eject2;
2055
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002056 len += sprintf(p + len, "status:\t\t%s\n",
2057 tp_features.bay_status ?
2058 (occupied ? "occupied" : "unoccupied") :
2059 "not supported");
2060 if (tp_features.bay_status2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002061 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
2062 "occupied" : "unoccupied");
2063
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002064 eject = tp_features.bay_eject && occupied;
2065 eject2 = tp_features.bay_eject2 && occupied2;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002066
2067 if (eject && eject2)
2068 len += sprintf(p + len, "commands:\teject, eject2\n");
2069 else if (eject)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 len += sprintf(p + len, "commands:\teject\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002071 else if (eject2)
2072 len += sprintf(p + len, "commands:\teject2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073
2074 return len;
2075}
2076
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002077static int bay_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078{
2079 char *cmd;
2080
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002081 if (!tp_features.bay_eject && !tp_features.bay_eject2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002082 return -ENODEV;
2083
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002085 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002086 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
2087 return -EIO;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002088 } else if (tp_features.bay_eject2 &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002089 strlencmp(cmd, "eject2") == 0) {
2090 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 return -EIO;
2092 } else
2093 return -EINVAL;
2094 }
2095
2096 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002097}
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002098
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002099static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
2100 .notify = bay_notify,
2101 .handle = &bay_handle,
2102 .type = ACPI_SYSTEM_NOTIFY,
2103};
2104
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002105static struct ibm_struct bay_driver_data = {
2106 .name = "bay",
2107 .read = bay_read,
2108 .write = bay_write,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002109 .acpi = &ibm_bay_acpidriver,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002110};
2111
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03002112#endif /* CONFIG_THINKPAD_ACPI_BAY */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002114/*************************************************************************
2115 * CMOS subdriver
2116 */
2117
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03002118/* sysfs cmos_command -------------------------------------------------- */
2119static ssize_t cmos_command_store(struct device *dev,
2120 struct device_attribute *attr,
2121 const char *buf, size_t count)
2122{
2123 unsigned long cmos_cmd;
2124 int res;
2125
2126 if (parse_strtoul(buf, 21, &cmos_cmd))
2127 return -EINVAL;
2128
2129 res = issue_thinkpad_cmos_command(cmos_cmd);
2130 return (res)? res : count;
2131}
2132
2133static struct device_attribute dev_attr_cmos_command =
2134 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
2135
2136/* --------------------------------------------------------------------- */
2137
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002138static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002139{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03002140 int res;
2141
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002142 vdbg_printk(TPACPI_DBG_INIT,
2143 "initializing cmos commands subdriver\n");
2144
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002145 IBM_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002146
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002147 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
2148 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03002149
2150 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
2151 if (res)
2152 return res;
2153
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002154 return (cmos_handle)? 0 : 1;
2155}
2156
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03002157static void cmos_exit(void)
2158{
2159 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
2160}
2161
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002162static int cmos_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163{
2164 int len = 0;
2165
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002166 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2167 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 if (!cmos_handle)
2169 len += sprintf(p + len, "status:\t\tnot supported\n");
2170 else {
2171 len += sprintf(p + len, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002172 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 }
2174
2175 return len;
2176}
2177
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002178static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179{
2180 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03002181 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182
2183 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002184 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
2185 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 /* cmos_cmd set */
2187 } else
2188 return -EINVAL;
2189
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03002190 res = issue_thinkpad_cmos_command(cmos_cmd);
2191 if (res)
2192 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 }
2194
2195 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002196}
2197
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002198static struct ibm_struct cmos_driver_data = {
2199 .name = "cmos",
2200 .read = cmos_read,
2201 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03002202 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002203};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002204
2205/*************************************************************************
2206 * LED subdriver
2207 */
2208
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02002209static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002210
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002211IBM_HANDLE(led, ec, "SLED", /* 570 */
2212 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
2213 "LED", /* all others */
2214 ); /* R30, R31 */
2215
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002216static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002217{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002218 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
2219
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002220 IBM_ACPIHANDLE_INIT(led);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002221
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002222 if (!led_handle)
2223 /* led not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002224 led_supported = TPACPI_LED_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002225 else if (strlencmp(led_path, "SLED") == 0)
2226 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002227 led_supported = TPACPI_LED_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002228 else if (strlencmp(led_path, "SYSL") == 0)
2229 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002230 led_supported = TPACPI_LED_OLD;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002231 else
2232 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002233 led_supported = TPACPI_LED_NEW;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002234
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002235 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
2236 str_supported(led_supported), led_supported);
2237
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002238 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002239}
2240
2241#define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
2242
2243static int led_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244{
2245 int len = 0;
2246
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002247 if (!led_supported) {
2248 len += sprintf(p + len, "status:\t\tnot supported\n");
2249 return len;
2250 }
2251 len += sprintf(p + len, "status:\t\tsupported\n");
2252
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002253 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002254 /* 570 */
2255 int i, status;
2256 for (i = 0; i < 8; i++) {
2257 if (!acpi_evalf(ec_handle,
2258 &status, "GLED", "dd", 1 << i))
2259 return -EIO;
2260 len += sprintf(p + len, "%d:\t\t%s\n",
2261 i, led_status(status));
2262 }
2263 }
2264
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 len += sprintf(p + len, "commands:\t"
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002266 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267
2268 return len;
2269}
2270
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002271/* off, on, blink */
2272static const int led_sled_arg1[] = { 0, 1, 3 };
2273static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
2274static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
2275static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
2276
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002277static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278{
2279 char *cmd;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002280 int led, ind, ret;
2281
2282 if (!led_supported)
2283 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284
2285 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002286 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 return -EINVAL;
2288
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002289 if (strstr(cmd, "off")) {
2290 ind = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 } else if (strstr(cmd, "on")) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002292 ind = 1;
2293 } else if (strstr(cmd, "blink")) {
2294 ind = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 } else
2296 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002297
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002298 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002299 /* 570 */
2300 led = 1 << led;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002302 led, led_sled_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 return -EIO;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002304 } else if (led_supported == TPACPI_LED_OLD) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002305 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
2306 led = 1 << led;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002307 ret = ec_write(TPACPI_LED_EC_HLMS, led);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002308 if (ret >= 0)
2309 ret =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002310 ec_write(TPACPI_LED_EC_HLBL,
Henrique de Moraes Holschuhe062e032007-03-23 17:33:55 -03002311 led * led_exp_hlbl[ind]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002312 if (ret >= 0)
2313 ret =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002314 ec_write(TPACPI_LED_EC_HLCL,
Henrique de Moraes Holschuhe062e032007-03-23 17:33:55 -03002315 led * led_exp_hlcl[ind]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002316 if (ret < 0)
2317 return ret;
2318 } else {
2319 /* all others */
2320 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
2321 led, led_led_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002323 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 }
2325
2326 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002327}
2328
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002329static struct ibm_struct led_driver_data = {
2330 .name = "led",
2331 .read = led_read,
2332 .write = led_write,
2333};
2334
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002335/*************************************************************************
2336 * Beep subdriver
2337 */
2338
2339IBM_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
2340
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002341static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002342{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002343 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
2344
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002345 IBM_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002346
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002347 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
2348 str_supported(beep_handle != NULL));
2349
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002350 return (beep_handle)? 0 : 1;
2351}
2352
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002353static int beep_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354{
2355 int len = 0;
2356
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002357 if (!beep_handle)
2358 len += sprintf(p + len, "status:\t\tnot supported\n");
2359 else {
2360 len += sprintf(p + len, "status:\t\tsupported\n");
2361 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
2362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363
2364 return len;
2365}
2366
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002367static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368{
2369 char *cmd;
2370 int beep_cmd;
2371
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002372 if (!beep_handle)
2373 return -ENODEV;
2374
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002376 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
2377 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 /* beep_cmd set */
2379 } else
2380 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002381 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 return -EIO;
2383 }
2384
2385 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002386}
2387
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002388static struct ibm_struct beep_driver_data = {
2389 .name = "beep",
2390 .read = beep_read,
2391 .write = beep_write,
2392};
2393
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002394/*************************************************************************
2395 * Thermal subdriver
2396 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002397
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002398static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002399
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03002400/* sysfs temp##_input -------------------------------------------------- */
2401
2402static ssize_t thermal_temp_input_show(struct device *dev,
2403 struct device_attribute *attr,
2404 char *buf)
2405{
2406 struct sensor_device_attribute *sensor_attr =
2407 to_sensor_dev_attr(attr);
2408 int idx = sensor_attr->index;
2409 s32 value;
2410 int res;
2411
2412 res = thermal_get_sensor(idx, &value);
2413 if (res)
2414 return res;
2415 if (value == TP_EC_THERMAL_TMP_NA * 1000)
2416 return -ENXIO;
2417
2418 return snprintf(buf, PAGE_SIZE, "%d\n", value);
2419}
2420
2421#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
2422 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, thermal_temp_input_show, NULL, _idxB)
2423
2424static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
2425 THERMAL_SENSOR_ATTR_TEMP(1, 0),
2426 THERMAL_SENSOR_ATTR_TEMP(2, 1),
2427 THERMAL_SENSOR_ATTR_TEMP(3, 2),
2428 THERMAL_SENSOR_ATTR_TEMP(4, 3),
2429 THERMAL_SENSOR_ATTR_TEMP(5, 4),
2430 THERMAL_SENSOR_ATTR_TEMP(6, 5),
2431 THERMAL_SENSOR_ATTR_TEMP(7, 6),
2432 THERMAL_SENSOR_ATTR_TEMP(8, 7),
2433 THERMAL_SENSOR_ATTR_TEMP(9, 8),
2434 THERMAL_SENSOR_ATTR_TEMP(10, 9),
2435 THERMAL_SENSOR_ATTR_TEMP(11, 10),
2436 THERMAL_SENSOR_ATTR_TEMP(12, 11),
2437 THERMAL_SENSOR_ATTR_TEMP(13, 12),
2438 THERMAL_SENSOR_ATTR_TEMP(14, 13),
2439 THERMAL_SENSOR_ATTR_TEMP(15, 14),
2440 THERMAL_SENSOR_ATTR_TEMP(16, 15),
2441};
2442
2443#define THERMAL_ATTRS(X) \
2444 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
2445
2446static struct attribute *thermal_temp_input_attr[] = {
2447 THERMAL_ATTRS(8),
2448 THERMAL_ATTRS(9),
2449 THERMAL_ATTRS(10),
2450 THERMAL_ATTRS(11),
2451 THERMAL_ATTRS(12),
2452 THERMAL_ATTRS(13),
2453 THERMAL_ATTRS(14),
2454 THERMAL_ATTRS(15),
2455 THERMAL_ATTRS(0),
2456 THERMAL_ATTRS(1),
2457 THERMAL_ATTRS(2),
2458 THERMAL_ATTRS(3),
2459 THERMAL_ATTRS(4),
2460 THERMAL_ATTRS(5),
2461 THERMAL_ATTRS(6),
2462 THERMAL_ATTRS(7),
2463 NULL
2464};
2465
2466static const struct attribute_group thermal_temp_input16_group = {
2467 .attrs = thermal_temp_input_attr
2468};
2469
2470static const struct attribute_group thermal_temp_input8_group = {
2471 .attrs = &thermal_temp_input_attr[8]
2472};
2473
2474#undef THERMAL_SENSOR_ATTR_TEMP
2475#undef THERMAL_ATTRS
2476
2477/* --------------------------------------------------------------------- */
2478
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002479static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002480{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002481 u8 t, ta1, ta2;
2482 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002483 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03002484 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002485
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002486 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
2487
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002488 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002489
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002490 if (ibm_thinkpad_ec_found && experimental) {
2491 /*
2492 * Direct EC access mode: sensors at registers
2493 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
2494 * non-implemented, thermal sensors return 0x80 when
2495 * not available
2496 */
2497
2498 ta1 = ta2 = 0;
2499 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002500 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002501 ta1 |= t;
2502 } else {
2503 ta1 = 0;
2504 break;
2505 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002506 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002507 ta2 |= t;
2508 } else {
2509 ta1 = 0;
2510 break;
2511 }
2512 }
2513 if (ta1 == 0) {
2514 /* This is sheer paranoia, but we handle it anyway */
2515 if (acpi_tmp7) {
2516 printk(IBM_ERR
2517 "ThinkPad ACPI EC access misbehaving, "
2518 "falling back to ACPI TMPx access mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002519 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002520 } else {
2521 printk(IBM_ERR
2522 "ThinkPad ACPI EC access misbehaving, "
2523 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002524 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002525 }
2526 } else {
2527 thermal_read_mode =
2528 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002529 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002530 }
2531 } else if (acpi_tmp7) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002532 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
2533 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002534 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002535 } else {
2536 /* Standard ACPI TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002537 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002538 }
2539 } else {
2540 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002541 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002542 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002543
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002544 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
2545 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
2546 thermal_read_mode);
2547
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03002548 switch(thermal_read_mode) {
2549 case TPACPI_THERMAL_TPEC_16:
2550 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2551 &thermal_temp_input16_group);
2552 if (res)
2553 return res;
2554 break;
2555 case TPACPI_THERMAL_TPEC_8:
2556 case TPACPI_THERMAL_ACPI_TMP07:
2557 case TPACPI_THERMAL_ACPI_UPDT:
2558 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2559 &thermal_temp_input8_group);
2560 if (res)
2561 return res;
2562 break;
2563 case TPACPI_THERMAL_NONE:
2564 default:
2565 return 1;
2566 }
2567
2568 return 0;
2569}
2570
2571static void thermal_exit(void)
2572{
2573 switch(thermal_read_mode) {
2574 case TPACPI_THERMAL_TPEC_16:
2575 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2576 &thermal_temp_input16_group);
2577 break;
2578 case TPACPI_THERMAL_TPEC_8:
2579 case TPACPI_THERMAL_ACPI_TMP07:
2580 case TPACPI_THERMAL_ACPI_UPDT:
2581 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2582 &thermal_temp_input16_group);
2583 break;
2584 case TPACPI_THERMAL_NONE:
2585 default:
2586 break;
2587 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002588}
2589
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002590/* idx is zero-based */
2591static int thermal_get_sensor(int idx, s32 *value)
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002592{
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002593 int t;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002594 s8 tmp;
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002595 char tmpi[5];
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002596
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002597 t = TP_EC_THERMAL_TMP0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002598
2599 switch (thermal_read_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002600#if TPACPI_MAX_THERMAL_SENSORS >= 16
2601 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002602 if (idx >= 8 && idx <= 15) {
2603 t = TP_EC_THERMAL_TMP8;
2604 idx -= 8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002605 }
2606 /* fallthrough */
2607#endif
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002608 case TPACPI_THERMAL_TPEC_8:
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002609 if (idx <= 7) {
2610 if (!acpi_ec_read(t + idx, &tmp))
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002611 return -EIO;
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002612 *value = tmp * 1000;
2613 return 0;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002614 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002615 break;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002616
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002617 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002618 if (idx <= 7) {
2619 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
2620 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
2621 return -EIO;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002622 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
2623 return -EIO;
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002624 *value = (t - 2732) * 100;
2625 return 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002626 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002627 break;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002628
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002629 case TPACPI_THERMAL_ACPI_TMP07:
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002630 if (idx <= 7) {
2631 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002632 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
2633 return -EIO;
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002634 *value = t * 1000;
2635 return 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002636 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002637 break;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002638
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002639 case TPACPI_THERMAL_NONE:
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002640 default:
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002641 return -ENOSYS;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002642 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002643
2644 return -EINVAL;
2645}
2646
2647static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
2648{
2649 int res, i;
2650 int n;
2651
2652 n = 8;
2653 i = 0;
2654
2655 if (!s)
2656 return -EINVAL;
2657
2658 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
2659 n = 16;
2660
2661 for(i = 0 ; i < n; i++) {
2662 res = thermal_get_sensor(i, &s->temp[i]);
2663 if (res)
2664 return res;
2665 }
2666
2667 return n;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002668}
2669
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002670static int thermal_read(char *p)
2671{
2672 int len = 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002673 int n, i;
2674 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002675
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002676 n = thermal_get_sensors(&t);
2677 if (unlikely(n < 0))
2678 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002679
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002680 len += sprintf(p + len, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002681
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002682 if (n > 0) {
2683 for (i = 0; i < (n - 1); i++)
2684 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
2685 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
2686 } else
2687 len += sprintf(p + len, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002688
2689 return len;
2690}
2691
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002692static struct ibm_struct thermal_driver_data = {
2693 .name = "thermal",
2694 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03002695 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002696};
2697
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002698/*************************************************************************
2699 * EC Dump subdriver
2700 */
2701
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002702static u8 ecdump_regs[256];
2703
2704static int ecdump_read(char *p)
2705{
2706 int len = 0;
2707 int i, j;
2708 u8 v;
2709
2710 len += sprintf(p + len, "EC "
2711 " +00 +01 +02 +03 +04 +05 +06 +07"
2712 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
2713 for (i = 0; i < 256; i += 16) {
2714 len += sprintf(p + len, "EC 0x%02x:", i);
2715 for (j = 0; j < 16; j++) {
2716 if (!acpi_ec_read(i + j, &v))
2717 break;
2718 if (v != ecdump_regs[i + j])
2719 len += sprintf(p + len, " *%02x", v);
2720 else
2721 len += sprintf(p + len, " %02x", v);
2722 ecdump_regs[i + j] = v;
2723 }
2724 len += sprintf(p + len, "\n");
2725 if (j != 16)
2726 break;
2727 }
2728
2729 /* These are way too dangerous to advertise openly... */
2730#if 0
2731 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
2732 " (<offset> is 00-ff, <value> is 00-ff)\n");
2733 len += sprintf(p + len, "commands:\t0x<offset> <value> "
2734 " (<offset> is 00-ff, <value> is 0-255)\n");
2735#endif
2736 return len;
2737}
2738
2739static int ecdump_write(char *buf)
2740{
2741 char *cmd;
2742 int i, v;
2743
2744 while ((cmd = next_cmd(&buf))) {
2745 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
2746 /* i and v set */
2747 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
2748 /* i and v set */
2749 } else
2750 return -EINVAL;
2751 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
2752 if (!acpi_ec_write(i, v))
2753 return -EIO;
2754 } else
2755 return -EINVAL;
2756 }
2757
2758 return 0;
2759}
2760
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002761static struct ibm_struct ecdump_driver_data = {
2762 .name = "ecdump",
2763 .read = ecdump_read,
2764 .write = ecdump_write,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03002765 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002766};
2767
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002768/*************************************************************************
2769 * Backlight/brightness subdriver
2770 */
Holger Macht8acb0252006-10-20 14:30:28 -07002771
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03002772static struct backlight_device *ibm_backlight_device;
Holger Macht8acb0252006-10-20 14:30:28 -07002773
Richard Purdie599a52d2007-02-10 23:07:48 +00002774static struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02002775 .get_brightness = brightness_get,
2776 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02002777};
2778
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002779static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02002780{
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02002781 int b;
2782
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002783 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
2784
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02002785 b = brightness_get(NULL);
2786 if (b < 0)
2787 return b;
2788
Henrique de Moraes Holschuh7d5a0152007-04-24 11:48:20 -03002789 ibm_backlight_device = backlight_device_register(
2790 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
2791 &ibm_backlight_data);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02002792 if (IS_ERR(ibm_backlight_device)) {
2793 printk(IBM_ERR "Could not register backlight device\n");
2794 return PTR_ERR(ibm_backlight_device);
2795 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002796 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02002797
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02002798 ibm_backlight_device->props.max_brightness = 7;
2799 ibm_backlight_device->props.brightness = b;
2800 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00002801
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02002802 return 0;
2803}
2804
2805static void brightness_exit(void)
2806{
2807 if (ibm_backlight_device) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002808 vdbg_printk(TPACPI_DBG_EXIT,
2809 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02002810 backlight_device_unregister(ibm_backlight_device);
2811 ibm_backlight_device = NULL;
2812 }
2813}
2814
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002815static int brightness_update_status(struct backlight_device *bd)
2816{
2817 return brightness_set(
2818 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
2819 bd->props.power == FB_BLANK_UNBLANK) ?
2820 bd->props.brightness : 0);
2821}
2822
2823static int brightness_get(struct backlight_device *bd)
2824{
2825 u8 level;
2826 if (!acpi_ec_read(brightness_offset, &level))
2827 return -EIO;
2828
2829 level &= 0x7;
2830
2831 return level;
2832}
2833
2834static int brightness_set(int value)
2835{
2836 int cmos_cmd, inc, i;
2837 int current_value = brightness_get(NULL);
2838
2839 value &= 7;
2840
2841 cmos_cmd = value > current_value ? TP_CMOS_BRIGHTNESS_UP : TP_CMOS_BRIGHTNESS_DOWN;
2842 inc = value > current_value ? 1 : -1;
2843 for (i = current_value; i != value; i += inc) {
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03002844 if (issue_thinkpad_cmos_command(cmos_cmd))
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002845 return -EIO;
2846 if (!acpi_ec_write(brightness_offset, i + inc))
2847 return -EIO;
2848 }
2849
2850 return 0;
2851}
2852
2853static int brightness_read(char *p)
2854{
2855 int len = 0;
2856 int level;
2857
2858 if ((level = brightness_get(NULL)) < 0) {
2859 len += sprintf(p + len, "level:\t\tunreadable\n");
2860 } else {
2861 len += sprintf(p + len, "level:\t\t%d\n", level & 0x7);
2862 len += sprintf(p + len, "commands:\tup, down\n");
2863 len += sprintf(p + len, "commands:\tlevel <level>"
2864 " (<level> is 0-7)\n");
2865 }
2866
2867 return len;
2868}
2869
2870static int brightness_write(char *buf)
2871{
2872 int level;
2873 int new_level;
2874 char *cmd;
2875
2876 while ((cmd = next_cmd(&buf))) {
2877 if ((level = brightness_get(NULL)) < 0)
2878 return level;
2879 level &= 7;
2880
2881 if (strlencmp(cmd, "up") == 0) {
2882 new_level = level == 7 ? 7 : level + 1;
2883 } else if (strlencmp(cmd, "down") == 0) {
2884 new_level = level == 0 ? 0 : level - 1;
2885 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
2886 new_level >= 0 && new_level <= 7) {
2887 /* new_level set */
2888 } else
2889 return -EINVAL;
2890
2891 brightness_set(new_level);
2892 }
2893
2894 return 0;
2895}
2896
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002897static struct ibm_struct brightness_driver_data = {
2898 .name = "brightness",
2899 .read = brightness_read,
2900 .write = brightness_write,
2901 .exit = brightness_exit,
2902};
2903
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002904/*************************************************************************
2905 * Volume subdriver
2906 */
2907
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002908static int volume_read(char *p)
2909{
2910 int len = 0;
2911 u8 level;
2912
2913 if (!acpi_ec_read(volume_offset, &level)) {
2914 len += sprintf(p + len, "level:\t\tunreadable\n");
2915 } else {
2916 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
2917 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
2918 len += sprintf(p + len, "commands:\tup, down, mute\n");
2919 len += sprintf(p + len, "commands:\tlevel <level>"
2920 " (<level> is 0-15)\n");
2921 }
2922
2923 return len;
2924}
2925
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002926static int volume_write(char *buf)
2927{
2928 int cmos_cmd, inc, i;
2929 u8 level, mute;
2930 int new_level, new_mute;
2931 char *cmd;
2932
2933 while ((cmd = next_cmd(&buf))) {
2934 if (!acpi_ec_read(volume_offset, &level))
2935 return -EIO;
2936 new_mute = mute = level & 0x40;
2937 new_level = level = level & 0xf;
2938
2939 if (strlencmp(cmd, "up") == 0) {
2940 if (mute)
2941 new_mute = 0;
2942 else
2943 new_level = level == 15 ? 15 : level + 1;
2944 } else if (strlencmp(cmd, "down") == 0) {
2945 if (mute)
2946 new_mute = 0;
2947 else
2948 new_level = level == 0 ? 0 : level - 1;
2949 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
2950 new_level >= 0 && new_level <= 15) {
2951 /* new_level set */
2952 } else if (strlencmp(cmd, "mute") == 0) {
2953 new_mute = 0x40;
2954 } else
2955 return -EINVAL;
2956
2957 if (new_level != level) { /* mute doesn't change */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002958 cmos_cmd = new_level > level ? TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002959 inc = new_level > level ? 1 : -1;
2960
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03002961 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002962 !acpi_ec_write(volume_offset, level)))
2963 return -EIO;
2964
2965 for (i = level; i != new_level; i += inc)
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03002966 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002967 !acpi_ec_write(volume_offset, i + inc))
2968 return -EIO;
2969
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03002970 if (mute && (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002971 !acpi_ec_write(volume_offset,
2972 new_level + mute)))
2973 return -EIO;
2974 }
2975
2976 if (new_mute != mute) { /* level doesn't change */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002977 cmos_cmd = new_mute ? TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002978
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03002979 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002980 !acpi_ec_write(volume_offset, level + new_mute))
2981 return -EIO;
2982 }
2983 }
2984
2985 return 0;
2986}
2987
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002988static struct ibm_struct volume_driver_data = {
2989 .name = "volume",
2990 .read = volume_read,
2991 .write = volume_write,
2992};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002993
2994/*************************************************************************
2995 * Fan subdriver
2996 */
2997
2998/*
2999 * FAN ACCESS MODES
3000 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003001 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003002 * ACPI GFAN method: returns fan level
3003 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003004 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03003005 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003006 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003007 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003008 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
3009 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03003010 * EC 0x2f (HFSP) might be available *for reading*, but do not use
3011 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003012 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003013 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03003014 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
3015 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003016 *
3017 * Fan speed changes of any sort (including those caused by the
3018 * disengaged mode) are usually done slowly by the firmware as the
3019 * maximum ammount of fan duty cycle change per second seems to be
3020 * limited.
3021 *
3022 * Reading is not available if GFAN exists.
3023 * Writing is not available if SFAN exists.
3024 *
3025 * Bits
3026 * 7 automatic mode engaged;
3027 * (default operation mode of the ThinkPad)
3028 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03003029 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003030 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03003031 * the tachometer while the fan controller ramps up
3032 * the speed (which can take up to a few *minutes*).
3033 * Speeds up fan to 100% duty-cycle, which is far above
3034 * the standard RPM levels. It is not impossible that
3035 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003036 * 5-3 unused in some models. Extra bits for fan level
3037 * in others, but still useless as all values above
3038 * 7 map to the same speed as level 7 in these models.
3039 * 2-0 fan level (0..7 usually)
3040 * 0x00 = stop
3041 * 0x07 = max (set when temperatures critical)
3042 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003043 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003044 *
3045 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
3046 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
3047 * does so, its initial value is meaningless (0x07).
3048 *
3049 * For firmware bugs, refer to:
3050 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
3051 *
3052 * ----
3053 *
3054 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
3055 * Main fan tachometer reading (in RPM)
3056 *
3057 * This register is present on all ThinkPads with a new-style EC, and
3058 * it is known not to be present on the A21m/e, and T22, as there is
3059 * something else in offset 0x84 according to the ACPI DSDT. Other
3060 * ThinkPads from this same time period (and earlier) probably lack the
3061 * tachometer as well.
3062 *
3063 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
3064 * was never fixed by IBM to report the EC firmware version string
3065 * probably support the tachometer (like the early X models), so
3066 * detecting it is quite hard. We need more data to know for sure.
3067 *
3068 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
3069 * might result.
3070 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03003071 * FIRMWARE BUG: may go stale while the EC is switching to full speed
3072 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003073 *
3074 * For firmware bugs, refer to:
3075 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
3076 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003077 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003078 * ThinkPad X31, X40, X41. Not available in the X60.
3079 *
3080 * FANS ACPI handle: takes three arguments: low speed, medium speed,
3081 * high speed. ACPI DSDT seems to map these three speeds to levels
3082 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
3083 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
3084 *
3085 * The speeds are stored on handles
3086 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
3087 *
3088 * There are three default speed sets, acessible as handles:
3089 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
3090 *
3091 * ACPI DSDT switches which set is in use depending on various
3092 * factors.
3093 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003094 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003095 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
3096 * but the ACPI tables just mention level 7.
3097 */
3098
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003099static enum fan_status_access_mode fan_status_access_mode;
3100static enum fan_control_access_mode fan_control_access_mode;
3101static enum fan_control_commands fan_control_commands;
3102
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02003103static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003104static u8 fan_control_desired_level;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02003105
Len Brown25c68a32006-12-08 04:43:41 -05003106static void fan_watchdog_fire(struct work_struct *ignored);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003107static int fan_watchdog_maxinterval;
Len Brown25c68a32006-12-08 04:43:41 -05003108static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003109
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003110IBM_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
3111IBM_HANDLE(gfan, ec, "GFAN", /* 570 */
3112 "\\FSPD", /* 600e/x, 770e, 770x */
3113 ); /* all others */
3114IBM_HANDLE(sfan, ec, "SFAN", /* 570 */
3115 "JFNS", /* 770x-JL */
3116 ); /* all others */
3117
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003118/*
3119 * SYSFS fan layout: hwmon compatible (device)
3120 *
3121 * pwm*_enable:
3122 * 0: "disengaged" mode
3123 * 1: manual mode
3124 * 2: native EC "auto" mode (recommended, hardware default)
3125 *
3126 * pwm*: set speed in manual mode, ignored otherwise.
3127 * 0 is level 0; 255 is level 7. Intermediate points done with linear
3128 * interpolation.
3129 *
3130 * fan*_input: tachometer reading, RPM
3131 *
3132 *
3133 * SYSFS fan layout: extensions
3134 *
3135 * fan_watchdog (driver):
3136 * fan watchdog interval in seconds, 0 disables (default), max 120
3137 */
3138
3139/* sysfs fan pwm1_enable ----------------------------------------------- */
3140static ssize_t fan_pwm1_enable_show(struct device *dev,
3141 struct device_attribute *attr,
3142 char *buf)
3143{
3144 int res, mode;
3145 u8 status;
3146
3147 res = fan_get_status_safe(&status);
3148 if (res)
3149 return res;
3150
3151 if (unlikely(tp_features.fan_ctrl_status_undef)) {
3152 if (status != fan_control_initial_status) {
3153 tp_features.fan_ctrl_status_undef = 0;
3154 } else {
3155 /* Return most likely status. In fact, it
3156 * might be the only possible status */
3157 status = TP_EC_FAN_AUTO;
3158 }
3159 }
3160
3161 if (status & TP_EC_FAN_FULLSPEED) {
3162 mode = 0;
3163 } else if (status & TP_EC_FAN_AUTO) {
3164 mode = 2;
3165 } else
3166 mode = 1;
3167
3168 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
3169}
3170
3171static ssize_t fan_pwm1_enable_store(struct device *dev,
3172 struct device_attribute *attr,
3173 const char *buf, size_t count)
3174{
3175 unsigned long t;
3176 int res, level;
3177
3178 if (parse_strtoul(buf, 2, &t))
3179 return -EINVAL;
3180
3181 switch (t) {
3182 case 0:
3183 level = TP_EC_FAN_FULLSPEED;
3184 break;
3185 case 1:
3186 level = TPACPI_FAN_LAST_LEVEL;
3187 break;
3188 case 2:
3189 level = TP_EC_FAN_AUTO;
3190 break;
3191 case 3:
3192 /* reserved for software-controlled auto mode */
3193 return -ENOSYS;
3194 default:
3195 return -EINVAL;
3196 }
3197
3198 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03003199 if (res == -ENXIO)
3200 return -EINVAL;
3201 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003202 return res;
3203
3204 fan_watchdog_reset();
3205
3206 return count;
3207}
3208
3209static struct device_attribute dev_attr_fan_pwm1_enable =
3210 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
3211 fan_pwm1_enable_show, fan_pwm1_enable_store);
3212
3213/* sysfs fan pwm1 ------------------------------------------------------ */
3214static ssize_t fan_pwm1_show(struct device *dev,
3215 struct device_attribute *attr,
3216 char *buf)
3217{
3218 int res;
3219 u8 status;
3220
3221 res = fan_get_status_safe(&status);
3222 if (res)
3223 return res;
3224
3225 if (unlikely(tp_features.fan_ctrl_status_undef)) {
3226 if (status != fan_control_initial_status) {
3227 tp_features.fan_ctrl_status_undef = 0;
3228 } else {
3229 status = TP_EC_FAN_AUTO;
3230 }
3231 }
3232
3233 if ((status &
3234 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
3235 status = fan_control_desired_level;
3236
3237 if (status > 7)
3238 status = 7;
3239
3240 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
3241}
3242
3243static ssize_t fan_pwm1_store(struct device *dev,
3244 struct device_attribute *attr,
3245 const char *buf, size_t count)
3246{
3247 unsigned long s;
3248 int rc;
3249 u8 status, newlevel;
3250
3251 if (parse_strtoul(buf, 255, &s))
3252 return -EINVAL;
3253
3254 /* scale down from 0-255 to 0-7 */
3255 newlevel = (s >> 5) & 0x07;
3256
3257 rc = mutex_lock_interruptible(&fan_mutex);
3258 if (rc < 0)
3259 return rc;
3260
3261 rc = fan_get_status(&status);
3262 if (!rc && (status &
3263 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
3264 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03003265 if (rc == -ENXIO)
3266 rc = -EINVAL;
3267 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003268 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03003269 fan_watchdog_reset();
3270 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003271 }
3272
3273 mutex_unlock(&fan_mutex);
3274 return (rc)? rc : count;
3275}
3276
3277static struct device_attribute dev_attr_fan_pwm1 =
3278 __ATTR(pwm1, S_IWUSR | S_IRUGO,
3279 fan_pwm1_show, fan_pwm1_store);
3280
3281/* sysfs fan fan1_input ------------------------------------------------ */
3282static ssize_t fan_fan1_input_show(struct device *dev,
3283 struct device_attribute *attr,
3284 char *buf)
3285{
3286 int res;
3287 unsigned int speed;
3288
3289 res = fan_get_speed(&speed);
3290 if (res < 0)
3291 return res;
3292
3293 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
3294}
3295
3296static struct device_attribute dev_attr_fan_fan1_input =
3297 __ATTR(fan1_input, S_IRUGO,
3298 fan_fan1_input_show, NULL);
3299
3300/* sysfs fan fan_watchdog (driver) ------------------------------------- */
3301static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
3302 char *buf)
3303{
3304 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
3305}
3306
3307static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
3308 const char *buf, size_t count)
3309{
3310 unsigned long t;
3311
3312 if (parse_strtoul(buf, 120, &t))
3313 return -EINVAL;
3314
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03003315 if (!fan_control_allowed)
3316 return -EPERM;
3317
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003318 fan_watchdog_maxinterval = t;
3319 fan_watchdog_reset();
3320
3321 return count;
3322}
3323
3324static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
3325 fan_fan_watchdog_show, fan_fan_watchdog_store);
3326
3327/* --------------------------------------------------------------------- */
3328static struct attribute *fan_attributes[] = {
3329 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
3330 &dev_attr_fan_fan1_input.attr,
3331 NULL
3332};
3333
3334static const struct attribute_group fan_attr_group = {
3335 .attrs = fan_attributes,
3336};
3337
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003338static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003339{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003340 int rc;
3341
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003342 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
3343
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003344 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003345 fan_status_access_mode = TPACPI_FAN_NONE;
3346 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003347 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003348 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003349 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003350 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003351
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003352 IBM_ACPIHANDLE_INIT(fans);
3353 IBM_ACPIHANDLE_INIT(gfan);
3354 IBM_ACPIHANDLE_INIT(sfan);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003355
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003356 if (gfan_handle) {
3357 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003358 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003359 } else {
3360 /* all other ThinkPads: note that even old-style
3361 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02003362 if (likely(acpi_ec_read(fan_status_offset,
3363 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003364 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02003365
3366 /* In some ThinkPads, neither the EC nor the ACPI
3367 * DSDT initialize the fan status, and it ends up
3368 * being set to 0x07 when it *could* be either
3369 * 0x07 or 0x80.
3370 *
3371 * Enable for TP-1Y (T43), TP-78 (R51e),
3372 * TP-76 (R52), TP-70 (T43, R52), which are known
3373 * to be buggy. */
3374 if (fan_control_initial_status == 0x07 &&
3375 ibm_thinkpad_ec_found &&
3376 ((ibm_thinkpad_ec_found[0] == '1' &&
3377 ibm_thinkpad_ec_found[1] == 'Y') ||
3378 (ibm_thinkpad_ec_found[0] == '7' &&
3379 (ibm_thinkpad_ec_found[1] == '6' ||
3380 ibm_thinkpad_ec_found[1] == '8' ||
3381 ibm_thinkpad_ec_found[1] == '0'))
3382 )) {
3383 printk(IBM_NOTICE
3384 "fan_init: initial fan status is "
3385 "unknown, assuming it is in auto "
3386 "mode\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003387 tp_features.fan_ctrl_status_undef = 1;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02003388 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003389 } else {
3390 printk(IBM_ERR
3391 "ThinkPad ACPI EC access misbehaving, "
3392 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003393 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003394 }
3395 }
3396
3397 if (sfan_handle) {
3398 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003399 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02003400 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003401 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003402 } else {
3403 if (!gfan_handle) {
3404 /* gfan without sfan means no fan control */
3405 /* all other models implement TP EC 0x2f control */
3406
3407 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02003408 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003409 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003410 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003411 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003412 TPACPI_FAN_CMD_SPEED |
3413 TPACPI_FAN_CMD_LEVEL |
3414 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003415 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003416 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02003417 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003418 TPACPI_FAN_CMD_LEVEL |
3419 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003420 }
3421 }
3422 }
3423
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003424 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
3425 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
3426 fan_control_access_mode != TPACPI_FAN_WR_NONE),
3427 fan_status_access_mode, fan_control_access_mode);
3428
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03003429 /* fan control master switch */
3430 if (!fan_control_allowed) {
3431 fan_control_access_mode = TPACPI_FAN_WR_NONE;
3432 fan_control_commands = 0;
3433 dbg_printk(TPACPI_DBG_INIT,
3434 "fan control features disabled by parameter\n");
3435 }
3436
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003437 /* update fan_control_desired_level */
3438 if (fan_status_access_mode != TPACPI_FAN_NONE)
3439 fan_get_status_safe(NULL);
3440
3441 if (fan_status_access_mode != TPACPI_FAN_NONE ||
3442 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
3443 rc = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3444 &fan_attr_group);
3445 if (!(rc < 0))
3446 rc = driver_create_file(&tpacpi_pdriver.driver,
3447 &driver_attr_fan_watchdog);
3448 if (rc < 0)
3449 return rc;
3450 return 0;
3451 } else
3452 return 1;
3453}
3454
3455/*
3456 * Call with fan_mutex held
3457 */
3458static void fan_update_desired_level(u8 status)
3459{
3460 if ((status &
3461 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
3462 if (status > 7)
3463 fan_control_desired_level = 7;
3464 else
3465 fan_control_desired_level = status;
3466 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003467}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003468
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02003469static int fan_get_status(u8 *status)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003470{
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02003471 u8 s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003472
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02003473 /* TODO:
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003474 * Add TPACPI_FAN_RD_ACPI_FANS ? */
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02003475
Henrique de Moraes Holschuh3ef8a602006-11-24 11:47:10 -02003476 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003477 case TPACPI_FAN_RD_ACPI_GFAN:
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003478 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02003479
3480 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003481 return -EIO;
3482
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02003483 if (likely(status))
3484 *status = s & 0x07;
3485
3486 break;
3487
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003488 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02003489 /* all except 570, 600e/x, 770e, 770x */
3490 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
3491 return -EIO;
3492
3493 if (likely(status))
3494 *status = s;
3495
3496 break;
3497
3498 default:
3499 return -ENXIO;
3500 }
3501
3502 return 0;
3503}
3504
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003505static int fan_get_status_safe(u8 *status)
3506{
3507 int rc;
3508 u8 s;
3509
3510 rc = mutex_lock_interruptible(&fan_mutex);
3511 if (rc < 0)
3512 return rc;
3513 rc = fan_get_status(&s);
3514 if (!rc)
3515 fan_update_desired_level(s);
3516 mutex_unlock(&fan_mutex);
3517
3518 if (status)
3519 *status = s;
3520
3521 return rc;
3522}
3523
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003524static void fan_exit(void)
3525{
Henrique de Moraes Holschuh99fba3f2007-04-21 11:08:44 -03003526 vdbg_printk(TPACPI_DBG_EXIT, "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003527
3528 /* FIXME: can we really do this unconditionally? */
3529 sysfs_remove_group(&tpacpi_pdev->dev.kobj, &fan_attr_group);
3530 driver_remove_file(&tpacpi_pdriver.driver, &driver_attr_fan_watchdog);
3531
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003532 cancel_delayed_work(&fan_watchdog_task);
3533 flush_scheduled_work();
3534}
3535
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02003536static int fan_get_speed(unsigned int *speed)
3537{
3538 u8 hi, lo;
3539
3540 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003541 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02003542 /* all except 570, 600e/x, 770e, 770x */
3543 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
3544 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
3545 return -EIO;
3546
3547 if (likely(speed))
3548 *speed = (hi << 8) | lo;
3549
3550 break;
3551
3552 default:
3553 return -ENXIO;
3554 }
3555
3556 return 0;
3557}
3558
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003559static void fan_watchdog_fire(struct work_struct *ignored)
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003560{
Henrique de Moraes Holschuh99fba3f2007-04-21 11:08:44 -03003561 int rc;
3562
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003563 printk(IBM_NOTICE "fan watchdog: enabling fan\n");
Henrique de Moraes Holschuh99fba3f2007-04-21 11:08:44 -03003564 rc = fan_set_enable();
3565 if (rc < 0) {
3566 printk(IBM_ERR "fan watchdog: error %d while enabling fan, "
3567 "will try again later...\n", -rc);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003568 /* reschedule for later */
3569 fan_watchdog_reset();
3570 }
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003571}
3572
3573static void fan_watchdog_reset(void)
3574{
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03003575 static int fan_watchdog_active;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003576
Henrique de Moraes Holschuh4985cd02007-04-27 22:00:10 -03003577 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
3578 return;
3579
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003580 if (fan_watchdog_active)
3581 cancel_delayed_work(&fan_watchdog_task);
3582
3583 if (fan_watchdog_maxinterval > 0) {
3584 fan_watchdog_active = 1;
3585 if (!schedule_delayed_work(&fan_watchdog_task,
3586 msecs_to_jiffies(fan_watchdog_maxinterval
3587 * 1000))) {
3588 printk(IBM_ERR "failed to schedule the fan watchdog, "
3589 "watchdog will not trigger\n");
3590 }
3591 } else
3592 fan_watchdog_active = 0;
3593}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003594
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003595static int fan_set_level(int level)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003596{
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03003597 if (!fan_control_allowed)
3598 return -EPERM;
3599
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003600 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003601 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003602 if (level >= 0 && level <= 7) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003603 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003604 return -EIO;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003605 } else
3606 return -EINVAL;
3607 break;
3608
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003609 case TPACPI_FAN_WR_ACPI_FANS:
3610 case TPACPI_FAN_WR_TPEC:
3611 if ((level != TP_EC_FAN_AUTO) &&
3612 (level != TP_EC_FAN_FULLSPEED) &&
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02003613 ((level < 0) || (level > 7)))
3614 return -EINVAL;
3615
Henrique de Moraes Holschuheaa75712007-04-24 11:48:18 -03003616 /* safety net should the EC not support AUTO
3617 * or FULLSPEED mode bits and just ignore them */
3618 if (level & TP_EC_FAN_FULLSPEED)
3619 level |= 7; /* safety min speed 7 */
3620 else if (level & TP_EC_FAN_FULLSPEED)
3621 level |= 4; /* safety min speed 4 */
3622
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003623 if (!acpi_ec_write(fan_status_offset, level))
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02003624 return -EIO;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02003625 else
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003626 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02003627 break;
3628
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003629 default:
3630 return -ENXIO;
3631 }
3632 return 0;
3633}
3634
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003635static int fan_set_level_safe(int level)
3636{
3637 int rc;
3638
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03003639 if (!fan_control_allowed)
3640 return -EPERM;
3641
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003642 rc = mutex_lock_interruptible(&fan_mutex);
3643 if (rc < 0)
3644 return rc;
3645
3646 if (level == TPACPI_FAN_LAST_LEVEL)
3647 level = fan_control_desired_level;
3648
3649 rc = fan_set_level(level);
3650 if (!rc)
3651 fan_update_desired_level(level);
3652
3653 mutex_unlock(&fan_mutex);
3654 return rc;
3655}
3656
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003657static int fan_set_enable(void)
3658{
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02003659 u8 s;
3660 int rc;
3661
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03003662 if (!fan_control_allowed)
3663 return -EPERM;
3664
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003665 rc = mutex_lock_interruptible(&fan_mutex);
3666 if (rc < 0)
3667 return rc;
3668
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003669 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003670 case TPACPI_FAN_WR_ACPI_FANS:
3671 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003672 rc = fan_get_status(&s);
3673 if (rc < 0)
3674 break;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02003675
3676 /* Don't go out of emergency fan mode */
Henrique de Moraes Holschuheaa75712007-04-24 11:48:18 -03003677 if (s != 7) {
3678 s &= 0x07;
3679 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
3680 }
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02003681
3682 if (!acpi_ec_write(fan_status_offset, s))
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003683 rc = -EIO;
3684 else {
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003685 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003686 rc = 0;
3687 }
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02003688 break;
3689
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003690 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003691 rc = fan_get_status(&s);
3692 if (rc < 0)
3693 break;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02003694
3695 s &= 0x07;
3696
3697 /* Set fan to at least level 4 */
Henrique de Moraes Holschuheaa75712007-04-24 11:48:18 -03003698 s |= 4;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02003699
3700 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003701 rc= -EIO;
3702 else
3703 rc = 0;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003704 break;
3705
3706 default:
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003707 rc = -ENXIO;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003708 }
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003709
3710 mutex_unlock(&fan_mutex);
3711 return rc;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003712}
3713
3714static int fan_set_disable(void)
3715{
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003716 int rc;
3717
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03003718 if (!fan_control_allowed)
3719 return -EPERM;
3720
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003721 rc = mutex_lock_interruptible(&fan_mutex);
3722 if (rc < 0)
3723 return rc;
3724
3725 rc = 0;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003726 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003727 case TPACPI_FAN_WR_ACPI_FANS:
3728 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003729 if (!acpi_ec_write(fan_status_offset, 0x00))
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003730 rc = -EIO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003731 else {
3732 fan_control_desired_level = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003733 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003734 }
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003735 break;
3736
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003737 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02003738 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003739 rc = -EIO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003740 else
3741 fan_control_desired_level = 0;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02003742 break;
3743
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003744 default:
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003745 rc = -ENXIO;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003746 }
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003747
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003748
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003749 mutex_unlock(&fan_mutex);
3750 return rc;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003751}
3752
3753static int fan_set_speed(int speed)
3754{
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003755 int rc;
3756
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03003757 if (!fan_control_allowed)
3758 return -EPERM;
3759
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003760 rc = mutex_lock_interruptible(&fan_mutex);
3761 if (rc < 0)
3762 return rc;
3763
3764 rc = 0;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003765 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003766 case TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003767 if (speed >= 0 && speed <= 65535) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003768 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
3769 speed, speed, speed))
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003770 rc = -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003771 } else
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003772 rc = -EINVAL;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003773 break;
3774
3775 default:
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003776 rc = -ENXIO;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003777 }
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003778
3779 mutex_unlock(&fan_mutex);
3780 return rc;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003781}
3782
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003783static int fan_read(char *p)
3784{
3785 int len = 0;
3786 int rc;
3787 u8 status;
3788 unsigned int speed = 0;
3789
3790 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003791 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003792 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003793 if ((rc = fan_get_status_safe(&status)) < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003794 return rc;
3795
3796 len += sprintf(p + len, "status:\t\t%s\n"
3797 "level:\t\t%d\n",
3798 (status != 0) ? "enabled" : "disabled", status);
3799 break;
3800
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003801 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003802 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003803 if ((rc = fan_get_status_safe(&status)) < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003804 return rc;
3805
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003806 if (unlikely(tp_features.fan_ctrl_status_undef)) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003807 if (status != fan_control_initial_status)
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003808 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003809 else
3810 /* Return most likely status. In fact, it
3811 * might be the only possible status */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003812 status = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003813 }
3814
3815 len += sprintf(p + len, "status:\t\t%s\n",
3816 (status != 0) ? "enabled" : "disabled");
3817
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003818 if ((rc = fan_get_speed(&speed)) < 0)
3819 return rc;
3820
3821 len += sprintf(p + len, "speed:\t\t%d\n", speed);
3822
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003823 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003824 /* Disengaged mode takes precedence */
3825 len += sprintf(p + len, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003826 else if (status & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003827 len += sprintf(p + len, "level:\t\tauto\n");
3828 else
3829 len += sprintf(p + len, "level:\t\t%d\n", status);
3830 break;
3831
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003832 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003833 default:
3834 len += sprintf(p + len, "status:\t\tnot supported\n");
3835 }
3836
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003837 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003838 len += sprintf(p + len, "commands:\tlevel <level>");
3839
3840 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003841 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003842 len += sprintf(p + len, " (<level> is 0-7)\n");
3843 break;
3844
3845 default:
3846 len += sprintf(p + len, " (<level> is 0-7, "
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003847 "auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003848 break;
3849 }
3850 }
3851
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003852 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003853 len += sprintf(p + len, "commands:\tenable, disable\n"
3854 "commands:\twatchdog <timeout> (<timeout> is 0 (off), "
3855 "1-120 (seconds))\n");
3856
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003857 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003858 len += sprintf(p + len, "commands:\tspeed <speed>"
3859 " (<speed> is 0-65535)\n");
3860
3861 return len;
3862}
3863
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003864static int fan_write_cmd_level(const char *cmd, int *rc)
3865{
3866 int level;
3867
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02003868 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003869 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003870 else if ((strlencmp(cmd, "level disengaged") == 0) |
3871 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003872 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02003873 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003874 return 0;
3875
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003876 if ((*rc = fan_set_level_safe(level)) == -ENXIO)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003877 printk(IBM_ERR "level command accepted for unsupported "
3878 "access mode %d", fan_control_access_mode);
3879
3880 return 1;
3881}
3882
3883static int fan_write_cmd_enable(const char *cmd, int *rc)
3884{
3885 if (strlencmp(cmd, "enable") != 0)
3886 return 0;
3887
3888 if ((*rc = fan_set_enable()) == -ENXIO)
3889 printk(IBM_ERR "enable command accepted for unsupported "
3890 "access mode %d", fan_control_access_mode);
3891
3892 return 1;
3893}
3894
3895static int fan_write_cmd_disable(const char *cmd, int *rc)
3896{
3897 if (strlencmp(cmd, "disable") != 0)
3898 return 0;
3899
3900 if ((*rc = fan_set_disable()) == -ENXIO)
3901 printk(IBM_ERR "disable command accepted for unsupported "
3902 "access mode %d", fan_control_access_mode);
3903
3904 return 1;
3905}
3906
3907static int fan_write_cmd_speed(const char *cmd, int *rc)
3908{
3909 int speed;
3910
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02003911 /* TODO:
3912 * Support speed <low> <medium> <high> ? */
3913
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003914 if (sscanf(cmd, "speed %d", &speed) != 1)
3915 return 0;
3916
3917 if ((*rc = fan_set_speed(speed)) == -ENXIO)
3918 printk(IBM_ERR "speed command accepted for unsupported "
3919 "access mode %d", fan_control_access_mode);
3920
3921 return 1;
3922}
3923
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003924static int fan_write_cmd_watchdog(const char *cmd, int *rc)
3925{
3926 int interval;
3927
3928 if (sscanf(cmd, "watchdog %d", &interval) != 1)
3929 return 0;
3930
3931 if (interval < 0 || interval > 120)
3932 *rc = -EINVAL;
3933 else
3934 fan_watchdog_maxinterval = interval;
3935
3936 return 1;
3937}
3938
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003939static int fan_write(char *buf)
3940{
3941 char *cmd;
3942 int rc = 0;
3943
3944 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003945 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003946 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003947 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003948 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003949 fan_write_cmd_disable(cmd, &rc) ||
3950 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003951 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003952 fan_write_cmd_speed(cmd, &rc))
3953 )
3954 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003955 else if (!rc)
3956 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003957 }
3958
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003959 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003960}
3961
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003962static struct ibm_struct fan_driver_data = {
3963 .name = "fan",
3964 .read = fan_read,
3965 .write = fan_write,
3966 .exit = fan_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003967};
3968
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003969/****************************************************************************
3970 ****************************************************************************
3971 *
3972 * Infrastructure
3973 *
3974 ****************************************************************************
3975 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003977/* /proc support */
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03003978static struct proc_dir_entry *proc_dir;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003979
3980/* Subdriver registry */
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003981static LIST_HEAD(tpacpi_all_drivers);
3982
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003984/*
3985 * Module and infrastructure proble, init and exit handling
3986 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003988#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003989static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003990{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003991 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003992
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003993 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003994}
3995#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
3996
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003997static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998{
3999 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004000 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001 struct proc_dir_entry *entry;
4002
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004003 BUG_ON(ibm == NULL);
4004
4005 INIT_LIST_HEAD(&ibm->all_drivers);
4006
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004007 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008 return 0;
4009
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004010 dbg_printk(TPACPI_DBG_INIT,
4011 "probing for %s\n", ibm->name);
4012
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004013 if (iibm->init) {
4014 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004015 if (ret > 0)
4016 return 0; /* probe failed */
4017 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004019
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004020 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021 }
4022
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004023 if (ibm->acpi) {
4024 if (ibm->acpi->hid) {
4025 ret = register_tpacpi_subdriver(ibm);
4026 if (ret)
4027 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004028 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004029
4030 if (ibm->acpi->notify) {
4031 ret = setup_acpi_notify(ibm);
4032 if (ret == -ENODEV) {
4033 printk(IBM_NOTICE "disabling subdriver %s\n",
4034 ibm->name);
4035 ret = 0;
4036 goto err_out;
4037 }
4038 if (ret < 0)
4039 goto err_out;
4040 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004041 }
4042
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004043 dbg_printk(TPACPI_DBG_INIT,
4044 "%s installed\n", ibm->name);
4045
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004046 if (ibm->read) {
4047 entry = create_proc_entry(ibm->name,
4048 S_IFREG | S_IRUGO | S_IWUSR,
4049 proc_dir);
4050 if (!entry) {
4051 printk(IBM_ERR "unable to create proc entry %s\n",
4052 ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004053 ret = -ENODEV;
4054 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004055 }
4056 entry->owner = THIS_MODULE;
4057 entry->data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004058 entry->read_proc = &dispatch_procfs_read;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004059 if (ibm->write)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004060 entry->write_proc = &dispatch_procfs_write;
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004061 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004062 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004064 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
4065
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004067
4068err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004069 dbg_printk(TPACPI_DBG_INIT,
4070 "%s: at error exit path with result %d\n",
4071 ibm->name, ret);
4072
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004073 ibm_exit(ibm);
4074 return (ret < 0)? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075}
4076
4077static void ibm_exit(struct ibm_struct *ibm)
4078{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004079 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004080
4081 list_del_init(&ibm->all_drivers);
4082
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004083 if (ibm->flags.acpi_notify_installed) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004084 dbg_printk(TPACPI_DBG_EXIT,
4085 "%s: acpi_remove_notify_handler\n", ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004086 BUG_ON(!ibm->acpi);
4087 acpi_remove_notify_handler(*ibm->acpi->handle,
4088 ibm->acpi->type,
4089 dispatch_acpi_notify);
4090 ibm->flags.acpi_notify_installed = 0;
4091 ibm->flags.acpi_notify_installed = 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004092 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004094 if (ibm->flags.proc_created) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004095 dbg_printk(TPACPI_DBG_EXIT,
4096 "%s: remove_proc_entry\n", ibm->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097 remove_proc_entry(ibm->name, proc_dir);
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004098 ibm->flags.proc_created = 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004099 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004101 if (ibm->flags.acpi_driver_registered) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004102 dbg_printk(TPACPI_DBG_EXIT,
4103 "%s: acpi_bus_unregister_driver\n", ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004104 BUG_ON(!ibm->acpi);
4105 acpi_bus_unregister_driver(ibm->acpi->driver);
4106 kfree(ibm->acpi->driver);
4107 ibm->acpi->driver = NULL;
4108 ibm->flags.acpi_driver_registered = 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004109 }
4110
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004111 if (ibm->flags.init_called && ibm->exit) {
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004112 ibm->exit();
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004113 ibm->flags.init_called = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004115
4116 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117}
4118
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004119/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03004121static char *ibm_thinkpad_ec_found;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02004123static char* __init check_dmi_for_ec(void)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004124{
4125 struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02004126 char ec_fw_string[18];
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004127
4128 /*
4129 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
4130 * X32 or newer, all Z series; Some models must have an
4131 * up-to-date BIOS or they will not be detected.
4132 *
4133 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
4134 */
4135 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02004136 if (sscanf(dev->name,
4137 "IBM ThinkPad Embedded Controller -[%17c",
4138 ec_fw_string) == 1) {
4139 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
4140 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
4141 return kstrdup(ec_fw_string, GFP_KERNEL);
4142 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004143 }
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02004144 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145}
4146
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004147static int __init probe_for_thinkpad(void)
4148{
4149 int is_thinkpad;
4150
4151 if (acpi_disabled)
4152 return -ENODEV;
4153
4154 /*
4155 * Non-ancient models have better DMI tagging, but very old models
4156 * don't.
4157 */
4158 is_thinkpad = dmi_name_in_vendors("ThinkPad");
4159
4160 /* ec is required because many other handles are relative to it */
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004161 IBM_ACPIHANDLE_INIT(ec);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004162 if (!ec_handle) {
4163 if (is_thinkpad)
4164 printk(IBM_ERR
4165 "Not yet supported ThinkPad detected!\n");
4166 return -ENODEV;
4167 }
4168
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03004169 /*
4170 * Risks a regression on very old machines, but reduces potential
4171 * false positives a damn great deal
4172 */
4173 if (!is_thinkpad)
4174 is_thinkpad = dmi_name_in_vendors("IBM");
4175
4176 if (!is_thinkpad && !force_load)
4177 return -ENODEV;
4178
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004179 return 0;
4180}
4181
4182
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004183/* Module init, exit, parameters */
4184
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004185static struct ibm_init_struct ibms_init[] __initdata = {
4186 {
4187 .init = thinkpad_acpi_driver_init,
4188 .data = &thinkpad_acpi_driver_data,
4189 },
4190 {
4191 .init = hotkey_init,
4192 .data = &hotkey_driver_data,
4193 },
4194 {
4195 .init = bluetooth_init,
4196 .data = &bluetooth_driver_data,
4197 },
4198 {
4199 .init = wan_init,
4200 .data = &wan_driver_data,
4201 },
4202 {
4203 .init = video_init,
4204 .data = &video_driver_data,
4205 },
4206 {
4207 .init = light_init,
4208 .data = &light_driver_data,
4209 },
4210#ifdef CONFIG_THINKPAD_ACPI_DOCK
4211 {
4212 .init = dock_init,
4213 .data = &dock_driver_data[0],
4214 },
4215 {
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03004216 .init = dock_init2,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004217 .data = &dock_driver_data[1],
4218 },
4219#endif
4220#ifdef CONFIG_THINKPAD_ACPI_BAY
4221 {
4222 .init = bay_init,
4223 .data = &bay_driver_data,
4224 },
4225#endif
4226 {
4227 .init = cmos_init,
4228 .data = &cmos_driver_data,
4229 },
4230 {
4231 .init = led_init,
4232 .data = &led_driver_data,
4233 },
4234 {
4235 .init = beep_init,
4236 .data = &beep_driver_data,
4237 },
4238 {
4239 .init = thermal_init,
4240 .data = &thermal_driver_data,
4241 },
4242 {
4243 .data = &ecdump_driver_data,
4244 },
4245 {
4246 .init = brightness_init,
4247 .data = &brightness_driver_data,
4248 },
4249 {
4250 .data = &volume_driver_data,
4251 },
4252 {
4253 .init = fan_init,
4254 .data = &fan_driver_data,
4255 },
4256};
4257
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004258static int __init set_ibm_param(const char *val, struct kernel_param *kp)
4259{
4260 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004261 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004262
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004263 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
4264 ibm = ibms_init[i].data;
4265 BUG_ON(ibm == NULL);
4266
4267 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
4268 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004269 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004270 strcpy(ibms_init[i].param, val);
4271 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004272 return 0;
4273 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004274 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004275
4276 return -EINVAL;
4277}
4278
4279static int experimental;
4280module_param(experimental, int, 0);
4281
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03004282static u32 dbg_level;
4283module_param_named(debug, dbg_level, uint, 0);
4284
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03004285static int force_load;
4286module_param(force_load, int, 0);
4287
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03004288static int fan_control_allowed;
4289module_param_named(fan_control, fan_control_allowed, int, 0);
4290
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004291#define IBM_PARAM(feature) \
4292 module_param_call(feature, set_ibm_param, NULL, NULL, 0)
4293
4294IBM_PARAM(hotkey);
4295IBM_PARAM(bluetooth);
4296IBM_PARAM(video);
4297IBM_PARAM(light);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03004298#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004299IBM_PARAM(dock);
4300#endif
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03004301#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004302IBM_PARAM(bay);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03004303#endif /* CONFIG_THINKPAD_ACPI_BAY */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004304IBM_PARAM(cmos);
4305IBM_PARAM(led);
4306IBM_PARAM(beep);
4307IBM_PARAM(ecdump);
4308IBM_PARAM(brightness);
4309IBM_PARAM(volume);
4310IBM_PARAM(fan);
4311
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03004312static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313{
4314 int ret, i;
4315
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03004316 /* Driver-level probe */
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004317 ret = probe_for_thinkpad();
4318 if (ret)
4319 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03004321 /* Driver initialization */
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004322 ibm_thinkpad_ec_found = check_dmi_for_ec();
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004323 IBM_ACPIHANDLE_INIT(ecrd);
4324 IBM_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004326 proc_dir = proc_mkdir(IBM_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 if (!proc_dir) {
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004328 printk(IBM_ERR "unable to create proc dir " IBM_PROC_DIR);
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03004329 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330 return -ENODEV;
4331 }
4332 proc_dir->owner = THIS_MODULE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004333
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03004334 ret = platform_driver_register(&tpacpi_pdriver);
4335 if (ret) {
4336 printk(IBM_ERR "unable to register platform driver\n");
4337 thinkpad_acpi_module_exit();
4338 return ret;
4339 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03004340 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
4341 if (ret) {
4342 printk(IBM_ERR "unable to create sysfs driver attributes\n");
4343 thinkpad_acpi_module_exit();
4344 return ret;
4345 }
4346
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03004347
4348 /* Device initialization */
4349 tpacpi_pdev = platform_device_register_simple(IBM_DRVR_NAME, -1,
4350 NULL, 0);
4351 if (IS_ERR(tpacpi_pdev)) {
4352 ret = PTR_ERR(tpacpi_pdev);
4353 tpacpi_pdev = NULL;
4354 printk(IBM_ERR "unable to register platform device\n");
4355 thinkpad_acpi_module_exit();
4356 return ret;
4357 }
4358 tpacpi_hwmon = hwmon_device_register(&tpacpi_pdev->dev);
4359 if (IS_ERR(tpacpi_hwmon)) {
4360 ret = PTR_ERR(tpacpi_hwmon);
4361 tpacpi_hwmon = NULL;
4362 printk(IBM_ERR "unable to register hwmon device\n");
4363 thinkpad_acpi_module_exit();
4364 return ret;
4365 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004366 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
4367 ret = ibm_init(&ibms_init[i]);
4368 if (ret >= 0 && *ibms_init[i].param)
4369 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03004371 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372 return ret;
4373 }
4374 }
4375
4376 return 0;
4377}
4378
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03004379static void thinkpad_acpi_module_exit(void)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004380{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004381 struct ibm_struct *ibm, *itmp;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004382
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004383 list_for_each_entry_safe_reverse(ibm, itmp,
4384 &tpacpi_all_drivers,
4385 all_drivers) {
4386 ibm_exit(ibm);
4387 }
4388
4389 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004390
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03004391 if (tpacpi_hwmon)
4392 hwmon_device_unregister(tpacpi_hwmon);
4393
4394 if (tpacpi_pdev)
4395 platform_device_unregister(tpacpi_pdev);
4396
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03004397 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03004398 platform_driver_unregister(&tpacpi_pdriver);
4399
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004400 if (proc_dir)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004401 remove_proc_entry(IBM_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004402
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004403 kfree(ibm_thinkpad_ec_found);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004404}
4405
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03004406module_init(thinkpad_acpi_module_init);
4407module_exit(thinkpad_acpi_module_exit);