blob: 92e7b6eb8f1903db2a14a494704cda2d75df283b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * ibm_acpi.c - IBM ThinkPad ACPI Extras
3 *
4 *
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
Henrique de Moraes Holschuhf9ff43a2006-11-25 16:37:38 -02006 * Copyright (C) 2006 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Borislav Deianov78f81cc2005-08-17 00:00:00 -040021 */
22
Henrique de Moraes Holschuhf9ff43a2006-11-25 16:37:38 -020023#define IBM_VERSION "0.13"
Borislav Deianov78f81cc2005-08-17 00:00:00 -040024
25/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 * Changelog:
Henrique de Moraes Holschuhf9ff43a2006-11-25 16:37:38 -020027 *
28 * 2006-11-22 0.13 new maintainer
29 * changelog now lives in git commit history, and will
30 * not be updated further in-file.
Borislav Deianov78f81cc2005-08-17 00:00:00 -040031 *
32 * 2005-08-17 0.12 fix compilation on 2.6.13-rc kernels
33 * 2005-03-17 0.11 support for 600e, 770x
34 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
35 * support for 770e, G41
36 * G40 and G41 don't have a thinklight
37 * temperatures no longer experimental
38 * experimental brightness control
39 * experimental volume control
40 * experimental fan enable/disable
41 * 2005-01-16 0.10 fix module loading on R30, R31
42 * 2005-01-16 0.9 support for 570, R30, R31
43 * ultrabay support on A22p, A3x
44 * limit arg for cmos, led, beep, drop experimental status
45 * more capable led control on A21e, A22p, T20-22, X20
46 * experimental temperatures and fan speed
47 * experimental embedded controller register dump
48 * mark more functions as __init, drop incorrect __exit
49 * use MODULE_VERSION
50 * thanks to Henrik Brix Andersen <brix@gentoo.org>
51 * fix parameter passing on module loading
52 * thanks to Rusty Russell <rusty@rustcorp.com.au>
53 * thanks to Jim Radford <radford@blackbean.org>
54 * 2004-11-08 0.8 fix init error case, don't return from a macro
55 * thanks to Chris Wright <chrisw@osdl.org>
56 * 2004-10-23 0.7 fix module loading on A21e, A22p, T20, T21, X20
57 * fix led control on A21e
58 * 2004-10-19 0.6 use acpi_bus_register_driver() to claim HKEY device
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 * 2004-10-18 0.5 thinklight support on A21e, G40, R32, T20, T21, X20
60 * proc file format changed
61 * video_switch command
62 * experimental cmos control
63 * experimental led control
64 * experimental acpi sounds
Borislav Deianov78f81cc2005-08-17 00:00:00 -040065 * 2004-09-16 0.4 support for module parameters
66 * hotkey mask can be prefixed by 0x
67 * video output switching
68 * video expansion control
69 * ultrabay eject support
70 * removed lcd brightness/on/off control, didn't work
71 * 2004-08-17 0.3 support for R40
72 * lcd off, brightness control
73 * thinklight on/off
74 * 2004-08-14 0.2 support for T series, X20
75 * bluetooth enable/disable
76 * hotkey events disabled by default
77 * removed fan control, currently useless
78 * 2004-08-09 0.1 initial release, support for X series
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 */
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#include <linux/kernel.h>
82#include <linux/module.h>
83#include <linux/init.h>
84#include <linux/types.h>
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -020085#include <linux/string.h>
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -020086
Linus Torvalds1da177e2005-04-16 15:20:36 -070087#include <linux/proc_fs.h>
Holger Macht8acb0252006-10-20 14:30:28 -070088#include <linux/backlight.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070089#include <asm/uaccess.h>
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -020090
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -020091#include <linux/dmi.h>
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -020092#include <linux/jiffies.h>
93#include <linux/workqueue.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95#include <acpi/acpi_drivers.h>
96#include <acpi/acnamesp.h>
97
98#define IBM_NAME "ibm"
99#define IBM_DESC "IBM ThinkPad ACPI Extras"
100#define IBM_FILE "ibm_acpi"
101#define IBM_URL "http://ibm-acpi.sf.net/"
102
Henrique de Moraes Holschuhf9ff43a2006-11-25 16:37:38 -0200103MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400104MODULE_DESCRIPTION(IBM_DESC);
105MODULE_VERSION(IBM_VERSION);
106MODULE_LICENSE("GPL");
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108#define IBM_DIR IBM_NAME
109
110#define IBM_LOG IBM_FILE ": "
111#define IBM_ERR KERN_ERR IBM_LOG
112#define IBM_NOTICE KERN_NOTICE IBM_LOG
113#define IBM_INFO KERN_INFO IBM_LOG
114#define IBM_DEBUG KERN_DEBUG IBM_LOG
115
116#define IBM_MAX_ACPI_ARGS 3
117
118#define __unused __attribute__ ((unused))
119
120static int experimental;
121module_param(experimental, int, 0);
122
123static acpi_handle root_handle = NULL;
124
125#define IBM_HANDLE(object, parent, paths...) \
126 static acpi_handle object##_handle; \
127 static acpi_handle *object##_parent = &parent##_handle; \
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400128 static char *object##_path; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 static char *object##_paths[] = { paths }
130
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400131IBM_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
132 "\\_SB.PCI.ISA.EC", /* 570 */
133 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
134 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
135 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
136 "\\_SB.PCI0.ICH3.EC0", /* R31 */
137 "\\_SB.PCI0.LPC.EC", /* all others */
138 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400140IBM_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
141 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
142 "\\_SB.PCI0.VID0", /* 770e */
143 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
144 "\\_SB.PCI0.AGP.VID", /* all others */
145 ); /* R30, R31 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400147IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400149IBM_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, T4x, X31, X40 */
150 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
151 "\\CMS", /* R40, R40e */
152 ); /* all others */
Kristen Accardi63e5f242006-02-23 17:56:06 -0800153#ifdef CONFIG_ACPI_IBM_DOCK
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400154IBM_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
155 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
156 "\\_SB.PCI0.PCI1.DOCK", /* all others */
157 "\\_SB.PCI.ISA.SLCE", /* 570 */
158 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
Kristen Accardi63e5f242006-02-23 17:56:06 -0800159#endif
Henrique de Moraes Holschuh2df910b2006-11-25 16:35:09 -0200160#ifdef CONFIG_ACPI_IBM_BAY
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400161IBM_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
162 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhe0298992006-11-25 16:35:09 -0200163 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400164 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
165 ); /* A21e, R30, R31 */
166
167IBM_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
168 "_EJ0", /* all others */
169 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
170
171IBM_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
172 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
173 ); /* all others */
174
175IBM_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
176 "_EJ0", /* 770x */
177 ); /* all others */
Henrique de Moraes Holschuh2df910b2006-11-25 16:35:09 -0200178#endif
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400179
180/* don't list other alternatives as we install a notify handler on the 570 */
181IBM_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
182
183IBM_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
184 "^HKEY", /* R30, R31 */
185 "HKEY", /* all others */
186 ); /* 570 */
187
188IBM_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
189IBM_HANDLE(ledb, ec, "LEDB"); /* G4x */
190
191IBM_HANDLE(led, ec, "SLED", /* 570 */
192 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
193 "LED", /* all others */
194 ); /* R30, R31 */
195
196IBM_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
197IBM_HANDLE(ecrd, ec, "ECRD"); /* 570 */
198IBM_HANDLE(ecwr, ec, "ECWR"); /* 570 */
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200199IBM_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400200
201IBM_HANDLE(gfan, ec, "GFAN", /* 570 */
202 "\\FSPD", /* 600e/x, 770e, 770x */
203 ); /* all others */
204
205IBM_HANDLE(sfan, ec, "SFAN", /* 570 */
206 "JFNS", /* 770x-JL */
207 ); /* all others */
208
209#define IBM_HKEY_HID "IBM0068"
210#define IBM_PCI_HID "PNP0A03"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -0200212enum thermal_access_mode {
213 IBMACPI_THERMAL_NONE = 0, /* No thermal support */
214 IBMACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
215 IBMACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -0200216 IBMACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
217 IBMACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -0200218};
219
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -0200220#define IBMACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -0200221struct ibm_thermal_sensors_struct {
222 s32 temp[IBMACPI_MAX_THERMAL_SENSORS];
223};
224
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200225/*
226 * FAN ACCESS MODES
227 *
228 * IBMACPI_FAN_RD_ACPI_GFAN:
229 * ACPI GFAN method: returns fan level
230 *
231 * see IBMACPI_FAN_WR_ACPI_SFAN
232 * EC 0x2f not available if GFAN exists
233 *
234 * IBMACPI_FAN_WR_ACPI_SFAN:
235 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
236 *
237 * EC 0x2f might be available *for reading*, but never for writing.
238 *
239 * IBMACPI_FAN_WR_TPEC:
240 * ThinkPad EC register 0x2f (HFSP): fan control loop mode Supported
241 * on almost all ThinkPads
242 *
243 * Fan speed changes of any sort (including those caused by the
244 * disengaged mode) are usually done slowly by the firmware as the
245 * maximum ammount of fan duty cycle change per second seems to be
246 * limited.
247 *
248 * Reading is not available if GFAN exists.
249 * Writing is not available if SFAN exists.
250 *
251 * Bits
252 * 7 automatic mode engaged;
253 * (default operation mode of the ThinkPad)
254 * fan level is ignored in this mode.
255 * 6 disengage mode (takes precedence over bit 7);
256 * not available on all thinkpads. May disable
257 * the tachometer, and speeds up fan to 100% duty-cycle,
258 * which speeds it up far above the standard RPM
259 * levels. It is not impossible that it could cause
260 * hardware damage.
261 * 5-3 unused in some models. Extra bits for fan level
262 * in others, but still useless as all values above
263 * 7 map to the same speed as level 7 in these models.
264 * 2-0 fan level (0..7 usually)
265 * 0x00 = stop
266 * 0x07 = max (set when temperatures critical)
267 * Some ThinkPads may have other levels, see
268 * IBMACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
269 *
270 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
271 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
272 * does so, its initial value is meaningless (0x07).
273 *
274 * For firmware bugs, refer to:
275 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
276 *
277 * ----
278 *
279 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
280 * Main fan tachometer reading (in RPM)
281 *
282 * This register is present on all ThinkPads with a new-style EC, and
283 * it is known not to be present on the A21m/e, and T22, as there is
284 * something else in offset 0x84 according to the ACPI DSDT. Other
285 * ThinkPads from this same time period (and earlier) probably lack the
286 * tachometer as well.
287 *
288 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
289 * was never fixed by IBM to report the EC firmware version string
290 * probably support the tachometer (like the early X models), so
291 * detecting it is quite hard. We need more data to know for sure.
292 *
293 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
294 * might result.
295 *
296 * FIRMWARE BUG: when EC 0x2f bit 6 is set (disengaged mode), this
297 * register is not invalidated in ThinkPads that disable tachometer
298 * readings. Thus, the tachometer readings go stale.
299 *
300 * For firmware bugs, refer to:
301 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
302 *
303 * IBMACPI_FAN_WR_ACPI_FANS:
304 * ThinkPad X31, X40, X41. Not available in the X60.
305 *
306 * FANS ACPI handle: takes three arguments: low speed, medium speed,
307 * high speed. ACPI DSDT seems to map these three speeds to levels
308 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
309 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
310 *
311 * The speeds are stored on handles
312 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
313 *
314 * There are three default speed sets, acessible as handles:
315 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
316 *
317 * ACPI DSDT switches which set is in use depending on various
318 * factors.
319 *
320 * IBMACPI_FAN_WR_TPEC is also available and should be used to
321 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
322 * but the ACPI tables just mention level 7.
323 */
324
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -0200325enum fan_status_access_mode {
326 IBMACPI_FAN_NONE = 0, /* No fan status or control */
327 IBMACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
328 IBMACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
329};
330
331enum fan_control_access_mode {
332 IBMACPI_FAN_WR_NONE = 0, /* No fan control */
333 IBMACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
334 IBMACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
335 IBMACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
336};
337
338enum fan_control_commands {
339 IBMACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
340 IBMACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -0200341 IBMACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
342 * and also watchdog cmd */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -0200343};
344
345enum { /* Fan control constants */
346 fan_status_offset = 0x2f, /* EC register 0x2f */
347 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
348 * 0x84 must be read before 0x85 */
Henrique de Moraes Holschuhbab812a2006-11-24 11:47:12 -0200349
350 IBMACPI_FAN_EC_DISENGAGED = 0x40, /* EC mode: tachometer
351 * disengaged */
352 IBMACPI_FAN_EC_AUTO = 0x80, /* EC mode: auto fan
353 * control */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -0200354};
355
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -0200356static char *ibm_thinkpad_ec_found = NULL;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -0200357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358struct ibm_struct {
359 char *name;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400360 char param[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
362 char *hid;
363 struct acpi_driver *driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400365 int (*init) (void);
366 int (*read) (char *);
367 int (*write) (char *);
368 void (*exit) (void);
369
370 void (*notify) (struct ibm_struct *, u32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 acpi_handle *handle;
372 int type;
373 struct acpi_device *device;
374
375 int driver_registered;
376 int proc_created;
377 int init_called;
378 int notify_installed;
379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 int experimental;
381};
382
383static struct proc_dir_entry *proc_dir = NULL;
384
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -0200385static struct backlight_device *ibm_backlight_device = NULL;
Holger Macht8acb0252006-10-20 14:30:28 -0700386
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387#define onoff(status,bit) ((status) & (1 << (bit)) ? "on" : "off")
388#define enabled(status,bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
389#define strlencmp(a,b) (strncmp((a), (b), strlen(b)))
390
391static int acpi_evalf(acpi_handle handle,
392 void *res, char *method, char *fmt, ...)
393{
394 char *fmt0 = fmt;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400395 struct acpi_object_list params;
396 union acpi_object in_objs[IBM_MAX_ACPI_ARGS];
397 struct acpi_buffer result, *resultp;
398 union acpi_object out_obj;
399 acpi_status status;
400 va_list ap;
401 char res_type;
402 int success;
403 int quiet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
405 if (!*fmt) {
406 printk(IBM_ERR "acpi_evalf() called with empty format\n");
407 return 0;
408 }
409
410 if (*fmt == 'q') {
411 quiet = 1;
412 fmt++;
413 } else
414 quiet = 0;
415
416 res_type = *(fmt++);
417
418 params.count = 0;
419 params.pointer = &in_objs[0];
420
421 va_start(ap, fmt);
422 while (*fmt) {
423 char c = *(fmt++);
424 switch (c) {
425 case 'd': /* int */
426 in_objs[params.count].integer.value = va_arg(ap, int);
427 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
428 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400429 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 default:
431 printk(IBM_ERR "acpi_evalf() called "
432 "with invalid format character '%c'\n", c);
433 return 0;
434 }
435 }
436 va_end(ap);
437
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400438 if (res_type != 'v') {
439 result.length = sizeof(out_obj);
440 result.pointer = &out_obj;
441 resultp = &result;
442 } else
443 resultp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400445 status = acpi_evaluate_object(handle, method, &params, resultp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
447 switch (res_type) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400448 case 'd': /* int */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 if (res)
450 *(int *)res = out_obj.integer.value;
451 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
452 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400453 case 'v': /* void */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 success = status == AE_OK;
455 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400456 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 default:
458 printk(IBM_ERR "acpi_evalf() called "
459 "with invalid format character '%c'\n", res_type);
460 return 0;
461 }
462
463 if (!success && !quiet)
464 printk(IBM_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
465 method, fmt0, status);
466
467 return success;
468}
469
470static void __unused acpi_print_int(acpi_handle handle, char *method)
471{
472 int i;
473
474 if (acpi_evalf(handle, &i, method, "d"))
475 printk(IBM_INFO "%s = 0x%x\n", method, i);
476 else
477 printk(IBM_ERR "error calling %s\n", method);
478}
479
480static char *next_cmd(char **cmds)
481{
482 char *start = *cmds;
483 char *end;
484
485 while ((end = strchr(start, ',')) && end == start)
486 start = end + 1;
487
488 if (!end)
489 return NULL;
490
491 *end = 0;
492 *cmds = end + 1;
493 return start;
494}
495
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400496static int driver_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
498 printk(IBM_INFO "%s v%s\n", IBM_DESC, IBM_VERSION);
499 printk(IBM_INFO "%s\n", IBM_URL);
500
501 return 0;
502}
503
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400504static int driver_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505{
506 int len = 0;
507
508 len += sprintf(p + len, "driver:\t\t%s\n", IBM_DESC);
509 len += sprintf(p + len, "version:\t%s\n", IBM_VERSION);
510
511 return len;
512}
513
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400514static int hotkey_supported;
515static int hotkey_mask_supported;
516static int hotkey_orig_status;
517static int hotkey_orig_mask;
518
519static int hotkey_get(int *status, int *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520{
521 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400522 return 0;
523
524 if (hotkey_mask_supported)
525 if (!acpi_evalf(hkey_handle, mask, "DHKN", "d"))
526 return 0;
527
528 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529}
530
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400531static int hotkey_set(int status, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
533 int i;
534
535 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 return 0;
537
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400538 if (hotkey_mask_supported)
539 for (i = 0; i < 32; i++) {
540 int bit = ((1 << i) & mask) != 0;
541 if (!acpi_evalf(hkey_handle,
542 NULL, "MHKM", "vdd", i + 1, bit))
543 return 0;
544 }
545
546 return 1;
547}
548
549static int hotkey_init(void)
550{
551 /* hotkey not supported on 570 */
552 hotkey_supported = hkey_handle != NULL;
553
554 if (hotkey_supported) {
555 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
556 A30, R30, R31, T20-22, X20-21, X22-24 */
557 hotkey_mask_supported =
558 acpi_evalf(hkey_handle, NULL, "DHKN", "qv");
559
560 if (!hotkey_get(&hotkey_orig_status, &hotkey_orig_mask))
561 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 }
563
564 return 0;
565}
566
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400567static int hotkey_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
569 int status, mask;
570 int len = 0;
571
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400572 if (!hotkey_supported) {
573 len += sprintf(p + len, "status:\t\tnot supported\n");
574 return len;
575 }
576
577 if (!hotkey_get(&status, &mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 return -EIO;
579
580 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400581 if (hotkey_mask_supported) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 len += sprintf(p + len, "mask:\t\t0x%04x\n", mask);
583 len += sprintf(p + len,
584 "commands:\tenable, disable, reset, <mask>\n");
585 } else {
586 len += sprintf(p + len, "mask:\t\tnot supported\n");
587 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
588 }
589
590 return len;
591}
592
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400593static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
595 int status, mask;
596 char *cmd;
597 int do_cmd = 0;
598
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400599 if (!hotkey_supported)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 return -ENODEV;
601
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400602 if (!hotkey_get(&status, &mask))
603 return -EIO;
604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 while ((cmd = next_cmd(&buf))) {
606 if (strlencmp(cmd, "enable") == 0) {
607 status = 1;
608 } else if (strlencmp(cmd, "disable") == 0) {
609 status = 0;
610 } else if (strlencmp(cmd, "reset") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400611 status = hotkey_orig_status;
612 mask = hotkey_orig_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
614 /* mask set */
615 } else if (sscanf(cmd, "%x", &mask) == 1) {
616 /* mask set */
617 } else
618 return -EINVAL;
619 do_cmd = 1;
620 }
621
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400622 if (do_cmd && !hotkey_set(status, mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 return -EIO;
624
625 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400626}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400628static void hotkey_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400630 if (hotkey_supported)
631 hotkey_set(hotkey_orig_status, hotkey_orig_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632}
633
634static void hotkey_notify(struct ibm_struct *ibm, u32 event)
635{
636 int hkey;
637
638 if (acpi_evalf(hkey_handle, &hkey, "MHKP", "d"))
639 acpi_bus_generate_event(ibm->device, event, hkey);
640 else {
641 printk(IBM_ERR "unknown hotkey event %d\n", event);
642 acpi_bus_generate_event(ibm->device, event, 0);
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400643 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644}
645
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400646static int bluetooth_supported;
647
648static int bluetooth_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400650 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
651 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
652 bluetooth_supported = hkey_handle &&
653 acpi_evalf(hkey_handle, NULL, "GBDC", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
655 return 0;
656}
657
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400658static int bluetooth_status(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659{
660 int status;
661
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400662 if (!bluetooth_supported ||
663 !acpi_evalf(hkey_handle, &status, "GBDC", "d"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 status = 0;
665
666 return status;
667}
668
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400669static int bluetooth_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670{
671 int len = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400672 int status = bluetooth_status();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400674 if (!bluetooth_supported)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 len += sprintf(p + len, "status:\t\tnot supported\n");
676 else if (!(status & 1))
677 len += sprintf(p + len, "status:\t\tnot installed\n");
678 else {
679 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 1));
680 len += sprintf(p + len, "commands:\tenable, disable\n");
681 }
682
683 return len;
684}
685
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400686static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400688 int status = bluetooth_status();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 char *cmd;
690 int do_cmd = 0;
691
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400692 if (!bluetooth_supported)
693 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695 while ((cmd = next_cmd(&buf))) {
696 if (strlencmp(cmd, "enable") == 0) {
697 status |= 2;
698 } else if (strlencmp(cmd, "disable") == 0) {
699 status &= ~2;
700 } else
701 return -EINVAL;
702 do_cmd = 1;
703 }
704
705 if (do_cmd && !acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400706 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
708 return 0;
709}
710
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -0400711static int wan_supported;
712
713static int wan_init(void)
714{
715 wan_supported = hkey_handle &&
716 acpi_evalf(hkey_handle, NULL, "GWAN", "qv");
717
718 return 0;
719}
720
721static int wan_status(void)
722{
723 int status;
724
Henrique de Moraes Holschuh8d297262006-11-24 11:47:07 -0200725 if (!wan_supported || !acpi_evalf(hkey_handle, &status, "GWAN", "d"))
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -0400726 status = 0;
727
728 return status;
729}
730
731static int wan_read(char *p)
732{
733 int len = 0;
734 int status = wan_status();
735
736 if (!wan_supported)
737 len += sprintf(p + len, "status:\t\tnot supported\n");
738 else if (!(status & 1))
739 len += sprintf(p + len, "status:\t\tnot installed\n");
740 else {
741 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 1));
742 len += sprintf(p + len, "commands:\tenable, disable\n");
743 }
744
745 return len;
746}
747
748static int wan_write(char *buf)
749{
750 int status = wan_status();
751 char *cmd;
752 int do_cmd = 0;
753
754 if (!wan_supported)
755 return -ENODEV;
756
757 while ((cmd = next_cmd(&buf))) {
758 if (strlencmp(cmd, "enable") == 0) {
759 status |= 2;
760 } else if (strlencmp(cmd, "disable") == 0) {
761 status &= ~2;
762 } else
763 return -EINVAL;
764 do_cmd = 1;
765 }
766
767 if (do_cmd && !acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
768 return -EIO;
769
770 return 0;
771}
772
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200773enum video_access_mode {
774 IBMACPI_VIDEO_NONE = 0,
775 IBMACPI_VIDEO_570, /* 570 */
776 IBMACPI_VIDEO_770, /* 600e/x, 770e, 770x */
777 IBMACPI_VIDEO_NEW, /* all others */
778};
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400779
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200780static enum video_access_mode video_supported;
781static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400782
783static int video_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400785 int ivga;
786
787 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
788 /* G41, assume IVGA doesn't change */
789 vid_handle = vid2_handle;
790
791 if (!vid_handle)
792 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200793 video_supported = IBMACPI_VIDEO_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400794 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
795 /* 570 */
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200796 video_supported = IBMACPI_VIDEO_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400797 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
798 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200799 video_supported = IBMACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400800 else
801 /* all others */
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200802 video_supported = IBMACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
804 return 0;
805}
806
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400807static int video_status(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808{
809 int status = 0;
810 int i;
811
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200812 if (video_supported == IBMACPI_VIDEO_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400813 if (acpi_evalf(NULL, &i, "\\_SB.PHS", "dd", 0x87))
814 status = i & 3;
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200815 } else if (video_supported == IBMACPI_VIDEO_770) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400816 if (acpi_evalf(NULL, &i, "\\VCDL", "d"))
817 status |= 0x01 * i;
818 if (acpi_evalf(NULL, &i, "\\VCDC", "d"))
819 status |= 0x02 * i;
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200820 } else if (video_supported == IBMACPI_VIDEO_NEW) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400821 acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1);
822 if (acpi_evalf(NULL, &i, "\\VCDC", "d"))
823 status |= 0x02 * i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400825 acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0);
826 if (acpi_evalf(NULL, &i, "\\VCDL", "d"))
827 status |= 0x01 * i;
828 if (acpi_evalf(NULL, &i, "\\VCDD", "d"))
829 status |= 0x08 * i;
830 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
832 return status;
833}
834
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400835static int video_autosw(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400837 int autosw = 0;
838
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200839 if (video_supported == IBMACPI_VIDEO_570)
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400840 acpi_evalf(vid_handle, &autosw, "SWIT", "d");
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200841 else if (video_supported == IBMACPI_VIDEO_770 ||
842 video_supported == IBMACPI_VIDEO_NEW)
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400843 acpi_evalf(vid_handle, &autosw, "^VDEE", "d");
844
845 return autosw & 1;
846}
847
848static int video_read(char *p)
849{
850 int status = video_status();
851 int autosw = video_autosw();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 int len = 0;
853
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400854 if (!video_supported) {
855 len += sprintf(p + len, "status:\t\tnot supported\n");
856 return len;
857 }
858
859 len += sprintf(p + len, "status:\t\tsupported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
861 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200862 if (video_supported == IBMACPI_VIDEO_NEW)
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400863 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
864 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
865 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
866 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
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, "commands:\tdvi_enable, dvi_disable\n");
869 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
871
872 return len;
873}
874
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400875static int video_switch(void)
876{
877 int autosw = video_autosw();
878 int ret;
879
880 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
881 return -EIO;
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200882 ret = video_supported == IBMACPI_VIDEO_570 ?
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400883 acpi_evalf(ec_handle, NULL, "_Q16", "v") :
884 acpi_evalf(vid_handle, NULL, "VSWT", "v");
885 acpi_evalf(vid_handle, NULL, "_DOS", "vd", autosw);
886
887 return ret;
888}
889
890static int video_expand(void)
891{
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200892 if (video_supported == IBMACPI_VIDEO_570)
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400893 return acpi_evalf(ec_handle, NULL, "_Q17", "v");
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200894 else if (video_supported == IBMACPI_VIDEO_770)
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400895 return acpi_evalf(vid_handle, NULL, "VEXP", "v");
896 else
897 return acpi_evalf(NULL, NULL, "\\VEXP", "v");
898}
899
900static int video_switch2(int status)
901{
902 int ret;
903
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200904 if (video_supported == IBMACPI_VIDEO_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400905 ret = acpi_evalf(NULL, NULL,
906 "\\_SB.PHS2", "vdd", 0x8b, status | 0x80);
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200907 } else if (video_supported == IBMACPI_VIDEO_770) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400908 int autosw = video_autosw();
909 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
910 return -EIO;
911
912 ret = acpi_evalf(vid_handle, NULL,
913 "ASWT", "vdd", status * 0x100, 0);
914
915 acpi_evalf(vid_handle, NULL, "_DOS", "vd", autosw);
916 } else {
917 ret = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
918 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
919 }
920
921 return ret;
922}
923
924static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925{
926 char *cmd;
927 int enable, disable, status;
928
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400929 if (!video_supported)
930 return -ENODEV;
931
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 enable = disable = 0;
933
934 while ((cmd = next_cmd(&buf))) {
935 if (strlencmp(cmd, "lcd_enable") == 0) {
936 enable |= 0x01;
937 } else if (strlencmp(cmd, "lcd_disable") == 0) {
938 disable |= 0x01;
939 } else if (strlencmp(cmd, "crt_enable") == 0) {
940 enable |= 0x02;
941 } else if (strlencmp(cmd, "crt_disable") == 0) {
942 disable |= 0x02;
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200943 } else if (video_supported == IBMACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400944 strlencmp(cmd, "dvi_enable") == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 enable |= 0x08;
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200946 } else if (video_supported == IBMACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400947 strlencmp(cmd, "dvi_disable") == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 disable |= 0x08;
949 } else if (strlencmp(cmd, "auto_enable") == 0) {
950 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
951 return -EIO;
952 } else if (strlencmp(cmd, "auto_disable") == 0) {
953 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 0))
954 return -EIO;
955 } else if (strlencmp(cmd, "video_switch") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400956 if (!video_switch())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 return -EIO;
958 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400959 if (!video_expand())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 return -EIO;
961 } else
962 return -EINVAL;
963 }
964
965 if (enable || disable) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400966 status = (video_status() & 0x0f & ~disable) | enable;
967 if (!video_switch2(status))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 return -EIO;
969 }
970
971 return 0;
972}
973
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400974static void video_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400976 acpi_evalf(vid_handle, NULL, "_DOS", "vd", video_orig_autosw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977}
978
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400979static int light_supported;
980static int light_status_supported;
981
982static int light_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400984 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
985 light_supported = (cmos_handle || lght_handle) && !ledb_handle;
986
987 if (light_supported)
988 /* light status not supported on
989 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
990 light_status_supported = acpi_evalf(ec_handle, NULL,
991 "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
993 return 0;
994}
995
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400996static int light_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997{
998 int len = 0;
999 int status = 0;
1000
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001001 if (!light_supported) {
1002 len += sprintf(p + len, "status:\t\tnot supported\n");
1003 } else if (!light_status_supported) {
1004 len += sprintf(p + len, "status:\t\tunknown\n");
1005 len += sprintf(p + len, "commands:\ton, off\n");
1006 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
1008 return -EIO;
1009 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001010 len += sprintf(p + len, "commands:\ton, off\n");
1011 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
1013 return len;
1014}
1015
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001016static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017{
1018 int cmos_cmd, lght_cmd;
1019 char *cmd;
1020 int success;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001021
1022 if (!light_supported)
1023 return -ENODEV;
1024
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 while ((cmd = next_cmd(&buf))) {
1026 if (strlencmp(cmd, "on") == 0) {
1027 cmos_cmd = 0x0c;
1028 lght_cmd = 1;
1029 } else if (strlencmp(cmd, "off") == 0) {
1030 cmos_cmd = 0x0d;
1031 lght_cmd = 0;
1032 } else
1033 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001034
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 success = cmos_handle ?
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001036 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
1037 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 if (!success)
1039 return -EIO;
1040 }
1041
1042 return 0;
1043}
1044
Henrique de Moraes Holschuh2df910b2006-11-25 16:35:09 -02001045#if defined(CONFIG_ACPI_IBM_DOCK) || defined(CONFIG_ACPI_IBM_BAY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046static int _sta(acpi_handle handle)
1047{
1048 int status;
1049
1050 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
1051 status = 0;
1052
1053 return status;
1054}
Henrique de Moraes Holschuh2df910b2006-11-25 16:35:09 -02001055#endif
Kristen Accardi63e5f242006-02-23 17:56:06 -08001056#ifdef CONFIG_ACPI_IBM_DOCK
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057#define dock_docked() (_sta(dock_handle) & 1)
1058
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001059static int dock_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060{
1061 int len = 0;
1062 int docked = dock_docked();
1063
1064 if (!dock_handle)
1065 len += sprintf(p + len, "status:\t\tnot supported\n");
1066 else if (!docked)
1067 len += sprintf(p + len, "status:\t\tundocked\n");
1068 else {
1069 len += sprintf(p + len, "status:\t\tdocked\n");
1070 len += sprintf(p + len, "commands:\tdock, undock\n");
1071 }
1072
1073 return len;
1074}
1075
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001076static int dock_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077{
1078 char *cmd;
1079
1080 if (!dock_docked())
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001081 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
1083 while ((cmd = next_cmd(&buf))) {
1084 if (strlencmp(cmd, "undock") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001085 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
1086 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 return -EIO;
1088 } else if (strlencmp(cmd, "dock") == 0) {
1089 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
1090 return -EIO;
1091 } else
1092 return -EINVAL;
1093 }
1094
1095 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001096}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
1098static void dock_notify(struct ibm_struct *ibm, u32 event)
1099{
1100 int docked = dock_docked();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001101 int pci = ibm->hid && strstr(ibm->hid, IBM_PCI_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001103 if (event == 1 && !pci) /* 570 */
1104 acpi_bus_generate_event(ibm->device, event, 1); /* button */
1105 else if (event == 1 && pci) /* 570 */
1106 acpi_bus_generate_event(ibm->device, event, 3); /* dock */
1107 else if (event == 3 && docked)
1108 acpi_bus_generate_event(ibm->device, event, 1); /* button */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 else if (event == 3 && !docked)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001110 acpi_bus_generate_event(ibm->device, event, 2); /* undock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 else if (event == 0 && docked)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001112 acpi_bus_generate_event(ibm->device, event, 3); /* dock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 else {
1114 printk(IBM_ERR "unknown dock event %d, status %d\n",
1115 event, _sta(dock_handle));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001116 acpi_bus_generate_event(ibm->device, event, 0); /* unknown */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 }
1118}
Kristen Accardi63e5f242006-02-23 17:56:06 -08001119#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
Henrique de Moraes Holschuh2df910b2006-11-25 16:35:09 -02001121#ifdef CONFIG_ACPI_IBM_BAY
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001122static int bay_status_supported;
1123static int bay_status2_supported;
1124static int bay_eject_supported;
1125static int bay_eject2_supported;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001127static int bay_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001129 bay_status_supported = bay_handle &&
1130 acpi_evalf(bay_handle, NULL, "_STA", "qv");
1131 bay_status2_supported = bay2_handle &&
1132 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
1133
1134 bay_eject_supported = bay_handle && bay_ej_handle &&
1135 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
1136 bay_eject2_supported = bay2_handle && bay2_ej_handle &&
1137 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
1139 return 0;
1140}
1141
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001142#define bay_occupied(b) (_sta(b##_handle) & 1)
1143
1144static int bay_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145{
1146 int len = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001147 int occupied = bay_occupied(bay);
1148 int occupied2 = bay_occupied(bay2);
1149 int eject, eject2;
1150
1151 len += sprintf(p + len, "status:\t\t%s\n", bay_status_supported ?
1152 (occupied ? "occupied" : "unoccupied") :
1153 "not supported");
1154 if (bay_status2_supported)
1155 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
1156 "occupied" : "unoccupied");
1157
1158 eject = bay_eject_supported && occupied;
1159 eject2 = bay_eject2_supported && occupied2;
1160
1161 if (eject && eject2)
1162 len += sprintf(p + len, "commands:\teject, eject2\n");
1163 else if (eject)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 len += sprintf(p + len, "commands:\teject\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001165 else if (eject2)
1166 len += sprintf(p + len, "commands:\teject2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
1168 return len;
1169}
1170
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001171static int bay_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172{
1173 char *cmd;
1174
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001175 if (!bay_eject_supported && !bay_eject2_supported)
1176 return -ENODEV;
1177
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001179 if (bay_eject_supported && strlencmp(cmd, "eject") == 0) {
1180 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
1181 return -EIO;
1182 } else if (bay_eject2_supported &&
1183 strlencmp(cmd, "eject2") == 0) {
1184 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 return -EIO;
1186 } else
1187 return -EINVAL;
1188 }
1189
1190 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001191}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
1193static void bay_notify(struct ibm_struct *ibm, u32 event)
1194{
1195 acpi_bus_generate_event(ibm->device, event, 0);
1196}
Henrique de Moraes Holschuh2df910b2006-11-25 16:35:09 -02001197#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001199static int cmos_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200{
1201 int len = 0;
1202
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001203 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1204 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 if (!cmos_handle)
1206 len += sprintf(p + len, "status:\t\tnot supported\n");
1207 else {
1208 len += sprintf(p + len, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001209 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 }
1211
1212 return len;
1213}
1214
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001215static int cmos_eval(int cmos_cmd)
1216{
1217 if (cmos_handle)
1218 return acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd);
1219 else
1220 return 1;
1221}
1222
1223static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224{
1225 char *cmd;
1226 int cmos_cmd;
1227
1228 if (!cmos_handle)
1229 return -EINVAL;
1230
1231 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001232 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
1233 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 /* cmos_cmd set */
1235 } else
1236 return -EINVAL;
1237
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001238 if (!cmos_eval(cmos_cmd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 return -EIO;
1240 }
1241
1242 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001243}
1244
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02001245enum led_access_mode {
1246 IBMACPI_LED_NONE = 0,
1247 IBMACPI_LED_570, /* 570 */
1248 IBMACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
1249 IBMACPI_LED_NEW, /* all others */
1250};
1251static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001252
1253static int led_init(void)
1254{
1255 if (!led_handle)
1256 /* led not supported on R30, R31 */
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02001257 led_supported = IBMACPI_LED_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001258 else if (strlencmp(led_path, "SLED") == 0)
1259 /* 570 */
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02001260 led_supported = IBMACPI_LED_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001261 else if (strlencmp(led_path, "SYSL") == 0)
1262 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02001263 led_supported = IBMACPI_LED_OLD;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001264 else
1265 /* all others */
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02001266 led_supported = IBMACPI_LED_NEW;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001267
1268 return 0;
1269}
1270
1271#define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
1272
1273static int led_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274{
1275 int len = 0;
1276
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001277 if (!led_supported) {
1278 len += sprintf(p + len, "status:\t\tnot supported\n");
1279 return len;
1280 }
1281 len += sprintf(p + len, "status:\t\tsupported\n");
1282
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02001283 if (led_supported == IBMACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001284 /* 570 */
1285 int i, status;
1286 for (i = 0; i < 8; i++) {
1287 if (!acpi_evalf(ec_handle,
1288 &status, "GLED", "dd", 1 << i))
1289 return -EIO;
1290 len += sprintf(p + len, "%d:\t\t%s\n",
1291 i, led_status(status));
1292 }
1293 }
1294
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 len += sprintf(p + len, "commands:\t"
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001296 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
1298 return len;
1299}
1300
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001301/* off, on, blink */
1302static const int led_sled_arg1[] = { 0, 1, 3 };
1303static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
1304static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
1305static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
1306
1307#define EC_HLCL 0x0c
1308#define EC_HLBL 0x0d
1309#define EC_HLMS 0x0e
1310
1311static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312{
1313 char *cmd;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001314 int led, ind, ret;
1315
1316 if (!led_supported)
1317 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
1319 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001320 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 return -EINVAL;
1322
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001323 if (strstr(cmd, "off")) {
1324 ind = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 } else if (strstr(cmd, "on")) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001326 ind = 1;
1327 } else if (strstr(cmd, "blink")) {
1328 ind = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 } else
1330 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001331
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02001332 if (led_supported == IBMACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001333 /* 570 */
1334 led = 1 << led;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001336 led, led_sled_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 return -EIO;
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02001338 } else if (led_supported == IBMACPI_LED_OLD) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001339 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
1340 led = 1 << led;
1341 ret = ec_write(EC_HLMS, led);
1342 if (ret >= 0)
1343 ret =
1344 ec_write(EC_HLBL, led * led_exp_hlbl[ind]);
1345 if (ret >= 0)
1346 ret =
1347 ec_write(EC_HLCL, led * led_exp_hlcl[ind]);
1348 if (ret < 0)
1349 return ret;
1350 } else {
1351 /* all others */
1352 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
1353 led, led_led_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001355 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 }
1357
1358 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001359}
1360
1361static int beep_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362{
1363 int len = 0;
1364
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001365 if (!beep_handle)
1366 len += sprintf(p + len, "status:\t\tnot supported\n");
1367 else {
1368 len += sprintf(p + len, "status:\t\tsupported\n");
1369 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
1370 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
1372 return len;
1373}
1374
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001375static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376{
1377 char *cmd;
1378 int beep_cmd;
1379
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001380 if (!beep_handle)
1381 return -ENODEV;
1382
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001384 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
1385 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 /* beep_cmd set */
1387 } else
1388 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001389 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 return -EIO;
1391 }
1392
1393 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001394}
1395
1396static int acpi_ec_read(int i, u8 * p)
1397{
1398 int v;
1399
1400 if (ecrd_handle) {
1401 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
1402 return 0;
1403 *p = v;
1404 } else {
1405 if (ec_read(i, p) < 0)
1406 return 0;
1407 }
1408
1409 return 1;
1410}
1411
1412static int acpi_ec_write(int i, u8 v)
1413{
1414 if (ecwr_handle) {
1415 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
1416 return 0;
1417 } else {
1418 if (ec_write(i, v) < 0)
1419 return 0;
1420 }
1421
1422 return 1;
1423}
1424
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001425static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001426
1427static int thermal_init(void)
1428{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001429 u8 t, ta1, ta2;
1430 int i;
1431 int acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
1432
1433 if (ibm_thinkpad_ec_found && experimental) {
1434 /*
1435 * Direct EC access mode: sensors at registers
1436 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
1437 * non-implemented, thermal sensors return 0x80 when
1438 * not available
1439 */
1440
1441 ta1 = ta2 = 0;
1442 for (i = 0; i < 8; i++) {
1443 if (likely(acpi_ec_read(0x78 + i, &t))) {
1444 ta1 |= t;
1445 } else {
1446 ta1 = 0;
1447 break;
1448 }
1449 if (likely(acpi_ec_read(0xC0 + i, &t))) {
1450 ta2 |= t;
1451 } else {
1452 ta1 = 0;
1453 break;
1454 }
1455 }
1456 if (ta1 == 0) {
1457 /* This is sheer paranoia, but we handle it anyway */
1458 if (acpi_tmp7) {
1459 printk(IBM_ERR
1460 "ThinkPad ACPI EC access misbehaving, "
1461 "falling back to ACPI TMPx access mode\n");
1462 thermal_read_mode = IBMACPI_THERMAL_ACPI_TMP07;
1463 } else {
1464 printk(IBM_ERR
1465 "ThinkPad ACPI EC access misbehaving, "
1466 "disabling thermal sensors access\n");
1467 thermal_read_mode = IBMACPI_THERMAL_NONE;
1468 }
1469 } else {
1470 thermal_read_mode =
1471 (ta2 != 0) ?
1472 IBMACPI_THERMAL_TPEC_16 : IBMACPI_THERMAL_TPEC_8;
1473 }
1474 } else if (acpi_tmp7) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001475 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
1476 /* 600e/x, 770e, 770x */
1477 thermal_read_mode = IBMACPI_THERMAL_ACPI_UPDT;
1478 } else {
1479 /* Standard ACPI TMPx access, max 8 sensors */
1480 thermal_read_mode = IBMACPI_THERMAL_ACPI_TMP07;
1481 }
1482 } else {
1483 /* temperatures not supported on 570, G4x, R30, R31, R32 */
1484 thermal_read_mode = IBMACPI_THERMAL_NONE;
1485 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001486
1487 return 0;
1488}
1489
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001490static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
1491{
1492 int i, t;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001493 s8 tmp;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001494 char tmpi[] = "TMPi";
1495
1496 if (!s)
1497 return -EINVAL;
1498
1499 switch (thermal_read_mode) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001500#if IBMACPI_MAX_THERMAL_SENSORS >= 16
1501 case IBMACPI_THERMAL_TPEC_16:
1502 for (i = 0; i < 8; i++) {
1503 if (!acpi_ec_read(0xC0 + i, &tmp))
1504 return -EIO;
1505 s->temp[i + 8] = tmp * 1000;
1506 }
1507 /* fallthrough */
1508#endif
1509 case IBMACPI_THERMAL_TPEC_8:
1510 for (i = 0; i < 8; i++) {
1511 if (!acpi_ec_read(0x78 + i, &tmp))
1512 return -EIO;
1513 s->temp[i] = tmp * 1000;
1514 }
1515 return (thermal_read_mode == IBMACPI_THERMAL_TPEC_16) ? 16 : 8;
1516
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001517 case IBMACPI_THERMAL_ACPI_UPDT:
1518 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
1519 return -EIO;
1520 for (i = 0; i < 8; i++) {
1521 tmpi[3] = '0' + i;
1522 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
1523 return -EIO;
1524 s->temp[i] = (t - 2732) * 100;
1525 }
1526 return 8;
1527
1528 case IBMACPI_THERMAL_ACPI_TMP07:
1529 for (i = 0; i < 8; i++) {
1530 tmpi[3] = '0' + i;
1531 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
1532 return -EIO;
1533 s->temp[i] = t * 1000;
1534 }
1535 return 8;
1536
1537 case IBMACPI_THERMAL_NONE:
1538 default:
1539 return 0;
1540 }
1541}
1542
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001543static int thermal_read(char *p)
1544{
1545 int len = 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001546 int n, i;
1547 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001548
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001549 n = thermal_get_sensors(&t);
1550 if (unlikely(n < 0))
1551 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001552
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001553 len += sprintf(p + len, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001554
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001555 if (n > 0) {
1556 for (i = 0; i < (n - 1); i++)
1557 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
1558 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
1559 } else
1560 len += sprintf(p + len, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001561
1562 return len;
1563}
1564
1565static u8 ecdump_regs[256];
1566
1567static int ecdump_read(char *p)
1568{
1569 int len = 0;
1570 int i, j;
1571 u8 v;
1572
1573 len += sprintf(p + len, "EC "
1574 " +00 +01 +02 +03 +04 +05 +06 +07"
1575 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
1576 for (i = 0; i < 256; i += 16) {
1577 len += sprintf(p + len, "EC 0x%02x:", i);
1578 for (j = 0; j < 16; j++) {
1579 if (!acpi_ec_read(i + j, &v))
1580 break;
1581 if (v != ecdump_regs[i + j])
1582 len += sprintf(p + len, " *%02x", v);
1583 else
1584 len += sprintf(p + len, " %02x", v);
1585 ecdump_regs[i + j] = v;
1586 }
1587 len += sprintf(p + len, "\n");
1588 if (j != 16)
1589 break;
1590 }
1591
1592 /* These are way too dangerous to advertise openly... */
1593#if 0
1594 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
1595 " (<offset> is 00-ff, <value> is 00-ff)\n");
1596 len += sprintf(p + len, "commands:\t0x<offset> <value> "
1597 " (<offset> is 00-ff, <value> is 0-255)\n");
1598#endif
1599 return len;
1600}
1601
1602static int ecdump_write(char *buf)
1603{
1604 char *cmd;
1605 int i, v;
1606
1607 while ((cmd = next_cmd(&buf))) {
1608 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
1609 /* i and v set */
1610 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
1611 /* i and v set */
1612 } else
1613 return -EINVAL;
1614 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
1615 if (!acpi_ec_write(i, v))
1616 return -EIO;
1617 } else
1618 return -EINVAL;
1619 }
1620
1621 return 0;
1622}
1623
1624static int brightness_offset = 0x31;
1625
Holger Macht8acb0252006-10-20 14:30:28 -07001626static int brightness_get(struct backlight_device *bd)
1627{
Henrique de Moraes Holschuh8d297262006-11-24 11:47:07 -02001628 u8 level;
1629 if (!acpi_ec_read(brightness_offset, &level))
1630 return -EIO;
Holger Macht8acb0252006-10-20 14:30:28 -07001631
Henrique de Moraes Holschuh8d297262006-11-24 11:47:07 -02001632 level &= 0x7;
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02001633
Henrique de Moraes Holschuh8d297262006-11-24 11:47:07 -02001634 return level;
Holger Macht8acb0252006-10-20 14:30:28 -07001635}
1636
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001637static int brightness_read(char *p)
1638{
1639 int len = 0;
Holger Macht8acb0252006-10-20 14:30:28 -07001640 int level;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001641
Holger Macht8acb0252006-10-20 14:30:28 -07001642 if ((level = brightness_get(NULL)) < 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001643 len += sprintf(p + len, "level:\t\tunreadable\n");
1644 } else {
1645 len += sprintf(p + len, "level:\t\t%d\n", level & 0x7);
1646 len += sprintf(p + len, "commands:\tup, down\n");
1647 len += sprintf(p + len, "commands:\tlevel <level>"
1648 " (<level> is 0-7)\n");
1649 }
1650
1651 return len;
1652}
1653
1654#define BRIGHTNESS_UP 4
1655#define BRIGHTNESS_DOWN 5
1656
Holger Macht8acb0252006-10-20 14:30:28 -07001657static int brightness_set(int value)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001658{
1659 int cmos_cmd, inc, i;
Holger Macht8acb0252006-10-20 14:30:28 -07001660 int current_value = brightness_get(NULL);
1661
1662 value &= 7;
1663
1664 cmos_cmd = value > current_value ? BRIGHTNESS_UP : BRIGHTNESS_DOWN;
1665 inc = value > current_value ? 1 : -1;
1666 for (i = current_value; i != value; i += inc) {
1667 if (!cmos_eval(cmos_cmd))
1668 return -EIO;
1669 if (!acpi_ec_write(brightness_offset, i + inc))
1670 return -EIO;
1671 }
1672
1673 return 0;
1674}
1675
1676static int brightness_write(char *buf)
1677{
1678 int level;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001679 int new_level;
1680 char *cmd;
1681
1682 while ((cmd = next_cmd(&buf))) {
Holger Macht8acb0252006-10-20 14:30:28 -07001683 if ((level = brightness_get(NULL)) < 0)
1684 return level;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001685 level &= 7;
1686
1687 if (strlencmp(cmd, "up") == 0) {
1688 new_level = level == 7 ? 7 : level + 1;
1689 } else if (strlencmp(cmd, "down") == 0) {
1690 new_level = level == 0 ? 0 : level - 1;
1691 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
1692 new_level >= 0 && new_level <= 7) {
1693 /* new_level set */
1694 } else
1695 return -EINVAL;
1696
Holger Macht8acb0252006-10-20 14:30:28 -07001697 brightness_set(new_level);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001698 }
1699
1700 return 0;
1701}
1702
Holger Macht8acb0252006-10-20 14:30:28 -07001703static int brightness_update_status(struct backlight_device *bd)
1704{
1705 return brightness_set(bd->props->brightness);
1706}
1707
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02001708static struct backlight_properties ibm_backlight_data = {
1709 .owner = THIS_MODULE,
1710 .get_brightness = brightness_get,
1711 .update_status = brightness_update_status,
1712 .max_brightness = 7,
1713};
1714
1715static int brightness_init(void)
1716{
1717 ibm_backlight_device = backlight_device_register("ibm", NULL,
1718 &ibm_backlight_data);
1719 if (IS_ERR(ibm_backlight_device)) {
1720 printk(IBM_ERR "Could not register backlight device\n");
1721 return PTR_ERR(ibm_backlight_device);
1722 }
1723
1724 return 0;
1725}
1726
1727static void brightness_exit(void)
1728{
1729 if (ibm_backlight_device) {
1730 backlight_device_unregister(ibm_backlight_device);
1731 ibm_backlight_device = NULL;
1732 }
1733}
1734
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001735static int volume_offset = 0x30;
1736
1737static int volume_read(char *p)
1738{
1739 int len = 0;
1740 u8 level;
1741
1742 if (!acpi_ec_read(volume_offset, &level)) {
1743 len += sprintf(p + len, "level:\t\tunreadable\n");
1744 } else {
1745 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
1746 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
1747 len += sprintf(p + len, "commands:\tup, down, mute\n");
1748 len += sprintf(p + len, "commands:\tlevel <level>"
1749 " (<level> is 0-15)\n");
1750 }
1751
1752 return len;
1753}
1754
1755#define VOLUME_DOWN 0
1756#define VOLUME_UP 1
1757#define VOLUME_MUTE 2
1758
1759static int volume_write(char *buf)
1760{
1761 int cmos_cmd, inc, i;
1762 u8 level, mute;
1763 int new_level, new_mute;
1764 char *cmd;
1765
1766 while ((cmd = next_cmd(&buf))) {
1767 if (!acpi_ec_read(volume_offset, &level))
1768 return -EIO;
1769 new_mute = mute = level & 0x40;
1770 new_level = level = level & 0xf;
1771
1772 if (strlencmp(cmd, "up") == 0) {
1773 if (mute)
1774 new_mute = 0;
1775 else
1776 new_level = level == 15 ? 15 : level + 1;
1777 } else if (strlencmp(cmd, "down") == 0) {
1778 if (mute)
1779 new_mute = 0;
1780 else
1781 new_level = level == 0 ? 0 : level - 1;
1782 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
1783 new_level >= 0 && new_level <= 15) {
1784 /* new_level set */
1785 } else if (strlencmp(cmd, "mute") == 0) {
1786 new_mute = 0x40;
1787 } else
1788 return -EINVAL;
1789
1790 if (new_level != level) { /* mute doesn't change */
1791 cmos_cmd = new_level > level ? VOLUME_UP : VOLUME_DOWN;
1792 inc = new_level > level ? 1 : -1;
1793
1794 if (mute && (!cmos_eval(cmos_cmd) ||
1795 !acpi_ec_write(volume_offset, level)))
1796 return -EIO;
1797
1798 for (i = level; i != new_level; i += inc)
1799 if (!cmos_eval(cmos_cmd) ||
1800 !acpi_ec_write(volume_offset, i + inc))
1801 return -EIO;
1802
1803 if (mute && (!cmos_eval(VOLUME_MUTE) ||
1804 !acpi_ec_write(volume_offset,
1805 new_level + mute)))
1806 return -EIO;
1807 }
1808
1809 if (new_mute != mute) { /* level doesn't change */
1810 cmos_cmd = new_mute ? VOLUME_MUTE : VOLUME_UP;
1811
1812 if (!cmos_eval(cmos_cmd) ||
1813 !acpi_ec_write(volume_offset, level + new_mute))
1814 return -EIO;
1815 }
1816 }
1817
1818 return 0;
1819}
1820
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001821static enum fan_status_access_mode fan_status_access_mode;
1822static enum fan_control_access_mode fan_control_access_mode;
1823static enum fan_control_commands fan_control_commands;
1824
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02001825static int fan_control_status_known;
1826static u8 fan_control_initial_status;
1827
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02001828static void fan_watchdog_fire(void *ignored);
1829static int fan_watchdog_maxinterval;
1830static DECLARE_WORK(fan_watchdog_task, fan_watchdog_fire, NULL);
1831
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001832static int fan_init(void)
1833{
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001834 fan_status_access_mode = IBMACPI_FAN_NONE;
1835 fan_control_access_mode = IBMACPI_FAN_WR_NONE;
1836 fan_control_commands = 0;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02001837 fan_control_status_known = 1;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02001838 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001839
1840 if (gfan_handle) {
1841 /* 570, 600e/x, 770e, 770x */
1842 fan_status_access_mode = IBMACPI_FAN_RD_ACPI_GFAN;
1843 } else {
1844 /* all other ThinkPads: note that even old-style
1845 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02001846 if (likely(acpi_ec_read(fan_status_offset,
1847 &fan_control_initial_status))) {
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001848 fan_status_access_mode = IBMACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02001849
1850 /* In some ThinkPads, neither the EC nor the ACPI
1851 * DSDT initialize the fan status, and it ends up
1852 * being set to 0x07 when it *could* be either
1853 * 0x07 or 0x80.
1854 *
1855 * Enable for TP-1Y (T43), TP-78 (R51e),
1856 * TP-76 (R52), TP-70 (T43, R52), which are known
1857 * to be buggy. */
1858 if (fan_control_initial_status == 0x07 &&
1859 ibm_thinkpad_ec_found &&
1860 ((ibm_thinkpad_ec_found[0] == '1' &&
1861 ibm_thinkpad_ec_found[1] == 'Y') ||
1862 (ibm_thinkpad_ec_found[0] == '7' &&
1863 (ibm_thinkpad_ec_found[1] == '6' ||
1864 ibm_thinkpad_ec_found[1] == '8' ||
1865 ibm_thinkpad_ec_found[1] == '0'))
1866 )) {
1867 printk(IBM_NOTICE
1868 "fan_init: initial fan status is "
1869 "unknown, assuming it is in auto "
1870 "mode\n");
1871 fan_control_status_known = 0;
1872 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001873 } else {
1874 printk(IBM_ERR
1875 "ThinkPad ACPI EC access misbehaving, "
1876 "fan status and control unavailable\n");
1877 return 0;
1878 }
1879 }
1880
1881 if (sfan_handle) {
1882 /* 570, 770x-JL */
1883 fan_control_access_mode = IBMACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02001884 fan_control_commands |=
1885 IBMACPI_FAN_CMD_LEVEL | IBMACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001886 } else {
1887 if (!gfan_handle) {
1888 /* gfan without sfan means no fan control */
1889 /* all other models implement TP EC 0x2f control */
1890
1891 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02001892 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001893 fan_control_access_mode =
1894 IBMACPI_FAN_WR_ACPI_FANS;
1895 fan_control_commands |=
1896 IBMACPI_FAN_CMD_SPEED |
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02001897 IBMACPI_FAN_CMD_LEVEL |
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001898 IBMACPI_FAN_CMD_ENABLE;
1899 } else {
1900 fan_control_access_mode = IBMACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02001901 fan_control_commands |=
1902 IBMACPI_FAN_CMD_LEVEL |
1903 IBMACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001904 }
1905 }
1906 }
1907
1908 return 0;
1909}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001910
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02001911static int fan_get_status(u8 *status)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001912{
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02001913 u8 s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001914
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02001915 /* TODO:
1916 * Add IBMACPI_FAN_RD_ACPI_FANS ? */
1917
Henrique de Moraes Holschuh3ef8a602006-11-24 11:47:10 -02001918 switch (fan_status_access_mode) {
1919 case IBMACPI_FAN_RD_ACPI_GFAN:
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001920 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02001921
1922 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001923 return -EIO;
1924
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02001925 if (likely(status))
1926 *status = s & 0x07;
1927
1928 break;
1929
1930 case IBMACPI_FAN_RD_TPEC:
1931 /* all except 570, 600e/x, 770e, 770x */
1932 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
1933 return -EIO;
1934
1935 if (likely(status))
1936 *status = s;
1937
1938 break;
1939
1940 default:
1941 return -ENXIO;
1942 }
1943
1944 return 0;
1945}
1946
1947static int fan_get_speed(unsigned int *speed)
1948{
1949 u8 hi, lo;
1950
1951 switch (fan_status_access_mode) {
1952 case IBMACPI_FAN_RD_TPEC:
1953 /* all except 570, 600e/x, 770e, 770x */
1954 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
1955 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
1956 return -EIO;
1957
1958 if (likely(speed))
1959 *speed = (hi << 8) | lo;
1960
1961 break;
1962
1963 default:
1964 return -ENXIO;
1965 }
1966
1967 return 0;
1968}
1969
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02001970static void fan_exit(void)
1971{
1972 cancel_delayed_work(&fan_watchdog_task);
1973 flush_scheduled_work();
1974}
1975
1976static void fan_watchdog_reset(void)
1977{
1978 static int fan_watchdog_active = 0;
1979
1980 if (fan_watchdog_active)
1981 cancel_delayed_work(&fan_watchdog_task);
1982
1983 if (fan_watchdog_maxinterval > 0) {
1984 fan_watchdog_active = 1;
1985 if (!schedule_delayed_work(&fan_watchdog_task,
1986 msecs_to_jiffies(fan_watchdog_maxinterval
1987 * 1000))) {
1988 printk(IBM_ERR "failed to schedule the fan watchdog, "
1989 "watchdog will not trigger\n");
1990 }
1991 } else
1992 fan_watchdog_active = 0;
1993}
1994
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02001995static int fan_read(char *p)
1996{
1997 int len = 0;
1998 int rc;
1999 u8 status;
2000 unsigned int speed = 0;
2001
2002 switch (fan_status_access_mode) {
2003 case IBMACPI_FAN_RD_ACPI_GFAN:
2004 /* 570, 600e/x, 770e, 770x */
2005 if ((rc = fan_get_status(&status)) < 0)
2006 return rc;
2007
Henrique de Moraes Holschuhbab812a2006-11-24 11:47:12 -02002008 len += sprintf(p + len, "status:\t\t%s\n"
2009 "level:\t\t%d\n",
2010 (status != 0) ? "enabled" : "disabled", status);
Henrique de Moraes Holschuh3ef8a602006-11-24 11:47:10 -02002011 break;
2012
2013 case IBMACPI_FAN_RD_TPEC:
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002014 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02002015 if ((rc = fan_get_status(&status)) < 0)
2016 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002017
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002018 if (unlikely(!fan_control_status_known)) {
2019 if (status != fan_control_initial_status)
2020 fan_control_status_known = 1;
2021 else
2022 /* Return most likely status. In fact, it
2023 * might be the only possible status */
2024 status = IBMACPI_FAN_EC_AUTO;
2025 }
2026
Henrique de Moraes Holschuhbab812a2006-11-24 11:47:12 -02002027 len += sprintf(p + len, "status:\t\t%s\n",
2028 (status != 0) ? "enabled" : "disabled");
Henrique de Moraes Holschuh3ef8a602006-11-24 11:47:10 -02002029
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002030 /* No ThinkPad boots on disengaged mode, we can safely
2031 * assume the tachometer is online if fan control status
2032 * was unknown */
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02002033 if ((rc = fan_get_speed(&speed)) < 0)
2034 return rc;
2035
2036 len += sprintf(p + len, "speed:\t\t%d\n", speed);
Henrique de Moraes Holschuhbab812a2006-11-24 11:47:12 -02002037
2038 if (status & IBMACPI_FAN_EC_DISENGAGED)
2039 /* Disengaged mode takes precedence */
2040 len += sprintf(p + len, "level:\t\tdisengaged\n");
2041 else if (status & IBMACPI_FAN_EC_AUTO)
2042 len += sprintf(p + len, "level:\t\tauto\n");
2043 else
2044 len += sprintf(p + len, "level:\t\t%d\n", status);
Henrique de Moraes Holschuh3ef8a602006-11-24 11:47:10 -02002045 break;
2046
2047 case IBMACPI_FAN_NONE:
2048 default:
2049 len += sprintf(p + len, "status:\t\tnot supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002050 }
2051
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02002052 if (fan_control_commands & IBMACPI_FAN_CMD_LEVEL) {
2053 len += sprintf(p + len, "commands:\tlevel <level>");
2054
2055 switch (fan_control_access_mode) {
2056 case IBMACPI_FAN_WR_ACPI_SFAN:
2057 len += sprintf(p + len, " (<level> is 0-7)\n");
2058 break;
2059
2060 default:
2061 len += sprintf(p + len, " (<level> is 0-7, "
2062 "auto, disengaged)\n");
2063 break;
2064 }
2065 }
Henrique de Moraes Holschuh3ef8a602006-11-24 11:47:10 -02002066
2067 if (fan_control_commands & IBMACPI_FAN_CMD_ENABLE)
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002068 len += sprintf(p + len, "commands:\tenable, disable\n"
2069 "commands:\twatchdog <timeout> (<timeout> is 0 (off), "
2070 "1-120 (seconds))\n");
Henrique de Moraes Holschuh3ef8a602006-11-24 11:47:10 -02002071
2072 if (fan_control_commands & IBMACPI_FAN_CMD_SPEED)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002073 len += sprintf(p + len, "commands:\tspeed <speed>"
2074 " (<speed> is 0-65535)\n");
2075
2076 return len;
2077}
2078
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002079static int fan_set_level(int level)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002080{
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002081 switch (fan_control_access_mode) {
2082 case IBMACPI_FAN_WR_ACPI_SFAN:
2083 if (level >= 0 && level <= 7) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002084 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
2085 return -EIO;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002086 } else
2087 return -EINVAL;
2088 break;
2089
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02002090 case IBMACPI_FAN_WR_ACPI_FANS:
2091 case IBMACPI_FAN_WR_TPEC:
2092 if ((level != IBMACPI_FAN_EC_AUTO) &&
2093 (level != IBMACPI_FAN_EC_DISENGAGED) &&
2094 ((level < 0) || (level > 7)))
2095 return -EINVAL;
2096
2097 if (!acpi_ec_write(fan_status_offset, level))
2098 return -EIO;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002099 else
2100 fan_control_status_known = 1;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02002101 break;
2102
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002103 default:
2104 return -ENXIO;
2105 }
2106 return 0;
2107}
2108
2109static int fan_set_enable(void)
2110{
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002111 u8 s;
2112 int rc;
2113
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002114 switch (fan_control_access_mode) {
2115 case IBMACPI_FAN_WR_ACPI_FANS:
2116 case IBMACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002117 if ((rc = fan_get_status(&s)) < 0)
2118 return rc;
2119
2120 /* Don't go out of emergency fan mode */
2121 if (s != 7)
2122 s = IBMACPI_FAN_EC_AUTO;
2123
2124 if (!acpi_ec_write(fan_status_offset, s))
2125 return -EIO;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002126 else
2127 fan_control_status_known = 1;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002128 break;
2129
2130 case IBMACPI_FAN_WR_ACPI_SFAN:
2131 if ((rc = fan_get_status(&s)) < 0)
2132 return rc;
2133
2134 s &= 0x07;
2135
2136 /* Set fan to at least level 4 */
2137 if (s < 4)
2138 s = 4;
2139
2140 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002141 return -EIO;
2142 break;
2143
2144 default:
2145 return -ENXIO;
2146 }
2147 return 0;
2148}
2149
2150static int fan_set_disable(void)
2151{
2152 switch (fan_control_access_mode) {
2153 case IBMACPI_FAN_WR_ACPI_FANS:
2154 case IBMACPI_FAN_WR_TPEC:
2155 if (!acpi_ec_write(fan_status_offset, 0x00))
2156 return -EIO;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002157 else
2158 fan_control_status_known = 1;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002159 break;
2160
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002161 case IBMACPI_FAN_WR_ACPI_SFAN:
2162 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
2163 return -EIO;
2164 break;
2165
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002166 default:
2167 return -ENXIO;
2168 }
2169 return 0;
2170}
2171
2172static int fan_set_speed(int speed)
2173{
2174 switch (fan_control_access_mode) {
2175 case IBMACPI_FAN_WR_ACPI_FANS:
2176 if (speed >= 0 && speed <= 65535) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002177 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
2178 speed, speed, speed))
2179 return -EIO;
2180 } else
2181 return -EINVAL;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002182 break;
2183
2184 default:
2185 return -ENXIO;
2186 }
2187 return 0;
2188}
2189
2190static int fan_write_cmd_level(const char *cmd, int *rc)
2191{
2192 int level;
2193
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02002194 if (strlencmp(cmd, "level auto") == 0)
2195 level = IBMACPI_FAN_EC_AUTO;
2196 else if (strlencmp(cmd, "level disengaged") == 0)
2197 level = IBMACPI_FAN_EC_DISENGAGED;
2198 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002199 return 0;
2200
2201 if ((*rc = fan_set_level(level)) == -ENXIO)
2202 printk(IBM_ERR "level command accepted for unsupported "
2203 "access mode %d", fan_control_access_mode);
2204
2205 return 1;
2206}
2207
2208static int fan_write_cmd_enable(const char *cmd, int *rc)
2209{
2210 if (strlencmp(cmd, "enable") != 0)
2211 return 0;
2212
2213 if ((*rc = fan_set_enable()) == -ENXIO)
2214 printk(IBM_ERR "enable command accepted for unsupported "
2215 "access mode %d", fan_control_access_mode);
2216
2217 return 1;
2218}
2219
2220static int fan_write_cmd_disable(const char *cmd, int *rc)
2221{
2222 if (strlencmp(cmd, "disable") != 0)
2223 return 0;
2224
2225 if ((*rc = fan_set_disable()) == -ENXIO)
2226 printk(IBM_ERR "disable command accepted for unsupported "
2227 "access mode %d", fan_control_access_mode);
2228
2229 return 1;
2230}
2231
2232static int fan_write_cmd_speed(const char *cmd, int *rc)
2233{
2234 int speed;
2235
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02002236 /* TODO:
2237 * Support speed <low> <medium> <high> ? */
2238
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002239 if (sscanf(cmd, "speed %d", &speed) != 1)
2240 return 0;
2241
2242 if ((*rc = fan_set_speed(speed)) == -ENXIO)
2243 printk(IBM_ERR "speed command accepted for unsupported "
2244 "access mode %d", fan_control_access_mode);
2245
2246 return 1;
2247}
2248
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002249static int fan_write_cmd_watchdog(const char *cmd, int *rc)
2250{
2251 int interval;
2252
2253 if (sscanf(cmd, "watchdog %d", &interval) != 1)
2254 return 0;
2255
2256 if (interval < 0 || interval > 120)
2257 *rc = -EINVAL;
2258 else
2259 fan_watchdog_maxinterval = interval;
2260
2261 return 1;
2262}
2263
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002264static int fan_write(char *buf)
2265{
2266 char *cmd;
2267 int rc = 0;
2268
2269 while (!rc && (cmd = next_cmd(&buf))) {
2270 if (!((fan_control_commands & IBMACPI_FAN_CMD_LEVEL) &&
2271 fan_write_cmd_level(cmd, &rc)) &&
2272 !((fan_control_commands & IBMACPI_FAN_CMD_ENABLE) &&
2273 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002274 fan_write_cmd_disable(cmd, &rc) ||
2275 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002276 !((fan_control_commands & IBMACPI_FAN_CMD_SPEED) &&
2277 fan_write_cmd_speed(cmd, &rc))
2278 )
2279 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002280 else if (!rc)
2281 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002282 }
2283
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002284 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002285}
2286
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002287static void fan_watchdog_fire(void *ignored)
2288{
2289 printk(IBM_NOTICE "fan watchdog: enabling fan\n");
2290 if (fan_set_enable()) {
2291 printk(IBM_ERR "fan watchdog: error while enabling fan\n");
2292 /* reschedule for later */
2293 fan_watchdog_reset();
2294 }
2295}
2296
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297static struct ibm_struct ibms[] = {
2298 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002299 .name = "driver",
2300 .init = driver_init,
2301 .read = driver_read,
2302 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002304 .name = "hotkey",
2305 .hid = IBM_HKEY_HID,
2306 .init = hotkey_init,
2307 .read = hotkey_read,
2308 .write = hotkey_write,
2309 .exit = hotkey_exit,
2310 .notify = hotkey_notify,
2311 .handle = &hkey_handle,
2312 .type = ACPI_DEVICE_NOTIFY,
2313 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002315 .name = "bluetooth",
2316 .init = bluetooth_init,
2317 .read = bluetooth_read,
2318 .write = bluetooth_write,
2319 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 {
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002321 .name = "wan",
2322 .init = wan_init,
2323 .read = wan_read,
2324 .write = wan_write,
2325 .experimental = 1,
2326 },
2327 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002328 .name = "video",
2329 .init = video_init,
2330 .read = video_read,
2331 .write = video_write,
2332 .exit = video_exit,
2333 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002335 .name = "light",
2336 .init = light_init,
2337 .read = light_read,
2338 .write = light_write,
2339 },
Kristen Accardi63e5f242006-02-23 17:56:06 -08002340#ifdef CONFIG_ACPI_IBM_DOCK
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002342 .name = "dock",
2343 .read = dock_read,
2344 .write = dock_write,
2345 .notify = dock_notify,
2346 .handle = &dock_handle,
2347 .type = ACPI_SYSTEM_NOTIFY,
2348 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002350 .name = "dock",
2351 .hid = IBM_PCI_HID,
2352 .notify = dock_notify,
2353 .handle = &pci_handle,
2354 .type = ACPI_SYSTEM_NOTIFY,
2355 },
Kristen Accardi63e5f242006-02-23 17:56:06 -08002356#endif
Henrique de Moraes Holschuh2df910b2006-11-25 16:35:09 -02002357#ifdef CONFIG_ACPI_IBM_BAY
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002359 .name = "bay",
2360 .init = bay_init,
2361 .read = bay_read,
2362 .write = bay_write,
2363 .notify = bay_notify,
2364 .handle = &bay_handle,
2365 .type = ACPI_SYSTEM_NOTIFY,
2366 },
Henrique de Moraes Holschuh2df910b2006-11-25 16:35:09 -02002367#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002369 .name = "cmos",
2370 .read = cmos_read,
2371 .write = cmos_write,
2372 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002374 .name = "led",
2375 .init = led_init,
2376 .read = led_read,
2377 .write = led_write,
2378 },
2379 {
2380 .name = "beep",
2381 .read = beep_read,
2382 .write = beep_write,
2383 },
2384 {
2385 .name = "thermal",
2386 .init = thermal_init,
2387 .read = thermal_read,
2388 },
2389 {
2390 .name = "ecdump",
2391 .read = ecdump_read,
2392 .write = ecdump_write,
2393 .experimental = 1,
2394 },
2395 {
2396 .name = "brightness",
2397 .read = brightness_read,
2398 .write = brightness_write,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02002399 .init = brightness_init,
2400 .exit = brightness_exit,
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002401 },
2402 {
2403 .name = "volume",
2404 .read = volume_read,
2405 .write = volume_write,
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002406 },
2407 {
2408 .name = "fan",
2409 .read = fan_read,
2410 .write = fan_write,
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002411 .init = fan_init,
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002412 .exit = fan_exit,
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002413 .experimental = 1,
2414 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416
2417static int dispatch_read(char *page, char **start, off_t off, int count,
2418 int *eof, void *data)
2419{
2420 struct ibm_struct *ibm = (struct ibm_struct *)data;
2421 int len;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002422
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 if (!ibm || !ibm->read)
2424 return -EINVAL;
2425
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002426 len = ibm->read(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 if (len < 0)
2428 return len;
2429
2430 if (len <= off + count)
2431 *eof = 1;
2432 *start = page + off;
2433 len -= off;
2434 if (len > count)
2435 len = count;
2436 if (len < 0)
2437 len = 0;
2438
2439 return len;
2440}
2441
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002442static int dispatch_write(struct file *file, const char __user * userbuf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 unsigned long count, void *data)
2444{
2445 struct ibm_struct *ibm = (struct ibm_struct *)data;
2446 char *kernbuf;
2447 int ret;
2448
2449 if (!ibm || !ibm->write)
2450 return -EINVAL;
2451
2452 kernbuf = kmalloc(count + 2, GFP_KERNEL);
2453 if (!kernbuf)
2454 return -ENOMEM;
2455
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002456 if (copy_from_user(kernbuf, userbuf, count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 kfree(kernbuf);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002458 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 }
2460
2461 kernbuf[count] = 0;
2462 strcat(kernbuf, ",");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002463 ret = ibm->write(kernbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 if (ret == 0)
2465 ret = count;
2466
2467 kfree(kernbuf);
2468
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002469 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470}
2471
2472static void dispatch_notify(acpi_handle handle, u32 event, void *data)
2473{
2474 struct ibm_struct *ibm = (struct ibm_struct *)data;
2475
2476 if (!ibm || !ibm->notify)
2477 return;
2478
2479 ibm->notify(ibm, event);
2480}
2481
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002482static int __init setup_notify(struct ibm_struct *ibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483{
2484 acpi_status status;
2485 int ret;
2486
2487 if (!*ibm->handle)
2488 return 0;
2489
2490 ret = acpi_bus_get_device(*ibm->handle, &ibm->device);
2491 if (ret < 0) {
2492 printk(IBM_ERR "%s device not present\n", ibm->name);
2493 return 0;
2494 }
2495
2496 acpi_driver_data(ibm->device) = ibm;
2497 sprintf(acpi_device_class(ibm->device), "%s/%s", IBM_NAME, ibm->name);
2498
2499 status = acpi_install_notify_handler(*ibm->handle, ibm->type,
2500 dispatch_notify, ibm);
2501 if (ACPI_FAILURE(status)) {
2502 printk(IBM_ERR "acpi_install_notify_handler(%s) failed: %d\n",
2503 ibm->name, status);
2504 return -ENODEV;
2505 }
2506
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 return 0;
2508}
2509
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002510static int __init ibm_device_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511{
2512 return 0;
2513}
2514
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002515static int __init register_driver(struct ibm_struct *ibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516{
2517 int ret;
2518
2519 ibm->driver = kmalloc(sizeof(struct acpi_driver), GFP_KERNEL);
2520 if (!ibm->driver) {
2521 printk(IBM_ERR "kmalloc(ibm->driver) failed\n");
2522 return -1;
2523 }
2524
2525 memset(ibm->driver, 0, sizeof(struct acpi_driver));
Henrique de Moraes Holschuh3dfd35c2006-11-24 10:32:32 -02002526 sprintf(ibm->driver->name, "%s_%s", IBM_NAME, ibm->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 ibm->driver->ids = ibm->hid;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002528 ibm->driver->ops.add = &ibm_device_add;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529
2530 ret = acpi_bus_register_driver(ibm->driver);
2531 if (ret < 0) {
2532 printk(IBM_ERR "acpi_bus_register_driver(%s) failed: %d\n",
2533 ibm->hid, ret);
2534 kfree(ibm->driver);
2535 }
2536
2537 return ret;
2538}
2539
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002540static int __init ibm_init(struct ibm_struct *ibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541{
2542 int ret;
2543 struct proc_dir_entry *entry;
2544
2545 if (ibm->experimental && !experimental)
2546 return 0;
2547
2548 if (ibm->hid) {
2549 ret = register_driver(ibm);
2550 if (ret < 0)
2551 return ret;
2552 ibm->driver_registered = 1;
2553 }
2554
2555 if (ibm->init) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002556 ret = ibm->init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 if (ret != 0)
2558 return ret;
2559 ibm->init_called = 1;
2560 }
2561
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002562 if (ibm->read) {
2563 entry = create_proc_entry(ibm->name,
2564 S_IFREG | S_IRUGO | S_IWUSR,
2565 proc_dir);
2566 if (!entry) {
2567 printk(IBM_ERR "unable to create proc entry %s\n",
2568 ibm->name);
2569 return -ENODEV;
2570 }
2571 entry->owner = THIS_MODULE;
2572 entry->data = ibm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 entry->read_proc = &dispatch_read;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002574 if (ibm->write)
2575 entry->write_proc = &dispatch_write;
2576 ibm->proc_created = 1;
2577 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578
2579 if (ibm->notify) {
2580 ret = setup_notify(ibm);
2581 if (ret < 0)
2582 return ret;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002583 ibm->notify_installed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 }
2585
2586 return 0;
2587}
2588
2589static void ibm_exit(struct ibm_struct *ibm)
2590{
2591 if (ibm->notify_installed)
2592 acpi_remove_notify_handler(*ibm->handle, ibm->type,
2593 dispatch_notify);
2594
2595 if (ibm->proc_created)
2596 remove_proc_entry(ibm->name, proc_dir);
2597
2598 if (ibm->init_called && ibm->exit)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002599 ibm->exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600
2601 if (ibm->driver_registered) {
2602 acpi_bus_unregister_driver(ibm->driver);
2603 kfree(ibm->driver);
2604 }
2605}
2606
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002607static void __init ibm_handle_init(char *name,
2608 acpi_handle * handle, acpi_handle parent,
2609 char **paths, int num_paths, char **path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610{
2611 int i;
2612 acpi_status status;
2613
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002614 for (i = 0; i < num_paths; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 status = acpi_get_handle(parent, paths[i], handle);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002616 if (ACPI_SUCCESS(status)) {
2617 *path = paths[i];
2618 return;
2619 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002621
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 *handle = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623}
2624
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002625#define IBM_HANDLE_INIT(object) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 ibm_handle_init(#object, &object##_handle, *object##_parent, \
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002627 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628
2629static int set_ibm_param(const char *val, struct kernel_param *kp)
2630{
2631 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002633 for (i = 0; i < ARRAY_SIZE(ibms); i++)
2634 if (strcmp(ibms[i].name, kp->name) == 0 && ibms[i].write) {
2635 if (strlen(val) > sizeof(ibms[i].param) - 2)
2636 return -ENOSPC;
2637 strcpy(ibms[i].param, val);
2638 strcat(ibms[i].param, ",");
2639 return 0;
2640 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 return -EINVAL;
2643}
2644
2645#define IBM_PARAM(feature) \
2646 module_param_call(feature, set_ibm_param, NULL, NULL, 0)
2647
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002648IBM_PARAM(hotkey);
2649IBM_PARAM(bluetooth);
2650IBM_PARAM(video);
2651IBM_PARAM(light);
Kristen Accardi63e5f242006-02-23 17:56:06 -08002652#ifdef CONFIG_ACPI_IBM_DOCK
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002653IBM_PARAM(dock);
Kristen Accardi63e5f242006-02-23 17:56:06 -08002654#endif
Henrique de Moraes Holschuh2df910b2006-11-25 16:35:09 -02002655#ifdef CONFIG_ACPI_IBM_BAY
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002656IBM_PARAM(bay);
Henrique de Moraes Holschuh2df910b2006-11-25 16:35:09 -02002657#endif
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002658IBM_PARAM(cmos);
2659IBM_PARAM(led);
2660IBM_PARAM(beep);
2661IBM_PARAM(ecdump);
2662IBM_PARAM(brightness);
2663IBM_PARAM(volume);
2664IBM_PARAM(fan);
2665
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666static void acpi_ibm_exit(void)
2667{
2668 int i;
2669
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002670 for (i = ARRAY_SIZE(ibms) - 1; i >= 0; i--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 ibm_exit(&ibms[i]);
2672
2673 remove_proc_entry(IBM_DIR, acpi_root_dir);
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02002674
2675 if (ibm_thinkpad_ec_found)
2676 kfree(ibm_thinkpad_ec_found);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677}
2678
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02002679static char* __init check_dmi_for_ec(void)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002680{
2681 struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02002682 char ec_fw_string[18];
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002683
2684 /*
2685 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
2686 * X32 or newer, all Z series; Some models must have an
2687 * up-to-date BIOS or they will not be detected.
2688 *
2689 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
2690 */
2691 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02002692 if (sscanf(dev->name,
2693 "IBM ThinkPad Embedded Controller -[%17c",
2694 ec_fw_string) == 1) {
2695 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
2696 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
2697 return kstrdup(ec_fw_string, GFP_KERNEL);
2698 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002699 }
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02002700 return NULL;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002701}
2702
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703static int __init acpi_ibm_init(void)
2704{
2705 int ret, i;
2706
2707 if (acpi_disabled)
2708 return -ENODEV;
2709
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002710 if (!acpi_specific_hotkey_enabled) {
2711 printk(IBM_ERR "using generic hotkey driver\n");
2712 return -ENODEV;
Luming Yufb9802f2005-03-18 18:03:45 -05002713 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002715 /* ec is required because many other handles are relative to it */
2716 IBM_HANDLE_INIT(ec);
2717 if (!ec_handle) {
2718 printk(IBM_ERR "ec object not found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 return -ENODEV;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002720 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002722 /* Models with newer firmware report the EC in DMI */
2723 ibm_thinkpad_ec_found = check_dmi_for_ec();
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02002724 if (ibm_thinkpad_ec_found)
2725 printk(IBM_INFO "ThinkPad EC firmware %s\n",
2726 ibm_thinkpad_ec_found);
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002727
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 /* these handles are not required */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002729 IBM_HANDLE_INIT(vid);
2730 IBM_HANDLE_INIT(vid2);
2731 IBM_HANDLE_INIT(ledb);
2732 IBM_HANDLE_INIT(led);
2733 IBM_HANDLE_INIT(hkey);
2734 IBM_HANDLE_INIT(lght);
2735 IBM_HANDLE_INIT(cmos);
Kristen Accardi63e5f242006-02-23 17:56:06 -08002736#ifdef CONFIG_ACPI_IBM_DOCK
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002737 IBM_HANDLE_INIT(dock);
Kristen Accardi63e5f242006-02-23 17:56:06 -08002738#endif
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002739 IBM_HANDLE_INIT(pci);
Henrique de Moraes Holschuh2df910b2006-11-25 16:35:09 -02002740#ifdef CONFIG_ACPI_IBM_BAY
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002741 IBM_HANDLE_INIT(bay);
2742 if (bay_handle)
2743 IBM_HANDLE_INIT(bay_ej);
2744 IBM_HANDLE_INIT(bay2);
2745 if (bay2_handle)
2746 IBM_HANDLE_INIT(bay2_ej);
Henrique de Moraes Holschuh2df910b2006-11-25 16:35:09 -02002747#endif
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002748 IBM_HANDLE_INIT(beep);
2749 IBM_HANDLE_INIT(ecrd);
2750 IBM_HANDLE_INIT(ecwr);
2751 IBM_HANDLE_INIT(fans);
2752 IBM_HANDLE_INIT(gfan);
2753 IBM_HANDLE_INIT(sfan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754
2755 proc_dir = proc_mkdir(IBM_DIR, acpi_root_dir);
2756 if (!proc_dir) {
2757 printk(IBM_ERR "unable to create proc dir %s", IBM_DIR);
2758 return -ENODEV;
2759 }
2760 proc_dir->owner = THIS_MODULE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002761
2762 for (i = 0; i < ARRAY_SIZE(ibms); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 ret = ibm_init(&ibms[i]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002764 if (ret >= 0 && *ibms[i].param)
2765 ret = ibms[i].write(ibms[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 if (ret < 0) {
2767 acpi_ibm_exit();
2768 return ret;
2769 }
2770 }
2771
2772 return 0;
2773}
2774
2775module_init(acpi_ibm_init);
2776module_exit(acpi_ibm_exit);