blob: 9ff9142ce0639f161c002c0039d1df247d65dccc [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 Holschuhb856f5b2007-10-30 17:46:23 -020024#define IBM_VERSION "0.17"
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -020025#define TPACPI_SYSFS_VERSION 0x020101
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 Holschuh94954cc62007-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 Holschuh94954cc62007-07-18 23:45:27 -0300528static struct platform_device *tpacpi_pdev;
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300529static struct platform_device *tpacpi_sensors_pdev;
Tony Jones1beeffe2007-08-20 13:46:20 -0700530static struct device *tpacpi_hwmon;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300531static struct input_dev *tpacpi_inputdev;
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -0300532static struct mutex tpacpi_inputdev_send_mutex;
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300533
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300534
535static int tpacpi_resume_handler(struct platform_device *pdev)
536{
537 struct ibm_struct *ibm, *itmp;
538
539 list_for_each_entry_safe(ibm, itmp,
540 &tpacpi_all_drivers,
541 all_drivers) {
542 if (ibm->resume)
543 (ibm->resume)();
544 }
545
546 return 0;
547}
548
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300549static struct platform_driver tpacpi_pdriver = {
550 .driver = {
551 .name = IBM_DRVR_NAME,
552 .owner = THIS_MODULE,
553 },
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300554 .resume = tpacpi_resume_handler,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300555};
556
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300557static struct platform_driver tpacpi_hwmon_pdriver = {
558 .driver = {
559 .name = IBM_HWMON_DRVR_NAME,
560 .owner = THIS_MODULE,
561 },
562};
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300563
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -0300564/*************************************************************************
565 * thinkpad-acpi driver attributes
566 */
567
568/* interface_version --------------------------------------------------- */
569static ssize_t tpacpi_driver_interface_version_show(
570 struct device_driver *drv,
571 char *buf)
572{
573 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
574}
575
576static DRIVER_ATTR(interface_version, S_IRUGO,
577 tpacpi_driver_interface_version_show, NULL);
578
579/* debug_level --------------------------------------------------------- */
580static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
581 char *buf)
582{
583 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
584}
585
586static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
587 const char *buf, size_t count)
588{
589 unsigned long t;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -0300590
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300591 if (parse_strtoul(buf, 0xffff, &t))
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -0300592 return -EINVAL;
593
594 dbg_level = t;
595
596 return count;
597}
598
599static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
600 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
601
602/* version ------------------------------------------------------------- */
603static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
604 char *buf)
605{
606 return snprintf(buf, PAGE_SIZE, "%s v%s\n", IBM_DESC, IBM_VERSION);
607}
608
609static DRIVER_ATTR(version, S_IRUGO,
610 tpacpi_driver_version_show, NULL);
611
612/* --------------------------------------------------------------------- */
613
614static struct driver_attribute* tpacpi_driver_attributes[] = {
615 &driver_attr_debug_level, &driver_attr_version,
616 &driver_attr_interface_version,
617};
618
619static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
620{
621 int i, res;
622
623 i = 0;
624 res = 0;
625 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
626 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
627 i++;
628 }
629
630 return res;
631}
632
633static void tpacpi_remove_driver_attributes(struct device_driver *drv)
634{
635 int i;
636
637 for(i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
638 driver_remove_file(drv, tpacpi_driver_attributes[i]);
639}
640
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300641/*************************************************************************
642 * sysfs support helpers
643 */
644
645struct attribute_set_obj {
646 struct attribute_set s;
647 struct attribute *a;
648} __attribute__((packed));
649
650static struct attribute_set *create_attr_set(unsigned int max_members,
651 const char* name)
652{
653 struct attribute_set_obj *sobj;
654
655 if (max_members == 0)
656 return NULL;
657
658 /* Allocates space for implicit NULL at the end too */
659 sobj = kzalloc(sizeof(struct attribute_set_obj) +
660 max_members * sizeof(struct attribute *),
661 GFP_KERNEL);
662 if (!sobj)
663 return NULL;
664 sobj->s.max_members = max_members;
665 sobj->s.group.attrs = &sobj->a;
666 sobj->s.group.name = name;
667
668 return &sobj->s;
669}
670
671/* not multi-threaded safe, use it in a single thread per set */
672static int add_to_attr_set(struct attribute_set* s, struct attribute *attr)
673{
674 if (!s || !attr)
675 return -EINVAL;
676
677 if (s->members >= s->max_members)
678 return -ENOMEM;
679
680 s->group.attrs[s->members] = attr;
681 s->members++;
682
683 return 0;
684}
685
686static int add_many_to_attr_set(struct attribute_set* s,
687 struct attribute **attr,
688 unsigned int count)
689{
690 int i, res;
691
692 for (i = 0; i < count; i++) {
693 res = add_to_attr_set(s, attr[i]);
694 if (res)
695 return res;
696 }
697
698 return 0;
699}
700
701static void delete_attr_set(struct attribute_set* s, struct kobject *kobj)
702{
703 sysfs_remove_group(kobj, &s->group);
704 destroy_attr_set(s);
705}
706
707static int parse_strtoul(const char *buf,
708 unsigned long max, unsigned long *value)
709{
710 char *endp;
711
Henrique de Moraes Holschuh32afbf02007-10-08 10:12:56 -0300712 while (*buf && isspace(*buf))
713 buf++;
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300714 *value = simple_strtoul(buf, &endp, 0);
715 while (*endp && isspace(*endp))
716 endp++;
717 if (*endp || *value > max)
718 return -EINVAL;
719
720 return 0;
721}
722
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300723/****************************************************************************
724 ****************************************************************************
725 *
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300726 * Subdrivers
727 *
728 ****************************************************************************
729 ****************************************************************************/
730
731/*************************************************************************
Henrique de Moraes Holschuh142cfc92007-04-21 11:08:26 -0300732 * thinkpad-acpi init subdriver
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300733 */
734
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300735static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736{
737 printk(IBM_INFO "%s v%s\n", IBM_DESC, IBM_VERSION);
738 printk(IBM_INFO "%s\n", IBM_URL);
739
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -0300740 printk(IBM_INFO "ThinkPad BIOS %s, EC %s\n",
741 (thinkpad_id.bios_version_str) ?
742 thinkpad_id.bios_version_str : "unknown",
743 (thinkpad_id.ec_version_str) ?
744 thinkpad_id.ec_version_str : "unknown");
745
746 if (thinkpad_id.vendor && thinkpad_id.model_str)
747 printk(IBM_INFO "%s %s\n",
748 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
749 "IBM" : ((thinkpad_id.vendor ==
750 PCI_VENDOR_ID_LENOVO) ?
751 "Lenovo" : "Unknown vendor"),
752 thinkpad_id.model_str);
Henrique de Moraes Holschuh3945ac32007-02-06 19:13:44 -0200753
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 return 0;
755}
756
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300757static int thinkpad_acpi_driver_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758{
759 int len = 0;
760
761 len += sprintf(p + len, "driver:\t\t%s\n", IBM_DESC);
762 len += sprintf(p + len, "version:\t%s\n", IBM_VERSION);
763
764 return len;
765}
766
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300767static struct ibm_struct thinkpad_acpi_driver_data = {
768 .name = "driver",
769 .read = thinkpad_acpi_driver_read,
770};
771
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300772/*************************************************************************
773 * Hotkey subdriver
774 */
775
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -0200776enum { /* Keys available through NVRAM polling */
777 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
778 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
779};
780
781enum { /* Positions of some of the keys in hotkey masks */
782 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
783 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
784 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
785 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
786 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
787 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
788 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
789 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
790 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
791 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
792 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
793};
794
795enum { /* NVRAM to ACPI HKEY group map */
796 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
797 TP_ACPI_HKEY_ZOOM_MASK |
798 TP_ACPI_HKEY_DISPSWTCH_MASK |
799 TP_ACPI_HKEY_HIBERNATE_MASK,
800 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
801 TP_ACPI_HKEY_BRGHTDWN_MASK,
802 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
803 TP_ACPI_HKEY_VOLDWN_MASK |
804 TP_ACPI_HKEY_MUTE_MASK,
805};
806
807#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
808struct tp_nvram_state {
809 u16 thinkpad_toggle:1;
810 u16 zoom_toggle:1;
811 u16 display_toggle:1;
812 u16 thinklight_toggle:1;
813 u16 hibernate_toggle:1;
814 u16 displayexp_toggle:1;
815 u16 display_state:1;
816 u16 brightness_toggle:1;
817 u16 volume_toggle:1;
818 u16 mute:1;
819
820 u8 brightness_level;
821 u8 volume_level;
822};
823
824static struct task_struct *tpacpi_hotkey_task;
825static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
826static int hotkey_poll_freq = 10; /* Hz */
827static struct mutex hotkey_thread_mutex;
828static struct mutex hotkey_thread_data_mutex;
829static unsigned int hotkey_config_change;
830
831#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
832
833#define hotkey_source_mask 0U
834
835#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
836
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400837static int hotkey_orig_status;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -0300838static u32 hotkey_orig_mask;
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -0300839static u32 hotkey_all_mask;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -0300840static u32 hotkey_reserved_mask;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -0200841static u32 hotkey_mask;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -0300842
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -0300843static u16 *hotkey_keycode_map;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400844
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -0300845static struct attribute_set *hotkey_dev_attributes;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300846
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -0200847#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
848#define HOTKEY_CONFIG_CRITICAL_START \
849 mutex_lock(&hotkey_thread_data_mutex); \
850 hotkey_config_change++;
851#define HOTKEY_CONFIG_CRITICAL_END \
852 mutex_unlock(&hotkey_thread_data_mutex);
853#else
854#define HOTKEY_CONFIG_CRITICAL_START
855#define HOTKEY_CONFIG_CRITICAL_END
856#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
857
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -0300858static int hotkey_get_wlsw(int *status)
859{
860 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
861 return -EIO;
862 return 0;
863}
864
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -0200865/*
866 * Call with hotkey_mutex held
867 */
868static int hotkey_mask_get(void)
869{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -0200870 u32 m = 0;
871
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -0200872 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -0200873 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -0200874 return -EIO;
875 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -0200876 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -0200877
878 return 0;
879}
880
881/*
882 * Call with hotkey_mutex held
883 */
884static int hotkey_mask_set(u32 mask)
885{
886 int i;
887 int rc = 0;
888
889 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -0200890 HOTKEY_CONFIG_CRITICAL_START
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -0200891 for (i = 0; i < 32; i++) {
892 u32 m = 1 << i;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -0200893 /* enable in firmware mask only keys not in NVRAM
894 * mode, but enable the key in the cached hotkey_mask
895 * regardless of mode, or the key will end up
896 * disabled by hotkey_mask_get() */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -0200897 if (!acpi_evalf(hkey_handle,
898 NULL, "MHKM", "vdd", i + 1,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -0200899 !!((mask & ~hotkey_source_mask) & m))) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -0200900 rc = -EIO;
901 break;
902 } else {
903 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
904 }
905 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -0200906 HOTKEY_CONFIG_CRITICAL_END
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -0200907
908 /* hotkey_mask_get must be called unconditionally below */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -0200909 if (!hotkey_mask_get() && !rc &&
910 (hotkey_mask & ~hotkey_source_mask) !=
911 (mask & ~hotkey_source_mask)) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -0200912 printk(IBM_NOTICE
913 "requested hot key mask 0x%08x, but "
914 "firmware forced it to 0x%08x\n",
915 mask, hotkey_mask);
916 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -0200917 } else {
918#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
919 HOTKEY_CONFIG_CRITICAL_START
920 hotkey_mask = mask & hotkey_source_mask;
921 HOTKEY_CONFIG_CRITICAL_END
922 hotkey_mask_get();
923 if (hotkey_mask != mask) {
924 printk(IBM_NOTICE
925 "requested hot key mask 0x%08x, "
926 "forced to 0x%08x (NVRAM poll mask is "
927 "0x%08x): no firmware mask support\n",
928 mask, hotkey_mask, hotkey_source_mask);
929 }
930#else
931 hotkey_mask_get();
932 rc = -ENXIO;
933#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -0200934 }
935
936 return rc;
937}
938
939static int hotkey_status_get(int *status)
940{
941 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
942 return -EIO;
943
944 return 0;
945}
946
947static int hotkey_status_set(int status)
948{
949 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
950 return -EIO;
951
952 return 0;
953}
954
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -0200955static void tpacpi_input_send_radiosw(void)
956{
957 int wlsw;
958
959 mutex_lock(&tpacpi_inputdev_send_mutex);
960
961 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
962 input_report_switch(tpacpi_inputdev,
963 SW_RADIO, !!wlsw);
964 input_sync(tpacpi_inputdev);
965 }
966
967 mutex_unlock(&tpacpi_inputdev_send_mutex);
968}
969
970static void tpacpi_input_send_key(unsigned int scancode)
971{
972 unsigned int keycode;
973
974 keycode = hotkey_keycode_map[scancode];
975
976 if (keycode != KEY_RESERVED) {
977 mutex_lock(&tpacpi_inputdev_send_mutex);
978
979 input_report_key(tpacpi_inputdev, keycode, 1);
980 if (keycode == KEY_UNKNOWN)
981 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
982 scancode);
983 input_sync(tpacpi_inputdev);
984
985 input_report_key(tpacpi_inputdev, keycode, 0);
986 if (keycode == KEY_UNKNOWN)
987 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
988 scancode);
989 input_sync(tpacpi_inputdev);
990
991 mutex_unlock(&tpacpi_inputdev_send_mutex);
992 }
993}
994
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -0200995#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
996static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
997
998static void tpacpi_hotkey_send_key(unsigned int scancode)
999{
1000 tpacpi_input_send_key(scancode);
1001 if (hotkey_report_mode < 2) {
1002 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1003 0x80, 0x1001 + scancode);
1004 }
1005}
1006
1007static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1008{
1009 u8 d;
1010
1011 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1012 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1013 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1014 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1015 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1016 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1017 }
1018 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1019 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1020 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1021 }
1022 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1023 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1024 n->displayexp_toggle =
1025 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1026 }
1027 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1028 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1029 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1030 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1031 n->brightness_toggle =
1032 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1033 }
1034 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1035 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1036 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1037 >> TP_NVRAM_POS_LEVEL_VOLUME;
1038 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1039 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1040 }
1041}
1042
1043#define TPACPI_COMPARE_KEY(__scancode, __member) \
1044 do { if ((mask & (1 << __scancode)) && oldn->__member != newn->__member) \
1045 tpacpi_hotkey_send_key(__scancode); } while (0)
1046
1047#define TPACPI_MAY_SEND_KEY(__scancode) \
1048 do { if (mask & (1 << __scancode)) \
1049 tpacpi_hotkey_send_key(__scancode); } while (0)
1050
1051static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
1052 struct tp_nvram_state *newn,
1053 u32 mask)
1054{
1055 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1056 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1057 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1058 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1059
1060 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1061
1062 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1063
1064 /* handle volume */
1065 if (oldn->volume_toggle != newn->volume_toggle) {
1066 if (oldn->mute != newn->mute) {
1067 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1068 }
1069 if (oldn->volume_level > newn->volume_level) {
1070 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1071 } else if (oldn->volume_level < newn->volume_level) {
1072 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1073 } else if (oldn->mute == newn->mute) {
1074 /* repeated key presses that didn't change state */
1075 if (newn->mute) {
1076 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1077 } else if (newn->volume_level != 0) {
1078 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1079 } else {
1080 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1081 }
1082 }
1083 }
1084
1085 /* handle brightness */
1086 if (oldn->brightness_toggle != newn->brightness_toggle) {
1087 if (oldn->brightness_level < newn->brightness_level) {
1088 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1089 } else if (oldn->brightness_level > newn->brightness_level) {
1090 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1091 } else {
1092 /* repeated key presses that didn't change state */
1093 if (newn->brightness_level != 0) {
1094 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1095 } else {
1096 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1097 }
1098 }
1099 }
1100}
1101
1102#undef TPACPI_COMPARE_KEY
1103#undef TPACPI_MAY_SEND_KEY
1104
1105static int hotkey_kthread(void *data)
1106{
1107 struct tp_nvram_state s[2];
1108 u32 mask;
1109 unsigned int si, so;
1110 unsigned long t;
1111 unsigned int change_detector, must_reset;
1112
1113 mutex_lock(&hotkey_thread_mutex);
1114
1115 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1116 goto exit;
1117
1118 set_freezable();
1119
1120 so = 0;
1121 si = 1;
1122 t = 0;
1123
1124 /* Initial state for compares */
1125 mutex_lock(&hotkey_thread_data_mutex);
1126 change_detector = hotkey_config_change;
1127 mask = hotkey_source_mask & hotkey_mask;
1128 mutex_unlock(&hotkey_thread_data_mutex);
1129 hotkey_read_nvram(&s[so], mask);
1130
1131 while (!kthread_should_stop() && hotkey_poll_freq) {
1132 if (t == 0)
1133 t = 1000/hotkey_poll_freq;
1134 t = msleep_interruptible(t);
1135 if (unlikely(kthread_should_stop()))
1136 break;
1137 must_reset = try_to_freeze();
1138 if (t > 0 && !must_reset)
1139 continue;
1140
1141 mutex_lock(&hotkey_thread_data_mutex);
1142 if (must_reset || hotkey_config_change != change_detector) {
1143 /* forget old state on thaw or config change */
1144 si = so;
1145 t = 0;
1146 change_detector = hotkey_config_change;
1147 }
1148 mask = hotkey_source_mask & hotkey_mask;
1149 mutex_unlock(&hotkey_thread_data_mutex);
1150
1151 if (likely(mask)) {
1152 hotkey_read_nvram(&s[si], mask);
1153 if (likely(si != so)) {
1154 hotkey_compare_and_issue_event(&s[so], &s[si],
1155 mask);
1156 }
1157 }
1158
1159 so = si;
1160 si ^= 1;
1161 }
1162
1163exit:
1164 mutex_unlock(&hotkey_thread_mutex);
1165 return 0;
1166}
1167
1168static void hotkey_poll_stop_sync(void)
1169{
1170 if (tpacpi_hotkey_task) {
1171 if (frozen(tpacpi_hotkey_task) ||
1172 freezing(tpacpi_hotkey_task))
1173 thaw_process(tpacpi_hotkey_task);
1174
1175 kthread_stop(tpacpi_hotkey_task);
1176 tpacpi_hotkey_task = NULL;
1177 mutex_lock(&hotkey_thread_mutex);
1178 /* at this point, the thread did exit */
1179 mutex_unlock(&hotkey_thread_mutex);
1180 }
1181}
1182
1183/* call with hotkey_mutex held */
1184static void hotkey_poll_setup(int may_warn)
1185{
1186 if ((hotkey_source_mask & hotkey_mask) != 0 &&
1187 hotkey_poll_freq > 0 &&
1188 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1189 if (!tpacpi_hotkey_task) {
1190 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
1191 NULL, IBM_FILE "d");
1192 if (IS_ERR(tpacpi_hotkey_task)) {
1193 tpacpi_hotkey_task = NULL;
1194 printk(IBM_ERR "could not create kernel thread "
1195 "for hotkey polling\n");
1196 }
1197 }
1198 } else {
1199 hotkey_poll_stop_sync();
1200 if (may_warn &&
1201 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
1202 printk(IBM_NOTICE "hot keys 0x%08x require polling, "
1203 "which is currently disabled\n",
1204 hotkey_source_mask);
1205 }
1206 }
1207}
1208
1209static void hotkey_poll_setup_safe(int may_warn)
1210{
1211 mutex_lock(&hotkey_mutex);
1212 hotkey_poll_setup(may_warn);
1213 mutex_unlock(&hotkey_mutex);
1214}
1215
1216static int hotkey_inputdev_open(struct input_dev *dev)
1217{
1218 switch (tpacpi_lifecycle) {
1219 case TPACPI_LIFE_INIT:
1220 /*
1221 * hotkey_init will call hotkey_poll_setup_safe
1222 * at the appropriate moment
1223 */
1224 return 0;
1225 case TPACPI_LIFE_EXITING:
1226 return -EBUSY;
1227 case TPACPI_LIFE_RUNNING:
1228 hotkey_poll_setup_safe(0);
1229 return 0;
1230 }
1231
1232 /* Should only happen if tpacpi_lifecycle is corrupt */
1233 BUG();
1234 return -EBUSY;
1235}
1236
1237static void hotkey_inputdev_close(struct input_dev *dev)
1238{
1239 /* disable hotkey polling when possible */
1240 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1241 hotkey_poll_setup_safe(0);
1242}
1243#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1244
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001245/* sysfs hotkey enable ------------------------------------------------- */
1246static ssize_t hotkey_enable_show(struct device *dev,
1247 struct device_attribute *attr,
1248 char *buf)
1249{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001250 int res, status;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001251
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001252 res = hotkey_status_get(&status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001253 if (res)
1254 return res;
1255
1256 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1257}
1258
1259static ssize_t hotkey_enable_store(struct device *dev,
1260 struct device_attribute *attr,
1261 const char *buf, size_t count)
1262{
1263 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001264 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001265
1266 if (parse_strtoul(buf, 1, &t))
1267 return -EINVAL;
1268
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001269 res = hotkey_status_set(t);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001270
1271 return (res) ? res : count;
1272}
1273
1274static struct device_attribute dev_attr_hotkey_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001275 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001276 hotkey_enable_show, hotkey_enable_store);
1277
1278/* sysfs hotkey mask --------------------------------------------------- */
1279static ssize_t hotkey_mask_show(struct device *dev,
1280 struct device_attribute *attr,
1281 char *buf)
1282{
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001283 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001284
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001285 if (mutex_lock_interruptible(&hotkey_mutex))
1286 return -ERESTARTSYS;
1287 res = hotkey_mask_get();
1288 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001289
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001290 return (res)?
1291 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001292}
1293
1294static ssize_t hotkey_mask_store(struct device *dev,
1295 struct device_attribute *attr,
1296 const char *buf, size_t count)
1297{
1298 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001299 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001300
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001301 if (parse_strtoul(buf, 0xffffffffUL, &t))
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001302 return -EINVAL;
1303
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001304 if (mutex_lock_interruptible(&hotkey_mutex))
1305 return -ERESTARTSYS;
1306
1307 res = hotkey_mask_set(t);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001308
1309#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1310 hotkey_poll_setup(1);
1311#endif
1312
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001313 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001314
1315 return (res) ? res : count;
1316}
1317
1318static struct device_attribute dev_attr_hotkey_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001319 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001320 hotkey_mask_show, hotkey_mask_store);
1321
1322/* sysfs hotkey bios_enabled ------------------------------------------- */
1323static ssize_t hotkey_bios_enabled_show(struct device *dev,
1324 struct device_attribute *attr,
1325 char *buf)
1326{
1327 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
1328}
1329
1330static struct device_attribute dev_attr_hotkey_bios_enabled =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001331 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001332
1333/* sysfs hotkey bios_mask ---------------------------------------------- */
1334static ssize_t hotkey_bios_mask_show(struct device *dev,
1335 struct device_attribute *attr,
1336 char *buf)
1337{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001338 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001339}
1340
1341static struct device_attribute dev_attr_hotkey_bios_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001342 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001343
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001344/* sysfs hotkey all_mask ----------------------------------------------- */
1345static ssize_t hotkey_all_mask_show(struct device *dev,
1346 struct device_attribute *attr,
1347 char *buf)
1348{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001349 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1350 hotkey_all_mask | hotkey_source_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001351}
1352
1353static struct device_attribute dev_attr_hotkey_all_mask =
1354 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
1355
1356/* sysfs hotkey recommended_mask --------------------------------------- */
1357static ssize_t hotkey_recommended_mask_show(struct device *dev,
1358 struct device_attribute *attr,
1359 char *buf)
1360{
1361 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001362 (hotkey_all_mask | hotkey_source_mask)
1363 & ~hotkey_reserved_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001364}
1365
1366static struct device_attribute dev_attr_hotkey_recommended_mask =
1367 __ATTR(hotkey_recommended_mask, S_IRUGO,
1368 hotkey_recommended_mask_show, NULL);
1369
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001370#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1371
1372/* sysfs hotkey hotkey_source_mask ------------------------------------- */
1373static ssize_t hotkey_source_mask_show(struct device *dev,
1374 struct device_attribute *attr,
1375 char *buf)
1376{
1377 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
1378}
1379
1380static ssize_t hotkey_source_mask_store(struct device *dev,
1381 struct device_attribute *attr,
1382 const char *buf, size_t count)
1383{
1384 unsigned long t;
1385
1386 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
1387 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
1388 return -EINVAL;
1389
1390 if (mutex_lock_interruptible(&hotkey_mutex))
1391 return -ERESTARTSYS;
1392
1393 HOTKEY_CONFIG_CRITICAL_START
1394 hotkey_source_mask = t;
1395 HOTKEY_CONFIG_CRITICAL_END
1396
1397 hotkey_poll_setup(1);
1398
1399 mutex_unlock(&hotkey_mutex);
1400
1401 return count;
1402}
1403
1404static struct device_attribute dev_attr_hotkey_source_mask =
1405 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
1406 hotkey_source_mask_show, hotkey_source_mask_store);
1407
1408/* sysfs hotkey hotkey_poll_freq --------------------------------------- */
1409static ssize_t hotkey_poll_freq_show(struct device *dev,
1410 struct device_attribute *attr,
1411 char *buf)
1412{
1413 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
1414}
1415
1416static ssize_t hotkey_poll_freq_store(struct device *dev,
1417 struct device_attribute *attr,
1418 const char *buf, size_t count)
1419{
1420 unsigned long t;
1421
1422 if (parse_strtoul(buf, 25, &t))
1423 return -EINVAL;
1424
1425 if (mutex_lock_interruptible(&hotkey_mutex))
1426 return -ERESTARTSYS;
1427
1428 hotkey_poll_freq = t;
1429
1430 hotkey_poll_setup(1);
1431 mutex_unlock(&hotkey_mutex);
1432
1433 return count;
1434}
1435
1436static struct device_attribute dev_attr_hotkey_poll_freq =
1437 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
1438 hotkey_poll_freq_show, hotkey_poll_freq_store);
1439
1440#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1441
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001442/* sysfs hotkey radio_sw ----------------------------------------------- */
1443static ssize_t hotkey_radio_sw_show(struct device *dev,
1444 struct device_attribute *attr,
1445 char *buf)
1446{
1447 int res, s;
1448 res = hotkey_get_wlsw(&s);
1449 if (res < 0)
1450 return res;
1451
1452 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1453}
1454
1455static struct device_attribute dev_attr_hotkey_radio_sw =
1456 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
1457
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001458/* sysfs hotkey report_mode -------------------------------------------- */
1459static ssize_t hotkey_report_mode_show(struct device *dev,
1460 struct device_attribute *attr,
1461 char *buf)
1462{
1463 return snprintf(buf, PAGE_SIZE, "%d\n",
1464 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
1465}
1466
1467static struct device_attribute dev_attr_hotkey_report_mode =
1468 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
1469
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001470/* --------------------------------------------------------------------- */
1471
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001472static struct attribute *hotkey_attributes[] __initdata = {
1473 &dev_attr_hotkey_enable.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001474 &dev_attr_hotkey_bios_enabled.attr,
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001475 &dev_attr_hotkey_report_mode.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001476#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1477 &dev_attr_hotkey_mask.attr,
1478 &dev_attr_hotkey_all_mask.attr,
1479 &dev_attr_hotkey_recommended_mask.attr,
1480 &dev_attr_hotkey_source_mask.attr,
1481 &dev_attr_hotkey_poll_freq.attr,
1482#endif
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001483};
1484
1485static struct attribute *hotkey_mask_attributes[] __initdata = {
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001486 &dev_attr_hotkey_bios_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001487#ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1488 &dev_attr_hotkey_mask.attr,
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001489 &dev_attr_hotkey_all_mask.attr,
1490 &dev_attr_hotkey_recommended_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001491#endif
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001492};
1493
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001494static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001495{
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001496 /* Requirements for changing the default keymaps:
1497 *
1498 * 1. Many of the keys are mapped to KEY_RESERVED for very
1499 * good reasons. Do not change them unless you have deep
1500 * knowledge on the IBM and Lenovo ThinkPad firmware for
1501 * the various ThinkPad models. The driver behaves
1502 * differently for KEY_RESERVED: such keys have their
1503 * hot key mask *unset* in mask_recommended, and also
1504 * in the initial hot key mask programmed into the
1505 * firmware at driver load time, which means the firm-
1506 * ware may react very differently if you change them to
1507 * something else;
1508 *
1509 * 2. You must be subscribed to the linux-thinkpad and
1510 * ibm-acpi-devel mailing lists, and you should read the
1511 * list archives since 2007 if you want to change the
1512 * keymaps. This requirement exists so that you will
1513 * know the past history of problems with the thinkpad-
1514 * acpi driver keymaps, and also that you will be
1515 * listening to any bug reports;
1516 *
1517 * 3. Do not send thinkpad-acpi specific patches directly to
1518 * for merging, *ever*. Send them to the linux-acpi
1519 * mailinglist for comments. Merging is to be done only
1520 * through acpi-test and the ACPI maintainer.
1521 *
1522 * If the above is too much to ask, don't change the keymap.
1523 * Ask the thinkpad-acpi maintainer to do it, instead.
1524 */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001525 static u16 ibm_keycode_map[] __initdata = {
1526 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1527 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
1528 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1529 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001530
1531 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001532 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1533 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1534 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001535
1536 /* brightness: firmware always reacts to them, unless
1537 * X.org did some tricks in the radeon BIOS scratch
1538 * registers of *some* models */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02001539 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02001540 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001541
1542 /* Thinklight: firmware always react to it */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001543 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001544
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001545 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
1546 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001547
1548 /* Volume: firmware always react to it and reprograms
1549 * the built-in *extra* mixer. Never map it to control
1550 * another mixer by default. */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02001551 KEY_RESERVED, /* 0x14: VOLUME UP */
1552 KEY_RESERVED, /* 0x15: VOLUME DOWN */
1553 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001554
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001555 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001556
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001557 /* (assignments unknown, please report if found) */
1558 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1559 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1560 };
1561 static u16 lenovo_keycode_map[] __initdata = {
1562 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1563 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
1564 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1565 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001566
1567 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001568 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1569 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1570 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001571
Henrique de Moraes Holschuh56a185b2007-12-13 12:14:09 -02001572 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuh56a185b2007-12-13 12:14:09 -02001573 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001574
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001575 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001576
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001577 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
1578 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001579
1580 /* Volume: z60/z61, T60 (BIOS version?): firmware always
1581 * react to it and reprograms the built-in *extra* mixer.
1582 * Never map it to control another mixer by default.
1583 *
1584 * T60?, T61, R60?, R61: firmware and EC tries to send
1585 * these over the regular keyboard, so these are no-ops,
1586 * but there are still weird bugs re. MUTE, so do not
1587 * change unless you get test reports from all Lenovo
1588 * models. May cause the BIOS to interfere with the
1589 * HDA mixer.
1590 */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02001591 KEY_RESERVED, /* 0x14: VOLUME UP */
1592 KEY_RESERVED, /* 0x15: VOLUME DOWN */
1593 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001594
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001595 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001596
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001597 /* (assignments unknown, please report if found) */
1598 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1599 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1600 };
1601
1602#define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
1603#define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
1604#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
1605
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001606 int res, i;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001607 int status;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001608 int hkeyv;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001609
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001610 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
1611
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001612 BUG_ON(!tpacpi_inputdev);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001613 BUG_ON(tpacpi_inputdev->open != NULL ||
1614 tpacpi_inputdev->close != NULL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001615
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001616 IBM_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001617 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001618
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001619#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1620 mutex_init(&hotkey_thread_mutex);
1621 mutex_init(&hotkey_thread_data_mutex);
1622#endif
1623
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001624 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001625 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001626
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001627 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001628 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001629
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001630 if (tp_features.hotkey) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001631 hotkey_dev_attributes = create_attr_set(10, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001632 if (!hotkey_dev_attributes)
1633 return -ENOMEM;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001634 res = add_many_to_attr_set(hotkey_dev_attributes,
1635 hotkey_attributes,
1636 ARRAY_SIZE(hotkey_attributes));
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001637 if (res)
1638 return res;
1639
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001640 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001641 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
1642 for HKEY interface version 0x100 */
1643 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
1644 if ((hkeyv >> 8) != 1) {
1645 printk(IBM_ERR "unknown version of the "
1646 "HKEY interface: 0x%x\n", hkeyv);
1647 printk(IBM_ERR "please report this to %s\n",
1648 IBM_MAIL);
1649 } else {
1650 /*
1651 * MHKV 0x100 in A31, R40, R40e,
1652 * T4x, X31, and later
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001653 */
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001654 tp_features.hotkey_mask = 1;
1655 }
1656 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001657
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001658 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001659 str_supported(tp_features.hotkey_mask));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001660
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001661 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001662 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001663 "MHKA", "qd")) {
1664 printk(IBM_ERR
1665 "missing MHKA handler, "
1666 "please report this to %s\n",
1667 IBM_MAIL);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001668 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001669 }
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001670 }
1671
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001672 /* hotkey_source_mask *must* be zero for
1673 * the first hotkey_mask_get */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001674 res = hotkey_status_get(&hotkey_orig_status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001675 if (!res && tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001676 res = hotkey_mask_get();
1677 hotkey_orig_mask = hotkey_mask;
1678 if (!res) {
1679 res = add_many_to_attr_set(
1680 hotkey_dev_attributes,
1681 hotkey_mask_attributes,
1682 ARRAY_SIZE(hotkey_mask_attributes));
1683 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001684 }
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001685
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001686#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1687 if (tp_features.hotkey_mask) {
1688 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
1689 & ~hotkey_all_mask;
1690 } else {
1691 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
1692 }
1693
1694 vdbg_printk(TPACPI_DBG_INIT,
1695 "hotkey source mask 0x%08x, polling freq %d\n",
1696 hotkey_source_mask, hotkey_poll_freq);
1697#endif
1698
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001699 /* Not all thinkpads have a hardware radio switch */
1700 if (!res && acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
1701 tp_features.hotkey_wlsw = 1;
1702 printk(IBM_INFO
1703 "radio switch found; radios are %s\n",
1704 enabled(status, 0));
1705 res = add_to_attr_set(hotkey_dev_attributes,
1706 &dev_attr_hotkey_radio_sw.attr);
1707 }
1708
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001709 if (!res)
1710 res = register_attr_set_with_sysfs(
1711 hotkey_dev_attributes,
1712 &tpacpi_pdev->dev.kobj);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001713 if (res)
1714 return res;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001715
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001716 /* Set up key map */
1717
1718 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
1719 GFP_KERNEL);
1720 if (!hotkey_keycode_map) {
1721 printk(IBM_ERR "failed to allocate memory for key map\n");
1722 return -ENOMEM;
1723 }
1724
1725 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
1726 dbg_printk(TPACPI_DBG_INIT,
1727 "using Lenovo default hot key map\n");
1728 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
1729 TPACPI_HOTKEY_MAP_SIZE);
1730 } else {
1731 dbg_printk(TPACPI_DBG_INIT,
1732 "using IBM default hot key map\n");
1733 memcpy(hotkey_keycode_map, &ibm_keycode_map,
1734 TPACPI_HOTKEY_MAP_SIZE);
1735 }
1736
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001737 set_bit(EV_KEY, tpacpi_inputdev->evbit);
1738 set_bit(EV_MSC, tpacpi_inputdev->evbit);
1739 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001740 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
1741 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
1742 tpacpi_inputdev->keycode = hotkey_keycode_map;
1743 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001744 if (hotkey_keycode_map[i] != KEY_RESERVED) {
1745 set_bit(hotkey_keycode_map[i],
1746 tpacpi_inputdev->keybit);
1747 } else {
1748 if (i < sizeof(hotkey_reserved_mask)*8)
1749 hotkey_reserved_mask |= 1 << i;
1750 }
1751 }
1752
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03001753 if (tp_features.hotkey_wlsw) {
1754 set_bit(EV_SW, tpacpi_inputdev->evbit);
1755 set_bit(SW_RADIO, tpacpi_inputdev->swbit);
1756 }
1757
Henrique de Moraes Holschuh1a343762007-07-18 23:45:36 -03001758 dbg_printk(TPACPI_DBG_INIT,
1759 "enabling hot key handling\n");
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001760 res = hotkey_status_set(1);
1761 if (res)
1762 return res;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001763 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
1764 & ~hotkey_reserved_mask)
Henrique de Moraes Holschuh1a343762007-07-18 23:45:36 -03001765 | hotkey_orig_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001766 if (res < 0 && res != -ENXIO)
Henrique de Moraes Holschuh1a343762007-07-18 23:45:36 -03001767 return res;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001768
1769 dbg_printk(TPACPI_DBG_INIT,
1770 "legacy hot key reporting over procfs %s\n",
1771 (hotkey_report_mode < 2) ?
1772 "enabled" : "disabled");
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001773
1774#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1775 tpacpi_inputdev->open = &hotkey_inputdev_open;
1776 tpacpi_inputdev->close = &hotkey_inputdev_close;
1777
1778 hotkey_poll_setup_safe(1);
1779#endif
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001780 }
1781
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001782 return (tp_features.hotkey)? 0 : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001783}
1784
1785static void hotkey_exit(void)
1786{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001787#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1788 hotkey_poll_stop_sync();
1789#endif
1790
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001791 if (tp_features.hotkey) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001792 dbg_printk(TPACPI_DBG_EXIT, "restoring original hot key mask\n");
1793 /* no short-circuit boolean operator below! */
1794 if ((hotkey_mask_set(hotkey_orig_mask) |
1795 hotkey_status_set(hotkey_orig_status)) != 0)
1796 printk(IBM_ERR "failed to restore hot key mask to BIOS defaults\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001797 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001798
1799 if (hotkey_dev_attributes) {
1800 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
1801 hotkey_dev_attributes = NULL;
1802 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001803}
1804
1805static void hotkey_notify(struct ibm_struct *ibm, u32 event)
1806{
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001807 u32 hkey;
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02001808 unsigned int scancode;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03001809 int send_acpi_ev;
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03001810 int ignore_acpi_ev;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001811
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03001812 if (event != 0x80) {
1813 printk(IBM_ERR "unknown HKEY notification event %d\n", event);
1814 /* forward it to userspace, maybe it knows how to handle it */
1815 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
1816 ibm->acpi->device->dev.bus_id,
1817 event, 0);
1818 return;
1819 }
1820
1821 while (1) {
1822 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
1823 printk(IBM_ERR "failed to retrieve HKEY event\n");
1824 return;
1825 }
1826
1827 if (hkey == 0) {
1828 /* queue empty */
1829 return;
1830 }
1831
1832 send_acpi_ev = 0;
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03001833 ignore_acpi_ev = 0;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03001834
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001835 switch (hkey >> 12) {
1836 case 1:
1837 /* 0x1000-0x1FFF: key presses */
1838 scancode = hkey & 0xfff;
1839 if (scancode > 0 && scancode < 0x21) {
1840 scancode--;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001841 if (!(hotkey_source_mask & (1 << scancode))) {
1842 tpacpi_input_send_key(scancode);
1843 } else {
1844 ignore_acpi_ev = 1;
1845 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001846 } else {
1847 printk(IBM_ERR
1848 "hotkey 0x%04x out of range for keyboard map\n",
1849 hkey);
1850 send_acpi_ev = 1;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001851 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001852 break;
1853 case 5:
1854 /* 0x5000-0x5FFF: LID */
1855 /* we don't handle it through this path, just
1856 * eat up known LID events */
1857 if (hkey != 0x5001 && hkey != 0x5002) {
1858 printk(IBM_ERR
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03001859 "unknown LID-related HKEY event: 0x%04x\n",
1860 hkey);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001861 send_acpi_ev = 1;
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03001862 } else {
1863 ignore_acpi_ev = 1;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001864 }
1865 break;
1866 case 7:
1867 /* 0x7000-0x7FFF: misc */
1868 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
1869 tpacpi_input_send_radiosw();
1870 break;
1871 }
1872 /* fallthrough to default */
1873 default:
1874 /* case 2: dock-related */
1875 /* 0x2305 - T43 waking up due to bay lever eject while aslept */
1876 /* case 3: ultra-bay related. maybe bay in dock? */
1877 /* 0x3003 - T43 after wake up by bay lever eject (0x2305) */
1878 printk(IBM_NOTICE "unhandled HKEY event 0x%04x\n", hkey);
1879 send_acpi_ev = 1;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001880 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001881
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03001882 /* Legacy events */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03001883 if (!ignore_acpi_ev && (send_acpi_ev || hotkey_report_mode < 2)) {
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03001884 acpi_bus_generate_proc_event(ibm->acpi->device, event, hkey);
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03001885 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001886
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03001887 /* netlink events */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03001888 if (!ignore_acpi_ev && send_acpi_ev) {
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03001889 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
1890 ibm->acpi->device->dev.bus_id,
1891 event, hkey);
1892 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001893 }
1894}
1895
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03001896static void hotkey_resume(void)
1897{
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001898 if (hotkey_mask_get())
1899 printk(IBM_ERR "error while trying to read hot key mask from firmware\n");
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03001900 tpacpi_input_send_radiosw();
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001901#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1902 hotkey_poll_setup_safe(0);
1903#endif
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03001904}
1905
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001906/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001907static int hotkey_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001909 int res, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 int len = 0;
1911
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001912 if (!tp_features.hotkey) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001913 len += sprintf(p + len, "status:\t\tnot supported\n");
1914 return len;
1915 }
1916
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02001917 if (mutex_lock_interruptible(&hotkey_mutex))
1918 return -ERESTARTSYS;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001919 res = hotkey_status_get(&status);
1920 if (!res)
1921 res = hotkey_mask_get();
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001922 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001923 if (res)
1924 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925
1926 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001927 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001928 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 len += sprintf(p + len,
1930 "commands:\tenable, disable, reset, <mask>\n");
1931 } else {
1932 len += sprintf(p + len, "mask:\t\tnot supported\n");
1933 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
1934 }
1935
1936 return len;
1937}
1938
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001939static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001941 int res, status;
1942 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001945 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 return -ENODEV;
1947
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02001948 if (mutex_lock_interruptible(&hotkey_mutex))
1949 return -ERESTARTSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001950
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001951 status = -1;
1952 mask = hotkey_mask;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001953
1954 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 while ((cmd = next_cmd(&buf))) {
1956 if (strlencmp(cmd, "enable") == 0) {
1957 status = 1;
1958 } else if (strlencmp(cmd, "disable") == 0) {
1959 status = 0;
1960 } else if (strlencmp(cmd, "reset") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001961 status = hotkey_orig_status;
1962 mask = hotkey_orig_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
1964 /* mask set */
1965 } else if (sscanf(cmd, "%x", &mask) == 1) {
1966 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001967 } else {
1968 res = -EINVAL;
1969 goto errexit;
1970 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 }
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001972 if (status != -1)
1973 res = hotkey_status_set(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001975 if (!res && mask != hotkey_mask)
1976 res = hotkey_mask_set(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001978errexit:
1979 mutex_unlock(&hotkey_mutex);
1980 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001981}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982
Thomas Renninger1ba90e32007-07-23 14:44:41 +02001983static const struct acpi_device_id ibm_htk_device_ids[] = {
1984 {IBM_HKEY_HID, 0},
1985 {"", 0},
1986};
1987
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001988static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +02001989 .hid = ibm_htk_device_ids,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001990 .notify = hotkey_notify,
1991 .handle = &hkey_handle,
1992 .type = ACPI_DEVICE_NOTIFY,
1993};
1994
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001995static struct ibm_struct hotkey_driver_data = {
1996 .name = "hotkey",
1997 .read = hotkey_read,
1998 .write = hotkey_write,
1999 .exit = hotkey_exit,
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002000 .resume = hotkey_resume,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002001 .acpi = &ibm_hotkey_acpidriver,
2002};
2003
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002004/*************************************************************************
2005 * Bluetooth subdriver
2006 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002008/* sysfs bluetooth enable ---------------------------------------------- */
2009static ssize_t bluetooth_enable_show(struct device *dev,
2010 struct device_attribute *attr,
2011 char *buf)
2012{
2013 int status;
2014
2015 status = bluetooth_get_radiosw();
2016 if (status < 0)
2017 return status;
2018
2019 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2020}
2021
2022static ssize_t bluetooth_enable_store(struct device *dev,
2023 struct device_attribute *attr,
2024 const char *buf, size_t count)
2025{
2026 unsigned long t;
2027 int res;
2028
2029 if (parse_strtoul(buf, 1, &t))
2030 return -EINVAL;
2031
2032 res = bluetooth_set_radiosw(t);
2033
2034 return (res) ? res : count;
2035}
2036
2037static struct device_attribute dev_attr_bluetooth_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002038 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002039 bluetooth_enable_show, bluetooth_enable_store);
2040
2041/* --------------------------------------------------------------------- */
2042
2043static struct attribute *bluetooth_attributes[] = {
2044 &dev_attr_bluetooth_enable.attr,
2045 NULL
2046};
2047
2048static const struct attribute_group bluetooth_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002049 .attrs = bluetooth_attributes,
2050};
2051
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002052static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002054 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002055 int status = 0;
2056
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002057 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
2058
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002059 IBM_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002060
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002061 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2062 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002063 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002064 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002066 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
2067 str_supported(tp_features.bluetooth),
2068 status);
2069
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002070 if (tp_features.bluetooth) {
2071 if (!(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
2072 /* no bluetooth hardware present in system */
2073 tp_features.bluetooth = 0;
2074 dbg_printk(TPACPI_DBG_INIT,
2075 "bluetooth hardware not installed\n");
2076 } else {
2077 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2078 &bluetooth_attr_group);
2079 if (res)
2080 return res;
2081 }
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002082 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002083
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002084 return (tp_features.bluetooth)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085}
2086
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002087static void bluetooth_exit(void)
2088{
2089 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2090 &bluetooth_attr_group);
2091}
2092
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002093static int bluetooth_get_radiosw(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094{
2095 int status;
2096
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002097 if (!tp_features.bluetooth)
2098 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002100 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2101 return -EIO;
2102
2103 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
2104}
2105
2106static int bluetooth_set_radiosw(int radio_on)
2107{
2108 int status;
2109
2110 if (!tp_features.bluetooth)
2111 return -ENODEV;
2112
2113 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2114 return -EIO;
2115 if (radio_on)
2116 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
2117 else
2118 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
2119 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
2120 return -EIO;
2121
2122 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123}
2124
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002125/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002126static int bluetooth_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127{
2128 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002129 int status = bluetooth_get_radiosw();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002131 if (!tp_features.bluetooth)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 len += sprintf(p + len, "status:\t\tnot supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002134 len += sprintf(p + len, "status:\t\t%s\n",
2135 (status)? "enabled" : "disabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 len += sprintf(p + len, "commands:\tenable, disable\n");
2137 }
2138
2139 return len;
2140}
2141
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002142static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002146 if (!tp_features.bluetooth)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002147 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148
2149 while ((cmd = next_cmd(&buf))) {
2150 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002151 bluetooth_set_radiosw(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002153 bluetooth_set_radiosw(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 } else
2155 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 }
2157
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 return 0;
2159}
2160
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002161static struct ibm_struct bluetooth_driver_data = {
2162 .name = "bluetooth",
2163 .read = bluetooth_read,
2164 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002165 .exit = bluetooth_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002166};
2167
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002168/*************************************************************************
2169 * Wan subdriver
2170 */
2171
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002172/* sysfs wan enable ---------------------------------------------------- */
2173static ssize_t wan_enable_show(struct device *dev,
2174 struct device_attribute *attr,
2175 char *buf)
2176{
2177 int status;
2178
2179 status = wan_get_radiosw();
2180 if (status < 0)
2181 return status;
2182
2183 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2184}
2185
2186static ssize_t wan_enable_store(struct device *dev,
2187 struct device_attribute *attr,
2188 const char *buf, size_t count)
2189{
2190 unsigned long t;
2191 int res;
2192
2193 if (parse_strtoul(buf, 1, &t))
2194 return -EINVAL;
2195
2196 res = wan_set_radiosw(t);
2197
2198 return (res) ? res : count;
2199}
2200
2201static struct device_attribute dev_attr_wan_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002202 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002203 wan_enable_show, wan_enable_store);
2204
2205/* --------------------------------------------------------------------- */
2206
2207static struct attribute *wan_attributes[] = {
2208 &dev_attr_wan_enable.attr,
2209 NULL
2210};
2211
2212static const struct attribute_group wan_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002213 .attrs = wan_attributes,
2214};
2215
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002216static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002217{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002218 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002219 int status = 0;
2220
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002221 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
2222
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002223 IBM_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002224
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002225 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002226 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002227
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002228 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
2229 str_supported(tp_features.wan),
2230 status);
2231
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002232 if (tp_features.wan) {
2233 if (!(status & TP_ACPI_WANCARD_HWPRESENT)) {
2234 /* no wan hardware present in system */
2235 tp_features.wan = 0;
2236 dbg_printk(TPACPI_DBG_INIT,
2237 "wan hardware not installed\n");
2238 } else {
2239 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2240 &wan_attr_group);
2241 if (res)
2242 return res;
2243 }
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002244 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002245
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002246 return (tp_features.wan)? 0 : 1;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002247}
2248
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002249static void wan_exit(void)
2250{
2251 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2252 &wan_attr_group);
2253}
2254
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002255static int wan_get_radiosw(void)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002256{
2257 int status;
2258
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002259 if (!tp_features.wan)
2260 return -ENODEV;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002261
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002262 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2263 return -EIO;
2264
2265 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
2266}
2267
2268static int wan_set_radiosw(int radio_on)
2269{
2270 int status;
2271
2272 if (!tp_features.wan)
2273 return -ENODEV;
2274
2275 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2276 return -EIO;
2277 if (radio_on)
2278 status |= TP_ACPI_WANCARD_RADIOSSW;
2279 else
2280 status &= ~TP_ACPI_WANCARD_RADIOSSW;
2281 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
2282 return -EIO;
2283
2284 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002285}
2286
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002287/* procfs -------------------------------------------------------------- */
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002288static int wan_read(char *p)
2289{
2290 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002291 int status = wan_get_radiosw();
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002292
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002293 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002294 len += sprintf(p + len, "status:\t\tnot supported\n");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002295 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002296 len += sprintf(p + len, "status:\t\t%s\n",
2297 (status)? "enabled" : "disabled");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002298 len += sprintf(p + len, "commands:\tenable, disable\n");
2299 }
2300
2301 return len;
2302}
2303
2304static int wan_write(char *buf)
2305{
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002306 char *cmd;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002307
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002308 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002309 return -ENODEV;
2310
2311 while ((cmd = next_cmd(&buf))) {
2312 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002313 wan_set_radiosw(1);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002314 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002315 wan_set_radiosw(0);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002316 } else
2317 return -EINVAL;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002318 }
2319
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002320 return 0;
2321}
2322
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002323static struct ibm_struct wan_driver_data = {
2324 .name = "wan",
2325 .read = wan_read,
2326 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002327 .exit = wan_exit,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03002328 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002329};
2330
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002331/*************************************************************************
2332 * Video subdriver
2333 */
2334
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02002335static enum video_access_mode video_supported;
2336static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002337
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002338IBM_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
2339 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
2340 "\\_SB.PCI0.VID0", /* 770e */
2341 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
2342 "\\_SB.PCI0.AGP.VID", /* all others */
2343 ); /* R30, R31 */
2344
2345IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
2346
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002347static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002349 int ivga;
2350
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002351 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
2352
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002353 IBM_ACPIHANDLE_INIT(vid);
2354 IBM_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002355
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002356 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
2357 /* G41, assume IVGA doesn't change */
2358 vid_handle = vid2_handle;
2359
2360 if (!vid_handle)
2361 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002362 video_supported = TPACPI_VIDEO_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002363 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
2364 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002365 video_supported = TPACPI_VIDEO_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002366 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
2367 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002368 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002369 else
2370 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002371 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002373 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
2374 str_supported(video_supported != TPACPI_VIDEO_NONE),
2375 video_supported);
2376
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002377 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378}
2379
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002380static void video_exit(void)
2381{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002382 dbg_printk(TPACPI_DBG_EXIT,
2383 "restoring original video autoswitch mode\n");
2384 if (video_autosw_set(video_orig_autosw))
2385 printk(IBM_ERR "error while trying to restore original "
2386 "video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002387}
2388
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002389static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390{
2391 int status = 0;
2392 int i;
2393
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002394 switch (video_supported) {
2395 case TPACPI_VIDEO_570:
2396 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
2397 TP_ACPI_VIDEO_570_PHSCMD))
2398 return -EIO;
2399 status = i & TP_ACPI_VIDEO_570_PHSMASK;
2400 break;
2401 case TPACPI_VIDEO_770:
2402 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
2403 return -EIO;
2404 if (i)
2405 status |= TP_ACPI_VIDEO_S_LCD;
2406 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
2407 return -EIO;
2408 if (i)
2409 status |= TP_ACPI_VIDEO_S_CRT;
2410 break;
2411 case TPACPI_VIDEO_NEW:
2412 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
2413 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
2414 return -EIO;
2415 if (i)
2416 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002418 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
2419 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
2420 return -EIO;
2421 if (i)
2422 status |= TP_ACPI_VIDEO_S_LCD;
2423 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
2424 return -EIO;
2425 if (i)
2426 status |= TP_ACPI_VIDEO_S_DVI;
2427 break;
2428 default:
2429 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002430 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431
2432 return status;
2433}
2434
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002435static int video_outputsw_set(int status)
2436{
2437 int autosw;
2438 int res = 0;
2439
2440 switch (video_supported) {
2441 case TPACPI_VIDEO_570:
2442 res = acpi_evalf(NULL, NULL,
2443 "\\_SB.PHS2", "vdd",
2444 TP_ACPI_VIDEO_570_PHS2CMD,
2445 status | TP_ACPI_VIDEO_570_PHS2SET);
2446 break;
2447 case TPACPI_VIDEO_770:
2448 autosw = video_autosw_get();
2449 if (autosw < 0)
2450 return autosw;
2451
2452 res = video_autosw_set(1);
2453 if (res)
2454 return res;
2455 res = acpi_evalf(vid_handle, NULL,
2456 "ASWT", "vdd", status * 0x100, 0);
2457 if (!autosw && video_autosw_set(autosw)) {
2458 printk(IBM_ERR "video auto-switch left enabled due to error\n");
2459 return -EIO;
2460 }
2461 break;
2462 case TPACPI_VIDEO_NEW:
2463 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
2464 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
2465 break;
2466 default:
2467 return -ENOSYS;
2468 }
2469
2470 return (res)? 0 : -EIO;
2471}
2472
2473static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002475 int autosw = 0;
2476
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002477 switch (video_supported) {
2478 case TPACPI_VIDEO_570:
2479 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
2480 return -EIO;
2481 break;
2482 case TPACPI_VIDEO_770:
2483 case TPACPI_VIDEO_NEW:
2484 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
2485 return -EIO;
2486 break;
2487 default:
2488 return -ENOSYS;
2489 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002490
2491 return autosw & 1;
2492}
2493
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002494static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002495{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002496 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002497 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002498 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002499}
2500
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002501static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002502{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002503 int autosw = video_autosw_get();
2504 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002505
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002506 if (autosw < 0)
2507 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002508
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002509 switch (video_supported) {
2510 case TPACPI_VIDEO_570:
2511 res = video_autosw_set(1);
2512 if (res)
2513 return res;
2514 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
2515 break;
2516 case TPACPI_VIDEO_770:
2517 case TPACPI_VIDEO_NEW:
2518 res = video_autosw_set(1);
2519 if (res)
2520 return res;
2521 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
2522 break;
2523 default:
2524 return -ENOSYS;
2525 }
2526 if (!autosw && video_autosw_set(autosw)) {
2527 printk(IBM_ERR "video auto-switch left enabled due to error\n");
2528 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002529 }
2530
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002531 return (res)? 0 : -EIO;
2532}
2533
2534static int video_expand_toggle(void)
2535{
2536 switch (video_supported) {
2537 case TPACPI_VIDEO_570:
2538 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
2539 0 : -EIO;
2540 case TPACPI_VIDEO_770:
2541 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
2542 0 : -EIO;
2543 case TPACPI_VIDEO_NEW:
2544 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
2545 0 : -EIO;
2546 default:
2547 return -ENOSYS;
2548 }
2549 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002550}
2551
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002552static int video_read(char *p)
2553{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002554 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002555 int len = 0;
2556
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002557 if (video_supported == TPACPI_VIDEO_NONE) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002558 len += sprintf(p + len, "status:\t\tnot supported\n");
2559 return len;
2560 }
2561
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002562 status = video_outputsw_get();
2563 if (status < 0)
2564 return status;
2565
2566 autosw = video_autosw_get();
2567 if (autosw < 0)
2568 return autosw;
2569
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002570 len += sprintf(p + len, "status:\t\tsupported\n");
2571 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
2572 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002573 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002574 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
2575 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
2576 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
2577 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002578 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002579 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
2580 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
2581 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
2582
2583 return len;
2584}
2585
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002586static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587{
2588 char *cmd;
2589 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002590 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002592 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002593 return -ENODEV;
2594
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002595 enable = 0;
2596 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597
2598 while ((cmd = next_cmd(&buf))) {
2599 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002600 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002602 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002604 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002606 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002607 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002608 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002609 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002610 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002611 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002612 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002614 res = video_autosw_set(1);
2615 if (res)
2616 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002618 res = video_autosw_set(0);
2619 if (res)
2620 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002622 res = video_outputsw_cycle();
2623 if (res)
2624 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002626 res = video_expand_toggle();
2627 if (res)
2628 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 } else
2630 return -EINVAL;
2631 }
2632
2633 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002634 status = video_outputsw_get();
2635 if (status < 0)
2636 return status;
2637 res = video_outputsw_set((status & ~disable) | enable);
2638 if (res)
2639 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 }
2641
2642 return 0;
2643}
2644
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002645static struct ibm_struct video_driver_data = {
2646 .name = "video",
2647 .read = video_read,
2648 .write = video_write,
2649 .exit = video_exit,
2650};
2651
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002652/*************************************************************************
2653 * Light (thinklight) subdriver
2654 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002656IBM_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
2657IBM_HANDLE(ledb, ec, "LEDB"); /* G4x */
2658
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002659static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002661 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
2662
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002663 IBM_ACPIHANDLE_INIT(ledb);
2664 IBM_ACPIHANDLE_INIT(lght);
2665 IBM_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002666
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002667 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002668 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002669
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002670 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002671 /* light status not supported on
2672 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002673 tp_features.light_status =
2674 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002676 vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002677 str_supported(tp_features.light));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002678
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002679 return (tp_features.light)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680}
2681
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002682static int light_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683{
2684 int len = 0;
2685 int status = 0;
2686
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002687 if (!tp_features.light) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002688 len += sprintf(p + len, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002689 } else if (!tp_features.light_status) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002690 len += sprintf(p + len, "status:\t\tunknown\n");
2691 len += sprintf(p + len, "commands:\ton, off\n");
2692 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
2694 return -EIO;
2695 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002696 len += sprintf(p + len, "commands:\ton, off\n");
2697 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698
2699 return len;
2700}
2701
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002702static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703{
2704 int cmos_cmd, lght_cmd;
2705 char *cmd;
2706 int success;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002707
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002708 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002709 return -ENODEV;
2710
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 while ((cmd = next_cmd(&buf))) {
2712 if (strlencmp(cmd, "on") == 0) {
2713 cmos_cmd = 0x0c;
2714 lght_cmd = 1;
2715 } else if (strlencmp(cmd, "off") == 0) {
2716 cmos_cmd = 0x0d;
2717 lght_cmd = 0;
2718 } else
2719 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002720
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 success = cmos_handle ?
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002722 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
2723 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 if (!success)
2725 return -EIO;
2726 }
2727
2728 return 0;
2729}
2730
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002731static struct ibm_struct light_driver_data = {
2732 .name = "light",
2733 .read = light_read,
2734 .write = light_write,
2735};
2736
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002737/*************************************************************************
2738 * Dock subdriver
2739 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03002741#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002742
2743IBM_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
2744 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
2745 "\\_SB.PCI0.PCI1.DOCK", /* all others */
2746 "\\_SB.PCI.ISA.SLCE", /* 570 */
2747 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
2748
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002749/* don't list other alternatives as we install a notify handler on the 570 */
2750IBM_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
2751
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002752static const struct acpi_device_id ibm_pci_device_ids[] = {
2753 {PCI_ROOT_HID_STRING, 0},
2754 {"", 0},
2755};
2756
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03002757static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
2758 {
2759 .notify = dock_notify,
2760 .handle = &dock_handle,
2761 .type = ACPI_SYSTEM_NOTIFY,
2762 },
2763 {
Henrique de Moraes Holschuh996fba02007-07-18 23:45:40 -03002764 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
2765 * We just use it to get notifications of dock hotplug
2766 * in very old thinkpads */
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002767 .hid = ibm_pci_device_ids,
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03002768 .notify = dock_notify,
2769 .handle = &pci_handle,
2770 .type = ACPI_SYSTEM_NOTIFY,
2771 },
2772};
2773
2774static struct ibm_struct dock_driver_data[2] = {
2775 {
2776 .name = "dock",
2777 .read = dock_read,
2778 .write = dock_write,
2779 .acpi = &ibm_dock_acpidriver[0],
2780 },
2781 {
2782 .name = "dock",
2783 .acpi = &ibm_dock_acpidriver[1],
2784 },
2785};
2786
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787#define dock_docked() (_sta(dock_handle) & 1)
2788
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002789static int __init dock_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002790{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002791 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
2792
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002793 IBM_ACPIHANDLE_INIT(dock);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002794
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002795 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
2796 str_supported(dock_handle != NULL));
2797
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002798 return (dock_handle)? 0 : 1;
2799}
2800
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03002801static int __init dock_init2(struct ibm_init_struct *iibm)
2802{
2803 int dock2_needed;
2804
2805 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
2806
2807 if (dock_driver_data[0].flags.acpi_driver_registered &&
2808 dock_driver_data[0].flags.acpi_notify_installed) {
2809 IBM_ACPIHANDLE_INIT(pci);
2810 dock2_needed = (pci_handle != NULL);
2811 vdbg_printk(TPACPI_DBG_INIT,
2812 "dock PCI handler for the TP 570 is %s\n",
2813 str_supported(dock2_needed));
2814 } else {
2815 vdbg_printk(TPACPI_DBG_INIT,
2816 "dock subdriver part 2 not required\n");
2817 dock2_needed = 0;
2818 }
2819
2820 return (dock2_needed)? 0 : 1;
2821}
2822
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002823static void dock_notify(struct ibm_struct *ibm, u32 event)
2824{
2825 int docked = dock_docked();
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002826 int pci = ibm->acpi->hid && ibm->acpi->device &&
2827 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
Zhang Rui962ce8c2007-08-23 01:24:31 +08002828 int data;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002829
2830 if (event == 1 && !pci) /* 570 */
Zhang Rui962ce8c2007-08-23 01:24:31 +08002831 data = 1; /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002832 else if (event == 1 && pci) /* 570 */
Zhang Rui962ce8c2007-08-23 01:24:31 +08002833 data = 3; /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002834 else if (event == 3 && docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08002835 data = 1; /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002836 else if (event == 3 && !docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08002837 data = 2; /* undock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002838 else if (event == 0 && docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08002839 data = 3; /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002840 else {
2841 printk(IBM_ERR "unknown dock event %d, status %d\n",
2842 event, _sta(dock_handle));
Zhang Rui962ce8c2007-08-23 01:24:31 +08002843 data = 0; /* unknown */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002844 }
Len Brown14e04fb32007-08-23 15:20:26 -04002845 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
Zhang Rui962ce8c2007-08-23 01:24:31 +08002846 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
2847 ibm->acpi->device->dev.bus_id,
2848 event, data);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002849}
2850
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002851static int dock_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852{
2853 int len = 0;
2854 int docked = dock_docked();
2855
2856 if (!dock_handle)
2857 len += sprintf(p + len, "status:\t\tnot supported\n");
2858 else if (!docked)
2859 len += sprintf(p + len, "status:\t\tundocked\n");
2860 else {
2861 len += sprintf(p + len, "status:\t\tdocked\n");
2862 len += sprintf(p + len, "commands:\tdock, undock\n");
2863 }
2864
2865 return len;
2866}
2867
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002868static int dock_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869{
2870 char *cmd;
2871
2872 if (!dock_docked())
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002873 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874
2875 while ((cmd = next_cmd(&buf))) {
2876 if (strlencmp(cmd, "undock") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002877 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
2878 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 return -EIO;
2880 } else if (strlencmp(cmd, "dock") == 0) {
2881 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
2882 return -EIO;
2883 } else
2884 return -EINVAL;
2885 }
2886
2887 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002888}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03002890#endif /* CONFIG_THINKPAD_ACPI_DOCK */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002892/*************************************************************************
2893 * Bay subdriver
2894 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03002896#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002897IBM_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
2898 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
2899 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
2900 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
2901 ); /* A21e, R30, R31 */
2902IBM_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
2903 "_EJ0", /* all others */
2904 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
2905IBM_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
2906 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
2907 ); /* all others */
2908IBM_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
2909 "_EJ0", /* 770x */
2910 ); /* all others */
2911
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002912static int __init bay_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002914 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
2915
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002916 IBM_ACPIHANDLE_INIT(bay);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002917 if (bay_handle)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002918 IBM_ACPIHANDLE_INIT(bay_ej);
2919 IBM_ACPIHANDLE_INIT(bay2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002920 if (bay2_handle)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002921 IBM_ACPIHANDLE_INIT(bay2_ej);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002922
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002923 tp_features.bay_status = bay_handle &&
2924 acpi_evalf(bay_handle, NULL, "_STA", "qv");
2925 tp_features.bay_status2 = bay2_handle &&
2926 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002927
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002928 tp_features.bay_eject = bay_handle && bay_ej_handle &&
2929 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
2930 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
2931 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002933 vdbg_printk(TPACPI_DBG_INIT,
2934 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002935 str_supported(tp_features.bay_status),
2936 str_supported(tp_features.bay_eject),
2937 str_supported(tp_features.bay_status2),
2938 str_supported(tp_features.bay_eject2));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002939
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002940 return (tp_features.bay_status || tp_features.bay_eject ||
2941 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942}
2943
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002944static void bay_notify(struct ibm_struct *ibm, u32 event)
2945{
Len Brown14e04fb32007-08-23 15:20:26 -04002946 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
Zhang Rui962ce8c2007-08-23 01:24:31 +08002947 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
2948 ibm->acpi->device->dev.bus_id,
2949 event, 0);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002950}
2951
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002952#define bay_occupied(b) (_sta(b##_handle) & 1)
2953
2954static int bay_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955{
2956 int len = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002957 int occupied = bay_occupied(bay);
2958 int occupied2 = bay_occupied(bay2);
2959 int eject, eject2;
2960
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002961 len += sprintf(p + len, "status:\t\t%s\n",
2962 tp_features.bay_status ?
2963 (occupied ? "occupied" : "unoccupied") :
2964 "not supported");
2965 if (tp_features.bay_status2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002966 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
2967 "occupied" : "unoccupied");
2968
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002969 eject = tp_features.bay_eject && occupied;
2970 eject2 = tp_features.bay_eject2 && occupied2;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002971
2972 if (eject && eject2)
2973 len += sprintf(p + len, "commands:\teject, eject2\n");
2974 else if (eject)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 len += sprintf(p + len, "commands:\teject\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002976 else if (eject2)
2977 len += sprintf(p + len, "commands:\teject2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978
2979 return len;
2980}
2981
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002982static int bay_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983{
2984 char *cmd;
2985
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002986 if (!tp_features.bay_eject && !tp_features.bay_eject2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002987 return -ENODEV;
2988
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002990 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002991 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
2992 return -EIO;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002993 } else if (tp_features.bay_eject2 &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002994 strlencmp(cmd, "eject2") == 0) {
2995 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 return -EIO;
2997 } else
2998 return -EINVAL;
2999 }
3000
3001 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003002}
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003003
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003004static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
3005 .notify = bay_notify,
3006 .handle = &bay_handle,
3007 .type = ACPI_SYSTEM_NOTIFY,
3008};
3009
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003010static struct ibm_struct bay_driver_data = {
3011 .name = "bay",
3012 .read = bay_read,
3013 .write = bay_write,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003014 .acpi = &ibm_bay_acpidriver,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003015};
3016
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003017#endif /* CONFIG_THINKPAD_ACPI_BAY */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003019/*************************************************************************
3020 * CMOS subdriver
3021 */
3022
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003023/* sysfs cmos_command -------------------------------------------------- */
3024static ssize_t cmos_command_store(struct device *dev,
3025 struct device_attribute *attr,
3026 const char *buf, size_t count)
3027{
3028 unsigned long cmos_cmd;
3029 int res;
3030
3031 if (parse_strtoul(buf, 21, &cmos_cmd))
3032 return -EINVAL;
3033
3034 res = issue_thinkpad_cmos_command(cmos_cmd);
3035 return (res)? res : count;
3036}
3037
3038static struct device_attribute dev_attr_cmos_command =
3039 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
3040
3041/* --------------------------------------------------------------------- */
3042
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003043static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003044{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003045 int res;
3046
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003047 vdbg_printk(TPACPI_DBG_INIT,
3048 "initializing cmos commands subdriver\n");
3049
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003050 IBM_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003051
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003052 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
3053 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003054
3055 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3056 if (res)
3057 return res;
3058
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003059 return (cmos_handle)? 0 : 1;
3060}
3061
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003062static void cmos_exit(void)
3063{
3064 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3065}
3066
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003067static int cmos_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068{
3069 int len = 0;
3070
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003071 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3072 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 if (!cmos_handle)
3074 len += sprintf(p + len, "status:\t\tnot supported\n");
3075 else {
3076 len += sprintf(p + len, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003077 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 }
3079
3080 return len;
3081}
3082
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003083static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084{
3085 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03003086 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087
3088 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003089 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
3090 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 /* cmos_cmd set */
3092 } else
3093 return -EINVAL;
3094
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03003095 res = issue_thinkpad_cmos_command(cmos_cmd);
3096 if (res)
3097 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 }
3099
3100 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003101}
3102
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003103static struct ibm_struct cmos_driver_data = {
3104 .name = "cmos",
3105 .read = cmos_read,
3106 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003107 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003108};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003109
3110/*************************************************************************
3111 * LED subdriver
3112 */
3113
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02003114static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003115
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003116IBM_HANDLE(led, ec, "SLED", /* 570 */
3117 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
3118 "LED", /* all others */
3119 ); /* R30, R31 */
3120
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003121static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003122{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003123 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
3124
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003125 IBM_ACPIHANDLE_INIT(led);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003126
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003127 if (!led_handle)
3128 /* led not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003129 led_supported = TPACPI_LED_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003130 else if (strlencmp(led_path, "SLED") == 0)
3131 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003132 led_supported = TPACPI_LED_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003133 else if (strlencmp(led_path, "SYSL") == 0)
3134 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003135 led_supported = TPACPI_LED_OLD;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003136 else
3137 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003138 led_supported = TPACPI_LED_NEW;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003139
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003140 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
3141 str_supported(led_supported), led_supported);
3142
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003143 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003144}
3145
3146#define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
3147
3148static int led_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149{
3150 int len = 0;
3151
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003152 if (!led_supported) {
3153 len += sprintf(p + len, "status:\t\tnot supported\n");
3154 return len;
3155 }
3156 len += sprintf(p + len, "status:\t\tsupported\n");
3157
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003158 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003159 /* 570 */
3160 int i, status;
3161 for (i = 0; i < 8; i++) {
3162 if (!acpi_evalf(ec_handle,
3163 &status, "GLED", "dd", 1 << i))
3164 return -EIO;
3165 len += sprintf(p + len, "%d:\t\t%s\n",
3166 i, led_status(status));
3167 }
3168 }
3169
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170 len += sprintf(p + len, "commands:\t"
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003171 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172
3173 return len;
3174}
3175
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003176/* off, on, blink */
3177static const int led_sled_arg1[] = { 0, 1, 3 };
3178static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
3179static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
3180static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
3181
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003182static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183{
3184 char *cmd;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003185 int led, ind, ret;
3186
3187 if (!led_supported)
3188 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189
3190 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003191 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 return -EINVAL;
3193
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003194 if (strstr(cmd, "off")) {
3195 ind = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 } else if (strstr(cmd, "on")) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003197 ind = 1;
3198 } else if (strstr(cmd, "blink")) {
3199 ind = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 } else
3201 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003202
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003203 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003204 /* 570 */
3205 led = 1 << led;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003207 led, led_sled_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 return -EIO;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003209 } else if (led_supported == TPACPI_LED_OLD) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003210 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
3211 led = 1 << led;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003212 ret = ec_write(TPACPI_LED_EC_HLMS, led);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003213 if (ret >= 0)
3214 ret =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003215 ec_write(TPACPI_LED_EC_HLBL,
Henrique de Moraes Holschuhe062e032007-03-23 17:33:55 -03003216 led * led_exp_hlbl[ind]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003217 if (ret >= 0)
3218 ret =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003219 ec_write(TPACPI_LED_EC_HLCL,
Henrique de Moraes Holschuhe062e032007-03-23 17:33:55 -03003220 led * led_exp_hlcl[ind]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003221 if (ret < 0)
3222 return ret;
3223 } else {
3224 /* all others */
3225 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
3226 led, led_led_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003228 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 }
3230
3231 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003232}
3233
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003234static struct ibm_struct led_driver_data = {
3235 .name = "led",
3236 .read = led_read,
3237 .write = led_write,
3238};
3239
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003240/*************************************************************************
3241 * Beep subdriver
3242 */
3243
3244IBM_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
3245
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003246static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003247{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003248 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
3249
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003250 IBM_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003251
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003252 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
3253 str_supported(beep_handle != NULL));
3254
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003255 return (beep_handle)? 0 : 1;
3256}
3257
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003258static int beep_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259{
3260 int len = 0;
3261
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003262 if (!beep_handle)
3263 len += sprintf(p + len, "status:\t\tnot supported\n");
3264 else {
3265 len += sprintf(p + len, "status:\t\tsupported\n");
3266 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
3267 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268
3269 return len;
3270}
3271
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003272static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273{
3274 char *cmd;
3275 int beep_cmd;
3276
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003277 if (!beep_handle)
3278 return -ENODEV;
3279
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003281 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
3282 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 /* beep_cmd set */
3284 } else
3285 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003286 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 return -EIO;
3288 }
3289
3290 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003291}
3292
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003293static struct ibm_struct beep_driver_data = {
3294 .name = "beep",
3295 .read = beep_read,
3296 .write = beep_write,
3297};
3298
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003299/*************************************************************************
3300 * Thermal subdriver
3301 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003302
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003303static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003304
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003305/* sysfs temp##_input -------------------------------------------------- */
3306
3307static ssize_t thermal_temp_input_show(struct device *dev,
3308 struct device_attribute *attr,
3309 char *buf)
3310{
3311 struct sensor_device_attribute *sensor_attr =
3312 to_sensor_dev_attr(attr);
3313 int idx = sensor_attr->index;
3314 s32 value;
3315 int res;
3316
3317 res = thermal_get_sensor(idx, &value);
3318 if (res)
3319 return res;
3320 if (value == TP_EC_THERMAL_TMP_NA * 1000)
3321 return -ENXIO;
3322
3323 return snprintf(buf, PAGE_SIZE, "%d\n", value);
3324}
3325
3326#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
3327 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, thermal_temp_input_show, NULL, _idxB)
3328
3329static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
3330 THERMAL_SENSOR_ATTR_TEMP(1, 0),
3331 THERMAL_SENSOR_ATTR_TEMP(2, 1),
3332 THERMAL_SENSOR_ATTR_TEMP(3, 2),
3333 THERMAL_SENSOR_ATTR_TEMP(4, 3),
3334 THERMAL_SENSOR_ATTR_TEMP(5, 4),
3335 THERMAL_SENSOR_ATTR_TEMP(6, 5),
3336 THERMAL_SENSOR_ATTR_TEMP(7, 6),
3337 THERMAL_SENSOR_ATTR_TEMP(8, 7),
3338 THERMAL_SENSOR_ATTR_TEMP(9, 8),
3339 THERMAL_SENSOR_ATTR_TEMP(10, 9),
3340 THERMAL_SENSOR_ATTR_TEMP(11, 10),
3341 THERMAL_SENSOR_ATTR_TEMP(12, 11),
3342 THERMAL_SENSOR_ATTR_TEMP(13, 12),
3343 THERMAL_SENSOR_ATTR_TEMP(14, 13),
3344 THERMAL_SENSOR_ATTR_TEMP(15, 14),
3345 THERMAL_SENSOR_ATTR_TEMP(16, 15),
3346};
3347
3348#define THERMAL_ATTRS(X) \
3349 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
3350
3351static struct attribute *thermal_temp_input_attr[] = {
3352 THERMAL_ATTRS(8),
3353 THERMAL_ATTRS(9),
3354 THERMAL_ATTRS(10),
3355 THERMAL_ATTRS(11),
3356 THERMAL_ATTRS(12),
3357 THERMAL_ATTRS(13),
3358 THERMAL_ATTRS(14),
3359 THERMAL_ATTRS(15),
3360 THERMAL_ATTRS(0),
3361 THERMAL_ATTRS(1),
3362 THERMAL_ATTRS(2),
3363 THERMAL_ATTRS(3),
3364 THERMAL_ATTRS(4),
3365 THERMAL_ATTRS(5),
3366 THERMAL_ATTRS(6),
3367 THERMAL_ATTRS(7),
3368 NULL
3369};
3370
3371static const struct attribute_group thermal_temp_input16_group = {
3372 .attrs = thermal_temp_input_attr
3373};
3374
3375static const struct attribute_group thermal_temp_input8_group = {
3376 .attrs = &thermal_temp_input_attr[8]
3377};
3378
3379#undef THERMAL_SENSOR_ATTR_TEMP
3380#undef THERMAL_ATTRS
3381
3382/* --------------------------------------------------------------------- */
3383
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003384static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003385{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003386 u8 t, ta1, ta2;
3387 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003388 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003389 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003390
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003391 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
3392
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003393 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003394
Henrique de Moraes Holschuh3d6f99c2007-07-18 23:45:46 -03003395 if (thinkpad_id.ec_model) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003396 /*
3397 * Direct EC access mode: sensors at registers
3398 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
3399 * non-implemented, thermal sensors return 0x80 when
3400 * not available
3401 */
3402
3403 ta1 = ta2 = 0;
3404 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03003405 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003406 ta1 |= t;
3407 } else {
3408 ta1 = 0;
3409 break;
3410 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03003411 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003412 ta2 |= t;
3413 } else {
3414 ta1 = 0;
3415 break;
3416 }
3417 }
3418 if (ta1 == 0) {
3419 /* This is sheer paranoia, but we handle it anyway */
3420 if (acpi_tmp7) {
3421 printk(IBM_ERR
3422 "ThinkPad ACPI EC access misbehaving, "
3423 "falling back to ACPI TMPx access mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003424 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003425 } else {
3426 printk(IBM_ERR
3427 "ThinkPad ACPI EC access misbehaving, "
3428 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003429 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003430 }
3431 } else {
3432 thermal_read_mode =
3433 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003434 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003435 }
3436 } else if (acpi_tmp7) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003437 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
3438 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003439 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003440 } else {
3441 /* Standard ACPI TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003442 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003443 }
3444 } else {
3445 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003446 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003447 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003448
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003449 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
3450 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
3451 thermal_read_mode);
3452
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003453 switch(thermal_read_mode) {
3454 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03003455 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003456 &thermal_temp_input16_group);
3457 if (res)
3458 return res;
3459 break;
3460 case TPACPI_THERMAL_TPEC_8:
3461 case TPACPI_THERMAL_ACPI_TMP07:
3462 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03003463 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003464 &thermal_temp_input8_group);
3465 if (res)
3466 return res;
3467 break;
3468 case TPACPI_THERMAL_NONE:
3469 default:
3470 return 1;
3471 }
3472
3473 return 0;
3474}
3475
3476static void thermal_exit(void)
3477{
3478 switch(thermal_read_mode) {
3479 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03003480 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003481 &thermal_temp_input16_group);
3482 break;
3483 case TPACPI_THERMAL_TPEC_8:
3484 case TPACPI_THERMAL_ACPI_TMP07:
3485 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03003486 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003487 &thermal_temp_input16_group);
3488 break;
3489 case TPACPI_THERMAL_NONE:
3490 default:
3491 break;
3492 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003493}
3494
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03003495/* idx is zero-based */
3496static int thermal_get_sensor(int idx, s32 *value)
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003497{
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03003498 int t;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003499 s8 tmp;
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03003500 char tmpi[5];
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003501
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03003502 t = TP_EC_THERMAL_TMP0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003503
3504 switch (thermal_read_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003505#if TPACPI_MAX_THERMAL_SENSORS >= 16
3506 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03003507 if (idx >= 8 && idx <= 15) {
3508 t = TP_EC_THERMAL_TMP8;
3509 idx -= 8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003510 }
3511 /* fallthrough */
3512#endif
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003513 case TPACPI_THERMAL_TPEC_8:
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03003514 if (idx <= 7) {
3515 if (!acpi_ec_read(t + idx, &tmp))
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003516 return -EIO;
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03003517 *value = tmp * 1000;
3518 return 0;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003519 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03003520 break;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003521
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003522 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03003523 if (idx <= 7) {
3524 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
3525 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
3526 return -EIO;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003527 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
3528 return -EIO;
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03003529 *value = (t - 2732) * 100;
3530 return 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003531 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03003532 break;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003533
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003534 case TPACPI_THERMAL_ACPI_TMP07:
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03003535 if (idx <= 7) {
3536 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003537 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
3538 return -EIO;
Henrique de Moraes Holschuha8fba3da2007-07-18 23:45:45 -03003539 if (t > 127 || t < -127)
3540 t = TP_EC_THERMAL_TMP_NA;
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03003541 *value = t * 1000;
3542 return 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003543 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03003544 break;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003545
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003546 case TPACPI_THERMAL_NONE:
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003547 default:
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03003548 return -ENOSYS;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003549 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03003550
3551 return -EINVAL;
3552}
3553
3554static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
3555{
3556 int res, i;
3557 int n;
3558
3559 n = 8;
3560 i = 0;
3561
3562 if (!s)
3563 return -EINVAL;
3564
3565 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
3566 n = 16;
3567
3568 for(i = 0 ; i < n; i++) {
3569 res = thermal_get_sensor(i, &s->temp[i]);
3570 if (res)
3571 return res;
3572 }
3573
3574 return n;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003575}
3576
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003577static int thermal_read(char *p)
3578{
3579 int len = 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003580 int n, i;
3581 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003582
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003583 n = thermal_get_sensors(&t);
3584 if (unlikely(n < 0))
3585 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003586
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003587 len += sprintf(p + len, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003588
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003589 if (n > 0) {
3590 for (i = 0; i < (n - 1); i++)
3591 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
3592 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
3593 } else
3594 len += sprintf(p + len, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003595
3596 return len;
3597}
3598
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003599static struct ibm_struct thermal_driver_data = {
3600 .name = "thermal",
3601 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003602 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003603};
3604
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003605/*************************************************************************
3606 * EC Dump subdriver
3607 */
3608
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003609static u8 ecdump_regs[256];
3610
3611static int ecdump_read(char *p)
3612{
3613 int len = 0;
3614 int i, j;
3615 u8 v;
3616
3617 len += sprintf(p + len, "EC "
3618 " +00 +01 +02 +03 +04 +05 +06 +07"
3619 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
3620 for (i = 0; i < 256; i += 16) {
3621 len += sprintf(p + len, "EC 0x%02x:", i);
3622 for (j = 0; j < 16; j++) {
3623 if (!acpi_ec_read(i + j, &v))
3624 break;
3625 if (v != ecdump_regs[i + j])
3626 len += sprintf(p + len, " *%02x", v);
3627 else
3628 len += sprintf(p + len, " %02x", v);
3629 ecdump_regs[i + j] = v;
3630 }
3631 len += sprintf(p + len, "\n");
3632 if (j != 16)
3633 break;
3634 }
3635
3636 /* These are way too dangerous to advertise openly... */
3637#if 0
3638 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
3639 " (<offset> is 00-ff, <value> is 00-ff)\n");
3640 len += sprintf(p + len, "commands:\t0x<offset> <value> "
3641 " (<offset> is 00-ff, <value> is 0-255)\n");
3642#endif
3643 return len;
3644}
3645
3646static int ecdump_write(char *buf)
3647{
3648 char *cmd;
3649 int i, v;
3650
3651 while ((cmd = next_cmd(&buf))) {
3652 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
3653 /* i and v set */
3654 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
3655 /* i and v set */
3656 } else
3657 return -EINVAL;
3658 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
3659 if (!acpi_ec_write(i, v))
3660 return -EIO;
3661 } else
3662 return -EINVAL;
3663 }
3664
3665 return 0;
3666}
3667
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003668static struct ibm_struct ecdump_driver_data = {
3669 .name = "ecdump",
3670 .read = ecdump_read,
3671 .write = ecdump_write,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03003672 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003673};
3674
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003675/*************************************************************************
3676 * Backlight/brightness subdriver
3677 */
Holger Macht8acb0252006-10-20 14:30:28 -07003678
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03003679static struct backlight_device *ibm_backlight_device;
Holger Macht8acb0252006-10-20 14:30:28 -07003680
Richard Purdie599a52d2007-02-10 23:07:48 +00003681static struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02003682 .get_brightness = brightness_get,
3683 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02003684};
3685
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03003686static struct mutex brightness_mutex;
3687
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02003688static int __init tpacpi_query_bcll_levels(acpi_handle handle)
3689{
3690 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
3691 union acpi_object *obj;
3692 int rc;
3693
3694 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
3695 obj = (union acpi_object *)buffer.pointer;
3696 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
3697 printk(IBM_ERR "Unknown BCLL data, "
3698 "please report this to %s\n", IBM_MAIL);
3699 rc = 0;
3700 } else {
3701 rc = obj->package.count;
3702 }
3703 } else {
3704 return 0;
3705 }
3706
3707 kfree(buffer.pointer);
3708 return rc;
3709}
3710
3711static acpi_status __init brightness_find_bcll(acpi_handle handle, u32 lvl,
3712 void *context, void **rv)
3713{
3714 char name[ACPI_PATH_SEGMENT_LENGTH];
3715 struct acpi_buffer buffer = { sizeof(name), &name };
3716
3717 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
3718 !strncmp("BCLL", name, sizeof(name) - 1)) {
3719 if (tpacpi_query_bcll_levels(handle) == 16) {
3720 *rv = handle;
3721 return AE_CTRL_TERMINATE;
3722 } else {
3723 return AE_OK;
3724 }
3725 } else {
3726 return AE_OK;
3727 }
3728}
3729
3730static int __init brightness_check_levels(void)
3731{
3732 int status;
3733 void *found_node = NULL;
3734
3735 if (!vid_handle) {
3736 IBM_ACPIHANDLE_INIT(vid);
3737 }
3738 if (!vid_handle)
3739 return 0;
3740
3741 /* Search for a BCLL package with 16 levels */
3742 status = acpi_walk_namespace(ACPI_TYPE_PACKAGE, vid_handle, 3,
3743 brightness_find_bcll, NULL, &found_node);
3744
3745 return (ACPI_SUCCESS(status) && found_node != NULL);
3746}
3747
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02003748static acpi_status __init brightness_find_bcl(acpi_handle handle, u32 lvl,
3749 void *context, void **rv)
3750{
3751 char name[ACPI_PATH_SEGMENT_LENGTH];
3752 struct acpi_buffer buffer = { sizeof(name), &name };
3753
3754 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
3755 !strncmp("_BCL", name, sizeof(name) - 1)) {
3756 *rv = handle;
3757 return AE_CTRL_TERMINATE;
3758 } else {
3759 return AE_OK;
3760 }
3761}
3762
3763static int __init brightness_check_std_acpi_support(void)
3764{
3765 int status;
3766 void *found_node = NULL;
3767
3768 if (!vid_handle) {
3769 IBM_ACPIHANDLE_INIT(vid);
3770 }
3771 if (!vid_handle)
3772 return 0;
3773
3774 /* Search for a _BCL method, but don't execute it */
3775 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
3776 brightness_find_bcl, NULL, &found_node);
3777
3778 return (ACPI_SUCCESS(status) && found_node != NULL);
3779}
3780
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003781static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02003782{
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02003783 int b;
3784
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003785 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
3786
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03003787 mutex_init(&brightness_mutex);
3788
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02003789 if (!brightness_enable) {
3790 dbg_printk(TPACPI_DBG_INIT,
3791 "brightness support disabled by module parameter\n");
3792 return 1;
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02003793 } else if (brightness_enable > 1) {
3794 if (brightness_check_std_acpi_support()) {
3795 printk(IBM_NOTICE
3796 "standard ACPI backlight interface available, not loading native one...\n");
3797 return 1;
3798 }
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02003799 }
3800
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03003801 if (!brightness_mode) {
3802 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
3803 brightness_mode = 2;
3804 else
3805 brightness_mode = 3;
3806
3807 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
3808 brightness_mode);
3809 }
3810
3811 if (brightness_mode > 3)
3812 return -EINVAL;
3813
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02003814 tp_features.bright_16levels =
3815 thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO &&
3816 brightness_check_levels();
3817
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02003818 b = brightness_get(NULL);
3819 if (b < 0)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03003820 return 1;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02003821
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02003822 if (tp_features.bright_16levels)
3823 printk(IBM_INFO "detected a 16-level brightness capable ThinkPad\n");
3824
Henrique de Moraes Holschuh7d5a0152007-04-24 11:48:20 -03003825 ibm_backlight_device = backlight_device_register(
3826 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
3827 &ibm_backlight_data);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02003828 if (IS_ERR(ibm_backlight_device)) {
3829 printk(IBM_ERR "Could not register backlight device\n");
3830 return PTR_ERR(ibm_backlight_device);
3831 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003832 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02003833
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02003834 ibm_backlight_device->props.max_brightness =
3835 (tp_features.bright_16levels)? 15 : 7;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02003836 ibm_backlight_device->props.brightness = b;
3837 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00003838
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02003839 return 0;
3840}
3841
3842static void brightness_exit(void)
3843{
3844 if (ibm_backlight_device) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003845 vdbg_printk(TPACPI_DBG_EXIT,
3846 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02003847 backlight_device_unregister(ibm_backlight_device);
3848 ibm_backlight_device = NULL;
3849 }
3850}
3851
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003852static int brightness_update_status(struct backlight_device *bd)
3853{
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02003854 /* it is the backlight class's job (caller) to handle
3855 * EINTR and other errors properly */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003856 return brightness_set(
3857 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
3858 bd->props.power == FB_BLANK_UNBLANK) ?
3859 bd->props.brightness : 0);
3860}
3861
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03003862/*
3863 * ThinkPads can read brightness from two places: EC 0x31, or
3864 * CMOS NVRAM byte 0x5E, bits 0-3.
3865 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003866static int brightness_get(struct backlight_device *bd)
3867{
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03003868 u8 lec = 0, lcmos = 0, level = 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003869
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03003870 if (brightness_mode & 1) {
3871 if (!acpi_ec_read(brightness_offset, &lec))
3872 return -EIO;
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02003873 lec &= (tp_features.bright_16levels)? 0x0f : 0x07;
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03003874 level = lec;
3875 };
3876 if (brightness_mode & 2) {
3877 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
3878 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
3879 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02003880 lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03003881 level = lcmos;
3882 }
3883
3884 if (brightness_mode == 3 && lec != lcmos) {
3885 printk(IBM_ERR
3886 "CMOS NVRAM (%u) and EC (%u) do not agree "
3887 "on display brightness level\n",
3888 (unsigned int) lcmos,
3889 (unsigned int) lec);
3890 return -EIO;
3891 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003892
3893 return level;
3894}
3895
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02003896/* May return EINTR which can always be mapped to ERESTARTSYS */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003897static int brightness_set(int value)
3898{
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03003899 int cmos_cmd, inc, i, res;
3900 int current_value;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003901
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02003902 if (value > ((tp_features.bright_16levels)? 15 : 7))
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03003903 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003904
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03003905 res = mutex_lock_interruptible(&brightness_mutex);
3906 if (res < 0)
3907 return res;
3908
3909 current_value = brightness_get(NULL);
3910 if (current_value < 0) {
3911 res = current_value;
3912 goto errout;
3913 }
3914
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03003915 cmos_cmd = value > current_value ?
3916 TP_CMOS_BRIGHTNESS_UP :
3917 TP_CMOS_BRIGHTNESS_DOWN;
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02003918 inc = (value > current_value)? 1 : -1;
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03003919
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03003920 res = 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003921 for (i = current_value; i != value; i += inc) {
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03003922 if ((brightness_mode & 2) &&
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03003923 issue_thinkpad_cmos_command(cmos_cmd)) {
3924 res = -EIO;
3925 goto errout;
3926 }
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03003927 if ((brightness_mode & 1) &&
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03003928 !acpi_ec_write(brightness_offset, i + inc)) {
3929 res = -EIO;
3930 goto errout;;
3931 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003932 }
3933
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03003934errout:
3935 mutex_unlock(&brightness_mutex);
3936 return res;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003937}
3938
3939static int brightness_read(char *p)
3940{
3941 int len = 0;
3942 int level;
3943
3944 if ((level = brightness_get(NULL)) < 0) {
3945 len += sprintf(p + len, "level:\t\tunreadable\n");
3946 } else {
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02003947 len += sprintf(p + len, "level:\t\t%d\n", level);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003948 len += sprintf(p + len, "commands:\tup, down\n");
3949 len += sprintf(p + len, "commands:\tlevel <level>"
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02003950 " (<level> is 0-%d)\n",
3951 (tp_features.bright_16levels) ? 15 : 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003952 }
3953
3954 return len;
3955}
3956
3957static int brightness_write(char *buf)
3958{
3959 int level;
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02003960 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003961 char *cmd;
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02003962 int max_level = (tp_features.bright_16levels) ? 15 : 7;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003963
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02003964 level = brightness_get(NULL);
3965 if (level < 0)
3966 return level;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003967
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02003968 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003969 if (strlencmp(cmd, "up") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02003970 if (level < max_level)
3971 level++;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003972 } else if (strlencmp(cmd, "down") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02003973 if (level > 0)
3974 level--;
3975 } else if (sscanf(cmd, "level %d", &level) == 1 &&
3976 level >= 0 && level <= max_level) {
3977 /* new level set */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003978 } else
3979 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003980 }
3981
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02003982 /*
3983 * Now we know what the final level should be, so we try to set it.
3984 * Doing it this way makes the syscall restartable in case of EINTR
3985 */
3986 rc = brightness_set(level);
3987 return (rc == -EINTR)? ERESTARTSYS : rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003988}
3989
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003990static struct ibm_struct brightness_driver_data = {
3991 .name = "brightness",
3992 .read = brightness_read,
3993 .write = brightness_write,
3994 .exit = brightness_exit,
3995};
3996
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003997/*************************************************************************
3998 * Volume subdriver
3999 */
4000
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004001static int volume_read(char *p)
4002{
4003 int len = 0;
4004 u8 level;
4005
4006 if (!acpi_ec_read(volume_offset, &level)) {
4007 len += sprintf(p + len, "level:\t\tunreadable\n");
4008 } else {
4009 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
4010 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
4011 len += sprintf(p + len, "commands:\tup, down, mute\n");
4012 len += sprintf(p + len, "commands:\tlevel <level>"
4013 " (<level> is 0-15)\n");
4014 }
4015
4016 return len;
4017}
4018
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004019static int volume_write(char *buf)
4020{
4021 int cmos_cmd, inc, i;
4022 u8 level, mute;
4023 int new_level, new_mute;
4024 char *cmd;
4025
4026 while ((cmd = next_cmd(&buf))) {
4027 if (!acpi_ec_read(volume_offset, &level))
4028 return -EIO;
4029 new_mute = mute = level & 0x40;
4030 new_level = level = level & 0xf;
4031
4032 if (strlencmp(cmd, "up") == 0) {
4033 if (mute)
4034 new_mute = 0;
4035 else
4036 new_level = level == 15 ? 15 : level + 1;
4037 } else if (strlencmp(cmd, "down") == 0) {
4038 if (mute)
4039 new_mute = 0;
4040 else
4041 new_level = level == 0 ? 0 : level - 1;
4042 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
4043 new_level >= 0 && new_level <= 15) {
4044 /* new_level set */
4045 } else if (strlencmp(cmd, "mute") == 0) {
4046 new_mute = 0x40;
4047 } else
4048 return -EINVAL;
4049
4050 if (new_level != level) { /* mute doesn't change */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004051 cmos_cmd = new_level > level ? TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004052 inc = new_level > level ? 1 : -1;
4053
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004054 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004055 !acpi_ec_write(volume_offset, level)))
4056 return -EIO;
4057
4058 for (i = level; i != new_level; i += inc)
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004059 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004060 !acpi_ec_write(volume_offset, i + inc))
4061 return -EIO;
4062
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004063 if (mute && (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004064 !acpi_ec_write(volume_offset,
4065 new_level + mute)))
4066 return -EIO;
4067 }
4068
4069 if (new_mute != mute) { /* level doesn't change */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004070 cmos_cmd = new_mute ? TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004071
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004072 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004073 !acpi_ec_write(volume_offset, level + new_mute))
4074 return -EIO;
4075 }
4076 }
4077
4078 return 0;
4079}
4080
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004081static struct ibm_struct volume_driver_data = {
4082 .name = "volume",
4083 .read = volume_read,
4084 .write = volume_write,
4085};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004086
4087/*************************************************************************
4088 * Fan subdriver
4089 */
4090
4091/*
4092 * FAN ACCESS MODES
4093 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004094 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004095 * ACPI GFAN method: returns fan level
4096 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004097 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004098 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004099 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004100 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004101 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
4102 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004103 * EC 0x2f (HFSP) might be available *for reading*, but do not use
4104 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004105 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004106 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004107 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
4108 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004109 *
4110 * Fan speed changes of any sort (including those caused by the
4111 * disengaged mode) are usually done slowly by the firmware as the
4112 * maximum ammount of fan duty cycle change per second seems to be
4113 * limited.
4114 *
4115 * Reading is not available if GFAN exists.
4116 * Writing is not available if SFAN exists.
4117 *
4118 * Bits
4119 * 7 automatic mode engaged;
4120 * (default operation mode of the ThinkPad)
4121 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004122 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004123 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004124 * the tachometer while the fan controller ramps up
4125 * the speed (which can take up to a few *minutes*).
4126 * Speeds up fan to 100% duty-cycle, which is far above
4127 * the standard RPM levels. It is not impossible that
4128 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004129 * 5-3 unused in some models. Extra bits for fan level
4130 * in others, but still useless as all values above
4131 * 7 map to the same speed as level 7 in these models.
4132 * 2-0 fan level (0..7 usually)
4133 * 0x00 = stop
4134 * 0x07 = max (set when temperatures critical)
4135 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004136 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004137 *
4138 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
4139 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
4140 * does so, its initial value is meaningless (0x07).
4141 *
4142 * For firmware bugs, refer to:
4143 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
4144 *
4145 * ----
4146 *
4147 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
4148 * Main fan tachometer reading (in RPM)
4149 *
4150 * This register is present on all ThinkPads with a new-style EC, and
4151 * it is known not to be present on the A21m/e, and T22, as there is
4152 * something else in offset 0x84 according to the ACPI DSDT. Other
4153 * ThinkPads from this same time period (and earlier) probably lack the
4154 * tachometer as well.
4155 *
4156 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
4157 * was never fixed by IBM to report the EC firmware version string
4158 * probably support the tachometer (like the early X models), so
4159 * detecting it is quite hard. We need more data to know for sure.
4160 *
4161 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
4162 * might result.
4163 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004164 * FIRMWARE BUG: may go stale while the EC is switching to full speed
4165 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004166 *
4167 * For firmware bugs, refer to:
4168 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
4169 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004170 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004171 * ThinkPad X31, X40, X41. Not available in the X60.
4172 *
4173 * FANS ACPI handle: takes three arguments: low speed, medium speed,
4174 * high speed. ACPI DSDT seems to map these three speeds to levels
4175 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
4176 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
4177 *
4178 * The speeds are stored on handles
4179 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
4180 *
4181 * There are three default speed sets, acessible as handles:
4182 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
4183 *
4184 * ACPI DSDT switches which set is in use depending on various
4185 * factors.
4186 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004187 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004188 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
4189 * but the ACPI tables just mention level 7.
4190 */
4191
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02004192static enum fan_status_access_mode fan_status_access_mode;
4193static enum fan_control_access_mode fan_control_access_mode;
4194static enum fan_control_commands fan_control_commands;
4195
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02004196static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004197static u8 fan_control_desired_level;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02004198
Len Brown25c68a32006-12-08 04:43:41 -05004199static void fan_watchdog_fire(struct work_struct *ignored);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02004200static int fan_watchdog_maxinterval;
Len Brown25c68a32006-12-08 04:43:41 -05004201static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02004202
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004203IBM_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
4204IBM_HANDLE(gfan, ec, "GFAN", /* 570 */
4205 "\\FSPD", /* 600e/x, 770e, 770x */
4206 ); /* all others */
4207IBM_HANDLE(sfan, ec, "SFAN", /* 570 */
4208 "JFNS", /* 770x-JL */
4209 ); /* all others */
4210
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004211/*
4212 * SYSFS fan layout: hwmon compatible (device)
4213 *
4214 * pwm*_enable:
4215 * 0: "disengaged" mode
4216 * 1: manual mode
4217 * 2: native EC "auto" mode (recommended, hardware default)
4218 *
4219 * pwm*: set speed in manual mode, ignored otherwise.
4220 * 0 is level 0; 255 is level 7. Intermediate points done with linear
4221 * interpolation.
4222 *
4223 * fan*_input: tachometer reading, RPM
4224 *
4225 *
4226 * SYSFS fan layout: extensions
4227 *
4228 * fan_watchdog (driver):
4229 * fan watchdog interval in seconds, 0 disables (default), max 120
4230 */
4231
4232/* sysfs fan pwm1_enable ----------------------------------------------- */
4233static ssize_t fan_pwm1_enable_show(struct device *dev,
4234 struct device_attribute *attr,
4235 char *buf)
4236{
4237 int res, mode;
4238 u8 status;
4239
4240 res = fan_get_status_safe(&status);
4241 if (res)
4242 return res;
4243
4244 if (unlikely(tp_features.fan_ctrl_status_undef)) {
4245 if (status != fan_control_initial_status) {
4246 tp_features.fan_ctrl_status_undef = 0;
4247 } else {
4248 /* Return most likely status. In fact, it
4249 * might be the only possible status */
4250 status = TP_EC_FAN_AUTO;
4251 }
4252 }
4253
4254 if (status & TP_EC_FAN_FULLSPEED) {
4255 mode = 0;
4256 } else if (status & TP_EC_FAN_AUTO) {
4257 mode = 2;
4258 } else
4259 mode = 1;
4260
4261 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
4262}
4263
4264static ssize_t fan_pwm1_enable_store(struct device *dev,
4265 struct device_attribute *attr,
4266 const char *buf, size_t count)
4267{
4268 unsigned long t;
4269 int res, level;
4270
4271 if (parse_strtoul(buf, 2, &t))
4272 return -EINVAL;
4273
4274 switch (t) {
4275 case 0:
4276 level = TP_EC_FAN_FULLSPEED;
4277 break;
4278 case 1:
4279 level = TPACPI_FAN_LAST_LEVEL;
4280 break;
4281 case 2:
4282 level = TP_EC_FAN_AUTO;
4283 break;
4284 case 3:
4285 /* reserved for software-controlled auto mode */
4286 return -ENOSYS;
4287 default:
4288 return -EINVAL;
4289 }
4290
4291 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03004292 if (res == -ENXIO)
4293 return -EINVAL;
4294 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004295 return res;
4296
4297 fan_watchdog_reset();
4298
4299 return count;
4300}
4301
4302static struct device_attribute dev_attr_fan_pwm1_enable =
4303 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
4304 fan_pwm1_enable_show, fan_pwm1_enable_store);
4305
4306/* sysfs fan pwm1 ------------------------------------------------------ */
4307static ssize_t fan_pwm1_show(struct device *dev,
4308 struct device_attribute *attr,
4309 char *buf)
4310{
4311 int res;
4312 u8 status;
4313
4314 res = fan_get_status_safe(&status);
4315 if (res)
4316 return res;
4317
4318 if (unlikely(tp_features.fan_ctrl_status_undef)) {
4319 if (status != fan_control_initial_status) {
4320 tp_features.fan_ctrl_status_undef = 0;
4321 } else {
4322 status = TP_EC_FAN_AUTO;
4323 }
4324 }
4325
4326 if ((status &
4327 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
4328 status = fan_control_desired_level;
4329
4330 if (status > 7)
4331 status = 7;
4332
4333 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
4334}
4335
4336static ssize_t fan_pwm1_store(struct device *dev,
4337 struct device_attribute *attr,
4338 const char *buf, size_t count)
4339{
4340 unsigned long s;
4341 int rc;
4342 u8 status, newlevel;
4343
4344 if (parse_strtoul(buf, 255, &s))
4345 return -EINVAL;
4346
4347 /* scale down from 0-255 to 0-7 */
4348 newlevel = (s >> 5) & 0x07;
4349
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02004350 if (mutex_lock_interruptible(&fan_mutex))
4351 return -ERESTARTSYS;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004352
4353 rc = fan_get_status(&status);
4354 if (!rc && (status &
4355 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
4356 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03004357 if (rc == -ENXIO)
4358 rc = -EINVAL;
4359 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004360 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03004361 fan_watchdog_reset();
4362 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004363 }
4364
4365 mutex_unlock(&fan_mutex);
4366 return (rc)? rc : count;
4367}
4368
4369static struct device_attribute dev_attr_fan_pwm1 =
4370 __ATTR(pwm1, S_IWUSR | S_IRUGO,
4371 fan_pwm1_show, fan_pwm1_store);
4372
4373/* sysfs fan fan1_input ------------------------------------------------ */
4374static ssize_t fan_fan1_input_show(struct device *dev,
4375 struct device_attribute *attr,
4376 char *buf)
4377{
4378 int res;
4379 unsigned int speed;
4380
4381 res = fan_get_speed(&speed);
4382 if (res < 0)
4383 return res;
4384
4385 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
4386}
4387
4388static struct device_attribute dev_attr_fan_fan1_input =
4389 __ATTR(fan1_input, S_IRUGO,
4390 fan_fan1_input_show, NULL);
4391
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03004392/* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004393static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
4394 char *buf)
4395{
4396 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
4397}
4398
4399static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
4400 const char *buf, size_t count)
4401{
4402 unsigned long t;
4403
4404 if (parse_strtoul(buf, 120, &t))
4405 return -EINVAL;
4406
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03004407 if (!fan_control_allowed)
4408 return -EPERM;
4409
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004410 fan_watchdog_maxinterval = t;
4411 fan_watchdog_reset();
4412
4413 return count;
4414}
4415
4416static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
4417 fan_fan_watchdog_show, fan_fan_watchdog_store);
4418
4419/* --------------------------------------------------------------------- */
4420static struct attribute *fan_attributes[] = {
4421 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
4422 &dev_attr_fan_fan1_input.attr,
4423 NULL
4424};
4425
4426static const struct attribute_group fan_attr_group = {
4427 .attrs = fan_attributes,
4428};
4429
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004430static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02004431{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004432 int rc;
4433
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004434 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
4435
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004436 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004437 fan_status_access_mode = TPACPI_FAN_NONE;
4438 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02004439 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02004440 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004441 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004442 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02004443
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004444 IBM_ACPIHANDLE_INIT(fans);
4445 IBM_ACPIHANDLE_INIT(gfan);
4446 IBM_ACPIHANDLE_INIT(sfan);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004447
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02004448 if (gfan_handle) {
4449 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004450 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02004451 } else {
4452 /* all other ThinkPads: note that even old-style
4453 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02004454 if (likely(acpi_ec_read(fan_status_offset,
4455 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004456 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02004457
4458 /* In some ThinkPads, neither the EC nor the ACPI
4459 * DSDT initialize the fan status, and it ends up
4460 * being set to 0x07 when it *could* be either
4461 * 0x07 or 0x80.
4462 *
4463 * Enable for TP-1Y (T43), TP-78 (R51e),
4464 * TP-76 (R52), TP-70 (T43, R52), which are known
4465 * to be buggy. */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03004466 if (fan_control_initial_status == 0x07) {
4467 switch (thinkpad_id.ec_model) {
4468 case 0x5931: /* TP-1Y */
4469 case 0x3837: /* TP-78 */
4470 case 0x3637: /* TP-76 */
4471 case 0x3037: /* TP-70 */
4472 printk(IBM_NOTICE
4473 "fan_init: initial fan status is "
4474 "unknown, assuming it is in auto "
4475 "mode\n");
4476 tp_features.fan_ctrl_status_undef = 1;
4477 ;;
4478 }
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02004479 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02004480 } else {
4481 printk(IBM_ERR
4482 "ThinkPad ACPI EC access misbehaving, "
4483 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004484 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02004485 }
4486 }
4487
4488 if (sfan_handle) {
4489 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004490 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02004491 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004492 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02004493 } else {
4494 if (!gfan_handle) {
4495 /* gfan without sfan means no fan control */
4496 /* all other models implement TP EC 0x2f control */
4497
4498 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02004499 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02004500 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004501 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02004502 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004503 TPACPI_FAN_CMD_SPEED |
4504 TPACPI_FAN_CMD_LEVEL |
4505 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02004506 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004507 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02004508 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004509 TPACPI_FAN_CMD_LEVEL |
4510 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02004511 }
4512 }
4513 }
4514
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004515 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
4516 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
4517 fan_control_access_mode != TPACPI_FAN_WR_NONE),
4518 fan_status_access_mode, fan_control_access_mode);
4519
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03004520 /* fan control master switch */
4521 if (!fan_control_allowed) {
4522 fan_control_access_mode = TPACPI_FAN_WR_NONE;
4523 fan_control_commands = 0;
4524 dbg_printk(TPACPI_DBG_INIT,
4525 "fan control features disabled by parameter\n");
4526 }
4527
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004528 /* update fan_control_desired_level */
4529 if (fan_status_access_mode != TPACPI_FAN_NONE)
4530 fan_get_status_safe(NULL);
4531
4532 if (fan_status_access_mode != TPACPI_FAN_NONE ||
4533 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03004534 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004535 &fan_attr_group);
4536 if (!(rc < 0))
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03004537 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004538 &driver_attr_fan_watchdog);
4539 if (rc < 0)
4540 return rc;
4541 return 0;
4542 } else
4543 return 1;
4544}
4545
4546/*
4547 * Call with fan_mutex held
4548 */
4549static void fan_update_desired_level(u8 status)
4550{
4551 if ((status &
4552 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
4553 if (status > 7)
4554 fan_control_desired_level = 7;
4555 else
4556 fan_control_desired_level = status;
4557 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02004558}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004559
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02004560static int fan_get_status(u8 *status)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004561{
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02004562 u8 s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004563
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02004564 /* TODO:
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004565 * Add TPACPI_FAN_RD_ACPI_FANS ? */
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02004566
Henrique de Moraes Holschuh3ef8a602006-11-24 11:47:10 -02004567 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004568 case TPACPI_FAN_RD_ACPI_GFAN:
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004569 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02004570
4571 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004572 return -EIO;
4573
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02004574 if (likely(status))
4575 *status = s & 0x07;
4576
4577 break;
4578
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004579 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02004580 /* all except 570, 600e/x, 770e, 770x */
4581 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
4582 return -EIO;
4583
4584 if (likely(status))
4585 *status = s;
4586
4587 break;
4588
4589 default:
4590 return -ENXIO;
4591 }
4592
4593 return 0;
4594}
4595
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004596static int fan_get_status_safe(u8 *status)
4597{
4598 int rc;
4599 u8 s;
4600
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02004601 if (mutex_lock_interruptible(&fan_mutex))
4602 return -ERESTARTSYS;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004603 rc = fan_get_status(&s);
4604 if (!rc)
4605 fan_update_desired_level(s);
4606 mutex_unlock(&fan_mutex);
4607
4608 if (status)
4609 *status = s;
4610
4611 return rc;
4612}
4613
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004614static void fan_exit(void)
4615{
Henrique de Moraes Holschuh99fba3f2007-04-21 11:08:44 -03004616 vdbg_printk(TPACPI_DBG_EXIT, "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004617
4618 /* FIXME: can we really do this unconditionally? */
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03004619 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
4620 driver_remove_file(&tpacpi_hwmon_pdriver.driver, &driver_attr_fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004621
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004622 cancel_delayed_work(&fan_watchdog_task);
4623 flush_scheduled_work();
4624}
4625
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02004626static int fan_get_speed(unsigned int *speed)
4627{
4628 u8 hi, lo;
4629
4630 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004631 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02004632 /* all except 570, 600e/x, 770e, 770x */
4633 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
4634 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
4635 return -EIO;
4636
4637 if (likely(speed))
4638 *speed = (hi << 8) | lo;
4639
4640 break;
4641
4642 default:
4643 return -ENXIO;
4644 }
4645
4646 return 0;
4647}
4648
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004649static void fan_watchdog_fire(struct work_struct *ignored)
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02004650{
Henrique de Moraes Holschuh99fba3f2007-04-21 11:08:44 -03004651 int rc;
4652
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03004653 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
4654 return;
4655
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004656 printk(IBM_NOTICE "fan watchdog: enabling fan\n");
Henrique de Moraes Holschuh99fba3f2007-04-21 11:08:44 -03004657 rc = fan_set_enable();
4658 if (rc < 0) {
4659 printk(IBM_ERR "fan watchdog: error %d while enabling fan, "
4660 "will try again later...\n", -rc);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004661 /* reschedule for later */
4662 fan_watchdog_reset();
4663 }
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02004664}
4665
4666static void fan_watchdog_reset(void)
4667{
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03004668 static int fan_watchdog_active;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02004669
Henrique de Moraes Holschuh4985cd02007-04-27 22:00:10 -03004670 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
4671 return;
4672
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02004673 if (fan_watchdog_active)
4674 cancel_delayed_work(&fan_watchdog_task);
4675
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03004676 if (fan_watchdog_maxinterval > 0 &&
4677 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02004678 fan_watchdog_active = 1;
4679 if (!schedule_delayed_work(&fan_watchdog_task,
4680 msecs_to_jiffies(fan_watchdog_maxinterval
4681 * 1000))) {
4682 printk(IBM_ERR "failed to schedule the fan watchdog, "
4683 "watchdog will not trigger\n");
4684 }
4685 } else
4686 fan_watchdog_active = 0;
4687}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004688
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004689static int fan_set_level(int level)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004690{
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03004691 if (!fan_control_allowed)
4692 return -EPERM;
4693
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004694 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004695 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004696 if (level >= 0 && level <= 7) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004697 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004698 return -EIO;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004699 } else
4700 return -EINVAL;
4701 break;
4702
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004703 case TPACPI_FAN_WR_ACPI_FANS:
4704 case TPACPI_FAN_WR_TPEC:
4705 if ((level != TP_EC_FAN_AUTO) &&
4706 (level != TP_EC_FAN_FULLSPEED) &&
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02004707 ((level < 0) || (level > 7)))
4708 return -EINVAL;
4709
Henrique de Moraes Holschuheaa75712007-04-24 11:48:18 -03004710 /* safety net should the EC not support AUTO
4711 * or FULLSPEED mode bits and just ignore them */
4712 if (level & TP_EC_FAN_FULLSPEED)
4713 level |= 7; /* safety min speed 7 */
4714 else if (level & TP_EC_FAN_FULLSPEED)
4715 level |= 4; /* safety min speed 4 */
4716
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004717 if (!acpi_ec_write(fan_status_offset, level))
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02004718 return -EIO;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02004719 else
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004720 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02004721 break;
4722
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004723 default:
4724 return -ENXIO;
4725 }
4726 return 0;
4727}
4728
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004729static int fan_set_level_safe(int level)
4730{
4731 int rc;
4732
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03004733 if (!fan_control_allowed)
4734 return -EPERM;
4735
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02004736 if (mutex_lock_interruptible(&fan_mutex))
4737 return -ERESTARTSYS;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004738
4739 if (level == TPACPI_FAN_LAST_LEVEL)
4740 level = fan_control_desired_level;
4741
4742 rc = fan_set_level(level);
4743 if (!rc)
4744 fan_update_desired_level(level);
4745
4746 mutex_unlock(&fan_mutex);
4747 return rc;
4748}
4749
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004750static int fan_set_enable(void)
4751{
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02004752 u8 s;
4753 int rc;
4754
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03004755 if (!fan_control_allowed)
4756 return -EPERM;
4757
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02004758 if (mutex_lock_interruptible(&fan_mutex))
4759 return -ERESTARTSYS;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004760
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004761 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004762 case TPACPI_FAN_WR_ACPI_FANS:
4763 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004764 rc = fan_get_status(&s);
4765 if (rc < 0)
4766 break;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02004767
4768 /* Don't go out of emergency fan mode */
Henrique de Moraes Holschuheaa75712007-04-24 11:48:18 -03004769 if (s != 7) {
4770 s &= 0x07;
4771 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
4772 }
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02004773
4774 if (!acpi_ec_write(fan_status_offset, s))
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004775 rc = -EIO;
4776 else {
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004777 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004778 rc = 0;
4779 }
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02004780 break;
4781
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004782 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004783 rc = fan_get_status(&s);
4784 if (rc < 0)
4785 break;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02004786
4787 s &= 0x07;
4788
4789 /* Set fan to at least level 4 */
Henrique de Moraes Holschuheaa75712007-04-24 11:48:18 -03004790 s |= 4;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02004791
4792 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004793 rc= -EIO;
4794 else
4795 rc = 0;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004796 break;
4797
4798 default:
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004799 rc = -ENXIO;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004800 }
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004801
4802 mutex_unlock(&fan_mutex);
4803 return rc;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004804}
4805
4806static int fan_set_disable(void)
4807{
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004808 int rc;
4809
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03004810 if (!fan_control_allowed)
4811 return -EPERM;
4812
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02004813 if (mutex_lock_interruptible(&fan_mutex))
4814 return -ERESTARTSYS;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004815
4816 rc = 0;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004817 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004818 case TPACPI_FAN_WR_ACPI_FANS:
4819 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004820 if (!acpi_ec_write(fan_status_offset, 0x00))
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004821 rc = -EIO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004822 else {
4823 fan_control_desired_level = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004824 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004825 }
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004826 break;
4827
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004828 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02004829 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004830 rc = -EIO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004831 else
4832 fan_control_desired_level = 0;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02004833 break;
4834
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004835 default:
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004836 rc = -ENXIO;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004837 }
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004838
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004839
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004840 mutex_unlock(&fan_mutex);
4841 return rc;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004842}
4843
4844static int fan_set_speed(int speed)
4845{
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004846 int rc;
4847
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03004848 if (!fan_control_allowed)
4849 return -EPERM;
4850
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02004851 if (mutex_lock_interruptible(&fan_mutex))
4852 return -ERESTARTSYS;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004853
4854 rc = 0;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004855 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004856 case TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004857 if (speed >= 0 && speed <= 65535) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004858 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
4859 speed, speed, speed))
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004860 rc = -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004861 } else
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004862 rc = -EINVAL;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004863 break;
4864
4865 default:
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004866 rc = -ENXIO;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004867 }
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004868
4869 mutex_unlock(&fan_mutex);
4870 return rc;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004871}
4872
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004873static int fan_read(char *p)
4874{
4875 int len = 0;
4876 int rc;
4877 u8 status;
4878 unsigned int speed = 0;
4879
4880 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004881 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004882 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004883 if ((rc = fan_get_status_safe(&status)) < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004884 return rc;
4885
4886 len += sprintf(p + len, "status:\t\t%s\n"
4887 "level:\t\t%d\n",
4888 (status != 0) ? "enabled" : "disabled", status);
4889 break;
4890
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004891 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004892 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004893 if ((rc = fan_get_status_safe(&status)) < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004894 return rc;
4895
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004896 if (unlikely(tp_features.fan_ctrl_status_undef)) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004897 if (status != fan_control_initial_status)
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004898 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004899 else
4900 /* Return most likely status. In fact, it
4901 * might be the only possible status */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004902 status = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004903 }
4904
4905 len += sprintf(p + len, "status:\t\t%s\n",
4906 (status != 0) ? "enabled" : "disabled");
4907
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004908 if ((rc = fan_get_speed(&speed)) < 0)
4909 return rc;
4910
4911 len += sprintf(p + len, "speed:\t\t%d\n", speed);
4912
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004913 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004914 /* Disengaged mode takes precedence */
4915 len += sprintf(p + len, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004916 else if (status & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004917 len += sprintf(p + len, "level:\t\tauto\n");
4918 else
4919 len += sprintf(p + len, "level:\t\t%d\n", status);
4920 break;
4921
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004922 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004923 default:
4924 len += sprintf(p + len, "status:\t\tnot supported\n");
4925 }
4926
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004927 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004928 len += sprintf(p + len, "commands:\tlevel <level>");
4929
4930 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004931 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004932 len += sprintf(p + len, " (<level> is 0-7)\n");
4933 break;
4934
4935 default:
4936 len += sprintf(p + len, " (<level> is 0-7, "
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004937 "auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004938 break;
4939 }
4940 }
4941
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004942 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004943 len += sprintf(p + len, "commands:\tenable, disable\n"
4944 "commands:\twatchdog <timeout> (<timeout> is 0 (off), "
4945 "1-120 (seconds))\n");
4946
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004947 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004948 len += sprintf(p + len, "commands:\tspeed <speed>"
4949 " (<speed> is 0-65535)\n");
4950
4951 return len;
4952}
4953
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004954static int fan_write_cmd_level(const char *cmd, int *rc)
4955{
4956 int level;
4957
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02004958 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004959 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004960 else if ((strlencmp(cmd, "level disengaged") == 0) |
4961 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004962 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02004963 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004964 return 0;
4965
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004966 if ((*rc = fan_set_level_safe(level)) == -ENXIO)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004967 printk(IBM_ERR "level command accepted for unsupported "
4968 "access mode %d", fan_control_access_mode);
4969
4970 return 1;
4971}
4972
4973static int fan_write_cmd_enable(const char *cmd, int *rc)
4974{
4975 if (strlencmp(cmd, "enable") != 0)
4976 return 0;
4977
4978 if ((*rc = fan_set_enable()) == -ENXIO)
4979 printk(IBM_ERR "enable command accepted for unsupported "
4980 "access mode %d", fan_control_access_mode);
4981
4982 return 1;
4983}
4984
4985static int fan_write_cmd_disable(const char *cmd, int *rc)
4986{
4987 if (strlencmp(cmd, "disable") != 0)
4988 return 0;
4989
4990 if ((*rc = fan_set_disable()) == -ENXIO)
4991 printk(IBM_ERR "disable command accepted for unsupported "
4992 "access mode %d", fan_control_access_mode);
4993
4994 return 1;
4995}
4996
4997static int fan_write_cmd_speed(const char *cmd, int *rc)
4998{
4999 int speed;
5000
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02005001 /* TODO:
5002 * Support speed <low> <medium> <high> ? */
5003
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005004 if (sscanf(cmd, "speed %d", &speed) != 1)
5005 return 0;
5006
5007 if ((*rc = fan_set_speed(speed)) == -ENXIO)
5008 printk(IBM_ERR "speed command accepted for unsupported "
5009 "access mode %d", fan_control_access_mode);
5010
5011 return 1;
5012}
5013
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005014static int fan_write_cmd_watchdog(const char *cmd, int *rc)
5015{
5016 int interval;
5017
5018 if (sscanf(cmd, "watchdog %d", &interval) != 1)
5019 return 0;
5020
5021 if (interval < 0 || interval > 120)
5022 *rc = -EINVAL;
5023 else
5024 fan_watchdog_maxinterval = interval;
5025
5026 return 1;
5027}
5028
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005029static int fan_write(char *buf)
5030{
5031 char *cmd;
5032 int rc = 0;
5033
5034 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005035 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005036 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005037 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005038 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005039 fan_write_cmd_disable(cmd, &rc) ||
5040 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005041 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005042 fan_write_cmd_speed(cmd, &rc))
5043 )
5044 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005045 else if (!rc)
5046 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005047 }
5048
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005049 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005050}
5051
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005052static struct ibm_struct fan_driver_data = {
5053 .name = "fan",
5054 .read = fan_read,
5055 .write = fan_write,
5056 .exit = fan_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005057};
5058
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005059/****************************************************************************
5060 ****************************************************************************
5061 *
5062 * Infrastructure
5063 *
5064 ****************************************************************************
5065 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005067/* sysfs name ---------------------------------------------------------- */
5068static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
5069 struct device_attribute *attr,
5070 char *buf)
5071{
5072 return snprintf(buf, PAGE_SIZE, "%s\n", IBM_NAME);
5073}
5074
5075static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
5076 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
5077
5078/* --------------------------------------------------------------------- */
5079
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005080/* /proc support */
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03005081static struct proc_dir_entry *proc_dir;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005082
5083/* Subdriver registry */
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005084static LIST_HEAD(tpacpi_all_drivers);
5085
Linus Torvalds1da177e2005-04-16 15:20:36 -07005086
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005087/*
5088 * Module and infrastructure proble, init and exit handling
5089 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005090
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005091#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005092static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005093{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005094 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005095
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005096 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005097}
5098#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
5099
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005100static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101{
5102 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005103 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104 struct proc_dir_entry *entry;
5105
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005106 BUG_ON(ibm == NULL);
5107
5108 INIT_LIST_HEAD(&ibm->all_drivers);
5109
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03005110 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111 return 0;
5112
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005113 dbg_printk(TPACPI_DBG_INIT,
5114 "probing for %s\n", ibm->name);
5115
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005116 if (iibm->init) {
5117 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005118 if (ret > 0)
5119 return 0; /* probe failed */
5120 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005121 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005122
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03005123 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005124 }
5125
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005126 if (ibm->acpi) {
5127 if (ibm->acpi->hid) {
5128 ret = register_tpacpi_subdriver(ibm);
5129 if (ret)
5130 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005131 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005132
5133 if (ibm->acpi->notify) {
5134 ret = setup_acpi_notify(ibm);
5135 if (ret == -ENODEV) {
5136 printk(IBM_NOTICE "disabling subdriver %s\n",
5137 ibm->name);
5138 ret = 0;
5139 goto err_out;
5140 }
5141 if (ret < 0)
5142 goto err_out;
5143 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005144 }
5145
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005146 dbg_printk(TPACPI_DBG_INIT,
5147 "%s installed\n", ibm->name);
5148
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005149 if (ibm->read) {
5150 entry = create_proc_entry(ibm->name,
5151 S_IFREG | S_IRUGO | S_IWUSR,
5152 proc_dir);
5153 if (!entry) {
5154 printk(IBM_ERR "unable to create proc entry %s\n",
5155 ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005156 ret = -ENODEV;
5157 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005158 }
5159 entry->owner = THIS_MODULE;
5160 entry->data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005161 entry->read_proc = &dispatch_procfs_read;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005162 if (ibm->write)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005163 entry->write_proc = &dispatch_procfs_write;
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03005164 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005165 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005166
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005167 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
5168
Linus Torvalds1da177e2005-04-16 15:20:36 -07005169 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005170
5171err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005172 dbg_printk(TPACPI_DBG_INIT,
5173 "%s: at error exit path with result %d\n",
5174 ibm->name, ret);
5175
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005176 ibm_exit(ibm);
5177 return (ret < 0)? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005178}
5179
5180static void ibm_exit(struct ibm_struct *ibm)
5181{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005182 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005183
5184 list_del_init(&ibm->all_drivers);
5185
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005186 if (ibm->flags.acpi_notify_installed) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005187 dbg_printk(TPACPI_DBG_EXIT,
5188 "%s: acpi_remove_notify_handler\n", ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005189 BUG_ON(!ibm->acpi);
5190 acpi_remove_notify_handler(*ibm->acpi->handle,
5191 ibm->acpi->type,
5192 dispatch_acpi_notify);
5193 ibm->flags.acpi_notify_installed = 0;
5194 ibm->flags.acpi_notify_installed = 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005195 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005196
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03005197 if (ibm->flags.proc_created) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005198 dbg_printk(TPACPI_DBG_EXIT,
5199 "%s: remove_proc_entry\n", ibm->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005200 remove_proc_entry(ibm->name, proc_dir);
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03005201 ibm->flags.proc_created = 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005202 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005204 if (ibm->flags.acpi_driver_registered) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005205 dbg_printk(TPACPI_DBG_EXIT,
5206 "%s: acpi_bus_unregister_driver\n", ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005207 BUG_ON(!ibm->acpi);
5208 acpi_bus_unregister_driver(ibm->acpi->driver);
5209 kfree(ibm->acpi->driver);
5210 ibm->acpi->driver = NULL;
5211 ibm->flags.acpi_driver_registered = 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005212 }
5213
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03005214 if (ibm->flags.init_called && ibm->exit) {
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005215 ibm->exit();
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03005216 ibm->flags.init_called = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005217 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005218
5219 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005220}
5221
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005222/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005223
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005224static void __init get_thinkpad_model_data(struct thinkpad_id_data *tp)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005225{
Jeff Garzik18552562007-10-03 15:15:40 -04005226 const struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02005227 char ec_fw_string[18];
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005228
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005229 if (!tp)
5230 return;
5231
5232 memset(tp, 0, sizeof(*tp));
5233
5234 if (dmi_name_in_vendors("IBM"))
5235 tp->vendor = PCI_VENDOR_ID_IBM;
5236 else if (dmi_name_in_vendors("LENOVO"))
5237 tp->vendor = PCI_VENDOR_ID_LENOVO;
5238 else
5239 return;
5240
5241 tp->bios_version_str = kstrdup(dmi_get_system_info(DMI_BIOS_VERSION),
5242 GFP_KERNEL);
5243 if (!tp->bios_version_str)
5244 return;
5245 tp->bios_model = tp->bios_version_str[0]
5246 | (tp->bios_version_str[1] << 8);
5247
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005248 /*
5249 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
5250 * X32 or newer, all Z series; Some models must have an
5251 * up-to-date BIOS or they will not be detected.
5252 *
5253 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
5254 */
5255 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02005256 if (sscanf(dev->name,
5257 "IBM ThinkPad Embedded Controller -[%17c",
5258 ec_fw_string) == 1) {
5259 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
5260 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005261
5262 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
5263 tp->ec_model = ec_fw_string[0]
5264 | (ec_fw_string[1] << 8);
5265 break;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02005266 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005267 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005268
5269 tp->model_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_VERSION),
5270 GFP_KERNEL);
5271 if (strnicmp(tp->model_str, "ThinkPad", 8) != 0) {
5272 kfree(tp->model_str);
5273 tp->model_str = NULL;
5274 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005275}
5276
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005277static int __init probe_for_thinkpad(void)
5278{
5279 int is_thinkpad;
5280
5281 if (acpi_disabled)
5282 return -ENODEV;
5283
5284 /*
5285 * Non-ancient models have better DMI tagging, but very old models
5286 * don't.
5287 */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005288 is_thinkpad = (thinkpad_id.model_str != NULL);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005289
5290 /* ec is required because many other handles are relative to it */
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005291 IBM_ACPIHANDLE_INIT(ec);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005292 if (!ec_handle) {
5293 if (is_thinkpad)
5294 printk(IBM_ERR
5295 "Not yet supported ThinkPad detected!\n");
5296 return -ENODEV;
5297 }
5298
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03005299 /*
5300 * Risks a regression on very old machines, but reduces potential
5301 * false positives a damn great deal
5302 */
5303 if (!is_thinkpad)
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005304 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03005305
5306 if (!is_thinkpad && !force_load)
5307 return -ENODEV;
5308
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005309 return 0;
5310}
5311
5312
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005313/* Module init, exit, parameters */
5314
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005315static struct ibm_init_struct ibms_init[] __initdata = {
5316 {
5317 .init = thinkpad_acpi_driver_init,
5318 .data = &thinkpad_acpi_driver_data,
5319 },
5320 {
5321 .init = hotkey_init,
5322 .data = &hotkey_driver_data,
5323 },
5324 {
5325 .init = bluetooth_init,
5326 .data = &bluetooth_driver_data,
5327 },
5328 {
5329 .init = wan_init,
5330 .data = &wan_driver_data,
5331 },
5332 {
5333 .init = video_init,
5334 .data = &video_driver_data,
5335 },
5336 {
5337 .init = light_init,
5338 .data = &light_driver_data,
5339 },
5340#ifdef CONFIG_THINKPAD_ACPI_DOCK
5341 {
5342 .init = dock_init,
5343 .data = &dock_driver_data[0],
5344 },
5345 {
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03005346 .init = dock_init2,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005347 .data = &dock_driver_data[1],
5348 },
5349#endif
5350#ifdef CONFIG_THINKPAD_ACPI_BAY
5351 {
5352 .init = bay_init,
5353 .data = &bay_driver_data,
5354 },
5355#endif
5356 {
5357 .init = cmos_init,
5358 .data = &cmos_driver_data,
5359 },
5360 {
5361 .init = led_init,
5362 .data = &led_driver_data,
5363 },
5364 {
5365 .init = beep_init,
5366 .data = &beep_driver_data,
5367 },
5368 {
5369 .init = thermal_init,
5370 .data = &thermal_driver_data,
5371 },
5372 {
5373 .data = &ecdump_driver_data,
5374 },
5375 {
5376 .init = brightness_init,
5377 .data = &brightness_driver_data,
5378 },
5379 {
5380 .data = &volume_driver_data,
5381 },
5382 {
5383 .init = fan_init,
5384 .data = &fan_driver_data,
5385 },
5386};
5387
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005388static int __init set_ibm_param(const char *val, struct kernel_param *kp)
5389{
5390 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005391 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005392
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02005393 if (!kp || !kp->name || !val)
5394 return -EINVAL;
5395
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005396 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
5397 ibm = ibms_init[i].data;
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02005398 WARN_ON(ibm == NULL);
5399
5400 if (!ibm || !ibm->name)
5401 continue;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005402
5403 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
5404 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005405 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005406 strcpy(ibms_init[i].param, val);
5407 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005408 return 0;
5409 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005410 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005411
5412 return -EINVAL;
5413}
5414
5415static int experimental;
5416module_param(experimental, int, 0);
5417
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03005418static u32 dbg_level;
5419module_param_named(debug, dbg_level, uint, 0);
5420
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03005421static int force_load;
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03005422module_param(force_load, bool, 0);
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03005423
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03005424static int fan_control_allowed;
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03005425module_param_named(fan_control, fan_control_allowed, bool, 0);
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03005426
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03005427static int brightness_mode;
5428module_param_named(brightness_mode, brightness_mode, int, 0);
5429
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02005430static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5431module_param(brightness_enable, uint, 0);
5432
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03005433static unsigned int hotkey_report_mode;
5434module_param(hotkey_report_mode, uint, 0);
5435
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005436#define IBM_PARAM(feature) \
5437 module_param_call(feature, set_ibm_param, NULL, NULL, 0)
5438
5439IBM_PARAM(hotkey);
5440IBM_PARAM(bluetooth);
5441IBM_PARAM(video);
5442IBM_PARAM(light);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03005443#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005444IBM_PARAM(dock);
5445#endif
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03005446#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005447IBM_PARAM(bay);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03005448#endif /* CONFIG_THINKPAD_ACPI_BAY */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005449IBM_PARAM(cmos);
5450IBM_PARAM(led);
5451IBM_PARAM(beep);
5452IBM_PARAM(ecdump);
5453IBM_PARAM(brightness);
5454IBM_PARAM(volume);
5455IBM_PARAM(fan);
5456
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03005457static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005458{
5459 int ret, i;
5460
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03005461 tpacpi_lifecycle = TPACPI_LIFE_INIT;
5462
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03005463 /* Parameter checking */
5464 if (hotkey_report_mode > 2)
5465 return -EINVAL;
5466
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03005467 /* Driver-level probe */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005468
5469 get_thinkpad_model_data(&thinkpad_id);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005470 ret = probe_for_thinkpad();
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005471 if (ret) {
5472 thinkpad_acpi_module_exit();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005473 return ret;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005474 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005475
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03005476 /* Driver initialization */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005477
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005478 IBM_ACPIHANDLE_INIT(ecrd);
5479 IBM_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005480
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005481 proc_dir = proc_mkdir(IBM_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005482 if (!proc_dir) {
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005483 printk(IBM_ERR "unable to create proc dir " IBM_PROC_DIR);
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03005484 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005485 return -ENODEV;
5486 }
5487 proc_dir->owner = THIS_MODULE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005488
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03005489 ret = platform_driver_register(&tpacpi_pdriver);
5490 if (ret) {
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005491 printk(IBM_ERR "unable to register main platform driver\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03005492 thinkpad_acpi_module_exit();
5493 return ret;
5494 }
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03005495 tp_features.platform_drv_registered = 1;
5496
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005497 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
5498 if (ret) {
5499 printk(IBM_ERR "unable to register hwmon platform driver\n");
5500 thinkpad_acpi_module_exit();
5501 return ret;
5502 }
5503 tp_features.sensors_pdrv_registered = 1;
5504
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03005505 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03005506 if (!ret) {
5507 tp_features.platform_drv_attrs_registered = 1;
5508 ret = tpacpi_create_driver_attributes(&tpacpi_hwmon_pdriver.driver);
5509 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03005510 if (ret) {
5511 printk(IBM_ERR "unable to create sysfs driver attributes\n");
5512 thinkpad_acpi_module_exit();
5513 return ret;
5514 }
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03005515 tp_features.sensors_pdrv_attrs_registered = 1;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03005516
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03005517
5518 /* Device initialization */
5519 tpacpi_pdev = platform_device_register_simple(IBM_DRVR_NAME, -1,
5520 NULL, 0);
5521 if (IS_ERR(tpacpi_pdev)) {
5522 ret = PTR_ERR(tpacpi_pdev);
5523 tpacpi_pdev = NULL;
5524 printk(IBM_ERR "unable to register platform device\n");
5525 thinkpad_acpi_module_exit();
5526 return ret;
5527 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005528 tpacpi_sensors_pdev = platform_device_register_simple(
5529 IBM_HWMON_DRVR_NAME,
5530 -1, NULL, 0);
5531 if (IS_ERR(tpacpi_sensors_pdev)) {
5532 ret = PTR_ERR(tpacpi_sensors_pdev);
5533 tpacpi_sensors_pdev = NULL;
5534 printk(IBM_ERR "unable to register hwmon platform device\n");
5535 thinkpad_acpi_module_exit();
5536 return ret;
5537 }
5538 ret = device_create_file(&tpacpi_sensors_pdev->dev,
5539 &dev_attr_thinkpad_acpi_pdev_name);
5540 if (ret) {
5541 printk(IBM_ERR
5542 "unable to create sysfs hwmon device attributes\n");
5543 thinkpad_acpi_module_exit();
5544 return ret;
5545 }
5546 tp_features.sensors_pdev_attrs_registered = 1;
5547 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03005548 if (IS_ERR(tpacpi_hwmon)) {
5549 ret = PTR_ERR(tpacpi_hwmon);
5550 tpacpi_hwmon = NULL;
5551 printk(IBM_ERR "unable to register hwmon device\n");
5552 thinkpad_acpi_module_exit();
5553 return ret;
5554 }
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03005555 mutex_init(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03005556 tpacpi_inputdev = input_allocate_device();
5557 if (!tpacpi_inputdev) {
5558 printk(IBM_ERR "unable to allocate input device\n");
5559 thinkpad_acpi_module_exit();
5560 return -ENOMEM;
5561 } else {
5562 /* Prepare input device, but don't register */
5563 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
5564 tpacpi_inputdev->phys = IBM_DRVR_NAME "/input0";
5565 tpacpi_inputdev->id.bustype = BUS_HOST;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03005566 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
5567 thinkpad_id.vendor :
5568 PCI_VENDOR_ID_IBM;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03005569 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
5570 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
5571 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005572 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
5573 ret = ibm_init(&ibms_init[i]);
5574 if (ret >= 0 && *ibms_init[i].param)
5575 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005576 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03005577 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005578 return ret;
5579 }
5580 }
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03005581 ret = input_register_device(tpacpi_inputdev);
5582 if (ret < 0) {
5583 printk(IBM_ERR "unable to register input device\n");
5584 thinkpad_acpi_module_exit();
5585 return ret;
5586 } else {
5587 tp_features.input_device_registered = 1;
5588 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005589
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03005590 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005591 return 0;
5592}
5593
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03005594static void thinkpad_acpi_module_exit(void)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005595{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005596 struct ibm_struct *ibm, *itmp;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005597
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03005598 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
5599
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005600 list_for_each_entry_safe_reverse(ibm, itmp,
5601 &tpacpi_all_drivers,
5602 all_drivers) {
5603 ibm_exit(ibm);
5604 }
5605
5606 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005607
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03005608 if (tpacpi_inputdev) {
5609 if (tp_features.input_device_registered)
5610 input_unregister_device(tpacpi_inputdev);
5611 else
5612 input_free_device(tpacpi_inputdev);
5613 }
5614
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03005615 if (tpacpi_hwmon)
5616 hwmon_device_unregister(tpacpi_hwmon);
5617
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005618 if (tp_features.sensors_pdev_attrs_registered)
5619 device_remove_file(&tpacpi_sensors_pdev->dev,
5620 &dev_attr_thinkpad_acpi_pdev_name);
5621 if (tpacpi_sensors_pdev)
5622 platform_device_unregister(tpacpi_sensors_pdev);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03005623 if (tpacpi_pdev)
5624 platform_device_unregister(tpacpi_pdev);
5625
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03005626 if (tp_features.sensors_pdrv_attrs_registered)
5627 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03005628 if (tp_features.platform_drv_attrs_registered)
5629 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
5630
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005631 if (tp_features.sensors_pdrv_registered)
5632 platform_driver_unregister(&tpacpi_hwmon_pdriver);
5633
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03005634 if (tp_features.platform_drv_registered)
5635 platform_driver_unregister(&tpacpi_pdriver);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03005636
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005637 if (proc_dir)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005638 remove_proc_entry(IBM_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005639
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005640 kfree(thinkpad_id.bios_version_str);
5641 kfree(thinkpad_id.ec_version_str);
5642 kfree(thinkpad_id.model_str);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005643}
5644
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03005645module_init(thinkpad_acpi_module_init);
5646module_exit(thinkpad_acpi_module_exit);