blob: f15a58f7403fd4b231ddd267ebb02f098d22957b [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 Holschuhc78d5c92007-07-18 23:45:47 -030024#define IBM_VERSION "0.15"
Henrique de Moraes Holschuh741553c2007-07-18 23:45:39 -030025#define TPACPI_SYSFS_VERSION 0x010000
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 Holschuh7f5d1cd2007-07-18 23:45:34 -0300513 * Device model: input, hwmon and platform
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300514 *
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 Holschuh7f5d1cd2007-07-18 23:45:34 -0300520static struct input_dev *tpacpi_inputdev;
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300521
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300522
523static int tpacpi_resume_handler(struct platform_device *pdev)
524{
525 struct ibm_struct *ibm, *itmp;
526
527 list_for_each_entry_safe(ibm, itmp,
528 &tpacpi_all_drivers,
529 all_drivers) {
530 if (ibm->resume)
531 (ibm->resume)();
532 }
533
534 return 0;
535}
536
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300537static struct platform_driver tpacpi_pdriver = {
538 .driver = {
539 .name = IBM_DRVR_NAME,
540 .owner = THIS_MODULE,
541 },
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300542 .resume = tpacpi_resume_handler,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300543};
544
545
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -0300546/*************************************************************************
547 * thinkpad-acpi driver attributes
548 */
549
550/* interface_version --------------------------------------------------- */
551static ssize_t tpacpi_driver_interface_version_show(
552 struct device_driver *drv,
553 char *buf)
554{
555 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
556}
557
558static DRIVER_ATTR(interface_version, S_IRUGO,
559 tpacpi_driver_interface_version_show, NULL);
560
561/* debug_level --------------------------------------------------------- */
562static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
563 char *buf)
564{
565 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
566}
567
568static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
569 const char *buf, size_t count)
570{
571 unsigned long t;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -0300572
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300573 if (parse_strtoul(buf, 0xffff, &t))
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -0300574 return -EINVAL;
575
576 dbg_level = t;
577
578 return count;
579}
580
581static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
582 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
583
584/* version ------------------------------------------------------------- */
585static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
586 char *buf)
587{
588 return snprintf(buf, PAGE_SIZE, "%s v%s\n", IBM_DESC, IBM_VERSION);
589}
590
591static DRIVER_ATTR(version, S_IRUGO,
592 tpacpi_driver_version_show, NULL);
593
594/* --------------------------------------------------------------------- */
595
596static struct driver_attribute* tpacpi_driver_attributes[] = {
597 &driver_attr_debug_level, &driver_attr_version,
598 &driver_attr_interface_version,
599};
600
601static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
602{
603 int i, res;
604
605 i = 0;
606 res = 0;
607 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
608 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
609 i++;
610 }
611
612 return res;
613}
614
615static void tpacpi_remove_driver_attributes(struct device_driver *drv)
616{
617 int i;
618
619 for(i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
620 driver_remove_file(drv, tpacpi_driver_attributes[i]);
621}
622
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300623/*************************************************************************
624 * sysfs support helpers
625 */
626
627struct attribute_set_obj {
628 struct attribute_set s;
629 struct attribute *a;
630} __attribute__((packed));
631
632static struct attribute_set *create_attr_set(unsigned int max_members,
633 const char* name)
634{
635 struct attribute_set_obj *sobj;
636
637 if (max_members == 0)
638 return NULL;
639
640 /* Allocates space for implicit NULL at the end too */
641 sobj = kzalloc(sizeof(struct attribute_set_obj) +
642 max_members * sizeof(struct attribute *),
643 GFP_KERNEL);
644 if (!sobj)
645 return NULL;
646 sobj->s.max_members = max_members;
647 sobj->s.group.attrs = &sobj->a;
648 sobj->s.group.name = name;
649
650 return &sobj->s;
651}
652
653/* not multi-threaded safe, use it in a single thread per set */
654static int add_to_attr_set(struct attribute_set* s, struct attribute *attr)
655{
656 if (!s || !attr)
657 return -EINVAL;
658
659 if (s->members >= s->max_members)
660 return -ENOMEM;
661
662 s->group.attrs[s->members] = attr;
663 s->members++;
664
665 return 0;
666}
667
668static int add_many_to_attr_set(struct attribute_set* s,
669 struct attribute **attr,
670 unsigned int count)
671{
672 int i, res;
673
674 for (i = 0; i < count; i++) {
675 res = add_to_attr_set(s, attr[i]);
676 if (res)
677 return res;
678 }
679
680 return 0;
681}
682
683static void delete_attr_set(struct attribute_set* s, struct kobject *kobj)
684{
685 sysfs_remove_group(kobj, &s->group);
686 destroy_attr_set(s);
687}
688
689static int parse_strtoul(const char *buf,
690 unsigned long max, unsigned long *value)
691{
692 char *endp;
693
694 *value = simple_strtoul(buf, &endp, 0);
695 while (*endp && isspace(*endp))
696 endp++;
697 if (*endp || *value > max)
698 return -EINVAL;
699
700 return 0;
701}
702
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300703/****************************************************************************
704 ****************************************************************************
705 *
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300706 * Subdrivers
707 *
708 ****************************************************************************
709 ****************************************************************************/
710
711/*************************************************************************
Henrique de Moraes Holschuh142cfc92007-04-21 11:08:26 -0300712 * thinkpad-acpi init subdriver
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300713 */
714
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300715static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716{
717 printk(IBM_INFO "%s v%s\n", IBM_DESC, IBM_VERSION);
718 printk(IBM_INFO "%s\n", IBM_URL);
719
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -0300720 printk(IBM_INFO "ThinkPad BIOS %s, EC %s\n",
721 (thinkpad_id.bios_version_str) ?
722 thinkpad_id.bios_version_str : "unknown",
723 (thinkpad_id.ec_version_str) ?
724 thinkpad_id.ec_version_str : "unknown");
725
726 if (thinkpad_id.vendor && thinkpad_id.model_str)
727 printk(IBM_INFO "%s %s\n",
728 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
729 "IBM" : ((thinkpad_id.vendor ==
730 PCI_VENDOR_ID_LENOVO) ?
731 "Lenovo" : "Unknown vendor"),
732 thinkpad_id.model_str);
Henrique de Moraes Holschuh3945ac32007-02-06 19:13:44 -0200733
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 return 0;
735}
736
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300737static int thinkpad_acpi_driver_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738{
739 int len = 0;
740
741 len += sprintf(p + len, "driver:\t\t%s\n", IBM_DESC);
742 len += sprintf(p + len, "version:\t%s\n", IBM_VERSION);
743
744 return len;
745}
746
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300747static struct ibm_struct thinkpad_acpi_driver_data = {
748 .name = "driver",
749 .read = thinkpad_acpi_driver_read,
750};
751
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300752/*************************************************************************
753 * Hotkey subdriver
754 */
755
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400756static int hotkey_orig_status;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -0300757static u32 hotkey_orig_mask;
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -0300758static u32 hotkey_all_mask;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -0300759static u32 hotkey_reserved_mask;
760
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -0300761static u16 *hotkey_keycode_map;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400762
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -0300763static struct attribute_set *hotkey_dev_attributes;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300764
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -0300765static int hotkey_get_wlsw(int *status)
766{
767 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
768 return -EIO;
769 return 0;
770}
771
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300772/* sysfs hotkey enable ------------------------------------------------- */
773static ssize_t hotkey_enable_show(struct device *dev,
774 struct device_attribute *attr,
775 char *buf)
776{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -0300777 int res, status;
778 u32 mask;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300779
780 res = hotkey_get(&status, &mask);
781 if (res)
782 return res;
783
784 return snprintf(buf, PAGE_SIZE, "%d\n", status);
785}
786
787static ssize_t hotkey_enable_store(struct device *dev,
788 struct device_attribute *attr,
789 const char *buf, size_t count)
790{
791 unsigned long t;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -0300792 int res, status;
793 u32 mask;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300794
795 if (parse_strtoul(buf, 1, &t))
796 return -EINVAL;
797
798 res = hotkey_get(&status, &mask);
799 if (!res)
800 res = hotkey_set(t, mask);
801
802 return (res) ? res : count;
803}
804
805static struct device_attribute dev_attr_hotkey_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -0300806 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300807 hotkey_enable_show, hotkey_enable_store);
808
809/* sysfs hotkey mask --------------------------------------------------- */
810static ssize_t hotkey_mask_show(struct device *dev,
811 struct device_attribute *attr,
812 char *buf)
813{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -0300814 int res, status;
815 u32 mask;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300816
817 res = hotkey_get(&status, &mask);
818 if (res)
819 return res;
820
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -0300821 return snprintf(buf, PAGE_SIZE, "0x%08x\n", mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300822}
823
824static ssize_t hotkey_mask_store(struct device *dev,
825 struct device_attribute *attr,
826 const char *buf, size_t count)
827{
828 unsigned long t;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -0300829 int res, status;
830 u32 mask;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300831
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -0300832 if (parse_strtoul(buf, 0xffffffffUL, &t))
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300833 return -EINVAL;
834
835 res = hotkey_get(&status, &mask);
836 if (!res)
837 hotkey_set(status, t);
838
839 return (res) ? res : count;
840}
841
842static struct device_attribute dev_attr_hotkey_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -0300843 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300844 hotkey_mask_show, hotkey_mask_store);
845
846/* sysfs hotkey bios_enabled ------------------------------------------- */
847static ssize_t hotkey_bios_enabled_show(struct device *dev,
848 struct device_attribute *attr,
849 char *buf)
850{
851 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
852}
853
854static struct device_attribute dev_attr_hotkey_bios_enabled =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -0300855 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300856
857/* sysfs hotkey bios_mask ---------------------------------------------- */
858static ssize_t hotkey_bios_mask_show(struct device *dev,
859 struct device_attribute *attr,
860 char *buf)
861{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -0300862 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300863}
864
865static struct device_attribute dev_attr_hotkey_bios_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -0300866 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300867
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -0300868/* sysfs hotkey all_mask ----------------------------------------------- */
869static ssize_t hotkey_all_mask_show(struct device *dev,
870 struct device_attribute *attr,
871 char *buf)
872{
873 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_all_mask);
874}
875
876static struct device_attribute dev_attr_hotkey_all_mask =
877 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
878
879/* sysfs hotkey recommended_mask --------------------------------------- */
880static ssize_t hotkey_recommended_mask_show(struct device *dev,
881 struct device_attribute *attr,
882 char *buf)
883{
884 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
885 hotkey_all_mask & ~hotkey_reserved_mask);
886}
887
888static struct device_attribute dev_attr_hotkey_recommended_mask =
889 __ATTR(hotkey_recommended_mask, S_IRUGO,
890 hotkey_recommended_mask_show, NULL);
891
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -0300892/* sysfs hotkey radio_sw ----------------------------------------------- */
893static ssize_t hotkey_radio_sw_show(struct device *dev,
894 struct device_attribute *attr,
895 char *buf)
896{
897 int res, s;
898 res = hotkey_get_wlsw(&s);
899 if (res < 0)
900 return res;
901
902 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
903}
904
905static struct device_attribute dev_attr_hotkey_radio_sw =
906 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
907
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300908/* --------------------------------------------------------------------- */
909
910static struct attribute *hotkey_mask_attributes[] = {
911 &dev_attr_hotkey_mask.attr,
912 &dev_attr_hotkey_bios_enabled.attr,
913 &dev_attr_hotkey_bios_mask.attr,
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -0300914 &dev_attr_hotkey_all_mask.attr,
915 &dev_attr_hotkey_recommended_mask.attr,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300916};
917
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300918static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300919{
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -0300920
921 static u16 ibm_keycode_map[] __initdata = {
922 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
923 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
924 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
925 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
926 /* Scan codes 0x0C to 0x0F: Other ACPI HKEY hot keys */
927 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
928 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
929 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
930 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
931 /* Scan codes 0x10 to 0x1F: Extended ACPI HKEY hot keys */
932 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
933 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
934 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
935 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
936 KEY_RESERVED, /* 0x14: VOLUME UP */
937 KEY_RESERVED, /* 0x15: VOLUME DOWN */
938 KEY_RESERVED, /* 0x16: MUTE */
939 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
940 /* (assignments unknown, please report if found) */
941 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
942 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
943 };
944 static u16 lenovo_keycode_map[] __initdata = {
945 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
946 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
947 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
948 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
949 /* Scan codes 0x0C to 0x0F: Other ACPI HKEY hot keys */
950 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
951 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
952 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
953 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
954 /* Scan codes 0x10 to 0x1F: Extended ACPI HKEY hot keys */
955 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
956 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
957 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
958 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
959 KEY_RESERVED, /* 0x14: VOLUME UP */
960 KEY_RESERVED, /* 0x15: VOLUME DOWN */
961 KEY_RESERVED, /* 0x16: MUTE */
962 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
963 /* (assignments unknown, please report if found) */
964 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
965 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
966 };
967
968#define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
969#define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
970#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
971
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -0300972 int res, i;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -0300973 int status;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300974
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300975 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
976
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -0300977 BUG_ON(!tpacpi_inputdev);
978
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300979 IBM_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -0300980 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300981
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300982 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300983 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300984
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300985 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300986 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300987
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300988 if (tp_features.hotkey) {
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -0300989 hotkey_dev_attributes = create_attr_set(7, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300990 if (!hotkey_dev_attributes)
991 return -ENOMEM;
992 res = add_to_attr_set(hotkey_dev_attributes,
993 &dev_attr_hotkey_enable.attr);
994 if (res)
995 return res;
996
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300997 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
998 A30, R30, R31, T20-22, X20-21, X22-24 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300999 tp_features.hotkey_mask =
1000 acpi_evalf(hkey_handle, NULL, "DHKN", "qv");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001001
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001002 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001003 str_supported(tp_features.hotkey_mask));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001004
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001005 if (tp_features.hotkey_mask) {
1006 /* MHKA available in A31, R40, R40e, T4x, X31, and later */
1007 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
1008 "MHKA", "qd"))
1009 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
1010 }
1011
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001012 res = hotkey_get(&hotkey_orig_status, &hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001013 if (!res && tp_features.hotkey_mask) {
1014 res = add_many_to_attr_set(hotkey_dev_attributes,
1015 hotkey_mask_attributes,
1016 ARRAY_SIZE(hotkey_mask_attributes));
1017 }
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001018
1019 /* Not all thinkpads have a hardware radio switch */
1020 if (!res && acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
1021 tp_features.hotkey_wlsw = 1;
1022 printk(IBM_INFO
1023 "radio switch found; radios are %s\n",
1024 enabled(status, 0));
1025 res = add_to_attr_set(hotkey_dev_attributes,
1026 &dev_attr_hotkey_radio_sw.attr);
1027 }
1028
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001029 if (!res)
1030 res = register_attr_set_with_sysfs(
1031 hotkey_dev_attributes,
1032 &tpacpi_pdev->dev.kobj);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001033 if (res)
1034 return res;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001035
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001036 /* Set up key map */
1037
1038 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
1039 GFP_KERNEL);
1040 if (!hotkey_keycode_map) {
1041 printk(IBM_ERR "failed to allocate memory for key map\n");
1042 return -ENOMEM;
1043 }
1044
1045 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
1046 dbg_printk(TPACPI_DBG_INIT,
1047 "using Lenovo default hot key map\n");
1048 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
1049 TPACPI_HOTKEY_MAP_SIZE);
1050 } else {
1051 dbg_printk(TPACPI_DBG_INIT,
1052 "using IBM default hot key map\n");
1053 memcpy(hotkey_keycode_map, &ibm_keycode_map,
1054 TPACPI_HOTKEY_MAP_SIZE);
1055 }
1056
Henrique de Moraes Holschuh1a343762007-07-18 23:45:36 -03001057#ifndef CONFIG_THINKPAD_ACPI_INPUT_ENABLED
1058 for (i = 0; i < 12; i++)
1059 hotkey_keycode_map[i] = KEY_UNKNOWN;
1060#endif /* ! CONFIG_THINKPAD_ACPI_INPUT_ENABLED */
1061
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001062 set_bit(EV_KEY, tpacpi_inputdev->evbit);
1063 set_bit(EV_MSC, tpacpi_inputdev->evbit);
1064 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001065 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
1066 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
1067 tpacpi_inputdev->keycode = hotkey_keycode_map;
1068 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001069 if (hotkey_keycode_map[i] != KEY_RESERVED) {
1070 set_bit(hotkey_keycode_map[i],
1071 tpacpi_inputdev->keybit);
1072 } else {
1073 if (i < sizeof(hotkey_reserved_mask)*8)
1074 hotkey_reserved_mask |= 1 << i;
1075 }
1076 }
1077
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03001078 if (tp_features.hotkey_wlsw) {
1079 set_bit(EV_SW, tpacpi_inputdev->evbit);
1080 set_bit(SW_RADIO, tpacpi_inputdev->swbit);
1081 }
1082
Henrique de Moraes Holschuh1a343762007-07-18 23:45:36 -03001083#ifdef CONFIG_THINKPAD_ACPI_INPUT_ENABLED
1084 dbg_printk(TPACPI_DBG_INIT,
1085 "enabling hot key handling\n");
1086 res = hotkey_set(1, (hotkey_all_mask & ~hotkey_reserved_mask)
1087 | hotkey_orig_mask);
1088 if (res)
1089 return res;
1090#endif /* CONFIG_THINKPAD_ACPI_INPUT_ENABLED */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001091 }
1092
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001093 return (tp_features.hotkey)? 0 : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001094}
1095
1096static void hotkey_exit(void)
1097{
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001098 int res;
1099
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001100 if (tp_features.hotkey) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001101 dbg_printk(TPACPI_DBG_EXIT, "restoring original hotkey mask\n");
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001102 res = hotkey_set(hotkey_orig_status, hotkey_orig_mask);
1103 if (res)
1104 printk(IBM_ERR "failed to restore hotkey to BIOS defaults\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001105 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001106
1107 if (hotkey_dev_attributes) {
1108 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
1109 hotkey_dev_attributes = NULL;
1110 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001111}
1112
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001113static void tpacpi_input_send_key(unsigned int scancode,
1114 unsigned int keycode)
1115{
1116 if (keycode != KEY_RESERVED) {
1117 input_report_key(tpacpi_inputdev, keycode, 1);
1118 if (keycode == KEY_UNKNOWN)
1119 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1120 scancode);
1121 input_sync(tpacpi_inputdev);
1122
1123 input_report_key(tpacpi_inputdev, keycode, 0);
1124 if (keycode == KEY_UNKNOWN)
1125 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1126 scancode);
1127 input_sync(tpacpi_inputdev);
1128 }
1129}
1130
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03001131static void tpacpi_input_send_radiosw(void)
1132{
1133 int wlsw;
1134
1135 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw))
1136 input_report_switch(tpacpi_inputdev,
1137 SW_RADIO, !!wlsw);
1138}
1139
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001140static void hotkey_notify(struct ibm_struct *ibm, u32 event)
1141{
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001142 u32 hkey;
1143 unsigned int keycode, scancode;
1144 int sendacpi = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001145
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001146 if (event == 0x80 && acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001147 if (tpacpi_inputdev->users > 0) {
1148 switch (hkey >> 12) {
1149 case 1:
1150 /* 0x1000-0x1FFF: key presses */
1151 scancode = hkey & 0xfff;
1152 if (scancode > 0 && scancode < 0x21) {
1153 scancode--;
1154 keycode = hotkey_keycode_map[scancode];
1155 tpacpi_input_send_key(scancode, keycode);
1156 sendacpi = (keycode == KEY_RESERVED
1157 || keycode == KEY_UNKNOWN);
1158 } else {
1159 printk(IBM_ERR
1160 "hotkey 0x%04x out of range for keyboard map\n",
1161 hkey);
1162 }
1163 break;
1164 case 5:
1165 /* 0x5000-0x5FFF: LID */
1166 /* we don't handle it through this path, just
1167 * eat up known LID events */
1168 if (hkey != 0x5001 && hkey != 0x5002) {
1169 printk(IBM_ERR
1170 "unknown LID-related hotkey event: 0x%04x\n",
1171 hkey);
1172 }
1173 break;
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03001174 case 7:
1175 /* 0x7000-0x7FFF: misc */
1176 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
1177 tpacpi_input_send_radiosw();
1178 sendacpi = 0;
1179 break;
1180 }
1181 /* fallthrough to default */
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001182 default:
1183 /* case 2: dock-related */
1184 /* 0x2305 - T43 waking up due to bay lever eject while aslept */
1185 /* case 3: ultra-bay related. maybe bay in dock? */
1186 /* 0x3003 - T43 after wake up by bay lever eject (0x2305) */
1187 printk(IBM_NOTICE "unhandled hotkey event 0x%04x\n", hkey);
1188 }
1189 }
1190
1191 if (sendacpi)
1192 acpi_bus_generate_event(ibm->acpi->device, event, hkey);
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001193 } else {
1194 printk(IBM_ERR "unknown hotkey notification event %d\n", event);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001195 acpi_bus_generate_event(ibm->acpi->device, event, 0);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001196 }
1197}
1198
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03001199static void hotkey_resume(void)
1200{
1201 tpacpi_input_send_radiosw();
1202}
1203
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001204/*
1205 * Call with hotkey_mutex held
1206 */
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001207static int hotkey_get(int *status, u32 *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208{
1209 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001210 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001211
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001212 if (tp_features.hotkey_mask)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001213 if (!acpi_evalf(hkey_handle, mask, "DHKN", "d"))
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001214 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001215
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001216 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217}
1218
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001219/*
1220 * Call with hotkey_mutex held
1221 */
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001222static int hotkey_set(int status, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223{
1224 int i;
1225
1226 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001227 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001229 if (tp_features.hotkey_mask)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001230 for (i = 0; i < 32; i++) {
1231 int bit = ((1 << i) & mask) != 0;
1232 if (!acpi_evalf(hkey_handle,
1233 NULL, "MHKM", "vdd", i + 1, bit))
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001234 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001235 }
1236
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001237 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001238}
1239
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001240/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001241static int hotkey_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001243 int res, status;
1244 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 int len = 0;
1246
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001247 if (!tp_features.hotkey) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001248 len += sprintf(p + len, "status:\t\tnot supported\n");
1249 return len;
1250 }
1251
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001252 res = mutex_lock_interruptible(&hotkey_mutex);
1253 if (res < 0)
1254 return res;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001255 res = hotkey_get(&status, &mask);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001256 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001257 if (res)
1258 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
1260 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001261 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001262 len += sprintf(p + len, "mask:\t\t0x%08x\n", mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 len += sprintf(p + len,
1264 "commands:\tenable, disable, reset, <mask>\n");
1265 } else {
1266 len += sprintf(p + len, "mask:\t\tnot supported\n");
1267 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
1268 }
1269
1270 return len;
1271}
1272
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001273static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001275 int res, status;
1276 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 char *cmd;
1278 int do_cmd = 0;
1279
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001280 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 return -ENODEV;
1282
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001283 res = mutex_lock_interruptible(&hotkey_mutex);
1284 if (res < 0)
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001285 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001286
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001287 res = hotkey_get(&status, &mask);
1288 if (res)
1289 goto errexit;
1290
1291 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 while ((cmd = next_cmd(&buf))) {
1293 if (strlencmp(cmd, "enable") == 0) {
1294 status = 1;
1295 } else if (strlencmp(cmd, "disable") == 0) {
1296 status = 0;
1297 } else if (strlencmp(cmd, "reset") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001298 status = hotkey_orig_status;
1299 mask = hotkey_orig_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
1301 /* mask set */
1302 } else if (sscanf(cmd, "%x", &mask) == 1) {
1303 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001304 } else {
1305 res = -EINVAL;
1306 goto errexit;
1307 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 do_cmd = 1;
1309 }
1310
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001311 if (do_cmd)
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001312 res = hotkey_set(status, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001314errexit:
1315 mutex_unlock(&hotkey_mutex);
1316 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001317}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001319static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001320 .hid = IBM_HKEY_HID,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001321 .notify = hotkey_notify,
1322 .handle = &hkey_handle,
1323 .type = ACPI_DEVICE_NOTIFY,
1324};
1325
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001326static struct ibm_struct hotkey_driver_data = {
1327 .name = "hotkey",
1328 .read = hotkey_read,
1329 .write = hotkey_write,
1330 .exit = hotkey_exit,
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03001331 .resume = hotkey_resume,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001332 .acpi = &ibm_hotkey_acpidriver,
1333};
1334
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001335/*************************************************************************
1336 * Bluetooth subdriver
1337 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001339/* sysfs bluetooth enable ---------------------------------------------- */
1340static ssize_t bluetooth_enable_show(struct device *dev,
1341 struct device_attribute *attr,
1342 char *buf)
1343{
1344 int status;
1345
1346 status = bluetooth_get_radiosw();
1347 if (status < 0)
1348 return status;
1349
1350 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
1351}
1352
1353static ssize_t bluetooth_enable_store(struct device *dev,
1354 struct device_attribute *attr,
1355 const char *buf, size_t count)
1356{
1357 unsigned long t;
1358 int res;
1359
1360 if (parse_strtoul(buf, 1, &t))
1361 return -EINVAL;
1362
1363 res = bluetooth_set_radiosw(t);
1364
1365 return (res) ? res : count;
1366}
1367
1368static struct device_attribute dev_attr_bluetooth_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001369 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001370 bluetooth_enable_show, bluetooth_enable_store);
1371
1372/* --------------------------------------------------------------------- */
1373
1374static struct attribute *bluetooth_attributes[] = {
1375 &dev_attr_bluetooth_enable.attr,
1376 NULL
1377};
1378
1379static const struct attribute_group bluetooth_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001380 .attrs = bluetooth_attributes,
1381};
1382
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001383static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001385 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001386 int status = 0;
1387
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001388 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
1389
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001390 IBM_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001391
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001392 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1393 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001394 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001395 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001397 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
1398 str_supported(tp_features.bluetooth),
1399 status);
1400
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001401 if (tp_features.bluetooth) {
1402 if (!(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
1403 /* no bluetooth hardware present in system */
1404 tp_features.bluetooth = 0;
1405 dbg_printk(TPACPI_DBG_INIT,
1406 "bluetooth hardware not installed\n");
1407 } else {
1408 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
1409 &bluetooth_attr_group);
1410 if (res)
1411 return res;
1412 }
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001413 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001414
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001415 return (tp_features.bluetooth)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416}
1417
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001418static void bluetooth_exit(void)
1419{
1420 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
1421 &bluetooth_attr_group);
1422}
1423
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001424static int bluetooth_get_radiosw(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425{
1426 int status;
1427
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001428 if (!tp_features.bluetooth)
1429 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001431 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
1432 return -EIO;
1433
1434 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
1435}
1436
1437static int bluetooth_set_radiosw(int radio_on)
1438{
1439 int status;
1440
1441 if (!tp_features.bluetooth)
1442 return -ENODEV;
1443
1444 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
1445 return -EIO;
1446 if (radio_on)
1447 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
1448 else
1449 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
1450 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
1451 return -EIO;
1452
1453 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454}
1455
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001456/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001457static int bluetooth_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458{
1459 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001460 int status = bluetooth_get_radiosw();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001462 if (!tp_features.bluetooth)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 len += sprintf(p + len, "status:\t\tnot supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001465 len += sprintf(p + len, "status:\t\t%s\n",
1466 (status)? "enabled" : "disabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 len += sprintf(p + len, "commands:\tenable, disable\n");
1468 }
1469
1470 return len;
1471}
1472
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001473static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001477 if (!tp_features.bluetooth)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001478 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
1480 while ((cmd = next_cmd(&buf))) {
1481 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001482 bluetooth_set_radiosw(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001484 bluetooth_set_radiosw(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 } else
1486 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 }
1488
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 return 0;
1490}
1491
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001492static struct ibm_struct bluetooth_driver_data = {
1493 .name = "bluetooth",
1494 .read = bluetooth_read,
1495 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001496 .exit = bluetooth_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001497};
1498
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001499/*************************************************************************
1500 * Wan subdriver
1501 */
1502
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001503/* sysfs wan enable ---------------------------------------------------- */
1504static ssize_t wan_enable_show(struct device *dev,
1505 struct device_attribute *attr,
1506 char *buf)
1507{
1508 int status;
1509
1510 status = wan_get_radiosw();
1511 if (status < 0)
1512 return status;
1513
1514 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
1515}
1516
1517static ssize_t wan_enable_store(struct device *dev,
1518 struct device_attribute *attr,
1519 const char *buf, size_t count)
1520{
1521 unsigned long t;
1522 int res;
1523
1524 if (parse_strtoul(buf, 1, &t))
1525 return -EINVAL;
1526
1527 res = wan_set_radiosw(t);
1528
1529 return (res) ? res : count;
1530}
1531
1532static struct device_attribute dev_attr_wan_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001533 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001534 wan_enable_show, wan_enable_store);
1535
1536/* --------------------------------------------------------------------- */
1537
1538static struct attribute *wan_attributes[] = {
1539 &dev_attr_wan_enable.attr,
1540 NULL
1541};
1542
1543static const struct attribute_group wan_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001544 .attrs = wan_attributes,
1545};
1546
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001547static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001548{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001549 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001550 int status = 0;
1551
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001552 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
1553
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001554 IBM_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001555
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001556 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001557 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001558
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001559 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
1560 str_supported(tp_features.wan),
1561 status);
1562
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001563 if (tp_features.wan) {
1564 if (!(status & TP_ACPI_WANCARD_HWPRESENT)) {
1565 /* no wan hardware present in system */
1566 tp_features.wan = 0;
1567 dbg_printk(TPACPI_DBG_INIT,
1568 "wan hardware not installed\n");
1569 } else {
1570 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
1571 &wan_attr_group);
1572 if (res)
1573 return res;
1574 }
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001575 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001576
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001577 return (tp_features.wan)? 0 : 1;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001578}
1579
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001580static void wan_exit(void)
1581{
1582 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
1583 &wan_attr_group);
1584}
1585
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001586static int wan_get_radiosw(void)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001587{
1588 int status;
1589
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001590 if (!tp_features.wan)
1591 return -ENODEV;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001592
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001593 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
1594 return -EIO;
1595
1596 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
1597}
1598
1599static int wan_set_radiosw(int radio_on)
1600{
1601 int status;
1602
1603 if (!tp_features.wan)
1604 return -ENODEV;
1605
1606 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
1607 return -EIO;
1608 if (radio_on)
1609 status |= TP_ACPI_WANCARD_RADIOSSW;
1610 else
1611 status &= ~TP_ACPI_WANCARD_RADIOSSW;
1612 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
1613 return -EIO;
1614
1615 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001616}
1617
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001618/* procfs -------------------------------------------------------------- */
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001619static int wan_read(char *p)
1620{
1621 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001622 int status = wan_get_radiosw();
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001623
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001624 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001625 len += sprintf(p + len, "status:\t\tnot supported\n");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001626 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001627 len += sprintf(p + len, "status:\t\t%s\n",
1628 (status)? "enabled" : "disabled");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001629 len += sprintf(p + len, "commands:\tenable, disable\n");
1630 }
1631
1632 return len;
1633}
1634
1635static int wan_write(char *buf)
1636{
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001637 char *cmd;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001638
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001639 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001640 return -ENODEV;
1641
1642 while ((cmd = next_cmd(&buf))) {
1643 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001644 wan_set_radiosw(1);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001645 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001646 wan_set_radiosw(0);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001647 } else
1648 return -EINVAL;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001649 }
1650
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001651 return 0;
1652}
1653
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001654static struct ibm_struct wan_driver_data = {
1655 .name = "wan",
1656 .read = wan_read,
1657 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001658 .exit = wan_exit,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03001659 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001660};
1661
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001662/*************************************************************************
1663 * Video subdriver
1664 */
1665
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02001666static enum video_access_mode video_supported;
1667static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001668
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001669IBM_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
1670 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
1671 "\\_SB.PCI0.VID0", /* 770e */
1672 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
1673 "\\_SB.PCI0.AGP.VID", /* all others */
1674 ); /* R30, R31 */
1675
1676IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
1677
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001678static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001680 int ivga;
1681
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001682 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
1683
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001684 IBM_ACPIHANDLE_INIT(vid);
1685 IBM_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001686
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001687 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
1688 /* G41, assume IVGA doesn't change */
1689 vid_handle = vid2_handle;
1690
1691 if (!vid_handle)
1692 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001693 video_supported = TPACPI_VIDEO_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001694 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
1695 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001696 video_supported = TPACPI_VIDEO_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001697 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
1698 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001699 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001700 else
1701 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001702 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001704 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
1705 str_supported(video_supported != TPACPI_VIDEO_NONE),
1706 video_supported);
1707
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001708 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709}
1710
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001711static void video_exit(void)
1712{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001713 dbg_printk(TPACPI_DBG_EXIT,
1714 "restoring original video autoswitch mode\n");
1715 if (video_autosw_set(video_orig_autosw))
1716 printk(IBM_ERR "error while trying to restore original "
1717 "video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001718}
1719
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001720static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721{
1722 int status = 0;
1723 int i;
1724
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001725 switch (video_supported) {
1726 case TPACPI_VIDEO_570:
1727 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
1728 TP_ACPI_VIDEO_570_PHSCMD))
1729 return -EIO;
1730 status = i & TP_ACPI_VIDEO_570_PHSMASK;
1731 break;
1732 case TPACPI_VIDEO_770:
1733 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
1734 return -EIO;
1735 if (i)
1736 status |= TP_ACPI_VIDEO_S_LCD;
1737 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
1738 return -EIO;
1739 if (i)
1740 status |= TP_ACPI_VIDEO_S_CRT;
1741 break;
1742 case TPACPI_VIDEO_NEW:
1743 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
1744 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
1745 return -EIO;
1746 if (i)
1747 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001749 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
1750 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
1751 return -EIO;
1752 if (i)
1753 status |= TP_ACPI_VIDEO_S_LCD;
1754 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
1755 return -EIO;
1756 if (i)
1757 status |= TP_ACPI_VIDEO_S_DVI;
1758 break;
1759 default:
1760 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001761 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762
1763 return status;
1764}
1765
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001766static int video_outputsw_set(int status)
1767{
1768 int autosw;
1769 int res = 0;
1770
1771 switch (video_supported) {
1772 case TPACPI_VIDEO_570:
1773 res = acpi_evalf(NULL, NULL,
1774 "\\_SB.PHS2", "vdd",
1775 TP_ACPI_VIDEO_570_PHS2CMD,
1776 status | TP_ACPI_VIDEO_570_PHS2SET);
1777 break;
1778 case TPACPI_VIDEO_770:
1779 autosw = video_autosw_get();
1780 if (autosw < 0)
1781 return autosw;
1782
1783 res = video_autosw_set(1);
1784 if (res)
1785 return res;
1786 res = acpi_evalf(vid_handle, NULL,
1787 "ASWT", "vdd", status * 0x100, 0);
1788 if (!autosw && video_autosw_set(autosw)) {
1789 printk(IBM_ERR "video auto-switch left enabled due to error\n");
1790 return -EIO;
1791 }
1792 break;
1793 case TPACPI_VIDEO_NEW:
1794 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
1795 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
1796 break;
1797 default:
1798 return -ENOSYS;
1799 }
1800
1801 return (res)? 0 : -EIO;
1802}
1803
1804static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001806 int autosw = 0;
1807
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001808 switch (video_supported) {
1809 case TPACPI_VIDEO_570:
1810 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
1811 return -EIO;
1812 break;
1813 case TPACPI_VIDEO_770:
1814 case TPACPI_VIDEO_NEW:
1815 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
1816 return -EIO;
1817 break;
1818 default:
1819 return -ENOSYS;
1820 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001821
1822 return autosw & 1;
1823}
1824
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001825static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001826{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001827 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001828 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001829 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001830}
1831
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001832static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001833{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001834 int autosw = video_autosw_get();
1835 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001836
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001837 if (autosw < 0)
1838 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001839
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001840 switch (video_supported) {
1841 case TPACPI_VIDEO_570:
1842 res = video_autosw_set(1);
1843 if (res)
1844 return res;
1845 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
1846 break;
1847 case TPACPI_VIDEO_770:
1848 case TPACPI_VIDEO_NEW:
1849 res = video_autosw_set(1);
1850 if (res)
1851 return res;
1852 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
1853 break;
1854 default:
1855 return -ENOSYS;
1856 }
1857 if (!autosw && video_autosw_set(autosw)) {
1858 printk(IBM_ERR "video auto-switch left enabled due to error\n");
1859 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001860 }
1861
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001862 return (res)? 0 : -EIO;
1863}
1864
1865static int video_expand_toggle(void)
1866{
1867 switch (video_supported) {
1868 case TPACPI_VIDEO_570:
1869 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
1870 0 : -EIO;
1871 case TPACPI_VIDEO_770:
1872 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
1873 0 : -EIO;
1874 case TPACPI_VIDEO_NEW:
1875 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
1876 0 : -EIO;
1877 default:
1878 return -ENOSYS;
1879 }
1880 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001881}
1882
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001883static int video_read(char *p)
1884{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001885 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001886 int len = 0;
1887
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001888 if (video_supported == TPACPI_VIDEO_NONE) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001889 len += sprintf(p + len, "status:\t\tnot supported\n");
1890 return len;
1891 }
1892
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001893 status = video_outputsw_get();
1894 if (status < 0)
1895 return status;
1896
1897 autosw = video_autosw_get();
1898 if (autosw < 0)
1899 return autosw;
1900
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001901 len += sprintf(p + len, "status:\t\tsupported\n");
1902 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
1903 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001904 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001905 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
1906 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
1907 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
1908 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001909 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001910 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
1911 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
1912 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
1913
1914 return len;
1915}
1916
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001917static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918{
1919 char *cmd;
1920 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001921 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001923 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001924 return -ENODEV;
1925
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001926 enable = 0;
1927 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928
1929 while ((cmd = next_cmd(&buf))) {
1930 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001931 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001933 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001935 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001937 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001938 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001939 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001940 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001941 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001942 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001943 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001945 res = video_autosw_set(1);
1946 if (res)
1947 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001949 res = video_autosw_set(0);
1950 if (res)
1951 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001953 res = video_outputsw_cycle();
1954 if (res)
1955 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001957 res = video_expand_toggle();
1958 if (res)
1959 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 } else
1961 return -EINVAL;
1962 }
1963
1964 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001965 status = video_outputsw_get();
1966 if (status < 0)
1967 return status;
1968 res = video_outputsw_set((status & ~disable) | enable);
1969 if (res)
1970 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 }
1972
1973 return 0;
1974}
1975
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001976static struct ibm_struct video_driver_data = {
1977 .name = "video",
1978 .read = video_read,
1979 .write = video_write,
1980 .exit = video_exit,
1981};
1982
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001983/*************************************************************************
1984 * Light (thinklight) subdriver
1985 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001987IBM_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
1988IBM_HANDLE(ledb, ec, "LEDB"); /* G4x */
1989
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001990static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001992 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
1993
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001994 IBM_ACPIHANDLE_INIT(ledb);
1995 IBM_ACPIHANDLE_INIT(lght);
1996 IBM_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001997
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001998 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001999 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002000
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002001 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002002 /* light status not supported on
2003 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002004 tp_features.light_status =
2005 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002007 vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002008 str_supported(tp_features.light));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002009
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002010 return (tp_features.light)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011}
2012
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002013static int light_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014{
2015 int len = 0;
2016 int status = 0;
2017
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002018 if (!tp_features.light) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002019 len += sprintf(p + len, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002020 } else if (!tp_features.light_status) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002021 len += sprintf(p + len, "status:\t\tunknown\n");
2022 len += sprintf(p + len, "commands:\ton, off\n");
2023 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
2025 return -EIO;
2026 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002027 len += sprintf(p + len, "commands:\ton, off\n");
2028 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029
2030 return len;
2031}
2032
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002033static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034{
2035 int cmos_cmd, lght_cmd;
2036 char *cmd;
2037 int success;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002038
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002039 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002040 return -ENODEV;
2041
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 while ((cmd = next_cmd(&buf))) {
2043 if (strlencmp(cmd, "on") == 0) {
2044 cmos_cmd = 0x0c;
2045 lght_cmd = 1;
2046 } else if (strlencmp(cmd, "off") == 0) {
2047 cmos_cmd = 0x0d;
2048 lght_cmd = 0;
2049 } else
2050 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002051
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 success = cmos_handle ?
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002053 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
2054 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 if (!success)
2056 return -EIO;
2057 }
2058
2059 return 0;
2060}
2061
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002062static struct ibm_struct light_driver_data = {
2063 .name = "light",
2064 .read = light_read,
2065 .write = light_write,
2066};
2067
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002068/*************************************************************************
2069 * Dock subdriver
2070 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03002072#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002073
2074IBM_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
2075 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
2076 "\\_SB.PCI0.PCI1.DOCK", /* all others */
2077 "\\_SB.PCI.ISA.SLCE", /* 570 */
2078 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
2079
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002080/* don't list other alternatives as we install a notify handler on the 570 */
2081IBM_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
2082
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03002083static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
2084 {
2085 .notify = dock_notify,
2086 .handle = &dock_handle,
2087 .type = ACPI_SYSTEM_NOTIFY,
2088 },
2089 {
Henrique de Moraes Holschuh996fba02007-07-18 23:45:40 -03002090 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
2091 * We just use it to get notifications of dock hotplug
2092 * in very old thinkpads */
2093 .hid = PCI_ROOT_HID_STRING,
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03002094 .notify = dock_notify,
2095 .handle = &pci_handle,
2096 .type = ACPI_SYSTEM_NOTIFY,
2097 },
2098};
2099
2100static struct ibm_struct dock_driver_data[2] = {
2101 {
2102 .name = "dock",
2103 .read = dock_read,
2104 .write = dock_write,
2105 .acpi = &ibm_dock_acpidriver[0],
2106 },
2107 {
2108 .name = "dock",
2109 .acpi = &ibm_dock_acpidriver[1],
2110 },
2111};
2112
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113#define dock_docked() (_sta(dock_handle) & 1)
2114
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002115static int __init dock_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002116{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002117 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
2118
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002119 IBM_ACPIHANDLE_INIT(dock);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002120
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002121 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
2122 str_supported(dock_handle != NULL));
2123
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002124 return (dock_handle)? 0 : 1;
2125}
2126
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03002127static int __init dock_init2(struct ibm_init_struct *iibm)
2128{
2129 int dock2_needed;
2130
2131 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
2132
2133 if (dock_driver_data[0].flags.acpi_driver_registered &&
2134 dock_driver_data[0].flags.acpi_notify_installed) {
2135 IBM_ACPIHANDLE_INIT(pci);
2136 dock2_needed = (pci_handle != NULL);
2137 vdbg_printk(TPACPI_DBG_INIT,
2138 "dock PCI handler for the TP 570 is %s\n",
2139 str_supported(dock2_needed));
2140 } else {
2141 vdbg_printk(TPACPI_DBG_INIT,
2142 "dock subdriver part 2 not required\n");
2143 dock2_needed = 0;
2144 }
2145
2146 return (dock2_needed)? 0 : 1;
2147}
2148
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002149static void dock_notify(struct ibm_struct *ibm, u32 event)
2150{
2151 int docked = dock_docked();
Henrique de Moraes Holschuh996fba02007-07-18 23:45:40 -03002152 int pci = ibm->acpi->hid && strstr(ibm->acpi->hid, PCI_ROOT_HID_STRING);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002153
2154 if (event == 1 && !pci) /* 570 */
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002155 acpi_bus_generate_event(ibm->acpi->device, event, 1); /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002156 else if (event == 1 && pci) /* 570 */
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002157 acpi_bus_generate_event(ibm->acpi->device, event, 3); /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002158 else if (event == 3 && docked)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002159 acpi_bus_generate_event(ibm->acpi->device, event, 1); /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002160 else if (event == 3 && !docked)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002161 acpi_bus_generate_event(ibm->acpi->device, event, 2); /* undock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002162 else if (event == 0 && docked)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002163 acpi_bus_generate_event(ibm->acpi->device, event, 3); /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002164 else {
2165 printk(IBM_ERR "unknown dock event %d, status %d\n",
2166 event, _sta(dock_handle));
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002167 acpi_bus_generate_event(ibm->acpi->device, event, 0); /* unknown */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002168 }
2169}
2170
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002171static int dock_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172{
2173 int len = 0;
2174 int docked = dock_docked();
2175
2176 if (!dock_handle)
2177 len += sprintf(p + len, "status:\t\tnot supported\n");
2178 else if (!docked)
2179 len += sprintf(p + len, "status:\t\tundocked\n");
2180 else {
2181 len += sprintf(p + len, "status:\t\tdocked\n");
2182 len += sprintf(p + len, "commands:\tdock, undock\n");
2183 }
2184
2185 return len;
2186}
2187
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002188static int dock_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189{
2190 char *cmd;
2191
2192 if (!dock_docked())
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002193 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194
2195 while ((cmd = next_cmd(&buf))) {
2196 if (strlencmp(cmd, "undock") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002197 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
2198 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 return -EIO;
2200 } else if (strlencmp(cmd, "dock") == 0) {
2201 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
2202 return -EIO;
2203 } else
2204 return -EINVAL;
2205 }
2206
2207 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002208}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03002210#endif /* CONFIG_THINKPAD_ACPI_DOCK */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002212/*************************************************************************
2213 * Bay subdriver
2214 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03002216#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002217IBM_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
2218 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
2219 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
2220 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
2221 ); /* A21e, R30, R31 */
2222IBM_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
2223 "_EJ0", /* all others */
2224 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
2225IBM_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
2226 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
2227 ); /* all others */
2228IBM_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
2229 "_EJ0", /* 770x */
2230 ); /* all others */
2231
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002232static int __init bay_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002234 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
2235
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002236 IBM_ACPIHANDLE_INIT(bay);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002237 if (bay_handle)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002238 IBM_ACPIHANDLE_INIT(bay_ej);
2239 IBM_ACPIHANDLE_INIT(bay2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002240 if (bay2_handle)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002241 IBM_ACPIHANDLE_INIT(bay2_ej);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002242
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002243 tp_features.bay_status = bay_handle &&
2244 acpi_evalf(bay_handle, NULL, "_STA", "qv");
2245 tp_features.bay_status2 = bay2_handle &&
2246 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002247
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002248 tp_features.bay_eject = bay_handle && bay_ej_handle &&
2249 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
2250 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
2251 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002253 vdbg_printk(TPACPI_DBG_INIT,
2254 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002255 str_supported(tp_features.bay_status),
2256 str_supported(tp_features.bay_eject),
2257 str_supported(tp_features.bay_status2),
2258 str_supported(tp_features.bay_eject2));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002259
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002260 return (tp_features.bay_status || tp_features.bay_eject ||
2261 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262}
2263
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002264static void bay_notify(struct ibm_struct *ibm, u32 event)
2265{
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002266 acpi_bus_generate_event(ibm->acpi->device, event, 0);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002267}
2268
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002269#define bay_occupied(b) (_sta(b##_handle) & 1)
2270
2271static int bay_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272{
2273 int len = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002274 int occupied = bay_occupied(bay);
2275 int occupied2 = bay_occupied(bay2);
2276 int eject, eject2;
2277
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002278 len += sprintf(p + len, "status:\t\t%s\n",
2279 tp_features.bay_status ?
2280 (occupied ? "occupied" : "unoccupied") :
2281 "not supported");
2282 if (tp_features.bay_status2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002283 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
2284 "occupied" : "unoccupied");
2285
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002286 eject = tp_features.bay_eject && occupied;
2287 eject2 = tp_features.bay_eject2 && occupied2;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002288
2289 if (eject && eject2)
2290 len += sprintf(p + len, "commands:\teject, eject2\n");
2291 else if (eject)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 len += sprintf(p + len, "commands:\teject\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002293 else if (eject2)
2294 len += sprintf(p + len, "commands:\teject2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295
2296 return len;
2297}
2298
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002299static int bay_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300{
2301 char *cmd;
2302
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002303 if (!tp_features.bay_eject && !tp_features.bay_eject2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002304 return -ENODEV;
2305
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002307 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002308 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
2309 return -EIO;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002310 } else if (tp_features.bay_eject2 &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002311 strlencmp(cmd, "eject2") == 0) {
2312 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 return -EIO;
2314 } else
2315 return -EINVAL;
2316 }
2317
2318 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002319}
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002320
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002321static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
2322 .notify = bay_notify,
2323 .handle = &bay_handle,
2324 .type = ACPI_SYSTEM_NOTIFY,
2325};
2326
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002327static struct ibm_struct bay_driver_data = {
2328 .name = "bay",
2329 .read = bay_read,
2330 .write = bay_write,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002331 .acpi = &ibm_bay_acpidriver,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002332};
2333
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03002334#endif /* CONFIG_THINKPAD_ACPI_BAY */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002336/*************************************************************************
2337 * CMOS subdriver
2338 */
2339
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03002340/* sysfs cmos_command -------------------------------------------------- */
2341static ssize_t cmos_command_store(struct device *dev,
2342 struct device_attribute *attr,
2343 const char *buf, size_t count)
2344{
2345 unsigned long cmos_cmd;
2346 int res;
2347
2348 if (parse_strtoul(buf, 21, &cmos_cmd))
2349 return -EINVAL;
2350
2351 res = issue_thinkpad_cmos_command(cmos_cmd);
2352 return (res)? res : count;
2353}
2354
2355static struct device_attribute dev_attr_cmos_command =
2356 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
2357
2358/* --------------------------------------------------------------------- */
2359
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002360static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002361{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03002362 int res;
2363
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002364 vdbg_printk(TPACPI_DBG_INIT,
2365 "initializing cmos commands subdriver\n");
2366
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002367 IBM_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002368
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002369 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
2370 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03002371
2372 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
2373 if (res)
2374 return res;
2375
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002376 return (cmos_handle)? 0 : 1;
2377}
2378
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03002379static void cmos_exit(void)
2380{
2381 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
2382}
2383
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002384static int cmos_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385{
2386 int len = 0;
2387
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002388 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2389 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 if (!cmos_handle)
2391 len += sprintf(p + len, "status:\t\tnot supported\n");
2392 else {
2393 len += sprintf(p + len, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002394 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 }
2396
2397 return len;
2398}
2399
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002400static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401{
2402 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03002403 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404
2405 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002406 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
2407 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 /* cmos_cmd set */
2409 } else
2410 return -EINVAL;
2411
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03002412 res = issue_thinkpad_cmos_command(cmos_cmd);
2413 if (res)
2414 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 }
2416
2417 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002418}
2419
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002420static struct ibm_struct cmos_driver_data = {
2421 .name = "cmos",
2422 .read = cmos_read,
2423 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03002424 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002425};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002426
2427/*************************************************************************
2428 * LED subdriver
2429 */
2430
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02002431static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002432
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002433IBM_HANDLE(led, ec, "SLED", /* 570 */
2434 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
2435 "LED", /* all others */
2436 ); /* R30, R31 */
2437
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002438static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002439{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002440 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
2441
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002442 IBM_ACPIHANDLE_INIT(led);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002443
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002444 if (!led_handle)
2445 /* led not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002446 led_supported = TPACPI_LED_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002447 else if (strlencmp(led_path, "SLED") == 0)
2448 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002449 led_supported = TPACPI_LED_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002450 else if (strlencmp(led_path, "SYSL") == 0)
2451 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002452 led_supported = TPACPI_LED_OLD;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002453 else
2454 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002455 led_supported = TPACPI_LED_NEW;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002456
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002457 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
2458 str_supported(led_supported), led_supported);
2459
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002460 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002461}
2462
2463#define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
2464
2465static int led_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466{
2467 int len = 0;
2468
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002469 if (!led_supported) {
2470 len += sprintf(p + len, "status:\t\tnot supported\n");
2471 return len;
2472 }
2473 len += sprintf(p + len, "status:\t\tsupported\n");
2474
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002475 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002476 /* 570 */
2477 int i, status;
2478 for (i = 0; i < 8; i++) {
2479 if (!acpi_evalf(ec_handle,
2480 &status, "GLED", "dd", 1 << i))
2481 return -EIO;
2482 len += sprintf(p + len, "%d:\t\t%s\n",
2483 i, led_status(status));
2484 }
2485 }
2486
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 len += sprintf(p + len, "commands:\t"
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002488 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489
2490 return len;
2491}
2492
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002493/* off, on, blink */
2494static const int led_sled_arg1[] = { 0, 1, 3 };
2495static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
2496static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
2497static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
2498
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002499static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500{
2501 char *cmd;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002502 int led, ind, ret;
2503
2504 if (!led_supported)
2505 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506
2507 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002508 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 return -EINVAL;
2510
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002511 if (strstr(cmd, "off")) {
2512 ind = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 } else if (strstr(cmd, "on")) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002514 ind = 1;
2515 } else if (strstr(cmd, "blink")) {
2516 ind = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 } else
2518 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002519
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002520 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002521 /* 570 */
2522 led = 1 << led;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002524 led, led_sled_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 return -EIO;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002526 } else if (led_supported == TPACPI_LED_OLD) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002527 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
2528 led = 1 << led;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002529 ret = ec_write(TPACPI_LED_EC_HLMS, led);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002530 if (ret >= 0)
2531 ret =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002532 ec_write(TPACPI_LED_EC_HLBL,
Henrique de Moraes Holschuhe062e032007-03-23 17:33:55 -03002533 led * led_exp_hlbl[ind]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002534 if (ret >= 0)
2535 ret =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002536 ec_write(TPACPI_LED_EC_HLCL,
Henrique de Moraes Holschuhe062e032007-03-23 17:33:55 -03002537 led * led_exp_hlcl[ind]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002538 if (ret < 0)
2539 return ret;
2540 } else {
2541 /* all others */
2542 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
2543 led, led_led_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002545 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 }
2547
2548 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002549}
2550
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002551static struct ibm_struct led_driver_data = {
2552 .name = "led",
2553 .read = led_read,
2554 .write = led_write,
2555};
2556
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002557/*************************************************************************
2558 * Beep subdriver
2559 */
2560
2561IBM_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
2562
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002563static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002564{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002565 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
2566
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002567 IBM_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002568
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002569 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
2570 str_supported(beep_handle != NULL));
2571
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002572 return (beep_handle)? 0 : 1;
2573}
2574
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002575static int beep_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576{
2577 int len = 0;
2578
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002579 if (!beep_handle)
2580 len += sprintf(p + len, "status:\t\tnot supported\n");
2581 else {
2582 len += sprintf(p + len, "status:\t\tsupported\n");
2583 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
2584 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585
2586 return len;
2587}
2588
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002589static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590{
2591 char *cmd;
2592 int beep_cmd;
2593
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002594 if (!beep_handle)
2595 return -ENODEV;
2596
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002598 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
2599 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 /* beep_cmd set */
2601 } else
2602 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002603 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 return -EIO;
2605 }
2606
2607 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002608}
2609
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002610static struct ibm_struct beep_driver_data = {
2611 .name = "beep",
2612 .read = beep_read,
2613 .write = beep_write,
2614};
2615
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002616/*************************************************************************
2617 * Thermal subdriver
2618 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002619
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002620static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002621
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03002622/* sysfs temp##_input -------------------------------------------------- */
2623
2624static ssize_t thermal_temp_input_show(struct device *dev,
2625 struct device_attribute *attr,
2626 char *buf)
2627{
2628 struct sensor_device_attribute *sensor_attr =
2629 to_sensor_dev_attr(attr);
2630 int idx = sensor_attr->index;
2631 s32 value;
2632 int res;
2633
2634 res = thermal_get_sensor(idx, &value);
2635 if (res)
2636 return res;
2637 if (value == TP_EC_THERMAL_TMP_NA * 1000)
2638 return -ENXIO;
2639
2640 return snprintf(buf, PAGE_SIZE, "%d\n", value);
2641}
2642
2643#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
2644 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, thermal_temp_input_show, NULL, _idxB)
2645
2646static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
2647 THERMAL_SENSOR_ATTR_TEMP(1, 0),
2648 THERMAL_SENSOR_ATTR_TEMP(2, 1),
2649 THERMAL_SENSOR_ATTR_TEMP(3, 2),
2650 THERMAL_SENSOR_ATTR_TEMP(4, 3),
2651 THERMAL_SENSOR_ATTR_TEMP(5, 4),
2652 THERMAL_SENSOR_ATTR_TEMP(6, 5),
2653 THERMAL_SENSOR_ATTR_TEMP(7, 6),
2654 THERMAL_SENSOR_ATTR_TEMP(8, 7),
2655 THERMAL_SENSOR_ATTR_TEMP(9, 8),
2656 THERMAL_SENSOR_ATTR_TEMP(10, 9),
2657 THERMAL_SENSOR_ATTR_TEMP(11, 10),
2658 THERMAL_SENSOR_ATTR_TEMP(12, 11),
2659 THERMAL_SENSOR_ATTR_TEMP(13, 12),
2660 THERMAL_SENSOR_ATTR_TEMP(14, 13),
2661 THERMAL_SENSOR_ATTR_TEMP(15, 14),
2662 THERMAL_SENSOR_ATTR_TEMP(16, 15),
2663};
2664
2665#define THERMAL_ATTRS(X) \
2666 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
2667
2668static struct attribute *thermal_temp_input_attr[] = {
2669 THERMAL_ATTRS(8),
2670 THERMAL_ATTRS(9),
2671 THERMAL_ATTRS(10),
2672 THERMAL_ATTRS(11),
2673 THERMAL_ATTRS(12),
2674 THERMAL_ATTRS(13),
2675 THERMAL_ATTRS(14),
2676 THERMAL_ATTRS(15),
2677 THERMAL_ATTRS(0),
2678 THERMAL_ATTRS(1),
2679 THERMAL_ATTRS(2),
2680 THERMAL_ATTRS(3),
2681 THERMAL_ATTRS(4),
2682 THERMAL_ATTRS(5),
2683 THERMAL_ATTRS(6),
2684 THERMAL_ATTRS(7),
2685 NULL
2686};
2687
2688static const struct attribute_group thermal_temp_input16_group = {
2689 .attrs = thermal_temp_input_attr
2690};
2691
2692static const struct attribute_group thermal_temp_input8_group = {
2693 .attrs = &thermal_temp_input_attr[8]
2694};
2695
2696#undef THERMAL_SENSOR_ATTR_TEMP
2697#undef THERMAL_ATTRS
2698
2699/* --------------------------------------------------------------------- */
2700
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002701static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002702{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002703 u8 t, ta1, ta2;
2704 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002705 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03002706 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002707
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002708 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
2709
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002710 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002711
Henrique de Moraes Holschuh3d6f99c2007-07-18 23:45:46 -03002712 if (thinkpad_id.ec_model) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002713 /*
2714 * Direct EC access mode: sensors at registers
2715 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
2716 * non-implemented, thermal sensors return 0x80 when
2717 * not available
2718 */
2719
2720 ta1 = ta2 = 0;
2721 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002722 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002723 ta1 |= t;
2724 } else {
2725 ta1 = 0;
2726 break;
2727 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002728 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002729 ta2 |= t;
2730 } else {
2731 ta1 = 0;
2732 break;
2733 }
2734 }
2735 if (ta1 == 0) {
2736 /* This is sheer paranoia, but we handle it anyway */
2737 if (acpi_tmp7) {
2738 printk(IBM_ERR
2739 "ThinkPad ACPI EC access misbehaving, "
2740 "falling back to ACPI TMPx access mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002741 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002742 } else {
2743 printk(IBM_ERR
2744 "ThinkPad ACPI EC access misbehaving, "
2745 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002746 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002747 }
2748 } else {
2749 thermal_read_mode =
2750 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002751 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002752 }
2753 } else if (acpi_tmp7) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002754 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
2755 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002756 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002757 } else {
2758 /* Standard ACPI TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002759 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002760 }
2761 } else {
2762 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002763 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002764 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002765
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002766 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
2767 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
2768 thermal_read_mode);
2769
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03002770 switch(thermal_read_mode) {
2771 case TPACPI_THERMAL_TPEC_16:
2772 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2773 &thermal_temp_input16_group);
2774 if (res)
2775 return res;
2776 break;
2777 case TPACPI_THERMAL_TPEC_8:
2778 case TPACPI_THERMAL_ACPI_TMP07:
2779 case TPACPI_THERMAL_ACPI_UPDT:
2780 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2781 &thermal_temp_input8_group);
2782 if (res)
2783 return res;
2784 break;
2785 case TPACPI_THERMAL_NONE:
2786 default:
2787 return 1;
2788 }
2789
2790 return 0;
2791}
2792
2793static void thermal_exit(void)
2794{
2795 switch(thermal_read_mode) {
2796 case TPACPI_THERMAL_TPEC_16:
2797 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2798 &thermal_temp_input16_group);
2799 break;
2800 case TPACPI_THERMAL_TPEC_8:
2801 case TPACPI_THERMAL_ACPI_TMP07:
2802 case TPACPI_THERMAL_ACPI_UPDT:
2803 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2804 &thermal_temp_input16_group);
2805 break;
2806 case TPACPI_THERMAL_NONE:
2807 default:
2808 break;
2809 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002810}
2811
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002812/* idx is zero-based */
2813static int thermal_get_sensor(int idx, s32 *value)
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002814{
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002815 int t;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002816 s8 tmp;
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002817 char tmpi[5];
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002818
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002819 t = TP_EC_THERMAL_TMP0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002820
2821 switch (thermal_read_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002822#if TPACPI_MAX_THERMAL_SENSORS >= 16
2823 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002824 if (idx >= 8 && idx <= 15) {
2825 t = TP_EC_THERMAL_TMP8;
2826 idx -= 8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002827 }
2828 /* fallthrough */
2829#endif
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002830 case TPACPI_THERMAL_TPEC_8:
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002831 if (idx <= 7) {
2832 if (!acpi_ec_read(t + idx, &tmp))
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002833 return -EIO;
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002834 *value = tmp * 1000;
2835 return 0;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002836 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002837 break;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002838
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002839 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002840 if (idx <= 7) {
2841 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
2842 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
2843 return -EIO;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002844 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
2845 return -EIO;
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002846 *value = (t - 2732) * 100;
2847 return 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002848 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002849 break;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002850
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002851 case TPACPI_THERMAL_ACPI_TMP07:
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002852 if (idx <= 7) {
2853 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002854 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
2855 return -EIO;
Henrique de Moraes Holschuha8fba3da2007-07-18 23:45:45 -03002856 if (t > 127 || t < -127)
2857 t = TP_EC_THERMAL_TMP_NA;
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002858 *value = t * 1000;
2859 return 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002860 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002861 break;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002862
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002863 case TPACPI_THERMAL_NONE:
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002864 default:
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002865 return -ENOSYS;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002866 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002867
2868 return -EINVAL;
2869}
2870
2871static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
2872{
2873 int res, i;
2874 int n;
2875
2876 n = 8;
2877 i = 0;
2878
2879 if (!s)
2880 return -EINVAL;
2881
2882 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
2883 n = 16;
2884
2885 for(i = 0 ; i < n; i++) {
2886 res = thermal_get_sensor(i, &s->temp[i]);
2887 if (res)
2888 return res;
2889 }
2890
2891 return n;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002892}
2893
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002894static int thermal_read(char *p)
2895{
2896 int len = 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002897 int n, i;
2898 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002899
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002900 n = thermal_get_sensors(&t);
2901 if (unlikely(n < 0))
2902 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002903
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002904 len += sprintf(p + len, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002905
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002906 if (n > 0) {
2907 for (i = 0; i < (n - 1); i++)
2908 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
2909 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
2910 } else
2911 len += sprintf(p + len, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002912
2913 return len;
2914}
2915
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002916static struct ibm_struct thermal_driver_data = {
2917 .name = "thermal",
2918 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03002919 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002920};
2921
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002922/*************************************************************************
2923 * EC Dump subdriver
2924 */
2925
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002926static u8 ecdump_regs[256];
2927
2928static int ecdump_read(char *p)
2929{
2930 int len = 0;
2931 int i, j;
2932 u8 v;
2933
2934 len += sprintf(p + len, "EC "
2935 " +00 +01 +02 +03 +04 +05 +06 +07"
2936 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
2937 for (i = 0; i < 256; i += 16) {
2938 len += sprintf(p + len, "EC 0x%02x:", i);
2939 for (j = 0; j < 16; j++) {
2940 if (!acpi_ec_read(i + j, &v))
2941 break;
2942 if (v != ecdump_regs[i + j])
2943 len += sprintf(p + len, " *%02x", v);
2944 else
2945 len += sprintf(p + len, " %02x", v);
2946 ecdump_regs[i + j] = v;
2947 }
2948 len += sprintf(p + len, "\n");
2949 if (j != 16)
2950 break;
2951 }
2952
2953 /* These are way too dangerous to advertise openly... */
2954#if 0
2955 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
2956 " (<offset> is 00-ff, <value> is 00-ff)\n");
2957 len += sprintf(p + len, "commands:\t0x<offset> <value> "
2958 " (<offset> is 00-ff, <value> is 0-255)\n");
2959#endif
2960 return len;
2961}
2962
2963static int ecdump_write(char *buf)
2964{
2965 char *cmd;
2966 int i, v;
2967
2968 while ((cmd = next_cmd(&buf))) {
2969 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
2970 /* i and v set */
2971 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
2972 /* i and v set */
2973 } else
2974 return -EINVAL;
2975 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
2976 if (!acpi_ec_write(i, v))
2977 return -EIO;
2978 } else
2979 return -EINVAL;
2980 }
2981
2982 return 0;
2983}
2984
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002985static struct ibm_struct ecdump_driver_data = {
2986 .name = "ecdump",
2987 .read = ecdump_read,
2988 .write = ecdump_write,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03002989 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002990};
2991
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002992/*************************************************************************
2993 * Backlight/brightness subdriver
2994 */
Holger Macht8acb0252006-10-20 14:30:28 -07002995
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03002996static struct backlight_device *ibm_backlight_device;
Holger Macht8acb0252006-10-20 14:30:28 -07002997
Richard Purdie599a52d2007-02-10 23:07:48 +00002998static struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02002999 .get_brightness = brightness_get,
3000 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02003001};
3002
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03003003static struct mutex brightness_mutex;
3004
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003005static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02003006{
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02003007 int b;
3008
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003009 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
3010
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03003011 mutex_init(&brightness_mutex);
3012
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03003013 if (!brightness_mode) {
3014 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
3015 brightness_mode = 2;
3016 else
3017 brightness_mode = 3;
3018
3019 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
3020 brightness_mode);
3021 }
3022
3023 if (brightness_mode > 3)
3024 return -EINVAL;
3025
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02003026 b = brightness_get(NULL);
3027 if (b < 0)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03003028 return 1;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02003029
Henrique de Moraes Holschuh7d5a0152007-04-24 11:48:20 -03003030 ibm_backlight_device = backlight_device_register(
3031 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
3032 &ibm_backlight_data);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02003033 if (IS_ERR(ibm_backlight_device)) {
3034 printk(IBM_ERR "Could not register backlight device\n");
3035 return PTR_ERR(ibm_backlight_device);
3036 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003037 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02003038
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02003039 ibm_backlight_device->props.max_brightness = 7;
3040 ibm_backlight_device->props.brightness = b;
3041 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00003042
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02003043 return 0;
3044}
3045
3046static void brightness_exit(void)
3047{
3048 if (ibm_backlight_device) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003049 vdbg_printk(TPACPI_DBG_EXIT,
3050 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02003051 backlight_device_unregister(ibm_backlight_device);
3052 ibm_backlight_device = NULL;
3053 }
3054}
3055
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003056static int brightness_update_status(struct backlight_device *bd)
3057{
3058 return brightness_set(
3059 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
3060 bd->props.power == FB_BLANK_UNBLANK) ?
3061 bd->props.brightness : 0);
3062}
3063
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03003064/*
3065 * ThinkPads can read brightness from two places: EC 0x31, or
3066 * CMOS NVRAM byte 0x5E, bits 0-3.
3067 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003068static int brightness_get(struct backlight_device *bd)
3069{
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03003070 u8 lec = 0, lcmos = 0, level = 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003071
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03003072 if (brightness_mode & 1) {
3073 if (!acpi_ec_read(brightness_offset, &lec))
3074 return -EIO;
3075 lec &= 7;
3076 level = lec;
3077 };
3078 if (brightness_mode & 2) {
3079 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
3080 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
3081 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
3082 level = lcmos;
3083 }
3084
3085 if (brightness_mode == 3 && lec != lcmos) {
3086 printk(IBM_ERR
3087 "CMOS NVRAM (%u) and EC (%u) do not agree "
3088 "on display brightness level\n",
3089 (unsigned int) lcmos,
3090 (unsigned int) lec);
3091 return -EIO;
3092 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003093
3094 return level;
3095}
3096
3097static int brightness_set(int value)
3098{
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03003099 int cmos_cmd, inc, i, res;
3100 int current_value;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003101
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03003102 if (value > 7)
3103 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003104
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03003105 res = mutex_lock_interruptible(&brightness_mutex);
3106 if (res < 0)
3107 return res;
3108
3109 current_value = brightness_get(NULL);
3110 if (current_value < 0) {
3111 res = current_value;
3112 goto errout;
3113 }
3114
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03003115 cmos_cmd = value > current_value ?
3116 TP_CMOS_BRIGHTNESS_UP :
3117 TP_CMOS_BRIGHTNESS_DOWN;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003118 inc = value > current_value ? 1 : -1;
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03003119
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03003120 res = 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003121 for (i = current_value; i != value; i += inc) {
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03003122 if ((brightness_mode & 2) &&
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03003123 issue_thinkpad_cmos_command(cmos_cmd)) {
3124 res = -EIO;
3125 goto errout;
3126 }
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03003127 if ((brightness_mode & 1) &&
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03003128 !acpi_ec_write(brightness_offset, i + inc)) {
3129 res = -EIO;
3130 goto errout;;
3131 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003132 }
3133
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03003134errout:
3135 mutex_unlock(&brightness_mutex);
3136 return res;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003137}
3138
3139static int brightness_read(char *p)
3140{
3141 int len = 0;
3142 int level;
3143
3144 if ((level = brightness_get(NULL)) < 0) {
3145 len += sprintf(p + len, "level:\t\tunreadable\n");
3146 } else {
3147 len += sprintf(p + len, "level:\t\t%d\n", level & 0x7);
3148 len += sprintf(p + len, "commands:\tup, down\n");
3149 len += sprintf(p + len, "commands:\tlevel <level>"
3150 " (<level> is 0-7)\n");
3151 }
3152
3153 return len;
3154}
3155
3156static int brightness_write(char *buf)
3157{
3158 int level;
3159 int new_level;
3160 char *cmd;
3161
3162 while ((cmd = next_cmd(&buf))) {
3163 if ((level = brightness_get(NULL)) < 0)
3164 return level;
3165 level &= 7;
3166
3167 if (strlencmp(cmd, "up") == 0) {
3168 new_level = level == 7 ? 7 : level + 1;
3169 } else if (strlencmp(cmd, "down") == 0) {
3170 new_level = level == 0 ? 0 : level - 1;
3171 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
3172 new_level >= 0 && new_level <= 7) {
3173 /* new_level set */
3174 } else
3175 return -EINVAL;
3176
3177 brightness_set(new_level);
3178 }
3179
3180 return 0;
3181}
3182
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003183static struct ibm_struct brightness_driver_data = {
3184 .name = "brightness",
3185 .read = brightness_read,
3186 .write = brightness_write,
3187 .exit = brightness_exit,
3188};
3189
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003190/*************************************************************************
3191 * Volume subdriver
3192 */
3193
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003194static int volume_read(char *p)
3195{
3196 int len = 0;
3197 u8 level;
3198
3199 if (!acpi_ec_read(volume_offset, &level)) {
3200 len += sprintf(p + len, "level:\t\tunreadable\n");
3201 } else {
3202 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
3203 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
3204 len += sprintf(p + len, "commands:\tup, down, mute\n");
3205 len += sprintf(p + len, "commands:\tlevel <level>"
3206 " (<level> is 0-15)\n");
3207 }
3208
3209 return len;
3210}
3211
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003212static int volume_write(char *buf)
3213{
3214 int cmos_cmd, inc, i;
3215 u8 level, mute;
3216 int new_level, new_mute;
3217 char *cmd;
3218
3219 while ((cmd = next_cmd(&buf))) {
3220 if (!acpi_ec_read(volume_offset, &level))
3221 return -EIO;
3222 new_mute = mute = level & 0x40;
3223 new_level = level = level & 0xf;
3224
3225 if (strlencmp(cmd, "up") == 0) {
3226 if (mute)
3227 new_mute = 0;
3228 else
3229 new_level = level == 15 ? 15 : level + 1;
3230 } else if (strlencmp(cmd, "down") == 0) {
3231 if (mute)
3232 new_mute = 0;
3233 else
3234 new_level = level == 0 ? 0 : level - 1;
3235 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
3236 new_level >= 0 && new_level <= 15) {
3237 /* new_level set */
3238 } else if (strlencmp(cmd, "mute") == 0) {
3239 new_mute = 0x40;
3240 } else
3241 return -EINVAL;
3242
3243 if (new_level != level) { /* mute doesn't change */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003244 cmos_cmd = new_level > level ? TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003245 inc = new_level > level ? 1 : -1;
3246
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03003247 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003248 !acpi_ec_write(volume_offset, level)))
3249 return -EIO;
3250
3251 for (i = level; i != new_level; i += inc)
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03003252 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003253 !acpi_ec_write(volume_offset, i + inc))
3254 return -EIO;
3255
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03003256 if (mute && (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003257 !acpi_ec_write(volume_offset,
3258 new_level + mute)))
3259 return -EIO;
3260 }
3261
3262 if (new_mute != mute) { /* level doesn't change */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003263 cmos_cmd = new_mute ? TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003264
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03003265 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003266 !acpi_ec_write(volume_offset, level + new_mute))
3267 return -EIO;
3268 }
3269 }
3270
3271 return 0;
3272}
3273
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003274static struct ibm_struct volume_driver_data = {
3275 .name = "volume",
3276 .read = volume_read,
3277 .write = volume_write,
3278};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003279
3280/*************************************************************************
3281 * Fan subdriver
3282 */
3283
3284/*
3285 * FAN ACCESS MODES
3286 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003287 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003288 * ACPI GFAN method: returns fan level
3289 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003290 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03003291 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003292 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003293 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003294 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
3295 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03003296 * EC 0x2f (HFSP) might be available *for reading*, but do not use
3297 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003298 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003299 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03003300 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
3301 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003302 *
3303 * Fan speed changes of any sort (including those caused by the
3304 * disengaged mode) are usually done slowly by the firmware as the
3305 * maximum ammount of fan duty cycle change per second seems to be
3306 * limited.
3307 *
3308 * Reading is not available if GFAN exists.
3309 * Writing is not available if SFAN exists.
3310 *
3311 * Bits
3312 * 7 automatic mode engaged;
3313 * (default operation mode of the ThinkPad)
3314 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03003315 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003316 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03003317 * the tachometer while the fan controller ramps up
3318 * the speed (which can take up to a few *minutes*).
3319 * Speeds up fan to 100% duty-cycle, which is far above
3320 * the standard RPM levels. It is not impossible that
3321 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003322 * 5-3 unused in some models. Extra bits for fan level
3323 * in others, but still useless as all values above
3324 * 7 map to the same speed as level 7 in these models.
3325 * 2-0 fan level (0..7 usually)
3326 * 0x00 = stop
3327 * 0x07 = max (set when temperatures critical)
3328 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003329 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003330 *
3331 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
3332 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
3333 * does so, its initial value is meaningless (0x07).
3334 *
3335 * For firmware bugs, refer to:
3336 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
3337 *
3338 * ----
3339 *
3340 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
3341 * Main fan tachometer reading (in RPM)
3342 *
3343 * This register is present on all ThinkPads with a new-style EC, and
3344 * it is known not to be present on the A21m/e, and T22, as there is
3345 * something else in offset 0x84 according to the ACPI DSDT. Other
3346 * ThinkPads from this same time period (and earlier) probably lack the
3347 * tachometer as well.
3348 *
3349 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
3350 * was never fixed by IBM to report the EC firmware version string
3351 * probably support the tachometer (like the early X models), so
3352 * detecting it is quite hard. We need more data to know for sure.
3353 *
3354 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
3355 * might result.
3356 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03003357 * FIRMWARE BUG: may go stale while the EC is switching to full speed
3358 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003359 *
3360 * For firmware bugs, refer to:
3361 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
3362 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003363 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003364 * ThinkPad X31, X40, X41. Not available in the X60.
3365 *
3366 * FANS ACPI handle: takes three arguments: low speed, medium speed,
3367 * high speed. ACPI DSDT seems to map these three speeds to levels
3368 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
3369 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
3370 *
3371 * The speeds are stored on handles
3372 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
3373 *
3374 * There are three default speed sets, acessible as handles:
3375 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
3376 *
3377 * ACPI DSDT switches which set is in use depending on various
3378 * factors.
3379 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003380 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003381 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
3382 * but the ACPI tables just mention level 7.
3383 */
3384
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003385static enum fan_status_access_mode fan_status_access_mode;
3386static enum fan_control_access_mode fan_control_access_mode;
3387static enum fan_control_commands fan_control_commands;
3388
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02003389static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003390static u8 fan_control_desired_level;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02003391
Len Brown25c68a32006-12-08 04:43:41 -05003392static void fan_watchdog_fire(struct work_struct *ignored);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003393static int fan_watchdog_maxinterval;
Len Brown25c68a32006-12-08 04:43:41 -05003394static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003395
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003396IBM_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
3397IBM_HANDLE(gfan, ec, "GFAN", /* 570 */
3398 "\\FSPD", /* 600e/x, 770e, 770x */
3399 ); /* all others */
3400IBM_HANDLE(sfan, ec, "SFAN", /* 570 */
3401 "JFNS", /* 770x-JL */
3402 ); /* all others */
3403
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003404/*
3405 * SYSFS fan layout: hwmon compatible (device)
3406 *
3407 * pwm*_enable:
3408 * 0: "disengaged" mode
3409 * 1: manual mode
3410 * 2: native EC "auto" mode (recommended, hardware default)
3411 *
3412 * pwm*: set speed in manual mode, ignored otherwise.
3413 * 0 is level 0; 255 is level 7. Intermediate points done with linear
3414 * interpolation.
3415 *
3416 * fan*_input: tachometer reading, RPM
3417 *
3418 *
3419 * SYSFS fan layout: extensions
3420 *
3421 * fan_watchdog (driver):
3422 * fan watchdog interval in seconds, 0 disables (default), max 120
3423 */
3424
3425/* sysfs fan pwm1_enable ----------------------------------------------- */
3426static ssize_t fan_pwm1_enable_show(struct device *dev,
3427 struct device_attribute *attr,
3428 char *buf)
3429{
3430 int res, mode;
3431 u8 status;
3432
3433 res = fan_get_status_safe(&status);
3434 if (res)
3435 return res;
3436
3437 if (unlikely(tp_features.fan_ctrl_status_undef)) {
3438 if (status != fan_control_initial_status) {
3439 tp_features.fan_ctrl_status_undef = 0;
3440 } else {
3441 /* Return most likely status. In fact, it
3442 * might be the only possible status */
3443 status = TP_EC_FAN_AUTO;
3444 }
3445 }
3446
3447 if (status & TP_EC_FAN_FULLSPEED) {
3448 mode = 0;
3449 } else if (status & TP_EC_FAN_AUTO) {
3450 mode = 2;
3451 } else
3452 mode = 1;
3453
3454 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
3455}
3456
3457static ssize_t fan_pwm1_enable_store(struct device *dev,
3458 struct device_attribute *attr,
3459 const char *buf, size_t count)
3460{
3461 unsigned long t;
3462 int res, level;
3463
3464 if (parse_strtoul(buf, 2, &t))
3465 return -EINVAL;
3466
3467 switch (t) {
3468 case 0:
3469 level = TP_EC_FAN_FULLSPEED;
3470 break;
3471 case 1:
3472 level = TPACPI_FAN_LAST_LEVEL;
3473 break;
3474 case 2:
3475 level = TP_EC_FAN_AUTO;
3476 break;
3477 case 3:
3478 /* reserved for software-controlled auto mode */
3479 return -ENOSYS;
3480 default:
3481 return -EINVAL;
3482 }
3483
3484 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03003485 if (res == -ENXIO)
3486 return -EINVAL;
3487 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003488 return res;
3489
3490 fan_watchdog_reset();
3491
3492 return count;
3493}
3494
3495static struct device_attribute dev_attr_fan_pwm1_enable =
3496 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
3497 fan_pwm1_enable_show, fan_pwm1_enable_store);
3498
3499/* sysfs fan pwm1 ------------------------------------------------------ */
3500static ssize_t fan_pwm1_show(struct device *dev,
3501 struct device_attribute *attr,
3502 char *buf)
3503{
3504 int res;
3505 u8 status;
3506
3507 res = fan_get_status_safe(&status);
3508 if (res)
3509 return res;
3510
3511 if (unlikely(tp_features.fan_ctrl_status_undef)) {
3512 if (status != fan_control_initial_status) {
3513 tp_features.fan_ctrl_status_undef = 0;
3514 } else {
3515 status = TP_EC_FAN_AUTO;
3516 }
3517 }
3518
3519 if ((status &
3520 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
3521 status = fan_control_desired_level;
3522
3523 if (status > 7)
3524 status = 7;
3525
3526 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
3527}
3528
3529static ssize_t fan_pwm1_store(struct device *dev,
3530 struct device_attribute *attr,
3531 const char *buf, size_t count)
3532{
3533 unsigned long s;
3534 int rc;
3535 u8 status, newlevel;
3536
3537 if (parse_strtoul(buf, 255, &s))
3538 return -EINVAL;
3539
3540 /* scale down from 0-255 to 0-7 */
3541 newlevel = (s >> 5) & 0x07;
3542
3543 rc = mutex_lock_interruptible(&fan_mutex);
3544 if (rc < 0)
3545 return rc;
3546
3547 rc = fan_get_status(&status);
3548 if (!rc && (status &
3549 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
3550 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03003551 if (rc == -ENXIO)
3552 rc = -EINVAL;
3553 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003554 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03003555 fan_watchdog_reset();
3556 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003557 }
3558
3559 mutex_unlock(&fan_mutex);
3560 return (rc)? rc : count;
3561}
3562
3563static struct device_attribute dev_attr_fan_pwm1 =
3564 __ATTR(pwm1, S_IWUSR | S_IRUGO,
3565 fan_pwm1_show, fan_pwm1_store);
3566
3567/* sysfs fan fan1_input ------------------------------------------------ */
3568static ssize_t fan_fan1_input_show(struct device *dev,
3569 struct device_attribute *attr,
3570 char *buf)
3571{
3572 int res;
3573 unsigned int speed;
3574
3575 res = fan_get_speed(&speed);
3576 if (res < 0)
3577 return res;
3578
3579 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
3580}
3581
3582static struct device_attribute dev_attr_fan_fan1_input =
3583 __ATTR(fan1_input, S_IRUGO,
3584 fan_fan1_input_show, NULL);
3585
3586/* sysfs fan fan_watchdog (driver) ------------------------------------- */
3587static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
3588 char *buf)
3589{
3590 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
3591}
3592
3593static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
3594 const char *buf, size_t count)
3595{
3596 unsigned long t;
3597
3598 if (parse_strtoul(buf, 120, &t))
3599 return -EINVAL;
3600
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03003601 if (!fan_control_allowed)
3602 return -EPERM;
3603
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003604 fan_watchdog_maxinterval = t;
3605 fan_watchdog_reset();
3606
3607 return count;
3608}
3609
3610static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
3611 fan_fan_watchdog_show, fan_fan_watchdog_store);
3612
3613/* --------------------------------------------------------------------- */
3614static struct attribute *fan_attributes[] = {
3615 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
3616 &dev_attr_fan_fan1_input.attr,
3617 NULL
3618};
3619
3620static const struct attribute_group fan_attr_group = {
3621 .attrs = fan_attributes,
3622};
3623
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003624static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003625{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003626 int rc;
3627
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003628 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
3629
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003630 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003631 fan_status_access_mode = TPACPI_FAN_NONE;
3632 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003633 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003634 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003635 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003636 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003637
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003638 IBM_ACPIHANDLE_INIT(fans);
3639 IBM_ACPIHANDLE_INIT(gfan);
3640 IBM_ACPIHANDLE_INIT(sfan);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003641
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003642 if (gfan_handle) {
3643 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003644 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003645 } else {
3646 /* all other ThinkPads: note that even old-style
3647 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02003648 if (likely(acpi_ec_read(fan_status_offset,
3649 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003650 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02003651
3652 /* In some ThinkPads, neither the EC nor the ACPI
3653 * DSDT initialize the fan status, and it ends up
3654 * being set to 0x07 when it *could* be either
3655 * 0x07 or 0x80.
3656 *
3657 * Enable for TP-1Y (T43), TP-78 (R51e),
3658 * TP-76 (R52), TP-70 (T43, R52), which are known
3659 * to be buggy. */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03003660 if (fan_control_initial_status == 0x07) {
3661 switch (thinkpad_id.ec_model) {
3662 case 0x5931: /* TP-1Y */
3663 case 0x3837: /* TP-78 */
3664 case 0x3637: /* TP-76 */
3665 case 0x3037: /* TP-70 */
3666 printk(IBM_NOTICE
3667 "fan_init: initial fan status is "
3668 "unknown, assuming it is in auto "
3669 "mode\n");
3670 tp_features.fan_ctrl_status_undef = 1;
3671 ;;
3672 }
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02003673 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003674 } else {
3675 printk(IBM_ERR
3676 "ThinkPad ACPI EC access misbehaving, "
3677 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003678 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003679 }
3680 }
3681
3682 if (sfan_handle) {
3683 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003684 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02003685 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003686 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003687 } else {
3688 if (!gfan_handle) {
3689 /* gfan without sfan means no fan control */
3690 /* all other models implement TP EC 0x2f control */
3691
3692 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02003693 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003694 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003695 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003696 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003697 TPACPI_FAN_CMD_SPEED |
3698 TPACPI_FAN_CMD_LEVEL |
3699 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003700 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003701 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02003702 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003703 TPACPI_FAN_CMD_LEVEL |
3704 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003705 }
3706 }
3707 }
3708
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003709 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
3710 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
3711 fan_control_access_mode != TPACPI_FAN_WR_NONE),
3712 fan_status_access_mode, fan_control_access_mode);
3713
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03003714 /* fan control master switch */
3715 if (!fan_control_allowed) {
3716 fan_control_access_mode = TPACPI_FAN_WR_NONE;
3717 fan_control_commands = 0;
3718 dbg_printk(TPACPI_DBG_INIT,
3719 "fan control features disabled by parameter\n");
3720 }
3721
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003722 /* update fan_control_desired_level */
3723 if (fan_status_access_mode != TPACPI_FAN_NONE)
3724 fan_get_status_safe(NULL);
3725
3726 if (fan_status_access_mode != TPACPI_FAN_NONE ||
3727 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
3728 rc = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3729 &fan_attr_group);
3730 if (!(rc < 0))
3731 rc = driver_create_file(&tpacpi_pdriver.driver,
3732 &driver_attr_fan_watchdog);
3733 if (rc < 0)
3734 return rc;
3735 return 0;
3736 } else
3737 return 1;
3738}
3739
3740/*
3741 * Call with fan_mutex held
3742 */
3743static void fan_update_desired_level(u8 status)
3744{
3745 if ((status &
3746 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
3747 if (status > 7)
3748 fan_control_desired_level = 7;
3749 else
3750 fan_control_desired_level = status;
3751 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003752}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003753
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02003754static int fan_get_status(u8 *status)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003755{
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02003756 u8 s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003757
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02003758 /* TODO:
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003759 * Add TPACPI_FAN_RD_ACPI_FANS ? */
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02003760
Henrique de Moraes Holschuh3ef8a602006-11-24 11:47:10 -02003761 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003762 case TPACPI_FAN_RD_ACPI_GFAN:
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003763 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02003764
3765 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003766 return -EIO;
3767
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02003768 if (likely(status))
3769 *status = s & 0x07;
3770
3771 break;
3772
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003773 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02003774 /* all except 570, 600e/x, 770e, 770x */
3775 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
3776 return -EIO;
3777
3778 if (likely(status))
3779 *status = s;
3780
3781 break;
3782
3783 default:
3784 return -ENXIO;
3785 }
3786
3787 return 0;
3788}
3789
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003790static int fan_get_status_safe(u8 *status)
3791{
3792 int rc;
3793 u8 s;
3794
3795 rc = mutex_lock_interruptible(&fan_mutex);
3796 if (rc < 0)
3797 return rc;
3798 rc = fan_get_status(&s);
3799 if (!rc)
3800 fan_update_desired_level(s);
3801 mutex_unlock(&fan_mutex);
3802
3803 if (status)
3804 *status = s;
3805
3806 return rc;
3807}
3808
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003809static void fan_exit(void)
3810{
Henrique de Moraes Holschuh99fba3f2007-04-21 11:08:44 -03003811 vdbg_printk(TPACPI_DBG_EXIT, "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003812
3813 /* FIXME: can we really do this unconditionally? */
3814 sysfs_remove_group(&tpacpi_pdev->dev.kobj, &fan_attr_group);
3815 driver_remove_file(&tpacpi_pdriver.driver, &driver_attr_fan_watchdog);
3816
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003817 cancel_delayed_work(&fan_watchdog_task);
3818 flush_scheduled_work();
3819}
3820
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02003821static int fan_get_speed(unsigned int *speed)
3822{
3823 u8 hi, lo;
3824
3825 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003826 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02003827 /* all except 570, 600e/x, 770e, 770x */
3828 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
3829 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
3830 return -EIO;
3831
3832 if (likely(speed))
3833 *speed = (hi << 8) | lo;
3834
3835 break;
3836
3837 default:
3838 return -ENXIO;
3839 }
3840
3841 return 0;
3842}
3843
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003844static void fan_watchdog_fire(struct work_struct *ignored)
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003845{
Henrique de Moraes Holschuh99fba3f2007-04-21 11:08:44 -03003846 int rc;
3847
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003848 printk(IBM_NOTICE "fan watchdog: enabling fan\n");
Henrique de Moraes Holschuh99fba3f2007-04-21 11:08:44 -03003849 rc = fan_set_enable();
3850 if (rc < 0) {
3851 printk(IBM_ERR "fan watchdog: error %d while enabling fan, "
3852 "will try again later...\n", -rc);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003853 /* reschedule for later */
3854 fan_watchdog_reset();
3855 }
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003856}
3857
3858static void fan_watchdog_reset(void)
3859{
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03003860 static int fan_watchdog_active;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003861
Henrique de Moraes Holschuh4985cd02007-04-27 22:00:10 -03003862 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
3863 return;
3864
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003865 if (fan_watchdog_active)
3866 cancel_delayed_work(&fan_watchdog_task);
3867
3868 if (fan_watchdog_maxinterval > 0) {
3869 fan_watchdog_active = 1;
3870 if (!schedule_delayed_work(&fan_watchdog_task,
3871 msecs_to_jiffies(fan_watchdog_maxinterval
3872 * 1000))) {
3873 printk(IBM_ERR "failed to schedule the fan watchdog, "
3874 "watchdog will not trigger\n");
3875 }
3876 } else
3877 fan_watchdog_active = 0;
3878}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003879
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003880static int fan_set_level(int level)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003881{
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03003882 if (!fan_control_allowed)
3883 return -EPERM;
3884
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003885 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003886 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003887 if (level >= 0 && level <= 7) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003888 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003889 return -EIO;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003890 } else
3891 return -EINVAL;
3892 break;
3893
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003894 case TPACPI_FAN_WR_ACPI_FANS:
3895 case TPACPI_FAN_WR_TPEC:
3896 if ((level != TP_EC_FAN_AUTO) &&
3897 (level != TP_EC_FAN_FULLSPEED) &&
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02003898 ((level < 0) || (level > 7)))
3899 return -EINVAL;
3900
Henrique de Moraes Holschuheaa75712007-04-24 11:48:18 -03003901 /* safety net should the EC not support AUTO
3902 * or FULLSPEED mode bits and just ignore them */
3903 if (level & TP_EC_FAN_FULLSPEED)
3904 level |= 7; /* safety min speed 7 */
3905 else if (level & TP_EC_FAN_FULLSPEED)
3906 level |= 4; /* safety min speed 4 */
3907
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003908 if (!acpi_ec_write(fan_status_offset, level))
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02003909 return -EIO;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02003910 else
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003911 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02003912 break;
3913
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003914 default:
3915 return -ENXIO;
3916 }
3917 return 0;
3918}
3919
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003920static int fan_set_level_safe(int level)
3921{
3922 int rc;
3923
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03003924 if (!fan_control_allowed)
3925 return -EPERM;
3926
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003927 rc = mutex_lock_interruptible(&fan_mutex);
3928 if (rc < 0)
3929 return rc;
3930
3931 if (level == TPACPI_FAN_LAST_LEVEL)
3932 level = fan_control_desired_level;
3933
3934 rc = fan_set_level(level);
3935 if (!rc)
3936 fan_update_desired_level(level);
3937
3938 mutex_unlock(&fan_mutex);
3939 return rc;
3940}
3941
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003942static int fan_set_enable(void)
3943{
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02003944 u8 s;
3945 int rc;
3946
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03003947 if (!fan_control_allowed)
3948 return -EPERM;
3949
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003950 rc = mutex_lock_interruptible(&fan_mutex);
3951 if (rc < 0)
3952 return rc;
3953
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003954 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003955 case TPACPI_FAN_WR_ACPI_FANS:
3956 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003957 rc = fan_get_status(&s);
3958 if (rc < 0)
3959 break;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02003960
3961 /* Don't go out of emergency fan mode */
Henrique de Moraes Holschuheaa75712007-04-24 11:48:18 -03003962 if (s != 7) {
3963 s &= 0x07;
3964 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
3965 }
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02003966
3967 if (!acpi_ec_write(fan_status_offset, s))
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003968 rc = -EIO;
3969 else {
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003970 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003971 rc = 0;
3972 }
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02003973 break;
3974
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003975 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003976 rc = fan_get_status(&s);
3977 if (rc < 0)
3978 break;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02003979
3980 s &= 0x07;
3981
3982 /* Set fan to at least level 4 */
Henrique de Moraes Holschuheaa75712007-04-24 11:48:18 -03003983 s |= 4;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02003984
3985 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003986 rc= -EIO;
3987 else
3988 rc = 0;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003989 break;
3990
3991 default:
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003992 rc = -ENXIO;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003993 }
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003994
3995 mutex_unlock(&fan_mutex);
3996 return rc;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003997}
3998
3999static int fan_set_disable(void)
4000{
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004001 int rc;
4002
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03004003 if (!fan_control_allowed)
4004 return -EPERM;
4005
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004006 rc = mutex_lock_interruptible(&fan_mutex);
4007 if (rc < 0)
4008 return rc;
4009
4010 rc = 0;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004011 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004012 case TPACPI_FAN_WR_ACPI_FANS:
4013 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004014 if (!acpi_ec_write(fan_status_offset, 0x00))
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004015 rc = -EIO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004016 else {
4017 fan_control_desired_level = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004018 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004019 }
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004020 break;
4021
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004022 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02004023 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004024 rc = -EIO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004025 else
4026 fan_control_desired_level = 0;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02004027 break;
4028
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004029 default:
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004030 rc = -ENXIO;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004031 }
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004032
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004033
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004034 mutex_unlock(&fan_mutex);
4035 return rc;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004036}
4037
4038static int fan_set_speed(int speed)
4039{
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004040 int rc;
4041
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03004042 if (!fan_control_allowed)
4043 return -EPERM;
4044
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004045 rc = mutex_lock_interruptible(&fan_mutex);
4046 if (rc < 0)
4047 return rc;
4048
4049 rc = 0;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004050 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004051 case TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004052 if (speed >= 0 && speed <= 65535) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004053 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
4054 speed, speed, speed))
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004055 rc = -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004056 } else
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004057 rc = -EINVAL;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004058 break;
4059
4060 default:
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004061 rc = -ENXIO;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004062 }
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004063
4064 mutex_unlock(&fan_mutex);
4065 return rc;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004066}
4067
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004068static int fan_read(char *p)
4069{
4070 int len = 0;
4071 int rc;
4072 u8 status;
4073 unsigned int speed = 0;
4074
4075 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004076 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004077 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004078 if ((rc = fan_get_status_safe(&status)) < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004079 return rc;
4080
4081 len += sprintf(p + len, "status:\t\t%s\n"
4082 "level:\t\t%d\n",
4083 (status != 0) ? "enabled" : "disabled", status);
4084 break;
4085
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004086 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004087 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004088 if ((rc = fan_get_status_safe(&status)) < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004089 return rc;
4090
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004091 if (unlikely(tp_features.fan_ctrl_status_undef)) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004092 if (status != fan_control_initial_status)
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004093 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004094 else
4095 /* Return most likely status. In fact, it
4096 * might be the only possible status */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004097 status = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004098 }
4099
4100 len += sprintf(p + len, "status:\t\t%s\n",
4101 (status != 0) ? "enabled" : "disabled");
4102
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004103 if ((rc = fan_get_speed(&speed)) < 0)
4104 return rc;
4105
4106 len += sprintf(p + len, "speed:\t\t%d\n", speed);
4107
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004108 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004109 /* Disengaged mode takes precedence */
4110 len += sprintf(p + len, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004111 else if (status & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004112 len += sprintf(p + len, "level:\t\tauto\n");
4113 else
4114 len += sprintf(p + len, "level:\t\t%d\n", status);
4115 break;
4116
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004117 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004118 default:
4119 len += sprintf(p + len, "status:\t\tnot supported\n");
4120 }
4121
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004122 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004123 len += sprintf(p + len, "commands:\tlevel <level>");
4124
4125 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004126 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004127 len += sprintf(p + len, " (<level> is 0-7)\n");
4128 break;
4129
4130 default:
4131 len += sprintf(p + len, " (<level> is 0-7, "
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004132 "auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004133 break;
4134 }
4135 }
4136
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004137 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004138 len += sprintf(p + len, "commands:\tenable, disable\n"
4139 "commands:\twatchdog <timeout> (<timeout> is 0 (off), "
4140 "1-120 (seconds))\n");
4141
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004142 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004143 len += sprintf(p + len, "commands:\tspeed <speed>"
4144 " (<speed> is 0-65535)\n");
4145
4146 return len;
4147}
4148
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004149static int fan_write_cmd_level(const char *cmd, int *rc)
4150{
4151 int level;
4152
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02004153 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004154 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004155 else if ((strlencmp(cmd, "level disengaged") == 0) |
4156 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004157 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02004158 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004159 return 0;
4160
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004161 if ((*rc = fan_set_level_safe(level)) == -ENXIO)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004162 printk(IBM_ERR "level command accepted for unsupported "
4163 "access mode %d", fan_control_access_mode);
4164
4165 return 1;
4166}
4167
4168static int fan_write_cmd_enable(const char *cmd, int *rc)
4169{
4170 if (strlencmp(cmd, "enable") != 0)
4171 return 0;
4172
4173 if ((*rc = fan_set_enable()) == -ENXIO)
4174 printk(IBM_ERR "enable command accepted for unsupported "
4175 "access mode %d", fan_control_access_mode);
4176
4177 return 1;
4178}
4179
4180static int fan_write_cmd_disable(const char *cmd, int *rc)
4181{
4182 if (strlencmp(cmd, "disable") != 0)
4183 return 0;
4184
4185 if ((*rc = fan_set_disable()) == -ENXIO)
4186 printk(IBM_ERR "disable command accepted for unsupported "
4187 "access mode %d", fan_control_access_mode);
4188
4189 return 1;
4190}
4191
4192static int fan_write_cmd_speed(const char *cmd, int *rc)
4193{
4194 int speed;
4195
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02004196 /* TODO:
4197 * Support speed <low> <medium> <high> ? */
4198
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004199 if (sscanf(cmd, "speed %d", &speed) != 1)
4200 return 0;
4201
4202 if ((*rc = fan_set_speed(speed)) == -ENXIO)
4203 printk(IBM_ERR "speed command accepted for unsupported "
4204 "access mode %d", fan_control_access_mode);
4205
4206 return 1;
4207}
4208
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02004209static int fan_write_cmd_watchdog(const char *cmd, int *rc)
4210{
4211 int interval;
4212
4213 if (sscanf(cmd, "watchdog %d", &interval) != 1)
4214 return 0;
4215
4216 if (interval < 0 || interval > 120)
4217 *rc = -EINVAL;
4218 else
4219 fan_watchdog_maxinterval = interval;
4220
4221 return 1;
4222}
4223
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004224static int fan_write(char *buf)
4225{
4226 char *cmd;
4227 int rc = 0;
4228
4229 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004230 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004231 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004232 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004233 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02004234 fan_write_cmd_disable(cmd, &rc) ||
4235 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004236 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004237 fan_write_cmd_speed(cmd, &rc))
4238 )
4239 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02004240 else if (!rc)
4241 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004242 }
4243
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004244 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004245}
4246
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004247static struct ibm_struct fan_driver_data = {
4248 .name = "fan",
4249 .read = fan_read,
4250 .write = fan_write,
4251 .exit = fan_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004252};
4253
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004254/****************************************************************************
4255 ****************************************************************************
4256 *
4257 * Infrastructure
4258 *
4259 ****************************************************************************
4260 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004262/* /proc support */
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03004263static struct proc_dir_entry *proc_dir;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004264
4265/* Subdriver registry */
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004266static LIST_HEAD(tpacpi_all_drivers);
4267
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004269/*
4270 * Module and infrastructure proble, init and exit handling
4271 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004273#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004274static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004275{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004276 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004277
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004278 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004279}
4280#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
4281
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004282static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283{
4284 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004285 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286 struct proc_dir_entry *entry;
4287
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004288 BUG_ON(ibm == NULL);
4289
4290 INIT_LIST_HEAD(&ibm->all_drivers);
4291
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004292 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293 return 0;
4294
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004295 dbg_printk(TPACPI_DBG_INIT,
4296 "probing for %s\n", ibm->name);
4297
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004298 if (iibm->init) {
4299 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004300 if (ret > 0)
4301 return 0; /* probe failed */
4302 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004304
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004305 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306 }
4307
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004308 if (ibm->acpi) {
4309 if (ibm->acpi->hid) {
4310 ret = register_tpacpi_subdriver(ibm);
4311 if (ret)
4312 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004313 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004314
4315 if (ibm->acpi->notify) {
4316 ret = setup_acpi_notify(ibm);
4317 if (ret == -ENODEV) {
4318 printk(IBM_NOTICE "disabling subdriver %s\n",
4319 ibm->name);
4320 ret = 0;
4321 goto err_out;
4322 }
4323 if (ret < 0)
4324 goto err_out;
4325 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004326 }
4327
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004328 dbg_printk(TPACPI_DBG_INIT,
4329 "%s installed\n", ibm->name);
4330
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004331 if (ibm->read) {
4332 entry = create_proc_entry(ibm->name,
4333 S_IFREG | S_IRUGO | S_IWUSR,
4334 proc_dir);
4335 if (!entry) {
4336 printk(IBM_ERR "unable to create proc entry %s\n",
4337 ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004338 ret = -ENODEV;
4339 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004340 }
4341 entry->owner = THIS_MODULE;
4342 entry->data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004343 entry->read_proc = &dispatch_procfs_read;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004344 if (ibm->write)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004345 entry->write_proc = &dispatch_procfs_write;
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004346 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004347 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004349 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
4350
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004352
4353err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004354 dbg_printk(TPACPI_DBG_INIT,
4355 "%s: at error exit path with result %d\n",
4356 ibm->name, ret);
4357
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004358 ibm_exit(ibm);
4359 return (ret < 0)? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360}
4361
4362static void ibm_exit(struct ibm_struct *ibm)
4363{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004364 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004365
4366 list_del_init(&ibm->all_drivers);
4367
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004368 if (ibm->flags.acpi_notify_installed) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004369 dbg_printk(TPACPI_DBG_EXIT,
4370 "%s: acpi_remove_notify_handler\n", ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004371 BUG_ON(!ibm->acpi);
4372 acpi_remove_notify_handler(*ibm->acpi->handle,
4373 ibm->acpi->type,
4374 dispatch_acpi_notify);
4375 ibm->flags.acpi_notify_installed = 0;
4376 ibm->flags.acpi_notify_installed = 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004377 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004379 if (ibm->flags.proc_created) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004380 dbg_printk(TPACPI_DBG_EXIT,
4381 "%s: remove_proc_entry\n", ibm->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382 remove_proc_entry(ibm->name, proc_dir);
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004383 ibm->flags.proc_created = 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004384 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004386 if (ibm->flags.acpi_driver_registered) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004387 dbg_printk(TPACPI_DBG_EXIT,
4388 "%s: acpi_bus_unregister_driver\n", ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004389 BUG_ON(!ibm->acpi);
4390 acpi_bus_unregister_driver(ibm->acpi->driver);
4391 kfree(ibm->acpi->driver);
4392 ibm->acpi->driver = NULL;
4393 ibm->flags.acpi_driver_registered = 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004394 }
4395
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004396 if (ibm->flags.init_called && ibm->exit) {
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004397 ibm->exit();
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004398 ibm->flags.init_called = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004400
4401 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402}
4403
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004404/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03004406static void __init get_thinkpad_model_data(struct thinkpad_id_data *tp)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004407{
4408 struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02004409 char ec_fw_string[18];
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004410
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03004411 if (!tp)
4412 return;
4413
4414 memset(tp, 0, sizeof(*tp));
4415
4416 if (dmi_name_in_vendors("IBM"))
4417 tp->vendor = PCI_VENDOR_ID_IBM;
4418 else if (dmi_name_in_vendors("LENOVO"))
4419 tp->vendor = PCI_VENDOR_ID_LENOVO;
4420 else
4421 return;
4422
4423 tp->bios_version_str = kstrdup(dmi_get_system_info(DMI_BIOS_VERSION),
4424 GFP_KERNEL);
4425 if (!tp->bios_version_str)
4426 return;
4427 tp->bios_model = tp->bios_version_str[0]
4428 | (tp->bios_version_str[1] << 8);
4429
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004430 /*
4431 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
4432 * X32 or newer, all Z series; Some models must have an
4433 * up-to-date BIOS or they will not be detected.
4434 *
4435 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
4436 */
4437 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02004438 if (sscanf(dev->name,
4439 "IBM ThinkPad Embedded Controller -[%17c",
4440 ec_fw_string) == 1) {
4441 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
4442 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03004443
4444 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
4445 tp->ec_model = ec_fw_string[0]
4446 | (ec_fw_string[1] << 8);
4447 break;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02004448 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004449 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03004450
4451 tp->model_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_VERSION),
4452 GFP_KERNEL);
4453 if (strnicmp(tp->model_str, "ThinkPad", 8) != 0) {
4454 kfree(tp->model_str);
4455 tp->model_str = NULL;
4456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457}
4458
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004459static int __init probe_for_thinkpad(void)
4460{
4461 int is_thinkpad;
4462
4463 if (acpi_disabled)
4464 return -ENODEV;
4465
4466 /*
4467 * Non-ancient models have better DMI tagging, but very old models
4468 * don't.
4469 */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03004470 is_thinkpad = (thinkpad_id.model_str != NULL);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004471
4472 /* ec is required because many other handles are relative to it */
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004473 IBM_ACPIHANDLE_INIT(ec);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004474 if (!ec_handle) {
4475 if (is_thinkpad)
4476 printk(IBM_ERR
4477 "Not yet supported ThinkPad detected!\n");
4478 return -ENODEV;
4479 }
4480
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03004481 /*
4482 * Risks a regression on very old machines, but reduces potential
4483 * false positives a damn great deal
4484 */
4485 if (!is_thinkpad)
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03004486 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03004487
4488 if (!is_thinkpad && !force_load)
4489 return -ENODEV;
4490
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004491 return 0;
4492}
4493
4494
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004495/* Module init, exit, parameters */
4496
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004497static struct ibm_init_struct ibms_init[] __initdata = {
4498 {
4499 .init = thinkpad_acpi_driver_init,
4500 .data = &thinkpad_acpi_driver_data,
4501 },
4502 {
4503 .init = hotkey_init,
4504 .data = &hotkey_driver_data,
4505 },
4506 {
4507 .init = bluetooth_init,
4508 .data = &bluetooth_driver_data,
4509 },
4510 {
4511 .init = wan_init,
4512 .data = &wan_driver_data,
4513 },
4514 {
4515 .init = video_init,
4516 .data = &video_driver_data,
4517 },
4518 {
4519 .init = light_init,
4520 .data = &light_driver_data,
4521 },
4522#ifdef CONFIG_THINKPAD_ACPI_DOCK
4523 {
4524 .init = dock_init,
4525 .data = &dock_driver_data[0],
4526 },
4527 {
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03004528 .init = dock_init2,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004529 .data = &dock_driver_data[1],
4530 },
4531#endif
4532#ifdef CONFIG_THINKPAD_ACPI_BAY
4533 {
4534 .init = bay_init,
4535 .data = &bay_driver_data,
4536 },
4537#endif
4538 {
4539 .init = cmos_init,
4540 .data = &cmos_driver_data,
4541 },
4542 {
4543 .init = led_init,
4544 .data = &led_driver_data,
4545 },
4546 {
4547 .init = beep_init,
4548 .data = &beep_driver_data,
4549 },
4550 {
4551 .init = thermal_init,
4552 .data = &thermal_driver_data,
4553 },
4554 {
4555 .data = &ecdump_driver_data,
4556 },
4557 {
4558 .init = brightness_init,
4559 .data = &brightness_driver_data,
4560 },
4561 {
4562 .data = &volume_driver_data,
4563 },
4564 {
4565 .init = fan_init,
4566 .data = &fan_driver_data,
4567 },
4568};
4569
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004570static int __init set_ibm_param(const char *val, struct kernel_param *kp)
4571{
4572 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004573 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004574
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004575 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
4576 ibm = ibms_init[i].data;
4577 BUG_ON(ibm == NULL);
4578
4579 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
4580 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004581 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004582 strcpy(ibms_init[i].param, val);
4583 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004584 return 0;
4585 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004586 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004587
4588 return -EINVAL;
4589}
4590
4591static int experimental;
4592module_param(experimental, int, 0);
4593
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03004594static u32 dbg_level;
4595module_param_named(debug, dbg_level, uint, 0);
4596
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03004597static int force_load;
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03004598module_param(force_load, bool, 0);
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03004599
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03004600static int fan_control_allowed;
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03004601module_param_named(fan_control, fan_control_allowed, bool, 0);
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03004602
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03004603static int brightness_mode;
4604module_param_named(brightness_mode, brightness_mode, int, 0);
4605
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004606#define IBM_PARAM(feature) \
4607 module_param_call(feature, set_ibm_param, NULL, NULL, 0)
4608
4609IBM_PARAM(hotkey);
4610IBM_PARAM(bluetooth);
4611IBM_PARAM(video);
4612IBM_PARAM(light);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03004613#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004614IBM_PARAM(dock);
4615#endif
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03004616#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004617IBM_PARAM(bay);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03004618#endif /* CONFIG_THINKPAD_ACPI_BAY */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004619IBM_PARAM(cmos);
4620IBM_PARAM(led);
4621IBM_PARAM(beep);
4622IBM_PARAM(ecdump);
4623IBM_PARAM(brightness);
4624IBM_PARAM(volume);
4625IBM_PARAM(fan);
4626
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03004627static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628{
4629 int ret, i;
4630
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03004631 /* Driver-level probe */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03004632
4633 get_thinkpad_model_data(&thinkpad_id);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004634 ret = probe_for_thinkpad();
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03004635 if (ret) {
4636 thinkpad_acpi_module_exit();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004637 return ret;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03004638 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03004640 /* Driver initialization */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03004641
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004642 IBM_ACPIHANDLE_INIT(ecrd);
4643 IBM_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004645 proc_dir = proc_mkdir(IBM_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646 if (!proc_dir) {
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004647 printk(IBM_ERR "unable to create proc dir " IBM_PROC_DIR);
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03004648 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649 return -ENODEV;
4650 }
4651 proc_dir->owner = THIS_MODULE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004652
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03004653 ret = platform_driver_register(&tpacpi_pdriver);
4654 if (ret) {
4655 printk(IBM_ERR "unable to register platform driver\n");
4656 thinkpad_acpi_module_exit();
4657 return ret;
4658 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03004659 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
4660 if (ret) {
4661 printk(IBM_ERR "unable to create sysfs driver attributes\n");
4662 thinkpad_acpi_module_exit();
4663 return ret;
4664 }
4665
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03004666
4667 /* Device initialization */
4668 tpacpi_pdev = platform_device_register_simple(IBM_DRVR_NAME, -1,
4669 NULL, 0);
4670 if (IS_ERR(tpacpi_pdev)) {
4671 ret = PTR_ERR(tpacpi_pdev);
4672 tpacpi_pdev = NULL;
4673 printk(IBM_ERR "unable to register platform device\n");
4674 thinkpad_acpi_module_exit();
4675 return ret;
4676 }
4677 tpacpi_hwmon = hwmon_device_register(&tpacpi_pdev->dev);
4678 if (IS_ERR(tpacpi_hwmon)) {
4679 ret = PTR_ERR(tpacpi_hwmon);
4680 tpacpi_hwmon = NULL;
4681 printk(IBM_ERR "unable to register hwmon device\n");
4682 thinkpad_acpi_module_exit();
4683 return ret;
4684 }
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03004685 tpacpi_inputdev = input_allocate_device();
4686 if (!tpacpi_inputdev) {
4687 printk(IBM_ERR "unable to allocate input device\n");
4688 thinkpad_acpi_module_exit();
4689 return -ENOMEM;
4690 } else {
4691 /* Prepare input device, but don't register */
4692 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
4693 tpacpi_inputdev->phys = IBM_DRVR_NAME "/input0";
4694 tpacpi_inputdev->id.bustype = BUS_HOST;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03004695 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
4696 thinkpad_id.vendor :
4697 PCI_VENDOR_ID_IBM;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03004698 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
4699 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
4700 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004701 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
4702 ret = ibm_init(&ibms_init[i]);
4703 if (ret >= 0 && *ibms_init[i].param)
4704 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03004706 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707 return ret;
4708 }
4709 }
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03004710 ret = input_register_device(tpacpi_inputdev);
4711 if (ret < 0) {
4712 printk(IBM_ERR "unable to register input device\n");
4713 thinkpad_acpi_module_exit();
4714 return ret;
4715 } else {
4716 tp_features.input_device_registered = 1;
4717 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718
4719 return 0;
4720}
4721
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03004722static void thinkpad_acpi_module_exit(void)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004723{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004724 struct ibm_struct *ibm, *itmp;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004725
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004726 list_for_each_entry_safe_reverse(ibm, itmp,
4727 &tpacpi_all_drivers,
4728 all_drivers) {
4729 ibm_exit(ibm);
4730 }
4731
4732 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004733
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03004734 if (tpacpi_inputdev) {
4735 if (tp_features.input_device_registered)
4736 input_unregister_device(tpacpi_inputdev);
4737 else
4738 input_free_device(tpacpi_inputdev);
4739 }
4740
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03004741 if (tpacpi_hwmon)
4742 hwmon_device_unregister(tpacpi_hwmon);
4743
4744 if (tpacpi_pdev)
4745 platform_device_unregister(tpacpi_pdev);
4746
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03004747 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03004748 platform_driver_unregister(&tpacpi_pdriver);
4749
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004750 if (proc_dir)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004751 remove_proc_entry(IBM_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004752
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03004753 kfree(thinkpad_id.bios_version_str);
4754 kfree(thinkpad_id.ec_version_str);
4755 kfree(thinkpad_id.model_str);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004756}
4757
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03004758module_init(thinkpad_acpi_module_init);
4759module_exit(thinkpad_acpi_module_exit);