blob: c86b228375cc33a5d7e5cd8cb83fd6bab4af72b1 [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 Holschuh94b08712007-07-18 23:45:32 -030025#define TPACPI_SYSFS_VERSION 0x000200
Borislav Deianov78f81cc2005-08-17 00:00:00 -040026
27/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 * Changelog:
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -030029 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
30 * drivers/misc.
Henrique de Moraes Holschuhf9ff43a2006-11-25 16:37:38 -020031 *
32 * 2006-11-22 0.13 new maintainer
33 * changelog now lives in git commit history, and will
34 * not be updated further in-file.
Henrique de Moraes Holschuh837ca6d2007-03-23 17:33:54 -030035 *
Borislav Deianov78f81cc2005-08-17 00:00:00 -040036 * 2005-08-17 0.12 fix compilation on 2.6.13-rc kernels
37 * 2005-03-17 0.11 support for 600e, 770x
38 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
39 * support for 770e, G41
40 * G40 and G41 don't have a thinklight
41 * temperatures no longer experimental
42 * experimental brightness control
43 * experimental volume control
44 * experimental fan enable/disable
Henrique de Moraes Holschuh837ca6d2007-03-23 17:33:54 -030045 * 2005-01-16 0.10 fix module loading on R30, R31
Borislav Deianov78f81cc2005-08-17 00:00:00 -040046 * 2005-01-16 0.9 support for 570, R30, R31
47 * ultrabay support on A22p, A3x
48 * limit arg for cmos, led, beep, drop experimental status
49 * more capable led control on A21e, A22p, T20-22, X20
50 * experimental temperatures and fan speed
51 * experimental embedded controller register dump
52 * mark more functions as __init, drop incorrect __exit
53 * use MODULE_VERSION
54 * thanks to Henrik Brix Andersen <brix@gentoo.org>
55 * fix parameter passing on module loading
56 * thanks to Rusty Russell <rusty@rustcorp.com.au>
57 * thanks to Jim Radford <radford@blackbean.org>
58 * 2004-11-08 0.8 fix init error case, don't return from a macro
59 * thanks to Chris Wright <chrisw@osdl.org>
60 * 2004-10-23 0.7 fix module loading on A21e, A22p, T20, T21, X20
61 * fix led control on A21e
62 * 2004-10-19 0.6 use acpi_bus_register_driver() to claim HKEY device
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 * 2004-10-18 0.5 thinklight support on A21e, G40, R32, T20, T21, X20
64 * proc file format changed
65 * video_switch command
66 * experimental cmos control
67 * experimental led control
68 * experimental acpi sounds
Borislav Deianov78f81cc2005-08-17 00:00:00 -040069 * 2004-09-16 0.4 support for module parameters
70 * hotkey mask can be prefixed by 0x
71 * video output switching
72 * video expansion control
73 * ultrabay eject support
74 * removed lcd brightness/on/off control, didn't work
75 * 2004-08-17 0.3 support for R40
76 * lcd off, brightness control
77 * thinklight on/off
78 * 2004-08-14 0.2 support for T series, X20
79 * bluetooth enable/disable
80 * hotkey events disabled by default
81 * removed fan control, currently useless
82 * 2004-08-09 0.1 initial release, support for X series
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 */
84
Henrique de Moraes Holschuhf21f85d2007-03-29 01:58:40 -030085#include "thinkpad_acpi.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Henrique de Moraes Holschuhf9ff43a2006-11-25 16:37:38 -020087MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
Borislav Deianov78f81cc2005-08-17 00:00:00 -040088MODULE_DESCRIPTION(IBM_DESC);
89MODULE_VERSION(IBM_VERSION);
90MODULE_LICENSE("GPL");
91
Henrique de Moraes Holschuhd903ac52007-03-29 01:58:42 -030092/* Please remove this in year 2009 */
93MODULE_ALIAS("ibm_acpi");
94
Henrique de Moraes Holschuhb964b432007-07-18 23:45:26 -030095/*
96 * DMI matching for module autoloading
97 *
98 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
99 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
100 *
101 * Only models listed in thinkwiki will be supported, so add yours
102 * if it is not there yet.
103 */
104#define IBM_BIOS_MODULE_ALIAS(__type) \
105 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW")
106
107/* Non-ancient thinkpads */
108MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
109MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
110
111/* Ancient thinkpad BIOSes have to be identified by
112 * BIOS type or model number, and there are far less
113 * BIOS types than model numbers... */
114IBM_BIOS_MODULE_ALIAS("I[B,D,H,I,M,N,O,T,W,V,Y,Z]");
115IBM_BIOS_MODULE_ALIAS("1[0,3,6,8,A-G,I,K,M-P,S,T]");
116IBM_BIOS_MODULE_ALIAS("K[U,X-Z]");
117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118#define __unused __attribute__ ((unused))
119
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300120/****************************************************************************
121 ****************************************************************************
122 *
123 * ACPI Helpers and device model
124 *
125 ****************************************************************************
126 ****************************************************************************/
127
128/*************************************************************************
129 * ACPI basic handles
130 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -0300132static acpi_handle root_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134#define IBM_HANDLE(object, parent, paths...) \
135 static acpi_handle object##_handle; \
136 static acpi_handle *object##_parent = &parent##_handle; \
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400137 static char *object##_path; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 static char *object##_paths[] = { paths }
139
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400140IBM_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
141 "\\_SB.PCI.ISA.EC", /* 570 */
142 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
143 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
144 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
145 "\\_SB.PCI0.ICH3.EC0", /* R31 */
146 "\\_SB.PCI0.LPC.EC", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300147 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300149IBM_HANDLE(ecrd, ec, "ECRD"); /* 570 */
150IBM_HANDLE(ecwr, ec, "ECWR"); /* 570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300152
153/*************************************************************************
154 * Misc ACPI handles
155 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400157IBM_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, T4x, X31, X40 */
158 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
159 "\\CMS", /* R40, R40e */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300160 ); /* all others */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400161
162IBM_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
163 "^HKEY", /* R30, R31 */
164 "HKEY", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300165 ); /* 570 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400166
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400167
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300168/*************************************************************************
169 * ACPI helpers
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200170 */
171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172static int acpi_evalf(acpi_handle handle,
173 void *res, char *method, char *fmt, ...)
174{
175 char *fmt0 = fmt;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400176 struct acpi_object_list params;
177 union acpi_object in_objs[IBM_MAX_ACPI_ARGS];
178 struct acpi_buffer result, *resultp;
179 union acpi_object out_obj;
180 acpi_status status;
181 va_list ap;
182 char res_type;
183 int success;
184 int quiet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
186 if (!*fmt) {
187 printk(IBM_ERR "acpi_evalf() called with empty format\n");
188 return 0;
189 }
190
191 if (*fmt == 'q') {
192 quiet = 1;
193 fmt++;
194 } else
195 quiet = 0;
196
197 res_type = *(fmt++);
198
199 params.count = 0;
200 params.pointer = &in_objs[0];
201
202 va_start(ap, fmt);
203 while (*fmt) {
204 char c = *(fmt++);
205 switch (c) {
206 case 'd': /* int */
207 in_objs[params.count].integer.value = va_arg(ap, int);
208 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
209 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400210 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 default:
212 printk(IBM_ERR "acpi_evalf() called "
213 "with invalid format character '%c'\n", c);
214 return 0;
215 }
216 }
217 va_end(ap);
218
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400219 if (res_type != 'v') {
220 result.length = sizeof(out_obj);
221 result.pointer = &out_obj;
222 resultp = &result;
223 } else
224 resultp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400226 status = acpi_evaluate_object(handle, method, &params, resultp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228 switch (res_type) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400229 case 'd': /* int */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 if (res)
231 *(int *)res = out_obj.integer.value;
232 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
233 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400234 case 'v': /* void */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 success = status == AE_OK;
236 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400237 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 default:
239 printk(IBM_ERR "acpi_evalf() called "
240 "with invalid format character '%c'\n", res_type);
241 return 0;
242 }
243
244 if (!success && !quiet)
245 printk(IBM_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
246 method, fmt0, status);
247
248 return success;
249}
250
251static void __unused acpi_print_int(acpi_handle handle, char *method)
252{
253 int i;
254
255 if (acpi_evalf(handle, &i, method, "d"))
256 printk(IBM_INFO "%s = 0x%x\n", method, i);
257 else
258 printk(IBM_ERR "error calling %s\n", method);
259}
260
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300261static int acpi_ec_read(int i, u8 * p)
262{
263 int v;
264
265 if (ecrd_handle) {
266 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
267 return 0;
268 *p = v;
269 } else {
270 if (ec_read(i, p) < 0)
271 return 0;
272 }
273
274 return 1;
275}
276
277static int acpi_ec_write(int i, u8 v)
278{
279 if (ecwr_handle) {
280 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
281 return 0;
282 } else {
283 if (ec_write(i, v) < 0)
284 return 0;
285 }
286
287 return 1;
288}
289
290static int _sta(acpi_handle handle)
291{
292 int status;
293
294 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
295 status = 0;
296
297 return status;
298}
299
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -0300300static int issue_thinkpad_cmos_command(int cmos_cmd)
301{
302 if (!cmos_handle)
303 return -ENXIO;
304
305 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
306 return -EIO;
307
308 return 0;
309}
310
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300311/*************************************************************************
312 * ACPI device model
313 */
314
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300315static void drv_acpi_handle_init(char *name,
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300316 acpi_handle *handle, acpi_handle parent,
317 char **paths, int num_paths, char **path)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300318{
319 int i;
320 acpi_status status;
321
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300322 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
323 name);
324
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300325 for (i = 0; i < num_paths; i++) {
326 status = acpi_get_handle(parent, paths[i], handle);
327 if (ACPI_SUCCESS(status)) {
328 *path = paths[i];
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300329 dbg_printk(TPACPI_DBG_INIT,
330 "Found ACPI handle %s for %s\n",
331 *path, name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300332 return;
333 }
334 }
335
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300336 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
337 name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300338 *handle = NULL;
339}
340
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300341static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300342{
343 struct ibm_struct *ibm = data;
344
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300345 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300346 return;
347
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300348 ibm->acpi->notify(ibm, event);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300349}
350
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300351static int __init setup_acpi_notify(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300352{
353 acpi_status status;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300354 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300355
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300356 BUG_ON(!ibm->acpi);
357
358 if (!*ibm->acpi->handle)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300359 return 0;
360
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300361 vdbg_printk(TPACPI_DBG_INIT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300362 "setting up ACPI notify for %s\n", ibm->name);
363
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300364 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
365 if (rc < 0) {
366 printk(IBM_ERR "acpi_bus_get_device(%s) failed: %d\n",
367 ibm->name, rc);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300368 return -ENODEV;
369 }
370
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300371 acpi_driver_data(ibm->acpi->device) = ibm;
372 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300373 IBM_ACPI_EVENT_PREFIX,
374 ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300375
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300376 status = acpi_install_notify_handler(*ibm->acpi->handle,
377 ibm->acpi->type, dispatch_acpi_notify, ibm);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300378 if (ACPI_FAILURE(status)) {
379 if (status == AE_ALREADY_EXISTS) {
380 printk(IBM_NOTICE "another device driver is already handling %s events\n",
381 ibm->name);
382 } else {
383 printk(IBM_ERR "acpi_install_notify_handler(%s) failed: %d\n",
384 ibm->name, status);
385 }
386 return -ENODEV;
387 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300388 ibm->flags.acpi_notify_installed = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300389 return 0;
390}
391
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300392static int __init tpacpi_device_add(struct acpi_device *device)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300393{
394 return 0;
395}
396
Henrique de Moraes Holschuh67001212007-04-21 11:08:25 -0300397static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300398{
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300399 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300400
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300401 dbg_printk(TPACPI_DBG_INIT,
402 "registering %s as an ACPI driver\n", ibm->name);
403
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300404 BUG_ON(!ibm->acpi);
405
406 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
407 if (!ibm->acpi->driver) {
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300408 printk(IBM_ERR "kzalloc(ibm->driver) failed\n");
409 return -ENOMEM;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300410 }
411
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300412 sprintf(ibm->acpi->driver->name, "%s_%s", IBM_NAME, ibm->name);
413 ibm->acpi->driver->ids = ibm->acpi->hid;
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 Holschuh7f5d1cd2007-07-18 23:45:34 -0300513 * Device model: input, hwmon and platform
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300514 *
515 ****************************************************************************
516 ****************************************************************************/
517
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -0300518static struct platform_device *tpacpi_pdev;
519static struct class_device *tpacpi_hwmon;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300520static struct input_dev *tpacpi_inputdev;
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300521
522static struct platform_driver tpacpi_pdriver = {
523 .driver = {
524 .name = IBM_DRVR_NAME,
525 .owner = THIS_MODULE,
526 },
527};
528
529
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -0300530/*************************************************************************
531 * thinkpad-acpi driver attributes
532 */
533
534/* interface_version --------------------------------------------------- */
535static ssize_t tpacpi_driver_interface_version_show(
536 struct device_driver *drv,
537 char *buf)
538{
539 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
540}
541
542static DRIVER_ATTR(interface_version, S_IRUGO,
543 tpacpi_driver_interface_version_show, NULL);
544
545/* debug_level --------------------------------------------------------- */
546static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
547 char *buf)
548{
549 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
550}
551
552static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
553 const char *buf, size_t count)
554{
555 unsigned long t;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -0300556
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300557 if (parse_strtoul(buf, 0xffff, &t))
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -0300558 return -EINVAL;
559
560 dbg_level = t;
561
562 return count;
563}
564
565static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
566 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
567
568/* version ------------------------------------------------------------- */
569static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
570 char *buf)
571{
572 return snprintf(buf, PAGE_SIZE, "%s v%s\n", IBM_DESC, IBM_VERSION);
573}
574
575static DRIVER_ATTR(version, S_IRUGO,
576 tpacpi_driver_version_show, NULL);
577
578/* --------------------------------------------------------------------- */
579
580static struct driver_attribute* tpacpi_driver_attributes[] = {
581 &driver_attr_debug_level, &driver_attr_version,
582 &driver_attr_interface_version,
583};
584
585static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
586{
587 int i, res;
588
589 i = 0;
590 res = 0;
591 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
592 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
593 i++;
594 }
595
596 return res;
597}
598
599static void tpacpi_remove_driver_attributes(struct device_driver *drv)
600{
601 int i;
602
603 for(i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
604 driver_remove_file(drv, tpacpi_driver_attributes[i]);
605}
606
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300607/*************************************************************************
608 * sysfs support helpers
609 */
610
611struct attribute_set_obj {
612 struct attribute_set s;
613 struct attribute *a;
614} __attribute__((packed));
615
616static struct attribute_set *create_attr_set(unsigned int max_members,
617 const char* name)
618{
619 struct attribute_set_obj *sobj;
620
621 if (max_members == 0)
622 return NULL;
623
624 /* Allocates space for implicit NULL at the end too */
625 sobj = kzalloc(sizeof(struct attribute_set_obj) +
626 max_members * sizeof(struct attribute *),
627 GFP_KERNEL);
628 if (!sobj)
629 return NULL;
630 sobj->s.max_members = max_members;
631 sobj->s.group.attrs = &sobj->a;
632 sobj->s.group.name = name;
633
634 return &sobj->s;
635}
636
637/* not multi-threaded safe, use it in a single thread per set */
638static int add_to_attr_set(struct attribute_set* s, struct attribute *attr)
639{
640 if (!s || !attr)
641 return -EINVAL;
642
643 if (s->members >= s->max_members)
644 return -ENOMEM;
645
646 s->group.attrs[s->members] = attr;
647 s->members++;
648
649 return 0;
650}
651
652static int add_many_to_attr_set(struct attribute_set* s,
653 struct attribute **attr,
654 unsigned int count)
655{
656 int i, res;
657
658 for (i = 0; i < count; i++) {
659 res = add_to_attr_set(s, attr[i]);
660 if (res)
661 return res;
662 }
663
664 return 0;
665}
666
667static void delete_attr_set(struct attribute_set* s, struct kobject *kobj)
668{
669 sysfs_remove_group(kobj, &s->group);
670 destroy_attr_set(s);
671}
672
673static int parse_strtoul(const char *buf,
674 unsigned long max, unsigned long *value)
675{
676 char *endp;
677
678 *value = simple_strtoul(buf, &endp, 0);
679 while (*endp && isspace(*endp))
680 endp++;
681 if (*endp || *value > max)
682 return -EINVAL;
683
684 return 0;
685}
686
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300687/****************************************************************************
688 ****************************************************************************
689 *
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300690 * Subdrivers
691 *
692 ****************************************************************************
693 ****************************************************************************/
694
695/*************************************************************************
Henrique de Moraes Holschuh142cfc92007-04-21 11:08:26 -0300696 * thinkpad-acpi init subdriver
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300697 */
698
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300699static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700{
701 printk(IBM_INFO "%s v%s\n", IBM_DESC, IBM_VERSION);
702 printk(IBM_INFO "%s\n", IBM_URL);
703
Henrique de Moraes Holschuh3945ac32007-02-06 19:13:44 -0200704 if (ibm_thinkpad_ec_found)
705 printk(IBM_INFO "ThinkPad EC firmware %s\n",
706 ibm_thinkpad_ec_found);
707
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 return 0;
709}
710
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300711static int thinkpad_acpi_driver_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
713 int len = 0;
714
715 len += sprintf(p + len, "driver:\t\t%s\n", IBM_DESC);
716 len += sprintf(p + len, "version:\t%s\n", IBM_VERSION);
717
718 return len;
719}
720
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300721static struct ibm_struct thinkpad_acpi_driver_data = {
722 .name = "driver",
723 .read = thinkpad_acpi_driver_read,
724};
725
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300726/*************************************************************************
727 * Hotkey subdriver
728 */
729
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400730static int hotkey_orig_status;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -0300731static u32 hotkey_orig_mask;
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -0300732static u32 hotkey_all_mask;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -0300733static u32 hotkey_reserved_mask;
734
735static u16 hotkey_keycode_map[] = {
736 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
Henrique de Moraes Holschuh1a343762007-07-18 23:45:36 -0300737 KEY_FN_F1, KEY_FN_F2, KEY_FN_F3, KEY_SLEEP,
738 KEY_FN_F5, KEY_FN_F6, KEY_FN_F7, KEY_FN_F8,
739 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -0300740 /* Scan codes 0x0C to 0x0F: Other ACPI HKEY hot keys */
741 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
742 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
743 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
744 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
745 /* Scan codes 0x10 to 0x1F: Extended ACPI HKEY hot keys */
746 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
747 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
748 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
749 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
750 KEY_RESERVED, /* 0x14: VOLUME UP */
751 KEY_RESERVED, /* 0x15: VOLUME DOWN */
752 KEY_RESERVED, /* 0x16: MUTE */
753 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
754 /* (assignments unknown, please report if found) */
755 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
756 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
757};
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400758
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -0300759static struct attribute_set *hotkey_dev_attributes;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300760
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -0300761static int hotkey_get_wlsw(int *status)
762{
763 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
764 return -EIO;
765 return 0;
766}
767
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300768/* sysfs hotkey enable ------------------------------------------------- */
769static ssize_t hotkey_enable_show(struct device *dev,
770 struct device_attribute *attr,
771 char *buf)
772{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -0300773 int res, status;
774 u32 mask;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300775
776 res = hotkey_get(&status, &mask);
777 if (res)
778 return res;
779
780 return snprintf(buf, PAGE_SIZE, "%d\n", status);
781}
782
783static ssize_t hotkey_enable_store(struct device *dev,
784 struct device_attribute *attr,
785 const char *buf, size_t count)
786{
787 unsigned long t;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -0300788 int res, status;
789 u32 mask;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300790
791 if (parse_strtoul(buf, 1, &t))
792 return -EINVAL;
793
794 res = hotkey_get(&status, &mask);
795 if (!res)
796 res = hotkey_set(t, mask);
797
798 return (res) ? res : count;
799}
800
801static struct device_attribute dev_attr_hotkey_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -0300802 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300803 hotkey_enable_show, hotkey_enable_store);
804
805/* sysfs hotkey mask --------------------------------------------------- */
806static ssize_t hotkey_mask_show(struct device *dev,
807 struct device_attribute *attr,
808 char *buf)
809{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -0300810 int res, status;
811 u32 mask;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300812
813 res = hotkey_get(&status, &mask);
814 if (res)
815 return res;
816
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -0300817 return snprintf(buf, PAGE_SIZE, "0x%08x\n", mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300818}
819
820static ssize_t hotkey_mask_store(struct device *dev,
821 struct device_attribute *attr,
822 const char *buf, size_t count)
823{
824 unsigned long t;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -0300825 int res, status;
826 u32 mask;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300827
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -0300828 if (parse_strtoul(buf, 0xffffffffUL, &t))
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300829 return -EINVAL;
830
831 res = hotkey_get(&status, &mask);
832 if (!res)
833 hotkey_set(status, t);
834
835 return (res) ? res : count;
836}
837
838static struct device_attribute dev_attr_hotkey_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -0300839 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300840 hotkey_mask_show, hotkey_mask_store);
841
842/* sysfs hotkey bios_enabled ------------------------------------------- */
843static ssize_t hotkey_bios_enabled_show(struct device *dev,
844 struct device_attribute *attr,
845 char *buf)
846{
847 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
848}
849
850static struct device_attribute dev_attr_hotkey_bios_enabled =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -0300851 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300852
853/* sysfs hotkey bios_mask ---------------------------------------------- */
854static ssize_t hotkey_bios_mask_show(struct device *dev,
855 struct device_attribute *attr,
856 char *buf)
857{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -0300858 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300859}
860
861static struct device_attribute dev_attr_hotkey_bios_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -0300862 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300863
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -0300864/* sysfs hotkey all_mask ----------------------------------------------- */
865static ssize_t hotkey_all_mask_show(struct device *dev,
866 struct device_attribute *attr,
867 char *buf)
868{
869 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_all_mask);
870}
871
872static struct device_attribute dev_attr_hotkey_all_mask =
873 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
874
875/* sysfs hotkey recommended_mask --------------------------------------- */
876static ssize_t hotkey_recommended_mask_show(struct device *dev,
877 struct device_attribute *attr,
878 char *buf)
879{
880 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
881 hotkey_all_mask & ~hotkey_reserved_mask);
882}
883
884static struct device_attribute dev_attr_hotkey_recommended_mask =
885 __ATTR(hotkey_recommended_mask, S_IRUGO,
886 hotkey_recommended_mask_show, NULL);
887
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -0300888/* sysfs hotkey radio_sw ----------------------------------------------- */
889static ssize_t hotkey_radio_sw_show(struct device *dev,
890 struct device_attribute *attr,
891 char *buf)
892{
893 int res, s;
894 res = hotkey_get_wlsw(&s);
895 if (res < 0)
896 return res;
897
898 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
899}
900
901static struct device_attribute dev_attr_hotkey_radio_sw =
902 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
903
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300904/* --------------------------------------------------------------------- */
905
906static struct attribute *hotkey_mask_attributes[] = {
907 &dev_attr_hotkey_mask.attr,
908 &dev_attr_hotkey_bios_enabled.attr,
909 &dev_attr_hotkey_bios_mask.attr,
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -0300910 &dev_attr_hotkey_all_mask.attr,
911 &dev_attr_hotkey_recommended_mask.attr,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300912};
913
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300914static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300915{
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -0300916 int res, i;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -0300917 int status;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300918
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300919 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
920
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -0300921 BUG_ON(!tpacpi_inputdev);
922
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300923 IBM_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -0300924 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300925
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300926 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300927 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300928
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300929 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300930 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300931
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300932 if (tp_features.hotkey) {
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -0300933 hotkey_dev_attributes = create_attr_set(7, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300934 if (!hotkey_dev_attributes)
935 return -ENOMEM;
936 res = add_to_attr_set(hotkey_dev_attributes,
937 &dev_attr_hotkey_enable.attr);
938 if (res)
939 return res;
940
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300941 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
942 A30, R30, R31, T20-22, X20-21, X22-24 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300943 tp_features.hotkey_mask =
944 acpi_evalf(hkey_handle, NULL, "DHKN", "qv");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300945
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300946 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -0300947 str_supported(tp_features.hotkey_mask));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300948
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -0300949 if (tp_features.hotkey_mask) {
950 /* MHKA available in A31, R40, R40e, T4x, X31, and later */
951 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
952 "MHKA", "qd"))
953 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
954 }
955
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300956 res = hotkey_get(&hotkey_orig_status, &hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300957 if (!res && tp_features.hotkey_mask) {
958 res = add_many_to_attr_set(hotkey_dev_attributes,
959 hotkey_mask_attributes,
960 ARRAY_SIZE(hotkey_mask_attributes));
961 }
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -0300962
963 /* Not all thinkpads have a hardware radio switch */
964 if (!res && acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
965 tp_features.hotkey_wlsw = 1;
966 printk(IBM_INFO
967 "radio switch found; radios are %s\n",
968 enabled(status, 0));
969 res = add_to_attr_set(hotkey_dev_attributes,
970 &dev_attr_hotkey_radio_sw.attr);
971 }
972
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -0300973 if (!res)
974 res = register_attr_set_with_sysfs(
975 hotkey_dev_attributes,
976 &tpacpi_pdev->dev.kobj);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -0300977 if (res)
978 return res;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -0300979
Henrique de Moraes Holschuh1a343762007-07-18 23:45:36 -0300980#ifndef CONFIG_THINKPAD_ACPI_INPUT_ENABLED
981 for (i = 0; i < 12; i++)
982 hotkey_keycode_map[i] = KEY_UNKNOWN;
983#endif /* ! CONFIG_THINKPAD_ACPI_INPUT_ENABLED */
984
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -0300985 set_bit(EV_KEY, tpacpi_inputdev->evbit);
986 set_bit(EV_MSC, tpacpi_inputdev->evbit);
987 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
988 tpacpi_inputdev->keycodesize = sizeof(hotkey_keycode_map[0]);
989 tpacpi_inputdev->keycodemax = ARRAY_SIZE(hotkey_keycode_map);
990 tpacpi_inputdev->keycode = &hotkey_keycode_map;
991 for (i = 0; i < ARRAY_SIZE(hotkey_keycode_map); i++) {
992 if (hotkey_keycode_map[i] != KEY_RESERVED) {
993 set_bit(hotkey_keycode_map[i],
994 tpacpi_inputdev->keybit);
995 } else {
996 if (i < sizeof(hotkey_reserved_mask)*8)
997 hotkey_reserved_mask |= 1 << i;
998 }
999 }
1000
Henrique de Moraes Holschuh1a343762007-07-18 23:45:36 -03001001#ifdef CONFIG_THINKPAD_ACPI_INPUT_ENABLED
1002 dbg_printk(TPACPI_DBG_INIT,
1003 "enabling hot key handling\n");
1004 res = hotkey_set(1, (hotkey_all_mask & ~hotkey_reserved_mask)
1005 | hotkey_orig_mask);
1006 if (res)
1007 return res;
1008#endif /* CONFIG_THINKPAD_ACPI_INPUT_ENABLED */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001009 }
1010
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001011 return (tp_features.hotkey)? 0 : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001012}
1013
1014static void hotkey_exit(void)
1015{
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001016 int res;
1017
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001018 if (tp_features.hotkey) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001019 dbg_printk(TPACPI_DBG_EXIT, "restoring original hotkey mask\n");
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001020 res = hotkey_set(hotkey_orig_status, hotkey_orig_mask);
1021 if (res)
1022 printk(IBM_ERR "failed to restore hotkey to BIOS defaults\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001023 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001024
1025 if (hotkey_dev_attributes) {
1026 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
1027 hotkey_dev_attributes = NULL;
1028 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001029}
1030
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001031static void tpacpi_input_send_key(unsigned int scancode,
1032 unsigned int keycode)
1033{
1034 if (keycode != KEY_RESERVED) {
1035 input_report_key(tpacpi_inputdev, keycode, 1);
1036 if (keycode == KEY_UNKNOWN)
1037 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1038 scancode);
1039 input_sync(tpacpi_inputdev);
1040
1041 input_report_key(tpacpi_inputdev, keycode, 0);
1042 if (keycode == KEY_UNKNOWN)
1043 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1044 scancode);
1045 input_sync(tpacpi_inputdev);
1046 }
1047}
1048
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001049static void hotkey_notify(struct ibm_struct *ibm, u32 event)
1050{
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001051 u32 hkey;
1052 unsigned int keycode, scancode;
1053 int sendacpi = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001054
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001055 if (event == 0x80 && acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001056 if (tpacpi_inputdev->users > 0) {
1057 switch (hkey >> 12) {
1058 case 1:
1059 /* 0x1000-0x1FFF: key presses */
1060 scancode = hkey & 0xfff;
1061 if (scancode > 0 && scancode < 0x21) {
1062 scancode--;
1063 keycode = hotkey_keycode_map[scancode];
1064 tpacpi_input_send_key(scancode, keycode);
1065 sendacpi = (keycode == KEY_RESERVED
1066 || keycode == KEY_UNKNOWN);
1067 } else {
1068 printk(IBM_ERR
1069 "hotkey 0x%04x out of range for keyboard map\n",
1070 hkey);
1071 }
1072 break;
1073 case 5:
1074 /* 0x5000-0x5FFF: LID */
1075 /* we don't handle it through this path, just
1076 * eat up known LID events */
1077 if (hkey != 0x5001 && hkey != 0x5002) {
1078 printk(IBM_ERR
1079 "unknown LID-related hotkey event: 0x%04x\n",
1080 hkey);
1081 }
1082 break;
1083 default:
1084 /* case 2: dock-related */
1085 /* 0x2305 - T43 waking up due to bay lever eject while aslept */
1086 /* case 3: ultra-bay related. maybe bay in dock? */
1087 /* 0x3003 - T43 after wake up by bay lever eject (0x2305) */
1088 printk(IBM_NOTICE "unhandled hotkey event 0x%04x\n", hkey);
1089 }
1090 }
1091
1092 if (sendacpi)
1093 acpi_bus_generate_event(ibm->acpi->device, event, hkey);
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001094 } else {
1095 printk(IBM_ERR "unknown hotkey notification event %d\n", event);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001096 acpi_bus_generate_event(ibm->acpi->device, event, 0);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001097 }
1098}
1099
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001100/*
1101 * Call with hotkey_mutex held
1102 */
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001103static int hotkey_get(int *status, u32 *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104{
1105 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001106 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001107
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001108 if (tp_features.hotkey_mask)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001109 if (!acpi_evalf(hkey_handle, mask, "DHKN", "d"))
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001110 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001111
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001112 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113}
1114
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001115/*
1116 * Call with hotkey_mutex held
1117 */
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001118static int hotkey_set(int status, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119{
1120 int i;
1121
1122 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001123 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001125 if (tp_features.hotkey_mask)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001126 for (i = 0; i < 32; i++) {
1127 int bit = ((1 << i) & mask) != 0;
1128 if (!acpi_evalf(hkey_handle,
1129 NULL, "MHKM", "vdd", i + 1, bit))
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001130 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001131 }
1132
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001133 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001134}
1135
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001136/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001137static int hotkey_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001139 int res, status;
1140 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 int len = 0;
1142
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001143 if (!tp_features.hotkey) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001144 len += sprintf(p + len, "status:\t\tnot supported\n");
1145 return len;
1146 }
1147
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001148 res = mutex_lock_interruptible(&hotkey_mutex);
1149 if (res < 0)
1150 return res;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001151 res = hotkey_get(&status, &mask);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001152 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001153 if (res)
1154 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
1156 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001157 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001158 len += sprintf(p + len, "mask:\t\t0x%08x\n", mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 len += sprintf(p + len,
1160 "commands:\tenable, disable, reset, <mask>\n");
1161 } else {
1162 len += sprintf(p + len, "mask:\t\tnot supported\n");
1163 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
1164 }
1165
1166 return len;
1167}
1168
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001169static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001171 int res, status;
1172 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 char *cmd;
1174 int do_cmd = 0;
1175
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001176 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 return -ENODEV;
1178
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001179 res = mutex_lock_interruptible(&hotkey_mutex);
1180 if (res < 0)
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001181 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001182
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001183 res = hotkey_get(&status, &mask);
1184 if (res)
1185 goto errexit;
1186
1187 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 while ((cmd = next_cmd(&buf))) {
1189 if (strlencmp(cmd, "enable") == 0) {
1190 status = 1;
1191 } else if (strlencmp(cmd, "disable") == 0) {
1192 status = 0;
1193 } else if (strlencmp(cmd, "reset") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001194 status = hotkey_orig_status;
1195 mask = hotkey_orig_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
1197 /* mask set */
1198 } else if (sscanf(cmd, "%x", &mask) == 1) {
1199 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001200 } else {
1201 res = -EINVAL;
1202 goto errexit;
1203 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 do_cmd = 1;
1205 }
1206
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001207 if (do_cmd)
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001208 res = hotkey_set(status, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001210errexit:
1211 mutex_unlock(&hotkey_mutex);
1212 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001213}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001215static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001216 .hid = IBM_HKEY_HID,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001217 .notify = hotkey_notify,
1218 .handle = &hkey_handle,
1219 .type = ACPI_DEVICE_NOTIFY,
1220};
1221
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001222static struct ibm_struct hotkey_driver_data = {
1223 .name = "hotkey",
1224 .read = hotkey_read,
1225 .write = hotkey_write,
1226 .exit = hotkey_exit,
1227 .acpi = &ibm_hotkey_acpidriver,
1228};
1229
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001230/*************************************************************************
1231 * Bluetooth subdriver
1232 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001234/* sysfs bluetooth enable ---------------------------------------------- */
1235static ssize_t bluetooth_enable_show(struct device *dev,
1236 struct device_attribute *attr,
1237 char *buf)
1238{
1239 int status;
1240
1241 status = bluetooth_get_radiosw();
1242 if (status < 0)
1243 return status;
1244
1245 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
1246}
1247
1248static ssize_t bluetooth_enable_store(struct device *dev,
1249 struct device_attribute *attr,
1250 const char *buf, size_t count)
1251{
1252 unsigned long t;
1253 int res;
1254
1255 if (parse_strtoul(buf, 1, &t))
1256 return -EINVAL;
1257
1258 res = bluetooth_set_radiosw(t);
1259
1260 return (res) ? res : count;
1261}
1262
1263static struct device_attribute dev_attr_bluetooth_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001264 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001265 bluetooth_enable_show, bluetooth_enable_store);
1266
1267/* --------------------------------------------------------------------- */
1268
1269static struct attribute *bluetooth_attributes[] = {
1270 &dev_attr_bluetooth_enable.attr,
1271 NULL
1272};
1273
1274static const struct attribute_group bluetooth_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001275 .attrs = bluetooth_attributes,
1276};
1277
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001278static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001280 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001281 int status = 0;
1282
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001283 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
1284
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001285 IBM_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001286
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001287 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1288 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001289 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001290 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001292 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
1293 str_supported(tp_features.bluetooth),
1294 status);
1295
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001296 if (tp_features.bluetooth) {
1297 if (!(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
1298 /* no bluetooth hardware present in system */
1299 tp_features.bluetooth = 0;
1300 dbg_printk(TPACPI_DBG_INIT,
1301 "bluetooth hardware not installed\n");
1302 } else {
1303 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
1304 &bluetooth_attr_group);
1305 if (res)
1306 return res;
1307 }
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001308 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001309
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001310 return (tp_features.bluetooth)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311}
1312
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001313static void bluetooth_exit(void)
1314{
1315 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
1316 &bluetooth_attr_group);
1317}
1318
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001319static int bluetooth_get_radiosw(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320{
1321 int status;
1322
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001323 if (!tp_features.bluetooth)
1324 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001326 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
1327 return -EIO;
1328
1329 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
1330}
1331
1332static int bluetooth_set_radiosw(int radio_on)
1333{
1334 int status;
1335
1336 if (!tp_features.bluetooth)
1337 return -ENODEV;
1338
1339 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
1340 return -EIO;
1341 if (radio_on)
1342 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
1343 else
1344 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
1345 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
1346 return -EIO;
1347
1348 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349}
1350
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001351/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001352static int bluetooth_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353{
1354 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001355 int status = bluetooth_get_radiosw();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001357 if (!tp_features.bluetooth)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 len += sprintf(p + len, "status:\t\tnot supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001360 len += sprintf(p + len, "status:\t\t%s\n",
1361 (status)? "enabled" : "disabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 len += sprintf(p + len, "commands:\tenable, disable\n");
1363 }
1364
1365 return len;
1366}
1367
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001368static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001372 if (!tp_features.bluetooth)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001373 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
1375 while ((cmd = next_cmd(&buf))) {
1376 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001377 bluetooth_set_radiosw(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001379 bluetooth_set_radiosw(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 } else
1381 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 }
1383
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 return 0;
1385}
1386
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001387static struct ibm_struct bluetooth_driver_data = {
1388 .name = "bluetooth",
1389 .read = bluetooth_read,
1390 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001391 .exit = bluetooth_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001392};
1393
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001394/*************************************************************************
1395 * Wan subdriver
1396 */
1397
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001398/* sysfs wan enable ---------------------------------------------------- */
1399static ssize_t wan_enable_show(struct device *dev,
1400 struct device_attribute *attr,
1401 char *buf)
1402{
1403 int status;
1404
1405 status = wan_get_radiosw();
1406 if (status < 0)
1407 return status;
1408
1409 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
1410}
1411
1412static ssize_t wan_enable_store(struct device *dev,
1413 struct device_attribute *attr,
1414 const char *buf, size_t count)
1415{
1416 unsigned long t;
1417 int res;
1418
1419 if (parse_strtoul(buf, 1, &t))
1420 return -EINVAL;
1421
1422 res = wan_set_radiosw(t);
1423
1424 return (res) ? res : count;
1425}
1426
1427static struct device_attribute dev_attr_wan_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001428 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001429 wan_enable_show, wan_enable_store);
1430
1431/* --------------------------------------------------------------------- */
1432
1433static struct attribute *wan_attributes[] = {
1434 &dev_attr_wan_enable.attr,
1435 NULL
1436};
1437
1438static const struct attribute_group wan_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001439 .attrs = wan_attributes,
1440};
1441
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001442static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001443{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001444 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001445 int status = 0;
1446
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001447 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
1448
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001449 IBM_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001450
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001451 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001452 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001453
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001454 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
1455 str_supported(tp_features.wan),
1456 status);
1457
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001458 if (tp_features.wan) {
1459 if (!(status & TP_ACPI_WANCARD_HWPRESENT)) {
1460 /* no wan hardware present in system */
1461 tp_features.wan = 0;
1462 dbg_printk(TPACPI_DBG_INIT,
1463 "wan hardware not installed\n");
1464 } else {
1465 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
1466 &wan_attr_group);
1467 if (res)
1468 return res;
1469 }
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001470 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001471
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001472 return (tp_features.wan)? 0 : 1;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001473}
1474
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001475static void wan_exit(void)
1476{
1477 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
1478 &wan_attr_group);
1479}
1480
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001481static int wan_get_radiosw(void)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001482{
1483 int status;
1484
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001485 if (!tp_features.wan)
1486 return -ENODEV;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001487
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001488 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
1489 return -EIO;
1490
1491 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
1492}
1493
1494static int wan_set_radiosw(int radio_on)
1495{
1496 int status;
1497
1498 if (!tp_features.wan)
1499 return -ENODEV;
1500
1501 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
1502 return -EIO;
1503 if (radio_on)
1504 status |= TP_ACPI_WANCARD_RADIOSSW;
1505 else
1506 status &= ~TP_ACPI_WANCARD_RADIOSSW;
1507 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
1508 return -EIO;
1509
1510 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001511}
1512
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001513/* procfs -------------------------------------------------------------- */
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001514static int wan_read(char *p)
1515{
1516 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001517 int status = wan_get_radiosw();
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001518
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001519 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001520 len += sprintf(p + len, "status:\t\tnot supported\n");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001521 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001522 len += sprintf(p + len, "status:\t\t%s\n",
1523 (status)? "enabled" : "disabled");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001524 len += sprintf(p + len, "commands:\tenable, disable\n");
1525 }
1526
1527 return len;
1528}
1529
1530static int wan_write(char *buf)
1531{
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001532 char *cmd;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001533
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001534 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001535 return -ENODEV;
1536
1537 while ((cmd = next_cmd(&buf))) {
1538 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001539 wan_set_radiosw(1);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001540 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03001541 wan_set_radiosw(0);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001542 } else
1543 return -EINVAL;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001544 }
1545
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04001546 return 0;
1547}
1548
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001549static struct ibm_struct wan_driver_data = {
1550 .name = "wan",
1551 .read = wan_read,
1552 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03001553 .exit = wan_exit,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03001554 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001555};
1556
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001557/*************************************************************************
1558 * Video subdriver
1559 */
1560
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02001561static enum video_access_mode video_supported;
1562static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001563
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001564IBM_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
1565 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
1566 "\\_SB.PCI0.VID0", /* 770e */
1567 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
1568 "\\_SB.PCI0.AGP.VID", /* all others */
1569 ); /* R30, R31 */
1570
1571IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
1572
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001573static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001575 int ivga;
1576
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001577 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
1578
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001579 IBM_ACPIHANDLE_INIT(vid);
1580 IBM_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001581
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001582 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
1583 /* G41, assume IVGA doesn't change */
1584 vid_handle = vid2_handle;
1585
1586 if (!vid_handle)
1587 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001588 video_supported = TPACPI_VIDEO_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001589 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
1590 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001591 video_supported = TPACPI_VIDEO_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001592 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
1593 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001594 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001595 else
1596 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001597 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001599 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
1600 str_supported(video_supported != TPACPI_VIDEO_NONE),
1601 video_supported);
1602
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001603 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604}
1605
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001606static void video_exit(void)
1607{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001608 dbg_printk(TPACPI_DBG_EXIT,
1609 "restoring original video autoswitch mode\n");
1610 if (video_autosw_set(video_orig_autosw))
1611 printk(IBM_ERR "error while trying to restore original "
1612 "video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001613}
1614
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001615static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616{
1617 int status = 0;
1618 int i;
1619
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001620 switch (video_supported) {
1621 case TPACPI_VIDEO_570:
1622 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
1623 TP_ACPI_VIDEO_570_PHSCMD))
1624 return -EIO;
1625 status = i & TP_ACPI_VIDEO_570_PHSMASK;
1626 break;
1627 case TPACPI_VIDEO_770:
1628 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
1629 return -EIO;
1630 if (i)
1631 status |= TP_ACPI_VIDEO_S_LCD;
1632 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
1633 return -EIO;
1634 if (i)
1635 status |= TP_ACPI_VIDEO_S_CRT;
1636 break;
1637 case TPACPI_VIDEO_NEW:
1638 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
1639 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
1640 return -EIO;
1641 if (i)
1642 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001644 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
1645 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
1646 return -EIO;
1647 if (i)
1648 status |= TP_ACPI_VIDEO_S_LCD;
1649 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
1650 return -EIO;
1651 if (i)
1652 status |= TP_ACPI_VIDEO_S_DVI;
1653 break;
1654 default:
1655 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001656 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
1658 return status;
1659}
1660
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001661static int video_outputsw_set(int status)
1662{
1663 int autosw;
1664 int res = 0;
1665
1666 switch (video_supported) {
1667 case TPACPI_VIDEO_570:
1668 res = acpi_evalf(NULL, NULL,
1669 "\\_SB.PHS2", "vdd",
1670 TP_ACPI_VIDEO_570_PHS2CMD,
1671 status | TP_ACPI_VIDEO_570_PHS2SET);
1672 break;
1673 case TPACPI_VIDEO_770:
1674 autosw = video_autosw_get();
1675 if (autosw < 0)
1676 return autosw;
1677
1678 res = video_autosw_set(1);
1679 if (res)
1680 return res;
1681 res = acpi_evalf(vid_handle, NULL,
1682 "ASWT", "vdd", status * 0x100, 0);
1683 if (!autosw && video_autosw_set(autosw)) {
1684 printk(IBM_ERR "video auto-switch left enabled due to error\n");
1685 return -EIO;
1686 }
1687 break;
1688 case TPACPI_VIDEO_NEW:
1689 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
1690 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
1691 break;
1692 default:
1693 return -ENOSYS;
1694 }
1695
1696 return (res)? 0 : -EIO;
1697}
1698
1699static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001701 int autosw = 0;
1702
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001703 switch (video_supported) {
1704 case TPACPI_VIDEO_570:
1705 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
1706 return -EIO;
1707 break;
1708 case TPACPI_VIDEO_770:
1709 case TPACPI_VIDEO_NEW:
1710 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
1711 return -EIO;
1712 break;
1713 default:
1714 return -ENOSYS;
1715 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001716
1717 return autosw & 1;
1718}
1719
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001720static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001721{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001722 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001723 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001724 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001725}
1726
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001727static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001728{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001729 int autosw = video_autosw_get();
1730 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001731
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001732 if (autosw < 0)
1733 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001734
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001735 switch (video_supported) {
1736 case TPACPI_VIDEO_570:
1737 res = video_autosw_set(1);
1738 if (res)
1739 return res;
1740 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
1741 break;
1742 case TPACPI_VIDEO_770:
1743 case TPACPI_VIDEO_NEW:
1744 res = video_autosw_set(1);
1745 if (res)
1746 return res;
1747 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
1748 break;
1749 default:
1750 return -ENOSYS;
1751 }
1752 if (!autosw && video_autosw_set(autosw)) {
1753 printk(IBM_ERR "video auto-switch left enabled due to error\n");
1754 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001755 }
1756
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001757 return (res)? 0 : -EIO;
1758}
1759
1760static int video_expand_toggle(void)
1761{
1762 switch (video_supported) {
1763 case TPACPI_VIDEO_570:
1764 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
1765 0 : -EIO;
1766 case TPACPI_VIDEO_770:
1767 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
1768 0 : -EIO;
1769 case TPACPI_VIDEO_NEW:
1770 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
1771 0 : -EIO;
1772 default:
1773 return -ENOSYS;
1774 }
1775 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001776}
1777
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001778static int video_read(char *p)
1779{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001780 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001781 int len = 0;
1782
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001783 if (video_supported == TPACPI_VIDEO_NONE) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001784 len += sprintf(p + len, "status:\t\tnot supported\n");
1785 return len;
1786 }
1787
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001788 status = video_outputsw_get();
1789 if (status < 0)
1790 return status;
1791
1792 autosw = video_autosw_get();
1793 if (autosw < 0)
1794 return autosw;
1795
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001796 len += sprintf(p + len, "status:\t\tsupported\n");
1797 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
1798 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001799 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001800 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
1801 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
1802 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
1803 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001804 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001805 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
1806 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
1807 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
1808
1809 return len;
1810}
1811
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001812static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813{
1814 char *cmd;
1815 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001816 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001818 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001819 return -ENODEV;
1820
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001821 enable = 0;
1822 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823
1824 while ((cmd = next_cmd(&buf))) {
1825 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001826 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001828 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001830 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001832 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001833 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001834 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001835 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03001836 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001837 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001838 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001840 res = video_autosw_set(1);
1841 if (res)
1842 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001844 res = video_autosw_set(0);
1845 if (res)
1846 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001848 res = video_outputsw_cycle();
1849 if (res)
1850 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001852 res = video_expand_toggle();
1853 if (res)
1854 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 } else
1856 return -EINVAL;
1857 }
1858
1859 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03001860 status = video_outputsw_get();
1861 if (status < 0)
1862 return status;
1863 res = video_outputsw_set((status & ~disable) | enable);
1864 if (res)
1865 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 }
1867
1868 return 0;
1869}
1870
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001871static struct ibm_struct video_driver_data = {
1872 .name = "video",
1873 .read = video_read,
1874 .write = video_write,
1875 .exit = video_exit,
1876};
1877
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001878/*************************************************************************
1879 * Light (thinklight) subdriver
1880 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001882IBM_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
1883IBM_HANDLE(ledb, ec, "LEDB"); /* G4x */
1884
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001885static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001887 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
1888
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001889 IBM_ACPIHANDLE_INIT(ledb);
1890 IBM_ACPIHANDLE_INIT(lght);
1891 IBM_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001892
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001893 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001894 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001895
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001896 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001897 /* light status not supported on
1898 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001899 tp_features.light_status =
1900 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001902 vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001903 str_supported(tp_features.light));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001904
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001905 return (tp_features.light)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906}
1907
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001908static int light_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909{
1910 int len = 0;
1911 int status = 0;
1912
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001913 if (!tp_features.light) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001914 len += sprintf(p + len, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001915 } else if (!tp_features.light_status) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001916 len += sprintf(p + len, "status:\t\tunknown\n");
1917 len += sprintf(p + len, "commands:\ton, off\n");
1918 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
1920 return -EIO;
1921 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001922 len += sprintf(p + len, "commands:\ton, off\n");
1923 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924
1925 return len;
1926}
1927
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001928static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929{
1930 int cmos_cmd, lght_cmd;
1931 char *cmd;
1932 int success;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001933
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001934 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001935 return -ENODEV;
1936
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 while ((cmd = next_cmd(&buf))) {
1938 if (strlencmp(cmd, "on") == 0) {
1939 cmos_cmd = 0x0c;
1940 lght_cmd = 1;
1941 } else if (strlencmp(cmd, "off") == 0) {
1942 cmos_cmd = 0x0d;
1943 lght_cmd = 0;
1944 } else
1945 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001946
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 success = cmos_handle ?
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001948 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
1949 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 if (!success)
1951 return -EIO;
1952 }
1953
1954 return 0;
1955}
1956
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001957static struct ibm_struct light_driver_data = {
1958 .name = "light",
1959 .read = light_read,
1960 .write = light_write,
1961};
1962
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001963/*************************************************************************
1964 * Dock subdriver
1965 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03001967#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001968
1969IBM_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
1970 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
1971 "\\_SB.PCI0.PCI1.DOCK", /* all others */
1972 "\\_SB.PCI.ISA.SLCE", /* 570 */
1973 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
1974
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001975/* don't list other alternatives as we install a notify handler on the 570 */
1976IBM_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
1977
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03001978static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
1979 {
1980 .notify = dock_notify,
1981 .handle = &dock_handle,
1982 .type = ACPI_SYSTEM_NOTIFY,
1983 },
1984 {
1985 .hid = IBM_PCI_HID,
1986 .notify = dock_notify,
1987 .handle = &pci_handle,
1988 .type = ACPI_SYSTEM_NOTIFY,
1989 },
1990};
1991
1992static struct ibm_struct dock_driver_data[2] = {
1993 {
1994 .name = "dock",
1995 .read = dock_read,
1996 .write = dock_write,
1997 .acpi = &ibm_dock_acpidriver[0],
1998 },
1999 {
2000 .name = "dock",
2001 .acpi = &ibm_dock_acpidriver[1],
2002 },
2003};
2004
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005#define dock_docked() (_sta(dock_handle) & 1)
2006
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002007static int __init dock_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002008{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002009 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
2010
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002011 IBM_ACPIHANDLE_INIT(dock);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002012
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002013 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
2014 str_supported(dock_handle != NULL));
2015
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002016 return (dock_handle)? 0 : 1;
2017}
2018
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03002019static int __init dock_init2(struct ibm_init_struct *iibm)
2020{
2021 int dock2_needed;
2022
2023 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
2024
2025 if (dock_driver_data[0].flags.acpi_driver_registered &&
2026 dock_driver_data[0].flags.acpi_notify_installed) {
2027 IBM_ACPIHANDLE_INIT(pci);
2028 dock2_needed = (pci_handle != NULL);
2029 vdbg_printk(TPACPI_DBG_INIT,
2030 "dock PCI handler for the TP 570 is %s\n",
2031 str_supported(dock2_needed));
2032 } else {
2033 vdbg_printk(TPACPI_DBG_INIT,
2034 "dock subdriver part 2 not required\n");
2035 dock2_needed = 0;
2036 }
2037
2038 return (dock2_needed)? 0 : 1;
2039}
2040
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002041static void dock_notify(struct ibm_struct *ibm, u32 event)
2042{
2043 int docked = dock_docked();
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002044 int pci = ibm->acpi->hid && strstr(ibm->acpi->hid, IBM_PCI_HID);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002045
2046 if (event == 1 && !pci) /* 570 */
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002047 acpi_bus_generate_event(ibm->acpi->device, event, 1); /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002048 else if (event == 1 && pci) /* 570 */
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002049 acpi_bus_generate_event(ibm->acpi->device, event, 3); /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002050 else if (event == 3 && docked)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002051 acpi_bus_generate_event(ibm->acpi->device, event, 1); /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002052 else if (event == 3 && !docked)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002053 acpi_bus_generate_event(ibm->acpi->device, event, 2); /* undock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002054 else if (event == 0 && docked)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002055 acpi_bus_generate_event(ibm->acpi->device, event, 3); /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002056 else {
2057 printk(IBM_ERR "unknown dock event %d, status %d\n",
2058 event, _sta(dock_handle));
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002059 acpi_bus_generate_event(ibm->acpi->device, event, 0); /* unknown */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002060 }
2061}
2062
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002063static int dock_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064{
2065 int len = 0;
2066 int docked = dock_docked();
2067
2068 if (!dock_handle)
2069 len += sprintf(p + len, "status:\t\tnot supported\n");
2070 else if (!docked)
2071 len += sprintf(p + len, "status:\t\tundocked\n");
2072 else {
2073 len += sprintf(p + len, "status:\t\tdocked\n");
2074 len += sprintf(p + len, "commands:\tdock, undock\n");
2075 }
2076
2077 return len;
2078}
2079
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002080static int dock_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081{
2082 char *cmd;
2083
2084 if (!dock_docked())
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002085 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086
2087 while ((cmd = next_cmd(&buf))) {
2088 if (strlencmp(cmd, "undock") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002089 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
2090 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 return -EIO;
2092 } else if (strlencmp(cmd, "dock") == 0) {
2093 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
2094 return -EIO;
2095 } else
2096 return -EINVAL;
2097 }
2098
2099 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002100}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03002102#endif /* CONFIG_THINKPAD_ACPI_DOCK */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002104/*************************************************************************
2105 * Bay subdriver
2106 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03002108#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002109IBM_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
2110 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
2111 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
2112 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
2113 ); /* A21e, R30, R31 */
2114IBM_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
2115 "_EJ0", /* all others */
2116 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
2117IBM_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
2118 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
2119 ); /* all others */
2120IBM_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
2121 "_EJ0", /* 770x */
2122 ); /* all others */
2123
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002124static int __init bay_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002126 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
2127
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002128 IBM_ACPIHANDLE_INIT(bay);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002129 if (bay_handle)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002130 IBM_ACPIHANDLE_INIT(bay_ej);
2131 IBM_ACPIHANDLE_INIT(bay2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002132 if (bay2_handle)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002133 IBM_ACPIHANDLE_INIT(bay2_ej);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002134
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002135 tp_features.bay_status = bay_handle &&
2136 acpi_evalf(bay_handle, NULL, "_STA", "qv");
2137 tp_features.bay_status2 = bay2_handle &&
2138 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002139
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002140 tp_features.bay_eject = bay_handle && bay_ej_handle &&
2141 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
2142 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
2143 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002145 vdbg_printk(TPACPI_DBG_INIT,
2146 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002147 str_supported(tp_features.bay_status),
2148 str_supported(tp_features.bay_eject),
2149 str_supported(tp_features.bay_status2),
2150 str_supported(tp_features.bay_eject2));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002151
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002152 return (tp_features.bay_status || tp_features.bay_eject ||
2153 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154}
2155
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002156static void bay_notify(struct ibm_struct *ibm, u32 event)
2157{
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002158 acpi_bus_generate_event(ibm->acpi->device, event, 0);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002159}
2160
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002161#define bay_occupied(b) (_sta(b##_handle) & 1)
2162
2163static int bay_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164{
2165 int len = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002166 int occupied = bay_occupied(bay);
2167 int occupied2 = bay_occupied(bay2);
2168 int eject, eject2;
2169
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002170 len += sprintf(p + len, "status:\t\t%s\n",
2171 tp_features.bay_status ?
2172 (occupied ? "occupied" : "unoccupied") :
2173 "not supported");
2174 if (tp_features.bay_status2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002175 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
2176 "occupied" : "unoccupied");
2177
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002178 eject = tp_features.bay_eject && occupied;
2179 eject2 = tp_features.bay_eject2 && occupied2;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002180
2181 if (eject && eject2)
2182 len += sprintf(p + len, "commands:\teject, eject2\n");
2183 else if (eject)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 len += sprintf(p + len, "commands:\teject\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002185 else if (eject2)
2186 len += sprintf(p + len, "commands:\teject2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187
2188 return len;
2189}
2190
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002191static int bay_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192{
2193 char *cmd;
2194
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002195 if (!tp_features.bay_eject && !tp_features.bay_eject2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002196 return -ENODEV;
2197
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002199 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002200 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
2201 return -EIO;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002202 } else if (tp_features.bay_eject2 &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002203 strlencmp(cmd, "eject2") == 0) {
2204 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 return -EIO;
2206 } else
2207 return -EINVAL;
2208 }
2209
2210 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002211}
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002212
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002213static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
2214 .notify = bay_notify,
2215 .handle = &bay_handle,
2216 .type = ACPI_SYSTEM_NOTIFY,
2217};
2218
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002219static struct ibm_struct bay_driver_data = {
2220 .name = "bay",
2221 .read = bay_read,
2222 .write = bay_write,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002223 .acpi = &ibm_bay_acpidriver,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002224};
2225
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03002226#endif /* CONFIG_THINKPAD_ACPI_BAY */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002228/*************************************************************************
2229 * CMOS subdriver
2230 */
2231
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03002232/* sysfs cmos_command -------------------------------------------------- */
2233static ssize_t cmos_command_store(struct device *dev,
2234 struct device_attribute *attr,
2235 const char *buf, size_t count)
2236{
2237 unsigned long cmos_cmd;
2238 int res;
2239
2240 if (parse_strtoul(buf, 21, &cmos_cmd))
2241 return -EINVAL;
2242
2243 res = issue_thinkpad_cmos_command(cmos_cmd);
2244 return (res)? res : count;
2245}
2246
2247static struct device_attribute dev_attr_cmos_command =
2248 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
2249
2250/* --------------------------------------------------------------------- */
2251
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002252static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002253{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03002254 int res;
2255
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002256 vdbg_printk(TPACPI_DBG_INIT,
2257 "initializing cmos commands subdriver\n");
2258
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002259 IBM_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002260
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002261 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
2262 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03002263
2264 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
2265 if (res)
2266 return res;
2267
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002268 return (cmos_handle)? 0 : 1;
2269}
2270
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03002271static void cmos_exit(void)
2272{
2273 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
2274}
2275
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002276static int cmos_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277{
2278 int len = 0;
2279
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002280 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2281 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 if (!cmos_handle)
2283 len += sprintf(p + len, "status:\t\tnot supported\n");
2284 else {
2285 len += sprintf(p + len, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002286 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 }
2288
2289 return len;
2290}
2291
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002292static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293{
2294 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03002295 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296
2297 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002298 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
2299 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 /* cmos_cmd set */
2301 } else
2302 return -EINVAL;
2303
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03002304 res = issue_thinkpad_cmos_command(cmos_cmd);
2305 if (res)
2306 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 }
2308
2309 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002310}
2311
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002312static struct ibm_struct cmos_driver_data = {
2313 .name = "cmos",
2314 .read = cmos_read,
2315 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03002316 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002317};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002318
2319/*************************************************************************
2320 * LED subdriver
2321 */
2322
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02002323static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002324
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002325IBM_HANDLE(led, ec, "SLED", /* 570 */
2326 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
2327 "LED", /* all others */
2328 ); /* R30, R31 */
2329
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002330static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002331{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002332 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
2333
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002334 IBM_ACPIHANDLE_INIT(led);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002335
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002336 if (!led_handle)
2337 /* led not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002338 led_supported = TPACPI_LED_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002339 else if (strlencmp(led_path, "SLED") == 0)
2340 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002341 led_supported = TPACPI_LED_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002342 else if (strlencmp(led_path, "SYSL") == 0)
2343 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002344 led_supported = TPACPI_LED_OLD;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002345 else
2346 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002347 led_supported = TPACPI_LED_NEW;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002348
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002349 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
2350 str_supported(led_supported), led_supported);
2351
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002352 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002353}
2354
2355#define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
2356
2357static int led_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358{
2359 int len = 0;
2360
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002361 if (!led_supported) {
2362 len += sprintf(p + len, "status:\t\tnot supported\n");
2363 return len;
2364 }
2365 len += sprintf(p + len, "status:\t\tsupported\n");
2366
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002367 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002368 /* 570 */
2369 int i, status;
2370 for (i = 0; i < 8; i++) {
2371 if (!acpi_evalf(ec_handle,
2372 &status, "GLED", "dd", 1 << i))
2373 return -EIO;
2374 len += sprintf(p + len, "%d:\t\t%s\n",
2375 i, led_status(status));
2376 }
2377 }
2378
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 len += sprintf(p + len, "commands:\t"
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002380 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381
2382 return len;
2383}
2384
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002385/* off, on, blink */
2386static const int led_sled_arg1[] = { 0, 1, 3 };
2387static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
2388static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
2389static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
2390
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002391static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392{
2393 char *cmd;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002394 int led, ind, ret;
2395
2396 if (!led_supported)
2397 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398
2399 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002400 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 return -EINVAL;
2402
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002403 if (strstr(cmd, "off")) {
2404 ind = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 } else if (strstr(cmd, "on")) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002406 ind = 1;
2407 } else if (strstr(cmd, "blink")) {
2408 ind = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 } else
2410 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002411
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002412 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002413 /* 570 */
2414 led = 1 << led;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002416 led, led_sled_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 return -EIO;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002418 } else if (led_supported == TPACPI_LED_OLD) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002419 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
2420 led = 1 << led;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002421 ret = ec_write(TPACPI_LED_EC_HLMS, led);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002422 if (ret >= 0)
2423 ret =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002424 ec_write(TPACPI_LED_EC_HLBL,
Henrique de Moraes Holschuhe062e032007-03-23 17:33:55 -03002425 led * led_exp_hlbl[ind]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002426 if (ret >= 0)
2427 ret =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002428 ec_write(TPACPI_LED_EC_HLCL,
Henrique de Moraes Holschuhe062e032007-03-23 17:33:55 -03002429 led * led_exp_hlcl[ind]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002430 if (ret < 0)
2431 return ret;
2432 } else {
2433 /* all others */
2434 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
2435 led, led_led_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002437 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 }
2439
2440 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002441}
2442
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002443static struct ibm_struct led_driver_data = {
2444 .name = "led",
2445 .read = led_read,
2446 .write = led_write,
2447};
2448
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002449/*************************************************************************
2450 * Beep subdriver
2451 */
2452
2453IBM_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
2454
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002455static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002456{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002457 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
2458
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002459 IBM_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002460
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002461 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
2462 str_supported(beep_handle != NULL));
2463
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002464 return (beep_handle)? 0 : 1;
2465}
2466
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002467static int beep_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468{
2469 int len = 0;
2470
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002471 if (!beep_handle)
2472 len += sprintf(p + len, "status:\t\tnot supported\n");
2473 else {
2474 len += sprintf(p + len, "status:\t\tsupported\n");
2475 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
2476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477
2478 return len;
2479}
2480
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002481static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482{
2483 char *cmd;
2484 int beep_cmd;
2485
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002486 if (!beep_handle)
2487 return -ENODEV;
2488
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002490 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
2491 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 /* beep_cmd set */
2493 } else
2494 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002495 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 return -EIO;
2497 }
2498
2499 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002500}
2501
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002502static struct ibm_struct beep_driver_data = {
2503 .name = "beep",
2504 .read = beep_read,
2505 .write = beep_write,
2506};
2507
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002508/*************************************************************************
2509 * Thermal subdriver
2510 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002511
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002512static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002513
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03002514/* sysfs temp##_input -------------------------------------------------- */
2515
2516static ssize_t thermal_temp_input_show(struct device *dev,
2517 struct device_attribute *attr,
2518 char *buf)
2519{
2520 struct sensor_device_attribute *sensor_attr =
2521 to_sensor_dev_attr(attr);
2522 int idx = sensor_attr->index;
2523 s32 value;
2524 int res;
2525
2526 res = thermal_get_sensor(idx, &value);
2527 if (res)
2528 return res;
2529 if (value == TP_EC_THERMAL_TMP_NA * 1000)
2530 return -ENXIO;
2531
2532 return snprintf(buf, PAGE_SIZE, "%d\n", value);
2533}
2534
2535#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
2536 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, thermal_temp_input_show, NULL, _idxB)
2537
2538static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
2539 THERMAL_SENSOR_ATTR_TEMP(1, 0),
2540 THERMAL_SENSOR_ATTR_TEMP(2, 1),
2541 THERMAL_SENSOR_ATTR_TEMP(3, 2),
2542 THERMAL_SENSOR_ATTR_TEMP(4, 3),
2543 THERMAL_SENSOR_ATTR_TEMP(5, 4),
2544 THERMAL_SENSOR_ATTR_TEMP(6, 5),
2545 THERMAL_SENSOR_ATTR_TEMP(7, 6),
2546 THERMAL_SENSOR_ATTR_TEMP(8, 7),
2547 THERMAL_SENSOR_ATTR_TEMP(9, 8),
2548 THERMAL_SENSOR_ATTR_TEMP(10, 9),
2549 THERMAL_SENSOR_ATTR_TEMP(11, 10),
2550 THERMAL_SENSOR_ATTR_TEMP(12, 11),
2551 THERMAL_SENSOR_ATTR_TEMP(13, 12),
2552 THERMAL_SENSOR_ATTR_TEMP(14, 13),
2553 THERMAL_SENSOR_ATTR_TEMP(15, 14),
2554 THERMAL_SENSOR_ATTR_TEMP(16, 15),
2555};
2556
2557#define THERMAL_ATTRS(X) \
2558 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
2559
2560static struct attribute *thermal_temp_input_attr[] = {
2561 THERMAL_ATTRS(8),
2562 THERMAL_ATTRS(9),
2563 THERMAL_ATTRS(10),
2564 THERMAL_ATTRS(11),
2565 THERMAL_ATTRS(12),
2566 THERMAL_ATTRS(13),
2567 THERMAL_ATTRS(14),
2568 THERMAL_ATTRS(15),
2569 THERMAL_ATTRS(0),
2570 THERMAL_ATTRS(1),
2571 THERMAL_ATTRS(2),
2572 THERMAL_ATTRS(3),
2573 THERMAL_ATTRS(4),
2574 THERMAL_ATTRS(5),
2575 THERMAL_ATTRS(6),
2576 THERMAL_ATTRS(7),
2577 NULL
2578};
2579
2580static const struct attribute_group thermal_temp_input16_group = {
2581 .attrs = thermal_temp_input_attr
2582};
2583
2584static const struct attribute_group thermal_temp_input8_group = {
2585 .attrs = &thermal_temp_input_attr[8]
2586};
2587
2588#undef THERMAL_SENSOR_ATTR_TEMP
2589#undef THERMAL_ATTRS
2590
2591/* --------------------------------------------------------------------- */
2592
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002593static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002594{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002595 u8 t, ta1, ta2;
2596 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002597 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03002598 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002599
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002600 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
2601
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002602 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002603
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002604 if (ibm_thinkpad_ec_found && experimental) {
2605 /*
2606 * Direct EC access mode: sensors at registers
2607 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
2608 * non-implemented, thermal sensors return 0x80 when
2609 * not available
2610 */
2611
2612 ta1 = ta2 = 0;
2613 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002614 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002615 ta1 |= t;
2616 } else {
2617 ta1 = 0;
2618 break;
2619 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002620 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002621 ta2 |= t;
2622 } else {
2623 ta1 = 0;
2624 break;
2625 }
2626 }
2627 if (ta1 == 0) {
2628 /* This is sheer paranoia, but we handle it anyway */
2629 if (acpi_tmp7) {
2630 printk(IBM_ERR
2631 "ThinkPad ACPI EC access misbehaving, "
2632 "falling back to ACPI TMPx access mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002633 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002634 } else {
2635 printk(IBM_ERR
2636 "ThinkPad ACPI EC access misbehaving, "
2637 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002638 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002639 }
2640 } else {
2641 thermal_read_mode =
2642 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002643 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002644 }
2645 } else if (acpi_tmp7) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002646 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
2647 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002648 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002649 } else {
2650 /* Standard ACPI TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002651 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002652 }
2653 } else {
2654 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002655 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002656 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002657
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002658 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
2659 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
2660 thermal_read_mode);
2661
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03002662 switch(thermal_read_mode) {
2663 case TPACPI_THERMAL_TPEC_16:
2664 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2665 &thermal_temp_input16_group);
2666 if (res)
2667 return res;
2668 break;
2669 case TPACPI_THERMAL_TPEC_8:
2670 case TPACPI_THERMAL_ACPI_TMP07:
2671 case TPACPI_THERMAL_ACPI_UPDT:
2672 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2673 &thermal_temp_input8_group);
2674 if (res)
2675 return res;
2676 break;
2677 case TPACPI_THERMAL_NONE:
2678 default:
2679 return 1;
2680 }
2681
2682 return 0;
2683}
2684
2685static void thermal_exit(void)
2686{
2687 switch(thermal_read_mode) {
2688 case TPACPI_THERMAL_TPEC_16:
2689 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2690 &thermal_temp_input16_group);
2691 break;
2692 case TPACPI_THERMAL_TPEC_8:
2693 case TPACPI_THERMAL_ACPI_TMP07:
2694 case TPACPI_THERMAL_ACPI_UPDT:
2695 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2696 &thermal_temp_input16_group);
2697 break;
2698 case TPACPI_THERMAL_NONE:
2699 default:
2700 break;
2701 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002702}
2703
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002704/* idx is zero-based */
2705static int thermal_get_sensor(int idx, s32 *value)
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002706{
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002707 int t;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002708 s8 tmp;
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002709 char tmpi[5];
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002710
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002711 t = TP_EC_THERMAL_TMP0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002712
2713 switch (thermal_read_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002714#if TPACPI_MAX_THERMAL_SENSORS >= 16
2715 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002716 if (idx >= 8 && idx <= 15) {
2717 t = TP_EC_THERMAL_TMP8;
2718 idx -= 8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002719 }
2720 /* fallthrough */
2721#endif
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002722 case TPACPI_THERMAL_TPEC_8:
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002723 if (idx <= 7) {
2724 if (!acpi_ec_read(t + idx, &tmp))
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002725 return -EIO;
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002726 *value = tmp * 1000;
2727 return 0;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002728 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002729 break;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002730
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002731 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002732 if (idx <= 7) {
2733 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
2734 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
2735 return -EIO;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002736 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
2737 return -EIO;
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002738 *value = (t - 2732) * 100;
2739 return 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002740 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002741 break;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002742
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002743 case TPACPI_THERMAL_ACPI_TMP07:
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002744 if (idx <= 7) {
2745 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002746 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
2747 return -EIO;
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002748 *value = t * 1000;
2749 return 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002750 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002751 break;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002752
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002753 case TPACPI_THERMAL_NONE:
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002754 default:
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002755 return -ENOSYS;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002756 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03002757
2758 return -EINVAL;
2759}
2760
2761static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
2762{
2763 int res, i;
2764 int n;
2765
2766 n = 8;
2767 i = 0;
2768
2769 if (!s)
2770 return -EINVAL;
2771
2772 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
2773 n = 16;
2774
2775 for(i = 0 ; i < n; i++) {
2776 res = thermal_get_sensor(i, &s->temp[i]);
2777 if (res)
2778 return res;
2779 }
2780
2781 return n;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002782}
2783
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002784static int thermal_read(char *p)
2785{
2786 int len = 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002787 int n, i;
2788 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002789
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002790 n = thermal_get_sensors(&t);
2791 if (unlikely(n < 0))
2792 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002793
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002794 len += sprintf(p + len, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002795
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02002796 if (n > 0) {
2797 for (i = 0; i < (n - 1); i++)
2798 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
2799 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
2800 } else
2801 len += sprintf(p + len, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002802
2803 return len;
2804}
2805
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002806static struct ibm_struct thermal_driver_data = {
2807 .name = "thermal",
2808 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03002809 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002810};
2811
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002812/*************************************************************************
2813 * EC Dump subdriver
2814 */
2815
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002816static u8 ecdump_regs[256];
2817
2818static int ecdump_read(char *p)
2819{
2820 int len = 0;
2821 int i, j;
2822 u8 v;
2823
2824 len += sprintf(p + len, "EC "
2825 " +00 +01 +02 +03 +04 +05 +06 +07"
2826 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
2827 for (i = 0; i < 256; i += 16) {
2828 len += sprintf(p + len, "EC 0x%02x:", i);
2829 for (j = 0; j < 16; j++) {
2830 if (!acpi_ec_read(i + j, &v))
2831 break;
2832 if (v != ecdump_regs[i + j])
2833 len += sprintf(p + len, " *%02x", v);
2834 else
2835 len += sprintf(p + len, " %02x", v);
2836 ecdump_regs[i + j] = v;
2837 }
2838 len += sprintf(p + len, "\n");
2839 if (j != 16)
2840 break;
2841 }
2842
2843 /* These are way too dangerous to advertise openly... */
2844#if 0
2845 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
2846 " (<offset> is 00-ff, <value> is 00-ff)\n");
2847 len += sprintf(p + len, "commands:\t0x<offset> <value> "
2848 " (<offset> is 00-ff, <value> is 0-255)\n");
2849#endif
2850 return len;
2851}
2852
2853static int ecdump_write(char *buf)
2854{
2855 char *cmd;
2856 int i, v;
2857
2858 while ((cmd = next_cmd(&buf))) {
2859 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
2860 /* i and v set */
2861 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
2862 /* i and v set */
2863 } else
2864 return -EINVAL;
2865 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
2866 if (!acpi_ec_write(i, v))
2867 return -EIO;
2868 } else
2869 return -EINVAL;
2870 }
2871
2872 return 0;
2873}
2874
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002875static struct ibm_struct ecdump_driver_data = {
2876 .name = "ecdump",
2877 .read = ecdump_read,
2878 .write = ecdump_write,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03002879 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002880};
2881
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002882/*************************************************************************
2883 * Backlight/brightness subdriver
2884 */
Holger Macht8acb0252006-10-20 14:30:28 -07002885
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03002886static struct backlight_device *ibm_backlight_device;
Holger Macht8acb0252006-10-20 14:30:28 -07002887
Richard Purdie599a52d2007-02-10 23:07:48 +00002888static struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02002889 .get_brightness = brightness_get,
2890 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02002891};
2892
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002893static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02002894{
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02002895 int b;
2896
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002897 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
2898
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02002899 b = brightness_get(NULL);
2900 if (b < 0)
2901 return b;
2902
Henrique de Moraes Holschuh7d5a0152007-04-24 11:48:20 -03002903 ibm_backlight_device = backlight_device_register(
2904 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
2905 &ibm_backlight_data);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02002906 if (IS_ERR(ibm_backlight_device)) {
2907 printk(IBM_ERR "Could not register backlight device\n");
2908 return PTR_ERR(ibm_backlight_device);
2909 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002910 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02002911
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02002912 ibm_backlight_device->props.max_brightness = 7;
2913 ibm_backlight_device->props.brightness = b;
2914 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00002915
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02002916 return 0;
2917}
2918
2919static void brightness_exit(void)
2920{
2921 if (ibm_backlight_device) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002922 vdbg_printk(TPACPI_DBG_EXIT,
2923 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02002924 backlight_device_unregister(ibm_backlight_device);
2925 ibm_backlight_device = NULL;
2926 }
2927}
2928
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002929static int brightness_update_status(struct backlight_device *bd)
2930{
2931 return brightness_set(
2932 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
2933 bd->props.power == FB_BLANK_UNBLANK) ?
2934 bd->props.brightness : 0);
2935}
2936
2937static int brightness_get(struct backlight_device *bd)
2938{
2939 u8 level;
2940 if (!acpi_ec_read(brightness_offset, &level))
2941 return -EIO;
2942
2943 level &= 0x7;
2944
2945 return level;
2946}
2947
2948static int brightness_set(int value)
2949{
2950 int cmos_cmd, inc, i;
2951 int current_value = brightness_get(NULL);
2952
2953 value &= 7;
2954
2955 cmos_cmd = value > current_value ? TP_CMOS_BRIGHTNESS_UP : TP_CMOS_BRIGHTNESS_DOWN;
2956 inc = value > current_value ? 1 : -1;
2957 for (i = current_value; i != value; i += inc) {
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03002958 if (issue_thinkpad_cmos_command(cmos_cmd))
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002959 return -EIO;
2960 if (!acpi_ec_write(brightness_offset, i + inc))
2961 return -EIO;
2962 }
2963
2964 return 0;
2965}
2966
2967static int brightness_read(char *p)
2968{
2969 int len = 0;
2970 int level;
2971
2972 if ((level = brightness_get(NULL)) < 0) {
2973 len += sprintf(p + len, "level:\t\tunreadable\n");
2974 } else {
2975 len += sprintf(p + len, "level:\t\t%d\n", level & 0x7);
2976 len += sprintf(p + len, "commands:\tup, down\n");
2977 len += sprintf(p + len, "commands:\tlevel <level>"
2978 " (<level> is 0-7)\n");
2979 }
2980
2981 return len;
2982}
2983
2984static int brightness_write(char *buf)
2985{
2986 int level;
2987 int new_level;
2988 char *cmd;
2989
2990 while ((cmd = next_cmd(&buf))) {
2991 if ((level = brightness_get(NULL)) < 0)
2992 return level;
2993 level &= 7;
2994
2995 if (strlencmp(cmd, "up") == 0) {
2996 new_level = level == 7 ? 7 : level + 1;
2997 } else if (strlencmp(cmd, "down") == 0) {
2998 new_level = level == 0 ? 0 : level - 1;
2999 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
3000 new_level >= 0 && new_level <= 7) {
3001 /* new_level set */
3002 } else
3003 return -EINVAL;
3004
3005 brightness_set(new_level);
3006 }
3007
3008 return 0;
3009}
3010
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003011static struct ibm_struct brightness_driver_data = {
3012 .name = "brightness",
3013 .read = brightness_read,
3014 .write = brightness_write,
3015 .exit = brightness_exit,
3016};
3017
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003018/*************************************************************************
3019 * Volume subdriver
3020 */
3021
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003022static int volume_read(char *p)
3023{
3024 int len = 0;
3025 u8 level;
3026
3027 if (!acpi_ec_read(volume_offset, &level)) {
3028 len += sprintf(p + len, "level:\t\tunreadable\n");
3029 } else {
3030 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
3031 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
3032 len += sprintf(p + len, "commands:\tup, down, mute\n");
3033 len += sprintf(p + len, "commands:\tlevel <level>"
3034 " (<level> is 0-15)\n");
3035 }
3036
3037 return len;
3038}
3039
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003040static int volume_write(char *buf)
3041{
3042 int cmos_cmd, inc, i;
3043 u8 level, mute;
3044 int new_level, new_mute;
3045 char *cmd;
3046
3047 while ((cmd = next_cmd(&buf))) {
3048 if (!acpi_ec_read(volume_offset, &level))
3049 return -EIO;
3050 new_mute = mute = level & 0x40;
3051 new_level = level = level & 0xf;
3052
3053 if (strlencmp(cmd, "up") == 0) {
3054 if (mute)
3055 new_mute = 0;
3056 else
3057 new_level = level == 15 ? 15 : level + 1;
3058 } else if (strlencmp(cmd, "down") == 0) {
3059 if (mute)
3060 new_mute = 0;
3061 else
3062 new_level = level == 0 ? 0 : level - 1;
3063 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
3064 new_level >= 0 && new_level <= 15) {
3065 /* new_level set */
3066 } else if (strlencmp(cmd, "mute") == 0) {
3067 new_mute = 0x40;
3068 } else
3069 return -EINVAL;
3070
3071 if (new_level != level) { /* mute doesn't change */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003072 cmos_cmd = new_level > level ? TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003073 inc = new_level > level ? 1 : -1;
3074
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03003075 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003076 !acpi_ec_write(volume_offset, level)))
3077 return -EIO;
3078
3079 for (i = level; i != new_level; i += inc)
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03003080 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003081 !acpi_ec_write(volume_offset, i + inc))
3082 return -EIO;
3083
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03003084 if (mute && (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003085 !acpi_ec_write(volume_offset,
3086 new_level + mute)))
3087 return -EIO;
3088 }
3089
3090 if (new_mute != mute) { /* level doesn't change */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003091 cmos_cmd = new_mute ? TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003092
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03003093 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003094 !acpi_ec_write(volume_offset, level + new_mute))
3095 return -EIO;
3096 }
3097 }
3098
3099 return 0;
3100}
3101
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003102static struct ibm_struct volume_driver_data = {
3103 .name = "volume",
3104 .read = volume_read,
3105 .write = volume_write,
3106};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003107
3108/*************************************************************************
3109 * Fan subdriver
3110 */
3111
3112/*
3113 * FAN ACCESS MODES
3114 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003115 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003116 * ACPI GFAN method: returns fan level
3117 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003118 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03003119 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003120 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003121 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003122 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
3123 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03003124 * EC 0x2f (HFSP) might be available *for reading*, but do not use
3125 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003126 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003127 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03003128 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
3129 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003130 *
3131 * Fan speed changes of any sort (including those caused by the
3132 * disengaged mode) are usually done slowly by the firmware as the
3133 * maximum ammount of fan duty cycle change per second seems to be
3134 * limited.
3135 *
3136 * Reading is not available if GFAN exists.
3137 * Writing is not available if SFAN exists.
3138 *
3139 * Bits
3140 * 7 automatic mode engaged;
3141 * (default operation mode of the ThinkPad)
3142 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03003143 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003144 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03003145 * the tachometer while the fan controller ramps up
3146 * the speed (which can take up to a few *minutes*).
3147 * Speeds up fan to 100% duty-cycle, which is far above
3148 * the standard RPM levels. It is not impossible that
3149 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003150 * 5-3 unused in some models. Extra bits for fan level
3151 * in others, but still useless as all values above
3152 * 7 map to the same speed as level 7 in these models.
3153 * 2-0 fan level (0..7 usually)
3154 * 0x00 = stop
3155 * 0x07 = max (set when temperatures critical)
3156 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003157 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003158 *
3159 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
3160 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
3161 * does so, its initial value is meaningless (0x07).
3162 *
3163 * For firmware bugs, refer to:
3164 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
3165 *
3166 * ----
3167 *
3168 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
3169 * Main fan tachometer reading (in RPM)
3170 *
3171 * This register is present on all ThinkPads with a new-style EC, and
3172 * it is known not to be present on the A21m/e, and T22, as there is
3173 * something else in offset 0x84 according to the ACPI DSDT. Other
3174 * ThinkPads from this same time period (and earlier) probably lack the
3175 * tachometer as well.
3176 *
3177 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
3178 * was never fixed by IBM to report the EC firmware version string
3179 * probably support the tachometer (like the early X models), so
3180 * detecting it is quite hard. We need more data to know for sure.
3181 *
3182 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
3183 * might result.
3184 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03003185 * FIRMWARE BUG: may go stale while the EC is switching to full speed
3186 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003187 *
3188 * For firmware bugs, refer to:
3189 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
3190 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003191 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003192 * ThinkPad X31, X40, X41. Not available in the X60.
3193 *
3194 * FANS ACPI handle: takes three arguments: low speed, medium speed,
3195 * high speed. ACPI DSDT seems to map these three speeds to levels
3196 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
3197 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
3198 *
3199 * The speeds are stored on handles
3200 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
3201 *
3202 * There are three default speed sets, acessible as handles:
3203 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
3204 *
3205 * ACPI DSDT switches which set is in use depending on various
3206 * factors.
3207 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003208 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003209 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
3210 * but the ACPI tables just mention level 7.
3211 */
3212
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003213static enum fan_status_access_mode fan_status_access_mode;
3214static enum fan_control_access_mode fan_control_access_mode;
3215static enum fan_control_commands fan_control_commands;
3216
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02003217static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003218static u8 fan_control_desired_level;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02003219
Len Brown25c68a32006-12-08 04:43:41 -05003220static void fan_watchdog_fire(struct work_struct *ignored);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003221static int fan_watchdog_maxinterval;
Len Brown25c68a32006-12-08 04:43:41 -05003222static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003223
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003224IBM_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
3225IBM_HANDLE(gfan, ec, "GFAN", /* 570 */
3226 "\\FSPD", /* 600e/x, 770e, 770x */
3227 ); /* all others */
3228IBM_HANDLE(sfan, ec, "SFAN", /* 570 */
3229 "JFNS", /* 770x-JL */
3230 ); /* all others */
3231
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003232/*
3233 * SYSFS fan layout: hwmon compatible (device)
3234 *
3235 * pwm*_enable:
3236 * 0: "disengaged" mode
3237 * 1: manual mode
3238 * 2: native EC "auto" mode (recommended, hardware default)
3239 *
3240 * pwm*: set speed in manual mode, ignored otherwise.
3241 * 0 is level 0; 255 is level 7. Intermediate points done with linear
3242 * interpolation.
3243 *
3244 * fan*_input: tachometer reading, RPM
3245 *
3246 *
3247 * SYSFS fan layout: extensions
3248 *
3249 * fan_watchdog (driver):
3250 * fan watchdog interval in seconds, 0 disables (default), max 120
3251 */
3252
3253/* sysfs fan pwm1_enable ----------------------------------------------- */
3254static ssize_t fan_pwm1_enable_show(struct device *dev,
3255 struct device_attribute *attr,
3256 char *buf)
3257{
3258 int res, mode;
3259 u8 status;
3260
3261 res = fan_get_status_safe(&status);
3262 if (res)
3263 return res;
3264
3265 if (unlikely(tp_features.fan_ctrl_status_undef)) {
3266 if (status != fan_control_initial_status) {
3267 tp_features.fan_ctrl_status_undef = 0;
3268 } else {
3269 /* Return most likely status. In fact, it
3270 * might be the only possible status */
3271 status = TP_EC_FAN_AUTO;
3272 }
3273 }
3274
3275 if (status & TP_EC_FAN_FULLSPEED) {
3276 mode = 0;
3277 } else if (status & TP_EC_FAN_AUTO) {
3278 mode = 2;
3279 } else
3280 mode = 1;
3281
3282 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
3283}
3284
3285static ssize_t fan_pwm1_enable_store(struct device *dev,
3286 struct device_attribute *attr,
3287 const char *buf, size_t count)
3288{
3289 unsigned long t;
3290 int res, level;
3291
3292 if (parse_strtoul(buf, 2, &t))
3293 return -EINVAL;
3294
3295 switch (t) {
3296 case 0:
3297 level = TP_EC_FAN_FULLSPEED;
3298 break;
3299 case 1:
3300 level = TPACPI_FAN_LAST_LEVEL;
3301 break;
3302 case 2:
3303 level = TP_EC_FAN_AUTO;
3304 break;
3305 case 3:
3306 /* reserved for software-controlled auto mode */
3307 return -ENOSYS;
3308 default:
3309 return -EINVAL;
3310 }
3311
3312 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03003313 if (res == -ENXIO)
3314 return -EINVAL;
3315 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003316 return res;
3317
3318 fan_watchdog_reset();
3319
3320 return count;
3321}
3322
3323static struct device_attribute dev_attr_fan_pwm1_enable =
3324 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
3325 fan_pwm1_enable_show, fan_pwm1_enable_store);
3326
3327/* sysfs fan pwm1 ------------------------------------------------------ */
3328static ssize_t fan_pwm1_show(struct device *dev,
3329 struct device_attribute *attr,
3330 char *buf)
3331{
3332 int res;
3333 u8 status;
3334
3335 res = fan_get_status_safe(&status);
3336 if (res)
3337 return res;
3338
3339 if (unlikely(tp_features.fan_ctrl_status_undef)) {
3340 if (status != fan_control_initial_status) {
3341 tp_features.fan_ctrl_status_undef = 0;
3342 } else {
3343 status = TP_EC_FAN_AUTO;
3344 }
3345 }
3346
3347 if ((status &
3348 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
3349 status = fan_control_desired_level;
3350
3351 if (status > 7)
3352 status = 7;
3353
3354 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
3355}
3356
3357static ssize_t fan_pwm1_store(struct device *dev,
3358 struct device_attribute *attr,
3359 const char *buf, size_t count)
3360{
3361 unsigned long s;
3362 int rc;
3363 u8 status, newlevel;
3364
3365 if (parse_strtoul(buf, 255, &s))
3366 return -EINVAL;
3367
3368 /* scale down from 0-255 to 0-7 */
3369 newlevel = (s >> 5) & 0x07;
3370
3371 rc = mutex_lock_interruptible(&fan_mutex);
3372 if (rc < 0)
3373 return rc;
3374
3375 rc = fan_get_status(&status);
3376 if (!rc && (status &
3377 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
3378 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03003379 if (rc == -ENXIO)
3380 rc = -EINVAL;
3381 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003382 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03003383 fan_watchdog_reset();
3384 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003385 }
3386
3387 mutex_unlock(&fan_mutex);
3388 return (rc)? rc : count;
3389}
3390
3391static struct device_attribute dev_attr_fan_pwm1 =
3392 __ATTR(pwm1, S_IWUSR | S_IRUGO,
3393 fan_pwm1_show, fan_pwm1_store);
3394
3395/* sysfs fan fan1_input ------------------------------------------------ */
3396static ssize_t fan_fan1_input_show(struct device *dev,
3397 struct device_attribute *attr,
3398 char *buf)
3399{
3400 int res;
3401 unsigned int speed;
3402
3403 res = fan_get_speed(&speed);
3404 if (res < 0)
3405 return res;
3406
3407 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
3408}
3409
3410static struct device_attribute dev_attr_fan_fan1_input =
3411 __ATTR(fan1_input, S_IRUGO,
3412 fan_fan1_input_show, NULL);
3413
3414/* sysfs fan fan_watchdog (driver) ------------------------------------- */
3415static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
3416 char *buf)
3417{
3418 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
3419}
3420
3421static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
3422 const char *buf, size_t count)
3423{
3424 unsigned long t;
3425
3426 if (parse_strtoul(buf, 120, &t))
3427 return -EINVAL;
3428
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03003429 if (!fan_control_allowed)
3430 return -EPERM;
3431
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003432 fan_watchdog_maxinterval = t;
3433 fan_watchdog_reset();
3434
3435 return count;
3436}
3437
3438static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
3439 fan_fan_watchdog_show, fan_fan_watchdog_store);
3440
3441/* --------------------------------------------------------------------- */
3442static struct attribute *fan_attributes[] = {
3443 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
3444 &dev_attr_fan_fan1_input.attr,
3445 NULL
3446};
3447
3448static const struct attribute_group fan_attr_group = {
3449 .attrs = fan_attributes,
3450};
3451
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003452static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003453{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003454 int rc;
3455
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003456 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
3457
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003458 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003459 fan_status_access_mode = TPACPI_FAN_NONE;
3460 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003461 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003462 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003463 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003464 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003465
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003466 IBM_ACPIHANDLE_INIT(fans);
3467 IBM_ACPIHANDLE_INIT(gfan);
3468 IBM_ACPIHANDLE_INIT(sfan);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003469
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003470 if (gfan_handle) {
3471 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003472 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003473 } else {
3474 /* all other ThinkPads: note that even old-style
3475 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02003476 if (likely(acpi_ec_read(fan_status_offset,
3477 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003478 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02003479
3480 /* In some ThinkPads, neither the EC nor the ACPI
3481 * DSDT initialize the fan status, and it ends up
3482 * being set to 0x07 when it *could* be either
3483 * 0x07 or 0x80.
3484 *
3485 * Enable for TP-1Y (T43), TP-78 (R51e),
3486 * TP-76 (R52), TP-70 (T43, R52), which are known
3487 * to be buggy. */
3488 if (fan_control_initial_status == 0x07 &&
3489 ibm_thinkpad_ec_found &&
3490 ((ibm_thinkpad_ec_found[0] == '1' &&
3491 ibm_thinkpad_ec_found[1] == 'Y') ||
3492 (ibm_thinkpad_ec_found[0] == '7' &&
3493 (ibm_thinkpad_ec_found[1] == '6' ||
3494 ibm_thinkpad_ec_found[1] == '8' ||
3495 ibm_thinkpad_ec_found[1] == '0'))
3496 )) {
3497 printk(IBM_NOTICE
3498 "fan_init: initial fan status is "
3499 "unknown, assuming it is in auto "
3500 "mode\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003501 tp_features.fan_ctrl_status_undef = 1;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02003502 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003503 } else {
3504 printk(IBM_ERR
3505 "ThinkPad ACPI EC access misbehaving, "
3506 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003507 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003508 }
3509 }
3510
3511 if (sfan_handle) {
3512 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003513 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02003514 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003515 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003516 } else {
3517 if (!gfan_handle) {
3518 /* gfan without sfan means no fan control */
3519 /* all other models implement TP EC 0x2f control */
3520
3521 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02003522 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003523 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003524 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003525 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003526 TPACPI_FAN_CMD_SPEED |
3527 TPACPI_FAN_CMD_LEVEL |
3528 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003529 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003530 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02003531 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003532 TPACPI_FAN_CMD_LEVEL |
3533 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003534 }
3535 }
3536 }
3537
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003538 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
3539 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
3540 fan_control_access_mode != TPACPI_FAN_WR_NONE),
3541 fan_status_access_mode, fan_control_access_mode);
3542
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03003543 /* fan control master switch */
3544 if (!fan_control_allowed) {
3545 fan_control_access_mode = TPACPI_FAN_WR_NONE;
3546 fan_control_commands = 0;
3547 dbg_printk(TPACPI_DBG_INIT,
3548 "fan control features disabled by parameter\n");
3549 }
3550
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003551 /* update fan_control_desired_level */
3552 if (fan_status_access_mode != TPACPI_FAN_NONE)
3553 fan_get_status_safe(NULL);
3554
3555 if (fan_status_access_mode != TPACPI_FAN_NONE ||
3556 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
3557 rc = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3558 &fan_attr_group);
3559 if (!(rc < 0))
3560 rc = driver_create_file(&tpacpi_pdriver.driver,
3561 &driver_attr_fan_watchdog);
3562 if (rc < 0)
3563 return rc;
3564 return 0;
3565 } else
3566 return 1;
3567}
3568
3569/*
3570 * Call with fan_mutex held
3571 */
3572static void fan_update_desired_level(u8 status)
3573{
3574 if ((status &
3575 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
3576 if (status > 7)
3577 fan_control_desired_level = 7;
3578 else
3579 fan_control_desired_level = status;
3580 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02003581}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003582
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02003583static int fan_get_status(u8 *status)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003584{
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02003585 u8 s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003586
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02003587 /* TODO:
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003588 * Add TPACPI_FAN_RD_ACPI_FANS ? */
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02003589
Henrique de Moraes Holschuh3ef8a602006-11-24 11:47:10 -02003590 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003591 case TPACPI_FAN_RD_ACPI_GFAN:
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003592 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02003593
3594 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003595 return -EIO;
3596
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02003597 if (likely(status))
3598 *status = s & 0x07;
3599
3600 break;
3601
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003602 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02003603 /* all except 570, 600e/x, 770e, 770x */
3604 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
3605 return -EIO;
3606
3607 if (likely(status))
3608 *status = s;
3609
3610 break;
3611
3612 default:
3613 return -ENXIO;
3614 }
3615
3616 return 0;
3617}
3618
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003619static int fan_get_status_safe(u8 *status)
3620{
3621 int rc;
3622 u8 s;
3623
3624 rc = mutex_lock_interruptible(&fan_mutex);
3625 if (rc < 0)
3626 return rc;
3627 rc = fan_get_status(&s);
3628 if (!rc)
3629 fan_update_desired_level(s);
3630 mutex_unlock(&fan_mutex);
3631
3632 if (status)
3633 *status = s;
3634
3635 return rc;
3636}
3637
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003638static void fan_exit(void)
3639{
Henrique de Moraes Holschuh99fba3f2007-04-21 11:08:44 -03003640 vdbg_printk(TPACPI_DBG_EXIT, "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003641
3642 /* FIXME: can we really do this unconditionally? */
3643 sysfs_remove_group(&tpacpi_pdev->dev.kobj, &fan_attr_group);
3644 driver_remove_file(&tpacpi_pdriver.driver, &driver_attr_fan_watchdog);
3645
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003646 cancel_delayed_work(&fan_watchdog_task);
3647 flush_scheduled_work();
3648}
3649
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02003650static int fan_get_speed(unsigned int *speed)
3651{
3652 u8 hi, lo;
3653
3654 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003655 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02003656 /* all except 570, 600e/x, 770e, 770x */
3657 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
3658 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
3659 return -EIO;
3660
3661 if (likely(speed))
3662 *speed = (hi << 8) | lo;
3663
3664 break;
3665
3666 default:
3667 return -ENXIO;
3668 }
3669
3670 return 0;
3671}
3672
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003673static void fan_watchdog_fire(struct work_struct *ignored)
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003674{
Henrique de Moraes Holschuh99fba3f2007-04-21 11:08:44 -03003675 int rc;
3676
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003677 printk(IBM_NOTICE "fan watchdog: enabling fan\n");
Henrique de Moraes Holschuh99fba3f2007-04-21 11:08:44 -03003678 rc = fan_set_enable();
3679 if (rc < 0) {
3680 printk(IBM_ERR "fan watchdog: error %d while enabling fan, "
3681 "will try again later...\n", -rc);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003682 /* reschedule for later */
3683 fan_watchdog_reset();
3684 }
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003685}
3686
3687static void fan_watchdog_reset(void)
3688{
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03003689 static int fan_watchdog_active;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003690
Henrique de Moraes Holschuh4985cd02007-04-27 22:00:10 -03003691 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
3692 return;
3693
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02003694 if (fan_watchdog_active)
3695 cancel_delayed_work(&fan_watchdog_task);
3696
3697 if (fan_watchdog_maxinterval > 0) {
3698 fan_watchdog_active = 1;
3699 if (!schedule_delayed_work(&fan_watchdog_task,
3700 msecs_to_jiffies(fan_watchdog_maxinterval
3701 * 1000))) {
3702 printk(IBM_ERR "failed to schedule the fan watchdog, "
3703 "watchdog will not trigger\n");
3704 }
3705 } else
3706 fan_watchdog_active = 0;
3707}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003708
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003709static int fan_set_level(int level)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003710{
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03003711 if (!fan_control_allowed)
3712 return -EPERM;
3713
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003714 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003715 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003716 if (level >= 0 && level <= 7) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003717 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003718 return -EIO;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003719 } else
3720 return -EINVAL;
3721 break;
3722
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003723 case TPACPI_FAN_WR_ACPI_FANS:
3724 case TPACPI_FAN_WR_TPEC:
3725 if ((level != TP_EC_FAN_AUTO) &&
3726 (level != TP_EC_FAN_FULLSPEED) &&
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02003727 ((level < 0) || (level > 7)))
3728 return -EINVAL;
3729
Henrique de Moraes Holschuheaa75712007-04-24 11:48:18 -03003730 /* safety net should the EC not support AUTO
3731 * or FULLSPEED mode bits and just ignore them */
3732 if (level & TP_EC_FAN_FULLSPEED)
3733 level |= 7; /* safety min speed 7 */
3734 else if (level & TP_EC_FAN_FULLSPEED)
3735 level |= 4; /* safety min speed 4 */
3736
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003737 if (!acpi_ec_write(fan_status_offset, level))
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02003738 return -EIO;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02003739 else
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003740 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02003741 break;
3742
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003743 default:
3744 return -ENXIO;
3745 }
3746 return 0;
3747}
3748
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003749static int fan_set_level_safe(int level)
3750{
3751 int rc;
3752
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03003753 if (!fan_control_allowed)
3754 return -EPERM;
3755
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003756 rc = mutex_lock_interruptible(&fan_mutex);
3757 if (rc < 0)
3758 return rc;
3759
3760 if (level == TPACPI_FAN_LAST_LEVEL)
3761 level = fan_control_desired_level;
3762
3763 rc = fan_set_level(level);
3764 if (!rc)
3765 fan_update_desired_level(level);
3766
3767 mutex_unlock(&fan_mutex);
3768 return rc;
3769}
3770
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003771static int fan_set_enable(void)
3772{
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02003773 u8 s;
3774 int rc;
3775
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03003776 if (!fan_control_allowed)
3777 return -EPERM;
3778
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003779 rc = mutex_lock_interruptible(&fan_mutex);
3780 if (rc < 0)
3781 return rc;
3782
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003783 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003784 case TPACPI_FAN_WR_ACPI_FANS:
3785 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003786 rc = fan_get_status(&s);
3787 if (rc < 0)
3788 break;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02003789
3790 /* Don't go out of emergency fan mode */
Henrique de Moraes Holschuheaa75712007-04-24 11:48:18 -03003791 if (s != 7) {
3792 s &= 0x07;
3793 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
3794 }
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02003795
3796 if (!acpi_ec_write(fan_status_offset, s))
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003797 rc = -EIO;
3798 else {
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003799 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003800 rc = 0;
3801 }
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02003802 break;
3803
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003804 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003805 rc = fan_get_status(&s);
3806 if (rc < 0)
3807 break;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02003808
3809 s &= 0x07;
3810
3811 /* Set fan to at least level 4 */
Henrique de Moraes Holschuheaa75712007-04-24 11:48:18 -03003812 s |= 4;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02003813
3814 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003815 rc= -EIO;
3816 else
3817 rc = 0;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003818 break;
3819
3820 default:
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003821 rc = -ENXIO;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003822 }
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003823
3824 mutex_unlock(&fan_mutex);
3825 return rc;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003826}
3827
3828static int fan_set_disable(void)
3829{
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003830 int rc;
3831
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03003832 if (!fan_control_allowed)
3833 return -EPERM;
3834
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003835 rc = mutex_lock_interruptible(&fan_mutex);
3836 if (rc < 0)
3837 return rc;
3838
3839 rc = 0;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003840 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003841 case TPACPI_FAN_WR_ACPI_FANS:
3842 case TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003843 if (!acpi_ec_write(fan_status_offset, 0x00))
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003844 rc = -EIO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003845 else {
3846 fan_control_desired_level = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003847 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003848 }
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003849 break;
3850
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003851 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02003852 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003853 rc = -EIO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003854 else
3855 fan_control_desired_level = 0;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02003856 break;
3857
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003858 default:
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003859 rc = -ENXIO;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003860 }
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003861
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003862
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003863 mutex_unlock(&fan_mutex);
3864 return rc;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003865}
3866
3867static int fan_set_speed(int speed)
3868{
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003869 int rc;
3870
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03003871 if (!fan_control_allowed)
3872 return -EPERM;
3873
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003874 rc = mutex_lock_interruptible(&fan_mutex);
3875 if (rc < 0)
3876 return rc;
3877
3878 rc = 0;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003879 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003880 case TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003881 if (speed >= 0 && speed <= 65535) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003882 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
3883 speed, speed, speed))
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003884 rc = -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003885 } else
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003886 rc = -EINVAL;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003887 break;
3888
3889 default:
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003890 rc = -ENXIO;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003891 }
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03003892
3893 mutex_unlock(&fan_mutex);
3894 return rc;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003895}
3896
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003897static int fan_read(char *p)
3898{
3899 int len = 0;
3900 int rc;
3901 u8 status;
3902 unsigned int speed = 0;
3903
3904 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003905 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003906 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003907 if ((rc = fan_get_status_safe(&status)) < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003908 return rc;
3909
3910 len += sprintf(p + len, "status:\t\t%s\n"
3911 "level:\t\t%d\n",
3912 (status != 0) ? "enabled" : "disabled", status);
3913 break;
3914
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003915 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003916 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003917 if ((rc = fan_get_status_safe(&status)) < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003918 return rc;
3919
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003920 if (unlikely(tp_features.fan_ctrl_status_undef)) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003921 if (status != fan_control_initial_status)
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003922 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003923 else
3924 /* Return most likely status. In fact, it
3925 * might be the only possible status */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003926 status = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003927 }
3928
3929 len += sprintf(p + len, "status:\t\t%s\n",
3930 (status != 0) ? "enabled" : "disabled");
3931
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003932 if ((rc = fan_get_speed(&speed)) < 0)
3933 return rc;
3934
3935 len += sprintf(p + len, "speed:\t\t%d\n", speed);
3936
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003937 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003938 /* Disengaged mode takes precedence */
3939 len += sprintf(p + len, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003940 else if (status & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003941 len += sprintf(p + len, "level:\t\tauto\n");
3942 else
3943 len += sprintf(p + len, "level:\t\t%d\n", status);
3944 break;
3945
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003946 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003947 default:
3948 len += sprintf(p + len, "status:\t\tnot supported\n");
3949 }
3950
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003951 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003952 len += sprintf(p + len, "commands:\tlevel <level>");
3953
3954 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003955 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003956 len += sprintf(p + len, " (<level> is 0-7)\n");
3957 break;
3958
3959 default:
3960 len += sprintf(p + len, " (<level> is 0-7, "
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003961 "auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003962 break;
3963 }
3964 }
3965
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003966 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003967 len += sprintf(p + len, "commands:\tenable, disable\n"
3968 "commands:\twatchdog <timeout> (<timeout> is 0 (off), "
3969 "1-120 (seconds))\n");
3970
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003971 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003972 len += sprintf(p + len, "commands:\tspeed <speed>"
3973 " (<speed> is 0-65535)\n");
3974
3975 return len;
3976}
3977
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003978static int fan_write_cmd_level(const char *cmd, int *rc)
3979{
3980 int level;
3981
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02003982 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003983 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003984 else if ((strlencmp(cmd, "level disengaged") == 0) |
3985 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003986 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02003987 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003988 return 0;
3989
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03003990 if ((*rc = fan_set_level_safe(level)) == -ENXIO)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02003991 printk(IBM_ERR "level command accepted for unsupported "
3992 "access mode %d", fan_control_access_mode);
3993
3994 return 1;
3995}
3996
3997static int fan_write_cmd_enable(const char *cmd, int *rc)
3998{
3999 if (strlencmp(cmd, "enable") != 0)
4000 return 0;
4001
4002 if ((*rc = fan_set_enable()) == -ENXIO)
4003 printk(IBM_ERR "enable command accepted for unsupported "
4004 "access mode %d", fan_control_access_mode);
4005
4006 return 1;
4007}
4008
4009static int fan_write_cmd_disable(const char *cmd, int *rc)
4010{
4011 if (strlencmp(cmd, "disable") != 0)
4012 return 0;
4013
4014 if ((*rc = fan_set_disable()) == -ENXIO)
4015 printk(IBM_ERR "disable command accepted for unsupported "
4016 "access mode %d", fan_control_access_mode);
4017
4018 return 1;
4019}
4020
4021static int fan_write_cmd_speed(const char *cmd, int *rc)
4022{
4023 int speed;
4024
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02004025 /* TODO:
4026 * Support speed <low> <medium> <high> ? */
4027
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004028 if (sscanf(cmd, "speed %d", &speed) != 1)
4029 return 0;
4030
4031 if ((*rc = fan_set_speed(speed)) == -ENXIO)
4032 printk(IBM_ERR "speed command accepted for unsupported "
4033 "access mode %d", fan_control_access_mode);
4034
4035 return 1;
4036}
4037
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02004038static int fan_write_cmd_watchdog(const char *cmd, int *rc)
4039{
4040 int interval;
4041
4042 if (sscanf(cmd, "watchdog %d", &interval) != 1)
4043 return 0;
4044
4045 if (interval < 0 || interval > 120)
4046 *rc = -EINVAL;
4047 else
4048 fan_watchdog_maxinterval = interval;
4049
4050 return 1;
4051}
4052
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004053static int fan_write(char *buf)
4054{
4055 char *cmd;
4056 int rc = 0;
4057
4058 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004059 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004060 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004061 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004062 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02004063 fan_write_cmd_disable(cmd, &rc) ||
4064 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004065 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004066 fan_write_cmd_speed(cmd, &rc))
4067 )
4068 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02004069 else if (!rc)
4070 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004071 }
4072
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02004073 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004074}
4075
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004076static struct ibm_struct fan_driver_data = {
4077 .name = "fan",
4078 .read = fan_read,
4079 .write = fan_write,
4080 .exit = fan_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004081};
4082
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004083/****************************************************************************
4084 ****************************************************************************
4085 *
4086 * Infrastructure
4087 *
4088 ****************************************************************************
4089 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004091/* /proc support */
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03004092static struct proc_dir_entry *proc_dir;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004093
4094/* Subdriver registry */
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004095static LIST_HEAD(tpacpi_all_drivers);
4096
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004098/*
4099 * Module and infrastructure proble, init and exit handling
4100 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004102#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004103static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004104{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004105 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004106
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004107 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004108}
4109#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
4110
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004111static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112{
4113 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004114 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115 struct proc_dir_entry *entry;
4116
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004117 BUG_ON(ibm == NULL);
4118
4119 INIT_LIST_HEAD(&ibm->all_drivers);
4120
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004121 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122 return 0;
4123
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004124 dbg_printk(TPACPI_DBG_INIT,
4125 "probing for %s\n", ibm->name);
4126
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004127 if (iibm->init) {
4128 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004129 if (ret > 0)
4130 return 0; /* probe failed */
4131 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004133
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004134 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135 }
4136
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004137 if (ibm->acpi) {
4138 if (ibm->acpi->hid) {
4139 ret = register_tpacpi_subdriver(ibm);
4140 if (ret)
4141 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004142 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004143
4144 if (ibm->acpi->notify) {
4145 ret = setup_acpi_notify(ibm);
4146 if (ret == -ENODEV) {
4147 printk(IBM_NOTICE "disabling subdriver %s\n",
4148 ibm->name);
4149 ret = 0;
4150 goto err_out;
4151 }
4152 if (ret < 0)
4153 goto err_out;
4154 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004155 }
4156
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004157 dbg_printk(TPACPI_DBG_INIT,
4158 "%s installed\n", ibm->name);
4159
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004160 if (ibm->read) {
4161 entry = create_proc_entry(ibm->name,
4162 S_IFREG | S_IRUGO | S_IWUSR,
4163 proc_dir);
4164 if (!entry) {
4165 printk(IBM_ERR "unable to create proc entry %s\n",
4166 ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004167 ret = -ENODEV;
4168 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004169 }
4170 entry->owner = THIS_MODULE;
4171 entry->data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004172 entry->read_proc = &dispatch_procfs_read;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004173 if (ibm->write)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004174 entry->write_proc = &dispatch_procfs_write;
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004175 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004176 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004178 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
4179
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004181
4182err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004183 dbg_printk(TPACPI_DBG_INIT,
4184 "%s: at error exit path with result %d\n",
4185 ibm->name, ret);
4186
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004187 ibm_exit(ibm);
4188 return (ret < 0)? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189}
4190
4191static void ibm_exit(struct ibm_struct *ibm)
4192{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004193 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004194
4195 list_del_init(&ibm->all_drivers);
4196
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004197 if (ibm->flags.acpi_notify_installed) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004198 dbg_printk(TPACPI_DBG_EXIT,
4199 "%s: acpi_remove_notify_handler\n", ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004200 BUG_ON(!ibm->acpi);
4201 acpi_remove_notify_handler(*ibm->acpi->handle,
4202 ibm->acpi->type,
4203 dispatch_acpi_notify);
4204 ibm->flags.acpi_notify_installed = 0;
4205 ibm->flags.acpi_notify_installed = 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004206 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004208 if (ibm->flags.proc_created) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004209 dbg_printk(TPACPI_DBG_EXIT,
4210 "%s: remove_proc_entry\n", ibm->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211 remove_proc_entry(ibm->name, proc_dir);
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004212 ibm->flags.proc_created = 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004213 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004215 if (ibm->flags.acpi_driver_registered) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004216 dbg_printk(TPACPI_DBG_EXIT,
4217 "%s: acpi_bus_unregister_driver\n", ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004218 BUG_ON(!ibm->acpi);
4219 acpi_bus_unregister_driver(ibm->acpi->driver);
4220 kfree(ibm->acpi->driver);
4221 ibm->acpi->driver = NULL;
4222 ibm->flags.acpi_driver_registered = 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004223 }
4224
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004225 if (ibm->flags.init_called && ibm->exit) {
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004226 ibm->exit();
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004227 ibm->flags.init_called = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004229
4230 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231}
4232
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004233/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03004235static char *ibm_thinkpad_ec_found;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02004237static char* __init check_dmi_for_ec(void)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004238{
4239 struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02004240 char ec_fw_string[18];
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004241
4242 /*
4243 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
4244 * X32 or newer, all Z series; Some models must have an
4245 * up-to-date BIOS or they will not be detected.
4246 *
4247 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
4248 */
4249 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02004250 if (sscanf(dev->name,
4251 "IBM ThinkPad Embedded Controller -[%17c",
4252 ec_fw_string) == 1) {
4253 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
4254 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
4255 return kstrdup(ec_fw_string, GFP_KERNEL);
4256 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004257 }
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02004258 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259}
4260
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004261static int __init probe_for_thinkpad(void)
4262{
4263 int is_thinkpad;
4264
4265 if (acpi_disabled)
4266 return -ENODEV;
4267
4268 /*
4269 * Non-ancient models have better DMI tagging, but very old models
4270 * don't.
4271 */
4272 is_thinkpad = dmi_name_in_vendors("ThinkPad");
4273
4274 /* ec is required because many other handles are relative to it */
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004275 IBM_ACPIHANDLE_INIT(ec);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004276 if (!ec_handle) {
4277 if (is_thinkpad)
4278 printk(IBM_ERR
4279 "Not yet supported ThinkPad detected!\n");
4280 return -ENODEV;
4281 }
4282
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03004283 /*
4284 * Risks a regression on very old machines, but reduces potential
4285 * false positives a damn great deal
4286 */
4287 if (!is_thinkpad)
4288 is_thinkpad = dmi_name_in_vendors("IBM");
4289
4290 if (!is_thinkpad && !force_load)
4291 return -ENODEV;
4292
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004293 return 0;
4294}
4295
4296
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004297/* Module init, exit, parameters */
4298
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004299static struct ibm_init_struct ibms_init[] __initdata = {
4300 {
4301 .init = thinkpad_acpi_driver_init,
4302 .data = &thinkpad_acpi_driver_data,
4303 },
4304 {
4305 .init = hotkey_init,
4306 .data = &hotkey_driver_data,
4307 },
4308 {
4309 .init = bluetooth_init,
4310 .data = &bluetooth_driver_data,
4311 },
4312 {
4313 .init = wan_init,
4314 .data = &wan_driver_data,
4315 },
4316 {
4317 .init = video_init,
4318 .data = &video_driver_data,
4319 },
4320 {
4321 .init = light_init,
4322 .data = &light_driver_data,
4323 },
4324#ifdef CONFIG_THINKPAD_ACPI_DOCK
4325 {
4326 .init = dock_init,
4327 .data = &dock_driver_data[0],
4328 },
4329 {
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03004330 .init = dock_init2,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004331 .data = &dock_driver_data[1],
4332 },
4333#endif
4334#ifdef CONFIG_THINKPAD_ACPI_BAY
4335 {
4336 .init = bay_init,
4337 .data = &bay_driver_data,
4338 },
4339#endif
4340 {
4341 .init = cmos_init,
4342 .data = &cmos_driver_data,
4343 },
4344 {
4345 .init = led_init,
4346 .data = &led_driver_data,
4347 },
4348 {
4349 .init = beep_init,
4350 .data = &beep_driver_data,
4351 },
4352 {
4353 .init = thermal_init,
4354 .data = &thermal_driver_data,
4355 },
4356 {
4357 .data = &ecdump_driver_data,
4358 },
4359 {
4360 .init = brightness_init,
4361 .data = &brightness_driver_data,
4362 },
4363 {
4364 .data = &volume_driver_data,
4365 },
4366 {
4367 .init = fan_init,
4368 .data = &fan_driver_data,
4369 },
4370};
4371
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004372static int __init set_ibm_param(const char *val, struct kernel_param *kp)
4373{
4374 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004375 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004376
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004377 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
4378 ibm = ibms_init[i].data;
4379 BUG_ON(ibm == NULL);
4380
4381 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
4382 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004383 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004384 strcpy(ibms_init[i].param, val);
4385 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004386 return 0;
4387 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004388 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004389
4390 return -EINVAL;
4391}
4392
4393static int experimental;
4394module_param(experimental, int, 0);
4395
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03004396static u32 dbg_level;
4397module_param_named(debug, dbg_level, uint, 0);
4398
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03004399static int force_load;
4400module_param(force_load, int, 0);
4401
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03004402static int fan_control_allowed;
4403module_param_named(fan_control, fan_control_allowed, int, 0);
4404
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004405#define IBM_PARAM(feature) \
4406 module_param_call(feature, set_ibm_param, NULL, NULL, 0)
4407
4408IBM_PARAM(hotkey);
4409IBM_PARAM(bluetooth);
4410IBM_PARAM(video);
4411IBM_PARAM(light);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03004412#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004413IBM_PARAM(dock);
4414#endif
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03004415#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004416IBM_PARAM(bay);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03004417#endif /* CONFIG_THINKPAD_ACPI_BAY */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004418IBM_PARAM(cmos);
4419IBM_PARAM(led);
4420IBM_PARAM(beep);
4421IBM_PARAM(ecdump);
4422IBM_PARAM(brightness);
4423IBM_PARAM(volume);
4424IBM_PARAM(fan);
4425
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03004426static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427{
4428 int ret, i;
4429
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03004430 /* Driver-level probe */
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004431 ret = probe_for_thinkpad();
4432 if (ret)
4433 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03004435 /* Driver initialization */
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004436 ibm_thinkpad_ec_found = check_dmi_for_ec();
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004437 IBM_ACPIHANDLE_INIT(ecrd);
4438 IBM_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004440 proc_dir = proc_mkdir(IBM_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441 if (!proc_dir) {
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004442 printk(IBM_ERR "unable to create proc dir " IBM_PROC_DIR);
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03004443 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444 return -ENODEV;
4445 }
4446 proc_dir->owner = THIS_MODULE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004447
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03004448 ret = platform_driver_register(&tpacpi_pdriver);
4449 if (ret) {
4450 printk(IBM_ERR "unable to register platform driver\n");
4451 thinkpad_acpi_module_exit();
4452 return ret;
4453 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03004454 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
4455 if (ret) {
4456 printk(IBM_ERR "unable to create sysfs driver attributes\n");
4457 thinkpad_acpi_module_exit();
4458 return ret;
4459 }
4460
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03004461
4462 /* Device initialization */
4463 tpacpi_pdev = platform_device_register_simple(IBM_DRVR_NAME, -1,
4464 NULL, 0);
4465 if (IS_ERR(tpacpi_pdev)) {
4466 ret = PTR_ERR(tpacpi_pdev);
4467 tpacpi_pdev = NULL;
4468 printk(IBM_ERR "unable to register platform device\n");
4469 thinkpad_acpi_module_exit();
4470 return ret;
4471 }
4472 tpacpi_hwmon = hwmon_device_register(&tpacpi_pdev->dev);
4473 if (IS_ERR(tpacpi_hwmon)) {
4474 ret = PTR_ERR(tpacpi_hwmon);
4475 tpacpi_hwmon = NULL;
4476 printk(IBM_ERR "unable to register hwmon device\n");
4477 thinkpad_acpi_module_exit();
4478 return ret;
4479 }
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03004480 tpacpi_inputdev = input_allocate_device();
4481 if (!tpacpi_inputdev) {
4482 printk(IBM_ERR "unable to allocate input device\n");
4483 thinkpad_acpi_module_exit();
4484 return -ENOMEM;
4485 } else {
4486 /* Prepare input device, but don't register */
4487 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
4488 tpacpi_inputdev->phys = IBM_DRVR_NAME "/input0";
4489 tpacpi_inputdev->id.bustype = BUS_HOST;
4490 tpacpi_inputdev->id.vendor = TPACPI_HKEY_INPUT_VENDOR;
4491 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
4492 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
4493 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004494 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
4495 ret = ibm_init(&ibms_init[i]);
4496 if (ret >= 0 && *ibms_init[i].param)
4497 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03004499 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500 return ret;
4501 }
4502 }
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03004503 ret = input_register_device(tpacpi_inputdev);
4504 if (ret < 0) {
4505 printk(IBM_ERR "unable to register input device\n");
4506 thinkpad_acpi_module_exit();
4507 return ret;
4508 } else {
4509 tp_features.input_device_registered = 1;
4510 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511
4512 return 0;
4513}
4514
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03004515static void thinkpad_acpi_module_exit(void)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004516{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004517 struct ibm_struct *ibm, *itmp;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004518
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004519 list_for_each_entry_safe_reverse(ibm, itmp,
4520 &tpacpi_all_drivers,
4521 all_drivers) {
4522 ibm_exit(ibm);
4523 }
4524
4525 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004526
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03004527 if (tpacpi_inputdev) {
4528 if (tp_features.input_device_registered)
4529 input_unregister_device(tpacpi_inputdev);
4530 else
4531 input_free_device(tpacpi_inputdev);
4532 }
4533
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03004534 if (tpacpi_hwmon)
4535 hwmon_device_unregister(tpacpi_hwmon);
4536
4537 if (tpacpi_pdev)
4538 platform_device_unregister(tpacpi_pdev);
4539
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03004540 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03004541 platform_driver_unregister(&tpacpi_pdriver);
4542
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004543 if (proc_dir)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004544 remove_proc_entry(IBM_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004545
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03004546 kfree(ibm_thinkpad_ec_found);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004547}
4548
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03004549module_init(thinkpad_acpi_module_init);
4550module_exit(thinkpad_acpi_module_exit);