blob: e7309a668732b257271ec3b9f6e176c9c7ef408d [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>
Henrique de Moraes Holschuhf9ff43a2006-11-25 16:37:38 -02006 * Copyright (C) 2006 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Borislav Deianov78f81cc2005-08-17 00:00:00 -040021 */
22
Henrique de Moraes Holschuhf9ff43a2006-11-25 16:37:38 -020023#define IBM_VERSION "0.13"
Borislav Deianov78f81cc2005-08-17 00:00:00 -040024
25/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 * Changelog:
Henrique de Moraes Holschuhf9ff43a2006-11-25 16:37:38 -020027 *
28 * 2006-11-22 0.13 new maintainer
29 * changelog now lives in git commit history, and will
30 * not be updated further in-file.
Borislav Deianov78f81cc2005-08-17 00:00:00 -040031 *
32 * 2005-08-17 0.12 fix compilation on 2.6.13-rc kernels
33 * 2005-03-17 0.11 support for 600e, 770x
34 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
35 * support for 770e, G41
36 * G40 and G41 don't have a thinklight
37 * temperatures no longer experimental
38 * experimental brightness control
39 * experimental volume control
40 * experimental fan enable/disable
41 * 2005-01-16 0.10 fix module loading on R30, R31
42 * 2005-01-16 0.9 support for 570, R30, R31
43 * ultrabay support on A22p, A3x
44 * limit arg for cmos, led, beep, drop experimental status
45 * more capable led control on A21e, A22p, T20-22, X20
46 * experimental temperatures and fan speed
47 * experimental embedded controller register dump
48 * mark more functions as __init, drop incorrect __exit
49 * use MODULE_VERSION
50 * thanks to Henrik Brix Andersen <brix@gentoo.org>
51 * fix parameter passing on module loading
52 * thanks to Rusty Russell <rusty@rustcorp.com.au>
53 * thanks to Jim Radford <radford@blackbean.org>
54 * 2004-11-08 0.8 fix init error case, don't return from a macro
55 * thanks to Chris Wright <chrisw@osdl.org>
56 * 2004-10-23 0.7 fix module loading on A21e, A22p, T20, T21, X20
57 * fix led control on A21e
58 * 2004-10-19 0.6 use acpi_bus_register_driver() to claim HKEY device
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 * 2004-10-18 0.5 thinklight support on A21e, G40, R32, T20, T21, X20
60 * proc file format changed
61 * video_switch command
62 * experimental cmos control
63 * experimental led control
64 * experimental acpi sounds
Borislav Deianov78f81cc2005-08-17 00:00:00 -040065 * 2004-09-16 0.4 support for module parameters
66 * hotkey mask can be prefixed by 0x
67 * video output switching
68 * video expansion control
69 * ultrabay eject support
70 * removed lcd brightness/on/off control, didn't work
71 * 2004-08-17 0.3 support for R40
72 * lcd off, brightness control
73 * thinklight on/off
74 * 2004-08-14 0.2 support for T series, X20
75 * bluetooth enable/disable
76 * hotkey events disabled by default
77 * removed fan control, currently useless
78 * 2004-08-09 0.1 initial release, support for X series
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 */
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#include <linux/kernel.h>
82#include <linux/module.h>
83#include <linux/init.h>
84#include <linux/types.h>
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -020085#include <linux/string.h>
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -020086
Linus Torvalds1da177e2005-04-16 15:20:36 -070087#include <linux/proc_fs.h>
Holger Macht8acb0252006-10-20 14:30:28 -070088#include <linux/backlight.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070089#include <asm/uaccess.h>
90
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -020091#include <linux/dmi.h>
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -020092#include <linux/jiffies.h>
93#include <linux/workqueue.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95#include <acpi/acpi_drivers.h>
96#include <acpi/acnamesp.h>
97
98#define IBM_NAME "ibm"
99#define IBM_DESC "IBM ThinkPad ACPI Extras"
100#define IBM_FILE "ibm_acpi"
101#define IBM_URL "http://ibm-acpi.sf.net/"
102
Henrique de Moraes Holschuhf9ff43a2006-11-25 16:37:38 -0200103MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400104MODULE_DESCRIPTION(IBM_DESC);
105MODULE_VERSION(IBM_VERSION);
106MODULE_LICENSE("GPL");
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108#define IBM_DIR IBM_NAME
109
110#define IBM_LOG IBM_FILE ": "
111#define IBM_ERR KERN_ERR IBM_LOG
112#define IBM_NOTICE KERN_NOTICE IBM_LOG
113#define IBM_INFO KERN_INFO IBM_LOG
114#define IBM_DEBUG KERN_DEBUG IBM_LOG
115
116#define IBM_MAX_ACPI_ARGS 3
117
118#define __unused __attribute__ ((unused))
119
120static int experimental;
121module_param(experimental, int, 0);
122
123static acpi_handle root_handle = NULL;
124
125#define IBM_HANDLE(object, parent, paths...) \
126 static acpi_handle object##_handle; \
127 static acpi_handle *object##_parent = &parent##_handle; \
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400128 static char *object##_path; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 static char *object##_paths[] = { paths }
130
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400131IBM_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
132 "\\_SB.PCI.ISA.EC", /* 570 */
133 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
134 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
135 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
136 "\\_SB.PCI0.ICH3.EC0", /* R31 */
137 "\\_SB.PCI0.LPC.EC", /* all others */
138 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400140IBM_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
141 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
142 "\\_SB.PCI0.VID0", /* 770e */
143 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
144 "\\_SB.PCI0.AGP.VID", /* all others */
145 ); /* R30, R31 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400147IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400149IBM_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, T4x, X31, X40 */
150 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
151 "\\CMS", /* R40, R40e */
152 ); /* all others */
Kristen Accardi63e5f242006-02-23 17:56:06 -0800153#ifdef CONFIG_ACPI_IBM_DOCK
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400154IBM_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
155 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
156 "\\_SB.PCI0.PCI1.DOCK", /* all others */
157 "\\_SB.PCI.ISA.SLCE", /* 570 */
158 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
Kristen Accardi63e5f242006-02-23 17:56:06 -0800159#endif
Henrique de Moraes Holschuh2bc808a2007-02-21 13:05:38 -0200160#ifdef CONFIG_ACPI_IBM_BAY
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400161IBM_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
162 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhe0298992006-11-25 16:35:09 -0200163 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400164 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
165 ); /* A21e, R30, R31 */
166
167IBM_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
168 "_EJ0", /* all others */
169 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
170
171IBM_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
172 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
173 ); /* all others */
174
175IBM_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
176 "_EJ0", /* 770x */
177 ); /* all others */
Henrique de Moraes Holschuh2bc808a2007-02-21 13:05:38 -0200178#endif /* CONFIG_ACPI_IBM_BAY */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400179
180/* don't list other alternatives as we install a notify handler on the 570 */
181IBM_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
182
183IBM_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
184 "^HKEY", /* R30, R31 */
185 "HKEY", /* all others */
186 ); /* 570 */
187
188IBM_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
189IBM_HANDLE(ledb, ec, "LEDB"); /* G4x */
190
191IBM_HANDLE(led, ec, "SLED", /* 570 */
192 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
193 "LED", /* all others */
194 ); /* R30, R31 */
195
196IBM_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
197IBM_HANDLE(ecrd, ec, "ECRD"); /* 570 */
198IBM_HANDLE(ecwr, ec, "ECWR"); /* 570 */
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200199IBM_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400200
201IBM_HANDLE(gfan, ec, "GFAN", /* 570 */
202 "\\FSPD", /* 600e/x, 770e, 770x */
203 ); /* all others */
204
205IBM_HANDLE(sfan, ec, "SFAN", /* 570 */
206 "JFNS", /* 770x-JL */
207 ); /* all others */
208
209#define IBM_HKEY_HID "IBM0068"
210#define IBM_PCI_HID "PNP0A03"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -0200212enum thermal_access_mode {
213 IBMACPI_THERMAL_NONE = 0, /* No thermal support */
214 IBMACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
215 IBMACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -0200216 IBMACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
217 IBMACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -0200218};
219
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -0200220#define IBMACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -0200221struct ibm_thermal_sensors_struct {
222 s32 temp[IBMACPI_MAX_THERMAL_SENSORS];
223};
224
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200225/*
226 * FAN ACCESS MODES
227 *
228 * IBMACPI_FAN_RD_ACPI_GFAN:
229 * ACPI GFAN method: returns fan level
230 *
231 * see IBMACPI_FAN_WR_ACPI_SFAN
232 * EC 0x2f not available if GFAN exists
233 *
234 * IBMACPI_FAN_WR_ACPI_SFAN:
235 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
236 *
237 * EC 0x2f might be available *for reading*, but never for writing.
238 *
239 * IBMACPI_FAN_WR_TPEC:
240 * ThinkPad EC register 0x2f (HFSP): fan control loop mode Supported
241 * on almost all ThinkPads
242 *
243 * Fan speed changes of any sort (including those caused by the
244 * disengaged mode) are usually done slowly by the firmware as the
245 * maximum ammount of fan duty cycle change per second seems to be
246 * limited.
247 *
248 * Reading is not available if GFAN exists.
249 * Writing is not available if SFAN exists.
250 *
251 * Bits
252 * 7 automatic mode engaged;
253 * (default operation mode of the ThinkPad)
254 * fan level is ignored in this mode.
255 * 6 disengage mode (takes precedence over bit 7);
256 * not available on all thinkpads. May disable
257 * the tachometer, and speeds up fan to 100% duty-cycle,
258 * which speeds it up far above the standard RPM
259 * levels. It is not impossible that it could cause
260 * hardware damage.
261 * 5-3 unused in some models. Extra bits for fan level
262 * in others, but still useless as all values above
263 * 7 map to the same speed as level 7 in these models.
264 * 2-0 fan level (0..7 usually)
265 * 0x00 = stop
266 * 0x07 = max (set when temperatures critical)
267 * Some ThinkPads may have other levels, see
268 * IBMACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
269 *
270 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
271 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
272 * does so, its initial value is meaningless (0x07).
273 *
274 * For firmware bugs, refer to:
275 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
276 *
277 * ----
278 *
279 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
280 * Main fan tachometer reading (in RPM)
281 *
282 * This register is present on all ThinkPads with a new-style EC, and
283 * it is known not to be present on the A21m/e, and T22, as there is
284 * something else in offset 0x84 according to the ACPI DSDT. Other
285 * ThinkPads from this same time period (and earlier) probably lack the
286 * tachometer as well.
287 *
288 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
289 * was never fixed by IBM to report the EC firmware version string
290 * probably support the tachometer (like the early X models), so
291 * detecting it is quite hard. We need more data to know for sure.
292 *
293 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
294 * might result.
295 *
296 * FIRMWARE BUG: when EC 0x2f bit 6 is set (disengaged mode), this
297 * register is not invalidated in ThinkPads that disable tachometer
298 * readings. Thus, the tachometer readings go stale.
299 *
300 * For firmware bugs, refer to:
301 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
302 *
303 * IBMACPI_FAN_WR_ACPI_FANS:
304 * ThinkPad X31, X40, X41. Not available in the X60.
305 *
306 * FANS ACPI handle: takes three arguments: low speed, medium speed,
307 * high speed. ACPI DSDT seems to map these three speeds to levels
308 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
309 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
310 *
311 * The speeds are stored on handles
312 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
313 *
314 * There are three default speed sets, acessible as handles:
315 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
316 *
317 * ACPI DSDT switches which set is in use depending on various
318 * factors.
319 *
320 * IBMACPI_FAN_WR_TPEC is also available and should be used to
321 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
322 * but the ACPI tables just mention level 7.
323 */
324
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -0200325enum fan_status_access_mode {
326 IBMACPI_FAN_NONE = 0, /* No fan status or control */
327 IBMACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
328 IBMACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
329};
330
331enum fan_control_access_mode {
332 IBMACPI_FAN_WR_NONE = 0, /* No fan control */
333 IBMACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
334 IBMACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
335 IBMACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
336};
337
338enum fan_control_commands {
339 IBMACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
340 IBMACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -0200341 IBMACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
342 * and also watchdog cmd */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -0200343};
344
345enum { /* Fan control constants */
346 fan_status_offset = 0x2f, /* EC register 0x2f */
347 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
348 * 0x84 must be read before 0x85 */
Henrique de Moraes Holschuhbab812a2006-11-24 11:47:12 -0200349
350 IBMACPI_FAN_EC_DISENGAGED = 0x40, /* EC mode: tachometer
351 * disengaged */
352 IBMACPI_FAN_EC_AUTO = 0x80, /* EC mode: auto fan
353 * control */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -0200354};
355
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -0200356static char *ibm_thinkpad_ec_found = NULL;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -0200357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358struct ibm_struct {
359 char *name;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400360 char param[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
362 char *hid;
363 struct acpi_driver *driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400365 int (*init) (void);
366 int (*read) (char *);
367 int (*write) (char *);
368 void (*exit) (void);
369
370 void (*notify) (struct ibm_struct *, u32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 acpi_handle *handle;
372 int type;
373 struct acpi_device *device;
374
375 int driver_registered;
376 int proc_created;
377 int init_called;
378 int notify_installed;
379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 int experimental;
381};
382
383static struct proc_dir_entry *proc_dir = NULL;
384
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -0200385static struct backlight_device *ibm_backlight_device = NULL;
Holger Macht8acb0252006-10-20 14:30:28 -0700386
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387#define onoff(status,bit) ((status) & (1 << (bit)) ? "on" : "off")
388#define enabled(status,bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
389#define strlencmp(a,b) (strncmp((a), (b), strlen(b)))
390
391static int acpi_evalf(acpi_handle handle,
392 void *res, char *method, char *fmt, ...)
393{
394 char *fmt0 = fmt;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400395 struct acpi_object_list params;
396 union acpi_object in_objs[IBM_MAX_ACPI_ARGS];
397 struct acpi_buffer result, *resultp;
398 union acpi_object out_obj;
399 acpi_status status;
400 va_list ap;
401 char res_type;
402 int success;
403 int quiet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
405 if (!*fmt) {
406 printk(IBM_ERR "acpi_evalf() called with empty format\n");
407 return 0;
408 }
409
410 if (*fmt == 'q') {
411 quiet = 1;
412 fmt++;
413 } else
414 quiet = 0;
415
416 res_type = *(fmt++);
417
418 params.count = 0;
419 params.pointer = &in_objs[0];
420
421 va_start(ap, fmt);
422 while (*fmt) {
423 char c = *(fmt++);
424 switch (c) {
425 case 'd': /* int */
426 in_objs[params.count].integer.value = va_arg(ap, int);
427 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
428 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400429 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 default:
431 printk(IBM_ERR "acpi_evalf() called "
432 "with invalid format character '%c'\n", c);
433 return 0;
434 }
435 }
436 va_end(ap);
437
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400438 if (res_type != 'v') {
439 result.length = sizeof(out_obj);
440 result.pointer = &out_obj;
441 resultp = &result;
442 } else
443 resultp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400445 status = acpi_evaluate_object(handle, method, &params, resultp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
447 switch (res_type) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400448 case 'd': /* int */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 if (res)
450 *(int *)res = out_obj.integer.value;
451 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
452 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400453 case 'v': /* void */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 success = status == AE_OK;
455 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400456 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 default:
458 printk(IBM_ERR "acpi_evalf() called "
459 "with invalid format character '%c'\n", res_type);
460 return 0;
461 }
462
463 if (!success && !quiet)
464 printk(IBM_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
465 method, fmt0, status);
466
467 return success;
468}
469
470static void __unused acpi_print_int(acpi_handle handle, char *method)
471{
472 int i;
473
474 if (acpi_evalf(handle, &i, method, "d"))
475 printk(IBM_INFO "%s = 0x%x\n", method, i);
476 else
477 printk(IBM_ERR "error calling %s\n", method);
478}
479
480static char *next_cmd(char **cmds)
481{
482 char *start = *cmds;
483 char *end;
484
485 while ((end = strchr(start, ',')) && end == start)
486 start = end + 1;
487
488 if (!end)
489 return NULL;
490
491 *end = 0;
492 *cmds = end + 1;
493 return start;
494}
495
Adrian Bunk1f217822006-12-19 13:01:28 -0800496static int ibm_acpi_driver_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
498 printk(IBM_INFO "%s v%s\n", IBM_DESC, IBM_VERSION);
499 printk(IBM_INFO "%s\n", IBM_URL);
500
Henrique de Moraes Holschuh3945ac32007-02-06 19:13:44 -0200501 if (ibm_thinkpad_ec_found)
502 printk(IBM_INFO "ThinkPad EC firmware %s\n",
503 ibm_thinkpad_ec_found);
504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 return 0;
506}
507
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400508static int driver_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509{
510 int len = 0;
511
512 len += sprintf(p + len, "driver:\t\t%s\n", IBM_DESC);
513 len += sprintf(p + len, "version:\t%s\n", IBM_VERSION);
514
515 return len;
516}
517
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400518static int hotkey_supported;
519static int hotkey_mask_supported;
520static int hotkey_orig_status;
521static int hotkey_orig_mask;
522
523static int hotkey_get(int *status, int *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524{
525 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400526 return 0;
527
528 if (hotkey_mask_supported)
529 if (!acpi_evalf(hkey_handle, mask, "DHKN", "d"))
530 return 0;
531
532 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533}
534
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400535static int hotkey_set(int status, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536{
537 int i;
538
539 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 return 0;
541
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400542 if (hotkey_mask_supported)
543 for (i = 0; i < 32; i++) {
544 int bit = ((1 << i) & mask) != 0;
545 if (!acpi_evalf(hkey_handle,
546 NULL, "MHKM", "vdd", i + 1, bit))
547 return 0;
548 }
549
550 return 1;
551}
552
553static int hotkey_init(void)
554{
555 /* hotkey not supported on 570 */
556 hotkey_supported = hkey_handle != NULL;
557
558 if (hotkey_supported) {
559 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
560 A30, R30, R31, T20-22, X20-21, X22-24 */
561 hotkey_mask_supported =
562 acpi_evalf(hkey_handle, NULL, "DHKN", "qv");
563
564 if (!hotkey_get(&hotkey_orig_status, &hotkey_orig_mask))
565 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 }
567
568 return 0;
569}
570
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400571static int hotkey_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572{
573 int status, mask;
574 int len = 0;
575
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400576 if (!hotkey_supported) {
577 len += sprintf(p + len, "status:\t\tnot supported\n");
578 return len;
579 }
580
581 if (!hotkey_get(&status, &mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 return -EIO;
583
584 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400585 if (hotkey_mask_supported) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 len += sprintf(p + len, "mask:\t\t0x%04x\n", mask);
587 len += sprintf(p + len,
588 "commands:\tenable, disable, reset, <mask>\n");
589 } else {
590 len += sprintf(p + len, "mask:\t\tnot supported\n");
591 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
592 }
593
594 return len;
595}
596
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400597static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
599 int status, mask;
600 char *cmd;
601 int do_cmd = 0;
602
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400603 if (!hotkey_supported)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 return -ENODEV;
605
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400606 if (!hotkey_get(&status, &mask))
607 return -EIO;
608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 while ((cmd = next_cmd(&buf))) {
610 if (strlencmp(cmd, "enable") == 0) {
611 status = 1;
612 } else if (strlencmp(cmd, "disable") == 0) {
613 status = 0;
614 } else if (strlencmp(cmd, "reset") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400615 status = hotkey_orig_status;
616 mask = hotkey_orig_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
618 /* mask set */
619 } else if (sscanf(cmd, "%x", &mask) == 1) {
620 /* mask set */
621 } else
622 return -EINVAL;
623 do_cmd = 1;
624 }
625
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400626 if (do_cmd && !hotkey_set(status, mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 return -EIO;
628
629 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400630}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400632static void hotkey_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400634 if (hotkey_supported)
635 hotkey_set(hotkey_orig_status, hotkey_orig_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636}
637
638static void hotkey_notify(struct ibm_struct *ibm, u32 event)
639{
640 int hkey;
641
642 if (acpi_evalf(hkey_handle, &hkey, "MHKP", "d"))
643 acpi_bus_generate_event(ibm->device, event, hkey);
644 else {
645 printk(IBM_ERR "unknown hotkey event %d\n", event);
646 acpi_bus_generate_event(ibm->device, event, 0);
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400647 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648}
649
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400650static int bluetooth_supported;
651
652static int bluetooth_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400654 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
655 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
656 bluetooth_supported = hkey_handle &&
657 acpi_evalf(hkey_handle, NULL, "GBDC", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
659 return 0;
660}
661
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400662static int bluetooth_status(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663{
664 int status;
665
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400666 if (!bluetooth_supported ||
667 !acpi_evalf(hkey_handle, &status, "GBDC", "d"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 status = 0;
669
670 return status;
671}
672
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400673static int bluetooth_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674{
675 int len = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400676 int status = bluetooth_status();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400678 if (!bluetooth_supported)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 len += sprintf(p + len, "status:\t\tnot supported\n");
680 else if (!(status & 1))
681 len += sprintf(p + len, "status:\t\tnot installed\n");
682 else {
683 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 1));
684 len += sprintf(p + len, "commands:\tenable, disable\n");
685 }
686
687 return len;
688}
689
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400690static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400692 int status = bluetooth_status();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 char *cmd;
694 int do_cmd = 0;
695
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400696 if (!bluetooth_supported)
697 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
699 while ((cmd = next_cmd(&buf))) {
700 if (strlencmp(cmd, "enable") == 0) {
701 status |= 2;
702 } else if (strlencmp(cmd, "disable") == 0) {
703 status &= ~2;
704 } else
705 return -EINVAL;
706 do_cmd = 1;
707 }
708
709 if (do_cmd && !acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400710 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
712 return 0;
713}
714
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -0400715static int wan_supported;
716
717static int wan_init(void)
718{
719 wan_supported = hkey_handle &&
720 acpi_evalf(hkey_handle, NULL, "GWAN", "qv");
721
722 return 0;
723}
724
725static int wan_status(void)
726{
727 int status;
728
Henrique de Moraes Holschuh8d297262006-11-24 11:47:07 -0200729 if (!wan_supported || !acpi_evalf(hkey_handle, &status, "GWAN", "d"))
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -0400730 status = 0;
731
732 return status;
733}
734
735static int wan_read(char *p)
736{
737 int len = 0;
738 int status = wan_status();
739
740 if (!wan_supported)
741 len += sprintf(p + len, "status:\t\tnot supported\n");
742 else if (!(status & 1))
743 len += sprintf(p + len, "status:\t\tnot installed\n");
744 else {
745 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 1));
746 len += sprintf(p + len, "commands:\tenable, disable\n");
747 }
748
749 return len;
750}
751
752static int wan_write(char *buf)
753{
754 int status = wan_status();
755 char *cmd;
756 int do_cmd = 0;
757
758 if (!wan_supported)
759 return -ENODEV;
760
761 while ((cmd = next_cmd(&buf))) {
762 if (strlencmp(cmd, "enable") == 0) {
763 status |= 2;
764 } else if (strlencmp(cmd, "disable") == 0) {
765 status &= ~2;
766 } else
767 return -EINVAL;
768 do_cmd = 1;
769 }
770
771 if (do_cmd && !acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
772 return -EIO;
773
774 return 0;
775}
776
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200777enum video_access_mode {
778 IBMACPI_VIDEO_NONE = 0,
779 IBMACPI_VIDEO_570, /* 570 */
780 IBMACPI_VIDEO_770, /* 600e/x, 770e, 770x */
781 IBMACPI_VIDEO_NEW, /* all others */
782};
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400783
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200784static enum video_access_mode video_supported;
785static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400786
787static int video_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400789 int ivga;
790
791 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
792 /* G41, assume IVGA doesn't change */
793 vid_handle = vid2_handle;
794
795 if (!vid_handle)
796 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200797 video_supported = IBMACPI_VIDEO_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400798 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
799 /* 570 */
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200800 video_supported = IBMACPI_VIDEO_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400801 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
802 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200803 video_supported = IBMACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400804 else
805 /* all others */
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200806 video_supported = IBMACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
808 return 0;
809}
810
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400811static int video_status(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812{
813 int status = 0;
814 int i;
815
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200816 if (video_supported == IBMACPI_VIDEO_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400817 if (acpi_evalf(NULL, &i, "\\_SB.PHS", "dd", 0x87))
818 status = i & 3;
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200819 } else if (video_supported == IBMACPI_VIDEO_770) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400820 if (acpi_evalf(NULL, &i, "\\VCDL", "d"))
821 status |= 0x01 * i;
822 if (acpi_evalf(NULL, &i, "\\VCDC", "d"))
823 status |= 0x02 * i;
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200824 } else if (video_supported == IBMACPI_VIDEO_NEW) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400825 acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1);
826 if (acpi_evalf(NULL, &i, "\\VCDC", "d"))
827 status |= 0x02 * i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400829 acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0);
830 if (acpi_evalf(NULL, &i, "\\VCDL", "d"))
831 status |= 0x01 * i;
832 if (acpi_evalf(NULL, &i, "\\VCDD", "d"))
833 status |= 0x08 * i;
834 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
836 return status;
837}
838
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400839static int video_autosw(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400841 int autosw = 0;
842
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200843 if (video_supported == IBMACPI_VIDEO_570)
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400844 acpi_evalf(vid_handle, &autosw, "SWIT", "d");
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200845 else if (video_supported == IBMACPI_VIDEO_770 ||
846 video_supported == IBMACPI_VIDEO_NEW)
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400847 acpi_evalf(vid_handle, &autosw, "^VDEE", "d");
848
849 return autosw & 1;
850}
851
852static int video_read(char *p)
853{
854 int status = video_status();
855 int autosw = video_autosw();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 int len = 0;
857
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400858 if (!video_supported) {
859 len += sprintf(p + len, "status:\t\tnot supported\n");
860 return len;
861 }
862
863 len += sprintf(p + len, "status:\t\tsupported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
865 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200866 if (video_supported == IBMACPI_VIDEO_NEW)
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400867 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
868 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
869 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
870 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200871 if (video_supported == IBMACPI_VIDEO_NEW)
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400872 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
873 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
875
876 return len;
877}
878
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400879static int video_switch(void)
880{
881 int autosw = video_autosw();
882 int ret;
883
884 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
885 return -EIO;
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200886 ret = video_supported == IBMACPI_VIDEO_570 ?
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400887 acpi_evalf(ec_handle, NULL, "_Q16", "v") :
888 acpi_evalf(vid_handle, NULL, "VSWT", "v");
889 acpi_evalf(vid_handle, NULL, "_DOS", "vd", autosw);
890
891 return ret;
892}
893
894static int video_expand(void)
895{
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200896 if (video_supported == IBMACPI_VIDEO_570)
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400897 return acpi_evalf(ec_handle, NULL, "_Q17", "v");
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200898 else if (video_supported == IBMACPI_VIDEO_770)
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400899 return acpi_evalf(vid_handle, NULL, "VEXP", "v");
900 else
901 return acpi_evalf(NULL, NULL, "\\VEXP", "v");
902}
903
904static int video_switch2(int status)
905{
906 int ret;
907
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200908 if (video_supported == IBMACPI_VIDEO_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400909 ret = acpi_evalf(NULL, NULL,
910 "\\_SB.PHS2", "vdd", 0x8b, status | 0x80);
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200911 } else if (video_supported == IBMACPI_VIDEO_770) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400912 int autosw = video_autosw();
913 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
914 return -EIO;
915
916 ret = acpi_evalf(vid_handle, NULL,
917 "ASWT", "vdd", status * 0x100, 0);
918
919 acpi_evalf(vid_handle, NULL, "_DOS", "vd", autosw);
920 } else {
921 ret = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
922 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
923 }
924
925 return ret;
926}
927
928static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929{
930 char *cmd;
931 int enable, disable, status;
932
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400933 if (!video_supported)
934 return -ENODEV;
935
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 enable = disable = 0;
937
938 while ((cmd = next_cmd(&buf))) {
939 if (strlencmp(cmd, "lcd_enable") == 0) {
940 enable |= 0x01;
941 } else if (strlencmp(cmd, "lcd_disable") == 0) {
942 disable |= 0x01;
943 } else if (strlencmp(cmd, "crt_enable") == 0) {
944 enable |= 0x02;
945 } else if (strlencmp(cmd, "crt_disable") == 0) {
946 disable |= 0x02;
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200947 } else if (video_supported == IBMACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400948 strlencmp(cmd, "dvi_enable") == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 enable |= 0x08;
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200950 } else if (video_supported == IBMACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400951 strlencmp(cmd, "dvi_disable") == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 disable |= 0x08;
953 } else if (strlencmp(cmd, "auto_enable") == 0) {
954 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
955 return -EIO;
956 } else if (strlencmp(cmd, "auto_disable") == 0) {
957 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 0))
958 return -EIO;
959 } else if (strlencmp(cmd, "video_switch") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400960 if (!video_switch())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 return -EIO;
962 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400963 if (!video_expand())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 return -EIO;
965 } else
966 return -EINVAL;
967 }
968
969 if (enable || disable) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400970 status = (video_status() & 0x0f & ~disable) | enable;
971 if (!video_switch2(status))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 return -EIO;
973 }
974
975 return 0;
976}
977
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400978static void video_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400980 acpi_evalf(vid_handle, NULL, "_DOS", "vd", video_orig_autosw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981}
982
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400983static int light_supported;
984static int light_status_supported;
985
986static int light_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400988 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
989 light_supported = (cmos_handle || lght_handle) && !ledb_handle;
990
991 if (light_supported)
992 /* light status not supported on
993 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
994 light_status_supported = acpi_evalf(ec_handle, NULL,
995 "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
997 return 0;
998}
999
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001000static int light_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001{
1002 int len = 0;
1003 int status = 0;
1004
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001005 if (!light_supported) {
1006 len += sprintf(p + len, "status:\t\tnot supported\n");
1007 } else if (!light_status_supported) {
1008 len += sprintf(p + len, "status:\t\tunknown\n");
1009 len += sprintf(p + len, "commands:\ton, off\n");
1010 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
1012 return -EIO;
1013 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001014 len += sprintf(p + len, "commands:\ton, off\n");
1015 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
1017 return len;
1018}
1019
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001020static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021{
1022 int cmos_cmd, lght_cmd;
1023 char *cmd;
1024 int success;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001025
1026 if (!light_supported)
1027 return -ENODEV;
1028
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 while ((cmd = next_cmd(&buf))) {
1030 if (strlencmp(cmd, "on") == 0) {
1031 cmos_cmd = 0x0c;
1032 lght_cmd = 1;
1033 } else if (strlencmp(cmd, "off") == 0) {
1034 cmos_cmd = 0x0d;
1035 lght_cmd = 0;
1036 } else
1037 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001038
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 success = cmos_handle ?
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001040 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
1041 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 if (!success)
1043 return -EIO;
1044 }
1045
1046 return 0;
1047}
1048
Henrique de Moraes Holschuh2bc808a2007-02-21 13:05:38 -02001049#if defined(CONFIG_ACPI_IBM_DOCK) || defined(CONFIG_ACPI_IBM_BAY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050static int _sta(acpi_handle handle)
1051{
1052 int status;
1053
1054 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
1055 status = 0;
1056
1057 return status;
1058}
Henrique de Moraes Holschuh2bc808a2007-02-21 13:05:38 -02001059#endif
Henrique de Moraes Holschuhd2fadbb2007-01-11 02:58:15 -05001060
Kristen Accardi63e5f242006-02-23 17:56:06 -08001061#ifdef CONFIG_ACPI_IBM_DOCK
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062#define dock_docked() (_sta(dock_handle) & 1)
1063
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001064static int dock_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065{
1066 int len = 0;
1067 int docked = dock_docked();
1068
1069 if (!dock_handle)
1070 len += sprintf(p + len, "status:\t\tnot supported\n");
1071 else if (!docked)
1072 len += sprintf(p + len, "status:\t\tundocked\n");
1073 else {
1074 len += sprintf(p + len, "status:\t\tdocked\n");
1075 len += sprintf(p + len, "commands:\tdock, undock\n");
1076 }
1077
1078 return len;
1079}
1080
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001081static int dock_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082{
1083 char *cmd;
1084
1085 if (!dock_docked())
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001086 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
1088 while ((cmd = next_cmd(&buf))) {
1089 if (strlencmp(cmd, "undock") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001090 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
1091 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 return -EIO;
1093 } else if (strlencmp(cmd, "dock") == 0) {
1094 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
1095 return -EIO;
1096 } else
1097 return -EINVAL;
1098 }
1099
1100 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001101}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
1103static void dock_notify(struct ibm_struct *ibm, u32 event)
1104{
1105 int docked = dock_docked();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001106 int pci = ibm->hid && strstr(ibm->hid, IBM_PCI_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001108 if (event == 1 && !pci) /* 570 */
1109 acpi_bus_generate_event(ibm->device, event, 1); /* button */
1110 else if (event == 1 && pci) /* 570 */
1111 acpi_bus_generate_event(ibm->device, event, 3); /* dock */
1112 else if (event == 3 && docked)
1113 acpi_bus_generate_event(ibm->device, event, 1); /* button */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 else if (event == 3 && !docked)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001115 acpi_bus_generate_event(ibm->device, event, 2); /* undock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 else if (event == 0 && docked)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001117 acpi_bus_generate_event(ibm->device, event, 3); /* dock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 else {
1119 printk(IBM_ERR "unknown dock event %d, status %d\n",
1120 event, _sta(dock_handle));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001121 acpi_bus_generate_event(ibm->device, event, 0); /* unknown */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 }
1123}
Kristen Accardi63e5f242006-02-23 17:56:06 -08001124#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
Henrique de Moraes Holschuh2bc808a2007-02-21 13:05:38 -02001126#ifdef CONFIG_ACPI_IBM_BAY
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001127static int bay_status_supported;
1128static int bay_status2_supported;
1129static int bay_eject_supported;
1130static int bay_eject2_supported;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001132static int bay_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001134 bay_status_supported = bay_handle &&
1135 acpi_evalf(bay_handle, NULL, "_STA", "qv");
1136 bay_status2_supported = bay2_handle &&
1137 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
1138
1139 bay_eject_supported = bay_handle && bay_ej_handle &&
1140 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
1141 bay_eject2_supported = bay2_handle && bay2_ej_handle &&
1142 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
1144 return 0;
1145}
1146
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001147#define bay_occupied(b) (_sta(b##_handle) & 1)
1148
1149static int bay_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150{
1151 int len = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001152 int occupied = bay_occupied(bay);
1153 int occupied2 = bay_occupied(bay2);
1154 int eject, eject2;
1155
1156 len += sprintf(p + len, "status:\t\t%s\n", bay_status_supported ?
1157 (occupied ? "occupied" : "unoccupied") :
1158 "not supported");
1159 if (bay_status2_supported)
1160 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
1161 "occupied" : "unoccupied");
1162
1163 eject = bay_eject_supported && occupied;
1164 eject2 = bay_eject2_supported && occupied2;
1165
1166 if (eject && eject2)
1167 len += sprintf(p + len, "commands:\teject, eject2\n");
1168 else if (eject)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 len += sprintf(p + len, "commands:\teject\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001170 else if (eject2)
1171 len += sprintf(p + len, "commands:\teject2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
1173 return len;
1174}
1175
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001176static int bay_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177{
1178 char *cmd;
1179
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001180 if (!bay_eject_supported && !bay_eject2_supported)
1181 return -ENODEV;
1182
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001184 if (bay_eject_supported && strlencmp(cmd, "eject") == 0) {
1185 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
1186 return -EIO;
1187 } else if (bay_eject2_supported &&
1188 strlencmp(cmd, "eject2") == 0) {
1189 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 return -EIO;
1191 } else
1192 return -EINVAL;
1193 }
1194
1195 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001196}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
1198static void bay_notify(struct ibm_struct *ibm, u32 event)
1199{
1200 acpi_bus_generate_event(ibm->device, event, 0);
1201}
Henrique de Moraes Holschuh2bc808a2007-02-21 13:05:38 -02001202#endif /* CONFIG_ACPI_IBM_BAY */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001204static int cmos_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205{
1206 int len = 0;
1207
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001208 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1209 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 if (!cmos_handle)
1211 len += sprintf(p + len, "status:\t\tnot supported\n");
1212 else {
1213 len += sprintf(p + len, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001214 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 }
1216
1217 return len;
1218}
1219
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001220static int cmos_eval(int cmos_cmd)
1221{
1222 if (cmos_handle)
1223 return acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd);
1224 else
1225 return 1;
1226}
1227
1228static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229{
1230 char *cmd;
1231 int cmos_cmd;
1232
1233 if (!cmos_handle)
1234 return -EINVAL;
1235
1236 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001237 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
1238 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 /* cmos_cmd set */
1240 } else
1241 return -EINVAL;
1242
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001243 if (!cmos_eval(cmos_cmd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 return -EIO;
1245 }
1246
1247 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001248}
1249
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02001250enum led_access_mode {
1251 IBMACPI_LED_NONE = 0,
1252 IBMACPI_LED_570, /* 570 */
1253 IBMACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
1254 IBMACPI_LED_NEW, /* all others */
1255};
1256static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001257
1258static int led_init(void)
1259{
1260 if (!led_handle)
1261 /* led not supported on R30, R31 */
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02001262 led_supported = IBMACPI_LED_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001263 else if (strlencmp(led_path, "SLED") == 0)
1264 /* 570 */
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02001265 led_supported = IBMACPI_LED_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001266 else if (strlencmp(led_path, "SYSL") == 0)
1267 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02001268 led_supported = IBMACPI_LED_OLD;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001269 else
1270 /* all others */
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02001271 led_supported = IBMACPI_LED_NEW;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001272
1273 return 0;
1274}
1275
1276#define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
1277
1278static int led_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279{
1280 int len = 0;
1281
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001282 if (!led_supported) {
1283 len += sprintf(p + len, "status:\t\tnot supported\n");
1284 return len;
1285 }
1286 len += sprintf(p + len, "status:\t\tsupported\n");
1287
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02001288 if (led_supported == IBMACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001289 /* 570 */
1290 int i, status;
1291 for (i = 0; i < 8; i++) {
1292 if (!acpi_evalf(ec_handle,
1293 &status, "GLED", "dd", 1 << i))
1294 return -EIO;
1295 len += sprintf(p + len, "%d:\t\t%s\n",
1296 i, led_status(status));
1297 }
1298 }
1299
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 len += sprintf(p + len, "commands:\t"
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001301 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
1303 return len;
1304}
1305
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001306/* off, on, blink */
1307static const int led_sled_arg1[] = { 0, 1, 3 };
1308static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
1309static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
1310static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
1311
1312#define EC_HLCL 0x0c
1313#define EC_HLBL 0x0d
1314#define EC_HLMS 0x0e
1315
1316static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317{
1318 char *cmd;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001319 int led, ind, ret;
1320
1321 if (!led_supported)
1322 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
1324 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001325 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 return -EINVAL;
1327
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001328 if (strstr(cmd, "off")) {
1329 ind = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 } else if (strstr(cmd, "on")) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001331 ind = 1;
1332 } else if (strstr(cmd, "blink")) {
1333 ind = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 } else
1335 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001336
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02001337 if (led_supported == IBMACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001338 /* 570 */
1339 led = 1 << led;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001341 led, led_sled_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 return -EIO;
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02001343 } else if (led_supported == IBMACPI_LED_OLD) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001344 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
1345 led = 1 << led;
1346 ret = ec_write(EC_HLMS, led);
1347 if (ret >= 0)
1348 ret =
1349 ec_write(EC_HLBL, led * led_exp_hlbl[ind]);
1350 if (ret >= 0)
1351 ret =
1352 ec_write(EC_HLCL, led * led_exp_hlcl[ind]);
1353 if (ret < 0)
1354 return ret;
1355 } else {
1356 /* all others */
1357 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
1358 led, led_led_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001360 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 }
1362
1363 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001364}
1365
1366static int beep_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367{
1368 int len = 0;
1369
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001370 if (!beep_handle)
1371 len += sprintf(p + len, "status:\t\tnot supported\n");
1372 else {
1373 len += sprintf(p + len, "status:\t\tsupported\n");
1374 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
1375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
1377 return len;
1378}
1379
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001380static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381{
1382 char *cmd;
1383 int beep_cmd;
1384
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001385 if (!beep_handle)
1386 return -ENODEV;
1387
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001389 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
1390 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 /* beep_cmd set */
1392 } else
1393 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001394 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 return -EIO;
1396 }
1397
1398 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001399}
1400
1401static int acpi_ec_read(int i, u8 * p)
1402{
1403 int v;
1404
1405 if (ecrd_handle) {
1406 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
1407 return 0;
1408 *p = v;
1409 } else {
1410 if (ec_read(i, p) < 0)
1411 return 0;
1412 }
1413
1414 return 1;
1415}
1416
1417static int acpi_ec_write(int i, u8 v)
1418{
1419 if (ecwr_handle) {
1420 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
1421 return 0;
1422 } else {
1423 if (ec_write(i, v) < 0)
1424 return 0;
1425 }
1426
1427 return 1;
1428}
1429
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001430static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001431
1432static int thermal_init(void)
1433{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001434 u8 t, ta1, ta2;
1435 int i;
1436 int acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001437
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001438 if (ibm_thinkpad_ec_found && experimental) {
1439 /*
1440 * Direct EC access mode: sensors at registers
1441 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
1442 * non-implemented, thermal sensors return 0x80 when
1443 * not available
1444 */
1445
1446 ta1 = ta2 = 0;
1447 for (i = 0; i < 8; i++) {
1448 if (likely(acpi_ec_read(0x78 + i, &t))) {
1449 ta1 |= t;
1450 } else {
1451 ta1 = 0;
1452 break;
1453 }
1454 if (likely(acpi_ec_read(0xC0 + i, &t))) {
1455 ta2 |= t;
1456 } else {
1457 ta1 = 0;
1458 break;
1459 }
1460 }
1461 if (ta1 == 0) {
1462 /* This is sheer paranoia, but we handle it anyway */
1463 if (acpi_tmp7) {
1464 printk(IBM_ERR
1465 "ThinkPad ACPI EC access misbehaving, "
1466 "falling back to ACPI TMPx access mode\n");
1467 thermal_read_mode = IBMACPI_THERMAL_ACPI_TMP07;
1468 } else {
1469 printk(IBM_ERR
1470 "ThinkPad ACPI EC access misbehaving, "
1471 "disabling thermal sensors access\n");
1472 thermal_read_mode = IBMACPI_THERMAL_NONE;
1473 }
1474 } else {
1475 thermal_read_mode =
1476 (ta2 != 0) ?
1477 IBMACPI_THERMAL_TPEC_16 : IBMACPI_THERMAL_TPEC_8;
1478 }
1479 } else if (acpi_tmp7) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001480 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
1481 /* 600e/x, 770e, 770x */
1482 thermal_read_mode = IBMACPI_THERMAL_ACPI_UPDT;
1483 } else {
1484 /* Standard ACPI TMPx access, max 8 sensors */
1485 thermal_read_mode = IBMACPI_THERMAL_ACPI_TMP07;
1486 }
1487 } else {
1488 /* temperatures not supported on 570, G4x, R30, R31, R32 */
1489 thermal_read_mode = IBMACPI_THERMAL_NONE;
1490 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001491
1492 return 0;
1493}
1494
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001495static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
1496{
1497 int i, t;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001498 s8 tmp;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001499 char tmpi[] = "TMPi";
1500
1501 if (!s)
1502 return -EINVAL;
1503
1504 switch (thermal_read_mode) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001505#if IBMACPI_MAX_THERMAL_SENSORS >= 16
1506 case IBMACPI_THERMAL_TPEC_16:
1507 for (i = 0; i < 8; i++) {
1508 if (!acpi_ec_read(0xC0 + i, &tmp))
1509 return -EIO;
1510 s->temp[i + 8] = tmp * 1000;
1511 }
1512 /* fallthrough */
1513#endif
1514 case IBMACPI_THERMAL_TPEC_8:
1515 for (i = 0; i < 8; i++) {
1516 if (!acpi_ec_read(0x78 + i, &tmp))
1517 return -EIO;
1518 s->temp[i] = tmp * 1000;
1519 }
1520 return (thermal_read_mode == IBMACPI_THERMAL_TPEC_16) ? 16 : 8;
1521
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001522 case IBMACPI_THERMAL_ACPI_UPDT:
1523 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
1524 return -EIO;
1525 for (i = 0; i < 8; i++) {
1526 tmpi[3] = '0' + i;
1527 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
1528 return -EIO;
1529 s->temp[i] = (t - 2732) * 100;
1530 }
1531 return 8;
1532
1533 case IBMACPI_THERMAL_ACPI_TMP07:
1534 for (i = 0; i < 8; i++) {
1535 tmpi[3] = '0' + i;
1536 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
1537 return -EIO;
1538 s->temp[i] = t * 1000;
1539 }
1540 return 8;
1541
1542 case IBMACPI_THERMAL_NONE:
1543 default:
1544 return 0;
1545 }
1546}
1547
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001548static int thermal_read(char *p)
1549{
1550 int len = 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001551 int n, i;
1552 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001553
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001554 n = thermal_get_sensors(&t);
1555 if (unlikely(n < 0))
1556 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001557
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001558 len += sprintf(p + len, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001559
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001560 if (n > 0) {
1561 for (i = 0; i < (n - 1); i++)
1562 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
1563 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
1564 } else
1565 len += sprintf(p + len, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001566
1567 return len;
1568}
1569
1570static u8 ecdump_regs[256];
1571
1572static int ecdump_read(char *p)
1573{
1574 int len = 0;
1575 int i, j;
1576 u8 v;
1577
1578 len += sprintf(p + len, "EC "
1579 " +00 +01 +02 +03 +04 +05 +06 +07"
1580 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
1581 for (i = 0; i < 256; i += 16) {
1582 len += sprintf(p + len, "EC 0x%02x:", i);
1583 for (j = 0; j < 16; j++) {
1584 if (!acpi_ec_read(i + j, &v))
1585 break;
1586 if (v != ecdump_regs[i + j])
1587 len += sprintf(p + len, " *%02x", v);
1588 else
1589 len += sprintf(p + len, " %02x", v);
1590 ecdump_regs[i + j] = v;
1591 }
1592 len += sprintf(p + len, "\n");
1593 if (j != 16)
1594 break;
1595 }
1596
1597 /* These are way too dangerous to advertise openly... */
1598#if 0
1599 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
1600 " (<offset> is 00-ff, <value> is 00-ff)\n");
1601 len += sprintf(p + len, "commands:\t0x<offset> <value> "
1602 " (<offset> is 00-ff, <value> is 0-255)\n");
1603#endif
1604 return len;
1605}
1606
1607static int ecdump_write(char *buf)
1608{
1609 char *cmd;
1610 int i, v;
1611
1612 while ((cmd = next_cmd(&buf))) {
1613 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
1614 /* i and v set */
1615 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
1616 /* i and v set */
1617 } else
1618 return -EINVAL;
1619 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
1620 if (!acpi_ec_write(i, v))
1621 return -EIO;
1622 } else
1623 return -EINVAL;
1624 }
1625
1626 return 0;
1627}
1628
1629static int brightness_offset = 0x31;
1630
Holger Macht8acb0252006-10-20 14:30:28 -07001631static int brightness_get(struct backlight_device *bd)
1632{
Henrique de Moraes Holschuh8d297262006-11-24 11:47:07 -02001633 u8 level;
1634 if (!acpi_ec_read(brightness_offset, &level))
1635 return -EIO;
Holger Macht8acb0252006-10-20 14:30:28 -07001636
Henrique de Moraes Holschuh8d297262006-11-24 11:47:07 -02001637 level &= 0x7;
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02001638
Henrique de Moraes Holschuh8d297262006-11-24 11:47:07 -02001639 return level;
Holger Macht8acb0252006-10-20 14:30:28 -07001640}
1641
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001642static int brightness_read(char *p)
1643{
1644 int len = 0;
Holger Macht8acb0252006-10-20 14:30:28 -07001645 int level;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001646
Holger Macht8acb0252006-10-20 14:30:28 -07001647 if ((level = brightness_get(NULL)) < 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001648 len += sprintf(p + len, "level:\t\tunreadable\n");
1649 } else {
1650 len += sprintf(p + len, "level:\t\t%d\n", level & 0x7);
1651 len += sprintf(p + len, "commands:\tup, down\n");
1652 len += sprintf(p + len, "commands:\tlevel <level>"
1653 " (<level> is 0-7)\n");
1654 }
1655
1656 return len;
1657}
1658
1659#define BRIGHTNESS_UP 4
1660#define BRIGHTNESS_DOWN 5
1661
Holger Macht8acb0252006-10-20 14:30:28 -07001662static int brightness_set(int value)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001663{
1664 int cmos_cmd, inc, i;
Holger Macht8acb0252006-10-20 14:30:28 -07001665 int current_value = brightness_get(NULL);
1666
1667 value &= 7;
1668
1669 cmos_cmd = value > current_value ? BRIGHTNESS_UP : BRIGHTNESS_DOWN;
1670 inc = value > current_value ? 1 : -1;
1671 for (i = current_value; i != value; i += inc) {
1672 if (!cmos_eval(cmos_cmd))
1673 return -EIO;
1674 if (!acpi_ec_write(brightness_offset, i + inc))
1675 return -EIO;
1676 }
1677
1678 return 0;
1679}
1680
1681static int brightness_write(char *buf)
1682{
1683 int level;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001684 int new_level;
1685 char *cmd;
1686
1687 while ((cmd = next_cmd(&buf))) {
Holger Macht8acb0252006-10-20 14:30:28 -07001688 if ((level = brightness_get(NULL)) < 0)
1689 return level;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001690 level &= 7;
1691
1692 if (strlencmp(cmd, "up") == 0) {
1693 new_level = level == 7 ? 7 : level + 1;
1694 } else if (strlencmp(cmd, "down") == 0) {
1695 new_level = level == 0 ? 0 : level - 1;
1696 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
1697 new_level >= 0 && new_level <= 7) {
1698 /* new_level set */
1699 } else
1700 return -EINVAL;
1701
Holger Macht8acb0252006-10-20 14:30:28 -07001702 brightness_set(new_level);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001703 }
1704
1705 return 0;
1706}
1707
Holger Macht8acb0252006-10-20 14:30:28 -07001708static int brightness_update_status(struct backlight_device *bd)
1709{
Richard Purdie599a52d2007-02-10 23:07:48 +00001710 return brightness_set(bd->props.brightness);
Holger Macht8acb0252006-10-20 14:30:28 -07001711}
1712
Richard Purdie599a52d2007-02-10 23:07:48 +00001713static struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02001714 .get_brightness = brightness_get,
1715 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02001716};
1717
1718static int brightness_init(void)
1719{
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02001720 int b;
1721
1722 b = brightness_get(NULL);
1723 if (b < 0)
1724 return b;
1725
Yu Luming519ab5f2006-12-19 12:56:15 -08001726 ibm_backlight_device = backlight_device_register("ibm", NULL, NULL,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02001727 &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
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02001733 ibm_backlight_device->props.max_brightness = 7;
1734 ibm_backlight_device->props.brightness = b;
1735 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00001736
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02001737 return 0;
1738}
1739
1740static void brightness_exit(void)
1741{
1742 if (ibm_backlight_device) {
1743 backlight_device_unregister(ibm_backlight_device);
1744 ibm_backlight_device = NULL;
1745 }
1746}
1747
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001748static int volume_offset = 0x30;
1749
1750static int volume_read(char *p)
1751{
1752 int len = 0;
1753 u8 level;
1754
1755 if (!acpi_ec_read(volume_offset, &level)) {
1756 len += sprintf(p + len, "level:\t\tunreadable\n");
1757 } else {
1758 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
1759 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
1760 len += sprintf(p + len, "commands:\tup, down, mute\n");
1761 len += sprintf(p + len, "commands:\tlevel <level>"
1762 " (<level> is 0-15)\n");
1763 }
1764
1765 return len;
1766}
1767
1768#define VOLUME_DOWN 0
1769#define VOLUME_UP 1
1770#define VOLUME_MUTE 2
1771
1772static int volume_write(char *buf)
1773{
1774 int cmos_cmd, inc, i;
1775 u8 level, mute;
1776 int new_level, new_mute;
1777 char *cmd;
1778
1779 while ((cmd = next_cmd(&buf))) {
1780 if (!acpi_ec_read(volume_offset, &level))
1781 return -EIO;
1782 new_mute = mute = level & 0x40;
1783 new_level = level = level & 0xf;
1784
1785 if (strlencmp(cmd, "up") == 0) {
1786 if (mute)
1787 new_mute = 0;
1788 else
1789 new_level = level == 15 ? 15 : level + 1;
1790 } else if (strlencmp(cmd, "down") == 0) {
1791 if (mute)
1792 new_mute = 0;
1793 else
1794 new_level = level == 0 ? 0 : level - 1;
1795 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
1796 new_level >= 0 && new_level <= 15) {
1797 /* new_level set */
1798 } else if (strlencmp(cmd, "mute") == 0) {
1799 new_mute = 0x40;
1800 } else
1801 return -EINVAL;
1802
1803 if (new_level != level) { /* mute doesn't change */
1804 cmos_cmd = new_level > level ? VOLUME_UP : VOLUME_DOWN;
1805 inc = new_level > level ? 1 : -1;
1806
1807 if (mute && (!cmos_eval(cmos_cmd) ||
1808 !acpi_ec_write(volume_offset, level)))
1809 return -EIO;
1810
1811 for (i = level; i != new_level; i += inc)
1812 if (!cmos_eval(cmos_cmd) ||
1813 !acpi_ec_write(volume_offset, i + inc))
1814 return -EIO;
1815
1816 if (mute && (!cmos_eval(VOLUME_MUTE) ||
1817 !acpi_ec_write(volume_offset,
1818 new_level + mute)))
1819 return -EIO;
1820 }
1821
1822 if (new_mute != mute) { /* level doesn't change */
1823 cmos_cmd = new_mute ? VOLUME_MUTE : VOLUME_UP;
1824
1825 if (!cmos_eval(cmos_cmd) ||
1826 !acpi_ec_write(volume_offset, level + new_mute))
1827 return -EIO;
1828 }
1829 }
1830
1831 return 0;
1832}
1833
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001834static enum fan_status_access_mode fan_status_access_mode;
1835static enum fan_control_access_mode fan_control_access_mode;
1836static enum fan_control_commands fan_control_commands;
1837
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02001838static int fan_control_status_known;
1839static u8 fan_control_initial_status;
1840
Len Brown25c68a32006-12-08 04:43:41 -05001841static void fan_watchdog_fire(struct work_struct *ignored);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02001842static int fan_watchdog_maxinterval;
Len Brown25c68a32006-12-08 04:43:41 -05001843static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02001844
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001845static int fan_init(void)
1846{
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001847 fan_status_access_mode = IBMACPI_FAN_NONE;
1848 fan_control_access_mode = IBMACPI_FAN_WR_NONE;
1849 fan_control_commands = 0;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02001850 fan_control_status_known = 1;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02001851 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001852
1853 if (gfan_handle) {
1854 /* 570, 600e/x, 770e, 770x */
1855 fan_status_access_mode = IBMACPI_FAN_RD_ACPI_GFAN;
1856 } else {
1857 /* all other ThinkPads: note that even old-style
1858 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02001859 if (likely(acpi_ec_read(fan_status_offset,
1860 &fan_control_initial_status))) {
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001861 fan_status_access_mode = IBMACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02001862
1863 /* In some ThinkPads, neither the EC nor the ACPI
1864 * DSDT initialize the fan status, and it ends up
1865 * being set to 0x07 when it *could* be either
1866 * 0x07 or 0x80.
1867 *
1868 * Enable for TP-1Y (T43), TP-78 (R51e),
1869 * TP-76 (R52), TP-70 (T43, R52), which are known
1870 * to be buggy. */
1871 if (fan_control_initial_status == 0x07 &&
1872 ibm_thinkpad_ec_found &&
1873 ((ibm_thinkpad_ec_found[0] == '1' &&
1874 ibm_thinkpad_ec_found[1] == 'Y') ||
1875 (ibm_thinkpad_ec_found[0] == '7' &&
1876 (ibm_thinkpad_ec_found[1] == '6' ||
1877 ibm_thinkpad_ec_found[1] == '8' ||
1878 ibm_thinkpad_ec_found[1] == '0'))
1879 )) {
1880 printk(IBM_NOTICE
1881 "fan_init: initial fan status is "
1882 "unknown, assuming it is in auto "
1883 "mode\n");
1884 fan_control_status_known = 0;
1885 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001886 } else {
1887 printk(IBM_ERR
1888 "ThinkPad ACPI EC access misbehaving, "
1889 "fan status and control unavailable\n");
1890 return 0;
1891 }
1892 }
1893
1894 if (sfan_handle) {
1895 /* 570, 770x-JL */
1896 fan_control_access_mode = IBMACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02001897 fan_control_commands |=
1898 IBMACPI_FAN_CMD_LEVEL | IBMACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001899 } else {
1900 if (!gfan_handle) {
1901 /* gfan without sfan means no fan control */
1902 /* all other models implement TP EC 0x2f control */
1903
1904 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02001905 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001906 fan_control_access_mode =
1907 IBMACPI_FAN_WR_ACPI_FANS;
1908 fan_control_commands |=
1909 IBMACPI_FAN_CMD_SPEED |
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02001910 IBMACPI_FAN_CMD_LEVEL |
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001911 IBMACPI_FAN_CMD_ENABLE;
1912 } else {
1913 fan_control_access_mode = IBMACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02001914 fan_control_commands |=
1915 IBMACPI_FAN_CMD_LEVEL |
1916 IBMACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001917 }
1918 }
1919 }
1920
1921 return 0;
1922}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001923
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02001924static int fan_get_status(u8 *status)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001925{
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02001926 u8 s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001927
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02001928 /* TODO:
1929 * Add IBMACPI_FAN_RD_ACPI_FANS ? */
1930
Henrique de Moraes Holschuh3ef8a602006-11-24 11:47:10 -02001931 switch (fan_status_access_mode) {
1932 case IBMACPI_FAN_RD_ACPI_GFAN:
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001933 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02001934
1935 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001936 return -EIO;
1937
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02001938 if (likely(status))
1939 *status = s & 0x07;
1940
1941 break;
1942
1943 case IBMACPI_FAN_RD_TPEC:
1944 /* all except 570, 600e/x, 770e, 770x */
1945 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
1946 return -EIO;
1947
1948 if (likely(status))
1949 *status = s;
1950
1951 break;
1952
1953 default:
1954 return -ENXIO;
1955 }
1956
1957 return 0;
1958}
1959
1960static int fan_get_speed(unsigned int *speed)
1961{
1962 u8 hi, lo;
1963
1964 switch (fan_status_access_mode) {
1965 case IBMACPI_FAN_RD_TPEC:
1966 /* all except 570, 600e/x, 770e, 770x */
1967 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
1968 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
1969 return -EIO;
1970
1971 if (likely(speed))
1972 *speed = (hi << 8) | lo;
1973
1974 break;
1975
1976 default:
1977 return -ENXIO;
1978 }
1979
1980 return 0;
1981}
1982
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02001983static void fan_exit(void)
1984{
1985 cancel_delayed_work(&fan_watchdog_task);
1986 flush_scheduled_work();
1987}
1988
1989static void fan_watchdog_reset(void)
1990{
1991 static int fan_watchdog_active = 0;
1992
1993 if (fan_watchdog_active)
1994 cancel_delayed_work(&fan_watchdog_task);
1995
1996 if (fan_watchdog_maxinterval > 0) {
1997 fan_watchdog_active = 1;
1998 if (!schedule_delayed_work(&fan_watchdog_task,
1999 msecs_to_jiffies(fan_watchdog_maxinterval
2000 * 1000))) {
2001 printk(IBM_ERR "failed to schedule the fan watchdog, "
2002 "watchdog will not trigger\n");
2003 }
2004 } else
2005 fan_watchdog_active = 0;
2006}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002007
2008static int fan_read(char *p)
2009{
2010 int len = 0;
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02002011 int rc;
2012 u8 status;
2013 unsigned int speed = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002014
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02002015 switch (fan_status_access_mode) {
2016 case IBMACPI_FAN_RD_ACPI_GFAN:
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002017 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02002018 if ((rc = fan_get_status(&status)) < 0)
2019 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002020
Henrique de Moraes Holschuhbab812a2006-11-24 11:47:12 -02002021 len += sprintf(p + len, "status:\t\t%s\n"
2022 "level:\t\t%d\n",
2023 (status != 0) ? "enabled" : "disabled", status);
Henrique de Moraes Holschuh3ef8a602006-11-24 11:47:10 -02002024 break;
2025
2026 case IBMACPI_FAN_RD_TPEC:
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002027 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02002028 if ((rc = fan_get_status(&status)) < 0)
2029 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002030
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002031 if (unlikely(!fan_control_status_known)) {
2032 if (status != fan_control_initial_status)
2033 fan_control_status_known = 1;
2034 else
2035 /* Return most likely status. In fact, it
2036 * might be the only possible status */
2037 status = IBMACPI_FAN_EC_AUTO;
2038 }
2039
Henrique de Moraes Holschuhbab812a2006-11-24 11:47:12 -02002040 len += sprintf(p + len, "status:\t\t%s\n",
2041 (status != 0) ? "enabled" : "disabled");
Henrique de Moraes Holschuh3ef8a602006-11-24 11:47:10 -02002042
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002043 /* No ThinkPad boots on disengaged mode, we can safely
2044 * assume the tachometer is online if fan control status
2045 * was unknown */
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02002046 if ((rc = fan_get_speed(&speed)) < 0)
2047 return rc;
2048
2049 len += sprintf(p + len, "speed:\t\t%d\n", speed);
Henrique de Moraes Holschuhbab812a2006-11-24 11:47:12 -02002050
2051 if (status & IBMACPI_FAN_EC_DISENGAGED)
2052 /* Disengaged mode takes precedence */
2053 len += sprintf(p + len, "level:\t\tdisengaged\n");
2054 else if (status & IBMACPI_FAN_EC_AUTO)
2055 len += sprintf(p + len, "level:\t\tauto\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002056 else
Henrique de Moraes Holschuhbab812a2006-11-24 11:47:12 -02002057 len += sprintf(p + len, "level:\t\t%d\n", status);
Henrique de Moraes Holschuh3ef8a602006-11-24 11:47:10 -02002058 break;
2059
2060 case IBMACPI_FAN_NONE:
2061 default:
2062 len += sprintf(p + len, "status:\t\tnot supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002063 }
2064
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02002065 if (fan_control_commands & IBMACPI_FAN_CMD_LEVEL) {
2066 len += sprintf(p + len, "commands:\tlevel <level>");
2067
2068 switch (fan_control_access_mode) {
2069 case IBMACPI_FAN_WR_ACPI_SFAN:
2070 len += sprintf(p + len, " (<level> is 0-7)\n");
2071 break;
2072
2073 default:
2074 len += sprintf(p + len, " (<level> is 0-7, "
2075 "auto, disengaged)\n");
2076 break;
2077 }
2078 }
Henrique de Moraes Holschuh3ef8a602006-11-24 11:47:10 -02002079
2080 if (fan_control_commands & IBMACPI_FAN_CMD_ENABLE)
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002081 len += sprintf(p + len, "commands:\tenable, disable\n"
2082 "commands:\twatchdog <timeout> (<timeout> is 0 (off), "
2083 "1-120 (seconds))\n");
Henrique de Moraes Holschuh3ef8a602006-11-24 11:47:10 -02002084
2085 if (fan_control_commands & IBMACPI_FAN_CMD_SPEED)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002086 len += sprintf(p + len, "commands:\tspeed <speed>"
2087 " (<speed> is 0-65535)\n");
2088
2089 return len;
2090}
2091
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002092static int fan_set_level(int level)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002093{
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002094 switch (fan_control_access_mode) {
2095 case IBMACPI_FAN_WR_ACPI_SFAN:
2096 if (level >= 0 && level <= 7) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002097 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
2098 return -EIO;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002099 } else
2100 return -EINVAL;
2101 break;
2102
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02002103 case IBMACPI_FAN_WR_ACPI_FANS:
2104 case IBMACPI_FAN_WR_TPEC:
2105 if ((level != IBMACPI_FAN_EC_AUTO) &&
2106 (level != IBMACPI_FAN_EC_DISENGAGED) &&
2107 ((level < 0) || (level > 7)))
2108 return -EINVAL;
2109
2110 if (!acpi_ec_write(fan_status_offset, level))
2111 return -EIO;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002112 else
2113 fan_control_status_known = 1;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02002114 break;
2115
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002116 default:
2117 return -ENXIO;
2118 }
2119 return 0;
2120}
2121
2122static int fan_set_enable(void)
2123{
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002124 u8 s;
2125 int rc;
2126
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002127 switch (fan_control_access_mode) {
2128 case IBMACPI_FAN_WR_ACPI_FANS:
2129 case IBMACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002130 if ((rc = fan_get_status(&s)) < 0)
2131 return rc;
2132
2133 /* Don't go out of emergency fan mode */
2134 if (s != 7)
2135 s = IBMACPI_FAN_EC_AUTO;
2136
2137 if (!acpi_ec_write(fan_status_offset, s))
2138 return -EIO;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002139 else
2140 fan_control_status_known = 1;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002141 break;
2142
2143 case IBMACPI_FAN_WR_ACPI_SFAN:
2144 if ((rc = fan_get_status(&s)) < 0)
2145 return rc;
2146
2147 s &= 0x07;
2148
2149 /* Set fan to at least level 4 */
2150 if (s < 4)
2151 s = 4;
2152
2153 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002154 return -EIO;
2155 break;
2156
2157 default:
2158 return -ENXIO;
2159 }
2160 return 0;
2161}
2162
2163static int fan_set_disable(void)
2164{
2165 switch (fan_control_access_mode) {
2166 case IBMACPI_FAN_WR_ACPI_FANS:
2167 case IBMACPI_FAN_WR_TPEC:
2168 if (!acpi_ec_write(fan_status_offset, 0x00))
2169 return -EIO;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002170 else
2171 fan_control_status_known = 1;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002172 break;
2173
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002174 case IBMACPI_FAN_WR_ACPI_SFAN:
2175 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
2176 return -EIO;
2177 break;
2178
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002179 default:
2180 return -ENXIO;
2181 }
2182 return 0;
2183}
2184
2185static int fan_set_speed(int speed)
2186{
2187 switch (fan_control_access_mode) {
2188 case IBMACPI_FAN_WR_ACPI_FANS:
2189 if (speed >= 0 && speed <= 65535) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002190 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
2191 speed, speed, speed))
2192 return -EIO;
2193 } else
2194 return -EINVAL;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002195 break;
2196
2197 default:
2198 return -ENXIO;
2199 }
2200 return 0;
2201}
2202
2203static int fan_write_cmd_level(const char *cmd, int *rc)
2204{
2205 int level;
2206
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02002207 if (strlencmp(cmd, "level auto") == 0)
2208 level = IBMACPI_FAN_EC_AUTO;
2209 else if (strlencmp(cmd, "level disengaged") == 0)
2210 level = IBMACPI_FAN_EC_DISENGAGED;
2211 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002212 return 0;
2213
2214 if ((*rc = fan_set_level(level)) == -ENXIO)
2215 printk(IBM_ERR "level command accepted for unsupported "
2216 "access mode %d", fan_control_access_mode);
2217
2218 return 1;
2219}
2220
2221static int fan_write_cmd_enable(const char *cmd, int *rc)
2222{
2223 if (strlencmp(cmd, "enable") != 0)
2224 return 0;
2225
2226 if ((*rc = fan_set_enable()) == -ENXIO)
2227 printk(IBM_ERR "enable command accepted for unsupported "
2228 "access mode %d", fan_control_access_mode);
2229
2230 return 1;
2231}
2232
2233static int fan_write_cmd_disable(const char *cmd, int *rc)
2234{
2235 if (strlencmp(cmd, "disable") != 0)
2236 return 0;
2237
2238 if ((*rc = fan_set_disable()) == -ENXIO)
2239 printk(IBM_ERR "disable command accepted for unsupported "
2240 "access mode %d", fan_control_access_mode);
2241
2242 return 1;
2243}
2244
2245static int fan_write_cmd_speed(const char *cmd, int *rc)
2246{
2247 int speed;
2248
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02002249 /* TODO:
2250 * Support speed <low> <medium> <high> ? */
2251
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002252 if (sscanf(cmd, "speed %d", &speed) != 1)
2253 return 0;
2254
2255 if ((*rc = fan_set_speed(speed)) == -ENXIO)
2256 printk(IBM_ERR "speed command accepted for unsupported "
2257 "access mode %d", fan_control_access_mode);
2258
2259 return 1;
2260}
2261
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002262static int fan_write_cmd_watchdog(const char *cmd, int *rc)
2263{
2264 int interval;
2265
2266 if (sscanf(cmd, "watchdog %d", &interval) != 1)
2267 return 0;
2268
2269 if (interval < 0 || interval > 120)
2270 *rc = -EINVAL;
2271 else
2272 fan_watchdog_maxinterval = interval;
2273
2274 return 1;
2275}
2276
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002277static int fan_write(char *buf)
2278{
2279 char *cmd;
2280 int rc = 0;
2281
2282 while (!rc && (cmd = next_cmd(&buf))) {
2283 if (!((fan_control_commands & IBMACPI_FAN_CMD_LEVEL) &&
2284 fan_write_cmd_level(cmd, &rc)) &&
2285 !((fan_control_commands & IBMACPI_FAN_CMD_ENABLE) &&
2286 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002287 fan_write_cmd_disable(cmd, &rc) ||
2288 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002289 !((fan_control_commands & IBMACPI_FAN_CMD_SPEED) &&
2290 fan_write_cmd_speed(cmd, &rc))
2291 )
2292 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002293 else if (!rc)
2294 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002295 }
2296
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002297 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002298}
2299
Len Brown25c68a32006-12-08 04:43:41 -05002300static void fan_watchdog_fire(struct work_struct *ignored)
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002301{
2302 printk(IBM_NOTICE "fan watchdog: enabling fan\n");
2303 if (fan_set_enable()) {
2304 printk(IBM_ERR "fan watchdog: error while enabling fan\n");
2305 /* reschedule for later */
2306 fan_watchdog_reset();
2307 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308}
2309
2310static struct ibm_struct ibms[] = {
2311 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002312 .name = "driver",
Adrian Bunk1f217822006-12-19 13:01:28 -08002313 .init = ibm_acpi_driver_init,
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002314 .read = driver_read,
2315 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002317 .name = "hotkey",
2318 .hid = IBM_HKEY_HID,
2319 .init = hotkey_init,
2320 .read = hotkey_read,
2321 .write = hotkey_write,
2322 .exit = hotkey_exit,
2323 .notify = hotkey_notify,
2324 .handle = &hkey_handle,
2325 .type = ACPI_DEVICE_NOTIFY,
2326 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002328 .name = "bluetooth",
2329 .init = bluetooth_init,
2330 .read = bluetooth_read,
2331 .write = bluetooth_write,
2332 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 {
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002334 .name = "wan",
2335 .init = wan_init,
2336 .read = wan_read,
2337 .write = wan_write,
2338 .experimental = 1,
2339 },
2340 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002341 .name = "video",
2342 .init = video_init,
2343 .read = video_read,
2344 .write = video_write,
2345 .exit = video_exit,
2346 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002348 .name = "light",
2349 .init = light_init,
2350 .read = light_read,
2351 .write = light_write,
2352 },
Kristen Accardi63e5f242006-02-23 17:56:06 -08002353#ifdef CONFIG_ACPI_IBM_DOCK
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002355 .name = "dock",
2356 .read = dock_read,
2357 .write = dock_write,
2358 .notify = dock_notify,
2359 .handle = &dock_handle,
2360 .type = ACPI_SYSTEM_NOTIFY,
2361 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002363 .name = "dock",
2364 .hid = IBM_PCI_HID,
2365 .notify = dock_notify,
2366 .handle = &pci_handle,
2367 .type = ACPI_SYSTEM_NOTIFY,
2368 },
Kristen Accardi63e5f242006-02-23 17:56:06 -08002369#endif
Henrique de Moraes Holschuh2bc808a2007-02-21 13:05:38 -02002370#ifdef CONFIG_ACPI_IBM_BAY
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002372 .name = "bay",
2373 .init = bay_init,
2374 .read = bay_read,
2375 .write = bay_write,
2376 .notify = bay_notify,
2377 .handle = &bay_handle,
2378 .type = ACPI_SYSTEM_NOTIFY,
2379 },
Henrique de Moraes Holschuh2bc808a2007-02-21 13:05:38 -02002380#endif /* CONFIG_ACPI_IBM_BAY */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002382 .name = "cmos",
2383 .read = cmos_read,
2384 .write = cmos_write,
2385 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002387 .name = "led",
2388 .init = led_init,
2389 .read = led_read,
2390 .write = led_write,
2391 },
2392 {
2393 .name = "beep",
2394 .read = beep_read,
2395 .write = beep_write,
2396 },
2397 {
2398 .name = "thermal",
2399 .init = thermal_init,
2400 .read = thermal_read,
2401 },
2402 {
2403 .name = "ecdump",
2404 .read = ecdump_read,
2405 .write = ecdump_write,
2406 .experimental = 1,
2407 },
2408 {
2409 .name = "brightness",
2410 .read = brightness_read,
2411 .write = brightness_write,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02002412 .init = brightness_init,
2413 .exit = brightness_exit,
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002414 },
2415 {
2416 .name = "volume",
2417 .read = volume_read,
2418 .write = volume_write,
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002419 },
2420 {
2421 .name = "fan",
2422 .read = fan_read,
2423 .write = fan_write,
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002424 .init = fan_init,
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002425 .exit = fan_exit,
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002426 .experimental = 1,
2427 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429
2430static int dispatch_read(char *page, char **start, off_t off, int count,
2431 int *eof, void *data)
2432{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02002433 struct ibm_struct *ibm = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 int len;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002435
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 if (!ibm || !ibm->read)
2437 return -EINVAL;
2438
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002439 len = ibm->read(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 if (len < 0)
2441 return len;
2442
2443 if (len <= off + count)
2444 *eof = 1;
2445 *start = page + off;
2446 len -= off;
2447 if (len > count)
2448 len = count;
2449 if (len < 0)
2450 len = 0;
2451
2452 return len;
2453}
2454
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002455static int dispatch_write(struct file *file, const char __user * userbuf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 unsigned long count, void *data)
2457{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02002458 struct ibm_struct *ibm = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 char *kernbuf;
2460 int ret;
2461
2462 if (!ibm || !ibm->write)
2463 return -EINVAL;
2464
2465 kernbuf = kmalloc(count + 2, GFP_KERNEL);
2466 if (!kernbuf)
2467 return -ENOMEM;
2468
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002469 if (copy_from_user(kernbuf, userbuf, count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 kfree(kernbuf);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002471 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 }
2473
2474 kernbuf[count] = 0;
2475 strcat(kernbuf, ",");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002476 ret = ibm->write(kernbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 if (ret == 0)
2478 ret = count;
2479
2480 kfree(kernbuf);
2481
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002482 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483}
2484
2485static void dispatch_notify(acpi_handle handle, u32 event, void *data)
2486{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02002487 struct ibm_struct *ibm = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488
2489 if (!ibm || !ibm->notify)
2490 return;
2491
2492 ibm->notify(ibm, event);
2493}
2494
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002495static int __init setup_notify(struct ibm_struct *ibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496{
2497 acpi_status status;
2498 int ret;
2499
2500 if (!*ibm->handle)
2501 return 0;
2502
2503 ret = acpi_bus_get_device(*ibm->handle, &ibm->device);
2504 if (ret < 0) {
2505 printk(IBM_ERR "%s device not present\n", ibm->name);
2506 return 0;
2507 }
2508
2509 acpi_driver_data(ibm->device) = ibm;
2510 sprintf(acpi_device_class(ibm->device), "%s/%s", IBM_NAME, ibm->name);
2511
2512 status = acpi_install_notify_handler(*ibm->handle, ibm->type,
2513 dispatch_notify, ibm);
2514 if (ACPI_FAILURE(status)) {
2515 printk(IBM_ERR "acpi_install_notify_handler(%s) failed: %d\n",
2516 ibm->name, status);
2517 return -ENODEV;
2518 }
Alexey Starikovskiy4afaf542006-12-18 14:53:33 -03002519 ibm->notify_installed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 return 0;
2521}
2522
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002523static int __init ibm_device_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524{
2525 return 0;
2526}
2527
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002528static int __init register_driver(struct ibm_struct *ibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529{
2530 int ret;
2531
Burman Yan36bcbec2006-12-19 12:56:11 -08002532 ibm->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 if (!ibm->driver) {
2534 printk(IBM_ERR "kmalloc(ibm->driver) failed\n");
2535 return -1;
2536 }
2537
Henrique de Moraes Holschuh3dfd35c2006-11-24 10:32:32 -02002538 sprintf(ibm->driver->name, "%s_%s", IBM_NAME, ibm->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 ibm->driver->ids = ibm->hid;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002540 ibm->driver->ops.add = &ibm_device_add;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541
2542 ret = acpi_bus_register_driver(ibm->driver);
2543 if (ret < 0) {
2544 printk(IBM_ERR "acpi_bus_register_driver(%s) failed: %d\n",
2545 ibm->hid, ret);
2546 kfree(ibm->driver);
2547 }
2548
2549 return ret;
2550}
2551
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002552static int __init ibm_init(struct ibm_struct *ibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553{
2554 int ret;
2555 struct proc_dir_entry *entry;
2556
2557 if (ibm->experimental && !experimental)
2558 return 0;
2559
2560 if (ibm->hid) {
2561 ret = register_driver(ibm);
2562 if (ret < 0)
2563 return ret;
2564 ibm->driver_registered = 1;
2565 }
2566
2567 if (ibm->init) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002568 ret = ibm->init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 if (ret != 0)
2570 return ret;
2571 ibm->init_called = 1;
2572 }
2573
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002574 if (ibm->read) {
2575 entry = create_proc_entry(ibm->name,
2576 S_IFREG | S_IRUGO | S_IWUSR,
2577 proc_dir);
2578 if (!entry) {
2579 printk(IBM_ERR "unable to create proc entry %s\n",
2580 ibm->name);
2581 return -ENODEV;
2582 }
2583 entry->owner = THIS_MODULE;
2584 entry->data = ibm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 entry->read_proc = &dispatch_read;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002586 if (ibm->write)
2587 entry->write_proc = &dispatch_write;
2588 ibm->proc_created = 1;
2589 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590
2591 if (ibm->notify) {
2592 ret = setup_notify(ibm);
2593 if (ret < 0)
2594 return ret;
2595 }
2596
2597 return 0;
2598}
2599
2600static void ibm_exit(struct ibm_struct *ibm)
2601{
2602 if (ibm->notify_installed)
2603 acpi_remove_notify_handler(*ibm->handle, ibm->type,
2604 dispatch_notify);
2605
2606 if (ibm->proc_created)
2607 remove_proc_entry(ibm->name, proc_dir);
2608
2609 if (ibm->init_called && ibm->exit)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002610 ibm->exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611
2612 if (ibm->driver_registered) {
2613 acpi_bus_unregister_driver(ibm->driver);
2614 kfree(ibm->driver);
2615 }
2616}
2617
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002618static void __init ibm_handle_init(char *name,
2619 acpi_handle * handle, acpi_handle parent,
2620 char **paths, int num_paths, char **path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621{
2622 int i;
2623 acpi_status status;
2624
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002625 for (i = 0; i < num_paths; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 status = acpi_get_handle(parent, paths[i], handle);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002627 if (ACPI_SUCCESS(status)) {
2628 *path = paths[i];
2629 return;
2630 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002632
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 *handle = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634}
2635
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002636#define IBM_HANDLE_INIT(object) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 ibm_handle_init(#object, &object##_handle, *object##_parent, \
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002638 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639
Henrique de Moraes Holschuh3945ac32007-02-06 19:13:44 -02002640static int __init set_ibm_param(const char *val, struct kernel_param *kp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641{
2642 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002644 for (i = 0; i < ARRAY_SIZE(ibms); i++)
2645 if (strcmp(ibms[i].name, kp->name) == 0 && ibms[i].write) {
2646 if (strlen(val) > sizeof(ibms[i].param) - 2)
2647 return -ENOSPC;
2648 strcpy(ibms[i].param, val);
2649 strcat(ibms[i].param, ",");
2650 return 0;
2651 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 return -EINVAL;
2654}
2655
2656#define IBM_PARAM(feature) \
2657 module_param_call(feature, set_ibm_param, NULL, NULL, 0)
2658
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002659IBM_PARAM(hotkey);
2660IBM_PARAM(bluetooth);
2661IBM_PARAM(video);
2662IBM_PARAM(light);
Kristen Accardi63e5f242006-02-23 17:56:06 -08002663#ifdef CONFIG_ACPI_IBM_DOCK
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002664IBM_PARAM(dock);
Kristen Accardi63e5f242006-02-23 17:56:06 -08002665#endif
Henrique de Moraes Holschuh2bc808a2007-02-21 13:05:38 -02002666#ifdef CONFIG_ACPI_IBM_BAY
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002667IBM_PARAM(bay);
Henrique de Moraes Holschuh2bc808a2007-02-21 13:05:38 -02002668#endif /* CONFIG_ACPI_IBM_BAY */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002669IBM_PARAM(cmos);
2670IBM_PARAM(led);
2671IBM_PARAM(beep);
2672IBM_PARAM(ecdump);
2673IBM_PARAM(brightness);
2674IBM_PARAM(volume);
2675IBM_PARAM(fan);
2676
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677static void acpi_ibm_exit(void)
2678{
2679 int i;
2680
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002681 for (i = ARRAY_SIZE(ibms) - 1; i >= 0; i--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 ibm_exit(&ibms[i]);
2683
Henrique de Moraes Holschuh3945ac32007-02-06 19:13:44 -02002684 if (proc_dir)
2685 remove_proc_entry(IBM_DIR, acpi_root_dir);
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02002686
2687 if (ibm_thinkpad_ec_found)
2688 kfree(ibm_thinkpad_ec_found);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689}
2690
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02002691static char* __init check_dmi_for_ec(void)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002692{
2693 struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02002694 char ec_fw_string[18];
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002695
2696 /*
2697 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
2698 * X32 or newer, all Z series; Some models must have an
2699 * up-to-date BIOS or they will not be detected.
2700 *
2701 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
2702 */
2703 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02002704 if (sscanf(dev->name,
2705 "IBM ThinkPad Embedded Controller -[%17c",
2706 ec_fw_string) == 1) {
2707 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
2708 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
2709 return kstrdup(ec_fw_string, GFP_KERNEL);
2710 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002711 }
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02002712 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713}
2714
2715static int __init acpi_ibm_init(void)
2716{
2717 int ret, i;
2718
2719 if (acpi_disabled)
2720 return -ENODEV;
2721
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002722 /* ec is required because many other handles are relative to it */
2723 IBM_HANDLE_INIT(ec);
2724 if (!ec_handle) {
2725 printk(IBM_ERR "ec object not found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 return -ENODEV;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002727 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002729 /* Models with newer firmware report the EC in DMI */
2730 ibm_thinkpad_ec_found = check_dmi_for_ec();
2731
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 /* these handles are not required */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002733 IBM_HANDLE_INIT(vid);
2734 IBM_HANDLE_INIT(vid2);
2735 IBM_HANDLE_INIT(ledb);
2736 IBM_HANDLE_INIT(led);
2737 IBM_HANDLE_INIT(hkey);
2738 IBM_HANDLE_INIT(lght);
2739 IBM_HANDLE_INIT(cmos);
Kristen Accardi63e5f242006-02-23 17:56:06 -08002740#ifdef CONFIG_ACPI_IBM_DOCK
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002741 IBM_HANDLE_INIT(dock);
Kristen Accardi63e5f242006-02-23 17:56:06 -08002742#endif
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002743 IBM_HANDLE_INIT(pci);
Henrique de Moraes Holschuh2bc808a2007-02-21 13:05:38 -02002744#ifdef CONFIG_ACPI_IBM_BAY
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002745 IBM_HANDLE_INIT(bay);
2746 if (bay_handle)
2747 IBM_HANDLE_INIT(bay_ej);
2748 IBM_HANDLE_INIT(bay2);
2749 if (bay2_handle)
2750 IBM_HANDLE_INIT(bay2_ej);
Henrique de Moraes Holschuh2bc808a2007-02-21 13:05:38 -02002751#endif /* CONFIG_ACPI_IBM_BAY */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002752 IBM_HANDLE_INIT(beep);
2753 IBM_HANDLE_INIT(ecrd);
2754 IBM_HANDLE_INIT(ecwr);
2755 IBM_HANDLE_INIT(fans);
2756 IBM_HANDLE_INIT(gfan);
2757 IBM_HANDLE_INIT(sfan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758
2759 proc_dir = proc_mkdir(IBM_DIR, acpi_root_dir);
2760 if (!proc_dir) {
2761 printk(IBM_ERR "unable to create proc dir %s", IBM_DIR);
Henrique de Moraes Holschuh3945ac32007-02-06 19:13:44 -02002762 acpi_ibm_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 return -ENODEV;
2764 }
2765 proc_dir->owner = THIS_MODULE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002766
2767 for (i = 0; i < ARRAY_SIZE(ibms); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 ret = ibm_init(&ibms[i]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002769 if (ret >= 0 && *ibms[i].param)
2770 ret = ibms[i].write(ibms[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 if (ret < 0) {
2772 acpi_ibm_exit();
2773 return ret;
2774 }
2775 }
2776
2777 return 0;
2778}
2779
2780module_init(acpi_ibm_init);
2781module_exit(acpi_ibm_exit);