blob: 22a5f228b55a0218fc6732af94acfc7811980602 [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 Holschuh643f12d2007-03-29 01:58:43 -030024#define IBM_VERSION "0.14"
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -030025#define TPACPI_SYSFS_VERSION 0x000100
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
132static acpi_handle root_handle = NULL;
133
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;
414 ibm->acpi->driver->ops.add = &tpacpi_device_add;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300415
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300416 rc = acpi_bus_register_driver(ibm->acpi->driver);
417 if (rc < 0) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300418 printk(IBM_ERR "acpi_bus_register_driver(%s) failed: %d\n",
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300419 ibm->acpi->hid, rc);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300420 kfree(ibm->acpi->driver);
421 ibm->acpi->driver = NULL;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300422 } else if (!rc)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300423 ibm->flags.acpi_driver_registered = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300424
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300425 return rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300426}
427
428
429/****************************************************************************
430 ****************************************************************************
431 *
432 * Procfs Helpers
433 *
434 ****************************************************************************
435 ****************************************************************************/
436
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300437static int dispatch_procfs_read(char *page, char **start, off_t off,
438 int count, int *eof, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300439{
440 struct ibm_struct *ibm = data;
441 int len;
442
443 if (!ibm || !ibm->read)
444 return -EINVAL;
445
446 len = ibm->read(page);
447 if (len < 0)
448 return len;
449
450 if (len <= off + count)
451 *eof = 1;
452 *start = page + off;
453 len -= off;
454 if (len > count)
455 len = count;
456 if (len < 0)
457 len = 0;
458
459 return len;
460}
461
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300462static int dispatch_procfs_write(struct file *file,
463 const char __user * userbuf,
464 unsigned long count, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300465{
466 struct ibm_struct *ibm = data;
467 char *kernbuf;
468 int ret;
469
470 if (!ibm || !ibm->write)
471 return -EINVAL;
472
473 kernbuf = kmalloc(count + 2, GFP_KERNEL);
474 if (!kernbuf)
475 return -ENOMEM;
476
477 if (copy_from_user(kernbuf, userbuf, count)) {
478 kfree(kernbuf);
479 return -EFAULT;
480 }
481
482 kernbuf[count] = 0;
483 strcat(kernbuf, ",");
484 ret = ibm->write(kernbuf);
485 if (ret == 0)
486 ret = count;
487
488 kfree(kernbuf);
489
490 return ret;
491}
492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493static char *next_cmd(char **cmds)
494{
495 char *start = *cmds;
496 char *end;
497
498 while ((end = strchr(start, ',')) && end == start)
499 start = end + 1;
500
501 if (!end)
502 return NULL;
503
504 *end = 0;
505 *cmds = end + 1;
506 return start;
507}
508
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300509
510/****************************************************************************
511 ****************************************************************************
512 *
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300513 * Device model: hwmon and platform
514 *
515 ****************************************************************************
516 ****************************************************************************/
517
518static struct platform_device *tpacpi_pdev = NULL;
519static struct class_device *tpacpi_hwmon = NULL;
520
521static struct platform_driver tpacpi_pdriver = {
522 .driver = {
523 .name = IBM_DRVR_NAME,
524 .owner = THIS_MODULE,
525 },
526};
527
528
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -0300529/*************************************************************************
530 * thinkpad-acpi driver attributes
531 */
532
533/* interface_version --------------------------------------------------- */
534static ssize_t tpacpi_driver_interface_version_show(
535 struct device_driver *drv,
536 char *buf)
537{
538 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
539}
540
541static DRIVER_ATTR(interface_version, S_IRUGO,
542 tpacpi_driver_interface_version_show, NULL);
543
544/* debug_level --------------------------------------------------------- */
545static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
546 char *buf)
547{
548 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
549}
550
551static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
552 const char *buf, size_t count)
553{
554 unsigned long t;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -0300555
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300556 if (parse_strtoul(buf, 0xffff, &t))
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -0300557 return -EINVAL;
558
559 dbg_level = t;
560
561 return count;
562}
563
564static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
565 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
566
567/* version ------------------------------------------------------------- */
568static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
569 char *buf)
570{
571 return snprintf(buf, PAGE_SIZE, "%s v%s\n", IBM_DESC, IBM_VERSION);
572}
573
574static DRIVER_ATTR(version, S_IRUGO,
575 tpacpi_driver_version_show, NULL);
576
577/* --------------------------------------------------------------------- */
578
579static struct driver_attribute* tpacpi_driver_attributes[] = {
580 &driver_attr_debug_level, &driver_attr_version,
581 &driver_attr_interface_version,
582};
583
584static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
585{
586 int i, res;
587
588 i = 0;
589 res = 0;
590 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
591 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
592 i++;
593 }
594
595 return res;
596}
597
598static void tpacpi_remove_driver_attributes(struct device_driver *drv)
599{
600 int i;
601
602 for(i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
603 driver_remove_file(drv, tpacpi_driver_attributes[i]);
604}
605
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300606/*************************************************************************
607 * sysfs support helpers
608 */
609
610struct attribute_set_obj {
611 struct attribute_set s;
612 struct attribute *a;
613} __attribute__((packed));
614
615static struct attribute_set *create_attr_set(unsigned int max_members,
616 const char* name)
617{
618 struct attribute_set_obj *sobj;
619
620 if (max_members == 0)
621 return NULL;
622
623 /* Allocates space for implicit NULL at the end too */
624 sobj = kzalloc(sizeof(struct attribute_set_obj) +
625 max_members * sizeof(struct attribute *),
626 GFP_KERNEL);
627 if (!sobj)
628 return NULL;
629 sobj->s.max_members = max_members;
630 sobj->s.group.attrs = &sobj->a;
631 sobj->s.group.name = name;
632
633 return &sobj->s;
634}
635
636/* not multi-threaded safe, use it in a single thread per set */
637static int add_to_attr_set(struct attribute_set* s, struct attribute *attr)
638{
639 if (!s || !attr)
640 return -EINVAL;
641
642 if (s->members >= s->max_members)
643 return -ENOMEM;
644
645 s->group.attrs[s->members] = attr;
646 s->members++;
647
648 return 0;
649}
650
651static int add_many_to_attr_set(struct attribute_set* s,
652 struct attribute **attr,
653 unsigned int count)
654{
655 int i, res;
656
657 for (i = 0; i < count; i++) {
658 res = add_to_attr_set(s, attr[i]);
659 if (res)
660 return res;
661 }
662
663 return 0;
664}
665
666static void delete_attr_set(struct attribute_set* s, struct kobject *kobj)
667{
668 sysfs_remove_group(kobj, &s->group);
669 destroy_attr_set(s);
670}
671
672static int parse_strtoul(const char *buf,
673 unsigned long max, unsigned long *value)
674{
675 char *endp;
676
677 *value = simple_strtoul(buf, &endp, 0);
678 while (*endp && isspace(*endp))
679 endp++;
680 if (*endp || *value > max)
681 return -EINVAL;
682
683 return 0;
684}
685
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300686/****************************************************************************
687 ****************************************************************************
688 *
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300689 * Subdrivers
690 *
691 ****************************************************************************
692 ****************************************************************************/
693
694/*************************************************************************
Henrique de Moraes Holschuh142cfc92007-04-21 11:08:26 -0300695 * thinkpad-acpi init subdriver
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300696 */
697
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300698static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{
700 printk(IBM_INFO "%s v%s\n", IBM_DESC, IBM_VERSION);
701 printk(IBM_INFO "%s\n", IBM_URL);
702
Henrique de Moraes Holschuh3945ac32007-02-06 19:13:44 -0200703 if (ibm_thinkpad_ec_found)
704 printk(IBM_INFO "ThinkPad EC firmware %s\n",
705 ibm_thinkpad_ec_found);
706
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 return 0;
708}
709
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300710static int thinkpad_acpi_driver_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
712 int len = 0;
713
714 len += sprintf(p + len, "driver:\t\t%s\n", IBM_DESC);
715 len += sprintf(p + len, "version:\t%s\n", IBM_VERSION);
716
717 return len;
718}
719
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300720static struct ibm_struct thinkpad_acpi_driver_data = {
721 .name = "driver",
722 .read = thinkpad_acpi_driver_read,
723};
724
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300725/*************************************************************************
726 * Hotkey subdriver
727 */
728
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400729static int hotkey_orig_status;
730static int hotkey_orig_mask;
731
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300732static struct attribute_set *hotkey_dev_attributes = NULL;
733
734/* sysfs hotkey enable ------------------------------------------------- */
735static ssize_t hotkey_enable_show(struct device *dev,
736 struct device_attribute *attr,
737 char *buf)
738{
739 int res, status, mask;
740
741 res = hotkey_get(&status, &mask);
742 if (res)
743 return res;
744
745 return snprintf(buf, PAGE_SIZE, "%d\n", status);
746}
747
748static ssize_t hotkey_enable_store(struct device *dev,
749 struct device_attribute *attr,
750 const char *buf, size_t count)
751{
752 unsigned long t;
753 int res, status, mask;
754
755 if (parse_strtoul(buf, 1, &t))
756 return -EINVAL;
757
758 res = hotkey_get(&status, &mask);
759 if (!res)
760 res = hotkey_set(t, mask);
761
762 return (res) ? res : count;
763}
764
765static struct device_attribute dev_attr_hotkey_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -0300766 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300767 hotkey_enable_show, hotkey_enable_store);
768
769/* sysfs hotkey mask --------------------------------------------------- */
770static ssize_t hotkey_mask_show(struct device *dev,
771 struct device_attribute *attr,
772 char *buf)
773{
774 int res, status, mask;
775
776 res = hotkey_get(&status, &mask);
777 if (res)
778 return res;
779
780 return snprintf(buf, PAGE_SIZE, "0x%04x\n", mask);
781}
782
783static ssize_t hotkey_mask_store(struct device *dev,
784 struct device_attribute *attr,
785 const char *buf, size_t count)
786{
787 unsigned long t;
788 int res, status, mask;
789
790 if (parse_strtoul(buf, 0xffff, &t))
791 return -EINVAL;
792
793 res = hotkey_get(&status, &mask);
794 if (!res)
795 hotkey_set(status, t);
796
797 return (res) ? res : count;
798}
799
800static struct device_attribute dev_attr_hotkey_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -0300801 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300802 hotkey_mask_show, hotkey_mask_store);
803
804/* sysfs hotkey bios_enabled ------------------------------------------- */
805static ssize_t hotkey_bios_enabled_show(struct device *dev,
806 struct device_attribute *attr,
807 char *buf)
808{
809 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
810}
811
812static struct device_attribute dev_attr_hotkey_bios_enabled =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -0300813 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300814
815/* sysfs hotkey bios_mask ---------------------------------------------- */
816static ssize_t hotkey_bios_mask_show(struct device *dev,
817 struct device_attribute *attr,
818 char *buf)
819{
820 return snprintf(buf, PAGE_SIZE, "0x%04x\n", hotkey_orig_mask);
821}
822
823static struct device_attribute dev_attr_hotkey_bios_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -0300824 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300825
826/* --------------------------------------------------------------------- */
827
828static struct attribute *hotkey_mask_attributes[] = {
829 &dev_attr_hotkey_mask.attr,
830 &dev_attr_hotkey_bios_enabled.attr,
831 &dev_attr_hotkey_bios_mask.attr,
832};
833
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300834static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300835{
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300836 int res;
837
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300838 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
839
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300840 IBM_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -0300841 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300842
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300843 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300844 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300845
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300846 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300847 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300848
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300849 if (tp_features.hotkey) {
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -0300850 hotkey_dev_attributes = create_attr_set(4, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300851 if (!hotkey_dev_attributes)
852 return -ENOMEM;
853 res = add_to_attr_set(hotkey_dev_attributes,
854 &dev_attr_hotkey_enable.attr);
855 if (res)
856 return res;
857
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300858 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
859 A30, R30, R31, T20-22, X20-21, X22-24 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300860 tp_features.hotkey_mask =
861 acpi_evalf(hkey_handle, NULL, "DHKN", "qv");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300862
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300863 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300864 str_supported(tp_features.hotkey_mask));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300865
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300866 res = hotkey_get(&hotkey_orig_status, &hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300867 if (!res && tp_features.hotkey_mask) {
868 res = add_many_to_attr_set(hotkey_dev_attributes,
869 hotkey_mask_attributes,
870 ARRAY_SIZE(hotkey_mask_attributes));
871 }
872 if (!res)
873 res = register_attr_set_with_sysfs(
874 hotkey_dev_attributes,
875 &tpacpi_pdev->dev.kobj);
876
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300877 if (res)
878 return res;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300879 }
880
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300881 return (tp_features.hotkey)? 0 : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300882}
883
884static void hotkey_exit(void)
885{
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300886 int res;
887
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300888 if (tp_features.hotkey) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300889 dbg_printk(TPACPI_DBG_EXIT, "restoring original hotkey mask\n");
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300890 res = hotkey_set(hotkey_orig_status, hotkey_orig_mask);
891 if (res)
892 printk(IBM_ERR "failed to restore hotkey to BIOS defaults\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300893 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300894
895 if (hotkey_dev_attributes) {
896 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
897 hotkey_dev_attributes = NULL;
898 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300899}
900
901static void hotkey_notify(struct ibm_struct *ibm, u32 event)
902{
903 int hkey;
904
905 if (acpi_evalf(hkey_handle, &hkey, "MHKP", "d"))
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300906 acpi_bus_generate_event(ibm->acpi->device, event, hkey);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300907 else {
908 printk(IBM_ERR "unknown hotkey event %d\n", event);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300909 acpi_bus_generate_event(ibm->acpi->device, event, 0);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300910 }
911}
912
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -0300913/*
914 * Call with hotkey_mutex held
915 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400916static int hotkey_get(int *status, int *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917{
918 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300919 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400920
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300921 if (tp_features.hotkey_mask)
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400922 if (!acpi_evalf(hkey_handle, mask, "DHKN", "d"))
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300923 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400924
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300925 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926}
927
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -0300928/*
929 * Call with hotkey_mutex held
930 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400931static int hotkey_set(int status, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932{
933 int i;
934
935 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300936 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300938 if (tp_features.hotkey_mask)
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400939 for (i = 0; i < 32; i++) {
940 int bit = ((1 << i) & mask) != 0;
941 if (!acpi_evalf(hkey_handle,
942 NULL, "MHKM", "vdd", i + 1, bit))
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300943 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400944 }
945
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300946 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400947}
948
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300949/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400950static int hotkey_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951{
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300952 int res, status, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 int len = 0;
954
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300955 if (!tp_features.hotkey) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400956 len += sprintf(p + len, "status:\t\tnot supported\n");
957 return len;
958 }
959
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -0300960 res = mutex_lock_interruptible(&hotkey_mutex);
961 if (res < 0)
962 return res;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300963 res = hotkey_get(&status, &mask);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -0300964 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300965 if (res)
966 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
968 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300969 if (tp_features.hotkey_mask) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 len += sprintf(p + len, "mask:\t\t0x%04x\n", mask);
971 len += sprintf(p + len,
972 "commands:\tenable, disable, reset, <mask>\n");
973 } else {
974 len += sprintf(p + len, "mask:\t\tnot supported\n");
975 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
976 }
977
978 return len;
979}
980
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400981static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982{
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300983 int res, status, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 char *cmd;
985 int do_cmd = 0;
986
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300987 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 return -ENODEV;
989
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -0300990 res = mutex_lock_interruptible(&hotkey_mutex);
991 if (res < 0)
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300992 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400993
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -0300994 res = hotkey_get(&status, &mask);
995 if (res)
996 goto errexit;
997
998 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 while ((cmd = next_cmd(&buf))) {
1000 if (strlencmp(cmd, "enable") == 0) {
1001 status = 1;
1002 } else if (strlencmp(cmd, "disable") == 0) {
1003 status = 0;
1004 } else if (strlencmp(cmd, "reset") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001005 status = hotkey_orig_status;
1006 mask = hotkey_orig_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
1008 /* mask set */
1009 } else if (sscanf(cmd, "%x", &mask) == 1) {
1010 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001011 } else {
1012 res = -EINVAL;
1013 goto errexit;
1014 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 do_cmd = 1;
1016 }
1017
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001018 if (do_cmd)
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001019 res = hotkey_set(status, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001021errexit:
1022 mutex_unlock(&hotkey_mutex);
1023 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001024}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001026static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001027 .hid = IBM_HKEY_HID,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001028 .notify = hotkey_notify,
1029 .handle = &hkey_handle,
1030 .type = ACPI_DEVICE_NOTIFY,
1031};
1032
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001033static struct ibm_struct hotkey_driver_data = {
1034 .name = "hotkey",
1035 .read = hotkey_read,
1036 .write = hotkey_write,
1037 .exit = hotkey_exit,
1038 .acpi = &ibm_hotkey_acpidriver,
1039};
1040
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001041/*************************************************************************
1042 * Bluetooth subdriver
1043 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001045/* sysfs bluetooth enable ---------------------------------------------- */
1046static ssize_t bluetooth_enable_show(struct device *dev,
1047 struct device_attribute *attr,
1048 char *buf)
1049{
1050 int status;
1051
1052 status = bluetooth_get_radiosw();
1053 if (status < 0)
1054 return status;
1055
1056 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
1057}
1058
1059static ssize_t bluetooth_enable_store(struct device *dev,
1060 struct device_attribute *attr,
1061 const char *buf, size_t count)
1062{
1063 unsigned long t;
1064 int res;
1065
1066 if (parse_strtoul(buf, 1, &t))
1067 return -EINVAL;
1068
1069 res = bluetooth_set_radiosw(t);
1070
1071 return (res) ? res : count;
1072}
1073
1074static struct device_attribute dev_attr_bluetooth_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001075 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001076 bluetooth_enable_show, bluetooth_enable_store);
1077
1078/* --------------------------------------------------------------------- */
1079
1080static struct attribute *bluetooth_attributes[] = {
1081 &dev_attr_bluetooth_enable.attr,
1082 NULL
1083};
1084
1085static const struct attribute_group bluetooth_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001086 .attrs = bluetooth_attributes,
1087};
1088
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001089static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001091 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001092 int status = 0;
1093
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001094 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
1095
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001096 IBM_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001097
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001098 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1099 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001100 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001101 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001103 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
1104 str_supported(tp_features.bluetooth),
1105 status);
1106
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001107 if (tp_features.bluetooth) {
1108 if (!(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
1109 /* no bluetooth hardware present in system */
1110 tp_features.bluetooth = 0;
1111 dbg_printk(TPACPI_DBG_INIT,
1112 "bluetooth hardware not installed\n");
1113 } else {
1114 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
1115 &bluetooth_attr_group);
1116 if (res)
1117 return res;
1118 }
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001119 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001120
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001121 return (tp_features.bluetooth)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122}
1123
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001124static void bluetooth_exit(void)
1125{
1126 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
1127 &bluetooth_attr_group);
1128}
1129
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001130static int bluetooth_get_radiosw(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131{
1132 int status;
1133
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001134 if (!tp_features.bluetooth)
1135 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001137 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
1138 return -EIO;
1139
1140 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
1141}
1142
1143static int bluetooth_set_radiosw(int radio_on)
1144{
1145 int status;
1146
1147 if (!tp_features.bluetooth)
1148 return -ENODEV;
1149
1150 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
1151 return -EIO;
1152 if (radio_on)
1153 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
1154 else
1155 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
1156 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
1157 return -EIO;
1158
1159 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160}
1161
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001162/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001163static int bluetooth_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164{
1165 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001166 int status = bluetooth_get_radiosw();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001168 if (!tp_features.bluetooth)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 len += sprintf(p + len, "status:\t\tnot supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001171 len += sprintf(p + len, "status:\t\t%s\n",
1172 (status)? "enabled" : "disabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 len += sprintf(p + len, "commands:\tenable, disable\n");
1174 }
1175
1176 return len;
1177}
1178
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001179static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001183 if (!tp_features.bluetooth)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001184 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
1186 while ((cmd = next_cmd(&buf))) {
1187 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001188 bluetooth_set_radiosw(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001190 bluetooth_set_radiosw(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 } else
1192 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 }
1194
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 return 0;
1196}
1197
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001198static struct ibm_struct bluetooth_driver_data = {
1199 .name = "bluetooth",
1200 .read = bluetooth_read,
1201 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001202 .exit = bluetooth_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001203};
1204
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001205/*************************************************************************
1206 * Wan subdriver
1207 */
1208
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001209/* sysfs wan enable ---------------------------------------------------- */
1210static ssize_t wan_enable_show(struct device *dev,
1211 struct device_attribute *attr,
1212 char *buf)
1213{
1214 int status;
1215
1216 status = wan_get_radiosw();
1217 if (status < 0)
1218 return status;
1219
1220 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
1221}
1222
1223static ssize_t wan_enable_store(struct device *dev,
1224 struct device_attribute *attr,
1225 const char *buf, size_t count)
1226{
1227 unsigned long t;
1228 int res;
1229
1230 if (parse_strtoul(buf, 1, &t))
1231 return -EINVAL;
1232
1233 res = wan_set_radiosw(t);
1234
1235 return (res) ? res : count;
1236}
1237
1238static struct device_attribute dev_attr_wan_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001239 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001240 wan_enable_show, wan_enable_store);
1241
1242/* --------------------------------------------------------------------- */
1243
1244static struct attribute *wan_attributes[] = {
1245 &dev_attr_wan_enable.attr,
1246 NULL
1247};
1248
1249static const struct attribute_group wan_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001250 .attrs = wan_attributes,
1251};
1252
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001253static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001254{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001255 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001256 int status = 0;
1257
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001258 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
1259
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001260 IBM_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001261
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001262 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001263 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001264
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001265 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
1266 str_supported(tp_features.wan),
1267 status);
1268
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001269 if (tp_features.wan) {
1270 if (!(status & TP_ACPI_WANCARD_HWPRESENT)) {
1271 /* no wan hardware present in system */
1272 tp_features.wan = 0;
1273 dbg_printk(TPACPI_DBG_INIT,
1274 "wan hardware not installed\n");
1275 } else {
1276 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
1277 &wan_attr_group);
1278 if (res)
1279 return res;
1280 }
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001281 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001282
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001283 return (tp_features.wan)? 0 : 1;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001284}
1285
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001286static void wan_exit(void)
1287{
1288 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
1289 &wan_attr_group);
1290}
1291
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001292static int wan_get_radiosw(void)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001293{
1294 int status;
1295
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001296 if (!tp_features.wan)
1297 return -ENODEV;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001298
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001299 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
1300 return -EIO;
1301
1302 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
1303}
1304
1305static int wan_set_radiosw(int radio_on)
1306{
1307 int status;
1308
1309 if (!tp_features.wan)
1310 return -ENODEV;
1311
1312 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
1313 return -EIO;
1314 if (radio_on)
1315 status |= TP_ACPI_WANCARD_RADIOSSW;
1316 else
1317 status &= ~TP_ACPI_WANCARD_RADIOSSW;
1318 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
1319 return -EIO;
1320
1321 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001322}
1323
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001324/* procfs -------------------------------------------------------------- */
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001325static int wan_read(char *p)
1326{
1327 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001328 int status = wan_get_radiosw();
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001329
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001330 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001331 len += sprintf(p + len, "status:\t\tnot supported\n");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001332 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001333 len += sprintf(p + len, "status:\t\t%s\n",
1334 (status)? "enabled" : "disabled");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001335 len += sprintf(p + len, "commands:\tenable, disable\n");
1336 }
1337
1338 return len;
1339}
1340
1341static int wan_write(char *buf)
1342{
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001343 char *cmd;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001344
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001345 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001346 return -ENODEV;
1347
1348 while ((cmd = next_cmd(&buf))) {
1349 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001350 wan_set_radiosw(1);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001351 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001352 wan_set_radiosw(0);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001353 } else
1354 return -EINVAL;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001355 }
1356
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001357 return 0;
1358}
1359
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001360static struct ibm_struct wan_driver_data = {
1361 .name = "wan",
1362 .read = wan_read,
1363 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001364 .exit = wan_exit,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03001365 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001366};
1367
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001368/*************************************************************************
1369 * Video subdriver
1370 */
1371
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02001372static enum video_access_mode video_supported;
1373static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001374
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001375IBM_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
1376 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
1377 "\\_SB.PCI0.VID0", /* 770e */
1378 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
1379 "\\_SB.PCI0.AGP.VID", /* all others */
1380 ); /* R30, R31 */
1381
1382IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
1383
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001384static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001386 int ivga;
1387
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001388 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
1389
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001390 IBM_ACPIHANDLE_INIT(vid);
1391 IBM_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001392
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001393 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
1394 /* G41, assume IVGA doesn't change */
1395 vid_handle = vid2_handle;
1396
1397 if (!vid_handle)
1398 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001399 video_supported = TPACPI_VIDEO_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001400 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
1401 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001402 video_supported = TPACPI_VIDEO_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001403 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
1404 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001405 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001406 else
1407 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001408 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001410 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
1411 str_supported(video_supported != TPACPI_VIDEO_NONE),
1412 video_supported);
1413
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001414 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415}
1416
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001417static void video_exit(void)
1418{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001419 dbg_printk(TPACPI_DBG_EXIT,
1420 "restoring original video autoswitch mode\n");
1421 if (video_autosw_set(video_orig_autosw))
1422 printk(IBM_ERR "error while trying to restore original "
1423 "video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001424}
1425
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001426static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427{
1428 int status = 0;
1429 int i;
1430
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001431 switch (video_supported) {
1432 case TPACPI_VIDEO_570:
1433 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
1434 TP_ACPI_VIDEO_570_PHSCMD))
1435 return -EIO;
1436 status = i & TP_ACPI_VIDEO_570_PHSMASK;
1437 break;
1438 case TPACPI_VIDEO_770:
1439 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
1440 return -EIO;
1441 if (i)
1442 status |= TP_ACPI_VIDEO_S_LCD;
1443 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
1444 return -EIO;
1445 if (i)
1446 status |= TP_ACPI_VIDEO_S_CRT;
1447 break;
1448 case TPACPI_VIDEO_NEW:
1449 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
1450 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
1451 return -EIO;
1452 if (i)
1453 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001455 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
1456 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
1457 return -EIO;
1458 if (i)
1459 status |= TP_ACPI_VIDEO_S_LCD;
1460 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
1461 return -EIO;
1462 if (i)
1463 status |= TP_ACPI_VIDEO_S_DVI;
1464 break;
1465 default:
1466 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001467 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
1469 return status;
1470}
1471
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001472static int video_outputsw_set(int status)
1473{
1474 int autosw;
1475 int res = 0;
1476
1477 switch (video_supported) {
1478 case TPACPI_VIDEO_570:
1479 res = acpi_evalf(NULL, NULL,
1480 "\\_SB.PHS2", "vdd",
1481 TP_ACPI_VIDEO_570_PHS2CMD,
1482 status | TP_ACPI_VIDEO_570_PHS2SET);
1483 break;
1484 case TPACPI_VIDEO_770:
1485 autosw = video_autosw_get();
1486 if (autosw < 0)
1487 return autosw;
1488
1489 res = video_autosw_set(1);
1490 if (res)
1491 return res;
1492 res = acpi_evalf(vid_handle, NULL,
1493 "ASWT", "vdd", status * 0x100, 0);
1494 if (!autosw && video_autosw_set(autosw)) {
1495 printk(IBM_ERR "video auto-switch left enabled due to error\n");
1496 return -EIO;
1497 }
1498 break;
1499 case TPACPI_VIDEO_NEW:
1500 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
1501 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
1502 break;
1503 default:
1504 return -ENOSYS;
1505 }
1506
1507 return (res)? 0 : -EIO;
1508}
1509
1510static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001512 int autosw = 0;
1513
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001514 switch (video_supported) {
1515 case TPACPI_VIDEO_570:
1516 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
1517 return -EIO;
1518 break;
1519 case TPACPI_VIDEO_770:
1520 case TPACPI_VIDEO_NEW:
1521 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
1522 return -EIO;
1523 break;
1524 default:
1525 return -ENOSYS;
1526 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001527
1528 return autosw & 1;
1529}
1530
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001531static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001532{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001533 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001534 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001535 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001536}
1537
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001538static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001539{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001540 int autosw = video_autosw_get();
1541 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001542
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001543 if (autosw < 0)
1544 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001545
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001546 switch (video_supported) {
1547 case TPACPI_VIDEO_570:
1548 res = video_autosw_set(1);
1549 if (res)
1550 return res;
1551 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
1552 break;
1553 case TPACPI_VIDEO_770:
1554 case TPACPI_VIDEO_NEW:
1555 res = video_autosw_set(1);
1556 if (res)
1557 return res;
1558 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
1559 break;
1560 default:
1561 return -ENOSYS;
1562 }
1563 if (!autosw && video_autosw_set(autosw)) {
1564 printk(IBM_ERR "video auto-switch left enabled due to error\n");
1565 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001566 }
1567
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001568 return (res)? 0 : -EIO;
1569}
1570
1571static int video_expand_toggle(void)
1572{
1573 switch (video_supported) {
1574 case TPACPI_VIDEO_570:
1575 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
1576 0 : -EIO;
1577 case TPACPI_VIDEO_770:
1578 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
1579 0 : -EIO;
1580 case TPACPI_VIDEO_NEW:
1581 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
1582 0 : -EIO;
1583 default:
1584 return -ENOSYS;
1585 }
1586 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001587}
1588
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001589static int video_read(char *p)
1590{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001591 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001592 int len = 0;
1593
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001594 if (video_supported == TPACPI_VIDEO_NONE) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001595 len += sprintf(p + len, "status:\t\tnot supported\n");
1596 return len;
1597 }
1598
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001599 status = video_outputsw_get();
1600 if (status < 0)
1601 return status;
1602
1603 autosw = video_autosw_get();
1604 if (autosw < 0)
1605 return autosw;
1606
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001607 len += sprintf(p + len, "status:\t\tsupported\n");
1608 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
1609 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001610 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001611 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
1612 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
1613 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
1614 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001615 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001616 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
1617 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
1618 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
1619
1620 return len;
1621}
1622
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001623static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624{
1625 char *cmd;
1626 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001627 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001629 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001630 return -ENODEV;
1631
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001632 enable = 0;
1633 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
1635 while ((cmd = next_cmd(&buf))) {
1636 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001637 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001639 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001641 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001643 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001644 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001645 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001646 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001647 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001648 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001649 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001651 res = video_autosw_set(1);
1652 if (res)
1653 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001655 res = video_autosw_set(0);
1656 if (res)
1657 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001659 res = video_outputsw_cycle();
1660 if (res)
1661 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001663 res = video_expand_toggle();
1664 if (res)
1665 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 } else
1667 return -EINVAL;
1668 }
1669
1670 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001671 status = video_outputsw_get();
1672 if (status < 0)
1673 return status;
1674 res = video_outputsw_set((status & ~disable) | enable);
1675 if (res)
1676 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 }
1678
1679 return 0;
1680}
1681
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001682static struct ibm_struct video_driver_data = {
1683 .name = "video",
1684 .read = video_read,
1685 .write = video_write,
1686 .exit = video_exit,
1687};
1688
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001689/*************************************************************************
1690 * Light (thinklight) subdriver
1691 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001693IBM_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
1694IBM_HANDLE(ledb, ec, "LEDB"); /* G4x */
1695
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001696static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001698 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
1699
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001700 IBM_ACPIHANDLE_INIT(ledb);
1701 IBM_ACPIHANDLE_INIT(lght);
1702 IBM_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001703
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001704 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001705 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001706
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001707 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001708 /* light status not supported on
1709 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001710 tp_features.light_status =
1711 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001713 vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001714 str_supported(tp_features.light));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001715
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001716 return (tp_features.light)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717}
1718
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001719static int light_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720{
1721 int len = 0;
1722 int status = 0;
1723
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001724 if (!tp_features.light) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001725 len += sprintf(p + len, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001726 } else if (!tp_features.light_status) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001727 len += sprintf(p + len, "status:\t\tunknown\n");
1728 len += sprintf(p + len, "commands:\ton, off\n");
1729 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
1731 return -EIO;
1732 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001733 len += sprintf(p + len, "commands:\ton, off\n");
1734 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
1736 return len;
1737}
1738
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001739static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740{
1741 int cmos_cmd, lght_cmd;
1742 char *cmd;
1743 int success;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001744
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001745 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001746 return -ENODEV;
1747
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 while ((cmd = next_cmd(&buf))) {
1749 if (strlencmp(cmd, "on") == 0) {
1750 cmos_cmd = 0x0c;
1751 lght_cmd = 1;
1752 } else if (strlencmp(cmd, "off") == 0) {
1753 cmos_cmd = 0x0d;
1754 lght_cmd = 0;
1755 } else
1756 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001757
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 success = cmos_handle ?
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001759 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
1760 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 if (!success)
1762 return -EIO;
1763 }
1764
1765 return 0;
1766}
1767
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001768static struct ibm_struct light_driver_data = {
1769 .name = "light",
1770 .read = light_read,
1771 .write = light_write,
1772};
1773
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001774/*************************************************************************
1775 * Dock subdriver
1776 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03001778#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001779
1780IBM_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
1781 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
1782 "\\_SB.PCI0.PCI1.DOCK", /* all others */
1783 "\\_SB.PCI.ISA.SLCE", /* 570 */
1784 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
1785
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001786/* don't list other alternatives as we install a notify handler on the 570 */
1787IBM_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
1788
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03001789static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
1790 {
1791 .notify = dock_notify,
1792 .handle = &dock_handle,
1793 .type = ACPI_SYSTEM_NOTIFY,
1794 },
1795 {
1796 .hid = IBM_PCI_HID,
1797 .notify = dock_notify,
1798 .handle = &pci_handle,
1799 .type = ACPI_SYSTEM_NOTIFY,
1800 },
1801};
1802
1803static struct ibm_struct dock_driver_data[2] = {
1804 {
1805 .name = "dock",
1806 .read = dock_read,
1807 .write = dock_write,
1808 .acpi = &ibm_dock_acpidriver[0],
1809 },
1810 {
1811 .name = "dock",
1812 .acpi = &ibm_dock_acpidriver[1],
1813 },
1814};
1815
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816#define dock_docked() (_sta(dock_handle) & 1)
1817
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001818static int __init dock_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001819{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001820 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
1821
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001822 IBM_ACPIHANDLE_INIT(dock);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001823
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001824 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
1825 str_supported(dock_handle != NULL));
1826
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001827 return (dock_handle)? 0 : 1;
1828}
1829
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03001830static int __init dock_init2(struct ibm_init_struct *iibm)
1831{
1832 int dock2_needed;
1833
1834 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
1835
1836 if (dock_driver_data[0].flags.acpi_driver_registered &&
1837 dock_driver_data[0].flags.acpi_notify_installed) {
1838 IBM_ACPIHANDLE_INIT(pci);
1839 dock2_needed = (pci_handle != NULL);
1840 vdbg_printk(TPACPI_DBG_INIT,
1841 "dock PCI handler for the TP 570 is %s\n",
1842 str_supported(dock2_needed));
1843 } else {
1844 vdbg_printk(TPACPI_DBG_INIT,
1845 "dock subdriver part 2 not required\n");
1846 dock2_needed = 0;
1847 }
1848
1849 return (dock2_needed)? 0 : 1;
1850}
1851
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001852static void dock_notify(struct ibm_struct *ibm, u32 event)
1853{
1854 int docked = dock_docked();
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001855 int pci = ibm->acpi->hid && strstr(ibm->acpi->hid, IBM_PCI_HID);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001856
1857 if (event == 1 && !pci) /* 570 */
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001858 acpi_bus_generate_event(ibm->acpi->device, event, 1); /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001859 else if (event == 1 && pci) /* 570 */
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001860 acpi_bus_generate_event(ibm->acpi->device, event, 3); /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001861 else if (event == 3 && docked)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001862 acpi_bus_generate_event(ibm->acpi->device, event, 1); /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001863 else if (event == 3 && !docked)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001864 acpi_bus_generate_event(ibm->acpi->device, event, 2); /* undock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001865 else if (event == 0 && docked)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001866 acpi_bus_generate_event(ibm->acpi->device, event, 3); /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001867 else {
1868 printk(IBM_ERR "unknown dock event %d, status %d\n",
1869 event, _sta(dock_handle));
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001870 acpi_bus_generate_event(ibm->acpi->device, event, 0); /* unknown */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001871 }
1872}
1873
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001874static int dock_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875{
1876 int len = 0;
1877 int docked = dock_docked();
1878
1879 if (!dock_handle)
1880 len += sprintf(p + len, "status:\t\tnot supported\n");
1881 else if (!docked)
1882 len += sprintf(p + len, "status:\t\tundocked\n");
1883 else {
1884 len += sprintf(p + len, "status:\t\tdocked\n");
1885 len += sprintf(p + len, "commands:\tdock, undock\n");
1886 }
1887
1888 return len;
1889}
1890
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001891static int dock_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892{
1893 char *cmd;
1894
1895 if (!dock_docked())
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001896 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897
1898 while ((cmd = next_cmd(&buf))) {
1899 if (strlencmp(cmd, "undock") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001900 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
1901 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 return -EIO;
1903 } else if (strlencmp(cmd, "dock") == 0) {
1904 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
1905 return -EIO;
1906 } else
1907 return -EINVAL;
1908 }
1909
1910 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001911}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03001913#endif /* CONFIG_THINKPAD_ACPI_DOCK */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001915/*************************************************************************
1916 * Bay subdriver
1917 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03001919#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001920IBM_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
1921 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
1922 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
1923 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
1924 ); /* A21e, R30, R31 */
1925IBM_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
1926 "_EJ0", /* all others */
1927 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
1928IBM_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
1929 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
1930 ); /* all others */
1931IBM_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
1932 "_EJ0", /* 770x */
1933 ); /* all others */
1934
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001935static int __init bay_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001937 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
1938
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001939 IBM_ACPIHANDLE_INIT(bay);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001940 if (bay_handle)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001941 IBM_ACPIHANDLE_INIT(bay_ej);
1942 IBM_ACPIHANDLE_INIT(bay2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001943 if (bay2_handle)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001944 IBM_ACPIHANDLE_INIT(bay2_ej);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001945
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001946 tp_features.bay_status = bay_handle &&
1947 acpi_evalf(bay_handle, NULL, "_STA", "qv");
1948 tp_features.bay_status2 = bay2_handle &&
1949 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001950
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001951 tp_features.bay_eject = bay_handle && bay_ej_handle &&
1952 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
1953 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
1954 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001956 vdbg_printk(TPACPI_DBG_INIT,
1957 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001958 str_supported(tp_features.bay_status),
1959 str_supported(tp_features.bay_eject),
1960 str_supported(tp_features.bay_status2),
1961 str_supported(tp_features.bay_eject2));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001962
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001963 return (tp_features.bay_status || tp_features.bay_eject ||
1964 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965}
1966
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001967static void bay_notify(struct ibm_struct *ibm, u32 event)
1968{
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001969 acpi_bus_generate_event(ibm->acpi->device, event, 0);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001970}
1971
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001972#define bay_occupied(b) (_sta(b##_handle) & 1)
1973
1974static int bay_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975{
1976 int len = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001977 int occupied = bay_occupied(bay);
1978 int occupied2 = bay_occupied(bay2);
1979 int eject, eject2;
1980
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001981 len += sprintf(p + len, "status:\t\t%s\n",
1982 tp_features.bay_status ?
1983 (occupied ? "occupied" : "unoccupied") :
1984 "not supported");
1985 if (tp_features.bay_status2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001986 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
1987 "occupied" : "unoccupied");
1988
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001989 eject = tp_features.bay_eject && occupied;
1990 eject2 = tp_features.bay_eject2 && occupied2;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001991
1992 if (eject && eject2)
1993 len += sprintf(p + len, "commands:\teject, eject2\n");
1994 else if (eject)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 len += sprintf(p + len, "commands:\teject\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001996 else if (eject2)
1997 len += sprintf(p + len, "commands:\teject2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998
1999 return len;
2000}
2001
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002002static int bay_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003{
2004 char *cmd;
2005
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002006 if (!tp_features.bay_eject && !tp_features.bay_eject2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002007 return -ENODEV;
2008
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002010 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002011 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
2012 return -EIO;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002013 } else if (tp_features.bay_eject2 &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002014 strlencmp(cmd, "eject2") == 0) {
2015 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 return -EIO;
2017 } else
2018 return -EINVAL;
2019 }
2020
2021 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002022}
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002023
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002024static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
2025 .notify = bay_notify,
2026 .handle = &bay_handle,
2027 .type = ACPI_SYSTEM_NOTIFY,
2028};
2029
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002030static struct ibm_struct bay_driver_data = {
2031 .name = "bay",
2032 .read = bay_read,
2033 .write = bay_write,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002034 .acpi = &ibm_bay_acpidriver,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002035};
2036
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03002037#endif /* CONFIG_THINKPAD_ACPI_BAY */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002039/*************************************************************************
2040 * CMOS subdriver
2041 */
2042
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03002043/* sysfs cmos_command -------------------------------------------------- */
2044static ssize_t cmos_command_store(struct device *dev,
2045 struct device_attribute *attr,
2046 const char *buf, size_t count)
2047{
2048 unsigned long cmos_cmd;
2049 int res;
2050
2051 if (parse_strtoul(buf, 21, &cmos_cmd))
2052 return -EINVAL;
2053
2054 res = issue_thinkpad_cmos_command(cmos_cmd);
2055 return (res)? res : count;
2056}
2057
2058static struct device_attribute dev_attr_cmos_command =
2059 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
2060
2061/* --------------------------------------------------------------------- */
2062
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002063static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002064{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03002065 int res;
2066
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002067 vdbg_printk(TPACPI_DBG_INIT,
2068 "initializing cmos commands subdriver\n");
2069
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002070 IBM_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002071
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002072 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
2073 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03002074
2075 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
2076 if (res)
2077 return res;
2078
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002079 return (cmos_handle)? 0 : 1;
2080}
2081
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03002082static void cmos_exit(void)
2083{
2084 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
2085}
2086
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002087static int cmos_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088{
2089 int len = 0;
2090
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002091 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2092 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 if (!cmos_handle)
2094 len += sprintf(p + len, "status:\t\tnot supported\n");
2095 else {
2096 len += sprintf(p + len, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002097 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 }
2099
2100 return len;
2101}
2102
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002103static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104{
2105 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03002106 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107
2108 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002109 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
2110 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 /* cmos_cmd set */
2112 } else
2113 return -EINVAL;
2114
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03002115 res = issue_thinkpad_cmos_command(cmos_cmd);
2116 if (res)
2117 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 }
2119
2120 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002121}
2122
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002123static struct ibm_struct cmos_driver_data = {
2124 .name = "cmos",
2125 .read = cmos_read,
2126 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03002127 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002128};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002129
2130/*************************************************************************
2131 * LED subdriver
2132 */
2133
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02002134static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002135
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002136IBM_HANDLE(led, ec, "SLED", /* 570 */
2137 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
2138 "LED", /* all others */
2139 ); /* R30, R31 */
2140
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002141static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002142{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002143 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
2144
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002145 IBM_ACPIHANDLE_INIT(led);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002146
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002147 if (!led_handle)
2148 /* led not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002149 led_supported = TPACPI_LED_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002150 else if (strlencmp(led_path, "SLED") == 0)
2151 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002152 led_supported = TPACPI_LED_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002153 else if (strlencmp(led_path, "SYSL") == 0)
2154 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002155 led_supported = TPACPI_LED_OLD;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002156 else
2157 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002158 led_supported = TPACPI_LED_NEW;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002159
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002160 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
2161 str_supported(led_supported), led_supported);
2162
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002163 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002164}
2165
2166#define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
2167
2168static int led_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169{
2170 int len = 0;
2171
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002172 if (!led_supported) {
2173 len += sprintf(p + len, "status:\t\tnot supported\n");
2174 return len;
2175 }
2176 len += sprintf(p + len, "status:\t\tsupported\n");
2177
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002178 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002179 /* 570 */
2180 int i, status;
2181 for (i = 0; i < 8; i++) {
2182 if (!acpi_evalf(ec_handle,
2183 &status, "GLED", "dd", 1 << i))
2184 return -EIO;
2185 len += sprintf(p + len, "%d:\t\t%s\n",
2186 i, led_status(status));
2187 }
2188 }
2189
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 len += sprintf(p + len, "commands:\t"
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002191 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192
2193 return len;
2194}
2195
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002196/* off, on, blink */
2197static const int led_sled_arg1[] = { 0, 1, 3 };
2198static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
2199static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
2200static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
2201
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002202static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203{
2204 char *cmd;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002205 int led, ind, ret;
2206
2207 if (!led_supported)
2208 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
2210 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002211 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 return -EINVAL;
2213
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002214 if (strstr(cmd, "off")) {
2215 ind = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 } else if (strstr(cmd, "on")) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002217 ind = 1;
2218 } else if (strstr(cmd, "blink")) {
2219 ind = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 } else
2221 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002222
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002223 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002224 /* 570 */
2225 led = 1 << led;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002227 led, led_sled_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 return -EIO;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002229 } else if (led_supported == TPACPI_LED_OLD) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002230 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
2231 led = 1 << led;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002232 ret = ec_write(TPACPI_LED_EC_HLMS, led);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002233 if (ret >= 0)
2234 ret =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002235 ec_write(TPACPI_LED_EC_HLBL,
Henrique de Moraes Holschuhe062e032007-03-23 17:33:55 -03002236 led * led_exp_hlbl[ind]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002237 if (ret >= 0)
2238 ret =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002239 ec_write(TPACPI_LED_EC_HLCL,
Henrique de Moraes Holschuhe062e032007-03-23 17:33:55 -03002240 led * led_exp_hlcl[ind]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002241 if (ret < 0)
2242 return ret;
2243 } else {
2244 /* all others */
2245 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
2246 led, led_led_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 }
2250
2251 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002252}
2253
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002254static struct ibm_struct led_driver_data = {
2255 .name = "led",
2256 .read = led_read,
2257 .write = led_write,
2258};
2259
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002260/*************************************************************************
2261 * Beep subdriver
2262 */
2263
2264IBM_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
2265
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002266static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002267{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002268 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
2269
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002270 IBM_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002271
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002272 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
2273 str_supported(beep_handle != NULL));
2274
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002275 return (beep_handle)? 0 : 1;
2276}
2277
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002278static int beep_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279{
2280 int len = 0;
2281
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002282 if (!beep_handle)
2283 len += sprintf(p + len, "status:\t\tnot supported\n");
2284 else {
2285 len += sprintf(p + len, "status:\t\tsupported\n");
2286 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
2287 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288
2289 return len;
2290}
2291
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002292static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293{
2294 char *cmd;
2295 int beep_cmd;
2296
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002297 if (!beep_handle)
2298 return -ENODEV;
2299
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002301 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
2302 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 /* beep_cmd set */
2304 } else
2305 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002306 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 return -EIO;
2308 }
2309
2310 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002311}
2312
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002313static struct ibm_struct beep_driver_data = {
2314 .name = "beep",
2315 .read = beep_read,
2316 .write = beep_write,
2317};
2318
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002319/*************************************************************************
2320 * Thermal subdriver
2321 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002322
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002323static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002324
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03002325/* sysfs temp##_input -------------------------------------------------- */
2326
2327static ssize_t thermal_temp_input_show(struct device *dev,
2328 struct device_attribute *attr,
2329 char *buf)
2330{
2331 struct sensor_device_attribute *sensor_attr =
2332 to_sensor_dev_attr(attr);
2333 int idx = sensor_attr->index;
2334 s32 value;
2335 int res;
2336
2337 res = thermal_get_sensor(idx, &value);
2338 if (res)
2339 return res;
2340 if (value == TP_EC_THERMAL_TMP_NA * 1000)
2341 return -ENXIO;
2342
2343 return snprintf(buf, PAGE_SIZE, "%d\n", value);
2344}
2345
2346#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
2347 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, thermal_temp_input_show, NULL, _idxB)
2348
2349static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
2350 THERMAL_SENSOR_ATTR_TEMP(1, 0),
2351 THERMAL_SENSOR_ATTR_TEMP(2, 1),
2352 THERMAL_SENSOR_ATTR_TEMP(3, 2),
2353 THERMAL_SENSOR_ATTR_TEMP(4, 3),
2354 THERMAL_SENSOR_ATTR_TEMP(5, 4),
2355 THERMAL_SENSOR_ATTR_TEMP(6, 5),
2356 THERMAL_SENSOR_ATTR_TEMP(7, 6),
2357 THERMAL_SENSOR_ATTR_TEMP(8, 7),
2358 THERMAL_SENSOR_ATTR_TEMP(9, 8),
2359 THERMAL_SENSOR_ATTR_TEMP(10, 9),
2360 THERMAL_SENSOR_ATTR_TEMP(11, 10),
2361 THERMAL_SENSOR_ATTR_TEMP(12, 11),
2362 THERMAL_SENSOR_ATTR_TEMP(13, 12),
2363 THERMAL_SENSOR_ATTR_TEMP(14, 13),
2364 THERMAL_SENSOR_ATTR_TEMP(15, 14),
2365 THERMAL_SENSOR_ATTR_TEMP(16, 15),
2366};
2367
2368#define THERMAL_ATTRS(X) \
2369 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
2370
2371static struct attribute *thermal_temp_input_attr[] = {
2372 THERMAL_ATTRS(8),
2373 THERMAL_ATTRS(9),
2374 THERMAL_ATTRS(10),
2375 THERMAL_ATTRS(11),
2376 THERMAL_ATTRS(12),
2377 THERMAL_ATTRS(13),
2378 THERMAL_ATTRS(14),
2379 THERMAL_ATTRS(15),
2380 THERMAL_ATTRS(0),
2381 THERMAL_ATTRS(1),
2382 THERMAL_ATTRS(2),
2383 THERMAL_ATTRS(3),
2384 THERMAL_ATTRS(4),
2385 THERMAL_ATTRS(5),
2386 THERMAL_ATTRS(6),
2387 THERMAL_ATTRS(7),
2388 NULL
2389};
2390
2391static const struct attribute_group thermal_temp_input16_group = {
2392 .attrs = thermal_temp_input_attr
2393};
2394
2395static const struct attribute_group thermal_temp_input8_group = {
2396 .attrs = &thermal_temp_input_attr[8]
2397};
2398
2399#undef THERMAL_SENSOR_ATTR_TEMP
2400#undef THERMAL_ATTRS
2401
2402/* --------------------------------------------------------------------- */
2403
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002404static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002405{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002406 u8 t, ta1, ta2;
2407 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002408 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03002409 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002410
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002411 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
2412
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002413 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002414
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002415 if (ibm_thinkpad_ec_found && experimental) {
2416 /*
2417 * Direct EC access mode: sensors at registers
2418 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
2419 * non-implemented, thermal sensors return 0x80 when
2420 * not available
2421 */
2422
2423 ta1 = ta2 = 0;
2424 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002425 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002426 ta1 |= t;
2427 } else {
2428 ta1 = 0;
2429 break;
2430 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002431 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002432 ta2 |= t;
2433 } else {
2434 ta1 = 0;
2435 break;
2436 }
2437 }
2438 if (ta1 == 0) {
2439 /* This is sheer paranoia, but we handle it anyway */
2440 if (acpi_tmp7) {
2441 printk(IBM_ERR
2442 "ThinkPad ACPI EC access misbehaving, "
2443 "falling back to ACPI TMPx access mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002444 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002445 } else {
2446 printk(IBM_ERR
2447 "ThinkPad ACPI EC access misbehaving, "
2448 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002449 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002450 }
2451 } else {
2452 thermal_read_mode =
2453 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002454 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002455 }
2456 } else if (acpi_tmp7) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002457 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
2458 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002459 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002460 } else {
2461 /* Standard ACPI TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002462 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002463 }
2464 } else {
2465 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002466 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002467 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002468
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002469 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
2470 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
2471 thermal_read_mode);
2472
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03002473 switch(thermal_read_mode) {
2474 case TPACPI_THERMAL_TPEC_16:
2475 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2476 &thermal_temp_input16_group);
2477 if (res)
2478 return res;
2479 break;
2480 case TPACPI_THERMAL_TPEC_8:
2481 case TPACPI_THERMAL_ACPI_TMP07:
2482 case TPACPI_THERMAL_ACPI_UPDT:
2483 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2484 &thermal_temp_input8_group);
2485 if (res)
2486 return res;
2487 break;
2488 case TPACPI_THERMAL_NONE:
2489 default:
2490 return 1;
2491 }
2492
2493 return 0;
2494}
2495
2496static void thermal_exit(void)
2497{
2498 switch(thermal_read_mode) {
2499 case TPACPI_THERMAL_TPEC_16:
2500 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2501 &thermal_temp_input16_group);
2502 break;
2503 case TPACPI_THERMAL_TPEC_8:
2504 case TPACPI_THERMAL_ACPI_TMP07:
2505 case TPACPI_THERMAL_ACPI_UPDT:
2506 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2507 &thermal_temp_input16_group);
2508 break;
2509 case TPACPI_THERMAL_NONE:
2510 default:
2511 break;
2512 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002513}
2514
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002515/* idx is zero-based */
2516static int thermal_get_sensor(int idx, s32 *value)
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002517{
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002518 int t;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002519 s8 tmp;
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002520 char tmpi[5];
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002521
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002522 t = TP_EC_THERMAL_TMP0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002523
2524 switch (thermal_read_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002525#if TPACPI_MAX_THERMAL_SENSORS >= 16
2526 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002527 if (idx >= 8 && idx <= 15) {
2528 t = TP_EC_THERMAL_TMP8;
2529 idx -= 8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002530 }
2531 /* fallthrough */
2532#endif
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002533 case TPACPI_THERMAL_TPEC_8:
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002534 if (idx <= 7) {
2535 if (!acpi_ec_read(t + idx, &tmp))
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002536 return -EIO;
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002537 *value = tmp * 1000;
2538 return 0;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002539 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002540 break;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002541
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002542 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002543 if (idx <= 7) {
2544 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
2545 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
2546 return -EIO;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002547 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
2548 return -EIO;
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002549 *value = (t - 2732) * 100;
2550 return 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002551 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002552 break;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002553
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002554 case TPACPI_THERMAL_ACPI_TMP07:
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002555 if (idx <= 7) {
2556 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002557 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
2558 return -EIO;
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002559 *value = t * 1000;
2560 return 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002561 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002562 break;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002563
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002564 case TPACPI_THERMAL_NONE:
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002565 default:
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002566 return -ENOSYS;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002567 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002568
2569 return -EINVAL;
2570}
2571
2572static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
2573{
2574 int res, i;
2575 int n;
2576
2577 n = 8;
2578 i = 0;
2579
2580 if (!s)
2581 return -EINVAL;
2582
2583 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
2584 n = 16;
2585
2586 for(i = 0 ; i < n; i++) {
2587 res = thermal_get_sensor(i, &s->temp[i]);
2588 if (res)
2589 return res;
2590 }
2591
2592 return n;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002593}
2594
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002595static int thermal_read(char *p)
2596{
2597 int len = 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002598 int n, i;
2599 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002600
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002601 n = thermal_get_sensors(&t);
2602 if (unlikely(n < 0))
2603 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002604
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002605 len += sprintf(p + len, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002606
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002607 if (n > 0) {
2608 for (i = 0; i < (n - 1); i++)
2609 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
2610 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
2611 } else
2612 len += sprintf(p + len, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002613
2614 return len;
2615}
2616
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002617static struct ibm_struct thermal_driver_data = {
2618 .name = "thermal",
2619 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03002620 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002621};
2622
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002623/*************************************************************************
2624 * EC Dump subdriver
2625 */
2626
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002627static u8 ecdump_regs[256];
2628
2629static int ecdump_read(char *p)
2630{
2631 int len = 0;
2632 int i, j;
2633 u8 v;
2634
2635 len += sprintf(p + len, "EC "
2636 " +00 +01 +02 +03 +04 +05 +06 +07"
2637 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
2638 for (i = 0; i < 256; i += 16) {
2639 len += sprintf(p + len, "EC 0x%02x:", i);
2640 for (j = 0; j < 16; j++) {
2641 if (!acpi_ec_read(i + j, &v))
2642 break;
2643 if (v != ecdump_regs[i + j])
2644 len += sprintf(p + len, " *%02x", v);
2645 else
2646 len += sprintf(p + len, " %02x", v);
2647 ecdump_regs[i + j] = v;
2648 }
2649 len += sprintf(p + len, "\n");
2650 if (j != 16)
2651 break;
2652 }
2653
2654 /* These are way too dangerous to advertise openly... */
2655#if 0
2656 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
2657 " (<offset> is 00-ff, <value> is 00-ff)\n");
2658 len += sprintf(p + len, "commands:\t0x<offset> <value> "
2659 " (<offset> is 00-ff, <value> is 0-255)\n");
2660#endif
2661 return len;
2662}
2663
2664static int ecdump_write(char *buf)
2665{
2666 char *cmd;
2667 int i, v;
2668
2669 while ((cmd = next_cmd(&buf))) {
2670 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
2671 /* i and v set */
2672 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
2673 /* i and v set */
2674 } else
2675 return -EINVAL;
2676 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
2677 if (!acpi_ec_write(i, v))
2678 return -EIO;
2679 } else
2680 return -EINVAL;
2681 }
2682
2683 return 0;
2684}
2685
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002686static struct ibm_struct ecdump_driver_data = {
2687 .name = "ecdump",
2688 .read = ecdump_read,
2689 .write = ecdump_write,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03002690 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002691};
2692
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002693/*************************************************************************
2694 * Backlight/brightness subdriver
2695 */
Holger Macht8acb0252006-10-20 14:30:28 -07002696
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002697static struct backlight_device *ibm_backlight_device = NULL;
Holger Macht8acb0252006-10-20 14:30:28 -07002698
Richard Purdie599a52d2007-02-10 23:07:48 +00002699static struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02002700 .get_brightness = brightness_get,
2701 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02002702};
2703
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002704static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02002705{
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02002706 int b;
2707
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002708 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
2709
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02002710 b = brightness_get(NULL);
2711 if (b < 0)
2712 return b;
2713
Henrique de Moraes Holschuh7d5a0152007-04-24 11:48:20 -03002714 ibm_backlight_device = backlight_device_register(
2715 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
2716 &ibm_backlight_data);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02002717 if (IS_ERR(ibm_backlight_device)) {
2718 printk(IBM_ERR "Could not register backlight device\n");
2719 return PTR_ERR(ibm_backlight_device);
2720 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002721 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02002722
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02002723 ibm_backlight_device->props.max_brightness = 7;
2724 ibm_backlight_device->props.brightness = b;
2725 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00002726
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02002727 return 0;
2728}
2729
2730static void brightness_exit(void)
2731{
2732 if (ibm_backlight_device) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002733 vdbg_printk(TPACPI_DBG_EXIT,
2734 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02002735 backlight_device_unregister(ibm_backlight_device);
2736 ibm_backlight_device = NULL;
2737 }
2738}
2739
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002740static int brightness_update_status(struct backlight_device *bd)
2741{
2742 return brightness_set(
2743 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
2744 bd->props.power == FB_BLANK_UNBLANK) ?
2745 bd->props.brightness : 0);
2746}
2747
2748static int brightness_get(struct backlight_device *bd)
2749{
2750 u8 level;
2751 if (!acpi_ec_read(brightness_offset, &level))
2752 return -EIO;
2753
2754 level &= 0x7;
2755
2756 return level;
2757}
2758
2759static int brightness_set(int value)
2760{
2761 int cmos_cmd, inc, i;
2762 int current_value = brightness_get(NULL);
2763
2764 value &= 7;
2765
2766 cmos_cmd = value > current_value ? TP_CMOS_BRIGHTNESS_UP : TP_CMOS_BRIGHTNESS_DOWN;
2767 inc = value > current_value ? 1 : -1;
2768 for (i = current_value; i != value; i += inc) {
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03002769 if (issue_thinkpad_cmos_command(cmos_cmd))
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002770 return -EIO;
2771 if (!acpi_ec_write(brightness_offset, i + inc))
2772 return -EIO;
2773 }
2774
2775 return 0;
2776}
2777
2778static int brightness_read(char *p)
2779{
2780 int len = 0;
2781 int level;
2782
2783 if ((level = brightness_get(NULL)) < 0) {
2784 len += sprintf(p + len, "level:\t\tunreadable\n");
2785 } else {
2786 len += sprintf(p + len, "level:\t\t%d\n", level & 0x7);
2787 len += sprintf(p + len, "commands:\tup, down\n");
2788 len += sprintf(p + len, "commands:\tlevel <level>"
2789 " (<level> is 0-7)\n");
2790 }
2791
2792 return len;
2793}
2794
2795static int brightness_write(char *buf)
2796{
2797 int level;
2798 int new_level;
2799 char *cmd;
2800
2801 while ((cmd = next_cmd(&buf))) {
2802 if ((level = brightness_get(NULL)) < 0)
2803 return level;
2804 level &= 7;
2805
2806 if (strlencmp(cmd, "up") == 0) {
2807 new_level = level == 7 ? 7 : level + 1;
2808 } else if (strlencmp(cmd, "down") == 0) {
2809 new_level = level == 0 ? 0 : level - 1;
2810 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
2811 new_level >= 0 && new_level <= 7) {
2812 /* new_level set */
2813 } else
2814 return -EINVAL;
2815
2816 brightness_set(new_level);
2817 }
2818
2819 return 0;
2820}
2821
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002822static struct ibm_struct brightness_driver_data = {
2823 .name = "brightness",
2824 .read = brightness_read,
2825 .write = brightness_write,
2826 .exit = brightness_exit,
2827};
2828
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002829/*************************************************************************
2830 * Volume subdriver
2831 */
2832
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002833static int volume_read(char *p)
2834{
2835 int len = 0;
2836 u8 level;
2837
2838 if (!acpi_ec_read(volume_offset, &level)) {
2839 len += sprintf(p + len, "level:\t\tunreadable\n");
2840 } else {
2841 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
2842 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
2843 len += sprintf(p + len, "commands:\tup, down, mute\n");
2844 len += sprintf(p + len, "commands:\tlevel <level>"
2845 " (<level> is 0-15)\n");
2846 }
2847
2848 return len;
2849}
2850
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002851static int volume_write(char *buf)
2852{
2853 int cmos_cmd, inc, i;
2854 u8 level, mute;
2855 int new_level, new_mute;
2856 char *cmd;
2857
2858 while ((cmd = next_cmd(&buf))) {
2859 if (!acpi_ec_read(volume_offset, &level))
2860 return -EIO;
2861 new_mute = mute = level & 0x40;
2862 new_level = level = level & 0xf;
2863
2864 if (strlencmp(cmd, "up") == 0) {
2865 if (mute)
2866 new_mute = 0;
2867 else
2868 new_level = level == 15 ? 15 : level + 1;
2869 } else if (strlencmp(cmd, "down") == 0) {
2870 if (mute)
2871 new_mute = 0;
2872 else
2873 new_level = level == 0 ? 0 : level - 1;
2874 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
2875 new_level >= 0 && new_level <= 15) {
2876 /* new_level set */
2877 } else if (strlencmp(cmd, "mute") == 0) {
2878 new_mute = 0x40;
2879 } else
2880 return -EINVAL;
2881
2882 if (new_level != level) { /* mute doesn't change */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002883 cmos_cmd = new_level > level ? TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002884 inc = new_level > level ? 1 : -1;
2885
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03002886 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002887 !acpi_ec_write(volume_offset, level)))
2888 return -EIO;
2889
2890 for (i = level; i != new_level; i += inc)
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03002891 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002892 !acpi_ec_write(volume_offset, i + inc))
2893 return -EIO;
2894
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03002895 if (mute && (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002896 !acpi_ec_write(volume_offset,
2897 new_level + mute)))
2898 return -EIO;
2899 }
2900
2901 if (new_mute != mute) { /* level doesn't change */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002902 cmos_cmd = new_mute ? TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002903
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03002904 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002905 !acpi_ec_write(volume_offset, level + new_mute))
2906 return -EIO;
2907 }
2908 }
2909
2910 return 0;
2911}
2912
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002913static struct ibm_struct volume_driver_data = {
2914 .name = "volume",
2915 .read = volume_read,
2916 .write = volume_write,
2917};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002918
2919/*************************************************************************
2920 * Fan subdriver
2921 */
2922
2923/*
2924 * FAN ACCESS MODES
2925 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002926 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002927 * ACPI GFAN method: returns fan level
2928 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002929 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03002930 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002931 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002932 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002933 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
2934 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03002935 * EC 0x2f (HFSP) might be available *for reading*, but do not use
2936 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002937 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002938 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03002939 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
2940 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002941 *
2942 * Fan speed changes of any sort (including those caused by the
2943 * disengaged mode) are usually done slowly by the firmware as the
2944 * maximum ammount of fan duty cycle change per second seems to be
2945 * limited.
2946 *
2947 * Reading is not available if GFAN exists.
2948 * Writing is not available if SFAN exists.
2949 *
2950 * Bits
2951 * 7 automatic mode engaged;
2952 * (default operation mode of the ThinkPad)
2953 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03002954 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002955 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03002956 * the tachometer while the fan controller ramps up
2957 * the speed (which can take up to a few *minutes*).
2958 * Speeds up fan to 100% duty-cycle, which is far above
2959 * the standard RPM levels. It is not impossible that
2960 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002961 * 5-3 unused in some models. Extra bits for fan level
2962 * in others, but still useless as all values above
2963 * 7 map to the same speed as level 7 in these models.
2964 * 2-0 fan level (0..7 usually)
2965 * 0x00 = stop
2966 * 0x07 = max (set when temperatures critical)
2967 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002968 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002969 *
2970 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
2971 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
2972 * does so, its initial value is meaningless (0x07).
2973 *
2974 * For firmware bugs, refer to:
2975 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
2976 *
2977 * ----
2978 *
2979 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
2980 * Main fan tachometer reading (in RPM)
2981 *
2982 * This register is present on all ThinkPads with a new-style EC, and
2983 * it is known not to be present on the A21m/e, and T22, as there is
2984 * something else in offset 0x84 according to the ACPI DSDT. Other
2985 * ThinkPads from this same time period (and earlier) probably lack the
2986 * tachometer as well.
2987 *
2988 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
2989 * was never fixed by IBM to report the EC firmware version string
2990 * probably support the tachometer (like the early X models), so
2991 * detecting it is quite hard. We need more data to know for sure.
2992 *
2993 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
2994 * might result.
2995 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03002996 * FIRMWARE BUG: may go stale while the EC is switching to full speed
2997 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002998 *
2999 * For firmware bugs, refer to:
3000 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
3001 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003002 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003003 * ThinkPad X31, X40, X41. Not available in the X60.
3004 *
3005 * FANS ACPI handle: takes three arguments: low speed, medium speed,
3006 * high speed. ACPI DSDT seems to map these three speeds to levels
3007 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
3008 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
3009 *
3010 * The speeds are stored on handles
3011 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
3012 *
3013 * There are three default speed sets, acessible as handles:
3014 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
3015 *
3016 * ACPI DSDT switches which set is in use depending on various
3017 * factors.
3018 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003019 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003020 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
3021 * but the ACPI tables just mention level 7.
3022 */
3023
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003024static enum fan_status_access_mode fan_status_access_mode;
3025static enum fan_control_access_mode fan_control_access_mode;
3026static enum fan_control_commands fan_control_commands;
3027
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02003028static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003029static u8 fan_control_desired_level;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02003030
Len Brown25c68a32006-12-08 04:43:41 -05003031static void fan_watchdog_fire(struct work_struct *ignored);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003032static int fan_watchdog_maxinterval;
Len Brown25c68a32006-12-08 04:43:41 -05003033static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003034
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003035IBM_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
3036IBM_HANDLE(gfan, ec, "GFAN", /* 570 */
3037 "\\FSPD", /* 600e/x, 770e, 770x */
3038 ); /* all others */
3039IBM_HANDLE(sfan, ec, "SFAN", /* 570 */
3040 "JFNS", /* 770x-JL */
3041 ); /* all others */
3042
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003043/*
3044 * SYSFS fan layout: hwmon compatible (device)
3045 *
3046 * pwm*_enable:
3047 * 0: "disengaged" mode
3048 * 1: manual mode
3049 * 2: native EC "auto" mode (recommended, hardware default)
3050 *
3051 * pwm*: set speed in manual mode, ignored otherwise.
3052 * 0 is level 0; 255 is level 7. Intermediate points done with linear
3053 * interpolation.
3054 *
3055 * fan*_input: tachometer reading, RPM
3056 *
3057 *
3058 * SYSFS fan layout: extensions
3059 *
3060 * fan_watchdog (driver):
3061 * fan watchdog interval in seconds, 0 disables (default), max 120
3062 */
3063
3064/* sysfs fan pwm1_enable ----------------------------------------------- */
3065static ssize_t fan_pwm1_enable_show(struct device *dev,
3066 struct device_attribute *attr,
3067 char *buf)
3068{
3069 int res, mode;
3070 u8 status;
3071
3072 res = fan_get_status_safe(&status);
3073 if (res)
3074 return res;
3075
3076 if (unlikely(tp_features.fan_ctrl_status_undef)) {
3077 if (status != fan_control_initial_status) {
3078 tp_features.fan_ctrl_status_undef = 0;
3079 } else {
3080 /* Return most likely status. In fact, it
3081 * might be the only possible status */
3082 status = TP_EC_FAN_AUTO;
3083 }
3084 }
3085
3086 if (status & TP_EC_FAN_FULLSPEED) {
3087 mode = 0;
3088 } else if (status & TP_EC_FAN_AUTO) {
3089 mode = 2;
3090 } else
3091 mode = 1;
3092
3093 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
3094}
3095
3096static ssize_t fan_pwm1_enable_store(struct device *dev,
3097 struct device_attribute *attr,
3098 const char *buf, size_t count)
3099{
3100 unsigned long t;
3101 int res, level;
3102
3103 if (parse_strtoul(buf, 2, &t))
3104 return -EINVAL;
3105
3106 switch (t) {
3107 case 0:
3108 level = TP_EC_FAN_FULLSPEED;
3109 break;
3110 case 1:
3111 level = TPACPI_FAN_LAST_LEVEL;
3112 break;
3113 case 2:
3114 level = TP_EC_FAN_AUTO;
3115 break;
3116 case 3:
3117 /* reserved for software-controlled auto mode */
3118 return -ENOSYS;
3119 default:
3120 return -EINVAL;
3121 }
3122
3123 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03003124 if (res == -ENXIO)
3125 return -EINVAL;
3126 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003127 return res;
3128
3129 fan_watchdog_reset();
3130
3131 return count;
3132}
3133
3134static struct device_attribute dev_attr_fan_pwm1_enable =
3135 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
3136 fan_pwm1_enable_show, fan_pwm1_enable_store);
3137
3138/* sysfs fan pwm1 ------------------------------------------------------ */
3139static ssize_t fan_pwm1_show(struct device *dev,
3140 struct device_attribute *attr,
3141 char *buf)
3142{
3143 int res;
3144 u8 status;
3145
3146 res = fan_get_status_safe(&status);
3147 if (res)
3148 return res;
3149
3150 if (unlikely(tp_features.fan_ctrl_status_undef)) {
3151 if (status != fan_control_initial_status) {
3152 tp_features.fan_ctrl_status_undef = 0;
3153 } else {
3154 status = TP_EC_FAN_AUTO;
3155 }
3156 }
3157
3158 if ((status &
3159 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
3160 status = fan_control_desired_level;
3161
3162 if (status > 7)
3163 status = 7;
3164
3165 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
3166}
3167
3168static ssize_t fan_pwm1_store(struct device *dev,
3169 struct device_attribute *attr,
3170 const char *buf, size_t count)
3171{
3172 unsigned long s;
3173 int rc;
3174 u8 status, newlevel;
3175
3176 if (parse_strtoul(buf, 255, &s))
3177 return -EINVAL;
3178
3179 /* scale down from 0-255 to 0-7 */
3180 newlevel = (s >> 5) & 0x07;
3181
3182 rc = mutex_lock_interruptible(&fan_mutex);
3183 if (rc < 0)
3184 return rc;
3185
3186 rc = fan_get_status(&status);
3187 if (!rc && (status &
3188 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
3189 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03003190 if (rc == -ENXIO)
3191 rc = -EINVAL;
3192 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003193 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03003194 fan_watchdog_reset();
3195 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003196 }
3197
3198 mutex_unlock(&fan_mutex);
3199 return (rc)? rc : count;
3200}
3201
3202static struct device_attribute dev_attr_fan_pwm1 =
3203 __ATTR(pwm1, S_IWUSR | S_IRUGO,
3204 fan_pwm1_show, fan_pwm1_store);
3205
3206/* sysfs fan fan1_input ------------------------------------------------ */
3207static ssize_t fan_fan1_input_show(struct device *dev,
3208 struct device_attribute *attr,
3209 char *buf)
3210{
3211 int res;
3212 unsigned int speed;
3213
3214 res = fan_get_speed(&speed);
3215 if (res < 0)
3216 return res;
3217
3218 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
3219}
3220
3221static struct device_attribute dev_attr_fan_fan1_input =
3222 __ATTR(fan1_input, S_IRUGO,
3223 fan_fan1_input_show, NULL);
3224
3225/* sysfs fan fan_watchdog (driver) ------------------------------------- */
3226static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
3227 char *buf)
3228{
3229 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
3230}
3231
3232static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
3233 const char *buf, size_t count)
3234{
3235 unsigned long t;
3236
3237 if (parse_strtoul(buf, 120, &t))
3238 return -EINVAL;
3239
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03003240 if (!fan_control_allowed)
3241 return -EPERM;
3242
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003243 fan_watchdog_maxinterval = t;
3244 fan_watchdog_reset();
3245
3246 return count;
3247}
3248
3249static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
3250 fan_fan_watchdog_show, fan_fan_watchdog_store);
3251
3252/* --------------------------------------------------------------------- */
3253static struct attribute *fan_attributes[] = {
3254 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
3255 &dev_attr_fan_fan1_input.attr,
3256 NULL
3257};
3258
3259static const struct attribute_group fan_attr_group = {
3260 .attrs = fan_attributes,
3261};
3262
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003263static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003264{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003265 int rc;
3266
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003267 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
3268
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003269 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003270 fan_status_access_mode = TPACPI_FAN_NONE;
3271 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003272 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003273 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003274 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003275 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003276
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003277 IBM_ACPIHANDLE_INIT(fans);
3278 IBM_ACPIHANDLE_INIT(gfan);
3279 IBM_ACPIHANDLE_INIT(sfan);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003280
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003281 if (gfan_handle) {
3282 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003283 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003284 } else {
3285 /* all other ThinkPads: note that even old-style
3286 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02003287 if (likely(acpi_ec_read(fan_status_offset,
3288 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003289 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02003290
3291 /* In some ThinkPads, neither the EC nor the ACPI
3292 * DSDT initialize the fan status, and it ends up
3293 * being set to 0x07 when it *could* be either
3294 * 0x07 or 0x80.
3295 *
3296 * Enable for TP-1Y (T43), TP-78 (R51e),
3297 * TP-76 (R52), TP-70 (T43, R52), which are known
3298 * to be buggy. */
3299 if (fan_control_initial_status == 0x07 &&
3300 ibm_thinkpad_ec_found &&
3301 ((ibm_thinkpad_ec_found[0] == '1' &&
3302 ibm_thinkpad_ec_found[1] == 'Y') ||
3303 (ibm_thinkpad_ec_found[0] == '7' &&
3304 (ibm_thinkpad_ec_found[1] == '6' ||
3305 ibm_thinkpad_ec_found[1] == '8' ||
3306 ibm_thinkpad_ec_found[1] == '0'))
3307 )) {
3308 printk(IBM_NOTICE
3309 "fan_init: initial fan status is "
3310 "unknown, assuming it is in auto "
3311 "mode\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003312 tp_features.fan_ctrl_status_undef = 1;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02003313 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003314 } else {
3315 printk(IBM_ERR
3316 "ThinkPad ACPI EC access misbehaving, "
3317 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003318 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003319 }
3320 }
3321
3322 if (sfan_handle) {
3323 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003324 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02003325 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003326 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003327 } else {
3328 if (!gfan_handle) {
3329 /* gfan without sfan means no fan control */
3330 /* all other models implement TP EC 0x2f control */
3331
3332 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02003333 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003334 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003335 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003336 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003337 TPACPI_FAN_CMD_SPEED |
3338 TPACPI_FAN_CMD_LEVEL |
3339 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003340 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003341 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02003342 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003343 TPACPI_FAN_CMD_LEVEL |
3344 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003345 }
3346 }
3347 }
3348
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003349 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
3350 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
3351 fan_control_access_mode != TPACPI_FAN_WR_NONE),
3352 fan_status_access_mode, fan_control_access_mode);
3353
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03003354 /* fan control master switch */
3355 if (!fan_control_allowed) {
3356 fan_control_access_mode = TPACPI_FAN_WR_NONE;
3357 fan_control_commands = 0;
3358 dbg_printk(TPACPI_DBG_INIT,
3359 "fan control features disabled by parameter\n");
3360 }
3361
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003362 /* update fan_control_desired_level */
3363 if (fan_status_access_mode != TPACPI_FAN_NONE)
3364 fan_get_status_safe(NULL);
3365
3366 if (fan_status_access_mode != TPACPI_FAN_NONE ||
3367 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
3368 rc = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3369 &fan_attr_group);
3370 if (!(rc < 0))
3371 rc = driver_create_file(&tpacpi_pdriver.driver,
3372 &driver_attr_fan_watchdog);
3373 if (rc < 0)
3374 return rc;
3375 return 0;
3376 } else
3377 return 1;
3378}
3379
3380/*
3381 * Call with fan_mutex held
3382 */
3383static void fan_update_desired_level(u8 status)
3384{
3385 if ((status &
3386 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
3387 if (status > 7)
3388 fan_control_desired_level = 7;
3389 else
3390 fan_control_desired_level = status;
3391 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003392}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003393
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02003394static int fan_get_status(u8 *status)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003395{
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02003396 u8 s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003397
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02003398 /* TODO:
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003399 * Add TPACPI_FAN_RD_ACPI_FANS ? */
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02003400
Henrique de Moraes Holschuh3ef8a602006-11-24 11:47:10 -02003401 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003402 case TPACPI_FAN_RD_ACPI_GFAN:
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003403 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02003404
3405 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003406 return -EIO;
3407
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02003408 if (likely(status))
3409 *status = s & 0x07;
3410
3411 break;
3412
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003413 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02003414 /* all except 570, 600e/x, 770e, 770x */
3415 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
3416 return -EIO;
3417
3418 if (likely(status))
3419 *status = s;
3420
3421 break;
3422
3423 default:
3424 return -ENXIO;
3425 }
3426
3427 return 0;
3428}
3429
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003430static int fan_get_status_safe(u8 *status)
3431{
3432 int rc;
3433 u8 s;
3434
3435 rc = mutex_lock_interruptible(&fan_mutex);
3436 if (rc < 0)
3437 return rc;
3438 rc = fan_get_status(&s);
3439 if (!rc)
3440 fan_update_desired_level(s);
3441 mutex_unlock(&fan_mutex);
3442
3443 if (status)
3444 *status = s;
3445
3446 return rc;
3447}
3448
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003449static void fan_exit(void)
3450{
Henrique de Moraes Holschuh99fba3f2007-04-21 11:08:44 -03003451 vdbg_printk(TPACPI_DBG_EXIT, "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003452
3453 /* FIXME: can we really do this unconditionally? */
3454 sysfs_remove_group(&tpacpi_pdev->dev.kobj, &fan_attr_group);
3455 driver_remove_file(&tpacpi_pdriver.driver, &driver_attr_fan_watchdog);
3456
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003457 cancel_delayed_work(&fan_watchdog_task);
3458 flush_scheduled_work();
3459}
3460
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02003461static int fan_get_speed(unsigned int *speed)
3462{
3463 u8 hi, lo;
3464
3465 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003466 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02003467 /* all except 570, 600e/x, 770e, 770x */
3468 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
3469 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
3470 return -EIO;
3471
3472 if (likely(speed))
3473 *speed = (hi << 8) | lo;
3474
3475 break;
3476
3477 default:
3478 return -ENXIO;
3479 }
3480
3481 return 0;
3482}
3483
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003484static void fan_watchdog_fire(struct work_struct *ignored)
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003485{
Henrique de Moraes Holschuh99fba3f2007-04-21 11:08:44 -03003486 int rc;
3487
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003488 printk(IBM_NOTICE "fan watchdog: enabling fan\n");
Henrique de Moraes Holschuh99fba3f2007-04-21 11:08:44 -03003489 rc = fan_set_enable();
3490 if (rc < 0) {
3491 printk(IBM_ERR "fan watchdog: error %d while enabling fan, "
3492 "will try again later...\n", -rc);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003493 /* reschedule for later */
3494 fan_watchdog_reset();
3495 }
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003496}
3497
3498static void fan_watchdog_reset(void)
3499{
3500 static int fan_watchdog_active = 0;
3501
Henrique de Moraes Holschuh4985cd02007-04-27 22:00:10 -03003502 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
3503 return;
3504
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003505 if (fan_watchdog_active)
3506 cancel_delayed_work(&fan_watchdog_task);
3507
3508 if (fan_watchdog_maxinterval > 0) {
3509 fan_watchdog_active = 1;
3510 if (!schedule_delayed_work(&fan_watchdog_task,
3511 msecs_to_jiffies(fan_watchdog_maxinterval
3512 * 1000))) {
3513 printk(IBM_ERR "failed to schedule the fan watchdog, "
3514 "watchdog will not trigger\n");
3515 }
3516 } else
3517 fan_watchdog_active = 0;
3518}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003519
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003520static int fan_set_level(int level)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003521{
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03003522 if (!fan_control_allowed)
3523 return -EPERM;
3524
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003525 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003526 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003527 if (level >= 0 && level <= 7) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003528 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003529 return -EIO;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003530 } else
3531 return -EINVAL;
3532 break;
3533
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003534 case TPACPI_FAN_WR_ACPI_FANS:
3535 case TPACPI_FAN_WR_TPEC:
3536 if ((level != TP_EC_FAN_AUTO) &&
3537 (level != TP_EC_FAN_FULLSPEED) &&
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02003538 ((level < 0) || (level > 7)))
3539 return -EINVAL;
3540
Henrique de Moraes Holschuheaa75712007-04-24 11:48:18 -03003541 /* safety net should the EC not support AUTO
3542 * or FULLSPEED mode bits and just ignore them */
3543 if (level & TP_EC_FAN_FULLSPEED)
3544 level |= 7; /* safety min speed 7 */
3545 else if (level & TP_EC_FAN_FULLSPEED)
3546 level |= 4; /* safety min speed 4 */
3547
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003548 if (!acpi_ec_write(fan_status_offset, level))
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02003549 return -EIO;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02003550 else
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003551 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02003552 break;
3553
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003554 default:
3555 return -ENXIO;
3556 }
3557 return 0;
3558}
3559
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003560static int fan_set_level_safe(int level)
3561{
3562 int rc;
3563
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03003564 if (!fan_control_allowed)
3565 return -EPERM;
3566
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003567 rc = mutex_lock_interruptible(&fan_mutex);
3568 if (rc < 0)
3569 return rc;
3570
3571 if (level == TPACPI_FAN_LAST_LEVEL)
3572 level = fan_control_desired_level;
3573
3574 rc = fan_set_level(level);
3575 if (!rc)
3576 fan_update_desired_level(level);
3577
3578 mutex_unlock(&fan_mutex);
3579 return rc;
3580}
3581
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003582static int fan_set_enable(void)
3583{
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02003584 u8 s;
3585 int rc;
3586
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03003587 if (!fan_control_allowed)
3588 return -EPERM;
3589
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003590 rc = mutex_lock_interruptible(&fan_mutex);
3591 if (rc < 0)
3592 return rc;
3593
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003594 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003595 case TPACPI_FAN_WR_ACPI_FANS:
3596 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003597 rc = fan_get_status(&s);
3598 if (rc < 0)
3599 break;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02003600
3601 /* Don't go out of emergency fan mode */
Henrique de Moraes Holschuheaa75712007-04-24 11:48:18 -03003602 if (s != 7) {
3603 s &= 0x07;
3604 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
3605 }
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02003606
3607 if (!acpi_ec_write(fan_status_offset, s))
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003608 rc = -EIO;
3609 else {
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003610 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003611 rc = 0;
3612 }
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02003613 break;
3614
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003615 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003616 rc = fan_get_status(&s);
3617 if (rc < 0)
3618 break;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02003619
3620 s &= 0x07;
3621
3622 /* Set fan to at least level 4 */
Henrique de Moraes Holschuheaa75712007-04-24 11:48:18 -03003623 s |= 4;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02003624
3625 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003626 rc= -EIO;
3627 else
3628 rc = 0;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003629 break;
3630
3631 default:
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003632 rc = -ENXIO;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003633 }
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003634
3635 mutex_unlock(&fan_mutex);
3636 return rc;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003637}
3638
3639static int fan_set_disable(void)
3640{
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003641 int rc;
3642
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03003643 if (!fan_control_allowed)
3644 return -EPERM;
3645
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003646 rc = mutex_lock_interruptible(&fan_mutex);
3647 if (rc < 0)
3648 return rc;
3649
3650 rc = 0;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003651 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003652 case TPACPI_FAN_WR_ACPI_FANS:
3653 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003654 if (!acpi_ec_write(fan_status_offset, 0x00))
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003655 rc = -EIO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003656 else {
3657 fan_control_desired_level = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003658 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003659 }
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003660 break;
3661
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003662 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02003663 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003664 rc = -EIO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003665 else
3666 fan_control_desired_level = 0;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02003667 break;
3668
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003669 default:
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003670 rc = -ENXIO;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003671 }
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003672
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003673
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003674 mutex_unlock(&fan_mutex);
3675 return rc;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003676}
3677
3678static int fan_set_speed(int speed)
3679{
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003680 int rc;
3681
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03003682 if (!fan_control_allowed)
3683 return -EPERM;
3684
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003685 rc = mutex_lock_interruptible(&fan_mutex);
3686 if (rc < 0)
3687 return rc;
3688
3689 rc = 0;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003690 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003691 case TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003692 if (speed >= 0 && speed <= 65535) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003693 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
3694 speed, speed, speed))
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003695 rc = -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003696 } else
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003697 rc = -EINVAL;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003698 break;
3699
3700 default:
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003701 rc = -ENXIO;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003702 }
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003703
3704 mutex_unlock(&fan_mutex);
3705 return rc;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003706}
3707
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003708static int fan_read(char *p)
3709{
3710 int len = 0;
3711 int rc;
3712 u8 status;
3713 unsigned int speed = 0;
3714
3715 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003716 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003717 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003718 if ((rc = fan_get_status_safe(&status)) < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003719 return rc;
3720
3721 len += sprintf(p + len, "status:\t\t%s\n"
3722 "level:\t\t%d\n",
3723 (status != 0) ? "enabled" : "disabled", status);
3724 break;
3725
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003726 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003727 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003728 if ((rc = fan_get_status_safe(&status)) < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003729 return rc;
3730
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003731 if (unlikely(tp_features.fan_ctrl_status_undef)) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003732 if (status != fan_control_initial_status)
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003733 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003734 else
3735 /* Return most likely status. In fact, it
3736 * might be the only possible status */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003737 status = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003738 }
3739
3740 len += sprintf(p + len, "status:\t\t%s\n",
3741 (status != 0) ? "enabled" : "disabled");
3742
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003743 if ((rc = fan_get_speed(&speed)) < 0)
3744 return rc;
3745
3746 len += sprintf(p + len, "speed:\t\t%d\n", speed);
3747
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003748 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003749 /* Disengaged mode takes precedence */
3750 len += sprintf(p + len, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003751 else if (status & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003752 len += sprintf(p + len, "level:\t\tauto\n");
3753 else
3754 len += sprintf(p + len, "level:\t\t%d\n", status);
3755 break;
3756
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003757 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003758 default:
3759 len += sprintf(p + len, "status:\t\tnot supported\n");
3760 }
3761
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003762 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003763 len += sprintf(p + len, "commands:\tlevel <level>");
3764
3765 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003766 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003767 len += sprintf(p + len, " (<level> is 0-7)\n");
3768 break;
3769
3770 default:
3771 len += sprintf(p + len, " (<level> is 0-7, "
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003772 "auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003773 break;
3774 }
3775 }
3776
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003777 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003778 len += sprintf(p + len, "commands:\tenable, disable\n"
3779 "commands:\twatchdog <timeout> (<timeout> is 0 (off), "
3780 "1-120 (seconds))\n");
3781
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003782 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003783 len += sprintf(p + len, "commands:\tspeed <speed>"
3784 " (<speed> is 0-65535)\n");
3785
3786 return len;
3787}
3788
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003789static int fan_write_cmd_level(const char *cmd, int *rc)
3790{
3791 int level;
3792
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02003793 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003794 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003795 else if ((strlencmp(cmd, "level disengaged") == 0) |
3796 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003797 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02003798 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003799 return 0;
3800
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003801 if ((*rc = fan_set_level_safe(level)) == -ENXIO)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003802 printk(IBM_ERR "level command accepted for unsupported "
3803 "access mode %d", fan_control_access_mode);
3804
3805 return 1;
3806}
3807
3808static int fan_write_cmd_enable(const char *cmd, int *rc)
3809{
3810 if (strlencmp(cmd, "enable") != 0)
3811 return 0;
3812
3813 if ((*rc = fan_set_enable()) == -ENXIO)
3814 printk(IBM_ERR "enable command accepted for unsupported "
3815 "access mode %d", fan_control_access_mode);
3816
3817 return 1;
3818}
3819
3820static int fan_write_cmd_disable(const char *cmd, int *rc)
3821{
3822 if (strlencmp(cmd, "disable") != 0)
3823 return 0;
3824
3825 if ((*rc = fan_set_disable()) == -ENXIO)
3826 printk(IBM_ERR "disable command accepted for unsupported "
3827 "access mode %d", fan_control_access_mode);
3828
3829 return 1;
3830}
3831
3832static int fan_write_cmd_speed(const char *cmd, int *rc)
3833{
3834 int speed;
3835
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02003836 /* TODO:
3837 * Support speed <low> <medium> <high> ? */
3838
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003839 if (sscanf(cmd, "speed %d", &speed) != 1)
3840 return 0;
3841
3842 if ((*rc = fan_set_speed(speed)) == -ENXIO)
3843 printk(IBM_ERR "speed command accepted for unsupported "
3844 "access mode %d", fan_control_access_mode);
3845
3846 return 1;
3847}
3848
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003849static int fan_write_cmd_watchdog(const char *cmd, int *rc)
3850{
3851 int interval;
3852
3853 if (sscanf(cmd, "watchdog %d", &interval) != 1)
3854 return 0;
3855
3856 if (interval < 0 || interval > 120)
3857 *rc = -EINVAL;
3858 else
3859 fan_watchdog_maxinterval = interval;
3860
3861 return 1;
3862}
3863
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003864static int fan_write(char *buf)
3865{
3866 char *cmd;
3867 int rc = 0;
3868
3869 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003870 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003871 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003872 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003873 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003874 fan_write_cmd_disable(cmd, &rc) ||
3875 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003876 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003877 fan_write_cmd_speed(cmd, &rc))
3878 )
3879 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003880 else if (!rc)
3881 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003882 }
3883
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003884 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003885}
3886
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003887static struct ibm_struct fan_driver_data = {
3888 .name = "fan",
3889 .read = fan_read,
3890 .write = fan_write,
3891 .exit = fan_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003892};
3893
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003894/****************************************************************************
3895 ****************************************************************************
3896 *
3897 * Infrastructure
3898 *
3899 ****************************************************************************
3900 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003902/* /proc support */
3903static struct proc_dir_entry *proc_dir = NULL;
3904
3905/* Subdriver registry */
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003906static LIST_HEAD(tpacpi_all_drivers);
3907
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003909/*
3910 * Module and infrastructure proble, init and exit handling
3911 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003913#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003914static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003915{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003916 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003917
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003918 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003919}
3920#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
3921
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003922static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923{
3924 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003925 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926 struct proc_dir_entry *entry;
3927
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003928 BUG_ON(ibm == NULL);
3929
3930 INIT_LIST_HEAD(&ibm->all_drivers);
3931
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03003932 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933 return 0;
3934
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003935 dbg_printk(TPACPI_DBG_INIT,
3936 "probing for %s\n", ibm->name);
3937
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003938 if (iibm->init) {
3939 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003940 if (ret > 0)
3941 return 0; /* probe failed */
3942 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003944
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03003945 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946 }
3947
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003948 if (ibm->acpi) {
3949 if (ibm->acpi->hid) {
3950 ret = register_tpacpi_subdriver(ibm);
3951 if (ret)
3952 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003953 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003954
3955 if (ibm->acpi->notify) {
3956 ret = setup_acpi_notify(ibm);
3957 if (ret == -ENODEV) {
3958 printk(IBM_NOTICE "disabling subdriver %s\n",
3959 ibm->name);
3960 ret = 0;
3961 goto err_out;
3962 }
3963 if (ret < 0)
3964 goto err_out;
3965 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003966 }
3967
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003968 dbg_printk(TPACPI_DBG_INIT,
3969 "%s installed\n", ibm->name);
3970
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003971 if (ibm->read) {
3972 entry = create_proc_entry(ibm->name,
3973 S_IFREG | S_IRUGO | S_IWUSR,
3974 proc_dir);
3975 if (!entry) {
3976 printk(IBM_ERR "unable to create proc entry %s\n",
3977 ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003978 ret = -ENODEV;
3979 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003980 }
3981 entry->owner = THIS_MODULE;
3982 entry->data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003983 entry->read_proc = &dispatch_procfs_read;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003984 if (ibm->write)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003985 entry->write_proc = &dispatch_procfs_write;
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03003986 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003987 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003989 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
3990
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003992
3993err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003994 dbg_printk(TPACPI_DBG_INIT,
3995 "%s: at error exit path with result %d\n",
3996 ibm->name, ret);
3997
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003998 ibm_exit(ibm);
3999 return (ret < 0)? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000}
4001
4002static void ibm_exit(struct ibm_struct *ibm)
4003{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004004 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004005
4006 list_del_init(&ibm->all_drivers);
4007
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004008 if (ibm->flags.acpi_notify_installed) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004009 dbg_printk(TPACPI_DBG_EXIT,
4010 "%s: acpi_remove_notify_handler\n", ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004011 BUG_ON(!ibm->acpi);
4012 acpi_remove_notify_handler(*ibm->acpi->handle,
4013 ibm->acpi->type,
4014 dispatch_acpi_notify);
4015 ibm->flags.acpi_notify_installed = 0;
4016 ibm->flags.acpi_notify_installed = 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004017 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004019 if (ibm->flags.proc_created) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004020 dbg_printk(TPACPI_DBG_EXIT,
4021 "%s: remove_proc_entry\n", ibm->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022 remove_proc_entry(ibm->name, proc_dir);
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004023 ibm->flags.proc_created = 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004024 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004026 if (ibm->flags.acpi_driver_registered) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004027 dbg_printk(TPACPI_DBG_EXIT,
4028 "%s: acpi_bus_unregister_driver\n", ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004029 BUG_ON(!ibm->acpi);
4030 acpi_bus_unregister_driver(ibm->acpi->driver);
4031 kfree(ibm->acpi->driver);
4032 ibm->acpi->driver = NULL;
4033 ibm->flags.acpi_driver_registered = 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004034 }
4035
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004036 if (ibm->flags.init_called && ibm->exit) {
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004037 ibm->exit();
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004038 ibm->flags.init_called = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004040
4041 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042}
4043
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004044/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004046static char *ibm_thinkpad_ec_found = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02004048static char* __init check_dmi_for_ec(void)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004049{
4050 struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02004051 char ec_fw_string[18];
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004052
4053 /*
4054 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
4055 * X32 or newer, all Z series; Some models must have an
4056 * up-to-date BIOS or they will not be detected.
4057 *
4058 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
4059 */
4060 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02004061 if (sscanf(dev->name,
4062 "IBM ThinkPad Embedded Controller -[%17c",
4063 ec_fw_string) == 1) {
4064 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
4065 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
4066 return kstrdup(ec_fw_string, GFP_KERNEL);
4067 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004068 }
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02004069 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070}
4071
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004072static int __init probe_for_thinkpad(void)
4073{
4074 int is_thinkpad;
4075
4076 if (acpi_disabled)
4077 return -ENODEV;
4078
4079 /*
4080 * Non-ancient models have better DMI tagging, but very old models
4081 * don't.
4082 */
4083 is_thinkpad = dmi_name_in_vendors("ThinkPad");
4084
4085 /* ec is required because many other handles are relative to it */
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004086 IBM_ACPIHANDLE_INIT(ec);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004087 if (!ec_handle) {
4088 if (is_thinkpad)
4089 printk(IBM_ERR
4090 "Not yet supported ThinkPad detected!\n");
4091 return -ENODEV;
4092 }
4093
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03004094 /*
4095 * Risks a regression on very old machines, but reduces potential
4096 * false positives a damn great deal
4097 */
4098 if (!is_thinkpad)
4099 is_thinkpad = dmi_name_in_vendors("IBM");
4100
4101 if (!is_thinkpad && !force_load)
4102 return -ENODEV;
4103
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004104 return 0;
4105}
4106
4107
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004108/* Module init, exit, parameters */
4109
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004110static struct ibm_init_struct ibms_init[] __initdata = {
4111 {
4112 .init = thinkpad_acpi_driver_init,
4113 .data = &thinkpad_acpi_driver_data,
4114 },
4115 {
4116 .init = hotkey_init,
4117 .data = &hotkey_driver_data,
4118 },
4119 {
4120 .init = bluetooth_init,
4121 .data = &bluetooth_driver_data,
4122 },
4123 {
4124 .init = wan_init,
4125 .data = &wan_driver_data,
4126 },
4127 {
4128 .init = video_init,
4129 .data = &video_driver_data,
4130 },
4131 {
4132 .init = light_init,
4133 .data = &light_driver_data,
4134 },
4135#ifdef CONFIG_THINKPAD_ACPI_DOCK
4136 {
4137 .init = dock_init,
4138 .data = &dock_driver_data[0],
4139 },
4140 {
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03004141 .init = dock_init2,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004142 .data = &dock_driver_data[1],
4143 },
4144#endif
4145#ifdef CONFIG_THINKPAD_ACPI_BAY
4146 {
4147 .init = bay_init,
4148 .data = &bay_driver_data,
4149 },
4150#endif
4151 {
4152 .init = cmos_init,
4153 .data = &cmos_driver_data,
4154 },
4155 {
4156 .init = led_init,
4157 .data = &led_driver_data,
4158 },
4159 {
4160 .init = beep_init,
4161 .data = &beep_driver_data,
4162 },
4163 {
4164 .init = thermal_init,
4165 .data = &thermal_driver_data,
4166 },
4167 {
4168 .data = &ecdump_driver_data,
4169 },
4170 {
4171 .init = brightness_init,
4172 .data = &brightness_driver_data,
4173 },
4174 {
4175 .data = &volume_driver_data,
4176 },
4177 {
4178 .init = fan_init,
4179 .data = &fan_driver_data,
4180 },
4181};
4182
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004183static int __init set_ibm_param(const char *val, struct kernel_param *kp)
4184{
4185 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004186 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004187
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004188 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
4189 ibm = ibms_init[i].data;
4190 BUG_ON(ibm == NULL);
4191
4192 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
4193 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004194 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004195 strcpy(ibms_init[i].param, val);
4196 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004197 return 0;
4198 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004199 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004200
4201 return -EINVAL;
4202}
4203
4204static int experimental;
4205module_param(experimental, int, 0);
4206
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03004207static u32 dbg_level;
4208module_param_named(debug, dbg_level, uint, 0);
4209
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03004210static int force_load;
4211module_param(force_load, int, 0);
4212
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03004213static int fan_control_allowed;
4214module_param_named(fan_control, fan_control_allowed, int, 0);
4215
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004216#define IBM_PARAM(feature) \
4217 module_param_call(feature, set_ibm_param, NULL, NULL, 0)
4218
4219IBM_PARAM(hotkey);
4220IBM_PARAM(bluetooth);
4221IBM_PARAM(video);
4222IBM_PARAM(light);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03004223#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004224IBM_PARAM(dock);
4225#endif
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03004226#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004227IBM_PARAM(bay);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03004228#endif /* CONFIG_THINKPAD_ACPI_BAY */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004229IBM_PARAM(cmos);
4230IBM_PARAM(led);
4231IBM_PARAM(beep);
4232IBM_PARAM(ecdump);
4233IBM_PARAM(brightness);
4234IBM_PARAM(volume);
4235IBM_PARAM(fan);
4236
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03004237static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238{
4239 int ret, i;
4240
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03004241 /* Driver-level probe */
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004242 ret = probe_for_thinkpad();
4243 if (ret)
4244 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03004246 /* Driver initialization */
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004247 ibm_thinkpad_ec_found = check_dmi_for_ec();
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004248 IBM_ACPIHANDLE_INIT(ecrd);
4249 IBM_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004251 proc_dir = proc_mkdir(IBM_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252 if (!proc_dir) {
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004253 printk(IBM_ERR "unable to create proc dir " IBM_PROC_DIR);
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03004254 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255 return -ENODEV;
4256 }
4257 proc_dir->owner = THIS_MODULE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004258
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03004259 ret = platform_driver_register(&tpacpi_pdriver);
4260 if (ret) {
4261 printk(IBM_ERR "unable to register platform driver\n");
4262 thinkpad_acpi_module_exit();
4263 return ret;
4264 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03004265 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
4266 if (ret) {
4267 printk(IBM_ERR "unable to create sysfs driver attributes\n");
4268 thinkpad_acpi_module_exit();
4269 return ret;
4270 }
4271
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03004272
4273 /* Device initialization */
4274 tpacpi_pdev = platform_device_register_simple(IBM_DRVR_NAME, -1,
4275 NULL, 0);
4276 if (IS_ERR(tpacpi_pdev)) {
4277 ret = PTR_ERR(tpacpi_pdev);
4278 tpacpi_pdev = NULL;
4279 printk(IBM_ERR "unable to register platform device\n");
4280 thinkpad_acpi_module_exit();
4281 return ret;
4282 }
4283 tpacpi_hwmon = hwmon_device_register(&tpacpi_pdev->dev);
4284 if (IS_ERR(tpacpi_hwmon)) {
4285 ret = PTR_ERR(tpacpi_hwmon);
4286 tpacpi_hwmon = NULL;
4287 printk(IBM_ERR "unable to register hwmon device\n");
4288 thinkpad_acpi_module_exit();
4289 return ret;
4290 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004291 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
4292 ret = ibm_init(&ibms_init[i]);
4293 if (ret >= 0 && *ibms_init[i].param)
4294 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03004296 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297 return ret;
4298 }
4299 }
4300
4301 return 0;
4302}
4303
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03004304static void thinkpad_acpi_module_exit(void)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004305{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004306 struct ibm_struct *ibm, *itmp;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004307
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004308 list_for_each_entry_safe_reverse(ibm, itmp,
4309 &tpacpi_all_drivers,
4310 all_drivers) {
4311 ibm_exit(ibm);
4312 }
4313
4314 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004315
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03004316 if (tpacpi_hwmon)
4317 hwmon_device_unregister(tpacpi_hwmon);
4318
4319 if (tpacpi_pdev)
4320 platform_device_unregister(tpacpi_pdev);
4321
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03004322 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03004323 platform_driver_unregister(&tpacpi_pdriver);
4324
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004325 if (proc_dir)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004326 remove_proc_entry(IBM_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004327
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004328 kfree(ibm_thinkpad_ec_found);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004329}
4330
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03004331module_init(thinkpad_acpi_module_init);
4332module_exit(thinkpad_acpi_module_exit);