blob: dc1096608f4363bf0dd32312c55925b4fe168452 [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>
Henrique de Moraes Holschuhc9bf2962007-03-08 05:28:15 -030089#include <linux/fb.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070090#include <asm/uaccess.h>
91
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -020092#include <linux/dmi.h>
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -020093#include <linux/jiffies.h>
94#include <linux/workqueue.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96#include <acpi/acpi_drivers.h>
97#include <acpi/acnamesp.h>
98
99#define IBM_NAME "ibm"
100#define IBM_DESC "IBM ThinkPad ACPI Extras"
101#define IBM_FILE "ibm_acpi"
102#define IBM_URL "http://ibm-acpi.sf.net/"
103
Henrique de Moraes Holschuhf9ff43a2006-11-25 16:37:38 -0200104MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400105MODULE_DESCRIPTION(IBM_DESC);
106MODULE_VERSION(IBM_VERSION);
107MODULE_LICENSE("GPL");
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109#define IBM_DIR IBM_NAME
110
111#define IBM_LOG IBM_FILE ": "
112#define IBM_ERR KERN_ERR IBM_LOG
113#define IBM_NOTICE KERN_NOTICE IBM_LOG
114#define IBM_INFO KERN_INFO IBM_LOG
115#define IBM_DEBUG KERN_DEBUG IBM_LOG
116
117#define IBM_MAX_ACPI_ARGS 3
118
119#define __unused __attribute__ ((unused))
120
121static int experimental;
122module_param(experimental, int, 0);
123
124static acpi_handle root_handle = NULL;
125
126#define IBM_HANDLE(object, parent, paths...) \
127 static acpi_handle object##_handle; \
128 static acpi_handle *object##_parent = &parent##_handle; \
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400129 static char *object##_path; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 static char *object##_paths[] = { paths }
131
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400132IBM_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
133 "\\_SB.PCI.ISA.EC", /* 570 */
134 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
135 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
136 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
137 "\\_SB.PCI0.ICH3.EC0", /* R31 */
138 "\\_SB.PCI0.LPC.EC", /* all others */
139 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400141IBM_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
142 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
143 "\\_SB.PCI0.VID0", /* 770e */
144 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
145 "\\_SB.PCI0.AGP.VID", /* all others */
146 ); /* R30, R31 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400148IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400150IBM_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, T4x, X31, X40 */
151 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
152 "\\CMS", /* R40, R40e */
153 ); /* all others */
Kristen Accardi63e5f242006-02-23 17:56:06 -0800154#ifdef CONFIG_ACPI_IBM_DOCK
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400155IBM_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
156 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
157 "\\_SB.PCI0.PCI1.DOCK", /* all others */
158 "\\_SB.PCI.ISA.SLCE", /* 570 */
159 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
Kristen Accardi63e5f242006-02-23 17:56:06 -0800160#endif
Henrique de Moraes Holschuh2bc808a2007-02-21 13:05:38 -0200161#ifdef CONFIG_ACPI_IBM_BAY
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400162IBM_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
163 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhe0298992006-11-25 16:35:09 -0200164 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400165 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
166 ); /* A21e, R30, R31 */
167
168IBM_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
169 "_EJ0", /* all others */
170 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
171
172IBM_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
173 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
174 ); /* all others */
175
176IBM_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
177 "_EJ0", /* 770x */
178 ); /* all others */
Henrique de Moraes Holschuh2bc808a2007-02-21 13:05:38 -0200179#endif /* CONFIG_ACPI_IBM_BAY */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400180
181/* don't list other alternatives as we install a notify handler on the 570 */
182IBM_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
183
184IBM_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
185 "^HKEY", /* R30, R31 */
186 "HKEY", /* all others */
187 ); /* 570 */
188
189IBM_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
190IBM_HANDLE(ledb, ec, "LEDB"); /* G4x */
191
192IBM_HANDLE(led, ec, "SLED", /* 570 */
193 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
194 "LED", /* all others */
195 ); /* R30, R31 */
196
197IBM_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
198IBM_HANDLE(ecrd, ec, "ECRD"); /* 570 */
199IBM_HANDLE(ecwr, ec, "ECWR"); /* 570 */
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200200IBM_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400201
202IBM_HANDLE(gfan, ec, "GFAN", /* 570 */
203 "\\FSPD", /* 600e/x, 770e, 770x */
204 ); /* all others */
205
206IBM_HANDLE(sfan, ec, "SFAN", /* 570 */
207 "JFNS", /* 770x-JL */
208 ); /* all others */
209
210#define IBM_HKEY_HID "IBM0068"
211#define IBM_PCI_HID "PNP0A03"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -0200213enum thermal_access_mode {
214 IBMACPI_THERMAL_NONE = 0, /* No thermal support */
215 IBMACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
216 IBMACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -0200217 IBMACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
218 IBMACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -0200219};
220
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -0200221#define IBMACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -0200222struct ibm_thermal_sensors_struct {
223 s32 temp[IBMACPI_MAX_THERMAL_SENSORS];
224};
225
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200226/*
227 * FAN ACCESS MODES
228 *
229 * IBMACPI_FAN_RD_ACPI_GFAN:
230 * ACPI GFAN method: returns fan level
231 *
232 * see IBMACPI_FAN_WR_ACPI_SFAN
233 * EC 0x2f not available if GFAN exists
234 *
235 * IBMACPI_FAN_WR_ACPI_SFAN:
236 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
237 *
238 * EC 0x2f might be available *for reading*, but never for writing.
239 *
240 * IBMACPI_FAN_WR_TPEC:
241 * ThinkPad EC register 0x2f (HFSP): fan control loop mode Supported
242 * on almost all ThinkPads
243 *
244 * Fan speed changes of any sort (including those caused by the
245 * disengaged mode) are usually done slowly by the firmware as the
246 * maximum ammount of fan duty cycle change per second seems to be
247 * limited.
248 *
249 * Reading is not available if GFAN exists.
250 * Writing is not available if SFAN exists.
251 *
252 * Bits
253 * 7 automatic mode engaged;
254 * (default operation mode of the ThinkPad)
255 * fan level is ignored in this mode.
256 * 6 disengage mode (takes precedence over bit 7);
257 * not available on all thinkpads. May disable
258 * the tachometer, and speeds up fan to 100% duty-cycle,
259 * which speeds it up far above the standard RPM
260 * levels. It is not impossible that it could cause
261 * hardware damage.
262 * 5-3 unused in some models. Extra bits for fan level
263 * in others, but still useless as all values above
264 * 7 map to the same speed as level 7 in these models.
265 * 2-0 fan level (0..7 usually)
266 * 0x00 = stop
267 * 0x07 = max (set when temperatures critical)
268 * Some ThinkPads may have other levels, see
269 * IBMACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
270 *
271 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
272 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
273 * does so, its initial value is meaningless (0x07).
274 *
275 * For firmware bugs, refer to:
276 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
277 *
278 * ----
279 *
280 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
281 * Main fan tachometer reading (in RPM)
282 *
283 * This register is present on all ThinkPads with a new-style EC, and
284 * it is known not to be present on the A21m/e, and T22, as there is
285 * something else in offset 0x84 according to the ACPI DSDT. Other
286 * ThinkPads from this same time period (and earlier) probably lack the
287 * tachometer as well.
288 *
289 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
290 * was never fixed by IBM to report the EC firmware version string
291 * probably support the tachometer (like the early X models), so
292 * detecting it is quite hard. We need more data to know for sure.
293 *
294 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
295 * might result.
296 *
297 * FIRMWARE BUG: when EC 0x2f bit 6 is set (disengaged mode), this
298 * register is not invalidated in ThinkPads that disable tachometer
299 * readings. Thus, the tachometer readings go stale.
300 *
301 * For firmware bugs, refer to:
302 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
303 *
304 * IBMACPI_FAN_WR_ACPI_FANS:
305 * ThinkPad X31, X40, X41. Not available in the X60.
306 *
307 * FANS ACPI handle: takes three arguments: low speed, medium speed,
308 * high speed. ACPI DSDT seems to map these three speeds to levels
309 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
310 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
311 *
312 * The speeds are stored on handles
313 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
314 *
315 * There are three default speed sets, acessible as handles:
316 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
317 *
318 * ACPI DSDT switches which set is in use depending on various
319 * factors.
320 *
321 * IBMACPI_FAN_WR_TPEC is also available and should be used to
322 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
323 * but the ACPI tables just mention level 7.
324 */
325
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -0200326enum fan_status_access_mode {
327 IBMACPI_FAN_NONE = 0, /* No fan status or control */
328 IBMACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
329 IBMACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
330};
331
332enum fan_control_access_mode {
333 IBMACPI_FAN_WR_NONE = 0, /* No fan control */
334 IBMACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
335 IBMACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
336 IBMACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
337};
338
339enum fan_control_commands {
340 IBMACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
341 IBMACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -0200342 IBMACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
343 * and also watchdog cmd */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -0200344};
345
346enum { /* Fan control constants */
347 fan_status_offset = 0x2f, /* EC register 0x2f */
348 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
349 * 0x84 must be read before 0x85 */
Henrique de Moraes Holschuhbab812a2006-11-24 11:47:12 -0200350
351 IBMACPI_FAN_EC_DISENGAGED = 0x40, /* EC mode: tachometer
352 * disengaged */
353 IBMACPI_FAN_EC_AUTO = 0x80, /* EC mode: auto fan
354 * control */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -0200355};
356
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -0200357static char *ibm_thinkpad_ec_found = NULL;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -0200358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359struct ibm_struct {
360 char *name;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400361 char param[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363 char *hid;
364 struct acpi_driver *driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400366 int (*init) (void);
367 int (*read) (char *);
368 int (*write) (char *);
369 void (*exit) (void);
370
371 void (*notify) (struct ibm_struct *, u32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 acpi_handle *handle;
373 int type;
374 struct acpi_device *device;
375
376 int driver_registered;
377 int proc_created;
378 int init_called;
379 int notify_installed;
380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 int experimental;
382};
383
384static struct proc_dir_entry *proc_dir = NULL;
385
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -0200386static struct backlight_device *ibm_backlight_device = NULL;
Holger Macht8acb0252006-10-20 14:30:28 -0700387
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388#define onoff(status,bit) ((status) & (1 << (bit)) ? "on" : "off")
389#define enabled(status,bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
390#define strlencmp(a,b) (strncmp((a), (b), strlen(b)))
391
392static int acpi_evalf(acpi_handle handle,
393 void *res, char *method, char *fmt, ...)
394{
395 char *fmt0 = fmt;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400396 struct acpi_object_list params;
397 union acpi_object in_objs[IBM_MAX_ACPI_ARGS];
398 struct acpi_buffer result, *resultp;
399 union acpi_object out_obj;
400 acpi_status status;
401 va_list ap;
402 char res_type;
403 int success;
404 int quiet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
406 if (!*fmt) {
407 printk(IBM_ERR "acpi_evalf() called with empty format\n");
408 return 0;
409 }
410
411 if (*fmt == 'q') {
412 quiet = 1;
413 fmt++;
414 } else
415 quiet = 0;
416
417 res_type = *(fmt++);
418
419 params.count = 0;
420 params.pointer = &in_objs[0];
421
422 va_start(ap, fmt);
423 while (*fmt) {
424 char c = *(fmt++);
425 switch (c) {
426 case 'd': /* int */
427 in_objs[params.count].integer.value = va_arg(ap, int);
428 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
429 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400430 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 default:
432 printk(IBM_ERR "acpi_evalf() called "
433 "with invalid format character '%c'\n", c);
434 return 0;
435 }
436 }
437 va_end(ap);
438
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400439 if (res_type != 'v') {
440 result.length = sizeof(out_obj);
441 result.pointer = &out_obj;
442 resultp = &result;
443 } else
444 resultp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400446 status = acpi_evaluate_object(handle, method, &params, resultp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
448 switch (res_type) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400449 case 'd': /* int */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 if (res)
451 *(int *)res = out_obj.integer.value;
452 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
453 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400454 case 'v': /* void */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 success = status == AE_OK;
456 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400457 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 default:
459 printk(IBM_ERR "acpi_evalf() called "
460 "with invalid format character '%c'\n", res_type);
461 return 0;
462 }
463
464 if (!success && !quiet)
465 printk(IBM_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
466 method, fmt0, status);
467
468 return success;
469}
470
471static void __unused acpi_print_int(acpi_handle handle, char *method)
472{
473 int i;
474
475 if (acpi_evalf(handle, &i, method, "d"))
476 printk(IBM_INFO "%s = 0x%x\n", method, i);
477 else
478 printk(IBM_ERR "error calling %s\n", method);
479}
480
481static char *next_cmd(char **cmds)
482{
483 char *start = *cmds;
484 char *end;
485
486 while ((end = strchr(start, ',')) && end == start)
487 start = end + 1;
488
489 if (!end)
490 return NULL;
491
492 *end = 0;
493 *cmds = end + 1;
494 return start;
495}
496
Adrian Bunk1f217822006-12-19 13:01:28 -0800497static int ibm_acpi_driver_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
499 printk(IBM_INFO "%s v%s\n", IBM_DESC, IBM_VERSION);
500 printk(IBM_INFO "%s\n", IBM_URL);
501
Henrique de Moraes Holschuh3945ac32007-02-06 19:13:44 -0200502 if (ibm_thinkpad_ec_found)
503 printk(IBM_INFO "ThinkPad EC firmware %s\n",
504 ibm_thinkpad_ec_found);
505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 return 0;
507}
508
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400509static int driver_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510{
511 int len = 0;
512
513 len += sprintf(p + len, "driver:\t\t%s\n", IBM_DESC);
514 len += sprintf(p + len, "version:\t%s\n", IBM_VERSION);
515
516 return len;
517}
518
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400519static int hotkey_supported;
520static int hotkey_mask_supported;
521static int hotkey_orig_status;
522static int hotkey_orig_mask;
523
524static int hotkey_get(int *status, int *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525{
526 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400527 return 0;
528
529 if (hotkey_mask_supported)
530 if (!acpi_evalf(hkey_handle, mask, "DHKN", "d"))
531 return 0;
532
533 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534}
535
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400536static int hotkey_set(int status, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537{
538 int i;
539
540 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 return 0;
542
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400543 if (hotkey_mask_supported)
544 for (i = 0; i < 32; i++) {
545 int bit = ((1 << i) & mask) != 0;
546 if (!acpi_evalf(hkey_handle,
547 NULL, "MHKM", "vdd", i + 1, bit))
548 return 0;
549 }
550
551 return 1;
552}
553
554static int hotkey_init(void)
555{
556 /* hotkey not supported on 570 */
557 hotkey_supported = hkey_handle != NULL;
558
559 if (hotkey_supported) {
560 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
561 A30, R30, R31, T20-22, X20-21, X22-24 */
562 hotkey_mask_supported =
563 acpi_evalf(hkey_handle, NULL, "DHKN", "qv");
564
565 if (!hotkey_get(&hotkey_orig_status, &hotkey_orig_mask))
566 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 }
568
569 return 0;
570}
571
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400572static int hotkey_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573{
574 int status, mask;
575 int len = 0;
576
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400577 if (!hotkey_supported) {
578 len += sprintf(p + len, "status:\t\tnot supported\n");
579 return len;
580 }
581
582 if (!hotkey_get(&status, &mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 return -EIO;
584
585 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400586 if (hotkey_mask_supported) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 len += sprintf(p + len, "mask:\t\t0x%04x\n", mask);
588 len += sprintf(p + len,
589 "commands:\tenable, disable, reset, <mask>\n");
590 } else {
591 len += sprintf(p + len, "mask:\t\tnot supported\n");
592 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
593 }
594
595 return len;
596}
597
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400598static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599{
600 int status, mask;
601 char *cmd;
602 int do_cmd = 0;
603
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400604 if (!hotkey_supported)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 return -ENODEV;
606
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400607 if (!hotkey_get(&status, &mask))
608 return -EIO;
609
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 while ((cmd = next_cmd(&buf))) {
611 if (strlencmp(cmd, "enable") == 0) {
612 status = 1;
613 } else if (strlencmp(cmd, "disable") == 0) {
614 status = 0;
615 } else if (strlencmp(cmd, "reset") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400616 status = hotkey_orig_status;
617 mask = hotkey_orig_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
619 /* mask set */
620 } else if (sscanf(cmd, "%x", &mask) == 1) {
621 /* mask set */
622 } else
623 return -EINVAL;
624 do_cmd = 1;
625 }
626
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400627 if (do_cmd && !hotkey_set(status, mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 return -EIO;
629
630 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400631}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400633static void hotkey_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400635 if (hotkey_supported)
636 hotkey_set(hotkey_orig_status, hotkey_orig_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637}
638
639static void hotkey_notify(struct ibm_struct *ibm, u32 event)
640{
641 int hkey;
642
643 if (acpi_evalf(hkey_handle, &hkey, "MHKP", "d"))
644 acpi_bus_generate_event(ibm->device, event, hkey);
645 else {
646 printk(IBM_ERR "unknown hotkey event %d\n", event);
647 acpi_bus_generate_event(ibm->device, event, 0);
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400648 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649}
650
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400651static int bluetooth_supported;
652
653static int bluetooth_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400655 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
656 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
657 bluetooth_supported = hkey_handle &&
658 acpi_evalf(hkey_handle, NULL, "GBDC", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
660 return 0;
661}
662
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400663static int bluetooth_status(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664{
665 int status;
666
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400667 if (!bluetooth_supported ||
668 !acpi_evalf(hkey_handle, &status, "GBDC", "d"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 status = 0;
670
671 return status;
672}
673
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400674static int bluetooth_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
676 int len = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400677 int status = bluetooth_status();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400679 if (!bluetooth_supported)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 len += sprintf(p + len, "status:\t\tnot supported\n");
681 else if (!(status & 1))
682 len += sprintf(p + len, "status:\t\tnot installed\n");
683 else {
684 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 1));
685 len += sprintf(p + len, "commands:\tenable, disable\n");
686 }
687
688 return len;
689}
690
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400691static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400693 int status = bluetooth_status();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 char *cmd;
695 int do_cmd = 0;
696
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400697 if (!bluetooth_supported)
698 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
700 while ((cmd = next_cmd(&buf))) {
701 if (strlencmp(cmd, "enable") == 0) {
702 status |= 2;
703 } else if (strlencmp(cmd, "disable") == 0) {
704 status &= ~2;
705 } else
706 return -EINVAL;
707 do_cmd = 1;
708 }
709
710 if (do_cmd && !acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400711 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
713 return 0;
714}
715
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -0400716static int wan_supported;
717
718static int wan_init(void)
719{
720 wan_supported = hkey_handle &&
721 acpi_evalf(hkey_handle, NULL, "GWAN", "qv");
722
723 return 0;
724}
725
726static int wan_status(void)
727{
728 int status;
729
Henrique de Moraes Holschuh8d297262006-11-24 11:47:07 -0200730 if (!wan_supported || !acpi_evalf(hkey_handle, &status, "GWAN", "d"))
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -0400731 status = 0;
732
733 return status;
734}
735
736static int wan_read(char *p)
737{
738 int len = 0;
739 int status = wan_status();
740
741 if (!wan_supported)
742 len += sprintf(p + len, "status:\t\tnot supported\n");
743 else if (!(status & 1))
744 len += sprintf(p + len, "status:\t\tnot installed\n");
745 else {
746 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 1));
747 len += sprintf(p + len, "commands:\tenable, disable\n");
748 }
749
750 return len;
751}
752
753static int wan_write(char *buf)
754{
755 int status = wan_status();
756 char *cmd;
757 int do_cmd = 0;
758
759 if (!wan_supported)
760 return -ENODEV;
761
762 while ((cmd = next_cmd(&buf))) {
763 if (strlencmp(cmd, "enable") == 0) {
764 status |= 2;
765 } else if (strlencmp(cmd, "disable") == 0) {
766 status &= ~2;
767 } else
768 return -EINVAL;
769 do_cmd = 1;
770 }
771
772 if (do_cmd && !acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
773 return -EIO;
774
775 return 0;
776}
777
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200778enum video_access_mode {
779 IBMACPI_VIDEO_NONE = 0,
780 IBMACPI_VIDEO_570, /* 570 */
781 IBMACPI_VIDEO_770, /* 600e/x, 770e, 770x */
782 IBMACPI_VIDEO_NEW, /* all others */
783};
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400784
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200785static enum video_access_mode video_supported;
786static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400787
788static int video_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400790 int ivga;
791
792 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
793 /* G41, assume IVGA doesn't change */
794 vid_handle = vid2_handle;
795
796 if (!vid_handle)
797 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200798 video_supported = IBMACPI_VIDEO_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400799 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
800 /* 570 */
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200801 video_supported = IBMACPI_VIDEO_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400802 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
803 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200804 video_supported = IBMACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400805 else
806 /* all others */
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200807 video_supported = IBMACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
809 return 0;
810}
811
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400812static int video_status(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813{
814 int status = 0;
815 int i;
816
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200817 if (video_supported == IBMACPI_VIDEO_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400818 if (acpi_evalf(NULL, &i, "\\_SB.PHS", "dd", 0x87))
819 status = i & 3;
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200820 } else if (video_supported == IBMACPI_VIDEO_770) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400821 if (acpi_evalf(NULL, &i, "\\VCDL", "d"))
822 status |= 0x01 * i;
823 if (acpi_evalf(NULL, &i, "\\VCDC", "d"))
824 status |= 0x02 * i;
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200825 } else if (video_supported == IBMACPI_VIDEO_NEW) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400826 acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1);
827 if (acpi_evalf(NULL, &i, "\\VCDC", "d"))
828 status |= 0x02 * i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400830 acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0);
831 if (acpi_evalf(NULL, &i, "\\VCDL", "d"))
832 status |= 0x01 * i;
833 if (acpi_evalf(NULL, &i, "\\VCDD", "d"))
834 status |= 0x08 * i;
835 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
837 return status;
838}
839
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400840static int video_autosw(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400842 int autosw = 0;
843
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200844 if (video_supported == IBMACPI_VIDEO_570)
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400845 acpi_evalf(vid_handle, &autosw, "SWIT", "d");
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200846 else if (video_supported == IBMACPI_VIDEO_770 ||
847 video_supported == IBMACPI_VIDEO_NEW)
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400848 acpi_evalf(vid_handle, &autosw, "^VDEE", "d");
849
850 return autosw & 1;
851}
852
853static int video_read(char *p)
854{
855 int status = video_status();
856 int autosw = video_autosw();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 int len = 0;
858
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400859 if (!video_supported) {
860 len += sprintf(p + len, "status:\t\tnot supported\n");
861 return len;
862 }
863
864 len += sprintf(p + len, "status:\t\tsupported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
866 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200867 if (video_supported == IBMACPI_VIDEO_NEW)
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400868 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
869 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
870 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
871 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200872 if (video_supported == IBMACPI_VIDEO_NEW)
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400873 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
874 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
876
877 return len;
878}
879
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400880static int video_switch(void)
881{
882 int autosw = video_autosw();
883 int ret;
884
885 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
886 return -EIO;
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200887 ret = video_supported == IBMACPI_VIDEO_570 ?
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400888 acpi_evalf(ec_handle, NULL, "_Q16", "v") :
889 acpi_evalf(vid_handle, NULL, "VSWT", "v");
890 acpi_evalf(vid_handle, NULL, "_DOS", "vd", autosw);
891
892 return ret;
893}
894
895static int video_expand(void)
896{
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200897 if (video_supported == IBMACPI_VIDEO_570)
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400898 return acpi_evalf(ec_handle, NULL, "_Q17", "v");
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200899 else if (video_supported == IBMACPI_VIDEO_770)
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400900 return acpi_evalf(vid_handle, NULL, "VEXP", "v");
901 else
902 return acpi_evalf(NULL, NULL, "\\VEXP", "v");
903}
904
905static int video_switch2(int status)
906{
907 int ret;
908
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200909 if (video_supported == IBMACPI_VIDEO_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400910 ret = acpi_evalf(NULL, NULL,
911 "\\_SB.PHS2", "vdd", 0x8b, status | 0x80);
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200912 } else if (video_supported == IBMACPI_VIDEO_770) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400913 int autosw = video_autosw();
914 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
915 return -EIO;
916
917 ret = acpi_evalf(vid_handle, NULL,
918 "ASWT", "vdd", status * 0x100, 0);
919
920 acpi_evalf(vid_handle, NULL, "_DOS", "vd", autosw);
921 } else {
922 ret = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
923 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
924 }
925
926 return ret;
927}
928
929static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930{
931 char *cmd;
932 int enable, disable, status;
933
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400934 if (!video_supported)
935 return -ENODEV;
936
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 enable = disable = 0;
938
939 while ((cmd = next_cmd(&buf))) {
940 if (strlencmp(cmd, "lcd_enable") == 0) {
941 enable |= 0x01;
942 } else if (strlencmp(cmd, "lcd_disable") == 0) {
943 disable |= 0x01;
944 } else if (strlencmp(cmd, "crt_enable") == 0) {
945 enable |= 0x02;
946 } else if (strlencmp(cmd, "crt_disable") == 0) {
947 disable |= 0x02;
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200948 } else if (video_supported == IBMACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400949 strlencmp(cmd, "dvi_enable") == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 enable |= 0x08;
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200951 } else if (video_supported == IBMACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400952 strlencmp(cmd, "dvi_disable") == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 disable |= 0x08;
954 } else if (strlencmp(cmd, "auto_enable") == 0) {
955 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
956 return -EIO;
957 } else if (strlencmp(cmd, "auto_disable") == 0) {
958 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 0))
959 return -EIO;
960 } else if (strlencmp(cmd, "video_switch") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400961 if (!video_switch())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 return -EIO;
963 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400964 if (!video_expand())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 return -EIO;
966 } else
967 return -EINVAL;
968 }
969
970 if (enable || disable) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400971 status = (video_status() & 0x0f & ~disable) | enable;
972 if (!video_switch2(status))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 return -EIO;
974 }
975
976 return 0;
977}
978
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400979static void video_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400981 acpi_evalf(vid_handle, NULL, "_DOS", "vd", video_orig_autosw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982}
983
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400984static int light_supported;
985static int light_status_supported;
986
987static int light_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400989 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
990 light_supported = (cmos_handle || lght_handle) && !ledb_handle;
991
992 if (light_supported)
993 /* light status not supported on
994 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
995 light_status_supported = acpi_evalf(ec_handle, NULL,
996 "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
998 return 0;
999}
1000
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001001static int light_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002{
1003 int len = 0;
1004 int status = 0;
1005
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001006 if (!light_supported) {
1007 len += sprintf(p + len, "status:\t\tnot supported\n");
1008 } else if (!light_status_supported) {
1009 len += sprintf(p + len, "status:\t\tunknown\n");
1010 len += sprintf(p + len, "commands:\ton, off\n");
1011 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
1013 return -EIO;
1014 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001015 len += sprintf(p + len, "commands:\ton, off\n");
1016 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
1018 return len;
1019}
1020
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001021static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022{
1023 int cmos_cmd, lght_cmd;
1024 char *cmd;
1025 int success;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001026
1027 if (!light_supported)
1028 return -ENODEV;
1029
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 while ((cmd = next_cmd(&buf))) {
1031 if (strlencmp(cmd, "on") == 0) {
1032 cmos_cmd = 0x0c;
1033 lght_cmd = 1;
1034 } else if (strlencmp(cmd, "off") == 0) {
1035 cmos_cmd = 0x0d;
1036 lght_cmd = 0;
1037 } else
1038 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001039
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 success = cmos_handle ?
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001041 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
1042 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 if (!success)
1044 return -EIO;
1045 }
1046
1047 return 0;
1048}
1049
Henrique de Moraes Holschuh2bc808a2007-02-21 13:05:38 -02001050#if defined(CONFIG_ACPI_IBM_DOCK) || defined(CONFIG_ACPI_IBM_BAY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051static int _sta(acpi_handle handle)
1052{
1053 int status;
1054
1055 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
1056 status = 0;
1057
1058 return status;
1059}
Henrique de Moraes Holschuh2bc808a2007-02-21 13:05:38 -02001060#endif
Henrique de Moraes Holschuhd2fadbb2007-01-11 02:58:15 -05001061
Kristen Accardi63e5f242006-02-23 17:56:06 -08001062#ifdef CONFIG_ACPI_IBM_DOCK
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063#define dock_docked() (_sta(dock_handle) & 1)
1064
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001065static int dock_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066{
1067 int len = 0;
1068 int docked = dock_docked();
1069
1070 if (!dock_handle)
1071 len += sprintf(p + len, "status:\t\tnot supported\n");
1072 else if (!docked)
1073 len += sprintf(p + len, "status:\t\tundocked\n");
1074 else {
1075 len += sprintf(p + len, "status:\t\tdocked\n");
1076 len += sprintf(p + len, "commands:\tdock, undock\n");
1077 }
1078
1079 return len;
1080}
1081
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001082static int dock_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083{
1084 char *cmd;
1085
1086 if (!dock_docked())
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001087 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
1089 while ((cmd = next_cmd(&buf))) {
1090 if (strlencmp(cmd, "undock") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001091 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
1092 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 return -EIO;
1094 } else if (strlencmp(cmd, "dock") == 0) {
1095 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
1096 return -EIO;
1097 } else
1098 return -EINVAL;
1099 }
1100
1101 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001102}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
1104static void dock_notify(struct ibm_struct *ibm, u32 event)
1105{
1106 int docked = dock_docked();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001107 int pci = ibm->hid && strstr(ibm->hid, IBM_PCI_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001109 if (event == 1 && !pci) /* 570 */
1110 acpi_bus_generate_event(ibm->device, event, 1); /* button */
1111 else if (event == 1 && pci) /* 570 */
1112 acpi_bus_generate_event(ibm->device, event, 3); /* dock */
1113 else if (event == 3 && docked)
1114 acpi_bus_generate_event(ibm->device, event, 1); /* button */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 else if (event == 3 && !docked)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001116 acpi_bus_generate_event(ibm->device, event, 2); /* undock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 else if (event == 0 && docked)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001118 acpi_bus_generate_event(ibm->device, event, 3); /* dock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 else {
1120 printk(IBM_ERR "unknown dock event %d, status %d\n",
1121 event, _sta(dock_handle));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001122 acpi_bus_generate_event(ibm->device, event, 0); /* unknown */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 }
1124}
Kristen Accardi63e5f242006-02-23 17:56:06 -08001125#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
Henrique de Moraes Holschuh2bc808a2007-02-21 13:05:38 -02001127#ifdef CONFIG_ACPI_IBM_BAY
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001128static int bay_status_supported;
1129static int bay_status2_supported;
1130static int bay_eject_supported;
1131static int bay_eject2_supported;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001133static int bay_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001135 bay_status_supported = bay_handle &&
1136 acpi_evalf(bay_handle, NULL, "_STA", "qv");
1137 bay_status2_supported = bay2_handle &&
1138 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
1139
1140 bay_eject_supported = bay_handle && bay_ej_handle &&
1141 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
1142 bay_eject2_supported = bay2_handle && bay2_ej_handle &&
1143 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
1145 return 0;
1146}
1147
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001148#define bay_occupied(b) (_sta(b##_handle) & 1)
1149
1150static int bay_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151{
1152 int len = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001153 int occupied = bay_occupied(bay);
1154 int occupied2 = bay_occupied(bay2);
1155 int eject, eject2;
1156
1157 len += sprintf(p + len, "status:\t\t%s\n", bay_status_supported ?
1158 (occupied ? "occupied" : "unoccupied") :
1159 "not supported");
1160 if (bay_status2_supported)
1161 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
1162 "occupied" : "unoccupied");
1163
1164 eject = bay_eject_supported && occupied;
1165 eject2 = bay_eject2_supported && occupied2;
1166
1167 if (eject && eject2)
1168 len += sprintf(p + len, "commands:\teject, eject2\n");
1169 else if (eject)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 len += sprintf(p + len, "commands:\teject\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001171 else if (eject2)
1172 len += sprintf(p + len, "commands:\teject2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
1174 return len;
1175}
1176
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001177static int bay_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178{
1179 char *cmd;
1180
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001181 if (!bay_eject_supported && !bay_eject2_supported)
1182 return -ENODEV;
1183
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001185 if (bay_eject_supported && strlencmp(cmd, "eject") == 0) {
1186 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
1187 return -EIO;
1188 } else if (bay_eject2_supported &&
1189 strlencmp(cmd, "eject2") == 0) {
1190 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 return -EIO;
1192 } else
1193 return -EINVAL;
1194 }
1195
1196 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001197}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
1199static void bay_notify(struct ibm_struct *ibm, u32 event)
1200{
1201 acpi_bus_generate_event(ibm->device, event, 0);
1202}
Henrique de Moraes Holschuh2bc808a2007-02-21 13:05:38 -02001203#endif /* CONFIG_ACPI_IBM_BAY */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001205static int cmos_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206{
1207 int len = 0;
1208
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001209 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1210 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 if (!cmos_handle)
1212 len += sprintf(p + len, "status:\t\tnot supported\n");
1213 else {
1214 len += sprintf(p + len, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001215 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 }
1217
1218 return len;
1219}
1220
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001221static int cmos_eval(int cmos_cmd)
1222{
1223 if (cmos_handle)
1224 return acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd);
1225 else
1226 return 1;
1227}
1228
1229static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230{
1231 char *cmd;
1232 int cmos_cmd;
1233
1234 if (!cmos_handle)
1235 return -EINVAL;
1236
1237 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001238 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
1239 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 /* cmos_cmd set */
1241 } else
1242 return -EINVAL;
1243
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001244 if (!cmos_eval(cmos_cmd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 return -EIO;
1246 }
1247
1248 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001249}
1250
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02001251enum led_access_mode {
1252 IBMACPI_LED_NONE = 0,
1253 IBMACPI_LED_570, /* 570 */
1254 IBMACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
1255 IBMACPI_LED_NEW, /* all others */
1256};
1257static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001258
1259static int led_init(void)
1260{
1261 if (!led_handle)
1262 /* led not supported on R30, R31 */
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02001263 led_supported = IBMACPI_LED_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001264 else if (strlencmp(led_path, "SLED") == 0)
1265 /* 570 */
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02001266 led_supported = IBMACPI_LED_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001267 else if (strlencmp(led_path, "SYSL") == 0)
1268 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02001269 led_supported = IBMACPI_LED_OLD;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001270 else
1271 /* all others */
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02001272 led_supported = IBMACPI_LED_NEW;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001273
1274 return 0;
1275}
1276
1277#define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
1278
1279static int led_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280{
1281 int len = 0;
1282
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001283 if (!led_supported) {
1284 len += sprintf(p + len, "status:\t\tnot supported\n");
1285 return len;
1286 }
1287 len += sprintf(p + len, "status:\t\tsupported\n");
1288
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02001289 if (led_supported == IBMACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001290 /* 570 */
1291 int i, status;
1292 for (i = 0; i < 8; i++) {
1293 if (!acpi_evalf(ec_handle,
1294 &status, "GLED", "dd", 1 << i))
1295 return -EIO;
1296 len += sprintf(p + len, "%d:\t\t%s\n",
1297 i, led_status(status));
1298 }
1299 }
1300
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 len += sprintf(p + len, "commands:\t"
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001302 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
1304 return len;
1305}
1306
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001307/* off, on, blink */
1308static const int led_sled_arg1[] = { 0, 1, 3 };
1309static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
1310static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
1311static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
1312
1313#define EC_HLCL 0x0c
1314#define EC_HLBL 0x0d
1315#define EC_HLMS 0x0e
1316
1317static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318{
1319 char *cmd;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001320 int led, ind, ret;
1321
1322 if (!led_supported)
1323 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
1325 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001326 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 return -EINVAL;
1328
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001329 if (strstr(cmd, "off")) {
1330 ind = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 } else if (strstr(cmd, "on")) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001332 ind = 1;
1333 } else if (strstr(cmd, "blink")) {
1334 ind = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 } else
1336 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001337
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02001338 if (led_supported == IBMACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001339 /* 570 */
1340 led = 1 << led;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001342 led, led_sled_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 return -EIO;
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02001344 } else if (led_supported == IBMACPI_LED_OLD) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001345 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
1346 led = 1 << led;
1347 ret = ec_write(EC_HLMS, led);
1348 if (ret >= 0)
1349 ret =
1350 ec_write(EC_HLBL, led * led_exp_hlbl[ind]);
1351 if (ret >= 0)
1352 ret =
1353 ec_write(EC_HLCL, led * led_exp_hlcl[ind]);
1354 if (ret < 0)
1355 return ret;
1356 } else {
1357 /* all others */
1358 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
1359 led, led_led_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 }
1363
1364 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001365}
1366
1367static int beep_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368{
1369 int len = 0;
1370
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001371 if (!beep_handle)
1372 len += sprintf(p + len, "status:\t\tnot supported\n");
1373 else {
1374 len += sprintf(p + len, "status:\t\tsupported\n");
1375 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
1376 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
1378 return len;
1379}
1380
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001381static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382{
1383 char *cmd;
1384 int beep_cmd;
1385
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001386 if (!beep_handle)
1387 return -ENODEV;
1388
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001390 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
1391 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 /* beep_cmd set */
1393 } else
1394 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001395 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 return -EIO;
1397 }
1398
1399 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001400}
1401
1402static int acpi_ec_read(int i, u8 * p)
1403{
1404 int v;
1405
1406 if (ecrd_handle) {
1407 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
1408 return 0;
1409 *p = v;
1410 } else {
1411 if (ec_read(i, p) < 0)
1412 return 0;
1413 }
1414
1415 return 1;
1416}
1417
1418static int acpi_ec_write(int i, u8 v)
1419{
1420 if (ecwr_handle) {
1421 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
1422 return 0;
1423 } else {
1424 if (ec_write(i, v) < 0)
1425 return 0;
1426 }
1427
1428 return 1;
1429}
1430
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001431static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001432
1433static int thermal_init(void)
1434{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001435 u8 t, ta1, ta2;
1436 int i;
1437 int acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001438
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001439 if (ibm_thinkpad_ec_found && experimental) {
1440 /*
1441 * Direct EC access mode: sensors at registers
1442 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
1443 * non-implemented, thermal sensors return 0x80 when
1444 * not available
1445 */
1446
1447 ta1 = ta2 = 0;
1448 for (i = 0; i < 8; i++) {
1449 if (likely(acpi_ec_read(0x78 + i, &t))) {
1450 ta1 |= t;
1451 } else {
1452 ta1 = 0;
1453 break;
1454 }
1455 if (likely(acpi_ec_read(0xC0 + i, &t))) {
1456 ta2 |= t;
1457 } else {
1458 ta1 = 0;
1459 break;
1460 }
1461 }
1462 if (ta1 == 0) {
1463 /* This is sheer paranoia, but we handle it anyway */
1464 if (acpi_tmp7) {
1465 printk(IBM_ERR
1466 "ThinkPad ACPI EC access misbehaving, "
1467 "falling back to ACPI TMPx access mode\n");
1468 thermal_read_mode = IBMACPI_THERMAL_ACPI_TMP07;
1469 } else {
1470 printk(IBM_ERR
1471 "ThinkPad ACPI EC access misbehaving, "
1472 "disabling thermal sensors access\n");
1473 thermal_read_mode = IBMACPI_THERMAL_NONE;
1474 }
1475 } else {
1476 thermal_read_mode =
1477 (ta2 != 0) ?
1478 IBMACPI_THERMAL_TPEC_16 : IBMACPI_THERMAL_TPEC_8;
1479 }
1480 } else if (acpi_tmp7) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001481 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
1482 /* 600e/x, 770e, 770x */
1483 thermal_read_mode = IBMACPI_THERMAL_ACPI_UPDT;
1484 } else {
1485 /* Standard ACPI TMPx access, max 8 sensors */
1486 thermal_read_mode = IBMACPI_THERMAL_ACPI_TMP07;
1487 }
1488 } else {
1489 /* temperatures not supported on 570, G4x, R30, R31, R32 */
1490 thermal_read_mode = IBMACPI_THERMAL_NONE;
1491 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001492
1493 return 0;
1494}
1495
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001496static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
1497{
1498 int i, t;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001499 s8 tmp;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001500 char tmpi[] = "TMPi";
1501
1502 if (!s)
1503 return -EINVAL;
1504
1505 switch (thermal_read_mode) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001506#if IBMACPI_MAX_THERMAL_SENSORS >= 16
1507 case IBMACPI_THERMAL_TPEC_16:
1508 for (i = 0; i < 8; i++) {
1509 if (!acpi_ec_read(0xC0 + i, &tmp))
1510 return -EIO;
1511 s->temp[i + 8] = tmp * 1000;
1512 }
1513 /* fallthrough */
1514#endif
1515 case IBMACPI_THERMAL_TPEC_8:
1516 for (i = 0; i < 8; i++) {
1517 if (!acpi_ec_read(0x78 + i, &tmp))
1518 return -EIO;
1519 s->temp[i] = tmp * 1000;
1520 }
1521 return (thermal_read_mode == IBMACPI_THERMAL_TPEC_16) ? 16 : 8;
1522
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001523 case IBMACPI_THERMAL_ACPI_UPDT:
1524 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
1525 return -EIO;
1526 for (i = 0; i < 8; i++) {
1527 tmpi[3] = '0' + i;
1528 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
1529 return -EIO;
1530 s->temp[i] = (t - 2732) * 100;
1531 }
1532 return 8;
1533
1534 case IBMACPI_THERMAL_ACPI_TMP07:
1535 for (i = 0; i < 8; i++) {
1536 tmpi[3] = '0' + i;
1537 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
1538 return -EIO;
1539 s->temp[i] = t * 1000;
1540 }
1541 return 8;
1542
1543 case IBMACPI_THERMAL_NONE:
1544 default:
1545 return 0;
1546 }
1547}
1548
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001549static int thermal_read(char *p)
1550{
1551 int len = 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001552 int n, i;
1553 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001554
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001555 n = thermal_get_sensors(&t);
1556 if (unlikely(n < 0))
1557 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001558
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001559 len += sprintf(p + len, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001560
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001561 if (n > 0) {
1562 for (i = 0; i < (n - 1); i++)
1563 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
1564 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
1565 } else
1566 len += sprintf(p + len, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001567
1568 return len;
1569}
1570
1571static u8 ecdump_regs[256];
1572
1573static int ecdump_read(char *p)
1574{
1575 int len = 0;
1576 int i, j;
1577 u8 v;
1578
1579 len += sprintf(p + len, "EC "
1580 " +00 +01 +02 +03 +04 +05 +06 +07"
1581 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
1582 for (i = 0; i < 256; i += 16) {
1583 len += sprintf(p + len, "EC 0x%02x:", i);
1584 for (j = 0; j < 16; j++) {
1585 if (!acpi_ec_read(i + j, &v))
1586 break;
1587 if (v != ecdump_regs[i + j])
1588 len += sprintf(p + len, " *%02x", v);
1589 else
1590 len += sprintf(p + len, " %02x", v);
1591 ecdump_regs[i + j] = v;
1592 }
1593 len += sprintf(p + len, "\n");
1594 if (j != 16)
1595 break;
1596 }
1597
1598 /* These are way too dangerous to advertise openly... */
1599#if 0
1600 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
1601 " (<offset> is 00-ff, <value> is 00-ff)\n");
1602 len += sprintf(p + len, "commands:\t0x<offset> <value> "
1603 " (<offset> is 00-ff, <value> is 0-255)\n");
1604#endif
1605 return len;
1606}
1607
1608static int ecdump_write(char *buf)
1609{
1610 char *cmd;
1611 int i, v;
1612
1613 while ((cmd = next_cmd(&buf))) {
1614 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
1615 /* i and v set */
1616 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
1617 /* i and v set */
1618 } else
1619 return -EINVAL;
1620 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
1621 if (!acpi_ec_write(i, v))
1622 return -EIO;
1623 } else
1624 return -EINVAL;
1625 }
1626
1627 return 0;
1628}
1629
1630static int brightness_offset = 0x31;
1631
Holger Macht8acb0252006-10-20 14:30:28 -07001632static int brightness_get(struct backlight_device *bd)
1633{
Henrique de Moraes Holschuh8d297262006-11-24 11:47:07 -02001634 u8 level;
1635 if (!acpi_ec_read(brightness_offset, &level))
1636 return -EIO;
Holger Macht8acb0252006-10-20 14:30:28 -07001637
Henrique de Moraes Holschuh8d297262006-11-24 11:47:07 -02001638 level &= 0x7;
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02001639
Henrique de Moraes Holschuh8d297262006-11-24 11:47:07 -02001640 return level;
Holger Macht8acb0252006-10-20 14:30:28 -07001641}
1642
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001643static int brightness_read(char *p)
1644{
1645 int len = 0;
Holger Macht8acb0252006-10-20 14:30:28 -07001646 int level;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001647
Holger Macht8acb0252006-10-20 14:30:28 -07001648 if ((level = brightness_get(NULL)) < 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001649 len += sprintf(p + len, "level:\t\tunreadable\n");
1650 } else {
1651 len += sprintf(p + len, "level:\t\t%d\n", level & 0x7);
1652 len += sprintf(p + len, "commands:\tup, down\n");
1653 len += sprintf(p + len, "commands:\tlevel <level>"
1654 " (<level> is 0-7)\n");
1655 }
1656
1657 return len;
1658}
1659
1660#define BRIGHTNESS_UP 4
1661#define BRIGHTNESS_DOWN 5
1662
Holger Macht8acb0252006-10-20 14:30:28 -07001663static int brightness_set(int value)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001664{
1665 int cmos_cmd, inc, i;
Holger Macht8acb0252006-10-20 14:30:28 -07001666 int current_value = brightness_get(NULL);
1667
1668 value &= 7;
1669
1670 cmos_cmd = value > current_value ? BRIGHTNESS_UP : BRIGHTNESS_DOWN;
1671 inc = value > current_value ? 1 : -1;
1672 for (i = current_value; i != value; i += inc) {
1673 if (!cmos_eval(cmos_cmd))
1674 return -EIO;
1675 if (!acpi_ec_write(brightness_offset, i + inc))
1676 return -EIO;
1677 }
1678
1679 return 0;
1680}
1681
1682static int brightness_write(char *buf)
1683{
1684 int level;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001685 int new_level;
1686 char *cmd;
1687
1688 while ((cmd = next_cmd(&buf))) {
Holger Macht8acb0252006-10-20 14:30:28 -07001689 if ((level = brightness_get(NULL)) < 0)
1690 return level;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001691 level &= 7;
1692
1693 if (strlencmp(cmd, "up") == 0) {
1694 new_level = level == 7 ? 7 : level + 1;
1695 } else if (strlencmp(cmd, "down") == 0) {
1696 new_level = level == 0 ? 0 : level - 1;
1697 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
1698 new_level >= 0 && new_level <= 7) {
1699 /* new_level set */
1700 } else
1701 return -EINVAL;
1702
Holger Macht8acb0252006-10-20 14:30:28 -07001703 brightness_set(new_level);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001704 }
1705
1706 return 0;
1707}
1708
Holger Macht8acb0252006-10-20 14:30:28 -07001709static int brightness_update_status(struct backlight_device *bd)
1710{
Henrique de Moraes Holschuhc9bf2962007-03-08 05:28:15 -03001711 return brightness_set(
1712 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
1713 bd->props.power == FB_BLANK_UNBLANK) ?
1714 bd->props.brightness : 0);
Holger Macht8acb0252006-10-20 14:30:28 -07001715}
1716
Richard Purdie599a52d2007-02-10 23:07:48 +00001717static struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02001718 .get_brightness = brightness_get,
1719 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02001720};
1721
1722static int brightness_init(void)
1723{
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02001724 int b;
1725
1726 b = brightness_get(NULL);
1727 if (b < 0)
1728 return b;
1729
Yu Luming519ab5f2006-12-19 12:56:15 -08001730 ibm_backlight_device = backlight_device_register("ibm", NULL, NULL,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02001731 &ibm_backlight_data);
1732 if (IS_ERR(ibm_backlight_device)) {
1733 printk(IBM_ERR "Could not register backlight device\n");
1734 return PTR_ERR(ibm_backlight_device);
1735 }
1736
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02001737 ibm_backlight_device->props.max_brightness = 7;
1738 ibm_backlight_device->props.brightness = b;
1739 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00001740
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02001741 return 0;
1742}
1743
1744static void brightness_exit(void)
1745{
1746 if (ibm_backlight_device) {
1747 backlight_device_unregister(ibm_backlight_device);
1748 ibm_backlight_device = NULL;
1749 }
1750}
1751
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001752static int volume_offset = 0x30;
1753
1754static int volume_read(char *p)
1755{
1756 int len = 0;
1757 u8 level;
1758
1759 if (!acpi_ec_read(volume_offset, &level)) {
1760 len += sprintf(p + len, "level:\t\tunreadable\n");
1761 } else {
1762 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
1763 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
1764 len += sprintf(p + len, "commands:\tup, down, mute\n");
1765 len += sprintf(p + len, "commands:\tlevel <level>"
1766 " (<level> is 0-15)\n");
1767 }
1768
1769 return len;
1770}
1771
1772#define VOLUME_DOWN 0
1773#define VOLUME_UP 1
1774#define VOLUME_MUTE 2
1775
1776static int volume_write(char *buf)
1777{
1778 int cmos_cmd, inc, i;
1779 u8 level, mute;
1780 int new_level, new_mute;
1781 char *cmd;
1782
1783 while ((cmd = next_cmd(&buf))) {
1784 if (!acpi_ec_read(volume_offset, &level))
1785 return -EIO;
1786 new_mute = mute = level & 0x40;
1787 new_level = level = level & 0xf;
1788
1789 if (strlencmp(cmd, "up") == 0) {
1790 if (mute)
1791 new_mute = 0;
1792 else
1793 new_level = level == 15 ? 15 : level + 1;
1794 } else if (strlencmp(cmd, "down") == 0) {
1795 if (mute)
1796 new_mute = 0;
1797 else
1798 new_level = level == 0 ? 0 : level - 1;
1799 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
1800 new_level >= 0 && new_level <= 15) {
1801 /* new_level set */
1802 } else if (strlencmp(cmd, "mute") == 0) {
1803 new_mute = 0x40;
1804 } else
1805 return -EINVAL;
1806
1807 if (new_level != level) { /* mute doesn't change */
1808 cmos_cmd = new_level > level ? VOLUME_UP : VOLUME_DOWN;
1809 inc = new_level > level ? 1 : -1;
1810
1811 if (mute && (!cmos_eval(cmos_cmd) ||
1812 !acpi_ec_write(volume_offset, level)))
1813 return -EIO;
1814
1815 for (i = level; i != new_level; i += inc)
1816 if (!cmos_eval(cmos_cmd) ||
1817 !acpi_ec_write(volume_offset, i + inc))
1818 return -EIO;
1819
1820 if (mute && (!cmos_eval(VOLUME_MUTE) ||
1821 !acpi_ec_write(volume_offset,
1822 new_level + mute)))
1823 return -EIO;
1824 }
1825
1826 if (new_mute != mute) { /* level doesn't change */
1827 cmos_cmd = new_mute ? VOLUME_MUTE : VOLUME_UP;
1828
1829 if (!cmos_eval(cmos_cmd) ||
1830 !acpi_ec_write(volume_offset, level + new_mute))
1831 return -EIO;
1832 }
1833 }
1834
1835 return 0;
1836}
1837
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001838static enum fan_status_access_mode fan_status_access_mode;
1839static enum fan_control_access_mode fan_control_access_mode;
1840static enum fan_control_commands fan_control_commands;
1841
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02001842static int fan_control_status_known;
1843static u8 fan_control_initial_status;
1844
Len Brown25c68a32006-12-08 04:43:41 -05001845static void fan_watchdog_fire(struct work_struct *ignored);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02001846static int fan_watchdog_maxinterval;
Len Brown25c68a32006-12-08 04:43:41 -05001847static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02001848
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001849static int fan_init(void)
1850{
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001851 fan_status_access_mode = IBMACPI_FAN_NONE;
1852 fan_control_access_mode = IBMACPI_FAN_WR_NONE;
1853 fan_control_commands = 0;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02001854 fan_control_status_known = 1;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02001855 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001856
1857 if (gfan_handle) {
1858 /* 570, 600e/x, 770e, 770x */
1859 fan_status_access_mode = IBMACPI_FAN_RD_ACPI_GFAN;
1860 } else {
1861 /* all other ThinkPads: note that even old-style
1862 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02001863 if (likely(acpi_ec_read(fan_status_offset,
1864 &fan_control_initial_status))) {
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001865 fan_status_access_mode = IBMACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02001866
1867 /* In some ThinkPads, neither the EC nor the ACPI
1868 * DSDT initialize the fan status, and it ends up
1869 * being set to 0x07 when it *could* be either
1870 * 0x07 or 0x80.
1871 *
1872 * Enable for TP-1Y (T43), TP-78 (R51e),
1873 * TP-76 (R52), TP-70 (T43, R52), which are known
1874 * to be buggy. */
1875 if (fan_control_initial_status == 0x07 &&
1876 ibm_thinkpad_ec_found &&
1877 ((ibm_thinkpad_ec_found[0] == '1' &&
1878 ibm_thinkpad_ec_found[1] == 'Y') ||
1879 (ibm_thinkpad_ec_found[0] == '7' &&
1880 (ibm_thinkpad_ec_found[1] == '6' ||
1881 ibm_thinkpad_ec_found[1] == '8' ||
1882 ibm_thinkpad_ec_found[1] == '0'))
1883 )) {
1884 printk(IBM_NOTICE
1885 "fan_init: initial fan status is "
1886 "unknown, assuming it is in auto "
1887 "mode\n");
1888 fan_control_status_known = 0;
1889 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001890 } else {
1891 printk(IBM_ERR
1892 "ThinkPad ACPI EC access misbehaving, "
1893 "fan status and control unavailable\n");
1894 return 0;
1895 }
1896 }
1897
1898 if (sfan_handle) {
1899 /* 570, 770x-JL */
1900 fan_control_access_mode = IBMACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02001901 fan_control_commands |=
1902 IBMACPI_FAN_CMD_LEVEL | IBMACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001903 } else {
1904 if (!gfan_handle) {
1905 /* gfan without sfan means no fan control */
1906 /* all other models implement TP EC 0x2f control */
1907
1908 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02001909 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001910 fan_control_access_mode =
1911 IBMACPI_FAN_WR_ACPI_FANS;
1912 fan_control_commands |=
1913 IBMACPI_FAN_CMD_SPEED |
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02001914 IBMACPI_FAN_CMD_LEVEL |
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001915 IBMACPI_FAN_CMD_ENABLE;
1916 } else {
1917 fan_control_access_mode = IBMACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02001918 fan_control_commands |=
1919 IBMACPI_FAN_CMD_LEVEL |
1920 IBMACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001921 }
1922 }
1923 }
1924
1925 return 0;
1926}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001927
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02001928static int fan_get_status(u8 *status)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001929{
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02001930 u8 s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001931
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02001932 /* TODO:
1933 * Add IBMACPI_FAN_RD_ACPI_FANS ? */
1934
Henrique de Moraes Holschuh3ef8a602006-11-24 11:47:10 -02001935 switch (fan_status_access_mode) {
1936 case IBMACPI_FAN_RD_ACPI_GFAN:
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001937 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02001938
1939 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001940 return -EIO;
1941
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02001942 if (likely(status))
1943 *status = s & 0x07;
1944
1945 break;
1946
1947 case IBMACPI_FAN_RD_TPEC:
1948 /* all except 570, 600e/x, 770e, 770x */
1949 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
1950 return -EIO;
1951
1952 if (likely(status))
1953 *status = s;
1954
1955 break;
1956
1957 default:
1958 return -ENXIO;
1959 }
1960
1961 return 0;
1962}
1963
1964static int fan_get_speed(unsigned int *speed)
1965{
1966 u8 hi, lo;
1967
1968 switch (fan_status_access_mode) {
1969 case IBMACPI_FAN_RD_TPEC:
1970 /* all except 570, 600e/x, 770e, 770x */
1971 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
1972 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
1973 return -EIO;
1974
1975 if (likely(speed))
1976 *speed = (hi << 8) | lo;
1977
1978 break;
1979
1980 default:
1981 return -ENXIO;
1982 }
1983
1984 return 0;
1985}
1986
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02001987static void fan_exit(void)
1988{
1989 cancel_delayed_work(&fan_watchdog_task);
1990 flush_scheduled_work();
1991}
1992
1993static void fan_watchdog_reset(void)
1994{
1995 static int fan_watchdog_active = 0;
1996
1997 if (fan_watchdog_active)
1998 cancel_delayed_work(&fan_watchdog_task);
1999
2000 if (fan_watchdog_maxinterval > 0) {
2001 fan_watchdog_active = 1;
2002 if (!schedule_delayed_work(&fan_watchdog_task,
2003 msecs_to_jiffies(fan_watchdog_maxinterval
2004 * 1000))) {
2005 printk(IBM_ERR "failed to schedule the fan watchdog, "
2006 "watchdog will not trigger\n");
2007 }
2008 } else
2009 fan_watchdog_active = 0;
2010}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002011
2012static int fan_read(char *p)
2013{
2014 int len = 0;
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02002015 int rc;
2016 u8 status;
2017 unsigned int speed = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002018
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02002019 switch (fan_status_access_mode) {
2020 case IBMACPI_FAN_RD_ACPI_GFAN:
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002021 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02002022 if ((rc = fan_get_status(&status)) < 0)
2023 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002024
Henrique de Moraes Holschuhbab812a2006-11-24 11:47:12 -02002025 len += sprintf(p + len, "status:\t\t%s\n"
2026 "level:\t\t%d\n",
2027 (status != 0) ? "enabled" : "disabled", status);
Henrique de Moraes Holschuh3ef8a602006-11-24 11:47:10 -02002028 break;
2029
2030 case IBMACPI_FAN_RD_TPEC:
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002031 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02002032 if ((rc = fan_get_status(&status)) < 0)
2033 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002034
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002035 if (unlikely(!fan_control_status_known)) {
2036 if (status != fan_control_initial_status)
2037 fan_control_status_known = 1;
2038 else
2039 /* Return most likely status. In fact, it
2040 * might be the only possible status */
2041 status = IBMACPI_FAN_EC_AUTO;
2042 }
2043
Henrique de Moraes Holschuhbab812a2006-11-24 11:47:12 -02002044 len += sprintf(p + len, "status:\t\t%s\n",
2045 (status != 0) ? "enabled" : "disabled");
Henrique de Moraes Holschuh3ef8a602006-11-24 11:47:10 -02002046
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002047 /* No ThinkPad boots on disengaged mode, we can safely
2048 * assume the tachometer is online if fan control status
2049 * was unknown */
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02002050 if ((rc = fan_get_speed(&speed)) < 0)
2051 return rc;
2052
2053 len += sprintf(p + len, "speed:\t\t%d\n", speed);
Henrique de Moraes Holschuhbab812a2006-11-24 11:47:12 -02002054
2055 if (status & IBMACPI_FAN_EC_DISENGAGED)
2056 /* Disengaged mode takes precedence */
2057 len += sprintf(p + len, "level:\t\tdisengaged\n");
2058 else if (status & IBMACPI_FAN_EC_AUTO)
2059 len += sprintf(p + len, "level:\t\tauto\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002060 else
Henrique de Moraes Holschuhbab812a2006-11-24 11:47:12 -02002061 len += sprintf(p + len, "level:\t\t%d\n", status);
Henrique de Moraes Holschuh3ef8a602006-11-24 11:47:10 -02002062 break;
2063
2064 case IBMACPI_FAN_NONE:
2065 default:
2066 len += sprintf(p + len, "status:\t\tnot supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002067 }
2068
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02002069 if (fan_control_commands & IBMACPI_FAN_CMD_LEVEL) {
2070 len += sprintf(p + len, "commands:\tlevel <level>");
2071
2072 switch (fan_control_access_mode) {
2073 case IBMACPI_FAN_WR_ACPI_SFAN:
2074 len += sprintf(p + len, " (<level> is 0-7)\n");
2075 break;
2076
2077 default:
2078 len += sprintf(p + len, " (<level> is 0-7, "
2079 "auto, disengaged)\n");
2080 break;
2081 }
2082 }
Henrique de Moraes Holschuh3ef8a602006-11-24 11:47:10 -02002083
2084 if (fan_control_commands & IBMACPI_FAN_CMD_ENABLE)
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002085 len += sprintf(p + len, "commands:\tenable, disable\n"
2086 "commands:\twatchdog <timeout> (<timeout> is 0 (off), "
2087 "1-120 (seconds))\n");
Henrique de Moraes Holschuh3ef8a602006-11-24 11:47:10 -02002088
2089 if (fan_control_commands & IBMACPI_FAN_CMD_SPEED)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002090 len += sprintf(p + len, "commands:\tspeed <speed>"
2091 " (<speed> is 0-65535)\n");
2092
2093 return len;
2094}
2095
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002096static int fan_set_level(int level)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002097{
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002098 switch (fan_control_access_mode) {
2099 case IBMACPI_FAN_WR_ACPI_SFAN:
2100 if (level >= 0 && level <= 7) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002101 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
2102 return -EIO;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002103 } else
2104 return -EINVAL;
2105 break;
2106
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02002107 case IBMACPI_FAN_WR_ACPI_FANS:
2108 case IBMACPI_FAN_WR_TPEC:
2109 if ((level != IBMACPI_FAN_EC_AUTO) &&
2110 (level != IBMACPI_FAN_EC_DISENGAGED) &&
2111 ((level < 0) || (level > 7)))
2112 return -EINVAL;
2113
2114 if (!acpi_ec_write(fan_status_offset, level))
2115 return -EIO;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002116 else
2117 fan_control_status_known = 1;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02002118 break;
2119
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002120 default:
2121 return -ENXIO;
2122 }
2123 return 0;
2124}
2125
2126static int fan_set_enable(void)
2127{
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002128 u8 s;
2129 int rc;
2130
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002131 switch (fan_control_access_mode) {
2132 case IBMACPI_FAN_WR_ACPI_FANS:
2133 case IBMACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002134 if ((rc = fan_get_status(&s)) < 0)
2135 return rc;
2136
2137 /* Don't go out of emergency fan mode */
2138 if (s != 7)
2139 s = IBMACPI_FAN_EC_AUTO;
2140
2141 if (!acpi_ec_write(fan_status_offset, s))
2142 return -EIO;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002143 else
2144 fan_control_status_known = 1;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002145 break;
2146
2147 case IBMACPI_FAN_WR_ACPI_SFAN:
2148 if ((rc = fan_get_status(&s)) < 0)
2149 return rc;
2150
2151 s &= 0x07;
2152
2153 /* Set fan to at least level 4 */
2154 if (s < 4)
2155 s = 4;
2156
2157 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002158 return -EIO;
2159 break;
2160
2161 default:
2162 return -ENXIO;
2163 }
2164 return 0;
2165}
2166
2167static int fan_set_disable(void)
2168{
2169 switch (fan_control_access_mode) {
2170 case IBMACPI_FAN_WR_ACPI_FANS:
2171 case IBMACPI_FAN_WR_TPEC:
2172 if (!acpi_ec_write(fan_status_offset, 0x00))
2173 return -EIO;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002174 else
2175 fan_control_status_known = 1;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002176 break;
2177
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002178 case IBMACPI_FAN_WR_ACPI_SFAN:
2179 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
2180 return -EIO;
2181 break;
2182
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002183 default:
2184 return -ENXIO;
2185 }
2186 return 0;
2187}
2188
2189static int fan_set_speed(int speed)
2190{
2191 switch (fan_control_access_mode) {
2192 case IBMACPI_FAN_WR_ACPI_FANS:
2193 if (speed >= 0 && speed <= 65535) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002194 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
2195 speed, speed, speed))
2196 return -EIO;
2197 } else
2198 return -EINVAL;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002199 break;
2200
2201 default:
2202 return -ENXIO;
2203 }
2204 return 0;
2205}
2206
2207static int fan_write_cmd_level(const char *cmd, int *rc)
2208{
2209 int level;
2210
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02002211 if (strlencmp(cmd, "level auto") == 0)
2212 level = IBMACPI_FAN_EC_AUTO;
2213 else if (strlencmp(cmd, "level disengaged") == 0)
2214 level = IBMACPI_FAN_EC_DISENGAGED;
2215 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002216 return 0;
2217
2218 if ((*rc = fan_set_level(level)) == -ENXIO)
2219 printk(IBM_ERR "level command accepted for unsupported "
2220 "access mode %d", fan_control_access_mode);
2221
2222 return 1;
2223}
2224
2225static int fan_write_cmd_enable(const char *cmd, int *rc)
2226{
2227 if (strlencmp(cmd, "enable") != 0)
2228 return 0;
2229
2230 if ((*rc = fan_set_enable()) == -ENXIO)
2231 printk(IBM_ERR "enable command accepted for unsupported "
2232 "access mode %d", fan_control_access_mode);
2233
2234 return 1;
2235}
2236
2237static int fan_write_cmd_disable(const char *cmd, int *rc)
2238{
2239 if (strlencmp(cmd, "disable") != 0)
2240 return 0;
2241
2242 if ((*rc = fan_set_disable()) == -ENXIO)
2243 printk(IBM_ERR "disable command accepted for unsupported "
2244 "access mode %d", fan_control_access_mode);
2245
2246 return 1;
2247}
2248
2249static int fan_write_cmd_speed(const char *cmd, int *rc)
2250{
2251 int speed;
2252
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02002253 /* TODO:
2254 * Support speed <low> <medium> <high> ? */
2255
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002256 if (sscanf(cmd, "speed %d", &speed) != 1)
2257 return 0;
2258
2259 if ((*rc = fan_set_speed(speed)) == -ENXIO)
2260 printk(IBM_ERR "speed command accepted for unsupported "
2261 "access mode %d", fan_control_access_mode);
2262
2263 return 1;
2264}
2265
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002266static int fan_write_cmd_watchdog(const char *cmd, int *rc)
2267{
2268 int interval;
2269
2270 if (sscanf(cmd, "watchdog %d", &interval) != 1)
2271 return 0;
2272
2273 if (interval < 0 || interval > 120)
2274 *rc = -EINVAL;
2275 else
2276 fan_watchdog_maxinterval = interval;
2277
2278 return 1;
2279}
2280
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002281static int fan_write(char *buf)
2282{
2283 char *cmd;
2284 int rc = 0;
2285
2286 while (!rc && (cmd = next_cmd(&buf))) {
2287 if (!((fan_control_commands & IBMACPI_FAN_CMD_LEVEL) &&
2288 fan_write_cmd_level(cmd, &rc)) &&
2289 !((fan_control_commands & IBMACPI_FAN_CMD_ENABLE) &&
2290 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002291 fan_write_cmd_disable(cmd, &rc) ||
2292 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002293 !((fan_control_commands & IBMACPI_FAN_CMD_SPEED) &&
2294 fan_write_cmd_speed(cmd, &rc))
2295 )
2296 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002297 else if (!rc)
2298 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002299 }
2300
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002301 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002302}
2303
Len Brown25c68a32006-12-08 04:43:41 -05002304static void fan_watchdog_fire(struct work_struct *ignored)
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002305{
2306 printk(IBM_NOTICE "fan watchdog: enabling fan\n");
2307 if (fan_set_enable()) {
2308 printk(IBM_ERR "fan watchdog: error while enabling fan\n");
2309 /* reschedule for later */
2310 fan_watchdog_reset();
2311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312}
2313
2314static struct ibm_struct ibms[] = {
2315 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002316 .name = "driver",
Adrian Bunk1f217822006-12-19 13:01:28 -08002317 .init = ibm_acpi_driver_init,
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002318 .read = driver_read,
2319 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002321 .name = "hotkey",
2322 .hid = IBM_HKEY_HID,
2323 .init = hotkey_init,
2324 .read = hotkey_read,
2325 .write = hotkey_write,
2326 .exit = hotkey_exit,
2327 .notify = hotkey_notify,
2328 .handle = &hkey_handle,
2329 .type = ACPI_DEVICE_NOTIFY,
2330 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002332 .name = "bluetooth",
2333 .init = bluetooth_init,
2334 .read = bluetooth_read,
2335 .write = bluetooth_write,
2336 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 {
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002338 .name = "wan",
2339 .init = wan_init,
2340 .read = wan_read,
2341 .write = wan_write,
2342 .experimental = 1,
2343 },
2344 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002345 .name = "video",
2346 .init = video_init,
2347 .read = video_read,
2348 .write = video_write,
2349 .exit = video_exit,
2350 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002352 .name = "light",
2353 .init = light_init,
2354 .read = light_read,
2355 .write = light_write,
2356 },
Kristen Accardi63e5f242006-02-23 17:56:06 -08002357#ifdef CONFIG_ACPI_IBM_DOCK
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002359 .name = "dock",
2360 .read = dock_read,
2361 .write = dock_write,
2362 .notify = dock_notify,
2363 .handle = &dock_handle,
2364 .type = ACPI_SYSTEM_NOTIFY,
2365 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002367 .name = "dock",
2368 .hid = IBM_PCI_HID,
2369 .notify = dock_notify,
2370 .handle = &pci_handle,
2371 .type = ACPI_SYSTEM_NOTIFY,
2372 },
Kristen Accardi63e5f242006-02-23 17:56:06 -08002373#endif
Henrique de Moraes Holschuh2bc808a2007-02-21 13:05:38 -02002374#ifdef CONFIG_ACPI_IBM_BAY
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002376 .name = "bay",
2377 .init = bay_init,
2378 .read = bay_read,
2379 .write = bay_write,
2380 .notify = bay_notify,
2381 .handle = &bay_handle,
2382 .type = ACPI_SYSTEM_NOTIFY,
2383 },
Henrique de Moraes Holschuh2bc808a2007-02-21 13:05:38 -02002384#endif /* CONFIG_ACPI_IBM_BAY */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002386 .name = "cmos",
2387 .read = cmos_read,
2388 .write = cmos_write,
2389 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002391 .name = "led",
2392 .init = led_init,
2393 .read = led_read,
2394 .write = led_write,
2395 },
2396 {
2397 .name = "beep",
2398 .read = beep_read,
2399 .write = beep_write,
2400 },
2401 {
2402 .name = "thermal",
2403 .init = thermal_init,
2404 .read = thermal_read,
2405 },
2406 {
2407 .name = "ecdump",
2408 .read = ecdump_read,
2409 .write = ecdump_write,
2410 .experimental = 1,
2411 },
2412 {
2413 .name = "brightness",
2414 .read = brightness_read,
2415 .write = brightness_write,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02002416 .init = brightness_init,
2417 .exit = brightness_exit,
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002418 },
2419 {
2420 .name = "volume",
2421 .read = volume_read,
2422 .write = volume_write,
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002423 },
2424 {
2425 .name = "fan",
2426 .read = fan_read,
2427 .write = fan_write,
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002428 .init = fan_init,
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002429 .exit = fan_exit,
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002430 .experimental = 1,
2431 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433
2434static int dispatch_read(char *page, char **start, off_t off, int count,
2435 int *eof, void *data)
2436{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02002437 struct ibm_struct *ibm = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 int len;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002439
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 if (!ibm || !ibm->read)
2441 return -EINVAL;
2442
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002443 len = ibm->read(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 if (len < 0)
2445 return len;
2446
2447 if (len <= off + count)
2448 *eof = 1;
2449 *start = page + off;
2450 len -= off;
2451 if (len > count)
2452 len = count;
2453 if (len < 0)
2454 len = 0;
2455
2456 return len;
2457}
2458
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002459static int dispatch_write(struct file *file, const char __user * userbuf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 unsigned long count, void *data)
2461{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02002462 struct ibm_struct *ibm = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 char *kernbuf;
2464 int ret;
2465
2466 if (!ibm || !ibm->write)
2467 return -EINVAL;
2468
2469 kernbuf = kmalloc(count + 2, GFP_KERNEL);
2470 if (!kernbuf)
2471 return -ENOMEM;
2472
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002473 if (copy_from_user(kernbuf, userbuf, count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 kfree(kernbuf);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002475 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 }
2477
2478 kernbuf[count] = 0;
2479 strcat(kernbuf, ",");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002480 ret = ibm->write(kernbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 if (ret == 0)
2482 ret = count;
2483
2484 kfree(kernbuf);
2485
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002486 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487}
2488
2489static void dispatch_notify(acpi_handle handle, u32 event, void *data)
2490{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02002491 struct ibm_struct *ibm = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492
2493 if (!ibm || !ibm->notify)
2494 return;
2495
2496 ibm->notify(ibm, event);
2497}
2498
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002499static int __init setup_notify(struct ibm_struct *ibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500{
2501 acpi_status status;
2502 int ret;
2503
2504 if (!*ibm->handle)
2505 return 0;
2506
2507 ret = acpi_bus_get_device(*ibm->handle, &ibm->device);
2508 if (ret < 0) {
2509 printk(IBM_ERR "%s device not present\n", ibm->name);
Henrique de Moraes Holschuh96e89af2007-03-15 16:15:06 -03002510 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 }
2512
2513 acpi_driver_data(ibm->device) = ibm;
2514 sprintf(acpi_device_class(ibm->device), "%s/%s", IBM_NAME, ibm->name);
2515
2516 status = acpi_install_notify_handler(*ibm->handle, ibm->type,
2517 dispatch_notify, ibm);
2518 if (ACPI_FAILURE(status)) {
Henrique de Moraes Holschuh96e89af2007-03-15 16:15:06 -03002519 if (status == AE_ALREADY_EXISTS) {
2520 printk(IBM_NOTICE "another device driver is already handling %s events\n",
2521 ibm->name);
2522 } else {
2523 printk(IBM_ERR "acpi_install_notify_handler(%s) failed: %d\n",
2524 ibm->name, status);
2525 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 return -ENODEV;
2527 }
Alexey Starikovskiy4afaf542006-12-18 14:53:33 -03002528 ibm->notify_installed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 return 0;
2530}
2531
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002532static int __init ibm_device_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533{
2534 return 0;
2535}
2536
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002537static int __init register_driver(struct ibm_struct *ibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538{
2539 int ret;
2540
Burman Yan36bcbec2006-12-19 12:56:11 -08002541 ibm->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 if (!ibm->driver) {
2543 printk(IBM_ERR "kmalloc(ibm->driver) failed\n");
2544 return -1;
2545 }
2546
Henrique de Moraes Holschuh3dfd35c2006-11-24 10:32:32 -02002547 sprintf(ibm->driver->name, "%s_%s", IBM_NAME, ibm->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 ibm->driver->ids = ibm->hid;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002549 ibm->driver->ops.add = &ibm_device_add;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550
2551 ret = acpi_bus_register_driver(ibm->driver);
2552 if (ret < 0) {
2553 printk(IBM_ERR "acpi_bus_register_driver(%s) failed: %d\n",
2554 ibm->hid, ret);
2555 kfree(ibm->driver);
2556 }
2557
2558 return ret;
2559}
2560
Henrique de Moraes Holschuh96e89af2007-03-15 16:15:06 -03002561static void ibm_exit(struct ibm_struct *ibm);
2562
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002563static int __init ibm_init(struct ibm_struct *ibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564{
2565 int ret;
2566 struct proc_dir_entry *entry;
2567
2568 if (ibm->experimental && !experimental)
2569 return 0;
2570
2571 if (ibm->hid) {
2572 ret = register_driver(ibm);
2573 if (ret < 0)
2574 return ret;
2575 ibm->driver_registered = 1;
2576 }
2577
2578 if (ibm->init) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002579 ret = ibm->init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 if (ret != 0)
2581 return ret;
2582 ibm->init_called = 1;
2583 }
2584
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002585 if (ibm->read) {
2586 entry = create_proc_entry(ibm->name,
2587 S_IFREG | S_IRUGO | S_IWUSR,
2588 proc_dir);
2589 if (!entry) {
2590 printk(IBM_ERR "unable to create proc entry %s\n",
2591 ibm->name);
2592 return -ENODEV;
2593 }
2594 entry->owner = THIS_MODULE;
2595 entry->data = ibm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 entry->read_proc = &dispatch_read;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002597 if (ibm->write)
2598 entry->write_proc = &dispatch_write;
2599 ibm->proc_created = 1;
2600 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601
2602 if (ibm->notify) {
2603 ret = setup_notify(ibm);
Henrique de Moraes Holschuh96e89af2007-03-15 16:15:06 -03002604 if (ret == -ENODEV) {
2605 printk(IBM_NOTICE "disabling subdriver %s\n",
2606 ibm->name);
2607 ibm_exit(ibm);
2608 return 0;
2609 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 if (ret < 0)
2611 return ret;
2612 }
2613
2614 return 0;
2615}
2616
2617static void ibm_exit(struct ibm_struct *ibm)
2618{
2619 if (ibm->notify_installed)
2620 acpi_remove_notify_handler(*ibm->handle, ibm->type,
2621 dispatch_notify);
2622
2623 if (ibm->proc_created)
2624 remove_proc_entry(ibm->name, proc_dir);
2625
2626 if (ibm->init_called && ibm->exit)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002627 ibm->exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628
2629 if (ibm->driver_registered) {
2630 acpi_bus_unregister_driver(ibm->driver);
2631 kfree(ibm->driver);
2632 }
2633}
2634
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002635static void __init ibm_handle_init(char *name,
2636 acpi_handle * handle, acpi_handle parent,
2637 char **paths, int num_paths, char **path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638{
2639 int i;
2640 acpi_status status;
2641
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002642 for (i = 0; i < num_paths; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 status = acpi_get_handle(parent, paths[i], handle);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002644 if (ACPI_SUCCESS(status)) {
2645 *path = paths[i];
2646 return;
2647 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002649
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 *handle = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651}
2652
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002653#define IBM_HANDLE_INIT(object) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 ibm_handle_init(#object, &object##_handle, *object##_parent, \
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002655 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656
Henrique de Moraes Holschuh3945ac32007-02-06 19:13:44 -02002657static int __init set_ibm_param(const char *val, struct kernel_param *kp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658{
2659 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002661 for (i = 0; i < ARRAY_SIZE(ibms); i++)
2662 if (strcmp(ibms[i].name, kp->name) == 0 && ibms[i].write) {
2663 if (strlen(val) > sizeof(ibms[i].param) - 2)
2664 return -ENOSPC;
2665 strcpy(ibms[i].param, val);
2666 strcat(ibms[i].param, ",");
2667 return 0;
2668 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 return -EINVAL;
2671}
2672
2673#define IBM_PARAM(feature) \
2674 module_param_call(feature, set_ibm_param, NULL, NULL, 0)
2675
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002676IBM_PARAM(hotkey);
2677IBM_PARAM(bluetooth);
2678IBM_PARAM(video);
2679IBM_PARAM(light);
Kristen Accardi63e5f242006-02-23 17:56:06 -08002680#ifdef CONFIG_ACPI_IBM_DOCK
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002681IBM_PARAM(dock);
Kristen Accardi63e5f242006-02-23 17:56:06 -08002682#endif
Henrique de Moraes Holschuh2bc808a2007-02-21 13:05:38 -02002683#ifdef CONFIG_ACPI_IBM_BAY
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002684IBM_PARAM(bay);
Henrique de Moraes Holschuh2bc808a2007-02-21 13:05:38 -02002685#endif /* CONFIG_ACPI_IBM_BAY */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002686IBM_PARAM(cmos);
2687IBM_PARAM(led);
2688IBM_PARAM(beep);
2689IBM_PARAM(ecdump);
2690IBM_PARAM(brightness);
2691IBM_PARAM(volume);
2692IBM_PARAM(fan);
2693
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694static void acpi_ibm_exit(void)
2695{
2696 int i;
2697
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002698 for (i = ARRAY_SIZE(ibms) - 1; i >= 0; i--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 ibm_exit(&ibms[i]);
2700
Henrique de Moraes Holschuh3945ac32007-02-06 19:13:44 -02002701 if (proc_dir)
2702 remove_proc_entry(IBM_DIR, acpi_root_dir);
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02002703
2704 if (ibm_thinkpad_ec_found)
2705 kfree(ibm_thinkpad_ec_found);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706}
2707
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02002708static char* __init check_dmi_for_ec(void)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002709{
2710 struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02002711 char ec_fw_string[18];
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002712
2713 /*
2714 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
2715 * X32 or newer, all Z series; Some models must have an
2716 * up-to-date BIOS or they will not be detected.
2717 *
2718 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
2719 */
2720 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02002721 if (sscanf(dev->name,
2722 "IBM ThinkPad Embedded Controller -[%17c",
2723 ec_fw_string) == 1) {
2724 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
2725 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
2726 return kstrdup(ec_fw_string, GFP_KERNEL);
2727 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002728 }
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02002729 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730}
2731
2732static int __init acpi_ibm_init(void)
2733{
2734 int ret, i;
2735
2736 if (acpi_disabled)
2737 return -ENODEV;
2738
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002739 /* ec is required because many other handles are relative to it */
2740 IBM_HANDLE_INIT(ec);
2741 if (!ec_handle) {
2742 printk(IBM_ERR "ec object not found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 return -ENODEV;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002744 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002746 /* Models with newer firmware report the EC in DMI */
2747 ibm_thinkpad_ec_found = check_dmi_for_ec();
2748
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 /* these handles are not required */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002750 IBM_HANDLE_INIT(vid);
2751 IBM_HANDLE_INIT(vid2);
2752 IBM_HANDLE_INIT(ledb);
2753 IBM_HANDLE_INIT(led);
2754 IBM_HANDLE_INIT(hkey);
2755 IBM_HANDLE_INIT(lght);
2756 IBM_HANDLE_INIT(cmos);
Kristen Accardi63e5f242006-02-23 17:56:06 -08002757#ifdef CONFIG_ACPI_IBM_DOCK
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002758 IBM_HANDLE_INIT(dock);
Kristen Accardi63e5f242006-02-23 17:56:06 -08002759#endif
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002760 IBM_HANDLE_INIT(pci);
Henrique de Moraes Holschuh2bc808a2007-02-21 13:05:38 -02002761#ifdef CONFIG_ACPI_IBM_BAY
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002762 IBM_HANDLE_INIT(bay);
2763 if (bay_handle)
2764 IBM_HANDLE_INIT(bay_ej);
2765 IBM_HANDLE_INIT(bay2);
2766 if (bay2_handle)
2767 IBM_HANDLE_INIT(bay2_ej);
Henrique de Moraes Holschuh2bc808a2007-02-21 13:05:38 -02002768#endif /* CONFIG_ACPI_IBM_BAY */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002769 IBM_HANDLE_INIT(beep);
2770 IBM_HANDLE_INIT(ecrd);
2771 IBM_HANDLE_INIT(ecwr);
2772 IBM_HANDLE_INIT(fans);
2773 IBM_HANDLE_INIT(gfan);
2774 IBM_HANDLE_INIT(sfan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775
2776 proc_dir = proc_mkdir(IBM_DIR, acpi_root_dir);
2777 if (!proc_dir) {
2778 printk(IBM_ERR "unable to create proc dir %s", IBM_DIR);
Henrique de Moraes Holschuh3945ac32007-02-06 19:13:44 -02002779 acpi_ibm_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 return -ENODEV;
2781 }
2782 proc_dir->owner = THIS_MODULE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002783
2784 for (i = 0; i < ARRAY_SIZE(ibms); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 ret = ibm_init(&ibms[i]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002786 if (ret >= 0 && *ibms[i].param)
2787 ret = ibms[i].write(ibms[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 if (ret < 0) {
2789 acpi_ibm_exit();
2790 return ret;
2791 }
2792 }
2793
2794 return 0;
2795}
2796
2797module_init(acpi_ibm_init);
2798module_exit(acpi_ibm_exit);