blob: bb8956d0c1047fce4eb35f2323072b8438e59e40 [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 Holschuhc78d5c92007-07-18 23:45:47 -030024#define IBM_VERSION "0.15"
Henrique de Moraes Holschuh741553c2007-07-18 23:45:39 -030025#define TPACPI_SYSFS_VERSION 0x010000
Borislav Deianov78f81cc2005-08-17 00:00:00 -040026
27/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 * Changelog:
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -030029 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
30 * drivers/misc.
Henrique de Moraes Holschuhf9ff43a2006-11-25 16:37:38 -020031 *
32 * 2006-11-22 0.13 new maintainer
33 * changelog now lives in git commit history, and will
34 * not be updated further in-file.
Henrique de Moraes Holschuh837ca6d2007-03-23 17:33:54 -030035 *
Borislav Deianov78f81cc2005-08-17 00:00:00 -040036 * 2005-08-17 0.12 fix compilation on 2.6.13-rc kernels
37 * 2005-03-17 0.11 support for 600e, 770x
38 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
39 * support for 770e, G41
40 * G40 and G41 don't have a thinklight
41 * temperatures no longer experimental
42 * experimental brightness control
43 * experimental volume control
44 * experimental fan enable/disable
Henrique de Moraes Holschuh837ca6d2007-03-23 17:33:54 -030045 * 2005-01-16 0.10 fix module loading on R30, R31
Borislav Deianov78f81cc2005-08-17 00:00:00 -040046 * 2005-01-16 0.9 support for 570, R30, R31
47 * ultrabay support on A22p, A3x
48 * limit arg for cmos, led, beep, drop experimental status
49 * more capable led control on A21e, A22p, T20-22, X20
50 * experimental temperatures and fan speed
51 * experimental embedded controller register dump
52 * mark more functions as __init, drop incorrect __exit
53 * use MODULE_VERSION
54 * thanks to Henrik Brix Andersen <brix@gentoo.org>
55 * fix parameter passing on module loading
56 * thanks to Rusty Russell <rusty@rustcorp.com.au>
57 * thanks to Jim Radford <radford@blackbean.org>
58 * 2004-11-08 0.8 fix init error case, don't return from a macro
59 * thanks to Chris Wright <chrisw@osdl.org>
60 * 2004-10-23 0.7 fix module loading on A21e, A22p, T20, T21, X20
61 * fix led control on A21e
62 * 2004-10-19 0.6 use acpi_bus_register_driver() to claim HKEY device
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 * 2004-10-18 0.5 thinklight support on A21e, G40, R32, T20, T21, X20
64 * proc file format changed
65 * video_switch command
66 * experimental cmos control
67 * experimental led control
68 * experimental acpi sounds
Borislav Deianov78f81cc2005-08-17 00:00:00 -040069 * 2004-09-16 0.4 support for module parameters
70 * hotkey mask can be prefixed by 0x
71 * video output switching
72 * video expansion control
73 * ultrabay eject support
74 * removed lcd brightness/on/off control, didn't work
75 * 2004-08-17 0.3 support for R40
76 * lcd off, brightness control
77 * thinklight on/off
78 * 2004-08-14 0.2 support for T series, X20
79 * bluetooth enable/disable
80 * hotkey events disabled by default
81 * removed fan control, currently useless
82 * 2004-08-09 0.1 initial release, support for X series
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 */
84
Henrique de Moraes Holschuhf21f85d2007-03-29 01:58:40 -030085#include "thinkpad_acpi.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Henrique de Moraes Holschuhf9ff43a2006-11-25 16:37:38 -020087MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
Borislav Deianov78f81cc2005-08-17 00:00:00 -040088MODULE_DESCRIPTION(IBM_DESC);
89MODULE_VERSION(IBM_VERSION);
90MODULE_LICENSE("GPL");
91
Henrique de Moraes Holschuhd903ac52007-03-29 01:58:42 -030092/* Please remove this in year 2009 */
93MODULE_ALIAS("ibm_acpi");
94
Henrique de Moraes Holschuhb964b432007-07-18 23:45:26 -030095/*
96 * DMI matching for module autoloading
97 *
98 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
99 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
100 *
101 * Only models listed in thinkwiki will be supported, so add yours
102 * if it is not there yet.
103 */
104#define IBM_BIOS_MODULE_ALIAS(__type) \
105 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW")
106
107/* Non-ancient thinkpads */
108MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
109MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
110
111/* Ancient thinkpad BIOSes have to be identified by
112 * BIOS type or model number, and there are far less
113 * BIOS types than model numbers... */
114IBM_BIOS_MODULE_ALIAS("I[B,D,H,I,M,N,O,T,W,V,Y,Z]");
115IBM_BIOS_MODULE_ALIAS("1[0,3,6,8,A-G,I,K,M-P,S,T]");
116IBM_BIOS_MODULE_ALIAS("K[U,X-Z]");
117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118#define __unused __attribute__ ((unused))
119
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300120/****************************************************************************
121 ****************************************************************************
122 *
123 * ACPI Helpers and device model
124 *
125 ****************************************************************************
126 ****************************************************************************/
127
128/*************************************************************************
129 * ACPI basic handles
130 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -0300132static acpi_handle root_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134#define IBM_HANDLE(object, parent, paths...) \
135 static acpi_handle object##_handle; \
136 static acpi_handle *object##_parent = &parent##_handle; \
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400137 static char *object##_path; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 static char *object##_paths[] = { paths }
139
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400140IBM_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
141 "\\_SB.PCI.ISA.EC", /* 570 */
142 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
143 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
144 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
145 "\\_SB.PCI0.ICH3.EC0", /* R31 */
146 "\\_SB.PCI0.LPC.EC", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300147 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300149IBM_HANDLE(ecrd, ec, "ECRD"); /* 570 */
150IBM_HANDLE(ecwr, ec, "ECWR"); /* 570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300152
153/*************************************************************************
154 * Misc ACPI handles
155 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400157IBM_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, T4x, X31, X40 */
158 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
159 "\\CMS", /* R40, R40e */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300160 ); /* all others */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400161
162IBM_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
163 "^HKEY", /* R30, R31 */
164 "HKEY", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300165 ); /* 570 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400166
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400167
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300168/*************************************************************************
169 * ACPI helpers
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200170 */
171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172static int acpi_evalf(acpi_handle handle,
173 void *res, char *method, char *fmt, ...)
174{
175 char *fmt0 = fmt;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400176 struct acpi_object_list params;
177 union acpi_object in_objs[IBM_MAX_ACPI_ARGS];
178 struct acpi_buffer result, *resultp;
179 union acpi_object out_obj;
180 acpi_status status;
181 va_list ap;
182 char res_type;
183 int success;
184 int quiet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
186 if (!*fmt) {
187 printk(IBM_ERR "acpi_evalf() called with empty format\n");
188 return 0;
189 }
190
191 if (*fmt == 'q') {
192 quiet = 1;
193 fmt++;
194 } else
195 quiet = 0;
196
197 res_type = *(fmt++);
198
199 params.count = 0;
200 params.pointer = &in_objs[0];
201
202 va_start(ap, fmt);
203 while (*fmt) {
204 char c = *(fmt++);
205 switch (c) {
206 case 'd': /* int */
207 in_objs[params.count].integer.value = va_arg(ap, int);
208 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
209 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400210 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 default:
212 printk(IBM_ERR "acpi_evalf() called "
213 "with invalid format character '%c'\n", c);
214 return 0;
215 }
216 }
217 va_end(ap);
218
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400219 if (res_type != 'v') {
220 result.length = sizeof(out_obj);
221 result.pointer = &out_obj;
222 resultp = &result;
223 } else
224 resultp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400226 status = acpi_evaluate_object(handle, method, &params, resultp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228 switch (res_type) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400229 case 'd': /* int */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 if (res)
231 *(int *)res = out_obj.integer.value;
232 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
233 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400234 case 'v': /* void */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 success = status == AE_OK;
236 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400237 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 default:
239 printk(IBM_ERR "acpi_evalf() called "
240 "with invalid format character '%c'\n", res_type);
241 return 0;
242 }
243
244 if (!success && !quiet)
245 printk(IBM_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
246 method, fmt0, status);
247
248 return success;
249}
250
251static void __unused acpi_print_int(acpi_handle handle, char *method)
252{
253 int i;
254
255 if (acpi_evalf(handle, &i, method, "d"))
256 printk(IBM_INFO "%s = 0x%x\n", method, i);
257 else
258 printk(IBM_ERR "error calling %s\n", method);
259}
260
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300261static int acpi_ec_read(int i, u8 * p)
262{
263 int v;
264
265 if (ecrd_handle) {
266 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
267 return 0;
268 *p = v;
269 } else {
270 if (ec_read(i, p) < 0)
271 return 0;
272 }
273
274 return 1;
275}
276
277static int acpi_ec_write(int i, u8 v)
278{
279 if (ecwr_handle) {
280 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
281 return 0;
282 } else {
283 if (ec_write(i, v) < 0)
284 return 0;
285 }
286
287 return 1;
288}
289
290static int _sta(acpi_handle handle)
291{
292 int status;
293
294 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
295 status = 0;
296
297 return status;
298}
299
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -0300300static int issue_thinkpad_cmos_command(int cmos_cmd)
301{
302 if (!cmos_handle)
303 return -ENXIO;
304
305 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
306 return -EIO;
307
308 return 0;
309}
310
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300311/*************************************************************************
312 * ACPI device model
313 */
314
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300315static void drv_acpi_handle_init(char *name,
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300316 acpi_handle *handle, acpi_handle parent,
317 char **paths, int num_paths, char **path)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300318{
319 int i;
320 acpi_status status;
321
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300322 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
323 name);
324
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300325 for (i = 0; i < num_paths; i++) {
326 status = acpi_get_handle(parent, paths[i], handle);
327 if (ACPI_SUCCESS(status)) {
328 *path = paths[i];
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300329 dbg_printk(TPACPI_DBG_INIT,
330 "Found ACPI handle %s for %s\n",
331 *path, name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300332 return;
333 }
334 }
335
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300336 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
337 name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300338 *handle = NULL;
339}
340
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300341static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300342{
343 struct ibm_struct *ibm = data;
344
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300345 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300346 return;
347
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300348 ibm->acpi->notify(ibm, event);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300349}
350
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300351static int __init setup_acpi_notify(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300352{
353 acpi_status status;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300354 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300355
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300356 BUG_ON(!ibm->acpi);
357
358 if (!*ibm->acpi->handle)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300359 return 0;
360
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300361 vdbg_printk(TPACPI_DBG_INIT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300362 "setting up ACPI notify for %s\n", ibm->name);
363
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300364 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
365 if (rc < 0) {
366 printk(IBM_ERR "acpi_bus_get_device(%s) failed: %d\n",
367 ibm->name, rc);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300368 return -ENODEV;
369 }
370
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300371 acpi_driver_data(ibm->acpi->device) = ibm;
372 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300373 IBM_ACPI_EVENT_PREFIX,
374 ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300375
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300376 status = acpi_install_notify_handler(*ibm->acpi->handle,
377 ibm->acpi->type, dispatch_acpi_notify, ibm);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300378 if (ACPI_FAILURE(status)) {
379 if (status == AE_ALREADY_EXISTS) {
380 printk(IBM_NOTICE "another device driver is already handling %s events\n",
381 ibm->name);
382 } else {
383 printk(IBM_ERR "acpi_install_notify_handler(%s) failed: %d\n",
384 ibm->name, status);
385 }
386 return -ENODEV;
387 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300388 ibm->flags.acpi_notify_installed = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300389 return 0;
390}
391
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300392static int __init tpacpi_device_add(struct acpi_device *device)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300393{
394 return 0;
395}
396
Henrique de Moraes Holschuh67001212007-04-21 11:08:25 -0300397static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300398{
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300399 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300400
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300401 dbg_printk(TPACPI_DBG_INIT,
402 "registering %s as an ACPI driver\n", ibm->name);
403
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300404 BUG_ON(!ibm->acpi);
405
406 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
407 if (!ibm->acpi->driver) {
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300408 printk(IBM_ERR "kzalloc(ibm->driver) failed\n");
409 return -ENOMEM;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300410 }
411
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300412 sprintf(ibm->acpi->driver->name, "%s_%s", IBM_NAME, ibm->name);
413 ibm->acpi->driver->ids = ibm->acpi->hid;
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200414
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300415 ibm->acpi->driver->ops.add = &tpacpi_device_add;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300416
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300417 rc = acpi_bus_register_driver(ibm->acpi->driver);
418 if (rc < 0) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300419 printk(IBM_ERR "acpi_bus_register_driver(%s) failed: %d\n",
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200420 ibm->name, rc);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300421 kfree(ibm->acpi->driver);
422 ibm->acpi->driver = NULL;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300423 } else if (!rc)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300424 ibm->flags.acpi_driver_registered = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300425
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300426 return rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300427}
428
429
430/****************************************************************************
431 ****************************************************************************
432 *
433 * Procfs Helpers
434 *
435 ****************************************************************************
436 ****************************************************************************/
437
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300438static int dispatch_procfs_read(char *page, char **start, off_t off,
439 int count, int *eof, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300440{
441 struct ibm_struct *ibm = data;
442 int len;
443
444 if (!ibm || !ibm->read)
445 return -EINVAL;
446
447 len = ibm->read(page);
448 if (len < 0)
449 return len;
450
451 if (len <= off + count)
452 *eof = 1;
453 *start = page + off;
454 len -= off;
455 if (len > count)
456 len = count;
457 if (len < 0)
458 len = 0;
459
460 return len;
461}
462
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300463static int dispatch_procfs_write(struct file *file,
464 const char __user * userbuf,
465 unsigned long count, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300466{
467 struct ibm_struct *ibm = data;
468 char *kernbuf;
469 int ret;
470
471 if (!ibm || !ibm->write)
472 return -EINVAL;
473
474 kernbuf = kmalloc(count + 2, GFP_KERNEL);
475 if (!kernbuf)
476 return -ENOMEM;
477
478 if (copy_from_user(kernbuf, userbuf, count)) {
479 kfree(kernbuf);
480 return -EFAULT;
481 }
482
483 kernbuf[count] = 0;
484 strcat(kernbuf, ",");
485 ret = ibm->write(kernbuf);
486 if (ret == 0)
487 ret = count;
488
489 kfree(kernbuf);
490
491 return ret;
492}
493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494static char *next_cmd(char **cmds)
495{
496 char *start = *cmds;
497 char *end;
498
499 while ((end = strchr(start, ',')) && end == start)
500 start = end + 1;
501
502 if (!end)
503 return NULL;
504
505 *end = 0;
506 *cmds = end + 1;
507 return start;
508}
509
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300510
511/****************************************************************************
512 ****************************************************************************
513 *
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300514 * Device model: input, hwmon and platform
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300515 *
516 ****************************************************************************
517 ****************************************************************************/
518
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -0300519static struct platform_device *tpacpi_pdev;
520static struct class_device *tpacpi_hwmon;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300521static struct input_dev *tpacpi_inputdev;
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300522
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300523
524static int tpacpi_resume_handler(struct platform_device *pdev)
525{
526 struct ibm_struct *ibm, *itmp;
527
528 list_for_each_entry_safe(ibm, itmp,
529 &tpacpi_all_drivers,
530 all_drivers) {
531 if (ibm->resume)
532 (ibm->resume)();
533 }
534
535 return 0;
536}
537
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300538static struct platform_driver tpacpi_pdriver = {
539 .driver = {
540 .name = IBM_DRVR_NAME,
541 .owner = THIS_MODULE,
542 },
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300543 .resume = tpacpi_resume_handler,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300544};
545
546
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -0300547/*************************************************************************
548 * thinkpad-acpi driver attributes
549 */
550
551/* interface_version --------------------------------------------------- */
552static ssize_t tpacpi_driver_interface_version_show(
553 struct device_driver *drv,
554 char *buf)
555{
556 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
557}
558
559static DRIVER_ATTR(interface_version, S_IRUGO,
560 tpacpi_driver_interface_version_show, NULL);
561
562/* debug_level --------------------------------------------------------- */
563static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
564 char *buf)
565{
566 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
567}
568
569static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
570 const char *buf, size_t count)
571{
572 unsigned long t;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -0300573
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300574 if (parse_strtoul(buf, 0xffff, &t))
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -0300575 return -EINVAL;
576
577 dbg_level = t;
578
579 return count;
580}
581
582static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
583 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
584
585/* version ------------------------------------------------------------- */
586static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
587 char *buf)
588{
589 return snprintf(buf, PAGE_SIZE, "%s v%s\n", IBM_DESC, IBM_VERSION);
590}
591
592static DRIVER_ATTR(version, S_IRUGO,
593 tpacpi_driver_version_show, NULL);
594
595/* --------------------------------------------------------------------- */
596
597static struct driver_attribute* tpacpi_driver_attributes[] = {
598 &driver_attr_debug_level, &driver_attr_version,
599 &driver_attr_interface_version,
600};
601
602static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
603{
604 int i, res;
605
606 i = 0;
607 res = 0;
608 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
609 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
610 i++;
611 }
612
613 return res;
614}
615
616static void tpacpi_remove_driver_attributes(struct device_driver *drv)
617{
618 int i;
619
620 for(i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
621 driver_remove_file(drv, tpacpi_driver_attributes[i]);
622}
623
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300624/*************************************************************************
625 * sysfs support helpers
626 */
627
628struct attribute_set_obj {
629 struct attribute_set s;
630 struct attribute *a;
631} __attribute__((packed));
632
633static struct attribute_set *create_attr_set(unsigned int max_members,
634 const char* name)
635{
636 struct attribute_set_obj *sobj;
637
638 if (max_members == 0)
639 return NULL;
640
641 /* Allocates space for implicit NULL at the end too */
642 sobj = kzalloc(sizeof(struct attribute_set_obj) +
643 max_members * sizeof(struct attribute *),
644 GFP_KERNEL);
645 if (!sobj)
646 return NULL;
647 sobj->s.max_members = max_members;
648 sobj->s.group.attrs = &sobj->a;
649 sobj->s.group.name = name;
650
651 return &sobj->s;
652}
653
654/* not multi-threaded safe, use it in a single thread per set */
655static int add_to_attr_set(struct attribute_set* s, struct attribute *attr)
656{
657 if (!s || !attr)
658 return -EINVAL;
659
660 if (s->members >= s->max_members)
661 return -ENOMEM;
662
663 s->group.attrs[s->members] = attr;
664 s->members++;
665
666 return 0;
667}
668
669static int add_many_to_attr_set(struct attribute_set* s,
670 struct attribute **attr,
671 unsigned int count)
672{
673 int i, res;
674
675 for (i = 0; i < count; i++) {
676 res = add_to_attr_set(s, attr[i]);
677 if (res)
678 return res;
679 }
680
681 return 0;
682}
683
684static void delete_attr_set(struct attribute_set* s, struct kobject *kobj)
685{
686 sysfs_remove_group(kobj, &s->group);
687 destroy_attr_set(s);
688}
689
690static int parse_strtoul(const char *buf,
691 unsigned long max, unsigned long *value)
692{
693 char *endp;
694
695 *value = simple_strtoul(buf, &endp, 0);
696 while (*endp && isspace(*endp))
697 endp++;
698 if (*endp || *value > max)
699 return -EINVAL;
700
701 return 0;
702}
703
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300704/****************************************************************************
705 ****************************************************************************
706 *
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300707 * Subdrivers
708 *
709 ****************************************************************************
710 ****************************************************************************/
711
712/*************************************************************************
Henrique de Moraes Holschuh142cfc92007-04-21 11:08:26 -0300713 * thinkpad-acpi init subdriver
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300714 */
715
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300716static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717{
718 printk(IBM_INFO "%s v%s\n", IBM_DESC, IBM_VERSION);
719 printk(IBM_INFO "%s\n", IBM_URL);
720
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -0300721 printk(IBM_INFO "ThinkPad BIOS %s, EC %s\n",
722 (thinkpad_id.bios_version_str) ?
723 thinkpad_id.bios_version_str : "unknown",
724 (thinkpad_id.ec_version_str) ?
725 thinkpad_id.ec_version_str : "unknown");
726
727 if (thinkpad_id.vendor && thinkpad_id.model_str)
728 printk(IBM_INFO "%s %s\n",
729 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
730 "IBM" : ((thinkpad_id.vendor ==
731 PCI_VENDOR_ID_LENOVO) ?
732 "Lenovo" : "Unknown vendor"),
733 thinkpad_id.model_str);
Henrique de Moraes Holschuh3945ac32007-02-06 19:13:44 -0200734
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 return 0;
736}
737
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300738static int thinkpad_acpi_driver_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739{
740 int len = 0;
741
742 len += sprintf(p + len, "driver:\t\t%s\n", IBM_DESC);
743 len += sprintf(p + len, "version:\t%s\n", IBM_VERSION);
744
745 return len;
746}
747
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300748static struct ibm_struct thinkpad_acpi_driver_data = {
749 .name = "driver",
750 .read = thinkpad_acpi_driver_read,
751};
752
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300753/*************************************************************************
754 * Hotkey subdriver
755 */
756
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400757static int hotkey_orig_status;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -0300758static u32 hotkey_orig_mask;
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -0300759static u32 hotkey_all_mask;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -0300760static u32 hotkey_reserved_mask;
761
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -0300762static u16 *hotkey_keycode_map;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400763
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -0300764static struct attribute_set *hotkey_dev_attributes;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300765
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -0300766static int hotkey_get_wlsw(int *status)
767{
768 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
769 return -EIO;
770 return 0;
771}
772
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300773/* sysfs hotkey enable ------------------------------------------------- */
774static ssize_t hotkey_enable_show(struct device *dev,
775 struct device_attribute *attr,
776 char *buf)
777{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -0300778 int res, status;
779 u32 mask;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300780
781 res = hotkey_get(&status, &mask);
782 if (res)
783 return res;
784
785 return snprintf(buf, PAGE_SIZE, "%d\n", status);
786}
787
788static ssize_t hotkey_enable_store(struct device *dev,
789 struct device_attribute *attr,
790 const char *buf, size_t count)
791{
792 unsigned long t;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -0300793 int res, status;
794 u32 mask;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300795
796 if (parse_strtoul(buf, 1, &t))
797 return -EINVAL;
798
799 res = hotkey_get(&status, &mask);
800 if (!res)
801 res = hotkey_set(t, mask);
802
803 return (res) ? res : count;
804}
805
806static struct device_attribute dev_attr_hotkey_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -0300807 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300808 hotkey_enable_show, hotkey_enable_store);
809
810/* sysfs hotkey mask --------------------------------------------------- */
811static ssize_t hotkey_mask_show(struct device *dev,
812 struct device_attribute *attr,
813 char *buf)
814{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -0300815 int res, status;
816 u32 mask;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300817
818 res = hotkey_get(&status, &mask);
819 if (res)
820 return res;
821
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -0300822 return snprintf(buf, PAGE_SIZE, "0x%08x\n", mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300823}
824
825static ssize_t hotkey_mask_store(struct device *dev,
826 struct device_attribute *attr,
827 const char *buf, size_t count)
828{
829 unsigned long t;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -0300830 int res, status;
831 u32 mask;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300832
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -0300833 if (parse_strtoul(buf, 0xffffffffUL, &t))
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300834 return -EINVAL;
835
836 res = hotkey_get(&status, &mask);
837 if (!res)
838 hotkey_set(status, t);
839
840 return (res) ? res : count;
841}
842
843static struct device_attribute dev_attr_hotkey_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -0300844 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300845 hotkey_mask_show, hotkey_mask_store);
846
847/* sysfs hotkey bios_enabled ------------------------------------------- */
848static ssize_t hotkey_bios_enabled_show(struct device *dev,
849 struct device_attribute *attr,
850 char *buf)
851{
852 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
853}
854
855static struct device_attribute dev_attr_hotkey_bios_enabled =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -0300856 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300857
858/* sysfs hotkey bios_mask ---------------------------------------------- */
859static ssize_t hotkey_bios_mask_show(struct device *dev,
860 struct device_attribute *attr,
861 char *buf)
862{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -0300863 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300864}
865
866static struct device_attribute dev_attr_hotkey_bios_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -0300867 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300868
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -0300869/* sysfs hotkey all_mask ----------------------------------------------- */
870static ssize_t hotkey_all_mask_show(struct device *dev,
871 struct device_attribute *attr,
872 char *buf)
873{
874 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_all_mask);
875}
876
877static struct device_attribute dev_attr_hotkey_all_mask =
878 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
879
880/* sysfs hotkey recommended_mask --------------------------------------- */
881static ssize_t hotkey_recommended_mask_show(struct device *dev,
882 struct device_attribute *attr,
883 char *buf)
884{
885 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
886 hotkey_all_mask & ~hotkey_reserved_mask);
887}
888
889static struct device_attribute dev_attr_hotkey_recommended_mask =
890 __ATTR(hotkey_recommended_mask, S_IRUGO,
891 hotkey_recommended_mask_show, NULL);
892
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -0300893/* sysfs hotkey radio_sw ----------------------------------------------- */
894static ssize_t hotkey_radio_sw_show(struct device *dev,
895 struct device_attribute *attr,
896 char *buf)
897{
898 int res, s;
899 res = hotkey_get_wlsw(&s);
900 if (res < 0)
901 return res;
902
903 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
904}
905
906static struct device_attribute dev_attr_hotkey_radio_sw =
907 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
908
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300909/* --------------------------------------------------------------------- */
910
911static struct attribute *hotkey_mask_attributes[] = {
912 &dev_attr_hotkey_mask.attr,
913 &dev_attr_hotkey_bios_enabled.attr,
914 &dev_attr_hotkey_bios_mask.attr,
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -0300915 &dev_attr_hotkey_all_mask.attr,
916 &dev_attr_hotkey_recommended_mask.attr,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300917};
918
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300919static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300920{
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -0300921
922 static u16 ibm_keycode_map[] __initdata = {
923 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
924 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
925 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
926 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
927 /* Scan codes 0x0C to 0x0F: Other ACPI HKEY hot keys */
928 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
929 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
930 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
931 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
932 /* Scan codes 0x10 to 0x1F: Extended ACPI HKEY hot keys */
933 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
934 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
935 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
936 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
937 KEY_RESERVED, /* 0x14: VOLUME UP */
938 KEY_RESERVED, /* 0x15: VOLUME DOWN */
939 KEY_RESERVED, /* 0x16: MUTE */
940 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
941 /* (assignments unknown, please report if found) */
942 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
943 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
944 };
945 static u16 lenovo_keycode_map[] __initdata = {
946 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
947 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
948 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
949 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
950 /* Scan codes 0x0C to 0x0F: Other ACPI HKEY hot keys */
951 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
952 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
953 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
954 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
955 /* Scan codes 0x10 to 0x1F: Extended ACPI HKEY hot keys */
956 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
957 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
958 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
959 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
960 KEY_RESERVED, /* 0x14: VOLUME UP */
961 KEY_RESERVED, /* 0x15: VOLUME DOWN */
962 KEY_RESERVED, /* 0x16: MUTE */
963 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
964 /* (assignments unknown, please report if found) */
965 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
966 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
967 };
968
969#define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
970#define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
971#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
972
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -0300973 int res, i;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -0300974 int status;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300975
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300976 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
977
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -0300978 BUG_ON(!tpacpi_inputdev);
979
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300980 IBM_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -0300981 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300982
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300983 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300984 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300985
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300986 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300987 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300988
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300989 if (tp_features.hotkey) {
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -0300990 hotkey_dev_attributes = create_attr_set(7, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300991 if (!hotkey_dev_attributes)
992 return -ENOMEM;
993 res = add_to_attr_set(hotkey_dev_attributes,
994 &dev_attr_hotkey_enable.attr);
995 if (res)
996 return res;
997
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300998 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
999 A30, R30, R31, T20-22, X20-21, X22-24 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001000 tp_features.hotkey_mask =
1001 acpi_evalf(hkey_handle, NULL, "DHKN", "qv");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001002
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001003 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001004 str_supported(tp_features.hotkey_mask));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001005
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001006 if (tp_features.hotkey_mask) {
1007 /* MHKA available in A31, R40, R40e, T4x, X31, and later */
1008 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
1009 "MHKA", "qd"))
1010 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
1011 }
1012
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001013 res = hotkey_get(&hotkey_orig_status, &hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001014 if (!res && tp_features.hotkey_mask) {
1015 res = add_many_to_attr_set(hotkey_dev_attributes,
1016 hotkey_mask_attributes,
1017 ARRAY_SIZE(hotkey_mask_attributes));
1018 }
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001019
1020 /* Not all thinkpads have a hardware radio switch */
1021 if (!res && acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
1022 tp_features.hotkey_wlsw = 1;
1023 printk(IBM_INFO
1024 "radio switch found; radios are %s\n",
1025 enabled(status, 0));
1026 res = add_to_attr_set(hotkey_dev_attributes,
1027 &dev_attr_hotkey_radio_sw.attr);
1028 }
1029
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001030 if (!res)
1031 res = register_attr_set_with_sysfs(
1032 hotkey_dev_attributes,
1033 &tpacpi_pdev->dev.kobj);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001034 if (res)
1035 return res;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001036
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001037 /* Set up key map */
1038
1039 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
1040 GFP_KERNEL);
1041 if (!hotkey_keycode_map) {
1042 printk(IBM_ERR "failed to allocate memory for key map\n");
1043 return -ENOMEM;
1044 }
1045
1046 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
1047 dbg_printk(TPACPI_DBG_INIT,
1048 "using Lenovo default hot key map\n");
1049 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
1050 TPACPI_HOTKEY_MAP_SIZE);
1051 } else {
1052 dbg_printk(TPACPI_DBG_INIT,
1053 "using IBM default hot key map\n");
1054 memcpy(hotkey_keycode_map, &ibm_keycode_map,
1055 TPACPI_HOTKEY_MAP_SIZE);
1056 }
1057
Henrique de Moraes Holschuh1a343762007-07-18 23:45:36 -03001058#ifndef CONFIG_THINKPAD_ACPI_INPUT_ENABLED
1059 for (i = 0; i < 12; i++)
1060 hotkey_keycode_map[i] = KEY_UNKNOWN;
1061#endif /* ! CONFIG_THINKPAD_ACPI_INPUT_ENABLED */
1062
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001063 set_bit(EV_KEY, tpacpi_inputdev->evbit);
1064 set_bit(EV_MSC, tpacpi_inputdev->evbit);
1065 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001066 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
1067 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
1068 tpacpi_inputdev->keycode = hotkey_keycode_map;
1069 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001070 if (hotkey_keycode_map[i] != KEY_RESERVED) {
1071 set_bit(hotkey_keycode_map[i],
1072 tpacpi_inputdev->keybit);
1073 } else {
1074 if (i < sizeof(hotkey_reserved_mask)*8)
1075 hotkey_reserved_mask |= 1 << i;
1076 }
1077 }
1078
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03001079 if (tp_features.hotkey_wlsw) {
1080 set_bit(EV_SW, tpacpi_inputdev->evbit);
1081 set_bit(SW_RADIO, tpacpi_inputdev->swbit);
1082 }
1083
Henrique de Moraes Holschuh1a343762007-07-18 23:45:36 -03001084#ifdef CONFIG_THINKPAD_ACPI_INPUT_ENABLED
1085 dbg_printk(TPACPI_DBG_INIT,
1086 "enabling hot key handling\n");
1087 res = hotkey_set(1, (hotkey_all_mask & ~hotkey_reserved_mask)
1088 | hotkey_orig_mask);
1089 if (res)
1090 return res;
1091#endif /* CONFIG_THINKPAD_ACPI_INPUT_ENABLED */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001092 }
1093
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001094 return (tp_features.hotkey)? 0 : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001095}
1096
1097static void hotkey_exit(void)
1098{
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001099 int res;
1100
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001101 if (tp_features.hotkey) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001102 dbg_printk(TPACPI_DBG_EXIT, "restoring original hotkey mask\n");
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001103 res = hotkey_set(hotkey_orig_status, hotkey_orig_mask);
1104 if (res)
1105 printk(IBM_ERR "failed to restore hotkey to BIOS defaults\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001106 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001107
1108 if (hotkey_dev_attributes) {
1109 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
1110 hotkey_dev_attributes = NULL;
1111 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001112}
1113
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001114static void tpacpi_input_send_key(unsigned int scancode,
1115 unsigned int keycode)
1116{
1117 if (keycode != KEY_RESERVED) {
1118 input_report_key(tpacpi_inputdev, keycode, 1);
1119 if (keycode == KEY_UNKNOWN)
1120 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1121 scancode);
1122 input_sync(tpacpi_inputdev);
1123
1124 input_report_key(tpacpi_inputdev, keycode, 0);
1125 if (keycode == KEY_UNKNOWN)
1126 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1127 scancode);
1128 input_sync(tpacpi_inputdev);
1129 }
1130}
1131
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03001132static void tpacpi_input_send_radiosw(void)
1133{
1134 int wlsw;
1135
1136 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw))
1137 input_report_switch(tpacpi_inputdev,
1138 SW_RADIO, !!wlsw);
1139}
1140
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001141static void hotkey_notify(struct ibm_struct *ibm, u32 event)
1142{
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001143 u32 hkey;
1144 unsigned int keycode, scancode;
1145 int sendacpi = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001146
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001147 if (event == 0x80 && acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001148 if (tpacpi_inputdev->users > 0) {
1149 switch (hkey >> 12) {
1150 case 1:
1151 /* 0x1000-0x1FFF: key presses */
1152 scancode = hkey & 0xfff;
1153 if (scancode > 0 && scancode < 0x21) {
1154 scancode--;
1155 keycode = hotkey_keycode_map[scancode];
1156 tpacpi_input_send_key(scancode, keycode);
1157 sendacpi = (keycode == KEY_RESERVED
1158 || keycode == KEY_UNKNOWN);
1159 } else {
1160 printk(IBM_ERR
1161 "hotkey 0x%04x out of range for keyboard map\n",
1162 hkey);
1163 }
1164 break;
1165 case 5:
1166 /* 0x5000-0x5FFF: LID */
1167 /* we don't handle it through this path, just
1168 * eat up known LID events */
1169 if (hkey != 0x5001 && hkey != 0x5002) {
1170 printk(IBM_ERR
1171 "unknown LID-related hotkey event: 0x%04x\n",
1172 hkey);
1173 }
1174 break;
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03001175 case 7:
1176 /* 0x7000-0x7FFF: misc */
1177 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
1178 tpacpi_input_send_radiosw();
1179 sendacpi = 0;
1180 break;
1181 }
1182 /* fallthrough to default */
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001183 default:
1184 /* case 2: dock-related */
1185 /* 0x2305 - T43 waking up due to bay lever eject while aslept */
1186 /* case 3: ultra-bay related. maybe bay in dock? */
1187 /* 0x3003 - T43 after wake up by bay lever eject (0x2305) */
1188 printk(IBM_NOTICE "unhandled hotkey event 0x%04x\n", hkey);
1189 }
1190 }
1191
1192 if (sendacpi)
Len Brown14e04fb2007-08-23 15:20:26 -04001193 acpi_bus_generate_proc_event(ibm->acpi->device, event, hkey);
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001194 } else {
1195 printk(IBM_ERR "unknown hotkey notification event %d\n", event);
Len Brown14e04fb2007-08-23 15:20:26 -04001196 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001197 }
1198}
1199
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03001200static void hotkey_resume(void)
1201{
1202 tpacpi_input_send_radiosw();
1203}
1204
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001205/*
1206 * Call with hotkey_mutex held
1207 */
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001208static int hotkey_get(int *status, u32 *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209{
1210 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001211 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001212
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001213 if (tp_features.hotkey_mask)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001214 if (!acpi_evalf(hkey_handle, mask, "DHKN", "d"))
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001215 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001216
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001217 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218}
1219
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001220/*
1221 * Call with hotkey_mutex held
1222 */
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001223static int hotkey_set(int status, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224{
1225 int i;
1226
1227 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001228 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001230 if (tp_features.hotkey_mask)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001231 for (i = 0; i < 32; i++) {
1232 int bit = ((1 << i) & mask) != 0;
1233 if (!acpi_evalf(hkey_handle,
1234 NULL, "MHKM", "vdd", i + 1, bit))
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001235 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001236 }
1237
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001238 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001239}
1240
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001241/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001242static int hotkey_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001244 int res, status;
1245 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 int len = 0;
1247
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001248 if (!tp_features.hotkey) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001249 len += sprintf(p + len, "status:\t\tnot supported\n");
1250 return len;
1251 }
1252
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001253 res = mutex_lock_interruptible(&hotkey_mutex);
1254 if (res < 0)
1255 return res;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001256 res = hotkey_get(&status, &mask);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001257 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001258 if (res)
1259 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
1261 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001262 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001263 len += sprintf(p + len, "mask:\t\t0x%08x\n", mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 len += sprintf(p + len,
1265 "commands:\tenable, disable, reset, <mask>\n");
1266 } else {
1267 len += sprintf(p + len, "mask:\t\tnot supported\n");
1268 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
1269 }
1270
1271 return len;
1272}
1273
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001274static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001276 int res, status;
1277 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 char *cmd;
1279 int do_cmd = 0;
1280
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001281 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 return -ENODEV;
1283
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001284 res = mutex_lock_interruptible(&hotkey_mutex);
1285 if (res < 0)
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001286 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001287
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001288 res = hotkey_get(&status, &mask);
1289 if (res)
1290 goto errexit;
1291
1292 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 while ((cmd = next_cmd(&buf))) {
1294 if (strlencmp(cmd, "enable") == 0) {
1295 status = 1;
1296 } else if (strlencmp(cmd, "disable") == 0) {
1297 status = 0;
1298 } else if (strlencmp(cmd, "reset") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001299 status = hotkey_orig_status;
1300 mask = hotkey_orig_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
1302 /* mask set */
1303 } else if (sscanf(cmd, "%x", &mask) == 1) {
1304 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001305 } else {
1306 res = -EINVAL;
1307 goto errexit;
1308 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 do_cmd = 1;
1310 }
1311
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001312 if (do_cmd)
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001313 res = hotkey_set(status, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001315errexit:
1316 mutex_unlock(&hotkey_mutex);
1317 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001318}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
Thomas Renninger1ba90e32007-07-23 14:44:41 +02001320static const struct acpi_device_id ibm_htk_device_ids[] = {
1321 {IBM_HKEY_HID, 0},
1322 {"", 0},
1323};
1324
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001325static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +02001326 .hid = ibm_htk_device_ids,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001327 .notify = hotkey_notify,
1328 .handle = &hkey_handle,
1329 .type = ACPI_DEVICE_NOTIFY,
1330};
1331
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001332static struct ibm_struct hotkey_driver_data = {
1333 .name = "hotkey",
1334 .read = hotkey_read,
1335 .write = hotkey_write,
1336 .exit = hotkey_exit,
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03001337 .resume = hotkey_resume,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001338 .acpi = &ibm_hotkey_acpidriver,
1339};
1340
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001341/*************************************************************************
1342 * Bluetooth subdriver
1343 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001345/* sysfs bluetooth enable ---------------------------------------------- */
1346static ssize_t bluetooth_enable_show(struct device *dev,
1347 struct device_attribute *attr,
1348 char *buf)
1349{
1350 int status;
1351
1352 status = bluetooth_get_radiosw();
1353 if (status < 0)
1354 return status;
1355
1356 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
1357}
1358
1359static ssize_t bluetooth_enable_store(struct device *dev,
1360 struct device_attribute *attr,
1361 const char *buf, size_t count)
1362{
1363 unsigned long t;
1364 int res;
1365
1366 if (parse_strtoul(buf, 1, &t))
1367 return -EINVAL;
1368
1369 res = bluetooth_set_radiosw(t);
1370
1371 return (res) ? res : count;
1372}
1373
1374static struct device_attribute dev_attr_bluetooth_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001375 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001376 bluetooth_enable_show, bluetooth_enable_store);
1377
1378/* --------------------------------------------------------------------- */
1379
1380static struct attribute *bluetooth_attributes[] = {
1381 &dev_attr_bluetooth_enable.attr,
1382 NULL
1383};
1384
1385static const struct attribute_group bluetooth_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001386 .attrs = bluetooth_attributes,
1387};
1388
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001389static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001391 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001392 int status = 0;
1393
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001394 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
1395
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001396 IBM_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001397
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001398 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1399 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001400 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001401 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001403 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
1404 str_supported(tp_features.bluetooth),
1405 status);
1406
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001407 if (tp_features.bluetooth) {
1408 if (!(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
1409 /* no bluetooth hardware present in system */
1410 tp_features.bluetooth = 0;
1411 dbg_printk(TPACPI_DBG_INIT,
1412 "bluetooth hardware not installed\n");
1413 } else {
1414 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
1415 &bluetooth_attr_group);
1416 if (res)
1417 return res;
1418 }
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001419 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001420
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001421 return (tp_features.bluetooth)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422}
1423
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001424static void bluetooth_exit(void)
1425{
1426 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
1427 &bluetooth_attr_group);
1428}
1429
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001430static int bluetooth_get_radiosw(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431{
1432 int status;
1433
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001434 if (!tp_features.bluetooth)
1435 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001437 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
1438 return -EIO;
1439
1440 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
1441}
1442
1443static int bluetooth_set_radiosw(int radio_on)
1444{
1445 int status;
1446
1447 if (!tp_features.bluetooth)
1448 return -ENODEV;
1449
1450 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
1451 return -EIO;
1452 if (radio_on)
1453 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
1454 else
1455 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
1456 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
1457 return -EIO;
1458
1459 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460}
1461
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001462/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001463static int bluetooth_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464{
1465 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001466 int status = bluetooth_get_radiosw();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001468 if (!tp_features.bluetooth)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 len += sprintf(p + len, "status:\t\tnot supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001471 len += sprintf(p + len, "status:\t\t%s\n",
1472 (status)? "enabled" : "disabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 len += sprintf(p + len, "commands:\tenable, disable\n");
1474 }
1475
1476 return len;
1477}
1478
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001479static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001483 if (!tp_features.bluetooth)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001484 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
1486 while ((cmd = next_cmd(&buf))) {
1487 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001488 bluetooth_set_radiosw(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001490 bluetooth_set_radiosw(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 } else
1492 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 }
1494
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 return 0;
1496}
1497
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001498static struct ibm_struct bluetooth_driver_data = {
1499 .name = "bluetooth",
1500 .read = bluetooth_read,
1501 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001502 .exit = bluetooth_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001503};
1504
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001505/*************************************************************************
1506 * Wan subdriver
1507 */
1508
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001509/* sysfs wan enable ---------------------------------------------------- */
1510static ssize_t wan_enable_show(struct device *dev,
1511 struct device_attribute *attr,
1512 char *buf)
1513{
1514 int status;
1515
1516 status = wan_get_radiosw();
1517 if (status < 0)
1518 return status;
1519
1520 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
1521}
1522
1523static ssize_t wan_enable_store(struct device *dev,
1524 struct device_attribute *attr,
1525 const char *buf, size_t count)
1526{
1527 unsigned long t;
1528 int res;
1529
1530 if (parse_strtoul(buf, 1, &t))
1531 return -EINVAL;
1532
1533 res = wan_set_radiosw(t);
1534
1535 return (res) ? res : count;
1536}
1537
1538static struct device_attribute dev_attr_wan_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001539 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001540 wan_enable_show, wan_enable_store);
1541
1542/* --------------------------------------------------------------------- */
1543
1544static struct attribute *wan_attributes[] = {
1545 &dev_attr_wan_enable.attr,
1546 NULL
1547};
1548
1549static const struct attribute_group wan_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001550 .attrs = wan_attributes,
1551};
1552
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001553static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001554{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001555 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001556 int status = 0;
1557
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001558 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
1559
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001560 IBM_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001561
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001562 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001563 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001564
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001565 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
1566 str_supported(tp_features.wan),
1567 status);
1568
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001569 if (tp_features.wan) {
1570 if (!(status & TP_ACPI_WANCARD_HWPRESENT)) {
1571 /* no wan hardware present in system */
1572 tp_features.wan = 0;
1573 dbg_printk(TPACPI_DBG_INIT,
1574 "wan hardware not installed\n");
1575 } else {
1576 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
1577 &wan_attr_group);
1578 if (res)
1579 return res;
1580 }
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001581 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001582
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001583 return (tp_features.wan)? 0 : 1;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001584}
1585
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001586static void wan_exit(void)
1587{
1588 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
1589 &wan_attr_group);
1590}
1591
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001592static int wan_get_radiosw(void)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001593{
1594 int status;
1595
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001596 if (!tp_features.wan)
1597 return -ENODEV;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001598
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001599 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
1600 return -EIO;
1601
1602 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
1603}
1604
1605static int wan_set_radiosw(int radio_on)
1606{
1607 int status;
1608
1609 if (!tp_features.wan)
1610 return -ENODEV;
1611
1612 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
1613 return -EIO;
1614 if (radio_on)
1615 status |= TP_ACPI_WANCARD_RADIOSSW;
1616 else
1617 status &= ~TP_ACPI_WANCARD_RADIOSSW;
1618 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
1619 return -EIO;
1620
1621 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001622}
1623
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001624/* procfs -------------------------------------------------------------- */
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001625static int wan_read(char *p)
1626{
1627 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001628 int status = wan_get_radiosw();
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001629
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001630 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001631 len += sprintf(p + len, "status:\t\tnot supported\n");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001632 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001633 len += sprintf(p + len, "status:\t\t%s\n",
1634 (status)? "enabled" : "disabled");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001635 len += sprintf(p + len, "commands:\tenable, disable\n");
1636 }
1637
1638 return len;
1639}
1640
1641static int wan_write(char *buf)
1642{
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001643 char *cmd;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001644
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001645 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001646 return -ENODEV;
1647
1648 while ((cmd = next_cmd(&buf))) {
1649 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001650 wan_set_radiosw(1);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001651 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001652 wan_set_radiosw(0);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001653 } else
1654 return -EINVAL;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001655 }
1656
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001657 return 0;
1658}
1659
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001660static struct ibm_struct wan_driver_data = {
1661 .name = "wan",
1662 .read = wan_read,
1663 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001664 .exit = wan_exit,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03001665 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001666};
1667
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001668/*************************************************************************
1669 * Video subdriver
1670 */
1671
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02001672static enum video_access_mode video_supported;
1673static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001674
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001675IBM_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
1676 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
1677 "\\_SB.PCI0.VID0", /* 770e */
1678 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
1679 "\\_SB.PCI0.AGP.VID", /* all others */
1680 ); /* R30, R31 */
1681
1682IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
1683
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001684static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001686 int ivga;
1687
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001688 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
1689
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001690 IBM_ACPIHANDLE_INIT(vid);
1691 IBM_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001692
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001693 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
1694 /* G41, assume IVGA doesn't change */
1695 vid_handle = vid2_handle;
1696
1697 if (!vid_handle)
1698 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001699 video_supported = TPACPI_VIDEO_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001700 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
1701 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001702 video_supported = TPACPI_VIDEO_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001703 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
1704 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001705 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001706 else
1707 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001708 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001710 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
1711 str_supported(video_supported != TPACPI_VIDEO_NONE),
1712 video_supported);
1713
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001714 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715}
1716
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001717static void video_exit(void)
1718{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001719 dbg_printk(TPACPI_DBG_EXIT,
1720 "restoring original video autoswitch mode\n");
1721 if (video_autosw_set(video_orig_autosw))
1722 printk(IBM_ERR "error while trying to restore original "
1723 "video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001724}
1725
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001726static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727{
1728 int status = 0;
1729 int i;
1730
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001731 switch (video_supported) {
1732 case TPACPI_VIDEO_570:
1733 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
1734 TP_ACPI_VIDEO_570_PHSCMD))
1735 return -EIO;
1736 status = i & TP_ACPI_VIDEO_570_PHSMASK;
1737 break;
1738 case TPACPI_VIDEO_770:
1739 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
1740 return -EIO;
1741 if (i)
1742 status |= TP_ACPI_VIDEO_S_LCD;
1743 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
1744 return -EIO;
1745 if (i)
1746 status |= TP_ACPI_VIDEO_S_CRT;
1747 break;
1748 case TPACPI_VIDEO_NEW:
1749 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
1750 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
1751 return -EIO;
1752 if (i)
1753 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001755 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
1756 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
1757 return -EIO;
1758 if (i)
1759 status |= TP_ACPI_VIDEO_S_LCD;
1760 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
1761 return -EIO;
1762 if (i)
1763 status |= TP_ACPI_VIDEO_S_DVI;
1764 break;
1765 default:
1766 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001767 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768
1769 return status;
1770}
1771
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001772static int video_outputsw_set(int status)
1773{
1774 int autosw;
1775 int res = 0;
1776
1777 switch (video_supported) {
1778 case TPACPI_VIDEO_570:
1779 res = acpi_evalf(NULL, NULL,
1780 "\\_SB.PHS2", "vdd",
1781 TP_ACPI_VIDEO_570_PHS2CMD,
1782 status | TP_ACPI_VIDEO_570_PHS2SET);
1783 break;
1784 case TPACPI_VIDEO_770:
1785 autosw = video_autosw_get();
1786 if (autosw < 0)
1787 return autosw;
1788
1789 res = video_autosw_set(1);
1790 if (res)
1791 return res;
1792 res = acpi_evalf(vid_handle, NULL,
1793 "ASWT", "vdd", status * 0x100, 0);
1794 if (!autosw && video_autosw_set(autosw)) {
1795 printk(IBM_ERR "video auto-switch left enabled due to error\n");
1796 return -EIO;
1797 }
1798 break;
1799 case TPACPI_VIDEO_NEW:
1800 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
1801 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
1802 break;
1803 default:
1804 return -ENOSYS;
1805 }
1806
1807 return (res)? 0 : -EIO;
1808}
1809
1810static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001812 int autosw = 0;
1813
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001814 switch (video_supported) {
1815 case TPACPI_VIDEO_570:
1816 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
1817 return -EIO;
1818 break;
1819 case TPACPI_VIDEO_770:
1820 case TPACPI_VIDEO_NEW:
1821 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
1822 return -EIO;
1823 break;
1824 default:
1825 return -ENOSYS;
1826 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001827
1828 return autosw & 1;
1829}
1830
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001831static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001832{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001833 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001834 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001835 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001836}
1837
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001838static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001839{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001840 int autosw = video_autosw_get();
1841 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001842
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001843 if (autosw < 0)
1844 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001845
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001846 switch (video_supported) {
1847 case TPACPI_VIDEO_570:
1848 res = video_autosw_set(1);
1849 if (res)
1850 return res;
1851 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
1852 break;
1853 case TPACPI_VIDEO_770:
1854 case TPACPI_VIDEO_NEW:
1855 res = video_autosw_set(1);
1856 if (res)
1857 return res;
1858 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
1859 break;
1860 default:
1861 return -ENOSYS;
1862 }
1863 if (!autosw && video_autosw_set(autosw)) {
1864 printk(IBM_ERR "video auto-switch left enabled due to error\n");
1865 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001866 }
1867
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001868 return (res)? 0 : -EIO;
1869}
1870
1871static int video_expand_toggle(void)
1872{
1873 switch (video_supported) {
1874 case TPACPI_VIDEO_570:
1875 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
1876 0 : -EIO;
1877 case TPACPI_VIDEO_770:
1878 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
1879 0 : -EIO;
1880 case TPACPI_VIDEO_NEW:
1881 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
1882 0 : -EIO;
1883 default:
1884 return -ENOSYS;
1885 }
1886 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001887}
1888
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001889static int video_read(char *p)
1890{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001891 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001892 int len = 0;
1893
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001894 if (video_supported == TPACPI_VIDEO_NONE) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001895 len += sprintf(p + len, "status:\t\tnot supported\n");
1896 return len;
1897 }
1898
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001899 status = video_outputsw_get();
1900 if (status < 0)
1901 return status;
1902
1903 autosw = video_autosw_get();
1904 if (autosw < 0)
1905 return autosw;
1906
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001907 len += sprintf(p + len, "status:\t\tsupported\n");
1908 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
1909 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001910 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001911 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
1912 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
1913 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
1914 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001915 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001916 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
1917 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
1918 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
1919
1920 return len;
1921}
1922
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001923static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924{
1925 char *cmd;
1926 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001927 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001929 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001930 return -ENODEV;
1931
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001932 enable = 0;
1933 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
1935 while ((cmd = next_cmd(&buf))) {
1936 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001937 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001939 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001941 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001943 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001944 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001945 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001946 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001947 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001948 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001949 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001951 res = video_autosw_set(1);
1952 if (res)
1953 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001955 res = video_autosw_set(0);
1956 if (res)
1957 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001959 res = video_outputsw_cycle();
1960 if (res)
1961 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001963 res = video_expand_toggle();
1964 if (res)
1965 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 } else
1967 return -EINVAL;
1968 }
1969
1970 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001971 status = video_outputsw_get();
1972 if (status < 0)
1973 return status;
1974 res = video_outputsw_set((status & ~disable) | enable);
1975 if (res)
1976 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 }
1978
1979 return 0;
1980}
1981
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001982static struct ibm_struct video_driver_data = {
1983 .name = "video",
1984 .read = video_read,
1985 .write = video_write,
1986 .exit = video_exit,
1987};
1988
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001989/*************************************************************************
1990 * Light (thinklight) subdriver
1991 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001993IBM_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
1994IBM_HANDLE(ledb, ec, "LEDB"); /* G4x */
1995
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001996static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001998 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
1999
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002000 IBM_ACPIHANDLE_INIT(ledb);
2001 IBM_ACPIHANDLE_INIT(lght);
2002 IBM_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002003
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002004 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002005 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002006
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002007 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002008 /* light status not supported on
2009 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002010 tp_features.light_status =
2011 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002013 vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002014 str_supported(tp_features.light));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002015
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002016 return (tp_features.light)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017}
2018
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002019static int light_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020{
2021 int len = 0;
2022 int status = 0;
2023
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002024 if (!tp_features.light) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002025 len += sprintf(p + len, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002026 } else if (!tp_features.light_status) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002027 len += sprintf(p + len, "status:\t\tunknown\n");
2028 len += sprintf(p + len, "commands:\ton, off\n");
2029 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
2031 return -EIO;
2032 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002033 len += sprintf(p + len, "commands:\ton, off\n");
2034 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035
2036 return len;
2037}
2038
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002039static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040{
2041 int cmos_cmd, lght_cmd;
2042 char *cmd;
2043 int success;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002044
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002045 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002046 return -ENODEV;
2047
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 while ((cmd = next_cmd(&buf))) {
2049 if (strlencmp(cmd, "on") == 0) {
2050 cmos_cmd = 0x0c;
2051 lght_cmd = 1;
2052 } else if (strlencmp(cmd, "off") == 0) {
2053 cmos_cmd = 0x0d;
2054 lght_cmd = 0;
2055 } else
2056 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002057
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 success = cmos_handle ?
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002059 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
2060 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 if (!success)
2062 return -EIO;
2063 }
2064
2065 return 0;
2066}
2067
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002068static struct ibm_struct light_driver_data = {
2069 .name = "light",
2070 .read = light_read,
2071 .write = light_write,
2072};
2073
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002074/*************************************************************************
2075 * Dock subdriver
2076 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03002078#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002079
2080IBM_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
2081 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
2082 "\\_SB.PCI0.PCI1.DOCK", /* all others */
2083 "\\_SB.PCI.ISA.SLCE", /* 570 */
2084 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
2085
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002086/* don't list other alternatives as we install a notify handler on the 570 */
2087IBM_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
2088
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002089static const struct acpi_device_id ibm_pci_device_ids[] = {
2090 {PCI_ROOT_HID_STRING, 0},
2091 {"", 0},
2092};
2093
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03002094static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
2095 {
2096 .notify = dock_notify,
2097 .handle = &dock_handle,
2098 .type = ACPI_SYSTEM_NOTIFY,
2099 },
2100 {
Henrique de Moraes Holschuh996fba02007-07-18 23:45:40 -03002101 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
2102 * We just use it to get notifications of dock hotplug
2103 * in very old thinkpads */
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002104 .hid = ibm_pci_device_ids,
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03002105 .notify = dock_notify,
2106 .handle = &pci_handle,
2107 .type = ACPI_SYSTEM_NOTIFY,
2108 },
2109};
2110
2111static struct ibm_struct dock_driver_data[2] = {
2112 {
2113 .name = "dock",
2114 .read = dock_read,
2115 .write = dock_write,
2116 .acpi = &ibm_dock_acpidriver[0],
2117 },
2118 {
2119 .name = "dock",
2120 .acpi = &ibm_dock_acpidriver[1],
2121 },
2122};
2123
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124#define dock_docked() (_sta(dock_handle) & 1)
2125
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002126static int __init dock_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002127{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002128 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
2129
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002130 IBM_ACPIHANDLE_INIT(dock);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002131
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002132 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
2133 str_supported(dock_handle != NULL));
2134
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002135 return (dock_handle)? 0 : 1;
2136}
2137
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03002138static int __init dock_init2(struct ibm_init_struct *iibm)
2139{
2140 int dock2_needed;
2141
2142 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
2143
2144 if (dock_driver_data[0].flags.acpi_driver_registered &&
2145 dock_driver_data[0].flags.acpi_notify_installed) {
2146 IBM_ACPIHANDLE_INIT(pci);
2147 dock2_needed = (pci_handle != NULL);
2148 vdbg_printk(TPACPI_DBG_INIT,
2149 "dock PCI handler for the TP 570 is %s\n",
2150 str_supported(dock2_needed));
2151 } else {
2152 vdbg_printk(TPACPI_DBG_INIT,
2153 "dock subdriver part 2 not required\n");
2154 dock2_needed = 0;
2155 }
2156
2157 return (dock2_needed)? 0 : 1;
2158}
2159
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002160static void dock_notify(struct ibm_struct *ibm, u32 event)
2161{
2162 int docked = dock_docked();
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002163 int pci = ibm->acpi->hid && ibm->acpi->device &&
2164 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
Zhang Rui962ce8c2007-08-23 01:24:31 +08002165 int data;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002166
2167 if (event == 1 && !pci) /* 570 */
Zhang Rui962ce8c2007-08-23 01:24:31 +08002168 data = 1; /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002169 else if (event == 1 && pci) /* 570 */
Zhang Rui962ce8c2007-08-23 01:24:31 +08002170 data = 3; /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002171 else if (event == 3 && docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08002172 data = 1; /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002173 else if (event == 3 && !docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08002174 data = 2; /* undock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002175 else if (event == 0 && docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08002176 data = 3; /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002177 else {
2178 printk(IBM_ERR "unknown dock event %d, status %d\n",
2179 event, _sta(dock_handle));
Zhang Rui962ce8c2007-08-23 01:24:31 +08002180 data = 0; /* unknown */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002181 }
Len Brown14e04fb2007-08-23 15:20:26 -04002182 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
Zhang Rui962ce8c2007-08-23 01:24:31 +08002183 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
2184 ibm->acpi->device->dev.bus_id,
2185 event, data);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002186}
2187
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002188static int dock_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189{
2190 int len = 0;
2191 int docked = dock_docked();
2192
2193 if (!dock_handle)
2194 len += sprintf(p + len, "status:\t\tnot supported\n");
2195 else if (!docked)
2196 len += sprintf(p + len, "status:\t\tundocked\n");
2197 else {
2198 len += sprintf(p + len, "status:\t\tdocked\n");
2199 len += sprintf(p + len, "commands:\tdock, undock\n");
2200 }
2201
2202 return len;
2203}
2204
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002205static int dock_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206{
2207 char *cmd;
2208
2209 if (!dock_docked())
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002210 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211
2212 while ((cmd = next_cmd(&buf))) {
2213 if (strlencmp(cmd, "undock") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002214 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
2215 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 return -EIO;
2217 } else if (strlencmp(cmd, "dock") == 0) {
2218 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
2219 return -EIO;
2220 } else
2221 return -EINVAL;
2222 }
2223
2224 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002225}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03002227#endif /* CONFIG_THINKPAD_ACPI_DOCK */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002229/*************************************************************************
2230 * Bay subdriver
2231 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03002233#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002234IBM_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
2235 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
2236 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
2237 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
2238 ); /* A21e, R30, R31 */
2239IBM_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
2240 "_EJ0", /* all others */
2241 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
2242IBM_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
2243 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
2244 ); /* all others */
2245IBM_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
2246 "_EJ0", /* 770x */
2247 ); /* all others */
2248
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002249static int __init bay_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002251 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
2252
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002253 IBM_ACPIHANDLE_INIT(bay);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002254 if (bay_handle)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002255 IBM_ACPIHANDLE_INIT(bay_ej);
2256 IBM_ACPIHANDLE_INIT(bay2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002257 if (bay2_handle)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002258 IBM_ACPIHANDLE_INIT(bay2_ej);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002259
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002260 tp_features.bay_status = bay_handle &&
2261 acpi_evalf(bay_handle, NULL, "_STA", "qv");
2262 tp_features.bay_status2 = bay2_handle &&
2263 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002264
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002265 tp_features.bay_eject = bay_handle && bay_ej_handle &&
2266 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
2267 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
2268 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002270 vdbg_printk(TPACPI_DBG_INIT,
2271 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002272 str_supported(tp_features.bay_status),
2273 str_supported(tp_features.bay_eject),
2274 str_supported(tp_features.bay_status2),
2275 str_supported(tp_features.bay_eject2));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002276
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002277 return (tp_features.bay_status || tp_features.bay_eject ||
2278 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279}
2280
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002281static void bay_notify(struct ibm_struct *ibm, u32 event)
2282{
Len Brown14e04fb2007-08-23 15:20:26 -04002283 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
Zhang Rui962ce8c2007-08-23 01:24:31 +08002284 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
2285 ibm->acpi->device->dev.bus_id,
2286 event, 0);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002287}
2288
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002289#define bay_occupied(b) (_sta(b##_handle) & 1)
2290
2291static int bay_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292{
2293 int len = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002294 int occupied = bay_occupied(bay);
2295 int occupied2 = bay_occupied(bay2);
2296 int eject, eject2;
2297
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002298 len += sprintf(p + len, "status:\t\t%s\n",
2299 tp_features.bay_status ?
2300 (occupied ? "occupied" : "unoccupied") :
2301 "not supported");
2302 if (tp_features.bay_status2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002303 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
2304 "occupied" : "unoccupied");
2305
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002306 eject = tp_features.bay_eject && occupied;
2307 eject2 = tp_features.bay_eject2 && occupied2;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002308
2309 if (eject && eject2)
2310 len += sprintf(p + len, "commands:\teject, eject2\n");
2311 else if (eject)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 len += sprintf(p + len, "commands:\teject\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002313 else if (eject2)
2314 len += sprintf(p + len, "commands:\teject2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315
2316 return len;
2317}
2318
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002319static int bay_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320{
2321 char *cmd;
2322
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002323 if (!tp_features.bay_eject && !tp_features.bay_eject2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002324 return -ENODEV;
2325
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002327 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002328 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
2329 return -EIO;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002330 } else if (tp_features.bay_eject2 &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002331 strlencmp(cmd, "eject2") == 0) {
2332 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 return -EIO;
2334 } else
2335 return -EINVAL;
2336 }
2337
2338 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002339}
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002340
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002341static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
2342 .notify = bay_notify,
2343 .handle = &bay_handle,
2344 .type = ACPI_SYSTEM_NOTIFY,
2345};
2346
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002347static struct ibm_struct bay_driver_data = {
2348 .name = "bay",
2349 .read = bay_read,
2350 .write = bay_write,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002351 .acpi = &ibm_bay_acpidriver,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002352};
2353
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03002354#endif /* CONFIG_THINKPAD_ACPI_BAY */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002356/*************************************************************************
2357 * CMOS subdriver
2358 */
2359
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03002360/* sysfs cmos_command -------------------------------------------------- */
2361static ssize_t cmos_command_store(struct device *dev,
2362 struct device_attribute *attr,
2363 const char *buf, size_t count)
2364{
2365 unsigned long cmos_cmd;
2366 int res;
2367
2368 if (parse_strtoul(buf, 21, &cmos_cmd))
2369 return -EINVAL;
2370
2371 res = issue_thinkpad_cmos_command(cmos_cmd);
2372 return (res)? res : count;
2373}
2374
2375static struct device_attribute dev_attr_cmos_command =
2376 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
2377
2378/* --------------------------------------------------------------------- */
2379
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002380static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002381{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03002382 int res;
2383
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002384 vdbg_printk(TPACPI_DBG_INIT,
2385 "initializing cmos commands subdriver\n");
2386
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002387 IBM_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002388
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002389 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
2390 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03002391
2392 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
2393 if (res)
2394 return res;
2395
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002396 return (cmos_handle)? 0 : 1;
2397}
2398
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03002399static void cmos_exit(void)
2400{
2401 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
2402}
2403
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002404static int cmos_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405{
2406 int len = 0;
2407
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002408 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2409 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 if (!cmos_handle)
2411 len += sprintf(p + len, "status:\t\tnot supported\n");
2412 else {
2413 len += sprintf(p + len, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002414 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 }
2416
2417 return len;
2418}
2419
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002420static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421{
2422 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03002423 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424
2425 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002426 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
2427 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 /* cmos_cmd set */
2429 } else
2430 return -EINVAL;
2431
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03002432 res = issue_thinkpad_cmos_command(cmos_cmd);
2433 if (res)
2434 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 }
2436
2437 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002438}
2439
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002440static struct ibm_struct cmos_driver_data = {
2441 .name = "cmos",
2442 .read = cmos_read,
2443 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03002444 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002445};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002446
2447/*************************************************************************
2448 * LED subdriver
2449 */
2450
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02002451static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002452
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002453IBM_HANDLE(led, ec, "SLED", /* 570 */
2454 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
2455 "LED", /* all others */
2456 ); /* R30, R31 */
2457
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002458static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002459{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002460 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
2461
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002462 IBM_ACPIHANDLE_INIT(led);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002463
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002464 if (!led_handle)
2465 /* led not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002466 led_supported = TPACPI_LED_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002467 else if (strlencmp(led_path, "SLED") == 0)
2468 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002469 led_supported = TPACPI_LED_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002470 else if (strlencmp(led_path, "SYSL") == 0)
2471 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002472 led_supported = TPACPI_LED_OLD;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002473 else
2474 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002475 led_supported = TPACPI_LED_NEW;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002476
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002477 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
2478 str_supported(led_supported), led_supported);
2479
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002480 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002481}
2482
2483#define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
2484
2485static int led_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486{
2487 int len = 0;
2488
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002489 if (!led_supported) {
2490 len += sprintf(p + len, "status:\t\tnot supported\n");
2491 return len;
2492 }
2493 len += sprintf(p + len, "status:\t\tsupported\n");
2494
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002495 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002496 /* 570 */
2497 int i, status;
2498 for (i = 0; i < 8; i++) {
2499 if (!acpi_evalf(ec_handle,
2500 &status, "GLED", "dd", 1 << i))
2501 return -EIO;
2502 len += sprintf(p + len, "%d:\t\t%s\n",
2503 i, led_status(status));
2504 }
2505 }
2506
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 len += sprintf(p + len, "commands:\t"
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002508 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509
2510 return len;
2511}
2512
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002513/* off, on, blink */
2514static const int led_sled_arg1[] = { 0, 1, 3 };
2515static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
2516static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
2517static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
2518
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002519static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520{
2521 char *cmd;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002522 int led, ind, ret;
2523
2524 if (!led_supported)
2525 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526
2527 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002528 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 return -EINVAL;
2530
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002531 if (strstr(cmd, "off")) {
2532 ind = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 } else if (strstr(cmd, "on")) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002534 ind = 1;
2535 } else if (strstr(cmd, "blink")) {
2536 ind = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 } else
2538 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002539
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002540 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002541 /* 570 */
2542 led = 1 << led;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002544 led, led_sled_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 return -EIO;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002546 } else if (led_supported == TPACPI_LED_OLD) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002547 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
2548 led = 1 << led;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002549 ret = ec_write(TPACPI_LED_EC_HLMS, led);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002550 if (ret >= 0)
2551 ret =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002552 ec_write(TPACPI_LED_EC_HLBL,
Henrique de Moraes Holschuhe062e032007-03-23 17:33:55 -03002553 led * led_exp_hlbl[ind]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002554 if (ret >= 0)
2555 ret =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002556 ec_write(TPACPI_LED_EC_HLCL,
Henrique de Moraes Holschuhe062e032007-03-23 17:33:55 -03002557 led * led_exp_hlcl[ind]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002558 if (ret < 0)
2559 return ret;
2560 } else {
2561 /* all others */
2562 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
2563 led, led_led_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 }
2567
2568 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002569}
2570
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002571static struct ibm_struct led_driver_data = {
2572 .name = "led",
2573 .read = led_read,
2574 .write = led_write,
2575};
2576
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002577/*************************************************************************
2578 * Beep subdriver
2579 */
2580
2581IBM_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
2582
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002583static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002584{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002585 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
2586
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002587 IBM_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002588
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002589 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
2590 str_supported(beep_handle != NULL));
2591
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002592 return (beep_handle)? 0 : 1;
2593}
2594
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002595static int beep_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596{
2597 int len = 0;
2598
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002599 if (!beep_handle)
2600 len += sprintf(p + len, "status:\t\tnot supported\n");
2601 else {
2602 len += sprintf(p + len, "status:\t\tsupported\n");
2603 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
2604 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605
2606 return len;
2607}
2608
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002609static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610{
2611 char *cmd;
2612 int beep_cmd;
2613
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002614 if (!beep_handle)
2615 return -ENODEV;
2616
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002618 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
2619 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 /* beep_cmd set */
2621 } else
2622 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002623 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 return -EIO;
2625 }
2626
2627 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002628}
2629
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002630static struct ibm_struct beep_driver_data = {
2631 .name = "beep",
2632 .read = beep_read,
2633 .write = beep_write,
2634};
2635
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002636/*************************************************************************
2637 * Thermal subdriver
2638 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002639
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002640static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002641
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03002642/* sysfs temp##_input -------------------------------------------------- */
2643
2644static ssize_t thermal_temp_input_show(struct device *dev,
2645 struct device_attribute *attr,
2646 char *buf)
2647{
2648 struct sensor_device_attribute *sensor_attr =
2649 to_sensor_dev_attr(attr);
2650 int idx = sensor_attr->index;
2651 s32 value;
2652 int res;
2653
2654 res = thermal_get_sensor(idx, &value);
2655 if (res)
2656 return res;
2657 if (value == TP_EC_THERMAL_TMP_NA * 1000)
2658 return -ENXIO;
2659
2660 return snprintf(buf, PAGE_SIZE, "%d\n", value);
2661}
2662
2663#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
2664 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, thermal_temp_input_show, NULL, _idxB)
2665
2666static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
2667 THERMAL_SENSOR_ATTR_TEMP(1, 0),
2668 THERMAL_SENSOR_ATTR_TEMP(2, 1),
2669 THERMAL_SENSOR_ATTR_TEMP(3, 2),
2670 THERMAL_SENSOR_ATTR_TEMP(4, 3),
2671 THERMAL_SENSOR_ATTR_TEMP(5, 4),
2672 THERMAL_SENSOR_ATTR_TEMP(6, 5),
2673 THERMAL_SENSOR_ATTR_TEMP(7, 6),
2674 THERMAL_SENSOR_ATTR_TEMP(8, 7),
2675 THERMAL_SENSOR_ATTR_TEMP(9, 8),
2676 THERMAL_SENSOR_ATTR_TEMP(10, 9),
2677 THERMAL_SENSOR_ATTR_TEMP(11, 10),
2678 THERMAL_SENSOR_ATTR_TEMP(12, 11),
2679 THERMAL_SENSOR_ATTR_TEMP(13, 12),
2680 THERMAL_SENSOR_ATTR_TEMP(14, 13),
2681 THERMAL_SENSOR_ATTR_TEMP(15, 14),
2682 THERMAL_SENSOR_ATTR_TEMP(16, 15),
2683};
2684
2685#define THERMAL_ATTRS(X) \
2686 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
2687
2688static struct attribute *thermal_temp_input_attr[] = {
2689 THERMAL_ATTRS(8),
2690 THERMAL_ATTRS(9),
2691 THERMAL_ATTRS(10),
2692 THERMAL_ATTRS(11),
2693 THERMAL_ATTRS(12),
2694 THERMAL_ATTRS(13),
2695 THERMAL_ATTRS(14),
2696 THERMAL_ATTRS(15),
2697 THERMAL_ATTRS(0),
2698 THERMAL_ATTRS(1),
2699 THERMAL_ATTRS(2),
2700 THERMAL_ATTRS(3),
2701 THERMAL_ATTRS(4),
2702 THERMAL_ATTRS(5),
2703 THERMAL_ATTRS(6),
2704 THERMAL_ATTRS(7),
2705 NULL
2706};
2707
2708static const struct attribute_group thermal_temp_input16_group = {
2709 .attrs = thermal_temp_input_attr
2710};
2711
2712static const struct attribute_group thermal_temp_input8_group = {
2713 .attrs = &thermal_temp_input_attr[8]
2714};
2715
2716#undef THERMAL_SENSOR_ATTR_TEMP
2717#undef THERMAL_ATTRS
2718
2719/* --------------------------------------------------------------------- */
2720
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002721static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002722{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002723 u8 t, ta1, ta2;
2724 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002725 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03002726 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002727
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002728 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
2729
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002730 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002731
Henrique de Moraes Holschuh3d6f99c2007-07-18 23:45:46 -03002732 if (thinkpad_id.ec_model) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002733 /*
2734 * Direct EC access mode: sensors at registers
2735 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
2736 * non-implemented, thermal sensors return 0x80 when
2737 * not available
2738 */
2739
2740 ta1 = ta2 = 0;
2741 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002742 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002743 ta1 |= t;
2744 } else {
2745 ta1 = 0;
2746 break;
2747 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002748 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002749 ta2 |= t;
2750 } else {
2751 ta1 = 0;
2752 break;
2753 }
2754 }
2755 if (ta1 == 0) {
2756 /* This is sheer paranoia, but we handle it anyway */
2757 if (acpi_tmp7) {
2758 printk(IBM_ERR
2759 "ThinkPad ACPI EC access misbehaving, "
2760 "falling back to ACPI TMPx access mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002761 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002762 } else {
2763 printk(IBM_ERR
2764 "ThinkPad ACPI EC access misbehaving, "
2765 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002766 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002767 }
2768 } else {
2769 thermal_read_mode =
2770 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002771 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002772 }
2773 } else if (acpi_tmp7) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002774 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
2775 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002776 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002777 } else {
2778 /* Standard ACPI TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002779 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002780 }
2781 } else {
2782 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002783 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002784 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002785
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002786 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
2787 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
2788 thermal_read_mode);
2789
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03002790 switch(thermal_read_mode) {
2791 case TPACPI_THERMAL_TPEC_16:
2792 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2793 &thermal_temp_input16_group);
2794 if (res)
2795 return res;
2796 break;
2797 case TPACPI_THERMAL_TPEC_8:
2798 case TPACPI_THERMAL_ACPI_TMP07:
2799 case TPACPI_THERMAL_ACPI_UPDT:
2800 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2801 &thermal_temp_input8_group);
2802 if (res)
2803 return res;
2804 break;
2805 case TPACPI_THERMAL_NONE:
2806 default:
2807 return 1;
2808 }
2809
2810 return 0;
2811}
2812
2813static void thermal_exit(void)
2814{
2815 switch(thermal_read_mode) {
2816 case TPACPI_THERMAL_TPEC_16:
2817 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2818 &thermal_temp_input16_group);
2819 break;
2820 case TPACPI_THERMAL_TPEC_8:
2821 case TPACPI_THERMAL_ACPI_TMP07:
2822 case TPACPI_THERMAL_ACPI_UPDT:
2823 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2824 &thermal_temp_input16_group);
2825 break;
2826 case TPACPI_THERMAL_NONE:
2827 default:
2828 break;
2829 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002830}
2831
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002832/* idx is zero-based */
2833static int thermal_get_sensor(int idx, s32 *value)
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002834{
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002835 int t;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002836 s8 tmp;
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002837 char tmpi[5];
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002838
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002839 t = TP_EC_THERMAL_TMP0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002840
2841 switch (thermal_read_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002842#if TPACPI_MAX_THERMAL_SENSORS >= 16
2843 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002844 if (idx >= 8 && idx <= 15) {
2845 t = TP_EC_THERMAL_TMP8;
2846 idx -= 8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002847 }
2848 /* fallthrough */
2849#endif
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002850 case TPACPI_THERMAL_TPEC_8:
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002851 if (idx <= 7) {
2852 if (!acpi_ec_read(t + idx, &tmp))
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002853 return -EIO;
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002854 *value = tmp * 1000;
2855 return 0;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002856 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002857 break;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002858
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002859 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002860 if (idx <= 7) {
2861 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
2862 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
2863 return -EIO;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002864 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
2865 return -EIO;
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002866 *value = (t - 2732) * 100;
2867 return 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002868 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002869 break;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002870
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002871 case TPACPI_THERMAL_ACPI_TMP07:
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002872 if (idx <= 7) {
2873 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002874 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
2875 return -EIO;
Henrique de Moraes Holschuha8fba3da2007-07-18 23:45:45 -03002876 if (t > 127 || t < -127)
2877 t = TP_EC_THERMAL_TMP_NA;
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002878 *value = t * 1000;
2879 return 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002880 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002881 break;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002882
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002883 case TPACPI_THERMAL_NONE:
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002884 default:
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002885 return -ENOSYS;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002886 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002887
2888 return -EINVAL;
2889}
2890
2891static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
2892{
2893 int res, i;
2894 int n;
2895
2896 n = 8;
2897 i = 0;
2898
2899 if (!s)
2900 return -EINVAL;
2901
2902 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
2903 n = 16;
2904
2905 for(i = 0 ; i < n; i++) {
2906 res = thermal_get_sensor(i, &s->temp[i]);
2907 if (res)
2908 return res;
2909 }
2910
2911 return n;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002912}
2913
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002914static int thermal_read(char *p)
2915{
2916 int len = 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002917 int n, i;
2918 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002919
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002920 n = thermal_get_sensors(&t);
2921 if (unlikely(n < 0))
2922 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002923
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002924 len += sprintf(p + len, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002925
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002926 if (n > 0) {
2927 for (i = 0; i < (n - 1); i++)
2928 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
2929 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
2930 } else
2931 len += sprintf(p + len, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002932
2933 return len;
2934}
2935
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002936static struct ibm_struct thermal_driver_data = {
2937 .name = "thermal",
2938 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03002939 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002940};
2941
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002942/*************************************************************************
2943 * EC Dump subdriver
2944 */
2945
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002946static u8 ecdump_regs[256];
2947
2948static int ecdump_read(char *p)
2949{
2950 int len = 0;
2951 int i, j;
2952 u8 v;
2953
2954 len += sprintf(p + len, "EC "
2955 " +00 +01 +02 +03 +04 +05 +06 +07"
2956 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
2957 for (i = 0; i < 256; i += 16) {
2958 len += sprintf(p + len, "EC 0x%02x:", i);
2959 for (j = 0; j < 16; j++) {
2960 if (!acpi_ec_read(i + j, &v))
2961 break;
2962 if (v != ecdump_regs[i + j])
2963 len += sprintf(p + len, " *%02x", v);
2964 else
2965 len += sprintf(p + len, " %02x", v);
2966 ecdump_regs[i + j] = v;
2967 }
2968 len += sprintf(p + len, "\n");
2969 if (j != 16)
2970 break;
2971 }
2972
2973 /* These are way too dangerous to advertise openly... */
2974#if 0
2975 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
2976 " (<offset> is 00-ff, <value> is 00-ff)\n");
2977 len += sprintf(p + len, "commands:\t0x<offset> <value> "
2978 " (<offset> is 00-ff, <value> is 0-255)\n");
2979#endif
2980 return len;
2981}
2982
2983static int ecdump_write(char *buf)
2984{
2985 char *cmd;
2986 int i, v;
2987
2988 while ((cmd = next_cmd(&buf))) {
2989 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
2990 /* i and v set */
2991 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
2992 /* i and v set */
2993 } else
2994 return -EINVAL;
2995 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
2996 if (!acpi_ec_write(i, v))
2997 return -EIO;
2998 } else
2999 return -EINVAL;
3000 }
3001
3002 return 0;
3003}
3004
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003005static struct ibm_struct ecdump_driver_data = {
3006 .name = "ecdump",
3007 .read = ecdump_read,
3008 .write = ecdump_write,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03003009 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003010};
3011
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003012/*************************************************************************
3013 * Backlight/brightness subdriver
3014 */
Holger Macht8acb0252006-10-20 14:30:28 -07003015
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03003016static struct backlight_device *ibm_backlight_device;
Holger Macht8acb0252006-10-20 14:30:28 -07003017
Richard Purdie599a52d2007-02-10 23:07:48 +00003018static struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02003019 .get_brightness = brightness_get,
3020 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02003021};
3022
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03003023static struct mutex brightness_mutex;
3024
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003025static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02003026{
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02003027 int b;
3028
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003029 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
3030
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03003031 mutex_init(&brightness_mutex);
3032
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03003033 if (!brightness_mode) {
3034 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
3035 brightness_mode = 2;
3036 else
3037 brightness_mode = 3;
3038
3039 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
3040 brightness_mode);
3041 }
3042
3043 if (brightness_mode > 3)
3044 return -EINVAL;
3045
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02003046 b = brightness_get(NULL);
3047 if (b < 0)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03003048 return 1;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02003049
Henrique de Moraes Holschuh7d5a0152007-04-24 11:48:20 -03003050 ibm_backlight_device = backlight_device_register(
3051 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
3052 &ibm_backlight_data);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02003053 if (IS_ERR(ibm_backlight_device)) {
3054 printk(IBM_ERR "Could not register backlight device\n");
3055 return PTR_ERR(ibm_backlight_device);
3056 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003057 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02003058
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02003059 ibm_backlight_device->props.max_brightness = 7;
3060 ibm_backlight_device->props.brightness = b;
3061 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00003062
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02003063 return 0;
3064}
3065
3066static void brightness_exit(void)
3067{
3068 if (ibm_backlight_device) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003069 vdbg_printk(TPACPI_DBG_EXIT,
3070 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02003071 backlight_device_unregister(ibm_backlight_device);
3072 ibm_backlight_device = NULL;
3073 }
3074}
3075
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003076static int brightness_update_status(struct backlight_device *bd)
3077{
3078 return brightness_set(
3079 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
3080 bd->props.power == FB_BLANK_UNBLANK) ?
3081 bd->props.brightness : 0);
3082}
3083
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03003084/*
3085 * ThinkPads can read brightness from two places: EC 0x31, or
3086 * CMOS NVRAM byte 0x5E, bits 0-3.
3087 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003088static int brightness_get(struct backlight_device *bd)
3089{
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03003090 u8 lec = 0, lcmos = 0, level = 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003091
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03003092 if (brightness_mode & 1) {
3093 if (!acpi_ec_read(brightness_offset, &lec))
3094 return -EIO;
3095 lec &= 7;
3096 level = lec;
3097 };
3098 if (brightness_mode & 2) {
3099 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
3100 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
3101 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
3102 level = lcmos;
3103 }
3104
3105 if (brightness_mode == 3 && lec != lcmos) {
3106 printk(IBM_ERR
3107 "CMOS NVRAM (%u) and EC (%u) do not agree "
3108 "on display brightness level\n",
3109 (unsigned int) lcmos,
3110 (unsigned int) lec);
3111 return -EIO;
3112 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003113
3114 return level;
3115}
3116
3117static int brightness_set(int value)
3118{
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03003119 int cmos_cmd, inc, i, res;
3120 int current_value;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003121
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03003122 if (value > 7)
3123 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003124
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03003125 res = mutex_lock_interruptible(&brightness_mutex);
3126 if (res < 0)
3127 return res;
3128
3129 current_value = brightness_get(NULL);
3130 if (current_value < 0) {
3131 res = current_value;
3132 goto errout;
3133 }
3134
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03003135 cmos_cmd = value > current_value ?
3136 TP_CMOS_BRIGHTNESS_UP :
3137 TP_CMOS_BRIGHTNESS_DOWN;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003138 inc = value > current_value ? 1 : -1;
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03003139
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03003140 res = 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003141 for (i = current_value; i != value; i += inc) {
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03003142 if ((brightness_mode & 2) &&
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03003143 issue_thinkpad_cmos_command(cmos_cmd)) {
3144 res = -EIO;
3145 goto errout;
3146 }
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03003147 if ((brightness_mode & 1) &&
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03003148 !acpi_ec_write(brightness_offset, i + inc)) {
3149 res = -EIO;
3150 goto errout;;
3151 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003152 }
3153
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03003154errout:
3155 mutex_unlock(&brightness_mutex);
3156 return res;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003157}
3158
3159static int brightness_read(char *p)
3160{
3161 int len = 0;
3162 int level;
3163
3164 if ((level = brightness_get(NULL)) < 0) {
3165 len += sprintf(p + len, "level:\t\tunreadable\n");
3166 } else {
3167 len += sprintf(p + len, "level:\t\t%d\n", level & 0x7);
3168 len += sprintf(p + len, "commands:\tup, down\n");
3169 len += sprintf(p + len, "commands:\tlevel <level>"
3170 " (<level> is 0-7)\n");
3171 }
3172
3173 return len;
3174}
3175
3176static int brightness_write(char *buf)
3177{
3178 int level;
3179 int new_level;
3180 char *cmd;
3181
3182 while ((cmd = next_cmd(&buf))) {
3183 if ((level = brightness_get(NULL)) < 0)
3184 return level;
3185 level &= 7;
3186
3187 if (strlencmp(cmd, "up") == 0) {
3188 new_level = level == 7 ? 7 : level + 1;
3189 } else if (strlencmp(cmd, "down") == 0) {
3190 new_level = level == 0 ? 0 : level - 1;
3191 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
3192 new_level >= 0 && new_level <= 7) {
3193 /* new_level set */
3194 } else
3195 return -EINVAL;
3196
3197 brightness_set(new_level);
3198 }
3199
3200 return 0;
3201}
3202
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003203static struct ibm_struct brightness_driver_data = {
3204 .name = "brightness",
3205 .read = brightness_read,
3206 .write = brightness_write,
3207 .exit = brightness_exit,
3208};
3209
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003210/*************************************************************************
3211 * Volume subdriver
3212 */
3213
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003214static int volume_read(char *p)
3215{
3216 int len = 0;
3217 u8 level;
3218
3219 if (!acpi_ec_read(volume_offset, &level)) {
3220 len += sprintf(p + len, "level:\t\tunreadable\n");
3221 } else {
3222 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
3223 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
3224 len += sprintf(p + len, "commands:\tup, down, mute\n");
3225 len += sprintf(p + len, "commands:\tlevel <level>"
3226 " (<level> is 0-15)\n");
3227 }
3228
3229 return len;
3230}
3231
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003232static int volume_write(char *buf)
3233{
3234 int cmos_cmd, inc, i;
3235 u8 level, mute;
3236 int new_level, new_mute;
3237 char *cmd;
3238
3239 while ((cmd = next_cmd(&buf))) {
3240 if (!acpi_ec_read(volume_offset, &level))
3241 return -EIO;
3242 new_mute = mute = level & 0x40;
3243 new_level = level = level & 0xf;
3244
3245 if (strlencmp(cmd, "up") == 0) {
3246 if (mute)
3247 new_mute = 0;
3248 else
3249 new_level = level == 15 ? 15 : level + 1;
3250 } else if (strlencmp(cmd, "down") == 0) {
3251 if (mute)
3252 new_mute = 0;
3253 else
3254 new_level = level == 0 ? 0 : level - 1;
3255 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
3256 new_level >= 0 && new_level <= 15) {
3257 /* new_level set */
3258 } else if (strlencmp(cmd, "mute") == 0) {
3259 new_mute = 0x40;
3260 } else
3261 return -EINVAL;
3262
3263 if (new_level != level) { /* mute doesn't change */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003264 cmos_cmd = new_level > level ? TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003265 inc = new_level > level ? 1 : -1;
3266
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03003267 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003268 !acpi_ec_write(volume_offset, level)))
3269 return -EIO;
3270
3271 for (i = level; i != new_level; i += inc)
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03003272 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003273 !acpi_ec_write(volume_offset, i + inc))
3274 return -EIO;
3275
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03003276 if (mute && (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003277 !acpi_ec_write(volume_offset,
3278 new_level + mute)))
3279 return -EIO;
3280 }
3281
3282 if (new_mute != mute) { /* level doesn't change */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003283 cmos_cmd = new_mute ? TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003284
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03003285 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003286 !acpi_ec_write(volume_offset, level + new_mute))
3287 return -EIO;
3288 }
3289 }
3290
3291 return 0;
3292}
3293
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003294static struct ibm_struct volume_driver_data = {
3295 .name = "volume",
3296 .read = volume_read,
3297 .write = volume_write,
3298};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003299
3300/*************************************************************************
3301 * Fan subdriver
3302 */
3303
3304/*
3305 * FAN ACCESS MODES
3306 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003307 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003308 * ACPI GFAN method: returns fan level
3309 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003310 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03003311 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003312 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003313 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003314 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
3315 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03003316 * EC 0x2f (HFSP) might be available *for reading*, but do not use
3317 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003318 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003319 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03003320 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
3321 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003322 *
3323 * Fan speed changes of any sort (including those caused by the
3324 * disengaged mode) are usually done slowly by the firmware as the
3325 * maximum ammount of fan duty cycle change per second seems to be
3326 * limited.
3327 *
3328 * Reading is not available if GFAN exists.
3329 * Writing is not available if SFAN exists.
3330 *
3331 * Bits
3332 * 7 automatic mode engaged;
3333 * (default operation mode of the ThinkPad)
3334 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03003335 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003336 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03003337 * the tachometer while the fan controller ramps up
3338 * the speed (which can take up to a few *minutes*).
3339 * Speeds up fan to 100% duty-cycle, which is far above
3340 * the standard RPM levels. It is not impossible that
3341 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003342 * 5-3 unused in some models. Extra bits for fan level
3343 * in others, but still useless as all values above
3344 * 7 map to the same speed as level 7 in these models.
3345 * 2-0 fan level (0..7 usually)
3346 * 0x00 = stop
3347 * 0x07 = max (set when temperatures critical)
3348 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003349 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003350 *
3351 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
3352 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
3353 * does so, its initial value is meaningless (0x07).
3354 *
3355 * For firmware bugs, refer to:
3356 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
3357 *
3358 * ----
3359 *
3360 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
3361 * Main fan tachometer reading (in RPM)
3362 *
3363 * This register is present on all ThinkPads with a new-style EC, and
3364 * it is known not to be present on the A21m/e, and T22, as there is
3365 * something else in offset 0x84 according to the ACPI DSDT. Other
3366 * ThinkPads from this same time period (and earlier) probably lack the
3367 * tachometer as well.
3368 *
3369 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
3370 * was never fixed by IBM to report the EC firmware version string
3371 * probably support the tachometer (like the early X models), so
3372 * detecting it is quite hard. We need more data to know for sure.
3373 *
3374 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
3375 * might result.
3376 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03003377 * FIRMWARE BUG: may go stale while the EC is switching to full speed
3378 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003379 *
3380 * For firmware bugs, refer to:
3381 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
3382 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003383 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003384 * ThinkPad X31, X40, X41. Not available in the X60.
3385 *
3386 * FANS ACPI handle: takes three arguments: low speed, medium speed,
3387 * high speed. ACPI DSDT seems to map these three speeds to levels
3388 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
3389 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
3390 *
3391 * The speeds are stored on handles
3392 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
3393 *
3394 * There are three default speed sets, acessible as handles:
3395 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
3396 *
3397 * ACPI DSDT switches which set is in use depending on various
3398 * factors.
3399 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003400 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003401 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
3402 * but the ACPI tables just mention level 7.
3403 */
3404
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003405static enum fan_status_access_mode fan_status_access_mode;
3406static enum fan_control_access_mode fan_control_access_mode;
3407static enum fan_control_commands fan_control_commands;
3408
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02003409static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003410static u8 fan_control_desired_level;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02003411
Len Brown25c68a32006-12-08 04:43:41 -05003412static void fan_watchdog_fire(struct work_struct *ignored);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003413static int fan_watchdog_maxinterval;
Len Brown25c68a32006-12-08 04:43:41 -05003414static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003415
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003416IBM_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
3417IBM_HANDLE(gfan, ec, "GFAN", /* 570 */
3418 "\\FSPD", /* 600e/x, 770e, 770x */
3419 ); /* all others */
3420IBM_HANDLE(sfan, ec, "SFAN", /* 570 */
3421 "JFNS", /* 770x-JL */
3422 ); /* all others */
3423
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003424/*
3425 * SYSFS fan layout: hwmon compatible (device)
3426 *
3427 * pwm*_enable:
3428 * 0: "disengaged" mode
3429 * 1: manual mode
3430 * 2: native EC "auto" mode (recommended, hardware default)
3431 *
3432 * pwm*: set speed in manual mode, ignored otherwise.
3433 * 0 is level 0; 255 is level 7. Intermediate points done with linear
3434 * interpolation.
3435 *
3436 * fan*_input: tachometer reading, RPM
3437 *
3438 *
3439 * SYSFS fan layout: extensions
3440 *
3441 * fan_watchdog (driver):
3442 * fan watchdog interval in seconds, 0 disables (default), max 120
3443 */
3444
3445/* sysfs fan pwm1_enable ----------------------------------------------- */
3446static ssize_t fan_pwm1_enable_show(struct device *dev,
3447 struct device_attribute *attr,
3448 char *buf)
3449{
3450 int res, mode;
3451 u8 status;
3452
3453 res = fan_get_status_safe(&status);
3454 if (res)
3455 return res;
3456
3457 if (unlikely(tp_features.fan_ctrl_status_undef)) {
3458 if (status != fan_control_initial_status) {
3459 tp_features.fan_ctrl_status_undef = 0;
3460 } else {
3461 /* Return most likely status. In fact, it
3462 * might be the only possible status */
3463 status = TP_EC_FAN_AUTO;
3464 }
3465 }
3466
3467 if (status & TP_EC_FAN_FULLSPEED) {
3468 mode = 0;
3469 } else if (status & TP_EC_FAN_AUTO) {
3470 mode = 2;
3471 } else
3472 mode = 1;
3473
3474 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
3475}
3476
3477static ssize_t fan_pwm1_enable_store(struct device *dev,
3478 struct device_attribute *attr,
3479 const char *buf, size_t count)
3480{
3481 unsigned long t;
3482 int res, level;
3483
3484 if (parse_strtoul(buf, 2, &t))
3485 return -EINVAL;
3486
3487 switch (t) {
3488 case 0:
3489 level = TP_EC_FAN_FULLSPEED;
3490 break;
3491 case 1:
3492 level = TPACPI_FAN_LAST_LEVEL;
3493 break;
3494 case 2:
3495 level = TP_EC_FAN_AUTO;
3496 break;
3497 case 3:
3498 /* reserved for software-controlled auto mode */
3499 return -ENOSYS;
3500 default:
3501 return -EINVAL;
3502 }
3503
3504 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03003505 if (res == -ENXIO)
3506 return -EINVAL;
3507 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003508 return res;
3509
3510 fan_watchdog_reset();
3511
3512 return count;
3513}
3514
3515static struct device_attribute dev_attr_fan_pwm1_enable =
3516 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
3517 fan_pwm1_enable_show, fan_pwm1_enable_store);
3518
3519/* sysfs fan pwm1 ------------------------------------------------------ */
3520static ssize_t fan_pwm1_show(struct device *dev,
3521 struct device_attribute *attr,
3522 char *buf)
3523{
3524 int res;
3525 u8 status;
3526
3527 res = fan_get_status_safe(&status);
3528 if (res)
3529 return res;
3530
3531 if (unlikely(tp_features.fan_ctrl_status_undef)) {
3532 if (status != fan_control_initial_status) {
3533 tp_features.fan_ctrl_status_undef = 0;
3534 } else {
3535 status = TP_EC_FAN_AUTO;
3536 }
3537 }
3538
3539 if ((status &
3540 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
3541 status = fan_control_desired_level;
3542
3543 if (status > 7)
3544 status = 7;
3545
3546 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
3547}
3548
3549static ssize_t fan_pwm1_store(struct device *dev,
3550 struct device_attribute *attr,
3551 const char *buf, size_t count)
3552{
3553 unsigned long s;
3554 int rc;
3555 u8 status, newlevel;
3556
3557 if (parse_strtoul(buf, 255, &s))
3558 return -EINVAL;
3559
3560 /* scale down from 0-255 to 0-7 */
3561 newlevel = (s >> 5) & 0x07;
3562
3563 rc = mutex_lock_interruptible(&fan_mutex);
3564 if (rc < 0)
3565 return rc;
3566
3567 rc = fan_get_status(&status);
3568 if (!rc && (status &
3569 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
3570 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03003571 if (rc == -ENXIO)
3572 rc = -EINVAL;
3573 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003574 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03003575 fan_watchdog_reset();
3576 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003577 }
3578
3579 mutex_unlock(&fan_mutex);
3580 return (rc)? rc : count;
3581}
3582
3583static struct device_attribute dev_attr_fan_pwm1 =
3584 __ATTR(pwm1, S_IWUSR | S_IRUGO,
3585 fan_pwm1_show, fan_pwm1_store);
3586
3587/* sysfs fan fan1_input ------------------------------------------------ */
3588static ssize_t fan_fan1_input_show(struct device *dev,
3589 struct device_attribute *attr,
3590 char *buf)
3591{
3592 int res;
3593 unsigned int speed;
3594
3595 res = fan_get_speed(&speed);
3596 if (res < 0)
3597 return res;
3598
3599 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
3600}
3601
3602static struct device_attribute dev_attr_fan_fan1_input =
3603 __ATTR(fan1_input, S_IRUGO,
3604 fan_fan1_input_show, NULL);
3605
3606/* sysfs fan fan_watchdog (driver) ------------------------------------- */
3607static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
3608 char *buf)
3609{
3610 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
3611}
3612
3613static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
3614 const char *buf, size_t count)
3615{
3616 unsigned long t;
3617
3618 if (parse_strtoul(buf, 120, &t))
3619 return -EINVAL;
3620
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03003621 if (!fan_control_allowed)
3622 return -EPERM;
3623
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003624 fan_watchdog_maxinterval = t;
3625 fan_watchdog_reset();
3626
3627 return count;
3628}
3629
3630static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
3631 fan_fan_watchdog_show, fan_fan_watchdog_store);
3632
3633/* --------------------------------------------------------------------- */
3634static struct attribute *fan_attributes[] = {
3635 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
3636 &dev_attr_fan_fan1_input.attr,
3637 NULL
3638};
3639
3640static const struct attribute_group fan_attr_group = {
3641 .attrs = fan_attributes,
3642};
3643
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003644static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003645{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003646 int rc;
3647
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003648 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
3649
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003650 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003651 fan_status_access_mode = TPACPI_FAN_NONE;
3652 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003653 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003654 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003655 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003656 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003657
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003658 IBM_ACPIHANDLE_INIT(fans);
3659 IBM_ACPIHANDLE_INIT(gfan);
3660 IBM_ACPIHANDLE_INIT(sfan);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003661
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003662 if (gfan_handle) {
3663 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003664 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003665 } else {
3666 /* all other ThinkPads: note that even old-style
3667 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02003668 if (likely(acpi_ec_read(fan_status_offset,
3669 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003670 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02003671
3672 /* In some ThinkPads, neither the EC nor the ACPI
3673 * DSDT initialize the fan status, and it ends up
3674 * being set to 0x07 when it *could* be either
3675 * 0x07 or 0x80.
3676 *
3677 * Enable for TP-1Y (T43), TP-78 (R51e),
3678 * TP-76 (R52), TP-70 (T43, R52), which are known
3679 * to be buggy. */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03003680 if (fan_control_initial_status == 0x07) {
3681 switch (thinkpad_id.ec_model) {
3682 case 0x5931: /* TP-1Y */
3683 case 0x3837: /* TP-78 */
3684 case 0x3637: /* TP-76 */
3685 case 0x3037: /* TP-70 */
3686 printk(IBM_NOTICE
3687 "fan_init: initial fan status is "
3688 "unknown, assuming it is in auto "
3689 "mode\n");
3690 tp_features.fan_ctrl_status_undef = 1;
3691 ;;
3692 }
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02003693 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003694 } else {
3695 printk(IBM_ERR
3696 "ThinkPad ACPI EC access misbehaving, "
3697 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003698 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003699 }
3700 }
3701
3702 if (sfan_handle) {
3703 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003704 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02003705 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003706 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003707 } else {
3708 if (!gfan_handle) {
3709 /* gfan without sfan means no fan control */
3710 /* all other models implement TP EC 0x2f control */
3711
3712 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02003713 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003714 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003715 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003716 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003717 TPACPI_FAN_CMD_SPEED |
3718 TPACPI_FAN_CMD_LEVEL |
3719 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003720 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003721 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02003722 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003723 TPACPI_FAN_CMD_LEVEL |
3724 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003725 }
3726 }
3727 }
3728
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003729 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
3730 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
3731 fan_control_access_mode != TPACPI_FAN_WR_NONE),
3732 fan_status_access_mode, fan_control_access_mode);
3733
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03003734 /* fan control master switch */
3735 if (!fan_control_allowed) {
3736 fan_control_access_mode = TPACPI_FAN_WR_NONE;
3737 fan_control_commands = 0;
3738 dbg_printk(TPACPI_DBG_INIT,
3739 "fan control features disabled by parameter\n");
3740 }
3741
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003742 /* update fan_control_desired_level */
3743 if (fan_status_access_mode != TPACPI_FAN_NONE)
3744 fan_get_status_safe(NULL);
3745
3746 if (fan_status_access_mode != TPACPI_FAN_NONE ||
3747 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
3748 rc = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3749 &fan_attr_group);
3750 if (!(rc < 0))
3751 rc = driver_create_file(&tpacpi_pdriver.driver,
3752 &driver_attr_fan_watchdog);
3753 if (rc < 0)
3754 return rc;
3755 return 0;
3756 } else
3757 return 1;
3758}
3759
3760/*
3761 * Call with fan_mutex held
3762 */
3763static void fan_update_desired_level(u8 status)
3764{
3765 if ((status &
3766 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
3767 if (status > 7)
3768 fan_control_desired_level = 7;
3769 else
3770 fan_control_desired_level = status;
3771 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003772}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003773
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02003774static int fan_get_status(u8 *status)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003775{
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02003776 u8 s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003777
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02003778 /* TODO:
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003779 * Add TPACPI_FAN_RD_ACPI_FANS ? */
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02003780
Henrique de Moraes Holschuh3ef8a602006-11-24 11:47:10 -02003781 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003782 case TPACPI_FAN_RD_ACPI_GFAN:
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003783 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02003784
3785 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003786 return -EIO;
3787
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02003788 if (likely(status))
3789 *status = s & 0x07;
3790
3791 break;
3792
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003793 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02003794 /* all except 570, 600e/x, 770e, 770x */
3795 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
3796 return -EIO;
3797
3798 if (likely(status))
3799 *status = s;
3800
3801 break;
3802
3803 default:
3804 return -ENXIO;
3805 }
3806
3807 return 0;
3808}
3809
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003810static int fan_get_status_safe(u8 *status)
3811{
3812 int rc;
3813 u8 s;
3814
3815 rc = mutex_lock_interruptible(&fan_mutex);
3816 if (rc < 0)
3817 return rc;
3818 rc = fan_get_status(&s);
3819 if (!rc)
3820 fan_update_desired_level(s);
3821 mutex_unlock(&fan_mutex);
3822
3823 if (status)
3824 *status = s;
3825
3826 return rc;
3827}
3828
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003829static void fan_exit(void)
3830{
Henrique de Moraes Holschuh99fba3f2007-04-21 11:08:44 -03003831 vdbg_printk(TPACPI_DBG_EXIT, "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003832
3833 /* FIXME: can we really do this unconditionally? */
3834 sysfs_remove_group(&tpacpi_pdev->dev.kobj, &fan_attr_group);
3835 driver_remove_file(&tpacpi_pdriver.driver, &driver_attr_fan_watchdog);
3836
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003837 cancel_delayed_work(&fan_watchdog_task);
3838 flush_scheduled_work();
3839}
3840
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02003841static int fan_get_speed(unsigned int *speed)
3842{
3843 u8 hi, lo;
3844
3845 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003846 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02003847 /* all except 570, 600e/x, 770e, 770x */
3848 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
3849 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
3850 return -EIO;
3851
3852 if (likely(speed))
3853 *speed = (hi << 8) | lo;
3854
3855 break;
3856
3857 default:
3858 return -ENXIO;
3859 }
3860
3861 return 0;
3862}
3863
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003864static void fan_watchdog_fire(struct work_struct *ignored)
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003865{
Henrique de Moraes Holschuh99fba3f2007-04-21 11:08:44 -03003866 int rc;
3867
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003868 printk(IBM_NOTICE "fan watchdog: enabling fan\n");
Henrique de Moraes Holschuh99fba3f2007-04-21 11:08:44 -03003869 rc = fan_set_enable();
3870 if (rc < 0) {
3871 printk(IBM_ERR "fan watchdog: error %d while enabling fan, "
3872 "will try again later...\n", -rc);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003873 /* reschedule for later */
3874 fan_watchdog_reset();
3875 }
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003876}
3877
3878static void fan_watchdog_reset(void)
3879{
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03003880 static int fan_watchdog_active;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003881
Henrique de Moraes Holschuh4985cd02007-04-27 22:00:10 -03003882 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
3883 return;
3884
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003885 if (fan_watchdog_active)
3886 cancel_delayed_work(&fan_watchdog_task);
3887
3888 if (fan_watchdog_maxinterval > 0) {
3889 fan_watchdog_active = 1;
3890 if (!schedule_delayed_work(&fan_watchdog_task,
3891 msecs_to_jiffies(fan_watchdog_maxinterval
3892 * 1000))) {
3893 printk(IBM_ERR "failed to schedule the fan watchdog, "
3894 "watchdog will not trigger\n");
3895 }
3896 } else
3897 fan_watchdog_active = 0;
3898}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003899
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003900static int fan_set_level(int level)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003901{
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03003902 if (!fan_control_allowed)
3903 return -EPERM;
3904
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003905 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003906 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003907 if (level >= 0 && level <= 7) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003908 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003909 return -EIO;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003910 } else
3911 return -EINVAL;
3912 break;
3913
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003914 case TPACPI_FAN_WR_ACPI_FANS:
3915 case TPACPI_FAN_WR_TPEC:
3916 if ((level != TP_EC_FAN_AUTO) &&
3917 (level != TP_EC_FAN_FULLSPEED) &&
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02003918 ((level < 0) || (level > 7)))
3919 return -EINVAL;
3920
Henrique de Moraes Holschuheaa75712007-04-24 11:48:18 -03003921 /* safety net should the EC not support AUTO
3922 * or FULLSPEED mode bits and just ignore them */
3923 if (level & TP_EC_FAN_FULLSPEED)
3924 level |= 7; /* safety min speed 7 */
3925 else if (level & TP_EC_FAN_FULLSPEED)
3926 level |= 4; /* safety min speed 4 */
3927
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003928 if (!acpi_ec_write(fan_status_offset, level))
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02003929 return -EIO;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02003930 else
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003931 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02003932 break;
3933
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003934 default:
3935 return -ENXIO;
3936 }
3937 return 0;
3938}
3939
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003940static int fan_set_level_safe(int level)
3941{
3942 int rc;
3943
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03003944 if (!fan_control_allowed)
3945 return -EPERM;
3946
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003947 rc = mutex_lock_interruptible(&fan_mutex);
3948 if (rc < 0)
3949 return rc;
3950
3951 if (level == TPACPI_FAN_LAST_LEVEL)
3952 level = fan_control_desired_level;
3953
3954 rc = fan_set_level(level);
3955 if (!rc)
3956 fan_update_desired_level(level);
3957
3958 mutex_unlock(&fan_mutex);
3959 return rc;
3960}
3961
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003962static int fan_set_enable(void)
3963{
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02003964 u8 s;
3965 int rc;
3966
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03003967 if (!fan_control_allowed)
3968 return -EPERM;
3969
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003970 rc = mutex_lock_interruptible(&fan_mutex);
3971 if (rc < 0)
3972 return rc;
3973
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003974 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003975 case TPACPI_FAN_WR_ACPI_FANS:
3976 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003977 rc = fan_get_status(&s);
3978 if (rc < 0)
3979 break;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02003980
3981 /* Don't go out of emergency fan mode */
Henrique de Moraes Holschuheaa75712007-04-24 11:48:18 -03003982 if (s != 7) {
3983 s &= 0x07;
3984 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
3985 }
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02003986
3987 if (!acpi_ec_write(fan_status_offset, s))
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003988 rc = -EIO;
3989 else {
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003990 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003991 rc = 0;
3992 }
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02003993 break;
3994
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003995 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003996 rc = fan_get_status(&s);
3997 if (rc < 0)
3998 break;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02003999
4000 s &= 0x07;
4001
4002 /* Set fan to at least level 4 */
Henrique de Moraes Holschuheaa75712007-04-24 11:48:18 -03004003 s |= 4;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02004004
4005 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004006 rc= -EIO;
4007 else
4008 rc = 0;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004009 break;
4010
4011 default:
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004012 rc = -ENXIO;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004013 }
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004014
4015 mutex_unlock(&fan_mutex);
4016 return rc;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004017}
4018
4019static int fan_set_disable(void)
4020{
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004021 int rc;
4022
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03004023 if (!fan_control_allowed)
4024 return -EPERM;
4025
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004026 rc = mutex_lock_interruptible(&fan_mutex);
4027 if (rc < 0)
4028 return rc;
4029
4030 rc = 0;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004031 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004032 case TPACPI_FAN_WR_ACPI_FANS:
4033 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004034 if (!acpi_ec_write(fan_status_offset, 0x00))
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004035 rc = -EIO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004036 else {
4037 fan_control_desired_level = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004038 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004039 }
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004040 break;
4041
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004042 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02004043 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004044 rc = -EIO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004045 else
4046 fan_control_desired_level = 0;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02004047 break;
4048
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004049 default:
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004050 rc = -ENXIO;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004051 }
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004052
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004053
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004054 mutex_unlock(&fan_mutex);
4055 return rc;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004056}
4057
4058static int fan_set_speed(int speed)
4059{
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004060 int rc;
4061
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03004062 if (!fan_control_allowed)
4063 return -EPERM;
4064
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004065 rc = mutex_lock_interruptible(&fan_mutex);
4066 if (rc < 0)
4067 return rc;
4068
4069 rc = 0;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004070 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004071 case TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004072 if (speed >= 0 && speed <= 65535) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004073 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
4074 speed, speed, speed))
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004075 rc = -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004076 } else
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004077 rc = -EINVAL;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004078 break;
4079
4080 default:
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004081 rc = -ENXIO;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004082 }
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004083
4084 mutex_unlock(&fan_mutex);
4085 return rc;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004086}
4087
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004088static int fan_read(char *p)
4089{
4090 int len = 0;
4091 int rc;
4092 u8 status;
4093 unsigned int speed = 0;
4094
4095 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004096 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004097 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004098 if ((rc = fan_get_status_safe(&status)) < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004099 return rc;
4100
4101 len += sprintf(p + len, "status:\t\t%s\n"
4102 "level:\t\t%d\n",
4103 (status != 0) ? "enabled" : "disabled", status);
4104 break;
4105
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004106 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004107 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004108 if ((rc = fan_get_status_safe(&status)) < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004109 return rc;
4110
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004111 if (unlikely(tp_features.fan_ctrl_status_undef)) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004112 if (status != fan_control_initial_status)
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004113 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004114 else
4115 /* Return most likely status. In fact, it
4116 * might be the only possible status */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004117 status = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004118 }
4119
4120 len += sprintf(p + len, "status:\t\t%s\n",
4121 (status != 0) ? "enabled" : "disabled");
4122
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004123 if ((rc = fan_get_speed(&speed)) < 0)
4124 return rc;
4125
4126 len += sprintf(p + len, "speed:\t\t%d\n", speed);
4127
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004128 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004129 /* Disengaged mode takes precedence */
4130 len += sprintf(p + len, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004131 else if (status & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004132 len += sprintf(p + len, "level:\t\tauto\n");
4133 else
4134 len += sprintf(p + len, "level:\t\t%d\n", status);
4135 break;
4136
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004137 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004138 default:
4139 len += sprintf(p + len, "status:\t\tnot supported\n");
4140 }
4141
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004142 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004143 len += sprintf(p + len, "commands:\tlevel <level>");
4144
4145 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004146 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004147 len += sprintf(p + len, " (<level> is 0-7)\n");
4148 break;
4149
4150 default:
4151 len += sprintf(p + len, " (<level> is 0-7, "
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004152 "auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004153 break;
4154 }
4155 }
4156
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004157 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004158 len += sprintf(p + len, "commands:\tenable, disable\n"
4159 "commands:\twatchdog <timeout> (<timeout> is 0 (off), "
4160 "1-120 (seconds))\n");
4161
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004162 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004163 len += sprintf(p + len, "commands:\tspeed <speed>"
4164 " (<speed> is 0-65535)\n");
4165
4166 return len;
4167}
4168
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004169static int fan_write_cmd_level(const char *cmd, int *rc)
4170{
4171 int level;
4172
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02004173 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004174 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004175 else if ((strlencmp(cmd, "level disengaged") == 0) |
4176 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004177 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02004178 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004179 return 0;
4180
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004181 if ((*rc = fan_set_level_safe(level)) == -ENXIO)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004182 printk(IBM_ERR "level command accepted for unsupported "
4183 "access mode %d", fan_control_access_mode);
4184
4185 return 1;
4186}
4187
4188static int fan_write_cmd_enable(const char *cmd, int *rc)
4189{
4190 if (strlencmp(cmd, "enable") != 0)
4191 return 0;
4192
4193 if ((*rc = fan_set_enable()) == -ENXIO)
4194 printk(IBM_ERR "enable command accepted for unsupported "
4195 "access mode %d", fan_control_access_mode);
4196
4197 return 1;
4198}
4199
4200static int fan_write_cmd_disable(const char *cmd, int *rc)
4201{
4202 if (strlencmp(cmd, "disable") != 0)
4203 return 0;
4204
4205 if ((*rc = fan_set_disable()) == -ENXIO)
4206 printk(IBM_ERR "disable command accepted for unsupported "
4207 "access mode %d", fan_control_access_mode);
4208
4209 return 1;
4210}
4211
4212static int fan_write_cmd_speed(const char *cmd, int *rc)
4213{
4214 int speed;
4215
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02004216 /* TODO:
4217 * Support speed <low> <medium> <high> ? */
4218
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004219 if (sscanf(cmd, "speed %d", &speed) != 1)
4220 return 0;
4221
4222 if ((*rc = fan_set_speed(speed)) == -ENXIO)
4223 printk(IBM_ERR "speed command accepted for unsupported "
4224 "access mode %d", fan_control_access_mode);
4225
4226 return 1;
4227}
4228
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02004229static int fan_write_cmd_watchdog(const char *cmd, int *rc)
4230{
4231 int interval;
4232
4233 if (sscanf(cmd, "watchdog %d", &interval) != 1)
4234 return 0;
4235
4236 if (interval < 0 || interval > 120)
4237 *rc = -EINVAL;
4238 else
4239 fan_watchdog_maxinterval = interval;
4240
4241 return 1;
4242}
4243
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004244static int fan_write(char *buf)
4245{
4246 char *cmd;
4247 int rc = 0;
4248
4249 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004250 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004251 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004252 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004253 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02004254 fan_write_cmd_disable(cmd, &rc) ||
4255 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004256 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004257 fan_write_cmd_speed(cmd, &rc))
4258 )
4259 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02004260 else if (!rc)
4261 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004262 }
4263
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004264 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004265}
4266
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004267static struct ibm_struct fan_driver_data = {
4268 .name = "fan",
4269 .read = fan_read,
4270 .write = fan_write,
4271 .exit = fan_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004272};
4273
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004274/****************************************************************************
4275 ****************************************************************************
4276 *
4277 * Infrastructure
4278 *
4279 ****************************************************************************
4280 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004282/* /proc support */
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03004283static struct proc_dir_entry *proc_dir;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004284
4285/* Subdriver registry */
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004286static LIST_HEAD(tpacpi_all_drivers);
4287
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004289/*
4290 * Module and infrastructure proble, init and exit handling
4291 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004293#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004294static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004295{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004296 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004297
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004298 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004299}
4300#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
4301
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004302static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303{
4304 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004305 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306 struct proc_dir_entry *entry;
4307
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004308 BUG_ON(ibm == NULL);
4309
4310 INIT_LIST_HEAD(&ibm->all_drivers);
4311
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004312 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313 return 0;
4314
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004315 dbg_printk(TPACPI_DBG_INIT,
4316 "probing for %s\n", ibm->name);
4317
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004318 if (iibm->init) {
4319 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004320 if (ret > 0)
4321 return 0; /* probe failed */
4322 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004324
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004325 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326 }
4327
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004328 if (ibm->acpi) {
4329 if (ibm->acpi->hid) {
4330 ret = register_tpacpi_subdriver(ibm);
4331 if (ret)
4332 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004333 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004334
4335 if (ibm->acpi->notify) {
4336 ret = setup_acpi_notify(ibm);
4337 if (ret == -ENODEV) {
4338 printk(IBM_NOTICE "disabling subdriver %s\n",
4339 ibm->name);
4340 ret = 0;
4341 goto err_out;
4342 }
4343 if (ret < 0)
4344 goto err_out;
4345 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004346 }
4347
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004348 dbg_printk(TPACPI_DBG_INIT,
4349 "%s installed\n", ibm->name);
4350
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004351 if (ibm->read) {
4352 entry = create_proc_entry(ibm->name,
4353 S_IFREG | S_IRUGO | S_IWUSR,
4354 proc_dir);
4355 if (!entry) {
4356 printk(IBM_ERR "unable to create proc entry %s\n",
4357 ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004358 ret = -ENODEV;
4359 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004360 }
4361 entry->owner = THIS_MODULE;
4362 entry->data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004363 entry->read_proc = &dispatch_procfs_read;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004364 if (ibm->write)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004365 entry->write_proc = &dispatch_procfs_write;
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004366 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004369 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
4370
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004372
4373err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004374 dbg_printk(TPACPI_DBG_INIT,
4375 "%s: at error exit path with result %d\n",
4376 ibm->name, ret);
4377
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004378 ibm_exit(ibm);
4379 return (ret < 0)? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380}
4381
4382static void ibm_exit(struct ibm_struct *ibm)
4383{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004384 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004385
4386 list_del_init(&ibm->all_drivers);
4387
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004388 if (ibm->flags.acpi_notify_installed) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004389 dbg_printk(TPACPI_DBG_EXIT,
4390 "%s: acpi_remove_notify_handler\n", ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004391 BUG_ON(!ibm->acpi);
4392 acpi_remove_notify_handler(*ibm->acpi->handle,
4393 ibm->acpi->type,
4394 dispatch_acpi_notify);
4395 ibm->flags.acpi_notify_installed = 0;
4396 ibm->flags.acpi_notify_installed = 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004397 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004399 if (ibm->flags.proc_created) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004400 dbg_printk(TPACPI_DBG_EXIT,
4401 "%s: remove_proc_entry\n", ibm->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402 remove_proc_entry(ibm->name, proc_dir);
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004403 ibm->flags.proc_created = 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004404 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004406 if (ibm->flags.acpi_driver_registered) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004407 dbg_printk(TPACPI_DBG_EXIT,
4408 "%s: acpi_bus_unregister_driver\n", ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004409 BUG_ON(!ibm->acpi);
4410 acpi_bus_unregister_driver(ibm->acpi->driver);
4411 kfree(ibm->acpi->driver);
4412 ibm->acpi->driver = NULL;
4413 ibm->flags.acpi_driver_registered = 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004414 }
4415
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004416 if (ibm->flags.init_called && ibm->exit) {
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004417 ibm->exit();
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004418 ibm->flags.init_called = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004420
4421 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422}
4423
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004424/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03004426static void __init get_thinkpad_model_data(struct thinkpad_id_data *tp)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004427{
4428 struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02004429 char ec_fw_string[18];
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004430
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03004431 if (!tp)
4432 return;
4433
4434 memset(tp, 0, sizeof(*tp));
4435
4436 if (dmi_name_in_vendors("IBM"))
4437 tp->vendor = PCI_VENDOR_ID_IBM;
4438 else if (dmi_name_in_vendors("LENOVO"))
4439 tp->vendor = PCI_VENDOR_ID_LENOVO;
4440 else
4441 return;
4442
4443 tp->bios_version_str = kstrdup(dmi_get_system_info(DMI_BIOS_VERSION),
4444 GFP_KERNEL);
4445 if (!tp->bios_version_str)
4446 return;
4447 tp->bios_model = tp->bios_version_str[0]
4448 | (tp->bios_version_str[1] << 8);
4449
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004450 /*
4451 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
4452 * X32 or newer, all Z series; Some models must have an
4453 * up-to-date BIOS or they will not be detected.
4454 *
4455 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
4456 */
4457 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02004458 if (sscanf(dev->name,
4459 "IBM ThinkPad Embedded Controller -[%17c",
4460 ec_fw_string) == 1) {
4461 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
4462 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03004463
4464 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
4465 tp->ec_model = ec_fw_string[0]
4466 | (ec_fw_string[1] << 8);
4467 break;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02004468 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004469 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03004470
4471 tp->model_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_VERSION),
4472 GFP_KERNEL);
4473 if (strnicmp(tp->model_str, "ThinkPad", 8) != 0) {
4474 kfree(tp->model_str);
4475 tp->model_str = NULL;
4476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477}
4478
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004479static int __init probe_for_thinkpad(void)
4480{
4481 int is_thinkpad;
4482
4483 if (acpi_disabled)
4484 return -ENODEV;
4485
4486 /*
4487 * Non-ancient models have better DMI tagging, but very old models
4488 * don't.
4489 */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03004490 is_thinkpad = (thinkpad_id.model_str != NULL);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004491
4492 /* ec is required because many other handles are relative to it */
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004493 IBM_ACPIHANDLE_INIT(ec);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004494 if (!ec_handle) {
4495 if (is_thinkpad)
4496 printk(IBM_ERR
4497 "Not yet supported ThinkPad detected!\n");
4498 return -ENODEV;
4499 }
4500
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03004501 /*
4502 * Risks a regression on very old machines, but reduces potential
4503 * false positives a damn great deal
4504 */
4505 if (!is_thinkpad)
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03004506 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03004507
4508 if (!is_thinkpad && !force_load)
4509 return -ENODEV;
4510
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004511 return 0;
4512}
4513
4514
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004515/* Module init, exit, parameters */
4516
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004517static struct ibm_init_struct ibms_init[] __initdata = {
4518 {
4519 .init = thinkpad_acpi_driver_init,
4520 .data = &thinkpad_acpi_driver_data,
4521 },
4522 {
4523 .init = hotkey_init,
4524 .data = &hotkey_driver_data,
4525 },
4526 {
4527 .init = bluetooth_init,
4528 .data = &bluetooth_driver_data,
4529 },
4530 {
4531 .init = wan_init,
4532 .data = &wan_driver_data,
4533 },
4534 {
4535 .init = video_init,
4536 .data = &video_driver_data,
4537 },
4538 {
4539 .init = light_init,
4540 .data = &light_driver_data,
4541 },
4542#ifdef CONFIG_THINKPAD_ACPI_DOCK
4543 {
4544 .init = dock_init,
4545 .data = &dock_driver_data[0],
4546 },
4547 {
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03004548 .init = dock_init2,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004549 .data = &dock_driver_data[1],
4550 },
4551#endif
4552#ifdef CONFIG_THINKPAD_ACPI_BAY
4553 {
4554 .init = bay_init,
4555 .data = &bay_driver_data,
4556 },
4557#endif
4558 {
4559 .init = cmos_init,
4560 .data = &cmos_driver_data,
4561 },
4562 {
4563 .init = led_init,
4564 .data = &led_driver_data,
4565 },
4566 {
4567 .init = beep_init,
4568 .data = &beep_driver_data,
4569 },
4570 {
4571 .init = thermal_init,
4572 .data = &thermal_driver_data,
4573 },
4574 {
4575 .data = &ecdump_driver_data,
4576 },
4577 {
4578 .init = brightness_init,
4579 .data = &brightness_driver_data,
4580 },
4581 {
4582 .data = &volume_driver_data,
4583 },
4584 {
4585 .init = fan_init,
4586 .data = &fan_driver_data,
4587 },
4588};
4589
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004590static int __init set_ibm_param(const char *val, struct kernel_param *kp)
4591{
4592 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004593 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004594
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004595 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
4596 ibm = ibms_init[i].data;
4597 BUG_ON(ibm == NULL);
4598
4599 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
4600 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004601 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004602 strcpy(ibms_init[i].param, val);
4603 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004604 return 0;
4605 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004606 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004607
4608 return -EINVAL;
4609}
4610
4611static int experimental;
4612module_param(experimental, int, 0);
4613
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03004614static u32 dbg_level;
4615module_param_named(debug, dbg_level, uint, 0);
4616
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03004617static int force_load;
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03004618module_param(force_load, bool, 0);
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03004619
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03004620static int fan_control_allowed;
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03004621module_param_named(fan_control, fan_control_allowed, bool, 0);
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03004622
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03004623static int brightness_mode;
4624module_param_named(brightness_mode, brightness_mode, int, 0);
4625
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004626#define IBM_PARAM(feature) \
4627 module_param_call(feature, set_ibm_param, NULL, NULL, 0)
4628
4629IBM_PARAM(hotkey);
4630IBM_PARAM(bluetooth);
4631IBM_PARAM(video);
4632IBM_PARAM(light);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03004633#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004634IBM_PARAM(dock);
4635#endif
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03004636#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004637IBM_PARAM(bay);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03004638#endif /* CONFIG_THINKPAD_ACPI_BAY */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004639IBM_PARAM(cmos);
4640IBM_PARAM(led);
4641IBM_PARAM(beep);
4642IBM_PARAM(ecdump);
4643IBM_PARAM(brightness);
4644IBM_PARAM(volume);
4645IBM_PARAM(fan);
4646
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03004647static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648{
4649 int ret, i;
4650
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03004651 /* Driver-level probe */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03004652
4653 get_thinkpad_model_data(&thinkpad_id);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004654 ret = probe_for_thinkpad();
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03004655 if (ret) {
4656 thinkpad_acpi_module_exit();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004657 return ret;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03004658 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03004660 /* Driver initialization */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03004661
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004662 IBM_ACPIHANDLE_INIT(ecrd);
4663 IBM_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004665 proc_dir = proc_mkdir(IBM_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666 if (!proc_dir) {
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004667 printk(IBM_ERR "unable to create proc dir " IBM_PROC_DIR);
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03004668 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669 return -ENODEV;
4670 }
4671 proc_dir->owner = THIS_MODULE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004672
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03004673 ret = platform_driver_register(&tpacpi_pdriver);
4674 if (ret) {
4675 printk(IBM_ERR "unable to register platform driver\n");
4676 thinkpad_acpi_module_exit();
4677 return ret;
4678 }
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03004679 tp_features.platform_drv_registered = 1;
4680
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03004681 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
4682 if (ret) {
4683 printk(IBM_ERR "unable to create sysfs driver attributes\n");
4684 thinkpad_acpi_module_exit();
4685 return ret;
4686 }
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03004687 tp_features.platform_drv_attrs_registered = 1;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03004688
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03004689
4690 /* Device initialization */
4691 tpacpi_pdev = platform_device_register_simple(IBM_DRVR_NAME, -1,
4692 NULL, 0);
4693 if (IS_ERR(tpacpi_pdev)) {
4694 ret = PTR_ERR(tpacpi_pdev);
4695 tpacpi_pdev = NULL;
4696 printk(IBM_ERR "unable to register platform device\n");
4697 thinkpad_acpi_module_exit();
4698 return ret;
4699 }
4700 tpacpi_hwmon = hwmon_device_register(&tpacpi_pdev->dev);
4701 if (IS_ERR(tpacpi_hwmon)) {
4702 ret = PTR_ERR(tpacpi_hwmon);
4703 tpacpi_hwmon = NULL;
4704 printk(IBM_ERR "unable to register hwmon device\n");
4705 thinkpad_acpi_module_exit();
4706 return ret;
4707 }
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03004708 tpacpi_inputdev = input_allocate_device();
4709 if (!tpacpi_inputdev) {
4710 printk(IBM_ERR "unable to allocate input device\n");
4711 thinkpad_acpi_module_exit();
4712 return -ENOMEM;
4713 } else {
4714 /* Prepare input device, but don't register */
4715 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
4716 tpacpi_inputdev->phys = IBM_DRVR_NAME "/input0";
4717 tpacpi_inputdev->id.bustype = BUS_HOST;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03004718 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
4719 thinkpad_id.vendor :
4720 PCI_VENDOR_ID_IBM;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03004721 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
4722 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
4723 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004724 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
4725 ret = ibm_init(&ibms_init[i]);
4726 if (ret >= 0 && *ibms_init[i].param)
4727 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03004729 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004730 return ret;
4731 }
4732 }
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03004733 ret = input_register_device(tpacpi_inputdev);
4734 if (ret < 0) {
4735 printk(IBM_ERR "unable to register input device\n");
4736 thinkpad_acpi_module_exit();
4737 return ret;
4738 } else {
4739 tp_features.input_device_registered = 1;
4740 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741
4742 return 0;
4743}
4744
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03004745static void thinkpad_acpi_module_exit(void)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004746{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004747 struct ibm_struct *ibm, *itmp;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004748
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004749 list_for_each_entry_safe_reverse(ibm, itmp,
4750 &tpacpi_all_drivers,
4751 all_drivers) {
4752 ibm_exit(ibm);
4753 }
4754
4755 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004756
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03004757 if (tpacpi_inputdev) {
4758 if (tp_features.input_device_registered)
4759 input_unregister_device(tpacpi_inputdev);
4760 else
4761 input_free_device(tpacpi_inputdev);
4762 }
4763
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03004764 if (tpacpi_hwmon)
4765 hwmon_device_unregister(tpacpi_hwmon);
4766
4767 if (tpacpi_pdev)
4768 platform_device_unregister(tpacpi_pdev);
4769
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03004770 if (tp_features.platform_drv_attrs_registered)
4771 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
4772
4773 if (tp_features.platform_drv_registered)
4774 platform_driver_unregister(&tpacpi_pdriver);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03004775
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004776 if (proc_dir)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004777 remove_proc_entry(IBM_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004778
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03004779 kfree(thinkpad_id.bios_version_str);
4780 kfree(thinkpad_id.ec_version_str);
4781 kfree(thinkpad_id.model_str);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004782}
4783
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03004784module_init(thinkpad_acpi_module_init);
4785module_exit(thinkpad_acpi_module_exit);