blob: c6144ca6663861529a65d1f7f5c5d618be1f43f3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * ibm_acpi.c - IBM ThinkPad ACPI Extras
3 *
4 *
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
Henrique de Moraes Holschuhf9ff43a2006-11-25 16:37:38 -02006 * Copyright (C) 2006 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Borislav Deianov78f81cc2005-08-17 00:00:00 -040021 */
22
Henrique de Moraes Holschuhf9ff43a2006-11-25 16:37:38 -020023#define IBM_VERSION "0.13"
Borislav Deianov78f81cc2005-08-17 00:00:00 -040024
25/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 * Changelog:
Henrique de Moraes Holschuhf9ff43a2006-11-25 16:37:38 -020027 *
28 * 2006-11-22 0.13 new maintainer
29 * changelog now lives in git commit history, and will
30 * not be updated further in-file.
Borislav Deianov78f81cc2005-08-17 00:00:00 -040031 *
32 * 2005-08-17 0.12 fix compilation on 2.6.13-rc kernels
33 * 2005-03-17 0.11 support for 600e, 770x
34 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
35 * support for 770e, G41
36 * G40 and G41 don't have a thinklight
37 * temperatures no longer experimental
38 * experimental brightness control
39 * experimental volume control
40 * experimental fan enable/disable
41 * 2005-01-16 0.10 fix module loading on R30, R31
42 * 2005-01-16 0.9 support for 570, R30, R31
43 * ultrabay support on A22p, A3x
44 * limit arg for cmos, led, beep, drop experimental status
45 * more capable led control on A21e, A22p, T20-22, X20
46 * experimental temperatures and fan speed
47 * experimental embedded controller register dump
48 * mark more functions as __init, drop incorrect __exit
49 * use MODULE_VERSION
50 * thanks to Henrik Brix Andersen <brix@gentoo.org>
51 * fix parameter passing on module loading
52 * thanks to Rusty Russell <rusty@rustcorp.com.au>
53 * thanks to Jim Radford <radford@blackbean.org>
54 * 2004-11-08 0.8 fix init error case, don't return from a macro
55 * thanks to Chris Wright <chrisw@osdl.org>
56 * 2004-10-23 0.7 fix module loading on A21e, A22p, T20, T21, X20
57 * fix led control on A21e
58 * 2004-10-19 0.6 use acpi_bus_register_driver() to claim HKEY device
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 * 2004-10-18 0.5 thinklight support on A21e, G40, R32, T20, T21, X20
60 * proc file format changed
61 * video_switch command
62 * experimental cmos control
63 * experimental led control
64 * experimental acpi sounds
Borislav Deianov78f81cc2005-08-17 00:00:00 -040065 * 2004-09-16 0.4 support for module parameters
66 * hotkey mask can be prefixed by 0x
67 * video output switching
68 * video expansion control
69 * ultrabay eject support
70 * removed lcd brightness/on/off control, didn't work
71 * 2004-08-17 0.3 support for R40
72 * lcd off, brightness control
73 * thinklight on/off
74 * 2004-08-14 0.2 support for T series, X20
75 * bluetooth enable/disable
76 * hotkey events disabled by default
77 * removed fan control, currently useless
78 * 2004-08-09 0.1 initial release, support for X series
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 */
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#include <linux/kernel.h>
82#include <linux/module.h>
83#include <linux/init.h>
84#include <linux/types.h>
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -020085#include <linux/string.h>
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -020086
Linus Torvalds1da177e2005-04-16 15:20:36 -070087#include <linux/proc_fs.h>
Holger Macht8acb0252006-10-20 14:30:28 -070088#include <linux/backlight.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070089#include <asm/uaccess.h>
90
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -020091#include <linux/dmi.h>
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -020092#include <linux/jiffies.h>
93#include <linux/workqueue.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95#include <acpi/acpi_drivers.h>
96#include <acpi/acnamesp.h>
97
98#define IBM_NAME "ibm"
99#define IBM_DESC "IBM ThinkPad ACPI Extras"
100#define IBM_FILE "ibm_acpi"
101#define IBM_URL "http://ibm-acpi.sf.net/"
102
Henrique de Moraes Holschuhf9ff43a2006-11-25 16:37:38 -0200103MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400104MODULE_DESCRIPTION(IBM_DESC);
105MODULE_VERSION(IBM_VERSION);
106MODULE_LICENSE("GPL");
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108#define IBM_DIR IBM_NAME
109
110#define IBM_LOG IBM_FILE ": "
111#define IBM_ERR KERN_ERR IBM_LOG
112#define IBM_NOTICE KERN_NOTICE IBM_LOG
113#define IBM_INFO KERN_INFO IBM_LOG
114#define IBM_DEBUG KERN_DEBUG IBM_LOG
115
116#define IBM_MAX_ACPI_ARGS 3
117
118#define __unused __attribute__ ((unused))
119
120static int experimental;
121module_param(experimental, int, 0);
122
123static acpi_handle root_handle = NULL;
124
125#define IBM_HANDLE(object, parent, paths...) \
126 static acpi_handle object##_handle; \
127 static acpi_handle *object##_parent = &parent##_handle; \
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400128 static char *object##_path; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 static char *object##_paths[] = { paths }
130
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400131IBM_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
132 "\\_SB.PCI.ISA.EC", /* 570 */
133 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
134 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
135 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
136 "\\_SB.PCI0.ICH3.EC0", /* R31 */
137 "\\_SB.PCI0.LPC.EC", /* all others */
138 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400140IBM_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
141 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
142 "\\_SB.PCI0.VID0", /* 770e */
143 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
144 "\\_SB.PCI0.AGP.VID", /* all others */
145 ); /* R30, R31 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400147IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400149IBM_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, T4x, X31, X40 */
150 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
151 "\\CMS", /* R40, R40e */
152 ); /* all others */
Kristen Accardi63e5f242006-02-23 17:56:06 -0800153#ifdef CONFIG_ACPI_IBM_DOCK
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400154IBM_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
155 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
156 "\\_SB.PCI0.PCI1.DOCK", /* all others */
157 "\\_SB.PCI.ISA.SLCE", /* 570 */
158 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
Kristen Accardi63e5f242006-02-23 17:56:06 -0800159#endif
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400160IBM_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
161 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhe0298992006-11-25 16:35:09 -0200162 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400163 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
164 ); /* A21e, R30, R31 */
165
166IBM_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
167 "_EJ0", /* all others */
168 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
169
170IBM_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
171 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
172 ); /* all others */
173
174IBM_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
175 "_EJ0", /* 770x */
176 ); /* all others */
177
178/* don't list other alternatives as we install a notify handler on the 570 */
179IBM_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
180
181IBM_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
182 "^HKEY", /* R30, R31 */
183 "HKEY", /* all others */
184 ); /* 570 */
185
186IBM_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
187IBM_HANDLE(ledb, ec, "LEDB"); /* G4x */
188
189IBM_HANDLE(led, ec, "SLED", /* 570 */
190 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
191 "LED", /* all others */
192 ); /* R30, R31 */
193
194IBM_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
195IBM_HANDLE(ecrd, ec, "ECRD"); /* 570 */
196IBM_HANDLE(ecwr, ec, "ECWR"); /* 570 */
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200197IBM_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400198
199IBM_HANDLE(gfan, ec, "GFAN", /* 570 */
200 "\\FSPD", /* 600e/x, 770e, 770x */
201 ); /* all others */
202
203IBM_HANDLE(sfan, ec, "SFAN", /* 570 */
204 "JFNS", /* 770x-JL */
205 ); /* all others */
206
207#define IBM_HKEY_HID "IBM0068"
208#define IBM_PCI_HID "PNP0A03"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -0200210enum thermal_access_mode {
211 IBMACPI_THERMAL_NONE = 0, /* No thermal support */
212 IBMACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
213 IBMACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -0200214 IBMACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
215 IBMACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -0200216};
217
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -0200218#define IBMACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -0200219struct ibm_thermal_sensors_struct {
220 s32 temp[IBMACPI_MAX_THERMAL_SENSORS];
221};
222
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200223/*
224 * FAN ACCESS MODES
225 *
226 * IBMACPI_FAN_RD_ACPI_GFAN:
227 * ACPI GFAN method: returns fan level
228 *
229 * see IBMACPI_FAN_WR_ACPI_SFAN
230 * EC 0x2f not available if GFAN exists
231 *
232 * IBMACPI_FAN_WR_ACPI_SFAN:
233 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
234 *
235 * EC 0x2f might be available *for reading*, but never for writing.
236 *
237 * IBMACPI_FAN_WR_TPEC:
238 * ThinkPad EC register 0x2f (HFSP): fan control loop mode Supported
239 * on almost all ThinkPads
240 *
241 * Fan speed changes of any sort (including those caused by the
242 * disengaged mode) are usually done slowly by the firmware as the
243 * maximum ammount of fan duty cycle change per second seems to be
244 * limited.
245 *
246 * Reading is not available if GFAN exists.
247 * Writing is not available if SFAN exists.
248 *
249 * Bits
250 * 7 automatic mode engaged;
251 * (default operation mode of the ThinkPad)
252 * fan level is ignored in this mode.
253 * 6 disengage mode (takes precedence over bit 7);
254 * not available on all thinkpads. May disable
255 * the tachometer, and speeds up fan to 100% duty-cycle,
256 * which speeds it up far above the standard RPM
257 * levels. It is not impossible that it could cause
258 * hardware damage.
259 * 5-3 unused in some models. Extra bits for fan level
260 * in others, but still useless as all values above
261 * 7 map to the same speed as level 7 in these models.
262 * 2-0 fan level (0..7 usually)
263 * 0x00 = stop
264 * 0x07 = max (set when temperatures critical)
265 * Some ThinkPads may have other levels, see
266 * IBMACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
267 *
268 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
269 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
270 * does so, its initial value is meaningless (0x07).
271 *
272 * For firmware bugs, refer to:
273 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
274 *
275 * ----
276 *
277 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
278 * Main fan tachometer reading (in RPM)
279 *
280 * This register is present on all ThinkPads with a new-style EC, and
281 * it is known not to be present on the A21m/e, and T22, as there is
282 * something else in offset 0x84 according to the ACPI DSDT. Other
283 * ThinkPads from this same time period (and earlier) probably lack the
284 * tachometer as well.
285 *
286 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
287 * was never fixed by IBM to report the EC firmware version string
288 * probably support the tachometer (like the early X models), so
289 * detecting it is quite hard. We need more data to know for sure.
290 *
291 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
292 * might result.
293 *
294 * FIRMWARE BUG: when EC 0x2f bit 6 is set (disengaged mode), this
295 * register is not invalidated in ThinkPads that disable tachometer
296 * readings. Thus, the tachometer readings go stale.
297 *
298 * For firmware bugs, refer to:
299 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
300 *
301 * IBMACPI_FAN_WR_ACPI_FANS:
302 * ThinkPad X31, X40, X41. Not available in the X60.
303 *
304 * FANS ACPI handle: takes three arguments: low speed, medium speed,
305 * high speed. ACPI DSDT seems to map these three speeds to levels
306 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
307 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
308 *
309 * The speeds are stored on handles
310 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
311 *
312 * There are three default speed sets, acessible as handles:
313 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
314 *
315 * ACPI DSDT switches which set is in use depending on various
316 * factors.
317 *
318 * IBMACPI_FAN_WR_TPEC is also available and should be used to
319 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
320 * but the ACPI tables just mention level 7.
321 */
322
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -0200323enum fan_status_access_mode {
324 IBMACPI_FAN_NONE = 0, /* No fan status or control */
325 IBMACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
326 IBMACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
327};
328
329enum fan_control_access_mode {
330 IBMACPI_FAN_WR_NONE = 0, /* No fan control */
331 IBMACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
332 IBMACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
333 IBMACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
334};
335
336enum fan_control_commands {
337 IBMACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
338 IBMACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -0200339 IBMACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
340 * and also watchdog cmd */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -0200341};
342
343enum { /* Fan control constants */
344 fan_status_offset = 0x2f, /* EC register 0x2f */
345 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
346 * 0x84 must be read before 0x85 */
Henrique de Moraes Holschuhbab812a2006-11-24 11:47:12 -0200347
348 IBMACPI_FAN_EC_DISENGAGED = 0x40, /* EC mode: tachometer
349 * disengaged */
350 IBMACPI_FAN_EC_AUTO = 0x80, /* EC mode: auto fan
351 * control */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -0200352};
353
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -0200354static char *ibm_thinkpad_ec_found = NULL;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -0200355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356struct ibm_struct {
357 char *name;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400358 char param[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
360 char *hid;
361 struct acpi_driver *driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400363 int (*init) (void);
364 int (*read) (char *);
365 int (*write) (char *);
366 void (*exit) (void);
367
368 void (*notify) (struct ibm_struct *, u32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 acpi_handle *handle;
370 int type;
371 struct acpi_device *device;
372
373 int driver_registered;
374 int proc_created;
375 int init_called;
376 int notify_installed;
377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 int experimental;
379};
380
381static struct proc_dir_entry *proc_dir = NULL;
382
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -0200383static struct backlight_device *ibm_backlight_device = NULL;
Holger Macht8acb0252006-10-20 14:30:28 -0700384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385#define onoff(status,bit) ((status) & (1 << (bit)) ? "on" : "off")
386#define enabled(status,bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
387#define strlencmp(a,b) (strncmp((a), (b), strlen(b)))
388
389static int acpi_evalf(acpi_handle handle,
390 void *res, char *method, char *fmt, ...)
391{
392 char *fmt0 = fmt;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400393 struct acpi_object_list params;
394 union acpi_object in_objs[IBM_MAX_ACPI_ARGS];
395 struct acpi_buffer result, *resultp;
396 union acpi_object out_obj;
397 acpi_status status;
398 va_list ap;
399 char res_type;
400 int success;
401 int quiet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403 if (!*fmt) {
404 printk(IBM_ERR "acpi_evalf() called with empty format\n");
405 return 0;
406 }
407
408 if (*fmt == 'q') {
409 quiet = 1;
410 fmt++;
411 } else
412 quiet = 0;
413
414 res_type = *(fmt++);
415
416 params.count = 0;
417 params.pointer = &in_objs[0];
418
419 va_start(ap, fmt);
420 while (*fmt) {
421 char c = *(fmt++);
422 switch (c) {
423 case 'd': /* int */
424 in_objs[params.count].integer.value = va_arg(ap, int);
425 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
426 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400427 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 default:
429 printk(IBM_ERR "acpi_evalf() called "
430 "with invalid format character '%c'\n", c);
431 return 0;
432 }
433 }
434 va_end(ap);
435
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400436 if (res_type != 'v') {
437 result.length = sizeof(out_obj);
438 result.pointer = &out_obj;
439 resultp = &result;
440 } else
441 resultp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400443 status = acpi_evaluate_object(handle, method, &params, resultp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 switch (res_type) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400446 case 'd': /* int */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 if (res)
448 *(int *)res = out_obj.integer.value;
449 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
450 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400451 case 'v': /* void */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 success = status == AE_OK;
453 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400454 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 default:
456 printk(IBM_ERR "acpi_evalf() called "
457 "with invalid format character '%c'\n", res_type);
458 return 0;
459 }
460
461 if (!success && !quiet)
462 printk(IBM_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
463 method, fmt0, status);
464
465 return success;
466}
467
468static void __unused acpi_print_int(acpi_handle handle, char *method)
469{
470 int i;
471
472 if (acpi_evalf(handle, &i, method, "d"))
473 printk(IBM_INFO "%s = 0x%x\n", method, i);
474 else
475 printk(IBM_ERR "error calling %s\n", method);
476}
477
478static char *next_cmd(char **cmds)
479{
480 char *start = *cmds;
481 char *end;
482
483 while ((end = strchr(start, ',')) && end == start)
484 start = end + 1;
485
486 if (!end)
487 return NULL;
488
489 *end = 0;
490 *cmds = end + 1;
491 return start;
492}
493
Adrian Bunk1f217822006-12-19 13:01:28 -0800494static int ibm_acpi_driver_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
496 printk(IBM_INFO "%s v%s\n", IBM_DESC, IBM_VERSION);
497 printk(IBM_INFO "%s\n", IBM_URL);
498
499 return 0;
500}
501
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400502static int driver_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
504 int len = 0;
505
506 len += sprintf(p + len, "driver:\t\t%s\n", IBM_DESC);
507 len += sprintf(p + len, "version:\t%s\n", IBM_VERSION);
508
509 return len;
510}
511
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400512static int hotkey_supported;
513static int hotkey_mask_supported;
514static int hotkey_orig_status;
515static int hotkey_orig_mask;
516
517static int hotkey_get(int *status, int *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
519 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400520 return 0;
521
522 if (hotkey_mask_supported)
523 if (!acpi_evalf(hkey_handle, mask, "DHKN", "d"))
524 return 0;
525
526 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527}
528
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400529static int hotkey_set(int status, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530{
531 int i;
532
533 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 return 0;
535
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400536 if (hotkey_mask_supported)
537 for (i = 0; i < 32; i++) {
538 int bit = ((1 << i) & mask) != 0;
539 if (!acpi_evalf(hkey_handle,
540 NULL, "MHKM", "vdd", i + 1, bit))
541 return 0;
542 }
543
544 return 1;
545}
546
547static int hotkey_init(void)
548{
549 /* hotkey not supported on 570 */
550 hotkey_supported = hkey_handle != NULL;
551
552 if (hotkey_supported) {
553 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
554 A30, R30, R31, T20-22, X20-21, X22-24 */
555 hotkey_mask_supported =
556 acpi_evalf(hkey_handle, NULL, "DHKN", "qv");
557
558 if (!hotkey_get(&hotkey_orig_status, &hotkey_orig_mask))
559 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 }
561
562 return 0;
563}
564
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400565static int hotkey_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
567 int status, mask;
568 int len = 0;
569
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400570 if (!hotkey_supported) {
571 len += sprintf(p + len, "status:\t\tnot supported\n");
572 return len;
573 }
574
575 if (!hotkey_get(&status, &mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 return -EIO;
577
578 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400579 if (hotkey_mask_supported) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 len += sprintf(p + len, "mask:\t\t0x%04x\n", mask);
581 len += sprintf(p + len,
582 "commands:\tenable, disable, reset, <mask>\n");
583 } else {
584 len += sprintf(p + len, "mask:\t\tnot supported\n");
585 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
586 }
587
588 return len;
589}
590
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400591static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
593 int status, mask;
594 char *cmd;
595 int do_cmd = 0;
596
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400597 if (!hotkey_supported)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 return -ENODEV;
599
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400600 if (!hotkey_get(&status, &mask))
601 return -EIO;
602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 while ((cmd = next_cmd(&buf))) {
604 if (strlencmp(cmd, "enable") == 0) {
605 status = 1;
606 } else if (strlencmp(cmd, "disable") == 0) {
607 status = 0;
608 } else if (strlencmp(cmd, "reset") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400609 status = hotkey_orig_status;
610 mask = hotkey_orig_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
612 /* mask set */
613 } else if (sscanf(cmd, "%x", &mask) == 1) {
614 /* mask set */
615 } else
616 return -EINVAL;
617 do_cmd = 1;
618 }
619
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400620 if (do_cmd && !hotkey_set(status, mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 return -EIO;
622
623 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400624}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400626static void hotkey_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400628 if (hotkey_supported)
629 hotkey_set(hotkey_orig_status, hotkey_orig_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630}
631
632static void hotkey_notify(struct ibm_struct *ibm, u32 event)
633{
634 int hkey;
635
636 if (acpi_evalf(hkey_handle, &hkey, "MHKP", "d"))
637 acpi_bus_generate_event(ibm->device, event, hkey);
638 else {
639 printk(IBM_ERR "unknown hotkey event %d\n", event);
640 acpi_bus_generate_event(ibm->device, event, 0);
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400641 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642}
643
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400644static int bluetooth_supported;
645
646static int bluetooth_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400648 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
649 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
650 bluetooth_supported = hkey_handle &&
651 acpi_evalf(hkey_handle, NULL, "GBDC", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
653 return 0;
654}
655
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400656static int bluetooth_status(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657{
658 int status;
659
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400660 if (!bluetooth_supported ||
661 !acpi_evalf(hkey_handle, &status, "GBDC", "d"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 status = 0;
663
664 return status;
665}
666
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400667static int bluetooth_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668{
669 int len = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400670 int status = bluetooth_status();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400672 if (!bluetooth_supported)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 len += sprintf(p + len, "status:\t\tnot supported\n");
674 else if (!(status & 1))
675 len += sprintf(p + len, "status:\t\tnot installed\n");
676 else {
677 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 1));
678 len += sprintf(p + len, "commands:\tenable, disable\n");
679 }
680
681 return len;
682}
683
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400684static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400686 int status = bluetooth_status();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 char *cmd;
688 int do_cmd = 0;
689
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400690 if (!bluetooth_supported)
691 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
693 while ((cmd = next_cmd(&buf))) {
694 if (strlencmp(cmd, "enable") == 0) {
695 status |= 2;
696 } else if (strlencmp(cmd, "disable") == 0) {
697 status &= ~2;
698 } else
699 return -EINVAL;
700 do_cmd = 1;
701 }
702
703 if (do_cmd && !acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400704 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
706 return 0;
707}
708
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -0400709static int wan_supported;
710
711static int wan_init(void)
712{
713 wan_supported = hkey_handle &&
714 acpi_evalf(hkey_handle, NULL, "GWAN", "qv");
715
716 return 0;
717}
718
719static int wan_status(void)
720{
721 int status;
722
Henrique de Moraes Holschuh8d297262006-11-24 11:47:07 -0200723 if (!wan_supported || !acpi_evalf(hkey_handle, &status, "GWAN", "d"))
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -0400724 status = 0;
725
726 return status;
727}
728
729static int wan_read(char *p)
730{
731 int len = 0;
732 int status = wan_status();
733
734 if (!wan_supported)
735 len += sprintf(p + len, "status:\t\tnot supported\n");
736 else if (!(status & 1))
737 len += sprintf(p + len, "status:\t\tnot installed\n");
738 else {
739 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 1));
740 len += sprintf(p + len, "commands:\tenable, disable\n");
741 }
742
743 return len;
744}
745
746static int wan_write(char *buf)
747{
748 int status = wan_status();
749 char *cmd;
750 int do_cmd = 0;
751
752 if (!wan_supported)
753 return -ENODEV;
754
755 while ((cmd = next_cmd(&buf))) {
756 if (strlencmp(cmd, "enable") == 0) {
757 status |= 2;
758 } else if (strlencmp(cmd, "disable") == 0) {
759 status &= ~2;
760 } else
761 return -EINVAL;
762 do_cmd = 1;
763 }
764
765 if (do_cmd && !acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
766 return -EIO;
767
768 return 0;
769}
770
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200771enum video_access_mode {
772 IBMACPI_VIDEO_NONE = 0,
773 IBMACPI_VIDEO_570, /* 570 */
774 IBMACPI_VIDEO_770, /* 600e/x, 770e, 770x */
775 IBMACPI_VIDEO_NEW, /* all others */
776};
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400777
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200778static enum video_access_mode video_supported;
779static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400780
781static int video_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400783 int ivga;
784
785 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
786 /* G41, assume IVGA doesn't change */
787 vid_handle = vid2_handle;
788
789 if (!vid_handle)
790 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200791 video_supported = IBMACPI_VIDEO_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400792 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
793 /* 570 */
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200794 video_supported = IBMACPI_VIDEO_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400795 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
796 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200797 video_supported = IBMACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400798 else
799 /* all others */
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200800 video_supported = IBMACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
802 return 0;
803}
804
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400805static int video_status(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806{
807 int status = 0;
808 int i;
809
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200810 if (video_supported == IBMACPI_VIDEO_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400811 if (acpi_evalf(NULL, &i, "\\_SB.PHS", "dd", 0x87))
812 status = i & 3;
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200813 } else if (video_supported == IBMACPI_VIDEO_770) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400814 if (acpi_evalf(NULL, &i, "\\VCDL", "d"))
815 status |= 0x01 * i;
816 if (acpi_evalf(NULL, &i, "\\VCDC", "d"))
817 status |= 0x02 * i;
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200818 } else if (video_supported == IBMACPI_VIDEO_NEW) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400819 acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1);
820 if (acpi_evalf(NULL, &i, "\\VCDC", "d"))
821 status |= 0x02 * i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400823 acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0);
824 if (acpi_evalf(NULL, &i, "\\VCDL", "d"))
825 status |= 0x01 * i;
826 if (acpi_evalf(NULL, &i, "\\VCDD", "d"))
827 status |= 0x08 * i;
828 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
830 return status;
831}
832
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400833static int video_autosw(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400835 int autosw = 0;
836
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200837 if (video_supported == IBMACPI_VIDEO_570)
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400838 acpi_evalf(vid_handle, &autosw, "SWIT", "d");
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200839 else if (video_supported == IBMACPI_VIDEO_770 ||
840 video_supported == IBMACPI_VIDEO_NEW)
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400841 acpi_evalf(vid_handle, &autosw, "^VDEE", "d");
842
843 return autosw & 1;
844}
845
846static int video_read(char *p)
847{
848 int status = video_status();
849 int autosw = video_autosw();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 int len = 0;
851
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400852 if (!video_supported) {
853 len += sprintf(p + len, "status:\t\tnot supported\n");
854 return len;
855 }
856
857 len += sprintf(p + len, "status:\t\tsupported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
859 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200860 if (video_supported == IBMACPI_VIDEO_NEW)
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400861 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
862 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
863 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
864 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200865 if (video_supported == IBMACPI_VIDEO_NEW)
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400866 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
867 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
869
870 return len;
871}
872
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400873static int video_switch(void)
874{
875 int autosw = video_autosw();
876 int ret;
877
878 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
879 return -EIO;
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200880 ret = video_supported == IBMACPI_VIDEO_570 ?
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400881 acpi_evalf(ec_handle, NULL, "_Q16", "v") :
882 acpi_evalf(vid_handle, NULL, "VSWT", "v");
883 acpi_evalf(vid_handle, NULL, "_DOS", "vd", autosw);
884
885 return ret;
886}
887
888static int video_expand(void)
889{
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200890 if (video_supported == IBMACPI_VIDEO_570)
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400891 return acpi_evalf(ec_handle, NULL, "_Q17", "v");
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200892 else if (video_supported == IBMACPI_VIDEO_770)
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400893 return acpi_evalf(vid_handle, NULL, "VEXP", "v");
894 else
895 return acpi_evalf(NULL, NULL, "\\VEXP", "v");
896}
897
898static int video_switch2(int status)
899{
900 int ret;
901
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200902 if (video_supported == IBMACPI_VIDEO_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400903 ret = acpi_evalf(NULL, NULL,
904 "\\_SB.PHS2", "vdd", 0x8b, status | 0x80);
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200905 } else if (video_supported == IBMACPI_VIDEO_770) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400906 int autosw = video_autosw();
907 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
908 return -EIO;
909
910 ret = acpi_evalf(vid_handle, NULL,
911 "ASWT", "vdd", status * 0x100, 0);
912
913 acpi_evalf(vid_handle, NULL, "_DOS", "vd", autosw);
914 } else {
915 ret = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
916 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
917 }
918
919 return ret;
920}
921
922static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923{
924 char *cmd;
925 int enable, disable, status;
926
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400927 if (!video_supported)
928 return -ENODEV;
929
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 enable = disable = 0;
931
932 while ((cmd = next_cmd(&buf))) {
933 if (strlencmp(cmd, "lcd_enable") == 0) {
934 enable |= 0x01;
935 } else if (strlencmp(cmd, "lcd_disable") == 0) {
936 disable |= 0x01;
937 } else if (strlencmp(cmd, "crt_enable") == 0) {
938 enable |= 0x02;
939 } else if (strlencmp(cmd, "crt_disable") == 0) {
940 disable |= 0x02;
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200941 } else if (video_supported == IBMACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400942 strlencmp(cmd, "dvi_enable") == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 enable |= 0x08;
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200944 } else if (video_supported == IBMACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400945 strlencmp(cmd, "dvi_disable") == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 disable |= 0x08;
947 } else if (strlencmp(cmd, "auto_enable") == 0) {
948 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
949 return -EIO;
950 } else if (strlencmp(cmd, "auto_disable") == 0) {
951 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 0))
952 return -EIO;
953 } else if (strlencmp(cmd, "video_switch") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400954 if (!video_switch())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 return -EIO;
956 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400957 if (!video_expand())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 return -EIO;
959 } else
960 return -EINVAL;
961 }
962
963 if (enable || disable) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400964 status = (video_status() & 0x0f & ~disable) | enable;
965 if (!video_switch2(status))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 return -EIO;
967 }
968
969 return 0;
970}
971
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400972static void video_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400974 acpi_evalf(vid_handle, NULL, "_DOS", "vd", video_orig_autosw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975}
976
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400977static int light_supported;
978static int light_status_supported;
979
980static int light_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400982 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
983 light_supported = (cmos_handle || lght_handle) && !ledb_handle;
984
985 if (light_supported)
986 /* light status not supported on
987 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
988 light_status_supported = acpi_evalf(ec_handle, NULL,
989 "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
991 return 0;
992}
993
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400994static int light_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995{
996 int len = 0;
997 int status = 0;
998
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400999 if (!light_supported) {
1000 len += sprintf(p + len, "status:\t\tnot supported\n");
1001 } else if (!light_status_supported) {
1002 len += sprintf(p + len, "status:\t\tunknown\n");
1003 len += sprintf(p + len, "commands:\ton, off\n");
1004 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
1006 return -EIO;
1007 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001008 len += sprintf(p + len, "commands:\ton, off\n");
1009 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
1011 return len;
1012}
1013
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001014static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015{
1016 int cmos_cmd, lght_cmd;
1017 char *cmd;
1018 int success;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001019
1020 if (!light_supported)
1021 return -ENODEV;
1022
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 while ((cmd = next_cmd(&buf))) {
1024 if (strlencmp(cmd, "on") == 0) {
1025 cmos_cmd = 0x0c;
1026 lght_cmd = 1;
1027 } else if (strlencmp(cmd, "off") == 0) {
1028 cmos_cmd = 0x0d;
1029 lght_cmd = 0;
1030 } else
1031 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001032
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 success = cmos_handle ?
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001034 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
1035 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 if (!success)
1037 return -EIO;
1038 }
1039
1040 return 0;
1041}
1042
1043static int _sta(acpi_handle handle)
1044{
1045 int status;
1046
1047 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
1048 status = 0;
1049
1050 return status;
1051}
Henrique de Moraes Holschuhd2fadbb2007-01-11 02:58:15 -05001052
Kristen Accardi63e5f242006-02-23 17:56:06 -08001053#ifdef CONFIG_ACPI_IBM_DOCK
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054#define dock_docked() (_sta(dock_handle) & 1)
1055
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001056static int dock_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057{
1058 int len = 0;
1059 int docked = dock_docked();
1060
1061 if (!dock_handle)
1062 len += sprintf(p + len, "status:\t\tnot supported\n");
1063 else if (!docked)
1064 len += sprintf(p + len, "status:\t\tundocked\n");
1065 else {
1066 len += sprintf(p + len, "status:\t\tdocked\n");
1067 len += sprintf(p + len, "commands:\tdock, undock\n");
1068 }
1069
1070 return len;
1071}
1072
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001073static int dock_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074{
1075 char *cmd;
1076
1077 if (!dock_docked())
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001078 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
1080 while ((cmd = next_cmd(&buf))) {
1081 if (strlencmp(cmd, "undock") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001082 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
1083 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 return -EIO;
1085 } else if (strlencmp(cmd, "dock") == 0) {
1086 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
1087 return -EIO;
1088 } else
1089 return -EINVAL;
1090 }
1091
1092 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001093}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
1095static void dock_notify(struct ibm_struct *ibm, u32 event)
1096{
1097 int docked = dock_docked();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001098 int pci = ibm->hid && strstr(ibm->hid, IBM_PCI_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001100 if (event == 1 && !pci) /* 570 */
1101 acpi_bus_generate_event(ibm->device, event, 1); /* button */
1102 else if (event == 1 && pci) /* 570 */
1103 acpi_bus_generate_event(ibm->device, event, 3); /* dock */
1104 else if (event == 3 && docked)
1105 acpi_bus_generate_event(ibm->device, event, 1); /* button */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 else if (event == 3 && !docked)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001107 acpi_bus_generate_event(ibm->device, event, 2); /* undock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 else if (event == 0 && docked)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001109 acpi_bus_generate_event(ibm->device, event, 3); /* dock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 else {
1111 printk(IBM_ERR "unknown dock event %d, status %d\n",
1112 event, _sta(dock_handle));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001113 acpi_bus_generate_event(ibm->device, event, 0); /* unknown */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 }
1115}
Kristen Accardi63e5f242006-02-23 17:56:06 -08001116#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001118static int bay_status_supported;
1119static int bay_status2_supported;
1120static int bay_eject_supported;
1121static int bay_eject2_supported;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001123static int bay_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001125 bay_status_supported = bay_handle &&
1126 acpi_evalf(bay_handle, NULL, "_STA", "qv");
1127 bay_status2_supported = bay2_handle &&
1128 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
1129
1130 bay_eject_supported = bay_handle && bay_ej_handle &&
1131 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
1132 bay_eject2_supported = bay2_handle && bay2_ej_handle &&
1133 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
1135 return 0;
1136}
1137
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001138#define bay_occupied(b) (_sta(b##_handle) & 1)
1139
1140static int bay_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141{
1142 int len = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001143 int occupied = bay_occupied(bay);
1144 int occupied2 = bay_occupied(bay2);
1145 int eject, eject2;
1146
1147 len += sprintf(p + len, "status:\t\t%s\n", bay_status_supported ?
1148 (occupied ? "occupied" : "unoccupied") :
1149 "not supported");
1150 if (bay_status2_supported)
1151 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
1152 "occupied" : "unoccupied");
1153
1154 eject = bay_eject_supported && occupied;
1155 eject2 = bay_eject2_supported && occupied2;
1156
1157 if (eject && eject2)
1158 len += sprintf(p + len, "commands:\teject, eject2\n");
1159 else if (eject)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 len += sprintf(p + len, "commands:\teject\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001161 else if (eject2)
1162 len += sprintf(p + len, "commands:\teject2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
1164 return len;
1165}
1166
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001167static int bay_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168{
1169 char *cmd;
1170
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001171 if (!bay_eject_supported && !bay_eject2_supported)
1172 return -ENODEV;
1173
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001175 if (bay_eject_supported && strlencmp(cmd, "eject") == 0) {
1176 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
1177 return -EIO;
1178 } else if (bay_eject2_supported &&
1179 strlencmp(cmd, "eject2") == 0) {
1180 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 return -EIO;
1182 } else
1183 return -EINVAL;
1184 }
1185
1186 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001187}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
1189static void bay_notify(struct ibm_struct *ibm, u32 event)
1190{
1191 acpi_bus_generate_event(ibm->device, event, 0);
1192}
1193
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001194static int cmos_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195{
1196 int len = 0;
1197
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001198 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1199 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 if (!cmos_handle)
1201 len += sprintf(p + len, "status:\t\tnot supported\n");
1202 else {
1203 len += sprintf(p + len, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001204 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 }
1206
1207 return len;
1208}
1209
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001210static int cmos_eval(int cmos_cmd)
1211{
1212 if (cmos_handle)
1213 return acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd);
1214 else
1215 return 1;
1216}
1217
1218static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219{
1220 char *cmd;
1221 int cmos_cmd;
1222
1223 if (!cmos_handle)
1224 return -EINVAL;
1225
1226 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001227 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
1228 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 /* cmos_cmd set */
1230 } else
1231 return -EINVAL;
1232
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001233 if (!cmos_eval(cmos_cmd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 return -EIO;
1235 }
1236
1237 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001238}
1239
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02001240enum led_access_mode {
1241 IBMACPI_LED_NONE = 0,
1242 IBMACPI_LED_570, /* 570 */
1243 IBMACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
1244 IBMACPI_LED_NEW, /* all others */
1245};
1246static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001247
1248static int led_init(void)
1249{
1250 if (!led_handle)
1251 /* led not supported on R30, R31 */
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02001252 led_supported = IBMACPI_LED_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001253 else if (strlencmp(led_path, "SLED") == 0)
1254 /* 570 */
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02001255 led_supported = IBMACPI_LED_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001256 else if (strlencmp(led_path, "SYSL") == 0)
1257 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02001258 led_supported = IBMACPI_LED_OLD;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001259 else
1260 /* all others */
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02001261 led_supported = IBMACPI_LED_NEW;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001262
1263 return 0;
1264}
1265
1266#define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
1267
1268static int led_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269{
1270 int len = 0;
1271
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001272 if (!led_supported) {
1273 len += sprintf(p + len, "status:\t\tnot supported\n");
1274 return len;
1275 }
1276 len += sprintf(p + len, "status:\t\tsupported\n");
1277
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02001278 if (led_supported == IBMACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001279 /* 570 */
1280 int i, status;
1281 for (i = 0; i < 8; i++) {
1282 if (!acpi_evalf(ec_handle,
1283 &status, "GLED", "dd", 1 << i))
1284 return -EIO;
1285 len += sprintf(p + len, "%d:\t\t%s\n",
1286 i, led_status(status));
1287 }
1288 }
1289
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 len += sprintf(p + len, "commands:\t"
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001291 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
1293 return len;
1294}
1295
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001296/* off, on, blink */
1297static const int led_sled_arg1[] = { 0, 1, 3 };
1298static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
1299static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
1300static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
1301
1302#define EC_HLCL 0x0c
1303#define EC_HLBL 0x0d
1304#define EC_HLMS 0x0e
1305
1306static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307{
1308 char *cmd;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001309 int led, ind, ret;
1310
1311 if (!led_supported)
1312 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
1314 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001315 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 return -EINVAL;
1317
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001318 if (strstr(cmd, "off")) {
1319 ind = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 } else if (strstr(cmd, "on")) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001321 ind = 1;
1322 } else if (strstr(cmd, "blink")) {
1323 ind = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 } else
1325 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001326
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02001327 if (led_supported == IBMACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001328 /* 570 */
1329 led = 1 << led;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001331 led, led_sled_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 return -EIO;
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02001333 } else if (led_supported == IBMACPI_LED_OLD) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001334 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
1335 led = 1 << led;
1336 ret = ec_write(EC_HLMS, led);
1337 if (ret >= 0)
1338 ret =
1339 ec_write(EC_HLBL, led * led_exp_hlbl[ind]);
1340 if (ret >= 0)
1341 ret =
1342 ec_write(EC_HLCL, led * led_exp_hlcl[ind]);
1343 if (ret < 0)
1344 return ret;
1345 } else {
1346 /* all others */
1347 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
1348 led, led_led_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001350 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 }
1352
1353 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001354}
1355
1356static int beep_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357{
1358 int len = 0;
1359
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001360 if (!beep_handle)
1361 len += sprintf(p + len, "status:\t\tnot supported\n");
1362 else {
1363 len += sprintf(p + len, "status:\t\tsupported\n");
1364 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
1365 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
1367 return len;
1368}
1369
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001370static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371{
1372 char *cmd;
1373 int beep_cmd;
1374
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001375 if (!beep_handle)
1376 return -ENODEV;
1377
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001379 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
1380 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 /* beep_cmd set */
1382 } else
1383 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001384 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 return -EIO;
1386 }
1387
1388 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001389}
1390
1391static int acpi_ec_read(int i, u8 * p)
1392{
1393 int v;
1394
1395 if (ecrd_handle) {
1396 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
1397 return 0;
1398 *p = v;
1399 } else {
1400 if (ec_read(i, p) < 0)
1401 return 0;
1402 }
1403
1404 return 1;
1405}
1406
1407static int acpi_ec_write(int i, u8 v)
1408{
1409 if (ecwr_handle) {
1410 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
1411 return 0;
1412 } else {
1413 if (ec_write(i, v) < 0)
1414 return 0;
1415 }
1416
1417 return 1;
1418}
1419
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001420static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001421
1422static int thermal_init(void)
1423{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001424 u8 t, ta1, ta2;
1425 int i;
1426 int acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001427
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001428 if (ibm_thinkpad_ec_found && experimental) {
1429 /*
1430 * Direct EC access mode: sensors at registers
1431 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
1432 * non-implemented, thermal sensors return 0x80 when
1433 * not available
1434 */
1435
1436 ta1 = ta2 = 0;
1437 for (i = 0; i < 8; i++) {
1438 if (likely(acpi_ec_read(0x78 + i, &t))) {
1439 ta1 |= t;
1440 } else {
1441 ta1 = 0;
1442 break;
1443 }
1444 if (likely(acpi_ec_read(0xC0 + i, &t))) {
1445 ta2 |= t;
1446 } else {
1447 ta1 = 0;
1448 break;
1449 }
1450 }
1451 if (ta1 == 0) {
1452 /* This is sheer paranoia, but we handle it anyway */
1453 if (acpi_tmp7) {
1454 printk(IBM_ERR
1455 "ThinkPad ACPI EC access misbehaving, "
1456 "falling back to ACPI TMPx access mode\n");
1457 thermal_read_mode = IBMACPI_THERMAL_ACPI_TMP07;
1458 } else {
1459 printk(IBM_ERR
1460 "ThinkPad ACPI EC access misbehaving, "
1461 "disabling thermal sensors access\n");
1462 thermal_read_mode = IBMACPI_THERMAL_NONE;
1463 }
1464 } else {
1465 thermal_read_mode =
1466 (ta2 != 0) ?
1467 IBMACPI_THERMAL_TPEC_16 : IBMACPI_THERMAL_TPEC_8;
1468 }
1469 } else if (acpi_tmp7) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001470 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
1471 /* 600e/x, 770e, 770x */
1472 thermal_read_mode = IBMACPI_THERMAL_ACPI_UPDT;
1473 } else {
1474 /* Standard ACPI TMPx access, max 8 sensors */
1475 thermal_read_mode = IBMACPI_THERMAL_ACPI_TMP07;
1476 }
1477 } else {
1478 /* temperatures not supported on 570, G4x, R30, R31, R32 */
1479 thermal_read_mode = IBMACPI_THERMAL_NONE;
1480 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001481
1482 return 0;
1483}
1484
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001485static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
1486{
1487 int i, t;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001488 s8 tmp;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001489 char tmpi[] = "TMPi";
1490
1491 if (!s)
1492 return -EINVAL;
1493
1494 switch (thermal_read_mode) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001495#if IBMACPI_MAX_THERMAL_SENSORS >= 16
1496 case IBMACPI_THERMAL_TPEC_16:
1497 for (i = 0; i < 8; i++) {
1498 if (!acpi_ec_read(0xC0 + i, &tmp))
1499 return -EIO;
1500 s->temp[i + 8] = tmp * 1000;
1501 }
1502 /* fallthrough */
1503#endif
1504 case IBMACPI_THERMAL_TPEC_8:
1505 for (i = 0; i < 8; i++) {
1506 if (!acpi_ec_read(0x78 + i, &tmp))
1507 return -EIO;
1508 s->temp[i] = tmp * 1000;
1509 }
1510 return (thermal_read_mode == IBMACPI_THERMAL_TPEC_16) ? 16 : 8;
1511
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001512 case IBMACPI_THERMAL_ACPI_UPDT:
1513 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
1514 return -EIO;
1515 for (i = 0; i < 8; i++) {
1516 tmpi[3] = '0' + i;
1517 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
1518 return -EIO;
1519 s->temp[i] = (t - 2732) * 100;
1520 }
1521 return 8;
1522
1523 case IBMACPI_THERMAL_ACPI_TMP07:
1524 for (i = 0; i < 8; i++) {
1525 tmpi[3] = '0' + i;
1526 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
1527 return -EIO;
1528 s->temp[i] = t * 1000;
1529 }
1530 return 8;
1531
1532 case IBMACPI_THERMAL_NONE:
1533 default:
1534 return 0;
1535 }
1536}
1537
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001538static int thermal_read(char *p)
1539{
1540 int len = 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001541 int n, i;
1542 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001543
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001544 n = thermal_get_sensors(&t);
1545 if (unlikely(n < 0))
1546 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001547
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001548 len += sprintf(p + len, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001549
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001550 if (n > 0) {
1551 for (i = 0; i < (n - 1); i++)
1552 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
1553 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
1554 } else
1555 len += sprintf(p + len, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001556
1557 return len;
1558}
1559
1560static u8 ecdump_regs[256];
1561
1562static int ecdump_read(char *p)
1563{
1564 int len = 0;
1565 int i, j;
1566 u8 v;
1567
1568 len += sprintf(p + len, "EC "
1569 " +00 +01 +02 +03 +04 +05 +06 +07"
1570 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
1571 for (i = 0; i < 256; i += 16) {
1572 len += sprintf(p + len, "EC 0x%02x:", i);
1573 for (j = 0; j < 16; j++) {
1574 if (!acpi_ec_read(i + j, &v))
1575 break;
1576 if (v != ecdump_regs[i + j])
1577 len += sprintf(p + len, " *%02x", v);
1578 else
1579 len += sprintf(p + len, " %02x", v);
1580 ecdump_regs[i + j] = v;
1581 }
1582 len += sprintf(p + len, "\n");
1583 if (j != 16)
1584 break;
1585 }
1586
1587 /* These are way too dangerous to advertise openly... */
1588#if 0
1589 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
1590 " (<offset> is 00-ff, <value> is 00-ff)\n");
1591 len += sprintf(p + len, "commands:\t0x<offset> <value> "
1592 " (<offset> is 00-ff, <value> is 0-255)\n");
1593#endif
1594 return len;
1595}
1596
1597static int ecdump_write(char *buf)
1598{
1599 char *cmd;
1600 int i, v;
1601
1602 while ((cmd = next_cmd(&buf))) {
1603 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
1604 /* i and v set */
1605 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
1606 /* i and v set */
1607 } else
1608 return -EINVAL;
1609 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
1610 if (!acpi_ec_write(i, v))
1611 return -EIO;
1612 } else
1613 return -EINVAL;
1614 }
1615
1616 return 0;
1617}
1618
1619static int brightness_offset = 0x31;
1620
Holger Macht8acb0252006-10-20 14:30:28 -07001621static int brightness_get(struct backlight_device *bd)
1622{
Henrique de Moraes Holschuh8d297262006-11-24 11:47:07 -02001623 u8 level;
1624 if (!acpi_ec_read(brightness_offset, &level))
1625 return -EIO;
Holger Macht8acb0252006-10-20 14:30:28 -07001626
Henrique de Moraes Holschuh8d297262006-11-24 11:47:07 -02001627 level &= 0x7;
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02001628
Henrique de Moraes Holschuh8d297262006-11-24 11:47:07 -02001629 return level;
Holger Macht8acb0252006-10-20 14:30:28 -07001630}
1631
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001632static int brightness_read(char *p)
1633{
1634 int len = 0;
Holger Macht8acb0252006-10-20 14:30:28 -07001635 int level;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001636
Holger Macht8acb0252006-10-20 14:30:28 -07001637 if ((level = brightness_get(NULL)) < 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001638 len += sprintf(p + len, "level:\t\tunreadable\n");
1639 } else {
1640 len += sprintf(p + len, "level:\t\t%d\n", level & 0x7);
1641 len += sprintf(p + len, "commands:\tup, down\n");
1642 len += sprintf(p + len, "commands:\tlevel <level>"
1643 " (<level> is 0-7)\n");
1644 }
1645
1646 return len;
1647}
1648
1649#define BRIGHTNESS_UP 4
1650#define BRIGHTNESS_DOWN 5
1651
Holger Macht8acb0252006-10-20 14:30:28 -07001652static int brightness_set(int value)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001653{
1654 int cmos_cmd, inc, i;
Holger Macht8acb0252006-10-20 14:30:28 -07001655 int current_value = brightness_get(NULL);
1656
1657 value &= 7;
1658
1659 cmos_cmd = value > current_value ? BRIGHTNESS_UP : BRIGHTNESS_DOWN;
1660 inc = value > current_value ? 1 : -1;
1661 for (i = current_value; i != value; i += inc) {
1662 if (!cmos_eval(cmos_cmd))
1663 return -EIO;
1664 if (!acpi_ec_write(brightness_offset, i + inc))
1665 return -EIO;
1666 }
1667
1668 return 0;
1669}
1670
1671static int brightness_write(char *buf)
1672{
1673 int level;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001674 int new_level;
1675 char *cmd;
1676
1677 while ((cmd = next_cmd(&buf))) {
Holger Macht8acb0252006-10-20 14:30:28 -07001678 if ((level = brightness_get(NULL)) < 0)
1679 return level;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001680 level &= 7;
1681
1682 if (strlencmp(cmd, "up") == 0) {
1683 new_level = level == 7 ? 7 : level + 1;
1684 } else if (strlencmp(cmd, "down") == 0) {
1685 new_level = level == 0 ? 0 : level - 1;
1686 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
1687 new_level >= 0 && new_level <= 7) {
1688 /* new_level set */
1689 } else
1690 return -EINVAL;
1691
Holger Macht8acb0252006-10-20 14:30:28 -07001692 brightness_set(new_level);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001693 }
1694
1695 return 0;
1696}
1697
Holger Macht8acb0252006-10-20 14:30:28 -07001698static int brightness_update_status(struct backlight_device *bd)
1699{
1700 return brightness_set(bd->props->brightness);
1701}
1702
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02001703static struct backlight_properties ibm_backlight_data = {
1704 .owner = THIS_MODULE,
1705 .get_brightness = brightness_get,
1706 .update_status = brightness_update_status,
1707 .max_brightness = 7,
1708};
1709
1710static int brightness_init(void)
1711{
Yu Luming519ab5f2006-12-19 12:56:15 -08001712 ibm_backlight_device = backlight_device_register("ibm", NULL, NULL,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02001713 &ibm_backlight_data);
1714 if (IS_ERR(ibm_backlight_device)) {
1715 printk(IBM_ERR "Could not register backlight device\n");
1716 return PTR_ERR(ibm_backlight_device);
1717 }
1718
1719 return 0;
1720}
1721
1722static void brightness_exit(void)
1723{
1724 if (ibm_backlight_device) {
1725 backlight_device_unregister(ibm_backlight_device);
1726 ibm_backlight_device = NULL;
1727 }
1728}
1729
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001730static int volume_offset = 0x30;
1731
1732static int volume_read(char *p)
1733{
1734 int len = 0;
1735 u8 level;
1736
1737 if (!acpi_ec_read(volume_offset, &level)) {
1738 len += sprintf(p + len, "level:\t\tunreadable\n");
1739 } else {
1740 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
1741 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
1742 len += sprintf(p + len, "commands:\tup, down, mute\n");
1743 len += sprintf(p + len, "commands:\tlevel <level>"
1744 " (<level> is 0-15)\n");
1745 }
1746
1747 return len;
1748}
1749
1750#define VOLUME_DOWN 0
1751#define VOLUME_UP 1
1752#define VOLUME_MUTE 2
1753
1754static int volume_write(char *buf)
1755{
1756 int cmos_cmd, inc, i;
1757 u8 level, mute;
1758 int new_level, new_mute;
1759 char *cmd;
1760
1761 while ((cmd = next_cmd(&buf))) {
1762 if (!acpi_ec_read(volume_offset, &level))
1763 return -EIO;
1764 new_mute = mute = level & 0x40;
1765 new_level = level = level & 0xf;
1766
1767 if (strlencmp(cmd, "up") == 0) {
1768 if (mute)
1769 new_mute = 0;
1770 else
1771 new_level = level == 15 ? 15 : level + 1;
1772 } else if (strlencmp(cmd, "down") == 0) {
1773 if (mute)
1774 new_mute = 0;
1775 else
1776 new_level = level == 0 ? 0 : level - 1;
1777 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
1778 new_level >= 0 && new_level <= 15) {
1779 /* new_level set */
1780 } else if (strlencmp(cmd, "mute") == 0) {
1781 new_mute = 0x40;
1782 } else
1783 return -EINVAL;
1784
1785 if (new_level != level) { /* mute doesn't change */
1786 cmos_cmd = new_level > level ? VOLUME_UP : VOLUME_DOWN;
1787 inc = new_level > level ? 1 : -1;
1788
1789 if (mute && (!cmos_eval(cmos_cmd) ||
1790 !acpi_ec_write(volume_offset, level)))
1791 return -EIO;
1792
1793 for (i = level; i != new_level; i += inc)
1794 if (!cmos_eval(cmos_cmd) ||
1795 !acpi_ec_write(volume_offset, i + inc))
1796 return -EIO;
1797
1798 if (mute && (!cmos_eval(VOLUME_MUTE) ||
1799 !acpi_ec_write(volume_offset,
1800 new_level + mute)))
1801 return -EIO;
1802 }
1803
1804 if (new_mute != mute) { /* level doesn't change */
1805 cmos_cmd = new_mute ? VOLUME_MUTE : VOLUME_UP;
1806
1807 if (!cmos_eval(cmos_cmd) ||
1808 !acpi_ec_write(volume_offset, level + new_mute))
1809 return -EIO;
1810 }
1811 }
1812
1813 return 0;
1814}
1815
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001816static enum fan_status_access_mode fan_status_access_mode;
1817static enum fan_control_access_mode fan_control_access_mode;
1818static enum fan_control_commands fan_control_commands;
1819
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02001820static int fan_control_status_known;
1821static u8 fan_control_initial_status;
1822
Len Brown25c68a32006-12-08 04:43:41 -05001823static void fan_watchdog_fire(struct work_struct *ignored);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02001824static int fan_watchdog_maxinterval;
Len Brown25c68a32006-12-08 04:43:41 -05001825static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02001826
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001827static int fan_init(void)
1828{
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001829 fan_status_access_mode = IBMACPI_FAN_NONE;
1830 fan_control_access_mode = IBMACPI_FAN_WR_NONE;
1831 fan_control_commands = 0;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02001832 fan_control_status_known = 1;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02001833 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001834
1835 if (gfan_handle) {
1836 /* 570, 600e/x, 770e, 770x */
1837 fan_status_access_mode = IBMACPI_FAN_RD_ACPI_GFAN;
1838 } else {
1839 /* all other ThinkPads: note that even old-style
1840 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02001841 if (likely(acpi_ec_read(fan_status_offset,
1842 &fan_control_initial_status))) {
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001843 fan_status_access_mode = IBMACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02001844
1845 /* In some ThinkPads, neither the EC nor the ACPI
1846 * DSDT initialize the fan status, and it ends up
1847 * being set to 0x07 when it *could* be either
1848 * 0x07 or 0x80.
1849 *
1850 * Enable for TP-1Y (T43), TP-78 (R51e),
1851 * TP-76 (R52), TP-70 (T43, R52), which are known
1852 * to be buggy. */
1853 if (fan_control_initial_status == 0x07 &&
1854 ibm_thinkpad_ec_found &&
1855 ((ibm_thinkpad_ec_found[0] == '1' &&
1856 ibm_thinkpad_ec_found[1] == 'Y') ||
1857 (ibm_thinkpad_ec_found[0] == '7' &&
1858 (ibm_thinkpad_ec_found[1] == '6' ||
1859 ibm_thinkpad_ec_found[1] == '8' ||
1860 ibm_thinkpad_ec_found[1] == '0'))
1861 )) {
1862 printk(IBM_NOTICE
1863 "fan_init: initial fan status is "
1864 "unknown, assuming it is in auto "
1865 "mode\n");
1866 fan_control_status_known = 0;
1867 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001868 } else {
1869 printk(IBM_ERR
1870 "ThinkPad ACPI EC access misbehaving, "
1871 "fan status and control unavailable\n");
1872 return 0;
1873 }
1874 }
1875
1876 if (sfan_handle) {
1877 /* 570, 770x-JL */
1878 fan_control_access_mode = IBMACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02001879 fan_control_commands |=
1880 IBMACPI_FAN_CMD_LEVEL | IBMACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001881 } else {
1882 if (!gfan_handle) {
1883 /* gfan without sfan means no fan control */
1884 /* all other models implement TP EC 0x2f control */
1885
1886 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02001887 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001888 fan_control_access_mode =
1889 IBMACPI_FAN_WR_ACPI_FANS;
1890 fan_control_commands |=
1891 IBMACPI_FAN_CMD_SPEED |
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02001892 IBMACPI_FAN_CMD_LEVEL |
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001893 IBMACPI_FAN_CMD_ENABLE;
1894 } else {
1895 fan_control_access_mode = IBMACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02001896 fan_control_commands |=
1897 IBMACPI_FAN_CMD_LEVEL |
1898 IBMACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001899 }
1900 }
1901 }
1902
1903 return 0;
1904}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001905
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02001906static int fan_get_status(u8 *status)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001907{
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02001908 u8 s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001909
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02001910 /* TODO:
1911 * Add IBMACPI_FAN_RD_ACPI_FANS ? */
1912
Henrique de Moraes Holschuh3ef8a602006-11-24 11:47:10 -02001913 switch (fan_status_access_mode) {
1914 case IBMACPI_FAN_RD_ACPI_GFAN:
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001915 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02001916
1917 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001918 return -EIO;
1919
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02001920 if (likely(status))
1921 *status = s & 0x07;
1922
1923 break;
1924
1925 case IBMACPI_FAN_RD_TPEC:
1926 /* all except 570, 600e/x, 770e, 770x */
1927 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
1928 return -EIO;
1929
1930 if (likely(status))
1931 *status = s;
1932
1933 break;
1934
1935 default:
1936 return -ENXIO;
1937 }
1938
1939 return 0;
1940}
1941
1942static int fan_get_speed(unsigned int *speed)
1943{
1944 u8 hi, lo;
1945
1946 switch (fan_status_access_mode) {
1947 case IBMACPI_FAN_RD_TPEC:
1948 /* all except 570, 600e/x, 770e, 770x */
1949 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
1950 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
1951 return -EIO;
1952
1953 if (likely(speed))
1954 *speed = (hi << 8) | lo;
1955
1956 break;
1957
1958 default:
1959 return -ENXIO;
1960 }
1961
1962 return 0;
1963}
1964
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02001965static void fan_exit(void)
1966{
1967 cancel_delayed_work(&fan_watchdog_task);
1968 flush_scheduled_work();
1969}
1970
1971static void fan_watchdog_reset(void)
1972{
1973 static int fan_watchdog_active = 0;
1974
1975 if (fan_watchdog_active)
1976 cancel_delayed_work(&fan_watchdog_task);
1977
1978 if (fan_watchdog_maxinterval > 0) {
1979 fan_watchdog_active = 1;
1980 if (!schedule_delayed_work(&fan_watchdog_task,
1981 msecs_to_jiffies(fan_watchdog_maxinterval
1982 * 1000))) {
1983 printk(IBM_ERR "failed to schedule the fan watchdog, "
1984 "watchdog will not trigger\n");
1985 }
1986 } else
1987 fan_watchdog_active = 0;
1988}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001989
1990static int fan_read(char *p)
1991{
1992 int len = 0;
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02001993 int rc;
1994 u8 status;
1995 unsigned int speed = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001996
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02001997 switch (fan_status_access_mode) {
1998 case IBMACPI_FAN_RD_ACPI_GFAN:
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001999 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02002000 if ((rc = fan_get_status(&status)) < 0)
2001 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002002
Henrique de Moraes Holschuhbab812a2006-11-24 11:47:12 -02002003 len += sprintf(p + len, "status:\t\t%s\n"
2004 "level:\t\t%d\n",
2005 (status != 0) ? "enabled" : "disabled", status);
Henrique de Moraes Holschuh3ef8a602006-11-24 11:47:10 -02002006 break;
2007
2008 case IBMACPI_FAN_RD_TPEC:
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002009 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02002010 if ((rc = fan_get_status(&status)) < 0)
2011 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002012
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002013 if (unlikely(!fan_control_status_known)) {
2014 if (status != fan_control_initial_status)
2015 fan_control_status_known = 1;
2016 else
2017 /* Return most likely status. In fact, it
2018 * might be the only possible status */
2019 status = IBMACPI_FAN_EC_AUTO;
2020 }
2021
Henrique de Moraes Holschuhbab812a2006-11-24 11:47:12 -02002022 len += sprintf(p + len, "status:\t\t%s\n",
2023 (status != 0) ? "enabled" : "disabled");
Henrique de Moraes Holschuh3ef8a602006-11-24 11:47:10 -02002024
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002025 /* No ThinkPad boots on disengaged mode, we can safely
2026 * assume the tachometer is online if fan control status
2027 * was unknown */
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02002028 if ((rc = fan_get_speed(&speed)) < 0)
2029 return rc;
2030
2031 len += sprintf(p + len, "speed:\t\t%d\n", speed);
Henrique de Moraes Holschuhbab812a2006-11-24 11:47:12 -02002032
2033 if (status & IBMACPI_FAN_EC_DISENGAGED)
2034 /* Disengaged mode takes precedence */
2035 len += sprintf(p + len, "level:\t\tdisengaged\n");
2036 else if (status & IBMACPI_FAN_EC_AUTO)
2037 len += sprintf(p + len, "level:\t\tauto\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002038 else
Henrique de Moraes Holschuhbab812a2006-11-24 11:47:12 -02002039 len += sprintf(p + len, "level:\t\t%d\n", status);
Henrique de Moraes Holschuh3ef8a602006-11-24 11:47:10 -02002040 break;
2041
2042 case IBMACPI_FAN_NONE:
2043 default:
2044 len += sprintf(p + len, "status:\t\tnot supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002045 }
2046
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02002047 if (fan_control_commands & IBMACPI_FAN_CMD_LEVEL) {
2048 len += sprintf(p + len, "commands:\tlevel <level>");
2049
2050 switch (fan_control_access_mode) {
2051 case IBMACPI_FAN_WR_ACPI_SFAN:
2052 len += sprintf(p + len, " (<level> is 0-7)\n");
2053 break;
2054
2055 default:
2056 len += sprintf(p + len, " (<level> is 0-7, "
2057 "auto, disengaged)\n");
2058 break;
2059 }
2060 }
Henrique de Moraes Holschuh3ef8a602006-11-24 11:47:10 -02002061
2062 if (fan_control_commands & IBMACPI_FAN_CMD_ENABLE)
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002063 len += sprintf(p + len, "commands:\tenable, disable\n"
2064 "commands:\twatchdog <timeout> (<timeout> is 0 (off), "
2065 "1-120 (seconds))\n");
Henrique de Moraes Holschuh3ef8a602006-11-24 11:47:10 -02002066
2067 if (fan_control_commands & IBMACPI_FAN_CMD_SPEED)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002068 len += sprintf(p + len, "commands:\tspeed <speed>"
2069 " (<speed> is 0-65535)\n");
2070
2071 return len;
2072}
2073
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002074static int fan_set_level(int level)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002075{
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002076 switch (fan_control_access_mode) {
2077 case IBMACPI_FAN_WR_ACPI_SFAN:
2078 if (level >= 0 && level <= 7) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002079 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
2080 return -EIO;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002081 } else
2082 return -EINVAL;
2083 break;
2084
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02002085 case IBMACPI_FAN_WR_ACPI_FANS:
2086 case IBMACPI_FAN_WR_TPEC:
2087 if ((level != IBMACPI_FAN_EC_AUTO) &&
2088 (level != IBMACPI_FAN_EC_DISENGAGED) &&
2089 ((level < 0) || (level > 7)))
2090 return -EINVAL;
2091
2092 if (!acpi_ec_write(fan_status_offset, level))
2093 return -EIO;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002094 else
2095 fan_control_status_known = 1;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02002096 break;
2097
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002098 default:
2099 return -ENXIO;
2100 }
2101 return 0;
2102}
2103
2104static int fan_set_enable(void)
2105{
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002106 u8 s;
2107 int rc;
2108
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002109 switch (fan_control_access_mode) {
2110 case IBMACPI_FAN_WR_ACPI_FANS:
2111 case IBMACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002112 if ((rc = fan_get_status(&s)) < 0)
2113 return rc;
2114
2115 /* Don't go out of emergency fan mode */
2116 if (s != 7)
2117 s = IBMACPI_FAN_EC_AUTO;
2118
2119 if (!acpi_ec_write(fan_status_offset, s))
2120 return -EIO;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002121 else
2122 fan_control_status_known = 1;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002123 break;
2124
2125 case IBMACPI_FAN_WR_ACPI_SFAN:
2126 if ((rc = fan_get_status(&s)) < 0)
2127 return rc;
2128
2129 s &= 0x07;
2130
2131 /* Set fan to at least level 4 */
2132 if (s < 4)
2133 s = 4;
2134
2135 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002136 return -EIO;
2137 break;
2138
2139 default:
2140 return -ENXIO;
2141 }
2142 return 0;
2143}
2144
2145static int fan_set_disable(void)
2146{
2147 switch (fan_control_access_mode) {
2148 case IBMACPI_FAN_WR_ACPI_FANS:
2149 case IBMACPI_FAN_WR_TPEC:
2150 if (!acpi_ec_write(fan_status_offset, 0x00))
2151 return -EIO;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002152 else
2153 fan_control_status_known = 1;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002154 break;
2155
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002156 case IBMACPI_FAN_WR_ACPI_SFAN:
2157 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
2158 return -EIO;
2159 break;
2160
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002161 default:
2162 return -ENXIO;
2163 }
2164 return 0;
2165}
2166
2167static int fan_set_speed(int speed)
2168{
2169 switch (fan_control_access_mode) {
2170 case IBMACPI_FAN_WR_ACPI_FANS:
2171 if (speed >= 0 && speed <= 65535) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002172 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
2173 speed, speed, speed))
2174 return -EIO;
2175 } else
2176 return -EINVAL;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002177 break;
2178
2179 default:
2180 return -ENXIO;
2181 }
2182 return 0;
2183}
2184
2185static int fan_write_cmd_level(const char *cmd, int *rc)
2186{
2187 int level;
2188
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02002189 if (strlencmp(cmd, "level auto") == 0)
2190 level = IBMACPI_FAN_EC_AUTO;
2191 else if (strlencmp(cmd, "level disengaged") == 0)
2192 level = IBMACPI_FAN_EC_DISENGAGED;
2193 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002194 return 0;
2195
2196 if ((*rc = fan_set_level(level)) == -ENXIO)
2197 printk(IBM_ERR "level command accepted for unsupported "
2198 "access mode %d", fan_control_access_mode);
2199
2200 return 1;
2201}
2202
2203static int fan_write_cmd_enable(const char *cmd, int *rc)
2204{
2205 if (strlencmp(cmd, "enable") != 0)
2206 return 0;
2207
2208 if ((*rc = fan_set_enable()) == -ENXIO)
2209 printk(IBM_ERR "enable command accepted for unsupported "
2210 "access mode %d", fan_control_access_mode);
2211
2212 return 1;
2213}
2214
2215static int fan_write_cmd_disable(const char *cmd, int *rc)
2216{
2217 if (strlencmp(cmd, "disable") != 0)
2218 return 0;
2219
2220 if ((*rc = fan_set_disable()) == -ENXIO)
2221 printk(IBM_ERR "disable command accepted for unsupported "
2222 "access mode %d", fan_control_access_mode);
2223
2224 return 1;
2225}
2226
2227static int fan_write_cmd_speed(const char *cmd, int *rc)
2228{
2229 int speed;
2230
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02002231 /* TODO:
2232 * Support speed <low> <medium> <high> ? */
2233
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002234 if (sscanf(cmd, "speed %d", &speed) != 1)
2235 return 0;
2236
2237 if ((*rc = fan_set_speed(speed)) == -ENXIO)
2238 printk(IBM_ERR "speed command accepted for unsupported "
2239 "access mode %d", fan_control_access_mode);
2240
2241 return 1;
2242}
2243
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002244static int fan_write_cmd_watchdog(const char *cmd, int *rc)
2245{
2246 int interval;
2247
2248 if (sscanf(cmd, "watchdog %d", &interval) != 1)
2249 return 0;
2250
2251 if (interval < 0 || interval > 120)
2252 *rc = -EINVAL;
2253 else
2254 fan_watchdog_maxinterval = interval;
2255
2256 return 1;
2257}
2258
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002259static int fan_write(char *buf)
2260{
2261 char *cmd;
2262 int rc = 0;
2263
2264 while (!rc && (cmd = next_cmd(&buf))) {
2265 if (!((fan_control_commands & IBMACPI_FAN_CMD_LEVEL) &&
2266 fan_write_cmd_level(cmd, &rc)) &&
2267 !((fan_control_commands & IBMACPI_FAN_CMD_ENABLE) &&
2268 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002269 fan_write_cmd_disable(cmd, &rc) ||
2270 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002271 !((fan_control_commands & IBMACPI_FAN_CMD_SPEED) &&
2272 fan_write_cmd_speed(cmd, &rc))
2273 )
2274 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002275 else if (!rc)
2276 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002277 }
2278
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002279 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002280}
2281
Len Brown25c68a32006-12-08 04:43:41 -05002282static void fan_watchdog_fire(struct work_struct *ignored)
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002283{
2284 printk(IBM_NOTICE "fan watchdog: enabling fan\n");
2285 if (fan_set_enable()) {
2286 printk(IBM_ERR "fan watchdog: error while enabling fan\n");
2287 /* reschedule for later */
2288 fan_watchdog_reset();
2289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290}
2291
2292static struct ibm_struct ibms[] = {
2293 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002294 .name = "driver",
Adrian Bunk1f217822006-12-19 13:01:28 -08002295 .init = ibm_acpi_driver_init,
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002296 .read = driver_read,
2297 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002299 .name = "hotkey",
2300 .hid = IBM_HKEY_HID,
2301 .init = hotkey_init,
2302 .read = hotkey_read,
2303 .write = hotkey_write,
2304 .exit = hotkey_exit,
2305 .notify = hotkey_notify,
2306 .handle = &hkey_handle,
2307 .type = ACPI_DEVICE_NOTIFY,
2308 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002310 .name = "bluetooth",
2311 .init = bluetooth_init,
2312 .read = bluetooth_read,
2313 .write = bluetooth_write,
2314 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 {
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002316 .name = "wan",
2317 .init = wan_init,
2318 .read = wan_read,
2319 .write = wan_write,
2320 .experimental = 1,
2321 },
2322 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002323 .name = "video",
2324 .init = video_init,
2325 .read = video_read,
2326 .write = video_write,
2327 .exit = video_exit,
2328 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002330 .name = "light",
2331 .init = light_init,
2332 .read = light_read,
2333 .write = light_write,
2334 },
Kristen Accardi63e5f242006-02-23 17:56:06 -08002335#ifdef CONFIG_ACPI_IBM_DOCK
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002337 .name = "dock",
2338 .read = dock_read,
2339 .write = dock_write,
2340 .notify = dock_notify,
2341 .handle = &dock_handle,
2342 .type = ACPI_SYSTEM_NOTIFY,
2343 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002345 .name = "dock",
2346 .hid = IBM_PCI_HID,
2347 .notify = dock_notify,
2348 .handle = &pci_handle,
2349 .type = ACPI_SYSTEM_NOTIFY,
2350 },
Kristen Accardi63e5f242006-02-23 17:56:06 -08002351#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002353 .name = "bay",
2354 .init = bay_init,
2355 .read = bay_read,
2356 .write = bay_write,
2357 .notify = bay_notify,
2358 .handle = &bay_handle,
2359 .type = ACPI_SYSTEM_NOTIFY,
2360 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002362 .name = "cmos",
2363 .read = cmos_read,
2364 .write = cmos_write,
2365 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002367 .name = "led",
2368 .init = led_init,
2369 .read = led_read,
2370 .write = led_write,
2371 },
2372 {
2373 .name = "beep",
2374 .read = beep_read,
2375 .write = beep_write,
2376 },
2377 {
2378 .name = "thermal",
2379 .init = thermal_init,
2380 .read = thermal_read,
2381 },
2382 {
2383 .name = "ecdump",
2384 .read = ecdump_read,
2385 .write = ecdump_write,
2386 .experimental = 1,
2387 },
2388 {
2389 .name = "brightness",
2390 .read = brightness_read,
2391 .write = brightness_write,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02002392 .init = brightness_init,
2393 .exit = brightness_exit,
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002394 },
2395 {
2396 .name = "volume",
2397 .read = volume_read,
2398 .write = volume_write,
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002399 },
2400 {
2401 .name = "fan",
2402 .read = fan_read,
2403 .write = fan_write,
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002404 .init = fan_init,
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002405 .exit = fan_exit,
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002406 .experimental = 1,
2407 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409
2410static int dispatch_read(char *page, char **start, off_t off, int count,
2411 int *eof, void *data)
2412{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02002413 struct ibm_struct *ibm = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 int len;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002415
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 if (!ibm || !ibm->read)
2417 return -EINVAL;
2418
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002419 len = ibm->read(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 if (len < 0)
2421 return len;
2422
2423 if (len <= off + count)
2424 *eof = 1;
2425 *start = page + off;
2426 len -= off;
2427 if (len > count)
2428 len = count;
2429 if (len < 0)
2430 len = 0;
2431
2432 return len;
2433}
2434
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002435static int dispatch_write(struct file *file, const char __user * userbuf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 unsigned long count, void *data)
2437{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02002438 struct ibm_struct *ibm = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 char *kernbuf;
2440 int ret;
2441
2442 if (!ibm || !ibm->write)
2443 return -EINVAL;
2444
2445 kernbuf = kmalloc(count + 2, GFP_KERNEL);
2446 if (!kernbuf)
2447 return -ENOMEM;
2448
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002449 if (copy_from_user(kernbuf, userbuf, count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 kfree(kernbuf);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002451 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 }
2453
2454 kernbuf[count] = 0;
2455 strcat(kernbuf, ",");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002456 ret = ibm->write(kernbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 if (ret == 0)
2458 ret = count;
2459
2460 kfree(kernbuf);
2461
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002462 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463}
2464
2465static void dispatch_notify(acpi_handle handle, u32 event, void *data)
2466{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02002467 struct ibm_struct *ibm = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468
2469 if (!ibm || !ibm->notify)
2470 return;
2471
2472 ibm->notify(ibm, event);
2473}
2474
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002475static int __init setup_notify(struct ibm_struct *ibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476{
2477 acpi_status status;
2478 int ret;
2479
2480 if (!*ibm->handle)
2481 return 0;
2482
2483 ret = acpi_bus_get_device(*ibm->handle, &ibm->device);
2484 if (ret < 0) {
2485 printk(IBM_ERR "%s device not present\n", ibm->name);
2486 return 0;
2487 }
2488
2489 acpi_driver_data(ibm->device) = ibm;
2490 sprintf(acpi_device_class(ibm->device), "%s/%s", IBM_NAME, ibm->name);
2491
2492 status = acpi_install_notify_handler(*ibm->handle, ibm->type,
2493 dispatch_notify, ibm);
2494 if (ACPI_FAILURE(status)) {
2495 printk(IBM_ERR "acpi_install_notify_handler(%s) failed: %d\n",
2496 ibm->name, status);
2497 return -ENODEV;
2498 }
Alexey Starikovskiy4afaf542006-12-18 14:53:33 -03002499 ibm->notify_installed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 return 0;
2501}
2502
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002503static int __init ibm_device_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504{
2505 return 0;
2506}
2507
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002508static int __init register_driver(struct ibm_struct *ibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509{
2510 int ret;
2511
Burman Yan36bcbec2006-12-19 12:56:11 -08002512 ibm->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 if (!ibm->driver) {
2514 printk(IBM_ERR "kmalloc(ibm->driver) failed\n");
2515 return -1;
2516 }
2517
Henrique de Moraes Holschuh3dfd35c2006-11-24 10:32:32 -02002518 sprintf(ibm->driver->name, "%s_%s", IBM_NAME, ibm->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 ibm->driver->ids = ibm->hid;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002520 ibm->driver->ops.add = &ibm_device_add;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521
2522 ret = acpi_bus_register_driver(ibm->driver);
2523 if (ret < 0) {
2524 printk(IBM_ERR "acpi_bus_register_driver(%s) failed: %d\n",
2525 ibm->hid, ret);
2526 kfree(ibm->driver);
2527 }
2528
2529 return ret;
2530}
2531
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002532static int __init ibm_init(struct ibm_struct *ibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533{
2534 int ret;
2535 struct proc_dir_entry *entry;
2536
2537 if (ibm->experimental && !experimental)
2538 return 0;
2539
2540 if (ibm->hid) {
2541 ret = register_driver(ibm);
2542 if (ret < 0)
2543 return ret;
2544 ibm->driver_registered = 1;
2545 }
2546
2547 if (ibm->init) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002548 ret = ibm->init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 if (ret != 0)
2550 return ret;
2551 ibm->init_called = 1;
2552 }
2553
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002554 if (ibm->read) {
2555 entry = create_proc_entry(ibm->name,
2556 S_IFREG | S_IRUGO | S_IWUSR,
2557 proc_dir);
2558 if (!entry) {
2559 printk(IBM_ERR "unable to create proc entry %s\n",
2560 ibm->name);
2561 return -ENODEV;
2562 }
2563 entry->owner = THIS_MODULE;
2564 entry->data = ibm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 entry->read_proc = &dispatch_read;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002566 if (ibm->write)
2567 entry->write_proc = &dispatch_write;
2568 ibm->proc_created = 1;
2569 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570
2571 if (ibm->notify) {
2572 ret = setup_notify(ibm);
2573 if (ret < 0)
2574 return ret;
2575 }
2576
2577 return 0;
2578}
2579
2580static void ibm_exit(struct ibm_struct *ibm)
2581{
2582 if (ibm->notify_installed)
2583 acpi_remove_notify_handler(*ibm->handle, ibm->type,
2584 dispatch_notify);
2585
2586 if (ibm->proc_created)
2587 remove_proc_entry(ibm->name, proc_dir);
2588
2589 if (ibm->init_called && ibm->exit)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002590 ibm->exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591
2592 if (ibm->driver_registered) {
2593 acpi_bus_unregister_driver(ibm->driver);
2594 kfree(ibm->driver);
2595 }
2596}
2597
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002598static void __init ibm_handle_init(char *name,
2599 acpi_handle * handle, acpi_handle parent,
2600 char **paths, int num_paths, char **path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601{
2602 int i;
2603 acpi_status status;
2604
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002605 for (i = 0; i < num_paths; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 status = acpi_get_handle(parent, paths[i], handle);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002607 if (ACPI_SUCCESS(status)) {
2608 *path = paths[i];
2609 return;
2610 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002612
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 *handle = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614}
2615
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002616#define IBM_HANDLE_INIT(object) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 ibm_handle_init(#object, &object##_handle, *object##_parent, \
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002618 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619
2620static int set_ibm_param(const char *val, struct kernel_param *kp)
2621{
2622 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002624 for (i = 0; i < ARRAY_SIZE(ibms); i++)
2625 if (strcmp(ibms[i].name, kp->name) == 0 && ibms[i].write) {
2626 if (strlen(val) > sizeof(ibms[i].param) - 2)
2627 return -ENOSPC;
2628 strcpy(ibms[i].param, val);
2629 strcat(ibms[i].param, ",");
2630 return 0;
2631 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 return -EINVAL;
2634}
2635
2636#define IBM_PARAM(feature) \
2637 module_param_call(feature, set_ibm_param, NULL, NULL, 0)
2638
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002639IBM_PARAM(hotkey);
2640IBM_PARAM(bluetooth);
2641IBM_PARAM(video);
2642IBM_PARAM(light);
Kristen Accardi63e5f242006-02-23 17:56:06 -08002643#ifdef CONFIG_ACPI_IBM_DOCK
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002644IBM_PARAM(dock);
Kristen Accardi63e5f242006-02-23 17:56:06 -08002645#endif
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002646IBM_PARAM(bay);
2647IBM_PARAM(cmos);
2648IBM_PARAM(led);
2649IBM_PARAM(beep);
2650IBM_PARAM(ecdump);
2651IBM_PARAM(brightness);
2652IBM_PARAM(volume);
2653IBM_PARAM(fan);
2654
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655static void acpi_ibm_exit(void)
2656{
2657 int i;
2658
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002659 for (i = ARRAY_SIZE(ibms) - 1; i >= 0; i--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 ibm_exit(&ibms[i]);
2661
2662 remove_proc_entry(IBM_DIR, acpi_root_dir);
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02002663
2664 if (ibm_thinkpad_ec_found)
2665 kfree(ibm_thinkpad_ec_found);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666}
2667
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02002668static char* __init check_dmi_for_ec(void)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002669{
2670 struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02002671 char ec_fw_string[18];
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002672
2673 /*
2674 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
2675 * X32 or newer, all Z series; Some models must have an
2676 * up-to-date BIOS or they will not be detected.
2677 *
2678 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
2679 */
2680 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02002681 if (sscanf(dev->name,
2682 "IBM ThinkPad Embedded Controller -[%17c",
2683 ec_fw_string) == 1) {
2684 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
2685 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
2686 return kstrdup(ec_fw_string, GFP_KERNEL);
2687 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002688 }
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02002689 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690}
2691
2692static int __init acpi_ibm_init(void)
2693{
2694 int ret, i;
2695
2696 if (acpi_disabled)
2697 return -ENODEV;
2698
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002699 if (!acpi_specific_hotkey_enabled) {
2700 printk(IBM_ERR "using generic hotkey driver\n");
2701 return -ENODEV;
Luming Yufb9802f2005-03-18 18:03:45 -05002702 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002704 /* ec is required because many other handles are relative to it */
2705 IBM_HANDLE_INIT(ec);
2706 if (!ec_handle) {
2707 printk(IBM_ERR "ec object not found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 return -ENODEV;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002709 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002711 /* Models with newer firmware report the EC in DMI */
2712 ibm_thinkpad_ec_found = check_dmi_for_ec();
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02002713 if (ibm_thinkpad_ec_found)
2714 printk(IBM_INFO "ThinkPad EC firmware %s\n",
2715 ibm_thinkpad_ec_found);
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002716
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 /* these handles are not required */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002718 IBM_HANDLE_INIT(vid);
2719 IBM_HANDLE_INIT(vid2);
2720 IBM_HANDLE_INIT(ledb);
2721 IBM_HANDLE_INIT(led);
2722 IBM_HANDLE_INIT(hkey);
2723 IBM_HANDLE_INIT(lght);
2724 IBM_HANDLE_INIT(cmos);
Kristen Accardi63e5f242006-02-23 17:56:06 -08002725#ifdef CONFIG_ACPI_IBM_DOCK
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002726 IBM_HANDLE_INIT(dock);
Kristen Accardi63e5f242006-02-23 17:56:06 -08002727#endif
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002728 IBM_HANDLE_INIT(pci);
2729 IBM_HANDLE_INIT(bay);
2730 if (bay_handle)
2731 IBM_HANDLE_INIT(bay_ej);
2732 IBM_HANDLE_INIT(bay2);
2733 if (bay2_handle)
2734 IBM_HANDLE_INIT(bay2_ej);
2735 IBM_HANDLE_INIT(beep);
2736 IBM_HANDLE_INIT(ecrd);
2737 IBM_HANDLE_INIT(ecwr);
2738 IBM_HANDLE_INIT(fans);
2739 IBM_HANDLE_INIT(gfan);
2740 IBM_HANDLE_INIT(sfan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741
2742 proc_dir = proc_mkdir(IBM_DIR, acpi_root_dir);
2743 if (!proc_dir) {
2744 printk(IBM_ERR "unable to create proc dir %s", IBM_DIR);
2745 return -ENODEV;
2746 }
2747 proc_dir->owner = THIS_MODULE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002748
2749 for (i = 0; i < ARRAY_SIZE(ibms); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 ret = ibm_init(&ibms[i]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002751 if (ret >= 0 && *ibms[i].param)
2752 ret = ibms[i].write(ibms[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 if (ret < 0) {
2754 acpi_ibm_exit();
2755 return ret;
2756 }
2757 }
2758
2759 return 0;
2760}
2761
2762module_init(acpi_ibm_init);
2763module_exit(acpi_ibm_exit);