blob: 36e2667d8652fe9e73e2d5edc6af7cbf8d21528e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * ibm_acpi.c - IBM ThinkPad ACPI Extras
3 *
4 *
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Borislav Deianov78f81cc2005-08-17 00:00:00 -040020 */
21
22#define IBM_VERSION "0.12a"
23
24/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 * Changelog:
Borislav Deianov78f81cc2005-08-17 00:00:00 -040026 *
27 * 2005-08-17 0.12 fix compilation on 2.6.13-rc kernels
28 * 2005-03-17 0.11 support for 600e, 770x
29 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
30 * support for 770e, G41
31 * G40 and G41 don't have a thinklight
32 * temperatures no longer experimental
33 * experimental brightness control
34 * experimental volume control
35 * experimental fan enable/disable
36 * 2005-01-16 0.10 fix module loading on R30, R31
37 * 2005-01-16 0.9 support for 570, R30, R31
38 * ultrabay support on A22p, A3x
39 * limit arg for cmos, led, beep, drop experimental status
40 * more capable led control on A21e, A22p, T20-22, X20
41 * experimental temperatures and fan speed
42 * experimental embedded controller register dump
43 * mark more functions as __init, drop incorrect __exit
44 * use MODULE_VERSION
45 * thanks to Henrik Brix Andersen <brix@gentoo.org>
46 * fix parameter passing on module loading
47 * thanks to Rusty Russell <rusty@rustcorp.com.au>
48 * thanks to Jim Radford <radford@blackbean.org>
49 * 2004-11-08 0.8 fix init error case, don't return from a macro
50 * thanks to Chris Wright <chrisw@osdl.org>
51 * 2004-10-23 0.7 fix module loading on A21e, A22p, T20, T21, X20
52 * fix led control on A21e
53 * 2004-10-19 0.6 use acpi_bus_register_driver() to claim HKEY device
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 * 2004-10-18 0.5 thinklight support on A21e, G40, R32, T20, T21, X20
55 * proc file format changed
56 * video_switch command
57 * experimental cmos control
58 * experimental led control
59 * experimental acpi sounds
Borislav Deianov78f81cc2005-08-17 00:00:00 -040060 * 2004-09-16 0.4 support for module parameters
61 * hotkey mask can be prefixed by 0x
62 * video output switching
63 * video expansion control
64 * ultrabay eject support
65 * removed lcd brightness/on/off control, didn't work
66 * 2004-08-17 0.3 support for R40
67 * lcd off, brightness control
68 * thinklight on/off
69 * 2004-08-14 0.2 support for T series, X20
70 * bluetooth enable/disable
71 * hotkey events disabled by default
72 * removed fan control, currently useless
73 * 2004-08-09 0.1 initial release, support for X series
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 */
75
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#include <linux/kernel.h>
77#include <linux/module.h>
78#include <linux/init.h>
79#include <linux/types.h>
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -020080#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#include <linux/proc_fs.h>
Holger Macht8acb0252006-10-20 14:30:28 -070082#include <linux/backlight.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070083#include <asm/uaccess.h>
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -020084#include <linux/dmi.h>
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -020085#include <linux/jiffies.h>
86#include <linux/workqueue.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88#include <acpi/acpi_drivers.h>
89#include <acpi/acnamesp.h>
90
91#define IBM_NAME "ibm"
92#define IBM_DESC "IBM ThinkPad ACPI Extras"
93#define IBM_FILE "ibm_acpi"
94#define IBM_URL "http://ibm-acpi.sf.net/"
95
Borislav Deianov78f81cc2005-08-17 00:00:00 -040096MODULE_AUTHOR("Borislav Deianov");
97MODULE_DESCRIPTION(IBM_DESC);
98MODULE_VERSION(IBM_VERSION);
99MODULE_LICENSE("GPL");
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101#define IBM_DIR IBM_NAME
102
103#define IBM_LOG IBM_FILE ": "
104#define IBM_ERR KERN_ERR IBM_LOG
105#define IBM_NOTICE KERN_NOTICE IBM_LOG
106#define IBM_INFO KERN_INFO IBM_LOG
107#define IBM_DEBUG KERN_DEBUG IBM_LOG
108
109#define IBM_MAX_ACPI_ARGS 3
110
111#define __unused __attribute__ ((unused))
112
113static int experimental;
114module_param(experimental, int, 0);
115
116static acpi_handle root_handle = NULL;
117
118#define IBM_HANDLE(object, parent, paths...) \
119 static acpi_handle object##_handle; \
120 static acpi_handle *object##_parent = &parent##_handle; \
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400121 static char *object##_path; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 static char *object##_paths[] = { paths }
123
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400124/*
125 * The following models are supported to various degrees:
126 *
127 * 570, 600e, 600x, 770e, 770x
128 * A20m, A21e, A21m, A21p, A22p, A30, A30p, A31, A31p
129 * G40, G41
130 * R30, R31, R32, R40, R40e, R50, R50e, R50p, R51
131 * T20, T21, T22, T23, T30, T40, T40p, T41, T41p, T42, T42p, T43
132 * X20, X21, X22, X23, X24, X30, X31, X40
133 *
134 * The following models have no supported features:
135 *
136 * 240, 240x, i1400
137 *
138 * Still missing DSDTs for the following models:
139 *
140 * A20p, A22e, A22m
141 * R52
142 * S31
143 * T43p
144 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400146IBM_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
147 "\\_SB.PCI.ISA.EC", /* 570 */
148 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
149 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
150 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
151 "\\_SB.PCI0.ICH3.EC0", /* R31 */
152 "\\_SB.PCI0.LPC.EC", /* all others */
153 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400155IBM_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
156 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
157 "\\_SB.PCI0.VID0", /* 770e */
158 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
159 "\\_SB.PCI0.AGP.VID", /* all others */
160 ); /* R30, R31 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400162IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400164IBM_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, T4x, X31, X40 */
165 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
166 "\\CMS", /* R40, R40e */
167 ); /* all others */
Kristen Accardi63e5f242006-02-23 17:56:06 -0800168#ifdef CONFIG_ACPI_IBM_DOCK
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400169IBM_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
170 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
171 "\\_SB.PCI0.PCI1.DOCK", /* all others */
172 "\\_SB.PCI.ISA.SLCE", /* 570 */
173 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
Kristen Accardi63e5f242006-02-23 17:56:06 -0800174#endif
Henrique de Moraes Holschuh2df910b2006-11-25 16:35:09 -0200175#ifdef CONFIG_ACPI_IBM_BAY
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400176IBM_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
177 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhe0298992006-11-25 16:35:09 -0200178 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400179 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
180 ); /* A21e, R30, R31 */
181
182IBM_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
183 "_EJ0", /* all others */
184 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
185
186IBM_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
187 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
188 ); /* all others */
189
190IBM_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
191 "_EJ0", /* 770x */
192 ); /* all others */
Henrique de Moraes Holschuh2df910b2006-11-25 16:35:09 -0200193#endif
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400194
195/* don't list other alternatives as we install a notify handler on the 570 */
196IBM_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
197
198IBM_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
199 "^HKEY", /* R30, R31 */
200 "HKEY", /* all others */
201 ); /* 570 */
202
203IBM_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
204IBM_HANDLE(ledb, ec, "LEDB"); /* G4x */
205
206IBM_HANDLE(led, ec, "SLED", /* 570 */
207 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
208 "LED", /* all others */
209 ); /* R30, R31 */
210
211IBM_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
212IBM_HANDLE(ecrd, ec, "ECRD"); /* 570 */
213IBM_HANDLE(ecwr, ec, "ECWR"); /* 570 */
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200214IBM_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400215
216IBM_HANDLE(gfan, ec, "GFAN", /* 570 */
217 "\\FSPD", /* 600e/x, 770e, 770x */
218 ); /* all others */
219
220IBM_HANDLE(sfan, ec, "SFAN", /* 570 */
221 "JFNS", /* 770x-JL */
222 ); /* all others */
223
224#define IBM_HKEY_HID "IBM0068"
225#define IBM_PCI_HID "PNP0A03"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -0200227enum thermal_access_mode {
228 IBMACPI_THERMAL_NONE = 0, /* No thermal support */
229 IBMACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
230 IBMACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -0200231 IBMACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
232 IBMACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -0200233};
234
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -0200235#define IBMACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -0200236struct ibm_thermal_sensors_struct {
237 s32 temp[IBMACPI_MAX_THERMAL_SENSORS];
238};
239
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200240/*
241 * FAN ACCESS MODES
242 *
243 * IBMACPI_FAN_RD_ACPI_GFAN:
244 * ACPI GFAN method: returns fan level
245 *
246 * see IBMACPI_FAN_WR_ACPI_SFAN
247 * EC 0x2f not available if GFAN exists
248 *
249 * IBMACPI_FAN_WR_ACPI_SFAN:
250 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
251 *
252 * EC 0x2f might be available *for reading*, but never for writing.
253 *
254 * IBMACPI_FAN_WR_TPEC:
255 * ThinkPad EC register 0x2f (HFSP): fan control loop mode Supported
256 * on almost all ThinkPads
257 *
258 * Fan speed changes of any sort (including those caused by the
259 * disengaged mode) are usually done slowly by the firmware as the
260 * maximum ammount of fan duty cycle change per second seems to be
261 * limited.
262 *
263 * Reading is not available if GFAN exists.
264 * Writing is not available if SFAN exists.
265 *
266 * Bits
267 * 7 automatic mode engaged;
268 * (default operation mode of the ThinkPad)
269 * fan level is ignored in this mode.
270 * 6 disengage mode (takes precedence over bit 7);
271 * not available on all thinkpads. May disable
272 * the tachometer, and speeds up fan to 100% duty-cycle,
273 * which speeds it up far above the standard RPM
274 * levels. It is not impossible that it could cause
275 * hardware damage.
276 * 5-3 unused in some models. Extra bits for fan level
277 * in others, but still useless as all values above
278 * 7 map to the same speed as level 7 in these models.
279 * 2-0 fan level (0..7 usually)
280 * 0x00 = stop
281 * 0x07 = max (set when temperatures critical)
282 * Some ThinkPads may have other levels, see
283 * IBMACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
284 *
285 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
286 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
287 * does so, its initial value is meaningless (0x07).
288 *
289 * For firmware bugs, refer to:
290 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
291 *
292 * ----
293 *
294 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
295 * Main fan tachometer reading (in RPM)
296 *
297 * This register is present on all ThinkPads with a new-style EC, and
298 * it is known not to be present on the A21m/e, and T22, as there is
299 * something else in offset 0x84 according to the ACPI DSDT. Other
300 * ThinkPads from this same time period (and earlier) probably lack the
301 * tachometer as well.
302 *
303 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
304 * was never fixed by IBM to report the EC firmware version string
305 * probably support the tachometer (like the early X models), so
306 * detecting it is quite hard. We need more data to know for sure.
307 *
308 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
309 * might result.
310 *
311 * FIRMWARE BUG: when EC 0x2f bit 6 is set (disengaged mode), this
312 * register is not invalidated in ThinkPads that disable tachometer
313 * readings. Thus, the tachometer readings go stale.
314 *
315 * For firmware bugs, refer to:
316 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
317 *
318 * IBMACPI_FAN_WR_ACPI_FANS:
319 * ThinkPad X31, X40, X41. Not available in the X60.
320 *
321 * FANS ACPI handle: takes three arguments: low speed, medium speed,
322 * high speed. ACPI DSDT seems to map these three speeds to levels
323 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
324 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
325 *
326 * The speeds are stored on handles
327 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
328 *
329 * There are three default speed sets, acessible as handles:
330 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
331 *
332 * ACPI DSDT switches which set is in use depending on various
333 * factors.
334 *
335 * IBMACPI_FAN_WR_TPEC is also available and should be used to
336 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
337 * but the ACPI tables just mention level 7.
338 */
339
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -0200340enum fan_status_access_mode {
341 IBMACPI_FAN_NONE = 0, /* No fan status or control */
342 IBMACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
343 IBMACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
344};
345
346enum fan_control_access_mode {
347 IBMACPI_FAN_WR_NONE = 0, /* No fan control */
348 IBMACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
349 IBMACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
350 IBMACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
351};
352
353enum fan_control_commands {
354 IBMACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
355 IBMACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -0200356 IBMACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
357 * and also watchdog cmd */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -0200358};
359
360enum { /* Fan control constants */
361 fan_status_offset = 0x2f, /* EC register 0x2f */
362 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
363 * 0x84 must be read before 0x85 */
Henrique de Moraes Holschuhbab812a2006-11-24 11:47:12 -0200364
365 IBMACPI_FAN_EC_DISENGAGED = 0x40, /* EC mode: tachometer
366 * disengaged */
367 IBMACPI_FAN_EC_AUTO = 0x80, /* EC mode: auto fan
368 * control */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -0200369};
370
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -0200371static char *ibm_thinkpad_ec_found = NULL;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -0200372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373struct ibm_struct {
374 char *name;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400375 char param[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
377 char *hid;
378 struct acpi_driver *driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400380 int (*init) (void);
381 int (*read) (char *);
382 int (*write) (char *);
383 void (*exit) (void);
384
385 void (*notify) (struct ibm_struct *, u32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 acpi_handle *handle;
387 int type;
388 struct acpi_device *device;
389
390 int driver_registered;
391 int proc_created;
392 int init_called;
393 int notify_installed;
394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 int experimental;
396};
397
398static struct proc_dir_entry *proc_dir = NULL;
399
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -0200400static struct backlight_device *ibm_backlight_device = NULL;
Holger Macht8acb0252006-10-20 14:30:28 -0700401
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402#define onoff(status,bit) ((status) & (1 << (bit)) ? "on" : "off")
403#define enabled(status,bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
404#define strlencmp(a,b) (strncmp((a), (b), strlen(b)))
405
406static int acpi_evalf(acpi_handle handle,
407 void *res, char *method, char *fmt, ...)
408{
409 char *fmt0 = fmt;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400410 struct acpi_object_list params;
411 union acpi_object in_objs[IBM_MAX_ACPI_ARGS];
412 struct acpi_buffer result, *resultp;
413 union acpi_object out_obj;
414 acpi_status status;
415 va_list ap;
416 char res_type;
417 int success;
418 int quiet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
420 if (!*fmt) {
421 printk(IBM_ERR "acpi_evalf() called with empty format\n");
422 return 0;
423 }
424
425 if (*fmt == 'q') {
426 quiet = 1;
427 fmt++;
428 } else
429 quiet = 0;
430
431 res_type = *(fmt++);
432
433 params.count = 0;
434 params.pointer = &in_objs[0];
435
436 va_start(ap, fmt);
437 while (*fmt) {
438 char c = *(fmt++);
439 switch (c) {
440 case 'd': /* int */
441 in_objs[params.count].integer.value = va_arg(ap, int);
442 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
443 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400444 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 default:
446 printk(IBM_ERR "acpi_evalf() called "
447 "with invalid format character '%c'\n", c);
448 return 0;
449 }
450 }
451 va_end(ap);
452
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400453 if (res_type != 'v') {
454 result.length = sizeof(out_obj);
455 result.pointer = &out_obj;
456 resultp = &result;
457 } else
458 resultp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400460 status = acpi_evaluate_object(handle, method, &params, resultp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
462 switch (res_type) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400463 case 'd': /* int */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 if (res)
465 *(int *)res = out_obj.integer.value;
466 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
467 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400468 case 'v': /* void */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 success = status == AE_OK;
470 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400471 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 default:
473 printk(IBM_ERR "acpi_evalf() called "
474 "with invalid format character '%c'\n", res_type);
475 return 0;
476 }
477
478 if (!success && !quiet)
479 printk(IBM_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
480 method, fmt0, status);
481
482 return success;
483}
484
485static void __unused acpi_print_int(acpi_handle handle, char *method)
486{
487 int i;
488
489 if (acpi_evalf(handle, &i, method, "d"))
490 printk(IBM_INFO "%s = 0x%x\n", method, i);
491 else
492 printk(IBM_ERR "error calling %s\n", method);
493}
494
495static char *next_cmd(char **cmds)
496{
497 char *start = *cmds;
498 char *end;
499
500 while ((end = strchr(start, ',')) && end == start)
501 start = end + 1;
502
503 if (!end)
504 return NULL;
505
506 *end = 0;
507 *cmds = end + 1;
508 return start;
509}
510
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400511static int driver_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512{
513 printk(IBM_INFO "%s v%s\n", IBM_DESC, IBM_VERSION);
514 printk(IBM_INFO "%s\n", IBM_URL);
515
516 return 0;
517}
518
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400519static int driver_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520{
521 int len = 0;
522
523 len += sprintf(p + len, "driver:\t\t%s\n", IBM_DESC);
524 len += sprintf(p + len, "version:\t%s\n", IBM_VERSION);
525
526 return len;
527}
528
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400529static int hotkey_supported;
530static int hotkey_mask_supported;
531static int hotkey_orig_status;
532static int hotkey_orig_mask;
533
534static int hotkey_get(int *status, int *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535{
536 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400537 return 0;
538
539 if (hotkey_mask_supported)
540 if (!acpi_evalf(hkey_handle, mask, "DHKN", "d"))
541 return 0;
542
543 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544}
545
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400546static int hotkey_set(int status, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547{
548 int i;
549
550 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 return 0;
552
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400553 if (hotkey_mask_supported)
554 for (i = 0; i < 32; i++) {
555 int bit = ((1 << i) & mask) != 0;
556 if (!acpi_evalf(hkey_handle,
557 NULL, "MHKM", "vdd", i + 1, bit))
558 return 0;
559 }
560
561 return 1;
562}
563
564static int hotkey_init(void)
565{
566 /* hotkey not supported on 570 */
567 hotkey_supported = hkey_handle != NULL;
568
569 if (hotkey_supported) {
570 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
571 A30, R30, R31, T20-22, X20-21, X22-24 */
572 hotkey_mask_supported =
573 acpi_evalf(hkey_handle, NULL, "DHKN", "qv");
574
575 if (!hotkey_get(&hotkey_orig_status, &hotkey_orig_mask))
576 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 }
578
579 return 0;
580}
581
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400582static int hotkey_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583{
584 int status, mask;
585 int len = 0;
586
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400587 if (!hotkey_supported) {
588 len += sprintf(p + len, "status:\t\tnot supported\n");
589 return len;
590 }
591
592 if (!hotkey_get(&status, &mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 return -EIO;
594
595 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400596 if (hotkey_mask_supported) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 len += sprintf(p + len, "mask:\t\t0x%04x\n", mask);
598 len += sprintf(p + len,
599 "commands:\tenable, disable, reset, <mask>\n");
600 } else {
601 len += sprintf(p + len, "mask:\t\tnot supported\n");
602 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
603 }
604
605 return len;
606}
607
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400608static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609{
610 int status, mask;
611 char *cmd;
612 int do_cmd = 0;
613
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400614 if (!hotkey_supported)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 return -ENODEV;
616
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400617 if (!hotkey_get(&status, &mask))
618 return -EIO;
619
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 while ((cmd = next_cmd(&buf))) {
621 if (strlencmp(cmd, "enable") == 0) {
622 status = 1;
623 } else if (strlencmp(cmd, "disable") == 0) {
624 status = 0;
625 } else if (strlencmp(cmd, "reset") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400626 status = hotkey_orig_status;
627 mask = hotkey_orig_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
629 /* mask set */
630 } else if (sscanf(cmd, "%x", &mask) == 1) {
631 /* mask set */
632 } else
633 return -EINVAL;
634 do_cmd = 1;
635 }
636
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400637 if (do_cmd && !hotkey_set(status, mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 return -EIO;
639
640 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400641}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400643static void hotkey_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400645 if (hotkey_supported)
646 hotkey_set(hotkey_orig_status, hotkey_orig_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647}
648
649static void hotkey_notify(struct ibm_struct *ibm, u32 event)
650{
651 int hkey;
652
653 if (acpi_evalf(hkey_handle, &hkey, "MHKP", "d"))
654 acpi_bus_generate_event(ibm->device, event, hkey);
655 else {
656 printk(IBM_ERR "unknown hotkey event %d\n", event);
657 acpi_bus_generate_event(ibm->device, event, 0);
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400658 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659}
660
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400661static int bluetooth_supported;
662
663static int bluetooth_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400665 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
666 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
667 bluetooth_supported = hkey_handle &&
668 acpi_evalf(hkey_handle, NULL, "GBDC", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
670 return 0;
671}
672
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400673static int bluetooth_status(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674{
675 int status;
676
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400677 if (!bluetooth_supported ||
678 !acpi_evalf(hkey_handle, &status, "GBDC", "d"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 status = 0;
680
681 return status;
682}
683
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400684static int bluetooth_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
686 int len = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400687 int status = bluetooth_status();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400689 if (!bluetooth_supported)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 len += sprintf(p + len, "status:\t\tnot supported\n");
691 else if (!(status & 1))
692 len += sprintf(p + len, "status:\t\tnot installed\n");
693 else {
694 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 1));
695 len += sprintf(p + len, "commands:\tenable, disable\n");
696 }
697
698 return len;
699}
700
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400701static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400703 int status = bluetooth_status();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 char *cmd;
705 int do_cmd = 0;
706
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400707 if (!bluetooth_supported)
708 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
710 while ((cmd = next_cmd(&buf))) {
711 if (strlencmp(cmd, "enable") == 0) {
712 status |= 2;
713 } else if (strlencmp(cmd, "disable") == 0) {
714 status &= ~2;
715 } else
716 return -EINVAL;
717 do_cmd = 1;
718 }
719
720 if (do_cmd && !acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400721 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
723 return 0;
724}
725
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -0400726static int wan_supported;
727
728static int wan_init(void)
729{
730 wan_supported = hkey_handle &&
731 acpi_evalf(hkey_handle, NULL, "GWAN", "qv");
732
733 return 0;
734}
735
736static int wan_status(void)
737{
738 int status;
739
Henrique de Moraes Holschuh8d297262006-11-24 11:47:07 -0200740 if (!wan_supported || !acpi_evalf(hkey_handle, &status, "GWAN", "d"))
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -0400741 status = 0;
742
743 return status;
744}
745
746static int wan_read(char *p)
747{
748 int len = 0;
749 int status = wan_status();
750
751 if (!wan_supported)
752 len += sprintf(p + len, "status:\t\tnot supported\n");
753 else if (!(status & 1))
754 len += sprintf(p + len, "status:\t\tnot installed\n");
755 else {
756 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 1));
757 len += sprintf(p + len, "commands:\tenable, disable\n");
758 }
759
760 return len;
761}
762
763static int wan_write(char *buf)
764{
765 int status = wan_status();
766 char *cmd;
767 int do_cmd = 0;
768
769 if (!wan_supported)
770 return -ENODEV;
771
772 while ((cmd = next_cmd(&buf))) {
773 if (strlencmp(cmd, "enable") == 0) {
774 status |= 2;
775 } else if (strlencmp(cmd, "disable") == 0) {
776 status &= ~2;
777 } else
778 return -EINVAL;
779 do_cmd = 1;
780 }
781
782 if (do_cmd && !acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
783 return -EIO;
784
785 return 0;
786}
787
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400788static int video_supported;
789static int video_orig_autosw;
790
791#define VIDEO_570 1
792#define VIDEO_770 2
793#define VIDEO_NEW 3
794
795static int video_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400797 int ivga;
798
799 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
800 /* G41, assume IVGA doesn't change */
801 vid_handle = vid2_handle;
802
803 if (!vid_handle)
804 /* video switching not supported on R30, R31 */
805 video_supported = 0;
806 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
807 /* 570 */
808 video_supported = VIDEO_570;
809 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
810 /* 600e/x, 770e, 770x */
811 video_supported = VIDEO_770;
812 else
813 /* all others */
814 video_supported = VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
816 return 0;
817}
818
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400819static int video_status(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820{
821 int status = 0;
822 int i;
823
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400824 if (video_supported == VIDEO_570) {
825 if (acpi_evalf(NULL, &i, "\\_SB.PHS", "dd", 0x87))
826 status = i & 3;
827 } else if (video_supported == VIDEO_770) {
828 if (acpi_evalf(NULL, &i, "\\VCDL", "d"))
829 status |= 0x01 * i;
830 if (acpi_evalf(NULL, &i, "\\VCDC", "d"))
831 status |= 0x02 * i;
832 } else if (video_supported == VIDEO_NEW) {
833 acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1);
834 if (acpi_evalf(NULL, &i, "\\VCDC", "d"))
835 status |= 0x02 * i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400837 acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0);
838 if (acpi_evalf(NULL, &i, "\\VCDL", "d"))
839 status |= 0x01 * i;
840 if (acpi_evalf(NULL, &i, "\\VCDD", "d"))
841 status |= 0x08 * i;
842 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
844 return status;
845}
846
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400847static int video_autosw(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400849 int autosw = 0;
850
851 if (video_supported == VIDEO_570)
852 acpi_evalf(vid_handle, &autosw, "SWIT", "d");
853 else if (video_supported == VIDEO_770 || video_supported == VIDEO_NEW)
854 acpi_evalf(vid_handle, &autosw, "^VDEE", "d");
855
856 return autosw & 1;
857}
858
859static int video_read(char *p)
860{
861 int status = video_status();
862 int autosw = video_autosw();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 int len = 0;
864
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400865 if (!video_supported) {
866 len += sprintf(p + len, "status:\t\tnot supported\n");
867 return len;
868 }
869
870 len += sprintf(p + len, "status:\t\tsupported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
872 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400873 if (video_supported == VIDEO_NEW)
874 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
875 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
876 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
877 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
878 if (video_supported == VIDEO_NEW)
879 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
880 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
882
883 return len;
884}
885
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400886static int video_switch(void)
887{
888 int autosw = video_autosw();
889 int ret;
890
891 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
892 return -EIO;
893 ret = video_supported == VIDEO_570 ?
894 acpi_evalf(ec_handle, NULL, "_Q16", "v") :
895 acpi_evalf(vid_handle, NULL, "VSWT", "v");
896 acpi_evalf(vid_handle, NULL, "_DOS", "vd", autosw);
897
898 return ret;
899}
900
901static int video_expand(void)
902{
903 if (video_supported == VIDEO_570)
904 return acpi_evalf(ec_handle, NULL, "_Q17", "v");
905 else if (video_supported == VIDEO_770)
906 return acpi_evalf(vid_handle, NULL, "VEXP", "v");
907 else
908 return acpi_evalf(NULL, NULL, "\\VEXP", "v");
909}
910
911static int video_switch2(int status)
912{
913 int ret;
914
915 if (video_supported == VIDEO_570) {
916 ret = acpi_evalf(NULL, NULL,
917 "\\_SB.PHS2", "vdd", 0x8b, status | 0x80);
918 } else if (video_supported == VIDEO_770) {
919 int autosw = video_autosw();
920 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
921 return -EIO;
922
923 ret = acpi_evalf(vid_handle, NULL,
924 "ASWT", "vdd", status * 0x100, 0);
925
926 acpi_evalf(vid_handle, NULL, "_DOS", "vd", autosw);
927 } else {
928 ret = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
929 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
930 }
931
932 return ret;
933}
934
935static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936{
937 char *cmd;
938 int enable, disable, status;
939
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400940 if (!video_supported)
941 return -ENODEV;
942
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 enable = disable = 0;
944
945 while ((cmd = next_cmd(&buf))) {
946 if (strlencmp(cmd, "lcd_enable") == 0) {
947 enable |= 0x01;
948 } else if (strlencmp(cmd, "lcd_disable") == 0) {
949 disable |= 0x01;
950 } else if (strlencmp(cmd, "crt_enable") == 0) {
951 enable |= 0x02;
952 } else if (strlencmp(cmd, "crt_disable") == 0) {
953 disable |= 0x02;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400954 } else if (video_supported == VIDEO_NEW &&
955 strlencmp(cmd, "dvi_enable") == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 enable |= 0x08;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400957 } else if (video_supported == VIDEO_NEW &&
958 strlencmp(cmd, "dvi_disable") == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 disable |= 0x08;
960 } else if (strlencmp(cmd, "auto_enable") == 0) {
961 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
962 return -EIO;
963 } else if (strlencmp(cmd, "auto_disable") == 0) {
964 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 0))
965 return -EIO;
966 } else if (strlencmp(cmd, "video_switch") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400967 if (!video_switch())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 return -EIO;
969 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400970 if (!video_expand())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 return -EIO;
972 } else
973 return -EINVAL;
974 }
975
976 if (enable || disable) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400977 status = (video_status() & 0x0f & ~disable) | enable;
978 if (!video_switch2(status))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 return -EIO;
980 }
981
982 return 0;
983}
984
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400985static void video_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400987 acpi_evalf(vid_handle, NULL, "_DOS", "vd", video_orig_autosw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988}
989
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400990static int light_supported;
991static int light_status_supported;
992
993static int light_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400995 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
996 light_supported = (cmos_handle || lght_handle) && !ledb_handle;
997
998 if (light_supported)
999 /* light status not supported on
1000 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
1001 light_status_supported = acpi_evalf(ec_handle, NULL,
1002 "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
1004 return 0;
1005}
1006
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001007static int light_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008{
1009 int len = 0;
1010 int status = 0;
1011
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001012 if (!light_supported) {
1013 len += sprintf(p + len, "status:\t\tnot supported\n");
1014 } else if (!light_status_supported) {
1015 len += sprintf(p + len, "status:\t\tunknown\n");
1016 len += sprintf(p + len, "commands:\ton, off\n");
1017 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
1019 return -EIO;
1020 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001021 len += sprintf(p + len, "commands:\ton, off\n");
1022 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
1024 return len;
1025}
1026
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001027static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028{
1029 int cmos_cmd, lght_cmd;
1030 char *cmd;
1031 int success;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001032
1033 if (!light_supported)
1034 return -ENODEV;
1035
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 while ((cmd = next_cmd(&buf))) {
1037 if (strlencmp(cmd, "on") == 0) {
1038 cmos_cmd = 0x0c;
1039 lght_cmd = 1;
1040 } else if (strlencmp(cmd, "off") == 0) {
1041 cmos_cmd = 0x0d;
1042 lght_cmd = 0;
1043 } else
1044 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001045
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 success = cmos_handle ?
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001047 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
1048 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 if (!success)
1050 return -EIO;
1051 }
1052
1053 return 0;
1054}
1055
Henrique de Moraes Holschuh2df910b2006-11-25 16:35:09 -02001056#if defined(CONFIG_ACPI_IBM_DOCK) || defined(CONFIG_ACPI_IBM_BAY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057static int _sta(acpi_handle handle)
1058{
1059 int status;
1060
1061 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
1062 status = 0;
1063
1064 return status;
1065}
Henrique de Moraes Holschuh2df910b2006-11-25 16:35:09 -02001066#endif
Kristen Accardi63e5f242006-02-23 17:56:06 -08001067#ifdef CONFIG_ACPI_IBM_DOCK
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068#define dock_docked() (_sta(dock_handle) & 1)
1069
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001070static int dock_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071{
1072 int len = 0;
1073 int docked = dock_docked();
1074
1075 if (!dock_handle)
1076 len += sprintf(p + len, "status:\t\tnot supported\n");
1077 else if (!docked)
1078 len += sprintf(p + len, "status:\t\tundocked\n");
1079 else {
1080 len += sprintf(p + len, "status:\t\tdocked\n");
1081 len += sprintf(p + len, "commands:\tdock, undock\n");
1082 }
1083
1084 return len;
1085}
1086
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001087static int dock_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088{
1089 char *cmd;
1090
1091 if (!dock_docked())
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001092 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
1094 while ((cmd = next_cmd(&buf))) {
1095 if (strlencmp(cmd, "undock") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001096 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
1097 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 return -EIO;
1099 } else if (strlencmp(cmd, "dock") == 0) {
1100 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
1101 return -EIO;
1102 } else
1103 return -EINVAL;
1104 }
1105
1106 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001107}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
1109static void dock_notify(struct ibm_struct *ibm, u32 event)
1110{
1111 int docked = dock_docked();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001112 int pci = ibm->hid && strstr(ibm->hid, IBM_PCI_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001114 if (event == 1 && !pci) /* 570 */
1115 acpi_bus_generate_event(ibm->device, event, 1); /* button */
1116 else if (event == 1 && pci) /* 570 */
1117 acpi_bus_generate_event(ibm->device, event, 3); /* dock */
1118 else if (event == 3 && docked)
1119 acpi_bus_generate_event(ibm->device, event, 1); /* button */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 else if (event == 3 && !docked)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001121 acpi_bus_generate_event(ibm->device, event, 2); /* undock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 else if (event == 0 && docked)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001123 acpi_bus_generate_event(ibm->device, event, 3); /* dock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 else {
1125 printk(IBM_ERR "unknown dock event %d, status %d\n",
1126 event, _sta(dock_handle));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001127 acpi_bus_generate_event(ibm->device, event, 0); /* unknown */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 }
1129}
Kristen Accardi63e5f242006-02-23 17:56:06 -08001130#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
Henrique de Moraes Holschuh2df910b2006-11-25 16:35:09 -02001132#ifdef CONFIG_ACPI_IBM_BAY
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001133static int bay_status_supported;
1134static int bay_status2_supported;
1135static int bay_eject_supported;
1136static int bay_eject2_supported;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001138static int bay_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001140 bay_status_supported = bay_handle &&
1141 acpi_evalf(bay_handle, NULL, "_STA", "qv");
1142 bay_status2_supported = bay2_handle &&
1143 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
1144
1145 bay_eject_supported = bay_handle && bay_ej_handle &&
1146 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
1147 bay_eject2_supported = bay2_handle && bay2_ej_handle &&
1148 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
1150 return 0;
1151}
1152
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001153#define bay_occupied(b) (_sta(b##_handle) & 1)
1154
1155static int bay_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156{
1157 int len = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001158 int occupied = bay_occupied(bay);
1159 int occupied2 = bay_occupied(bay2);
1160 int eject, eject2;
1161
1162 len += sprintf(p + len, "status:\t\t%s\n", bay_status_supported ?
1163 (occupied ? "occupied" : "unoccupied") :
1164 "not supported");
1165 if (bay_status2_supported)
1166 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
1167 "occupied" : "unoccupied");
1168
1169 eject = bay_eject_supported && occupied;
1170 eject2 = bay_eject2_supported && occupied2;
1171
1172 if (eject && eject2)
1173 len += sprintf(p + len, "commands:\teject, eject2\n");
1174 else if (eject)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 len += sprintf(p + len, "commands:\teject\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001176 else if (eject2)
1177 len += sprintf(p + len, "commands:\teject2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
1179 return len;
1180}
1181
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001182static int bay_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183{
1184 char *cmd;
1185
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001186 if (!bay_eject_supported && !bay_eject2_supported)
1187 return -ENODEV;
1188
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001190 if (bay_eject_supported && strlencmp(cmd, "eject") == 0) {
1191 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
1192 return -EIO;
1193 } else if (bay_eject2_supported &&
1194 strlencmp(cmd, "eject2") == 0) {
1195 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 return -EIO;
1197 } else
1198 return -EINVAL;
1199 }
1200
1201 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001202}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
1204static void bay_notify(struct ibm_struct *ibm, u32 event)
1205{
1206 acpi_bus_generate_event(ibm->device, event, 0);
1207}
Henrique de Moraes Holschuh2df910b2006-11-25 16:35:09 -02001208#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001210static int cmos_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211{
1212 int len = 0;
1213
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001214 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1215 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 if (!cmos_handle)
1217 len += sprintf(p + len, "status:\t\tnot supported\n");
1218 else {
1219 len += sprintf(p + len, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001220 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 }
1222
1223 return len;
1224}
1225
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001226static int cmos_eval(int cmos_cmd)
1227{
1228 if (cmos_handle)
1229 return acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd);
1230 else
1231 return 1;
1232}
1233
1234static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235{
1236 char *cmd;
1237 int cmos_cmd;
1238
1239 if (!cmos_handle)
1240 return -EINVAL;
1241
1242 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001243 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
1244 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 /* cmos_cmd set */
1246 } else
1247 return -EINVAL;
1248
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001249 if (!cmos_eval(cmos_cmd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 return -EIO;
1251 }
1252
1253 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001254}
1255
1256static int led_supported;
1257
1258#define LED_570 1
1259#define LED_OLD 2
1260#define LED_NEW 3
1261
1262static int led_init(void)
1263{
1264 if (!led_handle)
1265 /* led not supported on R30, R31 */
1266 led_supported = 0;
1267 else if (strlencmp(led_path, "SLED") == 0)
1268 /* 570 */
1269 led_supported = LED_570;
1270 else if (strlencmp(led_path, "SYSL") == 0)
1271 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
1272 led_supported = LED_OLD;
1273 else
1274 /* all others */
1275 led_supported = LED_NEW;
1276
1277 return 0;
1278}
1279
1280#define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
1281
1282static int led_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283{
1284 int len = 0;
1285
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001286 if (!led_supported) {
1287 len += sprintf(p + len, "status:\t\tnot supported\n");
1288 return len;
1289 }
1290 len += sprintf(p + len, "status:\t\tsupported\n");
1291
1292 if (led_supported == LED_570) {
1293 /* 570 */
1294 int i, status;
1295 for (i = 0; i < 8; i++) {
1296 if (!acpi_evalf(ec_handle,
1297 &status, "GLED", "dd", 1 << i))
1298 return -EIO;
1299 len += sprintf(p + len, "%d:\t\t%s\n",
1300 i, led_status(status));
1301 }
1302 }
1303
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 len += sprintf(p + len, "commands:\t"
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001305 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
1307 return len;
1308}
1309
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001310/* off, on, blink */
1311static const int led_sled_arg1[] = { 0, 1, 3 };
1312static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
1313static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
1314static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
1315
1316#define EC_HLCL 0x0c
1317#define EC_HLBL 0x0d
1318#define EC_HLMS 0x0e
1319
1320static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321{
1322 char *cmd;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001323 int led, ind, ret;
1324
1325 if (!led_supported)
1326 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
1328 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001329 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 return -EINVAL;
1331
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001332 if (strstr(cmd, "off")) {
1333 ind = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 } else if (strstr(cmd, "on")) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001335 ind = 1;
1336 } else if (strstr(cmd, "blink")) {
1337 ind = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 } else
1339 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001340
1341 if (led_supported == LED_570) {
1342 /* 570 */
1343 led = 1 << led;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001345 led, led_sled_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001347 } else if (led_supported == LED_OLD) {
1348 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
1349 led = 1 << led;
1350 ret = ec_write(EC_HLMS, led);
1351 if (ret >= 0)
1352 ret =
1353 ec_write(EC_HLBL, led * led_exp_hlbl[ind]);
1354 if (ret >= 0)
1355 ret =
1356 ec_write(EC_HLCL, led * led_exp_hlcl[ind]);
1357 if (ret < 0)
1358 return ret;
1359 } else {
1360 /* all others */
1361 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
1362 led, led_led_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001364 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 }
1366
1367 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001368}
1369
1370static int beep_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371{
1372 int len = 0;
1373
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001374 if (!beep_handle)
1375 len += sprintf(p + len, "status:\t\tnot supported\n");
1376 else {
1377 len += sprintf(p + len, "status:\t\tsupported\n");
1378 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
1379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
1381 return len;
1382}
1383
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001384static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385{
1386 char *cmd;
1387 int beep_cmd;
1388
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001389 if (!beep_handle)
1390 return -ENODEV;
1391
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001393 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
1394 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 /* beep_cmd set */
1396 } else
1397 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001398 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 return -EIO;
1400 }
1401
1402 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001403}
1404
1405static int acpi_ec_read(int i, u8 * p)
1406{
1407 int v;
1408
1409 if (ecrd_handle) {
1410 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
1411 return 0;
1412 *p = v;
1413 } else {
1414 if (ec_read(i, p) < 0)
1415 return 0;
1416 }
1417
1418 return 1;
1419}
1420
1421static int acpi_ec_write(int i, u8 v)
1422{
1423 if (ecwr_handle) {
1424 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
1425 return 0;
1426 } else {
1427 if (ec_write(i, v) < 0)
1428 return 0;
1429 }
1430
1431 return 1;
1432}
1433
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001434static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001435
1436static int thermal_init(void)
1437{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001438 u8 t, ta1, ta2;
1439 int i;
1440 int acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
1441
1442 if (ibm_thinkpad_ec_found && experimental) {
1443 /*
1444 * Direct EC access mode: sensors at registers
1445 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
1446 * non-implemented, thermal sensors return 0x80 when
1447 * not available
1448 */
1449
1450 ta1 = ta2 = 0;
1451 for (i = 0; i < 8; i++) {
1452 if (likely(acpi_ec_read(0x78 + i, &t))) {
1453 ta1 |= t;
1454 } else {
1455 ta1 = 0;
1456 break;
1457 }
1458 if (likely(acpi_ec_read(0xC0 + i, &t))) {
1459 ta2 |= t;
1460 } else {
1461 ta1 = 0;
1462 break;
1463 }
1464 }
1465 if (ta1 == 0) {
1466 /* This is sheer paranoia, but we handle it anyway */
1467 if (acpi_tmp7) {
1468 printk(IBM_ERR
1469 "ThinkPad ACPI EC access misbehaving, "
1470 "falling back to ACPI TMPx access mode\n");
1471 thermal_read_mode = IBMACPI_THERMAL_ACPI_TMP07;
1472 } else {
1473 printk(IBM_ERR
1474 "ThinkPad ACPI EC access misbehaving, "
1475 "disabling thermal sensors access\n");
1476 thermal_read_mode = IBMACPI_THERMAL_NONE;
1477 }
1478 } else {
1479 thermal_read_mode =
1480 (ta2 != 0) ?
1481 IBMACPI_THERMAL_TPEC_16 : IBMACPI_THERMAL_TPEC_8;
1482 }
1483 } else if (acpi_tmp7) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001484 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
1485 /* 600e/x, 770e, 770x */
1486 thermal_read_mode = IBMACPI_THERMAL_ACPI_UPDT;
1487 } else {
1488 /* Standard ACPI TMPx access, max 8 sensors */
1489 thermal_read_mode = IBMACPI_THERMAL_ACPI_TMP07;
1490 }
1491 } else {
1492 /* temperatures not supported on 570, G4x, R30, R31, R32 */
1493 thermal_read_mode = IBMACPI_THERMAL_NONE;
1494 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001495
1496 return 0;
1497}
1498
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001499static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
1500{
1501 int i, t;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001502 s8 tmp;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001503 char tmpi[] = "TMPi";
1504
1505 if (!s)
1506 return -EINVAL;
1507
1508 switch (thermal_read_mode) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001509#if IBMACPI_MAX_THERMAL_SENSORS >= 16
1510 case IBMACPI_THERMAL_TPEC_16:
1511 for (i = 0; i < 8; i++) {
1512 if (!acpi_ec_read(0xC0 + i, &tmp))
1513 return -EIO;
1514 s->temp[i + 8] = tmp * 1000;
1515 }
1516 /* fallthrough */
1517#endif
1518 case IBMACPI_THERMAL_TPEC_8:
1519 for (i = 0; i < 8; i++) {
1520 if (!acpi_ec_read(0x78 + i, &tmp))
1521 return -EIO;
1522 s->temp[i] = tmp * 1000;
1523 }
1524 return (thermal_read_mode == IBMACPI_THERMAL_TPEC_16) ? 16 : 8;
1525
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001526 case IBMACPI_THERMAL_ACPI_UPDT:
1527 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
1528 return -EIO;
1529 for (i = 0; i < 8; i++) {
1530 tmpi[3] = '0' + i;
1531 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
1532 return -EIO;
1533 s->temp[i] = (t - 2732) * 100;
1534 }
1535 return 8;
1536
1537 case IBMACPI_THERMAL_ACPI_TMP07:
1538 for (i = 0; i < 8; i++) {
1539 tmpi[3] = '0' + i;
1540 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
1541 return -EIO;
1542 s->temp[i] = t * 1000;
1543 }
1544 return 8;
1545
1546 case IBMACPI_THERMAL_NONE:
1547 default:
1548 return 0;
1549 }
1550}
1551
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001552static int thermal_read(char *p)
1553{
1554 int len = 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001555 int n, i;
1556 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001557
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001558 n = thermal_get_sensors(&t);
1559 if (unlikely(n < 0))
1560 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001561
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001562 len += sprintf(p + len, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001563
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001564 if (n > 0) {
1565 for (i = 0; i < (n - 1); i++)
1566 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
1567 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
1568 } else
1569 len += sprintf(p + len, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001570
1571 return len;
1572}
1573
1574static u8 ecdump_regs[256];
1575
1576static int ecdump_read(char *p)
1577{
1578 int len = 0;
1579 int i, j;
1580 u8 v;
1581
1582 len += sprintf(p + len, "EC "
1583 " +00 +01 +02 +03 +04 +05 +06 +07"
1584 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
1585 for (i = 0; i < 256; i += 16) {
1586 len += sprintf(p + len, "EC 0x%02x:", i);
1587 for (j = 0; j < 16; j++) {
1588 if (!acpi_ec_read(i + j, &v))
1589 break;
1590 if (v != ecdump_regs[i + j])
1591 len += sprintf(p + len, " *%02x", v);
1592 else
1593 len += sprintf(p + len, " %02x", v);
1594 ecdump_regs[i + j] = v;
1595 }
1596 len += sprintf(p + len, "\n");
1597 if (j != 16)
1598 break;
1599 }
1600
1601 /* These are way too dangerous to advertise openly... */
1602#if 0
1603 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
1604 " (<offset> is 00-ff, <value> is 00-ff)\n");
1605 len += sprintf(p + len, "commands:\t0x<offset> <value> "
1606 " (<offset> is 00-ff, <value> is 0-255)\n");
1607#endif
1608 return len;
1609}
1610
1611static int ecdump_write(char *buf)
1612{
1613 char *cmd;
1614 int i, v;
1615
1616 while ((cmd = next_cmd(&buf))) {
1617 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
1618 /* i and v set */
1619 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
1620 /* i and v set */
1621 } else
1622 return -EINVAL;
1623 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
1624 if (!acpi_ec_write(i, v))
1625 return -EIO;
1626 } else
1627 return -EINVAL;
1628 }
1629
1630 return 0;
1631}
1632
1633static int brightness_offset = 0x31;
1634
Holger Macht8acb0252006-10-20 14:30:28 -07001635static int brightness_get(struct backlight_device *bd)
1636{
Henrique de Moraes Holschuh8d297262006-11-24 11:47:07 -02001637 u8 level;
1638 if (!acpi_ec_read(brightness_offset, &level))
1639 return -EIO;
Holger Macht8acb0252006-10-20 14:30:28 -07001640
Henrique de Moraes Holschuh8d297262006-11-24 11:47:07 -02001641 level &= 0x7;
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02001642
Henrique de Moraes Holschuh8d297262006-11-24 11:47:07 -02001643 return level;
Holger Macht8acb0252006-10-20 14:30:28 -07001644}
1645
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001646static int brightness_read(char *p)
1647{
1648 int len = 0;
Holger Macht8acb0252006-10-20 14:30:28 -07001649 int level;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001650
Holger Macht8acb0252006-10-20 14:30:28 -07001651 if ((level = brightness_get(NULL)) < 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001652 len += sprintf(p + len, "level:\t\tunreadable\n");
1653 } else {
1654 len += sprintf(p + len, "level:\t\t%d\n", level & 0x7);
1655 len += sprintf(p + len, "commands:\tup, down\n");
1656 len += sprintf(p + len, "commands:\tlevel <level>"
1657 " (<level> is 0-7)\n");
1658 }
1659
1660 return len;
1661}
1662
1663#define BRIGHTNESS_UP 4
1664#define BRIGHTNESS_DOWN 5
1665
Holger Macht8acb0252006-10-20 14:30:28 -07001666static int brightness_set(int value)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001667{
1668 int cmos_cmd, inc, i;
Holger Macht8acb0252006-10-20 14:30:28 -07001669 int current_value = brightness_get(NULL);
1670
1671 value &= 7;
1672
1673 cmos_cmd = value > current_value ? BRIGHTNESS_UP : BRIGHTNESS_DOWN;
1674 inc = value > current_value ? 1 : -1;
1675 for (i = current_value; i != value; i += inc) {
1676 if (!cmos_eval(cmos_cmd))
1677 return -EIO;
1678 if (!acpi_ec_write(brightness_offset, i + inc))
1679 return -EIO;
1680 }
1681
1682 return 0;
1683}
1684
1685static int brightness_write(char *buf)
1686{
1687 int level;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001688 int new_level;
1689 char *cmd;
1690
1691 while ((cmd = next_cmd(&buf))) {
Holger Macht8acb0252006-10-20 14:30:28 -07001692 if ((level = brightness_get(NULL)) < 0)
1693 return level;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001694 level &= 7;
1695
1696 if (strlencmp(cmd, "up") == 0) {
1697 new_level = level == 7 ? 7 : level + 1;
1698 } else if (strlencmp(cmd, "down") == 0) {
1699 new_level = level == 0 ? 0 : level - 1;
1700 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
1701 new_level >= 0 && new_level <= 7) {
1702 /* new_level set */
1703 } else
1704 return -EINVAL;
1705
Holger Macht8acb0252006-10-20 14:30:28 -07001706 brightness_set(new_level);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001707 }
1708
1709 return 0;
1710}
1711
Holger Macht8acb0252006-10-20 14:30:28 -07001712static int brightness_update_status(struct backlight_device *bd)
1713{
1714 return brightness_set(bd->props->brightness);
1715}
1716
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02001717static struct backlight_properties ibm_backlight_data = {
1718 .owner = THIS_MODULE,
1719 .get_brightness = brightness_get,
1720 .update_status = brightness_update_status,
1721 .max_brightness = 7,
1722};
1723
1724static int brightness_init(void)
1725{
1726 ibm_backlight_device = backlight_device_register("ibm", NULL,
1727 &ibm_backlight_data);
1728 if (IS_ERR(ibm_backlight_device)) {
1729 printk(IBM_ERR "Could not register backlight device\n");
1730 return PTR_ERR(ibm_backlight_device);
1731 }
1732
1733 return 0;
1734}
1735
1736static void brightness_exit(void)
1737{
1738 if (ibm_backlight_device) {
1739 backlight_device_unregister(ibm_backlight_device);
1740 ibm_backlight_device = NULL;
1741 }
1742}
1743
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001744static int volume_offset = 0x30;
1745
1746static int volume_read(char *p)
1747{
1748 int len = 0;
1749 u8 level;
1750
1751 if (!acpi_ec_read(volume_offset, &level)) {
1752 len += sprintf(p + len, "level:\t\tunreadable\n");
1753 } else {
1754 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
1755 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
1756 len += sprintf(p + len, "commands:\tup, down, mute\n");
1757 len += sprintf(p + len, "commands:\tlevel <level>"
1758 " (<level> is 0-15)\n");
1759 }
1760
1761 return len;
1762}
1763
1764#define VOLUME_DOWN 0
1765#define VOLUME_UP 1
1766#define VOLUME_MUTE 2
1767
1768static int volume_write(char *buf)
1769{
1770 int cmos_cmd, inc, i;
1771 u8 level, mute;
1772 int new_level, new_mute;
1773 char *cmd;
1774
1775 while ((cmd = next_cmd(&buf))) {
1776 if (!acpi_ec_read(volume_offset, &level))
1777 return -EIO;
1778 new_mute = mute = level & 0x40;
1779 new_level = level = level & 0xf;
1780
1781 if (strlencmp(cmd, "up") == 0) {
1782 if (mute)
1783 new_mute = 0;
1784 else
1785 new_level = level == 15 ? 15 : level + 1;
1786 } else if (strlencmp(cmd, "down") == 0) {
1787 if (mute)
1788 new_mute = 0;
1789 else
1790 new_level = level == 0 ? 0 : level - 1;
1791 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
1792 new_level >= 0 && new_level <= 15) {
1793 /* new_level set */
1794 } else if (strlencmp(cmd, "mute") == 0) {
1795 new_mute = 0x40;
1796 } else
1797 return -EINVAL;
1798
1799 if (new_level != level) { /* mute doesn't change */
1800 cmos_cmd = new_level > level ? VOLUME_UP : VOLUME_DOWN;
1801 inc = new_level > level ? 1 : -1;
1802
1803 if (mute && (!cmos_eval(cmos_cmd) ||
1804 !acpi_ec_write(volume_offset, level)))
1805 return -EIO;
1806
1807 for (i = level; i != new_level; i += inc)
1808 if (!cmos_eval(cmos_cmd) ||
1809 !acpi_ec_write(volume_offset, i + inc))
1810 return -EIO;
1811
1812 if (mute && (!cmos_eval(VOLUME_MUTE) ||
1813 !acpi_ec_write(volume_offset,
1814 new_level + mute)))
1815 return -EIO;
1816 }
1817
1818 if (new_mute != mute) { /* level doesn't change */
1819 cmos_cmd = new_mute ? VOLUME_MUTE : VOLUME_UP;
1820
1821 if (!cmos_eval(cmos_cmd) ||
1822 !acpi_ec_write(volume_offset, level + new_mute))
1823 return -EIO;
1824 }
1825 }
1826
1827 return 0;
1828}
1829
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001830static enum fan_status_access_mode fan_status_access_mode;
1831static enum fan_control_access_mode fan_control_access_mode;
1832static enum fan_control_commands fan_control_commands;
1833
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02001834static int fan_control_status_known;
1835static u8 fan_control_initial_status;
1836
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02001837static void fan_watchdog_fire(void *ignored);
1838static int fan_watchdog_maxinterval;
1839static DECLARE_WORK(fan_watchdog_task, fan_watchdog_fire, NULL);
1840
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001841static int fan_init(void)
1842{
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001843 fan_status_access_mode = IBMACPI_FAN_NONE;
1844 fan_control_access_mode = IBMACPI_FAN_WR_NONE;
1845 fan_control_commands = 0;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02001846 fan_control_status_known = 1;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02001847 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001848
1849 if (gfan_handle) {
1850 /* 570, 600e/x, 770e, 770x */
1851 fan_status_access_mode = IBMACPI_FAN_RD_ACPI_GFAN;
1852 } else {
1853 /* all other ThinkPads: note that even old-style
1854 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02001855 if (likely(acpi_ec_read(fan_status_offset,
1856 &fan_control_initial_status))) {
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001857 fan_status_access_mode = IBMACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02001858
1859 /* In some ThinkPads, neither the EC nor the ACPI
1860 * DSDT initialize the fan status, and it ends up
1861 * being set to 0x07 when it *could* be either
1862 * 0x07 or 0x80.
1863 *
1864 * Enable for TP-1Y (T43), TP-78 (R51e),
1865 * TP-76 (R52), TP-70 (T43, R52), which are known
1866 * to be buggy. */
1867 if (fan_control_initial_status == 0x07 &&
1868 ibm_thinkpad_ec_found &&
1869 ((ibm_thinkpad_ec_found[0] == '1' &&
1870 ibm_thinkpad_ec_found[1] == 'Y') ||
1871 (ibm_thinkpad_ec_found[0] == '7' &&
1872 (ibm_thinkpad_ec_found[1] == '6' ||
1873 ibm_thinkpad_ec_found[1] == '8' ||
1874 ibm_thinkpad_ec_found[1] == '0'))
1875 )) {
1876 printk(IBM_NOTICE
1877 "fan_init: initial fan status is "
1878 "unknown, assuming it is in auto "
1879 "mode\n");
1880 fan_control_status_known = 0;
1881 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001882 } else {
1883 printk(IBM_ERR
1884 "ThinkPad ACPI EC access misbehaving, "
1885 "fan status and control unavailable\n");
1886 return 0;
1887 }
1888 }
1889
1890 if (sfan_handle) {
1891 /* 570, 770x-JL */
1892 fan_control_access_mode = IBMACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02001893 fan_control_commands |=
1894 IBMACPI_FAN_CMD_LEVEL | IBMACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001895 } else {
1896 if (!gfan_handle) {
1897 /* gfan without sfan means no fan control */
1898 /* all other models implement TP EC 0x2f control */
1899
1900 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02001901 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001902 fan_control_access_mode =
1903 IBMACPI_FAN_WR_ACPI_FANS;
1904 fan_control_commands |=
1905 IBMACPI_FAN_CMD_SPEED |
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02001906 IBMACPI_FAN_CMD_LEVEL |
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001907 IBMACPI_FAN_CMD_ENABLE;
1908 } else {
1909 fan_control_access_mode = IBMACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02001910 fan_control_commands |=
1911 IBMACPI_FAN_CMD_LEVEL |
1912 IBMACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001913 }
1914 }
1915 }
1916
1917 return 0;
1918}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001919
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02001920static int fan_get_status(u8 *status)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001921{
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02001922 u8 s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001923
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02001924 /* TODO:
1925 * Add IBMACPI_FAN_RD_ACPI_FANS ? */
1926
Henrique de Moraes Holschuh3ef8a602006-11-24 11:47:10 -02001927 switch (fan_status_access_mode) {
1928 case IBMACPI_FAN_RD_ACPI_GFAN:
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001929 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02001930
1931 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001932 return -EIO;
1933
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02001934 if (likely(status))
1935 *status = s & 0x07;
1936
1937 break;
1938
1939 case IBMACPI_FAN_RD_TPEC:
1940 /* all except 570, 600e/x, 770e, 770x */
1941 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
1942 return -EIO;
1943
1944 if (likely(status))
1945 *status = s;
1946
1947 break;
1948
1949 default:
1950 return -ENXIO;
1951 }
1952
1953 return 0;
1954}
1955
1956static int fan_get_speed(unsigned int *speed)
1957{
1958 u8 hi, lo;
1959
1960 switch (fan_status_access_mode) {
1961 case IBMACPI_FAN_RD_TPEC:
1962 /* all except 570, 600e/x, 770e, 770x */
1963 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
1964 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
1965 return -EIO;
1966
1967 if (likely(speed))
1968 *speed = (hi << 8) | lo;
1969
1970 break;
1971
1972 default:
1973 return -ENXIO;
1974 }
1975
1976 return 0;
1977}
1978
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02001979static void fan_exit(void)
1980{
1981 cancel_delayed_work(&fan_watchdog_task);
1982 flush_scheduled_work();
1983}
1984
1985static void fan_watchdog_reset(void)
1986{
1987 static int fan_watchdog_active = 0;
1988
1989 if (fan_watchdog_active)
1990 cancel_delayed_work(&fan_watchdog_task);
1991
1992 if (fan_watchdog_maxinterval > 0) {
1993 fan_watchdog_active = 1;
1994 if (!schedule_delayed_work(&fan_watchdog_task,
1995 msecs_to_jiffies(fan_watchdog_maxinterval
1996 * 1000))) {
1997 printk(IBM_ERR "failed to schedule the fan watchdog, "
1998 "watchdog will not trigger\n");
1999 }
2000 } else
2001 fan_watchdog_active = 0;
2002}
2003
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02002004static int fan_read(char *p)
2005{
2006 int len = 0;
2007 int rc;
2008 u8 status;
2009 unsigned int speed = 0;
2010
2011 switch (fan_status_access_mode) {
2012 case IBMACPI_FAN_RD_ACPI_GFAN:
2013 /* 570, 600e/x, 770e, 770x */
2014 if ((rc = fan_get_status(&status)) < 0)
2015 return rc;
2016
Henrique de Moraes Holschuhbab812a2006-11-24 11:47:12 -02002017 len += sprintf(p + len, "status:\t\t%s\n"
2018 "level:\t\t%d\n",
2019 (status != 0) ? "enabled" : "disabled", status);
Henrique de Moraes Holschuh3ef8a602006-11-24 11:47:10 -02002020 break;
2021
2022 case IBMACPI_FAN_RD_TPEC:
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002023 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02002024 if ((rc = fan_get_status(&status)) < 0)
2025 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002026
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002027 if (unlikely(!fan_control_status_known)) {
2028 if (status != fan_control_initial_status)
2029 fan_control_status_known = 1;
2030 else
2031 /* Return most likely status. In fact, it
2032 * might be the only possible status */
2033 status = IBMACPI_FAN_EC_AUTO;
2034 }
2035
Henrique de Moraes Holschuhbab812a2006-11-24 11:47:12 -02002036 len += sprintf(p + len, "status:\t\t%s\n",
2037 (status != 0) ? "enabled" : "disabled");
Henrique de Moraes Holschuh3ef8a602006-11-24 11:47:10 -02002038
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002039 /* No ThinkPad boots on disengaged mode, we can safely
2040 * assume the tachometer is online if fan control status
2041 * was unknown */
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02002042 if ((rc = fan_get_speed(&speed)) < 0)
2043 return rc;
2044
2045 len += sprintf(p + len, "speed:\t\t%d\n", speed);
Henrique de Moraes Holschuhbab812a2006-11-24 11:47:12 -02002046
2047 if (status & IBMACPI_FAN_EC_DISENGAGED)
2048 /* Disengaged mode takes precedence */
2049 len += sprintf(p + len, "level:\t\tdisengaged\n");
2050 else if (status & IBMACPI_FAN_EC_AUTO)
2051 len += sprintf(p + len, "level:\t\tauto\n");
2052 else
2053 len += sprintf(p + len, "level:\t\t%d\n", status);
Henrique de Moraes Holschuh3ef8a602006-11-24 11:47:10 -02002054 break;
2055
2056 case IBMACPI_FAN_NONE:
2057 default:
2058 len += sprintf(p + len, "status:\t\tnot supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002059 }
2060
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02002061 if (fan_control_commands & IBMACPI_FAN_CMD_LEVEL) {
2062 len += sprintf(p + len, "commands:\tlevel <level>");
2063
2064 switch (fan_control_access_mode) {
2065 case IBMACPI_FAN_WR_ACPI_SFAN:
2066 len += sprintf(p + len, " (<level> is 0-7)\n");
2067 break;
2068
2069 default:
2070 len += sprintf(p + len, " (<level> is 0-7, "
2071 "auto, disengaged)\n");
2072 break;
2073 }
2074 }
Henrique de Moraes Holschuh3ef8a602006-11-24 11:47:10 -02002075
2076 if (fan_control_commands & IBMACPI_FAN_CMD_ENABLE)
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002077 len += sprintf(p + len, "commands:\tenable, disable\n"
2078 "commands:\twatchdog <timeout> (<timeout> is 0 (off), "
2079 "1-120 (seconds))\n");
Henrique de Moraes Holschuh3ef8a602006-11-24 11:47:10 -02002080
2081 if (fan_control_commands & IBMACPI_FAN_CMD_SPEED)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002082 len += sprintf(p + len, "commands:\tspeed <speed>"
2083 " (<speed> is 0-65535)\n");
2084
2085 return len;
2086}
2087
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002088static int fan_set_level(int level)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002089{
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002090 switch (fan_control_access_mode) {
2091 case IBMACPI_FAN_WR_ACPI_SFAN:
2092 if (level >= 0 && level <= 7) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002093 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
2094 return -EIO;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002095 } else
2096 return -EINVAL;
2097 break;
2098
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02002099 case IBMACPI_FAN_WR_ACPI_FANS:
2100 case IBMACPI_FAN_WR_TPEC:
2101 if ((level != IBMACPI_FAN_EC_AUTO) &&
2102 (level != IBMACPI_FAN_EC_DISENGAGED) &&
2103 ((level < 0) || (level > 7)))
2104 return -EINVAL;
2105
2106 if (!acpi_ec_write(fan_status_offset, level))
2107 return -EIO;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002108 else
2109 fan_control_status_known = 1;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02002110 break;
2111
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002112 default:
2113 return -ENXIO;
2114 }
2115 return 0;
2116}
2117
2118static int fan_set_enable(void)
2119{
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002120 u8 s;
2121 int rc;
2122
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002123 switch (fan_control_access_mode) {
2124 case IBMACPI_FAN_WR_ACPI_FANS:
2125 case IBMACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002126 if ((rc = fan_get_status(&s)) < 0)
2127 return rc;
2128
2129 /* Don't go out of emergency fan mode */
2130 if (s != 7)
2131 s = IBMACPI_FAN_EC_AUTO;
2132
2133 if (!acpi_ec_write(fan_status_offset, s))
2134 return -EIO;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002135 else
2136 fan_control_status_known = 1;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002137 break;
2138
2139 case IBMACPI_FAN_WR_ACPI_SFAN:
2140 if ((rc = fan_get_status(&s)) < 0)
2141 return rc;
2142
2143 s &= 0x07;
2144
2145 /* Set fan to at least level 4 */
2146 if (s < 4)
2147 s = 4;
2148
2149 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002150 return -EIO;
2151 break;
2152
2153 default:
2154 return -ENXIO;
2155 }
2156 return 0;
2157}
2158
2159static int fan_set_disable(void)
2160{
2161 switch (fan_control_access_mode) {
2162 case IBMACPI_FAN_WR_ACPI_FANS:
2163 case IBMACPI_FAN_WR_TPEC:
2164 if (!acpi_ec_write(fan_status_offset, 0x00))
2165 return -EIO;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002166 else
2167 fan_control_status_known = 1;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002168 break;
2169
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002170 case IBMACPI_FAN_WR_ACPI_SFAN:
2171 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
2172 return -EIO;
2173 break;
2174
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002175 default:
2176 return -ENXIO;
2177 }
2178 return 0;
2179}
2180
2181static int fan_set_speed(int speed)
2182{
2183 switch (fan_control_access_mode) {
2184 case IBMACPI_FAN_WR_ACPI_FANS:
2185 if (speed >= 0 && speed <= 65535) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002186 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
2187 speed, speed, speed))
2188 return -EIO;
2189 } else
2190 return -EINVAL;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002191 break;
2192
2193 default:
2194 return -ENXIO;
2195 }
2196 return 0;
2197}
2198
2199static int fan_write_cmd_level(const char *cmd, int *rc)
2200{
2201 int level;
2202
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02002203 if (strlencmp(cmd, "level auto") == 0)
2204 level = IBMACPI_FAN_EC_AUTO;
2205 else if (strlencmp(cmd, "level disengaged") == 0)
2206 level = IBMACPI_FAN_EC_DISENGAGED;
2207 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002208 return 0;
2209
2210 if ((*rc = fan_set_level(level)) == -ENXIO)
2211 printk(IBM_ERR "level command accepted for unsupported "
2212 "access mode %d", fan_control_access_mode);
2213
2214 return 1;
2215}
2216
2217static int fan_write_cmd_enable(const char *cmd, int *rc)
2218{
2219 if (strlencmp(cmd, "enable") != 0)
2220 return 0;
2221
2222 if ((*rc = fan_set_enable()) == -ENXIO)
2223 printk(IBM_ERR "enable command accepted for unsupported "
2224 "access mode %d", fan_control_access_mode);
2225
2226 return 1;
2227}
2228
2229static int fan_write_cmd_disable(const char *cmd, int *rc)
2230{
2231 if (strlencmp(cmd, "disable") != 0)
2232 return 0;
2233
2234 if ((*rc = fan_set_disable()) == -ENXIO)
2235 printk(IBM_ERR "disable command accepted for unsupported "
2236 "access mode %d", fan_control_access_mode);
2237
2238 return 1;
2239}
2240
2241static int fan_write_cmd_speed(const char *cmd, int *rc)
2242{
2243 int speed;
2244
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02002245 /* TODO:
2246 * Support speed <low> <medium> <high> ? */
2247
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002248 if (sscanf(cmd, "speed %d", &speed) != 1)
2249 return 0;
2250
2251 if ((*rc = fan_set_speed(speed)) == -ENXIO)
2252 printk(IBM_ERR "speed command accepted for unsupported "
2253 "access mode %d", fan_control_access_mode);
2254
2255 return 1;
2256}
2257
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002258static int fan_write_cmd_watchdog(const char *cmd, int *rc)
2259{
2260 int interval;
2261
2262 if (sscanf(cmd, "watchdog %d", &interval) != 1)
2263 return 0;
2264
2265 if (interval < 0 || interval > 120)
2266 *rc = -EINVAL;
2267 else
2268 fan_watchdog_maxinterval = interval;
2269
2270 return 1;
2271}
2272
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002273static int fan_write(char *buf)
2274{
2275 char *cmd;
2276 int rc = 0;
2277
2278 while (!rc && (cmd = next_cmd(&buf))) {
2279 if (!((fan_control_commands & IBMACPI_FAN_CMD_LEVEL) &&
2280 fan_write_cmd_level(cmd, &rc)) &&
2281 !((fan_control_commands & IBMACPI_FAN_CMD_ENABLE) &&
2282 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002283 fan_write_cmd_disable(cmd, &rc) ||
2284 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002285 !((fan_control_commands & IBMACPI_FAN_CMD_SPEED) &&
2286 fan_write_cmd_speed(cmd, &rc))
2287 )
2288 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002289 else if (!rc)
2290 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002291 }
2292
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002293 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002294}
2295
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002296static void fan_watchdog_fire(void *ignored)
2297{
2298 printk(IBM_NOTICE "fan watchdog: enabling fan\n");
2299 if (fan_set_enable()) {
2300 printk(IBM_ERR "fan watchdog: error while enabling fan\n");
2301 /* reschedule for later */
2302 fan_watchdog_reset();
2303 }
2304}
2305
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306static struct ibm_struct ibms[] = {
2307 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002308 .name = "driver",
2309 .init = driver_init,
2310 .read = driver_read,
2311 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002313 .name = "hotkey",
2314 .hid = IBM_HKEY_HID,
2315 .init = hotkey_init,
2316 .read = hotkey_read,
2317 .write = hotkey_write,
2318 .exit = hotkey_exit,
2319 .notify = hotkey_notify,
2320 .handle = &hkey_handle,
2321 .type = ACPI_DEVICE_NOTIFY,
2322 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002324 .name = "bluetooth",
2325 .init = bluetooth_init,
2326 .read = bluetooth_read,
2327 .write = bluetooth_write,
2328 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 {
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002330 .name = "wan",
2331 .init = wan_init,
2332 .read = wan_read,
2333 .write = wan_write,
2334 .experimental = 1,
2335 },
2336 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002337 .name = "video",
2338 .init = video_init,
2339 .read = video_read,
2340 .write = video_write,
2341 .exit = video_exit,
2342 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002344 .name = "light",
2345 .init = light_init,
2346 .read = light_read,
2347 .write = light_write,
2348 },
Kristen Accardi63e5f242006-02-23 17:56:06 -08002349#ifdef CONFIG_ACPI_IBM_DOCK
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002351 .name = "dock",
2352 .read = dock_read,
2353 .write = dock_write,
2354 .notify = dock_notify,
2355 .handle = &dock_handle,
2356 .type = ACPI_SYSTEM_NOTIFY,
2357 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002359 .name = "dock",
2360 .hid = IBM_PCI_HID,
2361 .notify = dock_notify,
2362 .handle = &pci_handle,
2363 .type = ACPI_SYSTEM_NOTIFY,
2364 },
Kristen Accardi63e5f242006-02-23 17:56:06 -08002365#endif
Henrique de Moraes Holschuh2df910b2006-11-25 16:35:09 -02002366#ifdef CONFIG_ACPI_IBM_BAY
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002368 .name = "bay",
2369 .init = bay_init,
2370 .read = bay_read,
2371 .write = bay_write,
2372 .notify = bay_notify,
2373 .handle = &bay_handle,
2374 .type = ACPI_SYSTEM_NOTIFY,
2375 },
Henrique de Moraes Holschuh2df910b2006-11-25 16:35:09 -02002376#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002378 .name = "cmos",
2379 .read = cmos_read,
2380 .write = cmos_write,
2381 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002383 .name = "led",
2384 .init = led_init,
2385 .read = led_read,
2386 .write = led_write,
2387 },
2388 {
2389 .name = "beep",
2390 .read = beep_read,
2391 .write = beep_write,
2392 },
2393 {
2394 .name = "thermal",
2395 .init = thermal_init,
2396 .read = thermal_read,
2397 },
2398 {
2399 .name = "ecdump",
2400 .read = ecdump_read,
2401 .write = ecdump_write,
2402 .experimental = 1,
2403 },
2404 {
2405 .name = "brightness",
2406 .read = brightness_read,
2407 .write = brightness_write,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02002408 .init = brightness_init,
2409 .exit = brightness_exit,
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002410 },
2411 {
2412 .name = "volume",
2413 .read = volume_read,
2414 .write = volume_write,
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002415 },
2416 {
2417 .name = "fan",
2418 .read = fan_read,
2419 .write = fan_write,
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002420 .init = fan_init,
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002421 .exit = fan_exit,
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002422 .experimental = 1,
2423 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425
2426static int dispatch_read(char *page, char **start, off_t off, int count,
2427 int *eof, void *data)
2428{
2429 struct ibm_struct *ibm = (struct ibm_struct *)data;
2430 int len;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002431
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 if (!ibm || !ibm->read)
2433 return -EINVAL;
2434
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002435 len = ibm->read(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 if (len < 0)
2437 return len;
2438
2439 if (len <= off + count)
2440 *eof = 1;
2441 *start = page + off;
2442 len -= off;
2443 if (len > count)
2444 len = count;
2445 if (len < 0)
2446 len = 0;
2447
2448 return len;
2449}
2450
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002451static int dispatch_write(struct file *file, const char __user * userbuf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 unsigned long count, void *data)
2453{
2454 struct ibm_struct *ibm = (struct ibm_struct *)data;
2455 char *kernbuf;
2456 int ret;
2457
2458 if (!ibm || !ibm->write)
2459 return -EINVAL;
2460
2461 kernbuf = kmalloc(count + 2, GFP_KERNEL);
2462 if (!kernbuf)
2463 return -ENOMEM;
2464
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002465 if (copy_from_user(kernbuf, userbuf, count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 kfree(kernbuf);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002467 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 }
2469
2470 kernbuf[count] = 0;
2471 strcat(kernbuf, ",");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002472 ret = ibm->write(kernbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 if (ret == 0)
2474 ret = count;
2475
2476 kfree(kernbuf);
2477
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002478 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479}
2480
2481static void dispatch_notify(acpi_handle handle, u32 event, void *data)
2482{
2483 struct ibm_struct *ibm = (struct ibm_struct *)data;
2484
2485 if (!ibm || !ibm->notify)
2486 return;
2487
2488 ibm->notify(ibm, event);
2489}
2490
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002491static int __init setup_notify(struct ibm_struct *ibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492{
2493 acpi_status status;
2494 int ret;
2495
2496 if (!*ibm->handle)
2497 return 0;
2498
2499 ret = acpi_bus_get_device(*ibm->handle, &ibm->device);
2500 if (ret < 0) {
2501 printk(IBM_ERR "%s device not present\n", ibm->name);
2502 return 0;
2503 }
2504
2505 acpi_driver_data(ibm->device) = ibm;
2506 sprintf(acpi_device_class(ibm->device), "%s/%s", IBM_NAME, ibm->name);
2507
2508 status = acpi_install_notify_handler(*ibm->handle, ibm->type,
2509 dispatch_notify, ibm);
2510 if (ACPI_FAILURE(status)) {
2511 printk(IBM_ERR "acpi_install_notify_handler(%s) failed: %d\n",
2512 ibm->name, status);
2513 return -ENODEV;
2514 }
2515
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 return 0;
2517}
2518
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002519static int __init ibm_device_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520{
2521 return 0;
2522}
2523
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002524static int __init register_driver(struct ibm_struct *ibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525{
2526 int ret;
2527
2528 ibm->driver = kmalloc(sizeof(struct acpi_driver), GFP_KERNEL);
2529 if (!ibm->driver) {
2530 printk(IBM_ERR "kmalloc(ibm->driver) failed\n");
2531 return -1;
2532 }
2533
2534 memset(ibm->driver, 0, sizeof(struct acpi_driver));
Henrique de Moraes Holschuh3dfd35c2006-11-24 10:32:32 -02002535 sprintf(ibm->driver->name, "%s_%s", IBM_NAME, ibm->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 ibm->driver->ids = ibm->hid;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002537 ibm->driver->ops.add = &ibm_device_add;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538
2539 ret = acpi_bus_register_driver(ibm->driver);
2540 if (ret < 0) {
2541 printk(IBM_ERR "acpi_bus_register_driver(%s) failed: %d\n",
2542 ibm->hid, ret);
2543 kfree(ibm->driver);
2544 }
2545
2546 return ret;
2547}
2548
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002549static int __init ibm_init(struct ibm_struct *ibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550{
2551 int ret;
2552 struct proc_dir_entry *entry;
2553
2554 if (ibm->experimental && !experimental)
2555 return 0;
2556
2557 if (ibm->hid) {
2558 ret = register_driver(ibm);
2559 if (ret < 0)
2560 return ret;
2561 ibm->driver_registered = 1;
2562 }
2563
2564 if (ibm->init) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002565 ret = ibm->init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 if (ret != 0)
2567 return ret;
2568 ibm->init_called = 1;
2569 }
2570
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002571 if (ibm->read) {
2572 entry = create_proc_entry(ibm->name,
2573 S_IFREG | S_IRUGO | S_IWUSR,
2574 proc_dir);
2575 if (!entry) {
2576 printk(IBM_ERR "unable to create proc entry %s\n",
2577 ibm->name);
2578 return -ENODEV;
2579 }
2580 entry->owner = THIS_MODULE;
2581 entry->data = ibm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 entry->read_proc = &dispatch_read;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002583 if (ibm->write)
2584 entry->write_proc = &dispatch_write;
2585 ibm->proc_created = 1;
2586 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587
2588 if (ibm->notify) {
2589 ret = setup_notify(ibm);
2590 if (ret < 0)
2591 return ret;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002592 ibm->notify_installed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 }
2594
2595 return 0;
2596}
2597
2598static void ibm_exit(struct ibm_struct *ibm)
2599{
2600 if (ibm->notify_installed)
2601 acpi_remove_notify_handler(*ibm->handle, ibm->type,
2602 dispatch_notify);
2603
2604 if (ibm->proc_created)
2605 remove_proc_entry(ibm->name, proc_dir);
2606
2607 if (ibm->init_called && ibm->exit)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002608 ibm->exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609
2610 if (ibm->driver_registered) {
2611 acpi_bus_unregister_driver(ibm->driver);
2612 kfree(ibm->driver);
2613 }
2614}
2615
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002616static void __init ibm_handle_init(char *name,
2617 acpi_handle * handle, acpi_handle parent,
2618 char **paths, int num_paths, char **path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619{
2620 int i;
2621 acpi_status status;
2622
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002623 for (i = 0; i < num_paths; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 status = acpi_get_handle(parent, paths[i], handle);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002625 if (ACPI_SUCCESS(status)) {
2626 *path = paths[i];
2627 return;
2628 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002630
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 *handle = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632}
2633
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002634#define IBM_HANDLE_INIT(object) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 ibm_handle_init(#object, &object##_handle, *object##_parent, \
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002636 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637
2638static int set_ibm_param(const char *val, struct kernel_param *kp)
2639{
2640 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002642 for (i = 0; i < ARRAY_SIZE(ibms); i++)
2643 if (strcmp(ibms[i].name, kp->name) == 0 && ibms[i].write) {
2644 if (strlen(val) > sizeof(ibms[i].param) - 2)
2645 return -ENOSPC;
2646 strcpy(ibms[i].param, val);
2647 strcat(ibms[i].param, ",");
2648 return 0;
2649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 return -EINVAL;
2652}
2653
2654#define IBM_PARAM(feature) \
2655 module_param_call(feature, set_ibm_param, NULL, NULL, 0)
2656
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002657IBM_PARAM(hotkey);
2658IBM_PARAM(bluetooth);
2659IBM_PARAM(video);
2660IBM_PARAM(light);
Kristen Accardi63e5f242006-02-23 17:56:06 -08002661#ifdef CONFIG_ACPI_IBM_DOCK
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002662IBM_PARAM(dock);
Kristen Accardi63e5f242006-02-23 17:56:06 -08002663#endif
Henrique de Moraes Holschuh2df910b2006-11-25 16:35:09 -02002664#ifdef CONFIG_ACPI_IBM_BAY
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002665IBM_PARAM(bay);
Henrique de Moraes Holschuh2df910b2006-11-25 16:35:09 -02002666#endif
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002667IBM_PARAM(cmos);
2668IBM_PARAM(led);
2669IBM_PARAM(beep);
2670IBM_PARAM(ecdump);
2671IBM_PARAM(brightness);
2672IBM_PARAM(volume);
2673IBM_PARAM(fan);
2674
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675static void acpi_ibm_exit(void)
2676{
2677 int i;
2678
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002679 for (i = ARRAY_SIZE(ibms) - 1; i >= 0; i--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 ibm_exit(&ibms[i]);
2681
2682 remove_proc_entry(IBM_DIR, acpi_root_dir);
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02002683
2684 if (ibm_thinkpad_ec_found)
2685 kfree(ibm_thinkpad_ec_found);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686}
2687
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02002688static char* __init check_dmi_for_ec(void)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002689{
2690 struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02002691 char ec_fw_string[18];
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002692
2693 /*
2694 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
2695 * X32 or newer, all Z series; Some models must have an
2696 * up-to-date BIOS or they will not be detected.
2697 *
2698 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
2699 */
2700 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02002701 if (sscanf(dev->name,
2702 "IBM ThinkPad Embedded Controller -[%17c",
2703 ec_fw_string) == 1) {
2704 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
2705 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
2706 return kstrdup(ec_fw_string, GFP_KERNEL);
2707 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002708 }
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02002709 return NULL;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002710}
2711
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712static int __init acpi_ibm_init(void)
2713{
2714 int ret, i;
2715
2716 if (acpi_disabled)
2717 return -ENODEV;
2718
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002719 if (!acpi_specific_hotkey_enabled) {
2720 printk(IBM_ERR "using generic hotkey driver\n");
2721 return -ENODEV;
Luming Yufb9802f2005-03-18 18:03:45 -05002722 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002724 /* ec is required because many other handles are relative to it */
2725 IBM_HANDLE_INIT(ec);
2726 if (!ec_handle) {
2727 printk(IBM_ERR "ec object not found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 return -ENODEV;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002731 /* Models with newer firmware report the EC in DMI */
2732 ibm_thinkpad_ec_found = check_dmi_for_ec();
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02002733 if (ibm_thinkpad_ec_found)
2734 printk(IBM_INFO "ThinkPad EC firmware %s\n",
2735 ibm_thinkpad_ec_found);
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002736
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 /* these handles are not required */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002738 IBM_HANDLE_INIT(vid);
2739 IBM_HANDLE_INIT(vid2);
2740 IBM_HANDLE_INIT(ledb);
2741 IBM_HANDLE_INIT(led);
2742 IBM_HANDLE_INIT(hkey);
2743 IBM_HANDLE_INIT(lght);
2744 IBM_HANDLE_INIT(cmos);
Kristen Accardi63e5f242006-02-23 17:56:06 -08002745#ifdef CONFIG_ACPI_IBM_DOCK
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002746 IBM_HANDLE_INIT(dock);
Kristen Accardi63e5f242006-02-23 17:56:06 -08002747#endif
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002748 IBM_HANDLE_INIT(pci);
Henrique de Moraes Holschuh2df910b2006-11-25 16:35:09 -02002749#ifdef CONFIG_ACPI_IBM_BAY
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002750 IBM_HANDLE_INIT(bay);
2751 if (bay_handle)
2752 IBM_HANDLE_INIT(bay_ej);
2753 IBM_HANDLE_INIT(bay2);
2754 if (bay2_handle)
2755 IBM_HANDLE_INIT(bay2_ej);
Henrique de Moraes Holschuh2df910b2006-11-25 16:35:09 -02002756#endif
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002757 IBM_HANDLE_INIT(beep);
2758 IBM_HANDLE_INIT(ecrd);
2759 IBM_HANDLE_INIT(ecwr);
2760 IBM_HANDLE_INIT(fans);
2761 IBM_HANDLE_INIT(gfan);
2762 IBM_HANDLE_INIT(sfan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763
2764 proc_dir = proc_mkdir(IBM_DIR, acpi_root_dir);
2765 if (!proc_dir) {
2766 printk(IBM_ERR "unable to create proc dir %s", IBM_DIR);
2767 return -ENODEV;
2768 }
2769 proc_dir->owner = THIS_MODULE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002770
2771 for (i = 0; i < ARRAY_SIZE(ibms); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 ret = ibm_init(&ibms[i]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002773 if (ret >= 0 && *ibms[i].param)
2774 ret = ibms[i].write(ibms[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 if (ret < 0) {
2776 acpi_ibm_exit();
2777 return ret;
2778 }
2779 }
2780
2781 return 0;
2782}
2783
2784module_init(acpi_ibm_init);
2785module_exit(acpi_ibm_exit);