blob: 49d4f4af759e4ec3361118c8cb5793f685877d1a [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 Holschuh94954cc2007-07-18 23:45:27 -0300138static acpi_handle root_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140#define IBM_HANDLE(object, parent, paths...) \
141 static acpi_handle object##_handle; \
142 static acpi_handle *object##_parent = &parent##_handle; \
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400143 static char *object##_path; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 static char *object##_paths[] = { paths }
145
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400146IBM_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
147 "\\_SB.PCI.ISA.EC", /* 570 */
148 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
149 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
150 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
151 "\\_SB.PCI0.ICH3.EC0", /* R31 */
152 "\\_SB.PCI0.LPC.EC", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300153 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300155IBM_HANDLE(ecrd, ec, "ECRD"); /* 570 */
156IBM_HANDLE(ecwr, ec, "ECWR"); /* 570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300158
159/*************************************************************************
160 * Misc ACPI handles
161 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400163IBM_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, T4x, X31, X40 */
164 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
165 "\\CMS", /* R40, R40e */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300166 ); /* all others */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400167
168IBM_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
169 "^HKEY", /* R30, R31 */
170 "HKEY", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300171 ); /* 570 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400172
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400173
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300174/*************************************************************************
175 * ACPI helpers
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200176 */
177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178static int acpi_evalf(acpi_handle handle,
179 void *res, char *method, char *fmt, ...)
180{
181 char *fmt0 = fmt;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400182 struct acpi_object_list params;
183 union acpi_object in_objs[IBM_MAX_ACPI_ARGS];
184 struct acpi_buffer result, *resultp;
185 union acpi_object out_obj;
186 acpi_status status;
187 va_list ap;
188 char res_type;
189 int success;
190 int quiet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192 if (!*fmt) {
193 printk(IBM_ERR "acpi_evalf() called with empty format\n");
194 return 0;
195 }
196
197 if (*fmt == 'q') {
198 quiet = 1;
199 fmt++;
200 } else
201 quiet = 0;
202
203 res_type = *(fmt++);
204
205 params.count = 0;
206 params.pointer = &in_objs[0];
207
208 va_start(ap, fmt);
209 while (*fmt) {
210 char c = *(fmt++);
211 switch (c) {
212 case 'd': /* int */
213 in_objs[params.count].integer.value = va_arg(ap, int);
214 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
215 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400216 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 default:
218 printk(IBM_ERR "acpi_evalf() called "
219 "with invalid format character '%c'\n", c);
220 return 0;
221 }
222 }
223 va_end(ap);
224
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400225 if (res_type != 'v') {
226 result.length = sizeof(out_obj);
227 result.pointer = &out_obj;
228 resultp = &result;
229 } else
230 resultp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400232 status = acpi_evaluate_object(handle, method, &params, resultp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234 switch (res_type) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400235 case 'd': /* int */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 if (res)
237 *(int *)res = out_obj.integer.value;
238 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
239 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400240 case 'v': /* void */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 success = status == AE_OK;
242 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400243 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 default:
245 printk(IBM_ERR "acpi_evalf() called "
246 "with invalid format character '%c'\n", res_type);
247 return 0;
248 }
249
250 if (!success && !quiet)
251 printk(IBM_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
252 method, fmt0, status);
253
254 return success;
255}
256
257static void __unused acpi_print_int(acpi_handle handle, char *method)
258{
259 int i;
260
261 if (acpi_evalf(handle, &i, method, "d"))
262 printk(IBM_INFO "%s = 0x%x\n", method, i);
263 else
264 printk(IBM_ERR "error calling %s\n", method);
265}
266
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300267static int acpi_ec_read(int i, u8 * p)
268{
269 int v;
270
271 if (ecrd_handle) {
272 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
273 return 0;
274 *p = v;
275 } else {
276 if (ec_read(i, p) < 0)
277 return 0;
278 }
279
280 return 1;
281}
282
283static int acpi_ec_write(int i, u8 v)
284{
285 if (ecwr_handle) {
286 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
287 return 0;
288 } else {
289 if (ec_write(i, v) < 0)
290 return 0;
291 }
292
293 return 1;
294}
295
296static int _sta(acpi_handle handle)
297{
298 int status;
299
300 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
301 status = 0;
302
303 return status;
304}
305
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -0300306static int issue_thinkpad_cmos_command(int cmos_cmd)
307{
308 if (!cmos_handle)
309 return -ENXIO;
310
311 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
312 return -EIO;
313
314 return 0;
315}
316
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300317/*************************************************************************
318 * ACPI device model
319 */
320
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300321static void drv_acpi_handle_init(char *name,
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300322 acpi_handle *handle, acpi_handle parent,
323 char **paths, int num_paths, char **path)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300324{
325 int i;
326 acpi_status status;
327
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300328 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
329 name);
330
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300331 for (i = 0; i < num_paths; i++) {
332 status = acpi_get_handle(parent, paths[i], handle);
333 if (ACPI_SUCCESS(status)) {
334 *path = paths[i];
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300335 dbg_printk(TPACPI_DBG_INIT,
336 "Found ACPI handle %s for %s\n",
337 *path, name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300338 return;
339 }
340 }
341
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300342 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
343 name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300344 *handle = NULL;
345}
346
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300347static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300348{
349 struct ibm_struct *ibm = data;
350
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300351 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
352 return;
353
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300354 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300355 return;
356
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300357 ibm->acpi->notify(ibm, event);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300358}
359
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300360static int __init setup_acpi_notify(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300361{
362 acpi_status status;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300363 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300364
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300365 BUG_ON(!ibm->acpi);
366
367 if (!*ibm->acpi->handle)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300368 return 0;
369
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300370 vdbg_printk(TPACPI_DBG_INIT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300371 "setting up ACPI notify for %s\n", ibm->name);
372
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300373 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
374 if (rc < 0) {
375 printk(IBM_ERR "acpi_bus_get_device(%s) failed: %d\n",
376 ibm->name, rc);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300377 return -ENODEV;
378 }
379
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300380 acpi_driver_data(ibm->acpi->device) = ibm;
381 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300382 IBM_ACPI_EVENT_PREFIX,
383 ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300384
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300385 status = acpi_install_notify_handler(*ibm->acpi->handle,
386 ibm->acpi->type, dispatch_acpi_notify, ibm);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300387 if (ACPI_FAILURE(status)) {
388 if (status == AE_ALREADY_EXISTS) {
389 printk(IBM_NOTICE "another device driver is already handling %s events\n",
390 ibm->name);
391 } else {
392 printk(IBM_ERR "acpi_install_notify_handler(%s) failed: %d\n",
393 ibm->name, status);
394 }
395 return -ENODEV;
396 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300397 ibm->flags.acpi_notify_installed = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300398 return 0;
399}
400
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300401static int __init tpacpi_device_add(struct acpi_device *device)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300402{
403 return 0;
404}
405
Henrique de Moraes Holschuh67001212007-04-21 11:08:25 -0300406static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300407{
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300408 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300409
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300410 dbg_printk(TPACPI_DBG_INIT,
411 "registering %s as an ACPI driver\n", ibm->name);
412
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300413 BUG_ON(!ibm->acpi);
414
415 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
416 if (!ibm->acpi->driver) {
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300417 printk(IBM_ERR "kzalloc(ibm->driver) failed\n");
418 return -ENOMEM;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300419 }
420
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300421 sprintf(ibm->acpi->driver->name, "%s_%s", IBM_NAME, ibm->name);
422 ibm->acpi->driver->ids = ibm->acpi->hid;
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200423
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300424 ibm->acpi->driver->ops.add = &tpacpi_device_add;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300425
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300426 rc = acpi_bus_register_driver(ibm->acpi->driver);
427 if (rc < 0) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300428 printk(IBM_ERR "acpi_bus_register_driver(%s) failed: %d\n",
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200429 ibm->name, rc);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300430 kfree(ibm->acpi->driver);
431 ibm->acpi->driver = NULL;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300432 } else if (!rc)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300433 ibm->flags.acpi_driver_registered = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300434
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300435 return rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300436}
437
438
439/****************************************************************************
440 ****************************************************************************
441 *
442 * Procfs Helpers
443 *
444 ****************************************************************************
445 ****************************************************************************/
446
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300447static int dispatch_procfs_read(char *page, char **start, off_t off,
448 int count, int *eof, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300449{
450 struct ibm_struct *ibm = data;
451 int len;
452
453 if (!ibm || !ibm->read)
454 return -EINVAL;
455
456 len = ibm->read(page);
457 if (len < 0)
458 return len;
459
460 if (len <= off + count)
461 *eof = 1;
462 *start = page + off;
463 len -= off;
464 if (len > count)
465 len = count;
466 if (len < 0)
467 len = 0;
468
469 return len;
470}
471
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300472static int dispatch_procfs_write(struct file *file,
473 const char __user * userbuf,
474 unsigned long count, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300475{
476 struct ibm_struct *ibm = data;
477 char *kernbuf;
478 int ret;
479
480 if (!ibm || !ibm->write)
481 return -EINVAL;
482
483 kernbuf = kmalloc(count + 2, GFP_KERNEL);
484 if (!kernbuf)
485 return -ENOMEM;
486
487 if (copy_from_user(kernbuf, userbuf, count)) {
488 kfree(kernbuf);
489 return -EFAULT;
490 }
491
492 kernbuf[count] = 0;
493 strcat(kernbuf, ",");
494 ret = ibm->write(kernbuf);
495 if (ret == 0)
496 ret = count;
497
498 kfree(kernbuf);
499
500 return ret;
501}
502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503static char *next_cmd(char **cmds)
504{
505 char *start = *cmds;
506 char *end;
507
508 while ((end = strchr(start, ',')) && end == start)
509 start = end + 1;
510
511 if (!end)
512 return NULL;
513
514 *end = 0;
515 *cmds = end + 1;
516 return start;
517}
518
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300519
520/****************************************************************************
521 ****************************************************************************
522 *
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300523 * Device model: input, hwmon and platform
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300524 *
525 ****************************************************************************
526 ****************************************************************************/
527
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -0300528static struct platform_device *tpacpi_pdev;
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300529static struct platform_device *tpacpi_sensors_pdev;
Tony Jones1beeffe2007-08-20 13:46:20 -0700530static struct device *tpacpi_hwmon;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300531static struct input_dev *tpacpi_inputdev;
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -0300532static struct mutex tpacpi_inputdev_send_mutex;
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300533
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300534
535static int tpacpi_resume_handler(struct platform_device *pdev)
536{
537 struct ibm_struct *ibm, *itmp;
538
539 list_for_each_entry_safe(ibm, itmp,
540 &tpacpi_all_drivers,
541 all_drivers) {
542 if (ibm->resume)
543 (ibm->resume)();
544 }
545
546 return 0;
547}
548
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300549static struct platform_driver tpacpi_pdriver = {
550 .driver = {
551 .name = IBM_DRVR_NAME,
552 .owner = THIS_MODULE,
553 },
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300554 .resume = tpacpi_resume_handler,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300555};
556
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300557static struct platform_driver tpacpi_hwmon_pdriver = {
558 .driver = {
559 .name = IBM_HWMON_DRVR_NAME,
560 .owner = THIS_MODULE,
561 },
562};
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300563
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -0300564/*************************************************************************
565 * thinkpad-acpi driver attributes
566 */
567
568/* interface_version --------------------------------------------------- */
569static ssize_t tpacpi_driver_interface_version_show(
570 struct device_driver *drv,
571 char *buf)
572{
573 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
574}
575
576static DRIVER_ATTR(interface_version, S_IRUGO,
577 tpacpi_driver_interface_version_show, NULL);
578
579/* debug_level --------------------------------------------------------- */
580static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
581 char *buf)
582{
583 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
584}
585
586static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
587 const char *buf, size_t count)
588{
589 unsigned long t;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -0300590
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300591 if (parse_strtoul(buf, 0xffff, &t))
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -0300592 return -EINVAL;
593
594 dbg_level = t;
595
596 return count;
597}
598
599static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
600 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
601
602/* version ------------------------------------------------------------- */
603static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
604 char *buf)
605{
606 return snprintf(buf, PAGE_SIZE, "%s v%s\n", IBM_DESC, IBM_VERSION);
607}
608
609static DRIVER_ATTR(version, S_IRUGO,
610 tpacpi_driver_version_show, NULL);
611
612/* --------------------------------------------------------------------- */
613
614static struct driver_attribute* tpacpi_driver_attributes[] = {
615 &driver_attr_debug_level, &driver_attr_version,
616 &driver_attr_interface_version,
617};
618
619static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
620{
621 int i, res;
622
623 i = 0;
624 res = 0;
625 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
626 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
627 i++;
628 }
629
630 return res;
631}
632
633static void tpacpi_remove_driver_attributes(struct device_driver *drv)
634{
635 int i;
636
637 for(i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
638 driver_remove_file(drv, tpacpi_driver_attributes[i]);
639}
640
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300641/*************************************************************************
642 * sysfs support helpers
643 */
644
645struct attribute_set_obj {
646 struct attribute_set s;
647 struct attribute *a;
648} __attribute__((packed));
649
650static struct attribute_set *create_attr_set(unsigned int max_members,
651 const char* name)
652{
653 struct attribute_set_obj *sobj;
654
655 if (max_members == 0)
656 return NULL;
657
658 /* Allocates space for implicit NULL at the end too */
659 sobj = kzalloc(sizeof(struct attribute_set_obj) +
660 max_members * sizeof(struct attribute *),
661 GFP_KERNEL);
662 if (!sobj)
663 return NULL;
664 sobj->s.max_members = max_members;
665 sobj->s.group.attrs = &sobj->a;
666 sobj->s.group.name = name;
667
668 return &sobj->s;
669}
670
671/* not multi-threaded safe, use it in a single thread per set */
672static int add_to_attr_set(struct attribute_set* s, struct attribute *attr)
673{
674 if (!s || !attr)
675 return -EINVAL;
676
677 if (s->members >= s->max_members)
678 return -ENOMEM;
679
680 s->group.attrs[s->members] = attr;
681 s->members++;
682
683 return 0;
684}
685
686static int add_many_to_attr_set(struct attribute_set* s,
687 struct attribute **attr,
688 unsigned int count)
689{
690 int i, res;
691
692 for (i = 0; i < count; i++) {
693 res = add_to_attr_set(s, attr[i]);
694 if (res)
695 return res;
696 }
697
698 return 0;
699}
700
701static void delete_attr_set(struct attribute_set* s, struct kobject *kobj)
702{
703 sysfs_remove_group(kobj, &s->group);
704 destroy_attr_set(s);
705}
706
707static int parse_strtoul(const char *buf,
708 unsigned long max, unsigned long *value)
709{
710 char *endp;
711
Henrique de Moraes Holschuh32afbf02007-10-08 10:12:56 -0300712 while (*buf && isspace(*buf))
713 buf++;
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300714 *value = simple_strtoul(buf, &endp, 0);
715 while (*endp && isspace(*endp))
716 endp++;
717 if (*endp || *value > max)
718 return -EINVAL;
719
720 return 0;
721}
722
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300723/****************************************************************************
724 ****************************************************************************
725 *
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300726 * Subdrivers
727 *
728 ****************************************************************************
729 ****************************************************************************/
730
731/*************************************************************************
Henrique de Moraes Holschuh142cfc92007-04-21 11:08:26 -0300732 * thinkpad-acpi init subdriver
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300733 */
734
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300735static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736{
737 printk(IBM_INFO "%s v%s\n", IBM_DESC, IBM_VERSION);
738 printk(IBM_INFO "%s\n", IBM_URL);
739
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -0300740 printk(IBM_INFO "ThinkPad BIOS %s, EC %s\n",
741 (thinkpad_id.bios_version_str) ?
742 thinkpad_id.bios_version_str : "unknown",
743 (thinkpad_id.ec_version_str) ?
744 thinkpad_id.ec_version_str : "unknown");
745
746 if (thinkpad_id.vendor && thinkpad_id.model_str)
747 printk(IBM_INFO "%s %s\n",
748 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
749 "IBM" : ((thinkpad_id.vendor ==
750 PCI_VENDOR_ID_LENOVO) ?
751 "Lenovo" : "Unknown vendor"),
752 thinkpad_id.model_str);
Henrique de Moraes Holschuh3945ac32007-02-06 19:13:44 -0200753
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 return 0;
755}
756
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300757static int thinkpad_acpi_driver_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758{
759 int len = 0;
760
761 len += sprintf(p + len, "driver:\t\t%s\n", IBM_DESC);
762 len += sprintf(p + len, "version:\t%s\n", IBM_VERSION);
763
764 return len;
765}
766
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300767static struct ibm_struct thinkpad_acpi_driver_data = {
768 .name = "driver",
769 .read = thinkpad_acpi_driver_read,
770};
771
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300772/*************************************************************************
773 * Hotkey subdriver
774 */
775
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400776static int hotkey_orig_status;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -0300777static u32 hotkey_orig_mask;
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -0300778static u32 hotkey_all_mask;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -0300779static u32 hotkey_reserved_mask;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -0200780static u32 hotkey_mask;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -0300781
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -0300782static u16 *hotkey_keycode_map;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400783
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -0300784static struct attribute_set *hotkey_dev_attributes;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300785
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -0300786static int hotkey_get_wlsw(int *status)
787{
788 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
789 return -EIO;
790 return 0;
791}
792
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -0200793/*
794 * Call with hotkey_mutex held
795 */
796static int hotkey_mask_get(void)
797{
798 if (tp_features.hotkey_mask) {
799 if (!acpi_evalf(hkey_handle, &hotkey_mask, "DHKN", "d"))
800 return -EIO;
801 }
802
803 return 0;
804}
805
806/*
807 * Call with hotkey_mutex held
808 */
809static int hotkey_mask_set(u32 mask)
810{
811 int i;
812 int rc = 0;
813
814 if (tp_features.hotkey_mask) {
815 for (i = 0; i < 32; i++) {
816 u32 m = 1 << i;
817 if (!acpi_evalf(hkey_handle,
818 NULL, "MHKM", "vdd", i + 1,
819 !!(mask & m))) {
820 rc = -EIO;
821 break;
822 } else {
823 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
824 }
825 }
826
827 /* hotkey_mask_get must be called unconditionally below */
828 if (!hotkey_mask_get() && !rc && hotkey_mask != mask) {
829 printk(IBM_NOTICE
830 "requested hot key mask 0x%08x, but "
831 "firmware forced it to 0x%08x\n",
832 mask, hotkey_mask);
833 }
834 }
835
836 return rc;
837}
838
839static int hotkey_status_get(int *status)
840{
841 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
842 return -EIO;
843
844 return 0;
845}
846
847static int hotkey_status_set(int status)
848{
849 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
850 return -EIO;
851
852 return 0;
853}
854
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300855/* sysfs hotkey enable ------------------------------------------------- */
856static ssize_t hotkey_enable_show(struct device *dev,
857 struct device_attribute *attr,
858 char *buf)
859{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -0300860 int res, status;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300861
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -0200862 res = hotkey_status_get(&status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300863 if (res)
864 return res;
865
866 return snprintf(buf, PAGE_SIZE, "%d\n", status);
867}
868
869static ssize_t hotkey_enable_store(struct device *dev,
870 struct device_attribute *attr,
871 const char *buf, size_t count)
872{
873 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -0200874 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300875
876 if (parse_strtoul(buf, 1, &t))
877 return -EINVAL;
878
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -0200879 res = hotkey_status_set(t);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300880
881 return (res) ? res : count;
882}
883
884static struct device_attribute dev_attr_hotkey_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -0300885 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300886 hotkey_enable_show, hotkey_enable_store);
887
888/* sysfs hotkey mask --------------------------------------------------- */
889static ssize_t hotkey_mask_show(struct device *dev,
890 struct device_attribute *attr,
891 char *buf)
892{
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -0200893 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300894
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -0200895 if (mutex_lock_interruptible(&hotkey_mutex))
896 return -ERESTARTSYS;
897 res = hotkey_mask_get();
898 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300899
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -0200900 return (res)?
901 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300902}
903
904static ssize_t hotkey_mask_store(struct device *dev,
905 struct device_attribute *attr,
906 const char *buf, size_t count)
907{
908 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -0200909 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300910
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -0300911 if (parse_strtoul(buf, 0xffffffffUL, &t))
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300912 return -EINVAL;
913
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -0200914 if (mutex_lock_interruptible(&hotkey_mutex))
915 return -ERESTARTSYS;
916
917 res = hotkey_mask_set(t);
918 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300919
920 return (res) ? res : count;
921}
922
923static struct device_attribute dev_attr_hotkey_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -0300924 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300925 hotkey_mask_show, hotkey_mask_store);
926
927/* sysfs hotkey bios_enabled ------------------------------------------- */
928static ssize_t hotkey_bios_enabled_show(struct device *dev,
929 struct device_attribute *attr,
930 char *buf)
931{
932 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
933}
934
935static struct device_attribute dev_attr_hotkey_bios_enabled =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -0300936 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300937
938/* sysfs hotkey bios_mask ---------------------------------------------- */
939static ssize_t hotkey_bios_mask_show(struct device *dev,
940 struct device_attribute *attr,
941 char *buf)
942{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -0300943 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300944}
945
946static struct device_attribute dev_attr_hotkey_bios_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -0300947 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300948
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -0300949/* sysfs hotkey all_mask ----------------------------------------------- */
950static ssize_t hotkey_all_mask_show(struct device *dev,
951 struct device_attribute *attr,
952 char *buf)
953{
954 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_all_mask);
955}
956
957static struct device_attribute dev_attr_hotkey_all_mask =
958 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
959
960/* sysfs hotkey recommended_mask --------------------------------------- */
961static ssize_t hotkey_recommended_mask_show(struct device *dev,
962 struct device_attribute *attr,
963 char *buf)
964{
965 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
966 hotkey_all_mask & ~hotkey_reserved_mask);
967}
968
969static struct device_attribute dev_attr_hotkey_recommended_mask =
970 __ATTR(hotkey_recommended_mask, S_IRUGO,
971 hotkey_recommended_mask_show, NULL);
972
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -0300973/* sysfs hotkey radio_sw ----------------------------------------------- */
974static ssize_t hotkey_radio_sw_show(struct device *dev,
975 struct device_attribute *attr,
976 char *buf)
977{
978 int res, s;
979 res = hotkey_get_wlsw(&s);
980 if (res < 0)
981 return res;
982
983 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
984}
985
986static struct device_attribute dev_attr_hotkey_radio_sw =
987 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
988
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -0300989/* sysfs hotkey report_mode -------------------------------------------- */
990static ssize_t hotkey_report_mode_show(struct device *dev,
991 struct device_attribute *attr,
992 char *buf)
993{
994 return snprintf(buf, PAGE_SIZE, "%d\n",
995 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
996}
997
998static struct device_attribute dev_attr_hotkey_report_mode =
999 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
1000
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001001/* --------------------------------------------------------------------- */
1002
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001003static struct attribute *hotkey_attributes[] __initdata = {
1004 &dev_attr_hotkey_enable.attr,
1005 &dev_attr_hotkey_report_mode.attr,
1006};
1007
1008static struct attribute *hotkey_mask_attributes[] __initdata = {
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001009 &dev_attr_hotkey_mask.attr,
1010 &dev_attr_hotkey_bios_enabled.attr,
1011 &dev_attr_hotkey_bios_mask.attr,
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001012 &dev_attr_hotkey_all_mask.attr,
1013 &dev_attr_hotkey_recommended_mask.attr,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001014};
1015
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001016static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001017{
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001018 /* Requirements for changing the default keymaps:
1019 *
1020 * 1. Many of the keys are mapped to KEY_RESERVED for very
1021 * good reasons. Do not change them unless you have deep
1022 * knowledge on the IBM and Lenovo ThinkPad firmware for
1023 * the various ThinkPad models. The driver behaves
1024 * differently for KEY_RESERVED: such keys have their
1025 * hot key mask *unset* in mask_recommended, and also
1026 * in the initial hot key mask programmed into the
1027 * firmware at driver load time, which means the firm-
1028 * ware may react very differently if you change them to
1029 * something else;
1030 *
1031 * 2. You must be subscribed to the linux-thinkpad and
1032 * ibm-acpi-devel mailing lists, and you should read the
1033 * list archives since 2007 if you want to change the
1034 * keymaps. This requirement exists so that you will
1035 * know the past history of problems with the thinkpad-
1036 * acpi driver keymaps, and also that you will be
1037 * listening to any bug reports;
1038 *
1039 * 3. Do not send thinkpad-acpi specific patches directly to
1040 * for merging, *ever*. Send them to the linux-acpi
1041 * mailinglist for comments. Merging is to be done only
1042 * through acpi-test and the ACPI maintainer.
1043 *
1044 * If the above is too much to ask, don't change the keymap.
1045 * Ask the thinkpad-acpi maintainer to do it, instead.
1046 */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001047 static u16 ibm_keycode_map[] __initdata = {
1048 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1049 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
1050 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1051 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001052
1053 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001054 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1055 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1056 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001057
1058 /* brightness: firmware always reacts to them, unless
1059 * X.org did some tricks in the radeon BIOS scratch
1060 * registers of *some* models */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02001061 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02001062 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001063
1064 /* Thinklight: firmware always react to it */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001065 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001066
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001067 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
1068 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001069
1070 /* Volume: firmware always react to it and reprograms
1071 * the built-in *extra* mixer. Never map it to control
1072 * another mixer by default. */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02001073 KEY_RESERVED, /* 0x14: VOLUME UP */
1074 KEY_RESERVED, /* 0x15: VOLUME DOWN */
1075 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001076
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001077 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001078
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001079 /* (assignments unknown, please report if found) */
1080 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1081 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1082 };
1083 static u16 lenovo_keycode_map[] __initdata = {
1084 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1085 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
1086 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1087 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001088
1089 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001090 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1091 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1092 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001093
Henrique de Moraes Holschuh56a185b2007-12-13 12:14:09 -02001094 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuh56a185b2007-12-13 12:14:09 -02001095 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001096
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001097 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001098
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001099 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
1100 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001101
1102 /* Volume: z60/z61, T60 (BIOS version?): firmware always
1103 * react to it and reprograms the built-in *extra* mixer.
1104 * Never map it to control another mixer by default.
1105 *
1106 * T60?, T61, R60?, R61: firmware and EC tries to send
1107 * these over the regular keyboard, so these are no-ops,
1108 * but there are still weird bugs re. MUTE, so do not
1109 * change unless you get test reports from all Lenovo
1110 * models. May cause the BIOS to interfere with the
1111 * HDA mixer.
1112 */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02001113 KEY_RESERVED, /* 0x14: VOLUME UP */
1114 KEY_RESERVED, /* 0x15: VOLUME DOWN */
1115 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001116
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001117 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001118
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001119 /* (assignments unknown, please report if found) */
1120 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1121 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1122 };
1123
1124#define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
1125#define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
1126#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
1127
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001128 int res, i;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001129 int status;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001130 int hkeyv;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001131
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001132 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
1133
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001134 BUG_ON(!tpacpi_inputdev);
1135
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001136 IBM_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001137 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001138
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001139 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001140 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001141
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001142 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001143 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001144
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001145 if (tp_features.hotkey) {
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001146 hotkey_dev_attributes = create_attr_set(8, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001147 if (!hotkey_dev_attributes)
1148 return -ENOMEM;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001149 res = add_many_to_attr_set(hotkey_dev_attributes,
1150 hotkey_attributes,
1151 ARRAY_SIZE(hotkey_attributes));
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001152 if (res)
1153 return res;
1154
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001155 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001156 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
1157 for HKEY interface version 0x100 */
1158 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
1159 if ((hkeyv >> 8) != 1) {
1160 printk(IBM_ERR "unknown version of the "
1161 "HKEY interface: 0x%x\n", hkeyv);
1162 printk(IBM_ERR "please report this to %s\n",
1163 IBM_MAIL);
1164 } else {
1165 /*
1166 * MHKV 0x100 in A31, R40, R40e,
1167 * T4x, X31, and later
1168 * */
1169 tp_features.hotkey_mask = 1;
1170 }
1171 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001172
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001173 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001174 str_supported(tp_features.hotkey_mask));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001175
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001176 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001177 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001178 "MHKA", "qd")) {
1179 printk(IBM_ERR
1180 "missing MHKA handler, "
1181 "please report this to %s\n",
1182 IBM_MAIL);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001183 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001184 }
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001185 }
1186
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001187 res = hotkey_status_get(&hotkey_orig_status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001188 if (!res && tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001189 res = hotkey_mask_get();
1190 hotkey_orig_mask = hotkey_mask;
1191 if (!res) {
1192 res = add_many_to_attr_set(
1193 hotkey_dev_attributes,
1194 hotkey_mask_attributes,
1195 ARRAY_SIZE(hotkey_mask_attributes));
1196 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001197 }
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001198
1199 /* Not all thinkpads have a hardware radio switch */
1200 if (!res && acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
1201 tp_features.hotkey_wlsw = 1;
1202 printk(IBM_INFO
1203 "radio switch found; radios are %s\n",
1204 enabled(status, 0));
1205 res = add_to_attr_set(hotkey_dev_attributes,
1206 &dev_attr_hotkey_radio_sw.attr);
1207 }
1208
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001209 if (!res)
1210 res = register_attr_set_with_sysfs(
1211 hotkey_dev_attributes,
1212 &tpacpi_pdev->dev.kobj);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001213 if (res)
1214 return res;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001215
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001216 /* Set up key map */
1217
1218 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
1219 GFP_KERNEL);
1220 if (!hotkey_keycode_map) {
1221 printk(IBM_ERR "failed to allocate memory for key map\n");
1222 return -ENOMEM;
1223 }
1224
1225 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
1226 dbg_printk(TPACPI_DBG_INIT,
1227 "using Lenovo default hot key map\n");
1228 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
1229 TPACPI_HOTKEY_MAP_SIZE);
1230 } else {
1231 dbg_printk(TPACPI_DBG_INIT,
1232 "using IBM default hot key map\n");
1233 memcpy(hotkey_keycode_map, &ibm_keycode_map,
1234 TPACPI_HOTKEY_MAP_SIZE);
1235 }
1236
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001237 set_bit(EV_KEY, tpacpi_inputdev->evbit);
1238 set_bit(EV_MSC, tpacpi_inputdev->evbit);
1239 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001240 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
1241 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
1242 tpacpi_inputdev->keycode = hotkey_keycode_map;
1243 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001244 if (hotkey_keycode_map[i] != KEY_RESERVED) {
1245 set_bit(hotkey_keycode_map[i],
1246 tpacpi_inputdev->keybit);
1247 } else {
1248 if (i < sizeof(hotkey_reserved_mask)*8)
1249 hotkey_reserved_mask |= 1 << i;
1250 }
1251 }
1252
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03001253 if (tp_features.hotkey_wlsw) {
1254 set_bit(EV_SW, tpacpi_inputdev->evbit);
1255 set_bit(SW_RADIO, tpacpi_inputdev->swbit);
1256 }
1257
Henrique de Moraes Holschuh1a343762007-07-18 23:45:36 -03001258 dbg_printk(TPACPI_DBG_INIT,
1259 "enabling hot key handling\n");
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001260 res = hotkey_status_set(1);
1261 if (res)
1262 return res;
1263 res = hotkey_mask_set((hotkey_all_mask & ~hotkey_reserved_mask)
Henrique de Moraes Holschuh1a343762007-07-18 23:45:36 -03001264 | hotkey_orig_mask);
1265 if (res)
1266 return res;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001267
1268 dbg_printk(TPACPI_DBG_INIT,
1269 "legacy hot key reporting over procfs %s\n",
1270 (hotkey_report_mode < 2) ?
1271 "enabled" : "disabled");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001272 }
1273
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001274 return (tp_features.hotkey)? 0 : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001275}
1276
1277static void hotkey_exit(void)
1278{
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001279 if (tp_features.hotkey) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001280 dbg_printk(TPACPI_DBG_EXIT, "restoring original hot key mask\n");
1281 /* no short-circuit boolean operator below! */
1282 if ((hotkey_mask_set(hotkey_orig_mask) |
1283 hotkey_status_set(hotkey_orig_status)) != 0)
1284 printk(IBM_ERR "failed to restore hot key mask to BIOS defaults\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001285 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001286
1287 if (hotkey_dev_attributes) {
1288 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
1289 hotkey_dev_attributes = NULL;
1290 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001291}
1292
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001293static void tpacpi_input_send_key(unsigned int scancode,
1294 unsigned int keycode)
1295{
1296 if (keycode != KEY_RESERVED) {
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03001297 mutex_lock(&tpacpi_inputdev_send_mutex);
1298
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001299 input_report_key(tpacpi_inputdev, keycode, 1);
1300 if (keycode == KEY_UNKNOWN)
1301 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1302 scancode);
1303 input_sync(tpacpi_inputdev);
1304
1305 input_report_key(tpacpi_inputdev, keycode, 0);
1306 if (keycode == KEY_UNKNOWN)
1307 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1308 scancode);
1309 input_sync(tpacpi_inputdev);
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03001310
1311 mutex_unlock(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001312 }
1313}
1314
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03001315static void tpacpi_input_send_radiosw(void)
1316{
1317 int wlsw;
1318
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03001319 mutex_lock(&tpacpi_inputdev_send_mutex);
1320
Henrique de Moraes Holschuhf4e1e432007-09-23 11:39:00 -03001321 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03001322 input_report_switch(tpacpi_inputdev,
1323 SW_RADIO, !!wlsw);
Henrique de Moraes Holschuhf4e1e432007-09-23 11:39:00 -03001324 input_sync(tpacpi_inputdev);
1325 }
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03001326
1327 mutex_unlock(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03001328}
1329
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001330static void hotkey_notify(struct ibm_struct *ibm, u32 event)
1331{
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001332 u32 hkey;
1333 unsigned int keycode, scancode;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03001334 int send_acpi_ev;
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03001335 int ignore_acpi_ev;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001336
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03001337 if (event != 0x80) {
1338 printk(IBM_ERR "unknown HKEY notification event %d\n", event);
1339 /* forward it to userspace, maybe it knows how to handle it */
1340 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
1341 ibm->acpi->device->dev.bus_id,
1342 event, 0);
1343 return;
1344 }
1345
1346 while (1) {
1347 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
1348 printk(IBM_ERR "failed to retrieve HKEY event\n");
1349 return;
1350 }
1351
1352 if (hkey == 0) {
1353 /* queue empty */
1354 return;
1355 }
1356
1357 send_acpi_ev = 0;
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03001358 ignore_acpi_ev = 0;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03001359
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001360 switch (hkey >> 12) {
1361 case 1:
1362 /* 0x1000-0x1FFF: key presses */
1363 scancode = hkey & 0xfff;
1364 if (scancode > 0 && scancode < 0x21) {
1365 scancode--;
1366 keycode = hotkey_keycode_map[scancode];
1367 tpacpi_input_send_key(scancode, keycode);
1368 } else {
1369 printk(IBM_ERR
1370 "hotkey 0x%04x out of range for keyboard map\n",
1371 hkey);
1372 send_acpi_ev = 1;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001373 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001374 break;
1375 case 5:
1376 /* 0x5000-0x5FFF: LID */
1377 /* we don't handle it through this path, just
1378 * eat up known LID events */
1379 if (hkey != 0x5001 && hkey != 0x5002) {
1380 printk(IBM_ERR
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03001381 "unknown LID-related HKEY event: 0x%04x\n",
1382 hkey);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001383 send_acpi_ev = 1;
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03001384 } else {
1385 ignore_acpi_ev = 1;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001386 }
1387 break;
1388 case 7:
1389 /* 0x7000-0x7FFF: misc */
1390 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
1391 tpacpi_input_send_radiosw();
1392 break;
1393 }
1394 /* fallthrough to default */
1395 default:
1396 /* case 2: dock-related */
1397 /* 0x2305 - T43 waking up due to bay lever eject while aslept */
1398 /* case 3: ultra-bay related. maybe bay in dock? */
1399 /* 0x3003 - T43 after wake up by bay lever eject (0x2305) */
1400 printk(IBM_NOTICE "unhandled HKEY event 0x%04x\n", hkey);
1401 send_acpi_ev = 1;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001402 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001403
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03001404 /* Legacy events */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03001405 if (!ignore_acpi_ev && (send_acpi_ev || hotkey_report_mode < 2)) {
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03001406 acpi_bus_generate_proc_event(ibm->acpi->device, event, hkey);
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03001407 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001408
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03001409 /* netlink events */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03001410 if (!ignore_acpi_ev && send_acpi_ev) {
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03001411 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
1412 ibm->acpi->device->dev.bus_id,
1413 event, hkey);
1414 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001415 }
1416}
1417
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03001418static void hotkey_resume(void)
1419{
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001420 if (hotkey_mask_get())
1421 printk(IBM_ERR "error while trying to read hot key mask from firmware\n");
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03001422 tpacpi_input_send_radiosw();
1423}
1424
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001425/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001426static int hotkey_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001428 int res, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 int len = 0;
1430
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001431 if (!tp_features.hotkey) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001432 len += sprintf(p + len, "status:\t\tnot supported\n");
1433 return len;
1434 }
1435
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02001436 if (mutex_lock_interruptible(&hotkey_mutex))
1437 return -ERESTARTSYS;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001438 res = hotkey_status_get(&status);
1439 if (!res)
1440 res = hotkey_mask_get();
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001441 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001442 if (res)
1443 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
1445 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001446 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001447 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 len += sprintf(p + len,
1449 "commands:\tenable, disable, reset, <mask>\n");
1450 } else {
1451 len += sprintf(p + len, "mask:\t\tnot supported\n");
1452 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
1453 }
1454
1455 return len;
1456}
1457
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001458static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001460 int res, status;
1461 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001464 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 return -ENODEV;
1466
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02001467 if (mutex_lock_interruptible(&hotkey_mutex))
1468 return -ERESTARTSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001469
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001470 status = -1;
1471 mask = hotkey_mask;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001472
1473 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 while ((cmd = next_cmd(&buf))) {
1475 if (strlencmp(cmd, "enable") == 0) {
1476 status = 1;
1477 } else if (strlencmp(cmd, "disable") == 0) {
1478 status = 0;
1479 } else if (strlencmp(cmd, "reset") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001480 status = hotkey_orig_status;
1481 mask = hotkey_orig_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
1483 /* mask set */
1484 } else if (sscanf(cmd, "%x", &mask) == 1) {
1485 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001486 } else {
1487 res = -EINVAL;
1488 goto errexit;
1489 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 }
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001491 if (status != -1)
1492 res = hotkey_status_set(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001494 if (!res && mask != hotkey_mask)
1495 res = hotkey_mask_set(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001497errexit:
1498 mutex_unlock(&hotkey_mutex);
1499 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001500}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
Thomas Renninger1ba90e32007-07-23 14:44:41 +02001502static const struct acpi_device_id ibm_htk_device_ids[] = {
1503 {IBM_HKEY_HID, 0},
1504 {"", 0},
1505};
1506
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001507static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +02001508 .hid = ibm_htk_device_ids,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001509 .notify = hotkey_notify,
1510 .handle = &hkey_handle,
1511 .type = ACPI_DEVICE_NOTIFY,
1512};
1513
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001514static struct ibm_struct hotkey_driver_data = {
1515 .name = "hotkey",
1516 .read = hotkey_read,
1517 .write = hotkey_write,
1518 .exit = hotkey_exit,
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03001519 .resume = hotkey_resume,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001520 .acpi = &ibm_hotkey_acpidriver,
1521};
1522
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001523/*************************************************************************
1524 * Bluetooth subdriver
1525 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001527/* sysfs bluetooth enable ---------------------------------------------- */
1528static ssize_t bluetooth_enable_show(struct device *dev,
1529 struct device_attribute *attr,
1530 char *buf)
1531{
1532 int status;
1533
1534 status = bluetooth_get_radiosw();
1535 if (status < 0)
1536 return status;
1537
1538 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
1539}
1540
1541static ssize_t bluetooth_enable_store(struct device *dev,
1542 struct device_attribute *attr,
1543 const char *buf, size_t count)
1544{
1545 unsigned long t;
1546 int res;
1547
1548 if (parse_strtoul(buf, 1, &t))
1549 return -EINVAL;
1550
1551 res = bluetooth_set_radiosw(t);
1552
1553 return (res) ? res : count;
1554}
1555
1556static struct device_attribute dev_attr_bluetooth_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001557 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001558 bluetooth_enable_show, bluetooth_enable_store);
1559
1560/* --------------------------------------------------------------------- */
1561
1562static struct attribute *bluetooth_attributes[] = {
1563 &dev_attr_bluetooth_enable.attr,
1564 NULL
1565};
1566
1567static const struct attribute_group bluetooth_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001568 .attrs = bluetooth_attributes,
1569};
1570
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001571static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001573 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001574 int status = 0;
1575
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001576 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
1577
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001578 IBM_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001579
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001580 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1581 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001582 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001583 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001585 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
1586 str_supported(tp_features.bluetooth),
1587 status);
1588
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001589 if (tp_features.bluetooth) {
1590 if (!(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
1591 /* no bluetooth hardware present in system */
1592 tp_features.bluetooth = 0;
1593 dbg_printk(TPACPI_DBG_INIT,
1594 "bluetooth hardware not installed\n");
1595 } else {
1596 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
1597 &bluetooth_attr_group);
1598 if (res)
1599 return res;
1600 }
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001601 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001602
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001603 return (tp_features.bluetooth)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604}
1605
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001606static void bluetooth_exit(void)
1607{
1608 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
1609 &bluetooth_attr_group);
1610}
1611
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001612static int bluetooth_get_radiosw(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613{
1614 int status;
1615
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001616 if (!tp_features.bluetooth)
1617 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001619 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
1620 return -EIO;
1621
1622 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
1623}
1624
1625static int bluetooth_set_radiosw(int radio_on)
1626{
1627 int status;
1628
1629 if (!tp_features.bluetooth)
1630 return -ENODEV;
1631
1632 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
1633 return -EIO;
1634 if (radio_on)
1635 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
1636 else
1637 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
1638 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
1639 return -EIO;
1640
1641 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642}
1643
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001644/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001645static int bluetooth_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646{
1647 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001648 int status = bluetooth_get_radiosw();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001650 if (!tp_features.bluetooth)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 len += sprintf(p + len, "status:\t\tnot supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001653 len += sprintf(p + len, "status:\t\t%s\n",
1654 (status)? "enabled" : "disabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 len += sprintf(p + len, "commands:\tenable, disable\n");
1656 }
1657
1658 return len;
1659}
1660
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001661static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001665 if (!tp_features.bluetooth)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001666 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
1668 while ((cmd = next_cmd(&buf))) {
1669 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001670 bluetooth_set_radiosw(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001672 bluetooth_set_radiosw(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 } else
1674 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 }
1676
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 return 0;
1678}
1679
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001680static struct ibm_struct bluetooth_driver_data = {
1681 .name = "bluetooth",
1682 .read = bluetooth_read,
1683 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001684 .exit = bluetooth_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001685};
1686
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001687/*************************************************************************
1688 * Wan subdriver
1689 */
1690
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001691/* sysfs wan enable ---------------------------------------------------- */
1692static ssize_t wan_enable_show(struct device *dev,
1693 struct device_attribute *attr,
1694 char *buf)
1695{
1696 int status;
1697
1698 status = wan_get_radiosw();
1699 if (status < 0)
1700 return status;
1701
1702 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
1703}
1704
1705static ssize_t wan_enable_store(struct device *dev,
1706 struct device_attribute *attr,
1707 const char *buf, size_t count)
1708{
1709 unsigned long t;
1710 int res;
1711
1712 if (parse_strtoul(buf, 1, &t))
1713 return -EINVAL;
1714
1715 res = wan_set_radiosw(t);
1716
1717 return (res) ? res : count;
1718}
1719
1720static struct device_attribute dev_attr_wan_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001721 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001722 wan_enable_show, wan_enable_store);
1723
1724/* --------------------------------------------------------------------- */
1725
1726static struct attribute *wan_attributes[] = {
1727 &dev_attr_wan_enable.attr,
1728 NULL
1729};
1730
1731static const struct attribute_group wan_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001732 .attrs = wan_attributes,
1733};
1734
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001735static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001736{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001737 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001738 int status = 0;
1739
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001740 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
1741
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001742 IBM_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001743
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001744 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001745 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001746
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001747 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
1748 str_supported(tp_features.wan),
1749 status);
1750
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001751 if (tp_features.wan) {
1752 if (!(status & TP_ACPI_WANCARD_HWPRESENT)) {
1753 /* no wan hardware present in system */
1754 tp_features.wan = 0;
1755 dbg_printk(TPACPI_DBG_INIT,
1756 "wan hardware not installed\n");
1757 } else {
1758 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
1759 &wan_attr_group);
1760 if (res)
1761 return res;
1762 }
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001763 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001764
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001765 return (tp_features.wan)? 0 : 1;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001766}
1767
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001768static void wan_exit(void)
1769{
1770 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
1771 &wan_attr_group);
1772}
1773
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001774static int wan_get_radiosw(void)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001775{
1776 int status;
1777
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001778 if (!tp_features.wan)
1779 return -ENODEV;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001780
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001781 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
1782 return -EIO;
1783
1784 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
1785}
1786
1787static int wan_set_radiosw(int radio_on)
1788{
1789 int status;
1790
1791 if (!tp_features.wan)
1792 return -ENODEV;
1793
1794 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
1795 return -EIO;
1796 if (radio_on)
1797 status |= TP_ACPI_WANCARD_RADIOSSW;
1798 else
1799 status &= ~TP_ACPI_WANCARD_RADIOSSW;
1800 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
1801 return -EIO;
1802
1803 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001804}
1805
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001806/* procfs -------------------------------------------------------------- */
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001807static int wan_read(char *p)
1808{
1809 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001810 int status = wan_get_radiosw();
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001811
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001812 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001813 len += sprintf(p + len, "status:\t\tnot supported\n");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001814 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001815 len += sprintf(p + len, "status:\t\t%s\n",
1816 (status)? "enabled" : "disabled");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001817 len += sprintf(p + len, "commands:\tenable, disable\n");
1818 }
1819
1820 return len;
1821}
1822
1823static int wan_write(char *buf)
1824{
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001825 char *cmd;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001826
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001827 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001828 return -ENODEV;
1829
1830 while ((cmd = next_cmd(&buf))) {
1831 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001832 wan_set_radiosw(1);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001833 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001834 wan_set_radiosw(0);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001835 } else
1836 return -EINVAL;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001837 }
1838
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001839 return 0;
1840}
1841
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001842static struct ibm_struct wan_driver_data = {
1843 .name = "wan",
1844 .read = wan_read,
1845 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001846 .exit = wan_exit,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03001847 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001848};
1849
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001850/*************************************************************************
1851 * Video subdriver
1852 */
1853
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02001854static enum video_access_mode video_supported;
1855static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001856
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001857IBM_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
1858 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
1859 "\\_SB.PCI0.VID0", /* 770e */
1860 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
1861 "\\_SB.PCI0.AGP.VID", /* all others */
1862 ); /* R30, R31 */
1863
1864IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
1865
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001866static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001868 int ivga;
1869
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001870 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
1871
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001872 IBM_ACPIHANDLE_INIT(vid);
1873 IBM_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001874
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001875 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
1876 /* G41, assume IVGA doesn't change */
1877 vid_handle = vid2_handle;
1878
1879 if (!vid_handle)
1880 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001881 video_supported = TPACPI_VIDEO_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001882 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
1883 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001884 video_supported = TPACPI_VIDEO_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001885 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
1886 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001887 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001888 else
1889 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001890 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001892 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
1893 str_supported(video_supported != TPACPI_VIDEO_NONE),
1894 video_supported);
1895
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001896 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897}
1898
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001899static void video_exit(void)
1900{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001901 dbg_printk(TPACPI_DBG_EXIT,
1902 "restoring original video autoswitch mode\n");
1903 if (video_autosw_set(video_orig_autosw))
1904 printk(IBM_ERR "error while trying to restore original "
1905 "video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001906}
1907
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001908static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909{
1910 int status = 0;
1911 int i;
1912
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001913 switch (video_supported) {
1914 case TPACPI_VIDEO_570:
1915 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
1916 TP_ACPI_VIDEO_570_PHSCMD))
1917 return -EIO;
1918 status = i & TP_ACPI_VIDEO_570_PHSMASK;
1919 break;
1920 case TPACPI_VIDEO_770:
1921 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
1922 return -EIO;
1923 if (i)
1924 status |= TP_ACPI_VIDEO_S_LCD;
1925 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
1926 return -EIO;
1927 if (i)
1928 status |= TP_ACPI_VIDEO_S_CRT;
1929 break;
1930 case TPACPI_VIDEO_NEW:
1931 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
1932 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
1933 return -EIO;
1934 if (i)
1935 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001937 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
1938 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
1939 return -EIO;
1940 if (i)
1941 status |= TP_ACPI_VIDEO_S_LCD;
1942 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
1943 return -EIO;
1944 if (i)
1945 status |= TP_ACPI_VIDEO_S_DVI;
1946 break;
1947 default:
1948 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001949 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950
1951 return status;
1952}
1953
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001954static int video_outputsw_set(int status)
1955{
1956 int autosw;
1957 int res = 0;
1958
1959 switch (video_supported) {
1960 case TPACPI_VIDEO_570:
1961 res = acpi_evalf(NULL, NULL,
1962 "\\_SB.PHS2", "vdd",
1963 TP_ACPI_VIDEO_570_PHS2CMD,
1964 status | TP_ACPI_VIDEO_570_PHS2SET);
1965 break;
1966 case TPACPI_VIDEO_770:
1967 autosw = video_autosw_get();
1968 if (autosw < 0)
1969 return autosw;
1970
1971 res = video_autosw_set(1);
1972 if (res)
1973 return res;
1974 res = acpi_evalf(vid_handle, NULL,
1975 "ASWT", "vdd", status * 0x100, 0);
1976 if (!autosw && video_autosw_set(autosw)) {
1977 printk(IBM_ERR "video auto-switch left enabled due to error\n");
1978 return -EIO;
1979 }
1980 break;
1981 case TPACPI_VIDEO_NEW:
1982 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
1983 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
1984 break;
1985 default:
1986 return -ENOSYS;
1987 }
1988
1989 return (res)? 0 : -EIO;
1990}
1991
1992static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001994 int autosw = 0;
1995
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001996 switch (video_supported) {
1997 case TPACPI_VIDEO_570:
1998 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
1999 return -EIO;
2000 break;
2001 case TPACPI_VIDEO_770:
2002 case TPACPI_VIDEO_NEW:
2003 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
2004 return -EIO;
2005 break;
2006 default:
2007 return -ENOSYS;
2008 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002009
2010 return autosw & 1;
2011}
2012
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002013static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002014{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002015 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002016 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002017 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002018}
2019
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002020static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002021{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002022 int autosw = video_autosw_get();
2023 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002024
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002025 if (autosw < 0)
2026 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002027
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002028 switch (video_supported) {
2029 case TPACPI_VIDEO_570:
2030 res = video_autosw_set(1);
2031 if (res)
2032 return res;
2033 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
2034 break;
2035 case TPACPI_VIDEO_770:
2036 case TPACPI_VIDEO_NEW:
2037 res = video_autosw_set(1);
2038 if (res)
2039 return res;
2040 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
2041 break;
2042 default:
2043 return -ENOSYS;
2044 }
2045 if (!autosw && video_autosw_set(autosw)) {
2046 printk(IBM_ERR "video auto-switch left enabled due to error\n");
2047 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002048 }
2049
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002050 return (res)? 0 : -EIO;
2051}
2052
2053static int video_expand_toggle(void)
2054{
2055 switch (video_supported) {
2056 case TPACPI_VIDEO_570:
2057 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
2058 0 : -EIO;
2059 case TPACPI_VIDEO_770:
2060 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
2061 0 : -EIO;
2062 case TPACPI_VIDEO_NEW:
2063 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
2064 0 : -EIO;
2065 default:
2066 return -ENOSYS;
2067 }
2068 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002069}
2070
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002071static int video_read(char *p)
2072{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002073 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002074 int len = 0;
2075
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002076 if (video_supported == TPACPI_VIDEO_NONE) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002077 len += sprintf(p + len, "status:\t\tnot supported\n");
2078 return len;
2079 }
2080
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002081 status = video_outputsw_get();
2082 if (status < 0)
2083 return status;
2084
2085 autosw = video_autosw_get();
2086 if (autosw < 0)
2087 return autosw;
2088
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002089 len += sprintf(p + len, "status:\t\tsupported\n");
2090 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
2091 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002092 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002093 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
2094 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
2095 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
2096 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002097 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002098 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
2099 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
2100 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
2101
2102 return len;
2103}
2104
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002105static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106{
2107 char *cmd;
2108 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002109 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002111 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002112 return -ENODEV;
2113
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002114 enable = 0;
2115 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116
2117 while ((cmd = next_cmd(&buf))) {
2118 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002119 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002121 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002123 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002125 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002126 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002127 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002128 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002129 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002130 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002131 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002133 res = video_autosw_set(1);
2134 if (res)
2135 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002137 res = video_autosw_set(0);
2138 if (res)
2139 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002141 res = video_outputsw_cycle();
2142 if (res)
2143 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002145 res = video_expand_toggle();
2146 if (res)
2147 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 } else
2149 return -EINVAL;
2150 }
2151
2152 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002153 status = video_outputsw_get();
2154 if (status < 0)
2155 return status;
2156 res = video_outputsw_set((status & ~disable) | enable);
2157 if (res)
2158 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 }
2160
2161 return 0;
2162}
2163
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002164static struct ibm_struct video_driver_data = {
2165 .name = "video",
2166 .read = video_read,
2167 .write = video_write,
2168 .exit = video_exit,
2169};
2170
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002171/*************************************************************************
2172 * Light (thinklight) subdriver
2173 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002175IBM_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
2176IBM_HANDLE(ledb, ec, "LEDB"); /* G4x */
2177
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002178static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002180 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
2181
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002182 IBM_ACPIHANDLE_INIT(ledb);
2183 IBM_ACPIHANDLE_INIT(lght);
2184 IBM_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002185
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002186 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002187 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002188
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002189 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002190 /* light status not supported on
2191 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002192 tp_features.light_status =
2193 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002195 vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002196 str_supported(tp_features.light));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002197
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002198 return (tp_features.light)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199}
2200
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002201static int light_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202{
2203 int len = 0;
2204 int status = 0;
2205
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002206 if (!tp_features.light) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002207 len += sprintf(p + len, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002208 } else if (!tp_features.light_status) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002209 len += sprintf(p + len, "status:\t\tunknown\n");
2210 len += sprintf(p + len, "commands:\ton, off\n");
2211 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
2213 return -EIO;
2214 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002215 len += sprintf(p + len, "commands:\ton, off\n");
2216 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217
2218 return len;
2219}
2220
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002221static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222{
2223 int cmos_cmd, lght_cmd;
2224 char *cmd;
2225 int success;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002226
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002227 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002228 return -ENODEV;
2229
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 while ((cmd = next_cmd(&buf))) {
2231 if (strlencmp(cmd, "on") == 0) {
2232 cmos_cmd = 0x0c;
2233 lght_cmd = 1;
2234 } else if (strlencmp(cmd, "off") == 0) {
2235 cmos_cmd = 0x0d;
2236 lght_cmd = 0;
2237 } else
2238 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002239
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 success = cmos_handle ?
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002241 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
2242 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 if (!success)
2244 return -EIO;
2245 }
2246
2247 return 0;
2248}
2249
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002250static struct ibm_struct light_driver_data = {
2251 .name = "light",
2252 .read = light_read,
2253 .write = light_write,
2254};
2255
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002256/*************************************************************************
2257 * Dock subdriver
2258 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03002260#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002261
2262IBM_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
2263 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
2264 "\\_SB.PCI0.PCI1.DOCK", /* all others */
2265 "\\_SB.PCI.ISA.SLCE", /* 570 */
2266 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
2267
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002268/* don't list other alternatives as we install a notify handler on the 570 */
2269IBM_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
2270
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002271static const struct acpi_device_id ibm_pci_device_ids[] = {
2272 {PCI_ROOT_HID_STRING, 0},
2273 {"", 0},
2274};
2275
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03002276static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
2277 {
2278 .notify = dock_notify,
2279 .handle = &dock_handle,
2280 .type = ACPI_SYSTEM_NOTIFY,
2281 },
2282 {
Henrique de Moraes Holschuh996fba02007-07-18 23:45:40 -03002283 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
2284 * We just use it to get notifications of dock hotplug
2285 * in very old thinkpads */
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002286 .hid = ibm_pci_device_ids,
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03002287 .notify = dock_notify,
2288 .handle = &pci_handle,
2289 .type = ACPI_SYSTEM_NOTIFY,
2290 },
2291};
2292
2293static struct ibm_struct dock_driver_data[2] = {
2294 {
2295 .name = "dock",
2296 .read = dock_read,
2297 .write = dock_write,
2298 .acpi = &ibm_dock_acpidriver[0],
2299 },
2300 {
2301 .name = "dock",
2302 .acpi = &ibm_dock_acpidriver[1],
2303 },
2304};
2305
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306#define dock_docked() (_sta(dock_handle) & 1)
2307
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002308static int __init dock_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002309{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002310 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
2311
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002312 IBM_ACPIHANDLE_INIT(dock);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002313
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002314 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
2315 str_supported(dock_handle != NULL));
2316
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002317 return (dock_handle)? 0 : 1;
2318}
2319
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03002320static int __init dock_init2(struct ibm_init_struct *iibm)
2321{
2322 int dock2_needed;
2323
2324 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
2325
2326 if (dock_driver_data[0].flags.acpi_driver_registered &&
2327 dock_driver_data[0].flags.acpi_notify_installed) {
2328 IBM_ACPIHANDLE_INIT(pci);
2329 dock2_needed = (pci_handle != NULL);
2330 vdbg_printk(TPACPI_DBG_INIT,
2331 "dock PCI handler for the TP 570 is %s\n",
2332 str_supported(dock2_needed));
2333 } else {
2334 vdbg_printk(TPACPI_DBG_INIT,
2335 "dock subdriver part 2 not required\n");
2336 dock2_needed = 0;
2337 }
2338
2339 return (dock2_needed)? 0 : 1;
2340}
2341
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002342static void dock_notify(struct ibm_struct *ibm, u32 event)
2343{
2344 int docked = dock_docked();
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002345 int pci = ibm->acpi->hid && ibm->acpi->device &&
2346 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
Zhang Rui962ce8c2007-08-23 01:24:31 +08002347 int data;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002348
2349 if (event == 1 && !pci) /* 570 */
Zhang Rui962ce8c2007-08-23 01:24:31 +08002350 data = 1; /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002351 else if (event == 1 && pci) /* 570 */
Zhang Rui962ce8c2007-08-23 01:24:31 +08002352 data = 3; /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002353 else if (event == 3 && docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08002354 data = 1; /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002355 else if (event == 3 && !docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08002356 data = 2; /* undock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002357 else if (event == 0 && docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08002358 data = 3; /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002359 else {
2360 printk(IBM_ERR "unknown dock event %d, status %d\n",
2361 event, _sta(dock_handle));
Zhang Rui962ce8c2007-08-23 01:24:31 +08002362 data = 0; /* unknown */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002363 }
Len Brown14e04fb2007-08-23 15:20:26 -04002364 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
Zhang Rui962ce8c2007-08-23 01:24:31 +08002365 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
2366 ibm->acpi->device->dev.bus_id,
2367 event, data);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002368}
2369
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002370static int dock_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371{
2372 int len = 0;
2373 int docked = dock_docked();
2374
2375 if (!dock_handle)
2376 len += sprintf(p + len, "status:\t\tnot supported\n");
2377 else if (!docked)
2378 len += sprintf(p + len, "status:\t\tundocked\n");
2379 else {
2380 len += sprintf(p + len, "status:\t\tdocked\n");
2381 len += sprintf(p + len, "commands:\tdock, undock\n");
2382 }
2383
2384 return len;
2385}
2386
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002387static int dock_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388{
2389 char *cmd;
2390
2391 if (!dock_docked())
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002392 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393
2394 while ((cmd = next_cmd(&buf))) {
2395 if (strlencmp(cmd, "undock") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002396 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
2397 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 return -EIO;
2399 } else if (strlencmp(cmd, "dock") == 0) {
2400 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
2401 return -EIO;
2402 } else
2403 return -EINVAL;
2404 }
2405
2406 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002407}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03002409#endif /* CONFIG_THINKPAD_ACPI_DOCK */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002411/*************************************************************************
2412 * Bay subdriver
2413 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03002415#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002416IBM_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
2417 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
2418 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
2419 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
2420 ); /* A21e, R30, R31 */
2421IBM_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
2422 "_EJ0", /* all others */
2423 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
2424IBM_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
2425 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
2426 ); /* all others */
2427IBM_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
2428 "_EJ0", /* 770x */
2429 ); /* all others */
2430
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002431static int __init bay_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002433 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
2434
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002435 IBM_ACPIHANDLE_INIT(bay);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002436 if (bay_handle)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002437 IBM_ACPIHANDLE_INIT(bay_ej);
2438 IBM_ACPIHANDLE_INIT(bay2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002439 if (bay2_handle)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002440 IBM_ACPIHANDLE_INIT(bay2_ej);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002441
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002442 tp_features.bay_status = bay_handle &&
2443 acpi_evalf(bay_handle, NULL, "_STA", "qv");
2444 tp_features.bay_status2 = bay2_handle &&
2445 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002446
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002447 tp_features.bay_eject = bay_handle && bay_ej_handle &&
2448 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
2449 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
2450 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002452 vdbg_printk(TPACPI_DBG_INIT,
2453 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002454 str_supported(tp_features.bay_status),
2455 str_supported(tp_features.bay_eject),
2456 str_supported(tp_features.bay_status2),
2457 str_supported(tp_features.bay_eject2));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002458
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002459 return (tp_features.bay_status || tp_features.bay_eject ||
2460 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461}
2462
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002463static void bay_notify(struct ibm_struct *ibm, u32 event)
2464{
Len Brown14e04fb2007-08-23 15:20:26 -04002465 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
Zhang Rui962ce8c2007-08-23 01:24:31 +08002466 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
2467 ibm->acpi->device->dev.bus_id,
2468 event, 0);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002469}
2470
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002471#define bay_occupied(b) (_sta(b##_handle) & 1)
2472
2473static int bay_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474{
2475 int len = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002476 int occupied = bay_occupied(bay);
2477 int occupied2 = bay_occupied(bay2);
2478 int eject, eject2;
2479
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002480 len += sprintf(p + len, "status:\t\t%s\n",
2481 tp_features.bay_status ?
2482 (occupied ? "occupied" : "unoccupied") :
2483 "not supported");
2484 if (tp_features.bay_status2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002485 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
2486 "occupied" : "unoccupied");
2487
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002488 eject = tp_features.bay_eject && occupied;
2489 eject2 = tp_features.bay_eject2 && occupied2;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002490
2491 if (eject && eject2)
2492 len += sprintf(p + len, "commands:\teject, eject2\n");
2493 else if (eject)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 len += sprintf(p + len, "commands:\teject\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002495 else if (eject2)
2496 len += sprintf(p + len, "commands:\teject2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497
2498 return len;
2499}
2500
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002501static int bay_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502{
2503 char *cmd;
2504
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002505 if (!tp_features.bay_eject && !tp_features.bay_eject2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002506 return -ENODEV;
2507
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002509 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002510 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
2511 return -EIO;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002512 } else if (tp_features.bay_eject2 &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002513 strlencmp(cmd, "eject2") == 0) {
2514 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 return -EIO;
2516 } else
2517 return -EINVAL;
2518 }
2519
2520 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002521}
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002522
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002523static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
2524 .notify = bay_notify,
2525 .handle = &bay_handle,
2526 .type = ACPI_SYSTEM_NOTIFY,
2527};
2528
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002529static struct ibm_struct bay_driver_data = {
2530 .name = "bay",
2531 .read = bay_read,
2532 .write = bay_write,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002533 .acpi = &ibm_bay_acpidriver,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002534};
2535
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03002536#endif /* CONFIG_THINKPAD_ACPI_BAY */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002538/*************************************************************************
2539 * CMOS subdriver
2540 */
2541
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03002542/* sysfs cmos_command -------------------------------------------------- */
2543static ssize_t cmos_command_store(struct device *dev,
2544 struct device_attribute *attr,
2545 const char *buf, size_t count)
2546{
2547 unsigned long cmos_cmd;
2548 int res;
2549
2550 if (parse_strtoul(buf, 21, &cmos_cmd))
2551 return -EINVAL;
2552
2553 res = issue_thinkpad_cmos_command(cmos_cmd);
2554 return (res)? res : count;
2555}
2556
2557static struct device_attribute dev_attr_cmos_command =
2558 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
2559
2560/* --------------------------------------------------------------------- */
2561
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002562static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002563{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03002564 int res;
2565
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002566 vdbg_printk(TPACPI_DBG_INIT,
2567 "initializing cmos commands subdriver\n");
2568
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002569 IBM_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002570
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002571 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
2572 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03002573
2574 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
2575 if (res)
2576 return res;
2577
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002578 return (cmos_handle)? 0 : 1;
2579}
2580
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03002581static void cmos_exit(void)
2582{
2583 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
2584}
2585
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002586static int cmos_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587{
2588 int len = 0;
2589
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002590 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2591 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 if (!cmos_handle)
2593 len += sprintf(p + len, "status:\t\tnot supported\n");
2594 else {
2595 len += sprintf(p + len, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002596 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 }
2598
2599 return len;
2600}
2601
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002602static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603{
2604 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03002605 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606
2607 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002608 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
2609 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 /* cmos_cmd set */
2611 } else
2612 return -EINVAL;
2613
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03002614 res = issue_thinkpad_cmos_command(cmos_cmd);
2615 if (res)
2616 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 }
2618
2619 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002620}
2621
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002622static struct ibm_struct cmos_driver_data = {
2623 .name = "cmos",
2624 .read = cmos_read,
2625 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03002626 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002627};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002628
2629/*************************************************************************
2630 * LED subdriver
2631 */
2632
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02002633static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002634
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002635IBM_HANDLE(led, ec, "SLED", /* 570 */
2636 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
2637 "LED", /* all others */
2638 ); /* R30, R31 */
2639
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002640static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002641{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002642 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
2643
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002644 IBM_ACPIHANDLE_INIT(led);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002645
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002646 if (!led_handle)
2647 /* led not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002648 led_supported = TPACPI_LED_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002649 else if (strlencmp(led_path, "SLED") == 0)
2650 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002651 led_supported = TPACPI_LED_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002652 else if (strlencmp(led_path, "SYSL") == 0)
2653 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002654 led_supported = TPACPI_LED_OLD;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002655 else
2656 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002657 led_supported = TPACPI_LED_NEW;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002658
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002659 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
2660 str_supported(led_supported), led_supported);
2661
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002662 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002663}
2664
2665#define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
2666
2667static int led_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668{
2669 int len = 0;
2670
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002671 if (!led_supported) {
2672 len += sprintf(p + len, "status:\t\tnot supported\n");
2673 return len;
2674 }
2675 len += sprintf(p + len, "status:\t\tsupported\n");
2676
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002677 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002678 /* 570 */
2679 int i, status;
2680 for (i = 0; i < 8; i++) {
2681 if (!acpi_evalf(ec_handle,
2682 &status, "GLED", "dd", 1 << i))
2683 return -EIO;
2684 len += sprintf(p + len, "%d:\t\t%s\n",
2685 i, led_status(status));
2686 }
2687 }
2688
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 len += sprintf(p + len, "commands:\t"
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002690 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691
2692 return len;
2693}
2694
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002695/* off, on, blink */
2696static const int led_sled_arg1[] = { 0, 1, 3 };
2697static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
2698static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
2699static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
2700
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002701static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702{
2703 char *cmd;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002704 int led, ind, ret;
2705
2706 if (!led_supported)
2707 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708
2709 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002710 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 return -EINVAL;
2712
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002713 if (strstr(cmd, "off")) {
2714 ind = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 } else if (strstr(cmd, "on")) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002716 ind = 1;
2717 } else if (strstr(cmd, "blink")) {
2718 ind = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 } else
2720 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002721
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002722 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002723 /* 570 */
2724 led = 1 << led;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002726 led, led_sled_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 return -EIO;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002728 } else if (led_supported == TPACPI_LED_OLD) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002729 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
2730 led = 1 << led;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002731 ret = ec_write(TPACPI_LED_EC_HLMS, led);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002732 if (ret >= 0)
2733 ret =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002734 ec_write(TPACPI_LED_EC_HLBL,
Henrique de Moraes Holschuhe062e032007-03-23 17:33:55 -03002735 led * led_exp_hlbl[ind]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002736 if (ret >= 0)
2737 ret =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002738 ec_write(TPACPI_LED_EC_HLCL,
Henrique de Moraes Holschuhe062e032007-03-23 17:33:55 -03002739 led * led_exp_hlcl[ind]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002740 if (ret < 0)
2741 return ret;
2742 } else {
2743 /* all others */
2744 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
2745 led, led_led_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002747 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 }
2749
2750 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002751}
2752
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002753static struct ibm_struct led_driver_data = {
2754 .name = "led",
2755 .read = led_read,
2756 .write = led_write,
2757};
2758
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002759/*************************************************************************
2760 * Beep subdriver
2761 */
2762
2763IBM_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
2764
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002765static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002766{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002767 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
2768
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002769 IBM_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002770
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002771 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
2772 str_supported(beep_handle != NULL));
2773
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002774 return (beep_handle)? 0 : 1;
2775}
2776
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002777static int beep_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778{
2779 int len = 0;
2780
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002781 if (!beep_handle)
2782 len += sprintf(p + len, "status:\t\tnot supported\n");
2783 else {
2784 len += sprintf(p + len, "status:\t\tsupported\n");
2785 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
2786 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787
2788 return len;
2789}
2790
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002791static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792{
2793 char *cmd;
2794 int beep_cmd;
2795
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002796 if (!beep_handle)
2797 return -ENODEV;
2798
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002800 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
2801 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 /* beep_cmd set */
2803 } else
2804 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002805 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 return -EIO;
2807 }
2808
2809 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002810}
2811
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002812static struct ibm_struct beep_driver_data = {
2813 .name = "beep",
2814 .read = beep_read,
2815 .write = beep_write,
2816};
2817
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002818/*************************************************************************
2819 * Thermal subdriver
2820 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002821
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002822static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002823
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03002824/* sysfs temp##_input -------------------------------------------------- */
2825
2826static ssize_t thermal_temp_input_show(struct device *dev,
2827 struct device_attribute *attr,
2828 char *buf)
2829{
2830 struct sensor_device_attribute *sensor_attr =
2831 to_sensor_dev_attr(attr);
2832 int idx = sensor_attr->index;
2833 s32 value;
2834 int res;
2835
2836 res = thermal_get_sensor(idx, &value);
2837 if (res)
2838 return res;
2839 if (value == TP_EC_THERMAL_TMP_NA * 1000)
2840 return -ENXIO;
2841
2842 return snprintf(buf, PAGE_SIZE, "%d\n", value);
2843}
2844
2845#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
2846 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, thermal_temp_input_show, NULL, _idxB)
2847
2848static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
2849 THERMAL_SENSOR_ATTR_TEMP(1, 0),
2850 THERMAL_SENSOR_ATTR_TEMP(2, 1),
2851 THERMAL_SENSOR_ATTR_TEMP(3, 2),
2852 THERMAL_SENSOR_ATTR_TEMP(4, 3),
2853 THERMAL_SENSOR_ATTR_TEMP(5, 4),
2854 THERMAL_SENSOR_ATTR_TEMP(6, 5),
2855 THERMAL_SENSOR_ATTR_TEMP(7, 6),
2856 THERMAL_SENSOR_ATTR_TEMP(8, 7),
2857 THERMAL_SENSOR_ATTR_TEMP(9, 8),
2858 THERMAL_SENSOR_ATTR_TEMP(10, 9),
2859 THERMAL_SENSOR_ATTR_TEMP(11, 10),
2860 THERMAL_SENSOR_ATTR_TEMP(12, 11),
2861 THERMAL_SENSOR_ATTR_TEMP(13, 12),
2862 THERMAL_SENSOR_ATTR_TEMP(14, 13),
2863 THERMAL_SENSOR_ATTR_TEMP(15, 14),
2864 THERMAL_SENSOR_ATTR_TEMP(16, 15),
2865};
2866
2867#define THERMAL_ATTRS(X) \
2868 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
2869
2870static struct attribute *thermal_temp_input_attr[] = {
2871 THERMAL_ATTRS(8),
2872 THERMAL_ATTRS(9),
2873 THERMAL_ATTRS(10),
2874 THERMAL_ATTRS(11),
2875 THERMAL_ATTRS(12),
2876 THERMAL_ATTRS(13),
2877 THERMAL_ATTRS(14),
2878 THERMAL_ATTRS(15),
2879 THERMAL_ATTRS(0),
2880 THERMAL_ATTRS(1),
2881 THERMAL_ATTRS(2),
2882 THERMAL_ATTRS(3),
2883 THERMAL_ATTRS(4),
2884 THERMAL_ATTRS(5),
2885 THERMAL_ATTRS(6),
2886 THERMAL_ATTRS(7),
2887 NULL
2888};
2889
2890static const struct attribute_group thermal_temp_input16_group = {
2891 .attrs = thermal_temp_input_attr
2892};
2893
2894static const struct attribute_group thermal_temp_input8_group = {
2895 .attrs = &thermal_temp_input_attr[8]
2896};
2897
2898#undef THERMAL_SENSOR_ATTR_TEMP
2899#undef THERMAL_ATTRS
2900
2901/* --------------------------------------------------------------------- */
2902
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002903static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002904{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002905 u8 t, ta1, ta2;
2906 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002907 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03002908 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002909
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002910 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
2911
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002912 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002913
Henrique de Moraes Holschuh3d6f99c2007-07-18 23:45:46 -03002914 if (thinkpad_id.ec_model) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002915 /*
2916 * Direct EC access mode: sensors at registers
2917 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
2918 * non-implemented, thermal sensors return 0x80 when
2919 * not available
2920 */
2921
2922 ta1 = ta2 = 0;
2923 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002924 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002925 ta1 |= t;
2926 } else {
2927 ta1 = 0;
2928 break;
2929 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002930 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002931 ta2 |= t;
2932 } else {
2933 ta1 = 0;
2934 break;
2935 }
2936 }
2937 if (ta1 == 0) {
2938 /* This is sheer paranoia, but we handle it anyway */
2939 if (acpi_tmp7) {
2940 printk(IBM_ERR
2941 "ThinkPad ACPI EC access misbehaving, "
2942 "falling back to ACPI TMPx access mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002943 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002944 } else {
2945 printk(IBM_ERR
2946 "ThinkPad ACPI EC access misbehaving, "
2947 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002948 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002949 }
2950 } else {
2951 thermal_read_mode =
2952 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002953 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002954 }
2955 } else if (acpi_tmp7) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002956 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
2957 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002958 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002959 } else {
2960 /* Standard ACPI TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002961 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002962 }
2963 } else {
2964 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002965 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002966 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002967
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002968 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
2969 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
2970 thermal_read_mode);
2971
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03002972 switch(thermal_read_mode) {
2973 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03002974 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03002975 &thermal_temp_input16_group);
2976 if (res)
2977 return res;
2978 break;
2979 case TPACPI_THERMAL_TPEC_8:
2980 case TPACPI_THERMAL_ACPI_TMP07:
2981 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03002982 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03002983 &thermal_temp_input8_group);
2984 if (res)
2985 return res;
2986 break;
2987 case TPACPI_THERMAL_NONE:
2988 default:
2989 return 1;
2990 }
2991
2992 return 0;
2993}
2994
2995static void thermal_exit(void)
2996{
2997 switch(thermal_read_mode) {
2998 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03002999 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003000 &thermal_temp_input16_group);
3001 break;
3002 case TPACPI_THERMAL_TPEC_8:
3003 case TPACPI_THERMAL_ACPI_TMP07:
3004 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03003005 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003006 &thermal_temp_input16_group);
3007 break;
3008 case TPACPI_THERMAL_NONE:
3009 default:
3010 break;
3011 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003012}
3013
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03003014/* idx is zero-based */
3015static int thermal_get_sensor(int idx, s32 *value)
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003016{
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03003017 int t;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003018 s8 tmp;
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03003019 char tmpi[5];
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003020
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03003021 t = TP_EC_THERMAL_TMP0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003022
3023 switch (thermal_read_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003024#if TPACPI_MAX_THERMAL_SENSORS >= 16
3025 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03003026 if (idx >= 8 && idx <= 15) {
3027 t = TP_EC_THERMAL_TMP8;
3028 idx -= 8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003029 }
3030 /* fallthrough */
3031#endif
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003032 case TPACPI_THERMAL_TPEC_8:
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03003033 if (idx <= 7) {
3034 if (!acpi_ec_read(t + idx, &tmp))
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003035 return -EIO;
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03003036 *value = tmp * 1000;
3037 return 0;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003038 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03003039 break;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003040
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003041 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03003042 if (idx <= 7) {
3043 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
3044 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
3045 return -EIO;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003046 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
3047 return -EIO;
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03003048 *value = (t - 2732) * 100;
3049 return 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003050 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03003051 break;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003052
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003053 case TPACPI_THERMAL_ACPI_TMP07:
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03003054 if (idx <= 7) {
3055 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003056 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
3057 return -EIO;
Henrique de Moraes Holschuha8fba3da2007-07-18 23:45:45 -03003058 if (t > 127 || t < -127)
3059 t = TP_EC_THERMAL_TMP_NA;
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03003060 *value = t * 1000;
3061 return 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003062 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03003063 break;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003064
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003065 case TPACPI_THERMAL_NONE:
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003066 default:
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03003067 return -ENOSYS;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003068 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03003069
3070 return -EINVAL;
3071}
3072
3073static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
3074{
3075 int res, i;
3076 int n;
3077
3078 n = 8;
3079 i = 0;
3080
3081 if (!s)
3082 return -EINVAL;
3083
3084 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
3085 n = 16;
3086
3087 for(i = 0 ; i < n; i++) {
3088 res = thermal_get_sensor(i, &s->temp[i]);
3089 if (res)
3090 return res;
3091 }
3092
3093 return n;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003094}
3095
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003096static int thermal_read(char *p)
3097{
3098 int len = 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003099 int n, i;
3100 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003101
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003102 n = thermal_get_sensors(&t);
3103 if (unlikely(n < 0))
3104 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003105
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003106 len += sprintf(p + len, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003107
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003108 if (n > 0) {
3109 for (i = 0; i < (n - 1); i++)
3110 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
3111 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
3112 } else
3113 len += sprintf(p + len, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003114
3115 return len;
3116}
3117
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003118static struct ibm_struct thermal_driver_data = {
3119 .name = "thermal",
3120 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003121 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003122};
3123
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003124/*************************************************************************
3125 * EC Dump subdriver
3126 */
3127
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003128static u8 ecdump_regs[256];
3129
3130static int ecdump_read(char *p)
3131{
3132 int len = 0;
3133 int i, j;
3134 u8 v;
3135
3136 len += sprintf(p + len, "EC "
3137 " +00 +01 +02 +03 +04 +05 +06 +07"
3138 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
3139 for (i = 0; i < 256; i += 16) {
3140 len += sprintf(p + len, "EC 0x%02x:", i);
3141 for (j = 0; j < 16; j++) {
3142 if (!acpi_ec_read(i + j, &v))
3143 break;
3144 if (v != ecdump_regs[i + j])
3145 len += sprintf(p + len, " *%02x", v);
3146 else
3147 len += sprintf(p + len, " %02x", v);
3148 ecdump_regs[i + j] = v;
3149 }
3150 len += sprintf(p + len, "\n");
3151 if (j != 16)
3152 break;
3153 }
3154
3155 /* These are way too dangerous to advertise openly... */
3156#if 0
3157 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
3158 " (<offset> is 00-ff, <value> is 00-ff)\n");
3159 len += sprintf(p + len, "commands:\t0x<offset> <value> "
3160 " (<offset> is 00-ff, <value> is 0-255)\n");
3161#endif
3162 return len;
3163}
3164
3165static int ecdump_write(char *buf)
3166{
3167 char *cmd;
3168 int i, v;
3169
3170 while ((cmd = next_cmd(&buf))) {
3171 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
3172 /* i and v set */
3173 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
3174 /* i and v set */
3175 } else
3176 return -EINVAL;
3177 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
3178 if (!acpi_ec_write(i, v))
3179 return -EIO;
3180 } else
3181 return -EINVAL;
3182 }
3183
3184 return 0;
3185}
3186
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003187static struct ibm_struct ecdump_driver_data = {
3188 .name = "ecdump",
3189 .read = ecdump_read,
3190 .write = ecdump_write,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03003191 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003192};
3193
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003194/*************************************************************************
3195 * Backlight/brightness subdriver
3196 */
Holger Macht8acb0252006-10-20 14:30:28 -07003197
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03003198static struct backlight_device *ibm_backlight_device;
Holger Macht8acb0252006-10-20 14:30:28 -07003199
Richard Purdie599a52d2007-02-10 23:07:48 +00003200static struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02003201 .get_brightness = brightness_get,
3202 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02003203};
3204
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03003205static struct mutex brightness_mutex;
3206
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02003207static int __init tpacpi_query_bcll_levels(acpi_handle handle)
3208{
3209 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
3210 union acpi_object *obj;
3211 int rc;
3212
3213 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
3214 obj = (union acpi_object *)buffer.pointer;
3215 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
3216 printk(IBM_ERR "Unknown BCLL data, "
3217 "please report this to %s\n", IBM_MAIL);
3218 rc = 0;
3219 } else {
3220 rc = obj->package.count;
3221 }
3222 } else {
3223 return 0;
3224 }
3225
3226 kfree(buffer.pointer);
3227 return rc;
3228}
3229
3230static acpi_status __init brightness_find_bcll(acpi_handle handle, u32 lvl,
3231 void *context, void **rv)
3232{
3233 char name[ACPI_PATH_SEGMENT_LENGTH];
3234 struct acpi_buffer buffer = { sizeof(name), &name };
3235
3236 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
3237 !strncmp("BCLL", name, sizeof(name) - 1)) {
3238 if (tpacpi_query_bcll_levels(handle) == 16) {
3239 *rv = handle;
3240 return AE_CTRL_TERMINATE;
3241 } else {
3242 return AE_OK;
3243 }
3244 } else {
3245 return AE_OK;
3246 }
3247}
3248
3249static int __init brightness_check_levels(void)
3250{
3251 int status;
3252 void *found_node = NULL;
3253
3254 if (!vid_handle) {
3255 IBM_ACPIHANDLE_INIT(vid);
3256 }
3257 if (!vid_handle)
3258 return 0;
3259
3260 /* Search for a BCLL package with 16 levels */
3261 status = acpi_walk_namespace(ACPI_TYPE_PACKAGE, vid_handle, 3,
3262 brightness_find_bcll, NULL, &found_node);
3263
3264 return (ACPI_SUCCESS(status) && found_node != NULL);
3265}
3266
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02003267static acpi_status __init brightness_find_bcl(acpi_handle handle, u32 lvl,
3268 void *context, void **rv)
3269{
3270 char name[ACPI_PATH_SEGMENT_LENGTH];
3271 struct acpi_buffer buffer = { sizeof(name), &name };
3272
3273 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
3274 !strncmp("_BCL", name, sizeof(name) - 1)) {
3275 *rv = handle;
3276 return AE_CTRL_TERMINATE;
3277 } else {
3278 return AE_OK;
3279 }
3280}
3281
3282static int __init brightness_check_std_acpi_support(void)
3283{
3284 int status;
3285 void *found_node = NULL;
3286
3287 if (!vid_handle) {
3288 IBM_ACPIHANDLE_INIT(vid);
3289 }
3290 if (!vid_handle)
3291 return 0;
3292
3293 /* Search for a _BCL method, but don't execute it */
3294 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
3295 brightness_find_bcl, NULL, &found_node);
3296
3297 return (ACPI_SUCCESS(status) && found_node != NULL);
3298}
3299
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003300static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02003301{
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02003302 int b;
3303
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003304 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
3305
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03003306 mutex_init(&brightness_mutex);
3307
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02003308 if (!brightness_enable) {
3309 dbg_printk(TPACPI_DBG_INIT,
3310 "brightness support disabled by module parameter\n");
3311 return 1;
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02003312 } else if (brightness_enable > 1) {
3313 if (brightness_check_std_acpi_support()) {
3314 printk(IBM_NOTICE
3315 "standard ACPI backlight interface available, not loading native one...\n");
3316 return 1;
3317 }
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02003318 }
3319
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03003320 if (!brightness_mode) {
3321 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
3322 brightness_mode = 2;
3323 else
3324 brightness_mode = 3;
3325
3326 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
3327 brightness_mode);
3328 }
3329
3330 if (brightness_mode > 3)
3331 return -EINVAL;
3332
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02003333 tp_features.bright_16levels =
3334 thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO &&
3335 brightness_check_levels();
3336
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02003337 b = brightness_get(NULL);
3338 if (b < 0)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03003339 return 1;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02003340
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02003341 if (tp_features.bright_16levels)
3342 printk(IBM_INFO "detected a 16-level brightness capable ThinkPad\n");
3343
Henrique de Moraes Holschuh7d5a0152007-04-24 11:48:20 -03003344 ibm_backlight_device = backlight_device_register(
3345 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
3346 &ibm_backlight_data);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02003347 if (IS_ERR(ibm_backlight_device)) {
3348 printk(IBM_ERR "Could not register backlight device\n");
3349 return PTR_ERR(ibm_backlight_device);
3350 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003351 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02003352
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02003353 ibm_backlight_device->props.max_brightness =
3354 (tp_features.bright_16levels)? 15 : 7;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02003355 ibm_backlight_device->props.brightness = b;
3356 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00003357
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02003358 return 0;
3359}
3360
3361static void brightness_exit(void)
3362{
3363 if (ibm_backlight_device) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003364 vdbg_printk(TPACPI_DBG_EXIT,
3365 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02003366 backlight_device_unregister(ibm_backlight_device);
3367 ibm_backlight_device = NULL;
3368 }
3369}
3370
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003371static int brightness_update_status(struct backlight_device *bd)
3372{
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02003373 /* it is the backlight class's job (caller) to handle
3374 * EINTR and other errors properly */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003375 return brightness_set(
3376 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
3377 bd->props.power == FB_BLANK_UNBLANK) ?
3378 bd->props.brightness : 0);
3379}
3380
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03003381/*
3382 * ThinkPads can read brightness from two places: EC 0x31, or
3383 * CMOS NVRAM byte 0x5E, bits 0-3.
3384 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003385static int brightness_get(struct backlight_device *bd)
3386{
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03003387 u8 lec = 0, lcmos = 0, level = 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003388
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03003389 if (brightness_mode & 1) {
3390 if (!acpi_ec_read(brightness_offset, &lec))
3391 return -EIO;
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02003392 lec &= (tp_features.bright_16levels)? 0x0f : 0x07;
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03003393 level = lec;
3394 };
3395 if (brightness_mode & 2) {
3396 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
3397 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
3398 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02003399 lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03003400 level = lcmos;
3401 }
3402
3403 if (brightness_mode == 3 && lec != lcmos) {
3404 printk(IBM_ERR
3405 "CMOS NVRAM (%u) and EC (%u) do not agree "
3406 "on display brightness level\n",
3407 (unsigned int) lcmos,
3408 (unsigned int) lec);
3409 return -EIO;
3410 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003411
3412 return level;
3413}
3414
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02003415/* May return EINTR which can always be mapped to ERESTARTSYS */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003416static int brightness_set(int value)
3417{
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03003418 int cmos_cmd, inc, i, res;
3419 int current_value;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003420
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02003421 if (value > ((tp_features.bright_16levels)? 15 : 7))
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03003422 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003423
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03003424 res = mutex_lock_interruptible(&brightness_mutex);
3425 if (res < 0)
3426 return res;
3427
3428 current_value = brightness_get(NULL);
3429 if (current_value < 0) {
3430 res = current_value;
3431 goto errout;
3432 }
3433
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03003434 cmos_cmd = value > current_value ?
3435 TP_CMOS_BRIGHTNESS_UP :
3436 TP_CMOS_BRIGHTNESS_DOWN;
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02003437 inc = (value > current_value)? 1 : -1;
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03003438
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03003439 res = 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003440 for (i = current_value; i != value; i += inc) {
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03003441 if ((brightness_mode & 2) &&
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03003442 issue_thinkpad_cmos_command(cmos_cmd)) {
3443 res = -EIO;
3444 goto errout;
3445 }
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03003446 if ((brightness_mode & 1) &&
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03003447 !acpi_ec_write(brightness_offset, i + inc)) {
3448 res = -EIO;
3449 goto errout;;
3450 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003451 }
3452
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03003453errout:
3454 mutex_unlock(&brightness_mutex);
3455 return res;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003456}
3457
3458static int brightness_read(char *p)
3459{
3460 int len = 0;
3461 int level;
3462
3463 if ((level = brightness_get(NULL)) < 0) {
3464 len += sprintf(p + len, "level:\t\tunreadable\n");
3465 } else {
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02003466 len += sprintf(p + len, "level:\t\t%d\n", level);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003467 len += sprintf(p + len, "commands:\tup, down\n");
3468 len += sprintf(p + len, "commands:\tlevel <level>"
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02003469 " (<level> is 0-%d)\n",
3470 (tp_features.bright_16levels) ? 15 : 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003471 }
3472
3473 return len;
3474}
3475
3476static int brightness_write(char *buf)
3477{
3478 int level;
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02003479 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003480 char *cmd;
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02003481 int max_level = (tp_features.bright_16levels) ? 15 : 7;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003482
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02003483 level = brightness_get(NULL);
3484 if (level < 0)
3485 return level;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003486
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02003487 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003488 if (strlencmp(cmd, "up") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02003489 if (level < max_level)
3490 level++;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003491 } else if (strlencmp(cmd, "down") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02003492 if (level > 0)
3493 level--;
3494 } else if (sscanf(cmd, "level %d", &level) == 1 &&
3495 level >= 0 && level <= max_level) {
3496 /* new level set */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003497 } else
3498 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003499 }
3500
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02003501 /*
3502 * Now we know what the final level should be, so we try to set it.
3503 * Doing it this way makes the syscall restartable in case of EINTR
3504 */
3505 rc = brightness_set(level);
3506 return (rc == -EINTR)? ERESTARTSYS : rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003507}
3508
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003509static struct ibm_struct brightness_driver_data = {
3510 .name = "brightness",
3511 .read = brightness_read,
3512 .write = brightness_write,
3513 .exit = brightness_exit,
3514};
3515
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003516/*************************************************************************
3517 * Volume subdriver
3518 */
3519
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003520static int volume_read(char *p)
3521{
3522 int len = 0;
3523 u8 level;
3524
3525 if (!acpi_ec_read(volume_offset, &level)) {
3526 len += sprintf(p + len, "level:\t\tunreadable\n");
3527 } else {
3528 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
3529 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
3530 len += sprintf(p + len, "commands:\tup, down, mute\n");
3531 len += sprintf(p + len, "commands:\tlevel <level>"
3532 " (<level> is 0-15)\n");
3533 }
3534
3535 return len;
3536}
3537
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003538static int volume_write(char *buf)
3539{
3540 int cmos_cmd, inc, i;
3541 u8 level, mute;
3542 int new_level, new_mute;
3543 char *cmd;
3544
3545 while ((cmd = next_cmd(&buf))) {
3546 if (!acpi_ec_read(volume_offset, &level))
3547 return -EIO;
3548 new_mute = mute = level & 0x40;
3549 new_level = level = level & 0xf;
3550
3551 if (strlencmp(cmd, "up") == 0) {
3552 if (mute)
3553 new_mute = 0;
3554 else
3555 new_level = level == 15 ? 15 : level + 1;
3556 } else if (strlencmp(cmd, "down") == 0) {
3557 if (mute)
3558 new_mute = 0;
3559 else
3560 new_level = level == 0 ? 0 : level - 1;
3561 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
3562 new_level >= 0 && new_level <= 15) {
3563 /* new_level set */
3564 } else if (strlencmp(cmd, "mute") == 0) {
3565 new_mute = 0x40;
3566 } else
3567 return -EINVAL;
3568
3569 if (new_level != level) { /* mute doesn't change */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003570 cmos_cmd = new_level > level ? TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003571 inc = new_level > level ? 1 : -1;
3572
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03003573 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003574 !acpi_ec_write(volume_offset, level)))
3575 return -EIO;
3576
3577 for (i = level; i != new_level; i += inc)
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03003578 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003579 !acpi_ec_write(volume_offset, i + inc))
3580 return -EIO;
3581
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03003582 if (mute && (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003583 !acpi_ec_write(volume_offset,
3584 new_level + mute)))
3585 return -EIO;
3586 }
3587
3588 if (new_mute != mute) { /* level doesn't change */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003589 cmos_cmd = new_mute ? TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003590
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03003591 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003592 !acpi_ec_write(volume_offset, level + new_mute))
3593 return -EIO;
3594 }
3595 }
3596
3597 return 0;
3598}
3599
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003600static struct ibm_struct volume_driver_data = {
3601 .name = "volume",
3602 .read = volume_read,
3603 .write = volume_write,
3604};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003605
3606/*************************************************************************
3607 * Fan subdriver
3608 */
3609
3610/*
3611 * FAN ACCESS MODES
3612 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003613 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003614 * ACPI GFAN method: returns fan level
3615 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003616 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03003617 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003618 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003619 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003620 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
3621 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03003622 * EC 0x2f (HFSP) might be available *for reading*, but do not use
3623 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003624 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003625 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03003626 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
3627 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003628 *
3629 * Fan speed changes of any sort (including those caused by the
3630 * disengaged mode) are usually done slowly by the firmware as the
3631 * maximum ammount of fan duty cycle change per second seems to be
3632 * limited.
3633 *
3634 * Reading is not available if GFAN exists.
3635 * Writing is not available if SFAN exists.
3636 *
3637 * Bits
3638 * 7 automatic mode engaged;
3639 * (default operation mode of the ThinkPad)
3640 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03003641 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003642 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03003643 * the tachometer while the fan controller ramps up
3644 * the speed (which can take up to a few *minutes*).
3645 * Speeds up fan to 100% duty-cycle, which is far above
3646 * the standard RPM levels. It is not impossible that
3647 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003648 * 5-3 unused in some models. Extra bits for fan level
3649 * in others, but still useless as all values above
3650 * 7 map to the same speed as level 7 in these models.
3651 * 2-0 fan level (0..7 usually)
3652 * 0x00 = stop
3653 * 0x07 = max (set when temperatures critical)
3654 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003655 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003656 *
3657 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
3658 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
3659 * does so, its initial value is meaningless (0x07).
3660 *
3661 * For firmware bugs, refer to:
3662 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
3663 *
3664 * ----
3665 *
3666 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
3667 * Main fan tachometer reading (in RPM)
3668 *
3669 * This register is present on all ThinkPads with a new-style EC, and
3670 * it is known not to be present on the A21m/e, and T22, as there is
3671 * something else in offset 0x84 according to the ACPI DSDT. Other
3672 * ThinkPads from this same time period (and earlier) probably lack the
3673 * tachometer as well.
3674 *
3675 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
3676 * was never fixed by IBM to report the EC firmware version string
3677 * probably support the tachometer (like the early X models), so
3678 * detecting it is quite hard. We need more data to know for sure.
3679 *
3680 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
3681 * might result.
3682 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03003683 * FIRMWARE BUG: may go stale while the EC is switching to full speed
3684 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003685 *
3686 * For firmware bugs, refer to:
3687 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
3688 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003689 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003690 * ThinkPad X31, X40, X41. Not available in the X60.
3691 *
3692 * FANS ACPI handle: takes three arguments: low speed, medium speed,
3693 * high speed. ACPI DSDT seems to map these three speeds to levels
3694 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
3695 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
3696 *
3697 * The speeds are stored on handles
3698 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
3699 *
3700 * There are three default speed sets, acessible as handles:
3701 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
3702 *
3703 * ACPI DSDT switches which set is in use depending on various
3704 * factors.
3705 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003706 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003707 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
3708 * but the ACPI tables just mention level 7.
3709 */
3710
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003711static enum fan_status_access_mode fan_status_access_mode;
3712static enum fan_control_access_mode fan_control_access_mode;
3713static enum fan_control_commands fan_control_commands;
3714
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02003715static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003716static u8 fan_control_desired_level;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02003717
Len Brown25c68a32006-12-08 04:43:41 -05003718static void fan_watchdog_fire(struct work_struct *ignored);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003719static int fan_watchdog_maxinterval;
Len Brown25c68a32006-12-08 04:43:41 -05003720static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003721
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003722IBM_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
3723IBM_HANDLE(gfan, ec, "GFAN", /* 570 */
3724 "\\FSPD", /* 600e/x, 770e, 770x */
3725 ); /* all others */
3726IBM_HANDLE(sfan, ec, "SFAN", /* 570 */
3727 "JFNS", /* 770x-JL */
3728 ); /* all others */
3729
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003730/*
3731 * SYSFS fan layout: hwmon compatible (device)
3732 *
3733 * pwm*_enable:
3734 * 0: "disengaged" mode
3735 * 1: manual mode
3736 * 2: native EC "auto" mode (recommended, hardware default)
3737 *
3738 * pwm*: set speed in manual mode, ignored otherwise.
3739 * 0 is level 0; 255 is level 7. Intermediate points done with linear
3740 * interpolation.
3741 *
3742 * fan*_input: tachometer reading, RPM
3743 *
3744 *
3745 * SYSFS fan layout: extensions
3746 *
3747 * fan_watchdog (driver):
3748 * fan watchdog interval in seconds, 0 disables (default), max 120
3749 */
3750
3751/* sysfs fan pwm1_enable ----------------------------------------------- */
3752static ssize_t fan_pwm1_enable_show(struct device *dev,
3753 struct device_attribute *attr,
3754 char *buf)
3755{
3756 int res, mode;
3757 u8 status;
3758
3759 res = fan_get_status_safe(&status);
3760 if (res)
3761 return res;
3762
3763 if (unlikely(tp_features.fan_ctrl_status_undef)) {
3764 if (status != fan_control_initial_status) {
3765 tp_features.fan_ctrl_status_undef = 0;
3766 } else {
3767 /* Return most likely status. In fact, it
3768 * might be the only possible status */
3769 status = TP_EC_FAN_AUTO;
3770 }
3771 }
3772
3773 if (status & TP_EC_FAN_FULLSPEED) {
3774 mode = 0;
3775 } else if (status & TP_EC_FAN_AUTO) {
3776 mode = 2;
3777 } else
3778 mode = 1;
3779
3780 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
3781}
3782
3783static ssize_t fan_pwm1_enable_store(struct device *dev,
3784 struct device_attribute *attr,
3785 const char *buf, size_t count)
3786{
3787 unsigned long t;
3788 int res, level;
3789
3790 if (parse_strtoul(buf, 2, &t))
3791 return -EINVAL;
3792
3793 switch (t) {
3794 case 0:
3795 level = TP_EC_FAN_FULLSPEED;
3796 break;
3797 case 1:
3798 level = TPACPI_FAN_LAST_LEVEL;
3799 break;
3800 case 2:
3801 level = TP_EC_FAN_AUTO;
3802 break;
3803 case 3:
3804 /* reserved for software-controlled auto mode */
3805 return -ENOSYS;
3806 default:
3807 return -EINVAL;
3808 }
3809
3810 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03003811 if (res == -ENXIO)
3812 return -EINVAL;
3813 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003814 return res;
3815
3816 fan_watchdog_reset();
3817
3818 return count;
3819}
3820
3821static struct device_attribute dev_attr_fan_pwm1_enable =
3822 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
3823 fan_pwm1_enable_show, fan_pwm1_enable_store);
3824
3825/* sysfs fan pwm1 ------------------------------------------------------ */
3826static ssize_t fan_pwm1_show(struct device *dev,
3827 struct device_attribute *attr,
3828 char *buf)
3829{
3830 int res;
3831 u8 status;
3832
3833 res = fan_get_status_safe(&status);
3834 if (res)
3835 return res;
3836
3837 if (unlikely(tp_features.fan_ctrl_status_undef)) {
3838 if (status != fan_control_initial_status) {
3839 tp_features.fan_ctrl_status_undef = 0;
3840 } else {
3841 status = TP_EC_FAN_AUTO;
3842 }
3843 }
3844
3845 if ((status &
3846 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
3847 status = fan_control_desired_level;
3848
3849 if (status > 7)
3850 status = 7;
3851
3852 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
3853}
3854
3855static ssize_t fan_pwm1_store(struct device *dev,
3856 struct device_attribute *attr,
3857 const char *buf, size_t count)
3858{
3859 unsigned long s;
3860 int rc;
3861 u8 status, newlevel;
3862
3863 if (parse_strtoul(buf, 255, &s))
3864 return -EINVAL;
3865
3866 /* scale down from 0-255 to 0-7 */
3867 newlevel = (s >> 5) & 0x07;
3868
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02003869 if (mutex_lock_interruptible(&fan_mutex))
3870 return -ERESTARTSYS;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003871
3872 rc = fan_get_status(&status);
3873 if (!rc && (status &
3874 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
3875 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03003876 if (rc == -ENXIO)
3877 rc = -EINVAL;
3878 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003879 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03003880 fan_watchdog_reset();
3881 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003882 }
3883
3884 mutex_unlock(&fan_mutex);
3885 return (rc)? rc : count;
3886}
3887
3888static struct device_attribute dev_attr_fan_pwm1 =
3889 __ATTR(pwm1, S_IWUSR | S_IRUGO,
3890 fan_pwm1_show, fan_pwm1_store);
3891
3892/* sysfs fan fan1_input ------------------------------------------------ */
3893static ssize_t fan_fan1_input_show(struct device *dev,
3894 struct device_attribute *attr,
3895 char *buf)
3896{
3897 int res;
3898 unsigned int speed;
3899
3900 res = fan_get_speed(&speed);
3901 if (res < 0)
3902 return res;
3903
3904 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
3905}
3906
3907static struct device_attribute dev_attr_fan_fan1_input =
3908 __ATTR(fan1_input, S_IRUGO,
3909 fan_fan1_input_show, NULL);
3910
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03003911/* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003912static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
3913 char *buf)
3914{
3915 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
3916}
3917
3918static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
3919 const char *buf, size_t count)
3920{
3921 unsigned long t;
3922
3923 if (parse_strtoul(buf, 120, &t))
3924 return -EINVAL;
3925
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03003926 if (!fan_control_allowed)
3927 return -EPERM;
3928
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003929 fan_watchdog_maxinterval = t;
3930 fan_watchdog_reset();
3931
3932 return count;
3933}
3934
3935static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
3936 fan_fan_watchdog_show, fan_fan_watchdog_store);
3937
3938/* --------------------------------------------------------------------- */
3939static struct attribute *fan_attributes[] = {
3940 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
3941 &dev_attr_fan_fan1_input.attr,
3942 NULL
3943};
3944
3945static const struct attribute_group fan_attr_group = {
3946 .attrs = fan_attributes,
3947};
3948
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003949static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003950{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003951 int rc;
3952
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003953 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
3954
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003955 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003956 fan_status_access_mode = TPACPI_FAN_NONE;
3957 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003958 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003959 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003960 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003961 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003962
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003963 IBM_ACPIHANDLE_INIT(fans);
3964 IBM_ACPIHANDLE_INIT(gfan);
3965 IBM_ACPIHANDLE_INIT(sfan);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003966
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003967 if (gfan_handle) {
3968 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003969 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003970 } else {
3971 /* all other ThinkPads: note that even old-style
3972 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02003973 if (likely(acpi_ec_read(fan_status_offset,
3974 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003975 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02003976
3977 /* In some ThinkPads, neither the EC nor the ACPI
3978 * DSDT initialize the fan status, and it ends up
3979 * being set to 0x07 when it *could* be either
3980 * 0x07 or 0x80.
3981 *
3982 * Enable for TP-1Y (T43), TP-78 (R51e),
3983 * TP-76 (R52), TP-70 (T43, R52), which are known
3984 * to be buggy. */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03003985 if (fan_control_initial_status == 0x07) {
3986 switch (thinkpad_id.ec_model) {
3987 case 0x5931: /* TP-1Y */
3988 case 0x3837: /* TP-78 */
3989 case 0x3637: /* TP-76 */
3990 case 0x3037: /* TP-70 */
3991 printk(IBM_NOTICE
3992 "fan_init: initial fan status is "
3993 "unknown, assuming it is in auto "
3994 "mode\n");
3995 tp_features.fan_ctrl_status_undef = 1;
3996 ;;
3997 }
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02003998 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003999 } else {
4000 printk(IBM_ERR
4001 "ThinkPad ACPI EC access misbehaving, "
4002 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004003 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02004004 }
4005 }
4006
4007 if (sfan_handle) {
4008 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004009 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02004010 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004011 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02004012 } else {
4013 if (!gfan_handle) {
4014 /* gfan without sfan means no fan control */
4015 /* all other models implement TP EC 0x2f control */
4016
4017 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02004018 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02004019 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004020 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02004021 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004022 TPACPI_FAN_CMD_SPEED |
4023 TPACPI_FAN_CMD_LEVEL |
4024 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02004025 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004026 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02004027 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004028 TPACPI_FAN_CMD_LEVEL |
4029 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02004030 }
4031 }
4032 }
4033
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004034 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
4035 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
4036 fan_control_access_mode != TPACPI_FAN_WR_NONE),
4037 fan_status_access_mode, fan_control_access_mode);
4038
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03004039 /* fan control master switch */
4040 if (!fan_control_allowed) {
4041 fan_control_access_mode = TPACPI_FAN_WR_NONE;
4042 fan_control_commands = 0;
4043 dbg_printk(TPACPI_DBG_INIT,
4044 "fan control features disabled by parameter\n");
4045 }
4046
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004047 /* update fan_control_desired_level */
4048 if (fan_status_access_mode != TPACPI_FAN_NONE)
4049 fan_get_status_safe(NULL);
4050
4051 if (fan_status_access_mode != TPACPI_FAN_NONE ||
4052 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03004053 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004054 &fan_attr_group);
4055 if (!(rc < 0))
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03004056 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004057 &driver_attr_fan_watchdog);
4058 if (rc < 0)
4059 return rc;
4060 return 0;
4061 } else
4062 return 1;
4063}
4064
4065/*
4066 * Call with fan_mutex held
4067 */
4068static void fan_update_desired_level(u8 status)
4069{
4070 if ((status &
4071 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
4072 if (status > 7)
4073 fan_control_desired_level = 7;
4074 else
4075 fan_control_desired_level = status;
4076 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02004077}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004078
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02004079static int fan_get_status(u8 *status)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004080{
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02004081 u8 s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004082
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02004083 /* TODO:
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004084 * Add TPACPI_FAN_RD_ACPI_FANS ? */
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02004085
Henrique de Moraes Holschuh3ef8a602006-11-24 11:47:10 -02004086 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004087 case TPACPI_FAN_RD_ACPI_GFAN:
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004088 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02004089
4090 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004091 return -EIO;
4092
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02004093 if (likely(status))
4094 *status = s & 0x07;
4095
4096 break;
4097
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004098 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02004099 /* all except 570, 600e/x, 770e, 770x */
4100 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
4101 return -EIO;
4102
4103 if (likely(status))
4104 *status = s;
4105
4106 break;
4107
4108 default:
4109 return -ENXIO;
4110 }
4111
4112 return 0;
4113}
4114
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004115static int fan_get_status_safe(u8 *status)
4116{
4117 int rc;
4118 u8 s;
4119
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02004120 if (mutex_lock_interruptible(&fan_mutex))
4121 return -ERESTARTSYS;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004122 rc = fan_get_status(&s);
4123 if (!rc)
4124 fan_update_desired_level(s);
4125 mutex_unlock(&fan_mutex);
4126
4127 if (status)
4128 *status = s;
4129
4130 return rc;
4131}
4132
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004133static void fan_exit(void)
4134{
Henrique de Moraes Holschuh99fba3f2007-04-21 11:08:44 -03004135 vdbg_printk(TPACPI_DBG_EXIT, "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004136
4137 /* FIXME: can we really do this unconditionally? */
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03004138 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
4139 driver_remove_file(&tpacpi_hwmon_pdriver.driver, &driver_attr_fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004140
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004141 cancel_delayed_work(&fan_watchdog_task);
4142 flush_scheduled_work();
4143}
4144
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02004145static int fan_get_speed(unsigned int *speed)
4146{
4147 u8 hi, lo;
4148
4149 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004150 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02004151 /* all except 570, 600e/x, 770e, 770x */
4152 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
4153 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
4154 return -EIO;
4155
4156 if (likely(speed))
4157 *speed = (hi << 8) | lo;
4158
4159 break;
4160
4161 default:
4162 return -ENXIO;
4163 }
4164
4165 return 0;
4166}
4167
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004168static void fan_watchdog_fire(struct work_struct *ignored)
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02004169{
Henrique de Moraes Holschuh99fba3f2007-04-21 11:08:44 -03004170 int rc;
4171
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03004172 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
4173 return;
4174
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004175 printk(IBM_NOTICE "fan watchdog: enabling fan\n");
Henrique de Moraes Holschuh99fba3f2007-04-21 11:08:44 -03004176 rc = fan_set_enable();
4177 if (rc < 0) {
4178 printk(IBM_ERR "fan watchdog: error %d while enabling fan, "
4179 "will try again later...\n", -rc);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004180 /* reschedule for later */
4181 fan_watchdog_reset();
4182 }
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02004183}
4184
4185static void fan_watchdog_reset(void)
4186{
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03004187 static int fan_watchdog_active;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02004188
Henrique de Moraes Holschuh4985cd02007-04-27 22:00:10 -03004189 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
4190 return;
4191
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02004192 if (fan_watchdog_active)
4193 cancel_delayed_work(&fan_watchdog_task);
4194
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03004195 if (fan_watchdog_maxinterval > 0 &&
4196 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02004197 fan_watchdog_active = 1;
4198 if (!schedule_delayed_work(&fan_watchdog_task,
4199 msecs_to_jiffies(fan_watchdog_maxinterval
4200 * 1000))) {
4201 printk(IBM_ERR "failed to schedule the fan watchdog, "
4202 "watchdog will not trigger\n");
4203 }
4204 } else
4205 fan_watchdog_active = 0;
4206}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004207
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004208static int fan_set_level(int level)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004209{
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03004210 if (!fan_control_allowed)
4211 return -EPERM;
4212
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004213 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004214 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004215 if (level >= 0 && level <= 7) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004216 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004217 return -EIO;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004218 } else
4219 return -EINVAL;
4220 break;
4221
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004222 case TPACPI_FAN_WR_ACPI_FANS:
4223 case TPACPI_FAN_WR_TPEC:
4224 if ((level != TP_EC_FAN_AUTO) &&
4225 (level != TP_EC_FAN_FULLSPEED) &&
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02004226 ((level < 0) || (level > 7)))
4227 return -EINVAL;
4228
Henrique de Moraes Holschuheaa75712007-04-24 11:48:18 -03004229 /* safety net should the EC not support AUTO
4230 * or FULLSPEED mode bits and just ignore them */
4231 if (level & TP_EC_FAN_FULLSPEED)
4232 level |= 7; /* safety min speed 7 */
4233 else if (level & TP_EC_FAN_FULLSPEED)
4234 level |= 4; /* safety min speed 4 */
4235
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004236 if (!acpi_ec_write(fan_status_offset, level))
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02004237 return -EIO;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02004238 else
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004239 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02004240 break;
4241
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004242 default:
4243 return -ENXIO;
4244 }
4245 return 0;
4246}
4247
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004248static int fan_set_level_safe(int level)
4249{
4250 int rc;
4251
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03004252 if (!fan_control_allowed)
4253 return -EPERM;
4254
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02004255 if (mutex_lock_interruptible(&fan_mutex))
4256 return -ERESTARTSYS;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004257
4258 if (level == TPACPI_FAN_LAST_LEVEL)
4259 level = fan_control_desired_level;
4260
4261 rc = fan_set_level(level);
4262 if (!rc)
4263 fan_update_desired_level(level);
4264
4265 mutex_unlock(&fan_mutex);
4266 return rc;
4267}
4268
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004269static int fan_set_enable(void)
4270{
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02004271 u8 s;
4272 int rc;
4273
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03004274 if (!fan_control_allowed)
4275 return -EPERM;
4276
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02004277 if (mutex_lock_interruptible(&fan_mutex))
4278 return -ERESTARTSYS;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004279
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004280 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004281 case TPACPI_FAN_WR_ACPI_FANS:
4282 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004283 rc = fan_get_status(&s);
4284 if (rc < 0)
4285 break;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02004286
4287 /* Don't go out of emergency fan mode */
Henrique de Moraes Holschuheaa75712007-04-24 11:48:18 -03004288 if (s != 7) {
4289 s &= 0x07;
4290 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
4291 }
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02004292
4293 if (!acpi_ec_write(fan_status_offset, s))
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004294 rc = -EIO;
4295 else {
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004296 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004297 rc = 0;
4298 }
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02004299 break;
4300
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004301 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004302 rc = fan_get_status(&s);
4303 if (rc < 0)
4304 break;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02004305
4306 s &= 0x07;
4307
4308 /* Set fan to at least level 4 */
Henrique de Moraes Holschuheaa75712007-04-24 11:48:18 -03004309 s |= 4;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02004310
4311 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004312 rc= -EIO;
4313 else
4314 rc = 0;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004315 break;
4316
4317 default:
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004318 rc = -ENXIO;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004319 }
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004320
4321 mutex_unlock(&fan_mutex);
4322 return rc;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004323}
4324
4325static int fan_set_disable(void)
4326{
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004327 int rc;
4328
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03004329 if (!fan_control_allowed)
4330 return -EPERM;
4331
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02004332 if (mutex_lock_interruptible(&fan_mutex))
4333 return -ERESTARTSYS;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004334
4335 rc = 0;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004336 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004337 case TPACPI_FAN_WR_ACPI_FANS:
4338 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004339 if (!acpi_ec_write(fan_status_offset, 0x00))
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004340 rc = -EIO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004341 else {
4342 fan_control_desired_level = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004343 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004344 }
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004345 break;
4346
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004347 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02004348 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004349 rc = -EIO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004350 else
4351 fan_control_desired_level = 0;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02004352 break;
4353
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004354 default:
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004355 rc = -ENXIO;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004356 }
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004357
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004358
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004359 mutex_unlock(&fan_mutex);
4360 return rc;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004361}
4362
4363static int fan_set_speed(int speed)
4364{
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004365 int rc;
4366
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03004367 if (!fan_control_allowed)
4368 return -EPERM;
4369
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02004370 if (mutex_lock_interruptible(&fan_mutex))
4371 return -ERESTARTSYS;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004372
4373 rc = 0;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004374 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004375 case TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004376 if (speed >= 0 && speed <= 65535) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004377 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
4378 speed, speed, speed))
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004379 rc = -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004380 } else
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004381 rc = -EINVAL;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004382 break;
4383
4384 default:
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004385 rc = -ENXIO;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004386 }
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004387
4388 mutex_unlock(&fan_mutex);
4389 return rc;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004390}
4391
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004392static int fan_read(char *p)
4393{
4394 int len = 0;
4395 int rc;
4396 u8 status;
4397 unsigned int speed = 0;
4398
4399 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004400 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004401 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004402 if ((rc = fan_get_status_safe(&status)) < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004403 return rc;
4404
4405 len += sprintf(p + len, "status:\t\t%s\n"
4406 "level:\t\t%d\n",
4407 (status != 0) ? "enabled" : "disabled", status);
4408 break;
4409
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004410 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004411 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004412 if ((rc = fan_get_status_safe(&status)) < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004413 return rc;
4414
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004415 if (unlikely(tp_features.fan_ctrl_status_undef)) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004416 if (status != fan_control_initial_status)
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004417 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004418 else
4419 /* Return most likely status. In fact, it
4420 * might be the only possible status */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004421 status = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004422 }
4423
4424 len += sprintf(p + len, "status:\t\t%s\n",
4425 (status != 0) ? "enabled" : "disabled");
4426
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004427 if ((rc = fan_get_speed(&speed)) < 0)
4428 return rc;
4429
4430 len += sprintf(p + len, "speed:\t\t%d\n", speed);
4431
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004432 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004433 /* Disengaged mode takes precedence */
4434 len += sprintf(p + len, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004435 else if (status & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004436 len += sprintf(p + len, "level:\t\tauto\n");
4437 else
4438 len += sprintf(p + len, "level:\t\t%d\n", status);
4439 break;
4440
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004441 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004442 default:
4443 len += sprintf(p + len, "status:\t\tnot supported\n");
4444 }
4445
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004446 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004447 len += sprintf(p + len, "commands:\tlevel <level>");
4448
4449 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004450 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004451 len += sprintf(p + len, " (<level> is 0-7)\n");
4452 break;
4453
4454 default:
4455 len += sprintf(p + len, " (<level> is 0-7, "
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004456 "auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004457 break;
4458 }
4459 }
4460
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004461 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004462 len += sprintf(p + len, "commands:\tenable, disable\n"
4463 "commands:\twatchdog <timeout> (<timeout> is 0 (off), "
4464 "1-120 (seconds))\n");
4465
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004466 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004467 len += sprintf(p + len, "commands:\tspeed <speed>"
4468 " (<speed> is 0-65535)\n");
4469
4470 return len;
4471}
4472
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004473static int fan_write_cmd_level(const char *cmd, int *rc)
4474{
4475 int level;
4476
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02004477 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004478 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004479 else if ((strlencmp(cmd, "level disengaged") == 0) |
4480 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004481 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02004482 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004483 return 0;
4484
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004485 if ((*rc = fan_set_level_safe(level)) == -ENXIO)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004486 printk(IBM_ERR "level command accepted for unsupported "
4487 "access mode %d", fan_control_access_mode);
4488
4489 return 1;
4490}
4491
4492static int fan_write_cmd_enable(const char *cmd, int *rc)
4493{
4494 if (strlencmp(cmd, "enable") != 0)
4495 return 0;
4496
4497 if ((*rc = fan_set_enable()) == -ENXIO)
4498 printk(IBM_ERR "enable command accepted for unsupported "
4499 "access mode %d", fan_control_access_mode);
4500
4501 return 1;
4502}
4503
4504static int fan_write_cmd_disable(const char *cmd, int *rc)
4505{
4506 if (strlencmp(cmd, "disable") != 0)
4507 return 0;
4508
4509 if ((*rc = fan_set_disable()) == -ENXIO)
4510 printk(IBM_ERR "disable command accepted for unsupported "
4511 "access mode %d", fan_control_access_mode);
4512
4513 return 1;
4514}
4515
4516static int fan_write_cmd_speed(const char *cmd, int *rc)
4517{
4518 int speed;
4519
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02004520 /* TODO:
4521 * Support speed <low> <medium> <high> ? */
4522
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004523 if (sscanf(cmd, "speed %d", &speed) != 1)
4524 return 0;
4525
4526 if ((*rc = fan_set_speed(speed)) == -ENXIO)
4527 printk(IBM_ERR "speed command accepted for unsupported "
4528 "access mode %d", fan_control_access_mode);
4529
4530 return 1;
4531}
4532
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02004533static int fan_write_cmd_watchdog(const char *cmd, int *rc)
4534{
4535 int interval;
4536
4537 if (sscanf(cmd, "watchdog %d", &interval) != 1)
4538 return 0;
4539
4540 if (interval < 0 || interval > 120)
4541 *rc = -EINVAL;
4542 else
4543 fan_watchdog_maxinterval = interval;
4544
4545 return 1;
4546}
4547
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004548static int fan_write(char *buf)
4549{
4550 char *cmd;
4551 int rc = 0;
4552
4553 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004554 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004555 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004556 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004557 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02004558 fan_write_cmd_disable(cmd, &rc) ||
4559 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004560 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004561 fan_write_cmd_speed(cmd, &rc))
4562 )
4563 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02004564 else if (!rc)
4565 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004566 }
4567
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004568 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004569}
4570
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004571static struct ibm_struct fan_driver_data = {
4572 .name = "fan",
4573 .read = fan_read,
4574 .write = fan_write,
4575 .exit = fan_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004576};
4577
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004578/****************************************************************************
4579 ****************************************************************************
4580 *
4581 * Infrastructure
4582 *
4583 ****************************************************************************
4584 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03004586/* sysfs name ---------------------------------------------------------- */
4587static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
4588 struct device_attribute *attr,
4589 char *buf)
4590{
4591 return snprintf(buf, PAGE_SIZE, "%s\n", IBM_NAME);
4592}
4593
4594static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
4595 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
4596
4597/* --------------------------------------------------------------------- */
4598
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004599/* /proc support */
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03004600static struct proc_dir_entry *proc_dir;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004601
4602/* Subdriver registry */
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004603static LIST_HEAD(tpacpi_all_drivers);
4604
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004606/*
4607 * Module and infrastructure proble, init and exit handling
4608 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004610#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004611static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004612{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004613 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004614
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004615 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004616}
4617#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
4618
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004619static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620{
4621 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004622 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623 struct proc_dir_entry *entry;
4624
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004625 BUG_ON(ibm == NULL);
4626
4627 INIT_LIST_HEAD(&ibm->all_drivers);
4628
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004629 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630 return 0;
4631
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004632 dbg_printk(TPACPI_DBG_INIT,
4633 "probing for %s\n", ibm->name);
4634
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004635 if (iibm->init) {
4636 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004637 if (ret > 0)
4638 return 0; /* probe failed */
4639 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004641
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004642 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643 }
4644
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004645 if (ibm->acpi) {
4646 if (ibm->acpi->hid) {
4647 ret = register_tpacpi_subdriver(ibm);
4648 if (ret)
4649 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004650 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004651
4652 if (ibm->acpi->notify) {
4653 ret = setup_acpi_notify(ibm);
4654 if (ret == -ENODEV) {
4655 printk(IBM_NOTICE "disabling subdriver %s\n",
4656 ibm->name);
4657 ret = 0;
4658 goto err_out;
4659 }
4660 if (ret < 0)
4661 goto err_out;
4662 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004663 }
4664
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004665 dbg_printk(TPACPI_DBG_INIT,
4666 "%s installed\n", ibm->name);
4667
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004668 if (ibm->read) {
4669 entry = create_proc_entry(ibm->name,
4670 S_IFREG | S_IRUGO | S_IWUSR,
4671 proc_dir);
4672 if (!entry) {
4673 printk(IBM_ERR "unable to create proc entry %s\n",
4674 ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004675 ret = -ENODEV;
4676 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004677 }
4678 entry->owner = THIS_MODULE;
4679 entry->data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004680 entry->read_proc = &dispatch_procfs_read;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004681 if (ibm->write)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004682 entry->write_proc = &dispatch_procfs_write;
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004683 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004684 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004686 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
4687
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004689
4690err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004691 dbg_printk(TPACPI_DBG_INIT,
4692 "%s: at error exit path with result %d\n",
4693 ibm->name, ret);
4694
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004695 ibm_exit(ibm);
4696 return (ret < 0)? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697}
4698
4699static void ibm_exit(struct ibm_struct *ibm)
4700{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004701 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004702
4703 list_del_init(&ibm->all_drivers);
4704
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004705 if (ibm->flags.acpi_notify_installed) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004706 dbg_printk(TPACPI_DBG_EXIT,
4707 "%s: acpi_remove_notify_handler\n", ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004708 BUG_ON(!ibm->acpi);
4709 acpi_remove_notify_handler(*ibm->acpi->handle,
4710 ibm->acpi->type,
4711 dispatch_acpi_notify);
4712 ibm->flags.acpi_notify_installed = 0;
4713 ibm->flags.acpi_notify_installed = 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004714 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004716 if (ibm->flags.proc_created) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004717 dbg_printk(TPACPI_DBG_EXIT,
4718 "%s: remove_proc_entry\n", ibm->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719 remove_proc_entry(ibm->name, proc_dir);
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004720 ibm->flags.proc_created = 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004721 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004723 if (ibm->flags.acpi_driver_registered) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004724 dbg_printk(TPACPI_DBG_EXIT,
4725 "%s: acpi_bus_unregister_driver\n", ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004726 BUG_ON(!ibm->acpi);
4727 acpi_bus_unregister_driver(ibm->acpi->driver);
4728 kfree(ibm->acpi->driver);
4729 ibm->acpi->driver = NULL;
4730 ibm->flags.acpi_driver_registered = 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004731 }
4732
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004733 if (ibm->flags.init_called && ibm->exit) {
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004734 ibm->exit();
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004735 ibm->flags.init_called = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004737
4738 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739}
4740
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004741/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03004743static void __init get_thinkpad_model_data(struct thinkpad_id_data *tp)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004744{
Jeff Garzik18552562007-10-03 15:15:40 -04004745 const struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02004746 char ec_fw_string[18];
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004747
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03004748 if (!tp)
4749 return;
4750
4751 memset(tp, 0, sizeof(*tp));
4752
4753 if (dmi_name_in_vendors("IBM"))
4754 tp->vendor = PCI_VENDOR_ID_IBM;
4755 else if (dmi_name_in_vendors("LENOVO"))
4756 tp->vendor = PCI_VENDOR_ID_LENOVO;
4757 else
4758 return;
4759
4760 tp->bios_version_str = kstrdup(dmi_get_system_info(DMI_BIOS_VERSION),
4761 GFP_KERNEL);
4762 if (!tp->bios_version_str)
4763 return;
4764 tp->bios_model = tp->bios_version_str[0]
4765 | (tp->bios_version_str[1] << 8);
4766
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004767 /*
4768 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
4769 * X32 or newer, all Z series; Some models must have an
4770 * up-to-date BIOS or they will not be detected.
4771 *
4772 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
4773 */
4774 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02004775 if (sscanf(dev->name,
4776 "IBM ThinkPad Embedded Controller -[%17c",
4777 ec_fw_string) == 1) {
4778 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
4779 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03004780
4781 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
4782 tp->ec_model = ec_fw_string[0]
4783 | (ec_fw_string[1] << 8);
4784 break;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02004785 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004786 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03004787
4788 tp->model_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_VERSION),
4789 GFP_KERNEL);
4790 if (strnicmp(tp->model_str, "ThinkPad", 8) != 0) {
4791 kfree(tp->model_str);
4792 tp->model_str = NULL;
4793 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794}
4795
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004796static int __init probe_for_thinkpad(void)
4797{
4798 int is_thinkpad;
4799
4800 if (acpi_disabled)
4801 return -ENODEV;
4802
4803 /*
4804 * Non-ancient models have better DMI tagging, but very old models
4805 * don't.
4806 */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03004807 is_thinkpad = (thinkpad_id.model_str != NULL);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004808
4809 /* ec is required because many other handles are relative to it */
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004810 IBM_ACPIHANDLE_INIT(ec);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004811 if (!ec_handle) {
4812 if (is_thinkpad)
4813 printk(IBM_ERR
4814 "Not yet supported ThinkPad detected!\n");
4815 return -ENODEV;
4816 }
4817
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03004818 /*
4819 * Risks a regression on very old machines, but reduces potential
4820 * false positives a damn great deal
4821 */
4822 if (!is_thinkpad)
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03004823 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03004824
4825 if (!is_thinkpad && !force_load)
4826 return -ENODEV;
4827
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004828 return 0;
4829}
4830
4831
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004832/* Module init, exit, parameters */
4833
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004834static struct ibm_init_struct ibms_init[] __initdata = {
4835 {
4836 .init = thinkpad_acpi_driver_init,
4837 .data = &thinkpad_acpi_driver_data,
4838 },
4839 {
4840 .init = hotkey_init,
4841 .data = &hotkey_driver_data,
4842 },
4843 {
4844 .init = bluetooth_init,
4845 .data = &bluetooth_driver_data,
4846 },
4847 {
4848 .init = wan_init,
4849 .data = &wan_driver_data,
4850 },
4851 {
4852 .init = video_init,
4853 .data = &video_driver_data,
4854 },
4855 {
4856 .init = light_init,
4857 .data = &light_driver_data,
4858 },
4859#ifdef CONFIG_THINKPAD_ACPI_DOCK
4860 {
4861 .init = dock_init,
4862 .data = &dock_driver_data[0],
4863 },
4864 {
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03004865 .init = dock_init2,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004866 .data = &dock_driver_data[1],
4867 },
4868#endif
4869#ifdef CONFIG_THINKPAD_ACPI_BAY
4870 {
4871 .init = bay_init,
4872 .data = &bay_driver_data,
4873 },
4874#endif
4875 {
4876 .init = cmos_init,
4877 .data = &cmos_driver_data,
4878 },
4879 {
4880 .init = led_init,
4881 .data = &led_driver_data,
4882 },
4883 {
4884 .init = beep_init,
4885 .data = &beep_driver_data,
4886 },
4887 {
4888 .init = thermal_init,
4889 .data = &thermal_driver_data,
4890 },
4891 {
4892 .data = &ecdump_driver_data,
4893 },
4894 {
4895 .init = brightness_init,
4896 .data = &brightness_driver_data,
4897 },
4898 {
4899 .data = &volume_driver_data,
4900 },
4901 {
4902 .init = fan_init,
4903 .data = &fan_driver_data,
4904 },
4905};
4906
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004907static int __init set_ibm_param(const char *val, struct kernel_param *kp)
4908{
4909 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004910 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004911
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02004912 if (!kp || !kp->name || !val)
4913 return -EINVAL;
4914
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004915 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
4916 ibm = ibms_init[i].data;
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02004917 WARN_ON(ibm == NULL);
4918
4919 if (!ibm || !ibm->name)
4920 continue;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004921
4922 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
4923 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004924 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004925 strcpy(ibms_init[i].param, val);
4926 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004927 return 0;
4928 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004929 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004930
4931 return -EINVAL;
4932}
4933
4934static int experimental;
4935module_param(experimental, int, 0);
4936
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03004937static u32 dbg_level;
4938module_param_named(debug, dbg_level, uint, 0);
4939
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03004940static int force_load;
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03004941module_param(force_load, bool, 0);
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03004942
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03004943static int fan_control_allowed;
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03004944module_param_named(fan_control, fan_control_allowed, bool, 0);
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03004945
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03004946static int brightness_mode;
4947module_param_named(brightness_mode, brightness_mode, int, 0);
4948
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02004949static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
4950module_param(brightness_enable, uint, 0);
4951
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004952static unsigned int hotkey_report_mode;
4953module_param(hotkey_report_mode, uint, 0);
4954
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004955#define IBM_PARAM(feature) \
4956 module_param_call(feature, set_ibm_param, NULL, NULL, 0)
4957
4958IBM_PARAM(hotkey);
4959IBM_PARAM(bluetooth);
4960IBM_PARAM(video);
4961IBM_PARAM(light);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03004962#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004963IBM_PARAM(dock);
4964#endif
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03004965#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004966IBM_PARAM(bay);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03004967#endif /* CONFIG_THINKPAD_ACPI_BAY */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004968IBM_PARAM(cmos);
4969IBM_PARAM(led);
4970IBM_PARAM(beep);
4971IBM_PARAM(ecdump);
4972IBM_PARAM(brightness);
4973IBM_PARAM(volume);
4974IBM_PARAM(fan);
4975
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03004976static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977{
4978 int ret, i;
4979
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03004980 tpacpi_lifecycle = TPACPI_LIFE_INIT;
4981
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004982 /* Parameter checking */
4983 if (hotkey_report_mode > 2)
4984 return -EINVAL;
4985
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03004986 /* Driver-level probe */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03004987
4988 get_thinkpad_model_data(&thinkpad_id);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004989 ret = probe_for_thinkpad();
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03004990 if (ret) {
4991 thinkpad_acpi_module_exit();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004992 return ret;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03004993 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004994
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03004995 /* Driver initialization */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03004996
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004997 IBM_ACPIHANDLE_INIT(ecrd);
4998 IBM_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004999
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005000 proc_dir = proc_mkdir(IBM_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001 if (!proc_dir) {
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005002 printk(IBM_ERR "unable to create proc dir " IBM_PROC_DIR);
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03005003 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005004 return -ENODEV;
5005 }
5006 proc_dir->owner = THIS_MODULE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005007
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03005008 ret = platform_driver_register(&tpacpi_pdriver);
5009 if (ret) {
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005010 printk(IBM_ERR "unable to register main platform driver\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03005011 thinkpad_acpi_module_exit();
5012 return ret;
5013 }
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03005014 tp_features.platform_drv_registered = 1;
5015
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005016 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
5017 if (ret) {
5018 printk(IBM_ERR "unable to register hwmon platform driver\n");
5019 thinkpad_acpi_module_exit();
5020 return ret;
5021 }
5022 tp_features.sensors_pdrv_registered = 1;
5023
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03005024 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03005025 if (!ret) {
5026 tp_features.platform_drv_attrs_registered = 1;
5027 ret = tpacpi_create_driver_attributes(&tpacpi_hwmon_pdriver.driver);
5028 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03005029 if (ret) {
5030 printk(IBM_ERR "unable to create sysfs driver attributes\n");
5031 thinkpad_acpi_module_exit();
5032 return ret;
5033 }
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03005034 tp_features.sensors_pdrv_attrs_registered = 1;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03005035
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03005036
5037 /* Device initialization */
5038 tpacpi_pdev = platform_device_register_simple(IBM_DRVR_NAME, -1,
5039 NULL, 0);
5040 if (IS_ERR(tpacpi_pdev)) {
5041 ret = PTR_ERR(tpacpi_pdev);
5042 tpacpi_pdev = NULL;
5043 printk(IBM_ERR "unable to register platform device\n");
5044 thinkpad_acpi_module_exit();
5045 return ret;
5046 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005047 tpacpi_sensors_pdev = platform_device_register_simple(
5048 IBM_HWMON_DRVR_NAME,
5049 -1, NULL, 0);
5050 if (IS_ERR(tpacpi_sensors_pdev)) {
5051 ret = PTR_ERR(tpacpi_sensors_pdev);
5052 tpacpi_sensors_pdev = NULL;
5053 printk(IBM_ERR "unable to register hwmon platform device\n");
5054 thinkpad_acpi_module_exit();
5055 return ret;
5056 }
5057 ret = device_create_file(&tpacpi_sensors_pdev->dev,
5058 &dev_attr_thinkpad_acpi_pdev_name);
5059 if (ret) {
5060 printk(IBM_ERR
5061 "unable to create sysfs hwmon device attributes\n");
5062 thinkpad_acpi_module_exit();
5063 return ret;
5064 }
5065 tp_features.sensors_pdev_attrs_registered = 1;
5066 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03005067 if (IS_ERR(tpacpi_hwmon)) {
5068 ret = PTR_ERR(tpacpi_hwmon);
5069 tpacpi_hwmon = NULL;
5070 printk(IBM_ERR "unable to register hwmon device\n");
5071 thinkpad_acpi_module_exit();
5072 return ret;
5073 }
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03005074 mutex_init(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03005075 tpacpi_inputdev = input_allocate_device();
5076 if (!tpacpi_inputdev) {
5077 printk(IBM_ERR "unable to allocate input device\n");
5078 thinkpad_acpi_module_exit();
5079 return -ENOMEM;
5080 } else {
5081 /* Prepare input device, but don't register */
5082 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
5083 tpacpi_inputdev->phys = IBM_DRVR_NAME "/input0";
5084 tpacpi_inputdev->id.bustype = BUS_HOST;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03005085 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
5086 thinkpad_id.vendor :
5087 PCI_VENDOR_ID_IBM;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03005088 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
5089 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
5090 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005091 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
5092 ret = ibm_init(&ibms_init[i]);
5093 if (ret >= 0 && *ibms_init[i].param)
5094 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03005096 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005097 return ret;
5098 }
5099 }
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03005100 ret = input_register_device(tpacpi_inputdev);
5101 if (ret < 0) {
5102 printk(IBM_ERR "unable to register input device\n");
5103 thinkpad_acpi_module_exit();
5104 return ret;
5105 } else {
5106 tp_features.input_device_registered = 1;
5107 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005108
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03005109 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005110 return 0;
5111}
5112
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03005113static void thinkpad_acpi_module_exit(void)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005114{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005115 struct ibm_struct *ibm, *itmp;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005116
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03005117 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
5118
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005119 list_for_each_entry_safe_reverse(ibm, itmp,
5120 &tpacpi_all_drivers,
5121 all_drivers) {
5122 ibm_exit(ibm);
5123 }
5124
5125 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005126
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03005127 if (tpacpi_inputdev) {
5128 if (tp_features.input_device_registered)
5129 input_unregister_device(tpacpi_inputdev);
5130 else
5131 input_free_device(tpacpi_inputdev);
5132 }
5133
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03005134 if (tpacpi_hwmon)
5135 hwmon_device_unregister(tpacpi_hwmon);
5136
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005137 if (tp_features.sensors_pdev_attrs_registered)
5138 device_remove_file(&tpacpi_sensors_pdev->dev,
5139 &dev_attr_thinkpad_acpi_pdev_name);
5140 if (tpacpi_sensors_pdev)
5141 platform_device_unregister(tpacpi_sensors_pdev);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03005142 if (tpacpi_pdev)
5143 platform_device_unregister(tpacpi_pdev);
5144
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03005145 if (tp_features.sensors_pdrv_attrs_registered)
5146 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03005147 if (tp_features.platform_drv_attrs_registered)
5148 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
5149
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005150 if (tp_features.sensors_pdrv_registered)
5151 platform_driver_unregister(&tpacpi_hwmon_pdriver);
5152
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03005153 if (tp_features.platform_drv_registered)
5154 platform_driver_unregister(&tpacpi_pdriver);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03005155
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005156 if (proc_dir)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005157 remove_proc_entry(IBM_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005158
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005159 kfree(thinkpad_id.bios_version_str);
5160 kfree(thinkpad_id.ec_version_str);
5161 kfree(thinkpad_id.model_str);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005162}
5163
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03005164module_init(thinkpad_acpi_module_init);
5165module_exit(thinkpad_acpi_module_exit);