Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
643f12db | 2 | * thinkpad_acpi.c - ThinkPad ACPI Extras |
1da177e4 LT |
3 | * |
4 | * | |
78f81cc4 | 5 | * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net> |
a62bc916 | 6 | * Copyright (C) 2006-2007 Henrique de Moraes Holschuh <hmh@hmh.eng.br> |
1da177e4 LT |
7 | * |
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 | |
a62bc916 HMH |
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
21 | * 02110-1301, USA. | |
78f81cc4 BD |
22 | */ |
23 | ||
3b0c6485 | 24 | #define IBM_VERSION "0.16" |
741553c2 | 25 | #define TPACPI_SYSFS_VERSION 0x010000 |
78f81cc4 BD |
26 | |
27 | /* | |
1da177e4 | 28 | * Changelog: |
643f12db HMH |
29 | * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to |
30 | * drivers/misc. | |
f9ff43a6 HMH |
31 | * |
32 | * 2006-11-22 0.13 new maintainer | |
33 | * changelog now lives in git commit history, and will | |
34 | * not be updated further in-file. | |
837ca6dd | 35 | * |
78f81cc4 BD |
36 | * 2005-08-17 0.12 fix compilation on 2.6.13-rc kernels |
37 | * 2005-03-17 0.11 support for 600e, 770x | |
38 | * thanks to Jamie Lentin <lentinj@dial.pipex.com> | |
39 | * support for 770e, G41 | |
40 | * G40 and G41 don't have a thinklight | |
41 | * temperatures no longer experimental | |
42 | * experimental brightness control | |
43 | * experimental volume control | |
44 | * experimental fan enable/disable | |
837ca6dd | 45 | * 2005-01-16 0.10 fix module loading on R30, R31 |
78f81cc4 BD |
46 | * 2005-01-16 0.9 support for 570, R30, R31 |
47 | * ultrabay support on A22p, A3x | |
48 | * limit arg for cmos, led, beep, drop experimental status | |
49 | * more capable led control on A21e, A22p, T20-22, X20 | |
50 | * experimental temperatures and fan speed | |
51 | * experimental embedded controller register dump | |
52 | * mark more functions as __init, drop incorrect __exit | |
53 | * use MODULE_VERSION | |
54 | * thanks to Henrik Brix Andersen <brix@gentoo.org> | |
55 | * fix parameter passing on module loading | |
56 | * thanks to Rusty Russell <rusty@rustcorp.com.au> | |
57 | * thanks to Jim Radford <radford@blackbean.org> | |
58 | * 2004-11-08 0.8 fix init error case, don't return from a macro | |
59 | * thanks to Chris Wright <chrisw@osdl.org> | |
60 | * 2004-10-23 0.7 fix module loading on A21e, A22p, T20, T21, X20 | |
61 | * fix led control on A21e | |
62 | * 2004-10-19 0.6 use acpi_bus_register_driver() to claim HKEY device | |
1da177e4 LT |
63 | * 2004-10-18 0.5 thinklight support on A21e, G40, R32, T20, T21, X20 |
64 | * proc file format changed | |
65 | * video_switch command | |
66 | * experimental cmos control | |
67 | * experimental led control | |
68 | * experimental acpi sounds | |
78f81cc4 BD |
69 | * 2004-09-16 0.4 support for module parameters |
70 | * hotkey mask can be prefixed by 0x | |
71 | * video output switching | |
72 | * video expansion control | |
73 | * ultrabay eject support | |
74 | * removed lcd brightness/on/off control, didn't work | |
75 | * 2004-08-17 0.3 support for R40 | |
76 | * lcd off, brightness control | |
77 | * thinklight on/off | |
78 | * 2004-08-14 0.2 support for T series, X20 | |
79 | * bluetooth enable/disable | |
80 | * hotkey events disabled by default | |
81 | * removed fan control, currently useless | |
82 | * 2004-08-09 0.1 initial release, support for X series | |
1da177e4 LT |
83 | */ |
84 | ||
f21f85de | 85 | #include "thinkpad_acpi.h" |
1da177e4 | 86 | |
f9ff43a6 | 87 | MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh"); |
78f81cc4 BD |
88 | MODULE_DESCRIPTION(IBM_DESC); |
89 | MODULE_VERSION(IBM_VERSION); | |
90 | MODULE_LICENSE("GPL"); | |
91 | ||
d903ac54 HMH |
92 | /* Please remove this in year 2009 */ |
93 | MODULE_ALIAS("ibm_acpi"); | |
94 | ||
b964b437 HMH |
95 | /* |
96 | * DMI matching for module autoloading | |
97 | * | |
98 | * See http://thinkwiki.org/wiki/List_of_DMI_IDs | |
99 | * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads | |
100 | * | |
101 | * Only models listed in thinkwiki will be supported, so add yours | |
102 | * if it is not there yet. | |
103 | */ | |
104 | #define IBM_BIOS_MODULE_ALIAS(__type) \ | |
105 | MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW") | |
106 | ||
107 | /* Non-ancient thinkpads */ | |
108 | MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*"); | |
109 | MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*"); | |
110 | ||
111 | /* Ancient thinkpad BIOSes have to be identified by | |
112 | * BIOS type or model number, and there are far less | |
113 | * BIOS types than model numbers... */ | |
114 | IBM_BIOS_MODULE_ALIAS("I[B,D,H,I,M,N,O,T,W,V,Y,Z]"); | |
115 | IBM_BIOS_MODULE_ALIAS("1[0,3,6,8,A-G,I,K,M-P,S,T]"); | |
116 | IBM_BIOS_MODULE_ALIAS("K[U,X-Z]"); | |
117 | ||
1da177e4 LT |
118 | #define __unused __attribute__ ((unused)) |
119 | ||
8fef502e HMH |
120 | static enum { |
121 | TPACPI_LIFE_INIT = 0, | |
122 | TPACPI_LIFE_RUNNING, | |
123 | TPACPI_LIFE_EXITING, | |
124 | } tpacpi_lifecycle; | |
125 | ||
56b6aeb0 HMH |
126 | /**************************************************************************** |
127 | **************************************************************************** | |
128 | * | |
129 | * ACPI Helpers and device model | |
130 | * | |
131 | **************************************************************************** | |
132 | ****************************************************************************/ | |
133 | ||
134 | /************************************************************************* | |
135 | * ACPI basic handles | |
136 | */ | |
1da177e4 | 137 | |
94954cc6 | 138 | static acpi_handle root_handle; |
1da177e4 LT |
139 | |
140 | #define IBM_HANDLE(object, parent, paths...) \ | |
141 | static acpi_handle object##_handle; \ | |
142 | static acpi_handle *object##_parent = &parent##_handle; \ | |
78f81cc4 | 143 | static char *object##_path; \ |
1da177e4 LT |
144 | static char *object##_paths[] = { paths } |
145 | ||
78f81cc4 BD |
146 | IBM_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */ |
147 | "\\_SB.PCI.ISA.EC", /* 570 */ | |
148 | "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */ | |
149 | "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */ | |
150 | "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */ | |
151 | "\\_SB.PCI0.ICH3.EC0", /* R31 */ | |
152 | "\\_SB.PCI0.LPC.EC", /* all others */ | |
56b6aeb0 | 153 | ); |
78f81cc4 | 154 | |
56b6aeb0 HMH |
155 | IBM_HANDLE(ecrd, ec, "ECRD"); /* 570 */ |
156 | IBM_HANDLE(ecwr, ec, "ECWR"); /* 570 */ | |
78f81cc4 | 157 | |
56b6aeb0 HMH |
158 | |
159 | /************************************************************************* | |
160 | * Misc ACPI handles | |
161 | */ | |
78f81cc4 BD |
162 | |
163 | IBM_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, T4x, X31, X40 */ | |
164 | "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */ | |
165 | "\\CMS", /* R40, R40e */ | |
56b6aeb0 | 166 | ); /* all others */ |
78f81cc4 BD |
167 | |
168 | IBM_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */ | |
169 | "^HKEY", /* R30, R31 */ | |
170 | "HKEY", /* all others */ | |
56b6aeb0 | 171 | ); /* 570 */ |
78f81cc4 | 172 | |
78f81cc4 | 173 | |
56b6aeb0 HMH |
174 | /************************************************************************* |
175 | * ACPI helpers | |
a8b7a662 HMH |
176 | */ |
177 | ||
1da177e4 LT |
178 | static int acpi_evalf(acpi_handle handle, |
179 | void *res, char *method, char *fmt, ...) | |
180 | { | |
181 | char *fmt0 = fmt; | |
78f81cc4 BD |
182 | struct acpi_object_list params; |
183 | union acpi_object in_objs[IBM_MAX_ACPI_ARGS]; | |
184 | struct acpi_buffer result, *resultp; | |
185 | union acpi_object out_obj; | |
186 | acpi_status status; | |
187 | va_list ap; | |
188 | char res_type; | |
189 | int success; | |
190 | int quiet; | |
1da177e4 LT |
191 | |
192 | if (!*fmt) { | |
193 | printk(IBM_ERR "acpi_evalf() called with empty format\n"); | |
194 | return 0; | |
195 | } | |
196 | ||
197 | if (*fmt == 'q') { | |
198 | quiet = 1; | |
199 | fmt++; | |
200 | } else | |
201 | quiet = 0; | |
202 | ||
203 | res_type = *(fmt++); | |
204 | ||
205 | params.count = 0; | |
206 | params.pointer = &in_objs[0]; | |
207 | ||
208 | va_start(ap, fmt); | |
209 | while (*fmt) { | |
210 | char c = *(fmt++); | |
211 | switch (c) { | |
212 | case 'd': /* int */ | |
213 | in_objs[params.count].integer.value = va_arg(ap, int); | |
214 | in_objs[params.count++].type = ACPI_TYPE_INTEGER; | |
215 | break; | |
78f81cc4 | 216 | /* add more types as needed */ |
1da177e4 LT |
217 | default: |
218 | printk(IBM_ERR "acpi_evalf() called " | |
219 | "with invalid format character '%c'\n", c); | |
220 | return 0; | |
221 | } | |
222 | } | |
223 | va_end(ap); | |
224 | ||
78f81cc4 BD |
225 | if (res_type != 'v') { |
226 | result.length = sizeof(out_obj); | |
227 | result.pointer = &out_obj; | |
228 | resultp = &result; | |
229 | } else | |
230 | resultp = NULL; | |
1da177e4 | 231 | |
78f81cc4 | 232 | status = acpi_evaluate_object(handle, method, ¶ms, resultp); |
1da177e4 LT |
233 | |
234 | switch (res_type) { | |
78f81cc4 | 235 | case 'd': /* int */ |
1da177e4 LT |
236 | if (res) |
237 | *(int *)res = out_obj.integer.value; | |
238 | success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER; | |
239 | break; | |
78f81cc4 | 240 | case 'v': /* void */ |
1da177e4 LT |
241 | success = status == AE_OK; |
242 | break; | |
78f81cc4 | 243 | /* add more types as needed */ |
1da177e4 LT |
244 | default: |
245 | printk(IBM_ERR "acpi_evalf() called " | |
246 | "with invalid format character '%c'\n", res_type); | |
247 | return 0; | |
248 | } | |
249 | ||
56b6aeb0 HMH |
250 | if (!success && !quiet) |
251 | printk(IBM_ERR "acpi_evalf(%s, %s, ...) failed: %d\n", | |
252 | method, fmt0, status); | |
253 | ||
254 | return success; | |
255 | } | |
256 | ||
257 | static void __unused acpi_print_int(acpi_handle handle, char *method) | |
258 | { | |
259 | int i; | |
260 | ||
261 | if (acpi_evalf(handle, &i, method, "d")) | |
262 | printk(IBM_INFO "%s = 0x%x\n", method, i); | |
263 | else | |
264 | printk(IBM_ERR "error calling %s\n", method); | |
265 | } | |
266 | ||
267 | static int acpi_ec_read(int i, u8 * p) | |
268 | { | |
269 | int v; | |
270 | ||
271 | if (ecrd_handle) { | |
272 | if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i)) | |
273 | return 0; | |
274 | *p = v; | |
275 | } else { | |
276 | if (ec_read(i, p) < 0) | |
277 | return 0; | |
278 | } | |
279 | ||
280 | return 1; | |
281 | } | |
282 | ||
283 | static int acpi_ec_write(int i, u8 v) | |
284 | { | |
285 | if (ecwr_handle) { | |
286 | if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v)) | |
287 | return 0; | |
288 | } else { | |
289 | if (ec_write(i, v) < 0) | |
290 | return 0; | |
291 | } | |
292 | ||
293 | return 1; | |
294 | } | |
295 | ||
296 | static int _sta(acpi_handle handle) | |
297 | { | |
298 | int status; | |
299 | ||
300 | if (!handle || !acpi_evalf(handle, &status, "_STA", "d")) | |
301 | status = 0; | |
302 | ||
303 | return status; | |
304 | } | |
305 | ||
c9bea99c HMH |
306 | static int issue_thinkpad_cmos_command(int cmos_cmd) |
307 | { | |
308 | if (!cmos_handle) | |
309 | return -ENXIO; | |
310 | ||
311 | if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd)) | |
312 | return -EIO; | |
313 | ||
314 | return 0; | |
315 | } | |
316 | ||
56b6aeb0 HMH |
317 | /************************************************************************* |
318 | * ACPI device model | |
319 | */ | |
320 | ||
8d376cd6 | 321 | static void drv_acpi_handle_init(char *name, |
5fba344c HMH |
322 | acpi_handle *handle, acpi_handle parent, |
323 | char **paths, int num_paths, char **path) | |
56b6aeb0 HMH |
324 | { |
325 | int i; | |
326 | acpi_status status; | |
327 | ||
5ae930e6 HMH |
328 | vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n", |
329 | name); | |
330 | ||
56b6aeb0 HMH |
331 | for (i = 0; i < num_paths; i++) { |
332 | status = acpi_get_handle(parent, paths[i], handle); | |
333 | if (ACPI_SUCCESS(status)) { | |
334 | *path = paths[i]; | |
5ae930e6 HMH |
335 | dbg_printk(TPACPI_DBG_INIT, |
336 | "Found ACPI handle %s for %s\n", | |
337 | *path, name); | |
56b6aeb0 HMH |
338 | return; |
339 | } | |
340 | } | |
341 | ||
5ae930e6 HMH |
342 | vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n", |
343 | name); | |
56b6aeb0 HMH |
344 | *handle = NULL; |
345 | } | |
346 | ||
8d376cd6 | 347 | static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data) |
56b6aeb0 HMH |
348 | { |
349 | struct ibm_struct *ibm = data; | |
350 | ||
8fef502e HMH |
351 | if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING) |
352 | return; | |
353 | ||
8d376cd6 | 354 | if (!ibm || !ibm->acpi || !ibm->acpi->notify) |
56b6aeb0 HMH |
355 | return; |
356 | ||
8d376cd6 | 357 | ibm->acpi->notify(ibm, event); |
56b6aeb0 HMH |
358 | } |
359 | ||
8d376cd6 | 360 | static int __init setup_acpi_notify(struct ibm_struct *ibm) |
56b6aeb0 HMH |
361 | { |
362 | acpi_status status; | |
5ae930e6 | 363 | int rc; |
56b6aeb0 | 364 | |
8d376cd6 HMH |
365 | BUG_ON(!ibm->acpi); |
366 | ||
367 | if (!*ibm->acpi->handle) | |
56b6aeb0 HMH |
368 | return 0; |
369 | ||
5ae930e6 | 370 | vdbg_printk(TPACPI_DBG_INIT, |
fe08bc4b HMH |
371 | "setting up ACPI notify for %s\n", ibm->name); |
372 | ||
5ae930e6 HMH |
373 | rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device); |
374 | if (rc < 0) { | |
375 | printk(IBM_ERR "acpi_bus_get_device(%s) failed: %d\n", | |
376 | ibm->name, rc); | |
56b6aeb0 HMH |
377 | return -ENODEV; |
378 | } | |
379 | ||
8d376cd6 HMH |
380 | acpi_driver_data(ibm->acpi->device) = ibm; |
381 | sprintf(acpi_device_class(ibm->acpi->device), "%s/%s", | |
643f12db HMH |
382 | IBM_ACPI_EVENT_PREFIX, |
383 | ibm->name); | |
56b6aeb0 | 384 | |
8d376cd6 HMH |
385 | status = acpi_install_notify_handler(*ibm->acpi->handle, |
386 | ibm->acpi->type, dispatch_acpi_notify, ibm); | |
56b6aeb0 HMH |
387 | if (ACPI_FAILURE(status)) { |
388 | if (status == AE_ALREADY_EXISTS) { | |
389 | printk(IBM_NOTICE "another device driver is already handling %s events\n", | |
390 | ibm->name); | |
391 | } else { | |
392 | printk(IBM_ERR "acpi_install_notify_handler(%s) failed: %d\n", | |
393 | ibm->name, status); | |
394 | } | |
395 | return -ENODEV; | |
396 | } | |
8d376cd6 | 397 | ibm->flags.acpi_notify_installed = 1; |
56b6aeb0 HMH |
398 | return 0; |
399 | } | |
400 | ||
8d376cd6 | 401 | static int __init tpacpi_device_add(struct acpi_device *device) |
56b6aeb0 HMH |
402 | { |
403 | return 0; | |
404 | } | |
405 | ||
6700121b | 406 | static int __init register_tpacpi_subdriver(struct ibm_struct *ibm) |
56b6aeb0 | 407 | { |
5ae930e6 | 408 | int rc; |
56b6aeb0 | 409 | |
fe08bc4b HMH |
410 | dbg_printk(TPACPI_DBG_INIT, |
411 | "registering %s as an ACPI driver\n", ibm->name); | |
412 | ||
8d376cd6 HMH |
413 | BUG_ON(!ibm->acpi); |
414 | ||
415 | ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL); | |
416 | if (!ibm->acpi->driver) { | |
5fba344c HMH |
417 | printk(IBM_ERR "kzalloc(ibm->driver) failed\n"); |
418 | return -ENOMEM; | |
56b6aeb0 HMH |
419 | } |
420 | ||
8d376cd6 HMH |
421 | sprintf(ibm->acpi->driver->name, "%s_%s", IBM_NAME, ibm->name); |
422 | ibm->acpi->driver->ids = ibm->acpi->hid; | |
1ba90e3a | 423 | |
8d376cd6 | 424 | ibm->acpi->driver->ops.add = &tpacpi_device_add; |
56b6aeb0 | 425 | |
5ae930e6 HMH |
426 | rc = acpi_bus_register_driver(ibm->acpi->driver); |
427 | if (rc < 0) { | |
56b6aeb0 | 428 | printk(IBM_ERR "acpi_bus_register_driver(%s) failed: %d\n", |
1ba90e3a | 429 | ibm->name, rc); |
8d376cd6 HMH |
430 | kfree(ibm->acpi->driver); |
431 | ibm->acpi->driver = NULL; | |
5ae930e6 | 432 | } else if (!rc) |
8d376cd6 | 433 | ibm->flags.acpi_driver_registered = 1; |
56b6aeb0 | 434 | |
5ae930e6 | 435 | return rc; |
56b6aeb0 HMH |
436 | } |
437 | ||
438 | ||
439 | /**************************************************************************** | |
440 | **************************************************************************** | |
441 | * | |
442 | * Procfs Helpers | |
443 | * | |
444 | **************************************************************************** | |
445 | ****************************************************************************/ | |
446 | ||
8d376cd6 HMH |
447 | static int dispatch_procfs_read(char *page, char **start, off_t off, |
448 | int count, int *eof, void *data) | |
56b6aeb0 HMH |
449 | { |
450 | struct ibm_struct *ibm = data; | |
451 | int len; | |
452 | ||
453 | if (!ibm || !ibm->read) | |
454 | return -EINVAL; | |
455 | ||
456 | len = ibm->read(page); | |
457 | if (len < 0) | |
458 | return len; | |
459 | ||
460 | if (len <= off + count) | |
461 | *eof = 1; | |
462 | *start = page + off; | |
463 | len -= off; | |
464 | if (len > count) | |
465 | len = count; | |
466 | if (len < 0) | |
467 | len = 0; | |
468 | ||
469 | return len; | |
470 | } | |
471 | ||
8d376cd6 HMH |
472 | static int dispatch_procfs_write(struct file *file, |
473 | const char __user * userbuf, | |
474 | unsigned long count, void *data) | |
56b6aeb0 HMH |
475 | { |
476 | struct ibm_struct *ibm = data; | |
477 | char *kernbuf; | |
478 | int ret; | |
479 | ||
480 | if (!ibm || !ibm->write) | |
481 | return -EINVAL; | |
482 | ||
483 | kernbuf = kmalloc(count + 2, GFP_KERNEL); | |
484 | if (!kernbuf) | |
485 | return -ENOMEM; | |
1da177e4 | 486 | |
56b6aeb0 HMH |
487 | if (copy_from_user(kernbuf, userbuf, count)) { |
488 | kfree(kernbuf); | |
489 | return -EFAULT; | |
490 | } | |
1da177e4 | 491 | |
56b6aeb0 HMH |
492 | kernbuf[count] = 0; |
493 | strcat(kernbuf, ","); | |
494 | ret = ibm->write(kernbuf); | |
495 | if (ret == 0) | |
496 | ret = count; | |
1da177e4 | 497 | |
56b6aeb0 HMH |
498 | kfree(kernbuf); |
499 | ||
500 | return ret; | |
1da177e4 LT |
501 | } |
502 | ||
503 | static char *next_cmd(char **cmds) | |
504 | { | |
505 | char *start = *cmds; | |
506 | char *end; | |
507 | ||
508 | while ((end = strchr(start, ',')) && end == start) | |
509 | start = end + 1; | |
510 | ||
511 | if (!end) | |
512 | return NULL; | |
513 | ||
514 | *end = 0; | |
515 | *cmds = end + 1; | |
516 | return start; | |
517 | } | |
518 | ||
56b6aeb0 | 519 | |
54ae1501 HMH |
520 | /**************************************************************************** |
521 | **************************************************************************** | |
522 | * | |
7f5d1cd6 | 523 | * Device model: input, hwmon and platform |
54ae1501 HMH |
524 | * |
525 | **************************************************************************** | |
526 | ****************************************************************************/ | |
527 | ||
94954cc6 HMH |
528 | static struct platform_device *tpacpi_pdev; |
529 | static struct class_device *tpacpi_hwmon; | |
7f5d1cd6 | 530 | static struct input_dev *tpacpi_inputdev; |
8523ed6f | 531 | static struct mutex tpacpi_inputdev_send_mutex; |
54ae1501 | 532 | |
e295e850 HMH |
533 | |
534 | static int tpacpi_resume_handler(struct platform_device *pdev) | |
535 | { | |
536 | struct ibm_struct *ibm, *itmp; | |
537 | ||
538 | list_for_each_entry_safe(ibm, itmp, | |
539 | &tpacpi_all_drivers, | |
540 | all_drivers) { | |
541 | if (ibm->resume) | |
542 | (ibm->resume)(); | |
543 | } | |
544 | ||
545 | return 0; | |
546 | } | |
547 | ||
54ae1501 HMH |
548 | static struct platform_driver tpacpi_pdriver = { |
549 | .driver = { | |
550 | .name = IBM_DRVR_NAME, | |
551 | .owner = THIS_MODULE, | |
552 | }, | |
e295e850 | 553 | .resume = tpacpi_resume_handler, |
54ae1501 HMH |
554 | }; |
555 | ||
556 | ||
176750d6 HMH |
557 | /************************************************************************* |
558 | * thinkpad-acpi driver attributes | |
559 | */ | |
560 | ||
561 | /* interface_version --------------------------------------------------- */ | |
562 | static ssize_t tpacpi_driver_interface_version_show( | |
563 | struct device_driver *drv, | |
564 | char *buf) | |
565 | { | |
566 | return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION); | |
567 | } | |
568 | ||
569 | static DRIVER_ATTR(interface_version, S_IRUGO, | |
570 | tpacpi_driver_interface_version_show, NULL); | |
571 | ||
572 | /* debug_level --------------------------------------------------------- */ | |
573 | static ssize_t tpacpi_driver_debug_show(struct device_driver *drv, | |
574 | char *buf) | |
575 | { | |
576 | return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level); | |
577 | } | |
578 | ||
579 | static ssize_t tpacpi_driver_debug_store(struct device_driver *drv, | |
580 | const char *buf, size_t count) | |
581 | { | |
582 | unsigned long t; | |
176750d6 | 583 | |
7252374a | 584 | if (parse_strtoul(buf, 0xffff, &t)) |
176750d6 HMH |
585 | return -EINVAL; |
586 | ||
587 | dbg_level = t; | |
588 | ||
589 | return count; | |
590 | } | |
591 | ||
592 | static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO, | |
593 | tpacpi_driver_debug_show, tpacpi_driver_debug_store); | |
594 | ||
595 | /* version ------------------------------------------------------------- */ | |
596 | static ssize_t tpacpi_driver_version_show(struct device_driver *drv, | |
597 | char *buf) | |
598 | { | |
599 | return snprintf(buf, PAGE_SIZE, "%s v%s\n", IBM_DESC, IBM_VERSION); | |
600 | } | |
601 | ||
602 | static DRIVER_ATTR(version, S_IRUGO, | |
603 | tpacpi_driver_version_show, NULL); | |
604 | ||
605 | /* --------------------------------------------------------------------- */ | |
606 | ||
607 | static struct driver_attribute* tpacpi_driver_attributes[] = { | |
608 | &driver_attr_debug_level, &driver_attr_version, | |
609 | &driver_attr_interface_version, | |
610 | }; | |
611 | ||
612 | static int __init tpacpi_create_driver_attributes(struct device_driver *drv) | |
613 | { | |
614 | int i, res; | |
615 | ||
616 | i = 0; | |
617 | res = 0; | |
618 | while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) { | |
619 | res = driver_create_file(drv, tpacpi_driver_attributes[i]); | |
620 | i++; | |
621 | } | |
622 | ||
623 | return res; | |
624 | } | |
625 | ||
626 | static void tpacpi_remove_driver_attributes(struct device_driver *drv) | |
627 | { | |
628 | int i; | |
629 | ||
630 | for(i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++) | |
631 | driver_remove_file(drv, tpacpi_driver_attributes[i]); | |
632 | } | |
633 | ||
7252374a HMH |
634 | /************************************************************************* |
635 | * sysfs support helpers | |
636 | */ | |
637 | ||
638 | struct attribute_set_obj { | |
639 | struct attribute_set s; | |
640 | struct attribute *a; | |
641 | } __attribute__((packed)); | |
642 | ||
643 | static struct attribute_set *create_attr_set(unsigned int max_members, | |
644 | const char* name) | |
645 | { | |
646 | struct attribute_set_obj *sobj; | |
647 | ||
648 | if (max_members == 0) | |
649 | return NULL; | |
650 | ||
651 | /* Allocates space for implicit NULL at the end too */ | |
652 | sobj = kzalloc(sizeof(struct attribute_set_obj) + | |
653 | max_members * sizeof(struct attribute *), | |
654 | GFP_KERNEL); | |
655 | if (!sobj) | |
656 | return NULL; | |
657 | sobj->s.max_members = max_members; | |
658 | sobj->s.group.attrs = &sobj->a; | |
659 | sobj->s.group.name = name; | |
660 | ||
661 | return &sobj->s; | |
662 | } | |
663 | ||
664 | /* not multi-threaded safe, use it in a single thread per set */ | |
665 | static int add_to_attr_set(struct attribute_set* s, struct attribute *attr) | |
666 | { | |
667 | if (!s || !attr) | |
668 | return -EINVAL; | |
669 | ||
670 | if (s->members >= s->max_members) | |
671 | return -ENOMEM; | |
672 | ||
673 | s->group.attrs[s->members] = attr; | |
674 | s->members++; | |
675 | ||
676 | return 0; | |
677 | } | |
678 | ||
679 | static int add_many_to_attr_set(struct attribute_set* s, | |
680 | struct attribute **attr, | |
681 | unsigned int count) | |
682 | { | |
683 | int i, res; | |
684 | ||
685 | for (i = 0; i < count; i++) { | |
686 | res = add_to_attr_set(s, attr[i]); | |
687 | if (res) | |
688 | return res; | |
689 | } | |
690 | ||
691 | return 0; | |
692 | } | |
693 | ||
694 | static void delete_attr_set(struct attribute_set* s, struct kobject *kobj) | |
695 | { | |
696 | sysfs_remove_group(kobj, &s->group); | |
697 | destroy_attr_set(s); | |
698 | } | |
699 | ||
700 | static int parse_strtoul(const char *buf, | |
701 | unsigned long max, unsigned long *value) | |
702 | { | |
703 | char *endp; | |
704 | ||
705 | *value = simple_strtoul(buf, &endp, 0); | |
706 | while (*endp && isspace(*endp)) | |
707 | endp++; | |
708 | if (*endp || *value > max) | |
709 | return -EINVAL; | |
710 | ||
711 | return 0; | |
712 | } | |
713 | ||
56b6aeb0 HMH |
714 | /**************************************************************************** |
715 | **************************************************************************** | |
716 | * | |
717 | * Subdrivers | |
718 | * | |
719 | **************************************************************************** | |
720 | ****************************************************************************/ | |
721 | ||
722 | /************************************************************************* | |
142cfc90 | 723 | * thinkpad-acpi init subdriver |
56b6aeb0 HMH |
724 | */ |
725 | ||
a5763f22 | 726 | static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm) |
1da177e4 LT |
727 | { |
728 | printk(IBM_INFO "%s v%s\n", IBM_DESC, IBM_VERSION); | |
729 | printk(IBM_INFO "%s\n", IBM_URL); | |
730 | ||
d5a2f2f1 HMH |
731 | printk(IBM_INFO "ThinkPad BIOS %s, EC %s\n", |
732 | (thinkpad_id.bios_version_str) ? | |
733 | thinkpad_id.bios_version_str : "unknown", | |
734 | (thinkpad_id.ec_version_str) ? | |
735 | thinkpad_id.ec_version_str : "unknown"); | |
736 | ||
737 | if (thinkpad_id.vendor && thinkpad_id.model_str) | |
738 | printk(IBM_INFO "%s %s\n", | |
739 | (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ? | |
740 | "IBM" : ((thinkpad_id.vendor == | |
741 | PCI_VENDOR_ID_LENOVO) ? | |
742 | "Lenovo" : "Unknown vendor"), | |
743 | thinkpad_id.model_str); | |
3945ac36 | 744 | |
1da177e4 LT |
745 | return 0; |
746 | } | |
747 | ||
643f12db | 748 | static int thinkpad_acpi_driver_read(char *p) |
1da177e4 LT |
749 | { |
750 | int len = 0; | |
751 | ||
752 | len += sprintf(p + len, "driver:\t\t%s\n", IBM_DESC); | |
753 | len += sprintf(p + len, "version:\t%s\n", IBM_VERSION); | |
754 | ||
755 | return len; | |
756 | } | |
757 | ||
a5763f22 HMH |
758 | static struct ibm_struct thinkpad_acpi_driver_data = { |
759 | .name = "driver", | |
760 | .read = thinkpad_acpi_driver_read, | |
761 | }; | |
762 | ||
56b6aeb0 HMH |
763 | /************************************************************************* |
764 | * Hotkey subdriver | |
765 | */ | |
766 | ||
78f81cc4 | 767 | static int hotkey_orig_status; |
ae92bd17 | 768 | static u32 hotkey_orig_mask; |
9b010de5 | 769 | static u32 hotkey_all_mask; |
6a38abbf HMH |
770 | static u32 hotkey_reserved_mask; |
771 | ||
edf0e0e5 | 772 | static u16 *hotkey_keycode_map; |
78f81cc4 | 773 | |
94954cc6 | 774 | static struct attribute_set *hotkey_dev_attributes; |
a0416420 | 775 | |
74941a69 HMH |
776 | static int hotkey_get_wlsw(int *status) |
777 | { | |
778 | if (!acpi_evalf(hkey_handle, status, "WLSW", "d")) | |
779 | return -EIO; | |
780 | return 0; | |
781 | } | |
782 | ||
a0416420 HMH |
783 | /* sysfs hotkey enable ------------------------------------------------- */ |
784 | static ssize_t hotkey_enable_show(struct device *dev, | |
785 | struct device_attribute *attr, | |
786 | char *buf) | |
787 | { | |
ae92bd17 HMH |
788 | int res, status; |
789 | u32 mask; | |
a0416420 HMH |
790 | |
791 | res = hotkey_get(&status, &mask); | |
792 | if (res) | |
793 | return res; | |
794 | ||
795 | return snprintf(buf, PAGE_SIZE, "%d\n", status); | |
796 | } | |
797 | ||
798 | static ssize_t hotkey_enable_store(struct device *dev, | |
799 | struct device_attribute *attr, | |
800 | const char *buf, size_t count) | |
801 | { | |
802 | unsigned long t; | |
ae92bd17 HMH |
803 | int res, status; |
804 | u32 mask; | |
a0416420 HMH |
805 | |
806 | if (parse_strtoul(buf, 1, &t)) | |
807 | return -EINVAL; | |
808 | ||
809 | res = hotkey_get(&status, &mask); | |
810 | if (!res) | |
811 | res = hotkey_set(t, mask); | |
812 | ||
813 | return (res) ? res : count; | |
814 | } | |
815 | ||
816 | static struct device_attribute dev_attr_hotkey_enable = | |
cc4c24e1 | 817 | __ATTR(hotkey_enable, S_IWUSR | S_IRUGO, |
a0416420 HMH |
818 | hotkey_enable_show, hotkey_enable_store); |
819 | ||
820 | /* sysfs hotkey mask --------------------------------------------------- */ | |
821 | static ssize_t hotkey_mask_show(struct device *dev, | |
822 | struct device_attribute *attr, | |
823 | char *buf) | |
824 | { | |
ae92bd17 HMH |
825 | int res, status; |
826 | u32 mask; | |
a0416420 HMH |
827 | |
828 | res = hotkey_get(&status, &mask); | |
829 | if (res) | |
830 | return res; | |
831 | ||
ae92bd17 | 832 | return snprintf(buf, PAGE_SIZE, "0x%08x\n", mask); |
a0416420 HMH |
833 | } |
834 | ||
835 | static ssize_t hotkey_mask_store(struct device *dev, | |
836 | struct device_attribute *attr, | |
837 | const char *buf, size_t count) | |
838 | { | |
839 | unsigned long t; | |
ae92bd17 HMH |
840 | int res, status; |
841 | u32 mask; | |
a0416420 | 842 | |
ae92bd17 | 843 | if (parse_strtoul(buf, 0xffffffffUL, &t)) |
a0416420 HMH |
844 | return -EINVAL; |
845 | ||
846 | res = hotkey_get(&status, &mask); | |
847 | if (!res) | |
848 | hotkey_set(status, t); | |
849 | ||
850 | return (res) ? res : count; | |
851 | } | |
852 | ||
853 | static struct device_attribute dev_attr_hotkey_mask = | |
cc4c24e1 | 854 | __ATTR(hotkey_mask, S_IWUSR | S_IRUGO, |
a0416420 HMH |
855 | hotkey_mask_show, hotkey_mask_store); |
856 | ||
857 | /* sysfs hotkey bios_enabled ------------------------------------------- */ | |
858 | static ssize_t hotkey_bios_enabled_show(struct device *dev, | |
859 | struct device_attribute *attr, | |
860 | char *buf) | |
861 | { | |
862 | return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status); | |
863 | } | |
864 | ||
865 | static struct device_attribute dev_attr_hotkey_bios_enabled = | |
cc4c24e1 | 866 | __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL); |
a0416420 HMH |
867 | |
868 | /* sysfs hotkey bios_mask ---------------------------------------------- */ | |
869 | static ssize_t hotkey_bios_mask_show(struct device *dev, | |
870 | struct device_attribute *attr, | |
871 | char *buf) | |
872 | { | |
ae92bd17 | 873 | return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask); |
a0416420 HMH |
874 | } |
875 | ||
876 | static struct device_attribute dev_attr_hotkey_bios_mask = | |
cc4c24e1 | 877 | __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL); |
a0416420 | 878 | |
9b010de5 HMH |
879 | /* sysfs hotkey all_mask ----------------------------------------------- */ |
880 | static ssize_t hotkey_all_mask_show(struct device *dev, | |
881 | struct device_attribute *attr, | |
882 | char *buf) | |
883 | { | |
884 | return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_all_mask); | |
885 | } | |
886 | ||
887 | static struct device_attribute dev_attr_hotkey_all_mask = | |
888 | __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL); | |
889 | ||
890 | /* sysfs hotkey recommended_mask --------------------------------------- */ | |
891 | static ssize_t hotkey_recommended_mask_show(struct device *dev, | |
892 | struct device_attribute *attr, | |
893 | char *buf) | |
894 | { | |
895 | return snprintf(buf, PAGE_SIZE, "0x%08x\n", | |
896 | hotkey_all_mask & ~hotkey_reserved_mask); | |
897 | } | |
898 | ||
899 | static struct device_attribute dev_attr_hotkey_recommended_mask = | |
900 | __ATTR(hotkey_recommended_mask, S_IRUGO, | |
901 | hotkey_recommended_mask_show, NULL); | |
902 | ||
74941a69 HMH |
903 | /* sysfs hotkey radio_sw ----------------------------------------------- */ |
904 | static ssize_t hotkey_radio_sw_show(struct device *dev, | |
905 | struct device_attribute *attr, | |
906 | char *buf) | |
907 | { | |
908 | int res, s; | |
909 | res = hotkey_get_wlsw(&s); | |
910 | if (res < 0) | |
911 | return res; | |
912 | ||
913 | return snprintf(buf, PAGE_SIZE, "%d\n", !!s); | |
914 | } | |
915 | ||
916 | static struct device_attribute dev_attr_hotkey_radio_sw = | |
917 | __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL); | |
918 | ||
ff80f137 HMH |
919 | /* sysfs hotkey report_mode -------------------------------------------- */ |
920 | static ssize_t hotkey_report_mode_show(struct device *dev, | |
921 | struct device_attribute *attr, | |
922 | char *buf) | |
923 | { | |
924 | return snprintf(buf, PAGE_SIZE, "%d\n", | |
925 | (hotkey_report_mode != 0) ? hotkey_report_mode : 1); | |
926 | } | |
927 | ||
928 | static struct device_attribute dev_attr_hotkey_report_mode = | |
929 | __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL); | |
930 | ||
a0416420 HMH |
931 | /* --------------------------------------------------------------------- */ |
932 | ||
ff80f137 HMH |
933 | static struct attribute *hotkey_attributes[] __initdata = { |
934 | &dev_attr_hotkey_enable.attr, | |
935 | &dev_attr_hotkey_report_mode.attr, | |
936 | }; | |
937 | ||
938 | static struct attribute *hotkey_mask_attributes[] __initdata = { | |
a0416420 HMH |
939 | &dev_attr_hotkey_mask.attr, |
940 | &dev_attr_hotkey_bios_enabled.attr, | |
941 | &dev_attr_hotkey_bios_mask.attr, | |
9b010de5 HMH |
942 | &dev_attr_hotkey_all_mask.attr, |
943 | &dev_attr_hotkey_recommended_mask.attr, | |
a0416420 HMH |
944 | }; |
945 | ||
a5763f22 | 946 | static int __init hotkey_init(struct ibm_init_struct *iibm) |
56b6aeb0 | 947 | { |
edf0e0e5 HMH |
948 | |
949 | static u16 ibm_keycode_map[] __initdata = { | |
950 | /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */ | |
951 | KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP, | |
952 | KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8, | |
953 | KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND, | |
954 | /* Scan codes 0x0C to 0x0F: Other ACPI HKEY hot keys */ | |
955 | KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */ | |
956 | KEY_UNKNOWN, /* 0x0D: FN+INSERT */ | |
957 | KEY_UNKNOWN, /* 0x0E: FN+DELETE */ | |
958 | KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */ | |
959 | /* Scan codes 0x10 to 0x1F: Extended ACPI HKEY hot keys */ | |
960 | KEY_RESERVED, /* 0x10: FN+END (brightness down) */ | |
961 | KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */ | |
962 | KEY_UNKNOWN, /* 0x12: FN+PGDOWN */ | |
963 | KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */ | |
964 | KEY_RESERVED, /* 0x14: VOLUME UP */ | |
965 | KEY_RESERVED, /* 0x15: VOLUME DOWN */ | |
966 | KEY_RESERVED, /* 0x16: MUTE */ | |
967 | KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */ | |
968 | /* (assignments unknown, please report if found) */ | |
969 | KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, | |
970 | KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, | |
971 | }; | |
972 | static u16 lenovo_keycode_map[] __initdata = { | |
973 | /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */ | |
974 | KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP, | |
975 | KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8, | |
976 | KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND, | |
977 | /* Scan codes 0x0C to 0x0F: Other ACPI HKEY hot keys */ | |
978 | KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */ | |
979 | KEY_UNKNOWN, /* 0x0D: FN+INSERT */ | |
980 | KEY_UNKNOWN, /* 0x0E: FN+DELETE */ | |
981 | KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */ | |
982 | /* Scan codes 0x10 to 0x1F: Extended ACPI HKEY hot keys */ | |
983 | KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */ | |
984 | KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */ | |
985 | KEY_UNKNOWN, /* 0x12: FN+PGDOWN */ | |
986 | KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */ | |
987 | KEY_RESERVED, /* 0x14: VOLUME UP */ | |
988 | KEY_RESERVED, /* 0x15: VOLUME DOWN */ | |
989 | KEY_RESERVED, /* 0x16: MUTE */ | |
990 | KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */ | |
991 | /* (assignments unknown, please report if found) */ | |
992 | KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, | |
993 | KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, | |
994 | }; | |
995 | ||
996 | #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map) | |
997 | #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map) | |
998 | #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0]) | |
999 | ||
6a38abbf | 1000 | int res, i; |
74941a69 | 1001 | int status; |
b86c4722 | 1002 | |
fe08bc4b HMH |
1003 | vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n"); |
1004 | ||
6a38abbf HMH |
1005 | BUG_ON(!tpacpi_inputdev); |
1006 | ||
8d376cd6 | 1007 | IBM_ACPIHANDLE_INIT(hkey); |
40ca9fdf | 1008 | mutex_init(&hotkey_mutex); |
5fba344c | 1009 | |
56b6aeb0 | 1010 | /* hotkey not supported on 570 */ |
d8fd94d9 | 1011 | tp_features.hotkey = hkey_handle != NULL; |
56b6aeb0 | 1012 | |
fe08bc4b | 1013 | vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n", |
d8fd94d9 | 1014 | str_supported(tp_features.hotkey)); |
fe08bc4b | 1015 | |
d8fd94d9 | 1016 | if (tp_features.hotkey) { |
ff80f137 | 1017 | hotkey_dev_attributes = create_attr_set(8, NULL); |
a0416420 HMH |
1018 | if (!hotkey_dev_attributes) |
1019 | return -ENOMEM; | |
ff80f137 HMH |
1020 | res = add_many_to_attr_set(hotkey_dev_attributes, |
1021 | hotkey_attributes, | |
1022 | ARRAY_SIZE(hotkey_attributes)); | |
a0416420 HMH |
1023 | if (res) |
1024 | return res; | |
1025 | ||
56b6aeb0 HMH |
1026 | /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p, |
1027 | A30, R30, R31, T20-22, X20-21, X22-24 */ | |
d8fd94d9 HMH |
1028 | tp_features.hotkey_mask = |
1029 | acpi_evalf(hkey_handle, NULL, "DHKN", "qv"); | |
56b6aeb0 | 1030 | |
fe08bc4b | 1031 | vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n", |
d8fd94d9 | 1032 | str_supported(tp_features.hotkey_mask)); |
fe08bc4b | 1033 | |
9b010de5 HMH |
1034 | if (tp_features.hotkey_mask) { |
1035 | /* MHKA available in A31, R40, R40e, T4x, X31, and later */ | |
1036 | if (!acpi_evalf(hkey_handle, &hotkey_all_mask, | |
1037 | "MHKA", "qd")) | |
1038 | hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */ | |
1039 | } | |
1040 | ||
b86c4722 | 1041 | res = hotkey_get(&hotkey_orig_status, &hotkey_orig_mask); |
a0416420 HMH |
1042 | if (!res && tp_features.hotkey_mask) { |
1043 | res = add_many_to_attr_set(hotkey_dev_attributes, | |
1044 | hotkey_mask_attributes, | |
1045 | ARRAY_SIZE(hotkey_mask_attributes)); | |
1046 | } | |
74941a69 HMH |
1047 | |
1048 | /* Not all thinkpads have a hardware radio switch */ | |
1049 | if (!res && acpi_evalf(hkey_handle, &status, "WLSW", "qd")) { | |
1050 | tp_features.hotkey_wlsw = 1; | |
1051 | printk(IBM_INFO | |
1052 | "radio switch found; radios are %s\n", | |
1053 | enabled(status, 0)); | |
1054 | res = add_to_attr_set(hotkey_dev_attributes, | |
1055 | &dev_attr_hotkey_radio_sw.attr); | |
1056 | } | |
1057 | ||
a0416420 HMH |
1058 | if (!res) |
1059 | res = register_attr_set_with_sysfs( | |
1060 | hotkey_dev_attributes, | |
1061 | &tpacpi_pdev->dev.kobj); | |
b86c4722 HMH |
1062 | if (res) |
1063 | return res; | |
6a38abbf | 1064 | |
edf0e0e5 HMH |
1065 | /* Set up key map */ |
1066 | ||
1067 | hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE, | |
1068 | GFP_KERNEL); | |
1069 | if (!hotkey_keycode_map) { | |
1070 | printk(IBM_ERR "failed to allocate memory for key map\n"); | |
1071 | return -ENOMEM; | |
1072 | } | |
1073 | ||
1074 | if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) { | |
1075 | dbg_printk(TPACPI_DBG_INIT, | |
1076 | "using Lenovo default hot key map\n"); | |
1077 | memcpy(hotkey_keycode_map, &lenovo_keycode_map, | |
1078 | TPACPI_HOTKEY_MAP_SIZE); | |
1079 | } else { | |
1080 | dbg_printk(TPACPI_DBG_INIT, | |
1081 | "using IBM default hot key map\n"); | |
1082 | memcpy(hotkey_keycode_map, &ibm_keycode_map, | |
1083 | TPACPI_HOTKEY_MAP_SIZE); | |
1084 | } | |
1085 | ||
6a38abbf HMH |
1086 | set_bit(EV_KEY, tpacpi_inputdev->evbit); |
1087 | set_bit(EV_MSC, tpacpi_inputdev->evbit); | |
1088 | set_bit(MSC_SCAN, tpacpi_inputdev->mscbit); | |
edf0e0e5 HMH |
1089 | tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE; |
1090 | tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN; | |
1091 | tpacpi_inputdev->keycode = hotkey_keycode_map; | |
1092 | for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) { | |
6a38abbf HMH |
1093 | if (hotkey_keycode_map[i] != KEY_RESERVED) { |
1094 | set_bit(hotkey_keycode_map[i], | |
1095 | tpacpi_inputdev->keybit); | |
1096 | } else { | |
1097 | if (i < sizeof(hotkey_reserved_mask)*8) | |
1098 | hotkey_reserved_mask |= 1 << i; | |
1099 | } | |
1100 | } | |
1101 | ||
5c29d58f HMH |
1102 | if (tp_features.hotkey_wlsw) { |
1103 | set_bit(EV_SW, tpacpi_inputdev->evbit); | |
1104 | set_bit(SW_RADIO, tpacpi_inputdev->swbit); | |
1105 | } | |
1106 | ||
1a343760 HMH |
1107 | dbg_printk(TPACPI_DBG_INIT, |
1108 | "enabling hot key handling\n"); | |
1109 | res = hotkey_set(1, (hotkey_all_mask & ~hotkey_reserved_mask) | |
1110 | | hotkey_orig_mask); | |
1111 | if (res) | |
1112 | return res; | |
ff80f137 HMH |
1113 | |
1114 | dbg_printk(TPACPI_DBG_INIT, | |
1115 | "legacy hot key reporting over procfs %s\n", | |
1116 | (hotkey_report_mode < 2) ? | |
1117 | "enabled" : "disabled"); | |
56b6aeb0 HMH |
1118 | } |
1119 | ||
d8fd94d9 | 1120 | return (tp_features.hotkey)? 0 : 1; |
56b6aeb0 HMH |
1121 | } |
1122 | ||
1123 | static void hotkey_exit(void) | |
1124 | { | |
b86c4722 HMH |
1125 | int res; |
1126 | ||
d8fd94d9 | 1127 | if (tp_features.hotkey) { |
fe08bc4b | 1128 | dbg_printk(TPACPI_DBG_EXIT, "restoring original hotkey mask\n"); |
b86c4722 HMH |
1129 | res = hotkey_set(hotkey_orig_status, hotkey_orig_mask); |
1130 | if (res) | |
1131 | printk(IBM_ERR "failed to restore hotkey to BIOS defaults\n"); | |
5fba344c | 1132 | } |
a0416420 HMH |
1133 | |
1134 | if (hotkey_dev_attributes) { | |
1135 | delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj); | |
1136 | hotkey_dev_attributes = NULL; | |
1137 | } | |
56b6aeb0 HMH |
1138 | } |
1139 | ||
6a38abbf HMH |
1140 | static void tpacpi_input_send_key(unsigned int scancode, |
1141 | unsigned int keycode) | |
1142 | { | |
1143 | if (keycode != KEY_RESERVED) { | |
8523ed6f HMH |
1144 | mutex_lock(&tpacpi_inputdev_send_mutex); |
1145 | ||
6a38abbf HMH |
1146 | input_report_key(tpacpi_inputdev, keycode, 1); |
1147 | if (keycode == KEY_UNKNOWN) | |
1148 | input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, | |
1149 | scancode); | |
1150 | input_sync(tpacpi_inputdev); | |
1151 | ||
1152 | input_report_key(tpacpi_inputdev, keycode, 0); | |
1153 | if (keycode == KEY_UNKNOWN) | |
1154 | input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, | |
1155 | scancode); | |
1156 | input_sync(tpacpi_inputdev); | |
8523ed6f HMH |
1157 | |
1158 | mutex_unlock(&tpacpi_inputdev_send_mutex); | |
6a38abbf HMH |
1159 | } |
1160 | } | |
1161 | ||
5c29d58f HMH |
1162 | static void tpacpi_input_send_radiosw(void) |
1163 | { | |
1164 | int wlsw; | |
1165 | ||
8523ed6f HMH |
1166 | mutex_lock(&tpacpi_inputdev_send_mutex); |
1167 | ||
f4e1e43c | 1168 | if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) { |
5c29d58f HMH |
1169 | input_report_switch(tpacpi_inputdev, |
1170 | SW_RADIO, !!wlsw); | |
f4e1e43c HMH |
1171 | input_sync(tpacpi_inputdev); |
1172 | } | |
8523ed6f HMH |
1173 | |
1174 | mutex_unlock(&tpacpi_inputdev_send_mutex); | |
5c29d58f HMH |
1175 | } |
1176 | ||
56b6aeb0 HMH |
1177 | static void hotkey_notify(struct ibm_struct *ibm, u32 event) |
1178 | { | |
6a38abbf HMH |
1179 | u32 hkey; |
1180 | unsigned int keycode, scancode; | |
ff80f137 | 1181 | int send_acpi_ev = 0; |
56b6aeb0 | 1182 | |
ae92bd17 | 1183 | if (event == 0x80 && acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) { |
ff80f137 HMH |
1184 | switch (hkey >> 12) { |
1185 | case 1: | |
1186 | /* 0x1000-0x1FFF: key presses */ | |
1187 | scancode = hkey & 0xfff; | |
1188 | if (scancode > 0 && scancode < 0x21) { | |
1189 | scancode--; | |
1190 | keycode = hotkey_keycode_map[scancode]; | |
1191 | tpacpi_input_send_key(scancode, keycode); | |
1192 | } else { | |
1193 | printk(IBM_ERR | |
1194 | "hotkey 0x%04x out of range for keyboard map\n", | |
1195 | hkey); | |
1196 | send_acpi_ev = 1; | |
1197 | } | |
1198 | break; | |
1199 | case 5: | |
1200 | /* 0x5000-0x5FFF: LID */ | |
1201 | /* we don't handle it through this path, just | |
1202 | * eat up known LID events */ | |
1203 | if (hkey != 0x5001 && hkey != 0x5002) { | |
1204 | printk(IBM_ERR | |
1205 | "unknown LID-related hotkey event: 0x%04x\n", | |
1206 | hkey); | |
1207 | send_acpi_ev = 1; | |
1208 | } | |
1209 | break; | |
1210 | case 7: | |
1211 | /* 0x7000-0x7FFF: misc */ | |
1212 | if (tp_features.hotkey_wlsw && hkey == 0x7000) { | |
1213 | tpacpi_input_send_radiosw(); | |
6a38abbf | 1214 | break; |
6a38abbf | 1215 | } |
ff80f137 HMH |
1216 | /* fallthrough to default */ |
1217 | default: | |
1218 | /* case 2: dock-related */ | |
1219 | /* 0x2305 - T43 waking up due to bay lever eject while aslept */ | |
1220 | /* case 3: ultra-bay related. maybe bay in dock? */ | |
1221 | /* 0x3003 - T43 after wake up by bay lever eject (0x2305) */ | |
1222 | printk(IBM_NOTICE "unhandled HKEY event 0x%04x\n", hkey); | |
1223 | send_acpi_ev = 1; | |
6a38abbf | 1224 | } |
ae92bd17 HMH |
1225 | } else { |
1226 | printk(IBM_ERR "unknown hotkey notification event %d\n", event); | |
ff80f137 HMH |
1227 | hkey = 0; |
1228 | send_acpi_ev = 1; | |
1229 | } | |
1230 | ||
1231 | /* Legacy events */ | |
1232 | if (send_acpi_ev || hotkey_report_mode < 2) | |
1233 | acpi_bus_generate_proc_event(ibm->acpi->device, event, hkey); | |
1234 | ||
1235 | /* netlink events */ | |
1236 | if (send_acpi_ev) { | |
1237 | acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class, | |
1238 | ibm->acpi->device->dev.bus_id, | |
1239 | event, hkey); | |
56b6aeb0 HMH |
1240 | } |
1241 | } | |
1242 | ||
5c29d58f HMH |
1243 | static void hotkey_resume(void) |
1244 | { | |
1245 | tpacpi_input_send_radiosw(); | |
1246 | } | |
1247 | ||
40ca9fdf HMH |
1248 | /* |
1249 | * Call with hotkey_mutex held | |
1250 | */ | |
ae92bd17 | 1251 | static int hotkey_get(int *status, u32 *mask) |
1da177e4 LT |
1252 | { |
1253 | if (!acpi_evalf(hkey_handle, status, "DHKC", "d")) | |
b86c4722 | 1254 | return -EIO; |
78f81cc4 | 1255 | |
d8fd94d9 | 1256 | if (tp_features.hotkey_mask) |
78f81cc4 | 1257 | if (!acpi_evalf(hkey_handle, mask, "DHKN", "d")) |
b86c4722 | 1258 | return -EIO; |
78f81cc4 | 1259 | |
b86c4722 | 1260 | return 0; |
1da177e4 LT |
1261 | } |
1262 | ||
40ca9fdf HMH |
1263 | /* |
1264 | * Call with hotkey_mutex held | |
1265 | */ | |
ae92bd17 | 1266 | static int hotkey_set(int status, u32 mask) |
1da177e4 LT |
1267 | { |
1268 | int i; | |
1269 | ||
1270 | if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status)) | |
b86c4722 | 1271 | return -EIO; |
1da177e4 | 1272 | |
d8fd94d9 | 1273 | if (tp_features.hotkey_mask) |
78f81cc4 BD |
1274 | for (i = 0; i < 32; i++) { |
1275 | int bit = ((1 << i) & mask) != 0; | |
1276 | if (!acpi_evalf(hkey_handle, | |
1277 | NULL, "MHKM", "vdd", i + 1, bit)) | |
b86c4722 | 1278 | return -EIO; |
78f81cc4 | 1279 | } |
1da177e4 | 1280 | |
b86c4722 | 1281 | return 0; |
1da177e4 LT |
1282 | } |
1283 | ||
a0416420 | 1284 | /* procfs -------------------------------------------------------------- */ |
78f81cc4 | 1285 | static int hotkey_read(char *p) |
1da177e4 | 1286 | { |
ae92bd17 HMH |
1287 | int res, status; |
1288 | u32 mask; | |
1da177e4 LT |
1289 | int len = 0; |
1290 | ||
d8fd94d9 | 1291 | if (!tp_features.hotkey) { |
78f81cc4 BD |
1292 | len += sprintf(p + len, "status:\t\tnot supported\n"); |
1293 | return len; | |
1294 | } | |
1295 | ||
40ca9fdf HMH |
1296 | res = mutex_lock_interruptible(&hotkey_mutex); |
1297 | if (res < 0) | |
1298 | return res; | |
b86c4722 | 1299 | res = hotkey_get(&status, &mask); |
40ca9fdf | 1300 | mutex_unlock(&hotkey_mutex); |
b86c4722 HMH |
1301 | if (res) |
1302 | return res; | |
1da177e4 LT |
1303 | |
1304 | len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0)); | |
d8fd94d9 | 1305 | if (tp_features.hotkey_mask) { |
ae92bd17 | 1306 | len += sprintf(p + len, "mask:\t\t0x%08x\n", mask); |
1da177e4 LT |
1307 | len += sprintf(p + len, |
1308 | "commands:\tenable, disable, reset, <mask>\n"); | |
1309 | } else { | |
1310 | len += sprintf(p + len, "mask:\t\tnot supported\n"); | |
1311 | len += sprintf(p + len, "commands:\tenable, disable, reset\n"); | |
1312 | } | |
1313 | ||
1314 | return len; | |
1315 | } | |
1316 | ||
78f81cc4 | 1317 | static int hotkey_write(char *buf) |
1da177e4 | 1318 | { |
ae92bd17 HMH |
1319 | int res, status; |
1320 | u32 mask; | |
1da177e4 LT |
1321 | char *cmd; |
1322 | int do_cmd = 0; | |
1323 | ||
d8fd94d9 | 1324 | if (!tp_features.hotkey) |
1da177e4 LT |
1325 | return -ENODEV; |
1326 | ||
40ca9fdf HMH |
1327 | res = mutex_lock_interruptible(&hotkey_mutex); |
1328 | if (res < 0) | |
1329 | return res; | |
1330 | ||
b86c4722 HMH |
1331 | res = hotkey_get(&status, &mask); |
1332 | if (res) | |
40ca9fdf | 1333 | goto errexit; |
78f81cc4 | 1334 | |
40ca9fdf | 1335 | res = 0; |
1da177e4 LT |
1336 | while ((cmd = next_cmd(&buf))) { |
1337 | if (strlencmp(cmd, "enable") == 0) { | |
1338 | status = 1; | |
1339 | } else if (strlencmp(cmd, "disable") == 0) { | |
1340 | status = 0; | |
1341 | } else if (strlencmp(cmd, "reset") == 0) { | |
78f81cc4 BD |
1342 | status = hotkey_orig_status; |
1343 | mask = hotkey_orig_mask; | |
1da177e4 LT |
1344 | } else if (sscanf(cmd, "0x%x", &mask) == 1) { |
1345 | /* mask set */ | |
1346 | } else if (sscanf(cmd, "%x", &mask) == 1) { | |
1347 | /* mask set */ | |
40ca9fdf HMH |
1348 | } else { |
1349 | res = -EINVAL; | |
1350 | goto errexit; | |
1351 | } | |
1da177e4 LT |
1352 | do_cmd = 1; |
1353 | } | |
1354 | ||
40ca9fdf | 1355 | if (do_cmd) |
b86c4722 | 1356 | res = hotkey_set(status, mask); |
1da177e4 | 1357 | |
40ca9fdf HMH |
1358 | errexit: |
1359 | mutex_unlock(&hotkey_mutex); | |
1360 | return res; | |
78f81cc4 | 1361 | } |
1da177e4 | 1362 | |
1ba90e3a TR |
1363 | static const struct acpi_device_id ibm_htk_device_ids[] = { |
1364 | {IBM_HKEY_HID, 0}, | |
1365 | {"", 0}, | |
1366 | }; | |
1367 | ||
8d376cd6 | 1368 | static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = { |
1ba90e3a | 1369 | .hid = ibm_htk_device_ids, |
8d376cd6 HMH |
1370 | .notify = hotkey_notify, |
1371 | .handle = &hkey_handle, | |
1372 | .type = ACPI_DEVICE_NOTIFY, | |
1373 | }; | |
1374 | ||
a5763f22 HMH |
1375 | static struct ibm_struct hotkey_driver_data = { |
1376 | .name = "hotkey", | |
a5763f22 HMH |
1377 | .read = hotkey_read, |
1378 | .write = hotkey_write, | |
1379 | .exit = hotkey_exit, | |
5c29d58f | 1380 | .resume = hotkey_resume, |
8d376cd6 | 1381 | .acpi = &ibm_hotkey_acpidriver, |
a5763f22 HMH |
1382 | }; |
1383 | ||
56b6aeb0 HMH |
1384 | /************************************************************************* |
1385 | * Bluetooth subdriver | |
1386 | */ | |
1da177e4 | 1387 | |
d3a6ade4 HMH |
1388 | /* sysfs bluetooth enable ---------------------------------------------- */ |
1389 | static ssize_t bluetooth_enable_show(struct device *dev, | |
1390 | struct device_attribute *attr, | |
1391 | char *buf) | |
1392 | { | |
1393 | int status; | |
1394 | ||
1395 | status = bluetooth_get_radiosw(); | |
1396 | if (status < 0) | |
1397 | return status; | |
1398 | ||
1399 | return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0); | |
1400 | } | |
1401 | ||
1402 | static ssize_t bluetooth_enable_store(struct device *dev, | |
1403 | struct device_attribute *attr, | |
1404 | const char *buf, size_t count) | |
1405 | { | |
1406 | unsigned long t; | |
1407 | int res; | |
1408 | ||
1409 | if (parse_strtoul(buf, 1, &t)) | |
1410 | return -EINVAL; | |
1411 | ||
1412 | res = bluetooth_set_radiosw(t); | |
1413 | ||
1414 | return (res) ? res : count; | |
1415 | } | |
1416 | ||
1417 | static struct device_attribute dev_attr_bluetooth_enable = | |
cc4c24e1 | 1418 | __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO, |
d3a6ade4 HMH |
1419 | bluetooth_enable_show, bluetooth_enable_store); |
1420 | ||
1421 | /* --------------------------------------------------------------------- */ | |
1422 | ||
1423 | static struct attribute *bluetooth_attributes[] = { | |
1424 | &dev_attr_bluetooth_enable.attr, | |
1425 | NULL | |
1426 | }; | |
1427 | ||
1428 | static const struct attribute_group bluetooth_attr_group = { | |
d3a6ade4 HMH |
1429 | .attrs = bluetooth_attributes, |
1430 | }; | |
1431 | ||
a5763f22 | 1432 | static int __init bluetooth_init(struct ibm_init_struct *iibm) |
1da177e4 | 1433 | { |
d3a6ade4 | 1434 | int res; |
d6fdd1e9 HMH |
1435 | int status = 0; |
1436 | ||
fe08bc4b HMH |
1437 | vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n"); |
1438 | ||
8d376cd6 | 1439 | IBM_ACPIHANDLE_INIT(hkey); |
5fba344c | 1440 | |
78f81cc4 BD |
1441 | /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p, |
1442 | G4x, R30, R31, R40e, R50e, T20-22, X20-21 */ | |
d8fd94d9 | 1443 | tp_features.bluetooth = hkey_handle && |
d6fdd1e9 HMH |
1444 | acpi_evalf(hkey_handle, &status, "GBDC", "qd"); |
1445 | ||
1446 | vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n", | |
1447 | str_supported(tp_features.bluetooth), | |
1448 | status); | |
1449 | ||
d3a6ade4 HMH |
1450 | if (tp_features.bluetooth) { |
1451 | if (!(status & TP_ACPI_BLUETOOTH_HWPRESENT)) { | |
1452 | /* no bluetooth hardware present in system */ | |
1453 | tp_features.bluetooth = 0; | |
1454 | dbg_printk(TPACPI_DBG_INIT, | |
1455 | "bluetooth hardware not installed\n"); | |
1456 | } else { | |
1457 | res = sysfs_create_group(&tpacpi_pdev->dev.kobj, | |
1458 | &bluetooth_attr_group); | |
1459 | if (res) | |
1460 | return res; | |
1461 | } | |
d6fdd1e9 | 1462 | } |
fe08bc4b | 1463 | |
d8fd94d9 | 1464 | return (tp_features.bluetooth)? 0 : 1; |
1da177e4 LT |
1465 | } |
1466 | ||
d3a6ade4 HMH |
1467 | static void bluetooth_exit(void) |
1468 | { | |
1469 | sysfs_remove_group(&tpacpi_pdev->dev.kobj, | |
1470 | &bluetooth_attr_group); | |
1471 | } | |
1472 | ||
d6fdd1e9 | 1473 | static int bluetooth_get_radiosw(void) |
1da177e4 LT |
1474 | { |
1475 | int status; | |
1476 | ||
d6fdd1e9 HMH |
1477 | if (!tp_features.bluetooth) |
1478 | return -ENODEV; | |
1da177e4 | 1479 | |
d6fdd1e9 HMH |
1480 | if (!acpi_evalf(hkey_handle, &status, "GBDC", "d")) |
1481 | return -EIO; | |
1482 | ||
1483 | return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0); | |
1484 | } | |
1485 | ||
1486 | static int bluetooth_set_radiosw(int radio_on) | |
1487 | { | |
1488 | int status; | |
1489 | ||
1490 | if (!tp_features.bluetooth) | |
1491 | return -ENODEV; | |
1492 | ||
1493 | if (!acpi_evalf(hkey_handle, &status, "GBDC", "d")) | |
1494 | return -EIO; | |
1495 | if (radio_on) | |
1496 | status |= TP_ACPI_BLUETOOTH_RADIOSSW; | |
1497 | else | |
1498 | status &= ~TP_ACPI_BLUETOOTH_RADIOSSW; | |
1499 | if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status)) | |
1500 | return -EIO; | |
1501 | ||
1502 | return 0; | |
1da177e4 LT |
1503 | } |
1504 | ||
d3a6ade4 | 1505 | /* procfs -------------------------------------------------------------- */ |
78f81cc4 | 1506 | static int bluetooth_read(char *p) |
1da177e4 LT |
1507 | { |
1508 | int len = 0; | |
d6fdd1e9 | 1509 | int status = bluetooth_get_radiosw(); |
1da177e4 | 1510 | |
d8fd94d9 | 1511 | if (!tp_features.bluetooth) |
1da177e4 | 1512 | len += sprintf(p + len, "status:\t\tnot supported\n"); |
1da177e4 | 1513 | else { |
d6fdd1e9 HMH |
1514 | len += sprintf(p + len, "status:\t\t%s\n", |
1515 | (status)? "enabled" : "disabled"); | |
1da177e4 LT |
1516 | len += sprintf(p + len, "commands:\tenable, disable\n"); |
1517 | } | |
1518 | ||
1519 | return len; | |
1520 | } | |
1521 | ||
78f81cc4 | 1522 | static int bluetooth_write(char *buf) |
1da177e4 | 1523 | { |
1da177e4 | 1524 | char *cmd; |
1da177e4 | 1525 | |
d8fd94d9 | 1526 | if (!tp_features.bluetooth) |
78f81cc4 | 1527 | return -ENODEV; |
1da177e4 LT |
1528 | |
1529 | while ((cmd = next_cmd(&buf))) { | |
1530 | if (strlencmp(cmd, "enable") == 0) { | |
d6fdd1e9 | 1531 | bluetooth_set_radiosw(1); |
1da177e4 | 1532 | } else if (strlencmp(cmd, "disable") == 0) { |
d6fdd1e9 | 1533 | bluetooth_set_radiosw(0); |
1da177e4 LT |
1534 | } else |
1535 | return -EINVAL; | |
1da177e4 LT |
1536 | } |
1537 | ||
1da177e4 LT |
1538 | return 0; |
1539 | } | |
1540 | ||
a5763f22 HMH |
1541 | static struct ibm_struct bluetooth_driver_data = { |
1542 | .name = "bluetooth", | |
1543 | .read = bluetooth_read, | |
1544 | .write = bluetooth_write, | |
d3a6ade4 | 1545 | .exit = bluetooth_exit, |
a5763f22 HMH |
1546 | }; |
1547 | ||
56b6aeb0 HMH |
1548 | /************************************************************************* |
1549 | * Wan subdriver | |
1550 | */ | |
1551 | ||
d3a6ade4 HMH |
1552 | /* sysfs wan enable ---------------------------------------------------- */ |
1553 | static ssize_t wan_enable_show(struct device *dev, | |
1554 | struct device_attribute *attr, | |
1555 | char *buf) | |
1556 | { | |
1557 | int status; | |
1558 | ||
1559 | status = wan_get_radiosw(); | |
1560 | if (status < 0) | |
1561 | return status; | |
1562 | ||
1563 | return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0); | |
1564 | } | |
1565 | ||
1566 | static ssize_t wan_enable_store(struct device *dev, | |
1567 | struct device_attribute *attr, | |
1568 | const char *buf, size_t count) | |
1569 | { | |
1570 | unsigned long t; | |
1571 | int res; | |
1572 | ||
1573 | if (parse_strtoul(buf, 1, &t)) | |
1574 | return -EINVAL; | |
1575 | ||
1576 | res = wan_set_radiosw(t); | |
1577 | ||
1578 | return (res) ? res : count; | |
1579 | } | |
1580 | ||
1581 | static struct device_attribute dev_attr_wan_enable = | |
cc4c24e1 | 1582 | __ATTR(wwan_enable, S_IWUSR | S_IRUGO, |
d3a6ade4 HMH |
1583 | wan_enable_show, wan_enable_store); |
1584 | ||
1585 | /* --------------------------------------------------------------------- */ | |
1586 | ||
1587 | static struct attribute *wan_attributes[] = { | |
1588 | &dev_attr_wan_enable.attr, | |
1589 | NULL | |
1590 | }; | |
1591 | ||
1592 | static const struct attribute_group wan_attr_group = { | |
d3a6ade4 HMH |
1593 | .attrs = wan_attributes, |
1594 | }; | |
1595 | ||
a5763f22 | 1596 | static int __init wan_init(struct ibm_init_struct *iibm) |
42adb53c | 1597 | { |
d3a6ade4 | 1598 | int res; |
d6fdd1e9 HMH |
1599 | int status = 0; |
1600 | ||
fe08bc4b HMH |
1601 | vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n"); |
1602 | ||
8d376cd6 | 1603 | IBM_ACPIHANDLE_INIT(hkey); |
5fba344c | 1604 | |
d8fd94d9 | 1605 | tp_features.wan = hkey_handle && |
d6fdd1e9 HMH |
1606 | acpi_evalf(hkey_handle, &status, "GWAN", "qd"); |
1607 | ||
1608 | vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n", | |
1609 | str_supported(tp_features.wan), | |
1610 | status); | |
1611 | ||
d3a6ade4 HMH |
1612 | if (tp_features.wan) { |
1613 | if (!(status & TP_ACPI_WANCARD_HWPRESENT)) { | |
1614 | /* no wan hardware present in system */ | |
1615 | tp_features.wan = 0; | |
1616 | dbg_printk(TPACPI_DBG_INIT, | |
1617 | "wan hardware not installed\n"); | |
1618 | } else { | |
1619 | res = sysfs_create_group(&tpacpi_pdev->dev.kobj, | |
1620 | &wan_attr_group); | |
1621 | if (res) | |
1622 | return res; | |
1623 | } | |
d6fdd1e9 | 1624 | } |
fe08bc4b | 1625 | |
d8fd94d9 | 1626 | return (tp_features.wan)? 0 : 1; |
42adb53c JF |
1627 | } |
1628 | ||
d3a6ade4 HMH |
1629 | static void wan_exit(void) |
1630 | { | |
1631 | sysfs_remove_group(&tpacpi_pdev->dev.kobj, | |
1632 | &wan_attr_group); | |
1633 | } | |
1634 | ||
d6fdd1e9 | 1635 | static int wan_get_radiosw(void) |
42adb53c JF |
1636 | { |
1637 | int status; | |
1638 | ||
d6fdd1e9 HMH |
1639 | if (!tp_features.wan) |
1640 | return -ENODEV; | |
42adb53c | 1641 | |
d6fdd1e9 HMH |
1642 | if (!acpi_evalf(hkey_handle, &status, "GWAN", "d")) |
1643 | return -EIO; | |
1644 | ||
1645 | return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0); | |
1646 | } | |
1647 | ||
1648 | static int wan_set_radiosw(int radio_on) | |
1649 | { | |
1650 | int status; | |
1651 | ||
1652 | if (!tp_features.wan) | |
1653 | return -ENODEV; | |
1654 | ||
1655 | if (!acpi_evalf(hkey_handle, &status, "GWAN", "d")) | |
1656 | return -EIO; | |
1657 | if (radio_on) | |
1658 | status |= TP_ACPI_WANCARD_RADIOSSW; | |
1659 | else | |
1660 | status &= ~TP_ACPI_WANCARD_RADIOSSW; | |
1661 | if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status)) | |
1662 | return -EIO; | |
1663 | ||
1664 | return 0; | |
42adb53c JF |
1665 | } |
1666 | ||
d3a6ade4 | 1667 | /* procfs -------------------------------------------------------------- */ |
42adb53c JF |
1668 | static int wan_read(char *p) |
1669 | { | |
1670 | int len = 0; | |
d6fdd1e9 | 1671 | int status = wan_get_radiosw(); |
42adb53c | 1672 | |
d8fd94d9 | 1673 | if (!tp_features.wan) |
42adb53c | 1674 | len += sprintf(p + len, "status:\t\tnot supported\n"); |
42adb53c | 1675 | else { |
d6fdd1e9 HMH |
1676 | len += sprintf(p + len, "status:\t\t%s\n", |
1677 | (status)? "enabled" : "disabled"); | |
42adb53c JF |
1678 | len += sprintf(p + len, "commands:\tenable, disable\n"); |
1679 | } | |
1680 | ||
1681 | return len; | |
1682 | } | |
1683 | ||
1684 | static int wan_write(char *buf) | |
1685 | { | |
42adb53c | 1686 | char *cmd; |
42adb53c | 1687 | |
d8fd94d9 | 1688 | if (!tp_features.wan) |
42adb53c JF |
1689 | return -ENODEV; |
1690 | ||
1691 | while ((cmd = next_cmd(&buf))) { | |
1692 | if (strlencmp(cmd, "enable") == 0) { | |
d6fdd1e9 | 1693 | wan_set_radiosw(1); |
42adb53c | 1694 | } else if (strlencmp(cmd, "disable") == 0) { |
d6fdd1e9 | 1695 | wan_set_radiosw(0); |
42adb53c JF |
1696 | } else |
1697 | return -EINVAL; | |
42adb53c JF |
1698 | } |
1699 | ||
42adb53c JF |
1700 | return 0; |
1701 | } | |
1702 | ||
a5763f22 HMH |
1703 | static struct ibm_struct wan_driver_data = { |
1704 | .name = "wan", | |
1705 | .read = wan_read, | |
1706 | .write = wan_write, | |
d3a6ade4 | 1707 | .exit = wan_exit, |
92641177 | 1708 | .flags.experimental = 1, |
a5763f22 HMH |
1709 | }; |
1710 | ||
56b6aeb0 HMH |
1711 | /************************************************************************* |
1712 | * Video subdriver | |
1713 | */ | |
1714 | ||
9a8e1738 HMH |
1715 | static enum video_access_mode video_supported; |
1716 | static int video_orig_autosw; | |
78f81cc4 | 1717 | |
56b6aeb0 HMH |
1718 | IBM_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */ |
1719 | "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */ | |
1720 | "\\_SB.PCI0.VID0", /* 770e */ | |
1721 | "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */ | |
1722 | "\\_SB.PCI0.AGP.VID", /* all others */ | |
1723 | ); /* R30, R31 */ | |
1724 | ||
1725 | IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */ | |
1726 | ||
a5763f22 | 1727 | static int __init video_init(struct ibm_init_struct *iibm) |
1da177e4 | 1728 | { |
78f81cc4 BD |
1729 | int ivga; |
1730 | ||
fe08bc4b HMH |
1731 | vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n"); |
1732 | ||
8d376cd6 HMH |
1733 | IBM_ACPIHANDLE_INIT(vid); |
1734 | IBM_ACPIHANDLE_INIT(vid2); | |
5fba344c | 1735 | |
78f81cc4 BD |
1736 | if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga) |
1737 | /* G41, assume IVGA doesn't change */ | |
1738 | vid_handle = vid2_handle; | |
1739 | ||
1740 | if (!vid_handle) | |
1741 | /* video switching not supported on R30, R31 */ | |
efa27145 | 1742 | video_supported = TPACPI_VIDEO_NONE; |
78f81cc4 BD |
1743 | else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd")) |
1744 | /* 570 */ | |
efa27145 | 1745 | video_supported = TPACPI_VIDEO_570; |
78f81cc4 BD |
1746 | else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd")) |
1747 | /* 600e/x, 770e, 770x */ | |
efa27145 | 1748 | video_supported = TPACPI_VIDEO_770; |
78f81cc4 BD |
1749 | else |
1750 | /* all others */ | |
efa27145 | 1751 | video_supported = TPACPI_VIDEO_NEW; |
1da177e4 | 1752 | |
fe08bc4b HMH |
1753 | vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n", |
1754 | str_supported(video_supported != TPACPI_VIDEO_NONE), | |
1755 | video_supported); | |
1756 | ||
5fba344c | 1757 | return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1; |
1da177e4 LT |
1758 | } |
1759 | ||
56b6aeb0 HMH |
1760 | static void video_exit(void) |
1761 | { | |
83f34724 HMH |
1762 | dbg_printk(TPACPI_DBG_EXIT, |
1763 | "restoring original video autoswitch mode\n"); | |
1764 | if (video_autosw_set(video_orig_autosw)) | |
1765 | printk(IBM_ERR "error while trying to restore original " | |
1766 | "video autoswitch mode\n"); | |
56b6aeb0 HMH |
1767 | } |
1768 | ||
83f34724 | 1769 | static int video_outputsw_get(void) |
1da177e4 LT |
1770 | { |
1771 | int status = 0; | |
1772 | int i; | |
1773 | ||
83f34724 HMH |
1774 | switch (video_supported) { |
1775 | case TPACPI_VIDEO_570: | |
1776 | if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd", | |
1777 | TP_ACPI_VIDEO_570_PHSCMD)) | |
1778 | return -EIO; | |
1779 | status = i & TP_ACPI_VIDEO_570_PHSMASK; | |
1780 | break; | |
1781 | case TPACPI_VIDEO_770: | |
1782 | if (!acpi_evalf(NULL, &i, "\\VCDL", "d")) | |
1783 | return -EIO; | |
1784 | if (i) | |
1785 | status |= TP_ACPI_VIDEO_S_LCD; | |
1786 | if (!acpi_evalf(NULL, &i, "\\VCDC", "d")) | |
1787 | return -EIO; | |
1788 | if (i) | |
1789 | status |= TP_ACPI_VIDEO_S_CRT; | |
1790 | break; | |
1791 | case TPACPI_VIDEO_NEW: | |
1792 | if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) || | |
1793 | !acpi_evalf(NULL, &i, "\\VCDC", "d")) | |
1794 | return -EIO; | |
1795 | if (i) | |
1796 | status |= TP_ACPI_VIDEO_S_CRT; | |
1797 | ||
1798 | if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) || | |
1799 | !acpi_evalf(NULL, &i, "\\VCDL", "d")) | |
1800 | return -EIO; | |
1801 | if (i) | |
1802 | status |= TP_ACPI_VIDEO_S_LCD; | |
1803 | if (!acpi_evalf(NULL, &i, "\\VCDD", "d")) | |
1804 | return -EIO; | |
1805 | if (i) | |
1806 | status |= TP_ACPI_VIDEO_S_DVI; | |
1807 | break; | |
1808 | default: | |
1809 | return -ENOSYS; | |
78f81cc4 BD |
1810 | } |
1811 | ||
1812 | return status; | |
1813 | } | |
1da177e4 | 1814 | |
83f34724 | 1815 | static int video_outputsw_set(int status) |
78f81cc4 | 1816 | { |
83f34724 HMH |
1817 | int autosw; |
1818 | int res = 0; | |
1819 | ||
1820 | switch (video_supported) { | |
1821 | case TPACPI_VIDEO_570: | |
1822 | res = acpi_evalf(NULL, NULL, | |
1823 | "\\_SB.PHS2", "vdd", | |
1824 | TP_ACPI_VIDEO_570_PHS2CMD, | |
1825 | status | TP_ACPI_VIDEO_570_PHS2SET); | |
1826 | break; | |
1827 | case TPACPI_VIDEO_770: | |
1828 | autosw = video_autosw_get(); | |
1829 | if (autosw < 0) | |
1830 | return autosw; | |
1da177e4 | 1831 | |
83f34724 HMH |
1832 | res = video_autosw_set(1); |
1833 | if (res) | |
1834 | return res; | |
1835 | res = acpi_evalf(vid_handle, NULL, | |
1836 | "ASWT", "vdd", status * 0x100, 0); | |
1837 | if (!autosw && video_autosw_set(autosw)) { | |
1838 | printk(IBM_ERR "video auto-switch left enabled due to error\n"); | |
1839 | return -EIO; | |
1840 | } | |
1841 | break; | |
1842 | case TPACPI_VIDEO_NEW: | |
1843 | res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) && | |
1844 | acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1); | |
1845 | break; | |
1846 | default: | |
1847 | return -ENOSYS; | |
1848 | } | |
1da177e4 | 1849 | |
83f34724 | 1850 | return (res)? 0 : -EIO; |
1da177e4 LT |
1851 | } |
1852 | ||
83f34724 | 1853 | static int video_autosw_get(void) |
78f81cc4 | 1854 | { |
83f34724 | 1855 | int autosw = 0; |
78f81cc4 | 1856 | |
83f34724 HMH |
1857 | switch (video_supported) { |
1858 | case TPACPI_VIDEO_570: | |
1859 | if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d")) | |
1860 | return -EIO; | |
1861 | break; | |
1862 | case TPACPI_VIDEO_770: | |
1863 | case TPACPI_VIDEO_NEW: | |
1864 | if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d")) | |
1865 | return -EIO; | |
1866 | break; | |
1867 | default: | |
1868 | return -ENOSYS; | |
1869 | } | |
78f81cc4 | 1870 | |
83f34724 | 1871 | return autosw & 1; |
78f81cc4 BD |
1872 | } |
1873 | ||
83f34724 | 1874 | static int video_autosw_set(int enable) |
78f81cc4 | 1875 | { |
83f34724 HMH |
1876 | if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0)) |
1877 | return -EIO; | |
1878 | return 0; | |
78f81cc4 BD |
1879 | } |
1880 | ||
83f34724 | 1881 | static int video_outputsw_cycle(void) |
78f81cc4 | 1882 | { |
83f34724 HMH |
1883 | int autosw = video_autosw_get(); |
1884 | int res; | |
78f81cc4 | 1885 | |
83f34724 HMH |
1886 | if (autosw < 0) |
1887 | return autosw; | |
78f81cc4 | 1888 | |
83f34724 HMH |
1889 | switch (video_supported) { |
1890 | case TPACPI_VIDEO_570: | |
1891 | res = video_autosw_set(1); | |
1892 | if (res) | |
1893 | return res; | |
1894 | res = acpi_evalf(ec_handle, NULL, "_Q16", "v"); | |
1895 | break; | |
1896 | case TPACPI_VIDEO_770: | |
1897 | case TPACPI_VIDEO_NEW: | |
1898 | res = video_autosw_set(1); | |
1899 | if (res) | |
1900 | return res; | |
1901 | res = acpi_evalf(vid_handle, NULL, "VSWT", "v"); | |
1902 | break; | |
1903 | default: | |
1904 | return -ENOSYS; | |
1905 | } | |
1906 | if (!autosw && video_autosw_set(autosw)) { | |
1907 | printk(IBM_ERR "video auto-switch left enabled due to error\n"); | |
1908 | return -EIO; | |
78f81cc4 BD |
1909 | } |
1910 | ||
83f34724 HMH |
1911 | return (res)? 0 : -EIO; |
1912 | } | |
1913 | ||
1914 | static int video_expand_toggle(void) | |
1915 | { | |
1916 | switch (video_supported) { | |
1917 | case TPACPI_VIDEO_570: | |
1918 | return acpi_evalf(ec_handle, NULL, "_Q17", "v")? | |
1919 | 0 : -EIO; | |
1920 | case TPACPI_VIDEO_770: | |
1921 | return acpi_evalf(vid_handle, NULL, "VEXP", "v")? | |
1922 | 0 : -EIO; | |
1923 | case TPACPI_VIDEO_NEW: | |
1924 | return acpi_evalf(NULL, NULL, "\\VEXP", "v")? | |
1925 | 0 : -EIO; | |
1926 | default: | |
1927 | return -ENOSYS; | |
1928 | } | |
1929 | /* not reached */ | |
78f81cc4 BD |
1930 | } |
1931 | ||
56b6aeb0 HMH |
1932 | static int video_read(char *p) |
1933 | { | |
83f34724 | 1934 | int status, autosw; |
56b6aeb0 HMH |
1935 | int len = 0; |
1936 | ||
83f34724 | 1937 | if (video_supported == TPACPI_VIDEO_NONE) { |
56b6aeb0 HMH |
1938 | len += sprintf(p + len, "status:\t\tnot supported\n"); |
1939 | return len; | |
1940 | } | |
1941 | ||
83f34724 HMH |
1942 | status = video_outputsw_get(); |
1943 | if (status < 0) | |
1944 | return status; | |
1945 | ||
1946 | autosw = video_autosw_get(); | |
1947 | if (autosw < 0) | |
1948 | return autosw; | |
1949 | ||
56b6aeb0 HMH |
1950 | len += sprintf(p + len, "status:\t\tsupported\n"); |
1951 | len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0)); | |
1952 | len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1)); | |
efa27145 | 1953 | if (video_supported == TPACPI_VIDEO_NEW) |
56b6aeb0 HMH |
1954 | len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3)); |
1955 | len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0)); | |
1956 | len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n"); | |
1957 | len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n"); | |
efa27145 | 1958 | if (video_supported == TPACPI_VIDEO_NEW) |
56b6aeb0 HMH |
1959 | len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n"); |
1960 | len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n"); | |
1961 | len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n"); | |
1962 | ||
1963 | return len; | |
1964 | } | |
1965 | ||
78f81cc4 | 1966 | static int video_write(char *buf) |
1da177e4 LT |
1967 | { |
1968 | char *cmd; | |
1969 | int enable, disable, status; | |
83f34724 | 1970 | int res; |
1da177e4 | 1971 | |
83f34724 | 1972 | if (video_supported == TPACPI_VIDEO_NONE) |
78f81cc4 BD |
1973 | return -ENODEV; |
1974 | ||
83f34724 HMH |
1975 | enable = 0; |
1976 | disable = 0; | |
1da177e4 LT |
1977 | |
1978 | while ((cmd = next_cmd(&buf))) { | |
1979 | if (strlencmp(cmd, "lcd_enable") == 0) { | |
83f34724 | 1980 | enable |= TP_ACPI_VIDEO_S_LCD; |
1da177e4 | 1981 | } else if (strlencmp(cmd, "lcd_disable") == 0) { |
83f34724 | 1982 | disable |= TP_ACPI_VIDEO_S_LCD; |
1da177e4 | 1983 | } else if (strlencmp(cmd, "crt_enable") == 0) { |
83f34724 | 1984 | enable |= TP_ACPI_VIDEO_S_CRT; |
1da177e4 | 1985 | } else if (strlencmp(cmd, "crt_disable") == 0) { |
83f34724 | 1986 | disable |= TP_ACPI_VIDEO_S_CRT; |
efa27145 | 1987 | } else if (video_supported == TPACPI_VIDEO_NEW && |
78f81cc4 | 1988 | strlencmp(cmd, "dvi_enable") == 0) { |
83f34724 | 1989 | enable |= TP_ACPI_VIDEO_S_DVI; |
efa27145 | 1990 | } else if (video_supported == TPACPI_VIDEO_NEW && |
78f81cc4 | 1991 | strlencmp(cmd, "dvi_disable") == 0) { |
83f34724 | 1992 | disable |= TP_ACPI_VIDEO_S_DVI; |
1da177e4 | 1993 | } else if (strlencmp(cmd, "auto_enable") == 0) { |
83f34724 HMH |
1994 | res = video_autosw_set(1); |
1995 | if (res) | |
1996 | return res; | |
1da177e4 | 1997 | } else if (strlencmp(cmd, "auto_disable") == 0) { |
83f34724 HMH |
1998 | res = video_autosw_set(0); |
1999 | if (res) | |
2000 | return res; | |
1da177e4 | 2001 | } else if (strlencmp(cmd, "video_switch") == 0) { |
83f34724 HMH |
2002 | res = video_outputsw_cycle(); |
2003 | if (res) | |
2004 | return res; | |
1da177e4 | 2005 | } else if (strlencmp(cmd, "expand_toggle") == 0) { |
83f34724 HMH |
2006 | res = video_expand_toggle(); |
2007 | if (res) | |
2008 | return res; | |
1da177e4 LT |
2009 | } else |
2010 | return -EINVAL; | |
2011 | } | |
2012 | ||
2013 | if (enable || disable) { | |
83f34724 HMH |
2014 | status = video_outputsw_get(); |
2015 | if (status < 0) | |
2016 | return status; | |
2017 | res = video_outputsw_set((status & ~disable) | enable); | |
2018 | if (res) | |
2019 | return res; | |
1da177e4 LT |
2020 | } |
2021 | ||
2022 | return 0; | |
2023 | } | |
2024 | ||
a5763f22 HMH |
2025 | static struct ibm_struct video_driver_data = { |
2026 | .name = "video", | |
2027 | .read = video_read, | |
2028 | .write = video_write, | |
2029 | .exit = video_exit, | |
2030 | }; | |
2031 | ||
56b6aeb0 HMH |
2032 | /************************************************************************* |
2033 | * Light (thinklight) subdriver | |
2034 | */ | |
1da177e4 | 2035 | |
56b6aeb0 HMH |
2036 | IBM_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */ |
2037 | IBM_HANDLE(ledb, ec, "LEDB"); /* G4x */ | |
2038 | ||
a5763f22 | 2039 | static int __init light_init(struct ibm_init_struct *iibm) |
1da177e4 | 2040 | { |
fe08bc4b HMH |
2041 | vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n"); |
2042 | ||
8d376cd6 HMH |
2043 | IBM_ACPIHANDLE_INIT(ledb); |
2044 | IBM_ACPIHANDLE_INIT(lght); | |
2045 | IBM_ACPIHANDLE_INIT(cmos); | |
5fba344c | 2046 | |
78f81cc4 | 2047 | /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */ |
d8fd94d9 | 2048 | tp_features.light = (cmos_handle || lght_handle) && !ledb_handle; |
78f81cc4 | 2049 | |
d8fd94d9 | 2050 | if (tp_features.light) |
78f81cc4 BD |
2051 | /* light status not supported on |
2052 | 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */ | |
d8fd94d9 HMH |
2053 | tp_features.light_status = |
2054 | acpi_evalf(ec_handle, NULL, "KBLT", "qv"); | |
1da177e4 | 2055 | |
fe08bc4b | 2056 | vdbg_printk(TPACPI_DBG_INIT, "light is %s\n", |
d8fd94d9 | 2057 | str_supported(tp_features.light)); |
fe08bc4b | 2058 | |
d8fd94d9 | 2059 | return (tp_features.light)? 0 : 1; |
1da177e4 LT |
2060 | } |
2061 | ||
78f81cc4 | 2062 | static int light_read(char *p) |
1da177e4 LT |
2063 | { |
2064 | int len = 0; | |
2065 | int status = 0; | |
2066 | ||
d8fd94d9 | 2067 | if (!tp_features.light) { |
78f81cc4 | 2068 | len += sprintf(p + len, "status:\t\tnot supported\n"); |
d8fd94d9 | 2069 | } else if (!tp_features.light_status) { |
78f81cc4 BD |
2070 | len += sprintf(p + len, "status:\t\tunknown\n"); |
2071 | len += sprintf(p + len, "commands:\ton, off\n"); | |
2072 | } else { | |
1da177e4 LT |
2073 | if (!acpi_evalf(ec_handle, &status, "KBLT", "d")) |
2074 | return -EIO; | |
2075 | len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0)); | |
78f81cc4 BD |
2076 | len += sprintf(p + len, "commands:\ton, off\n"); |
2077 | } | |
1da177e4 LT |
2078 | |
2079 | return len; | |
2080 | } | |
2081 | ||
78f81cc4 | 2082 | static int light_write(char *buf) |
1da177e4 LT |
2083 | { |
2084 | int cmos_cmd, lght_cmd; | |
2085 | char *cmd; | |
2086 | int success; | |
78f81cc4 | 2087 | |
d8fd94d9 | 2088 | if (!tp_features.light) |
78f81cc4 BD |
2089 | return -ENODEV; |
2090 | ||
1da177e4 LT |
2091 | while ((cmd = next_cmd(&buf))) { |
2092 | if (strlencmp(cmd, "on") == 0) { | |
2093 | cmos_cmd = 0x0c; | |
2094 | lght_cmd = 1; | |
2095 | } else if (strlencmp(cmd, "off") == 0) { | |
2096 | cmos_cmd = 0x0d; | |
2097 | lght_cmd = 0; | |
2098 | } else | |
2099 | return -EINVAL; | |
78f81cc4 | 2100 | |
1da177e4 | 2101 | success = cmos_handle ? |
78f81cc4 BD |
2102 | acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) : |
2103 | acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd); | |
1da177e4 LT |
2104 | if (!success) |
2105 | return -EIO; | |
2106 | } | |
2107 | ||
2108 | return 0; | |
2109 | } | |
2110 | ||
a5763f22 HMH |
2111 | static struct ibm_struct light_driver_data = { |
2112 | .name = "light", | |
2113 | .read = light_read, | |
2114 | .write = light_write, | |
2115 | }; | |
2116 | ||
56b6aeb0 HMH |
2117 | /************************************************************************* |
2118 | * Dock subdriver | |
2119 | */ | |
1da177e4 | 2120 | |
85998248 | 2121 | #ifdef CONFIG_THINKPAD_ACPI_DOCK |
56b6aeb0 HMH |
2122 | |
2123 | IBM_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */ | |
2124 | "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */ | |
2125 | "\\_SB.PCI0.PCI1.DOCK", /* all others */ | |
2126 | "\\_SB.PCI.ISA.SLCE", /* 570 */ | |
2127 | ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */ | |
2128 | ||
5fba344c HMH |
2129 | /* don't list other alternatives as we install a notify handler on the 570 */ |
2130 | IBM_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */ | |
2131 | ||
1ba90e3a TR |
2132 | static const struct acpi_device_id ibm_pci_device_ids[] = { |
2133 | {PCI_ROOT_HID_STRING, 0}, | |
2134 | {"", 0}, | |
2135 | }; | |
2136 | ||
d94a7f16 HMH |
2137 | static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = { |
2138 | { | |
2139 | .notify = dock_notify, | |
2140 | .handle = &dock_handle, | |
2141 | .type = ACPI_SYSTEM_NOTIFY, | |
2142 | }, | |
2143 | { | |
996fba08 HMH |
2144 | /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING. |
2145 | * We just use it to get notifications of dock hotplug | |
2146 | * in very old thinkpads */ | |
1ba90e3a | 2147 | .hid = ibm_pci_device_ids, |
d94a7f16 HMH |
2148 | .notify = dock_notify, |
2149 | .handle = &pci_handle, | |
2150 | .type = ACPI_SYSTEM_NOTIFY, | |
2151 | }, | |
2152 | }; | |
2153 | ||
2154 | static struct ibm_struct dock_driver_data[2] = { | |
2155 | { | |
2156 | .name = "dock", | |
2157 | .read = dock_read, | |
2158 | .write = dock_write, | |
2159 | .acpi = &ibm_dock_acpidriver[0], | |
2160 | }, | |
2161 | { | |
2162 | .name = "dock", | |
2163 | .acpi = &ibm_dock_acpidriver[1], | |
2164 | }, | |
2165 | }; | |
2166 | ||
1da177e4 LT |
2167 | #define dock_docked() (_sta(dock_handle) & 1) |
2168 | ||
a5763f22 | 2169 | static int __init dock_init(struct ibm_init_struct *iibm) |
5fba344c | 2170 | { |
fe08bc4b HMH |
2171 | vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n"); |
2172 | ||
8d376cd6 | 2173 | IBM_ACPIHANDLE_INIT(dock); |
5fba344c | 2174 | |
fe08bc4b HMH |
2175 | vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n", |
2176 | str_supported(dock_handle != NULL)); | |
2177 | ||
5fba344c HMH |
2178 | return (dock_handle)? 0 : 1; |
2179 | } | |
2180 | ||
d94a7f16 HMH |
2181 | static int __init dock_init2(struct ibm_init_struct *iibm) |
2182 | { | |
2183 | int dock2_needed; | |
2184 | ||
2185 | vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n"); | |
2186 | ||
2187 | if (dock_driver_data[0].flags.acpi_driver_registered && | |
2188 | dock_driver_data[0].flags.acpi_notify_installed) { | |
2189 | IBM_ACPIHANDLE_INIT(pci); | |
2190 | dock2_needed = (pci_handle != NULL); | |
2191 | vdbg_printk(TPACPI_DBG_INIT, | |
2192 | "dock PCI handler for the TP 570 is %s\n", | |
2193 | str_supported(dock2_needed)); | |
2194 | } else { | |
2195 | vdbg_printk(TPACPI_DBG_INIT, | |
2196 | "dock subdriver part 2 not required\n"); | |
2197 | dock2_needed = 0; | |
2198 | } | |
2199 | ||
2200 | return (dock2_needed)? 0 : 1; | |
2201 | } | |
2202 | ||
56b6aeb0 HMH |
2203 | static void dock_notify(struct ibm_struct *ibm, u32 event) |
2204 | { | |
2205 | int docked = dock_docked(); | |
1ba90e3a TR |
2206 | int pci = ibm->acpi->hid && ibm->acpi->device && |
2207 | acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids); | |
962ce8ca | 2208 | int data; |
56b6aeb0 HMH |
2209 | |
2210 | if (event == 1 && !pci) /* 570 */ | |
962ce8ca | 2211 | data = 1; /* button */ |
56b6aeb0 | 2212 | else if (event == 1 && pci) /* 570 */ |
962ce8ca | 2213 | data = 3; /* dock */ |
56b6aeb0 | 2214 | else if (event == 3 && docked) |
962ce8ca | 2215 | data = 1; /* button */ |
56b6aeb0 | 2216 | else if (event == 3 && !docked) |
962ce8ca | 2217 | data = 2; /* undock */ |
56b6aeb0 | 2218 | else if (event == 0 && docked) |
962ce8ca | 2219 | data = 3; /* dock */ |
56b6aeb0 HMH |
2220 | else { |
2221 | printk(IBM_ERR "unknown dock event %d, status %d\n", | |
2222 | event, _sta(dock_handle)); | |
962ce8ca | 2223 | data = 0; /* unknown */ |
56b6aeb0 | 2224 | } |
14e04fb3 | 2225 | acpi_bus_generate_proc_event(ibm->acpi->device, event, data); |
962ce8ca ZR |
2226 | acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class, |
2227 | ibm->acpi->device->dev.bus_id, | |
2228 | event, data); | |
56b6aeb0 HMH |
2229 | } |
2230 | ||
78f81cc4 | 2231 | static int dock_read(char *p) |
1da177e4 LT |
2232 | { |
2233 | int len = 0; | |
2234 | int docked = dock_docked(); | |
2235 | ||
2236 | if (!dock_handle) | |
2237 | len += sprintf(p + len, "status:\t\tnot supported\n"); | |
2238 | else if (!docked) | |
2239 | len += sprintf(p + len, "status:\t\tundocked\n"); | |
2240 | else { | |
2241 | len += sprintf(p + len, "status:\t\tdocked\n"); | |
2242 | len += sprintf(p + len, "commands:\tdock, undock\n"); | |
2243 | } | |
2244 | ||
2245 | return len; | |
2246 | } | |
2247 | ||
78f81cc4 | 2248 | static int dock_write(char *buf) |
1da177e4 LT |
2249 | { |
2250 | char *cmd; | |
2251 | ||
2252 | if (!dock_docked()) | |
78f81cc4 | 2253 | return -ENODEV; |
1da177e4 LT |
2254 | |
2255 | while ((cmd = next_cmd(&buf))) { | |
2256 | if (strlencmp(cmd, "undock") == 0) { | |
78f81cc4 BD |
2257 | if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) || |
2258 | !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1)) | |
56b6aeb0 HMH |
2259 | return -EIO; |
2260 | } else if (strlencmp(cmd, "dock") == 0) { | |
2261 | if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1)) | |
2262 | return -EIO; | |
2263 | } else | |
2264 | return -EINVAL; | |
1da177e4 | 2265 | } |
56b6aeb0 HMH |
2266 | |
2267 | return 0; | |
1da177e4 | 2268 | } |
56b6aeb0 | 2269 | |
85998248 | 2270 | #endif /* CONFIG_THINKPAD_ACPI_DOCK */ |
56b6aeb0 HMH |
2271 | |
2272 | /************************************************************************* | |
2273 | * Bay subdriver | |
2274 | */ | |
1da177e4 | 2275 | |
85998248 | 2276 | #ifdef CONFIG_THINKPAD_ACPI_BAY |
56b6aeb0 HMH |
2277 | IBM_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */ |
2278 | "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */ | |
2279 | "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */ | |
2280 | "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */ | |
2281 | ); /* A21e, R30, R31 */ | |
2282 | IBM_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */ | |
2283 | "_EJ0", /* all others */ | |
2284 | ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */ | |
2285 | IBM_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */ | |
2286 | "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */ | |
2287 | ); /* all others */ | |
2288 | IBM_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */ | |
2289 | "_EJ0", /* 770x */ | |
2290 | ); /* all others */ | |
2291 | ||
a5763f22 | 2292 | static int __init bay_init(struct ibm_init_struct *iibm) |
1da177e4 | 2293 | { |
fe08bc4b HMH |
2294 | vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n"); |
2295 | ||
8d376cd6 | 2296 | IBM_ACPIHANDLE_INIT(bay); |
5fba344c | 2297 | if (bay_handle) |
8d376cd6 HMH |
2298 | IBM_ACPIHANDLE_INIT(bay_ej); |
2299 | IBM_ACPIHANDLE_INIT(bay2); | |
5fba344c | 2300 | if (bay2_handle) |
8d376cd6 | 2301 | IBM_ACPIHANDLE_INIT(bay2_ej); |
5fba344c | 2302 | |
d8fd94d9 HMH |
2303 | tp_features.bay_status = bay_handle && |
2304 | acpi_evalf(bay_handle, NULL, "_STA", "qv"); | |
2305 | tp_features.bay_status2 = bay2_handle && | |
2306 | acpi_evalf(bay2_handle, NULL, "_STA", "qv"); | |
78f81cc4 | 2307 | |
d8fd94d9 HMH |
2308 | tp_features.bay_eject = bay_handle && bay_ej_handle && |
2309 | (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental); | |
2310 | tp_features.bay_eject2 = bay2_handle && bay2_ej_handle && | |
2311 | (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental); | |
1da177e4 | 2312 | |
fe08bc4b HMH |
2313 | vdbg_printk(TPACPI_DBG_INIT, |
2314 | "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n", | |
d8fd94d9 HMH |
2315 | str_supported(tp_features.bay_status), |
2316 | str_supported(tp_features.bay_eject), | |
2317 | str_supported(tp_features.bay_status2), | |
2318 | str_supported(tp_features.bay_eject2)); | |
fe08bc4b | 2319 | |
d8fd94d9 HMH |
2320 | return (tp_features.bay_status || tp_features.bay_eject || |
2321 | tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1; | |
1da177e4 LT |
2322 | } |
2323 | ||
56b6aeb0 HMH |
2324 | static void bay_notify(struct ibm_struct *ibm, u32 event) |
2325 | { | |
14e04fb3 | 2326 | acpi_bus_generate_proc_event(ibm->acpi->device, event, 0); |
962ce8ca ZR |
2327 | acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class, |
2328 | ibm->acpi->device->dev.bus_id, | |
2329 | event, 0); | |
56b6aeb0 HMH |
2330 | } |
2331 | ||
78f81cc4 BD |
2332 | #define bay_occupied(b) (_sta(b##_handle) & 1) |
2333 | ||
2334 | static int bay_read(char *p) | |
1da177e4 LT |
2335 | { |
2336 | int len = 0; | |
78f81cc4 BD |
2337 | int occupied = bay_occupied(bay); |
2338 | int occupied2 = bay_occupied(bay2); | |
2339 | int eject, eject2; | |
2340 | ||
d8fd94d9 HMH |
2341 | len += sprintf(p + len, "status:\t\t%s\n", |
2342 | tp_features.bay_status ? | |
2343 | (occupied ? "occupied" : "unoccupied") : | |
2344 | "not supported"); | |
2345 | if (tp_features.bay_status2) | |
78f81cc4 BD |
2346 | len += sprintf(p + len, "status2:\t%s\n", occupied2 ? |
2347 | "occupied" : "unoccupied"); | |
2348 | ||
d8fd94d9 HMH |
2349 | eject = tp_features.bay_eject && occupied; |
2350 | eject2 = tp_features.bay_eject2 && occupied2; | |
78f81cc4 BD |
2351 | |
2352 | if (eject && eject2) | |
2353 | len += sprintf(p + len, "commands:\teject, eject2\n"); | |
2354 | else if (eject) | |
1da177e4 | 2355 | len += sprintf(p + len, "commands:\teject\n"); |
78f81cc4 BD |
2356 | else if (eject2) |
2357 | len += sprintf(p + len, "commands:\teject2\n"); | |
1da177e4 LT |
2358 | |
2359 | return len; | |
2360 | } | |
2361 | ||
78f81cc4 | 2362 | static int bay_write(char *buf) |
1da177e4 LT |
2363 | { |
2364 | char *cmd; | |
2365 | ||
d8fd94d9 | 2366 | if (!tp_features.bay_eject && !tp_features.bay_eject2) |
78f81cc4 BD |
2367 | return -ENODEV; |
2368 | ||
1da177e4 | 2369 | while ((cmd = next_cmd(&buf))) { |
d8fd94d9 | 2370 | if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) { |
78f81cc4 BD |
2371 | if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1)) |
2372 | return -EIO; | |
d8fd94d9 | 2373 | } else if (tp_features.bay_eject2 && |
78f81cc4 BD |
2374 | strlencmp(cmd, "eject2") == 0) { |
2375 | if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1)) | |
1da177e4 LT |
2376 | return -EIO; |
2377 | } else | |
2378 | return -EINVAL; | |
2379 | } | |
2380 | ||
2381 | return 0; | |
78f81cc4 | 2382 | } |
a5763f22 | 2383 | |
8d376cd6 HMH |
2384 | static struct tp_acpi_drv_struct ibm_bay_acpidriver = { |
2385 | .notify = bay_notify, | |
2386 | .handle = &bay_handle, | |
2387 | .type = ACPI_SYSTEM_NOTIFY, | |
2388 | }; | |
2389 | ||
a5763f22 HMH |
2390 | static struct ibm_struct bay_driver_data = { |
2391 | .name = "bay", | |
2392 | .read = bay_read, | |
2393 | .write = bay_write, | |
8d376cd6 | 2394 | .acpi = &ibm_bay_acpidriver, |
a5763f22 HMH |
2395 | }; |
2396 | ||
85998248 | 2397 | #endif /* CONFIG_THINKPAD_ACPI_BAY */ |
1da177e4 | 2398 | |
56b6aeb0 HMH |
2399 | /************************************************************************* |
2400 | * CMOS subdriver | |
2401 | */ | |
2402 | ||
b616004c HMH |
2403 | /* sysfs cmos_command -------------------------------------------------- */ |
2404 | static ssize_t cmos_command_store(struct device *dev, | |
2405 | struct device_attribute *attr, | |
2406 | const char *buf, size_t count) | |
2407 | { | |
2408 | unsigned long cmos_cmd; | |
2409 | int res; | |
2410 | ||
2411 | if (parse_strtoul(buf, 21, &cmos_cmd)) | |
2412 | return -EINVAL; | |
2413 | ||
2414 | res = issue_thinkpad_cmos_command(cmos_cmd); | |
2415 | return (res)? res : count; | |
2416 | } | |
2417 | ||
2418 | static struct device_attribute dev_attr_cmos_command = | |
2419 | __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store); | |
2420 | ||
2421 | /* --------------------------------------------------------------------- */ | |
2422 | ||
a5763f22 | 2423 | static int __init cmos_init(struct ibm_init_struct *iibm) |
5fba344c | 2424 | { |
b616004c HMH |
2425 | int res; |
2426 | ||
fe08bc4b HMH |
2427 | vdbg_printk(TPACPI_DBG_INIT, |
2428 | "initializing cmos commands subdriver\n"); | |
2429 | ||
8d376cd6 | 2430 | IBM_ACPIHANDLE_INIT(cmos); |
5fba344c | 2431 | |
fe08bc4b HMH |
2432 | vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n", |
2433 | str_supported(cmos_handle != NULL)); | |
b616004c HMH |
2434 | |
2435 | res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command); | |
2436 | if (res) | |
2437 | return res; | |
2438 | ||
5fba344c HMH |
2439 | return (cmos_handle)? 0 : 1; |
2440 | } | |
2441 | ||
b616004c HMH |
2442 | static void cmos_exit(void) |
2443 | { | |
2444 | device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command); | |
2445 | } | |
2446 | ||
78f81cc4 | 2447 | static int cmos_read(char *p) |
1da177e4 LT |
2448 | { |
2449 | int len = 0; | |
2450 | ||
78f81cc4 BD |
2451 | /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p, |
2452 | R30, R31, T20-22, X20-21 */ | |
1da177e4 LT |
2453 | if (!cmos_handle) |
2454 | len += sprintf(p + len, "status:\t\tnot supported\n"); | |
2455 | else { | |
2456 | len += sprintf(p + len, "status:\t\tsupported\n"); | |
78f81cc4 | 2457 | len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n"); |
1da177e4 LT |
2458 | } |
2459 | ||
2460 | return len; | |
2461 | } | |
2462 | ||
78f81cc4 | 2463 | static int cmos_write(char *buf) |
1da177e4 LT |
2464 | { |
2465 | char *cmd; | |
c9bea99c | 2466 | int cmos_cmd, res; |
1da177e4 LT |
2467 | |
2468 | while ((cmd = next_cmd(&buf))) { | |
78f81cc4 BD |
2469 | if (sscanf(cmd, "%u", &cmos_cmd) == 1 && |
2470 | cmos_cmd >= 0 && cmos_cmd <= 21) { | |
1da177e4 LT |
2471 | /* cmos_cmd set */ |
2472 | } else | |
2473 | return -EINVAL; | |
2474 | ||
c9bea99c HMH |
2475 | res = issue_thinkpad_cmos_command(cmos_cmd); |
2476 | if (res) | |
2477 | return res; | |
1da177e4 LT |
2478 | } |
2479 | ||
2480 | return 0; | |
78f81cc4 BD |
2481 | } |
2482 | ||
a5763f22 HMH |
2483 | static struct ibm_struct cmos_driver_data = { |
2484 | .name = "cmos", | |
2485 | .read = cmos_read, | |
2486 | .write = cmos_write, | |
b616004c | 2487 | .exit = cmos_exit, |
a5763f22 | 2488 | }; |
56b6aeb0 HMH |
2489 | |
2490 | /************************************************************************* | |
2491 | * LED subdriver | |
2492 | */ | |
2493 | ||
9a8e1738 | 2494 | static enum led_access_mode led_supported; |
78f81cc4 | 2495 | |
56b6aeb0 HMH |
2496 | IBM_HANDLE(led, ec, "SLED", /* 570 */ |
2497 | "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */ | |
2498 | "LED", /* all others */ | |
2499 | ); /* R30, R31 */ | |
2500 | ||
a5763f22 | 2501 | static int __init led_init(struct ibm_init_struct *iibm) |
78f81cc4 | 2502 | { |
fe08bc4b HMH |
2503 | vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n"); |
2504 | ||
8d376cd6 | 2505 | IBM_ACPIHANDLE_INIT(led); |
5fba344c | 2506 | |
78f81cc4 BD |
2507 | if (!led_handle) |
2508 | /* led not supported on R30, R31 */ | |
efa27145 | 2509 | led_supported = TPACPI_LED_NONE; |
78f81cc4 BD |
2510 | else if (strlencmp(led_path, "SLED") == 0) |
2511 | /* 570 */ | |
efa27145 | 2512 | led_supported = TPACPI_LED_570; |
78f81cc4 BD |
2513 | else if (strlencmp(led_path, "SYSL") == 0) |
2514 | /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */ | |
efa27145 | 2515 | led_supported = TPACPI_LED_OLD; |
78f81cc4 BD |
2516 | else |
2517 | /* all others */ | |
efa27145 | 2518 | led_supported = TPACPI_LED_NEW; |
78f81cc4 | 2519 | |
fe08bc4b HMH |
2520 | vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n", |
2521 | str_supported(led_supported), led_supported); | |
2522 | ||
5fba344c | 2523 | return (led_supported != TPACPI_LED_NONE)? 0 : 1; |
78f81cc4 BD |
2524 | } |
2525 | ||
2526 | #define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking")) | |
2527 | ||
2528 | static int led_read(char *p) | |
1da177e4 LT |
2529 | { |
2530 | int len = 0; | |
2531 | ||
78f81cc4 BD |
2532 | if (!led_supported) { |
2533 | len += sprintf(p + len, "status:\t\tnot supported\n"); | |
2534 | return len; | |
2535 | } | |
2536 | len += sprintf(p + len, "status:\t\tsupported\n"); | |
2537 | ||
efa27145 | 2538 | if (led_supported == TPACPI_LED_570) { |
78f81cc4 BD |
2539 | /* 570 */ |
2540 | int i, status; | |
2541 | for (i = 0; i < 8; i++) { | |
2542 | if (!acpi_evalf(ec_handle, | |
2543 | &status, "GLED", "dd", 1 << i)) | |
2544 | return -EIO; | |
2545 | len += sprintf(p + len, "%d:\t\t%s\n", | |
2546 | i, led_status(status)); | |
2547 | } | |
2548 | } | |
2549 | ||
1da177e4 | 2550 | len += sprintf(p + len, "commands:\t" |
78f81cc4 | 2551 | "<led> on, <led> off, <led> blink (<led> is 0-7)\n"); |
1da177e4 LT |
2552 | |
2553 | return len; | |
2554 | } | |
2555 | ||
78f81cc4 BD |
2556 | /* off, on, blink */ |
2557 | static const int led_sled_arg1[] = { 0, 1, 3 }; | |
2558 | static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */ | |
2559 | static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */ | |
2560 | static const int led_led_arg1[] = { 0, 0x80, 0xc0 }; | |
2561 | ||
78f81cc4 | 2562 | static int led_write(char *buf) |
1da177e4 LT |
2563 | { |
2564 | char *cmd; | |
78f81cc4 BD |
2565 | int led, ind, ret; |
2566 | ||
2567 | if (!led_supported) | |
2568 | return -ENODEV; | |
1da177e4 LT |
2569 | |
2570 | while ((cmd = next_cmd(&buf))) { | |
78f81cc4 | 2571 | if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7) |
1da177e4 LT |
2572 | return -EINVAL; |
2573 | ||
78f81cc4 BD |
2574 | if (strstr(cmd, "off")) { |
2575 | ind = 0; | |
1da177e4 | 2576 | } else if (strstr(cmd, "on")) { |
78f81cc4 BD |
2577 | ind = 1; |
2578 | } else if (strstr(cmd, "blink")) { | |
2579 | ind = 2; | |
1da177e4 LT |
2580 | } else |
2581 | return -EINVAL; | |
78f81cc4 | 2582 | |
efa27145 | 2583 | if (led_supported == TPACPI_LED_570) { |
78f81cc4 BD |
2584 | /* 570 */ |
2585 | led = 1 << led; | |
1da177e4 | 2586 | if (!acpi_evalf(led_handle, NULL, NULL, "vdd", |
78f81cc4 | 2587 | led, led_sled_arg1[ind])) |
1da177e4 | 2588 | return -EIO; |
efa27145 | 2589 | } else if (led_supported == TPACPI_LED_OLD) { |
78f81cc4 BD |
2590 | /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */ |
2591 | led = 1 << led; | |
efa27145 | 2592 | ret = ec_write(TPACPI_LED_EC_HLMS, led); |
78f81cc4 BD |
2593 | if (ret >= 0) |
2594 | ret = | |
efa27145 | 2595 | ec_write(TPACPI_LED_EC_HLBL, |
e062e034 | 2596 | led * led_exp_hlbl[ind]); |
78f81cc4 BD |
2597 | if (ret >= 0) |
2598 | ret = | |
efa27145 | 2599 | ec_write(TPACPI_LED_EC_HLCL, |
e062e034 | 2600 | led * led_exp_hlcl[ind]); |
78f81cc4 BD |
2601 | if (ret < 0) |
2602 | return ret; | |
2603 | } else { | |
2604 | /* all others */ | |
2605 | if (!acpi_evalf(led_handle, NULL, NULL, "vdd", | |
2606 | led, led_led_arg1[ind])) | |
1da177e4 | 2607 | return -EIO; |
78f81cc4 BD |
2608 | } |
2609 | } | |
2610 | ||
2611 | return 0; | |
2612 | } | |
2613 | ||
a5763f22 HMH |
2614 | static struct ibm_struct led_driver_data = { |
2615 | .name = "led", | |
2616 | .read = led_read, | |
2617 | .write = led_write, | |
2618 | }; | |
2619 | ||
56b6aeb0 HMH |
2620 | /************************************************************************* |
2621 | * Beep subdriver | |
2622 | */ | |
2623 | ||
2624 | IBM_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */ | |
2625 | ||
a5763f22 | 2626 | static int __init beep_init(struct ibm_init_struct *iibm) |
5fba344c | 2627 | { |
fe08bc4b HMH |
2628 | vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n"); |
2629 | ||
8d376cd6 | 2630 | IBM_ACPIHANDLE_INIT(beep); |
5fba344c | 2631 | |
fe08bc4b HMH |
2632 | vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n", |
2633 | str_supported(beep_handle != NULL)); | |
2634 | ||
5fba344c HMH |
2635 | return (beep_handle)? 0 : 1; |
2636 | } | |
2637 | ||
78f81cc4 BD |
2638 | static int beep_read(char *p) |
2639 | { | |
2640 | int len = 0; | |
2641 | ||
2642 | if (!beep_handle) | |
2643 | len += sprintf(p + len, "status:\t\tnot supported\n"); | |
2644 | else { | |
2645 | len += sprintf(p + len, "status:\t\tsupported\n"); | |
2646 | len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n"); | |
2647 | } | |
2648 | ||
2649 | return len; | |
2650 | } | |
2651 | ||
2652 | static int beep_write(char *buf) | |
2653 | { | |
2654 | char *cmd; | |
2655 | int beep_cmd; | |
2656 | ||
2657 | if (!beep_handle) | |
2658 | return -ENODEV; | |
2659 | ||
2660 | while ((cmd = next_cmd(&buf))) { | |
2661 | if (sscanf(cmd, "%u", &beep_cmd) == 1 && | |
2662 | beep_cmd >= 0 && beep_cmd <= 17) { | |
2663 | /* beep_cmd set */ | |
2664 | } else | |
2665 | return -EINVAL; | |
2666 | if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0)) | |
2667 | return -EIO; | |
2668 | } | |
2669 | ||
2670 | return 0; | |
2671 | } | |
2672 | ||
a5763f22 HMH |
2673 | static struct ibm_struct beep_driver_data = { |
2674 | .name = "beep", | |
2675 | .read = beep_read, | |
2676 | .write = beep_write, | |
2677 | }; | |
2678 | ||
56b6aeb0 HMH |
2679 | /************************************************************************* |
2680 | * Thermal subdriver | |
2681 | */ | |
78f81cc4 | 2682 | |
a26f878a | 2683 | static enum thermal_access_mode thermal_read_mode; |
78f81cc4 | 2684 | |
2c37aa4e HMH |
2685 | /* sysfs temp##_input -------------------------------------------------- */ |
2686 | ||
2687 | static ssize_t thermal_temp_input_show(struct device *dev, | |
2688 | struct device_attribute *attr, | |
2689 | char *buf) | |
2690 | { | |
2691 | struct sensor_device_attribute *sensor_attr = | |
2692 | to_sensor_dev_attr(attr); | |
2693 | int idx = sensor_attr->index; | |
2694 | s32 value; | |
2695 | int res; | |
2696 | ||
2697 | res = thermal_get_sensor(idx, &value); | |
2698 | if (res) | |
2699 | return res; | |
2700 | if (value == TP_EC_THERMAL_TMP_NA * 1000) | |
2701 | return -ENXIO; | |
2702 | ||
2703 | return snprintf(buf, PAGE_SIZE, "%d\n", value); | |
2704 | } | |
2705 | ||
2706 | #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \ | |
2707 | SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, thermal_temp_input_show, NULL, _idxB) | |
2708 | ||
2709 | static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = { | |
2710 | THERMAL_SENSOR_ATTR_TEMP(1, 0), | |
2711 | THERMAL_SENSOR_ATTR_TEMP(2, 1), | |
2712 | THERMAL_SENSOR_ATTR_TEMP(3, 2), | |
2713 | THERMAL_SENSOR_ATTR_TEMP(4, 3), | |
2714 | THERMAL_SENSOR_ATTR_TEMP(5, 4), | |
2715 | THERMAL_SENSOR_ATTR_TEMP(6, 5), | |
2716 | THERMAL_SENSOR_ATTR_TEMP(7, 6), | |
2717 | THERMAL_SENSOR_ATTR_TEMP(8, 7), | |
2718 | THERMAL_SENSOR_ATTR_TEMP(9, 8), | |
2719 | THERMAL_SENSOR_ATTR_TEMP(10, 9), | |
2720 | THERMAL_SENSOR_ATTR_TEMP(11, 10), | |
2721 | THERMAL_SENSOR_ATTR_TEMP(12, 11), | |
2722 | THERMAL_SENSOR_ATTR_TEMP(13, 12), | |
2723 | THERMAL_SENSOR_ATTR_TEMP(14, 13), | |
2724 | THERMAL_SENSOR_ATTR_TEMP(15, 14), | |
2725 | THERMAL_SENSOR_ATTR_TEMP(16, 15), | |
2726 | }; | |
2727 | ||
2728 | #define THERMAL_ATTRS(X) \ | |
2729 | &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr | |
2730 | ||
2731 | static struct attribute *thermal_temp_input_attr[] = { | |
2732 | THERMAL_ATTRS(8), | |
2733 | THERMAL_ATTRS(9), | |
2734 | THERMAL_ATTRS(10), | |
2735 | THERMAL_ATTRS(11), | |
2736 | THERMAL_ATTRS(12), | |
2737 | THERMAL_ATTRS(13), | |
2738 | THERMAL_ATTRS(14), | |
2739 | THERMAL_ATTRS(15), | |
2740 | THERMAL_ATTRS(0), | |
2741 | THERMAL_ATTRS(1), | |
2742 | THERMAL_ATTRS(2), | |
2743 | THERMAL_ATTRS(3), | |
2744 | THERMAL_ATTRS(4), | |
2745 | THERMAL_ATTRS(5), | |
2746 | THERMAL_ATTRS(6), | |
2747 | THERMAL_ATTRS(7), | |
2748 | NULL | |
2749 | }; | |
2750 | ||
2751 | static const struct attribute_group thermal_temp_input16_group = { | |
2752 | .attrs = thermal_temp_input_attr | |
2753 | }; | |
2754 | ||
2755 | static const struct attribute_group thermal_temp_input8_group = { | |
2756 | .attrs = &thermal_temp_input_attr[8] | |
2757 | }; | |
2758 | ||
2759 | #undef THERMAL_SENSOR_ATTR_TEMP | |
2760 | #undef THERMAL_ATTRS | |
2761 | ||
2762 | /* --------------------------------------------------------------------- */ | |
2763 | ||
a5763f22 | 2764 | static int __init thermal_init(struct ibm_init_struct *iibm) |
78f81cc4 | 2765 | { |
60eb0b35 HMH |
2766 | u8 t, ta1, ta2; |
2767 | int i; | |
5fba344c | 2768 | int acpi_tmp7; |
2c37aa4e | 2769 | int res; |
5fba344c | 2770 | |
fe08bc4b HMH |
2771 | vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n"); |
2772 | ||
5fba344c | 2773 | acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv"); |
60eb0b35 | 2774 | |
3d6f99ca | 2775 | if (thinkpad_id.ec_model) { |
60eb0b35 HMH |
2776 | /* |
2777 | * Direct EC access mode: sensors at registers | |
2778 | * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for | |
2779 | * non-implemented, thermal sensors return 0x80 when | |
2780 | * not available | |
2781 | */ | |
78f81cc4 | 2782 | |
60eb0b35 HMH |
2783 | ta1 = ta2 = 0; |
2784 | for (i = 0; i < 8; i++) { | |
04cc862c | 2785 | if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) { |
60eb0b35 HMH |
2786 | ta1 |= t; |
2787 | } else { | |
2788 | ta1 = 0; | |
2789 | break; | |
2790 | } | |
04cc862c | 2791 | if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) { |
60eb0b35 HMH |
2792 | ta2 |= t; |
2793 | } else { | |
2794 | ta1 = 0; | |
2795 | break; | |
2796 | } | |
2797 | } | |
2798 | if (ta1 == 0) { | |
2799 | /* This is sheer paranoia, but we handle it anyway */ | |
2800 | if (acpi_tmp7) { | |
2801 | printk(IBM_ERR | |
2802 | "ThinkPad ACPI EC access misbehaving, " | |
2803 | "falling back to ACPI TMPx access mode\n"); | |
efa27145 | 2804 | thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07; |
60eb0b35 HMH |
2805 | } else { |
2806 | printk(IBM_ERR | |
2807 | "ThinkPad ACPI EC access misbehaving, " | |
2808 | "disabling thermal sensors access\n"); | |
efa27145 | 2809 | thermal_read_mode = TPACPI_THERMAL_NONE; |
60eb0b35 HMH |
2810 | } |
2811 | } else { | |
2812 | thermal_read_mode = | |
2813 | (ta2 != 0) ? | |
efa27145 | 2814 | TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8; |
60eb0b35 HMH |
2815 | } |
2816 | } else if (acpi_tmp7) { | |
a26f878a HMH |
2817 | if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) { |
2818 | /* 600e/x, 770e, 770x */ | |
efa27145 | 2819 | thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT; |
a26f878a HMH |
2820 | } else { |
2821 | /* Standard ACPI TMPx access, max 8 sensors */ | |
efa27145 | 2822 | thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07; |
a26f878a HMH |
2823 | } |
2824 | } else { | |
2825 | /* temperatures not supported on 570, G4x, R30, R31, R32 */ | |
efa27145 | 2826 | thermal_read_mode = TPACPI_THERMAL_NONE; |
a26f878a | 2827 | } |
78f81cc4 | 2828 | |
fe08bc4b HMH |
2829 | vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n", |
2830 | str_supported(thermal_read_mode != TPACPI_THERMAL_NONE), | |
2831 | thermal_read_mode); | |
2832 | ||
2c37aa4e HMH |
2833 | switch(thermal_read_mode) { |
2834 | case TPACPI_THERMAL_TPEC_16: | |
2835 | res = sysfs_create_group(&tpacpi_pdev->dev.kobj, | |
2836 | &thermal_temp_input16_group); | |
2837 | if (res) | |
2838 | return res; | |
2839 | break; | |
2840 | case TPACPI_THERMAL_TPEC_8: | |
2841 | case TPACPI_THERMAL_ACPI_TMP07: | |
2842 | case TPACPI_THERMAL_ACPI_UPDT: | |
2843 | res = sysfs_create_group(&tpacpi_pdev->dev.kobj, | |
2844 | &thermal_temp_input8_group); | |
2845 | if (res) | |
2846 | return res; | |
2847 | break; | |
2848 | case TPACPI_THERMAL_NONE: | |
2849 | default: | |
2850 | return 1; | |
2851 | } | |
2852 | ||
2853 | return 0; | |
2854 | } | |
2855 | ||
2856 | static void thermal_exit(void) | |
2857 | { | |
2858 | switch(thermal_read_mode) { | |
2859 | case TPACPI_THERMAL_TPEC_16: | |
2860 | sysfs_remove_group(&tpacpi_pdev->dev.kobj, | |
2861 | &thermal_temp_input16_group); | |
2862 | break; | |
2863 | case TPACPI_THERMAL_TPEC_8: | |
2864 | case TPACPI_THERMAL_ACPI_TMP07: | |
2865 | case TPACPI_THERMAL_ACPI_UPDT: | |
2866 | sysfs_remove_group(&tpacpi_pdev->dev.kobj, | |
2867 | &thermal_temp_input16_group); | |
2868 | break; | |
2869 | case TPACPI_THERMAL_NONE: | |
2870 | default: | |
2871 | break; | |
2872 | } | |
78f81cc4 BD |
2873 | } |
2874 | ||
04cc862c HMH |
2875 | /* idx is zero-based */ |
2876 | static int thermal_get_sensor(int idx, s32 *value) | |
78f81cc4 | 2877 | { |
04cc862c | 2878 | int t; |
60eb0b35 | 2879 | s8 tmp; |
04cc862c | 2880 | char tmpi[5]; |
78f81cc4 | 2881 | |
04cc862c | 2882 | t = TP_EC_THERMAL_TMP0; |
78f81cc4 | 2883 | |
a26f878a | 2884 | switch (thermal_read_mode) { |
efa27145 HMH |
2885 | #if TPACPI_MAX_THERMAL_SENSORS >= 16 |
2886 | case TPACPI_THERMAL_TPEC_16: | |
04cc862c HMH |
2887 | if (idx >= 8 && idx <= 15) { |
2888 | t = TP_EC_THERMAL_TMP8; | |
2889 | idx -= 8; | |
60eb0b35 HMH |
2890 | } |
2891 | /* fallthrough */ | |
2892 | #endif | |
efa27145 | 2893 | case TPACPI_THERMAL_TPEC_8: |
04cc862c HMH |
2894 | if (idx <= 7) { |
2895 | if (!acpi_ec_read(t + idx, &tmp)) | |
78f81cc4 | 2896 | return -EIO; |
04cc862c HMH |
2897 | *value = tmp * 1000; |
2898 | return 0; | |
60eb0b35 | 2899 | } |
04cc862c | 2900 | break; |
78f81cc4 | 2901 | |
efa27145 | 2902 | case TPACPI_THERMAL_ACPI_UPDT: |
04cc862c HMH |
2903 | if (idx <= 7) { |
2904 | snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx); | |
2905 | if (!acpi_evalf(ec_handle, NULL, "UPDT", "v")) | |
2906 | return -EIO; | |
78f81cc4 BD |
2907 | if (!acpi_evalf(ec_handle, &t, tmpi, "d")) |
2908 | return -EIO; | |
04cc862c HMH |
2909 | *value = (t - 2732) * 100; |
2910 | return 0; | |
78f81cc4 | 2911 | } |
04cc862c | 2912 | break; |
78f81cc4 | 2913 | |
efa27145 | 2914 | case TPACPI_THERMAL_ACPI_TMP07: |
04cc862c HMH |
2915 | if (idx <= 7) { |
2916 | snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx); | |
78f81cc4 BD |
2917 | if (!acpi_evalf(ec_handle, &t, tmpi, "d")) |
2918 | return -EIO; | |
a8fba3da HMH |
2919 | if (t > 127 || t < -127) |
2920 | t = TP_EC_THERMAL_TMP_NA; | |
04cc862c HMH |
2921 | *value = t * 1000; |
2922 | return 0; | |
78f81cc4 | 2923 | } |
04cc862c | 2924 | break; |
78f81cc4 | 2925 | |
efa27145 | 2926 | case TPACPI_THERMAL_NONE: |
a26f878a | 2927 | default: |
04cc862c HMH |
2928 | return -ENOSYS; |
2929 | } | |
2930 | ||
2931 | return -EINVAL; | |
2932 | } | |
2933 | ||
2934 | static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s) | |
2935 | { | |
2936 | int res, i; | |
2937 | int n; | |
2938 | ||
2939 | n = 8; | |
2940 | i = 0; | |
2941 | ||
2942 | if (!s) | |
2943 | return -EINVAL; | |
2944 | ||
2945 | if (thermal_read_mode == TPACPI_THERMAL_TPEC_16) | |
2946 | n = 16; | |
2947 | ||
2948 | for(i = 0 ; i < n; i++) { | |
2949 | res = thermal_get_sensor(i, &s->temp[i]); | |
2950 | if (res) | |
2951 | return res; | |
78f81cc4 | 2952 | } |
04cc862c HMH |
2953 | |
2954 | return n; | |
a26f878a HMH |
2955 | } |
2956 | ||
2957 | static int thermal_read(char *p) | |
2958 | { | |
2959 | int len = 0; | |
2960 | int n, i; | |
2961 | struct ibm_thermal_sensors_struct t; | |
2962 | ||
2963 | n = thermal_get_sensors(&t); | |
2964 | if (unlikely(n < 0)) | |
2965 | return n; | |
2966 | ||
2967 | len += sprintf(p + len, "temperatures:\t"); | |
2968 | ||
2969 | if (n > 0) { | |
2970 | for (i = 0; i < (n - 1); i++) | |
2971 | len += sprintf(p + len, "%d ", t.temp[i] / 1000); | |
2972 | len += sprintf(p + len, "%d\n", t.temp[i] / 1000); | |
2973 | } else | |
2974 | len += sprintf(p + len, "not supported\n"); | |
78f81cc4 BD |
2975 | |
2976 | return len; | |
2977 | } | |
2978 | ||
a5763f22 HMH |
2979 | static struct ibm_struct thermal_driver_data = { |
2980 | .name = "thermal", | |
2981 | .read = thermal_read, | |
2c37aa4e | 2982 | .exit = thermal_exit, |
a5763f22 HMH |
2983 | }; |
2984 | ||
56b6aeb0 HMH |
2985 | /************************************************************************* |
2986 | * EC Dump subdriver | |
2987 | */ | |
2988 | ||
78f81cc4 BD |
2989 | static u8 ecdump_regs[256]; |
2990 | ||
2991 | static int ecdump_read(char *p) | |
2992 | { | |
2993 | int len = 0; | |
2994 | int i, j; | |
2995 | u8 v; | |
2996 | ||
2997 | len += sprintf(p + len, "EC " | |
2998 | " +00 +01 +02 +03 +04 +05 +06 +07" | |
2999 | " +08 +09 +0a +0b +0c +0d +0e +0f\n"); | |
3000 | for (i = 0; i < 256; i += 16) { | |
3001 | len += sprintf(p + len, "EC 0x%02x:", i); | |
3002 | for (j = 0; j < 16; j++) { | |
3003 | if (!acpi_ec_read(i + j, &v)) | |
3004 | break; | |
3005 | if (v != ecdump_regs[i + j]) | |
3006 | len += sprintf(p + len, " *%02x", v); | |
3007 | else | |
3008 | len += sprintf(p + len, " %02x", v); | |
3009 | ecdump_regs[i + j] = v; | |
3010 | } | |
3011 | len += sprintf(p + len, "\n"); | |
3012 | if (j != 16) | |
3013 | break; | |
3014 | } | |
3015 | ||
3016 | /* These are way too dangerous to advertise openly... */ | |
3017 | #if 0 | |
3018 | len += sprintf(p + len, "commands:\t0x<offset> 0x<value>" | |
3019 | " (<offset> is 00-ff, <value> is 00-ff)\n"); | |
3020 | len += sprintf(p + len, "commands:\t0x<offset> <value> " | |
3021 | " (<offset> is 00-ff, <value> is 0-255)\n"); | |
3022 | #endif | |
3023 | return len; | |
3024 | } | |
3025 | ||
3026 | static int ecdump_write(char *buf) | |
3027 | { | |
3028 | char *cmd; | |
3029 | int i, v; | |
3030 | ||
3031 | while ((cmd = next_cmd(&buf))) { | |
3032 | if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) { | |
3033 | /* i and v set */ | |
3034 | } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) { | |
3035 | /* i and v set */ | |
3036 | } else | |
3037 | return -EINVAL; | |
3038 | if (i >= 0 && i < 256 && v >= 0 && v < 256) { | |
3039 | if (!acpi_ec_write(i, v)) | |
1da177e4 LT |
3040 | return -EIO; |
3041 | } else | |
3042 | return -EINVAL; | |
3043 | } | |
3044 | ||
3045 | return 0; | |
78f81cc4 BD |
3046 | } |
3047 | ||
a5763f22 HMH |
3048 | static struct ibm_struct ecdump_driver_data = { |
3049 | .name = "ecdump", | |
3050 | .read = ecdump_read, | |
3051 | .write = ecdump_write, | |
92641177 | 3052 | .flags.experimental = 1, |
a5763f22 HMH |
3053 | }; |
3054 | ||
56b6aeb0 HMH |
3055 | /************************************************************************* |
3056 | * Backlight/brightness subdriver | |
3057 | */ | |
3058 | ||
94954cc6 | 3059 | static struct backlight_device *ibm_backlight_device; |
56b6aeb0 HMH |
3060 | |
3061 | static struct backlight_ops ibm_backlight_data = { | |
3062 | .get_brightness = brightness_get, | |
3063 | .update_status = brightness_update_status, | |
3064 | }; | |
3065 | ||
f432255e HMH |
3066 | static struct mutex brightness_mutex; |
3067 | ||
a5763f22 | 3068 | static int __init brightness_init(struct ibm_init_struct *iibm) |
56b6aeb0 HMH |
3069 | { |
3070 | int b; | |
3071 | ||
fe08bc4b HMH |
3072 | vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n"); |
3073 | ||
f432255e HMH |
3074 | mutex_init(&brightness_mutex); |
3075 | ||
24d3b774 HMH |
3076 | if (!brightness_mode) { |
3077 | if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) | |
3078 | brightness_mode = 2; | |
3079 | else | |
3080 | brightness_mode = 3; | |
3081 | ||
3082 | dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n", | |
3083 | brightness_mode); | |
3084 | } | |
3085 | ||
3086 | if (brightness_mode > 3) | |
3087 | return -EINVAL; | |
3088 | ||
56b6aeb0 HMH |
3089 | b = brightness_get(NULL); |
3090 | if (b < 0) | |
24d3b774 | 3091 | return 1; |
56b6aeb0 | 3092 | |
7d5a015e HMH |
3093 | ibm_backlight_device = backlight_device_register( |
3094 | TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL, | |
3095 | &ibm_backlight_data); | |
56b6aeb0 HMH |
3096 | if (IS_ERR(ibm_backlight_device)) { |
3097 | printk(IBM_ERR "Could not register backlight device\n"); | |
3098 | return PTR_ERR(ibm_backlight_device); | |
3099 | } | |
fe08bc4b | 3100 | vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n"); |
56b6aeb0 HMH |
3101 | |
3102 | ibm_backlight_device->props.max_brightness = 7; | |
3103 | ibm_backlight_device->props.brightness = b; | |
3104 | backlight_update_status(ibm_backlight_device); | |
3105 | ||
3106 | return 0; | |
3107 | } | |
3108 | ||
3109 | static void brightness_exit(void) | |
3110 | { | |
3111 | if (ibm_backlight_device) { | |
fe08bc4b HMH |
3112 | vdbg_printk(TPACPI_DBG_EXIT, |
3113 | "calling backlight_device_unregister()\n"); | |
56b6aeb0 HMH |
3114 | backlight_device_unregister(ibm_backlight_device); |
3115 | ibm_backlight_device = NULL; | |
3116 | } | |
3117 | } | |
3118 | ||
3119 | static int brightness_update_status(struct backlight_device *bd) | |
3120 | { | |
3121 | return brightness_set( | |
3122 | (bd->props.fb_blank == FB_BLANK_UNBLANK && | |
3123 | bd->props.power == FB_BLANK_UNBLANK) ? | |
3124 | bd->props.brightness : 0); | |
3125 | } | |
3126 | ||
24d3b774 HMH |
3127 | /* |
3128 | * ThinkPads can read brightness from two places: EC 0x31, or | |
3129 | * CMOS NVRAM byte 0x5E, bits 0-3. | |
3130 | */ | |
8acb0250 HM |
3131 | static int brightness_get(struct backlight_device *bd) |
3132 | { | |
24d3b774 | 3133 | u8 lec = 0, lcmos = 0, level = 0; |
8acb0250 | 3134 | |
24d3b774 HMH |
3135 | if (brightness_mode & 1) { |
3136 | if (!acpi_ec_read(brightness_offset, &lec)) | |
3137 | return -EIO; | |
3138 | lec &= 7; | |
3139 | level = lec; | |
3140 | }; | |
3141 | if (brightness_mode & 2) { | |
3142 | lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS) | |
3143 | & TP_NVRAM_MASK_LEVEL_BRIGHTNESS) | |
3144 | >> TP_NVRAM_POS_LEVEL_BRIGHTNESS; | |
3145 | level = lcmos; | |
3146 | } | |
3147 | ||
3148 | if (brightness_mode == 3 && lec != lcmos) { | |
3149 | printk(IBM_ERR | |
3150 | "CMOS NVRAM (%u) and EC (%u) do not agree " | |
3151 | "on display brightness level\n", | |
3152 | (unsigned int) lcmos, | |
3153 | (unsigned int) lec); | |
3154 | return -EIO; | |
3155 | } | |
fb87a811 | 3156 | |
8d297264 | 3157 | return level; |
8acb0250 HM |
3158 | } |
3159 | ||
8acb0250 | 3160 | static int brightness_set(int value) |
1da177e4 | 3161 | { |
f432255e HMH |
3162 | int cmos_cmd, inc, i, res; |
3163 | int current_value; | |
8acb0250 | 3164 | |
24d3b774 HMH |
3165 | if (value > 7) |
3166 | return -EINVAL; | |
8acb0250 | 3167 | |
f432255e HMH |
3168 | res = mutex_lock_interruptible(&brightness_mutex); |
3169 | if (res < 0) | |
3170 | return res; | |
3171 | ||
3172 | current_value = brightness_get(NULL); | |
3173 | if (current_value < 0) { | |
3174 | res = current_value; | |
3175 | goto errout; | |
3176 | } | |
3177 | ||
24d3b774 HMH |
3178 | cmos_cmd = value > current_value ? |
3179 | TP_CMOS_BRIGHTNESS_UP : | |
3180 | TP_CMOS_BRIGHTNESS_DOWN; | |
8acb0250 | 3181 | inc = value > current_value ? 1 : -1; |
24d3b774 | 3182 | |
f432255e | 3183 | res = 0; |
8acb0250 | 3184 | for (i = current_value; i != value; i += inc) { |
24d3b774 | 3185 | if ((brightness_mode & 2) && |
f432255e HMH |
3186 | issue_thinkpad_cmos_command(cmos_cmd)) { |
3187 | res = -EIO; | |
3188 | goto errout; | |
3189 | } | |
24d3b774 | 3190 | if ((brightness_mode & 1) && |
f432255e HMH |
3191 | !acpi_ec_write(brightness_offset, i + inc)) { |
3192 | res = -EIO; | |
3193 | goto errout;; | |
3194 | } | |
8acb0250 HM |
3195 | } |
3196 | ||
f432255e HMH |
3197 | errout: |
3198 | mutex_unlock(&brightness_mutex); | |
3199 | return res; | |
8acb0250 HM |
3200 | } |
3201 | ||
56b6aeb0 HMH |
3202 | static int brightness_read(char *p) |
3203 | { | |
3204 | int len = 0; | |
3205 | int level; | |
3206 | ||
3207 | if ((level = brightness_get(NULL)) < 0) { | |
3208 | len += sprintf(p + len, "level:\t\tunreadable\n"); | |
3209 | } else { | |
3210 | len += sprintf(p + len, "level:\t\t%d\n", level & 0x7); | |
3211 | len += sprintf(p + len, "commands:\tup, down\n"); | |
3212 | len += sprintf(p + len, "commands:\tlevel <level>" | |
3213 | " (<level> is 0-7)\n"); | |
3214 | } | |
3215 | ||
3216 | return len; | |
3217 | } | |
3218 | ||
8acb0250 HM |
3219 | static int brightness_write(char *buf) |
3220 | { | |
3221 | int level; | |
78f81cc4 | 3222 | int new_level; |
1da177e4 | 3223 | char *cmd; |
1da177e4 LT |
3224 | |
3225 | while ((cmd = next_cmd(&buf))) { | |
8acb0250 HM |
3226 | if ((level = brightness_get(NULL)) < 0) |
3227 | return level; | |
78f81cc4 BD |
3228 | level &= 7; |
3229 | ||
3230 | if (strlencmp(cmd, "up") == 0) { | |
3231 | new_level = level == 7 ? 7 : level + 1; | |
3232 | } else if (strlencmp(cmd, "down") == 0) { | |
3233 | new_level = level == 0 ? 0 : level - 1; | |
3234 | } else if (sscanf(cmd, "level %d", &new_level) == 1 && | |
3235 | new_level >= 0 && new_level <= 7) { | |
3236 | /* new_level set */ | |
3237 | } else | |
3238 | return -EINVAL; | |
3239 | ||
8acb0250 | 3240 | brightness_set(new_level); |
78f81cc4 BD |
3241 | } |
3242 | ||
3243 | return 0; | |
3244 | } | |
3245 | ||
a5763f22 HMH |
3246 | static struct ibm_struct brightness_driver_data = { |
3247 | .name = "brightness", | |
3248 | .read = brightness_read, | |
3249 | .write = brightness_write, | |
3250 | .exit = brightness_exit, | |
3251 | }; | |
3252 | ||
56b6aeb0 HMH |
3253 | /************************************************************************* |
3254 | * Volume subdriver | |
3255 | */ | |
fb87a811 | 3256 | |
78f81cc4 BD |
3257 | static int volume_read(char *p) |
3258 | { | |
3259 | int len = 0; | |
3260 | u8 level; | |
3261 | ||
3262 | if (!acpi_ec_read(volume_offset, &level)) { | |
3263 | len += sprintf(p + len, "level:\t\tunreadable\n"); | |
3264 | } else { | |
3265 | len += sprintf(p + len, "level:\t\t%d\n", level & 0xf); | |
3266 | len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6)); | |
3267 | len += sprintf(p + len, "commands:\tup, down, mute\n"); | |
3268 | len += sprintf(p + len, "commands:\tlevel <level>" | |
3269 | " (<level> is 0-15)\n"); | |
3270 | } | |
3271 | ||
3272 | return len; | |
3273 | } | |
3274 | ||
78f81cc4 BD |
3275 | static int volume_write(char *buf) |
3276 | { | |
3277 | int cmos_cmd, inc, i; | |
3278 | u8 level, mute; | |
3279 | int new_level, new_mute; | |
3280 | char *cmd; | |
3281 | ||
3282 | while ((cmd = next_cmd(&buf))) { | |
3283 | if (!acpi_ec_read(volume_offset, &level)) | |
3284 | return -EIO; | |
3285 | new_mute = mute = level & 0x40; | |
3286 | new_level = level = level & 0xf; | |
3287 | ||
3288 | if (strlencmp(cmd, "up") == 0) { | |
3289 | if (mute) | |
3290 | new_mute = 0; | |
3291 | else | |
3292 | new_level = level == 15 ? 15 : level + 1; | |
3293 | } else if (strlencmp(cmd, "down") == 0) { | |
3294 | if (mute) | |
3295 | new_mute = 0; | |
3296 | else | |
3297 | new_level = level == 0 ? 0 : level - 1; | |
3298 | } else if (sscanf(cmd, "level %d", &new_level) == 1 && | |
3299 | new_level >= 0 && new_level <= 15) { | |
3300 | /* new_level set */ | |
3301 | } else if (strlencmp(cmd, "mute") == 0) { | |
3302 | new_mute = 0x40; | |
1da177e4 LT |
3303 | } else |
3304 | return -EINVAL; | |
3305 | ||
78f81cc4 | 3306 | if (new_level != level) { /* mute doesn't change */ |
56b6aeb0 | 3307 | cmos_cmd = new_level > level ? TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN; |
78f81cc4 BD |
3308 | inc = new_level > level ? 1 : -1; |
3309 | ||
c9bea99c | 3310 | if (mute && (issue_thinkpad_cmos_command(cmos_cmd) || |
78f81cc4 BD |
3311 | !acpi_ec_write(volume_offset, level))) |
3312 | return -EIO; | |
3313 | ||
3314 | for (i = level; i != new_level; i += inc) | |
c9bea99c | 3315 | if (issue_thinkpad_cmos_command(cmos_cmd) || |
78f81cc4 BD |
3316 | !acpi_ec_write(volume_offset, i + inc)) |
3317 | return -EIO; | |
3318 | ||
c9bea99c | 3319 | if (mute && (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) || |
78f81cc4 BD |
3320 | !acpi_ec_write(volume_offset, |
3321 | new_level + mute))) | |
3322 | return -EIO; | |
3323 | } | |
3324 | ||
3325 | if (new_mute != mute) { /* level doesn't change */ | |
56b6aeb0 | 3326 | cmos_cmd = new_mute ? TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP; |
78f81cc4 | 3327 | |
c9bea99c | 3328 | if (issue_thinkpad_cmos_command(cmos_cmd) || |
78f81cc4 BD |
3329 | !acpi_ec_write(volume_offset, level + new_mute)) |
3330 | return -EIO; | |
3331 | } | |
3332 | } | |
3333 | ||
3334 | return 0; | |
3335 | } | |
3336 | ||
a5763f22 HMH |
3337 | static struct ibm_struct volume_driver_data = { |
3338 | .name = "volume", | |
3339 | .read = volume_read, | |
3340 | .write = volume_write, | |
3341 | }; | |
56b6aeb0 HMH |
3342 | |
3343 | /************************************************************************* | |
3344 | * Fan subdriver | |
3345 | */ | |
3346 | ||
3347 | /* | |
3348 | * FAN ACCESS MODES | |
3349 | * | |
efa27145 | 3350 | * TPACPI_FAN_RD_ACPI_GFAN: |
56b6aeb0 HMH |
3351 | * ACPI GFAN method: returns fan level |
3352 | * | |
efa27145 | 3353 | * see TPACPI_FAN_WR_ACPI_SFAN |
f51d1a39 | 3354 | * EC 0x2f (HFSP) not available if GFAN exists |
56b6aeb0 | 3355 | * |
efa27145 | 3356 | * TPACPI_FAN_WR_ACPI_SFAN: |
56b6aeb0 HMH |
3357 | * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max) |
3358 | * | |
f51d1a39 HMH |
3359 | * EC 0x2f (HFSP) might be available *for reading*, but do not use |
3360 | * it for writing. | |
56b6aeb0 | 3361 | * |
efa27145 | 3362 | * TPACPI_FAN_WR_TPEC: |
f51d1a39 HMH |
3363 | * ThinkPad EC register 0x2f (HFSP): fan control loop mode |
3364 | * Supported on almost all ThinkPads | |
56b6aeb0 HMH |
3365 | * |
3366 | * Fan speed changes of any sort (including those caused by the | |
3367 | * disengaged mode) are usually done slowly by the firmware as the | |
3368 | * maximum ammount of fan duty cycle change per second seems to be | |
3369 | * limited. | |
3370 | * | |
3371 | * Reading is not available if GFAN exists. | |
3372 | * Writing is not available if SFAN exists. | |
3373 | * | |
3374 | * Bits | |
3375 | * 7 automatic mode engaged; | |
3376 | * (default operation mode of the ThinkPad) | |
3377 | * fan level is ignored in this mode. | |
f51d1a39 | 3378 | * 6 full speed mode (takes precedence over bit 7); |
56b6aeb0 | 3379 | * not available on all thinkpads. May disable |
f51d1a39 HMH |
3380 | * the tachometer while the fan controller ramps up |
3381 | * the speed (which can take up to a few *minutes*). | |
3382 | * Speeds up fan to 100% duty-cycle, which is far above | |
3383 | * the standard RPM levels. It is not impossible that | |
3384 | * it could cause hardware damage. | |
56b6aeb0 HMH |
3385 | * 5-3 unused in some models. Extra bits for fan level |
3386 | * in others, but still useless as all values above | |
3387 | * 7 map to the same speed as level 7 in these models. | |
3388 | * 2-0 fan level (0..7 usually) | |
3389 | * 0x00 = stop | |
3390 | * 0x07 = max (set when temperatures critical) | |
3391 | * Some ThinkPads may have other levels, see | |
efa27145 | 3392 | * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41) |
56b6aeb0 HMH |
3393 | * |
3394 | * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at | |
3395 | * boot. Apparently the EC does not intialize it, so unless ACPI DSDT | |
3396 | * does so, its initial value is meaningless (0x07). | |
3397 | * | |
3398 | * For firmware bugs, refer to: | |
3399 | * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues | |
3400 | * | |
3401 | * ---- | |
3402 | * | |
3403 | * ThinkPad EC register 0x84 (LSB), 0x85 (MSB): | |
3404 | * Main fan tachometer reading (in RPM) | |
3405 | * | |
3406 | * This register is present on all ThinkPads with a new-style EC, and | |
3407 | * it is known not to be present on the A21m/e, and T22, as there is | |
3408 | * something else in offset 0x84 according to the ACPI DSDT. Other | |
3409 | * ThinkPads from this same time period (and earlier) probably lack the | |
3410 | * tachometer as well. | |
3411 | * | |
3412 | * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare | |
3413 | * was never fixed by IBM to report the EC firmware version string | |
3414 | * probably support the tachometer (like the early X models), so | |
3415 | * detecting it is quite hard. We need more data to know for sure. | |
3416 | * | |
3417 | * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings | |
3418 | * might result. | |
3419 | * | |
f51d1a39 HMH |
3420 | * FIRMWARE BUG: may go stale while the EC is switching to full speed |
3421 | * mode. | |
56b6aeb0 HMH |
3422 | * |
3423 | * For firmware bugs, refer to: | |
3424 | * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues | |
3425 | * | |
efa27145 | 3426 | * TPACPI_FAN_WR_ACPI_FANS: |
56b6aeb0 HMH |
3427 | * ThinkPad X31, X40, X41. Not available in the X60. |
3428 | * | |
3429 | * FANS ACPI handle: takes three arguments: low speed, medium speed, | |
3430 | * high speed. ACPI DSDT seems to map these three speeds to levels | |
3431 | * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH | |
3432 | * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3") | |
3433 | * | |
3434 | * The speeds are stored on handles | |
3435 | * (FANA:FAN9), (FANC:FANB), (FANE:FAND). | |
3436 | * | |
3437 | * There are three default speed sets, acessible as handles: | |
3438 | * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H | |
3439 | * | |
3440 | * ACPI DSDT switches which set is in use depending on various | |
3441 | * factors. | |
3442 | * | |
efa27145 | 3443 | * TPACPI_FAN_WR_TPEC is also available and should be used to |
56b6aeb0 HMH |
3444 | * command the fan. The X31/X40/X41 seems to have 8 fan levels, |
3445 | * but the ACPI tables just mention level 7. | |
3446 | */ | |
3447 | ||
69ba91cb HMH |
3448 | static enum fan_status_access_mode fan_status_access_mode; |
3449 | static enum fan_control_access_mode fan_control_access_mode; | |
3450 | static enum fan_control_commands fan_control_commands; | |
78f81cc4 | 3451 | |
778b4d74 | 3452 | static u8 fan_control_initial_status; |
fe98a52c | 3453 | static u8 fan_control_desired_level; |
778b4d74 | 3454 | |
25c68a33 | 3455 | static void fan_watchdog_fire(struct work_struct *ignored); |
16663a87 | 3456 | static int fan_watchdog_maxinterval; |
25c68a33 | 3457 | static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire); |
16663a87 | 3458 | |
56b6aeb0 HMH |
3459 | IBM_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */ |
3460 | IBM_HANDLE(gfan, ec, "GFAN", /* 570 */ | |
3461 | "\\FSPD", /* 600e/x, 770e, 770x */ | |
3462 | ); /* all others */ | |
3463 | IBM_HANDLE(sfan, ec, "SFAN", /* 570 */ | |
3464 | "JFNS", /* 770x-JL */ | |
3465 | ); /* all others */ | |
3466 | ||
fe98a52c HMH |
3467 | /* |
3468 | * SYSFS fan layout: hwmon compatible (device) | |
3469 | * | |
3470 | * pwm*_enable: | |
3471 | * 0: "disengaged" mode | |
3472 | * 1: manual mode | |
3473 | * 2: native EC "auto" mode (recommended, hardware default) | |
3474 | * | |
3475 | * pwm*: set speed in manual mode, ignored otherwise. | |
3476 | * 0 is level 0; 255 is level 7. Intermediate points done with linear | |
3477 | * interpolation. | |
3478 | * | |
3479 | * fan*_input: tachometer reading, RPM | |
3480 | * | |
3481 | * | |
3482 | * SYSFS fan layout: extensions | |
3483 | * | |
3484 | * fan_watchdog (driver): | |
3485 | * fan watchdog interval in seconds, 0 disables (default), max 120 | |
3486 | */ | |
3487 | ||
3488 | /* sysfs fan pwm1_enable ----------------------------------------------- */ | |
3489 | static ssize_t fan_pwm1_enable_show(struct device *dev, | |
3490 | struct device_attribute *attr, | |
3491 | char *buf) | |
3492 | { | |
3493 | int res, mode; | |
3494 | u8 status; | |
3495 | ||
3496 | res = fan_get_status_safe(&status); | |
3497 | if (res) | |
3498 | return res; | |
3499 | ||
3500 | if (unlikely(tp_features.fan_ctrl_status_undef)) { | |
3501 | if (status != fan_control_initial_status) { | |
3502 | tp_features.fan_ctrl_status_undef = 0; | |
3503 | } else { | |
3504 | /* Return most likely status. In fact, it | |
3505 | * might be the only possible status */ | |
3506 | status = TP_EC_FAN_AUTO; | |
3507 | } | |
3508 | } | |
3509 | ||
3510 | if (status & TP_EC_FAN_FULLSPEED) { | |
3511 | mode = 0; | |
3512 | } else if (status & TP_EC_FAN_AUTO) { | |
3513 | mode = 2; | |
3514 | } else | |
3515 | mode = 1; | |
3516 | ||
3517 | return snprintf(buf, PAGE_SIZE, "%d\n", mode); | |
3518 | } | |
3519 | ||
3520 | static ssize_t fan_pwm1_enable_store(struct device *dev, | |
3521 | struct device_attribute *attr, | |
3522 | const char *buf, size_t count) | |
3523 | { | |
3524 | unsigned long t; | |
3525 | int res, level; | |
3526 | ||
3527 | if (parse_strtoul(buf, 2, &t)) | |
3528 | return -EINVAL; | |
3529 | ||
3530 | switch (t) { | |
3531 | case 0: | |
3532 | level = TP_EC_FAN_FULLSPEED; | |
3533 | break; | |
3534 | case 1: | |
3535 | level = TPACPI_FAN_LAST_LEVEL; | |
3536 | break; | |
3537 | case 2: | |
3538 | level = TP_EC_FAN_AUTO; | |
3539 | break; | |
3540 | case 3: | |
3541 | /* reserved for software-controlled auto mode */ | |
3542 | return -ENOSYS; | |
3543 | default: | |
3544 | return -EINVAL; | |
3545 | } | |
3546 | ||
3547 | res = fan_set_level_safe(level); | |
c573ddb9 HMH |
3548 | if (res == -ENXIO) |
3549 | return -EINVAL; | |
3550 | else if (res < 0) | |
fe98a52c HMH |
3551 | return res; |
3552 | ||
3553 | fan_watchdog_reset(); | |
3554 | ||
3555 | return count; | |
3556 | } | |
3557 | ||
3558 | static struct device_attribute dev_attr_fan_pwm1_enable = | |
3559 | __ATTR(pwm1_enable, S_IWUSR | S_IRUGO, | |
3560 | fan_pwm1_enable_show, fan_pwm1_enable_store); | |
3561 | ||
3562 | /* sysfs fan pwm1 ------------------------------------------------------ */ | |
3563 | static ssize_t fan_pwm1_show(struct device *dev, | |
3564 | struct device_attribute *attr, | |
3565 | char *buf) | |
3566 | { | |
3567 | int res; | |
3568 | u8 status; | |
3569 | ||
3570 | res = fan_get_status_safe(&status); | |
3571 | if (res) | |
3572 | return res; | |
3573 | ||
3574 | if (unlikely(tp_features.fan_ctrl_status_undef)) { | |
3575 | if (status != fan_control_initial_status) { | |
3576 | tp_features.fan_ctrl_status_undef = 0; | |
3577 | } else { | |
3578 | status = TP_EC_FAN_AUTO; | |
3579 | } | |
3580 | } | |
3581 | ||
3582 | if ((status & | |
3583 | (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0) | |
3584 | status = fan_control_desired_level; | |
3585 | ||
3586 | if (status > 7) | |
3587 | status = 7; | |
3588 | ||
3589 | return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7); | |
3590 | } | |
3591 | ||
3592 | static ssize_t fan_pwm1_store(struct device *dev, | |
3593 | struct device_attribute *attr, | |
3594 | const char *buf, size_t count) | |
3595 | { | |
3596 | unsigned long s; | |
3597 | int rc; | |
3598 | u8 status, newlevel; | |
3599 | ||
3600 | if (parse_strtoul(buf, 255, &s)) | |
3601 | return -EINVAL; | |
3602 | ||
3603 | /* scale down from 0-255 to 0-7 */ | |
3604 | newlevel = (s >> 5) & 0x07; | |
3605 | ||
3606 | rc = mutex_lock_interruptible(&fan_mutex); | |
3607 | if (rc < 0) | |
3608 | return rc; | |
3609 | ||
3610 | rc = fan_get_status(&status); | |
3611 | if (!rc && (status & | |
3612 | (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) { | |
3613 | rc = fan_set_level(newlevel); | |
c573ddb9 HMH |
3614 | if (rc == -ENXIO) |
3615 | rc = -EINVAL; | |
3616 | else if (!rc) { | |
fe98a52c | 3617 | fan_update_desired_level(newlevel); |
ca4ac2f4 HMH |
3618 | fan_watchdog_reset(); |
3619 | } | |
fe98a52c HMH |
3620 | } |
3621 | ||
3622 | mutex_unlock(&fan_mutex); | |
3623 | return (rc)? rc : count; | |
3624 | } | |
3625 | ||
3626 | static struct device_attribute dev_attr_fan_pwm1 = | |
3627 | __ATTR(pwm1, S_IWUSR | S_IRUGO, | |
3628 | fan_pwm1_show, fan_pwm1_store); | |
3629 | ||
3630 | /* sysfs fan fan1_input ------------------------------------------------ */ | |
3631 | static ssize_t fan_fan1_input_show(struct device *dev, | |
3632 | struct device_attribute *attr, | |
3633 | char *buf) | |
3634 | { | |
3635 | int res; | |
3636 | unsigned int speed; | |
3637 | ||
3638 | res = fan_get_speed(&speed); | |
3639 | if (res < 0) | |
3640 | return res; | |
3641 | ||
3642 | return snprintf(buf, PAGE_SIZE, "%u\n", speed); | |
3643 | } | |
3644 | ||
3645 | static struct device_attribute dev_attr_fan_fan1_input = | |
3646 | __ATTR(fan1_input, S_IRUGO, | |
3647 | fan_fan1_input_show, NULL); | |
3648 | ||
3649 | /* sysfs fan fan_watchdog (driver) ------------------------------------- */ | |
3650 | static ssize_t fan_fan_watchdog_show(struct device_driver *drv, | |
3651 | char *buf) | |
3652 | { | |
3653 | return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval); | |
3654 | } | |
3655 | ||
3656 | static ssize_t fan_fan_watchdog_store(struct device_driver *drv, | |
3657 | const char *buf, size_t count) | |
3658 | { | |
3659 | unsigned long t; | |
3660 | ||
3661 | if (parse_strtoul(buf, 120, &t)) | |
3662 | return -EINVAL; | |
3663 | ||
ecf2a80a HMH |
3664 | if (!fan_control_allowed) |
3665 | return -EPERM; | |
3666 | ||
fe98a52c HMH |
3667 | fan_watchdog_maxinterval = t; |
3668 | fan_watchdog_reset(); | |
3669 | ||
3670 | return count; | |
3671 | } | |
3672 | ||
3673 | static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO, | |
3674 | fan_fan_watchdog_show, fan_fan_watchdog_store); | |
3675 | ||
3676 | /* --------------------------------------------------------------------- */ | |
3677 | static struct attribute *fan_attributes[] = { | |
3678 | &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr, | |
3679 | &dev_attr_fan_fan1_input.attr, | |
3680 | NULL | |
3681 | }; | |
3682 | ||
3683 | static const struct attribute_group fan_attr_group = { | |
3684 | .attrs = fan_attributes, | |
3685 | }; | |
3686 | ||
a5763f22 | 3687 | static int __init fan_init(struct ibm_init_struct *iibm) |
78f81cc4 | 3688 | { |
fe98a52c HMH |
3689 | int rc; |
3690 | ||
fe08bc4b HMH |
3691 | vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n"); |
3692 | ||
40ca9fdf | 3693 | mutex_init(&fan_mutex); |
efa27145 HMH |
3694 | fan_status_access_mode = TPACPI_FAN_NONE; |
3695 | fan_control_access_mode = TPACPI_FAN_WR_NONE; | |
69ba91cb | 3696 | fan_control_commands = 0; |
16663a87 | 3697 | fan_watchdog_maxinterval = 0; |
d8fd94d9 | 3698 | tp_features.fan_ctrl_status_undef = 0; |
fe98a52c | 3699 | fan_control_desired_level = 7; |
78f81cc4 | 3700 | |
8d376cd6 HMH |
3701 | IBM_ACPIHANDLE_INIT(fans); |
3702 | IBM_ACPIHANDLE_INIT(gfan); | |
3703 | IBM_ACPIHANDLE_INIT(sfan); | |
5fba344c | 3704 | |
78f81cc4 BD |
3705 | if (gfan_handle) { |
3706 | /* 570, 600e/x, 770e, 770x */ | |
efa27145 | 3707 | fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN; |
69ba91cb HMH |
3708 | } else { |
3709 | /* all other ThinkPads: note that even old-style | |
3710 | * ThinkPad ECs supports the fan control register */ | |
778b4d74 HMH |
3711 | if (likely(acpi_ec_read(fan_status_offset, |
3712 | &fan_control_initial_status))) { | |
efa27145 | 3713 | fan_status_access_mode = TPACPI_FAN_RD_TPEC; |
778b4d74 HMH |
3714 | |
3715 | /* In some ThinkPads, neither the EC nor the ACPI | |
3716 | * DSDT initialize the fan status, and it ends up | |
3717 | * being set to 0x07 when it *could* be either | |
3718 | * 0x07 or 0x80. | |
3719 | * | |
3720 | * Enable for TP-1Y (T43), TP-78 (R51e), | |
3721 | * TP-76 (R52), TP-70 (T43, R52), which are known | |
3722 | * to be buggy. */ | |
d5a2f2f1 HMH |
3723 | if (fan_control_initial_status == 0x07) { |
3724 | switch (thinkpad_id.ec_model) { | |
3725 | case 0x5931: /* TP-1Y */ | |
3726 | case 0x3837: /* TP-78 */ | |
3727 | case 0x3637: /* TP-76 */ | |
3728 | case 0x3037: /* TP-70 */ | |
3729 | printk(IBM_NOTICE | |
3730 | "fan_init: initial fan status is " | |
3731 | "unknown, assuming it is in auto " | |
3732 | "mode\n"); | |
3733 | tp_features.fan_ctrl_status_undef = 1; | |
3734 | ;; | |
3735 | } | |
778b4d74 | 3736 | } |
69ba91cb HMH |
3737 | } else { |
3738 | printk(IBM_ERR | |
3739 | "ThinkPad ACPI EC access misbehaving, " | |
3740 | "fan status and control unavailable\n"); | |
5fba344c | 3741 | return 1; |
69ba91cb HMH |
3742 | } |
3743 | } | |
78f81cc4 | 3744 | |
69ba91cb HMH |
3745 | if (sfan_handle) { |
3746 | /* 570, 770x-JL */ | |
efa27145 | 3747 | fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN; |
1c6a334e | 3748 | fan_control_commands |= |
efa27145 | 3749 | TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE; |
78f81cc4 | 3750 | } else { |
69ba91cb HMH |
3751 | if (!gfan_handle) { |
3752 | /* gfan without sfan means no fan control */ | |
3753 | /* all other models implement TP EC 0x2f control */ | |
3754 | ||
3755 | if (fans_handle) { | |
a8b7a662 | 3756 | /* X31, X40, X41 */ |
69ba91cb | 3757 | fan_control_access_mode = |
efa27145 | 3758 | TPACPI_FAN_WR_ACPI_FANS; |
69ba91cb | 3759 | fan_control_commands |= |
efa27145 HMH |
3760 | TPACPI_FAN_CMD_SPEED | |
3761 | TPACPI_FAN_CMD_LEVEL | | |
3762 | TPACPI_FAN_CMD_ENABLE; | |
69ba91cb | 3763 | } else { |
efa27145 | 3764 | fan_control_access_mode = TPACPI_FAN_WR_TPEC; |
a12095c2 | 3765 | fan_control_commands |= |
efa27145 HMH |
3766 | TPACPI_FAN_CMD_LEVEL | |
3767 | TPACPI_FAN_CMD_ENABLE; | |
69ba91cb HMH |
3768 | } |
3769 | } | |
3770 | } | |
3771 | ||
fe08bc4b HMH |
3772 | vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n", |
3773 | str_supported(fan_status_access_mode != TPACPI_FAN_NONE || | |
3774 | fan_control_access_mode != TPACPI_FAN_WR_NONE), | |
3775 | fan_status_access_mode, fan_control_access_mode); | |
3776 | ||
ecf2a80a HMH |
3777 | /* fan control master switch */ |
3778 | if (!fan_control_allowed) { | |
3779 | fan_control_access_mode = TPACPI_FAN_WR_NONE; | |
3780 | fan_control_commands = 0; | |
3781 | dbg_printk(TPACPI_DBG_INIT, | |
3782 | "fan control features disabled by parameter\n"); | |
3783 | } | |
3784 | ||
fe98a52c HMH |
3785 | /* update fan_control_desired_level */ |
3786 | if (fan_status_access_mode != TPACPI_FAN_NONE) | |
3787 | fan_get_status_safe(NULL); | |
3788 | ||
3789 | if (fan_status_access_mode != TPACPI_FAN_NONE || | |
3790 | fan_control_access_mode != TPACPI_FAN_WR_NONE) { | |
3791 | rc = sysfs_create_group(&tpacpi_pdev->dev.kobj, | |
3792 | &fan_attr_group); | |
3793 | if (!(rc < 0)) | |
3794 | rc = driver_create_file(&tpacpi_pdriver.driver, | |
3795 | &driver_attr_fan_watchdog); | |
3796 | if (rc < 0) | |
3797 | return rc; | |
3798 | return 0; | |
3799 | } else | |
3800 | return 1; | |
3801 | } | |
3802 | ||
3803 | /* | |
3804 | * Call with fan_mutex held | |
3805 | */ | |
3806 | static void fan_update_desired_level(u8 status) | |
3807 | { | |
3808 | if ((status & | |
3809 | (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) { | |
3810 | if (status > 7) | |
3811 | fan_control_desired_level = 7; | |
3812 | else | |
3813 | fan_control_desired_level = status; | |
3814 | } | |
69ba91cb | 3815 | } |
78f81cc4 | 3816 | |
c52f0aa5 | 3817 | static int fan_get_status(u8 *status) |
78f81cc4 | 3818 | { |
c52f0aa5 | 3819 | u8 s; |
78f81cc4 | 3820 | |
a8b7a662 | 3821 | /* TODO: |
efa27145 | 3822 | * Add TPACPI_FAN_RD_ACPI_FANS ? */ |
a8b7a662 | 3823 | |
3ef8a609 | 3824 | switch (fan_status_access_mode) { |
efa27145 | 3825 | case TPACPI_FAN_RD_ACPI_GFAN: |
78f81cc4 | 3826 | /* 570, 600e/x, 770e, 770x */ |
c52f0aa5 HMH |
3827 | |
3828 | if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d"))) | |
1da177e4 | 3829 | return -EIO; |
78f81cc4 | 3830 | |
c52f0aa5 HMH |
3831 | if (likely(status)) |
3832 | *status = s & 0x07; | |
3ef8a609 HMH |
3833 | |
3834 | break; | |
3835 | ||
efa27145 | 3836 | case TPACPI_FAN_RD_TPEC: |
78f81cc4 | 3837 | /* all except 570, 600e/x, 770e, 770x */ |
c52f0aa5 | 3838 | if (unlikely(!acpi_ec_read(fan_status_offset, &s))) |
3ef8a609 | 3839 | return -EIO; |
78f81cc4 | 3840 | |
c52f0aa5 HMH |
3841 | if (likely(status)) |
3842 | *status = s; | |
3843 | ||
3844 | break; | |
3845 | ||
3846 | default: | |
3847 | return -ENXIO; | |
78f81cc4 BD |
3848 | } |
3849 | ||
c52f0aa5 HMH |
3850 | return 0; |
3851 | } | |
3852 | ||
fe98a52c HMH |
3853 | static int fan_get_status_safe(u8 *status) |
3854 | { | |
3855 | int rc; | |
3856 | u8 s; | |
3857 | ||
3858 | rc = mutex_lock_interruptible(&fan_mutex); | |
3859 | if (rc < 0) | |
3860 | return rc; | |
3861 | rc = fan_get_status(&s); | |
3862 | if (!rc) | |
3863 | fan_update_desired_level(s); | |
3864 | mutex_unlock(&fan_mutex); | |
3865 | ||
3866 | if (status) | |
3867 | *status = s; | |
3868 | ||
3869 | return rc; | |
3870 | } | |
3871 | ||
56b6aeb0 HMH |
3872 | static void fan_exit(void) |
3873 | { | |
99fba3f8 | 3874 | vdbg_printk(TPACPI_DBG_EXIT, "cancelling any pending fan watchdog tasks\n"); |
fe98a52c HMH |
3875 | |
3876 | /* FIXME: can we really do this unconditionally? */ | |
3877 | sysfs_remove_group(&tpacpi_pdev->dev.kobj, &fan_attr_group); | |
3878 | driver_remove_file(&tpacpi_pdriver.driver, &driver_attr_fan_watchdog); | |
3879 | ||
56b6aeb0 HMH |
3880 | cancel_delayed_work(&fan_watchdog_task); |
3881 | flush_scheduled_work(); | |
3882 | } | |
3883 | ||
c52f0aa5 HMH |
3884 | static int fan_get_speed(unsigned int *speed) |
3885 | { | |
3886 | u8 hi, lo; | |
3887 | ||
3888 | switch (fan_status_access_mode) { | |
efa27145 | 3889 | case TPACPI_FAN_RD_TPEC: |
78f81cc4 | 3890 | /* all except 570, 600e/x, 770e, 770x */ |
3ef8a609 HMH |
3891 | if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) || |
3892 | !acpi_ec_read(fan_rpm_offset + 1, &hi))) | |
3893 | return -EIO; | |
3ef8a609 | 3894 | |
c52f0aa5 HMH |
3895 | if (likely(speed)) |
3896 | *speed = (hi << 8) | lo; | |
3897 | ||
3898 | break; | |
3899 | ||
3900 | default: | |
3901 | return -ENXIO; | |
3902 | } | |
3903 | ||
3904 | return 0; | |
3905 | } | |
3906 | ||
56b6aeb0 | 3907 | static void fan_watchdog_fire(struct work_struct *ignored) |
16663a87 | 3908 | { |
99fba3f8 HMH |
3909 | int rc; |
3910 | ||
8fef502e HMH |
3911 | if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING) |
3912 | return; | |
3913 | ||
56b6aeb0 | 3914 | printk(IBM_NOTICE "fan watchdog: enabling fan\n"); |
99fba3f8 HMH |
3915 | rc = fan_set_enable(); |
3916 | if (rc < 0) { | |
3917 | printk(IBM_ERR "fan watchdog: error %d while enabling fan, " | |
3918 | "will try again later...\n", -rc); | |
56b6aeb0 HMH |
3919 | /* reschedule for later */ |
3920 | fan_watchdog_reset(); | |
3921 | } | |
16663a87 HMH |
3922 | } |
3923 | ||
3924 | static void fan_watchdog_reset(void) | |
3925 | { | |
94954cc6 | 3926 | static int fan_watchdog_active; |
16663a87 | 3927 | |
4985cd0a HMH |
3928 | if (fan_control_access_mode == TPACPI_FAN_WR_NONE) |
3929 | return; | |
3930 | ||
16663a87 HMH |
3931 | if (fan_watchdog_active) |
3932 | cancel_delayed_work(&fan_watchdog_task); | |
3933 | ||
8fef502e HMH |
3934 | if (fan_watchdog_maxinterval > 0 && |
3935 | tpacpi_lifecycle != TPACPI_LIFE_EXITING) { | |
16663a87 HMH |
3936 | fan_watchdog_active = 1; |
3937 | if (!schedule_delayed_work(&fan_watchdog_task, | |
3938 | msecs_to_jiffies(fan_watchdog_maxinterval | |
3939 | * 1000))) { | |
3940 | printk(IBM_ERR "failed to schedule the fan watchdog, " | |
3941 | "watchdog will not trigger\n"); | |
3942 | } | |
3943 | } else | |
3944 | fan_watchdog_active = 0; | |
3945 | } | |
3946 | ||
18ad7996 | 3947 | static int fan_set_level(int level) |
78f81cc4 | 3948 | { |
ecf2a80a HMH |
3949 | if (!fan_control_allowed) |
3950 | return -EPERM; | |
3951 | ||
18ad7996 | 3952 | switch (fan_control_access_mode) { |
efa27145 | 3953 | case TPACPI_FAN_WR_ACPI_SFAN: |
18ad7996 | 3954 | if (level >= 0 && level <= 7) { |
fe98a52c | 3955 | if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level)) |
78f81cc4 | 3956 | return -EIO; |
18ad7996 HMH |
3957 | } else |
3958 | return -EINVAL; | |
3959 | break; | |
3960 | ||
efa27145 HMH |
3961 | case TPACPI_FAN_WR_ACPI_FANS: |
3962 | case TPACPI_FAN_WR_TPEC: | |
3963 | if ((level != TP_EC_FAN_AUTO) && | |
3964 | (level != TP_EC_FAN_FULLSPEED) && | |
a12095c2 HMH |
3965 | ((level < 0) || (level > 7))) |
3966 | return -EINVAL; | |
3967 | ||
eaa7571b HMH |
3968 | /* safety net should the EC not support AUTO |
3969 | * or FULLSPEED mode bits and just ignore them */ | |
3970 | if (level & TP_EC_FAN_FULLSPEED) | |
3971 | level |= 7; /* safety min speed 7 */ | |
3972 | else if (level & TP_EC_FAN_FULLSPEED) | |
3973 | level |= 4; /* safety min speed 4 */ | |
3974 | ||
fe98a52c | 3975 | if (!acpi_ec_write(fan_status_offset, level)) |
a12095c2 | 3976 | return -EIO; |
778b4d74 | 3977 | else |
d8fd94d9 | 3978 | tp_features.fan_ctrl_status_undef = 0; |
a12095c2 HMH |
3979 | break; |
3980 | ||
18ad7996 HMH |
3981 | default: |
3982 | return -ENXIO; | |
3983 | } | |
3984 | return 0; | |
3985 | } | |
3986 | ||
fe98a52c HMH |
3987 | static int fan_set_level_safe(int level) |
3988 | { | |
3989 | int rc; | |
3990 | ||
ecf2a80a HMH |
3991 | if (!fan_control_allowed) |
3992 | return -EPERM; | |
3993 | ||
fe98a52c HMH |
3994 | rc = mutex_lock_interruptible(&fan_mutex); |
3995 | if (rc < 0) | |
3996 | return rc; | |
3997 | ||
3998 | if (level == TPACPI_FAN_LAST_LEVEL) | |
3999 | level = fan_control_desired_level; | |
4000 | ||
4001 | rc = fan_set_level(level); | |
4002 | if (!rc) | |
4003 | fan_update_desired_level(level); | |
4004 | ||
4005 | mutex_unlock(&fan_mutex); | |
4006 | return rc; | |
4007 | } | |
4008 | ||
18ad7996 HMH |
4009 | static int fan_set_enable(void) |
4010 | { | |
1c6a334e HMH |
4011 | u8 s; |
4012 | int rc; | |
4013 | ||
ecf2a80a HMH |
4014 | if (!fan_control_allowed) |
4015 | return -EPERM; | |
4016 | ||
40ca9fdf HMH |
4017 | rc = mutex_lock_interruptible(&fan_mutex); |
4018 | if (rc < 0) | |
4019 | return rc; | |
4020 | ||
18ad7996 | 4021 | switch (fan_control_access_mode) { |
efa27145 HMH |
4022 | case TPACPI_FAN_WR_ACPI_FANS: |
4023 | case TPACPI_FAN_WR_TPEC: | |
40ca9fdf HMH |
4024 | rc = fan_get_status(&s); |
4025 | if (rc < 0) | |
4026 | break; | |
1c6a334e HMH |
4027 | |
4028 | /* Don't go out of emergency fan mode */ | |
eaa7571b HMH |
4029 | if (s != 7) { |
4030 | s &= 0x07; | |
4031 | s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */ | |
4032 | } | |
1c6a334e HMH |
4033 | |
4034 | if (!acpi_ec_write(fan_status_offset, s)) | |
40ca9fdf HMH |
4035 | rc = -EIO; |
4036 | else { | |
d8fd94d9 | 4037 | tp_features.fan_ctrl_status_undef = 0; |
40ca9fdf HMH |
4038 | rc = 0; |
4039 | } | |
1c6a334e HMH |
4040 | break; |
4041 | ||
efa27145 | 4042 | case TPACPI_FAN_WR_ACPI_SFAN: |
40ca9fdf HMH |
4043 | rc = fan_get_status(&s); |
4044 | if (rc < 0) | |
4045 | break; | |
1c6a334e HMH |
4046 | |
4047 | s &= 0x07; | |
4048 | ||
4049 | /* Set fan to at least level 4 */ | |
eaa7571b | 4050 | s |= 4; |
1c6a334e HMH |
4051 | |
4052 | if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s)) | |
40ca9fdf HMH |
4053 | rc= -EIO; |
4054 | else | |
4055 | rc = 0; | |
18ad7996 HMH |
4056 | break; |
4057 | ||
4058 | default: | |
40ca9fdf | 4059 | rc = -ENXIO; |
18ad7996 | 4060 | } |
40ca9fdf HMH |
4061 | |
4062 | mutex_unlock(&fan_mutex); | |
4063 | return rc; | |
18ad7996 HMH |
4064 | } |
4065 | ||
4066 | static int fan_set_disable(void) | |
4067 | { | |
40ca9fdf HMH |
4068 | int rc; |
4069 | ||
ecf2a80a HMH |
4070 | if (!fan_control_allowed) |
4071 | return -EPERM; | |
4072 | ||
40ca9fdf HMH |
4073 | rc = mutex_lock_interruptible(&fan_mutex); |
4074 | if (rc < 0) | |
4075 | return rc; | |
4076 | ||
4077 | rc = 0; | |
18ad7996 | 4078 | switch (fan_control_access_mode) { |
efa27145 HMH |
4079 | case TPACPI_FAN_WR_ACPI_FANS: |
4080 | case TPACPI_FAN_WR_TPEC: | |
18ad7996 | 4081 | if (!acpi_ec_write(fan_status_offset, 0x00)) |
40ca9fdf | 4082 | rc = -EIO; |
fe98a52c HMH |
4083 | else { |
4084 | fan_control_desired_level = 0; | |
d8fd94d9 | 4085 | tp_features.fan_ctrl_status_undef = 0; |
fe98a52c | 4086 | } |
18ad7996 HMH |
4087 | break; |
4088 | ||
efa27145 | 4089 | case TPACPI_FAN_WR_ACPI_SFAN: |
1c6a334e | 4090 | if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00)) |
40ca9fdf | 4091 | rc = -EIO; |
fe98a52c HMH |
4092 | else |
4093 | fan_control_desired_level = 0; | |
1c6a334e HMH |
4094 | break; |
4095 | ||
18ad7996 | 4096 | default: |
40ca9fdf | 4097 | rc = -ENXIO; |
18ad7996 | 4098 | } |
40ca9fdf | 4099 | |
fe98a52c | 4100 | |
40ca9fdf HMH |
4101 | mutex_unlock(&fan_mutex); |
4102 | return rc; | |
56b6aeb0 HMH |
4103 | } |
4104 | ||
4105 | static int fan_set_speed(int speed) | |
4106 | { | |
40ca9fdf HMH |
4107 | int rc; |
4108 | ||
ecf2a80a HMH |
4109 | if (!fan_control_allowed) |
4110 | return -EPERM; | |
4111 | ||
40ca9fdf HMH |
4112 | rc = mutex_lock_interruptible(&fan_mutex); |
4113 | if (rc < 0) | |
4114 | return rc; | |
4115 | ||
4116 | rc = 0; | |
56b6aeb0 | 4117 | switch (fan_control_access_mode) { |
efa27145 | 4118 | case TPACPI_FAN_WR_ACPI_FANS: |
56b6aeb0 HMH |
4119 | if (speed >= 0 && speed <= 65535) { |
4120 | if (!acpi_evalf(fans_handle, NULL, NULL, "vddd", | |
4121 | speed, speed, speed)) | |
40ca9fdf | 4122 | rc = -EIO; |
56b6aeb0 | 4123 | } else |
40ca9fdf | 4124 | rc = -EINVAL; |
56b6aeb0 HMH |
4125 | break; |
4126 | ||
4127 | default: | |
40ca9fdf | 4128 | rc = -ENXIO; |
56b6aeb0 | 4129 | } |
40ca9fdf HMH |
4130 | |
4131 | mutex_unlock(&fan_mutex); | |
4132 | return rc; | |
56b6aeb0 HMH |
4133 | } |
4134 | ||
4135 | static int fan_read(char *p) | |
4136 | { | |
4137 | int len = 0; | |
4138 | int rc; | |
4139 | u8 status; | |
4140 | unsigned int speed = 0; | |
4141 | ||
4142 | switch (fan_status_access_mode) { | |
efa27145 | 4143 | case TPACPI_FAN_RD_ACPI_GFAN: |
56b6aeb0 | 4144 | /* 570, 600e/x, 770e, 770x */ |
fe98a52c | 4145 | if ((rc = fan_get_status_safe(&status)) < 0) |
56b6aeb0 HMH |
4146 | return rc; |
4147 | ||
4148 | len += sprintf(p + len, "status:\t\t%s\n" | |
4149 | "level:\t\t%d\n", | |
4150 | (status != 0) ? "enabled" : "disabled", status); | |
4151 | break; | |
4152 | ||
efa27145 | 4153 | case TPACPI_FAN_RD_TPEC: |
56b6aeb0 | 4154 | /* all except 570, 600e/x, 770e, 770x */ |
fe98a52c | 4155 | if ((rc = fan_get_status_safe(&status)) < 0) |
56b6aeb0 HMH |
4156 | return rc; |
4157 | ||
d8fd94d9 | 4158 | if (unlikely(tp_features.fan_ctrl_status_undef)) { |
56b6aeb0 | 4159 | if (status != fan_control_initial_status) |
d8fd94d9 | 4160 | tp_features.fan_ctrl_status_undef = 0; |
56b6aeb0 HMH |
4161 | else |
4162 | /* Return most likely status. In fact, it | |
4163 | * might be the only possible status */ | |
efa27145 | 4164 | status = TP_EC_FAN_AUTO; |
56b6aeb0 HMH |
4165 | } |
4166 | ||
4167 | len += sprintf(p + len, "status:\t\t%s\n", | |
4168 | (status != 0) ? "enabled" : "disabled"); | |
4169 | ||
56b6aeb0 HMH |
4170 | if ((rc = fan_get_speed(&speed)) < 0) |
4171 | return rc; | |
4172 | ||
4173 | len += sprintf(p + len, "speed:\t\t%d\n", speed); | |
4174 | ||
efa27145 | 4175 | if (status & TP_EC_FAN_FULLSPEED) |
56b6aeb0 HMH |
4176 | /* Disengaged mode takes precedence */ |
4177 | len += sprintf(p + len, "level:\t\tdisengaged\n"); | |
efa27145 | 4178 | else if (status & TP_EC_FAN_AUTO) |
56b6aeb0 HMH |
4179 | len += sprintf(p + len, "level:\t\tauto\n"); |
4180 | else | |
4181 | len += sprintf(p + len, "level:\t\t%d\n", status); | |
4182 | break; | |
4183 | ||
efa27145 | 4184 | case TPACPI_FAN_NONE: |
56b6aeb0 HMH |
4185 | default: |
4186 | len += sprintf(p + len, "status:\t\tnot supported\n"); | |
4187 | } | |
4188 | ||
efa27145 | 4189 | if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) { |
56b6aeb0 HMH |
4190 | len += sprintf(p + len, "commands:\tlevel <level>"); |
4191 | ||
4192 | switch (fan_control_access_mode) { | |
efa27145 | 4193 | case TPACPI_FAN_WR_ACPI_SFAN: |
56b6aeb0 HMH |
4194 | len += sprintf(p + len, " (<level> is 0-7)\n"); |
4195 | break; | |
4196 | ||
4197 | default: | |
4198 | len += sprintf(p + len, " (<level> is 0-7, " | |
fe98a52c | 4199 | "auto, disengaged, full-speed)\n"); |
56b6aeb0 HMH |
4200 | break; |
4201 | } | |
4202 | } | |
18ad7996 | 4203 | |
efa27145 | 4204 | if (fan_control_commands & TPACPI_FAN_CMD_ENABLE) |
56b6aeb0 HMH |
4205 | len += sprintf(p + len, "commands:\tenable, disable\n" |
4206 | "commands:\twatchdog <timeout> (<timeout> is 0 (off), " | |
4207 | "1-120 (seconds))\n"); | |
1da177e4 | 4208 | |
efa27145 | 4209 | if (fan_control_commands & TPACPI_FAN_CMD_SPEED) |
56b6aeb0 HMH |
4210 | len += sprintf(p + len, "commands:\tspeed <speed>" |
4211 | " (<speed> is 0-65535)\n"); | |
4212 | ||
4213 | return len; | |
78f81cc4 BD |
4214 | } |
4215 | ||
18ad7996 HMH |
4216 | static int fan_write_cmd_level(const char *cmd, int *rc) |
4217 | { | |
4218 | int level; | |
4219 | ||
a12095c2 | 4220 | if (strlencmp(cmd, "level auto") == 0) |
efa27145 | 4221 | level = TP_EC_FAN_AUTO; |
fe98a52c HMH |
4222 | else if ((strlencmp(cmd, "level disengaged") == 0) | |
4223 | (strlencmp(cmd, "level full-speed") == 0)) | |
efa27145 | 4224 | level = TP_EC_FAN_FULLSPEED; |
a12095c2 | 4225 | else if (sscanf(cmd, "level %d", &level) != 1) |
18ad7996 HMH |
4226 | return 0; |
4227 | ||
fe98a52c | 4228 | if ((*rc = fan_set_level_safe(level)) == -ENXIO) |
18ad7996 HMH |
4229 | printk(IBM_ERR "level command accepted for unsupported " |
4230 | "access mode %d", fan_control_access_mode); | |
4231 | ||
4232 | return 1; | |
4233 | } | |
4234 | ||
4235 | static int fan_write_cmd_enable(const char *cmd, int *rc) | |
4236 | { | |
4237 | if (strlencmp(cmd, "enable") != 0) | |
4238 | return 0; | |
4239 | ||
4240 | if ((*rc = fan_set_enable()) == -ENXIO) | |
4241 | printk(IBM_ERR "enable command accepted for unsupported " | |
4242 | "access mode %d", fan_control_access_mode); | |
4243 | ||
4244 | return 1; | |
4245 | } | |
4246 | ||
4247 | static int fan_write_cmd_disable(const char *cmd, int *rc) | |
4248 | { | |
4249 | if (strlencmp(cmd, "disable") != 0) | |
4250 | return 0; | |
4251 | ||
4252 | if ((*rc = fan_set_disable()) == -ENXIO) | |
4253 | printk(IBM_ERR "disable command accepted for unsupported " | |
4254 | "access mode %d", fan_control_access_mode); | |
4255 | ||
4256 | return 1; | |
4257 | } | |
4258 | ||
4259 | static int fan_write_cmd_speed(const char *cmd, int *rc) | |
4260 | { | |
4261 | int speed; | |
4262 | ||
a8b7a662 HMH |
4263 | /* TODO: |
4264 | * Support speed <low> <medium> <high> ? */ | |
4265 | ||
18ad7996 HMH |
4266 | if (sscanf(cmd, "speed %d", &speed) != 1) |
4267 | return 0; | |
4268 | ||
4269 | if ((*rc = fan_set_speed(speed)) == -ENXIO) | |
4270 | printk(IBM_ERR "speed command accepted for unsupported " | |
4271 | "access mode %d", fan_control_access_mode); | |
4272 | ||
4273 | return 1; | |
4274 | } | |
4275 | ||
16663a87 HMH |
4276 | static int fan_write_cmd_watchdog(const char *cmd, int *rc) |
4277 | { | |
4278 | int interval; | |
4279 | ||
4280 | if (sscanf(cmd, "watchdog %d", &interval) != 1) | |
4281 | return 0; | |
4282 | ||
4283 | if (interval < 0 || interval > 120) | |
4284 | *rc = -EINVAL; | |
4285 | else | |
4286 | fan_watchdog_maxinterval = interval; | |
4287 | ||
4288 | return 1; | |
4289 | } | |
4290 | ||
18ad7996 HMH |
4291 | static int fan_write(char *buf) |
4292 | { | |
4293 | char *cmd; | |
4294 | int rc = 0; | |
4295 | ||
4296 | while (!rc && (cmd = next_cmd(&buf))) { | |
efa27145 | 4297 | if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) && |
18ad7996 | 4298 | fan_write_cmd_level(cmd, &rc)) && |
efa27145 | 4299 | !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) && |
18ad7996 | 4300 | (fan_write_cmd_enable(cmd, &rc) || |
16663a87 HMH |
4301 | fan_write_cmd_disable(cmd, &rc) || |
4302 | fan_write_cmd_watchdog(cmd, &rc))) && | |
efa27145 | 4303 | !((fan_control_commands & TPACPI_FAN_CMD_SPEED) && |
18ad7996 HMH |
4304 | fan_write_cmd_speed(cmd, &rc)) |
4305 | ) | |
4306 | rc = -EINVAL; | |
16663a87 HMH |
4307 | else if (!rc) |
4308 | fan_watchdog_reset(); | |
18ad7996 HMH |
4309 | } |
4310 | ||
4311 | return rc; | |
4312 | } | |
4313 | ||
a5763f22 HMH |
4314 | static struct ibm_struct fan_driver_data = { |
4315 | .name = "fan", | |
4316 | .read = fan_read, | |
4317 | .write = fan_write, | |
4318 | .exit = fan_exit, | |
a5763f22 HMH |
4319 | }; |
4320 | ||
56b6aeb0 HMH |
4321 | /**************************************************************************** |
4322 | **************************************************************************** | |
4323 | * | |
4324 | * Infrastructure | |
4325 | * | |
4326 | **************************************************************************** | |
4327 | ****************************************************************************/ | |
4328 | ||
4329 | /* /proc support */ | |
94954cc6 | 4330 | static struct proc_dir_entry *proc_dir; |
16663a87 | 4331 | |
56b6aeb0 | 4332 | /* Subdriver registry */ |
a5763f22 HMH |
4333 | static LIST_HEAD(tpacpi_all_drivers); |
4334 | ||
1da177e4 | 4335 | |
56b6aeb0 HMH |
4336 | /* |
4337 | * Module and infrastructure proble, init and exit handling | |
4338 | */ | |
1da177e4 | 4339 | |
fe08bc4b | 4340 | #ifdef CONFIG_THINKPAD_ACPI_DEBUG |
a5763f22 | 4341 | static const char * __init str_supported(int is_supported) |
fe08bc4b | 4342 | { |
a5763f22 | 4343 | static char text_unsupported[] __initdata = "not supported"; |
fe08bc4b | 4344 | |
a5763f22 | 4345 | return (is_supported)? &text_unsupported[4] : &text_unsupported[0]; |
fe08bc4b HMH |
4346 | } |
4347 | #endif /* CONFIG_THINKPAD_ACPI_DEBUG */ | |
4348 | ||
a5763f22 | 4349 | static int __init ibm_init(struct ibm_init_struct *iibm) |
1da177e4 LT |
4350 | { |
4351 | int ret; | |
a5763f22 | 4352 | struct ibm_struct *ibm = iibm->data; |
1da177e4 LT |
4353 | struct proc_dir_entry *entry; |
4354 | ||
a5763f22 HMH |
4355 | BUG_ON(ibm == NULL); |
4356 | ||
4357 | INIT_LIST_HEAD(&ibm->all_drivers); | |
4358 | ||
92641177 | 4359 | if (ibm->flags.experimental && !experimental) |
1da177e4 LT |
4360 | return 0; |
4361 | ||
fe08bc4b HMH |
4362 | dbg_printk(TPACPI_DBG_INIT, |
4363 | "probing for %s\n", ibm->name); | |
4364 | ||
a5763f22 HMH |
4365 | if (iibm->init) { |
4366 | ret = iibm->init(iibm); | |
5fba344c HMH |
4367 | if (ret > 0) |
4368 | return 0; /* probe failed */ | |
4369 | if (ret) | |
1da177e4 | 4370 | return ret; |
a5763f22 | 4371 | |
92641177 | 4372 | ibm->flags.init_called = 1; |
1da177e4 LT |
4373 | } |
4374 | ||
8d376cd6 HMH |
4375 | if (ibm->acpi) { |
4376 | if (ibm->acpi->hid) { | |
4377 | ret = register_tpacpi_subdriver(ibm); | |
4378 | if (ret) | |
4379 | goto err_out; | |
4380 | } | |
5fba344c | 4381 | |
8d376cd6 HMH |
4382 | if (ibm->acpi->notify) { |
4383 | ret = setup_acpi_notify(ibm); | |
4384 | if (ret == -ENODEV) { | |
4385 | printk(IBM_NOTICE "disabling subdriver %s\n", | |
4386 | ibm->name); | |
4387 | ret = 0; | |
4388 | goto err_out; | |
4389 | } | |
4390 | if (ret < 0) | |
4391 | goto err_out; | |
5fba344c | 4392 | } |
5fba344c HMH |
4393 | } |
4394 | ||
fe08bc4b HMH |
4395 | dbg_printk(TPACPI_DBG_INIT, |
4396 | "%s installed\n", ibm->name); | |
4397 | ||
78f81cc4 BD |
4398 | if (ibm->read) { |
4399 | entry = create_proc_entry(ibm->name, | |
4400 | S_IFREG | S_IRUGO | S_IWUSR, | |
4401 | proc_dir); | |
4402 | if (!entry) { | |
4403 | printk(IBM_ERR "unable to create proc entry %s\n", | |
4404 | ibm->name); | |
5fba344c HMH |
4405 | ret = -ENODEV; |
4406 | goto err_out; | |
78f81cc4 BD |
4407 | } |
4408 | entry->owner = THIS_MODULE; | |
4409 | entry->data = ibm; | |
8d376cd6 | 4410 | entry->read_proc = &dispatch_procfs_read; |
78f81cc4 | 4411 | if (ibm->write) |
8d376cd6 | 4412 | entry->write_proc = &dispatch_procfs_write; |
92641177 | 4413 | ibm->flags.proc_created = 1; |
78f81cc4 | 4414 | } |
1da177e4 | 4415 | |
a5763f22 HMH |
4416 | list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers); |
4417 | ||
1da177e4 | 4418 | return 0; |
5fba344c HMH |
4419 | |
4420 | err_out: | |
fe08bc4b HMH |
4421 | dbg_printk(TPACPI_DBG_INIT, |
4422 | "%s: at error exit path with result %d\n", | |
4423 | ibm->name, ret); | |
4424 | ||
5fba344c HMH |
4425 | ibm_exit(ibm); |
4426 | return (ret < 0)? ret : 0; | |
1da177e4 LT |
4427 | } |
4428 | ||
4429 | static void ibm_exit(struct ibm_struct *ibm) | |
4430 | { | |
fe08bc4b | 4431 | dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name); |
a5763f22 HMH |
4432 | |
4433 | list_del_init(&ibm->all_drivers); | |
4434 | ||
8d376cd6 | 4435 | if (ibm->flags.acpi_notify_installed) { |
fe08bc4b HMH |
4436 | dbg_printk(TPACPI_DBG_EXIT, |
4437 | "%s: acpi_remove_notify_handler\n", ibm->name); | |
8d376cd6 HMH |
4438 | BUG_ON(!ibm->acpi); |
4439 | acpi_remove_notify_handler(*ibm->acpi->handle, | |
4440 | ibm->acpi->type, | |
4441 | dispatch_acpi_notify); | |
4442 | ibm->flags.acpi_notify_installed = 0; | |
4443 | ibm->flags.acpi_notify_installed = 0; | |
5fba344c | 4444 | } |
1da177e4 | 4445 | |
92641177 | 4446 | if (ibm->flags.proc_created) { |
fe08bc4b HMH |
4447 | dbg_printk(TPACPI_DBG_EXIT, |
4448 | "%s: remove_proc_entry\n", ibm->name); | |
1da177e4 | 4449 | remove_proc_entry(ibm->name, proc_dir); |
92641177 | 4450 | ibm->flags.proc_created = 0; |
5fba344c | 4451 | } |
1da177e4 | 4452 | |
8d376cd6 | 4453 | if (ibm->flags.acpi_driver_registered) { |
fe08bc4b HMH |
4454 | dbg_printk(TPACPI_DBG_EXIT, |
4455 | "%s: acpi_bus_unregister_driver\n", ibm->name); | |
8d376cd6 HMH |
4456 | BUG_ON(!ibm->acpi); |
4457 | acpi_bus_unregister_driver(ibm->acpi->driver); | |
4458 | kfree(ibm->acpi->driver); | |
4459 | ibm->acpi->driver = NULL; | |
4460 | ibm->flags.acpi_driver_registered = 0; | |
5fba344c HMH |
4461 | } |
4462 | ||
92641177 | 4463 | if (ibm->flags.init_called && ibm->exit) { |
5fba344c | 4464 | ibm->exit(); |
92641177 | 4465 | ibm->flags.init_called = 0; |
1da177e4 | 4466 | } |
a5763f22 HMH |
4467 | |
4468 | dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name); | |
1da177e4 LT |
4469 | } |
4470 | ||
56b6aeb0 HMH |
4471 | /* Probing */ |
4472 | ||
d5a2f2f1 | 4473 | static void __init get_thinkpad_model_data(struct thinkpad_id_data *tp) |
1da177e4 | 4474 | { |
56b6aeb0 HMH |
4475 | struct dmi_device *dev = NULL; |
4476 | char ec_fw_string[18]; | |
1da177e4 | 4477 | |
d5a2f2f1 HMH |
4478 | if (!tp) |
4479 | return; | |
4480 | ||
4481 | memset(tp, 0, sizeof(*tp)); | |
4482 | ||
4483 | if (dmi_name_in_vendors("IBM")) | |
4484 | tp->vendor = PCI_VENDOR_ID_IBM; | |
4485 | else if (dmi_name_in_vendors("LENOVO")) | |
4486 | tp->vendor = PCI_VENDOR_ID_LENOVO; | |
4487 | else | |
4488 | return; | |
4489 | ||
4490 | tp->bios_version_str = kstrdup(dmi_get_system_info(DMI_BIOS_VERSION), | |
4491 | GFP_KERNEL); | |
4492 | if (!tp->bios_version_str) | |
4493 | return; | |
4494 | tp->bios_model = tp->bios_version_str[0] | |
4495 | | (tp->bios_version_str[1] << 8); | |
4496 | ||
56b6aeb0 HMH |
4497 | /* |
4498 | * ThinkPad T23 or newer, A31 or newer, R50e or newer, | |
4499 | * X32 or newer, all Z series; Some models must have an | |
4500 | * up-to-date BIOS or they will not be detected. | |
4501 | * | |
4502 | * See http://thinkwiki.org/wiki/List_of_DMI_IDs | |
4503 | */ | |
4504 | while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) { | |
4505 | if (sscanf(dev->name, | |
4506 | "IBM ThinkPad Embedded Controller -[%17c", | |
4507 | ec_fw_string) == 1) { | |
4508 | ec_fw_string[sizeof(ec_fw_string) - 1] = 0; | |
4509 | ec_fw_string[strcspn(ec_fw_string, " ]")] = 0; | |
d5a2f2f1 HMH |
4510 | |
4511 | tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL); | |
4512 | tp->ec_model = ec_fw_string[0] | |
4513 | | (ec_fw_string[1] << 8); | |
4514 | break; | |
78f81cc4 | 4515 | } |
1da177e4 | 4516 | } |
d5a2f2f1 HMH |
4517 | |
4518 | tp->model_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_VERSION), | |
4519 | GFP_KERNEL); | |
4520 | if (strnicmp(tp->model_str, "ThinkPad", 8) != 0) { | |
4521 | kfree(tp->model_str); | |
4522 | tp->model_str = NULL; | |
4523 | } | |
1da177e4 LT |
4524 | } |
4525 | ||
5fba344c HMH |
4526 | static int __init probe_for_thinkpad(void) |
4527 | { | |
4528 | int is_thinkpad; | |
4529 | ||
4530 | if (acpi_disabled) | |
4531 | return -ENODEV; | |
4532 | ||
4533 | /* | |
4534 | * Non-ancient models have better DMI tagging, but very old models | |
4535 | * don't. | |
4536 | */ | |
d5a2f2f1 | 4537 | is_thinkpad = (thinkpad_id.model_str != NULL); |
5fba344c HMH |
4538 | |
4539 | /* ec is required because many other handles are relative to it */ | |
8d376cd6 | 4540 | IBM_ACPIHANDLE_INIT(ec); |
5fba344c HMH |
4541 | if (!ec_handle) { |
4542 | if (is_thinkpad) | |
4543 | printk(IBM_ERR | |
4544 | "Not yet supported ThinkPad detected!\n"); | |
4545 | return -ENODEV; | |
4546 | } | |
4547 | ||
0dcef77c HMH |
4548 | /* |
4549 | * Risks a regression on very old machines, but reduces potential | |
4550 | * false positives a damn great deal | |
4551 | */ | |
4552 | if (!is_thinkpad) | |
d5a2f2f1 | 4553 | is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM); |
0dcef77c HMH |
4554 | |
4555 | if (!is_thinkpad && !force_load) | |
4556 | return -ENODEV; | |
4557 | ||
5fba344c HMH |
4558 | return 0; |
4559 | } | |
4560 | ||
4561 | ||
56b6aeb0 | 4562 | /* Module init, exit, parameters */ |
1da177e4 | 4563 | |
a5763f22 HMH |
4564 | static struct ibm_init_struct ibms_init[] __initdata = { |
4565 | { | |
4566 | .init = thinkpad_acpi_driver_init, | |
4567 | .data = &thinkpad_acpi_driver_data, | |
4568 | }, | |
4569 | { | |
4570 | .init = hotkey_init, | |
4571 | .data = &hotkey_driver_data, | |
4572 | }, | |
4573 | { | |
4574 | .init = bluetooth_init, | |
4575 | .data = &bluetooth_driver_data, | |
4576 | }, | |
4577 | { | |
4578 | .init = wan_init, | |
4579 | .data = &wan_driver_data, | |
4580 | }, | |
4581 | { | |
4582 | .init = video_init, | |
4583 | .data = &video_driver_data, | |
4584 | }, | |
4585 | { | |
4586 | .init = light_init, | |
4587 | .data = &light_driver_data, | |
4588 | }, | |
4589 | #ifdef CONFIG_THINKPAD_ACPI_DOCK | |
4590 | { | |
4591 | .init = dock_init, | |
4592 | .data = &dock_driver_data[0], | |
4593 | }, | |
4594 | { | |
d94a7f16 | 4595 | .init = dock_init2, |
a5763f22 HMH |
4596 | .data = &dock_driver_data[1], |
4597 | }, | |
4598 | #endif | |
4599 | #ifdef CONFIG_THINKPAD_ACPI_BAY | |
4600 | { | |
4601 | .init = bay_init, | |
4602 | .data = &bay_driver_data, | |
4603 | }, | |
4604 | #endif | |
4605 | { | |
4606 | .init = cmos_init, | |
4607 | .data = &cmos_driver_data, | |
4608 | }, | |
4609 | { | |
4610 | .init = led_init, | |
4611 | .data = &led_driver_data, | |
4612 | }, | |
4613 | { | |
4614 | .init = beep_init, | |
4615 | .data = &beep_driver_data, | |
4616 | }, | |
4617 | { | |
4618 | .init = thermal_init, | |
4619 | .data = &thermal_driver_data, | |
4620 | }, | |
4621 | { | |
4622 | .data = &ecdump_driver_data, | |
4623 | }, | |
4624 | { | |
4625 | .init = brightness_init, | |
4626 | .data = &brightness_driver_data, | |
4627 | }, | |
4628 | { | |
4629 | .data = &volume_driver_data, | |
4630 | }, | |
4631 | { | |
4632 | .init = fan_init, | |
4633 | .data = &fan_driver_data, | |
4634 | }, | |
4635 | }; | |
4636 | ||
3945ac36 | 4637 | static int __init set_ibm_param(const char *val, struct kernel_param *kp) |
1da177e4 LT |
4638 | { |
4639 | unsigned int i; | |
a5763f22 | 4640 | struct ibm_struct *ibm; |
1da177e4 | 4641 | |
a5763f22 HMH |
4642 | for (i = 0; i < ARRAY_SIZE(ibms_init); i++) { |
4643 | ibm = ibms_init[i].data; | |
4644 | BUG_ON(ibm == NULL); | |
4645 | ||
4646 | if (strcmp(ibm->name, kp->name) == 0 && ibm->write) { | |
4647 | if (strlen(val) > sizeof(ibms_init[i].param) - 2) | |
78f81cc4 | 4648 | return -ENOSPC; |
a5763f22 HMH |
4649 | strcpy(ibms_init[i].param, val); |
4650 | strcat(ibms_init[i].param, ","); | |
78f81cc4 BD |
4651 | return 0; |
4652 | } | |
a5763f22 | 4653 | } |
1da177e4 | 4654 | |
1da177e4 LT |
4655 | return -EINVAL; |
4656 | } | |
4657 | ||
56b6aeb0 HMH |
4658 | static int experimental; |
4659 | module_param(experimental, int, 0); | |
4660 | ||
132ce091 HMH |
4661 | static u32 dbg_level; |
4662 | module_param_named(debug, dbg_level, uint, 0); | |
4663 | ||
0dcef77c | 4664 | static int force_load; |
86cc9445 | 4665 | module_param(force_load, bool, 0); |
0dcef77c | 4666 | |
ecf2a80a | 4667 | static int fan_control_allowed; |
86cc9445 | 4668 | module_param_named(fan_control, fan_control_allowed, bool, 0); |
ecf2a80a | 4669 | |
24d3b774 HMH |
4670 | static int brightness_mode; |
4671 | module_param_named(brightness_mode, brightness_mode, int, 0); | |
4672 | ||
ff80f137 HMH |
4673 | static unsigned int hotkey_report_mode; |
4674 | module_param(hotkey_report_mode, uint, 0); | |
4675 | ||
1da177e4 LT |
4676 | #define IBM_PARAM(feature) \ |
4677 | module_param_call(feature, set_ibm_param, NULL, NULL, 0) | |
4678 | ||
78f81cc4 BD |
4679 | IBM_PARAM(hotkey); |
4680 | IBM_PARAM(bluetooth); | |
4681 | IBM_PARAM(video); | |
4682 | IBM_PARAM(light); | |
85998248 | 4683 | #ifdef CONFIG_THINKPAD_ACPI_DOCK |
78f81cc4 | 4684 | IBM_PARAM(dock); |
63e5f248 | 4685 | #endif |
85998248 | 4686 | #ifdef CONFIG_THINKPAD_ACPI_BAY |
78f81cc4 | 4687 | IBM_PARAM(bay); |
85998248 | 4688 | #endif /* CONFIG_THINKPAD_ACPI_BAY */ |
78f81cc4 BD |
4689 | IBM_PARAM(cmos); |
4690 | IBM_PARAM(led); | |
4691 | IBM_PARAM(beep); | |
4692 | IBM_PARAM(ecdump); | |
4693 | IBM_PARAM(brightness); | |
4694 | IBM_PARAM(volume); | |
4695 | IBM_PARAM(fan); | |
4696 | ||
1def7115 | 4697 | static int __init thinkpad_acpi_module_init(void) |
1da177e4 LT |
4698 | { |
4699 | int ret, i; | |
4700 | ||
8fef502e HMH |
4701 | tpacpi_lifecycle = TPACPI_LIFE_INIT; |
4702 | ||
ff80f137 HMH |
4703 | /* Parameter checking */ |
4704 | if (hotkey_report_mode > 2) | |
4705 | return -EINVAL; | |
4706 | ||
54ae1501 | 4707 | /* Driver-level probe */ |
d5a2f2f1 HMH |
4708 | |
4709 | get_thinkpad_model_data(&thinkpad_id); | |
5fba344c | 4710 | ret = probe_for_thinkpad(); |
d5a2f2f1 HMH |
4711 | if (ret) { |
4712 | thinkpad_acpi_module_exit(); | |
5fba344c | 4713 | return ret; |
d5a2f2f1 | 4714 | } |
1da177e4 | 4715 | |
54ae1501 | 4716 | /* Driver initialization */ |
d5a2f2f1 | 4717 | |
8d376cd6 HMH |
4718 | IBM_ACPIHANDLE_INIT(ecrd); |
4719 | IBM_ACPIHANDLE_INIT(ecwr); | |
1da177e4 | 4720 | |
8d376cd6 | 4721 | proc_dir = proc_mkdir(IBM_PROC_DIR, acpi_root_dir); |
1da177e4 | 4722 | if (!proc_dir) { |
8d376cd6 | 4723 | printk(IBM_ERR "unable to create proc dir " IBM_PROC_DIR); |
1def7115 | 4724 | thinkpad_acpi_module_exit(); |
1da177e4 LT |
4725 | return -ENODEV; |
4726 | } | |
4727 | proc_dir->owner = THIS_MODULE; | |
78f81cc4 | 4728 | |
54ae1501 HMH |
4729 | ret = platform_driver_register(&tpacpi_pdriver); |
4730 | if (ret) { | |
4731 | printk(IBM_ERR "unable to register platform driver\n"); | |
4732 | thinkpad_acpi_module_exit(); | |
4733 | return ret; | |
4734 | } | |
ac36393d HMH |
4735 | tp_features.platform_drv_registered = 1; |
4736 | ||
176750d6 HMH |
4737 | ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver); |
4738 | if (ret) { | |
4739 | printk(IBM_ERR "unable to create sysfs driver attributes\n"); | |
4740 | thinkpad_acpi_module_exit(); | |
4741 | return ret; | |
4742 | } | |
ac36393d | 4743 | tp_features.platform_drv_attrs_registered = 1; |
176750d6 | 4744 | |
54ae1501 HMH |
4745 | |
4746 | /* Device initialization */ | |
4747 | tpacpi_pdev = platform_device_register_simple(IBM_DRVR_NAME, -1, | |
4748 | NULL, 0); | |
4749 | if (IS_ERR(tpacpi_pdev)) { | |
4750 | ret = PTR_ERR(tpacpi_pdev); | |
4751 | tpacpi_pdev = NULL; | |
4752 | printk(IBM_ERR "unable to register platform device\n"); | |
4753 | thinkpad_acpi_module_exit(); | |
4754 | return ret; | |
4755 | } | |
4756 | tpacpi_hwmon = hwmon_device_register(&tpacpi_pdev->dev); | |
4757 | if (IS_ERR(tpacpi_hwmon)) { | |
4758 | ret = PTR_ERR(tpacpi_hwmon); | |
4759 | tpacpi_hwmon = NULL; | |
4760 | printk(IBM_ERR "unable to register hwmon device\n"); | |
4761 | thinkpad_acpi_module_exit(); | |
4762 | return ret; | |
4763 | } | |
8523ed6f | 4764 | mutex_init(&tpacpi_inputdev_send_mutex); |
7f5d1cd6 HMH |
4765 | tpacpi_inputdev = input_allocate_device(); |
4766 | if (!tpacpi_inputdev) { | |
4767 | printk(IBM_ERR "unable to allocate input device\n"); | |
4768 | thinkpad_acpi_module_exit(); | |
4769 | return -ENOMEM; | |
4770 | } else { | |
4771 | /* Prepare input device, but don't register */ | |
4772 | tpacpi_inputdev->name = "ThinkPad Extra Buttons"; | |
4773 | tpacpi_inputdev->phys = IBM_DRVR_NAME "/input0"; | |
4774 | tpacpi_inputdev->id.bustype = BUS_HOST; | |
edf0e0e5 HMH |
4775 | tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ? |
4776 | thinkpad_id.vendor : | |
4777 | PCI_VENDOR_ID_IBM; | |
7f5d1cd6 HMH |
4778 | tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT; |
4779 | tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION; | |
4780 | } | |
a5763f22 HMH |
4781 | for (i = 0; i < ARRAY_SIZE(ibms_init); i++) { |
4782 | ret = ibm_init(&ibms_init[i]); | |
4783 | if (ret >= 0 && *ibms_init[i].param) | |
4784 | ret = ibms_init[i].data->write(ibms_init[i].param); | |
1da177e4 | 4785 | if (ret < 0) { |
1def7115 | 4786 | thinkpad_acpi_module_exit(); |
1da177e4 LT |
4787 | return ret; |
4788 | } | |
4789 | } | |
7f5d1cd6 HMH |
4790 | ret = input_register_device(tpacpi_inputdev); |
4791 | if (ret < 0) { | |
4792 | printk(IBM_ERR "unable to register input device\n"); | |
4793 | thinkpad_acpi_module_exit(); | |
4794 | return ret; | |
4795 | } else { | |
4796 | tp_features.input_device_registered = 1; | |
4797 | } | |
1da177e4 | 4798 | |
8fef502e | 4799 | tpacpi_lifecycle = TPACPI_LIFE_RUNNING; |
1da177e4 LT |
4800 | return 0; |
4801 | } | |
4802 | ||
1def7115 | 4803 | static void thinkpad_acpi_module_exit(void) |
56b6aeb0 | 4804 | { |
a5763f22 HMH |
4805 | struct ibm_struct *ibm, *itmp; |
4806 | ||
8fef502e HMH |
4807 | tpacpi_lifecycle = TPACPI_LIFE_EXITING; |
4808 | ||
a5763f22 HMH |
4809 | list_for_each_entry_safe_reverse(ibm, itmp, |
4810 | &tpacpi_all_drivers, | |
4811 | all_drivers) { | |
4812 | ibm_exit(ibm); | |
4813 | } | |
56b6aeb0 | 4814 | |
a5763f22 | 4815 | dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n"); |
56b6aeb0 | 4816 | |
7f5d1cd6 HMH |
4817 | if (tpacpi_inputdev) { |
4818 | if (tp_features.input_device_registered) | |
4819 | input_unregister_device(tpacpi_inputdev); | |
4820 | else | |
4821 | input_free_device(tpacpi_inputdev); | |
4822 | } | |
4823 | ||
54ae1501 HMH |
4824 | if (tpacpi_hwmon) |
4825 | hwmon_device_unregister(tpacpi_hwmon); | |
4826 | ||
4827 | if (tpacpi_pdev) | |
4828 | platform_device_unregister(tpacpi_pdev); | |
4829 | ||
ac36393d HMH |
4830 | if (tp_features.platform_drv_attrs_registered) |
4831 | tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver); | |
4832 | ||
4833 | if (tp_features.platform_drv_registered) | |
4834 | platform_driver_unregister(&tpacpi_pdriver); | |
54ae1501 | 4835 | |
56b6aeb0 | 4836 | if (proc_dir) |
8d376cd6 | 4837 | remove_proc_entry(IBM_PROC_DIR, acpi_root_dir); |
56b6aeb0 | 4838 | |
d5a2f2f1 HMH |
4839 | kfree(thinkpad_id.bios_version_str); |
4840 | kfree(thinkpad_id.ec_version_str); | |
4841 | kfree(thinkpad_id.model_str); | |
56b6aeb0 HMH |
4842 | } |
4843 | ||
1def7115 HMH |
4844 | module_init(thinkpad_acpi_module_init); |
4845 | module_exit(thinkpad_acpi_module_exit); |