blob: e2da95498f32ed96062371b1a49290c7efd425ec [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.
Henrique de Moraes Holschuh837ca6d2007-03-23 17:33:54 -030031 *
Borislav Deianov78f81cc2005-08-17 00:00:00 -040032 * 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
Henrique de Moraes Holschuh837ca6d2007-03-23 17:33:54 -030041 * 2005-01-16 0.10 fix module loading on R30, R31
Borislav Deianov78f81cc2005-08-17 00:00:00 -040042 * 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
Henrique de Moraes Holschuh1406cdd2007-03-23 17:33:56 -030081#include "ibm_acpi.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Henrique de Moraes Holschuhf9ff43a2006-11-25 16:37:38 -020083MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
Borislav Deianov78f81cc2005-08-17 00:00:00 -040084MODULE_DESCRIPTION(IBM_DESC);
85MODULE_VERSION(IBM_VERSION);
86MODULE_LICENSE("GPL");
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088#define __unused __attribute__ ((unused))
89
90static int experimental;
91module_param(experimental, int, 0);
92
93static acpi_handle root_handle = NULL;
94
95#define IBM_HANDLE(object, parent, paths...) \
96 static acpi_handle object##_handle; \
97 static acpi_handle *object##_parent = &parent##_handle; \
Borislav Deianov78f81cc2005-08-17 00:00:00 -040098 static char *object##_path; \
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 static char *object##_paths[] = { paths }
100
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400101IBM_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
102 "\\_SB.PCI.ISA.EC", /* 570 */
103 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
104 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
105 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
106 "\\_SB.PCI0.ICH3.EC0", /* R31 */
107 "\\_SB.PCI0.LPC.EC", /* all others */
108 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400110IBM_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
111 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
112 "\\_SB.PCI0.VID0", /* 770e */
113 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
114 "\\_SB.PCI0.AGP.VID", /* all others */
115 ); /* R30, R31 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400117IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400119IBM_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, T4x, X31, X40 */
120 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
121 "\\CMS", /* R40, R40e */
122 ); /* all others */
Kristen Accardi63e5f242006-02-23 17:56:06 -0800123#ifdef CONFIG_ACPI_IBM_DOCK
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400124IBM_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
125 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
126 "\\_SB.PCI0.PCI1.DOCK", /* all others */
127 "\\_SB.PCI.ISA.SLCE", /* 570 */
128 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
Kristen Accardi63e5f242006-02-23 17:56:06 -0800129#endif
Henrique de Moraes Holschuh2bc808a2007-02-21 13:05:38 -0200130#ifdef CONFIG_ACPI_IBM_BAY
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400131IBM_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
132 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuh837ca6d2007-03-23 17:33:54 -0300133 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400134 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
135 ); /* A21e, R30, R31 */
136
137IBM_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
138 "_EJ0", /* all others */
139 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
140
141IBM_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
142 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
143 ); /* all others */
144
145IBM_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
146 "_EJ0", /* 770x */
147 ); /* all others */
Henrique de Moraes Holschuh2bc808a2007-02-21 13:05:38 -0200148#endif /* CONFIG_ACPI_IBM_BAY */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400149
150/* don't list other alternatives as we install a notify handler on the 570 */
151IBM_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
152
153IBM_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
154 "^HKEY", /* R30, R31 */
155 "HKEY", /* all others */
156 ); /* 570 */
157
158IBM_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
159IBM_HANDLE(ledb, ec, "LEDB"); /* G4x */
160
161IBM_HANDLE(led, ec, "SLED", /* 570 */
162 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
163 "LED", /* all others */
164 ); /* R30, R31 */
165
166IBM_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
167IBM_HANDLE(ecrd, ec, "ECRD"); /* 570 */
168IBM_HANDLE(ecwr, ec, "ECWR"); /* 570 */
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200169IBM_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400170
171IBM_HANDLE(gfan, ec, "GFAN", /* 570 */
172 "\\FSPD", /* 600e/x, 770e, 770x */
173 ); /* all others */
174
175IBM_HANDLE(sfan, ec, "SFAN", /* 570 */
176 "JFNS", /* 770x-JL */
177 ); /* all others */
178
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200179/*
180 * FAN ACCESS MODES
181 *
182 * IBMACPI_FAN_RD_ACPI_GFAN:
183 * ACPI GFAN method: returns fan level
184 *
185 * see IBMACPI_FAN_WR_ACPI_SFAN
186 * EC 0x2f not available if GFAN exists
187 *
188 * IBMACPI_FAN_WR_ACPI_SFAN:
189 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
190 *
191 * EC 0x2f might be available *for reading*, but never for writing.
192 *
193 * IBMACPI_FAN_WR_TPEC:
194 * ThinkPad EC register 0x2f (HFSP): fan control loop mode Supported
195 * on almost all ThinkPads
196 *
197 * Fan speed changes of any sort (including those caused by the
198 * disengaged mode) are usually done slowly by the firmware as the
199 * maximum ammount of fan duty cycle change per second seems to be
200 * limited.
201 *
202 * Reading is not available if GFAN exists.
203 * Writing is not available if SFAN exists.
204 *
205 * Bits
206 * 7 automatic mode engaged;
207 * (default operation mode of the ThinkPad)
208 * fan level is ignored in this mode.
209 * 6 disengage mode (takes precedence over bit 7);
210 * not available on all thinkpads. May disable
211 * the tachometer, and speeds up fan to 100% duty-cycle,
212 * which speeds it up far above the standard RPM
213 * levels. It is not impossible that it could cause
214 * hardware damage.
215 * 5-3 unused in some models. Extra bits for fan level
216 * in others, but still useless as all values above
217 * 7 map to the same speed as level 7 in these models.
218 * 2-0 fan level (0..7 usually)
219 * 0x00 = stop
220 * 0x07 = max (set when temperatures critical)
221 * Some ThinkPads may have other levels, see
222 * IBMACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
223 *
224 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
225 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
226 * does so, its initial value is meaningless (0x07).
227 *
228 * For firmware bugs, refer to:
229 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
230 *
231 * ----
232 *
233 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
234 * Main fan tachometer reading (in RPM)
235 *
236 * This register is present on all ThinkPads with a new-style EC, and
237 * it is known not to be present on the A21m/e, and T22, as there is
238 * something else in offset 0x84 according to the ACPI DSDT. Other
239 * ThinkPads from this same time period (and earlier) probably lack the
240 * tachometer as well.
241 *
242 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
243 * was never fixed by IBM to report the EC firmware version string
244 * probably support the tachometer (like the early X models), so
245 * detecting it is quite hard. We need more data to know for sure.
246 *
247 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
248 * might result.
249 *
250 * FIRMWARE BUG: when EC 0x2f bit 6 is set (disengaged mode), this
251 * register is not invalidated in ThinkPads that disable tachometer
252 * readings. Thus, the tachometer readings go stale.
253 *
254 * For firmware bugs, refer to:
255 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
256 *
257 * IBMACPI_FAN_WR_ACPI_FANS:
258 * ThinkPad X31, X40, X41. Not available in the X60.
259 *
260 * FANS ACPI handle: takes three arguments: low speed, medium speed,
261 * high speed. ACPI DSDT seems to map these three speeds to levels
262 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
263 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
264 *
265 * The speeds are stored on handles
266 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
267 *
268 * There are three default speed sets, acessible as handles:
269 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
270 *
271 * ACPI DSDT switches which set is in use depending on various
272 * factors.
273 *
274 * IBMACPI_FAN_WR_TPEC is also available and should be used to
275 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
276 * but the ACPI tables just mention level 7.
277 */
278
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -0200279static char *ibm_thinkpad_ec_found = NULL;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -0200280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281static struct proc_dir_entry *proc_dir = NULL;
282
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -0200283static struct backlight_device *ibm_backlight_device = NULL;
Holger Macht8acb0252006-10-20 14:30:28 -0700284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285static int acpi_evalf(acpi_handle handle,
286 void *res, char *method, char *fmt, ...)
287{
288 char *fmt0 = fmt;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400289 struct acpi_object_list params;
290 union acpi_object in_objs[IBM_MAX_ACPI_ARGS];
291 struct acpi_buffer result, *resultp;
292 union acpi_object out_obj;
293 acpi_status status;
294 va_list ap;
295 char res_type;
296 int success;
297 int quiet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
299 if (!*fmt) {
300 printk(IBM_ERR "acpi_evalf() called with empty format\n");
301 return 0;
302 }
303
304 if (*fmt == 'q') {
305 quiet = 1;
306 fmt++;
307 } else
308 quiet = 0;
309
310 res_type = *(fmt++);
311
312 params.count = 0;
313 params.pointer = &in_objs[0];
314
315 va_start(ap, fmt);
316 while (*fmt) {
317 char c = *(fmt++);
318 switch (c) {
319 case 'd': /* int */
320 in_objs[params.count].integer.value = va_arg(ap, int);
321 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
322 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400323 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 default:
325 printk(IBM_ERR "acpi_evalf() called "
326 "with invalid format character '%c'\n", c);
327 return 0;
328 }
329 }
330 va_end(ap);
331
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400332 if (res_type != 'v') {
333 result.length = sizeof(out_obj);
334 result.pointer = &out_obj;
335 resultp = &result;
336 } else
337 resultp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400339 status = acpi_evaluate_object(handle, method, &params, resultp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341 switch (res_type) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400342 case 'd': /* int */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 if (res)
344 *(int *)res = out_obj.integer.value;
345 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
346 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400347 case 'v': /* void */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 success = status == AE_OK;
349 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400350 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 default:
352 printk(IBM_ERR "acpi_evalf() called "
353 "with invalid format character '%c'\n", res_type);
354 return 0;
355 }
356
357 if (!success && !quiet)
358 printk(IBM_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
359 method, fmt0, status);
360
361 return success;
362}
363
364static void __unused acpi_print_int(acpi_handle handle, char *method)
365{
366 int i;
367
368 if (acpi_evalf(handle, &i, method, "d"))
369 printk(IBM_INFO "%s = 0x%x\n", method, i);
370 else
371 printk(IBM_ERR "error calling %s\n", method);
372}
373
374static char *next_cmd(char **cmds)
375{
376 char *start = *cmds;
377 char *end;
378
379 while ((end = strchr(start, ',')) && end == start)
380 start = end + 1;
381
382 if (!end)
383 return NULL;
384
385 *end = 0;
386 *cmds = end + 1;
387 return start;
388}
389
Adrian Bunk1f217822006-12-19 13:01:28 -0800390static int ibm_acpi_driver_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
392 printk(IBM_INFO "%s v%s\n", IBM_DESC, IBM_VERSION);
393 printk(IBM_INFO "%s\n", IBM_URL);
394
Henrique de Moraes Holschuh3945ac32007-02-06 19:13:44 -0200395 if (ibm_thinkpad_ec_found)
396 printk(IBM_INFO "ThinkPad EC firmware %s\n",
397 ibm_thinkpad_ec_found);
398
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 return 0;
400}
401
Henrique de Moraes Holschuhe062e032007-03-23 17:33:55 -0300402static int ibm_acpi_driver_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
404 int len = 0;
405
406 len += sprintf(p + len, "driver:\t\t%s\n", IBM_DESC);
407 len += sprintf(p + len, "version:\t%s\n", IBM_VERSION);
408
409 return len;
410}
411
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400412static int hotkey_supported;
413static int hotkey_mask_supported;
414static int hotkey_orig_status;
415static int hotkey_orig_mask;
416
417static int hotkey_get(int *status, int *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418{
419 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400420 return 0;
421
422 if (hotkey_mask_supported)
423 if (!acpi_evalf(hkey_handle, mask, "DHKN", "d"))
424 return 0;
425
426 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427}
428
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400429static int hotkey_set(int status, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
431 int i;
432
433 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 return 0;
435
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400436 if (hotkey_mask_supported)
437 for (i = 0; i < 32; i++) {
438 int bit = ((1 << i) & mask) != 0;
439 if (!acpi_evalf(hkey_handle,
440 NULL, "MHKM", "vdd", i + 1, bit))
441 return 0;
442 }
443
444 return 1;
445}
446
447static int hotkey_init(void)
448{
449 /* hotkey not supported on 570 */
450 hotkey_supported = hkey_handle != NULL;
451
452 if (hotkey_supported) {
453 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
454 A30, R30, R31, T20-22, X20-21, X22-24 */
455 hotkey_mask_supported =
456 acpi_evalf(hkey_handle, NULL, "DHKN", "qv");
457
458 if (!hotkey_get(&hotkey_orig_status, &hotkey_orig_mask))
459 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 }
461
462 return 0;
463}
464
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400465static int hotkey_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
467 int status, mask;
468 int len = 0;
469
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400470 if (!hotkey_supported) {
471 len += sprintf(p + len, "status:\t\tnot supported\n");
472 return len;
473 }
474
475 if (!hotkey_get(&status, &mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 return -EIO;
477
478 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400479 if (hotkey_mask_supported) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 len += sprintf(p + len, "mask:\t\t0x%04x\n", mask);
481 len += sprintf(p + len,
482 "commands:\tenable, disable, reset, <mask>\n");
483 } else {
484 len += sprintf(p + len, "mask:\t\tnot supported\n");
485 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
486 }
487
488 return len;
489}
490
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400491static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
493 int status, mask;
494 char *cmd;
495 int do_cmd = 0;
496
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400497 if (!hotkey_supported)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 return -ENODEV;
499
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400500 if (!hotkey_get(&status, &mask))
501 return -EIO;
502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 while ((cmd = next_cmd(&buf))) {
504 if (strlencmp(cmd, "enable") == 0) {
505 status = 1;
506 } else if (strlencmp(cmd, "disable") == 0) {
507 status = 0;
508 } else if (strlencmp(cmd, "reset") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400509 status = hotkey_orig_status;
510 mask = hotkey_orig_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
512 /* mask set */
513 } else if (sscanf(cmd, "%x", &mask) == 1) {
514 /* mask set */
515 } else
516 return -EINVAL;
517 do_cmd = 1;
518 }
519
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400520 if (do_cmd && !hotkey_set(status, mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 return -EIO;
522
523 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400524}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400526static void hotkey_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400528 if (hotkey_supported)
529 hotkey_set(hotkey_orig_status, hotkey_orig_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530}
531
532static void hotkey_notify(struct ibm_struct *ibm, u32 event)
533{
534 int hkey;
535
536 if (acpi_evalf(hkey_handle, &hkey, "MHKP", "d"))
537 acpi_bus_generate_event(ibm->device, event, hkey);
538 else {
539 printk(IBM_ERR "unknown hotkey event %d\n", event);
540 acpi_bus_generate_event(ibm->device, event, 0);
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400541 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542}
543
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400544static int bluetooth_supported;
545
546static int bluetooth_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400548 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
549 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
550 bluetooth_supported = hkey_handle &&
551 acpi_evalf(hkey_handle, NULL, "GBDC", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
553 return 0;
554}
555
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400556static int bluetooth_status(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
558 int status;
559
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400560 if (!bluetooth_supported ||
561 !acpi_evalf(hkey_handle, &status, "GBDC", "d"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 status = 0;
563
564 return status;
565}
566
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400567static int bluetooth_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
569 int len = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400570 int status = bluetooth_status();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400572 if (!bluetooth_supported)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 len += sprintf(p + len, "status:\t\tnot supported\n");
574 else if (!(status & 1))
575 len += sprintf(p + len, "status:\t\tnot installed\n");
576 else {
577 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 1));
578 len += sprintf(p + len, "commands:\tenable, disable\n");
579 }
580
581 return len;
582}
583
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400584static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400586 int status = bluetooth_status();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 char *cmd;
588 int do_cmd = 0;
589
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400590 if (!bluetooth_supported)
591 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
593 while ((cmd = next_cmd(&buf))) {
594 if (strlencmp(cmd, "enable") == 0) {
595 status |= 2;
596 } else if (strlencmp(cmd, "disable") == 0) {
597 status &= ~2;
598 } else
599 return -EINVAL;
600 do_cmd = 1;
601 }
602
603 if (do_cmd && !acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400604 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
606 return 0;
607}
608
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -0400609static int wan_supported;
610
611static int wan_init(void)
612{
613 wan_supported = hkey_handle &&
614 acpi_evalf(hkey_handle, NULL, "GWAN", "qv");
615
616 return 0;
617}
618
619static int wan_status(void)
620{
621 int status;
622
Henrique de Moraes Holschuh8d297262006-11-24 11:47:07 -0200623 if (!wan_supported || !acpi_evalf(hkey_handle, &status, "GWAN", "d"))
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -0400624 status = 0;
625
626 return status;
627}
628
629static int wan_read(char *p)
630{
631 int len = 0;
632 int status = wan_status();
633
634 if (!wan_supported)
635 len += sprintf(p + len, "status:\t\tnot supported\n");
636 else if (!(status & 1))
637 len += sprintf(p + len, "status:\t\tnot installed\n");
638 else {
639 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 1));
640 len += sprintf(p + len, "commands:\tenable, disable\n");
641 }
642
643 return len;
644}
645
646static int wan_write(char *buf)
647{
648 int status = wan_status();
649 char *cmd;
650 int do_cmd = 0;
651
652 if (!wan_supported)
653 return -ENODEV;
654
655 while ((cmd = next_cmd(&buf))) {
656 if (strlencmp(cmd, "enable") == 0) {
657 status |= 2;
658 } else if (strlencmp(cmd, "disable") == 0) {
659 status &= ~2;
660 } else
661 return -EINVAL;
662 do_cmd = 1;
663 }
664
665 if (do_cmd && !acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
666 return -EIO;
667
668 return 0;
669}
670
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200671static enum video_access_mode video_supported;
672static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400673
674static int video_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400676 int ivga;
677
678 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
679 /* G41, assume IVGA doesn't change */
680 vid_handle = vid2_handle;
681
682 if (!vid_handle)
683 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200684 video_supported = IBMACPI_VIDEO_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400685 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
686 /* 570 */
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200687 video_supported = IBMACPI_VIDEO_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400688 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
689 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200690 video_supported = IBMACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400691 else
692 /* all others */
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200693 video_supported = IBMACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695 return 0;
696}
697
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400698static int video_status(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{
700 int status = 0;
701 int i;
702
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200703 if (video_supported == IBMACPI_VIDEO_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400704 if (acpi_evalf(NULL, &i, "\\_SB.PHS", "dd", 0x87))
705 status = i & 3;
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200706 } else if (video_supported == IBMACPI_VIDEO_770) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400707 if (acpi_evalf(NULL, &i, "\\VCDL", "d"))
708 status |= 0x01 * i;
709 if (acpi_evalf(NULL, &i, "\\VCDC", "d"))
710 status |= 0x02 * i;
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200711 } else if (video_supported == IBMACPI_VIDEO_NEW) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400712 acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1);
713 if (acpi_evalf(NULL, &i, "\\VCDC", "d"))
714 status |= 0x02 * i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400716 acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0);
717 if (acpi_evalf(NULL, &i, "\\VCDL", "d"))
718 status |= 0x01 * i;
719 if (acpi_evalf(NULL, &i, "\\VCDD", "d"))
720 status |= 0x08 * i;
721 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
723 return status;
724}
725
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400726static int video_autosw(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400728 int autosw = 0;
729
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200730 if (video_supported == IBMACPI_VIDEO_570)
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400731 acpi_evalf(vid_handle, &autosw, "SWIT", "d");
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200732 else if (video_supported == IBMACPI_VIDEO_770 ||
733 video_supported == IBMACPI_VIDEO_NEW)
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400734 acpi_evalf(vid_handle, &autosw, "^VDEE", "d");
735
736 return autosw & 1;
737}
738
739static int video_read(char *p)
740{
741 int status = video_status();
742 int autosw = video_autosw();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 int len = 0;
744
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400745 if (!video_supported) {
746 len += sprintf(p + len, "status:\t\tnot supported\n");
747 return len;
748 }
749
750 len += sprintf(p + len, "status:\t\tsupported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
752 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200753 if (video_supported == IBMACPI_VIDEO_NEW)
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400754 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
755 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
756 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
757 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200758 if (video_supported == IBMACPI_VIDEO_NEW)
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400759 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
760 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
762
763 return len;
764}
765
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400766static int video_switch(void)
767{
768 int autosw = video_autosw();
769 int ret;
770
771 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
772 return -EIO;
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200773 ret = video_supported == IBMACPI_VIDEO_570 ?
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400774 acpi_evalf(ec_handle, NULL, "_Q16", "v") :
775 acpi_evalf(vid_handle, NULL, "VSWT", "v");
776 acpi_evalf(vid_handle, NULL, "_DOS", "vd", autosw);
777
778 return ret;
779}
780
781static int video_expand(void)
782{
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200783 if (video_supported == IBMACPI_VIDEO_570)
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400784 return acpi_evalf(ec_handle, NULL, "_Q17", "v");
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200785 else if (video_supported == IBMACPI_VIDEO_770)
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400786 return acpi_evalf(vid_handle, NULL, "VEXP", "v");
787 else
788 return acpi_evalf(NULL, NULL, "\\VEXP", "v");
789}
790
791static int video_switch2(int status)
792{
793 int ret;
794
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200795 if (video_supported == IBMACPI_VIDEO_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400796 ret = acpi_evalf(NULL, NULL,
797 "\\_SB.PHS2", "vdd", 0x8b, status | 0x80);
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200798 } else if (video_supported == IBMACPI_VIDEO_770) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400799 int autosw = video_autosw();
800 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
801 return -EIO;
802
803 ret = acpi_evalf(vid_handle, NULL,
804 "ASWT", "vdd", status * 0x100, 0);
805
806 acpi_evalf(vid_handle, NULL, "_DOS", "vd", autosw);
807 } else {
808 ret = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
809 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
810 }
811
812 return ret;
813}
814
815static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816{
817 char *cmd;
818 int enable, disable, status;
819
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400820 if (!video_supported)
821 return -ENODEV;
822
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 enable = disable = 0;
824
825 while ((cmd = next_cmd(&buf))) {
826 if (strlencmp(cmd, "lcd_enable") == 0) {
827 enable |= 0x01;
828 } else if (strlencmp(cmd, "lcd_disable") == 0) {
829 disable |= 0x01;
830 } else if (strlencmp(cmd, "crt_enable") == 0) {
831 enable |= 0x02;
832 } else if (strlencmp(cmd, "crt_disable") == 0) {
833 disable |= 0x02;
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200834 } else if (video_supported == IBMACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400835 strlencmp(cmd, "dvi_enable") == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 enable |= 0x08;
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -0200837 } else if (video_supported == IBMACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400838 strlencmp(cmd, "dvi_disable") == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 disable |= 0x08;
840 } else if (strlencmp(cmd, "auto_enable") == 0) {
841 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
842 return -EIO;
843 } else if (strlencmp(cmd, "auto_disable") == 0) {
844 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 0))
845 return -EIO;
846 } else if (strlencmp(cmd, "video_switch") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400847 if (!video_switch())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 return -EIO;
849 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400850 if (!video_expand())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 return -EIO;
852 } else
853 return -EINVAL;
854 }
855
856 if (enable || disable) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400857 status = (video_status() & 0x0f & ~disable) | enable;
858 if (!video_switch2(status))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 return -EIO;
860 }
861
862 return 0;
863}
864
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400865static void video_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400867 acpi_evalf(vid_handle, NULL, "_DOS", "vd", video_orig_autosw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868}
869
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400870static int light_supported;
871static int light_status_supported;
872
873static int light_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874{
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400875 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
876 light_supported = (cmos_handle || lght_handle) && !ledb_handle;
877
878 if (light_supported)
879 /* light status not supported on
880 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
881 light_status_supported = acpi_evalf(ec_handle, NULL,
882 "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
884 return 0;
885}
886
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400887static int light_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888{
889 int len = 0;
890 int status = 0;
891
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400892 if (!light_supported) {
893 len += sprintf(p + len, "status:\t\tnot supported\n");
894 } else if (!light_status_supported) {
895 len += sprintf(p + len, "status:\t\tunknown\n");
896 len += sprintf(p + len, "commands:\ton, off\n");
897 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
899 return -EIO;
900 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400901 len += sprintf(p + len, "commands:\ton, off\n");
902 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
904 return len;
905}
906
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400907static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908{
909 int cmos_cmd, lght_cmd;
910 char *cmd;
911 int success;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400912
913 if (!light_supported)
914 return -ENODEV;
915
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 while ((cmd = next_cmd(&buf))) {
917 if (strlencmp(cmd, "on") == 0) {
918 cmos_cmd = 0x0c;
919 lght_cmd = 1;
920 } else if (strlencmp(cmd, "off") == 0) {
921 cmos_cmd = 0x0d;
922 lght_cmd = 0;
923 } else
924 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400925
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 success = cmos_handle ?
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400927 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
928 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 if (!success)
930 return -EIO;
931 }
932
933 return 0;
934}
935
Henrique de Moraes Holschuh2bc808a2007-02-21 13:05:38 -0200936#if defined(CONFIG_ACPI_IBM_DOCK) || defined(CONFIG_ACPI_IBM_BAY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937static int _sta(acpi_handle handle)
938{
939 int status;
940
941 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
942 status = 0;
943
944 return status;
945}
Henrique de Moraes Holschuh2bc808a2007-02-21 13:05:38 -0200946#endif
Henrique de Moraes Holschuhd2fadbb2007-01-11 02:58:15 -0500947
Kristen Accardi63e5f242006-02-23 17:56:06 -0800948#ifdef CONFIG_ACPI_IBM_DOCK
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949#define dock_docked() (_sta(dock_handle) & 1)
950
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400951static int dock_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952{
953 int len = 0;
954 int docked = dock_docked();
955
956 if (!dock_handle)
957 len += sprintf(p + len, "status:\t\tnot supported\n");
958 else if (!docked)
959 len += sprintf(p + len, "status:\t\tundocked\n");
960 else {
961 len += sprintf(p + len, "status:\t\tdocked\n");
962 len += sprintf(p + len, "commands:\tdock, undock\n");
963 }
964
965 return len;
966}
967
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400968static int dock_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969{
970 char *cmd;
971
972 if (!dock_docked())
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400973 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
975 while ((cmd = next_cmd(&buf))) {
976 if (strlencmp(cmd, "undock") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400977 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
978 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 return -EIO;
980 } else if (strlencmp(cmd, "dock") == 0) {
981 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
982 return -EIO;
983 } else
984 return -EINVAL;
985 }
986
987 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400988}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
990static void dock_notify(struct ibm_struct *ibm, u32 event)
991{
992 int docked = dock_docked();
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400993 int pci = ibm->hid && strstr(ibm->hid, IBM_PCI_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400995 if (event == 1 && !pci) /* 570 */
996 acpi_bus_generate_event(ibm->device, event, 1); /* button */
997 else if (event == 1 && pci) /* 570 */
998 acpi_bus_generate_event(ibm->device, event, 3); /* dock */
999 else if (event == 3 && docked)
1000 acpi_bus_generate_event(ibm->device, event, 1); /* button */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 else if (event == 3 && !docked)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001002 acpi_bus_generate_event(ibm->device, event, 2); /* undock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 else if (event == 0 && docked)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001004 acpi_bus_generate_event(ibm->device, event, 3); /* dock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 else {
1006 printk(IBM_ERR "unknown dock event %d, status %d\n",
1007 event, _sta(dock_handle));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001008 acpi_bus_generate_event(ibm->device, event, 0); /* unknown */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 }
1010}
Kristen Accardi63e5f242006-02-23 17:56:06 -08001011#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
Henrique de Moraes Holschuh2bc808a2007-02-21 13:05:38 -02001013#ifdef CONFIG_ACPI_IBM_BAY
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001014static int bay_status_supported;
1015static int bay_status2_supported;
1016static int bay_eject_supported;
1017static int bay_eject2_supported;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001019static int bay_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001021 bay_status_supported = bay_handle &&
1022 acpi_evalf(bay_handle, NULL, "_STA", "qv");
1023 bay_status2_supported = bay2_handle &&
1024 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
1025
1026 bay_eject_supported = bay_handle && bay_ej_handle &&
1027 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
1028 bay_eject2_supported = bay2_handle && bay2_ej_handle &&
1029 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
1031 return 0;
1032}
1033
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001034#define bay_occupied(b) (_sta(b##_handle) & 1)
1035
1036static int bay_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037{
1038 int len = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001039 int occupied = bay_occupied(bay);
1040 int occupied2 = bay_occupied(bay2);
1041 int eject, eject2;
1042
1043 len += sprintf(p + len, "status:\t\t%s\n", bay_status_supported ?
1044 (occupied ? "occupied" : "unoccupied") :
1045 "not supported");
1046 if (bay_status2_supported)
1047 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
1048 "occupied" : "unoccupied");
1049
1050 eject = bay_eject_supported && occupied;
1051 eject2 = bay_eject2_supported && occupied2;
1052
1053 if (eject && eject2)
1054 len += sprintf(p + len, "commands:\teject, eject2\n");
1055 else if (eject)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 len += sprintf(p + len, "commands:\teject\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001057 else if (eject2)
1058 len += sprintf(p + len, "commands:\teject2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
1060 return len;
1061}
1062
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001063static int bay_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064{
1065 char *cmd;
1066
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001067 if (!bay_eject_supported && !bay_eject2_supported)
1068 return -ENODEV;
1069
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001071 if (bay_eject_supported && strlencmp(cmd, "eject") == 0) {
1072 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
1073 return -EIO;
1074 } else if (bay_eject2_supported &&
1075 strlencmp(cmd, "eject2") == 0) {
1076 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 return -EIO;
1078 } else
1079 return -EINVAL;
1080 }
1081
1082 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001083}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
1085static void bay_notify(struct ibm_struct *ibm, u32 event)
1086{
1087 acpi_bus_generate_event(ibm->device, event, 0);
1088}
Henrique de Moraes Holschuh2bc808a2007-02-21 13:05:38 -02001089#endif /* CONFIG_ACPI_IBM_BAY */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001091static int cmos_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092{
1093 int len = 0;
1094
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001095 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1096 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 if (!cmos_handle)
1098 len += sprintf(p + len, "status:\t\tnot supported\n");
1099 else {
1100 len += sprintf(p + len, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001101 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 }
1103
1104 return len;
1105}
1106
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001107static int cmos_eval(int cmos_cmd)
1108{
1109 if (cmos_handle)
1110 return acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd);
1111 else
1112 return 1;
1113}
1114
1115static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116{
1117 char *cmd;
1118 int cmos_cmd;
1119
1120 if (!cmos_handle)
1121 return -EINVAL;
1122
1123 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001124 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
1125 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 /* cmos_cmd set */
1127 } else
1128 return -EINVAL;
1129
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001130 if (!cmos_eval(cmos_cmd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 return -EIO;
1132 }
1133
1134 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001135}
1136
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02001137static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001138
1139static int led_init(void)
1140{
1141 if (!led_handle)
1142 /* led not supported on R30, R31 */
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02001143 led_supported = IBMACPI_LED_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001144 else if (strlencmp(led_path, "SLED") == 0)
1145 /* 570 */
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02001146 led_supported = IBMACPI_LED_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001147 else if (strlencmp(led_path, "SYSL") == 0)
1148 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02001149 led_supported = IBMACPI_LED_OLD;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001150 else
1151 /* all others */
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02001152 led_supported = IBMACPI_LED_NEW;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001153
1154 return 0;
1155}
1156
1157#define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
1158
1159static int led_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160{
1161 int len = 0;
1162
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001163 if (!led_supported) {
1164 len += sprintf(p + len, "status:\t\tnot supported\n");
1165 return len;
1166 }
1167 len += sprintf(p + len, "status:\t\tsupported\n");
1168
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02001169 if (led_supported == IBMACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001170 /* 570 */
1171 int i, status;
1172 for (i = 0; i < 8; i++) {
1173 if (!acpi_evalf(ec_handle,
1174 &status, "GLED", "dd", 1 << i))
1175 return -EIO;
1176 len += sprintf(p + len, "%d:\t\t%s\n",
1177 i, led_status(status));
1178 }
1179 }
1180
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 len += sprintf(p + len, "commands:\t"
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001182 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
1184 return len;
1185}
1186
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001187/* off, on, blink */
1188static const int led_sled_arg1[] = { 0, 1, 3 };
1189static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
1190static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
1191static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
1192
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001193static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194{
1195 char *cmd;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001196 int led, ind, ret;
1197
1198 if (!led_supported)
1199 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
1201 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001202 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 return -EINVAL;
1204
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001205 if (strstr(cmd, "off")) {
1206 ind = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 } else if (strstr(cmd, "on")) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001208 ind = 1;
1209 } else if (strstr(cmd, "blink")) {
1210 ind = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 } else
1212 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001213
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02001214 if (led_supported == IBMACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001215 /* 570 */
1216 led = 1 << led;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001218 led, led_sled_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 return -EIO;
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02001220 } else if (led_supported == IBMACPI_LED_OLD) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001221 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
1222 led = 1 << led;
Henrique de Moraes Holschuhe062e032007-03-23 17:33:55 -03001223 ret = ec_write(IBMACPI_LED_EC_HLMS, led);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001224 if (ret >= 0)
1225 ret =
Henrique de Moraes Holschuhe062e032007-03-23 17:33:55 -03001226 ec_write(IBMACPI_LED_EC_HLBL,
1227 led * led_exp_hlbl[ind]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001228 if (ret >= 0)
1229 ret =
Henrique de Moraes Holschuhe062e032007-03-23 17:33:55 -03001230 ec_write(IBMACPI_LED_EC_HLCL,
1231 led * led_exp_hlcl[ind]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001232 if (ret < 0)
1233 return ret;
1234 } else {
1235 /* all others */
1236 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
1237 led, led_led_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 }
1241
1242 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001243}
1244
1245static int beep_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246{
1247 int len = 0;
1248
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001249 if (!beep_handle)
1250 len += sprintf(p + len, "status:\t\tnot supported\n");
1251 else {
1252 len += sprintf(p + len, "status:\t\tsupported\n");
1253 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
1254 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
1256 return len;
1257}
1258
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001259static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260{
1261 char *cmd;
1262 int beep_cmd;
1263
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001264 if (!beep_handle)
1265 return -ENODEV;
1266
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001268 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
1269 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 /* beep_cmd set */
1271 } else
1272 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001273 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 return -EIO;
1275 }
1276
1277 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001278}
1279
1280static int acpi_ec_read(int i, u8 * p)
1281{
1282 int v;
1283
1284 if (ecrd_handle) {
1285 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
1286 return 0;
1287 *p = v;
1288 } else {
1289 if (ec_read(i, p) < 0)
1290 return 0;
1291 }
1292
1293 return 1;
1294}
1295
1296static int acpi_ec_write(int i, u8 v)
1297{
1298 if (ecwr_handle) {
1299 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
1300 return 0;
1301 } else {
1302 if (ec_write(i, v) < 0)
1303 return 0;
1304 }
1305
1306 return 1;
1307}
1308
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001309static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001310
1311static int thermal_init(void)
1312{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001313 u8 t, ta1, ta2;
1314 int i;
1315 int acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001316
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001317 if (ibm_thinkpad_ec_found && experimental) {
1318 /*
1319 * Direct EC access mode: sensors at registers
1320 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
1321 * non-implemented, thermal sensors return 0x80 when
1322 * not available
1323 */
1324
1325 ta1 = ta2 = 0;
1326 for (i = 0; i < 8; i++) {
1327 if (likely(acpi_ec_read(0x78 + i, &t))) {
1328 ta1 |= t;
1329 } else {
1330 ta1 = 0;
1331 break;
1332 }
1333 if (likely(acpi_ec_read(0xC0 + i, &t))) {
1334 ta2 |= t;
1335 } else {
1336 ta1 = 0;
1337 break;
1338 }
1339 }
1340 if (ta1 == 0) {
1341 /* This is sheer paranoia, but we handle it anyway */
1342 if (acpi_tmp7) {
1343 printk(IBM_ERR
1344 "ThinkPad ACPI EC access misbehaving, "
1345 "falling back to ACPI TMPx access mode\n");
1346 thermal_read_mode = IBMACPI_THERMAL_ACPI_TMP07;
1347 } else {
1348 printk(IBM_ERR
1349 "ThinkPad ACPI EC access misbehaving, "
1350 "disabling thermal sensors access\n");
1351 thermal_read_mode = IBMACPI_THERMAL_NONE;
1352 }
1353 } else {
1354 thermal_read_mode =
1355 (ta2 != 0) ?
1356 IBMACPI_THERMAL_TPEC_16 : IBMACPI_THERMAL_TPEC_8;
1357 }
1358 } else if (acpi_tmp7) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001359 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
1360 /* 600e/x, 770e, 770x */
1361 thermal_read_mode = IBMACPI_THERMAL_ACPI_UPDT;
1362 } else {
1363 /* Standard ACPI TMPx access, max 8 sensors */
1364 thermal_read_mode = IBMACPI_THERMAL_ACPI_TMP07;
1365 }
1366 } else {
1367 /* temperatures not supported on 570, G4x, R30, R31, R32 */
1368 thermal_read_mode = IBMACPI_THERMAL_NONE;
1369 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001370
1371 return 0;
1372}
1373
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001374static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
1375{
1376 int i, t;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001377 s8 tmp;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001378 char tmpi[] = "TMPi";
1379
1380 if (!s)
1381 return -EINVAL;
1382
1383 switch (thermal_read_mode) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02001384#if IBMACPI_MAX_THERMAL_SENSORS >= 16
1385 case IBMACPI_THERMAL_TPEC_16:
1386 for (i = 0; i < 8; i++) {
1387 if (!acpi_ec_read(0xC0 + i, &tmp))
1388 return -EIO;
1389 s->temp[i + 8] = tmp * 1000;
1390 }
1391 /* fallthrough */
1392#endif
1393 case IBMACPI_THERMAL_TPEC_8:
1394 for (i = 0; i < 8; i++) {
1395 if (!acpi_ec_read(0x78 + i, &tmp))
1396 return -EIO;
1397 s->temp[i] = tmp * 1000;
1398 }
1399 return (thermal_read_mode == IBMACPI_THERMAL_TPEC_16) ? 16 : 8;
1400
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001401 case IBMACPI_THERMAL_ACPI_UPDT:
1402 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
1403 return -EIO;
1404 for (i = 0; i < 8; i++) {
1405 tmpi[3] = '0' + i;
1406 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
1407 return -EIO;
1408 s->temp[i] = (t - 2732) * 100;
1409 }
1410 return 8;
1411
1412 case IBMACPI_THERMAL_ACPI_TMP07:
1413 for (i = 0; i < 8; i++) {
1414 tmpi[3] = '0' + i;
1415 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
1416 return -EIO;
1417 s->temp[i] = t * 1000;
1418 }
1419 return 8;
1420
1421 case IBMACPI_THERMAL_NONE:
1422 default:
1423 return 0;
1424 }
1425}
1426
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001427static int thermal_read(char *p)
1428{
1429 int len = 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001430 int n, i;
1431 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001432
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001433 n = thermal_get_sensors(&t);
1434 if (unlikely(n < 0))
1435 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001436
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001437 len += sprintf(p + len, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001438
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02001439 if (n > 0) {
1440 for (i = 0; i < (n - 1); i++)
1441 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
1442 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
1443 } else
1444 len += sprintf(p + len, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001445
1446 return len;
1447}
1448
1449static u8 ecdump_regs[256];
1450
1451static int ecdump_read(char *p)
1452{
1453 int len = 0;
1454 int i, j;
1455 u8 v;
1456
1457 len += sprintf(p + len, "EC "
1458 " +00 +01 +02 +03 +04 +05 +06 +07"
1459 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
1460 for (i = 0; i < 256; i += 16) {
1461 len += sprintf(p + len, "EC 0x%02x:", i);
1462 for (j = 0; j < 16; j++) {
1463 if (!acpi_ec_read(i + j, &v))
1464 break;
1465 if (v != ecdump_regs[i + j])
1466 len += sprintf(p + len, " *%02x", v);
1467 else
1468 len += sprintf(p + len, " %02x", v);
1469 ecdump_regs[i + j] = v;
1470 }
1471 len += sprintf(p + len, "\n");
1472 if (j != 16)
1473 break;
1474 }
1475
1476 /* These are way too dangerous to advertise openly... */
1477#if 0
1478 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
1479 " (<offset> is 00-ff, <value> is 00-ff)\n");
1480 len += sprintf(p + len, "commands:\t0x<offset> <value> "
1481 " (<offset> is 00-ff, <value> is 0-255)\n");
1482#endif
1483 return len;
1484}
1485
1486static int ecdump_write(char *buf)
1487{
1488 char *cmd;
1489 int i, v;
1490
1491 while ((cmd = next_cmd(&buf))) {
1492 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
1493 /* i and v set */
1494 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
1495 /* i and v set */
1496 } else
1497 return -EINVAL;
1498 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
1499 if (!acpi_ec_write(i, v))
1500 return -EIO;
1501 } else
1502 return -EINVAL;
1503 }
1504
1505 return 0;
1506}
1507
Holger Macht8acb0252006-10-20 14:30:28 -07001508static int brightness_get(struct backlight_device *bd)
1509{
Henrique de Moraes Holschuh8d297262006-11-24 11:47:07 -02001510 u8 level;
1511 if (!acpi_ec_read(brightness_offset, &level))
1512 return -EIO;
Holger Macht8acb0252006-10-20 14:30:28 -07001513
Henrique de Moraes Holschuh8d297262006-11-24 11:47:07 -02001514 level &= 0x7;
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02001515
Henrique de Moraes Holschuh8d297262006-11-24 11:47:07 -02001516 return level;
Holger Macht8acb0252006-10-20 14:30:28 -07001517}
1518
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001519static int brightness_read(char *p)
1520{
1521 int len = 0;
Holger Macht8acb0252006-10-20 14:30:28 -07001522 int level;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001523
Holger Macht8acb0252006-10-20 14:30:28 -07001524 if ((level = brightness_get(NULL)) < 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001525 len += sprintf(p + len, "level:\t\tunreadable\n");
1526 } else {
1527 len += sprintf(p + len, "level:\t\t%d\n", level & 0x7);
1528 len += sprintf(p + len, "commands:\tup, down\n");
1529 len += sprintf(p + len, "commands:\tlevel <level>"
1530 " (<level> is 0-7)\n");
1531 }
1532
1533 return len;
1534}
1535
Holger Macht8acb0252006-10-20 14:30:28 -07001536static int brightness_set(int value)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001537{
1538 int cmos_cmd, inc, i;
Holger Macht8acb0252006-10-20 14:30:28 -07001539 int current_value = brightness_get(NULL);
1540
1541 value &= 7;
1542
Henrique de Moraes Holschuhe062e032007-03-23 17:33:55 -03001543 cmos_cmd = value > current_value ?
1544 TP_CMOS_BRIGHTNESS_UP : TP_CMOS_BRIGHTNESS_DOWN;
Holger Macht8acb0252006-10-20 14:30:28 -07001545 inc = value > current_value ? 1 : -1;
1546 for (i = current_value; i != value; i += inc) {
1547 if (!cmos_eval(cmos_cmd))
1548 return -EIO;
1549 if (!acpi_ec_write(brightness_offset, i + inc))
1550 return -EIO;
1551 }
1552
1553 return 0;
1554}
1555
1556static int brightness_write(char *buf)
1557{
1558 int level;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001559 int new_level;
1560 char *cmd;
1561
1562 while ((cmd = next_cmd(&buf))) {
Holger Macht8acb0252006-10-20 14:30:28 -07001563 if ((level = brightness_get(NULL)) < 0)
1564 return level;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001565 level &= 7;
1566
1567 if (strlencmp(cmd, "up") == 0) {
1568 new_level = level == 7 ? 7 : level + 1;
1569 } else if (strlencmp(cmd, "down") == 0) {
1570 new_level = level == 0 ? 0 : level - 1;
1571 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
1572 new_level >= 0 && new_level <= 7) {
1573 /* new_level set */
1574 } else
1575 return -EINVAL;
1576
Holger Macht8acb0252006-10-20 14:30:28 -07001577 brightness_set(new_level);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001578 }
1579
1580 return 0;
1581}
1582
Holger Macht8acb0252006-10-20 14:30:28 -07001583static int brightness_update_status(struct backlight_device *bd)
1584{
Henrique de Moraes Holschuhc9bf2962007-03-08 05:28:15 -03001585 return brightness_set(
1586 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
1587 bd->props.power == FB_BLANK_UNBLANK) ?
1588 bd->props.brightness : 0);
Holger Macht8acb0252006-10-20 14:30:28 -07001589}
1590
Richard Purdie599a52d2007-02-10 23:07:48 +00001591static struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02001592 .get_brightness = brightness_get,
1593 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02001594};
1595
1596static int brightness_init(void)
1597{
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02001598 int b;
1599
1600 b = brightness_get(NULL);
1601 if (b < 0)
1602 return b;
1603
Yu Luming519ab5f2006-12-19 12:56:15 -08001604 ibm_backlight_device = backlight_device_register("ibm", NULL, NULL,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02001605 &ibm_backlight_data);
1606 if (IS_ERR(ibm_backlight_device)) {
1607 printk(IBM_ERR "Could not register backlight device\n");
1608 return PTR_ERR(ibm_backlight_device);
1609 }
1610
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02001611 ibm_backlight_device->props.max_brightness = 7;
1612 ibm_backlight_device->props.brightness = b;
1613 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00001614
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02001615 return 0;
1616}
1617
1618static void brightness_exit(void)
1619{
1620 if (ibm_backlight_device) {
1621 backlight_device_unregister(ibm_backlight_device);
1622 ibm_backlight_device = NULL;
1623 }
1624}
1625
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001626static int volume_read(char *p)
1627{
1628 int len = 0;
1629 u8 level;
1630
1631 if (!acpi_ec_read(volume_offset, &level)) {
1632 len += sprintf(p + len, "level:\t\tunreadable\n");
1633 } else {
1634 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
1635 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
1636 len += sprintf(p + len, "commands:\tup, down, mute\n");
1637 len += sprintf(p + len, "commands:\tlevel <level>"
1638 " (<level> is 0-15)\n");
1639 }
1640
1641 return len;
1642}
1643
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001644static int volume_write(char *buf)
1645{
1646 int cmos_cmd, inc, i;
1647 u8 level, mute;
1648 int new_level, new_mute;
1649 char *cmd;
1650
1651 while ((cmd = next_cmd(&buf))) {
1652 if (!acpi_ec_read(volume_offset, &level))
1653 return -EIO;
1654 new_mute = mute = level & 0x40;
1655 new_level = level = level & 0xf;
1656
1657 if (strlencmp(cmd, "up") == 0) {
1658 if (mute)
1659 new_mute = 0;
1660 else
1661 new_level = level == 15 ? 15 : level + 1;
1662 } else if (strlencmp(cmd, "down") == 0) {
1663 if (mute)
1664 new_mute = 0;
1665 else
1666 new_level = level == 0 ? 0 : level - 1;
1667 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
1668 new_level >= 0 && new_level <= 15) {
1669 /* new_level set */
1670 } else if (strlencmp(cmd, "mute") == 0) {
1671 new_mute = 0x40;
1672 } else
1673 return -EINVAL;
1674
1675 if (new_level != level) { /* mute doesn't change */
Henrique de Moraes Holschuhe062e032007-03-23 17:33:55 -03001676 cmos_cmd = new_level > level ?
1677 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001678 inc = new_level > level ? 1 : -1;
1679
1680 if (mute && (!cmos_eval(cmos_cmd) ||
1681 !acpi_ec_write(volume_offset, level)))
1682 return -EIO;
1683
1684 for (i = level; i != new_level; i += inc)
1685 if (!cmos_eval(cmos_cmd) ||
1686 !acpi_ec_write(volume_offset, i + inc))
1687 return -EIO;
1688
Henrique de Moraes Holschuhe062e032007-03-23 17:33:55 -03001689 if (mute && (!cmos_eval(TP_CMOS_VOLUME_MUTE) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001690 !acpi_ec_write(volume_offset,
1691 new_level + mute)))
1692 return -EIO;
1693 }
1694
1695 if (new_mute != mute) { /* level doesn't change */
Henrique de Moraes Holschuhe062e032007-03-23 17:33:55 -03001696 cmos_cmd = new_mute ?
1697 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001698
1699 if (!cmos_eval(cmos_cmd) ||
1700 !acpi_ec_write(volume_offset, level + new_mute))
1701 return -EIO;
1702 }
1703 }
1704
1705 return 0;
1706}
1707
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001708static enum fan_status_access_mode fan_status_access_mode;
1709static enum fan_control_access_mode fan_control_access_mode;
1710static enum fan_control_commands fan_control_commands;
1711
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02001712static int fan_control_status_known;
1713static u8 fan_control_initial_status;
1714
Len Brown25c68a32006-12-08 04:43:41 -05001715static void fan_watchdog_fire(struct work_struct *ignored);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02001716static int fan_watchdog_maxinterval;
Len Brown25c68a32006-12-08 04:43:41 -05001717static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02001718
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001719static int fan_init(void)
1720{
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001721 fan_status_access_mode = IBMACPI_FAN_NONE;
1722 fan_control_access_mode = IBMACPI_FAN_WR_NONE;
1723 fan_control_commands = 0;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02001724 fan_control_status_known = 1;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02001725 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001726
1727 if (gfan_handle) {
1728 /* 570, 600e/x, 770e, 770x */
1729 fan_status_access_mode = IBMACPI_FAN_RD_ACPI_GFAN;
1730 } else {
1731 /* all other ThinkPads: note that even old-style
1732 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02001733 if (likely(acpi_ec_read(fan_status_offset,
1734 &fan_control_initial_status))) {
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001735 fan_status_access_mode = IBMACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02001736
1737 /* In some ThinkPads, neither the EC nor the ACPI
1738 * DSDT initialize the fan status, and it ends up
1739 * being set to 0x07 when it *could* be either
1740 * 0x07 or 0x80.
1741 *
1742 * Enable for TP-1Y (T43), TP-78 (R51e),
1743 * TP-76 (R52), TP-70 (T43, R52), which are known
1744 * to be buggy. */
1745 if (fan_control_initial_status == 0x07 &&
1746 ibm_thinkpad_ec_found &&
1747 ((ibm_thinkpad_ec_found[0] == '1' &&
1748 ibm_thinkpad_ec_found[1] == 'Y') ||
1749 (ibm_thinkpad_ec_found[0] == '7' &&
1750 (ibm_thinkpad_ec_found[1] == '6' ||
1751 ibm_thinkpad_ec_found[1] == '8' ||
1752 ibm_thinkpad_ec_found[1] == '0'))
1753 )) {
1754 printk(IBM_NOTICE
1755 "fan_init: initial fan status is "
1756 "unknown, assuming it is in auto "
1757 "mode\n");
1758 fan_control_status_known = 0;
1759 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001760 } else {
1761 printk(IBM_ERR
1762 "ThinkPad ACPI EC access misbehaving, "
1763 "fan status and control unavailable\n");
1764 return 0;
1765 }
1766 }
1767
1768 if (sfan_handle) {
1769 /* 570, 770x-JL */
1770 fan_control_access_mode = IBMACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02001771 fan_control_commands |=
1772 IBMACPI_FAN_CMD_LEVEL | IBMACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001773 } else {
1774 if (!gfan_handle) {
1775 /* gfan without sfan means no fan control */
1776 /* all other models implement TP EC 0x2f control */
1777
1778 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02001779 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001780 fan_control_access_mode =
1781 IBMACPI_FAN_WR_ACPI_FANS;
1782 fan_control_commands |=
1783 IBMACPI_FAN_CMD_SPEED |
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02001784 IBMACPI_FAN_CMD_LEVEL |
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001785 IBMACPI_FAN_CMD_ENABLE;
1786 } else {
1787 fan_control_access_mode = IBMACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02001788 fan_control_commands |=
1789 IBMACPI_FAN_CMD_LEVEL |
1790 IBMACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02001791 }
1792 }
1793 }
1794
1795 return 0;
1796}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001797
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02001798static int fan_get_status(u8 *status)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001799{
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02001800 u8 s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001801
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02001802 /* TODO:
1803 * Add IBMACPI_FAN_RD_ACPI_FANS ? */
1804
Henrique de Moraes Holschuh3ef8a602006-11-24 11:47:10 -02001805 switch (fan_status_access_mode) {
1806 case IBMACPI_FAN_RD_ACPI_GFAN:
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001807 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02001808
1809 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001810 return -EIO;
1811
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02001812 if (likely(status))
1813 *status = s & 0x07;
1814
1815 break;
1816
1817 case IBMACPI_FAN_RD_TPEC:
1818 /* all except 570, 600e/x, 770e, 770x */
1819 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
1820 return -EIO;
1821
1822 if (likely(status))
1823 *status = s;
1824
1825 break;
1826
1827 default:
1828 return -ENXIO;
1829 }
1830
1831 return 0;
1832}
1833
1834static int fan_get_speed(unsigned int *speed)
1835{
1836 u8 hi, lo;
1837
1838 switch (fan_status_access_mode) {
1839 case IBMACPI_FAN_RD_TPEC:
1840 /* all except 570, 600e/x, 770e, 770x */
1841 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
1842 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
1843 return -EIO;
1844
1845 if (likely(speed))
1846 *speed = (hi << 8) | lo;
1847
1848 break;
1849
1850 default:
1851 return -ENXIO;
1852 }
1853
1854 return 0;
1855}
1856
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02001857static void fan_exit(void)
1858{
1859 cancel_delayed_work(&fan_watchdog_task);
1860 flush_scheduled_work();
1861}
1862
1863static void fan_watchdog_reset(void)
1864{
1865 static int fan_watchdog_active = 0;
1866
1867 if (fan_watchdog_active)
1868 cancel_delayed_work(&fan_watchdog_task);
1869
1870 if (fan_watchdog_maxinterval > 0) {
1871 fan_watchdog_active = 1;
1872 if (!schedule_delayed_work(&fan_watchdog_task,
1873 msecs_to_jiffies(fan_watchdog_maxinterval
1874 * 1000))) {
1875 printk(IBM_ERR "failed to schedule the fan watchdog, "
1876 "watchdog will not trigger\n");
1877 }
1878 } else
1879 fan_watchdog_active = 0;
1880}
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001881
1882static int fan_read(char *p)
1883{
1884 int len = 0;
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02001885 int rc;
1886 u8 status;
1887 unsigned int speed = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001888
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02001889 switch (fan_status_access_mode) {
1890 case IBMACPI_FAN_RD_ACPI_GFAN:
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001891 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02001892 if ((rc = fan_get_status(&status)) < 0)
1893 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001894
Henrique de Moraes Holschuhbab812a2006-11-24 11:47:12 -02001895 len += sprintf(p + len, "status:\t\t%s\n"
1896 "level:\t\t%d\n",
1897 (status != 0) ? "enabled" : "disabled", status);
Henrique de Moraes Holschuh3ef8a602006-11-24 11:47:10 -02001898 break;
1899
1900 case IBMACPI_FAN_RD_TPEC:
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001901 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02001902 if ((rc = fan_get_status(&status)) < 0)
1903 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001904
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02001905 if (unlikely(!fan_control_status_known)) {
1906 if (status != fan_control_initial_status)
1907 fan_control_status_known = 1;
1908 else
1909 /* Return most likely status. In fact, it
1910 * might be the only possible status */
1911 status = IBMACPI_FAN_EC_AUTO;
1912 }
1913
Henrique de Moraes Holschuhbab812a2006-11-24 11:47:12 -02001914 len += sprintf(p + len, "status:\t\t%s\n",
1915 (status != 0) ? "enabled" : "disabled");
Henrique de Moraes Holschuh3ef8a602006-11-24 11:47:10 -02001916
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02001917 /* No ThinkPad boots on disengaged mode, we can safely
1918 * assume the tachometer is online if fan control status
1919 * was unknown */
Henrique de Moraes Holschuhc52f0aa2006-11-24 11:47:10 -02001920 if ((rc = fan_get_speed(&speed)) < 0)
1921 return rc;
1922
1923 len += sprintf(p + len, "speed:\t\t%d\n", speed);
Henrique de Moraes Holschuhbab812a2006-11-24 11:47:12 -02001924
1925 if (status & IBMACPI_FAN_EC_DISENGAGED)
1926 /* Disengaged mode takes precedence */
1927 len += sprintf(p + len, "level:\t\tdisengaged\n");
1928 else if (status & IBMACPI_FAN_EC_AUTO)
1929 len += sprintf(p + len, "level:\t\tauto\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001930 else
Henrique de Moraes Holschuhbab812a2006-11-24 11:47:12 -02001931 len += sprintf(p + len, "level:\t\t%d\n", status);
Henrique de Moraes Holschuh3ef8a602006-11-24 11:47:10 -02001932 break;
1933
1934 case IBMACPI_FAN_NONE:
1935 default:
1936 len += sprintf(p + len, "status:\t\tnot supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001937 }
1938
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02001939 if (fan_control_commands & IBMACPI_FAN_CMD_LEVEL) {
1940 len += sprintf(p + len, "commands:\tlevel <level>");
1941
1942 switch (fan_control_access_mode) {
1943 case IBMACPI_FAN_WR_ACPI_SFAN:
1944 len += sprintf(p + len, " (<level> is 0-7)\n");
1945 break;
1946
1947 default:
1948 len += sprintf(p + len, " (<level> is 0-7, "
1949 "auto, disengaged)\n");
1950 break;
1951 }
1952 }
Henrique de Moraes Holschuh3ef8a602006-11-24 11:47:10 -02001953
1954 if (fan_control_commands & IBMACPI_FAN_CMD_ENABLE)
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02001955 len += sprintf(p + len, "commands:\tenable, disable\n"
1956 "commands:\twatchdog <timeout> (<timeout> is 0 (off), "
1957 "1-120 (seconds))\n");
Henrique de Moraes Holschuh3ef8a602006-11-24 11:47:10 -02001958
1959 if (fan_control_commands & IBMACPI_FAN_CMD_SPEED)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001960 len += sprintf(p + len, "commands:\tspeed <speed>"
1961 " (<speed> is 0-65535)\n");
1962
1963 return len;
1964}
1965
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02001966static int fan_set_level(int level)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001967{
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02001968 switch (fan_control_access_mode) {
1969 case IBMACPI_FAN_WR_ACPI_SFAN:
1970 if (level >= 0 && level <= 7) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001971 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
1972 return -EIO;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02001973 } else
1974 return -EINVAL;
1975 break;
1976
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02001977 case IBMACPI_FAN_WR_ACPI_FANS:
1978 case IBMACPI_FAN_WR_TPEC:
1979 if ((level != IBMACPI_FAN_EC_AUTO) &&
1980 (level != IBMACPI_FAN_EC_DISENGAGED) &&
1981 ((level < 0) || (level > 7)))
1982 return -EINVAL;
1983
1984 if (!acpi_ec_write(fan_status_offset, level))
1985 return -EIO;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02001986 else
1987 fan_control_status_known = 1;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02001988 break;
1989
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02001990 default:
1991 return -ENXIO;
1992 }
1993 return 0;
1994}
1995
1996static int fan_set_enable(void)
1997{
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02001998 u8 s;
1999 int rc;
2000
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002001 switch (fan_control_access_mode) {
2002 case IBMACPI_FAN_WR_ACPI_FANS:
2003 case IBMACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002004 if ((rc = fan_get_status(&s)) < 0)
2005 return rc;
2006
2007 /* Don't go out of emergency fan mode */
2008 if (s != 7)
2009 s = IBMACPI_FAN_EC_AUTO;
2010
2011 if (!acpi_ec_write(fan_status_offset, s))
2012 return -EIO;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002013 else
2014 fan_control_status_known = 1;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002015 break;
2016
2017 case IBMACPI_FAN_WR_ACPI_SFAN:
2018 if ((rc = fan_get_status(&s)) < 0)
2019 return rc;
2020
2021 s &= 0x07;
2022
2023 /* Set fan to at least level 4 */
2024 if (s < 4)
2025 s = 4;
2026
2027 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002028 return -EIO;
2029 break;
2030
2031 default:
2032 return -ENXIO;
2033 }
2034 return 0;
2035}
2036
2037static int fan_set_disable(void)
2038{
2039 switch (fan_control_access_mode) {
2040 case IBMACPI_FAN_WR_ACPI_FANS:
2041 case IBMACPI_FAN_WR_TPEC:
2042 if (!acpi_ec_write(fan_status_offset, 0x00))
2043 return -EIO;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02002044 else
2045 fan_control_status_known = 1;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002046 break;
2047
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02002048 case IBMACPI_FAN_WR_ACPI_SFAN:
2049 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
2050 return -EIO;
2051 break;
2052
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002053 default:
2054 return -ENXIO;
2055 }
2056 return 0;
2057}
2058
2059static int fan_set_speed(int speed)
2060{
2061 switch (fan_control_access_mode) {
2062 case IBMACPI_FAN_WR_ACPI_FANS:
2063 if (speed >= 0 && speed <= 65535) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002064 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
2065 speed, speed, speed))
2066 return -EIO;
2067 } else
2068 return -EINVAL;
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002069 break;
2070
2071 default:
2072 return -ENXIO;
2073 }
2074 return 0;
2075}
2076
2077static int fan_write_cmd_level(const char *cmd, int *rc)
2078{
2079 int level;
2080
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02002081 if (strlencmp(cmd, "level auto") == 0)
2082 level = IBMACPI_FAN_EC_AUTO;
2083 else if (strlencmp(cmd, "level disengaged") == 0)
2084 level = IBMACPI_FAN_EC_DISENGAGED;
2085 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002086 return 0;
2087
2088 if ((*rc = fan_set_level(level)) == -ENXIO)
2089 printk(IBM_ERR "level command accepted for unsupported "
2090 "access mode %d", fan_control_access_mode);
2091
2092 return 1;
2093}
2094
2095static int fan_write_cmd_enable(const char *cmd, int *rc)
2096{
2097 if (strlencmp(cmd, "enable") != 0)
2098 return 0;
2099
2100 if ((*rc = fan_set_enable()) == -ENXIO)
2101 printk(IBM_ERR "enable command accepted for unsupported "
2102 "access mode %d", fan_control_access_mode);
2103
2104 return 1;
2105}
2106
2107static int fan_write_cmd_disable(const char *cmd, int *rc)
2108{
2109 if (strlencmp(cmd, "disable") != 0)
2110 return 0;
2111
2112 if ((*rc = fan_set_disable()) == -ENXIO)
2113 printk(IBM_ERR "disable command accepted for unsupported "
2114 "access mode %d", fan_control_access_mode);
2115
2116 return 1;
2117}
2118
2119static int fan_write_cmd_speed(const char *cmd, int *rc)
2120{
2121 int speed;
2122
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02002123 /* TODO:
2124 * Support speed <low> <medium> <high> ? */
2125
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002126 if (sscanf(cmd, "speed %d", &speed) != 1)
2127 return 0;
2128
2129 if ((*rc = fan_set_speed(speed)) == -ENXIO)
2130 printk(IBM_ERR "speed command accepted for unsupported "
2131 "access mode %d", fan_control_access_mode);
2132
2133 return 1;
2134}
2135
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002136static int fan_write_cmd_watchdog(const char *cmd, int *rc)
2137{
2138 int interval;
2139
2140 if (sscanf(cmd, "watchdog %d", &interval) != 1)
2141 return 0;
2142
2143 if (interval < 0 || interval > 120)
2144 *rc = -EINVAL;
2145 else
2146 fan_watchdog_maxinterval = interval;
2147
2148 return 1;
2149}
2150
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002151static int fan_write(char *buf)
2152{
2153 char *cmd;
2154 int rc = 0;
2155
2156 while (!rc && (cmd = next_cmd(&buf))) {
2157 if (!((fan_control_commands & IBMACPI_FAN_CMD_LEVEL) &&
2158 fan_write_cmd_level(cmd, &rc)) &&
2159 !((fan_control_commands & IBMACPI_FAN_CMD_ENABLE) &&
2160 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002161 fan_write_cmd_disable(cmd, &rc) ||
2162 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002163 !((fan_control_commands & IBMACPI_FAN_CMD_SPEED) &&
2164 fan_write_cmd_speed(cmd, &rc))
2165 )
2166 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002167 else if (!rc)
2168 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002169 }
2170
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02002171 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002172}
2173
Len Brown25c68a32006-12-08 04:43:41 -05002174static void fan_watchdog_fire(struct work_struct *ignored)
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002175{
2176 printk(IBM_NOTICE "fan watchdog: enabling fan\n");
2177 if (fan_set_enable()) {
2178 printk(IBM_ERR "fan watchdog: error while enabling fan\n");
2179 /* reschedule for later */
2180 fan_watchdog_reset();
2181 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182}
2183
2184static struct ibm_struct ibms[] = {
2185 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002186 .name = "driver",
Adrian Bunk1f217822006-12-19 13:01:28 -08002187 .init = ibm_acpi_driver_init,
Henrique de Moraes Holschuhe062e032007-03-23 17:33:55 -03002188 .read = ibm_acpi_driver_read,
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002189 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002191 .name = "hotkey",
2192 .hid = IBM_HKEY_HID,
2193 .init = hotkey_init,
2194 .read = hotkey_read,
2195 .write = hotkey_write,
2196 .exit = hotkey_exit,
2197 .notify = hotkey_notify,
2198 .handle = &hkey_handle,
2199 .type = ACPI_DEVICE_NOTIFY,
2200 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002202 .name = "bluetooth",
2203 .init = bluetooth_init,
2204 .read = bluetooth_read,
2205 .write = bluetooth_write,
2206 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 {
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002208 .name = "wan",
2209 .init = wan_init,
2210 .read = wan_read,
2211 .write = wan_write,
2212 .experimental = 1,
2213 },
2214 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002215 .name = "video",
2216 .init = video_init,
2217 .read = video_read,
2218 .write = video_write,
2219 .exit = video_exit,
2220 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002222 .name = "light",
2223 .init = light_init,
2224 .read = light_read,
2225 .write = light_write,
2226 },
Kristen Accardi63e5f242006-02-23 17:56:06 -08002227#ifdef CONFIG_ACPI_IBM_DOCK
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002229 .name = "dock",
2230 .read = dock_read,
2231 .write = dock_write,
2232 .notify = dock_notify,
2233 .handle = &dock_handle,
2234 .type = ACPI_SYSTEM_NOTIFY,
2235 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002237 .name = "dock",
2238 .hid = IBM_PCI_HID,
2239 .notify = dock_notify,
2240 .handle = &pci_handle,
2241 .type = ACPI_SYSTEM_NOTIFY,
2242 },
Kristen Accardi63e5f242006-02-23 17:56:06 -08002243#endif
Henrique de Moraes Holschuh2bc808a2007-02-21 13:05:38 -02002244#ifdef CONFIG_ACPI_IBM_BAY
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002246 .name = "bay",
2247 .init = bay_init,
2248 .read = bay_read,
2249 .write = bay_write,
2250 .notify = bay_notify,
2251 .handle = &bay_handle,
2252 .type = ACPI_SYSTEM_NOTIFY,
2253 },
Henrique de Moraes Holschuh2bc808a2007-02-21 13:05:38 -02002254#endif /* CONFIG_ACPI_IBM_BAY */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002256 .name = "cmos",
2257 .read = cmos_read,
2258 .write = cmos_write,
2259 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002261 .name = "led",
2262 .init = led_init,
2263 .read = led_read,
2264 .write = led_write,
2265 },
2266 {
2267 .name = "beep",
2268 .read = beep_read,
2269 .write = beep_write,
2270 },
2271 {
2272 .name = "thermal",
2273 .init = thermal_init,
2274 .read = thermal_read,
2275 },
2276 {
2277 .name = "ecdump",
2278 .read = ecdump_read,
2279 .write = ecdump_write,
2280 .experimental = 1,
2281 },
2282 {
2283 .name = "brightness",
2284 .read = brightness_read,
2285 .write = brightness_write,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02002286 .init = brightness_init,
2287 .exit = brightness_exit,
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002288 },
2289 {
2290 .name = "volume",
2291 .read = volume_read,
2292 .write = volume_write,
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002293 },
2294 {
2295 .name = "fan",
2296 .read = fan_read,
2297 .write = fan_write,
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02002298 .init = fan_init,
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02002299 .exit = fan_exit,
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002300 .experimental = 1,
2301 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303
2304static int dispatch_read(char *page, char **start, off_t off, int count,
2305 int *eof, void *data)
2306{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02002307 struct ibm_struct *ibm = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 int len;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002309
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 if (!ibm || !ibm->read)
2311 return -EINVAL;
2312
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002313 len = ibm->read(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 if (len < 0)
2315 return len;
2316
2317 if (len <= off + count)
2318 *eof = 1;
2319 *start = page + off;
2320 len -= off;
2321 if (len > count)
2322 len = count;
2323 if (len < 0)
2324 len = 0;
2325
2326 return len;
2327}
2328
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002329static int dispatch_write(struct file *file, const char __user * userbuf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 unsigned long count, void *data)
2331{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02002332 struct ibm_struct *ibm = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 char *kernbuf;
2334 int ret;
2335
2336 if (!ibm || !ibm->write)
2337 return -EINVAL;
2338
2339 kernbuf = kmalloc(count + 2, GFP_KERNEL);
2340 if (!kernbuf)
2341 return -ENOMEM;
2342
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002343 if (copy_from_user(kernbuf, userbuf, count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 kfree(kernbuf);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002345 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 }
2347
2348 kernbuf[count] = 0;
2349 strcat(kernbuf, ",");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002350 ret = ibm->write(kernbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 if (ret == 0)
2352 ret = count;
2353
2354 kfree(kernbuf);
2355
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002356 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357}
2358
2359static void dispatch_notify(acpi_handle handle, u32 event, void *data)
2360{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02002361 struct ibm_struct *ibm = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362
2363 if (!ibm || !ibm->notify)
2364 return;
2365
2366 ibm->notify(ibm, event);
2367}
2368
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002369static int __init setup_notify(struct ibm_struct *ibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370{
2371 acpi_status status;
2372 int ret;
2373
2374 if (!*ibm->handle)
2375 return 0;
2376
2377 ret = acpi_bus_get_device(*ibm->handle, &ibm->device);
2378 if (ret < 0) {
2379 printk(IBM_ERR "%s device not present\n", ibm->name);
Henrique de Moraes Holschuh96e89af2007-03-15 16:15:06 -03002380 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 }
2382
2383 acpi_driver_data(ibm->device) = ibm;
2384 sprintf(acpi_device_class(ibm->device), "%s/%s", IBM_NAME, ibm->name);
2385
2386 status = acpi_install_notify_handler(*ibm->handle, ibm->type,
2387 dispatch_notify, ibm);
2388 if (ACPI_FAILURE(status)) {
Henrique de Moraes Holschuh96e89af2007-03-15 16:15:06 -03002389 if (status == AE_ALREADY_EXISTS) {
2390 printk(IBM_NOTICE "another device driver is already handling %s events\n",
2391 ibm->name);
2392 } else {
2393 printk(IBM_ERR "acpi_install_notify_handler(%s) failed: %d\n",
2394 ibm->name, status);
2395 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 return -ENODEV;
2397 }
Alexey Starikovskiy4afaf542006-12-18 14:53:33 -03002398 ibm->notify_installed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 return 0;
2400}
2401
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002402static int __init ibm_device_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403{
2404 return 0;
2405}
2406
Henrique de Moraes Holschuhe062e032007-03-23 17:33:55 -03002407static int __init register_ibmacpi_subdriver(struct ibm_struct *ibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408{
2409 int ret;
2410
Burman Yan36bcbec2006-12-19 12:56:11 -08002411 ibm->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 if (!ibm->driver) {
2413 printk(IBM_ERR "kmalloc(ibm->driver) failed\n");
2414 return -1;
2415 }
2416
Henrique de Moraes Holschuh3dfd35c2006-11-24 10:32:32 -02002417 sprintf(ibm->driver->name, "%s_%s", IBM_NAME, ibm->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 ibm->driver->ids = ibm->hid;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002419 ibm->driver->ops.add = &ibm_device_add;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420
2421 ret = acpi_bus_register_driver(ibm->driver);
2422 if (ret < 0) {
2423 printk(IBM_ERR "acpi_bus_register_driver(%s) failed: %d\n",
2424 ibm->hid, ret);
2425 kfree(ibm->driver);
2426 }
2427
2428 return ret;
2429}
2430
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002431static int __init ibm_init(struct ibm_struct *ibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432{
2433 int ret;
2434 struct proc_dir_entry *entry;
2435
2436 if (ibm->experimental && !experimental)
2437 return 0;
2438
2439 if (ibm->hid) {
Henrique de Moraes Holschuhe062e032007-03-23 17:33:55 -03002440 ret = register_ibmacpi_subdriver(ibm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 if (ret < 0)
2442 return ret;
2443 ibm->driver_registered = 1;
2444 }
2445
2446 if (ibm->init) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002447 ret = ibm->init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 if (ret != 0)
2449 return ret;
2450 ibm->init_called = 1;
2451 }
2452
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002453 if (ibm->read) {
2454 entry = create_proc_entry(ibm->name,
2455 S_IFREG | S_IRUGO | S_IWUSR,
2456 proc_dir);
2457 if (!entry) {
2458 printk(IBM_ERR "unable to create proc entry %s\n",
2459 ibm->name);
2460 return -ENODEV;
2461 }
2462 entry->owner = THIS_MODULE;
2463 entry->data = ibm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 entry->read_proc = &dispatch_read;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002465 if (ibm->write)
2466 entry->write_proc = &dispatch_write;
2467 ibm->proc_created = 1;
2468 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469
2470 if (ibm->notify) {
2471 ret = setup_notify(ibm);
Henrique de Moraes Holschuh96e89af2007-03-15 16:15:06 -03002472 if (ret == -ENODEV) {
2473 printk(IBM_NOTICE "disabling subdriver %s\n",
2474 ibm->name);
2475 ibm_exit(ibm);
2476 return 0;
2477 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 if (ret < 0)
2479 return ret;
2480 }
2481
2482 return 0;
2483}
2484
2485static void ibm_exit(struct ibm_struct *ibm)
2486{
2487 if (ibm->notify_installed)
2488 acpi_remove_notify_handler(*ibm->handle, ibm->type,
2489 dispatch_notify);
2490
2491 if (ibm->proc_created)
2492 remove_proc_entry(ibm->name, proc_dir);
2493
2494 if (ibm->init_called && ibm->exit)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002495 ibm->exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496
2497 if (ibm->driver_registered) {
2498 acpi_bus_unregister_driver(ibm->driver);
2499 kfree(ibm->driver);
2500 }
2501}
2502
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002503static void __init ibm_handle_init(char *name,
2504 acpi_handle * handle, acpi_handle parent,
2505 char **paths, int num_paths, char **path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506{
2507 int i;
2508 acpi_status status;
2509
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002510 for (i = 0; i < num_paths; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 status = acpi_get_handle(parent, paths[i], handle);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002512 if (ACPI_SUCCESS(status)) {
2513 *path = paths[i];
2514 return;
2515 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002517
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 *handle = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519}
2520
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002521#define IBM_HANDLE_INIT(object) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 ibm_handle_init(#object, &object##_handle, *object##_parent, \
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002523 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524
Henrique de Moraes Holschuh3945ac32007-02-06 19:13:44 -02002525static int __init set_ibm_param(const char *val, struct kernel_param *kp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526{
2527 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002529 for (i = 0; i < ARRAY_SIZE(ibms); i++)
2530 if (strcmp(ibms[i].name, kp->name) == 0 && ibms[i].write) {
2531 if (strlen(val) > sizeof(ibms[i].param) - 2)
2532 return -ENOSPC;
2533 strcpy(ibms[i].param, val);
2534 strcat(ibms[i].param, ",");
2535 return 0;
2536 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 return -EINVAL;
2539}
2540
2541#define IBM_PARAM(feature) \
2542 module_param_call(feature, set_ibm_param, NULL, NULL, 0)
2543
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002544IBM_PARAM(hotkey);
2545IBM_PARAM(bluetooth);
2546IBM_PARAM(video);
2547IBM_PARAM(light);
Kristen Accardi63e5f242006-02-23 17:56:06 -08002548#ifdef CONFIG_ACPI_IBM_DOCK
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002549IBM_PARAM(dock);
Kristen Accardi63e5f242006-02-23 17:56:06 -08002550#endif
Henrique de Moraes Holschuh2bc808a2007-02-21 13:05:38 -02002551#ifdef CONFIG_ACPI_IBM_BAY
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002552IBM_PARAM(bay);
Henrique de Moraes Holschuh2bc808a2007-02-21 13:05:38 -02002553#endif /* CONFIG_ACPI_IBM_BAY */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002554IBM_PARAM(cmos);
2555IBM_PARAM(led);
2556IBM_PARAM(beep);
2557IBM_PARAM(ecdump);
2558IBM_PARAM(brightness);
2559IBM_PARAM(volume);
2560IBM_PARAM(fan);
2561
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562static void acpi_ibm_exit(void)
2563{
2564 int i;
2565
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002566 for (i = ARRAY_SIZE(ibms) - 1; i >= 0; i--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 ibm_exit(&ibms[i]);
2568
Henrique de Moraes Holschuh3945ac32007-02-06 19:13:44 -02002569 if (proc_dir)
2570 remove_proc_entry(IBM_DIR, acpi_root_dir);
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02002571
2572 if (ibm_thinkpad_ec_found)
2573 kfree(ibm_thinkpad_ec_found);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574}
2575
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02002576static char* __init check_dmi_for_ec(void)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002577{
2578 struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02002579 char ec_fw_string[18];
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002580
2581 /*
2582 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
2583 * X32 or newer, all Z series; Some models must have an
2584 * up-to-date BIOS or they will not be detected.
2585 *
2586 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
2587 */
2588 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02002589 if (sscanf(dev->name,
2590 "IBM ThinkPad Embedded Controller -[%17c",
2591 ec_fw_string) == 1) {
2592 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
2593 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
2594 return kstrdup(ec_fw_string, GFP_KERNEL);
2595 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002596 }
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02002597 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598}
2599
2600static int __init acpi_ibm_init(void)
2601{
2602 int ret, i;
2603
2604 if (acpi_disabled)
2605 return -ENODEV;
2606
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002607 /* ec is required because many other handles are relative to it */
2608 IBM_HANDLE_INIT(ec);
2609 if (!ec_handle) {
2610 printk(IBM_ERR "ec object not found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 return -ENODEV;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002612 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02002614 /* Models with newer firmware report the EC in DMI */
2615 ibm_thinkpad_ec_found = check_dmi_for_ec();
2616
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 /* these handles are not required */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002618 IBM_HANDLE_INIT(vid);
2619 IBM_HANDLE_INIT(vid2);
2620 IBM_HANDLE_INIT(ledb);
2621 IBM_HANDLE_INIT(led);
2622 IBM_HANDLE_INIT(hkey);
2623 IBM_HANDLE_INIT(lght);
2624 IBM_HANDLE_INIT(cmos);
Kristen Accardi63e5f242006-02-23 17:56:06 -08002625#ifdef CONFIG_ACPI_IBM_DOCK
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002626 IBM_HANDLE_INIT(dock);
Kristen Accardi63e5f242006-02-23 17:56:06 -08002627#endif
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002628 IBM_HANDLE_INIT(pci);
Henrique de Moraes Holschuh2bc808a2007-02-21 13:05:38 -02002629#ifdef CONFIG_ACPI_IBM_BAY
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002630 IBM_HANDLE_INIT(bay);
2631 if (bay_handle)
2632 IBM_HANDLE_INIT(bay_ej);
2633 IBM_HANDLE_INIT(bay2);
2634 if (bay2_handle)
2635 IBM_HANDLE_INIT(bay2_ej);
Henrique de Moraes Holschuh2bc808a2007-02-21 13:05:38 -02002636#endif /* CONFIG_ACPI_IBM_BAY */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002637 IBM_HANDLE_INIT(beep);
2638 IBM_HANDLE_INIT(ecrd);
2639 IBM_HANDLE_INIT(ecwr);
2640 IBM_HANDLE_INIT(fans);
2641 IBM_HANDLE_INIT(gfan);
2642 IBM_HANDLE_INIT(sfan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643
2644 proc_dir = proc_mkdir(IBM_DIR, acpi_root_dir);
2645 if (!proc_dir) {
2646 printk(IBM_ERR "unable to create proc dir %s", IBM_DIR);
Henrique de Moraes Holschuh3945ac32007-02-06 19:13:44 -02002647 acpi_ibm_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 return -ENODEV;
2649 }
2650 proc_dir->owner = THIS_MODULE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002651
2652 for (i = 0; i < ARRAY_SIZE(ibms); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 ret = ibm_init(&ibms[i]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002654 if (ret >= 0 && *ibms[i].param)
2655 ret = ibms[i].write(ibms[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 if (ret < 0) {
2657 acpi_ibm_exit();
2658 return ret;
2659 }
2660 }
2661
2662 return 0;
2663}
2664
2665module_init(acpi_ibm_init);
2666module_exit(acpi_ibm_exit);