blob: 2155139793b1b9777c4cf71cf0ea3441b2d9e0d6 [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 Holschuh3b0c6482007-09-04 11:13:16 -030024#define IBM_VERSION "0.16"
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 Holschuh8fef5022007-09-23 11:39:02 -0300120static enum {
121 TPACPI_LIFE_INIT = 0,
122 TPACPI_LIFE_RUNNING,
123 TPACPI_LIFE_EXITING,
124} tpacpi_lifecycle;
125
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300126/****************************************************************************
127 ****************************************************************************
128 *
129 * ACPI Helpers and device model
130 *
131 ****************************************************************************
132 ****************************************************************************/
133
134/*************************************************************************
135 * ACPI basic handles
136 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -0300138static acpi_handle root_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140#define IBM_HANDLE(object, parent, paths...) \
141 static acpi_handle object##_handle; \
142 static acpi_handle *object##_parent = &parent##_handle; \
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400143 static char *object##_path; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 static char *object##_paths[] = { paths }
145
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400146IBM_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
147 "\\_SB.PCI.ISA.EC", /* 570 */
148 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
149 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
150 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
151 "\\_SB.PCI0.ICH3.EC0", /* R31 */
152 "\\_SB.PCI0.LPC.EC", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300153 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300155IBM_HANDLE(ecrd, ec, "ECRD"); /* 570 */
156IBM_HANDLE(ecwr, ec, "ECWR"); /* 570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300158
159/*************************************************************************
160 * Misc ACPI handles
161 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400163IBM_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, T4x, X31, X40 */
164 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
165 "\\CMS", /* R40, R40e */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300166 ); /* all others */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400167
168IBM_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
169 "^HKEY", /* R30, R31 */
170 "HKEY", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300171 ); /* 570 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400172
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400173
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300174/*************************************************************************
175 * ACPI helpers
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200176 */
177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178static int acpi_evalf(acpi_handle handle,
179 void *res, char *method, char *fmt, ...)
180{
181 char *fmt0 = fmt;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400182 struct acpi_object_list params;
183 union acpi_object in_objs[IBM_MAX_ACPI_ARGS];
184 struct acpi_buffer result, *resultp;
185 union acpi_object out_obj;
186 acpi_status status;
187 va_list ap;
188 char res_type;
189 int success;
190 int quiet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192 if (!*fmt) {
193 printk(IBM_ERR "acpi_evalf() called with empty format\n");
194 return 0;
195 }
196
197 if (*fmt == 'q') {
198 quiet = 1;
199 fmt++;
200 } else
201 quiet = 0;
202
203 res_type = *(fmt++);
204
205 params.count = 0;
206 params.pointer = &in_objs[0];
207
208 va_start(ap, fmt);
209 while (*fmt) {
210 char c = *(fmt++);
211 switch (c) {
212 case 'd': /* int */
213 in_objs[params.count].integer.value = va_arg(ap, int);
214 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
215 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400216 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 default:
218 printk(IBM_ERR "acpi_evalf() called "
219 "with invalid format character '%c'\n", c);
220 return 0;
221 }
222 }
223 va_end(ap);
224
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400225 if (res_type != 'v') {
226 result.length = sizeof(out_obj);
227 result.pointer = &out_obj;
228 resultp = &result;
229 } else
230 resultp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400232 status = acpi_evaluate_object(handle, method, &params, resultp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234 switch (res_type) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400235 case 'd': /* int */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 if (res)
237 *(int *)res = out_obj.integer.value;
238 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
239 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400240 case 'v': /* void */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 success = status == AE_OK;
242 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400243 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 default:
245 printk(IBM_ERR "acpi_evalf() called "
246 "with invalid format character '%c'\n", res_type);
247 return 0;
248 }
249
250 if (!success && !quiet)
251 printk(IBM_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
252 method, fmt0, status);
253
254 return success;
255}
256
257static void __unused acpi_print_int(acpi_handle handle, char *method)
258{
259 int i;
260
261 if (acpi_evalf(handle, &i, method, "d"))
262 printk(IBM_INFO "%s = 0x%x\n", method, i);
263 else
264 printk(IBM_ERR "error calling %s\n", method);
265}
266
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300267static int acpi_ec_read(int i, u8 * p)
268{
269 int v;
270
271 if (ecrd_handle) {
272 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
273 return 0;
274 *p = v;
275 } else {
276 if (ec_read(i, p) < 0)
277 return 0;
278 }
279
280 return 1;
281}
282
283static int acpi_ec_write(int i, u8 v)
284{
285 if (ecwr_handle) {
286 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
287 return 0;
288 } else {
289 if (ec_write(i, v) < 0)
290 return 0;
291 }
292
293 return 1;
294}
295
296static int _sta(acpi_handle handle)
297{
298 int status;
299
300 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
301 status = 0;
302
303 return status;
304}
305
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -0300306static int issue_thinkpad_cmos_command(int cmos_cmd)
307{
308 if (!cmos_handle)
309 return -ENXIO;
310
311 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
312 return -EIO;
313
314 return 0;
315}
316
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300317/*************************************************************************
318 * ACPI device model
319 */
320
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300321static void drv_acpi_handle_init(char *name,
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300322 acpi_handle *handle, acpi_handle parent,
323 char **paths, int num_paths, char **path)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300324{
325 int i;
326 acpi_status status;
327
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300328 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
329 name);
330
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300331 for (i = 0; i < num_paths; i++) {
332 status = acpi_get_handle(parent, paths[i], handle);
333 if (ACPI_SUCCESS(status)) {
334 *path = paths[i];
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300335 dbg_printk(TPACPI_DBG_INIT,
336 "Found ACPI handle %s for %s\n",
337 *path, name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300338 return;
339 }
340 }
341
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300342 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
343 name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300344 *handle = NULL;
345}
346
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300347static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300348{
349 struct ibm_struct *ibm = data;
350
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300351 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
352 return;
353
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300354 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300355 return;
356
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300357 ibm->acpi->notify(ibm, event);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300358}
359
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300360static int __init setup_acpi_notify(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300361{
362 acpi_status status;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300363 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300364
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300365 BUG_ON(!ibm->acpi);
366
367 if (!*ibm->acpi->handle)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300368 return 0;
369
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300370 vdbg_printk(TPACPI_DBG_INIT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300371 "setting up ACPI notify for %s\n", ibm->name);
372
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300373 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
374 if (rc < 0) {
375 printk(IBM_ERR "acpi_bus_get_device(%s) failed: %d\n",
376 ibm->name, rc);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300377 return -ENODEV;
378 }
379
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300380 acpi_driver_data(ibm->acpi->device) = ibm;
381 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300382 IBM_ACPI_EVENT_PREFIX,
383 ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300384
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300385 status = acpi_install_notify_handler(*ibm->acpi->handle,
386 ibm->acpi->type, dispatch_acpi_notify, ibm);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300387 if (ACPI_FAILURE(status)) {
388 if (status == AE_ALREADY_EXISTS) {
389 printk(IBM_NOTICE "another device driver is already handling %s events\n",
390 ibm->name);
391 } else {
392 printk(IBM_ERR "acpi_install_notify_handler(%s) failed: %d\n",
393 ibm->name, status);
394 }
395 return -ENODEV;
396 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300397 ibm->flags.acpi_notify_installed = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300398 return 0;
399}
400
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300401static int __init tpacpi_device_add(struct acpi_device *device)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300402{
403 return 0;
404}
405
Henrique de Moraes Holschuh67001212007-04-21 11:08:25 -0300406static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300407{
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300408 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300409
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300410 dbg_printk(TPACPI_DBG_INIT,
411 "registering %s as an ACPI driver\n", ibm->name);
412
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300413 BUG_ON(!ibm->acpi);
414
415 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
416 if (!ibm->acpi->driver) {
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300417 printk(IBM_ERR "kzalloc(ibm->driver) failed\n");
418 return -ENOMEM;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300419 }
420
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300421 sprintf(ibm->acpi->driver->name, "%s_%s", IBM_NAME, ibm->name);
422 ibm->acpi->driver->ids = ibm->acpi->hid;
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200423
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300424 ibm->acpi->driver->ops.add = &tpacpi_device_add;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300425
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300426 rc = acpi_bus_register_driver(ibm->acpi->driver);
427 if (rc < 0) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300428 printk(IBM_ERR "acpi_bus_register_driver(%s) failed: %d\n",
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200429 ibm->name, rc);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300430 kfree(ibm->acpi->driver);
431 ibm->acpi->driver = NULL;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300432 } else if (!rc)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300433 ibm->flags.acpi_driver_registered = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300434
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300435 return rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300436}
437
438
439/****************************************************************************
440 ****************************************************************************
441 *
442 * Procfs Helpers
443 *
444 ****************************************************************************
445 ****************************************************************************/
446
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300447static int dispatch_procfs_read(char *page, char **start, off_t off,
448 int count, int *eof, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300449{
450 struct ibm_struct *ibm = data;
451 int len;
452
453 if (!ibm || !ibm->read)
454 return -EINVAL;
455
456 len = ibm->read(page);
457 if (len < 0)
458 return len;
459
460 if (len <= off + count)
461 *eof = 1;
462 *start = page + off;
463 len -= off;
464 if (len > count)
465 len = count;
466 if (len < 0)
467 len = 0;
468
469 return len;
470}
471
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300472static int dispatch_procfs_write(struct file *file,
473 const char __user * userbuf,
474 unsigned long count, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300475{
476 struct ibm_struct *ibm = data;
477 char *kernbuf;
478 int ret;
479
480 if (!ibm || !ibm->write)
481 return -EINVAL;
482
483 kernbuf = kmalloc(count + 2, GFP_KERNEL);
484 if (!kernbuf)
485 return -ENOMEM;
486
487 if (copy_from_user(kernbuf, userbuf, count)) {
488 kfree(kernbuf);
489 return -EFAULT;
490 }
491
492 kernbuf[count] = 0;
493 strcat(kernbuf, ",");
494 ret = ibm->write(kernbuf);
495 if (ret == 0)
496 ret = count;
497
498 kfree(kernbuf);
499
500 return ret;
501}
502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503static char *next_cmd(char **cmds)
504{
505 char *start = *cmds;
506 char *end;
507
508 while ((end = strchr(start, ',')) && end == start)
509 start = end + 1;
510
511 if (!end)
512 return NULL;
513
514 *end = 0;
515 *cmds = end + 1;
516 return start;
517}
518
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300519
520/****************************************************************************
521 ****************************************************************************
522 *
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300523 * Device model: input, hwmon and platform
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300524 *
525 ****************************************************************************
526 ****************************************************************************/
527
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -0300528static struct platform_device *tpacpi_pdev;
529static struct class_device *tpacpi_hwmon;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300530static struct input_dev *tpacpi_inputdev;
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -0300531static struct mutex tpacpi_inputdev_send_mutex;
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300532
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300533
534static int tpacpi_resume_handler(struct platform_device *pdev)
535{
536 struct ibm_struct *ibm, *itmp;
537
538 list_for_each_entry_safe(ibm, itmp,
539 &tpacpi_all_drivers,
540 all_drivers) {
541 if (ibm->resume)
542 (ibm->resume)();
543 }
544
545 return 0;
546}
547
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300548static struct platform_driver tpacpi_pdriver = {
549 .driver = {
550 .name = IBM_DRVR_NAME,
551 .owner = THIS_MODULE,
552 },
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300553 .resume = tpacpi_resume_handler,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300554};
555
556
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -0300557/*************************************************************************
558 * thinkpad-acpi driver attributes
559 */
560
561/* interface_version --------------------------------------------------- */
562static ssize_t tpacpi_driver_interface_version_show(
563 struct device_driver *drv,
564 char *buf)
565{
566 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
567}
568
569static DRIVER_ATTR(interface_version, S_IRUGO,
570 tpacpi_driver_interface_version_show, NULL);
571
572/* debug_level --------------------------------------------------------- */
573static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
574 char *buf)
575{
576 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
577}
578
579static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
580 const char *buf, size_t count)
581{
582 unsigned long t;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -0300583
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300584 if (parse_strtoul(buf, 0xffff, &t))
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -0300585 return -EINVAL;
586
587 dbg_level = t;
588
589 return count;
590}
591
592static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
593 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
594
595/* version ------------------------------------------------------------- */
596static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
597 char *buf)
598{
599 return snprintf(buf, PAGE_SIZE, "%s v%s\n", IBM_DESC, IBM_VERSION);
600}
601
602static DRIVER_ATTR(version, S_IRUGO,
603 tpacpi_driver_version_show, NULL);
604
605/* --------------------------------------------------------------------- */
606
607static struct driver_attribute* tpacpi_driver_attributes[] = {
608 &driver_attr_debug_level, &driver_attr_version,
609 &driver_attr_interface_version,
610};
611
612static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
613{
614 int i, res;
615
616 i = 0;
617 res = 0;
618 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
619 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
620 i++;
621 }
622
623 return res;
624}
625
626static void tpacpi_remove_driver_attributes(struct device_driver *drv)
627{
628 int i;
629
630 for(i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
631 driver_remove_file(drv, tpacpi_driver_attributes[i]);
632}
633
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300634/*************************************************************************
635 * sysfs support helpers
636 */
637
638struct attribute_set_obj {
639 struct attribute_set s;
640 struct attribute *a;
641} __attribute__((packed));
642
643static struct attribute_set *create_attr_set(unsigned int max_members,
644 const char* name)
645{
646 struct attribute_set_obj *sobj;
647
648 if (max_members == 0)
649 return NULL;
650
651 /* Allocates space for implicit NULL at the end too */
652 sobj = kzalloc(sizeof(struct attribute_set_obj) +
653 max_members * sizeof(struct attribute *),
654 GFP_KERNEL);
655 if (!sobj)
656 return NULL;
657 sobj->s.max_members = max_members;
658 sobj->s.group.attrs = &sobj->a;
659 sobj->s.group.name = name;
660
661 return &sobj->s;
662}
663
664/* not multi-threaded safe, use it in a single thread per set */
665static int add_to_attr_set(struct attribute_set* s, struct attribute *attr)
666{
667 if (!s || !attr)
668 return -EINVAL;
669
670 if (s->members >= s->max_members)
671 return -ENOMEM;
672
673 s->group.attrs[s->members] = attr;
674 s->members++;
675
676 return 0;
677}
678
679static int add_many_to_attr_set(struct attribute_set* s,
680 struct attribute **attr,
681 unsigned int count)
682{
683 int i, res;
684
685 for (i = 0; i < count; i++) {
686 res = add_to_attr_set(s, attr[i]);
687 if (res)
688 return res;
689 }
690
691 return 0;
692}
693
694static void delete_attr_set(struct attribute_set* s, struct kobject *kobj)
695{
696 sysfs_remove_group(kobj, &s->group);
697 destroy_attr_set(s);
698}
699
700static int parse_strtoul(const char *buf,
701 unsigned long max, unsigned long *value)
702{
703 char *endp;
704
705 *value = simple_strtoul(buf, &endp, 0);
706 while (*endp && isspace(*endp))
707 endp++;
708 if (*endp || *value > max)
709 return -EINVAL;
710
711 return 0;
712}
713
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300714/****************************************************************************
715 ****************************************************************************
716 *
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300717 * Subdrivers
718 *
719 ****************************************************************************
720 ****************************************************************************/
721
722/*************************************************************************
Henrique de Moraes Holschuh142cfc92007-04-21 11:08:26 -0300723 * thinkpad-acpi init subdriver
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300724 */
725
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300726static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727{
728 printk(IBM_INFO "%s v%s\n", IBM_DESC, IBM_VERSION);
729 printk(IBM_INFO "%s\n", IBM_URL);
730
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -0300731 printk(IBM_INFO "ThinkPad BIOS %s, EC %s\n",
732 (thinkpad_id.bios_version_str) ?
733 thinkpad_id.bios_version_str : "unknown",
734 (thinkpad_id.ec_version_str) ?
735 thinkpad_id.ec_version_str : "unknown");
736
737 if (thinkpad_id.vendor && thinkpad_id.model_str)
738 printk(IBM_INFO "%s %s\n",
739 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
740 "IBM" : ((thinkpad_id.vendor ==
741 PCI_VENDOR_ID_LENOVO) ?
742 "Lenovo" : "Unknown vendor"),
743 thinkpad_id.model_str);
Henrique de Moraes Holschuh3945ac32007-02-06 19:13:44 -0200744
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 return 0;
746}
747
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300748static int thinkpad_acpi_driver_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749{
750 int len = 0;
751
752 len += sprintf(p + len, "driver:\t\t%s\n", IBM_DESC);
753 len += sprintf(p + len, "version:\t%s\n", IBM_VERSION);
754
755 return len;
756}
757
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300758static struct ibm_struct thinkpad_acpi_driver_data = {
759 .name = "driver",
760 .read = thinkpad_acpi_driver_read,
761};
762
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300763/*************************************************************************
764 * Hotkey subdriver
765 */
766
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400767static int hotkey_orig_status;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -0300768static u32 hotkey_orig_mask;
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -0300769static u32 hotkey_all_mask;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -0300770static u32 hotkey_reserved_mask;
771
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -0300772static u16 *hotkey_keycode_map;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400773
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -0300774static struct attribute_set *hotkey_dev_attributes;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300775
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -0300776static int hotkey_get_wlsw(int *status)
777{
778 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
779 return -EIO;
780 return 0;
781}
782
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300783/* sysfs hotkey enable ------------------------------------------------- */
784static ssize_t hotkey_enable_show(struct device *dev,
785 struct device_attribute *attr,
786 char *buf)
787{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -0300788 int res, status;
789 u32 mask;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300790
791 res = hotkey_get(&status, &mask);
792 if (res)
793 return res;
794
795 return snprintf(buf, PAGE_SIZE, "%d\n", status);
796}
797
798static ssize_t hotkey_enable_store(struct device *dev,
799 struct device_attribute *attr,
800 const char *buf, size_t count)
801{
802 unsigned long t;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -0300803 int res, status;
804 u32 mask;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300805
806 if (parse_strtoul(buf, 1, &t))
807 return -EINVAL;
808
809 res = hotkey_get(&status, &mask);
810 if (!res)
811 res = hotkey_set(t, mask);
812
813 return (res) ? res : count;
814}
815
816static struct device_attribute dev_attr_hotkey_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -0300817 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300818 hotkey_enable_show, hotkey_enable_store);
819
820/* sysfs hotkey mask --------------------------------------------------- */
821static ssize_t hotkey_mask_show(struct device *dev,
822 struct device_attribute *attr,
823 char *buf)
824{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -0300825 int res, status;
826 u32 mask;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300827
828 res = hotkey_get(&status, &mask);
829 if (res)
830 return res;
831
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -0300832 return snprintf(buf, PAGE_SIZE, "0x%08x\n", mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300833}
834
835static ssize_t hotkey_mask_store(struct device *dev,
836 struct device_attribute *attr,
837 const char *buf, size_t count)
838{
839 unsigned long t;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -0300840 int res, status;
841 u32 mask;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300842
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -0300843 if (parse_strtoul(buf, 0xffffffffUL, &t))
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300844 return -EINVAL;
845
846 res = hotkey_get(&status, &mask);
847 if (!res)
848 hotkey_set(status, t);
849
850 return (res) ? res : count;
851}
852
853static struct device_attribute dev_attr_hotkey_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -0300854 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300855 hotkey_mask_show, hotkey_mask_store);
856
857/* sysfs hotkey bios_enabled ------------------------------------------- */
858static ssize_t hotkey_bios_enabled_show(struct device *dev,
859 struct device_attribute *attr,
860 char *buf)
861{
862 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
863}
864
865static struct device_attribute dev_attr_hotkey_bios_enabled =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -0300866 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300867
868/* sysfs hotkey bios_mask ---------------------------------------------- */
869static ssize_t hotkey_bios_mask_show(struct device *dev,
870 struct device_attribute *attr,
871 char *buf)
872{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -0300873 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300874}
875
876static struct device_attribute dev_attr_hotkey_bios_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -0300877 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300878
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -0300879/* sysfs hotkey all_mask ----------------------------------------------- */
880static ssize_t hotkey_all_mask_show(struct device *dev,
881 struct device_attribute *attr,
882 char *buf)
883{
884 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_all_mask);
885}
886
887static struct device_attribute dev_attr_hotkey_all_mask =
888 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
889
890/* sysfs hotkey recommended_mask --------------------------------------- */
891static ssize_t hotkey_recommended_mask_show(struct device *dev,
892 struct device_attribute *attr,
893 char *buf)
894{
895 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
896 hotkey_all_mask & ~hotkey_reserved_mask);
897}
898
899static struct device_attribute dev_attr_hotkey_recommended_mask =
900 __ATTR(hotkey_recommended_mask, S_IRUGO,
901 hotkey_recommended_mask_show, NULL);
902
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -0300903/* sysfs hotkey radio_sw ----------------------------------------------- */
904static ssize_t hotkey_radio_sw_show(struct device *dev,
905 struct device_attribute *attr,
906 char *buf)
907{
908 int res, s;
909 res = hotkey_get_wlsw(&s);
910 if (res < 0)
911 return res;
912
913 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
914}
915
916static struct device_attribute dev_attr_hotkey_radio_sw =
917 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
918
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -0300919/* sysfs hotkey report_mode -------------------------------------------- */
920static ssize_t hotkey_report_mode_show(struct device *dev,
921 struct device_attribute *attr,
922 char *buf)
923{
924 return snprintf(buf, PAGE_SIZE, "%d\n",
925 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
926}
927
928static struct device_attribute dev_attr_hotkey_report_mode =
929 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
930
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300931/* --------------------------------------------------------------------- */
932
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -0300933static struct attribute *hotkey_attributes[] __initdata = {
934 &dev_attr_hotkey_enable.attr,
935 &dev_attr_hotkey_report_mode.attr,
936};
937
938static struct attribute *hotkey_mask_attributes[] __initdata = {
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300939 &dev_attr_hotkey_mask.attr,
940 &dev_attr_hotkey_bios_enabled.attr,
941 &dev_attr_hotkey_bios_mask.attr,
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -0300942 &dev_attr_hotkey_all_mask.attr,
943 &dev_attr_hotkey_recommended_mask.attr,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300944};
945
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300946static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300947{
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -0300948
949 static u16 ibm_keycode_map[] __initdata = {
950 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
951 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
952 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
953 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
954 /* Scan codes 0x0C to 0x0F: Other ACPI HKEY hot keys */
955 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
956 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
957 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
958 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
959 /* Scan codes 0x10 to 0x1F: Extended ACPI HKEY hot keys */
960 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
961 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
962 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
963 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
964 KEY_RESERVED, /* 0x14: VOLUME UP */
965 KEY_RESERVED, /* 0x15: VOLUME DOWN */
966 KEY_RESERVED, /* 0x16: MUTE */
967 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
968 /* (assignments unknown, please report if found) */
969 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
970 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
971 };
972 static u16 lenovo_keycode_map[] __initdata = {
973 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
974 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
975 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
976 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
977 /* Scan codes 0x0C to 0x0F: Other ACPI HKEY hot keys */
978 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
979 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
980 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
981 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
982 /* Scan codes 0x10 to 0x1F: Extended ACPI HKEY hot keys */
983 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
984 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
985 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
986 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
987 KEY_RESERVED, /* 0x14: VOLUME UP */
988 KEY_RESERVED, /* 0x15: VOLUME DOWN */
989 KEY_RESERVED, /* 0x16: MUTE */
990 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
991 /* (assignments unknown, please report if found) */
992 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
993 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
994 };
995
996#define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
997#define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
998#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
999
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001000 int res, i;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001001 int status;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001002
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001003 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
1004
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001005 BUG_ON(!tpacpi_inputdev);
1006
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001007 IBM_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001008 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001009
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001010 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001011 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001012
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001013 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001014 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001015
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001016 if (tp_features.hotkey) {
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001017 hotkey_dev_attributes = create_attr_set(8, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001018 if (!hotkey_dev_attributes)
1019 return -ENOMEM;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001020 res = add_many_to_attr_set(hotkey_dev_attributes,
1021 hotkey_attributes,
1022 ARRAY_SIZE(hotkey_attributes));
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001023 if (res)
1024 return res;
1025
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001026 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1027 A30, R30, R31, T20-22, X20-21, X22-24 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001028 tp_features.hotkey_mask =
1029 acpi_evalf(hkey_handle, NULL, "DHKN", "qv");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001030
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001031 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001032 str_supported(tp_features.hotkey_mask));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001033
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001034 if (tp_features.hotkey_mask) {
1035 /* MHKA available in A31, R40, R40e, T4x, X31, and later */
1036 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
1037 "MHKA", "qd"))
1038 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
1039 }
1040
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001041 res = hotkey_get(&hotkey_orig_status, &hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001042 if (!res && tp_features.hotkey_mask) {
1043 res = add_many_to_attr_set(hotkey_dev_attributes,
1044 hotkey_mask_attributes,
1045 ARRAY_SIZE(hotkey_mask_attributes));
1046 }
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001047
1048 /* Not all thinkpads have a hardware radio switch */
1049 if (!res && acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
1050 tp_features.hotkey_wlsw = 1;
1051 printk(IBM_INFO
1052 "radio switch found; radios are %s\n",
1053 enabled(status, 0));
1054 res = add_to_attr_set(hotkey_dev_attributes,
1055 &dev_attr_hotkey_radio_sw.attr);
1056 }
1057
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001058 if (!res)
1059 res = register_attr_set_with_sysfs(
1060 hotkey_dev_attributes,
1061 &tpacpi_pdev->dev.kobj);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001062 if (res)
1063 return res;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001064
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001065 /* Set up key map */
1066
1067 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
1068 GFP_KERNEL);
1069 if (!hotkey_keycode_map) {
1070 printk(IBM_ERR "failed to allocate memory for key map\n");
1071 return -ENOMEM;
1072 }
1073
1074 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
1075 dbg_printk(TPACPI_DBG_INIT,
1076 "using Lenovo default hot key map\n");
1077 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
1078 TPACPI_HOTKEY_MAP_SIZE);
1079 } else {
1080 dbg_printk(TPACPI_DBG_INIT,
1081 "using IBM default hot key map\n");
1082 memcpy(hotkey_keycode_map, &ibm_keycode_map,
1083 TPACPI_HOTKEY_MAP_SIZE);
1084 }
1085
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001086 set_bit(EV_KEY, tpacpi_inputdev->evbit);
1087 set_bit(EV_MSC, tpacpi_inputdev->evbit);
1088 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001089 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
1090 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
1091 tpacpi_inputdev->keycode = hotkey_keycode_map;
1092 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001093 if (hotkey_keycode_map[i] != KEY_RESERVED) {
1094 set_bit(hotkey_keycode_map[i],
1095 tpacpi_inputdev->keybit);
1096 } else {
1097 if (i < sizeof(hotkey_reserved_mask)*8)
1098 hotkey_reserved_mask |= 1 << i;
1099 }
1100 }
1101
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03001102 if (tp_features.hotkey_wlsw) {
1103 set_bit(EV_SW, tpacpi_inputdev->evbit);
1104 set_bit(SW_RADIO, tpacpi_inputdev->swbit);
1105 }
1106
Henrique de Moraes Holschuh1a343762007-07-18 23:45:36 -03001107 dbg_printk(TPACPI_DBG_INIT,
1108 "enabling hot key handling\n");
1109 res = hotkey_set(1, (hotkey_all_mask & ~hotkey_reserved_mask)
1110 | hotkey_orig_mask);
1111 if (res)
1112 return res;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001113
1114 dbg_printk(TPACPI_DBG_INIT,
1115 "legacy hot key reporting over procfs %s\n",
1116 (hotkey_report_mode < 2) ?
1117 "enabled" : "disabled");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001118 }
1119
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001120 return (tp_features.hotkey)? 0 : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001121}
1122
1123static void hotkey_exit(void)
1124{
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001125 int res;
1126
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001127 if (tp_features.hotkey) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001128 dbg_printk(TPACPI_DBG_EXIT, "restoring original hotkey mask\n");
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001129 res = hotkey_set(hotkey_orig_status, hotkey_orig_mask);
1130 if (res)
1131 printk(IBM_ERR "failed to restore hotkey to BIOS defaults\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001132 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001133
1134 if (hotkey_dev_attributes) {
1135 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
1136 hotkey_dev_attributes = NULL;
1137 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001138}
1139
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001140static void tpacpi_input_send_key(unsigned int scancode,
1141 unsigned int keycode)
1142{
1143 if (keycode != KEY_RESERVED) {
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03001144 mutex_lock(&tpacpi_inputdev_send_mutex);
1145
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001146 input_report_key(tpacpi_inputdev, keycode, 1);
1147 if (keycode == KEY_UNKNOWN)
1148 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1149 scancode);
1150 input_sync(tpacpi_inputdev);
1151
1152 input_report_key(tpacpi_inputdev, keycode, 0);
1153 if (keycode == KEY_UNKNOWN)
1154 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1155 scancode);
1156 input_sync(tpacpi_inputdev);
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03001157
1158 mutex_unlock(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001159 }
1160}
1161
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03001162static void tpacpi_input_send_radiosw(void)
1163{
1164 int wlsw;
1165
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03001166 mutex_lock(&tpacpi_inputdev_send_mutex);
1167
Henrique de Moraes Holschuhf4e1e432007-09-23 11:39:00 -03001168 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03001169 input_report_switch(tpacpi_inputdev,
1170 SW_RADIO, !!wlsw);
Henrique de Moraes Holschuhf4e1e432007-09-23 11:39:00 -03001171 input_sync(tpacpi_inputdev);
1172 }
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03001173
1174 mutex_unlock(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03001175}
1176
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001177static void hotkey_notify(struct ibm_struct *ibm, u32 event)
1178{
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001179 u32 hkey;
1180 unsigned int keycode, scancode;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001181 int send_acpi_ev = 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001182
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001183 if (event == 0x80 && acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001184 switch (hkey >> 12) {
1185 case 1:
1186 /* 0x1000-0x1FFF: key presses */
1187 scancode = hkey & 0xfff;
1188 if (scancode > 0 && scancode < 0x21) {
1189 scancode--;
1190 keycode = hotkey_keycode_map[scancode];
1191 tpacpi_input_send_key(scancode, keycode);
1192 } else {
1193 printk(IBM_ERR
1194 "hotkey 0x%04x out of range for keyboard map\n",
1195 hkey);
1196 send_acpi_ev = 1;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001197 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001198 break;
1199 case 5:
1200 /* 0x5000-0x5FFF: LID */
1201 /* we don't handle it through this path, just
1202 * eat up known LID events */
1203 if (hkey != 0x5001 && hkey != 0x5002) {
1204 printk(IBM_ERR
1205 "unknown LID-related hotkey event: 0x%04x\n",
1206 hkey);
1207 send_acpi_ev = 1;
1208 }
1209 break;
1210 case 7:
1211 /* 0x7000-0x7FFF: misc */
1212 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
1213 tpacpi_input_send_radiosw();
1214 break;
1215 }
1216 /* fallthrough to default */
1217 default:
1218 /* case 2: dock-related */
1219 /* 0x2305 - T43 waking up due to bay lever eject while aslept */
1220 /* case 3: ultra-bay related. maybe bay in dock? */
1221 /* 0x3003 - T43 after wake up by bay lever eject (0x2305) */
1222 printk(IBM_NOTICE "unhandled HKEY event 0x%04x\n", hkey);
1223 send_acpi_ev = 1;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001224 }
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001225 } else {
1226 printk(IBM_ERR "unknown hotkey notification event %d\n", event);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001227 hkey = 0;
1228 send_acpi_ev = 1;
1229 }
1230
1231 /* Legacy events */
1232 if (send_acpi_ev || hotkey_report_mode < 2)
1233 acpi_bus_generate_proc_event(ibm->acpi->device, event, hkey);
1234
1235 /* netlink events */
1236 if (send_acpi_ev) {
1237 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
1238 ibm->acpi->device->dev.bus_id,
1239 event, hkey);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001240 }
1241}
1242
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03001243static void hotkey_resume(void)
1244{
1245 tpacpi_input_send_radiosw();
1246}
1247
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001248/*
1249 * Call with hotkey_mutex held
1250 */
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001251static int hotkey_get(int *status, u32 *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252{
1253 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001254 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001255
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001256 if (tp_features.hotkey_mask)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001257 if (!acpi_evalf(hkey_handle, mask, "DHKN", "d"))
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001258 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001259
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001260 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261}
1262
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001263/*
1264 * Call with hotkey_mutex held
1265 */
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001266static int hotkey_set(int status, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267{
1268 int i;
1269
1270 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001271 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001273 if (tp_features.hotkey_mask)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001274 for (i = 0; i < 32; i++) {
1275 int bit = ((1 << i) & mask) != 0;
1276 if (!acpi_evalf(hkey_handle,
1277 NULL, "MHKM", "vdd", i + 1, bit))
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001278 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001279 }
1280
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001281 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001282}
1283
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001284/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001285static int hotkey_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001287 int res, status;
1288 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 int len = 0;
1290
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001291 if (!tp_features.hotkey) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001292 len += sprintf(p + len, "status:\t\tnot supported\n");
1293 return len;
1294 }
1295
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001296 res = mutex_lock_interruptible(&hotkey_mutex);
1297 if (res < 0)
1298 return res;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001299 res = hotkey_get(&status, &mask);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001300 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001301 if (res)
1302 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
1304 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001305 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001306 len += sprintf(p + len, "mask:\t\t0x%08x\n", mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 len += sprintf(p + len,
1308 "commands:\tenable, disable, reset, <mask>\n");
1309 } else {
1310 len += sprintf(p + len, "mask:\t\tnot supported\n");
1311 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
1312 }
1313
1314 return len;
1315}
1316
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001317static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001319 int res, status;
1320 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 char *cmd;
1322 int do_cmd = 0;
1323
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001324 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 return -ENODEV;
1326
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001327 res = mutex_lock_interruptible(&hotkey_mutex);
1328 if (res < 0)
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001329 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001330
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001331 res = hotkey_get(&status, &mask);
1332 if (res)
1333 goto errexit;
1334
1335 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 while ((cmd = next_cmd(&buf))) {
1337 if (strlencmp(cmd, "enable") == 0) {
1338 status = 1;
1339 } else if (strlencmp(cmd, "disable") == 0) {
1340 status = 0;
1341 } else if (strlencmp(cmd, "reset") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001342 status = hotkey_orig_status;
1343 mask = hotkey_orig_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
1345 /* mask set */
1346 } else if (sscanf(cmd, "%x", &mask) == 1) {
1347 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001348 } else {
1349 res = -EINVAL;
1350 goto errexit;
1351 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 do_cmd = 1;
1353 }
1354
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001355 if (do_cmd)
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001356 res = hotkey_set(status, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001358errexit:
1359 mutex_unlock(&hotkey_mutex);
1360 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001361}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
Thomas Renninger1ba90e32007-07-23 14:44:41 +02001363static const struct acpi_device_id ibm_htk_device_ids[] = {
1364 {IBM_HKEY_HID, 0},
1365 {"", 0},
1366};
1367
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001368static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +02001369 .hid = ibm_htk_device_ids,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001370 .notify = hotkey_notify,
1371 .handle = &hkey_handle,
1372 .type = ACPI_DEVICE_NOTIFY,
1373};
1374
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001375static struct ibm_struct hotkey_driver_data = {
1376 .name = "hotkey",
1377 .read = hotkey_read,
1378 .write = hotkey_write,
1379 .exit = hotkey_exit,
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03001380 .resume = hotkey_resume,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001381 .acpi = &ibm_hotkey_acpidriver,
1382};
1383
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001384/*************************************************************************
1385 * Bluetooth subdriver
1386 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001388/* sysfs bluetooth enable ---------------------------------------------- */
1389static ssize_t bluetooth_enable_show(struct device *dev,
1390 struct device_attribute *attr,
1391 char *buf)
1392{
1393 int status;
1394
1395 status = bluetooth_get_radiosw();
1396 if (status < 0)
1397 return status;
1398
1399 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
1400}
1401
1402static ssize_t bluetooth_enable_store(struct device *dev,
1403 struct device_attribute *attr,
1404 const char *buf, size_t count)
1405{
1406 unsigned long t;
1407 int res;
1408
1409 if (parse_strtoul(buf, 1, &t))
1410 return -EINVAL;
1411
1412 res = bluetooth_set_radiosw(t);
1413
1414 return (res) ? res : count;
1415}
1416
1417static struct device_attribute dev_attr_bluetooth_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001418 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001419 bluetooth_enable_show, bluetooth_enable_store);
1420
1421/* --------------------------------------------------------------------- */
1422
1423static struct attribute *bluetooth_attributes[] = {
1424 &dev_attr_bluetooth_enable.attr,
1425 NULL
1426};
1427
1428static const struct attribute_group bluetooth_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001429 .attrs = bluetooth_attributes,
1430};
1431
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001432static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001434 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001435 int status = 0;
1436
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001437 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
1438
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001439 IBM_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001440
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001441 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1442 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001443 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001444 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001446 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
1447 str_supported(tp_features.bluetooth),
1448 status);
1449
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001450 if (tp_features.bluetooth) {
1451 if (!(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
1452 /* no bluetooth hardware present in system */
1453 tp_features.bluetooth = 0;
1454 dbg_printk(TPACPI_DBG_INIT,
1455 "bluetooth hardware not installed\n");
1456 } else {
1457 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
1458 &bluetooth_attr_group);
1459 if (res)
1460 return res;
1461 }
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001462 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001463
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001464 return (tp_features.bluetooth)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465}
1466
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001467static void bluetooth_exit(void)
1468{
1469 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
1470 &bluetooth_attr_group);
1471}
1472
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001473static int bluetooth_get_radiosw(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474{
1475 int status;
1476
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001477 if (!tp_features.bluetooth)
1478 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001480 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
1481 return -EIO;
1482
1483 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
1484}
1485
1486static int bluetooth_set_radiosw(int radio_on)
1487{
1488 int status;
1489
1490 if (!tp_features.bluetooth)
1491 return -ENODEV;
1492
1493 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
1494 return -EIO;
1495 if (radio_on)
1496 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
1497 else
1498 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
1499 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
1500 return -EIO;
1501
1502 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503}
1504
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001505/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001506static int bluetooth_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507{
1508 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001509 int status = bluetooth_get_radiosw();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001511 if (!tp_features.bluetooth)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 len += sprintf(p + len, "status:\t\tnot supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001514 len += sprintf(p + len, "status:\t\t%s\n",
1515 (status)? "enabled" : "disabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 len += sprintf(p + len, "commands:\tenable, disable\n");
1517 }
1518
1519 return len;
1520}
1521
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001522static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001526 if (!tp_features.bluetooth)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001527 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
1529 while ((cmd = next_cmd(&buf))) {
1530 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001531 bluetooth_set_radiosw(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001533 bluetooth_set_radiosw(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 } else
1535 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 }
1537
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 return 0;
1539}
1540
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001541static struct ibm_struct bluetooth_driver_data = {
1542 .name = "bluetooth",
1543 .read = bluetooth_read,
1544 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001545 .exit = bluetooth_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001546};
1547
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001548/*************************************************************************
1549 * Wan subdriver
1550 */
1551
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001552/* sysfs wan enable ---------------------------------------------------- */
1553static ssize_t wan_enable_show(struct device *dev,
1554 struct device_attribute *attr,
1555 char *buf)
1556{
1557 int status;
1558
1559 status = wan_get_radiosw();
1560 if (status < 0)
1561 return status;
1562
1563 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
1564}
1565
1566static ssize_t wan_enable_store(struct device *dev,
1567 struct device_attribute *attr,
1568 const char *buf, size_t count)
1569{
1570 unsigned long t;
1571 int res;
1572
1573 if (parse_strtoul(buf, 1, &t))
1574 return -EINVAL;
1575
1576 res = wan_set_radiosw(t);
1577
1578 return (res) ? res : count;
1579}
1580
1581static struct device_attribute dev_attr_wan_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001582 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001583 wan_enable_show, wan_enable_store);
1584
1585/* --------------------------------------------------------------------- */
1586
1587static struct attribute *wan_attributes[] = {
1588 &dev_attr_wan_enable.attr,
1589 NULL
1590};
1591
1592static const struct attribute_group wan_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001593 .attrs = wan_attributes,
1594};
1595
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001596static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001597{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001598 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001599 int status = 0;
1600
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001601 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
1602
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001603 IBM_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001604
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001605 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001606 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001607
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001608 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
1609 str_supported(tp_features.wan),
1610 status);
1611
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001612 if (tp_features.wan) {
1613 if (!(status & TP_ACPI_WANCARD_HWPRESENT)) {
1614 /* no wan hardware present in system */
1615 tp_features.wan = 0;
1616 dbg_printk(TPACPI_DBG_INIT,
1617 "wan hardware not installed\n");
1618 } else {
1619 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
1620 &wan_attr_group);
1621 if (res)
1622 return res;
1623 }
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001624 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001625
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001626 return (tp_features.wan)? 0 : 1;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001627}
1628
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001629static void wan_exit(void)
1630{
1631 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
1632 &wan_attr_group);
1633}
1634
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001635static int wan_get_radiosw(void)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001636{
1637 int status;
1638
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001639 if (!tp_features.wan)
1640 return -ENODEV;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001641
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001642 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
1643 return -EIO;
1644
1645 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
1646}
1647
1648static int wan_set_radiosw(int radio_on)
1649{
1650 int status;
1651
1652 if (!tp_features.wan)
1653 return -ENODEV;
1654
1655 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
1656 return -EIO;
1657 if (radio_on)
1658 status |= TP_ACPI_WANCARD_RADIOSSW;
1659 else
1660 status &= ~TP_ACPI_WANCARD_RADIOSSW;
1661 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
1662 return -EIO;
1663
1664 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001665}
1666
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001667/* procfs -------------------------------------------------------------- */
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001668static int wan_read(char *p)
1669{
1670 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001671 int status = wan_get_radiosw();
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001672
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001673 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001674 len += sprintf(p + len, "status:\t\tnot supported\n");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001675 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001676 len += sprintf(p + len, "status:\t\t%s\n",
1677 (status)? "enabled" : "disabled");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001678 len += sprintf(p + len, "commands:\tenable, disable\n");
1679 }
1680
1681 return len;
1682}
1683
1684static int wan_write(char *buf)
1685{
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001686 char *cmd;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001687
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001688 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001689 return -ENODEV;
1690
1691 while ((cmd = next_cmd(&buf))) {
1692 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001693 wan_set_radiosw(1);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001694 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001695 wan_set_radiosw(0);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001696 } else
1697 return -EINVAL;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001698 }
1699
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001700 return 0;
1701}
1702
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001703static struct ibm_struct wan_driver_data = {
1704 .name = "wan",
1705 .read = wan_read,
1706 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001707 .exit = wan_exit,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03001708 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001709};
1710
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001711/*************************************************************************
1712 * Video subdriver
1713 */
1714
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02001715static enum video_access_mode video_supported;
1716static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001717
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001718IBM_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
1719 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
1720 "\\_SB.PCI0.VID0", /* 770e */
1721 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
1722 "\\_SB.PCI0.AGP.VID", /* all others */
1723 ); /* R30, R31 */
1724
1725IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
1726
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001727static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001729 int ivga;
1730
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001731 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
1732
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001733 IBM_ACPIHANDLE_INIT(vid);
1734 IBM_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001735
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001736 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
1737 /* G41, assume IVGA doesn't change */
1738 vid_handle = vid2_handle;
1739
1740 if (!vid_handle)
1741 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001742 video_supported = TPACPI_VIDEO_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001743 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
1744 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001745 video_supported = TPACPI_VIDEO_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001746 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
1747 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001748 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001749 else
1750 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001751 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001753 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
1754 str_supported(video_supported != TPACPI_VIDEO_NONE),
1755 video_supported);
1756
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001757 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758}
1759
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001760static void video_exit(void)
1761{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001762 dbg_printk(TPACPI_DBG_EXIT,
1763 "restoring original video autoswitch mode\n");
1764 if (video_autosw_set(video_orig_autosw))
1765 printk(IBM_ERR "error while trying to restore original "
1766 "video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001767}
1768
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001769static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770{
1771 int status = 0;
1772 int i;
1773
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001774 switch (video_supported) {
1775 case TPACPI_VIDEO_570:
1776 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
1777 TP_ACPI_VIDEO_570_PHSCMD))
1778 return -EIO;
1779 status = i & TP_ACPI_VIDEO_570_PHSMASK;
1780 break;
1781 case TPACPI_VIDEO_770:
1782 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
1783 return -EIO;
1784 if (i)
1785 status |= TP_ACPI_VIDEO_S_LCD;
1786 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
1787 return -EIO;
1788 if (i)
1789 status |= TP_ACPI_VIDEO_S_CRT;
1790 break;
1791 case TPACPI_VIDEO_NEW:
1792 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
1793 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
1794 return -EIO;
1795 if (i)
1796 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001798 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
1799 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
1800 return -EIO;
1801 if (i)
1802 status |= TP_ACPI_VIDEO_S_LCD;
1803 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
1804 return -EIO;
1805 if (i)
1806 status |= TP_ACPI_VIDEO_S_DVI;
1807 break;
1808 default:
1809 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001810 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811
1812 return status;
1813}
1814
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001815static int video_outputsw_set(int status)
1816{
1817 int autosw;
1818 int res = 0;
1819
1820 switch (video_supported) {
1821 case TPACPI_VIDEO_570:
1822 res = acpi_evalf(NULL, NULL,
1823 "\\_SB.PHS2", "vdd",
1824 TP_ACPI_VIDEO_570_PHS2CMD,
1825 status | TP_ACPI_VIDEO_570_PHS2SET);
1826 break;
1827 case TPACPI_VIDEO_770:
1828 autosw = video_autosw_get();
1829 if (autosw < 0)
1830 return autosw;
1831
1832 res = video_autosw_set(1);
1833 if (res)
1834 return res;
1835 res = acpi_evalf(vid_handle, NULL,
1836 "ASWT", "vdd", status * 0x100, 0);
1837 if (!autosw && video_autosw_set(autosw)) {
1838 printk(IBM_ERR "video auto-switch left enabled due to error\n");
1839 return -EIO;
1840 }
1841 break;
1842 case TPACPI_VIDEO_NEW:
1843 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
1844 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
1845 break;
1846 default:
1847 return -ENOSYS;
1848 }
1849
1850 return (res)? 0 : -EIO;
1851}
1852
1853static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001855 int autosw = 0;
1856
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001857 switch (video_supported) {
1858 case TPACPI_VIDEO_570:
1859 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
1860 return -EIO;
1861 break;
1862 case TPACPI_VIDEO_770:
1863 case TPACPI_VIDEO_NEW:
1864 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
1865 return -EIO;
1866 break;
1867 default:
1868 return -ENOSYS;
1869 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001870
1871 return autosw & 1;
1872}
1873
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001874static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001875{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001876 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001877 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001878 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001879}
1880
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001881static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001882{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001883 int autosw = video_autosw_get();
1884 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001885
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001886 if (autosw < 0)
1887 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001888
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001889 switch (video_supported) {
1890 case TPACPI_VIDEO_570:
1891 res = video_autosw_set(1);
1892 if (res)
1893 return res;
1894 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
1895 break;
1896 case TPACPI_VIDEO_770:
1897 case TPACPI_VIDEO_NEW:
1898 res = video_autosw_set(1);
1899 if (res)
1900 return res;
1901 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
1902 break;
1903 default:
1904 return -ENOSYS;
1905 }
1906 if (!autosw && video_autosw_set(autosw)) {
1907 printk(IBM_ERR "video auto-switch left enabled due to error\n");
1908 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001909 }
1910
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001911 return (res)? 0 : -EIO;
1912}
1913
1914static int video_expand_toggle(void)
1915{
1916 switch (video_supported) {
1917 case TPACPI_VIDEO_570:
1918 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
1919 0 : -EIO;
1920 case TPACPI_VIDEO_770:
1921 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
1922 0 : -EIO;
1923 case TPACPI_VIDEO_NEW:
1924 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
1925 0 : -EIO;
1926 default:
1927 return -ENOSYS;
1928 }
1929 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001930}
1931
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001932static int video_read(char *p)
1933{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001934 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001935 int len = 0;
1936
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001937 if (video_supported == TPACPI_VIDEO_NONE) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001938 len += sprintf(p + len, "status:\t\tnot supported\n");
1939 return len;
1940 }
1941
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001942 status = video_outputsw_get();
1943 if (status < 0)
1944 return status;
1945
1946 autosw = video_autosw_get();
1947 if (autosw < 0)
1948 return autosw;
1949
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001950 len += sprintf(p + len, "status:\t\tsupported\n");
1951 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
1952 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001953 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001954 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
1955 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
1956 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
1957 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001958 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001959 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
1960 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
1961 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
1962
1963 return len;
1964}
1965
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001966static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967{
1968 char *cmd;
1969 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001970 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001972 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001973 return -ENODEV;
1974
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001975 enable = 0;
1976 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977
1978 while ((cmd = next_cmd(&buf))) {
1979 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001980 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001982 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001984 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001986 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001987 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001988 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001989 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001990 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001991 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001992 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001994 res = video_autosw_set(1);
1995 if (res)
1996 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001998 res = video_autosw_set(0);
1999 if (res)
2000 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002002 res = video_outputsw_cycle();
2003 if (res)
2004 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002006 res = video_expand_toggle();
2007 if (res)
2008 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 } else
2010 return -EINVAL;
2011 }
2012
2013 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002014 status = video_outputsw_get();
2015 if (status < 0)
2016 return status;
2017 res = video_outputsw_set((status & ~disable) | enable);
2018 if (res)
2019 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 }
2021
2022 return 0;
2023}
2024
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002025static struct ibm_struct video_driver_data = {
2026 .name = "video",
2027 .read = video_read,
2028 .write = video_write,
2029 .exit = video_exit,
2030};
2031
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002032/*************************************************************************
2033 * Light (thinklight) subdriver
2034 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002036IBM_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
2037IBM_HANDLE(ledb, ec, "LEDB"); /* G4x */
2038
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002039static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002041 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
2042
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002043 IBM_ACPIHANDLE_INIT(ledb);
2044 IBM_ACPIHANDLE_INIT(lght);
2045 IBM_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002046
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002047 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002048 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002049
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002050 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002051 /* light status not supported on
2052 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002053 tp_features.light_status =
2054 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002056 vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002057 str_supported(tp_features.light));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002058
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002059 return (tp_features.light)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060}
2061
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002062static int light_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063{
2064 int len = 0;
2065 int status = 0;
2066
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002067 if (!tp_features.light) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002068 len += sprintf(p + len, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002069 } else if (!tp_features.light_status) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002070 len += sprintf(p + len, "status:\t\tunknown\n");
2071 len += sprintf(p + len, "commands:\ton, off\n");
2072 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
2074 return -EIO;
2075 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002076 len += sprintf(p + len, "commands:\ton, off\n");
2077 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078
2079 return len;
2080}
2081
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002082static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083{
2084 int cmos_cmd, lght_cmd;
2085 char *cmd;
2086 int success;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002087
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002088 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002089 return -ENODEV;
2090
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 while ((cmd = next_cmd(&buf))) {
2092 if (strlencmp(cmd, "on") == 0) {
2093 cmos_cmd = 0x0c;
2094 lght_cmd = 1;
2095 } else if (strlencmp(cmd, "off") == 0) {
2096 cmos_cmd = 0x0d;
2097 lght_cmd = 0;
2098 } else
2099 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002100
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 success = cmos_handle ?
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002102 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
2103 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 if (!success)
2105 return -EIO;
2106 }
2107
2108 return 0;
2109}
2110
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002111static struct ibm_struct light_driver_data = {
2112 .name = "light",
2113 .read = light_read,
2114 .write = light_write,
2115};
2116
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002117/*************************************************************************
2118 * Dock subdriver
2119 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03002121#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002122
2123IBM_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
2124 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
2125 "\\_SB.PCI0.PCI1.DOCK", /* all others */
2126 "\\_SB.PCI.ISA.SLCE", /* 570 */
2127 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
2128
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002129/* don't list other alternatives as we install a notify handler on the 570 */
2130IBM_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
2131
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002132static const struct acpi_device_id ibm_pci_device_ids[] = {
2133 {PCI_ROOT_HID_STRING, 0},
2134 {"", 0},
2135};
2136
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03002137static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
2138 {
2139 .notify = dock_notify,
2140 .handle = &dock_handle,
2141 .type = ACPI_SYSTEM_NOTIFY,
2142 },
2143 {
Henrique de Moraes Holschuh996fba02007-07-18 23:45:40 -03002144 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
2145 * We just use it to get notifications of dock hotplug
2146 * in very old thinkpads */
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002147 .hid = ibm_pci_device_ids,
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03002148 .notify = dock_notify,
2149 .handle = &pci_handle,
2150 .type = ACPI_SYSTEM_NOTIFY,
2151 },
2152};
2153
2154static struct ibm_struct dock_driver_data[2] = {
2155 {
2156 .name = "dock",
2157 .read = dock_read,
2158 .write = dock_write,
2159 .acpi = &ibm_dock_acpidriver[0],
2160 },
2161 {
2162 .name = "dock",
2163 .acpi = &ibm_dock_acpidriver[1],
2164 },
2165};
2166
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167#define dock_docked() (_sta(dock_handle) & 1)
2168
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002169static int __init dock_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002170{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002171 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
2172
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002173 IBM_ACPIHANDLE_INIT(dock);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002174
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002175 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
2176 str_supported(dock_handle != NULL));
2177
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002178 return (dock_handle)? 0 : 1;
2179}
2180
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03002181static int __init dock_init2(struct ibm_init_struct *iibm)
2182{
2183 int dock2_needed;
2184
2185 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
2186
2187 if (dock_driver_data[0].flags.acpi_driver_registered &&
2188 dock_driver_data[0].flags.acpi_notify_installed) {
2189 IBM_ACPIHANDLE_INIT(pci);
2190 dock2_needed = (pci_handle != NULL);
2191 vdbg_printk(TPACPI_DBG_INIT,
2192 "dock PCI handler for the TP 570 is %s\n",
2193 str_supported(dock2_needed));
2194 } else {
2195 vdbg_printk(TPACPI_DBG_INIT,
2196 "dock subdriver part 2 not required\n");
2197 dock2_needed = 0;
2198 }
2199
2200 return (dock2_needed)? 0 : 1;
2201}
2202
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002203static void dock_notify(struct ibm_struct *ibm, u32 event)
2204{
2205 int docked = dock_docked();
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002206 int pci = ibm->acpi->hid && ibm->acpi->device &&
2207 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
Zhang Rui962ce8c2007-08-23 01:24:31 +08002208 int data;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002209
2210 if (event == 1 && !pci) /* 570 */
Zhang Rui962ce8c2007-08-23 01:24:31 +08002211 data = 1; /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002212 else if (event == 1 && pci) /* 570 */
Zhang Rui962ce8c2007-08-23 01:24:31 +08002213 data = 3; /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002214 else if (event == 3 && docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08002215 data = 1; /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002216 else if (event == 3 && !docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08002217 data = 2; /* undock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002218 else if (event == 0 && docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08002219 data = 3; /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002220 else {
2221 printk(IBM_ERR "unknown dock event %d, status %d\n",
2222 event, _sta(dock_handle));
Zhang Rui962ce8c2007-08-23 01:24:31 +08002223 data = 0; /* unknown */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002224 }
Len Brown14e04fb2007-08-23 15:20:26 -04002225 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
Zhang Rui962ce8c2007-08-23 01:24:31 +08002226 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
2227 ibm->acpi->device->dev.bus_id,
2228 event, data);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002229}
2230
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002231static int dock_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232{
2233 int len = 0;
2234 int docked = dock_docked();
2235
2236 if (!dock_handle)
2237 len += sprintf(p + len, "status:\t\tnot supported\n");
2238 else if (!docked)
2239 len += sprintf(p + len, "status:\t\tundocked\n");
2240 else {
2241 len += sprintf(p + len, "status:\t\tdocked\n");
2242 len += sprintf(p + len, "commands:\tdock, undock\n");
2243 }
2244
2245 return len;
2246}
2247
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002248static int dock_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249{
2250 char *cmd;
2251
2252 if (!dock_docked())
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002253 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254
2255 while ((cmd = next_cmd(&buf))) {
2256 if (strlencmp(cmd, "undock") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002257 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
2258 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 return -EIO;
2260 } else if (strlencmp(cmd, "dock") == 0) {
2261 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
2262 return -EIO;
2263 } else
2264 return -EINVAL;
2265 }
2266
2267 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002268}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03002270#endif /* CONFIG_THINKPAD_ACPI_DOCK */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002272/*************************************************************************
2273 * Bay subdriver
2274 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03002276#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002277IBM_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
2278 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
2279 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
2280 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
2281 ); /* A21e, R30, R31 */
2282IBM_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
2283 "_EJ0", /* all others */
2284 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
2285IBM_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
2286 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
2287 ); /* all others */
2288IBM_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
2289 "_EJ0", /* 770x */
2290 ); /* all others */
2291
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002292static int __init bay_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002294 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
2295
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002296 IBM_ACPIHANDLE_INIT(bay);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002297 if (bay_handle)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002298 IBM_ACPIHANDLE_INIT(bay_ej);
2299 IBM_ACPIHANDLE_INIT(bay2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002300 if (bay2_handle)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002301 IBM_ACPIHANDLE_INIT(bay2_ej);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002302
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002303 tp_features.bay_status = bay_handle &&
2304 acpi_evalf(bay_handle, NULL, "_STA", "qv");
2305 tp_features.bay_status2 = bay2_handle &&
2306 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002307
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002308 tp_features.bay_eject = bay_handle && bay_ej_handle &&
2309 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
2310 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
2311 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002313 vdbg_printk(TPACPI_DBG_INIT,
2314 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002315 str_supported(tp_features.bay_status),
2316 str_supported(tp_features.bay_eject),
2317 str_supported(tp_features.bay_status2),
2318 str_supported(tp_features.bay_eject2));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002319
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002320 return (tp_features.bay_status || tp_features.bay_eject ||
2321 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322}
2323
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002324static void bay_notify(struct ibm_struct *ibm, u32 event)
2325{
Len Brown14e04fb2007-08-23 15:20:26 -04002326 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
Zhang Rui962ce8c2007-08-23 01:24:31 +08002327 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
2328 ibm->acpi->device->dev.bus_id,
2329 event, 0);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002330}
2331
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002332#define bay_occupied(b) (_sta(b##_handle) & 1)
2333
2334static int bay_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335{
2336 int len = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002337 int occupied = bay_occupied(bay);
2338 int occupied2 = bay_occupied(bay2);
2339 int eject, eject2;
2340
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002341 len += sprintf(p + len, "status:\t\t%s\n",
2342 tp_features.bay_status ?
2343 (occupied ? "occupied" : "unoccupied") :
2344 "not supported");
2345 if (tp_features.bay_status2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002346 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
2347 "occupied" : "unoccupied");
2348
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002349 eject = tp_features.bay_eject && occupied;
2350 eject2 = tp_features.bay_eject2 && occupied2;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002351
2352 if (eject && eject2)
2353 len += sprintf(p + len, "commands:\teject, eject2\n");
2354 else if (eject)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 len += sprintf(p + len, "commands:\teject\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002356 else if (eject2)
2357 len += sprintf(p + len, "commands:\teject2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358
2359 return len;
2360}
2361
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002362static int bay_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363{
2364 char *cmd;
2365
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002366 if (!tp_features.bay_eject && !tp_features.bay_eject2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002367 return -ENODEV;
2368
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002370 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002371 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
2372 return -EIO;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002373 } else if (tp_features.bay_eject2 &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002374 strlencmp(cmd, "eject2") == 0) {
2375 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 return -EIO;
2377 } else
2378 return -EINVAL;
2379 }
2380
2381 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002382}
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002383
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002384static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
2385 .notify = bay_notify,
2386 .handle = &bay_handle,
2387 .type = ACPI_SYSTEM_NOTIFY,
2388};
2389
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002390static struct ibm_struct bay_driver_data = {
2391 .name = "bay",
2392 .read = bay_read,
2393 .write = bay_write,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002394 .acpi = &ibm_bay_acpidriver,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002395};
2396
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03002397#endif /* CONFIG_THINKPAD_ACPI_BAY */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002399/*************************************************************************
2400 * CMOS subdriver
2401 */
2402
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03002403/* sysfs cmos_command -------------------------------------------------- */
2404static ssize_t cmos_command_store(struct device *dev,
2405 struct device_attribute *attr,
2406 const char *buf, size_t count)
2407{
2408 unsigned long cmos_cmd;
2409 int res;
2410
2411 if (parse_strtoul(buf, 21, &cmos_cmd))
2412 return -EINVAL;
2413
2414 res = issue_thinkpad_cmos_command(cmos_cmd);
2415 return (res)? res : count;
2416}
2417
2418static struct device_attribute dev_attr_cmos_command =
2419 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
2420
2421/* --------------------------------------------------------------------- */
2422
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002423static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002424{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03002425 int res;
2426
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002427 vdbg_printk(TPACPI_DBG_INIT,
2428 "initializing cmos commands subdriver\n");
2429
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002430 IBM_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002431
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002432 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
2433 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03002434
2435 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
2436 if (res)
2437 return res;
2438
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002439 return (cmos_handle)? 0 : 1;
2440}
2441
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03002442static void cmos_exit(void)
2443{
2444 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
2445}
2446
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002447static int cmos_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448{
2449 int len = 0;
2450
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002451 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2452 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 if (!cmos_handle)
2454 len += sprintf(p + len, "status:\t\tnot supported\n");
2455 else {
2456 len += sprintf(p + len, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002457 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 }
2459
2460 return len;
2461}
2462
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002463static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464{
2465 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03002466 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467
2468 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002469 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
2470 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 /* cmos_cmd set */
2472 } else
2473 return -EINVAL;
2474
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03002475 res = issue_thinkpad_cmos_command(cmos_cmd);
2476 if (res)
2477 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 }
2479
2480 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002481}
2482
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002483static struct ibm_struct cmos_driver_data = {
2484 .name = "cmos",
2485 .read = cmos_read,
2486 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03002487 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002488};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002489
2490/*************************************************************************
2491 * LED subdriver
2492 */
2493
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02002494static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002495
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002496IBM_HANDLE(led, ec, "SLED", /* 570 */
2497 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
2498 "LED", /* all others */
2499 ); /* R30, R31 */
2500
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002501static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002502{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002503 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
2504
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002505 IBM_ACPIHANDLE_INIT(led);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002506
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002507 if (!led_handle)
2508 /* led not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002509 led_supported = TPACPI_LED_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002510 else if (strlencmp(led_path, "SLED") == 0)
2511 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002512 led_supported = TPACPI_LED_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002513 else if (strlencmp(led_path, "SYSL") == 0)
2514 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002515 led_supported = TPACPI_LED_OLD;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002516 else
2517 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002518 led_supported = TPACPI_LED_NEW;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002519
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002520 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
2521 str_supported(led_supported), led_supported);
2522
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002523 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002524}
2525
2526#define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
2527
2528static int led_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529{
2530 int len = 0;
2531
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002532 if (!led_supported) {
2533 len += sprintf(p + len, "status:\t\tnot supported\n");
2534 return len;
2535 }
2536 len += sprintf(p + len, "status:\t\tsupported\n");
2537
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002538 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002539 /* 570 */
2540 int i, status;
2541 for (i = 0; i < 8; i++) {
2542 if (!acpi_evalf(ec_handle,
2543 &status, "GLED", "dd", 1 << i))
2544 return -EIO;
2545 len += sprintf(p + len, "%d:\t\t%s\n",
2546 i, led_status(status));
2547 }
2548 }
2549
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 len += sprintf(p + len, "commands:\t"
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002551 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552
2553 return len;
2554}
2555
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002556/* off, on, blink */
2557static const int led_sled_arg1[] = { 0, 1, 3 };
2558static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
2559static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
2560static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
2561
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002562static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563{
2564 char *cmd;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002565 int led, ind, ret;
2566
2567 if (!led_supported)
2568 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569
2570 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002571 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 return -EINVAL;
2573
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002574 if (strstr(cmd, "off")) {
2575 ind = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 } else if (strstr(cmd, "on")) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002577 ind = 1;
2578 } else if (strstr(cmd, "blink")) {
2579 ind = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 } else
2581 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002582
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002583 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002584 /* 570 */
2585 led = 1 << led;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002587 led, led_sled_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 return -EIO;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002589 } else if (led_supported == TPACPI_LED_OLD) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002590 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
2591 led = 1 << led;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002592 ret = ec_write(TPACPI_LED_EC_HLMS, led);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002593 if (ret >= 0)
2594 ret =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002595 ec_write(TPACPI_LED_EC_HLBL,
Henrique de Moraes Holschuhe062e032007-03-23 17:33:55 -03002596 led * led_exp_hlbl[ind]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002597 if (ret >= 0)
2598 ret =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002599 ec_write(TPACPI_LED_EC_HLCL,
Henrique de Moraes Holschuhe062e032007-03-23 17:33:55 -03002600 led * led_exp_hlcl[ind]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002601 if (ret < 0)
2602 return ret;
2603 } else {
2604 /* all others */
2605 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
2606 led, led_led_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002608 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 }
2610
2611 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002612}
2613
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002614static struct ibm_struct led_driver_data = {
2615 .name = "led",
2616 .read = led_read,
2617 .write = led_write,
2618};
2619
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002620/*************************************************************************
2621 * Beep subdriver
2622 */
2623
2624IBM_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
2625
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002626static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002627{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002628 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
2629
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002630 IBM_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002631
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002632 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
2633 str_supported(beep_handle != NULL));
2634
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002635 return (beep_handle)? 0 : 1;
2636}
2637
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002638static int beep_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639{
2640 int len = 0;
2641
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002642 if (!beep_handle)
2643 len += sprintf(p + len, "status:\t\tnot supported\n");
2644 else {
2645 len += sprintf(p + len, "status:\t\tsupported\n");
2646 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
2647 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648
2649 return len;
2650}
2651
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002652static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653{
2654 char *cmd;
2655 int beep_cmd;
2656
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002657 if (!beep_handle)
2658 return -ENODEV;
2659
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002661 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
2662 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 /* beep_cmd set */
2664 } else
2665 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002666 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 return -EIO;
2668 }
2669
2670 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002671}
2672
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002673static struct ibm_struct beep_driver_data = {
2674 .name = "beep",
2675 .read = beep_read,
2676 .write = beep_write,
2677};
2678
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002679/*************************************************************************
2680 * Thermal subdriver
2681 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002682
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002683static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002684
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03002685/* sysfs temp##_input -------------------------------------------------- */
2686
2687static ssize_t thermal_temp_input_show(struct device *dev,
2688 struct device_attribute *attr,
2689 char *buf)
2690{
2691 struct sensor_device_attribute *sensor_attr =
2692 to_sensor_dev_attr(attr);
2693 int idx = sensor_attr->index;
2694 s32 value;
2695 int res;
2696
2697 res = thermal_get_sensor(idx, &value);
2698 if (res)
2699 return res;
2700 if (value == TP_EC_THERMAL_TMP_NA * 1000)
2701 return -ENXIO;
2702
2703 return snprintf(buf, PAGE_SIZE, "%d\n", value);
2704}
2705
2706#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
2707 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, thermal_temp_input_show, NULL, _idxB)
2708
2709static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
2710 THERMAL_SENSOR_ATTR_TEMP(1, 0),
2711 THERMAL_SENSOR_ATTR_TEMP(2, 1),
2712 THERMAL_SENSOR_ATTR_TEMP(3, 2),
2713 THERMAL_SENSOR_ATTR_TEMP(4, 3),
2714 THERMAL_SENSOR_ATTR_TEMP(5, 4),
2715 THERMAL_SENSOR_ATTR_TEMP(6, 5),
2716 THERMAL_SENSOR_ATTR_TEMP(7, 6),
2717 THERMAL_SENSOR_ATTR_TEMP(8, 7),
2718 THERMAL_SENSOR_ATTR_TEMP(9, 8),
2719 THERMAL_SENSOR_ATTR_TEMP(10, 9),
2720 THERMAL_SENSOR_ATTR_TEMP(11, 10),
2721 THERMAL_SENSOR_ATTR_TEMP(12, 11),
2722 THERMAL_SENSOR_ATTR_TEMP(13, 12),
2723 THERMAL_SENSOR_ATTR_TEMP(14, 13),
2724 THERMAL_SENSOR_ATTR_TEMP(15, 14),
2725 THERMAL_SENSOR_ATTR_TEMP(16, 15),
2726};
2727
2728#define THERMAL_ATTRS(X) \
2729 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
2730
2731static struct attribute *thermal_temp_input_attr[] = {
2732 THERMAL_ATTRS(8),
2733 THERMAL_ATTRS(9),
2734 THERMAL_ATTRS(10),
2735 THERMAL_ATTRS(11),
2736 THERMAL_ATTRS(12),
2737 THERMAL_ATTRS(13),
2738 THERMAL_ATTRS(14),
2739 THERMAL_ATTRS(15),
2740 THERMAL_ATTRS(0),
2741 THERMAL_ATTRS(1),
2742 THERMAL_ATTRS(2),
2743 THERMAL_ATTRS(3),
2744 THERMAL_ATTRS(4),
2745 THERMAL_ATTRS(5),
2746 THERMAL_ATTRS(6),
2747 THERMAL_ATTRS(7),
2748 NULL
2749};
2750
2751static const struct attribute_group thermal_temp_input16_group = {
2752 .attrs = thermal_temp_input_attr
2753};
2754
2755static const struct attribute_group thermal_temp_input8_group = {
2756 .attrs = &thermal_temp_input_attr[8]
2757};
2758
2759#undef THERMAL_SENSOR_ATTR_TEMP
2760#undef THERMAL_ATTRS
2761
2762/* --------------------------------------------------------------------- */
2763
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002764static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002765{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002766 u8 t, ta1, ta2;
2767 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002768 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03002769 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002770
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002771 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
2772
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002773 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002774
Henrique de Moraes Holschuh3d6f99c2007-07-18 23:45:46 -03002775 if (thinkpad_id.ec_model) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002776 /*
2777 * Direct EC access mode: sensors at registers
2778 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
2779 * non-implemented, thermal sensors return 0x80 when
2780 * not available
2781 */
2782
2783 ta1 = ta2 = 0;
2784 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002785 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002786 ta1 |= t;
2787 } else {
2788 ta1 = 0;
2789 break;
2790 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002791 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002792 ta2 |= t;
2793 } else {
2794 ta1 = 0;
2795 break;
2796 }
2797 }
2798 if (ta1 == 0) {
2799 /* This is sheer paranoia, but we handle it anyway */
2800 if (acpi_tmp7) {
2801 printk(IBM_ERR
2802 "ThinkPad ACPI EC access misbehaving, "
2803 "falling back to ACPI TMPx access mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002804 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002805 } else {
2806 printk(IBM_ERR
2807 "ThinkPad ACPI EC access misbehaving, "
2808 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002809 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002810 }
2811 } else {
2812 thermal_read_mode =
2813 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002814 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002815 }
2816 } else if (acpi_tmp7) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002817 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
2818 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002819 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002820 } else {
2821 /* Standard ACPI TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002822 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002823 }
2824 } else {
2825 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002826 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002827 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002828
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002829 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
2830 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
2831 thermal_read_mode);
2832
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03002833 switch(thermal_read_mode) {
2834 case TPACPI_THERMAL_TPEC_16:
2835 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2836 &thermal_temp_input16_group);
2837 if (res)
2838 return res;
2839 break;
2840 case TPACPI_THERMAL_TPEC_8:
2841 case TPACPI_THERMAL_ACPI_TMP07:
2842 case TPACPI_THERMAL_ACPI_UPDT:
2843 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2844 &thermal_temp_input8_group);
2845 if (res)
2846 return res;
2847 break;
2848 case TPACPI_THERMAL_NONE:
2849 default:
2850 return 1;
2851 }
2852
2853 return 0;
2854}
2855
2856static void thermal_exit(void)
2857{
2858 switch(thermal_read_mode) {
2859 case TPACPI_THERMAL_TPEC_16:
2860 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2861 &thermal_temp_input16_group);
2862 break;
2863 case TPACPI_THERMAL_TPEC_8:
2864 case TPACPI_THERMAL_ACPI_TMP07:
2865 case TPACPI_THERMAL_ACPI_UPDT:
2866 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2867 &thermal_temp_input16_group);
2868 break;
2869 case TPACPI_THERMAL_NONE:
2870 default:
2871 break;
2872 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002873}
2874
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002875/* idx is zero-based */
2876static int thermal_get_sensor(int idx, s32 *value)
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002877{
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002878 int t;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002879 s8 tmp;
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002880 char tmpi[5];
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002881
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002882 t = TP_EC_THERMAL_TMP0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002883
2884 switch (thermal_read_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002885#if TPACPI_MAX_THERMAL_SENSORS >= 16
2886 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002887 if (idx >= 8 && idx <= 15) {
2888 t = TP_EC_THERMAL_TMP8;
2889 idx -= 8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002890 }
2891 /* fallthrough */
2892#endif
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002893 case TPACPI_THERMAL_TPEC_8:
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002894 if (idx <= 7) {
2895 if (!acpi_ec_read(t + idx, &tmp))
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002896 return -EIO;
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002897 *value = tmp * 1000;
2898 return 0;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002899 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002900 break;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002901
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002902 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002903 if (idx <= 7) {
2904 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
2905 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
2906 return -EIO;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002907 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
2908 return -EIO;
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002909 *value = (t - 2732) * 100;
2910 return 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002911 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002912 break;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002913
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002914 case TPACPI_THERMAL_ACPI_TMP07:
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002915 if (idx <= 7) {
2916 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002917 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
2918 return -EIO;
Henrique de Moraes Holschuha8fba3da2007-07-18 23:45:45 -03002919 if (t > 127 || t < -127)
2920 t = TP_EC_THERMAL_TMP_NA;
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002921 *value = t * 1000;
2922 return 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002923 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002924 break;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002925
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002926 case TPACPI_THERMAL_NONE:
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002927 default:
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002928 return -ENOSYS;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002929 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002930
2931 return -EINVAL;
2932}
2933
2934static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
2935{
2936 int res, i;
2937 int n;
2938
2939 n = 8;
2940 i = 0;
2941
2942 if (!s)
2943 return -EINVAL;
2944
2945 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
2946 n = 16;
2947
2948 for(i = 0 ; i < n; i++) {
2949 res = thermal_get_sensor(i, &s->temp[i]);
2950 if (res)
2951 return res;
2952 }
2953
2954 return n;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002955}
2956
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002957static int thermal_read(char *p)
2958{
2959 int len = 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002960 int n, i;
2961 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002962
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002963 n = thermal_get_sensors(&t);
2964 if (unlikely(n < 0))
2965 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002966
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002967 len += sprintf(p + len, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002968
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002969 if (n > 0) {
2970 for (i = 0; i < (n - 1); i++)
2971 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
2972 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
2973 } else
2974 len += sprintf(p + len, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002975
2976 return len;
2977}
2978
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002979static struct ibm_struct thermal_driver_data = {
2980 .name = "thermal",
2981 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03002982 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002983};
2984
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002985/*************************************************************************
2986 * EC Dump subdriver
2987 */
2988
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002989static u8 ecdump_regs[256];
2990
2991static int ecdump_read(char *p)
2992{
2993 int len = 0;
2994 int i, j;
2995 u8 v;
2996
2997 len += sprintf(p + len, "EC "
2998 " +00 +01 +02 +03 +04 +05 +06 +07"
2999 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
3000 for (i = 0; i < 256; i += 16) {
3001 len += sprintf(p + len, "EC 0x%02x:", i);
3002 for (j = 0; j < 16; j++) {
3003 if (!acpi_ec_read(i + j, &v))
3004 break;
3005 if (v != ecdump_regs[i + j])
3006 len += sprintf(p + len, " *%02x", v);
3007 else
3008 len += sprintf(p + len, " %02x", v);
3009 ecdump_regs[i + j] = v;
3010 }
3011 len += sprintf(p + len, "\n");
3012 if (j != 16)
3013 break;
3014 }
3015
3016 /* These are way too dangerous to advertise openly... */
3017#if 0
3018 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
3019 " (<offset> is 00-ff, <value> is 00-ff)\n");
3020 len += sprintf(p + len, "commands:\t0x<offset> <value> "
3021 " (<offset> is 00-ff, <value> is 0-255)\n");
3022#endif
3023 return len;
3024}
3025
3026static int ecdump_write(char *buf)
3027{
3028 char *cmd;
3029 int i, v;
3030
3031 while ((cmd = next_cmd(&buf))) {
3032 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
3033 /* i and v set */
3034 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
3035 /* i and v set */
3036 } else
3037 return -EINVAL;
3038 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
3039 if (!acpi_ec_write(i, v))
3040 return -EIO;
3041 } else
3042 return -EINVAL;
3043 }
3044
3045 return 0;
3046}
3047
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003048static struct ibm_struct ecdump_driver_data = {
3049 .name = "ecdump",
3050 .read = ecdump_read,
3051 .write = ecdump_write,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03003052 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003053};
3054
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003055/*************************************************************************
3056 * Backlight/brightness subdriver
3057 */
Holger Macht8acb0252006-10-20 14:30:28 -07003058
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03003059static struct backlight_device *ibm_backlight_device;
Holger Macht8acb0252006-10-20 14:30:28 -07003060
Richard Purdie599a52d2007-02-10 23:07:48 +00003061static struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02003062 .get_brightness = brightness_get,
3063 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02003064};
3065
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03003066static struct mutex brightness_mutex;
3067
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003068static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02003069{
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02003070 int b;
3071
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003072 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
3073
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03003074 mutex_init(&brightness_mutex);
3075
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03003076 if (!brightness_mode) {
3077 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
3078 brightness_mode = 2;
3079 else
3080 brightness_mode = 3;
3081
3082 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
3083 brightness_mode);
3084 }
3085
3086 if (brightness_mode > 3)
3087 return -EINVAL;
3088
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02003089 b = brightness_get(NULL);
3090 if (b < 0)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03003091 return 1;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02003092
Henrique de Moraes Holschuh7d5a0152007-04-24 11:48:20 -03003093 ibm_backlight_device = backlight_device_register(
3094 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
3095 &ibm_backlight_data);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02003096 if (IS_ERR(ibm_backlight_device)) {
3097 printk(IBM_ERR "Could not register backlight device\n");
3098 return PTR_ERR(ibm_backlight_device);
3099 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003100 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02003101
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02003102 ibm_backlight_device->props.max_brightness = 7;
3103 ibm_backlight_device->props.brightness = b;
3104 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00003105
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02003106 return 0;
3107}
3108
3109static void brightness_exit(void)
3110{
3111 if (ibm_backlight_device) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003112 vdbg_printk(TPACPI_DBG_EXIT,
3113 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02003114 backlight_device_unregister(ibm_backlight_device);
3115 ibm_backlight_device = NULL;
3116 }
3117}
3118
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003119static int brightness_update_status(struct backlight_device *bd)
3120{
3121 return brightness_set(
3122 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
3123 bd->props.power == FB_BLANK_UNBLANK) ?
3124 bd->props.brightness : 0);
3125}
3126
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03003127/*
3128 * ThinkPads can read brightness from two places: EC 0x31, or
3129 * CMOS NVRAM byte 0x5E, bits 0-3.
3130 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003131static int brightness_get(struct backlight_device *bd)
3132{
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03003133 u8 lec = 0, lcmos = 0, level = 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003134
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03003135 if (brightness_mode & 1) {
3136 if (!acpi_ec_read(brightness_offset, &lec))
3137 return -EIO;
3138 lec &= 7;
3139 level = lec;
3140 };
3141 if (brightness_mode & 2) {
3142 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
3143 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
3144 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
3145 level = lcmos;
3146 }
3147
3148 if (brightness_mode == 3 && lec != lcmos) {
3149 printk(IBM_ERR
3150 "CMOS NVRAM (%u) and EC (%u) do not agree "
3151 "on display brightness level\n",
3152 (unsigned int) lcmos,
3153 (unsigned int) lec);
3154 return -EIO;
3155 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003156
3157 return level;
3158}
3159
3160static int brightness_set(int value)
3161{
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03003162 int cmos_cmd, inc, i, res;
3163 int current_value;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003164
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03003165 if (value > 7)
3166 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003167
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03003168 res = mutex_lock_interruptible(&brightness_mutex);
3169 if (res < 0)
3170 return res;
3171
3172 current_value = brightness_get(NULL);
3173 if (current_value < 0) {
3174 res = current_value;
3175 goto errout;
3176 }
3177
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03003178 cmos_cmd = value > current_value ?
3179 TP_CMOS_BRIGHTNESS_UP :
3180 TP_CMOS_BRIGHTNESS_DOWN;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003181 inc = value > current_value ? 1 : -1;
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03003182
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03003183 res = 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003184 for (i = current_value; i != value; i += inc) {
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03003185 if ((brightness_mode & 2) &&
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03003186 issue_thinkpad_cmos_command(cmos_cmd)) {
3187 res = -EIO;
3188 goto errout;
3189 }
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03003190 if ((brightness_mode & 1) &&
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03003191 !acpi_ec_write(brightness_offset, i + inc)) {
3192 res = -EIO;
3193 goto errout;;
3194 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003195 }
3196
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03003197errout:
3198 mutex_unlock(&brightness_mutex);
3199 return res;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003200}
3201
3202static int brightness_read(char *p)
3203{
3204 int len = 0;
3205 int level;
3206
3207 if ((level = brightness_get(NULL)) < 0) {
3208 len += sprintf(p + len, "level:\t\tunreadable\n");
3209 } else {
3210 len += sprintf(p + len, "level:\t\t%d\n", level & 0x7);
3211 len += sprintf(p + len, "commands:\tup, down\n");
3212 len += sprintf(p + len, "commands:\tlevel <level>"
3213 " (<level> is 0-7)\n");
3214 }
3215
3216 return len;
3217}
3218
3219static int brightness_write(char *buf)
3220{
3221 int level;
3222 int new_level;
3223 char *cmd;
3224
3225 while ((cmd = next_cmd(&buf))) {
3226 if ((level = brightness_get(NULL)) < 0)
3227 return level;
3228 level &= 7;
3229
3230 if (strlencmp(cmd, "up") == 0) {
3231 new_level = level == 7 ? 7 : level + 1;
3232 } else if (strlencmp(cmd, "down") == 0) {
3233 new_level = level == 0 ? 0 : level - 1;
3234 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
3235 new_level >= 0 && new_level <= 7) {
3236 /* new_level set */
3237 } else
3238 return -EINVAL;
3239
3240 brightness_set(new_level);
3241 }
3242
3243 return 0;
3244}
3245
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003246static struct ibm_struct brightness_driver_data = {
3247 .name = "brightness",
3248 .read = brightness_read,
3249 .write = brightness_write,
3250 .exit = brightness_exit,
3251};
3252
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003253/*************************************************************************
3254 * Volume subdriver
3255 */
3256
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003257static int volume_read(char *p)
3258{
3259 int len = 0;
3260 u8 level;
3261
3262 if (!acpi_ec_read(volume_offset, &level)) {
3263 len += sprintf(p + len, "level:\t\tunreadable\n");
3264 } else {
3265 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
3266 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
3267 len += sprintf(p + len, "commands:\tup, down, mute\n");
3268 len += sprintf(p + len, "commands:\tlevel <level>"
3269 " (<level> is 0-15)\n");
3270 }
3271
3272 return len;
3273}
3274
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003275static int volume_write(char *buf)
3276{
3277 int cmos_cmd, inc, i;
3278 u8 level, mute;
3279 int new_level, new_mute;
3280 char *cmd;
3281
3282 while ((cmd = next_cmd(&buf))) {
3283 if (!acpi_ec_read(volume_offset, &level))
3284 return -EIO;
3285 new_mute = mute = level & 0x40;
3286 new_level = level = level & 0xf;
3287
3288 if (strlencmp(cmd, "up") == 0) {
3289 if (mute)
3290 new_mute = 0;
3291 else
3292 new_level = level == 15 ? 15 : level + 1;
3293 } else if (strlencmp(cmd, "down") == 0) {
3294 if (mute)
3295 new_mute = 0;
3296 else
3297 new_level = level == 0 ? 0 : level - 1;
3298 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
3299 new_level >= 0 && new_level <= 15) {
3300 /* new_level set */
3301 } else if (strlencmp(cmd, "mute") == 0) {
3302 new_mute = 0x40;
3303 } else
3304 return -EINVAL;
3305
3306 if (new_level != level) { /* mute doesn't change */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003307 cmos_cmd = new_level > level ? TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003308 inc = new_level > level ? 1 : -1;
3309
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03003310 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003311 !acpi_ec_write(volume_offset, level)))
3312 return -EIO;
3313
3314 for (i = level; i != new_level; i += inc)
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03003315 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003316 !acpi_ec_write(volume_offset, i + inc))
3317 return -EIO;
3318
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03003319 if (mute && (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003320 !acpi_ec_write(volume_offset,
3321 new_level + mute)))
3322 return -EIO;
3323 }
3324
3325 if (new_mute != mute) { /* level doesn't change */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003326 cmos_cmd = new_mute ? TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003327
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03003328 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003329 !acpi_ec_write(volume_offset, level + new_mute))
3330 return -EIO;
3331 }
3332 }
3333
3334 return 0;
3335}
3336
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003337static struct ibm_struct volume_driver_data = {
3338 .name = "volume",
3339 .read = volume_read,
3340 .write = volume_write,
3341};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003342
3343/*************************************************************************
3344 * Fan subdriver
3345 */
3346
3347/*
3348 * FAN ACCESS MODES
3349 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003350 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003351 * ACPI GFAN method: returns fan level
3352 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003353 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03003354 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003355 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003356 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003357 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
3358 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03003359 * EC 0x2f (HFSP) might be available *for reading*, but do not use
3360 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003361 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003362 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03003363 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
3364 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003365 *
3366 * Fan speed changes of any sort (including those caused by the
3367 * disengaged mode) are usually done slowly by the firmware as the
3368 * maximum ammount of fan duty cycle change per second seems to be
3369 * limited.
3370 *
3371 * Reading is not available if GFAN exists.
3372 * Writing is not available if SFAN exists.
3373 *
3374 * Bits
3375 * 7 automatic mode engaged;
3376 * (default operation mode of the ThinkPad)
3377 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03003378 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003379 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03003380 * the tachometer while the fan controller ramps up
3381 * the speed (which can take up to a few *minutes*).
3382 * Speeds up fan to 100% duty-cycle, which is far above
3383 * the standard RPM levels. It is not impossible that
3384 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003385 * 5-3 unused in some models. Extra bits for fan level
3386 * in others, but still useless as all values above
3387 * 7 map to the same speed as level 7 in these models.
3388 * 2-0 fan level (0..7 usually)
3389 * 0x00 = stop
3390 * 0x07 = max (set when temperatures critical)
3391 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003392 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003393 *
3394 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
3395 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
3396 * does so, its initial value is meaningless (0x07).
3397 *
3398 * For firmware bugs, refer to:
3399 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
3400 *
3401 * ----
3402 *
3403 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
3404 * Main fan tachometer reading (in RPM)
3405 *
3406 * This register is present on all ThinkPads with a new-style EC, and
3407 * it is known not to be present on the A21m/e, and T22, as there is
3408 * something else in offset 0x84 according to the ACPI DSDT. Other
3409 * ThinkPads from this same time period (and earlier) probably lack the
3410 * tachometer as well.
3411 *
3412 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
3413 * was never fixed by IBM to report the EC firmware version string
3414 * probably support the tachometer (like the early X models), so
3415 * detecting it is quite hard. We need more data to know for sure.
3416 *
3417 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
3418 * might result.
3419 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03003420 * FIRMWARE BUG: may go stale while the EC is switching to full speed
3421 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003422 *
3423 * For firmware bugs, refer to:
3424 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
3425 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003426 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003427 * ThinkPad X31, X40, X41. Not available in the X60.
3428 *
3429 * FANS ACPI handle: takes three arguments: low speed, medium speed,
3430 * high speed. ACPI DSDT seems to map these three speeds to levels
3431 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
3432 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
3433 *
3434 * The speeds are stored on handles
3435 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
3436 *
3437 * There are three default speed sets, acessible as handles:
3438 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
3439 *
3440 * ACPI DSDT switches which set is in use depending on various
3441 * factors.
3442 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003443 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003444 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
3445 * but the ACPI tables just mention level 7.
3446 */
3447
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003448static enum fan_status_access_mode fan_status_access_mode;
3449static enum fan_control_access_mode fan_control_access_mode;
3450static enum fan_control_commands fan_control_commands;
3451
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02003452static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003453static u8 fan_control_desired_level;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02003454
Len Brown25c68a32006-12-08 04:43:41 -05003455static void fan_watchdog_fire(struct work_struct *ignored);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003456static int fan_watchdog_maxinterval;
Len Brown25c68a32006-12-08 04:43:41 -05003457static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003458
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003459IBM_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
3460IBM_HANDLE(gfan, ec, "GFAN", /* 570 */
3461 "\\FSPD", /* 600e/x, 770e, 770x */
3462 ); /* all others */
3463IBM_HANDLE(sfan, ec, "SFAN", /* 570 */
3464 "JFNS", /* 770x-JL */
3465 ); /* all others */
3466
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003467/*
3468 * SYSFS fan layout: hwmon compatible (device)
3469 *
3470 * pwm*_enable:
3471 * 0: "disengaged" mode
3472 * 1: manual mode
3473 * 2: native EC "auto" mode (recommended, hardware default)
3474 *
3475 * pwm*: set speed in manual mode, ignored otherwise.
3476 * 0 is level 0; 255 is level 7. Intermediate points done with linear
3477 * interpolation.
3478 *
3479 * fan*_input: tachometer reading, RPM
3480 *
3481 *
3482 * SYSFS fan layout: extensions
3483 *
3484 * fan_watchdog (driver):
3485 * fan watchdog interval in seconds, 0 disables (default), max 120
3486 */
3487
3488/* sysfs fan pwm1_enable ----------------------------------------------- */
3489static ssize_t fan_pwm1_enable_show(struct device *dev,
3490 struct device_attribute *attr,
3491 char *buf)
3492{
3493 int res, mode;
3494 u8 status;
3495
3496 res = fan_get_status_safe(&status);
3497 if (res)
3498 return res;
3499
3500 if (unlikely(tp_features.fan_ctrl_status_undef)) {
3501 if (status != fan_control_initial_status) {
3502 tp_features.fan_ctrl_status_undef = 0;
3503 } else {
3504 /* Return most likely status. In fact, it
3505 * might be the only possible status */
3506 status = TP_EC_FAN_AUTO;
3507 }
3508 }
3509
3510 if (status & TP_EC_FAN_FULLSPEED) {
3511 mode = 0;
3512 } else if (status & TP_EC_FAN_AUTO) {
3513 mode = 2;
3514 } else
3515 mode = 1;
3516
3517 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
3518}
3519
3520static ssize_t fan_pwm1_enable_store(struct device *dev,
3521 struct device_attribute *attr,
3522 const char *buf, size_t count)
3523{
3524 unsigned long t;
3525 int res, level;
3526
3527 if (parse_strtoul(buf, 2, &t))
3528 return -EINVAL;
3529
3530 switch (t) {
3531 case 0:
3532 level = TP_EC_FAN_FULLSPEED;
3533 break;
3534 case 1:
3535 level = TPACPI_FAN_LAST_LEVEL;
3536 break;
3537 case 2:
3538 level = TP_EC_FAN_AUTO;
3539 break;
3540 case 3:
3541 /* reserved for software-controlled auto mode */
3542 return -ENOSYS;
3543 default:
3544 return -EINVAL;
3545 }
3546
3547 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03003548 if (res == -ENXIO)
3549 return -EINVAL;
3550 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003551 return res;
3552
3553 fan_watchdog_reset();
3554
3555 return count;
3556}
3557
3558static struct device_attribute dev_attr_fan_pwm1_enable =
3559 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
3560 fan_pwm1_enable_show, fan_pwm1_enable_store);
3561
3562/* sysfs fan pwm1 ------------------------------------------------------ */
3563static ssize_t fan_pwm1_show(struct device *dev,
3564 struct device_attribute *attr,
3565 char *buf)
3566{
3567 int res;
3568 u8 status;
3569
3570 res = fan_get_status_safe(&status);
3571 if (res)
3572 return res;
3573
3574 if (unlikely(tp_features.fan_ctrl_status_undef)) {
3575 if (status != fan_control_initial_status) {
3576 tp_features.fan_ctrl_status_undef = 0;
3577 } else {
3578 status = TP_EC_FAN_AUTO;
3579 }
3580 }
3581
3582 if ((status &
3583 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
3584 status = fan_control_desired_level;
3585
3586 if (status > 7)
3587 status = 7;
3588
3589 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
3590}
3591
3592static ssize_t fan_pwm1_store(struct device *dev,
3593 struct device_attribute *attr,
3594 const char *buf, size_t count)
3595{
3596 unsigned long s;
3597 int rc;
3598 u8 status, newlevel;
3599
3600 if (parse_strtoul(buf, 255, &s))
3601 return -EINVAL;
3602
3603 /* scale down from 0-255 to 0-7 */
3604 newlevel = (s >> 5) & 0x07;
3605
3606 rc = mutex_lock_interruptible(&fan_mutex);
3607 if (rc < 0)
3608 return rc;
3609
3610 rc = fan_get_status(&status);
3611 if (!rc && (status &
3612 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
3613 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03003614 if (rc == -ENXIO)
3615 rc = -EINVAL;
3616 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003617 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03003618 fan_watchdog_reset();
3619 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003620 }
3621
3622 mutex_unlock(&fan_mutex);
3623 return (rc)? rc : count;
3624}
3625
3626static struct device_attribute dev_attr_fan_pwm1 =
3627 __ATTR(pwm1, S_IWUSR | S_IRUGO,
3628 fan_pwm1_show, fan_pwm1_store);
3629
3630/* sysfs fan fan1_input ------------------------------------------------ */
3631static ssize_t fan_fan1_input_show(struct device *dev,
3632 struct device_attribute *attr,
3633 char *buf)
3634{
3635 int res;
3636 unsigned int speed;
3637
3638 res = fan_get_speed(&speed);
3639 if (res < 0)
3640 return res;
3641
3642 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
3643}
3644
3645static struct device_attribute dev_attr_fan_fan1_input =
3646 __ATTR(fan1_input, S_IRUGO,
3647 fan_fan1_input_show, NULL);
3648
3649/* sysfs fan fan_watchdog (driver) ------------------------------------- */
3650static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
3651 char *buf)
3652{
3653 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
3654}
3655
3656static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
3657 const char *buf, size_t count)
3658{
3659 unsigned long t;
3660
3661 if (parse_strtoul(buf, 120, &t))
3662 return -EINVAL;
3663
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03003664 if (!fan_control_allowed)
3665 return -EPERM;
3666
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003667 fan_watchdog_maxinterval = t;
3668 fan_watchdog_reset();
3669
3670 return count;
3671}
3672
3673static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
3674 fan_fan_watchdog_show, fan_fan_watchdog_store);
3675
3676/* --------------------------------------------------------------------- */
3677static struct attribute *fan_attributes[] = {
3678 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
3679 &dev_attr_fan_fan1_input.attr,
3680 NULL
3681};
3682
3683static const struct attribute_group fan_attr_group = {
3684 .attrs = fan_attributes,
3685};
3686
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003687static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003688{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003689 int rc;
3690
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003691 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
3692
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003693 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003694 fan_status_access_mode = TPACPI_FAN_NONE;
3695 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003696 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003697 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003698 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003699 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003700
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003701 IBM_ACPIHANDLE_INIT(fans);
3702 IBM_ACPIHANDLE_INIT(gfan);
3703 IBM_ACPIHANDLE_INIT(sfan);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003704
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003705 if (gfan_handle) {
3706 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003707 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003708 } else {
3709 /* all other ThinkPads: note that even old-style
3710 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02003711 if (likely(acpi_ec_read(fan_status_offset,
3712 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003713 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02003714
3715 /* In some ThinkPads, neither the EC nor the ACPI
3716 * DSDT initialize the fan status, and it ends up
3717 * being set to 0x07 when it *could* be either
3718 * 0x07 or 0x80.
3719 *
3720 * Enable for TP-1Y (T43), TP-78 (R51e),
3721 * TP-76 (R52), TP-70 (T43, R52), which are known
3722 * to be buggy. */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03003723 if (fan_control_initial_status == 0x07) {
3724 switch (thinkpad_id.ec_model) {
3725 case 0x5931: /* TP-1Y */
3726 case 0x3837: /* TP-78 */
3727 case 0x3637: /* TP-76 */
3728 case 0x3037: /* TP-70 */
3729 printk(IBM_NOTICE
3730 "fan_init: initial fan status is "
3731 "unknown, assuming it is in auto "
3732 "mode\n");
3733 tp_features.fan_ctrl_status_undef = 1;
3734 ;;
3735 }
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02003736 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003737 } else {
3738 printk(IBM_ERR
3739 "ThinkPad ACPI EC access misbehaving, "
3740 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003741 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003742 }
3743 }
3744
3745 if (sfan_handle) {
3746 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003747 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02003748 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003749 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003750 } else {
3751 if (!gfan_handle) {
3752 /* gfan without sfan means no fan control */
3753 /* all other models implement TP EC 0x2f control */
3754
3755 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02003756 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003757 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003758 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003759 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003760 TPACPI_FAN_CMD_SPEED |
3761 TPACPI_FAN_CMD_LEVEL |
3762 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003763 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003764 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02003765 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003766 TPACPI_FAN_CMD_LEVEL |
3767 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003768 }
3769 }
3770 }
3771
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003772 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
3773 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
3774 fan_control_access_mode != TPACPI_FAN_WR_NONE),
3775 fan_status_access_mode, fan_control_access_mode);
3776
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03003777 /* fan control master switch */
3778 if (!fan_control_allowed) {
3779 fan_control_access_mode = TPACPI_FAN_WR_NONE;
3780 fan_control_commands = 0;
3781 dbg_printk(TPACPI_DBG_INIT,
3782 "fan control features disabled by parameter\n");
3783 }
3784
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003785 /* update fan_control_desired_level */
3786 if (fan_status_access_mode != TPACPI_FAN_NONE)
3787 fan_get_status_safe(NULL);
3788
3789 if (fan_status_access_mode != TPACPI_FAN_NONE ||
3790 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
3791 rc = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3792 &fan_attr_group);
3793 if (!(rc < 0))
3794 rc = driver_create_file(&tpacpi_pdriver.driver,
3795 &driver_attr_fan_watchdog);
3796 if (rc < 0)
3797 return rc;
3798 return 0;
3799 } else
3800 return 1;
3801}
3802
3803/*
3804 * Call with fan_mutex held
3805 */
3806static void fan_update_desired_level(u8 status)
3807{
3808 if ((status &
3809 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
3810 if (status > 7)
3811 fan_control_desired_level = 7;
3812 else
3813 fan_control_desired_level = status;
3814 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003815}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003816
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02003817static int fan_get_status(u8 *status)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003818{
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02003819 u8 s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003820
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02003821 /* TODO:
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003822 * Add TPACPI_FAN_RD_ACPI_FANS ? */
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02003823
Henrique de Moraes Holschuh3ef8a602006-11-24 11:47:10 -02003824 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003825 case TPACPI_FAN_RD_ACPI_GFAN:
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003826 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02003827
3828 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003829 return -EIO;
3830
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02003831 if (likely(status))
3832 *status = s & 0x07;
3833
3834 break;
3835
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003836 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02003837 /* all except 570, 600e/x, 770e, 770x */
3838 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
3839 return -EIO;
3840
3841 if (likely(status))
3842 *status = s;
3843
3844 break;
3845
3846 default:
3847 return -ENXIO;
3848 }
3849
3850 return 0;
3851}
3852
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003853static int fan_get_status_safe(u8 *status)
3854{
3855 int rc;
3856 u8 s;
3857
3858 rc = mutex_lock_interruptible(&fan_mutex);
3859 if (rc < 0)
3860 return rc;
3861 rc = fan_get_status(&s);
3862 if (!rc)
3863 fan_update_desired_level(s);
3864 mutex_unlock(&fan_mutex);
3865
3866 if (status)
3867 *status = s;
3868
3869 return rc;
3870}
3871
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003872static void fan_exit(void)
3873{
Henrique de Moraes Holschuh99fba3f2007-04-21 11:08:44 -03003874 vdbg_printk(TPACPI_DBG_EXIT, "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003875
3876 /* FIXME: can we really do this unconditionally? */
3877 sysfs_remove_group(&tpacpi_pdev->dev.kobj, &fan_attr_group);
3878 driver_remove_file(&tpacpi_pdriver.driver, &driver_attr_fan_watchdog);
3879
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003880 cancel_delayed_work(&fan_watchdog_task);
3881 flush_scheduled_work();
3882}
3883
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02003884static int fan_get_speed(unsigned int *speed)
3885{
3886 u8 hi, lo;
3887
3888 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003889 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02003890 /* all except 570, 600e/x, 770e, 770x */
3891 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
3892 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
3893 return -EIO;
3894
3895 if (likely(speed))
3896 *speed = (hi << 8) | lo;
3897
3898 break;
3899
3900 default:
3901 return -ENXIO;
3902 }
3903
3904 return 0;
3905}
3906
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003907static void fan_watchdog_fire(struct work_struct *ignored)
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003908{
Henrique de Moraes Holschuh99fba3f2007-04-21 11:08:44 -03003909 int rc;
3910
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03003911 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
3912 return;
3913
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003914 printk(IBM_NOTICE "fan watchdog: enabling fan\n");
Henrique de Moraes Holschuh99fba3f2007-04-21 11:08:44 -03003915 rc = fan_set_enable();
3916 if (rc < 0) {
3917 printk(IBM_ERR "fan watchdog: error %d while enabling fan, "
3918 "will try again later...\n", -rc);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003919 /* reschedule for later */
3920 fan_watchdog_reset();
3921 }
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003922}
3923
3924static void fan_watchdog_reset(void)
3925{
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03003926 static int fan_watchdog_active;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003927
Henrique de Moraes Holschuh4985cd02007-04-27 22:00:10 -03003928 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
3929 return;
3930
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003931 if (fan_watchdog_active)
3932 cancel_delayed_work(&fan_watchdog_task);
3933
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03003934 if (fan_watchdog_maxinterval > 0 &&
3935 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003936 fan_watchdog_active = 1;
3937 if (!schedule_delayed_work(&fan_watchdog_task,
3938 msecs_to_jiffies(fan_watchdog_maxinterval
3939 * 1000))) {
3940 printk(IBM_ERR "failed to schedule the fan watchdog, "
3941 "watchdog will not trigger\n");
3942 }
3943 } else
3944 fan_watchdog_active = 0;
3945}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003946
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003947static int fan_set_level(int level)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003948{
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03003949 if (!fan_control_allowed)
3950 return -EPERM;
3951
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003952 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003953 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003954 if (level >= 0 && level <= 7) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003955 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003956 return -EIO;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003957 } else
3958 return -EINVAL;
3959 break;
3960
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003961 case TPACPI_FAN_WR_ACPI_FANS:
3962 case TPACPI_FAN_WR_TPEC:
3963 if ((level != TP_EC_FAN_AUTO) &&
3964 (level != TP_EC_FAN_FULLSPEED) &&
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02003965 ((level < 0) || (level > 7)))
3966 return -EINVAL;
3967
Henrique de Moraes Holschuheaa75712007-04-24 11:48:18 -03003968 /* safety net should the EC not support AUTO
3969 * or FULLSPEED mode bits and just ignore them */
3970 if (level & TP_EC_FAN_FULLSPEED)
3971 level |= 7; /* safety min speed 7 */
3972 else if (level & TP_EC_FAN_FULLSPEED)
3973 level |= 4; /* safety min speed 4 */
3974
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003975 if (!acpi_ec_write(fan_status_offset, level))
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02003976 return -EIO;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02003977 else
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003978 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02003979 break;
3980
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003981 default:
3982 return -ENXIO;
3983 }
3984 return 0;
3985}
3986
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003987static int fan_set_level_safe(int level)
3988{
3989 int rc;
3990
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03003991 if (!fan_control_allowed)
3992 return -EPERM;
3993
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003994 rc = mutex_lock_interruptible(&fan_mutex);
3995 if (rc < 0)
3996 return rc;
3997
3998 if (level == TPACPI_FAN_LAST_LEVEL)
3999 level = fan_control_desired_level;
4000
4001 rc = fan_set_level(level);
4002 if (!rc)
4003 fan_update_desired_level(level);
4004
4005 mutex_unlock(&fan_mutex);
4006 return rc;
4007}
4008
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004009static int fan_set_enable(void)
4010{
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02004011 u8 s;
4012 int rc;
4013
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03004014 if (!fan_control_allowed)
4015 return -EPERM;
4016
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004017 rc = mutex_lock_interruptible(&fan_mutex);
4018 if (rc < 0)
4019 return rc;
4020
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004021 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004022 case TPACPI_FAN_WR_ACPI_FANS:
4023 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004024 rc = fan_get_status(&s);
4025 if (rc < 0)
4026 break;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02004027
4028 /* Don't go out of emergency fan mode */
Henrique de Moraes Holschuheaa75712007-04-24 11:48:18 -03004029 if (s != 7) {
4030 s &= 0x07;
4031 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
4032 }
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02004033
4034 if (!acpi_ec_write(fan_status_offset, s))
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004035 rc = -EIO;
4036 else {
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004037 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004038 rc = 0;
4039 }
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02004040 break;
4041
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004042 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004043 rc = fan_get_status(&s);
4044 if (rc < 0)
4045 break;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02004046
4047 s &= 0x07;
4048
4049 /* Set fan to at least level 4 */
Henrique de Moraes Holschuheaa75712007-04-24 11:48:18 -03004050 s |= 4;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02004051
4052 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004053 rc= -EIO;
4054 else
4055 rc = 0;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004056 break;
4057
4058 default:
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004059 rc = -ENXIO;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004060 }
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004061
4062 mutex_unlock(&fan_mutex);
4063 return rc;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004064}
4065
4066static int fan_set_disable(void)
4067{
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004068 int rc;
4069
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03004070 if (!fan_control_allowed)
4071 return -EPERM;
4072
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004073 rc = mutex_lock_interruptible(&fan_mutex);
4074 if (rc < 0)
4075 return rc;
4076
4077 rc = 0;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004078 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004079 case TPACPI_FAN_WR_ACPI_FANS:
4080 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004081 if (!acpi_ec_write(fan_status_offset, 0x00))
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004082 rc = -EIO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004083 else {
4084 fan_control_desired_level = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004085 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004086 }
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004087 break;
4088
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004089 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02004090 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004091 rc = -EIO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004092 else
4093 fan_control_desired_level = 0;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02004094 break;
4095
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004096 default:
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004097 rc = -ENXIO;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004098 }
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004099
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004100
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004101 mutex_unlock(&fan_mutex);
4102 return rc;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004103}
4104
4105static int fan_set_speed(int speed)
4106{
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004107 int rc;
4108
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03004109 if (!fan_control_allowed)
4110 return -EPERM;
4111
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004112 rc = mutex_lock_interruptible(&fan_mutex);
4113 if (rc < 0)
4114 return rc;
4115
4116 rc = 0;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004117 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004118 case TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004119 if (speed >= 0 && speed <= 65535) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004120 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
4121 speed, speed, speed))
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004122 rc = -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004123 } else
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004124 rc = -EINVAL;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004125 break;
4126
4127 default:
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004128 rc = -ENXIO;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004129 }
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004130
4131 mutex_unlock(&fan_mutex);
4132 return rc;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004133}
4134
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004135static int fan_read(char *p)
4136{
4137 int len = 0;
4138 int rc;
4139 u8 status;
4140 unsigned int speed = 0;
4141
4142 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004143 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004144 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004145 if ((rc = fan_get_status_safe(&status)) < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004146 return rc;
4147
4148 len += sprintf(p + len, "status:\t\t%s\n"
4149 "level:\t\t%d\n",
4150 (status != 0) ? "enabled" : "disabled", status);
4151 break;
4152
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004153 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004154 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004155 if ((rc = fan_get_status_safe(&status)) < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004156 return rc;
4157
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004158 if (unlikely(tp_features.fan_ctrl_status_undef)) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004159 if (status != fan_control_initial_status)
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004160 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004161 else
4162 /* Return most likely status. In fact, it
4163 * might be the only possible status */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004164 status = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004165 }
4166
4167 len += sprintf(p + len, "status:\t\t%s\n",
4168 (status != 0) ? "enabled" : "disabled");
4169
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004170 if ((rc = fan_get_speed(&speed)) < 0)
4171 return rc;
4172
4173 len += sprintf(p + len, "speed:\t\t%d\n", speed);
4174
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004175 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004176 /* Disengaged mode takes precedence */
4177 len += sprintf(p + len, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004178 else if (status & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004179 len += sprintf(p + len, "level:\t\tauto\n");
4180 else
4181 len += sprintf(p + len, "level:\t\t%d\n", status);
4182 break;
4183
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004184 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004185 default:
4186 len += sprintf(p + len, "status:\t\tnot supported\n");
4187 }
4188
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004189 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004190 len += sprintf(p + len, "commands:\tlevel <level>");
4191
4192 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004193 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004194 len += sprintf(p + len, " (<level> is 0-7)\n");
4195 break;
4196
4197 default:
4198 len += sprintf(p + len, " (<level> is 0-7, "
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004199 "auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004200 break;
4201 }
4202 }
4203
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004204 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004205 len += sprintf(p + len, "commands:\tenable, disable\n"
4206 "commands:\twatchdog <timeout> (<timeout> is 0 (off), "
4207 "1-120 (seconds))\n");
4208
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004209 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004210 len += sprintf(p + len, "commands:\tspeed <speed>"
4211 " (<speed> is 0-65535)\n");
4212
4213 return len;
4214}
4215
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004216static int fan_write_cmd_level(const char *cmd, int *rc)
4217{
4218 int level;
4219
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02004220 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004221 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004222 else if ((strlencmp(cmd, "level disengaged") == 0) |
4223 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004224 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02004225 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004226 return 0;
4227
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004228 if ((*rc = fan_set_level_safe(level)) == -ENXIO)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004229 printk(IBM_ERR "level command accepted for unsupported "
4230 "access mode %d", fan_control_access_mode);
4231
4232 return 1;
4233}
4234
4235static int fan_write_cmd_enable(const char *cmd, int *rc)
4236{
4237 if (strlencmp(cmd, "enable") != 0)
4238 return 0;
4239
4240 if ((*rc = fan_set_enable()) == -ENXIO)
4241 printk(IBM_ERR "enable command accepted for unsupported "
4242 "access mode %d", fan_control_access_mode);
4243
4244 return 1;
4245}
4246
4247static int fan_write_cmd_disable(const char *cmd, int *rc)
4248{
4249 if (strlencmp(cmd, "disable") != 0)
4250 return 0;
4251
4252 if ((*rc = fan_set_disable()) == -ENXIO)
4253 printk(IBM_ERR "disable command accepted for unsupported "
4254 "access mode %d", fan_control_access_mode);
4255
4256 return 1;
4257}
4258
4259static int fan_write_cmd_speed(const char *cmd, int *rc)
4260{
4261 int speed;
4262
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02004263 /* TODO:
4264 * Support speed <low> <medium> <high> ? */
4265
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004266 if (sscanf(cmd, "speed %d", &speed) != 1)
4267 return 0;
4268
4269 if ((*rc = fan_set_speed(speed)) == -ENXIO)
4270 printk(IBM_ERR "speed command accepted for unsupported "
4271 "access mode %d", fan_control_access_mode);
4272
4273 return 1;
4274}
4275
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02004276static int fan_write_cmd_watchdog(const char *cmd, int *rc)
4277{
4278 int interval;
4279
4280 if (sscanf(cmd, "watchdog %d", &interval) != 1)
4281 return 0;
4282
4283 if (interval < 0 || interval > 120)
4284 *rc = -EINVAL;
4285 else
4286 fan_watchdog_maxinterval = interval;
4287
4288 return 1;
4289}
4290
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004291static int fan_write(char *buf)
4292{
4293 char *cmd;
4294 int rc = 0;
4295
4296 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004297 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004298 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004299 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004300 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02004301 fan_write_cmd_disable(cmd, &rc) ||
4302 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004303 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004304 fan_write_cmd_speed(cmd, &rc))
4305 )
4306 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02004307 else if (!rc)
4308 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004309 }
4310
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004311 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004312}
4313
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004314static struct ibm_struct fan_driver_data = {
4315 .name = "fan",
4316 .read = fan_read,
4317 .write = fan_write,
4318 .exit = fan_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004319};
4320
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004321/****************************************************************************
4322 ****************************************************************************
4323 *
4324 * Infrastructure
4325 *
4326 ****************************************************************************
4327 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004329/* /proc support */
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03004330static struct proc_dir_entry *proc_dir;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004331
4332/* Subdriver registry */
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004333static LIST_HEAD(tpacpi_all_drivers);
4334
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004336/*
4337 * Module and infrastructure proble, init and exit handling
4338 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004340#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004341static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004342{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004343 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004344
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004345 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004346}
4347#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
4348
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004349static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350{
4351 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004352 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353 struct proc_dir_entry *entry;
4354
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004355 BUG_ON(ibm == NULL);
4356
4357 INIT_LIST_HEAD(&ibm->all_drivers);
4358
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004359 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360 return 0;
4361
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004362 dbg_printk(TPACPI_DBG_INIT,
4363 "probing for %s\n", ibm->name);
4364
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004365 if (iibm->init) {
4366 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004367 if (ret > 0)
4368 return 0; /* probe failed */
4369 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004371
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004372 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373 }
4374
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004375 if (ibm->acpi) {
4376 if (ibm->acpi->hid) {
4377 ret = register_tpacpi_subdriver(ibm);
4378 if (ret)
4379 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004380 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004381
4382 if (ibm->acpi->notify) {
4383 ret = setup_acpi_notify(ibm);
4384 if (ret == -ENODEV) {
4385 printk(IBM_NOTICE "disabling subdriver %s\n",
4386 ibm->name);
4387 ret = 0;
4388 goto err_out;
4389 }
4390 if (ret < 0)
4391 goto err_out;
4392 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004393 }
4394
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004395 dbg_printk(TPACPI_DBG_INIT,
4396 "%s installed\n", ibm->name);
4397
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004398 if (ibm->read) {
4399 entry = create_proc_entry(ibm->name,
4400 S_IFREG | S_IRUGO | S_IWUSR,
4401 proc_dir);
4402 if (!entry) {
4403 printk(IBM_ERR "unable to create proc entry %s\n",
4404 ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004405 ret = -ENODEV;
4406 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004407 }
4408 entry->owner = THIS_MODULE;
4409 entry->data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004410 entry->read_proc = &dispatch_procfs_read;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004411 if (ibm->write)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004412 entry->write_proc = &dispatch_procfs_write;
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004413 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004414 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004416 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
4417
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004419
4420err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004421 dbg_printk(TPACPI_DBG_INIT,
4422 "%s: at error exit path with result %d\n",
4423 ibm->name, ret);
4424
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004425 ibm_exit(ibm);
4426 return (ret < 0)? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427}
4428
4429static void ibm_exit(struct ibm_struct *ibm)
4430{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004431 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004432
4433 list_del_init(&ibm->all_drivers);
4434
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004435 if (ibm->flags.acpi_notify_installed) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004436 dbg_printk(TPACPI_DBG_EXIT,
4437 "%s: acpi_remove_notify_handler\n", ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004438 BUG_ON(!ibm->acpi);
4439 acpi_remove_notify_handler(*ibm->acpi->handle,
4440 ibm->acpi->type,
4441 dispatch_acpi_notify);
4442 ibm->flags.acpi_notify_installed = 0;
4443 ibm->flags.acpi_notify_installed = 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004444 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004446 if (ibm->flags.proc_created) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004447 dbg_printk(TPACPI_DBG_EXIT,
4448 "%s: remove_proc_entry\n", ibm->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449 remove_proc_entry(ibm->name, proc_dir);
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004450 ibm->flags.proc_created = 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004451 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004453 if (ibm->flags.acpi_driver_registered) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004454 dbg_printk(TPACPI_DBG_EXIT,
4455 "%s: acpi_bus_unregister_driver\n", ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004456 BUG_ON(!ibm->acpi);
4457 acpi_bus_unregister_driver(ibm->acpi->driver);
4458 kfree(ibm->acpi->driver);
4459 ibm->acpi->driver = NULL;
4460 ibm->flags.acpi_driver_registered = 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004461 }
4462
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004463 if (ibm->flags.init_called && ibm->exit) {
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004464 ibm->exit();
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004465 ibm->flags.init_called = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004467
4468 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469}
4470
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004471/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03004473static void __init get_thinkpad_model_data(struct thinkpad_id_data *tp)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004474{
4475 struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02004476 char ec_fw_string[18];
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004477
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03004478 if (!tp)
4479 return;
4480
4481 memset(tp, 0, sizeof(*tp));
4482
4483 if (dmi_name_in_vendors("IBM"))
4484 tp->vendor = PCI_VENDOR_ID_IBM;
4485 else if (dmi_name_in_vendors("LENOVO"))
4486 tp->vendor = PCI_VENDOR_ID_LENOVO;
4487 else
4488 return;
4489
4490 tp->bios_version_str = kstrdup(dmi_get_system_info(DMI_BIOS_VERSION),
4491 GFP_KERNEL);
4492 if (!tp->bios_version_str)
4493 return;
4494 tp->bios_model = tp->bios_version_str[0]
4495 | (tp->bios_version_str[1] << 8);
4496
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004497 /*
4498 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
4499 * X32 or newer, all Z series; Some models must have an
4500 * up-to-date BIOS or they will not be detected.
4501 *
4502 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
4503 */
4504 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02004505 if (sscanf(dev->name,
4506 "IBM ThinkPad Embedded Controller -[%17c",
4507 ec_fw_string) == 1) {
4508 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
4509 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03004510
4511 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
4512 tp->ec_model = ec_fw_string[0]
4513 | (ec_fw_string[1] << 8);
4514 break;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02004515 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004516 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03004517
4518 tp->model_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_VERSION),
4519 GFP_KERNEL);
4520 if (strnicmp(tp->model_str, "ThinkPad", 8) != 0) {
4521 kfree(tp->model_str);
4522 tp->model_str = NULL;
4523 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524}
4525
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004526static int __init probe_for_thinkpad(void)
4527{
4528 int is_thinkpad;
4529
4530 if (acpi_disabled)
4531 return -ENODEV;
4532
4533 /*
4534 * Non-ancient models have better DMI tagging, but very old models
4535 * don't.
4536 */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03004537 is_thinkpad = (thinkpad_id.model_str != NULL);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004538
4539 /* ec is required because many other handles are relative to it */
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004540 IBM_ACPIHANDLE_INIT(ec);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004541 if (!ec_handle) {
4542 if (is_thinkpad)
4543 printk(IBM_ERR
4544 "Not yet supported ThinkPad detected!\n");
4545 return -ENODEV;
4546 }
4547
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03004548 /*
4549 * Risks a regression on very old machines, but reduces potential
4550 * false positives a damn great deal
4551 */
4552 if (!is_thinkpad)
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03004553 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03004554
4555 if (!is_thinkpad && !force_load)
4556 return -ENODEV;
4557
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004558 return 0;
4559}
4560
4561
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004562/* Module init, exit, parameters */
4563
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004564static struct ibm_init_struct ibms_init[] __initdata = {
4565 {
4566 .init = thinkpad_acpi_driver_init,
4567 .data = &thinkpad_acpi_driver_data,
4568 },
4569 {
4570 .init = hotkey_init,
4571 .data = &hotkey_driver_data,
4572 },
4573 {
4574 .init = bluetooth_init,
4575 .data = &bluetooth_driver_data,
4576 },
4577 {
4578 .init = wan_init,
4579 .data = &wan_driver_data,
4580 },
4581 {
4582 .init = video_init,
4583 .data = &video_driver_data,
4584 },
4585 {
4586 .init = light_init,
4587 .data = &light_driver_data,
4588 },
4589#ifdef CONFIG_THINKPAD_ACPI_DOCK
4590 {
4591 .init = dock_init,
4592 .data = &dock_driver_data[0],
4593 },
4594 {
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03004595 .init = dock_init2,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004596 .data = &dock_driver_data[1],
4597 },
4598#endif
4599#ifdef CONFIG_THINKPAD_ACPI_BAY
4600 {
4601 .init = bay_init,
4602 .data = &bay_driver_data,
4603 },
4604#endif
4605 {
4606 .init = cmos_init,
4607 .data = &cmos_driver_data,
4608 },
4609 {
4610 .init = led_init,
4611 .data = &led_driver_data,
4612 },
4613 {
4614 .init = beep_init,
4615 .data = &beep_driver_data,
4616 },
4617 {
4618 .init = thermal_init,
4619 .data = &thermal_driver_data,
4620 },
4621 {
4622 .data = &ecdump_driver_data,
4623 },
4624 {
4625 .init = brightness_init,
4626 .data = &brightness_driver_data,
4627 },
4628 {
4629 .data = &volume_driver_data,
4630 },
4631 {
4632 .init = fan_init,
4633 .data = &fan_driver_data,
4634 },
4635};
4636
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004637static int __init set_ibm_param(const char *val, struct kernel_param *kp)
4638{
4639 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004640 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004641
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004642 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
4643 ibm = ibms_init[i].data;
4644 BUG_ON(ibm == NULL);
4645
4646 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
4647 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004648 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004649 strcpy(ibms_init[i].param, val);
4650 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004651 return 0;
4652 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004653 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004654
4655 return -EINVAL;
4656}
4657
4658static int experimental;
4659module_param(experimental, int, 0);
4660
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03004661static u32 dbg_level;
4662module_param_named(debug, dbg_level, uint, 0);
4663
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03004664static int force_load;
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03004665module_param(force_load, bool, 0);
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03004666
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03004667static int fan_control_allowed;
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03004668module_param_named(fan_control, fan_control_allowed, bool, 0);
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03004669
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03004670static int brightness_mode;
4671module_param_named(brightness_mode, brightness_mode, int, 0);
4672
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004673static unsigned int hotkey_report_mode;
4674module_param(hotkey_report_mode, uint, 0);
4675
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004676#define IBM_PARAM(feature) \
4677 module_param_call(feature, set_ibm_param, NULL, NULL, 0)
4678
4679IBM_PARAM(hotkey);
4680IBM_PARAM(bluetooth);
4681IBM_PARAM(video);
4682IBM_PARAM(light);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03004683#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004684IBM_PARAM(dock);
4685#endif
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03004686#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004687IBM_PARAM(bay);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03004688#endif /* CONFIG_THINKPAD_ACPI_BAY */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004689IBM_PARAM(cmos);
4690IBM_PARAM(led);
4691IBM_PARAM(beep);
4692IBM_PARAM(ecdump);
4693IBM_PARAM(brightness);
4694IBM_PARAM(volume);
4695IBM_PARAM(fan);
4696
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03004697static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004698{
4699 int ret, i;
4700
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03004701 tpacpi_lifecycle = TPACPI_LIFE_INIT;
4702
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004703 /* Parameter checking */
4704 if (hotkey_report_mode > 2)
4705 return -EINVAL;
4706
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03004707 /* Driver-level probe */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03004708
4709 get_thinkpad_model_data(&thinkpad_id);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004710 ret = probe_for_thinkpad();
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03004711 if (ret) {
4712 thinkpad_acpi_module_exit();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004713 return ret;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03004714 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03004716 /* Driver initialization */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03004717
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004718 IBM_ACPIHANDLE_INIT(ecrd);
4719 IBM_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004721 proc_dir = proc_mkdir(IBM_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722 if (!proc_dir) {
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004723 printk(IBM_ERR "unable to create proc dir " IBM_PROC_DIR);
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03004724 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725 return -ENODEV;
4726 }
4727 proc_dir->owner = THIS_MODULE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004728
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03004729 ret = platform_driver_register(&tpacpi_pdriver);
4730 if (ret) {
4731 printk(IBM_ERR "unable to register platform driver\n");
4732 thinkpad_acpi_module_exit();
4733 return ret;
4734 }
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03004735 tp_features.platform_drv_registered = 1;
4736
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03004737 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
4738 if (ret) {
4739 printk(IBM_ERR "unable to create sysfs driver attributes\n");
4740 thinkpad_acpi_module_exit();
4741 return ret;
4742 }
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03004743 tp_features.platform_drv_attrs_registered = 1;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03004744
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03004745
4746 /* Device initialization */
4747 tpacpi_pdev = platform_device_register_simple(IBM_DRVR_NAME, -1,
4748 NULL, 0);
4749 if (IS_ERR(tpacpi_pdev)) {
4750 ret = PTR_ERR(tpacpi_pdev);
4751 tpacpi_pdev = NULL;
4752 printk(IBM_ERR "unable to register platform device\n");
4753 thinkpad_acpi_module_exit();
4754 return ret;
4755 }
4756 tpacpi_hwmon = hwmon_device_register(&tpacpi_pdev->dev);
4757 if (IS_ERR(tpacpi_hwmon)) {
4758 ret = PTR_ERR(tpacpi_hwmon);
4759 tpacpi_hwmon = NULL;
4760 printk(IBM_ERR "unable to register hwmon device\n");
4761 thinkpad_acpi_module_exit();
4762 return ret;
4763 }
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03004764 mutex_init(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03004765 tpacpi_inputdev = input_allocate_device();
4766 if (!tpacpi_inputdev) {
4767 printk(IBM_ERR "unable to allocate input device\n");
4768 thinkpad_acpi_module_exit();
4769 return -ENOMEM;
4770 } else {
4771 /* Prepare input device, but don't register */
4772 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
4773 tpacpi_inputdev->phys = IBM_DRVR_NAME "/input0";
4774 tpacpi_inputdev->id.bustype = BUS_HOST;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03004775 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
4776 thinkpad_id.vendor :
4777 PCI_VENDOR_ID_IBM;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03004778 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
4779 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
4780 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004781 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
4782 ret = ibm_init(&ibms_init[i]);
4783 if (ret >= 0 && *ibms_init[i].param)
4784 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03004786 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787 return ret;
4788 }
4789 }
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03004790 ret = input_register_device(tpacpi_inputdev);
4791 if (ret < 0) {
4792 printk(IBM_ERR "unable to register input device\n");
4793 thinkpad_acpi_module_exit();
4794 return ret;
4795 } else {
4796 tp_features.input_device_registered = 1;
4797 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03004799 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800 return 0;
4801}
4802
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03004803static void thinkpad_acpi_module_exit(void)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004804{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004805 struct ibm_struct *ibm, *itmp;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004806
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03004807 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
4808
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004809 list_for_each_entry_safe_reverse(ibm, itmp,
4810 &tpacpi_all_drivers,
4811 all_drivers) {
4812 ibm_exit(ibm);
4813 }
4814
4815 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004816
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03004817 if (tpacpi_inputdev) {
4818 if (tp_features.input_device_registered)
4819 input_unregister_device(tpacpi_inputdev);
4820 else
4821 input_free_device(tpacpi_inputdev);
4822 }
4823
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03004824 if (tpacpi_hwmon)
4825 hwmon_device_unregister(tpacpi_hwmon);
4826
4827 if (tpacpi_pdev)
4828 platform_device_unregister(tpacpi_pdev);
4829
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03004830 if (tp_features.platform_drv_attrs_registered)
4831 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
4832
4833 if (tp_features.platform_drv_registered)
4834 platform_driver_unregister(&tpacpi_pdriver);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03004835
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004836 if (proc_dir)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004837 remove_proc_entry(IBM_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004838
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03004839 kfree(thinkpad_id.bios_version_str);
4840 kfree(thinkpad_id.ec_version_str);
4841 kfree(thinkpad_id.model_str);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004842}
4843
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03004844module_init(thinkpad_acpi_module_init);
4845module_exit(thinkpad_acpi_module_exit);