blob: 45be8e5f935196c80acdcd42d9f0f4f31d3e78b6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -03002 * thinkpad_acpi.c - ThinkPad ACPI Extras
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 *
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
Henrique de Moraes Holschuha62bc912007-03-23 17:33:58 -03006 * Copyright (C) 2006-2007 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
Henrique de Moraes Holschuha62bc912007-03-23 17:33:58 -030020 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
Borislav Deianov78f81cc2005-08-17 00:00:00 -040022 */
23
Henrique de Moraes Holschuh50efd832008-01-08 13:02:42 -020024#define IBM_VERSION "0.18"
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -020025#define TPACPI_SYSFS_VERSION 0x020101
Borislav Deianov78f81cc2005-08-17 00:00:00 -040026
27/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 * Changelog:
Henrique de Moraes Holschuh4b45cc02008-01-08 13:02:46 -020029 * 2007-10-20 changelog trimmed down
30 *
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -030031 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
32 * drivers/misc.
Henrique de Moraes Holschuhf9ff43a2006-11-25 16:37:38 -020033 *
34 * 2006-11-22 0.13 new maintainer
35 * changelog now lives in git commit history, and will
36 * not be updated further in-file.
Henrique de Moraes Holschuh837ca6d2007-03-23 17:33:54 -030037 *
Borislav Deianov78f81cc2005-08-17 00:00:00 -040038 * 2005-03-17 0.11 support for 600e, 770x
39 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
Henrique de Moraes Holschuh4b45cc02008-01-08 13:02:46 -020040 *
41 * 2005-01-16 0.9 use MODULE_VERSION
Borislav Deianov78f81cc2005-08-17 00:00:00 -040042 * thanks to Henrik Brix Andersen <brix@gentoo.org>
43 * fix parameter passing on module loading
44 * thanks to Rusty Russell <rusty@rustcorp.com.au>
45 * thanks to Jim Radford <radford@blackbean.org>
46 * 2004-11-08 0.8 fix init error case, don't return from a macro
47 * thanks to Chris Wright <chrisw@osdl.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 */
49
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020050#include <linux/kernel.h>
51#include <linux/module.h>
52#include <linux/init.h>
53#include <linux/types.h>
54#include <linux/string.h>
55#include <linux/list.h>
56#include <linux/mutex.h>
57#include <linux/kthread.h>
58#include <linux/freezer.h>
59#include <linux/delay.h>
60
61#include <linux/nvram.h>
62#include <linux/proc_fs.h>
63#include <linux/sysfs.h>
64#include <linux/backlight.h>
65#include <linux/fb.h>
66#include <linux/platform_device.h>
67#include <linux/hwmon.h>
68#include <linux/hwmon-sysfs.h>
69#include <linux/input.h>
70#include <asm/uaccess.h>
71
72#include <linux/dmi.h>
73#include <linux/jiffies.h>
74#include <linux/workqueue.h>
75
76#include <acpi/acpi_drivers.h>
77#include <acpi/acnamesp.h>
78
79#include <linux/pci_ids.h>
80
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020081
82/* ThinkPad CMOS commands */
83#define TP_CMOS_VOLUME_DOWN 0
84#define TP_CMOS_VOLUME_UP 1
85#define TP_CMOS_VOLUME_MUTE 2
86#define TP_CMOS_BRIGHTNESS_UP 4
87#define TP_CMOS_BRIGHTNESS_DOWN 5
88
89/* NVRAM Addresses */
90enum tp_nvram_addr {
91 TP_NVRAM_ADDR_HK2 = 0x57,
92 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
93 TP_NVRAM_ADDR_VIDEO = 0x59,
94 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
95 TP_NVRAM_ADDR_MIXER = 0x60,
96};
97
98/* NVRAM bit masks */
99enum {
100 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
101 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
102 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
103 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
104 TP_NVRAM_MASK_THINKLIGHT = 0x10,
105 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
106 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
107 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
108 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
109 TP_NVRAM_MASK_MUTE = 0x40,
110 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
111 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
112 TP_NVRAM_POS_LEVEL_VOLUME = 0,
113};
114
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200115/* ACPI HIDs */
116#define IBM_HKEY_HID "IBM0068"
117
118/* Input IDs */
119#define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
120#define TPACPI_HKEY_INPUT_VERSION 0x4101
121
122
123/****************************************************************************
124 * Main driver
125 */
126
127/* Module */
128#define IBM_NAME "thinkpad"
129#define IBM_DESC "ThinkPad ACPI Extras"
130#define IBM_FILE IBM_NAME "_acpi"
131#define IBM_URL "http://ibm-acpi.sf.net/"
132#define IBM_MAIL "ibm-acpi-devel@lists.sourceforge.net"
133
134#define IBM_PROC_DIR "ibm"
135#define IBM_ACPI_EVENT_PREFIX "ibm"
136#define IBM_DRVR_NAME IBM_FILE
137#define IBM_HWMON_DRVR_NAME IBM_NAME "_hwmon"
138
139#define IBM_MAX_ACPI_ARGS 3
140
141MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
142MODULE_DESCRIPTION(IBM_DESC);
143MODULE_VERSION(IBM_VERSION);
144MODULE_LICENSE("GPL");
145
146/* Please remove this in year 2009 */
147MODULE_ALIAS("ibm_acpi");
148
149/*
150 * DMI matching for module autoloading
151 *
152 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
153 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
154 *
155 * Only models listed in thinkwiki will be supported, so add yours
156 * if it is not there yet.
157 */
158#define IBM_BIOS_MODULE_ALIAS(__type) \
159 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW")
160
161/* Non-ancient thinkpads */
162MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
163MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
164
165/* Ancient thinkpad BIOSes have to be identified by
166 * BIOS type or model number, and there are far less
167 * BIOS types than model numbers... */
168IBM_BIOS_MODULE_ALIAS("I[B,D,H,I,M,N,O,T,W,V,Y,Z]");
169IBM_BIOS_MODULE_ALIAS("1[0,3,6,8,A-G,I,K,M-P,S,T]");
170IBM_BIOS_MODULE_ALIAS("K[U,X-Z]");
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200171
172/* Debugging */
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200173#define IBM_LOG IBM_FILE ": "
174#define IBM_ERR KERN_ERR IBM_LOG
175#define IBM_NOTICE KERN_NOTICE IBM_LOG
176#define IBM_INFO KERN_INFO IBM_LOG
177#define IBM_DEBUG KERN_DEBUG IBM_LOG
178
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200179#define TPACPI_DBG_ALL 0xffff
180#define TPACPI_DBG_ALL 0xffff
181#define TPACPI_DBG_INIT 0x0001
182#define TPACPI_DBG_EXIT 0x0002
183#define dbg_printk(a_dbg_level, format, arg...) \
184 do { if (dbg_level & a_dbg_level) \
185 printk(IBM_DEBUG "%s: " format, __func__ , ## arg); } while (0)
186#ifdef CONFIG_THINKPAD_ACPI_DEBUG
187#define vdbg_printk(a_dbg_level, format, arg...) \
188 dbg_printk(a_dbg_level, format, ## arg)
189static const char *str_supported(int is_supported);
190#else
191#define vdbg_printk(a_dbg_level, format, arg...)
192#endif
193
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200194#define onoff(status,bit) ((status) & (1 << (bit)) ? "on" : "off")
195#define enabled(status,bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
196#define strlencmp(a,b) (strncmp((a), (b), strlen(b)))
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200197
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200198
199/****************************************************************************
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200200 * Driver-wide structs and misc. variables
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200201 */
202
203struct ibm_struct;
204
205struct tp_acpi_drv_struct {
206 const struct acpi_device_id *hid;
207 struct acpi_driver *driver;
208
209 void (*notify) (struct ibm_struct *, u32);
210 acpi_handle *handle;
211 u32 type;
212 struct acpi_device *device;
213};
214
215struct ibm_struct {
216 char *name;
217
218 int (*read) (char *);
219 int (*write) (char *);
220 void (*exit) (void);
221 void (*resume) (void);
222
223 struct list_head all_drivers;
224
225 struct tp_acpi_drv_struct *acpi;
226
227 struct {
228 u8 acpi_driver_registered:1;
229 u8 acpi_notify_installed:1;
230 u8 proc_created:1;
231 u8 init_called:1;
232 u8 experimental:1;
233 } flags;
234};
235
236struct ibm_init_struct {
237 char param[32];
238
239 int (*init) (struct ibm_init_struct *);
240 struct ibm_struct *data;
241};
242
243static struct {
244#ifdef CONFIG_THINKPAD_ACPI_BAY
245 u32 bay_status:1;
246 u32 bay_eject:1;
247 u32 bay_status2:1;
248 u32 bay_eject2:1;
249#endif
250 u32 bluetooth:1;
251 u32 hotkey:1;
252 u32 hotkey_mask:1;
253 u32 hotkey_wlsw:1;
254 u32 light:1;
255 u32 light_status:1;
256 u32 bright_16levels:1;
257 u32 wan:1;
258 u32 fan_ctrl_status_undef:1;
259 u32 input_device_registered:1;
260 u32 platform_drv_registered:1;
261 u32 platform_drv_attrs_registered:1;
262 u32 sensors_pdrv_registered:1;
263 u32 sensors_pdrv_attrs_registered:1;
264 u32 sensors_pdev_attrs_registered:1;
265 u32 hotkey_poll_active:1;
266} tp_features;
267
268struct thinkpad_id_data {
269 unsigned int vendor; /* ThinkPad vendor:
270 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
271
272 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
273 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
274
275 u16 bios_model; /* Big Endian, TP-1Y = 0x5931, 0 = unknown */
276 u16 ec_model;
277
278 char *model_str;
279};
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200280static struct thinkpad_id_data thinkpad_id;
281
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300282static enum {
283 TPACPI_LIFE_INIT = 0,
284 TPACPI_LIFE_RUNNING,
285 TPACPI_LIFE_EXITING,
286} tpacpi_lifecycle;
287
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200288static int experimental;
289static u32 dbg_level;
290
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300291/****************************************************************************
292 ****************************************************************************
293 *
294 * ACPI Helpers and device model
295 *
296 ****************************************************************************
297 ****************************************************************************/
298
299/*************************************************************************
300 * ACPI basic handles
301 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -0300303static acpi_handle root_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
305#define IBM_HANDLE(object, parent, paths...) \
306 static acpi_handle object##_handle; \
307 static acpi_handle *object##_parent = &parent##_handle; \
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400308 static char *object##_path; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 static char *object##_paths[] = { paths }
310
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400311IBM_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
312 "\\_SB.PCI.ISA.EC", /* 570 */
313 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
314 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
315 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
316 "\\_SB.PCI0.ICH3.EC0", /* R31 */
317 "\\_SB.PCI0.LPC.EC", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300318 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300320IBM_HANDLE(ecrd, ec, "ECRD"); /* 570 */
321IBM_HANDLE(ecwr, ec, "ECWR"); /* 570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400323IBM_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, T4x, X31, X40 */
324 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
325 "\\CMS", /* R40, R40e */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300326 ); /* all others */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400327
328IBM_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
329 "^HKEY", /* R30, R31 */
330 "HKEY", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300331 ); /* 570 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400332
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400333
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300334/*************************************************************************
335 * ACPI helpers
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200336 */
337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338static int acpi_evalf(acpi_handle handle,
339 void *res, char *method, char *fmt, ...)
340{
341 char *fmt0 = fmt;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400342 struct acpi_object_list params;
343 union acpi_object in_objs[IBM_MAX_ACPI_ARGS];
344 struct acpi_buffer result, *resultp;
345 union acpi_object out_obj;
346 acpi_status status;
347 va_list ap;
348 char res_type;
349 int success;
350 int quiet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
352 if (!*fmt) {
353 printk(IBM_ERR "acpi_evalf() called with empty format\n");
354 return 0;
355 }
356
357 if (*fmt == 'q') {
358 quiet = 1;
359 fmt++;
360 } else
361 quiet = 0;
362
363 res_type = *(fmt++);
364
365 params.count = 0;
366 params.pointer = &in_objs[0];
367
368 va_start(ap, fmt);
369 while (*fmt) {
370 char c = *(fmt++);
371 switch (c) {
372 case 'd': /* int */
373 in_objs[params.count].integer.value = va_arg(ap, int);
374 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
375 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400376 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 default:
378 printk(IBM_ERR "acpi_evalf() called "
379 "with invalid format character '%c'\n", c);
380 return 0;
381 }
382 }
383 va_end(ap);
384
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400385 if (res_type != 'v') {
386 result.length = sizeof(out_obj);
387 result.pointer = &out_obj;
388 resultp = &result;
389 } else
390 resultp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400392 status = acpi_evaluate_object(handle, method, &params, resultp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
394 switch (res_type) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400395 case 'd': /* int */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 if (res)
397 *(int *)res = out_obj.integer.value;
398 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
399 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400400 case 'v': /* void */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 success = status == AE_OK;
402 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400403 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 default:
405 printk(IBM_ERR "acpi_evalf() called "
406 "with invalid format character '%c'\n", res_type);
407 return 0;
408 }
409
410 if (!success && !quiet)
411 printk(IBM_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
412 method, fmt0, status);
413
414 return success;
415}
416
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300417static int acpi_ec_read(int i, u8 * p)
418{
419 int v;
420
421 if (ecrd_handle) {
422 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
423 return 0;
424 *p = v;
425 } else {
426 if (ec_read(i, p) < 0)
427 return 0;
428 }
429
430 return 1;
431}
432
433static int acpi_ec_write(int i, u8 v)
434{
435 if (ecwr_handle) {
436 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
437 return 0;
438 } else {
439 if (ec_write(i, v) < 0)
440 return 0;
441 }
442
443 return 1;
444}
445
446static int _sta(acpi_handle handle)
447{
448 int status;
449
450 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
451 status = 0;
452
453 return status;
454}
455
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -0300456static int issue_thinkpad_cmos_command(int cmos_cmd)
457{
458 if (!cmos_handle)
459 return -ENXIO;
460
461 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
462 return -EIO;
463
464 return 0;
465}
466
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300467/*************************************************************************
468 * ACPI device model
469 */
470
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200471#define IBM_ACPIHANDLE_INIT(object) \
472 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
473 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
474
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300475static void drv_acpi_handle_init(char *name,
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300476 acpi_handle *handle, acpi_handle parent,
477 char **paths, int num_paths, char **path)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300478{
479 int i;
480 acpi_status status;
481
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300482 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
483 name);
484
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300485 for (i = 0; i < num_paths; i++) {
486 status = acpi_get_handle(parent, paths[i], handle);
487 if (ACPI_SUCCESS(status)) {
488 *path = paths[i];
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300489 dbg_printk(TPACPI_DBG_INIT,
490 "Found ACPI handle %s for %s\n",
491 *path, name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300492 return;
493 }
494 }
495
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300496 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
497 name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300498 *handle = NULL;
499}
500
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300501static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300502{
503 struct ibm_struct *ibm = data;
504
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300505 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
506 return;
507
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300508 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300509 return;
510
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300511 ibm->acpi->notify(ibm, event);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300512}
513
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300514static int __init setup_acpi_notify(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300515{
516 acpi_status status;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300517 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300518
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300519 BUG_ON(!ibm->acpi);
520
521 if (!*ibm->acpi->handle)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300522 return 0;
523
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300524 vdbg_printk(TPACPI_DBG_INIT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300525 "setting up ACPI notify for %s\n", ibm->name);
526
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300527 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
528 if (rc < 0) {
529 printk(IBM_ERR "acpi_bus_get_device(%s) failed: %d\n",
530 ibm->name, rc);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300531 return -ENODEV;
532 }
533
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300534 acpi_driver_data(ibm->acpi->device) = ibm;
535 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300536 IBM_ACPI_EVENT_PREFIX,
537 ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300538
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300539 status = acpi_install_notify_handler(*ibm->acpi->handle,
540 ibm->acpi->type, dispatch_acpi_notify, ibm);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300541 if (ACPI_FAILURE(status)) {
542 if (status == AE_ALREADY_EXISTS) {
543 printk(IBM_NOTICE "another device driver is already handling %s events\n",
544 ibm->name);
545 } else {
546 printk(IBM_ERR "acpi_install_notify_handler(%s) failed: %d\n",
547 ibm->name, status);
548 }
549 return -ENODEV;
550 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300551 ibm->flags.acpi_notify_installed = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300552 return 0;
553}
554
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300555static int __init tpacpi_device_add(struct acpi_device *device)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300556{
557 return 0;
558}
559
Henrique de Moraes Holschuh67001212007-04-21 11:08:25 -0300560static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300561{
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300562 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300563
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300564 dbg_printk(TPACPI_DBG_INIT,
565 "registering %s as an ACPI driver\n", ibm->name);
566
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300567 BUG_ON(!ibm->acpi);
568
569 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
570 if (!ibm->acpi->driver) {
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300571 printk(IBM_ERR "kzalloc(ibm->driver) failed\n");
572 return -ENOMEM;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300573 }
574
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300575 sprintf(ibm->acpi->driver->name, "%s_%s", IBM_NAME, ibm->name);
576 ibm->acpi->driver->ids = ibm->acpi->hid;
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200577
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300578 ibm->acpi->driver->ops.add = &tpacpi_device_add;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300579
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300580 rc = acpi_bus_register_driver(ibm->acpi->driver);
581 if (rc < 0) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300582 printk(IBM_ERR "acpi_bus_register_driver(%s) failed: %d\n",
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200583 ibm->name, rc);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300584 kfree(ibm->acpi->driver);
585 ibm->acpi->driver = NULL;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300586 } else if (!rc)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300587 ibm->flags.acpi_driver_registered = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300588
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300589 return rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300590}
591
592
593/****************************************************************************
594 ****************************************************************************
595 *
596 * Procfs Helpers
597 *
598 ****************************************************************************
599 ****************************************************************************/
600
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300601static int dispatch_procfs_read(char *page, char **start, off_t off,
602 int count, int *eof, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300603{
604 struct ibm_struct *ibm = data;
605 int len;
606
607 if (!ibm || !ibm->read)
608 return -EINVAL;
609
610 len = ibm->read(page);
611 if (len < 0)
612 return len;
613
614 if (len <= off + count)
615 *eof = 1;
616 *start = page + off;
617 len -= off;
618 if (len > count)
619 len = count;
620 if (len < 0)
621 len = 0;
622
623 return len;
624}
625
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300626static int dispatch_procfs_write(struct file *file,
627 const char __user * userbuf,
628 unsigned long count, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300629{
630 struct ibm_struct *ibm = data;
631 char *kernbuf;
632 int ret;
633
634 if (!ibm || !ibm->write)
635 return -EINVAL;
636
637 kernbuf = kmalloc(count + 2, GFP_KERNEL);
638 if (!kernbuf)
639 return -ENOMEM;
640
641 if (copy_from_user(kernbuf, userbuf, count)) {
642 kfree(kernbuf);
643 return -EFAULT;
644 }
645
646 kernbuf[count] = 0;
647 strcat(kernbuf, ",");
648 ret = ibm->write(kernbuf);
649 if (ret == 0)
650 ret = count;
651
652 kfree(kernbuf);
653
654 return ret;
655}
656
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657static char *next_cmd(char **cmds)
658{
659 char *start = *cmds;
660 char *end;
661
662 while ((end = strchr(start, ',')) && end == start)
663 start = end + 1;
664
665 if (!end)
666 return NULL;
667
668 *end = 0;
669 *cmds = end + 1;
670 return start;
671}
672
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300673
674/****************************************************************************
675 ****************************************************************************
676 *
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300677 * Device model: input, hwmon and platform
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300678 *
679 ****************************************************************************
680 ****************************************************************************/
681
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -0300682static struct platform_device *tpacpi_pdev;
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300683static struct platform_device *tpacpi_sensors_pdev;
Tony Jones1beeffe2007-08-20 13:46:20 -0700684static struct device *tpacpi_hwmon;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300685static struct input_dev *tpacpi_inputdev;
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -0300686static struct mutex tpacpi_inputdev_send_mutex;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200687static LIST_HEAD(tpacpi_all_drivers);
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300688
689static int tpacpi_resume_handler(struct platform_device *pdev)
690{
691 struct ibm_struct *ibm, *itmp;
692
693 list_for_each_entry_safe(ibm, itmp,
694 &tpacpi_all_drivers,
695 all_drivers) {
696 if (ibm->resume)
697 (ibm->resume)();
698 }
699
700 return 0;
701}
702
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300703static struct platform_driver tpacpi_pdriver = {
704 .driver = {
705 .name = IBM_DRVR_NAME,
706 .owner = THIS_MODULE,
707 },
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300708 .resume = tpacpi_resume_handler,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300709};
710
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300711static struct platform_driver tpacpi_hwmon_pdriver = {
712 .driver = {
713 .name = IBM_HWMON_DRVR_NAME,
714 .owner = THIS_MODULE,
715 },
716};
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300717
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -0300718/*************************************************************************
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300719 * sysfs support helpers
720 */
721
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200722struct attribute_set {
723 unsigned int members, max_members;
724 struct attribute_group group;
725};
726
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300727struct attribute_set_obj {
728 struct attribute_set s;
729 struct attribute *a;
730} __attribute__((packed));
731
732static struct attribute_set *create_attr_set(unsigned int max_members,
733 const char* name)
734{
735 struct attribute_set_obj *sobj;
736
737 if (max_members == 0)
738 return NULL;
739
740 /* Allocates space for implicit NULL at the end too */
741 sobj = kzalloc(sizeof(struct attribute_set_obj) +
742 max_members * sizeof(struct attribute *),
743 GFP_KERNEL);
744 if (!sobj)
745 return NULL;
746 sobj->s.max_members = max_members;
747 sobj->s.group.attrs = &sobj->a;
748 sobj->s.group.name = name;
749
750 return &sobj->s;
751}
752
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200753#define destroy_attr_set(_set) \
754 kfree(_set);
755
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300756/* not multi-threaded safe, use it in a single thread per set */
757static int add_to_attr_set(struct attribute_set* s, struct attribute *attr)
758{
759 if (!s || !attr)
760 return -EINVAL;
761
762 if (s->members >= s->max_members)
763 return -ENOMEM;
764
765 s->group.attrs[s->members] = attr;
766 s->members++;
767
768 return 0;
769}
770
771static int add_many_to_attr_set(struct attribute_set* s,
772 struct attribute **attr,
773 unsigned int count)
774{
775 int i, res;
776
777 for (i = 0; i < count; i++) {
778 res = add_to_attr_set(s, attr[i]);
779 if (res)
780 return res;
781 }
782
783 return 0;
784}
785
786static void delete_attr_set(struct attribute_set* s, struct kobject *kobj)
787{
788 sysfs_remove_group(kobj, &s->group);
789 destroy_attr_set(s);
790}
791
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200792#define register_attr_set_with_sysfs(_attr_set, _kobj) \
793 sysfs_create_group(_kobj, &_attr_set->group)
794
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300795static int parse_strtoul(const char *buf,
796 unsigned long max, unsigned long *value)
797{
798 char *endp;
799
Henrique de Moraes Holschuh32afbf02007-10-08 10:12:56 -0300800 while (*buf && isspace(*buf))
801 buf++;
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300802 *value = simple_strtoul(buf, &endp, 0);
803 while (*endp && isspace(*endp))
804 endp++;
805 if (*endp || *value > max)
806 return -EINVAL;
807
808 return 0;
809}
810
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200811/*************************************************************************
812 * thinkpad-acpi driver attributes
813 */
814
815/* interface_version --------------------------------------------------- */
816static ssize_t tpacpi_driver_interface_version_show(
817 struct device_driver *drv,
818 char *buf)
819{
820 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
821}
822
823static DRIVER_ATTR(interface_version, S_IRUGO,
824 tpacpi_driver_interface_version_show, NULL);
825
826/* debug_level --------------------------------------------------------- */
827static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
828 char *buf)
829{
830 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
831}
832
833static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
834 const char *buf, size_t count)
835{
836 unsigned long t;
837
838 if (parse_strtoul(buf, 0xffff, &t))
839 return -EINVAL;
840
841 dbg_level = t;
842
843 return count;
844}
845
846static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
847 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
848
849/* version ------------------------------------------------------------- */
850static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
851 char *buf)
852{
853 return snprintf(buf, PAGE_SIZE, "%s v%s\n", IBM_DESC, IBM_VERSION);
854}
855
856static DRIVER_ATTR(version, S_IRUGO,
857 tpacpi_driver_version_show, NULL);
858
859/* --------------------------------------------------------------------- */
860
861static struct driver_attribute* tpacpi_driver_attributes[] = {
862 &driver_attr_debug_level, &driver_attr_version,
863 &driver_attr_interface_version,
864};
865
866static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
867{
868 int i, res;
869
870 i = 0;
871 res = 0;
872 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
873 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
874 i++;
875 }
876
877 return res;
878}
879
880static void tpacpi_remove_driver_attributes(struct device_driver *drv)
881{
882 int i;
883
884 for(i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
885 driver_remove_file(drv, tpacpi_driver_attributes[i]);
886}
887
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300888/****************************************************************************
889 ****************************************************************************
890 *
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300891 * Subdrivers
892 *
893 ****************************************************************************
894 ****************************************************************************/
895
896/*************************************************************************
Henrique de Moraes Holschuh142cfc92007-04-21 11:08:26 -0300897 * thinkpad-acpi init subdriver
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300898 */
899
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300900static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901{
902 printk(IBM_INFO "%s v%s\n", IBM_DESC, IBM_VERSION);
903 printk(IBM_INFO "%s\n", IBM_URL);
904
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -0300905 printk(IBM_INFO "ThinkPad BIOS %s, EC %s\n",
906 (thinkpad_id.bios_version_str) ?
907 thinkpad_id.bios_version_str : "unknown",
908 (thinkpad_id.ec_version_str) ?
909 thinkpad_id.ec_version_str : "unknown");
910
911 if (thinkpad_id.vendor && thinkpad_id.model_str)
912 printk(IBM_INFO "%s %s\n",
913 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
914 "IBM" : ((thinkpad_id.vendor ==
915 PCI_VENDOR_ID_LENOVO) ?
916 "Lenovo" : "Unknown vendor"),
917 thinkpad_id.model_str);
Henrique de Moraes Holschuh3945ac32007-02-06 19:13:44 -0200918
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 return 0;
920}
921
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300922static int thinkpad_acpi_driver_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923{
924 int len = 0;
925
926 len += sprintf(p + len, "driver:\t\t%s\n", IBM_DESC);
927 len += sprintf(p + len, "version:\t%s\n", IBM_VERSION);
928
929 return len;
930}
931
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300932static struct ibm_struct thinkpad_acpi_driver_data = {
933 .name = "driver",
934 .read = thinkpad_acpi_driver_read,
935};
936
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300937/*************************************************************************
938 * Hotkey subdriver
939 */
940
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200941enum { /* hot key scan codes (derived from ACPI DSDT) */
942 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
943 TP_ACPI_HOTKEYSCAN_FNF2,
944 TP_ACPI_HOTKEYSCAN_FNF3,
945 TP_ACPI_HOTKEYSCAN_FNF4,
946 TP_ACPI_HOTKEYSCAN_FNF5,
947 TP_ACPI_HOTKEYSCAN_FNF6,
948 TP_ACPI_HOTKEYSCAN_FNF7,
949 TP_ACPI_HOTKEYSCAN_FNF8,
950 TP_ACPI_HOTKEYSCAN_FNF9,
951 TP_ACPI_HOTKEYSCAN_FNF10,
952 TP_ACPI_HOTKEYSCAN_FNF11,
953 TP_ACPI_HOTKEYSCAN_FNF12,
954 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
955 TP_ACPI_HOTKEYSCAN_FNINSERT,
956 TP_ACPI_HOTKEYSCAN_FNDELETE,
957 TP_ACPI_HOTKEYSCAN_FNHOME,
958 TP_ACPI_HOTKEYSCAN_FNEND,
959 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
960 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
961 TP_ACPI_HOTKEYSCAN_FNSPACE,
962 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
963 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
964 TP_ACPI_HOTKEYSCAN_MUTE,
965 TP_ACPI_HOTKEYSCAN_THINKPAD,
966};
967
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -0200968enum { /* Keys available through NVRAM polling */
969 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
970 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
971};
972
973enum { /* Positions of some of the keys in hotkey masks */
974 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
975 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
976 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
977 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
978 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
979 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
980 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
981 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
982 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
983 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
984 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
985};
986
987enum { /* NVRAM to ACPI HKEY group map */
988 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
989 TP_ACPI_HKEY_ZOOM_MASK |
990 TP_ACPI_HKEY_DISPSWTCH_MASK |
991 TP_ACPI_HKEY_HIBERNATE_MASK,
992 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
993 TP_ACPI_HKEY_BRGHTDWN_MASK,
994 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
995 TP_ACPI_HKEY_VOLDWN_MASK |
996 TP_ACPI_HKEY_MUTE_MASK,
997};
998
999#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1000struct tp_nvram_state {
1001 u16 thinkpad_toggle:1;
1002 u16 zoom_toggle:1;
1003 u16 display_toggle:1;
1004 u16 thinklight_toggle:1;
1005 u16 hibernate_toggle:1;
1006 u16 displayexp_toggle:1;
1007 u16 display_state:1;
1008 u16 brightness_toggle:1;
1009 u16 volume_toggle:1;
1010 u16 mute:1;
1011
1012 u8 brightness_level;
1013 u8 volume_level;
1014};
1015
1016static struct task_struct *tpacpi_hotkey_task;
1017static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1018static int hotkey_poll_freq = 10; /* Hz */
1019static struct mutex hotkey_thread_mutex;
1020static struct mutex hotkey_thread_data_mutex;
1021static unsigned int hotkey_config_change;
1022
1023#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1024
1025#define hotkey_source_mask 0U
1026
1027#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1028
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001029static struct mutex hotkey_mutex;
1030
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001031static int hotkey_orig_status;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001032static u32 hotkey_orig_mask;
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001033static u32 hotkey_all_mask;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001034static u32 hotkey_reserved_mask;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001035static u32 hotkey_mask;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001036
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001037static unsigned int hotkey_report_mode;
1038
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001039static u16 *hotkey_keycode_map;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001040
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03001041static struct attribute_set *hotkey_dev_attributes;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001042
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001043#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1044#define HOTKEY_CONFIG_CRITICAL_START \
1045 mutex_lock(&hotkey_thread_data_mutex); \
1046 hotkey_config_change++;
1047#define HOTKEY_CONFIG_CRITICAL_END \
1048 mutex_unlock(&hotkey_thread_data_mutex);
1049#else
1050#define HOTKEY_CONFIG_CRITICAL_START
1051#define HOTKEY_CONFIG_CRITICAL_END
1052#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1053
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001054static int hotkey_get_wlsw(int *status)
1055{
1056 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1057 return -EIO;
1058 return 0;
1059}
1060
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001061/*
1062 * Call with hotkey_mutex held
1063 */
1064static int hotkey_mask_get(void)
1065{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001066 u32 m = 0;
1067
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001068 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001069 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001070 return -EIO;
1071 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001072 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001073
1074 return 0;
1075}
1076
1077/*
1078 * Call with hotkey_mutex held
1079 */
1080static int hotkey_mask_set(u32 mask)
1081{
1082 int i;
1083 int rc = 0;
1084
1085 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001086 HOTKEY_CONFIG_CRITICAL_START
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001087 for (i = 0; i < 32; i++) {
1088 u32 m = 1 << i;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001089 /* enable in firmware mask only keys not in NVRAM
1090 * mode, but enable the key in the cached hotkey_mask
1091 * regardless of mode, or the key will end up
1092 * disabled by hotkey_mask_get() */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001093 if (!acpi_evalf(hkey_handle,
1094 NULL, "MHKM", "vdd", i + 1,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001095 !!((mask & ~hotkey_source_mask) & m))) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001096 rc = -EIO;
1097 break;
1098 } else {
1099 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1100 }
1101 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001102 HOTKEY_CONFIG_CRITICAL_END
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001103
1104 /* hotkey_mask_get must be called unconditionally below */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001105 if (!hotkey_mask_get() && !rc &&
1106 (hotkey_mask & ~hotkey_source_mask) !=
1107 (mask & ~hotkey_source_mask)) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001108 printk(IBM_NOTICE
1109 "requested hot key mask 0x%08x, but "
1110 "firmware forced it to 0x%08x\n",
1111 mask, hotkey_mask);
1112 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001113 } else {
1114#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1115 HOTKEY_CONFIG_CRITICAL_START
1116 hotkey_mask = mask & hotkey_source_mask;
1117 HOTKEY_CONFIG_CRITICAL_END
1118 hotkey_mask_get();
1119 if (hotkey_mask != mask) {
1120 printk(IBM_NOTICE
1121 "requested hot key mask 0x%08x, "
1122 "forced to 0x%08x (NVRAM poll mask is "
1123 "0x%08x): no firmware mask support\n",
1124 mask, hotkey_mask, hotkey_source_mask);
1125 }
1126#else
1127 hotkey_mask_get();
1128 rc = -ENXIO;
1129#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001130 }
1131
1132 return rc;
1133}
1134
1135static int hotkey_status_get(int *status)
1136{
1137 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1138 return -EIO;
1139
1140 return 0;
1141}
1142
1143static int hotkey_status_set(int status)
1144{
1145 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
1146 return -EIO;
1147
1148 return 0;
1149}
1150
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02001151static void tpacpi_input_send_radiosw(void)
1152{
1153 int wlsw;
1154
1155 mutex_lock(&tpacpi_inputdev_send_mutex);
1156
1157 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
1158 input_report_switch(tpacpi_inputdev,
1159 SW_RADIO, !!wlsw);
1160 input_sync(tpacpi_inputdev);
1161 }
1162
1163 mutex_unlock(&tpacpi_inputdev_send_mutex);
1164}
1165
1166static void tpacpi_input_send_key(unsigned int scancode)
1167{
1168 unsigned int keycode;
1169
1170 keycode = hotkey_keycode_map[scancode];
1171
1172 if (keycode != KEY_RESERVED) {
1173 mutex_lock(&tpacpi_inputdev_send_mutex);
1174
1175 input_report_key(tpacpi_inputdev, keycode, 1);
1176 if (keycode == KEY_UNKNOWN)
1177 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1178 scancode);
1179 input_sync(tpacpi_inputdev);
1180
1181 input_report_key(tpacpi_inputdev, keycode, 0);
1182 if (keycode == KEY_UNKNOWN)
1183 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1184 scancode);
1185 input_sync(tpacpi_inputdev);
1186
1187 mutex_unlock(&tpacpi_inputdev_send_mutex);
1188 }
1189}
1190
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001191#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1192static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1193
1194static void tpacpi_hotkey_send_key(unsigned int scancode)
1195{
1196 tpacpi_input_send_key(scancode);
1197 if (hotkey_report_mode < 2) {
1198 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1199 0x80, 0x1001 + scancode);
1200 }
1201}
1202
1203static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1204{
1205 u8 d;
1206
1207 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1208 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1209 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1210 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1211 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1212 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1213 }
1214 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1215 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1216 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1217 }
1218 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1219 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1220 n->displayexp_toggle =
1221 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1222 }
1223 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1224 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1225 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1226 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1227 n->brightness_toggle =
1228 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1229 }
1230 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1231 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1232 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1233 >> TP_NVRAM_POS_LEVEL_VOLUME;
1234 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1235 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1236 }
1237}
1238
1239#define TPACPI_COMPARE_KEY(__scancode, __member) \
1240 do { if ((mask & (1 << __scancode)) && oldn->__member != newn->__member) \
1241 tpacpi_hotkey_send_key(__scancode); } while (0)
1242
1243#define TPACPI_MAY_SEND_KEY(__scancode) \
1244 do { if (mask & (1 << __scancode)) \
1245 tpacpi_hotkey_send_key(__scancode); } while (0)
1246
1247static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
1248 struct tp_nvram_state *newn,
1249 u32 mask)
1250{
1251 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1252 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1253 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1254 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1255
1256 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1257
1258 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1259
1260 /* handle volume */
1261 if (oldn->volume_toggle != newn->volume_toggle) {
1262 if (oldn->mute != newn->mute) {
1263 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1264 }
1265 if (oldn->volume_level > newn->volume_level) {
1266 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1267 } else if (oldn->volume_level < newn->volume_level) {
1268 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1269 } else if (oldn->mute == newn->mute) {
1270 /* repeated key presses that didn't change state */
1271 if (newn->mute) {
1272 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1273 } else if (newn->volume_level != 0) {
1274 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1275 } else {
1276 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1277 }
1278 }
1279 }
1280
1281 /* handle brightness */
1282 if (oldn->brightness_toggle != newn->brightness_toggle) {
1283 if (oldn->brightness_level < newn->brightness_level) {
1284 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1285 } else if (oldn->brightness_level > newn->brightness_level) {
1286 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1287 } else {
1288 /* repeated key presses that didn't change state */
1289 if (newn->brightness_level != 0) {
1290 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1291 } else {
1292 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1293 }
1294 }
1295 }
1296}
1297
1298#undef TPACPI_COMPARE_KEY
1299#undef TPACPI_MAY_SEND_KEY
1300
1301static int hotkey_kthread(void *data)
1302{
1303 struct tp_nvram_state s[2];
1304 u32 mask;
1305 unsigned int si, so;
1306 unsigned long t;
1307 unsigned int change_detector, must_reset;
1308
1309 mutex_lock(&hotkey_thread_mutex);
1310
1311 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1312 goto exit;
1313
1314 set_freezable();
1315
1316 so = 0;
1317 si = 1;
1318 t = 0;
1319
1320 /* Initial state for compares */
1321 mutex_lock(&hotkey_thread_data_mutex);
1322 change_detector = hotkey_config_change;
1323 mask = hotkey_source_mask & hotkey_mask;
1324 mutex_unlock(&hotkey_thread_data_mutex);
1325 hotkey_read_nvram(&s[so], mask);
1326
1327 while (!kthread_should_stop() && hotkey_poll_freq) {
1328 if (t == 0)
1329 t = 1000/hotkey_poll_freq;
1330 t = msleep_interruptible(t);
1331 if (unlikely(kthread_should_stop()))
1332 break;
1333 must_reset = try_to_freeze();
1334 if (t > 0 && !must_reset)
1335 continue;
1336
1337 mutex_lock(&hotkey_thread_data_mutex);
1338 if (must_reset || hotkey_config_change != change_detector) {
1339 /* forget old state on thaw or config change */
1340 si = so;
1341 t = 0;
1342 change_detector = hotkey_config_change;
1343 }
1344 mask = hotkey_source_mask & hotkey_mask;
1345 mutex_unlock(&hotkey_thread_data_mutex);
1346
1347 if (likely(mask)) {
1348 hotkey_read_nvram(&s[si], mask);
1349 if (likely(si != so)) {
1350 hotkey_compare_and_issue_event(&s[so], &s[si],
1351 mask);
1352 }
1353 }
1354
1355 so = si;
1356 si ^= 1;
1357 }
1358
1359exit:
1360 mutex_unlock(&hotkey_thread_mutex);
1361 return 0;
1362}
1363
1364static void hotkey_poll_stop_sync(void)
1365{
1366 if (tpacpi_hotkey_task) {
1367 if (frozen(tpacpi_hotkey_task) ||
1368 freezing(tpacpi_hotkey_task))
1369 thaw_process(tpacpi_hotkey_task);
1370
1371 kthread_stop(tpacpi_hotkey_task);
1372 tpacpi_hotkey_task = NULL;
1373 mutex_lock(&hotkey_thread_mutex);
1374 /* at this point, the thread did exit */
1375 mutex_unlock(&hotkey_thread_mutex);
1376 }
1377}
1378
1379/* call with hotkey_mutex held */
1380static void hotkey_poll_setup(int may_warn)
1381{
1382 if ((hotkey_source_mask & hotkey_mask) != 0 &&
1383 hotkey_poll_freq > 0 &&
1384 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1385 if (!tpacpi_hotkey_task) {
1386 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
1387 NULL, IBM_FILE "d");
1388 if (IS_ERR(tpacpi_hotkey_task)) {
1389 tpacpi_hotkey_task = NULL;
1390 printk(IBM_ERR "could not create kernel thread "
1391 "for hotkey polling\n");
1392 }
1393 }
1394 } else {
1395 hotkey_poll_stop_sync();
1396 if (may_warn &&
1397 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
1398 printk(IBM_NOTICE "hot keys 0x%08x require polling, "
1399 "which is currently disabled\n",
1400 hotkey_source_mask);
1401 }
1402 }
1403}
1404
1405static void hotkey_poll_setup_safe(int may_warn)
1406{
1407 mutex_lock(&hotkey_mutex);
1408 hotkey_poll_setup(may_warn);
1409 mutex_unlock(&hotkey_mutex);
1410}
1411
1412static int hotkey_inputdev_open(struct input_dev *dev)
1413{
1414 switch (tpacpi_lifecycle) {
1415 case TPACPI_LIFE_INIT:
1416 /*
1417 * hotkey_init will call hotkey_poll_setup_safe
1418 * at the appropriate moment
1419 */
1420 return 0;
1421 case TPACPI_LIFE_EXITING:
1422 return -EBUSY;
1423 case TPACPI_LIFE_RUNNING:
1424 hotkey_poll_setup_safe(0);
1425 return 0;
1426 }
1427
1428 /* Should only happen if tpacpi_lifecycle is corrupt */
1429 BUG();
1430 return -EBUSY;
1431}
1432
1433static void hotkey_inputdev_close(struct input_dev *dev)
1434{
1435 /* disable hotkey polling when possible */
1436 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1437 hotkey_poll_setup_safe(0);
1438}
1439#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1440
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001441/* sysfs hotkey enable ------------------------------------------------- */
1442static ssize_t hotkey_enable_show(struct device *dev,
1443 struct device_attribute *attr,
1444 char *buf)
1445{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001446 int res, status;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001447
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001448 res = hotkey_status_get(&status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001449 if (res)
1450 return res;
1451
1452 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1453}
1454
1455static ssize_t hotkey_enable_store(struct device *dev,
1456 struct device_attribute *attr,
1457 const char *buf, size_t count)
1458{
1459 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001460 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001461
1462 if (parse_strtoul(buf, 1, &t))
1463 return -EINVAL;
1464
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001465 res = hotkey_status_set(t);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001466
1467 return (res) ? res : count;
1468}
1469
1470static struct device_attribute dev_attr_hotkey_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001471 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001472 hotkey_enable_show, hotkey_enable_store);
1473
1474/* sysfs hotkey mask --------------------------------------------------- */
1475static ssize_t hotkey_mask_show(struct device *dev,
1476 struct device_attribute *attr,
1477 char *buf)
1478{
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001479 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001480
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001481 if (mutex_lock_interruptible(&hotkey_mutex))
1482 return -ERESTARTSYS;
1483 res = hotkey_mask_get();
1484 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001485
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001486 return (res)?
1487 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001488}
1489
1490static ssize_t hotkey_mask_store(struct device *dev,
1491 struct device_attribute *attr,
1492 const char *buf, size_t count)
1493{
1494 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001495 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001496
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001497 if (parse_strtoul(buf, 0xffffffffUL, &t))
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001498 return -EINVAL;
1499
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001500 if (mutex_lock_interruptible(&hotkey_mutex))
1501 return -ERESTARTSYS;
1502
1503 res = hotkey_mask_set(t);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001504
1505#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1506 hotkey_poll_setup(1);
1507#endif
1508
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001509 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001510
1511 return (res) ? res : count;
1512}
1513
1514static struct device_attribute dev_attr_hotkey_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001515 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001516 hotkey_mask_show, hotkey_mask_store);
1517
1518/* sysfs hotkey bios_enabled ------------------------------------------- */
1519static ssize_t hotkey_bios_enabled_show(struct device *dev,
1520 struct device_attribute *attr,
1521 char *buf)
1522{
1523 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
1524}
1525
1526static struct device_attribute dev_attr_hotkey_bios_enabled =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001527 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001528
1529/* sysfs hotkey bios_mask ---------------------------------------------- */
1530static ssize_t hotkey_bios_mask_show(struct device *dev,
1531 struct device_attribute *attr,
1532 char *buf)
1533{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001534 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001535}
1536
1537static struct device_attribute dev_attr_hotkey_bios_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001538 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001539
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001540/* sysfs hotkey all_mask ----------------------------------------------- */
1541static ssize_t hotkey_all_mask_show(struct device *dev,
1542 struct device_attribute *attr,
1543 char *buf)
1544{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001545 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1546 hotkey_all_mask | hotkey_source_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001547}
1548
1549static struct device_attribute dev_attr_hotkey_all_mask =
1550 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
1551
1552/* sysfs hotkey recommended_mask --------------------------------------- */
1553static ssize_t hotkey_recommended_mask_show(struct device *dev,
1554 struct device_attribute *attr,
1555 char *buf)
1556{
1557 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001558 (hotkey_all_mask | hotkey_source_mask)
1559 & ~hotkey_reserved_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001560}
1561
1562static struct device_attribute dev_attr_hotkey_recommended_mask =
1563 __ATTR(hotkey_recommended_mask, S_IRUGO,
1564 hotkey_recommended_mask_show, NULL);
1565
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001566#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1567
1568/* sysfs hotkey hotkey_source_mask ------------------------------------- */
1569static ssize_t hotkey_source_mask_show(struct device *dev,
1570 struct device_attribute *attr,
1571 char *buf)
1572{
1573 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
1574}
1575
1576static ssize_t hotkey_source_mask_store(struct device *dev,
1577 struct device_attribute *attr,
1578 const char *buf, size_t count)
1579{
1580 unsigned long t;
1581
1582 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
1583 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
1584 return -EINVAL;
1585
1586 if (mutex_lock_interruptible(&hotkey_mutex))
1587 return -ERESTARTSYS;
1588
1589 HOTKEY_CONFIG_CRITICAL_START
1590 hotkey_source_mask = t;
1591 HOTKEY_CONFIG_CRITICAL_END
1592
1593 hotkey_poll_setup(1);
1594
1595 mutex_unlock(&hotkey_mutex);
1596
1597 return count;
1598}
1599
1600static struct device_attribute dev_attr_hotkey_source_mask =
1601 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
1602 hotkey_source_mask_show, hotkey_source_mask_store);
1603
1604/* sysfs hotkey hotkey_poll_freq --------------------------------------- */
1605static ssize_t hotkey_poll_freq_show(struct device *dev,
1606 struct device_attribute *attr,
1607 char *buf)
1608{
1609 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
1610}
1611
1612static ssize_t hotkey_poll_freq_store(struct device *dev,
1613 struct device_attribute *attr,
1614 const char *buf, size_t count)
1615{
1616 unsigned long t;
1617
1618 if (parse_strtoul(buf, 25, &t))
1619 return -EINVAL;
1620
1621 if (mutex_lock_interruptible(&hotkey_mutex))
1622 return -ERESTARTSYS;
1623
1624 hotkey_poll_freq = t;
1625
1626 hotkey_poll_setup(1);
1627 mutex_unlock(&hotkey_mutex);
1628
1629 return count;
1630}
1631
1632static struct device_attribute dev_attr_hotkey_poll_freq =
1633 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
1634 hotkey_poll_freq_show, hotkey_poll_freq_store);
1635
1636#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1637
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001638/* sysfs hotkey radio_sw ----------------------------------------------- */
1639static ssize_t hotkey_radio_sw_show(struct device *dev,
1640 struct device_attribute *attr,
1641 char *buf)
1642{
1643 int res, s;
1644 res = hotkey_get_wlsw(&s);
1645 if (res < 0)
1646 return res;
1647
1648 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1649}
1650
1651static struct device_attribute dev_attr_hotkey_radio_sw =
1652 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
1653
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001654/* sysfs hotkey report_mode -------------------------------------------- */
1655static ssize_t hotkey_report_mode_show(struct device *dev,
1656 struct device_attribute *attr,
1657 char *buf)
1658{
1659 return snprintf(buf, PAGE_SIZE, "%d\n",
1660 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
1661}
1662
1663static struct device_attribute dev_attr_hotkey_report_mode =
1664 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
1665
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001666/* --------------------------------------------------------------------- */
1667
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001668static struct attribute *hotkey_attributes[] __initdata = {
1669 &dev_attr_hotkey_enable.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001670 &dev_attr_hotkey_bios_enabled.attr,
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001671 &dev_attr_hotkey_report_mode.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001672#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1673 &dev_attr_hotkey_mask.attr,
1674 &dev_attr_hotkey_all_mask.attr,
1675 &dev_attr_hotkey_recommended_mask.attr,
1676 &dev_attr_hotkey_source_mask.attr,
1677 &dev_attr_hotkey_poll_freq.attr,
1678#endif
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001679};
1680
1681static struct attribute *hotkey_mask_attributes[] __initdata = {
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001682 &dev_attr_hotkey_bios_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001683#ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1684 &dev_attr_hotkey_mask.attr,
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001685 &dev_attr_hotkey_all_mask.attr,
1686 &dev_attr_hotkey_recommended_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001687#endif
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001688};
1689
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001690static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001691{
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001692 /* Requirements for changing the default keymaps:
1693 *
1694 * 1. Many of the keys are mapped to KEY_RESERVED for very
1695 * good reasons. Do not change them unless you have deep
1696 * knowledge on the IBM and Lenovo ThinkPad firmware for
1697 * the various ThinkPad models. The driver behaves
1698 * differently for KEY_RESERVED: such keys have their
1699 * hot key mask *unset* in mask_recommended, and also
1700 * in the initial hot key mask programmed into the
1701 * firmware at driver load time, which means the firm-
1702 * ware may react very differently if you change them to
1703 * something else;
1704 *
1705 * 2. You must be subscribed to the linux-thinkpad and
1706 * ibm-acpi-devel mailing lists, and you should read the
1707 * list archives since 2007 if you want to change the
1708 * keymaps. This requirement exists so that you will
1709 * know the past history of problems with the thinkpad-
1710 * acpi driver keymaps, and also that you will be
1711 * listening to any bug reports;
1712 *
1713 * 3. Do not send thinkpad-acpi specific patches directly to
1714 * for merging, *ever*. Send them to the linux-acpi
1715 * mailinglist for comments. Merging is to be done only
1716 * through acpi-test and the ACPI maintainer.
1717 *
1718 * If the above is too much to ask, don't change the keymap.
1719 * Ask the thinkpad-acpi maintainer to do it, instead.
1720 */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001721 static u16 ibm_keycode_map[] __initdata = {
1722 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1723 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
1724 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1725 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001726
1727 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001728 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1729 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1730 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001731
1732 /* brightness: firmware always reacts to them, unless
1733 * X.org did some tricks in the radeon BIOS scratch
1734 * registers of *some* models */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02001735 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02001736 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001737
1738 /* Thinklight: firmware always react to it */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001739 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001740
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001741 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
1742 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001743
1744 /* Volume: firmware always react to it and reprograms
1745 * the built-in *extra* mixer. Never map it to control
1746 * another mixer by default. */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02001747 KEY_RESERVED, /* 0x14: VOLUME UP */
1748 KEY_RESERVED, /* 0x15: VOLUME DOWN */
1749 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001750
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001751 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001752
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001753 /* (assignments unknown, please report if found) */
1754 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1755 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1756 };
1757 static u16 lenovo_keycode_map[] __initdata = {
1758 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1759 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
1760 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1761 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001762
1763 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001764 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1765 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1766 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001767
Henrique de Moraes Holschuh56a185b2007-12-13 12:14:09 -02001768 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuh56a185b2007-12-13 12:14:09 -02001769 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001770
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001771 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001772
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001773 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
1774 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001775
1776 /* Volume: z60/z61, T60 (BIOS version?): firmware always
1777 * react to it and reprograms the built-in *extra* mixer.
1778 * Never map it to control another mixer by default.
1779 *
1780 * T60?, T61, R60?, R61: firmware and EC tries to send
1781 * these over the regular keyboard, so these are no-ops,
1782 * but there are still weird bugs re. MUTE, so do not
1783 * change unless you get test reports from all Lenovo
1784 * models. May cause the BIOS to interfere with the
1785 * HDA mixer.
1786 */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02001787 KEY_RESERVED, /* 0x14: VOLUME UP */
1788 KEY_RESERVED, /* 0x15: VOLUME DOWN */
1789 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001790
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001791 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001792
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001793 /* (assignments unknown, please report if found) */
1794 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1795 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1796 };
1797
1798#define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
1799#define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
1800#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
1801
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001802 int res, i;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001803 int status;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001804 int hkeyv;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001805
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001806 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
1807
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001808 BUG_ON(!tpacpi_inputdev);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001809 BUG_ON(tpacpi_inputdev->open != NULL ||
1810 tpacpi_inputdev->close != NULL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001811
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03001812 IBM_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03001813 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001814
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001815#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1816 mutex_init(&hotkey_thread_mutex);
1817 mutex_init(&hotkey_thread_data_mutex);
1818#endif
1819
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001820 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001821 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001822
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001823 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001824 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001825
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001826 if (tp_features.hotkey) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001827 hotkey_dev_attributes = create_attr_set(10, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001828 if (!hotkey_dev_attributes)
1829 return -ENOMEM;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001830 res = add_many_to_attr_set(hotkey_dev_attributes,
1831 hotkey_attributes,
1832 ARRAY_SIZE(hotkey_attributes));
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001833 if (res)
1834 return res;
1835
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001836 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001837 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
1838 for HKEY interface version 0x100 */
1839 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
1840 if ((hkeyv >> 8) != 1) {
1841 printk(IBM_ERR "unknown version of the "
1842 "HKEY interface: 0x%x\n", hkeyv);
1843 printk(IBM_ERR "please report this to %s\n",
1844 IBM_MAIL);
1845 } else {
1846 /*
1847 * MHKV 0x100 in A31, R40, R40e,
1848 * T4x, X31, and later
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001849 */
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001850 tp_features.hotkey_mask = 1;
1851 }
1852 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001853
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001854 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001855 str_supported(tp_features.hotkey_mask));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03001856
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001857 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001858 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001859 "MHKA", "qd")) {
1860 printk(IBM_ERR
1861 "missing MHKA handler, "
1862 "please report this to %s\n",
1863 IBM_MAIL);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001864 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03001865 }
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001866 }
1867
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001868 /* hotkey_source_mask *must* be zero for
1869 * the first hotkey_mask_get */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001870 res = hotkey_status_get(&hotkey_orig_status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001871 if (!res && tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001872 res = hotkey_mask_get();
1873 hotkey_orig_mask = hotkey_mask;
1874 if (!res) {
1875 res = add_many_to_attr_set(
1876 hotkey_dev_attributes,
1877 hotkey_mask_attributes,
1878 ARRAY_SIZE(hotkey_mask_attributes));
1879 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001880 }
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001881
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001882#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1883 if (tp_features.hotkey_mask) {
1884 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
1885 & ~hotkey_all_mask;
1886 } else {
1887 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
1888 }
1889
1890 vdbg_printk(TPACPI_DBG_INIT,
1891 "hotkey source mask 0x%08x, polling freq %d\n",
1892 hotkey_source_mask, hotkey_poll_freq);
1893#endif
1894
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001895 /* Not all thinkpads have a hardware radio switch */
1896 if (!res && acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
1897 tp_features.hotkey_wlsw = 1;
1898 printk(IBM_INFO
1899 "radio switch found; radios are %s\n",
1900 enabled(status, 0));
1901 res = add_to_attr_set(hotkey_dev_attributes,
1902 &dev_attr_hotkey_radio_sw.attr);
1903 }
1904
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001905 if (!res)
1906 res = register_attr_set_with_sysfs(
1907 hotkey_dev_attributes,
1908 &tpacpi_pdev->dev.kobj);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03001909 if (res)
1910 return res;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001911
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001912 /* Set up key map */
1913
1914 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
1915 GFP_KERNEL);
1916 if (!hotkey_keycode_map) {
1917 printk(IBM_ERR "failed to allocate memory for key map\n");
1918 return -ENOMEM;
1919 }
1920
1921 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
1922 dbg_printk(TPACPI_DBG_INIT,
1923 "using Lenovo default hot key map\n");
1924 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
1925 TPACPI_HOTKEY_MAP_SIZE);
1926 } else {
1927 dbg_printk(TPACPI_DBG_INIT,
1928 "using IBM default hot key map\n");
1929 memcpy(hotkey_keycode_map, &ibm_keycode_map,
1930 TPACPI_HOTKEY_MAP_SIZE);
1931 }
1932
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001933 set_bit(EV_KEY, tpacpi_inputdev->evbit);
1934 set_bit(EV_MSC, tpacpi_inputdev->evbit);
1935 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001936 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
1937 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
1938 tpacpi_inputdev->keycode = hotkey_keycode_map;
1939 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001940 if (hotkey_keycode_map[i] != KEY_RESERVED) {
1941 set_bit(hotkey_keycode_map[i],
1942 tpacpi_inputdev->keybit);
1943 } else {
1944 if (i < sizeof(hotkey_reserved_mask)*8)
1945 hotkey_reserved_mask |= 1 << i;
1946 }
1947 }
1948
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03001949 if (tp_features.hotkey_wlsw) {
1950 set_bit(EV_SW, tpacpi_inputdev->evbit);
1951 set_bit(SW_RADIO, tpacpi_inputdev->swbit);
1952 }
1953
Henrique de Moraes Holschuh1a343762007-07-18 23:45:36 -03001954 dbg_printk(TPACPI_DBG_INIT,
1955 "enabling hot key handling\n");
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001956 res = hotkey_status_set(1);
1957 if (res)
1958 return res;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001959 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
1960 & ~hotkey_reserved_mask)
Henrique de Moraes Holschuh1a343762007-07-18 23:45:36 -03001961 | hotkey_orig_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001962 if (res < 0 && res != -ENXIO)
Henrique de Moraes Holschuh1a343762007-07-18 23:45:36 -03001963 return res;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001964
1965 dbg_printk(TPACPI_DBG_INIT,
1966 "legacy hot key reporting over procfs %s\n",
1967 (hotkey_report_mode < 2) ?
1968 "enabled" : "disabled");
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001969
1970#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1971 tpacpi_inputdev->open = &hotkey_inputdev_open;
1972 tpacpi_inputdev->close = &hotkey_inputdev_close;
1973
1974 hotkey_poll_setup_safe(1);
1975#endif
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001976 }
1977
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001978 return (tp_features.hotkey)? 0 : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001979}
1980
1981static void hotkey_exit(void)
1982{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001983#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1984 hotkey_poll_stop_sync();
1985#endif
1986
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03001987 if (tp_features.hotkey) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001988 dbg_printk(TPACPI_DBG_EXIT, "restoring original hot key mask\n");
1989 /* no short-circuit boolean operator below! */
1990 if ((hotkey_mask_set(hotkey_orig_mask) |
1991 hotkey_status_set(hotkey_orig_status)) != 0)
1992 printk(IBM_ERR "failed to restore hot key mask to BIOS defaults\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03001993 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001994
1995 if (hotkey_dev_attributes) {
1996 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
1997 hotkey_dev_attributes = NULL;
1998 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001999}
2000
2001static void hotkey_notify(struct ibm_struct *ibm, u32 event)
2002{
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002003 u32 hkey;
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002004 unsigned int scancode;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002005 int send_acpi_ev;
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002006 int ignore_acpi_ev;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002007
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002008 if (event != 0x80) {
2009 printk(IBM_ERR "unknown HKEY notification event %d\n", event);
2010 /* forward it to userspace, maybe it knows how to handle it */
2011 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
2012 ibm->acpi->device->dev.bus_id,
2013 event, 0);
2014 return;
2015 }
2016
2017 while (1) {
2018 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
2019 printk(IBM_ERR "failed to retrieve HKEY event\n");
2020 return;
2021 }
2022
2023 if (hkey == 0) {
2024 /* queue empty */
2025 return;
2026 }
2027
2028 send_acpi_ev = 0;
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002029 ignore_acpi_ev = 0;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002030
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002031 switch (hkey >> 12) {
2032 case 1:
2033 /* 0x1000-0x1FFF: key presses */
2034 scancode = hkey & 0xfff;
2035 if (scancode > 0 && scancode < 0x21) {
2036 scancode--;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002037 if (!(hotkey_source_mask & (1 << scancode))) {
2038 tpacpi_input_send_key(scancode);
2039 } else {
2040 ignore_acpi_ev = 1;
2041 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002042 } else {
2043 printk(IBM_ERR
2044 "hotkey 0x%04x out of range for keyboard map\n",
2045 hkey);
2046 send_acpi_ev = 1;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002047 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002048 break;
2049 case 5:
2050 /* 0x5000-0x5FFF: LID */
2051 /* we don't handle it through this path, just
2052 * eat up known LID events */
2053 if (hkey != 0x5001 && hkey != 0x5002) {
2054 printk(IBM_ERR
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002055 "unknown LID-related HKEY event: 0x%04x\n",
2056 hkey);
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002057 send_acpi_ev = 1;
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002058 } else {
2059 ignore_acpi_ev = 1;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002060 }
2061 break;
2062 case 7:
2063 /* 0x7000-0x7FFF: misc */
2064 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
2065 tpacpi_input_send_radiosw();
2066 break;
2067 }
2068 /* fallthrough to default */
2069 default:
2070 /* case 2: dock-related */
2071 /* 0x2305 - T43 waking up due to bay lever eject while aslept */
2072 /* case 3: ultra-bay related. maybe bay in dock? */
2073 /* 0x3003 - T43 after wake up by bay lever eject (0x2305) */
2074 printk(IBM_NOTICE "unhandled HKEY event 0x%04x\n", hkey);
2075 send_acpi_ev = 1;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002076 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002077
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002078 /* Legacy events */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002079 if (!ignore_acpi_ev && (send_acpi_ev || hotkey_report_mode < 2)) {
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002080 acpi_bus_generate_proc_event(ibm->acpi->device, event, hkey);
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002081 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002082
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002083 /* netlink events */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002084 if (!ignore_acpi_ev && send_acpi_ev) {
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002085 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
2086 ibm->acpi->device->dev.bus_id,
2087 event, hkey);
2088 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002089 }
2090}
2091
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002092static void hotkey_resume(void)
2093{
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002094 if (hotkey_mask_get())
2095 printk(IBM_ERR "error while trying to read hot key mask from firmware\n");
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002096 tpacpi_input_send_radiosw();
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002097#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2098 hotkey_poll_setup_safe(0);
2099#endif
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002100}
2101
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002102/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002103static int hotkey_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002105 int res, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 int len = 0;
2107
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002108 if (!tp_features.hotkey) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002109 len += sprintf(p + len, "status:\t\tnot supported\n");
2110 return len;
2111 }
2112
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02002113 if (mutex_lock_interruptible(&hotkey_mutex))
2114 return -ERESTARTSYS;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002115 res = hotkey_status_get(&status);
2116 if (!res)
2117 res = hotkey_mask_get();
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002118 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03002119 if (res)
2120 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121
2122 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002123 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002124 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 len += sprintf(p + len,
2126 "commands:\tenable, disable, reset, <mask>\n");
2127 } else {
2128 len += sprintf(p + len, "mask:\t\tnot supported\n");
2129 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
2130 }
2131
2132 return len;
2133}
2134
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002135static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002137 int res, status;
2138 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002141 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 return -ENODEV;
2143
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02002144 if (mutex_lock_interruptible(&hotkey_mutex))
2145 return -ERESTARTSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002146
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002147 status = -1;
2148 mask = hotkey_mask;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002149
2150 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 while ((cmd = next_cmd(&buf))) {
2152 if (strlencmp(cmd, "enable") == 0) {
2153 status = 1;
2154 } else if (strlencmp(cmd, "disable") == 0) {
2155 status = 0;
2156 } else if (strlencmp(cmd, "reset") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002157 status = hotkey_orig_status;
2158 mask = hotkey_orig_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
2160 /* mask set */
2161 } else if (sscanf(cmd, "%x", &mask) == 1) {
2162 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002163 } else {
2164 res = -EINVAL;
2165 goto errexit;
2166 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 }
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002168 if (status != -1)
2169 res = hotkey_status_set(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002171 if (!res && mask != hotkey_mask)
2172 res = hotkey_mask_set(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002174errexit:
2175 mutex_unlock(&hotkey_mutex);
2176 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002177}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002179static const struct acpi_device_id ibm_htk_device_ids[] = {
2180 {IBM_HKEY_HID, 0},
2181 {"", 0},
2182};
2183
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002184static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002185 .hid = ibm_htk_device_ids,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002186 .notify = hotkey_notify,
2187 .handle = &hkey_handle,
2188 .type = ACPI_DEVICE_NOTIFY,
2189};
2190
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002191static struct ibm_struct hotkey_driver_data = {
2192 .name = "hotkey",
2193 .read = hotkey_read,
2194 .write = hotkey_write,
2195 .exit = hotkey_exit,
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002196 .resume = hotkey_resume,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002197 .acpi = &ibm_hotkey_acpidriver,
2198};
2199
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002200/*************************************************************************
2201 * Bluetooth subdriver
2202 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002204enum {
2205 /* ACPI GBDC/SBDC bits */
2206 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
2207 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
2208 TP_ACPI_BLUETOOTH_UNK = 0x04, /* unknown function */
2209};
2210
2211static int bluetooth_get_radiosw(void);
2212static int bluetooth_set_radiosw(int radio_on);
2213
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002214/* sysfs bluetooth enable ---------------------------------------------- */
2215static ssize_t bluetooth_enable_show(struct device *dev,
2216 struct device_attribute *attr,
2217 char *buf)
2218{
2219 int status;
2220
2221 status = bluetooth_get_radiosw();
2222 if (status < 0)
2223 return status;
2224
2225 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2226}
2227
2228static ssize_t bluetooth_enable_store(struct device *dev,
2229 struct device_attribute *attr,
2230 const char *buf, size_t count)
2231{
2232 unsigned long t;
2233 int res;
2234
2235 if (parse_strtoul(buf, 1, &t))
2236 return -EINVAL;
2237
2238 res = bluetooth_set_radiosw(t);
2239
2240 return (res) ? res : count;
2241}
2242
2243static struct device_attribute dev_attr_bluetooth_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002244 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002245 bluetooth_enable_show, bluetooth_enable_store);
2246
2247/* --------------------------------------------------------------------- */
2248
2249static struct attribute *bluetooth_attributes[] = {
2250 &dev_attr_bluetooth_enable.attr,
2251 NULL
2252};
2253
2254static const struct attribute_group bluetooth_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002255 .attrs = bluetooth_attributes,
2256};
2257
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002258static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002260 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002261 int status = 0;
2262
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002263 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
2264
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002265 IBM_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002266
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002267 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2268 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002269 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002270 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002272 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
2273 str_supported(tp_features.bluetooth),
2274 status);
2275
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002276 if (tp_features.bluetooth) {
2277 if (!(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
2278 /* no bluetooth hardware present in system */
2279 tp_features.bluetooth = 0;
2280 dbg_printk(TPACPI_DBG_INIT,
2281 "bluetooth hardware not installed\n");
2282 } else {
2283 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2284 &bluetooth_attr_group);
2285 if (res)
2286 return res;
2287 }
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002288 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002289
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002290 return (tp_features.bluetooth)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291}
2292
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002293static void bluetooth_exit(void)
2294{
2295 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2296 &bluetooth_attr_group);
2297}
2298
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002299static int bluetooth_get_radiosw(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300{
2301 int status;
2302
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002303 if (!tp_features.bluetooth)
2304 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002306 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2307 return -EIO;
2308
2309 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
2310}
2311
2312static int bluetooth_set_radiosw(int radio_on)
2313{
2314 int status;
2315
2316 if (!tp_features.bluetooth)
2317 return -ENODEV;
2318
2319 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2320 return -EIO;
2321 if (radio_on)
2322 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
2323 else
2324 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
2325 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
2326 return -EIO;
2327
2328 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329}
2330
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002331/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002332static int bluetooth_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333{
2334 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002335 int status = bluetooth_get_radiosw();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002337 if (!tp_features.bluetooth)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 len += sprintf(p + len, "status:\t\tnot supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002340 len += sprintf(p + len, "status:\t\t%s\n",
2341 (status)? "enabled" : "disabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 len += sprintf(p + len, "commands:\tenable, disable\n");
2343 }
2344
2345 return len;
2346}
2347
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002348static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002352 if (!tp_features.bluetooth)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002353 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354
2355 while ((cmd = next_cmd(&buf))) {
2356 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002357 bluetooth_set_radiosw(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002359 bluetooth_set_radiosw(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 } else
2361 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 }
2363
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 return 0;
2365}
2366
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002367static struct ibm_struct bluetooth_driver_data = {
2368 .name = "bluetooth",
2369 .read = bluetooth_read,
2370 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002371 .exit = bluetooth_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002372};
2373
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002374/*************************************************************************
2375 * Wan subdriver
2376 */
2377
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002378enum {
2379 /* ACPI GWAN/SWAN bits */
2380 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
2381 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
2382 TP_ACPI_WANCARD_UNK = 0x04, /* unknown function */
2383};
2384
2385static int wan_get_radiosw(void);
2386static int wan_set_radiosw(int radio_on);
2387
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002388/* sysfs wan enable ---------------------------------------------------- */
2389static ssize_t wan_enable_show(struct device *dev,
2390 struct device_attribute *attr,
2391 char *buf)
2392{
2393 int status;
2394
2395 status = wan_get_radiosw();
2396 if (status < 0)
2397 return status;
2398
2399 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2400}
2401
2402static ssize_t wan_enable_store(struct device *dev,
2403 struct device_attribute *attr,
2404 const char *buf, size_t count)
2405{
2406 unsigned long t;
2407 int res;
2408
2409 if (parse_strtoul(buf, 1, &t))
2410 return -EINVAL;
2411
2412 res = wan_set_radiosw(t);
2413
2414 return (res) ? res : count;
2415}
2416
2417static struct device_attribute dev_attr_wan_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002418 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002419 wan_enable_show, wan_enable_store);
2420
2421/* --------------------------------------------------------------------- */
2422
2423static struct attribute *wan_attributes[] = {
2424 &dev_attr_wan_enable.attr,
2425 NULL
2426};
2427
2428static const struct attribute_group wan_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002429 .attrs = wan_attributes,
2430};
2431
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002432static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002433{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002434 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002435 int status = 0;
2436
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002437 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
2438
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002439 IBM_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002440
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002441 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002442 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002443
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002444 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
2445 str_supported(tp_features.wan),
2446 status);
2447
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002448 if (tp_features.wan) {
2449 if (!(status & TP_ACPI_WANCARD_HWPRESENT)) {
2450 /* no wan hardware present in system */
2451 tp_features.wan = 0;
2452 dbg_printk(TPACPI_DBG_INIT,
2453 "wan hardware not installed\n");
2454 } else {
2455 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2456 &wan_attr_group);
2457 if (res)
2458 return res;
2459 }
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002460 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002461
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002462 return (tp_features.wan)? 0 : 1;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002463}
2464
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002465static void wan_exit(void)
2466{
2467 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2468 &wan_attr_group);
2469}
2470
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002471static int wan_get_radiosw(void)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002472{
2473 int status;
2474
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002475 if (!tp_features.wan)
2476 return -ENODEV;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002477
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002478 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2479 return -EIO;
2480
2481 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
2482}
2483
2484static int wan_set_radiosw(int radio_on)
2485{
2486 int status;
2487
2488 if (!tp_features.wan)
2489 return -ENODEV;
2490
2491 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2492 return -EIO;
2493 if (radio_on)
2494 status |= TP_ACPI_WANCARD_RADIOSSW;
2495 else
2496 status &= ~TP_ACPI_WANCARD_RADIOSSW;
2497 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
2498 return -EIO;
2499
2500 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002501}
2502
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002503/* procfs -------------------------------------------------------------- */
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002504static int wan_read(char *p)
2505{
2506 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002507 int status = wan_get_radiosw();
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002508
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002509 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002510 len += sprintf(p + len, "status:\t\tnot supported\n");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002511 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002512 len += sprintf(p + len, "status:\t\t%s\n",
2513 (status)? "enabled" : "disabled");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002514 len += sprintf(p + len, "commands:\tenable, disable\n");
2515 }
2516
2517 return len;
2518}
2519
2520static int wan_write(char *buf)
2521{
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002522 char *cmd;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002523
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002524 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002525 return -ENODEV;
2526
2527 while ((cmd = next_cmd(&buf))) {
2528 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002529 wan_set_radiosw(1);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002530 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002531 wan_set_radiosw(0);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002532 } else
2533 return -EINVAL;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002534 }
2535
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002536 return 0;
2537}
2538
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002539static struct ibm_struct wan_driver_data = {
2540 .name = "wan",
2541 .read = wan_read,
2542 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002543 .exit = wan_exit,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03002544 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002545};
2546
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002547/*************************************************************************
2548 * Video subdriver
2549 */
2550
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002551enum video_access_mode {
2552 TPACPI_VIDEO_NONE = 0,
2553 TPACPI_VIDEO_570, /* 570 */
2554 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
2555 TPACPI_VIDEO_NEW, /* all others */
2556};
2557
2558enum { /* video status flags, based on VIDEO_570 */
2559 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
2560 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
2561 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
2562};
2563
2564enum { /* TPACPI_VIDEO_570 constants */
2565 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
2566 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
2567 * video_status_flags */
2568 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
2569 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
2570};
2571
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02002572static enum video_access_mode video_supported;
2573static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002574
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002575static int video_autosw_get(void);
2576static int video_autosw_set(int enable);
2577
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002578IBM_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
2579 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
2580 "\\_SB.PCI0.VID0", /* 770e */
2581 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
2582 "\\_SB.PCI0.AGP.VID", /* all others */
2583 ); /* R30, R31 */
2584
2585IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
2586
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002587static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002589 int ivga;
2590
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002591 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
2592
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002593 IBM_ACPIHANDLE_INIT(vid);
2594 IBM_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002595
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002596 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
2597 /* G41, assume IVGA doesn't change */
2598 vid_handle = vid2_handle;
2599
2600 if (!vid_handle)
2601 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002602 video_supported = TPACPI_VIDEO_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002603 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
2604 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002605 video_supported = TPACPI_VIDEO_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002606 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
2607 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002608 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002609 else
2610 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002611 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002613 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
2614 str_supported(video_supported != TPACPI_VIDEO_NONE),
2615 video_supported);
2616
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002617 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618}
2619
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002620static void video_exit(void)
2621{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002622 dbg_printk(TPACPI_DBG_EXIT,
2623 "restoring original video autoswitch mode\n");
2624 if (video_autosw_set(video_orig_autosw))
2625 printk(IBM_ERR "error while trying to restore original "
2626 "video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002627}
2628
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002629static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630{
2631 int status = 0;
2632 int i;
2633
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002634 switch (video_supported) {
2635 case TPACPI_VIDEO_570:
2636 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
2637 TP_ACPI_VIDEO_570_PHSCMD))
2638 return -EIO;
2639 status = i & TP_ACPI_VIDEO_570_PHSMASK;
2640 break;
2641 case TPACPI_VIDEO_770:
2642 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
2643 return -EIO;
2644 if (i)
2645 status |= TP_ACPI_VIDEO_S_LCD;
2646 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
2647 return -EIO;
2648 if (i)
2649 status |= TP_ACPI_VIDEO_S_CRT;
2650 break;
2651 case TPACPI_VIDEO_NEW:
2652 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
2653 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
2654 return -EIO;
2655 if (i)
2656 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002658 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
2659 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
2660 return -EIO;
2661 if (i)
2662 status |= TP_ACPI_VIDEO_S_LCD;
2663 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
2664 return -EIO;
2665 if (i)
2666 status |= TP_ACPI_VIDEO_S_DVI;
2667 break;
2668 default:
2669 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002670 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671
2672 return status;
2673}
2674
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002675static int video_outputsw_set(int status)
2676{
2677 int autosw;
2678 int res = 0;
2679
2680 switch (video_supported) {
2681 case TPACPI_VIDEO_570:
2682 res = acpi_evalf(NULL, NULL,
2683 "\\_SB.PHS2", "vdd",
2684 TP_ACPI_VIDEO_570_PHS2CMD,
2685 status | TP_ACPI_VIDEO_570_PHS2SET);
2686 break;
2687 case TPACPI_VIDEO_770:
2688 autosw = video_autosw_get();
2689 if (autosw < 0)
2690 return autosw;
2691
2692 res = video_autosw_set(1);
2693 if (res)
2694 return res;
2695 res = acpi_evalf(vid_handle, NULL,
2696 "ASWT", "vdd", status * 0x100, 0);
2697 if (!autosw && video_autosw_set(autosw)) {
2698 printk(IBM_ERR "video auto-switch left enabled due to error\n");
2699 return -EIO;
2700 }
2701 break;
2702 case TPACPI_VIDEO_NEW:
2703 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
2704 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
2705 break;
2706 default:
2707 return -ENOSYS;
2708 }
2709
2710 return (res)? 0 : -EIO;
2711}
2712
2713static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002715 int autosw = 0;
2716
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002717 switch (video_supported) {
2718 case TPACPI_VIDEO_570:
2719 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
2720 return -EIO;
2721 break;
2722 case TPACPI_VIDEO_770:
2723 case TPACPI_VIDEO_NEW:
2724 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
2725 return -EIO;
2726 break;
2727 default:
2728 return -ENOSYS;
2729 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002730
2731 return autosw & 1;
2732}
2733
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002734static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002735{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002736 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002737 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002738 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002739}
2740
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002741static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002742{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002743 int autosw = video_autosw_get();
2744 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002745
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002746 if (autosw < 0)
2747 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002748
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002749 switch (video_supported) {
2750 case TPACPI_VIDEO_570:
2751 res = video_autosw_set(1);
2752 if (res)
2753 return res;
2754 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
2755 break;
2756 case TPACPI_VIDEO_770:
2757 case TPACPI_VIDEO_NEW:
2758 res = video_autosw_set(1);
2759 if (res)
2760 return res;
2761 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
2762 break;
2763 default:
2764 return -ENOSYS;
2765 }
2766 if (!autosw && video_autosw_set(autosw)) {
2767 printk(IBM_ERR "video auto-switch left enabled due to error\n");
2768 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002769 }
2770
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002771 return (res)? 0 : -EIO;
2772}
2773
2774static int video_expand_toggle(void)
2775{
2776 switch (video_supported) {
2777 case TPACPI_VIDEO_570:
2778 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
2779 0 : -EIO;
2780 case TPACPI_VIDEO_770:
2781 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
2782 0 : -EIO;
2783 case TPACPI_VIDEO_NEW:
2784 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
2785 0 : -EIO;
2786 default:
2787 return -ENOSYS;
2788 }
2789 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002790}
2791
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002792static int video_read(char *p)
2793{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002794 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002795 int len = 0;
2796
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002797 if (video_supported == TPACPI_VIDEO_NONE) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002798 len += sprintf(p + len, "status:\t\tnot supported\n");
2799 return len;
2800 }
2801
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002802 status = video_outputsw_get();
2803 if (status < 0)
2804 return status;
2805
2806 autosw = video_autosw_get();
2807 if (autosw < 0)
2808 return autosw;
2809
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002810 len += sprintf(p + len, "status:\t\tsupported\n");
2811 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
2812 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002813 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002814 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
2815 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
2816 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
2817 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002818 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002819 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
2820 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
2821 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
2822
2823 return len;
2824}
2825
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002826static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827{
2828 char *cmd;
2829 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002830 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002832 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002833 return -ENODEV;
2834
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002835 enable = 0;
2836 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837
2838 while ((cmd = next_cmd(&buf))) {
2839 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002840 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002842 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002844 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002846 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002847 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002848 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002849 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002850 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002851 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002852 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002854 res = video_autosw_set(1);
2855 if (res)
2856 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002858 res = video_autosw_set(0);
2859 if (res)
2860 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002862 res = video_outputsw_cycle();
2863 if (res)
2864 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002866 res = video_expand_toggle();
2867 if (res)
2868 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 } else
2870 return -EINVAL;
2871 }
2872
2873 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002874 status = video_outputsw_get();
2875 if (status < 0)
2876 return status;
2877 res = video_outputsw_set((status & ~disable) | enable);
2878 if (res)
2879 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 }
2881
2882 return 0;
2883}
2884
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002885static struct ibm_struct video_driver_data = {
2886 .name = "video",
2887 .read = video_read,
2888 .write = video_write,
2889 .exit = video_exit,
2890};
2891
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002892/*************************************************************************
2893 * Light (thinklight) subdriver
2894 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002896IBM_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
2897IBM_HANDLE(ledb, ec, "LEDB"); /* G4x */
2898
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002899static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002901 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
2902
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002903 IBM_ACPIHANDLE_INIT(ledb);
2904 IBM_ACPIHANDLE_INIT(lght);
2905 IBM_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002906
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002907 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002908 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002909
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002910 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002911 /* light status not supported on
2912 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002913 tp_features.light_status =
2914 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002916 vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002917 str_supported(tp_features.light));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002918
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002919 return (tp_features.light)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920}
2921
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002922static int light_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923{
2924 int len = 0;
2925 int status = 0;
2926
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002927 if (!tp_features.light) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002928 len += sprintf(p + len, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002929 } else if (!tp_features.light_status) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002930 len += sprintf(p + len, "status:\t\tunknown\n");
2931 len += sprintf(p + len, "commands:\ton, off\n");
2932 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
2934 return -EIO;
2935 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002936 len += sprintf(p + len, "commands:\ton, off\n");
2937 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938
2939 return len;
2940}
2941
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002942static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943{
2944 int cmos_cmd, lght_cmd;
2945 char *cmd;
2946 int success;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002947
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002948 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002949 return -ENODEV;
2950
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 while ((cmd = next_cmd(&buf))) {
2952 if (strlencmp(cmd, "on") == 0) {
2953 cmos_cmd = 0x0c;
2954 lght_cmd = 1;
2955 } else if (strlencmp(cmd, "off") == 0) {
2956 cmos_cmd = 0x0d;
2957 lght_cmd = 0;
2958 } else
2959 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002960
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 success = cmos_handle ?
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002962 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
2963 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 if (!success)
2965 return -EIO;
2966 }
2967
2968 return 0;
2969}
2970
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002971static struct ibm_struct light_driver_data = {
2972 .name = "light",
2973 .read = light_read,
2974 .write = light_write,
2975};
2976
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002977/*************************************************************************
2978 * Dock subdriver
2979 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03002981#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002982
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002983static void dock_notify(struct ibm_struct *ibm, u32 event);
2984static int dock_read(char *p);
2985static int dock_write(char *buf);
2986
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002987IBM_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
2988 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
2989 "\\_SB.PCI0.PCI1.DOCK", /* all others */
2990 "\\_SB.PCI.ISA.SLCE", /* 570 */
2991 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
2992
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002993/* don't list other alternatives as we install a notify handler on the 570 */
2994IBM_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
2995
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002996static const struct acpi_device_id ibm_pci_device_ids[] = {
2997 {PCI_ROOT_HID_STRING, 0},
2998 {"", 0},
2999};
3000
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003001static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
3002 {
3003 .notify = dock_notify,
3004 .handle = &dock_handle,
3005 .type = ACPI_SYSTEM_NOTIFY,
3006 },
3007 {
Henrique de Moraes Holschuh996fba02007-07-18 23:45:40 -03003008 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
3009 * We just use it to get notifications of dock hotplug
3010 * in very old thinkpads */
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003011 .hid = ibm_pci_device_ids,
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003012 .notify = dock_notify,
3013 .handle = &pci_handle,
3014 .type = ACPI_SYSTEM_NOTIFY,
3015 },
3016};
3017
3018static struct ibm_struct dock_driver_data[2] = {
3019 {
3020 .name = "dock",
3021 .read = dock_read,
3022 .write = dock_write,
3023 .acpi = &ibm_dock_acpidriver[0],
3024 },
3025 {
3026 .name = "dock",
3027 .acpi = &ibm_dock_acpidriver[1],
3028 },
3029};
3030
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031#define dock_docked() (_sta(dock_handle) & 1)
3032
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003033static int __init dock_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003034{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003035 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
3036
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003037 IBM_ACPIHANDLE_INIT(dock);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003038
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003039 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
3040 str_supported(dock_handle != NULL));
3041
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003042 return (dock_handle)? 0 : 1;
3043}
3044
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003045static int __init dock_init2(struct ibm_init_struct *iibm)
3046{
3047 int dock2_needed;
3048
3049 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
3050
3051 if (dock_driver_data[0].flags.acpi_driver_registered &&
3052 dock_driver_data[0].flags.acpi_notify_installed) {
3053 IBM_ACPIHANDLE_INIT(pci);
3054 dock2_needed = (pci_handle != NULL);
3055 vdbg_printk(TPACPI_DBG_INIT,
3056 "dock PCI handler for the TP 570 is %s\n",
3057 str_supported(dock2_needed));
3058 } else {
3059 vdbg_printk(TPACPI_DBG_INIT,
3060 "dock subdriver part 2 not required\n");
3061 dock2_needed = 0;
3062 }
3063
3064 return (dock2_needed)? 0 : 1;
3065}
3066
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003067static void dock_notify(struct ibm_struct *ibm, u32 event)
3068{
3069 int docked = dock_docked();
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003070 int pci = ibm->acpi->hid && ibm->acpi->device &&
3071 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
Zhang Rui962ce8c2007-08-23 01:24:31 +08003072 int data;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003073
3074 if (event == 1 && !pci) /* 570 */
Zhang Rui962ce8c2007-08-23 01:24:31 +08003075 data = 1; /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003076 else if (event == 1 && pci) /* 570 */
Zhang Rui962ce8c2007-08-23 01:24:31 +08003077 data = 3; /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003078 else if (event == 3 && docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08003079 data = 1; /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003080 else if (event == 3 && !docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08003081 data = 2; /* undock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003082 else if (event == 0 && docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08003083 data = 3; /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003084 else {
3085 printk(IBM_ERR "unknown dock event %d, status %d\n",
3086 event, _sta(dock_handle));
Zhang Rui962ce8c2007-08-23 01:24:31 +08003087 data = 0; /* unknown */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003088 }
Len Brown14e04fb32007-08-23 15:20:26 -04003089 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
Zhang Rui962ce8c2007-08-23 01:24:31 +08003090 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3091 ibm->acpi->device->dev.bus_id,
3092 event, data);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003093}
3094
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003095static int dock_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096{
3097 int len = 0;
3098 int docked = dock_docked();
3099
3100 if (!dock_handle)
3101 len += sprintf(p + len, "status:\t\tnot supported\n");
3102 else if (!docked)
3103 len += sprintf(p + len, "status:\t\tundocked\n");
3104 else {
3105 len += sprintf(p + len, "status:\t\tdocked\n");
3106 len += sprintf(p + len, "commands:\tdock, undock\n");
3107 }
3108
3109 return len;
3110}
3111
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003112static int dock_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113{
3114 char *cmd;
3115
3116 if (!dock_docked())
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003117 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118
3119 while ((cmd = next_cmd(&buf))) {
3120 if (strlencmp(cmd, "undock") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003121 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
3122 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 return -EIO;
3124 } else if (strlencmp(cmd, "dock") == 0) {
3125 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
3126 return -EIO;
3127 } else
3128 return -EINVAL;
3129 }
3130
3131 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003132}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003134#endif /* CONFIG_THINKPAD_ACPI_DOCK */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003136/*************************************************************************
3137 * Bay subdriver
3138 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003140#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003141
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003142IBM_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
3143 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
3144 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
3145 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
3146 ); /* A21e, R30, R31 */
3147IBM_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
3148 "_EJ0", /* all others */
3149 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
3150IBM_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
3151 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
3152 ); /* all others */
3153IBM_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
3154 "_EJ0", /* 770x */
3155 ); /* all others */
3156
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003157static int __init bay_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003159 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
3160
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003161 IBM_ACPIHANDLE_INIT(bay);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003162 if (bay_handle)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003163 IBM_ACPIHANDLE_INIT(bay_ej);
3164 IBM_ACPIHANDLE_INIT(bay2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003165 if (bay2_handle)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003166 IBM_ACPIHANDLE_INIT(bay2_ej);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003167
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003168 tp_features.bay_status = bay_handle &&
3169 acpi_evalf(bay_handle, NULL, "_STA", "qv");
3170 tp_features.bay_status2 = bay2_handle &&
3171 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003172
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003173 tp_features.bay_eject = bay_handle && bay_ej_handle &&
3174 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
3175 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
3176 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003178 vdbg_printk(TPACPI_DBG_INIT,
3179 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003180 str_supported(tp_features.bay_status),
3181 str_supported(tp_features.bay_eject),
3182 str_supported(tp_features.bay_status2),
3183 str_supported(tp_features.bay_eject2));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003184
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003185 return (tp_features.bay_status || tp_features.bay_eject ||
3186 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187}
3188
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003189static void bay_notify(struct ibm_struct *ibm, u32 event)
3190{
Len Brown14e04fb32007-08-23 15:20:26 -04003191 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
Zhang Rui962ce8c2007-08-23 01:24:31 +08003192 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3193 ibm->acpi->device->dev.bus_id,
3194 event, 0);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003195}
3196
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003197#define bay_occupied(b) (_sta(b##_handle) & 1)
3198
3199static int bay_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200{
3201 int len = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003202 int occupied = bay_occupied(bay);
3203 int occupied2 = bay_occupied(bay2);
3204 int eject, eject2;
3205
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003206 len += sprintf(p + len, "status:\t\t%s\n",
3207 tp_features.bay_status ?
3208 (occupied ? "occupied" : "unoccupied") :
3209 "not supported");
3210 if (tp_features.bay_status2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003211 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
3212 "occupied" : "unoccupied");
3213
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003214 eject = tp_features.bay_eject && occupied;
3215 eject2 = tp_features.bay_eject2 && occupied2;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003216
3217 if (eject && eject2)
3218 len += sprintf(p + len, "commands:\teject, eject2\n");
3219 else if (eject)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 len += sprintf(p + len, "commands:\teject\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003221 else if (eject2)
3222 len += sprintf(p + len, "commands:\teject2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223
3224 return len;
3225}
3226
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003227static int bay_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228{
3229 char *cmd;
3230
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003231 if (!tp_features.bay_eject && !tp_features.bay_eject2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003232 return -ENODEV;
3233
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003235 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003236 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
3237 return -EIO;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003238 } else if (tp_features.bay_eject2 &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003239 strlencmp(cmd, "eject2") == 0) {
3240 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 return -EIO;
3242 } else
3243 return -EINVAL;
3244 }
3245
3246 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003247}
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003248
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003249static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
3250 .notify = bay_notify,
3251 .handle = &bay_handle,
3252 .type = ACPI_SYSTEM_NOTIFY,
3253};
3254
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003255static struct ibm_struct bay_driver_data = {
3256 .name = "bay",
3257 .read = bay_read,
3258 .write = bay_write,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003259 .acpi = &ibm_bay_acpidriver,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003260};
3261
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003262#endif /* CONFIG_THINKPAD_ACPI_BAY */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003264/*************************************************************************
3265 * CMOS subdriver
3266 */
3267
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003268/* sysfs cmos_command -------------------------------------------------- */
3269static ssize_t cmos_command_store(struct device *dev,
3270 struct device_attribute *attr,
3271 const char *buf, size_t count)
3272{
3273 unsigned long cmos_cmd;
3274 int res;
3275
3276 if (parse_strtoul(buf, 21, &cmos_cmd))
3277 return -EINVAL;
3278
3279 res = issue_thinkpad_cmos_command(cmos_cmd);
3280 return (res)? res : count;
3281}
3282
3283static struct device_attribute dev_attr_cmos_command =
3284 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
3285
3286/* --------------------------------------------------------------------- */
3287
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003288static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003289{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003290 int res;
3291
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003292 vdbg_printk(TPACPI_DBG_INIT,
3293 "initializing cmos commands subdriver\n");
3294
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003295 IBM_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003296
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003297 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
3298 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003299
3300 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3301 if (res)
3302 return res;
3303
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003304 return (cmos_handle)? 0 : 1;
3305}
3306
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003307static void cmos_exit(void)
3308{
3309 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3310}
3311
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003312static int cmos_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313{
3314 int len = 0;
3315
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003316 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3317 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 if (!cmos_handle)
3319 len += sprintf(p + len, "status:\t\tnot supported\n");
3320 else {
3321 len += sprintf(p + len, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003322 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 }
3324
3325 return len;
3326}
3327
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003328static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329{
3330 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03003331 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332
3333 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003334 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
3335 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 /* cmos_cmd set */
3337 } else
3338 return -EINVAL;
3339
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03003340 res = issue_thinkpad_cmos_command(cmos_cmd);
3341 if (res)
3342 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 }
3344
3345 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003346}
3347
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003348static struct ibm_struct cmos_driver_data = {
3349 .name = "cmos",
3350 .read = cmos_read,
3351 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003352 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003353};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003354
3355/*************************************************************************
3356 * LED subdriver
3357 */
3358
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003359enum led_access_mode {
3360 TPACPI_LED_NONE = 0,
3361 TPACPI_LED_570, /* 570 */
3362 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
3363 TPACPI_LED_NEW, /* all others */
3364};
3365
3366enum { /* For TPACPI_LED_OLD */
3367 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
3368 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
3369 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
3370};
3371
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02003372static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003373
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003374IBM_HANDLE(led, ec, "SLED", /* 570 */
3375 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
3376 "LED", /* all others */
3377 ); /* R30, R31 */
3378
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003379static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003380{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003381 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
3382
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003383 IBM_ACPIHANDLE_INIT(led);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003384
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003385 if (!led_handle)
3386 /* led not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003387 led_supported = TPACPI_LED_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003388 else if (strlencmp(led_path, "SLED") == 0)
3389 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003390 led_supported = TPACPI_LED_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003391 else if (strlencmp(led_path, "SYSL") == 0)
3392 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003393 led_supported = TPACPI_LED_OLD;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003394 else
3395 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003396 led_supported = TPACPI_LED_NEW;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003397
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003398 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
3399 str_supported(led_supported), led_supported);
3400
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003401 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003402}
3403
3404#define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
3405
3406static int led_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407{
3408 int len = 0;
3409
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003410 if (!led_supported) {
3411 len += sprintf(p + len, "status:\t\tnot supported\n");
3412 return len;
3413 }
3414 len += sprintf(p + len, "status:\t\tsupported\n");
3415
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003416 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003417 /* 570 */
3418 int i, status;
3419 for (i = 0; i < 8; i++) {
3420 if (!acpi_evalf(ec_handle,
3421 &status, "GLED", "dd", 1 << i))
3422 return -EIO;
3423 len += sprintf(p + len, "%d:\t\t%s\n",
3424 i, led_status(status));
3425 }
3426 }
3427
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 len += sprintf(p + len, "commands:\t"
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003429 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430
3431 return len;
3432}
3433
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003434/* off, on, blink */
3435static const int led_sled_arg1[] = { 0, 1, 3 };
3436static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
3437static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
3438static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
3439
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003440static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441{
3442 char *cmd;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003443 int led, ind, ret;
3444
3445 if (!led_supported)
3446 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447
3448 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003449 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450 return -EINVAL;
3451
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003452 if (strstr(cmd, "off")) {
3453 ind = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 } else if (strstr(cmd, "on")) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003455 ind = 1;
3456 } else if (strstr(cmd, "blink")) {
3457 ind = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458 } else
3459 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003460
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003461 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003462 /* 570 */
3463 led = 1 << led;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003465 led, led_sled_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466 return -EIO;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003467 } else if (led_supported == TPACPI_LED_OLD) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003468 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
3469 led = 1 << led;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003470 ret = ec_write(TPACPI_LED_EC_HLMS, led);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003471 if (ret >= 0)
3472 ret =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003473 ec_write(TPACPI_LED_EC_HLBL,
Henrique de Moraes Holschuhe062e032007-03-23 17:33:55 -03003474 led * led_exp_hlbl[ind]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003475 if (ret >= 0)
3476 ret =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003477 ec_write(TPACPI_LED_EC_HLCL,
Henrique de Moraes Holschuhe062e032007-03-23 17:33:55 -03003478 led * led_exp_hlcl[ind]);
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003479 if (ret < 0)
3480 return ret;
3481 } else {
3482 /* all others */
3483 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
3484 led, led_led_arg1[ind]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003486 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 }
3488
3489 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003490}
3491
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003492static struct ibm_struct led_driver_data = {
3493 .name = "led",
3494 .read = led_read,
3495 .write = led_write,
3496};
3497
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003498/*************************************************************************
3499 * Beep subdriver
3500 */
3501
3502IBM_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
3503
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003504static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003505{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003506 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
3507
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003508 IBM_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003509
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003510 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
3511 str_supported(beep_handle != NULL));
3512
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003513 return (beep_handle)? 0 : 1;
3514}
3515
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003516static int beep_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517{
3518 int len = 0;
3519
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003520 if (!beep_handle)
3521 len += sprintf(p + len, "status:\t\tnot supported\n");
3522 else {
3523 len += sprintf(p + len, "status:\t\tsupported\n");
3524 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
3525 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526
3527 return len;
3528}
3529
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003530static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531{
3532 char *cmd;
3533 int beep_cmd;
3534
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003535 if (!beep_handle)
3536 return -ENODEV;
3537
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003539 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
3540 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 /* beep_cmd set */
3542 } else
3543 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003544 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545 return -EIO;
3546 }
3547
3548 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003549}
3550
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003551static struct ibm_struct beep_driver_data = {
3552 .name = "beep",
3553 .read = beep_read,
3554 .write = beep_write,
3555};
3556
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003557/*************************************************************************
3558 * Thermal subdriver
3559 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003560
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003561enum thermal_access_mode {
3562 TPACPI_THERMAL_NONE = 0, /* No thermal support */
3563 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
3564 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
3565 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
3566 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
3567};
3568
3569enum { /* TPACPI_THERMAL_TPEC_* */
3570 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
3571 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
3572 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
3573};
3574
3575#define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
3576struct ibm_thermal_sensors_struct {
3577 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
3578};
3579
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003580static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003581
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02003582/* idx is zero-based */
3583static int thermal_get_sensor(int idx, s32 *value)
3584{
3585 int t;
3586 s8 tmp;
3587 char tmpi[5];
3588
3589 t = TP_EC_THERMAL_TMP0;
3590
3591 switch (thermal_read_mode) {
3592#if TPACPI_MAX_THERMAL_SENSORS >= 16
3593 case TPACPI_THERMAL_TPEC_16:
3594 if (idx >= 8 && idx <= 15) {
3595 t = TP_EC_THERMAL_TMP8;
3596 idx -= 8;
3597 }
3598 /* fallthrough */
3599#endif
3600 case TPACPI_THERMAL_TPEC_8:
3601 if (idx <= 7) {
3602 if (!acpi_ec_read(t + idx, &tmp))
3603 return -EIO;
3604 *value = tmp * 1000;
3605 return 0;
3606 }
3607 break;
3608
3609 case TPACPI_THERMAL_ACPI_UPDT:
3610 if (idx <= 7) {
3611 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
3612 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
3613 return -EIO;
3614 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
3615 return -EIO;
3616 *value = (t - 2732) * 100;
3617 return 0;
3618 }
3619 break;
3620
3621 case TPACPI_THERMAL_ACPI_TMP07:
3622 if (idx <= 7) {
3623 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
3624 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
3625 return -EIO;
3626 if (t > 127 || t < -127)
3627 t = TP_EC_THERMAL_TMP_NA;
3628 *value = t * 1000;
3629 return 0;
3630 }
3631 break;
3632
3633 case TPACPI_THERMAL_NONE:
3634 default:
3635 return -ENOSYS;
3636 }
3637
3638 return -EINVAL;
3639}
3640
3641static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
3642{
3643 int res, i;
3644 int n;
3645
3646 n = 8;
3647 i = 0;
3648
3649 if (!s)
3650 return -EINVAL;
3651
3652 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
3653 n = 16;
3654
3655 for(i = 0 ; i < n; i++) {
3656 res = thermal_get_sensor(i, &s->temp[i]);
3657 if (res)
3658 return res;
3659 }
3660
3661 return n;
3662}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003663
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003664/* sysfs temp##_input -------------------------------------------------- */
3665
3666static ssize_t thermal_temp_input_show(struct device *dev,
3667 struct device_attribute *attr,
3668 char *buf)
3669{
3670 struct sensor_device_attribute *sensor_attr =
3671 to_sensor_dev_attr(attr);
3672 int idx = sensor_attr->index;
3673 s32 value;
3674 int res;
3675
3676 res = thermal_get_sensor(idx, &value);
3677 if (res)
3678 return res;
3679 if (value == TP_EC_THERMAL_TMP_NA * 1000)
3680 return -ENXIO;
3681
3682 return snprintf(buf, PAGE_SIZE, "%d\n", value);
3683}
3684
3685#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
3686 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, thermal_temp_input_show, NULL, _idxB)
3687
3688static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
3689 THERMAL_SENSOR_ATTR_TEMP(1, 0),
3690 THERMAL_SENSOR_ATTR_TEMP(2, 1),
3691 THERMAL_SENSOR_ATTR_TEMP(3, 2),
3692 THERMAL_SENSOR_ATTR_TEMP(4, 3),
3693 THERMAL_SENSOR_ATTR_TEMP(5, 4),
3694 THERMAL_SENSOR_ATTR_TEMP(6, 5),
3695 THERMAL_SENSOR_ATTR_TEMP(7, 6),
3696 THERMAL_SENSOR_ATTR_TEMP(8, 7),
3697 THERMAL_SENSOR_ATTR_TEMP(9, 8),
3698 THERMAL_SENSOR_ATTR_TEMP(10, 9),
3699 THERMAL_SENSOR_ATTR_TEMP(11, 10),
3700 THERMAL_SENSOR_ATTR_TEMP(12, 11),
3701 THERMAL_SENSOR_ATTR_TEMP(13, 12),
3702 THERMAL_SENSOR_ATTR_TEMP(14, 13),
3703 THERMAL_SENSOR_ATTR_TEMP(15, 14),
3704 THERMAL_SENSOR_ATTR_TEMP(16, 15),
3705};
3706
3707#define THERMAL_ATTRS(X) \
3708 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
3709
3710static struct attribute *thermal_temp_input_attr[] = {
3711 THERMAL_ATTRS(8),
3712 THERMAL_ATTRS(9),
3713 THERMAL_ATTRS(10),
3714 THERMAL_ATTRS(11),
3715 THERMAL_ATTRS(12),
3716 THERMAL_ATTRS(13),
3717 THERMAL_ATTRS(14),
3718 THERMAL_ATTRS(15),
3719 THERMAL_ATTRS(0),
3720 THERMAL_ATTRS(1),
3721 THERMAL_ATTRS(2),
3722 THERMAL_ATTRS(3),
3723 THERMAL_ATTRS(4),
3724 THERMAL_ATTRS(5),
3725 THERMAL_ATTRS(6),
3726 THERMAL_ATTRS(7),
3727 NULL
3728};
3729
3730static const struct attribute_group thermal_temp_input16_group = {
3731 .attrs = thermal_temp_input_attr
3732};
3733
3734static const struct attribute_group thermal_temp_input8_group = {
3735 .attrs = &thermal_temp_input_attr[8]
3736};
3737
3738#undef THERMAL_SENSOR_ATTR_TEMP
3739#undef THERMAL_ATTRS
3740
3741/* --------------------------------------------------------------------- */
3742
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003743static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003744{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003745 u8 t, ta1, ta2;
3746 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003747 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003748 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003749
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003750 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
3751
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003752 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003753
Henrique de Moraes Holschuh3d6f99c2007-07-18 23:45:46 -03003754 if (thinkpad_id.ec_model) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003755 /*
3756 * Direct EC access mode: sensors at registers
3757 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
3758 * non-implemented, thermal sensors return 0x80 when
3759 * not available
3760 */
3761
3762 ta1 = ta2 = 0;
3763 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03003764 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003765 ta1 |= t;
3766 } else {
3767 ta1 = 0;
3768 break;
3769 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03003770 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003771 ta2 |= t;
3772 } else {
3773 ta1 = 0;
3774 break;
3775 }
3776 }
3777 if (ta1 == 0) {
3778 /* This is sheer paranoia, but we handle it anyway */
3779 if (acpi_tmp7) {
3780 printk(IBM_ERR
3781 "ThinkPad ACPI EC access misbehaving, "
3782 "falling back to ACPI TMPx access mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003783 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003784 } else {
3785 printk(IBM_ERR
3786 "ThinkPad ACPI EC access misbehaving, "
3787 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003788 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003789 }
3790 } else {
3791 thermal_read_mode =
3792 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003793 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02003794 }
3795 } else if (acpi_tmp7) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003796 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
3797 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003798 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003799 } else {
3800 /* Standard ACPI TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003801 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003802 }
3803 } else {
3804 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003805 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003806 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003807
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003808 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
3809 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
3810 thermal_read_mode);
3811
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003812 switch(thermal_read_mode) {
3813 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03003814 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003815 &thermal_temp_input16_group);
3816 if (res)
3817 return res;
3818 break;
3819 case TPACPI_THERMAL_TPEC_8:
3820 case TPACPI_THERMAL_ACPI_TMP07:
3821 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03003822 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003823 &thermal_temp_input8_group);
3824 if (res)
3825 return res;
3826 break;
3827 case TPACPI_THERMAL_NONE:
3828 default:
3829 return 1;
3830 }
3831
3832 return 0;
3833}
3834
3835static void thermal_exit(void)
3836{
3837 switch(thermal_read_mode) {
3838 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03003839 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003840 &thermal_temp_input16_group);
3841 break;
3842 case TPACPI_THERMAL_TPEC_8:
3843 case TPACPI_THERMAL_ACPI_TMP07:
3844 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03003845 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003846 &thermal_temp_input16_group);
3847 break;
3848 case TPACPI_THERMAL_NONE:
3849 default:
3850 break;
3851 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003852}
3853
3854static int thermal_read(char *p)
3855{
3856 int len = 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003857 int n, i;
3858 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003859
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003860 n = thermal_get_sensors(&t);
3861 if (unlikely(n < 0))
3862 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003863
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003864 len += sprintf(p + len, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003865
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02003866 if (n > 0) {
3867 for (i = 0; i < (n - 1); i++)
3868 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
3869 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
3870 } else
3871 len += sprintf(p + len, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003872
3873 return len;
3874}
3875
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003876static struct ibm_struct thermal_driver_data = {
3877 .name = "thermal",
3878 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03003879 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003880};
3881
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003882/*************************************************************************
3883 * EC Dump subdriver
3884 */
3885
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003886static u8 ecdump_regs[256];
3887
3888static int ecdump_read(char *p)
3889{
3890 int len = 0;
3891 int i, j;
3892 u8 v;
3893
3894 len += sprintf(p + len, "EC "
3895 " +00 +01 +02 +03 +04 +05 +06 +07"
3896 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
3897 for (i = 0; i < 256; i += 16) {
3898 len += sprintf(p + len, "EC 0x%02x:", i);
3899 for (j = 0; j < 16; j++) {
3900 if (!acpi_ec_read(i + j, &v))
3901 break;
3902 if (v != ecdump_regs[i + j])
3903 len += sprintf(p + len, " *%02x", v);
3904 else
3905 len += sprintf(p + len, " %02x", v);
3906 ecdump_regs[i + j] = v;
3907 }
3908 len += sprintf(p + len, "\n");
3909 if (j != 16)
3910 break;
3911 }
3912
3913 /* These are way too dangerous to advertise openly... */
3914#if 0
3915 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
3916 " (<offset> is 00-ff, <value> is 00-ff)\n");
3917 len += sprintf(p + len, "commands:\t0x<offset> <value> "
3918 " (<offset> is 00-ff, <value> is 0-255)\n");
3919#endif
3920 return len;
3921}
3922
3923static int ecdump_write(char *buf)
3924{
3925 char *cmd;
3926 int i, v;
3927
3928 while ((cmd = next_cmd(&buf))) {
3929 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
3930 /* i and v set */
3931 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
3932 /* i and v set */
3933 } else
3934 return -EINVAL;
3935 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
3936 if (!acpi_ec_write(i, v))
3937 return -EIO;
3938 } else
3939 return -EINVAL;
3940 }
3941
3942 return 0;
3943}
3944
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003945static struct ibm_struct ecdump_driver_data = {
3946 .name = "ecdump",
3947 .read = ecdump_read,
3948 .write = ecdump_write,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03003949 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003950};
3951
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003952/*************************************************************************
3953 * Backlight/brightness subdriver
3954 */
Holger Macht8acb0252006-10-20 14:30:28 -07003955
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003956#define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
3957
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03003958static struct backlight_device *ibm_backlight_device;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003959static int brightness_offset = 0x31;
3960static int brightness_mode;
3961static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
3962
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02003963static struct mutex brightness_mutex;
3964
3965/*
3966 * ThinkPads can read brightness from two places: EC 0x31, or
3967 * CMOS NVRAM byte 0x5E, bits 0-3.
3968 */
3969static int brightness_get(struct backlight_device *bd)
3970{
3971 u8 lec = 0, lcmos = 0, level = 0;
3972
3973 if (brightness_mode & 1) {
3974 if (!acpi_ec_read(brightness_offset, &lec))
3975 return -EIO;
3976 lec &= (tp_features.bright_16levels)? 0x0f : 0x07;
3977 level = lec;
3978 };
3979 if (brightness_mode & 2) {
3980 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
3981 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
3982 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
3983 lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
3984 level = lcmos;
3985 }
3986
3987 if (brightness_mode == 3 && lec != lcmos) {
3988 printk(IBM_ERR
3989 "CMOS NVRAM (%u) and EC (%u) do not agree "
3990 "on display brightness level\n",
3991 (unsigned int) lcmos,
3992 (unsigned int) lec);
3993 return -EIO;
3994 }
3995
3996 return level;
3997}
3998
3999/* May return EINTR which can always be mapped to ERESTARTSYS */
4000static int brightness_set(int value)
4001{
4002 int cmos_cmd, inc, i, res;
4003 int current_value;
4004
4005 if (value > ((tp_features.bright_16levels)? 15 : 7))
4006 return -EINVAL;
4007
4008 res = mutex_lock_interruptible(&brightness_mutex);
4009 if (res < 0)
4010 return res;
4011
4012 current_value = brightness_get(NULL);
4013 if (current_value < 0) {
4014 res = current_value;
4015 goto errout;
4016 }
4017
4018 cmos_cmd = value > current_value ?
4019 TP_CMOS_BRIGHTNESS_UP :
4020 TP_CMOS_BRIGHTNESS_DOWN;
4021 inc = (value > current_value)? 1 : -1;
4022
4023 res = 0;
4024 for (i = current_value; i != value; i += inc) {
4025 if ((brightness_mode & 2) &&
4026 issue_thinkpad_cmos_command(cmos_cmd)) {
4027 res = -EIO;
4028 goto errout;
4029 }
4030 if ((brightness_mode & 1) &&
4031 !acpi_ec_write(brightness_offset, i + inc)) {
4032 res = -EIO;
4033 goto errout;;
4034 }
4035 }
4036
4037errout:
4038 mutex_unlock(&brightness_mutex);
4039 return res;
4040}
4041
4042/* sysfs backlight class ----------------------------------------------- */
4043
4044static int brightness_update_status(struct backlight_device *bd)
4045{
4046 /* it is the backlight class's job (caller) to handle
4047 * EINTR and other errors properly */
4048 return brightness_set(
4049 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
4050 bd->props.power == FB_BLANK_UNBLANK) ?
4051 bd->props.brightness : 0);
4052}
Holger Macht8acb0252006-10-20 14:30:28 -07004053
Richard Purdie599a52d2007-02-10 23:07:48 +00004054static struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004055 .get_brightness = brightness_get,
4056 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004057};
4058
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004059/* --------------------------------------------------------------------- */
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03004060
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004061static int __init tpacpi_query_bcll_levels(acpi_handle handle)
4062{
4063 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
4064 union acpi_object *obj;
4065 int rc;
4066
4067 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
4068 obj = (union acpi_object *)buffer.pointer;
4069 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
4070 printk(IBM_ERR "Unknown BCLL data, "
4071 "please report this to %s\n", IBM_MAIL);
4072 rc = 0;
4073 } else {
4074 rc = obj->package.count;
4075 }
4076 } else {
4077 return 0;
4078 }
4079
4080 kfree(buffer.pointer);
4081 return rc;
4082}
4083
4084static acpi_status __init brightness_find_bcll(acpi_handle handle, u32 lvl,
4085 void *context, void **rv)
4086{
4087 char name[ACPI_PATH_SEGMENT_LENGTH];
4088 struct acpi_buffer buffer = { sizeof(name), &name };
4089
4090 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
4091 !strncmp("BCLL", name, sizeof(name) - 1)) {
4092 if (tpacpi_query_bcll_levels(handle) == 16) {
4093 *rv = handle;
4094 return AE_CTRL_TERMINATE;
4095 } else {
4096 return AE_OK;
4097 }
4098 } else {
4099 return AE_OK;
4100 }
4101}
4102
4103static int __init brightness_check_levels(void)
4104{
4105 int status;
4106 void *found_node = NULL;
4107
4108 if (!vid_handle) {
4109 IBM_ACPIHANDLE_INIT(vid);
4110 }
4111 if (!vid_handle)
4112 return 0;
4113
4114 /* Search for a BCLL package with 16 levels */
4115 status = acpi_walk_namespace(ACPI_TYPE_PACKAGE, vid_handle, 3,
4116 brightness_find_bcll, NULL, &found_node);
4117
4118 return (ACPI_SUCCESS(status) && found_node != NULL);
4119}
4120
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02004121static acpi_status __init brightness_find_bcl(acpi_handle handle, u32 lvl,
4122 void *context, void **rv)
4123{
4124 char name[ACPI_PATH_SEGMENT_LENGTH];
4125 struct acpi_buffer buffer = { sizeof(name), &name };
4126
4127 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
4128 !strncmp("_BCL", name, sizeof(name) - 1)) {
4129 *rv = handle;
4130 return AE_CTRL_TERMINATE;
4131 } else {
4132 return AE_OK;
4133 }
4134}
4135
4136static int __init brightness_check_std_acpi_support(void)
4137{
4138 int status;
4139 void *found_node = NULL;
4140
4141 if (!vid_handle) {
4142 IBM_ACPIHANDLE_INIT(vid);
4143 }
4144 if (!vid_handle)
4145 return 0;
4146
4147 /* Search for a _BCL method, but don't execute it */
4148 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
4149 brightness_find_bcl, NULL, &found_node);
4150
4151 return (ACPI_SUCCESS(status) && found_node != NULL);
4152}
4153
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004154static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004155{
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02004156 int b;
4157
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004158 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
4159
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03004160 mutex_init(&brightness_mutex);
4161
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02004162 if (!brightness_enable) {
4163 dbg_printk(TPACPI_DBG_INIT,
4164 "brightness support disabled by module parameter\n");
4165 return 1;
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02004166 } else if (brightness_enable > 1) {
4167 if (brightness_check_std_acpi_support()) {
4168 printk(IBM_NOTICE
4169 "standard ACPI backlight interface available, not loading native one...\n");
4170 return 1;
4171 }
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02004172 }
4173
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03004174 if (!brightness_mode) {
4175 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
4176 brightness_mode = 2;
4177 else
4178 brightness_mode = 3;
4179
4180 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
4181 brightness_mode);
4182 }
4183
4184 if (brightness_mode > 3)
4185 return -EINVAL;
4186
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004187 tp_features.bright_16levels =
4188 thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO &&
4189 brightness_check_levels();
4190
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02004191 b = brightness_get(NULL);
4192 if (b < 0)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03004193 return 1;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02004194
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004195 if (tp_features.bright_16levels)
4196 printk(IBM_INFO "detected a 16-level brightness capable ThinkPad\n");
4197
Henrique de Moraes Holschuh7d5a0152007-04-24 11:48:20 -03004198 ibm_backlight_device = backlight_device_register(
4199 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
4200 &ibm_backlight_data);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004201 if (IS_ERR(ibm_backlight_device)) {
4202 printk(IBM_ERR "Could not register backlight device\n");
4203 return PTR_ERR(ibm_backlight_device);
4204 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004205 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004206
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004207 ibm_backlight_device->props.max_brightness =
4208 (tp_features.bright_16levels)? 15 : 7;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02004209 ibm_backlight_device->props.brightness = b;
4210 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00004211
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004212 return 0;
4213}
4214
4215static void brightness_exit(void)
4216{
4217 if (ibm_backlight_device) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004218 vdbg_printk(TPACPI_DBG_EXIT,
4219 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004220 backlight_device_unregister(ibm_backlight_device);
4221 ibm_backlight_device = NULL;
4222 }
4223}
4224
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004225static int brightness_read(char *p)
4226{
4227 int len = 0;
4228 int level;
4229
4230 if ((level = brightness_get(NULL)) < 0) {
4231 len += sprintf(p + len, "level:\t\tunreadable\n");
4232 } else {
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004233 len += sprintf(p + len, "level:\t\t%d\n", level);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004234 len += sprintf(p + len, "commands:\tup, down\n");
4235 len += sprintf(p + len, "commands:\tlevel <level>"
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004236 " (<level> is 0-%d)\n",
4237 (tp_features.bright_16levels) ? 15 : 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004238 }
4239
4240 return len;
4241}
4242
4243static int brightness_write(char *buf)
4244{
4245 int level;
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004246 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004247 char *cmd;
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004248 int max_level = (tp_features.bright_16levels) ? 15 : 7;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004249
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004250 level = brightness_get(NULL);
4251 if (level < 0)
4252 return level;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004253
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004254 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004255 if (strlencmp(cmd, "up") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004256 if (level < max_level)
4257 level++;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004258 } else if (strlencmp(cmd, "down") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004259 if (level > 0)
4260 level--;
4261 } else if (sscanf(cmd, "level %d", &level) == 1 &&
4262 level >= 0 && level <= max_level) {
4263 /* new level set */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004264 } else
4265 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004266 }
4267
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004268 /*
4269 * Now we know what the final level should be, so we try to set it.
4270 * Doing it this way makes the syscall restartable in case of EINTR
4271 */
4272 rc = brightness_set(level);
4273 return (rc == -EINTR)? ERESTARTSYS : rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004274}
4275
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004276static struct ibm_struct brightness_driver_data = {
4277 .name = "brightness",
4278 .read = brightness_read,
4279 .write = brightness_write,
4280 .exit = brightness_exit,
4281};
4282
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004283/*************************************************************************
4284 * Volume subdriver
4285 */
4286
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004287static int volume_offset = 0x30;
4288
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004289static int volume_read(char *p)
4290{
4291 int len = 0;
4292 u8 level;
4293
4294 if (!acpi_ec_read(volume_offset, &level)) {
4295 len += sprintf(p + len, "level:\t\tunreadable\n");
4296 } else {
4297 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
4298 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
4299 len += sprintf(p + len, "commands:\tup, down, mute\n");
4300 len += sprintf(p + len, "commands:\tlevel <level>"
4301 " (<level> is 0-15)\n");
4302 }
4303
4304 return len;
4305}
4306
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004307static int volume_write(char *buf)
4308{
4309 int cmos_cmd, inc, i;
4310 u8 level, mute;
4311 int new_level, new_mute;
4312 char *cmd;
4313
4314 while ((cmd = next_cmd(&buf))) {
4315 if (!acpi_ec_read(volume_offset, &level))
4316 return -EIO;
4317 new_mute = mute = level & 0x40;
4318 new_level = level = level & 0xf;
4319
4320 if (strlencmp(cmd, "up") == 0) {
4321 if (mute)
4322 new_mute = 0;
4323 else
4324 new_level = level == 15 ? 15 : level + 1;
4325 } else if (strlencmp(cmd, "down") == 0) {
4326 if (mute)
4327 new_mute = 0;
4328 else
4329 new_level = level == 0 ? 0 : level - 1;
4330 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
4331 new_level >= 0 && new_level <= 15) {
4332 /* new_level set */
4333 } else if (strlencmp(cmd, "mute") == 0) {
4334 new_mute = 0x40;
4335 } else
4336 return -EINVAL;
4337
4338 if (new_level != level) { /* mute doesn't change */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004339 cmos_cmd = new_level > level ? TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004340 inc = new_level > level ? 1 : -1;
4341
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004342 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004343 !acpi_ec_write(volume_offset, level)))
4344 return -EIO;
4345
4346 for (i = level; i != new_level; i += inc)
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004347 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004348 !acpi_ec_write(volume_offset, i + inc))
4349 return -EIO;
4350
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004351 if (mute && (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004352 !acpi_ec_write(volume_offset,
4353 new_level + mute)))
4354 return -EIO;
4355 }
4356
4357 if (new_mute != mute) { /* level doesn't change */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004358 cmos_cmd = new_mute ? TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004359
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004360 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004361 !acpi_ec_write(volume_offset, level + new_mute))
4362 return -EIO;
4363 }
4364 }
4365
4366 return 0;
4367}
4368
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004369static struct ibm_struct volume_driver_data = {
4370 .name = "volume",
4371 .read = volume_read,
4372 .write = volume_write,
4373};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004374
4375/*************************************************************************
4376 * Fan subdriver
4377 */
4378
4379/*
4380 * FAN ACCESS MODES
4381 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004382 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004383 * ACPI GFAN method: returns fan level
4384 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004385 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004386 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004387 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004388 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004389 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
4390 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004391 * EC 0x2f (HFSP) might be available *for reading*, but do not use
4392 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004393 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004394 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004395 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
4396 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004397 *
4398 * Fan speed changes of any sort (including those caused by the
4399 * disengaged mode) are usually done slowly by the firmware as the
4400 * maximum ammount of fan duty cycle change per second seems to be
4401 * limited.
4402 *
4403 * Reading is not available if GFAN exists.
4404 * Writing is not available if SFAN exists.
4405 *
4406 * Bits
4407 * 7 automatic mode engaged;
4408 * (default operation mode of the ThinkPad)
4409 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004410 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004411 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004412 * the tachometer while the fan controller ramps up
4413 * the speed (which can take up to a few *minutes*).
4414 * Speeds up fan to 100% duty-cycle, which is far above
4415 * the standard RPM levels. It is not impossible that
4416 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004417 * 5-3 unused in some models. Extra bits for fan level
4418 * in others, but still useless as all values above
4419 * 7 map to the same speed as level 7 in these models.
4420 * 2-0 fan level (0..7 usually)
4421 * 0x00 = stop
4422 * 0x07 = max (set when temperatures critical)
4423 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004424 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004425 *
4426 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
4427 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
4428 * does so, its initial value is meaningless (0x07).
4429 *
4430 * For firmware bugs, refer to:
4431 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
4432 *
4433 * ----
4434 *
4435 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
4436 * Main fan tachometer reading (in RPM)
4437 *
4438 * This register is present on all ThinkPads with a new-style EC, and
4439 * it is known not to be present on the A21m/e, and T22, as there is
4440 * something else in offset 0x84 according to the ACPI DSDT. Other
4441 * ThinkPads from this same time period (and earlier) probably lack the
4442 * tachometer as well.
4443 *
4444 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
4445 * was never fixed by IBM to report the EC firmware version string
4446 * probably support the tachometer (like the early X models), so
4447 * detecting it is quite hard. We need more data to know for sure.
4448 *
4449 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
4450 * might result.
4451 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004452 * FIRMWARE BUG: may go stale while the EC is switching to full speed
4453 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004454 *
4455 * For firmware bugs, refer to:
4456 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
4457 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004458 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004459 * ThinkPad X31, X40, X41. Not available in the X60.
4460 *
4461 * FANS ACPI handle: takes three arguments: low speed, medium speed,
4462 * high speed. ACPI DSDT seems to map these three speeds to levels
4463 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
4464 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
4465 *
4466 * The speeds are stored on handles
4467 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
4468 *
4469 * There are three default speed sets, acessible as handles:
4470 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
4471 *
4472 * ACPI DSDT switches which set is in use depending on various
4473 * factors.
4474 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004475 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004476 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
4477 * but the ACPI tables just mention level 7.
4478 */
4479
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004480enum { /* Fan control constants */
4481 fan_status_offset = 0x2f, /* EC register 0x2f */
4482 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
4483 * 0x84 must be read before 0x85 */
4484
4485 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
4486 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
4487
4488 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
4489};
4490
4491enum fan_status_access_mode {
4492 TPACPI_FAN_NONE = 0, /* No fan status or control */
4493 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
4494 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
4495};
4496
4497enum fan_control_access_mode {
4498 TPACPI_FAN_WR_NONE = 0, /* No fan control */
4499 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
4500 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
4501 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
4502};
4503
4504enum fan_control_commands {
4505 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
4506 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
4507 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
4508 * and also watchdog cmd */
4509};
4510
4511static int fan_control_allowed;
4512
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02004513static enum fan_status_access_mode fan_status_access_mode;
4514static enum fan_control_access_mode fan_control_access_mode;
4515static enum fan_control_commands fan_control_commands;
4516
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02004517static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004518static u8 fan_control_desired_level;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02004519static int fan_watchdog_maxinterval;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004520
4521static struct mutex fan_mutex;
4522
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004523static void fan_watchdog_fire(struct work_struct *ignored);
Len Brown25c68a32006-12-08 04:43:41 -05004524static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02004525
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004526IBM_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
4527IBM_HANDLE(gfan, ec, "GFAN", /* 570 */
4528 "\\FSPD", /* 600e/x, 770e, 770x */
4529 ); /* all others */
4530IBM_HANDLE(sfan, ec, "SFAN", /* 570 */
4531 "JFNS", /* 770x-JL */
4532 ); /* all others */
4533
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004534/*
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004535 * Call with fan_mutex held
4536 */
4537static void fan_update_desired_level(u8 status)
4538{
4539 if ((status &
4540 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
4541 if (status > 7)
4542 fan_control_desired_level = 7;
4543 else
4544 fan_control_desired_level = status;
4545 }
4546}
4547
4548static int fan_get_status(u8 *status)
4549{
4550 u8 s;
4551
4552 /* TODO:
4553 * Add TPACPI_FAN_RD_ACPI_FANS ? */
4554
4555 switch (fan_status_access_mode) {
4556 case TPACPI_FAN_RD_ACPI_GFAN:
4557 /* 570, 600e/x, 770e, 770x */
4558
4559 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
4560 return -EIO;
4561
4562 if (likely(status))
4563 *status = s & 0x07;
4564
4565 break;
4566
4567 case TPACPI_FAN_RD_TPEC:
4568 /* all except 570, 600e/x, 770e, 770x */
4569 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
4570 return -EIO;
4571
4572 if (likely(status))
4573 *status = s;
4574
4575 break;
4576
4577 default:
4578 return -ENXIO;
4579 }
4580
4581 return 0;
4582}
4583
4584static int fan_get_status_safe(u8 *status)
4585{
4586 int rc;
4587 u8 s;
4588
4589 if (mutex_lock_interruptible(&fan_mutex))
4590 return -ERESTARTSYS;
4591 rc = fan_get_status(&s);
4592 if (!rc)
4593 fan_update_desired_level(s);
4594 mutex_unlock(&fan_mutex);
4595
4596 if (status)
4597 *status = s;
4598
4599 return rc;
4600}
4601
4602static int fan_get_speed(unsigned int *speed)
4603{
4604 u8 hi, lo;
4605
4606 switch (fan_status_access_mode) {
4607 case TPACPI_FAN_RD_TPEC:
4608 /* all except 570, 600e/x, 770e, 770x */
4609 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
4610 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
4611 return -EIO;
4612
4613 if (likely(speed))
4614 *speed = (hi << 8) | lo;
4615
4616 break;
4617
4618 default:
4619 return -ENXIO;
4620 }
4621
4622 return 0;
4623}
4624
4625static int fan_set_level(int level)
4626{
4627 if (!fan_control_allowed)
4628 return -EPERM;
4629
4630 switch (fan_control_access_mode) {
4631 case TPACPI_FAN_WR_ACPI_SFAN:
4632 if (level >= 0 && level <= 7) {
4633 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
4634 return -EIO;
4635 } else
4636 return -EINVAL;
4637 break;
4638
4639 case TPACPI_FAN_WR_ACPI_FANS:
4640 case TPACPI_FAN_WR_TPEC:
4641 if ((level != TP_EC_FAN_AUTO) &&
4642 (level != TP_EC_FAN_FULLSPEED) &&
4643 ((level < 0) || (level > 7)))
4644 return -EINVAL;
4645
4646 /* safety net should the EC not support AUTO
4647 * or FULLSPEED mode bits and just ignore them */
4648 if (level & TP_EC_FAN_FULLSPEED)
4649 level |= 7; /* safety min speed 7 */
4650 else if (level & TP_EC_FAN_FULLSPEED)
4651 level |= 4; /* safety min speed 4 */
4652
4653 if (!acpi_ec_write(fan_status_offset, level))
4654 return -EIO;
4655 else
4656 tp_features.fan_ctrl_status_undef = 0;
4657 break;
4658
4659 default:
4660 return -ENXIO;
4661 }
4662 return 0;
4663}
4664
4665static int fan_set_level_safe(int level)
4666{
4667 int rc;
4668
4669 if (!fan_control_allowed)
4670 return -EPERM;
4671
4672 if (mutex_lock_interruptible(&fan_mutex))
4673 return -ERESTARTSYS;
4674
4675 if (level == TPACPI_FAN_LAST_LEVEL)
4676 level = fan_control_desired_level;
4677
4678 rc = fan_set_level(level);
4679 if (!rc)
4680 fan_update_desired_level(level);
4681
4682 mutex_unlock(&fan_mutex);
4683 return rc;
4684}
4685
4686static int fan_set_enable(void)
4687{
4688 u8 s;
4689 int rc;
4690
4691 if (!fan_control_allowed)
4692 return -EPERM;
4693
4694 if (mutex_lock_interruptible(&fan_mutex))
4695 return -ERESTARTSYS;
4696
4697 switch (fan_control_access_mode) {
4698 case TPACPI_FAN_WR_ACPI_FANS:
4699 case TPACPI_FAN_WR_TPEC:
4700 rc = fan_get_status(&s);
4701 if (rc < 0)
4702 break;
4703
4704 /* Don't go out of emergency fan mode */
4705 if (s != 7) {
4706 s &= 0x07;
4707 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
4708 }
4709
4710 if (!acpi_ec_write(fan_status_offset, s))
4711 rc = -EIO;
4712 else {
4713 tp_features.fan_ctrl_status_undef = 0;
4714 rc = 0;
4715 }
4716 break;
4717
4718 case TPACPI_FAN_WR_ACPI_SFAN:
4719 rc = fan_get_status(&s);
4720 if (rc < 0)
4721 break;
4722
4723 s &= 0x07;
4724
4725 /* Set fan to at least level 4 */
4726 s |= 4;
4727
4728 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
4729 rc= -EIO;
4730 else
4731 rc = 0;
4732 break;
4733
4734 default:
4735 rc = -ENXIO;
4736 }
4737
4738 mutex_unlock(&fan_mutex);
4739 return rc;
4740}
4741
4742static int fan_set_disable(void)
4743{
4744 int rc;
4745
4746 if (!fan_control_allowed)
4747 return -EPERM;
4748
4749 if (mutex_lock_interruptible(&fan_mutex))
4750 return -ERESTARTSYS;
4751
4752 rc = 0;
4753 switch (fan_control_access_mode) {
4754 case TPACPI_FAN_WR_ACPI_FANS:
4755 case TPACPI_FAN_WR_TPEC:
4756 if (!acpi_ec_write(fan_status_offset, 0x00))
4757 rc = -EIO;
4758 else {
4759 fan_control_desired_level = 0;
4760 tp_features.fan_ctrl_status_undef = 0;
4761 }
4762 break;
4763
4764 case TPACPI_FAN_WR_ACPI_SFAN:
4765 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
4766 rc = -EIO;
4767 else
4768 fan_control_desired_level = 0;
4769 break;
4770
4771 default:
4772 rc = -ENXIO;
4773 }
4774
4775
4776 mutex_unlock(&fan_mutex);
4777 return rc;
4778}
4779
4780static int fan_set_speed(int speed)
4781{
4782 int rc;
4783
4784 if (!fan_control_allowed)
4785 return -EPERM;
4786
4787 if (mutex_lock_interruptible(&fan_mutex))
4788 return -ERESTARTSYS;
4789
4790 rc = 0;
4791 switch (fan_control_access_mode) {
4792 case TPACPI_FAN_WR_ACPI_FANS:
4793 if (speed >= 0 && speed <= 65535) {
4794 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
4795 speed, speed, speed))
4796 rc = -EIO;
4797 } else
4798 rc = -EINVAL;
4799 break;
4800
4801 default:
4802 rc = -ENXIO;
4803 }
4804
4805 mutex_unlock(&fan_mutex);
4806 return rc;
4807}
4808
4809static void fan_watchdog_reset(void)
4810{
4811 static int fan_watchdog_active;
4812
4813 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
4814 return;
4815
4816 if (fan_watchdog_active)
4817 cancel_delayed_work(&fan_watchdog_task);
4818
4819 if (fan_watchdog_maxinterval > 0 &&
4820 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
4821 fan_watchdog_active = 1;
4822 if (!schedule_delayed_work(&fan_watchdog_task,
4823 msecs_to_jiffies(fan_watchdog_maxinterval
4824 * 1000))) {
4825 printk(IBM_ERR "failed to schedule the fan watchdog, "
4826 "watchdog will not trigger\n");
4827 }
4828 } else
4829 fan_watchdog_active = 0;
4830}
4831
4832static void fan_watchdog_fire(struct work_struct *ignored)
4833{
4834 int rc;
4835
4836 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
4837 return;
4838
4839 printk(IBM_NOTICE "fan watchdog: enabling fan\n");
4840 rc = fan_set_enable();
4841 if (rc < 0) {
4842 printk(IBM_ERR "fan watchdog: error %d while enabling fan, "
4843 "will try again later...\n", -rc);
4844 /* reschedule for later */
4845 fan_watchdog_reset();
4846 }
4847}
4848
4849/*
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004850 * SYSFS fan layout: hwmon compatible (device)
4851 *
4852 * pwm*_enable:
4853 * 0: "disengaged" mode
4854 * 1: manual mode
4855 * 2: native EC "auto" mode (recommended, hardware default)
4856 *
4857 * pwm*: set speed in manual mode, ignored otherwise.
4858 * 0 is level 0; 255 is level 7. Intermediate points done with linear
4859 * interpolation.
4860 *
4861 * fan*_input: tachometer reading, RPM
4862 *
4863 *
4864 * SYSFS fan layout: extensions
4865 *
4866 * fan_watchdog (driver):
4867 * fan watchdog interval in seconds, 0 disables (default), max 120
4868 */
4869
4870/* sysfs fan pwm1_enable ----------------------------------------------- */
4871static ssize_t fan_pwm1_enable_show(struct device *dev,
4872 struct device_attribute *attr,
4873 char *buf)
4874{
4875 int res, mode;
4876 u8 status;
4877
4878 res = fan_get_status_safe(&status);
4879 if (res)
4880 return res;
4881
4882 if (unlikely(tp_features.fan_ctrl_status_undef)) {
4883 if (status != fan_control_initial_status) {
4884 tp_features.fan_ctrl_status_undef = 0;
4885 } else {
4886 /* Return most likely status. In fact, it
4887 * might be the only possible status */
4888 status = TP_EC_FAN_AUTO;
4889 }
4890 }
4891
4892 if (status & TP_EC_FAN_FULLSPEED) {
4893 mode = 0;
4894 } else if (status & TP_EC_FAN_AUTO) {
4895 mode = 2;
4896 } else
4897 mode = 1;
4898
4899 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
4900}
4901
4902static ssize_t fan_pwm1_enable_store(struct device *dev,
4903 struct device_attribute *attr,
4904 const char *buf, size_t count)
4905{
4906 unsigned long t;
4907 int res, level;
4908
4909 if (parse_strtoul(buf, 2, &t))
4910 return -EINVAL;
4911
4912 switch (t) {
4913 case 0:
4914 level = TP_EC_FAN_FULLSPEED;
4915 break;
4916 case 1:
4917 level = TPACPI_FAN_LAST_LEVEL;
4918 break;
4919 case 2:
4920 level = TP_EC_FAN_AUTO;
4921 break;
4922 case 3:
4923 /* reserved for software-controlled auto mode */
4924 return -ENOSYS;
4925 default:
4926 return -EINVAL;
4927 }
4928
4929 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03004930 if (res == -ENXIO)
4931 return -EINVAL;
4932 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004933 return res;
4934
4935 fan_watchdog_reset();
4936
4937 return count;
4938}
4939
4940static struct device_attribute dev_attr_fan_pwm1_enable =
4941 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
4942 fan_pwm1_enable_show, fan_pwm1_enable_store);
4943
4944/* sysfs fan pwm1 ------------------------------------------------------ */
4945static ssize_t fan_pwm1_show(struct device *dev,
4946 struct device_attribute *attr,
4947 char *buf)
4948{
4949 int res;
4950 u8 status;
4951
4952 res = fan_get_status_safe(&status);
4953 if (res)
4954 return res;
4955
4956 if (unlikely(tp_features.fan_ctrl_status_undef)) {
4957 if (status != fan_control_initial_status) {
4958 tp_features.fan_ctrl_status_undef = 0;
4959 } else {
4960 status = TP_EC_FAN_AUTO;
4961 }
4962 }
4963
4964 if ((status &
4965 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
4966 status = fan_control_desired_level;
4967
4968 if (status > 7)
4969 status = 7;
4970
4971 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
4972}
4973
4974static ssize_t fan_pwm1_store(struct device *dev,
4975 struct device_attribute *attr,
4976 const char *buf, size_t count)
4977{
4978 unsigned long s;
4979 int rc;
4980 u8 status, newlevel;
4981
4982 if (parse_strtoul(buf, 255, &s))
4983 return -EINVAL;
4984
4985 /* scale down from 0-255 to 0-7 */
4986 newlevel = (s >> 5) & 0x07;
4987
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02004988 if (mutex_lock_interruptible(&fan_mutex))
4989 return -ERESTARTSYS;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004990
4991 rc = fan_get_status(&status);
4992 if (!rc && (status &
4993 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
4994 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03004995 if (rc == -ENXIO)
4996 rc = -EINVAL;
4997 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03004998 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03004999 fan_watchdog_reset();
5000 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005001 }
5002
5003 mutex_unlock(&fan_mutex);
5004 return (rc)? rc : count;
5005}
5006
5007static struct device_attribute dev_attr_fan_pwm1 =
5008 __ATTR(pwm1, S_IWUSR | S_IRUGO,
5009 fan_pwm1_show, fan_pwm1_store);
5010
5011/* sysfs fan fan1_input ------------------------------------------------ */
5012static ssize_t fan_fan1_input_show(struct device *dev,
5013 struct device_attribute *attr,
5014 char *buf)
5015{
5016 int res;
5017 unsigned int speed;
5018
5019 res = fan_get_speed(&speed);
5020 if (res < 0)
5021 return res;
5022
5023 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
5024}
5025
5026static struct device_attribute dev_attr_fan_fan1_input =
5027 __ATTR(fan1_input, S_IRUGO,
5028 fan_fan1_input_show, NULL);
5029
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005030/* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005031static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
5032 char *buf)
5033{
5034 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
5035}
5036
5037static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
5038 const char *buf, size_t count)
5039{
5040 unsigned long t;
5041
5042 if (parse_strtoul(buf, 120, &t))
5043 return -EINVAL;
5044
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03005045 if (!fan_control_allowed)
5046 return -EPERM;
5047
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005048 fan_watchdog_maxinterval = t;
5049 fan_watchdog_reset();
5050
5051 return count;
5052}
5053
5054static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
5055 fan_fan_watchdog_show, fan_fan_watchdog_store);
5056
5057/* --------------------------------------------------------------------- */
5058static struct attribute *fan_attributes[] = {
5059 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
5060 &dev_attr_fan_fan1_input.attr,
5061 NULL
5062};
5063
5064static const struct attribute_group fan_attr_group = {
5065 .attrs = fan_attributes,
5066};
5067
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005068static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005069{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005070 int rc;
5071
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005072 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
5073
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03005074 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005075 fan_status_access_mode = TPACPI_FAN_NONE;
5076 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005077 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005078 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005079 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005080 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005081
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005082 IBM_ACPIHANDLE_INIT(fans);
5083 IBM_ACPIHANDLE_INIT(gfan);
5084 IBM_ACPIHANDLE_INIT(sfan);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005085
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005086 if (gfan_handle) {
5087 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005088 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005089 } else {
5090 /* all other ThinkPads: note that even old-style
5091 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02005092 if (likely(acpi_ec_read(fan_status_offset,
5093 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005094 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02005095
5096 /* In some ThinkPads, neither the EC nor the ACPI
5097 * DSDT initialize the fan status, and it ends up
5098 * being set to 0x07 when it *could* be either
5099 * 0x07 or 0x80.
5100 *
5101 * Enable for TP-1Y (T43), TP-78 (R51e),
5102 * TP-76 (R52), TP-70 (T43, R52), which are known
5103 * to be buggy. */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005104 if (fan_control_initial_status == 0x07) {
5105 switch (thinkpad_id.ec_model) {
5106 case 0x5931: /* TP-1Y */
5107 case 0x3837: /* TP-78 */
5108 case 0x3637: /* TP-76 */
5109 case 0x3037: /* TP-70 */
5110 printk(IBM_NOTICE
5111 "fan_init: initial fan status is "
5112 "unknown, assuming it is in auto "
5113 "mode\n");
5114 tp_features.fan_ctrl_status_undef = 1;
5115 ;;
5116 }
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02005117 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005118 } else {
5119 printk(IBM_ERR
5120 "ThinkPad ACPI EC access misbehaving, "
5121 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005122 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005123 }
5124 }
5125
5126 if (sfan_handle) {
5127 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005128 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02005129 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005130 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005131 } else {
5132 if (!gfan_handle) {
5133 /* gfan without sfan means no fan control */
5134 /* all other models implement TP EC 0x2f control */
5135
5136 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02005137 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005138 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005139 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005140 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005141 TPACPI_FAN_CMD_SPEED |
5142 TPACPI_FAN_CMD_LEVEL |
5143 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005144 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005145 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02005146 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005147 TPACPI_FAN_CMD_LEVEL |
5148 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005149 }
5150 }
5151 }
5152
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005153 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
5154 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
5155 fan_control_access_mode != TPACPI_FAN_WR_NONE),
5156 fan_status_access_mode, fan_control_access_mode);
5157
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03005158 /* fan control master switch */
5159 if (!fan_control_allowed) {
5160 fan_control_access_mode = TPACPI_FAN_WR_NONE;
5161 fan_control_commands = 0;
5162 dbg_printk(TPACPI_DBG_INIT,
5163 "fan control features disabled by parameter\n");
5164 }
5165
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005166 /* update fan_control_desired_level */
5167 if (fan_status_access_mode != TPACPI_FAN_NONE)
5168 fan_get_status_safe(NULL);
5169
5170 if (fan_status_access_mode != TPACPI_FAN_NONE ||
5171 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005172 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005173 &fan_attr_group);
5174 if (!(rc < 0))
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005175 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005176 &driver_attr_fan_watchdog);
5177 if (rc < 0)
5178 return rc;
5179 return 0;
5180 } else
5181 return 1;
5182}
5183
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005184static void fan_exit(void)
5185{
Henrique de Moraes Holschuh99fba3f2007-04-21 11:08:44 -03005186 vdbg_printk(TPACPI_DBG_EXIT, "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005187
5188 /* FIXME: can we really do this unconditionally? */
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005189 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
5190 driver_remove_file(&tpacpi_hwmon_pdriver.driver, &driver_attr_fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005191
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005192 cancel_delayed_work(&fan_watchdog_task);
5193 flush_scheduled_work();
5194}
5195
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005196static int fan_read(char *p)
5197{
5198 int len = 0;
5199 int rc;
5200 u8 status;
5201 unsigned int speed = 0;
5202
5203 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005204 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005205 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005206 if ((rc = fan_get_status_safe(&status)) < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005207 return rc;
5208
5209 len += sprintf(p + len, "status:\t\t%s\n"
5210 "level:\t\t%d\n",
5211 (status != 0) ? "enabled" : "disabled", status);
5212 break;
5213
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005214 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005215 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005216 if ((rc = fan_get_status_safe(&status)) < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005217 return rc;
5218
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005219 if (unlikely(tp_features.fan_ctrl_status_undef)) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005220 if (status != fan_control_initial_status)
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005221 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005222 else
5223 /* Return most likely status. In fact, it
5224 * might be the only possible status */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005225 status = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005226 }
5227
5228 len += sprintf(p + len, "status:\t\t%s\n",
5229 (status != 0) ? "enabled" : "disabled");
5230
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005231 if ((rc = fan_get_speed(&speed)) < 0)
5232 return rc;
5233
5234 len += sprintf(p + len, "speed:\t\t%d\n", speed);
5235
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005236 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005237 /* Disengaged mode takes precedence */
5238 len += sprintf(p + len, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005239 else if (status & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005240 len += sprintf(p + len, "level:\t\tauto\n");
5241 else
5242 len += sprintf(p + len, "level:\t\t%d\n", status);
5243 break;
5244
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005245 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005246 default:
5247 len += sprintf(p + len, "status:\t\tnot supported\n");
5248 }
5249
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005250 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005251 len += sprintf(p + len, "commands:\tlevel <level>");
5252
5253 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005254 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005255 len += sprintf(p + len, " (<level> is 0-7)\n");
5256 break;
5257
5258 default:
5259 len += sprintf(p + len, " (<level> is 0-7, "
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005260 "auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005261 break;
5262 }
5263 }
5264
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005265 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005266 len += sprintf(p + len, "commands:\tenable, disable\n"
5267 "commands:\twatchdog <timeout> (<timeout> is 0 (off), "
5268 "1-120 (seconds))\n");
5269
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005270 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005271 len += sprintf(p + len, "commands:\tspeed <speed>"
5272 " (<speed> is 0-65535)\n");
5273
5274 return len;
5275}
5276
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005277static int fan_write_cmd_level(const char *cmd, int *rc)
5278{
5279 int level;
5280
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02005281 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005282 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005283 else if ((strlencmp(cmd, "level disengaged") == 0) |
5284 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005285 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02005286 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005287 return 0;
5288
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005289 if ((*rc = fan_set_level_safe(level)) == -ENXIO)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005290 printk(IBM_ERR "level command accepted for unsupported "
5291 "access mode %d", fan_control_access_mode);
5292
5293 return 1;
5294}
5295
5296static int fan_write_cmd_enable(const char *cmd, int *rc)
5297{
5298 if (strlencmp(cmd, "enable") != 0)
5299 return 0;
5300
5301 if ((*rc = fan_set_enable()) == -ENXIO)
5302 printk(IBM_ERR "enable command accepted for unsupported "
5303 "access mode %d", fan_control_access_mode);
5304
5305 return 1;
5306}
5307
5308static int fan_write_cmd_disable(const char *cmd, int *rc)
5309{
5310 if (strlencmp(cmd, "disable") != 0)
5311 return 0;
5312
5313 if ((*rc = fan_set_disable()) == -ENXIO)
5314 printk(IBM_ERR "disable command accepted for unsupported "
5315 "access mode %d", fan_control_access_mode);
5316
5317 return 1;
5318}
5319
5320static int fan_write_cmd_speed(const char *cmd, int *rc)
5321{
5322 int speed;
5323
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02005324 /* TODO:
5325 * Support speed <low> <medium> <high> ? */
5326
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005327 if (sscanf(cmd, "speed %d", &speed) != 1)
5328 return 0;
5329
5330 if ((*rc = fan_set_speed(speed)) == -ENXIO)
5331 printk(IBM_ERR "speed command accepted for unsupported "
5332 "access mode %d", fan_control_access_mode);
5333
5334 return 1;
5335}
5336
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005337static int fan_write_cmd_watchdog(const char *cmd, int *rc)
5338{
5339 int interval;
5340
5341 if (sscanf(cmd, "watchdog %d", &interval) != 1)
5342 return 0;
5343
5344 if (interval < 0 || interval > 120)
5345 *rc = -EINVAL;
5346 else
5347 fan_watchdog_maxinterval = interval;
5348
5349 return 1;
5350}
5351
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005352static int fan_write(char *buf)
5353{
5354 char *cmd;
5355 int rc = 0;
5356
5357 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005358 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005359 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005360 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005361 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005362 fan_write_cmd_disable(cmd, &rc) ||
5363 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005364 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005365 fan_write_cmd_speed(cmd, &rc))
5366 )
5367 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005368 else if (!rc)
5369 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005370 }
5371
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005372 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005373}
5374
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005375static struct ibm_struct fan_driver_data = {
5376 .name = "fan",
5377 .read = fan_read,
5378 .write = fan_write,
5379 .exit = fan_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005380};
5381
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005382/****************************************************************************
5383 ****************************************************************************
5384 *
5385 * Infrastructure
5386 *
5387 ****************************************************************************
5388 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005390/* sysfs name ---------------------------------------------------------- */
5391static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
5392 struct device_attribute *attr,
5393 char *buf)
5394{
5395 return snprintf(buf, PAGE_SIZE, "%s\n", IBM_NAME);
5396}
5397
5398static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
5399 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
5400
5401/* --------------------------------------------------------------------- */
5402
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005403/* /proc support */
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03005404static struct proc_dir_entry *proc_dir;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005405
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005406/*
5407 * Module and infrastructure proble, init and exit handling
5408 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005409
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005410static int force_load;
5411
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005412#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005413static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005414{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005415 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005416
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005417 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005418}
5419#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
5420
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005421static void ibm_exit(struct ibm_struct *ibm)
5422{
5423 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
5424
5425 list_del_init(&ibm->all_drivers);
5426
5427 if (ibm->flags.acpi_notify_installed) {
5428 dbg_printk(TPACPI_DBG_EXIT,
5429 "%s: acpi_remove_notify_handler\n", ibm->name);
5430 BUG_ON(!ibm->acpi);
5431 acpi_remove_notify_handler(*ibm->acpi->handle,
5432 ibm->acpi->type,
5433 dispatch_acpi_notify);
5434 ibm->flags.acpi_notify_installed = 0;
5435 ibm->flags.acpi_notify_installed = 0;
5436 }
5437
5438 if (ibm->flags.proc_created) {
5439 dbg_printk(TPACPI_DBG_EXIT,
5440 "%s: remove_proc_entry\n", ibm->name);
5441 remove_proc_entry(ibm->name, proc_dir);
5442 ibm->flags.proc_created = 0;
5443 }
5444
5445 if (ibm->flags.acpi_driver_registered) {
5446 dbg_printk(TPACPI_DBG_EXIT,
5447 "%s: acpi_bus_unregister_driver\n", ibm->name);
5448 BUG_ON(!ibm->acpi);
5449 acpi_bus_unregister_driver(ibm->acpi->driver);
5450 kfree(ibm->acpi->driver);
5451 ibm->acpi->driver = NULL;
5452 ibm->flags.acpi_driver_registered = 0;
5453 }
5454
5455 if (ibm->flags.init_called && ibm->exit) {
5456 ibm->exit();
5457 ibm->flags.init_called = 0;
5458 }
5459
5460 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
5461}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005462
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005463static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005464{
5465 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005466 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005467 struct proc_dir_entry *entry;
5468
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005469 BUG_ON(ibm == NULL);
5470
5471 INIT_LIST_HEAD(&ibm->all_drivers);
5472
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03005473 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474 return 0;
5475
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005476 dbg_printk(TPACPI_DBG_INIT,
5477 "probing for %s\n", ibm->name);
5478
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005479 if (iibm->init) {
5480 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005481 if (ret > 0)
5482 return 0; /* probe failed */
5483 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005484 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005485
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03005486 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487 }
5488
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005489 if (ibm->acpi) {
5490 if (ibm->acpi->hid) {
5491 ret = register_tpacpi_subdriver(ibm);
5492 if (ret)
5493 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005494 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005495
5496 if (ibm->acpi->notify) {
5497 ret = setup_acpi_notify(ibm);
5498 if (ret == -ENODEV) {
5499 printk(IBM_NOTICE "disabling subdriver %s\n",
5500 ibm->name);
5501 ret = 0;
5502 goto err_out;
5503 }
5504 if (ret < 0)
5505 goto err_out;
5506 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005507 }
5508
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005509 dbg_printk(TPACPI_DBG_INIT,
5510 "%s installed\n", ibm->name);
5511
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005512 if (ibm->read) {
5513 entry = create_proc_entry(ibm->name,
5514 S_IFREG | S_IRUGO | S_IWUSR,
5515 proc_dir);
5516 if (!entry) {
5517 printk(IBM_ERR "unable to create proc entry %s\n",
5518 ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005519 ret = -ENODEV;
5520 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005521 }
5522 entry->owner = THIS_MODULE;
5523 entry->data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005524 entry->read_proc = &dispatch_procfs_read;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005525 if (ibm->write)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005526 entry->write_proc = &dispatch_procfs_write;
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03005527 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005529
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005530 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
5531
Linus Torvalds1da177e2005-04-16 15:20:36 -07005532 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005533
5534err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005535 dbg_printk(TPACPI_DBG_INIT,
5536 "%s: at error exit path with result %d\n",
5537 ibm->name, ret);
5538
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005539 ibm_exit(ibm);
5540 return (ret < 0)? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005541}
5542
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005543/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005544
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005545static void __init get_thinkpad_model_data(struct thinkpad_id_data *tp)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005546{
Jeff Garzik18552562007-10-03 15:15:40 -04005547 const struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02005548 char ec_fw_string[18];
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005549
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005550 if (!tp)
5551 return;
5552
5553 memset(tp, 0, sizeof(*tp));
5554
5555 if (dmi_name_in_vendors("IBM"))
5556 tp->vendor = PCI_VENDOR_ID_IBM;
5557 else if (dmi_name_in_vendors("LENOVO"))
5558 tp->vendor = PCI_VENDOR_ID_LENOVO;
5559 else
5560 return;
5561
5562 tp->bios_version_str = kstrdup(dmi_get_system_info(DMI_BIOS_VERSION),
5563 GFP_KERNEL);
5564 if (!tp->bios_version_str)
5565 return;
5566 tp->bios_model = tp->bios_version_str[0]
5567 | (tp->bios_version_str[1] << 8);
5568
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005569 /*
5570 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
5571 * X32 or newer, all Z series; Some models must have an
5572 * up-to-date BIOS or they will not be detected.
5573 *
5574 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
5575 */
5576 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02005577 if (sscanf(dev->name,
5578 "IBM ThinkPad Embedded Controller -[%17c",
5579 ec_fw_string) == 1) {
5580 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
5581 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005582
5583 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
5584 tp->ec_model = ec_fw_string[0]
5585 | (ec_fw_string[1] << 8);
5586 break;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02005587 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02005588 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005589
5590 tp->model_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_VERSION),
5591 GFP_KERNEL);
5592 if (strnicmp(tp->model_str, "ThinkPad", 8) != 0) {
5593 kfree(tp->model_str);
5594 tp->model_str = NULL;
5595 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005596}
5597
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005598static int __init probe_for_thinkpad(void)
5599{
5600 int is_thinkpad;
5601
5602 if (acpi_disabled)
5603 return -ENODEV;
5604
5605 /*
5606 * Non-ancient models have better DMI tagging, but very old models
5607 * don't.
5608 */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005609 is_thinkpad = (thinkpad_id.model_str != NULL);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005610
5611 /* ec is required because many other handles are relative to it */
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005612 IBM_ACPIHANDLE_INIT(ec);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005613 if (!ec_handle) {
5614 if (is_thinkpad)
5615 printk(IBM_ERR
5616 "Not yet supported ThinkPad detected!\n");
5617 return -ENODEV;
5618 }
5619
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03005620 /*
5621 * Risks a regression on very old machines, but reduces potential
5622 * false positives a damn great deal
5623 */
5624 if (!is_thinkpad)
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005625 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03005626
5627 if (!is_thinkpad && !force_load)
5628 return -ENODEV;
5629
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005630 return 0;
5631}
5632
5633
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005634/* Module init, exit, parameters */
5635
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005636static struct ibm_init_struct ibms_init[] __initdata = {
5637 {
5638 .init = thinkpad_acpi_driver_init,
5639 .data = &thinkpad_acpi_driver_data,
5640 },
5641 {
5642 .init = hotkey_init,
5643 .data = &hotkey_driver_data,
5644 },
5645 {
5646 .init = bluetooth_init,
5647 .data = &bluetooth_driver_data,
5648 },
5649 {
5650 .init = wan_init,
5651 .data = &wan_driver_data,
5652 },
5653 {
5654 .init = video_init,
5655 .data = &video_driver_data,
5656 },
5657 {
5658 .init = light_init,
5659 .data = &light_driver_data,
5660 },
5661#ifdef CONFIG_THINKPAD_ACPI_DOCK
5662 {
5663 .init = dock_init,
5664 .data = &dock_driver_data[0],
5665 },
5666 {
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03005667 .init = dock_init2,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005668 .data = &dock_driver_data[1],
5669 },
5670#endif
5671#ifdef CONFIG_THINKPAD_ACPI_BAY
5672 {
5673 .init = bay_init,
5674 .data = &bay_driver_data,
5675 },
5676#endif
5677 {
5678 .init = cmos_init,
5679 .data = &cmos_driver_data,
5680 },
5681 {
5682 .init = led_init,
5683 .data = &led_driver_data,
5684 },
5685 {
5686 .init = beep_init,
5687 .data = &beep_driver_data,
5688 },
5689 {
5690 .init = thermal_init,
5691 .data = &thermal_driver_data,
5692 },
5693 {
5694 .data = &ecdump_driver_data,
5695 },
5696 {
5697 .init = brightness_init,
5698 .data = &brightness_driver_data,
5699 },
5700 {
5701 .data = &volume_driver_data,
5702 },
5703 {
5704 .init = fan_init,
5705 .data = &fan_driver_data,
5706 },
5707};
5708
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005709static int __init set_ibm_param(const char *val, struct kernel_param *kp)
5710{
5711 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005712 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005713
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02005714 if (!kp || !kp->name || !val)
5715 return -EINVAL;
5716
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005717 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
5718 ibm = ibms_init[i].data;
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02005719 WARN_ON(ibm == NULL);
5720
5721 if (!ibm || !ibm->name)
5722 continue;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005723
5724 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
5725 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005726 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005727 strcpy(ibms_init[i].param, val);
5728 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005729 return 0;
5730 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005731 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005732
5733 return -EINVAL;
5734}
5735
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005736module_param(experimental, int, 0);
5737
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03005738module_param_named(debug, dbg_level, uint, 0);
5739
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03005740module_param(force_load, bool, 0);
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03005741
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03005742module_param_named(fan_control, fan_control_allowed, bool, 0);
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03005743
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03005744module_param_named(brightness_mode, brightness_mode, int, 0);
5745
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02005746module_param(brightness_enable, uint, 0);
5747
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03005748module_param(hotkey_report_mode, uint, 0);
5749
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005750#define IBM_PARAM(feature) \
5751 module_param_call(feature, set_ibm_param, NULL, NULL, 0)
5752
5753IBM_PARAM(hotkey);
5754IBM_PARAM(bluetooth);
5755IBM_PARAM(video);
5756IBM_PARAM(light);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03005757#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005758IBM_PARAM(dock);
5759#endif
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03005760#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005761IBM_PARAM(bay);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03005762#endif /* CONFIG_THINKPAD_ACPI_BAY */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005763IBM_PARAM(cmos);
5764IBM_PARAM(led);
5765IBM_PARAM(beep);
5766IBM_PARAM(ecdump);
5767IBM_PARAM(brightness);
5768IBM_PARAM(volume);
5769IBM_PARAM(fan);
5770
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005771static void thinkpad_acpi_module_exit(void)
5772{
5773 struct ibm_struct *ibm, *itmp;
5774
5775 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
5776
5777 list_for_each_entry_safe_reverse(ibm, itmp,
5778 &tpacpi_all_drivers,
5779 all_drivers) {
5780 ibm_exit(ibm);
5781 }
5782
5783 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
5784
5785 if (tpacpi_inputdev) {
5786 if (tp_features.input_device_registered)
5787 input_unregister_device(tpacpi_inputdev);
5788 else
5789 input_free_device(tpacpi_inputdev);
5790 }
5791
5792 if (tpacpi_hwmon)
5793 hwmon_device_unregister(tpacpi_hwmon);
5794
5795 if (tp_features.sensors_pdev_attrs_registered)
5796 device_remove_file(&tpacpi_sensors_pdev->dev,
5797 &dev_attr_thinkpad_acpi_pdev_name);
5798 if (tpacpi_sensors_pdev)
5799 platform_device_unregister(tpacpi_sensors_pdev);
5800 if (tpacpi_pdev)
5801 platform_device_unregister(tpacpi_pdev);
5802
5803 if (tp_features.sensors_pdrv_attrs_registered)
5804 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
5805 if (tp_features.platform_drv_attrs_registered)
5806 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
5807
5808 if (tp_features.sensors_pdrv_registered)
5809 platform_driver_unregister(&tpacpi_hwmon_pdriver);
5810
5811 if (tp_features.platform_drv_registered)
5812 platform_driver_unregister(&tpacpi_pdriver);
5813
5814 if (proc_dir)
5815 remove_proc_entry(IBM_PROC_DIR, acpi_root_dir);
5816
5817 kfree(thinkpad_id.bios_version_str);
5818 kfree(thinkpad_id.ec_version_str);
5819 kfree(thinkpad_id.model_str);
5820}
5821
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005822
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03005823static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005824{
5825 int ret, i;
5826
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03005827 tpacpi_lifecycle = TPACPI_LIFE_INIT;
5828
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03005829 /* Parameter checking */
5830 if (hotkey_report_mode > 2)
5831 return -EINVAL;
5832
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03005833 /* Driver-level probe */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005834
5835 get_thinkpad_model_data(&thinkpad_id);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005836 ret = probe_for_thinkpad();
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005837 if (ret) {
5838 thinkpad_acpi_module_exit();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005839 return ret;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005840 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005841
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03005842 /* Driver initialization */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005843
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005844 IBM_ACPIHANDLE_INIT(ecrd);
5845 IBM_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005846
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005847 proc_dir = proc_mkdir(IBM_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005848 if (!proc_dir) {
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03005849 printk(IBM_ERR "unable to create proc dir " IBM_PROC_DIR);
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03005850 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005851 return -ENODEV;
5852 }
5853 proc_dir->owner = THIS_MODULE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005854
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03005855 ret = platform_driver_register(&tpacpi_pdriver);
5856 if (ret) {
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005857 printk(IBM_ERR "unable to register main platform driver\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03005858 thinkpad_acpi_module_exit();
5859 return ret;
5860 }
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03005861 tp_features.platform_drv_registered = 1;
5862
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005863 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
5864 if (ret) {
5865 printk(IBM_ERR "unable to register hwmon platform driver\n");
5866 thinkpad_acpi_module_exit();
5867 return ret;
5868 }
5869 tp_features.sensors_pdrv_registered = 1;
5870
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03005871 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03005872 if (!ret) {
5873 tp_features.platform_drv_attrs_registered = 1;
5874 ret = tpacpi_create_driver_attributes(&tpacpi_hwmon_pdriver.driver);
5875 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03005876 if (ret) {
5877 printk(IBM_ERR "unable to create sysfs driver attributes\n");
5878 thinkpad_acpi_module_exit();
5879 return ret;
5880 }
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03005881 tp_features.sensors_pdrv_attrs_registered = 1;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03005882
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03005883
5884 /* Device initialization */
5885 tpacpi_pdev = platform_device_register_simple(IBM_DRVR_NAME, -1,
5886 NULL, 0);
5887 if (IS_ERR(tpacpi_pdev)) {
5888 ret = PTR_ERR(tpacpi_pdev);
5889 tpacpi_pdev = NULL;
5890 printk(IBM_ERR "unable to register platform device\n");
5891 thinkpad_acpi_module_exit();
5892 return ret;
5893 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005894 tpacpi_sensors_pdev = platform_device_register_simple(
5895 IBM_HWMON_DRVR_NAME,
5896 -1, NULL, 0);
5897 if (IS_ERR(tpacpi_sensors_pdev)) {
5898 ret = PTR_ERR(tpacpi_sensors_pdev);
5899 tpacpi_sensors_pdev = NULL;
5900 printk(IBM_ERR "unable to register hwmon platform device\n");
5901 thinkpad_acpi_module_exit();
5902 return ret;
5903 }
5904 ret = device_create_file(&tpacpi_sensors_pdev->dev,
5905 &dev_attr_thinkpad_acpi_pdev_name);
5906 if (ret) {
5907 printk(IBM_ERR
5908 "unable to create sysfs hwmon device attributes\n");
5909 thinkpad_acpi_module_exit();
5910 return ret;
5911 }
5912 tp_features.sensors_pdev_attrs_registered = 1;
5913 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03005914 if (IS_ERR(tpacpi_hwmon)) {
5915 ret = PTR_ERR(tpacpi_hwmon);
5916 tpacpi_hwmon = NULL;
5917 printk(IBM_ERR "unable to register hwmon device\n");
5918 thinkpad_acpi_module_exit();
5919 return ret;
5920 }
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03005921 mutex_init(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03005922 tpacpi_inputdev = input_allocate_device();
5923 if (!tpacpi_inputdev) {
5924 printk(IBM_ERR "unable to allocate input device\n");
5925 thinkpad_acpi_module_exit();
5926 return -ENOMEM;
5927 } else {
5928 /* Prepare input device, but don't register */
5929 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
5930 tpacpi_inputdev->phys = IBM_DRVR_NAME "/input0";
5931 tpacpi_inputdev->id.bustype = BUS_HOST;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03005932 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
5933 thinkpad_id.vendor :
5934 PCI_VENDOR_ID_IBM;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03005935 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
5936 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
5937 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005938 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
5939 ret = ibm_init(&ibms_init[i]);
5940 if (ret >= 0 && *ibms_init[i].param)
5941 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005942 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03005943 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005944 return ret;
5945 }
5946 }
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03005947 ret = input_register_device(tpacpi_inputdev);
5948 if (ret < 0) {
5949 printk(IBM_ERR "unable to register input device\n");
5950 thinkpad_acpi_module_exit();
5951 return ret;
5952 } else {
5953 tp_features.input_device_registered = 1;
5954 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005955
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03005956 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005957 return 0;
5958}
5959
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03005960module_init(thinkpad_acpi_module_init);
5961module_exit(thinkpad_acpi_module_exit);