blob: 8aa0f9694b87cc0b32c5570db8226c12ac14ac09 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -03002 * thinkpad_acpi.c - ThinkPad ACPI Extras
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 *
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
Henrique de Moraes Holschuha62bc912007-03-23 17:33:58 -03006 * Copyright (C) 2006-2007 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
Henrique de Moraes Holschuha62bc912007-03-23 17:33:58 -030020 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
Borislav Deianov78f81cc2005-08-17 00:00:00 -040022 */
23
Henrique de Moraes Holschuh3b0c6482007-09-04 11:13:16 -030024#define IBM_VERSION "0.16"
Henrique de Moraes 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 Holschuhff80f132007-09-04 11:13:15 -0300909/* sysfs hotkey report_mode -------------------------------------------- */
910static ssize_t hotkey_report_mode_show(struct device *dev,
911 struct device_attribute *attr,
912 char *buf)
913{
914 return snprintf(buf, PAGE_SIZE, "%d\n",
915 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
916}
917
918static struct device_attribute dev_attr_hotkey_report_mode =
919 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
920
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300921/* --------------------------------------------------------------------- */
922
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -0300923static struct attribute *hotkey_attributes[] __initdata = {
924 &dev_attr_hotkey_enable.attr,
925 &dev_attr_hotkey_report_mode.attr,
926};
927
928static struct attribute *hotkey_mask_attributes[] __initdata = {
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300929 &dev_attr_hotkey_mask.attr,
930 &dev_attr_hotkey_bios_enabled.attr,
931 &dev_attr_hotkey_bios_mask.attr,
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -0300932 &dev_attr_hotkey_all_mask.attr,
933 &dev_attr_hotkey_recommended_mask.attr,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300934};
935
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300936static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300937{
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -0300938
939 static u16 ibm_keycode_map[] __initdata = {
940 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
941 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
942 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
943 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
944 /* Scan codes 0x0C to 0x0F: Other ACPI HKEY hot keys */
945 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
946 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
947 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
948 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
949 /* Scan codes 0x10 to 0x1F: Extended ACPI HKEY hot keys */
950 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
951 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
952 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
953 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
954 KEY_RESERVED, /* 0x14: VOLUME UP */
955 KEY_RESERVED, /* 0x15: VOLUME DOWN */
956 KEY_RESERVED, /* 0x16: MUTE */
957 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
958 /* (assignments unknown, please report if found) */
959 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
960 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
961 };
962 static u16 lenovo_keycode_map[] __initdata = {
963 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
964 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
965 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
966 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
967 /* Scan codes 0x0C to 0x0F: Other ACPI HKEY hot keys */
968 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
969 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
970 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
971 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
972 /* Scan codes 0x10 to 0x1F: Extended ACPI HKEY hot keys */
973 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
974 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
975 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
976 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
977 KEY_RESERVED, /* 0x14: VOLUME UP */
978 KEY_RESERVED, /* 0x15: VOLUME DOWN */
979 KEY_RESERVED, /* 0x16: MUTE */
980 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
981 /* (assignments unknown, please report if found) */
982 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
983 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
984 };
985
986#define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
987#define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
988#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
989
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -0300990 int res, i;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -0300991 int status;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300992
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300993 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
994
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -0300995 BUG_ON(!tpacpi_inputdev);
996
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300997 IBM_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -0300998 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300999
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001000 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001001 tp_features.hotkey = hkey_handle != NULL;
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, "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001004 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001005
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001006 if (tp_features.hotkey) {
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001007 hotkey_dev_attributes = create_attr_set(8, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001008 if (!hotkey_dev_attributes)
1009 return -ENOMEM;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001010 res = add_many_to_attr_set(hotkey_dev_attributes,
1011 hotkey_attributes,
1012 ARRAY_SIZE(hotkey_attributes));
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001013 if (res)
1014 return res;
1015
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001016 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1017 A30, R30, R31, T20-22, X20-21, X22-24 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001018 tp_features.hotkey_mask =
1019 acpi_evalf(hkey_handle, NULL, "DHKN", "qv");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001020
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001021 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001022 str_supported(tp_features.hotkey_mask));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001023
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001024 if (tp_features.hotkey_mask) {
1025 /* MHKA available in A31, R40, R40e, T4x, X31, and later */
1026 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
1027 "MHKA", "qd"))
1028 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
1029 }
1030
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001031 res = hotkey_get(&hotkey_orig_status, &hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001032 if (!res && tp_features.hotkey_mask) {
1033 res = add_many_to_attr_set(hotkey_dev_attributes,
1034 hotkey_mask_attributes,
1035 ARRAY_SIZE(hotkey_mask_attributes));
1036 }
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001037
1038 /* Not all thinkpads have a hardware radio switch */
1039 if (!res && acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
1040 tp_features.hotkey_wlsw = 1;
1041 printk(IBM_INFO
1042 "radio switch found; radios are %s\n",
1043 enabled(status, 0));
1044 res = add_to_attr_set(hotkey_dev_attributes,
1045 &dev_attr_hotkey_radio_sw.attr);
1046 }
1047
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001048 if (!res)
1049 res = register_attr_set_with_sysfs(
1050 hotkey_dev_attributes,
1051 &tpacpi_pdev->dev.kobj);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001052 if (res)
1053 return res;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001054
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001055 /* Set up key map */
1056
1057 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
1058 GFP_KERNEL);
1059 if (!hotkey_keycode_map) {
1060 printk(IBM_ERR "failed to allocate memory for key map\n");
1061 return -ENOMEM;
1062 }
1063
1064 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
1065 dbg_printk(TPACPI_DBG_INIT,
1066 "using Lenovo default hot key map\n");
1067 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
1068 TPACPI_HOTKEY_MAP_SIZE);
1069 } else {
1070 dbg_printk(TPACPI_DBG_INIT,
1071 "using IBM default hot key map\n");
1072 memcpy(hotkey_keycode_map, &ibm_keycode_map,
1073 TPACPI_HOTKEY_MAP_SIZE);
1074 }
1075
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001076 set_bit(EV_KEY, tpacpi_inputdev->evbit);
1077 set_bit(EV_MSC, tpacpi_inputdev->evbit);
1078 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001079 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
1080 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
1081 tpacpi_inputdev->keycode = hotkey_keycode_map;
1082 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001083 if (hotkey_keycode_map[i] != KEY_RESERVED) {
1084 set_bit(hotkey_keycode_map[i],
1085 tpacpi_inputdev->keybit);
1086 } else {
1087 if (i < sizeof(hotkey_reserved_mask)*8)
1088 hotkey_reserved_mask |= 1 << i;
1089 }
1090 }
1091
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03001092 if (tp_features.hotkey_wlsw) {
1093 set_bit(EV_SW, tpacpi_inputdev->evbit);
1094 set_bit(SW_RADIO, tpacpi_inputdev->swbit);
1095 }
1096
Henrique de Moraes Holschuh1a343762007-07-18 23:45:36 -03001097 dbg_printk(TPACPI_DBG_INIT,
1098 "enabling hot key handling\n");
1099 res = hotkey_set(1, (hotkey_all_mask & ~hotkey_reserved_mask)
1100 | hotkey_orig_mask);
1101 if (res)
1102 return res;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001103
1104 dbg_printk(TPACPI_DBG_INIT,
1105 "legacy hot key reporting over procfs %s\n",
1106 (hotkey_report_mode < 2) ?
1107 "enabled" : "disabled");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001108 }
1109
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001110 return (tp_features.hotkey)? 0 : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001111}
1112
1113static void hotkey_exit(void)
1114{
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001115 int res;
1116
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001117 if (tp_features.hotkey) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001118 dbg_printk(TPACPI_DBG_EXIT, "restoring original hotkey mask\n");
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001119 res = hotkey_set(hotkey_orig_status, hotkey_orig_mask);
1120 if (res)
1121 printk(IBM_ERR "failed to restore hotkey to BIOS defaults\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001122 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001123
1124 if (hotkey_dev_attributes) {
1125 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
1126 hotkey_dev_attributes = NULL;
1127 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001128}
1129
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001130static void tpacpi_input_send_key(unsigned int scancode,
1131 unsigned int keycode)
1132{
1133 if (keycode != KEY_RESERVED) {
1134 input_report_key(tpacpi_inputdev, keycode, 1);
1135 if (keycode == KEY_UNKNOWN)
1136 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1137 scancode);
1138 input_sync(tpacpi_inputdev);
1139
1140 input_report_key(tpacpi_inputdev, keycode, 0);
1141 if (keycode == KEY_UNKNOWN)
1142 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1143 scancode);
1144 input_sync(tpacpi_inputdev);
1145 }
1146}
1147
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03001148static void tpacpi_input_send_radiosw(void)
1149{
1150 int wlsw;
1151
Henrique de Moraes Holschuhf4e1e432007-09-23 11:39:00 -03001152 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03001153 input_report_switch(tpacpi_inputdev,
1154 SW_RADIO, !!wlsw);
Henrique de Moraes Holschuhf4e1e432007-09-23 11:39:00 -03001155 input_sync(tpacpi_inputdev);
1156 }
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03001157}
1158
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001159static void hotkey_notify(struct ibm_struct *ibm, u32 event)
1160{
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001161 u32 hkey;
1162 unsigned int keycode, scancode;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001163 int send_acpi_ev = 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001164
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001165 if (event == 0x80 && acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001166 switch (hkey >> 12) {
1167 case 1:
1168 /* 0x1000-0x1FFF: key presses */
1169 scancode = hkey & 0xfff;
1170 if (scancode > 0 && scancode < 0x21) {
1171 scancode--;
1172 keycode = hotkey_keycode_map[scancode];
1173 tpacpi_input_send_key(scancode, keycode);
1174 } else {
1175 printk(IBM_ERR
1176 "hotkey 0x%04x out of range for keyboard map\n",
1177 hkey);
1178 send_acpi_ev = 1;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001179 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001180 break;
1181 case 5:
1182 /* 0x5000-0x5FFF: LID */
1183 /* we don't handle it through this path, just
1184 * eat up known LID events */
1185 if (hkey != 0x5001 && hkey != 0x5002) {
1186 printk(IBM_ERR
1187 "unknown LID-related hotkey event: 0x%04x\n",
1188 hkey);
1189 send_acpi_ev = 1;
1190 }
1191 break;
1192 case 7:
1193 /* 0x7000-0x7FFF: misc */
1194 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
1195 tpacpi_input_send_radiosw();
1196 break;
1197 }
1198 /* fallthrough to default */
1199 default:
1200 /* case 2: dock-related */
1201 /* 0x2305 - T43 waking up due to bay lever eject while aslept */
1202 /* case 3: ultra-bay related. maybe bay in dock? */
1203 /* 0x3003 - T43 after wake up by bay lever eject (0x2305) */
1204 printk(IBM_NOTICE "unhandled HKEY event 0x%04x\n", hkey);
1205 send_acpi_ev = 1;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001206 }
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001207 } else {
1208 printk(IBM_ERR "unknown hotkey notification event %d\n", event);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001209 hkey = 0;
1210 send_acpi_ev = 1;
1211 }
1212
1213 /* Legacy events */
1214 if (send_acpi_ev || hotkey_report_mode < 2)
1215 acpi_bus_generate_proc_event(ibm->acpi->device, event, hkey);
1216
1217 /* netlink events */
1218 if (send_acpi_ev) {
1219 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
1220 ibm->acpi->device->dev.bus_id,
1221 event, hkey);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001222 }
1223}
1224
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03001225static void hotkey_resume(void)
1226{
1227 tpacpi_input_send_radiosw();
1228}
1229
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001230/*
1231 * Call with hotkey_mutex held
1232 */
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001233static int hotkey_get(int *status, u32 *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234{
1235 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001236 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001237
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001238 if (tp_features.hotkey_mask)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001239 if (!acpi_evalf(hkey_handle, mask, "DHKN", "d"))
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001240 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001241
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001242 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243}
1244
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001245/*
1246 * Call with hotkey_mutex held
1247 */
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001248static int hotkey_set(int status, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249{
1250 int i;
1251
1252 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001253 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001255 if (tp_features.hotkey_mask)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001256 for (i = 0; i < 32; i++) {
1257 int bit = ((1 << i) & mask) != 0;
1258 if (!acpi_evalf(hkey_handle,
1259 NULL, "MHKM", "vdd", i + 1, bit))
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001260 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001261 }
1262
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001263 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001264}
1265
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001266/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001267static int hotkey_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001269 int res, status;
1270 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 int len = 0;
1272
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001273 if (!tp_features.hotkey) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001274 len += sprintf(p + len, "status:\t\tnot supported\n");
1275 return len;
1276 }
1277
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001278 res = mutex_lock_interruptible(&hotkey_mutex);
1279 if (res < 0)
1280 return res;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001281 res = hotkey_get(&status, &mask);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001282 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001283 if (res)
1284 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
1286 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001287 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001288 len += sprintf(p + len, "mask:\t\t0x%08x\n", mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 len += sprintf(p + len,
1290 "commands:\tenable, disable, reset, <mask>\n");
1291 } else {
1292 len += sprintf(p + len, "mask:\t\tnot supported\n");
1293 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
1294 }
1295
1296 return len;
1297}
1298
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001299static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001301 int res, status;
1302 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 char *cmd;
1304 int do_cmd = 0;
1305
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001306 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 return -ENODEV;
1308
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001309 res = mutex_lock_interruptible(&hotkey_mutex);
1310 if (res < 0)
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001311 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001312
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001313 res = hotkey_get(&status, &mask);
1314 if (res)
1315 goto errexit;
1316
1317 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 while ((cmd = next_cmd(&buf))) {
1319 if (strlencmp(cmd, "enable") == 0) {
1320 status = 1;
1321 } else if (strlencmp(cmd, "disable") == 0) {
1322 status = 0;
1323 } else if (strlencmp(cmd, "reset") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001324 status = hotkey_orig_status;
1325 mask = hotkey_orig_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
1327 /* mask set */
1328 } else if (sscanf(cmd, "%x", &mask) == 1) {
1329 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001330 } else {
1331 res = -EINVAL;
1332 goto errexit;
1333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 do_cmd = 1;
1335 }
1336
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001337 if (do_cmd)
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001338 res = hotkey_set(status, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001340errexit:
1341 mutex_unlock(&hotkey_mutex);
1342 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001343}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
Thomas Renninger1ba90e32007-07-23 14:44:41 +02001345static const struct acpi_device_id ibm_htk_device_ids[] = {
1346 {IBM_HKEY_HID, 0},
1347 {"", 0},
1348};
1349
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001350static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +02001351 .hid = ibm_htk_device_ids,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001352 .notify = hotkey_notify,
1353 .handle = &hkey_handle,
1354 .type = ACPI_DEVICE_NOTIFY,
1355};
1356
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001357static struct ibm_struct hotkey_driver_data = {
1358 .name = "hotkey",
1359 .read = hotkey_read,
1360 .write = hotkey_write,
1361 .exit = hotkey_exit,
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03001362 .resume = hotkey_resume,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001363 .acpi = &ibm_hotkey_acpidriver,
1364};
1365
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001366/*************************************************************************
1367 * Bluetooth subdriver
1368 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001370/* sysfs bluetooth enable ---------------------------------------------- */
1371static ssize_t bluetooth_enable_show(struct device *dev,
1372 struct device_attribute *attr,
1373 char *buf)
1374{
1375 int status;
1376
1377 status = bluetooth_get_radiosw();
1378 if (status < 0)
1379 return status;
1380
1381 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
1382}
1383
1384static ssize_t bluetooth_enable_store(struct device *dev,
1385 struct device_attribute *attr,
1386 const char *buf, size_t count)
1387{
1388 unsigned long t;
1389 int res;
1390
1391 if (parse_strtoul(buf, 1, &t))
1392 return -EINVAL;
1393
1394 res = bluetooth_set_radiosw(t);
1395
1396 return (res) ? res : count;
1397}
1398
1399static struct device_attribute dev_attr_bluetooth_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001400 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001401 bluetooth_enable_show, bluetooth_enable_store);
1402
1403/* --------------------------------------------------------------------- */
1404
1405static struct attribute *bluetooth_attributes[] = {
1406 &dev_attr_bluetooth_enable.attr,
1407 NULL
1408};
1409
1410static const struct attribute_group bluetooth_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001411 .attrs = bluetooth_attributes,
1412};
1413
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001414static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001416 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001417 int status = 0;
1418
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001419 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
1420
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001421 IBM_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001422
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001423 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1424 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001425 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001426 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001428 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
1429 str_supported(tp_features.bluetooth),
1430 status);
1431
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001432 if (tp_features.bluetooth) {
1433 if (!(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
1434 /* no bluetooth hardware present in system */
1435 tp_features.bluetooth = 0;
1436 dbg_printk(TPACPI_DBG_INIT,
1437 "bluetooth hardware not installed\n");
1438 } else {
1439 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
1440 &bluetooth_attr_group);
1441 if (res)
1442 return res;
1443 }
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001444 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001445
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001446 return (tp_features.bluetooth)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447}
1448
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001449static void bluetooth_exit(void)
1450{
1451 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
1452 &bluetooth_attr_group);
1453}
1454
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001455static int bluetooth_get_radiosw(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456{
1457 int status;
1458
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001459 if (!tp_features.bluetooth)
1460 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001462 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
1463 return -EIO;
1464
1465 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
1466}
1467
1468static int bluetooth_set_radiosw(int radio_on)
1469{
1470 int status;
1471
1472 if (!tp_features.bluetooth)
1473 return -ENODEV;
1474
1475 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
1476 return -EIO;
1477 if (radio_on)
1478 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
1479 else
1480 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
1481 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
1482 return -EIO;
1483
1484 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485}
1486
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001487/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001488static int bluetooth_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489{
1490 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001491 int status = bluetooth_get_radiosw();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001493 if (!tp_features.bluetooth)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 len += sprintf(p + len, "status:\t\tnot supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001496 len += sprintf(p + len, "status:\t\t%s\n",
1497 (status)? "enabled" : "disabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 len += sprintf(p + len, "commands:\tenable, disable\n");
1499 }
1500
1501 return len;
1502}
1503
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001504static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001508 if (!tp_features.bluetooth)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001509 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
1511 while ((cmd = next_cmd(&buf))) {
1512 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001513 bluetooth_set_radiosw(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001515 bluetooth_set_radiosw(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 } else
1517 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 }
1519
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 return 0;
1521}
1522
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001523static struct ibm_struct bluetooth_driver_data = {
1524 .name = "bluetooth",
1525 .read = bluetooth_read,
1526 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001527 .exit = bluetooth_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001528};
1529
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001530/*************************************************************************
1531 * Wan subdriver
1532 */
1533
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001534/* sysfs wan enable ---------------------------------------------------- */
1535static ssize_t wan_enable_show(struct device *dev,
1536 struct device_attribute *attr,
1537 char *buf)
1538{
1539 int status;
1540
1541 status = wan_get_radiosw();
1542 if (status < 0)
1543 return status;
1544
1545 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
1546}
1547
1548static ssize_t wan_enable_store(struct device *dev,
1549 struct device_attribute *attr,
1550 const char *buf, size_t count)
1551{
1552 unsigned long t;
1553 int res;
1554
1555 if (parse_strtoul(buf, 1, &t))
1556 return -EINVAL;
1557
1558 res = wan_set_radiosw(t);
1559
1560 return (res) ? res : count;
1561}
1562
1563static struct device_attribute dev_attr_wan_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001564 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001565 wan_enable_show, wan_enable_store);
1566
1567/* --------------------------------------------------------------------- */
1568
1569static struct attribute *wan_attributes[] = {
1570 &dev_attr_wan_enable.attr,
1571 NULL
1572};
1573
1574static const struct attribute_group wan_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001575 .attrs = wan_attributes,
1576};
1577
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001578static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001579{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001580 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001581 int status = 0;
1582
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001583 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
1584
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001585 IBM_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001586
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001587 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001588 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001589
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001590 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
1591 str_supported(tp_features.wan),
1592 status);
1593
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001594 if (tp_features.wan) {
1595 if (!(status & TP_ACPI_WANCARD_HWPRESENT)) {
1596 /* no wan hardware present in system */
1597 tp_features.wan = 0;
1598 dbg_printk(TPACPI_DBG_INIT,
1599 "wan hardware not installed\n");
1600 } else {
1601 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
1602 &wan_attr_group);
1603 if (res)
1604 return res;
1605 }
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001606 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001607
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001608 return (tp_features.wan)? 0 : 1;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001609}
1610
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001611static void wan_exit(void)
1612{
1613 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
1614 &wan_attr_group);
1615}
1616
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001617static int wan_get_radiosw(void)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001618{
1619 int status;
1620
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001621 if (!tp_features.wan)
1622 return -ENODEV;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001623
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001624 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
1625 return -EIO;
1626
1627 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
1628}
1629
1630static int wan_set_radiosw(int radio_on)
1631{
1632 int status;
1633
1634 if (!tp_features.wan)
1635 return -ENODEV;
1636
1637 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
1638 return -EIO;
1639 if (radio_on)
1640 status |= TP_ACPI_WANCARD_RADIOSSW;
1641 else
1642 status &= ~TP_ACPI_WANCARD_RADIOSSW;
1643 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
1644 return -EIO;
1645
1646 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001647}
1648
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001649/* procfs -------------------------------------------------------------- */
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001650static int wan_read(char *p)
1651{
1652 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001653 int status = wan_get_radiosw();
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001654
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001655 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001656 len += sprintf(p + len, "status:\t\tnot supported\n");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001657 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001658 len += sprintf(p + len, "status:\t\t%s\n",
1659 (status)? "enabled" : "disabled");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001660 len += sprintf(p + len, "commands:\tenable, disable\n");
1661 }
1662
1663 return len;
1664}
1665
1666static int wan_write(char *buf)
1667{
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001668 char *cmd;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001669
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001670 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001671 return -ENODEV;
1672
1673 while ((cmd = next_cmd(&buf))) {
1674 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001675 wan_set_radiosw(1);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001676 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001677 wan_set_radiosw(0);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001678 } else
1679 return -EINVAL;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001680 }
1681
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001682 return 0;
1683}
1684
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001685static struct ibm_struct wan_driver_data = {
1686 .name = "wan",
1687 .read = wan_read,
1688 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001689 .exit = wan_exit,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03001690 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001691};
1692
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001693/*************************************************************************
1694 * Video subdriver
1695 */
1696
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02001697static enum video_access_mode video_supported;
1698static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001699
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001700IBM_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
1701 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
1702 "\\_SB.PCI0.VID0", /* 770e */
1703 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
1704 "\\_SB.PCI0.AGP.VID", /* all others */
1705 ); /* R30, R31 */
1706
1707IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
1708
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001709static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001711 int ivga;
1712
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001713 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
1714
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001715 IBM_ACPIHANDLE_INIT(vid);
1716 IBM_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001717
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001718 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
1719 /* G41, assume IVGA doesn't change */
1720 vid_handle = vid2_handle;
1721
1722 if (!vid_handle)
1723 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001724 video_supported = TPACPI_VIDEO_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001725 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
1726 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001727 video_supported = TPACPI_VIDEO_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001728 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
1729 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001730 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001731 else
1732 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001733 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001735 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
1736 str_supported(video_supported != TPACPI_VIDEO_NONE),
1737 video_supported);
1738
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001739 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740}
1741
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001742static void video_exit(void)
1743{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001744 dbg_printk(TPACPI_DBG_EXIT,
1745 "restoring original video autoswitch mode\n");
1746 if (video_autosw_set(video_orig_autosw))
1747 printk(IBM_ERR "error while trying to restore original "
1748 "video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001749}
1750
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001751static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752{
1753 int status = 0;
1754 int i;
1755
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001756 switch (video_supported) {
1757 case TPACPI_VIDEO_570:
1758 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
1759 TP_ACPI_VIDEO_570_PHSCMD))
1760 return -EIO;
1761 status = i & TP_ACPI_VIDEO_570_PHSMASK;
1762 break;
1763 case TPACPI_VIDEO_770:
1764 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
1765 return -EIO;
1766 if (i)
1767 status |= TP_ACPI_VIDEO_S_LCD;
1768 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
1769 return -EIO;
1770 if (i)
1771 status |= TP_ACPI_VIDEO_S_CRT;
1772 break;
1773 case TPACPI_VIDEO_NEW:
1774 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
1775 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
1776 return -EIO;
1777 if (i)
1778 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001780 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
1781 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
1782 return -EIO;
1783 if (i)
1784 status |= TP_ACPI_VIDEO_S_LCD;
1785 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
1786 return -EIO;
1787 if (i)
1788 status |= TP_ACPI_VIDEO_S_DVI;
1789 break;
1790 default:
1791 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001792 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
1794 return status;
1795}
1796
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001797static int video_outputsw_set(int status)
1798{
1799 int autosw;
1800 int res = 0;
1801
1802 switch (video_supported) {
1803 case TPACPI_VIDEO_570:
1804 res = acpi_evalf(NULL, NULL,
1805 "\\_SB.PHS2", "vdd",
1806 TP_ACPI_VIDEO_570_PHS2CMD,
1807 status | TP_ACPI_VIDEO_570_PHS2SET);
1808 break;
1809 case TPACPI_VIDEO_770:
1810 autosw = video_autosw_get();
1811 if (autosw < 0)
1812 return autosw;
1813
1814 res = video_autosw_set(1);
1815 if (res)
1816 return res;
1817 res = acpi_evalf(vid_handle, NULL,
1818 "ASWT", "vdd", status * 0x100, 0);
1819 if (!autosw && video_autosw_set(autosw)) {
1820 printk(IBM_ERR "video auto-switch left enabled due to error\n");
1821 return -EIO;
1822 }
1823 break;
1824 case TPACPI_VIDEO_NEW:
1825 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
1826 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
1827 break;
1828 default:
1829 return -ENOSYS;
1830 }
1831
1832 return (res)? 0 : -EIO;
1833}
1834
1835static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001837 int autosw = 0;
1838
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001839 switch (video_supported) {
1840 case TPACPI_VIDEO_570:
1841 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
1842 return -EIO;
1843 break;
1844 case TPACPI_VIDEO_770:
1845 case TPACPI_VIDEO_NEW:
1846 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
1847 return -EIO;
1848 break;
1849 default:
1850 return -ENOSYS;
1851 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001852
1853 return autosw & 1;
1854}
1855
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001856static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001857{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001858 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001859 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001860 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001861}
1862
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001863static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001864{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001865 int autosw = video_autosw_get();
1866 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001867
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001868 if (autosw < 0)
1869 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001870
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001871 switch (video_supported) {
1872 case TPACPI_VIDEO_570:
1873 res = video_autosw_set(1);
1874 if (res)
1875 return res;
1876 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
1877 break;
1878 case TPACPI_VIDEO_770:
1879 case TPACPI_VIDEO_NEW:
1880 res = video_autosw_set(1);
1881 if (res)
1882 return res;
1883 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
1884 break;
1885 default:
1886 return -ENOSYS;
1887 }
1888 if (!autosw && video_autosw_set(autosw)) {
1889 printk(IBM_ERR "video auto-switch left enabled due to error\n");
1890 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001891 }
1892
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001893 return (res)? 0 : -EIO;
1894}
1895
1896static int video_expand_toggle(void)
1897{
1898 switch (video_supported) {
1899 case TPACPI_VIDEO_570:
1900 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
1901 0 : -EIO;
1902 case TPACPI_VIDEO_770:
1903 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
1904 0 : -EIO;
1905 case TPACPI_VIDEO_NEW:
1906 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
1907 0 : -EIO;
1908 default:
1909 return -ENOSYS;
1910 }
1911 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001912}
1913
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001914static int video_read(char *p)
1915{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001916 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001917 int len = 0;
1918
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001919 if (video_supported == TPACPI_VIDEO_NONE) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001920 len += sprintf(p + len, "status:\t\tnot supported\n");
1921 return len;
1922 }
1923
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001924 status = video_outputsw_get();
1925 if (status < 0)
1926 return status;
1927
1928 autosw = video_autosw_get();
1929 if (autosw < 0)
1930 return autosw;
1931
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001932 len += sprintf(p + len, "status:\t\tsupported\n");
1933 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
1934 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001935 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001936 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
1937 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
1938 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
1939 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001940 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001941 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
1942 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
1943 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
1944
1945 return len;
1946}
1947
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001948static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949{
1950 char *cmd;
1951 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001952 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001954 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001955 return -ENODEV;
1956
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001957 enable = 0;
1958 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959
1960 while ((cmd = next_cmd(&buf))) {
1961 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001962 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001964 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001966 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001968 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001969 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001970 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001971 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001972 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001973 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001974 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001976 res = video_autosw_set(1);
1977 if (res)
1978 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001980 res = video_autosw_set(0);
1981 if (res)
1982 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001984 res = video_outputsw_cycle();
1985 if (res)
1986 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001988 res = video_expand_toggle();
1989 if (res)
1990 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 } else
1992 return -EINVAL;
1993 }
1994
1995 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001996 status = video_outputsw_get();
1997 if (status < 0)
1998 return status;
1999 res = video_outputsw_set((status & ~disable) | enable);
2000 if (res)
2001 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 }
2003
2004 return 0;
2005}
2006
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002007static struct ibm_struct video_driver_data = {
2008 .name = "video",
2009 .read = video_read,
2010 .write = video_write,
2011 .exit = video_exit,
2012};
2013
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002014/*************************************************************************
2015 * Light (thinklight) subdriver
2016 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002018IBM_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
2019IBM_HANDLE(ledb, ec, "LEDB"); /* G4x */
2020
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002021static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002023 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
2024
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002025 IBM_ACPIHANDLE_INIT(ledb);
2026 IBM_ACPIHANDLE_INIT(lght);
2027 IBM_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002028
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002029 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002030 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002031
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002032 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002033 /* light status not supported on
2034 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002035 tp_features.light_status =
2036 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002038 vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002039 str_supported(tp_features.light));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002040
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002041 return (tp_features.light)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042}
2043
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002044static int light_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045{
2046 int len = 0;
2047 int status = 0;
2048
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002049 if (!tp_features.light) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002050 len += sprintf(p + len, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002051 } else if (!tp_features.light_status) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002052 len += sprintf(p + len, "status:\t\tunknown\n");
2053 len += sprintf(p + len, "commands:\ton, off\n");
2054 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
2056 return -EIO;
2057 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002058 len += sprintf(p + len, "commands:\ton, off\n");
2059 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060
2061 return len;
2062}
2063
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002064static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065{
2066 int cmos_cmd, lght_cmd;
2067 char *cmd;
2068 int success;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002069
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002070 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002071 return -ENODEV;
2072
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 while ((cmd = next_cmd(&buf))) {
2074 if (strlencmp(cmd, "on") == 0) {
2075 cmos_cmd = 0x0c;
2076 lght_cmd = 1;
2077 } else if (strlencmp(cmd, "off") == 0) {
2078 cmos_cmd = 0x0d;
2079 lght_cmd = 0;
2080 } else
2081 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002082
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 success = cmos_handle ?
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002084 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
2085 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 if (!success)
2087 return -EIO;
2088 }
2089
2090 return 0;
2091}
2092
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002093static struct ibm_struct light_driver_data = {
2094 .name = "light",
2095 .read = light_read,
2096 .write = light_write,
2097};
2098
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002099/*************************************************************************
2100 * Dock subdriver
2101 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03002103#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002104
2105IBM_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
2106 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
2107 "\\_SB.PCI0.PCI1.DOCK", /* all others */
2108 "\\_SB.PCI.ISA.SLCE", /* 570 */
2109 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
2110
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002111/* don't list other alternatives as we install a notify handler on the 570 */
2112IBM_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
2113
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002114static const struct acpi_device_id ibm_pci_device_ids[] = {
2115 {PCI_ROOT_HID_STRING, 0},
2116 {"", 0},
2117};
2118
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03002119static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
2120 {
2121 .notify = dock_notify,
2122 .handle = &dock_handle,
2123 .type = ACPI_SYSTEM_NOTIFY,
2124 },
2125 {
Henrique de Moraes Holschuh996fba02007-07-18 23:45:40 -03002126 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
2127 * We just use it to get notifications of dock hotplug
2128 * in very old thinkpads */
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002129 .hid = ibm_pci_device_ids,
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03002130 .notify = dock_notify,
2131 .handle = &pci_handle,
2132 .type = ACPI_SYSTEM_NOTIFY,
2133 },
2134};
2135
2136static struct ibm_struct dock_driver_data[2] = {
2137 {
2138 .name = "dock",
2139 .read = dock_read,
2140 .write = dock_write,
2141 .acpi = &ibm_dock_acpidriver[0],
2142 },
2143 {
2144 .name = "dock",
2145 .acpi = &ibm_dock_acpidriver[1],
2146 },
2147};
2148
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149#define dock_docked() (_sta(dock_handle) & 1)
2150
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002151static int __init dock_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002152{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002153 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
2154
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002155 IBM_ACPIHANDLE_INIT(dock);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002156
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002157 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
2158 str_supported(dock_handle != NULL));
2159
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002160 return (dock_handle)? 0 : 1;
2161}
2162
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03002163static int __init dock_init2(struct ibm_init_struct *iibm)
2164{
2165 int dock2_needed;
2166
2167 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
2168
2169 if (dock_driver_data[0].flags.acpi_driver_registered &&
2170 dock_driver_data[0].flags.acpi_notify_installed) {
2171 IBM_ACPIHANDLE_INIT(pci);
2172 dock2_needed = (pci_handle != NULL);
2173 vdbg_printk(TPACPI_DBG_INIT,
2174 "dock PCI handler for the TP 570 is %s\n",
2175 str_supported(dock2_needed));
2176 } else {
2177 vdbg_printk(TPACPI_DBG_INIT,
2178 "dock subdriver part 2 not required\n");
2179 dock2_needed = 0;
2180 }
2181
2182 return (dock2_needed)? 0 : 1;
2183}
2184
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002185static void dock_notify(struct ibm_struct *ibm, u32 event)
2186{
2187 int docked = dock_docked();
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002188 int pci = ibm->acpi->hid && ibm->acpi->device &&
2189 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
Zhang Rui962ce8c2007-08-23 01:24:31 +08002190 int data;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002191
2192 if (event == 1 && !pci) /* 570 */
Zhang Rui962ce8c2007-08-23 01:24:31 +08002193 data = 1; /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002194 else if (event == 1 && pci) /* 570 */
Zhang Rui962ce8c2007-08-23 01:24:31 +08002195 data = 3; /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002196 else if (event == 3 && docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08002197 data = 1; /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002198 else if (event == 3 && !docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08002199 data = 2; /* undock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002200 else if (event == 0 && docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08002201 data = 3; /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002202 else {
2203 printk(IBM_ERR "unknown dock event %d, status %d\n",
2204 event, _sta(dock_handle));
Zhang Rui962ce8c2007-08-23 01:24:31 +08002205 data = 0; /* unknown */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002206 }
Len Brown14e04fb2007-08-23 15:20:26 -04002207 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
Zhang Rui962ce8c2007-08-23 01:24:31 +08002208 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
2209 ibm->acpi->device->dev.bus_id,
2210 event, data);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002211}
2212
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002213static int dock_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214{
2215 int len = 0;
2216 int docked = dock_docked();
2217
2218 if (!dock_handle)
2219 len += sprintf(p + len, "status:\t\tnot supported\n");
2220 else if (!docked)
2221 len += sprintf(p + len, "status:\t\tundocked\n");
2222 else {
2223 len += sprintf(p + len, "status:\t\tdocked\n");
2224 len += sprintf(p + len, "commands:\tdock, undock\n");
2225 }
2226
2227 return len;
2228}
2229
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002230static int dock_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231{
2232 char *cmd;
2233
2234 if (!dock_docked())
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002235 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236
2237 while ((cmd = next_cmd(&buf))) {
2238 if (strlencmp(cmd, "undock") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002239 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
2240 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 return -EIO;
2242 } else if (strlencmp(cmd, "dock") == 0) {
2243 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
2244 return -EIO;
2245 } else
2246 return -EINVAL;
2247 }
2248
2249 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002250}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03002252#endif /* CONFIG_THINKPAD_ACPI_DOCK */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002254/*************************************************************************
2255 * Bay subdriver
2256 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03002258#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002259IBM_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
2260 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
2261 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
2262 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
2263 ); /* A21e, R30, R31 */
2264IBM_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
2265 "_EJ0", /* all others */
2266 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
2267IBM_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
2268 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
2269 ); /* all others */
2270IBM_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
2271 "_EJ0", /* 770x */
2272 ); /* all others */
2273
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002274static int __init bay_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002276 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
2277
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002278 IBM_ACPIHANDLE_INIT(bay);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002279 if (bay_handle)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002280 IBM_ACPIHANDLE_INIT(bay_ej);
2281 IBM_ACPIHANDLE_INIT(bay2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002282 if (bay2_handle)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002283 IBM_ACPIHANDLE_INIT(bay2_ej);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002284
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002285 tp_features.bay_status = bay_handle &&
2286 acpi_evalf(bay_handle, NULL, "_STA", "qv");
2287 tp_features.bay_status2 = bay2_handle &&
2288 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002289
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002290 tp_features.bay_eject = bay_handle && bay_ej_handle &&
2291 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
2292 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
2293 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002295 vdbg_printk(TPACPI_DBG_INIT,
2296 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002297 str_supported(tp_features.bay_status),
2298 str_supported(tp_features.bay_eject),
2299 str_supported(tp_features.bay_status2),
2300 str_supported(tp_features.bay_eject2));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002301
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002302 return (tp_features.bay_status || tp_features.bay_eject ||
2303 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304}
2305
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002306static void bay_notify(struct ibm_struct *ibm, u32 event)
2307{
Len Brown14e04fb2007-08-23 15:20:26 -04002308 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
Zhang Rui962ce8c2007-08-23 01:24:31 +08002309 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
2310 ibm->acpi->device->dev.bus_id,
2311 event, 0);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002312}
2313
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002314#define bay_occupied(b) (_sta(b##_handle) & 1)
2315
2316static int bay_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317{
2318 int len = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002319 int occupied = bay_occupied(bay);
2320 int occupied2 = bay_occupied(bay2);
2321 int eject, eject2;
2322
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002323 len += sprintf(p + len, "status:\t\t%s\n",
2324 tp_features.bay_status ?
2325 (occupied ? "occupied" : "unoccupied") :
2326 "not supported");
2327 if (tp_features.bay_status2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002328 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
2329 "occupied" : "unoccupied");
2330
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002331 eject = tp_features.bay_eject && occupied;
2332 eject2 = tp_features.bay_eject2 && occupied2;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002333
2334 if (eject && eject2)
2335 len += sprintf(p + len, "commands:\teject, eject2\n");
2336 else if (eject)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 len += sprintf(p + len, "commands:\teject\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002338 else if (eject2)
2339 len += sprintf(p + len, "commands:\teject2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340
2341 return len;
2342}
2343
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002344static int bay_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345{
2346 char *cmd;
2347
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002348 if (!tp_features.bay_eject && !tp_features.bay_eject2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002349 return -ENODEV;
2350
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002352 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002353 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
2354 return -EIO;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002355 } else if (tp_features.bay_eject2 &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002356 strlencmp(cmd, "eject2") == 0) {
2357 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 return -EIO;
2359 } else
2360 return -EINVAL;
2361 }
2362
2363 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002364}
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002365
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002366static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
2367 .notify = bay_notify,
2368 .handle = &bay_handle,
2369 .type = ACPI_SYSTEM_NOTIFY,
2370};
2371
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002372static struct ibm_struct bay_driver_data = {
2373 .name = "bay",
2374 .read = bay_read,
2375 .write = bay_write,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002376 .acpi = &ibm_bay_acpidriver,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002377};
2378
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03002379#endif /* CONFIG_THINKPAD_ACPI_BAY */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002381/*************************************************************************
2382 * CMOS subdriver
2383 */
2384
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03002385/* sysfs cmos_command -------------------------------------------------- */
2386static ssize_t cmos_command_store(struct device *dev,
2387 struct device_attribute *attr,
2388 const char *buf, size_t count)
2389{
2390 unsigned long cmos_cmd;
2391 int res;
2392
2393 if (parse_strtoul(buf, 21, &cmos_cmd))
2394 return -EINVAL;
2395
2396 res = issue_thinkpad_cmos_command(cmos_cmd);
2397 return (res)? res : count;
2398}
2399
2400static struct device_attribute dev_attr_cmos_command =
2401 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
2402
2403/* --------------------------------------------------------------------- */
2404
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002405static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002406{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03002407 int res;
2408
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002409 vdbg_printk(TPACPI_DBG_INIT,
2410 "initializing cmos commands subdriver\n");
2411
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002412 IBM_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002413
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002414 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
2415 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03002416
2417 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
2418 if (res)
2419 return res;
2420
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002421 return (cmos_handle)? 0 : 1;
2422}
2423
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03002424static void cmos_exit(void)
2425{
2426 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
2427}
2428
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002429static int cmos_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430{
2431 int len = 0;
2432
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002433 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2434 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 if (!cmos_handle)
2436 len += sprintf(p + len, "status:\t\tnot supported\n");
2437 else {
2438 len += sprintf(p + len, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002439 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 }
2441
2442 return len;
2443}
2444
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002445static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446{
2447 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03002448 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449
2450 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002451 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
2452 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 /* cmos_cmd set */
2454 } else
2455 return -EINVAL;
2456
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03002457 res = issue_thinkpad_cmos_command(cmos_cmd);
2458 if (res)
2459 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 }
2461
2462 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002463}
2464
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002465static struct ibm_struct cmos_driver_data = {
2466 .name = "cmos",
2467 .read = cmos_read,
2468 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03002469 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002470};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002471
2472/*************************************************************************
2473 * LED subdriver
2474 */
2475
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02002476static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002477
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002478IBM_HANDLE(led, ec, "SLED", /* 570 */
2479 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
2480 "LED", /* all others */
2481 ); /* R30, R31 */
2482
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002483static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002484{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002485 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
2486
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002487 IBM_ACPIHANDLE_INIT(led);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002488
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002489 if (!led_handle)
2490 /* led not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002491 led_supported = TPACPI_LED_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002492 else if (strlencmp(led_path, "SLED") == 0)
2493 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002494 led_supported = TPACPI_LED_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002495 else if (strlencmp(led_path, "SYSL") == 0)
2496 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002497 led_supported = TPACPI_LED_OLD;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002498 else
2499 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002500 led_supported = TPACPI_LED_NEW;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002501
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002502 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
2503 str_supported(led_supported), led_supported);
2504
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002505 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002506}
2507
2508#define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
2509
2510static int led_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511{
2512 int len = 0;
2513
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002514 if (!led_supported) {
2515 len += sprintf(p + len, "status:\t\tnot supported\n");
2516 return len;
2517 }
2518 len += sprintf(p + len, "status:\t\tsupported\n");
2519
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002520 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002521 /* 570 */
2522 int i, status;
2523 for (i = 0; i < 8; i++) {
2524 if (!acpi_evalf(ec_handle,
2525 &status, "GLED", "dd", 1 << i))
2526 return -EIO;
2527 len += sprintf(p + len, "%d:\t\t%s\n",
2528 i, led_status(status));
2529 }
2530 }
2531
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 len += sprintf(p + len, "commands:\t"
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002533 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534
2535 return len;
2536}
2537
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002538/* off, on, blink */
2539static const int led_sled_arg1[] = { 0, 1, 3 };
2540static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
2541static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
2542static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
2543
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002544static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545{
2546 char *cmd;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002547 int led, ind, ret;
2548
2549 if (!led_supported)
2550 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551
2552 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002553 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 return -EINVAL;
2555
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002556 if (strstr(cmd, "off")) {
2557 ind = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 } else if (strstr(cmd, "on")) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002559 ind = 1;
2560 } else if (strstr(cmd, "blink")) {
2561 ind = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 } else
2563 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002564
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002565 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002566 /* 570 */
2567 led = 1 << led;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002569 led, led_sled_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 return -EIO;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002571 } else if (led_supported == TPACPI_LED_OLD) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002572 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
2573 led = 1 << led;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002574 ret = ec_write(TPACPI_LED_EC_HLMS, led);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002575 if (ret >= 0)
2576 ret =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002577 ec_write(TPACPI_LED_EC_HLBL,
Henrique de Moraes Holschuhe062e032007-03-23 17:33:55 -03002578 led * led_exp_hlbl[ind]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002579 if (ret >= 0)
2580 ret =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002581 ec_write(TPACPI_LED_EC_HLCL,
Henrique de Moraes Holschuhe062e032007-03-23 17:33:55 -03002582 led * led_exp_hlcl[ind]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002583 if (ret < 0)
2584 return ret;
2585 } else {
2586 /* all others */
2587 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
2588 led, led_led_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002590 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 }
2592
2593 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002594}
2595
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002596static struct ibm_struct led_driver_data = {
2597 .name = "led",
2598 .read = led_read,
2599 .write = led_write,
2600};
2601
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002602/*************************************************************************
2603 * Beep subdriver
2604 */
2605
2606IBM_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
2607
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002608static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002609{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002610 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
2611
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002612 IBM_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002613
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002614 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
2615 str_supported(beep_handle != NULL));
2616
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002617 return (beep_handle)? 0 : 1;
2618}
2619
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002620static int beep_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621{
2622 int len = 0;
2623
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002624 if (!beep_handle)
2625 len += sprintf(p + len, "status:\t\tnot supported\n");
2626 else {
2627 len += sprintf(p + len, "status:\t\tsupported\n");
2628 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
2629 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630
2631 return len;
2632}
2633
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002634static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635{
2636 char *cmd;
2637 int beep_cmd;
2638
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002639 if (!beep_handle)
2640 return -ENODEV;
2641
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002643 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
2644 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 /* beep_cmd set */
2646 } else
2647 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002648 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 return -EIO;
2650 }
2651
2652 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002653}
2654
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002655static struct ibm_struct beep_driver_data = {
2656 .name = "beep",
2657 .read = beep_read,
2658 .write = beep_write,
2659};
2660
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002661/*************************************************************************
2662 * Thermal subdriver
2663 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002664
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002665static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002666
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03002667/* sysfs temp##_input -------------------------------------------------- */
2668
2669static ssize_t thermal_temp_input_show(struct device *dev,
2670 struct device_attribute *attr,
2671 char *buf)
2672{
2673 struct sensor_device_attribute *sensor_attr =
2674 to_sensor_dev_attr(attr);
2675 int idx = sensor_attr->index;
2676 s32 value;
2677 int res;
2678
2679 res = thermal_get_sensor(idx, &value);
2680 if (res)
2681 return res;
2682 if (value == TP_EC_THERMAL_TMP_NA * 1000)
2683 return -ENXIO;
2684
2685 return snprintf(buf, PAGE_SIZE, "%d\n", value);
2686}
2687
2688#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
2689 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, thermal_temp_input_show, NULL, _idxB)
2690
2691static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
2692 THERMAL_SENSOR_ATTR_TEMP(1, 0),
2693 THERMAL_SENSOR_ATTR_TEMP(2, 1),
2694 THERMAL_SENSOR_ATTR_TEMP(3, 2),
2695 THERMAL_SENSOR_ATTR_TEMP(4, 3),
2696 THERMAL_SENSOR_ATTR_TEMP(5, 4),
2697 THERMAL_SENSOR_ATTR_TEMP(6, 5),
2698 THERMAL_SENSOR_ATTR_TEMP(7, 6),
2699 THERMAL_SENSOR_ATTR_TEMP(8, 7),
2700 THERMAL_SENSOR_ATTR_TEMP(9, 8),
2701 THERMAL_SENSOR_ATTR_TEMP(10, 9),
2702 THERMAL_SENSOR_ATTR_TEMP(11, 10),
2703 THERMAL_SENSOR_ATTR_TEMP(12, 11),
2704 THERMAL_SENSOR_ATTR_TEMP(13, 12),
2705 THERMAL_SENSOR_ATTR_TEMP(14, 13),
2706 THERMAL_SENSOR_ATTR_TEMP(15, 14),
2707 THERMAL_SENSOR_ATTR_TEMP(16, 15),
2708};
2709
2710#define THERMAL_ATTRS(X) \
2711 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
2712
2713static struct attribute *thermal_temp_input_attr[] = {
2714 THERMAL_ATTRS(8),
2715 THERMAL_ATTRS(9),
2716 THERMAL_ATTRS(10),
2717 THERMAL_ATTRS(11),
2718 THERMAL_ATTRS(12),
2719 THERMAL_ATTRS(13),
2720 THERMAL_ATTRS(14),
2721 THERMAL_ATTRS(15),
2722 THERMAL_ATTRS(0),
2723 THERMAL_ATTRS(1),
2724 THERMAL_ATTRS(2),
2725 THERMAL_ATTRS(3),
2726 THERMAL_ATTRS(4),
2727 THERMAL_ATTRS(5),
2728 THERMAL_ATTRS(6),
2729 THERMAL_ATTRS(7),
2730 NULL
2731};
2732
2733static const struct attribute_group thermal_temp_input16_group = {
2734 .attrs = thermal_temp_input_attr
2735};
2736
2737static const struct attribute_group thermal_temp_input8_group = {
2738 .attrs = &thermal_temp_input_attr[8]
2739};
2740
2741#undef THERMAL_SENSOR_ATTR_TEMP
2742#undef THERMAL_ATTRS
2743
2744/* --------------------------------------------------------------------- */
2745
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002746static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002747{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002748 u8 t, ta1, ta2;
2749 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002750 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03002751 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002752
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002753 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
2754
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002755 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002756
Henrique de Moraes Holschuh3d6f99c2007-07-18 23:45:46 -03002757 if (thinkpad_id.ec_model) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002758 /*
2759 * Direct EC access mode: sensors at registers
2760 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
2761 * non-implemented, thermal sensors return 0x80 when
2762 * not available
2763 */
2764
2765 ta1 = ta2 = 0;
2766 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002767 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002768 ta1 |= t;
2769 } else {
2770 ta1 = 0;
2771 break;
2772 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002773 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002774 ta2 |= t;
2775 } else {
2776 ta1 = 0;
2777 break;
2778 }
2779 }
2780 if (ta1 == 0) {
2781 /* This is sheer paranoia, but we handle it anyway */
2782 if (acpi_tmp7) {
2783 printk(IBM_ERR
2784 "ThinkPad ACPI EC access misbehaving, "
2785 "falling back to ACPI TMPx access mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002786 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002787 } else {
2788 printk(IBM_ERR
2789 "ThinkPad ACPI EC access misbehaving, "
2790 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002791 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002792 }
2793 } else {
2794 thermal_read_mode =
2795 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002796 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002797 }
2798 } else if (acpi_tmp7) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002799 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
2800 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002801 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002802 } else {
2803 /* Standard ACPI TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002804 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002805 }
2806 } else {
2807 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002808 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002809 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002810
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002811 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
2812 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
2813 thermal_read_mode);
2814
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03002815 switch(thermal_read_mode) {
2816 case TPACPI_THERMAL_TPEC_16:
2817 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2818 &thermal_temp_input16_group);
2819 if (res)
2820 return res;
2821 break;
2822 case TPACPI_THERMAL_TPEC_8:
2823 case TPACPI_THERMAL_ACPI_TMP07:
2824 case TPACPI_THERMAL_ACPI_UPDT:
2825 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2826 &thermal_temp_input8_group);
2827 if (res)
2828 return res;
2829 break;
2830 case TPACPI_THERMAL_NONE:
2831 default:
2832 return 1;
2833 }
2834
2835 return 0;
2836}
2837
2838static void thermal_exit(void)
2839{
2840 switch(thermal_read_mode) {
2841 case TPACPI_THERMAL_TPEC_16:
2842 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2843 &thermal_temp_input16_group);
2844 break;
2845 case TPACPI_THERMAL_TPEC_8:
2846 case TPACPI_THERMAL_ACPI_TMP07:
2847 case TPACPI_THERMAL_ACPI_UPDT:
2848 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2849 &thermal_temp_input16_group);
2850 break;
2851 case TPACPI_THERMAL_NONE:
2852 default:
2853 break;
2854 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002855}
2856
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002857/* idx is zero-based */
2858static int thermal_get_sensor(int idx, s32 *value)
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002859{
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002860 int t;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002861 s8 tmp;
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002862 char tmpi[5];
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002863
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002864 t = TP_EC_THERMAL_TMP0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002865
2866 switch (thermal_read_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002867#if TPACPI_MAX_THERMAL_SENSORS >= 16
2868 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002869 if (idx >= 8 && idx <= 15) {
2870 t = TP_EC_THERMAL_TMP8;
2871 idx -= 8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002872 }
2873 /* fallthrough */
2874#endif
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002875 case TPACPI_THERMAL_TPEC_8:
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002876 if (idx <= 7) {
2877 if (!acpi_ec_read(t + idx, &tmp))
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002878 return -EIO;
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002879 *value = tmp * 1000;
2880 return 0;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002881 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002882 break;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002883
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002884 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002885 if (idx <= 7) {
2886 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
2887 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
2888 return -EIO;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002889 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
2890 return -EIO;
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002891 *value = (t - 2732) * 100;
2892 return 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002893 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002894 break;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002895
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002896 case TPACPI_THERMAL_ACPI_TMP07:
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002897 if (idx <= 7) {
2898 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002899 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
2900 return -EIO;
Henrique de Moraes Holschuha8fba3da2007-07-18 23:45:45 -03002901 if (t > 127 || t < -127)
2902 t = TP_EC_THERMAL_TMP_NA;
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002903 *value = t * 1000;
2904 return 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002905 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002906 break;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002907
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002908 case TPACPI_THERMAL_NONE:
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002909 default:
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002910 return -ENOSYS;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002911 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002912
2913 return -EINVAL;
2914}
2915
2916static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
2917{
2918 int res, i;
2919 int n;
2920
2921 n = 8;
2922 i = 0;
2923
2924 if (!s)
2925 return -EINVAL;
2926
2927 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
2928 n = 16;
2929
2930 for(i = 0 ; i < n; i++) {
2931 res = thermal_get_sensor(i, &s->temp[i]);
2932 if (res)
2933 return res;
2934 }
2935
2936 return n;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002937}
2938
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002939static int thermal_read(char *p)
2940{
2941 int len = 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002942 int n, i;
2943 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002944
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002945 n = thermal_get_sensors(&t);
2946 if (unlikely(n < 0))
2947 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002948
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002949 len += sprintf(p + len, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002950
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002951 if (n > 0) {
2952 for (i = 0; i < (n - 1); i++)
2953 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
2954 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
2955 } else
2956 len += sprintf(p + len, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002957
2958 return len;
2959}
2960
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002961static struct ibm_struct thermal_driver_data = {
2962 .name = "thermal",
2963 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03002964 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002965};
2966
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002967/*************************************************************************
2968 * EC Dump subdriver
2969 */
2970
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002971static u8 ecdump_regs[256];
2972
2973static int ecdump_read(char *p)
2974{
2975 int len = 0;
2976 int i, j;
2977 u8 v;
2978
2979 len += sprintf(p + len, "EC "
2980 " +00 +01 +02 +03 +04 +05 +06 +07"
2981 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
2982 for (i = 0; i < 256; i += 16) {
2983 len += sprintf(p + len, "EC 0x%02x:", i);
2984 for (j = 0; j < 16; j++) {
2985 if (!acpi_ec_read(i + j, &v))
2986 break;
2987 if (v != ecdump_regs[i + j])
2988 len += sprintf(p + len, " *%02x", v);
2989 else
2990 len += sprintf(p + len, " %02x", v);
2991 ecdump_regs[i + j] = v;
2992 }
2993 len += sprintf(p + len, "\n");
2994 if (j != 16)
2995 break;
2996 }
2997
2998 /* These are way too dangerous to advertise openly... */
2999#if 0
3000 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
3001 " (<offset> is 00-ff, <value> is 00-ff)\n");
3002 len += sprintf(p + len, "commands:\t0x<offset> <value> "
3003 " (<offset> is 00-ff, <value> is 0-255)\n");
3004#endif
3005 return len;
3006}
3007
3008static int ecdump_write(char *buf)
3009{
3010 char *cmd;
3011 int i, v;
3012
3013 while ((cmd = next_cmd(&buf))) {
3014 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
3015 /* i and v set */
3016 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
3017 /* i and v set */
3018 } else
3019 return -EINVAL;
3020 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
3021 if (!acpi_ec_write(i, v))
3022 return -EIO;
3023 } else
3024 return -EINVAL;
3025 }
3026
3027 return 0;
3028}
3029
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003030static struct ibm_struct ecdump_driver_data = {
3031 .name = "ecdump",
3032 .read = ecdump_read,
3033 .write = ecdump_write,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03003034 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003035};
3036
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003037/*************************************************************************
3038 * Backlight/brightness subdriver
3039 */
Holger Macht8acb0252006-10-20 14:30:28 -07003040
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03003041static struct backlight_device *ibm_backlight_device;
Holger Macht8acb0252006-10-20 14:30:28 -07003042
Richard Purdie599a52d2007-02-10 23:07:48 +00003043static struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02003044 .get_brightness = brightness_get,
3045 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02003046};
3047
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03003048static struct mutex brightness_mutex;
3049
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003050static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02003051{
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02003052 int b;
3053
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003054 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
3055
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03003056 mutex_init(&brightness_mutex);
3057
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03003058 if (!brightness_mode) {
3059 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
3060 brightness_mode = 2;
3061 else
3062 brightness_mode = 3;
3063
3064 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
3065 brightness_mode);
3066 }
3067
3068 if (brightness_mode > 3)
3069 return -EINVAL;
3070
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02003071 b = brightness_get(NULL);
3072 if (b < 0)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03003073 return 1;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02003074
Henrique de Moraes Holschuh7d5a0152007-04-24 11:48:20 -03003075 ibm_backlight_device = backlight_device_register(
3076 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
3077 &ibm_backlight_data);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02003078 if (IS_ERR(ibm_backlight_device)) {
3079 printk(IBM_ERR "Could not register backlight device\n");
3080 return PTR_ERR(ibm_backlight_device);
3081 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003082 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02003083
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02003084 ibm_backlight_device->props.max_brightness = 7;
3085 ibm_backlight_device->props.brightness = b;
3086 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00003087
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02003088 return 0;
3089}
3090
3091static void brightness_exit(void)
3092{
3093 if (ibm_backlight_device) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003094 vdbg_printk(TPACPI_DBG_EXIT,
3095 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02003096 backlight_device_unregister(ibm_backlight_device);
3097 ibm_backlight_device = NULL;
3098 }
3099}
3100
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003101static int brightness_update_status(struct backlight_device *bd)
3102{
3103 return brightness_set(
3104 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
3105 bd->props.power == FB_BLANK_UNBLANK) ?
3106 bd->props.brightness : 0);
3107}
3108
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03003109/*
3110 * ThinkPads can read brightness from two places: EC 0x31, or
3111 * CMOS NVRAM byte 0x5E, bits 0-3.
3112 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003113static int brightness_get(struct backlight_device *bd)
3114{
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03003115 u8 lec = 0, lcmos = 0, level = 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003116
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03003117 if (brightness_mode & 1) {
3118 if (!acpi_ec_read(brightness_offset, &lec))
3119 return -EIO;
3120 lec &= 7;
3121 level = lec;
3122 };
3123 if (brightness_mode & 2) {
3124 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
3125 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
3126 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
3127 level = lcmos;
3128 }
3129
3130 if (brightness_mode == 3 && lec != lcmos) {
3131 printk(IBM_ERR
3132 "CMOS NVRAM (%u) and EC (%u) do not agree "
3133 "on display brightness level\n",
3134 (unsigned int) lcmos,
3135 (unsigned int) lec);
3136 return -EIO;
3137 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003138
3139 return level;
3140}
3141
3142static int brightness_set(int value)
3143{
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03003144 int cmos_cmd, inc, i, res;
3145 int current_value;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003146
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03003147 if (value > 7)
3148 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003149
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03003150 res = mutex_lock_interruptible(&brightness_mutex);
3151 if (res < 0)
3152 return res;
3153
3154 current_value = brightness_get(NULL);
3155 if (current_value < 0) {
3156 res = current_value;
3157 goto errout;
3158 }
3159
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03003160 cmos_cmd = value > current_value ?
3161 TP_CMOS_BRIGHTNESS_UP :
3162 TP_CMOS_BRIGHTNESS_DOWN;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003163 inc = value > current_value ? 1 : -1;
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03003164
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03003165 res = 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003166 for (i = current_value; i != value; i += inc) {
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03003167 if ((brightness_mode & 2) &&
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03003168 issue_thinkpad_cmos_command(cmos_cmd)) {
3169 res = -EIO;
3170 goto errout;
3171 }
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03003172 if ((brightness_mode & 1) &&
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03003173 !acpi_ec_write(brightness_offset, i + inc)) {
3174 res = -EIO;
3175 goto errout;;
3176 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003177 }
3178
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03003179errout:
3180 mutex_unlock(&brightness_mutex);
3181 return res;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003182}
3183
3184static int brightness_read(char *p)
3185{
3186 int len = 0;
3187 int level;
3188
3189 if ((level = brightness_get(NULL)) < 0) {
3190 len += sprintf(p + len, "level:\t\tunreadable\n");
3191 } else {
3192 len += sprintf(p + len, "level:\t\t%d\n", level & 0x7);
3193 len += sprintf(p + len, "commands:\tup, down\n");
3194 len += sprintf(p + len, "commands:\tlevel <level>"
3195 " (<level> is 0-7)\n");
3196 }
3197
3198 return len;
3199}
3200
3201static int brightness_write(char *buf)
3202{
3203 int level;
3204 int new_level;
3205 char *cmd;
3206
3207 while ((cmd = next_cmd(&buf))) {
3208 if ((level = brightness_get(NULL)) < 0)
3209 return level;
3210 level &= 7;
3211
3212 if (strlencmp(cmd, "up") == 0) {
3213 new_level = level == 7 ? 7 : level + 1;
3214 } else if (strlencmp(cmd, "down") == 0) {
3215 new_level = level == 0 ? 0 : level - 1;
3216 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
3217 new_level >= 0 && new_level <= 7) {
3218 /* new_level set */
3219 } else
3220 return -EINVAL;
3221
3222 brightness_set(new_level);
3223 }
3224
3225 return 0;
3226}
3227
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003228static struct ibm_struct brightness_driver_data = {
3229 .name = "brightness",
3230 .read = brightness_read,
3231 .write = brightness_write,
3232 .exit = brightness_exit,
3233};
3234
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003235/*************************************************************************
3236 * Volume subdriver
3237 */
3238
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003239static int volume_read(char *p)
3240{
3241 int len = 0;
3242 u8 level;
3243
3244 if (!acpi_ec_read(volume_offset, &level)) {
3245 len += sprintf(p + len, "level:\t\tunreadable\n");
3246 } else {
3247 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
3248 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
3249 len += sprintf(p + len, "commands:\tup, down, mute\n");
3250 len += sprintf(p + len, "commands:\tlevel <level>"
3251 " (<level> is 0-15)\n");
3252 }
3253
3254 return len;
3255}
3256
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003257static int volume_write(char *buf)
3258{
3259 int cmos_cmd, inc, i;
3260 u8 level, mute;
3261 int new_level, new_mute;
3262 char *cmd;
3263
3264 while ((cmd = next_cmd(&buf))) {
3265 if (!acpi_ec_read(volume_offset, &level))
3266 return -EIO;
3267 new_mute = mute = level & 0x40;
3268 new_level = level = level & 0xf;
3269
3270 if (strlencmp(cmd, "up") == 0) {
3271 if (mute)
3272 new_mute = 0;
3273 else
3274 new_level = level == 15 ? 15 : level + 1;
3275 } else if (strlencmp(cmd, "down") == 0) {
3276 if (mute)
3277 new_mute = 0;
3278 else
3279 new_level = level == 0 ? 0 : level - 1;
3280 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
3281 new_level >= 0 && new_level <= 15) {
3282 /* new_level set */
3283 } else if (strlencmp(cmd, "mute") == 0) {
3284 new_mute = 0x40;
3285 } else
3286 return -EINVAL;
3287
3288 if (new_level != level) { /* mute doesn't change */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003289 cmos_cmd = new_level > level ? TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003290 inc = new_level > level ? 1 : -1;
3291
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03003292 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003293 !acpi_ec_write(volume_offset, level)))
3294 return -EIO;
3295
3296 for (i = level; i != new_level; i += inc)
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03003297 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003298 !acpi_ec_write(volume_offset, i + inc))
3299 return -EIO;
3300
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03003301 if (mute && (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003302 !acpi_ec_write(volume_offset,
3303 new_level + mute)))
3304 return -EIO;
3305 }
3306
3307 if (new_mute != mute) { /* level doesn't change */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003308 cmos_cmd = new_mute ? TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003309
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03003310 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003311 !acpi_ec_write(volume_offset, level + new_mute))
3312 return -EIO;
3313 }
3314 }
3315
3316 return 0;
3317}
3318
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003319static struct ibm_struct volume_driver_data = {
3320 .name = "volume",
3321 .read = volume_read,
3322 .write = volume_write,
3323};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003324
3325/*************************************************************************
3326 * Fan subdriver
3327 */
3328
3329/*
3330 * FAN ACCESS MODES
3331 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003332 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003333 * ACPI GFAN method: returns fan level
3334 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003335 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03003336 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003337 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003338 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003339 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
3340 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03003341 * EC 0x2f (HFSP) might be available *for reading*, but do not use
3342 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003343 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003344 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03003345 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
3346 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003347 *
3348 * Fan speed changes of any sort (including those caused by the
3349 * disengaged mode) are usually done slowly by the firmware as the
3350 * maximum ammount of fan duty cycle change per second seems to be
3351 * limited.
3352 *
3353 * Reading is not available if GFAN exists.
3354 * Writing is not available if SFAN exists.
3355 *
3356 * Bits
3357 * 7 automatic mode engaged;
3358 * (default operation mode of the ThinkPad)
3359 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03003360 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003361 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03003362 * the tachometer while the fan controller ramps up
3363 * the speed (which can take up to a few *minutes*).
3364 * Speeds up fan to 100% duty-cycle, which is far above
3365 * the standard RPM levels. It is not impossible that
3366 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003367 * 5-3 unused in some models. Extra bits for fan level
3368 * in others, but still useless as all values above
3369 * 7 map to the same speed as level 7 in these models.
3370 * 2-0 fan level (0..7 usually)
3371 * 0x00 = stop
3372 * 0x07 = max (set when temperatures critical)
3373 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003374 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003375 *
3376 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
3377 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
3378 * does so, its initial value is meaningless (0x07).
3379 *
3380 * For firmware bugs, refer to:
3381 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
3382 *
3383 * ----
3384 *
3385 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
3386 * Main fan tachometer reading (in RPM)
3387 *
3388 * This register is present on all ThinkPads with a new-style EC, and
3389 * it is known not to be present on the A21m/e, and T22, as there is
3390 * something else in offset 0x84 according to the ACPI DSDT. Other
3391 * ThinkPads from this same time period (and earlier) probably lack the
3392 * tachometer as well.
3393 *
3394 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
3395 * was never fixed by IBM to report the EC firmware version string
3396 * probably support the tachometer (like the early X models), so
3397 * detecting it is quite hard. We need more data to know for sure.
3398 *
3399 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
3400 * might result.
3401 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03003402 * FIRMWARE BUG: may go stale while the EC is switching to full speed
3403 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003404 *
3405 * For firmware bugs, refer to:
3406 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
3407 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003408 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003409 * ThinkPad X31, X40, X41. Not available in the X60.
3410 *
3411 * FANS ACPI handle: takes three arguments: low speed, medium speed,
3412 * high speed. ACPI DSDT seems to map these three speeds to levels
3413 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
3414 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
3415 *
3416 * The speeds are stored on handles
3417 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
3418 *
3419 * There are three default speed sets, acessible as handles:
3420 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
3421 *
3422 * ACPI DSDT switches which set is in use depending on various
3423 * factors.
3424 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003425 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003426 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
3427 * but the ACPI tables just mention level 7.
3428 */
3429
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003430static enum fan_status_access_mode fan_status_access_mode;
3431static enum fan_control_access_mode fan_control_access_mode;
3432static enum fan_control_commands fan_control_commands;
3433
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02003434static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003435static u8 fan_control_desired_level;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02003436
Len Brown25c68a32006-12-08 04:43:41 -05003437static void fan_watchdog_fire(struct work_struct *ignored);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003438static int fan_watchdog_maxinterval;
Len Brown25c68a32006-12-08 04:43:41 -05003439static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003440
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003441IBM_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
3442IBM_HANDLE(gfan, ec, "GFAN", /* 570 */
3443 "\\FSPD", /* 600e/x, 770e, 770x */
3444 ); /* all others */
3445IBM_HANDLE(sfan, ec, "SFAN", /* 570 */
3446 "JFNS", /* 770x-JL */
3447 ); /* all others */
3448
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003449/*
3450 * SYSFS fan layout: hwmon compatible (device)
3451 *
3452 * pwm*_enable:
3453 * 0: "disengaged" mode
3454 * 1: manual mode
3455 * 2: native EC "auto" mode (recommended, hardware default)
3456 *
3457 * pwm*: set speed in manual mode, ignored otherwise.
3458 * 0 is level 0; 255 is level 7. Intermediate points done with linear
3459 * interpolation.
3460 *
3461 * fan*_input: tachometer reading, RPM
3462 *
3463 *
3464 * SYSFS fan layout: extensions
3465 *
3466 * fan_watchdog (driver):
3467 * fan watchdog interval in seconds, 0 disables (default), max 120
3468 */
3469
3470/* sysfs fan pwm1_enable ----------------------------------------------- */
3471static ssize_t fan_pwm1_enable_show(struct device *dev,
3472 struct device_attribute *attr,
3473 char *buf)
3474{
3475 int res, mode;
3476 u8 status;
3477
3478 res = fan_get_status_safe(&status);
3479 if (res)
3480 return res;
3481
3482 if (unlikely(tp_features.fan_ctrl_status_undef)) {
3483 if (status != fan_control_initial_status) {
3484 tp_features.fan_ctrl_status_undef = 0;
3485 } else {
3486 /* Return most likely status. In fact, it
3487 * might be the only possible status */
3488 status = TP_EC_FAN_AUTO;
3489 }
3490 }
3491
3492 if (status & TP_EC_FAN_FULLSPEED) {
3493 mode = 0;
3494 } else if (status & TP_EC_FAN_AUTO) {
3495 mode = 2;
3496 } else
3497 mode = 1;
3498
3499 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
3500}
3501
3502static ssize_t fan_pwm1_enable_store(struct device *dev,
3503 struct device_attribute *attr,
3504 const char *buf, size_t count)
3505{
3506 unsigned long t;
3507 int res, level;
3508
3509 if (parse_strtoul(buf, 2, &t))
3510 return -EINVAL;
3511
3512 switch (t) {
3513 case 0:
3514 level = TP_EC_FAN_FULLSPEED;
3515 break;
3516 case 1:
3517 level = TPACPI_FAN_LAST_LEVEL;
3518 break;
3519 case 2:
3520 level = TP_EC_FAN_AUTO;
3521 break;
3522 case 3:
3523 /* reserved for software-controlled auto mode */
3524 return -ENOSYS;
3525 default:
3526 return -EINVAL;
3527 }
3528
3529 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03003530 if (res == -ENXIO)
3531 return -EINVAL;
3532 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003533 return res;
3534
3535 fan_watchdog_reset();
3536
3537 return count;
3538}
3539
3540static struct device_attribute dev_attr_fan_pwm1_enable =
3541 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
3542 fan_pwm1_enable_show, fan_pwm1_enable_store);
3543
3544/* sysfs fan pwm1 ------------------------------------------------------ */
3545static ssize_t fan_pwm1_show(struct device *dev,
3546 struct device_attribute *attr,
3547 char *buf)
3548{
3549 int res;
3550 u8 status;
3551
3552 res = fan_get_status_safe(&status);
3553 if (res)
3554 return res;
3555
3556 if (unlikely(tp_features.fan_ctrl_status_undef)) {
3557 if (status != fan_control_initial_status) {
3558 tp_features.fan_ctrl_status_undef = 0;
3559 } else {
3560 status = TP_EC_FAN_AUTO;
3561 }
3562 }
3563
3564 if ((status &
3565 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
3566 status = fan_control_desired_level;
3567
3568 if (status > 7)
3569 status = 7;
3570
3571 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
3572}
3573
3574static ssize_t fan_pwm1_store(struct device *dev,
3575 struct device_attribute *attr,
3576 const char *buf, size_t count)
3577{
3578 unsigned long s;
3579 int rc;
3580 u8 status, newlevel;
3581
3582 if (parse_strtoul(buf, 255, &s))
3583 return -EINVAL;
3584
3585 /* scale down from 0-255 to 0-7 */
3586 newlevel = (s >> 5) & 0x07;
3587
3588 rc = mutex_lock_interruptible(&fan_mutex);
3589 if (rc < 0)
3590 return rc;
3591
3592 rc = fan_get_status(&status);
3593 if (!rc && (status &
3594 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
3595 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03003596 if (rc == -ENXIO)
3597 rc = -EINVAL;
3598 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003599 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03003600 fan_watchdog_reset();
3601 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003602 }
3603
3604 mutex_unlock(&fan_mutex);
3605 return (rc)? rc : count;
3606}
3607
3608static struct device_attribute dev_attr_fan_pwm1 =
3609 __ATTR(pwm1, S_IWUSR | S_IRUGO,
3610 fan_pwm1_show, fan_pwm1_store);
3611
3612/* sysfs fan fan1_input ------------------------------------------------ */
3613static ssize_t fan_fan1_input_show(struct device *dev,
3614 struct device_attribute *attr,
3615 char *buf)
3616{
3617 int res;
3618 unsigned int speed;
3619
3620 res = fan_get_speed(&speed);
3621 if (res < 0)
3622 return res;
3623
3624 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
3625}
3626
3627static struct device_attribute dev_attr_fan_fan1_input =
3628 __ATTR(fan1_input, S_IRUGO,
3629 fan_fan1_input_show, NULL);
3630
3631/* sysfs fan fan_watchdog (driver) ------------------------------------- */
3632static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
3633 char *buf)
3634{
3635 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
3636}
3637
3638static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
3639 const char *buf, size_t count)
3640{
3641 unsigned long t;
3642
3643 if (parse_strtoul(buf, 120, &t))
3644 return -EINVAL;
3645
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03003646 if (!fan_control_allowed)
3647 return -EPERM;
3648
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003649 fan_watchdog_maxinterval = t;
3650 fan_watchdog_reset();
3651
3652 return count;
3653}
3654
3655static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
3656 fan_fan_watchdog_show, fan_fan_watchdog_store);
3657
3658/* --------------------------------------------------------------------- */
3659static struct attribute *fan_attributes[] = {
3660 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
3661 &dev_attr_fan_fan1_input.attr,
3662 NULL
3663};
3664
3665static const struct attribute_group fan_attr_group = {
3666 .attrs = fan_attributes,
3667};
3668
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003669static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003670{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003671 int rc;
3672
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003673 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
3674
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003675 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003676 fan_status_access_mode = TPACPI_FAN_NONE;
3677 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003678 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003679 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003680 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003681 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003682
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003683 IBM_ACPIHANDLE_INIT(fans);
3684 IBM_ACPIHANDLE_INIT(gfan);
3685 IBM_ACPIHANDLE_INIT(sfan);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003686
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003687 if (gfan_handle) {
3688 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003689 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003690 } else {
3691 /* all other ThinkPads: note that even old-style
3692 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02003693 if (likely(acpi_ec_read(fan_status_offset,
3694 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003695 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02003696
3697 /* In some ThinkPads, neither the EC nor the ACPI
3698 * DSDT initialize the fan status, and it ends up
3699 * being set to 0x07 when it *could* be either
3700 * 0x07 or 0x80.
3701 *
3702 * Enable for TP-1Y (T43), TP-78 (R51e),
3703 * TP-76 (R52), TP-70 (T43, R52), which are known
3704 * to be buggy. */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03003705 if (fan_control_initial_status == 0x07) {
3706 switch (thinkpad_id.ec_model) {
3707 case 0x5931: /* TP-1Y */
3708 case 0x3837: /* TP-78 */
3709 case 0x3637: /* TP-76 */
3710 case 0x3037: /* TP-70 */
3711 printk(IBM_NOTICE
3712 "fan_init: initial fan status is "
3713 "unknown, assuming it is in auto "
3714 "mode\n");
3715 tp_features.fan_ctrl_status_undef = 1;
3716 ;;
3717 }
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02003718 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003719 } else {
3720 printk(IBM_ERR
3721 "ThinkPad ACPI EC access misbehaving, "
3722 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003723 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003724 }
3725 }
3726
3727 if (sfan_handle) {
3728 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003729 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02003730 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003731 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003732 } else {
3733 if (!gfan_handle) {
3734 /* gfan without sfan means no fan control */
3735 /* all other models implement TP EC 0x2f control */
3736
3737 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02003738 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003739 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003740 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003741 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003742 TPACPI_FAN_CMD_SPEED |
3743 TPACPI_FAN_CMD_LEVEL |
3744 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003745 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003746 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02003747 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003748 TPACPI_FAN_CMD_LEVEL |
3749 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003750 }
3751 }
3752 }
3753
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003754 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
3755 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
3756 fan_control_access_mode != TPACPI_FAN_WR_NONE),
3757 fan_status_access_mode, fan_control_access_mode);
3758
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03003759 /* fan control master switch */
3760 if (!fan_control_allowed) {
3761 fan_control_access_mode = TPACPI_FAN_WR_NONE;
3762 fan_control_commands = 0;
3763 dbg_printk(TPACPI_DBG_INIT,
3764 "fan control features disabled by parameter\n");
3765 }
3766
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003767 /* update fan_control_desired_level */
3768 if (fan_status_access_mode != TPACPI_FAN_NONE)
3769 fan_get_status_safe(NULL);
3770
3771 if (fan_status_access_mode != TPACPI_FAN_NONE ||
3772 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
3773 rc = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3774 &fan_attr_group);
3775 if (!(rc < 0))
3776 rc = driver_create_file(&tpacpi_pdriver.driver,
3777 &driver_attr_fan_watchdog);
3778 if (rc < 0)
3779 return rc;
3780 return 0;
3781 } else
3782 return 1;
3783}
3784
3785/*
3786 * Call with fan_mutex held
3787 */
3788static void fan_update_desired_level(u8 status)
3789{
3790 if ((status &
3791 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
3792 if (status > 7)
3793 fan_control_desired_level = 7;
3794 else
3795 fan_control_desired_level = status;
3796 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003797}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003798
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02003799static int fan_get_status(u8 *status)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003800{
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02003801 u8 s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003802
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02003803 /* TODO:
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003804 * Add TPACPI_FAN_RD_ACPI_FANS ? */
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02003805
Henrique de Moraes Holschuh3ef8a602006-11-24 11:47:10 -02003806 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003807 case TPACPI_FAN_RD_ACPI_GFAN:
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003808 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02003809
3810 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003811 return -EIO;
3812
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02003813 if (likely(status))
3814 *status = s & 0x07;
3815
3816 break;
3817
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003818 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02003819 /* all except 570, 600e/x, 770e, 770x */
3820 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
3821 return -EIO;
3822
3823 if (likely(status))
3824 *status = s;
3825
3826 break;
3827
3828 default:
3829 return -ENXIO;
3830 }
3831
3832 return 0;
3833}
3834
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003835static int fan_get_status_safe(u8 *status)
3836{
3837 int rc;
3838 u8 s;
3839
3840 rc = mutex_lock_interruptible(&fan_mutex);
3841 if (rc < 0)
3842 return rc;
3843 rc = fan_get_status(&s);
3844 if (!rc)
3845 fan_update_desired_level(s);
3846 mutex_unlock(&fan_mutex);
3847
3848 if (status)
3849 *status = s;
3850
3851 return rc;
3852}
3853
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003854static void fan_exit(void)
3855{
Henrique de Moraes Holschuh99fba3f2007-04-21 11:08:44 -03003856 vdbg_printk(TPACPI_DBG_EXIT, "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003857
3858 /* FIXME: can we really do this unconditionally? */
3859 sysfs_remove_group(&tpacpi_pdev->dev.kobj, &fan_attr_group);
3860 driver_remove_file(&tpacpi_pdriver.driver, &driver_attr_fan_watchdog);
3861
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003862 cancel_delayed_work(&fan_watchdog_task);
3863 flush_scheduled_work();
3864}
3865
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02003866static int fan_get_speed(unsigned int *speed)
3867{
3868 u8 hi, lo;
3869
3870 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003871 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02003872 /* all except 570, 600e/x, 770e, 770x */
3873 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
3874 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
3875 return -EIO;
3876
3877 if (likely(speed))
3878 *speed = (hi << 8) | lo;
3879
3880 break;
3881
3882 default:
3883 return -ENXIO;
3884 }
3885
3886 return 0;
3887}
3888
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003889static void fan_watchdog_fire(struct work_struct *ignored)
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003890{
Henrique de Moraes Holschuh99fba3f2007-04-21 11:08:44 -03003891 int rc;
3892
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003893 printk(IBM_NOTICE "fan watchdog: enabling fan\n");
Henrique de Moraes Holschuh99fba3f2007-04-21 11:08:44 -03003894 rc = fan_set_enable();
3895 if (rc < 0) {
3896 printk(IBM_ERR "fan watchdog: error %d while enabling fan, "
3897 "will try again later...\n", -rc);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003898 /* reschedule for later */
3899 fan_watchdog_reset();
3900 }
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003901}
3902
3903static void fan_watchdog_reset(void)
3904{
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03003905 static int fan_watchdog_active;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003906
Henrique de Moraes Holschuh4985cd02007-04-27 22:00:10 -03003907 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
3908 return;
3909
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003910 if (fan_watchdog_active)
3911 cancel_delayed_work(&fan_watchdog_task);
3912
3913 if (fan_watchdog_maxinterval > 0) {
3914 fan_watchdog_active = 1;
3915 if (!schedule_delayed_work(&fan_watchdog_task,
3916 msecs_to_jiffies(fan_watchdog_maxinterval
3917 * 1000))) {
3918 printk(IBM_ERR "failed to schedule the fan watchdog, "
3919 "watchdog will not trigger\n");
3920 }
3921 } else
3922 fan_watchdog_active = 0;
3923}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003924
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003925static int fan_set_level(int level)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003926{
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03003927 if (!fan_control_allowed)
3928 return -EPERM;
3929
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003930 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003931 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003932 if (level >= 0 && level <= 7) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003933 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003934 return -EIO;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003935 } else
3936 return -EINVAL;
3937 break;
3938
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003939 case TPACPI_FAN_WR_ACPI_FANS:
3940 case TPACPI_FAN_WR_TPEC:
3941 if ((level != TP_EC_FAN_AUTO) &&
3942 (level != TP_EC_FAN_FULLSPEED) &&
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02003943 ((level < 0) || (level > 7)))
3944 return -EINVAL;
3945
Henrique de Moraes Holschuheaa75712007-04-24 11:48:18 -03003946 /* safety net should the EC not support AUTO
3947 * or FULLSPEED mode bits and just ignore them */
3948 if (level & TP_EC_FAN_FULLSPEED)
3949 level |= 7; /* safety min speed 7 */
3950 else if (level & TP_EC_FAN_FULLSPEED)
3951 level |= 4; /* safety min speed 4 */
3952
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003953 if (!acpi_ec_write(fan_status_offset, level))
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02003954 return -EIO;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02003955 else
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003956 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02003957 break;
3958
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003959 default:
3960 return -ENXIO;
3961 }
3962 return 0;
3963}
3964
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003965static int fan_set_level_safe(int level)
3966{
3967 int rc;
3968
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03003969 if (!fan_control_allowed)
3970 return -EPERM;
3971
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003972 rc = mutex_lock_interruptible(&fan_mutex);
3973 if (rc < 0)
3974 return rc;
3975
3976 if (level == TPACPI_FAN_LAST_LEVEL)
3977 level = fan_control_desired_level;
3978
3979 rc = fan_set_level(level);
3980 if (!rc)
3981 fan_update_desired_level(level);
3982
3983 mutex_unlock(&fan_mutex);
3984 return rc;
3985}
3986
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003987static int fan_set_enable(void)
3988{
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02003989 u8 s;
3990 int rc;
3991
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03003992 if (!fan_control_allowed)
3993 return -EPERM;
3994
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003995 rc = mutex_lock_interruptible(&fan_mutex);
3996 if (rc < 0)
3997 return rc;
3998
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003999 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004000 case TPACPI_FAN_WR_ACPI_FANS:
4001 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004002 rc = fan_get_status(&s);
4003 if (rc < 0)
4004 break;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02004005
4006 /* Don't go out of emergency fan mode */
Henrique de Moraes Holschuheaa75712007-04-24 11:48:18 -03004007 if (s != 7) {
4008 s &= 0x07;
4009 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
4010 }
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02004011
4012 if (!acpi_ec_write(fan_status_offset, s))
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004013 rc = -EIO;
4014 else {
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004015 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004016 rc = 0;
4017 }
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02004018 break;
4019
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004020 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004021 rc = fan_get_status(&s);
4022 if (rc < 0)
4023 break;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02004024
4025 s &= 0x07;
4026
4027 /* Set fan to at least level 4 */
Henrique de Moraes Holschuheaa75712007-04-24 11:48:18 -03004028 s |= 4;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02004029
4030 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004031 rc= -EIO;
4032 else
4033 rc = 0;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004034 break;
4035
4036 default:
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004037 rc = -ENXIO;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004038 }
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004039
4040 mutex_unlock(&fan_mutex);
4041 return rc;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004042}
4043
4044static int fan_set_disable(void)
4045{
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004046 int rc;
4047
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03004048 if (!fan_control_allowed)
4049 return -EPERM;
4050
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004051 rc = mutex_lock_interruptible(&fan_mutex);
4052 if (rc < 0)
4053 return rc;
4054
4055 rc = 0;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004056 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004057 case TPACPI_FAN_WR_ACPI_FANS:
4058 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004059 if (!acpi_ec_write(fan_status_offset, 0x00))
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004060 rc = -EIO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004061 else {
4062 fan_control_desired_level = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004063 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004064 }
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004065 break;
4066
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004067 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02004068 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004069 rc = -EIO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004070 else
4071 fan_control_desired_level = 0;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02004072 break;
4073
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004074 default:
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004075 rc = -ENXIO;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004076 }
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004077
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004078
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004079 mutex_unlock(&fan_mutex);
4080 return rc;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004081}
4082
4083static int fan_set_speed(int speed)
4084{
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004085 int rc;
4086
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03004087 if (!fan_control_allowed)
4088 return -EPERM;
4089
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004090 rc = mutex_lock_interruptible(&fan_mutex);
4091 if (rc < 0)
4092 return rc;
4093
4094 rc = 0;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004095 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004096 case TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004097 if (speed >= 0 && speed <= 65535) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004098 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
4099 speed, speed, speed))
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004100 rc = -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004101 } else
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004102 rc = -EINVAL;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004103 break;
4104
4105 default:
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004106 rc = -ENXIO;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004107 }
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03004108
4109 mutex_unlock(&fan_mutex);
4110 return rc;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004111}
4112
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004113static int fan_read(char *p)
4114{
4115 int len = 0;
4116 int rc;
4117 u8 status;
4118 unsigned int speed = 0;
4119
4120 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004121 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004122 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004123 if ((rc = fan_get_status_safe(&status)) < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004124 return rc;
4125
4126 len += sprintf(p + len, "status:\t\t%s\n"
4127 "level:\t\t%d\n",
4128 (status != 0) ? "enabled" : "disabled", status);
4129 break;
4130
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004131 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004132 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004133 if ((rc = fan_get_status_safe(&status)) < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004134 return rc;
4135
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004136 if (unlikely(tp_features.fan_ctrl_status_undef)) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004137 if (status != fan_control_initial_status)
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03004138 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004139 else
4140 /* Return most likely status. In fact, it
4141 * might be the only possible status */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004142 status = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004143 }
4144
4145 len += sprintf(p + len, "status:\t\t%s\n",
4146 (status != 0) ? "enabled" : "disabled");
4147
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004148 if ((rc = fan_get_speed(&speed)) < 0)
4149 return rc;
4150
4151 len += sprintf(p + len, "speed:\t\t%d\n", speed);
4152
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004153 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004154 /* Disengaged mode takes precedence */
4155 len += sprintf(p + len, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004156 else if (status & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004157 len += sprintf(p + len, "level:\t\tauto\n");
4158 else
4159 len += sprintf(p + len, "level:\t\t%d\n", status);
4160 break;
4161
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004162 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004163 default:
4164 len += sprintf(p + len, "status:\t\tnot supported\n");
4165 }
4166
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004167 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004168 len += sprintf(p + len, "commands:\tlevel <level>");
4169
4170 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004171 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004172 len += sprintf(p + len, " (<level> is 0-7)\n");
4173 break;
4174
4175 default:
4176 len += sprintf(p + len, " (<level> is 0-7, "
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004177 "auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004178 break;
4179 }
4180 }
4181
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004182 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004183 len += sprintf(p + len, "commands:\tenable, disable\n"
4184 "commands:\twatchdog <timeout> (<timeout> is 0 (off), "
4185 "1-120 (seconds))\n");
4186
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004187 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004188 len += sprintf(p + len, "commands:\tspeed <speed>"
4189 " (<speed> is 0-65535)\n");
4190
4191 return len;
4192}
4193
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004194static int fan_write_cmd_level(const char *cmd, int *rc)
4195{
4196 int level;
4197
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02004198 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004199 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004200 else if ((strlencmp(cmd, "level disengaged") == 0) |
4201 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004202 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02004203 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004204 return 0;
4205
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004206 if ((*rc = fan_set_level_safe(level)) == -ENXIO)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004207 printk(IBM_ERR "level command accepted for unsupported "
4208 "access mode %d", fan_control_access_mode);
4209
4210 return 1;
4211}
4212
4213static int fan_write_cmd_enable(const char *cmd, int *rc)
4214{
4215 if (strlencmp(cmd, "enable") != 0)
4216 return 0;
4217
4218 if ((*rc = fan_set_enable()) == -ENXIO)
4219 printk(IBM_ERR "enable command accepted for unsupported "
4220 "access mode %d", fan_control_access_mode);
4221
4222 return 1;
4223}
4224
4225static int fan_write_cmd_disable(const char *cmd, int *rc)
4226{
4227 if (strlencmp(cmd, "disable") != 0)
4228 return 0;
4229
4230 if ((*rc = fan_set_disable()) == -ENXIO)
4231 printk(IBM_ERR "disable command accepted for unsupported "
4232 "access mode %d", fan_control_access_mode);
4233
4234 return 1;
4235}
4236
4237static int fan_write_cmd_speed(const char *cmd, int *rc)
4238{
4239 int speed;
4240
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02004241 /* TODO:
4242 * Support speed <low> <medium> <high> ? */
4243
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004244 if (sscanf(cmd, "speed %d", &speed) != 1)
4245 return 0;
4246
4247 if ((*rc = fan_set_speed(speed)) == -ENXIO)
4248 printk(IBM_ERR "speed command accepted for unsupported "
4249 "access mode %d", fan_control_access_mode);
4250
4251 return 1;
4252}
4253
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02004254static int fan_write_cmd_watchdog(const char *cmd, int *rc)
4255{
4256 int interval;
4257
4258 if (sscanf(cmd, "watchdog %d", &interval) != 1)
4259 return 0;
4260
4261 if (interval < 0 || interval > 120)
4262 *rc = -EINVAL;
4263 else
4264 fan_watchdog_maxinterval = interval;
4265
4266 return 1;
4267}
4268
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004269static int fan_write(char *buf)
4270{
4271 char *cmd;
4272 int rc = 0;
4273
4274 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004275 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004276 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004277 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004278 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02004279 fan_write_cmd_disable(cmd, &rc) ||
4280 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004281 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004282 fan_write_cmd_speed(cmd, &rc))
4283 )
4284 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02004285 else if (!rc)
4286 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004287 }
4288
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004289 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004290}
4291
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004292static struct ibm_struct fan_driver_data = {
4293 .name = "fan",
4294 .read = fan_read,
4295 .write = fan_write,
4296 .exit = fan_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004297};
4298
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004299/****************************************************************************
4300 ****************************************************************************
4301 *
4302 * Infrastructure
4303 *
4304 ****************************************************************************
4305 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004307/* /proc support */
Henrique de Moraes Holschuh94954cc2007-07-18 23:45:27 -03004308static struct proc_dir_entry *proc_dir;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004309
4310/* Subdriver registry */
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004311static LIST_HEAD(tpacpi_all_drivers);
4312
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004314/*
4315 * Module and infrastructure proble, init and exit handling
4316 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004318#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004319static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004320{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004321 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004322
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004323 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004324}
4325#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
4326
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004327static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328{
4329 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004330 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331 struct proc_dir_entry *entry;
4332
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004333 BUG_ON(ibm == NULL);
4334
4335 INIT_LIST_HEAD(&ibm->all_drivers);
4336
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004337 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338 return 0;
4339
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004340 dbg_printk(TPACPI_DBG_INIT,
4341 "probing for %s\n", ibm->name);
4342
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004343 if (iibm->init) {
4344 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004345 if (ret > 0)
4346 return 0; /* probe failed */
4347 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004349
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004350 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351 }
4352
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004353 if (ibm->acpi) {
4354 if (ibm->acpi->hid) {
4355 ret = register_tpacpi_subdriver(ibm);
4356 if (ret)
4357 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004358 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004359
4360 if (ibm->acpi->notify) {
4361 ret = setup_acpi_notify(ibm);
4362 if (ret == -ENODEV) {
4363 printk(IBM_NOTICE "disabling subdriver %s\n",
4364 ibm->name);
4365 ret = 0;
4366 goto err_out;
4367 }
4368 if (ret < 0)
4369 goto err_out;
4370 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004371 }
4372
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004373 dbg_printk(TPACPI_DBG_INIT,
4374 "%s installed\n", ibm->name);
4375
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004376 if (ibm->read) {
4377 entry = create_proc_entry(ibm->name,
4378 S_IFREG | S_IRUGO | S_IWUSR,
4379 proc_dir);
4380 if (!entry) {
4381 printk(IBM_ERR "unable to create proc entry %s\n",
4382 ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004383 ret = -ENODEV;
4384 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004385 }
4386 entry->owner = THIS_MODULE;
4387 entry->data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004388 entry->read_proc = &dispatch_procfs_read;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004389 if (ibm->write)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004390 entry->write_proc = &dispatch_procfs_write;
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004391 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004392 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004394 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
4395
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004397
4398err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004399 dbg_printk(TPACPI_DBG_INIT,
4400 "%s: at error exit path with result %d\n",
4401 ibm->name, ret);
4402
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004403 ibm_exit(ibm);
4404 return (ret < 0)? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405}
4406
4407static void ibm_exit(struct ibm_struct *ibm)
4408{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004409 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004410
4411 list_del_init(&ibm->all_drivers);
4412
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004413 if (ibm->flags.acpi_notify_installed) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004414 dbg_printk(TPACPI_DBG_EXIT,
4415 "%s: acpi_remove_notify_handler\n", ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004416 BUG_ON(!ibm->acpi);
4417 acpi_remove_notify_handler(*ibm->acpi->handle,
4418 ibm->acpi->type,
4419 dispatch_acpi_notify);
4420 ibm->flags.acpi_notify_installed = 0;
4421 ibm->flags.acpi_notify_installed = 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004422 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004424 if (ibm->flags.proc_created) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004425 dbg_printk(TPACPI_DBG_EXIT,
4426 "%s: remove_proc_entry\n", ibm->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427 remove_proc_entry(ibm->name, proc_dir);
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004428 ibm->flags.proc_created = 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004431 if (ibm->flags.acpi_driver_registered) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004432 dbg_printk(TPACPI_DBG_EXIT,
4433 "%s: acpi_bus_unregister_driver\n", ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004434 BUG_ON(!ibm->acpi);
4435 acpi_bus_unregister_driver(ibm->acpi->driver);
4436 kfree(ibm->acpi->driver);
4437 ibm->acpi->driver = NULL;
4438 ibm->flags.acpi_driver_registered = 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004439 }
4440
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004441 if (ibm->flags.init_called && ibm->exit) {
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004442 ibm->exit();
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004443 ibm->flags.init_called = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004445
4446 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447}
4448
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004449/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03004451static void __init get_thinkpad_model_data(struct thinkpad_id_data *tp)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004452{
4453 struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02004454 char ec_fw_string[18];
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004455
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03004456 if (!tp)
4457 return;
4458
4459 memset(tp, 0, sizeof(*tp));
4460
4461 if (dmi_name_in_vendors("IBM"))
4462 tp->vendor = PCI_VENDOR_ID_IBM;
4463 else if (dmi_name_in_vendors("LENOVO"))
4464 tp->vendor = PCI_VENDOR_ID_LENOVO;
4465 else
4466 return;
4467
4468 tp->bios_version_str = kstrdup(dmi_get_system_info(DMI_BIOS_VERSION),
4469 GFP_KERNEL);
4470 if (!tp->bios_version_str)
4471 return;
4472 tp->bios_model = tp->bios_version_str[0]
4473 | (tp->bios_version_str[1] << 8);
4474
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004475 /*
4476 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
4477 * X32 or newer, all Z series; Some models must have an
4478 * up-to-date BIOS or they will not be detected.
4479 *
4480 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
4481 */
4482 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02004483 if (sscanf(dev->name,
4484 "IBM ThinkPad Embedded Controller -[%17c",
4485 ec_fw_string) == 1) {
4486 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
4487 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03004488
4489 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
4490 tp->ec_model = ec_fw_string[0]
4491 | (ec_fw_string[1] << 8);
4492 break;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02004493 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004494 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03004495
4496 tp->model_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_VERSION),
4497 GFP_KERNEL);
4498 if (strnicmp(tp->model_str, "ThinkPad", 8) != 0) {
4499 kfree(tp->model_str);
4500 tp->model_str = NULL;
4501 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502}
4503
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004504static int __init probe_for_thinkpad(void)
4505{
4506 int is_thinkpad;
4507
4508 if (acpi_disabled)
4509 return -ENODEV;
4510
4511 /*
4512 * Non-ancient models have better DMI tagging, but very old models
4513 * don't.
4514 */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03004515 is_thinkpad = (thinkpad_id.model_str != NULL);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004516
4517 /* ec is required because many other handles are relative to it */
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004518 IBM_ACPIHANDLE_INIT(ec);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004519 if (!ec_handle) {
4520 if (is_thinkpad)
4521 printk(IBM_ERR
4522 "Not yet supported ThinkPad detected!\n");
4523 return -ENODEV;
4524 }
4525
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03004526 /*
4527 * Risks a regression on very old machines, but reduces potential
4528 * false positives a damn great deal
4529 */
4530 if (!is_thinkpad)
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03004531 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03004532
4533 if (!is_thinkpad && !force_load)
4534 return -ENODEV;
4535
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004536 return 0;
4537}
4538
4539
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004540/* Module init, exit, parameters */
4541
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004542static struct ibm_init_struct ibms_init[] __initdata = {
4543 {
4544 .init = thinkpad_acpi_driver_init,
4545 .data = &thinkpad_acpi_driver_data,
4546 },
4547 {
4548 .init = hotkey_init,
4549 .data = &hotkey_driver_data,
4550 },
4551 {
4552 .init = bluetooth_init,
4553 .data = &bluetooth_driver_data,
4554 },
4555 {
4556 .init = wan_init,
4557 .data = &wan_driver_data,
4558 },
4559 {
4560 .init = video_init,
4561 .data = &video_driver_data,
4562 },
4563 {
4564 .init = light_init,
4565 .data = &light_driver_data,
4566 },
4567#ifdef CONFIG_THINKPAD_ACPI_DOCK
4568 {
4569 .init = dock_init,
4570 .data = &dock_driver_data[0],
4571 },
4572 {
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03004573 .init = dock_init2,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004574 .data = &dock_driver_data[1],
4575 },
4576#endif
4577#ifdef CONFIG_THINKPAD_ACPI_BAY
4578 {
4579 .init = bay_init,
4580 .data = &bay_driver_data,
4581 },
4582#endif
4583 {
4584 .init = cmos_init,
4585 .data = &cmos_driver_data,
4586 },
4587 {
4588 .init = led_init,
4589 .data = &led_driver_data,
4590 },
4591 {
4592 .init = beep_init,
4593 .data = &beep_driver_data,
4594 },
4595 {
4596 .init = thermal_init,
4597 .data = &thermal_driver_data,
4598 },
4599 {
4600 .data = &ecdump_driver_data,
4601 },
4602 {
4603 .init = brightness_init,
4604 .data = &brightness_driver_data,
4605 },
4606 {
4607 .data = &volume_driver_data,
4608 },
4609 {
4610 .init = fan_init,
4611 .data = &fan_driver_data,
4612 },
4613};
4614
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004615static int __init set_ibm_param(const char *val, struct kernel_param *kp)
4616{
4617 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004618 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004619
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004620 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
4621 ibm = ibms_init[i].data;
4622 BUG_ON(ibm == NULL);
4623
4624 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
4625 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004626 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004627 strcpy(ibms_init[i].param, val);
4628 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004629 return 0;
4630 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004631 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004632
4633 return -EINVAL;
4634}
4635
4636static int experimental;
4637module_param(experimental, int, 0);
4638
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03004639static u32 dbg_level;
4640module_param_named(debug, dbg_level, uint, 0);
4641
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03004642static int force_load;
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03004643module_param(force_load, bool, 0);
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03004644
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03004645static int fan_control_allowed;
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03004646module_param_named(fan_control, fan_control_allowed, bool, 0);
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03004647
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03004648static int brightness_mode;
4649module_param_named(brightness_mode, brightness_mode, int, 0);
4650
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004651static unsigned int hotkey_report_mode;
4652module_param(hotkey_report_mode, uint, 0);
4653
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004654#define IBM_PARAM(feature) \
4655 module_param_call(feature, set_ibm_param, NULL, NULL, 0)
4656
4657IBM_PARAM(hotkey);
4658IBM_PARAM(bluetooth);
4659IBM_PARAM(video);
4660IBM_PARAM(light);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03004661#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004662IBM_PARAM(dock);
4663#endif
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03004664#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004665IBM_PARAM(bay);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03004666#endif /* CONFIG_THINKPAD_ACPI_BAY */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004667IBM_PARAM(cmos);
4668IBM_PARAM(led);
4669IBM_PARAM(beep);
4670IBM_PARAM(ecdump);
4671IBM_PARAM(brightness);
4672IBM_PARAM(volume);
4673IBM_PARAM(fan);
4674
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03004675static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676{
4677 int ret, i;
4678
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03004679 /* Parameter checking */
4680 if (hotkey_report_mode > 2)
4681 return -EINVAL;
4682
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03004683 /* Driver-level probe */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03004684
4685 get_thinkpad_model_data(&thinkpad_id);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004686 ret = probe_for_thinkpad();
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03004687 if (ret) {
4688 thinkpad_acpi_module_exit();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004689 return ret;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03004690 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03004692 /* Driver initialization */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03004693
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004694 IBM_ACPIHANDLE_INIT(ecrd);
4695 IBM_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004697 proc_dir = proc_mkdir(IBM_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004698 if (!proc_dir) {
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004699 printk(IBM_ERR "unable to create proc dir " IBM_PROC_DIR);
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03004700 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701 return -ENODEV;
4702 }
4703 proc_dir->owner = THIS_MODULE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004704
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03004705 ret = platform_driver_register(&tpacpi_pdriver);
4706 if (ret) {
4707 printk(IBM_ERR "unable to register platform driver\n");
4708 thinkpad_acpi_module_exit();
4709 return ret;
4710 }
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03004711 tp_features.platform_drv_registered = 1;
4712
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03004713 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
4714 if (ret) {
4715 printk(IBM_ERR "unable to create sysfs driver attributes\n");
4716 thinkpad_acpi_module_exit();
4717 return ret;
4718 }
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03004719 tp_features.platform_drv_attrs_registered = 1;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03004720
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03004721
4722 /* Device initialization */
4723 tpacpi_pdev = platform_device_register_simple(IBM_DRVR_NAME, -1,
4724 NULL, 0);
4725 if (IS_ERR(tpacpi_pdev)) {
4726 ret = PTR_ERR(tpacpi_pdev);
4727 tpacpi_pdev = NULL;
4728 printk(IBM_ERR "unable to register platform device\n");
4729 thinkpad_acpi_module_exit();
4730 return ret;
4731 }
4732 tpacpi_hwmon = hwmon_device_register(&tpacpi_pdev->dev);
4733 if (IS_ERR(tpacpi_hwmon)) {
4734 ret = PTR_ERR(tpacpi_hwmon);
4735 tpacpi_hwmon = NULL;
4736 printk(IBM_ERR "unable to register hwmon device\n");
4737 thinkpad_acpi_module_exit();
4738 return ret;
4739 }
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03004740 tpacpi_inputdev = input_allocate_device();
4741 if (!tpacpi_inputdev) {
4742 printk(IBM_ERR "unable to allocate input device\n");
4743 thinkpad_acpi_module_exit();
4744 return -ENOMEM;
4745 } else {
4746 /* Prepare input device, but don't register */
4747 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
4748 tpacpi_inputdev->phys = IBM_DRVR_NAME "/input0";
4749 tpacpi_inputdev->id.bustype = BUS_HOST;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03004750 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
4751 thinkpad_id.vendor :
4752 PCI_VENDOR_ID_IBM;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03004753 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
4754 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
4755 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004756 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
4757 ret = ibm_init(&ibms_init[i]);
4758 if (ret >= 0 && *ibms_init[i].param)
4759 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03004761 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762 return ret;
4763 }
4764 }
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03004765 ret = input_register_device(tpacpi_inputdev);
4766 if (ret < 0) {
4767 printk(IBM_ERR "unable to register input device\n");
4768 thinkpad_acpi_module_exit();
4769 return ret;
4770 } else {
4771 tp_features.input_device_registered = 1;
4772 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773
4774 return 0;
4775}
4776
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03004777static void thinkpad_acpi_module_exit(void)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004778{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004779 struct ibm_struct *ibm, *itmp;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004780
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004781 list_for_each_entry_safe_reverse(ibm, itmp,
4782 &tpacpi_all_drivers,
4783 all_drivers) {
4784 ibm_exit(ibm);
4785 }
4786
4787 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004788
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03004789 if (tpacpi_inputdev) {
4790 if (tp_features.input_device_registered)
4791 input_unregister_device(tpacpi_inputdev);
4792 else
4793 input_free_device(tpacpi_inputdev);
4794 }
4795
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03004796 if (tpacpi_hwmon)
4797 hwmon_device_unregister(tpacpi_hwmon);
4798
4799 if (tpacpi_pdev)
4800 platform_device_unregister(tpacpi_pdev);
4801
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03004802 if (tp_features.platform_drv_attrs_registered)
4803 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
4804
4805 if (tp_features.platform_drv_registered)
4806 platform_driver_unregister(&tpacpi_pdriver);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03004807
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004808 if (proc_dir)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004809 remove_proc_entry(IBM_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004810
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03004811 kfree(thinkpad_id.bios_version_str);
4812 kfree(thinkpad_id.ec_version_str);
4813 kfree(thinkpad_id.model_str);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004814}
4815
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03004816module_init(thinkpad_acpi_module_init);
4817module_exit(thinkpad_acpi_module_exit);