blob: e953276664a0f77aef6a0a997452488d75a294c3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -03002 * thinkpad_acpi.c - ThinkPad ACPI Extras
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 *
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
Henrique de Moraes Holschuha62bc912007-03-23 17:33:58 -03006 * Copyright (C) 2006-2007 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
Henrique de Moraes Holschuha62bc912007-03-23 17:33:58 -030020 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
Borislav Deianov78f81cc2005-08-17 00:00:00 -040022 */
23
Henrique de Moraes Holschuh3b0c6482007-09-04 11:13:16 -030024#define IBM_VERSION "0.16"
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -030025#define TPACPI_SYSFS_VERSION 0x020000
Borislav Deianov78f81cc2005-08-17 00:00:00 -040026
27/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 * Changelog:
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -030029 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
30 * drivers/misc.
Henrique de Moraes Holschuhf9ff43a2006-11-25 16:37:38 -020031 *
32 * 2006-11-22 0.13 new maintainer
33 * changelog now lives in git commit history, and will
34 * not be updated further in-file.
Henrique de Moraes Holschuh837ca6d2007-03-23 17:33:54 -030035 *
Borislav Deianov78f81cc2005-08-17 00:00:00 -040036 * 2005-08-17 0.12 fix compilation on 2.6.13-rc kernels
37 * 2005-03-17 0.11 support for 600e, 770x
38 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
39 * support for 770e, G41
40 * G40 and G41 don't have a thinklight
41 * temperatures no longer experimental
42 * experimental brightness control
43 * experimental volume control
44 * experimental fan enable/disable
Henrique de Moraes Holschuh837ca6d2007-03-23 17:33:54 -030045 * 2005-01-16 0.10 fix module loading on R30, R31
Borislav Deianov78f81cc2005-08-17 00:00:00 -040046 * 2005-01-16 0.9 support for 570, R30, R31
47 * ultrabay support on A22p, A3x
48 * limit arg for cmos, led, beep, drop experimental status
49 * more capable led control on A21e, A22p, T20-22, X20
50 * experimental temperatures and fan speed
51 * experimental embedded controller register dump
52 * mark more functions as __init, drop incorrect __exit
53 * use MODULE_VERSION
54 * thanks to Henrik Brix Andersen <brix@gentoo.org>
55 * fix parameter passing on module loading
56 * thanks to Rusty Russell <rusty@rustcorp.com.au>
57 * thanks to Jim Radford <radford@blackbean.org>
58 * 2004-11-08 0.8 fix init error case, don't return from a macro
59 * thanks to Chris Wright <chrisw@osdl.org>
60 * 2004-10-23 0.7 fix module loading on A21e, A22p, T20, T21, X20
61 * fix led control on A21e
62 * 2004-10-19 0.6 use acpi_bus_register_driver() to claim HKEY device
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 * 2004-10-18 0.5 thinklight support on A21e, G40, R32, T20, T21, X20
64 * proc file format changed
65 * video_switch command
66 * experimental cmos control
67 * experimental led control
68 * experimental acpi sounds
Borislav Deianov78f81cc2005-08-17 00:00:00 -040069 * 2004-09-16 0.4 support for module parameters
70 * hotkey mask can be prefixed by 0x
71 * video output switching
72 * video expansion control
73 * ultrabay eject support
74 * removed lcd brightness/on/off control, didn't work
75 * 2004-08-17 0.3 support for R40
76 * lcd off, brightness control
77 * thinklight on/off
78 * 2004-08-14 0.2 support for T series, X20
79 * bluetooth enable/disable
80 * hotkey events disabled by default
81 * removed fan control, currently useless
82 * 2004-08-09 0.1 initial release, support for X series
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 */
84
Henrique de Moraes Holschuhf21f85d2007-03-29 01:58:40 -030085#include "thinkpad_acpi.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Henrique de Moraes Holschuhf9ff43a2006-11-25 16:37:38 -020087MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
Borislav Deianov78f81cc2005-08-17 00:00:00 -040088MODULE_DESCRIPTION(IBM_DESC);
89MODULE_VERSION(IBM_VERSION);
90MODULE_LICENSE("GPL");
91
Henrique de Moraes Holschuhd903ac52007-03-29 01:58:42 -030092/* Please remove this in year 2009 */
93MODULE_ALIAS("ibm_acpi");
94
Henrique de Moraes Holschuhb964b432007-07-18 23:45:26 -030095/*
96 * DMI matching for module autoloading
97 *
98 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
99 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
100 *
101 * Only models listed in thinkwiki will be supported, so add yours
102 * if it is not there yet.
103 */
104#define IBM_BIOS_MODULE_ALIAS(__type) \
105 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW")
106
107/* Non-ancient thinkpads */
108MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
109MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
110
111/* Ancient thinkpad BIOSes have to be identified by
112 * BIOS type or model number, and there are far less
113 * BIOS types than model numbers... */
114IBM_BIOS_MODULE_ALIAS("I[B,D,H,I,M,N,O,T,W,V,Y,Z]");
115IBM_BIOS_MODULE_ALIAS("1[0,3,6,8,A-G,I,K,M-P,S,T]");
116IBM_BIOS_MODULE_ALIAS("K[U,X-Z]");
117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118#define __unused __attribute__ ((unused))
119
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300120static enum {
121 TPACPI_LIFE_INIT = 0,
122 TPACPI_LIFE_RUNNING,
123 TPACPI_LIFE_EXITING,
124} tpacpi_lifecycle;
125
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300126/****************************************************************************
127 ****************************************************************************
128 *
129 * ACPI Helpers and device model
130 *
131 ****************************************************************************
132 ****************************************************************************/
133
134/*************************************************************************
135 * ACPI basic handles
136 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -0300138static acpi_handle root_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140#define IBM_HANDLE(object, parent, paths...) \
141 static acpi_handle object##_handle; \
142 static acpi_handle *object##_parent = &parent##_handle; \
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400143 static char *object##_path; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 static char *object##_paths[] = { paths }
145
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400146IBM_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
147 "\\_SB.PCI.ISA.EC", /* 570 */
148 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
149 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
150 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
151 "\\_SB.PCI0.ICH3.EC0", /* R31 */
152 "\\_SB.PCI0.LPC.EC", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300153 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300155IBM_HANDLE(ecrd, ec, "ECRD"); /* 570 */
156IBM_HANDLE(ecwr, ec, "ECWR"); /* 570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300158
159/*************************************************************************
160 * Misc ACPI handles
161 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400163IBM_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, T4x, X31, X40 */
164 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
165 "\\CMS", /* R40, R40e */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300166 ); /* all others */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400167
168IBM_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
169 "^HKEY", /* R30, R31 */
170 "HKEY", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300171 ); /* 570 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400172
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400173
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300174/*************************************************************************
175 * ACPI helpers
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200176 */
177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178static int acpi_evalf(acpi_handle handle,
179 void *res, char *method, char *fmt, ...)
180{
181 char *fmt0 = fmt;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400182 struct acpi_object_list params;
183 union acpi_object in_objs[IBM_MAX_ACPI_ARGS];
184 struct acpi_buffer result, *resultp;
185 union acpi_object out_obj;
186 acpi_status status;
187 va_list ap;
188 char res_type;
189 int success;
190 int quiet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192 if (!*fmt) {
193 printk(IBM_ERR "acpi_evalf() called with empty format\n");
194 return 0;
195 }
196
197 if (*fmt == 'q') {
198 quiet = 1;
199 fmt++;
200 } else
201 quiet = 0;
202
203 res_type = *(fmt++);
204
205 params.count = 0;
206 params.pointer = &in_objs[0];
207
208 va_start(ap, fmt);
209 while (*fmt) {
210 char c = *(fmt++);
211 switch (c) {
212 case 'd': /* int */
213 in_objs[params.count].integer.value = va_arg(ap, int);
214 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
215 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400216 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 default:
218 printk(IBM_ERR "acpi_evalf() called "
219 "with invalid format character '%c'\n", c);
220 return 0;
221 }
222 }
223 va_end(ap);
224
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400225 if (res_type != 'v') {
226 result.length = sizeof(out_obj);
227 result.pointer = &out_obj;
228 resultp = &result;
229 } else
230 resultp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400232 status = acpi_evaluate_object(handle, method, &params, resultp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234 switch (res_type) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400235 case 'd': /* int */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 if (res)
237 *(int *)res = out_obj.integer.value;
238 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
239 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400240 case 'v': /* void */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 success = status == AE_OK;
242 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400243 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 default:
245 printk(IBM_ERR "acpi_evalf() called "
246 "with invalid format character '%c'\n", res_type);
247 return 0;
248 }
249
250 if (!success && !quiet)
251 printk(IBM_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
252 method, fmt0, status);
253
254 return success;
255}
256
257static void __unused acpi_print_int(acpi_handle handle, char *method)
258{
259 int i;
260
261 if (acpi_evalf(handle, &i, method, "d"))
262 printk(IBM_INFO "%s = 0x%x\n", method, i);
263 else
264 printk(IBM_ERR "error calling %s\n", method);
265}
266
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300267static int acpi_ec_read(int i, u8 * p)
268{
269 int v;
270
271 if (ecrd_handle) {
272 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
273 return 0;
274 *p = v;
275 } else {
276 if (ec_read(i, p) < 0)
277 return 0;
278 }
279
280 return 1;
281}
282
283static int acpi_ec_write(int i, u8 v)
284{
285 if (ecwr_handle) {
286 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
287 return 0;
288 } else {
289 if (ec_write(i, v) < 0)
290 return 0;
291 }
292
293 return 1;
294}
295
296static int _sta(acpi_handle handle)
297{
298 int status;
299
300 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
301 status = 0;
302
303 return status;
304}
305
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -0300306static int issue_thinkpad_cmos_command(int cmos_cmd)
307{
308 if (!cmos_handle)
309 return -ENXIO;
310
311 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
312 return -EIO;
313
314 return 0;
315}
316
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300317/*************************************************************************
318 * ACPI device model
319 */
320
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300321static void drv_acpi_handle_init(char *name,
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300322 acpi_handle *handle, acpi_handle parent,
323 char **paths, int num_paths, char **path)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300324{
325 int i;
326 acpi_status status;
327
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300328 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
329 name);
330
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300331 for (i = 0; i < num_paths; i++) {
332 status = acpi_get_handle(parent, paths[i], handle);
333 if (ACPI_SUCCESS(status)) {
334 *path = paths[i];
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300335 dbg_printk(TPACPI_DBG_INIT,
336 "Found ACPI handle %s for %s\n",
337 *path, name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300338 return;
339 }
340 }
341
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300342 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
343 name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300344 *handle = NULL;
345}
346
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300347static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300348{
349 struct ibm_struct *ibm = data;
350
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300351 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
352 return;
353
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300354 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300355 return;
356
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300357 ibm->acpi->notify(ibm, event);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300358}
359
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300360static int __init setup_acpi_notify(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300361{
362 acpi_status status;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300363 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300364
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300365 BUG_ON(!ibm->acpi);
366
367 if (!*ibm->acpi->handle)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300368 return 0;
369
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300370 vdbg_printk(TPACPI_DBG_INIT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300371 "setting up ACPI notify for %s\n", ibm->name);
372
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300373 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
374 if (rc < 0) {
375 printk(IBM_ERR "acpi_bus_get_device(%s) failed: %d\n",
376 ibm->name, rc);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300377 return -ENODEV;
378 }
379
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300380 acpi_driver_data(ibm->acpi->device) = ibm;
381 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300382 IBM_ACPI_EVENT_PREFIX,
383 ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300384
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300385 status = acpi_install_notify_handler(*ibm->acpi->handle,
386 ibm->acpi->type, dispatch_acpi_notify, ibm);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300387 if (ACPI_FAILURE(status)) {
388 if (status == AE_ALREADY_EXISTS) {
389 printk(IBM_NOTICE "another device driver is already handling %s events\n",
390 ibm->name);
391 } else {
392 printk(IBM_ERR "acpi_install_notify_handler(%s) failed: %d\n",
393 ibm->name, status);
394 }
395 return -ENODEV;
396 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300397 ibm->flags.acpi_notify_installed = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300398 return 0;
399}
400
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300401static int __init tpacpi_device_add(struct acpi_device *device)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300402{
403 return 0;
404}
405
Henrique de Moraes Holschuh67001212007-04-21 11:08:25 -0300406static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300407{
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300408 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300409
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300410 dbg_printk(TPACPI_DBG_INIT,
411 "registering %s as an ACPI driver\n", ibm->name);
412
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300413 BUG_ON(!ibm->acpi);
414
415 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
416 if (!ibm->acpi->driver) {
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300417 printk(IBM_ERR "kzalloc(ibm->driver) failed\n");
418 return -ENOMEM;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300419 }
420
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300421 sprintf(ibm->acpi->driver->name, "%s_%s", IBM_NAME, ibm->name);
422 ibm->acpi->driver->ids = ibm->acpi->hid;
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200423
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300424 ibm->acpi->driver->ops.add = &tpacpi_device_add;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300425
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300426 rc = acpi_bus_register_driver(ibm->acpi->driver);
427 if (rc < 0) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300428 printk(IBM_ERR "acpi_bus_register_driver(%s) failed: %d\n",
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200429 ibm->name, rc);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300430 kfree(ibm->acpi->driver);
431 ibm->acpi->driver = NULL;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300432 } else if (!rc)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300433 ibm->flags.acpi_driver_registered = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300434
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300435 return rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300436}
437
438
439/****************************************************************************
440 ****************************************************************************
441 *
442 * Procfs Helpers
443 *
444 ****************************************************************************
445 ****************************************************************************/
446
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300447static int dispatch_procfs_read(char *page, char **start, off_t off,
448 int count, int *eof, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300449{
450 struct ibm_struct *ibm = data;
451 int len;
452
453 if (!ibm || !ibm->read)
454 return -EINVAL;
455
456 len = ibm->read(page);
457 if (len < 0)
458 return len;
459
460 if (len <= off + count)
461 *eof = 1;
462 *start = page + off;
463 len -= off;
464 if (len > count)
465 len = count;
466 if (len < 0)
467 len = 0;
468
469 return len;
470}
471
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300472static int dispatch_procfs_write(struct file *file,
473 const char __user * userbuf,
474 unsigned long count, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300475{
476 struct ibm_struct *ibm = data;
477 char *kernbuf;
478 int ret;
479
480 if (!ibm || !ibm->write)
481 return -EINVAL;
482
483 kernbuf = kmalloc(count + 2, GFP_KERNEL);
484 if (!kernbuf)
485 return -ENOMEM;
486
487 if (copy_from_user(kernbuf, userbuf, count)) {
488 kfree(kernbuf);
489 return -EFAULT;
490 }
491
492 kernbuf[count] = 0;
493 strcat(kernbuf, ",");
494 ret = ibm->write(kernbuf);
495 if (ret == 0)
496 ret = count;
497
498 kfree(kernbuf);
499
500 return ret;
501}
502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503static char *next_cmd(char **cmds)
504{
505 char *start = *cmds;
506 char *end;
507
508 while ((end = strchr(start, ',')) && end == start)
509 start = end + 1;
510
511 if (!end)
512 return NULL;
513
514 *end = 0;
515 *cmds = end + 1;
516 return start;
517}
518
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300519
520/****************************************************************************
521 ****************************************************************************
522 *
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300523 * Device model: input, hwmon and platform
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300524 *
525 ****************************************************************************
526 ****************************************************************************/
527
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -0300528static struct platform_device *tpacpi_pdev;
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
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400776static int hotkey_orig_status;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -0300777static u32 hotkey_orig_mask;
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -0300778static u32 hotkey_all_mask;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -0300779static u32 hotkey_reserved_mask;
780
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -0300781static u16 *hotkey_keycode_map;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400782
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -0300783static struct attribute_set *hotkey_dev_attributes;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300784
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -0300785static int hotkey_get_wlsw(int *status)
786{
787 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
788 return -EIO;
789 return 0;
790}
791
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300792/* sysfs hotkey enable ------------------------------------------------- */
793static ssize_t hotkey_enable_show(struct device *dev,
794 struct device_attribute *attr,
795 char *buf)
796{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -0300797 int res, status;
798 u32 mask;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300799
800 res = hotkey_get(&status, &mask);
801 if (res)
802 return res;
803
804 return snprintf(buf, PAGE_SIZE, "%d\n", status);
805}
806
807static ssize_t hotkey_enable_store(struct device *dev,
808 struct device_attribute *attr,
809 const char *buf, size_t count)
810{
811 unsigned long t;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -0300812 int res, status;
813 u32 mask;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300814
815 if (parse_strtoul(buf, 1, &t))
816 return -EINVAL;
817
818 res = hotkey_get(&status, &mask);
819 if (!res)
820 res = hotkey_set(t, mask);
821
822 return (res) ? res : count;
823}
824
825static struct device_attribute dev_attr_hotkey_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -0300826 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300827 hotkey_enable_show, hotkey_enable_store);
828
829/* sysfs hotkey mask --------------------------------------------------- */
830static ssize_t hotkey_mask_show(struct device *dev,
831 struct device_attribute *attr,
832 char *buf)
833{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -0300834 int res, status;
835 u32 mask;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300836
837 res = hotkey_get(&status, &mask);
838 if (res)
839 return res;
840
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -0300841 return snprintf(buf, PAGE_SIZE, "0x%08x\n", mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300842}
843
844static ssize_t hotkey_mask_store(struct device *dev,
845 struct device_attribute *attr,
846 const char *buf, size_t count)
847{
848 unsigned long t;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -0300849 int res, status;
850 u32 mask;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300851
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -0300852 if (parse_strtoul(buf, 0xffffffffUL, &t))
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300853 return -EINVAL;
854
855 res = hotkey_get(&status, &mask);
856 if (!res)
857 hotkey_set(status, t);
858
859 return (res) ? res : count;
860}
861
862static struct device_attribute dev_attr_hotkey_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -0300863 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300864 hotkey_mask_show, hotkey_mask_store);
865
866/* sysfs hotkey bios_enabled ------------------------------------------- */
867static ssize_t hotkey_bios_enabled_show(struct device *dev,
868 struct device_attribute *attr,
869 char *buf)
870{
871 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
872}
873
874static struct device_attribute dev_attr_hotkey_bios_enabled =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -0300875 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300876
877/* sysfs hotkey bios_mask ---------------------------------------------- */
878static ssize_t hotkey_bios_mask_show(struct device *dev,
879 struct device_attribute *attr,
880 char *buf)
881{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -0300882 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300883}
884
885static struct device_attribute dev_attr_hotkey_bios_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -0300886 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300887
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -0300888/* sysfs hotkey all_mask ----------------------------------------------- */
889static ssize_t hotkey_all_mask_show(struct device *dev,
890 struct device_attribute *attr,
891 char *buf)
892{
893 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_all_mask);
894}
895
896static struct device_attribute dev_attr_hotkey_all_mask =
897 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
898
899/* sysfs hotkey recommended_mask --------------------------------------- */
900static ssize_t hotkey_recommended_mask_show(struct device *dev,
901 struct device_attribute *attr,
902 char *buf)
903{
904 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
905 hotkey_all_mask & ~hotkey_reserved_mask);
906}
907
908static struct device_attribute dev_attr_hotkey_recommended_mask =
909 __ATTR(hotkey_recommended_mask, S_IRUGO,
910 hotkey_recommended_mask_show, NULL);
911
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -0300912/* sysfs hotkey radio_sw ----------------------------------------------- */
913static ssize_t hotkey_radio_sw_show(struct device *dev,
914 struct device_attribute *attr,
915 char *buf)
916{
917 int res, s;
918 res = hotkey_get_wlsw(&s);
919 if (res < 0)
920 return res;
921
922 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
923}
924
925static struct device_attribute dev_attr_hotkey_radio_sw =
926 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
927
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -0300928/* sysfs hotkey report_mode -------------------------------------------- */
929static ssize_t hotkey_report_mode_show(struct device *dev,
930 struct device_attribute *attr,
931 char *buf)
932{
933 return snprintf(buf, PAGE_SIZE, "%d\n",
934 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
935}
936
937static struct device_attribute dev_attr_hotkey_report_mode =
938 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
939
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300940/* --------------------------------------------------------------------- */
941
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -0300942static struct attribute *hotkey_attributes[] __initdata = {
943 &dev_attr_hotkey_enable.attr,
944 &dev_attr_hotkey_report_mode.attr,
945};
946
947static struct attribute *hotkey_mask_attributes[] __initdata = {
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300948 &dev_attr_hotkey_mask.attr,
949 &dev_attr_hotkey_bios_enabled.attr,
950 &dev_attr_hotkey_bios_mask.attr,
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -0300951 &dev_attr_hotkey_all_mask.attr,
952 &dev_attr_hotkey_recommended_mask.attr,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300953};
954
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300955static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300956{
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -0300957
958 static u16 ibm_keycode_map[] __initdata = {
959 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
960 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
961 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
962 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
963 /* Scan codes 0x0C to 0x0F: Other ACPI HKEY hot keys */
964 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
965 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
966 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Jeremy Katzfba956c2007-10-15 16:45:10 -0400967 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -0300968 /* Scan codes 0x10 to 0x1F: Extended ACPI HKEY hot keys */
Jeremy Katzfba956c2007-10-15 16:45:10 -0400969 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -0300970 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
971 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
972 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Jeremy Katzfba956c2007-10-15 16:45:10 -0400973 KEY_VOLUMEUP, /* 0x14: VOLUME UP */
974 KEY_VOLUMEDOWN, /* 0x15: VOLUME DOWN */
975 KEY_MUTE, /* 0x16: MUTE */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -0300976 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
977 /* (assignments unknown, please report if found) */
978 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
979 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
980 };
981 static u16 lenovo_keycode_map[] __initdata = {
982 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
983 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
984 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
985 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
986 /* Scan codes 0x0C to 0x0F: Other ACPI HKEY hot keys */
987 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
988 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
989 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
990 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
991 /* Scan codes 0x10 to 0x1F: Extended ACPI HKEY hot keys */
992 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
993 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
994 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
995 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Jeremy Katzfba956c2007-10-15 16:45:10 -0400996 KEY_VOLUMEUP, /* 0x14: VOLUME UP */
997 KEY_VOLUMEDOWN, /* 0x15: VOLUME DOWN */
998 KEY_MUTE, /* 0x16: MUTE */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -0300999 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
1000 /* (assignments unknown, please report if found) */
1001 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1002 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1003 };
1004
1005#define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
1006#define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
1007#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
1008
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001009 int res, i;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001010 int status;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001011 int hkeyv;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001012
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001013 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
1014
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001015 BUG_ON(!tpacpi_inputdev);
1016
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001017 IBM_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001018 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001019
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001020 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001021 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001022
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001023 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001024 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001025
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001026 if (tp_features.hotkey) {
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001027 hotkey_dev_attributes = create_attr_set(8, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001028 if (!hotkey_dev_attributes)
1029 return -ENOMEM;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001030 res = add_many_to_attr_set(hotkey_dev_attributes,
1031 hotkey_attributes,
1032 ARRAY_SIZE(hotkey_attributes));
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001033 if (res)
1034 return res;
1035
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001036 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001037 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
1038 for HKEY interface version 0x100 */
1039 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
1040 if ((hkeyv >> 8) != 1) {
1041 printk(IBM_ERR "unknown version of the "
1042 "HKEY interface: 0x%x\n", hkeyv);
1043 printk(IBM_ERR "please report this to %s\n",
1044 IBM_MAIL);
1045 } else {
1046 /*
1047 * MHKV 0x100 in A31, R40, R40e,
1048 * T4x, X31, and later
1049 * */
1050 tp_features.hotkey_mask = 1;
1051 }
1052 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001053
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001054 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001055 str_supported(tp_features.hotkey_mask));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001056
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001057 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001058 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001059 "MHKA", "qd")) {
1060 printk(IBM_ERR
1061 "missing MHKA handler, "
1062 "please report this to %s\n",
1063 IBM_MAIL);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001064 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001065 }
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001066 }
1067
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001068 res = hotkey_get(&hotkey_orig_status, &hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001069 if (!res && tp_features.hotkey_mask) {
1070 res = add_many_to_attr_set(hotkey_dev_attributes,
1071 hotkey_mask_attributes,
1072 ARRAY_SIZE(hotkey_mask_attributes));
1073 }
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001074
1075 /* Not all thinkpads have a hardware radio switch */
1076 if (!res && acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
1077 tp_features.hotkey_wlsw = 1;
1078 printk(IBM_INFO
1079 "radio switch found; radios are %s\n",
1080 enabled(status, 0));
1081 res = add_to_attr_set(hotkey_dev_attributes,
1082 &dev_attr_hotkey_radio_sw.attr);
1083 }
1084
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001085 if (!res)
1086 res = register_attr_set_with_sysfs(
1087 hotkey_dev_attributes,
1088 &tpacpi_pdev->dev.kobj);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001089 if (res)
1090 return res;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001091
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001092 /* Set up key map */
1093
1094 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
1095 GFP_KERNEL);
1096 if (!hotkey_keycode_map) {
1097 printk(IBM_ERR "failed to allocate memory for key map\n");
1098 return -ENOMEM;
1099 }
1100
1101 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
1102 dbg_printk(TPACPI_DBG_INIT,
1103 "using Lenovo default hot key map\n");
1104 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
1105 TPACPI_HOTKEY_MAP_SIZE);
1106 } else {
1107 dbg_printk(TPACPI_DBG_INIT,
1108 "using IBM default hot key map\n");
1109 memcpy(hotkey_keycode_map, &ibm_keycode_map,
1110 TPACPI_HOTKEY_MAP_SIZE);
1111 }
1112
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001113 set_bit(EV_KEY, tpacpi_inputdev->evbit);
1114 set_bit(EV_MSC, tpacpi_inputdev->evbit);
1115 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001116 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
1117 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
1118 tpacpi_inputdev->keycode = hotkey_keycode_map;
1119 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001120 if (hotkey_keycode_map[i] != KEY_RESERVED) {
1121 set_bit(hotkey_keycode_map[i],
1122 tpacpi_inputdev->keybit);
1123 } else {
1124 if (i < sizeof(hotkey_reserved_mask)*8)
1125 hotkey_reserved_mask |= 1 << i;
1126 }
1127 }
1128
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03001129 if (tp_features.hotkey_wlsw) {
1130 set_bit(EV_SW, tpacpi_inputdev->evbit);
1131 set_bit(SW_RADIO, tpacpi_inputdev->swbit);
1132 }
1133
Henrique de Moraes Holschuh1a343762007-07-18 23:45:36 -03001134 dbg_printk(TPACPI_DBG_INIT,
1135 "enabling hot key handling\n");
1136 res = hotkey_set(1, (hotkey_all_mask & ~hotkey_reserved_mask)
1137 | hotkey_orig_mask);
1138 if (res)
1139 return res;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001140
1141 dbg_printk(TPACPI_DBG_INIT,
1142 "legacy hot key reporting over procfs %s\n",
1143 (hotkey_report_mode < 2) ?
1144 "enabled" : "disabled");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001145 }
1146
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001147 return (tp_features.hotkey)? 0 : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001148}
1149
1150static void hotkey_exit(void)
1151{
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001152 int res;
1153
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001154 if (tp_features.hotkey) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001155 dbg_printk(TPACPI_DBG_EXIT, "restoring original hotkey mask\n");
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001156 res = hotkey_set(hotkey_orig_status, hotkey_orig_mask);
1157 if (res)
1158 printk(IBM_ERR "failed to restore hotkey to BIOS defaults\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001159 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001160
1161 if (hotkey_dev_attributes) {
1162 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
1163 hotkey_dev_attributes = NULL;
1164 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001165}
1166
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001167static void tpacpi_input_send_key(unsigned int scancode,
1168 unsigned int keycode)
1169{
1170 if (keycode != KEY_RESERVED) {
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03001171 mutex_lock(&tpacpi_inputdev_send_mutex);
1172
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001173 input_report_key(tpacpi_inputdev, keycode, 1);
1174 if (keycode == KEY_UNKNOWN)
1175 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1176 scancode);
1177 input_sync(tpacpi_inputdev);
1178
1179 input_report_key(tpacpi_inputdev, keycode, 0);
1180 if (keycode == KEY_UNKNOWN)
1181 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1182 scancode);
1183 input_sync(tpacpi_inputdev);
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03001184
1185 mutex_unlock(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001186 }
1187}
1188
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03001189static void tpacpi_input_send_radiosw(void)
1190{
1191 int wlsw;
1192
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03001193 mutex_lock(&tpacpi_inputdev_send_mutex);
1194
Henrique de Moraes Holschuhf4e1e432007-09-23 11:39:00 -03001195 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03001196 input_report_switch(tpacpi_inputdev,
1197 SW_RADIO, !!wlsw);
Henrique de Moraes Holschuhf4e1e432007-09-23 11:39:00 -03001198 input_sync(tpacpi_inputdev);
1199 }
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03001200
1201 mutex_unlock(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03001202}
1203
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001204static void hotkey_notify(struct ibm_struct *ibm, u32 event)
1205{
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001206 u32 hkey;
1207 unsigned int keycode, scancode;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03001208 int send_acpi_ev;
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03001209 int ignore_acpi_ev;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001210
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03001211 if (event != 0x80) {
1212 printk(IBM_ERR "unknown HKEY notification event %d\n", event);
1213 /* forward it to userspace, maybe it knows how to handle it */
1214 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
1215 ibm->acpi->device->dev.bus_id,
1216 event, 0);
1217 return;
1218 }
1219
1220 while (1) {
1221 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
1222 printk(IBM_ERR "failed to retrieve HKEY event\n");
1223 return;
1224 }
1225
1226 if (hkey == 0) {
1227 /* queue empty */
1228 return;
1229 }
1230
1231 send_acpi_ev = 0;
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03001232 ignore_acpi_ev = 0;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03001233
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001234 switch (hkey >> 12) {
1235 case 1:
1236 /* 0x1000-0x1FFF: key presses */
1237 scancode = hkey & 0xfff;
1238 if (scancode > 0 && scancode < 0x21) {
1239 scancode--;
1240 keycode = hotkey_keycode_map[scancode];
1241 tpacpi_input_send_key(scancode, keycode);
1242 } else {
1243 printk(IBM_ERR
1244 "hotkey 0x%04x out of range for keyboard map\n",
1245 hkey);
1246 send_acpi_ev = 1;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001247 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001248 break;
1249 case 5:
1250 /* 0x5000-0x5FFF: LID */
1251 /* we don't handle it through this path, just
1252 * eat up known LID events */
1253 if (hkey != 0x5001 && hkey != 0x5002) {
1254 printk(IBM_ERR
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03001255 "unknown LID-related HKEY event: 0x%04x\n",
1256 hkey);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001257 send_acpi_ev = 1;
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03001258 } else {
1259 ignore_acpi_ev = 1;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001260 }
1261 break;
1262 case 7:
1263 /* 0x7000-0x7FFF: misc */
1264 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
1265 tpacpi_input_send_radiosw();
1266 break;
1267 }
1268 /* fallthrough to default */
1269 default:
1270 /* case 2: dock-related */
1271 /* 0x2305 - T43 waking up due to bay lever eject while aslept */
1272 /* case 3: ultra-bay related. maybe bay in dock? */
1273 /* 0x3003 - T43 after wake up by bay lever eject (0x2305) */
1274 printk(IBM_NOTICE "unhandled HKEY event 0x%04x\n", hkey);
1275 send_acpi_ev = 1;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001276 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001277
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03001278 /* Legacy events */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03001279 if (!ignore_acpi_ev && (send_acpi_ev || hotkey_report_mode < 2)) {
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03001280 acpi_bus_generate_proc_event(ibm->acpi->device, event, hkey);
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03001281 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001282
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03001283 /* netlink events */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03001284 if (!ignore_acpi_ev && send_acpi_ev) {
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03001285 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
1286 ibm->acpi->device->dev.bus_id,
1287 event, hkey);
1288 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001289 }
1290}
1291
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03001292static void hotkey_resume(void)
1293{
1294 tpacpi_input_send_radiosw();
1295}
1296
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001297/*
1298 * Call with hotkey_mutex held
1299 */
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001300static int hotkey_get(int *status, u32 *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301{
1302 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001303 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001304
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001305 if (tp_features.hotkey_mask)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001306 if (!acpi_evalf(hkey_handle, mask, "DHKN", "d"))
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001307 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001308
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001309 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310}
1311
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001312/*
1313 * Call with hotkey_mutex held
1314 */
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001315static int hotkey_set(int status, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316{
1317 int i;
1318
1319 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001320 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001322 if (tp_features.hotkey_mask)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001323 for (i = 0; i < 32; i++) {
1324 int bit = ((1 << i) & mask) != 0;
1325 if (!acpi_evalf(hkey_handle,
1326 NULL, "MHKM", "vdd", i + 1, bit))
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001327 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001328 }
1329
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001330 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001331}
1332
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001333/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001334static int hotkey_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001336 int res, status;
1337 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 int len = 0;
1339
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001340 if (!tp_features.hotkey) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001341 len += sprintf(p + len, "status:\t\tnot supported\n");
1342 return len;
1343 }
1344
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001345 res = mutex_lock_interruptible(&hotkey_mutex);
1346 if (res < 0)
1347 return res;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001348 res = hotkey_get(&status, &mask);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001349 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001350 if (res)
1351 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
1353 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001354 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001355 len += sprintf(p + len, "mask:\t\t0x%08x\n", mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 len += sprintf(p + len,
1357 "commands:\tenable, disable, reset, <mask>\n");
1358 } else {
1359 len += sprintf(p + len, "mask:\t\tnot supported\n");
1360 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
1361 }
1362
1363 return len;
1364}
1365
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001366static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001368 int res, status;
1369 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 char *cmd;
1371 int do_cmd = 0;
1372
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001373 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 return -ENODEV;
1375
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001376 res = mutex_lock_interruptible(&hotkey_mutex);
1377 if (res < 0)
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001378 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001379
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001380 res = hotkey_get(&status, &mask);
1381 if (res)
1382 goto errexit;
1383
1384 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 while ((cmd = next_cmd(&buf))) {
1386 if (strlencmp(cmd, "enable") == 0) {
1387 status = 1;
1388 } else if (strlencmp(cmd, "disable") == 0) {
1389 status = 0;
1390 } else if (strlencmp(cmd, "reset") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001391 status = hotkey_orig_status;
1392 mask = hotkey_orig_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
1394 /* mask set */
1395 } else if (sscanf(cmd, "%x", &mask) == 1) {
1396 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001397 } else {
1398 res = -EINVAL;
1399 goto errexit;
1400 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 do_cmd = 1;
1402 }
1403
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001404 if (do_cmd)
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001405 res = hotkey_set(status, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001407errexit:
1408 mutex_unlock(&hotkey_mutex);
1409 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001410}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
Thomas Renninger1ba90e32007-07-23 14:44:41 +02001412static const struct acpi_device_id ibm_htk_device_ids[] = {
1413 {IBM_HKEY_HID, 0},
1414 {"", 0},
1415};
1416
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001417static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +02001418 .hid = ibm_htk_device_ids,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001419 .notify = hotkey_notify,
1420 .handle = &hkey_handle,
1421 .type = ACPI_DEVICE_NOTIFY,
1422};
1423
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001424static struct ibm_struct hotkey_driver_data = {
1425 .name = "hotkey",
1426 .read = hotkey_read,
1427 .write = hotkey_write,
1428 .exit = hotkey_exit,
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03001429 .resume = hotkey_resume,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001430 .acpi = &ibm_hotkey_acpidriver,
1431};
1432
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001433/*************************************************************************
1434 * Bluetooth subdriver
1435 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001437/* sysfs bluetooth enable ---------------------------------------------- */
1438static ssize_t bluetooth_enable_show(struct device *dev,
1439 struct device_attribute *attr,
1440 char *buf)
1441{
1442 int status;
1443
1444 status = bluetooth_get_radiosw();
1445 if (status < 0)
1446 return status;
1447
1448 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
1449}
1450
1451static ssize_t bluetooth_enable_store(struct device *dev,
1452 struct device_attribute *attr,
1453 const char *buf, size_t count)
1454{
1455 unsigned long t;
1456 int res;
1457
1458 if (parse_strtoul(buf, 1, &t))
1459 return -EINVAL;
1460
1461 res = bluetooth_set_radiosw(t);
1462
1463 return (res) ? res : count;
1464}
1465
1466static struct device_attribute dev_attr_bluetooth_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001467 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001468 bluetooth_enable_show, bluetooth_enable_store);
1469
1470/* --------------------------------------------------------------------- */
1471
1472static struct attribute *bluetooth_attributes[] = {
1473 &dev_attr_bluetooth_enable.attr,
1474 NULL
1475};
1476
1477static const struct attribute_group bluetooth_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001478 .attrs = bluetooth_attributes,
1479};
1480
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001481static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001483 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001484 int status = 0;
1485
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001486 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
1487
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001488 IBM_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001489
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001490 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1491 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001492 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001493 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001495 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
1496 str_supported(tp_features.bluetooth),
1497 status);
1498
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001499 if (tp_features.bluetooth) {
1500 if (!(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
1501 /* no bluetooth hardware present in system */
1502 tp_features.bluetooth = 0;
1503 dbg_printk(TPACPI_DBG_INIT,
1504 "bluetooth hardware not installed\n");
1505 } else {
1506 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
1507 &bluetooth_attr_group);
1508 if (res)
1509 return res;
1510 }
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001511 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001512
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001513 return (tp_features.bluetooth)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514}
1515
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001516static void bluetooth_exit(void)
1517{
1518 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
1519 &bluetooth_attr_group);
1520}
1521
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001522static int bluetooth_get_radiosw(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523{
1524 int status;
1525
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001526 if (!tp_features.bluetooth)
1527 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001529 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
1530 return -EIO;
1531
1532 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
1533}
1534
1535static int bluetooth_set_radiosw(int radio_on)
1536{
1537 int status;
1538
1539 if (!tp_features.bluetooth)
1540 return -ENODEV;
1541
1542 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
1543 return -EIO;
1544 if (radio_on)
1545 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
1546 else
1547 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
1548 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
1549 return -EIO;
1550
1551 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552}
1553
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001554/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001555static int bluetooth_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556{
1557 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001558 int status = bluetooth_get_radiosw();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001560 if (!tp_features.bluetooth)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 len += sprintf(p + len, "status:\t\tnot supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001563 len += sprintf(p + len, "status:\t\t%s\n",
1564 (status)? "enabled" : "disabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 len += sprintf(p + len, "commands:\tenable, disable\n");
1566 }
1567
1568 return len;
1569}
1570
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001571static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001575 if (!tp_features.bluetooth)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001576 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
1578 while ((cmd = next_cmd(&buf))) {
1579 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001580 bluetooth_set_radiosw(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001582 bluetooth_set_radiosw(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 } else
1584 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 }
1586
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 return 0;
1588}
1589
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001590static struct ibm_struct bluetooth_driver_data = {
1591 .name = "bluetooth",
1592 .read = bluetooth_read,
1593 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001594 .exit = bluetooth_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001595};
1596
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001597/*************************************************************************
1598 * Wan subdriver
1599 */
1600
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001601/* sysfs wan enable ---------------------------------------------------- */
1602static ssize_t wan_enable_show(struct device *dev,
1603 struct device_attribute *attr,
1604 char *buf)
1605{
1606 int status;
1607
1608 status = wan_get_radiosw();
1609 if (status < 0)
1610 return status;
1611
1612 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
1613}
1614
1615static ssize_t wan_enable_store(struct device *dev,
1616 struct device_attribute *attr,
1617 const char *buf, size_t count)
1618{
1619 unsigned long t;
1620 int res;
1621
1622 if (parse_strtoul(buf, 1, &t))
1623 return -EINVAL;
1624
1625 res = wan_set_radiosw(t);
1626
1627 return (res) ? res : count;
1628}
1629
1630static struct device_attribute dev_attr_wan_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001631 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001632 wan_enable_show, wan_enable_store);
1633
1634/* --------------------------------------------------------------------- */
1635
1636static struct attribute *wan_attributes[] = {
1637 &dev_attr_wan_enable.attr,
1638 NULL
1639};
1640
1641static const struct attribute_group wan_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001642 .attrs = wan_attributes,
1643};
1644
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001645static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001646{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001647 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001648 int status = 0;
1649
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001650 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
1651
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001652 IBM_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001653
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001654 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001655 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001656
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001657 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
1658 str_supported(tp_features.wan),
1659 status);
1660
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001661 if (tp_features.wan) {
1662 if (!(status & TP_ACPI_WANCARD_HWPRESENT)) {
1663 /* no wan hardware present in system */
1664 tp_features.wan = 0;
1665 dbg_printk(TPACPI_DBG_INIT,
1666 "wan hardware not installed\n");
1667 } else {
1668 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
1669 &wan_attr_group);
1670 if (res)
1671 return res;
1672 }
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001673 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001674
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001675 return (tp_features.wan)? 0 : 1;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001676}
1677
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001678static void wan_exit(void)
1679{
1680 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
1681 &wan_attr_group);
1682}
1683
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001684static int wan_get_radiosw(void)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001685{
1686 int status;
1687
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001688 if (!tp_features.wan)
1689 return -ENODEV;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001690
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001691 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
1692 return -EIO;
1693
1694 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
1695}
1696
1697static int wan_set_radiosw(int radio_on)
1698{
1699 int status;
1700
1701 if (!tp_features.wan)
1702 return -ENODEV;
1703
1704 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
1705 return -EIO;
1706 if (radio_on)
1707 status |= TP_ACPI_WANCARD_RADIOSSW;
1708 else
1709 status &= ~TP_ACPI_WANCARD_RADIOSSW;
1710 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
1711 return -EIO;
1712
1713 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001714}
1715
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001716/* procfs -------------------------------------------------------------- */
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001717static int wan_read(char *p)
1718{
1719 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001720 int status = wan_get_radiosw();
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001721
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001722 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001723 len += sprintf(p + len, "status:\t\tnot supported\n");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001724 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001725 len += sprintf(p + len, "status:\t\t%s\n",
1726 (status)? "enabled" : "disabled");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001727 len += sprintf(p + len, "commands:\tenable, disable\n");
1728 }
1729
1730 return len;
1731}
1732
1733static int wan_write(char *buf)
1734{
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001735 char *cmd;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001736
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001737 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001738 return -ENODEV;
1739
1740 while ((cmd = next_cmd(&buf))) {
1741 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001742 wan_set_radiosw(1);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001743 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001744 wan_set_radiosw(0);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001745 } else
1746 return -EINVAL;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001747 }
1748
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001749 return 0;
1750}
1751
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001752static struct ibm_struct wan_driver_data = {
1753 .name = "wan",
1754 .read = wan_read,
1755 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001756 .exit = wan_exit,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03001757 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001758};
1759
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001760/*************************************************************************
1761 * Video subdriver
1762 */
1763
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02001764static enum video_access_mode video_supported;
1765static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001766
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001767IBM_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
1768 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
1769 "\\_SB.PCI0.VID0", /* 770e */
1770 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
1771 "\\_SB.PCI0.AGP.VID", /* all others */
1772 ); /* R30, R31 */
1773
1774IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
1775
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001776static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001778 int ivga;
1779
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001780 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
1781
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001782 IBM_ACPIHANDLE_INIT(vid);
1783 IBM_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001784
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001785 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
1786 /* G41, assume IVGA doesn't change */
1787 vid_handle = vid2_handle;
1788
1789 if (!vid_handle)
1790 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001791 video_supported = TPACPI_VIDEO_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001792 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
1793 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001794 video_supported = TPACPI_VIDEO_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001795 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
1796 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001797 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001798 else
1799 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001800 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001802 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
1803 str_supported(video_supported != TPACPI_VIDEO_NONE),
1804 video_supported);
1805
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001806 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807}
1808
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001809static void video_exit(void)
1810{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001811 dbg_printk(TPACPI_DBG_EXIT,
1812 "restoring original video autoswitch mode\n");
1813 if (video_autosw_set(video_orig_autosw))
1814 printk(IBM_ERR "error while trying to restore original "
1815 "video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001816}
1817
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001818static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819{
1820 int status = 0;
1821 int i;
1822
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001823 switch (video_supported) {
1824 case TPACPI_VIDEO_570:
1825 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
1826 TP_ACPI_VIDEO_570_PHSCMD))
1827 return -EIO;
1828 status = i & TP_ACPI_VIDEO_570_PHSMASK;
1829 break;
1830 case TPACPI_VIDEO_770:
1831 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
1832 return -EIO;
1833 if (i)
1834 status |= TP_ACPI_VIDEO_S_LCD;
1835 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
1836 return -EIO;
1837 if (i)
1838 status |= TP_ACPI_VIDEO_S_CRT;
1839 break;
1840 case TPACPI_VIDEO_NEW:
1841 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
1842 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
1843 return -EIO;
1844 if (i)
1845 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001847 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
1848 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
1849 return -EIO;
1850 if (i)
1851 status |= TP_ACPI_VIDEO_S_LCD;
1852 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
1853 return -EIO;
1854 if (i)
1855 status |= TP_ACPI_VIDEO_S_DVI;
1856 break;
1857 default:
1858 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001859 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860
1861 return status;
1862}
1863
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001864static int video_outputsw_set(int status)
1865{
1866 int autosw;
1867 int res = 0;
1868
1869 switch (video_supported) {
1870 case TPACPI_VIDEO_570:
1871 res = acpi_evalf(NULL, NULL,
1872 "\\_SB.PHS2", "vdd",
1873 TP_ACPI_VIDEO_570_PHS2CMD,
1874 status | TP_ACPI_VIDEO_570_PHS2SET);
1875 break;
1876 case TPACPI_VIDEO_770:
1877 autosw = video_autosw_get();
1878 if (autosw < 0)
1879 return autosw;
1880
1881 res = video_autosw_set(1);
1882 if (res)
1883 return res;
1884 res = acpi_evalf(vid_handle, NULL,
1885 "ASWT", "vdd", status * 0x100, 0);
1886 if (!autosw && video_autosw_set(autosw)) {
1887 printk(IBM_ERR "video auto-switch left enabled due to error\n");
1888 return -EIO;
1889 }
1890 break;
1891 case TPACPI_VIDEO_NEW:
1892 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
1893 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
1894 break;
1895 default:
1896 return -ENOSYS;
1897 }
1898
1899 return (res)? 0 : -EIO;
1900}
1901
1902static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001904 int autosw = 0;
1905
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001906 switch (video_supported) {
1907 case TPACPI_VIDEO_570:
1908 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
1909 return -EIO;
1910 break;
1911 case TPACPI_VIDEO_770:
1912 case TPACPI_VIDEO_NEW:
1913 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
1914 return -EIO;
1915 break;
1916 default:
1917 return -ENOSYS;
1918 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001919
1920 return autosw & 1;
1921}
1922
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001923static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001924{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001925 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001926 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001927 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001928}
1929
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001930static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001931{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001932 int autosw = video_autosw_get();
1933 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001934
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001935 if (autosw < 0)
1936 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001937
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001938 switch (video_supported) {
1939 case TPACPI_VIDEO_570:
1940 res = video_autosw_set(1);
1941 if (res)
1942 return res;
1943 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
1944 break;
1945 case TPACPI_VIDEO_770:
1946 case TPACPI_VIDEO_NEW:
1947 res = video_autosw_set(1);
1948 if (res)
1949 return res;
1950 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
1951 break;
1952 default:
1953 return -ENOSYS;
1954 }
1955 if (!autosw && video_autosw_set(autosw)) {
1956 printk(IBM_ERR "video auto-switch left enabled due to error\n");
1957 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001958 }
1959
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001960 return (res)? 0 : -EIO;
1961}
1962
1963static int video_expand_toggle(void)
1964{
1965 switch (video_supported) {
1966 case TPACPI_VIDEO_570:
1967 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
1968 0 : -EIO;
1969 case TPACPI_VIDEO_770:
1970 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
1971 0 : -EIO;
1972 case TPACPI_VIDEO_NEW:
1973 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
1974 0 : -EIO;
1975 default:
1976 return -ENOSYS;
1977 }
1978 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001979}
1980
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001981static int video_read(char *p)
1982{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001983 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001984 int len = 0;
1985
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001986 if (video_supported == TPACPI_VIDEO_NONE) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001987 len += sprintf(p + len, "status:\t\tnot supported\n");
1988 return len;
1989 }
1990
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001991 status = video_outputsw_get();
1992 if (status < 0)
1993 return status;
1994
1995 autosw = video_autosw_get();
1996 if (autosw < 0)
1997 return autosw;
1998
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001999 len += sprintf(p + len, "status:\t\tsupported\n");
2000 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
2001 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002002 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002003 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
2004 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
2005 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
2006 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002007 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002008 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
2009 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
2010 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
2011
2012 return len;
2013}
2014
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002015static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016{
2017 char *cmd;
2018 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002019 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002021 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002022 return -ENODEV;
2023
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002024 enable = 0;
2025 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026
2027 while ((cmd = next_cmd(&buf))) {
2028 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002029 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002031 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002033 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002035 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002036 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002037 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002038 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002039 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002040 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002041 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002043 res = video_autosw_set(1);
2044 if (res)
2045 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002047 res = video_autosw_set(0);
2048 if (res)
2049 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002051 res = video_outputsw_cycle();
2052 if (res)
2053 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002055 res = video_expand_toggle();
2056 if (res)
2057 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 } else
2059 return -EINVAL;
2060 }
2061
2062 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002063 status = video_outputsw_get();
2064 if (status < 0)
2065 return status;
2066 res = video_outputsw_set((status & ~disable) | enable);
2067 if (res)
2068 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 }
2070
2071 return 0;
2072}
2073
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002074static struct ibm_struct video_driver_data = {
2075 .name = "video",
2076 .read = video_read,
2077 .write = video_write,
2078 .exit = video_exit,
2079};
2080
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002081/*************************************************************************
2082 * Light (thinklight) subdriver
2083 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002085IBM_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
2086IBM_HANDLE(ledb, ec, "LEDB"); /* G4x */
2087
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002088static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002090 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
2091
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002092 IBM_ACPIHANDLE_INIT(ledb);
2093 IBM_ACPIHANDLE_INIT(lght);
2094 IBM_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002095
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002096 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002097 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002098
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002099 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002100 /* light status not supported on
2101 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002102 tp_features.light_status =
2103 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002105 vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002106 str_supported(tp_features.light));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002107
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002108 return (tp_features.light)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109}
2110
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002111static int light_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112{
2113 int len = 0;
2114 int status = 0;
2115
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002116 if (!tp_features.light) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002117 len += sprintf(p + len, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002118 } else if (!tp_features.light_status) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002119 len += sprintf(p + len, "status:\t\tunknown\n");
2120 len += sprintf(p + len, "commands:\ton, off\n");
2121 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
2123 return -EIO;
2124 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002125 len += sprintf(p + len, "commands:\ton, off\n");
2126 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127
2128 return len;
2129}
2130
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002131static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132{
2133 int cmos_cmd, lght_cmd;
2134 char *cmd;
2135 int success;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002136
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002137 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002138 return -ENODEV;
2139
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 while ((cmd = next_cmd(&buf))) {
2141 if (strlencmp(cmd, "on") == 0) {
2142 cmos_cmd = 0x0c;
2143 lght_cmd = 1;
2144 } else if (strlencmp(cmd, "off") == 0) {
2145 cmos_cmd = 0x0d;
2146 lght_cmd = 0;
2147 } else
2148 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002149
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 success = cmos_handle ?
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002151 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
2152 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 if (!success)
2154 return -EIO;
2155 }
2156
2157 return 0;
2158}
2159
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002160static struct ibm_struct light_driver_data = {
2161 .name = "light",
2162 .read = light_read,
2163 .write = light_write,
2164};
2165
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002166/*************************************************************************
2167 * Dock subdriver
2168 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03002170#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002171
2172IBM_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
2173 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
2174 "\\_SB.PCI0.PCI1.DOCK", /* all others */
2175 "\\_SB.PCI.ISA.SLCE", /* 570 */
2176 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
2177
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002178/* don't list other alternatives as we install a notify handler on the 570 */
2179IBM_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
2180
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002181static const struct acpi_device_id ibm_pci_device_ids[] = {
2182 {PCI_ROOT_HID_STRING, 0},
2183 {"", 0},
2184};
2185
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03002186static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
2187 {
2188 .notify = dock_notify,
2189 .handle = &dock_handle,
2190 .type = ACPI_SYSTEM_NOTIFY,
2191 },
2192 {
Henrique de Moraes Holschuh996fba02007-07-18 23:45:40 -03002193 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
2194 * We just use it to get notifications of dock hotplug
2195 * in very old thinkpads */
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002196 .hid = ibm_pci_device_ids,
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03002197 .notify = dock_notify,
2198 .handle = &pci_handle,
2199 .type = ACPI_SYSTEM_NOTIFY,
2200 },
2201};
2202
2203static struct ibm_struct dock_driver_data[2] = {
2204 {
2205 .name = "dock",
2206 .read = dock_read,
2207 .write = dock_write,
2208 .acpi = &ibm_dock_acpidriver[0],
2209 },
2210 {
2211 .name = "dock",
2212 .acpi = &ibm_dock_acpidriver[1],
2213 },
2214};
2215
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216#define dock_docked() (_sta(dock_handle) & 1)
2217
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002218static int __init dock_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002219{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002220 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
2221
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002222 IBM_ACPIHANDLE_INIT(dock);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002223
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002224 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
2225 str_supported(dock_handle != NULL));
2226
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002227 return (dock_handle)? 0 : 1;
2228}
2229
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03002230static int __init dock_init2(struct ibm_init_struct *iibm)
2231{
2232 int dock2_needed;
2233
2234 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
2235
2236 if (dock_driver_data[0].flags.acpi_driver_registered &&
2237 dock_driver_data[0].flags.acpi_notify_installed) {
2238 IBM_ACPIHANDLE_INIT(pci);
2239 dock2_needed = (pci_handle != NULL);
2240 vdbg_printk(TPACPI_DBG_INIT,
2241 "dock PCI handler for the TP 570 is %s\n",
2242 str_supported(dock2_needed));
2243 } else {
2244 vdbg_printk(TPACPI_DBG_INIT,
2245 "dock subdriver part 2 not required\n");
2246 dock2_needed = 0;
2247 }
2248
2249 return (dock2_needed)? 0 : 1;
2250}
2251
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002252static void dock_notify(struct ibm_struct *ibm, u32 event)
2253{
2254 int docked = dock_docked();
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002255 int pci = ibm->acpi->hid && ibm->acpi->device &&
2256 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
Zhang Rui962ce8c2007-08-23 01:24:31 +08002257 int data;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002258
2259 if (event == 1 && !pci) /* 570 */
Zhang Rui962ce8c2007-08-23 01:24:31 +08002260 data = 1; /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002261 else if (event == 1 && pci) /* 570 */
Zhang Rui962ce8c2007-08-23 01:24:31 +08002262 data = 3; /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002263 else if (event == 3 && docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08002264 data = 1; /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002265 else if (event == 3 && !docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08002266 data = 2; /* undock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002267 else if (event == 0 && docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08002268 data = 3; /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002269 else {
2270 printk(IBM_ERR "unknown dock event %d, status %d\n",
2271 event, _sta(dock_handle));
Zhang Rui962ce8c2007-08-23 01:24:31 +08002272 data = 0; /* unknown */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002273 }
Len Brown14e04fb2007-08-23 15:20:26 -04002274 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
Zhang Rui962ce8c2007-08-23 01:24:31 +08002275 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
2276 ibm->acpi->device->dev.bus_id,
2277 event, data);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002278}
2279
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002280static int dock_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281{
2282 int len = 0;
2283 int docked = dock_docked();
2284
2285 if (!dock_handle)
2286 len += sprintf(p + len, "status:\t\tnot supported\n");
2287 else if (!docked)
2288 len += sprintf(p + len, "status:\t\tundocked\n");
2289 else {
2290 len += sprintf(p + len, "status:\t\tdocked\n");
2291 len += sprintf(p + len, "commands:\tdock, undock\n");
2292 }
2293
2294 return len;
2295}
2296
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002297static int dock_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298{
2299 char *cmd;
2300
2301 if (!dock_docked())
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002302 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303
2304 while ((cmd = next_cmd(&buf))) {
2305 if (strlencmp(cmd, "undock") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002306 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
2307 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 return -EIO;
2309 } else if (strlencmp(cmd, "dock") == 0) {
2310 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
2311 return -EIO;
2312 } else
2313 return -EINVAL;
2314 }
2315
2316 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002317}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03002319#endif /* CONFIG_THINKPAD_ACPI_DOCK */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002321/*************************************************************************
2322 * Bay subdriver
2323 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03002325#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002326IBM_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
2327 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
2328 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
2329 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
2330 ); /* A21e, R30, R31 */
2331IBM_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
2332 "_EJ0", /* all others */
2333 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
2334IBM_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
2335 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
2336 ); /* all others */
2337IBM_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
2338 "_EJ0", /* 770x */
2339 ); /* all others */
2340
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002341static int __init bay_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002343 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
2344
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002345 IBM_ACPIHANDLE_INIT(bay);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002346 if (bay_handle)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002347 IBM_ACPIHANDLE_INIT(bay_ej);
2348 IBM_ACPIHANDLE_INIT(bay2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002349 if (bay2_handle)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002350 IBM_ACPIHANDLE_INIT(bay2_ej);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002351
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002352 tp_features.bay_status = bay_handle &&
2353 acpi_evalf(bay_handle, NULL, "_STA", "qv");
2354 tp_features.bay_status2 = bay2_handle &&
2355 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002356
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002357 tp_features.bay_eject = bay_handle && bay_ej_handle &&
2358 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
2359 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
2360 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002362 vdbg_printk(TPACPI_DBG_INIT,
2363 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002364 str_supported(tp_features.bay_status),
2365 str_supported(tp_features.bay_eject),
2366 str_supported(tp_features.bay_status2),
2367 str_supported(tp_features.bay_eject2));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002368
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002369 return (tp_features.bay_status || tp_features.bay_eject ||
2370 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371}
2372
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002373static void bay_notify(struct ibm_struct *ibm, u32 event)
2374{
Len Brown14e04fb2007-08-23 15:20:26 -04002375 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
Zhang Rui962ce8c2007-08-23 01:24:31 +08002376 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
2377 ibm->acpi->device->dev.bus_id,
2378 event, 0);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002379}
2380
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002381#define bay_occupied(b) (_sta(b##_handle) & 1)
2382
2383static int bay_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384{
2385 int len = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002386 int occupied = bay_occupied(bay);
2387 int occupied2 = bay_occupied(bay2);
2388 int eject, eject2;
2389
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002390 len += sprintf(p + len, "status:\t\t%s\n",
2391 tp_features.bay_status ?
2392 (occupied ? "occupied" : "unoccupied") :
2393 "not supported");
2394 if (tp_features.bay_status2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002395 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
2396 "occupied" : "unoccupied");
2397
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002398 eject = tp_features.bay_eject && occupied;
2399 eject2 = tp_features.bay_eject2 && occupied2;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002400
2401 if (eject && eject2)
2402 len += sprintf(p + len, "commands:\teject, eject2\n");
2403 else if (eject)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 len += sprintf(p + len, "commands:\teject\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002405 else if (eject2)
2406 len += sprintf(p + len, "commands:\teject2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407
2408 return len;
2409}
2410
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002411static int bay_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412{
2413 char *cmd;
2414
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002415 if (!tp_features.bay_eject && !tp_features.bay_eject2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002416 return -ENODEV;
2417
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002419 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002420 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
2421 return -EIO;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002422 } else if (tp_features.bay_eject2 &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002423 strlencmp(cmd, "eject2") == 0) {
2424 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 return -EIO;
2426 } else
2427 return -EINVAL;
2428 }
2429
2430 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002431}
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002432
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002433static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
2434 .notify = bay_notify,
2435 .handle = &bay_handle,
2436 .type = ACPI_SYSTEM_NOTIFY,
2437};
2438
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002439static struct ibm_struct bay_driver_data = {
2440 .name = "bay",
2441 .read = bay_read,
2442 .write = bay_write,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002443 .acpi = &ibm_bay_acpidriver,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002444};
2445
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03002446#endif /* CONFIG_THINKPAD_ACPI_BAY */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002448/*************************************************************************
2449 * CMOS subdriver
2450 */
2451
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03002452/* sysfs cmos_command -------------------------------------------------- */
2453static ssize_t cmos_command_store(struct device *dev,
2454 struct device_attribute *attr,
2455 const char *buf, size_t count)
2456{
2457 unsigned long cmos_cmd;
2458 int res;
2459
2460 if (parse_strtoul(buf, 21, &cmos_cmd))
2461 return -EINVAL;
2462
2463 res = issue_thinkpad_cmos_command(cmos_cmd);
2464 return (res)? res : count;
2465}
2466
2467static struct device_attribute dev_attr_cmos_command =
2468 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
2469
2470/* --------------------------------------------------------------------- */
2471
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002472static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002473{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03002474 int res;
2475
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002476 vdbg_printk(TPACPI_DBG_INIT,
2477 "initializing cmos commands subdriver\n");
2478
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002479 IBM_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002480
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002481 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
2482 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03002483
2484 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
2485 if (res)
2486 return res;
2487
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002488 return (cmos_handle)? 0 : 1;
2489}
2490
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03002491static void cmos_exit(void)
2492{
2493 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
2494}
2495
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002496static int cmos_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497{
2498 int len = 0;
2499
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002500 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2501 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 if (!cmos_handle)
2503 len += sprintf(p + len, "status:\t\tnot supported\n");
2504 else {
2505 len += sprintf(p + len, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002506 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 }
2508
2509 return len;
2510}
2511
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002512static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513{
2514 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03002515 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516
2517 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002518 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
2519 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 /* cmos_cmd set */
2521 } else
2522 return -EINVAL;
2523
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03002524 res = issue_thinkpad_cmos_command(cmos_cmd);
2525 if (res)
2526 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 }
2528
2529 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002530}
2531
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002532static struct ibm_struct cmos_driver_data = {
2533 .name = "cmos",
2534 .read = cmos_read,
2535 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03002536 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002537};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002538
2539/*************************************************************************
2540 * LED subdriver
2541 */
2542
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02002543static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002544
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002545IBM_HANDLE(led, ec, "SLED", /* 570 */
2546 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
2547 "LED", /* all others */
2548 ); /* R30, R31 */
2549
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002550static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002551{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002552 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
2553
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002554 IBM_ACPIHANDLE_INIT(led);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002555
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002556 if (!led_handle)
2557 /* led not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002558 led_supported = TPACPI_LED_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002559 else if (strlencmp(led_path, "SLED") == 0)
2560 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002561 led_supported = TPACPI_LED_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002562 else if (strlencmp(led_path, "SYSL") == 0)
2563 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002564 led_supported = TPACPI_LED_OLD;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002565 else
2566 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002567 led_supported = TPACPI_LED_NEW;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002568
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002569 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
2570 str_supported(led_supported), led_supported);
2571
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002572 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002573}
2574
2575#define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
2576
2577static int led_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578{
2579 int len = 0;
2580
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002581 if (!led_supported) {
2582 len += sprintf(p + len, "status:\t\tnot supported\n");
2583 return len;
2584 }
2585 len += sprintf(p + len, "status:\t\tsupported\n");
2586
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002587 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002588 /* 570 */
2589 int i, status;
2590 for (i = 0; i < 8; i++) {
2591 if (!acpi_evalf(ec_handle,
2592 &status, "GLED", "dd", 1 << i))
2593 return -EIO;
2594 len += sprintf(p + len, "%d:\t\t%s\n",
2595 i, led_status(status));
2596 }
2597 }
2598
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 len += sprintf(p + len, "commands:\t"
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002600 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601
2602 return len;
2603}
2604
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002605/* off, on, blink */
2606static const int led_sled_arg1[] = { 0, 1, 3 };
2607static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
2608static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
2609static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
2610
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002611static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612{
2613 char *cmd;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002614 int led, ind, ret;
2615
2616 if (!led_supported)
2617 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618
2619 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002620 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 return -EINVAL;
2622
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002623 if (strstr(cmd, "off")) {
2624 ind = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 } else if (strstr(cmd, "on")) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002626 ind = 1;
2627 } else if (strstr(cmd, "blink")) {
2628 ind = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 } else
2630 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002631
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002632 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002633 /* 570 */
2634 led = 1 << led;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002636 led, led_sled_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 return -EIO;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002638 } else if (led_supported == TPACPI_LED_OLD) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002639 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
2640 led = 1 << led;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002641 ret = ec_write(TPACPI_LED_EC_HLMS, led);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002642 if (ret >= 0)
2643 ret =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002644 ec_write(TPACPI_LED_EC_HLBL,
Henrique de Moraes Holschuhe062e032007-03-23 17:33:55 -03002645 led * led_exp_hlbl[ind]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002646 if (ret >= 0)
2647 ret =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002648 ec_write(TPACPI_LED_EC_HLCL,
Henrique de Moraes Holschuhe062e032007-03-23 17:33:55 -03002649 led * led_exp_hlcl[ind]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002650 if (ret < 0)
2651 return ret;
2652 } else {
2653 /* all others */
2654 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
2655 led, led_led_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002657 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 }
2659
2660 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002661}
2662
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002663static struct ibm_struct led_driver_data = {
2664 .name = "led",
2665 .read = led_read,
2666 .write = led_write,
2667};
2668
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002669/*************************************************************************
2670 * Beep subdriver
2671 */
2672
2673IBM_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
2674
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002675static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002676{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002677 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
2678
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002679 IBM_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002680
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002681 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
2682 str_supported(beep_handle != NULL));
2683
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002684 return (beep_handle)? 0 : 1;
2685}
2686
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002687static int beep_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688{
2689 int len = 0;
2690
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002691 if (!beep_handle)
2692 len += sprintf(p + len, "status:\t\tnot supported\n");
2693 else {
2694 len += sprintf(p + len, "status:\t\tsupported\n");
2695 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
2696 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697
2698 return len;
2699}
2700
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002701static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702{
2703 char *cmd;
2704 int beep_cmd;
2705
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002706 if (!beep_handle)
2707 return -ENODEV;
2708
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002710 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
2711 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 /* beep_cmd set */
2713 } else
2714 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002715 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 return -EIO;
2717 }
2718
2719 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002720}
2721
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002722static struct ibm_struct beep_driver_data = {
2723 .name = "beep",
2724 .read = beep_read,
2725 .write = beep_write,
2726};
2727
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002728/*************************************************************************
2729 * Thermal subdriver
2730 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002731
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002732static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002733
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03002734/* sysfs temp##_input -------------------------------------------------- */
2735
2736static ssize_t thermal_temp_input_show(struct device *dev,
2737 struct device_attribute *attr,
2738 char *buf)
2739{
2740 struct sensor_device_attribute *sensor_attr =
2741 to_sensor_dev_attr(attr);
2742 int idx = sensor_attr->index;
2743 s32 value;
2744 int res;
2745
2746 res = thermal_get_sensor(idx, &value);
2747 if (res)
2748 return res;
2749 if (value == TP_EC_THERMAL_TMP_NA * 1000)
2750 return -ENXIO;
2751
2752 return snprintf(buf, PAGE_SIZE, "%d\n", value);
2753}
2754
2755#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
2756 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, thermal_temp_input_show, NULL, _idxB)
2757
2758static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
2759 THERMAL_SENSOR_ATTR_TEMP(1, 0),
2760 THERMAL_SENSOR_ATTR_TEMP(2, 1),
2761 THERMAL_SENSOR_ATTR_TEMP(3, 2),
2762 THERMAL_SENSOR_ATTR_TEMP(4, 3),
2763 THERMAL_SENSOR_ATTR_TEMP(5, 4),
2764 THERMAL_SENSOR_ATTR_TEMP(6, 5),
2765 THERMAL_SENSOR_ATTR_TEMP(7, 6),
2766 THERMAL_SENSOR_ATTR_TEMP(8, 7),
2767 THERMAL_SENSOR_ATTR_TEMP(9, 8),
2768 THERMAL_SENSOR_ATTR_TEMP(10, 9),
2769 THERMAL_SENSOR_ATTR_TEMP(11, 10),
2770 THERMAL_SENSOR_ATTR_TEMP(12, 11),
2771 THERMAL_SENSOR_ATTR_TEMP(13, 12),
2772 THERMAL_SENSOR_ATTR_TEMP(14, 13),
2773 THERMAL_SENSOR_ATTR_TEMP(15, 14),
2774 THERMAL_SENSOR_ATTR_TEMP(16, 15),
2775};
2776
2777#define THERMAL_ATTRS(X) \
2778 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
2779
2780static struct attribute *thermal_temp_input_attr[] = {
2781 THERMAL_ATTRS(8),
2782 THERMAL_ATTRS(9),
2783 THERMAL_ATTRS(10),
2784 THERMAL_ATTRS(11),
2785 THERMAL_ATTRS(12),
2786 THERMAL_ATTRS(13),
2787 THERMAL_ATTRS(14),
2788 THERMAL_ATTRS(15),
2789 THERMAL_ATTRS(0),
2790 THERMAL_ATTRS(1),
2791 THERMAL_ATTRS(2),
2792 THERMAL_ATTRS(3),
2793 THERMAL_ATTRS(4),
2794 THERMAL_ATTRS(5),
2795 THERMAL_ATTRS(6),
2796 THERMAL_ATTRS(7),
2797 NULL
2798};
2799
2800static const struct attribute_group thermal_temp_input16_group = {
2801 .attrs = thermal_temp_input_attr
2802};
2803
2804static const struct attribute_group thermal_temp_input8_group = {
2805 .attrs = &thermal_temp_input_attr[8]
2806};
2807
2808#undef THERMAL_SENSOR_ATTR_TEMP
2809#undef THERMAL_ATTRS
2810
2811/* --------------------------------------------------------------------- */
2812
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002813static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002814{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002815 u8 t, ta1, ta2;
2816 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002817 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03002818 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002819
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002820 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
2821
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002822 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002823
Henrique de Moraes Holschuh3d6f99c2007-07-18 23:45:46 -03002824 if (thinkpad_id.ec_model) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002825 /*
2826 * Direct EC access mode: sensors at registers
2827 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
2828 * non-implemented, thermal sensors return 0x80 when
2829 * not available
2830 */
2831
2832 ta1 = ta2 = 0;
2833 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002834 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002835 ta1 |= t;
2836 } else {
2837 ta1 = 0;
2838 break;
2839 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002840 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002841 ta2 |= t;
2842 } else {
2843 ta1 = 0;
2844 break;
2845 }
2846 }
2847 if (ta1 == 0) {
2848 /* This is sheer paranoia, but we handle it anyway */
2849 if (acpi_tmp7) {
2850 printk(IBM_ERR
2851 "ThinkPad ACPI EC access misbehaving, "
2852 "falling back to ACPI TMPx access mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002853 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002854 } else {
2855 printk(IBM_ERR
2856 "ThinkPad ACPI EC access misbehaving, "
2857 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002858 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002859 }
2860 } else {
2861 thermal_read_mode =
2862 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002863 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002864 }
2865 } else if (acpi_tmp7) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002866 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
2867 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002868 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002869 } else {
2870 /* Standard ACPI TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002871 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002872 }
2873 } else {
2874 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002875 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002876 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002877
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002878 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
2879 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
2880 thermal_read_mode);
2881
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03002882 switch(thermal_read_mode) {
2883 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03002884 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03002885 &thermal_temp_input16_group);
2886 if (res)
2887 return res;
2888 break;
2889 case TPACPI_THERMAL_TPEC_8:
2890 case TPACPI_THERMAL_ACPI_TMP07:
2891 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03002892 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03002893 &thermal_temp_input8_group);
2894 if (res)
2895 return res;
2896 break;
2897 case TPACPI_THERMAL_NONE:
2898 default:
2899 return 1;
2900 }
2901
2902 return 0;
2903}
2904
2905static void thermal_exit(void)
2906{
2907 switch(thermal_read_mode) {
2908 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03002909 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03002910 &thermal_temp_input16_group);
2911 break;
2912 case TPACPI_THERMAL_TPEC_8:
2913 case TPACPI_THERMAL_ACPI_TMP07:
2914 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03002915 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03002916 &thermal_temp_input16_group);
2917 break;
2918 case TPACPI_THERMAL_NONE:
2919 default:
2920 break;
2921 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002922}
2923
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002924/* idx is zero-based */
2925static int thermal_get_sensor(int idx, s32 *value)
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002926{
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002927 int t;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002928 s8 tmp;
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002929 char tmpi[5];
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002930
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002931 t = TP_EC_THERMAL_TMP0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002932
2933 switch (thermal_read_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002934#if TPACPI_MAX_THERMAL_SENSORS >= 16
2935 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002936 if (idx >= 8 && idx <= 15) {
2937 t = TP_EC_THERMAL_TMP8;
2938 idx -= 8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002939 }
2940 /* fallthrough */
2941#endif
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002942 case TPACPI_THERMAL_TPEC_8:
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002943 if (idx <= 7) {
2944 if (!acpi_ec_read(t + idx, &tmp))
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002945 return -EIO;
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002946 *value = tmp * 1000;
2947 return 0;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002948 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002949 break;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002950
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002951 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002952 if (idx <= 7) {
2953 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
2954 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
2955 return -EIO;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002956 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
2957 return -EIO;
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002958 *value = (t - 2732) * 100;
2959 return 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002960 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002961 break;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002962
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002963 case TPACPI_THERMAL_ACPI_TMP07:
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002964 if (idx <= 7) {
2965 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002966 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
2967 return -EIO;
Henrique de Moraes Holschuha8fba3da2007-07-18 23:45:45 -03002968 if (t > 127 || t < -127)
2969 t = TP_EC_THERMAL_TMP_NA;
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002970 *value = t * 1000;
2971 return 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002972 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002973 break;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002974
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002975 case TPACPI_THERMAL_NONE:
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002976 default:
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002977 return -ENOSYS;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002978 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002979
2980 return -EINVAL;
2981}
2982
2983static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
2984{
2985 int res, i;
2986 int n;
2987
2988 n = 8;
2989 i = 0;
2990
2991 if (!s)
2992 return -EINVAL;
2993
2994 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
2995 n = 16;
2996
2997 for(i = 0 ; i < n; i++) {
2998 res = thermal_get_sensor(i, &s->temp[i]);
2999 if (res)
3000 return res;
3001 }
3002
3003 return n;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003004}
3005
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003006static int thermal_read(char *p)
3007{
3008 int len = 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003009 int n, i;
3010 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003011
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003012 n = thermal_get_sensors(&t);
3013 if (unlikely(n < 0))
3014 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003015
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003016 len += sprintf(p + len, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003017
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003018 if (n > 0) {
3019 for (i = 0; i < (n - 1); i++)
3020 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
3021 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
3022 } else
3023 len += sprintf(p + len, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003024
3025 return len;
3026}
3027
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003028static struct ibm_struct thermal_driver_data = {
3029 .name = "thermal",
3030 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003031 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003032};
3033
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003034/*************************************************************************
3035 * EC Dump subdriver
3036 */
3037
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003038static u8 ecdump_regs[256];
3039
3040static int ecdump_read(char *p)
3041{
3042 int len = 0;
3043 int i, j;
3044 u8 v;
3045
3046 len += sprintf(p + len, "EC "
3047 " +00 +01 +02 +03 +04 +05 +06 +07"
3048 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
3049 for (i = 0; i < 256; i += 16) {
3050 len += sprintf(p + len, "EC 0x%02x:", i);
3051 for (j = 0; j < 16; j++) {
3052 if (!acpi_ec_read(i + j, &v))
3053 break;
3054 if (v != ecdump_regs[i + j])
3055 len += sprintf(p + len, " *%02x", v);
3056 else
3057 len += sprintf(p + len, " %02x", v);
3058 ecdump_regs[i + j] = v;
3059 }
3060 len += sprintf(p + len, "\n");
3061 if (j != 16)
3062 break;
3063 }
3064
3065 /* These are way too dangerous to advertise openly... */
3066#if 0
3067 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
3068 " (<offset> is 00-ff, <value> is 00-ff)\n");
3069 len += sprintf(p + len, "commands:\t0x<offset> <value> "
3070 " (<offset> is 00-ff, <value> is 0-255)\n");
3071#endif
3072 return len;
3073}
3074
3075static int ecdump_write(char *buf)
3076{
3077 char *cmd;
3078 int i, v;
3079
3080 while ((cmd = next_cmd(&buf))) {
3081 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
3082 /* i and v set */
3083 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
3084 /* i and v set */
3085 } else
3086 return -EINVAL;
3087 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
3088 if (!acpi_ec_write(i, v))
3089 return -EIO;
3090 } else
3091 return -EINVAL;
3092 }
3093
3094 return 0;
3095}
3096
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003097static struct ibm_struct ecdump_driver_data = {
3098 .name = "ecdump",
3099 .read = ecdump_read,
3100 .write = ecdump_write,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03003101 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003102};
3103
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003104/*************************************************************************
3105 * Backlight/brightness subdriver
3106 */
Holger Macht8acb0252006-10-20 14:30:28 -07003107
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03003108static struct backlight_device *ibm_backlight_device;
Holger Macht8acb0252006-10-20 14:30:28 -07003109
Richard Purdie599a52d2007-02-10 23:07:48 +00003110static struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02003111 .get_brightness = brightness_get,
3112 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02003113};
3114
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03003115static struct mutex brightness_mutex;
3116
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003117static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02003118{
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02003119 int b;
3120
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003121 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
3122
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03003123 mutex_init(&brightness_mutex);
3124
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03003125 if (!brightness_mode) {
3126 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
3127 brightness_mode = 2;
3128 else
3129 brightness_mode = 3;
3130
3131 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
3132 brightness_mode);
3133 }
3134
3135 if (brightness_mode > 3)
3136 return -EINVAL;
3137
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02003138 b = brightness_get(NULL);
3139 if (b < 0)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03003140 return 1;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02003141
Henrique de Moraes Holschuh7d5a0152007-04-24 11:48:20 -03003142 ibm_backlight_device = backlight_device_register(
3143 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
3144 &ibm_backlight_data);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02003145 if (IS_ERR(ibm_backlight_device)) {
3146 printk(IBM_ERR "Could not register backlight device\n");
3147 return PTR_ERR(ibm_backlight_device);
3148 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003149 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02003150
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02003151 ibm_backlight_device->props.max_brightness = 7;
3152 ibm_backlight_device->props.brightness = b;
3153 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00003154
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02003155 return 0;
3156}
3157
3158static void brightness_exit(void)
3159{
3160 if (ibm_backlight_device) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003161 vdbg_printk(TPACPI_DBG_EXIT,
3162 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02003163 backlight_device_unregister(ibm_backlight_device);
3164 ibm_backlight_device = NULL;
3165 }
3166}
3167
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003168static int brightness_update_status(struct backlight_device *bd)
3169{
3170 return brightness_set(
3171 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
3172 bd->props.power == FB_BLANK_UNBLANK) ?
3173 bd->props.brightness : 0);
3174}
3175
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03003176/*
3177 * ThinkPads can read brightness from two places: EC 0x31, or
3178 * CMOS NVRAM byte 0x5E, bits 0-3.
3179 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003180static int brightness_get(struct backlight_device *bd)
3181{
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03003182 u8 lec = 0, lcmos = 0, level = 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003183
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03003184 if (brightness_mode & 1) {
3185 if (!acpi_ec_read(brightness_offset, &lec))
3186 return -EIO;
3187 lec &= 7;
3188 level = lec;
3189 };
3190 if (brightness_mode & 2) {
3191 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
3192 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
3193 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
3194 level = lcmos;
3195 }
3196
3197 if (brightness_mode == 3 && lec != lcmos) {
3198 printk(IBM_ERR
3199 "CMOS NVRAM (%u) and EC (%u) do not agree "
3200 "on display brightness level\n",
3201 (unsigned int) lcmos,
3202 (unsigned int) lec);
3203 return -EIO;
3204 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003205
3206 return level;
3207}
3208
3209static int brightness_set(int value)
3210{
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03003211 int cmos_cmd, inc, i, res;
3212 int current_value;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003213
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03003214 if (value > 7)
3215 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003216
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03003217 res = mutex_lock_interruptible(&brightness_mutex);
3218 if (res < 0)
3219 return res;
3220
3221 current_value = brightness_get(NULL);
3222 if (current_value < 0) {
3223 res = current_value;
3224 goto errout;
3225 }
3226
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03003227 cmos_cmd = value > current_value ?
3228 TP_CMOS_BRIGHTNESS_UP :
3229 TP_CMOS_BRIGHTNESS_DOWN;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003230 inc = value > current_value ? 1 : -1;
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03003231
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03003232 res = 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003233 for (i = current_value; i != value; i += inc) {
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03003234 if ((brightness_mode & 2) &&
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03003235 issue_thinkpad_cmos_command(cmos_cmd)) {
3236 res = -EIO;
3237 goto errout;
3238 }
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03003239 if ((brightness_mode & 1) &&
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03003240 !acpi_ec_write(brightness_offset, i + inc)) {
3241 res = -EIO;
3242 goto errout;;
3243 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003244 }
3245
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03003246errout:
3247 mutex_unlock(&brightness_mutex);
3248 return res;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003249}
3250
3251static int brightness_read(char *p)
3252{
3253 int len = 0;
3254 int level;
3255
3256 if ((level = brightness_get(NULL)) < 0) {
3257 len += sprintf(p + len, "level:\t\tunreadable\n");
3258 } else {
3259 len += sprintf(p + len, "level:\t\t%d\n", level & 0x7);
3260 len += sprintf(p + len, "commands:\tup, down\n");
3261 len += sprintf(p + len, "commands:\tlevel <level>"
3262 " (<level> is 0-7)\n");
3263 }
3264
3265 return len;
3266}
3267
3268static int brightness_write(char *buf)
3269{
3270 int level;
3271 int new_level;
3272 char *cmd;
3273
3274 while ((cmd = next_cmd(&buf))) {
3275 if ((level = brightness_get(NULL)) < 0)
3276 return level;
3277 level &= 7;
3278
3279 if (strlencmp(cmd, "up") == 0) {
3280 new_level = level == 7 ? 7 : level + 1;
3281 } else if (strlencmp(cmd, "down") == 0) {
3282 new_level = level == 0 ? 0 : level - 1;
3283 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
3284 new_level >= 0 && new_level <= 7) {
3285 /* new_level set */
3286 } else
3287 return -EINVAL;
3288
3289 brightness_set(new_level);
3290 }
3291
3292 return 0;
3293}
3294
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003295static struct ibm_struct brightness_driver_data = {
3296 .name = "brightness",
3297 .read = brightness_read,
3298 .write = brightness_write,
3299 .exit = brightness_exit,
3300};
3301
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003302/*************************************************************************
3303 * Volume subdriver
3304 */
3305
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003306static int volume_read(char *p)
3307{
3308 int len = 0;
3309 u8 level;
3310
3311 if (!acpi_ec_read(volume_offset, &level)) {
3312 len += sprintf(p + len, "level:\t\tunreadable\n");
3313 } else {
3314 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
3315 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
3316 len += sprintf(p + len, "commands:\tup, down, mute\n");
3317 len += sprintf(p + len, "commands:\tlevel <level>"
3318 " (<level> is 0-15)\n");
3319 }
3320
3321 return len;
3322}
3323
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003324static int volume_write(char *buf)
3325{
3326 int cmos_cmd, inc, i;
3327 u8 level, mute;
3328 int new_level, new_mute;
3329 char *cmd;
3330
3331 while ((cmd = next_cmd(&buf))) {
3332 if (!acpi_ec_read(volume_offset, &level))
3333 return -EIO;
3334 new_mute = mute = level & 0x40;
3335 new_level = level = level & 0xf;
3336
3337 if (strlencmp(cmd, "up") == 0) {
3338 if (mute)
3339 new_mute = 0;
3340 else
3341 new_level = level == 15 ? 15 : level + 1;
3342 } else if (strlencmp(cmd, "down") == 0) {
3343 if (mute)
3344 new_mute = 0;
3345 else
3346 new_level = level == 0 ? 0 : level - 1;
3347 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
3348 new_level >= 0 && new_level <= 15) {
3349 /* new_level set */
3350 } else if (strlencmp(cmd, "mute") == 0) {
3351 new_mute = 0x40;
3352 } else
3353 return -EINVAL;
3354
3355 if (new_level != level) { /* mute doesn't change */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003356 cmos_cmd = new_level > level ? TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003357 inc = new_level > level ? 1 : -1;
3358
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03003359 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003360 !acpi_ec_write(volume_offset, level)))
3361 return -EIO;
3362
3363 for (i = level; i != new_level; i += inc)
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03003364 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003365 !acpi_ec_write(volume_offset, i + inc))
3366 return -EIO;
3367
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03003368 if (mute && (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003369 !acpi_ec_write(volume_offset,
3370 new_level + mute)))
3371 return -EIO;
3372 }
3373
3374 if (new_mute != mute) { /* level doesn't change */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003375 cmos_cmd = new_mute ? TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003376
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03003377 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003378 !acpi_ec_write(volume_offset, level + new_mute))
3379 return -EIO;
3380 }
3381 }
3382
3383 return 0;
3384}
3385
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003386static struct ibm_struct volume_driver_data = {
3387 .name = "volume",
3388 .read = volume_read,
3389 .write = volume_write,
3390};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003391
3392/*************************************************************************
3393 * Fan subdriver
3394 */
3395
3396/*
3397 * FAN ACCESS MODES
3398 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003399 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003400 * ACPI GFAN method: returns fan level
3401 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003402 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03003403 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003404 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003405 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003406 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
3407 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03003408 * EC 0x2f (HFSP) might be available *for reading*, but do not use
3409 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003410 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003411 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03003412 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
3413 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003414 *
3415 * Fan speed changes of any sort (including those caused by the
3416 * disengaged mode) are usually done slowly by the firmware as the
3417 * maximum ammount of fan duty cycle change per second seems to be
3418 * limited.
3419 *
3420 * Reading is not available if GFAN exists.
3421 * Writing is not available if SFAN exists.
3422 *
3423 * Bits
3424 * 7 automatic mode engaged;
3425 * (default operation mode of the ThinkPad)
3426 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03003427 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003428 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03003429 * the tachometer while the fan controller ramps up
3430 * the speed (which can take up to a few *minutes*).
3431 * Speeds up fan to 100% duty-cycle, which is far above
3432 * the standard RPM levels. It is not impossible that
3433 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003434 * 5-3 unused in some models. Extra bits for fan level
3435 * in others, but still useless as all values above
3436 * 7 map to the same speed as level 7 in these models.
3437 * 2-0 fan level (0..7 usually)
3438 * 0x00 = stop
3439 * 0x07 = max (set when temperatures critical)
3440 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003441 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003442 *
3443 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
3444 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
3445 * does so, its initial value is meaningless (0x07).
3446 *
3447 * For firmware bugs, refer to:
3448 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
3449 *
3450 * ----
3451 *
3452 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
3453 * Main fan tachometer reading (in RPM)
3454 *
3455 * This register is present on all ThinkPads with a new-style EC, and
3456 * it is known not to be present on the A21m/e, and T22, as there is
3457 * something else in offset 0x84 according to the ACPI DSDT. Other
3458 * ThinkPads from this same time period (and earlier) probably lack the
3459 * tachometer as well.
3460 *
3461 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
3462 * was never fixed by IBM to report the EC firmware version string
3463 * probably support the tachometer (like the early X models), so
3464 * detecting it is quite hard. We need more data to know for sure.
3465 *
3466 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
3467 * might result.
3468 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03003469 * FIRMWARE BUG: may go stale while the EC is switching to full speed
3470 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003471 *
3472 * For firmware bugs, refer to:
3473 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
3474 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003475 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003476 * ThinkPad X31, X40, X41. Not available in the X60.
3477 *
3478 * FANS ACPI handle: takes three arguments: low speed, medium speed,
3479 * high speed. ACPI DSDT seems to map these three speeds to levels
3480 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
3481 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
3482 *
3483 * The speeds are stored on handles
3484 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
3485 *
3486 * There are three default speed sets, acessible as handles:
3487 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
3488 *
3489 * ACPI DSDT switches which set is in use depending on various
3490 * factors.
3491 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003492 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003493 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
3494 * but the ACPI tables just mention level 7.
3495 */
3496
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003497static enum fan_status_access_mode fan_status_access_mode;
3498static enum fan_control_access_mode fan_control_access_mode;
3499static enum fan_control_commands fan_control_commands;
3500
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02003501static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003502static u8 fan_control_desired_level;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02003503
Len Brown25c68a32006-12-08 04:43:41 -05003504static void fan_watchdog_fire(struct work_struct *ignored);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003505static int fan_watchdog_maxinterval;
Len Brown25c68a32006-12-08 04:43:41 -05003506static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003507
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003508IBM_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
3509IBM_HANDLE(gfan, ec, "GFAN", /* 570 */
3510 "\\FSPD", /* 600e/x, 770e, 770x */
3511 ); /* all others */
3512IBM_HANDLE(sfan, ec, "SFAN", /* 570 */
3513 "JFNS", /* 770x-JL */
3514 ); /* all others */
3515
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003516/*
3517 * SYSFS fan layout: hwmon compatible (device)
3518 *
3519 * pwm*_enable:
3520 * 0: "disengaged" mode
3521 * 1: manual mode
3522 * 2: native EC "auto" mode (recommended, hardware default)
3523 *
3524 * pwm*: set speed in manual mode, ignored otherwise.
3525 * 0 is level 0; 255 is level 7. Intermediate points done with linear
3526 * interpolation.
3527 *
3528 * fan*_input: tachometer reading, RPM
3529 *
3530 *
3531 * SYSFS fan layout: extensions
3532 *
3533 * fan_watchdog (driver):
3534 * fan watchdog interval in seconds, 0 disables (default), max 120
3535 */
3536
3537/* sysfs fan pwm1_enable ----------------------------------------------- */
3538static ssize_t fan_pwm1_enable_show(struct device *dev,
3539 struct device_attribute *attr,
3540 char *buf)
3541{
3542 int res, mode;
3543 u8 status;
3544
3545 res = fan_get_status_safe(&status);
3546 if (res)
3547 return res;
3548
3549 if (unlikely(tp_features.fan_ctrl_status_undef)) {
3550 if (status != fan_control_initial_status) {
3551 tp_features.fan_ctrl_status_undef = 0;
3552 } else {
3553 /* Return most likely status. In fact, it
3554 * might be the only possible status */
3555 status = TP_EC_FAN_AUTO;
3556 }
3557 }
3558
3559 if (status & TP_EC_FAN_FULLSPEED) {
3560 mode = 0;
3561 } else if (status & TP_EC_FAN_AUTO) {
3562 mode = 2;
3563 } else
3564 mode = 1;
3565
3566 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
3567}
3568
3569static ssize_t fan_pwm1_enable_store(struct device *dev,
3570 struct device_attribute *attr,
3571 const char *buf, size_t count)
3572{
3573 unsigned long t;
3574 int res, level;
3575
3576 if (parse_strtoul(buf, 2, &t))
3577 return -EINVAL;
3578
3579 switch (t) {
3580 case 0:
3581 level = TP_EC_FAN_FULLSPEED;
3582 break;
3583 case 1:
3584 level = TPACPI_FAN_LAST_LEVEL;
3585 break;
3586 case 2:
3587 level = TP_EC_FAN_AUTO;
3588 break;
3589 case 3:
3590 /* reserved for software-controlled auto mode */
3591 return -ENOSYS;
3592 default:
3593 return -EINVAL;
3594 }
3595
3596 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03003597 if (res == -ENXIO)
3598 return -EINVAL;
3599 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003600 return res;
3601
3602 fan_watchdog_reset();
3603
3604 return count;
3605}
3606
3607static struct device_attribute dev_attr_fan_pwm1_enable =
3608 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
3609 fan_pwm1_enable_show, fan_pwm1_enable_store);
3610
3611/* sysfs fan pwm1 ------------------------------------------------------ */
3612static ssize_t fan_pwm1_show(struct device *dev,
3613 struct device_attribute *attr,
3614 char *buf)
3615{
3616 int res;
3617 u8 status;
3618
3619 res = fan_get_status_safe(&status);
3620 if (res)
3621 return res;
3622
3623 if (unlikely(tp_features.fan_ctrl_status_undef)) {
3624 if (status != fan_control_initial_status) {
3625 tp_features.fan_ctrl_status_undef = 0;
3626 } else {
3627 status = TP_EC_FAN_AUTO;
3628 }
3629 }
3630
3631 if ((status &
3632 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
3633 status = fan_control_desired_level;
3634
3635 if (status > 7)
3636 status = 7;
3637
3638 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
3639}
3640
3641static ssize_t fan_pwm1_store(struct device *dev,
3642 struct device_attribute *attr,
3643 const char *buf, size_t count)
3644{
3645 unsigned long s;
3646 int rc;
3647 u8 status, newlevel;
3648
3649 if (parse_strtoul(buf, 255, &s))
3650 return -EINVAL;
3651
3652 /* scale down from 0-255 to 0-7 */
3653 newlevel = (s >> 5) & 0x07;
3654
3655 rc = mutex_lock_interruptible(&fan_mutex);
3656 if (rc < 0)
3657 return rc;
3658
3659 rc = fan_get_status(&status);
3660 if (!rc && (status &
3661 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
3662 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03003663 if (rc == -ENXIO)
3664 rc = -EINVAL;
3665 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003666 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03003667 fan_watchdog_reset();
3668 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003669 }
3670
3671 mutex_unlock(&fan_mutex);
3672 return (rc)? rc : count;
3673}
3674
3675static struct device_attribute dev_attr_fan_pwm1 =
3676 __ATTR(pwm1, S_IWUSR | S_IRUGO,
3677 fan_pwm1_show, fan_pwm1_store);
3678
3679/* sysfs fan fan1_input ------------------------------------------------ */
3680static ssize_t fan_fan1_input_show(struct device *dev,
3681 struct device_attribute *attr,
3682 char *buf)
3683{
3684 int res;
3685 unsigned int speed;
3686
3687 res = fan_get_speed(&speed);
3688 if (res < 0)
3689 return res;
3690
3691 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
3692}
3693
3694static struct device_attribute dev_attr_fan_fan1_input =
3695 __ATTR(fan1_input, S_IRUGO,
3696 fan_fan1_input_show, NULL);
3697
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03003698/* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003699static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
3700 char *buf)
3701{
3702 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
3703}
3704
3705static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
3706 const char *buf, size_t count)
3707{
3708 unsigned long t;
3709
3710 if (parse_strtoul(buf, 120, &t))
3711 return -EINVAL;
3712
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03003713 if (!fan_control_allowed)
3714 return -EPERM;
3715
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003716 fan_watchdog_maxinterval = t;
3717 fan_watchdog_reset();
3718
3719 return count;
3720}
3721
3722static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
3723 fan_fan_watchdog_show, fan_fan_watchdog_store);
3724
3725/* --------------------------------------------------------------------- */
3726static struct attribute *fan_attributes[] = {
3727 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
3728 &dev_attr_fan_fan1_input.attr,
3729 NULL
3730};
3731
3732static const struct attribute_group fan_attr_group = {
3733 .attrs = fan_attributes,
3734};
3735
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003736static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003737{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003738 int rc;
3739
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003740 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
3741
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003742 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003743 fan_status_access_mode = TPACPI_FAN_NONE;
3744 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003745 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003746 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003747 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003748 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003749
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003750 IBM_ACPIHANDLE_INIT(fans);
3751 IBM_ACPIHANDLE_INIT(gfan);
3752 IBM_ACPIHANDLE_INIT(sfan);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003753
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003754 if (gfan_handle) {
3755 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003756 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003757 } else {
3758 /* all other ThinkPads: note that even old-style
3759 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02003760 if (likely(acpi_ec_read(fan_status_offset,
3761 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003762 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02003763
3764 /* In some ThinkPads, neither the EC nor the ACPI
3765 * DSDT initialize the fan status, and it ends up
3766 * being set to 0x07 when it *could* be either
3767 * 0x07 or 0x80.
3768 *
3769 * Enable for TP-1Y (T43), TP-78 (R51e),
3770 * TP-76 (R52), TP-70 (T43, R52), which are known
3771 * to be buggy. */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03003772 if (fan_control_initial_status == 0x07) {
3773 switch (thinkpad_id.ec_model) {
3774 case 0x5931: /* TP-1Y */
3775 case 0x3837: /* TP-78 */
3776 case 0x3637: /* TP-76 */
3777 case 0x3037: /* TP-70 */
3778 printk(IBM_NOTICE
3779 "fan_init: initial fan status is "
3780 "unknown, assuming it is in auto "
3781 "mode\n");
3782 tp_features.fan_ctrl_status_undef = 1;
3783 ;;
3784 }
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02003785 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003786 } else {
3787 printk(IBM_ERR
3788 "ThinkPad ACPI EC access misbehaving, "
3789 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003790 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003791 }
3792 }
3793
3794 if (sfan_handle) {
3795 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003796 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02003797 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003798 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003799 } else {
3800 if (!gfan_handle) {
3801 /* gfan without sfan means no fan control */
3802 /* all other models implement TP EC 0x2f control */
3803
3804 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02003805 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003806 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003807 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003808 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003809 TPACPI_FAN_CMD_SPEED |
3810 TPACPI_FAN_CMD_LEVEL |
3811 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003812 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003813 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02003814 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003815 TPACPI_FAN_CMD_LEVEL |
3816 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003817 }
3818 }
3819 }
3820
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003821 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
3822 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
3823 fan_control_access_mode != TPACPI_FAN_WR_NONE),
3824 fan_status_access_mode, fan_control_access_mode);
3825
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03003826 /* fan control master switch */
3827 if (!fan_control_allowed) {
3828 fan_control_access_mode = TPACPI_FAN_WR_NONE;
3829 fan_control_commands = 0;
3830 dbg_printk(TPACPI_DBG_INIT,
3831 "fan control features disabled by parameter\n");
3832 }
3833
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003834 /* update fan_control_desired_level */
3835 if (fan_status_access_mode != TPACPI_FAN_NONE)
3836 fan_get_status_safe(NULL);
3837
3838 if (fan_status_access_mode != TPACPI_FAN_NONE ||
3839 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03003840 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003841 &fan_attr_group);
3842 if (!(rc < 0))
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03003843 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003844 &driver_attr_fan_watchdog);
3845 if (rc < 0)
3846 return rc;
3847 return 0;
3848 } else
3849 return 1;
3850}
3851
3852/*
3853 * Call with fan_mutex held
3854 */
3855static void fan_update_desired_level(u8 status)
3856{
3857 if ((status &
3858 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
3859 if (status > 7)
3860 fan_control_desired_level = 7;
3861 else
3862 fan_control_desired_level = status;
3863 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003864}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003865
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02003866static int fan_get_status(u8 *status)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003867{
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02003868 u8 s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003869
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02003870 /* TODO:
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003871 * Add TPACPI_FAN_RD_ACPI_FANS ? */
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02003872
Henrique de Moraes Holschuh3ef8a602006-11-24 11:47:10 -02003873 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003874 case TPACPI_FAN_RD_ACPI_GFAN:
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003875 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02003876
3877 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003878 return -EIO;
3879
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02003880 if (likely(status))
3881 *status = s & 0x07;
3882
3883 break;
3884
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003885 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02003886 /* all except 570, 600e/x, 770e, 770x */
3887 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
3888 return -EIO;
3889
3890 if (likely(status))
3891 *status = s;
3892
3893 break;
3894
3895 default:
3896 return -ENXIO;
3897 }
3898
3899 return 0;
3900}
3901
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003902static int fan_get_status_safe(u8 *status)
3903{
3904 int rc;
3905 u8 s;
3906
3907 rc = mutex_lock_interruptible(&fan_mutex);
3908 if (rc < 0)
3909 return rc;
3910 rc = fan_get_status(&s);
3911 if (!rc)
3912 fan_update_desired_level(s);
3913 mutex_unlock(&fan_mutex);
3914
3915 if (status)
3916 *status = s;
3917
3918 return rc;
3919}
3920
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003921static void fan_exit(void)
3922{
Henrique de Moraes Holschuh99fba3f2007-04-21 11:08:44 -03003923 vdbg_printk(TPACPI_DBG_EXIT, "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003924
3925 /* FIXME: can we really do this unconditionally? */
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03003926 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
3927 driver_remove_file(&tpacpi_hwmon_pdriver.driver, &driver_attr_fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003928
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003929 cancel_delayed_work(&fan_watchdog_task);
3930 flush_scheduled_work();
3931}
3932
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02003933static int fan_get_speed(unsigned int *speed)
3934{
3935 u8 hi, lo;
3936
3937 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003938 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02003939 /* all except 570, 600e/x, 770e, 770x */
3940 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
3941 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
3942 return -EIO;
3943
3944 if (likely(speed))
3945 *speed = (hi << 8) | lo;
3946
3947 break;
3948
3949 default:
3950 return -ENXIO;
3951 }
3952
3953 return 0;
3954}
3955
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003956static void fan_watchdog_fire(struct work_struct *ignored)
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003957{
Henrique de Moraes Holschuh99fba3f2007-04-21 11:08:44 -03003958 int rc;
3959
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03003960 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
3961 return;
3962
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003963 printk(IBM_NOTICE "fan watchdog: enabling fan\n");
Henrique de Moraes Holschuh99fba3f2007-04-21 11:08:44 -03003964 rc = fan_set_enable();
3965 if (rc < 0) {
3966 printk(IBM_ERR "fan watchdog: error %d while enabling fan, "
3967 "will try again later...\n", -rc);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003968 /* reschedule for later */
3969 fan_watchdog_reset();
3970 }
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003971}
3972
3973static void fan_watchdog_reset(void)
3974{
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03003975 static int fan_watchdog_active;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003976
Henrique de Moraes Holschuh4985cd02007-04-27 22:00:10 -03003977 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
3978 return;
3979
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003980 if (fan_watchdog_active)
3981 cancel_delayed_work(&fan_watchdog_task);
3982
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03003983 if (fan_watchdog_maxinterval > 0 &&
3984 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003985 fan_watchdog_active = 1;
3986 if (!schedule_delayed_work(&fan_watchdog_task,
3987 msecs_to_jiffies(fan_watchdog_maxinterval
3988 * 1000))) {
3989 printk(IBM_ERR "failed to schedule the fan watchdog, "
3990 "watchdog will not trigger\n");
3991 }
3992 } else
3993 fan_watchdog_active = 0;
3994}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003995
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003996static int fan_set_level(int level)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003997{
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03003998 if (!fan_control_allowed)
3999 return -EPERM;
4000
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004001 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004002 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004003 if (level >= 0 && level <= 7) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004004 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004005 return -EIO;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004006 } else
4007 return -EINVAL;
4008 break;
4009
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004010 case TPACPI_FAN_WR_ACPI_FANS:
4011 case TPACPI_FAN_WR_TPEC:
4012 if ((level != TP_EC_FAN_AUTO) &&
4013 (level != TP_EC_FAN_FULLSPEED) &&
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02004014 ((level < 0) || (level > 7)))
4015 return -EINVAL;
4016
Henrique de Moraes Holschuheaa75712007-04-24 11:48:18 -03004017 /* safety net should the EC not support AUTO
4018 * or FULLSPEED mode bits and just ignore them */
4019 if (level & TP_EC_FAN_FULLSPEED)
4020 level |= 7; /* safety min speed 7 */
4021 else if (level & TP_EC_FAN_FULLSPEED)
4022 level |= 4; /* safety min speed 4 */
4023
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004024 if (!acpi_ec_write(fan_status_offset, level))
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02004025 return -EIO;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02004026 else
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004027 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02004028 break;
4029
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004030 default:
4031 return -ENXIO;
4032 }
4033 return 0;
4034}
4035
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004036static int fan_set_level_safe(int level)
4037{
4038 int rc;
4039
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03004040 if (!fan_control_allowed)
4041 return -EPERM;
4042
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004043 rc = mutex_lock_interruptible(&fan_mutex);
4044 if (rc < 0)
4045 return rc;
4046
4047 if (level == TPACPI_FAN_LAST_LEVEL)
4048 level = fan_control_desired_level;
4049
4050 rc = fan_set_level(level);
4051 if (!rc)
4052 fan_update_desired_level(level);
4053
4054 mutex_unlock(&fan_mutex);
4055 return rc;
4056}
4057
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004058static int fan_set_enable(void)
4059{
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02004060 u8 s;
4061 int rc;
4062
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03004063 if (!fan_control_allowed)
4064 return -EPERM;
4065
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004066 rc = mutex_lock_interruptible(&fan_mutex);
4067 if (rc < 0)
4068 return rc;
4069
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004070 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004071 case TPACPI_FAN_WR_ACPI_FANS:
4072 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004073 rc = fan_get_status(&s);
4074 if (rc < 0)
4075 break;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02004076
4077 /* Don't go out of emergency fan mode */
Henrique de Moraes Holschuheaa75712007-04-24 11:48:18 -03004078 if (s != 7) {
4079 s &= 0x07;
4080 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
4081 }
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02004082
4083 if (!acpi_ec_write(fan_status_offset, s))
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004084 rc = -EIO;
4085 else {
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004086 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004087 rc = 0;
4088 }
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02004089 break;
4090
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004091 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004092 rc = fan_get_status(&s);
4093 if (rc < 0)
4094 break;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02004095
4096 s &= 0x07;
4097
4098 /* Set fan to at least level 4 */
Henrique de Moraes Holschuheaa75712007-04-24 11:48:18 -03004099 s |= 4;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02004100
4101 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004102 rc= -EIO;
4103 else
4104 rc = 0;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004105 break;
4106
4107 default:
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004108 rc = -ENXIO;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004109 }
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004110
4111 mutex_unlock(&fan_mutex);
4112 return rc;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004113}
4114
4115static int fan_set_disable(void)
4116{
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004117 int rc;
4118
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03004119 if (!fan_control_allowed)
4120 return -EPERM;
4121
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004122 rc = mutex_lock_interruptible(&fan_mutex);
4123 if (rc < 0)
4124 return rc;
4125
4126 rc = 0;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004127 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004128 case TPACPI_FAN_WR_ACPI_FANS:
4129 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004130 if (!acpi_ec_write(fan_status_offset, 0x00))
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004131 rc = -EIO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004132 else {
4133 fan_control_desired_level = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004134 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004135 }
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004136 break;
4137
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004138 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02004139 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004140 rc = -EIO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004141 else
4142 fan_control_desired_level = 0;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02004143 break;
4144
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004145 default:
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004146 rc = -ENXIO;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004147 }
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004148
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004149
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004150 mutex_unlock(&fan_mutex);
4151 return rc;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004152}
4153
4154static int fan_set_speed(int speed)
4155{
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004156 int rc;
4157
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03004158 if (!fan_control_allowed)
4159 return -EPERM;
4160
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004161 rc = mutex_lock_interruptible(&fan_mutex);
4162 if (rc < 0)
4163 return rc;
4164
4165 rc = 0;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004166 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004167 case TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004168 if (speed >= 0 && speed <= 65535) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004169 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
4170 speed, speed, speed))
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004171 rc = -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004172 } else
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004173 rc = -EINVAL;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004174 break;
4175
4176 default:
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004177 rc = -ENXIO;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004178 }
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004179
4180 mutex_unlock(&fan_mutex);
4181 return rc;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004182}
4183
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004184static int fan_read(char *p)
4185{
4186 int len = 0;
4187 int rc;
4188 u8 status;
4189 unsigned int speed = 0;
4190
4191 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004192 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004193 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004194 if ((rc = fan_get_status_safe(&status)) < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004195 return rc;
4196
4197 len += sprintf(p + len, "status:\t\t%s\n"
4198 "level:\t\t%d\n",
4199 (status != 0) ? "enabled" : "disabled", status);
4200 break;
4201
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004202 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004203 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004204 if ((rc = fan_get_status_safe(&status)) < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004205 return rc;
4206
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004207 if (unlikely(tp_features.fan_ctrl_status_undef)) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004208 if (status != fan_control_initial_status)
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004209 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004210 else
4211 /* Return most likely status. In fact, it
4212 * might be the only possible status */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004213 status = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004214 }
4215
4216 len += sprintf(p + len, "status:\t\t%s\n",
4217 (status != 0) ? "enabled" : "disabled");
4218
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004219 if ((rc = fan_get_speed(&speed)) < 0)
4220 return rc;
4221
4222 len += sprintf(p + len, "speed:\t\t%d\n", speed);
4223
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004224 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004225 /* Disengaged mode takes precedence */
4226 len += sprintf(p + len, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004227 else if (status & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004228 len += sprintf(p + len, "level:\t\tauto\n");
4229 else
4230 len += sprintf(p + len, "level:\t\t%d\n", status);
4231 break;
4232
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004233 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004234 default:
4235 len += sprintf(p + len, "status:\t\tnot supported\n");
4236 }
4237
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004238 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004239 len += sprintf(p + len, "commands:\tlevel <level>");
4240
4241 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004242 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004243 len += sprintf(p + len, " (<level> is 0-7)\n");
4244 break;
4245
4246 default:
4247 len += sprintf(p + len, " (<level> is 0-7, "
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004248 "auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004249 break;
4250 }
4251 }
4252
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004253 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004254 len += sprintf(p + len, "commands:\tenable, disable\n"
4255 "commands:\twatchdog <timeout> (<timeout> is 0 (off), "
4256 "1-120 (seconds))\n");
4257
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004258 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004259 len += sprintf(p + len, "commands:\tspeed <speed>"
4260 " (<speed> is 0-65535)\n");
4261
4262 return len;
4263}
4264
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004265static int fan_write_cmd_level(const char *cmd, int *rc)
4266{
4267 int level;
4268
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02004269 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004270 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004271 else if ((strlencmp(cmd, "level disengaged") == 0) |
4272 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004273 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02004274 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004275 return 0;
4276
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004277 if ((*rc = fan_set_level_safe(level)) == -ENXIO)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004278 printk(IBM_ERR "level command accepted for unsupported "
4279 "access mode %d", fan_control_access_mode);
4280
4281 return 1;
4282}
4283
4284static int fan_write_cmd_enable(const char *cmd, int *rc)
4285{
4286 if (strlencmp(cmd, "enable") != 0)
4287 return 0;
4288
4289 if ((*rc = fan_set_enable()) == -ENXIO)
4290 printk(IBM_ERR "enable command accepted for unsupported "
4291 "access mode %d", fan_control_access_mode);
4292
4293 return 1;
4294}
4295
4296static int fan_write_cmd_disable(const char *cmd, int *rc)
4297{
4298 if (strlencmp(cmd, "disable") != 0)
4299 return 0;
4300
4301 if ((*rc = fan_set_disable()) == -ENXIO)
4302 printk(IBM_ERR "disable command accepted for unsupported "
4303 "access mode %d", fan_control_access_mode);
4304
4305 return 1;
4306}
4307
4308static int fan_write_cmd_speed(const char *cmd, int *rc)
4309{
4310 int speed;
4311
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02004312 /* TODO:
4313 * Support speed <low> <medium> <high> ? */
4314
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004315 if (sscanf(cmd, "speed %d", &speed) != 1)
4316 return 0;
4317
4318 if ((*rc = fan_set_speed(speed)) == -ENXIO)
4319 printk(IBM_ERR "speed command accepted for unsupported "
4320 "access mode %d", fan_control_access_mode);
4321
4322 return 1;
4323}
4324
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02004325static int fan_write_cmd_watchdog(const char *cmd, int *rc)
4326{
4327 int interval;
4328
4329 if (sscanf(cmd, "watchdog %d", &interval) != 1)
4330 return 0;
4331
4332 if (interval < 0 || interval > 120)
4333 *rc = -EINVAL;
4334 else
4335 fan_watchdog_maxinterval = interval;
4336
4337 return 1;
4338}
4339
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004340static int fan_write(char *buf)
4341{
4342 char *cmd;
4343 int rc = 0;
4344
4345 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004346 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004347 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004348 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004349 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02004350 fan_write_cmd_disable(cmd, &rc) ||
4351 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004352 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004353 fan_write_cmd_speed(cmd, &rc))
4354 )
4355 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02004356 else if (!rc)
4357 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004358 }
4359
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004360 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004361}
4362
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004363static struct ibm_struct fan_driver_data = {
4364 .name = "fan",
4365 .read = fan_read,
4366 .write = fan_write,
4367 .exit = fan_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004368};
4369
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004370/****************************************************************************
4371 ****************************************************************************
4372 *
4373 * Infrastructure
4374 *
4375 ****************************************************************************
4376 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03004378/* sysfs name ---------------------------------------------------------- */
4379static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
4380 struct device_attribute *attr,
4381 char *buf)
4382{
4383 return snprintf(buf, PAGE_SIZE, "%s\n", IBM_NAME);
4384}
4385
4386static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
4387 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
4388
4389/* --------------------------------------------------------------------- */
4390
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004391/* /proc support */
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03004392static struct proc_dir_entry *proc_dir;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004393
4394/* Subdriver registry */
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004395static LIST_HEAD(tpacpi_all_drivers);
4396
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004398/*
4399 * Module and infrastructure proble, init and exit handling
4400 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004402#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004403static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004404{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004405 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004406
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004407 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004408}
4409#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
4410
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004411static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412{
4413 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004414 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415 struct proc_dir_entry *entry;
4416
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004417 BUG_ON(ibm == NULL);
4418
4419 INIT_LIST_HEAD(&ibm->all_drivers);
4420
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004421 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422 return 0;
4423
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004424 dbg_printk(TPACPI_DBG_INIT,
4425 "probing for %s\n", ibm->name);
4426
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004427 if (iibm->init) {
4428 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004429 if (ret > 0)
4430 return 0; /* probe failed */
4431 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004433
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004434 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435 }
4436
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004437 if (ibm->acpi) {
4438 if (ibm->acpi->hid) {
4439 ret = register_tpacpi_subdriver(ibm);
4440 if (ret)
4441 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004442 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004443
4444 if (ibm->acpi->notify) {
4445 ret = setup_acpi_notify(ibm);
4446 if (ret == -ENODEV) {
4447 printk(IBM_NOTICE "disabling subdriver %s\n",
4448 ibm->name);
4449 ret = 0;
4450 goto err_out;
4451 }
4452 if (ret < 0)
4453 goto err_out;
4454 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004455 }
4456
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004457 dbg_printk(TPACPI_DBG_INIT,
4458 "%s installed\n", ibm->name);
4459
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004460 if (ibm->read) {
4461 entry = create_proc_entry(ibm->name,
4462 S_IFREG | S_IRUGO | S_IWUSR,
4463 proc_dir);
4464 if (!entry) {
4465 printk(IBM_ERR "unable to create proc entry %s\n",
4466 ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004467 ret = -ENODEV;
4468 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004469 }
4470 entry->owner = THIS_MODULE;
4471 entry->data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004472 entry->read_proc = &dispatch_procfs_read;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004473 if (ibm->write)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004474 entry->write_proc = &dispatch_procfs_write;
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004475 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004478 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
4479
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004481
4482err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004483 dbg_printk(TPACPI_DBG_INIT,
4484 "%s: at error exit path with result %d\n",
4485 ibm->name, ret);
4486
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004487 ibm_exit(ibm);
4488 return (ret < 0)? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489}
4490
4491static void ibm_exit(struct ibm_struct *ibm)
4492{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004493 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004494
4495 list_del_init(&ibm->all_drivers);
4496
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004497 if (ibm->flags.acpi_notify_installed) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004498 dbg_printk(TPACPI_DBG_EXIT,
4499 "%s: acpi_remove_notify_handler\n", ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004500 BUG_ON(!ibm->acpi);
4501 acpi_remove_notify_handler(*ibm->acpi->handle,
4502 ibm->acpi->type,
4503 dispatch_acpi_notify);
4504 ibm->flags.acpi_notify_installed = 0;
4505 ibm->flags.acpi_notify_installed = 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004506 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004508 if (ibm->flags.proc_created) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004509 dbg_printk(TPACPI_DBG_EXIT,
4510 "%s: remove_proc_entry\n", ibm->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511 remove_proc_entry(ibm->name, proc_dir);
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004512 ibm->flags.proc_created = 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004513 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004515 if (ibm->flags.acpi_driver_registered) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004516 dbg_printk(TPACPI_DBG_EXIT,
4517 "%s: acpi_bus_unregister_driver\n", ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004518 BUG_ON(!ibm->acpi);
4519 acpi_bus_unregister_driver(ibm->acpi->driver);
4520 kfree(ibm->acpi->driver);
4521 ibm->acpi->driver = NULL;
4522 ibm->flags.acpi_driver_registered = 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004523 }
4524
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004525 if (ibm->flags.init_called && ibm->exit) {
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004526 ibm->exit();
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004527 ibm->flags.init_called = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004529
4530 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531}
4532
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004533/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03004535static void __init get_thinkpad_model_data(struct thinkpad_id_data *tp)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004536{
Jeff Garzik18552562007-10-03 15:15:40 -04004537 const struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02004538 char ec_fw_string[18];
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004539
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03004540 if (!tp)
4541 return;
4542
4543 memset(tp, 0, sizeof(*tp));
4544
4545 if (dmi_name_in_vendors("IBM"))
4546 tp->vendor = PCI_VENDOR_ID_IBM;
4547 else if (dmi_name_in_vendors("LENOVO"))
4548 tp->vendor = PCI_VENDOR_ID_LENOVO;
4549 else
4550 return;
4551
4552 tp->bios_version_str = kstrdup(dmi_get_system_info(DMI_BIOS_VERSION),
4553 GFP_KERNEL);
4554 if (!tp->bios_version_str)
4555 return;
4556 tp->bios_model = tp->bios_version_str[0]
4557 | (tp->bios_version_str[1] << 8);
4558
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004559 /*
4560 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
4561 * X32 or newer, all Z series; Some models must have an
4562 * up-to-date BIOS or they will not be detected.
4563 *
4564 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
4565 */
4566 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02004567 if (sscanf(dev->name,
4568 "IBM ThinkPad Embedded Controller -[%17c",
4569 ec_fw_string) == 1) {
4570 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
4571 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03004572
4573 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
4574 tp->ec_model = ec_fw_string[0]
4575 | (ec_fw_string[1] << 8);
4576 break;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02004577 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004578 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03004579
4580 tp->model_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_VERSION),
4581 GFP_KERNEL);
4582 if (strnicmp(tp->model_str, "ThinkPad", 8) != 0) {
4583 kfree(tp->model_str);
4584 tp->model_str = NULL;
4585 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586}
4587
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004588static int __init probe_for_thinkpad(void)
4589{
4590 int is_thinkpad;
4591
4592 if (acpi_disabled)
4593 return -ENODEV;
4594
4595 /*
4596 * Non-ancient models have better DMI tagging, but very old models
4597 * don't.
4598 */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03004599 is_thinkpad = (thinkpad_id.model_str != NULL);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004600
4601 /* ec is required because many other handles are relative to it */
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004602 IBM_ACPIHANDLE_INIT(ec);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004603 if (!ec_handle) {
4604 if (is_thinkpad)
4605 printk(IBM_ERR
4606 "Not yet supported ThinkPad detected!\n");
4607 return -ENODEV;
4608 }
4609
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03004610 /*
4611 * Risks a regression on very old machines, but reduces potential
4612 * false positives a damn great deal
4613 */
4614 if (!is_thinkpad)
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03004615 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03004616
4617 if (!is_thinkpad && !force_load)
4618 return -ENODEV;
4619
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004620 return 0;
4621}
4622
4623
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004624/* Module init, exit, parameters */
4625
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004626static struct ibm_init_struct ibms_init[] __initdata = {
4627 {
4628 .init = thinkpad_acpi_driver_init,
4629 .data = &thinkpad_acpi_driver_data,
4630 },
4631 {
4632 .init = hotkey_init,
4633 .data = &hotkey_driver_data,
4634 },
4635 {
4636 .init = bluetooth_init,
4637 .data = &bluetooth_driver_data,
4638 },
4639 {
4640 .init = wan_init,
4641 .data = &wan_driver_data,
4642 },
4643 {
4644 .init = video_init,
4645 .data = &video_driver_data,
4646 },
4647 {
4648 .init = light_init,
4649 .data = &light_driver_data,
4650 },
4651#ifdef CONFIG_THINKPAD_ACPI_DOCK
4652 {
4653 .init = dock_init,
4654 .data = &dock_driver_data[0],
4655 },
4656 {
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03004657 .init = dock_init2,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004658 .data = &dock_driver_data[1],
4659 },
4660#endif
4661#ifdef CONFIG_THINKPAD_ACPI_BAY
4662 {
4663 .init = bay_init,
4664 .data = &bay_driver_data,
4665 },
4666#endif
4667 {
4668 .init = cmos_init,
4669 .data = &cmos_driver_data,
4670 },
4671 {
4672 .init = led_init,
4673 .data = &led_driver_data,
4674 },
4675 {
4676 .init = beep_init,
4677 .data = &beep_driver_data,
4678 },
4679 {
4680 .init = thermal_init,
4681 .data = &thermal_driver_data,
4682 },
4683 {
4684 .data = &ecdump_driver_data,
4685 },
4686 {
4687 .init = brightness_init,
4688 .data = &brightness_driver_data,
4689 },
4690 {
4691 .data = &volume_driver_data,
4692 },
4693 {
4694 .init = fan_init,
4695 .data = &fan_driver_data,
4696 },
4697};
4698
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004699static int __init set_ibm_param(const char *val, struct kernel_param *kp)
4700{
4701 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004702 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004703
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004704 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
4705 ibm = ibms_init[i].data;
4706 BUG_ON(ibm == NULL);
4707
4708 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
4709 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004710 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004711 strcpy(ibms_init[i].param, val);
4712 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004713 return 0;
4714 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004715 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004716
4717 return -EINVAL;
4718}
4719
4720static int experimental;
4721module_param(experimental, int, 0);
4722
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03004723static u32 dbg_level;
4724module_param_named(debug, dbg_level, uint, 0);
4725
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03004726static int force_load;
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03004727module_param(force_load, bool, 0);
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03004728
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03004729static int fan_control_allowed;
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03004730module_param_named(fan_control, fan_control_allowed, bool, 0);
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03004731
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03004732static int brightness_mode;
4733module_param_named(brightness_mode, brightness_mode, int, 0);
4734
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004735static unsigned int hotkey_report_mode;
4736module_param(hotkey_report_mode, uint, 0);
4737
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004738#define IBM_PARAM(feature) \
4739 module_param_call(feature, set_ibm_param, NULL, NULL, 0)
4740
4741IBM_PARAM(hotkey);
4742IBM_PARAM(bluetooth);
4743IBM_PARAM(video);
4744IBM_PARAM(light);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03004745#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004746IBM_PARAM(dock);
4747#endif
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03004748#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004749IBM_PARAM(bay);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03004750#endif /* CONFIG_THINKPAD_ACPI_BAY */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004751IBM_PARAM(cmos);
4752IBM_PARAM(led);
4753IBM_PARAM(beep);
4754IBM_PARAM(ecdump);
4755IBM_PARAM(brightness);
4756IBM_PARAM(volume);
4757IBM_PARAM(fan);
4758
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03004759static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760{
4761 int ret, i;
4762
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03004763 tpacpi_lifecycle = TPACPI_LIFE_INIT;
4764
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004765 /* Parameter checking */
4766 if (hotkey_report_mode > 2)
4767 return -EINVAL;
4768
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03004769 /* Driver-level probe */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03004770
4771 get_thinkpad_model_data(&thinkpad_id);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004772 ret = probe_for_thinkpad();
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03004773 if (ret) {
4774 thinkpad_acpi_module_exit();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004775 return ret;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03004776 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03004778 /* Driver initialization */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03004779
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004780 IBM_ACPIHANDLE_INIT(ecrd);
4781 IBM_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004783 proc_dir = proc_mkdir(IBM_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784 if (!proc_dir) {
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004785 printk(IBM_ERR "unable to create proc dir " IBM_PROC_DIR);
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03004786 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787 return -ENODEV;
4788 }
4789 proc_dir->owner = THIS_MODULE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004790
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03004791 ret = platform_driver_register(&tpacpi_pdriver);
4792 if (ret) {
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03004793 printk(IBM_ERR "unable to register main platform driver\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03004794 thinkpad_acpi_module_exit();
4795 return ret;
4796 }
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03004797 tp_features.platform_drv_registered = 1;
4798
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03004799 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
4800 if (ret) {
4801 printk(IBM_ERR "unable to register hwmon platform driver\n");
4802 thinkpad_acpi_module_exit();
4803 return ret;
4804 }
4805 tp_features.sensors_pdrv_registered = 1;
4806
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03004807 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03004808 if (!ret) {
4809 tp_features.platform_drv_attrs_registered = 1;
4810 ret = tpacpi_create_driver_attributes(&tpacpi_hwmon_pdriver.driver);
4811 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03004812 if (ret) {
4813 printk(IBM_ERR "unable to create sysfs driver attributes\n");
4814 thinkpad_acpi_module_exit();
4815 return ret;
4816 }
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03004817 tp_features.sensors_pdrv_attrs_registered = 1;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03004818
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03004819
4820 /* Device initialization */
4821 tpacpi_pdev = platform_device_register_simple(IBM_DRVR_NAME, -1,
4822 NULL, 0);
4823 if (IS_ERR(tpacpi_pdev)) {
4824 ret = PTR_ERR(tpacpi_pdev);
4825 tpacpi_pdev = NULL;
4826 printk(IBM_ERR "unable to register platform device\n");
4827 thinkpad_acpi_module_exit();
4828 return ret;
4829 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03004830 tpacpi_sensors_pdev = platform_device_register_simple(
4831 IBM_HWMON_DRVR_NAME,
4832 -1, NULL, 0);
4833 if (IS_ERR(tpacpi_sensors_pdev)) {
4834 ret = PTR_ERR(tpacpi_sensors_pdev);
4835 tpacpi_sensors_pdev = NULL;
4836 printk(IBM_ERR "unable to register hwmon platform device\n");
4837 thinkpad_acpi_module_exit();
4838 return ret;
4839 }
4840 ret = device_create_file(&tpacpi_sensors_pdev->dev,
4841 &dev_attr_thinkpad_acpi_pdev_name);
4842 if (ret) {
4843 printk(IBM_ERR
4844 "unable to create sysfs hwmon device attributes\n");
4845 thinkpad_acpi_module_exit();
4846 return ret;
4847 }
4848 tp_features.sensors_pdev_attrs_registered = 1;
4849 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03004850 if (IS_ERR(tpacpi_hwmon)) {
4851 ret = PTR_ERR(tpacpi_hwmon);
4852 tpacpi_hwmon = NULL;
4853 printk(IBM_ERR "unable to register hwmon device\n");
4854 thinkpad_acpi_module_exit();
4855 return ret;
4856 }
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03004857 mutex_init(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03004858 tpacpi_inputdev = input_allocate_device();
4859 if (!tpacpi_inputdev) {
4860 printk(IBM_ERR "unable to allocate input device\n");
4861 thinkpad_acpi_module_exit();
4862 return -ENOMEM;
4863 } else {
4864 /* Prepare input device, but don't register */
4865 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
4866 tpacpi_inputdev->phys = IBM_DRVR_NAME "/input0";
4867 tpacpi_inputdev->id.bustype = BUS_HOST;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03004868 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
4869 thinkpad_id.vendor :
4870 PCI_VENDOR_ID_IBM;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03004871 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
4872 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
4873 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004874 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
4875 ret = ibm_init(&ibms_init[i]);
4876 if (ret >= 0 && *ibms_init[i].param)
4877 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03004879 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880 return ret;
4881 }
4882 }
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03004883 ret = input_register_device(tpacpi_inputdev);
4884 if (ret < 0) {
4885 printk(IBM_ERR "unable to register input device\n");
4886 thinkpad_acpi_module_exit();
4887 return ret;
4888 } else {
4889 tp_features.input_device_registered = 1;
4890 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03004892 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893 return 0;
4894}
4895
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03004896static void thinkpad_acpi_module_exit(void)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004897{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004898 struct ibm_struct *ibm, *itmp;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004899
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03004900 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
4901
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004902 list_for_each_entry_safe_reverse(ibm, itmp,
4903 &tpacpi_all_drivers,
4904 all_drivers) {
4905 ibm_exit(ibm);
4906 }
4907
4908 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004909
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03004910 if (tpacpi_inputdev) {
4911 if (tp_features.input_device_registered)
4912 input_unregister_device(tpacpi_inputdev);
4913 else
4914 input_free_device(tpacpi_inputdev);
4915 }
4916
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03004917 if (tpacpi_hwmon)
4918 hwmon_device_unregister(tpacpi_hwmon);
4919
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03004920 if (tp_features.sensors_pdev_attrs_registered)
4921 device_remove_file(&tpacpi_sensors_pdev->dev,
4922 &dev_attr_thinkpad_acpi_pdev_name);
4923 if (tpacpi_sensors_pdev)
4924 platform_device_unregister(tpacpi_sensors_pdev);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03004925 if (tpacpi_pdev)
4926 platform_device_unregister(tpacpi_pdev);
4927
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03004928 if (tp_features.sensors_pdrv_attrs_registered)
4929 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03004930 if (tp_features.platform_drv_attrs_registered)
4931 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
4932
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03004933 if (tp_features.sensors_pdrv_registered)
4934 platform_driver_unregister(&tpacpi_hwmon_pdriver);
4935
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03004936 if (tp_features.platform_drv_registered)
4937 platform_driver_unregister(&tpacpi_pdriver);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03004938
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004939 if (proc_dir)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004940 remove_proc_entry(IBM_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004941
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03004942 kfree(thinkpad_id.bios_version_str);
4943 kfree(thinkpad_id.ec_version_str);
4944 kfree(thinkpad_id.model_str);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004945}
4946
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03004947module_init(thinkpad_acpi_module_init);
4948module_exit(thinkpad_acpi_module_exit);