blob: 7b1080f5843cb6c84f4cce0d819e81d3a7b78427 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -03002 * thinkpad_acpi.c - ThinkPad ACPI Extras
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 *
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
Henrique de Moraes Holschuha62bc912007-03-23 17:33:58 -03006 * Copyright (C) 2006-2007 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
Henrique de Moraes Holschuha62bc912007-03-23 17:33:58 -030020 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
Borislav Deianov78f81cc2005-08-17 00:00:00 -040022 */
23
Henrique de Moraes Holschuhb856f5b2007-10-30 17:46:23 -020024#define IBM_VERSION "0.17"
Henrique de Moraes 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 Holschuh94954cc62007-07-18 23:45:27 -0300138static acpi_handle root_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140#define IBM_HANDLE(object, parent, paths...) \
141 static acpi_handle object##_handle; \
142 static acpi_handle *object##_parent = &parent##_handle; \
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400143 static char *object##_path; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 static char *object##_paths[] = { paths }
145
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400146IBM_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
147 "\\_SB.PCI.ISA.EC", /* 570 */
148 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
149 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
150 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
151 "\\_SB.PCI0.ICH3.EC0", /* R31 */
152 "\\_SB.PCI0.LPC.EC", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300153 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300155IBM_HANDLE(ecrd, ec, "ECRD"); /* 570 */
156IBM_HANDLE(ecwr, ec, "ECWR"); /* 570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300158
159/*************************************************************************
160 * Misc ACPI handles
161 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400163IBM_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, T4x, X31, X40 */
164 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
165 "\\CMS", /* R40, R40e */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300166 ); /* all others */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400167
168IBM_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
169 "^HKEY", /* R30, R31 */
170 "HKEY", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300171 ); /* 570 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400172
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400173
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300174/*************************************************************************
175 * ACPI helpers
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200176 */
177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178static int acpi_evalf(acpi_handle handle,
179 void *res, char *method, char *fmt, ...)
180{
181 char *fmt0 = fmt;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400182 struct acpi_object_list params;
183 union acpi_object in_objs[IBM_MAX_ACPI_ARGS];
184 struct acpi_buffer result, *resultp;
185 union acpi_object out_obj;
186 acpi_status status;
187 va_list ap;
188 char res_type;
189 int success;
190 int quiet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192 if (!*fmt) {
193 printk(IBM_ERR "acpi_evalf() called with empty format\n");
194 return 0;
195 }
196
197 if (*fmt == 'q') {
198 quiet = 1;
199 fmt++;
200 } else
201 quiet = 0;
202
203 res_type = *(fmt++);
204
205 params.count = 0;
206 params.pointer = &in_objs[0];
207
208 va_start(ap, fmt);
209 while (*fmt) {
210 char c = *(fmt++);
211 switch (c) {
212 case 'd': /* int */
213 in_objs[params.count].integer.value = va_arg(ap, int);
214 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
215 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400216 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 default:
218 printk(IBM_ERR "acpi_evalf() called "
219 "with invalid format character '%c'\n", c);
220 return 0;
221 }
222 }
223 va_end(ap);
224
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400225 if (res_type != 'v') {
226 result.length = sizeof(out_obj);
227 result.pointer = &out_obj;
228 resultp = &result;
229 } else
230 resultp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400232 status = acpi_evaluate_object(handle, method, &params, resultp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234 switch (res_type) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400235 case 'd': /* int */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 if (res)
237 *(int *)res = out_obj.integer.value;
238 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
239 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400240 case 'v': /* void */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 success = status == AE_OK;
242 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400243 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 default:
245 printk(IBM_ERR "acpi_evalf() called "
246 "with invalid format character '%c'\n", res_type);
247 return 0;
248 }
249
250 if (!success && !quiet)
251 printk(IBM_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
252 method, fmt0, status);
253
254 return success;
255}
256
257static void __unused acpi_print_int(acpi_handle handle, char *method)
258{
259 int i;
260
261 if (acpi_evalf(handle, &i, method, "d"))
262 printk(IBM_INFO "%s = 0x%x\n", method, i);
263 else
264 printk(IBM_ERR "error calling %s\n", method);
265}
266
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300267static int acpi_ec_read(int i, u8 * p)
268{
269 int v;
270
271 if (ecrd_handle) {
272 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
273 return 0;
274 *p = v;
275 } else {
276 if (ec_read(i, p) < 0)
277 return 0;
278 }
279
280 return 1;
281}
282
283static int acpi_ec_write(int i, u8 v)
284{
285 if (ecwr_handle) {
286 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
287 return 0;
288 } else {
289 if (ec_write(i, v) < 0)
290 return 0;
291 }
292
293 return 1;
294}
295
296static int _sta(acpi_handle handle)
297{
298 int status;
299
300 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
301 status = 0;
302
303 return status;
304}
305
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -0300306static int issue_thinkpad_cmos_command(int cmos_cmd)
307{
308 if (!cmos_handle)
309 return -ENXIO;
310
311 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
312 return -EIO;
313
314 return 0;
315}
316
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300317/*************************************************************************
318 * ACPI device model
319 */
320
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300321static void drv_acpi_handle_init(char *name,
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300322 acpi_handle *handle, acpi_handle parent,
323 char **paths, int num_paths, char **path)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300324{
325 int i;
326 acpi_status status;
327
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300328 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
329 name);
330
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300331 for (i = 0; i < num_paths; i++) {
332 status = acpi_get_handle(parent, paths[i], handle);
333 if (ACPI_SUCCESS(status)) {
334 *path = paths[i];
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300335 dbg_printk(TPACPI_DBG_INIT,
336 "Found ACPI handle %s for %s\n",
337 *path, name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300338 return;
339 }
340 }
341
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300342 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
343 name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300344 *handle = NULL;
345}
346
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300347static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300348{
349 struct ibm_struct *ibm = data;
350
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300351 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
352 return;
353
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300354 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300355 return;
356
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300357 ibm->acpi->notify(ibm, event);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300358}
359
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300360static int __init setup_acpi_notify(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300361{
362 acpi_status status;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300363 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300364
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300365 BUG_ON(!ibm->acpi);
366
367 if (!*ibm->acpi->handle)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300368 return 0;
369
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300370 vdbg_printk(TPACPI_DBG_INIT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300371 "setting up ACPI notify for %s\n", ibm->name);
372
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300373 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
374 if (rc < 0) {
375 printk(IBM_ERR "acpi_bus_get_device(%s) failed: %d\n",
376 ibm->name, rc);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300377 return -ENODEV;
378 }
379
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300380 acpi_driver_data(ibm->acpi->device) = ibm;
381 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300382 IBM_ACPI_EVENT_PREFIX,
383 ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300384
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300385 status = acpi_install_notify_handler(*ibm->acpi->handle,
386 ibm->acpi->type, dispatch_acpi_notify, ibm);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300387 if (ACPI_FAILURE(status)) {
388 if (status == AE_ALREADY_EXISTS) {
389 printk(IBM_NOTICE "another device driver is already handling %s events\n",
390 ibm->name);
391 } else {
392 printk(IBM_ERR "acpi_install_notify_handler(%s) failed: %d\n",
393 ibm->name, status);
394 }
395 return -ENODEV;
396 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300397 ibm->flags.acpi_notify_installed = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300398 return 0;
399}
400
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300401static int __init tpacpi_device_add(struct acpi_device *device)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300402{
403 return 0;
404}
405
Henrique de Moraes Holschuh67001212007-04-21 11:08:25 -0300406static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300407{
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300408 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300409
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300410 dbg_printk(TPACPI_DBG_INIT,
411 "registering %s as an ACPI driver\n", ibm->name);
412
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300413 BUG_ON(!ibm->acpi);
414
415 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
416 if (!ibm->acpi->driver) {
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300417 printk(IBM_ERR "kzalloc(ibm->driver) failed\n");
418 return -ENOMEM;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300419 }
420
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300421 sprintf(ibm->acpi->driver->name, "%s_%s", IBM_NAME, ibm->name);
422 ibm->acpi->driver->ids = ibm->acpi->hid;
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200423
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300424 ibm->acpi->driver->ops.add = &tpacpi_device_add;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300425
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300426 rc = acpi_bus_register_driver(ibm->acpi->driver);
427 if (rc < 0) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300428 printk(IBM_ERR "acpi_bus_register_driver(%s) failed: %d\n",
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200429 ibm->name, rc);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300430 kfree(ibm->acpi->driver);
431 ibm->acpi->driver = NULL;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300432 } else if (!rc)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300433 ibm->flags.acpi_driver_registered = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300434
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300435 return rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300436}
437
438
439/****************************************************************************
440 ****************************************************************************
441 *
442 * Procfs Helpers
443 *
444 ****************************************************************************
445 ****************************************************************************/
446
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300447static int dispatch_procfs_read(char *page, char **start, off_t off,
448 int count, int *eof, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300449{
450 struct ibm_struct *ibm = data;
451 int len;
452
453 if (!ibm || !ibm->read)
454 return -EINVAL;
455
456 len = ibm->read(page);
457 if (len < 0)
458 return len;
459
460 if (len <= off + count)
461 *eof = 1;
462 *start = page + off;
463 len -= off;
464 if (len > count)
465 len = count;
466 if (len < 0)
467 len = 0;
468
469 return len;
470}
471
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300472static int dispatch_procfs_write(struct file *file,
473 const char __user * userbuf,
474 unsigned long count, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300475{
476 struct ibm_struct *ibm = data;
477 char *kernbuf;
478 int ret;
479
480 if (!ibm || !ibm->write)
481 return -EINVAL;
482
483 kernbuf = kmalloc(count + 2, GFP_KERNEL);
484 if (!kernbuf)
485 return -ENOMEM;
486
487 if (copy_from_user(kernbuf, userbuf, count)) {
488 kfree(kernbuf);
489 return -EFAULT;
490 }
491
492 kernbuf[count] = 0;
493 strcat(kernbuf, ",");
494 ret = ibm->write(kernbuf);
495 if (ret == 0)
496 ret = count;
497
498 kfree(kernbuf);
499
500 return ret;
501}
502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503static char *next_cmd(char **cmds)
504{
505 char *start = *cmds;
506 char *end;
507
508 while ((end = strchr(start, ',')) && end == start)
509 start = end + 1;
510
511 if (!end)
512 return NULL;
513
514 *end = 0;
515 *cmds = end + 1;
516 return start;
517}
518
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300519
520/****************************************************************************
521 ****************************************************************************
522 *
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300523 * Device model: input, hwmon and platform
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300524 *
525 ****************************************************************************
526 ****************************************************************************/
527
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -0300528static struct platform_device *tpacpi_pdev;
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300529static struct platform_device *tpacpi_sensors_pdev;
Tony Jones1beeffe2007-08-20 13:46:20 -0700530static struct device *tpacpi_hwmon;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300531static struct input_dev *tpacpi_inputdev;
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -0300532static struct mutex tpacpi_inputdev_send_mutex;
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300533
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300534
535static int tpacpi_resume_handler(struct platform_device *pdev)
536{
537 struct ibm_struct *ibm, *itmp;
538
539 list_for_each_entry_safe(ibm, itmp,
540 &tpacpi_all_drivers,
541 all_drivers) {
542 if (ibm->resume)
543 (ibm->resume)();
544 }
545
546 return 0;
547}
548
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300549static struct platform_driver tpacpi_pdriver = {
550 .driver = {
551 .name = IBM_DRVR_NAME,
552 .owner = THIS_MODULE,
553 },
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300554 .resume = tpacpi_resume_handler,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300555};
556
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300557static struct platform_driver tpacpi_hwmon_pdriver = {
558 .driver = {
559 .name = IBM_HWMON_DRVR_NAME,
560 .owner = THIS_MODULE,
561 },
562};
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300563
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -0300564/*************************************************************************
565 * thinkpad-acpi driver attributes
566 */
567
568/* interface_version --------------------------------------------------- */
569static ssize_t tpacpi_driver_interface_version_show(
570 struct device_driver *drv,
571 char *buf)
572{
573 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
574}
575
576static DRIVER_ATTR(interface_version, S_IRUGO,
577 tpacpi_driver_interface_version_show, NULL);
578
579/* debug_level --------------------------------------------------------- */
580static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
581 char *buf)
582{
583 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
584}
585
586static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
587 const char *buf, size_t count)
588{
589 unsigned long t;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -0300590
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300591 if (parse_strtoul(buf, 0xffff, &t))
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -0300592 return -EINVAL;
593
594 dbg_level = t;
595
596 return count;
597}
598
599static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
600 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
601
602/* version ------------------------------------------------------------- */
603static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
604 char *buf)
605{
606 return snprintf(buf, PAGE_SIZE, "%s v%s\n", IBM_DESC, IBM_VERSION);
607}
608
609static DRIVER_ATTR(version, S_IRUGO,
610 tpacpi_driver_version_show, NULL);
611
612/* --------------------------------------------------------------------- */
613
614static struct driver_attribute* tpacpi_driver_attributes[] = {
615 &driver_attr_debug_level, &driver_attr_version,
616 &driver_attr_interface_version,
617};
618
619static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
620{
621 int i, res;
622
623 i = 0;
624 res = 0;
625 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
626 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
627 i++;
628 }
629
630 return res;
631}
632
633static void tpacpi_remove_driver_attributes(struct device_driver *drv)
634{
635 int i;
636
637 for(i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
638 driver_remove_file(drv, tpacpi_driver_attributes[i]);
639}
640
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300641/*************************************************************************
642 * sysfs support helpers
643 */
644
645struct attribute_set_obj {
646 struct attribute_set s;
647 struct attribute *a;
648} __attribute__((packed));
649
650static struct attribute_set *create_attr_set(unsigned int max_members,
651 const char* name)
652{
653 struct attribute_set_obj *sobj;
654
655 if (max_members == 0)
656 return NULL;
657
658 /* Allocates space for implicit NULL at the end too */
659 sobj = kzalloc(sizeof(struct attribute_set_obj) +
660 max_members * sizeof(struct attribute *),
661 GFP_KERNEL);
662 if (!sobj)
663 return NULL;
664 sobj->s.max_members = max_members;
665 sobj->s.group.attrs = &sobj->a;
666 sobj->s.group.name = name;
667
668 return &sobj->s;
669}
670
671/* not multi-threaded safe, use it in a single thread per set */
672static int add_to_attr_set(struct attribute_set* s, struct attribute *attr)
673{
674 if (!s || !attr)
675 return -EINVAL;
676
677 if (s->members >= s->max_members)
678 return -ENOMEM;
679
680 s->group.attrs[s->members] = attr;
681 s->members++;
682
683 return 0;
684}
685
686static int add_many_to_attr_set(struct attribute_set* s,
687 struct attribute **attr,
688 unsigned int count)
689{
690 int i, res;
691
692 for (i = 0; i < count; i++) {
693 res = add_to_attr_set(s, attr[i]);
694 if (res)
695 return res;
696 }
697
698 return 0;
699}
700
701static void delete_attr_set(struct attribute_set* s, struct kobject *kobj)
702{
703 sysfs_remove_group(kobj, &s->group);
704 destroy_attr_set(s);
705}
706
707static int parse_strtoul(const char *buf,
708 unsigned long max, unsigned long *value)
709{
710 char *endp;
711
Henrique de Moraes Holschuh32afbf02007-10-08 10:12:56 -0300712 while (*buf && isspace(*buf))
713 buf++;
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300714 *value = simple_strtoul(buf, &endp, 0);
715 while (*endp && isspace(*endp))
716 endp++;
717 if (*endp || *value > max)
718 return -EINVAL;
719
720 return 0;
721}
722
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300723/****************************************************************************
724 ****************************************************************************
725 *
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300726 * Subdrivers
727 *
728 ****************************************************************************
729 ****************************************************************************/
730
731/*************************************************************************
Henrique de Moraes Holschuh142cfc92007-04-21 11:08:26 -0300732 * thinkpad-acpi init subdriver
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300733 */
734
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300735static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736{
737 printk(IBM_INFO "%s v%s\n", IBM_DESC, IBM_VERSION);
738 printk(IBM_INFO "%s\n", IBM_URL);
739
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -0300740 printk(IBM_INFO "ThinkPad BIOS %s, EC %s\n",
741 (thinkpad_id.bios_version_str) ?
742 thinkpad_id.bios_version_str : "unknown",
743 (thinkpad_id.ec_version_str) ?
744 thinkpad_id.ec_version_str : "unknown");
745
746 if (thinkpad_id.vendor && thinkpad_id.model_str)
747 printk(IBM_INFO "%s %s\n",
748 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
749 "IBM" : ((thinkpad_id.vendor ==
750 PCI_VENDOR_ID_LENOVO) ?
751 "Lenovo" : "Unknown vendor"),
752 thinkpad_id.model_str);
Henrique de Moraes Holschuh3945ac32007-02-06 19:13:44 -0200753
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 return 0;
755}
756
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300757static int thinkpad_acpi_driver_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758{
759 int len = 0;
760
761 len += sprintf(p + len, "driver:\t\t%s\n", IBM_DESC);
762 len += sprintf(p + len, "version:\t%s\n", IBM_VERSION);
763
764 return len;
765}
766
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300767static struct ibm_struct thinkpad_acpi_driver_data = {
768 .name = "driver",
769 .read = thinkpad_acpi_driver_read,
770};
771
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300772/*************************************************************************
773 * Hotkey subdriver
774 */
775
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 Holschuh94954cc62007-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 Holschuh0f089142008-01-08 13:02:38 -0200957 /* Requirements for changing the default keymaps:
958 *
959 * 1. Many of the keys are mapped to KEY_RESERVED for very
960 * good reasons. Do not change them unless you have deep
961 * knowledge on the IBM and Lenovo ThinkPad firmware for
962 * the various ThinkPad models. The driver behaves
963 * differently for KEY_RESERVED: such keys have their
964 * hot key mask *unset* in mask_recommended, and also
965 * in the initial hot key mask programmed into the
966 * firmware at driver load time, which means the firm-
967 * ware may react very differently if you change them to
968 * something else;
969 *
970 * 2. You must be subscribed to the linux-thinkpad and
971 * ibm-acpi-devel mailing lists, and you should read the
972 * list archives since 2007 if you want to change the
973 * keymaps. This requirement exists so that you will
974 * know the past history of problems with the thinkpad-
975 * acpi driver keymaps, and also that you will be
976 * listening to any bug reports;
977 *
978 * 3. Do not send thinkpad-acpi specific patches directly to
979 * for merging, *ever*. Send them to the linux-acpi
980 * mailinglist for comments. Merging is to be done only
981 * through acpi-test and the ACPI maintainer.
982 *
983 * If the above is too much to ask, don't change the keymap.
984 * Ask the thinkpad-acpi maintainer to do it, instead.
985 */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -0300986 static u16 ibm_keycode_map[] __initdata = {
987 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
988 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
989 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
990 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -0200991
992 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -0300993 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
994 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
995 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -0200996
997 /* brightness: firmware always reacts to them, unless
998 * X.org did some tricks in the radeon BIOS scratch
999 * registers of *some* models */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02001000 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02001001 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001002
1003 /* Thinklight: firmware always react to it */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001004 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001005
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001006 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
1007 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001008
1009 /* Volume: firmware always react to it and reprograms
1010 * the built-in *extra* mixer. Never map it to control
1011 * another mixer by default. */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02001012 KEY_RESERVED, /* 0x14: VOLUME UP */
1013 KEY_RESERVED, /* 0x15: VOLUME DOWN */
1014 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001015
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001016 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001017
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001018 /* (assignments unknown, please report if found) */
1019 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1020 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1021 };
1022 static u16 lenovo_keycode_map[] __initdata = {
1023 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1024 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
1025 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1026 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001027
1028 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001029 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1030 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1031 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001032
Henrique de Moraes Holschuh56a185b2007-12-13 12:14:09 -02001033 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuh56a185b2007-12-13 12:14:09 -02001034 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001035
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001036 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001037
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001038 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
1039 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001040
1041 /* Volume: z60/z61, T60 (BIOS version?): firmware always
1042 * react to it and reprograms the built-in *extra* mixer.
1043 * Never map it to control another mixer by default.
1044 *
1045 * T60?, T61, R60?, R61: firmware and EC tries to send
1046 * these over the regular keyboard, so these are no-ops,
1047 * but there are still weird bugs re. MUTE, so do not
1048 * change unless you get test reports from all Lenovo
1049 * models. May cause the BIOS to interfere with the
1050 * HDA mixer.
1051 */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02001052 KEY_RESERVED, /* 0x14: VOLUME UP */
1053 KEY_RESERVED, /* 0x15: VOLUME DOWN */
1054 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001055
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001056 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001057
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001058 /* (assignments unknown, please report if found) */
1059 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1060 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1061 };
1062
1063#define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
1064#define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
1065#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
1066
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001067 int res, i;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001068 int status;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001069 int hkeyv;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001070
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001071 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
1072
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001073 BUG_ON(!tpacpi_inputdev);
1074
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001075 IBM_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001076 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001077
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001078 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001079 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001080
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001081 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001082 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001083
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001084 if (tp_features.hotkey) {
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001085 hotkey_dev_attributes = create_attr_set(8, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001086 if (!hotkey_dev_attributes)
1087 return -ENOMEM;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001088 res = add_many_to_attr_set(hotkey_dev_attributes,
1089 hotkey_attributes,
1090 ARRAY_SIZE(hotkey_attributes));
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001091 if (res)
1092 return res;
1093
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001094 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001095 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
1096 for HKEY interface version 0x100 */
1097 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
1098 if ((hkeyv >> 8) != 1) {
1099 printk(IBM_ERR "unknown version of the "
1100 "HKEY interface: 0x%x\n", hkeyv);
1101 printk(IBM_ERR "please report this to %s\n",
1102 IBM_MAIL);
1103 } else {
1104 /*
1105 * MHKV 0x100 in A31, R40, R40e,
1106 * T4x, X31, and later
1107 * */
1108 tp_features.hotkey_mask = 1;
1109 }
1110 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001111
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001112 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001113 str_supported(tp_features.hotkey_mask));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001114
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001115 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001116 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001117 "MHKA", "qd")) {
1118 printk(IBM_ERR
1119 "missing MHKA handler, "
1120 "please report this to %s\n",
1121 IBM_MAIL);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001122 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001123 }
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001124 }
1125
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001126 res = hotkey_get(&hotkey_orig_status, &hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001127 if (!res && tp_features.hotkey_mask) {
1128 res = add_many_to_attr_set(hotkey_dev_attributes,
1129 hotkey_mask_attributes,
1130 ARRAY_SIZE(hotkey_mask_attributes));
1131 }
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001132
1133 /* Not all thinkpads have a hardware radio switch */
1134 if (!res && acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
1135 tp_features.hotkey_wlsw = 1;
1136 printk(IBM_INFO
1137 "radio switch found; radios are %s\n",
1138 enabled(status, 0));
1139 res = add_to_attr_set(hotkey_dev_attributes,
1140 &dev_attr_hotkey_radio_sw.attr);
1141 }
1142
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001143 if (!res)
1144 res = register_attr_set_with_sysfs(
1145 hotkey_dev_attributes,
1146 &tpacpi_pdev->dev.kobj);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001147 if (res)
1148 return res;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001149
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001150 /* Set up key map */
1151
1152 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
1153 GFP_KERNEL);
1154 if (!hotkey_keycode_map) {
1155 printk(IBM_ERR "failed to allocate memory for key map\n");
1156 return -ENOMEM;
1157 }
1158
1159 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
1160 dbg_printk(TPACPI_DBG_INIT,
1161 "using Lenovo default hot key map\n");
1162 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
1163 TPACPI_HOTKEY_MAP_SIZE);
1164 } else {
1165 dbg_printk(TPACPI_DBG_INIT,
1166 "using IBM default hot key map\n");
1167 memcpy(hotkey_keycode_map, &ibm_keycode_map,
1168 TPACPI_HOTKEY_MAP_SIZE);
1169 }
1170
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001171 set_bit(EV_KEY, tpacpi_inputdev->evbit);
1172 set_bit(EV_MSC, tpacpi_inputdev->evbit);
1173 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001174 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
1175 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
1176 tpacpi_inputdev->keycode = hotkey_keycode_map;
1177 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001178 if (hotkey_keycode_map[i] != KEY_RESERVED) {
1179 set_bit(hotkey_keycode_map[i],
1180 tpacpi_inputdev->keybit);
1181 } else {
1182 if (i < sizeof(hotkey_reserved_mask)*8)
1183 hotkey_reserved_mask |= 1 << i;
1184 }
1185 }
1186
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03001187 if (tp_features.hotkey_wlsw) {
1188 set_bit(EV_SW, tpacpi_inputdev->evbit);
1189 set_bit(SW_RADIO, tpacpi_inputdev->swbit);
1190 }
1191
Henrique de Moraes Holschuh1a343762007-07-18 23:45:36 -03001192 dbg_printk(TPACPI_DBG_INIT,
1193 "enabling hot key handling\n");
1194 res = hotkey_set(1, (hotkey_all_mask & ~hotkey_reserved_mask)
1195 | hotkey_orig_mask);
1196 if (res)
1197 return res;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001198
1199 dbg_printk(TPACPI_DBG_INIT,
1200 "legacy hot key reporting over procfs %s\n",
1201 (hotkey_report_mode < 2) ?
1202 "enabled" : "disabled");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001203 }
1204
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001205 return (tp_features.hotkey)? 0 : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001206}
1207
1208static void hotkey_exit(void)
1209{
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001210 int res;
1211
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001212 if (tp_features.hotkey) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001213 dbg_printk(TPACPI_DBG_EXIT, "restoring original hotkey mask\n");
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001214 res = hotkey_set(hotkey_orig_status, hotkey_orig_mask);
1215 if (res)
1216 printk(IBM_ERR "failed to restore hotkey to BIOS defaults\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001217 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001218
1219 if (hotkey_dev_attributes) {
1220 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
1221 hotkey_dev_attributes = NULL;
1222 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001223}
1224
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001225static void tpacpi_input_send_key(unsigned int scancode,
1226 unsigned int keycode)
1227{
1228 if (keycode != KEY_RESERVED) {
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03001229 mutex_lock(&tpacpi_inputdev_send_mutex);
1230
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001231 input_report_key(tpacpi_inputdev, keycode, 1);
1232 if (keycode == KEY_UNKNOWN)
1233 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1234 scancode);
1235 input_sync(tpacpi_inputdev);
1236
1237 input_report_key(tpacpi_inputdev, keycode, 0);
1238 if (keycode == KEY_UNKNOWN)
1239 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1240 scancode);
1241 input_sync(tpacpi_inputdev);
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03001242
1243 mutex_unlock(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001244 }
1245}
1246
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03001247static void tpacpi_input_send_radiosw(void)
1248{
1249 int wlsw;
1250
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03001251 mutex_lock(&tpacpi_inputdev_send_mutex);
1252
Henrique de Moraes Holschuhf4e1e432007-09-23 11:39:00 -03001253 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03001254 input_report_switch(tpacpi_inputdev,
1255 SW_RADIO, !!wlsw);
Henrique de Moraes Holschuhf4e1e432007-09-23 11:39:00 -03001256 input_sync(tpacpi_inputdev);
1257 }
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03001258
1259 mutex_unlock(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03001260}
1261
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001262static void hotkey_notify(struct ibm_struct *ibm, u32 event)
1263{
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001264 u32 hkey;
1265 unsigned int keycode, scancode;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03001266 int send_acpi_ev;
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03001267 int ignore_acpi_ev;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001268
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03001269 if (event != 0x80) {
1270 printk(IBM_ERR "unknown HKEY notification event %d\n", event);
1271 /* forward it to userspace, maybe it knows how to handle it */
1272 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
1273 ibm->acpi->device->dev.bus_id,
1274 event, 0);
1275 return;
1276 }
1277
1278 while (1) {
1279 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
1280 printk(IBM_ERR "failed to retrieve HKEY event\n");
1281 return;
1282 }
1283
1284 if (hkey == 0) {
1285 /* queue empty */
1286 return;
1287 }
1288
1289 send_acpi_ev = 0;
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03001290 ignore_acpi_ev = 0;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03001291
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001292 switch (hkey >> 12) {
1293 case 1:
1294 /* 0x1000-0x1FFF: key presses */
1295 scancode = hkey & 0xfff;
1296 if (scancode > 0 && scancode < 0x21) {
1297 scancode--;
1298 keycode = hotkey_keycode_map[scancode];
1299 tpacpi_input_send_key(scancode, keycode);
1300 } else {
1301 printk(IBM_ERR
1302 "hotkey 0x%04x out of range for keyboard map\n",
1303 hkey);
1304 send_acpi_ev = 1;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001305 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001306 break;
1307 case 5:
1308 /* 0x5000-0x5FFF: LID */
1309 /* we don't handle it through this path, just
1310 * eat up known LID events */
1311 if (hkey != 0x5001 && hkey != 0x5002) {
1312 printk(IBM_ERR
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03001313 "unknown LID-related HKEY event: 0x%04x\n",
1314 hkey);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001315 send_acpi_ev = 1;
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03001316 } else {
1317 ignore_acpi_ev = 1;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001318 }
1319 break;
1320 case 7:
1321 /* 0x7000-0x7FFF: misc */
1322 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
1323 tpacpi_input_send_radiosw();
1324 break;
1325 }
1326 /* fallthrough to default */
1327 default:
1328 /* case 2: dock-related */
1329 /* 0x2305 - T43 waking up due to bay lever eject while aslept */
1330 /* case 3: ultra-bay related. maybe bay in dock? */
1331 /* 0x3003 - T43 after wake up by bay lever eject (0x2305) */
1332 printk(IBM_NOTICE "unhandled HKEY event 0x%04x\n", hkey);
1333 send_acpi_ev = 1;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001334 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001335
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03001336 /* Legacy events */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03001337 if (!ignore_acpi_ev && (send_acpi_ev || hotkey_report_mode < 2)) {
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03001338 acpi_bus_generate_proc_event(ibm->acpi->device, event, hkey);
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03001339 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001340
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03001341 /* netlink events */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03001342 if (!ignore_acpi_ev && send_acpi_ev) {
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03001343 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
1344 ibm->acpi->device->dev.bus_id,
1345 event, hkey);
1346 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001347 }
1348}
1349
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03001350static void hotkey_resume(void)
1351{
1352 tpacpi_input_send_radiosw();
1353}
1354
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001355/*
1356 * Call with hotkey_mutex held
1357 */
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001358static int hotkey_get(int *status, u32 *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359{
1360 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001361 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001362
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001363 if (tp_features.hotkey_mask)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001364 if (!acpi_evalf(hkey_handle, mask, "DHKN", "d"))
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001365 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001366
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001367 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368}
1369
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001370/*
1371 * Call with hotkey_mutex held
1372 */
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001373static int hotkey_set(int status, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374{
1375 int i;
1376
1377 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001378 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001380 if (tp_features.hotkey_mask)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001381 for (i = 0; i < 32; i++) {
1382 int bit = ((1 << i) & mask) != 0;
1383 if (!acpi_evalf(hkey_handle,
1384 NULL, "MHKM", "vdd", i + 1, bit))
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001385 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001386 }
1387
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001388 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001389}
1390
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001391/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001392static int hotkey_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001394 int res, status;
1395 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 int len = 0;
1397
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001398 if (!tp_features.hotkey) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001399 len += sprintf(p + len, "status:\t\tnot supported\n");
1400 return len;
1401 }
1402
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02001403 if (mutex_lock_interruptible(&hotkey_mutex))
1404 return -ERESTARTSYS;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001405 res = hotkey_get(&status, &mask);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001406 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001407 if (res)
1408 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
1410 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001411 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001412 len += sprintf(p + len, "mask:\t\t0x%08x\n", mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 len += sprintf(p + len,
1414 "commands:\tenable, disable, reset, <mask>\n");
1415 } else {
1416 len += sprintf(p + len, "mask:\t\tnot supported\n");
1417 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
1418 }
1419
1420 return len;
1421}
1422
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001423static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001425 int res, status;
1426 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 char *cmd;
1428 int do_cmd = 0;
1429
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001430 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 return -ENODEV;
1432
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02001433 if (mutex_lock_interruptible(&hotkey_mutex))
1434 return -ERESTARTSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001435
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001436 res = hotkey_get(&status, &mask);
1437 if (res)
1438 goto errexit;
1439
1440 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 while ((cmd = next_cmd(&buf))) {
1442 if (strlencmp(cmd, "enable") == 0) {
1443 status = 1;
1444 } else if (strlencmp(cmd, "disable") == 0) {
1445 status = 0;
1446 } else if (strlencmp(cmd, "reset") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001447 status = hotkey_orig_status;
1448 mask = hotkey_orig_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
1450 /* mask set */
1451 } else if (sscanf(cmd, "%x", &mask) == 1) {
1452 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001453 } else {
1454 res = -EINVAL;
1455 goto errexit;
1456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 do_cmd = 1;
1458 }
1459
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001460 if (do_cmd)
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001461 res = hotkey_set(status, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001463errexit:
1464 mutex_unlock(&hotkey_mutex);
1465 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001466}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
Thomas Renninger1ba90e32007-07-23 14:44:41 +02001468static const struct acpi_device_id ibm_htk_device_ids[] = {
1469 {IBM_HKEY_HID, 0},
1470 {"", 0},
1471};
1472
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001473static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +02001474 .hid = ibm_htk_device_ids,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001475 .notify = hotkey_notify,
1476 .handle = &hkey_handle,
1477 .type = ACPI_DEVICE_NOTIFY,
1478};
1479
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001480static struct ibm_struct hotkey_driver_data = {
1481 .name = "hotkey",
1482 .read = hotkey_read,
1483 .write = hotkey_write,
1484 .exit = hotkey_exit,
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03001485 .resume = hotkey_resume,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001486 .acpi = &ibm_hotkey_acpidriver,
1487};
1488
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001489/*************************************************************************
1490 * Bluetooth subdriver
1491 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001493/* sysfs bluetooth enable ---------------------------------------------- */
1494static ssize_t bluetooth_enable_show(struct device *dev,
1495 struct device_attribute *attr,
1496 char *buf)
1497{
1498 int status;
1499
1500 status = bluetooth_get_radiosw();
1501 if (status < 0)
1502 return status;
1503
1504 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
1505}
1506
1507static ssize_t bluetooth_enable_store(struct device *dev,
1508 struct device_attribute *attr,
1509 const char *buf, size_t count)
1510{
1511 unsigned long t;
1512 int res;
1513
1514 if (parse_strtoul(buf, 1, &t))
1515 return -EINVAL;
1516
1517 res = bluetooth_set_radiosw(t);
1518
1519 return (res) ? res : count;
1520}
1521
1522static struct device_attribute dev_attr_bluetooth_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001523 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001524 bluetooth_enable_show, bluetooth_enable_store);
1525
1526/* --------------------------------------------------------------------- */
1527
1528static struct attribute *bluetooth_attributes[] = {
1529 &dev_attr_bluetooth_enable.attr,
1530 NULL
1531};
1532
1533static const struct attribute_group bluetooth_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001534 .attrs = bluetooth_attributes,
1535};
1536
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001537static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001539 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001540 int status = 0;
1541
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001542 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
1543
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001544 IBM_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001545
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001546 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1547 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001548 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001549 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001551 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
1552 str_supported(tp_features.bluetooth),
1553 status);
1554
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001555 if (tp_features.bluetooth) {
1556 if (!(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
1557 /* no bluetooth hardware present in system */
1558 tp_features.bluetooth = 0;
1559 dbg_printk(TPACPI_DBG_INIT,
1560 "bluetooth hardware not installed\n");
1561 } else {
1562 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
1563 &bluetooth_attr_group);
1564 if (res)
1565 return res;
1566 }
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001567 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001568
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001569 return (tp_features.bluetooth)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570}
1571
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001572static void bluetooth_exit(void)
1573{
1574 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
1575 &bluetooth_attr_group);
1576}
1577
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001578static int bluetooth_get_radiosw(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579{
1580 int status;
1581
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001582 if (!tp_features.bluetooth)
1583 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001585 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
1586 return -EIO;
1587
1588 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
1589}
1590
1591static int bluetooth_set_radiosw(int radio_on)
1592{
1593 int status;
1594
1595 if (!tp_features.bluetooth)
1596 return -ENODEV;
1597
1598 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
1599 return -EIO;
1600 if (radio_on)
1601 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
1602 else
1603 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
1604 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
1605 return -EIO;
1606
1607 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608}
1609
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001610/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001611static int bluetooth_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612{
1613 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001614 int status = bluetooth_get_radiosw();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001616 if (!tp_features.bluetooth)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 len += sprintf(p + len, "status:\t\tnot supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001619 len += sprintf(p + len, "status:\t\t%s\n",
1620 (status)? "enabled" : "disabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 len += sprintf(p + len, "commands:\tenable, disable\n");
1622 }
1623
1624 return len;
1625}
1626
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001627static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001631 if (!tp_features.bluetooth)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001632 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633
1634 while ((cmd = next_cmd(&buf))) {
1635 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001636 bluetooth_set_radiosw(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001638 bluetooth_set_radiosw(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 } else
1640 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 }
1642
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 return 0;
1644}
1645
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001646static struct ibm_struct bluetooth_driver_data = {
1647 .name = "bluetooth",
1648 .read = bluetooth_read,
1649 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001650 .exit = bluetooth_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001651};
1652
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001653/*************************************************************************
1654 * Wan subdriver
1655 */
1656
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001657/* sysfs wan enable ---------------------------------------------------- */
1658static ssize_t wan_enable_show(struct device *dev,
1659 struct device_attribute *attr,
1660 char *buf)
1661{
1662 int status;
1663
1664 status = wan_get_radiosw();
1665 if (status < 0)
1666 return status;
1667
1668 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
1669}
1670
1671static ssize_t wan_enable_store(struct device *dev,
1672 struct device_attribute *attr,
1673 const char *buf, size_t count)
1674{
1675 unsigned long t;
1676 int res;
1677
1678 if (parse_strtoul(buf, 1, &t))
1679 return -EINVAL;
1680
1681 res = wan_set_radiosw(t);
1682
1683 return (res) ? res : count;
1684}
1685
1686static struct device_attribute dev_attr_wan_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001687 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001688 wan_enable_show, wan_enable_store);
1689
1690/* --------------------------------------------------------------------- */
1691
1692static struct attribute *wan_attributes[] = {
1693 &dev_attr_wan_enable.attr,
1694 NULL
1695};
1696
1697static const struct attribute_group wan_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001698 .attrs = wan_attributes,
1699};
1700
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001701static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001702{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001703 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001704 int status = 0;
1705
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001706 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
1707
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001708 IBM_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001709
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001710 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001711 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001712
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001713 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
1714 str_supported(tp_features.wan),
1715 status);
1716
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001717 if (tp_features.wan) {
1718 if (!(status & TP_ACPI_WANCARD_HWPRESENT)) {
1719 /* no wan hardware present in system */
1720 tp_features.wan = 0;
1721 dbg_printk(TPACPI_DBG_INIT,
1722 "wan hardware not installed\n");
1723 } else {
1724 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
1725 &wan_attr_group);
1726 if (res)
1727 return res;
1728 }
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001729 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001730
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001731 return (tp_features.wan)? 0 : 1;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001732}
1733
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001734static void wan_exit(void)
1735{
1736 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
1737 &wan_attr_group);
1738}
1739
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001740static int wan_get_radiosw(void)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001741{
1742 int status;
1743
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001744 if (!tp_features.wan)
1745 return -ENODEV;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001746
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001747 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
1748 return -EIO;
1749
1750 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
1751}
1752
1753static int wan_set_radiosw(int radio_on)
1754{
1755 int status;
1756
1757 if (!tp_features.wan)
1758 return -ENODEV;
1759
1760 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
1761 return -EIO;
1762 if (radio_on)
1763 status |= TP_ACPI_WANCARD_RADIOSSW;
1764 else
1765 status &= ~TP_ACPI_WANCARD_RADIOSSW;
1766 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
1767 return -EIO;
1768
1769 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001770}
1771
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001772/* procfs -------------------------------------------------------------- */
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001773static int wan_read(char *p)
1774{
1775 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001776 int status = wan_get_radiosw();
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001777
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001778 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001779 len += sprintf(p + len, "status:\t\tnot supported\n");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001780 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001781 len += sprintf(p + len, "status:\t\t%s\n",
1782 (status)? "enabled" : "disabled");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001783 len += sprintf(p + len, "commands:\tenable, disable\n");
1784 }
1785
1786 return len;
1787}
1788
1789static int wan_write(char *buf)
1790{
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001791 char *cmd;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001792
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001793 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001794 return -ENODEV;
1795
1796 while ((cmd = next_cmd(&buf))) {
1797 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001798 wan_set_radiosw(1);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001799 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001800 wan_set_radiosw(0);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001801 } else
1802 return -EINVAL;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001803 }
1804
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001805 return 0;
1806}
1807
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001808static struct ibm_struct wan_driver_data = {
1809 .name = "wan",
1810 .read = wan_read,
1811 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001812 .exit = wan_exit,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03001813 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001814};
1815
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001816/*************************************************************************
1817 * Video subdriver
1818 */
1819
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02001820static enum video_access_mode video_supported;
1821static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001822
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001823IBM_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
1824 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
1825 "\\_SB.PCI0.VID0", /* 770e */
1826 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
1827 "\\_SB.PCI0.AGP.VID", /* all others */
1828 ); /* R30, R31 */
1829
1830IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
1831
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001832static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001834 int ivga;
1835
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001836 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
1837
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001838 IBM_ACPIHANDLE_INIT(vid);
1839 IBM_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001840
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001841 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
1842 /* G41, assume IVGA doesn't change */
1843 vid_handle = vid2_handle;
1844
1845 if (!vid_handle)
1846 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001847 video_supported = TPACPI_VIDEO_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001848 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
1849 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001850 video_supported = TPACPI_VIDEO_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001851 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
1852 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001853 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001854 else
1855 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001856 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001858 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
1859 str_supported(video_supported != TPACPI_VIDEO_NONE),
1860 video_supported);
1861
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001862 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863}
1864
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001865static void video_exit(void)
1866{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001867 dbg_printk(TPACPI_DBG_EXIT,
1868 "restoring original video autoswitch mode\n");
1869 if (video_autosw_set(video_orig_autosw))
1870 printk(IBM_ERR "error while trying to restore original "
1871 "video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001872}
1873
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001874static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875{
1876 int status = 0;
1877 int i;
1878
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001879 switch (video_supported) {
1880 case TPACPI_VIDEO_570:
1881 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
1882 TP_ACPI_VIDEO_570_PHSCMD))
1883 return -EIO;
1884 status = i & TP_ACPI_VIDEO_570_PHSMASK;
1885 break;
1886 case TPACPI_VIDEO_770:
1887 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
1888 return -EIO;
1889 if (i)
1890 status |= TP_ACPI_VIDEO_S_LCD;
1891 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
1892 return -EIO;
1893 if (i)
1894 status |= TP_ACPI_VIDEO_S_CRT;
1895 break;
1896 case TPACPI_VIDEO_NEW:
1897 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
1898 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
1899 return -EIO;
1900 if (i)
1901 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001903 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
1904 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
1905 return -EIO;
1906 if (i)
1907 status |= TP_ACPI_VIDEO_S_LCD;
1908 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
1909 return -EIO;
1910 if (i)
1911 status |= TP_ACPI_VIDEO_S_DVI;
1912 break;
1913 default:
1914 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001915 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916
1917 return status;
1918}
1919
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001920static int video_outputsw_set(int status)
1921{
1922 int autosw;
1923 int res = 0;
1924
1925 switch (video_supported) {
1926 case TPACPI_VIDEO_570:
1927 res = acpi_evalf(NULL, NULL,
1928 "\\_SB.PHS2", "vdd",
1929 TP_ACPI_VIDEO_570_PHS2CMD,
1930 status | TP_ACPI_VIDEO_570_PHS2SET);
1931 break;
1932 case TPACPI_VIDEO_770:
1933 autosw = video_autosw_get();
1934 if (autosw < 0)
1935 return autosw;
1936
1937 res = video_autosw_set(1);
1938 if (res)
1939 return res;
1940 res = acpi_evalf(vid_handle, NULL,
1941 "ASWT", "vdd", status * 0x100, 0);
1942 if (!autosw && video_autosw_set(autosw)) {
1943 printk(IBM_ERR "video auto-switch left enabled due to error\n");
1944 return -EIO;
1945 }
1946 break;
1947 case TPACPI_VIDEO_NEW:
1948 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
1949 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
1950 break;
1951 default:
1952 return -ENOSYS;
1953 }
1954
1955 return (res)? 0 : -EIO;
1956}
1957
1958static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001960 int autosw = 0;
1961
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001962 switch (video_supported) {
1963 case TPACPI_VIDEO_570:
1964 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
1965 return -EIO;
1966 break;
1967 case TPACPI_VIDEO_770:
1968 case TPACPI_VIDEO_NEW:
1969 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
1970 return -EIO;
1971 break;
1972 default:
1973 return -ENOSYS;
1974 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001975
1976 return autosw & 1;
1977}
1978
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001979static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001980{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001981 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001982 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001983 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001984}
1985
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001986static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001987{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001988 int autosw = video_autosw_get();
1989 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001990
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001991 if (autosw < 0)
1992 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001993
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001994 switch (video_supported) {
1995 case TPACPI_VIDEO_570:
1996 res = video_autosw_set(1);
1997 if (res)
1998 return res;
1999 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
2000 break;
2001 case TPACPI_VIDEO_770:
2002 case TPACPI_VIDEO_NEW:
2003 res = video_autosw_set(1);
2004 if (res)
2005 return res;
2006 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
2007 break;
2008 default:
2009 return -ENOSYS;
2010 }
2011 if (!autosw && video_autosw_set(autosw)) {
2012 printk(IBM_ERR "video auto-switch left enabled due to error\n");
2013 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002014 }
2015
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002016 return (res)? 0 : -EIO;
2017}
2018
2019static int video_expand_toggle(void)
2020{
2021 switch (video_supported) {
2022 case TPACPI_VIDEO_570:
2023 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
2024 0 : -EIO;
2025 case TPACPI_VIDEO_770:
2026 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
2027 0 : -EIO;
2028 case TPACPI_VIDEO_NEW:
2029 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
2030 0 : -EIO;
2031 default:
2032 return -ENOSYS;
2033 }
2034 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002035}
2036
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002037static int video_read(char *p)
2038{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002039 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002040 int len = 0;
2041
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002042 if (video_supported == TPACPI_VIDEO_NONE) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002043 len += sprintf(p + len, "status:\t\tnot supported\n");
2044 return len;
2045 }
2046
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002047 status = video_outputsw_get();
2048 if (status < 0)
2049 return status;
2050
2051 autosw = video_autosw_get();
2052 if (autosw < 0)
2053 return autosw;
2054
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002055 len += sprintf(p + len, "status:\t\tsupported\n");
2056 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
2057 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002058 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002059 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
2060 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
2061 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
2062 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002063 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002064 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
2065 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
2066 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
2067
2068 return len;
2069}
2070
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002071static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072{
2073 char *cmd;
2074 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002075 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002077 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002078 return -ENODEV;
2079
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002080 enable = 0;
2081 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082
2083 while ((cmd = next_cmd(&buf))) {
2084 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002085 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002087 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002089 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002091 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002092 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002093 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002094 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002095 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002096 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002097 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002099 res = video_autosw_set(1);
2100 if (res)
2101 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002103 res = video_autosw_set(0);
2104 if (res)
2105 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002107 res = video_outputsw_cycle();
2108 if (res)
2109 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002111 res = video_expand_toggle();
2112 if (res)
2113 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 } else
2115 return -EINVAL;
2116 }
2117
2118 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002119 status = video_outputsw_get();
2120 if (status < 0)
2121 return status;
2122 res = video_outputsw_set((status & ~disable) | enable);
2123 if (res)
2124 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 }
2126
2127 return 0;
2128}
2129
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002130static struct ibm_struct video_driver_data = {
2131 .name = "video",
2132 .read = video_read,
2133 .write = video_write,
2134 .exit = video_exit,
2135};
2136
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002137/*************************************************************************
2138 * Light (thinklight) subdriver
2139 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002141IBM_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
2142IBM_HANDLE(ledb, ec, "LEDB"); /* G4x */
2143
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002144static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002146 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
2147
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002148 IBM_ACPIHANDLE_INIT(ledb);
2149 IBM_ACPIHANDLE_INIT(lght);
2150 IBM_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002151
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002152 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002153 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002154
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002155 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002156 /* light status not supported on
2157 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002158 tp_features.light_status =
2159 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002161 vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002162 str_supported(tp_features.light));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002163
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002164 return (tp_features.light)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165}
2166
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002167static int light_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168{
2169 int len = 0;
2170 int status = 0;
2171
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002172 if (!tp_features.light) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002173 len += sprintf(p + len, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002174 } else if (!tp_features.light_status) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002175 len += sprintf(p + len, "status:\t\tunknown\n");
2176 len += sprintf(p + len, "commands:\ton, off\n");
2177 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
2179 return -EIO;
2180 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002181 len += sprintf(p + len, "commands:\ton, off\n");
2182 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183
2184 return len;
2185}
2186
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002187static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188{
2189 int cmos_cmd, lght_cmd;
2190 char *cmd;
2191 int success;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002192
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002193 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002194 return -ENODEV;
2195
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 while ((cmd = next_cmd(&buf))) {
2197 if (strlencmp(cmd, "on") == 0) {
2198 cmos_cmd = 0x0c;
2199 lght_cmd = 1;
2200 } else if (strlencmp(cmd, "off") == 0) {
2201 cmos_cmd = 0x0d;
2202 lght_cmd = 0;
2203 } else
2204 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002205
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 success = cmos_handle ?
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002207 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
2208 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 if (!success)
2210 return -EIO;
2211 }
2212
2213 return 0;
2214}
2215
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002216static struct ibm_struct light_driver_data = {
2217 .name = "light",
2218 .read = light_read,
2219 .write = light_write,
2220};
2221
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002222/*************************************************************************
2223 * Dock subdriver
2224 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03002226#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002227
2228IBM_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
2229 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
2230 "\\_SB.PCI0.PCI1.DOCK", /* all others */
2231 "\\_SB.PCI.ISA.SLCE", /* 570 */
2232 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
2233
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002234/* don't list other alternatives as we install a notify handler on the 570 */
2235IBM_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
2236
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002237static const struct acpi_device_id ibm_pci_device_ids[] = {
2238 {PCI_ROOT_HID_STRING, 0},
2239 {"", 0},
2240};
2241
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03002242static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
2243 {
2244 .notify = dock_notify,
2245 .handle = &dock_handle,
2246 .type = ACPI_SYSTEM_NOTIFY,
2247 },
2248 {
Henrique de Moraes Holschuh996fba02007-07-18 23:45:40 -03002249 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
2250 * We just use it to get notifications of dock hotplug
2251 * in very old thinkpads */
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002252 .hid = ibm_pci_device_ids,
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03002253 .notify = dock_notify,
2254 .handle = &pci_handle,
2255 .type = ACPI_SYSTEM_NOTIFY,
2256 },
2257};
2258
2259static struct ibm_struct dock_driver_data[2] = {
2260 {
2261 .name = "dock",
2262 .read = dock_read,
2263 .write = dock_write,
2264 .acpi = &ibm_dock_acpidriver[0],
2265 },
2266 {
2267 .name = "dock",
2268 .acpi = &ibm_dock_acpidriver[1],
2269 },
2270};
2271
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272#define dock_docked() (_sta(dock_handle) & 1)
2273
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002274static int __init dock_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002275{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002276 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
2277
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002278 IBM_ACPIHANDLE_INIT(dock);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002279
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002280 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
2281 str_supported(dock_handle != NULL));
2282
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002283 return (dock_handle)? 0 : 1;
2284}
2285
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03002286static int __init dock_init2(struct ibm_init_struct *iibm)
2287{
2288 int dock2_needed;
2289
2290 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
2291
2292 if (dock_driver_data[0].flags.acpi_driver_registered &&
2293 dock_driver_data[0].flags.acpi_notify_installed) {
2294 IBM_ACPIHANDLE_INIT(pci);
2295 dock2_needed = (pci_handle != NULL);
2296 vdbg_printk(TPACPI_DBG_INIT,
2297 "dock PCI handler for the TP 570 is %s\n",
2298 str_supported(dock2_needed));
2299 } else {
2300 vdbg_printk(TPACPI_DBG_INIT,
2301 "dock subdriver part 2 not required\n");
2302 dock2_needed = 0;
2303 }
2304
2305 return (dock2_needed)? 0 : 1;
2306}
2307
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002308static void dock_notify(struct ibm_struct *ibm, u32 event)
2309{
2310 int docked = dock_docked();
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002311 int pci = ibm->acpi->hid && ibm->acpi->device &&
2312 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
Zhang Rui962ce8c2007-08-23 01:24:31 +08002313 int data;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002314
2315 if (event == 1 && !pci) /* 570 */
Zhang Rui962ce8c2007-08-23 01:24:31 +08002316 data = 1; /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002317 else if (event == 1 && pci) /* 570 */
Zhang Rui962ce8c2007-08-23 01:24:31 +08002318 data = 3; /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002319 else if (event == 3 && docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08002320 data = 1; /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002321 else if (event == 3 && !docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08002322 data = 2; /* undock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002323 else if (event == 0 && docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08002324 data = 3; /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002325 else {
2326 printk(IBM_ERR "unknown dock event %d, status %d\n",
2327 event, _sta(dock_handle));
Zhang Rui962ce8c2007-08-23 01:24:31 +08002328 data = 0; /* unknown */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002329 }
Len Brown14e04fb32007-08-23 15:20:26 -04002330 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
Zhang Rui962ce8c2007-08-23 01:24:31 +08002331 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
2332 ibm->acpi->device->dev.bus_id,
2333 event, data);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002334}
2335
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002336static int dock_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337{
2338 int len = 0;
2339 int docked = dock_docked();
2340
2341 if (!dock_handle)
2342 len += sprintf(p + len, "status:\t\tnot supported\n");
2343 else if (!docked)
2344 len += sprintf(p + len, "status:\t\tundocked\n");
2345 else {
2346 len += sprintf(p + len, "status:\t\tdocked\n");
2347 len += sprintf(p + len, "commands:\tdock, undock\n");
2348 }
2349
2350 return len;
2351}
2352
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002353static int dock_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354{
2355 char *cmd;
2356
2357 if (!dock_docked())
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002358 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359
2360 while ((cmd = next_cmd(&buf))) {
2361 if (strlencmp(cmd, "undock") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002362 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
2363 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 return -EIO;
2365 } else if (strlencmp(cmd, "dock") == 0) {
2366 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
2367 return -EIO;
2368 } else
2369 return -EINVAL;
2370 }
2371
2372 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002373}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03002375#endif /* CONFIG_THINKPAD_ACPI_DOCK */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002377/*************************************************************************
2378 * Bay subdriver
2379 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03002381#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002382IBM_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
2383 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
2384 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
2385 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
2386 ); /* A21e, R30, R31 */
2387IBM_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
2388 "_EJ0", /* all others */
2389 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
2390IBM_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
2391 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
2392 ); /* all others */
2393IBM_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
2394 "_EJ0", /* 770x */
2395 ); /* all others */
2396
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002397static int __init bay_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002399 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
2400
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002401 IBM_ACPIHANDLE_INIT(bay);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002402 if (bay_handle)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002403 IBM_ACPIHANDLE_INIT(bay_ej);
2404 IBM_ACPIHANDLE_INIT(bay2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002405 if (bay2_handle)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002406 IBM_ACPIHANDLE_INIT(bay2_ej);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002407
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002408 tp_features.bay_status = bay_handle &&
2409 acpi_evalf(bay_handle, NULL, "_STA", "qv");
2410 tp_features.bay_status2 = bay2_handle &&
2411 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002412
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002413 tp_features.bay_eject = bay_handle && bay_ej_handle &&
2414 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
2415 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
2416 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002418 vdbg_printk(TPACPI_DBG_INIT,
2419 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002420 str_supported(tp_features.bay_status),
2421 str_supported(tp_features.bay_eject),
2422 str_supported(tp_features.bay_status2),
2423 str_supported(tp_features.bay_eject2));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002424
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002425 return (tp_features.bay_status || tp_features.bay_eject ||
2426 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427}
2428
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002429static void bay_notify(struct ibm_struct *ibm, u32 event)
2430{
Len Brown14e04fb32007-08-23 15:20:26 -04002431 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
Zhang Rui962ce8c2007-08-23 01:24:31 +08002432 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
2433 ibm->acpi->device->dev.bus_id,
2434 event, 0);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002435}
2436
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002437#define bay_occupied(b) (_sta(b##_handle) & 1)
2438
2439static int bay_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440{
2441 int len = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002442 int occupied = bay_occupied(bay);
2443 int occupied2 = bay_occupied(bay2);
2444 int eject, eject2;
2445
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002446 len += sprintf(p + len, "status:\t\t%s\n",
2447 tp_features.bay_status ?
2448 (occupied ? "occupied" : "unoccupied") :
2449 "not supported");
2450 if (tp_features.bay_status2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002451 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
2452 "occupied" : "unoccupied");
2453
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002454 eject = tp_features.bay_eject && occupied;
2455 eject2 = tp_features.bay_eject2 && occupied2;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002456
2457 if (eject && eject2)
2458 len += sprintf(p + len, "commands:\teject, eject2\n");
2459 else if (eject)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 len += sprintf(p + len, "commands:\teject\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002461 else if (eject2)
2462 len += sprintf(p + len, "commands:\teject2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463
2464 return len;
2465}
2466
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002467static int bay_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468{
2469 char *cmd;
2470
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002471 if (!tp_features.bay_eject && !tp_features.bay_eject2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002472 return -ENODEV;
2473
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002475 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002476 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
2477 return -EIO;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002478 } else if (tp_features.bay_eject2 &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002479 strlencmp(cmd, "eject2") == 0) {
2480 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 return -EIO;
2482 } else
2483 return -EINVAL;
2484 }
2485
2486 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002487}
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002488
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002489static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
2490 .notify = bay_notify,
2491 .handle = &bay_handle,
2492 .type = ACPI_SYSTEM_NOTIFY,
2493};
2494
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002495static struct ibm_struct bay_driver_data = {
2496 .name = "bay",
2497 .read = bay_read,
2498 .write = bay_write,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002499 .acpi = &ibm_bay_acpidriver,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002500};
2501
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03002502#endif /* CONFIG_THINKPAD_ACPI_BAY */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002504/*************************************************************************
2505 * CMOS subdriver
2506 */
2507
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03002508/* sysfs cmos_command -------------------------------------------------- */
2509static ssize_t cmos_command_store(struct device *dev,
2510 struct device_attribute *attr,
2511 const char *buf, size_t count)
2512{
2513 unsigned long cmos_cmd;
2514 int res;
2515
2516 if (parse_strtoul(buf, 21, &cmos_cmd))
2517 return -EINVAL;
2518
2519 res = issue_thinkpad_cmos_command(cmos_cmd);
2520 return (res)? res : count;
2521}
2522
2523static struct device_attribute dev_attr_cmos_command =
2524 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
2525
2526/* --------------------------------------------------------------------- */
2527
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002528static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002529{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03002530 int res;
2531
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002532 vdbg_printk(TPACPI_DBG_INIT,
2533 "initializing cmos commands subdriver\n");
2534
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002535 IBM_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002536
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002537 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
2538 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03002539
2540 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
2541 if (res)
2542 return res;
2543
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002544 return (cmos_handle)? 0 : 1;
2545}
2546
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03002547static void cmos_exit(void)
2548{
2549 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
2550}
2551
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002552static int cmos_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553{
2554 int len = 0;
2555
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002556 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2557 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 if (!cmos_handle)
2559 len += sprintf(p + len, "status:\t\tnot supported\n");
2560 else {
2561 len += sprintf(p + len, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002562 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 }
2564
2565 return len;
2566}
2567
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002568static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569{
2570 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03002571 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572
2573 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002574 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
2575 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 /* cmos_cmd set */
2577 } else
2578 return -EINVAL;
2579
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03002580 res = issue_thinkpad_cmos_command(cmos_cmd);
2581 if (res)
2582 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 }
2584
2585 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002586}
2587
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002588static struct ibm_struct cmos_driver_data = {
2589 .name = "cmos",
2590 .read = cmos_read,
2591 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03002592 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002593};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002594
2595/*************************************************************************
2596 * LED subdriver
2597 */
2598
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02002599static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002600
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002601IBM_HANDLE(led, ec, "SLED", /* 570 */
2602 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
2603 "LED", /* all others */
2604 ); /* R30, R31 */
2605
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002606static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002607{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002608 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
2609
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002610 IBM_ACPIHANDLE_INIT(led);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002611
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002612 if (!led_handle)
2613 /* led not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002614 led_supported = TPACPI_LED_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002615 else if (strlencmp(led_path, "SLED") == 0)
2616 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002617 led_supported = TPACPI_LED_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002618 else if (strlencmp(led_path, "SYSL") == 0)
2619 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002620 led_supported = TPACPI_LED_OLD;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002621 else
2622 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002623 led_supported = TPACPI_LED_NEW;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002624
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002625 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
2626 str_supported(led_supported), led_supported);
2627
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002628 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002629}
2630
2631#define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
2632
2633static int led_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634{
2635 int len = 0;
2636
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002637 if (!led_supported) {
2638 len += sprintf(p + len, "status:\t\tnot supported\n");
2639 return len;
2640 }
2641 len += sprintf(p + len, "status:\t\tsupported\n");
2642
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002643 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002644 /* 570 */
2645 int i, status;
2646 for (i = 0; i < 8; i++) {
2647 if (!acpi_evalf(ec_handle,
2648 &status, "GLED", "dd", 1 << i))
2649 return -EIO;
2650 len += sprintf(p + len, "%d:\t\t%s\n",
2651 i, led_status(status));
2652 }
2653 }
2654
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 len += sprintf(p + len, "commands:\t"
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002656 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657
2658 return len;
2659}
2660
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002661/* off, on, blink */
2662static const int led_sled_arg1[] = { 0, 1, 3 };
2663static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
2664static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
2665static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
2666
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002667static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668{
2669 char *cmd;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002670 int led, ind, ret;
2671
2672 if (!led_supported)
2673 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674
2675 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002676 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 return -EINVAL;
2678
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002679 if (strstr(cmd, "off")) {
2680 ind = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 } else if (strstr(cmd, "on")) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002682 ind = 1;
2683 } else if (strstr(cmd, "blink")) {
2684 ind = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 } else
2686 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002687
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002688 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002689 /* 570 */
2690 led = 1 << led;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002692 led, led_sled_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 return -EIO;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002694 } else if (led_supported == TPACPI_LED_OLD) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002695 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
2696 led = 1 << led;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002697 ret = ec_write(TPACPI_LED_EC_HLMS, led);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002698 if (ret >= 0)
2699 ret =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002700 ec_write(TPACPI_LED_EC_HLBL,
Henrique de Moraes Holschuhe062e032007-03-23 17:33:55 -03002701 led * led_exp_hlbl[ind]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002702 if (ret >= 0)
2703 ret =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002704 ec_write(TPACPI_LED_EC_HLCL,
Henrique de Moraes Holschuhe062e032007-03-23 17:33:55 -03002705 led * led_exp_hlcl[ind]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002706 if (ret < 0)
2707 return ret;
2708 } else {
2709 /* all others */
2710 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
2711 led, led_led_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002713 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 }
2715
2716 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002717}
2718
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002719static struct ibm_struct led_driver_data = {
2720 .name = "led",
2721 .read = led_read,
2722 .write = led_write,
2723};
2724
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002725/*************************************************************************
2726 * Beep subdriver
2727 */
2728
2729IBM_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
2730
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002731static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002732{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002733 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
2734
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002735 IBM_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002736
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002737 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
2738 str_supported(beep_handle != NULL));
2739
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002740 return (beep_handle)? 0 : 1;
2741}
2742
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002743static int beep_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744{
2745 int len = 0;
2746
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002747 if (!beep_handle)
2748 len += sprintf(p + len, "status:\t\tnot supported\n");
2749 else {
2750 len += sprintf(p + len, "status:\t\tsupported\n");
2751 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
2752 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753
2754 return len;
2755}
2756
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002757static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758{
2759 char *cmd;
2760 int beep_cmd;
2761
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002762 if (!beep_handle)
2763 return -ENODEV;
2764
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002766 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
2767 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 /* beep_cmd set */
2769 } else
2770 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002771 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 return -EIO;
2773 }
2774
2775 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002776}
2777
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002778static struct ibm_struct beep_driver_data = {
2779 .name = "beep",
2780 .read = beep_read,
2781 .write = beep_write,
2782};
2783
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002784/*************************************************************************
2785 * Thermal subdriver
2786 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002787
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002788static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002789
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03002790/* sysfs temp##_input -------------------------------------------------- */
2791
2792static ssize_t thermal_temp_input_show(struct device *dev,
2793 struct device_attribute *attr,
2794 char *buf)
2795{
2796 struct sensor_device_attribute *sensor_attr =
2797 to_sensor_dev_attr(attr);
2798 int idx = sensor_attr->index;
2799 s32 value;
2800 int res;
2801
2802 res = thermal_get_sensor(idx, &value);
2803 if (res)
2804 return res;
2805 if (value == TP_EC_THERMAL_TMP_NA * 1000)
2806 return -ENXIO;
2807
2808 return snprintf(buf, PAGE_SIZE, "%d\n", value);
2809}
2810
2811#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
2812 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, thermal_temp_input_show, NULL, _idxB)
2813
2814static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
2815 THERMAL_SENSOR_ATTR_TEMP(1, 0),
2816 THERMAL_SENSOR_ATTR_TEMP(2, 1),
2817 THERMAL_SENSOR_ATTR_TEMP(3, 2),
2818 THERMAL_SENSOR_ATTR_TEMP(4, 3),
2819 THERMAL_SENSOR_ATTR_TEMP(5, 4),
2820 THERMAL_SENSOR_ATTR_TEMP(6, 5),
2821 THERMAL_SENSOR_ATTR_TEMP(7, 6),
2822 THERMAL_SENSOR_ATTR_TEMP(8, 7),
2823 THERMAL_SENSOR_ATTR_TEMP(9, 8),
2824 THERMAL_SENSOR_ATTR_TEMP(10, 9),
2825 THERMAL_SENSOR_ATTR_TEMP(11, 10),
2826 THERMAL_SENSOR_ATTR_TEMP(12, 11),
2827 THERMAL_SENSOR_ATTR_TEMP(13, 12),
2828 THERMAL_SENSOR_ATTR_TEMP(14, 13),
2829 THERMAL_SENSOR_ATTR_TEMP(15, 14),
2830 THERMAL_SENSOR_ATTR_TEMP(16, 15),
2831};
2832
2833#define THERMAL_ATTRS(X) \
2834 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
2835
2836static struct attribute *thermal_temp_input_attr[] = {
2837 THERMAL_ATTRS(8),
2838 THERMAL_ATTRS(9),
2839 THERMAL_ATTRS(10),
2840 THERMAL_ATTRS(11),
2841 THERMAL_ATTRS(12),
2842 THERMAL_ATTRS(13),
2843 THERMAL_ATTRS(14),
2844 THERMAL_ATTRS(15),
2845 THERMAL_ATTRS(0),
2846 THERMAL_ATTRS(1),
2847 THERMAL_ATTRS(2),
2848 THERMAL_ATTRS(3),
2849 THERMAL_ATTRS(4),
2850 THERMAL_ATTRS(5),
2851 THERMAL_ATTRS(6),
2852 THERMAL_ATTRS(7),
2853 NULL
2854};
2855
2856static const struct attribute_group thermal_temp_input16_group = {
2857 .attrs = thermal_temp_input_attr
2858};
2859
2860static const struct attribute_group thermal_temp_input8_group = {
2861 .attrs = &thermal_temp_input_attr[8]
2862};
2863
2864#undef THERMAL_SENSOR_ATTR_TEMP
2865#undef THERMAL_ATTRS
2866
2867/* --------------------------------------------------------------------- */
2868
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002869static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002870{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002871 u8 t, ta1, ta2;
2872 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002873 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03002874 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002875
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002876 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
2877
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002878 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002879
Henrique de Moraes Holschuh3d6f99c2007-07-18 23:45:46 -03002880 if (thinkpad_id.ec_model) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002881 /*
2882 * Direct EC access mode: sensors at registers
2883 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
2884 * non-implemented, thermal sensors return 0x80 when
2885 * not available
2886 */
2887
2888 ta1 = ta2 = 0;
2889 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002890 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002891 ta1 |= t;
2892 } else {
2893 ta1 = 0;
2894 break;
2895 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002896 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002897 ta2 |= t;
2898 } else {
2899 ta1 = 0;
2900 break;
2901 }
2902 }
2903 if (ta1 == 0) {
2904 /* This is sheer paranoia, but we handle it anyway */
2905 if (acpi_tmp7) {
2906 printk(IBM_ERR
2907 "ThinkPad ACPI EC access misbehaving, "
2908 "falling back to ACPI TMPx access mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002909 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002910 } else {
2911 printk(IBM_ERR
2912 "ThinkPad ACPI EC access misbehaving, "
2913 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002914 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002915 }
2916 } else {
2917 thermal_read_mode =
2918 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002919 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002920 }
2921 } else if (acpi_tmp7) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002922 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
2923 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002924 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002925 } else {
2926 /* Standard ACPI TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002927 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002928 }
2929 } else {
2930 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002931 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002932 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002933
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002934 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
2935 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
2936 thermal_read_mode);
2937
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03002938 switch(thermal_read_mode) {
2939 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03002940 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03002941 &thermal_temp_input16_group);
2942 if (res)
2943 return res;
2944 break;
2945 case TPACPI_THERMAL_TPEC_8:
2946 case TPACPI_THERMAL_ACPI_TMP07:
2947 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03002948 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03002949 &thermal_temp_input8_group);
2950 if (res)
2951 return res;
2952 break;
2953 case TPACPI_THERMAL_NONE:
2954 default:
2955 return 1;
2956 }
2957
2958 return 0;
2959}
2960
2961static void thermal_exit(void)
2962{
2963 switch(thermal_read_mode) {
2964 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03002965 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03002966 &thermal_temp_input16_group);
2967 break;
2968 case TPACPI_THERMAL_TPEC_8:
2969 case TPACPI_THERMAL_ACPI_TMP07:
2970 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03002971 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03002972 &thermal_temp_input16_group);
2973 break;
2974 case TPACPI_THERMAL_NONE:
2975 default:
2976 break;
2977 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002978}
2979
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002980/* idx is zero-based */
2981static int thermal_get_sensor(int idx, s32 *value)
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002982{
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002983 int t;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002984 s8 tmp;
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002985 char tmpi[5];
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002986
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002987 t = TP_EC_THERMAL_TMP0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002988
2989 switch (thermal_read_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002990#if TPACPI_MAX_THERMAL_SENSORS >= 16
2991 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002992 if (idx >= 8 && idx <= 15) {
2993 t = TP_EC_THERMAL_TMP8;
2994 idx -= 8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002995 }
2996 /* fallthrough */
2997#endif
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002998 case TPACPI_THERMAL_TPEC_8:
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002999 if (idx <= 7) {
3000 if (!acpi_ec_read(t + idx, &tmp))
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003001 return -EIO;
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03003002 *value = tmp * 1000;
3003 return 0;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003004 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03003005 break;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003006
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003007 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03003008 if (idx <= 7) {
3009 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
3010 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
3011 return -EIO;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003012 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
3013 return -EIO;
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03003014 *value = (t - 2732) * 100;
3015 return 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003016 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03003017 break;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003018
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003019 case TPACPI_THERMAL_ACPI_TMP07:
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03003020 if (idx <= 7) {
3021 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003022 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
3023 return -EIO;
Henrique de Moraes Holschuha8fba3da2007-07-18 23:45:45 -03003024 if (t > 127 || t < -127)
3025 t = TP_EC_THERMAL_TMP_NA;
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03003026 *value = t * 1000;
3027 return 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003028 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03003029 break;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003030
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003031 case TPACPI_THERMAL_NONE:
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003032 default:
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03003033 return -ENOSYS;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003034 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03003035
3036 return -EINVAL;
3037}
3038
3039static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
3040{
3041 int res, i;
3042 int n;
3043
3044 n = 8;
3045 i = 0;
3046
3047 if (!s)
3048 return -EINVAL;
3049
3050 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
3051 n = 16;
3052
3053 for(i = 0 ; i < n; i++) {
3054 res = thermal_get_sensor(i, &s->temp[i]);
3055 if (res)
3056 return res;
3057 }
3058
3059 return n;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003060}
3061
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003062static int thermal_read(char *p)
3063{
3064 int len = 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003065 int n, i;
3066 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003067
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003068 n = thermal_get_sensors(&t);
3069 if (unlikely(n < 0))
3070 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003071
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003072 len += sprintf(p + len, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003073
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003074 if (n > 0) {
3075 for (i = 0; i < (n - 1); i++)
3076 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
3077 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
3078 } else
3079 len += sprintf(p + len, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003080
3081 return len;
3082}
3083
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003084static struct ibm_struct thermal_driver_data = {
3085 .name = "thermal",
3086 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003087 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003088};
3089
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003090/*************************************************************************
3091 * EC Dump subdriver
3092 */
3093
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003094static u8 ecdump_regs[256];
3095
3096static int ecdump_read(char *p)
3097{
3098 int len = 0;
3099 int i, j;
3100 u8 v;
3101
3102 len += sprintf(p + len, "EC "
3103 " +00 +01 +02 +03 +04 +05 +06 +07"
3104 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
3105 for (i = 0; i < 256; i += 16) {
3106 len += sprintf(p + len, "EC 0x%02x:", i);
3107 for (j = 0; j < 16; j++) {
3108 if (!acpi_ec_read(i + j, &v))
3109 break;
3110 if (v != ecdump_regs[i + j])
3111 len += sprintf(p + len, " *%02x", v);
3112 else
3113 len += sprintf(p + len, " %02x", v);
3114 ecdump_regs[i + j] = v;
3115 }
3116 len += sprintf(p + len, "\n");
3117 if (j != 16)
3118 break;
3119 }
3120
3121 /* These are way too dangerous to advertise openly... */
3122#if 0
3123 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
3124 " (<offset> is 00-ff, <value> is 00-ff)\n");
3125 len += sprintf(p + len, "commands:\t0x<offset> <value> "
3126 " (<offset> is 00-ff, <value> is 0-255)\n");
3127#endif
3128 return len;
3129}
3130
3131static int ecdump_write(char *buf)
3132{
3133 char *cmd;
3134 int i, v;
3135
3136 while ((cmd = next_cmd(&buf))) {
3137 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
3138 /* i and v set */
3139 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
3140 /* i and v set */
3141 } else
3142 return -EINVAL;
3143 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
3144 if (!acpi_ec_write(i, v))
3145 return -EIO;
3146 } else
3147 return -EINVAL;
3148 }
3149
3150 return 0;
3151}
3152
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003153static struct ibm_struct ecdump_driver_data = {
3154 .name = "ecdump",
3155 .read = ecdump_read,
3156 .write = ecdump_write,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03003157 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003158};
3159
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003160/*************************************************************************
3161 * Backlight/brightness subdriver
3162 */
Holger Macht8acb0252006-10-20 14:30:28 -07003163
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03003164static struct backlight_device *ibm_backlight_device;
Holger Macht8acb0252006-10-20 14:30:28 -07003165
Richard Purdie599a52d2007-02-10 23:07:48 +00003166static struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02003167 .get_brightness = brightness_get,
3168 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02003169};
3170
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03003171static struct mutex brightness_mutex;
3172
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02003173static int __init tpacpi_query_bcll_levels(acpi_handle handle)
3174{
3175 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
3176 union acpi_object *obj;
3177 int rc;
3178
3179 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
3180 obj = (union acpi_object *)buffer.pointer;
3181 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
3182 printk(IBM_ERR "Unknown BCLL data, "
3183 "please report this to %s\n", IBM_MAIL);
3184 rc = 0;
3185 } else {
3186 rc = obj->package.count;
3187 }
3188 } else {
3189 return 0;
3190 }
3191
3192 kfree(buffer.pointer);
3193 return rc;
3194}
3195
3196static acpi_status __init brightness_find_bcll(acpi_handle handle, u32 lvl,
3197 void *context, void **rv)
3198{
3199 char name[ACPI_PATH_SEGMENT_LENGTH];
3200 struct acpi_buffer buffer = { sizeof(name), &name };
3201
3202 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
3203 !strncmp("BCLL", name, sizeof(name) - 1)) {
3204 if (tpacpi_query_bcll_levels(handle) == 16) {
3205 *rv = handle;
3206 return AE_CTRL_TERMINATE;
3207 } else {
3208 return AE_OK;
3209 }
3210 } else {
3211 return AE_OK;
3212 }
3213}
3214
3215static int __init brightness_check_levels(void)
3216{
3217 int status;
3218 void *found_node = NULL;
3219
3220 if (!vid_handle) {
3221 IBM_ACPIHANDLE_INIT(vid);
3222 }
3223 if (!vid_handle)
3224 return 0;
3225
3226 /* Search for a BCLL package with 16 levels */
3227 status = acpi_walk_namespace(ACPI_TYPE_PACKAGE, vid_handle, 3,
3228 brightness_find_bcll, NULL, &found_node);
3229
3230 return (ACPI_SUCCESS(status) && found_node != NULL);
3231}
3232
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02003233static acpi_status __init brightness_find_bcl(acpi_handle handle, u32 lvl,
3234 void *context, void **rv)
3235{
3236 char name[ACPI_PATH_SEGMENT_LENGTH];
3237 struct acpi_buffer buffer = { sizeof(name), &name };
3238
3239 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
3240 !strncmp("_BCL", name, sizeof(name) - 1)) {
3241 *rv = handle;
3242 return AE_CTRL_TERMINATE;
3243 } else {
3244 return AE_OK;
3245 }
3246}
3247
3248static int __init brightness_check_std_acpi_support(void)
3249{
3250 int status;
3251 void *found_node = NULL;
3252
3253 if (!vid_handle) {
3254 IBM_ACPIHANDLE_INIT(vid);
3255 }
3256 if (!vid_handle)
3257 return 0;
3258
3259 /* Search for a _BCL method, but don't execute it */
3260 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
3261 brightness_find_bcl, NULL, &found_node);
3262
3263 return (ACPI_SUCCESS(status) && found_node != NULL);
3264}
3265
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003266static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02003267{
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02003268 int b;
3269
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003270 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
3271
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03003272 mutex_init(&brightness_mutex);
3273
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02003274 if (!brightness_enable) {
3275 dbg_printk(TPACPI_DBG_INIT,
3276 "brightness support disabled by module parameter\n");
3277 return 1;
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02003278 } else if (brightness_enable > 1) {
3279 if (brightness_check_std_acpi_support()) {
3280 printk(IBM_NOTICE
3281 "standard ACPI backlight interface available, not loading native one...\n");
3282 return 1;
3283 }
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02003284 }
3285
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03003286 if (!brightness_mode) {
3287 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
3288 brightness_mode = 2;
3289 else
3290 brightness_mode = 3;
3291
3292 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
3293 brightness_mode);
3294 }
3295
3296 if (brightness_mode > 3)
3297 return -EINVAL;
3298
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02003299 tp_features.bright_16levels =
3300 thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO &&
3301 brightness_check_levels();
3302
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02003303 b = brightness_get(NULL);
3304 if (b < 0)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03003305 return 1;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02003306
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02003307 if (tp_features.bright_16levels)
3308 printk(IBM_INFO "detected a 16-level brightness capable ThinkPad\n");
3309
Henrique de Moraes Holschuh7d5a0152007-04-24 11:48:20 -03003310 ibm_backlight_device = backlight_device_register(
3311 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
3312 &ibm_backlight_data);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02003313 if (IS_ERR(ibm_backlight_device)) {
3314 printk(IBM_ERR "Could not register backlight device\n");
3315 return PTR_ERR(ibm_backlight_device);
3316 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003317 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02003318
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02003319 ibm_backlight_device->props.max_brightness =
3320 (tp_features.bright_16levels)? 15 : 7;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02003321 ibm_backlight_device->props.brightness = b;
3322 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00003323
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02003324 return 0;
3325}
3326
3327static void brightness_exit(void)
3328{
3329 if (ibm_backlight_device) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003330 vdbg_printk(TPACPI_DBG_EXIT,
3331 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02003332 backlight_device_unregister(ibm_backlight_device);
3333 ibm_backlight_device = NULL;
3334 }
3335}
3336
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003337static int brightness_update_status(struct backlight_device *bd)
3338{
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02003339 /* it is the backlight class's job (caller) to handle
3340 * EINTR and other errors properly */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003341 return brightness_set(
3342 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
3343 bd->props.power == FB_BLANK_UNBLANK) ?
3344 bd->props.brightness : 0);
3345}
3346
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03003347/*
3348 * ThinkPads can read brightness from two places: EC 0x31, or
3349 * CMOS NVRAM byte 0x5E, bits 0-3.
3350 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003351static int brightness_get(struct backlight_device *bd)
3352{
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03003353 u8 lec = 0, lcmos = 0, level = 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003354
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03003355 if (brightness_mode & 1) {
3356 if (!acpi_ec_read(brightness_offset, &lec))
3357 return -EIO;
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02003358 lec &= (tp_features.bright_16levels)? 0x0f : 0x07;
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03003359 level = lec;
3360 };
3361 if (brightness_mode & 2) {
3362 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
3363 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
3364 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02003365 lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03003366 level = lcmos;
3367 }
3368
3369 if (brightness_mode == 3 && lec != lcmos) {
3370 printk(IBM_ERR
3371 "CMOS NVRAM (%u) and EC (%u) do not agree "
3372 "on display brightness level\n",
3373 (unsigned int) lcmos,
3374 (unsigned int) lec);
3375 return -EIO;
3376 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003377
3378 return level;
3379}
3380
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02003381/* May return EINTR which can always be mapped to ERESTARTSYS */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003382static int brightness_set(int value)
3383{
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03003384 int cmos_cmd, inc, i, res;
3385 int current_value;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003386
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02003387 if (value > ((tp_features.bright_16levels)? 15 : 7))
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03003388 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003389
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03003390 res = mutex_lock_interruptible(&brightness_mutex);
3391 if (res < 0)
3392 return res;
3393
3394 current_value = brightness_get(NULL);
3395 if (current_value < 0) {
3396 res = current_value;
3397 goto errout;
3398 }
3399
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03003400 cmos_cmd = value > current_value ?
3401 TP_CMOS_BRIGHTNESS_UP :
3402 TP_CMOS_BRIGHTNESS_DOWN;
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02003403 inc = (value > current_value)? 1 : -1;
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03003404
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03003405 res = 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003406 for (i = current_value; i != value; i += inc) {
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03003407 if ((brightness_mode & 2) &&
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03003408 issue_thinkpad_cmos_command(cmos_cmd)) {
3409 res = -EIO;
3410 goto errout;
3411 }
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03003412 if ((brightness_mode & 1) &&
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03003413 !acpi_ec_write(brightness_offset, i + inc)) {
3414 res = -EIO;
3415 goto errout;;
3416 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003417 }
3418
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03003419errout:
3420 mutex_unlock(&brightness_mutex);
3421 return res;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003422}
3423
3424static int brightness_read(char *p)
3425{
3426 int len = 0;
3427 int level;
3428
3429 if ((level = brightness_get(NULL)) < 0) {
3430 len += sprintf(p + len, "level:\t\tunreadable\n");
3431 } else {
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02003432 len += sprintf(p + len, "level:\t\t%d\n", level);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003433 len += sprintf(p + len, "commands:\tup, down\n");
3434 len += sprintf(p + len, "commands:\tlevel <level>"
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02003435 " (<level> is 0-%d)\n",
3436 (tp_features.bright_16levels) ? 15 : 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003437 }
3438
3439 return len;
3440}
3441
3442static int brightness_write(char *buf)
3443{
3444 int level;
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02003445 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003446 char *cmd;
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02003447 int max_level = (tp_features.bright_16levels) ? 15 : 7;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003448
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02003449 level = brightness_get(NULL);
3450 if (level < 0)
3451 return level;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003452
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02003453 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003454 if (strlencmp(cmd, "up") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02003455 if (level < max_level)
3456 level++;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003457 } else if (strlencmp(cmd, "down") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02003458 if (level > 0)
3459 level--;
3460 } else if (sscanf(cmd, "level %d", &level) == 1 &&
3461 level >= 0 && level <= max_level) {
3462 /* new level set */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003463 } else
3464 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003465 }
3466
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02003467 /*
3468 * Now we know what the final level should be, so we try to set it.
3469 * Doing it this way makes the syscall restartable in case of EINTR
3470 */
3471 rc = brightness_set(level);
3472 return (rc == -EINTR)? ERESTARTSYS : rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003473}
3474
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003475static struct ibm_struct brightness_driver_data = {
3476 .name = "brightness",
3477 .read = brightness_read,
3478 .write = brightness_write,
3479 .exit = brightness_exit,
3480};
3481
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003482/*************************************************************************
3483 * Volume subdriver
3484 */
3485
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003486static int volume_read(char *p)
3487{
3488 int len = 0;
3489 u8 level;
3490
3491 if (!acpi_ec_read(volume_offset, &level)) {
3492 len += sprintf(p + len, "level:\t\tunreadable\n");
3493 } else {
3494 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
3495 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
3496 len += sprintf(p + len, "commands:\tup, down, mute\n");
3497 len += sprintf(p + len, "commands:\tlevel <level>"
3498 " (<level> is 0-15)\n");
3499 }
3500
3501 return len;
3502}
3503
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003504static int volume_write(char *buf)
3505{
3506 int cmos_cmd, inc, i;
3507 u8 level, mute;
3508 int new_level, new_mute;
3509 char *cmd;
3510
3511 while ((cmd = next_cmd(&buf))) {
3512 if (!acpi_ec_read(volume_offset, &level))
3513 return -EIO;
3514 new_mute = mute = level & 0x40;
3515 new_level = level = level & 0xf;
3516
3517 if (strlencmp(cmd, "up") == 0) {
3518 if (mute)
3519 new_mute = 0;
3520 else
3521 new_level = level == 15 ? 15 : level + 1;
3522 } else if (strlencmp(cmd, "down") == 0) {
3523 if (mute)
3524 new_mute = 0;
3525 else
3526 new_level = level == 0 ? 0 : level - 1;
3527 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
3528 new_level >= 0 && new_level <= 15) {
3529 /* new_level set */
3530 } else if (strlencmp(cmd, "mute") == 0) {
3531 new_mute = 0x40;
3532 } else
3533 return -EINVAL;
3534
3535 if (new_level != level) { /* mute doesn't change */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003536 cmos_cmd = new_level > level ? TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003537 inc = new_level > level ? 1 : -1;
3538
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03003539 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003540 !acpi_ec_write(volume_offset, level)))
3541 return -EIO;
3542
3543 for (i = level; i != new_level; i += inc)
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03003544 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003545 !acpi_ec_write(volume_offset, i + inc))
3546 return -EIO;
3547
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03003548 if (mute && (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003549 !acpi_ec_write(volume_offset,
3550 new_level + mute)))
3551 return -EIO;
3552 }
3553
3554 if (new_mute != mute) { /* level doesn't change */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003555 cmos_cmd = new_mute ? TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003556
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03003557 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003558 !acpi_ec_write(volume_offset, level + new_mute))
3559 return -EIO;
3560 }
3561 }
3562
3563 return 0;
3564}
3565
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003566static struct ibm_struct volume_driver_data = {
3567 .name = "volume",
3568 .read = volume_read,
3569 .write = volume_write,
3570};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003571
3572/*************************************************************************
3573 * Fan subdriver
3574 */
3575
3576/*
3577 * FAN ACCESS MODES
3578 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003579 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003580 * ACPI GFAN method: returns fan level
3581 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003582 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03003583 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003584 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003585 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003586 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
3587 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03003588 * EC 0x2f (HFSP) might be available *for reading*, but do not use
3589 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003590 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003591 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03003592 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
3593 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003594 *
3595 * Fan speed changes of any sort (including those caused by the
3596 * disengaged mode) are usually done slowly by the firmware as the
3597 * maximum ammount of fan duty cycle change per second seems to be
3598 * limited.
3599 *
3600 * Reading is not available if GFAN exists.
3601 * Writing is not available if SFAN exists.
3602 *
3603 * Bits
3604 * 7 automatic mode engaged;
3605 * (default operation mode of the ThinkPad)
3606 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03003607 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003608 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03003609 * the tachometer while the fan controller ramps up
3610 * the speed (which can take up to a few *minutes*).
3611 * Speeds up fan to 100% duty-cycle, which is far above
3612 * the standard RPM levels. It is not impossible that
3613 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003614 * 5-3 unused in some models. Extra bits for fan level
3615 * in others, but still useless as all values above
3616 * 7 map to the same speed as level 7 in these models.
3617 * 2-0 fan level (0..7 usually)
3618 * 0x00 = stop
3619 * 0x07 = max (set when temperatures critical)
3620 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003621 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003622 *
3623 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
3624 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
3625 * does so, its initial value is meaningless (0x07).
3626 *
3627 * For firmware bugs, refer to:
3628 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
3629 *
3630 * ----
3631 *
3632 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
3633 * Main fan tachometer reading (in RPM)
3634 *
3635 * This register is present on all ThinkPads with a new-style EC, and
3636 * it is known not to be present on the A21m/e, and T22, as there is
3637 * something else in offset 0x84 according to the ACPI DSDT. Other
3638 * ThinkPads from this same time period (and earlier) probably lack the
3639 * tachometer as well.
3640 *
3641 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
3642 * was never fixed by IBM to report the EC firmware version string
3643 * probably support the tachometer (like the early X models), so
3644 * detecting it is quite hard. We need more data to know for sure.
3645 *
3646 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
3647 * might result.
3648 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03003649 * FIRMWARE BUG: may go stale while the EC is switching to full speed
3650 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003651 *
3652 * For firmware bugs, refer to:
3653 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
3654 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003655 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003656 * ThinkPad X31, X40, X41. Not available in the X60.
3657 *
3658 * FANS ACPI handle: takes three arguments: low speed, medium speed,
3659 * high speed. ACPI DSDT seems to map these three speeds to levels
3660 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
3661 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
3662 *
3663 * The speeds are stored on handles
3664 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
3665 *
3666 * There are three default speed sets, acessible as handles:
3667 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
3668 *
3669 * ACPI DSDT switches which set is in use depending on various
3670 * factors.
3671 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003672 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003673 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
3674 * but the ACPI tables just mention level 7.
3675 */
3676
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003677static enum fan_status_access_mode fan_status_access_mode;
3678static enum fan_control_access_mode fan_control_access_mode;
3679static enum fan_control_commands fan_control_commands;
3680
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02003681static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003682static u8 fan_control_desired_level;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02003683
Len Brown25c68a32006-12-08 04:43:41 -05003684static void fan_watchdog_fire(struct work_struct *ignored);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003685static int fan_watchdog_maxinterval;
Len Brown25c68a32006-12-08 04:43:41 -05003686static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003687
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003688IBM_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
3689IBM_HANDLE(gfan, ec, "GFAN", /* 570 */
3690 "\\FSPD", /* 600e/x, 770e, 770x */
3691 ); /* all others */
3692IBM_HANDLE(sfan, ec, "SFAN", /* 570 */
3693 "JFNS", /* 770x-JL */
3694 ); /* all others */
3695
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003696/*
3697 * SYSFS fan layout: hwmon compatible (device)
3698 *
3699 * pwm*_enable:
3700 * 0: "disengaged" mode
3701 * 1: manual mode
3702 * 2: native EC "auto" mode (recommended, hardware default)
3703 *
3704 * pwm*: set speed in manual mode, ignored otherwise.
3705 * 0 is level 0; 255 is level 7. Intermediate points done with linear
3706 * interpolation.
3707 *
3708 * fan*_input: tachometer reading, RPM
3709 *
3710 *
3711 * SYSFS fan layout: extensions
3712 *
3713 * fan_watchdog (driver):
3714 * fan watchdog interval in seconds, 0 disables (default), max 120
3715 */
3716
3717/* sysfs fan pwm1_enable ----------------------------------------------- */
3718static ssize_t fan_pwm1_enable_show(struct device *dev,
3719 struct device_attribute *attr,
3720 char *buf)
3721{
3722 int res, mode;
3723 u8 status;
3724
3725 res = fan_get_status_safe(&status);
3726 if (res)
3727 return res;
3728
3729 if (unlikely(tp_features.fan_ctrl_status_undef)) {
3730 if (status != fan_control_initial_status) {
3731 tp_features.fan_ctrl_status_undef = 0;
3732 } else {
3733 /* Return most likely status. In fact, it
3734 * might be the only possible status */
3735 status = TP_EC_FAN_AUTO;
3736 }
3737 }
3738
3739 if (status & TP_EC_FAN_FULLSPEED) {
3740 mode = 0;
3741 } else if (status & TP_EC_FAN_AUTO) {
3742 mode = 2;
3743 } else
3744 mode = 1;
3745
3746 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
3747}
3748
3749static ssize_t fan_pwm1_enable_store(struct device *dev,
3750 struct device_attribute *attr,
3751 const char *buf, size_t count)
3752{
3753 unsigned long t;
3754 int res, level;
3755
3756 if (parse_strtoul(buf, 2, &t))
3757 return -EINVAL;
3758
3759 switch (t) {
3760 case 0:
3761 level = TP_EC_FAN_FULLSPEED;
3762 break;
3763 case 1:
3764 level = TPACPI_FAN_LAST_LEVEL;
3765 break;
3766 case 2:
3767 level = TP_EC_FAN_AUTO;
3768 break;
3769 case 3:
3770 /* reserved for software-controlled auto mode */
3771 return -ENOSYS;
3772 default:
3773 return -EINVAL;
3774 }
3775
3776 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03003777 if (res == -ENXIO)
3778 return -EINVAL;
3779 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003780 return res;
3781
3782 fan_watchdog_reset();
3783
3784 return count;
3785}
3786
3787static struct device_attribute dev_attr_fan_pwm1_enable =
3788 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
3789 fan_pwm1_enable_show, fan_pwm1_enable_store);
3790
3791/* sysfs fan pwm1 ------------------------------------------------------ */
3792static ssize_t fan_pwm1_show(struct device *dev,
3793 struct device_attribute *attr,
3794 char *buf)
3795{
3796 int res;
3797 u8 status;
3798
3799 res = fan_get_status_safe(&status);
3800 if (res)
3801 return res;
3802
3803 if (unlikely(tp_features.fan_ctrl_status_undef)) {
3804 if (status != fan_control_initial_status) {
3805 tp_features.fan_ctrl_status_undef = 0;
3806 } else {
3807 status = TP_EC_FAN_AUTO;
3808 }
3809 }
3810
3811 if ((status &
3812 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
3813 status = fan_control_desired_level;
3814
3815 if (status > 7)
3816 status = 7;
3817
3818 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
3819}
3820
3821static ssize_t fan_pwm1_store(struct device *dev,
3822 struct device_attribute *attr,
3823 const char *buf, size_t count)
3824{
3825 unsigned long s;
3826 int rc;
3827 u8 status, newlevel;
3828
3829 if (parse_strtoul(buf, 255, &s))
3830 return -EINVAL;
3831
3832 /* scale down from 0-255 to 0-7 */
3833 newlevel = (s >> 5) & 0x07;
3834
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02003835 if (mutex_lock_interruptible(&fan_mutex))
3836 return -ERESTARTSYS;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003837
3838 rc = fan_get_status(&status);
3839 if (!rc && (status &
3840 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
3841 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03003842 if (rc == -ENXIO)
3843 rc = -EINVAL;
3844 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003845 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03003846 fan_watchdog_reset();
3847 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003848 }
3849
3850 mutex_unlock(&fan_mutex);
3851 return (rc)? rc : count;
3852}
3853
3854static struct device_attribute dev_attr_fan_pwm1 =
3855 __ATTR(pwm1, S_IWUSR | S_IRUGO,
3856 fan_pwm1_show, fan_pwm1_store);
3857
3858/* sysfs fan fan1_input ------------------------------------------------ */
3859static ssize_t fan_fan1_input_show(struct device *dev,
3860 struct device_attribute *attr,
3861 char *buf)
3862{
3863 int res;
3864 unsigned int speed;
3865
3866 res = fan_get_speed(&speed);
3867 if (res < 0)
3868 return res;
3869
3870 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
3871}
3872
3873static struct device_attribute dev_attr_fan_fan1_input =
3874 __ATTR(fan1_input, S_IRUGO,
3875 fan_fan1_input_show, NULL);
3876
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03003877/* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003878static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
3879 char *buf)
3880{
3881 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
3882}
3883
3884static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
3885 const char *buf, size_t count)
3886{
3887 unsigned long t;
3888
3889 if (parse_strtoul(buf, 120, &t))
3890 return -EINVAL;
3891
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03003892 if (!fan_control_allowed)
3893 return -EPERM;
3894
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003895 fan_watchdog_maxinterval = t;
3896 fan_watchdog_reset();
3897
3898 return count;
3899}
3900
3901static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
3902 fan_fan_watchdog_show, fan_fan_watchdog_store);
3903
3904/* --------------------------------------------------------------------- */
3905static struct attribute *fan_attributes[] = {
3906 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
3907 &dev_attr_fan_fan1_input.attr,
3908 NULL
3909};
3910
3911static const struct attribute_group fan_attr_group = {
3912 .attrs = fan_attributes,
3913};
3914
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003915static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003916{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003917 int rc;
3918
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003919 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
3920
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003921 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003922 fan_status_access_mode = TPACPI_FAN_NONE;
3923 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003924 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003925 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003926 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003927 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003928
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003929 IBM_ACPIHANDLE_INIT(fans);
3930 IBM_ACPIHANDLE_INIT(gfan);
3931 IBM_ACPIHANDLE_INIT(sfan);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003932
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003933 if (gfan_handle) {
3934 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003935 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003936 } else {
3937 /* all other ThinkPads: note that even old-style
3938 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02003939 if (likely(acpi_ec_read(fan_status_offset,
3940 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003941 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02003942
3943 /* In some ThinkPads, neither the EC nor the ACPI
3944 * DSDT initialize the fan status, and it ends up
3945 * being set to 0x07 when it *could* be either
3946 * 0x07 or 0x80.
3947 *
3948 * Enable for TP-1Y (T43), TP-78 (R51e),
3949 * TP-76 (R52), TP-70 (T43, R52), which are known
3950 * to be buggy. */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03003951 if (fan_control_initial_status == 0x07) {
3952 switch (thinkpad_id.ec_model) {
3953 case 0x5931: /* TP-1Y */
3954 case 0x3837: /* TP-78 */
3955 case 0x3637: /* TP-76 */
3956 case 0x3037: /* TP-70 */
3957 printk(IBM_NOTICE
3958 "fan_init: initial fan status is "
3959 "unknown, assuming it is in auto "
3960 "mode\n");
3961 tp_features.fan_ctrl_status_undef = 1;
3962 ;;
3963 }
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02003964 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003965 } else {
3966 printk(IBM_ERR
3967 "ThinkPad ACPI EC access misbehaving, "
3968 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003969 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003970 }
3971 }
3972
3973 if (sfan_handle) {
3974 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003975 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02003976 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003977 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003978 } else {
3979 if (!gfan_handle) {
3980 /* gfan without sfan means no fan control */
3981 /* all other models implement TP EC 0x2f control */
3982
3983 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02003984 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003985 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003986 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003987 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003988 TPACPI_FAN_CMD_SPEED |
3989 TPACPI_FAN_CMD_LEVEL |
3990 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003991 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003992 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02003993 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003994 TPACPI_FAN_CMD_LEVEL |
3995 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003996 }
3997 }
3998 }
3999
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004000 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
4001 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
4002 fan_control_access_mode != TPACPI_FAN_WR_NONE),
4003 fan_status_access_mode, fan_control_access_mode);
4004
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03004005 /* fan control master switch */
4006 if (!fan_control_allowed) {
4007 fan_control_access_mode = TPACPI_FAN_WR_NONE;
4008 fan_control_commands = 0;
4009 dbg_printk(TPACPI_DBG_INIT,
4010 "fan control features disabled by parameter\n");
4011 }
4012
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004013 /* update fan_control_desired_level */
4014 if (fan_status_access_mode != TPACPI_FAN_NONE)
4015 fan_get_status_safe(NULL);
4016
4017 if (fan_status_access_mode != TPACPI_FAN_NONE ||
4018 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03004019 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004020 &fan_attr_group);
4021 if (!(rc < 0))
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03004022 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004023 &driver_attr_fan_watchdog);
4024 if (rc < 0)
4025 return rc;
4026 return 0;
4027 } else
4028 return 1;
4029}
4030
4031/*
4032 * Call with fan_mutex held
4033 */
4034static void fan_update_desired_level(u8 status)
4035{
4036 if ((status &
4037 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
4038 if (status > 7)
4039 fan_control_desired_level = 7;
4040 else
4041 fan_control_desired_level = status;
4042 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02004043}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004044
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02004045static int fan_get_status(u8 *status)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004046{
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02004047 u8 s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004048
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02004049 /* TODO:
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004050 * Add TPACPI_FAN_RD_ACPI_FANS ? */
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02004051
Henrique de Moraes Holschuh3ef8a602006-11-24 11:47:10 -02004052 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004053 case TPACPI_FAN_RD_ACPI_GFAN:
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004054 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02004055
4056 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004057 return -EIO;
4058
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02004059 if (likely(status))
4060 *status = s & 0x07;
4061
4062 break;
4063
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004064 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02004065 /* all except 570, 600e/x, 770e, 770x */
4066 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
4067 return -EIO;
4068
4069 if (likely(status))
4070 *status = s;
4071
4072 break;
4073
4074 default:
4075 return -ENXIO;
4076 }
4077
4078 return 0;
4079}
4080
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004081static int fan_get_status_safe(u8 *status)
4082{
4083 int rc;
4084 u8 s;
4085
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02004086 if (mutex_lock_interruptible(&fan_mutex))
4087 return -ERESTARTSYS;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004088 rc = fan_get_status(&s);
4089 if (!rc)
4090 fan_update_desired_level(s);
4091 mutex_unlock(&fan_mutex);
4092
4093 if (status)
4094 *status = s;
4095
4096 return rc;
4097}
4098
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004099static void fan_exit(void)
4100{
Henrique de Moraes Holschuh99fba3f2007-04-21 11:08:44 -03004101 vdbg_printk(TPACPI_DBG_EXIT, "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004102
4103 /* FIXME: can we really do this unconditionally? */
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03004104 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
4105 driver_remove_file(&tpacpi_hwmon_pdriver.driver, &driver_attr_fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004106
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004107 cancel_delayed_work(&fan_watchdog_task);
4108 flush_scheduled_work();
4109}
4110
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02004111static int fan_get_speed(unsigned int *speed)
4112{
4113 u8 hi, lo;
4114
4115 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004116 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02004117 /* all except 570, 600e/x, 770e, 770x */
4118 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
4119 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
4120 return -EIO;
4121
4122 if (likely(speed))
4123 *speed = (hi << 8) | lo;
4124
4125 break;
4126
4127 default:
4128 return -ENXIO;
4129 }
4130
4131 return 0;
4132}
4133
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004134static void fan_watchdog_fire(struct work_struct *ignored)
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02004135{
Henrique de Moraes Holschuh99fba3f2007-04-21 11:08:44 -03004136 int rc;
4137
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03004138 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
4139 return;
4140
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004141 printk(IBM_NOTICE "fan watchdog: enabling fan\n");
Henrique de Moraes Holschuh99fba3f2007-04-21 11:08:44 -03004142 rc = fan_set_enable();
4143 if (rc < 0) {
4144 printk(IBM_ERR "fan watchdog: error %d while enabling fan, "
4145 "will try again later...\n", -rc);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004146 /* reschedule for later */
4147 fan_watchdog_reset();
4148 }
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02004149}
4150
4151static void fan_watchdog_reset(void)
4152{
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03004153 static int fan_watchdog_active;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02004154
Henrique de Moraes Holschuh4985cd02007-04-27 22:00:10 -03004155 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
4156 return;
4157
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02004158 if (fan_watchdog_active)
4159 cancel_delayed_work(&fan_watchdog_task);
4160
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03004161 if (fan_watchdog_maxinterval > 0 &&
4162 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02004163 fan_watchdog_active = 1;
4164 if (!schedule_delayed_work(&fan_watchdog_task,
4165 msecs_to_jiffies(fan_watchdog_maxinterval
4166 * 1000))) {
4167 printk(IBM_ERR "failed to schedule the fan watchdog, "
4168 "watchdog will not trigger\n");
4169 }
4170 } else
4171 fan_watchdog_active = 0;
4172}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004173
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004174static int fan_set_level(int level)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004175{
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03004176 if (!fan_control_allowed)
4177 return -EPERM;
4178
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004179 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004180 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004181 if (level >= 0 && level <= 7) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004182 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004183 return -EIO;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004184 } else
4185 return -EINVAL;
4186 break;
4187
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004188 case TPACPI_FAN_WR_ACPI_FANS:
4189 case TPACPI_FAN_WR_TPEC:
4190 if ((level != TP_EC_FAN_AUTO) &&
4191 (level != TP_EC_FAN_FULLSPEED) &&
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02004192 ((level < 0) || (level > 7)))
4193 return -EINVAL;
4194
Henrique de Moraes Holschuheaa75712007-04-24 11:48:18 -03004195 /* safety net should the EC not support AUTO
4196 * or FULLSPEED mode bits and just ignore them */
4197 if (level & TP_EC_FAN_FULLSPEED)
4198 level |= 7; /* safety min speed 7 */
4199 else if (level & TP_EC_FAN_FULLSPEED)
4200 level |= 4; /* safety min speed 4 */
4201
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004202 if (!acpi_ec_write(fan_status_offset, level))
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02004203 return -EIO;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02004204 else
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004205 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02004206 break;
4207
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004208 default:
4209 return -ENXIO;
4210 }
4211 return 0;
4212}
4213
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004214static int fan_set_level_safe(int level)
4215{
4216 int rc;
4217
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03004218 if (!fan_control_allowed)
4219 return -EPERM;
4220
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02004221 if (mutex_lock_interruptible(&fan_mutex))
4222 return -ERESTARTSYS;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004223
4224 if (level == TPACPI_FAN_LAST_LEVEL)
4225 level = fan_control_desired_level;
4226
4227 rc = fan_set_level(level);
4228 if (!rc)
4229 fan_update_desired_level(level);
4230
4231 mutex_unlock(&fan_mutex);
4232 return rc;
4233}
4234
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004235static int fan_set_enable(void)
4236{
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02004237 u8 s;
4238 int rc;
4239
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03004240 if (!fan_control_allowed)
4241 return -EPERM;
4242
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02004243 if (mutex_lock_interruptible(&fan_mutex))
4244 return -ERESTARTSYS;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004245
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004246 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004247 case TPACPI_FAN_WR_ACPI_FANS:
4248 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004249 rc = fan_get_status(&s);
4250 if (rc < 0)
4251 break;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02004252
4253 /* Don't go out of emergency fan mode */
Henrique de Moraes Holschuheaa75712007-04-24 11:48:18 -03004254 if (s != 7) {
4255 s &= 0x07;
4256 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
4257 }
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02004258
4259 if (!acpi_ec_write(fan_status_offset, s))
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004260 rc = -EIO;
4261 else {
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004262 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004263 rc = 0;
4264 }
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02004265 break;
4266
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004267 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004268 rc = fan_get_status(&s);
4269 if (rc < 0)
4270 break;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02004271
4272 s &= 0x07;
4273
4274 /* Set fan to at least level 4 */
Henrique de Moraes Holschuheaa75712007-04-24 11:48:18 -03004275 s |= 4;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02004276
4277 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004278 rc= -EIO;
4279 else
4280 rc = 0;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004281 break;
4282
4283 default:
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004284 rc = -ENXIO;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004285 }
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004286
4287 mutex_unlock(&fan_mutex);
4288 return rc;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004289}
4290
4291static int fan_set_disable(void)
4292{
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004293 int rc;
4294
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03004295 if (!fan_control_allowed)
4296 return -EPERM;
4297
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02004298 if (mutex_lock_interruptible(&fan_mutex))
4299 return -ERESTARTSYS;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004300
4301 rc = 0;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004302 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004303 case TPACPI_FAN_WR_ACPI_FANS:
4304 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004305 if (!acpi_ec_write(fan_status_offset, 0x00))
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004306 rc = -EIO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004307 else {
4308 fan_control_desired_level = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004309 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004310 }
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004311 break;
4312
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004313 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02004314 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004315 rc = -EIO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004316 else
4317 fan_control_desired_level = 0;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02004318 break;
4319
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004320 default:
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004321 rc = -ENXIO;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004322 }
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004323
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004324
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004325 mutex_unlock(&fan_mutex);
4326 return rc;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004327}
4328
4329static int fan_set_speed(int speed)
4330{
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004331 int rc;
4332
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03004333 if (!fan_control_allowed)
4334 return -EPERM;
4335
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02004336 if (mutex_lock_interruptible(&fan_mutex))
4337 return -ERESTARTSYS;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004338
4339 rc = 0;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004340 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004341 case TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004342 if (speed >= 0 && speed <= 65535) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004343 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
4344 speed, speed, speed))
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004345 rc = -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004346 } else
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004347 rc = -EINVAL;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004348 break;
4349
4350 default:
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004351 rc = -ENXIO;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004352 }
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004353
4354 mutex_unlock(&fan_mutex);
4355 return rc;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004356}
4357
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004358static int fan_read(char *p)
4359{
4360 int len = 0;
4361 int rc;
4362 u8 status;
4363 unsigned int speed = 0;
4364
4365 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004366 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004367 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004368 if ((rc = fan_get_status_safe(&status)) < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004369 return rc;
4370
4371 len += sprintf(p + len, "status:\t\t%s\n"
4372 "level:\t\t%d\n",
4373 (status != 0) ? "enabled" : "disabled", status);
4374 break;
4375
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004376 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004377 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004378 if ((rc = fan_get_status_safe(&status)) < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004379 return rc;
4380
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004381 if (unlikely(tp_features.fan_ctrl_status_undef)) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004382 if (status != fan_control_initial_status)
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004383 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004384 else
4385 /* Return most likely status. In fact, it
4386 * might be the only possible status */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004387 status = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004388 }
4389
4390 len += sprintf(p + len, "status:\t\t%s\n",
4391 (status != 0) ? "enabled" : "disabled");
4392
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004393 if ((rc = fan_get_speed(&speed)) < 0)
4394 return rc;
4395
4396 len += sprintf(p + len, "speed:\t\t%d\n", speed);
4397
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004398 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004399 /* Disengaged mode takes precedence */
4400 len += sprintf(p + len, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004401 else if (status & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004402 len += sprintf(p + len, "level:\t\tauto\n");
4403 else
4404 len += sprintf(p + len, "level:\t\t%d\n", status);
4405 break;
4406
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004407 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004408 default:
4409 len += sprintf(p + len, "status:\t\tnot supported\n");
4410 }
4411
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004412 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004413 len += sprintf(p + len, "commands:\tlevel <level>");
4414
4415 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004416 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004417 len += sprintf(p + len, " (<level> is 0-7)\n");
4418 break;
4419
4420 default:
4421 len += sprintf(p + len, " (<level> is 0-7, "
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004422 "auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004423 break;
4424 }
4425 }
4426
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004427 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004428 len += sprintf(p + len, "commands:\tenable, disable\n"
4429 "commands:\twatchdog <timeout> (<timeout> is 0 (off), "
4430 "1-120 (seconds))\n");
4431
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004432 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004433 len += sprintf(p + len, "commands:\tspeed <speed>"
4434 " (<speed> is 0-65535)\n");
4435
4436 return len;
4437}
4438
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004439static int fan_write_cmd_level(const char *cmd, int *rc)
4440{
4441 int level;
4442
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02004443 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004444 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004445 else if ((strlencmp(cmd, "level disengaged") == 0) |
4446 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004447 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02004448 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004449 return 0;
4450
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004451 if ((*rc = fan_set_level_safe(level)) == -ENXIO)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004452 printk(IBM_ERR "level command accepted for unsupported "
4453 "access mode %d", fan_control_access_mode);
4454
4455 return 1;
4456}
4457
4458static int fan_write_cmd_enable(const char *cmd, int *rc)
4459{
4460 if (strlencmp(cmd, "enable") != 0)
4461 return 0;
4462
4463 if ((*rc = fan_set_enable()) == -ENXIO)
4464 printk(IBM_ERR "enable command accepted for unsupported "
4465 "access mode %d", fan_control_access_mode);
4466
4467 return 1;
4468}
4469
4470static int fan_write_cmd_disable(const char *cmd, int *rc)
4471{
4472 if (strlencmp(cmd, "disable") != 0)
4473 return 0;
4474
4475 if ((*rc = fan_set_disable()) == -ENXIO)
4476 printk(IBM_ERR "disable command accepted for unsupported "
4477 "access mode %d", fan_control_access_mode);
4478
4479 return 1;
4480}
4481
4482static int fan_write_cmd_speed(const char *cmd, int *rc)
4483{
4484 int speed;
4485
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02004486 /* TODO:
4487 * Support speed <low> <medium> <high> ? */
4488
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004489 if (sscanf(cmd, "speed %d", &speed) != 1)
4490 return 0;
4491
4492 if ((*rc = fan_set_speed(speed)) == -ENXIO)
4493 printk(IBM_ERR "speed command accepted for unsupported "
4494 "access mode %d", fan_control_access_mode);
4495
4496 return 1;
4497}
4498
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02004499static int fan_write_cmd_watchdog(const char *cmd, int *rc)
4500{
4501 int interval;
4502
4503 if (sscanf(cmd, "watchdog %d", &interval) != 1)
4504 return 0;
4505
4506 if (interval < 0 || interval > 120)
4507 *rc = -EINVAL;
4508 else
4509 fan_watchdog_maxinterval = interval;
4510
4511 return 1;
4512}
4513
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004514static int fan_write(char *buf)
4515{
4516 char *cmd;
4517 int rc = 0;
4518
4519 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004520 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004521 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004522 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004523 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02004524 fan_write_cmd_disable(cmd, &rc) ||
4525 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004526 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004527 fan_write_cmd_speed(cmd, &rc))
4528 )
4529 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02004530 else if (!rc)
4531 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004532 }
4533
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004534 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004535}
4536
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004537static struct ibm_struct fan_driver_data = {
4538 .name = "fan",
4539 .read = fan_read,
4540 .write = fan_write,
4541 .exit = fan_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004542};
4543
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004544/****************************************************************************
4545 ****************************************************************************
4546 *
4547 * Infrastructure
4548 *
4549 ****************************************************************************
4550 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03004552/* sysfs name ---------------------------------------------------------- */
4553static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
4554 struct device_attribute *attr,
4555 char *buf)
4556{
4557 return snprintf(buf, PAGE_SIZE, "%s\n", IBM_NAME);
4558}
4559
4560static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
4561 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
4562
4563/* --------------------------------------------------------------------- */
4564
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004565/* /proc support */
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03004566static struct proc_dir_entry *proc_dir;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004567
4568/* Subdriver registry */
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004569static LIST_HEAD(tpacpi_all_drivers);
4570
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004572/*
4573 * Module and infrastructure proble, init and exit handling
4574 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004576#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004577static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004578{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004579 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004580
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004581 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004582}
4583#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
4584
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004585static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586{
4587 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004588 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589 struct proc_dir_entry *entry;
4590
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004591 BUG_ON(ibm == NULL);
4592
4593 INIT_LIST_HEAD(&ibm->all_drivers);
4594
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004595 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596 return 0;
4597
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004598 dbg_printk(TPACPI_DBG_INIT,
4599 "probing for %s\n", ibm->name);
4600
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004601 if (iibm->init) {
4602 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004603 if (ret > 0)
4604 return 0; /* probe failed */
4605 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004607
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004608 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609 }
4610
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004611 if (ibm->acpi) {
4612 if (ibm->acpi->hid) {
4613 ret = register_tpacpi_subdriver(ibm);
4614 if (ret)
4615 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004616 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004617
4618 if (ibm->acpi->notify) {
4619 ret = setup_acpi_notify(ibm);
4620 if (ret == -ENODEV) {
4621 printk(IBM_NOTICE "disabling subdriver %s\n",
4622 ibm->name);
4623 ret = 0;
4624 goto err_out;
4625 }
4626 if (ret < 0)
4627 goto err_out;
4628 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004629 }
4630
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004631 dbg_printk(TPACPI_DBG_INIT,
4632 "%s installed\n", ibm->name);
4633
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004634 if (ibm->read) {
4635 entry = create_proc_entry(ibm->name,
4636 S_IFREG | S_IRUGO | S_IWUSR,
4637 proc_dir);
4638 if (!entry) {
4639 printk(IBM_ERR "unable to create proc entry %s\n",
4640 ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004641 ret = -ENODEV;
4642 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004643 }
4644 entry->owner = THIS_MODULE;
4645 entry->data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004646 entry->read_proc = &dispatch_procfs_read;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004647 if (ibm->write)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004648 entry->write_proc = &dispatch_procfs_write;
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004649 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004650 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004652 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
4653
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004655
4656err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004657 dbg_printk(TPACPI_DBG_INIT,
4658 "%s: at error exit path with result %d\n",
4659 ibm->name, ret);
4660
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004661 ibm_exit(ibm);
4662 return (ret < 0)? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663}
4664
4665static void ibm_exit(struct ibm_struct *ibm)
4666{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004667 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004668
4669 list_del_init(&ibm->all_drivers);
4670
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004671 if (ibm->flags.acpi_notify_installed) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004672 dbg_printk(TPACPI_DBG_EXIT,
4673 "%s: acpi_remove_notify_handler\n", ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004674 BUG_ON(!ibm->acpi);
4675 acpi_remove_notify_handler(*ibm->acpi->handle,
4676 ibm->acpi->type,
4677 dispatch_acpi_notify);
4678 ibm->flags.acpi_notify_installed = 0;
4679 ibm->flags.acpi_notify_installed = 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004680 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004682 if (ibm->flags.proc_created) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004683 dbg_printk(TPACPI_DBG_EXIT,
4684 "%s: remove_proc_entry\n", ibm->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685 remove_proc_entry(ibm->name, proc_dir);
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004686 ibm->flags.proc_created = 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004687 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004689 if (ibm->flags.acpi_driver_registered) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004690 dbg_printk(TPACPI_DBG_EXIT,
4691 "%s: acpi_bus_unregister_driver\n", ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004692 BUG_ON(!ibm->acpi);
4693 acpi_bus_unregister_driver(ibm->acpi->driver);
4694 kfree(ibm->acpi->driver);
4695 ibm->acpi->driver = NULL;
4696 ibm->flags.acpi_driver_registered = 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004697 }
4698
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004699 if (ibm->flags.init_called && ibm->exit) {
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004700 ibm->exit();
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004701 ibm->flags.init_called = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004703
4704 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705}
4706
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004707/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03004709static void __init get_thinkpad_model_data(struct thinkpad_id_data *tp)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004710{
Jeff Garzik18552562007-10-03 15:15:40 -04004711 const struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02004712 char ec_fw_string[18];
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004713
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03004714 if (!tp)
4715 return;
4716
4717 memset(tp, 0, sizeof(*tp));
4718
4719 if (dmi_name_in_vendors("IBM"))
4720 tp->vendor = PCI_VENDOR_ID_IBM;
4721 else if (dmi_name_in_vendors("LENOVO"))
4722 tp->vendor = PCI_VENDOR_ID_LENOVO;
4723 else
4724 return;
4725
4726 tp->bios_version_str = kstrdup(dmi_get_system_info(DMI_BIOS_VERSION),
4727 GFP_KERNEL);
4728 if (!tp->bios_version_str)
4729 return;
4730 tp->bios_model = tp->bios_version_str[0]
4731 | (tp->bios_version_str[1] << 8);
4732
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004733 /*
4734 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
4735 * X32 or newer, all Z series; Some models must have an
4736 * up-to-date BIOS or they will not be detected.
4737 *
4738 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
4739 */
4740 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02004741 if (sscanf(dev->name,
4742 "IBM ThinkPad Embedded Controller -[%17c",
4743 ec_fw_string) == 1) {
4744 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
4745 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03004746
4747 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
4748 tp->ec_model = ec_fw_string[0]
4749 | (ec_fw_string[1] << 8);
4750 break;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02004751 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004752 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03004753
4754 tp->model_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_VERSION),
4755 GFP_KERNEL);
4756 if (strnicmp(tp->model_str, "ThinkPad", 8) != 0) {
4757 kfree(tp->model_str);
4758 tp->model_str = NULL;
4759 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760}
4761
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004762static int __init probe_for_thinkpad(void)
4763{
4764 int is_thinkpad;
4765
4766 if (acpi_disabled)
4767 return -ENODEV;
4768
4769 /*
4770 * Non-ancient models have better DMI tagging, but very old models
4771 * don't.
4772 */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03004773 is_thinkpad = (thinkpad_id.model_str != NULL);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004774
4775 /* ec is required because many other handles are relative to it */
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004776 IBM_ACPIHANDLE_INIT(ec);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004777 if (!ec_handle) {
4778 if (is_thinkpad)
4779 printk(IBM_ERR
4780 "Not yet supported ThinkPad detected!\n");
4781 return -ENODEV;
4782 }
4783
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03004784 /*
4785 * Risks a regression on very old machines, but reduces potential
4786 * false positives a damn great deal
4787 */
4788 if (!is_thinkpad)
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03004789 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03004790
4791 if (!is_thinkpad && !force_load)
4792 return -ENODEV;
4793
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004794 return 0;
4795}
4796
4797
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004798/* Module init, exit, parameters */
4799
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004800static struct ibm_init_struct ibms_init[] __initdata = {
4801 {
4802 .init = thinkpad_acpi_driver_init,
4803 .data = &thinkpad_acpi_driver_data,
4804 },
4805 {
4806 .init = hotkey_init,
4807 .data = &hotkey_driver_data,
4808 },
4809 {
4810 .init = bluetooth_init,
4811 .data = &bluetooth_driver_data,
4812 },
4813 {
4814 .init = wan_init,
4815 .data = &wan_driver_data,
4816 },
4817 {
4818 .init = video_init,
4819 .data = &video_driver_data,
4820 },
4821 {
4822 .init = light_init,
4823 .data = &light_driver_data,
4824 },
4825#ifdef CONFIG_THINKPAD_ACPI_DOCK
4826 {
4827 .init = dock_init,
4828 .data = &dock_driver_data[0],
4829 },
4830 {
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03004831 .init = dock_init2,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004832 .data = &dock_driver_data[1],
4833 },
4834#endif
4835#ifdef CONFIG_THINKPAD_ACPI_BAY
4836 {
4837 .init = bay_init,
4838 .data = &bay_driver_data,
4839 },
4840#endif
4841 {
4842 .init = cmos_init,
4843 .data = &cmos_driver_data,
4844 },
4845 {
4846 .init = led_init,
4847 .data = &led_driver_data,
4848 },
4849 {
4850 .init = beep_init,
4851 .data = &beep_driver_data,
4852 },
4853 {
4854 .init = thermal_init,
4855 .data = &thermal_driver_data,
4856 },
4857 {
4858 .data = &ecdump_driver_data,
4859 },
4860 {
4861 .init = brightness_init,
4862 .data = &brightness_driver_data,
4863 },
4864 {
4865 .data = &volume_driver_data,
4866 },
4867 {
4868 .init = fan_init,
4869 .data = &fan_driver_data,
4870 },
4871};
4872
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004873static int __init set_ibm_param(const char *val, struct kernel_param *kp)
4874{
4875 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004876 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004877
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02004878 if (!kp || !kp->name || !val)
4879 return -EINVAL;
4880
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004881 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
4882 ibm = ibms_init[i].data;
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02004883 WARN_ON(ibm == NULL);
4884
4885 if (!ibm || !ibm->name)
4886 continue;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004887
4888 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
4889 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004890 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004891 strcpy(ibms_init[i].param, val);
4892 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004893 return 0;
4894 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004895 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004896
4897 return -EINVAL;
4898}
4899
4900static int experimental;
4901module_param(experimental, int, 0);
4902
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03004903static u32 dbg_level;
4904module_param_named(debug, dbg_level, uint, 0);
4905
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03004906static int force_load;
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03004907module_param(force_load, bool, 0);
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03004908
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03004909static int fan_control_allowed;
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03004910module_param_named(fan_control, fan_control_allowed, bool, 0);
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03004911
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03004912static int brightness_mode;
4913module_param_named(brightness_mode, brightness_mode, int, 0);
4914
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02004915static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
4916module_param(brightness_enable, uint, 0);
4917
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004918static unsigned int hotkey_report_mode;
4919module_param(hotkey_report_mode, uint, 0);
4920
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004921#define IBM_PARAM(feature) \
4922 module_param_call(feature, set_ibm_param, NULL, NULL, 0)
4923
4924IBM_PARAM(hotkey);
4925IBM_PARAM(bluetooth);
4926IBM_PARAM(video);
4927IBM_PARAM(light);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03004928#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004929IBM_PARAM(dock);
4930#endif
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03004931#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004932IBM_PARAM(bay);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03004933#endif /* CONFIG_THINKPAD_ACPI_BAY */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004934IBM_PARAM(cmos);
4935IBM_PARAM(led);
4936IBM_PARAM(beep);
4937IBM_PARAM(ecdump);
4938IBM_PARAM(brightness);
4939IBM_PARAM(volume);
4940IBM_PARAM(fan);
4941
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03004942static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004943{
4944 int ret, i;
4945
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03004946 tpacpi_lifecycle = TPACPI_LIFE_INIT;
4947
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004948 /* Parameter checking */
4949 if (hotkey_report_mode > 2)
4950 return -EINVAL;
4951
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03004952 /* Driver-level probe */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03004953
4954 get_thinkpad_model_data(&thinkpad_id);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004955 ret = probe_for_thinkpad();
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03004956 if (ret) {
4957 thinkpad_acpi_module_exit();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004958 return ret;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03004959 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03004961 /* Driver initialization */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03004962
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004963 IBM_ACPIHANDLE_INIT(ecrd);
4964 IBM_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004966 proc_dir = proc_mkdir(IBM_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967 if (!proc_dir) {
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004968 printk(IBM_ERR "unable to create proc dir " IBM_PROC_DIR);
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03004969 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004970 return -ENODEV;
4971 }
4972 proc_dir->owner = THIS_MODULE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004973
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03004974 ret = platform_driver_register(&tpacpi_pdriver);
4975 if (ret) {
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03004976 printk(IBM_ERR "unable to register main platform driver\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03004977 thinkpad_acpi_module_exit();
4978 return ret;
4979 }
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03004980 tp_features.platform_drv_registered = 1;
4981
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03004982 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
4983 if (ret) {
4984 printk(IBM_ERR "unable to register hwmon platform driver\n");
4985 thinkpad_acpi_module_exit();
4986 return ret;
4987 }
4988 tp_features.sensors_pdrv_registered = 1;
4989
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03004990 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03004991 if (!ret) {
4992 tp_features.platform_drv_attrs_registered = 1;
4993 ret = tpacpi_create_driver_attributes(&tpacpi_hwmon_pdriver.driver);
4994 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03004995 if (ret) {
4996 printk(IBM_ERR "unable to create sysfs driver attributes\n");
4997 thinkpad_acpi_module_exit();
4998 return ret;
4999 }
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03005000 tp_features.sensors_pdrv_attrs_registered = 1;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03005001
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03005002
5003 /* Device initialization */
5004 tpacpi_pdev = platform_device_register_simple(IBM_DRVR_NAME, -1,
5005 NULL, 0);
5006 if (IS_ERR(tpacpi_pdev)) {
5007 ret = PTR_ERR(tpacpi_pdev);
5008 tpacpi_pdev = NULL;
5009 printk(IBM_ERR "unable to register platform device\n");
5010 thinkpad_acpi_module_exit();
5011 return ret;
5012 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005013 tpacpi_sensors_pdev = platform_device_register_simple(
5014 IBM_HWMON_DRVR_NAME,
5015 -1, NULL, 0);
5016 if (IS_ERR(tpacpi_sensors_pdev)) {
5017 ret = PTR_ERR(tpacpi_sensors_pdev);
5018 tpacpi_sensors_pdev = NULL;
5019 printk(IBM_ERR "unable to register hwmon platform device\n");
5020 thinkpad_acpi_module_exit();
5021 return ret;
5022 }
5023 ret = device_create_file(&tpacpi_sensors_pdev->dev,
5024 &dev_attr_thinkpad_acpi_pdev_name);
5025 if (ret) {
5026 printk(IBM_ERR
5027 "unable to create sysfs hwmon device attributes\n");
5028 thinkpad_acpi_module_exit();
5029 return ret;
5030 }
5031 tp_features.sensors_pdev_attrs_registered = 1;
5032 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03005033 if (IS_ERR(tpacpi_hwmon)) {
5034 ret = PTR_ERR(tpacpi_hwmon);
5035 tpacpi_hwmon = NULL;
5036 printk(IBM_ERR "unable to register hwmon device\n");
5037 thinkpad_acpi_module_exit();
5038 return ret;
5039 }
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03005040 mutex_init(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03005041 tpacpi_inputdev = input_allocate_device();
5042 if (!tpacpi_inputdev) {
5043 printk(IBM_ERR "unable to allocate input device\n");
5044 thinkpad_acpi_module_exit();
5045 return -ENOMEM;
5046 } else {
5047 /* Prepare input device, but don't register */
5048 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
5049 tpacpi_inputdev->phys = IBM_DRVR_NAME "/input0";
5050 tpacpi_inputdev->id.bustype = BUS_HOST;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03005051 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
5052 thinkpad_id.vendor :
5053 PCI_VENDOR_ID_IBM;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03005054 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
5055 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
5056 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005057 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
5058 ret = ibm_init(&ibms_init[i]);
5059 if (ret >= 0 && *ibms_init[i].param)
5060 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005061 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03005062 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005063 return ret;
5064 }
5065 }
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03005066 ret = input_register_device(tpacpi_inputdev);
5067 if (ret < 0) {
5068 printk(IBM_ERR "unable to register input device\n");
5069 thinkpad_acpi_module_exit();
5070 return ret;
5071 } else {
5072 tp_features.input_device_registered = 1;
5073 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03005075 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005076 return 0;
5077}
5078
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03005079static void thinkpad_acpi_module_exit(void)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005080{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005081 struct ibm_struct *ibm, *itmp;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005082
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03005083 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
5084
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005085 list_for_each_entry_safe_reverse(ibm, itmp,
5086 &tpacpi_all_drivers,
5087 all_drivers) {
5088 ibm_exit(ibm);
5089 }
5090
5091 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005092
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03005093 if (tpacpi_inputdev) {
5094 if (tp_features.input_device_registered)
5095 input_unregister_device(tpacpi_inputdev);
5096 else
5097 input_free_device(tpacpi_inputdev);
5098 }
5099
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03005100 if (tpacpi_hwmon)
5101 hwmon_device_unregister(tpacpi_hwmon);
5102
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005103 if (tp_features.sensors_pdev_attrs_registered)
5104 device_remove_file(&tpacpi_sensors_pdev->dev,
5105 &dev_attr_thinkpad_acpi_pdev_name);
5106 if (tpacpi_sensors_pdev)
5107 platform_device_unregister(tpacpi_sensors_pdev);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03005108 if (tpacpi_pdev)
5109 platform_device_unregister(tpacpi_pdev);
5110
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03005111 if (tp_features.sensors_pdrv_attrs_registered)
5112 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03005113 if (tp_features.platform_drv_attrs_registered)
5114 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
5115
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005116 if (tp_features.sensors_pdrv_registered)
5117 platform_driver_unregister(&tpacpi_hwmon_pdriver);
5118
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03005119 if (tp_features.platform_drv_registered)
5120 platform_driver_unregister(&tpacpi_pdriver);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03005121
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005122 if (proc_dir)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005123 remove_proc_entry(IBM_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005124
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005125 kfree(thinkpad_id.bios_version_str);
5126 kfree(thinkpad_id.ec_version_str);
5127 kfree(thinkpad_id.model_str);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005128}
5129
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03005130module_init(thinkpad_acpi_module_init);
5131module_exit(thinkpad_acpi_module_exit);