ACPI: thinkpad-acpi: spring cleanup part 3
[deliverable/linux.git] / drivers / misc / thinkpad_acpi.c
CommitLineData
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
50efd831 24#define IBM_VERSION "0.18"
01e88f25 25#define TPACPI_SYSFS_VERSION 0x020101
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
0c78039f
HMH
85#include <linux/kernel.h>
86#include <linux/module.h>
87#include <linux/init.h>
88#include <linux/types.h>
89#include <linux/string.h>
90#include <linux/list.h>
91#include <linux/mutex.h>
92#include <linux/kthread.h>
93#include <linux/freezer.h>
94#include <linux/delay.h>
95
96#include <linux/nvram.h>
97#include <linux/proc_fs.h>
98#include <linux/sysfs.h>
99#include <linux/backlight.h>
100#include <linux/fb.h>
101#include <linux/platform_device.h>
102#include <linux/hwmon.h>
103#include <linux/hwmon-sysfs.h>
104#include <linux/input.h>
105#include <asm/uaccess.h>
106
107#include <linux/dmi.h>
108#include <linux/jiffies.h>
109#include <linux/workqueue.h>
110
111#include <acpi/acpi_drivers.h>
112#include <acpi/acnamesp.h>
113
114#include <linux/pci_ids.h>
115
0c78039f
HMH
116
117/* ThinkPad CMOS commands */
118#define TP_CMOS_VOLUME_DOWN 0
119#define TP_CMOS_VOLUME_UP 1
120#define TP_CMOS_VOLUME_MUTE 2
121#define TP_CMOS_BRIGHTNESS_UP 4
122#define TP_CMOS_BRIGHTNESS_DOWN 5
123
124/* NVRAM Addresses */
125enum tp_nvram_addr {
126 TP_NVRAM_ADDR_HK2 = 0x57,
127 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
128 TP_NVRAM_ADDR_VIDEO = 0x59,
129 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
130 TP_NVRAM_ADDR_MIXER = 0x60,
131};
132
133/* NVRAM bit masks */
134enum {
135 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
136 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
137 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
138 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
139 TP_NVRAM_MASK_THINKLIGHT = 0x10,
140 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
141 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
142 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
143 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
144 TP_NVRAM_MASK_MUTE = 0x40,
145 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
146 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
147 TP_NVRAM_POS_LEVEL_VOLUME = 0,
148};
149
b21a15f6
HMH
150/* ACPI HIDs */
151#define IBM_HKEY_HID "IBM0068"
152
153/* Input IDs */
154#define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
155#define TPACPI_HKEY_INPUT_VERSION 0x4101
156
157
158/****************************************************************************
159 * Main driver
160 */
161
162/* Module */
163#define IBM_NAME "thinkpad"
164#define IBM_DESC "ThinkPad ACPI Extras"
165#define IBM_FILE IBM_NAME "_acpi"
166#define IBM_URL "http://ibm-acpi.sf.net/"
167#define IBM_MAIL "ibm-acpi-devel@lists.sourceforge.net"
168
169#define IBM_PROC_DIR "ibm"
170#define IBM_ACPI_EVENT_PREFIX "ibm"
171#define IBM_DRVR_NAME IBM_FILE
172#define IBM_HWMON_DRVR_NAME IBM_NAME "_hwmon"
173
174#define IBM_MAX_ACPI_ARGS 3
175
176MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
177MODULE_DESCRIPTION(IBM_DESC);
178MODULE_VERSION(IBM_VERSION);
179MODULE_LICENSE("GPL");
180
181/* Please remove this in year 2009 */
182MODULE_ALIAS("ibm_acpi");
183
184/*
185 * DMI matching for module autoloading
186 *
187 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
188 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
189 *
190 * Only models listed in thinkwiki will be supported, so add yours
191 * if it is not there yet.
192 */
193#define IBM_BIOS_MODULE_ALIAS(__type) \
194 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW")
195
196/* Non-ancient thinkpads */
197MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
198MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
199
200/* Ancient thinkpad BIOSes have to be identified by
201 * BIOS type or model number, and there are far less
202 * BIOS types than model numbers... */
203IBM_BIOS_MODULE_ALIAS("I[B,D,H,I,M,N,O,T,W,V,Y,Z]");
204IBM_BIOS_MODULE_ALIAS("1[0,3,6,8,A-G,I,K,M-P,S,T]");
205IBM_BIOS_MODULE_ALIAS("K[U,X-Z]");
0c78039f
HMH
206
207/* Debugging */
b21a15f6
HMH
208#define IBM_LOG IBM_FILE ": "
209#define IBM_ERR KERN_ERR IBM_LOG
210#define IBM_NOTICE KERN_NOTICE IBM_LOG
211#define IBM_INFO KERN_INFO IBM_LOG
212#define IBM_DEBUG KERN_DEBUG IBM_LOG
213
0c78039f
HMH
214#define TPACPI_DBG_ALL 0xffff
215#define TPACPI_DBG_ALL 0xffff
216#define TPACPI_DBG_INIT 0x0001
217#define TPACPI_DBG_EXIT 0x0002
218#define dbg_printk(a_dbg_level, format, arg...) \
219 do { if (dbg_level & a_dbg_level) \
220 printk(IBM_DEBUG "%s: " format, __func__ , ## arg); } while (0)
221#ifdef CONFIG_THINKPAD_ACPI_DEBUG
222#define vdbg_printk(a_dbg_level, format, arg...) \
223 dbg_printk(a_dbg_level, format, ## arg)
224static const char *str_supported(int is_supported);
225#else
226#define vdbg_printk(a_dbg_level, format, arg...)
227#endif
228
b21a15f6
HMH
229#define onoff(status,bit) ((status) & (1 << (bit)) ? "on" : "off")
230#define enabled(status,bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
231#define strlencmp(a,b) (strncmp((a), (b), strlen(b)))
0c78039f 232
0c78039f
HMH
233
234/****************************************************************************
b21a15f6 235 * Driver-wide structs and misc. variables
0c78039f
HMH
236 */
237
238struct ibm_struct;
239
240struct tp_acpi_drv_struct {
241 const struct acpi_device_id *hid;
242 struct acpi_driver *driver;
243
244 void (*notify) (struct ibm_struct *, u32);
245 acpi_handle *handle;
246 u32 type;
247 struct acpi_device *device;
248};
249
250struct ibm_struct {
251 char *name;
252
253 int (*read) (char *);
254 int (*write) (char *);
255 void (*exit) (void);
256 void (*resume) (void);
257
258 struct list_head all_drivers;
259
260 struct tp_acpi_drv_struct *acpi;
261
262 struct {
263 u8 acpi_driver_registered:1;
264 u8 acpi_notify_installed:1;
265 u8 proc_created:1;
266 u8 init_called:1;
267 u8 experimental:1;
268 } flags;
269};
270
271struct ibm_init_struct {
272 char param[32];
273
274 int (*init) (struct ibm_init_struct *);
275 struct ibm_struct *data;
276};
277
278static struct {
279#ifdef CONFIG_THINKPAD_ACPI_BAY
280 u32 bay_status:1;
281 u32 bay_eject:1;
282 u32 bay_status2:1;
283 u32 bay_eject2:1;
284#endif
285 u32 bluetooth:1;
286 u32 hotkey:1;
287 u32 hotkey_mask:1;
288 u32 hotkey_wlsw:1;
289 u32 light:1;
290 u32 light_status:1;
291 u32 bright_16levels:1;
292 u32 wan:1;
293 u32 fan_ctrl_status_undef:1;
294 u32 input_device_registered:1;
295 u32 platform_drv_registered:1;
296 u32 platform_drv_attrs_registered:1;
297 u32 sensors_pdrv_registered:1;
298 u32 sensors_pdrv_attrs_registered:1;
299 u32 sensors_pdev_attrs_registered:1;
300 u32 hotkey_poll_active:1;
301} tp_features;
302
303struct thinkpad_id_data {
304 unsigned int vendor; /* ThinkPad vendor:
305 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
306
307 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
308 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
309
310 u16 bios_model; /* Big Endian, TP-1Y = 0x5931, 0 = unknown */
311 u16 ec_model;
312
313 char *model_str;
314};
0c78039f
HMH
315static struct thinkpad_id_data thinkpad_id;
316
1da177e4
LT
317#define __unused __attribute__ ((unused))
318
8fef502e
HMH
319static enum {
320 TPACPI_LIFE_INIT = 0,
321 TPACPI_LIFE_RUNNING,
322 TPACPI_LIFE_EXITING,
323} tpacpi_lifecycle;
324
b21a15f6
HMH
325static int experimental;
326static u32 dbg_level;
327
56b6aeb0
HMH
328/****************************************************************************
329 ****************************************************************************
330 *
331 * ACPI Helpers and device model
332 *
333 ****************************************************************************
334 ****************************************************************************/
335
336/*************************************************************************
337 * ACPI basic handles
338 */
1da177e4 339
94954cc6 340static acpi_handle root_handle;
1da177e4
LT
341
342#define IBM_HANDLE(object, parent, paths...) \
343 static acpi_handle object##_handle; \
344 static acpi_handle *object##_parent = &parent##_handle; \
78f81cc4 345 static char *object##_path; \
1da177e4
LT
346 static char *object##_paths[] = { paths }
347
78f81cc4
BD
348IBM_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
349 "\\_SB.PCI.ISA.EC", /* 570 */
350 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
351 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
352 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
353 "\\_SB.PCI0.ICH3.EC0", /* R31 */
354 "\\_SB.PCI0.LPC.EC", /* all others */
56b6aeb0 355 );
78f81cc4 356
56b6aeb0
HMH
357IBM_HANDLE(ecrd, ec, "ECRD"); /* 570 */
358IBM_HANDLE(ecwr, ec, "ECWR"); /* 570 */
78f81cc4 359
78f81cc4
BD
360IBM_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, T4x, X31, X40 */
361 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
362 "\\CMS", /* R40, R40e */
56b6aeb0 363 ); /* all others */
78f81cc4
BD
364
365IBM_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
366 "^HKEY", /* R30, R31 */
367 "HKEY", /* all others */
56b6aeb0 368 ); /* 570 */
78f81cc4 369
78f81cc4 370
56b6aeb0
HMH
371/*************************************************************************
372 * ACPI helpers
a8b7a662
HMH
373 */
374
1da177e4
LT
375static int acpi_evalf(acpi_handle handle,
376 void *res, char *method, char *fmt, ...)
377{
378 char *fmt0 = fmt;
78f81cc4
BD
379 struct acpi_object_list params;
380 union acpi_object in_objs[IBM_MAX_ACPI_ARGS];
381 struct acpi_buffer result, *resultp;
382 union acpi_object out_obj;
383 acpi_status status;
384 va_list ap;
385 char res_type;
386 int success;
387 int quiet;
1da177e4
LT
388
389 if (!*fmt) {
390 printk(IBM_ERR "acpi_evalf() called with empty format\n");
391 return 0;
392 }
393
394 if (*fmt == 'q') {
395 quiet = 1;
396 fmt++;
397 } else
398 quiet = 0;
399
400 res_type = *(fmt++);
401
402 params.count = 0;
403 params.pointer = &in_objs[0];
404
405 va_start(ap, fmt);
406 while (*fmt) {
407 char c = *(fmt++);
408 switch (c) {
409 case 'd': /* int */
410 in_objs[params.count].integer.value = va_arg(ap, int);
411 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
412 break;
78f81cc4 413 /* add more types as needed */
1da177e4
LT
414 default:
415 printk(IBM_ERR "acpi_evalf() called "
416 "with invalid format character '%c'\n", c);
417 return 0;
418 }
419 }
420 va_end(ap);
421
78f81cc4
BD
422 if (res_type != 'v') {
423 result.length = sizeof(out_obj);
424 result.pointer = &out_obj;
425 resultp = &result;
426 } else
427 resultp = NULL;
1da177e4 428
78f81cc4 429 status = acpi_evaluate_object(handle, method, &params, resultp);
1da177e4
LT
430
431 switch (res_type) {
78f81cc4 432 case 'd': /* int */
1da177e4
LT
433 if (res)
434 *(int *)res = out_obj.integer.value;
435 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
436 break;
78f81cc4 437 case 'v': /* void */
1da177e4
LT
438 success = status == AE_OK;
439 break;
78f81cc4 440 /* add more types as needed */
1da177e4
LT
441 default:
442 printk(IBM_ERR "acpi_evalf() called "
443 "with invalid format character '%c'\n", res_type);
444 return 0;
445 }
446
56b6aeb0
HMH
447 if (!success && !quiet)
448 printk(IBM_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
449 method, fmt0, status);
450
451 return success;
452}
453
454static void __unused acpi_print_int(acpi_handle handle, char *method)
455{
456 int i;
457
458 if (acpi_evalf(handle, &i, method, "d"))
459 printk(IBM_INFO "%s = 0x%x\n", method, i);
460 else
461 printk(IBM_ERR "error calling %s\n", method);
462}
463
464static int acpi_ec_read(int i, u8 * p)
465{
466 int v;
467
468 if (ecrd_handle) {
469 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
470 return 0;
471 *p = v;
472 } else {
473 if (ec_read(i, p) < 0)
474 return 0;
475 }
476
477 return 1;
478}
479
480static int acpi_ec_write(int i, u8 v)
481{
482 if (ecwr_handle) {
483 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
484 return 0;
485 } else {
486 if (ec_write(i, v) < 0)
487 return 0;
488 }
489
490 return 1;
491}
492
493static int _sta(acpi_handle handle)
494{
495 int status;
496
497 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
498 status = 0;
499
500 return status;
501}
502
c9bea99c
HMH
503static int issue_thinkpad_cmos_command(int cmos_cmd)
504{
505 if (!cmos_handle)
506 return -ENXIO;
507
508 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
509 return -EIO;
510
511 return 0;
512}
513
56b6aeb0
HMH
514/*************************************************************************
515 * ACPI device model
516 */
517
f74a27d4
HMH
518#define IBM_ACPIHANDLE_INIT(object) \
519 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
520 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
521
8d376cd6 522static void drv_acpi_handle_init(char *name,
5fba344c
HMH
523 acpi_handle *handle, acpi_handle parent,
524 char **paths, int num_paths, char **path)
56b6aeb0
HMH
525{
526 int i;
527 acpi_status status;
528
5ae930e6
HMH
529 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
530 name);
531
56b6aeb0
HMH
532 for (i = 0; i < num_paths; i++) {
533 status = acpi_get_handle(parent, paths[i], handle);
534 if (ACPI_SUCCESS(status)) {
535 *path = paths[i];
5ae930e6
HMH
536 dbg_printk(TPACPI_DBG_INIT,
537 "Found ACPI handle %s for %s\n",
538 *path, name);
56b6aeb0
HMH
539 return;
540 }
541 }
542
5ae930e6
HMH
543 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
544 name);
56b6aeb0
HMH
545 *handle = NULL;
546}
547
8d376cd6 548static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
56b6aeb0
HMH
549{
550 struct ibm_struct *ibm = data;
551
8fef502e
HMH
552 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
553 return;
554
8d376cd6 555 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
56b6aeb0
HMH
556 return;
557
8d376cd6 558 ibm->acpi->notify(ibm, event);
56b6aeb0
HMH
559}
560
8d376cd6 561static int __init setup_acpi_notify(struct ibm_struct *ibm)
56b6aeb0
HMH
562{
563 acpi_status status;
5ae930e6 564 int rc;
56b6aeb0 565
8d376cd6
HMH
566 BUG_ON(!ibm->acpi);
567
568 if (!*ibm->acpi->handle)
56b6aeb0
HMH
569 return 0;
570
5ae930e6 571 vdbg_printk(TPACPI_DBG_INIT,
fe08bc4b
HMH
572 "setting up ACPI notify for %s\n", ibm->name);
573
5ae930e6
HMH
574 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
575 if (rc < 0) {
576 printk(IBM_ERR "acpi_bus_get_device(%s) failed: %d\n",
577 ibm->name, rc);
56b6aeb0
HMH
578 return -ENODEV;
579 }
580
8d376cd6
HMH
581 acpi_driver_data(ibm->acpi->device) = ibm;
582 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
643f12db
HMH
583 IBM_ACPI_EVENT_PREFIX,
584 ibm->name);
56b6aeb0 585
8d376cd6
HMH
586 status = acpi_install_notify_handler(*ibm->acpi->handle,
587 ibm->acpi->type, dispatch_acpi_notify, ibm);
56b6aeb0
HMH
588 if (ACPI_FAILURE(status)) {
589 if (status == AE_ALREADY_EXISTS) {
590 printk(IBM_NOTICE "another device driver is already handling %s events\n",
591 ibm->name);
592 } else {
593 printk(IBM_ERR "acpi_install_notify_handler(%s) failed: %d\n",
594 ibm->name, status);
595 }
596 return -ENODEV;
597 }
8d376cd6 598 ibm->flags.acpi_notify_installed = 1;
56b6aeb0
HMH
599 return 0;
600}
601
8d376cd6 602static int __init tpacpi_device_add(struct acpi_device *device)
56b6aeb0
HMH
603{
604 return 0;
605}
606
6700121b 607static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
56b6aeb0 608{
5ae930e6 609 int rc;
56b6aeb0 610
fe08bc4b
HMH
611 dbg_printk(TPACPI_DBG_INIT,
612 "registering %s as an ACPI driver\n", ibm->name);
613
8d376cd6
HMH
614 BUG_ON(!ibm->acpi);
615
616 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
617 if (!ibm->acpi->driver) {
5fba344c
HMH
618 printk(IBM_ERR "kzalloc(ibm->driver) failed\n");
619 return -ENOMEM;
56b6aeb0
HMH
620 }
621
8d376cd6
HMH
622 sprintf(ibm->acpi->driver->name, "%s_%s", IBM_NAME, ibm->name);
623 ibm->acpi->driver->ids = ibm->acpi->hid;
1ba90e3a 624
8d376cd6 625 ibm->acpi->driver->ops.add = &tpacpi_device_add;
56b6aeb0 626
5ae930e6
HMH
627 rc = acpi_bus_register_driver(ibm->acpi->driver);
628 if (rc < 0) {
56b6aeb0 629 printk(IBM_ERR "acpi_bus_register_driver(%s) failed: %d\n",
1ba90e3a 630 ibm->name, rc);
8d376cd6
HMH
631 kfree(ibm->acpi->driver);
632 ibm->acpi->driver = NULL;
5ae930e6 633 } else if (!rc)
8d376cd6 634 ibm->flags.acpi_driver_registered = 1;
56b6aeb0 635
5ae930e6 636 return rc;
56b6aeb0
HMH
637}
638
639
640/****************************************************************************
641 ****************************************************************************
642 *
643 * Procfs Helpers
644 *
645 ****************************************************************************
646 ****************************************************************************/
647
8d376cd6
HMH
648static int dispatch_procfs_read(char *page, char **start, off_t off,
649 int count, int *eof, void *data)
56b6aeb0
HMH
650{
651 struct ibm_struct *ibm = data;
652 int len;
653
654 if (!ibm || !ibm->read)
655 return -EINVAL;
656
657 len = ibm->read(page);
658 if (len < 0)
659 return len;
660
661 if (len <= off + count)
662 *eof = 1;
663 *start = page + off;
664 len -= off;
665 if (len > count)
666 len = count;
667 if (len < 0)
668 len = 0;
669
670 return len;
671}
672
8d376cd6
HMH
673static int dispatch_procfs_write(struct file *file,
674 const char __user * userbuf,
675 unsigned long count, void *data)
56b6aeb0
HMH
676{
677 struct ibm_struct *ibm = data;
678 char *kernbuf;
679 int ret;
680
681 if (!ibm || !ibm->write)
682 return -EINVAL;
683
684 kernbuf = kmalloc(count + 2, GFP_KERNEL);
685 if (!kernbuf)
686 return -ENOMEM;
1da177e4 687
56b6aeb0
HMH
688 if (copy_from_user(kernbuf, userbuf, count)) {
689 kfree(kernbuf);
690 return -EFAULT;
691 }
1da177e4 692
56b6aeb0
HMH
693 kernbuf[count] = 0;
694 strcat(kernbuf, ",");
695 ret = ibm->write(kernbuf);
696 if (ret == 0)
697 ret = count;
1da177e4 698
56b6aeb0
HMH
699 kfree(kernbuf);
700
701 return ret;
1da177e4
LT
702}
703
704static char *next_cmd(char **cmds)
705{
706 char *start = *cmds;
707 char *end;
708
709 while ((end = strchr(start, ',')) && end == start)
710 start = end + 1;
711
712 if (!end)
713 return NULL;
714
715 *end = 0;
716 *cmds = end + 1;
717 return start;
718}
719
56b6aeb0 720
54ae1501
HMH
721/****************************************************************************
722 ****************************************************************************
723 *
7f5d1cd6 724 * Device model: input, hwmon and platform
54ae1501
HMH
725 *
726 ****************************************************************************
727 ****************************************************************************/
728
94954cc6 729static struct platform_device *tpacpi_pdev;
7fd40029 730static struct platform_device *tpacpi_sensors_pdev;
1beeffe4 731static struct device *tpacpi_hwmon;
7f5d1cd6 732static struct input_dev *tpacpi_inputdev;
8523ed6f 733static struct mutex tpacpi_inputdev_send_mutex;
b21a15f6 734static LIST_HEAD(tpacpi_all_drivers);
e295e850
HMH
735
736static int tpacpi_resume_handler(struct platform_device *pdev)
737{
738 struct ibm_struct *ibm, *itmp;
739
740 list_for_each_entry_safe(ibm, itmp,
741 &tpacpi_all_drivers,
742 all_drivers) {
743 if (ibm->resume)
744 (ibm->resume)();
745 }
746
747 return 0;
748}
749
54ae1501
HMH
750static struct platform_driver tpacpi_pdriver = {
751 .driver = {
752 .name = IBM_DRVR_NAME,
753 .owner = THIS_MODULE,
754 },
e295e850 755 .resume = tpacpi_resume_handler,
54ae1501
HMH
756};
757
7fd40029
HMH
758static struct platform_driver tpacpi_hwmon_pdriver = {
759 .driver = {
760 .name = IBM_HWMON_DRVR_NAME,
761 .owner = THIS_MODULE,
762 },
763};
54ae1501 764
176750d6 765/*************************************************************************
b21a15f6 766 * sysfs support helpers
176750d6
HMH
767 */
768
b21a15f6
HMH
769struct attribute_set {
770 unsigned int members, max_members;
771 struct attribute_group group;
176750d6
HMH
772};
773
7252374a
HMH
774struct attribute_set_obj {
775 struct attribute_set s;
776 struct attribute *a;
777} __attribute__((packed));
778
779static struct attribute_set *create_attr_set(unsigned int max_members,
780 const char* name)
781{
782 struct attribute_set_obj *sobj;
783
784 if (max_members == 0)
785 return NULL;
786
787 /* Allocates space for implicit NULL at the end too */
788 sobj = kzalloc(sizeof(struct attribute_set_obj) +
789 max_members * sizeof(struct attribute *),
790 GFP_KERNEL);
791 if (!sobj)
792 return NULL;
793 sobj->s.max_members = max_members;
794 sobj->s.group.attrs = &sobj->a;
795 sobj->s.group.name = name;
796
797 return &sobj->s;
798}
799
f74a27d4
HMH
800#define destroy_attr_set(_set) \
801 kfree(_set);
802
7252374a
HMH
803/* not multi-threaded safe, use it in a single thread per set */
804static int add_to_attr_set(struct attribute_set* s, struct attribute *attr)
805{
806 if (!s || !attr)
807 return -EINVAL;
808
809 if (s->members >= s->max_members)
810 return -ENOMEM;
811
812 s->group.attrs[s->members] = attr;
813 s->members++;
814
815 return 0;
816}
817
818static int add_many_to_attr_set(struct attribute_set* s,
819 struct attribute **attr,
820 unsigned int count)
821{
822 int i, res;
823
824 for (i = 0; i < count; i++) {
825 res = add_to_attr_set(s, attr[i]);
826 if (res)
827 return res;
828 }
829
830 return 0;
831}
832
833static void delete_attr_set(struct attribute_set* s, struct kobject *kobj)
834{
835 sysfs_remove_group(kobj, &s->group);
836 destroy_attr_set(s);
837}
838
f74a27d4
HMH
839#define register_attr_set_with_sysfs(_attr_set, _kobj) \
840 sysfs_create_group(_kobj, &_attr_set->group)
841
7252374a
HMH
842static int parse_strtoul(const char *buf,
843 unsigned long max, unsigned long *value)
844{
845 char *endp;
846
32afbf07
HMH
847 while (*buf && isspace(*buf))
848 buf++;
7252374a
HMH
849 *value = simple_strtoul(buf, &endp, 0);
850 while (*endp && isspace(*endp))
851 endp++;
852 if (*endp || *value > max)
853 return -EINVAL;
854
855 return 0;
856}
857
56b6aeb0 858/*************************************************************************
b21a15f6 859 * thinkpad-acpi driver attributes
56b6aeb0
HMH
860 */
861
b21a15f6
HMH
862/* interface_version --------------------------------------------------- */
863static ssize_t tpacpi_driver_interface_version_show(
864 struct device_driver *drv,
865 char *buf)
1da177e4 866{
b21a15f6
HMH
867 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
868}
869
870static DRIVER_ATTR(interface_version, S_IRUGO,
871 tpacpi_driver_interface_version_show, NULL);
872
873/* debug_level --------------------------------------------------------- */
874static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
875 char *buf)
876{
877 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
878}
879
880static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
881 const char *buf, size_t count)
882{
883 unsigned long t;
884
885 if (parse_strtoul(buf, 0xffff, &t))
886 return -EINVAL;
887
888 dbg_level = t;
889
890 return count;
891}
892
893static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
894 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
895
896/* version ------------------------------------------------------------- */
897static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
898 char *buf)
899{
900 return snprintf(buf, PAGE_SIZE, "%s v%s\n", IBM_DESC, IBM_VERSION);
901}
902
903static DRIVER_ATTR(version, S_IRUGO,
904 tpacpi_driver_version_show, NULL);
905
906/* --------------------------------------------------------------------- */
907
908static struct driver_attribute* tpacpi_driver_attributes[] = {
909 &driver_attr_debug_level, &driver_attr_version,
910 &driver_attr_interface_version,
911};
912
913static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
914{
915 int i, res;
916
917 i = 0;
918 res = 0;
919 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
920 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
921 i++;
922 }
923
924 return res;
925}
926
927static void tpacpi_remove_driver_attributes(struct device_driver *drv)
928{
929 int i;
930
931 for(i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
932 driver_remove_file(drv, tpacpi_driver_attributes[i]);
933}
934
935/****************************************************************************
936 ****************************************************************************
937 *
938 * Subdrivers
939 *
940 ****************************************************************************
941 ****************************************************************************/
942
943/*************************************************************************
944 * thinkpad-acpi init subdriver
945 */
946
947static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
948{
949 printk(IBM_INFO "%s v%s\n", IBM_DESC, IBM_VERSION);
950 printk(IBM_INFO "%s\n", IBM_URL);
951
952 printk(IBM_INFO "ThinkPad BIOS %s, EC %s\n",
953 (thinkpad_id.bios_version_str) ?
954 thinkpad_id.bios_version_str : "unknown",
955 (thinkpad_id.ec_version_str) ?
956 thinkpad_id.ec_version_str : "unknown");
d5a2f2f1
HMH
957
958 if (thinkpad_id.vendor && thinkpad_id.model_str)
959 printk(IBM_INFO "%s %s\n",
960 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
961 "IBM" : ((thinkpad_id.vendor ==
962 PCI_VENDOR_ID_LENOVO) ?
963 "Lenovo" : "Unknown vendor"),
964 thinkpad_id.model_str);
3945ac36 965
1da177e4
LT
966 return 0;
967}
968
643f12db 969static int thinkpad_acpi_driver_read(char *p)
1da177e4
LT
970{
971 int len = 0;
972
973 len += sprintf(p + len, "driver:\t\t%s\n", IBM_DESC);
974 len += sprintf(p + len, "version:\t%s\n", IBM_VERSION);
975
976 return len;
977}
978
a5763f22
HMH
979static struct ibm_struct thinkpad_acpi_driver_data = {
980 .name = "driver",
981 .read = thinkpad_acpi_driver_read,
982};
983
56b6aeb0
HMH
984/*************************************************************************
985 * Hotkey subdriver
986 */
987
f74a27d4
HMH
988enum { /* hot key scan codes (derived from ACPI DSDT) */
989 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
990 TP_ACPI_HOTKEYSCAN_FNF2,
991 TP_ACPI_HOTKEYSCAN_FNF3,
992 TP_ACPI_HOTKEYSCAN_FNF4,
993 TP_ACPI_HOTKEYSCAN_FNF5,
994 TP_ACPI_HOTKEYSCAN_FNF6,
995 TP_ACPI_HOTKEYSCAN_FNF7,
996 TP_ACPI_HOTKEYSCAN_FNF8,
997 TP_ACPI_HOTKEYSCAN_FNF9,
998 TP_ACPI_HOTKEYSCAN_FNF10,
999 TP_ACPI_HOTKEYSCAN_FNF11,
1000 TP_ACPI_HOTKEYSCAN_FNF12,
1001 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1002 TP_ACPI_HOTKEYSCAN_FNINSERT,
1003 TP_ACPI_HOTKEYSCAN_FNDELETE,
1004 TP_ACPI_HOTKEYSCAN_FNHOME,
1005 TP_ACPI_HOTKEYSCAN_FNEND,
1006 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1007 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1008 TP_ACPI_HOTKEYSCAN_FNSPACE,
1009 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1010 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1011 TP_ACPI_HOTKEYSCAN_MUTE,
1012 TP_ACPI_HOTKEYSCAN_THINKPAD,
1013};
1014
01e88f25
HMH
1015enum { /* Keys available through NVRAM polling */
1016 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1017 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1018};
1019
1020enum { /* Positions of some of the keys in hotkey masks */
1021 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1022 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1023 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1024 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1025 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1026 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1027 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1028 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1029 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1030 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1031 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1032};
1033
1034enum { /* NVRAM to ACPI HKEY group map */
1035 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1036 TP_ACPI_HKEY_ZOOM_MASK |
1037 TP_ACPI_HKEY_DISPSWTCH_MASK |
1038 TP_ACPI_HKEY_HIBERNATE_MASK,
1039 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1040 TP_ACPI_HKEY_BRGHTDWN_MASK,
1041 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1042 TP_ACPI_HKEY_VOLDWN_MASK |
1043 TP_ACPI_HKEY_MUTE_MASK,
1044};
1045
1046#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1047struct tp_nvram_state {
1048 u16 thinkpad_toggle:1;
1049 u16 zoom_toggle:1;
1050 u16 display_toggle:1;
1051 u16 thinklight_toggle:1;
1052 u16 hibernate_toggle:1;
1053 u16 displayexp_toggle:1;
1054 u16 display_state:1;
1055 u16 brightness_toggle:1;
1056 u16 volume_toggle:1;
1057 u16 mute:1;
1058
1059 u8 brightness_level;
1060 u8 volume_level;
1061};
1062
1063static struct task_struct *tpacpi_hotkey_task;
1064static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1065static int hotkey_poll_freq = 10; /* Hz */
1066static struct mutex hotkey_thread_mutex;
1067static struct mutex hotkey_thread_data_mutex;
1068static unsigned int hotkey_config_change;
1069
1070#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1071
1072#define hotkey_source_mask 0U
1073
1074#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1075
f74a27d4
HMH
1076static struct mutex hotkey_mutex;
1077
78f81cc4 1078static int hotkey_orig_status;
ae92bd17 1079static u32 hotkey_orig_mask;
9b010de5 1080static u32 hotkey_all_mask;
6a38abbf 1081static u32 hotkey_reserved_mask;
b2c985e7 1082static u32 hotkey_mask;
6a38abbf 1083
b21a15f6
HMH
1084static unsigned int hotkey_report_mode;
1085
edf0e0e5 1086static u16 *hotkey_keycode_map;
78f81cc4 1087
94954cc6 1088static struct attribute_set *hotkey_dev_attributes;
a0416420 1089
01e88f25
HMH
1090#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1091#define HOTKEY_CONFIG_CRITICAL_START \
1092 mutex_lock(&hotkey_thread_data_mutex); \
1093 hotkey_config_change++;
1094#define HOTKEY_CONFIG_CRITICAL_END \
1095 mutex_unlock(&hotkey_thread_data_mutex);
1096#else
1097#define HOTKEY_CONFIG_CRITICAL_START
1098#define HOTKEY_CONFIG_CRITICAL_END
1099#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1100
74941a69
HMH
1101static int hotkey_get_wlsw(int *status)
1102{
1103 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1104 return -EIO;
1105 return 0;
1106}
1107
b2c985e7
HMH
1108/*
1109 * Call with hotkey_mutex held
1110 */
1111static int hotkey_mask_get(void)
1112{
01e88f25
HMH
1113 u32 m = 0;
1114
b2c985e7 1115 if (tp_features.hotkey_mask) {
01e88f25 1116 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
b2c985e7
HMH
1117 return -EIO;
1118 }
01e88f25 1119 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
b2c985e7
HMH
1120
1121 return 0;
1122}
1123
1124/*
1125 * Call with hotkey_mutex held
1126 */
1127static int hotkey_mask_set(u32 mask)
1128{
1129 int i;
1130 int rc = 0;
1131
1132 if (tp_features.hotkey_mask) {
01e88f25 1133 HOTKEY_CONFIG_CRITICAL_START
b2c985e7
HMH
1134 for (i = 0; i < 32; i++) {
1135 u32 m = 1 << i;
01e88f25
HMH
1136 /* enable in firmware mask only keys not in NVRAM
1137 * mode, but enable the key in the cached hotkey_mask
1138 * regardless of mode, or the key will end up
1139 * disabled by hotkey_mask_get() */
b2c985e7
HMH
1140 if (!acpi_evalf(hkey_handle,
1141 NULL, "MHKM", "vdd", i + 1,
01e88f25 1142 !!((mask & ~hotkey_source_mask) & m))) {
b2c985e7
HMH
1143 rc = -EIO;
1144 break;
1145 } else {
1146 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1147 }
1148 }
01e88f25 1149 HOTKEY_CONFIG_CRITICAL_END
b2c985e7
HMH
1150
1151 /* hotkey_mask_get must be called unconditionally below */
01e88f25
HMH
1152 if (!hotkey_mask_get() && !rc &&
1153 (hotkey_mask & ~hotkey_source_mask) !=
1154 (mask & ~hotkey_source_mask)) {
b2c985e7
HMH
1155 printk(IBM_NOTICE
1156 "requested hot key mask 0x%08x, but "
1157 "firmware forced it to 0x%08x\n",
1158 mask, hotkey_mask);
1159 }
01e88f25
HMH
1160 } else {
1161#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1162 HOTKEY_CONFIG_CRITICAL_START
1163 hotkey_mask = mask & hotkey_source_mask;
1164 HOTKEY_CONFIG_CRITICAL_END
1165 hotkey_mask_get();
1166 if (hotkey_mask != mask) {
1167 printk(IBM_NOTICE
1168 "requested hot key mask 0x%08x, "
1169 "forced to 0x%08x (NVRAM poll mask is "
1170 "0x%08x): no firmware mask support\n",
1171 mask, hotkey_mask, hotkey_source_mask);
1172 }
1173#else
1174 hotkey_mask_get();
1175 rc = -ENXIO;
1176#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
b2c985e7
HMH
1177 }
1178
1179 return rc;
1180}
1181
1182static int hotkey_status_get(int *status)
1183{
1184 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1185 return -EIO;
1186
1187 return 0;
1188}
1189
1190static int hotkey_status_set(int status)
1191{
1192 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
1193 return -EIO;
1194
1195 return 0;
1196}
1197
b7c8c200
HMH
1198static void tpacpi_input_send_radiosw(void)
1199{
1200 int wlsw;
1201
1202 mutex_lock(&tpacpi_inputdev_send_mutex);
1203
1204 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
1205 input_report_switch(tpacpi_inputdev,
1206 SW_RADIO, !!wlsw);
1207 input_sync(tpacpi_inputdev);
1208 }
1209
1210 mutex_unlock(&tpacpi_inputdev_send_mutex);
1211}
1212
1213static void tpacpi_input_send_key(unsigned int scancode)
1214{
1215 unsigned int keycode;
1216
1217 keycode = hotkey_keycode_map[scancode];
1218
1219 if (keycode != KEY_RESERVED) {
1220 mutex_lock(&tpacpi_inputdev_send_mutex);
1221
1222 input_report_key(tpacpi_inputdev, keycode, 1);
1223 if (keycode == KEY_UNKNOWN)
1224 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1225 scancode);
1226 input_sync(tpacpi_inputdev);
1227
1228 input_report_key(tpacpi_inputdev, keycode, 0);
1229 if (keycode == KEY_UNKNOWN)
1230 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1231 scancode);
1232 input_sync(tpacpi_inputdev);
1233
1234 mutex_unlock(&tpacpi_inputdev_send_mutex);
1235 }
1236}
1237
01e88f25
HMH
1238#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1239static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1240
1241static void tpacpi_hotkey_send_key(unsigned int scancode)
1242{
1243 tpacpi_input_send_key(scancode);
1244 if (hotkey_report_mode < 2) {
1245 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1246 0x80, 0x1001 + scancode);
1247 }
1248}
1249
1250static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1251{
1252 u8 d;
1253
1254 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1255 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1256 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1257 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1258 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1259 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1260 }
1261 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1262 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1263 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1264 }
1265 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1266 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1267 n->displayexp_toggle =
1268 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1269 }
1270 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1271 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1272 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1273 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1274 n->brightness_toggle =
1275 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1276 }
1277 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1278 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1279 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1280 >> TP_NVRAM_POS_LEVEL_VOLUME;
1281 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1282 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1283 }
1284}
1285
1286#define TPACPI_COMPARE_KEY(__scancode, __member) \
1287 do { if ((mask & (1 << __scancode)) && oldn->__member != newn->__member) \
1288 tpacpi_hotkey_send_key(__scancode); } while (0)
1289
1290#define TPACPI_MAY_SEND_KEY(__scancode) \
1291 do { if (mask & (1 << __scancode)) \
1292 tpacpi_hotkey_send_key(__scancode); } while (0)
1293
1294static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
1295 struct tp_nvram_state *newn,
1296 u32 mask)
1297{
1298 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1299 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1300 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1301 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1302
1303 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1304
1305 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1306
1307 /* handle volume */
1308 if (oldn->volume_toggle != newn->volume_toggle) {
1309 if (oldn->mute != newn->mute) {
1310 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1311 }
1312 if (oldn->volume_level > newn->volume_level) {
1313 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1314 } else if (oldn->volume_level < newn->volume_level) {
1315 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1316 } else if (oldn->mute == newn->mute) {
1317 /* repeated key presses that didn't change state */
1318 if (newn->mute) {
1319 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1320 } else if (newn->volume_level != 0) {
1321 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1322 } else {
1323 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1324 }
1325 }
1326 }
1327
1328 /* handle brightness */
1329 if (oldn->brightness_toggle != newn->brightness_toggle) {
1330 if (oldn->brightness_level < newn->brightness_level) {
1331 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1332 } else if (oldn->brightness_level > newn->brightness_level) {
1333 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1334 } else {
1335 /* repeated key presses that didn't change state */
1336 if (newn->brightness_level != 0) {
1337 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1338 } else {
1339 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1340 }
1341 }
1342 }
1343}
1344
1345#undef TPACPI_COMPARE_KEY
1346#undef TPACPI_MAY_SEND_KEY
1347
1348static int hotkey_kthread(void *data)
1349{
1350 struct tp_nvram_state s[2];
1351 u32 mask;
1352 unsigned int si, so;
1353 unsigned long t;
1354 unsigned int change_detector, must_reset;
1355
1356 mutex_lock(&hotkey_thread_mutex);
1357
1358 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1359 goto exit;
1360
1361 set_freezable();
1362
1363 so = 0;
1364 si = 1;
1365 t = 0;
1366
1367 /* Initial state for compares */
1368 mutex_lock(&hotkey_thread_data_mutex);
1369 change_detector = hotkey_config_change;
1370 mask = hotkey_source_mask & hotkey_mask;
1371 mutex_unlock(&hotkey_thread_data_mutex);
1372 hotkey_read_nvram(&s[so], mask);
1373
1374 while (!kthread_should_stop() && hotkey_poll_freq) {
1375 if (t == 0)
1376 t = 1000/hotkey_poll_freq;
1377 t = msleep_interruptible(t);
1378 if (unlikely(kthread_should_stop()))
1379 break;
1380 must_reset = try_to_freeze();
1381 if (t > 0 && !must_reset)
1382 continue;
1383
1384 mutex_lock(&hotkey_thread_data_mutex);
1385 if (must_reset || hotkey_config_change != change_detector) {
1386 /* forget old state on thaw or config change */
1387 si = so;
1388 t = 0;
1389 change_detector = hotkey_config_change;
1390 }
1391 mask = hotkey_source_mask & hotkey_mask;
1392 mutex_unlock(&hotkey_thread_data_mutex);
1393
1394 if (likely(mask)) {
1395 hotkey_read_nvram(&s[si], mask);
1396 if (likely(si != so)) {
1397 hotkey_compare_and_issue_event(&s[so], &s[si],
1398 mask);
1399 }
1400 }
1401
1402 so = si;
1403 si ^= 1;
1404 }
1405
1406exit:
1407 mutex_unlock(&hotkey_thread_mutex);
1408 return 0;
1409}
1410
1411static void hotkey_poll_stop_sync(void)
1412{
1413 if (tpacpi_hotkey_task) {
1414 if (frozen(tpacpi_hotkey_task) ||
1415 freezing(tpacpi_hotkey_task))
1416 thaw_process(tpacpi_hotkey_task);
1417
1418 kthread_stop(tpacpi_hotkey_task);
1419 tpacpi_hotkey_task = NULL;
1420 mutex_lock(&hotkey_thread_mutex);
1421 /* at this point, the thread did exit */
1422 mutex_unlock(&hotkey_thread_mutex);
1423 }
1424}
1425
1426/* call with hotkey_mutex held */
1427static void hotkey_poll_setup(int may_warn)
1428{
1429 if ((hotkey_source_mask & hotkey_mask) != 0 &&
1430 hotkey_poll_freq > 0 &&
1431 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1432 if (!tpacpi_hotkey_task) {
1433 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
1434 NULL, IBM_FILE "d");
1435 if (IS_ERR(tpacpi_hotkey_task)) {
1436 tpacpi_hotkey_task = NULL;
1437 printk(IBM_ERR "could not create kernel thread "
1438 "for hotkey polling\n");
1439 }
1440 }
1441 } else {
1442 hotkey_poll_stop_sync();
1443 if (may_warn &&
1444 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
1445 printk(IBM_NOTICE "hot keys 0x%08x require polling, "
1446 "which is currently disabled\n",
1447 hotkey_source_mask);
1448 }
1449 }
1450}
1451
1452static void hotkey_poll_setup_safe(int may_warn)
1453{
1454 mutex_lock(&hotkey_mutex);
1455 hotkey_poll_setup(may_warn);
1456 mutex_unlock(&hotkey_mutex);
1457}
1458
1459static int hotkey_inputdev_open(struct input_dev *dev)
1460{
1461 switch (tpacpi_lifecycle) {
1462 case TPACPI_LIFE_INIT:
1463 /*
1464 * hotkey_init will call hotkey_poll_setup_safe
1465 * at the appropriate moment
1466 */
1467 return 0;
1468 case TPACPI_LIFE_EXITING:
1469 return -EBUSY;
1470 case TPACPI_LIFE_RUNNING:
1471 hotkey_poll_setup_safe(0);
1472 return 0;
1473 }
1474
1475 /* Should only happen if tpacpi_lifecycle is corrupt */
1476 BUG();
1477 return -EBUSY;
1478}
1479
1480static void hotkey_inputdev_close(struct input_dev *dev)
1481{
1482 /* disable hotkey polling when possible */
1483 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1484 hotkey_poll_setup_safe(0);
1485}
1486#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1487
a0416420
HMH
1488/* sysfs hotkey enable ------------------------------------------------- */
1489static ssize_t hotkey_enable_show(struct device *dev,
1490 struct device_attribute *attr,
1491 char *buf)
1492{
ae92bd17 1493 int res, status;
a0416420 1494
b2c985e7 1495 res = hotkey_status_get(&status);
a0416420
HMH
1496 if (res)
1497 return res;
1498
1499 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1500}
1501
1502static ssize_t hotkey_enable_store(struct device *dev,
1503 struct device_attribute *attr,
1504 const char *buf, size_t count)
1505{
1506 unsigned long t;
b2c985e7 1507 int res;
a0416420
HMH
1508
1509 if (parse_strtoul(buf, 1, &t))
1510 return -EINVAL;
1511
b2c985e7 1512 res = hotkey_status_set(t);
a0416420
HMH
1513
1514 return (res) ? res : count;
1515}
1516
1517static struct device_attribute dev_attr_hotkey_enable =
cc4c24e1 1518 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
a0416420
HMH
1519 hotkey_enable_show, hotkey_enable_store);
1520
1521/* sysfs hotkey mask --------------------------------------------------- */
1522static ssize_t hotkey_mask_show(struct device *dev,
1523 struct device_attribute *attr,
1524 char *buf)
1525{
b2c985e7 1526 int res;
a0416420 1527
b2c985e7
HMH
1528 if (mutex_lock_interruptible(&hotkey_mutex))
1529 return -ERESTARTSYS;
1530 res = hotkey_mask_get();
1531 mutex_unlock(&hotkey_mutex);
a0416420 1532
b2c985e7
HMH
1533 return (res)?
1534 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
a0416420
HMH
1535}
1536
1537static ssize_t hotkey_mask_store(struct device *dev,
1538 struct device_attribute *attr,
1539 const char *buf, size_t count)
1540{
1541 unsigned long t;
b2c985e7 1542 int res;
a0416420 1543
ae92bd17 1544 if (parse_strtoul(buf, 0xffffffffUL, &t))
a0416420
HMH
1545 return -EINVAL;
1546
b2c985e7
HMH
1547 if (mutex_lock_interruptible(&hotkey_mutex))
1548 return -ERESTARTSYS;
1549
1550 res = hotkey_mask_set(t);
01e88f25
HMH
1551
1552#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1553 hotkey_poll_setup(1);
1554#endif
1555
b2c985e7 1556 mutex_unlock(&hotkey_mutex);
a0416420
HMH
1557
1558 return (res) ? res : count;
1559}
1560
1561static struct device_attribute dev_attr_hotkey_mask =
cc4c24e1 1562 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
a0416420
HMH
1563 hotkey_mask_show, hotkey_mask_store);
1564
1565/* sysfs hotkey bios_enabled ------------------------------------------- */
1566static ssize_t hotkey_bios_enabled_show(struct device *dev,
1567 struct device_attribute *attr,
1568 char *buf)
1569{
1570 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
1571}
1572
1573static struct device_attribute dev_attr_hotkey_bios_enabled =
cc4c24e1 1574 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
a0416420
HMH
1575
1576/* sysfs hotkey bios_mask ---------------------------------------------- */
1577static ssize_t hotkey_bios_mask_show(struct device *dev,
1578 struct device_attribute *attr,
1579 char *buf)
1580{
ae92bd17 1581 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
a0416420
HMH
1582}
1583
1584static struct device_attribute dev_attr_hotkey_bios_mask =
cc4c24e1 1585 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
a0416420 1586
9b010de5
HMH
1587/* sysfs hotkey all_mask ----------------------------------------------- */
1588static ssize_t hotkey_all_mask_show(struct device *dev,
1589 struct device_attribute *attr,
1590 char *buf)
1591{
01e88f25
HMH
1592 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1593 hotkey_all_mask | hotkey_source_mask);
9b010de5
HMH
1594}
1595
1596static struct device_attribute dev_attr_hotkey_all_mask =
1597 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
1598
1599/* sysfs hotkey recommended_mask --------------------------------------- */
1600static ssize_t hotkey_recommended_mask_show(struct device *dev,
1601 struct device_attribute *attr,
1602 char *buf)
1603{
1604 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
01e88f25
HMH
1605 (hotkey_all_mask | hotkey_source_mask)
1606 & ~hotkey_reserved_mask);
9b010de5
HMH
1607}
1608
1609static struct device_attribute dev_attr_hotkey_recommended_mask =
1610 __ATTR(hotkey_recommended_mask, S_IRUGO,
1611 hotkey_recommended_mask_show, NULL);
1612
01e88f25
HMH
1613#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1614
1615/* sysfs hotkey hotkey_source_mask ------------------------------------- */
1616static ssize_t hotkey_source_mask_show(struct device *dev,
1617 struct device_attribute *attr,
1618 char *buf)
1619{
1620 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
1621}
1622
1623static ssize_t hotkey_source_mask_store(struct device *dev,
1624 struct device_attribute *attr,
1625 const char *buf, size_t count)
1626{
1627 unsigned long t;
1628
1629 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
1630 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
1631 return -EINVAL;
1632
1633 if (mutex_lock_interruptible(&hotkey_mutex))
1634 return -ERESTARTSYS;
1635
1636 HOTKEY_CONFIG_CRITICAL_START
1637 hotkey_source_mask = t;
1638 HOTKEY_CONFIG_CRITICAL_END
1639
1640 hotkey_poll_setup(1);
1641
1642 mutex_unlock(&hotkey_mutex);
1643
1644 return count;
1645}
1646
1647static struct device_attribute dev_attr_hotkey_source_mask =
1648 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
1649 hotkey_source_mask_show, hotkey_source_mask_store);
1650
1651/* sysfs hotkey hotkey_poll_freq --------------------------------------- */
1652static ssize_t hotkey_poll_freq_show(struct device *dev,
1653 struct device_attribute *attr,
1654 char *buf)
1655{
1656 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
1657}
1658
1659static ssize_t hotkey_poll_freq_store(struct device *dev,
1660 struct device_attribute *attr,
1661 const char *buf, size_t count)
1662{
1663 unsigned long t;
1664
1665 if (parse_strtoul(buf, 25, &t))
1666 return -EINVAL;
1667
1668 if (mutex_lock_interruptible(&hotkey_mutex))
1669 return -ERESTARTSYS;
1670
1671 hotkey_poll_freq = t;
1672
1673 hotkey_poll_setup(1);
1674 mutex_unlock(&hotkey_mutex);
1675
1676 return count;
1677}
1678
1679static struct device_attribute dev_attr_hotkey_poll_freq =
1680 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
1681 hotkey_poll_freq_show, hotkey_poll_freq_store);
1682
1683#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1684
74941a69
HMH
1685/* sysfs hotkey radio_sw ----------------------------------------------- */
1686static ssize_t hotkey_radio_sw_show(struct device *dev,
1687 struct device_attribute *attr,
1688 char *buf)
1689{
1690 int res, s;
1691 res = hotkey_get_wlsw(&s);
1692 if (res < 0)
1693 return res;
1694
1695 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1696}
1697
1698static struct device_attribute dev_attr_hotkey_radio_sw =
1699 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
1700
ff80f137
HMH
1701/* sysfs hotkey report_mode -------------------------------------------- */
1702static ssize_t hotkey_report_mode_show(struct device *dev,
1703 struct device_attribute *attr,
1704 char *buf)
1705{
1706 return snprintf(buf, PAGE_SIZE, "%d\n",
1707 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
1708}
1709
1710static struct device_attribute dev_attr_hotkey_report_mode =
1711 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
1712
a0416420
HMH
1713/* --------------------------------------------------------------------- */
1714
ff80f137
HMH
1715static struct attribute *hotkey_attributes[] __initdata = {
1716 &dev_attr_hotkey_enable.attr,
01e88f25 1717 &dev_attr_hotkey_bios_enabled.attr,
ff80f137 1718 &dev_attr_hotkey_report_mode.attr,
01e88f25
HMH
1719#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1720 &dev_attr_hotkey_mask.attr,
1721 &dev_attr_hotkey_all_mask.attr,
1722 &dev_attr_hotkey_recommended_mask.attr,
1723 &dev_attr_hotkey_source_mask.attr,
1724 &dev_attr_hotkey_poll_freq.attr,
1725#endif
ff80f137
HMH
1726};
1727
1728static struct attribute *hotkey_mask_attributes[] __initdata = {
a0416420 1729 &dev_attr_hotkey_bios_mask.attr,
01e88f25
HMH
1730#ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1731 &dev_attr_hotkey_mask.attr,
9b010de5
HMH
1732 &dev_attr_hotkey_all_mask.attr,
1733 &dev_attr_hotkey_recommended_mask.attr,
01e88f25 1734#endif
a0416420
HMH
1735};
1736
a5763f22 1737static int __init hotkey_init(struct ibm_init_struct *iibm)
56b6aeb0 1738{
0f089147
HMH
1739 /* Requirements for changing the default keymaps:
1740 *
1741 * 1. Many of the keys are mapped to KEY_RESERVED for very
1742 * good reasons. Do not change them unless you have deep
1743 * knowledge on the IBM and Lenovo ThinkPad firmware for
1744 * the various ThinkPad models. The driver behaves
1745 * differently for KEY_RESERVED: such keys have their
1746 * hot key mask *unset* in mask_recommended, and also
1747 * in the initial hot key mask programmed into the
1748 * firmware at driver load time, which means the firm-
1749 * ware may react very differently if you change them to
1750 * something else;
1751 *
1752 * 2. You must be subscribed to the linux-thinkpad and
1753 * ibm-acpi-devel mailing lists, and you should read the
1754 * list archives since 2007 if you want to change the
1755 * keymaps. This requirement exists so that you will
1756 * know the past history of problems with the thinkpad-
1757 * acpi driver keymaps, and also that you will be
1758 * listening to any bug reports;
1759 *
1760 * 3. Do not send thinkpad-acpi specific patches directly to
1761 * for merging, *ever*. Send them to the linux-acpi
1762 * mailinglist for comments. Merging is to be done only
1763 * through acpi-test and the ACPI maintainer.
1764 *
1765 * If the above is too much to ask, don't change the keymap.
1766 * Ask the thinkpad-acpi maintainer to do it, instead.
1767 */
edf0e0e5
HMH
1768 static u16 ibm_keycode_map[] __initdata = {
1769 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1770 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
1771 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1772 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
0f089147
HMH
1773
1774 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
edf0e0e5
HMH
1775 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1776 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1777 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
0f089147
HMH
1778
1779 /* brightness: firmware always reacts to them, unless
1780 * X.org did some tricks in the radeon BIOS scratch
1781 * registers of *some* models */
e927c08d 1782 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
e927c08d 1783 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
0f089147
HMH
1784
1785 /* Thinklight: firmware always react to it */
edf0e0e5 1786 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
0f089147 1787
edf0e0e5
HMH
1788 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
1789 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
0f089147
HMH
1790
1791 /* Volume: firmware always react to it and reprograms
1792 * the built-in *extra* mixer. Never map it to control
1793 * another mixer by default. */
e927c08d
HMH
1794 KEY_RESERVED, /* 0x14: VOLUME UP */
1795 KEY_RESERVED, /* 0x15: VOLUME DOWN */
1796 KEY_RESERVED, /* 0x16: MUTE */
0f089147 1797
edf0e0e5 1798 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
0f089147 1799
edf0e0e5
HMH
1800 /* (assignments unknown, please report if found) */
1801 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1802 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1803 };
1804 static u16 lenovo_keycode_map[] __initdata = {
1805 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1806 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
1807 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1808 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
0f089147
HMH
1809
1810 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
edf0e0e5
HMH
1811 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1812 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1813 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
0f089147 1814
56a185b4 1815 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
56a185b4 1816 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
0f089147 1817
edf0e0e5 1818 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
0f089147 1819
edf0e0e5
HMH
1820 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
1821 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
0f089147
HMH
1822
1823 /* Volume: z60/z61, T60 (BIOS version?): firmware always
1824 * react to it and reprograms the built-in *extra* mixer.
1825 * Never map it to control another mixer by default.
1826 *
1827 * T60?, T61, R60?, R61: firmware and EC tries to send
1828 * these over the regular keyboard, so these are no-ops,
1829 * but there are still weird bugs re. MUTE, so do not
1830 * change unless you get test reports from all Lenovo
1831 * models. May cause the BIOS to interfere with the
1832 * HDA mixer.
1833 */
e927c08d
HMH
1834 KEY_RESERVED, /* 0x14: VOLUME UP */
1835 KEY_RESERVED, /* 0x15: VOLUME DOWN */
1836 KEY_RESERVED, /* 0x16: MUTE */
0f089147 1837
edf0e0e5 1838 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
0f089147 1839
edf0e0e5
HMH
1840 /* (assignments unknown, please report if found) */
1841 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1842 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1843 };
1844
1845#define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
1846#define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
1847#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
1848
6a38abbf 1849 int res, i;
74941a69 1850 int status;
1b6521dc 1851 int hkeyv;
b86c4722 1852
fe08bc4b
HMH
1853 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
1854
6a38abbf 1855 BUG_ON(!tpacpi_inputdev);
01e88f25
HMH
1856 BUG_ON(tpacpi_inputdev->open != NULL ||
1857 tpacpi_inputdev->close != NULL);
6a38abbf 1858
8d376cd6 1859 IBM_ACPIHANDLE_INIT(hkey);
40ca9fdf 1860 mutex_init(&hotkey_mutex);
5fba344c 1861
01e88f25
HMH
1862#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1863 mutex_init(&hotkey_thread_mutex);
1864 mutex_init(&hotkey_thread_data_mutex);
1865#endif
1866
56b6aeb0 1867 /* hotkey not supported on 570 */
d8fd94d9 1868 tp_features.hotkey = hkey_handle != NULL;
56b6aeb0 1869
fe08bc4b 1870 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
d8fd94d9 1871 str_supported(tp_features.hotkey));
fe08bc4b 1872
d8fd94d9 1873 if (tp_features.hotkey) {
01e88f25 1874 hotkey_dev_attributes = create_attr_set(10, NULL);
a0416420
HMH
1875 if (!hotkey_dev_attributes)
1876 return -ENOMEM;
ff80f137
HMH
1877 res = add_many_to_attr_set(hotkey_dev_attributes,
1878 hotkey_attributes,
1879 ARRAY_SIZE(hotkey_attributes));
a0416420
HMH
1880 if (res)
1881 return res;
1882
56b6aeb0 1883 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1b6521dc
HMH
1884 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
1885 for HKEY interface version 0x100 */
1886 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
1887 if ((hkeyv >> 8) != 1) {
1888 printk(IBM_ERR "unknown version of the "
1889 "HKEY interface: 0x%x\n", hkeyv);
1890 printk(IBM_ERR "please report this to %s\n",
1891 IBM_MAIL);
1892 } else {
1893 /*
1894 * MHKV 0x100 in A31, R40, R40e,
1895 * T4x, X31, and later
01e88f25 1896 */
1b6521dc
HMH
1897 tp_features.hotkey_mask = 1;
1898 }
1899 }
56b6aeb0 1900
fe08bc4b 1901 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
d8fd94d9 1902 str_supported(tp_features.hotkey_mask));
fe08bc4b 1903
9b010de5 1904 if (tp_features.hotkey_mask) {
9b010de5 1905 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
1b6521dc
HMH
1906 "MHKA", "qd")) {
1907 printk(IBM_ERR
1908 "missing MHKA handler, "
1909 "please report this to %s\n",
1910 IBM_MAIL);
9b010de5 1911 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
1b6521dc 1912 }
9b010de5
HMH
1913 }
1914
01e88f25
HMH
1915 /* hotkey_source_mask *must* be zero for
1916 * the first hotkey_mask_get */
b2c985e7 1917 res = hotkey_status_get(&hotkey_orig_status);
a0416420 1918 if (!res && tp_features.hotkey_mask) {
b2c985e7
HMH
1919 res = hotkey_mask_get();
1920 hotkey_orig_mask = hotkey_mask;
1921 if (!res) {
1922 res = add_many_to_attr_set(
1923 hotkey_dev_attributes,
1924 hotkey_mask_attributes,
1925 ARRAY_SIZE(hotkey_mask_attributes));
1926 }
a0416420 1927 }
74941a69 1928
01e88f25
HMH
1929#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1930 if (tp_features.hotkey_mask) {
1931 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
1932 & ~hotkey_all_mask;
1933 } else {
1934 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
1935 }
1936
1937 vdbg_printk(TPACPI_DBG_INIT,
1938 "hotkey source mask 0x%08x, polling freq %d\n",
1939 hotkey_source_mask, hotkey_poll_freq);
1940#endif
1941
74941a69
HMH
1942 /* Not all thinkpads have a hardware radio switch */
1943 if (!res && acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
1944 tp_features.hotkey_wlsw = 1;
1945 printk(IBM_INFO
1946 "radio switch found; radios are %s\n",
1947 enabled(status, 0));
1948 res = add_to_attr_set(hotkey_dev_attributes,
1949 &dev_attr_hotkey_radio_sw.attr);
1950 }
1951
a0416420
HMH
1952 if (!res)
1953 res = register_attr_set_with_sysfs(
1954 hotkey_dev_attributes,
1955 &tpacpi_pdev->dev.kobj);
b86c4722
HMH
1956 if (res)
1957 return res;
6a38abbf 1958
edf0e0e5
HMH
1959 /* Set up key map */
1960
1961 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
1962 GFP_KERNEL);
1963 if (!hotkey_keycode_map) {
1964 printk(IBM_ERR "failed to allocate memory for key map\n");
1965 return -ENOMEM;
1966 }
1967
1968 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
1969 dbg_printk(TPACPI_DBG_INIT,
1970 "using Lenovo default hot key map\n");
1971 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
1972 TPACPI_HOTKEY_MAP_SIZE);
1973 } else {
1974 dbg_printk(TPACPI_DBG_INIT,
1975 "using IBM default hot key map\n");
1976 memcpy(hotkey_keycode_map, &ibm_keycode_map,
1977 TPACPI_HOTKEY_MAP_SIZE);
1978 }
1979
6a38abbf
HMH
1980 set_bit(EV_KEY, tpacpi_inputdev->evbit);
1981 set_bit(EV_MSC, tpacpi_inputdev->evbit);
1982 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
edf0e0e5
HMH
1983 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
1984 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
1985 tpacpi_inputdev->keycode = hotkey_keycode_map;
1986 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
6a38abbf
HMH
1987 if (hotkey_keycode_map[i] != KEY_RESERVED) {
1988 set_bit(hotkey_keycode_map[i],
1989 tpacpi_inputdev->keybit);
1990 } else {
1991 if (i < sizeof(hotkey_reserved_mask)*8)
1992 hotkey_reserved_mask |= 1 << i;
1993 }
1994 }
1995
5c29d58f
HMH
1996 if (tp_features.hotkey_wlsw) {
1997 set_bit(EV_SW, tpacpi_inputdev->evbit);
1998 set_bit(SW_RADIO, tpacpi_inputdev->swbit);
1999 }
2000
1a343760
HMH
2001 dbg_printk(TPACPI_DBG_INIT,
2002 "enabling hot key handling\n");
b2c985e7
HMH
2003 res = hotkey_status_set(1);
2004 if (res)
2005 return res;
01e88f25
HMH
2006 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
2007 & ~hotkey_reserved_mask)
1a343760 2008 | hotkey_orig_mask);
01e88f25 2009 if (res < 0 && res != -ENXIO)
1a343760 2010 return res;
ff80f137
HMH
2011
2012 dbg_printk(TPACPI_DBG_INIT,
2013 "legacy hot key reporting over procfs %s\n",
2014 (hotkey_report_mode < 2) ?
2015 "enabled" : "disabled");
01e88f25
HMH
2016
2017#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2018 tpacpi_inputdev->open = &hotkey_inputdev_open;
2019 tpacpi_inputdev->close = &hotkey_inputdev_close;
2020
2021 hotkey_poll_setup_safe(1);
2022#endif
56b6aeb0
HMH
2023 }
2024
d8fd94d9 2025 return (tp_features.hotkey)? 0 : 1;
56b6aeb0
HMH
2026}
2027
2028static void hotkey_exit(void)
2029{
01e88f25
HMH
2030#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2031 hotkey_poll_stop_sync();
2032#endif
2033
d8fd94d9 2034 if (tp_features.hotkey) {
b2c985e7
HMH
2035 dbg_printk(TPACPI_DBG_EXIT, "restoring original hot key mask\n");
2036 /* no short-circuit boolean operator below! */
2037 if ((hotkey_mask_set(hotkey_orig_mask) |
2038 hotkey_status_set(hotkey_orig_status)) != 0)
2039 printk(IBM_ERR "failed to restore hot key mask to BIOS defaults\n");
5fba344c 2040 }
a0416420
HMH
2041
2042 if (hotkey_dev_attributes) {
2043 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2044 hotkey_dev_attributes = NULL;
2045 }
56b6aeb0
HMH
2046}
2047
2048static void hotkey_notify(struct ibm_struct *ibm, u32 event)
2049{
6a38abbf 2050 u32 hkey;
b7c8c200 2051 unsigned int scancode;
3eea123d 2052 int send_acpi_ev;
3e5ce914 2053 int ignore_acpi_ev;
3eea123d
HMH
2054
2055 if (event != 0x80) {
2056 printk(IBM_ERR "unknown HKEY notification event %d\n", event);
2057 /* forward it to userspace, maybe it knows how to handle it */
2058 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
2059 ibm->acpi->device->dev.bus_id,
2060 event, 0);
2061 return;
2062 }
2063
2064 while (1) {
2065 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
2066 printk(IBM_ERR "failed to retrieve HKEY event\n");
2067 return;
2068 }
2069
2070 if (hkey == 0) {
2071 /* queue empty */
2072 return;
2073 }
2074
2075 send_acpi_ev = 0;
3e5ce914 2076 ignore_acpi_ev = 0;
56b6aeb0 2077
ff80f137
HMH
2078 switch (hkey >> 12) {
2079 case 1:
2080 /* 0x1000-0x1FFF: key presses */
2081 scancode = hkey & 0xfff;
2082 if (scancode > 0 && scancode < 0x21) {
2083 scancode--;
01e88f25
HMH
2084 if (!(hotkey_source_mask & (1 << scancode))) {
2085 tpacpi_input_send_key(scancode);
2086 } else {
2087 ignore_acpi_ev = 1;
2088 }
ff80f137
HMH
2089 } else {
2090 printk(IBM_ERR
2091 "hotkey 0x%04x out of range for keyboard map\n",
2092 hkey);
2093 send_acpi_ev = 1;
2094 }
2095 break;
2096 case 5:
2097 /* 0x5000-0x5FFF: LID */
2098 /* we don't handle it through this path, just
2099 * eat up known LID events */
2100 if (hkey != 0x5001 && hkey != 0x5002) {
2101 printk(IBM_ERR
3eea123d
HMH
2102 "unknown LID-related HKEY event: 0x%04x\n",
2103 hkey);
ff80f137 2104 send_acpi_ev = 1;
3e5ce914
HMH
2105 } else {
2106 ignore_acpi_ev = 1;
ff80f137
HMH
2107 }
2108 break;
2109 case 7:
2110 /* 0x7000-0x7FFF: misc */
2111 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
2112 tpacpi_input_send_radiosw();
6a38abbf 2113 break;
6a38abbf 2114 }
ff80f137
HMH
2115 /* fallthrough to default */
2116 default:
2117 /* case 2: dock-related */
2118 /* 0x2305 - T43 waking up due to bay lever eject while aslept */
2119 /* case 3: ultra-bay related. maybe bay in dock? */
2120 /* 0x3003 - T43 after wake up by bay lever eject (0x2305) */
2121 printk(IBM_NOTICE "unhandled HKEY event 0x%04x\n", hkey);
2122 send_acpi_ev = 1;
6a38abbf 2123 }
ff80f137 2124
3eea123d 2125 /* Legacy events */
3e5ce914 2126 if (!ignore_acpi_ev && (send_acpi_ev || hotkey_report_mode < 2)) {
3eea123d 2127 acpi_bus_generate_proc_event(ibm->acpi->device, event, hkey);
3e5ce914 2128 }
ff80f137 2129
3eea123d 2130 /* netlink events */
3e5ce914 2131 if (!ignore_acpi_ev && send_acpi_ev) {
3eea123d
HMH
2132 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
2133 ibm->acpi->device->dev.bus_id,
2134 event, hkey);
2135 }
56b6aeb0
HMH
2136 }
2137}
2138
5c29d58f
HMH
2139static void hotkey_resume(void)
2140{
b2c985e7
HMH
2141 if (hotkey_mask_get())
2142 printk(IBM_ERR "error while trying to read hot key mask from firmware\n");
5c29d58f 2143 tpacpi_input_send_radiosw();
01e88f25
HMH
2144#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2145 hotkey_poll_setup_safe(0);
2146#endif
5c29d58f
HMH
2147}
2148
a0416420 2149/* procfs -------------------------------------------------------------- */
78f81cc4 2150static int hotkey_read(char *p)
1da177e4 2151{
ae92bd17 2152 int res, status;
1da177e4
LT
2153 int len = 0;
2154
d8fd94d9 2155 if (!tp_features.hotkey) {
78f81cc4
BD
2156 len += sprintf(p + len, "status:\t\tnot supported\n");
2157 return len;
2158 }
2159
fc589a3c
HMH
2160 if (mutex_lock_interruptible(&hotkey_mutex))
2161 return -ERESTARTSYS;
b2c985e7
HMH
2162 res = hotkey_status_get(&status);
2163 if (!res)
2164 res = hotkey_mask_get();
40ca9fdf 2165 mutex_unlock(&hotkey_mutex);
b86c4722
HMH
2166 if (res)
2167 return res;
1da177e4
LT
2168
2169 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
d8fd94d9 2170 if (tp_features.hotkey_mask) {
b2c985e7 2171 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
1da177e4
LT
2172 len += sprintf(p + len,
2173 "commands:\tenable, disable, reset, <mask>\n");
2174 } else {
2175 len += sprintf(p + len, "mask:\t\tnot supported\n");
2176 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
2177 }
2178
2179 return len;
2180}
2181
78f81cc4 2182static int hotkey_write(char *buf)
1da177e4 2183{
ae92bd17
HMH
2184 int res, status;
2185 u32 mask;
1da177e4 2186 char *cmd;
1da177e4 2187
d8fd94d9 2188 if (!tp_features.hotkey)
1da177e4
LT
2189 return -ENODEV;
2190
fc589a3c
HMH
2191 if (mutex_lock_interruptible(&hotkey_mutex))
2192 return -ERESTARTSYS;
40ca9fdf 2193
b2c985e7
HMH
2194 status = -1;
2195 mask = hotkey_mask;
78f81cc4 2196
40ca9fdf 2197 res = 0;
1da177e4
LT
2198 while ((cmd = next_cmd(&buf))) {
2199 if (strlencmp(cmd, "enable") == 0) {
2200 status = 1;
2201 } else if (strlencmp(cmd, "disable") == 0) {
2202 status = 0;
2203 } else if (strlencmp(cmd, "reset") == 0) {
78f81cc4
BD
2204 status = hotkey_orig_status;
2205 mask = hotkey_orig_mask;
1da177e4
LT
2206 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
2207 /* mask set */
2208 } else if (sscanf(cmd, "%x", &mask) == 1) {
2209 /* mask set */
40ca9fdf
HMH
2210 } else {
2211 res = -EINVAL;
2212 goto errexit;
2213 }
1da177e4 2214 }
b2c985e7
HMH
2215 if (status != -1)
2216 res = hotkey_status_set(status);
1da177e4 2217
b2c985e7
HMH
2218 if (!res && mask != hotkey_mask)
2219 res = hotkey_mask_set(mask);
1da177e4 2220
40ca9fdf
HMH
2221errexit:
2222 mutex_unlock(&hotkey_mutex);
2223 return res;
78f81cc4 2224}
1da177e4 2225
1ba90e3a
TR
2226static const struct acpi_device_id ibm_htk_device_ids[] = {
2227 {IBM_HKEY_HID, 0},
2228 {"", 0},
2229};
2230
8d376cd6 2231static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
1ba90e3a 2232 .hid = ibm_htk_device_ids,
8d376cd6
HMH
2233 .notify = hotkey_notify,
2234 .handle = &hkey_handle,
2235 .type = ACPI_DEVICE_NOTIFY,
2236};
2237
a5763f22
HMH
2238static struct ibm_struct hotkey_driver_data = {
2239 .name = "hotkey",
a5763f22
HMH
2240 .read = hotkey_read,
2241 .write = hotkey_write,
2242 .exit = hotkey_exit,
5c29d58f 2243 .resume = hotkey_resume,
8d376cd6 2244 .acpi = &ibm_hotkey_acpidriver,
a5763f22
HMH
2245};
2246
56b6aeb0
HMH
2247/*************************************************************************
2248 * Bluetooth subdriver
2249 */
1da177e4 2250
f74a27d4
HMH
2251enum {
2252 /* ACPI GBDC/SBDC bits */
2253 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
2254 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
2255 TP_ACPI_BLUETOOTH_UNK = 0x04, /* unknown function */
2256};
2257
2258static int bluetooth_get_radiosw(void);
2259static int bluetooth_set_radiosw(int radio_on);
2260
d3a6ade4
HMH
2261/* sysfs bluetooth enable ---------------------------------------------- */
2262static ssize_t bluetooth_enable_show(struct device *dev,
2263 struct device_attribute *attr,
2264 char *buf)
2265{
2266 int status;
2267
2268 status = bluetooth_get_radiosw();
2269 if (status < 0)
2270 return status;
2271
2272 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2273}
2274
2275static ssize_t bluetooth_enable_store(struct device *dev,
2276 struct device_attribute *attr,
2277 const char *buf, size_t count)
2278{
2279 unsigned long t;
2280 int res;
2281
2282 if (parse_strtoul(buf, 1, &t))
2283 return -EINVAL;
2284
2285 res = bluetooth_set_radiosw(t);
2286
2287 return (res) ? res : count;
2288}
2289
2290static struct device_attribute dev_attr_bluetooth_enable =
cc4c24e1 2291 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
d3a6ade4
HMH
2292 bluetooth_enable_show, bluetooth_enable_store);
2293
2294/* --------------------------------------------------------------------- */
2295
2296static struct attribute *bluetooth_attributes[] = {
2297 &dev_attr_bluetooth_enable.attr,
2298 NULL
2299};
2300
2301static const struct attribute_group bluetooth_attr_group = {
d3a6ade4
HMH
2302 .attrs = bluetooth_attributes,
2303};
2304
a5763f22 2305static int __init bluetooth_init(struct ibm_init_struct *iibm)
1da177e4 2306{
d3a6ade4 2307 int res;
d6fdd1e9
HMH
2308 int status = 0;
2309
fe08bc4b
HMH
2310 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
2311
8d376cd6 2312 IBM_ACPIHANDLE_INIT(hkey);
5fba344c 2313
78f81cc4
BD
2314 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2315 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
d8fd94d9 2316 tp_features.bluetooth = hkey_handle &&
d6fdd1e9
HMH
2317 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
2318
2319 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
2320 str_supported(tp_features.bluetooth),
2321 status);
2322
d3a6ade4
HMH
2323 if (tp_features.bluetooth) {
2324 if (!(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
2325 /* no bluetooth hardware present in system */
2326 tp_features.bluetooth = 0;
2327 dbg_printk(TPACPI_DBG_INIT,
2328 "bluetooth hardware not installed\n");
2329 } else {
2330 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2331 &bluetooth_attr_group);
2332 if (res)
2333 return res;
2334 }
d6fdd1e9 2335 }
fe08bc4b 2336
d8fd94d9 2337 return (tp_features.bluetooth)? 0 : 1;
1da177e4
LT
2338}
2339
d3a6ade4
HMH
2340static void bluetooth_exit(void)
2341{
2342 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2343 &bluetooth_attr_group);
2344}
2345
d6fdd1e9 2346static int bluetooth_get_radiosw(void)
1da177e4
LT
2347{
2348 int status;
2349
d6fdd1e9
HMH
2350 if (!tp_features.bluetooth)
2351 return -ENODEV;
1da177e4 2352
d6fdd1e9
HMH
2353 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2354 return -EIO;
2355
2356 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
2357}
2358
2359static int bluetooth_set_radiosw(int radio_on)
2360{
2361 int status;
2362
2363 if (!tp_features.bluetooth)
2364 return -ENODEV;
2365
2366 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2367 return -EIO;
2368 if (radio_on)
2369 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
2370 else
2371 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
2372 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
2373 return -EIO;
2374
2375 return 0;
1da177e4
LT
2376}
2377
d3a6ade4 2378/* procfs -------------------------------------------------------------- */
78f81cc4 2379static int bluetooth_read(char *p)
1da177e4
LT
2380{
2381 int len = 0;
d6fdd1e9 2382 int status = bluetooth_get_radiosw();
1da177e4 2383
d8fd94d9 2384 if (!tp_features.bluetooth)
1da177e4 2385 len += sprintf(p + len, "status:\t\tnot supported\n");
1da177e4 2386 else {
d6fdd1e9
HMH
2387 len += sprintf(p + len, "status:\t\t%s\n",
2388 (status)? "enabled" : "disabled");
1da177e4
LT
2389 len += sprintf(p + len, "commands:\tenable, disable\n");
2390 }
2391
2392 return len;
2393}
2394
78f81cc4 2395static int bluetooth_write(char *buf)
1da177e4 2396{
1da177e4 2397 char *cmd;
1da177e4 2398
d8fd94d9 2399 if (!tp_features.bluetooth)
78f81cc4 2400 return -ENODEV;
1da177e4
LT
2401
2402 while ((cmd = next_cmd(&buf))) {
2403 if (strlencmp(cmd, "enable") == 0) {
d6fdd1e9 2404 bluetooth_set_radiosw(1);
1da177e4 2405 } else if (strlencmp(cmd, "disable") == 0) {
d6fdd1e9 2406 bluetooth_set_radiosw(0);
1da177e4
LT
2407 } else
2408 return -EINVAL;
1da177e4
LT
2409 }
2410
1da177e4
LT
2411 return 0;
2412}
2413
a5763f22
HMH
2414static struct ibm_struct bluetooth_driver_data = {
2415 .name = "bluetooth",
2416 .read = bluetooth_read,
2417 .write = bluetooth_write,
d3a6ade4 2418 .exit = bluetooth_exit,
a5763f22
HMH
2419};
2420
56b6aeb0
HMH
2421/*************************************************************************
2422 * Wan subdriver
2423 */
2424
f74a27d4
HMH
2425enum {
2426 /* ACPI GWAN/SWAN bits */
2427 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
2428 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
2429 TP_ACPI_WANCARD_UNK = 0x04, /* unknown function */
2430};
2431
2432static int wan_get_radiosw(void);
2433static int wan_set_radiosw(int radio_on);
2434
d3a6ade4
HMH
2435/* sysfs wan enable ---------------------------------------------------- */
2436static ssize_t wan_enable_show(struct device *dev,
2437 struct device_attribute *attr,
2438 char *buf)
2439{
2440 int status;
2441
2442 status = wan_get_radiosw();
2443 if (status < 0)
2444 return status;
2445
2446 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2447}
2448
2449static ssize_t wan_enable_store(struct device *dev,
2450 struct device_attribute *attr,
2451 const char *buf, size_t count)
2452{
2453 unsigned long t;
2454 int res;
2455
2456 if (parse_strtoul(buf, 1, &t))
2457 return -EINVAL;
2458
2459 res = wan_set_radiosw(t);
2460
2461 return (res) ? res : count;
2462}
2463
2464static struct device_attribute dev_attr_wan_enable =
cc4c24e1 2465 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
d3a6ade4
HMH
2466 wan_enable_show, wan_enable_store);
2467
2468/* --------------------------------------------------------------------- */
2469
2470static struct attribute *wan_attributes[] = {
2471 &dev_attr_wan_enable.attr,
2472 NULL
2473};
2474
2475static const struct attribute_group wan_attr_group = {
d3a6ade4
HMH
2476 .attrs = wan_attributes,
2477};
2478
a5763f22 2479static int __init wan_init(struct ibm_init_struct *iibm)
42adb53c 2480{
d3a6ade4 2481 int res;
d6fdd1e9
HMH
2482 int status = 0;
2483
fe08bc4b
HMH
2484 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
2485
8d376cd6 2486 IBM_ACPIHANDLE_INIT(hkey);
5fba344c 2487
d8fd94d9 2488 tp_features.wan = hkey_handle &&
d6fdd1e9
HMH
2489 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
2490
2491 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
2492 str_supported(tp_features.wan),
2493 status);
2494
d3a6ade4
HMH
2495 if (tp_features.wan) {
2496 if (!(status & TP_ACPI_WANCARD_HWPRESENT)) {
2497 /* no wan hardware present in system */
2498 tp_features.wan = 0;
2499 dbg_printk(TPACPI_DBG_INIT,
2500 "wan hardware not installed\n");
2501 } else {
2502 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2503 &wan_attr_group);
2504 if (res)
2505 return res;
2506 }
d6fdd1e9 2507 }
fe08bc4b 2508
d8fd94d9 2509 return (tp_features.wan)? 0 : 1;
42adb53c
JF
2510}
2511
d3a6ade4
HMH
2512static void wan_exit(void)
2513{
2514 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2515 &wan_attr_group);
2516}
2517
d6fdd1e9 2518static int wan_get_radiosw(void)
42adb53c
JF
2519{
2520 int status;
2521
d6fdd1e9
HMH
2522 if (!tp_features.wan)
2523 return -ENODEV;
42adb53c 2524
d6fdd1e9
HMH
2525 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2526 return -EIO;
2527
2528 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
2529}
2530
2531static int wan_set_radiosw(int radio_on)
2532{
2533 int status;
2534
2535 if (!tp_features.wan)
2536 return -ENODEV;
2537
2538 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2539 return -EIO;
2540 if (radio_on)
2541 status |= TP_ACPI_WANCARD_RADIOSSW;
2542 else
2543 status &= ~TP_ACPI_WANCARD_RADIOSSW;
2544 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
2545 return -EIO;
2546
2547 return 0;
42adb53c
JF
2548}
2549
d3a6ade4 2550/* procfs -------------------------------------------------------------- */
42adb53c
JF
2551static int wan_read(char *p)
2552{
2553 int len = 0;
d6fdd1e9 2554 int status = wan_get_radiosw();
42adb53c 2555
d8fd94d9 2556 if (!tp_features.wan)
42adb53c 2557 len += sprintf(p + len, "status:\t\tnot supported\n");
42adb53c 2558 else {
d6fdd1e9
HMH
2559 len += sprintf(p + len, "status:\t\t%s\n",
2560 (status)? "enabled" : "disabled");
42adb53c
JF
2561 len += sprintf(p + len, "commands:\tenable, disable\n");
2562 }
2563
2564 return len;
2565}
2566
2567static int wan_write(char *buf)
2568{
42adb53c 2569 char *cmd;
42adb53c 2570
d8fd94d9 2571 if (!tp_features.wan)
42adb53c
JF
2572 return -ENODEV;
2573
2574 while ((cmd = next_cmd(&buf))) {
2575 if (strlencmp(cmd, "enable") == 0) {
d6fdd1e9 2576 wan_set_radiosw(1);
42adb53c 2577 } else if (strlencmp(cmd, "disable") == 0) {
d6fdd1e9 2578 wan_set_radiosw(0);
42adb53c
JF
2579 } else
2580 return -EINVAL;
42adb53c
JF
2581 }
2582
42adb53c
JF
2583 return 0;
2584}
2585
a5763f22
HMH
2586static struct ibm_struct wan_driver_data = {
2587 .name = "wan",
2588 .read = wan_read,
2589 .write = wan_write,
d3a6ade4 2590 .exit = wan_exit,
92641177 2591 .flags.experimental = 1,
a5763f22
HMH
2592};
2593
56b6aeb0
HMH
2594/*************************************************************************
2595 * Video subdriver
2596 */
2597
f74a27d4
HMH
2598enum video_access_mode {
2599 TPACPI_VIDEO_NONE = 0,
2600 TPACPI_VIDEO_570, /* 570 */
2601 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
2602 TPACPI_VIDEO_NEW, /* all others */
2603};
2604
2605enum { /* video status flags, based on VIDEO_570 */
2606 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
2607 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
2608 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
2609};
2610
2611enum { /* TPACPI_VIDEO_570 constants */
2612 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
2613 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
2614 * video_status_flags */
2615 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
2616 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
2617};
2618
9a8e1738
HMH
2619static enum video_access_mode video_supported;
2620static int video_orig_autosw;
78f81cc4 2621
f74a27d4
HMH
2622static int video_autosw_get(void);
2623static int video_autosw_set(int enable);
2624
56b6aeb0
HMH
2625IBM_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
2626 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
2627 "\\_SB.PCI0.VID0", /* 770e */
2628 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
2629 "\\_SB.PCI0.AGP.VID", /* all others */
2630 ); /* R30, R31 */
2631
2632IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
2633
a5763f22 2634static int __init video_init(struct ibm_init_struct *iibm)
1da177e4 2635{
78f81cc4
BD
2636 int ivga;
2637
fe08bc4b
HMH
2638 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
2639
8d376cd6
HMH
2640 IBM_ACPIHANDLE_INIT(vid);
2641 IBM_ACPIHANDLE_INIT(vid2);
5fba344c 2642
78f81cc4
BD
2643 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
2644 /* G41, assume IVGA doesn't change */
2645 vid_handle = vid2_handle;
2646
2647 if (!vid_handle)
2648 /* video switching not supported on R30, R31 */
efa27145 2649 video_supported = TPACPI_VIDEO_NONE;
78f81cc4
BD
2650 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
2651 /* 570 */
efa27145 2652 video_supported = TPACPI_VIDEO_570;
78f81cc4
BD
2653 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
2654 /* 600e/x, 770e, 770x */
efa27145 2655 video_supported = TPACPI_VIDEO_770;
78f81cc4
BD
2656 else
2657 /* all others */
efa27145 2658 video_supported = TPACPI_VIDEO_NEW;
1da177e4 2659
fe08bc4b
HMH
2660 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
2661 str_supported(video_supported != TPACPI_VIDEO_NONE),
2662 video_supported);
2663
5fba344c 2664 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
1da177e4
LT
2665}
2666
56b6aeb0
HMH
2667static void video_exit(void)
2668{
83f34724
HMH
2669 dbg_printk(TPACPI_DBG_EXIT,
2670 "restoring original video autoswitch mode\n");
2671 if (video_autosw_set(video_orig_autosw))
2672 printk(IBM_ERR "error while trying to restore original "
2673 "video autoswitch mode\n");
56b6aeb0
HMH
2674}
2675
83f34724 2676static int video_outputsw_get(void)
1da177e4
LT
2677{
2678 int status = 0;
2679 int i;
2680
83f34724
HMH
2681 switch (video_supported) {
2682 case TPACPI_VIDEO_570:
2683 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
2684 TP_ACPI_VIDEO_570_PHSCMD))
2685 return -EIO;
2686 status = i & TP_ACPI_VIDEO_570_PHSMASK;
2687 break;
2688 case TPACPI_VIDEO_770:
2689 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
2690 return -EIO;
2691 if (i)
2692 status |= TP_ACPI_VIDEO_S_LCD;
2693 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
2694 return -EIO;
2695 if (i)
2696 status |= TP_ACPI_VIDEO_S_CRT;
2697 break;
2698 case TPACPI_VIDEO_NEW:
2699 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
2700 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
2701 return -EIO;
2702 if (i)
2703 status |= TP_ACPI_VIDEO_S_CRT;
2704
2705 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
2706 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
2707 return -EIO;
2708 if (i)
2709 status |= TP_ACPI_VIDEO_S_LCD;
2710 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
2711 return -EIO;
2712 if (i)
2713 status |= TP_ACPI_VIDEO_S_DVI;
2714 break;
2715 default:
2716 return -ENOSYS;
78f81cc4
BD
2717 }
2718
2719 return status;
2720}
1da177e4 2721
83f34724 2722static int video_outputsw_set(int status)
78f81cc4 2723{
83f34724
HMH
2724 int autosw;
2725 int res = 0;
2726
2727 switch (video_supported) {
2728 case TPACPI_VIDEO_570:
2729 res = acpi_evalf(NULL, NULL,
2730 "\\_SB.PHS2", "vdd",
2731 TP_ACPI_VIDEO_570_PHS2CMD,
2732 status | TP_ACPI_VIDEO_570_PHS2SET);
2733 break;
2734 case TPACPI_VIDEO_770:
2735 autosw = video_autosw_get();
2736 if (autosw < 0)
2737 return autosw;
1da177e4 2738
83f34724
HMH
2739 res = video_autosw_set(1);
2740 if (res)
2741 return res;
2742 res = acpi_evalf(vid_handle, NULL,
2743 "ASWT", "vdd", status * 0x100, 0);
2744 if (!autosw && video_autosw_set(autosw)) {
2745 printk(IBM_ERR "video auto-switch left enabled due to error\n");
2746 return -EIO;
2747 }
2748 break;
2749 case TPACPI_VIDEO_NEW:
2750 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
2751 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
2752 break;
2753 default:
2754 return -ENOSYS;
2755 }
1da177e4 2756
83f34724 2757 return (res)? 0 : -EIO;
1da177e4
LT
2758}
2759
83f34724 2760static int video_autosw_get(void)
78f81cc4 2761{
83f34724 2762 int autosw = 0;
78f81cc4 2763
83f34724
HMH
2764 switch (video_supported) {
2765 case TPACPI_VIDEO_570:
2766 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
2767 return -EIO;
2768 break;
2769 case TPACPI_VIDEO_770:
2770 case TPACPI_VIDEO_NEW:
2771 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
2772 return -EIO;
2773 break;
2774 default:
2775 return -ENOSYS;
2776 }
78f81cc4 2777
83f34724 2778 return autosw & 1;
78f81cc4
BD
2779}
2780
83f34724 2781static int video_autosw_set(int enable)
78f81cc4 2782{
83f34724
HMH
2783 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
2784 return -EIO;
2785 return 0;
78f81cc4
BD
2786}
2787
83f34724 2788static int video_outputsw_cycle(void)
78f81cc4 2789{
83f34724
HMH
2790 int autosw = video_autosw_get();
2791 int res;
78f81cc4 2792
83f34724
HMH
2793 if (autosw < 0)
2794 return autosw;
78f81cc4 2795
83f34724
HMH
2796 switch (video_supported) {
2797 case TPACPI_VIDEO_570:
2798 res = video_autosw_set(1);
2799 if (res)
2800 return res;
2801 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
2802 break;
2803 case TPACPI_VIDEO_770:
2804 case TPACPI_VIDEO_NEW:
2805 res = video_autosw_set(1);
2806 if (res)
2807 return res;
2808 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
2809 break;
2810 default:
2811 return -ENOSYS;
2812 }
2813 if (!autosw && video_autosw_set(autosw)) {
2814 printk(IBM_ERR "video auto-switch left enabled due to error\n");
2815 return -EIO;
78f81cc4
BD
2816 }
2817
83f34724
HMH
2818 return (res)? 0 : -EIO;
2819}
2820
2821static int video_expand_toggle(void)
2822{
2823 switch (video_supported) {
2824 case TPACPI_VIDEO_570:
2825 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
2826 0 : -EIO;
2827 case TPACPI_VIDEO_770:
2828 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
2829 0 : -EIO;
2830 case TPACPI_VIDEO_NEW:
2831 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
2832 0 : -EIO;
2833 default:
2834 return -ENOSYS;
2835 }
2836 /* not reached */
78f81cc4
BD
2837}
2838
56b6aeb0
HMH
2839static int video_read(char *p)
2840{
83f34724 2841 int status, autosw;
56b6aeb0
HMH
2842 int len = 0;
2843
83f34724 2844 if (video_supported == TPACPI_VIDEO_NONE) {
56b6aeb0
HMH
2845 len += sprintf(p + len, "status:\t\tnot supported\n");
2846 return len;
2847 }
2848
83f34724
HMH
2849 status = video_outputsw_get();
2850 if (status < 0)
2851 return status;
2852
2853 autosw = video_autosw_get();
2854 if (autosw < 0)
2855 return autosw;
2856
56b6aeb0
HMH
2857 len += sprintf(p + len, "status:\t\tsupported\n");
2858 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
2859 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
efa27145 2860 if (video_supported == TPACPI_VIDEO_NEW)
56b6aeb0
HMH
2861 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
2862 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
2863 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
2864 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
efa27145 2865 if (video_supported == TPACPI_VIDEO_NEW)
56b6aeb0
HMH
2866 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
2867 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
2868 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
2869
2870 return len;
2871}
2872
78f81cc4 2873static int video_write(char *buf)
1da177e4
LT
2874{
2875 char *cmd;
2876 int enable, disable, status;
83f34724 2877 int res;
1da177e4 2878
83f34724 2879 if (video_supported == TPACPI_VIDEO_NONE)
78f81cc4
BD
2880 return -ENODEV;
2881
83f34724
HMH
2882 enable = 0;
2883 disable = 0;
1da177e4
LT
2884
2885 while ((cmd = next_cmd(&buf))) {
2886 if (strlencmp(cmd, "lcd_enable") == 0) {
83f34724 2887 enable |= TP_ACPI_VIDEO_S_LCD;
1da177e4 2888 } else if (strlencmp(cmd, "lcd_disable") == 0) {
83f34724 2889 disable |= TP_ACPI_VIDEO_S_LCD;
1da177e4 2890 } else if (strlencmp(cmd, "crt_enable") == 0) {
83f34724 2891 enable |= TP_ACPI_VIDEO_S_CRT;
1da177e4 2892 } else if (strlencmp(cmd, "crt_disable") == 0) {
83f34724 2893 disable |= TP_ACPI_VIDEO_S_CRT;
efa27145 2894 } else if (video_supported == TPACPI_VIDEO_NEW &&
78f81cc4 2895 strlencmp(cmd, "dvi_enable") == 0) {
83f34724 2896 enable |= TP_ACPI_VIDEO_S_DVI;
efa27145 2897 } else if (video_supported == TPACPI_VIDEO_NEW &&
78f81cc4 2898 strlencmp(cmd, "dvi_disable") == 0) {
83f34724 2899 disable |= TP_ACPI_VIDEO_S_DVI;
1da177e4 2900 } else if (strlencmp(cmd, "auto_enable") == 0) {
83f34724
HMH
2901 res = video_autosw_set(1);
2902 if (res)
2903 return res;
1da177e4 2904 } else if (strlencmp(cmd, "auto_disable") == 0) {
83f34724
HMH
2905 res = video_autosw_set(0);
2906 if (res)
2907 return res;
1da177e4 2908 } else if (strlencmp(cmd, "video_switch") == 0) {
83f34724
HMH
2909 res = video_outputsw_cycle();
2910 if (res)
2911 return res;
1da177e4 2912 } else if (strlencmp(cmd, "expand_toggle") == 0) {
83f34724
HMH
2913 res = video_expand_toggle();
2914 if (res)
2915 return res;
1da177e4
LT
2916 } else
2917 return -EINVAL;
2918 }
2919
2920 if (enable || disable) {
83f34724
HMH
2921 status = video_outputsw_get();
2922 if (status < 0)
2923 return status;
2924 res = video_outputsw_set((status & ~disable) | enable);
2925 if (res)
2926 return res;
1da177e4
LT
2927 }
2928
2929 return 0;
2930}
2931
a5763f22
HMH
2932static struct ibm_struct video_driver_data = {
2933 .name = "video",
2934 .read = video_read,
2935 .write = video_write,
2936 .exit = video_exit,
2937};
2938
56b6aeb0
HMH
2939/*************************************************************************
2940 * Light (thinklight) subdriver
2941 */
1da177e4 2942
56b6aeb0
HMH
2943IBM_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
2944IBM_HANDLE(ledb, ec, "LEDB"); /* G4x */
2945
a5763f22 2946static int __init light_init(struct ibm_init_struct *iibm)
1da177e4 2947{
fe08bc4b
HMH
2948 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
2949
8d376cd6
HMH
2950 IBM_ACPIHANDLE_INIT(ledb);
2951 IBM_ACPIHANDLE_INIT(lght);
2952 IBM_ACPIHANDLE_INIT(cmos);
5fba344c 2953
78f81cc4 2954 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
d8fd94d9 2955 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
78f81cc4 2956
d8fd94d9 2957 if (tp_features.light)
78f81cc4
BD
2958 /* light status not supported on
2959 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
d8fd94d9
HMH
2960 tp_features.light_status =
2961 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
1da177e4 2962
fe08bc4b 2963 vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
d8fd94d9 2964 str_supported(tp_features.light));
fe08bc4b 2965
d8fd94d9 2966 return (tp_features.light)? 0 : 1;
1da177e4
LT
2967}
2968
78f81cc4 2969static int light_read(char *p)
1da177e4
LT
2970{
2971 int len = 0;
2972 int status = 0;
2973
d8fd94d9 2974 if (!tp_features.light) {
78f81cc4 2975 len += sprintf(p + len, "status:\t\tnot supported\n");
d8fd94d9 2976 } else if (!tp_features.light_status) {
78f81cc4
BD
2977 len += sprintf(p + len, "status:\t\tunknown\n");
2978 len += sprintf(p + len, "commands:\ton, off\n");
2979 } else {
1da177e4
LT
2980 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
2981 return -EIO;
2982 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
78f81cc4
BD
2983 len += sprintf(p + len, "commands:\ton, off\n");
2984 }
1da177e4
LT
2985
2986 return len;
2987}
2988
78f81cc4 2989static int light_write(char *buf)
1da177e4
LT
2990{
2991 int cmos_cmd, lght_cmd;
2992 char *cmd;
2993 int success;
78f81cc4 2994
d8fd94d9 2995 if (!tp_features.light)
78f81cc4
BD
2996 return -ENODEV;
2997
1da177e4
LT
2998 while ((cmd = next_cmd(&buf))) {
2999 if (strlencmp(cmd, "on") == 0) {
3000 cmos_cmd = 0x0c;
3001 lght_cmd = 1;
3002 } else if (strlencmp(cmd, "off") == 0) {
3003 cmos_cmd = 0x0d;
3004 lght_cmd = 0;
3005 } else
3006 return -EINVAL;
78f81cc4 3007
1da177e4 3008 success = cmos_handle ?
78f81cc4
BD
3009 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
3010 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
1da177e4
LT
3011 if (!success)
3012 return -EIO;
3013 }
3014
3015 return 0;
3016}
3017
a5763f22
HMH
3018static struct ibm_struct light_driver_data = {
3019 .name = "light",
3020 .read = light_read,
3021 .write = light_write,
3022};
3023
56b6aeb0
HMH
3024/*************************************************************************
3025 * Dock subdriver
3026 */
1da177e4 3027
85998248 3028#ifdef CONFIG_THINKPAD_ACPI_DOCK
56b6aeb0 3029
f74a27d4
HMH
3030static void dock_notify(struct ibm_struct *ibm, u32 event);
3031static int dock_read(char *p);
3032static int dock_write(char *buf);
3033
56b6aeb0
HMH
3034IBM_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
3035 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
3036 "\\_SB.PCI0.PCI1.DOCK", /* all others */
3037 "\\_SB.PCI.ISA.SLCE", /* 570 */
3038 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
3039
5fba344c
HMH
3040/* don't list other alternatives as we install a notify handler on the 570 */
3041IBM_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
3042
1ba90e3a
TR
3043static const struct acpi_device_id ibm_pci_device_ids[] = {
3044 {PCI_ROOT_HID_STRING, 0},
3045 {"", 0},
3046};
3047
d94a7f16
HMH
3048static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
3049 {
3050 .notify = dock_notify,
3051 .handle = &dock_handle,
3052 .type = ACPI_SYSTEM_NOTIFY,
3053 },
3054 {
996fba08
HMH
3055 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
3056 * We just use it to get notifications of dock hotplug
3057 * in very old thinkpads */
1ba90e3a 3058 .hid = ibm_pci_device_ids,
d94a7f16
HMH
3059 .notify = dock_notify,
3060 .handle = &pci_handle,
3061 .type = ACPI_SYSTEM_NOTIFY,
3062 },
3063};
3064
3065static struct ibm_struct dock_driver_data[2] = {
3066 {
3067 .name = "dock",
3068 .read = dock_read,
3069 .write = dock_write,
3070 .acpi = &ibm_dock_acpidriver[0],
3071 },
3072 {
3073 .name = "dock",
3074 .acpi = &ibm_dock_acpidriver[1],
3075 },
3076};
3077
1da177e4
LT
3078#define dock_docked() (_sta(dock_handle) & 1)
3079
a5763f22 3080static int __init dock_init(struct ibm_init_struct *iibm)
5fba344c 3081{
fe08bc4b
HMH
3082 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
3083
8d376cd6 3084 IBM_ACPIHANDLE_INIT(dock);
5fba344c 3085
fe08bc4b
HMH
3086 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
3087 str_supported(dock_handle != NULL));
3088
5fba344c
HMH
3089 return (dock_handle)? 0 : 1;
3090}
3091
d94a7f16
HMH
3092static int __init dock_init2(struct ibm_init_struct *iibm)
3093{
3094 int dock2_needed;
3095
3096 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
3097
3098 if (dock_driver_data[0].flags.acpi_driver_registered &&
3099 dock_driver_data[0].flags.acpi_notify_installed) {
3100 IBM_ACPIHANDLE_INIT(pci);
3101 dock2_needed = (pci_handle != NULL);
3102 vdbg_printk(TPACPI_DBG_INIT,
3103 "dock PCI handler for the TP 570 is %s\n",
3104 str_supported(dock2_needed));
3105 } else {
3106 vdbg_printk(TPACPI_DBG_INIT,
3107 "dock subdriver part 2 not required\n");
3108 dock2_needed = 0;
3109 }
3110
3111 return (dock2_needed)? 0 : 1;
3112}
3113
56b6aeb0
HMH
3114static void dock_notify(struct ibm_struct *ibm, u32 event)
3115{
3116 int docked = dock_docked();
1ba90e3a
TR
3117 int pci = ibm->acpi->hid && ibm->acpi->device &&
3118 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
962ce8ca 3119 int data;
56b6aeb0
HMH
3120
3121 if (event == 1 && !pci) /* 570 */
962ce8ca 3122 data = 1; /* button */
56b6aeb0 3123 else if (event == 1 && pci) /* 570 */
962ce8ca 3124 data = 3; /* dock */
56b6aeb0 3125 else if (event == 3 && docked)
962ce8ca 3126 data = 1; /* button */
56b6aeb0 3127 else if (event == 3 && !docked)
962ce8ca 3128 data = 2; /* undock */
56b6aeb0 3129 else if (event == 0 && docked)
962ce8ca 3130 data = 3; /* dock */
56b6aeb0
HMH
3131 else {
3132 printk(IBM_ERR "unknown dock event %d, status %d\n",
3133 event, _sta(dock_handle));
962ce8ca 3134 data = 0; /* unknown */
56b6aeb0 3135 }
14e04fb3 3136 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
962ce8ca
ZR
3137 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3138 ibm->acpi->device->dev.bus_id,
3139 event, data);
56b6aeb0
HMH
3140}
3141
78f81cc4 3142static int dock_read(char *p)
1da177e4
LT
3143{
3144 int len = 0;
3145 int docked = dock_docked();
3146
3147 if (!dock_handle)
3148 len += sprintf(p + len, "status:\t\tnot supported\n");
3149 else if (!docked)
3150 len += sprintf(p + len, "status:\t\tundocked\n");
3151 else {
3152 len += sprintf(p + len, "status:\t\tdocked\n");
3153 len += sprintf(p + len, "commands:\tdock, undock\n");
3154 }
3155
3156 return len;
3157}
3158
78f81cc4 3159static int dock_write(char *buf)
1da177e4
LT
3160{
3161 char *cmd;
3162
3163 if (!dock_docked())
78f81cc4 3164 return -ENODEV;
1da177e4
LT
3165
3166 while ((cmd = next_cmd(&buf))) {
3167 if (strlencmp(cmd, "undock") == 0) {
78f81cc4
BD
3168 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
3169 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
56b6aeb0
HMH
3170 return -EIO;
3171 } else if (strlencmp(cmd, "dock") == 0) {
3172 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
3173 return -EIO;
3174 } else
3175 return -EINVAL;
1da177e4 3176 }
56b6aeb0
HMH
3177
3178 return 0;
1da177e4 3179}
56b6aeb0 3180
85998248 3181#endif /* CONFIG_THINKPAD_ACPI_DOCK */
56b6aeb0
HMH
3182
3183/*************************************************************************
3184 * Bay subdriver
3185 */
1da177e4 3186
85998248 3187#ifdef CONFIG_THINKPAD_ACPI_BAY
f74a27d4 3188
56b6aeb0
HMH
3189IBM_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
3190 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
3191 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
3192 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
3193 ); /* A21e, R30, R31 */
3194IBM_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
3195 "_EJ0", /* all others */
3196 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
3197IBM_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
3198 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
3199 ); /* all others */
3200IBM_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
3201 "_EJ0", /* 770x */
3202 ); /* all others */
3203
a5763f22 3204static int __init bay_init(struct ibm_init_struct *iibm)
1da177e4 3205{
fe08bc4b
HMH
3206 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
3207
8d376cd6 3208 IBM_ACPIHANDLE_INIT(bay);
5fba344c 3209 if (bay_handle)
8d376cd6
HMH
3210 IBM_ACPIHANDLE_INIT(bay_ej);
3211 IBM_ACPIHANDLE_INIT(bay2);
5fba344c 3212 if (bay2_handle)
8d376cd6 3213 IBM_ACPIHANDLE_INIT(bay2_ej);
5fba344c 3214
d8fd94d9
HMH
3215 tp_features.bay_status = bay_handle &&
3216 acpi_evalf(bay_handle, NULL, "_STA", "qv");
3217 tp_features.bay_status2 = bay2_handle &&
3218 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
78f81cc4 3219
d8fd94d9
HMH
3220 tp_features.bay_eject = bay_handle && bay_ej_handle &&
3221 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
3222 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
3223 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
1da177e4 3224
fe08bc4b
HMH
3225 vdbg_printk(TPACPI_DBG_INIT,
3226 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
d8fd94d9
HMH
3227 str_supported(tp_features.bay_status),
3228 str_supported(tp_features.bay_eject),
3229 str_supported(tp_features.bay_status2),
3230 str_supported(tp_features.bay_eject2));
fe08bc4b 3231
d8fd94d9
HMH
3232 return (tp_features.bay_status || tp_features.bay_eject ||
3233 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
1da177e4
LT
3234}
3235
56b6aeb0
HMH
3236static void bay_notify(struct ibm_struct *ibm, u32 event)
3237{
14e04fb3 3238 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
962ce8ca
ZR
3239 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3240 ibm->acpi->device->dev.bus_id,
3241 event, 0);
56b6aeb0
HMH
3242}
3243
78f81cc4
BD
3244#define bay_occupied(b) (_sta(b##_handle) & 1)
3245
3246static int bay_read(char *p)
1da177e4
LT
3247{
3248 int len = 0;
78f81cc4
BD
3249 int occupied = bay_occupied(bay);
3250 int occupied2 = bay_occupied(bay2);
3251 int eject, eject2;
3252
d8fd94d9
HMH
3253 len += sprintf(p + len, "status:\t\t%s\n",
3254 tp_features.bay_status ?
3255 (occupied ? "occupied" : "unoccupied") :
3256 "not supported");
3257 if (tp_features.bay_status2)
78f81cc4
BD
3258 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
3259 "occupied" : "unoccupied");
3260
d8fd94d9
HMH
3261 eject = tp_features.bay_eject && occupied;
3262 eject2 = tp_features.bay_eject2 && occupied2;
78f81cc4
BD
3263
3264 if (eject && eject2)
3265 len += sprintf(p + len, "commands:\teject, eject2\n");
3266 else if (eject)
1da177e4 3267 len += sprintf(p + len, "commands:\teject\n");
78f81cc4
BD
3268 else if (eject2)
3269 len += sprintf(p + len, "commands:\teject2\n");
1da177e4
LT
3270
3271 return len;
3272}
3273
78f81cc4 3274static int bay_write(char *buf)
1da177e4
LT
3275{
3276 char *cmd;
3277
d8fd94d9 3278 if (!tp_features.bay_eject && !tp_features.bay_eject2)
78f81cc4
BD
3279 return -ENODEV;
3280
1da177e4 3281 while ((cmd = next_cmd(&buf))) {
d8fd94d9 3282 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
78f81cc4
BD
3283 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
3284 return -EIO;
d8fd94d9 3285 } else if (tp_features.bay_eject2 &&
78f81cc4
BD
3286 strlencmp(cmd, "eject2") == 0) {
3287 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
1da177e4
LT
3288 return -EIO;
3289 } else
3290 return -EINVAL;
3291 }
3292
3293 return 0;
78f81cc4 3294}
a5763f22 3295
8d376cd6
HMH
3296static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
3297 .notify = bay_notify,
3298 .handle = &bay_handle,
3299 .type = ACPI_SYSTEM_NOTIFY,
3300};
3301
a5763f22
HMH
3302static struct ibm_struct bay_driver_data = {
3303 .name = "bay",
3304 .read = bay_read,
3305 .write = bay_write,
8d376cd6 3306 .acpi = &ibm_bay_acpidriver,
a5763f22
HMH
3307};
3308
85998248 3309#endif /* CONFIG_THINKPAD_ACPI_BAY */
1da177e4 3310
56b6aeb0
HMH
3311/*************************************************************************
3312 * CMOS subdriver
3313 */
3314
b616004c
HMH
3315/* sysfs cmos_command -------------------------------------------------- */
3316static ssize_t cmos_command_store(struct device *dev,
3317 struct device_attribute *attr,
3318 const char *buf, size_t count)
3319{
3320 unsigned long cmos_cmd;
3321 int res;
3322
3323 if (parse_strtoul(buf, 21, &cmos_cmd))
3324 return -EINVAL;
3325
3326 res = issue_thinkpad_cmos_command(cmos_cmd);
3327 return (res)? res : count;
3328}
3329
3330static struct device_attribute dev_attr_cmos_command =
3331 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
3332
3333/* --------------------------------------------------------------------- */
3334
a5763f22 3335static int __init cmos_init(struct ibm_init_struct *iibm)
5fba344c 3336{
b616004c
HMH
3337 int res;
3338
fe08bc4b
HMH
3339 vdbg_printk(TPACPI_DBG_INIT,
3340 "initializing cmos commands subdriver\n");
3341
8d376cd6 3342 IBM_ACPIHANDLE_INIT(cmos);
5fba344c 3343
fe08bc4b
HMH
3344 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
3345 str_supported(cmos_handle != NULL));
b616004c
HMH
3346
3347 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3348 if (res)
3349 return res;
3350
5fba344c
HMH
3351 return (cmos_handle)? 0 : 1;
3352}
3353
b616004c
HMH
3354static void cmos_exit(void)
3355{
3356 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3357}
3358
78f81cc4 3359static int cmos_read(char *p)
1da177e4
LT
3360{
3361 int len = 0;
3362
78f81cc4
BD
3363 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3364 R30, R31, T20-22, X20-21 */
1da177e4
LT
3365 if (!cmos_handle)
3366 len += sprintf(p + len, "status:\t\tnot supported\n");
3367 else {
3368 len += sprintf(p + len, "status:\t\tsupported\n");
78f81cc4 3369 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
1da177e4
LT
3370 }
3371
3372 return len;
3373}
3374
78f81cc4 3375static int cmos_write(char *buf)
1da177e4
LT
3376{
3377 char *cmd;
c9bea99c 3378 int cmos_cmd, res;
1da177e4
LT
3379
3380 while ((cmd = next_cmd(&buf))) {
78f81cc4
BD
3381 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
3382 cmos_cmd >= 0 && cmos_cmd <= 21) {
1da177e4
LT
3383 /* cmos_cmd set */
3384 } else
3385 return -EINVAL;
3386
c9bea99c
HMH
3387 res = issue_thinkpad_cmos_command(cmos_cmd);
3388 if (res)
3389 return res;
1da177e4
LT
3390 }
3391
3392 return 0;
78f81cc4
BD
3393}
3394
a5763f22
HMH
3395static struct ibm_struct cmos_driver_data = {
3396 .name = "cmos",
3397 .read = cmos_read,
3398 .write = cmos_write,
b616004c 3399 .exit = cmos_exit,
a5763f22 3400};
56b6aeb0
HMH
3401
3402/*************************************************************************
3403 * LED subdriver
3404 */
3405
f74a27d4
HMH
3406enum led_access_mode {
3407 TPACPI_LED_NONE = 0,
3408 TPACPI_LED_570, /* 570 */
3409 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
3410 TPACPI_LED_NEW, /* all others */
3411};
3412
3413enum { /* For TPACPI_LED_OLD */
3414 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
3415 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
3416 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
3417};
3418
9a8e1738 3419static enum led_access_mode led_supported;
78f81cc4 3420
56b6aeb0
HMH
3421IBM_HANDLE(led, ec, "SLED", /* 570 */
3422 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
3423 "LED", /* all others */
3424 ); /* R30, R31 */
3425
a5763f22 3426static int __init led_init(struct ibm_init_struct *iibm)
78f81cc4 3427{
fe08bc4b
HMH
3428 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
3429
8d376cd6 3430 IBM_ACPIHANDLE_INIT(led);
5fba344c 3431
78f81cc4
BD
3432 if (!led_handle)
3433 /* led not supported on R30, R31 */
efa27145 3434 led_supported = TPACPI_LED_NONE;
78f81cc4
BD
3435 else if (strlencmp(led_path, "SLED") == 0)
3436 /* 570 */
efa27145 3437 led_supported = TPACPI_LED_570;
78f81cc4
BD
3438 else if (strlencmp(led_path, "SYSL") == 0)
3439 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
efa27145 3440 led_supported = TPACPI_LED_OLD;
78f81cc4
BD
3441 else
3442 /* all others */
efa27145 3443 led_supported = TPACPI_LED_NEW;
78f81cc4 3444
fe08bc4b
HMH
3445 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
3446 str_supported(led_supported), led_supported);
3447
5fba344c 3448 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
78f81cc4
BD
3449}
3450
3451#define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
3452
3453static int led_read(char *p)
1da177e4
LT
3454{
3455 int len = 0;
3456
78f81cc4
BD
3457 if (!led_supported) {
3458 len += sprintf(p + len, "status:\t\tnot supported\n");
3459 return len;
3460 }
3461 len += sprintf(p + len, "status:\t\tsupported\n");
3462
efa27145 3463 if (led_supported == TPACPI_LED_570) {
78f81cc4
BD
3464 /* 570 */
3465 int i, status;
3466 for (i = 0; i < 8; i++) {
3467 if (!acpi_evalf(ec_handle,
3468 &status, "GLED", "dd", 1 << i))
3469 return -EIO;
3470 len += sprintf(p + len, "%d:\t\t%s\n",
3471 i, led_status(status));
3472 }
3473 }
3474
1da177e4 3475 len += sprintf(p + len, "commands:\t"
78f81cc4 3476 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
1da177e4
LT
3477
3478 return len;
3479}
3480
78f81cc4
BD
3481/* off, on, blink */
3482static const int led_sled_arg1[] = { 0, 1, 3 };
3483static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
3484static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
3485static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
3486
78f81cc4 3487static int led_write(char *buf)
1da177e4
LT
3488{
3489 char *cmd;
78f81cc4
BD
3490 int led, ind, ret;
3491
3492 if (!led_supported)
3493 return -ENODEV;
1da177e4
LT
3494
3495 while ((cmd = next_cmd(&buf))) {
78f81cc4 3496 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
1da177e4
LT
3497 return -EINVAL;
3498
78f81cc4
BD
3499 if (strstr(cmd, "off")) {
3500 ind = 0;
1da177e4 3501 } else if (strstr(cmd, "on")) {
78f81cc4
BD
3502 ind = 1;
3503 } else if (strstr(cmd, "blink")) {
3504 ind = 2;
1da177e4
LT
3505 } else
3506 return -EINVAL;
78f81cc4 3507
efa27145 3508 if (led_supported == TPACPI_LED_570) {
78f81cc4
BD
3509 /* 570 */
3510 led = 1 << led;
1da177e4 3511 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
78f81cc4 3512 led, led_sled_arg1[ind]))
1da177e4 3513 return -EIO;
efa27145 3514 } else if (led_supported == TPACPI_LED_OLD) {
78f81cc4
BD
3515 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
3516 led = 1 << led;
efa27145 3517 ret = ec_write(TPACPI_LED_EC_HLMS, led);
78f81cc4
BD
3518 if (ret >= 0)
3519 ret =
efa27145 3520 ec_write(TPACPI_LED_EC_HLBL,
e062e034 3521 led * led_exp_hlbl[ind]);
78f81cc4
BD
3522 if (ret >= 0)
3523 ret =
efa27145 3524 ec_write(TPACPI_LED_EC_HLCL,
e062e034 3525 led * led_exp_hlcl[ind]);
78f81cc4
BD
3526 if (ret < 0)
3527 return ret;
3528 } else {
3529 /* all others */
3530 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
3531 led, led_led_arg1[ind]))
1da177e4 3532 return -EIO;
78f81cc4
BD
3533 }
3534 }
3535
3536 return 0;
3537}
3538
a5763f22
HMH
3539static struct ibm_struct led_driver_data = {
3540 .name = "led",
3541 .read = led_read,
3542 .write = led_write,
3543};
3544
56b6aeb0
HMH
3545/*************************************************************************
3546 * Beep subdriver
3547 */
3548
3549IBM_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
3550
a5763f22 3551static int __init beep_init(struct ibm_init_struct *iibm)
5fba344c 3552{
fe08bc4b
HMH
3553 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
3554
8d376cd6 3555 IBM_ACPIHANDLE_INIT(beep);
5fba344c 3556
fe08bc4b
HMH
3557 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
3558 str_supported(beep_handle != NULL));
3559
5fba344c
HMH
3560 return (beep_handle)? 0 : 1;
3561}
3562
78f81cc4
BD
3563static int beep_read(char *p)
3564{
3565 int len = 0;
3566
3567 if (!beep_handle)
3568 len += sprintf(p + len, "status:\t\tnot supported\n");
3569 else {
3570 len += sprintf(p + len, "status:\t\tsupported\n");
3571 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
3572 }
3573
3574 return len;
3575}
3576
3577static int beep_write(char *buf)
3578{
3579 char *cmd;
3580 int beep_cmd;
3581
3582 if (!beep_handle)
3583 return -ENODEV;
3584
3585 while ((cmd = next_cmd(&buf))) {
3586 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
3587 beep_cmd >= 0 && beep_cmd <= 17) {
3588 /* beep_cmd set */
3589 } else
3590 return -EINVAL;
3591 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
3592 return -EIO;
3593 }
3594
3595 return 0;
3596}
3597
a5763f22
HMH
3598static struct ibm_struct beep_driver_data = {
3599 .name = "beep",
3600 .read = beep_read,
3601 .write = beep_write,
3602};
3603
56b6aeb0
HMH
3604/*************************************************************************
3605 * Thermal subdriver
3606 */
78f81cc4 3607
f74a27d4
HMH
3608enum thermal_access_mode {
3609 TPACPI_THERMAL_NONE = 0, /* No thermal support */
3610 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
3611 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
3612 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
3613 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
3614};
3615
3616enum { /* TPACPI_THERMAL_TPEC_* */
3617 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
3618 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
3619 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
3620};
3621
3622#define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
3623struct ibm_thermal_sensors_struct {
3624 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
3625};
3626
a26f878a 3627static enum thermal_access_mode thermal_read_mode;
78f81cc4 3628
b21a15f6
HMH
3629/* idx is zero-based */
3630static int thermal_get_sensor(int idx, s32 *value)
3631{
3632 int t;
3633 s8 tmp;
3634 char tmpi[5];
3635
3636 t = TP_EC_THERMAL_TMP0;
3637
3638 switch (thermal_read_mode) {
3639#if TPACPI_MAX_THERMAL_SENSORS >= 16
3640 case TPACPI_THERMAL_TPEC_16:
3641 if (idx >= 8 && idx <= 15) {
3642 t = TP_EC_THERMAL_TMP8;
3643 idx -= 8;
3644 }
3645 /* fallthrough */
3646#endif
3647 case TPACPI_THERMAL_TPEC_8:
3648 if (idx <= 7) {
3649 if (!acpi_ec_read(t + idx, &tmp))
3650 return -EIO;
3651 *value = tmp * 1000;
3652 return 0;
3653 }
3654 break;
3655
3656 case TPACPI_THERMAL_ACPI_UPDT:
3657 if (idx <= 7) {
3658 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
3659 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
3660 return -EIO;
3661 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
3662 return -EIO;
3663 *value = (t - 2732) * 100;
3664 return 0;
3665 }
3666 break;
3667
3668 case TPACPI_THERMAL_ACPI_TMP07:
3669 if (idx <= 7) {
3670 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
3671 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
3672 return -EIO;
3673 if (t > 127 || t < -127)
3674 t = TP_EC_THERMAL_TMP_NA;
3675 *value = t * 1000;
3676 return 0;
3677 }
3678 break;
3679
3680 case TPACPI_THERMAL_NONE:
3681 default:
3682 return -ENOSYS;
3683 }
3684
3685 return -EINVAL;
3686}
3687
3688static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
3689{
3690 int res, i;
3691 int n;
3692
3693 n = 8;
3694 i = 0;
3695
3696 if (!s)
3697 return -EINVAL;
3698
3699 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
3700 n = 16;
3701
3702 for(i = 0 ; i < n; i++) {
3703 res = thermal_get_sensor(i, &s->temp[i]);
3704 if (res)
3705 return res;
3706 }
3707
3708 return n;
3709}
f74a27d4 3710
2c37aa4e
HMH
3711/* sysfs temp##_input -------------------------------------------------- */
3712
3713static ssize_t thermal_temp_input_show(struct device *dev,
3714 struct device_attribute *attr,
3715 char *buf)
3716{
3717 struct sensor_device_attribute *sensor_attr =
3718 to_sensor_dev_attr(attr);
3719 int idx = sensor_attr->index;
3720 s32 value;
3721 int res;
3722
3723 res = thermal_get_sensor(idx, &value);
3724 if (res)
3725 return res;
3726 if (value == TP_EC_THERMAL_TMP_NA * 1000)
3727 return -ENXIO;
3728
3729 return snprintf(buf, PAGE_SIZE, "%d\n", value);
3730}
3731
3732#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
3733 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, thermal_temp_input_show, NULL, _idxB)
3734
3735static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
3736 THERMAL_SENSOR_ATTR_TEMP(1, 0),
3737 THERMAL_SENSOR_ATTR_TEMP(2, 1),
3738 THERMAL_SENSOR_ATTR_TEMP(3, 2),
3739 THERMAL_SENSOR_ATTR_TEMP(4, 3),
3740 THERMAL_SENSOR_ATTR_TEMP(5, 4),
3741 THERMAL_SENSOR_ATTR_TEMP(6, 5),
3742 THERMAL_SENSOR_ATTR_TEMP(7, 6),
3743 THERMAL_SENSOR_ATTR_TEMP(8, 7),
3744 THERMAL_SENSOR_ATTR_TEMP(9, 8),
3745 THERMAL_SENSOR_ATTR_TEMP(10, 9),
3746 THERMAL_SENSOR_ATTR_TEMP(11, 10),
3747 THERMAL_SENSOR_ATTR_TEMP(12, 11),
3748 THERMAL_SENSOR_ATTR_TEMP(13, 12),
3749 THERMAL_SENSOR_ATTR_TEMP(14, 13),
3750 THERMAL_SENSOR_ATTR_TEMP(15, 14),
3751 THERMAL_SENSOR_ATTR_TEMP(16, 15),
3752};
3753
3754#define THERMAL_ATTRS(X) \
3755 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
3756
3757static struct attribute *thermal_temp_input_attr[] = {
3758 THERMAL_ATTRS(8),
3759 THERMAL_ATTRS(9),
3760 THERMAL_ATTRS(10),
3761 THERMAL_ATTRS(11),
3762 THERMAL_ATTRS(12),
3763 THERMAL_ATTRS(13),
3764 THERMAL_ATTRS(14),
3765 THERMAL_ATTRS(15),
3766 THERMAL_ATTRS(0),
3767 THERMAL_ATTRS(1),
3768 THERMAL_ATTRS(2),
3769 THERMAL_ATTRS(3),
3770 THERMAL_ATTRS(4),
3771 THERMAL_ATTRS(5),
3772 THERMAL_ATTRS(6),
3773 THERMAL_ATTRS(7),
3774 NULL
3775};
3776
3777static const struct attribute_group thermal_temp_input16_group = {
3778 .attrs = thermal_temp_input_attr
3779};
3780
3781static const struct attribute_group thermal_temp_input8_group = {
3782 .attrs = &thermal_temp_input_attr[8]
3783};
3784
3785#undef THERMAL_SENSOR_ATTR_TEMP
3786#undef THERMAL_ATTRS
3787
3788/* --------------------------------------------------------------------- */
3789
a5763f22 3790static int __init thermal_init(struct ibm_init_struct *iibm)
78f81cc4 3791{
60eb0b35
HMH
3792 u8 t, ta1, ta2;
3793 int i;
5fba344c 3794 int acpi_tmp7;
2c37aa4e 3795 int res;
5fba344c 3796
fe08bc4b
HMH
3797 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
3798
5fba344c 3799 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
60eb0b35 3800
3d6f99ca 3801 if (thinkpad_id.ec_model) {
60eb0b35
HMH
3802 /*
3803 * Direct EC access mode: sensors at registers
3804 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
3805 * non-implemented, thermal sensors return 0x80 when
3806 * not available
3807 */
78f81cc4 3808
60eb0b35
HMH
3809 ta1 = ta2 = 0;
3810 for (i = 0; i < 8; i++) {
04cc862c 3811 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
60eb0b35
HMH
3812 ta1 |= t;
3813 } else {
3814 ta1 = 0;
3815 break;
3816 }
04cc862c 3817 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
60eb0b35
HMH
3818 ta2 |= t;
3819 } else {
3820 ta1 = 0;
3821 break;
3822 }
3823 }
3824 if (ta1 == 0) {
3825 /* This is sheer paranoia, but we handle it anyway */
3826 if (acpi_tmp7) {
3827 printk(IBM_ERR
3828 "ThinkPad ACPI EC access misbehaving, "
3829 "falling back to ACPI TMPx access mode\n");
efa27145 3830 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
60eb0b35
HMH
3831 } else {
3832 printk(IBM_ERR
3833 "ThinkPad ACPI EC access misbehaving, "
3834 "disabling thermal sensors access\n");
efa27145 3835 thermal_read_mode = TPACPI_THERMAL_NONE;
60eb0b35
HMH
3836 }
3837 } else {
3838 thermal_read_mode =
3839 (ta2 != 0) ?
efa27145 3840 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
60eb0b35
HMH
3841 }
3842 } else if (acpi_tmp7) {
a26f878a
HMH
3843 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
3844 /* 600e/x, 770e, 770x */
efa27145 3845 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
a26f878a
HMH
3846 } else {
3847 /* Standard ACPI TMPx access, max 8 sensors */
efa27145 3848 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
a26f878a
HMH
3849 }
3850 } else {
3851 /* temperatures not supported on 570, G4x, R30, R31, R32 */
efa27145 3852 thermal_read_mode = TPACPI_THERMAL_NONE;
a26f878a 3853 }
78f81cc4 3854
fe08bc4b
HMH
3855 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
3856 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
3857 thermal_read_mode);
3858
2c37aa4e
HMH
3859 switch(thermal_read_mode) {
3860 case TPACPI_THERMAL_TPEC_16:
7fd40029 3861 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
2c37aa4e
HMH
3862 &thermal_temp_input16_group);
3863 if (res)
3864 return res;
3865 break;
3866 case TPACPI_THERMAL_TPEC_8:
3867 case TPACPI_THERMAL_ACPI_TMP07:
3868 case TPACPI_THERMAL_ACPI_UPDT:
7fd40029 3869 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
2c37aa4e
HMH
3870 &thermal_temp_input8_group);
3871 if (res)
3872 return res;
3873 break;
3874 case TPACPI_THERMAL_NONE:
3875 default:
3876 return 1;
3877 }
3878
3879 return 0;
3880}
3881
3882static void thermal_exit(void)
3883{
3884 switch(thermal_read_mode) {
3885 case TPACPI_THERMAL_TPEC_16:
7fd40029 3886 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
2c37aa4e
HMH
3887 &thermal_temp_input16_group);
3888 break;
3889 case TPACPI_THERMAL_TPEC_8:
3890 case TPACPI_THERMAL_ACPI_TMP07:
3891 case TPACPI_THERMAL_ACPI_UPDT:
7fd40029 3892 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
2c37aa4e
HMH
3893 &thermal_temp_input16_group);
3894 break;
3895 case TPACPI_THERMAL_NONE:
3896 default:
3897 break;
3898 }
78f81cc4
BD
3899}
3900
a26f878a
HMH
3901static int thermal_read(char *p)
3902{
3903 int len = 0;
3904 int n, i;
3905 struct ibm_thermal_sensors_struct t;
3906
3907 n = thermal_get_sensors(&t);
3908 if (unlikely(n < 0))
3909 return n;
3910
3911 len += sprintf(p + len, "temperatures:\t");
3912
3913 if (n > 0) {
3914 for (i = 0; i < (n - 1); i++)
3915 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
3916 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
3917 } else
3918 len += sprintf(p + len, "not supported\n");
78f81cc4
BD
3919
3920 return len;
3921}
3922
a5763f22
HMH
3923static struct ibm_struct thermal_driver_data = {
3924 .name = "thermal",
3925 .read = thermal_read,
2c37aa4e 3926 .exit = thermal_exit,
a5763f22
HMH
3927};
3928
56b6aeb0
HMH
3929/*************************************************************************
3930 * EC Dump subdriver
3931 */
3932
78f81cc4
BD
3933static u8 ecdump_regs[256];
3934
3935static int ecdump_read(char *p)
3936{
3937 int len = 0;
3938 int i, j;
3939 u8 v;
3940
3941 len += sprintf(p + len, "EC "
3942 " +00 +01 +02 +03 +04 +05 +06 +07"
3943 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
3944 for (i = 0; i < 256; i += 16) {
3945 len += sprintf(p + len, "EC 0x%02x:", i);
3946 for (j = 0; j < 16; j++) {
3947 if (!acpi_ec_read(i + j, &v))
3948 break;
3949 if (v != ecdump_regs[i + j])
3950 len += sprintf(p + len, " *%02x", v);
3951 else
3952 len += sprintf(p + len, " %02x", v);
3953 ecdump_regs[i + j] = v;
3954 }
3955 len += sprintf(p + len, "\n");
3956 if (j != 16)
3957 break;
3958 }
3959
3960 /* These are way too dangerous to advertise openly... */
3961#if 0
3962 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
3963 " (<offset> is 00-ff, <value> is 00-ff)\n");
3964 len += sprintf(p + len, "commands:\t0x<offset> <value> "
3965 " (<offset> is 00-ff, <value> is 0-255)\n");
3966#endif
3967 return len;
3968}
3969
3970static int ecdump_write(char *buf)
3971{
3972 char *cmd;
3973 int i, v;
3974
3975 while ((cmd = next_cmd(&buf))) {
3976 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
3977 /* i and v set */
3978 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
3979 /* i and v set */
3980 } else
3981 return -EINVAL;
3982 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
3983 if (!acpi_ec_write(i, v))
1da177e4
LT
3984 return -EIO;
3985 } else
3986 return -EINVAL;
3987 }
3988
3989 return 0;
78f81cc4
BD
3990}
3991
a5763f22
HMH
3992static struct ibm_struct ecdump_driver_data = {
3993 .name = "ecdump",
3994 .read = ecdump_read,
3995 .write = ecdump_write,
92641177 3996 .flags.experimental = 1,
a5763f22
HMH
3997};
3998
56b6aeb0
HMH
3999/*************************************************************************
4000 * Backlight/brightness subdriver
4001 */
4002
f74a27d4
HMH
4003#define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
4004
94954cc6 4005static struct backlight_device *ibm_backlight_device;
f74a27d4
HMH
4006static int brightness_offset = 0x31;
4007static int brightness_mode;
4008static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
4009
b21a15f6 4010static struct mutex brightness_mutex;
56b6aeb0 4011
b21a15f6
HMH
4012/*
4013 * ThinkPads can read brightness from two places: EC 0x31, or
4014 * CMOS NVRAM byte 0x5E, bits 0-3.
4015 */
4016static int brightness_get(struct backlight_device *bd)
4017{
4018 u8 lec = 0, lcmos = 0, level = 0;
4019
4020 if (brightness_mode & 1) {
4021 if (!acpi_ec_read(brightness_offset, &lec))
4022 return -EIO;
4023 lec &= (tp_features.bright_16levels)? 0x0f : 0x07;
4024 level = lec;
4025 };
4026 if (brightness_mode & 2) {
4027 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
4028 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
4029 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
4030 lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
4031 level = lcmos;
4032 }
4033
4034 if (brightness_mode == 3 && lec != lcmos) {
4035 printk(IBM_ERR
4036 "CMOS NVRAM (%u) and EC (%u) do not agree "
4037 "on display brightness level\n",
4038 (unsigned int) lcmos,
4039 (unsigned int) lec);
4040 return -EIO;
4041 }
4042
4043 return level;
4044}
4045
4046/* May return EINTR which can always be mapped to ERESTARTSYS */
4047static int brightness_set(int value)
4048{
4049 int cmos_cmd, inc, i, res;
4050 int current_value;
4051
4052 if (value > ((tp_features.bright_16levels)? 15 : 7))
4053 return -EINVAL;
4054
4055 res = mutex_lock_interruptible(&brightness_mutex);
4056 if (res < 0)
4057 return res;
4058
4059 current_value = brightness_get(NULL);
4060 if (current_value < 0) {
4061 res = current_value;
4062 goto errout;
4063 }
4064
4065 cmos_cmd = value > current_value ?
4066 TP_CMOS_BRIGHTNESS_UP :
4067 TP_CMOS_BRIGHTNESS_DOWN;
4068 inc = (value > current_value)? 1 : -1;
4069
4070 res = 0;
4071 for (i = current_value; i != value; i += inc) {
4072 if ((brightness_mode & 2) &&
4073 issue_thinkpad_cmos_command(cmos_cmd)) {
4074 res = -EIO;
4075 goto errout;
4076 }
4077 if ((brightness_mode & 1) &&
4078 !acpi_ec_write(brightness_offset, i + inc)) {
4079 res = -EIO;
4080 goto errout;;
4081 }
4082 }
4083
4084errout:
4085 mutex_unlock(&brightness_mutex);
4086 return res;
4087}
4088
4089/* sysfs backlight class ----------------------------------------------- */
4090
4091static int brightness_update_status(struct backlight_device *bd)
4092{
4093 /* it is the backlight class's job (caller) to handle
4094 * EINTR and other errors properly */
4095 return brightness_set(
4096 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
4097 bd->props.power == FB_BLANK_UNBLANK) ?
4098 bd->props.brightness : 0);
4099}
4100
4101static struct backlight_ops ibm_backlight_data = {
4102 .get_brightness = brightness_get,
56b6aeb0
HMH
4103 .update_status = brightness_update_status,
4104};
4105
b21a15f6 4106/* --------------------------------------------------------------------- */
f432255e 4107
a3f104c0
HMH
4108static int __init tpacpi_query_bcll_levels(acpi_handle handle)
4109{
4110 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
4111 union acpi_object *obj;
4112 int rc;
4113
4114 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
4115 obj = (union acpi_object *)buffer.pointer;
4116 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
4117 printk(IBM_ERR "Unknown BCLL data, "
4118 "please report this to %s\n", IBM_MAIL);
4119 rc = 0;
4120 } else {
4121 rc = obj->package.count;
4122 }
4123 } else {
4124 return 0;
4125 }
4126
4127 kfree(buffer.pointer);
4128 return rc;
4129}
4130
4131static acpi_status __init brightness_find_bcll(acpi_handle handle, u32 lvl,
4132 void *context, void **rv)
4133{
4134 char name[ACPI_PATH_SEGMENT_LENGTH];
4135 struct acpi_buffer buffer = { sizeof(name), &name };
4136
4137 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
4138 !strncmp("BCLL", name, sizeof(name) - 1)) {
4139 if (tpacpi_query_bcll_levels(handle) == 16) {
4140 *rv = handle;
4141 return AE_CTRL_TERMINATE;
4142 } else {
4143 return AE_OK;
4144 }
4145 } else {
4146 return AE_OK;
4147 }
4148}
4149
4150static int __init brightness_check_levels(void)
4151{
4152 int status;
4153 void *found_node = NULL;
4154
4155 if (!vid_handle) {
4156 IBM_ACPIHANDLE_INIT(vid);
4157 }
4158 if (!vid_handle)
4159 return 0;
4160
4161 /* Search for a BCLL package with 16 levels */
4162 status = acpi_walk_namespace(ACPI_TYPE_PACKAGE, vid_handle, 3,
4163 brightness_find_bcll, NULL, &found_node);
4164
4165 return (ACPI_SUCCESS(status) && found_node != NULL);
4166}
4167
e11e211a
HMH
4168static acpi_status __init brightness_find_bcl(acpi_handle handle, u32 lvl,
4169 void *context, void **rv)
4170{
4171 char name[ACPI_PATH_SEGMENT_LENGTH];
4172 struct acpi_buffer buffer = { sizeof(name), &name };
4173
4174 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
4175 !strncmp("_BCL", name, sizeof(name) - 1)) {
4176 *rv = handle;
4177 return AE_CTRL_TERMINATE;
4178 } else {
4179 return AE_OK;
4180 }
4181}
4182
4183static int __init brightness_check_std_acpi_support(void)
4184{
4185 int status;
4186 void *found_node = NULL;
4187
4188 if (!vid_handle) {
4189 IBM_ACPIHANDLE_INIT(vid);
4190 }
4191 if (!vid_handle)
4192 return 0;
4193
4194 /* Search for a _BCL method, but don't execute it */
4195 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
4196 brightness_find_bcl, NULL, &found_node);
4197
4198 return (ACPI_SUCCESS(status) && found_node != NULL);
4199}
4200
a5763f22 4201static int __init brightness_init(struct ibm_init_struct *iibm)
56b6aeb0
HMH
4202{
4203 int b;
4204
fe08bc4b
HMH
4205 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
4206
f432255e
HMH
4207 mutex_init(&brightness_mutex);
4208
87cc537a
HMH
4209 if (!brightness_enable) {
4210 dbg_printk(TPACPI_DBG_INIT,
4211 "brightness support disabled by module parameter\n");
4212 return 1;
e11e211a
HMH
4213 } else if (brightness_enable > 1) {
4214 if (brightness_check_std_acpi_support()) {
4215 printk(IBM_NOTICE
4216 "standard ACPI backlight interface available, not loading native one...\n");
4217 return 1;
4218 }
87cc537a
HMH
4219 }
4220
24d3b774
HMH
4221 if (!brightness_mode) {
4222 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
4223 brightness_mode = 2;
4224 else
4225 brightness_mode = 3;
4226
4227 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
4228 brightness_mode);
4229 }
4230
4231 if (brightness_mode > 3)
4232 return -EINVAL;
4233
a3f104c0
HMH
4234 tp_features.bright_16levels =
4235 thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO &&
4236 brightness_check_levels();
4237
56b6aeb0
HMH
4238 b = brightness_get(NULL);
4239 if (b < 0)
24d3b774 4240 return 1;
56b6aeb0 4241
a3f104c0
HMH
4242 if (tp_features.bright_16levels)
4243 printk(IBM_INFO "detected a 16-level brightness capable ThinkPad\n");
4244
7d5a015e
HMH
4245 ibm_backlight_device = backlight_device_register(
4246 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
4247 &ibm_backlight_data);
56b6aeb0
HMH
4248 if (IS_ERR(ibm_backlight_device)) {
4249 printk(IBM_ERR "Could not register backlight device\n");
4250 return PTR_ERR(ibm_backlight_device);
4251 }
fe08bc4b 4252 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
56b6aeb0 4253
a3f104c0
HMH
4254 ibm_backlight_device->props.max_brightness =
4255 (tp_features.bright_16levels)? 15 : 7;
56b6aeb0
HMH
4256 ibm_backlight_device->props.brightness = b;
4257 backlight_update_status(ibm_backlight_device);
4258
4259 return 0;
4260}
4261
4262static void brightness_exit(void)
4263{
4264 if (ibm_backlight_device) {
fe08bc4b
HMH
4265 vdbg_printk(TPACPI_DBG_EXIT,
4266 "calling backlight_device_unregister()\n");
56b6aeb0
HMH
4267 backlight_device_unregister(ibm_backlight_device);
4268 ibm_backlight_device = NULL;
4269 }
4270}
4271
56b6aeb0
HMH
4272static int brightness_read(char *p)
4273{
4274 int len = 0;
4275 int level;
4276
4277 if ((level = brightness_get(NULL)) < 0) {
4278 len += sprintf(p + len, "level:\t\tunreadable\n");
4279 } else {
a3f104c0 4280 len += sprintf(p + len, "level:\t\t%d\n", level);
56b6aeb0
HMH
4281 len += sprintf(p + len, "commands:\tup, down\n");
4282 len += sprintf(p + len, "commands:\tlevel <level>"
a3f104c0
HMH
4283 " (<level> is 0-%d)\n",
4284 (tp_features.bright_16levels) ? 15 : 7);
56b6aeb0
HMH
4285 }
4286
4287 return len;
4288}
4289
8acb0250
HM
4290static int brightness_write(char *buf)
4291{
4292 int level;
4273af8d 4293 int rc;
1da177e4 4294 char *cmd;
a3f104c0 4295 int max_level = (tp_features.bright_16levels) ? 15 : 7;
1da177e4 4296
4273af8d
HMH
4297 level = brightness_get(NULL);
4298 if (level < 0)
4299 return level;
78f81cc4 4300
4273af8d 4301 while ((cmd = next_cmd(&buf))) {
78f81cc4 4302 if (strlencmp(cmd, "up") == 0) {
4273af8d
HMH
4303 if (level < max_level)
4304 level++;
78f81cc4 4305 } else if (strlencmp(cmd, "down") == 0) {
4273af8d
HMH
4306 if (level > 0)
4307 level--;
4308 } else if (sscanf(cmd, "level %d", &level) == 1 &&
4309 level >= 0 && level <= max_level) {
4310 /* new level set */
78f81cc4
BD
4311 } else
4312 return -EINVAL;
78f81cc4
BD
4313 }
4314
4273af8d
HMH
4315 /*
4316 * Now we know what the final level should be, so we try to set it.
4317 * Doing it this way makes the syscall restartable in case of EINTR
4318 */
4319 rc = brightness_set(level);
4320 return (rc == -EINTR)? ERESTARTSYS : rc;
78f81cc4
BD
4321}
4322
a5763f22
HMH
4323static struct ibm_struct brightness_driver_data = {
4324 .name = "brightness",
4325 .read = brightness_read,
4326 .write = brightness_write,
4327 .exit = brightness_exit,
4328};
4329
56b6aeb0
HMH
4330/*************************************************************************
4331 * Volume subdriver
4332 */
fb87a811 4333
f74a27d4
HMH
4334static int volume_offset = 0x30;
4335
78f81cc4
BD
4336static int volume_read(char *p)
4337{
4338 int len = 0;
4339 u8 level;
4340
4341 if (!acpi_ec_read(volume_offset, &level)) {
4342 len += sprintf(p + len, "level:\t\tunreadable\n");
4343 } else {
4344 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
4345 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
4346 len += sprintf(p + len, "commands:\tup, down, mute\n");
4347 len += sprintf(p + len, "commands:\tlevel <level>"
4348 " (<level> is 0-15)\n");
4349 }
4350
4351 return len;
4352}
4353
78f81cc4
BD
4354static int volume_write(char *buf)
4355{
4356 int cmos_cmd, inc, i;
4357 u8 level, mute;
4358 int new_level, new_mute;
4359 char *cmd;
4360
4361 while ((cmd = next_cmd(&buf))) {
4362 if (!acpi_ec_read(volume_offset, &level))
4363 return -EIO;
4364 new_mute = mute = level & 0x40;
4365 new_level = level = level & 0xf;
4366
4367 if (strlencmp(cmd, "up") == 0) {
4368 if (mute)
4369 new_mute = 0;
4370 else
4371 new_level = level == 15 ? 15 : level + 1;
4372 } else if (strlencmp(cmd, "down") == 0) {
4373 if (mute)
4374 new_mute = 0;
4375 else
4376 new_level = level == 0 ? 0 : level - 1;
4377 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
4378 new_level >= 0 && new_level <= 15) {
4379 /* new_level set */
4380 } else if (strlencmp(cmd, "mute") == 0) {
4381 new_mute = 0x40;
1da177e4
LT
4382 } else
4383 return -EINVAL;
4384
78f81cc4 4385 if (new_level != level) { /* mute doesn't change */
56b6aeb0 4386 cmos_cmd = new_level > level ? TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
78f81cc4
BD
4387 inc = new_level > level ? 1 : -1;
4388
c9bea99c 4389 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
78f81cc4
BD
4390 !acpi_ec_write(volume_offset, level)))
4391 return -EIO;
4392
4393 for (i = level; i != new_level; i += inc)
c9bea99c 4394 if (issue_thinkpad_cmos_command(cmos_cmd) ||
78f81cc4
BD
4395 !acpi_ec_write(volume_offset, i + inc))
4396 return -EIO;
4397
c9bea99c 4398 if (mute && (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
78f81cc4
BD
4399 !acpi_ec_write(volume_offset,
4400 new_level + mute)))
4401 return -EIO;
4402 }
4403
4404 if (new_mute != mute) { /* level doesn't change */
56b6aeb0 4405 cmos_cmd = new_mute ? TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
78f81cc4 4406
c9bea99c 4407 if (issue_thinkpad_cmos_command(cmos_cmd) ||
78f81cc4
BD
4408 !acpi_ec_write(volume_offset, level + new_mute))
4409 return -EIO;
4410 }
4411 }
4412
4413 return 0;
4414}
4415
a5763f22
HMH
4416static struct ibm_struct volume_driver_data = {
4417 .name = "volume",
4418 .read = volume_read,
4419 .write = volume_write,
4420};
56b6aeb0
HMH
4421
4422/*************************************************************************
4423 * Fan subdriver
4424 */
4425
4426/*
4427 * FAN ACCESS MODES
4428 *
efa27145 4429 * TPACPI_FAN_RD_ACPI_GFAN:
56b6aeb0
HMH
4430 * ACPI GFAN method: returns fan level
4431 *
efa27145 4432 * see TPACPI_FAN_WR_ACPI_SFAN
f51d1a39 4433 * EC 0x2f (HFSP) not available if GFAN exists
56b6aeb0 4434 *
efa27145 4435 * TPACPI_FAN_WR_ACPI_SFAN:
56b6aeb0
HMH
4436 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
4437 *
f51d1a39
HMH
4438 * EC 0x2f (HFSP) might be available *for reading*, but do not use
4439 * it for writing.
56b6aeb0 4440 *
efa27145 4441 * TPACPI_FAN_WR_TPEC:
f51d1a39
HMH
4442 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
4443 * Supported on almost all ThinkPads
56b6aeb0
HMH
4444 *
4445 * Fan speed changes of any sort (including those caused by the
4446 * disengaged mode) are usually done slowly by the firmware as the
4447 * maximum ammount of fan duty cycle change per second seems to be
4448 * limited.
4449 *
4450 * Reading is not available if GFAN exists.
4451 * Writing is not available if SFAN exists.
4452 *
4453 * Bits
4454 * 7 automatic mode engaged;
4455 * (default operation mode of the ThinkPad)
4456 * fan level is ignored in this mode.
f51d1a39 4457 * 6 full speed mode (takes precedence over bit 7);
56b6aeb0 4458 * not available on all thinkpads. May disable
f51d1a39
HMH
4459 * the tachometer while the fan controller ramps up
4460 * the speed (which can take up to a few *minutes*).
4461 * Speeds up fan to 100% duty-cycle, which is far above
4462 * the standard RPM levels. It is not impossible that
4463 * it could cause hardware damage.
56b6aeb0
HMH
4464 * 5-3 unused in some models. Extra bits for fan level
4465 * in others, but still useless as all values above
4466 * 7 map to the same speed as level 7 in these models.
4467 * 2-0 fan level (0..7 usually)
4468 * 0x00 = stop
4469 * 0x07 = max (set when temperatures critical)
4470 * Some ThinkPads may have other levels, see
efa27145 4471 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
56b6aeb0
HMH
4472 *
4473 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
4474 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
4475 * does so, its initial value is meaningless (0x07).
4476 *
4477 * For firmware bugs, refer to:
4478 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
4479 *
4480 * ----
4481 *
4482 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
4483 * Main fan tachometer reading (in RPM)
4484 *
4485 * This register is present on all ThinkPads with a new-style EC, and
4486 * it is known not to be present on the A21m/e, and T22, as there is
4487 * something else in offset 0x84 according to the ACPI DSDT. Other
4488 * ThinkPads from this same time period (and earlier) probably lack the
4489 * tachometer as well.
4490 *
4491 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
4492 * was never fixed by IBM to report the EC firmware version string
4493 * probably support the tachometer (like the early X models), so
4494 * detecting it is quite hard. We need more data to know for sure.
4495 *
4496 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
4497 * might result.
4498 *
f51d1a39
HMH
4499 * FIRMWARE BUG: may go stale while the EC is switching to full speed
4500 * mode.
56b6aeb0
HMH
4501 *
4502 * For firmware bugs, refer to:
4503 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
4504 *
efa27145 4505 * TPACPI_FAN_WR_ACPI_FANS:
56b6aeb0
HMH
4506 * ThinkPad X31, X40, X41. Not available in the X60.
4507 *
4508 * FANS ACPI handle: takes three arguments: low speed, medium speed,
4509 * high speed. ACPI DSDT seems to map these three speeds to levels
4510 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
4511 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
4512 *
4513 * The speeds are stored on handles
4514 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
4515 *
4516 * There are three default speed sets, acessible as handles:
4517 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
4518 *
4519 * ACPI DSDT switches which set is in use depending on various
4520 * factors.
4521 *
efa27145 4522 * TPACPI_FAN_WR_TPEC is also available and should be used to
56b6aeb0
HMH
4523 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
4524 * but the ACPI tables just mention level 7.
4525 */
4526
f74a27d4
HMH
4527enum { /* Fan control constants */
4528 fan_status_offset = 0x2f, /* EC register 0x2f */
4529 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
4530 * 0x84 must be read before 0x85 */
4531
4532 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
4533 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
4534
4535 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
4536};
4537
4538enum fan_status_access_mode {
4539 TPACPI_FAN_NONE = 0, /* No fan status or control */
4540 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
4541 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
4542};
4543
4544enum fan_control_access_mode {
4545 TPACPI_FAN_WR_NONE = 0, /* No fan control */
4546 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
4547 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
4548 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
4549};
4550
4551enum fan_control_commands {
4552 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
4553 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
4554 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
4555 * and also watchdog cmd */
4556};
4557
4558static int fan_control_allowed;
4559
69ba91cb
HMH
4560static enum fan_status_access_mode fan_status_access_mode;
4561static enum fan_control_access_mode fan_control_access_mode;
4562static enum fan_control_commands fan_control_commands;
78f81cc4 4563
778b4d74 4564static u8 fan_control_initial_status;
fe98a52c 4565static u8 fan_control_desired_level;
f74a27d4
HMH
4566static int fan_watchdog_maxinterval;
4567
4568static struct mutex fan_mutex;
778b4d74 4569
25c68a33 4570static void fan_watchdog_fire(struct work_struct *ignored);
25c68a33 4571static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
16663a87 4572
56b6aeb0
HMH
4573IBM_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
4574IBM_HANDLE(gfan, ec, "GFAN", /* 570 */
4575 "\\FSPD", /* 600e/x, 770e, 770x */
4576 ); /* all others */
4577IBM_HANDLE(sfan, ec, "SFAN", /* 570 */
4578 "JFNS", /* 770x-JL */
4579 ); /* all others */
4580
fe98a52c 4581/*
b21a15f6 4582 * Call with fan_mutex held
fe98a52c 4583 */
b21a15f6 4584static void fan_update_desired_level(u8 status)
fe98a52c 4585{
b21a15f6
HMH
4586 if ((status &
4587 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
4588 if (status > 7)
4589 fan_control_desired_level = 7;
4590 else
4591 fan_control_desired_level = status;
4592 }
4593}
fe98a52c 4594
b21a15f6
HMH
4595static int fan_get_status(u8 *status)
4596{
4597 u8 s;
fe98a52c 4598
b21a15f6
HMH
4599 /* TODO:
4600 * Add TPACPI_FAN_RD_ACPI_FANS ? */
fe98a52c 4601
b21a15f6
HMH
4602 switch (fan_status_access_mode) {
4603 case TPACPI_FAN_RD_ACPI_GFAN:
4604 /* 570, 600e/x, 770e, 770x */
fe98a52c 4605
b21a15f6
HMH
4606 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
4607 return -EIO;
fe98a52c 4608
b21a15f6
HMH
4609 if (likely(status))
4610 *status = s & 0x07;
fe98a52c 4611
b21a15f6
HMH
4612 break;
4613
4614 case TPACPI_FAN_RD_TPEC:
4615 /* all except 570, 600e/x, 770e, 770x */
4616 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
4617 return -EIO;
4618
4619 if (likely(status))
4620 *status = s;
fe98a52c 4621
fe98a52c 4622 break;
b21a15f6 4623
fe98a52c 4624 default:
b21a15f6 4625 return -ENXIO;
fe98a52c
HMH
4626 }
4627
b21a15f6
HMH
4628 return 0;
4629}
fe98a52c 4630
b21a15f6
HMH
4631static int fan_get_status_safe(u8 *status)
4632{
4633 int rc;
4634 u8 s;
fe98a52c 4635
b21a15f6
HMH
4636 if (mutex_lock_interruptible(&fan_mutex))
4637 return -ERESTARTSYS;
4638 rc = fan_get_status(&s);
4639 if (!rc)
4640 fan_update_desired_level(s);
4641 mutex_unlock(&fan_mutex);
fe98a52c 4642
b21a15f6
HMH
4643 if (status)
4644 *status = s;
fe98a52c 4645
b21a15f6
HMH
4646 return rc;
4647}
4648
4649static int fan_get_speed(unsigned int *speed)
fe98a52c 4650{
b21a15f6 4651 u8 hi, lo;
fe98a52c 4652
b21a15f6
HMH
4653 switch (fan_status_access_mode) {
4654 case TPACPI_FAN_RD_TPEC:
4655 /* all except 570, 600e/x, 770e, 770x */
4656 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
4657 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
4658 return -EIO;
fe98a52c 4659
b21a15f6
HMH
4660 if (likely(speed))
4661 *speed = (hi << 8) | lo;
fe98a52c 4662
b21a15f6 4663 break;
fe98a52c 4664
b21a15f6
HMH
4665 default:
4666 return -ENXIO;
4667 }
fe98a52c 4668
b21a15f6 4669 return 0;
fe98a52c
HMH
4670}
4671
b21a15f6 4672static int fan_set_level(int level)
fe98a52c 4673{
b21a15f6
HMH
4674 if (!fan_control_allowed)
4675 return -EPERM;
fe98a52c 4676
b21a15f6
HMH
4677 switch (fan_control_access_mode) {
4678 case TPACPI_FAN_WR_ACPI_SFAN:
4679 if (level >= 0 && level <= 7) {
4680 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
4681 return -EIO;
4682 } else
4683 return -EINVAL;
4684 break;
fe98a52c 4685
b21a15f6
HMH
4686 case TPACPI_FAN_WR_ACPI_FANS:
4687 case TPACPI_FAN_WR_TPEC:
4688 if ((level != TP_EC_FAN_AUTO) &&
4689 (level != TP_EC_FAN_FULLSPEED) &&
4690 ((level < 0) || (level > 7)))
4691 return -EINVAL;
fe98a52c 4692
b21a15f6
HMH
4693 /* safety net should the EC not support AUTO
4694 * or FULLSPEED mode bits and just ignore them */
4695 if (level & TP_EC_FAN_FULLSPEED)
4696 level |= 7; /* safety min speed 7 */
4697 else if (level & TP_EC_FAN_FULLSPEED)
4698 level |= 4; /* safety min speed 4 */
fe98a52c 4699
b21a15f6
HMH
4700 if (!acpi_ec_write(fan_status_offset, level))
4701 return -EIO;
4702 else
4703 tp_features.fan_ctrl_status_undef = 0;
4704 break;
fe98a52c 4705
b21a15f6
HMH
4706 default:
4707 return -ENXIO;
4708 }
4709 return 0;
fe98a52c
HMH
4710}
4711
b21a15f6 4712static int fan_set_level_safe(int level)
fe98a52c 4713{
b21a15f6 4714 int rc;
fe98a52c 4715
b21a15f6
HMH
4716 if (!fan_control_allowed)
4717 return -EPERM;
fe98a52c 4718
b21a15f6
HMH
4719 if (mutex_lock_interruptible(&fan_mutex))
4720 return -ERESTARTSYS;
fe98a52c 4721
b21a15f6
HMH
4722 if (level == TPACPI_FAN_LAST_LEVEL)
4723 level = fan_control_desired_level;
fe98a52c 4724
b21a15f6
HMH
4725 rc = fan_set_level(level);
4726 if (!rc)
4727 fan_update_desired_level(level);
4728
4729 mutex_unlock(&fan_mutex);
4730 return rc;
fe98a52c
HMH
4731}
4732
b21a15f6 4733static int fan_set_enable(void)
fe98a52c 4734{
b21a15f6
HMH
4735 u8 s;
4736 int rc;
fe98a52c 4737
ecf2a80a
HMH
4738 if (!fan_control_allowed)
4739 return -EPERM;
4740
b21a15f6
HMH
4741 if (mutex_lock_interruptible(&fan_mutex))
4742 return -ERESTARTSYS;
fe98a52c 4743
b21a15f6
HMH
4744 switch (fan_control_access_mode) {
4745 case TPACPI_FAN_WR_ACPI_FANS:
4746 case TPACPI_FAN_WR_TPEC:
4747 rc = fan_get_status(&s);
4748 if (rc < 0)
4749 break;
fe98a52c 4750
b21a15f6
HMH
4751 /* Don't go out of emergency fan mode */
4752 if (s != 7) {
4753 s &= 0x07;
4754 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
4755 }
fe98a52c 4756
b21a15f6
HMH
4757 if (!acpi_ec_write(fan_status_offset, s))
4758 rc = -EIO;
4759 else {
4760 tp_features.fan_ctrl_status_undef = 0;
4761 rc = 0;
4762 }
4763 break;
fe98a52c 4764
b21a15f6
HMH
4765 case TPACPI_FAN_WR_ACPI_SFAN:
4766 rc = fan_get_status(&s);
4767 if (rc < 0)
4768 break;
fe98a52c 4769
b21a15f6 4770 s &= 0x07;
fe98a52c 4771
b21a15f6
HMH
4772 /* Set fan to at least level 4 */
4773 s |= 4;
fe08bc4b 4774
b21a15f6
HMH
4775 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
4776 rc= -EIO;
4777 else
4778 rc = 0;
4779 break;
78f81cc4 4780
b21a15f6
HMH
4781 default:
4782 rc = -ENXIO;
4783 }
5fba344c 4784
b21a15f6
HMH
4785 mutex_unlock(&fan_mutex);
4786 return rc;
69ba91cb 4787}
78f81cc4 4788
b21a15f6 4789static int fan_set_disable(void)
78f81cc4 4790{
b21a15f6 4791 int rc;
c52f0aa5 4792
b21a15f6
HMH
4793 if (!fan_control_allowed)
4794 return -EPERM;
78f81cc4 4795
b21a15f6
HMH
4796 if (mutex_lock_interruptible(&fan_mutex))
4797 return -ERESTARTSYS;
3ef8a609 4798
b21a15f6
HMH
4799 rc = 0;
4800 switch (fan_control_access_mode) {
4801 case TPACPI_FAN_WR_ACPI_FANS:
4802 case TPACPI_FAN_WR_TPEC:
4803 if (!acpi_ec_write(fan_status_offset, 0x00))
4804 rc = -EIO;
4805 else {
4806 fan_control_desired_level = 0;
4807 tp_features.fan_ctrl_status_undef = 0;
4808 }
3ef8a609
HMH
4809 break;
4810
b21a15f6
HMH
4811 case TPACPI_FAN_WR_ACPI_SFAN:
4812 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
4813 rc = -EIO;
4814 else
4815 fan_control_desired_level = 0;
c52f0aa5
HMH
4816 break;
4817
4818 default:
b21a15f6 4819 rc = -ENXIO;
78f81cc4
BD
4820 }
4821
fe98a52c 4822
fe98a52c 4823 mutex_unlock(&fan_mutex);
fe98a52c
HMH
4824 return rc;
4825}
4826
b21a15f6 4827static int fan_set_speed(int speed)
c52f0aa5 4828{
b21a15f6 4829 int rc;
c52f0aa5 4830
b21a15f6
HMH
4831 if (!fan_control_allowed)
4832 return -EPERM;
3ef8a609 4833
b21a15f6
HMH
4834 if (mutex_lock_interruptible(&fan_mutex))
4835 return -ERESTARTSYS;
c52f0aa5 4836
b21a15f6
HMH
4837 rc = 0;
4838 switch (fan_control_access_mode) {
4839 case TPACPI_FAN_WR_ACPI_FANS:
4840 if (speed >= 0 && speed <= 65535) {
4841 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
4842 speed, speed, speed))
4843 rc = -EIO;
4844 } else
4845 rc = -EINVAL;
c52f0aa5
HMH
4846 break;
4847
4848 default:
b21a15f6 4849 rc = -ENXIO;
c52f0aa5
HMH
4850 }
4851
b21a15f6
HMH
4852 mutex_unlock(&fan_mutex);
4853 return rc;
16663a87
HMH
4854}
4855
4856static void fan_watchdog_reset(void)
4857{
94954cc6 4858 static int fan_watchdog_active;
16663a87 4859
4985cd0a
HMH
4860 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
4861 return;
4862
16663a87
HMH
4863 if (fan_watchdog_active)
4864 cancel_delayed_work(&fan_watchdog_task);
4865
8fef502e
HMH
4866 if (fan_watchdog_maxinterval > 0 &&
4867 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
16663a87
HMH
4868 fan_watchdog_active = 1;
4869 if (!schedule_delayed_work(&fan_watchdog_task,
4870 msecs_to_jiffies(fan_watchdog_maxinterval
4871 * 1000))) {
4872 printk(IBM_ERR "failed to schedule the fan watchdog, "
4873 "watchdog will not trigger\n");
4874 }
4875 } else
4876 fan_watchdog_active = 0;
4877}
4878
b21a15f6 4879static void fan_watchdog_fire(struct work_struct *ignored)
78f81cc4 4880{
b21a15f6 4881 int rc;
18ad7996 4882
b21a15f6
HMH
4883 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
4884 return;
a12095c2 4885
b21a15f6
HMH
4886 printk(IBM_NOTICE "fan watchdog: enabling fan\n");
4887 rc = fan_set_enable();
4888 if (rc < 0) {
4889 printk(IBM_ERR "fan watchdog: error %d while enabling fan, "
4890 "will try again later...\n", -rc);
4891 /* reschedule for later */
4892 fan_watchdog_reset();
4893 }
4894}
eaa7571b 4895
b21a15f6
HMH
4896/*
4897 * SYSFS fan layout: hwmon compatible (device)
4898 *
4899 * pwm*_enable:
4900 * 0: "disengaged" mode
4901 * 1: manual mode
4902 * 2: native EC "auto" mode (recommended, hardware default)
4903 *
4904 * pwm*: set speed in manual mode, ignored otherwise.
4905 * 0 is level 0; 255 is level 7. Intermediate points done with linear
4906 * interpolation.
4907 *
4908 * fan*_input: tachometer reading, RPM
4909 *
4910 *
4911 * SYSFS fan layout: extensions
4912 *
4913 * fan_watchdog (driver):
4914 * fan watchdog interval in seconds, 0 disables (default), max 120
4915 */
4916
4917/* sysfs fan pwm1_enable ----------------------------------------------- */
4918static ssize_t fan_pwm1_enable_show(struct device *dev,
4919 struct device_attribute *attr,
4920 char *buf)
4921{
4922 int res, mode;
4923 u8 status;
4924
4925 res = fan_get_status_safe(&status);
4926 if (res)
4927 return res;
4928
4929 if (unlikely(tp_features.fan_ctrl_status_undef)) {
4930 if (status != fan_control_initial_status) {
d8fd94d9 4931 tp_features.fan_ctrl_status_undef = 0;
b21a15f6
HMH
4932 } else {
4933 /* Return most likely status. In fact, it
4934 * might be the only possible status */
4935 status = TP_EC_FAN_AUTO;
4936 }
4937 }
4938
4939 if (status & TP_EC_FAN_FULLSPEED) {
4940 mode = 0;
4941 } else if (status & TP_EC_FAN_AUTO) {
4942 mode = 2;
4943 } else
4944 mode = 1;
4945
4946 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
4947}
a12095c2 4948
b21a15f6
HMH
4949static ssize_t fan_pwm1_enable_store(struct device *dev,
4950 struct device_attribute *attr,
4951 const char *buf, size_t count)
4952{
4953 unsigned long t;
4954 int res, level;
4955
4956 if (parse_strtoul(buf, 2, &t))
4957 return -EINVAL;
4958
4959 switch (t) {
4960 case 0:
4961 level = TP_EC_FAN_FULLSPEED;
4962 break;
4963 case 1:
4964 level = TPACPI_FAN_LAST_LEVEL;
4965 break;
4966 case 2:
4967 level = TP_EC_FAN_AUTO;
4968 break;
4969 case 3:
4970 /* reserved for software-controlled auto mode */
4971 return -ENOSYS;
18ad7996 4972 default:
b21a15f6 4973 return -EINVAL;
18ad7996 4974 }
b21a15f6
HMH
4975
4976 res = fan_set_level_safe(level);
4977 if (res == -ENXIO)
4978 return -EINVAL;
4979 else if (res < 0)
4980 return res;
4981
4982 fan_watchdog_reset();
4983
4984 return count;
18ad7996
HMH
4985}
4986
b21a15f6
HMH
4987static struct device_attribute dev_attr_fan_pwm1_enable =
4988 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
4989 fan_pwm1_enable_show, fan_pwm1_enable_store);
4990
4991/* sysfs fan pwm1 ------------------------------------------------------ */
4992static ssize_t fan_pwm1_show(struct device *dev,
4993 struct device_attribute *attr,
4994 char *buf)
fe98a52c 4995{
b21a15f6
HMH
4996 int res;
4997 u8 status;
fe98a52c 4998
b21a15f6
HMH
4999 res = fan_get_status_safe(&status);
5000 if (res)
5001 return res;
ecf2a80a 5002
b21a15f6
HMH
5003 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5004 if (status != fan_control_initial_status) {
5005 tp_features.fan_ctrl_status_undef = 0;
5006 } else {
5007 status = TP_EC_FAN_AUTO;
5008 }
5009 }
fe98a52c 5010
b21a15f6
HMH
5011 if ((status &
5012 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
5013 status = fan_control_desired_level;
fe98a52c 5014
b21a15f6
HMH
5015 if (status > 7)
5016 status = 7;
fe98a52c 5017
b21a15f6 5018 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
fe98a52c
HMH
5019}
5020
b21a15f6
HMH
5021static ssize_t fan_pwm1_store(struct device *dev,
5022 struct device_attribute *attr,
5023 const char *buf, size_t count)
18ad7996 5024{
b21a15f6 5025 unsigned long s;
1c6a334e 5026 int rc;
b21a15f6 5027 u8 status, newlevel;
1c6a334e 5028
b21a15f6
HMH
5029 if (parse_strtoul(buf, 255, &s))
5030 return -EINVAL;
5031
5032 /* scale down from 0-255 to 0-7 */
5033 newlevel = (s >> 5) & 0x07;
ecf2a80a 5034
fc589a3c
HMH
5035 if (mutex_lock_interruptible(&fan_mutex))
5036 return -ERESTARTSYS;
40ca9fdf 5037
b21a15f6
HMH
5038 rc = fan_get_status(&status);
5039 if (!rc && (status &
5040 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
5041 rc = fan_set_level(newlevel);
5042 if (rc == -ENXIO)
5043 rc = -EINVAL;
5044 else if (!rc) {
5045 fan_update_desired_level(newlevel);
5046 fan_watchdog_reset();
eaa7571b 5047 }
b21a15f6 5048 }
1c6a334e 5049
b21a15f6
HMH
5050 mutex_unlock(&fan_mutex);
5051 return (rc)? rc : count;
5052}
1c6a334e 5053
b21a15f6
HMH
5054static struct device_attribute dev_attr_fan_pwm1 =
5055 __ATTR(pwm1, S_IWUSR | S_IRUGO,
5056 fan_pwm1_show, fan_pwm1_store);
1c6a334e 5057
b21a15f6
HMH
5058/* sysfs fan fan1_input ------------------------------------------------ */
5059static ssize_t fan_fan1_input_show(struct device *dev,
5060 struct device_attribute *attr,
5061 char *buf)
5062{
5063 int res;
5064 unsigned int speed;
1c6a334e 5065
b21a15f6
HMH
5066 res = fan_get_speed(&speed);
5067 if (res < 0)
5068 return res;
1c6a334e 5069
b21a15f6
HMH
5070 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
5071}
18ad7996 5072
b21a15f6
HMH
5073static struct device_attribute dev_attr_fan_fan1_input =
5074 __ATTR(fan1_input, S_IRUGO,
5075 fan_fan1_input_show, NULL);
40ca9fdf 5076
b21a15f6
HMH
5077/* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
5078static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
5079 char *buf)
5080{
5081 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
18ad7996
HMH
5082}
5083
b21a15f6
HMH
5084static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
5085 const char *buf, size_t count)
18ad7996 5086{
b21a15f6
HMH
5087 unsigned long t;
5088
5089 if (parse_strtoul(buf, 120, &t))
5090 return -EINVAL;
40ca9fdf 5091
ecf2a80a
HMH
5092 if (!fan_control_allowed)
5093 return -EPERM;
5094
b21a15f6
HMH
5095 fan_watchdog_maxinterval = t;
5096 fan_watchdog_reset();
40ca9fdf 5097
b21a15f6
HMH
5098 return count;
5099}
5100
5101static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
5102 fan_fan_watchdog_show, fan_fan_watchdog_store);
5103
5104/* --------------------------------------------------------------------- */
5105static struct attribute *fan_attributes[] = {
5106 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
5107 &dev_attr_fan_fan1_input.attr,
5108 NULL
5109};
5110
5111static const struct attribute_group fan_attr_group = {
5112 .attrs = fan_attributes,
5113};
5114
5115static int __init fan_init(struct ibm_init_struct *iibm)
5116{
5117 int rc;
5118
5119 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
5120
5121 mutex_init(&fan_mutex);
5122 fan_status_access_mode = TPACPI_FAN_NONE;
5123 fan_control_access_mode = TPACPI_FAN_WR_NONE;
5124 fan_control_commands = 0;
5125 fan_watchdog_maxinterval = 0;
5126 tp_features.fan_ctrl_status_undef = 0;
5127 fan_control_desired_level = 7;
5128
5129 IBM_ACPIHANDLE_INIT(fans);
5130 IBM_ACPIHANDLE_INIT(gfan);
5131 IBM_ACPIHANDLE_INIT(sfan);
5132
5133 if (gfan_handle) {
5134 /* 570, 600e/x, 770e, 770x */
5135 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
5136 } else {
5137 /* all other ThinkPads: note that even old-style
5138 * ThinkPad ECs supports the fan control register */
5139 if (likely(acpi_ec_read(fan_status_offset,
5140 &fan_control_initial_status))) {
5141 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
5142
5143 /* In some ThinkPads, neither the EC nor the ACPI
5144 * DSDT initialize the fan status, and it ends up
5145 * being set to 0x07 when it *could* be either
5146 * 0x07 or 0x80.
5147 *
5148 * Enable for TP-1Y (T43), TP-78 (R51e),
5149 * TP-76 (R52), TP-70 (T43, R52), which are known
5150 * to be buggy. */
5151 if (fan_control_initial_status == 0x07) {
5152 switch (thinkpad_id.ec_model) {
5153 case 0x5931: /* TP-1Y */
5154 case 0x3837: /* TP-78 */
5155 case 0x3637: /* TP-76 */
5156 case 0x3037: /* TP-70 */
5157 printk(IBM_NOTICE
5158 "fan_init: initial fan status is "
5159 "unknown, assuming it is in auto "
5160 "mode\n");
5161 tp_features.fan_ctrl_status_undef = 1;
5162 ;;
5163 }
5164 }
5165 } else {
5166 printk(IBM_ERR
5167 "ThinkPad ACPI EC access misbehaving, "
5168 "fan status and control unavailable\n");
5169 return 1;
5170 }
5171 }
5172
5173 if (sfan_handle) {
5174 /* 570, 770x-JL */
5175 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
5176 fan_control_commands |=
5177 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
5178 } else {
5179 if (!gfan_handle) {
5180 /* gfan without sfan means no fan control */
5181 /* all other models implement TP EC 0x2f control */
5182
5183 if (fans_handle) {
5184 /* X31, X40, X41 */
5185 fan_control_access_mode =
5186 TPACPI_FAN_WR_ACPI_FANS;
5187 fan_control_commands |=
5188 TPACPI_FAN_CMD_SPEED |
5189 TPACPI_FAN_CMD_LEVEL |
5190 TPACPI_FAN_CMD_ENABLE;
5191 } else {
5192 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
5193 fan_control_commands |=
5194 TPACPI_FAN_CMD_LEVEL |
5195 TPACPI_FAN_CMD_ENABLE;
5196 }
fe98a52c 5197 }
b21a15f6 5198 }
18ad7996 5199
b21a15f6
HMH
5200 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
5201 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
5202 fan_control_access_mode != TPACPI_FAN_WR_NONE),
5203 fan_status_access_mode, fan_control_access_mode);
1c6a334e 5204
b21a15f6
HMH
5205 /* fan control master switch */
5206 if (!fan_control_allowed) {
5207 fan_control_access_mode = TPACPI_FAN_WR_NONE;
5208 fan_control_commands = 0;
5209 dbg_printk(TPACPI_DBG_INIT,
5210 "fan control features disabled by parameter\n");
18ad7996 5211 }
40ca9fdf 5212
b21a15f6
HMH
5213 /* update fan_control_desired_level */
5214 if (fan_status_access_mode != TPACPI_FAN_NONE)
5215 fan_get_status_safe(NULL);
fe98a52c 5216
b21a15f6
HMH
5217 if (fan_status_access_mode != TPACPI_FAN_NONE ||
5218 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
5219 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5220 &fan_attr_group);
5221 if (!(rc < 0))
5222 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
5223 &driver_attr_fan_watchdog);
5224 if (rc < 0)
5225 return rc;
5226 return 0;
5227 } else
5228 return 1;
56b6aeb0
HMH
5229}
5230
b21a15f6 5231static void fan_exit(void)
56b6aeb0 5232{
b21a15f6 5233 vdbg_printk(TPACPI_DBG_EXIT, "cancelling any pending fan watchdog tasks\n");
56b6aeb0 5234
b21a15f6
HMH
5235 /* FIXME: can we really do this unconditionally? */
5236 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
5237 driver_remove_file(&tpacpi_hwmon_pdriver.driver, &driver_attr_fan_watchdog);
40ca9fdf 5238
b21a15f6
HMH
5239 cancel_delayed_work(&fan_watchdog_task);
5240 flush_scheduled_work();
56b6aeb0
HMH
5241}
5242
5243static int fan_read(char *p)
5244{
5245 int len = 0;
5246 int rc;
5247 u8 status;
5248 unsigned int speed = 0;
5249
5250 switch (fan_status_access_mode) {
efa27145 5251 case TPACPI_FAN_RD_ACPI_GFAN:
56b6aeb0 5252 /* 570, 600e/x, 770e, 770x */
fe98a52c 5253 if ((rc = fan_get_status_safe(&status)) < 0)
56b6aeb0
HMH
5254 return rc;
5255
5256 len += sprintf(p + len, "status:\t\t%s\n"
5257 "level:\t\t%d\n",
5258 (status != 0) ? "enabled" : "disabled", status);
5259 break;
5260
efa27145 5261 case TPACPI_FAN_RD_TPEC:
56b6aeb0 5262 /* all except 570, 600e/x, 770e, 770x */
fe98a52c 5263 if ((rc = fan_get_status_safe(&status)) < 0)
56b6aeb0
HMH
5264 return rc;
5265
d8fd94d9 5266 if (unlikely(tp_features.fan_ctrl_status_undef)) {
56b6aeb0 5267 if (status != fan_control_initial_status)
d8fd94d9 5268 tp_features.fan_ctrl_status_undef = 0;
56b6aeb0
HMH
5269 else
5270 /* Return most likely status. In fact, it
5271 * might be the only possible status */
efa27145 5272 status = TP_EC_FAN_AUTO;
56b6aeb0
HMH
5273 }
5274
5275 len += sprintf(p + len, "status:\t\t%s\n",
5276 (status != 0) ? "enabled" : "disabled");
5277
56b6aeb0
HMH
5278 if ((rc = fan_get_speed(&speed)) < 0)
5279 return rc;
5280
5281 len += sprintf(p + len, "speed:\t\t%d\n", speed);
5282
efa27145 5283 if (status & TP_EC_FAN_FULLSPEED)
56b6aeb0
HMH
5284 /* Disengaged mode takes precedence */
5285 len += sprintf(p + len, "level:\t\tdisengaged\n");
efa27145 5286 else if (status & TP_EC_FAN_AUTO)
56b6aeb0
HMH
5287 len += sprintf(p + len, "level:\t\tauto\n");
5288 else
5289 len += sprintf(p + len, "level:\t\t%d\n", status);
5290 break;
5291
efa27145 5292 case TPACPI_FAN_NONE:
56b6aeb0
HMH
5293 default:
5294 len += sprintf(p + len, "status:\t\tnot supported\n");
5295 }
5296
efa27145 5297 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
56b6aeb0
HMH
5298 len += sprintf(p + len, "commands:\tlevel <level>");
5299
5300 switch (fan_control_access_mode) {
efa27145 5301 case TPACPI_FAN_WR_ACPI_SFAN:
56b6aeb0
HMH
5302 len += sprintf(p + len, " (<level> is 0-7)\n");
5303 break;
5304
5305 default:
5306 len += sprintf(p + len, " (<level> is 0-7, "
fe98a52c 5307 "auto, disengaged, full-speed)\n");
56b6aeb0
HMH
5308 break;
5309 }
5310 }
18ad7996 5311
efa27145 5312 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
56b6aeb0
HMH
5313 len += sprintf(p + len, "commands:\tenable, disable\n"
5314 "commands:\twatchdog <timeout> (<timeout> is 0 (off), "
5315 "1-120 (seconds))\n");
1da177e4 5316
efa27145 5317 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
56b6aeb0
HMH
5318 len += sprintf(p + len, "commands:\tspeed <speed>"
5319 " (<speed> is 0-65535)\n");
5320
5321 return len;
78f81cc4
BD
5322}
5323
18ad7996
HMH
5324static int fan_write_cmd_level(const char *cmd, int *rc)
5325{
5326 int level;
5327
a12095c2 5328 if (strlencmp(cmd, "level auto") == 0)
efa27145 5329 level = TP_EC_FAN_AUTO;
fe98a52c
HMH
5330 else if ((strlencmp(cmd, "level disengaged") == 0) |
5331 (strlencmp(cmd, "level full-speed") == 0))
efa27145 5332 level = TP_EC_FAN_FULLSPEED;
a12095c2 5333 else if (sscanf(cmd, "level %d", &level) != 1)
18ad7996
HMH
5334 return 0;
5335
fe98a52c 5336 if ((*rc = fan_set_level_safe(level)) == -ENXIO)
18ad7996
HMH
5337 printk(IBM_ERR "level command accepted for unsupported "
5338 "access mode %d", fan_control_access_mode);
5339
5340 return 1;
5341}
5342
5343static int fan_write_cmd_enable(const char *cmd, int *rc)
5344{
5345 if (strlencmp(cmd, "enable") != 0)
5346 return 0;
5347
5348 if ((*rc = fan_set_enable()) == -ENXIO)
5349 printk(IBM_ERR "enable command accepted for unsupported "
5350 "access mode %d", fan_control_access_mode);
5351
5352 return 1;
5353}
5354
5355static int fan_write_cmd_disable(const char *cmd, int *rc)
5356{
5357 if (strlencmp(cmd, "disable") != 0)
5358 return 0;
5359
5360 if ((*rc = fan_set_disable()) == -ENXIO)
5361 printk(IBM_ERR "disable command accepted for unsupported "
5362 "access mode %d", fan_control_access_mode);
5363
5364 return 1;
5365}
5366
5367static int fan_write_cmd_speed(const char *cmd, int *rc)
5368{
5369 int speed;
5370
a8b7a662
HMH
5371 /* TODO:
5372 * Support speed <low> <medium> <high> ? */
5373
18ad7996
HMH
5374 if (sscanf(cmd, "speed %d", &speed) != 1)
5375 return 0;
5376
5377 if ((*rc = fan_set_speed(speed)) == -ENXIO)
5378 printk(IBM_ERR "speed command accepted for unsupported "
5379 "access mode %d", fan_control_access_mode);
5380
5381 return 1;
5382}
5383
16663a87
HMH
5384static int fan_write_cmd_watchdog(const char *cmd, int *rc)
5385{
5386 int interval;
5387
5388 if (sscanf(cmd, "watchdog %d", &interval) != 1)
5389 return 0;
5390
5391 if (interval < 0 || interval > 120)
5392 *rc = -EINVAL;
5393 else
5394 fan_watchdog_maxinterval = interval;
5395
5396 return 1;
5397}
5398
18ad7996
HMH
5399static int fan_write(char *buf)
5400{
5401 char *cmd;
5402 int rc = 0;
5403
5404 while (!rc && (cmd = next_cmd(&buf))) {
efa27145 5405 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
18ad7996 5406 fan_write_cmd_level(cmd, &rc)) &&
efa27145 5407 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
18ad7996 5408 (fan_write_cmd_enable(cmd, &rc) ||
16663a87
HMH
5409 fan_write_cmd_disable(cmd, &rc) ||
5410 fan_write_cmd_watchdog(cmd, &rc))) &&
efa27145 5411 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
18ad7996
HMH
5412 fan_write_cmd_speed(cmd, &rc))
5413 )
5414 rc = -EINVAL;
16663a87
HMH
5415 else if (!rc)
5416 fan_watchdog_reset();
18ad7996
HMH
5417 }
5418
5419 return rc;
5420}
5421
a5763f22
HMH
5422static struct ibm_struct fan_driver_data = {
5423 .name = "fan",
5424 .read = fan_read,
5425 .write = fan_write,
5426 .exit = fan_exit,
a5763f22
HMH
5427};
5428
56b6aeb0
HMH
5429/****************************************************************************
5430 ****************************************************************************
5431 *
5432 * Infrastructure
5433 *
5434 ****************************************************************************
5435 ****************************************************************************/
5436
7fd40029
HMH
5437/* sysfs name ---------------------------------------------------------- */
5438static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
5439 struct device_attribute *attr,
5440 char *buf)
5441{
5442 return snprintf(buf, PAGE_SIZE, "%s\n", IBM_NAME);
5443}
5444
5445static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
5446 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
5447
5448/* --------------------------------------------------------------------- */
5449
56b6aeb0 5450/* /proc support */
94954cc6 5451static struct proc_dir_entry *proc_dir;
16663a87 5452
56b6aeb0
HMH
5453/*
5454 * Module and infrastructure proble, init and exit handling
5455 */
1da177e4 5456
b21a15f6
HMH
5457static int force_load;
5458
fe08bc4b 5459#ifdef CONFIG_THINKPAD_ACPI_DEBUG
a5763f22 5460static const char * __init str_supported(int is_supported)
fe08bc4b 5461{
a5763f22 5462 static char text_unsupported[] __initdata = "not supported";
fe08bc4b 5463
a5763f22 5464 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
fe08bc4b
HMH
5465}
5466#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
5467
b21a15f6
HMH
5468static void ibm_exit(struct ibm_struct *ibm)
5469{
5470 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
5471
5472 list_del_init(&ibm->all_drivers);
5473
5474 if (ibm->flags.acpi_notify_installed) {
5475 dbg_printk(TPACPI_DBG_EXIT,
5476 "%s: acpi_remove_notify_handler\n", ibm->name);
5477 BUG_ON(!ibm->acpi);
5478 acpi_remove_notify_handler(*ibm->acpi->handle,
5479 ibm->acpi->type,
5480 dispatch_acpi_notify);
5481 ibm->flags.acpi_notify_installed = 0;
5482 ibm->flags.acpi_notify_installed = 0;
5483 }
5484
5485 if (ibm->flags.proc_created) {
5486 dbg_printk(TPACPI_DBG_EXIT,
5487 "%s: remove_proc_entry\n", ibm->name);
5488 remove_proc_entry(ibm->name, proc_dir);
5489 ibm->flags.proc_created = 0;
5490 }
5491
5492 if (ibm->flags.acpi_driver_registered) {
5493 dbg_printk(TPACPI_DBG_EXIT,
5494 "%s: acpi_bus_unregister_driver\n", ibm->name);
5495 BUG_ON(!ibm->acpi);
5496 acpi_bus_unregister_driver(ibm->acpi->driver);
5497 kfree(ibm->acpi->driver);
5498 ibm->acpi->driver = NULL;
5499 ibm->flags.acpi_driver_registered = 0;
5500 }
5501
5502 if (ibm->flags.init_called && ibm->exit) {
5503 ibm->exit();
5504 ibm->flags.init_called = 0;
5505 }
5506
5507 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
5508}
f74a27d4 5509
a5763f22 5510static int __init ibm_init(struct ibm_init_struct *iibm)
1da177e4
LT
5511{
5512 int ret;
a5763f22 5513 struct ibm_struct *ibm = iibm->data;
1da177e4
LT
5514 struct proc_dir_entry *entry;
5515
a5763f22
HMH
5516 BUG_ON(ibm == NULL);
5517
5518 INIT_LIST_HEAD(&ibm->all_drivers);
5519
92641177 5520 if (ibm->flags.experimental && !experimental)
1da177e4
LT
5521 return 0;
5522
fe08bc4b
HMH
5523 dbg_printk(TPACPI_DBG_INIT,
5524 "probing for %s\n", ibm->name);
5525
a5763f22
HMH
5526 if (iibm->init) {
5527 ret = iibm->init(iibm);
5fba344c
HMH
5528 if (ret > 0)
5529 return 0; /* probe failed */
5530 if (ret)
1da177e4 5531 return ret;
a5763f22 5532
92641177 5533 ibm->flags.init_called = 1;
1da177e4
LT
5534 }
5535
8d376cd6
HMH
5536 if (ibm->acpi) {
5537 if (ibm->acpi->hid) {
5538 ret = register_tpacpi_subdriver(ibm);
5539 if (ret)
5540 goto err_out;
5541 }
5fba344c 5542
8d376cd6
HMH
5543 if (ibm->acpi->notify) {
5544 ret = setup_acpi_notify(ibm);
5545 if (ret == -ENODEV) {
5546 printk(IBM_NOTICE "disabling subdriver %s\n",
5547 ibm->name);
5548 ret = 0;
5549 goto err_out;
5550 }
5551 if (ret < 0)
5552 goto err_out;
5fba344c 5553 }
5fba344c
HMH
5554 }
5555
fe08bc4b
HMH
5556 dbg_printk(TPACPI_DBG_INIT,
5557 "%s installed\n", ibm->name);
5558
78f81cc4
BD
5559 if (ibm->read) {
5560 entry = create_proc_entry(ibm->name,
5561 S_IFREG | S_IRUGO | S_IWUSR,
5562 proc_dir);
5563 if (!entry) {
5564 printk(IBM_ERR "unable to create proc entry %s\n",
5565 ibm->name);
5fba344c
HMH
5566 ret = -ENODEV;
5567 goto err_out;
78f81cc4
BD
5568 }
5569 entry->owner = THIS_MODULE;
5570 entry->data = ibm;
8d376cd6 5571 entry->read_proc = &dispatch_procfs_read;
78f81cc4 5572 if (ibm->write)
8d376cd6 5573 entry->write_proc = &dispatch_procfs_write;
92641177 5574 ibm->flags.proc_created = 1;
78f81cc4 5575 }
1da177e4 5576
a5763f22
HMH
5577 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
5578
1da177e4 5579 return 0;
5fba344c
HMH
5580
5581err_out:
fe08bc4b
HMH
5582 dbg_printk(TPACPI_DBG_INIT,
5583 "%s: at error exit path with result %d\n",
5584 ibm->name, ret);
5585
5fba344c
HMH
5586 ibm_exit(ibm);
5587 return (ret < 0)? ret : 0;
1da177e4
LT
5588}
5589
56b6aeb0
HMH
5590/* Probing */
5591
d5a2f2f1 5592static void __init get_thinkpad_model_data(struct thinkpad_id_data *tp)
1da177e4 5593{
1855256c 5594 const struct dmi_device *dev = NULL;
56b6aeb0 5595 char ec_fw_string[18];
1da177e4 5596
d5a2f2f1
HMH
5597 if (!tp)
5598 return;
5599
5600 memset(tp, 0, sizeof(*tp));
5601
5602 if (dmi_name_in_vendors("IBM"))
5603 tp->vendor = PCI_VENDOR_ID_IBM;
5604 else if (dmi_name_in_vendors("LENOVO"))
5605 tp->vendor = PCI_VENDOR_ID_LENOVO;
5606 else
5607 return;
5608
5609 tp->bios_version_str = kstrdup(dmi_get_system_info(DMI_BIOS_VERSION),
5610 GFP_KERNEL);
5611 if (!tp->bios_version_str)
5612 return;
5613 tp->bios_model = tp->bios_version_str[0]
5614 | (tp->bios_version_str[1] << 8);
5615
56b6aeb0
HMH
5616 /*
5617 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
5618 * X32 or newer, all Z series; Some models must have an
5619 * up-to-date BIOS or they will not be detected.
5620 *
5621 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
5622 */
5623 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
5624 if (sscanf(dev->name,
5625 "IBM ThinkPad Embedded Controller -[%17c",
5626 ec_fw_string) == 1) {
5627 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
5628 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
d5a2f2f1
HMH
5629
5630 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
5631 tp->ec_model = ec_fw_string[0]
5632 | (ec_fw_string[1] << 8);
5633 break;
78f81cc4 5634 }
1da177e4 5635 }
d5a2f2f1
HMH
5636
5637 tp->model_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_VERSION),
5638 GFP_KERNEL);
5639 if (strnicmp(tp->model_str, "ThinkPad", 8) != 0) {
5640 kfree(tp->model_str);
5641 tp->model_str = NULL;
5642 }
1da177e4
LT
5643}
5644
5fba344c
HMH
5645static int __init probe_for_thinkpad(void)
5646{
5647 int is_thinkpad;
5648
5649 if (acpi_disabled)
5650 return -ENODEV;
5651
5652 /*
5653 * Non-ancient models have better DMI tagging, but very old models
5654 * don't.
5655 */
d5a2f2f1 5656 is_thinkpad = (thinkpad_id.model_str != NULL);
5fba344c
HMH
5657
5658 /* ec is required because many other handles are relative to it */
8d376cd6 5659 IBM_ACPIHANDLE_INIT(ec);
5fba344c
HMH
5660 if (!ec_handle) {
5661 if (is_thinkpad)
5662 printk(IBM_ERR
5663 "Not yet supported ThinkPad detected!\n");
5664 return -ENODEV;
5665 }
5666
0dcef77c
HMH
5667 /*
5668 * Risks a regression on very old machines, but reduces potential
5669 * false positives a damn great deal
5670 */
5671 if (!is_thinkpad)
d5a2f2f1 5672 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
0dcef77c
HMH
5673
5674 if (!is_thinkpad && !force_load)
5675 return -ENODEV;
5676
5fba344c
HMH
5677 return 0;
5678}
5679
5680
56b6aeb0 5681/* Module init, exit, parameters */
1da177e4 5682
a5763f22
HMH
5683static struct ibm_init_struct ibms_init[] __initdata = {
5684 {
5685 .init = thinkpad_acpi_driver_init,
5686 .data = &thinkpad_acpi_driver_data,
5687 },
5688 {
5689 .init = hotkey_init,
5690 .data = &hotkey_driver_data,
5691 },
5692 {
5693 .init = bluetooth_init,
5694 .data = &bluetooth_driver_data,
5695 },
5696 {
5697 .init = wan_init,
5698 .data = &wan_driver_data,
5699 },
5700 {
5701 .init = video_init,
5702 .data = &video_driver_data,
5703 },
5704 {
5705 .init = light_init,
5706 .data = &light_driver_data,
5707 },
5708#ifdef CONFIG_THINKPAD_ACPI_DOCK
5709 {
5710 .init = dock_init,
5711 .data = &dock_driver_data[0],
5712 },
5713 {
d94a7f16 5714 .init = dock_init2,
a5763f22
HMH
5715 .data = &dock_driver_data[1],
5716 },
5717#endif
5718#ifdef CONFIG_THINKPAD_ACPI_BAY
5719 {
5720 .init = bay_init,
5721 .data = &bay_driver_data,
5722 },
5723#endif
5724 {
5725 .init = cmos_init,
5726 .data = &cmos_driver_data,
5727 },
5728 {
5729 .init = led_init,
5730 .data = &led_driver_data,
5731 },
5732 {
5733 .init = beep_init,
5734 .data = &beep_driver_data,
5735 },
5736 {
5737 .init = thermal_init,
5738 .data = &thermal_driver_data,
5739 },
5740 {
5741 .data = &ecdump_driver_data,
5742 },
5743 {
5744 .init = brightness_init,
5745 .data = &brightness_driver_data,
5746 },
5747 {
5748 .data = &volume_driver_data,
5749 },
5750 {
5751 .init = fan_init,
5752 .data = &fan_driver_data,
5753 },
5754};
5755
3945ac36 5756static int __init set_ibm_param(const char *val, struct kernel_param *kp)
1da177e4
LT
5757{
5758 unsigned int i;
a5763f22 5759 struct ibm_struct *ibm;
1da177e4 5760
59f91ff1
HMH
5761 if (!kp || !kp->name || !val)
5762 return -EINVAL;
5763
a5763f22
HMH
5764 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
5765 ibm = ibms_init[i].data;
59f91ff1
HMH
5766 WARN_ON(ibm == NULL);
5767
5768 if (!ibm || !ibm->name)
5769 continue;
a5763f22
HMH
5770
5771 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
5772 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
78f81cc4 5773 return -ENOSPC;
a5763f22
HMH
5774 strcpy(ibms_init[i].param, val);
5775 strcat(ibms_init[i].param, ",");
78f81cc4
BD
5776 return 0;
5777 }
a5763f22 5778 }
1da177e4 5779
1da177e4
LT
5780 return -EINVAL;
5781}
5782
56b6aeb0
HMH
5783module_param(experimental, int, 0);
5784
132ce091
HMH
5785module_param_named(debug, dbg_level, uint, 0);
5786
86cc9445 5787module_param(force_load, bool, 0);
0dcef77c 5788
86cc9445 5789module_param_named(fan_control, fan_control_allowed, bool, 0);
ecf2a80a 5790
24d3b774
HMH
5791module_param_named(brightness_mode, brightness_mode, int, 0);
5792
87cc537a
HMH
5793module_param(brightness_enable, uint, 0);
5794
ff80f137
HMH
5795module_param(hotkey_report_mode, uint, 0);
5796
1da177e4
LT
5797#define IBM_PARAM(feature) \
5798 module_param_call(feature, set_ibm_param, NULL, NULL, 0)
5799
78f81cc4
BD
5800IBM_PARAM(hotkey);
5801IBM_PARAM(bluetooth);
5802IBM_PARAM(video);
5803IBM_PARAM(light);
85998248 5804#ifdef CONFIG_THINKPAD_ACPI_DOCK
78f81cc4 5805IBM_PARAM(dock);
63e5f248 5806#endif
85998248 5807#ifdef CONFIG_THINKPAD_ACPI_BAY
78f81cc4 5808IBM_PARAM(bay);
85998248 5809#endif /* CONFIG_THINKPAD_ACPI_BAY */
78f81cc4
BD
5810IBM_PARAM(cmos);
5811IBM_PARAM(led);
5812IBM_PARAM(beep);
5813IBM_PARAM(ecdump);
5814IBM_PARAM(brightness);
5815IBM_PARAM(volume);
5816IBM_PARAM(fan);
5817
b21a15f6
HMH
5818static void thinkpad_acpi_module_exit(void)
5819{
5820 struct ibm_struct *ibm, *itmp;
5821
5822 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
5823
5824 list_for_each_entry_safe_reverse(ibm, itmp,
5825 &tpacpi_all_drivers,
5826 all_drivers) {
5827 ibm_exit(ibm);
5828 }
5829
5830 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
5831
5832 if (tpacpi_inputdev) {
5833 if (tp_features.input_device_registered)
5834 input_unregister_device(tpacpi_inputdev);
5835 else
5836 input_free_device(tpacpi_inputdev);
5837 }
5838
5839 if (tpacpi_hwmon)
5840 hwmon_device_unregister(tpacpi_hwmon);
5841
5842 if (tp_features.sensors_pdev_attrs_registered)
5843 device_remove_file(&tpacpi_sensors_pdev->dev,
5844 &dev_attr_thinkpad_acpi_pdev_name);
5845 if (tpacpi_sensors_pdev)
5846 platform_device_unregister(tpacpi_sensors_pdev);
5847 if (tpacpi_pdev)
5848 platform_device_unregister(tpacpi_pdev);
5849
5850 if (tp_features.sensors_pdrv_attrs_registered)
5851 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
5852 if (tp_features.platform_drv_attrs_registered)
5853 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
5854
5855 if (tp_features.sensors_pdrv_registered)
5856 platform_driver_unregister(&tpacpi_hwmon_pdriver);
5857
5858 if (tp_features.platform_drv_registered)
5859 platform_driver_unregister(&tpacpi_pdriver);
5860
5861 if (proc_dir)
5862 remove_proc_entry(IBM_PROC_DIR, acpi_root_dir);
5863
5864 kfree(thinkpad_id.bios_version_str);
5865 kfree(thinkpad_id.ec_version_str);
5866 kfree(thinkpad_id.model_str);
5867}
5868
f74a27d4 5869
1def7115 5870static int __init thinkpad_acpi_module_init(void)
1da177e4
LT
5871{
5872 int ret, i;
5873
8fef502e
HMH
5874 tpacpi_lifecycle = TPACPI_LIFE_INIT;
5875
ff80f137
HMH
5876 /* Parameter checking */
5877 if (hotkey_report_mode > 2)
5878 return -EINVAL;
5879
54ae1501 5880 /* Driver-level probe */
d5a2f2f1
HMH
5881
5882 get_thinkpad_model_data(&thinkpad_id);
5fba344c 5883 ret = probe_for_thinkpad();
d5a2f2f1
HMH
5884 if (ret) {
5885 thinkpad_acpi_module_exit();
5fba344c 5886 return ret;
d5a2f2f1 5887 }
1da177e4 5888
54ae1501 5889 /* Driver initialization */
d5a2f2f1 5890
8d376cd6
HMH
5891 IBM_ACPIHANDLE_INIT(ecrd);
5892 IBM_ACPIHANDLE_INIT(ecwr);
1da177e4 5893
8d376cd6 5894 proc_dir = proc_mkdir(IBM_PROC_DIR, acpi_root_dir);
1da177e4 5895 if (!proc_dir) {
8d376cd6 5896 printk(IBM_ERR "unable to create proc dir " IBM_PROC_DIR);
1def7115 5897 thinkpad_acpi_module_exit();
1da177e4
LT
5898 return -ENODEV;
5899 }
5900 proc_dir->owner = THIS_MODULE;
78f81cc4 5901
54ae1501
HMH
5902 ret = platform_driver_register(&tpacpi_pdriver);
5903 if (ret) {
7fd40029 5904 printk(IBM_ERR "unable to register main platform driver\n");
54ae1501
HMH
5905 thinkpad_acpi_module_exit();
5906 return ret;
5907 }
ac36393d
HMH
5908 tp_features.platform_drv_registered = 1;
5909
7fd40029
HMH
5910 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
5911 if (ret) {
5912 printk(IBM_ERR "unable to register hwmon platform driver\n");
5913 thinkpad_acpi_module_exit();
5914 return ret;
5915 }
5916 tp_features.sensors_pdrv_registered = 1;
5917
176750d6 5918 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
2369cc94
HMH
5919 if (!ret) {
5920 tp_features.platform_drv_attrs_registered = 1;
5921 ret = tpacpi_create_driver_attributes(&tpacpi_hwmon_pdriver.driver);
5922 }
176750d6
HMH
5923 if (ret) {
5924 printk(IBM_ERR "unable to create sysfs driver attributes\n");
5925 thinkpad_acpi_module_exit();
5926 return ret;
5927 }
2369cc94 5928 tp_features.sensors_pdrv_attrs_registered = 1;
176750d6 5929
54ae1501
HMH
5930
5931 /* Device initialization */
5932 tpacpi_pdev = platform_device_register_simple(IBM_DRVR_NAME, -1,
5933 NULL, 0);
5934 if (IS_ERR(tpacpi_pdev)) {
5935 ret = PTR_ERR(tpacpi_pdev);
5936 tpacpi_pdev = NULL;
5937 printk(IBM_ERR "unable to register platform device\n");
5938 thinkpad_acpi_module_exit();
5939 return ret;
5940 }
7fd40029
HMH
5941 tpacpi_sensors_pdev = platform_device_register_simple(
5942 IBM_HWMON_DRVR_NAME,
5943 -1, NULL, 0);
5944 if (IS_ERR(tpacpi_sensors_pdev)) {
5945 ret = PTR_ERR(tpacpi_sensors_pdev);
5946 tpacpi_sensors_pdev = NULL;
5947 printk(IBM_ERR "unable to register hwmon platform device\n");
5948 thinkpad_acpi_module_exit();
5949 return ret;
5950 }
5951 ret = device_create_file(&tpacpi_sensors_pdev->dev,
5952 &dev_attr_thinkpad_acpi_pdev_name);
5953 if (ret) {
5954 printk(IBM_ERR
5955 "unable to create sysfs hwmon device attributes\n");
5956 thinkpad_acpi_module_exit();
5957 return ret;
5958 }
5959 tp_features.sensors_pdev_attrs_registered = 1;
5960 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
54ae1501
HMH
5961 if (IS_ERR(tpacpi_hwmon)) {
5962 ret = PTR_ERR(tpacpi_hwmon);
5963 tpacpi_hwmon = NULL;
5964 printk(IBM_ERR "unable to register hwmon device\n");
5965 thinkpad_acpi_module_exit();
5966 return ret;
5967 }
8523ed6f 5968 mutex_init(&tpacpi_inputdev_send_mutex);
7f5d1cd6
HMH
5969 tpacpi_inputdev = input_allocate_device();
5970 if (!tpacpi_inputdev) {
5971 printk(IBM_ERR "unable to allocate input device\n");
5972 thinkpad_acpi_module_exit();
5973 return -ENOMEM;
5974 } else {
5975 /* Prepare input device, but don't register */
5976 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
5977 tpacpi_inputdev->phys = IBM_DRVR_NAME "/input0";
5978 tpacpi_inputdev->id.bustype = BUS_HOST;
edf0e0e5
HMH
5979 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
5980 thinkpad_id.vendor :
5981 PCI_VENDOR_ID_IBM;
7f5d1cd6
HMH
5982 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
5983 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
5984 }
a5763f22
HMH
5985 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
5986 ret = ibm_init(&ibms_init[i]);
5987 if (ret >= 0 && *ibms_init[i].param)
5988 ret = ibms_init[i].data->write(ibms_init[i].param);
1da177e4 5989 if (ret < 0) {
1def7115 5990 thinkpad_acpi_module_exit();
1da177e4
LT
5991 return ret;
5992 }
5993 }
7f5d1cd6
HMH
5994 ret = input_register_device(tpacpi_inputdev);
5995 if (ret < 0) {
5996 printk(IBM_ERR "unable to register input device\n");
5997 thinkpad_acpi_module_exit();
5998 return ret;
5999 } else {
6000 tp_features.input_device_registered = 1;
6001 }
1da177e4 6002
8fef502e 6003 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
1da177e4
LT
6004 return 0;
6005}
6006
1def7115
HMH
6007module_init(thinkpad_acpi_module_init);
6008module_exit(thinkpad_acpi_module_exit);
This page took 0.581474 seconds and 5 git commands to generate.